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                "{\n"
2456                "  A,\n"
2457                "  B,\n"
2458                "  C\n"
2459                "} ShortEnum1, ShortEnum2;",
2460                Style);
2461 }
2462 
2463 TEST_F(FormatTest, ShortCaseLabels) {
2464   FormatStyle Style = getLLVMStyle();
2465   Style.AllowShortCaseLabelsOnASingleLine = true;
2466   verifyFormat("switch (a) {\n"
2467                "case 1: x = 1; break;\n"
2468                "case 2: return;\n"
2469                "case 3:\n"
2470                "case 4:\n"
2471                "case 5: return;\n"
2472                "case 6: // comment\n"
2473                "  return;\n"
2474                "case 7:\n"
2475                "  // comment\n"
2476                "  return;\n"
2477                "case 8:\n"
2478                "  x = 8; // comment\n"
2479                "  break;\n"
2480                "default: y = 1; break;\n"
2481                "}",
2482                Style);
2483   verifyFormat("switch (a) {\n"
2484                "case 0: return; // comment\n"
2485                "case 1: break;  // comment\n"
2486                "case 2: return;\n"
2487                "// comment\n"
2488                "case 3: return;\n"
2489                "// comment 1\n"
2490                "// comment 2\n"
2491                "// comment 3\n"
2492                "case 4: break; /* comment */\n"
2493                "case 5:\n"
2494                "  // comment\n"
2495                "  break;\n"
2496                "case 6: /* comment */ x = 1; break;\n"
2497                "case 7: x = /* comment */ 1; break;\n"
2498                "case 8:\n"
2499                "  x = 1; /* comment */\n"
2500                "  break;\n"
2501                "case 9:\n"
2502                "  break; // comment line 1\n"
2503                "         // comment line 2\n"
2504                "}",
2505                Style);
2506   EXPECT_EQ("switch (a) {\n"
2507             "case 1:\n"
2508             "  x = 8;\n"
2509             "  // fall through\n"
2510             "case 2: x = 8;\n"
2511             "// comment\n"
2512             "case 3:\n"
2513             "  return; /* comment line 1\n"
2514             "           * comment line 2 */\n"
2515             "case 4: i = 8;\n"
2516             "// something else\n"
2517             "#if FOO\n"
2518             "case 5: break;\n"
2519             "#endif\n"
2520             "}",
2521             format("switch (a) {\n"
2522                    "case 1: x = 8;\n"
2523                    "  // fall through\n"
2524                    "case 2:\n"
2525                    "  x = 8;\n"
2526                    "// comment\n"
2527                    "case 3:\n"
2528                    "  return; /* comment line 1\n"
2529                    "           * comment line 2 */\n"
2530                    "case 4:\n"
2531                    "  i = 8;\n"
2532                    "// something else\n"
2533                    "#if FOO\n"
2534                    "case 5: break;\n"
2535                    "#endif\n"
2536                    "}",
2537                    Style));
2538   EXPECT_EQ("switch (a) {\n"
2539             "case 0:\n"
2540             "  return; // long long long long long long long long long long "
2541             "long long comment\n"
2542             "          // line\n"
2543             "}",
2544             format("switch (a) {\n"
2545                    "case 0: return; // long long long long long long long long "
2546                    "long long long long comment line\n"
2547                    "}",
2548                    Style));
2549   EXPECT_EQ("switch (a) {\n"
2550             "case 0:\n"
2551             "  return; /* long long long long long long long long long long "
2552             "long long comment\n"
2553             "             line */\n"
2554             "}",
2555             format("switch (a) {\n"
2556                    "case 0: return; /* long long long long long long long long "
2557                    "long long long long comment line */\n"
2558                    "}",
2559                    Style));
2560   verifyFormat("switch (a) {\n"
2561                "#if FOO\n"
2562                "case 0: return 0;\n"
2563                "#endif\n"
2564                "}",
2565                Style);
2566   verifyFormat("switch (a) {\n"
2567                "case 1: {\n"
2568                "}\n"
2569                "case 2: {\n"
2570                "  return;\n"
2571                "}\n"
2572                "case 3: {\n"
2573                "  x = 1;\n"
2574                "  return;\n"
2575                "}\n"
2576                "case 4:\n"
2577                "  if (x)\n"
2578                "    return;\n"
2579                "}",
2580                Style);
2581   Style.ColumnLimit = 21;
2582   verifyFormat("switch (a) {\n"
2583                "case 1: x = 1; break;\n"
2584                "case 2: return;\n"
2585                "case 3:\n"
2586                "case 4:\n"
2587                "case 5: return;\n"
2588                "default:\n"
2589                "  y = 1;\n"
2590                "  break;\n"
2591                "}",
2592                Style);
2593   Style.ColumnLimit = 80;
2594   Style.AllowShortCaseLabelsOnASingleLine = false;
2595   Style.IndentCaseLabels = true;
2596   EXPECT_EQ("switch (n) {\n"
2597             "  default /*comments*/:\n"
2598             "    return true;\n"
2599             "  case 0:\n"
2600             "    return false;\n"
2601             "}",
2602             format("switch (n) {\n"
2603                    "default/*comments*/:\n"
2604                    "  return true;\n"
2605                    "case 0:\n"
2606                    "  return false;\n"
2607                    "}",
2608                    Style));
2609   Style.AllowShortCaseLabelsOnASingleLine = true;
2610   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
2611   Style.BraceWrapping.AfterCaseLabel = true;
2612   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
2613   EXPECT_EQ("switch (n)\n"
2614             "{\n"
2615             "  case 0:\n"
2616             "  {\n"
2617             "    return false;\n"
2618             "  }\n"
2619             "  default:\n"
2620             "  {\n"
2621             "    return true;\n"
2622             "  }\n"
2623             "}",
2624             format("switch (n) {\n"
2625                    "  case 0: {\n"
2626                    "    return false;\n"
2627                    "  }\n"
2628                    "  default:\n"
2629                    "  {\n"
2630                    "    return true;\n"
2631                    "  }\n"
2632                    "}",
2633                    Style));
2634 }
2635 
2636 TEST_F(FormatTest, FormatsLabels) {
2637   verifyFormat("void f() {\n"
2638                "  some_code();\n"
2639                "test_label:\n"
2640                "  some_other_code();\n"
2641                "  {\n"
2642                "    some_more_code();\n"
2643                "  another_label:\n"
2644                "    some_more_code();\n"
2645                "  }\n"
2646                "}");
2647   verifyFormat("{\n"
2648                "  some_code();\n"
2649                "test_label:\n"
2650                "  some_other_code();\n"
2651                "}");
2652   verifyFormat("{\n"
2653                "  some_code();\n"
2654                "test_label:;\n"
2655                "  int i = 0;\n"
2656                "}");
2657   FormatStyle Style = getLLVMStyle();
2658   Style.IndentGotoLabels = false;
2659   verifyFormat("void f() {\n"
2660                "  some_code();\n"
2661                "test_label:\n"
2662                "  some_other_code();\n"
2663                "  {\n"
2664                "    some_more_code();\n"
2665                "another_label:\n"
2666                "    some_more_code();\n"
2667                "  }\n"
2668                "}",
2669                Style);
2670   verifyFormat("{\n"
2671                "  some_code();\n"
2672                "test_label:\n"
2673                "  some_other_code();\n"
2674                "}",
2675                Style);
2676   verifyFormat("{\n"
2677                "  some_code();\n"
2678                "test_label:;\n"
2679                "  int i = 0;\n"
2680                "}");
2681 }
2682 
2683 TEST_F(FormatTest, MultiLineControlStatements) {
2684   FormatStyle Style = getLLVMStyle();
2685   Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom;
2686   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine;
2687   Style.ColumnLimit = 20;
2688   // Short lines should keep opening brace on same line.
2689   EXPECT_EQ("if (foo) {\n"
2690             "  bar();\n"
2691             "}",
2692             format("if(foo){bar();}", Style));
2693   EXPECT_EQ("if (foo) {\n"
2694             "  bar();\n"
2695             "} else {\n"
2696             "  baz();\n"
2697             "}",
2698             format("if(foo){bar();}else{baz();}", Style));
2699   EXPECT_EQ("if (foo && bar) {\n"
2700             "  baz();\n"
2701             "}",
2702             format("if(foo&&bar){baz();}", Style));
2703   EXPECT_EQ("if (foo) {\n"
2704             "  bar();\n"
2705             "} else if (baz) {\n"
2706             "  quux();\n"
2707             "}",
2708             format("if(foo){bar();}else if(baz){quux();}", Style));
2709   EXPECT_EQ(
2710       "if (foo) {\n"
2711       "  bar();\n"
2712       "} else if (baz) {\n"
2713       "  quux();\n"
2714       "} else {\n"
2715       "  foobar();\n"
2716       "}",
2717       format("if(foo){bar();}else if(baz){quux();}else{foobar();}", Style));
2718   EXPECT_EQ("for (;;) {\n"
2719             "  foo();\n"
2720             "}",
2721             format("for(;;){foo();}"));
2722   EXPECT_EQ("while (1) {\n"
2723             "  foo();\n"
2724             "}",
2725             format("while(1){foo();}", Style));
2726   EXPECT_EQ("switch (foo) {\n"
2727             "case bar:\n"
2728             "  return;\n"
2729             "}",
2730             format("switch(foo){case bar:return;}", Style));
2731   EXPECT_EQ("try {\n"
2732             "  foo();\n"
2733             "} catch (...) {\n"
2734             "  bar();\n"
2735             "}",
2736             format("try{foo();}catch(...){bar();}", Style));
2737   EXPECT_EQ("do {\n"
2738             "  foo();\n"
2739             "} while (bar &&\n"
2740             "         baz);",
2741             format("do{foo();}while(bar&&baz);", Style));
2742   // Long lines should put opening brace on new line.
2743   EXPECT_EQ("if (foo && bar &&\n"
2744             "    baz)\n"
2745             "{\n"
2746             "  quux();\n"
2747             "}",
2748             format("if(foo&&bar&&baz){quux();}", Style));
2749   EXPECT_EQ("if (foo && bar &&\n"
2750             "    baz)\n"
2751             "{\n"
2752             "  quux();\n"
2753             "}",
2754             format("if (foo && bar &&\n"
2755                    "    baz) {\n"
2756                    "  quux();\n"
2757                    "}",
2758                    Style));
2759   EXPECT_EQ("if (foo) {\n"
2760             "  bar();\n"
2761             "} else if (baz ||\n"
2762             "           quux)\n"
2763             "{\n"
2764             "  foobar();\n"
2765             "}",
2766             format("if(foo){bar();}else if(baz||quux){foobar();}", Style));
2767   EXPECT_EQ(
2768       "if (foo) {\n"
2769       "  bar();\n"
2770       "} else if (baz ||\n"
2771       "           quux)\n"
2772       "{\n"
2773       "  foobar();\n"
2774       "} else {\n"
2775       "  barbaz();\n"
2776       "}",
2777       format("if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}",
2778              Style));
2779   EXPECT_EQ("for (int i = 0;\n"
2780             "     i < 10; ++i)\n"
2781             "{\n"
2782             "  foo();\n"
2783             "}",
2784             format("for(int i=0;i<10;++i){foo();}", Style));
2785   EXPECT_EQ("foreach (int i,\n"
2786             "         list)\n"
2787             "{\n"
2788             "  foo();\n"
2789             "}",
2790             format("foreach(int i, list){foo();}", Style));
2791   Style.ColumnLimit =
2792       40; // to concentrate at brace wrapping, not line wrap due to column limit
2793   EXPECT_EQ("foreach (int i, list) {\n"
2794             "  foo();\n"
2795             "}",
2796             format("foreach(int i, list){foo();}", Style));
2797   Style.ColumnLimit =
2798       20; // to concentrate at brace wrapping, not line wrap due to column limit
2799   EXPECT_EQ("while (foo || bar ||\n"
2800             "       baz)\n"
2801             "{\n"
2802             "  quux();\n"
2803             "}",
2804             format("while(foo||bar||baz){quux();}", Style));
2805   EXPECT_EQ("switch (\n"
2806             "    foo = barbaz)\n"
2807             "{\n"
2808             "case quux:\n"
2809             "  return;\n"
2810             "}",
2811             format("switch(foo=barbaz){case quux:return;}", Style));
2812   EXPECT_EQ("try {\n"
2813             "  foo();\n"
2814             "} catch (\n"
2815             "    Exception &bar)\n"
2816             "{\n"
2817             "  baz();\n"
2818             "}",
2819             format("try{foo();}catch(Exception&bar){baz();}", Style));
2820   Style.ColumnLimit =
2821       40; // to concentrate at brace wrapping, not line wrap due to column limit
2822   EXPECT_EQ("try {\n"
2823             "  foo();\n"
2824             "} catch (Exception &bar) {\n"
2825             "  baz();\n"
2826             "}",
2827             format("try{foo();}catch(Exception&bar){baz();}", Style));
2828   Style.ColumnLimit =
2829       20; // to concentrate at brace wrapping, not line wrap due to column limit
2830 
2831   Style.BraceWrapping.BeforeElse = true;
2832   EXPECT_EQ(
2833       "if (foo) {\n"
2834       "  bar();\n"
2835       "}\n"
2836       "else if (baz ||\n"
2837       "         quux)\n"
2838       "{\n"
2839       "  foobar();\n"
2840       "}\n"
2841       "else {\n"
2842       "  barbaz();\n"
2843       "}",
2844       format("if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}",
2845              Style));
2846 
2847   Style.BraceWrapping.BeforeCatch = true;
2848   EXPECT_EQ("try {\n"
2849             "  foo();\n"
2850             "}\n"
2851             "catch (...) {\n"
2852             "  baz();\n"
2853             "}",
2854             format("try{foo();}catch(...){baz();}", Style));
2855 }
2856 
2857 TEST_F(FormatTest, BeforeWhile) {
2858   FormatStyle Style = getLLVMStyle();
2859   Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom;
2860 
2861   verifyFormat("do {\n"
2862                "  foo();\n"
2863                "} while (1);",
2864                Style);
2865   Style.BraceWrapping.BeforeWhile = true;
2866   verifyFormat("do {\n"
2867                "  foo();\n"
2868                "}\n"
2869                "while (1);",
2870                Style);
2871 }
2872 
2873 //===----------------------------------------------------------------------===//
2874 // Tests for classes, namespaces, etc.
2875 //===----------------------------------------------------------------------===//
2876 
2877 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
2878   verifyFormat("class A {};");
2879 }
2880 
2881 TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
2882   verifyFormat("class A {\n"
2883                "public:\n"
2884                "public: // comment\n"
2885                "protected:\n"
2886                "private:\n"
2887                "  void f() {}\n"
2888                "};");
2889   verifyFormat("export class A {\n"
2890                "public:\n"
2891                "public: // comment\n"
2892                "protected:\n"
2893                "private:\n"
2894                "  void f() {}\n"
2895                "};");
2896   verifyGoogleFormat("class A {\n"
2897                      " public:\n"
2898                      " protected:\n"
2899                      " private:\n"
2900                      "  void f() {}\n"
2901                      "};");
2902   verifyGoogleFormat("export class A {\n"
2903                      " public:\n"
2904                      " protected:\n"
2905                      " private:\n"
2906                      "  void f() {}\n"
2907                      "};");
2908   verifyFormat("class A {\n"
2909                "public slots:\n"
2910                "  void f1() {}\n"
2911                "public Q_SLOTS:\n"
2912                "  void f2() {}\n"
2913                "protected slots:\n"
2914                "  void f3() {}\n"
2915                "protected Q_SLOTS:\n"
2916                "  void f4() {}\n"
2917                "private slots:\n"
2918                "  void f5() {}\n"
2919                "private Q_SLOTS:\n"
2920                "  void f6() {}\n"
2921                "signals:\n"
2922                "  void g1();\n"
2923                "Q_SIGNALS:\n"
2924                "  void g2();\n"
2925                "};");
2926 
2927   // Don't interpret 'signals' the wrong way.
2928   verifyFormat("signals.set();");
2929   verifyFormat("for (Signals signals : f()) {\n}");
2930   verifyFormat("{\n"
2931                "  signals.set(); // This needs indentation.\n"
2932                "}");
2933   verifyFormat("void f() {\n"
2934                "label:\n"
2935                "  signals.baz();\n"
2936                "}");
2937 }
2938 
2939 TEST_F(FormatTest, SeparatesLogicalBlocks) {
2940   EXPECT_EQ("class A {\n"
2941             "public:\n"
2942             "  void f();\n"
2943             "\n"
2944             "private:\n"
2945             "  void g() {}\n"
2946             "  // test\n"
2947             "protected:\n"
2948             "  int h;\n"
2949             "};",
2950             format("class A {\n"
2951                    "public:\n"
2952                    "void f();\n"
2953                    "private:\n"
2954                    "void g() {}\n"
2955                    "// test\n"
2956                    "protected:\n"
2957                    "int h;\n"
2958                    "};"));
2959   EXPECT_EQ("class A {\n"
2960             "protected:\n"
2961             "public:\n"
2962             "  void f();\n"
2963             "};",
2964             format("class A {\n"
2965                    "protected:\n"
2966                    "\n"
2967                    "public:\n"
2968                    "\n"
2969                    "  void f();\n"
2970                    "};"));
2971 
2972   // Even ensure proper spacing inside macros.
2973   EXPECT_EQ("#define B     \\\n"
2974             "  class A {   \\\n"
2975             "   protected: \\\n"
2976             "   public:    \\\n"
2977             "    void f(); \\\n"
2978             "  };",
2979             format("#define B     \\\n"
2980                    "  class A {   \\\n"
2981                    "   protected: \\\n"
2982                    "              \\\n"
2983                    "   public:    \\\n"
2984                    "              \\\n"
2985                    "    void f(); \\\n"
2986                    "  };",
2987                    getGoogleStyle()));
2988   // But don't remove empty lines after macros ending in access specifiers.
2989   EXPECT_EQ("#define A private:\n"
2990             "\n"
2991             "int i;",
2992             format("#define A         private:\n"
2993                    "\n"
2994                    "int              i;"));
2995 }
2996 
2997 TEST_F(FormatTest, FormatsClasses) {
2998   verifyFormat("class A : public B {};");
2999   verifyFormat("class A : public ::B {};");
3000 
3001   verifyFormat(
3002       "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3003       "                             public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
3004   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
3005                "    : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3006                "      public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
3007   verifyFormat(
3008       "class A : public B, public C, public D, public E, public F {};");
3009   verifyFormat("class AAAAAAAAAAAA : public B,\n"
3010                "                     public C,\n"
3011                "                     public D,\n"
3012                "                     public E,\n"
3013                "                     public F,\n"
3014                "                     public G {};");
3015 
3016   verifyFormat("class\n"
3017                "    ReallyReallyLongClassName {\n"
3018                "  int i;\n"
3019                "};",
3020                getLLVMStyleWithColumns(32));
3021   verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
3022                "                           aaaaaaaaaaaaaaaa> {};");
3023   verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n"
3024                "    : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n"
3025                "                                 aaaaaaaaaaaaaaaaaaaaaa> {};");
3026   verifyFormat("template <class R, class C>\n"
3027                "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n"
3028                "    : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};");
3029   verifyFormat("class ::A::B {};");
3030 }
3031 
3032 TEST_F(FormatTest, BreakInheritanceStyle) {
3033   FormatStyle StyleWithInheritanceBreakBeforeComma = getLLVMStyle();
3034   StyleWithInheritanceBreakBeforeComma.BreakInheritanceList =
3035       FormatStyle::BILS_BeforeComma;
3036   verifyFormat("class MyClass : public X {};",
3037                StyleWithInheritanceBreakBeforeComma);
3038   verifyFormat("class MyClass\n"
3039                "    : public X\n"
3040                "    , public Y {};",
3041                StyleWithInheritanceBreakBeforeComma);
3042   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA\n"
3043                "    : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n"
3044                "    , public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};",
3045                StyleWithInheritanceBreakBeforeComma);
3046   verifyFormat("struct aaaaaaaaaaaaa\n"
3047                "    : public aaaaaaaaaaaaaaaaaaa< // break\n"
3048                "          aaaaaaaaaaaaaaaa> {};",
3049                StyleWithInheritanceBreakBeforeComma);
3050 
3051   FormatStyle StyleWithInheritanceBreakAfterColon = getLLVMStyle();
3052   StyleWithInheritanceBreakAfterColon.BreakInheritanceList =
3053       FormatStyle::BILS_AfterColon;
3054   verifyFormat("class MyClass : public X {};",
3055                StyleWithInheritanceBreakAfterColon);
3056   verifyFormat("class MyClass : public X, public Y {};",
3057                StyleWithInheritanceBreakAfterColon);
3058   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA :\n"
3059                "    public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3060                "    public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};",
3061                StyleWithInheritanceBreakAfterColon);
3062   verifyFormat("struct aaaaaaaaaaaaa :\n"
3063                "    public aaaaaaaaaaaaaaaaaaa< // break\n"
3064                "        aaaaaaaaaaaaaaaa> {};",
3065                StyleWithInheritanceBreakAfterColon);
3066 
3067   FormatStyle StyleWithInheritanceBreakAfterComma = getLLVMStyle();
3068   StyleWithInheritanceBreakAfterComma.BreakInheritanceList =
3069       FormatStyle::BILS_AfterComma;
3070   verifyFormat("class MyClass : public X {};",
3071                StyleWithInheritanceBreakAfterComma);
3072   verifyFormat("class MyClass : public X,\n"
3073                "                public Y {};",
3074                StyleWithInheritanceBreakAfterComma);
3075   verifyFormat(
3076       "class AAAAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3077       "                               public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC "
3078       "{};",
3079       StyleWithInheritanceBreakAfterComma);
3080   verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
3081                "                           aaaaaaaaaaaaaaaa> {};",
3082                StyleWithInheritanceBreakAfterComma);
3083   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
3084                "    : public OnceBreak,\n"
3085                "      public AlwaysBreak,\n"
3086                "      EvenBasesFitInOneLine {};",
3087                StyleWithInheritanceBreakAfterComma);
3088 }
3089 
3090 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
3091   verifyFormat("class A {\n} a, b;");
3092   verifyFormat("struct A {\n} a, b;");
3093   verifyFormat("union A {\n} a;");
3094 }
3095 
3096 TEST_F(FormatTest, FormatsEnum) {
3097   verifyFormat("enum {\n"
3098                "  Zero,\n"
3099                "  One = 1,\n"
3100                "  Two = One + 1,\n"
3101                "  Three = (One + Two),\n"
3102                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
3103                "  Five = (One, Two, Three, Four, 5)\n"
3104                "};");
3105   verifyGoogleFormat("enum {\n"
3106                      "  Zero,\n"
3107                      "  One = 1,\n"
3108                      "  Two = One + 1,\n"
3109                      "  Three = (One + Two),\n"
3110                      "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
3111                      "  Five = (One, Two, Three, Four, 5)\n"
3112                      "};");
3113   verifyFormat("enum Enum {};");
3114   verifyFormat("enum {};");
3115   verifyFormat("enum X E {} d;");
3116   verifyFormat("enum __attribute__((...)) E {} d;");
3117   verifyFormat("enum __declspec__((...)) E {} d;");
3118   verifyFormat("enum {\n"
3119                "  Bar = Foo<int, int>::value\n"
3120                "};",
3121                getLLVMStyleWithColumns(30));
3122 
3123   verifyFormat("enum ShortEnum { A, B, C };");
3124   verifyGoogleFormat("enum ShortEnum { A, B, C };");
3125 
3126   EXPECT_EQ("enum KeepEmptyLines {\n"
3127             "  ONE,\n"
3128             "\n"
3129             "  TWO,\n"
3130             "\n"
3131             "  THREE\n"
3132             "}",
3133             format("enum KeepEmptyLines {\n"
3134                    "  ONE,\n"
3135                    "\n"
3136                    "  TWO,\n"
3137                    "\n"
3138                    "\n"
3139                    "  THREE\n"
3140                    "}"));
3141   verifyFormat("enum E { // comment\n"
3142                "  ONE,\n"
3143                "  TWO\n"
3144                "};\n"
3145                "int i;");
3146 
3147   FormatStyle EightIndent = getLLVMStyle();
3148   EightIndent.IndentWidth = 8;
3149   verifyFormat("enum {\n"
3150                "        VOID,\n"
3151                "        CHAR,\n"
3152                "        SHORT,\n"
3153                "        INT,\n"
3154                "        LONG,\n"
3155                "        SIGNED,\n"
3156                "        UNSIGNED,\n"
3157                "        BOOL,\n"
3158                "        FLOAT,\n"
3159                "        DOUBLE,\n"
3160                "        COMPLEX\n"
3161                "};",
3162                EightIndent);
3163 
3164   // Not enums.
3165   verifyFormat("enum X f() {\n"
3166                "  a();\n"
3167                "  return 42;\n"
3168                "}");
3169   verifyFormat("enum X Type::f() {\n"
3170                "  a();\n"
3171                "  return 42;\n"
3172                "}");
3173   verifyFormat("enum ::X f() {\n"
3174                "  a();\n"
3175                "  return 42;\n"
3176                "}");
3177   verifyFormat("enum ns::X f() {\n"
3178                "  a();\n"
3179                "  return 42;\n"
3180                "}");
3181 }
3182 
3183 TEST_F(FormatTest, FormatsEnumsWithErrors) {
3184   verifyFormat("enum Type {\n"
3185                "  One = 0; // These semicolons should be commas.\n"
3186                "  Two = 1;\n"
3187                "};");
3188   verifyFormat("namespace n {\n"
3189                "enum Type {\n"
3190                "  One,\n"
3191                "  Two, // missing };\n"
3192                "  int i;\n"
3193                "}\n"
3194                "void g() {}");
3195 }
3196 
3197 TEST_F(FormatTest, FormatsEnumStruct) {
3198   verifyFormat("enum struct {\n"
3199                "  Zero,\n"
3200                "  One = 1,\n"
3201                "  Two = One + 1,\n"
3202                "  Three = (One + Two),\n"
3203                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
3204                "  Five = (One, Two, Three, Four, 5)\n"
3205                "};");
3206   verifyFormat("enum struct Enum {};");
3207   verifyFormat("enum struct {};");
3208   verifyFormat("enum struct X E {} d;");
3209   verifyFormat("enum struct __attribute__((...)) E {} d;");
3210   verifyFormat("enum struct __declspec__((...)) E {} d;");
3211   verifyFormat("enum struct X f() {\n  a();\n  return 42;\n}");
3212 }
3213 
3214 TEST_F(FormatTest, FormatsEnumClass) {
3215   verifyFormat("enum class {\n"
3216                "  Zero,\n"
3217                "  One = 1,\n"
3218                "  Two = One + 1,\n"
3219                "  Three = (One + Two),\n"
3220                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
3221                "  Five = (One, Two, Three, Four, 5)\n"
3222                "};");
3223   verifyFormat("enum class Enum {};");
3224   verifyFormat("enum class {};");
3225   verifyFormat("enum class X E {} d;");
3226   verifyFormat("enum class __attribute__((...)) E {} d;");
3227   verifyFormat("enum class __declspec__((...)) E {} d;");
3228   verifyFormat("enum class X f() {\n  a();\n  return 42;\n}");
3229 }
3230 
3231 TEST_F(FormatTest, FormatsEnumTypes) {
3232   verifyFormat("enum X : int {\n"
3233                "  A, // Force multiple lines.\n"
3234                "  B\n"
3235                "};");
3236   verifyFormat("enum X : int { A, B };");
3237   verifyFormat("enum X : std::uint32_t { A, B };");
3238 }
3239 
3240 TEST_F(FormatTest, FormatsTypedefEnum) {
3241   FormatStyle Style = getLLVMStyle();
3242   Style.ColumnLimit = 40;
3243   verifyFormat("typedef enum {} EmptyEnum;");
3244   verifyFormat("typedef enum { A, B, C } ShortEnum;");
3245   verifyFormat("typedef enum {\n"
3246                "  ZERO = 0,\n"
3247                "  ONE = 1,\n"
3248                "  TWO = 2,\n"
3249                "  THREE = 3\n"
3250                "} LongEnum;",
3251                Style);
3252   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
3253   Style.BraceWrapping.AfterEnum = true;
3254   verifyFormat("typedef enum {} EmptyEnum;");
3255   verifyFormat("typedef enum { A, B, C } ShortEnum;");
3256   verifyFormat("typedef enum\n"
3257                "{\n"
3258                "  ZERO = 0,\n"
3259                "  ONE = 1,\n"
3260                "  TWO = 2,\n"
3261                "  THREE = 3\n"
3262                "} LongEnum;",
3263                Style);
3264 }
3265 
3266 TEST_F(FormatTest, FormatsNSEnums) {
3267   verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }");
3268   verifyGoogleFormat(
3269       "typedef NS_CLOSED_ENUM(NSInteger, SomeName) { AAA, BBB }");
3270   verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n"
3271                      "  // Information about someDecentlyLongValue.\n"
3272                      "  someDecentlyLongValue,\n"
3273                      "  // Information about anotherDecentlyLongValue.\n"
3274                      "  anotherDecentlyLongValue,\n"
3275                      "  // Information about aThirdDecentlyLongValue.\n"
3276                      "  aThirdDecentlyLongValue\n"
3277                      "};");
3278   verifyGoogleFormat("typedef NS_CLOSED_ENUM(NSInteger, MyType) {\n"
3279                      "  // Information about someDecentlyLongValue.\n"
3280                      "  someDecentlyLongValue,\n"
3281                      "  // Information about anotherDecentlyLongValue.\n"
3282                      "  anotherDecentlyLongValue,\n"
3283                      "  // Information about aThirdDecentlyLongValue.\n"
3284                      "  aThirdDecentlyLongValue\n"
3285                      "};");
3286   verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n"
3287                      "  a = 1,\n"
3288                      "  b = 2,\n"
3289                      "  c = 3,\n"
3290                      "};");
3291   verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n"
3292                      "  a = 1,\n"
3293                      "  b = 2,\n"
3294                      "  c = 3,\n"
3295                      "};");
3296   verifyGoogleFormat("typedef CF_CLOSED_ENUM(NSInteger, MyType) {\n"
3297                      "  a = 1,\n"
3298                      "  b = 2,\n"
3299                      "  c = 3,\n"
3300                      "};");
3301   verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n"
3302                      "  a = 1,\n"
3303                      "  b = 2,\n"
3304                      "  c = 3,\n"
3305                      "};");
3306 }
3307 
3308 TEST_F(FormatTest, FormatsBitfields) {
3309   verifyFormat("struct Bitfields {\n"
3310                "  unsigned sClass : 8;\n"
3311                "  unsigned ValueKind : 2;\n"
3312                "};");
3313   verifyFormat("struct A {\n"
3314                "  int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n"
3315                "      bbbbbbbbbbbbbbbbbbbbbbbbb;\n"
3316                "};");
3317   verifyFormat("struct MyStruct {\n"
3318                "  uchar data;\n"
3319                "  uchar : 8;\n"
3320                "  uchar : 8;\n"
3321                "  uchar other;\n"
3322                "};");
3323   FormatStyle Style = getLLVMStyle();
3324   Style.BitFieldColonSpacing = FormatStyle::BFCS_None;
3325   verifyFormat("struct Bitfields {\n"
3326                "  unsigned sClass:8;\n"
3327                "  unsigned ValueKind:2;\n"
3328                "  uchar other;\n"
3329                "};",
3330                Style);
3331   verifyFormat("struct A {\n"
3332                "  int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:1,\n"
3333                "      bbbbbbbbbbbbbbbbbbbbbbbbb:2;\n"
3334                "};",
3335                Style);
3336   Style.BitFieldColonSpacing = FormatStyle::BFCS_Before;
3337   verifyFormat("struct Bitfields {\n"
3338                "  unsigned sClass :8;\n"
3339                "  unsigned ValueKind :2;\n"
3340                "  uchar other;\n"
3341                "};",
3342                Style);
3343   Style.BitFieldColonSpacing = FormatStyle::BFCS_After;
3344   verifyFormat("struct Bitfields {\n"
3345                "  unsigned sClass: 8;\n"
3346                "  unsigned ValueKind: 2;\n"
3347                "  uchar other;\n"
3348                "};",
3349                Style);
3350 }
3351 
3352 TEST_F(FormatTest, FormatsNamespaces) {
3353   FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle();
3354   LLVMWithNoNamespaceFix.FixNamespaceComments = false;
3355 
3356   verifyFormat("namespace some_namespace {\n"
3357                "class A {};\n"
3358                "void f() { f(); }\n"
3359                "}",
3360                LLVMWithNoNamespaceFix);
3361   verifyFormat("namespace N::inline D {\n"
3362                "class A {};\n"
3363                "void f() { f(); }\n"
3364                "}",
3365                LLVMWithNoNamespaceFix);
3366   verifyFormat("namespace N::inline D::E {\n"
3367                "class A {};\n"
3368                "void f() { f(); }\n"
3369                "}",
3370                LLVMWithNoNamespaceFix);
3371   verifyFormat("namespace [[deprecated(\"foo[bar\")]] some_namespace {\n"
3372                "class A {};\n"
3373                "void f() { f(); }\n"
3374                "}",
3375                LLVMWithNoNamespaceFix);
3376   verifyFormat("/* something */ namespace some_namespace {\n"
3377                "class A {};\n"
3378                "void f() { f(); }\n"
3379                "}",
3380                LLVMWithNoNamespaceFix);
3381   verifyFormat("namespace {\n"
3382                "class A {};\n"
3383                "void f() { f(); }\n"
3384                "}",
3385                LLVMWithNoNamespaceFix);
3386   verifyFormat("/* something */ namespace {\n"
3387                "class A {};\n"
3388                "void f() { f(); }\n"
3389                "}",
3390                LLVMWithNoNamespaceFix);
3391   verifyFormat("inline namespace X {\n"
3392                "class A {};\n"
3393                "void f() { f(); }\n"
3394                "}",
3395                LLVMWithNoNamespaceFix);
3396   verifyFormat("/* something */ inline namespace X {\n"
3397                "class A {};\n"
3398                "void f() { f(); }\n"
3399                "}",
3400                LLVMWithNoNamespaceFix);
3401   verifyFormat("export namespace X {\n"
3402                "class A {};\n"
3403                "void f() { f(); }\n"
3404                "}",
3405                LLVMWithNoNamespaceFix);
3406   verifyFormat("using namespace some_namespace;\n"
3407                "class A {};\n"
3408                "void f() { f(); }",
3409                LLVMWithNoNamespaceFix);
3410 
3411   // This code is more common than we thought; if we
3412   // layout this correctly the semicolon will go into
3413   // its own line, which is undesirable.
3414   verifyFormat("namespace {};", LLVMWithNoNamespaceFix);
3415   verifyFormat("namespace {\n"
3416                "class A {};\n"
3417                "};",
3418                LLVMWithNoNamespaceFix);
3419 
3420   verifyFormat("namespace {\n"
3421                "int SomeVariable = 0; // comment\n"
3422                "} // namespace",
3423                LLVMWithNoNamespaceFix);
3424   EXPECT_EQ("#ifndef HEADER_GUARD\n"
3425             "#define HEADER_GUARD\n"
3426             "namespace my_namespace {\n"
3427             "int i;\n"
3428             "} // my_namespace\n"
3429             "#endif // HEADER_GUARD",
3430             format("#ifndef HEADER_GUARD\n"
3431                    " #define HEADER_GUARD\n"
3432                    "   namespace my_namespace {\n"
3433                    "int i;\n"
3434                    "}    // my_namespace\n"
3435                    "#endif    // HEADER_GUARD",
3436                    LLVMWithNoNamespaceFix));
3437 
3438   EXPECT_EQ("namespace A::B {\n"
3439             "class C {};\n"
3440             "}",
3441             format("namespace A::B {\n"
3442                    "class C {};\n"
3443                    "}",
3444                    LLVMWithNoNamespaceFix));
3445 
3446   FormatStyle Style = getLLVMStyle();
3447   Style.NamespaceIndentation = FormatStyle::NI_All;
3448   EXPECT_EQ("namespace out {\n"
3449             "  int i;\n"
3450             "  namespace in {\n"
3451             "    int i;\n"
3452             "  } // namespace in\n"
3453             "} // namespace out",
3454             format("namespace out {\n"
3455                    "int i;\n"
3456                    "namespace in {\n"
3457                    "int i;\n"
3458                    "} // namespace in\n"
3459                    "} // namespace out",
3460                    Style));
3461 
3462   Style.NamespaceIndentation = FormatStyle::NI_Inner;
3463   EXPECT_EQ("namespace out {\n"
3464             "int i;\n"
3465             "namespace in {\n"
3466             "  int i;\n"
3467             "} // namespace in\n"
3468             "} // namespace out",
3469             format("namespace out {\n"
3470                    "int i;\n"
3471                    "namespace in {\n"
3472                    "int i;\n"
3473                    "} // namespace in\n"
3474                    "} // namespace out",
3475                    Style));
3476 }
3477 
3478 TEST_F(FormatTest, NamespaceMacros) {
3479   FormatStyle Style = getLLVMStyle();
3480   Style.NamespaceMacros.push_back("TESTSUITE");
3481 
3482   verifyFormat("TESTSUITE(A) {\n"
3483                "int foo();\n"
3484                "} // TESTSUITE(A)",
3485                Style);
3486 
3487   verifyFormat("TESTSUITE(A, B) {\n"
3488                "int foo();\n"
3489                "} // TESTSUITE(A)",
3490                Style);
3491 
3492   // Properly indent according to NamespaceIndentation style
3493   Style.NamespaceIndentation = FormatStyle::NI_All;
3494   verifyFormat("TESTSUITE(A) {\n"
3495                "  int foo();\n"
3496                "} // TESTSUITE(A)",
3497                Style);
3498   verifyFormat("TESTSUITE(A) {\n"
3499                "  namespace B {\n"
3500                "    int foo();\n"
3501                "  } // namespace B\n"
3502                "} // TESTSUITE(A)",
3503                Style);
3504   verifyFormat("namespace A {\n"
3505                "  TESTSUITE(B) {\n"
3506                "    int foo();\n"
3507                "  } // TESTSUITE(B)\n"
3508                "} // namespace A",
3509                Style);
3510 
3511   Style.NamespaceIndentation = FormatStyle::NI_Inner;
3512   verifyFormat("TESTSUITE(A) {\n"
3513                "TESTSUITE(B) {\n"
3514                "  int foo();\n"
3515                "} // TESTSUITE(B)\n"
3516                "} // TESTSUITE(A)",
3517                Style);
3518   verifyFormat("TESTSUITE(A) {\n"
3519                "namespace B {\n"
3520                "  int foo();\n"
3521                "} // namespace B\n"
3522                "} // TESTSUITE(A)",
3523                Style);
3524   verifyFormat("namespace A {\n"
3525                "TESTSUITE(B) {\n"
3526                "  int foo();\n"
3527                "} // TESTSUITE(B)\n"
3528                "} // namespace A",
3529                Style);
3530 
3531   // Properly merge namespace-macros blocks in CompactNamespaces mode
3532   Style.NamespaceIndentation = FormatStyle::NI_None;
3533   Style.CompactNamespaces = true;
3534   verifyFormat("TESTSUITE(A) { TESTSUITE(B) {\n"
3535                "}} // TESTSUITE(A::B)",
3536                Style);
3537 
3538   EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n"
3539             "}} // TESTSUITE(out::in)",
3540             format("TESTSUITE(out) {\n"
3541                    "TESTSUITE(in) {\n"
3542                    "} // TESTSUITE(in)\n"
3543                    "} // TESTSUITE(out)",
3544                    Style));
3545 
3546   EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n"
3547             "}} // TESTSUITE(out::in)",
3548             format("TESTSUITE(out) {\n"
3549                    "TESTSUITE(in) {\n"
3550                    "} // TESTSUITE(in)\n"
3551                    "} // TESTSUITE(out)",
3552                    Style));
3553 
3554   // Do not merge different namespaces/macros
3555   EXPECT_EQ("namespace out {\n"
3556             "TESTSUITE(in) {\n"
3557             "} // TESTSUITE(in)\n"
3558             "} // namespace out",
3559             format("namespace out {\n"
3560                    "TESTSUITE(in) {\n"
3561                    "} // TESTSUITE(in)\n"
3562                    "} // namespace out",
3563                    Style));
3564   EXPECT_EQ("TESTSUITE(out) {\n"
3565             "namespace in {\n"
3566             "} // namespace in\n"
3567             "} // TESTSUITE(out)",
3568             format("TESTSUITE(out) {\n"
3569                    "namespace in {\n"
3570                    "} // namespace in\n"
3571                    "} // TESTSUITE(out)",
3572                    Style));
3573   Style.NamespaceMacros.push_back("FOOBAR");
3574   EXPECT_EQ("TESTSUITE(out) {\n"
3575             "FOOBAR(in) {\n"
3576             "} // FOOBAR(in)\n"
3577             "} // TESTSUITE(out)",
3578             format("TESTSUITE(out) {\n"
3579                    "FOOBAR(in) {\n"
3580                    "} // FOOBAR(in)\n"
3581                    "} // TESTSUITE(out)",
3582                    Style));
3583 }
3584 
3585 TEST_F(FormatTest, FormatsCompactNamespaces) {
3586   FormatStyle Style = getLLVMStyle();
3587   Style.CompactNamespaces = true;
3588   Style.NamespaceMacros.push_back("TESTSUITE");
3589 
3590   verifyFormat("namespace A { namespace B {\n"
3591                "}} // namespace A::B",
3592                Style);
3593 
3594   EXPECT_EQ("namespace out { namespace in {\n"
3595             "}} // namespace out::in",
3596             format("namespace out {\n"
3597                    "namespace in {\n"
3598                    "} // namespace in\n"
3599                    "} // namespace out",
3600                    Style));
3601 
3602   // Only namespaces which have both consecutive opening and end get compacted
3603   EXPECT_EQ("namespace out {\n"
3604             "namespace in1 {\n"
3605             "} // namespace in1\n"
3606             "namespace in2 {\n"
3607             "} // namespace in2\n"
3608             "} // namespace out",
3609             format("namespace out {\n"
3610                    "namespace in1 {\n"
3611                    "} // namespace in1\n"
3612                    "namespace in2 {\n"
3613                    "} // namespace in2\n"
3614                    "} // namespace out",
3615                    Style));
3616 
3617   EXPECT_EQ("namespace out {\n"
3618             "int i;\n"
3619             "namespace in {\n"
3620             "int j;\n"
3621             "} // namespace in\n"
3622             "int k;\n"
3623             "} // namespace out",
3624             format("namespace out { int i;\n"
3625                    "namespace in { int j; } // namespace in\n"
3626                    "int k; } // namespace out",
3627                    Style));
3628 
3629   EXPECT_EQ("namespace A { namespace B { namespace C {\n"
3630             "}}} // namespace A::B::C\n",
3631             format("namespace A { namespace B {\n"
3632                    "namespace C {\n"
3633                    "}} // namespace B::C\n"
3634                    "} // namespace A\n",
3635                    Style));
3636 
3637   Style.ColumnLimit = 40;
3638   EXPECT_EQ("namespace aaaaaaaaaa {\n"
3639             "namespace bbbbbbbbbb {\n"
3640             "}} // namespace aaaaaaaaaa::bbbbbbbbbb",
3641             format("namespace aaaaaaaaaa {\n"
3642                    "namespace bbbbbbbbbb {\n"
3643                    "} // namespace bbbbbbbbbb\n"
3644                    "} // namespace aaaaaaaaaa",
3645                    Style));
3646 
3647   EXPECT_EQ("namespace aaaaaa { namespace bbbbbb {\n"
3648             "namespace cccccc {\n"
3649             "}}} // namespace aaaaaa::bbbbbb::cccccc",
3650             format("namespace aaaaaa {\n"
3651                    "namespace bbbbbb {\n"
3652                    "namespace cccccc {\n"
3653                    "} // namespace cccccc\n"
3654                    "} // namespace bbbbbb\n"
3655                    "} // namespace aaaaaa",
3656                    Style));
3657   Style.ColumnLimit = 80;
3658 
3659   // Extra semicolon after 'inner' closing brace prevents merging
3660   EXPECT_EQ("namespace out { namespace in {\n"
3661             "}; } // namespace out::in",
3662             format("namespace out {\n"
3663                    "namespace in {\n"
3664                    "}; // namespace in\n"
3665                    "} // namespace out",
3666                    Style));
3667 
3668   // Extra semicolon after 'outer' closing brace is conserved
3669   EXPECT_EQ("namespace out { namespace in {\n"
3670             "}}; // namespace out::in",
3671             format("namespace out {\n"
3672                    "namespace in {\n"
3673                    "} // namespace in\n"
3674                    "}; // namespace out",
3675                    Style));
3676 
3677   Style.NamespaceIndentation = FormatStyle::NI_All;
3678   EXPECT_EQ("namespace out { namespace in {\n"
3679             "  int i;\n"
3680             "}} // namespace out::in",
3681             format("namespace out {\n"
3682                    "namespace in {\n"
3683                    "int i;\n"
3684                    "} // namespace in\n"
3685                    "} // namespace out",
3686                    Style));
3687   EXPECT_EQ("namespace out { namespace mid {\n"
3688             "  namespace in {\n"
3689             "    int j;\n"
3690             "  } // namespace in\n"
3691             "  int k;\n"
3692             "}} // namespace out::mid",
3693             format("namespace out { namespace mid {\n"
3694                    "namespace in { int j; } // namespace in\n"
3695                    "int k; }} // namespace out::mid",
3696                    Style));
3697 
3698   Style.NamespaceIndentation = FormatStyle::NI_Inner;
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 { namespace in {\n"
3709             "  int i;\n"
3710             "}}} // namespace out::mid::in",
3711             format("namespace out {\n"
3712                    "namespace mid {\n"
3713                    "namespace in {\n"
3714                    "int i;\n"
3715                    "} // namespace in\n"
3716                    "} // namespace mid\n"
3717                    "} // namespace out",
3718                    Style));
3719 }
3720 
3721 TEST_F(FormatTest, FormatsExternC) {
3722   verifyFormat("extern \"C\" {\nint a;");
3723   verifyFormat("extern \"C\" {}");
3724   verifyFormat("extern \"C\" {\n"
3725                "int foo();\n"
3726                "}");
3727   verifyFormat("extern \"C\" int foo() {}");
3728   verifyFormat("extern \"C\" int foo();");
3729   verifyFormat("extern \"C\" int foo() {\n"
3730                "  int i = 42;\n"
3731                "  return i;\n"
3732                "}");
3733 
3734   FormatStyle Style = getLLVMStyle();
3735   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
3736   Style.BraceWrapping.AfterFunction = true;
3737   verifyFormat("extern \"C\" int foo() {}", Style);
3738   verifyFormat("extern \"C\" int foo();", Style);
3739   verifyFormat("extern \"C\" int foo()\n"
3740                "{\n"
3741                "  int i = 42;\n"
3742                "  return i;\n"
3743                "}",
3744                Style);
3745 
3746   Style.BraceWrapping.AfterExternBlock = true;
3747   Style.BraceWrapping.SplitEmptyRecord = false;
3748   verifyFormat("extern \"C\"\n"
3749                "{}",
3750                Style);
3751   verifyFormat("extern \"C\"\n"
3752                "{\n"
3753                "  int foo();\n"
3754                "}",
3755                Style);
3756 }
3757 
3758 TEST_F(FormatTest, IndentExternBlockStyle) {
3759   FormatStyle Style = getLLVMStyle();
3760   Style.IndentWidth = 2;
3761 
3762   Style.IndentExternBlock = FormatStyle::IEBS_Indent;
3763   verifyFormat("extern \"C\" { /*9*/\n}", Style);
3764   verifyFormat("extern \"C\" {\n"
3765                "  int foo10();\n"
3766                "}",
3767                Style);
3768 
3769   Style.IndentExternBlock = FormatStyle::IEBS_NoIndent;
3770   verifyFormat("extern \"C\" { /*11*/\n}", Style);
3771   verifyFormat("extern \"C\" {\n"
3772                "int foo12();\n"
3773                "}",
3774                Style);
3775 
3776   Style.IndentExternBlock = FormatStyle::IEBS_AfterExternBlock;
3777   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
3778   Style.BraceWrapping.AfterExternBlock = true;
3779   verifyFormat("extern \"C\"\n{ /*13*/\n}", Style);
3780   verifyFormat("extern \"C\"\n{\n"
3781                "  int foo14();\n"
3782                "}",
3783                Style);
3784 
3785   Style.IndentExternBlock = FormatStyle::IEBS_AfterExternBlock;
3786   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
3787   Style.BraceWrapping.AfterExternBlock = false;
3788   verifyFormat("extern \"C\" { /*15*/\n}", Style);
3789   verifyFormat("extern \"C\" {\n"
3790                "int foo16();\n"
3791                "}",
3792                Style);
3793 }
3794 
3795 TEST_F(FormatTest, FormatsInlineASM) {
3796   verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
3797   verifyFormat("asm(\"nop\" ::: \"memory\");");
3798   verifyFormat(
3799       "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
3800       "    \"cpuid\\n\\t\"\n"
3801       "    \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
3802       "    : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
3803       "    : \"a\"(value));");
3804   EXPECT_EQ(
3805       "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
3806       "  __asm {\n"
3807       "        mov     edx,[that] // vtable in edx\n"
3808       "        mov     eax,methodIndex\n"
3809       "        call    [edx][eax*4] // stdcall\n"
3810       "  }\n"
3811       "}",
3812       format("void NS_InvokeByIndex(void *that,   unsigned int methodIndex) {\n"
3813              "    __asm {\n"
3814              "        mov     edx,[that] // vtable in edx\n"
3815              "        mov     eax,methodIndex\n"
3816              "        call    [edx][eax*4] // stdcall\n"
3817              "    }\n"
3818              "}"));
3819   EXPECT_EQ("_asm {\n"
3820             "  xor eax, eax;\n"
3821             "  cpuid;\n"
3822             "}",
3823             format("_asm {\n"
3824                    "  xor eax, eax;\n"
3825                    "  cpuid;\n"
3826                    "}"));
3827   verifyFormat("void function() {\n"
3828                "  // comment\n"
3829                "  asm(\"\");\n"
3830                "}");
3831   EXPECT_EQ("__asm {\n"
3832             "}\n"
3833             "int i;",
3834             format("__asm   {\n"
3835                    "}\n"
3836                    "int   i;"));
3837 }
3838 
3839 TEST_F(FormatTest, FormatTryCatch) {
3840   verifyFormat("try {\n"
3841                "  throw a * b;\n"
3842                "} catch (int a) {\n"
3843                "  // Do nothing.\n"
3844                "} catch (...) {\n"
3845                "  exit(42);\n"
3846                "}");
3847 
3848   // Function-level try statements.
3849   verifyFormat("int f() try { return 4; } catch (...) {\n"
3850                "  return 5;\n"
3851                "}");
3852   verifyFormat("class A {\n"
3853                "  int a;\n"
3854                "  A() try : a(0) {\n"
3855                "  } catch (...) {\n"
3856                "    throw;\n"
3857                "  }\n"
3858                "};\n");
3859   verifyFormat("class A {\n"
3860                "  int a;\n"
3861                "  A() try : a(0), b{1} {\n"
3862                "  } catch (...) {\n"
3863                "    throw;\n"
3864                "  }\n"
3865                "};\n");
3866   verifyFormat("class A {\n"
3867                "  int a;\n"
3868                "  A() try : a(0), b{1}, c{2} {\n"
3869                "  } catch (...) {\n"
3870                "    throw;\n"
3871                "  }\n"
3872                "};\n");
3873   verifyFormat("class A {\n"
3874                "  int a;\n"
3875                "  A() try : a(0), b{1}, c{2} {\n"
3876                "    { // New scope.\n"
3877                "    }\n"
3878                "  } catch (...) {\n"
3879                "    throw;\n"
3880                "  }\n"
3881                "};\n");
3882 
3883   // Incomplete try-catch blocks.
3884   verifyIncompleteFormat("try {} catch (");
3885 }
3886 
3887 TEST_F(FormatTest, FormatTryAsAVariable) {
3888   verifyFormat("int try;");
3889   verifyFormat("int try, size;");
3890   verifyFormat("try = foo();");
3891   verifyFormat("if (try < size) {\n  return true;\n}");
3892 
3893   verifyFormat("int catch;");
3894   verifyFormat("int catch, size;");
3895   verifyFormat("catch = foo();");
3896   verifyFormat("if (catch < size) {\n  return true;\n}");
3897 
3898   FormatStyle Style = getLLVMStyle();
3899   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
3900   Style.BraceWrapping.AfterFunction = true;
3901   Style.BraceWrapping.BeforeCatch = true;
3902   verifyFormat("try {\n"
3903                "  int bar = 1;\n"
3904                "}\n"
3905                "catch (...) {\n"
3906                "  int bar = 1;\n"
3907                "}",
3908                Style);
3909   verifyFormat("#if NO_EX\n"
3910                "try\n"
3911                "#endif\n"
3912                "{\n"
3913                "}\n"
3914                "#if NO_EX\n"
3915                "catch (...) {\n"
3916                "}",
3917                Style);
3918   verifyFormat("try /* abc */ {\n"
3919                "  int bar = 1;\n"
3920                "}\n"
3921                "catch (...) {\n"
3922                "  int bar = 1;\n"
3923                "}",
3924                Style);
3925   verifyFormat("try\n"
3926                "// abc\n"
3927                "{\n"
3928                "  int bar = 1;\n"
3929                "}\n"
3930                "catch (...) {\n"
3931                "  int bar = 1;\n"
3932                "}",
3933                Style);
3934 }
3935 
3936 TEST_F(FormatTest, FormatSEHTryCatch) {
3937   verifyFormat("__try {\n"
3938                "  int a = b * c;\n"
3939                "} __except (EXCEPTION_EXECUTE_HANDLER) {\n"
3940                "  // Do nothing.\n"
3941                "}");
3942 
3943   verifyFormat("__try {\n"
3944                "  int a = b * c;\n"
3945                "} __finally {\n"
3946                "  // Do nothing.\n"
3947                "}");
3948 
3949   verifyFormat("DEBUG({\n"
3950                "  __try {\n"
3951                "  } __finally {\n"
3952                "  }\n"
3953                "});\n");
3954 }
3955 
3956 TEST_F(FormatTest, IncompleteTryCatchBlocks) {
3957   verifyFormat("try {\n"
3958                "  f();\n"
3959                "} catch {\n"
3960                "  g();\n"
3961                "}");
3962   verifyFormat("try {\n"
3963                "  f();\n"
3964                "} catch (A a) MACRO(x) {\n"
3965                "  g();\n"
3966                "} catch (B b) MACRO(x) {\n"
3967                "  g();\n"
3968                "}");
3969 }
3970 
3971 TEST_F(FormatTest, FormatTryCatchBraceStyles) {
3972   FormatStyle Style = getLLVMStyle();
3973   for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla,
3974                           FormatStyle::BS_WebKit}) {
3975     Style.BreakBeforeBraces = BraceStyle;
3976     verifyFormat("try {\n"
3977                  "  // something\n"
3978                  "} catch (...) {\n"
3979                  "  // something\n"
3980                  "}",
3981                  Style);
3982   }
3983   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
3984   verifyFormat("try {\n"
3985                "  // something\n"
3986                "}\n"
3987                "catch (...) {\n"
3988                "  // something\n"
3989                "}",
3990                Style);
3991   verifyFormat("__try {\n"
3992                "  // something\n"
3993                "}\n"
3994                "__finally {\n"
3995                "  // something\n"
3996                "}",
3997                Style);
3998   verifyFormat("@try {\n"
3999                "  // something\n"
4000                "}\n"
4001                "@finally {\n"
4002                "  // something\n"
4003                "}",
4004                Style);
4005   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
4006   verifyFormat("try\n"
4007                "{\n"
4008                "  // something\n"
4009                "}\n"
4010                "catch (...)\n"
4011                "{\n"
4012                "  // something\n"
4013                "}",
4014                Style);
4015   Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
4016   verifyFormat("try\n"
4017                "  {\n"
4018                "  // something white\n"
4019                "  }\n"
4020                "catch (...)\n"
4021                "  {\n"
4022                "  // something white\n"
4023                "  }",
4024                Style);
4025   Style.BreakBeforeBraces = FormatStyle::BS_GNU;
4026   verifyFormat("try\n"
4027                "  {\n"
4028                "    // something\n"
4029                "  }\n"
4030                "catch (...)\n"
4031                "  {\n"
4032                "    // something\n"
4033                "  }",
4034                Style);
4035   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4036   Style.BraceWrapping.BeforeCatch = true;
4037   verifyFormat("try {\n"
4038                "  // something\n"
4039                "}\n"
4040                "catch (...) {\n"
4041                "  // something\n"
4042                "}",
4043                Style);
4044 }
4045 
4046 TEST_F(FormatTest, StaticInitializers) {
4047   verifyFormat("static SomeClass SC = {1, 'a'};");
4048 
4049   verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n"
4050                "    100000000, "
4051                "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};");
4052 
4053   // Here, everything other than the "}" would fit on a line.
4054   verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
4055                "    10000000000000000000000000};");
4056   EXPECT_EQ("S s = {a,\n"
4057             "\n"
4058             "       b};",
4059             format("S s = {\n"
4060                    "  a,\n"
4061                    "\n"
4062                    "  b\n"
4063                    "};"));
4064 
4065   // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
4066   // line. However, the formatting looks a bit off and this probably doesn't
4067   // happen often in practice.
4068   verifyFormat("static int Variable[1] = {\n"
4069                "    {1000000000000000000000000000000000000}};",
4070                getLLVMStyleWithColumns(40));
4071 }
4072 
4073 TEST_F(FormatTest, DesignatedInitializers) {
4074   verifyFormat("const struct A a = {.a = 1, .b = 2};");
4075   verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n"
4076                "                    .bbbbbbbbbb = 2,\n"
4077                "                    .cccccccccc = 3,\n"
4078                "                    .dddddddddd = 4,\n"
4079                "                    .eeeeeeeeee = 5};");
4080   verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
4081                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
4082                "    .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
4083                "    .ccccccccccccccccccccccccccc = 3,\n"
4084                "    .ddddddddddddddddddddddddddd = 4,\n"
4085                "    .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};");
4086 
4087   verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
4088 
4089   verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
4090   verifyFormat("const struct A a = {[1] = aaaaaaaaaa,\n"
4091                "                    [2] = bbbbbbbbbb,\n"
4092                "                    [3] = cccccccccc,\n"
4093                "                    [4] = dddddddddd,\n"
4094                "                    [5] = eeeeeeeeee};");
4095   verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
4096                "    [1] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4097                "    [2] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
4098                "    [3] = cccccccccccccccccccccccccccccccccccccc,\n"
4099                "    [4] = dddddddddddddddddddddddddddddddddddddd,\n"
4100                "    [5] = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee};");
4101 }
4102 
4103 TEST_F(FormatTest, NestedStaticInitializers) {
4104   verifyFormat("static A x = {{{}}};\n");
4105   verifyFormat("static A x = {{{init1, init2, init3, init4},\n"
4106                "               {init1, init2, init3, init4}}};",
4107                getLLVMStyleWithColumns(50));
4108 
4109   verifyFormat("somes Status::global_reps[3] = {\n"
4110                "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
4111                "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
4112                "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};",
4113                getLLVMStyleWithColumns(60));
4114   verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
4115                      "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
4116                      "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
4117                      "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
4118   verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n"
4119                "                  {rect.fRight - rect.fLeft, rect.fBottom - "
4120                "rect.fTop}};");
4121 
4122   verifyFormat(
4123       "SomeArrayOfSomeType a = {\n"
4124       "    {{1, 2, 3},\n"
4125       "     {1, 2, 3},\n"
4126       "     {111111111111111111111111111111, 222222222222222222222222222222,\n"
4127       "      333333333333333333333333333333},\n"
4128       "     {1, 2, 3},\n"
4129       "     {1, 2, 3}}};");
4130   verifyFormat(
4131       "SomeArrayOfSomeType a = {\n"
4132       "    {{1, 2, 3}},\n"
4133       "    {{1, 2, 3}},\n"
4134       "    {{111111111111111111111111111111, 222222222222222222222222222222,\n"
4135       "      333333333333333333333333333333}},\n"
4136       "    {{1, 2, 3}},\n"
4137       "    {{1, 2, 3}}};");
4138 
4139   verifyFormat("struct {\n"
4140                "  unsigned bit;\n"
4141                "  const char *const name;\n"
4142                "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n"
4143                "                 {kOsWin, \"Windows\"},\n"
4144                "                 {kOsLinux, \"Linux\"},\n"
4145                "                 {kOsCrOS, \"Chrome OS\"}};");
4146   verifyFormat("struct {\n"
4147                "  unsigned bit;\n"
4148                "  const char *const name;\n"
4149                "} kBitsToOs[] = {\n"
4150                "    {kOsMac, \"Mac\"},\n"
4151                "    {kOsWin, \"Windows\"},\n"
4152                "    {kOsLinux, \"Linux\"},\n"
4153                "    {kOsCrOS, \"Chrome OS\"},\n"
4154                "};");
4155 }
4156 
4157 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
4158   verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
4159                "                      \\\n"
4160                "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
4161 }
4162 
4163 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
4164   verifyFormat("virtual void write(ELFWriter *writerrr,\n"
4165                "                   OwningPtr<FileOutputBuffer> &buffer) = 0;");
4166 
4167   // Do break defaulted and deleted functions.
4168   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
4169                "    default;",
4170                getLLVMStyleWithColumns(40));
4171   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
4172                "    delete;",
4173                getLLVMStyleWithColumns(40));
4174 }
4175 
4176 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) {
4177   verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3",
4178                getLLVMStyleWithColumns(40));
4179   verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
4180                getLLVMStyleWithColumns(40));
4181   EXPECT_EQ("#define Q                              \\\n"
4182             "  \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\"    \\\n"
4183             "  \"aaaaaaaa.cpp\"",
4184             format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
4185                    getLLVMStyleWithColumns(40)));
4186 }
4187 
4188 TEST_F(FormatTest, UnderstandsLinePPDirective) {
4189   EXPECT_EQ("# 123 \"A string literal\"",
4190             format("   #     123    \"A string literal\""));
4191 }
4192 
4193 TEST_F(FormatTest, LayoutUnknownPPDirective) {
4194   EXPECT_EQ("#;", format("#;"));
4195   verifyFormat("#\n;\n;\n;");
4196 }
4197 
4198 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
4199   EXPECT_EQ("#line 42 \"test\"\n",
4200             format("#  \\\n  line  \\\n  42  \\\n  \"test\"\n"));
4201   EXPECT_EQ("#define A B\n", format("#  \\\n define  \\\n    A  \\\n       B\n",
4202                                     getLLVMStyleWithColumns(12)));
4203 }
4204 
4205 TEST_F(FormatTest, EndOfFileEndsPPDirective) {
4206   EXPECT_EQ("#line 42 \"test\"",
4207             format("#  \\\n  line  \\\n  42  \\\n  \"test\""));
4208   EXPECT_EQ("#define A B", format("#  \\\n define  \\\n    A  \\\n       B"));
4209 }
4210 
4211 TEST_F(FormatTest, DoesntRemoveUnknownTokens) {
4212   verifyFormat("#define A \\x20");
4213   verifyFormat("#define A \\ x20");
4214   EXPECT_EQ("#define A \\ x20", format("#define A \\   x20"));
4215   verifyFormat("#define A ''");
4216   verifyFormat("#define A ''qqq");
4217   verifyFormat("#define A `qqq");
4218   verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");");
4219   EXPECT_EQ("const char *c = STRINGIFY(\n"
4220             "\\na : b);",
4221             format("const char * c = STRINGIFY(\n"
4222                    "\\na : b);"));
4223 
4224   verifyFormat("a\r\\");
4225   verifyFormat("a\v\\");
4226   verifyFormat("a\f\\");
4227 }
4228 
4229 TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) {
4230   FormatStyle style = getChromiumStyle(FormatStyle::LK_Cpp);
4231   style.IndentWidth = 4;
4232   style.PPIndentWidth = 1;
4233 
4234   style.IndentPPDirectives = FormatStyle::PPDIS_None;
4235   verifyFormat("#ifdef __linux__\n"
4236                "void foo() {\n"
4237                "    int x = 0;\n"
4238                "}\n"
4239                "#define FOO\n"
4240                "#endif\n"
4241                "void bar() {\n"
4242                "    int y = 0;\n"
4243                "}\n",
4244                style);
4245 
4246   style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
4247   verifyFormat("#ifdef __linux__\n"
4248                "void foo() {\n"
4249                "    int x = 0;\n"
4250                "}\n"
4251                "# define FOO foo\n"
4252                "#endif\n"
4253                "void bar() {\n"
4254                "    int y = 0;\n"
4255                "}\n",
4256                style);
4257 
4258   style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash;
4259   verifyFormat("#ifdef __linux__\n"
4260                "void foo() {\n"
4261                "    int x = 0;\n"
4262                "}\n"
4263                " #define FOO foo\n"
4264                "#endif\n"
4265                "void bar() {\n"
4266                "    int y = 0;\n"
4267                "}\n",
4268                style);
4269 }
4270 
4271 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
4272   verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
4273   verifyFormat("#define A( \\\n    BB)", getLLVMStyleWithColumns(12));
4274   verifyFormat("#define A( \\\n    A, B)", getLLVMStyleWithColumns(12));
4275   // FIXME: We never break before the macro name.
4276   verifyFormat("#define AA( \\\n    B)", getLLVMStyleWithColumns(12));
4277 
4278   verifyFormat("#define A A\n#define A A");
4279   verifyFormat("#define A(X) A\n#define A A");
4280 
4281   verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
4282   verifyFormat("#define Something    \\\n  Other", getLLVMStyleWithColumns(22));
4283 }
4284 
4285 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
4286   EXPECT_EQ("// somecomment\n"
4287             "#include \"a.h\"\n"
4288             "#define A(  \\\n"
4289             "    A, B)\n"
4290             "#include \"b.h\"\n"
4291             "// somecomment\n",
4292             format("  // somecomment\n"
4293                    "  #include \"a.h\"\n"
4294                    "#define A(A,\\\n"
4295                    "    B)\n"
4296                    "    #include \"b.h\"\n"
4297                    " // somecomment\n",
4298                    getLLVMStyleWithColumns(13)));
4299 }
4300 
4301 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); }
4302 
4303 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
4304   EXPECT_EQ("#define A    \\\n"
4305             "  c;         \\\n"
4306             "  e;\n"
4307             "f;",
4308             format("#define A c; e;\n"
4309                    "f;",
4310                    getLLVMStyleWithColumns(14)));
4311 }
4312 
4313 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
4314 
4315 TEST_F(FormatTest, MacroDefinitionInsideStatement) {
4316   EXPECT_EQ("int x,\n"
4317             "#define A\n"
4318             "    y;",
4319             format("int x,\n#define A\ny;"));
4320 }
4321 
4322 TEST_F(FormatTest, HashInMacroDefinition) {
4323   EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle()));
4324   verifyFormat("#define A \\\n  b #c;", getLLVMStyleWithColumns(11));
4325   verifyFormat("#define A  \\\n"
4326                "  {        \\\n"
4327                "    f(#c); \\\n"
4328                "  }",
4329                getLLVMStyleWithColumns(11));
4330 
4331   verifyFormat("#define A(X)         \\\n"
4332                "  void function##X()",
4333                getLLVMStyleWithColumns(22));
4334 
4335   verifyFormat("#define A(a, b, c)   \\\n"
4336                "  void a##b##c()",
4337                getLLVMStyleWithColumns(22));
4338 
4339   verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
4340 }
4341 
4342 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
4343   EXPECT_EQ("#define A (x)", format("#define A (x)"));
4344   EXPECT_EQ("#define A(x)", format("#define A(x)"));
4345 
4346   FormatStyle Style = getLLVMStyle();
4347   Style.SpaceBeforeParens = FormatStyle::SBPO_Never;
4348   verifyFormat("#define true ((foo)1)", Style);
4349   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
4350   verifyFormat("#define false((foo)0)", Style);
4351 }
4352 
4353 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
4354   EXPECT_EQ("#define A b;", format("#define A \\\n"
4355                                    "          \\\n"
4356                                    "  b;",
4357                                    getLLVMStyleWithColumns(25)));
4358   EXPECT_EQ("#define A \\\n"
4359             "          \\\n"
4360             "  a;      \\\n"
4361             "  b;",
4362             format("#define A \\\n"
4363                    "          \\\n"
4364                    "  a;      \\\n"
4365                    "  b;",
4366                    getLLVMStyleWithColumns(11)));
4367   EXPECT_EQ("#define A \\\n"
4368             "  a;      \\\n"
4369             "          \\\n"
4370             "  b;",
4371             format("#define A \\\n"
4372                    "  a;      \\\n"
4373                    "          \\\n"
4374                    "  b;",
4375                    getLLVMStyleWithColumns(11)));
4376 }
4377 
4378 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
4379   verifyIncompleteFormat("#define A :");
4380   verifyFormat("#define SOMECASES  \\\n"
4381                "  case 1:          \\\n"
4382                "  case 2\n",
4383                getLLVMStyleWithColumns(20));
4384   verifyFormat("#define MACRO(a) \\\n"
4385                "  if (a)         \\\n"
4386                "    f();         \\\n"
4387                "  else           \\\n"
4388                "    g()",
4389                getLLVMStyleWithColumns(18));
4390   verifyFormat("#define A template <typename T>");
4391   verifyIncompleteFormat("#define STR(x) #x\n"
4392                          "f(STR(this_is_a_string_literal{));");
4393   verifyFormat("#pragma omp threadprivate( \\\n"
4394                "    y)), // expected-warning",
4395                getLLVMStyleWithColumns(28));
4396   verifyFormat("#d, = };");
4397   verifyFormat("#if \"a");
4398   verifyIncompleteFormat("({\n"
4399                          "#define b     \\\n"
4400                          "  }           \\\n"
4401                          "  a\n"
4402                          "a",
4403                          getLLVMStyleWithColumns(15));
4404   verifyFormat("#define A     \\\n"
4405                "  {           \\\n"
4406                "    {\n"
4407                "#define B     \\\n"
4408                "  }           \\\n"
4409                "  }",
4410                getLLVMStyleWithColumns(15));
4411   verifyNoCrash("#if a\na(\n#else\n#endif\n{a");
4412   verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}");
4413   verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};");
4414   verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() {      \n)}");
4415 }
4416 
4417 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
4418   verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
4419   EXPECT_EQ("class A : public QObject {\n"
4420             "  Q_OBJECT\n"
4421             "\n"
4422             "  A() {}\n"
4423             "};",
4424             format("class A  :  public QObject {\n"
4425                    "     Q_OBJECT\n"
4426                    "\n"
4427                    "  A() {\n}\n"
4428                    "}  ;"));
4429   EXPECT_EQ("MACRO\n"
4430             "/*static*/ int i;",
4431             format("MACRO\n"
4432                    " /*static*/ int   i;"));
4433   EXPECT_EQ("SOME_MACRO\n"
4434             "namespace {\n"
4435             "void f();\n"
4436             "} // namespace",
4437             format("SOME_MACRO\n"
4438                    "  namespace    {\n"
4439                    "void   f(  );\n"
4440                    "} // namespace"));
4441   // Only if the identifier contains at least 5 characters.
4442   EXPECT_EQ("HTTP f();", format("HTTP\nf();"));
4443   EXPECT_EQ("MACRO\nf();", format("MACRO\nf();"));
4444   // Only if everything is upper case.
4445   EXPECT_EQ("class A : public QObject {\n"
4446             "  Q_Object A() {}\n"
4447             "};",
4448             format("class A  :  public QObject {\n"
4449                    "     Q_Object\n"
4450                    "  A() {\n}\n"
4451                    "}  ;"));
4452 
4453   // Only if the next line can actually start an unwrapped line.
4454   EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;",
4455             format("SOME_WEIRD_LOG_MACRO\n"
4456                    "<< SomeThing;"));
4457 
4458   verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
4459                "(n, buffers))\n",
4460                getChromiumStyle(FormatStyle::LK_Cpp));
4461 
4462   // See PR41483
4463   EXPECT_EQ("/**/ FOO(a)\n"
4464             "FOO(b)",
4465             format("/**/ FOO(a)\n"
4466                    "FOO(b)"));
4467 }
4468 
4469 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
4470   EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
4471             "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
4472             "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
4473             "class X {};\n"
4474             "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
4475             "int *createScopDetectionPass() { return 0; }",
4476             format("  INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
4477                    "  INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
4478                    "  INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
4479                    "  class X {};\n"
4480                    "  INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
4481                    "  int *createScopDetectionPass() { return 0; }"));
4482   // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
4483   // braces, so that inner block is indented one level more.
4484   EXPECT_EQ("int q() {\n"
4485             "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
4486             "  IPC_MESSAGE_HANDLER(xxx, qqq)\n"
4487             "  IPC_END_MESSAGE_MAP()\n"
4488             "}",
4489             format("int q() {\n"
4490                    "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
4491                    "    IPC_MESSAGE_HANDLER(xxx, qqq)\n"
4492                    "  IPC_END_MESSAGE_MAP()\n"
4493                    "}"));
4494 
4495   // Same inside macros.
4496   EXPECT_EQ("#define LIST(L) \\\n"
4497             "  L(A)          \\\n"
4498             "  L(B)          \\\n"
4499             "  L(C)",
4500             format("#define LIST(L) \\\n"
4501                    "  L(A) \\\n"
4502                    "  L(B) \\\n"
4503                    "  L(C)",
4504                    getGoogleStyle()));
4505 
4506   // These must not be recognized as macros.
4507   EXPECT_EQ("int q() {\n"
4508             "  f(x);\n"
4509             "  f(x) {}\n"
4510             "  f(x)->g();\n"
4511             "  f(x)->*g();\n"
4512             "  f(x).g();\n"
4513             "  f(x) = x;\n"
4514             "  f(x) += x;\n"
4515             "  f(x) -= x;\n"
4516             "  f(x) *= x;\n"
4517             "  f(x) /= x;\n"
4518             "  f(x) %= x;\n"
4519             "  f(x) &= x;\n"
4520             "  f(x) |= x;\n"
4521             "  f(x) ^= x;\n"
4522             "  f(x) >>= x;\n"
4523             "  f(x) <<= x;\n"
4524             "  f(x)[y].z();\n"
4525             "  LOG(INFO) << x;\n"
4526             "  ifstream(x) >> x;\n"
4527             "}\n",
4528             format("int q() {\n"
4529                    "  f(x)\n;\n"
4530                    "  f(x)\n {}\n"
4531                    "  f(x)\n->g();\n"
4532                    "  f(x)\n->*g();\n"
4533                    "  f(x)\n.g();\n"
4534                    "  f(x)\n = x;\n"
4535                    "  f(x)\n += x;\n"
4536                    "  f(x)\n -= x;\n"
4537                    "  f(x)\n *= x;\n"
4538                    "  f(x)\n /= x;\n"
4539                    "  f(x)\n %= x;\n"
4540                    "  f(x)\n &= x;\n"
4541                    "  f(x)\n |= x;\n"
4542                    "  f(x)\n ^= x;\n"
4543                    "  f(x)\n >>= x;\n"
4544                    "  f(x)\n <<= x;\n"
4545                    "  f(x)\n[y].z();\n"
4546                    "  LOG(INFO)\n << x;\n"
4547                    "  ifstream(x)\n >> x;\n"
4548                    "}\n"));
4549   EXPECT_EQ("int q() {\n"
4550             "  F(x)\n"
4551             "  if (1) {\n"
4552             "  }\n"
4553             "  F(x)\n"
4554             "  while (1) {\n"
4555             "  }\n"
4556             "  F(x)\n"
4557             "  G(x);\n"
4558             "  F(x)\n"
4559             "  try {\n"
4560             "    Q();\n"
4561             "  } catch (...) {\n"
4562             "  }\n"
4563             "}\n",
4564             format("int q() {\n"
4565                    "F(x)\n"
4566                    "if (1) {}\n"
4567                    "F(x)\n"
4568                    "while (1) {}\n"
4569                    "F(x)\n"
4570                    "G(x);\n"
4571                    "F(x)\n"
4572                    "try { Q(); } catch (...) {}\n"
4573                    "}\n"));
4574   EXPECT_EQ("class A {\n"
4575             "  A() : t(0) {}\n"
4576             "  A(int i) noexcept() : {}\n"
4577             "  A(X x)\n" // FIXME: function-level try blocks are broken.
4578             "  try : t(0) {\n"
4579             "  } catch (...) {\n"
4580             "  }\n"
4581             "};",
4582             format("class A {\n"
4583                    "  A()\n : t(0) {}\n"
4584                    "  A(int i)\n noexcept() : {}\n"
4585                    "  A(X x)\n"
4586                    "  try : t(0) {} catch (...) {}\n"
4587                    "};"));
4588   FormatStyle Style = getLLVMStyle();
4589   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4590   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
4591   Style.BraceWrapping.AfterFunction = true;
4592   EXPECT_EQ("void f()\n"
4593             "try\n"
4594             "{\n"
4595             "}",
4596             format("void f() try {\n"
4597                    "}",
4598                    Style));
4599   EXPECT_EQ("class SomeClass {\n"
4600             "public:\n"
4601             "  SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
4602             "};",
4603             format("class SomeClass {\n"
4604                    "public:\n"
4605                    "  SomeClass()\n"
4606                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
4607                    "};"));
4608   EXPECT_EQ("class SomeClass {\n"
4609             "public:\n"
4610             "  SomeClass()\n"
4611             "      EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
4612             "};",
4613             format("class SomeClass {\n"
4614                    "public:\n"
4615                    "  SomeClass()\n"
4616                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
4617                    "};",
4618                    getLLVMStyleWithColumns(40)));
4619 
4620   verifyFormat("MACRO(>)");
4621 
4622   // Some macros contain an implicit semicolon.
4623   Style = getLLVMStyle();
4624   Style.StatementMacros.push_back("FOO");
4625   verifyFormat("FOO(a) int b = 0;");
4626   verifyFormat("FOO(a)\n"
4627                "int b = 0;",
4628                Style);
4629   verifyFormat("FOO(a);\n"
4630                "int b = 0;",
4631                Style);
4632   verifyFormat("FOO(argc, argv, \"4.0.2\")\n"
4633                "int b = 0;",
4634                Style);
4635   verifyFormat("FOO()\n"
4636                "int b = 0;",
4637                Style);
4638   verifyFormat("FOO\n"
4639                "int b = 0;",
4640                Style);
4641   verifyFormat("void f() {\n"
4642                "  FOO(a)\n"
4643                "  return a;\n"
4644                "}",
4645                Style);
4646   verifyFormat("FOO(a)\n"
4647                "FOO(b)",
4648                Style);
4649   verifyFormat("int a = 0;\n"
4650                "FOO(b)\n"
4651                "int c = 0;",
4652                Style);
4653   verifyFormat("int a = 0;\n"
4654                "int x = FOO(a)\n"
4655                "int b = 0;",
4656                Style);
4657   verifyFormat("void foo(int a) { FOO(a) }\n"
4658                "uint32_t bar() {}",
4659                Style);
4660 }
4661 
4662 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
4663   verifyFormat("#define A \\\n"
4664                "  f({     \\\n"
4665                "    g();  \\\n"
4666                "  });",
4667                getLLVMStyleWithColumns(11));
4668 }
4669 
4670 TEST_F(FormatTest, IndentPreprocessorDirectives) {
4671   FormatStyle Style = getLLVMStyle();
4672   Style.IndentPPDirectives = FormatStyle::PPDIS_None;
4673   Style.ColumnLimit = 40;
4674   verifyFormat("#ifdef _WIN32\n"
4675                "#define A 0\n"
4676                "#ifdef VAR2\n"
4677                "#define B 1\n"
4678                "#include <someheader.h>\n"
4679                "#define MACRO                          \\\n"
4680                "  some_very_long_func_aaaaaaaaaa();\n"
4681                "#endif\n"
4682                "#else\n"
4683                "#define A 1\n"
4684                "#endif",
4685                Style);
4686   Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
4687   verifyFormat("#ifdef _WIN32\n"
4688                "#  define A 0\n"
4689                "#  ifdef VAR2\n"
4690                "#    define B 1\n"
4691                "#    include <someheader.h>\n"
4692                "#    define MACRO                      \\\n"
4693                "      some_very_long_func_aaaaaaaaaa();\n"
4694                "#  endif\n"
4695                "#else\n"
4696                "#  define A 1\n"
4697                "#endif",
4698                Style);
4699   verifyFormat("#if A\n"
4700                "#  define MACRO                        \\\n"
4701                "    void a(int x) {                    \\\n"
4702                "      b();                             \\\n"
4703                "      c();                             \\\n"
4704                "      d();                             \\\n"
4705                "      e();                             \\\n"
4706                "      f();                             \\\n"
4707                "    }\n"
4708                "#endif",
4709                Style);
4710   // Comments before include guard.
4711   verifyFormat("// file comment\n"
4712                "// file comment\n"
4713                "#ifndef HEADER_H\n"
4714                "#define HEADER_H\n"
4715                "code();\n"
4716                "#endif",
4717                Style);
4718   // Test with include guards.
4719   verifyFormat("#ifndef HEADER_H\n"
4720                "#define HEADER_H\n"
4721                "code();\n"
4722                "#endif",
4723                Style);
4724   // Include guards must have a #define with the same variable immediately
4725   // after #ifndef.
4726   verifyFormat("#ifndef NOT_GUARD\n"
4727                "#  define FOO\n"
4728                "code();\n"
4729                "#endif",
4730                Style);
4731 
4732   // Include guards must cover the entire file.
4733   verifyFormat("code();\n"
4734                "code();\n"
4735                "#ifndef NOT_GUARD\n"
4736                "#  define NOT_GUARD\n"
4737                "code();\n"
4738                "#endif",
4739                Style);
4740   verifyFormat("#ifndef NOT_GUARD\n"
4741                "#  define NOT_GUARD\n"
4742                "code();\n"
4743                "#endif\n"
4744                "code();",
4745                Style);
4746   // Test with trailing blank lines.
4747   verifyFormat("#ifndef HEADER_H\n"
4748                "#define HEADER_H\n"
4749                "code();\n"
4750                "#endif\n",
4751                Style);
4752   // Include guards don't have #else.
4753   verifyFormat("#ifndef NOT_GUARD\n"
4754                "#  define NOT_GUARD\n"
4755                "code();\n"
4756                "#else\n"
4757                "#endif",
4758                Style);
4759   verifyFormat("#ifndef NOT_GUARD\n"
4760                "#  define NOT_GUARD\n"
4761                "code();\n"
4762                "#elif FOO\n"
4763                "#endif",
4764                Style);
4765   // Non-identifier #define after potential include guard.
4766   verifyFormat("#ifndef FOO\n"
4767                "#  define 1\n"
4768                "#endif\n",
4769                Style);
4770   // #if closes past last non-preprocessor line.
4771   verifyFormat("#ifndef FOO\n"
4772                "#define FOO\n"
4773                "#if 1\n"
4774                "int i;\n"
4775                "#  define A 0\n"
4776                "#endif\n"
4777                "#endif\n",
4778                Style);
4779   // Don't crash if there is an #elif directive without a condition.
4780   verifyFormat("#if 1\n"
4781                "int x;\n"
4782                "#elif\n"
4783                "int y;\n"
4784                "#else\n"
4785                "int z;\n"
4786                "#endif",
4787                Style);
4788   // FIXME: This doesn't handle the case where there's code between the
4789   // #ifndef and #define but all other conditions hold. This is because when
4790   // the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the
4791   // previous code line yet, so we can't detect it.
4792   EXPECT_EQ("#ifndef NOT_GUARD\n"
4793             "code();\n"
4794             "#define NOT_GUARD\n"
4795             "code();\n"
4796             "#endif",
4797             format("#ifndef NOT_GUARD\n"
4798                    "code();\n"
4799                    "#  define NOT_GUARD\n"
4800                    "code();\n"
4801                    "#endif",
4802                    Style));
4803   // FIXME: This doesn't handle cases where legitimate preprocessor lines may
4804   // be outside an include guard. Examples are #pragma once and
4805   // #pragma GCC diagnostic, or anything else that does not change the meaning
4806   // of the file if it's included multiple times.
4807   EXPECT_EQ("#ifdef WIN32\n"
4808             "#  pragma once\n"
4809             "#endif\n"
4810             "#ifndef HEADER_H\n"
4811             "#  define HEADER_H\n"
4812             "code();\n"
4813             "#endif",
4814             format("#ifdef WIN32\n"
4815                    "#  pragma once\n"
4816                    "#endif\n"
4817                    "#ifndef HEADER_H\n"
4818                    "#define HEADER_H\n"
4819                    "code();\n"
4820                    "#endif",
4821                    Style));
4822   // FIXME: This does not detect when there is a single non-preprocessor line
4823   // in front of an include-guard-like structure where other conditions hold
4824   // because ScopedLineState hides the line.
4825   EXPECT_EQ("code();\n"
4826             "#ifndef HEADER_H\n"
4827             "#define HEADER_H\n"
4828             "code();\n"
4829             "#endif",
4830             format("code();\n"
4831                    "#ifndef HEADER_H\n"
4832                    "#  define HEADER_H\n"
4833                    "code();\n"
4834                    "#endif",
4835                    Style));
4836   // Keep comments aligned with #, otherwise indent comments normally. These
4837   // tests cannot use verifyFormat because messUp manipulates leading
4838   // whitespace.
4839   {
4840     const char *Expected = ""
4841                            "void f() {\n"
4842                            "#if 1\n"
4843                            "// Preprocessor aligned.\n"
4844                            "#  define A 0\n"
4845                            "  // Code. Separated by blank line.\n"
4846                            "\n"
4847                            "#  define B 0\n"
4848                            "  // Code. Not aligned with #\n"
4849                            "#  define C 0\n"
4850                            "#endif";
4851     const char *ToFormat = ""
4852                            "void f() {\n"
4853                            "#if 1\n"
4854                            "// Preprocessor aligned.\n"
4855                            "#  define A 0\n"
4856                            "// Code. Separated by blank line.\n"
4857                            "\n"
4858                            "#  define B 0\n"
4859                            "   // Code. Not aligned with #\n"
4860                            "#  define C 0\n"
4861                            "#endif";
4862     EXPECT_EQ(Expected, format(ToFormat, Style));
4863     EXPECT_EQ(Expected, format(Expected, Style));
4864   }
4865   // Keep block quotes aligned.
4866   {
4867     const char *Expected = ""
4868                            "void f() {\n"
4869                            "#if 1\n"
4870                            "/* Preprocessor aligned. */\n"
4871                            "#  define A 0\n"
4872                            "  /* Code. Separated by blank line. */\n"
4873                            "\n"
4874                            "#  define B 0\n"
4875                            "  /* Code. Not aligned with # */\n"
4876                            "#  define C 0\n"
4877                            "#endif";
4878     const char *ToFormat = ""
4879                            "void f() {\n"
4880                            "#if 1\n"
4881                            "/* Preprocessor aligned. */\n"
4882                            "#  define A 0\n"
4883                            "/* Code. Separated by blank line. */\n"
4884                            "\n"
4885                            "#  define B 0\n"
4886                            "   /* Code. Not aligned with # */\n"
4887                            "#  define C 0\n"
4888                            "#endif";
4889     EXPECT_EQ(Expected, format(ToFormat, Style));
4890     EXPECT_EQ(Expected, format(Expected, Style));
4891   }
4892   // Keep comments aligned with un-indented directives.
4893   {
4894     const char *Expected = ""
4895                            "void f() {\n"
4896                            "// Preprocessor aligned.\n"
4897                            "#define A 0\n"
4898                            "  // Code. Separated by blank line.\n"
4899                            "\n"
4900                            "#define B 0\n"
4901                            "  // Code. Not aligned with #\n"
4902                            "#define C 0\n";
4903     const char *ToFormat = ""
4904                            "void f() {\n"
4905                            "// Preprocessor aligned.\n"
4906                            "#define A 0\n"
4907                            "// Code. Separated by blank line.\n"
4908                            "\n"
4909                            "#define B 0\n"
4910                            "   // Code. Not aligned with #\n"
4911                            "#define C 0\n";
4912     EXPECT_EQ(Expected, format(ToFormat, Style));
4913     EXPECT_EQ(Expected, format(Expected, Style));
4914   }
4915   // Test AfterHash with tabs.
4916   {
4917     FormatStyle Tabbed = Style;
4918     Tabbed.UseTab = FormatStyle::UT_Always;
4919     Tabbed.IndentWidth = 8;
4920     Tabbed.TabWidth = 8;
4921     verifyFormat("#ifdef _WIN32\n"
4922                  "#\tdefine A 0\n"
4923                  "#\tifdef VAR2\n"
4924                  "#\t\tdefine B 1\n"
4925                  "#\t\tinclude <someheader.h>\n"
4926                  "#\t\tdefine MACRO          \\\n"
4927                  "\t\t\tsome_very_long_func_aaaaaaaaaa();\n"
4928                  "#\tendif\n"
4929                  "#else\n"
4930                  "#\tdefine A 1\n"
4931                  "#endif",
4932                  Tabbed);
4933   }
4934 
4935   // Regression test: Multiline-macro inside include guards.
4936   verifyFormat("#ifndef HEADER_H\n"
4937                "#define HEADER_H\n"
4938                "#define A()        \\\n"
4939                "  int i;           \\\n"
4940                "  int j;\n"
4941                "#endif // HEADER_H",
4942                getLLVMStyleWithColumns(20));
4943 
4944   Style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash;
4945   // Basic before hash indent tests
4946   verifyFormat("#ifdef _WIN32\n"
4947                "  #define A 0\n"
4948                "  #ifdef VAR2\n"
4949                "    #define B 1\n"
4950                "    #include <someheader.h>\n"
4951                "    #define MACRO                      \\\n"
4952                "      some_very_long_func_aaaaaaaaaa();\n"
4953                "  #endif\n"
4954                "#else\n"
4955                "  #define A 1\n"
4956                "#endif",
4957                Style);
4958   verifyFormat("#if A\n"
4959                "  #define MACRO                        \\\n"
4960                "    void a(int x) {                    \\\n"
4961                "      b();                             \\\n"
4962                "      c();                             \\\n"
4963                "      d();                             \\\n"
4964                "      e();                             \\\n"
4965                "      f();                             \\\n"
4966                "    }\n"
4967                "#endif",
4968                Style);
4969   // Keep comments aligned with indented directives. These
4970   // tests cannot use verifyFormat because messUp manipulates leading
4971   // whitespace.
4972   {
4973     const char *Expected = "void f() {\n"
4974                            "// Aligned to preprocessor.\n"
4975                            "#if 1\n"
4976                            "  // Aligned to code.\n"
4977                            "  int a;\n"
4978                            "  #if 1\n"
4979                            "    // Aligned to preprocessor.\n"
4980                            "    #define A 0\n"
4981                            "  // Aligned to code.\n"
4982                            "  int b;\n"
4983                            "  #endif\n"
4984                            "#endif\n"
4985                            "}";
4986     const char *ToFormat = "void f() {\n"
4987                            "// Aligned to preprocessor.\n"
4988                            "#if 1\n"
4989                            "// Aligned to code.\n"
4990                            "int a;\n"
4991                            "#if 1\n"
4992                            "// Aligned to preprocessor.\n"
4993                            "#define A 0\n"
4994                            "// Aligned to code.\n"
4995                            "int b;\n"
4996                            "#endif\n"
4997                            "#endif\n"
4998                            "}";
4999     EXPECT_EQ(Expected, format(ToFormat, Style));
5000     EXPECT_EQ(Expected, format(Expected, Style));
5001   }
5002   {
5003     const char *Expected = "void f() {\n"
5004                            "/* Aligned to preprocessor. */\n"
5005                            "#if 1\n"
5006                            "  /* Aligned to code. */\n"
5007                            "  int a;\n"
5008                            "  #if 1\n"
5009                            "    /* Aligned to preprocessor. */\n"
5010                            "    #define A 0\n"
5011                            "  /* Aligned to code. */\n"
5012                            "  int b;\n"
5013                            "  #endif\n"
5014                            "#endif\n"
5015                            "}";
5016     const char *ToFormat = "void f() {\n"
5017                            "/* Aligned to preprocessor. */\n"
5018                            "#if 1\n"
5019                            "/* Aligned to code. */\n"
5020                            "int a;\n"
5021                            "#if 1\n"
5022                            "/* Aligned to preprocessor. */\n"
5023                            "#define A 0\n"
5024                            "/* Aligned to code. */\n"
5025                            "int b;\n"
5026                            "#endif\n"
5027                            "#endif\n"
5028                            "}";
5029     EXPECT_EQ(Expected, format(ToFormat, Style));
5030     EXPECT_EQ(Expected, format(Expected, Style));
5031   }
5032 
5033   // Test single comment before preprocessor
5034   verifyFormat("// Comment\n"
5035                "\n"
5036                "#if 1\n"
5037                "#endif",
5038                Style);
5039 }
5040 
5041 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
5042   verifyFormat("{\n  { a #c; }\n}");
5043 }
5044 
5045 TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
5046   EXPECT_EQ("#define A \\\n  {       \\\n    {\nint i;",
5047             format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
5048   EXPECT_EQ("#define A \\\n  }       \\\n  }\nint i;",
5049             format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
5050 }
5051 
5052 TEST_F(FormatTest, EscapedNewlines) {
5053   FormatStyle Narrow = getLLVMStyleWithColumns(11);
5054   EXPECT_EQ("#define A \\\n  int i;  \\\n  int j;",
5055             format("#define A \\\nint i;\\\n  int j;", Narrow));
5056   EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;"));
5057   EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
5058   EXPECT_EQ("/* \\  \\  \\\n */", format("\\\n/* \\  \\  \\\n */"));
5059   EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>"));
5060 
5061   FormatStyle AlignLeft = getLLVMStyle();
5062   AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
5063   EXPECT_EQ("#define MACRO(x) \\\n"
5064             "private:         \\\n"
5065             "  int x(int a);\n",
5066             format("#define MACRO(x) \\\n"
5067                    "private:         \\\n"
5068                    "  int x(int a);\n",
5069                    AlignLeft));
5070 
5071   // CRLF line endings
5072   EXPECT_EQ("#define A \\\r\n  int i;  \\\r\n  int j;",
5073             format("#define A \\\r\nint i;\\\r\n  int j;", Narrow));
5074   EXPECT_EQ("#define A\r\n\r\nint i;", format("#define A \\\r\n\r\n int i;"));
5075   EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
5076   EXPECT_EQ("/* \\  \\  \\\r\n */", format("\\\r\n/* \\  \\  \\\r\n */"));
5077   EXPECT_EQ("<a\r\n\\\\\r\n>", format("<a\r\n\\\\\r\n>"));
5078   EXPECT_EQ("#define MACRO(x) \\\r\n"
5079             "private:         \\\r\n"
5080             "  int x(int a);\r\n",
5081             format("#define MACRO(x) \\\r\n"
5082                    "private:         \\\r\n"
5083                    "  int x(int a);\r\n",
5084                    AlignLeft));
5085 
5086   FormatStyle DontAlign = getLLVMStyle();
5087   DontAlign.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
5088   DontAlign.MaxEmptyLinesToKeep = 3;
5089   // FIXME: can't use verifyFormat here because the newline before
5090   // "public:" is not inserted the first time it's reformatted
5091   EXPECT_EQ("#define A \\\n"
5092             "  class Foo { \\\n"
5093             "    void bar(); \\\n"
5094             "\\\n"
5095             "\\\n"
5096             "\\\n"
5097             "  public: \\\n"
5098             "    void baz(); \\\n"
5099             "  };",
5100             format("#define A \\\n"
5101                    "  class Foo { \\\n"
5102                    "    void bar(); \\\n"
5103                    "\\\n"
5104                    "\\\n"
5105                    "\\\n"
5106                    "  public: \\\n"
5107                    "    void baz(); \\\n"
5108                    "  };",
5109                    DontAlign));
5110 }
5111 
5112 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
5113   verifyFormat("#define A \\\n"
5114                "  int v(  \\\n"
5115                "      a); \\\n"
5116                "  int i;",
5117                getLLVMStyleWithColumns(11));
5118 }
5119 
5120 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
5121   EXPECT_EQ(
5122       "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
5123       "                      \\\n"
5124       "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
5125       "\n"
5126       "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
5127       "    aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
5128       format("  #define   ALooooooooooooooooooooooooooooooooooooooongMacro("
5129              "\\\n"
5130              "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
5131              "  \n"
5132              "   AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
5133              "  aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
5134 }
5135 
5136 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
5137   EXPECT_EQ("int\n"
5138             "#define A\n"
5139             "    a;",
5140             format("int\n#define A\na;"));
5141   verifyFormat("functionCallTo(\n"
5142                "    someOtherFunction(\n"
5143                "        withSomeParameters, whichInSequence,\n"
5144                "        areLongerThanALine(andAnotherCall,\n"
5145                "#define A B\n"
5146                "                           withMoreParamters,\n"
5147                "                           whichStronglyInfluenceTheLayout),\n"
5148                "        andMoreParameters),\n"
5149                "    trailing);",
5150                getLLVMStyleWithColumns(69));
5151   verifyFormat("Foo::Foo()\n"
5152                "#ifdef BAR\n"
5153                "    : baz(0)\n"
5154                "#endif\n"
5155                "{\n"
5156                "}");
5157   verifyFormat("void f() {\n"
5158                "  if (true)\n"
5159                "#ifdef A\n"
5160                "    f(42);\n"
5161                "  x();\n"
5162                "#else\n"
5163                "    g();\n"
5164                "  x();\n"
5165                "#endif\n"
5166                "}");
5167   verifyFormat("void f(param1, param2,\n"
5168                "       param3,\n"
5169                "#ifdef A\n"
5170                "       param4(param5,\n"
5171                "#ifdef A1\n"
5172                "              param6,\n"
5173                "#ifdef A2\n"
5174                "              param7),\n"
5175                "#else\n"
5176                "              param8),\n"
5177                "       param9,\n"
5178                "#endif\n"
5179                "       param10,\n"
5180                "#endif\n"
5181                "       param11)\n"
5182                "#else\n"
5183                "       param12)\n"
5184                "#endif\n"
5185                "{\n"
5186                "  x();\n"
5187                "}",
5188                getLLVMStyleWithColumns(28));
5189   verifyFormat("#if 1\n"
5190                "int i;");
5191   verifyFormat("#if 1\n"
5192                "#endif\n"
5193                "#if 1\n"
5194                "#else\n"
5195                "#endif\n");
5196   verifyFormat("DEBUG({\n"
5197                "  return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5198                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
5199                "});\n"
5200                "#if a\n"
5201                "#else\n"
5202                "#endif");
5203 
5204   verifyIncompleteFormat("void f(\n"
5205                          "#if A\n"
5206                          ");\n"
5207                          "#else\n"
5208                          "#endif");
5209 }
5210 
5211 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
5212   verifyFormat("#endif\n"
5213                "#if B");
5214 }
5215 
5216 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
5217   FormatStyle SingleLine = getLLVMStyle();
5218   SingleLine.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse;
5219   verifyFormat("#if 0\n"
5220                "#elif 1\n"
5221                "#endif\n"
5222                "void foo() {\n"
5223                "  if (test) foo2();\n"
5224                "}",
5225                SingleLine);
5226 }
5227 
5228 TEST_F(FormatTest, LayoutBlockInsideParens) {
5229   verifyFormat("functionCall({ int i; });");
5230   verifyFormat("functionCall({\n"
5231                "  int i;\n"
5232                "  int j;\n"
5233                "});");
5234   verifyFormat("functionCall(\n"
5235                "    {\n"
5236                "      int i;\n"
5237                "      int j;\n"
5238                "    },\n"
5239                "    aaaa, bbbb, cccc);");
5240   verifyFormat("functionA(functionB({\n"
5241                "            int i;\n"
5242                "            int j;\n"
5243                "          }),\n"
5244                "          aaaa, bbbb, cccc);");
5245   verifyFormat("functionCall(\n"
5246                "    {\n"
5247                "      int i;\n"
5248                "      int j;\n"
5249                "    },\n"
5250                "    aaaa, bbbb, // comment\n"
5251                "    cccc);");
5252   verifyFormat("functionA(functionB({\n"
5253                "            int i;\n"
5254                "            int j;\n"
5255                "          }),\n"
5256                "          aaaa, bbbb, // comment\n"
5257                "          cccc);");
5258   verifyFormat("functionCall(aaaa, bbbb, { int i; });");
5259   verifyFormat("functionCall(aaaa, bbbb, {\n"
5260                "  int i;\n"
5261                "  int j;\n"
5262                "});");
5263   verifyFormat(
5264       "Aaa(\n" // FIXME: There shouldn't be a linebreak here.
5265       "    {\n"
5266       "      int i; // break\n"
5267       "    },\n"
5268       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
5269       "                                     ccccccccccccccccc));");
5270   verifyFormat("DEBUG({\n"
5271                "  if (a)\n"
5272                "    f();\n"
5273                "});");
5274 }
5275 
5276 TEST_F(FormatTest, LayoutBlockInsideStatement) {
5277   EXPECT_EQ("SOME_MACRO { int i; }\n"
5278             "int i;",
5279             format("  SOME_MACRO  {int i;}  int i;"));
5280 }
5281 
5282 TEST_F(FormatTest, LayoutNestedBlocks) {
5283   verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
5284                "  struct s {\n"
5285                "    int i;\n"
5286                "  };\n"
5287                "  s kBitsToOs[] = {{10}};\n"
5288                "  for (int i = 0; i < 10; ++i)\n"
5289                "    return;\n"
5290                "}");
5291   verifyFormat("call(parameter, {\n"
5292                "  something();\n"
5293                "  // Comment using all columns.\n"
5294                "  somethingelse();\n"
5295                "});",
5296                getLLVMStyleWithColumns(40));
5297   verifyFormat("DEBUG( //\n"
5298                "    { f(); }, a);");
5299   verifyFormat("DEBUG( //\n"
5300                "    {\n"
5301                "      f(); //\n"
5302                "    },\n"
5303                "    a);");
5304 
5305   EXPECT_EQ("call(parameter, {\n"
5306             "  something();\n"
5307             "  // Comment too\n"
5308             "  // looooooooooong.\n"
5309             "  somethingElse();\n"
5310             "});",
5311             format("call(parameter, {\n"
5312                    "  something();\n"
5313                    "  // Comment too looooooooooong.\n"
5314                    "  somethingElse();\n"
5315                    "});",
5316                    getLLVMStyleWithColumns(29)));
5317   EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int   i; });"));
5318   EXPECT_EQ("DEBUG({ // comment\n"
5319             "  int i;\n"
5320             "});",
5321             format("DEBUG({ // comment\n"
5322                    "int  i;\n"
5323                    "});"));
5324   EXPECT_EQ("DEBUG({\n"
5325             "  int i;\n"
5326             "\n"
5327             "  // comment\n"
5328             "  int j;\n"
5329             "});",
5330             format("DEBUG({\n"
5331                    "  int  i;\n"
5332                    "\n"
5333                    "  // comment\n"
5334                    "  int  j;\n"
5335                    "});"));
5336 
5337   verifyFormat("DEBUG({\n"
5338                "  if (a)\n"
5339                "    return;\n"
5340                "});");
5341   verifyGoogleFormat("DEBUG({\n"
5342                      "  if (a) return;\n"
5343                      "});");
5344   FormatStyle Style = getGoogleStyle();
5345   Style.ColumnLimit = 45;
5346   verifyFormat("Debug(\n"
5347                "    aaaaa,\n"
5348                "    {\n"
5349                "      if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
5350                "    },\n"
5351                "    a);",
5352                Style);
5353 
5354   verifyFormat("SomeFunction({MACRO({ return output; }), b});");
5355 
5356   verifyNoCrash("^{v^{a}}");
5357 }
5358 
5359 TEST_F(FormatTest, FormatNestedBlocksInMacros) {
5360   EXPECT_EQ("#define MACRO()                     \\\n"
5361             "  Debug(aaa, /* force line break */ \\\n"
5362             "        {                           \\\n"
5363             "          int i;                    \\\n"
5364             "          int j;                    \\\n"
5365             "        })",
5366             format("#define   MACRO()   Debug(aaa,  /* force line break */ \\\n"
5367                    "          {  int   i;  int  j;   })",
5368                    getGoogleStyle()));
5369 
5370   EXPECT_EQ("#define A                                       \\\n"
5371             "  [] {                                          \\\n"
5372             "    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(        \\\n"
5373             "        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
5374             "  }",
5375             format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
5376                    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
5377                    getGoogleStyle()));
5378 }
5379 
5380 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
5381   EXPECT_EQ("{}", format("{}"));
5382   verifyFormat("enum E {};");
5383   verifyFormat("enum E {}");
5384   FormatStyle Style = getLLVMStyle();
5385   Style.SpaceInEmptyBlock = true;
5386   EXPECT_EQ("void f() { }", format("void f() {}", Style));
5387   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty;
5388   EXPECT_EQ("while (true) { }", format("while (true) {}", Style));
5389 }
5390 
5391 TEST_F(FormatTest, FormatBeginBlockEndMacros) {
5392   FormatStyle Style = getLLVMStyle();
5393   Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$";
5394   Style.MacroBlockEnd = "^[A-Z_]+_END$";
5395   verifyFormat("FOO_BEGIN\n"
5396                "  FOO_ENTRY\n"
5397                "FOO_END",
5398                Style);
5399   verifyFormat("FOO_BEGIN\n"
5400                "  NESTED_FOO_BEGIN\n"
5401                "    NESTED_FOO_ENTRY\n"
5402                "  NESTED_FOO_END\n"
5403                "FOO_END",
5404                Style);
5405   verifyFormat("FOO_BEGIN(Foo, Bar)\n"
5406                "  int x;\n"
5407                "  x = 1;\n"
5408                "FOO_END(Baz)",
5409                Style);
5410 }
5411 
5412 //===----------------------------------------------------------------------===//
5413 // Line break tests.
5414 //===----------------------------------------------------------------------===//
5415 
5416 TEST_F(FormatTest, PreventConfusingIndents) {
5417   verifyFormat(
5418       "void f() {\n"
5419       "  SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
5420       "                         parameter, parameter, parameter)),\n"
5421       "                     SecondLongCall(parameter));\n"
5422       "}");
5423   verifyFormat(
5424       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5425       "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
5426       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
5427       "    aaaaaaaaaaaaaaaaaaaaaaaa);");
5428   verifyFormat(
5429       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5430       "    [aaaaaaaaaaaaaaaaaaaaaaaa\n"
5431       "         [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
5432       "         [aaaaaaaaaaaaaaaaaaaaaaaa]];");
5433   verifyFormat(
5434       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
5435       "    aaaaaaaaaaaaaaaaaaaaaaaa<\n"
5436       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
5437       "    aaaaaaaaaaaaaaaaaaaaaaaa>;");
5438   verifyFormat("int a = bbbb && ccc &&\n"
5439                "        fffff(\n"
5440                "#define A Just forcing a new line\n"
5441                "            ddd);");
5442 }
5443 
5444 TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
5445   verifyFormat(
5446       "bool aaaaaaa =\n"
5447       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
5448       "    bbbbbbbb();");
5449   verifyFormat(
5450       "bool aaaaaaa =\n"
5451       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n"
5452       "    bbbbbbbb();");
5453 
5454   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
5455                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
5456                "    ccccccccc == ddddddddddd;");
5457   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
5458                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n"
5459                "    ccccccccc == ddddddddddd;");
5460   verifyFormat(
5461       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
5462       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n"
5463       "    ccccccccc == ddddddddddd;");
5464 
5465   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
5466                "                 aaaaaa) &&\n"
5467                "         bbbbbb && cccccc;");
5468   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
5469                "                 aaaaaa) >>\n"
5470                "         bbbbbb;");
5471   verifyFormat("aa = Whitespaces.addUntouchableComment(\n"
5472                "    SourceMgr.getSpellingColumnNumber(\n"
5473                "        TheLine.Last->FormatTok.Tok.getLocation()) -\n"
5474                "    1);");
5475 
5476   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
5477                "     bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
5478                "    cccccc) {\n}");
5479   verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
5480                "               bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n"
5481                "              cccccc) {\n}");
5482   verifyFormat("if CONSTEXPR ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
5483                "               bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n"
5484                "              cccccc) {\n}");
5485   verifyFormat("b = a &&\n"
5486                "    // Comment\n"
5487                "    b.c && d;");
5488 
5489   // If the LHS of a comparison is not a binary expression itself, the
5490   // additional linebreak confuses many people.
5491   verifyFormat(
5492       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5493       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
5494       "}");
5495   verifyFormat(
5496       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5497       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
5498       "}");
5499   verifyFormat(
5500       "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
5501       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
5502       "}");
5503   verifyFormat(
5504       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5505       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) <=> 5) {\n"
5506       "}");
5507   // Even explicit parentheses stress the precedence enough to make the
5508   // additional break unnecessary.
5509   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5510                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
5511                "}");
5512   // This cases is borderline, but with the indentation it is still readable.
5513   verifyFormat(
5514       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5515       "        aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5516       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
5517       "}",
5518       getLLVMStyleWithColumns(75));
5519 
5520   // If the LHS is a binary expression, we should still use the additional break
5521   // as otherwise the formatting hides the operator precedence.
5522   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5523                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
5524                "    5) {\n"
5525                "}");
5526   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5527                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa <=>\n"
5528                "    5) {\n"
5529                "}");
5530 
5531   FormatStyle OnePerLine = getLLVMStyle();
5532   OnePerLine.BinPackParameters = false;
5533   verifyFormat(
5534       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
5535       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
5536       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
5537       OnePerLine);
5538 
5539   verifyFormat("int i = someFunction(aaaaaaa, 0)\n"
5540                "                .aaa(aaaaaaaaaaaaa) *\n"
5541                "            aaaaaaa +\n"
5542                "        aaaaaaa;",
5543                getLLVMStyleWithColumns(40));
5544 }
5545 
5546 TEST_F(FormatTest, ExpressionIndentation) {
5547   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5548                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5549                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
5550                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
5551                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
5552                "                     bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
5553                "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
5554                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
5555                "                 ccccccccccccccccccccccccccccccccccccccccc;");
5556   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
5557                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5558                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
5559                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
5560   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5561                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
5562                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
5563                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
5564   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
5565                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
5566                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5567                "        bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
5568   verifyFormat("if () {\n"
5569                "} else if (aaaaa && bbbbb > // break\n"
5570                "                        ccccc) {\n"
5571                "}");
5572   verifyFormat("if () {\n"
5573                "} else if constexpr (aaaaa && bbbbb > // break\n"
5574                "                                  ccccc) {\n"
5575                "}");
5576   verifyFormat("if () {\n"
5577                "} else if CONSTEXPR (aaaaa && bbbbb > // break\n"
5578                "                                  ccccc) {\n"
5579                "}");
5580   verifyFormat("if () {\n"
5581                "} else if (aaaaa &&\n"
5582                "           bbbbb > // break\n"
5583                "               ccccc &&\n"
5584                "           ddddd) {\n"
5585                "}");
5586 
5587   // Presence of a trailing comment used to change indentation of b.
5588   verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
5589                "       b;\n"
5590                "return aaaaaaaaaaaaaaaaaaa +\n"
5591                "       b; //",
5592                getLLVMStyleWithColumns(30));
5593 }
5594 
5595 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
5596   // Not sure what the best system is here. Like this, the LHS can be found
5597   // immediately above an operator (everything with the same or a higher
5598   // indent). The RHS is aligned right of the operator and so compasses
5599   // everything until something with the same indent as the operator is found.
5600   // FIXME: Is this a good system?
5601   FormatStyle Style = getLLVMStyle();
5602   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
5603   verifyFormat(
5604       "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5605       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5606       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5607       "                 == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5608       "                            * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5609       "                        + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5610       "             && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5611       "                        * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5612       "                    > ccccccccccccccccccccccccccccccccccccccccc;",
5613       Style);
5614   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5615                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5616                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5617                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
5618                Style);
5619   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5620                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5621                "              * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5622                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
5623                Style);
5624   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5625                "    == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5626                "               * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5627                "           + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
5628                Style);
5629   verifyFormat("if () {\n"
5630                "} else if (aaaaa\n"
5631                "           && bbbbb // break\n"
5632                "                  > ccccc) {\n"
5633                "}",
5634                Style);
5635   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5636                "       && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
5637                Style);
5638   verifyFormat("return (a)\n"
5639                "       // comment\n"
5640                "       + b;",
5641                Style);
5642   verifyFormat(
5643       "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5644       "                 * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5645       "             + cc;",
5646       Style);
5647 
5648   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5649                "    = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
5650                Style);
5651 
5652   // Forced by comments.
5653   verifyFormat(
5654       "unsigned ContentSize =\n"
5655       "    sizeof(int16_t)   // DWARF ARange version number\n"
5656       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
5657       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
5658       "    + sizeof(int8_t); // Segment Size (in bytes)");
5659 
5660   verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
5661                "       == boost::fusion::at_c<1>(iiii).second;",
5662                Style);
5663 
5664   Style.ColumnLimit = 60;
5665   verifyFormat("zzzzzzzzzz\n"
5666                "    = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5667                "      >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
5668                Style);
5669 
5670   Style.ColumnLimit = 80;
5671   Style.IndentWidth = 4;
5672   Style.TabWidth = 4;
5673   Style.UseTab = FormatStyle::UT_Always;
5674   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
5675   Style.AlignOperands = FormatStyle::OAS_DontAlign;
5676   EXPECT_EQ("return someVeryVeryLongConditionThatBarelyFitsOnALine\n"
5677             "\t&& (someOtherLongishConditionPart1\n"
5678             "\t\t|| someOtherEvenLongerNestedConditionPart2);",
5679             format("return someVeryVeryLongConditionThatBarelyFitsOnALine && "
5680                    "(someOtherLongishConditionPart1 || "
5681                    "someOtherEvenLongerNestedConditionPart2);",
5682                    Style));
5683 }
5684 
5685 TEST_F(FormatTest, ExpressionIndentationStrictAlign) {
5686   FormatStyle Style = getLLVMStyle();
5687   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
5688   Style.AlignOperands = FormatStyle::OAS_AlignAfterOperator;
5689 
5690   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5691                "                   + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5692                "                   + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5693                "              == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5694                "                         * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5695                "                     + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5696                "          && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5697                "                     * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5698                "                 > ccccccccccccccccccccccccccccccccccccccccc;",
5699                Style);
5700   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5701                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5702                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5703                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
5704                Style);
5705   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5706                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5707                "              * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5708                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
5709                Style);
5710   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5711                "    == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5712                "               * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5713                "           + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
5714                Style);
5715   verifyFormat("if () {\n"
5716                "} else if (aaaaa\n"
5717                "           && bbbbb // break\n"
5718                "                  > ccccc) {\n"
5719                "}",
5720                Style);
5721   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5722                "    && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
5723                Style);
5724   verifyFormat("return (a)\n"
5725                "     // comment\n"
5726                "     + b;",
5727                Style);
5728   verifyFormat(
5729       "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5730       "               * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5731       "           + cc;",
5732       Style);
5733   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
5734                "     : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
5735                "                        : 3333333333333333;",
5736                Style);
5737   verifyFormat(
5738       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa    ? bbbbbbbbbbbbbbbbbb\n"
5739       "                           : ccccccccccccccc ? dddddddddddddddddd\n"
5740       "                                             : eeeeeeeeeeeeeeeeee)\n"
5741       "     : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
5742       "                        : 3333333333333333;",
5743       Style);
5744   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5745                "    = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
5746                Style);
5747 
5748   verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
5749                "    == boost::fusion::at_c<1>(iiii).second;",
5750                Style);
5751 
5752   Style.ColumnLimit = 60;
5753   verifyFormat("zzzzzzzzzzzzz\n"
5754                "    = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5755                "   >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
5756                Style);
5757 
5758   // Forced by comments.
5759   Style.ColumnLimit = 80;
5760   verifyFormat(
5761       "unsigned ContentSize\n"
5762       "    = sizeof(int16_t) // DWARF ARange version number\n"
5763       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
5764       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
5765       "    + sizeof(int8_t); // Segment Size (in bytes)",
5766       Style);
5767 
5768   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
5769   verifyFormat(
5770       "unsigned ContentSize =\n"
5771       "    sizeof(int16_t)   // DWARF ARange version number\n"
5772       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
5773       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
5774       "    + sizeof(int8_t); // Segment Size (in bytes)",
5775       Style);
5776 
5777   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
5778   verifyFormat(
5779       "unsigned ContentSize =\n"
5780       "    sizeof(int16_t)   // DWARF ARange version number\n"
5781       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
5782       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
5783       "    + sizeof(int8_t); // Segment Size (in bytes)",
5784       Style);
5785 }
5786 
5787 TEST_F(FormatTest, EnforcedOperatorWraps) {
5788   // Here we'd like to wrap after the || operators, but a comment is forcing an
5789   // earlier wrap.
5790   verifyFormat("bool x = aaaaa //\n"
5791                "         || bbbbb\n"
5792                "         //\n"
5793                "         || cccc;");
5794 }
5795 
5796 TEST_F(FormatTest, NoOperandAlignment) {
5797   FormatStyle Style = getLLVMStyle();
5798   Style.AlignOperands = FormatStyle::OAS_DontAlign;
5799   verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n"
5800                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5801                "                   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
5802                Style);
5803   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
5804   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5805                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5806                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5807                "        == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5808                "                * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5809                "            + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5810                "    && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5811                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5812                "        > ccccccccccccccccccccccccccccccccccccccccc;",
5813                Style);
5814 
5815   verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5816                "        * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5817                "    + cc;",
5818                Style);
5819   verifyFormat("int a = aa\n"
5820                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5821                "        * cccccccccccccccccccccccccccccccccccc;\n",
5822                Style);
5823 
5824   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
5825   verifyFormat("return (a > b\n"
5826                "    // comment1\n"
5827                "    // comment2\n"
5828                "    || c);",
5829                Style);
5830 }
5831 
5832 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) {
5833   FormatStyle Style = getLLVMStyle();
5834   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
5835   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5836                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5837                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
5838                Style);
5839 }
5840 
5841 TEST_F(FormatTest, AllowBinPackingInsideArguments) {
5842   FormatStyle Style = getLLVMStyle();
5843   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
5844   Style.BinPackArguments = false;
5845   Style.ColumnLimit = 40;
5846   verifyFormat("void test() {\n"
5847                "  someFunction(\n"
5848                "      this + argument + is + quite\n"
5849                "      + long + so + it + gets + wrapped\n"
5850                "      + but + remains + bin - packed);\n"
5851                "}",
5852                Style);
5853   verifyFormat("void test() {\n"
5854                "  someFunction(arg1,\n"
5855                "               this + argument + is\n"
5856                "                   + quite + long + so\n"
5857                "                   + it + gets + wrapped\n"
5858                "                   + but + remains + bin\n"
5859                "                   - packed,\n"
5860                "               arg3);\n"
5861                "}",
5862                Style);
5863   verifyFormat("void test() {\n"
5864                "  someFunction(\n"
5865                "      arg1,\n"
5866                "      this + argument + has\n"
5867                "          + anotherFunc(nested,\n"
5868                "                        calls + whose\n"
5869                "                            + arguments\n"
5870                "                            + are + also\n"
5871                "                            + wrapped,\n"
5872                "                        in + addition)\n"
5873                "          + to + being + bin - packed,\n"
5874                "      arg3);\n"
5875                "}",
5876                Style);
5877 
5878   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
5879   verifyFormat("void test() {\n"
5880                "  someFunction(\n"
5881                "      arg1,\n"
5882                "      this + argument + has +\n"
5883                "          anotherFunc(nested,\n"
5884                "                      calls + whose +\n"
5885                "                          arguments +\n"
5886                "                          are + also +\n"
5887                "                          wrapped,\n"
5888                "                      in + addition) +\n"
5889                "          to + being + bin - packed,\n"
5890                "      arg3);\n"
5891                "}",
5892                Style);
5893 }
5894 
5895 TEST_F(FormatTest, ConstructorInitializers) {
5896   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
5897   verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
5898                getLLVMStyleWithColumns(45));
5899   verifyFormat("Constructor()\n"
5900                "    : Inttializer(FitsOnTheLine) {}",
5901                getLLVMStyleWithColumns(44));
5902   verifyFormat("Constructor()\n"
5903                "    : Inttializer(FitsOnTheLine) {}",
5904                getLLVMStyleWithColumns(43));
5905 
5906   verifyFormat("template <typename T>\n"
5907                "Constructor() : Initializer(FitsOnTheLine) {}",
5908                getLLVMStyleWithColumns(45));
5909 
5910   verifyFormat(
5911       "SomeClass::Constructor()\n"
5912       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
5913 
5914   verifyFormat(
5915       "SomeClass::Constructor()\n"
5916       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
5917       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
5918   verifyFormat(
5919       "SomeClass::Constructor()\n"
5920       "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
5921       "      aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
5922   verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5923                "            aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5924                "    : aaaaaaaaaa(aaaaaa) {}");
5925 
5926   verifyFormat("Constructor()\n"
5927                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
5928                "      aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5929                "                               aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
5930                "      aaaaaaaaaaaaaaaaaaaaaaa() {}");
5931 
5932   verifyFormat("Constructor()\n"
5933                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5934                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
5935 
5936   verifyFormat("Constructor(int Parameter = 0)\n"
5937                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
5938                "      aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
5939   verifyFormat("Constructor()\n"
5940                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
5941                "}",
5942                getLLVMStyleWithColumns(60));
5943   verifyFormat("Constructor()\n"
5944                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5945                "          aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}");
5946 
5947   // Here a line could be saved by splitting the second initializer onto two
5948   // lines, but that is not desirable.
5949   verifyFormat("Constructor()\n"
5950                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
5951                "      aaaaaaaaaaa(aaaaaaaaaaa),\n"
5952                "      aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
5953 
5954   FormatStyle OnePerLine = getLLVMStyle();
5955   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
5956   OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false;
5957   verifyFormat("SomeClass::Constructor()\n"
5958                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
5959                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
5960                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
5961                OnePerLine);
5962   verifyFormat("SomeClass::Constructor()\n"
5963                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
5964                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
5965                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
5966                OnePerLine);
5967   verifyFormat("MyClass::MyClass(int var)\n"
5968                "    : some_var_(var),            // 4 space indent\n"
5969                "      some_other_var_(var + 1) { // lined up\n"
5970                "}",
5971                OnePerLine);
5972   verifyFormat("Constructor()\n"
5973                "    : aaaaa(aaaaaa),\n"
5974                "      aaaaa(aaaaaa),\n"
5975                "      aaaaa(aaaaaa),\n"
5976                "      aaaaa(aaaaaa),\n"
5977                "      aaaaa(aaaaaa) {}",
5978                OnePerLine);
5979   verifyFormat("Constructor()\n"
5980                "    : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
5981                "            aaaaaaaaaaaaaaaaaaaaaa) {}",
5982                OnePerLine);
5983   OnePerLine.BinPackParameters = false;
5984   verifyFormat(
5985       "Constructor()\n"
5986       "    : aaaaaaaaaaaaaaaaaaaaaaaa(\n"
5987       "          aaaaaaaaaaa().aaa(),\n"
5988       "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
5989       OnePerLine);
5990   OnePerLine.ColumnLimit = 60;
5991   verifyFormat("Constructor()\n"
5992                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
5993                "      bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
5994                OnePerLine);
5995 
5996   EXPECT_EQ("Constructor()\n"
5997             "    : // Comment forcing unwanted break.\n"
5998             "      aaaa(aaaa) {}",
5999             format("Constructor() :\n"
6000                    "    // Comment forcing unwanted break.\n"
6001                    "    aaaa(aaaa) {}"));
6002 }
6003 
6004 TEST_F(FormatTest, AllowAllConstructorInitializersOnNextLine) {
6005   FormatStyle Style = getLLVMStyle();
6006   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
6007   Style.ColumnLimit = 60;
6008   Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
6009   Style.AllowAllConstructorInitializersOnNextLine = true;
6010   Style.BinPackParameters = false;
6011 
6012   for (int i = 0; i < 4; ++i) {
6013     // Test all combinations of parameters that should not have an effect.
6014     Style.AllowAllParametersOfDeclarationOnNextLine = i & 1;
6015     Style.AllowAllArgumentsOnNextLine = i & 2;
6016 
6017     Style.AllowAllConstructorInitializersOnNextLine = true;
6018     Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
6019     verifyFormat("Constructor()\n"
6020                  "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6021                  Style);
6022     verifyFormat("Constructor() : a(a), b(b) {}", Style);
6023 
6024     Style.AllowAllConstructorInitializersOnNextLine = false;
6025     verifyFormat("Constructor()\n"
6026                  "    : aaaaaaaaaaaaaaaaaaaa(a)\n"
6027                  "    , bbbbbbbbbbbbbbbbbbbbb(b) {}",
6028                  Style);
6029     verifyFormat("Constructor() : a(a), b(b) {}", Style);
6030 
6031     Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
6032     Style.AllowAllConstructorInitializersOnNextLine = true;
6033     verifyFormat("Constructor()\n"
6034                  "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6035                  Style);
6036 
6037     Style.AllowAllConstructorInitializersOnNextLine = false;
6038     verifyFormat("Constructor()\n"
6039                  "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
6040                  "      bbbbbbbbbbbbbbbbbbbbb(b) {}",
6041                  Style);
6042 
6043     Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
6044     Style.AllowAllConstructorInitializersOnNextLine = true;
6045     verifyFormat("Constructor() :\n"
6046                  "    aaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6047                  Style);
6048 
6049     Style.AllowAllConstructorInitializersOnNextLine = false;
6050     verifyFormat("Constructor() :\n"
6051                  "    aaaaaaaaaaaaaaaaaa(a),\n"
6052                  "    bbbbbbbbbbbbbbbbbbbbb(b) {}",
6053                  Style);
6054   }
6055 
6056   // Test interactions between AllowAllParametersOfDeclarationOnNextLine and
6057   // AllowAllConstructorInitializersOnNextLine in all
6058   // BreakConstructorInitializers modes
6059   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
6060   Style.AllowAllParametersOfDeclarationOnNextLine = true;
6061   Style.AllowAllConstructorInitializersOnNextLine = false;
6062   verifyFormat("SomeClassWithALongName::Constructor(\n"
6063                "    int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n"
6064                "    : aaaaaaaaaaaaaaaaaaaa(a)\n"
6065                "    , bbbbbbbbbbbbbbbbbbbbb(b) {}",
6066                Style);
6067 
6068   Style.AllowAllConstructorInitializersOnNextLine = true;
6069   verifyFormat("SomeClassWithALongName::Constructor(\n"
6070                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6071                "    int bbbbbbbbbbbbb,\n"
6072                "    int cccccccccccccccc)\n"
6073                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6074                Style);
6075 
6076   Style.AllowAllParametersOfDeclarationOnNextLine = false;
6077   Style.AllowAllConstructorInitializersOnNextLine = false;
6078   verifyFormat("SomeClassWithALongName::Constructor(\n"
6079                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6080                "    int bbbbbbbbbbbbb)\n"
6081                "    : aaaaaaaaaaaaaaaaaaaa(a)\n"
6082                "    , bbbbbbbbbbbbbbbbbbbbb(b) {}",
6083                Style);
6084 
6085   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
6086 
6087   Style.AllowAllParametersOfDeclarationOnNextLine = true;
6088   verifyFormat("SomeClassWithALongName::Constructor(\n"
6089                "    int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n"
6090                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
6091                "      bbbbbbbbbbbbbbbbbbbbb(b) {}",
6092                Style);
6093 
6094   Style.AllowAllConstructorInitializersOnNextLine = true;
6095   verifyFormat("SomeClassWithALongName::Constructor(\n"
6096                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6097                "    int bbbbbbbbbbbbb,\n"
6098                "    int cccccccccccccccc)\n"
6099                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6100                Style);
6101 
6102   Style.AllowAllParametersOfDeclarationOnNextLine = false;
6103   Style.AllowAllConstructorInitializersOnNextLine = false;
6104   verifyFormat("SomeClassWithALongName::Constructor(\n"
6105                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6106                "    int bbbbbbbbbbbbb)\n"
6107                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
6108                "      bbbbbbbbbbbbbbbbbbbbb(b) {}",
6109                Style);
6110 
6111   Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
6112   Style.AllowAllParametersOfDeclarationOnNextLine = true;
6113   verifyFormat("SomeClassWithALongName::Constructor(\n"
6114                "    int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb) :\n"
6115                "    aaaaaaaaaaaaaaaaaaaa(a),\n"
6116                "    bbbbbbbbbbbbbbbbbbbbb(b) {}",
6117                Style);
6118 
6119   Style.AllowAllConstructorInitializersOnNextLine = true;
6120   verifyFormat("SomeClassWithALongName::Constructor(\n"
6121                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6122                "    int bbbbbbbbbbbbb,\n"
6123                "    int cccccccccccccccc) :\n"
6124                "    aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6125                Style);
6126 
6127   Style.AllowAllParametersOfDeclarationOnNextLine = false;
6128   Style.AllowAllConstructorInitializersOnNextLine = false;
6129   verifyFormat("SomeClassWithALongName::Constructor(\n"
6130                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6131                "    int bbbbbbbbbbbbb) :\n"
6132                "    aaaaaaaaaaaaaaaaaaaa(a),\n"
6133                "    bbbbbbbbbbbbbbbbbbbbb(b) {}",
6134                Style);
6135 }
6136 
6137 TEST_F(FormatTest, AllowAllArgumentsOnNextLine) {
6138   FormatStyle Style = getLLVMStyle();
6139   Style.ColumnLimit = 60;
6140   Style.BinPackArguments = false;
6141   for (int i = 0; i < 4; ++i) {
6142     // Test all combinations of parameters that should not have an effect.
6143     Style.AllowAllParametersOfDeclarationOnNextLine = i & 1;
6144     Style.AllowAllConstructorInitializersOnNextLine = i & 2;
6145 
6146     Style.AllowAllArgumentsOnNextLine = true;
6147     verifyFormat("void foo() {\n"
6148                  "  FunctionCallWithReallyLongName(\n"
6149                  "      aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb);\n"
6150                  "}",
6151                  Style);
6152     Style.AllowAllArgumentsOnNextLine = false;
6153     verifyFormat("void foo() {\n"
6154                  "  FunctionCallWithReallyLongName(\n"
6155                  "      aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6156                  "      bbbbbbbbbbbb);\n"
6157                  "}",
6158                  Style);
6159 
6160     Style.AllowAllArgumentsOnNextLine = true;
6161     verifyFormat("void foo() {\n"
6162                  "  auto VariableWithReallyLongName = {\n"
6163                  "      aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb};\n"
6164                  "}",
6165                  Style);
6166     Style.AllowAllArgumentsOnNextLine = false;
6167     verifyFormat("void foo() {\n"
6168                  "  auto VariableWithReallyLongName = {\n"
6169                  "      aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6170                  "      bbbbbbbbbbbb};\n"
6171                  "}",
6172                  Style);
6173   }
6174 
6175   // This parameter should not affect declarations.
6176   Style.BinPackParameters = false;
6177   Style.AllowAllArgumentsOnNextLine = false;
6178   Style.AllowAllParametersOfDeclarationOnNextLine = true;
6179   verifyFormat("void FunctionCallWithReallyLongName(\n"
6180                "    int aaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbb);",
6181                Style);
6182   Style.AllowAllParametersOfDeclarationOnNextLine = false;
6183   verifyFormat("void FunctionCallWithReallyLongName(\n"
6184                "    int aaaaaaaaaaaaaaaaaaaaaaa,\n"
6185                "    int bbbbbbbbbbbb);",
6186                Style);
6187 }
6188 
6189 TEST_F(FormatTest, AllowAllArgumentsOnNextLineDontAlign) {
6190   // Check that AllowAllArgumentsOnNextLine is respected for both BAS_DontAlign
6191   // and BAS_Align.
6192   auto Style = getLLVMStyle();
6193   Style.ColumnLimit = 35;
6194   StringRef Input = "functionCall(paramA, paramB, paramC);\n"
6195                     "void functionDecl(int A, int B, int C);";
6196   Style.AllowAllArgumentsOnNextLine = false;
6197   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
6198   EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n"
6199                       "    paramC);\n"
6200                       "void functionDecl(int A, int B,\n"
6201                       "    int C);"),
6202             format(Input, Style));
6203   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
6204   EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n"
6205                       "             paramC);\n"
6206                       "void functionDecl(int A, int B,\n"
6207                       "                  int C);"),
6208             format(Input, Style));
6209   // However, BAS_AlwaysBreak should take precedence over
6210   // AllowAllArgumentsOnNextLine.
6211   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
6212   EXPECT_EQ(StringRef("functionCall(\n"
6213                       "    paramA, paramB, paramC);\n"
6214                       "void functionDecl(\n"
6215                       "    int A, int B, int C);"),
6216             format(Input, Style));
6217 
6218   // When AllowAllArgumentsOnNextLine is set, we prefer breaking before the
6219   // first argument.
6220   Style.AllowAllArgumentsOnNextLine = true;
6221   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
6222   EXPECT_EQ(StringRef("functionCall(\n"
6223                       "    paramA, paramB, paramC);\n"
6224                       "void functionDecl(\n"
6225                       "    int A, int B, int C);"),
6226             format(Input, Style));
6227   // It wouldn't fit on one line with aligned parameters so this setting
6228   // doesn't change anything for BAS_Align.
6229   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
6230   EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n"
6231                       "             paramC);\n"
6232                       "void functionDecl(int A, int B,\n"
6233                       "                  int C);"),
6234             format(Input, Style));
6235   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
6236   EXPECT_EQ(StringRef("functionCall(\n"
6237                       "    paramA, paramB, paramC);\n"
6238                       "void functionDecl(\n"
6239                       "    int A, int B, int C);"),
6240             format(Input, Style));
6241 }
6242 
6243 TEST_F(FormatTest, BreakConstructorInitializersAfterColon) {
6244   FormatStyle Style = getLLVMStyle();
6245   Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
6246 
6247   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
6248   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}",
6249                getStyleWithColumns(Style, 45));
6250   verifyFormat("Constructor() :\n"
6251                "    Initializer(FitsOnTheLine) {}",
6252                getStyleWithColumns(Style, 44));
6253   verifyFormat("Constructor() :\n"
6254                "    Initializer(FitsOnTheLine) {}",
6255                getStyleWithColumns(Style, 43));
6256 
6257   verifyFormat("template <typename T>\n"
6258                "Constructor() : Initializer(FitsOnTheLine) {}",
6259                getStyleWithColumns(Style, 50));
6260   Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
6261   verifyFormat(
6262       "SomeClass::Constructor() :\n"
6263       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
6264       Style);
6265 
6266   Style.ConstructorInitializerAllOnOneLineOrOnePerLine = false;
6267   verifyFormat(
6268       "SomeClass::Constructor() :\n"
6269       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
6270       Style);
6271 
6272   verifyFormat(
6273       "SomeClass::Constructor() :\n"
6274       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6275       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
6276       Style);
6277   verifyFormat(
6278       "SomeClass::Constructor() :\n"
6279       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6280       "    aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
6281       Style);
6282   verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6283                "            aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
6284                "    aaaaaaaaaa(aaaaaa) {}",
6285                Style);
6286 
6287   verifyFormat("Constructor() :\n"
6288                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6289                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6290                "                             aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6291                "    aaaaaaaaaaaaaaaaaaaaaaa() {}",
6292                Style);
6293 
6294   verifyFormat("Constructor() :\n"
6295                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6296                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
6297                Style);
6298 
6299   verifyFormat("Constructor(int Parameter = 0) :\n"
6300                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
6301                "    aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}",
6302                Style);
6303   verifyFormat("Constructor() :\n"
6304                "    aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
6305                "}",
6306                getStyleWithColumns(Style, 60));
6307   verifyFormat("Constructor() :\n"
6308                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6309                "        aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}",
6310                Style);
6311 
6312   // Here a line could be saved by splitting the second initializer onto two
6313   // lines, but that is not desirable.
6314   verifyFormat("Constructor() :\n"
6315                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
6316                "    aaaaaaaaaaa(aaaaaaaaaaa),\n"
6317                "    aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
6318                Style);
6319 
6320   FormatStyle OnePerLine = Style;
6321   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
6322   OnePerLine.AllowAllConstructorInitializersOnNextLine = false;
6323   verifyFormat("SomeClass::Constructor() :\n"
6324                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6325                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6326                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
6327                OnePerLine);
6328   verifyFormat("SomeClass::Constructor() :\n"
6329                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
6330                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6331                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
6332                OnePerLine);
6333   verifyFormat("MyClass::MyClass(int var) :\n"
6334                "    some_var_(var),            // 4 space indent\n"
6335                "    some_other_var_(var + 1) { // lined up\n"
6336                "}",
6337                OnePerLine);
6338   verifyFormat("Constructor() :\n"
6339                "    aaaaa(aaaaaa),\n"
6340                "    aaaaa(aaaaaa),\n"
6341                "    aaaaa(aaaaaa),\n"
6342                "    aaaaa(aaaaaa),\n"
6343                "    aaaaa(aaaaaa) {}",
6344                OnePerLine);
6345   verifyFormat("Constructor() :\n"
6346                "    aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
6347                "          aaaaaaaaaaaaaaaaaaaaaa) {}",
6348                OnePerLine);
6349   OnePerLine.BinPackParameters = false;
6350   verifyFormat("Constructor() :\n"
6351                "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
6352                "        aaaaaaaaaaa().aaa(),\n"
6353                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
6354                OnePerLine);
6355   OnePerLine.ColumnLimit = 60;
6356   verifyFormat("Constructor() :\n"
6357                "    aaaaaaaaaaaaaaaaaaaa(a),\n"
6358                "    bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
6359                OnePerLine);
6360 
6361   EXPECT_EQ("Constructor() :\n"
6362             "    // Comment forcing unwanted break.\n"
6363             "    aaaa(aaaa) {}",
6364             format("Constructor() :\n"
6365                    "    // Comment forcing unwanted break.\n"
6366                    "    aaaa(aaaa) {}",
6367                    Style));
6368 
6369   Style.ColumnLimit = 0;
6370   verifyFormat("SomeClass::Constructor() :\n"
6371                "    a(a) {}",
6372                Style);
6373   verifyFormat("SomeClass::Constructor() noexcept :\n"
6374                "    a(a) {}",
6375                Style);
6376   verifyFormat("SomeClass::Constructor() :\n"
6377                "    a(a), b(b), c(c) {}",
6378                Style);
6379   verifyFormat("SomeClass::Constructor() :\n"
6380                "    a(a) {\n"
6381                "  foo();\n"
6382                "  bar();\n"
6383                "}",
6384                Style);
6385 
6386   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
6387   verifyFormat("SomeClass::Constructor() :\n"
6388                "    a(a), b(b), c(c) {\n"
6389                "}",
6390                Style);
6391   verifyFormat("SomeClass::Constructor() :\n"
6392                "    a(a) {\n"
6393                "}",
6394                Style);
6395 
6396   Style.ColumnLimit = 80;
6397   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
6398   Style.ConstructorInitializerIndentWidth = 2;
6399   verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}", Style);
6400   verifyFormat("SomeClass::Constructor() :\n"
6401                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6402                "  bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}",
6403                Style);
6404 
6405   // `ConstructorInitializerIndentWidth` actually applies to InheritanceList as
6406   // well
6407   Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
6408   verifyFormat(
6409       "class SomeClass\n"
6410       "  : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6411       "    public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
6412       Style);
6413   Style.BreakInheritanceList = FormatStyle::BILS_BeforeComma;
6414   verifyFormat(
6415       "class SomeClass\n"
6416       "  : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6417       "  , public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
6418       Style);
6419   Style.BreakInheritanceList = FormatStyle::BILS_AfterColon;
6420   verifyFormat(
6421       "class SomeClass :\n"
6422       "  public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6423       "  public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
6424       Style);
6425   Style.BreakInheritanceList = FormatStyle::BILS_AfterComma;
6426   verifyFormat(
6427       "class SomeClass\n"
6428       "  : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6429       "    public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
6430       Style);
6431 }
6432 
6433 #ifndef EXPENSIVE_CHECKS
6434 // Expensive checks enables libstdc++ checking which includes validating the
6435 // state of ranges used in std::priority_queue - this blows out the
6436 // runtime/scalability of the function and makes this test unacceptably slow.
6437 TEST_F(FormatTest, MemoizationTests) {
6438   // This breaks if the memoization lookup does not take \c Indent and
6439   // \c LastSpace into account.
6440   verifyFormat(
6441       "extern CFRunLoopTimerRef\n"
6442       "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
6443       "                     CFTimeInterval interval, CFOptionFlags flags,\n"
6444       "                     CFIndex order, CFRunLoopTimerCallBack callout,\n"
6445       "                     CFRunLoopTimerContext *context) {}");
6446 
6447   // Deep nesting somewhat works around our memoization.
6448   verifyFormat(
6449       "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
6450       "    aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
6451       "        aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
6452       "            aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
6453       "                aaaaa())))))))))))))))))))))))))))))))))))))));",
6454       getLLVMStyleWithColumns(65));
6455   verifyFormat(
6456       "aaaaa(\n"
6457       "    aaaaa,\n"
6458       "    aaaaa(\n"
6459       "        aaaaa,\n"
6460       "        aaaaa(\n"
6461       "            aaaaa,\n"
6462       "            aaaaa(\n"
6463       "                aaaaa,\n"
6464       "                aaaaa(\n"
6465       "                    aaaaa,\n"
6466       "                    aaaaa(\n"
6467       "                        aaaaa,\n"
6468       "                        aaaaa(\n"
6469       "                            aaaaa,\n"
6470       "                            aaaaa(\n"
6471       "                                aaaaa,\n"
6472       "                                aaaaa(\n"
6473       "                                    aaaaa,\n"
6474       "                                    aaaaa(\n"
6475       "                                        aaaaa,\n"
6476       "                                        aaaaa(\n"
6477       "                                            aaaaa,\n"
6478       "                                            aaaaa(\n"
6479       "                                                aaaaa,\n"
6480       "                                                aaaaa))))))))))));",
6481       getLLVMStyleWithColumns(65));
6482   verifyFormat(
6483       "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"
6484       "                                  a),\n"
6485       "                                a),\n"
6486       "                              a),\n"
6487       "                            a),\n"
6488       "                          a),\n"
6489       "                        a),\n"
6490       "                      a),\n"
6491       "                    a),\n"
6492       "                  a),\n"
6493       "                a),\n"
6494       "              a),\n"
6495       "            a),\n"
6496       "          a),\n"
6497       "        a),\n"
6498       "      a),\n"
6499       "    a),\n"
6500       "  a)",
6501       getLLVMStyleWithColumns(65));
6502 
6503   // This test takes VERY long when memoization is broken.
6504   FormatStyle OnePerLine = getLLVMStyle();
6505   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
6506   OnePerLine.BinPackParameters = false;
6507   std::string input = "Constructor()\n"
6508                       "    : aaaa(a,\n";
6509   for (unsigned i = 0, e = 80; i != e; ++i) {
6510     input += "           a,\n";
6511   }
6512   input += "           a) {}";
6513   verifyFormat(input, OnePerLine);
6514 }
6515 #endif
6516 
6517 TEST_F(FormatTest, BreaksAsHighAsPossible) {
6518   verifyFormat(
6519       "void f() {\n"
6520       "  if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
6521       "      (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
6522       "    f();\n"
6523       "}");
6524   verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
6525                "    Intervals[i - 1].getRange().getLast()) {\n}");
6526 }
6527 
6528 TEST_F(FormatTest, BreaksFunctionDeclarations) {
6529   // Principially, we break function declarations in a certain order:
6530   // 1) break amongst arguments.
6531   verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
6532                "                              Cccccccccccccc cccccccccccccc);");
6533   verifyFormat("template <class TemplateIt>\n"
6534                "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
6535                "                            TemplateIt *stop) {}");
6536 
6537   // 2) break after return type.
6538   verifyFormat(
6539       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6540       "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);",
6541       getGoogleStyle());
6542 
6543   // 3) break after (.
6544   verifyFormat(
6545       "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
6546       "    Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);",
6547       getGoogleStyle());
6548 
6549   // 4) break before after nested name specifiers.
6550   verifyFormat(
6551       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6552       "SomeClasssssssssssssssssssssssssssssssssssssss::\n"
6553       "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);",
6554       getGoogleStyle());
6555 
6556   // However, there are exceptions, if a sufficient amount of lines can be
6557   // saved.
6558   // FIXME: The precise cut-offs wrt. the number of saved lines might need some
6559   // more adjusting.
6560   verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
6561                "                                  Cccccccccccccc cccccccccc,\n"
6562                "                                  Cccccccccccccc cccccccccc,\n"
6563                "                                  Cccccccccccccc cccccccccc,\n"
6564                "                                  Cccccccccccccc cccccccccc);");
6565   verifyFormat(
6566       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6567       "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
6568       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
6569       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);",
6570       getGoogleStyle());
6571   verifyFormat(
6572       "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
6573       "                                          Cccccccccccccc cccccccccc,\n"
6574       "                                          Cccccccccccccc cccccccccc,\n"
6575       "                                          Cccccccccccccc cccccccccc,\n"
6576       "                                          Cccccccccccccc cccccccccc,\n"
6577       "                                          Cccccccccccccc cccccccccc,\n"
6578       "                                          Cccccccccccccc cccccccccc);");
6579   verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
6580                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
6581                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
6582                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
6583                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
6584 
6585   // Break after multi-line parameters.
6586   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6587                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6588                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6589                "    bbbb bbbb);");
6590   verifyFormat("void SomeLoooooooooooongFunction(\n"
6591                "    std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
6592                "        aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6593                "    int bbbbbbbbbbbbb);");
6594 
6595   // Treat overloaded operators like other functions.
6596   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
6597                "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
6598   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
6599                "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
6600   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
6601                "operator<<(const SomeLooooooooooooooooooooooooogType &other);");
6602   verifyGoogleFormat(
6603       "SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
6604       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
6605   verifyGoogleFormat(
6606       "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
6607       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
6608   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6609                "    int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
6610   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
6611                "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
6612   verifyGoogleFormat(
6613       "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n"
6614       "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6615       "    bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}");
6616   verifyGoogleFormat("template <typename T>\n"
6617                      "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6618                      "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n"
6619                      "    aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);");
6620 
6621   FormatStyle Style = getLLVMStyle();
6622   Style.PointerAlignment = FormatStyle::PAS_Left;
6623   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6624                "    aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}",
6625                Style);
6626   verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
6627                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
6628                Style);
6629 }
6630 
6631 TEST_F(FormatTest, DontBreakBeforeQualifiedOperator) {
6632   // Regression test for https://bugs.llvm.org/show_bug.cgi?id=40516:
6633   // Prefer keeping `::` followed by `operator` together.
6634   EXPECT_EQ("const aaaa::bbbbbbb &\n"
6635             "ccccccccc::operator++() {\n"
6636             "  stuff();\n"
6637             "}",
6638             format("const aaaa::bbbbbbb\n"
6639                    "&ccccccccc::operator++() { stuff(); }",
6640                    getLLVMStyleWithColumns(40)));
6641 }
6642 
6643 TEST_F(FormatTest, TrailingReturnType) {
6644   verifyFormat("auto foo() -> int;\n");
6645   // correct trailing return type spacing
6646   verifyFormat("auto operator->() -> int;\n");
6647   verifyFormat("auto operator++(int) -> int;\n");
6648 
6649   verifyFormat("struct S {\n"
6650                "  auto bar() const -> int;\n"
6651                "};");
6652   verifyFormat("template <size_t Order, typename T>\n"
6653                "auto load_img(const std::string &filename)\n"
6654                "    -> alias::tensor<Order, T, mem::tag::cpu> {}");
6655   verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n"
6656                "    -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}");
6657   verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}");
6658   verifyFormat("template <typename T>\n"
6659                "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n"
6660                "    -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());");
6661 
6662   // Not trailing return types.
6663   verifyFormat("void f() { auto a = b->c(); }");
6664 }
6665 
6666 TEST_F(FormatTest, DeductionGuides) {
6667   verifyFormat("template <class T> A(const T &, const T &) -> A<T &>;");
6668   verifyFormat("template <class T> explicit A(T &, T &&) -> A<T>;");
6669   verifyFormat("template <class... Ts> S(Ts...) -> S<Ts...>;");
6670   verifyFormat(
6671       "template <class... T>\n"
6672       "array(T &&...t) -> array<std::common_type_t<T...>, sizeof...(T)>;");
6673   verifyFormat("template <class T> A() -> A<decltype(p->foo<3>())>;");
6674   verifyFormat("template <class T> A() -> A<decltype(foo<traits<1>>)>;");
6675   verifyFormat("template <class T> A() -> A<sizeof(p->foo<1>)>;");
6676   verifyFormat("template <class T> A() -> A<(3 < 2)>;");
6677   verifyFormat("template <class T> A() -> A<((3) < (2))>;");
6678   verifyFormat("template <class T> x() -> x<1>;");
6679   verifyFormat("template <class T> explicit x(T &) -> x<1>;");
6680 
6681   // Ensure not deduction guides.
6682   verifyFormat("c()->f<int>();");
6683   verifyFormat("x()->foo<1>;");
6684   verifyFormat("x = p->foo<3>();");
6685   verifyFormat("x()->x<1>();");
6686   verifyFormat("x()->x<1>;");
6687 }
6688 
6689 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
6690   // Avoid breaking before trailing 'const' or other trailing annotations, if
6691   // they are not function-like.
6692   FormatStyle Style = getGoogleStyle();
6693   Style.ColumnLimit = 47;
6694   verifyFormat("void someLongFunction(\n"
6695                "    int someLoooooooooooooongParameter) const {\n}",
6696                getLLVMStyleWithColumns(47));
6697   verifyFormat("LoooooongReturnType\n"
6698                "someLoooooooongFunction() const {}",
6699                getLLVMStyleWithColumns(47));
6700   verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
6701                "    const {}",
6702                Style);
6703   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
6704                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
6705   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
6706                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
6707   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
6708                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
6709   verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
6710                "                   aaaaaaaaaaa aaaaa) const override;");
6711   verifyGoogleFormat(
6712       "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
6713       "    const override;");
6714 
6715   // Even if the first parameter has to be wrapped.
6716   verifyFormat("void someLongFunction(\n"
6717                "    int someLongParameter) const {}",
6718                getLLVMStyleWithColumns(46));
6719   verifyFormat("void someLongFunction(\n"
6720                "    int someLongParameter) const {}",
6721                Style);
6722   verifyFormat("void someLongFunction(\n"
6723                "    int someLongParameter) override {}",
6724                Style);
6725   verifyFormat("void someLongFunction(\n"
6726                "    int someLongParameter) OVERRIDE {}",
6727                Style);
6728   verifyFormat("void someLongFunction(\n"
6729                "    int someLongParameter) final {}",
6730                Style);
6731   verifyFormat("void someLongFunction(\n"
6732                "    int someLongParameter) FINAL {}",
6733                Style);
6734   verifyFormat("void someLongFunction(\n"
6735                "    int parameter) const override {}",
6736                Style);
6737 
6738   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
6739   verifyFormat("void someLongFunction(\n"
6740                "    int someLongParameter) const\n"
6741                "{\n"
6742                "}",
6743                Style);
6744 
6745   Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
6746   verifyFormat("void someLongFunction(\n"
6747                "    int someLongParameter) const\n"
6748                "  {\n"
6749                "  }",
6750                Style);
6751 
6752   // Unless these are unknown annotations.
6753   verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
6754                "                  aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
6755                "    LONG_AND_UGLY_ANNOTATION;");
6756 
6757   // Breaking before function-like trailing annotations is fine to keep them
6758   // close to their arguments.
6759   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
6760                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
6761   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
6762                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
6763   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
6764                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
6765   verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n"
6766                      "    AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);");
6767   verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});");
6768 
6769   verifyFormat(
6770       "void aaaaaaaaaaaaaaaaaa()\n"
6771       "    __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
6772       "                   aaaaaaaaaaaaaaaaaaaaaaaaa));");
6773   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6774                "    __attribute__((unused));");
6775   verifyGoogleFormat(
6776       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6777       "    GUARDED_BY(aaaaaaaaaaaa);");
6778   verifyGoogleFormat(
6779       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6780       "    GUARDED_BY(aaaaaaaaaaaa);");
6781   verifyGoogleFormat(
6782       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
6783       "    aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
6784   verifyGoogleFormat(
6785       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
6786       "    aaaaaaaaaaaaaaaaaaaaaaaaa;");
6787 }
6788 
6789 TEST_F(FormatTest, FunctionAnnotations) {
6790   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
6791                "int OldFunction(const string &parameter) {}");
6792   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
6793                "string OldFunction(const string &parameter) {}");
6794   verifyFormat("template <typename T>\n"
6795                "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
6796                "string OldFunction(const string &parameter) {}");
6797 
6798   // Not function annotations.
6799   verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6800                "                << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
6801   verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n"
6802                "       ThisIsATestWithAReallyReallyReallyReallyLongName) {}");
6803   verifyFormat("MACRO(abc).function() // wrap\n"
6804                "    << abc;");
6805   verifyFormat("MACRO(abc)->function() // wrap\n"
6806                "    << abc;");
6807   verifyFormat("MACRO(abc)::function() // wrap\n"
6808                "    << abc;");
6809 }
6810 
6811 TEST_F(FormatTest, BreaksDesireably) {
6812   verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
6813                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
6814                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
6815   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6816                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
6817                "}");
6818 
6819   verifyFormat(
6820       "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6821       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
6822 
6823   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6824                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6825                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
6826 
6827   verifyFormat(
6828       "aaaaaaaa(aaaaaaaaaaaaa,\n"
6829       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6830       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
6831       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6832       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
6833 
6834   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
6835                "    (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6836 
6837   verifyFormat(
6838       "void f() {\n"
6839       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
6840       "                                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
6841       "}");
6842   verifyFormat(
6843       "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6844       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
6845   verifyFormat(
6846       "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6847       "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
6848   verifyFormat(
6849       "aaaaaa(aaa,\n"
6850       "       new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6851       "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6852       "       aaaa);");
6853   verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
6854                "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6855                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6856 
6857   // Indent consistently independent of call expression and unary operator.
6858   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
6859                "    dddddddddddddddddddddddddddddd));");
6860   verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
6861                "    dddddddddddddddddddddddddddddd));");
6862   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
6863                "    dddddddddddddddddddddddddddddd));");
6864 
6865   // This test case breaks on an incorrect memoization, i.e. an optimization not
6866   // taking into account the StopAt value.
6867   verifyFormat(
6868       "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
6869       "       aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
6870       "       aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
6871       "       (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6872 
6873   verifyFormat("{\n  {\n    {\n"
6874                "      Annotation.SpaceRequiredBefore =\n"
6875                "          Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
6876                "          Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
6877                "    }\n  }\n}");
6878 
6879   // Break on an outer level if there was a break on an inner level.
6880   EXPECT_EQ("f(g(h(a, // comment\n"
6881             "      b, c),\n"
6882             "    d, e),\n"
6883             "  x, y);",
6884             format("f(g(h(a, // comment\n"
6885                    "    b, c), d, e), x, y);"));
6886 
6887   // Prefer breaking similar line breaks.
6888   verifyFormat(
6889       "const int kTrackingOptions = NSTrackingMouseMoved |\n"
6890       "                             NSTrackingMouseEnteredAndExited |\n"
6891       "                             NSTrackingActiveAlways;");
6892 }
6893 
6894 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) {
6895   FormatStyle NoBinPacking = getGoogleStyle();
6896   NoBinPacking.BinPackParameters = false;
6897   NoBinPacking.BinPackArguments = true;
6898   verifyFormat("void f() {\n"
6899                "  f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n"
6900                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
6901                "}",
6902                NoBinPacking);
6903   verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n"
6904                "       int aaaaaaaaaaaaaaaaaaaa,\n"
6905                "       int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
6906                NoBinPacking);
6907 
6908   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
6909   verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6910                "                        vector<int> bbbbbbbbbbbbbbb);",
6911                NoBinPacking);
6912   // FIXME: This behavior difference is probably not wanted. However, currently
6913   // we cannot distinguish BreakBeforeParameter being set because of the wrapped
6914   // template arguments from BreakBeforeParameter being set because of the
6915   // one-per-line formatting.
6916   verifyFormat(
6917       "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n"
6918       "                                             aaaaaaaaaa> aaaaaaaaaa);",
6919       NoBinPacking);
6920   verifyFormat(
6921       "void fffffffffff(\n"
6922       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n"
6923       "        aaaaaaaaaa);");
6924 }
6925 
6926 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
6927   FormatStyle NoBinPacking = getGoogleStyle();
6928   NoBinPacking.BinPackParameters = false;
6929   NoBinPacking.BinPackArguments = false;
6930   verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
6931                "  aaaaaaaaaaaaaaaaaaaa,\n"
6932                "  aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
6933                NoBinPacking);
6934   verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
6935                "        aaaaaaaaaaaaa,\n"
6936                "        aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
6937                NoBinPacking);
6938   verifyFormat(
6939       "aaaaaaaa(aaaaaaaaaaaaa,\n"
6940       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6941       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
6942       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6943       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
6944       NoBinPacking);
6945   verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
6946                "    .aaaaaaaaaaaaaaaaaa();",
6947                NoBinPacking);
6948   verifyFormat("void f() {\n"
6949                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6950                "      aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n"
6951                "}",
6952                NoBinPacking);
6953 
6954   verifyFormat(
6955       "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6956       "             aaaaaaaaaaaa,\n"
6957       "             aaaaaaaaaaaa);",
6958       NoBinPacking);
6959   verifyFormat(
6960       "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
6961       "                               ddddddddddddddddddddddddddddd),\n"
6962       "             test);",
6963       NoBinPacking);
6964 
6965   verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
6966                "            aaaaaaaaaaaaaaaaaaaaaaa,\n"
6967                "            aaaaaaaaaaaaaaaaaaaaaaa>\n"
6968                "    aaaaaaaaaaaaaaaaaa;",
6969                NoBinPacking);
6970   verifyFormat("a(\"a\"\n"
6971                "  \"a\",\n"
6972                "  a);");
6973 
6974   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
6975   verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
6976                "                aaaaaaaaa,\n"
6977                "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
6978                NoBinPacking);
6979   verifyFormat(
6980       "void f() {\n"
6981       "  aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
6982       "      .aaaaaaa();\n"
6983       "}",
6984       NoBinPacking);
6985   verifyFormat(
6986       "template <class SomeType, class SomeOtherType>\n"
6987       "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
6988       NoBinPacking);
6989 }
6990 
6991 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
6992   FormatStyle Style = getLLVMStyleWithColumns(15);
6993   Style.ExperimentalAutoDetectBinPacking = true;
6994   EXPECT_EQ("aaa(aaaa,\n"
6995             "    aaaa,\n"
6996             "    aaaa);\n"
6997             "aaa(aaaa,\n"
6998             "    aaaa,\n"
6999             "    aaaa);",
7000             format("aaa(aaaa,\n" // one-per-line
7001                    "  aaaa,\n"
7002                    "    aaaa  );\n"
7003                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
7004                    Style));
7005   EXPECT_EQ("aaa(aaaa, aaaa,\n"
7006             "    aaaa);\n"
7007             "aaa(aaaa, aaaa,\n"
7008             "    aaaa);",
7009             format("aaa(aaaa,  aaaa,\n" // bin-packed
7010                    "    aaaa  );\n"
7011                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
7012                    Style));
7013 }
7014 
7015 TEST_F(FormatTest, FormatsBuilderPattern) {
7016   verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n"
7017                "    .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
7018                "    .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
7019                "    .StartsWith(\".init\", ORDER_INIT)\n"
7020                "    .StartsWith(\".fini\", ORDER_FINI)\n"
7021                "    .StartsWith(\".hash\", ORDER_HASH)\n"
7022                "    .Default(ORDER_TEXT);\n");
7023 
7024   verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
7025                "       aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
7026   verifyFormat("aaaaaaa->aaaaaaa\n"
7027                "    ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7028                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7029                "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
7030   verifyFormat(
7031       "aaaaaaa->aaaaaaa\n"
7032       "    ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7033       "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
7034   verifyFormat(
7035       "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
7036       "    aaaaaaaaaaaaaa);");
7037   verifyFormat(
7038       "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
7039       "    aaaaaa->aaaaaaaaaaaa()\n"
7040       "        ->aaaaaaaaaaaaaaaa(\n"
7041       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7042       "        ->aaaaaaaaaaaaaaaaa();");
7043   verifyGoogleFormat(
7044       "void f() {\n"
7045       "  someo->Add((new util::filetools::Handler(dir))\n"
7046       "                 ->OnEvent1(NewPermanentCallback(\n"
7047       "                     this, &HandlerHolderClass::EventHandlerCBA))\n"
7048       "                 ->OnEvent2(NewPermanentCallback(\n"
7049       "                     this, &HandlerHolderClass::EventHandlerCBB))\n"
7050       "                 ->OnEvent3(NewPermanentCallback(\n"
7051       "                     this, &HandlerHolderClass::EventHandlerCBC))\n"
7052       "                 ->OnEvent5(NewPermanentCallback(\n"
7053       "                     this, &HandlerHolderClass::EventHandlerCBD))\n"
7054       "                 ->OnEvent6(NewPermanentCallback(\n"
7055       "                     this, &HandlerHolderClass::EventHandlerCBE)));\n"
7056       "}");
7057 
7058   verifyFormat(
7059       "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
7060   verifyFormat("aaaaaaaaaaaaaaa()\n"
7061                "    .aaaaaaaaaaaaaaa()\n"
7062                "    .aaaaaaaaaaaaaaa()\n"
7063                "    .aaaaaaaaaaaaaaa()\n"
7064                "    .aaaaaaaaaaaaaaa();");
7065   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
7066                "    .aaaaaaaaaaaaaaa()\n"
7067                "    .aaaaaaaaaaaaaaa()\n"
7068                "    .aaaaaaaaaaaaaaa();");
7069   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
7070                "    .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
7071                "    .aaaaaaaaaaaaaaa();");
7072   verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
7073                "    ->aaaaaaaaaaaaaae(0)\n"
7074                "    ->aaaaaaaaaaaaaaa();");
7075 
7076   // Don't linewrap after very short segments.
7077   verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7078                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7079                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
7080   verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7081                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7082                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
7083   verifyFormat("aaa()\n"
7084                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7085                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7086                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
7087 
7088   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
7089                "    .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7090                "    .has<bbbbbbbbbbbbbbbbbbbbb>();");
7091   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
7092                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
7093                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
7094 
7095   // Prefer not to break after empty parentheses.
7096   verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
7097                "    First->LastNewlineOffset);");
7098 
7099   // Prefer not to create "hanging" indents.
7100   verifyFormat(
7101       "return !soooooooooooooome_map\n"
7102       "            .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7103       "            .second;");
7104   verifyFormat(
7105       "return aaaaaaaaaaaaaaaa\n"
7106       "    .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n"
7107       "    .aaaa(aaaaaaaaaaaaaa);");
7108   // No hanging indent here.
7109   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n"
7110                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7111   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n"
7112                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7113   verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
7114                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7115                getLLVMStyleWithColumns(60));
7116   verifyFormat("aaaaaaaaaaaaaaaaaa\n"
7117                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
7118                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7119                getLLVMStyleWithColumns(59));
7120   verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7121                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7122                "    .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7123 
7124   // Dont break if only closing statements before member call
7125   verifyFormat("test() {\n"
7126                "  ([]() -> {\n"
7127                "    int b = 32;\n"
7128                "    return 3;\n"
7129                "  }).foo();\n"
7130                "}");
7131   verifyFormat("test() {\n"
7132                "  (\n"
7133                "      []() -> {\n"
7134                "        int b = 32;\n"
7135                "        return 3;\n"
7136                "      },\n"
7137                "      foo, bar)\n"
7138                "      .foo();\n"
7139                "}");
7140   verifyFormat("test() {\n"
7141                "  ([]() -> {\n"
7142                "    int b = 32;\n"
7143                "    return 3;\n"
7144                "  })\n"
7145                "      .foo()\n"
7146                "      .bar();\n"
7147                "}");
7148   verifyFormat("test() {\n"
7149                "  ([]() -> {\n"
7150                "    int b = 32;\n"
7151                "    return 3;\n"
7152                "  })\n"
7153                "      .foo(\"aaaaaaaaaaaaaaaaa\"\n"
7154                "           \"bbbb\");\n"
7155                "}",
7156                getLLVMStyleWithColumns(30));
7157 }
7158 
7159 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
7160   verifyFormat(
7161       "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
7162       "    bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
7163   verifyFormat(
7164       "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n"
7165       "    bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}");
7166 
7167   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
7168                "    ccccccccccccccccccccccccc) {\n}");
7169   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n"
7170                "    ccccccccccccccccccccccccc) {\n}");
7171 
7172   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
7173                "    ccccccccccccccccccccccccc) {\n}");
7174   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n"
7175                "    ccccccccccccccccccccccccc) {\n}");
7176 
7177   verifyFormat(
7178       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
7179       "    ccccccccccccccccccccccccc) {\n}");
7180   verifyFormat(
7181       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n"
7182       "    ccccccccccccccccccccccccc) {\n}");
7183 
7184   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
7185                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
7186                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
7187                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
7188   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n"
7189                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n"
7190                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n"
7191                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
7192 
7193   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
7194                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
7195                "    aaaaaaaaaaaaaaa != aa) {\n}");
7196   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n"
7197                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n"
7198                "    aaaaaaaaaaaaaaa != aa) {\n}");
7199 }
7200 
7201 TEST_F(FormatTest, BreaksAfterAssignments) {
7202   verifyFormat(
7203       "unsigned Cost =\n"
7204       "    TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
7205       "                        SI->getPointerAddressSpaceee());\n");
7206   verifyFormat(
7207       "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
7208       "    Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
7209 
7210   verifyFormat(
7211       "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
7212       "    aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
7213   verifyFormat("unsigned OriginalStartColumn =\n"
7214                "    SourceMgr.getSpellingColumnNumber(\n"
7215                "        Current.FormatTok.getStartOfNonWhitespace()) -\n"
7216                "    1;");
7217 }
7218 
7219 TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) {
7220   FormatStyle Style = getLLVMStyle();
7221   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
7222                "    bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;",
7223                Style);
7224 
7225   Style.PenaltyBreakAssignment = 20;
7226   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
7227                "                                 cccccccccccccccccccccccccc;",
7228                Style);
7229 }
7230 
7231 TEST_F(FormatTest, AlignsAfterAssignments) {
7232   verifyFormat(
7233       "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7234       "             aaaaaaaaaaaaaaaaaaaaaaaaa;");
7235   verifyFormat(
7236       "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7237       "          aaaaaaaaaaaaaaaaaaaaaaaaa;");
7238   verifyFormat(
7239       "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7240       "           aaaaaaaaaaaaaaaaaaaaaaaaa;");
7241   verifyFormat(
7242       "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7243       "              aaaaaaaaaaaaaaaaaaaaaaaaa);");
7244   verifyFormat(
7245       "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
7246       "                                            aaaaaaaaaaaaaaaaaaaaaaaa +\n"
7247       "                                            aaaaaaaaaaaaaaaaaaaaaaaa;");
7248 }
7249 
7250 TEST_F(FormatTest, AlignsAfterReturn) {
7251   verifyFormat(
7252       "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7253       "       aaaaaaaaaaaaaaaaaaaaaaaaa;");
7254   verifyFormat(
7255       "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7256       "        aaaaaaaaaaaaaaaaaaaaaaaaa);");
7257   verifyFormat(
7258       "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
7259       "       aaaaaaaaaaaaaaaaaaaaaa();");
7260   verifyFormat(
7261       "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
7262       "        aaaaaaaaaaaaaaaaaaaaaa());");
7263   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7264                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7265   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7266                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
7267                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
7268   verifyFormat("return\n"
7269                "    // true if code is one of a or b.\n"
7270                "    code == a || code == b;");
7271 }
7272 
7273 TEST_F(FormatTest, AlignsAfterOpenBracket) {
7274   verifyFormat(
7275       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
7276       "                                                aaaaaaaaa aaaaaaa) {}");
7277   verifyFormat(
7278       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
7279       "                                               aaaaaaaaaaa aaaaaaaaa);");
7280   verifyFormat(
7281       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
7282       "                                             aaaaaaaaaaaaaaaaaaaaa));");
7283   FormatStyle Style = getLLVMStyle();
7284   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
7285   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7286                "    aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}",
7287                Style);
7288   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
7289                "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);",
7290                Style);
7291   verifyFormat("SomeLongVariableName->someFunction(\n"
7292                "    foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));",
7293                Style);
7294   verifyFormat(
7295       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
7296       "    aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
7297       Style);
7298   verifyFormat(
7299       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
7300       "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7301       Style);
7302   verifyFormat(
7303       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
7304       "    aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
7305       Style);
7306 
7307   verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n"
7308                "    ccccccc(aaaaaaaaaaaaaaaaa,         //\n"
7309                "        b));",
7310                Style);
7311 
7312   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
7313   Style.BinPackArguments = false;
7314   Style.BinPackParameters = false;
7315   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7316                "    aaaaaaaaaaa aaaaaaaa,\n"
7317                "    aaaaaaaaa aaaaaaa,\n"
7318                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
7319                Style);
7320   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
7321                "    aaaaaaaaaaa aaaaaaaaa,\n"
7322                "    aaaaaaaaaaa aaaaaaaaa,\n"
7323                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7324                Style);
7325   verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n"
7326                "    aaaaaaaaaaaaaaa,\n"
7327                "    aaaaaaaaaaaaaaaaaaaaa,\n"
7328                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
7329                Style);
7330   verifyFormat(
7331       "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n"
7332       "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
7333       Style);
7334   verifyFormat(
7335       "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n"
7336       "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
7337       Style);
7338   verifyFormat(
7339       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
7340       "    aaaaaaaaaaaaaaaaaaaaa(\n"
7341       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n"
7342       "    aaaaaaaaaaaaaaaa);",
7343       Style);
7344   verifyFormat(
7345       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
7346       "    aaaaaaaaaaaaaaaaaaaaa(\n"
7347       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n"
7348       "    aaaaaaaaaaaaaaaa);",
7349       Style);
7350 }
7351 
7352 TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
7353   FormatStyle Style = getLLVMStyleWithColumns(40);
7354   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
7355                "          bbbbbbbbbbbbbbbbbbbbbb);",
7356                Style);
7357   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
7358   Style.AlignOperands = FormatStyle::OAS_DontAlign;
7359   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
7360                "          bbbbbbbbbbbbbbbbbbbbbb);",
7361                Style);
7362   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
7363   Style.AlignOperands = FormatStyle::OAS_Align;
7364   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
7365                "          bbbbbbbbbbbbbbbbbbbbbb);",
7366                Style);
7367   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
7368   Style.AlignOperands = FormatStyle::OAS_DontAlign;
7369   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
7370                "    bbbbbbbbbbbbbbbbbbbbbb);",
7371                Style);
7372 }
7373 
7374 TEST_F(FormatTest, BreaksConditionalExpressions) {
7375   verifyFormat(
7376       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7377       "                               ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7378       "                               : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7379   verifyFormat(
7380       "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
7381       "     aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7382       "                                : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7383   verifyFormat(
7384       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7385       "                                   : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7386   verifyFormat("aaaa(aaaaaaaaa, aaaaaaaaa,\n"
7387                "     aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7388                "             : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7389   verifyFormat(
7390       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
7391       "                                                    : aaaaaaaaaaaaa);");
7392   verifyFormat(
7393       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7394       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7395       "                                    : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7396       "                   aaaaaaaaaaaaa);");
7397   verifyFormat(
7398       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7399       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7400       "                   aaaaaaaaaaaaa);");
7401   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7402                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7403                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7404                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7405                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7406   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7407                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7408                "           ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7409                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7410                "           : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7411                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7412                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7413   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7414                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7415                "           ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7416                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7417                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7418   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7419                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7420                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
7421   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
7422                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7423                "        ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7424                "        : aaaaaaaaaaaaaaaa;");
7425   verifyFormat(
7426       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7427       "    ? aaaaaaaaaaaaaaa\n"
7428       "    : aaaaaaaaaaaaaaa;");
7429   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
7430                "          aaaaaaaaa\n"
7431                "      ? b\n"
7432                "      : c);");
7433   verifyFormat("return aaaa == bbbb\n"
7434                "           // comment\n"
7435                "           ? aaaa\n"
7436                "           : bbbb;");
7437   verifyFormat("unsigned Indent =\n"
7438                "    format(TheLine.First,\n"
7439                "           IndentForLevel[TheLine.Level] >= 0\n"
7440                "               ? IndentForLevel[TheLine.Level]\n"
7441                "               : TheLine * 2,\n"
7442                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
7443                getLLVMStyleWithColumns(60));
7444   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
7445                "                  ? aaaaaaaaaaaaaaa\n"
7446                "                  : bbbbbbbbbbbbbbb //\n"
7447                "                        ? ccccccccccccccc\n"
7448                "                        : ddddddddddddddd;");
7449   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
7450                "                  ? aaaaaaaaaaaaaaa\n"
7451                "                  : (bbbbbbbbbbbbbbb //\n"
7452                "                         ? ccccccccccccccc\n"
7453                "                         : ddddddddddddddd);");
7454   verifyFormat(
7455       "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7456       "                                      ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7457       "                                            aaaaaaaaaaaaaaaaaaaaa +\n"
7458       "                                            aaaaaaaaaaaaaaaaaaaaa\n"
7459       "                                      : aaaaaaaaaa;");
7460   verifyFormat(
7461       "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7462       "                                   : aaaaaaaaaaaaaaaaaaaaaa\n"
7463       "                      : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
7464 
7465   FormatStyle NoBinPacking = getLLVMStyle();
7466   NoBinPacking.BinPackArguments = false;
7467   verifyFormat(
7468       "void f() {\n"
7469       "  g(aaa,\n"
7470       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
7471       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7472       "        ? aaaaaaaaaaaaaaa\n"
7473       "        : aaaaaaaaaaaaaaa);\n"
7474       "}",
7475       NoBinPacking);
7476   verifyFormat(
7477       "void f() {\n"
7478       "  g(aaa,\n"
7479       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
7480       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7481       "        ?: aaaaaaaaaaaaaaa);\n"
7482       "}",
7483       NoBinPacking);
7484 
7485   verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n"
7486                "             // comment.\n"
7487                "             ccccccccccccccccccccccccccccccccccccccc\n"
7488                "                 ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7489                "                 : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);");
7490 
7491   // Assignments in conditional expressions. Apparently not uncommon :-(.
7492   verifyFormat("return a != b\n"
7493                "           // comment\n"
7494                "           ? a = b\n"
7495                "           : a = b;");
7496   verifyFormat("return a != b\n"
7497                "           // comment\n"
7498                "           ? a = a != b\n"
7499                "                     // comment\n"
7500                "                     ? a = b\n"
7501                "                     : a\n"
7502                "           : a;\n");
7503   verifyFormat("return a != b\n"
7504                "           // comment\n"
7505                "           ? a\n"
7506                "           : a = a != b\n"
7507                "                     // comment\n"
7508                "                     ? a = b\n"
7509                "                     : a;");
7510 
7511   // Chained conditionals
7512   FormatStyle Style = getLLVMStyle();
7513   Style.ColumnLimit = 70;
7514   Style.AlignOperands = FormatStyle::OAS_Align;
7515   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7516                "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7517                "                        : 3333333333333333;",
7518                Style);
7519   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7520                "       : bbbbbbbbbb     ? 2222222222222222\n"
7521                "                        : 3333333333333333;",
7522                Style);
7523   verifyFormat("return aaaaaaaaaa         ? 1111111111111111\n"
7524                "       : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
7525                "                          : 3333333333333333;",
7526                Style);
7527   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7528                "       : bbbbbbbbbbbbbb ? 222222\n"
7529                "                        : 333333;",
7530                Style);
7531   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7532                "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7533                "       : cccccccccccccc ? 3333333333333333\n"
7534                "                        : 4444444444444444;",
7535                Style);
7536   verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc)\n"
7537                "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7538                "                        : 3333333333333333;",
7539                Style);
7540   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7541                "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7542                "                        : (aaa ? bbb : ccc);",
7543                Style);
7544   verifyFormat(
7545       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7546       "                                             : cccccccccccccccccc)\n"
7547       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7548       "                        : 3333333333333333;",
7549       Style);
7550   verifyFormat(
7551       "return aaaaaaaaa        ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7552       "                                             : cccccccccccccccccc)\n"
7553       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7554       "                        : 3333333333333333;",
7555       Style);
7556   verifyFormat(
7557       "return aaaaaaaaa        ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7558       "                                             : dddddddddddddddddd)\n"
7559       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7560       "                        : 3333333333333333;",
7561       Style);
7562   verifyFormat(
7563       "return aaaaaaaaa        ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7564       "                                             : dddddddddddddddddd)\n"
7565       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7566       "                        : 3333333333333333;",
7567       Style);
7568   verifyFormat(
7569       "return aaaaaaaaa        ? 1111111111111111\n"
7570       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7571       "                        : a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7572       "                                             : dddddddddddddddddd)\n",
7573       Style);
7574   verifyFormat(
7575       "return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7576       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7577       "                        : (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7578       "                                             : cccccccccccccccccc);",
7579       Style);
7580   verifyFormat(
7581       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7582       "                           : ccccccccccccccc ? dddddddddddddddddd\n"
7583       "                                             : eeeeeeeeeeeeeeeeee)\n"
7584       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7585       "                        : 3333333333333333;",
7586       Style);
7587   verifyFormat(
7588       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa    ? bbbbbbbbbbbbbbbbbb\n"
7589       "                           : ccccccccccccccc ? dddddddddddddddddd\n"
7590       "                                             : eeeeeeeeeeeeeeeeee)\n"
7591       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7592       "                        : 3333333333333333;",
7593       Style);
7594   verifyFormat(
7595       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7596       "                           : cccccccccccc    ? dddddddddddddddddd\n"
7597       "                                             : eeeeeeeeeeeeeeeeee)\n"
7598       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7599       "                        : 3333333333333333;",
7600       Style);
7601   verifyFormat(
7602       "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7603       "                                             : cccccccccccccccccc\n"
7604       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7605       "                        : 3333333333333333;",
7606       Style);
7607   verifyFormat(
7608       "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7609       "                          : cccccccccccccccc ? dddddddddddddddddd\n"
7610       "                                             : eeeeeeeeeeeeeeeeee\n"
7611       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7612       "                        : 3333333333333333;",
7613       Style);
7614   verifyFormat("return aaaaaaaaaaaaaaaaaaaaa\n"
7615                "           ? (aaaaaaaaaaaaaaaaaa   ? bbbbbbbbbbbbbbbbbb\n"
7616                "              : cccccccccccccccccc ? dddddddddddddddddd\n"
7617                "                                   : eeeeeeeeeeeeeeeeee)\n"
7618                "       : bbbbbbbbbbbbbbbbbbb ? 2222222222222222\n"
7619                "                             : 3333333333333333;",
7620                Style);
7621   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaa\n"
7622                "           ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7623                "             : cccccccccccccccc ? dddddddddddddddddd\n"
7624                "                                : eeeeeeeeeeeeeeeeee\n"
7625                "       : bbbbbbbbbbbbbbbbbbbbbbb ? 2222222222222222\n"
7626                "                                 : 3333333333333333;",
7627                Style);
7628 
7629   Style.AlignOperands = FormatStyle::OAS_DontAlign;
7630   Style.BreakBeforeTernaryOperators = false;
7631   // FIXME: Aligning the question marks is weird given DontAlign.
7632   // Consider disabling this alignment in this case. Also check whether this
7633   // will render the adjustment from https://reviews.llvm.org/D82199
7634   // unnecessary.
7635   verifyFormat("int x = aaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa :\n"
7636                "    bbbb                ? cccccccccccccccccc :\n"
7637                "                          ddddd;\n",
7638                Style);
7639 
7640   EXPECT_EQ(
7641       "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n"
7642       "    /*\n"
7643       "     */\n"
7644       "    function() {\n"
7645       "      try {\n"
7646       "        return JJJJJJJJJJJJJJ(\n"
7647       "            pppppppppppppppppppppppppppppppppppppppppppppppppp);\n"
7648       "      }\n"
7649       "    } :\n"
7650       "    function() {};",
7651       format(
7652           "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n"
7653           "     /*\n"
7654           "      */\n"
7655           "     function() {\n"
7656           "      try {\n"
7657           "        return JJJJJJJJJJJJJJ(\n"
7658           "            pppppppppppppppppppppppppppppppppppppppppppppppppp);\n"
7659           "      }\n"
7660           "    } :\n"
7661           "    function() {};",
7662           getGoogleStyle(FormatStyle::LK_JavaScript)));
7663 }
7664 
7665 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
7666   FormatStyle Style = getLLVMStyle();
7667   Style.BreakBeforeTernaryOperators = false;
7668   Style.ColumnLimit = 70;
7669   verifyFormat(
7670       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
7671       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
7672       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7673       Style);
7674   verifyFormat(
7675       "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
7676       "     aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
7677       "                                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7678       Style);
7679   verifyFormat(
7680       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
7681       "                                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7682       Style);
7683   verifyFormat("aaaa(aaaaaaaa, aaaaaaaaaa,\n"
7684                "     aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
7685                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7686                Style);
7687   verifyFormat(
7688       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
7689       "                                                      aaaaaaaaaaaaa);",
7690       Style);
7691   verifyFormat(
7692       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7693       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
7694       "                                      aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7695       "                   aaaaaaaaaaaaa);",
7696       Style);
7697   verifyFormat(
7698       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7699       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7700       "                   aaaaaaaaaaaaa);",
7701       Style);
7702   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
7703                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7704                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
7705                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7706                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7707                Style);
7708   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7709                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
7710                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7711                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
7712                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7713                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7714                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7715                Style);
7716   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7717                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n"
7718                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7719                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7720                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7721                Style);
7722   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
7723                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
7724                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
7725                Style);
7726   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
7727                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
7728                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
7729                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
7730                Style);
7731   verifyFormat(
7732       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
7733       "    aaaaaaaaaaaaaaa :\n"
7734       "    aaaaaaaaaaaaaaa;",
7735       Style);
7736   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
7737                "          aaaaaaaaa ?\n"
7738                "      b :\n"
7739                "      c);",
7740                Style);
7741   verifyFormat("unsigned Indent =\n"
7742                "    format(TheLine.First,\n"
7743                "           IndentForLevel[TheLine.Level] >= 0 ?\n"
7744                "               IndentForLevel[TheLine.Level] :\n"
7745                "               TheLine * 2,\n"
7746                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
7747                Style);
7748   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
7749                "                  aaaaaaaaaaaaaaa :\n"
7750                "                  bbbbbbbbbbbbbbb ? //\n"
7751                "                      ccccccccccccccc :\n"
7752                "                      ddddddddddddddd;",
7753                Style);
7754   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
7755                "                  aaaaaaaaaaaaaaa :\n"
7756                "                  (bbbbbbbbbbbbbbb ? //\n"
7757                "                       ccccccccccccccc :\n"
7758                "                       ddddddddddddddd);",
7759                Style);
7760   verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
7761                "            /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n"
7762                "            ccccccccccccccccccccccccccc;",
7763                Style);
7764   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
7765                "           aaaaa :\n"
7766                "           bbbbbbbbbbbbbbb + cccccccccccccccc;",
7767                Style);
7768 
7769   // Chained conditionals
7770   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
7771                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7772                "                          3333333333333333;",
7773                Style);
7774   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
7775                "       bbbbbbbbbb       ? 2222222222222222 :\n"
7776                "                          3333333333333333;",
7777                Style);
7778   verifyFormat("return aaaaaaaaaa       ? 1111111111111111 :\n"
7779                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7780                "                          3333333333333333;",
7781                Style);
7782   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
7783                "       bbbbbbbbbbbbbbbb ? 222222 :\n"
7784                "                          333333;",
7785                Style);
7786   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
7787                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7788                "       cccccccccccccccc ? 3333333333333333 :\n"
7789                "                          4444444444444444;",
7790                Style);
7791   verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc) :\n"
7792                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7793                "                          3333333333333333;",
7794                Style);
7795   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
7796                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7797                "                          (aaa ? bbb : ccc);",
7798                Style);
7799   verifyFormat(
7800       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7801       "                                               cccccccccccccccccc) :\n"
7802       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7803       "                          3333333333333333;",
7804       Style);
7805   verifyFormat(
7806       "return aaaaaaaaa        ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7807       "                                               cccccccccccccccccc) :\n"
7808       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7809       "                          3333333333333333;",
7810       Style);
7811   verifyFormat(
7812       "return aaaaaaaaa        ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7813       "                                               dddddddddddddddddd) :\n"
7814       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7815       "                          3333333333333333;",
7816       Style);
7817   verifyFormat(
7818       "return aaaaaaaaa        ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7819       "                                               dddddddddddddddddd) :\n"
7820       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7821       "                          3333333333333333;",
7822       Style);
7823   verifyFormat(
7824       "return aaaaaaaaa        ? 1111111111111111 :\n"
7825       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7826       "                          a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7827       "                                               dddddddddddddddddd)\n",
7828       Style);
7829   verifyFormat(
7830       "return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
7831       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7832       "                          (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7833       "                                               cccccccccccccccccc);",
7834       Style);
7835   verifyFormat(
7836       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7837       "                           ccccccccccccccccc ? dddddddddddddddddd :\n"
7838       "                                               eeeeeeeeeeeeeeeeee) :\n"
7839       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7840       "                          3333333333333333;",
7841       Style);
7842   verifyFormat(
7843       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7844       "                           ccccccccccccc     ? dddddddddddddddddd :\n"
7845       "                                               eeeeeeeeeeeeeeeeee) :\n"
7846       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7847       "                          3333333333333333;",
7848       Style);
7849   verifyFormat(
7850       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaa     ? bbbbbbbbbbbbbbbbbb :\n"
7851       "                           ccccccccccccccccc ? dddddddddddddddddd :\n"
7852       "                                               eeeeeeeeeeeeeeeeee) :\n"
7853       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7854       "                          3333333333333333;",
7855       Style);
7856   verifyFormat(
7857       "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7858       "                                               cccccccccccccccccc :\n"
7859       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7860       "                          3333333333333333;",
7861       Style);
7862   verifyFormat(
7863       "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7864       "                          cccccccccccccccccc ? dddddddddddddddddd :\n"
7865       "                                               eeeeeeeeeeeeeeeeee :\n"
7866       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7867       "                          3333333333333333;",
7868       Style);
7869   verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n"
7870                "           (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7871                "            cccccccccccccccccc ? dddddddddddddddddd :\n"
7872                "                                 eeeeeeeeeeeeeeeeee) :\n"
7873                "       bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7874                "                               3333333333333333;",
7875                Style);
7876   verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n"
7877                "           aaaaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7878                "           cccccccccccccccccccc ? dddddddddddddddddd :\n"
7879                "                                  eeeeeeeeeeeeeeeeee :\n"
7880                "       bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7881                "                               3333333333333333;",
7882                Style);
7883 }
7884 
7885 TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
7886   verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
7887                "     aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
7888   verifyFormat("bool a = true, b = false;");
7889 
7890   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
7891                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
7892                "     bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
7893                "         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
7894   verifyFormat(
7895       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
7896       "         bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
7897       "     d = e && f;");
7898   verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
7899                "          c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
7900   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
7901                "          *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
7902   verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
7903                "          ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
7904 
7905   FormatStyle Style = getGoogleStyle();
7906   Style.PointerAlignment = FormatStyle::PAS_Left;
7907   Style.DerivePointerAlignment = false;
7908   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7909                "    *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
7910                "    *b = bbbbbbbbbbbbbbbbbbb;",
7911                Style);
7912   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
7913                "          *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;",
7914                Style);
7915   verifyFormat("vector<int*> a, b;", Style);
7916   verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style);
7917 }
7918 
7919 TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
7920   verifyFormat("arr[foo ? bar : baz];");
7921   verifyFormat("f()[foo ? bar : baz];");
7922   verifyFormat("(a + b)[foo ? bar : baz];");
7923   verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
7924 }
7925 
7926 TEST_F(FormatTest, AlignsStringLiterals) {
7927   verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
7928                "                                      \"short literal\");");
7929   verifyFormat(
7930       "looooooooooooooooooooooooongFunction(\n"
7931       "    \"short literal\"\n"
7932       "    \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
7933   verifyFormat("someFunction(\"Always break between multi-line\"\n"
7934                "             \" string literals\",\n"
7935                "             and, other, parameters);");
7936   EXPECT_EQ("fun + \"1243\" /* comment */\n"
7937             "      \"5678\";",
7938             format("fun + \"1243\" /* comment */\n"
7939                    "    \"5678\";",
7940                    getLLVMStyleWithColumns(28)));
7941   EXPECT_EQ(
7942       "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
7943       "         \"aaaaaaaaaaaaaaaaaaaaa\"\n"
7944       "         \"aaaaaaaaaaaaaaaa\";",
7945       format("aaaaaa ="
7946              "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
7947              "aaaaaaaaaaaaaaaaaaaaa\" "
7948              "\"aaaaaaaaaaaaaaaa\";"));
7949   verifyFormat("a = a + \"a\"\n"
7950                "        \"a\"\n"
7951                "        \"a\";");
7952   verifyFormat("f(\"a\", \"b\"\n"
7953                "       \"c\");");
7954 
7955   verifyFormat(
7956       "#define LL_FORMAT \"ll\"\n"
7957       "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
7958       "       \"d, ddddddddd: %\" LL_FORMAT \"d\");");
7959 
7960   verifyFormat("#define A(X)          \\\n"
7961                "  \"aaaaa\" #X \"bbbbbb\" \\\n"
7962                "  \"ccccc\"",
7963                getLLVMStyleWithColumns(23));
7964   verifyFormat("#define A \"def\"\n"
7965                "f(\"abc\" A \"ghi\"\n"
7966                "  \"jkl\");");
7967 
7968   verifyFormat("f(L\"a\"\n"
7969                "  L\"b\");");
7970   verifyFormat("#define A(X)            \\\n"
7971                "  L\"aaaaa\" #X L\"bbbbbb\" \\\n"
7972                "  L\"ccccc\"",
7973                getLLVMStyleWithColumns(25));
7974 
7975   verifyFormat("f(@\"a\"\n"
7976                "  @\"b\");");
7977   verifyFormat("NSString s = @\"a\"\n"
7978                "             @\"b\"\n"
7979                "             @\"c\";");
7980   verifyFormat("NSString s = @\"a\"\n"
7981                "              \"b\"\n"
7982                "              \"c\";");
7983 }
7984 
7985 TEST_F(FormatTest, ReturnTypeBreakingStyle) {
7986   FormatStyle Style = getLLVMStyle();
7987   // No declarations or definitions should be moved to own line.
7988   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None;
7989   verifyFormat("class A {\n"
7990                "  int f() { return 1; }\n"
7991                "  int g();\n"
7992                "};\n"
7993                "int f() { return 1; }\n"
7994                "int g();\n",
7995                Style);
7996 
7997   // All declarations and definitions should have the return type moved to its
7998   // own line.
7999   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
8000   Style.TypenameMacros = {"LIST"};
8001   verifyFormat("SomeType\n"
8002                "funcdecl(LIST(uint64_t));",
8003                Style);
8004   verifyFormat("class E {\n"
8005                "  int\n"
8006                "  f() {\n"
8007                "    return 1;\n"
8008                "  }\n"
8009                "  int\n"
8010                "  g();\n"
8011                "};\n"
8012                "int\n"
8013                "f() {\n"
8014                "  return 1;\n"
8015                "}\n"
8016                "int\n"
8017                "g();\n",
8018                Style);
8019 
8020   // Top-level definitions, and no kinds of declarations should have the
8021   // return type moved to its own line.
8022   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions;
8023   verifyFormat("class B {\n"
8024                "  int f() { return 1; }\n"
8025                "  int g();\n"
8026                "};\n"
8027                "int\n"
8028                "f() {\n"
8029                "  return 1;\n"
8030                "}\n"
8031                "int g();\n",
8032                Style);
8033 
8034   // Top-level definitions and declarations should have the return type moved
8035   // to its own line.
8036   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel;
8037   verifyFormat("class C {\n"
8038                "  int f() { return 1; }\n"
8039                "  int g();\n"
8040                "};\n"
8041                "int\n"
8042                "f() {\n"
8043                "  return 1;\n"
8044                "}\n"
8045                "int\n"
8046                "g();\n",
8047                Style);
8048 
8049   // All definitions should have the return type moved to its own line, but no
8050   // kinds of declarations.
8051   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
8052   verifyFormat("class D {\n"
8053                "  int\n"
8054                "  f() {\n"
8055                "    return 1;\n"
8056                "  }\n"
8057                "  int g();\n"
8058                "};\n"
8059                "int\n"
8060                "f() {\n"
8061                "  return 1;\n"
8062                "}\n"
8063                "int g();\n",
8064                Style);
8065   verifyFormat("const char *\n"
8066                "f(void) {\n" // Break here.
8067                "  return \"\";\n"
8068                "}\n"
8069                "const char *bar(void);\n", // No break here.
8070                Style);
8071   verifyFormat("template <class T>\n"
8072                "T *\n"
8073                "f(T &c) {\n" // Break here.
8074                "  return NULL;\n"
8075                "}\n"
8076                "template <class T> T *f(T &c);\n", // No break here.
8077                Style);
8078   verifyFormat("class C {\n"
8079                "  int\n"
8080                "  operator+() {\n"
8081                "    return 1;\n"
8082                "  }\n"
8083                "  int\n"
8084                "  operator()() {\n"
8085                "    return 1;\n"
8086                "  }\n"
8087                "};\n",
8088                Style);
8089   verifyFormat("void\n"
8090                "A::operator()() {}\n"
8091                "void\n"
8092                "A::operator>>() {}\n"
8093                "void\n"
8094                "A::operator+() {}\n"
8095                "void\n"
8096                "A::operator*() {}\n"
8097                "void\n"
8098                "A::operator->() {}\n"
8099                "void\n"
8100                "A::operator void *() {}\n"
8101                "void\n"
8102                "A::operator void &() {}\n"
8103                "void\n"
8104                "A::operator void &&() {}\n"
8105                "void\n"
8106                "A::operator char *() {}\n"
8107                "void\n"
8108                "A::operator[]() {}\n"
8109                "void\n"
8110                "A::operator!() {}\n"
8111                "void\n"
8112                "A::operator**() {}\n"
8113                "void\n"
8114                "A::operator<Foo> *() {}\n"
8115                "void\n"
8116                "A::operator<Foo> **() {}\n"
8117                "void\n"
8118                "A::operator<Foo> &() {}\n"
8119                "void\n"
8120                "A::operator void **() {}\n",
8121                Style);
8122   verifyFormat("constexpr auto\n"
8123                "operator()() const -> reference {}\n"
8124                "constexpr auto\n"
8125                "operator>>() const -> reference {}\n"
8126                "constexpr auto\n"
8127                "operator+() const -> reference {}\n"
8128                "constexpr auto\n"
8129                "operator*() const -> reference {}\n"
8130                "constexpr auto\n"
8131                "operator->() const -> reference {}\n"
8132                "constexpr auto\n"
8133                "operator++() const -> reference {}\n"
8134                "constexpr auto\n"
8135                "operator void *() const -> reference {}\n"
8136                "constexpr auto\n"
8137                "operator void **() const -> reference {}\n"
8138                "constexpr auto\n"
8139                "operator void *() const -> reference {}\n"
8140                "constexpr auto\n"
8141                "operator void &() const -> reference {}\n"
8142                "constexpr auto\n"
8143                "operator void &&() const -> reference {}\n"
8144                "constexpr auto\n"
8145                "operator char *() const -> reference {}\n"
8146                "constexpr auto\n"
8147                "operator!() const -> reference {}\n"
8148                "constexpr auto\n"
8149                "operator[]() const -> reference {}\n",
8150                Style);
8151   verifyFormat("void *operator new(std::size_t s);", // No break here.
8152                Style);
8153   verifyFormat("void *\n"
8154                "operator new(std::size_t s) {}",
8155                Style);
8156   verifyFormat("void *\n"
8157                "operator delete[](void *ptr) {}",
8158                Style);
8159   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
8160   verifyFormat("const char *\n"
8161                "f(void)\n" // Break here.
8162                "{\n"
8163                "  return \"\";\n"
8164                "}\n"
8165                "const char *bar(void);\n", // No break here.
8166                Style);
8167   verifyFormat("template <class T>\n"
8168                "T *\n"     // Problem here: no line break
8169                "f(T &c)\n" // Break here.
8170                "{\n"
8171                "  return NULL;\n"
8172                "}\n"
8173                "template <class T> T *f(T &c);\n", // No break here.
8174                Style);
8175   verifyFormat("int\n"
8176                "foo(A<bool> a)\n"
8177                "{\n"
8178                "  return a;\n"
8179                "}\n",
8180                Style);
8181   verifyFormat("int\n"
8182                "foo(A<8> a)\n"
8183                "{\n"
8184                "  return a;\n"
8185                "}\n",
8186                Style);
8187   verifyFormat("int\n"
8188                "foo(A<B<bool>, 8> a)\n"
8189                "{\n"
8190                "  return a;\n"
8191                "}\n",
8192                Style);
8193   verifyFormat("int\n"
8194                "foo(A<B<8>, bool> a)\n"
8195                "{\n"
8196                "  return a;\n"
8197                "}\n",
8198                Style);
8199   verifyFormat("int\n"
8200                "foo(A<B<bool>, bool> a)\n"
8201                "{\n"
8202                "  return a;\n"
8203                "}\n",
8204                Style);
8205   verifyFormat("int\n"
8206                "foo(A<B<8>, 8> a)\n"
8207                "{\n"
8208                "  return a;\n"
8209                "}\n",
8210                Style);
8211 
8212   Style = getGNUStyle();
8213 
8214   // Test for comments at the end of function declarations.
8215   verifyFormat("void\n"
8216                "foo (int a, /*abc*/ int b) // def\n"
8217                "{\n"
8218                "}\n",
8219                Style);
8220 
8221   verifyFormat("void\n"
8222                "foo (int a, /* abc */ int b) /* def */\n"
8223                "{\n"
8224                "}\n",
8225                Style);
8226 
8227   // Definitions that should not break after return type
8228   verifyFormat("void foo (int a, int b); // def\n", Style);
8229   verifyFormat("void foo (int a, int b); /* def */\n", Style);
8230   verifyFormat("void foo (int a, int b);\n", Style);
8231 }
8232 
8233 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
8234   FormatStyle NoBreak = getLLVMStyle();
8235   NoBreak.AlwaysBreakBeforeMultilineStrings = false;
8236   FormatStyle Break = getLLVMStyle();
8237   Break.AlwaysBreakBeforeMultilineStrings = true;
8238   verifyFormat("aaaa = \"bbbb\"\n"
8239                "       \"cccc\";",
8240                NoBreak);
8241   verifyFormat("aaaa =\n"
8242                "    \"bbbb\"\n"
8243                "    \"cccc\";",
8244                Break);
8245   verifyFormat("aaaa(\"bbbb\"\n"
8246                "     \"cccc\");",
8247                NoBreak);
8248   verifyFormat("aaaa(\n"
8249                "    \"bbbb\"\n"
8250                "    \"cccc\");",
8251                Break);
8252   verifyFormat("aaaa(qqq, \"bbbb\"\n"
8253                "          \"cccc\");",
8254                NoBreak);
8255   verifyFormat("aaaa(qqq,\n"
8256                "     \"bbbb\"\n"
8257                "     \"cccc\");",
8258                Break);
8259   verifyFormat("aaaa(qqq,\n"
8260                "     L\"bbbb\"\n"
8261                "     L\"cccc\");",
8262                Break);
8263   verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n"
8264                "                      \"bbbb\"));",
8265                Break);
8266   verifyFormat("string s = someFunction(\n"
8267                "    \"abc\"\n"
8268                "    \"abc\");",
8269                Break);
8270 
8271   // As we break before unary operators, breaking right after them is bad.
8272   verifyFormat("string foo = abc ? \"x\"\n"
8273                "                   \"blah blah blah blah blah blah\"\n"
8274                "                 : \"y\";",
8275                Break);
8276 
8277   // Don't break if there is no column gain.
8278   verifyFormat("f(\"aaaa\"\n"
8279                "  \"bbbb\");",
8280                Break);
8281 
8282   // Treat literals with escaped newlines like multi-line string literals.
8283   EXPECT_EQ("x = \"a\\\n"
8284             "b\\\n"
8285             "c\";",
8286             format("x = \"a\\\n"
8287                    "b\\\n"
8288                    "c\";",
8289                    NoBreak));
8290   EXPECT_EQ("xxxx =\n"
8291             "    \"a\\\n"
8292             "b\\\n"
8293             "c\";",
8294             format("xxxx = \"a\\\n"
8295                    "b\\\n"
8296                    "c\";",
8297                    Break));
8298 
8299   EXPECT_EQ("NSString *const kString =\n"
8300             "    @\"aaaa\"\n"
8301             "    @\"bbbb\";",
8302             format("NSString *const kString = @\"aaaa\"\n"
8303                    "@\"bbbb\";",
8304                    Break));
8305 
8306   Break.ColumnLimit = 0;
8307   verifyFormat("const char *hello = \"hello llvm\";", Break);
8308 }
8309 
8310 TEST_F(FormatTest, AlignsPipes) {
8311   verifyFormat(
8312       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8313       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8314       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8315   verifyFormat(
8316       "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
8317       "                     << aaaaaaaaaaaaaaaaaaaa;");
8318   verifyFormat(
8319       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8320       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8321   verifyFormat(
8322       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
8323       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8324   verifyFormat(
8325       "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
8326       "                \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
8327       "             << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
8328   verifyFormat(
8329       "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8330       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8331       "         << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8332   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8333                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8334                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8335                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
8336   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n"
8337                "             << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);");
8338   verifyFormat(
8339       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8340       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8341   verifyFormat(
8342       "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n"
8343       "                                       aaaaaaaaaaaaaaaaaaaaaaaaaa);");
8344 
8345   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
8346                "             << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
8347   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8348                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8349                "                    aaaaaaaaaaaaaaaaaaaaa)\n"
8350                "             << aaaaaaaaaaaaaaaaaaaaaaaaaa;");
8351   verifyFormat("LOG_IF(aaa == //\n"
8352                "       bbb)\n"
8353                "    << a << b;");
8354 
8355   // But sometimes, breaking before the first "<<" is desirable.
8356   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
8357                "    << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
8358   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
8359                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8360                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8361   verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
8362                "    << BEF << IsTemplate << Description << E->getType();");
8363   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
8364                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8365                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8366   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
8367                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8368                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8369                "    << aaa;");
8370 
8371   verifyFormat(
8372       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8373       "                    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
8374 
8375   // Incomplete string literal.
8376   EXPECT_EQ("llvm::errs() << \"\n"
8377             "             << a;",
8378             format("llvm::errs() << \"\n<<a;"));
8379 
8380   verifyFormat("void f() {\n"
8381                "  CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
8382                "      << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
8383                "}");
8384 
8385   // Handle 'endl'.
8386   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n"
8387                "             << bbbbbbbbbbbbbbbbbbbbbb << endl;");
8388   verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;");
8389 
8390   // Handle '\n'.
8391   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n"
8392                "             << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
8393   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n"
8394                "             << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';");
8395   verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n"
8396                "             << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";");
8397   verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
8398 }
8399 
8400 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) {
8401   verifyFormat("return out << \"somepacket = {\\n\"\n"
8402                "           << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
8403                "           << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
8404                "           << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
8405                "           << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
8406                "           << \"}\";");
8407 
8408   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
8409                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
8410                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
8411   verifyFormat(
8412       "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
8413       "             << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
8414       "             << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
8415       "             << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
8416       "             << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
8417   verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
8418                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
8419   verifyFormat(
8420       "void f() {\n"
8421       "  llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
8422       "               << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
8423       "}");
8424 
8425   // Breaking before the first "<<" is generally not desirable.
8426   verifyFormat(
8427       "llvm::errs()\n"
8428       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8429       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8430       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8431       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
8432       getLLVMStyleWithColumns(70));
8433   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
8434                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8435                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
8436                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8437                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
8438                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
8439                getLLVMStyleWithColumns(70));
8440 
8441   verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
8442                "           \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
8443                "           \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;");
8444   verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
8445                "                  \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
8446                "                  \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);");
8447   verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n"
8448                "           (aaaa + aaaa);",
8449                getLLVMStyleWithColumns(40));
8450   verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n"
8451                "                  (aaaaaaa + aaaaa));",
8452                getLLVMStyleWithColumns(40));
8453   verifyFormat(
8454       "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n"
8455       "                  SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n"
8456       "                  bbbbbbbbbbbbbbbbbbbbbbb);");
8457 }
8458 
8459 TEST_F(FormatTest, UnderstandsEquals) {
8460   verifyFormat(
8461       "aaaaaaaaaaaaaaaaa =\n"
8462       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8463   verifyFormat(
8464       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
8465       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
8466   verifyFormat(
8467       "if (a) {\n"
8468       "  f();\n"
8469       "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
8470       "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
8471       "}");
8472 
8473   verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
8474                "        100000000 + 10000000) {\n}");
8475 }
8476 
8477 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
8478   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
8479                "    .looooooooooooooooooooooooooooooooooooooongFunction();");
8480 
8481   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
8482                "    ->looooooooooooooooooooooooooooooooooooooongFunction();");
8483 
8484   verifyFormat(
8485       "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
8486       "                                                          Parameter2);");
8487 
8488   verifyFormat(
8489       "ShortObject->shortFunction(\n"
8490       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
8491       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
8492 
8493   verifyFormat("loooooooooooooongFunction(\n"
8494                "    LoooooooooooooongObject->looooooooooooooooongFunction());");
8495 
8496   verifyFormat(
8497       "function(LoooooooooooooooooooooooooooooooooooongObject\n"
8498       "             ->loooooooooooooooooooooooooooooooooooooooongFunction());");
8499 
8500   verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
8501                "    .WillRepeatedly(Return(SomeValue));");
8502   verifyFormat("void f() {\n"
8503                "  EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
8504                "      .Times(2)\n"
8505                "      .WillRepeatedly(Return(SomeValue));\n"
8506                "}");
8507   verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
8508                "    ccccccccccccccccccccccc);");
8509   verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8510                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8511                "          .aaaaa(aaaaa),\n"
8512                "      aaaaaaaaaaaaaaaaaaaaa);");
8513   verifyFormat("void f() {\n"
8514                "  aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8515                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
8516                "}");
8517   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8518                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8519                "    .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8520                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8521                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
8522   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8523                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8524                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8525                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
8526                "}");
8527 
8528   // Here, it is not necessary to wrap at "." or "->".
8529   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
8530                "    aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
8531   verifyFormat(
8532       "aaaaaaaaaaa->aaaaaaaaa(\n"
8533       "    aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8534       "    aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
8535 
8536   verifyFormat(
8537       "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8538       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
8539   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
8540                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
8541   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
8542                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
8543 
8544   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8545                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8546                "    .a();");
8547 
8548   FormatStyle NoBinPacking = getLLVMStyle();
8549   NoBinPacking.BinPackParameters = false;
8550   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
8551                "    .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
8552                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
8553                "                         aaaaaaaaaaaaaaaaaaa,\n"
8554                "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
8555                NoBinPacking);
8556 
8557   // If there is a subsequent call, change to hanging indentation.
8558   verifyFormat(
8559       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8560       "                         aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
8561       "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
8562   verifyFormat(
8563       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8564       "    aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
8565   verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8566                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8567                "                 .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
8568   verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8569                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8570                "               .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
8571 }
8572 
8573 TEST_F(FormatTest, WrapsTemplateDeclarations) {
8574   verifyFormat("template <typename T>\n"
8575                "virtual void loooooooooooongFunction(int Param1, int Param2);");
8576   verifyFormat("template <typename T>\n"
8577                "// T should be one of {A, B}.\n"
8578                "virtual void loooooooooooongFunction(int Param1, int Param2);");
8579   verifyFormat(
8580       "template <typename T>\n"
8581       "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
8582   verifyFormat("template <typename T>\n"
8583                "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
8584                "       int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
8585   verifyFormat(
8586       "template <typename T>\n"
8587       "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
8588       "                                      int Paaaaaaaaaaaaaaaaaaaaram2);");
8589   verifyFormat(
8590       "template <typename T>\n"
8591       "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
8592       "                    aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
8593       "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8594   verifyFormat("template <typename T>\n"
8595                "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8596                "    int aaaaaaaaaaaaaaaaaaaaaa);");
8597   verifyFormat(
8598       "template <typename T1, typename T2 = char, typename T3 = char,\n"
8599       "          typename T4 = char>\n"
8600       "void f();");
8601   verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
8602                "          template <typename> class cccccccccccccccccccccc,\n"
8603                "          typename ddddddddddddd>\n"
8604                "class C {};");
8605   verifyFormat(
8606       "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
8607       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8608 
8609   verifyFormat("void f() {\n"
8610                "  a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
8611                "      a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
8612                "}");
8613 
8614   verifyFormat("template <typename T> class C {};");
8615   verifyFormat("template <typename T> void f();");
8616   verifyFormat("template <typename T> void f() {}");
8617   verifyFormat(
8618       "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
8619       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8620       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
8621       "    new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
8622       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8623       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
8624       "        bbbbbbbbbbbbbbbbbbbbbbbb);",
8625       getLLVMStyleWithColumns(72));
8626   EXPECT_EQ("static_cast<A< //\n"
8627             "    B> *>(\n"
8628             "\n"
8629             ");",
8630             format("static_cast<A<//\n"
8631                    "    B>*>(\n"
8632                    "\n"
8633                    "    );"));
8634   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8635                "    const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
8636 
8637   FormatStyle AlwaysBreak = getLLVMStyle();
8638   AlwaysBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
8639   verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
8640   verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
8641   verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
8642   verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8643                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
8644                "    ccccccccccccccccccccccccccccccccccccccccccccccc);");
8645   verifyFormat("template <template <typename> class Fooooooo,\n"
8646                "          template <typename> class Baaaaaaar>\n"
8647                "struct C {};",
8648                AlwaysBreak);
8649   verifyFormat("template <typename T> // T can be A, B or C.\n"
8650                "struct C {};",
8651                AlwaysBreak);
8652   verifyFormat("template <enum E> class A {\n"
8653                "public:\n"
8654                "  E *f();\n"
8655                "};");
8656 
8657   FormatStyle NeverBreak = getLLVMStyle();
8658   NeverBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_No;
8659   verifyFormat("template <typename T> class C {};", NeverBreak);
8660   verifyFormat("template <typename T> void f();", NeverBreak);
8661   verifyFormat("template <typename T> void f() {}", NeverBreak);
8662   verifyFormat("template <typename T>\nvoid foo(aaaaaaaaaaaaaaaaaaaaaaaaaa "
8663                "bbbbbbbbbbbbbbbbbbbb) {}",
8664                NeverBreak);
8665   verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8666                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
8667                "    ccccccccccccccccccccccccccccccccccccccccccccccc);",
8668                NeverBreak);
8669   verifyFormat("template <template <typename> class Fooooooo,\n"
8670                "          template <typename> class Baaaaaaar>\n"
8671                "struct C {};",
8672                NeverBreak);
8673   verifyFormat("template <typename T> // T can be A, B or C.\n"
8674                "struct C {};",
8675                NeverBreak);
8676   verifyFormat("template <enum E> class A {\n"
8677                "public:\n"
8678                "  E *f();\n"
8679                "};",
8680                NeverBreak);
8681   NeverBreak.PenaltyBreakTemplateDeclaration = 100;
8682   verifyFormat("template <typename T> void\nfoo(aaaaaaaaaaaaaaaaaaaaaaaaaa "
8683                "bbbbbbbbbbbbbbbbbbbb) {}",
8684                NeverBreak);
8685 }
8686 
8687 TEST_F(FormatTest, WrapsTemplateDeclarationsWithComments) {
8688   FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp);
8689   Style.ColumnLimit = 60;
8690   EXPECT_EQ("// Baseline - no comments.\n"
8691             "template <\n"
8692             "    typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n"
8693             "void f() {}",
8694             format("// Baseline - no comments.\n"
8695                    "template <\n"
8696                    "    typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n"
8697                    "void f() {}",
8698                    Style));
8699 
8700   EXPECT_EQ("template <\n"
8701             "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value>  // trailing\n"
8702             "void f() {}",
8703             format("template <\n"
8704                    "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
8705                    "void f() {}",
8706                    Style));
8707 
8708   EXPECT_EQ(
8709       "template <\n"
8710       "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n"
8711       "void f() {}",
8712       format("template <typename aaaaaaaaaa<bbbbbbbbbbbb>::value>  /* line */\n"
8713              "void f() {}",
8714              Style));
8715 
8716   EXPECT_EQ(
8717       "template <\n"
8718       "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value>  // trailing\n"
8719       "                                               // multiline\n"
8720       "void f() {}",
8721       format("template <\n"
8722              "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
8723              "                                              // multiline\n"
8724              "void f() {}",
8725              Style));
8726 
8727   EXPECT_EQ(
8728       "template <typename aaaaaaaaaa<\n"
8729       "    bbbbbbbbbbbb>::value>  // trailing loooong\n"
8730       "void f() {}",
8731       format(
8732           "template <\n"
8733           "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing loooong\n"
8734           "void f() {}",
8735           Style));
8736 }
8737 
8738 TEST_F(FormatTest, WrapsTemplateParameters) {
8739   FormatStyle Style = getLLVMStyle();
8740   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
8741   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
8742   verifyFormat(
8743       "template <typename... a> struct q {};\n"
8744       "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
8745       "    aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
8746       "    y;",
8747       Style);
8748   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
8749   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
8750   verifyFormat(
8751       "template <typename... a> struct r {};\n"
8752       "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
8753       "    aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
8754       "    y;",
8755       Style);
8756   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
8757   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
8758   verifyFormat("template <typename... a> struct s {};\n"
8759                "extern s<\n"
8760                "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
8761                "aaaaaaaaaaaaaaaaaaaaaa,\n"
8762                "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
8763                "aaaaaaaaaaaaaaaaaaaaaa>\n"
8764                "    y;",
8765                Style);
8766   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
8767   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
8768   verifyFormat("template <typename... a> struct t {};\n"
8769                "extern t<\n"
8770                "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
8771                "aaaaaaaaaaaaaaaaaaaaaa,\n"
8772                "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
8773                "aaaaaaaaaaaaaaaaaaaaaa>\n"
8774                "    y;",
8775                Style);
8776 }
8777 
8778 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
8779   verifyFormat(
8780       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
8781       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
8782   verifyFormat(
8783       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
8784       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8785       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
8786 
8787   // FIXME: Should we have the extra indent after the second break?
8788   verifyFormat(
8789       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
8790       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
8791       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
8792 
8793   verifyFormat(
8794       "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
8795       "                    cccccccccccccccccccccccccccccccccccccccccccccc());");
8796 
8797   // Breaking at nested name specifiers is generally not desirable.
8798   verifyFormat(
8799       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8800       "    aaaaaaaaaaaaaaaaaaaaaaa);");
8801 
8802   verifyFormat("aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n"
8803                "                   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
8804                "                       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8805                "                   aaaaaaaaaaaaaaaaaaaaa);",
8806                getLLVMStyleWithColumns(74));
8807 
8808   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
8809                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8810                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
8811 }
8812 
8813 TEST_F(FormatTest, UnderstandsTemplateParameters) {
8814   verifyFormat("A<int> a;");
8815   verifyFormat("A<A<A<int>>> a;");
8816   verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
8817   verifyFormat("bool x = a < 1 || 2 > a;");
8818   verifyFormat("bool x = 5 < f<int>();");
8819   verifyFormat("bool x = f<int>() > 5;");
8820   verifyFormat("bool x = 5 < a<int>::x;");
8821   verifyFormat("bool x = a < 4 ? a > 2 : false;");
8822   verifyFormat("bool x = f() ? a < 2 : a > 2;");
8823 
8824   verifyGoogleFormat("A<A<int>> a;");
8825   verifyGoogleFormat("A<A<A<int>>> a;");
8826   verifyGoogleFormat("A<A<A<A<int>>>> a;");
8827   verifyGoogleFormat("A<A<int> > a;");
8828   verifyGoogleFormat("A<A<A<int> > > a;");
8829   verifyGoogleFormat("A<A<A<A<int> > > > a;");
8830   verifyGoogleFormat("A<::A<int>> a;");
8831   verifyGoogleFormat("A<::A> a;");
8832   verifyGoogleFormat("A< ::A> a;");
8833   verifyGoogleFormat("A< ::A<int> > a;");
8834   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
8835   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
8836   EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle()));
8837   EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle()));
8838   EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };",
8839             format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle()));
8840 
8841   verifyFormat("A<A<int>> a;", getChromiumStyle(FormatStyle::LK_Cpp));
8842 
8843   // template closer followed by a token that starts with > or =
8844   verifyFormat("bool b = a<1> > 1;");
8845   verifyFormat("bool b = a<1> >= 1;");
8846   verifyFormat("int i = a<1> >> 1;");
8847   FormatStyle Style = getLLVMStyle();
8848   Style.SpaceBeforeAssignmentOperators = false;
8849   verifyFormat("bool b= a<1> == 1;", Style);
8850   verifyFormat("a<int> = 1;", Style);
8851   verifyFormat("a<int> >>= 1;", Style);
8852 
8853   verifyFormat("test < a | b >> c;");
8854   verifyFormat("test<test<a | b>> c;");
8855   verifyFormat("test >> a >> b;");
8856   verifyFormat("test << a >> b;");
8857 
8858   verifyFormat("f<int>();");
8859   verifyFormat("template <typename T> void f() {}");
8860   verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
8861   verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : "
8862                "sizeof(char)>::type>;");
8863   verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};");
8864   verifyFormat("f(a.operator()<A>());");
8865   verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8866                "      .template operator()<A>());",
8867                getLLVMStyleWithColumns(35));
8868 
8869   // Not template parameters.
8870   verifyFormat("return a < b && c > d;");
8871   verifyFormat("void f() {\n"
8872                "  while (a < b && c > d) {\n"
8873                "  }\n"
8874                "}");
8875   verifyFormat("template <typename... Types>\n"
8876                "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
8877 
8878   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8879                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
8880                getLLVMStyleWithColumns(60));
8881   verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
8882   verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
8883   verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <");
8884   verifyFormat("some_templated_type<decltype([](int i) { return i; })>");
8885 }
8886 
8887 TEST_F(FormatTest, UnderstandsShiftOperators) {
8888   verifyFormat("if (i < x >> 1)");
8889   verifyFormat("while (i < x >> 1)");
8890   verifyFormat("for (unsigned i = 0; i < i; ++i, v = v >> 1)");
8891   verifyFormat("for (unsigned i = 0; i < x >> 1; ++i, v = v >> 1)");
8892   verifyFormat(
8893       "for (std::vector<int>::iterator i = 0; i < x >> 1; ++i, v = v >> 1)");
8894   verifyFormat("Foo.call<Bar<Function>>()");
8895   verifyFormat("if (Foo.call<Bar<Function>>() == 0)");
8896   verifyFormat("for (std::vector<std::pair<int>>::iterator i = 0; i < x >> 1; "
8897                "++i, v = v >> 1)");
8898   verifyFormat("if (w<u<v<x>>, 1>::t)");
8899 }
8900 
8901 TEST_F(FormatTest, BitshiftOperatorWidth) {
8902   EXPECT_EQ("int a = 1 << 2; /* foo\n"
8903             "                   bar */",
8904             format("int    a=1<<2;  /* foo\n"
8905                    "                   bar */"));
8906 
8907   EXPECT_EQ("int b = 256 >> 1; /* foo\n"
8908             "                     bar */",
8909             format("int  b  =256>>1 ;  /* foo\n"
8910                    "                      bar */"));
8911 }
8912 
8913 TEST_F(FormatTest, UnderstandsBinaryOperators) {
8914   verifyFormat("COMPARE(a, ==, b);");
8915   verifyFormat("auto s = sizeof...(Ts) - 1;");
8916 }
8917 
8918 TEST_F(FormatTest, UnderstandsPointersToMembers) {
8919   verifyFormat("int A::*x;");
8920   verifyFormat("int (S::*func)(void *);");
8921   verifyFormat("void f() { int (S::*func)(void *); }");
8922   verifyFormat("typedef bool *(Class::*Member)() const;");
8923   verifyFormat("void f() {\n"
8924                "  (a->*f)();\n"
8925                "  a->*x;\n"
8926                "  (a.*f)();\n"
8927                "  ((*a).*f)();\n"
8928                "  a.*x;\n"
8929                "}");
8930   verifyFormat("void f() {\n"
8931                "  (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
8932                "      aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
8933                "}");
8934   verifyFormat(
8935       "(aaaaaaaaaa->*bbbbbbb)(\n"
8936       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
8937   FormatStyle Style = getLLVMStyle();
8938   Style.PointerAlignment = FormatStyle::PAS_Left;
8939   verifyFormat("typedef bool* (Class::*Member)() const;", Style);
8940 }
8941 
8942 TEST_F(FormatTest, UnderstandsUnaryOperators) {
8943   verifyFormat("int a = -2;");
8944   verifyFormat("f(-1, -2, -3);");
8945   verifyFormat("a[-1] = 5;");
8946   verifyFormat("int a = 5 + -2;");
8947   verifyFormat("if (i == -1) {\n}");
8948   verifyFormat("if (i != -1) {\n}");
8949   verifyFormat("if (i > -1) {\n}");
8950   verifyFormat("if (i < -1) {\n}");
8951   verifyFormat("++(a->f());");
8952   verifyFormat("--(a->f());");
8953   verifyFormat("(a->f())++;");
8954   verifyFormat("a[42]++;");
8955   verifyFormat("if (!(a->f())) {\n}");
8956   verifyFormat("if (!+i) {\n}");
8957   verifyFormat("~&a;");
8958 
8959   verifyFormat("a-- > b;");
8960   verifyFormat("b ? -a : c;");
8961   verifyFormat("n * sizeof char16;");
8962   verifyFormat("n * alignof char16;", getGoogleStyle());
8963   verifyFormat("sizeof(char);");
8964   verifyFormat("alignof(char);", getGoogleStyle());
8965 
8966   verifyFormat("return -1;");
8967   verifyFormat("throw -1;");
8968   verifyFormat("switch (a) {\n"
8969                "case -1:\n"
8970                "  break;\n"
8971                "}");
8972   verifyFormat("#define X -1");
8973   verifyFormat("#define X -kConstant");
8974 
8975   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
8976   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
8977 
8978   verifyFormat("int a = /* confusing comment */ -1;");
8979   // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
8980   verifyFormat("int a = i /* confusing comment */++;");
8981 
8982   verifyFormat("co_yield -1;");
8983   verifyFormat("co_return -1;");
8984 
8985   // Check that * is not treated as a binary operator when we set
8986   // PointerAlignment as PAS_Left after a keyword and not a declaration.
8987   FormatStyle PASLeftStyle = getLLVMStyle();
8988   PASLeftStyle.PointerAlignment = FormatStyle::PAS_Left;
8989   verifyFormat("co_return *a;", PASLeftStyle);
8990   verifyFormat("co_await *a;", PASLeftStyle);
8991   verifyFormat("co_yield *a", PASLeftStyle);
8992   verifyFormat("return *a;", PASLeftStyle);
8993 }
8994 
8995 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) {
8996   verifyFormat("if (!aaaaaaaaaa( // break\n"
8997                "        aaaaa)) {\n"
8998                "}");
8999   verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
9000                "    aaaaa));");
9001   verifyFormat("*aaa = aaaaaaa( // break\n"
9002                "    bbbbbb);");
9003 }
9004 
9005 TEST_F(FormatTest, UnderstandsOverloadedOperators) {
9006   verifyFormat("bool operator<();");
9007   verifyFormat("bool operator>();");
9008   verifyFormat("bool operator=();");
9009   verifyFormat("bool operator==();");
9010   verifyFormat("bool operator!=();");
9011   verifyFormat("int operator+();");
9012   verifyFormat("int operator++();");
9013   verifyFormat("int operator++(int) volatile noexcept;");
9014   verifyFormat("bool operator,();");
9015   verifyFormat("bool operator();");
9016   verifyFormat("bool operator()();");
9017   verifyFormat("bool operator[]();");
9018   verifyFormat("operator bool();");
9019   verifyFormat("operator int();");
9020   verifyFormat("operator void *();");
9021   verifyFormat("operator SomeType<int>();");
9022   verifyFormat("operator SomeType<int, int>();");
9023   verifyFormat("operator SomeType<SomeType<int>>();");
9024   verifyFormat("void *operator new(std::size_t size);");
9025   verifyFormat("void *operator new[](std::size_t size);");
9026   verifyFormat("void operator delete(void *ptr);");
9027   verifyFormat("void operator delete[](void *ptr);");
9028   verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
9029                "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
9030   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n"
9031                "    aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;");
9032 
9033   verifyFormat(
9034       "ostream &operator<<(ostream &OutputStream,\n"
9035       "                    SomeReallyLongType WithSomeReallyLongValue);");
9036   verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
9037                "               const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
9038                "  return left.group < right.group;\n"
9039                "}");
9040   verifyFormat("SomeType &operator=(const SomeType &S);");
9041   verifyFormat("f.template operator()<int>();");
9042 
9043   verifyGoogleFormat("operator void*();");
9044   verifyGoogleFormat("operator SomeType<SomeType<int>>();");
9045   verifyGoogleFormat("operator ::A();");
9046 
9047   verifyFormat("using A::operator+;");
9048   verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n"
9049                "int i;");
9050 
9051   // Calling an operator as a member function.
9052   verifyFormat("void f() { a.operator*(); }");
9053   verifyFormat("void f() { a.operator*(b & b); }");
9054   verifyFormat("void f() { a->operator&(a * b); }");
9055   verifyFormat("void f() { NS::a.operator+(*b * *b); }");
9056   // TODO: Calling an operator as a non-member function is hard to distinguish.
9057   // https://llvm.org/PR50629
9058   // verifyFormat("void f() { operator*(a & a); }");
9059   // verifyFormat("void f() { operator&(a, b * b); }");
9060 }
9061 
9062 TEST_F(FormatTest, UnderstandsFunctionRefQualification) {
9063   verifyFormat("Deleted &operator=(const Deleted &) & = default;");
9064   verifyFormat("Deleted &operator=(const Deleted &) && = delete;");
9065   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;");
9066   verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;");
9067   verifyFormat("Deleted &operator=(const Deleted &) &;");
9068   verifyFormat("Deleted &operator=(const Deleted &) &&;");
9069   verifyFormat("SomeType MemberFunction(const Deleted &) &;");
9070   verifyFormat("SomeType MemberFunction(const Deleted &) &&;");
9071   verifyFormat("SomeType MemberFunction(const Deleted &) && {}");
9072   verifyFormat("SomeType MemberFunction(const Deleted &) && final {}");
9073   verifyFormat("SomeType MemberFunction(const Deleted &) && override {}");
9074   verifyFormat("void Fn(T const &) const &;");
9075   verifyFormat("void Fn(T const volatile &&) const volatile &&;");
9076   verifyFormat("template <typename T>\n"
9077                "void F(T) && = delete;",
9078                getGoogleStyle());
9079 
9080   FormatStyle AlignLeft = getLLVMStyle();
9081   AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
9082   verifyFormat("void A::b() && {}", AlignLeft);
9083   verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft);
9084   verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;",
9085                AlignLeft);
9086   verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft);
9087   verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft);
9088   verifyFormat("auto Function(T t) & -> void {}", AlignLeft);
9089   verifyFormat("auto Function(T... t) & -> void {}", AlignLeft);
9090   verifyFormat("auto Function(T) & -> void {}", AlignLeft);
9091   verifyFormat("auto Function(T) & -> void;", AlignLeft);
9092   verifyFormat("void Fn(T const&) const&;", AlignLeft);
9093   verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft);
9094 
9095   FormatStyle Spaces = getLLVMStyle();
9096   Spaces.SpacesInCStyleCastParentheses = true;
9097   verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces);
9098   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces);
9099   verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces);
9100   verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces);
9101 
9102   Spaces.SpacesInCStyleCastParentheses = false;
9103   Spaces.SpacesInParentheses = true;
9104   verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces);
9105   verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;",
9106                Spaces);
9107   verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces);
9108   verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces);
9109 
9110   FormatStyle BreakTemplate = getLLVMStyle();
9111   BreakTemplate.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
9112 
9113   verifyFormat("struct f {\n"
9114                "  template <class T>\n"
9115                "  int &foo(const std::string &str) &noexcept {}\n"
9116                "};",
9117                BreakTemplate);
9118 
9119   verifyFormat("struct f {\n"
9120                "  template <class T>\n"
9121                "  int &foo(const std::string &str) &&noexcept {}\n"
9122                "};",
9123                BreakTemplate);
9124 
9125   verifyFormat("struct f {\n"
9126                "  template <class T>\n"
9127                "  int &foo(const std::string &str) const &noexcept {}\n"
9128                "};",
9129                BreakTemplate);
9130 
9131   verifyFormat("struct f {\n"
9132                "  template <class T>\n"
9133                "  int &foo(const std::string &str) const &noexcept {}\n"
9134                "};",
9135                BreakTemplate);
9136 
9137   verifyFormat("struct f {\n"
9138                "  template <class T>\n"
9139                "  auto foo(const std::string &str) &&noexcept -> int & {}\n"
9140                "};",
9141                BreakTemplate);
9142 
9143   FormatStyle AlignLeftBreakTemplate = getLLVMStyle();
9144   AlignLeftBreakTemplate.AlwaysBreakTemplateDeclarations =
9145       FormatStyle::BTDS_Yes;
9146   AlignLeftBreakTemplate.PointerAlignment = FormatStyle::PAS_Left;
9147 
9148   verifyFormat("struct f {\n"
9149                "  template <class T>\n"
9150                "  int& foo(const std::string& str) & noexcept {}\n"
9151                "};",
9152                AlignLeftBreakTemplate);
9153 
9154   verifyFormat("struct f {\n"
9155                "  template <class T>\n"
9156                "  int& foo(const std::string& str) && noexcept {}\n"
9157                "};",
9158                AlignLeftBreakTemplate);
9159 
9160   verifyFormat("struct f {\n"
9161                "  template <class T>\n"
9162                "  int& foo(const std::string& str) const& noexcept {}\n"
9163                "};",
9164                AlignLeftBreakTemplate);
9165 
9166   verifyFormat("struct f {\n"
9167                "  template <class T>\n"
9168                "  int& foo(const std::string& str) const&& noexcept {}\n"
9169                "};",
9170                AlignLeftBreakTemplate);
9171 
9172   verifyFormat("struct f {\n"
9173                "  template <class T>\n"
9174                "  auto foo(const std::string& str) && noexcept -> int& {}\n"
9175                "};",
9176                AlignLeftBreakTemplate);
9177 
9178   // The `&` in `Type&` should not be confused with a trailing `&` of
9179   // DEPRECATED(reason) member function.
9180   verifyFormat("struct f {\n"
9181                "  template <class T>\n"
9182                "  DEPRECATED(reason)\n"
9183                "  Type &foo(arguments) {}\n"
9184                "};",
9185                BreakTemplate);
9186 
9187   verifyFormat("struct f {\n"
9188                "  template <class T>\n"
9189                "  DEPRECATED(reason)\n"
9190                "  Type& foo(arguments) {}\n"
9191                "};",
9192                AlignLeftBreakTemplate);
9193 
9194   verifyFormat("void (*foopt)(int) = &func;");
9195 }
9196 
9197 TEST_F(FormatTest, UnderstandsNewAndDelete) {
9198   verifyFormat("void f() {\n"
9199                "  A *a = new A;\n"
9200                "  A *a = new (placement) A;\n"
9201                "  delete a;\n"
9202                "  delete (A *)a;\n"
9203                "}");
9204   verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
9205                "    typename aaaaaaaaaaaaaaaaaaaaaaaa();");
9206   verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
9207                "    new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
9208                "        typename aaaaaaaaaaaaaaaaaaaaaaaa();");
9209   verifyFormat("delete[] h->p;");
9210 }
9211 
9212 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
9213   verifyFormat("int *f(int *a) {}");
9214   verifyFormat("int main(int argc, char **argv) {}");
9215   verifyFormat("Test::Test(int b) : a(b * b) {}");
9216   verifyIndependentOfContext("f(a, *a);");
9217   verifyFormat("void g() { f(*a); }");
9218   verifyIndependentOfContext("int a = b * 10;");
9219   verifyIndependentOfContext("int a = 10 * b;");
9220   verifyIndependentOfContext("int a = b * c;");
9221   verifyIndependentOfContext("int a += b * c;");
9222   verifyIndependentOfContext("int a -= b * c;");
9223   verifyIndependentOfContext("int a *= b * c;");
9224   verifyIndependentOfContext("int a /= b * c;");
9225   verifyIndependentOfContext("int a = *b;");
9226   verifyIndependentOfContext("int a = *b * c;");
9227   verifyIndependentOfContext("int a = b * *c;");
9228   verifyIndependentOfContext("int a = b * (10);");
9229   verifyIndependentOfContext("S << b * (10);");
9230   verifyIndependentOfContext("return 10 * b;");
9231   verifyIndependentOfContext("return *b * *c;");
9232   verifyIndependentOfContext("return a & ~b;");
9233   verifyIndependentOfContext("f(b ? *c : *d);");
9234   verifyIndependentOfContext("int a = b ? *c : *d;");
9235   verifyIndependentOfContext("*b = a;");
9236   verifyIndependentOfContext("a * ~b;");
9237   verifyIndependentOfContext("a * !b;");
9238   verifyIndependentOfContext("a * +b;");
9239   verifyIndependentOfContext("a * -b;");
9240   verifyIndependentOfContext("a * ++b;");
9241   verifyIndependentOfContext("a * --b;");
9242   verifyIndependentOfContext("a[4] * b;");
9243   verifyIndependentOfContext("a[a * a] = 1;");
9244   verifyIndependentOfContext("f() * b;");
9245   verifyIndependentOfContext("a * [self dostuff];");
9246   verifyIndependentOfContext("int x = a * (a + b);");
9247   verifyIndependentOfContext("(a *)(a + b);");
9248   verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
9249   verifyIndependentOfContext("int *pa = (int *)&a;");
9250   verifyIndependentOfContext("return sizeof(int **);");
9251   verifyIndependentOfContext("return sizeof(int ******);");
9252   verifyIndependentOfContext("return (int **&)a;");
9253   verifyIndependentOfContext("f((*PointerToArray)[10]);");
9254   verifyFormat("void f(Type (*parameter)[10]) {}");
9255   verifyFormat("void f(Type (&parameter)[10]) {}");
9256   verifyGoogleFormat("return sizeof(int**);");
9257   verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
9258   verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
9259   verifyFormat("auto a = [](int **&, int ***) {};");
9260   verifyFormat("auto PointerBinding = [](const char *S) {};");
9261   verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
9262   verifyFormat("[](const decltype(*a) &value) {}");
9263   verifyFormat("[](const typeof(*a) &value) {}");
9264   verifyFormat("[](const _Atomic(a *) &value) {}");
9265   verifyFormat("[](const __underlying_type(a) &value) {}");
9266   verifyFormat("decltype(a * b) F();");
9267   verifyFormat("typeof(a * b) F();");
9268   verifyFormat("#define MACRO() [](A *a) { return 1; }");
9269   verifyFormat("Constructor() : member([](A *a, B *b) {}) {}");
9270   verifyIndependentOfContext("typedef void (*f)(int *a);");
9271   verifyIndependentOfContext("int i{a * b};");
9272   verifyIndependentOfContext("aaa && aaa->f();");
9273   verifyIndependentOfContext("int x = ~*p;");
9274   verifyFormat("Constructor() : a(a), area(width * height) {}");
9275   verifyFormat("Constructor() : a(a), area(a, width * height) {}");
9276   verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}");
9277   verifyFormat("void f() { f(a, c * d); }");
9278   verifyFormat("void f() { f(new a(), c * d); }");
9279   verifyFormat("void f(const MyOverride &override);");
9280   verifyFormat("void f(const MyFinal &final);");
9281   verifyIndependentOfContext("bool a = f() && override.f();");
9282   verifyIndependentOfContext("bool a = f() && final.f();");
9283 
9284   verifyIndependentOfContext("InvalidRegions[*R] = 0;");
9285 
9286   verifyIndependentOfContext("A<int *> a;");
9287   verifyIndependentOfContext("A<int **> a;");
9288   verifyIndependentOfContext("A<int *, int *> a;");
9289   verifyIndependentOfContext("A<int *[]> a;");
9290   verifyIndependentOfContext(
9291       "const char *const p = reinterpret_cast<const char *const>(q);");
9292   verifyIndependentOfContext("A<int **, int **> a;");
9293   verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
9294   verifyFormat("for (char **a = b; *a; ++a) {\n}");
9295   verifyFormat("for (; a && b;) {\n}");
9296   verifyFormat("bool foo = true && [] { return false; }();");
9297 
9298   verifyFormat(
9299       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9300       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9301 
9302   verifyGoogleFormat("int const* a = &b;");
9303   verifyGoogleFormat("**outparam = 1;");
9304   verifyGoogleFormat("*outparam = a * b;");
9305   verifyGoogleFormat("int main(int argc, char** argv) {}");
9306   verifyGoogleFormat("A<int*> a;");
9307   verifyGoogleFormat("A<int**> a;");
9308   verifyGoogleFormat("A<int*, int*> a;");
9309   verifyGoogleFormat("A<int**, int**> a;");
9310   verifyGoogleFormat("f(b ? *c : *d);");
9311   verifyGoogleFormat("int a = b ? *c : *d;");
9312   verifyGoogleFormat("Type* t = **x;");
9313   verifyGoogleFormat("Type* t = *++*x;");
9314   verifyGoogleFormat("*++*x;");
9315   verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
9316   verifyGoogleFormat("Type* t = x++ * y;");
9317   verifyGoogleFormat(
9318       "const char* const p = reinterpret_cast<const char* const>(q);");
9319   verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
9320   verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
9321   verifyGoogleFormat("template <typename T>\n"
9322                      "void f(int i = 0, SomeType** temps = NULL);");
9323 
9324   FormatStyle Left = getLLVMStyle();
9325   Left.PointerAlignment = FormatStyle::PAS_Left;
9326   verifyFormat("x = *a(x) = *a(y);", Left);
9327   verifyFormat("for (;; *a = b) {\n}", Left);
9328   verifyFormat("return *this += 1;", Left);
9329   verifyFormat("throw *x;", Left);
9330   verifyFormat("delete *x;", Left);
9331   verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left);
9332   verifyFormat("[](const decltype(*a)* ptr) {}", Left);
9333   verifyFormat("[](const typeof(*a)* ptr) {}", Left);
9334   verifyFormat("[](const _Atomic(a*)* ptr) {}", Left);
9335   verifyFormat("[](const __underlying_type(a)* ptr) {}", Left);
9336   verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left);
9337   verifyFormat("auto x(A&&, B&&, C&&) -> D;", Left);
9338   verifyFormat("auto x = [](A&&, B&&, C&&) -> D {};", Left);
9339   verifyFormat("template <class T> X(T&&, T&&, T&&) -> X<T>;", Left);
9340 
9341   verifyIndependentOfContext("a = *(x + y);");
9342   verifyIndependentOfContext("a = &(x + y);");
9343   verifyIndependentOfContext("*(x + y).call();");
9344   verifyIndependentOfContext("&(x + y)->call();");
9345   verifyFormat("void f() { &(*I).first; }");
9346 
9347   verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
9348   verifyFormat(
9349       "int *MyValues = {\n"
9350       "    *A, // Operator detection might be confused by the '{'\n"
9351       "    *BB // Operator detection might be confused by previous comment\n"
9352       "};");
9353 
9354   verifyIndependentOfContext("if (int *a = &b)");
9355   verifyIndependentOfContext("if (int &a = *b)");
9356   verifyIndependentOfContext("if (a & b[i])");
9357   verifyIndependentOfContext("if constexpr (a & b[i])");
9358   verifyIndependentOfContext("if CONSTEXPR (a & b[i])");
9359   verifyIndependentOfContext("if (a * (b * c))");
9360   verifyIndependentOfContext("if constexpr (a * (b * c))");
9361   verifyIndependentOfContext("if CONSTEXPR (a * (b * c))");
9362   verifyIndependentOfContext("if (a::b::c::d & b[i])");
9363   verifyIndependentOfContext("if (*b[i])");
9364   verifyIndependentOfContext("if (int *a = (&b))");
9365   verifyIndependentOfContext("while (int *a = &b)");
9366   verifyIndependentOfContext("while (a * (b * c))");
9367   verifyIndependentOfContext("size = sizeof *a;");
9368   verifyIndependentOfContext("if (a && (b = c))");
9369   verifyFormat("void f() {\n"
9370                "  for (const int &v : Values) {\n"
9371                "  }\n"
9372                "}");
9373   verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
9374   verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
9375   verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
9376 
9377   verifyFormat("#define A (!a * b)");
9378   verifyFormat("#define MACRO     \\\n"
9379                "  int *i = a * b; \\\n"
9380                "  void f(a *b);",
9381                getLLVMStyleWithColumns(19));
9382 
9383   verifyIndependentOfContext("A = new SomeType *[Length];");
9384   verifyIndependentOfContext("A = new SomeType *[Length]();");
9385   verifyIndependentOfContext("T **t = new T *;");
9386   verifyIndependentOfContext("T **t = new T *();");
9387   verifyGoogleFormat("A = new SomeType*[Length]();");
9388   verifyGoogleFormat("A = new SomeType*[Length];");
9389   verifyGoogleFormat("T** t = new T*;");
9390   verifyGoogleFormat("T** t = new T*();");
9391 
9392   verifyFormat("STATIC_ASSERT((a & b) == 0);");
9393   verifyFormat("STATIC_ASSERT(0 == (a & b));");
9394   verifyFormat("template <bool a, bool b> "
9395                "typename t::if<x && y>::type f() {}");
9396   verifyFormat("template <int *y> f() {}");
9397   verifyFormat("vector<int *> v;");
9398   verifyFormat("vector<int *const> v;");
9399   verifyFormat("vector<int *const **const *> v;");
9400   verifyFormat("vector<int *volatile> v;");
9401   verifyFormat("vector<a *_Nonnull> v;");
9402   verifyFormat("vector<a *_Nullable> v;");
9403   verifyFormat("vector<a *_Null_unspecified> v;");
9404   verifyFormat("vector<a *__ptr32> v;");
9405   verifyFormat("vector<a *__ptr64> v;");
9406   verifyFormat("vector<a *__capability> v;");
9407   FormatStyle TypeMacros = getLLVMStyle();
9408   TypeMacros.TypenameMacros = {"LIST"};
9409   verifyFormat("vector<LIST(uint64_t)> v;", TypeMacros);
9410   verifyFormat("vector<LIST(uint64_t) *> v;", TypeMacros);
9411   verifyFormat("vector<LIST(uint64_t) **> v;", TypeMacros);
9412   verifyFormat("vector<LIST(uint64_t) *attr> v;", TypeMacros);
9413   verifyFormat("vector<A(uint64_t) * attr> v;", TypeMacros); // multiplication
9414 
9415   FormatStyle CustomQualifier = getLLVMStyle();
9416   // Add indentifers that should not be parsed as a qualifier by default.
9417   CustomQualifier.AttributeMacros.push_back("__my_qualifier");
9418   CustomQualifier.AttributeMacros.push_back("_My_qualifier");
9419   CustomQualifier.AttributeMacros.push_back("my_other_qualifier");
9420   verifyFormat("vector<a * __my_qualifier> parse_as_multiply;");
9421   verifyFormat("vector<a *__my_qualifier> v;", CustomQualifier);
9422   verifyFormat("vector<a * _My_qualifier> parse_as_multiply;");
9423   verifyFormat("vector<a *_My_qualifier> v;", CustomQualifier);
9424   verifyFormat("vector<a * my_other_qualifier> parse_as_multiply;");
9425   verifyFormat("vector<a *my_other_qualifier> v;", CustomQualifier);
9426   verifyFormat("vector<a * _NotAQualifier> v;");
9427   verifyFormat("vector<a * __not_a_qualifier> v;");
9428   verifyFormat("vector<a * b> v;");
9429   verifyFormat("foo<b && false>();");
9430   verifyFormat("foo<b & 1>();");
9431   verifyFormat("decltype(*::std::declval<const T &>()) void F();");
9432   verifyFormat("typeof(*::std::declval<const T &>()) void F();");
9433   verifyFormat("_Atomic(*::std::declval<const T &>()) void F();");
9434   verifyFormat("__underlying_type(*::std::declval<const T &>()) void F();");
9435   verifyFormat(
9436       "template <class T, class = typename std::enable_if<\n"
9437       "                       std::is_integral<T>::value &&\n"
9438       "                       (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
9439       "void F();",
9440       getLLVMStyleWithColumns(70));
9441   verifyFormat("template <class T,\n"
9442                "          class = typename std::enable_if<\n"
9443                "              std::is_integral<T>::value &&\n"
9444                "              (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n"
9445                "          class U>\n"
9446                "void F();",
9447                getLLVMStyleWithColumns(70));
9448   verifyFormat(
9449       "template <class T,\n"
9450       "          class = typename ::std::enable_if<\n"
9451       "              ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
9452       "void F();",
9453       getGoogleStyleWithColumns(68));
9454 
9455   verifyIndependentOfContext("MACRO(int *i);");
9456   verifyIndependentOfContext("MACRO(auto *a);");
9457   verifyIndependentOfContext("MACRO(const A *a);");
9458   verifyIndependentOfContext("MACRO(_Atomic(A) *a);");
9459   verifyIndependentOfContext("MACRO(decltype(A) *a);");
9460   verifyIndependentOfContext("MACRO(typeof(A) *a);");
9461   verifyIndependentOfContext("MACRO(__underlying_type(A) *a);");
9462   verifyIndependentOfContext("MACRO(A *const a);");
9463   verifyIndependentOfContext("MACRO(A *restrict a);");
9464   verifyIndependentOfContext("MACRO(A *__restrict__ a);");
9465   verifyIndependentOfContext("MACRO(A *__restrict a);");
9466   verifyIndependentOfContext("MACRO(A *volatile a);");
9467   verifyIndependentOfContext("MACRO(A *__volatile a);");
9468   verifyIndependentOfContext("MACRO(A *__volatile__ a);");
9469   verifyIndependentOfContext("MACRO(A *_Nonnull a);");
9470   verifyIndependentOfContext("MACRO(A *_Nullable a);");
9471   verifyIndependentOfContext("MACRO(A *_Null_unspecified a);");
9472   verifyIndependentOfContext("MACRO(A *__attribute__((foo)) a);");
9473   verifyIndependentOfContext("MACRO(A *__attribute((foo)) a);");
9474   verifyIndependentOfContext("MACRO(A *[[clang::attr]] a);");
9475   verifyIndependentOfContext("MACRO(A *[[clang::attr(\"foo\")]] a);");
9476   verifyIndependentOfContext("MACRO(A *__ptr32 a);");
9477   verifyIndependentOfContext("MACRO(A *__ptr64 a);");
9478   verifyIndependentOfContext("MACRO(A *__capability);");
9479   verifyIndependentOfContext("MACRO(A &__capability);");
9480   verifyFormat("MACRO(A *__my_qualifier);");               // type declaration
9481   verifyFormat("void f() { MACRO(A * __my_qualifier); }"); // multiplication
9482   // If we add __my_qualifier to AttributeMacros it should always be parsed as
9483   // a type declaration:
9484   verifyFormat("MACRO(A *__my_qualifier);", CustomQualifier);
9485   verifyFormat("void f() { MACRO(A *__my_qualifier); }", CustomQualifier);
9486   // Also check that TypenameMacros prevents parsing it as multiplication:
9487   verifyIndependentOfContext("MACRO(LIST(uint64_t) * a);"); // multiplication
9488   verifyIndependentOfContext("MACRO(LIST(uint64_t) *a);", TypeMacros); // type
9489 
9490   verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
9491   verifyFormat("void f() { f(float{1}, a * a); }");
9492   verifyFormat("void f() { f(float(1), a * a); }");
9493 
9494   verifyFormat("f((void (*)(int))g);");
9495   verifyFormat("f((void (&)(int))g);");
9496   verifyFormat("f((void (^)(int))g);");
9497 
9498   // FIXME: Is there a way to make this work?
9499   // verifyIndependentOfContext("MACRO(A *a);");
9500   verifyFormat("MACRO(A &B);");
9501   verifyFormat("MACRO(A *B);");
9502   verifyFormat("void f() { MACRO(A * B); }");
9503   verifyFormat("void f() { MACRO(A & B); }");
9504 
9505   // This lambda was mis-formatted after D88956 (treating it as a binop):
9506   verifyFormat("auto x = [](const decltype(x) &ptr) {};");
9507   verifyFormat("auto x = [](const decltype(x) *ptr) {};");
9508   verifyFormat("#define lambda [](const decltype(x) &ptr) {}");
9509   verifyFormat("#define lambda [](const decltype(x) *ptr) {}");
9510 
9511   verifyFormat("DatumHandle const *operator->() const { return input_; }");
9512   verifyFormat("return options != nullptr && operator==(*options);");
9513 
9514   EXPECT_EQ("#define OP(x)                                    \\\n"
9515             "  ostream &operator<<(ostream &s, const A &a) {  \\\n"
9516             "    return s << a.DebugString();                 \\\n"
9517             "  }",
9518             format("#define OP(x) \\\n"
9519                    "  ostream &operator<<(ostream &s, const A &a) { \\\n"
9520                    "    return s << a.DebugString(); \\\n"
9521                    "  }",
9522                    getLLVMStyleWithColumns(50)));
9523 
9524   // FIXME: We cannot handle this case yet; we might be able to figure out that
9525   // foo<x> d > v; doesn't make sense.
9526   verifyFormat("foo<a<b && c> d> v;");
9527 
9528   FormatStyle PointerMiddle = getLLVMStyle();
9529   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
9530   verifyFormat("delete *x;", PointerMiddle);
9531   verifyFormat("int * x;", PointerMiddle);
9532   verifyFormat("int *[] x;", PointerMiddle);
9533   verifyFormat("template <int * y> f() {}", PointerMiddle);
9534   verifyFormat("int * f(int * a) {}", PointerMiddle);
9535   verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle);
9536   verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle);
9537   verifyFormat("A<int *> a;", PointerMiddle);
9538   verifyFormat("A<int **> a;", PointerMiddle);
9539   verifyFormat("A<int *, int *> a;", PointerMiddle);
9540   verifyFormat("A<int *[]> a;", PointerMiddle);
9541   verifyFormat("A = new SomeType *[Length]();", PointerMiddle);
9542   verifyFormat("A = new SomeType *[Length];", PointerMiddle);
9543   verifyFormat("T ** t = new T *;", PointerMiddle);
9544 
9545   // Member function reference qualifiers aren't binary operators.
9546   verifyFormat("string // break\n"
9547                "operator()() & {}");
9548   verifyFormat("string // break\n"
9549                "operator()() && {}");
9550   verifyGoogleFormat("template <typename T>\n"
9551                      "auto x() & -> int {}");
9552 
9553   // Should be binary operators when used as an argument expression (overloaded
9554   // operator invoked as a member function).
9555   verifyFormat("void f() { a.operator()(a * a); }");
9556   verifyFormat("void f() { a->operator()(a & a); }");
9557   verifyFormat("void f() { a.operator()(*a & *a); }");
9558   verifyFormat("void f() { a->operator()(*a * *a); }");
9559 }
9560 
9561 TEST_F(FormatTest, UnderstandsAttributes) {
9562   verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
9563   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
9564                "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
9565   FormatStyle AfterType = getLLVMStyle();
9566   AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
9567   verifyFormat("__attribute__((nodebug)) void\n"
9568                "foo() {}\n",
9569                AfterType);
9570   verifyFormat("__unused void\n"
9571                "foo() {}",
9572                AfterType);
9573 
9574   FormatStyle CustomAttrs = getLLVMStyle();
9575   CustomAttrs.AttributeMacros.push_back("__unused");
9576   CustomAttrs.AttributeMacros.push_back("__attr1");
9577   CustomAttrs.AttributeMacros.push_back("__attr2");
9578   CustomAttrs.AttributeMacros.push_back("no_underscore_attr");
9579   verifyFormat("vector<SomeType *__attribute((foo))> v;");
9580   verifyFormat("vector<SomeType *__attribute__((foo))> v;");
9581   verifyFormat("vector<SomeType * __not_attribute__((foo))> v;");
9582   // Check that it is parsed as a multiplication without AttributeMacros and
9583   // as a pointer qualifier when we add __attr1/__attr2 to AttributeMacros.
9584   verifyFormat("vector<SomeType * __attr1> v;");
9585   verifyFormat("vector<SomeType __attr1 *> v;");
9586   verifyFormat("vector<SomeType __attr1 *const> v;");
9587   verifyFormat("vector<SomeType __attr1 * __attr2> v;");
9588   verifyFormat("vector<SomeType *__attr1> v;", CustomAttrs);
9589   verifyFormat("vector<SomeType *__attr2> v;", CustomAttrs);
9590   verifyFormat("vector<SomeType *no_underscore_attr> v;", CustomAttrs);
9591   verifyFormat("vector<SomeType __attr1 *> v;", CustomAttrs);
9592   verifyFormat("vector<SomeType __attr1 *const> v;", CustomAttrs);
9593   verifyFormat("vector<SomeType __attr1 *__attr2> v;", CustomAttrs);
9594   verifyFormat("vector<SomeType __attr1 *no_underscore_attr> v;", CustomAttrs);
9595 
9596   // Check that these are not parsed as function declarations:
9597   CustomAttrs.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
9598   CustomAttrs.BreakBeforeBraces = FormatStyle::BS_Allman;
9599   verifyFormat("SomeType s(InitValue);", CustomAttrs);
9600   verifyFormat("SomeType s{InitValue};", CustomAttrs);
9601   verifyFormat("SomeType *__unused s(InitValue);", CustomAttrs);
9602   verifyFormat("SomeType *__unused s{InitValue};", CustomAttrs);
9603   verifyFormat("SomeType s __unused(InitValue);", CustomAttrs);
9604   verifyFormat("SomeType s __unused{InitValue};", CustomAttrs);
9605   verifyFormat("SomeType *__capability s(InitValue);", CustomAttrs);
9606   verifyFormat("SomeType *__capability s{InitValue};", CustomAttrs);
9607 }
9608 
9609 TEST_F(FormatTest, UnderstandsPointerQualifiersInCast) {
9610   // Check that qualifiers on pointers don't break parsing of casts.
9611   verifyFormat("x = (foo *const)*v;");
9612   verifyFormat("x = (foo *volatile)*v;");
9613   verifyFormat("x = (foo *restrict)*v;");
9614   verifyFormat("x = (foo *__attribute__((foo)))*v;");
9615   verifyFormat("x = (foo *_Nonnull)*v;");
9616   verifyFormat("x = (foo *_Nullable)*v;");
9617   verifyFormat("x = (foo *_Null_unspecified)*v;");
9618   verifyFormat("x = (foo *_Nonnull)*v;");
9619   verifyFormat("x = (foo *[[clang::attr]])*v;");
9620   verifyFormat("x = (foo *[[clang::attr(\"foo\")]])*v;");
9621   verifyFormat("x = (foo *__ptr32)*v;");
9622   verifyFormat("x = (foo *__ptr64)*v;");
9623   verifyFormat("x = (foo *__capability)*v;");
9624 
9625   // Check that we handle multiple trailing qualifiers and skip them all to
9626   // determine that the expression is a cast to a pointer type.
9627   FormatStyle LongPointerRight = getLLVMStyleWithColumns(999);
9628   FormatStyle LongPointerLeft = getLLVMStyleWithColumns(999);
9629   LongPointerLeft.PointerAlignment = FormatStyle::PAS_Left;
9630   StringRef AllQualifiers =
9631       "const volatile restrict __attribute__((foo)) _Nonnull _Null_unspecified "
9632       "_Nonnull [[clang::attr]] __ptr32 __ptr64 __capability";
9633   verifyFormat(("x = (foo *" + AllQualifiers + ")*v;").str(), LongPointerRight);
9634   verifyFormat(("x = (foo* " + AllQualifiers + ")*v;").str(), LongPointerLeft);
9635 
9636   // Also check that address-of is not parsed as a binary bitwise-and:
9637   verifyFormat("x = (foo *const)&v;");
9638   verifyFormat(("x = (foo *" + AllQualifiers + ")&v;").str(), LongPointerRight);
9639   verifyFormat(("x = (foo* " + AllQualifiers + ")&v;").str(), LongPointerLeft);
9640 
9641   // Check custom qualifiers:
9642   FormatStyle CustomQualifier = getLLVMStyleWithColumns(999);
9643   CustomQualifier.AttributeMacros.push_back("__my_qualifier");
9644   verifyFormat("x = (foo * __my_qualifier) * v;"); // not parsed as qualifier.
9645   verifyFormat("x = (foo *__my_qualifier)*v;", CustomQualifier);
9646   verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)*v;").str(),
9647                CustomQualifier);
9648   verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)&v;").str(),
9649                CustomQualifier);
9650 
9651   // Check that unknown identifiers result in binary operator parsing:
9652   verifyFormat("x = (foo * __unknown_qualifier) * v;");
9653   verifyFormat("x = (foo * __unknown_qualifier) & v;");
9654 }
9655 
9656 TEST_F(FormatTest, UnderstandsSquareAttributes) {
9657   verifyFormat("SomeType s [[unused]] (InitValue);");
9658   verifyFormat("SomeType s [[gnu::unused]] (InitValue);");
9659   verifyFormat("SomeType s [[using gnu: unused]] (InitValue);");
9660   verifyFormat("[[gsl::suppress(\"clang-tidy-check-name\")]] void f() {}");
9661   verifyFormat("void f() [[deprecated(\"so sorry\")]];");
9662   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9663                "    [[unused]] aaaaaaaaaaaaaaaaaaaaaaa(int i);");
9664   verifyFormat("[[nodiscard]] bool f() { return false; }");
9665   verifyFormat("class [[nodiscard]] f {\npublic:\n  f() {}\n}");
9666   verifyFormat("class [[deprecated(\"so sorry\")]] f {\npublic:\n  f() {}\n}");
9667   verifyFormat("class [[gnu::unused]] f {\npublic:\n  f() {}\n}");
9668 
9669   // Make sure we do not mistake attributes for array subscripts.
9670   verifyFormat("int a() {}\n"
9671                "[[unused]] int b() {}\n");
9672   verifyFormat("NSArray *arr;\n"
9673                "arr[[Foo() bar]];");
9674 
9675   // On the other hand, we still need to correctly find array subscripts.
9676   verifyFormat("int a = std::vector<int>{1, 2, 3}[0];");
9677 
9678   // Make sure that we do not mistake Objective-C method inside array literals
9679   // as attributes, even if those method names are also keywords.
9680   verifyFormat("@[ [foo bar] ];");
9681   verifyFormat("@[ [NSArray class] ];");
9682   verifyFormat("@[ [foo enum] ];");
9683 
9684   verifyFormat("template <typename T> [[nodiscard]] int a() { return 1; }");
9685 
9686   // Make sure we do not parse attributes as lambda introducers.
9687   FormatStyle MultiLineFunctions = getLLVMStyle();
9688   MultiLineFunctions.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
9689   verifyFormat("[[unused]] int b() {\n"
9690                "  return 42;\n"
9691                "}\n",
9692                MultiLineFunctions);
9693 }
9694 
9695 TEST_F(FormatTest, AttributeClass) {
9696   FormatStyle Style = getChromiumStyle(FormatStyle::LK_Cpp);
9697   verifyFormat("class S {\n"
9698                "  S(S&&) = default;\n"
9699                "};",
9700                Style);
9701   verifyFormat("class [[nodiscard]] S {\n"
9702                "  S(S&&) = default;\n"
9703                "};",
9704                Style);
9705   verifyFormat("class __attribute((maybeunused)) S {\n"
9706                "  S(S&&) = default;\n"
9707                "};",
9708                Style);
9709   verifyFormat("struct S {\n"
9710                "  S(S&&) = default;\n"
9711                "};",
9712                Style);
9713   verifyFormat("struct [[nodiscard]] S {\n"
9714                "  S(S&&) = default;\n"
9715                "};",
9716                Style);
9717 }
9718 
9719 TEST_F(FormatTest, AttributesAfterMacro) {
9720   FormatStyle Style = getLLVMStyle();
9721   verifyFormat("MACRO;\n"
9722                "__attribute__((maybe_unused)) int foo() {\n"
9723                "  //...\n"
9724                "}");
9725 
9726   verifyFormat("MACRO;\n"
9727                "[[nodiscard]] int foo() {\n"
9728                "  //...\n"
9729                "}");
9730 
9731   EXPECT_EQ("MACRO\n\n"
9732             "__attribute__((maybe_unused)) int foo() {\n"
9733             "  //...\n"
9734             "}",
9735             format("MACRO\n\n"
9736                    "__attribute__((maybe_unused)) int foo() {\n"
9737                    "  //...\n"
9738                    "}"));
9739 
9740   EXPECT_EQ("MACRO\n\n"
9741             "[[nodiscard]] int foo() {\n"
9742             "  //...\n"
9743             "}",
9744             format("MACRO\n\n"
9745                    "[[nodiscard]] int foo() {\n"
9746                    "  //...\n"
9747                    "}"));
9748 }
9749 
9750 TEST_F(FormatTest, AttributePenaltyBreaking) {
9751   FormatStyle Style = getLLVMStyle();
9752   verifyFormat("void ABCDEFGH::ABCDEFGHIJKLMN(\n"
9753                "    [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}",
9754                Style);
9755   verifyFormat("void ABCDEFGH::ABCDEFGHIJK(\n"
9756                "    [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}",
9757                Style);
9758   verifyFormat("void ABCDEFGH::ABCDEFGH([[maybe_unused]] const "
9759                "shared_ptr<ALongTypeName> &C d) {\n}",
9760                Style);
9761 }
9762 
9763 TEST_F(FormatTest, UnderstandsEllipsis) {
9764   FormatStyle Style = getLLVMStyle();
9765   verifyFormat("int printf(const char *fmt, ...);");
9766   verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
9767   verifyFormat("template <class... Ts> void Foo(Ts *...ts) {}");
9768 
9769   verifyFormat("template <int *...PP> a;", Style);
9770 
9771   Style.PointerAlignment = FormatStyle::PAS_Left;
9772   verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", Style);
9773 
9774   verifyFormat("template <int*... PP> a;", Style);
9775 
9776   Style.PointerAlignment = FormatStyle::PAS_Middle;
9777   verifyFormat("template <int *... PP> a;", Style);
9778 }
9779 
9780 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
9781   EXPECT_EQ("int *a;\n"
9782             "int *a;\n"
9783             "int *a;",
9784             format("int *a;\n"
9785                    "int* a;\n"
9786                    "int *a;",
9787                    getGoogleStyle()));
9788   EXPECT_EQ("int* a;\n"
9789             "int* a;\n"
9790             "int* a;",
9791             format("int* a;\n"
9792                    "int* a;\n"
9793                    "int *a;",
9794                    getGoogleStyle()));
9795   EXPECT_EQ("int *a;\n"
9796             "int *a;\n"
9797             "int *a;",
9798             format("int *a;\n"
9799                    "int * a;\n"
9800                    "int *  a;",
9801                    getGoogleStyle()));
9802   EXPECT_EQ("auto x = [] {\n"
9803             "  int *a;\n"
9804             "  int *a;\n"
9805             "  int *a;\n"
9806             "};",
9807             format("auto x=[]{int *a;\n"
9808                    "int * a;\n"
9809                    "int *  a;};",
9810                    getGoogleStyle()));
9811 }
9812 
9813 TEST_F(FormatTest, UnderstandsRvalueReferences) {
9814   verifyFormat("int f(int &&a) {}");
9815   verifyFormat("int f(int a, char &&b) {}");
9816   verifyFormat("void f() { int &&a = b; }");
9817   verifyGoogleFormat("int f(int a, char&& b) {}");
9818   verifyGoogleFormat("void f() { int&& a = b; }");
9819 
9820   verifyIndependentOfContext("A<int &&> a;");
9821   verifyIndependentOfContext("A<int &&, int &&> a;");
9822   verifyGoogleFormat("A<int&&> a;");
9823   verifyGoogleFormat("A<int&&, int&&> a;");
9824 
9825   // Not rvalue references:
9826   verifyFormat("template <bool B, bool C> class A {\n"
9827                "  static_assert(B && C, \"Something is wrong\");\n"
9828                "};");
9829   verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
9830   verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
9831   verifyFormat("#define A(a, b) (a && b)");
9832 }
9833 
9834 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
9835   verifyFormat("void f() {\n"
9836                "  x[aaaaaaaaa -\n"
9837                "    b] = 23;\n"
9838                "}",
9839                getLLVMStyleWithColumns(15));
9840 }
9841 
9842 TEST_F(FormatTest, FormatsCasts) {
9843   verifyFormat("Type *A = static_cast<Type *>(P);");
9844   verifyFormat("Type *A = (Type *)P;");
9845   verifyFormat("Type *A = (vector<Type *, int *>)P;");
9846   verifyFormat("int a = (int)(2.0f);");
9847   verifyFormat("int a = (int)2.0f;");
9848   verifyFormat("x[(int32)y];");
9849   verifyFormat("x = (int32)y;");
9850   verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
9851   verifyFormat("int a = (int)*b;");
9852   verifyFormat("int a = (int)2.0f;");
9853   verifyFormat("int a = (int)~0;");
9854   verifyFormat("int a = (int)++a;");
9855   verifyFormat("int a = (int)sizeof(int);");
9856   verifyFormat("int a = (int)+2;");
9857   verifyFormat("my_int a = (my_int)2.0f;");
9858   verifyFormat("my_int a = (my_int)sizeof(int);");
9859   verifyFormat("return (my_int)aaa;");
9860   verifyFormat("#define x ((int)-1)");
9861   verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
9862   verifyFormat("#define p(q) ((int *)&q)");
9863   verifyFormat("fn(a)(b) + 1;");
9864 
9865   verifyFormat("void f() { my_int a = (my_int)*b; }");
9866   verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
9867   verifyFormat("my_int a = (my_int)~0;");
9868   verifyFormat("my_int a = (my_int)++a;");
9869   verifyFormat("my_int a = (my_int)-2;");
9870   verifyFormat("my_int a = (my_int)1;");
9871   verifyFormat("my_int a = (my_int *)1;");
9872   verifyFormat("my_int a = (const my_int)-1;");
9873   verifyFormat("my_int a = (const my_int *)-1;");
9874   verifyFormat("my_int a = (my_int)(my_int)-1;");
9875   verifyFormat("my_int a = (ns::my_int)-2;");
9876   verifyFormat("case (my_int)ONE:");
9877   verifyFormat("auto x = (X)this;");
9878   // Casts in Obj-C style calls used to not be recognized as such.
9879   verifyFormat("int a = [(type*)[((type*)val) arg] arg];", getGoogleStyle());
9880 
9881   // FIXME: single value wrapped with paren will be treated as cast.
9882   verifyFormat("void f(int i = (kValue)*kMask) {}");
9883 
9884   verifyFormat("{ (void)F; }");
9885 
9886   // Don't break after a cast's
9887   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
9888                "    (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
9889                "                                   bbbbbbbbbbbbbbbbbbbbbb);");
9890 
9891   // These are not casts.
9892   verifyFormat("void f(int *) {}");
9893   verifyFormat("f(foo)->b;");
9894   verifyFormat("f(foo).b;");
9895   verifyFormat("f(foo)(b);");
9896   verifyFormat("f(foo)[b];");
9897   verifyFormat("[](foo) { return 4; }(bar);");
9898   verifyFormat("(*funptr)(foo)[4];");
9899   verifyFormat("funptrs[4](foo)[4];");
9900   verifyFormat("void f(int *);");
9901   verifyFormat("void f(int *) = 0;");
9902   verifyFormat("void f(SmallVector<int>) {}");
9903   verifyFormat("void f(SmallVector<int>);");
9904   verifyFormat("void f(SmallVector<int>) = 0;");
9905   verifyFormat("void f(int i = (kA * kB) & kMask) {}");
9906   verifyFormat("int a = sizeof(int) * b;");
9907   verifyFormat("int a = alignof(int) * b;", getGoogleStyle());
9908   verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
9909   verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
9910   verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
9911 
9912   // These are not casts, but at some point were confused with casts.
9913   verifyFormat("virtual void foo(int *) override;");
9914   verifyFormat("virtual void foo(char &) const;");
9915   verifyFormat("virtual void foo(int *a, char *) const;");
9916   verifyFormat("int a = sizeof(int *) + b;");
9917   verifyFormat("int a = alignof(int *) + b;", getGoogleStyle());
9918   verifyFormat("bool b = f(g<int>) && c;");
9919   verifyFormat("typedef void (*f)(int i) func;");
9920   verifyFormat("void operator++(int) noexcept;");
9921   verifyFormat("void operator++(int &) noexcept;");
9922   verifyFormat("void operator delete(void *, std::size_t, const std::nothrow_t "
9923                "&) noexcept;");
9924   verifyFormat(
9925       "void operator delete(std::size_t, const std::nothrow_t &) noexcept;");
9926   verifyFormat("void operator delete(const std::nothrow_t &) noexcept;");
9927   verifyFormat("void operator delete(std::nothrow_t &) noexcept;");
9928   verifyFormat("void operator delete(nothrow_t &) noexcept;");
9929   verifyFormat("void operator delete(foo &) noexcept;");
9930   verifyFormat("void operator delete(foo) noexcept;");
9931   verifyFormat("void operator delete(int) noexcept;");
9932   verifyFormat("void operator delete(int &) noexcept;");
9933   verifyFormat("void operator delete(int &) volatile noexcept;");
9934   verifyFormat("void operator delete(int &) const");
9935   verifyFormat("void operator delete(int &) = default");
9936   verifyFormat("void operator delete(int &) = delete");
9937   verifyFormat("void operator delete(int &) [[noreturn]]");
9938   verifyFormat("void operator delete(int &) throw();");
9939   verifyFormat("void operator delete(int &) throw(int);");
9940   verifyFormat("auto operator delete(int &) -> int;");
9941   verifyFormat("auto operator delete(int &) override");
9942   verifyFormat("auto operator delete(int &) final");
9943 
9944   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
9945                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
9946   // FIXME: The indentation here is not ideal.
9947   verifyFormat(
9948       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9949       "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
9950       "        [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
9951 }
9952 
9953 TEST_F(FormatTest, FormatsFunctionTypes) {
9954   verifyFormat("A<bool()> a;");
9955   verifyFormat("A<SomeType()> a;");
9956   verifyFormat("A<void (*)(int, std::string)> a;");
9957   verifyFormat("A<void *(int)>;");
9958   verifyFormat("void *(*a)(int *, SomeType *);");
9959   verifyFormat("int (*func)(void *);");
9960   verifyFormat("void f() { int (*func)(void *); }");
9961   verifyFormat("template <class CallbackClass>\n"
9962                "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
9963 
9964   verifyGoogleFormat("A<void*(int*, SomeType*)>;");
9965   verifyGoogleFormat("void* (*a)(int);");
9966   verifyGoogleFormat(
9967       "template <class CallbackClass>\n"
9968       "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
9969 
9970   // Other constructs can look somewhat like function types:
9971   verifyFormat("A<sizeof(*x)> a;");
9972   verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
9973   verifyFormat("some_var = function(*some_pointer_var)[0];");
9974   verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
9975   verifyFormat("int x = f(&h)();");
9976   verifyFormat("returnsFunction(&param1, &param2)(param);");
9977   verifyFormat("std::function<\n"
9978                "    LooooooooooongTemplatedType<\n"
9979                "        SomeType>*(\n"
9980                "        LooooooooooooooooongType type)>\n"
9981                "    function;",
9982                getGoogleStyleWithColumns(40));
9983 }
9984 
9985 TEST_F(FormatTest, FormatsPointersToArrayTypes) {
9986   verifyFormat("A (*foo_)[6];");
9987   verifyFormat("vector<int> (*foo_)[6];");
9988 }
9989 
9990 TEST_F(FormatTest, BreaksLongVariableDeclarations) {
9991   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
9992                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
9993   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
9994                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
9995   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
9996                "    *LoooooooooooooooooooooooooooooooooooooooongVariable;");
9997 
9998   // Different ways of ()-initializiation.
9999   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
10000                "    LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
10001   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
10002                "    LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
10003   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
10004                "    LoooooooooooooooooooooooooooooooooooooooongVariable({});");
10005   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
10006                "    LoooooooooooooooooooooooooooooooooooooongVariable([A a]);");
10007 
10008   // Lambdas should not confuse the variable declaration heuristic.
10009   verifyFormat("LooooooooooooooooongType\n"
10010                "    variable(nullptr, [](A *a) {});",
10011                getLLVMStyleWithColumns(40));
10012 }
10013 
10014 TEST_F(FormatTest, BreaksLongDeclarations) {
10015   verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
10016                "    AnotherNameForTheLongType;");
10017   verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
10018                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10019   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10020                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
10021   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n"
10022                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
10023   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10024                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10025   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
10026                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10027   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
10028                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10029   verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
10030                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10031   verifyFormat("typeof(LoooooooooooooooooooooooooooooooooooooooooongName)\n"
10032                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10033   verifyFormat("_Atomic(LooooooooooooooooooooooooooooooooooooooooongName)\n"
10034                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10035   verifyFormat("__underlying_type(LooooooooooooooooooooooooooooooongName)\n"
10036                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10037   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10038                "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);");
10039   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10040                "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}");
10041   FormatStyle Indented = getLLVMStyle();
10042   Indented.IndentWrappedFunctionNames = true;
10043   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10044                "    LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
10045                Indented);
10046   verifyFormat(
10047       "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10048       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
10049       Indented);
10050   verifyFormat(
10051       "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
10052       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
10053       Indented);
10054   verifyFormat(
10055       "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
10056       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
10057       Indented);
10058 
10059   // FIXME: Without the comment, this breaks after "(".
10060   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType  // break\n"
10061                "    (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();",
10062                getGoogleStyle());
10063 
10064   verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
10065                "                  int LoooooooooooooooooooongParam2) {}");
10066   verifyFormat(
10067       "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
10068       "                                   SourceLocation L, IdentifierIn *II,\n"
10069       "                                   Type *T) {}");
10070   verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
10071                "ReallyReaaallyLongFunctionName(\n"
10072                "    const std::string &SomeParameter,\n"
10073                "    const SomeType<string, SomeOtherTemplateParameter>\n"
10074                "        &ReallyReallyLongParameterName,\n"
10075                "    const SomeType<string, SomeOtherTemplateParameter>\n"
10076                "        &AnotherLongParameterName) {}");
10077   verifyFormat("template <typename A>\n"
10078                "SomeLoooooooooooooooooooooongType<\n"
10079                "    typename some_namespace::SomeOtherType<A>::Type>\n"
10080                "Function() {}");
10081 
10082   verifyGoogleFormat(
10083       "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
10084       "    aaaaaaaaaaaaaaaaaaaaaaa;");
10085   verifyGoogleFormat(
10086       "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
10087       "                                   SourceLocation L) {}");
10088   verifyGoogleFormat(
10089       "some_namespace::LongReturnType\n"
10090       "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
10091       "    int first_long_parameter, int second_parameter) {}");
10092 
10093   verifyGoogleFormat("template <typename T>\n"
10094                      "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
10095                      "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
10096   verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10097                      "                   int aaaaaaaaaaaaaaaaaaaaaaa);");
10098 
10099   verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
10100                "    const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10101                "        *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
10102   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10103                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
10104                "        aaaaaaaaaaaaaaaaaaaaaaaa);");
10105   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10106                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
10107                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n"
10108                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
10109 
10110   verifyFormat("template <typename T> // Templates on own line.\n"
10111                "static int            // Some comment.\n"
10112                "MyFunction(int a);",
10113                getLLVMStyle());
10114 }
10115 
10116 TEST_F(FormatTest, FormatsAccessModifiers) {
10117   FormatStyle Style = getLLVMStyle();
10118   EXPECT_EQ(Style.EmptyLineBeforeAccessModifier,
10119             FormatStyle::ELBAMS_LogicalBlock);
10120   verifyFormat("struct foo {\n"
10121                "private:\n"
10122                "  void f() {}\n"
10123                "\n"
10124                "private:\n"
10125                "  int i;\n"
10126                "\n"
10127                "protected:\n"
10128                "  int j;\n"
10129                "};\n",
10130                Style);
10131   verifyFormat("struct foo {\n"
10132                "private:\n"
10133                "  void f() {}\n"
10134                "\n"
10135                "private:\n"
10136                "  int i;\n"
10137                "\n"
10138                "protected:\n"
10139                "  int j;\n"
10140                "};\n",
10141                "struct foo {\n"
10142                "private:\n"
10143                "  void f() {}\n"
10144                "private:\n"
10145                "  int i;\n"
10146                "protected:\n"
10147                "  int j;\n"
10148                "};\n",
10149                Style);
10150   verifyFormat("struct foo { /* comment */\n"
10151                "private:\n"
10152                "  int i;\n"
10153                "  // comment\n"
10154                "private:\n"
10155                "  int j;\n"
10156                "};\n",
10157                Style);
10158   verifyFormat("struct foo {\n"
10159                "#ifdef FOO\n"
10160                "#endif\n"
10161                "private:\n"
10162                "  int i;\n"
10163                "#ifdef FOO\n"
10164                "private:\n"
10165                "#endif\n"
10166                "  int j;\n"
10167                "};\n",
10168                Style);
10169   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
10170   verifyFormat("struct foo {\n"
10171                "private:\n"
10172                "  void f() {}\n"
10173                "private:\n"
10174                "  int i;\n"
10175                "protected:\n"
10176                "  int j;\n"
10177                "};\n",
10178                Style);
10179   verifyFormat("struct foo {\n"
10180                "private:\n"
10181                "  void f() {}\n"
10182                "private:\n"
10183                "  int i;\n"
10184                "protected:\n"
10185                "  int j;\n"
10186                "};\n",
10187                "struct foo {\n"
10188                "\n"
10189                "private:\n"
10190                "  void f() {}\n"
10191                "\n"
10192                "private:\n"
10193                "  int i;\n"
10194                "\n"
10195                "protected:\n"
10196                "  int j;\n"
10197                "};\n",
10198                Style);
10199   verifyFormat("struct foo { /* comment */\n"
10200                "private:\n"
10201                "  int i;\n"
10202                "  // comment\n"
10203                "private:\n"
10204                "  int j;\n"
10205                "};\n",
10206                "struct foo { /* comment */\n"
10207                "\n"
10208                "private:\n"
10209                "  int i;\n"
10210                "  // comment\n"
10211                "\n"
10212                "private:\n"
10213                "  int j;\n"
10214                "};\n",
10215                Style);
10216   verifyFormat("struct foo {\n"
10217                "#ifdef FOO\n"
10218                "#endif\n"
10219                "private:\n"
10220                "  int i;\n"
10221                "#ifdef FOO\n"
10222                "private:\n"
10223                "#endif\n"
10224                "  int j;\n"
10225                "};\n",
10226                "struct foo {\n"
10227                "#ifdef FOO\n"
10228                "#endif\n"
10229                "\n"
10230                "private:\n"
10231                "  int i;\n"
10232                "#ifdef FOO\n"
10233                "\n"
10234                "private:\n"
10235                "#endif\n"
10236                "  int j;\n"
10237                "};\n",
10238                Style);
10239   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
10240   verifyFormat("struct foo {\n"
10241                "private:\n"
10242                "  void f() {}\n"
10243                "\n"
10244                "private:\n"
10245                "  int i;\n"
10246                "\n"
10247                "protected:\n"
10248                "  int j;\n"
10249                "};\n",
10250                Style);
10251   verifyFormat("struct foo {\n"
10252                "private:\n"
10253                "  void f() {}\n"
10254                "\n"
10255                "private:\n"
10256                "  int i;\n"
10257                "\n"
10258                "protected:\n"
10259                "  int j;\n"
10260                "};\n",
10261                "struct foo {\n"
10262                "private:\n"
10263                "  void f() {}\n"
10264                "private:\n"
10265                "  int i;\n"
10266                "protected:\n"
10267                "  int j;\n"
10268                "};\n",
10269                Style);
10270   verifyFormat("struct foo { /* comment */\n"
10271                "private:\n"
10272                "  int i;\n"
10273                "  // comment\n"
10274                "\n"
10275                "private:\n"
10276                "  int j;\n"
10277                "};\n",
10278                "struct foo { /* comment */\n"
10279                "private:\n"
10280                "  int i;\n"
10281                "  // comment\n"
10282                "\n"
10283                "private:\n"
10284                "  int j;\n"
10285                "};\n",
10286                Style);
10287   verifyFormat("struct foo {\n"
10288                "#ifdef FOO\n"
10289                "#endif\n"
10290                "\n"
10291                "private:\n"
10292                "  int i;\n"
10293                "#ifdef FOO\n"
10294                "\n"
10295                "private:\n"
10296                "#endif\n"
10297                "  int j;\n"
10298                "};\n",
10299                "struct foo {\n"
10300                "#ifdef FOO\n"
10301                "#endif\n"
10302                "private:\n"
10303                "  int i;\n"
10304                "#ifdef FOO\n"
10305                "private:\n"
10306                "#endif\n"
10307                "  int j;\n"
10308                "};\n",
10309                Style);
10310   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
10311   EXPECT_EQ("struct foo {\n"
10312             "\n"
10313             "private:\n"
10314             "  void f() {}\n"
10315             "\n"
10316             "private:\n"
10317             "  int i;\n"
10318             "\n"
10319             "protected:\n"
10320             "  int j;\n"
10321             "};\n",
10322             format("struct foo {\n"
10323                    "\n"
10324                    "private:\n"
10325                    "  void f() {}\n"
10326                    "\n"
10327                    "private:\n"
10328                    "  int i;\n"
10329                    "\n"
10330                    "protected:\n"
10331                    "  int j;\n"
10332                    "};\n",
10333                    Style));
10334   verifyFormat("struct foo {\n"
10335                "private:\n"
10336                "  void f() {}\n"
10337                "private:\n"
10338                "  int i;\n"
10339                "protected:\n"
10340                "  int j;\n"
10341                "};\n",
10342                Style);
10343   EXPECT_EQ("struct foo { /* comment */\n"
10344             "\n"
10345             "private:\n"
10346             "  int i;\n"
10347             "  // comment\n"
10348             "\n"
10349             "private:\n"
10350             "  int j;\n"
10351             "};\n",
10352             format("struct foo { /* comment */\n"
10353                    "\n"
10354                    "private:\n"
10355                    "  int i;\n"
10356                    "  // comment\n"
10357                    "\n"
10358                    "private:\n"
10359                    "  int j;\n"
10360                    "};\n",
10361                    Style));
10362   verifyFormat("struct foo { /* comment */\n"
10363                "private:\n"
10364                "  int i;\n"
10365                "  // comment\n"
10366                "private:\n"
10367                "  int j;\n"
10368                "};\n",
10369                Style);
10370   EXPECT_EQ("struct foo {\n"
10371             "#ifdef FOO\n"
10372             "#endif\n"
10373             "\n"
10374             "private:\n"
10375             "  int i;\n"
10376             "#ifdef FOO\n"
10377             "\n"
10378             "private:\n"
10379             "#endif\n"
10380             "  int j;\n"
10381             "};\n",
10382             format("struct foo {\n"
10383                    "#ifdef FOO\n"
10384                    "#endif\n"
10385                    "\n"
10386                    "private:\n"
10387                    "  int i;\n"
10388                    "#ifdef FOO\n"
10389                    "\n"
10390                    "private:\n"
10391                    "#endif\n"
10392                    "  int j;\n"
10393                    "};\n",
10394                    Style));
10395   verifyFormat("struct foo {\n"
10396                "#ifdef FOO\n"
10397                "#endif\n"
10398                "private:\n"
10399                "  int i;\n"
10400                "#ifdef FOO\n"
10401                "private:\n"
10402                "#endif\n"
10403                "  int j;\n"
10404                "};\n",
10405                Style);
10406 
10407   FormatStyle NoEmptyLines = getLLVMStyle();
10408   NoEmptyLines.MaxEmptyLinesToKeep = 0;
10409   verifyFormat("struct foo {\n"
10410                "private:\n"
10411                "  void f() {}\n"
10412                "\n"
10413                "private:\n"
10414                "  int i;\n"
10415                "\n"
10416                "public:\n"
10417                "protected:\n"
10418                "  int j;\n"
10419                "};\n",
10420                NoEmptyLines);
10421 
10422   NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
10423   verifyFormat("struct foo {\n"
10424                "private:\n"
10425                "  void f() {}\n"
10426                "private:\n"
10427                "  int i;\n"
10428                "public:\n"
10429                "protected:\n"
10430                "  int j;\n"
10431                "};\n",
10432                NoEmptyLines);
10433 
10434   NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
10435   verifyFormat("struct foo {\n"
10436                "private:\n"
10437                "  void f() {}\n"
10438                "\n"
10439                "private:\n"
10440                "  int i;\n"
10441                "\n"
10442                "public:\n"
10443                "\n"
10444                "protected:\n"
10445                "  int j;\n"
10446                "};\n",
10447                NoEmptyLines);
10448 }
10449 
10450 TEST_F(FormatTest, FormatsAfterAccessModifiers) {
10451 
10452   FormatStyle Style = getLLVMStyle();
10453   EXPECT_EQ(Style.EmptyLineAfterAccessModifier, FormatStyle::ELAAMS_Never);
10454   verifyFormat("struct foo {\n"
10455                "private:\n"
10456                "  void f() {}\n"
10457                "\n"
10458                "private:\n"
10459                "  int i;\n"
10460                "\n"
10461                "protected:\n"
10462                "  int j;\n"
10463                "};\n",
10464                Style);
10465 
10466   // Check if lines are removed.
10467   verifyFormat("struct foo {\n"
10468                "private:\n"
10469                "  void f() {}\n"
10470                "\n"
10471                "private:\n"
10472                "  int i;\n"
10473                "\n"
10474                "protected:\n"
10475                "  int j;\n"
10476                "};\n",
10477                "struct foo {\n"
10478                "private:\n"
10479                "\n"
10480                "  void f() {}\n"
10481                "\n"
10482                "private:\n"
10483                "\n"
10484                "  int i;\n"
10485                "\n"
10486                "protected:\n"
10487                "\n"
10488                "  int j;\n"
10489                "};\n",
10490                Style);
10491 
10492   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
10493   verifyFormat("struct foo {\n"
10494                "private:\n"
10495                "\n"
10496                "  void f() {}\n"
10497                "\n"
10498                "private:\n"
10499                "\n"
10500                "  int i;\n"
10501                "\n"
10502                "protected:\n"
10503                "\n"
10504                "  int j;\n"
10505                "};\n",
10506                Style);
10507 
10508   // Check if lines are added.
10509   verifyFormat("struct foo {\n"
10510                "private:\n"
10511                "\n"
10512                "  void f() {}\n"
10513                "\n"
10514                "private:\n"
10515                "\n"
10516                "  int i;\n"
10517                "\n"
10518                "protected:\n"
10519                "\n"
10520                "  int j;\n"
10521                "};\n",
10522                "struct foo {\n"
10523                "private:\n"
10524                "  void f() {}\n"
10525                "\n"
10526                "private:\n"
10527                "  int i;\n"
10528                "\n"
10529                "protected:\n"
10530                "  int j;\n"
10531                "};\n",
10532                Style);
10533 
10534   // Leave tests rely on the code layout, test::messUp can not be used.
10535   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
10536   Style.MaxEmptyLinesToKeep = 0u;
10537   verifyFormat("struct foo {\n"
10538                "private:\n"
10539                "  void f() {}\n"
10540                "\n"
10541                "private:\n"
10542                "  int i;\n"
10543                "\n"
10544                "protected:\n"
10545                "  int j;\n"
10546                "};\n",
10547                Style);
10548 
10549   // Check if MaxEmptyLinesToKeep is respected.
10550   EXPECT_EQ("struct foo {\n"
10551             "private:\n"
10552             "  void f() {}\n"
10553             "\n"
10554             "private:\n"
10555             "  int i;\n"
10556             "\n"
10557             "protected:\n"
10558             "  int j;\n"
10559             "};\n",
10560             format("struct foo {\n"
10561                    "private:\n"
10562                    "\n\n\n"
10563                    "  void f() {}\n"
10564                    "\n"
10565                    "private:\n"
10566                    "\n\n\n"
10567                    "  int i;\n"
10568                    "\n"
10569                    "protected:\n"
10570                    "\n\n\n"
10571                    "  int j;\n"
10572                    "};\n",
10573                    Style));
10574 
10575   Style.MaxEmptyLinesToKeep = 1u;
10576   EXPECT_EQ("struct foo {\n"
10577             "private:\n"
10578             "\n"
10579             "  void f() {}\n"
10580             "\n"
10581             "private:\n"
10582             "\n"
10583             "  int i;\n"
10584             "\n"
10585             "protected:\n"
10586             "\n"
10587             "  int j;\n"
10588             "};\n",
10589             format("struct foo {\n"
10590                    "private:\n"
10591                    "\n"
10592                    "  void f() {}\n"
10593                    "\n"
10594                    "private:\n"
10595                    "\n"
10596                    "  int i;\n"
10597                    "\n"
10598                    "protected:\n"
10599                    "\n"
10600                    "  int j;\n"
10601                    "};\n",
10602                    Style));
10603   // Check if no lines are kept.
10604   EXPECT_EQ("struct foo {\n"
10605             "private:\n"
10606             "  void f() {}\n"
10607             "\n"
10608             "private:\n"
10609             "  int i;\n"
10610             "\n"
10611             "protected:\n"
10612             "  int j;\n"
10613             "};\n",
10614             format("struct foo {\n"
10615                    "private:\n"
10616                    "  void f() {}\n"
10617                    "\n"
10618                    "private:\n"
10619                    "  int i;\n"
10620                    "\n"
10621                    "protected:\n"
10622                    "  int j;\n"
10623                    "};\n",
10624                    Style));
10625   // Check if MaxEmptyLinesToKeep is respected.
10626   EXPECT_EQ("struct foo {\n"
10627             "private:\n"
10628             "\n"
10629             "  void f() {}\n"
10630             "\n"
10631             "private:\n"
10632             "\n"
10633             "  int i;\n"
10634             "\n"
10635             "protected:\n"
10636             "\n"
10637             "  int j;\n"
10638             "};\n",
10639             format("struct foo {\n"
10640                    "private:\n"
10641                    "\n\n\n"
10642                    "  void f() {}\n"
10643                    "\n"
10644                    "private:\n"
10645                    "\n\n\n"
10646                    "  int i;\n"
10647                    "\n"
10648                    "protected:\n"
10649                    "\n\n\n"
10650                    "  int j;\n"
10651                    "};\n",
10652                    Style));
10653 
10654   Style.MaxEmptyLinesToKeep = 10u;
10655   EXPECT_EQ("struct foo {\n"
10656             "private:\n"
10657             "\n\n\n"
10658             "  void f() {}\n"
10659             "\n"
10660             "private:\n"
10661             "\n\n\n"
10662             "  int i;\n"
10663             "\n"
10664             "protected:\n"
10665             "\n\n\n"
10666             "  int j;\n"
10667             "};\n",
10668             format("struct foo {\n"
10669                    "private:\n"
10670                    "\n\n\n"
10671                    "  void f() {}\n"
10672                    "\n"
10673                    "private:\n"
10674                    "\n\n\n"
10675                    "  int i;\n"
10676                    "\n"
10677                    "protected:\n"
10678                    "\n\n\n"
10679                    "  int j;\n"
10680                    "};\n",
10681                    Style));
10682 
10683   // Test with comments.
10684   Style = getLLVMStyle();
10685   verifyFormat("struct foo {\n"
10686                "private:\n"
10687                "  // comment\n"
10688                "  void f() {}\n"
10689                "\n"
10690                "private: /* comment */\n"
10691                "  int i;\n"
10692                "};\n",
10693                Style);
10694   verifyFormat("struct foo {\n"
10695                "private:\n"
10696                "  // comment\n"
10697                "  void f() {}\n"
10698                "\n"
10699                "private: /* comment */\n"
10700                "  int i;\n"
10701                "};\n",
10702                "struct foo {\n"
10703                "private:\n"
10704                "\n"
10705                "  // comment\n"
10706                "  void f() {}\n"
10707                "\n"
10708                "private: /* comment */\n"
10709                "\n"
10710                "  int i;\n"
10711                "};\n",
10712                Style);
10713 
10714   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
10715   verifyFormat("struct foo {\n"
10716                "private:\n"
10717                "\n"
10718                "  // comment\n"
10719                "  void f() {}\n"
10720                "\n"
10721                "private: /* comment */\n"
10722                "\n"
10723                "  int i;\n"
10724                "};\n",
10725                "struct foo {\n"
10726                "private:\n"
10727                "  // comment\n"
10728                "  void f() {}\n"
10729                "\n"
10730                "private: /* comment */\n"
10731                "  int i;\n"
10732                "};\n",
10733                Style);
10734   verifyFormat("struct foo {\n"
10735                "private:\n"
10736                "\n"
10737                "  // comment\n"
10738                "  void f() {}\n"
10739                "\n"
10740                "private: /* comment */\n"
10741                "\n"
10742                "  int i;\n"
10743                "};\n",
10744                Style);
10745 
10746   // Test with preprocessor defines.
10747   Style = getLLVMStyle();
10748   verifyFormat("struct foo {\n"
10749                "private:\n"
10750                "#ifdef FOO\n"
10751                "#endif\n"
10752                "  void f() {}\n"
10753                "};\n",
10754                Style);
10755   verifyFormat("struct foo {\n"
10756                "private:\n"
10757                "#ifdef FOO\n"
10758                "#endif\n"
10759                "  void f() {}\n"
10760                "};\n",
10761                "struct foo {\n"
10762                "private:\n"
10763                "\n"
10764                "#ifdef FOO\n"
10765                "#endif\n"
10766                "  void f() {}\n"
10767                "};\n",
10768                Style);
10769 
10770   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
10771   verifyFormat("struct foo {\n"
10772                "private:\n"
10773                "\n"
10774                "#ifdef FOO\n"
10775                "#endif\n"
10776                "  void f() {}\n"
10777                "};\n",
10778                "struct foo {\n"
10779                "private:\n"
10780                "#ifdef FOO\n"
10781                "#endif\n"
10782                "  void f() {}\n"
10783                "};\n",
10784                Style);
10785   verifyFormat("struct foo {\n"
10786                "private:\n"
10787                "\n"
10788                "#ifdef FOO\n"
10789                "#endif\n"
10790                "  void f() {}\n"
10791                "};\n",
10792                Style);
10793 }
10794 
10795 TEST_F(FormatTest, FormatsAfterAndBeforeAccessModifiersInteraction) {
10796   // Combined tests of EmptyLineAfterAccessModifier and
10797   // EmptyLineBeforeAccessModifier.
10798   FormatStyle Style = getLLVMStyle();
10799   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
10800   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
10801   verifyFormat("struct foo {\n"
10802                "private:\n"
10803                "\n"
10804                "protected:\n"
10805                "};\n",
10806                Style);
10807 
10808   Style.MaxEmptyLinesToKeep = 10u;
10809   // Both remove all new lines.
10810   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
10811   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
10812   verifyFormat("struct foo {\n"
10813                "private:\n"
10814                "protected:\n"
10815                "};\n",
10816                "struct foo {\n"
10817                "private:\n"
10818                "\n\n\n"
10819                "protected:\n"
10820                "};\n",
10821                Style);
10822 
10823   // Leave tests rely on the code layout, test::messUp can not be used.
10824   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
10825   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
10826   Style.MaxEmptyLinesToKeep = 10u;
10827   EXPECT_EQ("struct foo {\n"
10828             "private:\n"
10829             "\n\n\n"
10830             "protected:\n"
10831             "};\n",
10832             format("struct foo {\n"
10833                    "private:\n"
10834                    "\n\n\n"
10835                    "protected:\n"
10836                    "};\n",
10837                    Style));
10838   Style.MaxEmptyLinesToKeep = 3u;
10839   EXPECT_EQ("struct foo {\n"
10840             "private:\n"
10841             "\n\n\n"
10842             "protected:\n"
10843             "};\n",
10844             format("struct foo {\n"
10845                    "private:\n"
10846                    "\n\n\n"
10847                    "protected:\n"
10848                    "};\n",
10849                    Style));
10850   Style.MaxEmptyLinesToKeep = 1u;
10851   EXPECT_EQ("struct foo {\n"
10852             "private:\n"
10853             "\n\n\n"
10854             "protected:\n"
10855             "};\n",
10856             format("struct foo {\n"
10857                    "private:\n"
10858                    "\n\n\n"
10859                    "protected:\n"
10860                    "};\n",
10861                    Style)); // Based on new lines in original document and not
10862                             // on the setting.
10863 
10864   Style.MaxEmptyLinesToKeep = 10u;
10865   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
10866   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
10867   // Newlines are kept if they are greater than zero,
10868   // test::messUp removes all new lines which changes the logic
10869   EXPECT_EQ("struct foo {\n"
10870             "private:\n"
10871             "\n\n\n"
10872             "protected:\n"
10873             "};\n",
10874             format("struct foo {\n"
10875                    "private:\n"
10876                    "\n\n\n"
10877                    "protected:\n"
10878                    "};\n",
10879                    Style));
10880 
10881   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
10882   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
10883   // test::messUp removes all new lines which changes the logic
10884   EXPECT_EQ("struct foo {\n"
10885             "private:\n"
10886             "\n\n\n"
10887             "protected:\n"
10888             "};\n",
10889             format("struct foo {\n"
10890                    "private:\n"
10891                    "\n\n\n"
10892                    "protected:\n"
10893                    "};\n",
10894                    Style));
10895 
10896   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
10897   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
10898   EXPECT_EQ("struct foo {\n"
10899             "private:\n"
10900             "\n\n\n"
10901             "protected:\n"
10902             "};\n",
10903             format("struct foo {\n"
10904                    "private:\n"
10905                    "\n\n\n"
10906                    "protected:\n"
10907                    "};\n",
10908                    Style)); // test::messUp removes all new lines which changes
10909                             // the logic.
10910 
10911   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
10912   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
10913   verifyFormat("struct foo {\n"
10914                "private:\n"
10915                "protected:\n"
10916                "};\n",
10917                "struct foo {\n"
10918                "private:\n"
10919                "\n\n\n"
10920                "protected:\n"
10921                "};\n",
10922                Style);
10923 
10924   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
10925   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
10926   EXPECT_EQ("struct foo {\n"
10927             "private:\n"
10928             "\n\n\n"
10929             "protected:\n"
10930             "};\n",
10931             format("struct foo {\n"
10932                    "private:\n"
10933                    "\n\n\n"
10934                    "protected:\n"
10935                    "};\n",
10936                    Style)); // test::messUp removes all new lines which changes
10937                             // the logic.
10938 
10939   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
10940   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
10941   verifyFormat("struct foo {\n"
10942                "private:\n"
10943                "protected:\n"
10944                "};\n",
10945                "struct foo {\n"
10946                "private:\n"
10947                "\n\n\n"
10948                "protected:\n"
10949                "};\n",
10950                Style);
10951 
10952   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
10953   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
10954   verifyFormat("struct foo {\n"
10955                "private:\n"
10956                "protected:\n"
10957                "};\n",
10958                "struct foo {\n"
10959                "private:\n"
10960                "\n\n\n"
10961                "protected:\n"
10962                "};\n",
10963                Style);
10964 
10965   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
10966   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
10967   verifyFormat("struct foo {\n"
10968                "private:\n"
10969                "protected:\n"
10970                "};\n",
10971                "struct foo {\n"
10972                "private:\n"
10973                "\n\n\n"
10974                "protected:\n"
10975                "};\n",
10976                Style);
10977 
10978   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
10979   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
10980   verifyFormat("struct foo {\n"
10981                "private:\n"
10982                "protected:\n"
10983                "};\n",
10984                "struct foo {\n"
10985                "private:\n"
10986                "\n\n\n"
10987                "protected:\n"
10988                "};\n",
10989                Style);
10990 }
10991 
10992 TEST_F(FormatTest, FormatsArrays) {
10993   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
10994                "                         [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
10995   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n"
10996                "                         [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;");
10997   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n"
10998                "    aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}");
10999   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
11000                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
11001   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
11002                "    [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
11003   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
11004                "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
11005                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
11006   verifyFormat(
11007       "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
11008       "             << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
11009       "                                  [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
11010   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n"
11011                "    .aaaaaaaaaaaaaaaaaaaaaa();");
11012 
11013   verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
11014                      "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
11015   verifyFormat(
11016       "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
11017       "                                  .aaaaaaa[0]\n"
11018       "                                  .aaaaaaaaaaaaaaaaaaaaaa();");
11019   verifyFormat("a[::b::c];");
11020 
11021   verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10));
11022 
11023   FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0);
11024   verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit);
11025 }
11026 
11027 TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
11028   verifyFormat("(a)->b();");
11029   verifyFormat("--a;");
11030 }
11031 
11032 TEST_F(FormatTest, HandlesIncludeDirectives) {
11033   verifyFormat("#include <string>\n"
11034                "#include <a/b/c.h>\n"
11035                "#include \"a/b/string\"\n"
11036                "#include \"string.h\"\n"
11037                "#include \"string.h\"\n"
11038                "#include <a-a>\n"
11039                "#include < path with space >\n"
11040                "#include_next <test.h>"
11041                "#include \"abc.h\" // this is included for ABC\n"
11042                "#include \"some long include\" // with a comment\n"
11043                "#include \"some very long include path\"\n"
11044                "#include <some/very/long/include/path>\n",
11045                getLLVMStyleWithColumns(35));
11046   EXPECT_EQ("#include \"a.h\"", format("#include  \"a.h\""));
11047   EXPECT_EQ("#include <a>", format("#include<a>"));
11048 
11049   verifyFormat("#import <string>");
11050   verifyFormat("#import <a/b/c.h>");
11051   verifyFormat("#import \"a/b/string\"");
11052   verifyFormat("#import \"string.h\"");
11053   verifyFormat("#import \"string.h\"");
11054   verifyFormat("#if __has_include(<strstream>)\n"
11055                "#include <strstream>\n"
11056                "#endif");
11057 
11058   verifyFormat("#define MY_IMPORT <a/b>");
11059 
11060   verifyFormat("#if __has_include(<a/b>)");
11061   verifyFormat("#if __has_include_next(<a/b>)");
11062   verifyFormat("#define F __has_include(<a/b>)");
11063   verifyFormat("#define F __has_include_next(<a/b>)");
11064 
11065   // Protocol buffer definition or missing "#".
11066   verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
11067                getLLVMStyleWithColumns(30));
11068 
11069   FormatStyle Style = getLLVMStyle();
11070   Style.AlwaysBreakBeforeMultilineStrings = true;
11071   Style.ColumnLimit = 0;
11072   verifyFormat("#import \"abc.h\"", Style);
11073 
11074   // But 'import' might also be a regular C++ namespace.
11075   verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
11076                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
11077 }
11078 
11079 //===----------------------------------------------------------------------===//
11080 // Error recovery tests.
11081 //===----------------------------------------------------------------------===//
11082 
11083 TEST_F(FormatTest, IncompleteParameterLists) {
11084   FormatStyle NoBinPacking = getLLVMStyle();
11085   NoBinPacking.BinPackParameters = false;
11086   verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
11087                "                        double *min_x,\n"
11088                "                        double *max_x,\n"
11089                "                        double *min_y,\n"
11090                "                        double *max_y,\n"
11091                "                        double *min_z,\n"
11092                "                        double *max_z, ) {}",
11093                NoBinPacking);
11094 }
11095 
11096 TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
11097   verifyFormat("void f() { return; }\n42");
11098   verifyFormat("void f() {\n"
11099                "  if (0)\n"
11100                "    return;\n"
11101                "}\n"
11102                "42");
11103   verifyFormat("void f() { return }\n42");
11104   verifyFormat("void f() {\n"
11105                "  if (0)\n"
11106                "    return\n"
11107                "}\n"
11108                "42");
11109 }
11110 
11111 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
11112   EXPECT_EQ("void f() { return }", format("void  f ( )  {  return  }"));
11113   EXPECT_EQ("void f() {\n"
11114             "  if (a)\n"
11115             "    return\n"
11116             "}",
11117             format("void  f  (  )  {  if  ( a )  return  }"));
11118   EXPECT_EQ("namespace N {\n"
11119             "void f()\n"
11120             "}",
11121             format("namespace  N  {  void f()  }"));
11122   EXPECT_EQ("namespace N {\n"
11123             "void f() {}\n"
11124             "void g()\n"
11125             "} // namespace N",
11126             format("namespace N  { void f( ) { } void g( ) }"));
11127 }
11128 
11129 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
11130   verifyFormat("int aaaaaaaa =\n"
11131                "    // Overlylongcomment\n"
11132                "    b;",
11133                getLLVMStyleWithColumns(20));
11134   verifyFormat("function(\n"
11135                "    ShortArgument,\n"
11136                "    LoooooooooooongArgument);\n",
11137                getLLVMStyleWithColumns(20));
11138 }
11139 
11140 TEST_F(FormatTest, IncorrectAccessSpecifier) {
11141   verifyFormat("public:");
11142   verifyFormat("class A {\n"
11143                "public\n"
11144                "  void f() {}\n"
11145                "};");
11146   verifyFormat("public\n"
11147                "int qwerty;");
11148   verifyFormat("public\n"
11149                "B {}");
11150   verifyFormat("public\n"
11151                "{}");
11152   verifyFormat("public\n"
11153                "B { int x; }");
11154 }
11155 
11156 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
11157   verifyFormat("{");
11158   verifyFormat("#})");
11159   verifyNoCrash("(/**/[:!] ?[).");
11160 }
11161 
11162 TEST_F(FormatTest, IncorrectUnbalancedBracesInMacrosWithUnicode) {
11163   // Found by oss-fuzz:
11164   // https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=8212
11165   FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp);
11166   Style.ColumnLimit = 60;
11167   verifyNoCrash(
11168       "\x23\x47\xff\x20\x28\xff\x3c\xff\x3f\xff\x20\x2f\x7b\x7a\xff\x20"
11169       "\xff\xff\xff\xca\xb5\xff\xff\xff\xff\x3a\x7b\x7d\xff\x20\xff\x20"
11170       "\xff\x74\xff\x20\x7d\x7d\xff\x7b\x3a\xff\x20\x71\xff\x20\xff\x0a",
11171       Style);
11172 }
11173 
11174 TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
11175   verifyFormat("do {\n}");
11176   verifyFormat("do {\n}\n"
11177                "f();");
11178   verifyFormat("do {\n}\n"
11179                "wheeee(fun);");
11180   verifyFormat("do {\n"
11181                "  f();\n"
11182                "}");
11183 }
11184 
11185 TEST_F(FormatTest, IncorrectCodeMissingParens) {
11186   verifyFormat("if {\n  foo;\n  foo();\n}");
11187   verifyFormat("switch {\n  foo;\n  foo();\n}");
11188   verifyIncompleteFormat("for {\n  foo;\n  foo();\n}");
11189   verifyFormat("while {\n  foo;\n  foo();\n}");
11190   verifyFormat("do {\n  foo;\n  foo();\n} while;");
11191 }
11192 
11193 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
11194   verifyIncompleteFormat("namespace {\n"
11195                          "class Foo { Foo (\n"
11196                          "};\n"
11197                          "} // namespace");
11198 }
11199 
11200 TEST_F(FormatTest, IncorrectCodeErrorDetection) {
11201   EXPECT_EQ("{\n  {}\n", format("{\n{\n}\n"));
11202   EXPECT_EQ("{\n  {}\n", format("{\n  {\n}\n"));
11203   EXPECT_EQ("{\n  {}\n", format("{\n  {\n  }\n"));
11204   EXPECT_EQ("{\n  {}\n}\n}\n", format("{\n  {\n    }\n  }\n}\n"));
11205 
11206   EXPECT_EQ("{\n"
11207             "  {\n"
11208             "    breakme(\n"
11209             "        qwe);\n"
11210             "  }\n",
11211             format("{\n"
11212                    "    {\n"
11213                    " breakme(qwe);\n"
11214                    "}\n",
11215                    getLLVMStyleWithColumns(10)));
11216 }
11217 
11218 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
11219   verifyFormat("int x = {\n"
11220                "    avariable,\n"
11221                "    b(alongervariable)};",
11222                getLLVMStyleWithColumns(25));
11223 }
11224 
11225 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
11226   verifyFormat("return (a)(b){1, 2, 3};");
11227 }
11228 
11229 TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
11230   verifyFormat("vector<int> x{1, 2, 3, 4};");
11231   verifyFormat("vector<int> x{\n"
11232                "    1,\n"
11233                "    2,\n"
11234                "    3,\n"
11235                "    4,\n"
11236                "};");
11237   verifyFormat("vector<T> x{{}, {}, {}, {}};");
11238   verifyFormat("f({1, 2});");
11239   verifyFormat("auto v = Foo{-1};");
11240   verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
11241   verifyFormat("Class::Class : member{1, 2, 3} {}");
11242   verifyFormat("new vector<int>{1, 2, 3};");
11243   verifyFormat("new int[3]{1, 2, 3};");
11244   verifyFormat("new int{1};");
11245   verifyFormat("return {arg1, arg2};");
11246   verifyFormat("return {arg1, SomeType{parameter}};");
11247   verifyFormat("int count = set<int>{f(), g(), h()}.size();");
11248   verifyFormat("new T{arg1, arg2};");
11249   verifyFormat("f(MyMap[{composite, key}]);");
11250   verifyFormat("class Class {\n"
11251                "  T member = {arg1, arg2};\n"
11252                "};");
11253   verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
11254   verifyFormat("const struct A a = {.a = 1, .b = 2};");
11255   verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
11256   verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
11257   verifyFormat("int a = std::is_integral<int>{} + 0;");
11258 
11259   verifyFormat("int foo(int i) { return fo1{}(i); }");
11260   verifyFormat("int foo(int i) { return fo1{}(i); }");
11261   verifyFormat("auto i = decltype(x){};");
11262   verifyFormat("auto i = typeof(x){};");
11263   verifyFormat("auto i = _Atomic(x){};");
11264   verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
11265   verifyFormat("Node n{1, Node{1000}, //\n"
11266                "       2};");
11267   verifyFormat("Aaaa aaaaaaa{\n"
11268                "    {\n"
11269                "        aaaa,\n"
11270                "    },\n"
11271                "};");
11272   verifyFormat("class C : public D {\n"
11273                "  SomeClass SC{2};\n"
11274                "};");
11275   verifyFormat("class C : public A {\n"
11276                "  class D : public B {\n"
11277                "    void f() { int i{2}; }\n"
11278                "  };\n"
11279                "};");
11280   verifyFormat("#define A {a, a},");
11281 
11282   // Avoid breaking between equal sign and opening brace
11283   FormatStyle AvoidBreakingFirstArgument = getLLVMStyle();
11284   AvoidBreakingFirstArgument.PenaltyBreakBeforeFirstCallParameter = 200;
11285   verifyFormat("const std::unordered_map<std::string, int> MyHashTable =\n"
11286                "    {{\"aaaaaaaaaaaaaaaaaaaaa\", 0},\n"
11287                "     {\"bbbbbbbbbbbbbbbbbbbbb\", 1},\n"
11288                "     {\"ccccccccccccccccccccc\", 2}};",
11289                AvoidBreakingFirstArgument);
11290 
11291   // Binpacking only if there is no trailing comma
11292   verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n"
11293                "                      cccccccccc, dddddddddd};",
11294                getLLVMStyleWithColumns(50));
11295   verifyFormat("const Aaaaaa aaaaa = {\n"
11296                "    aaaaaaaaaaa,\n"
11297                "    bbbbbbbbbbb,\n"
11298                "    ccccccccccc,\n"
11299                "    ddddddddddd,\n"
11300                "};",
11301                getLLVMStyleWithColumns(50));
11302 
11303   // Cases where distinguising braced lists and blocks is hard.
11304   verifyFormat("vector<int> v{12} GUARDED_BY(mutex);");
11305   verifyFormat("void f() {\n"
11306                "  return; // comment\n"
11307                "}\n"
11308                "SomeType t;");
11309   verifyFormat("void f() {\n"
11310                "  if (a) {\n"
11311                "    f();\n"
11312                "  }\n"
11313                "}\n"
11314                "SomeType t;");
11315 
11316   // In combination with BinPackArguments = false.
11317   FormatStyle NoBinPacking = getLLVMStyle();
11318   NoBinPacking.BinPackArguments = false;
11319   verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
11320                "                      bbbbb,\n"
11321                "                      ccccc,\n"
11322                "                      ddddd,\n"
11323                "                      eeeee,\n"
11324                "                      ffffff,\n"
11325                "                      ggggg,\n"
11326                "                      hhhhhh,\n"
11327                "                      iiiiii,\n"
11328                "                      jjjjjj,\n"
11329                "                      kkkkkk};",
11330                NoBinPacking);
11331   verifyFormat("const Aaaaaa aaaaa = {\n"
11332                "    aaaaa,\n"
11333                "    bbbbb,\n"
11334                "    ccccc,\n"
11335                "    ddddd,\n"
11336                "    eeeee,\n"
11337                "    ffffff,\n"
11338                "    ggggg,\n"
11339                "    hhhhhh,\n"
11340                "    iiiiii,\n"
11341                "    jjjjjj,\n"
11342                "    kkkkkk,\n"
11343                "};",
11344                NoBinPacking);
11345   verifyFormat(
11346       "const Aaaaaa aaaaa = {\n"
11347       "    aaaaa,  bbbbb,  ccccc,  ddddd,  eeeee,  ffffff, ggggg, hhhhhh,\n"
11348       "    iiiiii, jjjjjj, kkkkkk, aaaaa,  bbbbb,  ccccc,  ddddd, eeeee,\n"
11349       "    ffffff, ggggg,  hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
11350       "};",
11351       NoBinPacking);
11352 
11353   NoBinPacking.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
11354   EXPECT_EQ("static uint8 CddDp83848Reg[] = {\n"
11355             "    CDDDP83848_BMCR_REGISTER,\n"
11356             "    CDDDP83848_BMSR_REGISTER,\n"
11357             "    CDDDP83848_RBR_REGISTER};",
11358             format("static uint8 CddDp83848Reg[] = {CDDDP83848_BMCR_REGISTER,\n"
11359                    "                                CDDDP83848_BMSR_REGISTER,\n"
11360                    "                                CDDDP83848_RBR_REGISTER};",
11361                    NoBinPacking));
11362 
11363   // FIXME: The alignment of these trailing comments might be bad. Then again,
11364   // this might be utterly useless in real code.
11365   verifyFormat("Constructor::Constructor()\n"
11366                "    : some_value{         //\n"
11367                "                 aaaaaaa, //\n"
11368                "                 bbbbbbb} {}");
11369 
11370   // In braced lists, the first comment is always assumed to belong to the
11371   // first element. Thus, it can be moved to the next or previous line as
11372   // appropriate.
11373   EXPECT_EQ("function({// First element:\n"
11374             "          1,\n"
11375             "          // Second element:\n"
11376             "          2});",
11377             format("function({\n"
11378                    "    // First element:\n"
11379                    "    1,\n"
11380                    "    // Second element:\n"
11381                    "    2});"));
11382   EXPECT_EQ("std::vector<int> MyNumbers{\n"
11383             "    // First element:\n"
11384             "    1,\n"
11385             "    // Second element:\n"
11386             "    2};",
11387             format("std::vector<int> MyNumbers{// First element:\n"
11388                    "                           1,\n"
11389                    "                           // Second element:\n"
11390                    "                           2};",
11391                    getLLVMStyleWithColumns(30)));
11392   // A trailing comma should still lead to an enforced line break and no
11393   // binpacking.
11394   EXPECT_EQ("vector<int> SomeVector = {\n"
11395             "    // aaa\n"
11396             "    1,\n"
11397             "    2,\n"
11398             "};",
11399             format("vector<int> SomeVector = { // aaa\n"
11400                    "    1, 2, };"));
11401 
11402   // C++11 brace initializer list l-braces should not be treated any differently
11403   // when breaking before lambda bodies is enabled
11404   FormatStyle BreakBeforeLambdaBody = getLLVMStyle();
11405   BreakBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
11406   BreakBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
11407   BreakBeforeLambdaBody.AlwaysBreakBeforeMultilineStrings = true;
11408   verifyFormat(
11409       "std::runtime_error{\n"
11410       "    \"Long string which will force a break onto the next line...\"};",
11411       BreakBeforeLambdaBody);
11412 
11413   FormatStyle ExtraSpaces = getLLVMStyle();
11414   ExtraSpaces.Cpp11BracedListStyle = false;
11415   ExtraSpaces.ColumnLimit = 75;
11416   verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
11417   verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
11418   verifyFormat("f({ 1, 2 });", ExtraSpaces);
11419   verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
11420   verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
11421   verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
11422   verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
11423   verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
11424   verifyFormat("return { arg1, arg2 };", ExtraSpaces);
11425   verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
11426   verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
11427   verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
11428   verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
11429   verifyFormat("class Class {\n"
11430                "  T member = { arg1, arg2 };\n"
11431                "};",
11432                ExtraSpaces);
11433   verifyFormat(
11434       "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
11435       "                                 aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
11436       "                  : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
11437       "                                 bbbbbbbbbbbbbbbbbbbb, bbbbb };",
11438       ExtraSpaces);
11439   verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
11440   verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
11441                ExtraSpaces);
11442   verifyFormat(
11443       "someFunction(OtherParam,\n"
11444       "             BracedList{ // comment 1 (Forcing interesting break)\n"
11445       "                         param1, param2,\n"
11446       "                         // comment 2\n"
11447       "                         param3, param4 });",
11448       ExtraSpaces);
11449   verifyFormat(
11450       "std::this_thread::sleep_for(\n"
11451       "    std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
11452       ExtraSpaces);
11453   verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n"
11454                "    aaaaaaa,\n"
11455                "    aaaaaaaaaa,\n"
11456                "    aaaaa,\n"
11457                "    aaaaaaaaaaaaaaa,\n"
11458                "    aaa,\n"
11459                "    aaaaaaaaaa,\n"
11460                "    a,\n"
11461                "    aaaaaaaaaaaaaaaaaaaaa,\n"
11462                "    aaaaaaaaaaaa,\n"
11463                "    aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
11464                "    aaaaaaa,\n"
11465                "    a};");
11466   verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
11467   verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces);
11468   verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces);
11469 
11470   // Avoid breaking between initializer/equal sign and opening brace
11471   ExtraSpaces.PenaltyBreakBeforeFirstCallParameter = 200;
11472   verifyFormat("const std::unordered_map<std::string, int> MyHashTable = {\n"
11473                "  { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n"
11474                "  { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n"
11475                "  { \"ccccccccccccccccccccc\", 2 }\n"
11476                "};",
11477                ExtraSpaces);
11478   verifyFormat("const std::unordered_map<std::string, int> MyHashTable{\n"
11479                "  { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n"
11480                "  { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n"
11481                "  { \"ccccccccccccccccccccc\", 2 }\n"
11482                "};",
11483                ExtraSpaces);
11484 
11485   FormatStyle SpaceBeforeBrace = getLLVMStyle();
11486   SpaceBeforeBrace.SpaceBeforeCpp11BracedList = true;
11487   verifyFormat("vector<int> x {1, 2, 3, 4};", SpaceBeforeBrace);
11488   verifyFormat("f({}, {{}, {}}, MyMap[{k, v}]);", SpaceBeforeBrace);
11489 
11490   FormatStyle SpaceBetweenBraces = getLLVMStyle();
11491   SpaceBetweenBraces.SpacesInAngles = FormatStyle::SIAS_Always;
11492   SpaceBetweenBraces.SpacesInParentheses = true;
11493   SpaceBetweenBraces.SpacesInSquareBrackets = true;
11494   verifyFormat("vector< int > x{ 1, 2, 3, 4 };", SpaceBetweenBraces);
11495   verifyFormat("f( {}, { {}, {} }, MyMap[ { k, v } ] );", SpaceBetweenBraces);
11496   verifyFormat("vector< int > x{ // comment 1\n"
11497                "                 1, 2, 3, 4 };",
11498                SpaceBetweenBraces);
11499   SpaceBetweenBraces.ColumnLimit = 20;
11500   EXPECT_EQ("vector< int > x{\n"
11501             "    1, 2, 3, 4 };",
11502             format("vector<int>x{1,2,3,4};", SpaceBetweenBraces));
11503   SpaceBetweenBraces.ColumnLimit = 24;
11504   EXPECT_EQ("vector< int > x{ 1, 2,\n"
11505             "                 3, 4 };",
11506             format("vector<int>x{1,2,3,4};", SpaceBetweenBraces));
11507   EXPECT_EQ("vector< int > x{\n"
11508             "    1,\n"
11509             "    2,\n"
11510             "    3,\n"
11511             "    4,\n"
11512             "};",
11513             format("vector<int>x{1,2,3,4,};", SpaceBetweenBraces));
11514   verifyFormat("vector< int > x{};", SpaceBetweenBraces);
11515   SpaceBetweenBraces.SpaceInEmptyParentheses = true;
11516   verifyFormat("vector< int > x{ };", SpaceBetweenBraces);
11517 }
11518 
11519 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
11520   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11521                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11522                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11523                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11524                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11525                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
11526   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n"
11527                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11528                "                 1, 22, 333, 4444, 55555, //\n"
11529                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11530                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
11531   verifyFormat(
11532       "vector<int> x = {1,       22, 333, 4444, 55555, 666666, 7777777,\n"
11533       "                 1,       22, 333, 4444, 55555, 666666, 7777777,\n"
11534       "                 1,       22, 333, 4444, 55555, 666666, // comment\n"
11535       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
11536       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
11537       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
11538       "                 7777777};");
11539   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
11540                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
11541                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
11542   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
11543                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
11544                "    // Separating comment.\n"
11545                "    X86::R8, X86::R9, X86::R10, X86::R11, 0};");
11546   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
11547                "    // Leading comment\n"
11548                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
11549                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
11550   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
11551                "                 1, 1, 1, 1};",
11552                getLLVMStyleWithColumns(39));
11553   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
11554                "                 1, 1, 1, 1};",
11555                getLLVMStyleWithColumns(38));
11556   verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
11557                "    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
11558                getLLVMStyleWithColumns(43));
11559   verifyFormat(
11560       "static unsigned SomeValues[10][3] = {\n"
11561       "    {1, 4, 0},  {4, 9, 0},  {4, 5, 9},  {8, 5, 4}, {1, 8, 4},\n"
11562       "    {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};");
11563   verifyFormat("static auto fields = new vector<string>{\n"
11564                "    \"aaaaaaaaaaaaa\",\n"
11565                "    \"aaaaaaaaaaaaa\",\n"
11566                "    \"aaaaaaaaaaaa\",\n"
11567                "    \"aaaaaaaaaaaaaa\",\n"
11568                "    \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
11569                "    \"aaaaaaaaaaaa\",\n"
11570                "    \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
11571                "};");
11572   verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};");
11573   verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n"
11574                "                 2, bbbbbbbbbbbbbbbbbbbbbb,\n"
11575                "                 3, cccccccccccccccccccccc};",
11576                getLLVMStyleWithColumns(60));
11577 
11578   // Trailing commas.
11579   verifyFormat("vector<int> x = {\n"
11580                "    1, 1, 1, 1, 1, 1, 1, 1,\n"
11581                "};",
11582                getLLVMStyleWithColumns(39));
11583   verifyFormat("vector<int> x = {\n"
11584                "    1, 1, 1, 1, 1, 1, 1, 1, //\n"
11585                "};",
11586                getLLVMStyleWithColumns(39));
11587   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
11588                "                 1, 1, 1, 1,\n"
11589                "                 /**/ /**/};",
11590                getLLVMStyleWithColumns(39));
11591 
11592   // Trailing comment in the first line.
11593   verifyFormat("vector<int> iiiiiiiiiiiiiii = {                      //\n"
11594                "    1111111111, 2222222222, 33333333333, 4444444444, //\n"
11595                "    111111111,  222222222,  3333333333,  444444444,  //\n"
11596                "    11111111,   22222222,   333333333,   44444444};");
11597   // Trailing comment in the last line.
11598   verifyFormat("int aaaaa[] = {\n"
11599                "    1, 2, 3, // comment\n"
11600                "    4, 5, 6  // comment\n"
11601                "};");
11602 
11603   // With nested lists, we should either format one item per line or all nested
11604   // lists one on line.
11605   // FIXME: For some nested lists, we can do better.
11606   verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
11607                "        {aaaaaaaaaaaaaaaaaaa},\n"
11608                "        {aaaaaaaaaaaaaaaaaaaaa},\n"
11609                "        {aaaaaaaaaaaaaaaaa}};",
11610                getLLVMStyleWithColumns(60));
11611   verifyFormat(
11612       "SomeStruct my_struct_array = {\n"
11613       "    {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
11614       "     aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
11615       "    {aaa, aaa},\n"
11616       "    {aaa, aaa},\n"
11617       "    {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
11618       "    {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
11619       "     aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
11620 
11621   // No column layout should be used here.
11622   verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
11623                "                   bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
11624 
11625   verifyNoCrash("a<,");
11626 
11627   // No braced initializer here.
11628   verifyFormat("void f() {\n"
11629                "  struct Dummy {};\n"
11630                "  f(v);\n"
11631                "}");
11632 
11633   // Long lists should be formatted in columns even if they are nested.
11634   verifyFormat(
11635       "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11636       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11637       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11638       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11639       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11640       "                          1, 22, 333, 4444, 55555, 666666, 7777777});");
11641 
11642   // Allow "single-column" layout even if that violates the column limit. There
11643   // isn't going to be a better way.
11644   verifyFormat("std::vector<int> a = {\n"
11645                "    aaaaaaaa,\n"
11646                "    aaaaaaaa,\n"
11647                "    aaaaaaaa,\n"
11648                "    aaaaaaaa,\n"
11649                "    aaaaaaaaaa,\n"
11650                "    aaaaaaaa,\n"
11651                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa};",
11652                getLLVMStyleWithColumns(30));
11653   verifyFormat("vector<int> aaaa = {\n"
11654                "    aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
11655                "    aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
11656                "    aaaaaa.aaaaaaa,\n"
11657                "    aaaaaa.aaaaaaa,\n"
11658                "    aaaaaa.aaaaaaa,\n"
11659                "    aaaaaa.aaaaaaa,\n"
11660                "};");
11661 
11662   // Don't create hanging lists.
11663   verifyFormat("someFunction(Param, {List1, List2,\n"
11664                "                     List3});",
11665                getLLVMStyleWithColumns(35));
11666   verifyFormat("someFunction(Param, Param,\n"
11667                "             {List1, List2,\n"
11668                "              List3});",
11669                getLLVMStyleWithColumns(35));
11670   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n"
11671                "                               aaaaaaaaaaaaaaaaaaaaaaa);");
11672 }
11673 
11674 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
11675   FormatStyle DoNotMerge = getLLVMStyle();
11676   DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
11677 
11678   verifyFormat("void f() { return 42; }");
11679   verifyFormat("void f() {\n"
11680                "  return 42;\n"
11681                "}",
11682                DoNotMerge);
11683   verifyFormat("void f() {\n"
11684                "  // Comment\n"
11685                "}");
11686   verifyFormat("{\n"
11687                "#error {\n"
11688                "  int a;\n"
11689                "}");
11690   verifyFormat("{\n"
11691                "  int a;\n"
11692                "#error {\n"
11693                "}");
11694   verifyFormat("void f() {} // comment");
11695   verifyFormat("void f() { int a; } // comment");
11696   verifyFormat("void f() {\n"
11697                "} // comment",
11698                DoNotMerge);
11699   verifyFormat("void f() {\n"
11700                "  int a;\n"
11701                "} // comment",
11702                DoNotMerge);
11703   verifyFormat("void f() {\n"
11704                "} // comment",
11705                getLLVMStyleWithColumns(15));
11706 
11707   verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
11708   verifyFormat("void f() {\n  return 42;\n}", getLLVMStyleWithColumns(22));
11709 
11710   verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
11711   verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
11712   verifyFormat("class C {\n"
11713                "  C()\n"
11714                "      : iiiiiiii(nullptr),\n"
11715                "        kkkkkkk(nullptr),\n"
11716                "        mmmmmmm(nullptr),\n"
11717                "        nnnnnnn(nullptr) {}\n"
11718                "};",
11719                getGoogleStyle());
11720 
11721   FormatStyle NoColumnLimit = getLLVMStyle();
11722   NoColumnLimit.ColumnLimit = 0;
11723   EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit));
11724   EXPECT_EQ("class C {\n"
11725             "  A() : b(0) {}\n"
11726             "};",
11727             format("class C{A():b(0){}};", NoColumnLimit));
11728   EXPECT_EQ("A()\n"
11729             "    : b(0) {\n"
11730             "}",
11731             format("A()\n:b(0)\n{\n}", NoColumnLimit));
11732 
11733   FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
11734   DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
11735       FormatStyle::SFS_None;
11736   EXPECT_EQ("A()\n"
11737             "    : b(0) {\n"
11738             "}",
11739             format("A():b(0){}", DoNotMergeNoColumnLimit));
11740   EXPECT_EQ("A()\n"
11741             "    : b(0) {\n"
11742             "}",
11743             format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit));
11744 
11745   verifyFormat("#define A          \\\n"
11746                "  void f() {       \\\n"
11747                "    int i;         \\\n"
11748                "  }",
11749                getLLVMStyleWithColumns(20));
11750   verifyFormat("#define A           \\\n"
11751                "  void f() { int i; }",
11752                getLLVMStyleWithColumns(21));
11753   verifyFormat("#define A            \\\n"
11754                "  void f() {         \\\n"
11755                "    int i;           \\\n"
11756                "  }                  \\\n"
11757                "  int j;",
11758                getLLVMStyleWithColumns(22));
11759   verifyFormat("#define A             \\\n"
11760                "  void f() { int i; } \\\n"
11761                "  int j;",
11762                getLLVMStyleWithColumns(23));
11763 }
11764 
11765 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) {
11766   FormatStyle MergeEmptyOnly = getLLVMStyle();
11767   MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
11768   verifyFormat("class C {\n"
11769                "  int f() {}\n"
11770                "};",
11771                MergeEmptyOnly);
11772   verifyFormat("class C {\n"
11773                "  int f() {\n"
11774                "    return 42;\n"
11775                "  }\n"
11776                "};",
11777                MergeEmptyOnly);
11778   verifyFormat("int f() {}", MergeEmptyOnly);
11779   verifyFormat("int f() {\n"
11780                "  return 42;\n"
11781                "}",
11782                MergeEmptyOnly);
11783 
11784   // Also verify behavior when BraceWrapping.AfterFunction = true
11785   MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
11786   MergeEmptyOnly.BraceWrapping.AfterFunction = true;
11787   verifyFormat("int f() {}", MergeEmptyOnly);
11788   verifyFormat("class C {\n"
11789                "  int f() {}\n"
11790                "};",
11791                MergeEmptyOnly);
11792 }
11793 
11794 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
11795   FormatStyle MergeInlineOnly = getLLVMStyle();
11796   MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
11797   verifyFormat("class C {\n"
11798                "  int f() { return 42; }\n"
11799                "};",
11800                MergeInlineOnly);
11801   verifyFormat("int f() {\n"
11802                "  return 42;\n"
11803                "}",
11804                MergeInlineOnly);
11805 
11806   // SFS_Inline implies SFS_Empty
11807   verifyFormat("class C {\n"
11808                "  int f() {}\n"
11809                "};",
11810                MergeInlineOnly);
11811   verifyFormat("int f() {}", MergeInlineOnly);
11812 
11813   // Also verify behavior when BraceWrapping.AfterFunction = true
11814   MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
11815   MergeInlineOnly.BraceWrapping.AfterFunction = true;
11816   verifyFormat("class C {\n"
11817                "  int f() { return 42; }\n"
11818                "};",
11819                MergeInlineOnly);
11820   verifyFormat("int f()\n"
11821                "{\n"
11822                "  return 42;\n"
11823                "}",
11824                MergeInlineOnly);
11825 
11826   // SFS_Inline implies SFS_Empty
11827   verifyFormat("int f() {}", MergeInlineOnly);
11828   verifyFormat("class C {\n"
11829                "  int f() {}\n"
11830                "};",
11831                MergeInlineOnly);
11832 }
11833 
11834 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) {
11835   FormatStyle MergeInlineOnly = getLLVMStyle();
11836   MergeInlineOnly.AllowShortFunctionsOnASingleLine =
11837       FormatStyle::SFS_InlineOnly;
11838   verifyFormat("class C {\n"
11839                "  int f() { return 42; }\n"
11840                "};",
11841                MergeInlineOnly);
11842   verifyFormat("int f() {\n"
11843                "  return 42;\n"
11844                "}",
11845                MergeInlineOnly);
11846 
11847   // SFS_InlineOnly does not imply SFS_Empty
11848   verifyFormat("class C {\n"
11849                "  int f() {}\n"
11850                "};",
11851                MergeInlineOnly);
11852   verifyFormat("int f() {\n"
11853                "}",
11854                MergeInlineOnly);
11855 
11856   // Also verify behavior when BraceWrapping.AfterFunction = true
11857   MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
11858   MergeInlineOnly.BraceWrapping.AfterFunction = true;
11859   verifyFormat("class C {\n"
11860                "  int f() { return 42; }\n"
11861                "};",
11862                MergeInlineOnly);
11863   verifyFormat("int f()\n"
11864                "{\n"
11865                "  return 42;\n"
11866                "}",
11867                MergeInlineOnly);
11868 
11869   // SFS_InlineOnly does not imply SFS_Empty
11870   verifyFormat("int f()\n"
11871                "{\n"
11872                "}",
11873                MergeInlineOnly);
11874   verifyFormat("class C {\n"
11875                "  int f() {}\n"
11876                "};",
11877                MergeInlineOnly);
11878 }
11879 
11880 TEST_F(FormatTest, SplitEmptyFunction) {
11881   FormatStyle Style = getLLVMStyle();
11882   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
11883   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
11884   Style.BraceWrapping.AfterFunction = true;
11885   Style.BraceWrapping.SplitEmptyFunction = false;
11886   Style.ColumnLimit = 40;
11887 
11888   verifyFormat("int f()\n"
11889                "{}",
11890                Style);
11891   verifyFormat("int f()\n"
11892                "{\n"
11893                "  return 42;\n"
11894                "}",
11895                Style);
11896   verifyFormat("int f()\n"
11897                "{\n"
11898                "  // some comment\n"
11899                "}",
11900                Style);
11901 
11902   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
11903   verifyFormat("int f() {}", Style);
11904   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
11905                "{}",
11906                Style);
11907   verifyFormat("int f()\n"
11908                "{\n"
11909                "  return 0;\n"
11910                "}",
11911                Style);
11912 
11913   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
11914   verifyFormat("class Foo {\n"
11915                "  int f() {}\n"
11916                "};\n",
11917                Style);
11918   verifyFormat("class Foo {\n"
11919                "  int f() { return 0; }\n"
11920                "};\n",
11921                Style);
11922   verifyFormat("class Foo {\n"
11923                "  int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
11924                "  {}\n"
11925                "};\n",
11926                Style);
11927   verifyFormat("class Foo {\n"
11928                "  int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
11929                "  {\n"
11930                "    return 0;\n"
11931                "  }\n"
11932                "};\n",
11933                Style);
11934 
11935   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
11936   verifyFormat("int f() {}", Style);
11937   verifyFormat("int f() { return 0; }", Style);
11938   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
11939                "{}",
11940                Style);
11941   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
11942                "{\n"
11943                "  return 0;\n"
11944                "}",
11945                Style);
11946 }
11947 TEST_F(FormatTest, KeepShortFunctionAfterPPElse) {
11948   FormatStyle Style = getLLVMStyle();
11949   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
11950   verifyFormat("#ifdef A\n"
11951                "int f() {}\n"
11952                "#else\n"
11953                "int g() {}\n"
11954                "#endif",
11955                Style);
11956 }
11957 
11958 TEST_F(FormatTest, SplitEmptyClass) {
11959   FormatStyle Style = getLLVMStyle();
11960   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
11961   Style.BraceWrapping.AfterClass = true;
11962   Style.BraceWrapping.SplitEmptyRecord = false;
11963 
11964   verifyFormat("class Foo\n"
11965                "{};",
11966                Style);
11967   verifyFormat("/* something */ class Foo\n"
11968                "{};",
11969                Style);
11970   verifyFormat("template <typename X> class Foo\n"
11971                "{};",
11972                Style);
11973   verifyFormat("class Foo\n"
11974                "{\n"
11975                "  Foo();\n"
11976                "};",
11977                Style);
11978   verifyFormat("typedef class Foo\n"
11979                "{\n"
11980                "} Foo_t;",
11981                Style);
11982 
11983   Style.BraceWrapping.SplitEmptyRecord = true;
11984   Style.BraceWrapping.AfterStruct = true;
11985   verifyFormat("class rep\n"
11986                "{\n"
11987                "};",
11988                Style);
11989   verifyFormat("struct rep\n"
11990                "{\n"
11991                "};",
11992                Style);
11993   verifyFormat("template <typename T> class rep\n"
11994                "{\n"
11995                "};",
11996                Style);
11997   verifyFormat("template <typename T> struct rep\n"
11998                "{\n"
11999                "};",
12000                Style);
12001   verifyFormat("class rep\n"
12002                "{\n"
12003                "  int x;\n"
12004                "};",
12005                Style);
12006   verifyFormat("struct rep\n"
12007                "{\n"
12008                "  int x;\n"
12009                "};",
12010                Style);
12011   verifyFormat("template <typename T> class rep\n"
12012                "{\n"
12013                "  int x;\n"
12014                "};",
12015                Style);
12016   verifyFormat("template <typename T> struct rep\n"
12017                "{\n"
12018                "  int x;\n"
12019                "};",
12020                Style);
12021   verifyFormat("template <typename T> class rep // Foo\n"
12022                "{\n"
12023                "  int x;\n"
12024                "};",
12025                Style);
12026   verifyFormat("template <typename T> struct rep // Bar\n"
12027                "{\n"
12028                "  int x;\n"
12029                "};",
12030                Style);
12031 
12032   verifyFormat("template <typename T> class rep<T>\n"
12033                "{\n"
12034                "  int x;\n"
12035                "};",
12036                Style);
12037 
12038   verifyFormat("template <typename T> class rep<std::complex<T>>\n"
12039                "{\n"
12040                "  int x;\n"
12041                "};",
12042                Style);
12043   verifyFormat("template <typename T> class rep<std::complex<T>>\n"
12044                "{\n"
12045                "};",
12046                Style);
12047 
12048   verifyFormat("#include \"stdint.h\"\n"
12049                "namespace rep {}",
12050                Style);
12051   verifyFormat("#include <stdint.h>\n"
12052                "namespace rep {}",
12053                Style);
12054   verifyFormat("#include <stdint.h>\n"
12055                "namespace rep {}",
12056                "#include <stdint.h>\n"
12057                "namespace rep {\n"
12058                "\n"
12059                "\n"
12060                "}",
12061                Style);
12062 }
12063 
12064 TEST_F(FormatTest, SplitEmptyStruct) {
12065   FormatStyle Style = getLLVMStyle();
12066   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12067   Style.BraceWrapping.AfterStruct = true;
12068   Style.BraceWrapping.SplitEmptyRecord = false;
12069 
12070   verifyFormat("struct Foo\n"
12071                "{};",
12072                Style);
12073   verifyFormat("/* something */ struct Foo\n"
12074                "{};",
12075                Style);
12076   verifyFormat("template <typename X> struct Foo\n"
12077                "{};",
12078                Style);
12079   verifyFormat("struct Foo\n"
12080                "{\n"
12081                "  Foo();\n"
12082                "};",
12083                Style);
12084   verifyFormat("typedef struct Foo\n"
12085                "{\n"
12086                "} Foo_t;",
12087                Style);
12088   // typedef struct Bar {} Bar_t;
12089 }
12090 
12091 TEST_F(FormatTest, SplitEmptyUnion) {
12092   FormatStyle Style = getLLVMStyle();
12093   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12094   Style.BraceWrapping.AfterUnion = true;
12095   Style.BraceWrapping.SplitEmptyRecord = false;
12096 
12097   verifyFormat("union Foo\n"
12098                "{};",
12099                Style);
12100   verifyFormat("/* something */ union Foo\n"
12101                "{};",
12102                Style);
12103   verifyFormat("union Foo\n"
12104                "{\n"
12105                "  A,\n"
12106                "};",
12107                Style);
12108   verifyFormat("typedef union Foo\n"
12109                "{\n"
12110                "} Foo_t;",
12111                Style);
12112 }
12113 
12114 TEST_F(FormatTest, SplitEmptyNamespace) {
12115   FormatStyle Style = getLLVMStyle();
12116   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12117   Style.BraceWrapping.AfterNamespace = true;
12118   Style.BraceWrapping.SplitEmptyNamespace = false;
12119 
12120   verifyFormat("namespace Foo\n"
12121                "{};",
12122                Style);
12123   verifyFormat("/* something */ namespace Foo\n"
12124                "{};",
12125                Style);
12126   verifyFormat("inline namespace Foo\n"
12127                "{};",
12128                Style);
12129   verifyFormat("/* something */ inline namespace Foo\n"
12130                "{};",
12131                Style);
12132   verifyFormat("export namespace Foo\n"
12133                "{};",
12134                Style);
12135   verifyFormat("namespace Foo\n"
12136                "{\n"
12137                "void Bar();\n"
12138                "};",
12139                Style);
12140 }
12141 
12142 TEST_F(FormatTest, NeverMergeShortRecords) {
12143   FormatStyle Style = getLLVMStyle();
12144 
12145   verifyFormat("class Foo {\n"
12146                "  Foo();\n"
12147                "};",
12148                Style);
12149   verifyFormat("typedef class Foo {\n"
12150                "  Foo();\n"
12151                "} Foo_t;",
12152                Style);
12153   verifyFormat("struct Foo {\n"
12154                "  Foo();\n"
12155                "};",
12156                Style);
12157   verifyFormat("typedef struct Foo {\n"
12158                "  Foo();\n"
12159                "} Foo_t;",
12160                Style);
12161   verifyFormat("union Foo {\n"
12162                "  A,\n"
12163                "};",
12164                Style);
12165   verifyFormat("typedef union Foo {\n"
12166                "  A,\n"
12167                "} Foo_t;",
12168                Style);
12169   verifyFormat("namespace Foo {\n"
12170                "void Bar();\n"
12171                "};",
12172                Style);
12173 
12174   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12175   Style.BraceWrapping.AfterClass = true;
12176   Style.BraceWrapping.AfterStruct = true;
12177   Style.BraceWrapping.AfterUnion = true;
12178   Style.BraceWrapping.AfterNamespace = true;
12179   verifyFormat("class Foo\n"
12180                "{\n"
12181                "  Foo();\n"
12182                "};",
12183                Style);
12184   verifyFormat("typedef class Foo\n"
12185                "{\n"
12186                "  Foo();\n"
12187                "} Foo_t;",
12188                Style);
12189   verifyFormat("struct Foo\n"
12190                "{\n"
12191                "  Foo();\n"
12192                "};",
12193                Style);
12194   verifyFormat("typedef struct Foo\n"
12195                "{\n"
12196                "  Foo();\n"
12197                "} Foo_t;",
12198                Style);
12199   verifyFormat("union Foo\n"
12200                "{\n"
12201                "  A,\n"
12202                "};",
12203                Style);
12204   verifyFormat("typedef union Foo\n"
12205                "{\n"
12206                "  A,\n"
12207                "} Foo_t;",
12208                Style);
12209   verifyFormat("namespace Foo\n"
12210                "{\n"
12211                "void Bar();\n"
12212                "};",
12213                Style);
12214 }
12215 
12216 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
12217   // Elaborate type variable declarations.
12218   verifyFormat("struct foo a = {bar};\nint n;");
12219   verifyFormat("class foo a = {bar};\nint n;");
12220   verifyFormat("union foo a = {bar};\nint n;");
12221 
12222   // Elaborate types inside function definitions.
12223   verifyFormat("struct foo f() {}\nint n;");
12224   verifyFormat("class foo f() {}\nint n;");
12225   verifyFormat("union foo f() {}\nint n;");
12226 
12227   // Templates.
12228   verifyFormat("template <class X> void f() {}\nint n;");
12229   verifyFormat("template <struct X> void f() {}\nint n;");
12230   verifyFormat("template <union X> void f() {}\nint n;");
12231 
12232   // Actual definitions...
12233   verifyFormat("struct {\n} n;");
12234   verifyFormat(
12235       "template <template <class T, class Y>, class Z> class X {\n} n;");
12236   verifyFormat("union Z {\n  int n;\n} x;");
12237   verifyFormat("class MACRO Z {\n} n;");
12238   verifyFormat("class MACRO(X) Z {\n} n;");
12239   verifyFormat("class __attribute__(X) Z {\n} n;");
12240   verifyFormat("class __declspec(X) Z {\n} n;");
12241   verifyFormat("class A##B##C {\n} n;");
12242   verifyFormat("class alignas(16) Z {\n} n;");
12243   verifyFormat("class MACRO(X) alignas(16) Z {\n} n;");
12244   verifyFormat("class MACROA MACRO(X) Z {\n} n;");
12245 
12246   // Redefinition from nested context:
12247   verifyFormat("class A::B::C {\n} n;");
12248 
12249   // Template definitions.
12250   verifyFormat(
12251       "template <typename F>\n"
12252       "Matcher(const Matcher<F> &Other,\n"
12253       "        typename enable_if_c<is_base_of<F, T>::value &&\n"
12254       "                             !is_same<F, T>::value>::type * = 0)\n"
12255       "    : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
12256 
12257   // FIXME: This is still incorrectly handled at the formatter side.
12258   verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
12259   verifyFormat("int i = SomeFunction(a<b, a> b);");
12260 
12261   // FIXME:
12262   // This now gets parsed incorrectly as class definition.
12263   // verifyFormat("class A<int> f() {\n}\nint n;");
12264 
12265   // Elaborate types where incorrectly parsing the structural element would
12266   // break the indent.
12267   verifyFormat("if (true)\n"
12268                "  class X x;\n"
12269                "else\n"
12270                "  f();\n");
12271 
12272   // This is simply incomplete. Formatting is not important, but must not crash.
12273   verifyFormat("class A:");
12274 }
12275 
12276 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
12277   EXPECT_EQ("#error Leave     all         white!!!!! space* alone!\n",
12278             format("#error Leave     all         white!!!!! space* alone!\n"));
12279   EXPECT_EQ(
12280       "#warning Leave     all         white!!!!! space* alone!\n",
12281       format("#warning Leave     all         white!!!!! space* alone!\n"));
12282   EXPECT_EQ("#error 1", format("  #  error   1"));
12283   EXPECT_EQ("#warning 1", format("  #  warning 1"));
12284 }
12285 
12286 TEST_F(FormatTest, FormatHashIfExpressions) {
12287   verifyFormat("#if AAAA && BBBB");
12288   verifyFormat("#if (AAAA && BBBB)");
12289   verifyFormat("#elif (AAAA && BBBB)");
12290   // FIXME: Come up with a better indentation for #elif.
12291   verifyFormat(
12292       "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) &&  \\\n"
12293       "    defined(BBBBBBBB)\n"
12294       "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) &&  \\\n"
12295       "    defined(BBBBBBBB)\n"
12296       "#endif",
12297       getLLVMStyleWithColumns(65));
12298 }
12299 
12300 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
12301   FormatStyle AllowsMergedIf = getGoogleStyle();
12302   AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
12303       FormatStyle::SIS_WithoutElse;
12304   verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
12305   verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
12306   verifyFormat("if (true)\n#error E\n  return 42;", AllowsMergedIf);
12307   EXPECT_EQ("if (true) return 42;",
12308             format("if (true)\nreturn 42;", AllowsMergedIf));
12309   FormatStyle ShortMergedIf = AllowsMergedIf;
12310   ShortMergedIf.ColumnLimit = 25;
12311   verifyFormat("#define A \\\n"
12312                "  if (true) return 42;",
12313                ShortMergedIf);
12314   verifyFormat("#define A \\\n"
12315                "  f();    \\\n"
12316                "  if (true)\n"
12317                "#define B",
12318                ShortMergedIf);
12319   verifyFormat("#define A \\\n"
12320                "  f();    \\\n"
12321                "  if (true)\n"
12322                "g();",
12323                ShortMergedIf);
12324   verifyFormat("{\n"
12325                "#ifdef A\n"
12326                "  // Comment\n"
12327                "  if (true) continue;\n"
12328                "#endif\n"
12329                "  // Comment\n"
12330                "  if (true) continue;\n"
12331                "}",
12332                ShortMergedIf);
12333   ShortMergedIf.ColumnLimit = 33;
12334   verifyFormat("#define A \\\n"
12335                "  if constexpr (true) return 42;",
12336                ShortMergedIf);
12337   verifyFormat("#define A \\\n"
12338                "  if CONSTEXPR (true) return 42;",
12339                ShortMergedIf);
12340   ShortMergedIf.ColumnLimit = 29;
12341   verifyFormat("#define A                   \\\n"
12342                "  if (aaaaaaaaaa) return 1; \\\n"
12343                "  return 2;",
12344                ShortMergedIf);
12345   ShortMergedIf.ColumnLimit = 28;
12346   verifyFormat("#define A         \\\n"
12347                "  if (aaaaaaaaaa) \\\n"
12348                "    return 1;     \\\n"
12349                "  return 2;",
12350                ShortMergedIf);
12351   verifyFormat("#define A                \\\n"
12352                "  if constexpr (aaaaaaa) \\\n"
12353                "    return 1;            \\\n"
12354                "  return 2;",
12355                ShortMergedIf);
12356   verifyFormat("#define A                \\\n"
12357                "  if CONSTEXPR (aaaaaaa) \\\n"
12358                "    return 1;            \\\n"
12359                "  return 2;",
12360                ShortMergedIf);
12361 }
12362 
12363 TEST_F(FormatTest, FormatStarDependingOnContext) {
12364   verifyFormat("void f(int *a);");
12365   verifyFormat("void f() { f(fint * b); }");
12366   verifyFormat("class A {\n  void f(int *a);\n};");
12367   verifyFormat("class A {\n  int *a;\n};");
12368   verifyFormat("namespace a {\n"
12369                "namespace b {\n"
12370                "class A {\n"
12371                "  void f() {}\n"
12372                "  int *a;\n"
12373                "};\n"
12374                "} // namespace b\n"
12375                "} // namespace a");
12376 }
12377 
12378 TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
12379   verifyFormat("while");
12380   verifyFormat("operator");
12381 }
12382 
12383 TEST_F(FormatTest, SkipsDeeplyNestedLines) {
12384   // This code would be painfully slow to format if we didn't skip it.
12385   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
12386                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
12387                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
12388                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
12389                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
12390                    "A(1, 1)\n"
12391                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x
12392                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12393                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12394                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12395                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12396                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12397                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12398                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12399                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12400                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n");
12401   // Deeply nested part is untouched, rest is formatted.
12402   EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n",
12403             format(std::string("int    i;\n") + Code + "int    j;\n",
12404                    getLLVMStyle(), SC_ExpectIncomplete));
12405 }
12406 
12407 //===----------------------------------------------------------------------===//
12408 // Objective-C tests.
12409 //===----------------------------------------------------------------------===//
12410 
12411 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
12412   verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
12413   EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
12414             format("-(NSUInteger)indexOfObject:(id)anObject;"));
12415   EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
12416   EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
12417   EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
12418             format("-(NSInteger)Method3:(id)anObject;"));
12419   EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
12420             format("-(NSInteger)Method4:(id)anObject;"));
12421   EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
12422             format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
12423   EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
12424             format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
12425   EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject "
12426             "forAllCells:(BOOL)flag;",
12427             format("- (void)sendAction:(SEL)aSelector to:(id)anObject "
12428                    "forAllCells:(BOOL)flag;"));
12429 
12430   // Very long objectiveC method declaration.
12431   verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
12432                "    (SoooooooooooooooooooooomeType *)bbbbbbbbbb;");
12433   verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
12434                "                    inRange:(NSRange)range\n"
12435                "                   outRange:(NSRange)out_range\n"
12436                "                  outRange1:(NSRange)out_range1\n"
12437                "                  outRange2:(NSRange)out_range2\n"
12438                "                  outRange3:(NSRange)out_range3\n"
12439                "                  outRange4:(NSRange)out_range4\n"
12440                "                  outRange5:(NSRange)out_range5\n"
12441                "                  outRange6:(NSRange)out_range6\n"
12442                "                  outRange7:(NSRange)out_range7\n"
12443                "                  outRange8:(NSRange)out_range8\n"
12444                "                  outRange9:(NSRange)out_range9;");
12445 
12446   // When the function name has to be wrapped.
12447   FormatStyle Style = getLLVMStyle();
12448   // ObjC ignores IndentWrappedFunctionNames when wrapping methods
12449   // and always indents instead.
12450   Style.IndentWrappedFunctionNames = false;
12451   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
12452                "    veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
12453                "               anotherName:(NSString)bbbbbbbbbbbbbb {\n"
12454                "}",
12455                Style);
12456   Style.IndentWrappedFunctionNames = true;
12457   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
12458                "    veryLooooooooooongName:(NSString)cccccccccccccc\n"
12459                "               anotherName:(NSString)dddddddddddddd {\n"
12460                "}",
12461                Style);
12462 
12463   verifyFormat("- (int)sum:(vector<int>)numbers;");
12464   verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
12465   // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
12466   // protocol lists (but not for template classes):
12467   // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
12468 
12469   verifyFormat("- (int (*)())foo:(int (*)())f;");
12470   verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
12471 
12472   // If there's no return type (very rare in practice!), LLVM and Google style
12473   // agree.
12474   verifyFormat("- foo;");
12475   verifyFormat("- foo:(int)f;");
12476   verifyGoogleFormat("- foo:(int)foo;");
12477 }
12478 
12479 TEST_F(FormatTest, BreaksStringLiterals) {
12480   EXPECT_EQ("\"some text \"\n"
12481             "\"other\";",
12482             format("\"some text other\";", getLLVMStyleWithColumns(12)));
12483   EXPECT_EQ("\"some text \"\n"
12484             "\"other\";",
12485             format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
12486   EXPECT_EQ(
12487       "#define A  \\\n"
12488       "  \"some \"  \\\n"
12489       "  \"text \"  \\\n"
12490       "  \"other\";",
12491       format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
12492   EXPECT_EQ(
12493       "#define A  \\\n"
12494       "  \"so \"    \\\n"
12495       "  \"text \"  \\\n"
12496       "  \"other\";",
12497       format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
12498 
12499   EXPECT_EQ("\"some text\"",
12500             format("\"some text\"", getLLVMStyleWithColumns(1)));
12501   EXPECT_EQ("\"some text\"",
12502             format("\"some text\"", getLLVMStyleWithColumns(11)));
12503   EXPECT_EQ("\"some \"\n"
12504             "\"text\"",
12505             format("\"some text\"", getLLVMStyleWithColumns(10)));
12506   EXPECT_EQ("\"some \"\n"
12507             "\"text\"",
12508             format("\"some text\"", getLLVMStyleWithColumns(7)));
12509   EXPECT_EQ("\"some\"\n"
12510             "\" tex\"\n"
12511             "\"t\"",
12512             format("\"some text\"", getLLVMStyleWithColumns(6)));
12513   EXPECT_EQ("\"some\"\n"
12514             "\" tex\"\n"
12515             "\" and\"",
12516             format("\"some tex and\"", getLLVMStyleWithColumns(6)));
12517   EXPECT_EQ("\"some\"\n"
12518             "\"/tex\"\n"
12519             "\"/and\"",
12520             format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
12521 
12522   EXPECT_EQ("variable =\n"
12523             "    \"long string \"\n"
12524             "    \"literal\";",
12525             format("variable = \"long string literal\";",
12526                    getLLVMStyleWithColumns(20)));
12527 
12528   EXPECT_EQ("variable = f(\n"
12529             "    \"long string \"\n"
12530             "    \"literal\",\n"
12531             "    short,\n"
12532             "    loooooooooooooooooooong);",
12533             format("variable = f(\"long string literal\", short, "
12534                    "loooooooooooooooooooong);",
12535                    getLLVMStyleWithColumns(20)));
12536 
12537   EXPECT_EQ(
12538       "f(g(\"long string \"\n"
12539       "    \"literal\"),\n"
12540       "  b);",
12541       format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20)));
12542   EXPECT_EQ("f(g(\"long string \"\n"
12543             "    \"literal\",\n"
12544             "    a),\n"
12545             "  b);",
12546             format("f(g(\"long string literal\", a), b);",
12547                    getLLVMStyleWithColumns(20)));
12548   EXPECT_EQ(
12549       "f(\"one two\".split(\n"
12550       "    variable));",
12551       format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
12552   EXPECT_EQ("f(\"one two three four five six \"\n"
12553             "  \"seven\".split(\n"
12554             "      really_looooong_variable));",
12555             format("f(\"one two three four five six seven\"."
12556                    "split(really_looooong_variable));",
12557                    getLLVMStyleWithColumns(33)));
12558 
12559   EXPECT_EQ("f(\"some \"\n"
12560             "  \"text\",\n"
12561             "  other);",
12562             format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
12563 
12564   // Only break as a last resort.
12565   verifyFormat(
12566       "aaaaaaaaaaaaaaaaaaaa(\n"
12567       "    aaaaaaaaaaaaaaaaaaaa,\n"
12568       "    aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
12569 
12570   EXPECT_EQ("\"splitmea\"\n"
12571             "\"trandomp\"\n"
12572             "\"oint\"",
12573             format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
12574 
12575   EXPECT_EQ("\"split/\"\n"
12576             "\"pathat/\"\n"
12577             "\"slashes\"",
12578             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
12579 
12580   EXPECT_EQ("\"split/\"\n"
12581             "\"pathat/\"\n"
12582             "\"slashes\"",
12583             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
12584   EXPECT_EQ("\"split at \"\n"
12585             "\"spaces/at/\"\n"
12586             "\"slashes.at.any$\"\n"
12587             "\"non-alphanumeric%\"\n"
12588             "\"1111111111characte\"\n"
12589             "\"rs\"",
12590             format("\"split at "
12591                    "spaces/at/"
12592                    "slashes.at."
12593                    "any$non-"
12594                    "alphanumeric%"
12595                    "1111111111characte"
12596                    "rs\"",
12597                    getLLVMStyleWithColumns(20)));
12598 
12599   // Verify that splitting the strings understands
12600   // Style::AlwaysBreakBeforeMultilineStrings.
12601   EXPECT_EQ("aaaaaaaaaaaa(\n"
12602             "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
12603             "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
12604             format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa "
12605                    "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
12606                    "aaaaaaaaaaaaaaaaaaaaaa\");",
12607                    getGoogleStyle()));
12608   EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
12609             "       \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
12610             format("return \"aaaaaaaaaaaaaaaaaaaaaa "
12611                    "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
12612                    "aaaaaaaaaaaaaaaaaaaaaa\";",
12613                    getGoogleStyle()));
12614   EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
12615             "                \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
12616             format("llvm::outs() << "
12617                    "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
12618                    "aaaaaaaaaaaaaaaaaaa\";"));
12619   EXPECT_EQ("ffff(\n"
12620             "    {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
12621             "     \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
12622             format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
12623                    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
12624                    getGoogleStyle()));
12625 
12626   FormatStyle Style = getLLVMStyleWithColumns(12);
12627   Style.BreakStringLiterals = false;
12628   EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style));
12629 
12630   FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
12631   AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
12632   EXPECT_EQ("#define A \\\n"
12633             "  \"some \" \\\n"
12634             "  \"text \" \\\n"
12635             "  \"other\";",
12636             format("#define A \"some text other\";", AlignLeft));
12637 }
12638 
12639 TEST_F(FormatTest, BreaksStringLiteralsAtColumnLimit) {
12640   EXPECT_EQ("C a = \"some more \"\n"
12641             "      \"text\";",
12642             format("C a = \"some more text\";", getLLVMStyleWithColumns(18)));
12643 }
12644 
12645 TEST_F(FormatTest, FullyRemoveEmptyLines) {
12646   FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80);
12647   NoEmptyLines.MaxEmptyLinesToKeep = 0;
12648   EXPECT_EQ("int i = a(b());",
12649             format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines));
12650 }
12651 
12652 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
12653   EXPECT_EQ(
12654       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
12655       "(\n"
12656       "    \"x\t\");",
12657       format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
12658              "aaaaaaa("
12659              "\"x\t\");"));
12660 }
12661 
12662 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
12663   EXPECT_EQ(
12664       "u8\"utf8 string \"\n"
12665       "u8\"literal\";",
12666       format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
12667   EXPECT_EQ(
12668       "u\"utf16 string \"\n"
12669       "u\"literal\";",
12670       format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
12671   EXPECT_EQ(
12672       "U\"utf32 string \"\n"
12673       "U\"literal\";",
12674       format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
12675   EXPECT_EQ("L\"wide string \"\n"
12676             "L\"literal\";",
12677             format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
12678   EXPECT_EQ("@\"NSString \"\n"
12679             "@\"literal\";",
12680             format("@\"NSString literal\";", getGoogleStyleWithColumns(19)));
12681   verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26));
12682 
12683   // This input makes clang-format try to split the incomplete unicode escape
12684   // sequence, which used to lead to a crasher.
12685   verifyNoCrash(
12686       "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
12687       getLLVMStyleWithColumns(60));
12688 }
12689 
12690 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) {
12691   FormatStyle Style = getGoogleStyleWithColumns(15);
12692   EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style));
12693   EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style));
12694   EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style));
12695   EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style));
12696   EXPECT_EQ("u8R\"x(raw literal)x\";",
12697             format("u8R\"x(raw literal)x\";", Style));
12698 }
12699 
12700 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
12701   FormatStyle Style = getLLVMStyleWithColumns(20);
12702   EXPECT_EQ(
12703       "_T(\"aaaaaaaaaaaaaa\")\n"
12704       "_T(\"aaaaaaaaaaaaaa\")\n"
12705       "_T(\"aaaaaaaaaaaa\")",
12706       format("  _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
12707   EXPECT_EQ("f(x,\n"
12708             "  _T(\"aaaaaaaaaaaa\")\n"
12709             "  _T(\"aaa\"),\n"
12710             "  z);",
12711             format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style));
12712 
12713   // FIXME: Handle embedded spaces in one iteration.
12714   //  EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
12715   //            "_T(\"aaaaaaaaaaaaa\")\n"
12716   //            "_T(\"aaaaaaaaaaaaa\")\n"
12717   //            "_T(\"a\")",
12718   //            format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
12719   //                   getLLVMStyleWithColumns(20)));
12720   EXPECT_EQ(
12721       "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
12722       format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style));
12723   EXPECT_EQ("f(\n"
12724             "#if !TEST\n"
12725             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
12726             "#endif\n"
12727             ");",
12728             format("f(\n"
12729                    "#if !TEST\n"
12730                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
12731                    "#endif\n"
12732                    ");"));
12733   EXPECT_EQ("f(\n"
12734             "\n"
12735             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));",
12736             format("f(\n"
12737                    "\n"
12738                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));"));
12739 }
12740 
12741 TEST_F(FormatTest, BreaksStringLiteralOperands) {
12742   // In a function call with two operands, the second can be broken with no line
12743   // break before it.
12744   EXPECT_EQ(
12745       "func(a, \"long long \"\n"
12746       "        \"long long\");",
12747       format("func(a, \"long long long long\");", getLLVMStyleWithColumns(24)));
12748   // In a function call with three operands, the second must be broken with a
12749   // line break before it.
12750   EXPECT_EQ("func(a,\n"
12751             "     \"long long long \"\n"
12752             "     \"long\",\n"
12753             "     c);",
12754             format("func(a, \"long long long long\", c);",
12755                    getLLVMStyleWithColumns(24)));
12756   // In a function call with three operands, the third must be broken with a
12757   // line break before it.
12758   EXPECT_EQ("func(a, b,\n"
12759             "     \"long long long \"\n"
12760             "     \"long\");",
12761             format("func(a, b, \"long long long long\");",
12762                    getLLVMStyleWithColumns(24)));
12763   // In a function call with three operands, both the second and the third must
12764   // be broken with a line break before them.
12765   EXPECT_EQ("func(a,\n"
12766             "     \"long long long \"\n"
12767             "     \"long\",\n"
12768             "     \"long long long \"\n"
12769             "     \"long\");",
12770             format("func(a, \"long long long long\", \"long long long long\");",
12771                    getLLVMStyleWithColumns(24)));
12772   // In a chain of << with two operands, the second can be broken with no line
12773   // break before it.
12774   EXPECT_EQ("a << \"line line \"\n"
12775             "     \"line\";",
12776             format("a << \"line line line\";", getLLVMStyleWithColumns(20)));
12777   // In a chain of << with three operands, the second can be broken with no line
12778   // break before it.
12779   EXPECT_EQ(
12780       "abcde << \"line \"\n"
12781       "         \"line line\"\n"
12782       "      << c;",
12783       format("abcde << \"line line line\" << c;", getLLVMStyleWithColumns(20)));
12784   // In a chain of << with three operands, the third must be broken with a line
12785   // break before it.
12786   EXPECT_EQ(
12787       "a << b\n"
12788       "  << \"line line \"\n"
12789       "     \"line\";",
12790       format("a << b << \"line line line\";", getLLVMStyleWithColumns(20)));
12791   // In a chain of << with three operands, the second can be broken with no line
12792   // break before it and the third must be broken with a line break before it.
12793   EXPECT_EQ("abcd << \"line line \"\n"
12794             "        \"line\"\n"
12795             "     << \"line line \"\n"
12796             "        \"line\";",
12797             format("abcd << \"line line line\" << \"line line line\";",
12798                    getLLVMStyleWithColumns(20)));
12799   // In a chain of binary operators with two operands, the second can be broken
12800   // with no line break before it.
12801   EXPECT_EQ(
12802       "abcd + \"line line \"\n"
12803       "       \"line line\";",
12804       format("abcd + \"line line line line\";", getLLVMStyleWithColumns(20)));
12805   // In a chain of binary operators with three operands, the second must be
12806   // broken with a line break before it.
12807   EXPECT_EQ("abcd +\n"
12808             "    \"line line \"\n"
12809             "    \"line line\" +\n"
12810             "    e;",
12811             format("abcd + \"line line line line\" + e;",
12812                    getLLVMStyleWithColumns(20)));
12813   // In a function call with two operands, with AlignAfterOpenBracket enabled,
12814   // the first must be broken with a line break before it.
12815   FormatStyle Style = getLLVMStyleWithColumns(25);
12816   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
12817   EXPECT_EQ("someFunction(\n"
12818             "    \"long long long \"\n"
12819             "    \"long\",\n"
12820             "    a);",
12821             format("someFunction(\"long long long long\", a);", Style));
12822 }
12823 
12824 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
12825   EXPECT_EQ(
12826       "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
12827       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
12828       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
12829       format("aaaaaaaaaaa  =  \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
12830              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
12831              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"));
12832 }
12833 
12834 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
12835   EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);",
12836             format("f(g(R\"x(raw literal)x\",   a), b);", getGoogleStyle()));
12837   EXPECT_EQ("fffffffffff(g(R\"x(\n"
12838             "multiline raw string literal xxxxxxxxxxxxxx\n"
12839             ")x\",\n"
12840             "              a),\n"
12841             "            b);",
12842             format("fffffffffff(g(R\"x(\n"
12843                    "multiline raw string literal xxxxxxxxxxxxxx\n"
12844                    ")x\", a), b);",
12845                    getGoogleStyleWithColumns(20)));
12846   EXPECT_EQ("fffffffffff(\n"
12847             "    g(R\"x(qqq\n"
12848             "multiline raw string literal xxxxxxxxxxxxxx\n"
12849             ")x\",\n"
12850             "      a),\n"
12851             "    b);",
12852             format("fffffffffff(g(R\"x(qqq\n"
12853                    "multiline raw string literal xxxxxxxxxxxxxx\n"
12854                    ")x\", a), b);",
12855                    getGoogleStyleWithColumns(20)));
12856 
12857   EXPECT_EQ("fffffffffff(R\"x(\n"
12858             "multiline raw string literal xxxxxxxxxxxxxx\n"
12859             ")x\");",
12860             format("fffffffffff(R\"x(\n"
12861                    "multiline raw string literal xxxxxxxxxxxxxx\n"
12862                    ")x\");",
12863                    getGoogleStyleWithColumns(20)));
12864   EXPECT_EQ("fffffffffff(R\"x(\n"
12865             "multiline raw string literal xxxxxxxxxxxxxx\n"
12866             ")x\" + bbbbbb);",
12867             format("fffffffffff(R\"x(\n"
12868                    "multiline raw string literal xxxxxxxxxxxxxx\n"
12869                    ")x\" +   bbbbbb);",
12870                    getGoogleStyleWithColumns(20)));
12871   EXPECT_EQ("fffffffffff(\n"
12872             "    R\"x(\n"
12873             "multiline raw string literal xxxxxxxxxxxxxx\n"
12874             ")x\" +\n"
12875             "    bbbbbb);",
12876             format("fffffffffff(\n"
12877                    " R\"x(\n"
12878                    "multiline raw string literal xxxxxxxxxxxxxx\n"
12879                    ")x\" + bbbbbb);",
12880                    getGoogleStyleWithColumns(20)));
12881   EXPECT_EQ("fffffffffff(R\"(single line raw string)\" + bbbbbb);",
12882             format("fffffffffff(\n"
12883                    " R\"(single line raw string)\" + bbbbbb);"));
12884 }
12885 
12886 TEST_F(FormatTest, SkipsUnknownStringLiterals) {
12887   verifyFormat("string a = \"unterminated;");
12888   EXPECT_EQ("function(\"unterminated,\n"
12889             "         OtherParameter);",
12890             format("function(  \"unterminated,\n"
12891                    "    OtherParameter);"));
12892 }
12893 
12894 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
12895   FormatStyle Style = getLLVMStyle();
12896   Style.Standard = FormatStyle::LS_Cpp03;
12897   EXPECT_EQ("#define x(_a) printf(\"foo\" _a);",
12898             format("#define x(_a) printf(\"foo\"_a);", Style));
12899 }
12900 
12901 TEST_F(FormatTest, CppLexVersion) {
12902   FormatStyle Style = getLLVMStyle();
12903   // Formatting of x * y differs if x is a type.
12904   verifyFormat("void foo() { MACRO(a * b); }", Style);
12905   verifyFormat("void foo() { MACRO(int *b); }", Style);
12906 
12907   // LLVM style uses latest lexer.
12908   verifyFormat("void foo() { MACRO(char8_t *b); }", Style);
12909   Style.Standard = FormatStyle::LS_Cpp17;
12910   // But in c++17, char8_t isn't a keyword.
12911   verifyFormat("void foo() { MACRO(char8_t * b); }", Style);
12912 }
12913 
12914 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); }
12915 
12916 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
12917   EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
12918             "             \"ddeeefff\");",
12919             format("someFunction(\"aaabbbcccdddeeefff\");",
12920                    getLLVMStyleWithColumns(25)));
12921   EXPECT_EQ("someFunction1234567890(\n"
12922             "    \"aaabbbcccdddeeefff\");",
12923             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
12924                    getLLVMStyleWithColumns(26)));
12925   EXPECT_EQ("someFunction1234567890(\n"
12926             "    \"aaabbbcccdddeeeff\"\n"
12927             "    \"f\");",
12928             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
12929                    getLLVMStyleWithColumns(25)));
12930   EXPECT_EQ("someFunction1234567890(\n"
12931             "    \"aaabbbcccdddeeeff\"\n"
12932             "    \"f\");",
12933             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
12934                    getLLVMStyleWithColumns(24)));
12935   EXPECT_EQ("someFunction(\n"
12936             "    \"aaabbbcc ddde \"\n"
12937             "    \"efff\");",
12938             format("someFunction(\"aaabbbcc ddde efff\");",
12939                    getLLVMStyleWithColumns(25)));
12940   EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
12941             "             \"ddeeefff\");",
12942             format("someFunction(\"aaabbbccc ddeeefff\");",
12943                    getLLVMStyleWithColumns(25)));
12944   EXPECT_EQ("someFunction1234567890(\n"
12945             "    \"aaabb \"\n"
12946             "    \"cccdddeeefff\");",
12947             format("someFunction1234567890(\"aaabb cccdddeeefff\");",
12948                    getLLVMStyleWithColumns(25)));
12949   EXPECT_EQ("#define A          \\\n"
12950             "  string s =       \\\n"
12951             "      \"123456789\"  \\\n"
12952             "      \"0\";         \\\n"
12953             "  int i;",
12954             format("#define A string s = \"1234567890\"; int i;",
12955                    getLLVMStyleWithColumns(20)));
12956   EXPECT_EQ("someFunction(\n"
12957             "    \"aaabbbcc \"\n"
12958             "    \"dddeeefff\");",
12959             format("someFunction(\"aaabbbcc dddeeefff\");",
12960                    getLLVMStyleWithColumns(25)));
12961 }
12962 
12963 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
12964   EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3)));
12965   EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2)));
12966   EXPECT_EQ("\"test\"\n"
12967             "\"\\n\"",
12968             format("\"test\\n\"", getLLVMStyleWithColumns(7)));
12969   EXPECT_EQ("\"tes\\\\\"\n"
12970             "\"n\"",
12971             format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
12972   EXPECT_EQ("\"\\\\\\\\\"\n"
12973             "\"\\n\"",
12974             format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
12975   EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
12976   EXPECT_EQ("\"\\uff01\"\n"
12977             "\"test\"",
12978             format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
12979   EXPECT_EQ("\"\\Uff01ff02\"",
12980             format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
12981   EXPECT_EQ("\"\\x000000000001\"\n"
12982             "\"next\"",
12983             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
12984   EXPECT_EQ("\"\\x000000000001next\"",
12985             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
12986   EXPECT_EQ("\"\\x000000000001\"",
12987             format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
12988   EXPECT_EQ("\"test\"\n"
12989             "\"\\000000\"\n"
12990             "\"000001\"",
12991             format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
12992   EXPECT_EQ("\"test\\000\"\n"
12993             "\"00000000\"\n"
12994             "\"1\"",
12995             format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
12996 }
12997 
12998 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
12999   verifyFormat("void f() {\n"
13000                "  return g() {}\n"
13001                "  void h() {}");
13002   verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
13003                "g();\n"
13004                "}");
13005 }
13006 
13007 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
13008   verifyFormat(
13009       "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
13010 }
13011 
13012 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
13013   verifyFormat("class X {\n"
13014                "  void f() {\n"
13015                "  }\n"
13016                "};",
13017                getLLVMStyleWithColumns(12));
13018 }
13019 
13020 TEST_F(FormatTest, ConfigurableIndentWidth) {
13021   FormatStyle EightIndent = getLLVMStyleWithColumns(18);
13022   EightIndent.IndentWidth = 8;
13023   EightIndent.ContinuationIndentWidth = 8;
13024   verifyFormat("void f() {\n"
13025                "        someFunction();\n"
13026                "        if (true) {\n"
13027                "                f();\n"
13028                "        }\n"
13029                "}",
13030                EightIndent);
13031   verifyFormat("class X {\n"
13032                "        void f() {\n"
13033                "        }\n"
13034                "};",
13035                EightIndent);
13036   verifyFormat("int x[] = {\n"
13037                "        call(),\n"
13038                "        call()};",
13039                EightIndent);
13040 }
13041 
13042 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
13043   verifyFormat("double\n"
13044                "f();",
13045                getLLVMStyleWithColumns(8));
13046 }
13047 
13048 TEST_F(FormatTest, ConfigurableUseOfTab) {
13049   FormatStyle Tab = getLLVMStyleWithColumns(42);
13050   Tab.IndentWidth = 8;
13051   Tab.UseTab = FormatStyle::UT_Always;
13052   Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left;
13053 
13054   EXPECT_EQ("if (aaaaaaaa && // q\n"
13055             "    bb)\t\t// w\n"
13056             "\t;",
13057             format("if (aaaaaaaa &&// q\n"
13058                    "bb)// w\n"
13059                    ";",
13060                    Tab));
13061   EXPECT_EQ("if (aaa && bbb) // w\n"
13062             "\t;",
13063             format("if(aaa&&bbb)// w\n"
13064                    ";",
13065                    Tab));
13066 
13067   verifyFormat("class X {\n"
13068                "\tvoid f() {\n"
13069                "\t\tsomeFunction(parameter1,\n"
13070                "\t\t\t     parameter2);\n"
13071                "\t}\n"
13072                "};",
13073                Tab);
13074   verifyFormat("#define A                        \\\n"
13075                "\tvoid f() {               \\\n"
13076                "\t\tsomeFunction(    \\\n"
13077                "\t\t    parameter1,  \\\n"
13078                "\t\t    parameter2); \\\n"
13079                "\t}",
13080                Tab);
13081   verifyFormat("int a;\t      // x\n"
13082                "int bbbbbbbb; // x\n",
13083                Tab);
13084 
13085   Tab.TabWidth = 4;
13086   Tab.IndentWidth = 8;
13087   verifyFormat("class TabWidth4Indent8 {\n"
13088                "\t\tvoid f() {\n"
13089                "\t\t\t\tsomeFunction(parameter1,\n"
13090                "\t\t\t\t\t\t\t parameter2);\n"
13091                "\t\t}\n"
13092                "};",
13093                Tab);
13094 
13095   Tab.TabWidth = 4;
13096   Tab.IndentWidth = 4;
13097   verifyFormat("class TabWidth4Indent4 {\n"
13098                "\tvoid f() {\n"
13099                "\t\tsomeFunction(parameter1,\n"
13100                "\t\t\t\t\t parameter2);\n"
13101                "\t}\n"
13102                "};",
13103                Tab);
13104 
13105   Tab.TabWidth = 8;
13106   Tab.IndentWidth = 4;
13107   verifyFormat("class TabWidth8Indent4 {\n"
13108                "    void f() {\n"
13109                "\tsomeFunction(parameter1,\n"
13110                "\t\t     parameter2);\n"
13111                "    }\n"
13112                "};",
13113                Tab);
13114 
13115   Tab.TabWidth = 8;
13116   Tab.IndentWidth = 8;
13117   EXPECT_EQ("/*\n"
13118             "\t      a\t\tcomment\n"
13119             "\t      in multiple lines\n"
13120             "       */",
13121             format("   /*\t \t \n"
13122                    " \t \t a\t\tcomment\t \t\n"
13123                    " \t \t in multiple lines\t\n"
13124                    " \t  */",
13125                    Tab));
13126 
13127   Tab.UseTab = FormatStyle::UT_ForIndentation;
13128   verifyFormat("{\n"
13129                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13130                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13131                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13132                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13133                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13134                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13135                "};",
13136                Tab);
13137   verifyFormat("enum AA {\n"
13138                "\ta1, // Force multiple lines\n"
13139                "\ta2,\n"
13140                "\ta3\n"
13141                "};",
13142                Tab);
13143   EXPECT_EQ("if (aaaaaaaa && // q\n"
13144             "    bb)         // w\n"
13145             "\t;",
13146             format("if (aaaaaaaa &&// q\n"
13147                    "bb)// w\n"
13148                    ";",
13149                    Tab));
13150   verifyFormat("class X {\n"
13151                "\tvoid f() {\n"
13152                "\t\tsomeFunction(parameter1,\n"
13153                "\t\t             parameter2);\n"
13154                "\t}\n"
13155                "};",
13156                Tab);
13157   verifyFormat("{\n"
13158                "\tQ(\n"
13159                "\t    {\n"
13160                "\t\t    int a;\n"
13161                "\t\t    someFunction(aaaaaaaa,\n"
13162                "\t\t                 bbbbbbb);\n"
13163                "\t    },\n"
13164                "\t    p);\n"
13165                "}",
13166                Tab);
13167   EXPECT_EQ("{\n"
13168             "\t/* aaaa\n"
13169             "\t   bbbb */\n"
13170             "}",
13171             format("{\n"
13172                    "/* aaaa\n"
13173                    "   bbbb */\n"
13174                    "}",
13175                    Tab));
13176   EXPECT_EQ("{\n"
13177             "\t/*\n"
13178             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13179             "\t  bbbbbbbbbbbbb\n"
13180             "\t*/\n"
13181             "}",
13182             format("{\n"
13183                    "/*\n"
13184                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13185                    "*/\n"
13186                    "}",
13187                    Tab));
13188   EXPECT_EQ("{\n"
13189             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13190             "\t// bbbbbbbbbbbbb\n"
13191             "}",
13192             format("{\n"
13193                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13194                    "}",
13195                    Tab));
13196   EXPECT_EQ("{\n"
13197             "\t/*\n"
13198             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13199             "\t  bbbbbbbbbbbbb\n"
13200             "\t*/\n"
13201             "}",
13202             format("{\n"
13203                    "\t/*\n"
13204                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13205                    "\t*/\n"
13206                    "}",
13207                    Tab));
13208   EXPECT_EQ("{\n"
13209             "\t/*\n"
13210             "\n"
13211             "\t*/\n"
13212             "}",
13213             format("{\n"
13214                    "\t/*\n"
13215                    "\n"
13216                    "\t*/\n"
13217                    "}",
13218                    Tab));
13219   EXPECT_EQ("{\n"
13220             "\t/*\n"
13221             " asdf\n"
13222             "\t*/\n"
13223             "}",
13224             format("{\n"
13225                    "\t/*\n"
13226                    " asdf\n"
13227                    "\t*/\n"
13228                    "}",
13229                    Tab));
13230 
13231   Tab.UseTab = FormatStyle::UT_Never;
13232   EXPECT_EQ("/*\n"
13233             "              a\t\tcomment\n"
13234             "              in multiple lines\n"
13235             "       */",
13236             format("   /*\t \t \n"
13237                    " \t \t a\t\tcomment\t \t\n"
13238                    " \t \t in multiple lines\t\n"
13239                    " \t  */",
13240                    Tab));
13241   EXPECT_EQ("/* some\n"
13242             "   comment */",
13243             format(" \t \t /* some\n"
13244                    " \t \t    comment */",
13245                    Tab));
13246   EXPECT_EQ("int a; /* some\n"
13247             "   comment */",
13248             format(" \t \t int a; /* some\n"
13249                    " \t \t    comment */",
13250                    Tab));
13251 
13252   EXPECT_EQ("int a; /* some\n"
13253             "comment */",
13254             format(" \t \t int\ta; /* some\n"
13255                    " \t \t    comment */",
13256                    Tab));
13257   EXPECT_EQ("f(\"\t\t\"); /* some\n"
13258             "    comment */",
13259             format(" \t \t f(\"\t\t\"); /* some\n"
13260                    " \t \t    comment */",
13261                    Tab));
13262   EXPECT_EQ("{\n"
13263             "        /*\n"
13264             "         * Comment\n"
13265             "         */\n"
13266             "        int i;\n"
13267             "}",
13268             format("{\n"
13269                    "\t/*\n"
13270                    "\t * Comment\n"
13271                    "\t */\n"
13272                    "\t int i;\n"
13273                    "}",
13274                    Tab));
13275 
13276   Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
13277   Tab.TabWidth = 8;
13278   Tab.IndentWidth = 8;
13279   EXPECT_EQ("if (aaaaaaaa && // q\n"
13280             "    bb)         // w\n"
13281             "\t;",
13282             format("if (aaaaaaaa &&// q\n"
13283                    "bb)// w\n"
13284                    ";",
13285                    Tab));
13286   EXPECT_EQ("if (aaa && bbb) // w\n"
13287             "\t;",
13288             format("if(aaa&&bbb)// w\n"
13289                    ";",
13290                    Tab));
13291   verifyFormat("class X {\n"
13292                "\tvoid f() {\n"
13293                "\t\tsomeFunction(parameter1,\n"
13294                "\t\t\t     parameter2);\n"
13295                "\t}\n"
13296                "};",
13297                Tab);
13298   verifyFormat("#define A                        \\\n"
13299                "\tvoid f() {               \\\n"
13300                "\t\tsomeFunction(    \\\n"
13301                "\t\t    parameter1,  \\\n"
13302                "\t\t    parameter2); \\\n"
13303                "\t}",
13304                Tab);
13305   Tab.TabWidth = 4;
13306   Tab.IndentWidth = 8;
13307   verifyFormat("class TabWidth4Indent8 {\n"
13308                "\t\tvoid f() {\n"
13309                "\t\t\t\tsomeFunction(parameter1,\n"
13310                "\t\t\t\t\t\t\t parameter2);\n"
13311                "\t\t}\n"
13312                "};",
13313                Tab);
13314   Tab.TabWidth = 4;
13315   Tab.IndentWidth = 4;
13316   verifyFormat("class TabWidth4Indent4 {\n"
13317                "\tvoid f() {\n"
13318                "\t\tsomeFunction(parameter1,\n"
13319                "\t\t\t\t\t parameter2);\n"
13320                "\t}\n"
13321                "};",
13322                Tab);
13323   Tab.TabWidth = 8;
13324   Tab.IndentWidth = 4;
13325   verifyFormat("class TabWidth8Indent4 {\n"
13326                "    void f() {\n"
13327                "\tsomeFunction(parameter1,\n"
13328                "\t\t     parameter2);\n"
13329                "    }\n"
13330                "};",
13331                Tab);
13332   Tab.TabWidth = 8;
13333   Tab.IndentWidth = 8;
13334   EXPECT_EQ("/*\n"
13335             "\t      a\t\tcomment\n"
13336             "\t      in multiple lines\n"
13337             "       */",
13338             format("   /*\t \t \n"
13339                    " \t \t a\t\tcomment\t \t\n"
13340                    " \t \t in multiple lines\t\n"
13341                    " \t  */",
13342                    Tab));
13343   verifyFormat("{\n"
13344                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13345                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13346                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13347                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13348                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13349                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13350                "};",
13351                Tab);
13352   verifyFormat("enum AA {\n"
13353                "\ta1, // Force multiple lines\n"
13354                "\ta2,\n"
13355                "\ta3\n"
13356                "};",
13357                Tab);
13358   EXPECT_EQ("if (aaaaaaaa && // q\n"
13359             "    bb)         // w\n"
13360             "\t;",
13361             format("if (aaaaaaaa &&// q\n"
13362                    "bb)// w\n"
13363                    ";",
13364                    Tab));
13365   verifyFormat("class X {\n"
13366                "\tvoid f() {\n"
13367                "\t\tsomeFunction(parameter1,\n"
13368                "\t\t\t     parameter2);\n"
13369                "\t}\n"
13370                "};",
13371                Tab);
13372   verifyFormat("{\n"
13373                "\tQ(\n"
13374                "\t    {\n"
13375                "\t\t    int a;\n"
13376                "\t\t    someFunction(aaaaaaaa,\n"
13377                "\t\t\t\t bbbbbbb);\n"
13378                "\t    },\n"
13379                "\t    p);\n"
13380                "}",
13381                Tab);
13382   EXPECT_EQ("{\n"
13383             "\t/* aaaa\n"
13384             "\t   bbbb */\n"
13385             "}",
13386             format("{\n"
13387                    "/* aaaa\n"
13388                    "   bbbb */\n"
13389                    "}",
13390                    Tab));
13391   EXPECT_EQ("{\n"
13392             "\t/*\n"
13393             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13394             "\t  bbbbbbbbbbbbb\n"
13395             "\t*/\n"
13396             "}",
13397             format("{\n"
13398                    "/*\n"
13399                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13400                    "*/\n"
13401                    "}",
13402                    Tab));
13403   EXPECT_EQ("{\n"
13404             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13405             "\t// bbbbbbbbbbbbb\n"
13406             "}",
13407             format("{\n"
13408                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13409                    "}",
13410                    Tab));
13411   EXPECT_EQ("{\n"
13412             "\t/*\n"
13413             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13414             "\t  bbbbbbbbbbbbb\n"
13415             "\t*/\n"
13416             "}",
13417             format("{\n"
13418                    "\t/*\n"
13419                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13420                    "\t*/\n"
13421                    "}",
13422                    Tab));
13423   EXPECT_EQ("{\n"
13424             "\t/*\n"
13425             "\n"
13426             "\t*/\n"
13427             "}",
13428             format("{\n"
13429                    "\t/*\n"
13430                    "\n"
13431                    "\t*/\n"
13432                    "}",
13433                    Tab));
13434   EXPECT_EQ("{\n"
13435             "\t/*\n"
13436             " asdf\n"
13437             "\t*/\n"
13438             "}",
13439             format("{\n"
13440                    "\t/*\n"
13441                    " asdf\n"
13442                    "\t*/\n"
13443                    "}",
13444                    Tab));
13445   EXPECT_EQ("/* some\n"
13446             "   comment */",
13447             format(" \t \t /* some\n"
13448                    " \t \t    comment */",
13449                    Tab));
13450   EXPECT_EQ("int a; /* some\n"
13451             "   comment */",
13452             format(" \t \t int a; /* some\n"
13453                    " \t \t    comment */",
13454                    Tab));
13455   EXPECT_EQ("int a; /* some\n"
13456             "comment */",
13457             format(" \t \t int\ta; /* some\n"
13458                    " \t \t    comment */",
13459                    Tab));
13460   EXPECT_EQ("f(\"\t\t\"); /* some\n"
13461             "    comment */",
13462             format(" \t \t f(\"\t\t\"); /* some\n"
13463                    " \t \t    comment */",
13464                    Tab));
13465   EXPECT_EQ("{\n"
13466             "\t/*\n"
13467             "\t * Comment\n"
13468             "\t */\n"
13469             "\tint i;\n"
13470             "}",
13471             format("{\n"
13472                    "\t/*\n"
13473                    "\t * Comment\n"
13474                    "\t */\n"
13475                    "\t int i;\n"
13476                    "}",
13477                    Tab));
13478   Tab.TabWidth = 2;
13479   Tab.IndentWidth = 2;
13480   EXPECT_EQ("{\n"
13481             "\t/* aaaa\n"
13482             "\t\t bbbb */\n"
13483             "}",
13484             format("{\n"
13485                    "/* aaaa\n"
13486                    "\t bbbb */\n"
13487                    "}",
13488                    Tab));
13489   EXPECT_EQ("{\n"
13490             "\t/*\n"
13491             "\t\taaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13492             "\t\tbbbbbbbbbbbbb\n"
13493             "\t*/\n"
13494             "}",
13495             format("{\n"
13496                    "/*\n"
13497                    "\taaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13498                    "*/\n"
13499                    "}",
13500                    Tab));
13501   Tab.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
13502   Tab.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
13503   Tab.TabWidth = 4;
13504   Tab.IndentWidth = 4;
13505   verifyFormat("class Assign {\n"
13506                "\tvoid f() {\n"
13507                "\t\tint         x      = 123;\n"
13508                "\t\tint         random = 4;\n"
13509                "\t\tstd::string alphabet =\n"
13510                "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
13511                "\t}\n"
13512                "};",
13513                Tab);
13514 
13515   Tab.UseTab = FormatStyle::UT_AlignWithSpaces;
13516   Tab.TabWidth = 8;
13517   Tab.IndentWidth = 8;
13518   EXPECT_EQ("if (aaaaaaaa && // q\n"
13519             "    bb)         // w\n"
13520             "\t;",
13521             format("if (aaaaaaaa &&// q\n"
13522                    "bb)// w\n"
13523                    ";",
13524                    Tab));
13525   EXPECT_EQ("if (aaa && bbb) // w\n"
13526             "\t;",
13527             format("if(aaa&&bbb)// w\n"
13528                    ";",
13529                    Tab));
13530   verifyFormat("class X {\n"
13531                "\tvoid f() {\n"
13532                "\t\tsomeFunction(parameter1,\n"
13533                "\t\t             parameter2);\n"
13534                "\t}\n"
13535                "};",
13536                Tab);
13537   verifyFormat("#define A                        \\\n"
13538                "\tvoid f() {               \\\n"
13539                "\t\tsomeFunction(    \\\n"
13540                "\t\t    parameter1,  \\\n"
13541                "\t\t    parameter2); \\\n"
13542                "\t}",
13543                Tab);
13544   Tab.TabWidth = 4;
13545   Tab.IndentWidth = 8;
13546   verifyFormat("class TabWidth4Indent8 {\n"
13547                "\t\tvoid f() {\n"
13548                "\t\t\t\tsomeFunction(parameter1,\n"
13549                "\t\t\t\t             parameter2);\n"
13550                "\t\t}\n"
13551                "};",
13552                Tab);
13553   Tab.TabWidth = 4;
13554   Tab.IndentWidth = 4;
13555   verifyFormat("class TabWidth4Indent4 {\n"
13556                "\tvoid f() {\n"
13557                "\t\tsomeFunction(parameter1,\n"
13558                "\t\t             parameter2);\n"
13559                "\t}\n"
13560                "};",
13561                Tab);
13562   Tab.TabWidth = 8;
13563   Tab.IndentWidth = 4;
13564   verifyFormat("class TabWidth8Indent4 {\n"
13565                "    void f() {\n"
13566                "\tsomeFunction(parameter1,\n"
13567                "\t             parameter2);\n"
13568                "    }\n"
13569                "};",
13570                Tab);
13571   Tab.TabWidth = 8;
13572   Tab.IndentWidth = 8;
13573   EXPECT_EQ("/*\n"
13574             "              a\t\tcomment\n"
13575             "              in multiple lines\n"
13576             "       */",
13577             format("   /*\t \t \n"
13578                    " \t \t a\t\tcomment\t \t\n"
13579                    " \t \t in multiple lines\t\n"
13580                    " \t  */",
13581                    Tab));
13582   verifyFormat("{\n"
13583                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13584                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13585                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13586                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13587                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13588                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13589                "};",
13590                Tab);
13591   verifyFormat("enum AA {\n"
13592                "\ta1, // Force multiple lines\n"
13593                "\ta2,\n"
13594                "\ta3\n"
13595                "};",
13596                Tab);
13597   EXPECT_EQ("if (aaaaaaaa && // q\n"
13598             "    bb)         // w\n"
13599             "\t;",
13600             format("if (aaaaaaaa &&// q\n"
13601                    "bb)// w\n"
13602                    ";",
13603                    Tab));
13604   verifyFormat("class X {\n"
13605                "\tvoid f() {\n"
13606                "\t\tsomeFunction(parameter1,\n"
13607                "\t\t             parameter2);\n"
13608                "\t}\n"
13609                "};",
13610                Tab);
13611   verifyFormat("{\n"
13612                "\tQ(\n"
13613                "\t    {\n"
13614                "\t\t    int a;\n"
13615                "\t\t    someFunction(aaaaaaaa,\n"
13616                "\t\t                 bbbbbbb);\n"
13617                "\t    },\n"
13618                "\t    p);\n"
13619                "}",
13620                Tab);
13621   EXPECT_EQ("{\n"
13622             "\t/* aaaa\n"
13623             "\t   bbbb */\n"
13624             "}",
13625             format("{\n"
13626                    "/* aaaa\n"
13627                    "   bbbb */\n"
13628                    "}",
13629                    Tab));
13630   EXPECT_EQ("{\n"
13631             "\t/*\n"
13632             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13633             "\t  bbbbbbbbbbbbb\n"
13634             "\t*/\n"
13635             "}",
13636             format("{\n"
13637                    "/*\n"
13638                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13639                    "*/\n"
13640                    "}",
13641                    Tab));
13642   EXPECT_EQ("{\n"
13643             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13644             "\t// bbbbbbbbbbbbb\n"
13645             "}",
13646             format("{\n"
13647                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13648                    "}",
13649                    Tab));
13650   EXPECT_EQ("{\n"
13651             "\t/*\n"
13652             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13653             "\t  bbbbbbbbbbbbb\n"
13654             "\t*/\n"
13655             "}",
13656             format("{\n"
13657                    "\t/*\n"
13658                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13659                    "\t*/\n"
13660                    "}",
13661                    Tab));
13662   EXPECT_EQ("{\n"
13663             "\t/*\n"
13664             "\n"
13665             "\t*/\n"
13666             "}",
13667             format("{\n"
13668                    "\t/*\n"
13669                    "\n"
13670                    "\t*/\n"
13671                    "}",
13672                    Tab));
13673   EXPECT_EQ("{\n"
13674             "\t/*\n"
13675             " asdf\n"
13676             "\t*/\n"
13677             "}",
13678             format("{\n"
13679                    "\t/*\n"
13680                    " asdf\n"
13681                    "\t*/\n"
13682                    "}",
13683                    Tab));
13684   EXPECT_EQ("/* some\n"
13685             "   comment */",
13686             format(" \t \t /* some\n"
13687                    " \t \t    comment */",
13688                    Tab));
13689   EXPECT_EQ("int a; /* some\n"
13690             "   comment */",
13691             format(" \t \t int a; /* some\n"
13692                    " \t \t    comment */",
13693                    Tab));
13694   EXPECT_EQ("int a; /* some\n"
13695             "comment */",
13696             format(" \t \t int\ta; /* some\n"
13697                    " \t \t    comment */",
13698                    Tab));
13699   EXPECT_EQ("f(\"\t\t\"); /* some\n"
13700             "    comment */",
13701             format(" \t \t f(\"\t\t\"); /* some\n"
13702                    " \t \t    comment */",
13703                    Tab));
13704   EXPECT_EQ("{\n"
13705             "\t/*\n"
13706             "\t * Comment\n"
13707             "\t */\n"
13708             "\tint i;\n"
13709             "}",
13710             format("{\n"
13711                    "\t/*\n"
13712                    "\t * Comment\n"
13713                    "\t */\n"
13714                    "\t int i;\n"
13715                    "}",
13716                    Tab));
13717   Tab.TabWidth = 2;
13718   Tab.IndentWidth = 2;
13719   EXPECT_EQ("{\n"
13720             "\t/* aaaa\n"
13721             "\t   bbbb */\n"
13722             "}",
13723             format("{\n"
13724                    "/* aaaa\n"
13725                    "   bbbb */\n"
13726                    "}",
13727                    Tab));
13728   EXPECT_EQ("{\n"
13729             "\t/*\n"
13730             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13731             "\t  bbbbbbbbbbbbb\n"
13732             "\t*/\n"
13733             "}",
13734             format("{\n"
13735                    "/*\n"
13736                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13737                    "*/\n"
13738                    "}",
13739                    Tab));
13740   Tab.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
13741   Tab.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
13742   Tab.TabWidth = 4;
13743   Tab.IndentWidth = 4;
13744   verifyFormat("class Assign {\n"
13745                "\tvoid f() {\n"
13746                "\t\tint         x      = 123;\n"
13747                "\t\tint         random = 4;\n"
13748                "\t\tstd::string alphabet =\n"
13749                "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
13750                "\t}\n"
13751                "};",
13752                Tab);
13753   Tab.AlignOperands = FormatStyle::OAS_Align;
13754   verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb +\n"
13755                "                 cccccccccccccccccccc;",
13756                Tab);
13757   // no alignment
13758   verifyFormat("int aaaaaaaaaa =\n"
13759                "\tbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
13760                Tab);
13761   verifyFormat("return aaaaaaaaaaaaaaaa ? 111111111111111\n"
13762                "       : bbbbbbbbbbbbbb ? 222222222222222\n"
13763                "                        : 333333333333333;",
13764                Tab);
13765   Tab.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
13766   Tab.AlignOperands = FormatStyle::OAS_AlignAfterOperator;
13767   verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb\n"
13768                "               + cccccccccccccccccccc;",
13769                Tab);
13770 }
13771 
13772 TEST_F(FormatTest, ZeroTabWidth) {
13773   FormatStyle Tab = getLLVMStyleWithColumns(42);
13774   Tab.IndentWidth = 8;
13775   Tab.UseTab = FormatStyle::UT_Never;
13776   Tab.TabWidth = 0;
13777   EXPECT_EQ("void a(){\n"
13778             "    // line starts with '\t'\n"
13779             "};",
13780             format("void a(){\n"
13781                    "\t// line starts with '\t'\n"
13782                    "};",
13783                    Tab));
13784 
13785   EXPECT_EQ("void a(){\n"
13786             "    // line starts with '\t'\n"
13787             "};",
13788             format("void a(){\n"
13789                    "\t\t// line starts with '\t'\n"
13790                    "};",
13791                    Tab));
13792 
13793   Tab.UseTab = FormatStyle::UT_ForIndentation;
13794   EXPECT_EQ("void a(){\n"
13795             "    // line starts with '\t'\n"
13796             "};",
13797             format("void a(){\n"
13798                    "\t// line starts with '\t'\n"
13799                    "};",
13800                    Tab));
13801 
13802   EXPECT_EQ("void a(){\n"
13803             "    // line starts with '\t'\n"
13804             "};",
13805             format("void a(){\n"
13806                    "\t\t// line starts with '\t'\n"
13807                    "};",
13808                    Tab));
13809 
13810   Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
13811   EXPECT_EQ("void a(){\n"
13812             "    // line starts with '\t'\n"
13813             "};",
13814             format("void a(){\n"
13815                    "\t// line starts with '\t'\n"
13816                    "};",
13817                    Tab));
13818 
13819   EXPECT_EQ("void a(){\n"
13820             "    // line starts with '\t'\n"
13821             "};",
13822             format("void a(){\n"
13823                    "\t\t// line starts with '\t'\n"
13824                    "};",
13825                    Tab));
13826 
13827   Tab.UseTab = FormatStyle::UT_AlignWithSpaces;
13828   EXPECT_EQ("void a(){\n"
13829             "    // line starts with '\t'\n"
13830             "};",
13831             format("void a(){\n"
13832                    "\t// line starts with '\t'\n"
13833                    "};",
13834                    Tab));
13835 
13836   EXPECT_EQ("void a(){\n"
13837             "    // line starts with '\t'\n"
13838             "};",
13839             format("void a(){\n"
13840                    "\t\t// line starts with '\t'\n"
13841                    "};",
13842                    Tab));
13843 
13844   Tab.UseTab = FormatStyle::UT_Always;
13845   EXPECT_EQ("void a(){\n"
13846             "// line starts with '\t'\n"
13847             "};",
13848             format("void a(){\n"
13849                    "\t// line starts with '\t'\n"
13850                    "};",
13851                    Tab));
13852 
13853   EXPECT_EQ("void a(){\n"
13854             "// line starts with '\t'\n"
13855             "};",
13856             format("void a(){\n"
13857                    "\t\t// line starts with '\t'\n"
13858                    "};",
13859                    Tab));
13860 }
13861 
13862 TEST_F(FormatTest, CalculatesOriginalColumn) {
13863   EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
13864             "q\"; /* some\n"
13865             "       comment */",
13866             format("  \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
13867                    "q\"; /* some\n"
13868                    "       comment */",
13869                    getLLVMStyle()));
13870   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
13871             "/* some\n"
13872             "   comment */",
13873             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
13874                    " /* some\n"
13875                    "    comment */",
13876                    getLLVMStyle()));
13877   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
13878             "qqq\n"
13879             "/* some\n"
13880             "   comment */",
13881             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
13882                    "qqq\n"
13883                    " /* some\n"
13884                    "    comment */",
13885                    getLLVMStyle()));
13886   EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
13887             "wwww; /* some\n"
13888             "         comment */",
13889             format("  inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
13890                    "wwww; /* some\n"
13891                    "         comment */",
13892                    getLLVMStyle()));
13893 }
13894 
13895 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
13896   FormatStyle NoSpace = getLLVMStyle();
13897   NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
13898 
13899   verifyFormat("while(true)\n"
13900                "  continue;",
13901                NoSpace);
13902   verifyFormat("for(;;)\n"
13903                "  continue;",
13904                NoSpace);
13905   verifyFormat("if(true)\n"
13906                "  f();\n"
13907                "else if(true)\n"
13908                "  f();",
13909                NoSpace);
13910   verifyFormat("do {\n"
13911                "  do_something();\n"
13912                "} while(something());",
13913                NoSpace);
13914   verifyFormat("switch(x) {\n"
13915                "default:\n"
13916                "  break;\n"
13917                "}",
13918                NoSpace);
13919   verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
13920   verifyFormat("size_t x = sizeof(x);", NoSpace);
13921   verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
13922   verifyFormat("auto f(int x) -> typeof(x);", NoSpace);
13923   verifyFormat("auto f(int x) -> _Atomic(x);", NoSpace);
13924   verifyFormat("auto f(int x) -> __underlying_type(x);", NoSpace);
13925   verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
13926   verifyFormat("alignas(128) char a[128];", NoSpace);
13927   verifyFormat("size_t x = alignof(MyType);", NoSpace);
13928   verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
13929   verifyFormat("int f() throw(Deprecated);", NoSpace);
13930   verifyFormat("typedef void (*cb)(int);", NoSpace);
13931   verifyFormat("T A::operator()();", NoSpace);
13932   verifyFormat("X A::operator++(T);", NoSpace);
13933   verifyFormat("auto lambda = []() { return 0; };", NoSpace);
13934 
13935   FormatStyle Space = getLLVMStyle();
13936   Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
13937 
13938   verifyFormat("int f ();", Space);
13939   verifyFormat("void f (int a, T b) {\n"
13940                "  while (true)\n"
13941                "    continue;\n"
13942                "}",
13943                Space);
13944   verifyFormat("if (true)\n"
13945                "  f ();\n"
13946                "else if (true)\n"
13947                "  f ();",
13948                Space);
13949   verifyFormat("do {\n"
13950                "  do_something ();\n"
13951                "} while (something ());",
13952                Space);
13953   verifyFormat("switch (x) {\n"
13954                "default:\n"
13955                "  break;\n"
13956                "}",
13957                Space);
13958   verifyFormat("A::A () : a (1) {}", Space);
13959   verifyFormat("void f () __attribute__ ((asdf));", Space);
13960   verifyFormat("*(&a + 1);\n"
13961                "&((&a)[1]);\n"
13962                "a[(b + c) * d];\n"
13963                "(((a + 1) * 2) + 3) * 4;",
13964                Space);
13965   verifyFormat("#define A(x) x", Space);
13966   verifyFormat("#define A (x) x", Space);
13967   verifyFormat("#if defined(x)\n"
13968                "#endif",
13969                Space);
13970   verifyFormat("auto i = std::make_unique<int> (5);", Space);
13971   verifyFormat("size_t x = sizeof (x);", Space);
13972   verifyFormat("auto f (int x) -> decltype (x);", Space);
13973   verifyFormat("auto f (int x) -> typeof (x);", Space);
13974   verifyFormat("auto f (int x) -> _Atomic (x);", Space);
13975   verifyFormat("auto f (int x) -> __underlying_type (x);", Space);
13976   verifyFormat("int f (T x) noexcept (x.create ());", Space);
13977   verifyFormat("alignas (128) char a[128];", Space);
13978   verifyFormat("size_t x = alignof (MyType);", Space);
13979   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
13980   verifyFormat("int f () throw (Deprecated);", Space);
13981   verifyFormat("typedef void (*cb) (int);", Space);
13982   verifyFormat("T A::operator() ();", Space);
13983   verifyFormat("X A::operator++ (T);", Space);
13984   verifyFormat("auto lambda = [] () { return 0; };", Space);
13985   verifyFormat("int x = int (y);", Space);
13986 
13987   FormatStyle SomeSpace = getLLVMStyle();
13988   SomeSpace.SpaceBeforeParens = FormatStyle::SBPO_NonEmptyParentheses;
13989 
13990   verifyFormat("[]() -> float {}", SomeSpace);
13991   verifyFormat("[] (auto foo) {}", SomeSpace);
13992   verifyFormat("[foo]() -> int {}", SomeSpace);
13993   verifyFormat("int f();", SomeSpace);
13994   verifyFormat("void f (int a, T b) {\n"
13995                "  while (true)\n"
13996                "    continue;\n"
13997                "}",
13998                SomeSpace);
13999   verifyFormat("if (true)\n"
14000                "  f();\n"
14001                "else if (true)\n"
14002                "  f();",
14003                SomeSpace);
14004   verifyFormat("do {\n"
14005                "  do_something();\n"
14006                "} while (something());",
14007                SomeSpace);
14008   verifyFormat("switch (x) {\n"
14009                "default:\n"
14010                "  break;\n"
14011                "}",
14012                SomeSpace);
14013   verifyFormat("A::A() : a (1) {}", SomeSpace);
14014   verifyFormat("void f() __attribute__ ((asdf));", SomeSpace);
14015   verifyFormat("*(&a + 1);\n"
14016                "&((&a)[1]);\n"
14017                "a[(b + c) * d];\n"
14018                "(((a + 1) * 2) + 3) * 4;",
14019                SomeSpace);
14020   verifyFormat("#define A(x) x", SomeSpace);
14021   verifyFormat("#define A (x) x", SomeSpace);
14022   verifyFormat("#if defined(x)\n"
14023                "#endif",
14024                SomeSpace);
14025   verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace);
14026   verifyFormat("size_t x = sizeof (x);", SomeSpace);
14027   verifyFormat("auto f (int x) -> decltype (x);", SomeSpace);
14028   verifyFormat("auto f (int x) -> typeof (x);", SomeSpace);
14029   verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace);
14030   verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace);
14031   verifyFormat("int f (T x) noexcept (x.create());", SomeSpace);
14032   verifyFormat("alignas (128) char a[128];", SomeSpace);
14033   verifyFormat("size_t x = alignof (MyType);", SomeSpace);
14034   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");",
14035                SomeSpace);
14036   verifyFormat("int f() throw (Deprecated);", SomeSpace);
14037   verifyFormat("typedef void (*cb) (int);", SomeSpace);
14038   verifyFormat("T A::operator()();", SomeSpace);
14039   verifyFormat("X A::operator++ (T);", SomeSpace);
14040   verifyFormat("int x = int (y);", SomeSpace);
14041   verifyFormat("auto lambda = []() { return 0; };", SomeSpace);
14042 }
14043 
14044 TEST_F(FormatTest, SpaceAfterLogicalNot) {
14045   FormatStyle Spaces = getLLVMStyle();
14046   Spaces.SpaceAfterLogicalNot = true;
14047 
14048   verifyFormat("bool x = ! y", Spaces);
14049   verifyFormat("if (! isFailure())", Spaces);
14050   verifyFormat("if (! (a && b))", Spaces);
14051   verifyFormat("\"Error!\"", Spaces);
14052   verifyFormat("! ! x", Spaces);
14053 }
14054 
14055 TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
14056   FormatStyle Spaces = getLLVMStyle();
14057 
14058   Spaces.SpacesInParentheses = true;
14059   verifyFormat("do_something( ::globalVar );", Spaces);
14060   verifyFormat("call( x, y, z );", Spaces);
14061   verifyFormat("call();", Spaces);
14062   verifyFormat("std::function<void( int, int )> callback;", Spaces);
14063   verifyFormat("void inFunction() { std::function<void( int, int )> fct; }",
14064                Spaces);
14065   verifyFormat("while ( (bool)1 )\n"
14066                "  continue;",
14067                Spaces);
14068   verifyFormat("for ( ;; )\n"
14069                "  continue;",
14070                Spaces);
14071   verifyFormat("if ( true )\n"
14072                "  f();\n"
14073                "else if ( true )\n"
14074                "  f();",
14075                Spaces);
14076   verifyFormat("do {\n"
14077                "  do_something( (int)i );\n"
14078                "} while ( something() );",
14079                Spaces);
14080   verifyFormat("switch ( x ) {\n"
14081                "default:\n"
14082                "  break;\n"
14083                "}",
14084                Spaces);
14085 
14086   Spaces.SpacesInParentheses = false;
14087   Spaces.SpacesInCStyleCastParentheses = true;
14088   verifyFormat("Type *A = ( Type * )P;", Spaces);
14089   verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
14090   verifyFormat("x = ( int32 )y;", Spaces);
14091   verifyFormat("int a = ( int )(2.0f);", Spaces);
14092   verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
14093   verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
14094   verifyFormat("#define x (( int )-1)", Spaces);
14095 
14096   // Run the first set of tests again with:
14097   Spaces.SpacesInParentheses = false;
14098   Spaces.SpaceInEmptyParentheses = true;
14099   Spaces.SpacesInCStyleCastParentheses = true;
14100   verifyFormat("call(x, y, z);", Spaces);
14101   verifyFormat("call( );", Spaces);
14102   verifyFormat("std::function<void(int, int)> callback;", Spaces);
14103   verifyFormat("while (( bool )1)\n"
14104                "  continue;",
14105                Spaces);
14106   verifyFormat("for (;;)\n"
14107                "  continue;",
14108                Spaces);
14109   verifyFormat("if (true)\n"
14110                "  f( );\n"
14111                "else if (true)\n"
14112                "  f( );",
14113                Spaces);
14114   verifyFormat("do {\n"
14115                "  do_something(( int )i);\n"
14116                "} while (something( ));",
14117                Spaces);
14118   verifyFormat("switch (x) {\n"
14119                "default:\n"
14120                "  break;\n"
14121                "}",
14122                Spaces);
14123 
14124   // Run the first set of tests again with:
14125   Spaces.SpaceAfterCStyleCast = true;
14126   verifyFormat("call(x, y, z);", Spaces);
14127   verifyFormat("call( );", Spaces);
14128   verifyFormat("std::function<void(int, int)> callback;", Spaces);
14129   verifyFormat("while (( bool ) 1)\n"
14130                "  continue;",
14131                Spaces);
14132   verifyFormat("for (;;)\n"
14133                "  continue;",
14134                Spaces);
14135   verifyFormat("if (true)\n"
14136                "  f( );\n"
14137                "else if (true)\n"
14138                "  f( );",
14139                Spaces);
14140   verifyFormat("do {\n"
14141                "  do_something(( int ) i);\n"
14142                "} while (something( ));",
14143                Spaces);
14144   verifyFormat("switch (x) {\n"
14145                "default:\n"
14146                "  break;\n"
14147                "}",
14148                Spaces);
14149 
14150   // Run subset of tests again with:
14151   Spaces.SpacesInCStyleCastParentheses = false;
14152   Spaces.SpaceAfterCStyleCast = true;
14153   verifyFormat("while ((bool) 1)\n"
14154                "  continue;",
14155                Spaces);
14156   verifyFormat("do {\n"
14157                "  do_something((int) i);\n"
14158                "} while (something( ));",
14159                Spaces);
14160 
14161   verifyFormat("size_t idx = (size_t) (ptr - ((char *) file));", Spaces);
14162   verifyFormat("size_t idx = (size_t) a;", Spaces);
14163   verifyFormat("size_t idx = (size_t) (a - 1);", Spaces);
14164   verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces);
14165   verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces);
14166   verifyFormat("size_t idx = (*foo)(a - 1);", Spaces);
14167   verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces);
14168   Spaces.ColumnLimit = 80;
14169   Spaces.IndentWidth = 4;
14170   Spaces.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
14171   verifyFormat("void foo( ) {\n"
14172                "    size_t foo = (*(function))(\n"
14173                "        Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, "
14174                "BarrrrrrrrrrrrLong,\n"
14175                "        FoooooooooLooooong);\n"
14176                "}",
14177                Spaces);
14178   Spaces.SpaceAfterCStyleCast = false;
14179   verifyFormat("size_t idx = (size_t)(ptr - ((char *)file));", Spaces);
14180   verifyFormat("size_t idx = (size_t)a;", Spaces);
14181   verifyFormat("size_t idx = (size_t)(a - 1);", Spaces);
14182   verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces);
14183   verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces);
14184   verifyFormat("size_t idx = (*foo)(a - 1);", Spaces);
14185   verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces);
14186 
14187   verifyFormat("void foo( ) {\n"
14188                "    size_t foo = (*(function))(\n"
14189                "        Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, "
14190                "BarrrrrrrrrrrrLong,\n"
14191                "        FoooooooooLooooong);\n"
14192                "}",
14193                Spaces);
14194 }
14195 
14196 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
14197   verifyFormat("int a[5];");
14198   verifyFormat("a[3] += 42;");
14199 
14200   FormatStyle Spaces = getLLVMStyle();
14201   Spaces.SpacesInSquareBrackets = true;
14202   // Not lambdas.
14203   verifyFormat("int a[ 5 ];", Spaces);
14204   verifyFormat("a[ 3 ] += 42;", Spaces);
14205   verifyFormat("constexpr char hello[]{\"hello\"};", Spaces);
14206   verifyFormat("double &operator[](int i) { return 0; }\n"
14207                "int i;",
14208                Spaces);
14209   verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces);
14210   verifyFormat("int i = a[ a ][ a ]->f();", Spaces);
14211   verifyFormat("int i = (*b)[ a ]->f();", Spaces);
14212   // Lambdas.
14213   verifyFormat("int c = []() -> int { return 2; }();\n", Spaces);
14214   verifyFormat("return [ i, args... ] {};", Spaces);
14215   verifyFormat("int foo = [ &bar ]() {};", Spaces);
14216   verifyFormat("int foo = [ = ]() {};", Spaces);
14217   verifyFormat("int foo = [ & ]() {};", Spaces);
14218   verifyFormat("int foo = [ =, &bar ]() {};", Spaces);
14219   verifyFormat("int foo = [ &bar, = ]() {};", Spaces);
14220 }
14221 
14222 TEST_F(FormatTest, ConfigurableSpaceBeforeBrackets) {
14223   FormatStyle NoSpaceStyle = getLLVMStyle();
14224   verifyFormat("int a[5];", NoSpaceStyle);
14225   verifyFormat("a[3] += 42;", NoSpaceStyle);
14226 
14227   verifyFormat("int a[1];", NoSpaceStyle);
14228   verifyFormat("int 1 [a];", NoSpaceStyle);
14229   verifyFormat("int a[1][2];", NoSpaceStyle);
14230   verifyFormat("a[7] = 5;", NoSpaceStyle);
14231   verifyFormat("int a = (f())[23];", NoSpaceStyle);
14232   verifyFormat("f([] {})", NoSpaceStyle);
14233 
14234   FormatStyle Space = getLLVMStyle();
14235   Space.SpaceBeforeSquareBrackets = true;
14236   verifyFormat("int c = []() -> int { return 2; }();\n", Space);
14237   verifyFormat("return [i, args...] {};", Space);
14238 
14239   verifyFormat("int a [5];", Space);
14240   verifyFormat("a [3] += 42;", Space);
14241   verifyFormat("constexpr char hello []{\"hello\"};", Space);
14242   verifyFormat("double &operator[](int i) { return 0; }\n"
14243                "int i;",
14244                Space);
14245   verifyFormat("std::unique_ptr<int []> foo() {}", Space);
14246   verifyFormat("int i = a [a][a]->f();", Space);
14247   verifyFormat("int i = (*b) [a]->f();", Space);
14248 
14249   verifyFormat("int a [1];", Space);
14250   verifyFormat("int 1 [a];", Space);
14251   verifyFormat("int a [1][2];", Space);
14252   verifyFormat("a [7] = 5;", Space);
14253   verifyFormat("int a = (f()) [23];", Space);
14254   verifyFormat("f([] {})", Space);
14255 }
14256 
14257 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
14258   verifyFormat("int a = 5;");
14259   verifyFormat("a += 42;");
14260   verifyFormat("a or_eq 8;");
14261 
14262   FormatStyle Spaces = getLLVMStyle();
14263   Spaces.SpaceBeforeAssignmentOperators = false;
14264   verifyFormat("int a= 5;", Spaces);
14265   verifyFormat("a+= 42;", Spaces);
14266   verifyFormat("a or_eq 8;", Spaces);
14267 }
14268 
14269 TEST_F(FormatTest, ConfigurableSpaceBeforeColon) {
14270   verifyFormat("class Foo : public Bar {};");
14271   verifyFormat("Foo::Foo() : foo(1) {}");
14272   verifyFormat("for (auto a : b) {\n}");
14273   verifyFormat("int x = a ? b : c;");
14274   verifyFormat("{\n"
14275                "label0:\n"
14276                "  int x = 0;\n"
14277                "}");
14278   verifyFormat("switch (x) {\n"
14279                "case 1:\n"
14280                "default:\n"
14281                "}");
14282   verifyFormat("switch (allBraces) {\n"
14283                "case 1: {\n"
14284                "  break;\n"
14285                "}\n"
14286                "case 2: {\n"
14287                "  [[fallthrough]];\n"
14288                "}\n"
14289                "default: {\n"
14290                "  break;\n"
14291                "}\n"
14292                "}");
14293 
14294   FormatStyle CtorInitializerStyle = getLLVMStyleWithColumns(30);
14295   CtorInitializerStyle.SpaceBeforeCtorInitializerColon = false;
14296   verifyFormat("class Foo : public Bar {};", CtorInitializerStyle);
14297   verifyFormat("Foo::Foo(): foo(1) {}", CtorInitializerStyle);
14298   verifyFormat("for (auto a : b) {\n}", CtorInitializerStyle);
14299   verifyFormat("int x = a ? b : c;", CtorInitializerStyle);
14300   verifyFormat("{\n"
14301                "label1:\n"
14302                "  int x = 0;\n"
14303                "}",
14304                CtorInitializerStyle);
14305   verifyFormat("switch (x) {\n"
14306                "case 1:\n"
14307                "default:\n"
14308                "}",
14309                CtorInitializerStyle);
14310   verifyFormat("switch (allBraces) {\n"
14311                "case 1: {\n"
14312                "  break;\n"
14313                "}\n"
14314                "case 2: {\n"
14315                "  [[fallthrough]];\n"
14316                "}\n"
14317                "default: {\n"
14318                "  break;\n"
14319                "}\n"
14320                "}",
14321                CtorInitializerStyle);
14322   CtorInitializerStyle.BreakConstructorInitializers =
14323       FormatStyle::BCIS_AfterColon;
14324   verifyFormat("Fooooooooooo::Fooooooooooo():\n"
14325                "    aaaaaaaaaaaaaaaa(1),\n"
14326                "    bbbbbbbbbbbbbbbb(2) {}",
14327                CtorInitializerStyle);
14328   CtorInitializerStyle.BreakConstructorInitializers =
14329       FormatStyle::BCIS_BeforeComma;
14330   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
14331                "    : aaaaaaaaaaaaaaaa(1)\n"
14332                "    , bbbbbbbbbbbbbbbb(2) {}",
14333                CtorInitializerStyle);
14334   CtorInitializerStyle.BreakConstructorInitializers =
14335       FormatStyle::BCIS_BeforeColon;
14336   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
14337                "    : aaaaaaaaaaaaaaaa(1),\n"
14338                "      bbbbbbbbbbbbbbbb(2) {}",
14339                CtorInitializerStyle);
14340   CtorInitializerStyle.ConstructorInitializerIndentWidth = 0;
14341   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
14342                ": aaaaaaaaaaaaaaaa(1),\n"
14343                "  bbbbbbbbbbbbbbbb(2) {}",
14344                CtorInitializerStyle);
14345 
14346   FormatStyle InheritanceStyle = getLLVMStyleWithColumns(30);
14347   InheritanceStyle.SpaceBeforeInheritanceColon = false;
14348   verifyFormat("class Foo: public Bar {};", InheritanceStyle);
14349   verifyFormat("Foo::Foo() : foo(1) {}", InheritanceStyle);
14350   verifyFormat("for (auto a : b) {\n}", InheritanceStyle);
14351   verifyFormat("int x = a ? b : c;", InheritanceStyle);
14352   verifyFormat("{\n"
14353                "label2:\n"
14354                "  int x = 0;\n"
14355                "}",
14356                InheritanceStyle);
14357   verifyFormat("switch (x) {\n"
14358                "case 1:\n"
14359                "default:\n"
14360                "}",
14361                InheritanceStyle);
14362   verifyFormat("switch (allBraces) {\n"
14363                "case 1: {\n"
14364                "  break;\n"
14365                "}\n"
14366                "case 2: {\n"
14367                "  [[fallthrough]];\n"
14368                "}\n"
14369                "default: {\n"
14370                "  break;\n"
14371                "}\n"
14372                "}",
14373                InheritanceStyle);
14374   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterComma;
14375   verifyFormat("class Foooooooooooooooooooooo\n"
14376                "    : public aaaaaaaaaaaaaaaaaa,\n"
14377                "      public bbbbbbbbbbbbbbbbbb {\n"
14378                "}",
14379                InheritanceStyle);
14380   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterColon;
14381   verifyFormat("class Foooooooooooooooooooooo:\n"
14382                "    public aaaaaaaaaaaaaaaaaa,\n"
14383                "    public bbbbbbbbbbbbbbbbbb {\n"
14384                "}",
14385                InheritanceStyle);
14386   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeComma;
14387   verifyFormat("class Foooooooooooooooooooooo\n"
14388                "    : public aaaaaaaaaaaaaaaaaa\n"
14389                "    , public bbbbbbbbbbbbbbbbbb {\n"
14390                "}",
14391                InheritanceStyle);
14392   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
14393   verifyFormat("class Foooooooooooooooooooooo\n"
14394                "    : public aaaaaaaaaaaaaaaaaa,\n"
14395                "      public bbbbbbbbbbbbbbbbbb {\n"
14396                "}",
14397                InheritanceStyle);
14398   InheritanceStyle.ConstructorInitializerIndentWidth = 0;
14399   verifyFormat("class Foooooooooooooooooooooo\n"
14400                ": public aaaaaaaaaaaaaaaaaa,\n"
14401                "  public bbbbbbbbbbbbbbbbbb {}",
14402                InheritanceStyle);
14403 
14404   FormatStyle ForLoopStyle = getLLVMStyle();
14405   ForLoopStyle.SpaceBeforeRangeBasedForLoopColon = false;
14406   verifyFormat("class Foo : public Bar {};", ForLoopStyle);
14407   verifyFormat("Foo::Foo() : foo(1) {}", ForLoopStyle);
14408   verifyFormat("for (auto a: b) {\n}", ForLoopStyle);
14409   verifyFormat("int x = a ? b : c;", ForLoopStyle);
14410   verifyFormat("{\n"
14411                "label2:\n"
14412                "  int x = 0;\n"
14413                "}",
14414                ForLoopStyle);
14415   verifyFormat("switch (x) {\n"
14416                "case 1:\n"
14417                "default:\n"
14418                "}",
14419                ForLoopStyle);
14420   verifyFormat("switch (allBraces) {\n"
14421                "case 1: {\n"
14422                "  break;\n"
14423                "}\n"
14424                "case 2: {\n"
14425                "  [[fallthrough]];\n"
14426                "}\n"
14427                "default: {\n"
14428                "  break;\n"
14429                "}\n"
14430                "}",
14431                ForLoopStyle);
14432 
14433   FormatStyle CaseStyle = getLLVMStyle();
14434   CaseStyle.SpaceBeforeCaseColon = true;
14435   verifyFormat("class Foo : public Bar {};", CaseStyle);
14436   verifyFormat("Foo::Foo() : foo(1) {}", CaseStyle);
14437   verifyFormat("for (auto a : b) {\n}", CaseStyle);
14438   verifyFormat("int x = a ? b : c;", CaseStyle);
14439   verifyFormat("switch (x) {\n"
14440                "case 1 :\n"
14441                "default :\n"
14442                "}",
14443                CaseStyle);
14444   verifyFormat("switch (allBraces) {\n"
14445                "case 1 : {\n"
14446                "  break;\n"
14447                "}\n"
14448                "case 2 : {\n"
14449                "  [[fallthrough]];\n"
14450                "}\n"
14451                "default : {\n"
14452                "  break;\n"
14453                "}\n"
14454                "}",
14455                CaseStyle);
14456 
14457   FormatStyle NoSpaceStyle = getLLVMStyle();
14458   EXPECT_EQ(NoSpaceStyle.SpaceBeforeCaseColon, false);
14459   NoSpaceStyle.SpaceBeforeCtorInitializerColon = false;
14460   NoSpaceStyle.SpaceBeforeInheritanceColon = false;
14461   NoSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false;
14462   verifyFormat("class Foo: public Bar {};", NoSpaceStyle);
14463   verifyFormat("Foo::Foo(): foo(1) {}", NoSpaceStyle);
14464   verifyFormat("for (auto a: b) {\n}", NoSpaceStyle);
14465   verifyFormat("int x = a ? b : c;", NoSpaceStyle);
14466   verifyFormat("{\n"
14467                "label3:\n"
14468                "  int x = 0;\n"
14469                "}",
14470                NoSpaceStyle);
14471   verifyFormat("switch (x) {\n"
14472                "case 1:\n"
14473                "default:\n"
14474                "}",
14475                NoSpaceStyle);
14476   verifyFormat("switch (allBraces) {\n"
14477                "case 1: {\n"
14478                "  break;\n"
14479                "}\n"
14480                "case 2: {\n"
14481                "  [[fallthrough]];\n"
14482                "}\n"
14483                "default: {\n"
14484                "  break;\n"
14485                "}\n"
14486                "}",
14487                NoSpaceStyle);
14488 
14489   FormatStyle InvertedSpaceStyle = getLLVMStyle();
14490   InvertedSpaceStyle.SpaceBeforeCaseColon = true;
14491   InvertedSpaceStyle.SpaceBeforeCtorInitializerColon = false;
14492   InvertedSpaceStyle.SpaceBeforeInheritanceColon = false;
14493   InvertedSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false;
14494   verifyFormat("class Foo: public Bar {};", InvertedSpaceStyle);
14495   verifyFormat("Foo::Foo(): foo(1) {}", InvertedSpaceStyle);
14496   verifyFormat("for (auto a: b) {\n}", InvertedSpaceStyle);
14497   verifyFormat("int x = a ? b : c;", InvertedSpaceStyle);
14498   verifyFormat("{\n"
14499                "label3:\n"
14500                "  int x = 0;\n"
14501                "}",
14502                InvertedSpaceStyle);
14503   verifyFormat("switch (x) {\n"
14504                "case 1 :\n"
14505                "case 2 : {\n"
14506                "  break;\n"
14507                "}\n"
14508                "default :\n"
14509                "  break;\n"
14510                "}",
14511                InvertedSpaceStyle);
14512   verifyFormat("switch (allBraces) {\n"
14513                "case 1 : {\n"
14514                "  break;\n"
14515                "}\n"
14516                "case 2 : {\n"
14517                "  [[fallthrough]];\n"
14518                "}\n"
14519                "default : {\n"
14520                "  break;\n"
14521                "}\n"
14522                "}",
14523                InvertedSpaceStyle);
14524 }
14525 
14526 TEST_F(FormatTest, ConfigurableSpaceAroundPointerQualifiers) {
14527   FormatStyle Style = getLLVMStyle();
14528 
14529   Style.PointerAlignment = FormatStyle::PAS_Left;
14530   Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default;
14531   verifyFormat("void* const* x = NULL;", Style);
14532 
14533 #define verifyQualifierSpaces(Code, Pointers, Qualifiers)                      \
14534   do {                                                                         \
14535     Style.PointerAlignment = FormatStyle::Pointers;                            \
14536     Style.SpaceAroundPointerQualifiers = FormatStyle::Qualifiers;              \
14537     verifyFormat(Code, Style);                                                 \
14538   } while (false)
14539 
14540   verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Default);
14541   verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_Default);
14542   verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Default);
14543 
14544   verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Before);
14545   verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Before);
14546   verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Before);
14547 
14548   verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_After);
14549   verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_After);
14550   verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_After);
14551 
14552   verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_Both);
14553   verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Both);
14554   verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Both);
14555 
14556   verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Default);
14557   verifyQualifierSpaces("Foo::operator void const *();", PAS_Right,
14558                         SAPQ_Default);
14559   verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
14560                         SAPQ_Default);
14561 
14562   verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Before);
14563   verifyQualifierSpaces("Foo::operator void const *();", PAS_Right,
14564                         SAPQ_Before);
14565   verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
14566                         SAPQ_Before);
14567 
14568   verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_After);
14569   verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_After);
14570   verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
14571                         SAPQ_After);
14572 
14573   verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_Both);
14574   verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_Both);
14575   verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, SAPQ_Both);
14576 
14577 #undef verifyQualifierSpaces
14578 
14579   FormatStyle Spaces = getLLVMStyle();
14580   Spaces.AttributeMacros.push_back("qualified");
14581   Spaces.PointerAlignment = FormatStyle::PAS_Right;
14582   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default;
14583   verifyFormat("SomeType *volatile *a = NULL;", Spaces);
14584   verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces);
14585   verifyFormat("std::vector<SomeType *const *> x;", Spaces);
14586   verifyFormat("std::vector<SomeType *qualified *> x;", Spaces);
14587   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
14588   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before;
14589   verifyFormat("SomeType * volatile *a = NULL;", Spaces);
14590   verifyFormat("SomeType * __attribute__((attr)) *a = NULL;", Spaces);
14591   verifyFormat("std::vector<SomeType * const *> x;", Spaces);
14592   verifyFormat("std::vector<SomeType * qualified *> x;", Spaces);
14593   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
14594 
14595   // Check that SAPQ_Before doesn't result in extra spaces for PAS_Left.
14596   Spaces.PointerAlignment = FormatStyle::PAS_Left;
14597   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before;
14598   verifyFormat("SomeType* volatile* a = NULL;", Spaces);
14599   verifyFormat("SomeType* __attribute__((attr))* a = NULL;", Spaces);
14600   verifyFormat("std::vector<SomeType* const*> x;", Spaces);
14601   verifyFormat("std::vector<SomeType* qualified*> x;", Spaces);
14602   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
14603   // However, setting it to SAPQ_After should add spaces after __attribute, etc.
14604   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After;
14605   verifyFormat("SomeType* volatile * a = NULL;", Spaces);
14606   verifyFormat("SomeType* __attribute__((attr)) * a = NULL;", Spaces);
14607   verifyFormat("std::vector<SomeType* const *> x;", Spaces);
14608   verifyFormat("std::vector<SomeType* qualified *> x;", Spaces);
14609   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
14610 
14611   // PAS_Middle should not have any noticeable changes even for SAPQ_Both
14612   Spaces.PointerAlignment = FormatStyle::PAS_Middle;
14613   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After;
14614   verifyFormat("SomeType * volatile * a = NULL;", Spaces);
14615   verifyFormat("SomeType * __attribute__((attr)) * a = NULL;", Spaces);
14616   verifyFormat("std::vector<SomeType * const *> x;", Spaces);
14617   verifyFormat("std::vector<SomeType * qualified *> x;", Spaces);
14618   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
14619 }
14620 
14621 TEST_F(FormatTest, AlignConsecutiveMacros) {
14622   FormatStyle Style = getLLVMStyle();
14623   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
14624   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
14625   Style.AlignConsecutiveMacros = FormatStyle::ACS_None;
14626 
14627   verifyFormat("#define a 3\n"
14628                "#define bbbb 4\n"
14629                "#define ccc (5)",
14630                Style);
14631 
14632   verifyFormat("#define f(x) (x * x)\n"
14633                "#define fff(x, y, z) (x * y + z)\n"
14634                "#define ffff(x, y) (x - y)",
14635                Style);
14636 
14637   verifyFormat("#define foo(x, y) (x + y)\n"
14638                "#define bar (5, 6)(2 + 2)",
14639                Style);
14640 
14641   verifyFormat("#define a 3\n"
14642                "#define bbbb 4\n"
14643                "#define ccc (5)\n"
14644                "#define f(x) (x * x)\n"
14645                "#define fff(x, y, z) (x * y + z)\n"
14646                "#define ffff(x, y) (x - y)",
14647                Style);
14648 
14649   Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
14650   verifyFormat("#define a    3\n"
14651                "#define bbbb 4\n"
14652                "#define ccc  (5)",
14653                Style);
14654 
14655   verifyFormat("#define f(x)         (x * x)\n"
14656                "#define fff(x, y, z) (x * y + z)\n"
14657                "#define ffff(x, y)   (x - y)",
14658                Style);
14659 
14660   verifyFormat("#define foo(x, y) (x + y)\n"
14661                "#define bar       (5, 6)(2 + 2)",
14662                Style);
14663 
14664   verifyFormat("#define a            3\n"
14665                "#define bbbb         4\n"
14666                "#define ccc          (5)\n"
14667                "#define f(x)         (x * x)\n"
14668                "#define fff(x, y, z) (x * y + z)\n"
14669                "#define ffff(x, y)   (x - y)",
14670                Style);
14671 
14672   verifyFormat("#define a         5\n"
14673                "#define foo(x, y) (x + y)\n"
14674                "#define CCC       (6)\n"
14675                "auto lambda = []() {\n"
14676                "  auto  ii = 0;\n"
14677                "  float j  = 0;\n"
14678                "  return 0;\n"
14679                "};\n"
14680                "int   i  = 0;\n"
14681                "float i2 = 0;\n"
14682                "auto  v  = type{\n"
14683                "    i = 1,   //\n"
14684                "    (i = 2), //\n"
14685                "    i = 3    //\n"
14686                "};",
14687                Style);
14688 
14689   Style.AlignConsecutiveMacros = FormatStyle::ACS_None;
14690   Style.ColumnLimit = 20;
14691 
14692   verifyFormat("#define a          \\\n"
14693                "  \"aabbbbbbbbbbbb\"\n"
14694                "#define D          \\\n"
14695                "  \"aabbbbbbbbbbbb\" \\\n"
14696                "  \"ccddeeeeeeeee\"\n"
14697                "#define B          \\\n"
14698                "  \"QQQQQQQQQQQQQ\"  \\\n"
14699                "  \"FFFFFFFFFFFFF\"  \\\n"
14700                "  \"LLLLLLLL\"\n",
14701                Style);
14702 
14703   Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
14704   verifyFormat("#define a          \\\n"
14705                "  \"aabbbbbbbbbbbb\"\n"
14706                "#define D          \\\n"
14707                "  \"aabbbbbbbbbbbb\" \\\n"
14708                "  \"ccddeeeeeeeee\"\n"
14709                "#define B          \\\n"
14710                "  \"QQQQQQQQQQQQQ\"  \\\n"
14711                "  \"FFFFFFFFFFFFF\"  \\\n"
14712                "  \"LLLLLLLL\"\n",
14713                Style);
14714 
14715   // Test across comments
14716   Style.MaxEmptyLinesToKeep = 10;
14717   Style.ReflowComments = false;
14718   Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossComments;
14719   EXPECT_EQ("#define a    3\n"
14720             "// line comment\n"
14721             "#define bbbb 4\n"
14722             "#define ccc  (5)",
14723             format("#define a 3\n"
14724                    "// line comment\n"
14725                    "#define bbbb 4\n"
14726                    "#define ccc (5)",
14727                    Style));
14728 
14729   EXPECT_EQ("#define a    3\n"
14730             "/* block comment */\n"
14731             "#define bbbb 4\n"
14732             "#define ccc  (5)",
14733             format("#define a  3\n"
14734                    "/* block comment */\n"
14735                    "#define bbbb 4\n"
14736                    "#define ccc (5)",
14737                    Style));
14738 
14739   EXPECT_EQ("#define a    3\n"
14740             "/* multi-line *\n"
14741             " * block comment */\n"
14742             "#define bbbb 4\n"
14743             "#define ccc  (5)",
14744             format("#define a 3\n"
14745                    "/* multi-line *\n"
14746                    " * block comment */\n"
14747                    "#define bbbb 4\n"
14748                    "#define ccc (5)",
14749                    Style));
14750 
14751   EXPECT_EQ("#define a    3\n"
14752             "// multi-line line comment\n"
14753             "//\n"
14754             "#define bbbb 4\n"
14755             "#define ccc  (5)",
14756             format("#define a  3\n"
14757                    "// multi-line line comment\n"
14758                    "//\n"
14759                    "#define bbbb 4\n"
14760                    "#define ccc (5)",
14761                    Style));
14762 
14763   EXPECT_EQ("#define a 3\n"
14764             "// empty lines still break.\n"
14765             "\n"
14766             "#define bbbb 4\n"
14767             "#define ccc  (5)",
14768             format("#define a     3\n"
14769                    "// empty lines still break.\n"
14770                    "\n"
14771                    "#define bbbb     4\n"
14772                    "#define ccc  (5)",
14773                    Style));
14774 
14775   // Test across empty lines
14776   Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossEmptyLines;
14777   EXPECT_EQ("#define a    3\n"
14778             "\n"
14779             "#define bbbb 4\n"
14780             "#define ccc  (5)",
14781             format("#define a 3\n"
14782                    "\n"
14783                    "#define bbbb 4\n"
14784                    "#define ccc (5)",
14785                    Style));
14786 
14787   EXPECT_EQ("#define a    3\n"
14788             "\n"
14789             "\n"
14790             "\n"
14791             "#define bbbb 4\n"
14792             "#define ccc  (5)",
14793             format("#define a        3\n"
14794                    "\n"
14795                    "\n"
14796                    "\n"
14797                    "#define bbbb 4\n"
14798                    "#define ccc (5)",
14799                    Style));
14800 
14801   EXPECT_EQ("#define a 3\n"
14802             "// comments should break alignment\n"
14803             "//\n"
14804             "#define bbbb 4\n"
14805             "#define ccc  (5)",
14806             format("#define a        3\n"
14807                    "// comments should break alignment\n"
14808                    "//\n"
14809                    "#define bbbb 4\n"
14810                    "#define ccc (5)",
14811                    Style));
14812 
14813   // Test across empty lines and comments
14814   Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossEmptyLinesAndComments;
14815   verifyFormat("#define a    3\n"
14816                "\n"
14817                "// line comment\n"
14818                "#define bbbb 4\n"
14819                "#define ccc  (5)",
14820                Style);
14821 
14822   EXPECT_EQ("#define a    3\n"
14823             "\n"
14824             "\n"
14825             "/* multi-line *\n"
14826             " * block comment */\n"
14827             "\n"
14828             "\n"
14829             "#define bbbb 4\n"
14830             "#define ccc  (5)",
14831             format("#define a 3\n"
14832                    "\n"
14833                    "\n"
14834                    "/* multi-line *\n"
14835                    " * block comment */\n"
14836                    "\n"
14837                    "\n"
14838                    "#define bbbb 4\n"
14839                    "#define ccc (5)",
14840                    Style));
14841 
14842   EXPECT_EQ("#define a    3\n"
14843             "\n"
14844             "\n"
14845             "/* multi-line *\n"
14846             " * block comment */\n"
14847             "\n"
14848             "\n"
14849             "#define bbbb 4\n"
14850             "#define ccc  (5)",
14851             format("#define a 3\n"
14852                    "\n"
14853                    "\n"
14854                    "/* multi-line *\n"
14855                    " * block comment */\n"
14856                    "\n"
14857                    "\n"
14858                    "#define bbbb 4\n"
14859                    "#define ccc       (5)",
14860                    Style));
14861 }
14862 
14863 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLines) {
14864   FormatStyle Alignment = getLLVMStyle();
14865   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
14866   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_AcrossEmptyLines;
14867 
14868   Alignment.MaxEmptyLinesToKeep = 10;
14869   /* Test alignment across empty lines */
14870   EXPECT_EQ("int a           = 5;\n"
14871             "\n"
14872             "int oneTwoThree = 123;",
14873             format("int a       = 5;\n"
14874                    "\n"
14875                    "int oneTwoThree= 123;",
14876                    Alignment));
14877   EXPECT_EQ("int a           = 5;\n"
14878             "int one         = 1;\n"
14879             "\n"
14880             "int oneTwoThree = 123;",
14881             format("int a = 5;\n"
14882                    "int one = 1;\n"
14883                    "\n"
14884                    "int oneTwoThree = 123;",
14885                    Alignment));
14886   EXPECT_EQ("int a           = 5;\n"
14887             "int one         = 1;\n"
14888             "\n"
14889             "int oneTwoThree = 123;\n"
14890             "int oneTwo      = 12;",
14891             format("int a = 5;\n"
14892                    "int one = 1;\n"
14893                    "\n"
14894                    "int oneTwoThree = 123;\n"
14895                    "int oneTwo = 12;",
14896                    Alignment));
14897 
14898   /* Test across comments */
14899   EXPECT_EQ("int a = 5;\n"
14900             "/* block comment */\n"
14901             "int oneTwoThree = 123;",
14902             format("int a = 5;\n"
14903                    "/* block comment */\n"
14904                    "int oneTwoThree=123;",
14905                    Alignment));
14906 
14907   EXPECT_EQ("int a = 5;\n"
14908             "// line comment\n"
14909             "int oneTwoThree = 123;",
14910             format("int a = 5;\n"
14911                    "// line comment\n"
14912                    "int oneTwoThree=123;",
14913                    Alignment));
14914 
14915   /* Test across comments and newlines */
14916   EXPECT_EQ("int a = 5;\n"
14917             "\n"
14918             "/* block comment */\n"
14919             "int oneTwoThree = 123;",
14920             format("int a = 5;\n"
14921                    "\n"
14922                    "/* block comment */\n"
14923                    "int oneTwoThree=123;",
14924                    Alignment));
14925 
14926   EXPECT_EQ("int a = 5;\n"
14927             "\n"
14928             "// line comment\n"
14929             "int oneTwoThree = 123;",
14930             format("int a = 5;\n"
14931                    "\n"
14932                    "// line comment\n"
14933                    "int oneTwoThree=123;",
14934                    Alignment));
14935 }
14936 
14937 TEST_F(FormatTest, AlignConsecutiveDeclarationsAcrossEmptyLinesAndComments) {
14938   FormatStyle Alignment = getLLVMStyle();
14939   Alignment.AlignConsecutiveDeclarations =
14940       FormatStyle::ACS_AcrossEmptyLinesAndComments;
14941   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
14942 
14943   Alignment.MaxEmptyLinesToKeep = 10;
14944   /* Test alignment across empty lines */
14945   EXPECT_EQ("int         a = 5;\n"
14946             "\n"
14947             "float const oneTwoThree = 123;",
14948             format("int a = 5;\n"
14949                    "\n"
14950                    "float const oneTwoThree = 123;",
14951                    Alignment));
14952   EXPECT_EQ("int         a = 5;\n"
14953             "float const one = 1;\n"
14954             "\n"
14955             "int         oneTwoThree = 123;",
14956             format("int a = 5;\n"
14957                    "float const one = 1;\n"
14958                    "\n"
14959                    "int oneTwoThree = 123;",
14960                    Alignment));
14961 
14962   /* Test across comments */
14963   EXPECT_EQ("float const a = 5;\n"
14964             "/* block comment */\n"
14965             "int         oneTwoThree = 123;",
14966             format("float const a = 5;\n"
14967                    "/* block comment */\n"
14968                    "int oneTwoThree=123;",
14969                    Alignment));
14970 
14971   EXPECT_EQ("float const a = 5;\n"
14972             "// line comment\n"
14973             "int         oneTwoThree = 123;",
14974             format("float const a = 5;\n"
14975                    "// line comment\n"
14976                    "int oneTwoThree=123;",
14977                    Alignment));
14978 
14979   /* Test across comments and newlines */
14980   EXPECT_EQ("float const a = 5;\n"
14981             "\n"
14982             "/* block comment */\n"
14983             "int         oneTwoThree = 123;",
14984             format("float const a = 5;\n"
14985                    "\n"
14986                    "/* block comment */\n"
14987                    "int         oneTwoThree=123;",
14988                    Alignment));
14989 
14990   EXPECT_EQ("float const a = 5;\n"
14991             "\n"
14992             "// line comment\n"
14993             "int         oneTwoThree = 123;",
14994             format("float const a = 5;\n"
14995                    "\n"
14996                    "// line comment\n"
14997                    "int oneTwoThree=123;",
14998                    Alignment));
14999 }
15000 
15001 TEST_F(FormatTest, AlignConsecutiveBitFieldsAcrossEmptyLinesAndComments) {
15002   FormatStyle Alignment = getLLVMStyle();
15003   Alignment.AlignConsecutiveBitFields =
15004       FormatStyle::ACS_AcrossEmptyLinesAndComments;
15005 
15006   Alignment.MaxEmptyLinesToKeep = 10;
15007   /* Test alignment across empty lines */
15008   EXPECT_EQ("int a            : 5;\n"
15009             "\n"
15010             "int longbitfield : 6;",
15011             format("int a : 5;\n"
15012                    "\n"
15013                    "int longbitfield : 6;",
15014                    Alignment));
15015   EXPECT_EQ("int a            : 5;\n"
15016             "int one          : 1;\n"
15017             "\n"
15018             "int longbitfield : 6;",
15019             format("int a : 5;\n"
15020                    "int one : 1;\n"
15021                    "\n"
15022                    "int longbitfield : 6;",
15023                    Alignment));
15024 
15025   /* Test across comments */
15026   EXPECT_EQ("int a            : 5;\n"
15027             "/* block comment */\n"
15028             "int longbitfield : 6;",
15029             format("int a : 5;\n"
15030                    "/* block comment */\n"
15031                    "int longbitfield : 6;",
15032                    Alignment));
15033   EXPECT_EQ("int a            : 5;\n"
15034             "int one          : 1;\n"
15035             "// line comment\n"
15036             "int longbitfield : 6;",
15037             format("int a : 5;\n"
15038                    "int one : 1;\n"
15039                    "// line comment\n"
15040                    "int longbitfield : 6;",
15041                    Alignment));
15042 
15043   /* Test across comments and newlines */
15044   EXPECT_EQ("int a            : 5;\n"
15045             "/* block comment */\n"
15046             "\n"
15047             "int longbitfield : 6;",
15048             format("int a : 5;\n"
15049                    "/* block comment */\n"
15050                    "\n"
15051                    "int longbitfield : 6;",
15052                    Alignment));
15053   EXPECT_EQ("int a            : 5;\n"
15054             "int one          : 1;\n"
15055             "\n"
15056             "// line comment\n"
15057             "\n"
15058             "int longbitfield : 6;",
15059             format("int a : 5;\n"
15060                    "int one : 1;\n"
15061                    "\n"
15062                    "// line comment \n"
15063                    "\n"
15064                    "int longbitfield : 6;",
15065                    Alignment));
15066 }
15067 
15068 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossComments) {
15069   FormatStyle Alignment = getLLVMStyle();
15070   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
15071   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_AcrossComments;
15072 
15073   Alignment.MaxEmptyLinesToKeep = 10;
15074   /* Test alignment across empty lines */
15075   EXPECT_EQ("int a = 5;\n"
15076             "\n"
15077             "int oneTwoThree = 123;",
15078             format("int a       = 5;\n"
15079                    "\n"
15080                    "int oneTwoThree= 123;",
15081                    Alignment));
15082   EXPECT_EQ("int a   = 5;\n"
15083             "int one = 1;\n"
15084             "\n"
15085             "int oneTwoThree = 123;",
15086             format("int a = 5;\n"
15087                    "int one = 1;\n"
15088                    "\n"
15089                    "int oneTwoThree = 123;",
15090                    Alignment));
15091 
15092   /* Test across comments */
15093   EXPECT_EQ("int a           = 5;\n"
15094             "/* block comment */\n"
15095             "int oneTwoThree = 123;",
15096             format("int a = 5;\n"
15097                    "/* block comment */\n"
15098                    "int oneTwoThree=123;",
15099                    Alignment));
15100 
15101   EXPECT_EQ("int a           = 5;\n"
15102             "// line comment\n"
15103             "int oneTwoThree = 123;",
15104             format("int a = 5;\n"
15105                    "// line comment\n"
15106                    "int oneTwoThree=123;",
15107                    Alignment));
15108 
15109   EXPECT_EQ("int a           = 5;\n"
15110             "/*\n"
15111             " * multi-line block comment\n"
15112             " */\n"
15113             "int oneTwoThree = 123;",
15114             format("int a = 5;\n"
15115                    "/*\n"
15116                    " * multi-line block comment\n"
15117                    " */\n"
15118                    "int oneTwoThree=123;",
15119                    Alignment));
15120 
15121   EXPECT_EQ("int a           = 5;\n"
15122             "//\n"
15123             "// multi-line line comment\n"
15124             "//\n"
15125             "int oneTwoThree = 123;",
15126             format("int a = 5;\n"
15127                    "//\n"
15128                    "// multi-line line comment\n"
15129                    "//\n"
15130                    "int oneTwoThree=123;",
15131                    Alignment));
15132 
15133   /* Test across comments and newlines */
15134   EXPECT_EQ("int a = 5;\n"
15135             "\n"
15136             "/* block comment */\n"
15137             "int oneTwoThree = 123;",
15138             format("int a = 5;\n"
15139                    "\n"
15140                    "/* block comment */\n"
15141                    "int oneTwoThree=123;",
15142                    Alignment));
15143 
15144   EXPECT_EQ("int a = 5;\n"
15145             "\n"
15146             "// line comment\n"
15147             "int oneTwoThree = 123;",
15148             format("int a = 5;\n"
15149                    "\n"
15150                    "// line comment\n"
15151                    "int oneTwoThree=123;",
15152                    Alignment));
15153 }
15154 
15155 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLinesAndComments) {
15156   FormatStyle Alignment = getLLVMStyle();
15157   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
15158   Alignment.AlignConsecutiveAssignments =
15159       FormatStyle::ACS_AcrossEmptyLinesAndComments;
15160   verifyFormat("int a           = 5;\n"
15161                "int oneTwoThree = 123;",
15162                Alignment);
15163   verifyFormat("int a           = method();\n"
15164                "int oneTwoThree = 133;",
15165                Alignment);
15166   verifyFormat("a &= 5;\n"
15167                "bcd *= 5;\n"
15168                "ghtyf += 5;\n"
15169                "dvfvdb -= 5;\n"
15170                "a /= 5;\n"
15171                "vdsvsv %= 5;\n"
15172                "sfdbddfbdfbb ^= 5;\n"
15173                "dvsdsv |= 5;\n"
15174                "int dsvvdvsdvvv = 123;",
15175                Alignment);
15176   verifyFormat("int i = 1, j = 10;\n"
15177                "something = 2000;",
15178                Alignment);
15179   verifyFormat("something = 2000;\n"
15180                "int i = 1, j = 10;\n",
15181                Alignment);
15182   verifyFormat("something = 2000;\n"
15183                "another   = 911;\n"
15184                "int i = 1, j = 10;\n"
15185                "oneMore = 1;\n"
15186                "i       = 2;",
15187                Alignment);
15188   verifyFormat("int a   = 5;\n"
15189                "int one = 1;\n"
15190                "method();\n"
15191                "int oneTwoThree = 123;\n"
15192                "int oneTwo      = 12;",
15193                Alignment);
15194   verifyFormat("int oneTwoThree = 123;\n"
15195                "int oneTwo      = 12;\n"
15196                "method();\n",
15197                Alignment);
15198   verifyFormat("int oneTwoThree = 123; // comment\n"
15199                "int oneTwo      = 12;  // comment",
15200                Alignment);
15201 
15202   // Bug 25167
15203   /* Uncomment when fixed
15204     verifyFormat("#if A\n"
15205                  "#else\n"
15206                  "int aaaaaaaa = 12;\n"
15207                  "#endif\n"
15208                  "#if B\n"
15209                  "#else\n"
15210                  "int a = 12;\n"
15211                  "#endif\n",
15212                  Alignment);
15213     verifyFormat("enum foo {\n"
15214                  "#if A\n"
15215                  "#else\n"
15216                  "  aaaaaaaa = 12;\n"
15217                  "#endif\n"
15218                  "#if B\n"
15219                  "#else\n"
15220                  "  a = 12;\n"
15221                  "#endif\n"
15222                  "};\n",
15223                  Alignment);
15224   */
15225 
15226   Alignment.MaxEmptyLinesToKeep = 10;
15227   /* Test alignment across empty lines */
15228   EXPECT_EQ("int a           = 5;\n"
15229             "\n"
15230             "int oneTwoThree = 123;",
15231             format("int a       = 5;\n"
15232                    "\n"
15233                    "int oneTwoThree= 123;",
15234                    Alignment));
15235   EXPECT_EQ("int a           = 5;\n"
15236             "int one         = 1;\n"
15237             "\n"
15238             "int oneTwoThree = 123;",
15239             format("int a = 5;\n"
15240                    "int one = 1;\n"
15241                    "\n"
15242                    "int oneTwoThree = 123;",
15243                    Alignment));
15244   EXPECT_EQ("int a           = 5;\n"
15245             "int one         = 1;\n"
15246             "\n"
15247             "int oneTwoThree = 123;\n"
15248             "int oneTwo      = 12;",
15249             format("int a = 5;\n"
15250                    "int one = 1;\n"
15251                    "\n"
15252                    "int oneTwoThree = 123;\n"
15253                    "int oneTwo = 12;",
15254                    Alignment));
15255 
15256   /* Test across comments */
15257   EXPECT_EQ("int a           = 5;\n"
15258             "/* block comment */\n"
15259             "int oneTwoThree = 123;",
15260             format("int a = 5;\n"
15261                    "/* block comment */\n"
15262                    "int oneTwoThree=123;",
15263                    Alignment));
15264 
15265   EXPECT_EQ("int a           = 5;\n"
15266             "// line comment\n"
15267             "int oneTwoThree = 123;",
15268             format("int a = 5;\n"
15269                    "// line comment\n"
15270                    "int oneTwoThree=123;",
15271                    Alignment));
15272 
15273   /* Test across comments and newlines */
15274   EXPECT_EQ("int a           = 5;\n"
15275             "\n"
15276             "/* block comment */\n"
15277             "int oneTwoThree = 123;",
15278             format("int a = 5;\n"
15279                    "\n"
15280                    "/* block comment */\n"
15281                    "int oneTwoThree=123;",
15282                    Alignment));
15283 
15284   EXPECT_EQ("int a           = 5;\n"
15285             "\n"
15286             "// line comment\n"
15287             "int oneTwoThree = 123;",
15288             format("int a = 5;\n"
15289                    "\n"
15290                    "// line comment\n"
15291                    "int oneTwoThree=123;",
15292                    Alignment));
15293 
15294   EXPECT_EQ("int a           = 5;\n"
15295             "//\n"
15296             "// multi-line line comment\n"
15297             "//\n"
15298             "int oneTwoThree = 123;",
15299             format("int a = 5;\n"
15300                    "//\n"
15301                    "// multi-line line comment\n"
15302                    "//\n"
15303                    "int oneTwoThree=123;",
15304                    Alignment));
15305 
15306   EXPECT_EQ("int a           = 5;\n"
15307             "/*\n"
15308             " *  multi-line block comment\n"
15309             " */\n"
15310             "int oneTwoThree = 123;",
15311             format("int a = 5;\n"
15312                    "/*\n"
15313                    " *  multi-line block comment\n"
15314                    " */\n"
15315                    "int oneTwoThree=123;",
15316                    Alignment));
15317 
15318   EXPECT_EQ("int a           = 5;\n"
15319             "\n"
15320             "/* block comment */\n"
15321             "\n"
15322             "\n"
15323             "\n"
15324             "int oneTwoThree = 123;",
15325             format("int a = 5;\n"
15326                    "\n"
15327                    "/* block comment */\n"
15328                    "\n"
15329                    "\n"
15330                    "\n"
15331                    "int oneTwoThree=123;",
15332                    Alignment));
15333 
15334   EXPECT_EQ("int a           = 5;\n"
15335             "\n"
15336             "// line comment\n"
15337             "\n"
15338             "\n"
15339             "\n"
15340             "int oneTwoThree = 123;",
15341             format("int a = 5;\n"
15342                    "\n"
15343                    "// line comment\n"
15344                    "\n"
15345                    "\n"
15346                    "\n"
15347                    "int oneTwoThree=123;",
15348                    Alignment));
15349 
15350   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
15351   verifyFormat("#define A \\\n"
15352                "  int aaaa       = 12; \\\n"
15353                "  int b          = 23; \\\n"
15354                "  int ccc        = 234; \\\n"
15355                "  int dddddddddd = 2345;",
15356                Alignment);
15357   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
15358   verifyFormat("#define A               \\\n"
15359                "  int aaaa       = 12;  \\\n"
15360                "  int b          = 23;  \\\n"
15361                "  int ccc        = 234; \\\n"
15362                "  int dddddddddd = 2345;",
15363                Alignment);
15364   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
15365   verifyFormat("#define A                                                      "
15366                "                \\\n"
15367                "  int aaaa       = 12;                                         "
15368                "                \\\n"
15369                "  int b          = 23;                                         "
15370                "                \\\n"
15371                "  int ccc        = 234;                                        "
15372                "                \\\n"
15373                "  int dddddddddd = 2345;",
15374                Alignment);
15375   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
15376                "k = 4, int l = 5,\n"
15377                "                  int m = 6) {\n"
15378                "  int j      = 10;\n"
15379                "  otherThing = 1;\n"
15380                "}",
15381                Alignment);
15382   verifyFormat("void SomeFunction(int parameter = 0) {\n"
15383                "  int i   = 1;\n"
15384                "  int j   = 2;\n"
15385                "  int big = 10000;\n"
15386                "}",
15387                Alignment);
15388   verifyFormat("class C {\n"
15389                "public:\n"
15390                "  int i            = 1;\n"
15391                "  virtual void f() = 0;\n"
15392                "};",
15393                Alignment);
15394   verifyFormat("int i = 1;\n"
15395                "if (SomeType t = getSomething()) {\n"
15396                "}\n"
15397                "int j   = 2;\n"
15398                "int big = 10000;",
15399                Alignment);
15400   verifyFormat("int j = 7;\n"
15401                "for (int k = 0; k < N; ++k) {\n"
15402                "}\n"
15403                "int j   = 2;\n"
15404                "int big = 10000;\n"
15405                "}",
15406                Alignment);
15407   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
15408   verifyFormat("int i = 1;\n"
15409                "LooooooooooongType loooooooooooooooooooooongVariable\n"
15410                "    = someLooooooooooooooooongFunction();\n"
15411                "int j = 2;",
15412                Alignment);
15413   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
15414   verifyFormat("int i = 1;\n"
15415                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
15416                "    someLooooooooooooooooongFunction();\n"
15417                "int j = 2;",
15418                Alignment);
15419 
15420   verifyFormat("auto lambda = []() {\n"
15421                "  auto i = 0;\n"
15422                "  return 0;\n"
15423                "};\n"
15424                "int i  = 0;\n"
15425                "auto v = type{\n"
15426                "    i = 1,   //\n"
15427                "    (i = 2), //\n"
15428                "    i = 3    //\n"
15429                "};",
15430                Alignment);
15431 
15432   verifyFormat(
15433       "int i      = 1;\n"
15434       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
15435       "                          loooooooooooooooooooooongParameterB);\n"
15436       "int j      = 2;",
15437       Alignment);
15438 
15439   verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
15440                "          typename B   = very_long_type_name_1,\n"
15441                "          typename T_2 = very_long_type_name_2>\n"
15442                "auto foo() {}\n",
15443                Alignment);
15444   verifyFormat("int a, b = 1;\n"
15445                "int c  = 2;\n"
15446                "int dd = 3;\n",
15447                Alignment);
15448   verifyFormat("int aa       = ((1 > 2) ? 3 : 4);\n"
15449                "float b[1][] = {{3.f}};\n",
15450                Alignment);
15451   verifyFormat("for (int i = 0; i < 1; i++)\n"
15452                "  int x = 1;\n",
15453                Alignment);
15454   verifyFormat("for (i = 0; i < 1; i++)\n"
15455                "  x = 1;\n"
15456                "y = 1;\n",
15457                Alignment);
15458 
15459   Alignment.ReflowComments = true;
15460   Alignment.ColumnLimit = 50;
15461   EXPECT_EQ("int x   = 0;\n"
15462             "int yy  = 1; /// specificlennospace\n"
15463             "int zzz = 2;\n",
15464             format("int x   = 0;\n"
15465                    "int yy  = 1; ///specificlennospace\n"
15466                    "int zzz = 2;\n",
15467                    Alignment));
15468 }
15469 
15470 TEST_F(FormatTest, AlignConsecutiveAssignments) {
15471   FormatStyle Alignment = getLLVMStyle();
15472   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
15473   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
15474   verifyFormat("int a = 5;\n"
15475                "int oneTwoThree = 123;",
15476                Alignment);
15477   verifyFormat("int a = 5;\n"
15478                "int oneTwoThree = 123;",
15479                Alignment);
15480 
15481   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
15482   verifyFormat("int a           = 5;\n"
15483                "int oneTwoThree = 123;",
15484                Alignment);
15485   verifyFormat("int a           = method();\n"
15486                "int oneTwoThree = 133;",
15487                Alignment);
15488   verifyFormat("a &= 5;\n"
15489                "bcd *= 5;\n"
15490                "ghtyf += 5;\n"
15491                "dvfvdb -= 5;\n"
15492                "a /= 5;\n"
15493                "vdsvsv %= 5;\n"
15494                "sfdbddfbdfbb ^= 5;\n"
15495                "dvsdsv |= 5;\n"
15496                "int dsvvdvsdvvv = 123;",
15497                Alignment);
15498   verifyFormat("int i = 1, j = 10;\n"
15499                "something = 2000;",
15500                Alignment);
15501   verifyFormat("something = 2000;\n"
15502                "int i = 1, j = 10;\n",
15503                Alignment);
15504   verifyFormat("something = 2000;\n"
15505                "another   = 911;\n"
15506                "int i = 1, j = 10;\n"
15507                "oneMore = 1;\n"
15508                "i       = 2;",
15509                Alignment);
15510   verifyFormat("int a   = 5;\n"
15511                "int one = 1;\n"
15512                "method();\n"
15513                "int oneTwoThree = 123;\n"
15514                "int oneTwo      = 12;",
15515                Alignment);
15516   verifyFormat("int oneTwoThree = 123;\n"
15517                "int oneTwo      = 12;\n"
15518                "method();\n",
15519                Alignment);
15520   verifyFormat("int oneTwoThree = 123; // comment\n"
15521                "int oneTwo      = 12;  // comment",
15522                Alignment);
15523 
15524   // Bug 25167
15525   /* Uncomment when fixed
15526     verifyFormat("#if A\n"
15527                  "#else\n"
15528                  "int aaaaaaaa = 12;\n"
15529                  "#endif\n"
15530                  "#if B\n"
15531                  "#else\n"
15532                  "int a = 12;\n"
15533                  "#endif\n",
15534                  Alignment);
15535     verifyFormat("enum foo {\n"
15536                  "#if A\n"
15537                  "#else\n"
15538                  "  aaaaaaaa = 12;\n"
15539                  "#endif\n"
15540                  "#if B\n"
15541                  "#else\n"
15542                  "  a = 12;\n"
15543                  "#endif\n"
15544                  "};\n",
15545                  Alignment);
15546   */
15547 
15548   EXPECT_EQ("int a = 5;\n"
15549             "\n"
15550             "int oneTwoThree = 123;",
15551             format("int a       = 5;\n"
15552                    "\n"
15553                    "int oneTwoThree= 123;",
15554                    Alignment));
15555   EXPECT_EQ("int a   = 5;\n"
15556             "int one = 1;\n"
15557             "\n"
15558             "int oneTwoThree = 123;",
15559             format("int a = 5;\n"
15560                    "int one = 1;\n"
15561                    "\n"
15562                    "int oneTwoThree = 123;",
15563                    Alignment));
15564   EXPECT_EQ("int a   = 5;\n"
15565             "int one = 1;\n"
15566             "\n"
15567             "int oneTwoThree = 123;\n"
15568             "int oneTwo      = 12;",
15569             format("int a = 5;\n"
15570                    "int one = 1;\n"
15571                    "\n"
15572                    "int oneTwoThree = 123;\n"
15573                    "int oneTwo = 12;",
15574                    Alignment));
15575   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
15576   verifyFormat("#define A \\\n"
15577                "  int aaaa       = 12; \\\n"
15578                "  int b          = 23; \\\n"
15579                "  int ccc        = 234; \\\n"
15580                "  int dddddddddd = 2345;",
15581                Alignment);
15582   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
15583   verifyFormat("#define A               \\\n"
15584                "  int aaaa       = 12;  \\\n"
15585                "  int b          = 23;  \\\n"
15586                "  int ccc        = 234; \\\n"
15587                "  int dddddddddd = 2345;",
15588                Alignment);
15589   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
15590   verifyFormat("#define A                                                      "
15591                "                \\\n"
15592                "  int aaaa       = 12;                                         "
15593                "                \\\n"
15594                "  int b          = 23;                                         "
15595                "                \\\n"
15596                "  int ccc        = 234;                                        "
15597                "                \\\n"
15598                "  int dddddddddd = 2345;",
15599                Alignment);
15600   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
15601                "k = 4, int l = 5,\n"
15602                "                  int m = 6) {\n"
15603                "  int j      = 10;\n"
15604                "  otherThing = 1;\n"
15605                "}",
15606                Alignment);
15607   verifyFormat("void SomeFunction(int parameter = 0) {\n"
15608                "  int i   = 1;\n"
15609                "  int j   = 2;\n"
15610                "  int big = 10000;\n"
15611                "}",
15612                Alignment);
15613   verifyFormat("class C {\n"
15614                "public:\n"
15615                "  int i            = 1;\n"
15616                "  virtual void f() = 0;\n"
15617                "};",
15618                Alignment);
15619   verifyFormat("int i = 1;\n"
15620                "if (SomeType t = getSomething()) {\n"
15621                "}\n"
15622                "int j   = 2;\n"
15623                "int big = 10000;",
15624                Alignment);
15625   verifyFormat("int j = 7;\n"
15626                "for (int k = 0; k < N; ++k) {\n"
15627                "}\n"
15628                "int j   = 2;\n"
15629                "int big = 10000;\n"
15630                "}",
15631                Alignment);
15632   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
15633   verifyFormat("int i = 1;\n"
15634                "LooooooooooongType loooooooooooooooooooooongVariable\n"
15635                "    = someLooooooooooooooooongFunction();\n"
15636                "int j = 2;",
15637                Alignment);
15638   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
15639   verifyFormat("int i = 1;\n"
15640                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
15641                "    someLooooooooooooooooongFunction();\n"
15642                "int j = 2;",
15643                Alignment);
15644 
15645   verifyFormat("auto lambda = []() {\n"
15646                "  auto i = 0;\n"
15647                "  return 0;\n"
15648                "};\n"
15649                "int i  = 0;\n"
15650                "auto v = type{\n"
15651                "    i = 1,   //\n"
15652                "    (i = 2), //\n"
15653                "    i = 3    //\n"
15654                "};",
15655                Alignment);
15656 
15657   verifyFormat(
15658       "int i      = 1;\n"
15659       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
15660       "                          loooooooooooooooooooooongParameterB);\n"
15661       "int j      = 2;",
15662       Alignment);
15663 
15664   verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
15665                "          typename B   = very_long_type_name_1,\n"
15666                "          typename T_2 = very_long_type_name_2>\n"
15667                "auto foo() {}\n",
15668                Alignment);
15669   verifyFormat("int a, b = 1;\n"
15670                "int c  = 2;\n"
15671                "int dd = 3;\n",
15672                Alignment);
15673   verifyFormat("int aa       = ((1 > 2) ? 3 : 4);\n"
15674                "float b[1][] = {{3.f}};\n",
15675                Alignment);
15676   verifyFormat("for (int i = 0; i < 1; i++)\n"
15677                "  int x = 1;\n",
15678                Alignment);
15679   verifyFormat("for (i = 0; i < 1; i++)\n"
15680                "  x = 1;\n"
15681                "y = 1;\n",
15682                Alignment);
15683 
15684   Alignment.ReflowComments = true;
15685   Alignment.ColumnLimit = 50;
15686   EXPECT_EQ("int x   = 0;\n"
15687             "int yy  = 1; /// specificlennospace\n"
15688             "int zzz = 2;\n",
15689             format("int x   = 0;\n"
15690                    "int yy  = 1; ///specificlennospace\n"
15691                    "int zzz = 2;\n",
15692                    Alignment));
15693 }
15694 
15695 TEST_F(FormatTest, AlignConsecutiveBitFields) {
15696   FormatStyle Alignment = getLLVMStyle();
15697   Alignment.AlignConsecutiveBitFields = FormatStyle::ACS_Consecutive;
15698   verifyFormat("int const a     : 5;\n"
15699                "int oneTwoThree : 23;",
15700                Alignment);
15701 
15702   // Initializers are allowed starting with c++2a
15703   verifyFormat("int const a     : 5 = 1;\n"
15704                "int oneTwoThree : 23 = 0;",
15705                Alignment);
15706 
15707   Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
15708   verifyFormat("int const a           : 5;\n"
15709                "int       oneTwoThree : 23;",
15710                Alignment);
15711 
15712   verifyFormat("int const a           : 5;  // comment\n"
15713                "int       oneTwoThree : 23; // comment",
15714                Alignment);
15715 
15716   verifyFormat("int const a           : 5 = 1;\n"
15717                "int       oneTwoThree : 23 = 0;",
15718                Alignment);
15719 
15720   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
15721   verifyFormat("int const a           : 5  = 1;\n"
15722                "int       oneTwoThree : 23 = 0;",
15723                Alignment);
15724   verifyFormat("int const a           : 5  = {1};\n"
15725                "int       oneTwoThree : 23 = 0;",
15726                Alignment);
15727 
15728   Alignment.BitFieldColonSpacing = FormatStyle::BFCS_None;
15729   verifyFormat("int const a          :5;\n"
15730                "int       oneTwoThree:23;",
15731                Alignment);
15732 
15733   Alignment.BitFieldColonSpacing = FormatStyle::BFCS_Before;
15734   verifyFormat("int const a           :5;\n"
15735                "int       oneTwoThree :23;",
15736                Alignment);
15737 
15738   Alignment.BitFieldColonSpacing = FormatStyle::BFCS_After;
15739   verifyFormat("int const a          : 5;\n"
15740                "int       oneTwoThree: 23;",
15741                Alignment);
15742 
15743   // Known limitations: ':' is only recognized as a bitfield colon when
15744   // followed by a number.
15745   /*
15746   verifyFormat("int oneTwoThree : SOME_CONSTANT;\n"
15747                "int a           : 5;",
15748                Alignment);
15749   */
15750 }
15751 
15752 TEST_F(FormatTest, AlignConsecutiveDeclarations) {
15753   FormatStyle Alignment = getLLVMStyle();
15754   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
15755   Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_None;
15756   Alignment.PointerAlignment = FormatStyle::PAS_Right;
15757   verifyFormat("float const a = 5;\n"
15758                "int oneTwoThree = 123;",
15759                Alignment);
15760   verifyFormat("int a = 5;\n"
15761                "float const oneTwoThree = 123;",
15762                Alignment);
15763 
15764   Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
15765   verifyFormat("float const a = 5;\n"
15766                "int         oneTwoThree = 123;",
15767                Alignment);
15768   verifyFormat("int         a = method();\n"
15769                "float const oneTwoThree = 133;",
15770                Alignment);
15771   verifyFormat("int i = 1, j = 10;\n"
15772                "something = 2000;",
15773                Alignment);
15774   verifyFormat("something = 2000;\n"
15775                "int i = 1, j = 10;\n",
15776                Alignment);
15777   verifyFormat("float      something = 2000;\n"
15778                "double     another = 911;\n"
15779                "int        i = 1, j = 10;\n"
15780                "const int *oneMore = 1;\n"
15781                "unsigned   i = 2;",
15782                Alignment);
15783   verifyFormat("float a = 5;\n"
15784                "int   one = 1;\n"
15785                "method();\n"
15786                "const double       oneTwoThree = 123;\n"
15787                "const unsigned int oneTwo = 12;",
15788                Alignment);
15789   verifyFormat("int      oneTwoThree{0}; // comment\n"
15790                "unsigned oneTwo;         // comment",
15791                Alignment);
15792   verifyFormat("unsigned int       *a;\n"
15793                "int                *b;\n"
15794                "unsigned int Const *c;\n"
15795                "unsigned int const *d;\n"
15796                "unsigned int Const &e;\n"
15797                "unsigned int const &f;",
15798                Alignment);
15799   verifyFormat("Const unsigned int *c;\n"
15800                "const unsigned int *d;\n"
15801                "Const unsigned int &e;\n"
15802                "const unsigned int &f;\n"
15803                "const unsigned      g;\n"
15804                "Const unsigned      h;",
15805                Alignment);
15806   EXPECT_EQ("float const a = 5;\n"
15807             "\n"
15808             "int oneTwoThree = 123;",
15809             format("float const   a = 5;\n"
15810                    "\n"
15811                    "int           oneTwoThree= 123;",
15812                    Alignment));
15813   EXPECT_EQ("float a = 5;\n"
15814             "int   one = 1;\n"
15815             "\n"
15816             "unsigned oneTwoThree = 123;",
15817             format("float    a = 5;\n"
15818                    "int      one = 1;\n"
15819                    "\n"
15820                    "unsigned oneTwoThree = 123;",
15821                    Alignment));
15822   EXPECT_EQ("float a = 5;\n"
15823             "int   one = 1;\n"
15824             "\n"
15825             "unsigned oneTwoThree = 123;\n"
15826             "int      oneTwo = 12;",
15827             format("float    a = 5;\n"
15828                    "int one = 1;\n"
15829                    "\n"
15830                    "unsigned oneTwoThree = 123;\n"
15831                    "int oneTwo = 12;",
15832                    Alignment));
15833   // Function prototype alignment
15834   verifyFormat("int    a();\n"
15835                "double b();",
15836                Alignment);
15837   verifyFormat("int    a(int x);\n"
15838                "double b();",
15839                Alignment);
15840   unsigned OldColumnLimit = Alignment.ColumnLimit;
15841   // We need to set ColumnLimit to zero, in order to stress nested alignments,
15842   // otherwise the function parameters will be re-flowed onto a single line.
15843   Alignment.ColumnLimit = 0;
15844   EXPECT_EQ("int    a(int   x,\n"
15845             "         float y);\n"
15846             "double b(int    x,\n"
15847             "         double y);",
15848             format("int a(int x,\n"
15849                    " float y);\n"
15850                    "double b(int x,\n"
15851                    " double y);",
15852                    Alignment));
15853   // This ensures that function parameters of function declarations are
15854   // correctly indented when their owning functions are indented.
15855   // The failure case here is for 'double y' to not be indented enough.
15856   EXPECT_EQ("double a(int x);\n"
15857             "int    b(int    y,\n"
15858             "         double z);",
15859             format("double a(int x);\n"
15860                    "int b(int y,\n"
15861                    " double z);",
15862                    Alignment));
15863   // Set ColumnLimit low so that we induce wrapping immediately after
15864   // the function name and opening paren.
15865   Alignment.ColumnLimit = 13;
15866   verifyFormat("int function(\n"
15867                "    int  x,\n"
15868                "    bool y);",
15869                Alignment);
15870   Alignment.ColumnLimit = OldColumnLimit;
15871   // Ensure function pointers don't screw up recursive alignment
15872   verifyFormat("int    a(int x, void (*fp)(int y));\n"
15873                "double b();",
15874                Alignment);
15875   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
15876   // Ensure recursive alignment is broken by function braces, so that the
15877   // "a = 1" does not align with subsequent assignments inside the function
15878   // body.
15879   verifyFormat("int func(int a = 1) {\n"
15880                "  int b  = 2;\n"
15881                "  int cc = 3;\n"
15882                "}",
15883                Alignment);
15884   verifyFormat("float      something = 2000;\n"
15885                "double     another   = 911;\n"
15886                "int        i = 1, j = 10;\n"
15887                "const int *oneMore = 1;\n"
15888                "unsigned   i       = 2;",
15889                Alignment);
15890   verifyFormat("int      oneTwoThree = {0}; // comment\n"
15891                "unsigned oneTwo      = 0;   // comment",
15892                Alignment);
15893   // Make sure that scope is correctly tracked, in the absence of braces
15894   verifyFormat("for (int i = 0; i < n; i++)\n"
15895                "  j = i;\n"
15896                "double x = 1;\n",
15897                Alignment);
15898   verifyFormat("if (int i = 0)\n"
15899                "  j = i;\n"
15900                "double x = 1;\n",
15901                Alignment);
15902   // Ensure operator[] and operator() are comprehended
15903   verifyFormat("struct test {\n"
15904                "  long long int foo();\n"
15905                "  int           operator[](int a);\n"
15906                "  double        bar();\n"
15907                "};\n",
15908                Alignment);
15909   verifyFormat("struct test {\n"
15910                "  long long int foo();\n"
15911                "  int           operator()(int a);\n"
15912                "  double        bar();\n"
15913                "};\n",
15914                Alignment);
15915 
15916   // PAS_Right
15917   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
15918             "  int const i   = 1;\n"
15919             "  int      *j   = 2;\n"
15920             "  int       big = 10000;\n"
15921             "\n"
15922             "  unsigned oneTwoThree = 123;\n"
15923             "  int      oneTwo      = 12;\n"
15924             "  method();\n"
15925             "  float k  = 2;\n"
15926             "  int   ll = 10000;\n"
15927             "}",
15928             format("void SomeFunction(int parameter= 0) {\n"
15929                    " int const  i= 1;\n"
15930                    "  int *j=2;\n"
15931                    " int big  =  10000;\n"
15932                    "\n"
15933                    "unsigned oneTwoThree  =123;\n"
15934                    "int oneTwo = 12;\n"
15935                    "  method();\n"
15936                    "float k= 2;\n"
15937                    "int ll=10000;\n"
15938                    "}",
15939                    Alignment));
15940   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
15941             "  int const i   = 1;\n"
15942             "  int     **j   = 2, ***k;\n"
15943             "  int      &k   = i;\n"
15944             "  int     &&l   = i + j;\n"
15945             "  int       big = 10000;\n"
15946             "\n"
15947             "  unsigned oneTwoThree = 123;\n"
15948             "  int      oneTwo      = 12;\n"
15949             "  method();\n"
15950             "  float k  = 2;\n"
15951             "  int   ll = 10000;\n"
15952             "}",
15953             format("void SomeFunction(int parameter= 0) {\n"
15954                    " int const  i= 1;\n"
15955                    "  int **j=2,***k;\n"
15956                    "int &k=i;\n"
15957                    "int &&l=i+j;\n"
15958                    " int big  =  10000;\n"
15959                    "\n"
15960                    "unsigned oneTwoThree  =123;\n"
15961                    "int oneTwo = 12;\n"
15962                    "  method();\n"
15963                    "float k= 2;\n"
15964                    "int ll=10000;\n"
15965                    "}",
15966                    Alignment));
15967   // variables are aligned at their name, pointers are at the right most
15968   // position
15969   verifyFormat("int   *a;\n"
15970                "int  **b;\n"
15971                "int ***c;\n"
15972                "int    foobar;\n",
15973                Alignment);
15974 
15975   // PAS_Left
15976   FormatStyle AlignmentLeft = Alignment;
15977   AlignmentLeft.PointerAlignment = FormatStyle::PAS_Left;
15978   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
15979             "  int const i   = 1;\n"
15980             "  int*      j   = 2;\n"
15981             "  int       big = 10000;\n"
15982             "\n"
15983             "  unsigned oneTwoThree = 123;\n"
15984             "  int      oneTwo      = 12;\n"
15985             "  method();\n"
15986             "  float k  = 2;\n"
15987             "  int   ll = 10000;\n"
15988             "}",
15989             format("void SomeFunction(int parameter= 0) {\n"
15990                    " int const  i= 1;\n"
15991                    "  int *j=2;\n"
15992                    " int big  =  10000;\n"
15993                    "\n"
15994                    "unsigned oneTwoThree  =123;\n"
15995                    "int oneTwo = 12;\n"
15996                    "  method();\n"
15997                    "float k= 2;\n"
15998                    "int ll=10000;\n"
15999                    "}",
16000                    AlignmentLeft));
16001   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
16002             "  int const i   = 1;\n"
16003             "  int**     j   = 2;\n"
16004             "  int&      k   = i;\n"
16005             "  int&&     l   = i + j;\n"
16006             "  int       big = 10000;\n"
16007             "\n"
16008             "  unsigned oneTwoThree = 123;\n"
16009             "  int      oneTwo      = 12;\n"
16010             "  method();\n"
16011             "  float k  = 2;\n"
16012             "  int   ll = 10000;\n"
16013             "}",
16014             format("void SomeFunction(int parameter= 0) {\n"
16015                    " int const  i= 1;\n"
16016                    "  int **j=2;\n"
16017                    "int &k=i;\n"
16018                    "int &&l=i+j;\n"
16019                    " int big  =  10000;\n"
16020                    "\n"
16021                    "unsigned oneTwoThree  =123;\n"
16022                    "int oneTwo = 12;\n"
16023                    "  method();\n"
16024                    "float k= 2;\n"
16025                    "int ll=10000;\n"
16026                    "}",
16027                    AlignmentLeft));
16028   // variables are aligned at their name, pointers are at the left most position
16029   verifyFormat("int*   a;\n"
16030                "int**  b;\n"
16031                "int*** c;\n"
16032                "int    foobar;\n",
16033                AlignmentLeft);
16034 
16035   // PAS_Middle
16036   FormatStyle AlignmentMiddle = Alignment;
16037   AlignmentMiddle.PointerAlignment = FormatStyle::PAS_Middle;
16038   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
16039             "  int const i   = 1;\n"
16040             "  int *     j   = 2;\n"
16041             "  int       big = 10000;\n"
16042             "\n"
16043             "  unsigned oneTwoThree = 123;\n"
16044             "  int      oneTwo      = 12;\n"
16045             "  method();\n"
16046             "  float k  = 2;\n"
16047             "  int   ll = 10000;\n"
16048             "}",
16049             format("void SomeFunction(int parameter= 0) {\n"
16050                    " int const  i= 1;\n"
16051                    "  int *j=2;\n"
16052                    " int big  =  10000;\n"
16053                    "\n"
16054                    "unsigned oneTwoThree  =123;\n"
16055                    "int oneTwo = 12;\n"
16056                    "  method();\n"
16057                    "float k= 2;\n"
16058                    "int ll=10000;\n"
16059                    "}",
16060                    AlignmentMiddle));
16061   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
16062             "  int const i   = 1;\n"
16063             "  int **    j   = 2, ***k;\n"
16064             "  int &     k   = i;\n"
16065             "  int &&    l   = i + j;\n"
16066             "  int       big = 10000;\n"
16067             "\n"
16068             "  unsigned oneTwoThree = 123;\n"
16069             "  int      oneTwo      = 12;\n"
16070             "  method();\n"
16071             "  float k  = 2;\n"
16072             "  int   ll = 10000;\n"
16073             "}",
16074             format("void SomeFunction(int parameter= 0) {\n"
16075                    " int const  i= 1;\n"
16076                    "  int **j=2,***k;\n"
16077                    "int &k=i;\n"
16078                    "int &&l=i+j;\n"
16079                    " int big  =  10000;\n"
16080                    "\n"
16081                    "unsigned oneTwoThree  =123;\n"
16082                    "int oneTwo = 12;\n"
16083                    "  method();\n"
16084                    "float k= 2;\n"
16085                    "int ll=10000;\n"
16086                    "}",
16087                    AlignmentMiddle));
16088   // variables are aligned at their name, pointers are in the middle
16089   verifyFormat("int *   a;\n"
16090                "int *   b;\n"
16091                "int *** c;\n"
16092                "int     foobar;\n",
16093                AlignmentMiddle);
16094 
16095   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
16096   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
16097   verifyFormat("#define A \\\n"
16098                "  int       aaaa = 12; \\\n"
16099                "  float     b = 23; \\\n"
16100                "  const int ccc = 234; \\\n"
16101                "  unsigned  dddddddddd = 2345;",
16102                Alignment);
16103   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
16104   verifyFormat("#define A              \\\n"
16105                "  int       aaaa = 12; \\\n"
16106                "  float     b = 23;    \\\n"
16107                "  const int ccc = 234; \\\n"
16108                "  unsigned  dddddddddd = 2345;",
16109                Alignment);
16110   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
16111   Alignment.ColumnLimit = 30;
16112   verifyFormat("#define A                    \\\n"
16113                "  int       aaaa = 12;       \\\n"
16114                "  float     b = 23;          \\\n"
16115                "  const int ccc = 234;       \\\n"
16116                "  int       dddddddddd = 2345;",
16117                Alignment);
16118   Alignment.ColumnLimit = 80;
16119   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
16120                "k = 4, int l = 5,\n"
16121                "                  int m = 6) {\n"
16122                "  const int j = 10;\n"
16123                "  otherThing = 1;\n"
16124                "}",
16125                Alignment);
16126   verifyFormat("void SomeFunction(int parameter = 0) {\n"
16127                "  int const i = 1;\n"
16128                "  int      *j = 2;\n"
16129                "  int       big = 10000;\n"
16130                "}",
16131                Alignment);
16132   verifyFormat("class C {\n"
16133                "public:\n"
16134                "  int          i = 1;\n"
16135                "  virtual void f() = 0;\n"
16136                "};",
16137                Alignment);
16138   verifyFormat("float i = 1;\n"
16139                "if (SomeType t = getSomething()) {\n"
16140                "}\n"
16141                "const unsigned j = 2;\n"
16142                "int            big = 10000;",
16143                Alignment);
16144   verifyFormat("float j = 7;\n"
16145                "for (int k = 0; k < N; ++k) {\n"
16146                "}\n"
16147                "unsigned j = 2;\n"
16148                "int      big = 10000;\n"
16149                "}",
16150                Alignment);
16151   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
16152   verifyFormat("float              i = 1;\n"
16153                "LooooooooooongType loooooooooooooooooooooongVariable\n"
16154                "    = someLooooooooooooooooongFunction();\n"
16155                "int j = 2;",
16156                Alignment);
16157   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
16158   verifyFormat("int                i = 1;\n"
16159                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
16160                "    someLooooooooooooooooongFunction();\n"
16161                "int j = 2;",
16162                Alignment);
16163 
16164   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16165   verifyFormat("auto lambda = []() {\n"
16166                "  auto  ii = 0;\n"
16167                "  float j  = 0;\n"
16168                "  return 0;\n"
16169                "};\n"
16170                "int   i  = 0;\n"
16171                "float i2 = 0;\n"
16172                "auto  v  = type{\n"
16173                "    i = 1,   //\n"
16174                "    (i = 2), //\n"
16175                "    i = 3    //\n"
16176                "};",
16177                Alignment);
16178   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
16179 
16180   verifyFormat(
16181       "int      i = 1;\n"
16182       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
16183       "                          loooooooooooooooooooooongParameterB);\n"
16184       "int      j = 2;",
16185       Alignment);
16186 
16187   // Test interactions with ColumnLimit and AlignConsecutiveAssignments:
16188   // We expect declarations and assignments to align, as long as it doesn't
16189   // exceed the column limit, starting a new alignment sequence whenever it
16190   // happens.
16191   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16192   Alignment.ColumnLimit = 30;
16193   verifyFormat("float    ii              = 1;\n"
16194                "unsigned j               = 2;\n"
16195                "int someVerylongVariable = 1;\n"
16196                "AnotherLongType  ll = 123456;\n"
16197                "VeryVeryLongType k  = 2;\n"
16198                "int              myvar = 1;",
16199                Alignment);
16200   Alignment.ColumnLimit = 80;
16201   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
16202 
16203   verifyFormat(
16204       "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n"
16205       "          typename LongType, typename B>\n"
16206       "auto foo() {}\n",
16207       Alignment);
16208   verifyFormat("float a, b = 1;\n"
16209                "int   c = 2;\n"
16210                "int   dd = 3;\n",
16211                Alignment);
16212   verifyFormat("int   aa = ((1 > 2) ? 3 : 4);\n"
16213                "float b[1][] = {{3.f}};\n",
16214                Alignment);
16215   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16216   verifyFormat("float a, b = 1;\n"
16217                "int   c  = 2;\n"
16218                "int   dd = 3;\n",
16219                Alignment);
16220   verifyFormat("int   aa     = ((1 > 2) ? 3 : 4);\n"
16221                "float b[1][] = {{3.f}};\n",
16222                Alignment);
16223   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
16224 
16225   Alignment.ColumnLimit = 30;
16226   Alignment.BinPackParameters = false;
16227   verifyFormat("void foo(float     a,\n"
16228                "         float     b,\n"
16229                "         int       c,\n"
16230                "         uint32_t *d) {\n"
16231                "  int   *e = 0;\n"
16232                "  float  f = 0;\n"
16233                "  double g = 0;\n"
16234                "}\n"
16235                "void bar(ino_t     a,\n"
16236                "         int       b,\n"
16237                "         uint32_t *c,\n"
16238                "         bool      d) {}\n",
16239                Alignment);
16240   Alignment.BinPackParameters = true;
16241   Alignment.ColumnLimit = 80;
16242 
16243   // Bug 33507
16244   Alignment.PointerAlignment = FormatStyle::PAS_Middle;
16245   verifyFormat(
16246       "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n"
16247       "  static const Version verVs2017;\n"
16248       "  return true;\n"
16249       "});\n",
16250       Alignment);
16251   Alignment.PointerAlignment = FormatStyle::PAS_Right;
16252 
16253   // See llvm.org/PR35641
16254   Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
16255   verifyFormat("int func() { //\n"
16256                "  int      b;\n"
16257                "  unsigned c;\n"
16258                "}",
16259                Alignment);
16260 
16261   // See PR37175
16262   FormatStyle Style = getMozillaStyle();
16263   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
16264   EXPECT_EQ("DECOR1 /**/ int8_t /**/ DECOR2 /**/\n"
16265             "foo(int a);",
16266             format("DECOR1 /**/ int8_t /**/ DECOR2 /**/ foo (int a);", Style));
16267 
16268   Alignment.PointerAlignment = FormatStyle::PAS_Left;
16269   verifyFormat("unsigned int*       a;\n"
16270                "int*                b;\n"
16271                "unsigned int Const* c;\n"
16272                "unsigned int const* d;\n"
16273                "unsigned int Const& e;\n"
16274                "unsigned int const& f;",
16275                Alignment);
16276   verifyFormat("Const unsigned int* c;\n"
16277                "const unsigned int* d;\n"
16278                "Const unsigned int& e;\n"
16279                "const unsigned int& f;\n"
16280                "const unsigned      g;\n"
16281                "Const unsigned      h;",
16282                Alignment);
16283 
16284   Alignment.PointerAlignment = FormatStyle::PAS_Middle;
16285   verifyFormat("unsigned int *       a;\n"
16286                "int *                b;\n"
16287                "unsigned int Const * c;\n"
16288                "unsigned int const * d;\n"
16289                "unsigned int Const & e;\n"
16290                "unsigned int const & f;",
16291                Alignment);
16292   verifyFormat("Const unsigned int * c;\n"
16293                "const unsigned int * d;\n"
16294                "Const unsigned int & e;\n"
16295                "const unsigned int & f;\n"
16296                "const unsigned       g;\n"
16297                "Const unsigned       h;",
16298                Alignment);
16299 }
16300 
16301 TEST_F(FormatTest, AlignWithLineBreaks) {
16302   auto Style = getLLVMStyleWithColumns(120);
16303 
16304   EXPECT_EQ(Style.AlignConsecutiveAssignments, FormatStyle::ACS_None);
16305   EXPECT_EQ(Style.AlignConsecutiveDeclarations, FormatStyle::ACS_None);
16306   verifyFormat("void foo() {\n"
16307                "  int myVar = 5;\n"
16308                "  double x = 3.14;\n"
16309                "  auto str = \"Hello \"\n"
16310                "             \"World\";\n"
16311                "  auto s = \"Hello \"\n"
16312                "           \"Again\";\n"
16313                "}",
16314                Style);
16315 
16316   // clang-format off
16317   verifyFormat("void foo() {\n"
16318                "  const int capacityBefore = Entries.capacity();\n"
16319                "  const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
16320                "                                            std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
16321                "  const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
16322                "                                          std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
16323                "}",
16324                Style);
16325   // clang-format on
16326 
16327   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16328   verifyFormat("void foo() {\n"
16329                "  int myVar = 5;\n"
16330                "  double x  = 3.14;\n"
16331                "  auto str  = \"Hello \"\n"
16332                "              \"World\";\n"
16333                "  auto s    = \"Hello \"\n"
16334                "              \"Again\";\n"
16335                "}",
16336                Style);
16337 
16338   // clang-format off
16339   verifyFormat("void foo() {\n"
16340                "  const int capacityBefore = Entries.capacity();\n"
16341                "  const auto newEntry      = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
16342                "                                                 std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
16343                "  const X newEntry2        = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
16344                "                                                 std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
16345                "}",
16346                Style);
16347   // clang-format on
16348 
16349   Style.AlignConsecutiveAssignments = FormatStyle::ACS_None;
16350   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
16351   verifyFormat("void foo() {\n"
16352                "  int    myVar = 5;\n"
16353                "  double x = 3.14;\n"
16354                "  auto   str = \"Hello \"\n"
16355                "               \"World\";\n"
16356                "  auto   s = \"Hello \"\n"
16357                "             \"Again\";\n"
16358                "}",
16359                Style);
16360 
16361   // clang-format off
16362   verifyFormat("void foo() {\n"
16363                "  const int  capacityBefore = Entries.capacity();\n"
16364                "  const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
16365                "                                            std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
16366                "  const X    newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
16367                "                                             std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
16368                "}",
16369                Style);
16370   // clang-format on
16371 
16372   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16373   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
16374 
16375   verifyFormat("void foo() {\n"
16376                "  int    myVar = 5;\n"
16377                "  double x     = 3.14;\n"
16378                "  auto   str   = \"Hello \"\n"
16379                "                 \"World\";\n"
16380                "  auto   s     = \"Hello \"\n"
16381                "                 \"Again\";\n"
16382                "}",
16383                Style);
16384 
16385   // clang-format off
16386   verifyFormat("void foo() {\n"
16387                "  const int  capacityBefore = Entries.capacity();\n"
16388                "  const auto newEntry       = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
16389                "                                                  std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
16390                "  const X    newEntry2      = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
16391                "                                                  std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
16392                "}",
16393                Style);
16394   // clang-format on
16395 }
16396 
16397 TEST_F(FormatTest, AlignWithInitializerPeriods) {
16398   auto Style = getLLVMStyleWithColumns(60);
16399 
16400   verifyFormat("void foo1(void) {\n"
16401                "  BYTE p[1] = 1;\n"
16402                "  A B = {.one_foooooooooooooooo = 2,\n"
16403                "         .two_fooooooooooooo = 3,\n"
16404                "         .three_fooooooooooooo = 4};\n"
16405                "  BYTE payload = 2;\n"
16406                "}",
16407                Style);
16408 
16409   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16410   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_None;
16411   verifyFormat("void foo2(void) {\n"
16412                "  BYTE p[1]    = 1;\n"
16413                "  A B          = {.one_foooooooooooooooo = 2,\n"
16414                "                  .two_fooooooooooooo    = 3,\n"
16415                "                  .three_fooooooooooooo  = 4};\n"
16416                "  BYTE payload = 2;\n"
16417                "}",
16418                Style);
16419 
16420   Style.AlignConsecutiveAssignments = FormatStyle::ACS_None;
16421   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
16422   verifyFormat("void foo3(void) {\n"
16423                "  BYTE p[1] = 1;\n"
16424                "  A    B = {.one_foooooooooooooooo = 2,\n"
16425                "            .two_fooooooooooooo = 3,\n"
16426                "            .three_fooooooooooooo = 4};\n"
16427                "  BYTE payload = 2;\n"
16428                "}",
16429                Style);
16430 
16431   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16432   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
16433   verifyFormat("void foo4(void) {\n"
16434                "  BYTE p[1]    = 1;\n"
16435                "  A    B       = {.one_foooooooooooooooo = 2,\n"
16436                "                  .two_fooooooooooooo    = 3,\n"
16437                "                  .three_fooooooooooooo  = 4};\n"
16438                "  BYTE payload = 2;\n"
16439                "}",
16440                Style);
16441 }
16442 
16443 TEST_F(FormatTest, LinuxBraceBreaking) {
16444   FormatStyle LinuxBraceStyle = getLLVMStyle();
16445   LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
16446   verifyFormat("namespace a\n"
16447                "{\n"
16448                "class A\n"
16449                "{\n"
16450                "  void f()\n"
16451                "  {\n"
16452                "    if (true) {\n"
16453                "      a();\n"
16454                "      b();\n"
16455                "    } else {\n"
16456                "      a();\n"
16457                "    }\n"
16458                "  }\n"
16459                "  void g() { return; }\n"
16460                "};\n"
16461                "struct B {\n"
16462                "  int x;\n"
16463                "};\n"
16464                "} // namespace a\n",
16465                LinuxBraceStyle);
16466   verifyFormat("enum X {\n"
16467                "  Y = 0,\n"
16468                "}\n",
16469                LinuxBraceStyle);
16470   verifyFormat("struct S {\n"
16471                "  int Type;\n"
16472                "  union {\n"
16473                "    int x;\n"
16474                "    double y;\n"
16475                "  } Value;\n"
16476                "  class C\n"
16477                "  {\n"
16478                "    MyFavoriteType Value;\n"
16479                "  } Class;\n"
16480                "}\n",
16481                LinuxBraceStyle);
16482 }
16483 
16484 TEST_F(FormatTest, MozillaBraceBreaking) {
16485   FormatStyle MozillaBraceStyle = getLLVMStyle();
16486   MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
16487   MozillaBraceStyle.FixNamespaceComments = false;
16488   verifyFormat("namespace a {\n"
16489                "class A\n"
16490                "{\n"
16491                "  void f()\n"
16492                "  {\n"
16493                "    if (true) {\n"
16494                "      a();\n"
16495                "      b();\n"
16496                "    }\n"
16497                "  }\n"
16498                "  void g() { return; }\n"
16499                "};\n"
16500                "enum E\n"
16501                "{\n"
16502                "  A,\n"
16503                "  // foo\n"
16504                "  B,\n"
16505                "  C\n"
16506                "};\n"
16507                "struct B\n"
16508                "{\n"
16509                "  int x;\n"
16510                "};\n"
16511                "}\n",
16512                MozillaBraceStyle);
16513   verifyFormat("struct S\n"
16514                "{\n"
16515                "  int Type;\n"
16516                "  union\n"
16517                "  {\n"
16518                "    int x;\n"
16519                "    double y;\n"
16520                "  } Value;\n"
16521                "  class C\n"
16522                "  {\n"
16523                "    MyFavoriteType Value;\n"
16524                "  } Class;\n"
16525                "}\n",
16526                MozillaBraceStyle);
16527 }
16528 
16529 TEST_F(FormatTest, StroustrupBraceBreaking) {
16530   FormatStyle StroustrupBraceStyle = getLLVMStyle();
16531   StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
16532   verifyFormat("namespace a {\n"
16533                "class A {\n"
16534                "  void f()\n"
16535                "  {\n"
16536                "    if (true) {\n"
16537                "      a();\n"
16538                "      b();\n"
16539                "    }\n"
16540                "  }\n"
16541                "  void g() { return; }\n"
16542                "};\n"
16543                "struct B {\n"
16544                "  int x;\n"
16545                "};\n"
16546                "} // namespace a\n",
16547                StroustrupBraceStyle);
16548 
16549   verifyFormat("void foo()\n"
16550                "{\n"
16551                "  if (a) {\n"
16552                "    a();\n"
16553                "  }\n"
16554                "  else {\n"
16555                "    b();\n"
16556                "  }\n"
16557                "}\n",
16558                StroustrupBraceStyle);
16559 
16560   verifyFormat("#ifdef _DEBUG\n"
16561                "int foo(int i = 0)\n"
16562                "#else\n"
16563                "int foo(int i = 5)\n"
16564                "#endif\n"
16565                "{\n"
16566                "  return i;\n"
16567                "}",
16568                StroustrupBraceStyle);
16569 
16570   verifyFormat("void foo() {}\n"
16571                "void bar()\n"
16572                "#ifdef _DEBUG\n"
16573                "{\n"
16574                "  foo();\n"
16575                "}\n"
16576                "#else\n"
16577                "{\n"
16578                "}\n"
16579                "#endif",
16580                StroustrupBraceStyle);
16581 
16582   verifyFormat("void foobar() { int i = 5; }\n"
16583                "#ifdef _DEBUG\n"
16584                "void bar() {}\n"
16585                "#else\n"
16586                "void bar() { foobar(); }\n"
16587                "#endif",
16588                StroustrupBraceStyle);
16589 }
16590 
16591 TEST_F(FormatTest, AllmanBraceBreaking) {
16592   FormatStyle AllmanBraceStyle = getLLVMStyle();
16593   AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
16594 
16595   EXPECT_EQ("namespace a\n"
16596             "{\n"
16597             "void f();\n"
16598             "void g();\n"
16599             "} // namespace a\n",
16600             format("namespace a\n"
16601                    "{\n"
16602                    "void f();\n"
16603                    "void g();\n"
16604                    "}\n",
16605                    AllmanBraceStyle));
16606 
16607   verifyFormat("namespace a\n"
16608                "{\n"
16609                "class A\n"
16610                "{\n"
16611                "  void f()\n"
16612                "  {\n"
16613                "    if (true)\n"
16614                "    {\n"
16615                "      a();\n"
16616                "      b();\n"
16617                "    }\n"
16618                "  }\n"
16619                "  void g() { return; }\n"
16620                "};\n"
16621                "struct B\n"
16622                "{\n"
16623                "  int x;\n"
16624                "};\n"
16625                "union C\n"
16626                "{\n"
16627                "};\n"
16628                "} // namespace a",
16629                AllmanBraceStyle);
16630 
16631   verifyFormat("void f()\n"
16632                "{\n"
16633                "  if (true)\n"
16634                "  {\n"
16635                "    a();\n"
16636                "  }\n"
16637                "  else if (false)\n"
16638                "  {\n"
16639                "    b();\n"
16640                "  }\n"
16641                "  else\n"
16642                "  {\n"
16643                "    c();\n"
16644                "  }\n"
16645                "}\n",
16646                AllmanBraceStyle);
16647 
16648   verifyFormat("void f()\n"
16649                "{\n"
16650                "  for (int i = 0; i < 10; ++i)\n"
16651                "  {\n"
16652                "    a();\n"
16653                "  }\n"
16654                "  while (false)\n"
16655                "  {\n"
16656                "    b();\n"
16657                "  }\n"
16658                "  do\n"
16659                "  {\n"
16660                "    c();\n"
16661                "  } while (false)\n"
16662                "}\n",
16663                AllmanBraceStyle);
16664 
16665   verifyFormat("void f(int a)\n"
16666                "{\n"
16667                "  switch (a)\n"
16668                "  {\n"
16669                "  case 0:\n"
16670                "    break;\n"
16671                "  case 1:\n"
16672                "  {\n"
16673                "    break;\n"
16674                "  }\n"
16675                "  case 2:\n"
16676                "  {\n"
16677                "  }\n"
16678                "  break;\n"
16679                "  default:\n"
16680                "    break;\n"
16681                "  }\n"
16682                "}\n",
16683                AllmanBraceStyle);
16684 
16685   verifyFormat("enum X\n"
16686                "{\n"
16687                "  Y = 0,\n"
16688                "}\n",
16689                AllmanBraceStyle);
16690   verifyFormat("enum X\n"
16691                "{\n"
16692                "  Y = 0\n"
16693                "}\n",
16694                AllmanBraceStyle);
16695 
16696   verifyFormat("@interface BSApplicationController ()\n"
16697                "{\n"
16698                "@private\n"
16699                "  id _extraIvar;\n"
16700                "}\n"
16701                "@end\n",
16702                AllmanBraceStyle);
16703 
16704   verifyFormat("#ifdef _DEBUG\n"
16705                "int foo(int i = 0)\n"
16706                "#else\n"
16707                "int foo(int i = 5)\n"
16708                "#endif\n"
16709                "{\n"
16710                "  return i;\n"
16711                "}",
16712                AllmanBraceStyle);
16713 
16714   verifyFormat("void foo() {}\n"
16715                "void bar()\n"
16716                "#ifdef _DEBUG\n"
16717                "{\n"
16718                "  foo();\n"
16719                "}\n"
16720                "#else\n"
16721                "{\n"
16722                "}\n"
16723                "#endif",
16724                AllmanBraceStyle);
16725 
16726   verifyFormat("void foobar() { int i = 5; }\n"
16727                "#ifdef _DEBUG\n"
16728                "void bar() {}\n"
16729                "#else\n"
16730                "void bar() { foobar(); }\n"
16731                "#endif",
16732                AllmanBraceStyle);
16733 
16734   EXPECT_EQ(AllmanBraceStyle.AllowShortLambdasOnASingleLine,
16735             FormatStyle::SLS_All);
16736 
16737   verifyFormat("[](int i) { return i + 2; };\n"
16738                "[](int i, int j)\n"
16739                "{\n"
16740                "  auto x = i + j;\n"
16741                "  auto y = i * j;\n"
16742                "  return x ^ y;\n"
16743                "};\n"
16744                "void foo()\n"
16745                "{\n"
16746                "  auto shortLambda = [](int i) { return i + 2; };\n"
16747                "  auto longLambda = [](int i, int j)\n"
16748                "  {\n"
16749                "    auto x = i + j;\n"
16750                "    auto y = i * j;\n"
16751                "    return x ^ y;\n"
16752                "  };\n"
16753                "}",
16754                AllmanBraceStyle);
16755 
16756   AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
16757 
16758   verifyFormat("[](int i)\n"
16759                "{\n"
16760                "  return i + 2;\n"
16761                "};\n"
16762                "[](int i, int j)\n"
16763                "{\n"
16764                "  auto x = i + j;\n"
16765                "  auto y = i * j;\n"
16766                "  return x ^ y;\n"
16767                "};\n"
16768                "void foo()\n"
16769                "{\n"
16770                "  auto shortLambda = [](int i)\n"
16771                "  {\n"
16772                "    return i + 2;\n"
16773                "  };\n"
16774                "  auto longLambda = [](int i, int j)\n"
16775                "  {\n"
16776                "    auto x = i + j;\n"
16777                "    auto y = i * j;\n"
16778                "    return x ^ y;\n"
16779                "  };\n"
16780                "}",
16781                AllmanBraceStyle);
16782 
16783   // Reset
16784   AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_All;
16785 
16786   // This shouldn't affect ObjC blocks..
16787   verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
16788                "  // ...\n"
16789                "  int i;\n"
16790                "}];",
16791                AllmanBraceStyle);
16792   verifyFormat("void (^block)(void) = ^{\n"
16793                "  // ...\n"
16794                "  int i;\n"
16795                "};",
16796                AllmanBraceStyle);
16797   // .. or dict literals.
16798   verifyFormat("void f()\n"
16799                "{\n"
16800                "  // ...\n"
16801                "  [object someMethod:@{@\"a\" : @\"b\"}];\n"
16802                "}",
16803                AllmanBraceStyle);
16804   verifyFormat("void f()\n"
16805                "{\n"
16806                "  // ...\n"
16807                "  [object someMethod:@{a : @\"b\"}];\n"
16808                "}",
16809                AllmanBraceStyle);
16810   verifyFormat("int f()\n"
16811                "{ // comment\n"
16812                "  return 42;\n"
16813                "}",
16814                AllmanBraceStyle);
16815 
16816   AllmanBraceStyle.ColumnLimit = 19;
16817   verifyFormat("void f() { int i; }", AllmanBraceStyle);
16818   AllmanBraceStyle.ColumnLimit = 18;
16819   verifyFormat("void f()\n"
16820                "{\n"
16821                "  int i;\n"
16822                "}",
16823                AllmanBraceStyle);
16824   AllmanBraceStyle.ColumnLimit = 80;
16825 
16826   FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle;
16827   BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine =
16828       FormatStyle::SIS_WithoutElse;
16829   BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
16830   verifyFormat("void f(bool b)\n"
16831                "{\n"
16832                "  if (b)\n"
16833                "  {\n"
16834                "    return;\n"
16835                "  }\n"
16836                "}\n",
16837                BreakBeforeBraceShortIfs);
16838   verifyFormat("void f(bool b)\n"
16839                "{\n"
16840                "  if constexpr (b)\n"
16841                "  {\n"
16842                "    return;\n"
16843                "  }\n"
16844                "}\n",
16845                BreakBeforeBraceShortIfs);
16846   verifyFormat("void f(bool b)\n"
16847                "{\n"
16848                "  if CONSTEXPR (b)\n"
16849                "  {\n"
16850                "    return;\n"
16851                "  }\n"
16852                "}\n",
16853                BreakBeforeBraceShortIfs);
16854   verifyFormat("void f(bool b)\n"
16855                "{\n"
16856                "  if (b) return;\n"
16857                "}\n",
16858                BreakBeforeBraceShortIfs);
16859   verifyFormat("void f(bool b)\n"
16860                "{\n"
16861                "  if constexpr (b) return;\n"
16862                "}\n",
16863                BreakBeforeBraceShortIfs);
16864   verifyFormat("void f(bool b)\n"
16865                "{\n"
16866                "  if CONSTEXPR (b) return;\n"
16867                "}\n",
16868                BreakBeforeBraceShortIfs);
16869   verifyFormat("void f(bool b)\n"
16870                "{\n"
16871                "  while (b)\n"
16872                "  {\n"
16873                "    return;\n"
16874                "  }\n"
16875                "}\n",
16876                BreakBeforeBraceShortIfs);
16877 }
16878 
16879 TEST_F(FormatTest, WhitesmithsBraceBreaking) {
16880   FormatStyle WhitesmithsBraceStyle = getLLVMStyle();
16881   WhitesmithsBraceStyle.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
16882 
16883   // Make a few changes to the style for testing purposes
16884   WhitesmithsBraceStyle.AllowShortFunctionsOnASingleLine =
16885       FormatStyle::SFS_Empty;
16886   WhitesmithsBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
16887   WhitesmithsBraceStyle.ColumnLimit = 0;
16888 
16889   // FIXME: this test case can't decide whether there should be a blank line
16890   // after the ~D() line or not. It adds one if one doesn't exist in the test
16891   // and it removes the line if one exists.
16892   /*
16893   verifyFormat("class A;\n"
16894                "namespace B\n"
16895                "  {\n"
16896                "class C;\n"
16897                "// Comment\n"
16898                "class D\n"
16899                "  {\n"
16900                "public:\n"
16901                "  D();\n"
16902                "  ~D() {}\n"
16903                "private:\n"
16904                "  enum E\n"
16905                "    {\n"
16906                "    F\n"
16907                "    }\n"
16908                "  };\n"
16909                "  } // namespace B\n",
16910                WhitesmithsBraceStyle);
16911   */
16912 
16913   WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_None;
16914   verifyFormat("namespace a\n"
16915                "  {\n"
16916                "class A\n"
16917                "  {\n"
16918                "  void f()\n"
16919                "    {\n"
16920                "    if (true)\n"
16921                "      {\n"
16922                "      a();\n"
16923                "      b();\n"
16924                "      }\n"
16925                "    }\n"
16926                "  void g()\n"
16927                "    {\n"
16928                "    return;\n"
16929                "    }\n"
16930                "  };\n"
16931                "struct B\n"
16932                "  {\n"
16933                "  int x;\n"
16934                "  };\n"
16935                "  } // namespace a",
16936                WhitesmithsBraceStyle);
16937 
16938   verifyFormat("namespace a\n"
16939                "  {\n"
16940                "namespace b\n"
16941                "  {\n"
16942                "class A\n"
16943                "  {\n"
16944                "  void f()\n"
16945                "    {\n"
16946                "    if (true)\n"
16947                "      {\n"
16948                "      a();\n"
16949                "      b();\n"
16950                "      }\n"
16951                "    }\n"
16952                "  void g()\n"
16953                "    {\n"
16954                "    return;\n"
16955                "    }\n"
16956                "  };\n"
16957                "struct B\n"
16958                "  {\n"
16959                "  int x;\n"
16960                "  };\n"
16961                "  } // namespace b\n"
16962                "  } // namespace a",
16963                WhitesmithsBraceStyle);
16964 
16965   WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_Inner;
16966   verifyFormat("namespace a\n"
16967                "  {\n"
16968                "namespace b\n"
16969                "  {\n"
16970                "  class A\n"
16971                "    {\n"
16972                "    void f()\n"
16973                "      {\n"
16974                "      if (true)\n"
16975                "        {\n"
16976                "        a();\n"
16977                "        b();\n"
16978                "        }\n"
16979                "      }\n"
16980                "    void g()\n"
16981                "      {\n"
16982                "      return;\n"
16983                "      }\n"
16984                "    };\n"
16985                "  struct B\n"
16986                "    {\n"
16987                "    int x;\n"
16988                "    };\n"
16989                "  } // namespace b\n"
16990                "  } // namespace a",
16991                WhitesmithsBraceStyle);
16992 
16993   WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_All;
16994   verifyFormat("namespace a\n"
16995                "  {\n"
16996                "  namespace b\n"
16997                "    {\n"
16998                "    class A\n"
16999                "      {\n"
17000                "      void f()\n"
17001                "        {\n"
17002                "        if (true)\n"
17003                "          {\n"
17004                "          a();\n"
17005                "          b();\n"
17006                "          }\n"
17007                "        }\n"
17008                "      void g()\n"
17009                "        {\n"
17010                "        return;\n"
17011                "        }\n"
17012                "      };\n"
17013                "    struct B\n"
17014                "      {\n"
17015                "      int x;\n"
17016                "      };\n"
17017                "    } // namespace b\n"
17018                "  }   // namespace a",
17019                WhitesmithsBraceStyle);
17020 
17021   verifyFormat("void f()\n"
17022                "  {\n"
17023                "  if (true)\n"
17024                "    {\n"
17025                "    a();\n"
17026                "    }\n"
17027                "  else if (false)\n"
17028                "    {\n"
17029                "    b();\n"
17030                "    }\n"
17031                "  else\n"
17032                "    {\n"
17033                "    c();\n"
17034                "    }\n"
17035                "  }\n",
17036                WhitesmithsBraceStyle);
17037 
17038   verifyFormat("void f()\n"
17039                "  {\n"
17040                "  for (int i = 0; i < 10; ++i)\n"
17041                "    {\n"
17042                "    a();\n"
17043                "    }\n"
17044                "  while (false)\n"
17045                "    {\n"
17046                "    b();\n"
17047                "    }\n"
17048                "  do\n"
17049                "    {\n"
17050                "    c();\n"
17051                "    } while (false)\n"
17052                "  }\n",
17053                WhitesmithsBraceStyle);
17054 
17055   WhitesmithsBraceStyle.IndentCaseLabels = true;
17056   verifyFormat("void switchTest1(int a)\n"
17057                "  {\n"
17058                "  switch (a)\n"
17059                "    {\n"
17060                "    case 2:\n"
17061                "      {\n"
17062                "      }\n"
17063                "      break;\n"
17064                "    }\n"
17065                "  }\n",
17066                WhitesmithsBraceStyle);
17067 
17068   verifyFormat("void switchTest2(int a)\n"
17069                "  {\n"
17070                "  switch (a)\n"
17071                "    {\n"
17072                "    case 0:\n"
17073                "      break;\n"
17074                "    case 1:\n"
17075                "      {\n"
17076                "      break;\n"
17077                "      }\n"
17078                "    case 2:\n"
17079                "      {\n"
17080                "      }\n"
17081                "      break;\n"
17082                "    default:\n"
17083                "      break;\n"
17084                "    }\n"
17085                "  }\n",
17086                WhitesmithsBraceStyle);
17087 
17088   verifyFormat("void switchTest3(int a)\n"
17089                "  {\n"
17090                "  switch (a)\n"
17091                "    {\n"
17092                "    case 0:\n"
17093                "      {\n"
17094                "      foo(x);\n"
17095                "      }\n"
17096                "      break;\n"
17097                "    default:\n"
17098                "      {\n"
17099                "      foo(1);\n"
17100                "      }\n"
17101                "      break;\n"
17102                "    }\n"
17103                "  }\n",
17104                WhitesmithsBraceStyle);
17105 
17106   WhitesmithsBraceStyle.IndentCaseLabels = false;
17107 
17108   verifyFormat("void switchTest4(int a)\n"
17109                "  {\n"
17110                "  switch (a)\n"
17111                "    {\n"
17112                "  case 2:\n"
17113                "    {\n"
17114                "    }\n"
17115                "    break;\n"
17116                "    }\n"
17117                "  }\n",
17118                WhitesmithsBraceStyle);
17119 
17120   verifyFormat("void switchTest5(int a)\n"
17121                "  {\n"
17122                "  switch (a)\n"
17123                "    {\n"
17124                "  case 0:\n"
17125                "    break;\n"
17126                "  case 1:\n"
17127                "    {\n"
17128                "    foo();\n"
17129                "    break;\n"
17130                "    }\n"
17131                "  case 2:\n"
17132                "    {\n"
17133                "    }\n"
17134                "    break;\n"
17135                "  default:\n"
17136                "    break;\n"
17137                "    }\n"
17138                "  }\n",
17139                WhitesmithsBraceStyle);
17140 
17141   verifyFormat("void switchTest6(int a)\n"
17142                "  {\n"
17143                "  switch (a)\n"
17144                "    {\n"
17145                "  case 0:\n"
17146                "    {\n"
17147                "    foo(x);\n"
17148                "    }\n"
17149                "    break;\n"
17150                "  default:\n"
17151                "    {\n"
17152                "    foo(1);\n"
17153                "    }\n"
17154                "    break;\n"
17155                "    }\n"
17156                "  }\n",
17157                WhitesmithsBraceStyle);
17158 
17159   verifyFormat("enum X\n"
17160                "  {\n"
17161                "  Y = 0, // testing\n"
17162                "  }\n",
17163                WhitesmithsBraceStyle);
17164 
17165   verifyFormat("enum X\n"
17166                "  {\n"
17167                "  Y = 0\n"
17168                "  }\n",
17169                WhitesmithsBraceStyle);
17170   verifyFormat("enum X\n"
17171                "  {\n"
17172                "  Y = 0,\n"
17173                "  Z = 1\n"
17174                "  };\n",
17175                WhitesmithsBraceStyle);
17176 
17177   verifyFormat("@interface BSApplicationController ()\n"
17178                "  {\n"
17179                "@private\n"
17180                "  id _extraIvar;\n"
17181                "  }\n"
17182                "@end\n",
17183                WhitesmithsBraceStyle);
17184 
17185   verifyFormat("#ifdef _DEBUG\n"
17186                "int foo(int i = 0)\n"
17187                "#else\n"
17188                "int foo(int i = 5)\n"
17189                "#endif\n"
17190                "  {\n"
17191                "  return i;\n"
17192                "  }",
17193                WhitesmithsBraceStyle);
17194 
17195   verifyFormat("void foo() {}\n"
17196                "void bar()\n"
17197                "#ifdef _DEBUG\n"
17198                "  {\n"
17199                "  foo();\n"
17200                "  }\n"
17201                "#else\n"
17202                "  {\n"
17203                "  }\n"
17204                "#endif",
17205                WhitesmithsBraceStyle);
17206 
17207   verifyFormat("void foobar()\n"
17208                "  {\n"
17209                "  int i = 5;\n"
17210                "  }\n"
17211                "#ifdef _DEBUG\n"
17212                "void bar()\n"
17213                "  {\n"
17214                "  }\n"
17215                "#else\n"
17216                "void bar()\n"
17217                "  {\n"
17218                "  foobar();\n"
17219                "  }\n"
17220                "#endif",
17221                WhitesmithsBraceStyle);
17222 
17223   // This shouldn't affect ObjC blocks..
17224   verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
17225                "  // ...\n"
17226                "  int i;\n"
17227                "}];",
17228                WhitesmithsBraceStyle);
17229   verifyFormat("void (^block)(void) = ^{\n"
17230                "  // ...\n"
17231                "  int i;\n"
17232                "};",
17233                WhitesmithsBraceStyle);
17234   // .. or dict literals.
17235   verifyFormat("void f()\n"
17236                "  {\n"
17237                "  [object someMethod:@{@\"a\" : @\"b\"}];\n"
17238                "  }",
17239                WhitesmithsBraceStyle);
17240 
17241   verifyFormat("int f()\n"
17242                "  { // comment\n"
17243                "  return 42;\n"
17244                "  }",
17245                WhitesmithsBraceStyle);
17246 
17247   FormatStyle BreakBeforeBraceShortIfs = WhitesmithsBraceStyle;
17248   BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine =
17249       FormatStyle::SIS_OnlyFirstIf;
17250   BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
17251   verifyFormat("void f(bool b)\n"
17252                "  {\n"
17253                "  if (b)\n"
17254                "    {\n"
17255                "    return;\n"
17256                "    }\n"
17257                "  }\n",
17258                BreakBeforeBraceShortIfs);
17259   verifyFormat("void f(bool b)\n"
17260                "  {\n"
17261                "  if (b) return;\n"
17262                "  }\n",
17263                BreakBeforeBraceShortIfs);
17264   verifyFormat("void f(bool b)\n"
17265                "  {\n"
17266                "  while (b)\n"
17267                "    {\n"
17268                "    return;\n"
17269                "    }\n"
17270                "  }\n",
17271                BreakBeforeBraceShortIfs);
17272 }
17273 
17274 TEST_F(FormatTest, GNUBraceBreaking) {
17275   FormatStyle GNUBraceStyle = getLLVMStyle();
17276   GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
17277   verifyFormat("namespace a\n"
17278                "{\n"
17279                "class A\n"
17280                "{\n"
17281                "  void f()\n"
17282                "  {\n"
17283                "    int a;\n"
17284                "    {\n"
17285                "      int b;\n"
17286                "    }\n"
17287                "    if (true)\n"
17288                "      {\n"
17289                "        a();\n"
17290                "        b();\n"
17291                "      }\n"
17292                "  }\n"
17293                "  void g() { return; }\n"
17294                "}\n"
17295                "} // namespace a",
17296                GNUBraceStyle);
17297 
17298   verifyFormat("void f()\n"
17299                "{\n"
17300                "  if (true)\n"
17301                "    {\n"
17302                "      a();\n"
17303                "    }\n"
17304                "  else if (false)\n"
17305                "    {\n"
17306                "      b();\n"
17307                "    }\n"
17308                "  else\n"
17309                "    {\n"
17310                "      c();\n"
17311                "    }\n"
17312                "}\n",
17313                GNUBraceStyle);
17314 
17315   verifyFormat("void f()\n"
17316                "{\n"
17317                "  for (int i = 0; i < 10; ++i)\n"
17318                "    {\n"
17319                "      a();\n"
17320                "    }\n"
17321                "  while (false)\n"
17322                "    {\n"
17323                "      b();\n"
17324                "    }\n"
17325                "  do\n"
17326                "    {\n"
17327                "      c();\n"
17328                "    }\n"
17329                "  while (false);\n"
17330                "}\n",
17331                GNUBraceStyle);
17332 
17333   verifyFormat("void f(int a)\n"
17334                "{\n"
17335                "  switch (a)\n"
17336                "    {\n"
17337                "    case 0:\n"
17338                "      break;\n"
17339                "    case 1:\n"
17340                "      {\n"
17341                "        break;\n"
17342                "      }\n"
17343                "    case 2:\n"
17344                "      {\n"
17345                "      }\n"
17346                "      break;\n"
17347                "    default:\n"
17348                "      break;\n"
17349                "    }\n"
17350                "}\n",
17351                GNUBraceStyle);
17352 
17353   verifyFormat("enum X\n"
17354                "{\n"
17355                "  Y = 0,\n"
17356                "}\n",
17357                GNUBraceStyle);
17358 
17359   verifyFormat("@interface BSApplicationController ()\n"
17360                "{\n"
17361                "@private\n"
17362                "  id _extraIvar;\n"
17363                "}\n"
17364                "@end\n",
17365                GNUBraceStyle);
17366 
17367   verifyFormat("#ifdef _DEBUG\n"
17368                "int foo(int i = 0)\n"
17369                "#else\n"
17370                "int foo(int i = 5)\n"
17371                "#endif\n"
17372                "{\n"
17373                "  return i;\n"
17374                "}",
17375                GNUBraceStyle);
17376 
17377   verifyFormat("void foo() {}\n"
17378                "void bar()\n"
17379                "#ifdef _DEBUG\n"
17380                "{\n"
17381                "  foo();\n"
17382                "}\n"
17383                "#else\n"
17384                "{\n"
17385                "}\n"
17386                "#endif",
17387                GNUBraceStyle);
17388 
17389   verifyFormat("void foobar() { int i = 5; }\n"
17390                "#ifdef _DEBUG\n"
17391                "void bar() {}\n"
17392                "#else\n"
17393                "void bar() { foobar(); }\n"
17394                "#endif",
17395                GNUBraceStyle);
17396 }
17397 
17398 TEST_F(FormatTest, WebKitBraceBreaking) {
17399   FormatStyle WebKitBraceStyle = getLLVMStyle();
17400   WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit;
17401   WebKitBraceStyle.FixNamespaceComments = false;
17402   verifyFormat("namespace a {\n"
17403                "class A {\n"
17404                "  void f()\n"
17405                "  {\n"
17406                "    if (true) {\n"
17407                "      a();\n"
17408                "      b();\n"
17409                "    }\n"
17410                "  }\n"
17411                "  void g() { return; }\n"
17412                "};\n"
17413                "enum E {\n"
17414                "  A,\n"
17415                "  // foo\n"
17416                "  B,\n"
17417                "  C\n"
17418                "};\n"
17419                "struct B {\n"
17420                "  int x;\n"
17421                "};\n"
17422                "}\n",
17423                WebKitBraceStyle);
17424   verifyFormat("struct S {\n"
17425                "  int Type;\n"
17426                "  union {\n"
17427                "    int x;\n"
17428                "    double y;\n"
17429                "  } Value;\n"
17430                "  class C {\n"
17431                "    MyFavoriteType Value;\n"
17432                "  } Class;\n"
17433                "};\n",
17434                WebKitBraceStyle);
17435 }
17436 
17437 TEST_F(FormatTest, CatchExceptionReferenceBinding) {
17438   verifyFormat("void f() {\n"
17439                "  try {\n"
17440                "  } catch (const Exception &e) {\n"
17441                "  }\n"
17442                "}\n",
17443                getLLVMStyle());
17444 }
17445 
17446 TEST_F(FormatTest, CatchAlignArrayOfStructuresRightAlignment) {
17447   auto Style = getLLVMStyle();
17448   Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
17449   Style.AlignConsecutiveAssignments =
17450       FormatStyle::AlignConsecutiveStyle::ACS_Consecutive;
17451   Style.AlignConsecutiveDeclarations =
17452       FormatStyle::AlignConsecutiveStyle::ACS_Consecutive;
17453   verifyFormat("struct test demo[] = {\n"
17454                "    {56,    23, \"hello\"},\n"
17455                "    {-1, 93463, \"world\"},\n"
17456                "    { 7,     5,    \"!!\"}\n"
17457                "};\n",
17458                Style);
17459 
17460   verifyFormat("struct test demo[] = {\n"
17461                "    {56,    23, \"hello\"}, // first line\n"
17462                "    {-1, 93463, \"world\"}, // second line\n"
17463                "    { 7,     5,    \"!!\"}  // third line\n"
17464                "};\n",
17465                Style);
17466 
17467   verifyFormat("struct test demo[4] = {\n"
17468                "    { 56,    23, 21,       \"oh\"}, // first line\n"
17469                "    { -1, 93463, 22,       \"my\"}, // second line\n"
17470                "    {  7,     5,  1, \"goodness\"}  // third line\n"
17471                "    {234,     5,  1, \"gracious\"}  // fourth line\n"
17472                "};\n",
17473                Style);
17474 
17475   verifyFormat("struct test demo[3] = {\n"
17476                "    {56,    23, \"hello\"},\n"
17477                "    {-1, 93463, \"world\"},\n"
17478                "    { 7,     5,    \"!!\"}\n"
17479                "};\n",
17480                Style);
17481 
17482   verifyFormat("struct test demo[3] = {\n"
17483                "    {int{56},    23, \"hello\"},\n"
17484                "    {int{-1}, 93463, \"world\"},\n"
17485                "    { int{7},     5,    \"!!\"}\n"
17486                "};\n",
17487                Style);
17488 
17489   verifyFormat("struct test demo[] = {\n"
17490                "    {56,    23, \"hello\"},\n"
17491                "    {-1, 93463, \"world\"},\n"
17492                "    { 7,     5,    \"!!\"},\n"
17493                "};\n",
17494                Style);
17495 
17496   verifyFormat("test demo[] = {\n"
17497                "    {56,    23, \"hello\"},\n"
17498                "    {-1, 93463, \"world\"},\n"
17499                "    { 7,     5,    \"!!\"},\n"
17500                "};\n",
17501                Style);
17502 
17503   verifyFormat("demo = std::array<struct test, 3>{\n"
17504                "    test{56,    23, \"hello\"},\n"
17505                "    test{-1, 93463, \"world\"},\n"
17506                "    test{ 7,     5,    \"!!\"},\n"
17507                "};\n",
17508                Style);
17509 
17510   verifyFormat("test demo[] = {\n"
17511                "    {56,    23, \"hello\"},\n"
17512                "#if X\n"
17513                "    {-1, 93463, \"world\"},\n"
17514                "#endif\n"
17515                "    { 7,     5,    \"!!\"}\n"
17516                "};\n",
17517                Style);
17518 
17519   verifyFormat(
17520       "test demo[] = {\n"
17521       "    { 7,    23,\n"
17522       "     \"hello world i am a very long line that really, in any\"\n"
17523       "     \"just world, ought to be split over multiple lines\"},\n"
17524       "    {-1, 93463,                                  \"world\"},\n"
17525       "    {56,     5,                                     \"!!\"}\n"
17526       "};\n",
17527       Style);
17528 
17529   verifyFormat("return GradForUnaryCwise(g, {\n"
17530                "                                {{\"sign\"}, \"Sign\",  "
17531                "  {\"x\", \"dy\"}},\n"
17532                "                                {  {\"dx\"},  \"Mul\", {\"dy\""
17533                ", \"sign\"}},\n"
17534                "});\n",
17535                Style);
17536 
17537   Style.ColumnLimit = 0;
17538   EXPECT_EQ(
17539       "test demo[] = {\n"
17540       "    {56,    23, \"hello world i am a very long line that really, "
17541       "in any just world, ought to be split over multiple lines\"},\n"
17542       "    {-1, 93463,                                                  "
17543       "                                                 \"world\"},\n"
17544       "    { 7,     5,                                                  "
17545       "                                                    \"!!\"},\n"
17546       "};",
17547       format("test demo[] = {{56, 23, \"hello world i am a very long line "
17548              "that really, in any just world, ought to be split over multiple "
17549              "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
17550              Style));
17551 
17552   Style.ColumnLimit = 80;
17553   verifyFormat("test demo[] = {\n"
17554                "    {56,    23, /* a comment */ \"hello\"},\n"
17555                "    {-1, 93463,                 \"world\"},\n"
17556                "    { 7,     5,                    \"!!\"}\n"
17557                "};\n",
17558                Style);
17559 
17560   verifyFormat("test demo[] = {\n"
17561                "    {56,    23,                    \"hello\"},\n"
17562                "    {-1, 93463, \"world\" /* comment here */},\n"
17563                "    { 7,     5,                       \"!!\"}\n"
17564                "};\n",
17565                Style);
17566 
17567   verifyFormat("test demo[] = {\n"
17568                "    {56, /* a comment */ 23, \"hello\"},\n"
17569                "    {-1,              93463, \"world\"},\n"
17570                "    { 7,                  5,    \"!!\"}\n"
17571                "};\n",
17572                Style);
17573 
17574   Style.ColumnLimit = 20;
17575   EXPECT_EQ(
17576       "demo = std::array<\n"
17577       "    struct test, 3>{\n"
17578       "    test{\n"
17579       "         56,    23,\n"
17580       "         \"hello \"\n"
17581       "         \"world i \"\n"
17582       "         \"am a very \"\n"
17583       "         \"long line \"\n"
17584       "         \"that \"\n"
17585       "         \"really, \"\n"
17586       "         \"in any \"\n"
17587       "         \"just \"\n"
17588       "         \"world, \"\n"
17589       "         \"ought to \"\n"
17590       "         \"be split \"\n"
17591       "         \"over \"\n"
17592       "         \"multiple \"\n"
17593       "         \"lines\"},\n"
17594       "    test{-1, 93463,\n"
17595       "         \"world\"},\n"
17596       "    test{ 7,     5,\n"
17597       "         \"!!\"   },\n"
17598       "};",
17599       format("demo = std::array<struct test, 3>{test{56, 23, \"hello world "
17600              "i am a very long line that really, in any just world, ought "
17601              "to be split over multiple lines\"},test{-1, 93463, \"world\"},"
17602              "test{7, 5, \"!!\"},};",
17603              Style));
17604   // This caused a core dump by enabling Alignment in the LLVMStyle globally
17605   Style = getLLVMStyleWithColumns(50);
17606   Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
17607   verifyFormat("static A x = {\n"
17608                "    {{init1, init2, init3, init4},\n"
17609                "     {init1, init2, init3, init4}}\n"
17610                "};",
17611                Style);
17612   Style.ColumnLimit = 100;
17613   EXPECT_EQ(
17614       "test demo[] = {\n"
17615       "    {56,    23,\n"
17616       "     \"hello world i am a very long line that really, in any just world"
17617       ", ought to be split over \"\n"
17618       "     \"multiple lines\"  },\n"
17619       "    {-1, 93463, \"world\"},\n"
17620       "    { 7,     5,    \"!!\"},\n"
17621       "};",
17622       format("test demo[] = {{56, 23, \"hello world i am a very long line "
17623              "that really, in any just world, ought to be split over multiple "
17624              "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
17625              Style));
17626 
17627   Style = getLLVMStyleWithColumns(50);
17628   Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
17629   Style.AlignConsecutiveAssignments =
17630       FormatStyle::AlignConsecutiveStyle::ACS_Consecutive;
17631   Style.AlignConsecutiveDeclarations =
17632       FormatStyle::AlignConsecutiveStyle::ACS_Consecutive;
17633   verifyFormat("struct test demo[] = {\n"
17634                "    {56,    23, \"hello\"},\n"
17635                "    {-1, 93463, \"world\"},\n"
17636                "    { 7,     5,    \"!!\"}\n"
17637                "};\n"
17638                "static A x = {\n"
17639                "    {{init1, init2, init3, init4},\n"
17640                "     {init1, init2, init3, init4}}\n"
17641                "};",
17642                Style);
17643   Style.ColumnLimit = 100;
17644   Style.AlignConsecutiveAssignments =
17645       FormatStyle::AlignConsecutiveStyle::ACS_AcrossComments;
17646   Style.AlignConsecutiveDeclarations =
17647       FormatStyle::AlignConsecutiveStyle::ACS_AcrossComments;
17648   verifyFormat("struct test demo[] = {\n"
17649                "    {56,    23, \"hello\"},\n"
17650                "    {-1, 93463, \"world\"},\n"
17651                "    { 7,     5,    \"!!\"}\n"
17652                "};\n"
17653                "struct test demo[4] = {\n"
17654                "    { 56,    23, 21,       \"oh\"}, // first line\n"
17655                "    { -1, 93463, 22,       \"my\"}, // second line\n"
17656                "    {  7,     5,  1, \"goodness\"}  // third line\n"
17657                "    {234,     5,  1, \"gracious\"}  // fourth line\n"
17658                "};\n",
17659                Style);
17660   EXPECT_EQ(
17661       "test demo[] = {\n"
17662       "    {56,\n"
17663       "     \"hello world i am a very long line that really, in any just world"
17664       ", ought to be split over \"\n"
17665       "     \"multiple lines\",    23},\n"
17666       "    {-1,      \"world\", 93463},\n"
17667       "    { 7,         \"!!\",     5},\n"
17668       "};",
17669       format("test demo[] = {{56, \"hello world i am a very long line "
17670              "that really, in any just world, ought to be split over multiple "
17671              "lines\", 23},{-1, \"world\", 93463},{7, \"!!\", 5},};",
17672              Style));
17673 }
17674 
17675 TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) {
17676   auto Style = getLLVMStyle();
17677   Style.AlignArrayOfStructures = FormatStyle::AIAS_Left;
17678   verifyFormat("struct test demo[] = {\n"
17679                "    {56, 23,    \"hello\"},\n"
17680                "    {-1, 93463, \"world\"},\n"
17681                "    {7,  5,     \"!!\"   }\n"
17682                "};\n",
17683                Style);
17684 
17685   verifyFormat("struct test demo[] = {\n"
17686                "    {56, 23,    \"hello\"}, // first line\n"
17687                "    {-1, 93463, \"world\"}, // second line\n"
17688                "    {7,  5,     \"!!\"   }  // third line\n"
17689                "};\n",
17690                Style);
17691   verifyFormat("struct test demo[4] = {\n"
17692                "    {56,  23,    21, \"oh\"      }, // first line\n"
17693                "    {-1,  93463, 22, \"my\"      }, // second line\n"
17694                "    {7,   5,     1,  \"goodness\"}  // third line\n"
17695                "    {234, 5,     1,  \"gracious\"}  // fourth line\n"
17696                "};\n",
17697                Style);
17698   verifyFormat("struct test demo[3] = {\n"
17699                "    {56, 23,    \"hello\"},\n"
17700                "    {-1, 93463, \"world\"},\n"
17701                "    {7,  5,     \"!!\"   }\n"
17702                "};\n",
17703                Style);
17704 
17705   verifyFormat("struct test demo[3] = {\n"
17706                "    {int{56}, 23,    \"hello\"},\n"
17707                "    {int{-1}, 93463, \"world\"},\n"
17708                "    {int{7},  5,     \"!!\"   }\n"
17709                "};\n",
17710                Style);
17711   verifyFormat("struct test demo[] = {\n"
17712                "    {56, 23,    \"hello\"},\n"
17713                "    {-1, 93463, \"world\"},\n"
17714                "    {7,  5,     \"!!\"   },\n"
17715                "};\n",
17716                Style);
17717   verifyFormat("test demo[] = {\n"
17718                "    {56, 23,    \"hello\"},\n"
17719                "    {-1, 93463, \"world\"},\n"
17720                "    {7,  5,     \"!!\"   },\n"
17721                "};\n",
17722                Style);
17723   verifyFormat("demo = std::array<struct test, 3>{\n"
17724                "    test{56, 23,    \"hello\"},\n"
17725                "    test{-1, 93463, \"world\"},\n"
17726                "    test{7,  5,     \"!!\"   },\n"
17727                "};\n",
17728                Style);
17729   verifyFormat("test demo[] = {\n"
17730                "    {56, 23,    \"hello\"},\n"
17731                "#if X\n"
17732                "    {-1, 93463, \"world\"},\n"
17733                "#endif\n"
17734                "    {7,  5,     \"!!\"   }\n"
17735                "};\n",
17736                Style);
17737   verifyFormat(
17738       "test demo[] = {\n"
17739       "    {7,  23,\n"
17740       "     \"hello world i am a very long line that really, in any\"\n"
17741       "     \"just world, ought to be split over multiple lines\"},\n"
17742       "    {-1, 93463, \"world\"                                 },\n"
17743       "    {56, 5,     \"!!\"                                    }\n"
17744       "};\n",
17745       Style);
17746 
17747   verifyFormat("return GradForUnaryCwise(g, {\n"
17748                "                                {{\"sign\"}, \"Sign\", {\"x\", "
17749                "\"dy\"}   },\n"
17750                "                                {{\"dx\"},   \"Mul\",  "
17751                "{\"dy\", \"sign\"}},\n"
17752                "});\n",
17753                Style);
17754 
17755   Style.ColumnLimit = 0;
17756   EXPECT_EQ(
17757       "test demo[] = {\n"
17758       "    {56, 23,    \"hello world i am a very long line that really, in any "
17759       "just world, ought to be split over multiple lines\"},\n"
17760       "    {-1, 93463, \"world\"                                               "
17761       "                                                   },\n"
17762       "    {7,  5,     \"!!\"                                                  "
17763       "                                                   },\n"
17764       "};",
17765       format("test demo[] = {{56, 23, \"hello world i am a very long line "
17766              "that really, in any just world, ought to be split over multiple "
17767              "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
17768              Style));
17769 
17770   Style.ColumnLimit = 80;
17771   verifyFormat("test demo[] = {\n"
17772                "    {56, 23,    /* a comment */ \"hello\"},\n"
17773                "    {-1, 93463, \"world\"                },\n"
17774                "    {7,  5,     \"!!\"                   }\n"
17775                "};\n",
17776                Style);
17777 
17778   verifyFormat("test demo[] = {\n"
17779                "    {56, 23,    \"hello\"                   },\n"
17780                "    {-1, 93463, \"world\" /* comment here */},\n"
17781                "    {7,  5,     \"!!\"                      }\n"
17782                "};\n",
17783                Style);
17784 
17785   verifyFormat("test demo[] = {\n"
17786                "    {56, /* a comment */ 23, \"hello\"},\n"
17787                "    {-1, 93463,              \"world\"},\n"
17788                "    {7,  5,                  \"!!\"   }\n"
17789                "};\n",
17790                Style);
17791 
17792   Style.ColumnLimit = 20;
17793   EXPECT_EQ(
17794       "demo = std::array<\n"
17795       "    struct test, 3>{\n"
17796       "    test{\n"
17797       "         56, 23,\n"
17798       "         \"hello \"\n"
17799       "         \"world i \"\n"
17800       "         \"am a very \"\n"
17801       "         \"long line \"\n"
17802       "         \"that \"\n"
17803       "         \"really, \"\n"
17804       "         \"in any \"\n"
17805       "         \"just \"\n"
17806       "         \"world, \"\n"
17807       "         \"ought to \"\n"
17808       "         \"be split \"\n"
17809       "         \"over \"\n"
17810       "         \"multiple \"\n"
17811       "         \"lines\"},\n"
17812       "    test{-1, 93463,\n"
17813       "         \"world\"},\n"
17814       "    test{7,  5,\n"
17815       "         \"!!\"   },\n"
17816       "};",
17817       format("demo = std::array<struct test, 3>{test{56, 23, \"hello world "
17818              "i am a very long line that really, in any just world, ought "
17819              "to be split over multiple lines\"},test{-1, 93463, \"world\"},"
17820              "test{7, 5, \"!!\"},};",
17821              Style));
17822 
17823   // This caused a core dump by enabling Alignment in the LLVMStyle globally
17824   Style = getLLVMStyleWithColumns(50);
17825   Style.AlignArrayOfStructures = FormatStyle::AIAS_Left;
17826   verifyFormat("static A x = {\n"
17827                "    {{init1, init2, init3, init4},\n"
17828                "     {init1, init2, init3, init4}}\n"
17829                "};",
17830                Style);
17831   Style.ColumnLimit = 100;
17832   EXPECT_EQ(
17833       "test demo[] = {\n"
17834       "    {56, 23,\n"
17835       "     \"hello world i am a very long line that really, in any just world"
17836       ", ought to be split over \"\n"
17837       "     \"multiple lines\"  },\n"
17838       "    {-1, 93463, \"world\"},\n"
17839       "    {7,  5,     \"!!\"   },\n"
17840       "};",
17841       format("test demo[] = {{56, 23, \"hello world i am a very long line "
17842              "that really, in any just world, ought to be split over multiple "
17843              "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
17844              Style));
17845 }
17846 
17847 TEST_F(FormatTest, UnderstandsPragmas) {
17848   verifyFormat("#pragma omp reduction(| : var)");
17849   verifyFormat("#pragma omp reduction(+ : var)");
17850 
17851   EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string "
17852             "(including parentheses).",
17853             format("#pragma    mark   Any non-hyphenated or hyphenated string "
17854                    "(including parentheses)."));
17855 }
17856 
17857 TEST_F(FormatTest, UnderstandPragmaOption) {
17858   verifyFormat("#pragma option -C -A");
17859 
17860   EXPECT_EQ("#pragma option -C -A", format("#pragma    option   -C   -A"));
17861 }
17862 
17863 TEST_F(FormatTest, OptimizeBreakPenaltyVsExcess) {
17864   FormatStyle Style = getLLVMStyle();
17865   Style.ColumnLimit = 20;
17866 
17867   // See PR41213
17868   EXPECT_EQ("/*\n"
17869             " *\t9012345\n"
17870             " * /8901\n"
17871             " */",
17872             format("/*\n"
17873                    " *\t9012345 /8901\n"
17874                    " */",
17875                    Style));
17876   EXPECT_EQ("/*\n"
17877             " *345678\n"
17878             " *\t/8901\n"
17879             " */",
17880             format("/*\n"
17881                    " *345678\t/8901\n"
17882                    " */",
17883                    Style));
17884 
17885   verifyFormat("int a; // the\n"
17886                "       // comment",
17887                Style);
17888   EXPECT_EQ("int a; /* first line\n"
17889             "        * second\n"
17890             "        * line third\n"
17891             "        * line\n"
17892             "        */",
17893             format("int a; /* first line\n"
17894                    "        * second\n"
17895                    "        * line third\n"
17896                    "        * line\n"
17897                    "        */",
17898                    Style));
17899   EXPECT_EQ("int a; // first line\n"
17900             "       // second\n"
17901             "       // line third\n"
17902             "       // line",
17903             format("int a; // first line\n"
17904                    "       // second line\n"
17905                    "       // third line",
17906                    Style));
17907 
17908   Style.PenaltyExcessCharacter = 90;
17909   verifyFormat("int a; // the comment", Style);
17910   EXPECT_EQ("int a; // the comment\n"
17911             "       // aaa",
17912             format("int a; // the comment aaa", Style));
17913   EXPECT_EQ("int a; /* first line\n"
17914             "        * second line\n"
17915             "        * third line\n"
17916             "        */",
17917             format("int a; /* first line\n"
17918                    "        * second line\n"
17919                    "        * third line\n"
17920                    "        */",
17921                    Style));
17922   EXPECT_EQ("int a; // first line\n"
17923             "       // second line\n"
17924             "       // third line",
17925             format("int a; // first line\n"
17926                    "       // second line\n"
17927                    "       // third line",
17928                    Style));
17929   // FIXME: Investigate why this is not getting the same layout as the test
17930   // above.
17931   EXPECT_EQ("int a; /* first line\n"
17932             "        * second line\n"
17933             "        * third line\n"
17934             "        */",
17935             format("int a; /* first line second line third line"
17936                    "\n*/",
17937                    Style));
17938 
17939   EXPECT_EQ("// foo bar baz bazfoo\n"
17940             "// foo bar foo bar\n",
17941             format("// foo bar baz bazfoo\n"
17942                    "// foo bar foo           bar\n",
17943                    Style));
17944   EXPECT_EQ("// foo bar baz bazfoo\n"
17945             "// foo bar foo bar\n",
17946             format("// foo bar baz      bazfoo\n"
17947                    "// foo            bar foo bar\n",
17948                    Style));
17949 
17950   // FIXME: Optimally, we'd keep bazfoo on the first line and reflow bar to the
17951   // next one.
17952   EXPECT_EQ("// foo bar baz bazfoo\n"
17953             "// bar foo bar\n",
17954             format("// foo bar baz      bazfoo bar\n"
17955                    "// foo            bar\n",
17956                    Style));
17957 
17958   EXPECT_EQ("// foo bar baz bazfoo\n"
17959             "// foo bar baz bazfoo\n"
17960             "// bar foo bar\n",
17961             format("// foo bar baz      bazfoo\n"
17962                    "// foo bar baz      bazfoo bar\n"
17963                    "// foo bar\n",
17964                    Style));
17965 
17966   EXPECT_EQ("// foo bar baz bazfoo\n"
17967             "// foo bar baz bazfoo\n"
17968             "// bar foo bar\n",
17969             format("// foo bar baz      bazfoo\n"
17970                    "// foo bar baz      bazfoo bar\n"
17971                    "// foo           bar\n",
17972                    Style));
17973 
17974   // Make sure we do not keep protruding characters if strict mode reflow is
17975   // cheaper than keeping protruding characters.
17976   Style.ColumnLimit = 21;
17977   EXPECT_EQ(
17978       "// foo foo foo foo\n"
17979       "// foo foo foo foo\n"
17980       "// foo foo foo foo\n",
17981       format("// foo foo foo foo foo foo foo foo foo foo foo foo\n", Style));
17982 
17983   EXPECT_EQ("int a = /* long block\n"
17984             "           comment */\n"
17985             "    42;",
17986             format("int a = /* long block comment */ 42;", Style));
17987 }
17988 
17989 #define EXPECT_ALL_STYLES_EQUAL(Styles)                                        \
17990   for (size_t i = 1; i < Styles.size(); ++i)                                   \
17991   EXPECT_EQ(Styles[0], Styles[i])                                              \
17992       << "Style #" << i << " of " << Styles.size() << " differs from Style #0"
17993 
17994 TEST_F(FormatTest, GetsPredefinedStyleByName) {
17995   SmallVector<FormatStyle, 3> Styles;
17996   Styles.resize(3);
17997 
17998   Styles[0] = getLLVMStyle();
17999   EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1]));
18000   EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2]));
18001   EXPECT_ALL_STYLES_EQUAL(Styles);
18002 
18003   Styles[0] = getGoogleStyle();
18004   EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1]));
18005   EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2]));
18006   EXPECT_ALL_STYLES_EQUAL(Styles);
18007 
18008   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
18009   EXPECT_TRUE(
18010       getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1]));
18011   EXPECT_TRUE(
18012       getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2]));
18013   EXPECT_ALL_STYLES_EQUAL(Styles);
18014 
18015   Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp);
18016   EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1]));
18017   EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2]));
18018   EXPECT_ALL_STYLES_EQUAL(Styles);
18019 
18020   Styles[0] = getMozillaStyle();
18021   EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1]));
18022   EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2]));
18023   EXPECT_ALL_STYLES_EQUAL(Styles);
18024 
18025   Styles[0] = getWebKitStyle();
18026   EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1]));
18027   EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2]));
18028   EXPECT_ALL_STYLES_EQUAL(Styles);
18029 
18030   Styles[0] = getGNUStyle();
18031   EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1]));
18032   EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2]));
18033   EXPECT_ALL_STYLES_EQUAL(Styles);
18034 
18035   EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0]));
18036 }
18037 
18038 TEST_F(FormatTest, GetsCorrectBasedOnStyle) {
18039   SmallVector<FormatStyle, 8> Styles;
18040   Styles.resize(2);
18041 
18042   Styles[0] = getGoogleStyle();
18043   Styles[1] = getLLVMStyle();
18044   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
18045   EXPECT_ALL_STYLES_EQUAL(Styles);
18046 
18047   Styles.resize(5);
18048   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
18049   Styles[1] = getLLVMStyle();
18050   Styles[1].Language = FormatStyle::LK_JavaScript;
18051   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
18052 
18053   Styles[2] = getLLVMStyle();
18054   Styles[2].Language = FormatStyle::LK_JavaScript;
18055   EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n"
18056                                   "BasedOnStyle: Google",
18057                                   &Styles[2])
18058                    .value());
18059 
18060   Styles[3] = getLLVMStyle();
18061   Styles[3].Language = FormatStyle::LK_JavaScript;
18062   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n"
18063                                   "Language: JavaScript",
18064                                   &Styles[3])
18065                    .value());
18066 
18067   Styles[4] = getLLVMStyle();
18068   Styles[4].Language = FormatStyle::LK_JavaScript;
18069   EXPECT_EQ(0, parseConfiguration("---\n"
18070                                   "BasedOnStyle: LLVM\n"
18071                                   "IndentWidth: 123\n"
18072                                   "---\n"
18073                                   "BasedOnStyle: Google\n"
18074                                   "Language: JavaScript",
18075                                   &Styles[4])
18076                    .value());
18077   EXPECT_ALL_STYLES_EQUAL(Styles);
18078 }
18079 
18080 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME)                             \
18081   Style.FIELD = false;                                                         \
18082   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value());      \
18083   EXPECT_TRUE(Style.FIELD);                                                    \
18084   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value());     \
18085   EXPECT_FALSE(Style.FIELD);
18086 
18087 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD)
18088 
18089 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME)              \
18090   Style.STRUCT.FIELD = false;                                                  \
18091   EXPECT_EQ(0,                                                                 \
18092             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": true", &Style)   \
18093                 .value());                                                     \
18094   EXPECT_TRUE(Style.STRUCT.FIELD);                                             \
18095   EXPECT_EQ(0,                                                                 \
18096             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": false", &Style)  \
18097                 .value());                                                     \
18098   EXPECT_FALSE(Style.STRUCT.FIELD);
18099 
18100 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD)                                 \
18101   CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD)
18102 
18103 #define CHECK_PARSE(TEXT, FIELD, VALUE)                                        \
18104   EXPECT_NE(VALUE, Style.FIELD) << "Initial value already the same!";          \
18105   EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value());                      \
18106   EXPECT_EQ(VALUE, Style.FIELD) << "Unexpected value after parsing!"
18107 
18108 TEST_F(FormatTest, ParsesConfigurationBools) {
18109   FormatStyle Style = {};
18110   Style.Language = FormatStyle::LK_Cpp;
18111   CHECK_PARSE_BOOL(AlignTrailingComments);
18112   CHECK_PARSE_BOOL(AllowAllArgumentsOnNextLine);
18113   CHECK_PARSE_BOOL(AllowAllConstructorInitializersOnNextLine);
18114   CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
18115   CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine);
18116   CHECK_PARSE_BOOL(AllowShortEnumsOnASingleLine);
18117   CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
18118   CHECK_PARSE_BOOL(BinPackArguments);
18119   CHECK_PARSE_BOOL(BinPackParameters);
18120   CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations);
18121   CHECK_PARSE_BOOL(BreakBeforeConceptDeclarations);
18122   CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
18123   CHECK_PARSE_BOOL(BreakStringLiterals);
18124   CHECK_PARSE_BOOL(CompactNamespaces);
18125   CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine);
18126   CHECK_PARSE_BOOL(DeriveLineEnding);
18127   CHECK_PARSE_BOOL(DerivePointerAlignment);
18128   CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding");
18129   CHECK_PARSE_BOOL(DisableFormat);
18130   CHECK_PARSE_BOOL(IndentAccessModifiers);
18131   CHECK_PARSE_BOOL(IndentCaseLabels);
18132   CHECK_PARSE_BOOL(IndentCaseBlocks);
18133   CHECK_PARSE_BOOL(IndentGotoLabels);
18134   CHECK_PARSE_BOOL(IndentRequires);
18135   CHECK_PARSE_BOOL(IndentWrappedFunctionNames);
18136   CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks);
18137   CHECK_PARSE_BOOL(ObjCSpaceAfterProperty);
18138   CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
18139   CHECK_PARSE_BOOL(Cpp11BracedListStyle);
18140   CHECK_PARSE_BOOL(ReflowComments);
18141   CHECK_PARSE_BOOL(SortUsingDeclarations);
18142   CHECK_PARSE_BOOL(SpacesInParentheses);
18143   CHECK_PARSE_BOOL(SpacesInSquareBrackets);
18144   CHECK_PARSE_BOOL(SpacesInConditionalStatement);
18145   CHECK_PARSE_BOOL(SpaceInEmptyBlock);
18146   CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
18147   CHECK_PARSE_BOOL(SpacesInContainerLiterals);
18148   CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
18149   CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
18150   CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword);
18151   CHECK_PARSE_BOOL(SpaceAfterLogicalNot);
18152   CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
18153   CHECK_PARSE_BOOL(SpaceBeforeCaseColon);
18154   CHECK_PARSE_BOOL(SpaceBeforeCpp11BracedList);
18155   CHECK_PARSE_BOOL(SpaceBeforeCtorInitializerColon);
18156   CHECK_PARSE_BOOL(SpaceBeforeInheritanceColon);
18157   CHECK_PARSE_BOOL(SpaceBeforeRangeBasedForLoopColon);
18158   CHECK_PARSE_BOOL(SpaceBeforeSquareBrackets);
18159   CHECK_PARSE_BOOL(UseCRLF);
18160 
18161   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterCaseLabel);
18162   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass);
18163   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum);
18164   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction);
18165   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace);
18166   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration);
18167   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct);
18168   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion);
18169   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterExternBlock);
18170   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch);
18171   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse);
18172   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeLambdaBody);
18173   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeWhile);
18174   CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces);
18175   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyFunction);
18176   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyRecord);
18177   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyNamespace);
18178 }
18179 
18180 #undef CHECK_PARSE_BOOL
18181 
18182 TEST_F(FormatTest, ParsesConfiguration) {
18183   FormatStyle Style = {};
18184   Style.Language = FormatStyle::LK_Cpp;
18185   CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
18186   CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",
18187               ConstructorInitializerIndentWidth, 1234u);
18188   CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u);
18189   CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
18190   CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
18191   CHECK_PARSE("PenaltyBreakAssignment: 1234", PenaltyBreakAssignment, 1234u);
18192   CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234",
18193               PenaltyBreakBeforeFirstCallParameter, 1234u);
18194   CHECK_PARSE("PenaltyBreakTemplateDeclaration: 1234",
18195               PenaltyBreakTemplateDeclaration, 1234u);
18196   CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
18197   CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
18198               PenaltyReturnTypeOnItsOwnLine, 1234u);
18199   CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
18200               SpacesBeforeTrailingComments, 1234u);
18201   CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
18202   CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u);
18203   CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$");
18204 
18205   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
18206   CHECK_PARSE("AlignConsecutiveAssignments: None", AlignConsecutiveAssignments,
18207               FormatStyle::ACS_None);
18208   CHECK_PARSE("AlignConsecutiveAssignments: Consecutive",
18209               AlignConsecutiveAssignments, FormatStyle::ACS_Consecutive);
18210   CHECK_PARSE("AlignConsecutiveAssignments: AcrossEmptyLines",
18211               AlignConsecutiveAssignments, FormatStyle::ACS_AcrossEmptyLines);
18212   CHECK_PARSE("AlignConsecutiveAssignments: AcrossEmptyLinesAndComments",
18213               AlignConsecutiveAssignments,
18214               FormatStyle::ACS_AcrossEmptyLinesAndComments);
18215   // For backwards compability, false / true should still parse
18216   CHECK_PARSE("AlignConsecutiveAssignments: false", AlignConsecutiveAssignments,
18217               FormatStyle::ACS_None);
18218   CHECK_PARSE("AlignConsecutiveAssignments: true", AlignConsecutiveAssignments,
18219               FormatStyle::ACS_Consecutive);
18220 
18221   Style.AlignConsecutiveBitFields = FormatStyle::ACS_Consecutive;
18222   CHECK_PARSE("AlignConsecutiveBitFields: None", AlignConsecutiveBitFields,
18223               FormatStyle::ACS_None);
18224   CHECK_PARSE("AlignConsecutiveBitFields: Consecutive",
18225               AlignConsecutiveBitFields, FormatStyle::ACS_Consecutive);
18226   CHECK_PARSE("AlignConsecutiveBitFields: AcrossEmptyLines",
18227               AlignConsecutiveBitFields, FormatStyle::ACS_AcrossEmptyLines);
18228   CHECK_PARSE("AlignConsecutiveBitFields: AcrossEmptyLinesAndComments",
18229               AlignConsecutiveBitFields,
18230               FormatStyle::ACS_AcrossEmptyLinesAndComments);
18231   // For backwards compability, false / true should still parse
18232   CHECK_PARSE("AlignConsecutiveBitFields: false", AlignConsecutiveBitFields,
18233               FormatStyle::ACS_None);
18234   CHECK_PARSE("AlignConsecutiveBitFields: true", AlignConsecutiveBitFields,
18235               FormatStyle::ACS_Consecutive);
18236 
18237   Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
18238   CHECK_PARSE("AlignConsecutiveMacros: None", AlignConsecutiveMacros,
18239               FormatStyle::ACS_None);
18240   CHECK_PARSE("AlignConsecutiveMacros: Consecutive", AlignConsecutiveMacros,
18241               FormatStyle::ACS_Consecutive);
18242   CHECK_PARSE("AlignConsecutiveMacros: AcrossEmptyLines",
18243               AlignConsecutiveMacros, FormatStyle::ACS_AcrossEmptyLines);
18244   CHECK_PARSE("AlignConsecutiveMacros: AcrossEmptyLinesAndComments",
18245               AlignConsecutiveMacros,
18246               FormatStyle::ACS_AcrossEmptyLinesAndComments);
18247   // For backwards compability, false / true should still parse
18248   CHECK_PARSE("AlignConsecutiveMacros: false", AlignConsecutiveMacros,
18249               FormatStyle::ACS_None);
18250   CHECK_PARSE("AlignConsecutiveMacros: true", AlignConsecutiveMacros,
18251               FormatStyle::ACS_Consecutive);
18252 
18253   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
18254   CHECK_PARSE("AlignConsecutiveDeclarations: None",
18255               AlignConsecutiveDeclarations, FormatStyle::ACS_None);
18256   CHECK_PARSE("AlignConsecutiveDeclarations: Consecutive",
18257               AlignConsecutiveDeclarations, FormatStyle::ACS_Consecutive);
18258   CHECK_PARSE("AlignConsecutiveDeclarations: AcrossEmptyLines",
18259               AlignConsecutiveDeclarations, FormatStyle::ACS_AcrossEmptyLines);
18260   CHECK_PARSE("AlignConsecutiveDeclarations: AcrossEmptyLinesAndComments",
18261               AlignConsecutiveDeclarations,
18262               FormatStyle::ACS_AcrossEmptyLinesAndComments);
18263   // For backwards compability, false / true should still parse
18264   CHECK_PARSE("AlignConsecutiveDeclarations: false",
18265               AlignConsecutiveDeclarations, FormatStyle::ACS_None);
18266   CHECK_PARSE("AlignConsecutiveDeclarations: true",
18267               AlignConsecutiveDeclarations, FormatStyle::ACS_Consecutive);
18268 
18269   Style.PointerAlignment = FormatStyle::PAS_Middle;
18270   CHECK_PARSE("PointerAlignment: Left", PointerAlignment,
18271               FormatStyle::PAS_Left);
18272   CHECK_PARSE("PointerAlignment: Right", PointerAlignment,
18273               FormatStyle::PAS_Right);
18274   CHECK_PARSE("PointerAlignment: Middle", PointerAlignment,
18275               FormatStyle::PAS_Middle);
18276   Style.ReferenceAlignment = FormatStyle::RAS_Middle;
18277   CHECK_PARSE("ReferenceAlignment: Pointer", ReferenceAlignment,
18278               FormatStyle::RAS_Pointer);
18279   CHECK_PARSE("ReferenceAlignment: Left", ReferenceAlignment,
18280               FormatStyle::RAS_Left);
18281   CHECK_PARSE("ReferenceAlignment: Right", ReferenceAlignment,
18282               FormatStyle::RAS_Right);
18283   CHECK_PARSE("ReferenceAlignment: Middle", ReferenceAlignment,
18284               FormatStyle::RAS_Middle);
18285   // For backward compatibility:
18286   CHECK_PARSE("PointerBindsToType: Left", PointerAlignment,
18287               FormatStyle::PAS_Left);
18288   CHECK_PARSE("PointerBindsToType: Right", PointerAlignment,
18289               FormatStyle::PAS_Right);
18290   CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment,
18291               FormatStyle::PAS_Middle);
18292 
18293   Style.Standard = FormatStyle::LS_Auto;
18294   CHECK_PARSE("Standard: c++03", Standard, FormatStyle::LS_Cpp03);
18295   CHECK_PARSE("Standard: c++11", Standard, FormatStyle::LS_Cpp11);
18296   CHECK_PARSE("Standard: c++14", Standard, FormatStyle::LS_Cpp14);
18297   CHECK_PARSE("Standard: c++17", Standard, FormatStyle::LS_Cpp17);
18298   CHECK_PARSE("Standard: c++20", Standard, FormatStyle::LS_Cpp20);
18299   CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
18300   CHECK_PARSE("Standard: Latest", Standard, FormatStyle::LS_Latest);
18301   // Legacy aliases:
18302   CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03);
18303   CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Latest);
18304   CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
18305   CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
18306 
18307   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
18308   CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment",
18309               BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment);
18310   CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators,
18311               FormatStyle::BOS_None);
18312   CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators,
18313               FormatStyle::BOS_All);
18314   // For backward compatibility:
18315   CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators,
18316               FormatStyle::BOS_None);
18317   CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators,
18318               FormatStyle::BOS_All);
18319 
18320   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
18321   CHECK_PARSE("BreakConstructorInitializers: BeforeComma",
18322               BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma);
18323   CHECK_PARSE("BreakConstructorInitializers: AfterColon",
18324               BreakConstructorInitializers, FormatStyle::BCIS_AfterColon);
18325   CHECK_PARSE("BreakConstructorInitializers: BeforeColon",
18326               BreakConstructorInitializers, FormatStyle::BCIS_BeforeColon);
18327   // For backward compatibility:
18328   CHECK_PARSE("BreakConstructorInitializersBeforeComma: true",
18329               BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma);
18330 
18331   Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
18332   CHECK_PARSE("BreakInheritanceList: AfterComma", BreakInheritanceList,
18333               FormatStyle::BILS_AfterComma);
18334   CHECK_PARSE("BreakInheritanceList: BeforeComma", BreakInheritanceList,
18335               FormatStyle::BILS_BeforeComma);
18336   CHECK_PARSE("BreakInheritanceList: AfterColon", BreakInheritanceList,
18337               FormatStyle::BILS_AfterColon);
18338   CHECK_PARSE("BreakInheritanceList: BeforeColon", BreakInheritanceList,
18339               FormatStyle::BILS_BeforeColon);
18340   // For backward compatibility:
18341   CHECK_PARSE("BreakBeforeInheritanceComma: true", BreakInheritanceList,
18342               FormatStyle::BILS_BeforeComma);
18343 
18344   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
18345   CHECK_PARSE("EmptyLineBeforeAccessModifier: Never",
18346               EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Never);
18347   CHECK_PARSE("EmptyLineBeforeAccessModifier: Leave",
18348               EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Leave);
18349   CHECK_PARSE("EmptyLineBeforeAccessModifier: LogicalBlock",
18350               EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_LogicalBlock);
18351   CHECK_PARSE("EmptyLineBeforeAccessModifier: Always",
18352               EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Always);
18353 
18354   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
18355   CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket,
18356               FormatStyle::BAS_Align);
18357   CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket,
18358               FormatStyle::BAS_DontAlign);
18359   CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket,
18360               FormatStyle::BAS_AlwaysBreak);
18361   // For backward compatibility:
18362   CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket,
18363               FormatStyle::BAS_DontAlign);
18364   CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket,
18365               FormatStyle::BAS_Align);
18366 
18367   Style.AlignEscapedNewlines = FormatStyle::ENAS_Left;
18368   CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines,
18369               FormatStyle::ENAS_DontAlign);
18370   CHECK_PARSE("AlignEscapedNewlines: Left", AlignEscapedNewlines,
18371               FormatStyle::ENAS_Left);
18372   CHECK_PARSE("AlignEscapedNewlines: Right", AlignEscapedNewlines,
18373               FormatStyle::ENAS_Right);
18374   // For backward compatibility:
18375   CHECK_PARSE("AlignEscapedNewlinesLeft: true", AlignEscapedNewlines,
18376               FormatStyle::ENAS_Left);
18377   CHECK_PARSE("AlignEscapedNewlinesLeft: false", AlignEscapedNewlines,
18378               FormatStyle::ENAS_Right);
18379 
18380   Style.AlignOperands = FormatStyle::OAS_Align;
18381   CHECK_PARSE("AlignOperands: DontAlign", AlignOperands,
18382               FormatStyle::OAS_DontAlign);
18383   CHECK_PARSE("AlignOperands: Align", AlignOperands, FormatStyle::OAS_Align);
18384   CHECK_PARSE("AlignOperands: AlignAfterOperator", AlignOperands,
18385               FormatStyle::OAS_AlignAfterOperator);
18386   // For backward compatibility:
18387   CHECK_PARSE("AlignOperands: false", AlignOperands,
18388               FormatStyle::OAS_DontAlign);
18389   CHECK_PARSE("AlignOperands: true", AlignOperands, FormatStyle::OAS_Align);
18390 
18391   Style.UseTab = FormatStyle::UT_ForIndentation;
18392   CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
18393   CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
18394   CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
18395   CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab,
18396               FormatStyle::UT_ForContinuationAndIndentation);
18397   CHECK_PARSE("UseTab: AlignWithSpaces", UseTab,
18398               FormatStyle::UT_AlignWithSpaces);
18399   // For backward compatibility:
18400   CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never);
18401   CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always);
18402 
18403   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty;
18404   CHECK_PARSE("AllowShortBlocksOnASingleLine: Never",
18405               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never);
18406   CHECK_PARSE("AllowShortBlocksOnASingleLine: Empty",
18407               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Empty);
18408   CHECK_PARSE("AllowShortBlocksOnASingleLine: Always",
18409               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always);
18410   // For backward compatibility:
18411   CHECK_PARSE("AllowShortBlocksOnASingleLine: false",
18412               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never);
18413   CHECK_PARSE("AllowShortBlocksOnASingleLine: true",
18414               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always);
18415 
18416   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
18417   CHECK_PARSE("AllowShortFunctionsOnASingleLine: None",
18418               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
18419   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline",
18420               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline);
18421   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty",
18422               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty);
18423   CHECK_PARSE("AllowShortFunctionsOnASingleLine: All",
18424               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
18425   // For backward compatibility:
18426   CHECK_PARSE("AllowShortFunctionsOnASingleLine: false",
18427               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
18428   CHECK_PARSE("AllowShortFunctionsOnASingleLine: true",
18429               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
18430 
18431   Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Both;
18432   CHECK_PARSE("SpaceAroundPointerQualifiers: Default",
18433               SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Default);
18434   CHECK_PARSE("SpaceAroundPointerQualifiers: Before",
18435               SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Before);
18436   CHECK_PARSE("SpaceAroundPointerQualifiers: After",
18437               SpaceAroundPointerQualifiers, FormatStyle::SAPQ_After);
18438   CHECK_PARSE("SpaceAroundPointerQualifiers: Both",
18439               SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Both);
18440 
18441   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
18442   CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens,
18443               FormatStyle::SBPO_Never);
18444   CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens,
18445               FormatStyle::SBPO_Always);
18446   CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens,
18447               FormatStyle::SBPO_ControlStatements);
18448   CHECK_PARSE("SpaceBeforeParens: ControlStatementsExceptControlMacros",
18449               SpaceBeforeParens,
18450               FormatStyle::SBPO_ControlStatementsExceptControlMacros);
18451   CHECK_PARSE("SpaceBeforeParens: NonEmptyParentheses", SpaceBeforeParens,
18452               FormatStyle::SBPO_NonEmptyParentheses);
18453   // For backward compatibility:
18454   CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens,
18455               FormatStyle::SBPO_Never);
18456   CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens,
18457               FormatStyle::SBPO_ControlStatements);
18458   CHECK_PARSE("SpaceBeforeParens: ControlStatementsExceptForEachMacros",
18459               SpaceBeforeParens,
18460               FormatStyle::SBPO_ControlStatementsExceptControlMacros);
18461 
18462   Style.ColumnLimit = 123;
18463   FormatStyle BaseStyle = getLLVMStyle();
18464   CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
18465   CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
18466 
18467   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
18468   CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
18469               FormatStyle::BS_Attach);
18470   CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
18471               FormatStyle::BS_Linux);
18472   CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces,
18473               FormatStyle::BS_Mozilla);
18474   CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
18475               FormatStyle::BS_Stroustrup);
18476   CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces,
18477               FormatStyle::BS_Allman);
18478   CHECK_PARSE("BreakBeforeBraces: Whitesmiths", BreakBeforeBraces,
18479               FormatStyle::BS_Whitesmiths);
18480   CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU);
18481   CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces,
18482               FormatStyle::BS_WebKit);
18483   CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces,
18484               FormatStyle::BS_Custom);
18485 
18486   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never;
18487   CHECK_PARSE("BraceWrapping:\n"
18488               "  AfterControlStatement: MultiLine",
18489               BraceWrapping.AfterControlStatement,
18490               FormatStyle::BWACS_MultiLine);
18491   CHECK_PARSE("BraceWrapping:\n"
18492               "  AfterControlStatement: Always",
18493               BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always);
18494   CHECK_PARSE("BraceWrapping:\n"
18495               "  AfterControlStatement: Never",
18496               BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never);
18497   // For backward compatibility:
18498   CHECK_PARSE("BraceWrapping:\n"
18499               "  AfterControlStatement: true",
18500               BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always);
18501   CHECK_PARSE("BraceWrapping:\n"
18502               "  AfterControlStatement: false",
18503               BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never);
18504 
18505   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
18506   CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType,
18507               FormatStyle::RTBS_None);
18508   CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType,
18509               FormatStyle::RTBS_All);
18510   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel",
18511               AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel);
18512   CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions",
18513               AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions);
18514   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions",
18515               AlwaysBreakAfterReturnType,
18516               FormatStyle::RTBS_TopLevelDefinitions);
18517 
18518   Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
18519   CHECK_PARSE("AlwaysBreakTemplateDeclarations: No",
18520               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_No);
18521   CHECK_PARSE("AlwaysBreakTemplateDeclarations: MultiLine",
18522               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine);
18523   CHECK_PARSE("AlwaysBreakTemplateDeclarations: Yes",
18524               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes);
18525   CHECK_PARSE("AlwaysBreakTemplateDeclarations: false",
18526               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine);
18527   CHECK_PARSE("AlwaysBreakTemplateDeclarations: true",
18528               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes);
18529 
18530   Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
18531   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None",
18532               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None);
18533   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All",
18534               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All);
18535   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel",
18536               AlwaysBreakAfterDefinitionReturnType,
18537               FormatStyle::DRTBS_TopLevel);
18538 
18539   Style.NamespaceIndentation = FormatStyle::NI_All;
18540   CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
18541               FormatStyle::NI_None);
18542   CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation,
18543               FormatStyle::NI_Inner);
18544   CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
18545               FormatStyle::NI_All);
18546 
18547   Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_OnlyFirstIf;
18548   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Never",
18549               AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never);
18550   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: WithoutElse",
18551               AllowShortIfStatementsOnASingleLine,
18552               FormatStyle::SIS_WithoutElse);
18553   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: OnlyFirstIf",
18554               AllowShortIfStatementsOnASingleLine,
18555               FormatStyle::SIS_OnlyFirstIf);
18556   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: AllIfsAndElse",
18557               AllowShortIfStatementsOnASingleLine,
18558               FormatStyle::SIS_AllIfsAndElse);
18559   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Always",
18560               AllowShortIfStatementsOnASingleLine,
18561               FormatStyle::SIS_OnlyFirstIf);
18562   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: false",
18563               AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never);
18564   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: true",
18565               AllowShortIfStatementsOnASingleLine,
18566               FormatStyle::SIS_WithoutElse);
18567 
18568   Style.IndentExternBlock = FormatStyle::IEBS_NoIndent;
18569   CHECK_PARSE("IndentExternBlock: AfterExternBlock", IndentExternBlock,
18570               FormatStyle::IEBS_AfterExternBlock);
18571   CHECK_PARSE("IndentExternBlock: Indent", IndentExternBlock,
18572               FormatStyle::IEBS_Indent);
18573   CHECK_PARSE("IndentExternBlock: NoIndent", IndentExternBlock,
18574               FormatStyle::IEBS_NoIndent);
18575   CHECK_PARSE("IndentExternBlock: true", IndentExternBlock,
18576               FormatStyle::IEBS_Indent);
18577   CHECK_PARSE("IndentExternBlock: false", IndentExternBlock,
18578               FormatStyle::IEBS_NoIndent);
18579 
18580   Style.BitFieldColonSpacing = FormatStyle::BFCS_None;
18581   CHECK_PARSE("BitFieldColonSpacing: Both", BitFieldColonSpacing,
18582               FormatStyle::BFCS_Both);
18583   CHECK_PARSE("BitFieldColonSpacing: None", BitFieldColonSpacing,
18584               FormatStyle::BFCS_None);
18585   CHECK_PARSE("BitFieldColonSpacing: Before", BitFieldColonSpacing,
18586               FormatStyle::BFCS_Before);
18587   CHECK_PARSE("BitFieldColonSpacing: After", BitFieldColonSpacing,
18588               FormatStyle::BFCS_After);
18589 
18590   Style.SortJavaStaticImport = FormatStyle::SJSIO_Before;
18591   CHECK_PARSE("SortJavaStaticImport: After", SortJavaStaticImport,
18592               FormatStyle::SJSIO_After);
18593   CHECK_PARSE("SortJavaStaticImport: Before", SortJavaStaticImport,
18594               FormatStyle::SJSIO_Before);
18595 
18596   // FIXME: This is required because parsing a configuration simply overwrites
18597   // the first N elements of the list instead of resetting it.
18598   Style.ForEachMacros.clear();
18599   std::vector<std::string> BoostForeach;
18600   BoostForeach.push_back("BOOST_FOREACH");
18601   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach);
18602   std::vector<std::string> BoostAndQForeach;
18603   BoostAndQForeach.push_back("BOOST_FOREACH");
18604   BoostAndQForeach.push_back("Q_FOREACH");
18605   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
18606               BoostAndQForeach);
18607 
18608   Style.IfMacros.clear();
18609   std::vector<std::string> CustomIfs;
18610   CustomIfs.push_back("MYIF");
18611   CHECK_PARSE("IfMacros: [MYIF]", IfMacros, CustomIfs);
18612 
18613   Style.AttributeMacros.clear();
18614   CHECK_PARSE("BasedOnStyle: LLVM", AttributeMacros,
18615               std::vector<std::string>{"__capability"});
18616   CHECK_PARSE("AttributeMacros: [attr1, attr2]", AttributeMacros,
18617               std::vector<std::string>({"attr1", "attr2"}));
18618 
18619   Style.StatementAttributeLikeMacros.clear();
18620   CHECK_PARSE("StatementAttributeLikeMacros: [emit,Q_EMIT]",
18621               StatementAttributeLikeMacros,
18622               std::vector<std::string>({"emit", "Q_EMIT"}));
18623 
18624   Style.StatementMacros.clear();
18625   CHECK_PARSE("StatementMacros: [QUNUSED]", StatementMacros,
18626               std::vector<std::string>{"QUNUSED"});
18627   CHECK_PARSE("StatementMacros: [QUNUSED, QT_REQUIRE_VERSION]", StatementMacros,
18628               std::vector<std::string>({"QUNUSED", "QT_REQUIRE_VERSION"}));
18629 
18630   Style.NamespaceMacros.clear();
18631   CHECK_PARSE("NamespaceMacros: [TESTSUITE]", NamespaceMacros,
18632               std::vector<std::string>{"TESTSUITE"});
18633   CHECK_PARSE("NamespaceMacros: [TESTSUITE, SUITE]", NamespaceMacros,
18634               std::vector<std::string>({"TESTSUITE", "SUITE"}));
18635 
18636   Style.WhitespaceSensitiveMacros.clear();
18637   CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE]",
18638               WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"});
18639   CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE, ASSERT]",
18640               WhitespaceSensitiveMacros,
18641               std::vector<std::string>({"STRINGIZE", "ASSERT"}));
18642   Style.WhitespaceSensitiveMacros.clear();
18643   CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE']",
18644               WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"});
18645   CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE', 'ASSERT']",
18646               WhitespaceSensitiveMacros,
18647               std::vector<std::string>({"STRINGIZE", "ASSERT"}));
18648 
18649   Style.IncludeStyle.IncludeCategories.clear();
18650   std::vector<tooling::IncludeStyle::IncludeCategory> ExpectedCategories = {
18651       {"abc/.*", 2, 0, false}, {".*", 1, 0, true}};
18652   CHECK_PARSE("IncludeCategories:\n"
18653               "  - Regex: abc/.*\n"
18654               "    Priority: 2\n"
18655               "  - Regex: .*\n"
18656               "    Priority: 1\n"
18657               "    CaseSensitive: true\n",
18658               IncludeStyle.IncludeCategories, ExpectedCategories);
18659   CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeStyle.IncludeIsMainRegex,
18660               "abc$");
18661   CHECK_PARSE("IncludeIsMainSourceRegex: 'abc$'",
18662               IncludeStyle.IncludeIsMainSourceRegex, "abc$");
18663 
18664   Style.SortIncludes = FormatStyle::SI_Never;
18665   CHECK_PARSE("SortIncludes: true", SortIncludes,
18666               FormatStyle::SI_CaseSensitive);
18667   CHECK_PARSE("SortIncludes: false", SortIncludes, FormatStyle::SI_Never);
18668   CHECK_PARSE("SortIncludes: CaseInsensitive", SortIncludes,
18669               FormatStyle::SI_CaseInsensitive);
18670   CHECK_PARSE("SortIncludes: CaseSensitive", SortIncludes,
18671               FormatStyle::SI_CaseSensitive);
18672   CHECK_PARSE("SortIncludes: Never", SortIncludes, FormatStyle::SI_Never);
18673 
18674   Style.RawStringFormats.clear();
18675   std::vector<FormatStyle::RawStringFormat> ExpectedRawStringFormats = {
18676       {
18677           FormatStyle::LK_TextProto,
18678           {"pb", "proto"},
18679           {"PARSE_TEXT_PROTO"},
18680           /*CanonicalDelimiter=*/"",
18681           "llvm",
18682       },
18683       {
18684           FormatStyle::LK_Cpp,
18685           {"cc", "cpp"},
18686           {"C_CODEBLOCK", "CPPEVAL"},
18687           /*CanonicalDelimiter=*/"cc",
18688           /*BasedOnStyle=*/"",
18689       },
18690   };
18691 
18692   CHECK_PARSE("RawStringFormats:\n"
18693               "  - Language: TextProto\n"
18694               "    Delimiters:\n"
18695               "      - 'pb'\n"
18696               "      - 'proto'\n"
18697               "    EnclosingFunctions:\n"
18698               "      - 'PARSE_TEXT_PROTO'\n"
18699               "    BasedOnStyle: llvm\n"
18700               "  - Language: Cpp\n"
18701               "    Delimiters:\n"
18702               "      - 'cc'\n"
18703               "      - 'cpp'\n"
18704               "    EnclosingFunctions:\n"
18705               "      - 'C_CODEBLOCK'\n"
18706               "      - 'CPPEVAL'\n"
18707               "    CanonicalDelimiter: 'cc'",
18708               RawStringFormats, ExpectedRawStringFormats);
18709 
18710   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
18711               "  Minimum: 0\n"
18712               "  Maximum: 0",
18713               SpacesInLineCommentPrefix.Minimum, 0u);
18714   EXPECT_EQ(Style.SpacesInLineCommentPrefix.Maximum, 0u);
18715   Style.SpacesInLineCommentPrefix.Minimum = 1;
18716   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
18717               "  Minimum: 2",
18718               SpacesInLineCommentPrefix.Minimum, 0u);
18719   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
18720               "  Maximum: -1",
18721               SpacesInLineCommentPrefix.Maximum, -1u);
18722   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
18723               "  Minimum: 2",
18724               SpacesInLineCommentPrefix.Minimum, 2u);
18725   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
18726               "  Maximum: 1",
18727               SpacesInLineCommentPrefix.Maximum, 1u);
18728   EXPECT_EQ(Style.SpacesInLineCommentPrefix.Minimum, 1u);
18729 
18730   Style.SpacesInAngles = FormatStyle::SIAS_Always;
18731   CHECK_PARSE("SpacesInAngles: Never", SpacesInAngles, FormatStyle::SIAS_Never);
18732   CHECK_PARSE("SpacesInAngles: Always", SpacesInAngles,
18733               FormatStyle::SIAS_Always);
18734   CHECK_PARSE("SpacesInAngles: Leave", SpacesInAngles, FormatStyle::SIAS_Leave);
18735   // For backward compatibility:
18736   CHECK_PARSE("SpacesInAngles: false", SpacesInAngles, FormatStyle::SIAS_Never);
18737   CHECK_PARSE("SpacesInAngles: true", SpacesInAngles, FormatStyle::SIAS_Always);
18738 }
18739 
18740 TEST_F(FormatTest, ParsesConfigurationWithLanguages) {
18741   FormatStyle Style = {};
18742   Style.Language = FormatStyle::LK_Cpp;
18743   CHECK_PARSE("Language: Cpp\n"
18744               "IndentWidth: 12",
18745               IndentWidth, 12u);
18746   EXPECT_EQ(parseConfiguration("Language: JavaScript\n"
18747                                "IndentWidth: 34",
18748                                &Style),
18749             ParseError::Unsuitable);
18750   FormatStyle BinPackedTCS = {};
18751   BinPackedTCS.Language = FormatStyle::LK_JavaScript;
18752   EXPECT_EQ(parseConfiguration("BinPackArguments: true\n"
18753                                "InsertTrailingCommas: Wrapped",
18754                                &BinPackedTCS),
18755             ParseError::BinPackTrailingCommaConflict);
18756   EXPECT_EQ(12u, Style.IndentWidth);
18757   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
18758   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
18759 
18760   Style.Language = FormatStyle::LK_JavaScript;
18761   CHECK_PARSE("Language: JavaScript\n"
18762               "IndentWidth: 12",
18763               IndentWidth, 12u);
18764   CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u);
18765   EXPECT_EQ(parseConfiguration("Language: Cpp\n"
18766                                "IndentWidth: 34",
18767                                &Style),
18768             ParseError::Unsuitable);
18769   EXPECT_EQ(23u, Style.IndentWidth);
18770   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
18771   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
18772 
18773   CHECK_PARSE("BasedOnStyle: LLVM\n"
18774               "IndentWidth: 67",
18775               IndentWidth, 67u);
18776 
18777   CHECK_PARSE("---\n"
18778               "Language: JavaScript\n"
18779               "IndentWidth: 12\n"
18780               "---\n"
18781               "Language: Cpp\n"
18782               "IndentWidth: 34\n"
18783               "...\n",
18784               IndentWidth, 12u);
18785 
18786   Style.Language = FormatStyle::LK_Cpp;
18787   CHECK_PARSE("---\n"
18788               "Language: JavaScript\n"
18789               "IndentWidth: 12\n"
18790               "---\n"
18791               "Language: Cpp\n"
18792               "IndentWidth: 34\n"
18793               "...\n",
18794               IndentWidth, 34u);
18795   CHECK_PARSE("---\n"
18796               "IndentWidth: 78\n"
18797               "---\n"
18798               "Language: JavaScript\n"
18799               "IndentWidth: 56\n"
18800               "...\n",
18801               IndentWidth, 78u);
18802 
18803   Style.ColumnLimit = 123;
18804   Style.IndentWidth = 234;
18805   Style.BreakBeforeBraces = FormatStyle::BS_Linux;
18806   Style.TabWidth = 345;
18807   EXPECT_FALSE(parseConfiguration("---\n"
18808                                   "IndentWidth: 456\n"
18809                                   "BreakBeforeBraces: Allman\n"
18810                                   "---\n"
18811                                   "Language: JavaScript\n"
18812                                   "IndentWidth: 111\n"
18813                                   "TabWidth: 111\n"
18814                                   "---\n"
18815                                   "Language: Cpp\n"
18816                                   "BreakBeforeBraces: Stroustrup\n"
18817                                   "TabWidth: 789\n"
18818                                   "...\n",
18819                                   &Style));
18820   EXPECT_EQ(123u, Style.ColumnLimit);
18821   EXPECT_EQ(456u, Style.IndentWidth);
18822   EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces);
18823   EXPECT_EQ(789u, Style.TabWidth);
18824 
18825   EXPECT_EQ(parseConfiguration("---\n"
18826                                "Language: JavaScript\n"
18827                                "IndentWidth: 56\n"
18828                                "---\n"
18829                                "IndentWidth: 78\n"
18830                                "...\n",
18831                                &Style),
18832             ParseError::Error);
18833   EXPECT_EQ(parseConfiguration("---\n"
18834                                "Language: JavaScript\n"
18835                                "IndentWidth: 56\n"
18836                                "---\n"
18837                                "Language: JavaScript\n"
18838                                "IndentWidth: 78\n"
18839                                "...\n",
18840                                &Style),
18841             ParseError::Error);
18842 
18843   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
18844 }
18845 
18846 #undef CHECK_PARSE
18847 
18848 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) {
18849   FormatStyle Style = {};
18850   Style.Language = FormatStyle::LK_JavaScript;
18851   Style.BreakBeforeTernaryOperators = true;
18852   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value());
18853   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
18854 
18855   Style.BreakBeforeTernaryOperators = true;
18856   EXPECT_EQ(0, parseConfiguration("---\n"
18857                                   "BasedOnStyle: Google\n"
18858                                   "---\n"
18859                                   "Language: JavaScript\n"
18860                                   "IndentWidth: 76\n"
18861                                   "...\n",
18862                                   &Style)
18863                    .value());
18864   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
18865   EXPECT_EQ(76u, Style.IndentWidth);
18866   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
18867 }
18868 
18869 TEST_F(FormatTest, ConfigurationRoundTripTest) {
18870   FormatStyle Style = getLLVMStyle();
18871   std::string YAML = configurationAsText(Style);
18872   FormatStyle ParsedStyle = {};
18873   ParsedStyle.Language = FormatStyle::LK_Cpp;
18874   EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
18875   EXPECT_EQ(Style, ParsedStyle);
18876 }
18877 
18878 TEST_F(FormatTest, WorksFor8bitEncodings) {
18879   EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
18880             "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
18881             "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
18882             "\"\xef\xee\xf0\xf3...\"",
18883             format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
18884                    "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
18885                    "\xef\xee\xf0\xf3...\"",
18886                    getLLVMStyleWithColumns(12)));
18887 }
18888 
18889 TEST_F(FormatTest, HandlesUTF8BOM) {
18890   EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf"));
18891   EXPECT_EQ("\xef\xbb\xbf#include <iostream>",
18892             format("\xef\xbb\xbf#include <iostream>"));
18893   EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>",
18894             format("\xef\xbb\xbf\n#include <iostream>"));
18895 }
18896 
18897 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
18898 #if !defined(_MSC_VER)
18899 
18900 TEST_F(FormatTest, CountsUTF8CharactersProperly) {
18901   verifyFormat("\"Однажды в студёную зимнюю пору...\"",
18902                getLLVMStyleWithColumns(35));
18903   verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
18904                getLLVMStyleWithColumns(31));
18905   verifyFormat("// Однажды в студёную зимнюю пору...",
18906                getLLVMStyleWithColumns(36));
18907   verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32));
18908   verifyFormat("/* Однажды в студёную зимнюю пору... */",
18909                getLLVMStyleWithColumns(39));
18910   verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
18911                getLLVMStyleWithColumns(35));
18912 }
18913 
18914 TEST_F(FormatTest, SplitsUTF8Strings) {
18915   // Non-printable characters' width is currently considered to be the length in
18916   // bytes in UTF8. The characters can be displayed in very different manner
18917   // (zero-width, single width with a substitution glyph, expanded to their code
18918   // (e.g. "<8d>"), so there's no single correct way to handle them.
18919   EXPECT_EQ("\"aaaaÄ\"\n"
18920             "\"\xc2\x8d\";",
18921             format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
18922   EXPECT_EQ("\"aaaaaaaÄ\"\n"
18923             "\"\xc2\x8d\";",
18924             format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
18925   EXPECT_EQ("\"Однажды, в \"\n"
18926             "\"студёную \"\n"
18927             "\"зимнюю \"\n"
18928             "\"пору,\"",
18929             format("\"Однажды, в студёную зимнюю пору,\"",
18930                    getLLVMStyleWithColumns(13)));
18931   EXPECT_EQ(
18932       "\"一 二 三 \"\n"
18933       "\"四 五六 \"\n"
18934       "\"七 八 九 \"\n"
18935       "\"十\"",
18936       format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11)));
18937   EXPECT_EQ("\"一\t\"\n"
18938             "\"二 \t\"\n"
18939             "\"三 四 \"\n"
18940             "\"五\t\"\n"
18941             "\"六 \t\"\n"
18942             "\"七 \"\n"
18943             "\"八九十\tqq\"",
18944             format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
18945                    getLLVMStyleWithColumns(11)));
18946 
18947   // UTF8 character in an escape sequence.
18948   EXPECT_EQ("\"aaaaaa\"\n"
18949             "\"\\\xC2\x8D\"",
18950             format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10)));
18951 }
18952 
18953 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
18954   EXPECT_EQ("const char *sssss =\n"
18955             "    \"一二三四五六七八\\\n"
18956             " 九 十\";",
18957             format("const char *sssss = \"一二三四五六七八\\\n"
18958                    " 九 十\";",
18959                    getLLVMStyleWithColumns(30)));
18960 }
18961 
18962 TEST_F(FormatTest, SplitsUTF8LineComments) {
18963   EXPECT_EQ("// aaaaÄ\xc2\x8d",
18964             format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10)));
18965   EXPECT_EQ("// Я из лесу\n"
18966             "// вышел; был\n"
18967             "// сильный\n"
18968             "// мороз.",
18969             format("// Я из лесу вышел; был сильный мороз.",
18970                    getLLVMStyleWithColumns(13)));
18971   EXPECT_EQ("// 一二三\n"
18972             "// 四五六七\n"
18973             "// 八  九\n"
18974             "// 十",
18975             format("// 一二三 四五六七 八  九 十", getLLVMStyleWithColumns(9)));
18976 }
18977 
18978 TEST_F(FormatTest, SplitsUTF8BlockComments) {
18979   EXPECT_EQ("/* Гляжу,\n"
18980             " * поднимается\n"
18981             " * медленно в\n"
18982             " * гору\n"
18983             " * Лошадка,\n"
18984             " * везущая\n"
18985             " * хворосту\n"
18986             " * воз. */",
18987             format("/* Гляжу, поднимается медленно в гору\n"
18988                    " * Лошадка, везущая хворосту воз. */",
18989                    getLLVMStyleWithColumns(13)));
18990   EXPECT_EQ(
18991       "/* 一二三\n"
18992       " * 四五六七\n"
18993       " * 八  九\n"
18994       " * 十  */",
18995       format("/* 一二三 四五六七 八  九 十  */", getLLVMStyleWithColumns(9)));
18996   EXPECT_EQ("/* �������� ��������\n"
18997             " * ��������\n"
18998             " * ������-�� */",
18999             format("/* �������� �������� �������� ������-�� */", getLLVMStyleWithColumns(12)));
19000 }
19001 
19002 #endif // _MSC_VER
19003 
19004 TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
19005   FormatStyle Style = getLLVMStyle();
19006 
19007   Style.ConstructorInitializerIndentWidth = 4;
19008   verifyFormat(
19009       "SomeClass::Constructor()\n"
19010       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
19011       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
19012       Style);
19013 
19014   Style.ConstructorInitializerIndentWidth = 2;
19015   verifyFormat(
19016       "SomeClass::Constructor()\n"
19017       "  : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
19018       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
19019       Style);
19020 
19021   Style.ConstructorInitializerIndentWidth = 0;
19022   verifyFormat(
19023       "SomeClass::Constructor()\n"
19024       ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
19025       "  aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
19026       Style);
19027   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
19028   verifyFormat(
19029       "SomeLongTemplateVariableName<\n"
19030       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>",
19031       Style);
19032   verifyFormat("bool smaller = 1 < "
19033                "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
19034                "                       "
19035                "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
19036                Style);
19037 
19038   Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
19039   verifyFormat("SomeClass::Constructor() :\n"
19040                "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa),\n"
19041                "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa) {}",
19042                Style);
19043 }
19044 
19045 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {
19046   FormatStyle Style = getLLVMStyle();
19047   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
19048   Style.ConstructorInitializerIndentWidth = 4;
19049   verifyFormat("SomeClass::Constructor()\n"
19050                "    : a(a)\n"
19051                "    , b(b)\n"
19052                "    , c(c) {}",
19053                Style);
19054   verifyFormat("SomeClass::Constructor()\n"
19055                "    : a(a) {}",
19056                Style);
19057 
19058   Style.ColumnLimit = 0;
19059   verifyFormat("SomeClass::Constructor()\n"
19060                "    : a(a) {}",
19061                Style);
19062   verifyFormat("SomeClass::Constructor() noexcept\n"
19063                "    : a(a) {}",
19064                Style);
19065   verifyFormat("SomeClass::Constructor()\n"
19066                "    : a(a)\n"
19067                "    , b(b)\n"
19068                "    , c(c) {}",
19069                Style);
19070   verifyFormat("SomeClass::Constructor()\n"
19071                "    : a(a) {\n"
19072                "  foo();\n"
19073                "  bar();\n"
19074                "}",
19075                Style);
19076 
19077   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
19078   verifyFormat("SomeClass::Constructor()\n"
19079                "    : a(a)\n"
19080                "    , b(b)\n"
19081                "    , c(c) {\n}",
19082                Style);
19083   verifyFormat("SomeClass::Constructor()\n"
19084                "    : a(a) {\n}",
19085                Style);
19086 
19087   Style.ColumnLimit = 80;
19088   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
19089   Style.ConstructorInitializerIndentWidth = 2;
19090   verifyFormat("SomeClass::Constructor()\n"
19091                "  : a(a)\n"
19092                "  , b(b)\n"
19093                "  , c(c) {}",
19094                Style);
19095 
19096   Style.ConstructorInitializerIndentWidth = 0;
19097   verifyFormat("SomeClass::Constructor()\n"
19098                ": a(a)\n"
19099                ", b(b)\n"
19100                ", c(c) {}",
19101                Style);
19102 
19103   Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
19104   Style.ConstructorInitializerIndentWidth = 4;
19105   verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
19106   verifyFormat(
19107       "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n",
19108       Style);
19109   verifyFormat(
19110       "SomeClass::Constructor()\n"
19111       "    : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
19112       Style);
19113   Style.ConstructorInitializerIndentWidth = 4;
19114   Style.ColumnLimit = 60;
19115   verifyFormat("SomeClass::Constructor()\n"
19116                "    : aaaaaaaa(aaaaaaaa)\n"
19117                "    , aaaaaaaa(aaaaaaaa)\n"
19118                "    , aaaaaaaa(aaaaaaaa) {}",
19119                Style);
19120 }
19121 
19122 TEST_F(FormatTest, Destructors) {
19123   verifyFormat("void F(int &i) { i.~int(); }");
19124   verifyFormat("void F(int &i) { i->~int(); }");
19125 }
19126 
19127 TEST_F(FormatTest, FormatsWithWebKitStyle) {
19128   FormatStyle Style = getWebKitStyle();
19129 
19130   // Don't indent in outer namespaces.
19131   verifyFormat("namespace outer {\n"
19132                "int i;\n"
19133                "namespace inner {\n"
19134                "    int i;\n"
19135                "} // namespace inner\n"
19136                "} // namespace outer\n"
19137                "namespace other_outer {\n"
19138                "int i;\n"
19139                "}",
19140                Style);
19141 
19142   // Don't indent case labels.
19143   verifyFormat("switch (variable) {\n"
19144                "case 1:\n"
19145                "case 2:\n"
19146                "    doSomething();\n"
19147                "    break;\n"
19148                "default:\n"
19149                "    ++variable;\n"
19150                "}",
19151                Style);
19152 
19153   // Wrap before binary operators.
19154   EXPECT_EQ("void f()\n"
19155             "{\n"
19156             "    if (aaaaaaaaaaaaaaaa\n"
19157             "        && bbbbbbbbbbbbbbbbbbbbbbbb\n"
19158             "        && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
19159             "        return;\n"
19160             "}",
19161             format("void f() {\n"
19162                    "if (aaaaaaaaaaaaaaaa\n"
19163                    "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
19164                    "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
19165                    "return;\n"
19166                    "}",
19167                    Style));
19168 
19169   // Allow functions on a single line.
19170   verifyFormat("void f() { return; }", Style);
19171 
19172   // Allow empty blocks on a single line and insert a space in empty blocks.
19173   EXPECT_EQ("void f() { }", format("void f() {}", Style));
19174   EXPECT_EQ("while (true) { }", format("while (true) {}", Style));
19175   // However, don't merge non-empty short loops.
19176   EXPECT_EQ("while (true) {\n"
19177             "    continue;\n"
19178             "}",
19179             format("while (true) { continue; }", Style));
19180 
19181   // Constructor initializers are formatted one per line with the "," on the
19182   // new line.
19183   verifyFormat("Constructor()\n"
19184                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
19185                "    , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
19186                "          aaaaaaaaaaaaaa)\n"
19187                "    , aaaaaaaaaaaaaaaaaaaaaaa()\n"
19188                "{\n"
19189                "}",
19190                Style);
19191   verifyFormat("SomeClass::Constructor()\n"
19192                "    : a(a)\n"
19193                "{\n"
19194                "}",
19195                Style);
19196   EXPECT_EQ("SomeClass::Constructor()\n"
19197             "    : a(a)\n"
19198             "{\n"
19199             "}",
19200             format("SomeClass::Constructor():a(a){}", Style));
19201   verifyFormat("SomeClass::Constructor()\n"
19202                "    : a(a)\n"
19203                "    , b(b)\n"
19204                "    , c(c)\n"
19205                "{\n"
19206                "}",
19207                Style);
19208   verifyFormat("SomeClass::Constructor()\n"
19209                "    : a(a)\n"
19210                "{\n"
19211                "    foo();\n"
19212                "    bar();\n"
19213                "}",
19214                Style);
19215 
19216   // Access specifiers should be aligned left.
19217   verifyFormat("class C {\n"
19218                "public:\n"
19219                "    int i;\n"
19220                "};",
19221                Style);
19222 
19223   // Do not align comments.
19224   verifyFormat("int a; // Do not\n"
19225                "double b; // align comments.",
19226                Style);
19227 
19228   // Do not align operands.
19229   EXPECT_EQ("ASSERT(aaaa\n"
19230             "    || bbbb);",
19231             format("ASSERT ( aaaa\n||bbbb);", Style));
19232 
19233   // Accept input's line breaks.
19234   EXPECT_EQ("if (aaaaaaaaaaaaaaa\n"
19235             "    || bbbbbbbbbbbbbbb) {\n"
19236             "    i++;\n"
19237             "}",
19238             format("if (aaaaaaaaaaaaaaa\n"
19239                    "|| bbbbbbbbbbbbbbb) { i++; }",
19240                    Style));
19241   EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
19242             "    i++;\n"
19243             "}",
19244             format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
19245 
19246   // Don't automatically break all macro definitions (llvm.org/PR17842).
19247   verifyFormat("#define aNumber 10", Style);
19248   // However, generally keep the line breaks that the user authored.
19249   EXPECT_EQ("#define aNumber \\\n"
19250             "    10",
19251             format("#define aNumber \\\n"
19252                    " 10",
19253                    Style));
19254 
19255   // Keep empty and one-element array literals on a single line.
19256   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
19257             "                                  copyItems:YES];",
19258             format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
19259                    "copyItems:YES];",
19260                    Style));
19261   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
19262             "                                  copyItems:YES];",
19263             format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
19264                    "             copyItems:YES];",
19265                    Style));
19266   // FIXME: This does not seem right, there should be more indentation before
19267   // the array literal's entries. Nested blocks have the same problem.
19268   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
19269             "    @\"a\",\n"
19270             "    @\"a\"\n"
19271             "]\n"
19272             "                                  copyItems:YES];",
19273             format("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
19274                    "     @\"a\",\n"
19275                    "     @\"a\"\n"
19276                    "     ]\n"
19277                    "       copyItems:YES];",
19278                    Style));
19279   EXPECT_EQ(
19280       "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
19281       "                                  copyItems:YES];",
19282       format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
19283              "   copyItems:YES];",
19284              Style));
19285 
19286   verifyFormat("[self.a b:c c:d];", Style);
19287   EXPECT_EQ("[self.a b:c\n"
19288             "        c:d];",
19289             format("[self.a b:c\n"
19290                    "c:d];",
19291                    Style));
19292 }
19293 
19294 TEST_F(FormatTest, FormatsLambdas) {
19295   verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n");
19296   verifyFormat(
19297       "int c = [b]() mutable noexcept { return [&b] { return b++; }(); }();\n");
19298   verifyFormat("int c = [&] { [=] { return b++; }(); }();\n");
19299   verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n");
19300   verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n");
19301   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n");
19302   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n");
19303   verifyFormat("auto c = [a = [b = 42] {}] {};\n");
19304   verifyFormat("auto c = [a = &i + 10, b = [] {}] {};\n");
19305   verifyFormat("int x = f(*+[] {});");
19306   verifyFormat("void f() {\n"
19307                "  other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
19308                "}\n");
19309   verifyFormat("void f() {\n"
19310                "  other(x.begin(), //\n"
19311                "        x.end(),   //\n"
19312                "        [&](int, int) { return 1; });\n"
19313                "}\n");
19314   verifyFormat("void f() {\n"
19315                "  other.other.other.other.other(\n"
19316                "      x.begin(), x.end(),\n"
19317                "      [something, rather](int, int, int, int, int, int, int) { "
19318                "return 1; });\n"
19319                "}\n");
19320   verifyFormat(
19321       "void f() {\n"
19322       "  other.other.other.other.other(\n"
19323       "      x.begin(), x.end(),\n"
19324       "      [something, rather](int, int, int, int, int, int, int) {\n"
19325       "        //\n"
19326       "      });\n"
19327       "}\n");
19328   verifyFormat("SomeFunction([]() { // A cool function...\n"
19329                "  return 43;\n"
19330                "});");
19331   EXPECT_EQ("SomeFunction([]() {\n"
19332             "#define A a\n"
19333             "  return 43;\n"
19334             "});",
19335             format("SomeFunction([](){\n"
19336                    "#define A a\n"
19337                    "return 43;\n"
19338                    "});"));
19339   verifyFormat("void f() {\n"
19340                "  SomeFunction([](decltype(x), A *a) {});\n"
19341                "  SomeFunction([](typeof(x), A *a) {});\n"
19342                "  SomeFunction([](_Atomic(x), A *a) {});\n"
19343                "  SomeFunction([](__underlying_type(x), A *a) {});\n"
19344                "}");
19345   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
19346                "    [](const aaaaaaaaaa &a) { return a; });");
19347   verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
19348                "  SomeOtherFunctioooooooooooooooooooooooooon();\n"
19349                "});");
19350   verifyFormat("Constructor()\n"
19351                "    : Field([] { // comment\n"
19352                "        int i;\n"
19353                "      }) {}");
19354   verifyFormat("auto my_lambda = [](const string &some_parameter) {\n"
19355                "  return some_parameter.size();\n"
19356                "};");
19357   verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n"
19358                "    [](const string &s) { return s; };");
19359   verifyFormat("int i = aaaaaa ? 1 //\n"
19360                "               : [] {\n"
19361                "                   return 2; //\n"
19362                "                 }();");
19363   verifyFormat("llvm::errs() << \"number of twos is \"\n"
19364                "             << std::count_if(v.begin(), v.end(), [](int x) {\n"
19365                "                  return x == 2; // force break\n"
19366                "                });");
19367   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
19368                "    [=](int iiiiiiiiiiii) {\n"
19369                "      return aaaaaaaaaaaaaaaaaaaaaaa !=\n"
19370                "             aaaaaaaaaaaaaaaaaaaaaaa;\n"
19371                "    });",
19372                getLLVMStyleWithColumns(60));
19373 
19374   verifyFormat("SomeFunction({[&] {\n"
19375                "                // comment\n"
19376                "              },\n"
19377                "              [&] {\n"
19378                "                // comment\n"
19379                "              }});");
19380   verifyFormat("SomeFunction({[&] {\n"
19381                "  // comment\n"
19382                "}});");
19383   verifyFormat(
19384       "virtual aaaaaaaaaaaaaaaa(\n"
19385       "    std::function<bool()> bbbbbbbbbbbb = [&]() { return true; },\n"
19386       "    aaaaa aaaaaaaaa);");
19387 
19388   // Lambdas with return types.
19389   verifyFormat("int c = []() -> int { return 2; }();\n");
19390   verifyFormat("int c = []() -> int * { return 2; }();\n");
19391   verifyFormat("int c = []() -> vector<int> { return {2}; }();\n");
19392   verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
19393   verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
19394   verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
19395   verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
19396   verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
19397   verifyFormat("[a, a]() -> a<1> {};");
19398   verifyFormat("[]() -> foo<5 + 2> { return {}; };");
19399   verifyFormat("[]() -> foo<5 - 2> { return {}; };");
19400   verifyFormat("[]() -> foo<5 / 2> { return {}; };");
19401   verifyFormat("[]() -> foo<5 * 2> { return {}; };");
19402   verifyFormat("[]() -> foo<5 % 2> { return {}; };");
19403   verifyFormat("[]() -> foo<5 << 2> { return {}; };");
19404   verifyFormat("[]() -> foo<!5> { return {}; };");
19405   verifyFormat("[]() -> foo<~5> { return {}; };");
19406   verifyFormat("[]() -> foo<5 | 2> { return {}; };");
19407   verifyFormat("[]() -> foo<5 || 2> { return {}; };");
19408   verifyFormat("[]() -> foo<5 & 2> { return {}; };");
19409   verifyFormat("[]() -> foo<5 && 2> { return {}; };");
19410   verifyFormat("[]() -> foo<5 == 2> { return {}; };");
19411   verifyFormat("[]() -> foo<5 != 2> { return {}; };");
19412   verifyFormat("[]() -> foo<5 >= 2> { return {}; };");
19413   verifyFormat("[]() -> foo<5 <= 2> { return {}; };");
19414   verifyFormat("[]() -> foo<5 < 2> { return {}; };");
19415   verifyFormat("[]() -> foo<2 ? 1 : 0> { return {}; };");
19416   verifyFormat("namespace bar {\n"
19417                "// broken:\n"
19418                "auto foo{[]() -> foo<5 + 2> { return {}; }};\n"
19419                "} // namespace bar");
19420   verifyFormat("namespace bar {\n"
19421                "// broken:\n"
19422                "auto foo{[]() -> foo<5 - 2> { return {}; }};\n"
19423                "} // namespace bar");
19424   verifyFormat("namespace bar {\n"
19425                "// broken:\n"
19426                "auto foo{[]() -> foo<5 / 2> { return {}; }};\n"
19427                "} // namespace bar");
19428   verifyFormat("namespace bar {\n"
19429                "// broken:\n"
19430                "auto foo{[]() -> foo<5 * 2> { return {}; }};\n"
19431                "} // namespace bar");
19432   verifyFormat("namespace bar {\n"
19433                "// broken:\n"
19434                "auto foo{[]() -> foo<5 % 2> { return {}; }};\n"
19435                "} // namespace bar");
19436   verifyFormat("namespace bar {\n"
19437                "// broken:\n"
19438                "auto foo{[]() -> foo<5 << 2> { return {}; }};\n"
19439                "} // namespace bar");
19440   verifyFormat("namespace bar {\n"
19441                "// broken:\n"
19442                "auto foo{[]() -> foo<!5> { return {}; }};\n"
19443                "} // namespace bar");
19444   verifyFormat("namespace bar {\n"
19445                "// broken:\n"
19446                "auto foo{[]() -> foo<~5> { return {}; }};\n"
19447                "} // namespace bar");
19448   verifyFormat("namespace bar {\n"
19449                "// broken:\n"
19450                "auto foo{[]() -> foo<5 | 2> { return {}; }};\n"
19451                "} // namespace bar");
19452   verifyFormat("namespace bar {\n"
19453                "// broken:\n"
19454                "auto foo{[]() -> foo<5 || 2> { return {}; }};\n"
19455                "} // namespace bar");
19456   verifyFormat("namespace bar {\n"
19457                "// broken:\n"
19458                "auto foo{[]() -> foo<5 & 2> { return {}; }};\n"
19459                "} // namespace bar");
19460   verifyFormat("namespace bar {\n"
19461                "// broken:\n"
19462                "auto foo{[]() -> foo<5 && 2> { return {}; }};\n"
19463                "} // namespace bar");
19464   verifyFormat("namespace bar {\n"
19465                "// broken:\n"
19466                "auto foo{[]() -> foo<5 == 2> { return {}; }};\n"
19467                "} // namespace bar");
19468   verifyFormat("namespace bar {\n"
19469                "// broken:\n"
19470                "auto foo{[]() -> foo<5 != 2> { return {}; }};\n"
19471                "} // namespace bar");
19472   verifyFormat("namespace bar {\n"
19473                "// broken:\n"
19474                "auto foo{[]() -> foo<5 >= 2> { return {}; }};\n"
19475                "} // namespace bar");
19476   verifyFormat("namespace bar {\n"
19477                "// broken:\n"
19478                "auto foo{[]() -> foo<5 <= 2> { return {}; }};\n"
19479                "} // namespace bar");
19480   verifyFormat("namespace bar {\n"
19481                "// broken:\n"
19482                "auto foo{[]() -> foo<5 < 2> { return {}; }};\n"
19483                "} // namespace bar");
19484   verifyFormat("namespace bar {\n"
19485                "// broken:\n"
19486                "auto foo{[]() -> foo<2 ? 1 : 0> { return {}; }};\n"
19487                "} // namespace bar");
19488   verifyFormat("[]() -> a<1> {};");
19489   verifyFormat("[]() -> a<1> { ; };");
19490   verifyFormat("[]() -> a<1> { ; }();");
19491   verifyFormat("[a, a]() -> a<true> {};");
19492   verifyFormat("[]() -> a<true> {};");
19493   verifyFormat("[]() -> a<true> { ; };");
19494   verifyFormat("[]() -> a<true> { ; }();");
19495   verifyFormat("[a, a]() -> a<false> {};");
19496   verifyFormat("[]() -> a<false> {};");
19497   verifyFormat("[]() -> a<false> { ; };");
19498   verifyFormat("[]() -> a<false> { ; }();");
19499   verifyFormat("auto foo{[]() -> foo<false> { ; }};");
19500   verifyFormat("namespace bar {\n"
19501                "auto foo{[]() -> foo<false> { ; }};\n"
19502                "} // namespace bar");
19503   verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
19504                "                   int j) -> int {\n"
19505                "  return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
19506                "};");
19507   verifyFormat(
19508       "aaaaaaaaaaaaaaaaaaaaaa(\n"
19509       "    [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n"
19510       "      return aaaaaaaaaaaaaaaaa;\n"
19511       "    });",
19512       getLLVMStyleWithColumns(70));
19513   verifyFormat("[]() //\n"
19514                "    -> int {\n"
19515                "  return 1; //\n"
19516                "};");
19517   verifyFormat("[]() -> Void<T...> {};");
19518   verifyFormat("[a, b]() -> Tuple<T...> { return {}; };");
19519 
19520   // Lambdas with explicit template argument lists.
19521   verifyFormat(
19522       "auto L = []<template <typename> class T, class U>(T<U> &&a) {};\n");
19523 
19524   // Multiple lambdas in the same parentheses change indentation rules. These
19525   // lambdas are forced to start on new lines.
19526   verifyFormat("SomeFunction(\n"
19527                "    []() {\n"
19528                "      //\n"
19529                "    },\n"
19530                "    []() {\n"
19531                "      //\n"
19532                "    });");
19533 
19534   // A lambda passed as arg0 is always pushed to the next line.
19535   verifyFormat("SomeFunction(\n"
19536                "    [this] {\n"
19537                "      //\n"
19538                "    },\n"
19539                "    1);\n");
19540 
19541   // A multi-line lambda passed as arg1 forces arg0 to be pushed out, just like
19542   // the arg0 case above.
19543   auto Style = getGoogleStyle();
19544   Style.BinPackArguments = false;
19545   verifyFormat("SomeFunction(\n"
19546                "    a,\n"
19547                "    [this] {\n"
19548                "      //\n"
19549                "    },\n"
19550                "    b);\n",
19551                Style);
19552   verifyFormat("SomeFunction(\n"
19553                "    a,\n"
19554                "    [this] {\n"
19555                "      //\n"
19556                "    },\n"
19557                "    b);\n");
19558 
19559   // A lambda with a very long line forces arg0 to be pushed out irrespective of
19560   // the BinPackArguments value (as long as the code is wide enough).
19561   verifyFormat(
19562       "something->SomeFunction(\n"
19563       "    a,\n"
19564       "    [this] {\n"
19565       "      "
19566       "D0000000000000000000000000000000000000000000000000000000000001();\n"
19567       "    },\n"
19568       "    b);\n");
19569 
19570   // A multi-line lambda is pulled up as long as the introducer fits on the
19571   // previous line and there are no further args.
19572   verifyFormat("function(1, [this, that] {\n"
19573                "  //\n"
19574                "});\n");
19575   verifyFormat("function([this, that] {\n"
19576                "  //\n"
19577                "});\n");
19578   // FIXME: this format is not ideal and we should consider forcing the first
19579   // arg onto its own line.
19580   verifyFormat("function(a, b, c, //\n"
19581                "         d, [this, that] {\n"
19582                "           //\n"
19583                "         });\n");
19584 
19585   // Multiple lambdas are treated correctly even when there is a short arg0.
19586   verifyFormat("SomeFunction(\n"
19587                "    1,\n"
19588                "    [this] {\n"
19589                "      //\n"
19590                "    },\n"
19591                "    [this] {\n"
19592                "      //\n"
19593                "    },\n"
19594                "    1);\n");
19595 
19596   // More complex introducers.
19597   verifyFormat("return [i, args...] {};");
19598 
19599   // Not lambdas.
19600   verifyFormat("constexpr char hello[]{\"hello\"};");
19601   verifyFormat("double &operator[](int i) { return 0; }\n"
19602                "int i;");
19603   verifyFormat("std::unique_ptr<int[]> foo() {}");
19604   verifyFormat("int i = a[a][a]->f();");
19605   verifyFormat("int i = (*b)[a]->f();");
19606 
19607   // Other corner cases.
19608   verifyFormat("void f() {\n"
19609                "  bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
19610                "  );\n"
19611                "}");
19612 
19613   // Lambdas created through weird macros.
19614   verifyFormat("void f() {\n"
19615                "  MACRO((const AA &a) { return 1; });\n"
19616                "  MACRO((AA &a) { return 1; });\n"
19617                "}");
19618 
19619   verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
19620                "      doo_dah();\n"
19621                "      doo_dah();\n"
19622                "    })) {\n"
19623                "}");
19624   verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n"
19625                "                doo_dah();\n"
19626                "                doo_dah();\n"
19627                "              })) {\n"
19628                "}");
19629   verifyFormat("if CONSTEXPR (blah_blah(whatever, whatever, [] {\n"
19630                "                doo_dah();\n"
19631                "                doo_dah();\n"
19632                "              })) {\n"
19633                "}");
19634   verifyFormat("auto lambda = []() {\n"
19635                "  int a = 2\n"
19636                "#if A\n"
19637                "          + 2\n"
19638                "#endif\n"
19639                "      ;\n"
19640                "};");
19641 
19642   // Lambdas with complex multiline introducers.
19643   verifyFormat(
19644       "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
19645       "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n"
19646       "        -> ::std::unordered_set<\n"
19647       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
19648       "      //\n"
19649       "    });");
19650 
19651   FormatStyle DoNotMerge = getLLVMStyle();
19652   DoNotMerge.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
19653   verifyFormat("auto c = []() {\n"
19654                "  return b;\n"
19655                "};",
19656                "auto c = []() { return b; };", DoNotMerge);
19657   verifyFormat("auto c = []() {\n"
19658                "};",
19659                " auto c = []() {};", DoNotMerge);
19660 
19661   FormatStyle MergeEmptyOnly = getLLVMStyle();
19662   MergeEmptyOnly.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Empty;
19663   verifyFormat("auto c = []() {\n"
19664                "  return b;\n"
19665                "};",
19666                "auto c = []() {\n"
19667                "  return b;\n"
19668                " };",
19669                MergeEmptyOnly);
19670   verifyFormat("auto c = []() {};",
19671                "auto c = []() {\n"
19672                "};",
19673                MergeEmptyOnly);
19674 
19675   FormatStyle MergeInline = getLLVMStyle();
19676   MergeInline.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Inline;
19677   verifyFormat("auto c = []() {\n"
19678                "  return b;\n"
19679                "};",
19680                "auto c = []() { return b; };", MergeInline);
19681   verifyFormat("function([]() { return b; })", "function([]() { return b; })",
19682                MergeInline);
19683   verifyFormat("function([]() { return b; }, a)",
19684                "function([]() { return b; }, a)", MergeInline);
19685   verifyFormat("function(a, []() { return b; })",
19686                "function(a, []() { return b; })", MergeInline);
19687 
19688   // Check option "BraceWrapping.BeforeLambdaBody" and different state of
19689   // AllowShortLambdasOnASingleLine
19690   FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle();
19691   LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
19692   LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
19693   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
19694       FormatStyle::ShortLambdaStyle::SLS_None;
19695   verifyFormat("FctWithOneNestedLambdaInline_SLS_None(\n"
19696                "    []()\n"
19697                "    {\n"
19698                "      return 17;\n"
19699                "    });",
19700                LLVMWithBeforeLambdaBody);
19701   verifyFormat("FctWithOneNestedLambdaEmpty_SLS_None(\n"
19702                "    []()\n"
19703                "    {\n"
19704                "    });",
19705                LLVMWithBeforeLambdaBody);
19706   verifyFormat("auto fct_SLS_None = []()\n"
19707                "{\n"
19708                "  return 17;\n"
19709                "};",
19710                LLVMWithBeforeLambdaBody);
19711   verifyFormat("TwoNestedLambdas_SLS_None(\n"
19712                "    []()\n"
19713                "    {\n"
19714                "      return Call(\n"
19715                "          []()\n"
19716                "          {\n"
19717                "            return 17;\n"
19718                "          });\n"
19719                "    });",
19720                LLVMWithBeforeLambdaBody);
19721   verifyFormat("void Fct() {\n"
19722                "  return {[]()\n"
19723                "          {\n"
19724                "            return 17;\n"
19725                "          }};\n"
19726                "}",
19727                LLVMWithBeforeLambdaBody);
19728 
19729   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
19730       FormatStyle::ShortLambdaStyle::SLS_Empty;
19731   verifyFormat("FctWithOneNestedLambdaInline_SLS_Empty(\n"
19732                "    []()\n"
19733                "    {\n"
19734                "      return 17;\n"
19735                "    });",
19736                LLVMWithBeforeLambdaBody);
19737   verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Empty([]() {});",
19738                LLVMWithBeforeLambdaBody);
19739   verifyFormat("FctWithOneNestedLambdaEmptyInsideAVeryVeryVeryVeryVeryVeryVeryL"
19740                "ongFunctionName_SLS_Empty(\n"
19741                "    []() {});",
19742                LLVMWithBeforeLambdaBody);
19743   verifyFormat("FctWithMultipleParams_SLS_Empty(A, B,\n"
19744                "                                []()\n"
19745                "                                {\n"
19746                "                                  return 17;\n"
19747                "                                });",
19748                LLVMWithBeforeLambdaBody);
19749   verifyFormat("auto fct_SLS_Empty = []()\n"
19750                "{\n"
19751                "  return 17;\n"
19752                "};",
19753                LLVMWithBeforeLambdaBody);
19754   verifyFormat("TwoNestedLambdas_SLS_Empty(\n"
19755                "    []()\n"
19756                "    {\n"
19757                "      return Call([]() {});\n"
19758                "    });",
19759                LLVMWithBeforeLambdaBody);
19760   verifyFormat("TwoNestedLambdas_SLS_Empty(A,\n"
19761                "                           []()\n"
19762                "                           {\n"
19763                "                             return Call([]() {});\n"
19764                "                           });",
19765                LLVMWithBeforeLambdaBody);
19766   verifyFormat(
19767       "FctWithLongLineInLambda_SLS_Empty(\n"
19768       "    []()\n"
19769       "    {\n"
19770       "      return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
19771       "                               AndShouldNotBeConsiderAsInline,\n"
19772       "                               LambdaBodyMustBeBreak);\n"
19773       "    });",
19774       LLVMWithBeforeLambdaBody);
19775 
19776   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
19777       FormatStyle::ShortLambdaStyle::SLS_Inline;
19778   verifyFormat("FctWithOneNestedLambdaInline_SLS_Inline([]() { return 17; });",
19779                LLVMWithBeforeLambdaBody);
19780   verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Inline([]() {});",
19781                LLVMWithBeforeLambdaBody);
19782   verifyFormat("auto fct_SLS_Inline = []()\n"
19783                "{\n"
19784                "  return 17;\n"
19785                "};",
19786                LLVMWithBeforeLambdaBody);
19787   verifyFormat("TwoNestedLambdas_SLS_Inline([]() { return Call([]() { return "
19788                "17; }); });",
19789                LLVMWithBeforeLambdaBody);
19790   verifyFormat(
19791       "FctWithLongLineInLambda_SLS_Inline(\n"
19792       "    []()\n"
19793       "    {\n"
19794       "      return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
19795       "                               AndShouldNotBeConsiderAsInline,\n"
19796       "                               LambdaBodyMustBeBreak);\n"
19797       "    });",
19798       LLVMWithBeforeLambdaBody);
19799   verifyFormat("FctWithMultipleParams_SLS_Inline("
19800                "VeryLongParameterThatShouldAskToBeOnMultiLine,\n"
19801                "                                 []() { return 17; });",
19802                LLVMWithBeforeLambdaBody);
19803   verifyFormat(
19804       "FctWithMultipleParams_SLS_Inline(FirstParam, []() { return 17; });",
19805       LLVMWithBeforeLambdaBody);
19806 
19807   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
19808       FormatStyle::ShortLambdaStyle::SLS_All;
19809   verifyFormat("FctWithOneNestedLambdaInline_SLS_All([]() { return 17; });",
19810                LLVMWithBeforeLambdaBody);
19811   verifyFormat("FctWithOneNestedLambdaEmpty_SLS_All([]() {});",
19812                LLVMWithBeforeLambdaBody);
19813   verifyFormat("auto fct_SLS_All = []() { return 17; };",
19814                LLVMWithBeforeLambdaBody);
19815   verifyFormat("FctWithOneParam_SLS_All(\n"
19816                "    []()\n"
19817                "    {\n"
19818                "      // A cool function...\n"
19819                "      return 43;\n"
19820                "    });",
19821                LLVMWithBeforeLambdaBody);
19822   verifyFormat("FctWithMultipleParams_SLS_All("
19823                "VeryLongParameterThatShouldAskToBeOnMultiLine,\n"
19824                "                              []() { return 17; });",
19825                LLVMWithBeforeLambdaBody);
19826   verifyFormat("FctWithMultipleParams_SLS_All(A, []() { return 17; });",
19827                LLVMWithBeforeLambdaBody);
19828   verifyFormat("FctWithMultipleParams_SLS_All(A, B, []() { return 17; });",
19829                LLVMWithBeforeLambdaBody);
19830   verifyFormat(
19831       "FctWithLongLineInLambda_SLS_All(\n"
19832       "    []()\n"
19833       "    {\n"
19834       "      return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
19835       "                               AndShouldNotBeConsiderAsInline,\n"
19836       "                               LambdaBodyMustBeBreak);\n"
19837       "    });",
19838       LLVMWithBeforeLambdaBody);
19839   verifyFormat(
19840       "auto fct_SLS_All = []()\n"
19841       "{\n"
19842       "  return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
19843       "                           AndShouldNotBeConsiderAsInline,\n"
19844       "                           LambdaBodyMustBeBreak);\n"
19845       "};",
19846       LLVMWithBeforeLambdaBody);
19847   LLVMWithBeforeLambdaBody.BinPackParameters = false;
19848   verifyFormat("FctAllOnSameLine_SLS_All([]() { return S; }, Fst, Second);",
19849                LLVMWithBeforeLambdaBody);
19850   verifyFormat(
19851       "FctWithLongLineInLambda_SLS_All([]() { return SomeValueNotSoLong; },\n"
19852       "                                FirstParam,\n"
19853       "                                SecondParam,\n"
19854       "                                ThirdParam,\n"
19855       "                                FourthParam);",
19856       LLVMWithBeforeLambdaBody);
19857   verifyFormat("FctWithLongLineInLambda_SLS_All(\n"
19858                "    []() { return "
19859                "SomeValueVeryVeryVeryVeryVeryVeryVeryVeryVeryLong; },\n"
19860                "    FirstParam,\n"
19861                "    SecondParam,\n"
19862                "    ThirdParam,\n"
19863                "    FourthParam);",
19864                LLVMWithBeforeLambdaBody);
19865   verifyFormat(
19866       "FctWithLongLineInLambda_SLS_All(FirstParam,\n"
19867       "                                SecondParam,\n"
19868       "                                ThirdParam,\n"
19869       "                                FourthParam,\n"
19870       "                                []() { return SomeValueNotSoLong; });",
19871       LLVMWithBeforeLambdaBody);
19872   verifyFormat("FctWithLongLineInLambda_SLS_All(\n"
19873                "    []()\n"
19874                "    {\n"
19875                "      return "
19876                "HereAVeryLongLineThatWillBeFormattedOnMultipleLineAndShouldNotB"
19877                "eConsiderAsInline;\n"
19878                "    });",
19879                LLVMWithBeforeLambdaBody);
19880   verifyFormat(
19881       "FctWithLongLineInLambda_SLS_All(\n"
19882       "    []()\n"
19883       "    {\n"
19884       "      return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
19885       "                               AndShouldNotBeConsiderAsInline,\n"
19886       "                               LambdaBodyMustBeBreak);\n"
19887       "    });",
19888       LLVMWithBeforeLambdaBody);
19889   verifyFormat("FctWithTwoParams_SLS_All(\n"
19890                "    []()\n"
19891                "    {\n"
19892                "      // A cool function...\n"
19893                "      return 43;\n"
19894                "    },\n"
19895                "    87);",
19896                LLVMWithBeforeLambdaBody);
19897   verifyFormat("FctWithTwoParams_SLS_All([]() { return 43; }, 87);",
19898                LLVMWithBeforeLambdaBody);
19899   verifyFormat("FctWithOneNestedLambdas_SLS_All([]() { return 17; });",
19900                LLVMWithBeforeLambdaBody);
19901   verifyFormat(
19902       "TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; }); });",
19903       LLVMWithBeforeLambdaBody);
19904   verifyFormat("TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; "
19905                "}); }, x);",
19906                LLVMWithBeforeLambdaBody);
19907   verifyFormat("TwoNestedLambdas_SLS_All(\n"
19908                "    []()\n"
19909                "    {\n"
19910                "      // A cool function...\n"
19911                "      return Call([]() { return 17; });\n"
19912                "    });",
19913                LLVMWithBeforeLambdaBody);
19914   verifyFormat("TwoNestedLambdas_SLS_All(\n"
19915                "    []()\n"
19916                "    {\n"
19917                "      return Call(\n"
19918                "          []()\n"
19919                "          {\n"
19920                "            // A cool function...\n"
19921                "            return 17;\n"
19922                "          });\n"
19923                "    });",
19924                LLVMWithBeforeLambdaBody);
19925 
19926   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
19927       FormatStyle::ShortLambdaStyle::SLS_None;
19928 
19929   verifyFormat("auto select = [this]() -> const Library::Object *\n"
19930                "{\n"
19931                "  return MyAssignment::SelectFromList(this);\n"
19932                "};\n",
19933                LLVMWithBeforeLambdaBody);
19934 
19935   verifyFormat("auto select = [this]() -> const Library::Object &\n"
19936                "{\n"
19937                "  return MyAssignment::SelectFromList(this);\n"
19938                "};\n",
19939                LLVMWithBeforeLambdaBody);
19940 
19941   verifyFormat("auto select = [this]() -> std::unique_ptr<Object>\n"
19942                "{\n"
19943                "  return MyAssignment::SelectFromList(this);\n"
19944                "};\n",
19945                LLVMWithBeforeLambdaBody);
19946 
19947   verifyFormat("namespace test {\n"
19948                "class Test {\n"
19949                "public:\n"
19950                "  Test() = default;\n"
19951                "};\n"
19952                "} // namespace test",
19953                LLVMWithBeforeLambdaBody);
19954 
19955   // Lambdas with different indentation styles.
19956   Style = getLLVMStyleWithColumns(100);
19957   EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n"
19958             "  return promise.then(\n"
19959             "      [this, &someVariable, someObject = "
19960             "std::mv(s)](std::vector<int> evaluated) mutable {\n"
19961             "        return someObject.startAsyncAction().then(\n"
19962             "            [this, &someVariable](AsyncActionResult result) "
19963             "mutable { result.processMore(); });\n"
19964             "      });\n"
19965             "}\n",
19966             format("SomeResult doSomething(SomeObject promise) {\n"
19967                    "  return promise.then([this, &someVariable, someObject = "
19968                    "std::mv(s)](std::vector<int> evaluated) mutable {\n"
19969                    "    return someObject.startAsyncAction().then([this, "
19970                    "&someVariable](AsyncActionResult result) mutable {\n"
19971                    "      result.processMore();\n"
19972                    "    });\n"
19973                    "  });\n"
19974                    "}\n",
19975                    Style));
19976   Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope;
19977   verifyFormat("test() {\n"
19978                "  ([]() -> {\n"
19979                "    int b = 32;\n"
19980                "    return 3;\n"
19981                "  }).foo();\n"
19982                "}",
19983                Style);
19984   verifyFormat("test() {\n"
19985                "  []() -> {\n"
19986                "    int b = 32;\n"
19987                "    return 3;\n"
19988                "  }\n"
19989                "}",
19990                Style);
19991   verifyFormat("std::sort(v.begin(), v.end(),\n"
19992                "          [](const auto &someLongArgumentName, const auto "
19993                "&someOtherLongArgumentName) {\n"
19994                "  return someLongArgumentName.someMemberVariable < "
19995                "someOtherLongArgumentName.someMemberVariable;\n"
19996                "});",
19997                Style);
19998   verifyFormat("test() {\n"
19999                "  (\n"
20000                "      []() -> {\n"
20001                "        int b = 32;\n"
20002                "        return 3;\n"
20003                "      },\n"
20004                "      foo, bar)\n"
20005                "      .foo();\n"
20006                "}",
20007                Style);
20008   verifyFormat("test() {\n"
20009                "  ([]() -> {\n"
20010                "    int b = 32;\n"
20011                "    return 3;\n"
20012                "  })\n"
20013                "      .foo()\n"
20014                "      .bar();\n"
20015                "}",
20016                Style);
20017   EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n"
20018             "  return promise.then(\n"
20019             "      [this, &someVariable, someObject = "
20020             "std::mv(s)](std::vector<int> evaluated) mutable {\n"
20021             "    return someObject.startAsyncAction().then(\n"
20022             "        [this, &someVariable](AsyncActionResult result) mutable { "
20023             "result.processMore(); });\n"
20024             "  });\n"
20025             "}\n",
20026             format("SomeResult doSomething(SomeObject promise) {\n"
20027                    "  return promise.then([this, &someVariable, someObject = "
20028                    "std::mv(s)](std::vector<int> evaluated) mutable {\n"
20029                    "    return someObject.startAsyncAction().then([this, "
20030                    "&someVariable](AsyncActionResult result) mutable {\n"
20031                    "      result.processMore();\n"
20032                    "    });\n"
20033                    "  });\n"
20034                    "}\n",
20035                    Style));
20036   EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n"
20037             "  return promise.then([this, &someVariable] {\n"
20038             "    return someObject.startAsyncAction().then(\n"
20039             "        [this, &someVariable](AsyncActionResult result) mutable { "
20040             "result.processMore(); });\n"
20041             "  });\n"
20042             "}\n",
20043             format("SomeResult doSomething(SomeObject promise) {\n"
20044                    "  return promise.then([this, &someVariable] {\n"
20045                    "    return someObject.startAsyncAction().then([this, "
20046                    "&someVariable](AsyncActionResult result) mutable {\n"
20047                    "      result.processMore();\n"
20048                    "    });\n"
20049                    "  });\n"
20050                    "}\n",
20051                    Style));
20052   Style = getGoogleStyle();
20053   Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope;
20054   EXPECT_EQ("#define A                                       \\\n"
20055             "  [] {                                          \\\n"
20056             "    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(        \\\n"
20057             "        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
20058             "      }",
20059             format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
20060                    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
20061                    Style));
20062   // TODO: The current formatting has a minor issue that's not worth fixing
20063   // right now whereby the closing brace is indented relative to the signature
20064   // instead of being aligned. This only happens with macros.
20065 }
20066 
20067 TEST_F(FormatTest, LambdaWithLineComments) {
20068   FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle();
20069   LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
20070   LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
20071   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
20072       FormatStyle::ShortLambdaStyle::SLS_All;
20073 
20074   verifyFormat("auto k = []() { return; }", LLVMWithBeforeLambdaBody);
20075   verifyFormat("auto k = []() // comment\n"
20076                "{ return; }",
20077                LLVMWithBeforeLambdaBody);
20078   verifyFormat("auto k = []() /* comment */ { return; }",
20079                LLVMWithBeforeLambdaBody);
20080   verifyFormat("auto k = []() /* comment */ /* comment */ { return; }",
20081                LLVMWithBeforeLambdaBody);
20082   verifyFormat("auto k = []() // X\n"
20083                "{ return; }",
20084                LLVMWithBeforeLambdaBody);
20085   verifyFormat(
20086       "auto k = []() // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"
20087       "{ return; }",
20088       LLVMWithBeforeLambdaBody);
20089 }
20090 
20091 TEST_F(FormatTest, EmptyLinesInLambdas) {
20092   verifyFormat("auto lambda = []() {\n"
20093                "  x(); //\n"
20094                "};",
20095                "auto lambda = []() {\n"
20096                "\n"
20097                "  x(); //\n"
20098                "\n"
20099                "};");
20100 }
20101 
20102 TEST_F(FormatTest, FormatsBlocks) {
20103   FormatStyle ShortBlocks = getLLVMStyle();
20104   ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
20105   verifyFormat("int (^Block)(int, int);", ShortBlocks);
20106   verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
20107   verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
20108   verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks);
20109   verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks);
20110   verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks);
20111 
20112   verifyFormat("foo(^{ bar(); });", ShortBlocks);
20113   verifyFormat("foo(a, ^{ bar(); });", ShortBlocks);
20114   verifyFormat("{ void (^block)(Object *x); }", ShortBlocks);
20115 
20116   verifyFormat("[operation setCompletionBlock:^{\n"
20117                "  [self onOperationDone];\n"
20118                "}];");
20119   verifyFormat("int i = {[operation setCompletionBlock:^{\n"
20120                "  [self onOperationDone];\n"
20121                "}]};");
20122   verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
20123                "  f();\n"
20124                "}];");
20125   verifyFormat("int a = [operation block:^int(int *i) {\n"
20126                "  return 1;\n"
20127                "}];");
20128   verifyFormat("[myObject doSomethingWith:arg1\n"
20129                "                      aaa:^int(int *a) {\n"
20130                "                        return 1;\n"
20131                "                      }\n"
20132                "                      bbb:f(a * bbbbbbbb)];");
20133 
20134   verifyFormat("[operation setCompletionBlock:^{\n"
20135                "  [self.delegate newDataAvailable];\n"
20136                "}];",
20137                getLLVMStyleWithColumns(60));
20138   verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
20139                "  NSString *path = [self sessionFilePath];\n"
20140                "  if (path) {\n"
20141                "    // ...\n"
20142                "  }\n"
20143                "});");
20144   verifyFormat("[[SessionService sharedService]\n"
20145                "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
20146                "      if (window) {\n"
20147                "        [self windowDidLoad:window];\n"
20148                "      } else {\n"
20149                "        [self errorLoadingWindow];\n"
20150                "      }\n"
20151                "    }];");
20152   verifyFormat("void (^largeBlock)(void) = ^{\n"
20153                "  // ...\n"
20154                "};\n",
20155                getLLVMStyleWithColumns(40));
20156   verifyFormat("[[SessionService sharedService]\n"
20157                "    loadWindowWithCompletionBlock: //\n"
20158                "        ^(SessionWindow *window) {\n"
20159                "          if (window) {\n"
20160                "            [self windowDidLoad:window];\n"
20161                "          } else {\n"
20162                "            [self errorLoadingWindow];\n"
20163                "          }\n"
20164                "        }];",
20165                getLLVMStyleWithColumns(60));
20166   verifyFormat("[myObject doSomethingWith:arg1\n"
20167                "    firstBlock:^(Foo *a) {\n"
20168                "      // ...\n"
20169                "      int i;\n"
20170                "    }\n"
20171                "    secondBlock:^(Bar *b) {\n"
20172                "      // ...\n"
20173                "      int i;\n"
20174                "    }\n"
20175                "    thirdBlock:^Foo(Bar *b) {\n"
20176                "      // ...\n"
20177                "      int i;\n"
20178                "    }];");
20179   verifyFormat("[myObject doSomethingWith:arg1\n"
20180                "               firstBlock:-1\n"
20181                "              secondBlock:^(Bar *b) {\n"
20182                "                // ...\n"
20183                "                int i;\n"
20184                "              }];");
20185 
20186   verifyFormat("f(^{\n"
20187                "  @autoreleasepool {\n"
20188                "    if (a) {\n"
20189                "      g();\n"
20190                "    }\n"
20191                "  }\n"
20192                "});");
20193   verifyFormat("Block b = ^int *(A *a, B *b) {}");
20194   verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n"
20195                "};");
20196 
20197   FormatStyle FourIndent = getLLVMStyle();
20198   FourIndent.ObjCBlockIndentWidth = 4;
20199   verifyFormat("[operation setCompletionBlock:^{\n"
20200                "    [self onOperationDone];\n"
20201                "}];",
20202                FourIndent);
20203 }
20204 
20205 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) {
20206   FormatStyle ZeroColumn = getLLVMStyle();
20207   ZeroColumn.ColumnLimit = 0;
20208 
20209   verifyFormat("[[SessionService sharedService] "
20210                "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
20211                "  if (window) {\n"
20212                "    [self windowDidLoad:window];\n"
20213                "  } else {\n"
20214                "    [self errorLoadingWindow];\n"
20215                "  }\n"
20216                "}];",
20217                ZeroColumn);
20218   EXPECT_EQ("[[SessionService sharedService]\n"
20219             "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
20220             "      if (window) {\n"
20221             "        [self windowDidLoad:window];\n"
20222             "      } else {\n"
20223             "        [self errorLoadingWindow];\n"
20224             "      }\n"
20225             "    }];",
20226             format("[[SessionService sharedService]\n"
20227                    "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
20228                    "                if (window) {\n"
20229                    "    [self windowDidLoad:window];\n"
20230                    "  } else {\n"
20231                    "    [self errorLoadingWindow];\n"
20232                    "  }\n"
20233                    "}];",
20234                    ZeroColumn));
20235   verifyFormat("[myObject doSomethingWith:arg1\n"
20236                "    firstBlock:^(Foo *a) {\n"
20237                "      // ...\n"
20238                "      int i;\n"
20239                "    }\n"
20240                "    secondBlock:^(Bar *b) {\n"
20241                "      // ...\n"
20242                "      int i;\n"
20243                "    }\n"
20244                "    thirdBlock:^Foo(Bar *b) {\n"
20245                "      // ...\n"
20246                "      int i;\n"
20247                "    }];",
20248                ZeroColumn);
20249   verifyFormat("f(^{\n"
20250                "  @autoreleasepool {\n"
20251                "    if (a) {\n"
20252                "      g();\n"
20253                "    }\n"
20254                "  }\n"
20255                "});",
20256                ZeroColumn);
20257   verifyFormat("void (^largeBlock)(void) = ^{\n"
20258                "  // ...\n"
20259                "};",
20260                ZeroColumn);
20261 
20262   ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
20263   EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };",
20264             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
20265   ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
20266   EXPECT_EQ("void (^largeBlock)(void) = ^{\n"
20267             "  int i;\n"
20268             "};",
20269             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
20270 }
20271 
20272 TEST_F(FormatTest, SupportsCRLF) {
20273   EXPECT_EQ("int a;\r\n"
20274             "int b;\r\n"
20275             "int c;\r\n",
20276             format("int a;\r\n"
20277                    "  int b;\r\n"
20278                    "    int c;\r\n",
20279                    getLLVMStyle()));
20280   EXPECT_EQ("int a;\r\n"
20281             "int b;\r\n"
20282             "int c;\r\n",
20283             format("int a;\r\n"
20284                    "  int b;\n"
20285                    "    int c;\r\n",
20286                    getLLVMStyle()));
20287   EXPECT_EQ("int a;\n"
20288             "int b;\n"
20289             "int c;\n",
20290             format("int a;\r\n"
20291                    "  int b;\n"
20292                    "    int c;\n",
20293                    getLLVMStyle()));
20294   EXPECT_EQ("\"aaaaaaa \"\r\n"
20295             "\"bbbbbbb\";\r\n",
20296             format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
20297   EXPECT_EQ("#define A \\\r\n"
20298             "  b;      \\\r\n"
20299             "  c;      \\\r\n"
20300             "  d;\r\n",
20301             format("#define A \\\r\n"
20302                    "  b; \\\r\n"
20303                    "  c; d; \r\n",
20304                    getGoogleStyle()));
20305 
20306   EXPECT_EQ("/*\r\n"
20307             "multi line block comments\r\n"
20308             "should not introduce\r\n"
20309             "an extra carriage return\r\n"
20310             "*/\r\n",
20311             format("/*\r\n"
20312                    "multi line block comments\r\n"
20313                    "should not introduce\r\n"
20314                    "an extra carriage return\r\n"
20315                    "*/\r\n"));
20316   EXPECT_EQ("/*\r\n"
20317             "\r\n"
20318             "*/",
20319             format("/*\r\n"
20320                    "    \r\r\r\n"
20321                    "*/"));
20322 
20323   FormatStyle style = getLLVMStyle();
20324 
20325   style.DeriveLineEnding = true;
20326   style.UseCRLF = false;
20327   EXPECT_EQ("union FooBarBazQux {\n"
20328             "  int foo;\n"
20329             "  int bar;\n"
20330             "  int baz;\n"
20331             "};",
20332             format("union FooBarBazQux {\r\n"
20333                    "  int foo;\n"
20334                    "  int bar;\r\n"
20335                    "  int baz;\n"
20336                    "};",
20337                    style));
20338   style.UseCRLF = true;
20339   EXPECT_EQ("union FooBarBazQux {\r\n"
20340             "  int foo;\r\n"
20341             "  int bar;\r\n"
20342             "  int baz;\r\n"
20343             "};",
20344             format("union FooBarBazQux {\r\n"
20345                    "  int foo;\n"
20346                    "  int bar;\r\n"
20347                    "  int baz;\n"
20348                    "};",
20349                    style));
20350 
20351   style.DeriveLineEnding = false;
20352   style.UseCRLF = false;
20353   EXPECT_EQ("union FooBarBazQux {\n"
20354             "  int foo;\n"
20355             "  int bar;\n"
20356             "  int baz;\n"
20357             "  int qux;\n"
20358             "};",
20359             format("union FooBarBazQux {\r\n"
20360                    "  int foo;\n"
20361                    "  int bar;\r\n"
20362                    "  int baz;\n"
20363                    "  int qux;\r\n"
20364                    "};",
20365                    style));
20366   style.UseCRLF = true;
20367   EXPECT_EQ("union FooBarBazQux {\r\n"
20368             "  int foo;\r\n"
20369             "  int bar;\r\n"
20370             "  int baz;\r\n"
20371             "  int qux;\r\n"
20372             "};",
20373             format("union FooBarBazQux {\r\n"
20374                    "  int foo;\n"
20375                    "  int bar;\r\n"
20376                    "  int baz;\n"
20377                    "  int qux;\n"
20378                    "};",
20379                    style));
20380 
20381   style.DeriveLineEnding = true;
20382   style.UseCRLF = false;
20383   EXPECT_EQ("union FooBarBazQux {\r\n"
20384             "  int foo;\r\n"
20385             "  int bar;\r\n"
20386             "  int baz;\r\n"
20387             "  int qux;\r\n"
20388             "};",
20389             format("union FooBarBazQux {\r\n"
20390                    "  int foo;\n"
20391                    "  int bar;\r\n"
20392                    "  int baz;\n"
20393                    "  int qux;\r\n"
20394                    "};",
20395                    style));
20396   style.UseCRLF = true;
20397   EXPECT_EQ("union FooBarBazQux {\n"
20398             "  int foo;\n"
20399             "  int bar;\n"
20400             "  int baz;\n"
20401             "  int qux;\n"
20402             "};",
20403             format("union FooBarBazQux {\r\n"
20404                    "  int foo;\n"
20405                    "  int bar;\r\n"
20406                    "  int baz;\n"
20407                    "  int qux;\n"
20408                    "};",
20409                    style));
20410 }
20411 
20412 TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
20413   verifyFormat("MY_CLASS(C) {\n"
20414                "  int i;\n"
20415                "  int j;\n"
20416                "};");
20417 }
20418 
20419 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
20420   FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
20421   TwoIndent.ContinuationIndentWidth = 2;
20422 
20423   EXPECT_EQ("int i =\n"
20424             "  longFunction(\n"
20425             "    arg);",
20426             format("int i = longFunction(arg);", TwoIndent));
20427 
20428   FormatStyle SixIndent = getLLVMStyleWithColumns(20);
20429   SixIndent.ContinuationIndentWidth = 6;
20430 
20431   EXPECT_EQ("int i =\n"
20432             "      longFunction(\n"
20433             "            arg);",
20434             format("int i = longFunction(arg);", SixIndent));
20435 }
20436 
20437 TEST_F(FormatTest, WrappedClosingParenthesisIndent) {
20438   FormatStyle Style = getLLVMStyle();
20439   verifyFormat("int Foo::getter(\n"
20440                "    //\n"
20441                ") const {\n"
20442                "  return foo;\n"
20443                "}",
20444                Style);
20445   verifyFormat("void Foo::setter(\n"
20446                "    //\n"
20447                ") {\n"
20448                "  foo = 1;\n"
20449                "}",
20450                Style);
20451 }
20452 
20453 TEST_F(FormatTest, SpacesInAngles) {
20454   FormatStyle Spaces = getLLVMStyle();
20455   Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
20456 
20457   verifyFormat("vector< ::std::string > x1;", Spaces);
20458   verifyFormat("Foo< int, Bar > x2;", Spaces);
20459   verifyFormat("Foo< ::int, ::Bar > x3;", Spaces);
20460 
20461   verifyFormat("static_cast< int >(arg);", Spaces);
20462   verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
20463   verifyFormat("f< int, float >();", Spaces);
20464   verifyFormat("template <> g() {}", Spaces);
20465   verifyFormat("template < std::vector< int > > f() {}", Spaces);
20466   verifyFormat("std::function< void(int, int) > fct;", Spaces);
20467   verifyFormat("void inFunction() { std::function< void(int, int) > fct; }",
20468                Spaces);
20469 
20470   Spaces.Standard = FormatStyle::LS_Cpp03;
20471   Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
20472   verifyFormat("A< A< int > >();", Spaces);
20473 
20474   Spaces.SpacesInAngles = FormatStyle::SIAS_Never;
20475   verifyFormat("A<A<int> >();", Spaces);
20476 
20477   Spaces.SpacesInAngles = FormatStyle::SIAS_Leave;
20478   verifyFormat("vector< ::std::string> x4;", "vector<::std::string> x4;",
20479                Spaces);
20480   verifyFormat("vector< ::std::string > x4;", "vector<::std::string > x4;",
20481                Spaces);
20482 
20483   verifyFormat("A<A<int> >();", Spaces);
20484   verifyFormat("A<A<int> >();", "A<A<int>>();", Spaces);
20485   verifyFormat("A< A< int > >();", Spaces);
20486 
20487   Spaces.Standard = FormatStyle::LS_Cpp11;
20488   Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
20489   verifyFormat("A< A< int > >();", Spaces);
20490 
20491   Spaces.SpacesInAngles = FormatStyle::SIAS_Never;
20492   verifyFormat("vector<::std::string> x4;", Spaces);
20493   verifyFormat("vector<int> x5;", Spaces);
20494   verifyFormat("Foo<int, Bar> x6;", Spaces);
20495   verifyFormat("Foo<::int, ::Bar> x7;", Spaces);
20496 
20497   verifyFormat("A<A<int>>();", Spaces);
20498 
20499   Spaces.SpacesInAngles = FormatStyle::SIAS_Leave;
20500   verifyFormat("vector<::std::string> x4;", Spaces);
20501   verifyFormat("vector< ::std::string > x4;", Spaces);
20502   verifyFormat("vector<int> x5;", Spaces);
20503   verifyFormat("vector< int > x5;", Spaces);
20504   verifyFormat("Foo<int, Bar> x6;", Spaces);
20505   verifyFormat("Foo< int, Bar > x6;", Spaces);
20506   verifyFormat("Foo<::int, ::Bar> x7;", Spaces);
20507   verifyFormat("Foo< ::int, ::Bar > x7;", Spaces);
20508 
20509   verifyFormat("A<A<int>>();", Spaces);
20510   verifyFormat("A< A< int > >();", Spaces);
20511   verifyFormat("A<A<int > >();", Spaces);
20512   verifyFormat("A< A< int>>();", Spaces);
20513 }
20514 
20515 TEST_F(FormatTest, SpaceAfterTemplateKeyword) {
20516   FormatStyle Style = getLLVMStyle();
20517   Style.SpaceAfterTemplateKeyword = false;
20518   verifyFormat("template<int> void foo();", Style);
20519 }
20520 
20521 TEST_F(FormatTest, TripleAngleBrackets) {
20522   verifyFormat("f<<<1, 1>>>();");
20523   verifyFormat("f<<<1, 1, 1, s>>>();");
20524   verifyFormat("f<<<a, b, c, d>>>();");
20525   EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();"));
20526   verifyFormat("f<param><<<1, 1>>>();");
20527   verifyFormat("f<1><<<1, 1>>>();");
20528   EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();"));
20529   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
20530                "aaaaaaaaaaa<<<\n    1, 1>>>();");
20531   verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n"
20532                "    <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();");
20533 }
20534 
20535 TEST_F(FormatTest, MergeLessLessAtEnd) {
20536   verifyFormat("<<");
20537   EXPECT_EQ("< < <", format("\\\n<<<"));
20538   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
20539                "aaallvm::outs() <<");
20540   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
20541                "aaaallvm::outs()\n    <<");
20542 }
20543 
20544 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
20545   std::string code = "#if A\n"
20546                      "#if B\n"
20547                      "a.\n"
20548                      "#endif\n"
20549                      "    a = 1;\n"
20550                      "#else\n"
20551                      "#endif\n"
20552                      "#if C\n"
20553                      "#else\n"
20554                      "#endif\n";
20555   EXPECT_EQ(code, format(code));
20556 }
20557 
20558 TEST_F(FormatTest, HandleConflictMarkers) {
20559   // Git/SVN conflict markers.
20560   EXPECT_EQ("int a;\n"
20561             "void f() {\n"
20562             "  callme(some(parameter1,\n"
20563             "<<<<<<< text by the vcs\n"
20564             "              parameter2),\n"
20565             "||||||| text by the vcs\n"
20566             "              parameter2),\n"
20567             "         parameter3,\n"
20568             "======= text by the vcs\n"
20569             "              parameter2, parameter3),\n"
20570             ">>>>>>> text by the vcs\n"
20571             "         otherparameter);\n",
20572             format("int a;\n"
20573                    "void f() {\n"
20574                    "  callme(some(parameter1,\n"
20575                    "<<<<<<< text by the vcs\n"
20576                    "  parameter2),\n"
20577                    "||||||| text by the vcs\n"
20578                    "  parameter2),\n"
20579                    "  parameter3,\n"
20580                    "======= text by the vcs\n"
20581                    "  parameter2,\n"
20582                    "  parameter3),\n"
20583                    ">>>>>>> text by the vcs\n"
20584                    "  otherparameter);\n"));
20585 
20586   // Perforce markers.
20587   EXPECT_EQ("void f() {\n"
20588             "  function(\n"
20589             ">>>> text by the vcs\n"
20590             "      parameter,\n"
20591             "==== text by the vcs\n"
20592             "      parameter,\n"
20593             "==== text by the vcs\n"
20594             "      parameter,\n"
20595             "<<<< text by the vcs\n"
20596             "      parameter);\n",
20597             format("void f() {\n"
20598                    "  function(\n"
20599                    ">>>> text by the vcs\n"
20600                    "  parameter,\n"
20601                    "==== text by the vcs\n"
20602                    "  parameter,\n"
20603                    "==== text by the vcs\n"
20604                    "  parameter,\n"
20605                    "<<<< text by the vcs\n"
20606                    "  parameter);\n"));
20607 
20608   EXPECT_EQ("<<<<<<<\n"
20609             "|||||||\n"
20610             "=======\n"
20611             ">>>>>>>",
20612             format("<<<<<<<\n"
20613                    "|||||||\n"
20614                    "=======\n"
20615                    ">>>>>>>"));
20616 
20617   EXPECT_EQ("<<<<<<<\n"
20618             "|||||||\n"
20619             "int i;\n"
20620             "=======\n"
20621             ">>>>>>>",
20622             format("<<<<<<<\n"
20623                    "|||||||\n"
20624                    "int i;\n"
20625                    "=======\n"
20626                    ">>>>>>>"));
20627 
20628   // FIXME: Handle parsing of macros around conflict markers correctly:
20629   EXPECT_EQ("#define Macro \\\n"
20630             "<<<<<<<\n"
20631             "Something \\\n"
20632             "|||||||\n"
20633             "Else \\\n"
20634             "=======\n"
20635             "Other \\\n"
20636             ">>>>>>>\n"
20637             "    End int i;\n",
20638             format("#define Macro \\\n"
20639                    "<<<<<<<\n"
20640                    "  Something \\\n"
20641                    "|||||||\n"
20642                    "  Else \\\n"
20643                    "=======\n"
20644                    "  Other \\\n"
20645                    ">>>>>>>\n"
20646                    "  End\n"
20647                    "int i;\n"));
20648 }
20649 
20650 TEST_F(FormatTest, DisableRegions) {
20651   EXPECT_EQ("int i;\n"
20652             "// clang-format off\n"
20653             "  int j;\n"
20654             "// clang-format on\n"
20655             "int k;",
20656             format(" int  i;\n"
20657                    "   // clang-format off\n"
20658                    "  int j;\n"
20659                    " // clang-format on\n"
20660                    "   int   k;"));
20661   EXPECT_EQ("int i;\n"
20662             "/* clang-format off */\n"
20663             "  int j;\n"
20664             "/* clang-format on */\n"
20665             "int k;",
20666             format(" int  i;\n"
20667                    "   /* clang-format off */\n"
20668                    "  int j;\n"
20669                    " /* clang-format on */\n"
20670                    "   int   k;"));
20671 
20672   // Don't reflow comments within disabled regions.
20673   EXPECT_EQ("// clang-format off\n"
20674             "// long long long long long long line\n"
20675             "/* clang-format on */\n"
20676             "/* long long long\n"
20677             " * long long long\n"
20678             " * line */\n"
20679             "int i;\n"
20680             "/* clang-format off */\n"
20681             "/* long long long long long long line */\n",
20682             format("// clang-format off\n"
20683                    "// long long long long long long line\n"
20684                    "/* clang-format on */\n"
20685                    "/* long long long long long long line */\n"
20686                    "int i;\n"
20687                    "/* clang-format off */\n"
20688                    "/* long long long long long long line */\n",
20689                    getLLVMStyleWithColumns(20)));
20690 }
20691 
20692 TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
20693   format("? ) =");
20694   verifyNoCrash("#define a\\\n /**/}");
20695 }
20696 
20697 TEST_F(FormatTest, FormatsTableGenCode) {
20698   FormatStyle Style = getLLVMStyle();
20699   Style.Language = FormatStyle::LK_TableGen;
20700   verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style);
20701 }
20702 
20703 TEST_F(FormatTest, ArrayOfTemplates) {
20704   EXPECT_EQ("auto a = new unique_ptr<int>[10];",
20705             format("auto a = new unique_ptr<int > [ 10];"));
20706 
20707   FormatStyle Spaces = getLLVMStyle();
20708   Spaces.SpacesInSquareBrackets = true;
20709   EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];",
20710             format("auto a = new unique_ptr<int > [10];", Spaces));
20711 }
20712 
20713 TEST_F(FormatTest, ArrayAsTemplateType) {
20714   EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;",
20715             format("auto a = unique_ptr < Foo < Bar>[ 10]> ;"));
20716 
20717   FormatStyle Spaces = getLLVMStyle();
20718   Spaces.SpacesInSquareBrackets = true;
20719   EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;",
20720             format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces));
20721 }
20722 
20723 TEST_F(FormatTest, NoSpaceAfterSuper) { verifyFormat("__super::FooBar();"); }
20724 
20725 TEST(FormatStyle, GetStyleWithEmptyFileName) {
20726   llvm::vfs::InMemoryFileSystem FS;
20727   auto Style1 = getStyle("file", "", "Google", "", &FS);
20728   ASSERT_TRUE((bool)Style1);
20729   ASSERT_EQ(*Style1, getGoogleStyle());
20730 }
20731 
20732 TEST(FormatStyle, GetStyleOfFile) {
20733   llvm::vfs::InMemoryFileSystem FS;
20734   // Test 1: format file in the same directory.
20735   ASSERT_TRUE(
20736       FS.addFile("/a/.clang-format", 0,
20737                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM")));
20738   ASSERT_TRUE(
20739       FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
20740   auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS);
20741   ASSERT_TRUE((bool)Style1);
20742   ASSERT_EQ(*Style1, getLLVMStyle());
20743 
20744   // Test 2.1: fallback to default.
20745   ASSERT_TRUE(
20746       FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
20747   auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS);
20748   ASSERT_TRUE((bool)Style2);
20749   ASSERT_EQ(*Style2, getMozillaStyle());
20750 
20751   // Test 2.2: no format on 'none' fallback style.
20752   Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
20753   ASSERT_TRUE((bool)Style2);
20754   ASSERT_EQ(*Style2, getNoStyle());
20755 
20756   // Test 2.3: format if config is found with no based style while fallback is
20757   // 'none'.
20758   ASSERT_TRUE(FS.addFile("/b/.clang-format", 0,
20759                          llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2")));
20760   Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
20761   ASSERT_TRUE((bool)Style2);
20762   ASSERT_EQ(*Style2, getLLVMStyle());
20763 
20764   // Test 2.4: format if yaml with no based style, while fallback is 'none'.
20765   Style2 = getStyle("{}", "a.h", "none", "", &FS);
20766   ASSERT_TRUE((bool)Style2);
20767   ASSERT_EQ(*Style2, getLLVMStyle());
20768 
20769   // Test 3: format file in parent directory.
20770   ASSERT_TRUE(
20771       FS.addFile("/c/.clang-format", 0,
20772                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google")));
20773   ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0,
20774                          llvm::MemoryBuffer::getMemBuffer("int i;")));
20775   auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS);
20776   ASSERT_TRUE((bool)Style3);
20777   ASSERT_EQ(*Style3, getGoogleStyle());
20778 
20779   // Test 4: error on invalid fallback style
20780   auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS);
20781   ASSERT_FALSE((bool)Style4);
20782   llvm::consumeError(Style4.takeError());
20783 
20784   // Test 5: error on invalid yaml on command line
20785   auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS);
20786   ASSERT_FALSE((bool)Style5);
20787   llvm::consumeError(Style5.takeError());
20788 
20789   // Test 6: error on invalid style
20790   auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS);
20791   ASSERT_FALSE((bool)Style6);
20792   llvm::consumeError(Style6.takeError());
20793 
20794   // Test 7: found config file, error on parsing it
20795   ASSERT_TRUE(
20796       FS.addFile("/d/.clang-format", 0,
20797                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n"
20798                                                   "InvalidKey: InvalidValue")));
20799   ASSERT_TRUE(
20800       FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
20801   auto Style7a = getStyle("file", "/d/.clang-format", "LLVM", "", &FS);
20802   ASSERT_FALSE((bool)Style7a);
20803   llvm::consumeError(Style7a.takeError());
20804 
20805   auto Style7b = getStyle("file", "/d/.clang-format", "LLVM", "", &FS, true);
20806   ASSERT_TRUE((bool)Style7b);
20807 
20808   // Test 8: inferred per-language defaults apply.
20809   auto StyleTd = getStyle("file", "x.td", "llvm", "", &FS);
20810   ASSERT_TRUE((bool)StyleTd);
20811   ASSERT_EQ(*StyleTd, getLLVMStyle(FormatStyle::LK_TableGen));
20812 
20813   // Test 9.1: overwriting a file style, when parent no file exists with no
20814   // fallback style
20815   ASSERT_TRUE(FS.addFile(
20816       "/e/sub/.clang-format", 0,
20817       llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: InheritParentConfig\n"
20818                                        "ColumnLimit: 20")));
20819   ASSERT_TRUE(FS.addFile("/e/sub/code.cpp", 0,
20820                          llvm::MemoryBuffer::getMemBuffer("int i;")));
20821   auto Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS);
20822   ASSERT_TRUE(static_cast<bool>(Style9));
20823   ASSERT_EQ(*Style9, [] {
20824     auto Style = getNoStyle();
20825     Style.ColumnLimit = 20;
20826     return Style;
20827   }());
20828 
20829   // Test 9.2: with LLVM fallback style
20830   Style9 = getStyle("file", "/e/sub/code.cpp", "LLVM", "", &FS);
20831   ASSERT_TRUE(static_cast<bool>(Style9));
20832   ASSERT_EQ(*Style9, [] {
20833     auto Style = getLLVMStyle();
20834     Style.ColumnLimit = 20;
20835     return Style;
20836   }());
20837 
20838   // Test 9.3: with a parent file
20839   ASSERT_TRUE(
20840       FS.addFile("/e/.clang-format", 0,
20841                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google\n"
20842                                                   "UseTab: Always")));
20843   Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS);
20844   ASSERT_TRUE(static_cast<bool>(Style9));
20845   ASSERT_EQ(*Style9, [] {
20846     auto Style = getGoogleStyle();
20847     Style.ColumnLimit = 20;
20848     Style.UseTab = FormatStyle::UT_Always;
20849     return Style;
20850   }());
20851 
20852   // Test 9.4: propagate more than one level
20853   ASSERT_TRUE(FS.addFile("/e/sub/sub/code.cpp", 0,
20854                          llvm::MemoryBuffer::getMemBuffer("int i;")));
20855   ASSERT_TRUE(FS.addFile("/e/sub/sub/.clang-format", 0,
20856                          llvm::MemoryBuffer::getMemBuffer(
20857                              "BasedOnStyle: InheritParentConfig\n"
20858                              "WhitespaceSensitiveMacros: ['FOO', 'BAR']")));
20859   std::vector<std::string> NonDefaultWhiteSpaceMacros{"FOO", "BAR"};
20860 
20861   const auto SubSubStyle = [&NonDefaultWhiteSpaceMacros] {
20862     auto Style = getGoogleStyle();
20863     Style.ColumnLimit = 20;
20864     Style.UseTab = FormatStyle::UT_Always;
20865     Style.WhitespaceSensitiveMacros = NonDefaultWhiteSpaceMacros;
20866     return Style;
20867   }();
20868 
20869   ASSERT_NE(Style9->WhitespaceSensitiveMacros, NonDefaultWhiteSpaceMacros);
20870   Style9 = getStyle("file", "/e/sub/sub/code.cpp", "none", "", &FS);
20871   ASSERT_TRUE(static_cast<bool>(Style9));
20872   ASSERT_EQ(*Style9, SubSubStyle);
20873 
20874   // Test 9.5: use InheritParentConfig as style name
20875   Style9 =
20876       getStyle("inheritparentconfig", "/e/sub/sub/code.cpp", "none", "", &FS);
20877   ASSERT_TRUE(static_cast<bool>(Style9));
20878   ASSERT_EQ(*Style9, SubSubStyle);
20879 
20880   // Test 9.6: use command line style with inheritance
20881   Style9 = getStyle("{BasedOnStyle: InheritParentConfig}", "/e/sub/code.cpp",
20882                     "none", "", &FS);
20883   ASSERT_TRUE(static_cast<bool>(Style9));
20884   ASSERT_EQ(*Style9, SubSubStyle);
20885 
20886   // Test 9.7: use command line style with inheritance and own config
20887   Style9 = getStyle("{BasedOnStyle: InheritParentConfig, "
20888                     "WhitespaceSensitiveMacros: ['FOO', 'BAR']}",
20889                     "/e/sub/code.cpp", "none", "", &FS);
20890   ASSERT_TRUE(static_cast<bool>(Style9));
20891   ASSERT_EQ(*Style9, SubSubStyle);
20892 
20893   // Test 9.8: use inheritance from a file without BasedOnStyle
20894   ASSERT_TRUE(FS.addFile("/e/withoutbase/.clang-format", 0,
20895                          llvm::MemoryBuffer::getMemBuffer("ColumnLimit: 123")));
20896   ASSERT_TRUE(
20897       FS.addFile("/e/withoutbase/sub/.clang-format", 0,
20898                  llvm::MemoryBuffer::getMemBuffer(
20899                      "BasedOnStyle: InheritParentConfig\nIndentWidth: 7")));
20900   // Make sure we do not use the fallback style
20901   Style9 = getStyle("file", "/e/withoutbase/code.cpp", "google", "", &FS);
20902   ASSERT_TRUE(static_cast<bool>(Style9));
20903   ASSERT_EQ(*Style9, [] {
20904     auto Style = getLLVMStyle();
20905     Style.ColumnLimit = 123;
20906     return Style;
20907   }());
20908 
20909   Style9 = getStyle("file", "/e/withoutbase/sub/code.cpp", "google", "", &FS);
20910   ASSERT_TRUE(static_cast<bool>(Style9));
20911   ASSERT_EQ(*Style9, [] {
20912     auto Style = getLLVMStyle();
20913     Style.ColumnLimit = 123;
20914     Style.IndentWidth = 7;
20915     return Style;
20916   }());
20917 }
20918 
20919 TEST_F(ReplacementTest, FormatCodeAfterReplacements) {
20920   // Column limit is 20.
20921   std::string Code = "Type *a =\n"
20922                      "    new Type();\n"
20923                      "g(iiiii, 0, jjjjj,\n"
20924                      "  0, kkkkk, 0, mm);\n"
20925                      "int  bad     = format   ;";
20926   std::string Expected = "auto a = new Type();\n"
20927                          "g(iiiii, nullptr,\n"
20928                          "  jjjjj, nullptr,\n"
20929                          "  kkkkk, nullptr,\n"
20930                          "  mm);\n"
20931                          "int  bad     = format   ;";
20932   FileID ID = Context.createInMemoryFile("format.cpp", Code);
20933   tooling::Replacements Replaces = toReplacements(
20934       {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6,
20935                             "auto "),
20936        tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1,
20937                             "nullptr"),
20938        tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1,
20939                             "nullptr"),
20940        tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1,
20941                             "nullptr")});
20942 
20943   format::FormatStyle Style = format::getLLVMStyle();
20944   Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility.
20945   auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
20946   EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
20947       << llvm::toString(FormattedReplaces.takeError()) << "\n";
20948   auto Result = applyAllReplacements(Code, *FormattedReplaces);
20949   EXPECT_TRUE(static_cast<bool>(Result));
20950   EXPECT_EQ(Expected, *Result);
20951 }
20952 
20953 TEST_F(ReplacementTest, SortIncludesAfterReplacement) {
20954   std::string Code = "#include \"a.h\"\n"
20955                      "#include \"c.h\"\n"
20956                      "\n"
20957                      "int main() {\n"
20958                      "  return 0;\n"
20959                      "}";
20960   std::string Expected = "#include \"a.h\"\n"
20961                          "#include \"b.h\"\n"
20962                          "#include \"c.h\"\n"
20963                          "\n"
20964                          "int main() {\n"
20965                          "  return 0;\n"
20966                          "}";
20967   FileID ID = Context.createInMemoryFile("fix.cpp", Code);
20968   tooling::Replacements Replaces = toReplacements(
20969       {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0,
20970                             "#include \"b.h\"\n")});
20971 
20972   format::FormatStyle Style = format::getLLVMStyle();
20973   Style.SortIncludes = FormatStyle::SI_CaseSensitive;
20974   auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
20975   EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
20976       << llvm::toString(FormattedReplaces.takeError()) << "\n";
20977   auto Result = applyAllReplacements(Code, *FormattedReplaces);
20978   EXPECT_TRUE(static_cast<bool>(Result));
20979   EXPECT_EQ(Expected, *Result);
20980 }
20981 
20982 TEST_F(FormatTest, FormatSortsUsingDeclarations) {
20983   EXPECT_EQ("using std::cin;\n"
20984             "using std::cout;",
20985             format("using std::cout;\n"
20986                    "using std::cin;",
20987                    getGoogleStyle()));
20988 }
20989 
20990 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) {
20991   format::FormatStyle Style = format::getLLVMStyle();
20992   Style.Standard = FormatStyle::LS_Cpp03;
20993   // cpp03 recognize this string as identifier u8 and literal character 'a'
20994   EXPECT_EQ("auto c = u8 'a';", format("auto c = u8'a';", Style));
20995 }
20996 
20997 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) {
20998   // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers
20999   // all modes, including C++11, C++14 and C++17
21000   EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';"));
21001 }
21002 
21003 TEST_F(FormatTest, DoNotFormatLikelyXml) {
21004   EXPECT_EQ("<!-- ;> -->", format("<!-- ;> -->", getGoogleStyle()));
21005   EXPECT_EQ(" <!-- >; -->", format(" <!-- >; -->", getGoogleStyle()));
21006 }
21007 
21008 TEST_F(FormatTest, StructuredBindings) {
21009   // Structured bindings is a C++17 feature.
21010   // all modes, including C++11, C++14 and C++17
21011   verifyFormat("auto [a, b] = f();");
21012   EXPECT_EQ("auto [a, b] = f();", format("auto[a, b] = f();"));
21013   EXPECT_EQ("const auto [a, b] = f();", format("const   auto[a, b] = f();"));
21014   EXPECT_EQ("auto const [a, b] = f();", format("auto  const[a, b] = f();"));
21015   EXPECT_EQ("auto const volatile [a, b] = f();",
21016             format("auto  const   volatile[a, b] = f();"));
21017   EXPECT_EQ("auto [a, b, c] = f();", format("auto   [  a  ,  b,c   ] = f();"));
21018   EXPECT_EQ("auto &[a, b, c] = f();",
21019             format("auto   &[  a  ,  b,c   ] = f();"));
21020   EXPECT_EQ("auto &&[a, b, c] = f();",
21021             format("auto   &&[  a  ,  b,c   ] = f();"));
21022   EXPECT_EQ("auto const &[a, b] = f();", format("auto  const&[a, b] = f();"));
21023   EXPECT_EQ("auto const volatile &&[a, b] = f();",
21024             format("auto  const  volatile  &&[a, b] = f();"));
21025   EXPECT_EQ("auto const &&[a, b] = f();",
21026             format("auto  const   &&  [a, b] = f();"));
21027   EXPECT_EQ("const auto &[a, b] = f();",
21028             format("const  auto  &  [a, b] = f();"));
21029   EXPECT_EQ("const auto volatile &&[a, b] = f();",
21030             format("const  auto   volatile  &&[a, b] = f();"));
21031   EXPECT_EQ("volatile const auto &&[a, b] = f();",
21032             format("volatile  const  auto   &&[a, b] = f();"));
21033   EXPECT_EQ("const auto &&[a, b] = f();",
21034             format("const  auto  &&  [a, b] = f();"));
21035 
21036   // Make sure we don't mistake structured bindings for lambdas.
21037   FormatStyle PointerMiddle = getLLVMStyle();
21038   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
21039   verifyFormat("auto [a1, b]{A * i};", getGoogleStyle());
21040   verifyFormat("auto [a2, b]{A * i};", getLLVMStyle());
21041   verifyFormat("auto [a3, b]{A * i};", PointerMiddle);
21042   verifyFormat("auto const [a1, b]{A * i};", getGoogleStyle());
21043   verifyFormat("auto const [a2, b]{A * i};", getLLVMStyle());
21044   verifyFormat("auto const [a3, b]{A * i};", PointerMiddle);
21045   verifyFormat("auto const& [a1, b]{A * i};", getGoogleStyle());
21046   verifyFormat("auto const &[a2, b]{A * i};", getLLVMStyle());
21047   verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle);
21048   verifyFormat("auto const&& [a1, b]{A * i};", getGoogleStyle());
21049   verifyFormat("auto const &&[a2, b]{A * i};", getLLVMStyle());
21050   verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle);
21051 
21052   EXPECT_EQ("for (const auto &&[a, b] : some_range) {\n}",
21053             format("for (const auto   &&   [a, b] : some_range) {\n}"));
21054   EXPECT_EQ("for (const auto &[a, b] : some_range) {\n}",
21055             format("for (const auto   &   [a, b] : some_range) {\n}"));
21056   EXPECT_EQ("for (const auto [a, b] : some_range) {\n}",
21057             format("for (const auto[a, b] : some_range) {\n}"));
21058   EXPECT_EQ("auto [x, y](expr);", format("auto[x,y]  (expr);"));
21059   EXPECT_EQ("auto &[x, y](expr);", format("auto  &  [x,y]  (expr);"));
21060   EXPECT_EQ("auto &&[x, y](expr);", format("auto  &&  [x,y]  (expr);"));
21061   EXPECT_EQ("auto const &[x, y](expr);",
21062             format("auto  const  &  [x,y]  (expr);"));
21063   EXPECT_EQ("auto const &&[x, y](expr);",
21064             format("auto  const  &&  [x,y]  (expr);"));
21065   EXPECT_EQ("auto [x, y]{expr};", format("auto[x,y]     {expr};"));
21066   EXPECT_EQ("auto const &[x, y]{expr};",
21067             format("auto  const  &  [x,y]  {expr};"));
21068   EXPECT_EQ("auto const &&[x, y]{expr};",
21069             format("auto  const  &&  [x,y]  {expr};"));
21070 
21071   format::FormatStyle Spaces = format::getLLVMStyle();
21072   Spaces.SpacesInSquareBrackets = true;
21073   verifyFormat("auto [ a, b ] = f();", Spaces);
21074   verifyFormat("auto &&[ a, b ] = f();", Spaces);
21075   verifyFormat("auto &[ a, b ] = f();", Spaces);
21076   verifyFormat("auto const &&[ a, b ] = f();", Spaces);
21077   verifyFormat("auto const &[ a, b ] = f();", Spaces);
21078 }
21079 
21080 TEST_F(FormatTest, FileAndCode) {
21081   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.cc", ""));
21082   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.m", ""));
21083   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.mm", ""));
21084   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", ""));
21085   EXPECT_EQ(FormatStyle::LK_ObjC,
21086             guessLanguage("foo.h", "@interface Foo\n@end\n"));
21087   EXPECT_EQ(
21088       FormatStyle::LK_ObjC,
21089       guessLanguage("foo.h", "#define TRY(x, y) @try { x; } @finally { y; }"));
21090   EXPECT_EQ(FormatStyle::LK_ObjC,
21091             guessLanguage("foo.h", "#define AVAIL(x) @available(x, *))"));
21092   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@class Foo;"));
21093   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo", ""));
21094   EXPECT_EQ(FormatStyle::LK_ObjC,
21095             guessLanguage("foo", "@interface Foo\n@end\n"));
21096   EXPECT_EQ(FormatStyle::LK_ObjC,
21097             guessLanguage("foo.h", "int DoStuff(CGRect rect);\n"));
21098   EXPECT_EQ(
21099       FormatStyle::LK_ObjC,
21100       guessLanguage("foo.h",
21101                     "#define MY_POINT_MAKE(x, y) CGPointMake((x), (y));\n"));
21102   EXPECT_EQ(
21103       FormatStyle::LK_Cpp,
21104       guessLanguage("foo.h", "#define FOO(...) auto bar = [] __VA_ARGS__;"));
21105 }
21106 
21107 TEST_F(FormatTest, GuessLanguageWithCpp11AttributeSpecifiers) {
21108   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[noreturn]];"));
21109   EXPECT_EQ(FormatStyle::LK_ObjC,
21110             guessLanguage("foo.h", "array[[calculator getIndex]];"));
21111   EXPECT_EQ(FormatStyle::LK_Cpp,
21112             guessLanguage("foo.h", "[[noreturn, deprecated(\"so sorry\")]];"));
21113   EXPECT_EQ(
21114       FormatStyle::LK_Cpp,
21115       guessLanguage("foo.h", "[[noreturn, deprecated(\"gone, sorry\")]];"));
21116   EXPECT_EQ(FormatStyle::LK_ObjC,
21117             guessLanguage("foo.h", "[[noreturn foo] bar];"));
21118   EXPECT_EQ(FormatStyle::LK_Cpp,
21119             guessLanguage("foo.h", "[[clang::fallthrough]];"));
21120   EXPECT_EQ(FormatStyle::LK_ObjC,
21121             guessLanguage("foo.h", "[[clang:fallthrough] foo];"));
21122   EXPECT_EQ(FormatStyle::LK_Cpp,
21123             guessLanguage("foo.h", "[[gsl::suppress(\"type\")]];"));
21124   EXPECT_EQ(FormatStyle::LK_Cpp,
21125             guessLanguage("foo.h", "[[using clang: fallthrough]];"));
21126   EXPECT_EQ(FormatStyle::LK_ObjC,
21127             guessLanguage("foo.h", "[[abusing clang:fallthrough] bar];"));
21128   EXPECT_EQ(FormatStyle::LK_Cpp,
21129             guessLanguage("foo.h", "[[using gsl: suppress(\"type\")]];"));
21130   EXPECT_EQ(
21131       FormatStyle::LK_Cpp,
21132       guessLanguage("foo.h", "for (auto &&[endpoint, stream] : streams_)"));
21133   EXPECT_EQ(
21134       FormatStyle::LK_Cpp,
21135       guessLanguage("foo.h",
21136                     "[[clang::callable_when(\"unconsumed\", \"unknown\")]]"));
21137   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[foo::bar, ...]]"));
21138 }
21139 
21140 TEST_F(FormatTest, GuessLanguageWithCaret) {
21141   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^);"));
21142   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^, Bar);"));
21143   EXPECT_EQ(FormatStyle::LK_ObjC,
21144             guessLanguage("foo.h", "int(^)(char, float);"));
21145   EXPECT_EQ(FormatStyle::LK_ObjC,
21146             guessLanguage("foo.h", "int(^foo)(char, float);"));
21147   EXPECT_EQ(FormatStyle::LK_ObjC,
21148             guessLanguage("foo.h", "int(^foo[10])(char, float);"));
21149   EXPECT_EQ(FormatStyle::LK_ObjC,
21150             guessLanguage("foo.h", "int(^foo[kNumEntries])(char, float);"));
21151   EXPECT_EQ(
21152       FormatStyle::LK_ObjC,
21153       guessLanguage("foo.h", "int(^foo[(kNumEntries + 10)])(char, float);"));
21154 }
21155 
21156 TEST_F(FormatTest, GuessLanguageWithPragmas) {
21157   EXPECT_EQ(FormatStyle::LK_Cpp,
21158             guessLanguage("foo.h", "__pragma(warning(disable:))"));
21159   EXPECT_EQ(FormatStyle::LK_Cpp,
21160             guessLanguage("foo.h", "#pragma(warning(disable:))"));
21161   EXPECT_EQ(FormatStyle::LK_Cpp,
21162             guessLanguage("foo.h", "_Pragma(warning(disable:))"));
21163 }
21164 
21165 TEST_F(FormatTest, FormatsInlineAsmSymbolicNames) {
21166   // ASM symbolic names are identifiers that must be surrounded by [] without
21167   // space in between:
21168   // https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#InputOperands
21169 
21170   // Example from https://bugs.llvm.org/show_bug.cgi?id=45108.
21171   verifyFormat(R"(//
21172 asm volatile("mrs %x[result], FPCR" : [result] "=r"(result));
21173 )");
21174 
21175   // A list of several ASM symbolic names.
21176   verifyFormat(R"(asm("mov %[e], %[d]" : [d] "=rm"(d), [e] "rm"(*e));)");
21177 
21178   // ASM symbolic names in inline ASM with inputs and outputs.
21179   verifyFormat(R"(//
21180 asm("cmoveq %1, %2, %[result]"
21181     : [result] "=r"(result)
21182     : "r"(test), "r"(new), "[result]"(old));
21183 )");
21184 
21185   // ASM symbolic names in inline ASM with no outputs.
21186   verifyFormat(R"(asm("mov %[e], %[d]" : : [d] "=rm"(d), [e] "rm"(*e));)");
21187 }
21188 
21189 TEST_F(FormatTest, GuessedLanguageWithInlineAsmClobbers) {
21190   EXPECT_EQ(FormatStyle::LK_Cpp,
21191             guessLanguage("foo.h", "void f() {\n"
21192                                    "  asm (\"mov %[e], %[d]\"\n"
21193                                    "     : [d] \"=rm\" (d)\n"
21194                                    "       [e] \"rm\" (*e));\n"
21195                                    "}"));
21196   EXPECT_EQ(FormatStyle::LK_Cpp,
21197             guessLanguage("foo.h", "void f() {\n"
21198                                    "  _asm (\"mov %[e], %[d]\"\n"
21199                                    "     : [d] \"=rm\" (d)\n"
21200                                    "       [e] \"rm\" (*e));\n"
21201                                    "}"));
21202   EXPECT_EQ(FormatStyle::LK_Cpp,
21203             guessLanguage("foo.h", "void f() {\n"
21204                                    "  __asm (\"mov %[e], %[d]\"\n"
21205                                    "     : [d] \"=rm\" (d)\n"
21206                                    "       [e] \"rm\" (*e));\n"
21207                                    "}"));
21208   EXPECT_EQ(FormatStyle::LK_Cpp,
21209             guessLanguage("foo.h", "void f() {\n"
21210                                    "  __asm__ (\"mov %[e], %[d]\"\n"
21211                                    "     : [d] \"=rm\" (d)\n"
21212                                    "       [e] \"rm\" (*e));\n"
21213                                    "}"));
21214   EXPECT_EQ(FormatStyle::LK_Cpp,
21215             guessLanguage("foo.h", "void f() {\n"
21216                                    "  asm (\"mov %[e], %[d]\"\n"
21217                                    "     : [d] \"=rm\" (d),\n"
21218                                    "       [e] \"rm\" (*e));\n"
21219                                    "}"));
21220   EXPECT_EQ(FormatStyle::LK_Cpp,
21221             guessLanguage("foo.h", "void f() {\n"
21222                                    "  asm volatile (\"mov %[e], %[d]\"\n"
21223                                    "     : [d] \"=rm\" (d)\n"
21224                                    "       [e] \"rm\" (*e));\n"
21225                                    "}"));
21226 }
21227 
21228 TEST_F(FormatTest, GuessLanguageWithChildLines) {
21229   EXPECT_EQ(FormatStyle::LK_Cpp,
21230             guessLanguage("foo.h", "#define FOO ({ std::string s; })"));
21231   EXPECT_EQ(FormatStyle::LK_ObjC,
21232             guessLanguage("foo.h", "#define FOO ({ NSString *s; })"));
21233   EXPECT_EQ(
21234       FormatStyle::LK_Cpp,
21235       guessLanguage("foo.h", "#define FOO ({ foo(); ({ std::string s; }) })"));
21236   EXPECT_EQ(
21237       FormatStyle::LK_ObjC,
21238       guessLanguage("foo.h", "#define FOO ({ foo(); ({ NSString *s; }) })"));
21239 }
21240 
21241 TEST_F(FormatTest, TypenameMacros) {
21242   std::vector<std::string> TypenameMacros = {"STACK_OF", "LIST", "TAILQ_ENTRY"};
21243 
21244   // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=30353
21245   FormatStyle Google = getGoogleStyleWithColumns(0);
21246   Google.TypenameMacros = TypenameMacros;
21247   verifyFormat("struct foo {\n"
21248                "  int bar;\n"
21249                "  TAILQ_ENTRY(a) bleh;\n"
21250                "};",
21251                Google);
21252 
21253   FormatStyle Macros = getLLVMStyle();
21254   Macros.TypenameMacros = TypenameMacros;
21255 
21256   verifyFormat("STACK_OF(int) a;", Macros);
21257   verifyFormat("STACK_OF(int) *a;", Macros);
21258   verifyFormat("STACK_OF(int const *) *a;", Macros);
21259   verifyFormat("STACK_OF(int *const) *a;", Macros);
21260   verifyFormat("STACK_OF(int, string) a;", Macros);
21261   verifyFormat("STACK_OF(LIST(int)) a;", Macros);
21262   verifyFormat("STACK_OF(LIST(int)) a, b;", Macros);
21263   verifyFormat("for (LIST(int) *a = NULL; a;) {\n}", Macros);
21264   verifyFormat("STACK_OF(int) f(LIST(int) *arg);", Macros);
21265   verifyFormat("vector<LIST(uint64_t) *attr> x;", Macros);
21266   verifyFormat("vector<LIST(uint64_t) *const> f(LIST(uint64_t) *arg);", Macros);
21267 
21268   Macros.PointerAlignment = FormatStyle::PAS_Left;
21269   verifyFormat("STACK_OF(int)* a;", Macros);
21270   verifyFormat("STACK_OF(int*)* a;", Macros);
21271   verifyFormat("x = (STACK_OF(uint64_t))*a;", Macros);
21272   verifyFormat("x = (STACK_OF(uint64_t))&a;", Macros);
21273   verifyFormat("vector<STACK_OF(uint64_t)* attr> x;", Macros);
21274 }
21275 
21276 TEST_F(FormatTest, AtomicQualifier) {
21277   // Check that we treate _Atomic as a type and not a function call
21278   FormatStyle Google = getGoogleStyleWithColumns(0);
21279   verifyFormat("struct foo {\n"
21280                "  int a1;\n"
21281                "  _Atomic(a) a2;\n"
21282                "  _Atomic(_Atomic(int) *const) a3;\n"
21283                "};",
21284                Google);
21285   verifyFormat("_Atomic(uint64_t) a;");
21286   verifyFormat("_Atomic(uint64_t) *a;");
21287   verifyFormat("_Atomic(uint64_t const *) *a;");
21288   verifyFormat("_Atomic(uint64_t *const) *a;");
21289   verifyFormat("_Atomic(const uint64_t *) *a;");
21290   verifyFormat("_Atomic(uint64_t) a;");
21291   verifyFormat("_Atomic(_Atomic(uint64_t)) a;");
21292   verifyFormat("_Atomic(_Atomic(uint64_t)) a, b;");
21293   verifyFormat("for (_Atomic(uint64_t) *a = NULL; a;) {\n}");
21294   verifyFormat("_Atomic(uint64_t) f(_Atomic(uint64_t) *arg);");
21295 
21296   verifyFormat("_Atomic(uint64_t) *s(InitValue);");
21297   verifyFormat("_Atomic(uint64_t) *s{InitValue};");
21298   FormatStyle Style = getLLVMStyle();
21299   Style.PointerAlignment = FormatStyle::PAS_Left;
21300   verifyFormat("_Atomic(uint64_t)* s(InitValue);", Style);
21301   verifyFormat("_Atomic(uint64_t)* s{InitValue};", Style);
21302   verifyFormat("_Atomic(int)* a;", Style);
21303   verifyFormat("_Atomic(int*)* a;", Style);
21304   verifyFormat("vector<_Atomic(uint64_t)* attr> x;", Style);
21305 
21306   Style.SpacesInCStyleCastParentheses = true;
21307   Style.SpacesInParentheses = false;
21308   verifyFormat("x = ( _Atomic(uint64_t) )*a;", Style);
21309   Style.SpacesInCStyleCastParentheses = false;
21310   Style.SpacesInParentheses = true;
21311   verifyFormat("x = (_Atomic( uint64_t ))*a;", Style);
21312   verifyFormat("x = (_Atomic( uint64_t ))&a;", Style);
21313 }
21314 
21315 TEST_F(FormatTest, AmbersandInLamda) {
21316   // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=41899
21317   FormatStyle AlignStyle = getLLVMStyle();
21318   AlignStyle.PointerAlignment = FormatStyle::PAS_Left;
21319   verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle);
21320   AlignStyle.PointerAlignment = FormatStyle::PAS_Right;
21321   verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle);
21322 }
21323 
21324 TEST_F(FormatTest, SpacesInConditionalStatement) {
21325   FormatStyle Spaces = getLLVMStyle();
21326   Spaces.IfMacros.clear();
21327   Spaces.IfMacros.push_back("MYIF");
21328   Spaces.SpacesInConditionalStatement = true;
21329   verifyFormat("for ( int i = 0; i; i++ )\n  continue;", Spaces);
21330   verifyFormat("if ( !a )\n  return;", Spaces);
21331   verifyFormat("if ( a )\n  return;", Spaces);
21332   verifyFormat("if constexpr ( a )\n  return;", Spaces);
21333   verifyFormat("MYIF ( a )\n  return;", Spaces);
21334   verifyFormat("MYIF ( a )\n  return;\nelse MYIF ( b )\n  return;", Spaces);
21335   verifyFormat("MYIF ( a )\n  return;\nelse\n  return;", Spaces);
21336   verifyFormat("switch ( a )\ncase 1:\n  return;", Spaces);
21337   verifyFormat("while ( a )\n  return;", Spaces);
21338   verifyFormat("while ( (a && b) )\n  return;", Spaces);
21339   verifyFormat("do {\n} while ( 1 != 0 );", Spaces);
21340   verifyFormat("try {\n} catch ( const std::exception & ) {\n}", Spaces);
21341   // Check that space on the left of "::" is inserted as expected at beginning
21342   // of condition.
21343   verifyFormat("while ( ::func() )\n  return;", Spaces);
21344 
21345   // Check impact of ControlStatementsExceptControlMacros is honored.
21346   Spaces.SpaceBeforeParens =
21347       FormatStyle::SBPO_ControlStatementsExceptControlMacros;
21348   verifyFormat("MYIF( a )\n  return;", Spaces);
21349   verifyFormat("MYIF( a )\n  return;\nelse MYIF( b )\n  return;", Spaces);
21350   verifyFormat("MYIF( a )\n  return;\nelse\n  return;", Spaces);
21351 }
21352 
21353 TEST_F(FormatTest, AlternativeOperators) {
21354   // Test case for ensuring alternate operators are not
21355   // combined with their right most neighbour.
21356   verifyFormat("int a and b;");
21357   verifyFormat("int a and_eq b;");
21358   verifyFormat("int a bitand b;");
21359   verifyFormat("int a bitor b;");
21360   verifyFormat("int a compl b;");
21361   verifyFormat("int a not b;");
21362   verifyFormat("int a not_eq b;");
21363   verifyFormat("int a or b;");
21364   verifyFormat("int a xor b;");
21365   verifyFormat("int a xor_eq b;");
21366   verifyFormat("return this not_eq bitand other;");
21367   verifyFormat("bool operator not_eq(const X bitand other)");
21368 
21369   verifyFormat("int a and 5;");
21370   verifyFormat("int a and_eq 5;");
21371   verifyFormat("int a bitand 5;");
21372   verifyFormat("int a bitor 5;");
21373   verifyFormat("int a compl 5;");
21374   verifyFormat("int a not 5;");
21375   verifyFormat("int a not_eq 5;");
21376   verifyFormat("int a or 5;");
21377   verifyFormat("int a xor 5;");
21378   verifyFormat("int a xor_eq 5;");
21379 
21380   verifyFormat("int a compl(5);");
21381   verifyFormat("int a not(5);");
21382 
21383   /* FIXME handle alternate tokens
21384    * https://en.cppreference.com/w/cpp/language/operator_alternative
21385   // alternative tokens
21386   verifyFormat("compl foo();");     //  ~foo();
21387   verifyFormat("foo() <%%>;");      // foo();
21388   verifyFormat("void foo() <%%>;"); // void foo(){}
21389   verifyFormat("int a <:1:>;");     // int a[1];[
21390   verifyFormat("%:define ABC abc"); // #define ABC abc
21391   verifyFormat("%:%:");             // ##
21392   */
21393 }
21394 
21395 TEST_F(FormatTest, STLWhileNotDefineChed) {
21396   verifyFormat("#if defined(while)\n"
21397                "#define while EMIT WARNING C4005\n"
21398                "#endif // while");
21399 }
21400 
21401 TEST_F(FormatTest, OperatorSpacing) {
21402   FormatStyle Style = getLLVMStyle();
21403   Style.PointerAlignment = FormatStyle::PAS_Right;
21404   verifyFormat("Foo::operator*();", Style);
21405   verifyFormat("Foo::operator void *();", Style);
21406   verifyFormat("Foo::operator void **();", Style);
21407   verifyFormat("Foo::operator void *&();", Style);
21408   verifyFormat("Foo::operator void *&&();", Style);
21409   verifyFormat("Foo::operator void const *();", Style);
21410   verifyFormat("Foo::operator void const **();", Style);
21411   verifyFormat("Foo::operator void const *&();", Style);
21412   verifyFormat("Foo::operator void const *&&();", Style);
21413   verifyFormat("Foo::operator()(void *);", Style);
21414   verifyFormat("Foo::operator*(void *);", Style);
21415   verifyFormat("Foo::operator*();", Style);
21416   verifyFormat("Foo::operator**();", Style);
21417   verifyFormat("Foo::operator&();", Style);
21418   verifyFormat("Foo::operator<int> *();", Style);
21419   verifyFormat("Foo::operator<Foo> *();", Style);
21420   verifyFormat("Foo::operator<int> **();", Style);
21421   verifyFormat("Foo::operator<Foo> **();", Style);
21422   verifyFormat("Foo::operator<int> &();", Style);
21423   verifyFormat("Foo::operator<Foo> &();", Style);
21424   verifyFormat("Foo::operator<int> &&();", Style);
21425   verifyFormat("Foo::operator<Foo> &&();", Style);
21426   verifyFormat("Foo::operator<int> *&();", Style);
21427   verifyFormat("Foo::operator<Foo> *&();", Style);
21428   verifyFormat("Foo::operator<int> *&&();", Style);
21429   verifyFormat("Foo::operator<Foo> *&&();", Style);
21430   verifyFormat("operator*(int (*)(), class Foo);", Style);
21431 
21432   verifyFormat("Foo::operator&();", Style);
21433   verifyFormat("Foo::operator void &();", Style);
21434   verifyFormat("Foo::operator void const &();", Style);
21435   verifyFormat("Foo::operator()(void &);", Style);
21436   verifyFormat("Foo::operator&(void &);", Style);
21437   verifyFormat("Foo::operator&();", Style);
21438   verifyFormat("operator&(int (&)(), class Foo);", Style);
21439 
21440   verifyFormat("Foo::operator&&();", Style);
21441   verifyFormat("Foo::operator**();", Style);
21442   verifyFormat("Foo::operator void &&();", Style);
21443   verifyFormat("Foo::operator void const &&();", Style);
21444   verifyFormat("Foo::operator()(void &&);", Style);
21445   verifyFormat("Foo::operator&&(void &&);", Style);
21446   verifyFormat("Foo::operator&&();", Style);
21447   verifyFormat("operator&&(int(&&)(), class Foo);", Style);
21448   verifyFormat("operator const nsTArrayRight<E> &()", Style);
21449   verifyFormat("[[nodiscard]] operator const nsTArrayRight<E, Allocator> &()",
21450                Style);
21451   verifyFormat("operator void **()", Style);
21452   verifyFormat("operator const FooRight<Object> &()", Style);
21453   verifyFormat("operator const FooRight<Object> *()", Style);
21454   verifyFormat("operator const FooRight<Object> **()", Style);
21455   verifyFormat("operator const FooRight<Object> *&()", Style);
21456   verifyFormat("operator const FooRight<Object> *&&()", Style);
21457 
21458   Style.PointerAlignment = FormatStyle::PAS_Left;
21459   verifyFormat("Foo::operator*();", Style);
21460   verifyFormat("Foo::operator**();", Style);
21461   verifyFormat("Foo::operator void*();", Style);
21462   verifyFormat("Foo::operator void**();", Style);
21463   verifyFormat("Foo::operator void*&();", Style);
21464   verifyFormat("Foo::operator void*&&();", Style);
21465   verifyFormat("Foo::operator void const*();", Style);
21466   verifyFormat("Foo::operator void const**();", Style);
21467   verifyFormat("Foo::operator void const*&();", Style);
21468   verifyFormat("Foo::operator void const*&&();", Style);
21469   verifyFormat("Foo::operator/*comment*/ void*();", Style);
21470   verifyFormat("Foo::operator/*a*/ const /*b*/ void*();", Style);
21471   verifyFormat("Foo::operator/*a*/ volatile /*b*/ void*();", Style);
21472   verifyFormat("Foo::operator()(void*);", Style);
21473   verifyFormat("Foo::operator*(void*);", Style);
21474   verifyFormat("Foo::operator*();", Style);
21475   verifyFormat("Foo::operator<int>*();", Style);
21476   verifyFormat("Foo::operator<Foo>*();", Style);
21477   verifyFormat("Foo::operator<int>**();", Style);
21478   verifyFormat("Foo::operator<Foo>**();", Style);
21479   verifyFormat("Foo::operator<Foo>*&();", Style);
21480   verifyFormat("Foo::operator<int>&();", Style);
21481   verifyFormat("Foo::operator<Foo>&();", Style);
21482   verifyFormat("Foo::operator<int>&&();", Style);
21483   verifyFormat("Foo::operator<Foo>&&();", Style);
21484   verifyFormat("Foo::operator<int>*&();", Style);
21485   verifyFormat("Foo::operator<Foo>*&();", Style);
21486   verifyFormat("operator*(int (*)(), class Foo);", Style);
21487 
21488   verifyFormat("Foo::operator&();", Style);
21489   verifyFormat("Foo::operator void&();", Style);
21490   verifyFormat("Foo::operator void const&();", Style);
21491   verifyFormat("Foo::operator/*comment*/ void&();", Style);
21492   verifyFormat("Foo::operator/*a*/ const /*b*/ void&();", Style);
21493   verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&();", Style);
21494   verifyFormat("Foo::operator()(void&);", Style);
21495   verifyFormat("Foo::operator&(void&);", Style);
21496   verifyFormat("Foo::operator&();", Style);
21497   verifyFormat("operator&(int (&)(), class Foo);", Style);
21498 
21499   verifyFormat("Foo::operator&&();", Style);
21500   verifyFormat("Foo::operator void&&();", Style);
21501   verifyFormat("Foo::operator void const&&();", Style);
21502   verifyFormat("Foo::operator/*comment*/ void&&();", Style);
21503   verifyFormat("Foo::operator/*a*/ const /*b*/ void&&();", Style);
21504   verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&&();", Style);
21505   verifyFormat("Foo::operator()(void&&);", Style);
21506   verifyFormat("Foo::operator&&(void&&);", Style);
21507   verifyFormat("Foo::operator&&();", Style);
21508   verifyFormat("operator&&(int(&&)(), class Foo);", Style);
21509   verifyFormat("operator const nsTArrayLeft<E>&()", Style);
21510   verifyFormat("[[nodiscard]] operator const nsTArrayLeft<E, Allocator>&()",
21511                Style);
21512   verifyFormat("operator void**()", Style);
21513   verifyFormat("operator const FooLeft<Object>&()", Style);
21514   verifyFormat("operator const FooLeft<Object>*()", Style);
21515   verifyFormat("operator const FooLeft<Object>**()", Style);
21516   verifyFormat("operator const FooLeft<Object>*&()", Style);
21517   verifyFormat("operator const FooLeft<Object>*&&()", Style);
21518 
21519   // PR45107
21520   verifyFormat("operator Vector<String>&();", Style);
21521   verifyFormat("operator const Vector<String>&();", Style);
21522   verifyFormat("operator foo::Bar*();", Style);
21523   verifyFormat("operator const Foo<X>::Bar<Y>*();", Style);
21524   verifyFormat("operator/*a*/ const /*b*/ Foo /*c*/<X> /*d*/ ::Bar<Y>*();",
21525                Style);
21526 
21527   Style.PointerAlignment = FormatStyle::PAS_Middle;
21528   verifyFormat("Foo::operator*();", Style);
21529   verifyFormat("Foo::operator void *();", Style);
21530   verifyFormat("Foo::operator()(void *);", Style);
21531   verifyFormat("Foo::operator*(void *);", Style);
21532   verifyFormat("Foo::operator*();", Style);
21533   verifyFormat("operator*(int (*)(), class Foo);", Style);
21534 
21535   verifyFormat("Foo::operator&();", Style);
21536   verifyFormat("Foo::operator void &();", Style);
21537   verifyFormat("Foo::operator void const &();", Style);
21538   verifyFormat("Foo::operator()(void &);", Style);
21539   verifyFormat("Foo::operator&(void &);", Style);
21540   verifyFormat("Foo::operator&();", Style);
21541   verifyFormat("operator&(int (&)(), class Foo);", Style);
21542 
21543   verifyFormat("Foo::operator&&();", Style);
21544   verifyFormat("Foo::operator void &&();", Style);
21545   verifyFormat("Foo::operator void const &&();", Style);
21546   verifyFormat("Foo::operator()(void &&);", Style);
21547   verifyFormat("Foo::operator&&(void &&);", Style);
21548   verifyFormat("Foo::operator&&();", Style);
21549   verifyFormat("operator&&(int(&&)(), class Foo);", Style);
21550 }
21551 
21552 TEST_F(FormatTest, OperatorPassedAsAFunctionPtr) {
21553   FormatStyle Style = getLLVMStyle();
21554   // PR46157
21555   verifyFormat("foo(operator+, -42);", Style);
21556   verifyFormat("foo(operator++, -42);", Style);
21557   verifyFormat("foo(operator--, -42);", Style);
21558   verifyFormat("foo(-42, operator--);", Style);
21559   verifyFormat("foo(-42, operator, );", Style);
21560   verifyFormat("foo(operator, , -42);", Style);
21561 }
21562 
21563 TEST_F(FormatTest, WhitespaceSensitiveMacros) {
21564   FormatStyle Style = getLLVMStyle();
21565   Style.WhitespaceSensitiveMacros.push_back("FOO");
21566 
21567   // Don't use the helpers here, since 'mess up' will change the whitespace
21568   // and these are all whitespace sensitive by definition
21569   EXPECT_EQ("FOO(String-ized&Messy+But(: :Still)=Intentional);",
21570             format("FOO(String-ized&Messy+But(: :Still)=Intentional);", Style));
21571   EXPECT_EQ(
21572       "FOO(String-ized&Messy+But\\(: :Still)=Intentional);",
21573       format("FOO(String-ized&Messy+But\\(: :Still)=Intentional);", Style));
21574   EXPECT_EQ("FOO(String-ized&Messy+But,: :Still=Intentional);",
21575             format("FOO(String-ized&Messy+But,: :Still=Intentional);", Style));
21576   EXPECT_EQ("FOO(String-ized&Messy+But,: :\n"
21577             "       Still=Intentional);",
21578             format("FOO(String-ized&Messy+But,: :\n"
21579                    "       Still=Intentional);",
21580                    Style));
21581   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
21582   EXPECT_EQ("FOO(String-ized=&Messy+But,: :\n"
21583             "       Still=Intentional);",
21584             format("FOO(String-ized=&Messy+But,: :\n"
21585                    "       Still=Intentional);",
21586                    Style));
21587 
21588   Style.ColumnLimit = 21;
21589   EXPECT_EQ("FOO(String-ized&Messy+But: :Still=Intentional);",
21590             format("FOO(String-ized&Messy+But: :Still=Intentional);", Style));
21591 }
21592 
21593 TEST_F(FormatTest, VeryLongNamespaceCommentSplit) {
21594   // These tests are not in NamespaceFixer because that doesn't
21595   // test its interaction with line wrapping
21596   FormatStyle Style = getLLVMStyle();
21597   Style.ColumnLimit = 80;
21598   verifyFormat("namespace {\n"
21599                "int i;\n"
21600                "int j;\n"
21601                "} // namespace",
21602                Style);
21603 
21604   verifyFormat("namespace AAA {\n"
21605                "int i;\n"
21606                "int j;\n"
21607                "} // namespace AAA",
21608                Style);
21609 
21610   EXPECT_EQ("namespace Averyveryveryverylongnamespace {\n"
21611             "int i;\n"
21612             "int j;\n"
21613             "} // namespace Averyveryveryverylongnamespace",
21614             format("namespace Averyveryveryverylongnamespace {\n"
21615                    "int i;\n"
21616                    "int j;\n"
21617                    "}",
21618                    Style));
21619 
21620   EXPECT_EQ(
21621       "namespace "
21622       "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n"
21623       "    went::mad::now {\n"
21624       "int i;\n"
21625       "int j;\n"
21626       "} // namespace\n"
21627       "  // "
21628       "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::"
21629       "went::mad::now",
21630       format("namespace "
21631              "would::it::save::you::a::lot::of::time::if_::i::"
21632              "just::gave::up::and_::went::mad::now {\n"
21633              "int i;\n"
21634              "int j;\n"
21635              "}",
21636              Style));
21637 
21638   // This used to duplicate the comment again and again on subsequent runs
21639   EXPECT_EQ(
21640       "namespace "
21641       "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n"
21642       "    went::mad::now {\n"
21643       "int i;\n"
21644       "int j;\n"
21645       "} // namespace\n"
21646       "  // "
21647       "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::"
21648       "went::mad::now",
21649       format("namespace "
21650              "would::it::save::you::a::lot::of::time::if_::i::"
21651              "just::gave::up::and_::went::mad::now {\n"
21652              "int i;\n"
21653              "int j;\n"
21654              "} // namespace\n"
21655              "  // "
21656              "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::"
21657              "and_::went::mad::now",
21658              Style));
21659 }
21660 
21661 TEST_F(FormatTest, LikelyUnlikely) {
21662   FormatStyle Style = getLLVMStyle();
21663 
21664   verifyFormat("if (argc > 5) [[unlikely]] {\n"
21665                "  return 29;\n"
21666                "}",
21667                Style);
21668 
21669   verifyFormat("if (argc > 5) [[likely]] {\n"
21670                "  return 29;\n"
21671                "}",
21672                Style);
21673 
21674   verifyFormat("if (argc > 5) [[unlikely]] {\n"
21675                "  return 29;\n"
21676                "} else [[likely]] {\n"
21677                "  return 42;\n"
21678                "}\n",
21679                Style);
21680 
21681   verifyFormat("if (argc > 5) [[unlikely]] {\n"
21682                "  return 29;\n"
21683                "} else if (argc > 10) [[likely]] {\n"
21684                "  return 99;\n"
21685                "} else {\n"
21686                "  return 42;\n"
21687                "}\n",
21688                Style);
21689 
21690   verifyFormat("if (argc > 5) [[gnu::unused]] {\n"
21691                "  return 29;\n"
21692                "}",
21693                Style);
21694 }
21695 
21696 TEST_F(FormatTest, PenaltyIndentedWhitespace) {
21697   verifyFormat("Constructor()\n"
21698                "    : aaaaaa(aaaaaa), aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
21699                "                          aaaa(aaaaaaaaaaaaaaaaaa, "
21700                "aaaaaaaaaaaaaaaaaat))");
21701   verifyFormat("Constructor()\n"
21702                "    : aaaaaaaaaaaaa(aaaaaa), "
21703                "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)");
21704 
21705   FormatStyle StyleWithWhitespacePenalty = getLLVMStyle();
21706   StyleWithWhitespacePenalty.PenaltyIndentedWhitespace = 5;
21707   verifyFormat("Constructor()\n"
21708                "    : aaaaaa(aaaaaa),\n"
21709                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
21710                "          aaaa(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaat))",
21711                StyleWithWhitespacePenalty);
21712   verifyFormat("Constructor()\n"
21713                "    : aaaaaaaaaaaaa(aaaaaa), "
21714                "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)",
21715                StyleWithWhitespacePenalty);
21716 }
21717 
21718 TEST_F(FormatTest, LLVMDefaultStyle) {
21719   FormatStyle Style = getLLVMStyle();
21720   verifyFormat("extern \"C\" {\n"
21721                "int foo();\n"
21722                "}",
21723                Style);
21724 }
21725 TEST_F(FormatTest, GNUDefaultStyle) {
21726   FormatStyle Style = getGNUStyle();
21727   verifyFormat("extern \"C\"\n"
21728                "{\n"
21729                "  int foo ();\n"
21730                "}",
21731                Style);
21732 }
21733 TEST_F(FormatTest, MozillaDefaultStyle) {
21734   FormatStyle Style = getMozillaStyle();
21735   verifyFormat("extern \"C\"\n"
21736                "{\n"
21737                "  int foo();\n"
21738                "}",
21739                Style);
21740 }
21741 TEST_F(FormatTest, GoogleDefaultStyle) {
21742   FormatStyle Style = getGoogleStyle();
21743   verifyFormat("extern \"C\" {\n"
21744                "int foo();\n"
21745                "}",
21746                Style);
21747 }
21748 TEST_F(FormatTest, ChromiumDefaultStyle) {
21749   FormatStyle Style = getChromiumStyle(FormatStyle::LanguageKind::LK_Cpp);
21750   verifyFormat("extern \"C\" {\n"
21751                "int foo();\n"
21752                "}",
21753                Style);
21754 }
21755 TEST_F(FormatTest, MicrosoftDefaultStyle) {
21756   FormatStyle Style = getMicrosoftStyle(FormatStyle::LanguageKind::LK_Cpp);
21757   verifyFormat("extern \"C\"\n"
21758                "{\n"
21759                "    int foo();\n"
21760                "}",
21761                Style);
21762 }
21763 TEST_F(FormatTest, WebKitDefaultStyle) {
21764   FormatStyle Style = getWebKitStyle();
21765   verifyFormat("extern \"C\" {\n"
21766                "int foo();\n"
21767                "}",
21768                Style);
21769 }
21770 
21771 TEST_F(FormatTest, ConceptsAndRequires) {
21772   FormatStyle Style = getLLVMStyle();
21773   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
21774 
21775   verifyFormat("template <typename T>\n"
21776                "concept Hashable = requires(T a) {\n"
21777                "  { std::hash<T>{}(a) } -> std::convertible_to<std::size_t>;\n"
21778                "};",
21779                Style);
21780   verifyFormat("template <typename T>\n"
21781                "concept EqualityComparable = requires(T a, T b) {\n"
21782                "  { a == b } -> bool;\n"
21783                "};",
21784                Style);
21785   verifyFormat("template <typename T>\n"
21786                "concept EqualityComparable = requires(T a, T b) {\n"
21787                "  { a == b } -> bool;\n"
21788                "  { a != b } -> bool;\n"
21789                "};",
21790                Style);
21791   verifyFormat("template <typename T>\n"
21792                "concept EqualityComparable = requires(T a, T b) {\n"
21793                "  { a == b } -> bool;\n"
21794                "  { a != b } -> bool;\n"
21795                "};",
21796                Style);
21797 
21798   verifyFormat("template <typename It>\n"
21799                "requires Iterator<It>\n"
21800                "void sort(It begin, It end) {\n"
21801                "  //....\n"
21802                "}",
21803                Style);
21804 
21805   verifyFormat("template <typename T>\n"
21806                "concept Large = sizeof(T) > 10;",
21807                Style);
21808 
21809   verifyFormat("template <typename T, typename U>\n"
21810                "concept FooableWith = requires(T t, U u) {\n"
21811                "  typename T::foo_type;\n"
21812                "  { t.foo(u) } -> typename T::foo_type;\n"
21813                "  t++;\n"
21814                "};\n"
21815                "void doFoo(FooableWith<int> auto t) {\n"
21816                "  t.foo(3);\n"
21817                "}",
21818                Style);
21819   verifyFormat("template <typename T>\n"
21820                "concept Context = sizeof(T) == 1;",
21821                Style);
21822   verifyFormat("template <typename T>\n"
21823                "concept Context = is_specialization_of_v<context, T>;",
21824                Style);
21825   verifyFormat("template <typename T>\n"
21826                "concept Node = std::is_object_v<T>;",
21827                Style);
21828   verifyFormat("template <typename T>\n"
21829                "concept Tree = true;",
21830                Style);
21831 
21832   verifyFormat("template <typename T> int g(T i) requires Concept1<I> {\n"
21833                "  //...\n"
21834                "}",
21835                Style);
21836 
21837   verifyFormat(
21838       "template <typename T> int g(T i) requires Concept1<I> && Concept2<I> {\n"
21839       "  //...\n"
21840       "}",
21841       Style);
21842 
21843   verifyFormat(
21844       "template <typename T> int g(T i) requires Concept1<I> || Concept2<I> {\n"
21845       "  //...\n"
21846       "}",
21847       Style);
21848 
21849   verifyFormat("template <typename T>\n"
21850                "veryveryvery_long_return_type g(T i) requires Concept1<I> || "
21851                "Concept2<I> {\n"
21852                "  //...\n"
21853                "}",
21854                Style);
21855 
21856   verifyFormat("template <typename T>\n"
21857                "veryveryvery_long_return_type g(T i) requires Concept1<I> && "
21858                "Concept2<I> {\n"
21859                "  //...\n"
21860                "}",
21861                Style);
21862 
21863   verifyFormat(
21864       "template <typename T>\n"
21865       "veryveryvery_long_return_type g(T i) requires Concept1 && Concept2 {\n"
21866       "  //...\n"
21867       "}",
21868       Style);
21869 
21870   verifyFormat(
21871       "template <typename T>\n"
21872       "veryveryvery_long_return_type g(T i) requires Concept1 || Concept2 {\n"
21873       "  //...\n"
21874       "}",
21875       Style);
21876 
21877   verifyFormat("template <typename It>\n"
21878                "requires Foo<It>() && Bar<It> {\n"
21879                "  //....\n"
21880                "}",
21881                Style);
21882 
21883   verifyFormat("template <typename It>\n"
21884                "requires Foo<Bar<It>>() && Bar<Foo<It, It>> {\n"
21885                "  //....\n"
21886                "}",
21887                Style);
21888 
21889   verifyFormat("template <typename It>\n"
21890                "requires Foo<Bar<It, It>>() && Bar<Foo<It, It>> {\n"
21891                "  //....\n"
21892                "}",
21893                Style);
21894 
21895   verifyFormat(
21896       "template <typename It>\n"
21897       "requires Foo<Bar<It>, Baz<It>>() && Bar<Foo<It>, Baz<It, It>> {\n"
21898       "  //....\n"
21899       "}",
21900       Style);
21901 
21902   Style.IndentRequires = true;
21903   verifyFormat("template <typename It>\n"
21904                "  requires Iterator<It>\n"
21905                "void sort(It begin, It end) {\n"
21906                "  //....\n"
21907                "}",
21908                Style);
21909   verifyFormat("template <std::size index_>\n"
21910                "  requires(index_ < sizeof...(Children_))\n"
21911                "Tree auto &child() {\n"
21912                "  // ...\n"
21913                "}",
21914                Style);
21915 
21916   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
21917   verifyFormat("template <typename T>\n"
21918                "concept Hashable = requires (T a) {\n"
21919                "  { std::hash<T>{}(a) } -> std::convertible_to<std::size_t>;\n"
21920                "};",
21921                Style);
21922 
21923   verifyFormat("template <class T = void>\n"
21924                "  requires EqualityComparable<T> || Same<T, void>\n"
21925                "struct equal_to;",
21926                Style);
21927 
21928   verifyFormat("template <class T>\n"
21929                "  requires requires {\n"
21930                "    T{};\n"
21931                "    T (int);\n"
21932                "  }\n",
21933                Style);
21934 
21935   Style.ColumnLimit = 78;
21936   verifyFormat("template <typename T>\n"
21937                "concept Context = Traits<typename T::traits_type> and\n"
21938                "    Interface<typename T::interface_type> and\n"
21939                "    Request<typename T::request_type> and\n"
21940                "    Response<typename T::response_type> and\n"
21941                "    ContextExtension<typename T::extension_type> and\n"
21942                "    ::std::is_copy_constructable<T> and "
21943                "::std::is_move_constructable<T> and\n"
21944                "    requires (T c) {\n"
21945                "  { c.response; } -> Response;\n"
21946                "} and requires (T c) {\n"
21947                "  { c.request; } -> Request;\n"
21948                "}\n",
21949                Style);
21950 
21951   verifyFormat("template <typename T>\n"
21952                "concept Context = Traits<typename T::traits_type> or\n"
21953                "    Interface<typename T::interface_type> or\n"
21954                "    Request<typename T::request_type> or\n"
21955                "    Response<typename T::response_type> or\n"
21956                "    ContextExtension<typename T::extension_type> or\n"
21957                "    ::std::is_copy_constructable<T> or "
21958                "::std::is_move_constructable<T> or\n"
21959                "    requires (T c) {\n"
21960                "  { c.response; } -> Response;\n"
21961                "} or requires (T c) {\n"
21962                "  { c.request; } -> Request;\n"
21963                "}\n",
21964                Style);
21965 
21966   verifyFormat("template <typename T>\n"
21967                "concept Context = Traits<typename T::traits_type> &&\n"
21968                "    Interface<typename T::interface_type> &&\n"
21969                "    Request<typename T::request_type> &&\n"
21970                "    Response<typename T::response_type> &&\n"
21971                "    ContextExtension<typename T::extension_type> &&\n"
21972                "    ::std::is_copy_constructable<T> && "
21973                "::std::is_move_constructable<T> &&\n"
21974                "    requires (T c) {\n"
21975                "  { c.response; } -> Response;\n"
21976                "} && requires (T c) {\n"
21977                "  { c.request; } -> Request;\n"
21978                "}\n",
21979                Style);
21980 
21981   verifyFormat("template <typename T>\nconcept someConcept = Constraint1<T> && "
21982                "Constraint2<T>;");
21983 
21984   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
21985   Style.BraceWrapping.AfterFunction = true;
21986   Style.BraceWrapping.AfterClass = true;
21987   Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
21988   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
21989   verifyFormat("void Foo () requires (std::copyable<T>)\n"
21990                "{\n"
21991                "  return\n"
21992                "}\n",
21993                Style);
21994 
21995   verifyFormat("void Foo () requires std::copyable<T>\n"
21996                "{\n"
21997                "  return\n"
21998                "}\n",
21999                Style);
22000 
22001   verifyFormat("template <std::semiregular F, std::semiregular... Args>\n"
22002                "  requires (std::invocable<F, std::invoke_result_t<Args>...>)\n"
22003                "struct constant;",
22004                Style);
22005 
22006   verifyFormat("template <std::semiregular F, std::semiregular... Args>\n"
22007                "  requires std::invocable<F, std::invoke_result_t<Args>...>\n"
22008                "struct constant;",
22009                Style);
22010 
22011   verifyFormat("template <class T>\n"
22012                "class plane_with_very_very_very_long_name\n"
22013                "{\n"
22014                "  constexpr plane_with_very_very_very_long_name () requires "
22015                "std::copyable<T>\n"
22016                "      : plane_with_very_very_very_long_name (1)\n"
22017                "  {\n"
22018                "  }\n"
22019                "}\n",
22020                Style);
22021 
22022   verifyFormat("template <class T>\n"
22023                "class plane_with_long_name\n"
22024                "{\n"
22025                "  constexpr plane_with_long_name () requires std::copyable<T>\n"
22026                "      : plane_with_long_name (1)\n"
22027                "  {\n"
22028                "  }\n"
22029                "}\n",
22030                Style);
22031 
22032   Style.BreakBeforeConceptDeclarations = false;
22033   verifyFormat("template <typename T> concept Tree = true;", Style);
22034 
22035   Style.IndentRequires = false;
22036   verifyFormat("template <std::semiregular F, std::semiregular... Args>\n"
22037                "requires (std::invocable<F, std::invoke_result_t<Args>...>) "
22038                "struct constant;",
22039                Style);
22040 }
22041 
22042 TEST_F(FormatTest, StatementAttributeLikeMacros) {
22043   FormatStyle Style = getLLVMStyle();
22044   StringRef Source = "void Foo::slot() {\n"
22045                      "  unsigned char MyChar = 'x';\n"
22046                      "  emit signal(MyChar);\n"
22047                      "  Q_EMIT signal(MyChar);\n"
22048                      "}";
22049 
22050   EXPECT_EQ(Source, format(Source, Style));
22051 
22052   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
22053   EXPECT_EQ("void Foo::slot() {\n"
22054             "  unsigned char MyChar = 'x';\n"
22055             "  emit          signal(MyChar);\n"
22056             "  Q_EMIT signal(MyChar);\n"
22057             "}",
22058             format(Source, Style));
22059 
22060   Style.StatementAttributeLikeMacros.push_back("emit");
22061   EXPECT_EQ(Source, format(Source, Style));
22062 
22063   Style.StatementAttributeLikeMacros = {};
22064   EXPECT_EQ("void Foo::slot() {\n"
22065             "  unsigned char MyChar = 'x';\n"
22066             "  emit          signal(MyChar);\n"
22067             "  Q_EMIT        signal(MyChar);\n"
22068             "}",
22069             format(Source, Style));
22070 }
22071 
22072 TEST_F(FormatTest, IndentAccessModifiers) {
22073   FormatStyle Style = getLLVMStyle();
22074   Style.IndentAccessModifiers = true;
22075   // Members are *two* levels below the record;
22076   // Style.IndentWidth == 2, thus yielding a 4 spaces wide indentation.
22077   verifyFormat("class C {\n"
22078                "    int i;\n"
22079                "};\n",
22080                Style);
22081   verifyFormat("union C {\n"
22082                "    int i;\n"
22083                "    unsigned u;\n"
22084                "};\n",
22085                Style);
22086   // Access modifiers should be indented one level below the record.
22087   verifyFormat("class C {\n"
22088                "  public:\n"
22089                "    int i;\n"
22090                "};\n",
22091                Style);
22092   verifyFormat("struct S {\n"
22093                "  private:\n"
22094                "    class C {\n"
22095                "        int j;\n"
22096                "\n"
22097                "      public:\n"
22098                "        C();\n"
22099                "    };\n"
22100                "\n"
22101                "  public:\n"
22102                "    int i;\n"
22103                "};\n",
22104                Style);
22105   // Enumerations are not records and should be unaffected.
22106   Style.AllowShortEnumsOnASingleLine = false;
22107   verifyFormat("enum class E\n"
22108                "{\n"
22109                "  A,\n"
22110                "  B\n"
22111                "};\n",
22112                Style);
22113   // Test with a different indentation width;
22114   // also proves that the result is Style.AccessModifierOffset agnostic.
22115   Style.IndentWidth = 3;
22116   verifyFormat("class C {\n"
22117                "   public:\n"
22118                "      int i;\n"
22119                "};\n",
22120                Style);
22121 }
22122 
22123 TEST_F(FormatTest, LimitlessStringsAndComments) {
22124   auto Style = getLLVMStyleWithColumns(0);
22125   constexpr StringRef Code =
22126       "/**\n"
22127       " * This is a multiline comment with quite some long lines, at least for "
22128       "the LLVM Style.\n"
22129       " * We will redo this with strings and line comments. Just to  check if "
22130       "everything is working.\n"
22131       " */\n"
22132       "bool foo() {\n"
22133       "  /* Single line multi line comment. */\n"
22134       "  const std::string String = \"This is a multiline string with quite "
22135       "some long lines, at least for the LLVM Style.\"\n"
22136       "                             \"We already did it with multi line "
22137       "comments, and we will do it with line comments. Just to check if "
22138       "everything is working.\";\n"
22139       "  // This is a line comment (block) with quite some long lines, at "
22140       "least for the LLVM Style.\n"
22141       "  // We already did this with multi line comments and strings. Just to "
22142       "check if everything is working.\n"
22143       "  const std::string SmallString = \"Hello World\";\n"
22144       "  // Small line comment\n"
22145       "  return String.size() > SmallString.size();\n"
22146       "}";
22147   EXPECT_EQ(Code, format(Code, Style));
22148 }
22149 } // namespace
22150 } // namespace format
22151 } // namespace clang
22152