xref: /llvm-project/clang/unittests/Format/FormatTest.cpp (revision 529aa4b011c4ae808d658022ef643c44dd9b2c9c)
1 //===- unittest/Format/FormatTest.cpp - Formatting unit tests -------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "clang/Format/Format.h"
10 
11 #include "../Tooling/ReplacementTest.h"
12 #include "FormatTestUtils.h"
13 
14 #include "llvm/Support/Debug.h"
15 #include "llvm/Support/MemoryBuffer.h"
16 #include "gtest/gtest.h"
17 
18 #define DEBUG_TYPE "format-test"
19 
20 using clang::tooling::ReplacementTest;
21 using clang::tooling::toReplacements;
22 using testing::ScopedTrace;
23 
24 namespace clang {
25 namespace format {
26 namespace {
27 
28 FormatStyle getGoogleStyle() { return getGoogleStyle(FormatStyle::LK_Cpp); }
29 
30 class FormatTest : public ::testing::Test {
31 protected:
32   enum StatusCheck { SC_ExpectComplete, SC_ExpectIncomplete, SC_DoNotCheck };
33 
34   std::string format(llvm::StringRef Code,
35                      const FormatStyle &Style = getLLVMStyle(),
36                      StatusCheck CheckComplete = SC_ExpectComplete) {
37     LLVM_DEBUG(llvm::errs() << "---\n");
38     LLVM_DEBUG(llvm::errs() << Code << "\n\n");
39     std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size()));
40     FormattingAttemptStatus Status;
41     tooling::Replacements Replaces =
42         reformat(Style, Code, Ranges, "<stdin>", &Status);
43     if (CheckComplete != SC_DoNotCheck) {
44       bool ExpectedCompleteFormat = CheckComplete == SC_ExpectComplete;
45       EXPECT_EQ(ExpectedCompleteFormat, Status.FormatComplete)
46           << Code << "\n\n";
47     }
48     ReplacementCount = Replaces.size();
49     auto Result = applyAllReplacements(Code, Replaces);
50     EXPECT_TRUE(static_cast<bool>(Result));
51     LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
52     return *Result;
53   }
54 
55   FormatStyle getStyleWithColumns(FormatStyle Style, unsigned ColumnLimit) {
56     Style.ColumnLimit = ColumnLimit;
57     return Style;
58   }
59 
60   FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) {
61     return getStyleWithColumns(getLLVMStyle(), ColumnLimit);
62   }
63 
64   FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) {
65     return getStyleWithColumns(getGoogleStyle(), ColumnLimit);
66   }
67 
68   void _verifyFormat(const char *File, int Line, llvm::StringRef Expected,
69                      llvm::StringRef Code,
70                      const FormatStyle &Style = getLLVMStyle()) {
71     ScopedTrace t(File, Line, ::testing::Message() << Code.str());
72     EXPECT_EQ(Expected.str(), format(Expected, Style))
73         << "Expected code is not stable";
74     EXPECT_EQ(Expected.str(), format(Code, Style));
75     if (Style.Language == FormatStyle::LK_Cpp) {
76       // Objective-C++ is a superset of C++, so everything checked for C++
77       // needs to be checked for Objective-C++ as well.
78       FormatStyle ObjCStyle = Style;
79       ObjCStyle.Language = FormatStyle::LK_ObjC;
80       EXPECT_EQ(Expected.str(), format(test::messUp(Code), ObjCStyle));
81     }
82   }
83 
84   void _verifyFormat(const char *File, int Line, llvm::StringRef Code,
85                      const FormatStyle &Style = getLLVMStyle()) {
86     _verifyFormat(File, Line, Code, test::messUp(Code), Style);
87   }
88 
89   void _verifyIncompleteFormat(const char *File, int Line, llvm::StringRef Code,
90                                const FormatStyle &Style = getLLVMStyle()) {
91     ScopedTrace t(File, Line, ::testing::Message() << Code.str());
92     EXPECT_EQ(Code.str(),
93               format(test::messUp(Code), Style, SC_ExpectIncomplete));
94   }
95 
96   void _verifyIndependentOfContext(const char *File, int Line,
97                                    llvm::StringRef Text,
98                                    const FormatStyle &Style = getLLVMStyle()) {
99     _verifyFormat(File, Line, Text, Style);
100     _verifyFormat(File, Line, llvm::Twine("void f() { " + Text + " }").str(),
101                   Style);
102   }
103 
104   /// \brief Verify that clang-format does not crash on the given input.
105   void verifyNoCrash(llvm::StringRef Code,
106                      const FormatStyle &Style = getLLVMStyle()) {
107     format(Code, Style, SC_DoNotCheck);
108   }
109 
110   int ReplacementCount;
111 };
112 
113 #define verifyIndependentOfContext(...)                                        \
114   _verifyIndependentOfContext(__FILE__, __LINE__, __VA_ARGS__)
115 #define verifyIncompleteFormat(...)                                            \
116   _verifyIncompleteFormat(__FILE__, __LINE__, __VA_ARGS__)
117 #define verifyFormat(...) _verifyFormat(__FILE__, __LINE__, __VA_ARGS__)
118 #define verifyGoogleFormat(Code) verifyFormat(Code, getGoogleStyle())
119 
120 TEST_F(FormatTest, MessUp) {
121   EXPECT_EQ("1 2 3", test::messUp("1 2 3"));
122   EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n"));
123   EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc"));
124   EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc"));
125   EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne"));
126 }
127 
128 TEST_F(FormatTest, DefaultLLVMStyleIsCpp) {
129   EXPECT_EQ(FormatStyle::LK_Cpp, getLLVMStyle().Language);
130 }
131 
132 TEST_F(FormatTest, LLVMStyleOverride) {
133   EXPECT_EQ(FormatStyle::LK_Proto,
134             getLLVMStyle(FormatStyle::LK_Proto).Language);
135 }
136 
137 //===----------------------------------------------------------------------===//
138 // Basic function tests.
139 //===----------------------------------------------------------------------===//
140 
141 TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) {
142   EXPECT_EQ(";", format(";"));
143 }
144 
145 TEST_F(FormatTest, FormatsGlobalStatementsAt0) {
146   EXPECT_EQ("int i;", format("  int i;"));
147   EXPECT_EQ("\nint i;", format(" \n\t \v \f  int i;"));
148   EXPECT_EQ("int i;\nint j;", format("    int i; int j;"));
149   EXPECT_EQ("int i;\nint j;", format("    int i;\n  int j;"));
150 }
151 
152 TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) {
153   EXPECT_EQ("int i;", format("int\ni;"));
154 }
155 
156 TEST_F(FormatTest, FormatsNestedBlockStatements) {
157   EXPECT_EQ("{\n  {\n    {}\n  }\n}", format("{{{}}}"));
158 }
159 
160 TEST_F(FormatTest, FormatsNestedCall) {
161   verifyFormat("Method(f1, f2(f3));");
162   verifyFormat("Method(f1(f2, f3()));");
163   verifyFormat("Method(f1(f2, (f3())));");
164 }
165 
166 TEST_F(FormatTest, NestedNameSpecifiers) {
167   verifyFormat("vector<::Type> v;");
168   verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())");
169   verifyFormat("static constexpr bool Bar = decltype(bar())::value;");
170   verifyFormat("static constexpr bool Bar = typeof(bar())::value;");
171   verifyFormat("static constexpr bool Bar = __underlying_type(bar())::value;");
172   verifyFormat("static constexpr bool Bar = _Atomic(bar())::value;");
173   verifyFormat("bool a = 2 < ::SomeFunction();");
174   verifyFormat("ALWAYS_INLINE ::std::string getName();");
175   verifyFormat("some::string getName();");
176 }
177 
178 TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) {
179   EXPECT_EQ("if (a) {\n"
180             "  f();\n"
181             "}",
182             format("if(a){f();}"));
183   EXPECT_EQ(4, ReplacementCount);
184   EXPECT_EQ("if (a) {\n"
185             "  f();\n"
186             "}",
187             format("if (a) {\n"
188                    "  f();\n"
189                    "}"));
190   EXPECT_EQ(0, ReplacementCount);
191   EXPECT_EQ("/*\r\n"
192             "\r\n"
193             "*/\r\n",
194             format("/*\r\n"
195                    "\r\n"
196                    "*/\r\n"));
197   EXPECT_EQ(0, ReplacementCount);
198 }
199 
200 TEST_F(FormatTest, RemovesEmptyLines) {
201   EXPECT_EQ("class C {\n"
202             "  int i;\n"
203             "};",
204             format("class C {\n"
205                    " int i;\n"
206                    "\n"
207                    "};"));
208 
209   // Don't remove empty lines at the start of namespaces or extern "C" blocks.
210   EXPECT_EQ("namespace N {\n"
211             "\n"
212             "int i;\n"
213             "}",
214             format("namespace N {\n"
215                    "\n"
216                    "int    i;\n"
217                    "}",
218                    getGoogleStyle()));
219   EXPECT_EQ("/* something */ namespace N {\n"
220             "\n"
221             "int i;\n"
222             "}",
223             format("/* something */ namespace N {\n"
224                    "\n"
225                    "int    i;\n"
226                    "}",
227                    getGoogleStyle()));
228   EXPECT_EQ("inline namespace N {\n"
229             "\n"
230             "int i;\n"
231             "}",
232             format("inline namespace N {\n"
233                    "\n"
234                    "int    i;\n"
235                    "}",
236                    getGoogleStyle()));
237   EXPECT_EQ("/* something */ inline namespace N {\n"
238             "\n"
239             "int i;\n"
240             "}",
241             format("/* something */ inline namespace N {\n"
242                    "\n"
243                    "int    i;\n"
244                    "}",
245                    getGoogleStyle()));
246   EXPECT_EQ("export namespace N {\n"
247             "\n"
248             "int i;\n"
249             "}",
250             format("export namespace N {\n"
251                    "\n"
252                    "int    i;\n"
253                    "}",
254                    getGoogleStyle()));
255   EXPECT_EQ("extern /**/ \"C\" /**/ {\n"
256             "\n"
257             "int i;\n"
258             "}",
259             format("extern /**/ \"C\" /**/ {\n"
260                    "\n"
261                    "int    i;\n"
262                    "}",
263                    getGoogleStyle()));
264 
265   auto CustomStyle = getLLVMStyle();
266   CustomStyle.BreakBeforeBraces = FormatStyle::BS_Custom;
267   CustomStyle.BraceWrapping.AfterNamespace = true;
268   CustomStyle.KeepEmptyLinesAtTheStartOfBlocks = false;
269   EXPECT_EQ("namespace N\n"
270             "{\n"
271             "\n"
272             "int i;\n"
273             "}",
274             format("namespace N\n"
275                    "{\n"
276                    "\n"
277                    "\n"
278                    "int    i;\n"
279                    "}",
280                    CustomStyle));
281   EXPECT_EQ("/* something */ namespace N\n"
282             "{\n"
283             "\n"
284             "int i;\n"
285             "}",
286             format("/* something */ namespace N {\n"
287                    "\n"
288                    "\n"
289                    "int    i;\n"
290                    "}",
291                    CustomStyle));
292   EXPECT_EQ("inline namespace N\n"
293             "{\n"
294             "\n"
295             "int i;\n"
296             "}",
297             format("inline namespace N\n"
298                    "{\n"
299                    "\n"
300                    "\n"
301                    "int    i;\n"
302                    "}",
303                    CustomStyle));
304   EXPECT_EQ("/* something */ inline namespace N\n"
305             "{\n"
306             "\n"
307             "int i;\n"
308             "}",
309             format("/* something */ inline namespace N\n"
310                    "{\n"
311                    "\n"
312                    "int    i;\n"
313                    "}",
314                    CustomStyle));
315   EXPECT_EQ("export namespace N\n"
316             "{\n"
317             "\n"
318             "int i;\n"
319             "}",
320             format("export namespace N\n"
321                    "{\n"
322                    "\n"
323                    "int    i;\n"
324                    "}",
325                    CustomStyle));
326   EXPECT_EQ("namespace a\n"
327             "{\n"
328             "namespace b\n"
329             "{\n"
330             "\n"
331             "class AA {};\n"
332             "\n"
333             "} // namespace b\n"
334             "} // namespace a\n",
335             format("namespace a\n"
336                    "{\n"
337                    "namespace b\n"
338                    "{\n"
339                    "\n"
340                    "\n"
341                    "class AA {};\n"
342                    "\n"
343                    "\n"
344                    "}\n"
345                    "}\n",
346                    CustomStyle));
347   EXPECT_EQ("namespace A /* comment */\n"
348             "{\n"
349             "class B {}\n"
350             "} // namespace A",
351             format("namespace A /* comment */ { class B {} }", CustomStyle));
352   EXPECT_EQ("namespace A\n"
353             "{ /* comment */\n"
354             "class B {}\n"
355             "} // namespace A",
356             format("namespace A {/* comment */ class B {} }", CustomStyle));
357   EXPECT_EQ("namespace A\n"
358             "{ /* comment */\n"
359             "\n"
360             "class B {}\n"
361             "\n"
362             ""
363             "} // namespace A",
364             format("namespace A { /* comment */\n"
365                    "\n"
366                    "\n"
367                    "class B {}\n"
368                    "\n"
369                    "\n"
370                    "}",
371                    CustomStyle));
372   EXPECT_EQ("namespace A /* comment */\n"
373             "{\n"
374             "\n"
375             "class B {}\n"
376             "\n"
377             "} // namespace A",
378             format("namespace A/* comment */ {\n"
379                    "\n"
380                    "\n"
381                    "class B {}\n"
382                    "\n"
383                    "\n"
384                    "}",
385                    CustomStyle));
386 
387   // ...but do keep inlining and removing empty lines for non-block extern "C"
388   // functions.
389   verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle());
390   EXPECT_EQ("extern \"C\" int f() {\n"
391             "  int i = 42;\n"
392             "  return i;\n"
393             "}",
394             format("extern \"C\" int f() {\n"
395                    "\n"
396                    "  int i = 42;\n"
397                    "  return i;\n"
398                    "}",
399                    getGoogleStyle()));
400 
401   // Remove empty lines at the beginning and end of blocks.
402   EXPECT_EQ("void f() {\n"
403             "\n"
404             "  if (a) {\n"
405             "\n"
406             "    f();\n"
407             "  }\n"
408             "}",
409             format("void f() {\n"
410                    "\n"
411                    "  if (a) {\n"
412                    "\n"
413                    "    f();\n"
414                    "\n"
415                    "  }\n"
416                    "\n"
417                    "}",
418                    getLLVMStyle()));
419   EXPECT_EQ("void f() {\n"
420             "  if (a) {\n"
421             "    f();\n"
422             "  }\n"
423             "}",
424             format("void f() {\n"
425                    "\n"
426                    "  if (a) {\n"
427                    "\n"
428                    "    f();\n"
429                    "\n"
430                    "  }\n"
431                    "\n"
432                    "}",
433                    getGoogleStyle()));
434 
435   // Don't remove empty lines in more complex control statements.
436   EXPECT_EQ("void f() {\n"
437             "  if (a) {\n"
438             "    f();\n"
439             "\n"
440             "  } else if (b) {\n"
441             "    f();\n"
442             "  }\n"
443             "}",
444             format("void f() {\n"
445                    "  if (a) {\n"
446                    "    f();\n"
447                    "\n"
448                    "  } else if (b) {\n"
449                    "    f();\n"
450                    "\n"
451                    "  }\n"
452                    "\n"
453                    "}"));
454 
455   // Don't remove empty lines before namespace endings.
456   FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle();
457   LLVMWithNoNamespaceFix.FixNamespaceComments = false;
458   EXPECT_EQ("namespace {\n"
459             "int i;\n"
460             "\n"
461             "}",
462             format("namespace {\n"
463                    "int i;\n"
464                    "\n"
465                    "}",
466                    LLVMWithNoNamespaceFix));
467   EXPECT_EQ("namespace {\n"
468             "int i;\n"
469             "}",
470             format("namespace {\n"
471                    "int i;\n"
472                    "}",
473                    LLVMWithNoNamespaceFix));
474   EXPECT_EQ("namespace {\n"
475             "int i;\n"
476             "\n"
477             "};",
478             format("namespace {\n"
479                    "int i;\n"
480                    "\n"
481                    "};",
482                    LLVMWithNoNamespaceFix));
483   EXPECT_EQ("namespace {\n"
484             "int i;\n"
485             "};",
486             format("namespace {\n"
487                    "int i;\n"
488                    "};",
489                    LLVMWithNoNamespaceFix));
490   EXPECT_EQ("namespace {\n"
491             "int i;\n"
492             "\n"
493             "}",
494             format("namespace {\n"
495                    "int i;\n"
496                    "\n"
497                    "}"));
498   EXPECT_EQ("namespace {\n"
499             "int i;\n"
500             "\n"
501             "} // namespace",
502             format("namespace {\n"
503                    "int i;\n"
504                    "\n"
505                    "}  // namespace"));
506 
507   FormatStyle Style = getLLVMStyle();
508   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
509   Style.MaxEmptyLinesToKeep = 2;
510   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
511   Style.BraceWrapping.AfterClass = true;
512   Style.BraceWrapping.AfterFunction = true;
513   Style.KeepEmptyLinesAtTheStartOfBlocks = false;
514 
515   EXPECT_EQ("class Foo\n"
516             "{\n"
517             "  Foo() {}\n"
518             "\n"
519             "  void funk() {}\n"
520             "};",
521             format("class Foo\n"
522                    "{\n"
523                    "  Foo()\n"
524                    "  {\n"
525                    "  }\n"
526                    "\n"
527                    "  void funk() {}\n"
528                    "};",
529                    Style));
530 }
531 
532 TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) {
533   verifyFormat("x = (a) and (b);");
534   verifyFormat("x = (a) or (b);");
535   verifyFormat("x = (a) bitand (b);");
536   verifyFormat("x = (a) bitor (b);");
537   verifyFormat("x = (a) not_eq (b);");
538   verifyFormat("x = (a) and_eq (b);");
539   verifyFormat("x = (a) or_eq (b);");
540   verifyFormat("x = (a) xor (b);");
541 }
542 
543 TEST_F(FormatTest, RecognizesUnaryOperatorKeywords) {
544   verifyFormat("x = compl(a);");
545   verifyFormat("x = not(a);");
546   verifyFormat("x = bitand(a);");
547   // Unary operator must not be merged with the next identifier
548   verifyFormat("x = compl a;");
549   verifyFormat("x = not a;");
550   verifyFormat("x = bitand a;");
551 }
552 
553 //===----------------------------------------------------------------------===//
554 // Tests for control statements.
555 //===----------------------------------------------------------------------===//
556 
557 TEST_F(FormatTest, FormatIfWithoutCompoundStatement) {
558   verifyFormat("if (true)\n  f();\ng();");
559   verifyFormat("if (a)\n  if (b)\n    if (c)\n      g();\nh();");
560   verifyFormat("if (a)\n  if (b) {\n    f();\n  }\ng();");
561   verifyFormat("if constexpr (true)\n"
562                "  f();\ng();");
563   verifyFormat("if CONSTEXPR (true)\n"
564                "  f();\ng();");
565   verifyFormat("if constexpr (a)\n"
566                "  if constexpr (b)\n"
567                "    if constexpr (c)\n"
568                "      g();\n"
569                "h();");
570   verifyFormat("if CONSTEXPR (a)\n"
571                "  if CONSTEXPR (b)\n"
572                "    if CONSTEXPR (c)\n"
573                "      g();\n"
574                "h();");
575   verifyFormat("if constexpr (a)\n"
576                "  if constexpr (b) {\n"
577                "    f();\n"
578                "  }\n"
579                "g();");
580   verifyFormat("if CONSTEXPR (a)\n"
581                "  if CONSTEXPR (b) {\n"
582                "    f();\n"
583                "  }\n"
584                "g();");
585 
586   verifyFormat("if (a)\n"
587                "  g();");
588   verifyFormat("if (a) {\n"
589                "  g()\n"
590                "};");
591   verifyFormat("if (a)\n"
592                "  g();\n"
593                "else\n"
594                "  g();");
595   verifyFormat("if (a) {\n"
596                "  g();\n"
597                "} else\n"
598                "  g();");
599   verifyFormat("if (a)\n"
600                "  g();\n"
601                "else {\n"
602                "  g();\n"
603                "}");
604   verifyFormat("if (a) {\n"
605                "  g();\n"
606                "} else {\n"
607                "  g();\n"
608                "}");
609   verifyFormat("if (a)\n"
610                "  g();\n"
611                "else if (b)\n"
612                "  g();\n"
613                "else\n"
614                "  g();");
615   verifyFormat("if (a) {\n"
616                "  g();\n"
617                "} else if (b)\n"
618                "  g();\n"
619                "else\n"
620                "  g();");
621   verifyFormat("if (a)\n"
622                "  g();\n"
623                "else if (b) {\n"
624                "  g();\n"
625                "} else\n"
626                "  g();");
627   verifyFormat("if (a)\n"
628                "  g();\n"
629                "else if (b)\n"
630                "  g();\n"
631                "else {\n"
632                "  g();\n"
633                "}");
634   verifyFormat("if (a)\n"
635                "  g();\n"
636                "else if (b) {\n"
637                "  g();\n"
638                "} else {\n"
639                "  g();\n"
640                "}");
641   verifyFormat("if (a) {\n"
642                "  g();\n"
643                "} else if (b) {\n"
644                "  g();\n"
645                "} else {\n"
646                "  g();\n"
647                "}");
648 
649   FormatStyle AllowsMergedIf = getLLVMStyle();
650   AllowsMergedIf.IfMacros.push_back("MYIF");
651   AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left;
652   AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
653       FormatStyle::SIS_WithoutElse;
654   verifyFormat("if (a)\n"
655                "  // comment\n"
656                "  f();",
657                AllowsMergedIf);
658   verifyFormat("{\n"
659                "  if (a)\n"
660                "  label:\n"
661                "    f();\n"
662                "}",
663                AllowsMergedIf);
664   verifyFormat("#define A \\\n"
665                "  if (a)  \\\n"
666                "  label:  \\\n"
667                "    f()",
668                AllowsMergedIf);
669   verifyFormat("if (a)\n"
670                "  ;",
671                AllowsMergedIf);
672   verifyFormat("if (a)\n"
673                "  if (b) return;",
674                AllowsMergedIf);
675 
676   verifyFormat("if (a) // Can't merge this\n"
677                "  f();\n",
678                AllowsMergedIf);
679   verifyFormat("if (a) /* still don't merge */\n"
680                "  f();",
681                AllowsMergedIf);
682   verifyFormat("if (a) { // Never merge this\n"
683                "  f();\n"
684                "}",
685                AllowsMergedIf);
686   verifyFormat("if (a) { /* Never merge this */\n"
687                "  f();\n"
688                "}",
689                AllowsMergedIf);
690   verifyFormat("MYIF (a)\n"
691                "  // comment\n"
692                "  f();",
693                AllowsMergedIf);
694   verifyFormat("{\n"
695                "  MYIF (a)\n"
696                "  label:\n"
697                "    f();\n"
698                "}",
699                AllowsMergedIf);
700   verifyFormat("#define A  \\\n"
701                "  MYIF (a) \\\n"
702                "  label:   \\\n"
703                "    f()",
704                AllowsMergedIf);
705   verifyFormat("MYIF (a)\n"
706                "  ;",
707                AllowsMergedIf);
708   verifyFormat("MYIF (a)\n"
709                "  MYIF (b) return;",
710                AllowsMergedIf);
711 
712   verifyFormat("MYIF (a) // Can't merge this\n"
713                "  f();\n",
714                AllowsMergedIf);
715   verifyFormat("MYIF (a) /* still don't merge */\n"
716                "  f();",
717                AllowsMergedIf);
718   verifyFormat("MYIF (a) { // Never merge this\n"
719                "  f();\n"
720                "}",
721                AllowsMergedIf);
722   verifyFormat("MYIF (a) { /* Never merge this */\n"
723                "  f();\n"
724                "}",
725                AllowsMergedIf);
726 
727   AllowsMergedIf.ColumnLimit = 14;
728   // Where line-lengths matter, a 2-letter synonym that maintains line length.
729   // Not IF to avoid any confusion that IF is somehow special.
730   AllowsMergedIf.IfMacros.push_back("FI");
731   verifyFormat("if (a) return;", AllowsMergedIf);
732   verifyFormat("if (aaaaaaaaa)\n"
733                "  return;",
734                AllowsMergedIf);
735   verifyFormat("FI (a) return;", AllowsMergedIf);
736   verifyFormat("FI (aaaaaaaaa)\n"
737                "  return;",
738                AllowsMergedIf);
739 
740   AllowsMergedIf.ColumnLimit = 13;
741   verifyFormat("if (a)\n  return;", AllowsMergedIf);
742   verifyFormat("FI (a)\n  return;", AllowsMergedIf);
743 
744   FormatStyle AllowsMergedIfElse = getLLVMStyle();
745   AllowsMergedIfElse.IfMacros.push_back("MYIF");
746   AllowsMergedIfElse.AllowShortIfStatementsOnASingleLine =
747       FormatStyle::SIS_AllIfsAndElse;
748   verifyFormat("if (a)\n"
749                "  // comment\n"
750                "  f();\n"
751                "else\n"
752                "  // comment\n"
753                "  f();",
754                AllowsMergedIfElse);
755   verifyFormat("{\n"
756                "  if (a)\n"
757                "  label:\n"
758                "    f();\n"
759                "  else\n"
760                "  label:\n"
761                "    f();\n"
762                "}",
763                AllowsMergedIfElse);
764   verifyFormat("if (a)\n"
765                "  ;\n"
766                "else\n"
767                "  ;",
768                AllowsMergedIfElse);
769   verifyFormat("if (a) {\n"
770                "} else {\n"
771                "}",
772                AllowsMergedIfElse);
773   verifyFormat("if (a) return;\n"
774                "else if (b) return;\n"
775                "else return;",
776                AllowsMergedIfElse);
777   verifyFormat("if (a) {\n"
778                "} else return;",
779                AllowsMergedIfElse);
780   verifyFormat("if (a) {\n"
781                "} else if (b) return;\n"
782                "else return;",
783                AllowsMergedIfElse);
784   verifyFormat("if (a) return;\n"
785                "else if (b) {\n"
786                "} else return;",
787                AllowsMergedIfElse);
788   verifyFormat("if (a)\n"
789                "  if (b) return;\n"
790                "  else return;",
791                AllowsMergedIfElse);
792   verifyFormat("if constexpr (a)\n"
793                "  if constexpr (b) return;\n"
794                "  else if constexpr (c) return;\n"
795                "  else return;",
796                AllowsMergedIfElse);
797   verifyFormat("MYIF (a)\n"
798                "  // comment\n"
799                "  f();\n"
800                "else\n"
801                "  // comment\n"
802                "  f();",
803                AllowsMergedIfElse);
804   verifyFormat("{\n"
805                "  MYIF (a)\n"
806                "  label:\n"
807                "    f();\n"
808                "  else\n"
809                "  label:\n"
810                "    f();\n"
811                "}",
812                AllowsMergedIfElse);
813   verifyFormat("MYIF (a)\n"
814                "  ;\n"
815                "else\n"
816                "  ;",
817                AllowsMergedIfElse);
818   verifyFormat("MYIF (a) {\n"
819                "} else {\n"
820                "}",
821                AllowsMergedIfElse);
822   verifyFormat("MYIF (a) return;\n"
823                "else MYIF (b) return;\n"
824                "else return;",
825                AllowsMergedIfElse);
826   verifyFormat("MYIF (a) {\n"
827                "} else return;",
828                AllowsMergedIfElse);
829   verifyFormat("MYIF (a) {\n"
830                "} else MYIF (b) return;\n"
831                "else return;",
832                AllowsMergedIfElse);
833   verifyFormat("MYIF (a) return;\n"
834                "else MYIF (b) {\n"
835                "} else return;",
836                AllowsMergedIfElse);
837   verifyFormat("MYIF (a)\n"
838                "  MYIF (b) return;\n"
839                "  else return;",
840                AllowsMergedIfElse);
841   verifyFormat("MYIF constexpr (a)\n"
842                "  MYIF constexpr (b) return;\n"
843                "  else MYIF constexpr (c) return;\n"
844                "  else return;",
845                AllowsMergedIfElse);
846 }
847 
848 TEST_F(FormatTest, FormatIfWithoutCompoundStatementButElseWith) {
849   FormatStyle AllowsMergedIf = getLLVMStyle();
850   AllowsMergedIf.IfMacros.push_back("MYIF");
851   AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left;
852   AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
853       FormatStyle::SIS_WithoutElse;
854   verifyFormat("if (a)\n"
855                "  f();\n"
856                "else {\n"
857                "  g();\n"
858                "}",
859                AllowsMergedIf);
860   verifyFormat("if (a)\n"
861                "  f();\n"
862                "else\n"
863                "  g();\n",
864                AllowsMergedIf);
865 
866   verifyFormat("if (a) g();", AllowsMergedIf);
867   verifyFormat("if (a) {\n"
868                "  g()\n"
869                "};",
870                AllowsMergedIf);
871   verifyFormat("if (a)\n"
872                "  g();\n"
873                "else\n"
874                "  g();",
875                AllowsMergedIf);
876   verifyFormat("if (a) {\n"
877                "  g();\n"
878                "} else\n"
879                "  g();",
880                AllowsMergedIf);
881   verifyFormat("if (a)\n"
882                "  g();\n"
883                "else {\n"
884                "  g();\n"
885                "}",
886                AllowsMergedIf);
887   verifyFormat("if (a) {\n"
888                "  g();\n"
889                "} else {\n"
890                "  g();\n"
891                "}",
892                AllowsMergedIf);
893   verifyFormat("if (a)\n"
894                "  g();\n"
895                "else if (b)\n"
896                "  g();\n"
897                "else\n"
898                "  g();",
899                AllowsMergedIf);
900   verifyFormat("if (a) {\n"
901                "  g();\n"
902                "} else if (b)\n"
903                "  g();\n"
904                "else\n"
905                "  g();",
906                AllowsMergedIf);
907   verifyFormat("if (a)\n"
908                "  g();\n"
909                "else if (b) {\n"
910                "  g();\n"
911                "} else\n"
912                "  g();",
913                AllowsMergedIf);
914   verifyFormat("if (a)\n"
915                "  g();\n"
916                "else if (b)\n"
917                "  g();\n"
918                "else {\n"
919                "  g();\n"
920                "}",
921                AllowsMergedIf);
922   verifyFormat("if (a)\n"
923                "  g();\n"
924                "else if (b) {\n"
925                "  g();\n"
926                "} else {\n"
927                "  g();\n"
928                "}",
929                AllowsMergedIf);
930   verifyFormat("if (a) {\n"
931                "  g();\n"
932                "} else if (b) {\n"
933                "  g();\n"
934                "} else {\n"
935                "  g();\n"
936                "}",
937                AllowsMergedIf);
938   verifyFormat("MYIF (a)\n"
939                "  f();\n"
940                "else {\n"
941                "  g();\n"
942                "}",
943                AllowsMergedIf);
944   verifyFormat("MYIF (a)\n"
945                "  f();\n"
946                "else\n"
947                "  g();\n",
948                AllowsMergedIf);
949 
950   verifyFormat("MYIF (a) g();", AllowsMergedIf);
951   verifyFormat("MYIF (a) {\n"
952                "  g()\n"
953                "};",
954                AllowsMergedIf);
955   verifyFormat("MYIF (a)\n"
956                "  g();\n"
957                "else\n"
958                "  g();",
959                AllowsMergedIf);
960   verifyFormat("MYIF (a) {\n"
961                "  g();\n"
962                "} else\n"
963                "  g();",
964                AllowsMergedIf);
965   verifyFormat("MYIF (a)\n"
966                "  g();\n"
967                "else {\n"
968                "  g();\n"
969                "}",
970                AllowsMergedIf);
971   verifyFormat("MYIF (a) {\n"
972                "  g();\n"
973                "} else {\n"
974                "  g();\n"
975                "}",
976                AllowsMergedIf);
977   verifyFormat("MYIF (a)\n"
978                "  g();\n"
979                "else MYIF (b)\n"
980                "  g();\n"
981                "else\n"
982                "  g();",
983                AllowsMergedIf);
984   verifyFormat("MYIF (a)\n"
985                "  g();\n"
986                "else if (b)\n"
987                "  g();\n"
988                "else\n"
989                "  g();",
990                AllowsMergedIf);
991   verifyFormat("MYIF (a) {\n"
992                "  g();\n"
993                "} else MYIF (b)\n"
994                "  g();\n"
995                "else\n"
996                "  g();",
997                AllowsMergedIf);
998   verifyFormat("MYIF (a) {\n"
999                "  g();\n"
1000                "} else if (b)\n"
1001                "  g();\n"
1002                "else\n"
1003                "  g();",
1004                AllowsMergedIf);
1005   verifyFormat("MYIF (a)\n"
1006                "  g();\n"
1007                "else MYIF (b) {\n"
1008                "  g();\n"
1009                "} else\n"
1010                "  g();",
1011                AllowsMergedIf);
1012   verifyFormat("MYIF (a)\n"
1013                "  g();\n"
1014                "else if (b) {\n"
1015                "  g();\n"
1016                "} else\n"
1017                "  g();",
1018                AllowsMergedIf);
1019   verifyFormat("MYIF (a)\n"
1020                "  g();\n"
1021                "else MYIF (b)\n"
1022                "  g();\n"
1023                "else {\n"
1024                "  g();\n"
1025                "}",
1026                AllowsMergedIf);
1027   verifyFormat("MYIF (a)\n"
1028                "  g();\n"
1029                "else if (b)\n"
1030                "  g();\n"
1031                "else {\n"
1032                "  g();\n"
1033                "}",
1034                AllowsMergedIf);
1035   verifyFormat("MYIF (a)\n"
1036                "  g();\n"
1037                "else MYIF (b) {\n"
1038                "  g();\n"
1039                "} else {\n"
1040                "  g();\n"
1041                "}",
1042                AllowsMergedIf);
1043   verifyFormat("MYIF (a)\n"
1044                "  g();\n"
1045                "else if (b) {\n"
1046                "  g();\n"
1047                "} else {\n"
1048                "  g();\n"
1049                "}",
1050                AllowsMergedIf);
1051   verifyFormat("MYIF (a) {\n"
1052                "  g();\n"
1053                "} else MYIF (b) {\n"
1054                "  g();\n"
1055                "} else {\n"
1056                "  g();\n"
1057                "}",
1058                AllowsMergedIf);
1059   verifyFormat("MYIF (a) {\n"
1060                "  g();\n"
1061                "} else if (b) {\n"
1062                "  g();\n"
1063                "} else {\n"
1064                "  g();\n"
1065                "}",
1066                AllowsMergedIf);
1067 
1068   AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
1069       FormatStyle::SIS_OnlyFirstIf;
1070 
1071   verifyFormat("if (a) f();\n"
1072                "else {\n"
1073                "  g();\n"
1074                "}",
1075                AllowsMergedIf);
1076   verifyFormat("if (a) f();\n"
1077                "else {\n"
1078                "  if (a) f();\n"
1079                "  else {\n"
1080                "    g();\n"
1081                "  }\n"
1082                "  g();\n"
1083                "}",
1084                AllowsMergedIf);
1085 
1086   verifyFormat("if (a) g();", AllowsMergedIf);
1087   verifyFormat("if (a) {\n"
1088                "  g()\n"
1089                "};",
1090                AllowsMergedIf);
1091   verifyFormat("if (a) g();\n"
1092                "else\n"
1093                "  g();",
1094                AllowsMergedIf);
1095   verifyFormat("if (a) {\n"
1096                "  g();\n"
1097                "} else\n"
1098                "  g();",
1099                AllowsMergedIf);
1100   verifyFormat("if (a) g();\n"
1101                "else {\n"
1102                "  g();\n"
1103                "}",
1104                AllowsMergedIf);
1105   verifyFormat("if (a) {\n"
1106                "  g();\n"
1107                "} else {\n"
1108                "  g();\n"
1109                "}",
1110                AllowsMergedIf);
1111   verifyFormat("if (a) g();\n"
1112                "else if (b)\n"
1113                "  g();\n"
1114                "else\n"
1115                "  g();",
1116                AllowsMergedIf);
1117   verifyFormat("if (a) {\n"
1118                "  g();\n"
1119                "} else if (b)\n"
1120                "  g();\n"
1121                "else\n"
1122                "  g();",
1123                AllowsMergedIf);
1124   verifyFormat("if (a) g();\n"
1125                "else if (b) {\n"
1126                "  g();\n"
1127                "} else\n"
1128                "  g();",
1129                AllowsMergedIf);
1130   verifyFormat("if (a) g();\n"
1131                "else if (b)\n"
1132                "  g();\n"
1133                "else {\n"
1134                "  g();\n"
1135                "}",
1136                AllowsMergedIf);
1137   verifyFormat("if (a) g();\n"
1138                "else if (b) {\n"
1139                "  g();\n"
1140                "} else {\n"
1141                "  g();\n"
1142                "}",
1143                AllowsMergedIf);
1144   verifyFormat("if (a) {\n"
1145                "  g();\n"
1146                "} else if (b) {\n"
1147                "  g();\n"
1148                "} else {\n"
1149                "  g();\n"
1150                "}",
1151                AllowsMergedIf);
1152   verifyFormat("MYIF (a) f();\n"
1153                "else {\n"
1154                "  g();\n"
1155                "}",
1156                AllowsMergedIf);
1157   verifyFormat("MYIF (a) f();\n"
1158                "else {\n"
1159                "  if (a) f();\n"
1160                "  else {\n"
1161                "    g();\n"
1162                "  }\n"
1163                "  g();\n"
1164                "}",
1165                AllowsMergedIf);
1166 
1167   verifyFormat("MYIF (a) g();", AllowsMergedIf);
1168   verifyFormat("MYIF (a) {\n"
1169                "  g()\n"
1170                "};",
1171                AllowsMergedIf);
1172   verifyFormat("MYIF (a) g();\n"
1173                "else\n"
1174                "  g();",
1175                AllowsMergedIf);
1176   verifyFormat("MYIF (a) {\n"
1177                "  g();\n"
1178                "} else\n"
1179                "  g();",
1180                AllowsMergedIf);
1181   verifyFormat("MYIF (a) g();\n"
1182                "else {\n"
1183                "  g();\n"
1184                "}",
1185                AllowsMergedIf);
1186   verifyFormat("MYIF (a) {\n"
1187                "  g();\n"
1188                "} else {\n"
1189                "  g();\n"
1190                "}",
1191                AllowsMergedIf);
1192   verifyFormat("MYIF (a) g();\n"
1193                "else MYIF (b)\n"
1194                "  g();\n"
1195                "else\n"
1196                "  g();",
1197                AllowsMergedIf);
1198   verifyFormat("MYIF (a) g();\n"
1199                "else if (b)\n"
1200                "  g();\n"
1201                "else\n"
1202                "  g();",
1203                AllowsMergedIf);
1204   verifyFormat("MYIF (a) {\n"
1205                "  g();\n"
1206                "} else MYIF (b)\n"
1207                "  g();\n"
1208                "else\n"
1209                "  g();",
1210                AllowsMergedIf);
1211   verifyFormat("MYIF (a) {\n"
1212                "  g();\n"
1213                "} else if (b)\n"
1214                "  g();\n"
1215                "else\n"
1216                "  g();",
1217                AllowsMergedIf);
1218   verifyFormat("MYIF (a) g();\n"
1219                "else MYIF (b) {\n"
1220                "  g();\n"
1221                "} else\n"
1222                "  g();",
1223                AllowsMergedIf);
1224   verifyFormat("MYIF (a) g();\n"
1225                "else if (b) {\n"
1226                "  g();\n"
1227                "} else\n"
1228                "  g();",
1229                AllowsMergedIf);
1230   verifyFormat("MYIF (a) g();\n"
1231                "else MYIF (b)\n"
1232                "  g();\n"
1233                "else {\n"
1234                "  g();\n"
1235                "}",
1236                AllowsMergedIf);
1237   verifyFormat("MYIF (a) g();\n"
1238                "else if (b)\n"
1239                "  g();\n"
1240                "else {\n"
1241                "  g();\n"
1242                "}",
1243                AllowsMergedIf);
1244   verifyFormat("MYIF (a) g();\n"
1245                "else MYIF (b) {\n"
1246                "  g();\n"
1247                "} else {\n"
1248                "  g();\n"
1249                "}",
1250                AllowsMergedIf);
1251   verifyFormat("MYIF (a) g();\n"
1252                "else if (b) {\n"
1253                "  g();\n"
1254                "} else {\n"
1255                "  g();\n"
1256                "}",
1257                AllowsMergedIf);
1258   verifyFormat("MYIF (a) {\n"
1259                "  g();\n"
1260                "} else MYIF (b) {\n"
1261                "  g();\n"
1262                "} else {\n"
1263                "  g();\n"
1264                "}",
1265                AllowsMergedIf);
1266   verifyFormat("MYIF (a) {\n"
1267                "  g();\n"
1268                "} else if (b) {\n"
1269                "  g();\n"
1270                "} else {\n"
1271                "  g();\n"
1272                "}",
1273                AllowsMergedIf);
1274 
1275   AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
1276       FormatStyle::SIS_AllIfsAndElse;
1277 
1278   verifyFormat("if (a) f();\n"
1279                "else {\n"
1280                "  g();\n"
1281                "}",
1282                AllowsMergedIf);
1283   verifyFormat("if (a) f();\n"
1284                "else {\n"
1285                "  if (a) f();\n"
1286                "  else {\n"
1287                "    g();\n"
1288                "  }\n"
1289                "  g();\n"
1290                "}",
1291                AllowsMergedIf);
1292 
1293   verifyFormat("if (a) g();", AllowsMergedIf);
1294   verifyFormat("if (a) {\n"
1295                "  g()\n"
1296                "};",
1297                AllowsMergedIf);
1298   verifyFormat("if (a) g();\n"
1299                "else g();",
1300                AllowsMergedIf);
1301   verifyFormat("if (a) {\n"
1302                "  g();\n"
1303                "} else g();",
1304                AllowsMergedIf);
1305   verifyFormat("if (a) g();\n"
1306                "else {\n"
1307                "  g();\n"
1308                "}",
1309                AllowsMergedIf);
1310   verifyFormat("if (a) {\n"
1311                "  g();\n"
1312                "} else {\n"
1313                "  g();\n"
1314                "}",
1315                AllowsMergedIf);
1316   verifyFormat("if (a) g();\n"
1317                "else if (b) g();\n"
1318                "else g();",
1319                AllowsMergedIf);
1320   verifyFormat("if (a) {\n"
1321                "  g();\n"
1322                "} else if (b) g();\n"
1323                "else g();",
1324                AllowsMergedIf);
1325   verifyFormat("if (a) g();\n"
1326                "else if (b) {\n"
1327                "  g();\n"
1328                "} else g();",
1329                AllowsMergedIf);
1330   verifyFormat("if (a) g();\n"
1331                "else if (b) g();\n"
1332                "else {\n"
1333                "  g();\n"
1334                "}",
1335                AllowsMergedIf);
1336   verifyFormat("if (a) g();\n"
1337                "else if (b) {\n"
1338                "  g();\n"
1339                "} else {\n"
1340                "  g();\n"
1341                "}",
1342                AllowsMergedIf);
1343   verifyFormat("if (a) {\n"
1344                "  g();\n"
1345                "} else if (b) {\n"
1346                "  g();\n"
1347                "} else {\n"
1348                "  g();\n"
1349                "}",
1350                AllowsMergedIf);
1351   verifyFormat("MYIF (a) f();\n"
1352                "else {\n"
1353                "  g();\n"
1354                "}",
1355                AllowsMergedIf);
1356   verifyFormat("MYIF (a) f();\n"
1357                "else {\n"
1358                "  if (a) f();\n"
1359                "  else {\n"
1360                "    g();\n"
1361                "  }\n"
1362                "  g();\n"
1363                "}",
1364                AllowsMergedIf);
1365 
1366   verifyFormat("MYIF (a) g();", AllowsMergedIf);
1367   verifyFormat("MYIF (a) {\n"
1368                "  g()\n"
1369                "};",
1370                AllowsMergedIf);
1371   verifyFormat("MYIF (a) g();\n"
1372                "else g();",
1373                AllowsMergedIf);
1374   verifyFormat("MYIF (a) {\n"
1375                "  g();\n"
1376                "} else g();",
1377                AllowsMergedIf);
1378   verifyFormat("MYIF (a) g();\n"
1379                "else {\n"
1380                "  g();\n"
1381                "}",
1382                AllowsMergedIf);
1383   verifyFormat("MYIF (a) {\n"
1384                "  g();\n"
1385                "} else {\n"
1386                "  g();\n"
1387                "}",
1388                AllowsMergedIf);
1389   verifyFormat("MYIF (a) g();\n"
1390                "else MYIF (b) g();\n"
1391                "else g();",
1392                AllowsMergedIf);
1393   verifyFormat("MYIF (a) g();\n"
1394                "else if (b) g();\n"
1395                "else g();",
1396                AllowsMergedIf);
1397   verifyFormat("MYIF (a) {\n"
1398                "  g();\n"
1399                "} else MYIF (b) g();\n"
1400                "else g();",
1401                AllowsMergedIf);
1402   verifyFormat("MYIF (a) {\n"
1403                "  g();\n"
1404                "} else if (b) g();\n"
1405                "else g();",
1406                AllowsMergedIf);
1407   verifyFormat("MYIF (a) g();\n"
1408                "else MYIF (b) {\n"
1409                "  g();\n"
1410                "} else g();",
1411                AllowsMergedIf);
1412   verifyFormat("MYIF (a) g();\n"
1413                "else if (b) {\n"
1414                "  g();\n"
1415                "} else g();",
1416                AllowsMergedIf);
1417   verifyFormat("MYIF (a) g();\n"
1418                "else MYIF (b) g();\n"
1419                "else {\n"
1420                "  g();\n"
1421                "}",
1422                AllowsMergedIf);
1423   verifyFormat("MYIF (a) g();\n"
1424                "else if (b) g();\n"
1425                "else {\n"
1426                "  g();\n"
1427                "}",
1428                AllowsMergedIf);
1429   verifyFormat("MYIF (a) g();\n"
1430                "else MYIF (b) {\n"
1431                "  g();\n"
1432                "} else {\n"
1433                "  g();\n"
1434                "}",
1435                AllowsMergedIf);
1436   verifyFormat("MYIF (a) g();\n"
1437                "else if (b) {\n"
1438                "  g();\n"
1439                "} else {\n"
1440                "  g();\n"
1441                "}",
1442                AllowsMergedIf);
1443   verifyFormat("MYIF (a) {\n"
1444                "  g();\n"
1445                "} else MYIF (b) {\n"
1446                "  g();\n"
1447                "} else {\n"
1448                "  g();\n"
1449                "}",
1450                AllowsMergedIf);
1451   verifyFormat("MYIF (a) {\n"
1452                "  g();\n"
1453                "} else if (b) {\n"
1454                "  g();\n"
1455                "} else {\n"
1456                "  g();\n"
1457                "}",
1458                AllowsMergedIf);
1459 }
1460 
1461 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
1462   FormatStyle AllowsMergedLoops = getLLVMStyle();
1463   AllowsMergedLoops.AllowShortLoopsOnASingleLine = true;
1464   verifyFormat("while (true) continue;", AllowsMergedLoops);
1465   verifyFormat("for (;;) continue;", AllowsMergedLoops);
1466   verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops);
1467   verifyFormat("BOOST_FOREACH (int &v, vec) v *= 2;", AllowsMergedLoops);
1468   verifyFormat("while (true)\n"
1469                "  ;",
1470                AllowsMergedLoops);
1471   verifyFormat("for (;;)\n"
1472                "  ;",
1473                AllowsMergedLoops);
1474   verifyFormat("for (;;)\n"
1475                "  for (;;) continue;",
1476                AllowsMergedLoops);
1477   verifyFormat("for (;;)\n"
1478                "  while (true) continue;",
1479                AllowsMergedLoops);
1480   verifyFormat("while (true)\n"
1481                "  for (;;) continue;",
1482                AllowsMergedLoops);
1483   verifyFormat("BOOST_FOREACH (int &v, vec)\n"
1484                "  for (;;) continue;",
1485                AllowsMergedLoops);
1486   verifyFormat("for (;;)\n"
1487                "  BOOST_FOREACH (int &v, vec) continue;",
1488                AllowsMergedLoops);
1489   verifyFormat("for (;;) // Can't merge this\n"
1490                "  continue;",
1491                AllowsMergedLoops);
1492   verifyFormat("for (;;) /* still don't merge */\n"
1493                "  continue;",
1494                AllowsMergedLoops);
1495   verifyFormat("do a++;\n"
1496                "while (true);",
1497                AllowsMergedLoops);
1498   verifyFormat("do /* Don't merge */\n"
1499                "  a++;\n"
1500                "while (true);",
1501                AllowsMergedLoops);
1502   verifyFormat("do // Don't merge\n"
1503                "  a++;\n"
1504                "while (true);",
1505                AllowsMergedLoops);
1506   verifyFormat("do\n"
1507                "  // Don't merge\n"
1508                "  a++;\n"
1509                "while (true);",
1510                AllowsMergedLoops);
1511   // Without braces labels are interpreted differently.
1512   verifyFormat("{\n"
1513                "  do\n"
1514                "  label:\n"
1515                "    a++;\n"
1516                "  while (true);\n"
1517                "}",
1518                AllowsMergedLoops);
1519 }
1520 
1521 TEST_F(FormatTest, FormatShortBracedStatements) {
1522   FormatStyle AllowSimpleBracedStatements = getLLVMStyle();
1523   AllowSimpleBracedStatements.IfMacros.push_back("MYIF");
1524   // Where line-lengths matter, a 2-letter synonym that maintains line length.
1525   // Not IF to avoid any confusion that IF is somehow special.
1526   AllowSimpleBracedStatements.IfMacros.push_back("FI");
1527   AllowSimpleBracedStatements.ColumnLimit = 40;
1528   AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine =
1529       FormatStyle::SBS_Always;
1530 
1531   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
1532       FormatStyle::SIS_WithoutElse;
1533   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
1534 
1535   AllowSimpleBracedStatements.BreakBeforeBraces = FormatStyle::BS_Custom;
1536   AllowSimpleBracedStatements.BraceWrapping.AfterFunction = true;
1537   AllowSimpleBracedStatements.BraceWrapping.SplitEmptyRecord = false;
1538 
1539   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
1540   verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements);
1541   verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements);
1542   verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements);
1543   verifyFormat("MYIF constexpr (true) {}", AllowSimpleBracedStatements);
1544   verifyFormat("MYIF CONSTEXPR (true) {}", AllowSimpleBracedStatements);
1545   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
1546   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
1547   verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
1548   verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements);
1549   verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements);
1550   verifyFormat("MYIF (true) { f(); }", AllowSimpleBracedStatements);
1551   verifyFormat("MYIF constexpr (true) { f(); }", AllowSimpleBracedStatements);
1552   verifyFormat("MYIF CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements);
1553   verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
1554   verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
1555   verifyFormat("if (true) { fffffffffffffffffffffff(); }",
1556                AllowSimpleBracedStatements);
1557   verifyFormat("if (true) {\n"
1558                "  ffffffffffffffffffffffff();\n"
1559                "}",
1560                AllowSimpleBracedStatements);
1561   verifyFormat("if (true) {\n"
1562                "  ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
1563                "}",
1564                AllowSimpleBracedStatements);
1565   verifyFormat("if (true) { //\n"
1566                "  f();\n"
1567                "}",
1568                AllowSimpleBracedStatements);
1569   verifyFormat("if (true) {\n"
1570                "  f();\n"
1571                "  f();\n"
1572                "}",
1573                AllowSimpleBracedStatements);
1574   verifyFormat("if (true) {\n"
1575                "  f();\n"
1576                "} else {\n"
1577                "  f();\n"
1578                "}",
1579                AllowSimpleBracedStatements);
1580   verifyFormat("FI (true) { fffffffffffffffffffffff(); }",
1581                AllowSimpleBracedStatements);
1582   verifyFormat("MYIF (true) {\n"
1583                "  ffffffffffffffffffffffff();\n"
1584                "}",
1585                AllowSimpleBracedStatements);
1586   verifyFormat("MYIF (true) {\n"
1587                "  ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
1588                "}",
1589                AllowSimpleBracedStatements);
1590   verifyFormat("MYIF (true) { //\n"
1591                "  f();\n"
1592                "}",
1593                AllowSimpleBracedStatements);
1594   verifyFormat("MYIF (true) {\n"
1595                "  f();\n"
1596                "  f();\n"
1597                "}",
1598                AllowSimpleBracedStatements);
1599   verifyFormat("MYIF (true) {\n"
1600                "  f();\n"
1601                "} else {\n"
1602                "  f();\n"
1603                "}",
1604                AllowSimpleBracedStatements);
1605 
1606   verifyFormat("struct A2 {\n"
1607                "  int X;\n"
1608                "};",
1609                AllowSimpleBracedStatements);
1610   verifyFormat("typedef struct A2 {\n"
1611                "  int X;\n"
1612                "} A2_t;",
1613                AllowSimpleBracedStatements);
1614   verifyFormat("template <int> struct A2 {\n"
1615                "  struct B {};\n"
1616                "};",
1617                AllowSimpleBracedStatements);
1618 
1619   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
1620       FormatStyle::SIS_Never;
1621   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
1622   verifyFormat("if (true) {\n"
1623                "  f();\n"
1624                "}",
1625                AllowSimpleBracedStatements);
1626   verifyFormat("if (true) {\n"
1627                "  f();\n"
1628                "} else {\n"
1629                "  f();\n"
1630                "}",
1631                AllowSimpleBracedStatements);
1632   verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements);
1633   verifyFormat("MYIF (true) {\n"
1634                "  f();\n"
1635                "}",
1636                AllowSimpleBracedStatements);
1637   verifyFormat("MYIF (true) {\n"
1638                "  f();\n"
1639                "} else {\n"
1640                "  f();\n"
1641                "}",
1642                AllowSimpleBracedStatements);
1643 
1644   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
1645   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
1646   verifyFormat("while (true) {\n"
1647                "  f();\n"
1648                "}",
1649                AllowSimpleBracedStatements);
1650   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
1651   verifyFormat("for (;;) {\n"
1652                "  f();\n"
1653                "}",
1654                AllowSimpleBracedStatements);
1655   verifyFormat("BOOST_FOREACH (int v, vec) {}", AllowSimpleBracedStatements);
1656   verifyFormat("BOOST_FOREACH (int v, vec) {\n"
1657                "  f();\n"
1658                "}",
1659                AllowSimpleBracedStatements);
1660 
1661   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
1662       FormatStyle::SIS_WithoutElse;
1663   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
1664   AllowSimpleBracedStatements.BraceWrapping.AfterControlStatement =
1665       FormatStyle::BWACS_Always;
1666 
1667   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
1668   verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements);
1669   verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements);
1670   verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements);
1671   verifyFormat("MYIF constexpr (true) {}", AllowSimpleBracedStatements);
1672   verifyFormat("MYIF CONSTEXPR (true) {}", AllowSimpleBracedStatements);
1673   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
1674   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
1675   verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
1676   verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements);
1677   verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements);
1678   verifyFormat("MYIF (true) { f(); }", AllowSimpleBracedStatements);
1679   verifyFormat("MYIF constexpr (true) { f(); }", AllowSimpleBracedStatements);
1680   verifyFormat("MYIF CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements);
1681   verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
1682   verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
1683   verifyFormat("if (true) { fffffffffffffffffffffff(); }",
1684                AllowSimpleBracedStatements);
1685   verifyFormat("if (true)\n"
1686                "{\n"
1687                "  ffffffffffffffffffffffff();\n"
1688                "}",
1689                AllowSimpleBracedStatements);
1690   verifyFormat("if (true)\n"
1691                "{\n"
1692                "  ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
1693                "}",
1694                AllowSimpleBracedStatements);
1695   verifyFormat("if (true)\n"
1696                "{ //\n"
1697                "  f();\n"
1698                "}",
1699                AllowSimpleBracedStatements);
1700   verifyFormat("if (true)\n"
1701                "{\n"
1702                "  f();\n"
1703                "  f();\n"
1704                "}",
1705                AllowSimpleBracedStatements);
1706   verifyFormat("if (true)\n"
1707                "{\n"
1708                "  f();\n"
1709                "} else\n"
1710                "{\n"
1711                "  f();\n"
1712                "}",
1713                AllowSimpleBracedStatements);
1714   verifyFormat("FI (true) { fffffffffffffffffffffff(); }",
1715                AllowSimpleBracedStatements);
1716   verifyFormat("MYIF (true)\n"
1717                "{\n"
1718                "  ffffffffffffffffffffffff();\n"
1719                "}",
1720                AllowSimpleBracedStatements);
1721   verifyFormat("MYIF (true)\n"
1722                "{\n"
1723                "  ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
1724                "}",
1725                AllowSimpleBracedStatements);
1726   verifyFormat("MYIF (true)\n"
1727                "{ //\n"
1728                "  f();\n"
1729                "}",
1730                AllowSimpleBracedStatements);
1731   verifyFormat("MYIF (true)\n"
1732                "{\n"
1733                "  f();\n"
1734                "  f();\n"
1735                "}",
1736                AllowSimpleBracedStatements);
1737   verifyFormat("MYIF (true)\n"
1738                "{\n"
1739                "  f();\n"
1740                "} else\n"
1741                "{\n"
1742                "  f();\n"
1743                "}",
1744                AllowSimpleBracedStatements);
1745 
1746   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
1747       FormatStyle::SIS_Never;
1748   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
1749   verifyFormat("if (true)\n"
1750                "{\n"
1751                "  f();\n"
1752                "}",
1753                AllowSimpleBracedStatements);
1754   verifyFormat("if (true)\n"
1755                "{\n"
1756                "  f();\n"
1757                "} else\n"
1758                "{\n"
1759                "  f();\n"
1760                "}",
1761                AllowSimpleBracedStatements);
1762   verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements);
1763   verifyFormat("MYIF (true)\n"
1764                "{\n"
1765                "  f();\n"
1766                "}",
1767                AllowSimpleBracedStatements);
1768   verifyFormat("MYIF (true)\n"
1769                "{\n"
1770                "  f();\n"
1771                "} else\n"
1772                "{\n"
1773                "  f();\n"
1774                "}",
1775                AllowSimpleBracedStatements);
1776 
1777   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
1778   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
1779   verifyFormat("while (true)\n"
1780                "{\n"
1781                "  f();\n"
1782                "}",
1783                AllowSimpleBracedStatements);
1784   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
1785   verifyFormat("for (;;)\n"
1786                "{\n"
1787                "  f();\n"
1788                "}",
1789                AllowSimpleBracedStatements);
1790   verifyFormat("BOOST_FOREACH (int v, vec) {}", AllowSimpleBracedStatements);
1791   verifyFormat("BOOST_FOREACH (int v, vec)\n"
1792                "{\n"
1793                "  f();\n"
1794                "}",
1795                AllowSimpleBracedStatements);
1796 }
1797 
1798 TEST_F(FormatTest, UnderstandsMacros) {
1799   verifyFormat("#define A (parentheses)");
1800   verifyFormat("#define true ((int)1)");
1801   verifyFormat("#define and(x)");
1802   verifyFormat("#define if(x) x");
1803   verifyFormat("#define return(x) (x)");
1804   verifyFormat("#define while(x) for (; x;)");
1805   verifyFormat("#define xor(x) (^(x))");
1806   verifyFormat("#define __except(x)");
1807   verifyFormat("#define __try(x)");
1808 }
1809 
1810 TEST_F(FormatTest, ShortBlocksInMacrosDontMergeWithCodeAfterMacro) {
1811   FormatStyle Style = getLLVMStyleWithColumns(60);
1812   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
1813   Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse;
1814   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
1815   EXPECT_EQ("#define A                                                  \\\n"
1816             "  if (HANDLEwernufrnuLwrmviferuvnierv)                     \\\n"
1817             "  {                                                        \\\n"
1818             "    RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier;               \\\n"
1819             "  }\n"
1820             "X;",
1821             format("#define A \\\n"
1822                    "   if (HANDLEwernufrnuLwrmviferuvnierv) { \\\n"
1823                    "      RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n"
1824                    "   }\n"
1825                    "X;",
1826                    Style));
1827 }
1828 
1829 TEST_F(FormatTest, ParseIfElse) {
1830   verifyFormat("if (true)\n"
1831                "  if (true)\n"
1832                "    if (true)\n"
1833                "      f();\n"
1834                "    else\n"
1835                "      g();\n"
1836                "  else\n"
1837                "    h();\n"
1838                "else\n"
1839                "  i();");
1840   verifyFormat("if (true)\n"
1841                "  if (true)\n"
1842                "    if (true) {\n"
1843                "      if (true)\n"
1844                "        f();\n"
1845                "    } else {\n"
1846                "      g();\n"
1847                "    }\n"
1848                "  else\n"
1849                "    h();\n"
1850                "else {\n"
1851                "  i();\n"
1852                "}");
1853   verifyFormat("if (true)\n"
1854                "  if constexpr (true)\n"
1855                "    if (true) {\n"
1856                "      if constexpr (true)\n"
1857                "        f();\n"
1858                "    } else {\n"
1859                "      g();\n"
1860                "    }\n"
1861                "  else\n"
1862                "    h();\n"
1863                "else {\n"
1864                "  i();\n"
1865                "}");
1866   verifyFormat("if (true)\n"
1867                "  if CONSTEXPR (true)\n"
1868                "    if (true) {\n"
1869                "      if CONSTEXPR (true)\n"
1870                "        f();\n"
1871                "    } else {\n"
1872                "      g();\n"
1873                "    }\n"
1874                "  else\n"
1875                "    h();\n"
1876                "else {\n"
1877                "  i();\n"
1878                "}");
1879   verifyFormat("void f() {\n"
1880                "  if (a) {\n"
1881                "  } else {\n"
1882                "  }\n"
1883                "}");
1884 }
1885 
1886 TEST_F(FormatTest, ElseIf) {
1887   verifyFormat("if (a) {\n} else if (b) {\n}");
1888   verifyFormat("if (a)\n"
1889                "  f();\n"
1890                "else if (b)\n"
1891                "  g();\n"
1892                "else\n"
1893                "  h();");
1894   verifyFormat("if (a)\n"
1895                "  f();\n"
1896                "else // comment\n"
1897                "  if (b) {\n"
1898                "    g();\n"
1899                "    h();\n"
1900                "  }");
1901   verifyFormat("if constexpr (a)\n"
1902                "  f();\n"
1903                "else if constexpr (b)\n"
1904                "  g();\n"
1905                "else\n"
1906                "  h();");
1907   verifyFormat("if CONSTEXPR (a)\n"
1908                "  f();\n"
1909                "else if CONSTEXPR (b)\n"
1910                "  g();\n"
1911                "else\n"
1912                "  h();");
1913   verifyFormat("if (a) {\n"
1914                "  f();\n"
1915                "}\n"
1916                "// or else ..\n"
1917                "else {\n"
1918                "  g()\n"
1919                "}");
1920 
1921   verifyFormat("if (a) {\n"
1922                "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1923                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
1924                "}");
1925   verifyFormat("if (a) {\n"
1926                "} else if constexpr (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1927                "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
1928                "}");
1929   verifyFormat("if (a) {\n"
1930                "} else if CONSTEXPR (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1931                "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
1932                "}");
1933   verifyFormat("if (a) {\n"
1934                "} else if (\n"
1935                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
1936                "}",
1937                getLLVMStyleWithColumns(62));
1938   verifyFormat("if (a) {\n"
1939                "} else if constexpr (\n"
1940                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
1941                "}",
1942                getLLVMStyleWithColumns(62));
1943   verifyFormat("if (a) {\n"
1944                "} else if CONSTEXPR (\n"
1945                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
1946                "}",
1947                getLLVMStyleWithColumns(62));
1948 }
1949 
1950 TEST_F(FormatTest, SeparatePointerReferenceAlignment) {
1951   FormatStyle Style = getLLVMStyle();
1952   // Check first the default LLVM style
1953   // Style.PointerAlignment = FormatStyle::PAS_Right;
1954   // Style.ReferenceAlignment = FormatStyle::RAS_Pointer;
1955   verifyFormat("int *f1(int *a, int &b, int &&c);", Style);
1956   verifyFormat("int &f2(int &&c, int *a, int &b);", Style);
1957   verifyFormat("int &&f3(int &b, int &&c, int *a);", Style);
1958   verifyFormat("int *f1(int &a) const &;", Style);
1959   verifyFormat("int *f1(int &a) const & = 0;", Style);
1960   verifyFormat("int *a = f1();", Style);
1961   verifyFormat("int &b = f2();", Style);
1962   verifyFormat("int &&c = f3();", Style);
1963   verifyFormat("for (auto a = 0, b = 0; const auto &c : {1, 2, 3})", Style);
1964   verifyFormat("for (auto a = 0, b = 0; const int &c : {1, 2, 3})", Style);
1965   verifyFormat("for (auto a = 0, b = 0; const Foo &c : {1, 2, 3})", Style);
1966   verifyFormat("for (auto a = 0, b = 0; const Foo *c : {1, 2, 3})", Style);
1967   verifyFormat("for (int a = 0, b = 0; const auto &c : {1, 2, 3})", Style);
1968   verifyFormat("for (int a = 0, b = 0; const int &c : {1, 2, 3})", Style);
1969   verifyFormat("for (int a = 0, b = 0; const Foo &c : {1, 2, 3})", Style);
1970   verifyFormat("for (int a = 0, b++; const auto &c : {1, 2, 3})", Style);
1971   verifyFormat("for (int a = 0, b++; const int &c : {1, 2, 3})", Style);
1972   verifyFormat("for (int a = 0, b++; const Foo &c : {1, 2, 3})", Style);
1973   verifyFormat("for (auto x = 0; auto &c : {1, 2, 3})", Style);
1974   verifyFormat("for (auto x = 0; int &c : {1, 2, 3})", Style);
1975   verifyFormat("for (int x = 0; auto &c : {1, 2, 3})", Style);
1976   verifyFormat("for (int x = 0; int &c : {1, 2, 3})", Style);
1977   verifyFormat("for (f(); auto &c : {1, 2, 3})", Style);
1978   verifyFormat("for (f(); int &c : {1, 2, 3})", Style);
1979 
1980   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
1981   verifyFormat("Const unsigned int *c;\n"
1982                "const unsigned int *d;\n"
1983                "Const unsigned int &e;\n"
1984                "const unsigned int &f;\n"
1985                "const unsigned    &&g;\n"
1986                "Const unsigned      h;",
1987                Style);
1988 
1989   Style.PointerAlignment = FormatStyle::PAS_Left;
1990   Style.ReferenceAlignment = FormatStyle::RAS_Pointer;
1991   verifyFormat("int* f1(int* a, int& b, int&& c);", Style);
1992   verifyFormat("int& f2(int&& c, int* a, int& b);", Style);
1993   verifyFormat("int&& f3(int& b, int&& c, int* a);", Style);
1994   verifyFormat("int* f1(int& a) const& = 0;", Style);
1995   verifyFormat("int* a = f1();", Style);
1996   verifyFormat("int& b = f2();", Style);
1997   verifyFormat("int&& c = f3();", Style);
1998   verifyFormat("for (auto a = 0, b = 0; const auto& c : {1, 2, 3})", Style);
1999   verifyFormat("for (auto a = 0, b = 0; const int& c : {1, 2, 3})", Style);
2000   verifyFormat("for (auto a = 0, b = 0; const Foo& c : {1, 2, 3})", Style);
2001   verifyFormat("for (auto a = 0, b = 0; const Foo* c : {1, 2, 3})", Style);
2002   verifyFormat("for (int a = 0, b = 0; const auto& c : {1, 2, 3})", Style);
2003   verifyFormat("for (int a = 0, b = 0; const int& c : {1, 2, 3})", Style);
2004   verifyFormat("for (int a = 0, b = 0; const Foo& c : {1, 2, 3})", Style);
2005   verifyFormat("for (int a = 0, b = 0; const Foo* c : {1, 2, 3})", Style);
2006   verifyFormat("for (int a = 0, b++; const auto& c : {1, 2, 3})", Style);
2007   verifyFormat("for (int a = 0, b++; const int& c : {1, 2, 3})", Style);
2008   verifyFormat("for (int a = 0, b++; const Foo& c : {1, 2, 3})", Style);
2009   verifyFormat("for (int a = 0, b++; const Foo* c : {1, 2, 3})", Style);
2010   verifyFormat("for (auto x = 0; auto& c : {1, 2, 3})", Style);
2011   verifyFormat("for (auto x = 0; int& c : {1, 2, 3})", Style);
2012   verifyFormat("for (int x = 0; auto& c : {1, 2, 3})", Style);
2013   verifyFormat("for (int x = 0; int& c : {1, 2, 3})", Style);
2014   verifyFormat("for (f(); auto& c : {1, 2, 3})", Style);
2015   verifyFormat("for (f(); int& c : {1, 2, 3})", Style);
2016 
2017   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
2018   verifyFormat("Const unsigned int* c;\n"
2019                "const unsigned int* d;\n"
2020                "Const unsigned int& e;\n"
2021                "const unsigned int& f;\n"
2022                "const unsigned&&    g;\n"
2023                "Const unsigned      h;",
2024                Style);
2025 
2026   Style.PointerAlignment = FormatStyle::PAS_Right;
2027   Style.ReferenceAlignment = FormatStyle::RAS_Left;
2028   verifyFormat("int *f1(int *a, int& b, int&& c);", Style);
2029   verifyFormat("int& f2(int&& c, int *a, int& b);", Style);
2030   verifyFormat("int&& f3(int& b, int&& c, int *a);", Style);
2031   verifyFormat("int *a = f1();", Style);
2032   verifyFormat("int& b = f2();", Style);
2033   verifyFormat("int&& c = f3();", Style);
2034   verifyFormat("for (auto a = 0, b = 0; const Foo *c : {1, 2, 3})", Style);
2035   verifyFormat("for (int a = 0, b = 0; const Foo *c : {1, 2, 3})", Style);
2036   verifyFormat("for (int a = 0, b++; const Foo *c : {1, 2, 3})", Style);
2037 
2038   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
2039   verifyFormat("Const unsigned int *c;\n"
2040                "const unsigned int *d;\n"
2041                "Const unsigned int& e;\n"
2042                "const unsigned int& f;\n"
2043                "const unsigned      g;\n"
2044                "Const unsigned      h;",
2045                Style);
2046 
2047   Style.PointerAlignment = FormatStyle::PAS_Left;
2048   Style.ReferenceAlignment = FormatStyle::RAS_Middle;
2049   verifyFormat("int* f1(int* a, int & b, int && c);", Style);
2050   verifyFormat("int & f2(int && c, int* a, int & b);", Style);
2051   verifyFormat("int && f3(int & b, int && c, int* a);", Style);
2052   verifyFormat("int* a = f1();", Style);
2053   verifyFormat("int & b = f2();", Style);
2054   verifyFormat("int && c = f3();", Style);
2055   verifyFormat("for (auto a = 0, b = 0; const auto & c : {1, 2, 3})", Style);
2056   verifyFormat("for (auto a = 0, b = 0; const int & c : {1, 2, 3})", Style);
2057   verifyFormat("for (auto a = 0, b = 0; const Foo & c : {1, 2, 3})", Style);
2058   verifyFormat("for (auto a = 0, b = 0; const Foo* c : {1, 2, 3})", Style);
2059   verifyFormat("for (int a = 0, b++; const auto & c : {1, 2, 3})", Style);
2060   verifyFormat("for (int a = 0, b++; const int & c : {1, 2, 3})", Style);
2061   verifyFormat("for (int a = 0, b++; const Foo & c : {1, 2, 3})", Style);
2062   verifyFormat("for (int a = 0, b++; const Foo* c : {1, 2, 3})", Style);
2063   verifyFormat("for (auto x = 0; auto & c : {1, 2, 3})", Style);
2064   verifyFormat("for (auto x = 0; int & c : {1, 2, 3})", Style);
2065   verifyFormat("for (int x = 0; auto & c : {1, 2, 3})", Style);
2066   verifyFormat("for (int x = 0; int & c : {1, 2, 3})", Style);
2067   verifyFormat("for (f(); auto & c : {1, 2, 3})", Style);
2068   verifyFormat("for (f(); int & c : {1, 2, 3})", Style);
2069 
2070   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
2071   verifyFormat("Const unsigned int*  c;\n"
2072                "const unsigned int*  d;\n"
2073                "Const unsigned int & e;\n"
2074                "const unsigned int & f;\n"
2075                "const unsigned &&    g;\n"
2076                "Const unsigned       h;",
2077                Style);
2078 
2079   Style.PointerAlignment = FormatStyle::PAS_Middle;
2080   Style.ReferenceAlignment = FormatStyle::RAS_Right;
2081   verifyFormat("int * f1(int * a, int &b, int &&c);", Style);
2082   verifyFormat("int &f2(int &&c, int * a, int &b);", Style);
2083   verifyFormat("int &&f3(int &b, int &&c, int * a);", Style);
2084   verifyFormat("int * a = f1();", Style);
2085   verifyFormat("int &b = f2();", Style);
2086   verifyFormat("int &&c = f3();", Style);
2087   verifyFormat("for (auto a = 0, b = 0; const Foo * c : {1, 2, 3})", Style);
2088   verifyFormat("for (int a = 0, b = 0; const Foo * c : {1, 2, 3})", Style);
2089   verifyFormat("for (int a = 0, b++; const Foo * c : {1, 2, 3})", Style);
2090 
2091   // FIXME: we don't handle this yet, so output may be arbitrary until it's
2092   // specifically handled
2093   // verifyFormat("int Add2(BTree * &Root, char * szToAdd)", Style);
2094 }
2095 
2096 TEST_F(FormatTest, FormatsForLoop) {
2097   verifyFormat(
2098       "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
2099       "     ++VeryVeryLongLoopVariable)\n"
2100       "  ;");
2101   verifyFormat("for (;;)\n"
2102                "  f();");
2103   verifyFormat("for (;;) {\n}");
2104   verifyFormat("for (;;) {\n"
2105                "  f();\n"
2106                "}");
2107   verifyFormat("for (int i = 0; (i < 10); ++i) {\n}");
2108 
2109   verifyFormat(
2110       "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
2111       "                                          E = UnwrappedLines.end();\n"
2112       "     I != E; ++I) {\n}");
2113 
2114   verifyFormat(
2115       "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
2116       "     ++IIIII) {\n}");
2117   verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n"
2118                "         aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n"
2119                "     aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}");
2120   verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n"
2121                "         I = FD->getDeclsInPrototypeScope().begin(),\n"
2122                "         E = FD->getDeclsInPrototypeScope().end();\n"
2123                "     I != E; ++I) {\n}");
2124   verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n"
2125                "         I = Container.begin(),\n"
2126                "         E = Container.end();\n"
2127                "     I != E; ++I) {\n}",
2128                getLLVMStyleWithColumns(76));
2129 
2130   verifyFormat(
2131       "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
2132       "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n"
2133       "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2134       "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
2135       "     ++aaaaaaaaaaa) {\n}");
2136   verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
2137                "                bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n"
2138                "     ++i) {\n}");
2139   verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n"
2140                "     aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
2141                "}");
2142   verifyFormat("for (some_namespace::SomeIterator iter( // force break\n"
2143                "         aaaaaaaaaa);\n"
2144                "     iter; ++iter) {\n"
2145                "}");
2146   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2147                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
2148                "     aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n"
2149                "     ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {");
2150 
2151   // These should not be formatted as Objective-C for-in loops.
2152   verifyFormat("for (Foo *x = 0; x != in; x++) {\n}");
2153   verifyFormat("Foo *x;\nfor (x = 0; x != in; x++) {\n}");
2154   verifyFormat("Foo *x;\nfor (x in y) {\n}");
2155   verifyFormat(
2156       "for (const Foo<Bar> &baz = in.value(); !baz.at_end(); ++baz) {\n}");
2157 
2158   FormatStyle NoBinPacking = getLLVMStyle();
2159   NoBinPacking.BinPackParameters = false;
2160   verifyFormat("for (int aaaaaaaaaaa = 1;\n"
2161                "     aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n"
2162                "                                           aaaaaaaaaaaaaaaa,\n"
2163                "                                           aaaaaaaaaaaaaaaa,\n"
2164                "                                           aaaaaaaaaaaaaaaa);\n"
2165                "     aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
2166                "}",
2167                NoBinPacking);
2168   verifyFormat(
2169       "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
2170       "                                          E = UnwrappedLines.end();\n"
2171       "     I != E;\n"
2172       "     ++I) {\n}",
2173       NoBinPacking);
2174 
2175   FormatStyle AlignLeft = getLLVMStyle();
2176   AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
2177   verifyFormat("for (A* a = start; a < end; ++a, ++value) {\n}", AlignLeft);
2178 }
2179 
2180 TEST_F(FormatTest, RangeBasedForLoops) {
2181   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
2182                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
2183   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n"
2184                "     aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}");
2185   verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n"
2186                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
2187   verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n"
2188                "     aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}");
2189 }
2190 
2191 TEST_F(FormatTest, ForEachLoops) {
2192   FormatStyle Style = getLLVMStyle();
2193   EXPECT_EQ(Style.AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never);
2194   EXPECT_EQ(Style.AllowShortLoopsOnASingleLine, false);
2195   verifyFormat("void f() {\n"
2196                "  for (;;) {\n"
2197                "  }\n"
2198                "  foreach (Item *item, itemlist) {\n"
2199                "  }\n"
2200                "  Q_FOREACH (Item *item, itemlist) {\n"
2201                "  }\n"
2202                "  BOOST_FOREACH (Item *item, itemlist) {\n"
2203                "  }\n"
2204                "  UNKNOWN_FOREACH(Item * item, itemlist) {}\n"
2205                "}",
2206                Style);
2207   verifyFormat("void f() {\n"
2208                "  for (;;)\n"
2209                "    int j = 1;\n"
2210                "  Q_FOREACH (int v, vec)\n"
2211                "    v *= 2;\n"
2212                "  for (;;) {\n"
2213                "    int j = 1;\n"
2214                "  }\n"
2215                "  Q_FOREACH (int v, vec) {\n"
2216                "    v *= 2;\n"
2217                "  }\n"
2218                "}",
2219                Style);
2220 
2221   FormatStyle ShortBlocks = getLLVMStyle();
2222   ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
2223   EXPECT_EQ(ShortBlocks.AllowShortLoopsOnASingleLine, false);
2224   verifyFormat("void f() {\n"
2225                "  for (;;)\n"
2226                "    int j = 1;\n"
2227                "  Q_FOREACH (int &v, vec)\n"
2228                "    v *= 2;\n"
2229                "  for (;;) {\n"
2230                "    int j = 1;\n"
2231                "  }\n"
2232                "  Q_FOREACH (int &v, vec) {\n"
2233                "    int j = 1;\n"
2234                "  }\n"
2235                "}",
2236                ShortBlocks);
2237 
2238   FormatStyle ShortLoops = getLLVMStyle();
2239   ShortLoops.AllowShortLoopsOnASingleLine = true;
2240   EXPECT_EQ(ShortLoops.AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never);
2241   verifyFormat("void f() {\n"
2242                "  for (;;) int j = 1;\n"
2243                "  Q_FOREACH (int &v, vec) int j = 1;\n"
2244                "  for (;;) {\n"
2245                "    int j = 1;\n"
2246                "  }\n"
2247                "  Q_FOREACH (int &v, vec) {\n"
2248                "    int j = 1;\n"
2249                "  }\n"
2250                "}",
2251                ShortLoops);
2252 
2253   FormatStyle ShortBlocksAndLoops = getLLVMStyle();
2254   ShortBlocksAndLoops.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
2255   ShortBlocksAndLoops.AllowShortLoopsOnASingleLine = true;
2256   verifyFormat("void f() {\n"
2257                "  for (;;) int j = 1;\n"
2258                "  Q_FOREACH (int &v, vec) int j = 1;\n"
2259                "  for (;;) { int j = 1; }\n"
2260                "  Q_FOREACH (int &v, vec) { int j = 1; }\n"
2261                "}",
2262                ShortBlocksAndLoops);
2263 
2264   Style.SpaceBeforeParens =
2265       FormatStyle::SBPO_ControlStatementsExceptControlMacros;
2266   verifyFormat("void f() {\n"
2267                "  for (;;) {\n"
2268                "  }\n"
2269                "  foreach(Item *item, itemlist) {\n"
2270                "  }\n"
2271                "  Q_FOREACH(Item *item, itemlist) {\n"
2272                "  }\n"
2273                "  BOOST_FOREACH(Item *item, itemlist) {\n"
2274                "  }\n"
2275                "  UNKNOWN_FOREACH(Item * item, itemlist) {}\n"
2276                "}",
2277                Style);
2278 
2279   // As function-like macros.
2280   verifyFormat("#define foreach(x, y)\n"
2281                "#define Q_FOREACH(x, y)\n"
2282                "#define BOOST_FOREACH(x, y)\n"
2283                "#define UNKNOWN_FOREACH(x, y)\n");
2284 
2285   // Not as function-like macros.
2286   verifyFormat("#define foreach (x, y)\n"
2287                "#define Q_FOREACH (x, y)\n"
2288                "#define BOOST_FOREACH (x, y)\n"
2289                "#define UNKNOWN_FOREACH (x, y)\n");
2290 
2291   // handle microsoft non standard extension
2292   verifyFormat("for each (char c in x->MyStringProperty)");
2293 }
2294 
2295 TEST_F(FormatTest, FormatsWhileLoop) {
2296   verifyFormat("while (true) {\n}");
2297   verifyFormat("while (true)\n"
2298                "  f();");
2299   verifyFormat("while () {\n}");
2300   verifyFormat("while () {\n"
2301                "  f();\n"
2302                "}");
2303 }
2304 
2305 TEST_F(FormatTest, FormatsDoWhile) {
2306   verifyFormat("do {\n"
2307                "  do_something();\n"
2308                "} while (something());");
2309   verifyFormat("do\n"
2310                "  do_something();\n"
2311                "while (something());");
2312 }
2313 
2314 TEST_F(FormatTest, FormatsSwitchStatement) {
2315   verifyFormat("switch (x) {\n"
2316                "case 1:\n"
2317                "  f();\n"
2318                "  break;\n"
2319                "case kFoo:\n"
2320                "case ns::kBar:\n"
2321                "case kBaz:\n"
2322                "  break;\n"
2323                "default:\n"
2324                "  g();\n"
2325                "  break;\n"
2326                "}");
2327   verifyFormat("switch (x) {\n"
2328                "case 1: {\n"
2329                "  f();\n"
2330                "  break;\n"
2331                "}\n"
2332                "case 2: {\n"
2333                "  break;\n"
2334                "}\n"
2335                "}");
2336   verifyFormat("switch (x) {\n"
2337                "case 1: {\n"
2338                "  f();\n"
2339                "  {\n"
2340                "    g();\n"
2341                "    h();\n"
2342                "  }\n"
2343                "  break;\n"
2344                "}\n"
2345                "}");
2346   verifyFormat("switch (x) {\n"
2347                "case 1: {\n"
2348                "  f();\n"
2349                "  if (foo) {\n"
2350                "    g();\n"
2351                "    h();\n"
2352                "  }\n"
2353                "  break;\n"
2354                "}\n"
2355                "}");
2356   verifyFormat("switch (x) {\n"
2357                "case 1: {\n"
2358                "  f();\n"
2359                "  g();\n"
2360                "} break;\n"
2361                "}");
2362   verifyFormat("switch (test)\n"
2363                "  ;");
2364   verifyFormat("switch (x) {\n"
2365                "default: {\n"
2366                "  // Do nothing.\n"
2367                "}\n"
2368                "}");
2369   verifyFormat("switch (x) {\n"
2370                "// comment\n"
2371                "// if 1, do f()\n"
2372                "case 1:\n"
2373                "  f();\n"
2374                "}");
2375   verifyFormat("switch (x) {\n"
2376                "case 1:\n"
2377                "  // Do amazing stuff\n"
2378                "  {\n"
2379                "    f();\n"
2380                "    g();\n"
2381                "  }\n"
2382                "  break;\n"
2383                "}");
2384   verifyFormat("#define A          \\\n"
2385                "  switch (x) {     \\\n"
2386                "  case a:          \\\n"
2387                "    foo = b;       \\\n"
2388                "  }",
2389                getLLVMStyleWithColumns(20));
2390   verifyFormat("#define OPERATION_CASE(name)           \\\n"
2391                "  case OP_name:                        \\\n"
2392                "    return operations::Operation##name\n",
2393                getLLVMStyleWithColumns(40));
2394   verifyFormat("switch (x) {\n"
2395                "case 1:;\n"
2396                "default:;\n"
2397                "  int i;\n"
2398                "}");
2399 
2400   verifyGoogleFormat("switch (x) {\n"
2401                      "  case 1:\n"
2402                      "    f();\n"
2403                      "    break;\n"
2404                      "  case kFoo:\n"
2405                      "  case ns::kBar:\n"
2406                      "  case kBaz:\n"
2407                      "    break;\n"
2408                      "  default:\n"
2409                      "    g();\n"
2410                      "    break;\n"
2411                      "}");
2412   verifyGoogleFormat("switch (x) {\n"
2413                      "  case 1: {\n"
2414                      "    f();\n"
2415                      "    break;\n"
2416                      "  }\n"
2417                      "}");
2418   verifyGoogleFormat("switch (test)\n"
2419                      "  ;");
2420 
2421   verifyGoogleFormat("#define OPERATION_CASE(name) \\\n"
2422                      "  case OP_name:              \\\n"
2423                      "    return operations::Operation##name\n");
2424   verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n"
2425                      "  // Get the correction operation class.\n"
2426                      "  switch (OpCode) {\n"
2427                      "    CASE(Add);\n"
2428                      "    CASE(Subtract);\n"
2429                      "    default:\n"
2430                      "      return operations::Unknown;\n"
2431                      "  }\n"
2432                      "#undef OPERATION_CASE\n"
2433                      "}");
2434   verifyFormat("DEBUG({\n"
2435                "  switch (x) {\n"
2436                "  case A:\n"
2437                "    f();\n"
2438                "    break;\n"
2439                "    // fallthrough\n"
2440                "  case B:\n"
2441                "    g();\n"
2442                "    break;\n"
2443                "  }\n"
2444                "});");
2445   EXPECT_EQ("DEBUG({\n"
2446             "  switch (x) {\n"
2447             "  case A:\n"
2448             "    f();\n"
2449             "    break;\n"
2450             "  // On B:\n"
2451             "  case B:\n"
2452             "    g();\n"
2453             "    break;\n"
2454             "  }\n"
2455             "});",
2456             format("DEBUG({\n"
2457                    "  switch (x) {\n"
2458                    "  case A:\n"
2459                    "    f();\n"
2460                    "    break;\n"
2461                    "  // On B:\n"
2462                    "  case B:\n"
2463                    "    g();\n"
2464                    "    break;\n"
2465                    "  }\n"
2466                    "});",
2467                    getLLVMStyle()));
2468   EXPECT_EQ("switch (n) {\n"
2469             "case 0: {\n"
2470             "  return false;\n"
2471             "}\n"
2472             "default: {\n"
2473             "  return true;\n"
2474             "}\n"
2475             "}",
2476             format("switch (n)\n"
2477                    "{\n"
2478                    "case 0: {\n"
2479                    "  return false;\n"
2480                    "}\n"
2481                    "default: {\n"
2482                    "  return true;\n"
2483                    "}\n"
2484                    "}",
2485                    getLLVMStyle()));
2486   verifyFormat("switch (a) {\n"
2487                "case (b):\n"
2488                "  return;\n"
2489                "}");
2490 
2491   verifyFormat("switch (a) {\n"
2492                "case some_namespace::\n"
2493                "    some_constant:\n"
2494                "  return;\n"
2495                "}",
2496                getLLVMStyleWithColumns(34));
2497 
2498   FormatStyle Style = getLLVMStyle();
2499   Style.IndentCaseLabels = true;
2500   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
2501   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
2502   Style.BraceWrapping.AfterCaseLabel = true;
2503   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
2504   EXPECT_EQ("switch (n)\n"
2505             "{\n"
2506             "  case 0:\n"
2507             "  {\n"
2508             "    return false;\n"
2509             "  }\n"
2510             "  default:\n"
2511             "  {\n"
2512             "    return true;\n"
2513             "  }\n"
2514             "}",
2515             format("switch (n) {\n"
2516                    "  case 0: {\n"
2517                    "    return false;\n"
2518                    "  }\n"
2519                    "  default: {\n"
2520                    "    return true;\n"
2521                    "  }\n"
2522                    "}",
2523                    Style));
2524   Style.BraceWrapping.AfterCaseLabel = false;
2525   EXPECT_EQ("switch (n)\n"
2526             "{\n"
2527             "  case 0: {\n"
2528             "    return false;\n"
2529             "  }\n"
2530             "  default: {\n"
2531             "    return true;\n"
2532             "  }\n"
2533             "}",
2534             format("switch (n) {\n"
2535                    "  case 0:\n"
2536                    "  {\n"
2537                    "    return false;\n"
2538                    "  }\n"
2539                    "  default:\n"
2540                    "  {\n"
2541                    "    return true;\n"
2542                    "  }\n"
2543                    "}",
2544                    Style));
2545   Style.IndentCaseLabels = false;
2546   Style.IndentCaseBlocks = true;
2547   EXPECT_EQ("switch (n)\n"
2548             "{\n"
2549             "case 0:\n"
2550             "  {\n"
2551             "    return false;\n"
2552             "  }\n"
2553             "case 1:\n"
2554             "  break;\n"
2555             "default:\n"
2556             "  {\n"
2557             "    return true;\n"
2558             "  }\n"
2559             "}",
2560             format("switch (n) {\n"
2561                    "case 0: {\n"
2562                    "  return false;\n"
2563                    "}\n"
2564                    "case 1:\n"
2565                    "  break;\n"
2566                    "default: {\n"
2567                    "  return true;\n"
2568                    "}\n"
2569                    "}",
2570                    Style));
2571   Style.IndentCaseLabels = true;
2572   Style.IndentCaseBlocks = true;
2573   EXPECT_EQ("switch (n)\n"
2574             "{\n"
2575             "  case 0:\n"
2576             "    {\n"
2577             "      return false;\n"
2578             "    }\n"
2579             "  case 1:\n"
2580             "    break;\n"
2581             "  default:\n"
2582             "    {\n"
2583             "      return true;\n"
2584             "    }\n"
2585             "}",
2586             format("switch (n) {\n"
2587                    "case 0: {\n"
2588                    "  return false;\n"
2589                    "}\n"
2590                    "case 1:\n"
2591                    "  break;\n"
2592                    "default: {\n"
2593                    "  return true;\n"
2594                    "}\n"
2595                    "}",
2596                    Style));
2597 }
2598 
2599 TEST_F(FormatTest, CaseRanges) {
2600   verifyFormat("switch (x) {\n"
2601                "case 'A' ... 'Z':\n"
2602                "case 1 ... 5:\n"
2603                "case a ... b:\n"
2604                "  break;\n"
2605                "}");
2606 }
2607 
2608 TEST_F(FormatTest, ShortEnums) {
2609   FormatStyle Style = getLLVMStyle();
2610   Style.AllowShortEnumsOnASingleLine = true;
2611   verifyFormat("enum { A, B, C } ShortEnum1, ShortEnum2;", Style);
2612   verifyFormat("typedef enum { A, B, C } ShortEnum1, ShortEnum2;", Style);
2613   Style.AllowShortEnumsOnASingleLine = false;
2614   verifyFormat("enum {\n"
2615                "  A,\n"
2616                "  B,\n"
2617                "  C\n"
2618                "} ShortEnum1, ShortEnum2;",
2619                Style);
2620   verifyFormat("typedef enum {\n"
2621                "  A,\n"
2622                "  B,\n"
2623                "  C\n"
2624                "} ShortEnum1, ShortEnum2;",
2625                Style);
2626   verifyFormat("enum {\n"
2627                "  A,\n"
2628                "} ShortEnum1, ShortEnum2;",
2629                Style);
2630   verifyFormat("typedef enum {\n"
2631                "  A,\n"
2632                "} ShortEnum1, ShortEnum2;",
2633                Style);
2634   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
2635   Style.BraceWrapping.AfterEnum = true;
2636   verifyFormat("enum\n"
2637                "{\n"
2638                "  A,\n"
2639                "  B,\n"
2640                "  C\n"
2641                "} ShortEnum1, ShortEnum2;",
2642                Style);
2643   verifyFormat("typedef enum\n"
2644                "{\n"
2645                "  A,\n"
2646                "  B,\n"
2647                "  C\n"
2648                "} ShortEnum1, ShortEnum2;",
2649                Style);
2650 }
2651 
2652 TEST_F(FormatTest, ShortCaseLabels) {
2653   FormatStyle Style = getLLVMStyle();
2654   Style.AllowShortCaseLabelsOnASingleLine = true;
2655   verifyFormat("switch (a) {\n"
2656                "case 1: x = 1; break;\n"
2657                "case 2: return;\n"
2658                "case 3:\n"
2659                "case 4:\n"
2660                "case 5: return;\n"
2661                "case 6: // comment\n"
2662                "  return;\n"
2663                "case 7:\n"
2664                "  // comment\n"
2665                "  return;\n"
2666                "case 8:\n"
2667                "  x = 8; // comment\n"
2668                "  break;\n"
2669                "default: y = 1; break;\n"
2670                "}",
2671                Style);
2672   verifyFormat("switch (a) {\n"
2673                "case 0: return; // comment\n"
2674                "case 1: break;  // comment\n"
2675                "case 2: return;\n"
2676                "// comment\n"
2677                "case 3: return;\n"
2678                "// comment 1\n"
2679                "// comment 2\n"
2680                "// comment 3\n"
2681                "case 4: break; /* comment */\n"
2682                "case 5:\n"
2683                "  // comment\n"
2684                "  break;\n"
2685                "case 6: /* comment */ x = 1; break;\n"
2686                "case 7: x = /* comment */ 1; break;\n"
2687                "case 8:\n"
2688                "  x = 1; /* comment */\n"
2689                "  break;\n"
2690                "case 9:\n"
2691                "  break; // comment line 1\n"
2692                "         // comment line 2\n"
2693                "}",
2694                Style);
2695   EXPECT_EQ("switch (a) {\n"
2696             "case 1:\n"
2697             "  x = 8;\n"
2698             "  // fall through\n"
2699             "case 2: x = 8;\n"
2700             "// comment\n"
2701             "case 3:\n"
2702             "  return; /* comment line 1\n"
2703             "           * comment line 2 */\n"
2704             "case 4: i = 8;\n"
2705             "// something else\n"
2706             "#if FOO\n"
2707             "case 5: break;\n"
2708             "#endif\n"
2709             "}",
2710             format("switch (a) {\n"
2711                    "case 1: x = 8;\n"
2712                    "  // fall through\n"
2713                    "case 2:\n"
2714                    "  x = 8;\n"
2715                    "// comment\n"
2716                    "case 3:\n"
2717                    "  return; /* comment line 1\n"
2718                    "           * comment line 2 */\n"
2719                    "case 4:\n"
2720                    "  i = 8;\n"
2721                    "// something else\n"
2722                    "#if FOO\n"
2723                    "case 5: break;\n"
2724                    "#endif\n"
2725                    "}",
2726                    Style));
2727   EXPECT_EQ("switch (a) {\n"
2728             "case 0:\n"
2729             "  return; // long long long long long long long long long long "
2730             "long long comment\n"
2731             "          // line\n"
2732             "}",
2733             format("switch (a) {\n"
2734                    "case 0: return; // long long long long long long long long "
2735                    "long long long long comment line\n"
2736                    "}",
2737                    Style));
2738   EXPECT_EQ("switch (a) {\n"
2739             "case 0:\n"
2740             "  return; /* long long long long long long long long long long "
2741             "long long comment\n"
2742             "             line */\n"
2743             "}",
2744             format("switch (a) {\n"
2745                    "case 0: return; /* long long long long long long long long "
2746                    "long long long long comment line */\n"
2747                    "}",
2748                    Style));
2749   verifyFormat("switch (a) {\n"
2750                "#if FOO\n"
2751                "case 0: return 0;\n"
2752                "#endif\n"
2753                "}",
2754                Style);
2755   verifyFormat("switch (a) {\n"
2756                "case 1: {\n"
2757                "}\n"
2758                "case 2: {\n"
2759                "  return;\n"
2760                "}\n"
2761                "case 3: {\n"
2762                "  x = 1;\n"
2763                "  return;\n"
2764                "}\n"
2765                "case 4:\n"
2766                "  if (x)\n"
2767                "    return;\n"
2768                "}",
2769                Style);
2770   Style.ColumnLimit = 21;
2771   verifyFormat("switch (a) {\n"
2772                "case 1: x = 1; break;\n"
2773                "case 2: return;\n"
2774                "case 3:\n"
2775                "case 4:\n"
2776                "case 5: return;\n"
2777                "default:\n"
2778                "  y = 1;\n"
2779                "  break;\n"
2780                "}",
2781                Style);
2782   Style.ColumnLimit = 80;
2783   Style.AllowShortCaseLabelsOnASingleLine = false;
2784   Style.IndentCaseLabels = true;
2785   EXPECT_EQ("switch (n) {\n"
2786             "  default /*comments*/:\n"
2787             "    return true;\n"
2788             "  case 0:\n"
2789             "    return false;\n"
2790             "}",
2791             format("switch (n) {\n"
2792                    "default/*comments*/:\n"
2793                    "  return true;\n"
2794                    "case 0:\n"
2795                    "  return false;\n"
2796                    "}",
2797                    Style));
2798   Style.AllowShortCaseLabelsOnASingleLine = true;
2799   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
2800   Style.BraceWrapping.AfterCaseLabel = true;
2801   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
2802   EXPECT_EQ("switch (n)\n"
2803             "{\n"
2804             "  case 0:\n"
2805             "  {\n"
2806             "    return false;\n"
2807             "  }\n"
2808             "  default:\n"
2809             "  {\n"
2810             "    return true;\n"
2811             "  }\n"
2812             "}",
2813             format("switch (n) {\n"
2814                    "  case 0: {\n"
2815                    "    return false;\n"
2816                    "  }\n"
2817                    "  default:\n"
2818                    "  {\n"
2819                    "    return true;\n"
2820                    "  }\n"
2821                    "}",
2822                    Style));
2823 }
2824 
2825 TEST_F(FormatTest, FormatsLabels) {
2826   verifyFormat("void f() {\n"
2827                "  some_code();\n"
2828                "test_label:\n"
2829                "  some_other_code();\n"
2830                "  {\n"
2831                "    some_more_code();\n"
2832                "  another_label:\n"
2833                "    some_more_code();\n"
2834                "  }\n"
2835                "}");
2836   verifyFormat("{\n"
2837                "  some_code();\n"
2838                "test_label:\n"
2839                "  some_other_code();\n"
2840                "}");
2841   verifyFormat("{\n"
2842                "  some_code();\n"
2843                "test_label:;\n"
2844                "  int i = 0;\n"
2845                "}");
2846   FormatStyle Style = getLLVMStyle();
2847   Style.IndentGotoLabels = false;
2848   verifyFormat("void f() {\n"
2849                "  some_code();\n"
2850                "test_label:\n"
2851                "  some_other_code();\n"
2852                "  {\n"
2853                "    some_more_code();\n"
2854                "another_label:\n"
2855                "    some_more_code();\n"
2856                "  }\n"
2857                "}",
2858                Style);
2859   verifyFormat("{\n"
2860                "  some_code();\n"
2861                "test_label:\n"
2862                "  some_other_code();\n"
2863                "}",
2864                Style);
2865   verifyFormat("{\n"
2866                "  some_code();\n"
2867                "test_label:;\n"
2868                "  int i = 0;\n"
2869                "}");
2870 }
2871 
2872 TEST_F(FormatTest, MultiLineControlStatements) {
2873   FormatStyle Style = getLLVMStyleWithColumns(20);
2874   Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom;
2875   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine;
2876   // Short lines should keep opening brace on same line.
2877   EXPECT_EQ("if (foo) {\n"
2878             "  bar();\n"
2879             "}",
2880             format("if(foo){bar();}", Style));
2881   EXPECT_EQ("if (foo) {\n"
2882             "  bar();\n"
2883             "} else {\n"
2884             "  baz();\n"
2885             "}",
2886             format("if(foo){bar();}else{baz();}", Style));
2887   EXPECT_EQ("if (foo && bar) {\n"
2888             "  baz();\n"
2889             "}",
2890             format("if(foo&&bar){baz();}", Style));
2891   EXPECT_EQ("if (foo) {\n"
2892             "  bar();\n"
2893             "} else if (baz) {\n"
2894             "  quux();\n"
2895             "}",
2896             format("if(foo){bar();}else if(baz){quux();}", Style));
2897   EXPECT_EQ(
2898       "if (foo) {\n"
2899       "  bar();\n"
2900       "} else if (baz) {\n"
2901       "  quux();\n"
2902       "} else {\n"
2903       "  foobar();\n"
2904       "}",
2905       format("if(foo){bar();}else if(baz){quux();}else{foobar();}", Style));
2906   EXPECT_EQ("for (;;) {\n"
2907             "  foo();\n"
2908             "}",
2909             format("for(;;){foo();}"));
2910   EXPECT_EQ("while (1) {\n"
2911             "  foo();\n"
2912             "}",
2913             format("while(1){foo();}", Style));
2914   EXPECT_EQ("switch (foo) {\n"
2915             "case bar:\n"
2916             "  return;\n"
2917             "}",
2918             format("switch(foo){case bar:return;}", Style));
2919   EXPECT_EQ("try {\n"
2920             "  foo();\n"
2921             "} catch (...) {\n"
2922             "  bar();\n"
2923             "}",
2924             format("try{foo();}catch(...){bar();}", Style));
2925   EXPECT_EQ("do {\n"
2926             "  foo();\n"
2927             "} while (bar &&\n"
2928             "         baz);",
2929             format("do{foo();}while(bar&&baz);", Style));
2930   // Long lines should put opening brace on new line.
2931   EXPECT_EQ("if (foo && bar &&\n"
2932             "    baz)\n"
2933             "{\n"
2934             "  quux();\n"
2935             "}",
2936             format("if(foo&&bar&&baz){quux();}", Style));
2937   EXPECT_EQ("if (foo && bar &&\n"
2938             "    baz)\n"
2939             "{\n"
2940             "  quux();\n"
2941             "}",
2942             format("if (foo && bar &&\n"
2943                    "    baz) {\n"
2944                    "  quux();\n"
2945                    "}",
2946                    Style));
2947   EXPECT_EQ("if (foo) {\n"
2948             "  bar();\n"
2949             "} else if (baz ||\n"
2950             "           quux)\n"
2951             "{\n"
2952             "  foobar();\n"
2953             "}",
2954             format("if(foo){bar();}else if(baz||quux){foobar();}", Style));
2955   EXPECT_EQ(
2956       "if (foo) {\n"
2957       "  bar();\n"
2958       "} else if (baz ||\n"
2959       "           quux)\n"
2960       "{\n"
2961       "  foobar();\n"
2962       "} else {\n"
2963       "  barbaz();\n"
2964       "}",
2965       format("if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}",
2966              Style));
2967   EXPECT_EQ("for (int i = 0;\n"
2968             "     i < 10; ++i)\n"
2969             "{\n"
2970             "  foo();\n"
2971             "}",
2972             format("for(int i=0;i<10;++i){foo();}", Style));
2973   EXPECT_EQ("foreach (int i,\n"
2974             "         list)\n"
2975             "{\n"
2976             "  foo();\n"
2977             "}",
2978             format("foreach(int i, list){foo();}", Style));
2979   Style.ColumnLimit =
2980       40; // to concentrate at brace wrapping, not line wrap due to column limit
2981   EXPECT_EQ("foreach (int i, list) {\n"
2982             "  foo();\n"
2983             "}",
2984             format("foreach(int i, list){foo();}", Style));
2985   Style.ColumnLimit =
2986       20; // to concentrate at brace wrapping, not line wrap due to column limit
2987   EXPECT_EQ("while (foo || bar ||\n"
2988             "       baz)\n"
2989             "{\n"
2990             "  quux();\n"
2991             "}",
2992             format("while(foo||bar||baz){quux();}", Style));
2993   EXPECT_EQ("switch (\n"
2994             "    foo = barbaz)\n"
2995             "{\n"
2996             "case quux:\n"
2997             "  return;\n"
2998             "}",
2999             format("switch(foo=barbaz){case quux:return;}", Style));
3000   EXPECT_EQ("try {\n"
3001             "  foo();\n"
3002             "} catch (\n"
3003             "    Exception &bar)\n"
3004             "{\n"
3005             "  baz();\n"
3006             "}",
3007             format("try{foo();}catch(Exception&bar){baz();}", Style));
3008   Style.ColumnLimit =
3009       40; // to concentrate at brace wrapping, not line wrap due to column limit
3010   EXPECT_EQ("try {\n"
3011             "  foo();\n"
3012             "} catch (Exception &bar) {\n"
3013             "  baz();\n"
3014             "}",
3015             format("try{foo();}catch(Exception&bar){baz();}", Style));
3016   Style.ColumnLimit =
3017       20; // to concentrate at brace wrapping, not line wrap due to column limit
3018 
3019   Style.BraceWrapping.BeforeElse = true;
3020   EXPECT_EQ(
3021       "if (foo) {\n"
3022       "  bar();\n"
3023       "}\n"
3024       "else if (baz ||\n"
3025       "         quux)\n"
3026       "{\n"
3027       "  foobar();\n"
3028       "}\n"
3029       "else {\n"
3030       "  barbaz();\n"
3031       "}",
3032       format("if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}",
3033              Style));
3034 
3035   Style.BraceWrapping.BeforeCatch = true;
3036   EXPECT_EQ("try {\n"
3037             "  foo();\n"
3038             "}\n"
3039             "catch (...) {\n"
3040             "  baz();\n"
3041             "}",
3042             format("try{foo();}catch(...){baz();}", Style));
3043 
3044   Style.BraceWrapping.AfterFunction = true;
3045   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine;
3046   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
3047   Style.ColumnLimit = 80;
3048   verifyFormat("void shortfunction() { bar(); }", Style);
3049 
3050   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
3051   verifyFormat("void shortfunction()\n"
3052                "{\n"
3053                "  bar();\n"
3054                "}",
3055                Style);
3056 }
3057 
3058 TEST_F(FormatTest, BeforeWhile) {
3059   FormatStyle Style = getLLVMStyle();
3060   Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom;
3061 
3062   verifyFormat("do {\n"
3063                "  foo();\n"
3064                "} while (1);",
3065                Style);
3066   Style.BraceWrapping.BeforeWhile = true;
3067   verifyFormat("do {\n"
3068                "  foo();\n"
3069                "}\n"
3070                "while (1);",
3071                Style);
3072 }
3073 
3074 //===----------------------------------------------------------------------===//
3075 // Tests for classes, namespaces, etc.
3076 //===----------------------------------------------------------------------===//
3077 
3078 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
3079   verifyFormat("class A {};");
3080 }
3081 
3082 TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
3083   verifyFormat("class A {\n"
3084                "public:\n"
3085                "public: // comment\n"
3086                "protected:\n"
3087                "private:\n"
3088                "  void f() {}\n"
3089                "};");
3090   verifyFormat("export class A {\n"
3091                "public:\n"
3092                "public: // comment\n"
3093                "protected:\n"
3094                "private:\n"
3095                "  void f() {}\n"
3096                "};");
3097   verifyGoogleFormat("class A {\n"
3098                      " public:\n"
3099                      " protected:\n"
3100                      " private:\n"
3101                      "  void f() {}\n"
3102                      "};");
3103   verifyGoogleFormat("export class A {\n"
3104                      " public:\n"
3105                      " protected:\n"
3106                      " private:\n"
3107                      "  void f() {}\n"
3108                      "};");
3109   verifyFormat("class A {\n"
3110                "public slots:\n"
3111                "  void f1() {}\n"
3112                "public Q_SLOTS:\n"
3113                "  void f2() {}\n"
3114                "protected slots:\n"
3115                "  void f3() {}\n"
3116                "protected Q_SLOTS:\n"
3117                "  void f4() {}\n"
3118                "private slots:\n"
3119                "  void f5() {}\n"
3120                "private Q_SLOTS:\n"
3121                "  void f6() {}\n"
3122                "signals:\n"
3123                "  void g1();\n"
3124                "Q_SIGNALS:\n"
3125                "  void g2();\n"
3126                "};");
3127 
3128   // Don't interpret 'signals' the wrong way.
3129   verifyFormat("signals.set();");
3130   verifyFormat("for (Signals signals : f()) {\n}");
3131   verifyFormat("{\n"
3132                "  signals.set(); // This needs indentation.\n"
3133                "}");
3134   verifyFormat("void f() {\n"
3135                "label:\n"
3136                "  signals.baz();\n"
3137                "}");
3138   verifyFormat("private[1];");
3139   verifyFormat("testArray[public] = 1;");
3140   verifyFormat("public();");
3141   verifyFormat("myFunc(public);");
3142   verifyFormat("std::vector<int> testVec = {private};");
3143   verifyFormat("private.p = 1;");
3144   verifyFormat("void function(private...){};");
3145   verifyFormat("if (private && public)\n");
3146   verifyFormat("private &= true;");
3147   verifyFormat("int x = private * public;");
3148   verifyFormat("public *= private;");
3149   verifyFormat("int x = public + private;");
3150   verifyFormat("private++;");
3151   verifyFormat("++private;");
3152   verifyFormat("public += private;");
3153   verifyFormat("public = public - private;");
3154   verifyFormat("public->foo();");
3155   verifyFormat("private--;");
3156   verifyFormat("--private;");
3157   verifyFormat("public -= 1;");
3158   verifyFormat("if (!private && !public)\n");
3159   verifyFormat("public != private;");
3160   verifyFormat("int x = public / private;");
3161   verifyFormat("public /= 2;");
3162   verifyFormat("public = public % 2;");
3163   verifyFormat("public %= 2;");
3164   verifyFormat("if (public < private)\n");
3165   verifyFormat("public << private;");
3166   verifyFormat("public <<= private;");
3167   verifyFormat("if (public > private)\n");
3168   verifyFormat("public >> private;");
3169   verifyFormat("public >>= private;");
3170   verifyFormat("public ^ private;");
3171   verifyFormat("public ^= private;");
3172   verifyFormat("public | private;");
3173   verifyFormat("public |= private;");
3174   verifyFormat("auto x = private ? 1 : 2;");
3175   verifyFormat("if (public == private)\n");
3176   verifyFormat("void foo(public, private)");
3177   verifyFormat("public::foo();");
3178 }
3179 
3180 TEST_F(FormatTest, SeparatesLogicalBlocks) {
3181   EXPECT_EQ("class A {\n"
3182             "public:\n"
3183             "  void f();\n"
3184             "\n"
3185             "private:\n"
3186             "  void g() {}\n"
3187             "  // test\n"
3188             "protected:\n"
3189             "  int h;\n"
3190             "};",
3191             format("class A {\n"
3192                    "public:\n"
3193                    "void f();\n"
3194                    "private:\n"
3195                    "void g() {}\n"
3196                    "// test\n"
3197                    "protected:\n"
3198                    "int h;\n"
3199                    "};"));
3200   EXPECT_EQ("class A {\n"
3201             "protected:\n"
3202             "public:\n"
3203             "  void f();\n"
3204             "};",
3205             format("class A {\n"
3206                    "protected:\n"
3207                    "\n"
3208                    "public:\n"
3209                    "\n"
3210                    "  void f();\n"
3211                    "};"));
3212 
3213   // Even ensure proper spacing inside macros.
3214   EXPECT_EQ("#define B     \\\n"
3215             "  class A {   \\\n"
3216             "   protected: \\\n"
3217             "   public:    \\\n"
3218             "    void f(); \\\n"
3219             "  };",
3220             format("#define B     \\\n"
3221                    "  class A {   \\\n"
3222                    "   protected: \\\n"
3223                    "              \\\n"
3224                    "   public:    \\\n"
3225                    "              \\\n"
3226                    "    void f(); \\\n"
3227                    "  };",
3228                    getGoogleStyle()));
3229   // But don't remove empty lines after macros ending in access specifiers.
3230   EXPECT_EQ("#define A private:\n"
3231             "\n"
3232             "int i;",
3233             format("#define A         private:\n"
3234                    "\n"
3235                    "int              i;"));
3236 }
3237 
3238 TEST_F(FormatTest, FormatsClasses) {
3239   verifyFormat("class A : public B {};");
3240   verifyFormat("class A : public ::B {};");
3241 
3242   verifyFormat(
3243       "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3244       "                             public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
3245   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
3246                "    : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3247                "      public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
3248   verifyFormat(
3249       "class A : public B, public C, public D, public E, public F {};");
3250   verifyFormat("class AAAAAAAAAAAA : public B,\n"
3251                "                     public C,\n"
3252                "                     public D,\n"
3253                "                     public E,\n"
3254                "                     public F,\n"
3255                "                     public G {};");
3256 
3257   verifyFormat("class\n"
3258                "    ReallyReallyLongClassName {\n"
3259                "  int i;\n"
3260                "};",
3261                getLLVMStyleWithColumns(32));
3262   verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
3263                "                           aaaaaaaaaaaaaaaa> {};");
3264   verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n"
3265                "    : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n"
3266                "                                 aaaaaaaaaaaaaaaaaaaaaa> {};");
3267   verifyFormat("template <class R, class C>\n"
3268                "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n"
3269                "    : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};");
3270   verifyFormat("class ::A::B {};");
3271 }
3272 
3273 TEST_F(FormatTest, BreakInheritanceStyle) {
3274   FormatStyle StyleWithInheritanceBreakBeforeComma = getLLVMStyle();
3275   StyleWithInheritanceBreakBeforeComma.BreakInheritanceList =
3276       FormatStyle::BILS_BeforeComma;
3277   verifyFormat("class MyClass : public X {};",
3278                StyleWithInheritanceBreakBeforeComma);
3279   verifyFormat("class MyClass\n"
3280                "    : public X\n"
3281                "    , public Y {};",
3282                StyleWithInheritanceBreakBeforeComma);
3283   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA\n"
3284                "    : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n"
3285                "    , public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};",
3286                StyleWithInheritanceBreakBeforeComma);
3287   verifyFormat("struct aaaaaaaaaaaaa\n"
3288                "    : public aaaaaaaaaaaaaaaaaaa< // break\n"
3289                "          aaaaaaaaaaaaaaaa> {};",
3290                StyleWithInheritanceBreakBeforeComma);
3291 
3292   FormatStyle StyleWithInheritanceBreakAfterColon = getLLVMStyle();
3293   StyleWithInheritanceBreakAfterColon.BreakInheritanceList =
3294       FormatStyle::BILS_AfterColon;
3295   verifyFormat("class MyClass : public X {};",
3296                StyleWithInheritanceBreakAfterColon);
3297   verifyFormat("class MyClass : public X, public Y {};",
3298                StyleWithInheritanceBreakAfterColon);
3299   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA :\n"
3300                "    public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3301                "    public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};",
3302                StyleWithInheritanceBreakAfterColon);
3303   verifyFormat("struct aaaaaaaaaaaaa :\n"
3304                "    public aaaaaaaaaaaaaaaaaaa< // break\n"
3305                "        aaaaaaaaaaaaaaaa> {};",
3306                StyleWithInheritanceBreakAfterColon);
3307 
3308   FormatStyle StyleWithInheritanceBreakAfterComma = getLLVMStyle();
3309   StyleWithInheritanceBreakAfterComma.BreakInheritanceList =
3310       FormatStyle::BILS_AfterComma;
3311   verifyFormat("class MyClass : public X {};",
3312                StyleWithInheritanceBreakAfterComma);
3313   verifyFormat("class MyClass : public X,\n"
3314                "                public Y {};",
3315                StyleWithInheritanceBreakAfterComma);
3316   verifyFormat(
3317       "class AAAAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3318       "                               public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC "
3319       "{};",
3320       StyleWithInheritanceBreakAfterComma);
3321   verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
3322                "                           aaaaaaaaaaaaaaaa> {};",
3323                StyleWithInheritanceBreakAfterComma);
3324   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
3325                "    : public OnceBreak,\n"
3326                "      public AlwaysBreak,\n"
3327                "      EvenBasesFitInOneLine {};",
3328                StyleWithInheritanceBreakAfterComma);
3329 }
3330 
3331 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
3332   verifyFormat("class A {\n} a, b;");
3333   verifyFormat("struct A {\n} a, b;");
3334   verifyFormat("union A {\n} a;");
3335 }
3336 
3337 TEST_F(FormatTest, FormatsEnum) {
3338   verifyFormat("enum {\n"
3339                "  Zero,\n"
3340                "  One = 1,\n"
3341                "  Two = One + 1,\n"
3342                "  Three = (One + Two),\n"
3343                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
3344                "  Five = (One, Two, Three, Four, 5)\n"
3345                "};");
3346   verifyGoogleFormat("enum {\n"
3347                      "  Zero,\n"
3348                      "  One = 1,\n"
3349                      "  Two = One + 1,\n"
3350                      "  Three = (One + Two),\n"
3351                      "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
3352                      "  Five = (One, Two, Three, Four, 5)\n"
3353                      "};");
3354   verifyFormat("enum Enum {};");
3355   verifyFormat("enum {};");
3356   verifyFormat("enum X E {} d;");
3357   verifyFormat("enum __attribute__((...)) E {} d;");
3358   verifyFormat("enum __declspec__((...)) E {} d;");
3359   verifyFormat("enum {\n"
3360                "  Bar = Foo<int, int>::value\n"
3361                "};",
3362                getLLVMStyleWithColumns(30));
3363 
3364   verifyFormat("enum ShortEnum { A, B, C };");
3365   verifyGoogleFormat("enum ShortEnum { A, B, C };");
3366 
3367   EXPECT_EQ("enum KeepEmptyLines {\n"
3368             "  ONE,\n"
3369             "\n"
3370             "  TWO,\n"
3371             "\n"
3372             "  THREE\n"
3373             "}",
3374             format("enum KeepEmptyLines {\n"
3375                    "  ONE,\n"
3376                    "\n"
3377                    "  TWO,\n"
3378                    "\n"
3379                    "\n"
3380                    "  THREE\n"
3381                    "}"));
3382   verifyFormat("enum E { // comment\n"
3383                "  ONE,\n"
3384                "  TWO\n"
3385                "};\n"
3386                "int i;");
3387 
3388   FormatStyle EightIndent = getLLVMStyle();
3389   EightIndent.IndentWidth = 8;
3390   verifyFormat("enum {\n"
3391                "        VOID,\n"
3392                "        CHAR,\n"
3393                "        SHORT,\n"
3394                "        INT,\n"
3395                "        LONG,\n"
3396                "        SIGNED,\n"
3397                "        UNSIGNED,\n"
3398                "        BOOL,\n"
3399                "        FLOAT,\n"
3400                "        DOUBLE,\n"
3401                "        COMPLEX\n"
3402                "};",
3403                EightIndent);
3404 
3405   // Not enums.
3406   verifyFormat("enum X f() {\n"
3407                "  a();\n"
3408                "  return 42;\n"
3409                "}");
3410   verifyFormat("enum X Type::f() {\n"
3411                "  a();\n"
3412                "  return 42;\n"
3413                "}");
3414   verifyFormat("enum ::X f() {\n"
3415                "  a();\n"
3416                "  return 42;\n"
3417                "}");
3418   verifyFormat("enum ns::X f() {\n"
3419                "  a();\n"
3420                "  return 42;\n"
3421                "}");
3422 }
3423 
3424 TEST_F(FormatTest, FormatsEnumsWithErrors) {
3425   verifyFormat("enum Type {\n"
3426                "  One = 0; // These semicolons should be commas.\n"
3427                "  Two = 1;\n"
3428                "};");
3429   verifyFormat("namespace n {\n"
3430                "enum Type {\n"
3431                "  One,\n"
3432                "  Two, // missing };\n"
3433                "  int i;\n"
3434                "}\n"
3435                "void g() {}");
3436 }
3437 
3438 TEST_F(FormatTest, FormatsEnumStruct) {
3439   verifyFormat("enum struct {\n"
3440                "  Zero,\n"
3441                "  One = 1,\n"
3442                "  Two = One + 1,\n"
3443                "  Three = (One + Two),\n"
3444                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
3445                "  Five = (One, Two, Three, Four, 5)\n"
3446                "};");
3447   verifyFormat("enum struct Enum {};");
3448   verifyFormat("enum struct {};");
3449   verifyFormat("enum struct X E {} d;");
3450   verifyFormat("enum struct __attribute__((...)) E {} d;");
3451   verifyFormat("enum struct __declspec__((...)) E {} d;");
3452   verifyFormat("enum struct X f() {\n  a();\n  return 42;\n}");
3453 }
3454 
3455 TEST_F(FormatTest, FormatsEnumClass) {
3456   verifyFormat("enum class {\n"
3457                "  Zero,\n"
3458                "  One = 1,\n"
3459                "  Two = One + 1,\n"
3460                "  Three = (One + Two),\n"
3461                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
3462                "  Five = (One, Two, Three, Four, 5)\n"
3463                "};");
3464   verifyFormat("enum class Enum {};");
3465   verifyFormat("enum class {};");
3466   verifyFormat("enum class X E {} d;");
3467   verifyFormat("enum class __attribute__((...)) E {} d;");
3468   verifyFormat("enum class __declspec__((...)) E {} d;");
3469   verifyFormat("enum class X f() {\n  a();\n  return 42;\n}");
3470 }
3471 
3472 TEST_F(FormatTest, FormatsEnumTypes) {
3473   verifyFormat("enum X : int {\n"
3474                "  A, // Force multiple lines.\n"
3475                "  B\n"
3476                "};");
3477   verifyFormat("enum X : int { A, B };");
3478   verifyFormat("enum X : std::uint32_t { A, B };");
3479 }
3480 
3481 TEST_F(FormatTest, FormatsTypedefEnum) {
3482   FormatStyle Style = getLLVMStyleWithColumns(40);
3483   verifyFormat("typedef enum {} EmptyEnum;");
3484   verifyFormat("typedef enum { A, B, C } ShortEnum;");
3485   verifyFormat("typedef enum {\n"
3486                "  ZERO = 0,\n"
3487                "  ONE = 1,\n"
3488                "  TWO = 2,\n"
3489                "  THREE = 3\n"
3490                "} LongEnum;",
3491                Style);
3492   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
3493   Style.BraceWrapping.AfterEnum = true;
3494   verifyFormat("typedef enum {} EmptyEnum;");
3495   verifyFormat("typedef enum { A, B, C } ShortEnum;");
3496   verifyFormat("typedef enum\n"
3497                "{\n"
3498                "  ZERO = 0,\n"
3499                "  ONE = 1,\n"
3500                "  TWO = 2,\n"
3501                "  THREE = 3\n"
3502                "} LongEnum;",
3503                Style);
3504 }
3505 
3506 TEST_F(FormatTest, FormatsNSEnums) {
3507   verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }");
3508   verifyGoogleFormat(
3509       "typedef NS_CLOSED_ENUM(NSInteger, SomeName) { AAA, BBB }");
3510   verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n"
3511                      "  // Information about someDecentlyLongValue.\n"
3512                      "  someDecentlyLongValue,\n"
3513                      "  // Information about anotherDecentlyLongValue.\n"
3514                      "  anotherDecentlyLongValue,\n"
3515                      "  // Information about aThirdDecentlyLongValue.\n"
3516                      "  aThirdDecentlyLongValue\n"
3517                      "};");
3518   verifyGoogleFormat("typedef NS_CLOSED_ENUM(NSInteger, MyType) {\n"
3519                      "  // Information about someDecentlyLongValue.\n"
3520                      "  someDecentlyLongValue,\n"
3521                      "  // Information about anotherDecentlyLongValue.\n"
3522                      "  anotherDecentlyLongValue,\n"
3523                      "  // Information about aThirdDecentlyLongValue.\n"
3524                      "  aThirdDecentlyLongValue\n"
3525                      "};");
3526   verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n"
3527                      "  a = 1,\n"
3528                      "  b = 2,\n"
3529                      "  c = 3,\n"
3530                      "};");
3531   verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n"
3532                      "  a = 1,\n"
3533                      "  b = 2,\n"
3534                      "  c = 3,\n"
3535                      "};");
3536   verifyGoogleFormat("typedef CF_CLOSED_ENUM(NSInteger, MyType) {\n"
3537                      "  a = 1,\n"
3538                      "  b = 2,\n"
3539                      "  c = 3,\n"
3540                      "};");
3541   verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n"
3542                      "  a = 1,\n"
3543                      "  b = 2,\n"
3544                      "  c = 3,\n"
3545                      "};");
3546 }
3547 
3548 TEST_F(FormatTest, FormatsBitfields) {
3549   verifyFormat("struct Bitfields {\n"
3550                "  unsigned sClass : 8;\n"
3551                "  unsigned ValueKind : 2;\n"
3552                "};");
3553   verifyFormat("struct A {\n"
3554                "  int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n"
3555                "      bbbbbbbbbbbbbbbbbbbbbbbbb;\n"
3556                "};");
3557   verifyFormat("struct MyStruct {\n"
3558                "  uchar data;\n"
3559                "  uchar : 8;\n"
3560                "  uchar : 8;\n"
3561                "  uchar other;\n"
3562                "};");
3563   FormatStyle Style = getLLVMStyle();
3564   Style.BitFieldColonSpacing = FormatStyle::BFCS_None;
3565   verifyFormat("struct Bitfields {\n"
3566                "  unsigned sClass:8;\n"
3567                "  unsigned ValueKind:2;\n"
3568                "  uchar other;\n"
3569                "};",
3570                Style);
3571   verifyFormat("struct A {\n"
3572                "  int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:1,\n"
3573                "      bbbbbbbbbbbbbbbbbbbbbbbbb:2;\n"
3574                "};",
3575                Style);
3576   Style.BitFieldColonSpacing = FormatStyle::BFCS_Before;
3577   verifyFormat("struct Bitfields {\n"
3578                "  unsigned sClass :8;\n"
3579                "  unsigned ValueKind :2;\n"
3580                "  uchar other;\n"
3581                "};",
3582                Style);
3583   Style.BitFieldColonSpacing = FormatStyle::BFCS_After;
3584   verifyFormat("struct Bitfields {\n"
3585                "  unsigned sClass: 8;\n"
3586                "  unsigned ValueKind: 2;\n"
3587                "  uchar other;\n"
3588                "};",
3589                Style);
3590 }
3591 
3592 TEST_F(FormatTest, FormatsNamespaces) {
3593   FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle();
3594   LLVMWithNoNamespaceFix.FixNamespaceComments = false;
3595 
3596   verifyFormat("namespace some_namespace {\n"
3597                "class A {};\n"
3598                "void f() { f(); }\n"
3599                "}",
3600                LLVMWithNoNamespaceFix);
3601   verifyFormat("namespace N::inline D {\n"
3602                "class A {};\n"
3603                "void f() { f(); }\n"
3604                "}",
3605                LLVMWithNoNamespaceFix);
3606   verifyFormat("namespace N::inline D::E {\n"
3607                "class A {};\n"
3608                "void f() { f(); }\n"
3609                "}",
3610                LLVMWithNoNamespaceFix);
3611   verifyFormat("namespace [[deprecated(\"foo[bar\")]] some_namespace {\n"
3612                "class A {};\n"
3613                "void f() { f(); }\n"
3614                "}",
3615                LLVMWithNoNamespaceFix);
3616   verifyFormat("/* something */ namespace some_namespace {\n"
3617                "class A {};\n"
3618                "void f() { f(); }\n"
3619                "}",
3620                LLVMWithNoNamespaceFix);
3621   verifyFormat("namespace {\n"
3622                "class A {};\n"
3623                "void f() { f(); }\n"
3624                "}",
3625                LLVMWithNoNamespaceFix);
3626   verifyFormat("/* something */ namespace {\n"
3627                "class A {};\n"
3628                "void f() { f(); }\n"
3629                "}",
3630                LLVMWithNoNamespaceFix);
3631   verifyFormat("inline namespace X {\n"
3632                "class A {};\n"
3633                "void f() { f(); }\n"
3634                "}",
3635                LLVMWithNoNamespaceFix);
3636   verifyFormat("/* something */ inline namespace X {\n"
3637                "class A {};\n"
3638                "void f() { f(); }\n"
3639                "}",
3640                LLVMWithNoNamespaceFix);
3641   verifyFormat("export namespace X {\n"
3642                "class A {};\n"
3643                "void f() { f(); }\n"
3644                "}",
3645                LLVMWithNoNamespaceFix);
3646   verifyFormat("using namespace some_namespace;\n"
3647                "class A {};\n"
3648                "void f() { f(); }",
3649                LLVMWithNoNamespaceFix);
3650 
3651   // This code is more common than we thought; if we
3652   // layout this correctly the semicolon will go into
3653   // its own line, which is undesirable.
3654   verifyFormat("namespace {};", LLVMWithNoNamespaceFix);
3655   verifyFormat("namespace {\n"
3656                "class A {};\n"
3657                "};",
3658                LLVMWithNoNamespaceFix);
3659 
3660   verifyFormat("namespace {\n"
3661                "int SomeVariable = 0; // comment\n"
3662                "} // namespace",
3663                LLVMWithNoNamespaceFix);
3664   EXPECT_EQ("#ifndef HEADER_GUARD\n"
3665             "#define HEADER_GUARD\n"
3666             "namespace my_namespace {\n"
3667             "int i;\n"
3668             "} // my_namespace\n"
3669             "#endif // HEADER_GUARD",
3670             format("#ifndef HEADER_GUARD\n"
3671                    " #define HEADER_GUARD\n"
3672                    "   namespace my_namespace {\n"
3673                    "int i;\n"
3674                    "}    // my_namespace\n"
3675                    "#endif    // HEADER_GUARD",
3676                    LLVMWithNoNamespaceFix));
3677 
3678   EXPECT_EQ("namespace A::B {\n"
3679             "class C {};\n"
3680             "}",
3681             format("namespace A::B {\n"
3682                    "class C {};\n"
3683                    "}",
3684                    LLVMWithNoNamespaceFix));
3685 
3686   FormatStyle Style = getLLVMStyle();
3687   Style.NamespaceIndentation = FormatStyle::NI_All;
3688   EXPECT_EQ("namespace out {\n"
3689             "  int i;\n"
3690             "  namespace in {\n"
3691             "    int i;\n"
3692             "  } // namespace in\n"
3693             "} // namespace out",
3694             format("namespace out {\n"
3695                    "int i;\n"
3696                    "namespace in {\n"
3697                    "int i;\n"
3698                    "} // namespace in\n"
3699                    "} // namespace out",
3700                    Style));
3701 
3702   FormatStyle ShortInlineFunctions = getLLVMStyle();
3703   ShortInlineFunctions.NamespaceIndentation = FormatStyle::NI_All;
3704   ShortInlineFunctions.AllowShortFunctionsOnASingleLine =
3705       FormatStyle::SFS_Inline;
3706   verifyFormat("namespace {\n"
3707                "  void f() {\n"
3708                "    return;\n"
3709                "  }\n"
3710                "} // namespace\n",
3711                ShortInlineFunctions);
3712   verifyFormat("namespace {\n"
3713                "  int some_int;\n"
3714                "  void f() {\n"
3715                "    return;\n"
3716                "  }\n"
3717                "} // namespace\n",
3718                ShortInlineFunctions);
3719   verifyFormat("namespace interface {\n"
3720                "  void f() {\n"
3721                "    return;\n"
3722                "  }\n"
3723                "} // namespace interface\n",
3724                ShortInlineFunctions);
3725   verifyFormat("namespace {\n"
3726                "  class X {\n"
3727                "    void f() { return; }\n"
3728                "  };\n"
3729                "} // namespace\n",
3730                ShortInlineFunctions);
3731   verifyFormat("namespace {\n"
3732                "  struct X {\n"
3733                "    void f() { return; }\n"
3734                "  };\n"
3735                "} // namespace\n",
3736                ShortInlineFunctions);
3737   verifyFormat("namespace {\n"
3738                "  union X {\n"
3739                "    void f() { return; }\n"
3740                "  };\n"
3741                "} // namespace\n",
3742                ShortInlineFunctions);
3743   verifyFormat("extern \"C\" {\n"
3744                "void f() {\n"
3745                "  return;\n"
3746                "}\n"
3747                "} // namespace\n",
3748                ShortInlineFunctions);
3749   verifyFormat("namespace {\n"
3750                "  class X {\n"
3751                "    void f() { return; }\n"
3752                "  } x;\n"
3753                "} // namespace\n",
3754                ShortInlineFunctions);
3755   verifyFormat("namespace {\n"
3756                "  [[nodiscard]] class X {\n"
3757                "    void f() { return; }\n"
3758                "  };\n"
3759                "} // namespace\n",
3760                ShortInlineFunctions);
3761   verifyFormat("namespace {\n"
3762                "  static class X {\n"
3763                "    void f() { return; }\n"
3764                "  } x;\n"
3765                "} // namespace\n",
3766                ShortInlineFunctions);
3767   verifyFormat("namespace {\n"
3768                "  constexpr class X {\n"
3769                "    void f() { return; }\n"
3770                "  } x;\n"
3771                "} // namespace\n",
3772                ShortInlineFunctions);
3773 
3774   ShortInlineFunctions.IndentExternBlock = FormatStyle::IEBS_Indent;
3775   verifyFormat("extern \"C\" {\n"
3776                "  void f() {\n"
3777                "    return;\n"
3778                "  }\n"
3779                "} // namespace\n",
3780                ShortInlineFunctions);
3781 
3782   Style.NamespaceIndentation = FormatStyle::NI_Inner;
3783   EXPECT_EQ("namespace out {\n"
3784             "int i;\n"
3785             "namespace in {\n"
3786             "  int i;\n"
3787             "} // namespace in\n"
3788             "} // namespace out",
3789             format("namespace out {\n"
3790                    "int i;\n"
3791                    "namespace in {\n"
3792                    "int i;\n"
3793                    "} // namespace in\n"
3794                    "} // namespace out",
3795                    Style));
3796 
3797   Style.NamespaceIndentation = FormatStyle::NI_None;
3798   verifyFormat("template <class T>\n"
3799                "concept a_concept = X<>;\n"
3800                "namespace B {\n"
3801                "struct b_struct {};\n"
3802                "} // namespace B\n",
3803                Style);
3804   verifyFormat("template <int I> constexpr void foo requires(I == 42) {}\n"
3805                "namespace ns {\n"
3806                "void foo() {}\n"
3807                "} // namespace ns\n",
3808                Style);
3809 }
3810 
3811 TEST_F(FormatTest, NamespaceMacros) {
3812   FormatStyle Style = getLLVMStyle();
3813   Style.NamespaceMacros.push_back("TESTSUITE");
3814 
3815   verifyFormat("TESTSUITE(A) {\n"
3816                "int foo();\n"
3817                "} // TESTSUITE(A)",
3818                Style);
3819 
3820   verifyFormat("TESTSUITE(A, B) {\n"
3821                "int foo();\n"
3822                "} // TESTSUITE(A)",
3823                Style);
3824 
3825   // Properly indent according to NamespaceIndentation style
3826   Style.NamespaceIndentation = FormatStyle::NI_All;
3827   verifyFormat("TESTSUITE(A) {\n"
3828                "  int foo();\n"
3829                "} // TESTSUITE(A)",
3830                Style);
3831   verifyFormat("TESTSUITE(A) {\n"
3832                "  namespace B {\n"
3833                "    int foo();\n"
3834                "  } // namespace B\n"
3835                "} // TESTSUITE(A)",
3836                Style);
3837   verifyFormat("namespace A {\n"
3838                "  TESTSUITE(B) {\n"
3839                "    int foo();\n"
3840                "  } // TESTSUITE(B)\n"
3841                "} // namespace A",
3842                Style);
3843 
3844   Style.NamespaceIndentation = FormatStyle::NI_Inner;
3845   verifyFormat("TESTSUITE(A) {\n"
3846                "TESTSUITE(B) {\n"
3847                "  int foo();\n"
3848                "} // TESTSUITE(B)\n"
3849                "} // TESTSUITE(A)",
3850                Style);
3851   verifyFormat("TESTSUITE(A) {\n"
3852                "namespace B {\n"
3853                "  int foo();\n"
3854                "} // namespace B\n"
3855                "} // TESTSUITE(A)",
3856                Style);
3857   verifyFormat("namespace A {\n"
3858                "TESTSUITE(B) {\n"
3859                "  int foo();\n"
3860                "} // TESTSUITE(B)\n"
3861                "} // namespace A",
3862                Style);
3863 
3864   // Properly merge namespace-macros blocks in CompactNamespaces mode
3865   Style.NamespaceIndentation = FormatStyle::NI_None;
3866   Style.CompactNamespaces = true;
3867   verifyFormat("TESTSUITE(A) { TESTSUITE(B) {\n"
3868                "}} // TESTSUITE(A::B)",
3869                Style);
3870 
3871   EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n"
3872             "}} // TESTSUITE(out::in)",
3873             format("TESTSUITE(out) {\n"
3874                    "TESTSUITE(in) {\n"
3875                    "} // TESTSUITE(in)\n"
3876                    "} // TESTSUITE(out)",
3877                    Style));
3878 
3879   EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n"
3880             "}} // TESTSUITE(out::in)",
3881             format("TESTSUITE(out) {\n"
3882                    "TESTSUITE(in) {\n"
3883                    "} // TESTSUITE(in)\n"
3884                    "} // TESTSUITE(out)",
3885                    Style));
3886 
3887   // Do not merge different namespaces/macros
3888   EXPECT_EQ("namespace out {\n"
3889             "TESTSUITE(in) {\n"
3890             "} // TESTSUITE(in)\n"
3891             "} // namespace out",
3892             format("namespace out {\n"
3893                    "TESTSUITE(in) {\n"
3894                    "} // TESTSUITE(in)\n"
3895                    "} // namespace out",
3896                    Style));
3897   EXPECT_EQ("TESTSUITE(out) {\n"
3898             "namespace in {\n"
3899             "} // namespace in\n"
3900             "} // TESTSUITE(out)",
3901             format("TESTSUITE(out) {\n"
3902                    "namespace in {\n"
3903                    "} // namespace in\n"
3904                    "} // TESTSUITE(out)",
3905                    Style));
3906   Style.NamespaceMacros.push_back("FOOBAR");
3907   EXPECT_EQ("TESTSUITE(out) {\n"
3908             "FOOBAR(in) {\n"
3909             "} // FOOBAR(in)\n"
3910             "} // TESTSUITE(out)",
3911             format("TESTSUITE(out) {\n"
3912                    "FOOBAR(in) {\n"
3913                    "} // FOOBAR(in)\n"
3914                    "} // TESTSUITE(out)",
3915                    Style));
3916 }
3917 
3918 TEST_F(FormatTest, FormatsCompactNamespaces) {
3919   FormatStyle Style = getLLVMStyle();
3920   Style.CompactNamespaces = true;
3921   Style.NamespaceMacros.push_back("TESTSUITE");
3922 
3923   verifyFormat("namespace A { namespace B {\n"
3924                "}} // namespace A::B",
3925                Style);
3926 
3927   EXPECT_EQ("namespace out { namespace in {\n"
3928             "}} // namespace out::in",
3929             format("namespace out {\n"
3930                    "namespace in {\n"
3931                    "} // namespace in\n"
3932                    "} // namespace out",
3933                    Style));
3934 
3935   // Only namespaces which have both consecutive opening and end get compacted
3936   EXPECT_EQ("namespace out {\n"
3937             "namespace in1 {\n"
3938             "} // namespace in1\n"
3939             "namespace in2 {\n"
3940             "} // namespace in2\n"
3941             "} // namespace out",
3942             format("namespace out {\n"
3943                    "namespace in1 {\n"
3944                    "} // namespace in1\n"
3945                    "namespace in2 {\n"
3946                    "} // namespace in2\n"
3947                    "} // namespace out",
3948                    Style));
3949 
3950   EXPECT_EQ("namespace out {\n"
3951             "int i;\n"
3952             "namespace in {\n"
3953             "int j;\n"
3954             "} // namespace in\n"
3955             "int k;\n"
3956             "} // namespace out",
3957             format("namespace out { int i;\n"
3958                    "namespace in { int j; } // namespace in\n"
3959                    "int k; } // namespace out",
3960                    Style));
3961 
3962   EXPECT_EQ("namespace A { namespace B { namespace C {\n"
3963             "}}} // namespace A::B::C\n",
3964             format("namespace A { namespace B {\n"
3965                    "namespace C {\n"
3966                    "}} // namespace B::C\n"
3967                    "} // namespace A\n",
3968                    Style));
3969 
3970   Style.ColumnLimit = 40;
3971   EXPECT_EQ("namespace aaaaaaaaaa {\n"
3972             "namespace bbbbbbbbbb {\n"
3973             "}} // namespace aaaaaaaaaa::bbbbbbbbbb",
3974             format("namespace aaaaaaaaaa {\n"
3975                    "namespace bbbbbbbbbb {\n"
3976                    "} // namespace bbbbbbbbbb\n"
3977                    "} // namespace aaaaaaaaaa",
3978                    Style));
3979 
3980   EXPECT_EQ("namespace aaaaaa { namespace bbbbbb {\n"
3981             "namespace cccccc {\n"
3982             "}}} // namespace aaaaaa::bbbbbb::cccccc",
3983             format("namespace aaaaaa {\n"
3984                    "namespace bbbbbb {\n"
3985                    "namespace cccccc {\n"
3986                    "} // namespace cccccc\n"
3987                    "} // namespace bbbbbb\n"
3988                    "} // namespace aaaaaa",
3989                    Style));
3990   Style.ColumnLimit = 80;
3991 
3992   // Extra semicolon after 'inner' closing brace prevents merging
3993   EXPECT_EQ("namespace out { namespace in {\n"
3994             "}; } // namespace out::in",
3995             format("namespace out {\n"
3996                    "namespace in {\n"
3997                    "}; // namespace in\n"
3998                    "} // namespace out",
3999                    Style));
4000 
4001   // Extra semicolon after 'outer' closing brace is conserved
4002   EXPECT_EQ("namespace out { namespace in {\n"
4003             "}}; // namespace out::in",
4004             format("namespace out {\n"
4005                    "namespace in {\n"
4006                    "} // namespace in\n"
4007                    "}; // namespace out",
4008                    Style));
4009 
4010   Style.NamespaceIndentation = FormatStyle::NI_All;
4011   EXPECT_EQ("namespace out { namespace in {\n"
4012             "  int i;\n"
4013             "}} // namespace out::in",
4014             format("namespace out {\n"
4015                    "namespace in {\n"
4016                    "int i;\n"
4017                    "} // namespace in\n"
4018                    "} // namespace out",
4019                    Style));
4020   EXPECT_EQ("namespace out { namespace mid {\n"
4021             "  namespace in {\n"
4022             "    int j;\n"
4023             "  } // namespace in\n"
4024             "  int k;\n"
4025             "}} // namespace out::mid",
4026             format("namespace out { namespace mid {\n"
4027                    "namespace in { int j; } // namespace in\n"
4028                    "int k; }} // namespace out::mid",
4029                    Style));
4030 
4031   Style.NamespaceIndentation = FormatStyle::NI_Inner;
4032   EXPECT_EQ("namespace out { namespace in {\n"
4033             "  int i;\n"
4034             "}} // namespace out::in",
4035             format("namespace out {\n"
4036                    "namespace in {\n"
4037                    "int i;\n"
4038                    "} // namespace in\n"
4039                    "} // namespace out",
4040                    Style));
4041   EXPECT_EQ("namespace out { namespace mid { namespace in {\n"
4042             "  int i;\n"
4043             "}}} // namespace out::mid::in",
4044             format("namespace out {\n"
4045                    "namespace mid {\n"
4046                    "namespace in {\n"
4047                    "int i;\n"
4048                    "} // namespace in\n"
4049                    "} // namespace mid\n"
4050                    "} // namespace out",
4051                    Style));
4052 
4053   Style.CompactNamespaces = true;
4054   Style.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
4055   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4056   Style.BraceWrapping.BeforeLambdaBody = true;
4057   verifyFormat("namespace out { namespace in {\n"
4058                "}} // namespace out::in",
4059                Style);
4060   EXPECT_EQ("namespace out { namespace in {\n"
4061             "}} // namespace out::in",
4062             format("namespace out {\n"
4063                    "namespace in {\n"
4064                    "} // namespace in\n"
4065                    "} // namespace out",
4066                    Style));
4067 }
4068 
4069 TEST_F(FormatTest, FormatsExternC) {
4070   verifyFormat("extern \"C\" {\nint a;");
4071   verifyFormat("extern \"C\" {}");
4072   verifyFormat("extern \"C\" {\n"
4073                "int foo();\n"
4074                "}");
4075   verifyFormat("extern \"C\" int foo() {}");
4076   verifyFormat("extern \"C\" int foo();");
4077   verifyFormat("extern \"C\" int foo() {\n"
4078                "  int i = 42;\n"
4079                "  return i;\n"
4080                "}");
4081 
4082   FormatStyle Style = getLLVMStyle();
4083   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4084   Style.BraceWrapping.AfterFunction = true;
4085   verifyFormat("extern \"C\" int foo() {}", Style);
4086   verifyFormat("extern \"C\" int foo();", Style);
4087   verifyFormat("extern \"C\" int foo()\n"
4088                "{\n"
4089                "  int i = 42;\n"
4090                "  return i;\n"
4091                "}",
4092                Style);
4093 
4094   Style.BraceWrapping.AfterExternBlock = true;
4095   Style.BraceWrapping.SplitEmptyRecord = false;
4096   verifyFormat("extern \"C\"\n"
4097                "{}",
4098                Style);
4099   verifyFormat("extern \"C\"\n"
4100                "{\n"
4101                "  int foo();\n"
4102                "}",
4103                Style);
4104 }
4105 
4106 TEST_F(FormatTest, IndentExternBlockStyle) {
4107   FormatStyle Style = getLLVMStyle();
4108   Style.IndentWidth = 2;
4109 
4110   Style.IndentExternBlock = FormatStyle::IEBS_Indent;
4111   verifyFormat("extern \"C\" { /*9*/\n"
4112                "}",
4113                Style);
4114   verifyFormat("extern \"C\" {\n"
4115                "  int foo10();\n"
4116                "}",
4117                Style);
4118 
4119   Style.IndentExternBlock = FormatStyle::IEBS_NoIndent;
4120   verifyFormat("extern \"C\" { /*11*/\n"
4121                "}",
4122                Style);
4123   verifyFormat("extern \"C\" {\n"
4124                "int foo12();\n"
4125                "}",
4126                Style);
4127 
4128   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4129   Style.BraceWrapping.AfterExternBlock = true;
4130   Style.IndentExternBlock = FormatStyle::IEBS_Indent;
4131   verifyFormat("extern \"C\"\n"
4132                "{ /*13*/\n"
4133                "}",
4134                Style);
4135   verifyFormat("extern \"C\"\n{\n"
4136                "  int foo14();\n"
4137                "}",
4138                Style);
4139 
4140   Style.BraceWrapping.AfterExternBlock = false;
4141   Style.IndentExternBlock = FormatStyle::IEBS_NoIndent;
4142   verifyFormat("extern \"C\" { /*15*/\n"
4143                "}",
4144                Style);
4145   verifyFormat("extern \"C\" {\n"
4146                "int foo16();\n"
4147                "}",
4148                Style);
4149 
4150   Style.BraceWrapping.AfterExternBlock = true;
4151   verifyFormat("extern \"C\"\n"
4152                "{ /*13*/\n"
4153                "}",
4154                Style);
4155   verifyFormat("extern \"C\"\n"
4156                "{\n"
4157                "int foo14();\n"
4158                "}",
4159                Style);
4160 
4161   Style.IndentExternBlock = FormatStyle::IEBS_Indent;
4162   verifyFormat("extern \"C\"\n"
4163                "{ /*13*/\n"
4164                "}",
4165                Style);
4166   verifyFormat("extern \"C\"\n"
4167                "{\n"
4168                "  int foo14();\n"
4169                "}",
4170                Style);
4171 }
4172 
4173 TEST_F(FormatTest, FormatsInlineASM) {
4174   verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
4175   verifyFormat("asm(\"nop\" ::: \"memory\");");
4176   verifyFormat(
4177       "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
4178       "    \"cpuid\\n\\t\"\n"
4179       "    \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
4180       "    : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
4181       "    : \"a\"(value));");
4182   EXPECT_EQ(
4183       "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
4184       "  __asm {\n"
4185       "        mov     edx,[that] // vtable in edx\n"
4186       "        mov     eax,methodIndex\n"
4187       "        call    [edx][eax*4] // stdcall\n"
4188       "  }\n"
4189       "}",
4190       format("void NS_InvokeByIndex(void *that,   unsigned int methodIndex) {\n"
4191              "    __asm {\n"
4192              "        mov     edx,[that] // vtable in edx\n"
4193              "        mov     eax,methodIndex\n"
4194              "        call    [edx][eax*4] // stdcall\n"
4195              "    }\n"
4196              "}"));
4197   EXPECT_EQ("_asm {\n"
4198             "  xor eax, eax;\n"
4199             "  cpuid;\n"
4200             "}",
4201             format("_asm {\n"
4202                    "  xor eax, eax;\n"
4203                    "  cpuid;\n"
4204                    "}"));
4205   verifyFormat("void function() {\n"
4206                "  // comment\n"
4207                "  asm(\"\");\n"
4208                "}");
4209   EXPECT_EQ("__asm {\n"
4210             "}\n"
4211             "int i;",
4212             format("__asm   {\n"
4213                    "}\n"
4214                    "int   i;"));
4215 }
4216 
4217 TEST_F(FormatTest, FormatTryCatch) {
4218   verifyFormat("try {\n"
4219                "  throw a * b;\n"
4220                "} catch (int a) {\n"
4221                "  // Do nothing.\n"
4222                "} catch (...) {\n"
4223                "  exit(42);\n"
4224                "}");
4225 
4226   // Function-level try statements.
4227   verifyFormat("int f() try { return 4; } catch (...) {\n"
4228                "  return 5;\n"
4229                "}");
4230   verifyFormat("class A {\n"
4231                "  int a;\n"
4232                "  A() try : a(0) {\n"
4233                "  } catch (...) {\n"
4234                "    throw;\n"
4235                "  }\n"
4236                "};\n");
4237   verifyFormat("class A {\n"
4238                "  int a;\n"
4239                "  A() try : a(0), b{1} {\n"
4240                "  } catch (...) {\n"
4241                "    throw;\n"
4242                "  }\n"
4243                "};\n");
4244   verifyFormat("class A {\n"
4245                "  int a;\n"
4246                "  A() try : a(0), b{1}, c{2} {\n"
4247                "  } catch (...) {\n"
4248                "    throw;\n"
4249                "  }\n"
4250                "};\n");
4251   verifyFormat("class A {\n"
4252                "  int a;\n"
4253                "  A() try : a(0), b{1}, c{2} {\n"
4254                "    { // New scope.\n"
4255                "    }\n"
4256                "  } catch (...) {\n"
4257                "    throw;\n"
4258                "  }\n"
4259                "};\n");
4260 
4261   // Incomplete try-catch blocks.
4262   verifyIncompleteFormat("try {} catch (");
4263 }
4264 
4265 TEST_F(FormatTest, FormatTryAsAVariable) {
4266   verifyFormat("int try;");
4267   verifyFormat("int try, size;");
4268   verifyFormat("try = foo();");
4269   verifyFormat("if (try < size) {\n  return true;\n}");
4270 
4271   verifyFormat("int catch;");
4272   verifyFormat("int catch, size;");
4273   verifyFormat("catch = foo();");
4274   verifyFormat("if (catch < size) {\n  return true;\n}");
4275 
4276   FormatStyle Style = getLLVMStyle();
4277   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4278   Style.BraceWrapping.AfterFunction = true;
4279   Style.BraceWrapping.BeforeCatch = true;
4280   verifyFormat("try {\n"
4281                "  int bar = 1;\n"
4282                "}\n"
4283                "catch (...) {\n"
4284                "  int bar = 1;\n"
4285                "}",
4286                Style);
4287   verifyFormat("#if NO_EX\n"
4288                "try\n"
4289                "#endif\n"
4290                "{\n"
4291                "}\n"
4292                "#if NO_EX\n"
4293                "catch (...) {\n"
4294                "}",
4295                Style);
4296   verifyFormat("try /* abc */ {\n"
4297                "  int bar = 1;\n"
4298                "}\n"
4299                "catch (...) {\n"
4300                "  int bar = 1;\n"
4301                "}",
4302                Style);
4303   verifyFormat("try\n"
4304                "// abc\n"
4305                "{\n"
4306                "  int bar = 1;\n"
4307                "}\n"
4308                "catch (...) {\n"
4309                "  int bar = 1;\n"
4310                "}",
4311                Style);
4312 }
4313 
4314 TEST_F(FormatTest, FormatSEHTryCatch) {
4315   verifyFormat("__try {\n"
4316                "  int a = b * c;\n"
4317                "} __except (EXCEPTION_EXECUTE_HANDLER) {\n"
4318                "  // Do nothing.\n"
4319                "}");
4320 
4321   verifyFormat("__try {\n"
4322                "  int a = b * c;\n"
4323                "} __finally {\n"
4324                "  // Do nothing.\n"
4325                "}");
4326 
4327   verifyFormat("DEBUG({\n"
4328                "  __try {\n"
4329                "  } __finally {\n"
4330                "  }\n"
4331                "});\n");
4332 }
4333 
4334 TEST_F(FormatTest, IncompleteTryCatchBlocks) {
4335   verifyFormat("try {\n"
4336                "  f();\n"
4337                "} catch {\n"
4338                "  g();\n"
4339                "}");
4340   verifyFormat("try {\n"
4341                "  f();\n"
4342                "} catch (A a) MACRO(x) {\n"
4343                "  g();\n"
4344                "} catch (B b) MACRO(x) {\n"
4345                "  g();\n"
4346                "}");
4347 }
4348 
4349 TEST_F(FormatTest, FormatTryCatchBraceStyles) {
4350   FormatStyle Style = getLLVMStyle();
4351   for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla,
4352                           FormatStyle::BS_WebKit}) {
4353     Style.BreakBeforeBraces = BraceStyle;
4354     verifyFormat("try {\n"
4355                  "  // something\n"
4356                  "} catch (...) {\n"
4357                  "  // something\n"
4358                  "}",
4359                  Style);
4360   }
4361   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
4362   verifyFormat("try {\n"
4363                "  // something\n"
4364                "}\n"
4365                "catch (...) {\n"
4366                "  // something\n"
4367                "}",
4368                Style);
4369   verifyFormat("__try {\n"
4370                "  // something\n"
4371                "}\n"
4372                "__finally {\n"
4373                "  // something\n"
4374                "}",
4375                Style);
4376   verifyFormat("@try {\n"
4377                "  // something\n"
4378                "}\n"
4379                "@finally {\n"
4380                "  // something\n"
4381                "}",
4382                Style);
4383   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
4384   verifyFormat("try\n"
4385                "{\n"
4386                "  // something\n"
4387                "}\n"
4388                "catch (...)\n"
4389                "{\n"
4390                "  // something\n"
4391                "}",
4392                Style);
4393   Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
4394   verifyFormat("try\n"
4395                "  {\n"
4396                "  // something white\n"
4397                "  }\n"
4398                "catch (...)\n"
4399                "  {\n"
4400                "  // something white\n"
4401                "  }",
4402                Style);
4403   Style.BreakBeforeBraces = FormatStyle::BS_GNU;
4404   verifyFormat("try\n"
4405                "  {\n"
4406                "    // something\n"
4407                "  }\n"
4408                "catch (...)\n"
4409                "  {\n"
4410                "    // something\n"
4411                "  }",
4412                Style);
4413   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4414   Style.BraceWrapping.BeforeCatch = true;
4415   verifyFormat("try {\n"
4416                "  // something\n"
4417                "}\n"
4418                "catch (...) {\n"
4419                "  // something\n"
4420                "}",
4421                Style);
4422 }
4423 
4424 TEST_F(FormatTest, StaticInitializers) {
4425   verifyFormat("static SomeClass SC = {1, 'a'};");
4426 
4427   verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n"
4428                "    100000000, "
4429                "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};");
4430 
4431   // Here, everything other than the "}" would fit on a line.
4432   verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
4433                "    10000000000000000000000000};");
4434   EXPECT_EQ("S s = {a,\n"
4435             "\n"
4436             "       b};",
4437             format("S s = {\n"
4438                    "  a,\n"
4439                    "\n"
4440                    "  b\n"
4441                    "};"));
4442 
4443   // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
4444   // line. However, the formatting looks a bit off and this probably doesn't
4445   // happen often in practice.
4446   verifyFormat("static int Variable[1] = {\n"
4447                "    {1000000000000000000000000000000000000}};",
4448                getLLVMStyleWithColumns(40));
4449 }
4450 
4451 TEST_F(FormatTest, DesignatedInitializers) {
4452   verifyFormat("const struct A a = {.a = 1, .b = 2};");
4453   verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n"
4454                "                    .bbbbbbbbbb = 2,\n"
4455                "                    .cccccccccc = 3,\n"
4456                "                    .dddddddddd = 4,\n"
4457                "                    .eeeeeeeeee = 5};");
4458   verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
4459                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
4460                "    .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
4461                "    .ccccccccccccccccccccccccccc = 3,\n"
4462                "    .ddddddddddddddddddddddddddd = 4,\n"
4463                "    .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};");
4464 
4465   verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
4466 
4467   verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
4468   verifyFormat("const struct A a = {[1] = aaaaaaaaaa,\n"
4469                "                    [2] = bbbbbbbbbb,\n"
4470                "                    [3] = cccccccccc,\n"
4471                "                    [4] = dddddddddd,\n"
4472                "                    [5] = eeeeeeeeee};");
4473   verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
4474                "    [1] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4475                "    [2] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
4476                "    [3] = cccccccccccccccccccccccccccccccccccccc,\n"
4477                "    [4] = dddddddddddddddddddddddddddddddddddddd,\n"
4478                "    [5] = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee};");
4479 }
4480 
4481 TEST_F(FormatTest, NestedStaticInitializers) {
4482   verifyFormat("static A x = {{{}}};\n");
4483   verifyFormat("static A x = {{{init1, init2, init3, init4},\n"
4484                "               {init1, init2, init3, init4}}};",
4485                getLLVMStyleWithColumns(50));
4486 
4487   verifyFormat("somes Status::global_reps[3] = {\n"
4488                "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
4489                "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
4490                "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};",
4491                getLLVMStyleWithColumns(60));
4492   verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
4493                      "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
4494                      "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
4495                      "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
4496   verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n"
4497                "                  {rect.fRight - rect.fLeft, rect.fBottom - "
4498                "rect.fTop}};");
4499 
4500   verifyFormat(
4501       "SomeArrayOfSomeType a = {\n"
4502       "    {{1, 2, 3},\n"
4503       "     {1, 2, 3},\n"
4504       "     {111111111111111111111111111111, 222222222222222222222222222222,\n"
4505       "      333333333333333333333333333333},\n"
4506       "     {1, 2, 3},\n"
4507       "     {1, 2, 3}}};");
4508   verifyFormat(
4509       "SomeArrayOfSomeType a = {\n"
4510       "    {{1, 2, 3}},\n"
4511       "    {{1, 2, 3}},\n"
4512       "    {{111111111111111111111111111111, 222222222222222222222222222222,\n"
4513       "      333333333333333333333333333333}},\n"
4514       "    {{1, 2, 3}},\n"
4515       "    {{1, 2, 3}}};");
4516 
4517   verifyFormat("struct {\n"
4518                "  unsigned bit;\n"
4519                "  const char *const name;\n"
4520                "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n"
4521                "                 {kOsWin, \"Windows\"},\n"
4522                "                 {kOsLinux, \"Linux\"},\n"
4523                "                 {kOsCrOS, \"Chrome OS\"}};");
4524   verifyFormat("struct {\n"
4525                "  unsigned bit;\n"
4526                "  const char *const name;\n"
4527                "} kBitsToOs[] = {\n"
4528                "    {kOsMac, \"Mac\"},\n"
4529                "    {kOsWin, \"Windows\"},\n"
4530                "    {kOsLinux, \"Linux\"},\n"
4531                "    {kOsCrOS, \"Chrome OS\"},\n"
4532                "};");
4533 }
4534 
4535 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
4536   verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
4537                "                      \\\n"
4538                "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
4539 }
4540 
4541 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
4542   verifyFormat("virtual void write(ELFWriter *writerrr,\n"
4543                "                   OwningPtr<FileOutputBuffer> &buffer) = 0;");
4544 
4545   // Do break defaulted and deleted functions.
4546   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
4547                "    default;",
4548                getLLVMStyleWithColumns(40));
4549   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
4550                "    delete;",
4551                getLLVMStyleWithColumns(40));
4552 }
4553 
4554 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) {
4555   verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3",
4556                getLLVMStyleWithColumns(40));
4557   verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
4558                getLLVMStyleWithColumns(40));
4559   EXPECT_EQ("#define Q                              \\\n"
4560             "  \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\"    \\\n"
4561             "  \"aaaaaaaa.cpp\"",
4562             format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
4563                    getLLVMStyleWithColumns(40)));
4564 }
4565 
4566 TEST_F(FormatTest, UnderstandsLinePPDirective) {
4567   EXPECT_EQ("# 123 \"A string literal\"",
4568             format("   #     123    \"A string literal\""));
4569 }
4570 
4571 TEST_F(FormatTest, LayoutUnknownPPDirective) {
4572   EXPECT_EQ("#;", format("#;"));
4573   verifyFormat("#\n;\n;\n;");
4574 }
4575 
4576 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
4577   EXPECT_EQ("#line 42 \"test\"\n",
4578             format("#  \\\n  line  \\\n  42  \\\n  \"test\"\n"));
4579   EXPECT_EQ("#define A B\n", format("#  \\\n define  \\\n    A  \\\n       B\n",
4580                                     getLLVMStyleWithColumns(12)));
4581 }
4582 
4583 TEST_F(FormatTest, EndOfFileEndsPPDirective) {
4584   EXPECT_EQ("#line 42 \"test\"",
4585             format("#  \\\n  line  \\\n  42  \\\n  \"test\""));
4586   EXPECT_EQ("#define A B", format("#  \\\n define  \\\n    A  \\\n       B"));
4587 }
4588 
4589 TEST_F(FormatTest, DoesntRemoveUnknownTokens) {
4590   verifyFormat("#define A \\x20");
4591   verifyFormat("#define A \\ x20");
4592   EXPECT_EQ("#define A \\ x20", format("#define A \\   x20"));
4593   verifyFormat("#define A ''");
4594   verifyFormat("#define A ''qqq");
4595   verifyFormat("#define A `qqq");
4596   verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");");
4597   EXPECT_EQ("const char *c = STRINGIFY(\n"
4598             "\\na : b);",
4599             format("const char * c = STRINGIFY(\n"
4600                    "\\na : b);"));
4601 
4602   verifyFormat("a\r\\");
4603   verifyFormat("a\v\\");
4604   verifyFormat("a\f\\");
4605 }
4606 
4607 TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) {
4608   FormatStyle style = getChromiumStyle(FormatStyle::LK_Cpp);
4609   style.IndentWidth = 4;
4610   style.PPIndentWidth = 1;
4611 
4612   style.IndentPPDirectives = FormatStyle::PPDIS_None;
4613   verifyFormat("#ifdef __linux__\n"
4614                "void foo() {\n"
4615                "    int x = 0;\n"
4616                "}\n"
4617                "#define FOO\n"
4618                "#endif\n"
4619                "void bar() {\n"
4620                "    int y = 0;\n"
4621                "}\n",
4622                style);
4623 
4624   style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
4625   verifyFormat("#ifdef __linux__\n"
4626                "void foo() {\n"
4627                "    int x = 0;\n"
4628                "}\n"
4629                "# define FOO foo\n"
4630                "#endif\n"
4631                "void bar() {\n"
4632                "    int y = 0;\n"
4633                "}\n",
4634                style);
4635 
4636   style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash;
4637   verifyFormat("#ifdef __linux__\n"
4638                "void foo() {\n"
4639                "    int x = 0;\n"
4640                "}\n"
4641                " #define FOO foo\n"
4642                "#endif\n"
4643                "void bar() {\n"
4644                "    int y = 0;\n"
4645                "}\n",
4646                style);
4647 }
4648 
4649 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
4650   verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
4651   verifyFormat("#define A( \\\n    BB)", getLLVMStyleWithColumns(12));
4652   verifyFormat("#define A( \\\n    A, B)", getLLVMStyleWithColumns(12));
4653   // FIXME: We never break before the macro name.
4654   verifyFormat("#define AA( \\\n    B)", getLLVMStyleWithColumns(12));
4655 
4656   verifyFormat("#define A A\n#define A A");
4657   verifyFormat("#define A(X) A\n#define A A");
4658 
4659   verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
4660   verifyFormat("#define Something    \\\n  Other", getLLVMStyleWithColumns(22));
4661 }
4662 
4663 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
4664   EXPECT_EQ("// somecomment\n"
4665             "#include \"a.h\"\n"
4666             "#define A(  \\\n"
4667             "    A, B)\n"
4668             "#include \"b.h\"\n"
4669             "// somecomment\n",
4670             format("  // somecomment\n"
4671                    "  #include \"a.h\"\n"
4672                    "#define A(A,\\\n"
4673                    "    B)\n"
4674                    "    #include \"b.h\"\n"
4675                    " // somecomment\n",
4676                    getLLVMStyleWithColumns(13)));
4677 }
4678 
4679 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); }
4680 
4681 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
4682   EXPECT_EQ("#define A    \\\n"
4683             "  c;         \\\n"
4684             "  e;\n"
4685             "f;",
4686             format("#define A c; e;\n"
4687                    "f;",
4688                    getLLVMStyleWithColumns(14)));
4689 }
4690 
4691 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
4692 
4693 TEST_F(FormatTest, MacroDefinitionInsideStatement) {
4694   EXPECT_EQ("int x,\n"
4695             "#define A\n"
4696             "    y;",
4697             format("int x,\n#define A\ny;"));
4698 }
4699 
4700 TEST_F(FormatTest, HashInMacroDefinition) {
4701   EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle()));
4702   EXPECT_EQ("#define A(c) u#c", format("#define A(c) u#c", getLLVMStyle()));
4703   EXPECT_EQ("#define A(c) U#c", format("#define A(c) U#c", getLLVMStyle()));
4704   EXPECT_EQ("#define A(c) u8#c", format("#define A(c) u8#c", getLLVMStyle()));
4705   EXPECT_EQ("#define A(c) LR#c", format("#define A(c) LR#c", getLLVMStyle()));
4706   EXPECT_EQ("#define A(c) uR#c", format("#define A(c) uR#c", getLLVMStyle()));
4707   EXPECT_EQ("#define A(c) UR#c", format("#define A(c) UR#c", getLLVMStyle()));
4708   EXPECT_EQ("#define A(c) u8R#c", format("#define A(c) u8R#c", getLLVMStyle()));
4709   verifyFormat("#define A \\\n  b #c;", getLLVMStyleWithColumns(11));
4710   verifyFormat("#define A  \\\n"
4711                "  {        \\\n"
4712                "    f(#c); \\\n"
4713                "  }",
4714                getLLVMStyleWithColumns(11));
4715 
4716   verifyFormat("#define A(X)         \\\n"
4717                "  void function##X()",
4718                getLLVMStyleWithColumns(22));
4719 
4720   verifyFormat("#define A(a, b, c)   \\\n"
4721                "  void a##b##c()",
4722                getLLVMStyleWithColumns(22));
4723 
4724   verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
4725 }
4726 
4727 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
4728   EXPECT_EQ("#define A (x)", format("#define A (x)"));
4729   EXPECT_EQ("#define A(x)", format("#define A(x)"));
4730 
4731   FormatStyle Style = getLLVMStyle();
4732   Style.SpaceBeforeParens = FormatStyle::SBPO_Never;
4733   verifyFormat("#define true ((foo)1)", Style);
4734   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
4735   verifyFormat("#define false((foo)0)", Style);
4736 }
4737 
4738 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
4739   EXPECT_EQ("#define A b;", format("#define A \\\n"
4740                                    "          \\\n"
4741                                    "  b;",
4742                                    getLLVMStyleWithColumns(25)));
4743   EXPECT_EQ("#define A \\\n"
4744             "          \\\n"
4745             "  a;      \\\n"
4746             "  b;",
4747             format("#define A \\\n"
4748                    "          \\\n"
4749                    "  a;      \\\n"
4750                    "  b;",
4751                    getLLVMStyleWithColumns(11)));
4752   EXPECT_EQ("#define A \\\n"
4753             "  a;      \\\n"
4754             "          \\\n"
4755             "  b;",
4756             format("#define A \\\n"
4757                    "  a;      \\\n"
4758                    "          \\\n"
4759                    "  b;",
4760                    getLLVMStyleWithColumns(11)));
4761 }
4762 
4763 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
4764   verifyIncompleteFormat("#define A :");
4765   verifyFormat("#define SOMECASES  \\\n"
4766                "  case 1:          \\\n"
4767                "  case 2\n",
4768                getLLVMStyleWithColumns(20));
4769   verifyFormat("#define MACRO(a) \\\n"
4770                "  if (a)         \\\n"
4771                "    f();         \\\n"
4772                "  else           \\\n"
4773                "    g()",
4774                getLLVMStyleWithColumns(18));
4775   verifyFormat("#define A template <typename T>");
4776   verifyIncompleteFormat("#define STR(x) #x\n"
4777                          "f(STR(this_is_a_string_literal{));");
4778   verifyFormat("#pragma omp threadprivate( \\\n"
4779                "    y)), // expected-warning",
4780                getLLVMStyleWithColumns(28));
4781   verifyFormat("#d, = };");
4782   verifyFormat("#if \"a");
4783   verifyIncompleteFormat("({\n"
4784                          "#define b     \\\n"
4785                          "  }           \\\n"
4786                          "  a\n"
4787                          "a",
4788                          getLLVMStyleWithColumns(15));
4789   verifyFormat("#define A     \\\n"
4790                "  {           \\\n"
4791                "    {\n"
4792                "#define B     \\\n"
4793                "  }           \\\n"
4794                "  }",
4795                getLLVMStyleWithColumns(15));
4796   verifyNoCrash("#if a\na(\n#else\n#endif\n{a");
4797   verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}");
4798   verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};");
4799   verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() {      \n)}");
4800 }
4801 
4802 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
4803   verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
4804   EXPECT_EQ("class A : public QObject {\n"
4805             "  Q_OBJECT\n"
4806             "\n"
4807             "  A() {}\n"
4808             "};",
4809             format("class A  :  public QObject {\n"
4810                    "     Q_OBJECT\n"
4811                    "\n"
4812                    "  A() {\n}\n"
4813                    "}  ;"));
4814   EXPECT_EQ("MACRO\n"
4815             "/*static*/ int i;",
4816             format("MACRO\n"
4817                    " /*static*/ int   i;"));
4818   EXPECT_EQ("SOME_MACRO\n"
4819             "namespace {\n"
4820             "void f();\n"
4821             "} // namespace",
4822             format("SOME_MACRO\n"
4823                    "  namespace    {\n"
4824                    "void   f(  );\n"
4825                    "} // namespace"));
4826   // Only if the identifier contains at least 5 characters.
4827   EXPECT_EQ("HTTP f();", format("HTTP\nf();"));
4828   EXPECT_EQ("MACRO\nf();", format("MACRO\nf();"));
4829   // Only if everything is upper case.
4830   EXPECT_EQ("class A : public QObject {\n"
4831             "  Q_Object A() {}\n"
4832             "};",
4833             format("class A  :  public QObject {\n"
4834                    "     Q_Object\n"
4835                    "  A() {\n}\n"
4836                    "}  ;"));
4837 
4838   // Only if the next line can actually start an unwrapped line.
4839   EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;",
4840             format("SOME_WEIRD_LOG_MACRO\n"
4841                    "<< SomeThing;"));
4842 
4843   verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
4844                "(n, buffers))\n",
4845                getChromiumStyle(FormatStyle::LK_Cpp));
4846 
4847   // See PR41483
4848   EXPECT_EQ("/**/ FOO(a)\n"
4849             "FOO(b)",
4850             format("/**/ FOO(a)\n"
4851                    "FOO(b)"));
4852 }
4853 
4854 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
4855   EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
4856             "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
4857             "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
4858             "class X {};\n"
4859             "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
4860             "int *createScopDetectionPass() { return 0; }",
4861             format("  INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
4862                    "  INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
4863                    "  INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
4864                    "  class X {};\n"
4865                    "  INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
4866                    "  int *createScopDetectionPass() { return 0; }"));
4867   // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
4868   // braces, so that inner block is indented one level more.
4869   EXPECT_EQ("int q() {\n"
4870             "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
4871             "  IPC_MESSAGE_HANDLER(xxx, qqq)\n"
4872             "  IPC_END_MESSAGE_MAP()\n"
4873             "}",
4874             format("int q() {\n"
4875                    "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
4876                    "    IPC_MESSAGE_HANDLER(xxx, qqq)\n"
4877                    "  IPC_END_MESSAGE_MAP()\n"
4878                    "}"));
4879 
4880   // Same inside macros.
4881   EXPECT_EQ("#define LIST(L) \\\n"
4882             "  L(A)          \\\n"
4883             "  L(B)          \\\n"
4884             "  L(C)",
4885             format("#define LIST(L) \\\n"
4886                    "  L(A) \\\n"
4887                    "  L(B) \\\n"
4888                    "  L(C)",
4889                    getGoogleStyle()));
4890 
4891   // These must not be recognized as macros.
4892   EXPECT_EQ("int q() {\n"
4893             "  f(x);\n"
4894             "  f(x) {}\n"
4895             "  f(x)->g();\n"
4896             "  f(x)->*g();\n"
4897             "  f(x).g();\n"
4898             "  f(x) = x;\n"
4899             "  f(x) += x;\n"
4900             "  f(x) -= x;\n"
4901             "  f(x) *= x;\n"
4902             "  f(x) /= x;\n"
4903             "  f(x) %= x;\n"
4904             "  f(x) &= x;\n"
4905             "  f(x) |= x;\n"
4906             "  f(x) ^= x;\n"
4907             "  f(x) >>= x;\n"
4908             "  f(x) <<= x;\n"
4909             "  f(x)[y].z();\n"
4910             "  LOG(INFO) << x;\n"
4911             "  ifstream(x) >> x;\n"
4912             "}\n",
4913             format("int q() {\n"
4914                    "  f(x)\n;\n"
4915                    "  f(x)\n {}\n"
4916                    "  f(x)\n->g();\n"
4917                    "  f(x)\n->*g();\n"
4918                    "  f(x)\n.g();\n"
4919                    "  f(x)\n = x;\n"
4920                    "  f(x)\n += x;\n"
4921                    "  f(x)\n -= x;\n"
4922                    "  f(x)\n *= x;\n"
4923                    "  f(x)\n /= x;\n"
4924                    "  f(x)\n %= x;\n"
4925                    "  f(x)\n &= x;\n"
4926                    "  f(x)\n |= x;\n"
4927                    "  f(x)\n ^= x;\n"
4928                    "  f(x)\n >>= x;\n"
4929                    "  f(x)\n <<= x;\n"
4930                    "  f(x)\n[y].z();\n"
4931                    "  LOG(INFO)\n << x;\n"
4932                    "  ifstream(x)\n >> x;\n"
4933                    "}\n"));
4934   EXPECT_EQ("int q() {\n"
4935             "  F(x)\n"
4936             "  if (1) {\n"
4937             "  }\n"
4938             "  F(x)\n"
4939             "  while (1) {\n"
4940             "  }\n"
4941             "  F(x)\n"
4942             "  G(x);\n"
4943             "  F(x)\n"
4944             "  try {\n"
4945             "    Q();\n"
4946             "  } catch (...) {\n"
4947             "  }\n"
4948             "}\n",
4949             format("int q() {\n"
4950                    "F(x)\n"
4951                    "if (1) {}\n"
4952                    "F(x)\n"
4953                    "while (1) {}\n"
4954                    "F(x)\n"
4955                    "G(x);\n"
4956                    "F(x)\n"
4957                    "try { Q(); } catch (...) {}\n"
4958                    "}\n"));
4959   EXPECT_EQ("class A {\n"
4960             "  A() : t(0) {}\n"
4961             "  A(int i) noexcept() : {}\n"
4962             "  A(X x)\n" // FIXME: function-level try blocks are broken.
4963             "  try : t(0) {\n"
4964             "  } catch (...) {\n"
4965             "  }\n"
4966             "};",
4967             format("class A {\n"
4968                    "  A()\n : t(0) {}\n"
4969                    "  A(int i)\n noexcept() : {}\n"
4970                    "  A(X x)\n"
4971                    "  try : t(0) {} catch (...) {}\n"
4972                    "};"));
4973   FormatStyle Style = getLLVMStyle();
4974   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4975   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
4976   Style.BraceWrapping.AfterFunction = true;
4977   EXPECT_EQ("void f()\n"
4978             "try\n"
4979             "{\n"
4980             "}",
4981             format("void f() try {\n"
4982                    "}",
4983                    Style));
4984   EXPECT_EQ("class SomeClass {\n"
4985             "public:\n"
4986             "  SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
4987             "};",
4988             format("class SomeClass {\n"
4989                    "public:\n"
4990                    "  SomeClass()\n"
4991                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
4992                    "};"));
4993   EXPECT_EQ("class SomeClass {\n"
4994             "public:\n"
4995             "  SomeClass()\n"
4996             "      EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
4997             "};",
4998             format("class SomeClass {\n"
4999                    "public:\n"
5000                    "  SomeClass()\n"
5001                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
5002                    "};",
5003                    getLLVMStyleWithColumns(40)));
5004 
5005   verifyFormat("MACRO(>)");
5006 
5007   // Some macros contain an implicit semicolon.
5008   Style = getLLVMStyle();
5009   Style.StatementMacros.push_back("FOO");
5010   verifyFormat("FOO(a) int b = 0;");
5011   verifyFormat("FOO(a)\n"
5012                "int b = 0;",
5013                Style);
5014   verifyFormat("FOO(a);\n"
5015                "int b = 0;",
5016                Style);
5017   verifyFormat("FOO(argc, argv, \"4.0.2\")\n"
5018                "int b = 0;",
5019                Style);
5020   verifyFormat("FOO()\n"
5021                "int b = 0;",
5022                Style);
5023   verifyFormat("FOO\n"
5024                "int b = 0;",
5025                Style);
5026   verifyFormat("void f() {\n"
5027                "  FOO(a)\n"
5028                "  return a;\n"
5029                "}",
5030                Style);
5031   verifyFormat("FOO(a)\n"
5032                "FOO(b)",
5033                Style);
5034   verifyFormat("int a = 0;\n"
5035                "FOO(b)\n"
5036                "int c = 0;",
5037                Style);
5038   verifyFormat("int a = 0;\n"
5039                "int x = FOO(a)\n"
5040                "int b = 0;",
5041                Style);
5042   verifyFormat("void foo(int a) { FOO(a) }\n"
5043                "uint32_t bar() {}",
5044                Style);
5045 }
5046 
5047 TEST_F(FormatTest, FormatsMacrosWithZeroColumnWidth) {
5048   FormatStyle ZeroColumn = getLLVMStyleWithColumns(0);
5049 
5050   verifyFormat("#define A LOOOOOOOOOOOOOOOOOOONG() LOOOOOOOOOOOOOOOOOOONG()",
5051                ZeroColumn);
5052 }
5053 
5054 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
5055   verifyFormat("#define A \\\n"
5056                "  f({     \\\n"
5057                "    g();  \\\n"
5058                "  });",
5059                getLLVMStyleWithColumns(11));
5060 }
5061 
5062 TEST_F(FormatTest, IndentPreprocessorDirectives) {
5063   FormatStyle Style = getLLVMStyleWithColumns(40);
5064   Style.IndentPPDirectives = FormatStyle::PPDIS_None;
5065   verifyFormat("#ifdef _WIN32\n"
5066                "#define A 0\n"
5067                "#ifdef VAR2\n"
5068                "#define B 1\n"
5069                "#include <someheader.h>\n"
5070                "#define MACRO                          \\\n"
5071                "  some_very_long_func_aaaaaaaaaa();\n"
5072                "#endif\n"
5073                "#else\n"
5074                "#define A 1\n"
5075                "#endif",
5076                Style);
5077   Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
5078   verifyFormat("#ifdef _WIN32\n"
5079                "#  define A 0\n"
5080                "#  ifdef VAR2\n"
5081                "#    define B 1\n"
5082                "#    include <someheader.h>\n"
5083                "#    define MACRO                      \\\n"
5084                "      some_very_long_func_aaaaaaaaaa();\n"
5085                "#  endif\n"
5086                "#else\n"
5087                "#  define A 1\n"
5088                "#endif",
5089                Style);
5090   verifyFormat("#if A\n"
5091                "#  define MACRO                        \\\n"
5092                "    void a(int x) {                    \\\n"
5093                "      b();                             \\\n"
5094                "      c();                             \\\n"
5095                "      d();                             \\\n"
5096                "      e();                             \\\n"
5097                "      f();                             \\\n"
5098                "    }\n"
5099                "#endif",
5100                Style);
5101   // Comments before include guard.
5102   verifyFormat("// file comment\n"
5103                "// file comment\n"
5104                "#ifndef HEADER_H\n"
5105                "#define HEADER_H\n"
5106                "code();\n"
5107                "#endif",
5108                Style);
5109   // Test with include guards.
5110   verifyFormat("#ifndef HEADER_H\n"
5111                "#define HEADER_H\n"
5112                "code();\n"
5113                "#endif",
5114                Style);
5115   // Include guards must have a #define with the same variable immediately
5116   // after #ifndef.
5117   verifyFormat("#ifndef NOT_GUARD\n"
5118                "#  define FOO\n"
5119                "code();\n"
5120                "#endif",
5121                Style);
5122 
5123   // Include guards must cover the entire file.
5124   verifyFormat("code();\n"
5125                "code();\n"
5126                "#ifndef NOT_GUARD\n"
5127                "#  define NOT_GUARD\n"
5128                "code();\n"
5129                "#endif",
5130                Style);
5131   verifyFormat("#ifndef NOT_GUARD\n"
5132                "#  define NOT_GUARD\n"
5133                "code();\n"
5134                "#endif\n"
5135                "code();",
5136                Style);
5137   // Test with trailing blank lines.
5138   verifyFormat("#ifndef HEADER_H\n"
5139                "#define HEADER_H\n"
5140                "code();\n"
5141                "#endif\n",
5142                Style);
5143   // Include guards don't have #else.
5144   verifyFormat("#ifndef NOT_GUARD\n"
5145                "#  define NOT_GUARD\n"
5146                "code();\n"
5147                "#else\n"
5148                "#endif",
5149                Style);
5150   verifyFormat("#ifndef NOT_GUARD\n"
5151                "#  define NOT_GUARD\n"
5152                "code();\n"
5153                "#elif FOO\n"
5154                "#endif",
5155                Style);
5156   // Non-identifier #define after potential include guard.
5157   verifyFormat("#ifndef FOO\n"
5158                "#  define 1\n"
5159                "#endif\n",
5160                Style);
5161   // #if closes past last non-preprocessor line.
5162   verifyFormat("#ifndef FOO\n"
5163                "#define FOO\n"
5164                "#if 1\n"
5165                "int i;\n"
5166                "#  define A 0\n"
5167                "#endif\n"
5168                "#endif\n",
5169                Style);
5170   // Don't crash if there is an #elif directive without a condition.
5171   verifyFormat("#if 1\n"
5172                "int x;\n"
5173                "#elif\n"
5174                "int y;\n"
5175                "#else\n"
5176                "int z;\n"
5177                "#endif",
5178                Style);
5179   // FIXME: This doesn't handle the case where there's code between the
5180   // #ifndef and #define but all other conditions hold. This is because when
5181   // the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the
5182   // previous code line yet, so we can't detect it.
5183   EXPECT_EQ("#ifndef NOT_GUARD\n"
5184             "code();\n"
5185             "#define NOT_GUARD\n"
5186             "code();\n"
5187             "#endif",
5188             format("#ifndef NOT_GUARD\n"
5189                    "code();\n"
5190                    "#  define NOT_GUARD\n"
5191                    "code();\n"
5192                    "#endif",
5193                    Style));
5194   // FIXME: This doesn't handle cases where legitimate preprocessor lines may
5195   // be outside an include guard. Examples are #pragma once and
5196   // #pragma GCC diagnostic, or anything else that does not change the meaning
5197   // of the file if it's included multiple times.
5198   EXPECT_EQ("#ifdef WIN32\n"
5199             "#  pragma once\n"
5200             "#endif\n"
5201             "#ifndef HEADER_H\n"
5202             "#  define HEADER_H\n"
5203             "code();\n"
5204             "#endif",
5205             format("#ifdef WIN32\n"
5206                    "#  pragma once\n"
5207                    "#endif\n"
5208                    "#ifndef HEADER_H\n"
5209                    "#define HEADER_H\n"
5210                    "code();\n"
5211                    "#endif",
5212                    Style));
5213   // FIXME: This does not detect when there is a single non-preprocessor line
5214   // in front of an include-guard-like structure where other conditions hold
5215   // because ScopedLineState hides the line.
5216   EXPECT_EQ("code();\n"
5217             "#ifndef HEADER_H\n"
5218             "#define HEADER_H\n"
5219             "code();\n"
5220             "#endif",
5221             format("code();\n"
5222                    "#ifndef HEADER_H\n"
5223                    "#  define HEADER_H\n"
5224                    "code();\n"
5225                    "#endif",
5226                    Style));
5227   // Keep comments aligned with #, otherwise indent comments normally. These
5228   // tests cannot use verifyFormat because messUp manipulates leading
5229   // whitespace.
5230   {
5231     const char *Expected = ""
5232                            "void f() {\n"
5233                            "#if 1\n"
5234                            "// Preprocessor aligned.\n"
5235                            "#  define A 0\n"
5236                            "  // Code. Separated by blank line.\n"
5237                            "\n"
5238                            "#  define B 0\n"
5239                            "  // Code. Not aligned with #\n"
5240                            "#  define C 0\n"
5241                            "#endif";
5242     const char *ToFormat = ""
5243                            "void f() {\n"
5244                            "#if 1\n"
5245                            "// Preprocessor aligned.\n"
5246                            "#  define A 0\n"
5247                            "// Code. Separated by blank line.\n"
5248                            "\n"
5249                            "#  define B 0\n"
5250                            "   // Code. Not aligned with #\n"
5251                            "#  define C 0\n"
5252                            "#endif";
5253     EXPECT_EQ(Expected, format(ToFormat, Style));
5254     EXPECT_EQ(Expected, format(Expected, Style));
5255   }
5256   // Keep block quotes aligned.
5257   {
5258     const char *Expected = ""
5259                            "void f() {\n"
5260                            "#if 1\n"
5261                            "/* Preprocessor aligned. */\n"
5262                            "#  define A 0\n"
5263                            "  /* Code. Separated by blank line. */\n"
5264                            "\n"
5265                            "#  define B 0\n"
5266                            "  /* Code. Not aligned with # */\n"
5267                            "#  define C 0\n"
5268                            "#endif";
5269     const char *ToFormat = ""
5270                            "void f() {\n"
5271                            "#if 1\n"
5272                            "/* Preprocessor aligned. */\n"
5273                            "#  define A 0\n"
5274                            "/* Code. Separated by blank line. */\n"
5275                            "\n"
5276                            "#  define B 0\n"
5277                            "   /* Code. Not aligned with # */\n"
5278                            "#  define C 0\n"
5279                            "#endif";
5280     EXPECT_EQ(Expected, format(ToFormat, Style));
5281     EXPECT_EQ(Expected, format(Expected, Style));
5282   }
5283   // Keep comments aligned with un-indented directives.
5284   {
5285     const char *Expected = ""
5286                            "void f() {\n"
5287                            "// Preprocessor aligned.\n"
5288                            "#define A 0\n"
5289                            "  // Code. Separated by blank line.\n"
5290                            "\n"
5291                            "#define B 0\n"
5292                            "  // Code. Not aligned with #\n"
5293                            "#define C 0\n";
5294     const char *ToFormat = ""
5295                            "void f() {\n"
5296                            "// Preprocessor aligned.\n"
5297                            "#define A 0\n"
5298                            "// Code. Separated by blank line.\n"
5299                            "\n"
5300                            "#define B 0\n"
5301                            "   // Code. Not aligned with #\n"
5302                            "#define C 0\n";
5303     EXPECT_EQ(Expected, format(ToFormat, Style));
5304     EXPECT_EQ(Expected, format(Expected, Style));
5305   }
5306   // Test AfterHash with tabs.
5307   {
5308     FormatStyle Tabbed = Style;
5309     Tabbed.UseTab = FormatStyle::UT_Always;
5310     Tabbed.IndentWidth = 8;
5311     Tabbed.TabWidth = 8;
5312     verifyFormat("#ifdef _WIN32\n"
5313                  "#\tdefine A 0\n"
5314                  "#\tifdef VAR2\n"
5315                  "#\t\tdefine B 1\n"
5316                  "#\t\tinclude <someheader.h>\n"
5317                  "#\t\tdefine MACRO          \\\n"
5318                  "\t\t\tsome_very_long_func_aaaaaaaaaa();\n"
5319                  "#\tendif\n"
5320                  "#else\n"
5321                  "#\tdefine A 1\n"
5322                  "#endif",
5323                  Tabbed);
5324   }
5325 
5326   // Regression test: Multiline-macro inside include guards.
5327   verifyFormat("#ifndef HEADER_H\n"
5328                "#define HEADER_H\n"
5329                "#define A()        \\\n"
5330                "  int i;           \\\n"
5331                "  int j;\n"
5332                "#endif // HEADER_H",
5333                getLLVMStyleWithColumns(20));
5334 
5335   Style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash;
5336   // Basic before hash indent tests
5337   verifyFormat("#ifdef _WIN32\n"
5338                "  #define A 0\n"
5339                "  #ifdef VAR2\n"
5340                "    #define B 1\n"
5341                "    #include <someheader.h>\n"
5342                "    #define MACRO                      \\\n"
5343                "      some_very_long_func_aaaaaaaaaa();\n"
5344                "  #endif\n"
5345                "#else\n"
5346                "  #define A 1\n"
5347                "#endif",
5348                Style);
5349   verifyFormat("#if A\n"
5350                "  #define MACRO                        \\\n"
5351                "    void a(int x) {                    \\\n"
5352                "      b();                             \\\n"
5353                "      c();                             \\\n"
5354                "      d();                             \\\n"
5355                "      e();                             \\\n"
5356                "      f();                             \\\n"
5357                "    }\n"
5358                "#endif",
5359                Style);
5360   // Keep comments aligned with indented directives. These
5361   // tests cannot use verifyFormat because messUp manipulates leading
5362   // whitespace.
5363   {
5364     const char *Expected = "void f() {\n"
5365                            "// Aligned to preprocessor.\n"
5366                            "#if 1\n"
5367                            "  // Aligned to code.\n"
5368                            "  int a;\n"
5369                            "  #if 1\n"
5370                            "    // Aligned to preprocessor.\n"
5371                            "    #define A 0\n"
5372                            "  // Aligned to code.\n"
5373                            "  int b;\n"
5374                            "  #endif\n"
5375                            "#endif\n"
5376                            "}";
5377     const char *ToFormat = "void f() {\n"
5378                            "// Aligned to preprocessor.\n"
5379                            "#if 1\n"
5380                            "// Aligned to code.\n"
5381                            "int a;\n"
5382                            "#if 1\n"
5383                            "// Aligned to preprocessor.\n"
5384                            "#define A 0\n"
5385                            "// Aligned to code.\n"
5386                            "int b;\n"
5387                            "#endif\n"
5388                            "#endif\n"
5389                            "}";
5390     EXPECT_EQ(Expected, format(ToFormat, Style));
5391     EXPECT_EQ(Expected, format(Expected, Style));
5392   }
5393   {
5394     const char *Expected = "void f() {\n"
5395                            "/* Aligned to preprocessor. */\n"
5396                            "#if 1\n"
5397                            "  /* Aligned to code. */\n"
5398                            "  int a;\n"
5399                            "  #if 1\n"
5400                            "    /* Aligned to preprocessor. */\n"
5401                            "    #define A 0\n"
5402                            "  /* Aligned to code. */\n"
5403                            "  int b;\n"
5404                            "  #endif\n"
5405                            "#endif\n"
5406                            "}";
5407     const char *ToFormat = "void f() {\n"
5408                            "/* Aligned to preprocessor. */\n"
5409                            "#if 1\n"
5410                            "/* Aligned to code. */\n"
5411                            "int a;\n"
5412                            "#if 1\n"
5413                            "/* Aligned to preprocessor. */\n"
5414                            "#define A 0\n"
5415                            "/* Aligned to code. */\n"
5416                            "int b;\n"
5417                            "#endif\n"
5418                            "#endif\n"
5419                            "}";
5420     EXPECT_EQ(Expected, format(ToFormat, Style));
5421     EXPECT_EQ(Expected, format(Expected, Style));
5422   }
5423 
5424   // Test single comment before preprocessor
5425   verifyFormat("// Comment\n"
5426                "\n"
5427                "#if 1\n"
5428                "#endif",
5429                Style);
5430 }
5431 
5432 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
5433   verifyFormat("{\n  { a #c; }\n}");
5434 }
5435 
5436 TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
5437   EXPECT_EQ("#define A \\\n  {       \\\n    {\nint i;",
5438             format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
5439   EXPECT_EQ("#define A \\\n  }       \\\n  }\nint i;",
5440             format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
5441 }
5442 
5443 TEST_F(FormatTest, EscapedNewlines) {
5444   FormatStyle Narrow = getLLVMStyleWithColumns(11);
5445   EXPECT_EQ("#define A \\\n  int i;  \\\n  int j;",
5446             format("#define A \\\nint i;\\\n  int j;", Narrow));
5447   EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;"));
5448   EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
5449   EXPECT_EQ("/* \\  \\  \\\n */", format("\\\n/* \\  \\  \\\n */"));
5450   EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>"));
5451 
5452   FormatStyle AlignLeft = getLLVMStyle();
5453   AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
5454   EXPECT_EQ("#define MACRO(x) \\\n"
5455             "private:         \\\n"
5456             "  int x(int a);\n",
5457             format("#define MACRO(x) \\\n"
5458                    "private:         \\\n"
5459                    "  int x(int a);\n",
5460                    AlignLeft));
5461 
5462   // CRLF line endings
5463   EXPECT_EQ("#define A \\\r\n  int i;  \\\r\n  int j;",
5464             format("#define A \\\r\nint i;\\\r\n  int j;", Narrow));
5465   EXPECT_EQ("#define A\r\n\r\nint i;", format("#define A \\\r\n\r\n int i;"));
5466   EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
5467   EXPECT_EQ("/* \\  \\  \\\r\n */", format("\\\r\n/* \\  \\  \\\r\n */"));
5468   EXPECT_EQ("<a\r\n\\\\\r\n>", format("<a\r\n\\\\\r\n>"));
5469   EXPECT_EQ("#define MACRO(x) \\\r\n"
5470             "private:         \\\r\n"
5471             "  int x(int a);\r\n",
5472             format("#define MACRO(x) \\\r\n"
5473                    "private:         \\\r\n"
5474                    "  int x(int a);\r\n",
5475                    AlignLeft));
5476 
5477   FormatStyle DontAlign = getLLVMStyle();
5478   DontAlign.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
5479   DontAlign.MaxEmptyLinesToKeep = 3;
5480   // FIXME: can't use verifyFormat here because the newline before
5481   // "public:" is not inserted the first time it's reformatted
5482   EXPECT_EQ("#define A \\\n"
5483             "  class Foo { \\\n"
5484             "    void bar(); \\\n"
5485             "\\\n"
5486             "\\\n"
5487             "\\\n"
5488             "  public: \\\n"
5489             "    void baz(); \\\n"
5490             "  };",
5491             format("#define A \\\n"
5492                    "  class Foo { \\\n"
5493                    "    void bar(); \\\n"
5494                    "\\\n"
5495                    "\\\n"
5496                    "\\\n"
5497                    "  public: \\\n"
5498                    "    void baz(); \\\n"
5499                    "  };",
5500                    DontAlign));
5501 }
5502 
5503 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
5504   verifyFormat("#define A \\\n"
5505                "  int v(  \\\n"
5506                "      a); \\\n"
5507                "  int i;",
5508                getLLVMStyleWithColumns(11));
5509 }
5510 
5511 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
5512   EXPECT_EQ(
5513       "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
5514       "                      \\\n"
5515       "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
5516       "\n"
5517       "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
5518       "    aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
5519       format("  #define   ALooooooooooooooooooooooooooooooooooooooongMacro("
5520              "\\\n"
5521              "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
5522              "  \n"
5523              "   AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
5524              "  aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
5525 }
5526 
5527 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
5528   EXPECT_EQ("int\n"
5529             "#define A\n"
5530             "    a;",
5531             format("int\n#define A\na;"));
5532   verifyFormat("functionCallTo(\n"
5533                "    someOtherFunction(\n"
5534                "        withSomeParameters, whichInSequence,\n"
5535                "        areLongerThanALine(andAnotherCall,\n"
5536                "#define A B\n"
5537                "                           withMoreParamters,\n"
5538                "                           whichStronglyInfluenceTheLayout),\n"
5539                "        andMoreParameters),\n"
5540                "    trailing);",
5541                getLLVMStyleWithColumns(69));
5542   verifyFormat("Foo::Foo()\n"
5543                "#ifdef BAR\n"
5544                "    : baz(0)\n"
5545                "#endif\n"
5546                "{\n"
5547                "}");
5548   verifyFormat("void f() {\n"
5549                "  if (true)\n"
5550                "#ifdef A\n"
5551                "    f(42);\n"
5552                "  x();\n"
5553                "#else\n"
5554                "    g();\n"
5555                "  x();\n"
5556                "#endif\n"
5557                "}");
5558   verifyFormat("void f(param1, param2,\n"
5559                "       param3,\n"
5560                "#ifdef A\n"
5561                "       param4(param5,\n"
5562                "#ifdef A1\n"
5563                "              param6,\n"
5564                "#ifdef A2\n"
5565                "              param7),\n"
5566                "#else\n"
5567                "              param8),\n"
5568                "       param9,\n"
5569                "#endif\n"
5570                "       param10,\n"
5571                "#endif\n"
5572                "       param11)\n"
5573                "#else\n"
5574                "       param12)\n"
5575                "#endif\n"
5576                "{\n"
5577                "  x();\n"
5578                "}",
5579                getLLVMStyleWithColumns(28));
5580   verifyFormat("#if 1\n"
5581                "int i;");
5582   verifyFormat("#if 1\n"
5583                "#endif\n"
5584                "#if 1\n"
5585                "#else\n"
5586                "#endif\n");
5587   verifyFormat("DEBUG({\n"
5588                "  return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5589                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
5590                "});\n"
5591                "#if a\n"
5592                "#else\n"
5593                "#endif");
5594 
5595   verifyIncompleteFormat("void f(\n"
5596                          "#if A\n"
5597                          ");\n"
5598                          "#else\n"
5599                          "#endif");
5600 }
5601 
5602 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
5603   verifyFormat("#endif\n"
5604                "#if B");
5605 }
5606 
5607 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
5608   FormatStyle SingleLine = getLLVMStyle();
5609   SingleLine.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse;
5610   verifyFormat("#if 0\n"
5611                "#elif 1\n"
5612                "#endif\n"
5613                "void foo() {\n"
5614                "  if (test) foo2();\n"
5615                "}",
5616                SingleLine);
5617 }
5618 
5619 TEST_F(FormatTest, LayoutBlockInsideParens) {
5620   verifyFormat("functionCall({ int i; });");
5621   verifyFormat("functionCall({\n"
5622                "  int i;\n"
5623                "  int j;\n"
5624                "});");
5625   verifyFormat("functionCall(\n"
5626                "    {\n"
5627                "      int i;\n"
5628                "      int j;\n"
5629                "    },\n"
5630                "    aaaa, bbbb, cccc);");
5631   verifyFormat("functionA(functionB({\n"
5632                "            int i;\n"
5633                "            int j;\n"
5634                "          }),\n"
5635                "          aaaa, bbbb, cccc);");
5636   verifyFormat("functionCall(\n"
5637                "    {\n"
5638                "      int i;\n"
5639                "      int j;\n"
5640                "    },\n"
5641                "    aaaa, bbbb, // comment\n"
5642                "    cccc);");
5643   verifyFormat("functionA(functionB({\n"
5644                "            int i;\n"
5645                "            int j;\n"
5646                "          }),\n"
5647                "          aaaa, bbbb, // comment\n"
5648                "          cccc);");
5649   verifyFormat("functionCall(aaaa, bbbb, { int i; });");
5650   verifyFormat("functionCall(aaaa, bbbb, {\n"
5651                "  int i;\n"
5652                "  int j;\n"
5653                "});");
5654   verifyFormat(
5655       "Aaa(\n" // FIXME: There shouldn't be a linebreak here.
5656       "    {\n"
5657       "      int i; // break\n"
5658       "    },\n"
5659       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
5660       "                                     ccccccccccccccccc));");
5661   verifyFormat("DEBUG({\n"
5662                "  if (a)\n"
5663                "    f();\n"
5664                "});");
5665 }
5666 
5667 TEST_F(FormatTest, LayoutBlockInsideStatement) {
5668   EXPECT_EQ("SOME_MACRO { int i; }\n"
5669             "int i;",
5670             format("  SOME_MACRO  {int i;}  int i;"));
5671 }
5672 
5673 TEST_F(FormatTest, LayoutNestedBlocks) {
5674   verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
5675                "  struct s {\n"
5676                "    int i;\n"
5677                "  };\n"
5678                "  s kBitsToOs[] = {{10}};\n"
5679                "  for (int i = 0; i < 10; ++i)\n"
5680                "    return;\n"
5681                "}");
5682   verifyFormat("call(parameter, {\n"
5683                "  something();\n"
5684                "  // Comment using all columns.\n"
5685                "  somethingelse();\n"
5686                "});",
5687                getLLVMStyleWithColumns(40));
5688   verifyFormat("DEBUG( //\n"
5689                "    { f(); }, a);");
5690   verifyFormat("DEBUG( //\n"
5691                "    {\n"
5692                "      f(); //\n"
5693                "    },\n"
5694                "    a);");
5695 
5696   EXPECT_EQ("call(parameter, {\n"
5697             "  something();\n"
5698             "  // Comment too\n"
5699             "  // looooooooooong.\n"
5700             "  somethingElse();\n"
5701             "});",
5702             format("call(parameter, {\n"
5703                    "  something();\n"
5704                    "  // Comment too looooooooooong.\n"
5705                    "  somethingElse();\n"
5706                    "});",
5707                    getLLVMStyleWithColumns(29)));
5708   EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int   i; });"));
5709   EXPECT_EQ("DEBUG({ // comment\n"
5710             "  int i;\n"
5711             "});",
5712             format("DEBUG({ // comment\n"
5713                    "int  i;\n"
5714                    "});"));
5715   EXPECT_EQ("DEBUG({\n"
5716             "  int i;\n"
5717             "\n"
5718             "  // comment\n"
5719             "  int j;\n"
5720             "});",
5721             format("DEBUG({\n"
5722                    "  int  i;\n"
5723                    "\n"
5724                    "  // comment\n"
5725                    "  int  j;\n"
5726                    "});"));
5727 
5728   verifyFormat("DEBUG({\n"
5729                "  if (a)\n"
5730                "    return;\n"
5731                "});");
5732   verifyGoogleFormat("DEBUG({\n"
5733                      "  if (a) return;\n"
5734                      "});");
5735   FormatStyle Style = getGoogleStyle();
5736   Style.ColumnLimit = 45;
5737   verifyFormat("Debug(\n"
5738                "    aaaaa,\n"
5739                "    {\n"
5740                "      if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
5741                "    },\n"
5742                "    a);",
5743                Style);
5744 
5745   verifyFormat("SomeFunction({MACRO({ return output; }), b});");
5746 
5747   verifyNoCrash("^{v^{a}}");
5748 }
5749 
5750 TEST_F(FormatTest, FormatNestedBlocksInMacros) {
5751   EXPECT_EQ("#define MACRO()                     \\\n"
5752             "  Debug(aaa, /* force line break */ \\\n"
5753             "        {                           \\\n"
5754             "          int i;                    \\\n"
5755             "          int j;                    \\\n"
5756             "        })",
5757             format("#define   MACRO()   Debug(aaa,  /* force line break */ \\\n"
5758                    "          {  int   i;  int  j;   })",
5759                    getGoogleStyle()));
5760 
5761   EXPECT_EQ("#define A                                       \\\n"
5762             "  [] {                                          \\\n"
5763             "    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(        \\\n"
5764             "        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
5765             "  }",
5766             format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
5767                    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
5768                    getGoogleStyle()));
5769 }
5770 
5771 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
5772   EXPECT_EQ("{}", format("{}"));
5773   verifyFormat("enum E {};");
5774   verifyFormat("enum E {}");
5775   FormatStyle Style = getLLVMStyle();
5776   Style.SpaceInEmptyBlock = true;
5777   EXPECT_EQ("void f() { }", format("void f() {}", Style));
5778   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty;
5779   EXPECT_EQ("while (true) { }", format("while (true) {}", Style));
5780   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
5781   Style.BraceWrapping.BeforeElse = false;
5782   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
5783   verifyFormat("if (a)\n"
5784                "{\n"
5785                "} else if (b)\n"
5786                "{\n"
5787                "} else\n"
5788                "{ }",
5789                Style);
5790   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never;
5791   verifyFormat("if (a) {\n"
5792                "} else if (b) {\n"
5793                "} else {\n"
5794                "}",
5795                Style);
5796   Style.BraceWrapping.BeforeElse = true;
5797   verifyFormat("if (a) { }\n"
5798                "else if (b) { }\n"
5799                "else { }",
5800                Style);
5801 }
5802 
5803 TEST_F(FormatTest, FormatBeginBlockEndMacros) {
5804   FormatStyle Style = getLLVMStyle();
5805   Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$";
5806   Style.MacroBlockEnd = "^[A-Z_]+_END$";
5807   verifyFormat("FOO_BEGIN\n"
5808                "  FOO_ENTRY\n"
5809                "FOO_END",
5810                Style);
5811   verifyFormat("FOO_BEGIN\n"
5812                "  NESTED_FOO_BEGIN\n"
5813                "    NESTED_FOO_ENTRY\n"
5814                "  NESTED_FOO_END\n"
5815                "FOO_END",
5816                Style);
5817   verifyFormat("FOO_BEGIN(Foo, Bar)\n"
5818                "  int x;\n"
5819                "  x = 1;\n"
5820                "FOO_END(Baz)",
5821                Style);
5822 }
5823 
5824 //===----------------------------------------------------------------------===//
5825 // Line break tests.
5826 //===----------------------------------------------------------------------===//
5827 
5828 TEST_F(FormatTest, PreventConfusingIndents) {
5829   verifyFormat(
5830       "void f() {\n"
5831       "  SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
5832       "                         parameter, parameter, parameter)),\n"
5833       "                     SecondLongCall(parameter));\n"
5834       "}");
5835   verifyFormat(
5836       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5837       "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
5838       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
5839       "    aaaaaaaaaaaaaaaaaaaaaaaa);");
5840   verifyFormat(
5841       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5842       "    [aaaaaaaaaaaaaaaaaaaaaaaa\n"
5843       "         [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
5844       "         [aaaaaaaaaaaaaaaaaaaaaaaa]];");
5845   verifyFormat(
5846       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
5847       "    aaaaaaaaaaaaaaaaaaaaaaaa<\n"
5848       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
5849       "    aaaaaaaaaaaaaaaaaaaaaaaa>;");
5850   verifyFormat("int a = bbbb && ccc &&\n"
5851                "        fffff(\n"
5852                "#define A Just forcing a new line\n"
5853                "            ddd);");
5854 }
5855 
5856 TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
5857   verifyFormat(
5858       "bool aaaaaaa =\n"
5859       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
5860       "    bbbbbbbb();");
5861   verifyFormat(
5862       "bool aaaaaaa =\n"
5863       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n"
5864       "    bbbbbbbb();");
5865 
5866   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
5867                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
5868                "    ccccccccc == ddddddddddd;");
5869   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
5870                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n"
5871                "    ccccccccc == ddddddddddd;");
5872   verifyFormat(
5873       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
5874       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n"
5875       "    ccccccccc == ddddddddddd;");
5876 
5877   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
5878                "                 aaaaaa) &&\n"
5879                "         bbbbbb && cccccc;");
5880   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
5881                "                 aaaaaa) >>\n"
5882                "         bbbbbb;");
5883   verifyFormat("aa = Whitespaces.addUntouchableComment(\n"
5884                "    SourceMgr.getSpellingColumnNumber(\n"
5885                "        TheLine.Last->FormatTok.Tok.getLocation()) -\n"
5886                "    1);");
5887 
5888   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
5889                "     bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
5890                "    cccccc) {\n}");
5891   verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
5892                "               bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n"
5893                "              cccccc) {\n}");
5894   verifyFormat("if CONSTEXPR ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
5895                "               bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n"
5896                "              cccccc) {\n}");
5897   verifyFormat("b = a &&\n"
5898                "    // Comment\n"
5899                "    b.c && d;");
5900 
5901   // If the LHS of a comparison is not a binary expression itself, the
5902   // additional linebreak confuses many people.
5903   verifyFormat(
5904       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5905       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
5906       "}");
5907   verifyFormat(
5908       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5909       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
5910       "}");
5911   verifyFormat(
5912       "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
5913       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
5914       "}");
5915   verifyFormat(
5916       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5917       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) <=> 5) {\n"
5918       "}");
5919   // Even explicit parentheses stress the precedence enough to make the
5920   // additional break unnecessary.
5921   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5922                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
5923                "}");
5924   // This cases is borderline, but with the indentation it is still readable.
5925   verifyFormat(
5926       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5927       "        aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5928       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
5929       "}",
5930       getLLVMStyleWithColumns(75));
5931 
5932   // If the LHS is a binary expression, we should still use the additional break
5933   // as otherwise the formatting hides the operator precedence.
5934   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5935                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
5936                "    5) {\n"
5937                "}");
5938   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5939                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa <=>\n"
5940                "    5) {\n"
5941                "}");
5942 
5943   FormatStyle OnePerLine = getLLVMStyle();
5944   OnePerLine.BinPackParameters = false;
5945   verifyFormat(
5946       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
5947       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
5948       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
5949       OnePerLine);
5950 
5951   verifyFormat("int i = someFunction(aaaaaaa, 0)\n"
5952                "                .aaa(aaaaaaaaaaaaa) *\n"
5953                "            aaaaaaa +\n"
5954                "        aaaaaaa;",
5955                getLLVMStyleWithColumns(40));
5956 }
5957 
5958 TEST_F(FormatTest, ExpressionIndentation) {
5959   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5960                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5961                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
5962                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
5963                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
5964                "                     bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
5965                "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
5966                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
5967                "                 ccccccccccccccccccccccccccccccccccccccccc;");
5968   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
5969                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5970                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
5971                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
5972   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5973                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
5974                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
5975                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
5976   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
5977                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
5978                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5979                "        bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
5980   verifyFormat("if () {\n"
5981                "} else if (aaaaa && bbbbb > // break\n"
5982                "                        ccccc) {\n"
5983                "}");
5984   verifyFormat("if () {\n"
5985                "} else if constexpr (aaaaa && bbbbb > // break\n"
5986                "                                  ccccc) {\n"
5987                "}");
5988   verifyFormat("if () {\n"
5989                "} else if CONSTEXPR (aaaaa && bbbbb > // break\n"
5990                "                                  ccccc) {\n"
5991                "}");
5992   verifyFormat("if () {\n"
5993                "} else if (aaaaa &&\n"
5994                "           bbbbb > // break\n"
5995                "               ccccc &&\n"
5996                "           ddddd) {\n"
5997                "}");
5998 
5999   // Presence of a trailing comment used to change indentation of b.
6000   verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
6001                "       b;\n"
6002                "return aaaaaaaaaaaaaaaaaaa +\n"
6003                "       b; //",
6004                getLLVMStyleWithColumns(30));
6005 }
6006 
6007 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
6008   // Not sure what the best system is here. Like this, the LHS can be found
6009   // immediately above an operator (everything with the same or a higher
6010   // indent). The RHS is aligned right of the operator and so compasses
6011   // everything until something with the same indent as the operator is found.
6012   // FIXME: Is this a good system?
6013   FormatStyle Style = getLLVMStyle();
6014   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
6015   verifyFormat(
6016       "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6017       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6018       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6019       "                 == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6020       "                            * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6021       "                        + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6022       "             && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6023       "                        * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6024       "                    > ccccccccccccccccccccccccccccccccccccccccc;",
6025       Style);
6026   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6027                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6028                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6029                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
6030                Style);
6031   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6032                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6033                "              * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6034                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
6035                Style);
6036   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6037                "    == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6038                "               * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6039                "           + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
6040                Style);
6041   verifyFormat("if () {\n"
6042                "} else if (aaaaa\n"
6043                "           && bbbbb // break\n"
6044                "                  > ccccc) {\n"
6045                "}",
6046                Style);
6047   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6048                "       && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
6049                Style);
6050   verifyFormat("return (a)\n"
6051                "       // comment\n"
6052                "       + b;",
6053                Style);
6054   verifyFormat(
6055       "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6056       "                 * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6057       "             + cc;",
6058       Style);
6059 
6060   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6061                "    = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
6062                Style);
6063 
6064   // Forced by comments.
6065   verifyFormat(
6066       "unsigned ContentSize =\n"
6067       "    sizeof(int16_t)   // DWARF ARange version number\n"
6068       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
6069       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
6070       "    + sizeof(int8_t); // Segment Size (in bytes)");
6071 
6072   verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
6073                "       == boost::fusion::at_c<1>(iiii).second;",
6074                Style);
6075 
6076   Style.ColumnLimit = 60;
6077   verifyFormat("zzzzzzzzzz\n"
6078                "    = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6079                "      >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
6080                Style);
6081 
6082   Style.ColumnLimit = 80;
6083   Style.IndentWidth = 4;
6084   Style.TabWidth = 4;
6085   Style.UseTab = FormatStyle::UT_Always;
6086   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
6087   Style.AlignOperands = FormatStyle::OAS_DontAlign;
6088   EXPECT_EQ("return someVeryVeryLongConditionThatBarelyFitsOnALine\n"
6089             "\t&& (someOtherLongishConditionPart1\n"
6090             "\t\t|| someOtherEvenLongerNestedConditionPart2);",
6091             format("return someVeryVeryLongConditionThatBarelyFitsOnALine && "
6092                    "(someOtherLongishConditionPart1 || "
6093                    "someOtherEvenLongerNestedConditionPart2);",
6094                    Style));
6095 }
6096 
6097 TEST_F(FormatTest, ExpressionIndentationStrictAlign) {
6098   FormatStyle Style = getLLVMStyle();
6099   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
6100   Style.AlignOperands = FormatStyle::OAS_AlignAfterOperator;
6101 
6102   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6103                "                   + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6104                "                   + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6105                "              == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6106                "                         * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6107                "                     + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6108                "          && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6109                "                     * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6110                "                 > ccccccccccccccccccccccccccccccccccccccccc;",
6111                Style);
6112   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6113                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6114                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6115                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
6116                Style);
6117   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6118                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6119                "              * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6120                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
6121                Style);
6122   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6123                "    == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6124                "               * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6125                "           + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
6126                Style);
6127   verifyFormat("if () {\n"
6128                "} else if (aaaaa\n"
6129                "           && bbbbb // break\n"
6130                "                  > ccccc) {\n"
6131                "}",
6132                Style);
6133   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6134                "    && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
6135                Style);
6136   verifyFormat("return (a)\n"
6137                "     // comment\n"
6138                "     + b;",
6139                Style);
6140   verifyFormat(
6141       "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6142       "               * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6143       "           + cc;",
6144       Style);
6145   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
6146                "     : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
6147                "                        : 3333333333333333;",
6148                Style);
6149   verifyFormat(
6150       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa    ? bbbbbbbbbbbbbbbbbb\n"
6151       "                           : ccccccccccccccc ? dddddddddddddddddd\n"
6152       "                                             : eeeeeeeeeeeeeeeeee)\n"
6153       "     : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
6154       "                        : 3333333333333333;",
6155       Style);
6156   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6157                "    = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
6158                Style);
6159 
6160   verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
6161                "    == boost::fusion::at_c<1>(iiii).second;",
6162                Style);
6163 
6164   Style.ColumnLimit = 60;
6165   verifyFormat("zzzzzzzzzzzzz\n"
6166                "    = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6167                "   >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
6168                Style);
6169 
6170   // Forced by comments.
6171   Style.ColumnLimit = 80;
6172   verifyFormat(
6173       "unsigned ContentSize\n"
6174       "    = sizeof(int16_t) // DWARF ARange version number\n"
6175       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
6176       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
6177       "    + sizeof(int8_t); // Segment Size (in bytes)",
6178       Style);
6179 
6180   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
6181   verifyFormat(
6182       "unsigned ContentSize =\n"
6183       "    sizeof(int16_t)   // DWARF ARange version number\n"
6184       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
6185       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
6186       "    + sizeof(int8_t); // Segment Size (in bytes)",
6187       Style);
6188 
6189   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
6190   verifyFormat(
6191       "unsigned ContentSize =\n"
6192       "    sizeof(int16_t)   // DWARF ARange version number\n"
6193       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
6194       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
6195       "    + sizeof(int8_t); // Segment Size (in bytes)",
6196       Style);
6197 }
6198 
6199 TEST_F(FormatTest, EnforcedOperatorWraps) {
6200   // Here we'd like to wrap after the || operators, but a comment is forcing an
6201   // earlier wrap.
6202   verifyFormat("bool x = aaaaa //\n"
6203                "         || bbbbb\n"
6204                "         //\n"
6205                "         || cccc;");
6206 }
6207 
6208 TEST_F(FormatTest, NoOperandAlignment) {
6209   FormatStyle Style = getLLVMStyle();
6210   Style.AlignOperands = FormatStyle::OAS_DontAlign;
6211   verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n"
6212                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
6213                "                   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
6214                Style);
6215   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
6216   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6217                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6218                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6219                "        == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6220                "                * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6221                "            + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6222                "    && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6223                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6224                "        > ccccccccccccccccccccccccccccccccccccccccc;",
6225                Style);
6226 
6227   verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6228                "        * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6229                "    + cc;",
6230                Style);
6231   verifyFormat("int a = aa\n"
6232                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6233                "        * cccccccccccccccccccccccccccccccccccc;\n",
6234                Style);
6235 
6236   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
6237   verifyFormat("return (a > b\n"
6238                "    // comment1\n"
6239                "    // comment2\n"
6240                "    || c);",
6241                Style);
6242 }
6243 
6244 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) {
6245   FormatStyle Style = getLLVMStyle();
6246   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
6247   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
6248                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6249                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
6250                Style);
6251 }
6252 
6253 TEST_F(FormatTest, AllowBinPackingInsideArguments) {
6254   FormatStyle Style = getLLVMStyleWithColumns(40);
6255   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
6256   Style.BinPackArguments = false;
6257   verifyFormat("void test() {\n"
6258                "  someFunction(\n"
6259                "      this + argument + is + quite\n"
6260                "      + long + so + it + gets + wrapped\n"
6261                "      + but + remains + bin - packed);\n"
6262                "}",
6263                Style);
6264   verifyFormat("void test() {\n"
6265                "  someFunction(arg1,\n"
6266                "               this + argument + is\n"
6267                "                   + quite + long + so\n"
6268                "                   + it + gets + wrapped\n"
6269                "                   + but + remains + bin\n"
6270                "                   - packed,\n"
6271                "               arg3);\n"
6272                "}",
6273                Style);
6274   verifyFormat("void test() {\n"
6275                "  someFunction(\n"
6276                "      arg1,\n"
6277                "      this + argument + has\n"
6278                "          + anotherFunc(nested,\n"
6279                "                        calls + whose\n"
6280                "                            + arguments\n"
6281                "                            + are + also\n"
6282                "                            + wrapped,\n"
6283                "                        in + addition)\n"
6284                "          + to + being + bin - packed,\n"
6285                "      arg3);\n"
6286                "}",
6287                Style);
6288 
6289   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
6290   verifyFormat("void test() {\n"
6291                "  someFunction(\n"
6292                "      arg1,\n"
6293                "      this + argument + has +\n"
6294                "          anotherFunc(nested,\n"
6295                "                      calls + whose +\n"
6296                "                          arguments +\n"
6297                "                          are + also +\n"
6298                "                          wrapped,\n"
6299                "                      in + addition) +\n"
6300                "          to + being + bin - packed,\n"
6301                "      arg3);\n"
6302                "}",
6303                Style);
6304 }
6305 
6306 TEST_F(FormatTest, ConstructorInitializers) {
6307   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
6308   verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
6309                getLLVMStyleWithColumns(45));
6310   verifyFormat("Constructor()\n"
6311                "    : Inttializer(FitsOnTheLine) {}",
6312                getLLVMStyleWithColumns(44));
6313   verifyFormat("Constructor()\n"
6314                "    : Inttializer(FitsOnTheLine) {}",
6315                getLLVMStyleWithColumns(43));
6316 
6317   verifyFormat("template <typename T>\n"
6318                "Constructor() : Initializer(FitsOnTheLine) {}",
6319                getLLVMStyleWithColumns(45));
6320 
6321   verifyFormat(
6322       "SomeClass::Constructor()\n"
6323       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
6324 
6325   verifyFormat(
6326       "SomeClass::Constructor()\n"
6327       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6328       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
6329   verifyFormat(
6330       "SomeClass::Constructor()\n"
6331       "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6332       "      aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
6333   verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6334                "            aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
6335                "    : aaaaaaaaaa(aaaaaa) {}");
6336 
6337   verifyFormat("Constructor()\n"
6338                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6339                "      aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6340                "                               aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6341                "      aaaaaaaaaaaaaaaaaaaaaaa() {}");
6342 
6343   verifyFormat("Constructor()\n"
6344                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6345                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
6346 
6347   verifyFormat("Constructor(int Parameter = 0)\n"
6348                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
6349                "      aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
6350   verifyFormat("Constructor()\n"
6351                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
6352                "}",
6353                getLLVMStyleWithColumns(60));
6354   verifyFormat("Constructor()\n"
6355                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6356                "          aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}");
6357 
6358   // Here a line could be saved by splitting the second initializer onto two
6359   // lines, but that is not desirable.
6360   verifyFormat("Constructor()\n"
6361                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
6362                "      aaaaaaaaaaa(aaaaaaaaaaa),\n"
6363                "      aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
6364 
6365   FormatStyle OnePerLine = getLLVMStyle();
6366   OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_Never;
6367   verifyFormat("MyClass::MyClass()\n"
6368                "    : a(a),\n"
6369                "      b(b),\n"
6370                "      c(c) {}",
6371                OnePerLine);
6372   verifyFormat("MyClass::MyClass()\n"
6373                "    : a(a), // comment\n"
6374                "      b(b),\n"
6375                "      c(c) {}",
6376                OnePerLine);
6377   verifyFormat("MyClass::MyClass(int a)\n"
6378                "    : b(a),      // comment\n"
6379                "      c(a + 1) { // lined up\n"
6380                "}",
6381                OnePerLine);
6382   verifyFormat("Constructor()\n"
6383                "    : a(b, b, b) {}",
6384                OnePerLine);
6385   OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6386   OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false;
6387   verifyFormat("SomeClass::Constructor()\n"
6388                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6389                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6390                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
6391                OnePerLine);
6392   verifyFormat("SomeClass::Constructor()\n"
6393                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
6394                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6395                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
6396                OnePerLine);
6397   verifyFormat("MyClass::MyClass(int var)\n"
6398                "    : some_var_(var),            // 4 space indent\n"
6399                "      some_other_var_(var + 1) { // lined up\n"
6400                "}",
6401                OnePerLine);
6402   verifyFormat("Constructor()\n"
6403                "    : aaaaa(aaaaaa),\n"
6404                "      aaaaa(aaaaaa),\n"
6405                "      aaaaa(aaaaaa),\n"
6406                "      aaaaa(aaaaaa),\n"
6407                "      aaaaa(aaaaaa) {}",
6408                OnePerLine);
6409   verifyFormat("Constructor()\n"
6410                "    : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
6411                "            aaaaaaaaaaaaaaaaaaaaaa) {}",
6412                OnePerLine);
6413   OnePerLine.BinPackParameters = false;
6414   verifyFormat(
6415       "Constructor()\n"
6416       "    : aaaaaaaaaaaaaaaaaaaaaaaa(\n"
6417       "          aaaaaaaaaaa().aaa(),\n"
6418       "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
6419       OnePerLine);
6420   OnePerLine.ColumnLimit = 60;
6421   verifyFormat("Constructor()\n"
6422                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
6423                "      bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
6424                OnePerLine);
6425 
6426   EXPECT_EQ("Constructor()\n"
6427             "    : // Comment forcing unwanted break.\n"
6428             "      aaaa(aaaa) {}",
6429             format("Constructor() :\n"
6430                    "    // Comment forcing unwanted break.\n"
6431                    "    aaaa(aaaa) {}"));
6432 }
6433 
6434 TEST_F(FormatTest, AllowAllConstructorInitializersOnNextLine) {
6435   FormatStyle Style = getLLVMStyleWithColumns(60);
6436   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
6437   Style.BinPackParameters = false;
6438 
6439   for (int i = 0; i < 4; ++i) {
6440     // Test all combinations of parameters that should not have an effect.
6441     Style.AllowAllParametersOfDeclarationOnNextLine = i & 1;
6442     Style.AllowAllArgumentsOnNextLine = i & 2;
6443 
6444     Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6445     Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
6446     verifyFormat("Constructor()\n"
6447                  "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6448                  Style);
6449     verifyFormat("Constructor() : a(a), b(b) {}", Style);
6450 
6451     Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
6452     verifyFormat("Constructor()\n"
6453                  "    : aaaaaaaaaaaaaaaaaaaa(a)\n"
6454                  "    , bbbbbbbbbbbbbbbbbbbbb(b) {}",
6455                  Style);
6456     verifyFormat("Constructor() : a(a), b(b) {}", Style);
6457 
6458     Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
6459     Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6460     verifyFormat("Constructor()\n"
6461                  "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6462                  Style);
6463 
6464     Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
6465     verifyFormat("Constructor()\n"
6466                  "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
6467                  "      bbbbbbbbbbbbbbbbbbbbb(b) {}",
6468                  Style);
6469 
6470     Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
6471     Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6472     verifyFormat("Constructor() :\n"
6473                  "    aaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6474                  Style);
6475 
6476     Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
6477     verifyFormat("Constructor() :\n"
6478                  "    aaaaaaaaaaaaaaaaaa(a),\n"
6479                  "    bbbbbbbbbbbbbbbbbbbbb(b) {}",
6480                  Style);
6481   }
6482 
6483   // Test interactions between AllowAllParametersOfDeclarationOnNextLine and
6484   // AllowAllConstructorInitializersOnNextLine in all
6485   // BreakConstructorInitializers modes
6486   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
6487   Style.AllowAllParametersOfDeclarationOnNextLine = true;
6488   verifyFormat("SomeClassWithALongName::Constructor(\n"
6489                "    int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n"
6490                "    : aaaaaaaaaaaaaaaaaaaa(a)\n"
6491                "    , bbbbbbbbbbbbbbbbbbbbb(b) {}",
6492                Style);
6493 
6494   Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6495   verifyFormat("SomeClassWithALongName::Constructor(\n"
6496                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6497                "    int bbbbbbbbbbbbb,\n"
6498                "    int cccccccccccccccc)\n"
6499                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6500                Style);
6501 
6502   Style.AllowAllParametersOfDeclarationOnNextLine = false;
6503   Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
6504   verifyFormat("SomeClassWithALongName::Constructor(\n"
6505                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6506                "    int bbbbbbbbbbbbb)\n"
6507                "    : aaaaaaaaaaaaaaaaaaaa(a)\n"
6508                "    , bbbbbbbbbbbbbbbbbbbbb(b) {}",
6509                Style);
6510 
6511   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
6512 
6513   Style.AllowAllParametersOfDeclarationOnNextLine = true;
6514   verifyFormat("SomeClassWithALongName::Constructor(\n"
6515                "    int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n"
6516                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
6517                "      bbbbbbbbbbbbbbbbbbbbb(b) {}",
6518                Style);
6519 
6520   Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6521   verifyFormat("SomeClassWithALongName::Constructor(\n"
6522                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6523                "    int bbbbbbbbbbbbb,\n"
6524                "    int cccccccccccccccc)\n"
6525                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6526                Style);
6527 
6528   Style.AllowAllParametersOfDeclarationOnNextLine = false;
6529   Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
6530   verifyFormat("SomeClassWithALongName::Constructor(\n"
6531                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6532                "    int bbbbbbbbbbbbb)\n"
6533                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
6534                "      bbbbbbbbbbbbbbbbbbbbb(b) {}",
6535                Style);
6536 
6537   Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
6538   Style.AllowAllParametersOfDeclarationOnNextLine = true;
6539   verifyFormat("SomeClassWithALongName::Constructor(\n"
6540                "    int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb) :\n"
6541                "    aaaaaaaaaaaaaaaaaaaa(a),\n"
6542                "    bbbbbbbbbbbbbbbbbbbbb(b) {}",
6543                Style);
6544 
6545   Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6546   verifyFormat("SomeClassWithALongName::Constructor(\n"
6547                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6548                "    int bbbbbbbbbbbbb,\n"
6549                "    int cccccccccccccccc) :\n"
6550                "    aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6551                Style);
6552 
6553   Style.AllowAllParametersOfDeclarationOnNextLine = false;
6554   Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
6555   verifyFormat("SomeClassWithALongName::Constructor(\n"
6556                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6557                "    int bbbbbbbbbbbbb) :\n"
6558                "    aaaaaaaaaaaaaaaaaaaa(a),\n"
6559                "    bbbbbbbbbbbbbbbbbbbbb(b) {}",
6560                Style);
6561 }
6562 
6563 TEST_F(FormatTest, AllowAllArgumentsOnNextLine) {
6564   FormatStyle Style = getLLVMStyleWithColumns(60);
6565   Style.BinPackArguments = false;
6566   for (int i = 0; i < 4; ++i) {
6567     // Test all combinations of parameters that should not have an effect.
6568     Style.AllowAllParametersOfDeclarationOnNextLine = i & 1;
6569     Style.PackConstructorInitializers =
6570         i & 2 ? FormatStyle::PCIS_BinPack : FormatStyle::PCIS_Never;
6571 
6572     Style.AllowAllArgumentsOnNextLine = true;
6573     verifyFormat("void foo() {\n"
6574                  "  FunctionCallWithReallyLongName(\n"
6575                  "      aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb);\n"
6576                  "}",
6577                  Style);
6578     Style.AllowAllArgumentsOnNextLine = false;
6579     verifyFormat("void foo() {\n"
6580                  "  FunctionCallWithReallyLongName(\n"
6581                  "      aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6582                  "      bbbbbbbbbbbb);\n"
6583                  "}",
6584                  Style);
6585 
6586     Style.AllowAllArgumentsOnNextLine = true;
6587     verifyFormat("void foo() {\n"
6588                  "  auto VariableWithReallyLongName = {\n"
6589                  "      aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb};\n"
6590                  "}",
6591                  Style);
6592     Style.AllowAllArgumentsOnNextLine = false;
6593     verifyFormat("void foo() {\n"
6594                  "  auto VariableWithReallyLongName = {\n"
6595                  "      aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6596                  "      bbbbbbbbbbbb};\n"
6597                  "}",
6598                  Style);
6599   }
6600 
6601   // This parameter should not affect declarations.
6602   Style.BinPackParameters = false;
6603   Style.AllowAllArgumentsOnNextLine = false;
6604   Style.AllowAllParametersOfDeclarationOnNextLine = true;
6605   verifyFormat("void FunctionCallWithReallyLongName(\n"
6606                "    int aaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbb);",
6607                Style);
6608   Style.AllowAllParametersOfDeclarationOnNextLine = false;
6609   verifyFormat("void FunctionCallWithReallyLongName(\n"
6610                "    int aaaaaaaaaaaaaaaaaaaaaaa,\n"
6611                "    int bbbbbbbbbbbb);",
6612                Style);
6613 }
6614 
6615 TEST_F(FormatTest, AllowAllArgumentsOnNextLineDontAlign) {
6616   // Check that AllowAllArgumentsOnNextLine is respected for both BAS_DontAlign
6617   // and BAS_Align.
6618   FormatStyle Style = getLLVMStyleWithColumns(35);
6619   StringRef Input = "functionCall(paramA, paramB, paramC);\n"
6620                     "void functionDecl(int A, int B, int C);";
6621   Style.AllowAllArgumentsOnNextLine = false;
6622   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
6623   EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n"
6624                       "    paramC);\n"
6625                       "void functionDecl(int A, int B,\n"
6626                       "    int C);"),
6627             format(Input, Style));
6628   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
6629   EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n"
6630                       "             paramC);\n"
6631                       "void functionDecl(int A, int B,\n"
6632                       "                  int C);"),
6633             format(Input, Style));
6634   // However, BAS_AlwaysBreak should take precedence over
6635   // AllowAllArgumentsOnNextLine.
6636   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
6637   EXPECT_EQ(StringRef("functionCall(\n"
6638                       "    paramA, paramB, paramC);\n"
6639                       "void functionDecl(\n"
6640                       "    int A, int B, int C);"),
6641             format(Input, Style));
6642 
6643   // When AllowAllArgumentsOnNextLine is set, we prefer breaking before the
6644   // first argument.
6645   Style.AllowAllArgumentsOnNextLine = true;
6646   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
6647   EXPECT_EQ(StringRef("functionCall(\n"
6648                       "    paramA, paramB, paramC);\n"
6649                       "void functionDecl(\n"
6650                       "    int A, int B, int C);"),
6651             format(Input, Style));
6652   // It wouldn't fit on one line with aligned parameters so this setting
6653   // doesn't change anything for BAS_Align.
6654   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
6655   EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n"
6656                       "             paramC);\n"
6657                       "void functionDecl(int A, int B,\n"
6658                       "                  int C);"),
6659             format(Input, Style));
6660   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
6661   EXPECT_EQ(StringRef("functionCall(\n"
6662                       "    paramA, paramB, paramC);\n"
6663                       "void functionDecl(\n"
6664                       "    int A, int B, int C);"),
6665             format(Input, Style));
6666 }
6667 
6668 TEST_F(FormatTest, BreakConstructorInitializersAfterColon) {
6669   FormatStyle Style = getLLVMStyle();
6670   Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
6671 
6672   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
6673   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}",
6674                getStyleWithColumns(Style, 45));
6675   verifyFormat("Constructor() :\n"
6676                "    Initializer(FitsOnTheLine) {}",
6677                getStyleWithColumns(Style, 44));
6678   verifyFormat("Constructor() :\n"
6679                "    Initializer(FitsOnTheLine) {}",
6680                getStyleWithColumns(Style, 43));
6681 
6682   verifyFormat("template <typename T>\n"
6683                "Constructor() : Initializer(FitsOnTheLine) {}",
6684                getStyleWithColumns(Style, 50));
6685   Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6686   verifyFormat(
6687       "SomeClass::Constructor() :\n"
6688       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
6689       Style);
6690 
6691   Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack;
6692   verifyFormat(
6693       "SomeClass::Constructor() :\n"
6694       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
6695       Style);
6696 
6697   verifyFormat(
6698       "SomeClass::Constructor() :\n"
6699       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6700       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
6701       Style);
6702   verifyFormat(
6703       "SomeClass::Constructor() :\n"
6704       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6705       "    aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
6706       Style);
6707   verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6708                "            aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
6709                "    aaaaaaaaaa(aaaaaa) {}",
6710                Style);
6711 
6712   verifyFormat("Constructor() :\n"
6713                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6714                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6715                "                             aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6716                "    aaaaaaaaaaaaaaaaaaaaaaa() {}",
6717                Style);
6718 
6719   verifyFormat("Constructor() :\n"
6720                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6721                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
6722                Style);
6723 
6724   verifyFormat("Constructor(int Parameter = 0) :\n"
6725                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
6726                "    aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}",
6727                Style);
6728   verifyFormat("Constructor() :\n"
6729                "    aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
6730                "}",
6731                getStyleWithColumns(Style, 60));
6732   verifyFormat("Constructor() :\n"
6733                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6734                "        aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}",
6735                Style);
6736 
6737   // Here a line could be saved by splitting the second initializer onto two
6738   // lines, but that is not desirable.
6739   verifyFormat("Constructor() :\n"
6740                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
6741                "    aaaaaaaaaaa(aaaaaaaaaaa),\n"
6742                "    aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
6743                Style);
6744 
6745   FormatStyle OnePerLine = Style;
6746   OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
6747   verifyFormat("SomeClass::Constructor() :\n"
6748                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6749                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6750                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
6751                OnePerLine);
6752   verifyFormat("SomeClass::Constructor() :\n"
6753                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
6754                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6755                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
6756                OnePerLine);
6757   verifyFormat("MyClass::MyClass(int var) :\n"
6758                "    some_var_(var),            // 4 space indent\n"
6759                "    some_other_var_(var + 1) { // lined up\n"
6760                "}",
6761                OnePerLine);
6762   verifyFormat("Constructor() :\n"
6763                "    aaaaa(aaaaaa),\n"
6764                "    aaaaa(aaaaaa),\n"
6765                "    aaaaa(aaaaaa),\n"
6766                "    aaaaa(aaaaaa),\n"
6767                "    aaaaa(aaaaaa) {}",
6768                OnePerLine);
6769   verifyFormat("Constructor() :\n"
6770                "    aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
6771                "          aaaaaaaaaaaaaaaaaaaaaa) {}",
6772                OnePerLine);
6773   OnePerLine.BinPackParameters = false;
6774   verifyFormat("Constructor() :\n"
6775                "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
6776                "        aaaaaaaaaaa().aaa(),\n"
6777                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
6778                OnePerLine);
6779   OnePerLine.ColumnLimit = 60;
6780   verifyFormat("Constructor() :\n"
6781                "    aaaaaaaaaaaaaaaaaaaa(a),\n"
6782                "    bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
6783                OnePerLine);
6784 
6785   EXPECT_EQ("Constructor() :\n"
6786             "    // Comment forcing unwanted break.\n"
6787             "    aaaa(aaaa) {}",
6788             format("Constructor() :\n"
6789                    "    // Comment forcing unwanted break.\n"
6790                    "    aaaa(aaaa) {}",
6791                    Style));
6792 
6793   Style.ColumnLimit = 0;
6794   verifyFormat("SomeClass::Constructor() :\n"
6795                "    a(a) {}",
6796                Style);
6797   verifyFormat("SomeClass::Constructor() noexcept :\n"
6798                "    a(a) {}",
6799                Style);
6800   verifyFormat("SomeClass::Constructor() :\n"
6801                "    a(a), b(b), c(c) {}",
6802                Style);
6803   verifyFormat("SomeClass::Constructor() :\n"
6804                "    a(a) {\n"
6805                "  foo();\n"
6806                "  bar();\n"
6807                "}",
6808                Style);
6809 
6810   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
6811   verifyFormat("SomeClass::Constructor() :\n"
6812                "    a(a), b(b), c(c) {\n"
6813                "}",
6814                Style);
6815   verifyFormat("SomeClass::Constructor() :\n"
6816                "    a(a) {\n"
6817                "}",
6818                Style);
6819 
6820   Style.ColumnLimit = 80;
6821   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
6822   Style.ConstructorInitializerIndentWidth = 2;
6823   verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}", Style);
6824   verifyFormat("SomeClass::Constructor() :\n"
6825                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6826                "  bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}",
6827                Style);
6828 
6829   // `ConstructorInitializerIndentWidth` actually applies to InheritanceList as
6830   // well
6831   Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
6832   verifyFormat(
6833       "class SomeClass\n"
6834       "  : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6835       "    public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
6836       Style);
6837   Style.BreakInheritanceList = FormatStyle::BILS_BeforeComma;
6838   verifyFormat(
6839       "class SomeClass\n"
6840       "  : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6841       "  , public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
6842       Style);
6843   Style.BreakInheritanceList = FormatStyle::BILS_AfterColon;
6844   verifyFormat(
6845       "class SomeClass :\n"
6846       "  public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6847       "  public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
6848       Style);
6849   Style.BreakInheritanceList = FormatStyle::BILS_AfterComma;
6850   verifyFormat(
6851       "class SomeClass\n"
6852       "  : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6853       "    public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
6854       Style);
6855 }
6856 
6857 #ifndef EXPENSIVE_CHECKS
6858 // Expensive checks enables libstdc++ checking which includes validating the
6859 // state of ranges used in std::priority_queue - this blows out the
6860 // runtime/scalability of the function and makes this test unacceptably slow.
6861 TEST_F(FormatTest, MemoizationTests) {
6862   // This breaks if the memoization lookup does not take \c Indent and
6863   // \c LastSpace into account.
6864   verifyFormat(
6865       "extern CFRunLoopTimerRef\n"
6866       "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
6867       "                     CFTimeInterval interval, CFOptionFlags flags,\n"
6868       "                     CFIndex order, CFRunLoopTimerCallBack callout,\n"
6869       "                     CFRunLoopTimerContext *context) {}");
6870 
6871   // Deep nesting somewhat works around our memoization.
6872   verifyFormat(
6873       "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
6874       "    aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
6875       "        aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
6876       "            aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
6877       "                aaaaa())))))))))))))))))))))))))))))))))))))));",
6878       getLLVMStyleWithColumns(65));
6879   verifyFormat(
6880       "aaaaa(\n"
6881       "    aaaaa,\n"
6882       "    aaaaa(\n"
6883       "        aaaaa,\n"
6884       "        aaaaa(\n"
6885       "            aaaaa,\n"
6886       "            aaaaa(\n"
6887       "                aaaaa,\n"
6888       "                aaaaa(\n"
6889       "                    aaaaa,\n"
6890       "                    aaaaa(\n"
6891       "                        aaaaa,\n"
6892       "                        aaaaa(\n"
6893       "                            aaaaa,\n"
6894       "                            aaaaa(\n"
6895       "                                aaaaa,\n"
6896       "                                aaaaa(\n"
6897       "                                    aaaaa,\n"
6898       "                                    aaaaa(\n"
6899       "                                        aaaaa,\n"
6900       "                                        aaaaa(\n"
6901       "                                            aaaaa,\n"
6902       "                                            aaaaa(\n"
6903       "                                                aaaaa,\n"
6904       "                                                aaaaa))))))))))));",
6905       getLLVMStyleWithColumns(65));
6906   verifyFormat(
6907       "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"
6908       "                                  a),\n"
6909       "                                a),\n"
6910       "                              a),\n"
6911       "                            a),\n"
6912       "                          a),\n"
6913       "                        a),\n"
6914       "                      a),\n"
6915       "                    a),\n"
6916       "                  a),\n"
6917       "                a),\n"
6918       "              a),\n"
6919       "            a),\n"
6920       "          a),\n"
6921       "        a),\n"
6922       "      a),\n"
6923       "    a),\n"
6924       "  a)",
6925       getLLVMStyleWithColumns(65));
6926 
6927   // This test takes VERY long when memoization is broken.
6928   FormatStyle OnePerLine = getLLVMStyle();
6929   OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6930   OnePerLine.BinPackParameters = false;
6931   std::string input = "Constructor()\n"
6932                       "    : aaaa(a,\n";
6933   for (unsigned i = 0, e = 80; i != e; ++i) {
6934     input += "           a,\n";
6935   }
6936   input += "           a) {}";
6937   verifyFormat(input, OnePerLine);
6938 }
6939 #endif
6940 
6941 TEST_F(FormatTest, BreaksAsHighAsPossible) {
6942   verifyFormat(
6943       "void f() {\n"
6944       "  if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
6945       "      (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
6946       "    f();\n"
6947       "}");
6948   verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
6949                "    Intervals[i - 1].getRange().getLast()) {\n}");
6950 }
6951 
6952 TEST_F(FormatTest, BreaksFunctionDeclarations) {
6953   // Principially, we break function declarations in a certain order:
6954   // 1) break amongst arguments.
6955   verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
6956                "                              Cccccccccccccc cccccccccccccc);");
6957   verifyFormat("template <class TemplateIt>\n"
6958                "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
6959                "                            TemplateIt *stop) {}");
6960 
6961   // 2) break after return type.
6962   verifyFormat(
6963       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6964       "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);",
6965       getGoogleStyle());
6966 
6967   // 3) break after (.
6968   verifyFormat(
6969       "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
6970       "    Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);",
6971       getGoogleStyle());
6972 
6973   // 4) break before after nested name specifiers.
6974   verifyFormat(
6975       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6976       "SomeClasssssssssssssssssssssssssssssssssssssss::\n"
6977       "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);",
6978       getGoogleStyle());
6979 
6980   // However, there are exceptions, if a sufficient amount of lines can be
6981   // saved.
6982   // FIXME: The precise cut-offs wrt. the number of saved lines might need some
6983   // more adjusting.
6984   verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
6985                "                                  Cccccccccccccc cccccccccc,\n"
6986                "                                  Cccccccccccccc cccccccccc,\n"
6987                "                                  Cccccccccccccc cccccccccc,\n"
6988                "                                  Cccccccccccccc cccccccccc);");
6989   verifyFormat(
6990       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6991       "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
6992       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
6993       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);",
6994       getGoogleStyle());
6995   verifyFormat(
6996       "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
6997       "                                          Cccccccccccccc cccccccccc,\n"
6998       "                                          Cccccccccccccc cccccccccc,\n"
6999       "                                          Cccccccccccccc cccccccccc,\n"
7000       "                                          Cccccccccccccc cccccccccc,\n"
7001       "                                          Cccccccccccccc cccccccccc,\n"
7002       "                                          Cccccccccccccc cccccccccc);");
7003   verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
7004                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
7005                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
7006                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
7007                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
7008 
7009   // Break after multi-line parameters.
7010   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7011                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7012                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7013                "    bbbb bbbb);");
7014   verifyFormat("void SomeLoooooooooooongFunction(\n"
7015                "    std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
7016                "        aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7017                "    int bbbbbbbbbbbbb);");
7018 
7019   // Treat overloaded operators like other functions.
7020   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
7021                "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
7022   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
7023                "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
7024   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
7025                "operator<<(const SomeLooooooooooooooooooooooooogType &other);");
7026   verifyGoogleFormat(
7027       "SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
7028       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
7029   verifyGoogleFormat(
7030       "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
7031       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
7032   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7033                "    int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
7034   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
7035                "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
7036   verifyGoogleFormat(
7037       "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n"
7038       "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7039       "    bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}");
7040   verifyGoogleFormat("template <typename T>\n"
7041                      "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7042                      "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n"
7043                      "    aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);");
7044 
7045   FormatStyle Style = getLLVMStyle();
7046   Style.PointerAlignment = FormatStyle::PAS_Left;
7047   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7048                "    aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}",
7049                Style);
7050   verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
7051                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
7052                Style);
7053 }
7054 
7055 TEST_F(FormatTest, DontBreakBeforeQualifiedOperator) {
7056   // Regression test for https://bugs.llvm.org/show_bug.cgi?id=40516:
7057   // Prefer keeping `::` followed by `operator` together.
7058   EXPECT_EQ("const aaaa::bbbbbbb &\n"
7059             "ccccccccc::operator++() {\n"
7060             "  stuff();\n"
7061             "}",
7062             format("const aaaa::bbbbbbb\n"
7063                    "&ccccccccc::operator++() { stuff(); }",
7064                    getLLVMStyleWithColumns(40)));
7065 }
7066 
7067 TEST_F(FormatTest, TrailingReturnType) {
7068   verifyFormat("auto foo() -> int;\n");
7069   // correct trailing return type spacing
7070   verifyFormat("auto operator->() -> int;\n");
7071   verifyFormat("auto operator++(int) -> int;\n");
7072 
7073   verifyFormat("struct S {\n"
7074                "  auto bar() const -> int;\n"
7075                "};");
7076   verifyFormat("template <size_t Order, typename T>\n"
7077                "auto load_img(const std::string &filename)\n"
7078                "    -> alias::tensor<Order, T, mem::tag::cpu> {}");
7079   verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n"
7080                "    -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}");
7081   verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}");
7082   verifyFormat("template <typename T>\n"
7083                "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n"
7084                "    -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());");
7085 
7086   // Not trailing return types.
7087   verifyFormat("void f() { auto a = b->c(); }");
7088   verifyFormat("auto a = p->foo();");
7089   verifyFormat("int a = p->foo();");
7090   verifyFormat("auto lmbd = [] NOEXCEPT -> int { return 0; };");
7091 }
7092 
7093 TEST_F(FormatTest, DeductionGuides) {
7094   verifyFormat("template <class T> A(const T &, const T &) -> A<T &>;");
7095   verifyFormat("template <class T> explicit A(T &, T &&) -> A<T>;");
7096   verifyFormat("template <class... Ts> S(Ts...) -> S<Ts...>;");
7097   verifyFormat(
7098       "template <class... T>\n"
7099       "array(T &&...t) -> array<std::common_type_t<T...>, sizeof...(T)>;");
7100   verifyFormat("template <class T> A() -> A<decltype(p->foo<3>())>;");
7101   verifyFormat("template <class T> A() -> A<decltype(foo<traits<1>>)>;");
7102   verifyFormat("template <class T> A() -> A<sizeof(p->foo<1>)>;");
7103   verifyFormat("template <class T> A() -> A<(3 < 2)>;");
7104   verifyFormat("template <class T> A() -> A<((3) < (2))>;");
7105   verifyFormat("template <class T> x() -> x<1>;");
7106   verifyFormat("template <class T> explicit x(T &) -> x<1>;");
7107 
7108   // Ensure not deduction guides.
7109   verifyFormat("c()->f<int>();");
7110   verifyFormat("x()->foo<1>;");
7111   verifyFormat("x = p->foo<3>();");
7112   verifyFormat("x()->x<1>();");
7113   verifyFormat("x()->x<1>;");
7114 }
7115 
7116 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
7117   // Avoid breaking before trailing 'const' or other trailing annotations, if
7118   // they are not function-like.
7119   FormatStyle Style = getGoogleStyleWithColumns(47);
7120   verifyFormat("void someLongFunction(\n"
7121                "    int someLoooooooooooooongParameter) const {\n}",
7122                getLLVMStyleWithColumns(47));
7123   verifyFormat("LoooooongReturnType\n"
7124                "someLoooooooongFunction() const {}",
7125                getLLVMStyleWithColumns(47));
7126   verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
7127                "    const {}",
7128                Style);
7129   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
7130                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
7131   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
7132                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
7133   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
7134                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
7135   verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
7136                "                   aaaaaaaaaaa aaaaa) const override;");
7137   verifyGoogleFormat(
7138       "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7139       "    const override;");
7140 
7141   // Even if the first parameter has to be wrapped.
7142   verifyFormat("void someLongFunction(\n"
7143                "    int someLongParameter) const {}",
7144                getLLVMStyleWithColumns(46));
7145   verifyFormat("void someLongFunction(\n"
7146                "    int someLongParameter) const {}",
7147                Style);
7148   verifyFormat("void someLongFunction(\n"
7149                "    int someLongParameter) override {}",
7150                Style);
7151   verifyFormat("void someLongFunction(\n"
7152                "    int someLongParameter) OVERRIDE {}",
7153                Style);
7154   verifyFormat("void someLongFunction(\n"
7155                "    int someLongParameter) final {}",
7156                Style);
7157   verifyFormat("void someLongFunction(\n"
7158                "    int someLongParameter) FINAL {}",
7159                Style);
7160   verifyFormat("void someLongFunction(\n"
7161                "    int parameter) const override {}",
7162                Style);
7163 
7164   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
7165   verifyFormat("void someLongFunction(\n"
7166                "    int someLongParameter) const\n"
7167                "{\n"
7168                "}",
7169                Style);
7170 
7171   Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
7172   verifyFormat("void someLongFunction(\n"
7173                "    int someLongParameter) const\n"
7174                "  {\n"
7175                "  }",
7176                Style);
7177 
7178   // Unless these are unknown annotations.
7179   verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
7180                "                  aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7181                "    LONG_AND_UGLY_ANNOTATION;");
7182 
7183   // Breaking before function-like trailing annotations is fine to keep them
7184   // close to their arguments.
7185   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7186                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
7187   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
7188                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
7189   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
7190                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
7191   verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n"
7192                      "    AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);");
7193   verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});");
7194 
7195   verifyFormat(
7196       "void aaaaaaaaaaaaaaaaaa()\n"
7197       "    __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
7198       "                   aaaaaaaaaaaaaaaaaaaaaaaaa));");
7199   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7200                "    __attribute__((unused));");
7201   verifyGoogleFormat(
7202       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7203       "    GUARDED_BY(aaaaaaaaaaaa);");
7204   verifyGoogleFormat(
7205       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7206       "    GUARDED_BY(aaaaaaaaaaaa);");
7207   verifyGoogleFormat(
7208       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
7209       "    aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
7210   verifyGoogleFormat(
7211       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
7212       "    aaaaaaaaaaaaaaaaaaaaaaaaa;");
7213 }
7214 
7215 TEST_F(FormatTest, FunctionAnnotations) {
7216   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
7217                "int OldFunction(const string &parameter) {}");
7218   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
7219                "string OldFunction(const string &parameter) {}");
7220   verifyFormat("template <typename T>\n"
7221                "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
7222                "string OldFunction(const string &parameter) {}");
7223 
7224   // Not function annotations.
7225   verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7226                "                << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
7227   verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n"
7228                "       ThisIsATestWithAReallyReallyReallyReallyLongName) {}");
7229   verifyFormat("MACRO(abc).function() // wrap\n"
7230                "    << abc;");
7231   verifyFormat("MACRO(abc)->function() // wrap\n"
7232                "    << abc;");
7233   verifyFormat("MACRO(abc)::function() // wrap\n"
7234                "    << abc;");
7235 }
7236 
7237 TEST_F(FormatTest, BreaksDesireably) {
7238   verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
7239                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
7240                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
7241   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7242                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
7243                "}");
7244 
7245   verifyFormat(
7246       "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7247       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
7248 
7249   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7250                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7251                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
7252 
7253   verifyFormat(
7254       "aaaaaaaa(aaaaaaaaaaaaa,\n"
7255       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7256       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
7257       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7258       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
7259 
7260   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
7261                "    (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7262 
7263   verifyFormat(
7264       "void f() {\n"
7265       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
7266       "                                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
7267       "}");
7268   verifyFormat(
7269       "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7270       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
7271   verifyFormat(
7272       "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7273       "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
7274   verifyFormat(
7275       "aaaaaa(aaa,\n"
7276       "       new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7277       "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7278       "       aaaa);");
7279   verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7280                "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7281                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7282 
7283   // Indent consistently independent of call expression and unary operator.
7284   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
7285                "    dddddddddddddddddddddddddddddd));");
7286   verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
7287                "    dddddddddddddddddddddddddddddd));");
7288   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
7289                "    dddddddddddddddddddddddddddddd));");
7290 
7291   // This test case breaks on an incorrect memoization, i.e. an optimization not
7292   // taking into account the StopAt value.
7293   verifyFormat(
7294       "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
7295       "       aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
7296       "       aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
7297       "       (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7298 
7299   verifyFormat("{\n  {\n    {\n"
7300                "      Annotation.SpaceRequiredBefore =\n"
7301                "          Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
7302                "          Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
7303                "    }\n  }\n}");
7304 
7305   // Break on an outer level if there was a break on an inner level.
7306   EXPECT_EQ("f(g(h(a, // comment\n"
7307             "      b, c),\n"
7308             "    d, e),\n"
7309             "  x, y);",
7310             format("f(g(h(a, // comment\n"
7311                    "    b, c), d, e), x, y);"));
7312 
7313   // Prefer breaking similar line breaks.
7314   verifyFormat(
7315       "const int kTrackingOptions = NSTrackingMouseMoved |\n"
7316       "                             NSTrackingMouseEnteredAndExited |\n"
7317       "                             NSTrackingActiveAlways;");
7318 }
7319 
7320 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) {
7321   FormatStyle NoBinPacking = getGoogleStyle();
7322   NoBinPacking.BinPackParameters = false;
7323   NoBinPacking.BinPackArguments = true;
7324   verifyFormat("void f() {\n"
7325                "  f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n"
7326                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
7327                "}",
7328                NoBinPacking);
7329   verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n"
7330                "       int aaaaaaaaaaaaaaaaaaaa,\n"
7331                "       int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
7332                NoBinPacking);
7333 
7334   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
7335   verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7336                "                        vector<int> bbbbbbbbbbbbbbb);",
7337                NoBinPacking);
7338   // FIXME: This behavior difference is probably not wanted. However, currently
7339   // we cannot distinguish BreakBeforeParameter being set because of the wrapped
7340   // template arguments from BreakBeforeParameter being set because of the
7341   // one-per-line formatting.
7342   verifyFormat(
7343       "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n"
7344       "                                             aaaaaaaaaa> aaaaaaaaaa);",
7345       NoBinPacking);
7346   verifyFormat(
7347       "void fffffffffff(\n"
7348       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n"
7349       "        aaaaaaaaaa);");
7350 }
7351 
7352 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
7353   FormatStyle NoBinPacking = getGoogleStyle();
7354   NoBinPacking.BinPackParameters = false;
7355   NoBinPacking.BinPackArguments = false;
7356   verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
7357                "  aaaaaaaaaaaaaaaaaaaa,\n"
7358                "  aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
7359                NoBinPacking);
7360   verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
7361                "        aaaaaaaaaaaaa,\n"
7362                "        aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
7363                NoBinPacking);
7364   verifyFormat(
7365       "aaaaaaaa(aaaaaaaaaaaaa,\n"
7366       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7367       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
7368       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7369       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
7370       NoBinPacking);
7371   verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
7372                "    .aaaaaaaaaaaaaaaaaa();",
7373                NoBinPacking);
7374   verifyFormat("void f() {\n"
7375                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7376                "      aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n"
7377                "}",
7378                NoBinPacking);
7379 
7380   verifyFormat(
7381       "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7382       "             aaaaaaaaaaaa,\n"
7383       "             aaaaaaaaaaaa);",
7384       NoBinPacking);
7385   verifyFormat(
7386       "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
7387       "                               ddddddddddddddddddddddddddddd),\n"
7388       "             test);",
7389       NoBinPacking);
7390 
7391   verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
7392                "            aaaaaaaaaaaaaaaaaaaaaaa,\n"
7393                "            aaaaaaaaaaaaaaaaaaaaaaa>\n"
7394                "    aaaaaaaaaaaaaaaaaa;",
7395                NoBinPacking);
7396   verifyFormat("a(\"a\"\n"
7397                "  \"a\",\n"
7398                "  a);");
7399 
7400   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
7401   verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
7402                "                aaaaaaaaa,\n"
7403                "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7404                NoBinPacking);
7405   verifyFormat(
7406       "void f() {\n"
7407       "  aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
7408       "      .aaaaaaa();\n"
7409       "}",
7410       NoBinPacking);
7411   verifyFormat(
7412       "template <class SomeType, class SomeOtherType>\n"
7413       "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
7414       NoBinPacking);
7415 }
7416 
7417 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
7418   FormatStyle Style = getLLVMStyleWithColumns(15);
7419   Style.ExperimentalAutoDetectBinPacking = true;
7420   EXPECT_EQ("aaa(aaaa,\n"
7421             "    aaaa,\n"
7422             "    aaaa);\n"
7423             "aaa(aaaa,\n"
7424             "    aaaa,\n"
7425             "    aaaa);",
7426             format("aaa(aaaa,\n" // one-per-line
7427                    "  aaaa,\n"
7428                    "    aaaa  );\n"
7429                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
7430                    Style));
7431   EXPECT_EQ("aaa(aaaa, aaaa,\n"
7432             "    aaaa);\n"
7433             "aaa(aaaa, aaaa,\n"
7434             "    aaaa);",
7435             format("aaa(aaaa,  aaaa,\n" // bin-packed
7436                    "    aaaa  );\n"
7437                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
7438                    Style));
7439 }
7440 
7441 TEST_F(FormatTest, FormatsBuilderPattern) {
7442   verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n"
7443                "    .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
7444                "    .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
7445                "    .StartsWith(\".init\", ORDER_INIT)\n"
7446                "    .StartsWith(\".fini\", ORDER_FINI)\n"
7447                "    .StartsWith(\".hash\", ORDER_HASH)\n"
7448                "    .Default(ORDER_TEXT);\n");
7449 
7450   verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
7451                "       aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
7452   verifyFormat("aaaaaaa->aaaaaaa\n"
7453                "    ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7454                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7455                "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
7456   verifyFormat(
7457       "aaaaaaa->aaaaaaa\n"
7458       "    ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7459       "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
7460   verifyFormat(
7461       "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
7462       "    aaaaaaaaaaaaaa);");
7463   verifyFormat(
7464       "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
7465       "    aaaaaa->aaaaaaaaaaaa()\n"
7466       "        ->aaaaaaaaaaaaaaaa(\n"
7467       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7468       "        ->aaaaaaaaaaaaaaaaa();");
7469   verifyGoogleFormat(
7470       "void f() {\n"
7471       "  someo->Add((new util::filetools::Handler(dir))\n"
7472       "                 ->OnEvent1(NewPermanentCallback(\n"
7473       "                     this, &HandlerHolderClass::EventHandlerCBA))\n"
7474       "                 ->OnEvent2(NewPermanentCallback(\n"
7475       "                     this, &HandlerHolderClass::EventHandlerCBB))\n"
7476       "                 ->OnEvent3(NewPermanentCallback(\n"
7477       "                     this, &HandlerHolderClass::EventHandlerCBC))\n"
7478       "                 ->OnEvent5(NewPermanentCallback(\n"
7479       "                     this, &HandlerHolderClass::EventHandlerCBD))\n"
7480       "                 ->OnEvent6(NewPermanentCallback(\n"
7481       "                     this, &HandlerHolderClass::EventHandlerCBE)));\n"
7482       "}");
7483 
7484   verifyFormat(
7485       "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
7486   verifyFormat("aaaaaaaaaaaaaaa()\n"
7487                "    .aaaaaaaaaaaaaaa()\n"
7488                "    .aaaaaaaaaaaaaaa()\n"
7489                "    .aaaaaaaaaaaaaaa()\n"
7490                "    .aaaaaaaaaaaaaaa();");
7491   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
7492                "    .aaaaaaaaaaaaaaa()\n"
7493                "    .aaaaaaaaaaaaaaa()\n"
7494                "    .aaaaaaaaaaaaaaa();");
7495   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
7496                "    .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
7497                "    .aaaaaaaaaaaaaaa();");
7498   verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
7499                "    ->aaaaaaaaaaaaaae(0)\n"
7500                "    ->aaaaaaaaaaaaaaa();");
7501 
7502   // Don't linewrap after very short segments.
7503   verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7504                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7505                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
7506   verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7507                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7508                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
7509   verifyFormat("aaa()\n"
7510                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7511                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7512                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
7513 
7514   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
7515                "    .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7516                "    .has<bbbbbbbbbbbbbbbbbbbbb>();");
7517   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
7518                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
7519                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
7520 
7521   // Prefer not to break after empty parentheses.
7522   verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
7523                "    First->LastNewlineOffset);");
7524 
7525   // Prefer not to create "hanging" indents.
7526   verifyFormat(
7527       "return !soooooooooooooome_map\n"
7528       "            .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7529       "            .second;");
7530   verifyFormat(
7531       "return aaaaaaaaaaaaaaaa\n"
7532       "    .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n"
7533       "    .aaaa(aaaaaaaaaaaaaa);");
7534   // No hanging indent here.
7535   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n"
7536                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7537   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n"
7538                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7539   verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
7540                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7541                getLLVMStyleWithColumns(60));
7542   verifyFormat("aaaaaaaaaaaaaaaaaa\n"
7543                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
7544                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7545                getLLVMStyleWithColumns(59));
7546   verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7547                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7548                "    .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7549 
7550   // Dont break if only closing statements before member call
7551   verifyFormat("test() {\n"
7552                "  ([]() -> {\n"
7553                "    int b = 32;\n"
7554                "    return 3;\n"
7555                "  }).foo();\n"
7556                "}");
7557   verifyFormat("test() {\n"
7558                "  (\n"
7559                "      []() -> {\n"
7560                "        int b = 32;\n"
7561                "        return 3;\n"
7562                "      },\n"
7563                "      foo, bar)\n"
7564                "      .foo();\n"
7565                "}");
7566   verifyFormat("test() {\n"
7567                "  ([]() -> {\n"
7568                "    int b = 32;\n"
7569                "    return 3;\n"
7570                "  })\n"
7571                "      .foo()\n"
7572                "      .bar();\n"
7573                "}");
7574   verifyFormat("test() {\n"
7575                "  ([]() -> {\n"
7576                "    int b = 32;\n"
7577                "    return 3;\n"
7578                "  })\n"
7579                "      .foo(\"aaaaaaaaaaaaaaaaa\"\n"
7580                "           \"bbbb\");\n"
7581                "}",
7582                getLLVMStyleWithColumns(30));
7583 }
7584 
7585 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
7586   verifyFormat(
7587       "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
7588       "    bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
7589   verifyFormat(
7590       "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n"
7591       "    bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}");
7592 
7593   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
7594                "    ccccccccccccccccccccccccc) {\n}");
7595   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n"
7596                "    ccccccccccccccccccccccccc) {\n}");
7597 
7598   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
7599                "    ccccccccccccccccccccccccc) {\n}");
7600   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n"
7601                "    ccccccccccccccccccccccccc) {\n}");
7602 
7603   verifyFormat(
7604       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
7605       "    ccccccccccccccccccccccccc) {\n}");
7606   verifyFormat(
7607       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n"
7608       "    ccccccccccccccccccccccccc) {\n}");
7609 
7610   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
7611                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
7612                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
7613                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
7614   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n"
7615                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n"
7616                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n"
7617                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
7618 
7619   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
7620                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
7621                "    aaaaaaaaaaaaaaa != aa) {\n}");
7622   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n"
7623                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n"
7624                "    aaaaaaaaaaaaaaa != aa) {\n}");
7625 }
7626 
7627 TEST_F(FormatTest, BreaksAfterAssignments) {
7628   verifyFormat(
7629       "unsigned Cost =\n"
7630       "    TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
7631       "                        SI->getPointerAddressSpaceee());\n");
7632   verifyFormat(
7633       "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
7634       "    Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
7635 
7636   verifyFormat(
7637       "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
7638       "    aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
7639   verifyFormat("unsigned OriginalStartColumn =\n"
7640                "    SourceMgr.getSpellingColumnNumber(\n"
7641                "        Current.FormatTok.getStartOfNonWhitespace()) -\n"
7642                "    1;");
7643 }
7644 
7645 TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) {
7646   FormatStyle Style = getLLVMStyle();
7647   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
7648                "    bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;",
7649                Style);
7650 
7651   Style.PenaltyBreakAssignment = 20;
7652   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
7653                "                                 cccccccccccccccccccccccccc;",
7654                Style);
7655 }
7656 
7657 TEST_F(FormatTest, AlignsAfterAssignments) {
7658   verifyFormat(
7659       "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7660       "             aaaaaaaaaaaaaaaaaaaaaaaaa;");
7661   verifyFormat(
7662       "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7663       "          aaaaaaaaaaaaaaaaaaaaaaaaa;");
7664   verifyFormat(
7665       "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7666       "           aaaaaaaaaaaaaaaaaaaaaaaaa;");
7667   verifyFormat(
7668       "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7669       "              aaaaaaaaaaaaaaaaaaaaaaaaa);");
7670   verifyFormat(
7671       "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
7672       "                                            aaaaaaaaaaaaaaaaaaaaaaaa +\n"
7673       "                                            aaaaaaaaaaaaaaaaaaaaaaaa;");
7674 }
7675 
7676 TEST_F(FormatTest, AlignsAfterReturn) {
7677   verifyFormat(
7678       "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7679       "       aaaaaaaaaaaaaaaaaaaaaaaaa;");
7680   verifyFormat(
7681       "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7682       "        aaaaaaaaaaaaaaaaaaaaaaaaa);");
7683   verifyFormat(
7684       "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
7685       "       aaaaaaaaaaaaaaaaaaaaaa();");
7686   verifyFormat(
7687       "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
7688       "        aaaaaaaaaaaaaaaaaaaaaa());");
7689   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7690                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7691   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7692                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
7693                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
7694   verifyFormat("return\n"
7695                "    // true if code is one of a or b.\n"
7696                "    code == a || code == b;");
7697 }
7698 
7699 TEST_F(FormatTest, AlignsAfterOpenBracket) {
7700   verifyFormat(
7701       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
7702       "                                                aaaaaaaaa aaaaaaa) {}");
7703   verifyFormat(
7704       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
7705       "                                               aaaaaaaaaaa aaaaaaaaa);");
7706   verifyFormat(
7707       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
7708       "                                             aaaaaaaaaaaaaaaaaaaaa));");
7709   FormatStyle Style = getLLVMStyle();
7710   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
7711   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7712                "    aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}",
7713                Style);
7714   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
7715                "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);",
7716                Style);
7717   verifyFormat("SomeLongVariableName->someFunction(\n"
7718                "    foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));",
7719                Style);
7720   verifyFormat(
7721       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
7722       "    aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
7723       Style);
7724   verifyFormat(
7725       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
7726       "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7727       Style);
7728   verifyFormat(
7729       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
7730       "    aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
7731       Style);
7732 
7733   verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n"
7734                "    ccccccc(aaaaaaaaaaaaaaaaa,         //\n"
7735                "        b));",
7736                Style);
7737 
7738   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
7739   Style.BinPackArguments = false;
7740   Style.BinPackParameters = false;
7741   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7742                "    aaaaaaaaaaa aaaaaaaa,\n"
7743                "    aaaaaaaaa aaaaaaa,\n"
7744                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
7745                Style);
7746   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
7747                "    aaaaaaaaaaa aaaaaaaaa,\n"
7748                "    aaaaaaaaaaa aaaaaaaaa,\n"
7749                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7750                Style);
7751   verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n"
7752                "    aaaaaaaaaaaaaaa,\n"
7753                "    aaaaaaaaaaaaaaaaaaaaa,\n"
7754                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
7755                Style);
7756   verifyFormat(
7757       "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n"
7758       "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
7759       Style);
7760   verifyFormat(
7761       "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n"
7762       "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
7763       Style);
7764   verifyFormat(
7765       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
7766       "    aaaaaaaaaaaaaaaaaaaaa(\n"
7767       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n"
7768       "    aaaaaaaaaaaaaaaa);",
7769       Style);
7770   verifyFormat(
7771       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
7772       "    aaaaaaaaaaaaaaaaaaaaa(\n"
7773       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n"
7774       "    aaaaaaaaaaaaaaaa);",
7775       Style);
7776 }
7777 
7778 TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
7779   FormatStyle Style = getLLVMStyleWithColumns(40);
7780   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
7781                "          bbbbbbbbbbbbbbbbbbbbbb);",
7782                Style);
7783   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
7784   Style.AlignOperands = FormatStyle::OAS_DontAlign;
7785   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
7786                "          bbbbbbbbbbbbbbbbbbbbbb);",
7787                Style);
7788   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
7789   Style.AlignOperands = FormatStyle::OAS_Align;
7790   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
7791                "          bbbbbbbbbbbbbbbbbbbbbb);",
7792                Style);
7793   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
7794   Style.AlignOperands = FormatStyle::OAS_DontAlign;
7795   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
7796                "    bbbbbbbbbbbbbbbbbbbbbb);",
7797                Style);
7798 }
7799 
7800 TEST_F(FormatTest, BreaksConditionalExpressions) {
7801   verifyFormat(
7802       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7803       "                               ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7804       "                               : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7805   verifyFormat(
7806       "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
7807       "     aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7808       "                                : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7809   verifyFormat(
7810       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7811       "                                   : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7812   verifyFormat("aaaa(aaaaaaaaa, aaaaaaaaa,\n"
7813                "     aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7814                "             : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7815   verifyFormat(
7816       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
7817       "                                                    : aaaaaaaaaaaaa);");
7818   verifyFormat(
7819       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7820       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7821       "                                    : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7822       "                   aaaaaaaaaaaaa);");
7823   verifyFormat(
7824       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7825       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7826       "                   aaaaaaaaaaaaa);");
7827   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7828                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7829                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7830                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7831                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7832   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7833                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7834                "           ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7835                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7836                "           : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7837                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7838                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7839   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7840                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7841                "           ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7842                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7843                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7844   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7845                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7846                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
7847   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
7848                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7849                "        ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7850                "        : aaaaaaaaaaaaaaaa;");
7851   verifyFormat(
7852       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7853       "    ? aaaaaaaaaaaaaaa\n"
7854       "    : aaaaaaaaaaaaaaa;");
7855   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
7856                "          aaaaaaaaa\n"
7857                "      ? b\n"
7858                "      : c);");
7859   verifyFormat("return aaaa == bbbb\n"
7860                "           // comment\n"
7861                "           ? aaaa\n"
7862                "           : bbbb;");
7863   verifyFormat("unsigned Indent =\n"
7864                "    format(TheLine.First,\n"
7865                "           IndentForLevel[TheLine.Level] >= 0\n"
7866                "               ? IndentForLevel[TheLine.Level]\n"
7867                "               : TheLine * 2,\n"
7868                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
7869                getLLVMStyleWithColumns(60));
7870   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
7871                "                  ? aaaaaaaaaaaaaaa\n"
7872                "                  : bbbbbbbbbbbbbbb //\n"
7873                "                        ? ccccccccccccccc\n"
7874                "                        : ddddddddddddddd;");
7875   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
7876                "                  ? aaaaaaaaaaaaaaa\n"
7877                "                  : (bbbbbbbbbbbbbbb //\n"
7878                "                         ? ccccccccccccccc\n"
7879                "                         : ddddddddddddddd);");
7880   verifyFormat(
7881       "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7882       "                                      ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7883       "                                            aaaaaaaaaaaaaaaaaaaaa +\n"
7884       "                                            aaaaaaaaaaaaaaaaaaaaa\n"
7885       "                                      : aaaaaaaaaa;");
7886   verifyFormat(
7887       "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7888       "                                   : aaaaaaaaaaaaaaaaaaaaaa\n"
7889       "                      : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
7890 
7891   FormatStyle NoBinPacking = getLLVMStyle();
7892   NoBinPacking.BinPackArguments = false;
7893   verifyFormat(
7894       "void f() {\n"
7895       "  g(aaa,\n"
7896       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
7897       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7898       "        ? aaaaaaaaaaaaaaa\n"
7899       "        : aaaaaaaaaaaaaaa);\n"
7900       "}",
7901       NoBinPacking);
7902   verifyFormat(
7903       "void f() {\n"
7904       "  g(aaa,\n"
7905       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
7906       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7907       "        ?: aaaaaaaaaaaaaaa);\n"
7908       "}",
7909       NoBinPacking);
7910 
7911   verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n"
7912                "             // comment.\n"
7913                "             ccccccccccccccccccccccccccccccccccccccc\n"
7914                "                 ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7915                "                 : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);");
7916 
7917   // Assignments in conditional expressions. Apparently not uncommon :-(.
7918   verifyFormat("return a != b\n"
7919                "           // comment\n"
7920                "           ? a = b\n"
7921                "           : a = b;");
7922   verifyFormat("return a != b\n"
7923                "           // comment\n"
7924                "           ? a = a != b\n"
7925                "                     // comment\n"
7926                "                     ? a = b\n"
7927                "                     : a\n"
7928                "           : a;\n");
7929   verifyFormat("return a != b\n"
7930                "           // comment\n"
7931                "           ? a\n"
7932                "           : a = a != b\n"
7933                "                     // comment\n"
7934                "                     ? a = b\n"
7935                "                     : a;");
7936 
7937   // Chained conditionals
7938   FormatStyle Style = getLLVMStyleWithColumns(70);
7939   Style.AlignOperands = FormatStyle::OAS_Align;
7940   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7941                "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7942                "                        : 3333333333333333;",
7943                Style);
7944   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7945                "       : bbbbbbbbbb     ? 2222222222222222\n"
7946                "                        : 3333333333333333;",
7947                Style);
7948   verifyFormat("return aaaaaaaaaa         ? 1111111111111111\n"
7949                "       : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
7950                "                          : 3333333333333333;",
7951                Style);
7952   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7953                "       : bbbbbbbbbbbbbb ? 222222\n"
7954                "                        : 333333;",
7955                Style);
7956   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7957                "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7958                "       : cccccccccccccc ? 3333333333333333\n"
7959                "                        : 4444444444444444;",
7960                Style);
7961   verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc)\n"
7962                "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7963                "                        : 3333333333333333;",
7964                Style);
7965   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7966                "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7967                "                        : (aaa ? bbb : ccc);",
7968                Style);
7969   verifyFormat(
7970       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7971       "                                             : cccccccccccccccccc)\n"
7972       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7973       "                        : 3333333333333333;",
7974       Style);
7975   verifyFormat(
7976       "return aaaaaaaaa        ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7977       "                                             : cccccccccccccccccc)\n"
7978       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7979       "                        : 3333333333333333;",
7980       Style);
7981   verifyFormat(
7982       "return aaaaaaaaa        ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7983       "                                             : dddddddddddddddddd)\n"
7984       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7985       "                        : 3333333333333333;",
7986       Style);
7987   verifyFormat(
7988       "return aaaaaaaaa        ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7989       "                                             : dddddddddddddddddd)\n"
7990       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7991       "                        : 3333333333333333;",
7992       Style);
7993   verifyFormat(
7994       "return aaaaaaaaa        ? 1111111111111111\n"
7995       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7996       "                        : a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7997       "                                             : dddddddddddddddddd)\n",
7998       Style);
7999   verifyFormat(
8000       "return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
8001       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
8002       "                        : (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
8003       "                                             : cccccccccccccccccc);",
8004       Style);
8005   verifyFormat(
8006       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
8007       "                           : ccccccccccccccc ? dddddddddddddddddd\n"
8008       "                                             : eeeeeeeeeeeeeeeeee)\n"
8009       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
8010       "                        : 3333333333333333;",
8011       Style);
8012   verifyFormat(
8013       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa    ? bbbbbbbbbbbbbbbbbb\n"
8014       "                           : ccccccccccccccc ? dddddddddddddddddd\n"
8015       "                                             : eeeeeeeeeeeeeeeeee)\n"
8016       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
8017       "                        : 3333333333333333;",
8018       Style);
8019   verifyFormat(
8020       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
8021       "                           : cccccccccccc    ? dddddddddddddddddd\n"
8022       "                                             : eeeeeeeeeeeeeeeeee)\n"
8023       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
8024       "                        : 3333333333333333;",
8025       Style);
8026   verifyFormat(
8027       "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
8028       "                                             : cccccccccccccccccc\n"
8029       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
8030       "                        : 3333333333333333;",
8031       Style);
8032   verifyFormat(
8033       "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
8034       "                          : cccccccccccccccc ? dddddddddddddddddd\n"
8035       "                                             : eeeeeeeeeeeeeeeeee\n"
8036       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
8037       "                        : 3333333333333333;",
8038       Style);
8039   verifyFormat("return aaaaaaaaaaaaaaaaaaaaa\n"
8040                "           ? (aaaaaaaaaaaaaaaaaa   ? bbbbbbbbbbbbbbbbbb\n"
8041                "              : cccccccccccccccccc ? dddddddddddddddddd\n"
8042                "                                   : eeeeeeeeeeeeeeeeee)\n"
8043                "       : bbbbbbbbbbbbbbbbbbb ? 2222222222222222\n"
8044                "                             : 3333333333333333;",
8045                Style);
8046   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaa\n"
8047                "           ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
8048                "             : cccccccccccccccc ? dddddddddddddddddd\n"
8049                "                                : eeeeeeeeeeeeeeeeee\n"
8050                "       : bbbbbbbbbbbbbbbbbbbbbbb ? 2222222222222222\n"
8051                "                                 : 3333333333333333;",
8052                Style);
8053 
8054   Style.AlignOperands = FormatStyle::OAS_DontAlign;
8055   Style.BreakBeforeTernaryOperators = false;
8056   // FIXME: Aligning the question marks is weird given DontAlign.
8057   // Consider disabling this alignment in this case. Also check whether this
8058   // will render the adjustment from https://reviews.llvm.org/D82199
8059   // unnecessary.
8060   verifyFormat("int x = aaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa :\n"
8061                "    bbbb                ? cccccccccccccccccc :\n"
8062                "                          ddddd;\n",
8063                Style);
8064 
8065   EXPECT_EQ(
8066       "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n"
8067       "    /*\n"
8068       "     */\n"
8069       "    function() {\n"
8070       "      try {\n"
8071       "        return JJJJJJJJJJJJJJ(\n"
8072       "            pppppppppppppppppppppppppppppppppppppppppppppppppp);\n"
8073       "      }\n"
8074       "    } :\n"
8075       "    function() {};",
8076       format(
8077           "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n"
8078           "     /*\n"
8079           "      */\n"
8080           "     function() {\n"
8081           "      try {\n"
8082           "        return JJJJJJJJJJJJJJ(\n"
8083           "            pppppppppppppppppppppppppppppppppppppppppppppppppp);\n"
8084           "      }\n"
8085           "    } :\n"
8086           "    function() {};",
8087           getGoogleStyle(FormatStyle::LK_JavaScript)));
8088 }
8089 
8090 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
8091   FormatStyle Style = getLLVMStyleWithColumns(70);
8092   Style.BreakBeforeTernaryOperators = false;
8093   verifyFormat(
8094       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
8095       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
8096       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
8097       Style);
8098   verifyFormat(
8099       "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
8100       "     aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
8101       "                                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
8102       Style);
8103   verifyFormat(
8104       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
8105       "                                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
8106       Style);
8107   verifyFormat("aaaa(aaaaaaaa, aaaaaaaaaa,\n"
8108                "     aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
8109                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
8110                Style);
8111   verifyFormat(
8112       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
8113       "                                                      aaaaaaaaaaaaa);",
8114       Style);
8115   verifyFormat(
8116       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8117       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
8118       "                                      aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8119       "                   aaaaaaaaaaaaa);",
8120       Style);
8121   verifyFormat(
8122       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8123       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8124       "                   aaaaaaaaaaaaa);",
8125       Style);
8126   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
8127                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8128                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
8129                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8130                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
8131                Style);
8132   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8133                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
8134                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8135                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
8136                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8137                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
8138                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
8139                Style);
8140   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8141                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n"
8142                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8143                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
8144                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
8145                Style);
8146   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
8147                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
8148                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
8149                Style);
8150   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
8151                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
8152                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
8153                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
8154                Style);
8155   verifyFormat(
8156       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
8157       "    aaaaaaaaaaaaaaa :\n"
8158       "    aaaaaaaaaaaaaaa;",
8159       Style);
8160   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
8161                "          aaaaaaaaa ?\n"
8162                "      b :\n"
8163                "      c);",
8164                Style);
8165   verifyFormat("unsigned Indent =\n"
8166                "    format(TheLine.First,\n"
8167                "           IndentForLevel[TheLine.Level] >= 0 ?\n"
8168                "               IndentForLevel[TheLine.Level] :\n"
8169                "               TheLine * 2,\n"
8170                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
8171                Style);
8172   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
8173                "                  aaaaaaaaaaaaaaa :\n"
8174                "                  bbbbbbbbbbbbbbb ? //\n"
8175                "                      ccccccccccccccc :\n"
8176                "                      ddddddddddddddd;",
8177                Style);
8178   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
8179                "                  aaaaaaaaaaaaaaa :\n"
8180                "                  (bbbbbbbbbbbbbbb ? //\n"
8181                "                       ccccccccccccccc :\n"
8182                "                       ddddddddddddddd);",
8183                Style);
8184   verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
8185                "            /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n"
8186                "            ccccccccccccccccccccccccccc;",
8187                Style);
8188   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
8189                "           aaaaa :\n"
8190                "           bbbbbbbbbbbbbbb + cccccccccccccccc;",
8191                Style);
8192 
8193   // Chained conditionals
8194   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
8195                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8196                "                          3333333333333333;",
8197                Style);
8198   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
8199                "       bbbbbbbbbb       ? 2222222222222222 :\n"
8200                "                          3333333333333333;",
8201                Style);
8202   verifyFormat("return aaaaaaaaaa       ? 1111111111111111 :\n"
8203                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8204                "                          3333333333333333;",
8205                Style);
8206   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
8207                "       bbbbbbbbbbbbbbbb ? 222222 :\n"
8208                "                          333333;",
8209                Style);
8210   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
8211                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8212                "       cccccccccccccccc ? 3333333333333333 :\n"
8213                "                          4444444444444444;",
8214                Style);
8215   verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc) :\n"
8216                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8217                "                          3333333333333333;",
8218                Style);
8219   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
8220                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8221                "                          (aaa ? bbb : ccc);",
8222                Style);
8223   verifyFormat(
8224       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8225       "                                               cccccccccccccccccc) :\n"
8226       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8227       "                          3333333333333333;",
8228       Style);
8229   verifyFormat(
8230       "return aaaaaaaaa        ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8231       "                                               cccccccccccccccccc) :\n"
8232       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8233       "                          3333333333333333;",
8234       Style);
8235   verifyFormat(
8236       "return aaaaaaaaa        ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8237       "                                               dddddddddddddddddd) :\n"
8238       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8239       "                          3333333333333333;",
8240       Style);
8241   verifyFormat(
8242       "return aaaaaaaaa        ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8243       "                                               dddddddddddddddddd) :\n"
8244       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8245       "                          3333333333333333;",
8246       Style);
8247   verifyFormat(
8248       "return aaaaaaaaa        ? 1111111111111111 :\n"
8249       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8250       "                          a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8251       "                                               dddddddddddddddddd)\n",
8252       Style);
8253   verifyFormat(
8254       "return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
8255       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8256       "                          (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8257       "                                               cccccccccccccccccc);",
8258       Style);
8259   verifyFormat(
8260       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8261       "                           ccccccccccccccccc ? dddddddddddddddddd :\n"
8262       "                                               eeeeeeeeeeeeeeeeee) :\n"
8263       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8264       "                          3333333333333333;",
8265       Style);
8266   verifyFormat(
8267       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8268       "                           ccccccccccccc     ? dddddddddddddddddd :\n"
8269       "                                               eeeeeeeeeeeeeeeeee) :\n"
8270       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8271       "                          3333333333333333;",
8272       Style);
8273   verifyFormat(
8274       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaa     ? bbbbbbbbbbbbbbbbbb :\n"
8275       "                           ccccccccccccccccc ? dddddddddddddddddd :\n"
8276       "                                               eeeeeeeeeeeeeeeeee) :\n"
8277       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8278       "                          3333333333333333;",
8279       Style);
8280   verifyFormat(
8281       "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8282       "                                               cccccccccccccccccc :\n"
8283       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8284       "                          3333333333333333;",
8285       Style);
8286   verifyFormat(
8287       "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8288       "                          cccccccccccccccccc ? dddddddddddddddddd :\n"
8289       "                                               eeeeeeeeeeeeeeeeee :\n"
8290       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8291       "                          3333333333333333;",
8292       Style);
8293   verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n"
8294                "           (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8295                "            cccccccccccccccccc ? dddddddddddddddddd :\n"
8296                "                                 eeeeeeeeeeeeeeeeee) :\n"
8297                "       bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8298                "                               3333333333333333;",
8299                Style);
8300   verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n"
8301                "           aaaaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8302                "           cccccccccccccccccccc ? dddddddddddddddddd :\n"
8303                "                                  eeeeeeeeeeeeeeeeee :\n"
8304                "       bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8305                "                               3333333333333333;",
8306                Style);
8307 }
8308 
8309 TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
8310   verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
8311                "     aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
8312   verifyFormat("bool a = true, b = false;");
8313 
8314   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
8315                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
8316                "     bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
8317                "         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
8318   verifyFormat(
8319       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
8320       "         bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
8321       "     d = e && f;");
8322   verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
8323                "          c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
8324   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
8325                "          *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
8326   verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
8327                "          ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
8328 
8329   FormatStyle Style = getGoogleStyle();
8330   Style.PointerAlignment = FormatStyle::PAS_Left;
8331   Style.DerivePointerAlignment = false;
8332   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8333                "    *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
8334                "    *b = bbbbbbbbbbbbbbbbbbb;",
8335                Style);
8336   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
8337                "          *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;",
8338                Style);
8339   verifyFormat("vector<int*> a, b;", Style);
8340   verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style);
8341 }
8342 
8343 TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
8344   verifyFormat("arr[foo ? bar : baz];");
8345   verifyFormat("f()[foo ? bar : baz];");
8346   verifyFormat("(a + b)[foo ? bar : baz];");
8347   verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
8348 }
8349 
8350 TEST_F(FormatTest, AlignsStringLiterals) {
8351   verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
8352                "                                      \"short literal\");");
8353   verifyFormat(
8354       "looooooooooooooooooooooooongFunction(\n"
8355       "    \"short literal\"\n"
8356       "    \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
8357   verifyFormat("someFunction(\"Always break between multi-line\"\n"
8358                "             \" string literals\",\n"
8359                "             and, other, parameters);");
8360   EXPECT_EQ("fun + \"1243\" /* comment */\n"
8361             "      \"5678\";",
8362             format("fun + \"1243\" /* comment */\n"
8363                    "    \"5678\";",
8364                    getLLVMStyleWithColumns(28)));
8365   EXPECT_EQ(
8366       "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
8367       "         \"aaaaaaaaaaaaaaaaaaaaa\"\n"
8368       "         \"aaaaaaaaaaaaaaaa\";",
8369       format("aaaaaa ="
8370              "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
8371              "aaaaaaaaaaaaaaaaaaaaa\" "
8372              "\"aaaaaaaaaaaaaaaa\";"));
8373   verifyFormat("a = a + \"a\"\n"
8374                "        \"a\"\n"
8375                "        \"a\";");
8376   verifyFormat("f(\"a\", \"b\"\n"
8377                "       \"c\");");
8378 
8379   verifyFormat(
8380       "#define LL_FORMAT \"ll\"\n"
8381       "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
8382       "       \"d, ddddddddd: %\" LL_FORMAT \"d\");");
8383 
8384   verifyFormat("#define A(X)          \\\n"
8385                "  \"aaaaa\" #X \"bbbbbb\" \\\n"
8386                "  \"ccccc\"",
8387                getLLVMStyleWithColumns(23));
8388   verifyFormat("#define A \"def\"\n"
8389                "f(\"abc\" A \"ghi\"\n"
8390                "  \"jkl\");");
8391 
8392   verifyFormat("f(L\"a\"\n"
8393                "  L\"b\");");
8394   verifyFormat("#define A(X)            \\\n"
8395                "  L\"aaaaa\" #X L\"bbbbbb\" \\\n"
8396                "  L\"ccccc\"",
8397                getLLVMStyleWithColumns(25));
8398 
8399   verifyFormat("f(@\"a\"\n"
8400                "  @\"b\");");
8401   verifyFormat("NSString s = @\"a\"\n"
8402                "             @\"b\"\n"
8403                "             @\"c\";");
8404   verifyFormat("NSString s = @\"a\"\n"
8405                "              \"b\"\n"
8406                "              \"c\";");
8407 }
8408 
8409 TEST_F(FormatTest, ReturnTypeBreakingStyle) {
8410   FormatStyle Style = getLLVMStyle();
8411   // No declarations or definitions should be moved to own line.
8412   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None;
8413   verifyFormat("class A {\n"
8414                "  int f() { return 1; }\n"
8415                "  int g();\n"
8416                "};\n"
8417                "int f() { return 1; }\n"
8418                "int g();\n",
8419                Style);
8420 
8421   // All declarations and definitions should have the return type moved to its
8422   // own line.
8423   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
8424   Style.TypenameMacros = {"LIST"};
8425   verifyFormat("SomeType\n"
8426                "funcdecl(LIST(uint64_t));",
8427                Style);
8428   verifyFormat("class E {\n"
8429                "  int\n"
8430                "  f() {\n"
8431                "    return 1;\n"
8432                "  }\n"
8433                "  int\n"
8434                "  g();\n"
8435                "};\n"
8436                "int\n"
8437                "f() {\n"
8438                "  return 1;\n"
8439                "}\n"
8440                "int\n"
8441                "g();\n",
8442                Style);
8443 
8444   // Top-level definitions, and no kinds of declarations should have the
8445   // return type moved to its own line.
8446   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions;
8447   verifyFormat("class B {\n"
8448                "  int f() { return 1; }\n"
8449                "  int g();\n"
8450                "};\n"
8451                "int\n"
8452                "f() {\n"
8453                "  return 1;\n"
8454                "}\n"
8455                "int g();\n",
8456                Style);
8457 
8458   // Top-level definitions and declarations should have the return type moved
8459   // to its own line.
8460   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel;
8461   verifyFormat("class C {\n"
8462                "  int f() { return 1; }\n"
8463                "  int g();\n"
8464                "};\n"
8465                "int\n"
8466                "f() {\n"
8467                "  return 1;\n"
8468                "}\n"
8469                "int\n"
8470                "g();\n",
8471                Style);
8472 
8473   // All definitions should have the return type moved to its own line, but no
8474   // kinds of declarations.
8475   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
8476   verifyFormat("class D {\n"
8477                "  int\n"
8478                "  f() {\n"
8479                "    return 1;\n"
8480                "  }\n"
8481                "  int g();\n"
8482                "};\n"
8483                "int\n"
8484                "f() {\n"
8485                "  return 1;\n"
8486                "}\n"
8487                "int g();\n",
8488                Style);
8489   verifyFormat("const char *\n"
8490                "f(void) {\n" // Break here.
8491                "  return \"\";\n"
8492                "}\n"
8493                "const char *bar(void);\n", // No break here.
8494                Style);
8495   verifyFormat("template <class T>\n"
8496                "T *\n"
8497                "f(T &c) {\n" // Break here.
8498                "  return NULL;\n"
8499                "}\n"
8500                "template <class T> T *f(T &c);\n", // No break here.
8501                Style);
8502   verifyFormat("class C {\n"
8503                "  int\n"
8504                "  operator+() {\n"
8505                "    return 1;\n"
8506                "  }\n"
8507                "  int\n"
8508                "  operator()() {\n"
8509                "    return 1;\n"
8510                "  }\n"
8511                "};\n",
8512                Style);
8513   verifyFormat("void\n"
8514                "A::operator()() {}\n"
8515                "void\n"
8516                "A::operator>>() {}\n"
8517                "void\n"
8518                "A::operator+() {}\n"
8519                "void\n"
8520                "A::operator*() {}\n"
8521                "void\n"
8522                "A::operator->() {}\n"
8523                "void\n"
8524                "A::operator void *() {}\n"
8525                "void\n"
8526                "A::operator void &() {}\n"
8527                "void\n"
8528                "A::operator void &&() {}\n"
8529                "void\n"
8530                "A::operator char *() {}\n"
8531                "void\n"
8532                "A::operator[]() {}\n"
8533                "void\n"
8534                "A::operator!() {}\n"
8535                "void\n"
8536                "A::operator**() {}\n"
8537                "void\n"
8538                "A::operator<Foo> *() {}\n"
8539                "void\n"
8540                "A::operator<Foo> **() {}\n"
8541                "void\n"
8542                "A::operator<Foo> &() {}\n"
8543                "void\n"
8544                "A::operator void **() {}\n",
8545                Style);
8546   verifyFormat("constexpr auto\n"
8547                "operator()() const -> reference {}\n"
8548                "constexpr auto\n"
8549                "operator>>() const -> reference {}\n"
8550                "constexpr auto\n"
8551                "operator+() const -> reference {}\n"
8552                "constexpr auto\n"
8553                "operator*() const -> reference {}\n"
8554                "constexpr auto\n"
8555                "operator->() const -> reference {}\n"
8556                "constexpr auto\n"
8557                "operator++() const -> reference {}\n"
8558                "constexpr auto\n"
8559                "operator void *() const -> reference {}\n"
8560                "constexpr auto\n"
8561                "operator void **() const -> reference {}\n"
8562                "constexpr auto\n"
8563                "operator void *() const -> reference {}\n"
8564                "constexpr auto\n"
8565                "operator void &() const -> reference {}\n"
8566                "constexpr auto\n"
8567                "operator void &&() const -> reference {}\n"
8568                "constexpr auto\n"
8569                "operator char *() const -> reference {}\n"
8570                "constexpr auto\n"
8571                "operator!() const -> reference {}\n"
8572                "constexpr auto\n"
8573                "operator[]() const -> reference {}\n",
8574                Style);
8575   verifyFormat("void *operator new(std::size_t s);", // No break here.
8576                Style);
8577   verifyFormat("void *\n"
8578                "operator new(std::size_t s) {}",
8579                Style);
8580   verifyFormat("void *\n"
8581                "operator delete[](void *ptr) {}",
8582                Style);
8583   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
8584   verifyFormat("const char *\n"
8585                "f(void)\n" // Break here.
8586                "{\n"
8587                "  return \"\";\n"
8588                "}\n"
8589                "const char *bar(void);\n", // No break here.
8590                Style);
8591   verifyFormat("template <class T>\n"
8592                "T *\n"     // Problem here: no line break
8593                "f(T &c)\n" // Break here.
8594                "{\n"
8595                "  return NULL;\n"
8596                "}\n"
8597                "template <class T> T *f(T &c);\n", // No break here.
8598                Style);
8599   verifyFormat("int\n"
8600                "foo(A<bool> a)\n"
8601                "{\n"
8602                "  return a;\n"
8603                "}\n",
8604                Style);
8605   verifyFormat("int\n"
8606                "foo(A<8> a)\n"
8607                "{\n"
8608                "  return a;\n"
8609                "}\n",
8610                Style);
8611   verifyFormat("int\n"
8612                "foo(A<B<bool>, 8> a)\n"
8613                "{\n"
8614                "  return a;\n"
8615                "}\n",
8616                Style);
8617   verifyFormat("int\n"
8618                "foo(A<B<8>, bool> a)\n"
8619                "{\n"
8620                "  return a;\n"
8621                "}\n",
8622                Style);
8623   verifyFormat("int\n"
8624                "foo(A<B<bool>, bool> a)\n"
8625                "{\n"
8626                "  return a;\n"
8627                "}\n",
8628                Style);
8629   verifyFormat("int\n"
8630                "foo(A<B<8>, 8> a)\n"
8631                "{\n"
8632                "  return a;\n"
8633                "}\n",
8634                Style);
8635 
8636   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
8637   Style.BraceWrapping.AfterFunction = true;
8638   verifyFormat("int f(i);\n" // No break here.
8639                "int\n"       // Break here.
8640                "f(i)\n"
8641                "{\n"
8642                "  return i + 1;\n"
8643                "}\n"
8644                "int\n" // Break here.
8645                "f(i)\n"
8646                "{\n"
8647                "  return i + 1;\n"
8648                "};",
8649                Style);
8650   verifyFormat("int f(a, b, c);\n" // No break here.
8651                "int\n"             // Break here.
8652                "f(a, b, c)\n"      // Break here.
8653                "short a, b;\n"
8654                "float c;\n"
8655                "{\n"
8656                "  return a + b < c;\n"
8657                "}\n"
8658                "int\n"        // Break here.
8659                "f(a, b, c)\n" // Break here.
8660                "short a, b;\n"
8661                "float c;\n"
8662                "{\n"
8663                "  return a + b < c;\n"
8664                "};",
8665                Style);
8666   verifyFormat("byte *\n" // Break here.
8667                "f(a)\n"   // Break here.
8668                "byte a[];\n"
8669                "{\n"
8670                "  return a;\n"
8671                "}",
8672                Style);
8673   verifyFormat("bool f(int a, int) override;\n"
8674                "Bar g(int a, Bar) final;\n"
8675                "Bar h(a, Bar) final;",
8676                Style);
8677   verifyFormat("int\n"
8678                "f(a)",
8679                Style);
8680   verifyFormat("bool\n"
8681                "f(size_t = 0, bool b = false)\n"
8682                "{\n"
8683                "  return !b;\n"
8684                "}",
8685                Style);
8686 
8687   // The return breaking style doesn't affect:
8688   // * function and object definitions with attribute-like macros
8689   verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n"
8690                "    ABSL_GUARDED_BY(mutex) = {};",
8691                getGoogleStyleWithColumns(40));
8692   verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n"
8693                "    ABSL_GUARDED_BY(mutex);  // comment",
8694                getGoogleStyleWithColumns(40));
8695   verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n"
8696                "    ABSL_GUARDED_BY(mutex1)\n"
8697                "        ABSL_GUARDED_BY(mutex2);",
8698                getGoogleStyleWithColumns(40));
8699   verifyFormat("Tttttt f(int a, int b)\n"
8700                "    ABSL_GUARDED_BY(mutex1)\n"
8701                "        ABSL_GUARDED_BY(mutex2);",
8702                getGoogleStyleWithColumns(40));
8703   // * typedefs
8704   verifyFormat("typedef ATTR(X) char x;", getGoogleStyle());
8705 
8706   Style = getGNUStyle();
8707 
8708   // Test for comments at the end of function declarations.
8709   verifyFormat("void\n"
8710                "foo (int a, /*abc*/ int b) // def\n"
8711                "{\n"
8712                "}\n",
8713                Style);
8714 
8715   verifyFormat("void\n"
8716                "foo (int a, /* abc */ int b) /* def */\n"
8717                "{\n"
8718                "}\n",
8719                Style);
8720 
8721   // Definitions that should not break after return type
8722   verifyFormat("void foo (int a, int b); // def\n", Style);
8723   verifyFormat("void foo (int a, int b); /* def */\n", Style);
8724   verifyFormat("void foo (int a, int b);\n", Style);
8725 }
8726 
8727 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
8728   FormatStyle NoBreak = getLLVMStyle();
8729   NoBreak.AlwaysBreakBeforeMultilineStrings = false;
8730   FormatStyle Break = getLLVMStyle();
8731   Break.AlwaysBreakBeforeMultilineStrings = true;
8732   verifyFormat("aaaa = \"bbbb\"\n"
8733                "       \"cccc\";",
8734                NoBreak);
8735   verifyFormat("aaaa =\n"
8736                "    \"bbbb\"\n"
8737                "    \"cccc\";",
8738                Break);
8739   verifyFormat("aaaa(\"bbbb\"\n"
8740                "     \"cccc\");",
8741                NoBreak);
8742   verifyFormat("aaaa(\n"
8743                "    \"bbbb\"\n"
8744                "    \"cccc\");",
8745                Break);
8746   verifyFormat("aaaa(qqq, \"bbbb\"\n"
8747                "          \"cccc\");",
8748                NoBreak);
8749   verifyFormat("aaaa(qqq,\n"
8750                "     \"bbbb\"\n"
8751                "     \"cccc\");",
8752                Break);
8753   verifyFormat("aaaa(qqq,\n"
8754                "     L\"bbbb\"\n"
8755                "     L\"cccc\");",
8756                Break);
8757   verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n"
8758                "                      \"bbbb\"));",
8759                Break);
8760   verifyFormat("string s = someFunction(\n"
8761                "    \"abc\"\n"
8762                "    \"abc\");",
8763                Break);
8764 
8765   // As we break before unary operators, breaking right after them is bad.
8766   verifyFormat("string foo = abc ? \"x\"\n"
8767                "                   \"blah blah blah blah blah blah\"\n"
8768                "                 : \"y\";",
8769                Break);
8770 
8771   // Don't break if there is no column gain.
8772   verifyFormat("f(\"aaaa\"\n"
8773                "  \"bbbb\");",
8774                Break);
8775 
8776   // Treat literals with escaped newlines like multi-line string literals.
8777   EXPECT_EQ("x = \"a\\\n"
8778             "b\\\n"
8779             "c\";",
8780             format("x = \"a\\\n"
8781                    "b\\\n"
8782                    "c\";",
8783                    NoBreak));
8784   EXPECT_EQ("xxxx =\n"
8785             "    \"a\\\n"
8786             "b\\\n"
8787             "c\";",
8788             format("xxxx = \"a\\\n"
8789                    "b\\\n"
8790                    "c\";",
8791                    Break));
8792 
8793   EXPECT_EQ("NSString *const kString =\n"
8794             "    @\"aaaa\"\n"
8795             "    @\"bbbb\";",
8796             format("NSString *const kString = @\"aaaa\"\n"
8797                    "@\"bbbb\";",
8798                    Break));
8799 
8800   Break.ColumnLimit = 0;
8801   verifyFormat("const char *hello = \"hello llvm\";", Break);
8802 }
8803 
8804 TEST_F(FormatTest, AlignsPipes) {
8805   verifyFormat(
8806       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8807       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8808       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8809   verifyFormat(
8810       "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
8811       "                     << aaaaaaaaaaaaaaaaaaaa;");
8812   verifyFormat(
8813       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8814       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8815   verifyFormat(
8816       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
8817       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8818   verifyFormat(
8819       "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
8820       "                \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
8821       "             << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
8822   verifyFormat(
8823       "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8824       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8825       "         << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8826   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8827                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8828                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8829                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
8830   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n"
8831                "             << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);");
8832   verifyFormat(
8833       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8834       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8835   verifyFormat(
8836       "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n"
8837       "                                       aaaaaaaaaaaaaaaaaaaaaaaaaa);");
8838 
8839   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
8840                "             << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
8841   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8842                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8843                "                    aaaaaaaaaaaaaaaaaaaaa)\n"
8844                "             << aaaaaaaaaaaaaaaaaaaaaaaaaa;");
8845   verifyFormat("LOG_IF(aaa == //\n"
8846                "       bbb)\n"
8847                "    << a << b;");
8848 
8849   // But sometimes, breaking before the first "<<" is desirable.
8850   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
8851                "    << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
8852   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
8853                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8854                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8855   verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
8856                "    << BEF << IsTemplate << Description << E->getType();");
8857   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
8858                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8859                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8860   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
8861                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8862                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8863                "    << aaa;");
8864 
8865   verifyFormat(
8866       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8867       "                    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
8868 
8869   // Incomplete string literal.
8870   EXPECT_EQ("llvm::errs() << \"\n"
8871             "             << a;",
8872             format("llvm::errs() << \"\n<<a;"));
8873 
8874   verifyFormat("void f() {\n"
8875                "  CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
8876                "      << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
8877                "}");
8878 
8879   // Handle 'endl'.
8880   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n"
8881                "             << bbbbbbbbbbbbbbbbbbbbbb << endl;");
8882   verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;");
8883 
8884   // Handle '\n'.
8885   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n"
8886                "             << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
8887   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n"
8888                "             << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';");
8889   verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n"
8890                "             << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";");
8891   verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
8892 }
8893 
8894 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) {
8895   verifyFormat("return out << \"somepacket = {\\n\"\n"
8896                "           << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
8897                "           << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
8898                "           << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
8899                "           << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
8900                "           << \"}\";");
8901 
8902   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
8903                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
8904                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
8905   verifyFormat(
8906       "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
8907       "             << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
8908       "             << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
8909       "             << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
8910       "             << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
8911   verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
8912                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
8913   verifyFormat(
8914       "void f() {\n"
8915       "  llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
8916       "               << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
8917       "}");
8918 
8919   // Breaking before the first "<<" is generally not desirable.
8920   verifyFormat(
8921       "llvm::errs()\n"
8922       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8923       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8924       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8925       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
8926       getLLVMStyleWithColumns(70));
8927   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
8928                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8929                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
8930                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8931                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
8932                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
8933                getLLVMStyleWithColumns(70));
8934 
8935   verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
8936                "           \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
8937                "           \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;");
8938   verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
8939                "                  \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
8940                "                  \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);");
8941   verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n"
8942                "           (aaaa + aaaa);",
8943                getLLVMStyleWithColumns(40));
8944   verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n"
8945                "                  (aaaaaaa + aaaaa));",
8946                getLLVMStyleWithColumns(40));
8947   verifyFormat(
8948       "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n"
8949       "                  SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n"
8950       "                  bbbbbbbbbbbbbbbbbbbbbbb);");
8951 }
8952 
8953 TEST_F(FormatTest, UnderstandsEquals) {
8954   verifyFormat(
8955       "aaaaaaaaaaaaaaaaa =\n"
8956       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8957   verifyFormat(
8958       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
8959       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
8960   verifyFormat(
8961       "if (a) {\n"
8962       "  f();\n"
8963       "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
8964       "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
8965       "}");
8966 
8967   verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
8968                "        100000000 + 10000000) {\n}");
8969 }
8970 
8971 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
8972   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
8973                "    .looooooooooooooooooooooooooooooooooooooongFunction();");
8974 
8975   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
8976                "    ->looooooooooooooooooooooooooooooooooooooongFunction();");
8977 
8978   verifyFormat(
8979       "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
8980       "                                                          Parameter2);");
8981 
8982   verifyFormat(
8983       "ShortObject->shortFunction(\n"
8984       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
8985       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
8986 
8987   verifyFormat("loooooooooooooongFunction(\n"
8988                "    LoooooooooooooongObject->looooooooooooooooongFunction());");
8989 
8990   verifyFormat(
8991       "function(LoooooooooooooooooooooooooooooooooooongObject\n"
8992       "             ->loooooooooooooooooooooooooooooooooooooooongFunction());");
8993 
8994   verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
8995                "    .WillRepeatedly(Return(SomeValue));");
8996   verifyFormat("void f() {\n"
8997                "  EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
8998                "      .Times(2)\n"
8999                "      .WillRepeatedly(Return(SomeValue));\n"
9000                "}");
9001   verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
9002                "    ccccccccccccccccccccccc);");
9003   verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9004                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9005                "          .aaaaa(aaaaa),\n"
9006                "      aaaaaaaaaaaaaaaaaaaaa);");
9007   verifyFormat("void f() {\n"
9008                "  aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9009                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
9010                "}");
9011   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9012                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9013                "    .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9014                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9015                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
9016   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9017                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9018                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9019                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
9020                "}");
9021 
9022   // Here, it is not necessary to wrap at "." or "->".
9023   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
9024                "    aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
9025   verifyFormat(
9026       "aaaaaaaaaaa->aaaaaaaaa(\n"
9027       "    aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9028       "    aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
9029 
9030   verifyFormat(
9031       "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9032       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
9033   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
9034                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
9035   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
9036                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
9037 
9038   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9039                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9040                "    .a();");
9041 
9042   FormatStyle NoBinPacking = getLLVMStyle();
9043   NoBinPacking.BinPackParameters = false;
9044   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
9045                "    .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
9046                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
9047                "                         aaaaaaaaaaaaaaaaaaa,\n"
9048                "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9049                NoBinPacking);
9050 
9051   // If there is a subsequent call, change to hanging indentation.
9052   verifyFormat(
9053       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9054       "                         aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
9055       "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
9056   verifyFormat(
9057       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9058       "    aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
9059   verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9060                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9061                "                 .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
9062   verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9063                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9064                "               .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
9065 }
9066 
9067 TEST_F(FormatTest, WrapsTemplateDeclarations) {
9068   verifyFormat("template <typename T>\n"
9069                "virtual void loooooooooooongFunction(int Param1, int Param2);");
9070   verifyFormat("template <typename T>\n"
9071                "// T should be one of {A, B}.\n"
9072                "virtual void loooooooooooongFunction(int Param1, int Param2);");
9073   verifyFormat(
9074       "template <typename T>\n"
9075       "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
9076   verifyFormat("template <typename T>\n"
9077                "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
9078                "       int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
9079   verifyFormat(
9080       "template <typename T>\n"
9081       "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
9082       "                                      int Paaaaaaaaaaaaaaaaaaaaram2);");
9083   verifyFormat(
9084       "template <typename T>\n"
9085       "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
9086       "                    aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
9087       "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9088   verifyFormat("template <typename T>\n"
9089                "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9090                "    int aaaaaaaaaaaaaaaaaaaaaa);");
9091   verifyFormat(
9092       "template <typename T1, typename T2 = char, typename T3 = char,\n"
9093       "          typename T4 = char>\n"
9094       "void f();");
9095   verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
9096                "          template <typename> class cccccccccccccccccccccc,\n"
9097                "          typename ddddddddddddd>\n"
9098                "class C {};");
9099   verifyFormat(
9100       "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
9101       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9102 
9103   verifyFormat("void f() {\n"
9104                "  a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
9105                "      a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
9106                "}");
9107 
9108   verifyFormat("template <typename T> class C {};");
9109   verifyFormat("template <typename T> void f();");
9110   verifyFormat("template <typename T> void f() {}");
9111   verifyFormat(
9112       "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
9113       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9114       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
9115       "    new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
9116       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9117       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
9118       "        bbbbbbbbbbbbbbbbbbbbbbbb);",
9119       getLLVMStyleWithColumns(72));
9120   EXPECT_EQ("static_cast<A< //\n"
9121             "    B> *>(\n"
9122             "\n"
9123             ");",
9124             format("static_cast<A<//\n"
9125                    "    B>*>(\n"
9126                    "\n"
9127                    "    );"));
9128   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9129                "    const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
9130 
9131   FormatStyle AlwaysBreak = getLLVMStyle();
9132   AlwaysBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
9133   verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
9134   verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
9135   verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
9136   verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9137                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
9138                "    ccccccccccccccccccccccccccccccccccccccccccccccc);");
9139   verifyFormat("template <template <typename> class Fooooooo,\n"
9140                "          template <typename> class Baaaaaaar>\n"
9141                "struct C {};",
9142                AlwaysBreak);
9143   verifyFormat("template <typename T> // T can be A, B or C.\n"
9144                "struct C {};",
9145                AlwaysBreak);
9146   verifyFormat("template <enum E> class A {\n"
9147                "public:\n"
9148                "  E *f();\n"
9149                "};");
9150 
9151   FormatStyle NeverBreak = getLLVMStyle();
9152   NeverBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_No;
9153   verifyFormat("template <typename T> class C {};", NeverBreak);
9154   verifyFormat("template <typename T> void f();", NeverBreak);
9155   verifyFormat("template <typename T> void f() {}", NeverBreak);
9156   verifyFormat("template <typename T>\nvoid foo(aaaaaaaaaaaaaaaaaaaaaaaaaa "
9157                "bbbbbbbbbbbbbbbbbbbb) {}",
9158                NeverBreak);
9159   verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9160                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
9161                "    ccccccccccccccccccccccccccccccccccccccccccccccc);",
9162                NeverBreak);
9163   verifyFormat("template <template <typename> class Fooooooo,\n"
9164                "          template <typename> class Baaaaaaar>\n"
9165                "struct C {};",
9166                NeverBreak);
9167   verifyFormat("template <typename T> // T can be A, B or C.\n"
9168                "struct C {};",
9169                NeverBreak);
9170   verifyFormat("template <enum E> class A {\n"
9171                "public:\n"
9172                "  E *f();\n"
9173                "};",
9174                NeverBreak);
9175   NeverBreak.PenaltyBreakTemplateDeclaration = 100;
9176   verifyFormat("template <typename T> void\nfoo(aaaaaaaaaaaaaaaaaaaaaaaaaa "
9177                "bbbbbbbbbbbbbbbbbbbb) {}",
9178                NeverBreak);
9179 }
9180 
9181 TEST_F(FormatTest, WrapsTemplateDeclarationsWithComments) {
9182   FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp);
9183   Style.ColumnLimit = 60;
9184   EXPECT_EQ("// Baseline - no comments.\n"
9185             "template <\n"
9186             "    typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n"
9187             "void f() {}",
9188             format("// Baseline - no comments.\n"
9189                    "template <\n"
9190                    "    typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n"
9191                    "void f() {}",
9192                    Style));
9193 
9194   EXPECT_EQ("template <\n"
9195             "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value>  // trailing\n"
9196             "void f() {}",
9197             format("template <\n"
9198                    "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
9199                    "void f() {}",
9200                    Style));
9201 
9202   EXPECT_EQ(
9203       "template <\n"
9204       "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n"
9205       "void f() {}",
9206       format("template <typename aaaaaaaaaa<bbbbbbbbbbbb>::value>  /* line */\n"
9207              "void f() {}",
9208              Style));
9209 
9210   EXPECT_EQ(
9211       "template <\n"
9212       "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value>  // trailing\n"
9213       "                                               // multiline\n"
9214       "void f() {}",
9215       format("template <\n"
9216              "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
9217              "                                              // multiline\n"
9218              "void f() {}",
9219              Style));
9220 
9221   EXPECT_EQ(
9222       "template <typename aaaaaaaaaa<\n"
9223       "    bbbbbbbbbbbb>::value>  // trailing loooong\n"
9224       "void f() {}",
9225       format(
9226           "template <\n"
9227           "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing loooong\n"
9228           "void f() {}",
9229           Style));
9230 }
9231 
9232 TEST_F(FormatTest, WrapsTemplateParameters) {
9233   FormatStyle Style = getLLVMStyle();
9234   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
9235   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
9236   verifyFormat(
9237       "template <typename... a> struct q {};\n"
9238       "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
9239       "    aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
9240       "    y;",
9241       Style);
9242   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
9243   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
9244   verifyFormat(
9245       "template <typename... a> struct r {};\n"
9246       "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
9247       "    aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
9248       "    y;",
9249       Style);
9250   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
9251   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
9252   verifyFormat("template <typename... a> struct s {};\n"
9253                "extern s<\n"
9254                "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
9255                "aaaaaaaaaaaaaaaaaaaaaa,\n"
9256                "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
9257                "aaaaaaaaaaaaaaaaaaaaaa>\n"
9258                "    y;",
9259                Style);
9260   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
9261   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
9262   verifyFormat("template <typename... a> struct t {};\n"
9263                "extern t<\n"
9264                "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
9265                "aaaaaaaaaaaaaaaaaaaaaa,\n"
9266                "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
9267                "aaaaaaaaaaaaaaaaaaaaaa>\n"
9268                "    y;",
9269                Style);
9270 }
9271 
9272 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
9273   verifyFormat(
9274       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
9275       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
9276   verifyFormat(
9277       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
9278       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9279       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
9280 
9281   // FIXME: Should we have the extra indent after the second break?
9282   verifyFormat(
9283       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
9284       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
9285       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
9286 
9287   verifyFormat(
9288       "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
9289       "                    cccccccccccccccccccccccccccccccccccccccccccccc());");
9290 
9291   // Breaking at nested name specifiers is generally not desirable.
9292   verifyFormat(
9293       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9294       "    aaaaaaaaaaaaaaaaaaaaaaa);");
9295 
9296   verifyFormat("aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n"
9297                "                   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
9298                "                       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9299                "                   aaaaaaaaaaaaaaaaaaaaa);",
9300                getLLVMStyleWithColumns(74));
9301 
9302   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
9303                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9304                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
9305 }
9306 
9307 TEST_F(FormatTest, UnderstandsTemplateParameters) {
9308   verifyFormat("A<int> a;");
9309   verifyFormat("A<A<A<int>>> a;");
9310   verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
9311   verifyFormat("bool x = a < 1 || 2 > a;");
9312   verifyFormat("bool x = 5 < f<int>();");
9313   verifyFormat("bool x = f<int>() > 5;");
9314   verifyFormat("bool x = 5 < a<int>::x;");
9315   verifyFormat("bool x = a < 4 ? a > 2 : false;");
9316   verifyFormat("bool x = f() ? a < 2 : a > 2;");
9317 
9318   verifyGoogleFormat("A<A<int>> a;");
9319   verifyGoogleFormat("A<A<A<int>>> a;");
9320   verifyGoogleFormat("A<A<A<A<int>>>> a;");
9321   verifyGoogleFormat("A<A<int> > a;");
9322   verifyGoogleFormat("A<A<A<int> > > a;");
9323   verifyGoogleFormat("A<A<A<A<int> > > > a;");
9324   verifyGoogleFormat("A<::A<int>> a;");
9325   verifyGoogleFormat("A<::A> a;");
9326   verifyGoogleFormat("A< ::A> a;");
9327   verifyGoogleFormat("A< ::A<int> > a;");
9328   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
9329   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
9330   EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle()));
9331   EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle()));
9332   EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };",
9333             format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle()));
9334 
9335   verifyFormat("A<A<int>> a;", getChromiumStyle(FormatStyle::LK_Cpp));
9336 
9337   // template closer followed by a token that starts with > or =
9338   verifyFormat("bool b = a<1> > 1;");
9339   verifyFormat("bool b = a<1> >= 1;");
9340   verifyFormat("int i = a<1> >> 1;");
9341   FormatStyle Style = getLLVMStyle();
9342   Style.SpaceBeforeAssignmentOperators = false;
9343   verifyFormat("bool b= a<1> == 1;", Style);
9344   verifyFormat("a<int> = 1;", Style);
9345   verifyFormat("a<int> >>= 1;", Style);
9346 
9347   verifyFormat("test < a | b >> c;");
9348   verifyFormat("test<test<a | b>> c;");
9349   verifyFormat("test >> a >> b;");
9350   verifyFormat("test << a >> b;");
9351 
9352   verifyFormat("f<int>();");
9353   verifyFormat("template <typename T> void f() {}");
9354   verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
9355   verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : "
9356                "sizeof(char)>::type>;");
9357   verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};");
9358   verifyFormat("f(a.operator()<A>());");
9359   verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9360                "      .template operator()<A>());",
9361                getLLVMStyleWithColumns(35));
9362 
9363   // Not template parameters.
9364   verifyFormat("return a < b && c > d;");
9365   verifyFormat("void f() {\n"
9366                "  while (a < b && c > d) {\n"
9367                "  }\n"
9368                "}");
9369   verifyFormat("template <typename... Types>\n"
9370                "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
9371 
9372   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9373                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
9374                getLLVMStyleWithColumns(60));
9375   verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
9376   verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
9377   verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <");
9378   verifyFormat("some_templated_type<decltype([](int i) { return i; })>");
9379 }
9380 
9381 TEST_F(FormatTest, UnderstandsShiftOperators) {
9382   verifyFormat("if (i < x >> 1)");
9383   verifyFormat("while (i < x >> 1)");
9384   verifyFormat("for (unsigned i = 0; i < i; ++i, v = v >> 1)");
9385   verifyFormat("for (unsigned i = 0; i < x >> 1; ++i, v = v >> 1)");
9386   verifyFormat(
9387       "for (std::vector<int>::iterator i = 0; i < x >> 1; ++i, v = v >> 1)");
9388   verifyFormat("Foo.call<Bar<Function>>()");
9389   verifyFormat("if (Foo.call<Bar<Function>>() == 0)");
9390   verifyFormat("for (std::vector<std::pair<int>>::iterator i = 0; i < x >> 1; "
9391                "++i, v = v >> 1)");
9392   verifyFormat("if (w<u<v<x>>, 1>::t)");
9393 }
9394 
9395 TEST_F(FormatTest, BitshiftOperatorWidth) {
9396   EXPECT_EQ("int a = 1 << 2; /* foo\n"
9397             "                   bar */",
9398             format("int    a=1<<2;  /* foo\n"
9399                    "                   bar */"));
9400 
9401   EXPECT_EQ("int b = 256 >> 1; /* foo\n"
9402             "                     bar */",
9403             format("int  b  =256>>1 ;  /* foo\n"
9404                    "                      bar */"));
9405 }
9406 
9407 TEST_F(FormatTest, UnderstandsBinaryOperators) {
9408   verifyFormat("COMPARE(a, ==, b);");
9409   verifyFormat("auto s = sizeof...(Ts) - 1;");
9410 }
9411 
9412 TEST_F(FormatTest, UnderstandsPointersToMembers) {
9413   verifyFormat("int A::*x;");
9414   verifyFormat("int (S::*func)(void *);");
9415   verifyFormat("void f() { int (S::*func)(void *); }");
9416   verifyFormat("typedef bool *(Class::*Member)() const;");
9417   verifyFormat("void f() {\n"
9418                "  (a->*f)();\n"
9419                "  a->*x;\n"
9420                "  (a.*f)();\n"
9421                "  ((*a).*f)();\n"
9422                "  a.*x;\n"
9423                "}");
9424   verifyFormat("void f() {\n"
9425                "  (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
9426                "      aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
9427                "}");
9428   verifyFormat(
9429       "(aaaaaaaaaa->*bbbbbbb)(\n"
9430       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
9431   FormatStyle Style = getLLVMStyle();
9432   Style.PointerAlignment = FormatStyle::PAS_Left;
9433   verifyFormat("typedef bool* (Class::*Member)() const;", Style);
9434 }
9435 
9436 TEST_F(FormatTest, UnderstandsUnaryOperators) {
9437   verifyFormat("int a = -2;");
9438   verifyFormat("f(-1, -2, -3);");
9439   verifyFormat("a[-1] = 5;");
9440   verifyFormat("int a = 5 + -2;");
9441   verifyFormat("if (i == -1) {\n}");
9442   verifyFormat("if (i != -1) {\n}");
9443   verifyFormat("if (i > -1) {\n}");
9444   verifyFormat("if (i < -1) {\n}");
9445   verifyFormat("++(a->f());");
9446   verifyFormat("--(a->f());");
9447   verifyFormat("(a->f())++;");
9448   verifyFormat("a[42]++;");
9449   verifyFormat("if (!(a->f())) {\n}");
9450   verifyFormat("if (!+i) {\n}");
9451   verifyFormat("~&a;");
9452 
9453   verifyFormat("a-- > b;");
9454   verifyFormat("b ? -a : c;");
9455   verifyFormat("n * sizeof char16;");
9456   verifyFormat("n * alignof char16;", getGoogleStyle());
9457   verifyFormat("sizeof(char);");
9458   verifyFormat("alignof(char);", getGoogleStyle());
9459 
9460   verifyFormat("return -1;");
9461   verifyFormat("throw -1;");
9462   verifyFormat("switch (a) {\n"
9463                "case -1:\n"
9464                "  break;\n"
9465                "}");
9466   verifyFormat("#define X -1");
9467   verifyFormat("#define X -kConstant");
9468 
9469   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
9470   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
9471 
9472   verifyFormat("int a = /* confusing comment */ -1;");
9473   // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
9474   verifyFormat("int a = i /* confusing comment */++;");
9475 
9476   verifyFormat("co_yield -1;");
9477   verifyFormat("co_return -1;");
9478 
9479   // Check that * is not treated as a binary operator when we set
9480   // PointerAlignment as PAS_Left after a keyword and not a declaration.
9481   FormatStyle PASLeftStyle = getLLVMStyle();
9482   PASLeftStyle.PointerAlignment = FormatStyle::PAS_Left;
9483   verifyFormat("co_return *a;", PASLeftStyle);
9484   verifyFormat("co_await *a;", PASLeftStyle);
9485   verifyFormat("co_yield *a", PASLeftStyle);
9486   verifyFormat("return *a;", PASLeftStyle);
9487 }
9488 
9489 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) {
9490   verifyFormat("if (!aaaaaaaaaa( // break\n"
9491                "        aaaaa)) {\n"
9492                "}");
9493   verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
9494                "    aaaaa));");
9495   verifyFormat("*aaa = aaaaaaa( // break\n"
9496                "    bbbbbb);");
9497 }
9498 
9499 TEST_F(FormatTest, UnderstandsOverloadedOperators) {
9500   verifyFormat("bool operator<();");
9501   verifyFormat("bool operator>();");
9502   verifyFormat("bool operator=();");
9503   verifyFormat("bool operator==();");
9504   verifyFormat("bool operator!=();");
9505   verifyFormat("int operator+();");
9506   verifyFormat("int operator++();");
9507   verifyFormat("int operator++(int) volatile noexcept;");
9508   verifyFormat("bool operator,();");
9509   verifyFormat("bool operator();");
9510   verifyFormat("bool operator()();");
9511   verifyFormat("bool operator[]();");
9512   verifyFormat("operator bool();");
9513   verifyFormat("operator int();");
9514   verifyFormat("operator void *();");
9515   verifyFormat("operator SomeType<int>();");
9516   verifyFormat("operator SomeType<int, int>();");
9517   verifyFormat("operator SomeType<SomeType<int>>();");
9518   verifyFormat("operator< <>();");
9519   verifyFormat("operator<< <>();");
9520   verifyFormat("< <>");
9521 
9522   verifyFormat("void *operator new(std::size_t size);");
9523   verifyFormat("void *operator new[](std::size_t size);");
9524   verifyFormat("void operator delete(void *ptr);");
9525   verifyFormat("void operator delete[](void *ptr);");
9526   verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
9527                "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
9528   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n"
9529                "    aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;");
9530 
9531   verifyFormat(
9532       "ostream &operator<<(ostream &OutputStream,\n"
9533       "                    SomeReallyLongType WithSomeReallyLongValue);");
9534   verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
9535                "               const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
9536                "  return left.group < right.group;\n"
9537                "}");
9538   verifyFormat("SomeType &operator=(const SomeType &S);");
9539   verifyFormat("f.template operator()<int>();");
9540 
9541   verifyGoogleFormat("operator void*();");
9542   verifyGoogleFormat("operator SomeType<SomeType<int>>();");
9543   verifyGoogleFormat("operator ::A();");
9544 
9545   verifyFormat("using A::operator+;");
9546   verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n"
9547                "int i;");
9548 
9549   // Calling an operator as a member function.
9550   verifyFormat("void f() { a.operator*(); }");
9551   verifyFormat("void f() { a.operator*(b & b); }");
9552   verifyFormat("void f() { a->operator&(a * b); }");
9553   verifyFormat("void f() { NS::a.operator+(*b * *b); }");
9554   // TODO: Calling an operator as a non-member function is hard to distinguish.
9555   // https://llvm.org/PR50629
9556   // verifyFormat("void f() { operator*(a & a); }");
9557   // verifyFormat("void f() { operator&(a, b * b); }");
9558 
9559   verifyFormat("::operator delete(foo);");
9560   verifyFormat("::operator new(n * sizeof(foo));");
9561   verifyFormat("foo() { ::operator delete(foo); }");
9562   verifyFormat("foo() { ::operator new(n * sizeof(foo)); }");
9563 }
9564 
9565 TEST_F(FormatTest, UnderstandsFunctionRefQualification) {
9566   verifyFormat("Deleted &operator=(const Deleted &) & = default;");
9567   verifyFormat("Deleted &operator=(const Deleted &) && = delete;");
9568   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;");
9569   verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;");
9570   verifyFormat("Deleted &operator=(const Deleted &) &;");
9571   verifyFormat("Deleted &operator=(const Deleted &) &&;");
9572   verifyFormat("SomeType MemberFunction(const Deleted &) &;");
9573   verifyFormat("SomeType MemberFunction(const Deleted &) &&;");
9574   verifyFormat("SomeType MemberFunction(const Deleted &) && {}");
9575   verifyFormat("SomeType MemberFunction(const Deleted &) && final {}");
9576   verifyFormat("SomeType MemberFunction(const Deleted &) && override {}");
9577   verifyFormat("void Fn(T const &) const &;");
9578   verifyFormat("void Fn(T const volatile &&) const volatile &&;");
9579   verifyFormat("template <typename T>\n"
9580                "void F(T) && = delete;",
9581                getGoogleStyle());
9582 
9583   FormatStyle AlignLeft = getLLVMStyle();
9584   AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
9585   verifyFormat("void A::b() && {}", AlignLeft);
9586   verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft);
9587   verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;",
9588                AlignLeft);
9589   verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft);
9590   verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft);
9591   verifyFormat("auto Function(T t) & -> void {}", AlignLeft);
9592   verifyFormat("auto Function(T... t) & -> void {}", AlignLeft);
9593   verifyFormat("auto Function(T) & -> void {}", AlignLeft);
9594   verifyFormat("auto Function(T) & -> void;", AlignLeft);
9595   verifyFormat("void Fn(T const&) const&;", AlignLeft);
9596   verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft);
9597 
9598   FormatStyle Spaces = getLLVMStyle();
9599   Spaces.SpacesInCStyleCastParentheses = true;
9600   verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces);
9601   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces);
9602   verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces);
9603   verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces);
9604 
9605   Spaces.SpacesInCStyleCastParentheses = false;
9606   Spaces.SpacesInParentheses = true;
9607   verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces);
9608   verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;",
9609                Spaces);
9610   verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces);
9611   verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces);
9612 
9613   FormatStyle BreakTemplate = getLLVMStyle();
9614   BreakTemplate.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
9615 
9616   verifyFormat("struct f {\n"
9617                "  template <class T>\n"
9618                "  int &foo(const std::string &str) &noexcept {}\n"
9619                "};",
9620                BreakTemplate);
9621 
9622   verifyFormat("struct f {\n"
9623                "  template <class T>\n"
9624                "  int &foo(const std::string &str) &&noexcept {}\n"
9625                "};",
9626                BreakTemplate);
9627 
9628   verifyFormat("struct f {\n"
9629                "  template <class T>\n"
9630                "  int &foo(const std::string &str) const &noexcept {}\n"
9631                "};",
9632                BreakTemplate);
9633 
9634   verifyFormat("struct f {\n"
9635                "  template <class T>\n"
9636                "  int &foo(const std::string &str) const &noexcept {}\n"
9637                "};",
9638                BreakTemplate);
9639 
9640   verifyFormat("struct f {\n"
9641                "  template <class T>\n"
9642                "  auto foo(const std::string &str) &&noexcept -> int & {}\n"
9643                "};",
9644                BreakTemplate);
9645 
9646   FormatStyle AlignLeftBreakTemplate = getLLVMStyle();
9647   AlignLeftBreakTemplate.AlwaysBreakTemplateDeclarations =
9648       FormatStyle::BTDS_Yes;
9649   AlignLeftBreakTemplate.PointerAlignment = FormatStyle::PAS_Left;
9650 
9651   verifyFormat("struct f {\n"
9652                "  template <class T>\n"
9653                "  int& foo(const std::string& str) & noexcept {}\n"
9654                "};",
9655                AlignLeftBreakTemplate);
9656 
9657   verifyFormat("struct f {\n"
9658                "  template <class T>\n"
9659                "  int& foo(const std::string& str) && noexcept {}\n"
9660                "};",
9661                AlignLeftBreakTemplate);
9662 
9663   verifyFormat("struct f {\n"
9664                "  template <class T>\n"
9665                "  int& foo(const std::string& str) const& noexcept {}\n"
9666                "};",
9667                AlignLeftBreakTemplate);
9668 
9669   verifyFormat("struct f {\n"
9670                "  template <class T>\n"
9671                "  int& foo(const std::string& str) const&& noexcept {}\n"
9672                "};",
9673                AlignLeftBreakTemplate);
9674 
9675   verifyFormat("struct f {\n"
9676                "  template <class T>\n"
9677                "  auto foo(const std::string& str) && noexcept -> int& {}\n"
9678                "};",
9679                AlignLeftBreakTemplate);
9680 
9681   // The `&` in `Type&` should not be confused with a trailing `&` of
9682   // DEPRECATED(reason) member function.
9683   verifyFormat("struct f {\n"
9684                "  template <class T>\n"
9685                "  DEPRECATED(reason)\n"
9686                "  Type &foo(arguments) {}\n"
9687                "};",
9688                BreakTemplate);
9689 
9690   verifyFormat("struct f {\n"
9691                "  template <class T>\n"
9692                "  DEPRECATED(reason)\n"
9693                "  Type& foo(arguments) {}\n"
9694                "};",
9695                AlignLeftBreakTemplate);
9696 
9697   verifyFormat("void (*foopt)(int) = &func;");
9698 }
9699 
9700 TEST_F(FormatTest, UnderstandsNewAndDelete) {
9701   verifyFormat("void f() {\n"
9702                "  A *a = new A;\n"
9703                "  A *a = new (placement) A;\n"
9704                "  delete a;\n"
9705                "  delete (A *)a;\n"
9706                "}");
9707   verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
9708                "    typename aaaaaaaaaaaaaaaaaaaaaaaa();");
9709   verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
9710                "    new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
9711                "        typename aaaaaaaaaaaaaaaaaaaaaaaa();");
9712   verifyFormat("delete[] h->p;");
9713 
9714   verifyFormat("void operator delete(void *foo) ATTRIB;");
9715   verifyFormat("void operator new(void *foo) ATTRIB;");
9716   verifyFormat("void operator delete[](void *foo) ATTRIB;");
9717   verifyFormat("void operator delete(void *ptr) noexcept;");
9718 }
9719 
9720 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
9721   verifyFormat("int *f(int *a) {}");
9722   verifyFormat("int main(int argc, char **argv) {}");
9723   verifyFormat("Test::Test(int b) : a(b * b) {}");
9724   verifyIndependentOfContext("f(a, *a);");
9725   verifyFormat("void g() { f(*a); }");
9726   verifyIndependentOfContext("int a = b * 10;");
9727   verifyIndependentOfContext("int a = 10 * b;");
9728   verifyIndependentOfContext("int a = b * c;");
9729   verifyIndependentOfContext("int a += b * c;");
9730   verifyIndependentOfContext("int a -= b * c;");
9731   verifyIndependentOfContext("int a *= b * c;");
9732   verifyIndependentOfContext("int a /= b * c;");
9733   verifyIndependentOfContext("int a = *b;");
9734   verifyIndependentOfContext("int a = *b * c;");
9735   verifyIndependentOfContext("int a = b * *c;");
9736   verifyIndependentOfContext("int a = b * (10);");
9737   verifyIndependentOfContext("S << b * (10);");
9738   verifyIndependentOfContext("return 10 * b;");
9739   verifyIndependentOfContext("return *b * *c;");
9740   verifyIndependentOfContext("return a & ~b;");
9741   verifyIndependentOfContext("f(b ? *c : *d);");
9742   verifyIndependentOfContext("int a = b ? *c : *d;");
9743   verifyIndependentOfContext("*b = a;");
9744   verifyIndependentOfContext("a * ~b;");
9745   verifyIndependentOfContext("a * !b;");
9746   verifyIndependentOfContext("a * +b;");
9747   verifyIndependentOfContext("a * -b;");
9748   verifyIndependentOfContext("a * ++b;");
9749   verifyIndependentOfContext("a * --b;");
9750   verifyIndependentOfContext("a[4] * b;");
9751   verifyIndependentOfContext("a[a * a] = 1;");
9752   verifyIndependentOfContext("f() * b;");
9753   verifyIndependentOfContext("a * [self dostuff];");
9754   verifyIndependentOfContext("int x = a * (a + b);");
9755   verifyIndependentOfContext("(a *)(a + b);");
9756   verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
9757   verifyIndependentOfContext("int *pa = (int *)&a;");
9758   verifyIndependentOfContext("return sizeof(int **);");
9759   verifyIndependentOfContext("return sizeof(int ******);");
9760   verifyIndependentOfContext("return (int **&)a;");
9761   verifyIndependentOfContext("f((*PointerToArray)[10]);");
9762   verifyFormat("void f(Type (*parameter)[10]) {}");
9763   verifyFormat("void f(Type (&parameter)[10]) {}");
9764   verifyGoogleFormat("return sizeof(int**);");
9765   verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
9766   verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
9767   verifyFormat("auto a = [](int **&, int ***) {};");
9768   verifyFormat("auto PointerBinding = [](const char *S) {};");
9769   verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
9770   verifyFormat("[](const decltype(*a) &value) {}");
9771   verifyFormat("[](const typeof(*a) &value) {}");
9772   verifyFormat("[](const _Atomic(a *) &value) {}");
9773   verifyFormat("[](const __underlying_type(a) &value) {}");
9774   verifyFormat("decltype(a * b) F();");
9775   verifyFormat("typeof(a * b) F();");
9776   verifyFormat("#define MACRO() [](A *a) { return 1; }");
9777   verifyFormat("Constructor() : member([](A *a, B *b) {}) {}");
9778   verifyIndependentOfContext("typedef void (*f)(int *a);");
9779   verifyIndependentOfContext("int i{a * b};");
9780   verifyIndependentOfContext("aaa && aaa->f();");
9781   verifyIndependentOfContext("int x = ~*p;");
9782   verifyFormat("Constructor() : a(a), area(width * height) {}");
9783   verifyFormat("Constructor() : a(a), area(a, width * height) {}");
9784   verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}");
9785   verifyFormat("void f() { f(a, c * d); }");
9786   verifyFormat("void f() { f(new a(), c * d); }");
9787   verifyFormat("void f(const MyOverride &override);");
9788   verifyFormat("void f(const MyFinal &final);");
9789   verifyIndependentOfContext("bool a = f() && override.f();");
9790   verifyIndependentOfContext("bool a = f() && final.f();");
9791 
9792   verifyIndependentOfContext("InvalidRegions[*R] = 0;");
9793 
9794   verifyIndependentOfContext("A<int *> a;");
9795   verifyIndependentOfContext("A<int **> a;");
9796   verifyIndependentOfContext("A<int *, int *> a;");
9797   verifyIndependentOfContext("A<int *[]> a;");
9798   verifyIndependentOfContext(
9799       "const char *const p = reinterpret_cast<const char *const>(q);");
9800   verifyIndependentOfContext("A<int **, int **> a;");
9801   verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
9802   verifyFormat("for (char **a = b; *a; ++a) {\n}");
9803   verifyFormat("for (; a && b;) {\n}");
9804   verifyFormat("bool foo = true && [] { return false; }();");
9805 
9806   verifyFormat(
9807       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9808       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9809 
9810   verifyGoogleFormat("int const* a = &b;");
9811   verifyGoogleFormat("**outparam = 1;");
9812   verifyGoogleFormat("*outparam = a * b;");
9813   verifyGoogleFormat("int main(int argc, char** argv) {}");
9814   verifyGoogleFormat("A<int*> a;");
9815   verifyGoogleFormat("A<int**> a;");
9816   verifyGoogleFormat("A<int*, int*> a;");
9817   verifyGoogleFormat("A<int**, int**> a;");
9818   verifyGoogleFormat("f(b ? *c : *d);");
9819   verifyGoogleFormat("int a = b ? *c : *d;");
9820   verifyGoogleFormat("Type* t = **x;");
9821   verifyGoogleFormat("Type* t = *++*x;");
9822   verifyGoogleFormat("*++*x;");
9823   verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
9824   verifyGoogleFormat("Type* t = x++ * y;");
9825   verifyGoogleFormat(
9826       "const char* const p = reinterpret_cast<const char* const>(q);");
9827   verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
9828   verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
9829   verifyGoogleFormat("template <typename T>\n"
9830                      "void f(int i = 0, SomeType** temps = NULL);");
9831 
9832   FormatStyle Left = getLLVMStyle();
9833   Left.PointerAlignment = FormatStyle::PAS_Left;
9834   verifyFormat("x = *a(x) = *a(y);", Left);
9835   verifyFormat("for (;; *a = b) {\n}", Left);
9836   verifyFormat("return *this += 1;", Left);
9837   verifyFormat("throw *x;", Left);
9838   verifyFormat("delete *x;", Left);
9839   verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left);
9840   verifyFormat("[](const decltype(*a)* ptr) {}", Left);
9841   verifyFormat("[](const typeof(*a)* ptr) {}", Left);
9842   verifyFormat("[](const _Atomic(a*)* ptr) {}", Left);
9843   verifyFormat("[](const __underlying_type(a)* ptr) {}", Left);
9844   verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left);
9845   verifyFormat("auto x(A&&, B&&, C&&) -> D;", Left);
9846   verifyFormat("auto x = [](A&&, B&&, C&&) -> D {};", Left);
9847   verifyFormat("template <class T> X(T&&, T&&, T&&) -> X<T>;", Left);
9848 
9849   verifyIndependentOfContext("a = *(x + y);");
9850   verifyIndependentOfContext("a = &(x + y);");
9851   verifyIndependentOfContext("*(x + y).call();");
9852   verifyIndependentOfContext("&(x + y)->call();");
9853   verifyFormat("void f() { &(*I).first; }");
9854 
9855   verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
9856   verifyFormat("f(* /* confusing comment */ foo);");
9857   verifyFormat("void (* /*deleter*/)(const Slice &key, void *value)");
9858   verifyFormat("void foo(int * // this is the first paramters\n"
9859                "         ,\n"
9860                "         int second);");
9861   verifyFormat("double term = a * // first\n"
9862                "              b;");
9863   verifyFormat(
9864       "int *MyValues = {\n"
9865       "    *A, // Operator detection might be confused by the '{'\n"
9866       "    *BB // Operator detection might be confused by previous comment\n"
9867       "};");
9868 
9869   verifyIndependentOfContext("if (int *a = &b)");
9870   verifyIndependentOfContext("if (int &a = *b)");
9871   verifyIndependentOfContext("if (a & b[i])");
9872   verifyIndependentOfContext("if constexpr (a & b[i])");
9873   verifyIndependentOfContext("if CONSTEXPR (a & b[i])");
9874   verifyIndependentOfContext("if (a * (b * c))");
9875   verifyIndependentOfContext("if constexpr (a * (b * c))");
9876   verifyIndependentOfContext("if CONSTEXPR (a * (b * c))");
9877   verifyIndependentOfContext("if (a::b::c::d & b[i])");
9878   verifyIndependentOfContext("if (*b[i])");
9879   verifyIndependentOfContext("if (int *a = (&b))");
9880   verifyIndependentOfContext("while (int *a = &b)");
9881   verifyIndependentOfContext("while (a * (b * c))");
9882   verifyIndependentOfContext("size = sizeof *a;");
9883   verifyIndependentOfContext("if (a && (b = c))");
9884   verifyFormat("void f() {\n"
9885                "  for (const int &v : Values) {\n"
9886                "  }\n"
9887                "}");
9888   verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
9889   verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
9890   verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
9891 
9892   verifyFormat("#define A (!a * b)");
9893   verifyFormat("#define MACRO     \\\n"
9894                "  int *i = a * b; \\\n"
9895                "  void f(a *b);",
9896                getLLVMStyleWithColumns(19));
9897 
9898   verifyIndependentOfContext("A = new SomeType *[Length];");
9899   verifyIndependentOfContext("A = new SomeType *[Length]();");
9900   verifyIndependentOfContext("T **t = new T *;");
9901   verifyIndependentOfContext("T **t = new T *();");
9902   verifyGoogleFormat("A = new SomeType*[Length]();");
9903   verifyGoogleFormat("A = new SomeType*[Length];");
9904   verifyGoogleFormat("T** t = new T*;");
9905   verifyGoogleFormat("T** t = new T*();");
9906 
9907   verifyFormat("STATIC_ASSERT((a & b) == 0);");
9908   verifyFormat("STATIC_ASSERT(0 == (a & b));");
9909   verifyFormat("template <bool a, bool b> "
9910                "typename t::if<x && y>::type f() {}");
9911   verifyFormat("template <int *y> f() {}");
9912   verifyFormat("vector<int *> v;");
9913   verifyFormat("vector<int *const> v;");
9914   verifyFormat("vector<int *const **const *> v;");
9915   verifyFormat("vector<int *volatile> v;");
9916   verifyFormat("vector<a *_Nonnull> v;");
9917   verifyFormat("vector<a *_Nullable> v;");
9918   verifyFormat("vector<a *_Null_unspecified> v;");
9919   verifyFormat("vector<a *__ptr32> v;");
9920   verifyFormat("vector<a *__ptr64> v;");
9921   verifyFormat("vector<a *__capability> v;");
9922   FormatStyle TypeMacros = getLLVMStyle();
9923   TypeMacros.TypenameMacros = {"LIST"};
9924   verifyFormat("vector<LIST(uint64_t)> v;", TypeMacros);
9925   verifyFormat("vector<LIST(uint64_t) *> v;", TypeMacros);
9926   verifyFormat("vector<LIST(uint64_t) **> v;", TypeMacros);
9927   verifyFormat("vector<LIST(uint64_t) *attr> v;", TypeMacros);
9928   verifyFormat("vector<A(uint64_t) * attr> v;", TypeMacros); // multiplication
9929 
9930   FormatStyle CustomQualifier = getLLVMStyle();
9931   // Add identifiers that should not be parsed as a qualifier by default.
9932   CustomQualifier.AttributeMacros.push_back("__my_qualifier");
9933   CustomQualifier.AttributeMacros.push_back("_My_qualifier");
9934   CustomQualifier.AttributeMacros.push_back("my_other_qualifier");
9935   verifyFormat("vector<a * __my_qualifier> parse_as_multiply;");
9936   verifyFormat("vector<a *__my_qualifier> v;", CustomQualifier);
9937   verifyFormat("vector<a * _My_qualifier> parse_as_multiply;");
9938   verifyFormat("vector<a *_My_qualifier> v;", CustomQualifier);
9939   verifyFormat("vector<a * my_other_qualifier> parse_as_multiply;");
9940   verifyFormat("vector<a *my_other_qualifier> v;", CustomQualifier);
9941   verifyFormat("vector<a * _NotAQualifier> v;");
9942   verifyFormat("vector<a * __not_a_qualifier> v;");
9943   verifyFormat("vector<a * b> v;");
9944   verifyFormat("foo<b && false>();");
9945   verifyFormat("foo<b & 1>();");
9946   verifyFormat("decltype(*::std::declval<const T &>()) void F();");
9947   verifyFormat("typeof(*::std::declval<const T &>()) void F();");
9948   verifyFormat("_Atomic(*::std::declval<const T &>()) void F();");
9949   verifyFormat("__underlying_type(*::std::declval<const T &>()) void F();");
9950   verifyFormat(
9951       "template <class T, class = typename std::enable_if<\n"
9952       "                       std::is_integral<T>::value &&\n"
9953       "                       (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
9954       "void F();",
9955       getLLVMStyleWithColumns(70));
9956   verifyFormat("template <class T,\n"
9957                "          class = typename std::enable_if<\n"
9958                "              std::is_integral<T>::value &&\n"
9959                "              (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n"
9960                "          class U>\n"
9961                "void F();",
9962                getLLVMStyleWithColumns(70));
9963   verifyFormat(
9964       "template <class T,\n"
9965       "          class = typename ::std::enable_if<\n"
9966       "              ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
9967       "void F();",
9968       getGoogleStyleWithColumns(68));
9969 
9970   verifyIndependentOfContext("MACRO(int *i);");
9971   verifyIndependentOfContext("MACRO(auto *a);");
9972   verifyIndependentOfContext("MACRO(const A *a);");
9973   verifyIndependentOfContext("MACRO(_Atomic(A) *a);");
9974   verifyIndependentOfContext("MACRO(decltype(A) *a);");
9975   verifyIndependentOfContext("MACRO(typeof(A) *a);");
9976   verifyIndependentOfContext("MACRO(__underlying_type(A) *a);");
9977   verifyIndependentOfContext("MACRO(A *const a);");
9978   verifyIndependentOfContext("MACRO(A *restrict a);");
9979   verifyIndependentOfContext("MACRO(A *__restrict__ a);");
9980   verifyIndependentOfContext("MACRO(A *__restrict a);");
9981   verifyIndependentOfContext("MACRO(A *volatile a);");
9982   verifyIndependentOfContext("MACRO(A *__volatile a);");
9983   verifyIndependentOfContext("MACRO(A *__volatile__ a);");
9984   verifyIndependentOfContext("MACRO(A *_Nonnull a);");
9985   verifyIndependentOfContext("MACRO(A *_Nullable a);");
9986   verifyIndependentOfContext("MACRO(A *_Null_unspecified a);");
9987   verifyIndependentOfContext("MACRO(A *__attribute__((foo)) a);");
9988   verifyIndependentOfContext("MACRO(A *__attribute((foo)) a);");
9989   verifyIndependentOfContext("MACRO(A *[[clang::attr]] a);");
9990   verifyIndependentOfContext("MACRO(A *[[clang::attr(\"foo\")]] a);");
9991   verifyIndependentOfContext("MACRO(A *__ptr32 a);");
9992   verifyIndependentOfContext("MACRO(A *__ptr64 a);");
9993   verifyIndependentOfContext("MACRO(A *__capability);");
9994   verifyIndependentOfContext("MACRO(A &__capability);");
9995   verifyFormat("MACRO(A *__my_qualifier);");               // type declaration
9996   verifyFormat("void f() { MACRO(A * __my_qualifier); }"); // multiplication
9997   // If we add __my_qualifier to AttributeMacros it should always be parsed as
9998   // a type declaration:
9999   verifyFormat("MACRO(A *__my_qualifier);", CustomQualifier);
10000   verifyFormat("void f() { MACRO(A *__my_qualifier); }", CustomQualifier);
10001   // Also check that TypenameMacros prevents parsing it as multiplication:
10002   verifyIndependentOfContext("MACRO(LIST(uint64_t) * a);"); // multiplication
10003   verifyIndependentOfContext("MACRO(LIST(uint64_t) *a);", TypeMacros); // type
10004 
10005   verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
10006   verifyFormat("void f() { f(float{1}, a * a); }");
10007   verifyFormat("void f() { f(float(1), a * a); }");
10008 
10009   verifyFormat("f((void (*)(int))g);");
10010   verifyFormat("f((void (&)(int))g);");
10011   verifyFormat("f((void (^)(int))g);");
10012 
10013   // FIXME: Is there a way to make this work?
10014   // verifyIndependentOfContext("MACRO(A *a);");
10015   verifyFormat("MACRO(A &B);");
10016   verifyFormat("MACRO(A *B);");
10017   verifyFormat("void f() { MACRO(A * B); }");
10018   verifyFormat("void f() { MACRO(A & B); }");
10019 
10020   // This lambda was mis-formatted after D88956 (treating it as a binop):
10021   verifyFormat("auto x = [](const decltype(x) &ptr) {};");
10022   verifyFormat("auto x = [](const decltype(x) *ptr) {};");
10023   verifyFormat("#define lambda [](const decltype(x) &ptr) {}");
10024   verifyFormat("#define lambda [](const decltype(x) *ptr) {}");
10025 
10026   verifyFormat("DatumHandle const *operator->() const { return input_; }");
10027   verifyFormat("return options != nullptr && operator==(*options);");
10028 
10029   EXPECT_EQ("#define OP(x)                                    \\\n"
10030             "  ostream &operator<<(ostream &s, const A &a) {  \\\n"
10031             "    return s << a.DebugString();                 \\\n"
10032             "  }",
10033             format("#define OP(x) \\\n"
10034                    "  ostream &operator<<(ostream &s, const A &a) { \\\n"
10035                    "    return s << a.DebugString(); \\\n"
10036                    "  }",
10037                    getLLVMStyleWithColumns(50)));
10038 
10039   // FIXME: We cannot handle this case yet; we might be able to figure out that
10040   // foo<x> d > v; doesn't make sense.
10041   verifyFormat("foo<a<b && c> d> v;");
10042 
10043   FormatStyle PointerMiddle = getLLVMStyle();
10044   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
10045   verifyFormat("delete *x;", PointerMiddle);
10046   verifyFormat("int * x;", PointerMiddle);
10047   verifyFormat("int *[] x;", PointerMiddle);
10048   verifyFormat("template <int * y> f() {}", PointerMiddle);
10049   verifyFormat("int * f(int * a) {}", PointerMiddle);
10050   verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle);
10051   verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle);
10052   verifyFormat("A<int *> a;", PointerMiddle);
10053   verifyFormat("A<int **> a;", PointerMiddle);
10054   verifyFormat("A<int *, int *> a;", PointerMiddle);
10055   verifyFormat("A<int *[]> a;", PointerMiddle);
10056   verifyFormat("A = new SomeType *[Length]();", PointerMiddle);
10057   verifyFormat("A = new SomeType *[Length];", PointerMiddle);
10058   verifyFormat("T ** t = new T *;", PointerMiddle);
10059 
10060   // Member function reference qualifiers aren't binary operators.
10061   verifyFormat("string // break\n"
10062                "operator()() & {}");
10063   verifyFormat("string // break\n"
10064                "operator()() && {}");
10065   verifyGoogleFormat("template <typename T>\n"
10066                      "auto x() & -> int {}");
10067 
10068   // Should be binary operators when used as an argument expression (overloaded
10069   // operator invoked as a member function).
10070   verifyFormat("void f() { a.operator()(a * a); }");
10071   verifyFormat("void f() { a->operator()(a & a); }");
10072   verifyFormat("void f() { a.operator()(*a & *a); }");
10073   verifyFormat("void f() { a->operator()(*a * *a); }");
10074 
10075   verifyFormat("int operator()(T (&&)[N]) { return 1; }");
10076   verifyFormat("int operator()(T (&)[N]) { return 0; }");
10077 }
10078 
10079 TEST_F(FormatTest, UnderstandsAttributes) {
10080   verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
10081   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
10082                "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
10083   verifyFormat("__attribute__((nodebug)) ::qualified_type f();");
10084   FormatStyle AfterType = getLLVMStyle();
10085   AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
10086   verifyFormat("__attribute__((nodebug)) void\n"
10087                "foo() {}\n",
10088                AfterType);
10089   verifyFormat("__unused void\n"
10090                "foo() {}",
10091                AfterType);
10092 
10093   FormatStyle CustomAttrs = getLLVMStyle();
10094   CustomAttrs.AttributeMacros.push_back("__unused");
10095   CustomAttrs.AttributeMacros.push_back("__attr1");
10096   CustomAttrs.AttributeMacros.push_back("__attr2");
10097   CustomAttrs.AttributeMacros.push_back("no_underscore_attr");
10098   verifyFormat("vector<SomeType *__attribute((foo))> v;");
10099   verifyFormat("vector<SomeType *__attribute__((foo))> v;");
10100   verifyFormat("vector<SomeType * __not_attribute__((foo))> v;");
10101   // Check that it is parsed as a multiplication without AttributeMacros and
10102   // as a pointer qualifier when we add __attr1/__attr2 to AttributeMacros.
10103   verifyFormat("vector<SomeType * __attr1> v;");
10104   verifyFormat("vector<SomeType __attr1 *> v;");
10105   verifyFormat("vector<SomeType __attr1 *const> v;");
10106   verifyFormat("vector<SomeType __attr1 * __attr2> v;");
10107   verifyFormat("vector<SomeType *__attr1> v;", CustomAttrs);
10108   verifyFormat("vector<SomeType *__attr2> v;", CustomAttrs);
10109   verifyFormat("vector<SomeType *no_underscore_attr> v;", CustomAttrs);
10110   verifyFormat("vector<SomeType __attr1 *> v;", CustomAttrs);
10111   verifyFormat("vector<SomeType __attr1 *const> v;", CustomAttrs);
10112   verifyFormat("vector<SomeType __attr1 *__attr2> v;", CustomAttrs);
10113   verifyFormat("vector<SomeType __attr1 *no_underscore_attr> v;", CustomAttrs);
10114 
10115   // Check that these are not parsed as function declarations:
10116   CustomAttrs.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
10117   CustomAttrs.BreakBeforeBraces = FormatStyle::BS_Allman;
10118   verifyFormat("SomeType s(InitValue);", CustomAttrs);
10119   verifyFormat("SomeType s{InitValue};", CustomAttrs);
10120   verifyFormat("SomeType *__unused s(InitValue);", CustomAttrs);
10121   verifyFormat("SomeType *__unused s{InitValue};", CustomAttrs);
10122   verifyFormat("SomeType s __unused(InitValue);", CustomAttrs);
10123   verifyFormat("SomeType s __unused{InitValue};", CustomAttrs);
10124   verifyFormat("SomeType *__capability s(InitValue);", CustomAttrs);
10125   verifyFormat("SomeType *__capability s{InitValue};", CustomAttrs);
10126 }
10127 
10128 TEST_F(FormatTest, UnderstandsPointerQualifiersInCast) {
10129   // Check that qualifiers on pointers don't break parsing of casts.
10130   verifyFormat("x = (foo *const)*v;");
10131   verifyFormat("x = (foo *volatile)*v;");
10132   verifyFormat("x = (foo *restrict)*v;");
10133   verifyFormat("x = (foo *__attribute__((foo)))*v;");
10134   verifyFormat("x = (foo *_Nonnull)*v;");
10135   verifyFormat("x = (foo *_Nullable)*v;");
10136   verifyFormat("x = (foo *_Null_unspecified)*v;");
10137   verifyFormat("x = (foo *_Nonnull)*v;");
10138   verifyFormat("x = (foo *[[clang::attr]])*v;");
10139   verifyFormat("x = (foo *[[clang::attr(\"foo\")]])*v;");
10140   verifyFormat("x = (foo *__ptr32)*v;");
10141   verifyFormat("x = (foo *__ptr64)*v;");
10142   verifyFormat("x = (foo *__capability)*v;");
10143 
10144   // Check that we handle multiple trailing qualifiers and skip them all to
10145   // determine that the expression is a cast to a pointer type.
10146   FormatStyle LongPointerRight = getLLVMStyleWithColumns(999);
10147   FormatStyle LongPointerLeft = getLLVMStyleWithColumns(999);
10148   LongPointerLeft.PointerAlignment = FormatStyle::PAS_Left;
10149   StringRef AllQualifiers =
10150       "const volatile restrict __attribute__((foo)) _Nonnull _Null_unspecified "
10151       "_Nonnull [[clang::attr]] __ptr32 __ptr64 __capability";
10152   verifyFormat(("x = (foo *" + AllQualifiers + ")*v;").str(), LongPointerRight);
10153   verifyFormat(("x = (foo* " + AllQualifiers + ")*v;").str(), LongPointerLeft);
10154 
10155   // Also check that address-of is not parsed as a binary bitwise-and:
10156   verifyFormat("x = (foo *const)&v;");
10157   verifyFormat(("x = (foo *" + AllQualifiers + ")&v;").str(), LongPointerRight);
10158   verifyFormat(("x = (foo* " + AllQualifiers + ")&v;").str(), LongPointerLeft);
10159 
10160   // Check custom qualifiers:
10161   FormatStyle CustomQualifier = getLLVMStyleWithColumns(999);
10162   CustomQualifier.AttributeMacros.push_back("__my_qualifier");
10163   verifyFormat("x = (foo * __my_qualifier) * v;"); // not parsed as qualifier.
10164   verifyFormat("x = (foo *__my_qualifier)*v;", CustomQualifier);
10165   verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)*v;").str(),
10166                CustomQualifier);
10167   verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)&v;").str(),
10168                CustomQualifier);
10169 
10170   // Check that unknown identifiers result in binary operator parsing:
10171   verifyFormat("x = (foo * __unknown_qualifier) * v;");
10172   verifyFormat("x = (foo * __unknown_qualifier) & v;");
10173 }
10174 
10175 TEST_F(FormatTest, UnderstandsSquareAttributes) {
10176   verifyFormat("SomeType s [[unused]] (InitValue);");
10177   verifyFormat("SomeType s [[gnu::unused]] (InitValue);");
10178   verifyFormat("SomeType s [[using gnu: unused]] (InitValue);");
10179   verifyFormat("[[gsl::suppress(\"clang-tidy-check-name\")]] void f() {}");
10180   verifyFormat("void f() [[deprecated(\"so sorry\")]];");
10181   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10182                "    [[unused]] aaaaaaaaaaaaaaaaaaaaaaa(int i);");
10183   verifyFormat("[[nodiscard]] bool f() { return false; }");
10184   verifyFormat("class [[nodiscard]] f {\npublic:\n  f() {}\n}");
10185   verifyFormat("class [[deprecated(\"so sorry\")]] f {\npublic:\n  f() {}\n}");
10186   verifyFormat("class [[gnu::unused]] f {\npublic:\n  f() {}\n}");
10187   verifyFormat("[[nodiscard]] ::qualified_type f();");
10188 
10189   // Make sure we do not mistake attributes for array subscripts.
10190   verifyFormat("int a() {}\n"
10191                "[[unused]] int b() {}\n");
10192   verifyFormat("NSArray *arr;\n"
10193                "arr[[Foo() bar]];");
10194 
10195   // On the other hand, we still need to correctly find array subscripts.
10196   verifyFormat("int a = std::vector<int>{1, 2, 3}[0];");
10197 
10198   // Make sure that we do not mistake Objective-C method inside array literals
10199   // as attributes, even if those method names are also keywords.
10200   verifyFormat("@[ [foo bar] ];");
10201   verifyFormat("@[ [NSArray class] ];");
10202   verifyFormat("@[ [foo enum] ];");
10203 
10204   verifyFormat("template <typename T> [[nodiscard]] int a() { return 1; }");
10205 
10206   // Make sure we do not parse attributes as lambda introducers.
10207   FormatStyle MultiLineFunctions = getLLVMStyle();
10208   MultiLineFunctions.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
10209   verifyFormat("[[unused]] int b() {\n"
10210                "  return 42;\n"
10211                "}\n",
10212                MultiLineFunctions);
10213 }
10214 
10215 TEST_F(FormatTest, AttributeClass) {
10216   FormatStyle Style = getChromiumStyle(FormatStyle::LK_Cpp);
10217   verifyFormat("class S {\n"
10218                "  S(S&&) = default;\n"
10219                "};",
10220                Style);
10221   verifyFormat("class [[nodiscard]] S {\n"
10222                "  S(S&&) = default;\n"
10223                "};",
10224                Style);
10225   verifyFormat("class __attribute((maybeunused)) S {\n"
10226                "  S(S&&) = default;\n"
10227                "};",
10228                Style);
10229   verifyFormat("struct S {\n"
10230                "  S(S&&) = default;\n"
10231                "};",
10232                Style);
10233   verifyFormat("struct [[nodiscard]] S {\n"
10234                "  S(S&&) = default;\n"
10235                "};",
10236                Style);
10237 }
10238 
10239 TEST_F(FormatTest, AttributesAfterMacro) {
10240   FormatStyle Style = getLLVMStyle();
10241   verifyFormat("MACRO;\n"
10242                "__attribute__((maybe_unused)) int foo() {\n"
10243                "  //...\n"
10244                "}");
10245 
10246   verifyFormat("MACRO;\n"
10247                "[[nodiscard]] int foo() {\n"
10248                "  //...\n"
10249                "}");
10250 
10251   EXPECT_EQ("MACRO\n\n"
10252             "__attribute__((maybe_unused)) int foo() {\n"
10253             "  //...\n"
10254             "}",
10255             format("MACRO\n\n"
10256                    "__attribute__((maybe_unused)) int foo() {\n"
10257                    "  //...\n"
10258                    "}"));
10259 
10260   EXPECT_EQ("MACRO\n\n"
10261             "[[nodiscard]] int foo() {\n"
10262             "  //...\n"
10263             "}",
10264             format("MACRO\n\n"
10265                    "[[nodiscard]] int foo() {\n"
10266                    "  //...\n"
10267                    "}"));
10268 }
10269 
10270 TEST_F(FormatTest, AttributePenaltyBreaking) {
10271   FormatStyle Style = getLLVMStyle();
10272   verifyFormat("void ABCDEFGH::ABCDEFGHIJKLMN(\n"
10273                "    [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}",
10274                Style);
10275   verifyFormat("void ABCDEFGH::ABCDEFGHIJK(\n"
10276                "    [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}",
10277                Style);
10278   verifyFormat("void ABCDEFGH::ABCDEFGH([[maybe_unused]] const "
10279                "shared_ptr<ALongTypeName> &C d) {\n}",
10280                Style);
10281 }
10282 
10283 TEST_F(FormatTest, UnderstandsEllipsis) {
10284   FormatStyle Style = getLLVMStyle();
10285   verifyFormat("int printf(const char *fmt, ...);");
10286   verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
10287   verifyFormat("template <class... Ts> void Foo(Ts *...ts) {}");
10288 
10289   verifyFormat("template <int *...PP> a;", Style);
10290 
10291   Style.PointerAlignment = FormatStyle::PAS_Left;
10292   verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", Style);
10293 
10294   verifyFormat("template <int*... PP> a;", Style);
10295 
10296   Style.PointerAlignment = FormatStyle::PAS_Middle;
10297   verifyFormat("template <int *... PP> a;", Style);
10298 }
10299 
10300 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
10301   EXPECT_EQ("int *a;\n"
10302             "int *a;\n"
10303             "int *a;",
10304             format("int *a;\n"
10305                    "int* a;\n"
10306                    "int *a;",
10307                    getGoogleStyle()));
10308   EXPECT_EQ("int* a;\n"
10309             "int* a;\n"
10310             "int* a;",
10311             format("int* a;\n"
10312                    "int* a;\n"
10313                    "int *a;",
10314                    getGoogleStyle()));
10315   EXPECT_EQ("int *a;\n"
10316             "int *a;\n"
10317             "int *a;",
10318             format("int *a;\n"
10319                    "int * a;\n"
10320                    "int *  a;",
10321                    getGoogleStyle()));
10322   EXPECT_EQ("auto x = [] {\n"
10323             "  int *a;\n"
10324             "  int *a;\n"
10325             "  int *a;\n"
10326             "};",
10327             format("auto x=[]{int *a;\n"
10328                    "int * a;\n"
10329                    "int *  a;};",
10330                    getGoogleStyle()));
10331 }
10332 
10333 TEST_F(FormatTest, UnderstandsRvalueReferences) {
10334   verifyFormat("int f(int &&a) {}");
10335   verifyFormat("int f(int a, char &&b) {}");
10336   verifyFormat("void f() { int &&a = b; }");
10337   verifyGoogleFormat("int f(int a, char&& b) {}");
10338   verifyGoogleFormat("void f() { int&& a = b; }");
10339 
10340   verifyIndependentOfContext("A<int &&> a;");
10341   verifyIndependentOfContext("A<int &&, int &&> a;");
10342   verifyGoogleFormat("A<int&&> a;");
10343   verifyGoogleFormat("A<int&&, int&&> a;");
10344 
10345   // Not rvalue references:
10346   verifyFormat("template <bool B, bool C> class A {\n"
10347                "  static_assert(B && C, \"Something is wrong\");\n"
10348                "};");
10349   verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
10350   verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
10351   verifyFormat("#define A(a, b) (a && b)");
10352 }
10353 
10354 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
10355   verifyFormat("void f() {\n"
10356                "  x[aaaaaaaaa -\n"
10357                "    b] = 23;\n"
10358                "}",
10359                getLLVMStyleWithColumns(15));
10360 }
10361 
10362 TEST_F(FormatTest, FormatsCasts) {
10363   verifyFormat("Type *A = static_cast<Type *>(P);");
10364   verifyFormat("Type *A = (Type *)P;");
10365   verifyFormat("Type *A = (vector<Type *, int *>)P;");
10366   verifyFormat("int a = (int)(2.0f);");
10367   verifyFormat("int a = (int)2.0f;");
10368   verifyFormat("x[(int32)y];");
10369   verifyFormat("x = (int32)y;");
10370   verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
10371   verifyFormat("int a = (int)*b;");
10372   verifyFormat("int a = (int)2.0f;");
10373   verifyFormat("int a = (int)~0;");
10374   verifyFormat("int a = (int)++a;");
10375   verifyFormat("int a = (int)sizeof(int);");
10376   verifyFormat("int a = (int)+2;");
10377   verifyFormat("my_int a = (my_int)2.0f;");
10378   verifyFormat("my_int a = (my_int)sizeof(int);");
10379   verifyFormat("return (my_int)aaa;");
10380   verifyFormat("#define x ((int)-1)");
10381   verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
10382   verifyFormat("#define p(q) ((int *)&q)");
10383   verifyFormat("fn(a)(b) + 1;");
10384 
10385   verifyFormat("void f() { my_int a = (my_int)*b; }");
10386   verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
10387   verifyFormat("my_int a = (my_int)~0;");
10388   verifyFormat("my_int a = (my_int)++a;");
10389   verifyFormat("my_int a = (my_int)-2;");
10390   verifyFormat("my_int a = (my_int)1;");
10391   verifyFormat("my_int a = (my_int *)1;");
10392   verifyFormat("my_int a = (const my_int)-1;");
10393   verifyFormat("my_int a = (const my_int *)-1;");
10394   verifyFormat("my_int a = (my_int)(my_int)-1;");
10395   verifyFormat("my_int a = (ns::my_int)-2;");
10396   verifyFormat("case (my_int)ONE:");
10397   verifyFormat("auto x = (X)this;");
10398   // Casts in Obj-C style calls used to not be recognized as such.
10399   verifyFormat("int a = [(type*)[((type*)val) arg] arg];", getGoogleStyle());
10400 
10401   // FIXME: single value wrapped with paren will be treated as cast.
10402   verifyFormat("void f(int i = (kValue)*kMask) {}");
10403 
10404   verifyFormat("{ (void)F; }");
10405 
10406   // Don't break after a cast's
10407   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
10408                "    (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
10409                "                                   bbbbbbbbbbbbbbbbbbbbbb);");
10410 
10411   verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(x)");
10412   verifyFormat("#define CONF_BOOL(x) (bool *)(x)");
10413   verifyFormat("#define CONF_BOOL(x) (bool)(x)");
10414   verifyFormat("bool *y = (bool *)(void *)(x);");
10415   verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(int)(x)");
10416   verifyFormat("bool *y = (bool *)(void *)(int)(x);");
10417   verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(int)foo(x)");
10418   verifyFormat("bool *y = (bool *)(void *)(int)foo(x);");
10419 
10420   // These are not casts.
10421   verifyFormat("void f(int *) {}");
10422   verifyFormat("f(foo)->b;");
10423   verifyFormat("f(foo).b;");
10424   verifyFormat("f(foo)(b);");
10425   verifyFormat("f(foo)[b];");
10426   verifyFormat("[](foo) { return 4; }(bar);");
10427   verifyFormat("(*funptr)(foo)[4];");
10428   verifyFormat("funptrs[4](foo)[4];");
10429   verifyFormat("void f(int *);");
10430   verifyFormat("void f(int *) = 0;");
10431   verifyFormat("void f(SmallVector<int>) {}");
10432   verifyFormat("void f(SmallVector<int>);");
10433   verifyFormat("void f(SmallVector<int>) = 0;");
10434   verifyFormat("void f(int i = (kA * kB) & kMask) {}");
10435   verifyFormat("int a = sizeof(int) * b;");
10436   verifyFormat("int a = alignof(int) * b;", getGoogleStyle());
10437   verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
10438   verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
10439   verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
10440 
10441   // These are not casts, but at some point were confused with casts.
10442   verifyFormat("virtual void foo(int *) override;");
10443   verifyFormat("virtual void foo(char &) const;");
10444   verifyFormat("virtual void foo(int *a, char *) const;");
10445   verifyFormat("int a = sizeof(int *) + b;");
10446   verifyFormat("int a = alignof(int *) + b;", getGoogleStyle());
10447   verifyFormat("bool b = f(g<int>) && c;");
10448   verifyFormat("typedef void (*f)(int i) func;");
10449   verifyFormat("void operator++(int) noexcept;");
10450   verifyFormat("void operator++(int &) noexcept;");
10451   verifyFormat("void operator delete(void *, std::size_t, const std::nothrow_t "
10452                "&) noexcept;");
10453   verifyFormat(
10454       "void operator delete(std::size_t, const std::nothrow_t &) noexcept;");
10455   verifyFormat("void operator delete(const std::nothrow_t &) noexcept;");
10456   verifyFormat("void operator delete(std::nothrow_t &) noexcept;");
10457   verifyFormat("void operator delete(nothrow_t &) noexcept;");
10458   verifyFormat("void operator delete(foo &) noexcept;");
10459   verifyFormat("void operator delete(foo) noexcept;");
10460   verifyFormat("void operator delete(int) noexcept;");
10461   verifyFormat("void operator delete(int &) noexcept;");
10462   verifyFormat("void operator delete(int &) volatile noexcept;");
10463   verifyFormat("void operator delete(int &) const");
10464   verifyFormat("void operator delete(int &) = default");
10465   verifyFormat("void operator delete(int &) = delete");
10466   verifyFormat("void operator delete(int &) [[noreturn]]");
10467   verifyFormat("void operator delete(int &) throw();");
10468   verifyFormat("void operator delete(int &) throw(int);");
10469   verifyFormat("auto operator delete(int &) -> int;");
10470   verifyFormat("auto operator delete(int &) override");
10471   verifyFormat("auto operator delete(int &) final");
10472 
10473   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
10474                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
10475   // FIXME: The indentation here is not ideal.
10476   verifyFormat(
10477       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10478       "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
10479       "        [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
10480 }
10481 
10482 TEST_F(FormatTest, FormatsFunctionTypes) {
10483   verifyFormat("A<bool()> a;");
10484   verifyFormat("A<SomeType()> a;");
10485   verifyFormat("A<void (*)(int, std::string)> a;");
10486   verifyFormat("A<void *(int)>;");
10487   verifyFormat("void *(*a)(int *, SomeType *);");
10488   verifyFormat("int (*func)(void *);");
10489   verifyFormat("void f() { int (*func)(void *); }");
10490   verifyFormat("template <class CallbackClass>\n"
10491                "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
10492 
10493   verifyGoogleFormat("A<void*(int*, SomeType*)>;");
10494   verifyGoogleFormat("void* (*a)(int);");
10495   verifyGoogleFormat(
10496       "template <class CallbackClass>\n"
10497       "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
10498 
10499   // Other constructs can look somewhat like function types:
10500   verifyFormat("A<sizeof(*x)> a;");
10501   verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
10502   verifyFormat("some_var = function(*some_pointer_var)[0];");
10503   verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
10504   verifyFormat("int x = f(&h)();");
10505   verifyFormat("returnsFunction(&param1, &param2)(param);");
10506   verifyFormat("std::function<\n"
10507                "    LooooooooooongTemplatedType<\n"
10508                "        SomeType>*(\n"
10509                "        LooooooooooooooooongType type)>\n"
10510                "    function;",
10511                getGoogleStyleWithColumns(40));
10512 }
10513 
10514 TEST_F(FormatTest, FormatsPointersToArrayTypes) {
10515   verifyFormat("A (*foo_)[6];");
10516   verifyFormat("vector<int> (*foo_)[6];");
10517 }
10518 
10519 TEST_F(FormatTest, BreaksLongVariableDeclarations) {
10520   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
10521                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
10522   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
10523                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
10524   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
10525                "    *LoooooooooooooooooooooooooooooooooooooooongVariable;");
10526 
10527   // Different ways of ()-initializiation.
10528   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
10529                "    LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
10530   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
10531                "    LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
10532   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
10533                "    LoooooooooooooooooooooooooooooooooooooooongVariable({});");
10534   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
10535                "    LoooooooooooooooooooooooooooooooooooooongVariable([A a]);");
10536 
10537   // Lambdas should not confuse the variable declaration heuristic.
10538   verifyFormat("LooooooooooooooooongType\n"
10539                "    variable(nullptr, [](A *a) {});",
10540                getLLVMStyleWithColumns(40));
10541 }
10542 
10543 TEST_F(FormatTest, BreaksLongDeclarations) {
10544   verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
10545                "    AnotherNameForTheLongType;");
10546   verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
10547                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10548   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10549                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
10550   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n"
10551                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
10552   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10553                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10554   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
10555                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10556   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
10557                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10558   verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
10559                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10560   verifyFormat("typeof(LoooooooooooooooooooooooooooooooooooooooooongName)\n"
10561                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10562   verifyFormat("_Atomic(LooooooooooooooooooooooooooooooooooooooooongName)\n"
10563                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10564   verifyFormat("__underlying_type(LooooooooooooooooooooooooooooooongName)\n"
10565                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10566   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10567                "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);");
10568   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10569                "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}");
10570   FormatStyle Indented = getLLVMStyle();
10571   Indented.IndentWrappedFunctionNames = true;
10572   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10573                "    LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
10574                Indented);
10575   verifyFormat(
10576       "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10577       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
10578       Indented);
10579   verifyFormat(
10580       "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
10581       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
10582       Indented);
10583   verifyFormat(
10584       "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
10585       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
10586       Indented);
10587 
10588   // FIXME: Without the comment, this breaks after "(".
10589   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType  // break\n"
10590                "    (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();",
10591                getGoogleStyle());
10592 
10593   verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
10594                "                  int LoooooooooooooooooooongParam2) {}");
10595   verifyFormat(
10596       "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
10597       "                                   SourceLocation L, IdentifierIn *II,\n"
10598       "                                   Type *T) {}");
10599   verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
10600                "ReallyReaaallyLongFunctionName(\n"
10601                "    const std::string &SomeParameter,\n"
10602                "    const SomeType<string, SomeOtherTemplateParameter>\n"
10603                "        &ReallyReallyLongParameterName,\n"
10604                "    const SomeType<string, SomeOtherTemplateParameter>\n"
10605                "        &AnotherLongParameterName) {}");
10606   verifyFormat("template <typename A>\n"
10607                "SomeLoooooooooooooooooooooongType<\n"
10608                "    typename some_namespace::SomeOtherType<A>::Type>\n"
10609                "Function() {}");
10610 
10611   verifyGoogleFormat(
10612       "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
10613       "    aaaaaaaaaaaaaaaaaaaaaaa;");
10614   verifyGoogleFormat(
10615       "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
10616       "                                   SourceLocation L) {}");
10617   verifyGoogleFormat(
10618       "some_namespace::LongReturnType\n"
10619       "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
10620       "    int first_long_parameter, int second_parameter) {}");
10621 
10622   verifyGoogleFormat("template <typename T>\n"
10623                      "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
10624                      "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
10625   verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10626                      "                   int aaaaaaaaaaaaaaaaaaaaaaa);");
10627 
10628   verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
10629                "    const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10630                "        *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
10631   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10632                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
10633                "        aaaaaaaaaaaaaaaaaaaaaaaa);");
10634   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10635                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
10636                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n"
10637                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
10638 
10639   verifyFormat("template <typename T> // Templates on own line.\n"
10640                "static int            // Some comment.\n"
10641                "MyFunction(int a);",
10642                getLLVMStyle());
10643 }
10644 
10645 TEST_F(FormatTest, FormatsAccessModifiers) {
10646   FormatStyle Style = getLLVMStyle();
10647   EXPECT_EQ(Style.EmptyLineBeforeAccessModifier,
10648             FormatStyle::ELBAMS_LogicalBlock);
10649   verifyFormat("struct foo {\n"
10650                "private:\n"
10651                "  void f() {}\n"
10652                "\n"
10653                "private:\n"
10654                "  int i;\n"
10655                "\n"
10656                "protected:\n"
10657                "  int j;\n"
10658                "};\n",
10659                Style);
10660   verifyFormat("struct foo {\n"
10661                "private:\n"
10662                "  void f() {}\n"
10663                "\n"
10664                "private:\n"
10665                "  int i;\n"
10666                "\n"
10667                "protected:\n"
10668                "  int j;\n"
10669                "};\n",
10670                "struct foo {\n"
10671                "private:\n"
10672                "  void f() {}\n"
10673                "private:\n"
10674                "  int i;\n"
10675                "protected:\n"
10676                "  int j;\n"
10677                "};\n",
10678                Style);
10679   verifyFormat("struct foo { /* comment */\n"
10680                "private:\n"
10681                "  int i;\n"
10682                "  // comment\n"
10683                "private:\n"
10684                "  int j;\n"
10685                "};\n",
10686                Style);
10687   verifyFormat("struct foo {\n"
10688                "#ifdef FOO\n"
10689                "#endif\n"
10690                "private:\n"
10691                "  int i;\n"
10692                "#ifdef FOO\n"
10693                "private:\n"
10694                "#endif\n"
10695                "  int j;\n"
10696                "};\n",
10697                Style);
10698   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
10699   verifyFormat("struct foo {\n"
10700                "private:\n"
10701                "  void f() {}\n"
10702                "private:\n"
10703                "  int i;\n"
10704                "protected:\n"
10705                "  int j;\n"
10706                "};\n",
10707                Style);
10708   verifyFormat("struct foo {\n"
10709                "private:\n"
10710                "  void f() {}\n"
10711                "private:\n"
10712                "  int i;\n"
10713                "protected:\n"
10714                "  int j;\n"
10715                "};\n",
10716                "struct foo {\n"
10717                "\n"
10718                "private:\n"
10719                "  void f() {}\n"
10720                "\n"
10721                "private:\n"
10722                "  int i;\n"
10723                "\n"
10724                "protected:\n"
10725                "  int j;\n"
10726                "};\n",
10727                Style);
10728   verifyFormat("struct foo { /* comment */\n"
10729                "private:\n"
10730                "  int i;\n"
10731                "  // comment\n"
10732                "private:\n"
10733                "  int j;\n"
10734                "};\n",
10735                "struct foo { /* comment */\n"
10736                "\n"
10737                "private:\n"
10738                "  int i;\n"
10739                "  // comment\n"
10740                "\n"
10741                "private:\n"
10742                "  int j;\n"
10743                "};\n",
10744                Style);
10745   verifyFormat("struct foo {\n"
10746                "#ifdef FOO\n"
10747                "#endif\n"
10748                "private:\n"
10749                "  int i;\n"
10750                "#ifdef FOO\n"
10751                "private:\n"
10752                "#endif\n"
10753                "  int j;\n"
10754                "};\n",
10755                "struct foo {\n"
10756                "#ifdef FOO\n"
10757                "#endif\n"
10758                "\n"
10759                "private:\n"
10760                "  int i;\n"
10761                "#ifdef FOO\n"
10762                "\n"
10763                "private:\n"
10764                "#endif\n"
10765                "  int j;\n"
10766                "};\n",
10767                Style);
10768   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
10769   verifyFormat("struct foo {\n"
10770                "private:\n"
10771                "  void f() {}\n"
10772                "\n"
10773                "private:\n"
10774                "  int i;\n"
10775                "\n"
10776                "protected:\n"
10777                "  int j;\n"
10778                "};\n",
10779                Style);
10780   verifyFormat("struct foo {\n"
10781                "private:\n"
10782                "  void f() {}\n"
10783                "\n"
10784                "private:\n"
10785                "  int i;\n"
10786                "\n"
10787                "protected:\n"
10788                "  int j;\n"
10789                "};\n",
10790                "struct foo {\n"
10791                "private:\n"
10792                "  void f() {}\n"
10793                "private:\n"
10794                "  int i;\n"
10795                "protected:\n"
10796                "  int j;\n"
10797                "};\n",
10798                Style);
10799   verifyFormat("struct foo { /* comment */\n"
10800                "private:\n"
10801                "  int i;\n"
10802                "  // comment\n"
10803                "\n"
10804                "private:\n"
10805                "  int j;\n"
10806                "};\n",
10807                "struct foo { /* comment */\n"
10808                "private:\n"
10809                "  int i;\n"
10810                "  // comment\n"
10811                "\n"
10812                "private:\n"
10813                "  int j;\n"
10814                "};\n",
10815                Style);
10816   verifyFormat("struct foo {\n"
10817                "#ifdef FOO\n"
10818                "#endif\n"
10819                "\n"
10820                "private:\n"
10821                "  int i;\n"
10822                "#ifdef FOO\n"
10823                "\n"
10824                "private:\n"
10825                "#endif\n"
10826                "  int j;\n"
10827                "};\n",
10828                "struct foo {\n"
10829                "#ifdef FOO\n"
10830                "#endif\n"
10831                "private:\n"
10832                "  int i;\n"
10833                "#ifdef FOO\n"
10834                "private:\n"
10835                "#endif\n"
10836                "  int j;\n"
10837                "};\n",
10838                Style);
10839   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
10840   EXPECT_EQ("struct foo {\n"
10841             "\n"
10842             "private:\n"
10843             "  void f() {}\n"
10844             "\n"
10845             "private:\n"
10846             "  int i;\n"
10847             "\n"
10848             "protected:\n"
10849             "  int j;\n"
10850             "};\n",
10851             format("struct foo {\n"
10852                    "\n"
10853                    "private:\n"
10854                    "  void f() {}\n"
10855                    "\n"
10856                    "private:\n"
10857                    "  int i;\n"
10858                    "\n"
10859                    "protected:\n"
10860                    "  int j;\n"
10861                    "};\n",
10862                    Style));
10863   verifyFormat("struct foo {\n"
10864                "private:\n"
10865                "  void f() {}\n"
10866                "private:\n"
10867                "  int i;\n"
10868                "protected:\n"
10869                "  int j;\n"
10870                "};\n",
10871                Style);
10872   EXPECT_EQ("struct foo { /* comment */\n"
10873             "\n"
10874             "private:\n"
10875             "  int i;\n"
10876             "  // comment\n"
10877             "\n"
10878             "private:\n"
10879             "  int j;\n"
10880             "};\n",
10881             format("struct foo { /* comment */\n"
10882                    "\n"
10883                    "private:\n"
10884                    "  int i;\n"
10885                    "  // comment\n"
10886                    "\n"
10887                    "private:\n"
10888                    "  int j;\n"
10889                    "};\n",
10890                    Style));
10891   verifyFormat("struct foo { /* comment */\n"
10892                "private:\n"
10893                "  int i;\n"
10894                "  // comment\n"
10895                "private:\n"
10896                "  int j;\n"
10897                "};\n",
10898                Style);
10899   EXPECT_EQ("struct foo {\n"
10900             "#ifdef FOO\n"
10901             "#endif\n"
10902             "\n"
10903             "private:\n"
10904             "  int i;\n"
10905             "#ifdef FOO\n"
10906             "\n"
10907             "private:\n"
10908             "#endif\n"
10909             "  int j;\n"
10910             "};\n",
10911             format("struct foo {\n"
10912                    "#ifdef FOO\n"
10913                    "#endif\n"
10914                    "\n"
10915                    "private:\n"
10916                    "  int i;\n"
10917                    "#ifdef FOO\n"
10918                    "\n"
10919                    "private:\n"
10920                    "#endif\n"
10921                    "  int j;\n"
10922                    "};\n",
10923                    Style));
10924   verifyFormat("struct foo {\n"
10925                "#ifdef FOO\n"
10926                "#endif\n"
10927                "private:\n"
10928                "  int i;\n"
10929                "#ifdef FOO\n"
10930                "private:\n"
10931                "#endif\n"
10932                "  int j;\n"
10933                "};\n",
10934                Style);
10935 
10936   FormatStyle NoEmptyLines = getLLVMStyle();
10937   NoEmptyLines.MaxEmptyLinesToKeep = 0;
10938   verifyFormat("struct foo {\n"
10939                "private:\n"
10940                "  void f() {}\n"
10941                "\n"
10942                "private:\n"
10943                "  int i;\n"
10944                "\n"
10945                "public:\n"
10946                "protected:\n"
10947                "  int j;\n"
10948                "};\n",
10949                NoEmptyLines);
10950 
10951   NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
10952   verifyFormat("struct foo {\n"
10953                "private:\n"
10954                "  void f() {}\n"
10955                "private:\n"
10956                "  int i;\n"
10957                "public:\n"
10958                "protected:\n"
10959                "  int j;\n"
10960                "};\n",
10961                NoEmptyLines);
10962 
10963   NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
10964   verifyFormat("struct foo {\n"
10965                "private:\n"
10966                "  void f() {}\n"
10967                "\n"
10968                "private:\n"
10969                "  int i;\n"
10970                "\n"
10971                "public:\n"
10972                "\n"
10973                "protected:\n"
10974                "  int j;\n"
10975                "};\n",
10976                NoEmptyLines);
10977 }
10978 
10979 TEST_F(FormatTest, FormatsAfterAccessModifiers) {
10980 
10981   FormatStyle Style = getLLVMStyle();
10982   EXPECT_EQ(Style.EmptyLineAfterAccessModifier, FormatStyle::ELAAMS_Never);
10983   verifyFormat("struct foo {\n"
10984                "private:\n"
10985                "  void f() {}\n"
10986                "\n"
10987                "private:\n"
10988                "  int i;\n"
10989                "\n"
10990                "protected:\n"
10991                "  int j;\n"
10992                "};\n",
10993                Style);
10994 
10995   // Check if lines are removed.
10996   verifyFormat("struct foo {\n"
10997                "private:\n"
10998                "  void f() {}\n"
10999                "\n"
11000                "private:\n"
11001                "  int i;\n"
11002                "\n"
11003                "protected:\n"
11004                "  int j;\n"
11005                "};\n",
11006                "struct foo {\n"
11007                "private:\n"
11008                "\n"
11009                "  void f() {}\n"
11010                "\n"
11011                "private:\n"
11012                "\n"
11013                "  int i;\n"
11014                "\n"
11015                "protected:\n"
11016                "\n"
11017                "  int j;\n"
11018                "};\n",
11019                Style);
11020 
11021   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
11022   verifyFormat("struct foo {\n"
11023                "private:\n"
11024                "\n"
11025                "  void f() {}\n"
11026                "\n"
11027                "private:\n"
11028                "\n"
11029                "  int i;\n"
11030                "\n"
11031                "protected:\n"
11032                "\n"
11033                "  int j;\n"
11034                "};\n",
11035                Style);
11036 
11037   // Check if lines are added.
11038   verifyFormat("struct foo {\n"
11039                "private:\n"
11040                "\n"
11041                "  void f() {}\n"
11042                "\n"
11043                "private:\n"
11044                "\n"
11045                "  int i;\n"
11046                "\n"
11047                "protected:\n"
11048                "\n"
11049                "  int j;\n"
11050                "};\n",
11051                "struct foo {\n"
11052                "private:\n"
11053                "  void f() {}\n"
11054                "\n"
11055                "private:\n"
11056                "  int i;\n"
11057                "\n"
11058                "protected:\n"
11059                "  int j;\n"
11060                "};\n",
11061                Style);
11062 
11063   // Leave tests rely on the code layout, test::messUp can not be used.
11064   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
11065   Style.MaxEmptyLinesToKeep = 0u;
11066   verifyFormat("struct foo {\n"
11067                "private:\n"
11068                "  void f() {}\n"
11069                "\n"
11070                "private:\n"
11071                "  int i;\n"
11072                "\n"
11073                "protected:\n"
11074                "  int j;\n"
11075                "};\n",
11076                Style);
11077 
11078   // Check if MaxEmptyLinesToKeep is respected.
11079   EXPECT_EQ("struct foo {\n"
11080             "private:\n"
11081             "  void f() {}\n"
11082             "\n"
11083             "private:\n"
11084             "  int i;\n"
11085             "\n"
11086             "protected:\n"
11087             "  int j;\n"
11088             "};\n",
11089             format("struct foo {\n"
11090                    "private:\n"
11091                    "\n\n\n"
11092                    "  void f() {}\n"
11093                    "\n"
11094                    "private:\n"
11095                    "\n\n\n"
11096                    "  int i;\n"
11097                    "\n"
11098                    "protected:\n"
11099                    "\n\n\n"
11100                    "  int j;\n"
11101                    "};\n",
11102                    Style));
11103 
11104   Style.MaxEmptyLinesToKeep = 1u;
11105   EXPECT_EQ("struct foo {\n"
11106             "private:\n"
11107             "\n"
11108             "  void f() {}\n"
11109             "\n"
11110             "private:\n"
11111             "\n"
11112             "  int i;\n"
11113             "\n"
11114             "protected:\n"
11115             "\n"
11116             "  int j;\n"
11117             "};\n",
11118             format("struct foo {\n"
11119                    "private:\n"
11120                    "\n"
11121                    "  void f() {}\n"
11122                    "\n"
11123                    "private:\n"
11124                    "\n"
11125                    "  int i;\n"
11126                    "\n"
11127                    "protected:\n"
11128                    "\n"
11129                    "  int j;\n"
11130                    "};\n",
11131                    Style));
11132   // Check if no lines are kept.
11133   EXPECT_EQ("struct foo {\n"
11134             "private:\n"
11135             "  void f() {}\n"
11136             "\n"
11137             "private:\n"
11138             "  int i;\n"
11139             "\n"
11140             "protected:\n"
11141             "  int j;\n"
11142             "};\n",
11143             format("struct foo {\n"
11144                    "private:\n"
11145                    "  void f() {}\n"
11146                    "\n"
11147                    "private:\n"
11148                    "  int i;\n"
11149                    "\n"
11150                    "protected:\n"
11151                    "  int j;\n"
11152                    "};\n",
11153                    Style));
11154   // Check if MaxEmptyLinesToKeep is respected.
11155   EXPECT_EQ("struct foo {\n"
11156             "private:\n"
11157             "\n"
11158             "  void f() {}\n"
11159             "\n"
11160             "private:\n"
11161             "\n"
11162             "  int i;\n"
11163             "\n"
11164             "protected:\n"
11165             "\n"
11166             "  int j;\n"
11167             "};\n",
11168             format("struct foo {\n"
11169                    "private:\n"
11170                    "\n\n\n"
11171                    "  void f() {}\n"
11172                    "\n"
11173                    "private:\n"
11174                    "\n\n\n"
11175                    "  int i;\n"
11176                    "\n"
11177                    "protected:\n"
11178                    "\n\n\n"
11179                    "  int j;\n"
11180                    "};\n",
11181                    Style));
11182 
11183   Style.MaxEmptyLinesToKeep = 10u;
11184   EXPECT_EQ("struct foo {\n"
11185             "private:\n"
11186             "\n\n\n"
11187             "  void f() {}\n"
11188             "\n"
11189             "private:\n"
11190             "\n\n\n"
11191             "  int i;\n"
11192             "\n"
11193             "protected:\n"
11194             "\n\n\n"
11195             "  int j;\n"
11196             "};\n",
11197             format("struct foo {\n"
11198                    "private:\n"
11199                    "\n\n\n"
11200                    "  void f() {}\n"
11201                    "\n"
11202                    "private:\n"
11203                    "\n\n\n"
11204                    "  int i;\n"
11205                    "\n"
11206                    "protected:\n"
11207                    "\n\n\n"
11208                    "  int j;\n"
11209                    "};\n",
11210                    Style));
11211 
11212   // Test with comments.
11213   Style = getLLVMStyle();
11214   verifyFormat("struct foo {\n"
11215                "private:\n"
11216                "  // comment\n"
11217                "  void f() {}\n"
11218                "\n"
11219                "private: /* comment */\n"
11220                "  int i;\n"
11221                "};\n",
11222                Style);
11223   verifyFormat("struct foo {\n"
11224                "private:\n"
11225                "  // comment\n"
11226                "  void f() {}\n"
11227                "\n"
11228                "private: /* comment */\n"
11229                "  int i;\n"
11230                "};\n",
11231                "struct foo {\n"
11232                "private:\n"
11233                "\n"
11234                "  // comment\n"
11235                "  void f() {}\n"
11236                "\n"
11237                "private: /* comment */\n"
11238                "\n"
11239                "  int i;\n"
11240                "};\n",
11241                Style);
11242 
11243   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
11244   verifyFormat("struct foo {\n"
11245                "private:\n"
11246                "\n"
11247                "  // comment\n"
11248                "  void f() {}\n"
11249                "\n"
11250                "private: /* comment */\n"
11251                "\n"
11252                "  int i;\n"
11253                "};\n",
11254                "struct foo {\n"
11255                "private:\n"
11256                "  // comment\n"
11257                "  void f() {}\n"
11258                "\n"
11259                "private: /* comment */\n"
11260                "  int i;\n"
11261                "};\n",
11262                Style);
11263   verifyFormat("struct foo {\n"
11264                "private:\n"
11265                "\n"
11266                "  // comment\n"
11267                "  void f() {}\n"
11268                "\n"
11269                "private: /* comment */\n"
11270                "\n"
11271                "  int i;\n"
11272                "};\n",
11273                Style);
11274 
11275   // Test with preprocessor defines.
11276   Style = getLLVMStyle();
11277   verifyFormat("struct foo {\n"
11278                "private:\n"
11279                "#ifdef FOO\n"
11280                "#endif\n"
11281                "  void f() {}\n"
11282                "};\n",
11283                Style);
11284   verifyFormat("struct foo {\n"
11285                "private:\n"
11286                "#ifdef FOO\n"
11287                "#endif\n"
11288                "  void f() {}\n"
11289                "};\n",
11290                "struct foo {\n"
11291                "private:\n"
11292                "\n"
11293                "#ifdef FOO\n"
11294                "#endif\n"
11295                "  void f() {}\n"
11296                "};\n",
11297                Style);
11298 
11299   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
11300   verifyFormat("struct foo {\n"
11301                "private:\n"
11302                "\n"
11303                "#ifdef FOO\n"
11304                "#endif\n"
11305                "  void f() {}\n"
11306                "};\n",
11307                "struct foo {\n"
11308                "private:\n"
11309                "#ifdef FOO\n"
11310                "#endif\n"
11311                "  void f() {}\n"
11312                "};\n",
11313                Style);
11314   verifyFormat("struct foo {\n"
11315                "private:\n"
11316                "\n"
11317                "#ifdef FOO\n"
11318                "#endif\n"
11319                "  void f() {}\n"
11320                "};\n",
11321                Style);
11322 }
11323 
11324 TEST_F(FormatTest, FormatsAfterAndBeforeAccessModifiersInteraction) {
11325   // Combined tests of EmptyLineAfterAccessModifier and
11326   // EmptyLineBeforeAccessModifier.
11327   FormatStyle Style = getLLVMStyle();
11328   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
11329   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
11330   verifyFormat("struct foo {\n"
11331                "private:\n"
11332                "\n"
11333                "protected:\n"
11334                "};\n",
11335                Style);
11336 
11337   Style.MaxEmptyLinesToKeep = 10u;
11338   // Both remove all new lines.
11339   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
11340   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
11341   verifyFormat("struct foo {\n"
11342                "private:\n"
11343                "protected:\n"
11344                "};\n",
11345                "struct foo {\n"
11346                "private:\n"
11347                "\n\n\n"
11348                "protected:\n"
11349                "};\n",
11350                Style);
11351 
11352   // Leave tests rely on the code layout, test::messUp can not be used.
11353   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
11354   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
11355   Style.MaxEmptyLinesToKeep = 10u;
11356   EXPECT_EQ("struct foo {\n"
11357             "private:\n"
11358             "\n\n\n"
11359             "protected:\n"
11360             "};\n",
11361             format("struct foo {\n"
11362                    "private:\n"
11363                    "\n\n\n"
11364                    "protected:\n"
11365                    "};\n",
11366                    Style));
11367   Style.MaxEmptyLinesToKeep = 3u;
11368   EXPECT_EQ("struct foo {\n"
11369             "private:\n"
11370             "\n\n\n"
11371             "protected:\n"
11372             "};\n",
11373             format("struct foo {\n"
11374                    "private:\n"
11375                    "\n\n\n"
11376                    "protected:\n"
11377                    "};\n",
11378                    Style));
11379   Style.MaxEmptyLinesToKeep = 1u;
11380   EXPECT_EQ("struct foo {\n"
11381             "private:\n"
11382             "\n\n\n"
11383             "protected:\n"
11384             "};\n",
11385             format("struct foo {\n"
11386                    "private:\n"
11387                    "\n\n\n"
11388                    "protected:\n"
11389                    "};\n",
11390                    Style)); // Based on new lines in original document and not
11391                             // on the setting.
11392 
11393   Style.MaxEmptyLinesToKeep = 10u;
11394   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
11395   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
11396   // Newlines are kept if they are greater than zero,
11397   // test::messUp removes all new lines which changes the logic
11398   EXPECT_EQ("struct foo {\n"
11399             "private:\n"
11400             "\n\n\n"
11401             "protected:\n"
11402             "};\n",
11403             format("struct foo {\n"
11404                    "private:\n"
11405                    "\n\n\n"
11406                    "protected:\n"
11407                    "};\n",
11408                    Style));
11409 
11410   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
11411   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
11412   // test::messUp removes all new lines which changes the logic
11413   EXPECT_EQ("struct foo {\n"
11414             "private:\n"
11415             "\n\n\n"
11416             "protected:\n"
11417             "};\n",
11418             format("struct foo {\n"
11419                    "private:\n"
11420                    "\n\n\n"
11421                    "protected:\n"
11422                    "};\n",
11423                    Style));
11424 
11425   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
11426   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
11427   EXPECT_EQ("struct foo {\n"
11428             "private:\n"
11429             "\n\n\n"
11430             "protected:\n"
11431             "};\n",
11432             format("struct foo {\n"
11433                    "private:\n"
11434                    "\n\n\n"
11435                    "protected:\n"
11436                    "};\n",
11437                    Style)); // test::messUp removes all new lines which changes
11438                             // the logic.
11439 
11440   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
11441   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
11442   verifyFormat("struct foo {\n"
11443                "private:\n"
11444                "protected:\n"
11445                "};\n",
11446                "struct foo {\n"
11447                "private:\n"
11448                "\n\n\n"
11449                "protected:\n"
11450                "};\n",
11451                Style);
11452 
11453   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
11454   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
11455   EXPECT_EQ("struct foo {\n"
11456             "private:\n"
11457             "\n\n\n"
11458             "protected:\n"
11459             "};\n",
11460             format("struct foo {\n"
11461                    "private:\n"
11462                    "\n\n\n"
11463                    "protected:\n"
11464                    "};\n",
11465                    Style)); // test::messUp removes all new lines which changes
11466                             // the logic.
11467 
11468   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
11469   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
11470   verifyFormat("struct foo {\n"
11471                "private:\n"
11472                "protected:\n"
11473                "};\n",
11474                "struct foo {\n"
11475                "private:\n"
11476                "\n\n\n"
11477                "protected:\n"
11478                "};\n",
11479                Style);
11480 
11481   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
11482   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
11483   verifyFormat("struct foo {\n"
11484                "private:\n"
11485                "protected:\n"
11486                "};\n",
11487                "struct foo {\n"
11488                "private:\n"
11489                "\n\n\n"
11490                "protected:\n"
11491                "};\n",
11492                Style);
11493 
11494   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
11495   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
11496   verifyFormat("struct foo {\n"
11497                "private:\n"
11498                "protected:\n"
11499                "};\n",
11500                "struct foo {\n"
11501                "private:\n"
11502                "\n\n\n"
11503                "protected:\n"
11504                "};\n",
11505                Style);
11506 
11507   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
11508   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
11509   verifyFormat("struct foo {\n"
11510                "private:\n"
11511                "protected:\n"
11512                "};\n",
11513                "struct foo {\n"
11514                "private:\n"
11515                "\n\n\n"
11516                "protected:\n"
11517                "};\n",
11518                Style);
11519 }
11520 
11521 TEST_F(FormatTest, FormatsArrays) {
11522   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
11523                "                         [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
11524   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n"
11525                "                         [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;");
11526   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n"
11527                "    aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}");
11528   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
11529                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
11530   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
11531                "    [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
11532   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
11533                "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
11534                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
11535   verifyFormat(
11536       "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
11537       "             << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
11538       "                                  [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
11539   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n"
11540                "    .aaaaaaaaaaaaaaaaaaaaaa();");
11541 
11542   verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
11543                      "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
11544   verifyFormat(
11545       "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
11546       "                                  .aaaaaaa[0]\n"
11547       "                                  .aaaaaaaaaaaaaaaaaaaaaa();");
11548   verifyFormat("a[::b::c];");
11549 
11550   verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10));
11551 
11552   FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0);
11553   verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit);
11554 }
11555 
11556 TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
11557   verifyFormat("(a)->b();");
11558   verifyFormat("--a;");
11559 }
11560 
11561 TEST_F(FormatTest, HandlesIncludeDirectives) {
11562   verifyFormat("#include <string>\n"
11563                "#include <a/b/c.h>\n"
11564                "#include \"a/b/string\"\n"
11565                "#include \"string.h\"\n"
11566                "#include \"string.h\"\n"
11567                "#include <a-a>\n"
11568                "#include < path with space >\n"
11569                "#include_next <test.h>"
11570                "#include \"abc.h\" // this is included for ABC\n"
11571                "#include \"some long include\" // with a comment\n"
11572                "#include \"some very long include path\"\n"
11573                "#include <some/very/long/include/path>\n",
11574                getLLVMStyleWithColumns(35));
11575   EXPECT_EQ("#include \"a.h\"", format("#include  \"a.h\""));
11576   EXPECT_EQ("#include <a>", format("#include<a>"));
11577 
11578   verifyFormat("#import <string>");
11579   verifyFormat("#import <a/b/c.h>");
11580   verifyFormat("#import \"a/b/string\"");
11581   verifyFormat("#import \"string.h\"");
11582   verifyFormat("#import \"string.h\"");
11583   verifyFormat("#if __has_include(<strstream>)\n"
11584                "#include <strstream>\n"
11585                "#endif");
11586 
11587   verifyFormat("#define MY_IMPORT <a/b>");
11588 
11589   verifyFormat("#if __has_include(<a/b>)");
11590   verifyFormat("#if __has_include_next(<a/b>)");
11591   verifyFormat("#define F __has_include(<a/b>)");
11592   verifyFormat("#define F __has_include_next(<a/b>)");
11593 
11594   // Protocol buffer definition or missing "#".
11595   verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
11596                getLLVMStyleWithColumns(30));
11597 
11598   FormatStyle Style = getLLVMStyle();
11599   Style.AlwaysBreakBeforeMultilineStrings = true;
11600   Style.ColumnLimit = 0;
11601   verifyFormat("#import \"abc.h\"", Style);
11602 
11603   // But 'import' might also be a regular C++ namespace.
11604   verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
11605                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
11606 }
11607 
11608 //===----------------------------------------------------------------------===//
11609 // Error recovery tests.
11610 //===----------------------------------------------------------------------===//
11611 
11612 TEST_F(FormatTest, IncompleteParameterLists) {
11613   FormatStyle NoBinPacking = getLLVMStyle();
11614   NoBinPacking.BinPackParameters = false;
11615   verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
11616                "                        double *min_x,\n"
11617                "                        double *max_x,\n"
11618                "                        double *min_y,\n"
11619                "                        double *max_y,\n"
11620                "                        double *min_z,\n"
11621                "                        double *max_z, ) {}",
11622                NoBinPacking);
11623 }
11624 
11625 TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
11626   verifyFormat("void f() { return; }\n42");
11627   verifyFormat("void f() {\n"
11628                "  if (0)\n"
11629                "    return;\n"
11630                "}\n"
11631                "42");
11632   verifyFormat("void f() { return }\n42");
11633   verifyFormat("void f() {\n"
11634                "  if (0)\n"
11635                "    return\n"
11636                "}\n"
11637                "42");
11638 }
11639 
11640 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
11641   EXPECT_EQ("void f() { return }", format("void  f ( )  {  return  }"));
11642   EXPECT_EQ("void f() {\n"
11643             "  if (a)\n"
11644             "    return\n"
11645             "}",
11646             format("void  f  (  )  {  if  ( a )  return  }"));
11647   EXPECT_EQ("namespace N {\n"
11648             "void f()\n"
11649             "}",
11650             format("namespace  N  {  void f()  }"));
11651   EXPECT_EQ("namespace N {\n"
11652             "void f() {}\n"
11653             "void g()\n"
11654             "} // namespace N",
11655             format("namespace N  { void f( ) { } void g( ) }"));
11656 }
11657 
11658 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
11659   verifyFormat("int aaaaaaaa =\n"
11660                "    // Overlylongcomment\n"
11661                "    b;",
11662                getLLVMStyleWithColumns(20));
11663   verifyFormat("function(\n"
11664                "    ShortArgument,\n"
11665                "    LoooooooooooongArgument);\n",
11666                getLLVMStyleWithColumns(20));
11667 }
11668 
11669 TEST_F(FormatTest, IncorrectAccessSpecifier) {
11670   verifyFormat("public:");
11671   verifyFormat("class A {\n"
11672                "public\n"
11673                "  void f() {}\n"
11674                "};");
11675   verifyFormat("public\n"
11676                "int qwerty;");
11677   verifyFormat("public\n"
11678                "B {}");
11679   verifyFormat("public\n"
11680                "{}");
11681   verifyFormat("public\n"
11682                "B { int x; }");
11683 }
11684 
11685 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
11686   verifyFormat("{");
11687   verifyFormat("#})");
11688   verifyNoCrash("(/**/[:!] ?[).");
11689 }
11690 
11691 TEST_F(FormatTest, IncorrectUnbalancedBracesInMacrosWithUnicode) {
11692   // Found by oss-fuzz:
11693   // https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=8212
11694   FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp);
11695   Style.ColumnLimit = 60;
11696   verifyNoCrash(
11697       "\x23\x47\xff\x20\x28\xff\x3c\xff\x3f\xff\x20\x2f\x7b\x7a\xff\x20"
11698       "\xff\xff\xff\xca\xb5\xff\xff\xff\xff\x3a\x7b\x7d\xff\x20\xff\x20"
11699       "\xff\x74\xff\x20\x7d\x7d\xff\x7b\x3a\xff\x20\x71\xff\x20\xff\x0a",
11700       Style);
11701 }
11702 
11703 TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
11704   verifyFormat("do {\n}");
11705   verifyFormat("do {\n}\n"
11706                "f();");
11707   verifyFormat("do {\n}\n"
11708                "wheeee(fun);");
11709   verifyFormat("do {\n"
11710                "  f();\n"
11711                "}");
11712 }
11713 
11714 TEST_F(FormatTest, IncorrectCodeMissingParens) {
11715   verifyFormat("if {\n  foo;\n  foo();\n}");
11716   verifyFormat("switch {\n  foo;\n  foo();\n}");
11717   verifyIncompleteFormat("for {\n  foo;\n  foo();\n}");
11718   verifyFormat("while {\n  foo;\n  foo();\n}");
11719   verifyFormat("do {\n  foo;\n  foo();\n} while;");
11720 }
11721 
11722 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
11723   verifyIncompleteFormat("namespace {\n"
11724                          "class Foo { Foo (\n"
11725                          "};\n"
11726                          "} // namespace");
11727 }
11728 
11729 TEST_F(FormatTest, IncorrectCodeErrorDetection) {
11730   EXPECT_EQ("{\n  {}\n", format("{\n{\n}\n"));
11731   EXPECT_EQ("{\n  {}\n", format("{\n  {\n}\n"));
11732   EXPECT_EQ("{\n  {}\n", format("{\n  {\n  }\n"));
11733   EXPECT_EQ("{\n  {}\n}\n}\n", format("{\n  {\n    }\n  }\n}\n"));
11734 
11735   EXPECT_EQ("{\n"
11736             "  {\n"
11737             "    breakme(\n"
11738             "        qwe);\n"
11739             "  }\n",
11740             format("{\n"
11741                    "    {\n"
11742                    " breakme(qwe);\n"
11743                    "}\n",
11744                    getLLVMStyleWithColumns(10)));
11745 }
11746 
11747 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
11748   verifyFormat("int x = {\n"
11749                "    avariable,\n"
11750                "    b(alongervariable)};",
11751                getLLVMStyleWithColumns(25));
11752 }
11753 
11754 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
11755   verifyFormat("return (a)(b){1, 2, 3};");
11756 }
11757 
11758 TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
11759   verifyFormat("vector<int> x{1, 2, 3, 4};");
11760   verifyFormat("vector<int> x{\n"
11761                "    1,\n"
11762                "    2,\n"
11763                "    3,\n"
11764                "    4,\n"
11765                "};");
11766   verifyFormat("vector<T> x{{}, {}, {}, {}};");
11767   verifyFormat("f({1, 2});");
11768   verifyFormat("auto v = Foo{-1};");
11769   verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
11770   verifyFormat("Class::Class : member{1, 2, 3} {}");
11771   verifyFormat("new vector<int>{1, 2, 3};");
11772   verifyFormat("new int[3]{1, 2, 3};");
11773   verifyFormat("new int{1};");
11774   verifyFormat("return {arg1, arg2};");
11775   verifyFormat("return {arg1, SomeType{parameter}};");
11776   verifyFormat("int count = set<int>{f(), g(), h()}.size();");
11777   verifyFormat("new T{arg1, arg2};");
11778   verifyFormat("f(MyMap[{composite, key}]);");
11779   verifyFormat("class Class {\n"
11780                "  T member = {arg1, arg2};\n"
11781                "};");
11782   verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
11783   verifyFormat("const struct A a = {.a = 1, .b = 2};");
11784   verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
11785   verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
11786   verifyFormat("int a = std::is_integral<int>{} + 0;");
11787 
11788   verifyFormat("int foo(int i) { return fo1{}(i); }");
11789   verifyFormat("int foo(int i) { return fo1{}(i); }");
11790   verifyFormat("auto i = decltype(x){};");
11791   verifyFormat("auto i = typeof(x){};");
11792   verifyFormat("auto i = _Atomic(x){};");
11793   verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
11794   verifyFormat("Node n{1, Node{1000}, //\n"
11795                "       2};");
11796   verifyFormat("Aaaa aaaaaaa{\n"
11797                "    {\n"
11798                "        aaaa,\n"
11799                "    },\n"
11800                "};");
11801   verifyFormat("class C : public D {\n"
11802                "  SomeClass SC{2};\n"
11803                "};");
11804   verifyFormat("class C : public A {\n"
11805                "  class D : public B {\n"
11806                "    void f() { int i{2}; }\n"
11807                "  };\n"
11808                "};");
11809   verifyFormat("#define A {a, a},");
11810   // Don't confuse braced list initializers with compound statements.
11811   verifyFormat(
11812       "class A {\n"
11813       "  A() : a{} {}\n"
11814       "  A(int b) : b(b) {}\n"
11815       "  A(int a, int b) : a(a), bs{{bs...}} { f(); }\n"
11816       "  int a, b;\n"
11817       "  explicit Expr(const Scalar<Result> &x) : u{Constant<Result>{x}} {}\n"
11818       "  explicit Expr(Scalar<Result> &&x) : u{Constant<Result>{std::move(x)}} "
11819       "{}\n"
11820       "};");
11821 
11822   // Avoid breaking between equal sign and opening brace
11823   FormatStyle AvoidBreakingFirstArgument = getLLVMStyle();
11824   AvoidBreakingFirstArgument.PenaltyBreakBeforeFirstCallParameter = 200;
11825   verifyFormat("const std::unordered_map<std::string, int> MyHashTable =\n"
11826                "    {{\"aaaaaaaaaaaaaaaaaaaaa\", 0},\n"
11827                "     {\"bbbbbbbbbbbbbbbbbbbbb\", 1},\n"
11828                "     {\"ccccccccccccccccccccc\", 2}};",
11829                AvoidBreakingFirstArgument);
11830 
11831   // Binpacking only if there is no trailing comma
11832   verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n"
11833                "                      cccccccccc, dddddddddd};",
11834                getLLVMStyleWithColumns(50));
11835   verifyFormat("const Aaaaaa aaaaa = {\n"
11836                "    aaaaaaaaaaa,\n"
11837                "    bbbbbbbbbbb,\n"
11838                "    ccccccccccc,\n"
11839                "    ddddddddddd,\n"
11840                "};",
11841                getLLVMStyleWithColumns(50));
11842 
11843   // Cases where distinguising braced lists and blocks is hard.
11844   verifyFormat("vector<int> v{12} GUARDED_BY(mutex);");
11845   verifyFormat("void f() {\n"
11846                "  return; // comment\n"
11847                "}\n"
11848                "SomeType t;");
11849   verifyFormat("void f() {\n"
11850                "  if (a) {\n"
11851                "    f();\n"
11852                "  }\n"
11853                "}\n"
11854                "SomeType t;");
11855 
11856   // In combination with BinPackArguments = false.
11857   FormatStyle NoBinPacking = getLLVMStyle();
11858   NoBinPacking.BinPackArguments = false;
11859   verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
11860                "                      bbbbb,\n"
11861                "                      ccccc,\n"
11862                "                      ddddd,\n"
11863                "                      eeeee,\n"
11864                "                      ffffff,\n"
11865                "                      ggggg,\n"
11866                "                      hhhhhh,\n"
11867                "                      iiiiii,\n"
11868                "                      jjjjjj,\n"
11869                "                      kkkkkk};",
11870                NoBinPacking);
11871   verifyFormat("const Aaaaaa aaaaa = {\n"
11872                "    aaaaa,\n"
11873                "    bbbbb,\n"
11874                "    ccccc,\n"
11875                "    ddddd,\n"
11876                "    eeeee,\n"
11877                "    ffffff,\n"
11878                "    ggggg,\n"
11879                "    hhhhhh,\n"
11880                "    iiiiii,\n"
11881                "    jjjjjj,\n"
11882                "    kkkkkk,\n"
11883                "};",
11884                NoBinPacking);
11885   verifyFormat(
11886       "const Aaaaaa aaaaa = {\n"
11887       "    aaaaa,  bbbbb,  ccccc,  ddddd,  eeeee,  ffffff, ggggg, hhhhhh,\n"
11888       "    iiiiii, jjjjjj, kkkkkk, aaaaa,  bbbbb,  ccccc,  ddddd, eeeee,\n"
11889       "    ffffff, ggggg,  hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
11890       "};",
11891       NoBinPacking);
11892 
11893   NoBinPacking.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
11894   EXPECT_EQ("static uint8 CddDp83848Reg[] = {\n"
11895             "    CDDDP83848_BMCR_REGISTER,\n"
11896             "    CDDDP83848_BMSR_REGISTER,\n"
11897             "    CDDDP83848_RBR_REGISTER};",
11898             format("static uint8 CddDp83848Reg[] = {CDDDP83848_BMCR_REGISTER,\n"
11899                    "                                CDDDP83848_BMSR_REGISTER,\n"
11900                    "                                CDDDP83848_RBR_REGISTER};",
11901                    NoBinPacking));
11902 
11903   // FIXME: The alignment of these trailing comments might be bad. Then again,
11904   // this might be utterly useless in real code.
11905   verifyFormat("Constructor::Constructor()\n"
11906                "    : some_value{         //\n"
11907                "                 aaaaaaa, //\n"
11908                "                 bbbbbbb} {}");
11909 
11910   // In braced lists, the first comment is always assumed to belong to the
11911   // first element. Thus, it can be moved to the next or previous line as
11912   // appropriate.
11913   EXPECT_EQ("function({// First element:\n"
11914             "          1,\n"
11915             "          // Second element:\n"
11916             "          2});",
11917             format("function({\n"
11918                    "    // First element:\n"
11919                    "    1,\n"
11920                    "    // Second element:\n"
11921                    "    2});"));
11922   EXPECT_EQ("std::vector<int> MyNumbers{\n"
11923             "    // First element:\n"
11924             "    1,\n"
11925             "    // Second element:\n"
11926             "    2};",
11927             format("std::vector<int> MyNumbers{// First element:\n"
11928                    "                           1,\n"
11929                    "                           // Second element:\n"
11930                    "                           2};",
11931                    getLLVMStyleWithColumns(30)));
11932   // A trailing comma should still lead to an enforced line break and no
11933   // binpacking.
11934   EXPECT_EQ("vector<int> SomeVector = {\n"
11935             "    // aaa\n"
11936             "    1,\n"
11937             "    2,\n"
11938             "};",
11939             format("vector<int> SomeVector = { // aaa\n"
11940                    "    1, 2, };"));
11941 
11942   // C++11 brace initializer list l-braces should not be treated any differently
11943   // when breaking before lambda bodies is enabled
11944   FormatStyle BreakBeforeLambdaBody = getLLVMStyle();
11945   BreakBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
11946   BreakBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
11947   BreakBeforeLambdaBody.AlwaysBreakBeforeMultilineStrings = true;
11948   verifyFormat(
11949       "std::runtime_error{\n"
11950       "    \"Long string which will force a break onto the next line...\"};",
11951       BreakBeforeLambdaBody);
11952 
11953   FormatStyle ExtraSpaces = getLLVMStyle();
11954   ExtraSpaces.Cpp11BracedListStyle = false;
11955   ExtraSpaces.ColumnLimit = 75;
11956   verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
11957   verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
11958   verifyFormat("f({ 1, 2 });", ExtraSpaces);
11959   verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
11960   verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
11961   verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
11962   verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
11963   verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
11964   verifyFormat("return { arg1, arg2 };", ExtraSpaces);
11965   verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
11966   verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
11967   verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
11968   verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
11969   verifyFormat("class Class {\n"
11970                "  T member = { arg1, arg2 };\n"
11971                "};",
11972                ExtraSpaces);
11973   verifyFormat(
11974       "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
11975       "                                 aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
11976       "                  : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
11977       "                                 bbbbbbbbbbbbbbbbbbbb, bbbbb };",
11978       ExtraSpaces);
11979   verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
11980   verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
11981                ExtraSpaces);
11982   verifyFormat(
11983       "someFunction(OtherParam,\n"
11984       "             BracedList{ // comment 1 (Forcing interesting break)\n"
11985       "                         param1, param2,\n"
11986       "                         // comment 2\n"
11987       "                         param3, param4 });",
11988       ExtraSpaces);
11989   verifyFormat(
11990       "std::this_thread::sleep_for(\n"
11991       "    std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
11992       ExtraSpaces);
11993   verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n"
11994                "    aaaaaaa,\n"
11995                "    aaaaaaaaaa,\n"
11996                "    aaaaa,\n"
11997                "    aaaaaaaaaaaaaaa,\n"
11998                "    aaa,\n"
11999                "    aaaaaaaaaa,\n"
12000                "    a,\n"
12001                "    aaaaaaaaaaaaaaaaaaaaa,\n"
12002                "    aaaaaaaaaaaa,\n"
12003                "    aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
12004                "    aaaaaaa,\n"
12005                "    a};");
12006   verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
12007   verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces);
12008   verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces);
12009 
12010   // Avoid breaking between initializer/equal sign and opening brace
12011   ExtraSpaces.PenaltyBreakBeforeFirstCallParameter = 200;
12012   verifyFormat("const std::unordered_map<std::string, int> MyHashTable = {\n"
12013                "  { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n"
12014                "  { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n"
12015                "  { \"ccccccccccccccccccccc\", 2 }\n"
12016                "};",
12017                ExtraSpaces);
12018   verifyFormat("const std::unordered_map<std::string, int> MyHashTable{\n"
12019                "  { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n"
12020                "  { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n"
12021                "  { \"ccccccccccccccccccccc\", 2 }\n"
12022                "};",
12023                ExtraSpaces);
12024 
12025   FormatStyle SpaceBeforeBrace = getLLVMStyle();
12026   SpaceBeforeBrace.SpaceBeforeCpp11BracedList = true;
12027   verifyFormat("vector<int> x {1, 2, 3, 4};", SpaceBeforeBrace);
12028   verifyFormat("f({}, {{}, {}}, MyMap[{k, v}]);", SpaceBeforeBrace);
12029 
12030   FormatStyle SpaceBetweenBraces = getLLVMStyle();
12031   SpaceBetweenBraces.SpacesInAngles = FormatStyle::SIAS_Always;
12032   SpaceBetweenBraces.SpacesInParentheses = true;
12033   SpaceBetweenBraces.SpacesInSquareBrackets = true;
12034   verifyFormat("vector< int > x{ 1, 2, 3, 4 };", SpaceBetweenBraces);
12035   verifyFormat("f( {}, { {}, {} }, MyMap[ { k, v } ] );", SpaceBetweenBraces);
12036   verifyFormat("vector< int > x{ // comment 1\n"
12037                "                 1, 2, 3, 4 };",
12038                SpaceBetweenBraces);
12039   SpaceBetweenBraces.ColumnLimit = 20;
12040   EXPECT_EQ("vector< int > x{\n"
12041             "    1, 2, 3, 4 };",
12042             format("vector<int>x{1,2,3,4};", SpaceBetweenBraces));
12043   SpaceBetweenBraces.ColumnLimit = 24;
12044   EXPECT_EQ("vector< int > x{ 1, 2,\n"
12045             "                 3, 4 };",
12046             format("vector<int>x{1,2,3,4};", SpaceBetweenBraces));
12047   EXPECT_EQ("vector< int > x{\n"
12048             "    1,\n"
12049             "    2,\n"
12050             "    3,\n"
12051             "    4,\n"
12052             "};",
12053             format("vector<int>x{1,2,3,4,};", SpaceBetweenBraces));
12054   verifyFormat("vector< int > x{};", SpaceBetweenBraces);
12055   SpaceBetweenBraces.SpaceInEmptyParentheses = true;
12056   verifyFormat("vector< int > x{ };", SpaceBetweenBraces);
12057 }
12058 
12059 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
12060   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
12061                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
12062                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
12063                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
12064                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
12065                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
12066   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n"
12067                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
12068                "                 1, 22, 333, 4444, 55555, //\n"
12069                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
12070                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
12071   verifyFormat(
12072       "vector<int> x = {1,       22, 333, 4444, 55555, 666666, 7777777,\n"
12073       "                 1,       22, 333, 4444, 55555, 666666, 7777777,\n"
12074       "                 1,       22, 333, 4444, 55555, 666666, // comment\n"
12075       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
12076       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
12077       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
12078       "                 7777777};");
12079   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
12080                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
12081                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
12082   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
12083                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
12084                "    // Separating comment.\n"
12085                "    X86::R8, X86::R9, X86::R10, X86::R11, 0};");
12086   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
12087                "    // Leading comment\n"
12088                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
12089                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
12090   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
12091                "                 1, 1, 1, 1};",
12092                getLLVMStyleWithColumns(39));
12093   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
12094                "                 1, 1, 1, 1};",
12095                getLLVMStyleWithColumns(38));
12096   verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
12097                "    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
12098                getLLVMStyleWithColumns(43));
12099   verifyFormat(
12100       "static unsigned SomeValues[10][3] = {\n"
12101       "    {1, 4, 0},  {4, 9, 0},  {4, 5, 9},  {8, 5, 4}, {1, 8, 4},\n"
12102       "    {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};");
12103   verifyFormat("static auto fields = new vector<string>{\n"
12104                "    \"aaaaaaaaaaaaa\",\n"
12105                "    \"aaaaaaaaaaaaa\",\n"
12106                "    \"aaaaaaaaaaaa\",\n"
12107                "    \"aaaaaaaaaaaaaa\",\n"
12108                "    \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
12109                "    \"aaaaaaaaaaaa\",\n"
12110                "    \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
12111                "};");
12112   verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};");
12113   verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n"
12114                "                 2, bbbbbbbbbbbbbbbbbbbbbb,\n"
12115                "                 3, cccccccccccccccccccccc};",
12116                getLLVMStyleWithColumns(60));
12117 
12118   // Trailing commas.
12119   verifyFormat("vector<int> x = {\n"
12120                "    1, 1, 1, 1, 1, 1, 1, 1,\n"
12121                "};",
12122                getLLVMStyleWithColumns(39));
12123   verifyFormat("vector<int> x = {\n"
12124                "    1, 1, 1, 1, 1, 1, 1, 1, //\n"
12125                "};",
12126                getLLVMStyleWithColumns(39));
12127   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
12128                "                 1, 1, 1, 1,\n"
12129                "                 /**/ /**/};",
12130                getLLVMStyleWithColumns(39));
12131 
12132   // Trailing comment in the first line.
12133   verifyFormat("vector<int> iiiiiiiiiiiiiii = {                      //\n"
12134                "    1111111111, 2222222222, 33333333333, 4444444444, //\n"
12135                "    111111111,  222222222,  3333333333,  444444444,  //\n"
12136                "    11111111,   22222222,   333333333,   44444444};");
12137   // Trailing comment in the last line.
12138   verifyFormat("int aaaaa[] = {\n"
12139                "    1, 2, 3, // comment\n"
12140                "    4, 5, 6  // comment\n"
12141                "};");
12142 
12143   // With nested lists, we should either format one item per line or all nested
12144   // lists one on line.
12145   // FIXME: For some nested lists, we can do better.
12146   verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
12147                "        {aaaaaaaaaaaaaaaaaaa},\n"
12148                "        {aaaaaaaaaaaaaaaaaaaaa},\n"
12149                "        {aaaaaaaaaaaaaaaaa}};",
12150                getLLVMStyleWithColumns(60));
12151   verifyFormat(
12152       "SomeStruct my_struct_array = {\n"
12153       "    {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
12154       "     aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
12155       "    {aaa, aaa},\n"
12156       "    {aaa, aaa},\n"
12157       "    {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
12158       "    {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
12159       "     aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
12160 
12161   // No column layout should be used here.
12162   verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
12163                "                   bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
12164 
12165   verifyNoCrash("a<,");
12166 
12167   // No braced initializer here.
12168   verifyFormat("void f() {\n"
12169                "  struct Dummy {};\n"
12170                "  f(v);\n"
12171                "}");
12172 
12173   // Long lists should be formatted in columns even if they are nested.
12174   verifyFormat(
12175       "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n"
12176       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
12177       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
12178       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
12179       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
12180       "                          1, 22, 333, 4444, 55555, 666666, 7777777});");
12181 
12182   // Allow "single-column" layout even if that violates the column limit. There
12183   // isn't going to be a better way.
12184   verifyFormat("std::vector<int> a = {\n"
12185                "    aaaaaaaa,\n"
12186                "    aaaaaaaa,\n"
12187                "    aaaaaaaa,\n"
12188                "    aaaaaaaa,\n"
12189                "    aaaaaaaaaa,\n"
12190                "    aaaaaaaa,\n"
12191                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa};",
12192                getLLVMStyleWithColumns(30));
12193   verifyFormat("vector<int> aaaa = {\n"
12194                "    aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
12195                "    aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
12196                "    aaaaaa.aaaaaaa,\n"
12197                "    aaaaaa.aaaaaaa,\n"
12198                "    aaaaaa.aaaaaaa,\n"
12199                "    aaaaaa.aaaaaaa,\n"
12200                "};");
12201 
12202   // Don't create hanging lists.
12203   verifyFormat("someFunction(Param, {List1, List2,\n"
12204                "                     List3});",
12205                getLLVMStyleWithColumns(35));
12206   verifyFormat("someFunction(Param, Param,\n"
12207                "             {List1, List2,\n"
12208                "              List3});",
12209                getLLVMStyleWithColumns(35));
12210   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n"
12211                "                               aaaaaaaaaaaaaaaaaaaaaaa);");
12212 }
12213 
12214 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
12215   FormatStyle DoNotMerge = getLLVMStyle();
12216   DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
12217 
12218   verifyFormat("void f() { return 42; }");
12219   verifyFormat("void f() {\n"
12220                "  return 42;\n"
12221                "}",
12222                DoNotMerge);
12223   verifyFormat("void f() {\n"
12224                "  // Comment\n"
12225                "}");
12226   verifyFormat("{\n"
12227                "#error {\n"
12228                "  int a;\n"
12229                "}");
12230   verifyFormat("{\n"
12231                "  int a;\n"
12232                "#error {\n"
12233                "}");
12234   verifyFormat("void f() {} // comment");
12235   verifyFormat("void f() { int a; } // comment");
12236   verifyFormat("void f() {\n"
12237                "} // comment",
12238                DoNotMerge);
12239   verifyFormat("void f() {\n"
12240                "  int a;\n"
12241                "} // comment",
12242                DoNotMerge);
12243   verifyFormat("void f() {\n"
12244                "} // comment",
12245                getLLVMStyleWithColumns(15));
12246 
12247   verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
12248   verifyFormat("void f() {\n  return 42;\n}", getLLVMStyleWithColumns(22));
12249 
12250   verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
12251   verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
12252   verifyFormat("class C {\n"
12253                "  C()\n"
12254                "      : iiiiiiii(nullptr),\n"
12255                "        kkkkkkk(nullptr),\n"
12256                "        mmmmmmm(nullptr),\n"
12257                "        nnnnnnn(nullptr) {}\n"
12258                "};",
12259                getGoogleStyle());
12260 
12261   FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0);
12262   EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit));
12263   EXPECT_EQ("class C {\n"
12264             "  A() : b(0) {}\n"
12265             "};",
12266             format("class C{A():b(0){}};", NoColumnLimit));
12267   EXPECT_EQ("A()\n"
12268             "    : b(0) {\n"
12269             "}",
12270             format("A()\n:b(0)\n{\n}", NoColumnLimit));
12271 
12272   FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
12273   DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
12274       FormatStyle::SFS_None;
12275   EXPECT_EQ("A()\n"
12276             "    : b(0) {\n"
12277             "}",
12278             format("A():b(0){}", DoNotMergeNoColumnLimit));
12279   EXPECT_EQ("A()\n"
12280             "    : b(0) {\n"
12281             "}",
12282             format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit));
12283 
12284   verifyFormat("#define A          \\\n"
12285                "  void f() {       \\\n"
12286                "    int i;         \\\n"
12287                "  }",
12288                getLLVMStyleWithColumns(20));
12289   verifyFormat("#define A           \\\n"
12290                "  void f() { int i; }",
12291                getLLVMStyleWithColumns(21));
12292   verifyFormat("#define A            \\\n"
12293                "  void f() {         \\\n"
12294                "    int i;           \\\n"
12295                "  }                  \\\n"
12296                "  int j;",
12297                getLLVMStyleWithColumns(22));
12298   verifyFormat("#define A             \\\n"
12299                "  void f() { int i; } \\\n"
12300                "  int j;",
12301                getLLVMStyleWithColumns(23));
12302 }
12303 
12304 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) {
12305   FormatStyle MergeEmptyOnly = getLLVMStyle();
12306   MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
12307   verifyFormat("class C {\n"
12308                "  int f() {}\n"
12309                "};",
12310                MergeEmptyOnly);
12311   verifyFormat("class C {\n"
12312                "  int f() {\n"
12313                "    return 42;\n"
12314                "  }\n"
12315                "};",
12316                MergeEmptyOnly);
12317   verifyFormat("int f() {}", MergeEmptyOnly);
12318   verifyFormat("int f() {\n"
12319                "  return 42;\n"
12320                "}",
12321                MergeEmptyOnly);
12322 
12323   // Also verify behavior when BraceWrapping.AfterFunction = true
12324   MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
12325   MergeEmptyOnly.BraceWrapping.AfterFunction = true;
12326   verifyFormat("int f() {}", MergeEmptyOnly);
12327   verifyFormat("class C {\n"
12328                "  int f() {}\n"
12329                "};",
12330                MergeEmptyOnly);
12331 }
12332 
12333 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
12334   FormatStyle MergeInlineOnly = getLLVMStyle();
12335   MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
12336   verifyFormat("class C {\n"
12337                "  int f() { return 42; }\n"
12338                "};",
12339                MergeInlineOnly);
12340   verifyFormat("int f() {\n"
12341                "  return 42;\n"
12342                "}",
12343                MergeInlineOnly);
12344 
12345   // SFS_Inline implies SFS_Empty
12346   verifyFormat("class C {\n"
12347                "  int f() {}\n"
12348                "};",
12349                MergeInlineOnly);
12350   verifyFormat("int f() {}", MergeInlineOnly);
12351 
12352   // Also verify behavior when BraceWrapping.AfterFunction = true
12353   MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
12354   MergeInlineOnly.BraceWrapping.AfterFunction = true;
12355   verifyFormat("class C {\n"
12356                "  int f() { return 42; }\n"
12357                "};",
12358                MergeInlineOnly);
12359   verifyFormat("int f()\n"
12360                "{\n"
12361                "  return 42;\n"
12362                "}",
12363                MergeInlineOnly);
12364 
12365   // SFS_Inline implies SFS_Empty
12366   verifyFormat("int f() {}", MergeInlineOnly);
12367   verifyFormat("class C {\n"
12368                "  int f() {}\n"
12369                "};",
12370                MergeInlineOnly);
12371 
12372   MergeInlineOnly.BraceWrapping.AfterClass = true;
12373   MergeInlineOnly.BraceWrapping.AfterStruct = true;
12374   verifyFormat("class C\n"
12375                "{\n"
12376                "  int f() { return 42; }\n"
12377                "};",
12378                MergeInlineOnly);
12379   verifyFormat("struct C\n"
12380                "{\n"
12381                "  int f() { return 42; }\n"
12382                "};",
12383                MergeInlineOnly);
12384   verifyFormat("int f()\n"
12385                "{\n"
12386                "  return 42;\n"
12387                "}",
12388                MergeInlineOnly);
12389   verifyFormat("int f() {}", MergeInlineOnly);
12390   verifyFormat("class C\n"
12391                "{\n"
12392                "  int f() { return 42; }\n"
12393                "};",
12394                MergeInlineOnly);
12395   verifyFormat("struct C\n"
12396                "{\n"
12397                "  int f() { return 42; }\n"
12398                "};",
12399                MergeInlineOnly);
12400   verifyFormat("struct C\n"
12401                "// comment\n"
12402                "/* comment */\n"
12403                "// comment\n"
12404                "{\n"
12405                "  int f() { return 42; }\n"
12406                "};",
12407                MergeInlineOnly);
12408   verifyFormat("/* comment */ struct C\n"
12409                "{\n"
12410                "  int f() { return 42; }\n"
12411                "};",
12412                MergeInlineOnly);
12413 }
12414 
12415 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) {
12416   FormatStyle MergeInlineOnly = getLLVMStyle();
12417   MergeInlineOnly.AllowShortFunctionsOnASingleLine =
12418       FormatStyle::SFS_InlineOnly;
12419   verifyFormat("class C {\n"
12420                "  int f() { return 42; }\n"
12421                "};",
12422                MergeInlineOnly);
12423   verifyFormat("int f() {\n"
12424                "  return 42;\n"
12425                "}",
12426                MergeInlineOnly);
12427 
12428   // SFS_InlineOnly does not imply SFS_Empty
12429   verifyFormat("class C {\n"
12430                "  int f() {}\n"
12431                "};",
12432                MergeInlineOnly);
12433   verifyFormat("int f() {\n"
12434                "}",
12435                MergeInlineOnly);
12436 
12437   // Also verify behavior when BraceWrapping.AfterFunction = true
12438   MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
12439   MergeInlineOnly.BraceWrapping.AfterFunction = true;
12440   verifyFormat("class C {\n"
12441                "  int f() { return 42; }\n"
12442                "};",
12443                MergeInlineOnly);
12444   verifyFormat("int f()\n"
12445                "{\n"
12446                "  return 42;\n"
12447                "}",
12448                MergeInlineOnly);
12449 
12450   // SFS_InlineOnly does not imply SFS_Empty
12451   verifyFormat("int f()\n"
12452                "{\n"
12453                "}",
12454                MergeInlineOnly);
12455   verifyFormat("class C {\n"
12456                "  int f() {}\n"
12457                "};",
12458                MergeInlineOnly);
12459 }
12460 
12461 TEST_F(FormatTest, SplitEmptyFunction) {
12462   FormatStyle Style = getLLVMStyleWithColumns(40);
12463   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
12464   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12465   Style.BraceWrapping.AfterFunction = true;
12466   Style.BraceWrapping.SplitEmptyFunction = false;
12467 
12468   verifyFormat("int f()\n"
12469                "{}",
12470                Style);
12471   verifyFormat("int f()\n"
12472                "{\n"
12473                "  return 42;\n"
12474                "}",
12475                Style);
12476   verifyFormat("int f()\n"
12477                "{\n"
12478                "  // some comment\n"
12479                "}",
12480                Style);
12481 
12482   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
12483   verifyFormat("int f() {}", Style);
12484   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
12485                "{}",
12486                Style);
12487   verifyFormat("int f()\n"
12488                "{\n"
12489                "  return 0;\n"
12490                "}",
12491                Style);
12492 
12493   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
12494   verifyFormat("class Foo {\n"
12495                "  int f() {}\n"
12496                "};\n",
12497                Style);
12498   verifyFormat("class Foo {\n"
12499                "  int f() { return 0; }\n"
12500                "};\n",
12501                Style);
12502   verifyFormat("class Foo {\n"
12503                "  int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
12504                "  {}\n"
12505                "};\n",
12506                Style);
12507   verifyFormat("class Foo {\n"
12508                "  int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
12509                "  {\n"
12510                "    return 0;\n"
12511                "  }\n"
12512                "};\n",
12513                Style);
12514 
12515   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
12516   verifyFormat("int f() {}", Style);
12517   verifyFormat("int f() { return 0; }", Style);
12518   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
12519                "{}",
12520                Style);
12521   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
12522                "{\n"
12523                "  return 0;\n"
12524                "}",
12525                Style);
12526 }
12527 
12528 TEST_F(FormatTest, SplitEmptyFunctionButNotRecord) {
12529   FormatStyle Style = getLLVMStyleWithColumns(40);
12530   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
12531   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12532   Style.BraceWrapping.AfterFunction = true;
12533   Style.BraceWrapping.SplitEmptyFunction = true;
12534   Style.BraceWrapping.SplitEmptyRecord = false;
12535 
12536   verifyFormat("class C {};", Style);
12537   verifyFormat("struct C {};", Style);
12538   verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
12539                "       int bbbbbbbbbbbbbbbbbbbbbbbb)\n"
12540                "{\n"
12541                "}",
12542                Style);
12543   verifyFormat("class C {\n"
12544                "  C()\n"
12545                "      : aaaaaaaaaaaaaaaaaaaaaaaaaaaa(),\n"
12546                "        bbbbbbbbbbbbbbbbbbb()\n"
12547                "  {\n"
12548                "  }\n"
12549                "  void\n"
12550                "  m(int aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
12551                "    int bbbbbbbbbbbbbbbbbbbbbbbb)\n"
12552                "  {\n"
12553                "  }\n"
12554                "};",
12555                Style);
12556 }
12557 
12558 TEST_F(FormatTest, KeepShortFunctionAfterPPElse) {
12559   FormatStyle Style = getLLVMStyle();
12560   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
12561   verifyFormat("#ifdef A\n"
12562                "int f() {}\n"
12563                "#else\n"
12564                "int g() {}\n"
12565                "#endif",
12566                Style);
12567 }
12568 
12569 TEST_F(FormatTest, SplitEmptyClass) {
12570   FormatStyle Style = getLLVMStyle();
12571   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12572   Style.BraceWrapping.AfterClass = true;
12573   Style.BraceWrapping.SplitEmptyRecord = false;
12574 
12575   verifyFormat("class Foo\n"
12576                "{};",
12577                Style);
12578   verifyFormat("/* something */ class Foo\n"
12579                "{};",
12580                Style);
12581   verifyFormat("template <typename X> class Foo\n"
12582                "{};",
12583                Style);
12584   verifyFormat("class Foo\n"
12585                "{\n"
12586                "  Foo();\n"
12587                "};",
12588                Style);
12589   verifyFormat("typedef class Foo\n"
12590                "{\n"
12591                "} Foo_t;",
12592                Style);
12593 
12594   Style.BraceWrapping.SplitEmptyRecord = true;
12595   Style.BraceWrapping.AfterStruct = true;
12596   verifyFormat("class rep\n"
12597                "{\n"
12598                "};",
12599                Style);
12600   verifyFormat("struct rep\n"
12601                "{\n"
12602                "};",
12603                Style);
12604   verifyFormat("template <typename T> class rep\n"
12605                "{\n"
12606                "};",
12607                Style);
12608   verifyFormat("template <typename T> struct rep\n"
12609                "{\n"
12610                "};",
12611                Style);
12612   verifyFormat("class rep\n"
12613                "{\n"
12614                "  int x;\n"
12615                "};",
12616                Style);
12617   verifyFormat("struct rep\n"
12618                "{\n"
12619                "  int x;\n"
12620                "};",
12621                Style);
12622   verifyFormat("template <typename T> class rep\n"
12623                "{\n"
12624                "  int x;\n"
12625                "};",
12626                Style);
12627   verifyFormat("template <typename T> struct rep\n"
12628                "{\n"
12629                "  int x;\n"
12630                "};",
12631                Style);
12632   verifyFormat("template <typename T> class rep // Foo\n"
12633                "{\n"
12634                "  int x;\n"
12635                "};",
12636                Style);
12637   verifyFormat("template <typename T> struct rep // Bar\n"
12638                "{\n"
12639                "  int x;\n"
12640                "};",
12641                Style);
12642 
12643   verifyFormat("template <typename T> class rep<T>\n"
12644                "{\n"
12645                "  int x;\n"
12646                "};",
12647                Style);
12648 
12649   verifyFormat("template <typename T> class rep<std::complex<T>>\n"
12650                "{\n"
12651                "  int x;\n"
12652                "};",
12653                Style);
12654   verifyFormat("template <typename T> class rep<std::complex<T>>\n"
12655                "{\n"
12656                "};",
12657                Style);
12658 
12659   verifyFormat("#include \"stdint.h\"\n"
12660                "namespace rep {}",
12661                Style);
12662   verifyFormat("#include <stdint.h>\n"
12663                "namespace rep {}",
12664                Style);
12665   verifyFormat("#include <stdint.h>\n"
12666                "namespace rep {}",
12667                "#include <stdint.h>\n"
12668                "namespace rep {\n"
12669                "\n"
12670                "\n"
12671                "}",
12672                Style);
12673 }
12674 
12675 TEST_F(FormatTest, SplitEmptyStruct) {
12676   FormatStyle Style = getLLVMStyle();
12677   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12678   Style.BraceWrapping.AfterStruct = true;
12679   Style.BraceWrapping.SplitEmptyRecord = false;
12680 
12681   verifyFormat("struct Foo\n"
12682                "{};",
12683                Style);
12684   verifyFormat("/* something */ struct Foo\n"
12685                "{};",
12686                Style);
12687   verifyFormat("template <typename X> struct Foo\n"
12688                "{};",
12689                Style);
12690   verifyFormat("struct Foo\n"
12691                "{\n"
12692                "  Foo();\n"
12693                "};",
12694                Style);
12695   verifyFormat("typedef struct Foo\n"
12696                "{\n"
12697                "} Foo_t;",
12698                Style);
12699   // typedef struct Bar {} Bar_t;
12700 }
12701 
12702 TEST_F(FormatTest, SplitEmptyUnion) {
12703   FormatStyle Style = getLLVMStyle();
12704   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12705   Style.BraceWrapping.AfterUnion = true;
12706   Style.BraceWrapping.SplitEmptyRecord = false;
12707 
12708   verifyFormat("union Foo\n"
12709                "{};",
12710                Style);
12711   verifyFormat("/* something */ union Foo\n"
12712                "{};",
12713                Style);
12714   verifyFormat("union Foo\n"
12715                "{\n"
12716                "  A,\n"
12717                "};",
12718                Style);
12719   verifyFormat("typedef union Foo\n"
12720                "{\n"
12721                "} Foo_t;",
12722                Style);
12723 }
12724 
12725 TEST_F(FormatTest, SplitEmptyNamespace) {
12726   FormatStyle Style = getLLVMStyle();
12727   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12728   Style.BraceWrapping.AfterNamespace = true;
12729   Style.BraceWrapping.SplitEmptyNamespace = false;
12730 
12731   verifyFormat("namespace Foo\n"
12732                "{};",
12733                Style);
12734   verifyFormat("/* something */ namespace Foo\n"
12735                "{};",
12736                Style);
12737   verifyFormat("inline namespace Foo\n"
12738                "{};",
12739                Style);
12740   verifyFormat("/* something */ inline namespace Foo\n"
12741                "{};",
12742                Style);
12743   verifyFormat("export namespace Foo\n"
12744                "{};",
12745                Style);
12746   verifyFormat("namespace Foo\n"
12747                "{\n"
12748                "void Bar();\n"
12749                "};",
12750                Style);
12751 }
12752 
12753 TEST_F(FormatTest, NeverMergeShortRecords) {
12754   FormatStyle Style = getLLVMStyle();
12755 
12756   verifyFormat("class Foo {\n"
12757                "  Foo();\n"
12758                "};",
12759                Style);
12760   verifyFormat("typedef class Foo {\n"
12761                "  Foo();\n"
12762                "} Foo_t;",
12763                Style);
12764   verifyFormat("struct Foo {\n"
12765                "  Foo();\n"
12766                "};",
12767                Style);
12768   verifyFormat("typedef struct Foo {\n"
12769                "  Foo();\n"
12770                "} Foo_t;",
12771                Style);
12772   verifyFormat("union Foo {\n"
12773                "  A,\n"
12774                "};",
12775                Style);
12776   verifyFormat("typedef union Foo {\n"
12777                "  A,\n"
12778                "} Foo_t;",
12779                Style);
12780   verifyFormat("namespace Foo {\n"
12781                "void Bar();\n"
12782                "};",
12783                Style);
12784 
12785   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12786   Style.BraceWrapping.AfterClass = true;
12787   Style.BraceWrapping.AfterStruct = true;
12788   Style.BraceWrapping.AfterUnion = true;
12789   Style.BraceWrapping.AfterNamespace = true;
12790   verifyFormat("class Foo\n"
12791                "{\n"
12792                "  Foo();\n"
12793                "};",
12794                Style);
12795   verifyFormat("typedef class Foo\n"
12796                "{\n"
12797                "  Foo();\n"
12798                "} Foo_t;",
12799                Style);
12800   verifyFormat("struct Foo\n"
12801                "{\n"
12802                "  Foo();\n"
12803                "};",
12804                Style);
12805   verifyFormat("typedef struct Foo\n"
12806                "{\n"
12807                "  Foo();\n"
12808                "} Foo_t;",
12809                Style);
12810   verifyFormat("union Foo\n"
12811                "{\n"
12812                "  A,\n"
12813                "};",
12814                Style);
12815   verifyFormat("typedef union Foo\n"
12816                "{\n"
12817                "  A,\n"
12818                "} Foo_t;",
12819                Style);
12820   verifyFormat("namespace Foo\n"
12821                "{\n"
12822                "void Bar();\n"
12823                "};",
12824                Style);
12825 }
12826 
12827 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
12828   // Elaborate type variable declarations.
12829   verifyFormat("struct foo a = {bar};\nint n;");
12830   verifyFormat("class foo a = {bar};\nint n;");
12831   verifyFormat("union foo a = {bar};\nint n;");
12832 
12833   // Elaborate types inside function definitions.
12834   verifyFormat("struct foo f() {}\nint n;");
12835   verifyFormat("class foo f() {}\nint n;");
12836   verifyFormat("union foo f() {}\nint n;");
12837 
12838   // Templates.
12839   verifyFormat("template <class X> void f() {}\nint n;");
12840   verifyFormat("template <struct X> void f() {}\nint n;");
12841   verifyFormat("template <union X> void f() {}\nint n;");
12842 
12843   // Actual definitions...
12844   verifyFormat("struct {\n} n;");
12845   verifyFormat(
12846       "template <template <class T, class Y>, class Z> class X {\n} n;");
12847   verifyFormat("union Z {\n  int n;\n} x;");
12848   verifyFormat("class MACRO Z {\n} n;");
12849   verifyFormat("class MACRO(X) Z {\n} n;");
12850   verifyFormat("class __attribute__(X) Z {\n} n;");
12851   verifyFormat("class __declspec(X) Z {\n} n;");
12852   verifyFormat("class A##B##C {\n} n;");
12853   verifyFormat("class alignas(16) Z {\n} n;");
12854   verifyFormat("class MACRO(X) alignas(16) Z {\n} n;");
12855   verifyFormat("class MACROA MACRO(X) Z {\n} n;");
12856 
12857   // Redefinition from nested context:
12858   verifyFormat("class A::B::C {\n} n;");
12859 
12860   // Template definitions.
12861   verifyFormat(
12862       "template <typename F>\n"
12863       "Matcher(const Matcher<F> &Other,\n"
12864       "        typename enable_if_c<is_base_of<F, T>::value &&\n"
12865       "                             !is_same<F, T>::value>::type * = 0)\n"
12866       "    : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
12867 
12868   // FIXME: This is still incorrectly handled at the formatter side.
12869   verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
12870   verifyFormat("int i = SomeFunction(a<b, a> b);");
12871 
12872   // FIXME:
12873   // This now gets parsed incorrectly as class definition.
12874   // verifyFormat("class A<int> f() {\n}\nint n;");
12875 
12876   // Elaborate types where incorrectly parsing the structural element would
12877   // break the indent.
12878   verifyFormat("if (true)\n"
12879                "  class X x;\n"
12880                "else\n"
12881                "  f();\n");
12882 
12883   // This is simply incomplete. Formatting is not important, but must not crash.
12884   verifyFormat("class A:");
12885 }
12886 
12887 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
12888   EXPECT_EQ("#error Leave     all         white!!!!! space* alone!\n",
12889             format("#error Leave     all         white!!!!! space* alone!\n"));
12890   EXPECT_EQ(
12891       "#warning Leave     all         white!!!!! space* alone!\n",
12892       format("#warning Leave     all         white!!!!! space* alone!\n"));
12893   EXPECT_EQ("#error 1", format("  #  error   1"));
12894   EXPECT_EQ("#warning 1", format("  #  warning 1"));
12895 }
12896 
12897 TEST_F(FormatTest, FormatHashIfExpressions) {
12898   verifyFormat("#if AAAA && BBBB");
12899   verifyFormat("#if (AAAA && BBBB)");
12900   verifyFormat("#elif (AAAA && BBBB)");
12901   // FIXME: Come up with a better indentation for #elif.
12902   verifyFormat(
12903       "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) &&  \\\n"
12904       "    defined(BBBBBBBB)\n"
12905       "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) &&  \\\n"
12906       "    defined(BBBBBBBB)\n"
12907       "#endif",
12908       getLLVMStyleWithColumns(65));
12909 }
12910 
12911 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
12912   FormatStyle AllowsMergedIf = getGoogleStyle();
12913   AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
12914       FormatStyle::SIS_WithoutElse;
12915   verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
12916   verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
12917   verifyFormat("if (true)\n#error E\n  return 42;", AllowsMergedIf);
12918   EXPECT_EQ("if (true) return 42;",
12919             format("if (true)\nreturn 42;", AllowsMergedIf));
12920   FormatStyle ShortMergedIf = AllowsMergedIf;
12921   ShortMergedIf.ColumnLimit = 25;
12922   verifyFormat("#define A \\\n"
12923                "  if (true) return 42;",
12924                ShortMergedIf);
12925   verifyFormat("#define A \\\n"
12926                "  f();    \\\n"
12927                "  if (true)\n"
12928                "#define B",
12929                ShortMergedIf);
12930   verifyFormat("#define A \\\n"
12931                "  f();    \\\n"
12932                "  if (true)\n"
12933                "g();",
12934                ShortMergedIf);
12935   verifyFormat("{\n"
12936                "#ifdef A\n"
12937                "  // Comment\n"
12938                "  if (true) continue;\n"
12939                "#endif\n"
12940                "  // Comment\n"
12941                "  if (true) continue;\n"
12942                "}",
12943                ShortMergedIf);
12944   ShortMergedIf.ColumnLimit = 33;
12945   verifyFormat("#define A \\\n"
12946                "  if constexpr (true) return 42;",
12947                ShortMergedIf);
12948   verifyFormat("#define A \\\n"
12949                "  if CONSTEXPR (true) return 42;",
12950                ShortMergedIf);
12951   ShortMergedIf.ColumnLimit = 29;
12952   verifyFormat("#define A                   \\\n"
12953                "  if (aaaaaaaaaa) return 1; \\\n"
12954                "  return 2;",
12955                ShortMergedIf);
12956   ShortMergedIf.ColumnLimit = 28;
12957   verifyFormat("#define A         \\\n"
12958                "  if (aaaaaaaaaa) \\\n"
12959                "    return 1;     \\\n"
12960                "  return 2;",
12961                ShortMergedIf);
12962   verifyFormat("#define A                \\\n"
12963                "  if constexpr (aaaaaaa) \\\n"
12964                "    return 1;            \\\n"
12965                "  return 2;",
12966                ShortMergedIf);
12967   verifyFormat("#define A                \\\n"
12968                "  if CONSTEXPR (aaaaaaa) \\\n"
12969                "    return 1;            \\\n"
12970                "  return 2;",
12971                ShortMergedIf);
12972 }
12973 
12974 TEST_F(FormatTest, FormatStarDependingOnContext) {
12975   verifyFormat("void f(int *a);");
12976   verifyFormat("void f() { f(fint * b); }");
12977   verifyFormat("class A {\n  void f(int *a);\n};");
12978   verifyFormat("class A {\n  int *a;\n};");
12979   verifyFormat("namespace a {\n"
12980                "namespace b {\n"
12981                "class A {\n"
12982                "  void f() {}\n"
12983                "  int *a;\n"
12984                "};\n"
12985                "} // namespace b\n"
12986                "} // namespace a");
12987 }
12988 
12989 TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
12990   verifyFormat("while");
12991   verifyFormat("operator");
12992 }
12993 
12994 TEST_F(FormatTest, SkipsDeeplyNestedLines) {
12995   // This code would be painfully slow to format if we didn't skip it.
12996   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
12997                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
12998                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
12999                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
13000                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
13001                    "A(1, 1)\n"
13002                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x
13003                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
13004                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
13005                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
13006                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
13007                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
13008                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
13009                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
13010                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
13011                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n");
13012   // Deeply nested part is untouched, rest is formatted.
13013   EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n",
13014             format(std::string("int    i;\n") + Code + "int    j;\n",
13015                    getLLVMStyle(), SC_ExpectIncomplete));
13016 }
13017 
13018 //===----------------------------------------------------------------------===//
13019 // Objective-C tests.
13020 //===----------------------------------------------------------------------===//
13021 
13022 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
13023   verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
13024   EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
13025             format("-(NSUInteger)indexOfObject:(id)anObject;"));
13026   EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
13027   EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
13028   EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
13029             format("-(NSInteger)Method3:(id)anObject;"));
13030   EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
13031             format("-(NSInteger)Method4:(id)anObject;"));
13032   EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
13033             format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
13034   EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
13035             format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
13036   EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject "
13037             "forAllCells:(BOOL)flag;",
13038             format("- (void)sendAction:(SEL)aSelector to:(id)anObject "
13039                    "forAllCells:(BOOL)flag;"));
13040 
13041   // Very long objectiveC method declaration.
13042   verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
13043                "    (SoooooooooooooooooooooomeType *)bbbbbbbbbb;");
13044   verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
13045                "                    inRange:(NSRange)range\n"
13046                "                   outRange:(NSRange)out_range\n"
13047                "                  outRange1:(NSRange)out_range1\n"
13048                "                  outRange2:(NSRange)out_range2\n"
13049                "                  outRange3:(NSRange)out_range3\n"
13050                "                  outRange4:(NSRange)out_range4\n"
13051                "                  outRange5:(NSRange)out_range5\n"
13052                "                  outRange6:(NSRange)out_range6\n"
13053                "                  outRange7:(NSRange)out_range7\n"
13054                "                  outRange8:(NSRange)out_range8\n"
13055                "                  outRange9:(NSRange)out_range9;");
13056 
13057   // When the function name has to be wrapped.
13058   FormatStyle Style = getLLVMStyle();
13059   // ObjC ignores IndentWrappedFunctionNames when wrapping methods
13060   // and always indents instead.
13061   Style.IndentWrappedFunctionNames = false;
13062   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
13063                "    veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
13064                "               anotherName:(NSString)bbbbbbbbbbbbbb {\n"
13065                "}",
13066                Style);
13067   Style.IndentWrappedFunctionNames = true;
13068   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
13069                "    veryLooooooooooongName:(NSString)cccccccccccccc\n"
13070                "               anotherName:(NSString)dddddddddddddd {\n"
13071                "}",
13072                Style);
13073 
13074   verifyFormat("- (int)sum:(vector<int>)numbers;");
13075   verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
13076   // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
13077   // protocol lists (but not for template classes):
13078   // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
13079 
13080   verifyFormat("- (int (*)())foo:(int (*)())f;");
13081   verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
13082 
13083   // If there's no return type (very rare in practice!), LLVM and Google style
13084   // agree.
13085   verifyFormat("- foo;");
13086   verifyFormat("- foo:(int)f;");
13087   verifyGoogleFormat("- foo:(int)foo;");
13088 }
13089 
13090 TEST_F(FormatTest, BreaksStringLiterals) {
13091   EXPECT_EQ("\"some text \"\n"
13092             "\"other\";",
13093             format("\"some text other\";", getLLVMStyleWithColumns(12)));
13094   EXPECT_EQ("\"some text \"\n"
13095             "\"other\";",
13096             format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
13097   EXPECT_EQ(
13098       "#define A  \\\n"
13099       "  \"some \"  \\\n"
13100       "  \"text \"  \\\n"
13101       "  \"other\";",
13102       format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
13103   EXPECT_EQ(
13104       "#define A  \\\n"
13105       "  \"so \"    \\\n"
13106       "  \"text \"  \\\n"
13107       "  \"other\";",
13108       format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
13109 
13110   EXPECT_EQ("\"some text\"",
13111             format("\"some text\"", getLLVMStyleWithColumns(1)));
13112   EXPECT_EQ("\"some text\"",
13113             format("\"some text\"", getLLVMStyleWithColumns(11)));
13114   EXPECT_EQ("\"some \"\n"
13115             "\"text\"",
13116             format("\"some text\"", getLLVMStyleWithColumns(10)));
13117   EXPECT_EQ("\"some \"\n"
13118             "\"text\"",
13119             format("\"some text\"", getLLVMStyleWithColumns(7)));
13120   EXPECT_EQ("\"some\"\n"
13121             "\" tex\"\n"
13122             "\"t\"",
13123             format("\"some text\"", getLLVMStyleWithColumns(6)));
13124   EXPECT_EQ("\"some\"\n"
13125             "\" tex\"\n"
13126             "\" and\"",
13127             format("\"some tex and\"", getLLVMStyleWithColumns(6)));
13128   EXPECT_EQ("\"some\"\n"
13129             "\"/tex\"\n"
13130             "\"/and\"",
13131             format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
13132 
13133   EXPECT_EQ("variable =\n"
13134             "    \"long string \"\n"
13135             "    \"literal\";",
13136             format("variable = \"long string literal\";",
13137                    getLLVMStyleWithColumns(20)));
13138 
13139   EXPECT_EQ("variable = f(\n"
13140             "    \"long string \"\n"
13141             "    \"literal\",\n"
13142             "    short,\n"
13143             "    loooooooooooooooooooong);",
13144             format("variable = f(\"long string literal\", short, "
13145                    "loooooooooooooooooooong);",
13146                    getLLVMStyleWithColumns(20)));
13147 
13148   EXPECT_EQ(
13149       "f(g(\"long string \"\n"
13150       "    \"literal\"),\n"
13151       "  b);",
13152       format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20)));
13153   EXPECT_EQ("f(g(\"long string \"\n"
13154             "    \"literal\",\n"
13155             "    a),\n"
13156             "  b);",
13157             format("f(g(\"long string literal\", a), b);",
13158                    getLLVMStyleWithColumns(20)));
13159   EXPECT_EQ(
13160       "f(\"one two\".split(\n"
13161       "    variable));",
13162       format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
13163   EXPECT_EQ("f(\"one two three four five six \"\n"
13164             "  \"seven\".split(\n"
13165             "      really_looooong_variable));",
13166             format("f(\"one two three four five six seven\"."
13167                    "split(really_looooong_variable));",
13168                    getLLVMStyleWithColumns(33)));
13169 
13170   EXPECT_EQ("f(\"some \"\n"
13171             "  \"text\",\n"
13172             "  other);",
13173             format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
13174 
13175   // Only break as a last resort.
13176   verifyFormat(
13177       "aaaaaaaaaaaaaaaaaaaa(\n"
13178       "    aaaaaaaaaaaaaaaaaaaa,\n"
13179       "    aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
13180 
13181   EXPECT_EQ("\"splitmea\"\n"
13182             "\"trandomp\"\n"
13183             "\"oint\"",
13184             format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
13185 
13186   EXPECT_EQ("\"split/\"\n"
13187             "\"pathat/\"\n"
13188             "\"slashes\"",
13189             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
13190 
13191   EXPECT_EQ("\"split/\"\n"
13192             "\"pathat/\"\n"
13193             "\"slashes\"",
13194             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
13195   EXPECT_EQ("\"split at \"\n"
13196             "\"spaces/at/\"\n"
13197             "\"slashes.at.any$\"\n"
13198             "\"non-alphanumeric%\"\n"
13199             "\"1111111111characte\"\n"
13200             "\"rs\"",
13201             format("\"split at "
13202                    "spaces/at/"
13203                    "slashes.at."
13204                    "any$non-"
13205                    "alphanumeric%"
13206                    "1111111111characte"
13207                    "rs\"",
13208                    getLLVMStyleWithColumns(20)));
13209 
13210   // Verify that splitting the strings understands
13211   // Style::AlwaysBreakBeforeMultilineStrings.
13212   EXPECT_EQ("aaaaaaaaaaaa(\n"
13213             "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
13214             "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
13215             format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa "
13216                    "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
13217                    "aaaaaaaaaaaaaaaaaaaaaa\");",
13218                    getGoogleStyle()));
13219   EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
13220             "       \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
13221             format("return \"aaaaaaaaaaaaaaaaaaaaaa "
13222                    "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
13223                    "aaaaaaaaaaaaaaaaaaaaaa\";",
13224                    getGoogleStyle()));
13225   EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
13226             "                \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
13227             format("llvm::outs() << "
13228                    "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
13229                    "aaaaaaaaaaaaaaaaaaa\";"));
13230   EXPECT_EQ("ffff(\n"
13231             "    {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
13232             "     \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
13233             format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
13234                    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
13235                    getGoogleStyle()));
13236 
13237   FormatStyle Style = getLLVMStyleWithColumns(12);
13238   Style.BreakStringLiterals = false;
13239   EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style));
13240 
13241   FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
13242   AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
13243   EXPECT_EQ("#define A \\\n"
13244             "  \"some \" \\\n"
13245             "  \"text \" \\\n"
13246             "  \"other\";",
13247             format("#define A \"some text other\";", AlignLeft));
13248 }
13249 
13250 TEST_F(FormatTest, BreaksStringLiteralsAtColumnLimit) {
13251   EXPECT_EQ("C a = \"some more \"\n"
13252             "      \"text\";",
13253             format("C a = \"some more text\";", getLLVMStyleWithColumns(18)));
13254 }
13255 
13256 TEST_F(FormatTest, FullyRemoveEmptyLines) {
13257   FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80);
13258   NoEmptyLines.MaxEmptyLinesToKeep = 0;
13259   EXPECT_EQ("int i = a(b());",
13260             format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines));
13261 }
13262 
13263 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
13264   EXPECT_EQ(
13265       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
13266       "(\n"
13267       "    \"x\t\");",
13268       format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
13269              "aaaaaaa("
13270              "\"x\t\");"));
13271 }
13272 
13273 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
13274   EXPECT_EQ(
13275       "u8\"utf8 string \"\n"
13276       "u8\"literal\";",
13277       format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
13278   EXPECT_EQ(
13279       "u\"utf16 string \"\n"
13280       "u\"literal\";",
13281       format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
13282   EXPECT_EQ(
13283       "U\"utf32 string \"\n"
13284       "U\"literal\";",
13285       format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
13286   EXPECT_EQ("L\"wide string \"\n"
13287             "L\"literal\";",
13288             format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
13289   EXPECT_EQ("@\"NSString \"\n"
13290             "@\"literal\";",
13291             format("@\"NSString literal\";", getGoogleStyleWithColumns(19)));
13292   verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26));
13293 
13294   // This input makes clang-format try to split the incomplete unicode escape
13295   // sequence, which used to lead to a crasher.
13296   verifyNoCrash(
13297       "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
13298       getLLVMStyleWithColumns(60));
13299 }
13300 
13301 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) {
13302   FormatStyle Style = getGoogleStyleWithColumns(15);
13303   EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style));
13304   EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style));
13305   EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style));
13306   EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style));
13307   EXPECT_EQ("u8R\"x(raw literal)x\";",
13308             format("u8R\"x(raw literal)x\";", Style));
13309 }
13310 
13311 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
13312   FormatStyle Style = getLLVMStyleWithColumns(20);
13313   EXPECT_EQ(
13314       "_T(\"aaaaaaaaaaaaaa\")\n"
13315       "_T(\"aaaaaaaaaaaaaa\")\n"
13316       "_T(\"aaaaaaaaaaaa\")",
13317       format("  _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
13318   EXPECT_EQ("f(x,\n"
13319             "  _T(\"aaaaaaaaaaaa\")\n"
13320             "  _T(\"aaa\"),\n"
13321             "  z);",
13322             format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style));
13323 
13324   // FIXME: Handle embedded spaces in one iteration.
13325   //  EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
13326   //            "_T(\"aaaaaaaaaaaaa\")\n"
13327   //            "_T(\"aaaaaaaaaaaaa\")\n"
13328   //            "_T(\"a\")",
13329   //            format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
13330   //                   getLLVMStyleWithColumns(20)));
13331   EXPECT_EQ(
13332       "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
13333       format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style));
13334   EXPECT_EQ("f(\n"
13335             "#if !TEST\n"
13336             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
13337             "#endif\n"
13338             ");",
13339             format("f(\n"
13340                    "#if !TEST\n"
13341                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
13342                    "#endif\n"
13343                    ");"));
13344   EXPECT_EQ("f(\n"
13345             "\n"
13346             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));",
13347             format("f(\n"
13348                    "\n"
13349                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));"));
13350   // Regression test for accessing tokens past the end of a vector in the
13351   // TokenLexer.
13352   verifyNoCrash(R"(_T(
13353 "
13354 )
13355 )");
13356 }
13357 
13358 TEST_F(FormatTest, BreaksStringLiteralOperands) {
13359   // In a function call with two operands, the second can be broken with no line
13360   // break before it.
13361   EXPECT_EQ(
13362       "func(a, \"long long \"\n"
13363       "        \"long long\");",
13364       format("func(a, \"long long long long\");", getLLVMStyleWithColumns(24)));
13365   // In a function call with three operands, the second must be broken with a
13366   // line break before it.
13367   EXPECT_EQ("func(a,\n"
13368             "     \"long long long \"\n"
13369             "     \"long\",\n"
13370             "     c);",
13371             format("func(a, \"long long long long\", c);",
13372                    getLLVMStyleWithColumns(24)));
13373   // In a function call with three operands, the third must be broken with a
13374   // line break before it.
13375   EXPECT_EQ("func(a, b,\n"
13376             "     \"long long long \"\n"
13377             "     \"long\");",
13378             format("func(a, b, \"long long long long\");",
13379                    getLLVMStyleWithColumns(24)));
13380   // In a function call with three operands, both the second and the third must
13381   // be broken with a line break before them.
13382   EXPECT_EQ("func(a,\n"
13383             "     \"long long long \"\n"
13384             "     \"long\",\n"
13385             "     \"long long long \"\n"
13386             "     \"long\");",
13387             format("func(a, \"long long long long\", \"long long long long\");",
13388                    getLLVMStyleWithColumns(24)));
13389   // In a chain of << with two operands, the second can be broken with no line
13390   // break before it.
13391   EXPECT_EQ("a << \"line line \"\n"
13392             "     \"line\";",
13393             format("a << \"line line line\";", getLLVMStyleWithColumns(20)));
13394   // In a chain of << with three operands, the second can be broken with no line
13395   // break before it.
13396   EXPECT_EQ(
13397       "abcde << \"line \"\n"
13398       "         \"line line\"\n"
13399       "      << c;",
13400       format("abcde << \"line line line\" << c;", getLLVMStyleWithColumns(20)));
13401   // In a chain of << with three operands, the third must be broken with a line
13402   // break before it.
13403   EXPECT_EQ(
13404       "a << b\n"
13405       "  << \"line line \"\n"
13406       "     \"line\";",
13407       format("a << b << \"line line line\";", getLLVMStyleWithColumns(20)));
13408   // In a chain of << with three operands, the second can be broken with no line
13409   // break before it and the third must be broken with a line break before it.
13410   EXPECT_EQ("abcd << \"line line \"\n"
13411             "        \"line\"\n"
13412             "     << \"line line \"\n"
13413             "        \"line\";",
13414             format("abcd << \"line line line\" << \"line line line\";",
13415                    getLLVMStyleWithColumns(20)));
13416   // In a chain of binary operators with two operands, the second can be broken
13417   // with no line break before it.
13418   EXPECT_EQ(
13419       "abcd + \"line line \"\n"
13420       "       \"line line\";",
13421       format("abcd + \"line line line line\";", getLLVMStyleWithColumns(20)));
13422   // In a chain of binary operators with three operands, the second must be
13423   // broken with a line break before it.
13424   EXPECT_EQ("abcd +\n"
13425             "    \"line line \"\n"
13426             "    \"line line\" +\n"
13427             "    e;",
13428             format("abcd + \"line line line line\" + e;",
13429                    getLLVMStyleWithColumns(20)));
13430   // In a function call with two operands, with AlignAfterOpenBracket enabled,
13431   // the first must be broken with a line break before it.
13432   FormatStyle Style = getLLVMStyleWithColumns(25);
13433   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
13434   EXPECT_EQ("someFunction(\n"
13435             "    \"long long long \"\n"
13436             "    \"long\",\n"
13437             "    a);",
13438             format("someFunction(\"long long long long\", a);", Style));
13439 }
13440 
13441 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
13442   EXPECT_EQ(
13443       "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
13444       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
13445       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
13446       format("aaaaaaaaaaa  =  \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
13447              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
13448              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"));
13449 }
13450 
13451 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
13452   EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);",
13453             format("f(g(R\"x(raw literal)x\",   a), b);", getGoogleStyle()));
13454   EXPECT_EQ("fffffffffff(g(R\"x(\n"
13455             "multiline raw string literal xxxxxxxxxxxxxx\n"
13456             ")x\",\n"
13457             "              a),\n"
13458             "            b);",
13459             format("fffffffffff(g(R\"x(\n"
13460                    "multiline raw string literal xxxxxxxxxxxxxx\n"
13461                    ")x\", a), b);",
13462                    getGoogleStyleWithColumns(20)));
13463   EXPECT_EQ("fffffffffff(\n"
13464             "    g(R\"x(qqq\n"
13465             "multiline raw string literal xxxxxxxxxxxxxx\n"
13466             ")x\",\n"
13467             "      a),\n"
13468             "    b);",
13469             format("fffffffffff(g(R\"x(qqq\n"
13470                    "multiline raw string literal xxxxxxxxxxxxxx\n"
13471                    ")x\", a), b);",
13472                    getGoogleStyleWithColumns(20)));
13473 
13474   EXPECT_EQ("fffffffffff(R\"x(\n"
13475             "multiline raw string literal xxxxxxxxxxxxxx\n"
13476             ")x\");",
13477             format("fffffffffff(R\"x(\n"
13478                    "multiline raw string literal xxxxxxxxxxxxxx\n"
13479                    ")x\");",
13480                    getGoogleStyleWithColumns(20)));
13481   EXPECT_EQ("fffffffffff(R\"x(\n"
13482             "multiline raw string literal xxxxxxxxxxxxxx\n"
13483             ")x\" + bbbbbb);",
13484             format("fffffffffff(R\"x(\n"
13485                    "multiline raw string literal xxxxxxxxxxxxxx\n"
13486                    ")x\" +   bbbbbb);",
13487                    getGoogleStyleWithColumns(20)));
13488   EXPECT_EQ("fffffffffff(\n"
13489             "    R\"x(\n"
13490             "multiline raw string literal xxxxxxxxxxxxxx\n"
13491             ")x\" +\n"
13492             "    bbbbbb);",
13493             format("fffffffffff(\n"
13494                    " R\"x(\n"
13495                    "multiline raw string literal xxxxxxxxxxxxxx\n"
13496                    ")x\" + bbbbbb);",
13497                    getGoogleStyleWithColumns(20)));
13498   EXPECT_EQ("fffffffffff(R\"(single line raw string)\" + bbbbbb);",
13499             format("fffffffffff(\n"
13500                    " R\"(single line raw string)\" + bbbbbb);"));
13501 }
13502 
13503 TEST_F(FormatTest, SkipsUnknownStringLiterals) {
13504   verifyFormat("string a = \"unterminated;");
13505   EXPECT_EQ("function(\"unterminated,\n"
13506             "         OtherParameter);",
13507             format("function(  \"unterminated,\n"
13508                    "    OtherParameter);"));
13509 }
13510 
13511 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
13512   FormatStyle Style = getLLVMStyle();
13513   Style.Standard = FormatStyle::LS_Cpp03;
13514   EXPECT_EQ("#define x(_a) printf(\"foo\" _a);",
13515             format("#define x(_a) printf(\"foo\"_a);", Style));
13516 }
13517 
13518 TEST_F(FormatTest, CppLexVersion) {
13519   FormatStyle Style = getLLVMStyle();
13520   // Formatting of x * y differs if x is a type.
13521   verifyFormat("void foo() { MACRO(a * b); }", Style);
13522   verifyFormat("void foo() { MACRO(int *b); }", Style);
13523 
13524   // LLVM style uses latest lexer.
13525   verifyFormat("void foo() { MACRO(char8_t *b); }", Style);
13526   Style.Standard = FormatStyle::LS_Cpp17;
13527   // But in c++17, char8_t isn't a keyword.
13528   verifyFormat("void foo() { MACRO(char8_t * b); }", Style);
13529 }
13530 
13531 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); }
13532 
13533 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
13534   EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
13535             "             \"ddeeefff\");",
13536             format("someFunction(\"aaabbbcccdddeeefff\");",
13537                    getLLVMStyleWithColumns(25)));
13538   EXPECT_EQ("someFunction1234567890(\n"
13539             "    \"aaabbbcccdddeeefff\");",
13540             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
13541                    getLLVMStyleWithColumns(26)));
13542   EXPECT_EQ("someFunction1234567890(\n"
13543             "    \"aaabbbcccdddeeeff\"\n"
13544             "    \"f\");",
13545             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
13546                    getLLVMStyleWithColumns(25)));
13547   EXPECT_EQ("someFunction1234567890(\n"
13548             "    \"aaabbbcccdddeeeff\"\n"
13549             "    \"f\");",
13550             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
13551                    getLLVMStyleWithColumns(24)));
13552   EXPECT_EQ("someFunction(\n"
13553             "    \"aaabbbcc ddde \"\n"
13554             "    \"efff\");",
13555             format("someFunction(\"aaabbbcc ddde efff\");",
13556                    getLLVMStyleWithColumns(25)));
13557   EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
13558             "             \"ddeeefff\");",
13559             format("someFunction(\"aaabbbccc ddeeefff\");",
13560                    getLLVMStyleWithColumns(25)));
13561   EXPECT_EQ("someFunction1234567890(\n"
13562             "    \"aaabb \"\n"
13563             "    \"cccdddeeefff\");",
13564             format("someFunction1234567890(\"aaabb cccdddeeefff\");",
13565                    getLLVMStyleWithColumns(25)));
13566   EXPECT_EQ("#define A          \\\n"
13567             "  string s =       \\\n"
13568             "      \"123456789\"  \\\n"
13569             "      \"0\";         \\\n"
13570             "  int i;",
13571             format("#define A string s = \"1234567890\"; int i;",
13572                    getLLVMStyleWithColumns(20)));
13573   EXPECT_EQ("someFunction(\n"
13574             "    \"aaabbbcc \"\n"
13575             "    \"dddeeefff\");",
13576             format("someFunction(\"aaabbbcc dddeeefff\");",
13577                    getLLVMStyleWithColumns(25)));
13578 }
13579 
13580 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
13581   EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3)));
13582   EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2)));
13583   EXPECT_EQ("\"test\"\n"
13584             "\"\\n\"",
13585             format("\"test\\n\"", getLLVMStyleWithColumns(7)));
13586   EXPECT_EQ("\"tes\\\\\"\n"
13587             "\"n\"",
13588             format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
13589   EXPECT_EQ("\"\\\\\\\\\"\n"
13590             "\"\\n\"",
13591             format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
13592   EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
13593   EXPECT_EQ("\"\\uff01\"\n"
13594             "\"test\"",
13595             format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
13596   EXPECT_EQ("\"\\Uff01ff02\"",
13597             format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
13598   EXPECT_EQ("\"\\x000000000001\"\n"
13599             "\"next\"",
13600             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
13601   EXPECT_EQ("\"\\x000000000001next\"",
13602             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
13603   EXPECT_EQ("\"\\x000000000001\"",
13604             format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
13605   EXPECT_EQ("\"test\"\n"
13606             "\"\\000000\"\n"
13607             "\"000001\"",
13608             format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
13609   EXPECT_EQ("\"test\\000\"\n"
13610             "\"00000000\"\n"
13611             "\"1\"",
13612             format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
13613 }
13614 
13615 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
13616   verifyFormat("void f() {\n"
13617                "  return g() {}\n"
13618                "  void h() {}");
13619   verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
13620                "g();\n"
13621                "}");
13622 }
13623 
13624 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
13625   verifyFormat(
13626       "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
13627 }
13628 
13629 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
13630   verifyFormat("class X {\n"
13631                "  void f() {\n"
13632                "  }\n"
13633                "};",
13634                getLLVMStyleWithColumns(12));
13635 }
13636 
13637 TEST_F(FormatTest, ConfigurableIndentWidth) {
13638   FormatStyle EightIndent = getLLVMStyleWithColumns(18);
13639   EightIndent.IndentWidth = 8;
13640   EightIndent.ContinuationIndentWidth = 8;
13641   verifyFormat("void f() {\n"
13642                "        someFunction();\n"
13643                "        if (true) {\n"
13644                "                f();\n"
13645                "        }\n"
13646                "}",
13647                EightIndent);
13648   verifyFormat("class X {\n"
13649                "        void f() {\n"
13650                "        }\n"
13651                "};",
13652                EightIndent);
13653   verifyFormat("int x[] = {\n"
13654                "        call(),\n"
13655                "        call()};",
13656                EightIndent);
13657 }
13658 
13659 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
13660   verifyFormat("double\n"
13661                "f();",
13662                getLLVMStyleWithColumns(8));
13663 }
13664 
13665 TEST_F(FormatTest, ConfigurableUseOfTab) {
13666   FormatStyle Tab = getLLVMStyleWithColumns(42);
13667   Tab.IndentWidth = 8;
13668   Tab.UseTab = FormatStyle::UT_Always;
13669   Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left;
13670 
13671   EXPECT_EQ("if (aaaaaaaa && // q\n"
13672             "    bb)\t\t// w\n"
13673             "\t;",
13674             format("if (aaaaaaaa &&// q\n"
13675                    "bb)// w\n"
13676                    ";",
13677                    Tab));
13678   EXPECT_EQ("if (aaa && bbb) // w\n"
13679             "\t;",
13680             format("if(aaa&&bbb)// w\n"
13681                    ";",
13682                    Tab));
13683 
13684   verifyFormat("class X {\n"
13685                "\tvoid f() {\n"
13686                "\t\tsomeFunction(parameter1,\n"
13687                "\t\t\t     parameter2);\n"
13688                "\t}\n"
13689                "};",
13690                Tab);
13691   verifyFormat("#define A                        \\\n"
13692                "\tvoid f() {               \\\n"
13693                "\t\tsomeFunction(    \\\n"
13694                "\t\t    parameter1,  \\\n"
13695                "\t\t    parameter2); \\\n"
13696                "\t}",
13697                Tab);
13698   verifyFormat("int a;\t      // x\n"
13699                "int bbbbbbbb; // x\n",
13700                Tab);
13701 
13702   Tab.TabWidth = 4;
13703   Tab.IndentWidth = 8;
13704   verifyFormat("class TabWidth4Indent8 {\n"
13705                "\t\tvoid f() {\n"
13706                "\t\t\t\tsomeFunction(parameter1,\n"
13707                "\t\t\t\t\t\t\t parameter2);\n"
13708                "\t\t}\n"
13709                "};",
13710                Tab);
13711 
13712   Tab.TabWidth = 4;
13713   Tab.IndentWidth = 4;
13714   verifyFormat("class TabWidth4Indent4 {\n"
13715                "\tvoid f() {\n"
13716                "\t\tsomeFunction(parameter1,\n"
13717                "\t\t\t\t\t parameter2);\n"
13718                "\t}\n"
13719                "};",
13720                Tab);
13721 
13722   Tab.TabWidth = 8;
13723   Tab.IndentWidth = 4;
13724   verifyFormat("class TabWidth8Indent4 {\n"
13725                "    void f() {\n"
13726                "\tsomeFunction(parameter1,\n"
13727                "\t\t     parameter2);\n"
13728                "    }\n"
13729                "};",
13730                Tab);
13731 
13732   Tab.TabWidth = 8;
13733   Tab.IndentWidth = 8;
13734   EXPECT_EQ("/*\n"
13735             "\t      a\t\tcomment\n"
13736             "\t      in multiple lines\n"
13737             "       */",
13738             format("   /*\t \t \n"
13739                    " \t \t a\t\tcomment\t \t\n"
13740                    " \t \t in multiple lines\t\n"
13741                    " \t  */",
13742                    Tab));
13743 
13744   Tab.UseTab = FormatStyle::UT_ForIndentation;
13745   verifyFormat("{\n"
13746                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13747                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13748                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13749                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13750                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13751                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13752                "};",
13753                Tab);
13754   verifyFormat("enum AA {\n"
13755                "\ta1, // Force multiple lines\n"
13756                "\ta2,\n"
13757                "\ta3\n"
13758                "};",
13759                Tab);
13760   EXPECT_EQ("if (aaaaaaaa && // q\n"
13761             "    bb)         // w\n"
13762             "\t;",
13763             format("if (aaaaaaaa &&// q\n"
13764                    "bb)// w\n"
13765                    ";",
13766                    Tab));
13767   verifyFormat("class X {\n"
13768                "\tvoid f() {\n"
13769                "\t\tsomeFunction(parameter1,\n"
13770                "\t\t             parameter2);\n"
13771                "\t}\n"
13772                "};",
13773                Tab);
13774   verifyFormat("{\n"
13775                "\tQ(\n"
13776                "\t    {\n"
13777                "\t\t    int a;\n"
13778                "\t\t    someFunction(aaaaaaaa,\n"
13779                "\t\t                 bbbbbbb);\n"
13780                "\t    },\n"
13781                "\t    p);\n"
13782                "}",
13783                Tab);
13784   EXPECT_EQ("{\n"
13785             "\t/* aaaa\n"
13786             "\t   bbbb */\n"
13787             "}",
13788             format("{\n"
13789                    "/* aaaa\n"
13790                    "   bbbb */\n"
13791                    "}",
13792                    Tab));
13793   EXPECT_EQ("{\n"
13794             "\t/*\n"
13795             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13796             "\t  bbbbbbbbbbbbb\n"
13797             "\t*/\n"
13798             "}",
13799             format("{\n"
13800                    "/*\n"
13801                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13802                    "*/\n"
13803                    "}",
13804                    Tab));
13805   EXPECT_EQ("{\n"
13806             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13807             "\t// bbbbbbbbbbbbb\n"
13808             "}",
13809             format("{\n"
13810                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13811                    "}",
13812                    Tab));
13813   EXPECT_EQ("{\n"
13814             "\t/*\n"
13815             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13816             "\t  bbbbbbbbbbbbb\n"
13817             "\t*/\n"
13818             "}",
13819             format("{\n"
13820                    "\t/*\n"
13821                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13822                    "\t*/\n"
13823                    "}",
13824                    Tab));
13825   EXPECT_EQ("{\n"
13826             "\t/*\n"
13827             "\n"
13828             "\t*/\n"
13829             "}",
13830             format("{\n"
13831                    "\t/*\n"
13832                    "\n"
13833                    "\t*/\n"
13834                    "}",
13835                    Tab));
13836   EXPECT_EQ("{\n"
13837             "\t/*\n"
13838             " asdf\n"
13839             "\t*/\n"
13840             "}",
13841             format("{\n"
13842                    "\t/*\n"
13843                    " asdf\n"
13844                    "\t*/\n"
13845                    "}",
13846                    Tab));
13847 
13848   verifyFormat("void f() {\n"
13849                "\treturn true ? aaaaaaaaaaaaaaaaaa\n"
13850                "\t            : bbbbbbbbbbbbbbbbbb\n"
13851                "}",
13852                Tab);
13853   FormatStyle TabNoBreak = Tab;
13854   TabNoBreak.BreakBeforeTernaryOperators = false;
13855   verifyFormat("void f() {\n"
13856                "\treturn true ? aaaaaaaaaaaaaaaaaa :\n"
13857                "\t              bbbbbbbbbbbbbbbbbb\n"
13858                "}",
13859                TabNoBreak);
13860   verifyFormat("void f() {\n"
13861                "\treturn true ?\n"
13862                "\t           aaaaaaaaaaaaaaaaaaaa :\n"
13863                "\t           bbbbbbbbbbbbbbbbbbbb\n"
13864                "}",
13865                TabNoBreak);
13866 
13867   Tab.UseTab = FormatStyle::UT_Never;
13868   EXPECT_EQ("/*\n"
13869             "              a\t\tcomment\n"
13870             "              in multiple lines\n"
13871             "       */",
13872             format("   /*\t \t \n"
13873                    " \t \t a\t\tcomment\t \t\n"
13874                    " \t \t in multiple lines\t\n"
13875                    " \t  */",
13876                    Tab));
13877   EXPECT_EQ("/* some\n"
13878             "   comment */",
13879             format(" \t \t /* some\n"
13880                    " \t \t    comment */",
13881                    Tab));
13882   EXPECT_EQ("int a; /* some\n"
13883             "   comment */",
13884             format(" \t \t int a; /* some\n"
13885                    " \t \t    comment */",
13886                    Tab));
13887 
13888   EXPECT_EQ("int a; /* some\n"
13889             "comment */",
13890             format(" \t \t int\ta; /* some\n"
13891                    " \t \t    comment */",
13892                    Tab));
13893   EXPECT_EQ("f(\"\t\t\"); /* some\n"
13894             "    comment */",
13895             format(" \t \t f(\"\t\t\"); /* some\n"
13896                    " \t \t    comment */",
13897                    Tab));
13898   EXPECT_EQ("{\n"
13899             "        /*\n"
13900             "         * Comment\n"
13901             "         */\n"
13902             "        int i;\n"
13903             "}",
13904             format("{\n"
13905                    "\t/*\n"
13906                    "\t * Comment\n"
13907                    "\t */\n"
13908                    "\t int i;\n"
13909                    "}",
13910                    Tab));
13911 
13912   Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
13913   Tab.TabWidth = 8;
13914   Tab.IndentWidth = 8;
13915   EXPECT_EQ("if (aaaaaaaa && // q\n"
13916             "    bb)         // w\n"
13917             "\t;",
13918             format("if (aaaaaaaa &&// q\n"
13919                    "bb)// w\n"
13920                    ";",
13921                    Tab));
13922   EXPECT_EQ("if (aaa && bbb) // w\n"
13923             "\t;",
13924             format("if(aaa&&bbb)// w\n"
13925                    ";",
13926                    Tab));
13927   verifyFormat("class X {\n"
13928                "\tvoid f() {\n"
13929                "\t\tsomeFunction(parameter1,\n"
13930                "\t\t\t     parameter2);\n"
13931                "\t}\n"
13932                "};",
13933                Tab);
13934   verifyFormat("#define A                        \\\n"
13935                "\tvoid f() {               \\\n"
13936                "\t\tsomeFunction(    \\\n"
13937                "\t\t    parameter1,  \\\n"
13938                "\t\t    parameter2); \\\n"
13939                "\t}",
13940                Tab);
13941   Tab.TabWidth = 4;
13942   Tab.IndentWidth = 8;
13943   verifyFormat("class TabWidth4Indent8 {\n"
13944                "\t\tvoid f() {\n"
13945                "\t\t\t\tsomeFunction(parameter1,\n"
13946                "\t\t\t\t\t\t\t parameter2);\n"
13947                "\t\t}\n"
13948                "};",
13949                Tab);
13950   Tab.TabWidth = 4;
13951   Tab.IndentWidth = 4;
13952   verifyFormat("class TabWidth4Indent4 {\n"
13953                "\tvoid f() {\n"
13954                "\t\tsomeFunction(parameter1,\n"
13955                "\t\t\t\t\t parameter2);\n"
13956                "\t}\n"
13957                "};",
13958                Tab);
13959   Tab.TabWidth = 8;
13960   Tab.IndentWidth = 4;
13961   verifyFormat("class TabWidth8Indent4 {\n"
13962                "    void f() {\n"
13963                "\tsomeFunction(parameter1,\n"
13964                "\t\t     parameter2);\n"
13965                "    }\n"
13966                "};",
13967                Tab);
13968   Tab.TabWidth = 8;
13969   Tab.IndentWidth = 8;
13970   EXPECT_EQ("/*\n"
13971             "\t      a\t\tcomment\n"
13972             "\t      in multiple lines\n"
13973             "       */",
13974             format("   /*\t \t \n"
13975                    " \t \t a\t\tcomment\t \t\n"
13976                    " \t \t in multiple lines\t\n"
13977                    " \t  */",
13978                    Tab));
13979   verifyFormat("{\n"
13980                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13981                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13982                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13983                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13984                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13985                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13986                "};",
13987                Tab);
13988   verifyFormat("enum AA {\n"
13989                "\ta1, // Force multiple lines\n"
13990                "\ta2,\n"
13991                "\ta3\n"
13992                "};",
13993                Tab);
13994   EXPECT_EQ("if (aaaaaaaa && // q\n"
13995             "    bb)         // w\n"
13996             "\t;",
13997             format("if (aaaaaaaa &&// q\n"
13998                    "bb)// w\n"
13999                    ";",
14000                    Tab));
14001   verifyFormat("class X {\n"
14002                "\tvoid f() {\n"
14003                "\t\tsomeFunction(parameter1,\n"
14004                "\t\t\t     parameter2);\n"
14005                "\t}\n"
14006                "};",
14007                Tab);
14008   verifyFormat("{\n"
14009                "\tQ(\n"
14010                "\t    {\n"
14011                "\t\t    int a;\n"
14012                "\t\t    someFunction(aaaaaaaa,\n"
14013                "\t\t\t\t bbbbbbb);\n"
14014                "\t    },\n"
14015                "\t    p);\n"
14016                "}",
14017                Tab);
14018   EXPECT_EQ("{\n"
14019             "\t/* aaaa\n"
14020             "\t   bbbb */\n"
14021             "}",
14022             format("{\n"
14023                    "/* aaaa\n"
14024                    "   bbbb */\n"
14025                    "}",
14026                    Tab));
14027   EXPECT_EQ("{\n"
14028             "\t/*\n"
14029             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
14030             "\t  bbbbbbbbbbbbb\n"
14031             "\t*/\n"
14032             "}",
14033             format("{\n"
14034                    "/*\n"
14035                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
14036                    "*/\n"
14037                    "}",
14038                    Tab));
14039   EXPECT_EQ("{\n"
14040             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
14041             "\t// bbbbbbbbbbbbb\n"
14042             "}",
14043             format("{\n"
14044                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
14045                    "}",
14046                    Tab));
14047   EXPECT_EQ("{\n"
14048             "\t/*\n"
14049             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
14050             "\t  bbbbbbbbbbbbb\n"
14051             "\t*/\n"
14052             "}",
14053             format("{\n"
14054                    "\t/*\n"
14055                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
14056                    "\t*/\n"
14057                    "}",
14058                    Tab));
14059   EXPECT_EQ("{\n"
14060             "\t/*\n"
14061             "\n"
14062             "\t*/\n"
14063             "}",
14064             format("{\n"
14065                    "\t/*\n"
14066                    "\n"
14067                    "\t*/\n"
14068                    "}",
14069                    Tab));
14070   EXPECT_EQ("{\n"
14071             "\t/*\n"
14072             " asdf\n"
14073             "\t*/\n"
14074             "}",
14075             format("{\n"
14076                    "\t/*\n"
14077                    " asdf\n"
14078                    "\t*/\n"
14079                    "}",
14080                    Tab));
14081   EXPECT_EQ("/* some\n"
14082             "   comment */",
14083             format(" \t \t /* some\n"
14084                    " \t \t    comment */",
14085                    Tab));
14086   EXPECT_EQ("int a; /* some\n"
14087             "   comment */",
14088             format(" \t \t int a; /* some\n"
14089                    " \t \t    comment */",
14090                    Tab));
14091   EXPECT_EQ("int a; /* some\n"
14092             "comment */",
14093             format(" \t \t int\ta; /* some\n"
14094                    " \t \t    comment */",
14095                    Tab));
14096   EXPECT_EQ("f(\"\t\t\"); /* some\n"
14097             "    comment */",
14098             format(" \t \t f(\"\t\t\"); /* some\n"
14099                    " \t \t    comment */",
14100                    Tab));
14101   EXPECT_EQ("{\n"
14102             "\t/*\n"
14103             "\t * Comment\n"
14104             "\t */\n"
14105             "\tint i;\n"
14106             "}",
14107             format("{\n"
14108                    "\t/*\n"
14109                    "\t * Comment\n"
14110                    "\t */\n"
14111                    "\t int i;\n"
14112                    "}",
14113                    Tab));
14114   Tab.TabWidth = 2;
14115   Tab.IndentWidth = 2;
14116   EXPECT_EQ("{\n"
14117             "\t/* aaaa\n"
14118             "\t\t bbbb */\n"
14119             "}",
14120             format("{\n"
14121                    "/* aaaa\n"
14122                    "\t bbbb */\n"
14123                    "}",
14124                    Tab));
14125   EXPECT_EQ("{\n"
14126             "\t/*\n"
14127             "\t\taaaaaaaaaaaaaaaaaaaaaaaaaa\n"
14128             "\t\tbbbbbbbbbbbbb\n"
14129             "\t*/\n"
14130             "}",
14131             format("{\n"
14132                    "/*\n"
14133                    "\taaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
14134                    "*/\n"
14135                    "}",
14136                    Tab));
14137   Tab.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
14138   Tab.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
14139   Tab.TabWidth = 4;
14140   Tab.IndentWidth = 4;
14141   verifyFormat("class Assign {\n"
14142                "\tvoid f() {\n"
14143                "\t\tint         x      = 123;\n"
14144                "\t\tint         random = 4;\n"
14145                "\t\tstd::string alphabet =\n"
14146                "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
14147                "\t}\n"
14148                "};",
14149                Tab);
14150 
14151   Tab.UseTab = FormatStyle::UT_AlignWithSpaces;
14152   Tab.TabWidth = 8;
14153   Tab.IndentWidth = 8;
14154   EXPECT_EQ("if (aaaaaaaa && // q\n"
14155             "    bb)         // w\n"
14156             "\t;",
14157             format("if (aaaaaaaa &&// q\n"
14158                    "bb)// w\n"
14159                    ";",
14160                    Tab));
14161   EXPECT_EQ("if (aaa && bbb) // w\n"
14162             "\t;",
14163             format("if(aaa&&bbb)// w\n"
14164                    ";",
14165                    Tab));
14166   verifyFormat("class X {\n"
14167                "\tvoid f() {\n"
14168                "\t\tsomeFunction(parameter1,\n"
14169                "\t\t             parameter2);\n"
14170                "\t}\n"
14171                "};",
14172                Tab);
14173   verifyFormat("#define A                        \\\n"
14174                "\tvoid f() {               \\\n"
14175                "\t\tsomeFunction(    \\\n"
14176                "\t\t    parameter1,  \\\n"
14177                "\t\t    parameter2); \\\n"
14178                "\t}",
14179                Tab);
14180   Tab.TabWidth = 4;
14181   Tab.IndentWidth = 8;
14182   verifyFormat("class TabWidth4Indent8 {\n"
14183                "\t\tvoid f() {\n"
14184                "\t\t\t\tsomeFunction(parameter1,\n"
14185                "\t\t\t\t             parameter2);\n"
14186                "\t\t}\n"
14187                "};",
14188                Tab);
14189   Tab.TabWidth = 4;
14190   Tab.IndentWidth = 4;
14191   verifyFormat("class TabWidth4Indent4 {\n"
14192                "\tvoid f() {\n"
14193                "\t\tsomeFunction(parameter1,\n"
14194                "\t\t             parameter2);\n"
14195                "\t}\n"
14196                "};",
14197                Tab);
14198   Tab.TabWidth = 8;
14199   Tab.IndentWidth = 4;
14200   verifyFormat("class TabWidth8Indent4 {\n"
14201                "    void f() {\n"
14202                "\tsomeFunction(parameter1,\n"
14203                "\t             parameter2);\n"
14204                "    }\n"
14205                "};",
14206                Tab);
14207   Tab.TabWidth = 8;
14208   Tab.IndentWidth = 8;
14209   EXPECT_EQ("/*\n"
14210             "              a\t\tcomment\n"
14211             "              in multiple lines\n"
14212             "       */",
14213             format("   /*\t \t \n"
14214                    " \t \t a\t\tcomment\t \t\n"
14215                    " \t \t in multiple lines\t\n"
14216                    " \t  */",
14217                    Tab));
14218   verifyFormat("{\n"
14219                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
14220                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
14221                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
14222                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
14223                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
14224                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
14225                "};",
14226                Tab);
14227   verifyFormat("enum AA {\n"
14228                "\ta1, // Force multiple lines\n"
14229                "\ta2,\n"
14230                "\ta3\n"
14231                "};",
14232                Tab);
14233   EXPECT_EQ("if (aaaaaaaa && // q\n"
14234             "    bb)         // w\n"
14235             "\t;",
14236             format("if (aaaaaaaa &&// q\n"
14237                    "bb)// w\n"
14238                    ";",
14239                    Tab));
14240   verifyFormat("class X {\n"
14241                "\tvoid f() {\n"
14242                "\t\tsomeFunction(parameter1,\n"
14243                "\t\t             parameter2);\n"
14244                "\t}\n"
14245                "};",
14246                Tab);
14247   verifyFormat("{\n"
14248                "\tQ(\n"
14249                "\t    {\n"
14250                "\t\t    int a;\n"
14251                "\t\t    someFunction(aaaaaaaa,\n"
14252                "\t\t                 bbbbbbb);\n"
14253                "\t    },\n"
14254                "\t    p);\n"
14255                "}",
14256                Tab);
14257   EXPECT_EQ("{\n"
14258             "\t/* aaaa\n"
14259             "\t   bbbb */\n"
14260             "}",
14261             format("{\n"
14262                    "/* aaaa\n"
14263                    "   bbbb */\n"
14264                    "}",
14265                    Tab));
14266   EXPECT_EQ("{\n"
14267             "\t/*\n"
14268             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
14269             "\t  bbbbbbbbbbbbb\n"
14270             "\t*/\n"
14271             "}",
14272             format("{\n"
14273                    "/*\n"
14274                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
14275                    "*/\n"
14276                    "}",
14277                    Tab));
14278   EXPECT_EQ("{\n"
14279             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
14280             "\t// bbbbbbbbbbbbb\n"
14281             "}",
14282             format("{\n"
14283                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
14284                    "}",
14285                    Tab));
14286   EXPECT_EQ("{\n"
14287             "\t/*\n"
14288             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
14289             "\t  bbbbbbbbbbbbb\n"
14290             "\t*/\n"
14291             "}",
14292             format("{\n"
14293                    "\t/*\n"
14294                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
14295                    "\t*/\n"
14296                    "}",
14297                    Tab));
14298   EXPECT_EQ("{\n"
14299             "\t/*\n"
14300             "\n"
14301             "\t*/\n"
14302             "}",
14303             format("{\n"
14304                    "\t/*\n"
14305                    "\n"
14306                    "\t*/\n"
14307                    "}",
14308                    Tab));
14309   EXPECT_EQ("{\n"
14310             "\t/*\n"
14311             " asdf\n"
14312             "\t*/\n"
14313             "}",
14314             format("{\n"
14315                    "\t/*\n"
14316                    " asdf\n"
14317                    "\t*/\n"
14318                    "}",
14319                    Tab));
14320   EXPECT_EQ("/* some\n"
14321             "   comment */",
14322             format(" \t \t /* some\n"
14323                    " \t \t    comment */",
14324                    Tab));
14325   EXPECT_EQ("int a; /* some\n"
14326             "   comment */",
14327             format(" \t \t int a; /* some\n"
14328                    " \t \t    comment */",
14329                    Tab));
14330   EXPECT_EQ("int a; /* some\n"
14331             "comment */",
14332             format(" \t \t int\ta; /* some\n"
14333                    " \t \t    comment */",
14334                    Tab));
14335   EXPECT_EQ("f(\"\t\t\"); /* some\n"
14336             "    comment */",
14337             format(" \t \t f(\"\t\t\"); /* some\n"
14338                    " \t \t    comment */",
14339                    Tab));
14340   EXPECT_EQ("{\n"
14341             "\t/*\n"
14342             "\t * Comment\n"
14343             "\t */\n"
14344             "\tint i;\n"
14345             "}",
14346             format("{\n"
14347                    "\t/*\n"
14348                    "\t * Comment\n"
14349                    "\t */\n"
14350                    "\t int i;\n"
14351                    "}",
14352                    Tab));
14353   Tab.TabWidth = 2;
14354   Tab.IndentWidth = 2;
14355   EXPECT_EQ("{\n"
14356             "\t/* aaaa\n"
14357             "\t   bbbb */\n"
14358             "}",
14359             format("{\n"
14360                    "/* aaaa\n"
14361                    "   bbbb */\n"
14362                    "}",
14363                    Tab));
14364   EXPECT_EQ("{\n"
14365             "\t/*\n"
14366             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
14367             "\t  bbbbbbbbbbbbb\n"
14368             "\t*/\n"
14369             "}",
14370             format("{\n"
14371                    "/*\n"
14372                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
14373                    "*/\n"
14374                    "}",
14375                    Tab));
14376   Tab.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
14377   Tab.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
14378   Tab.TabWidth = 4;
14379   Tab.IndentWidth = 4;
14380   verifyFormat("class Assign {\n"
14381                "\tvoid f() {\n"
14382                "\t\tint         x      = 123;\n"
14383                "\t\tint         random = 4;\n"
14384                "\t\tstd::string alphabet =\n"
14385                "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
14386                "\t}\n"
14387                "};",
14388                Tab);
14389   Tab.AlignOperands = FormatStyle::OAS_Align;
14390   verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb +\n"
14391                "                 cccccccccccccccccccc;",
14392                Tab);
14393   // no alignment
14394   verifyFormat("int aaaaaaaaaa =\n"
14395                "\tbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
14396                Tab);
14397   verifyFormat("return aaaaaaaaaaaaaaaa ? 111111111111111\n"
14398                "       : bbbbbbbbbbbbbb ? 222222222222222\n"
14399                "                        : 333333333333333;",
14400                Tab);
14401   Tab.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
14402   Tab.AlignOperands = FormatStyle::OAS_AlignAfterOperator;
14403   verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb\n"
14404                "               + cccccccccccccccccccc;",
14405                Tab);
14406 }
14407 
14408 TEST_F(FormatTest, ZeroTabWidth) {
14409   FormatStyle Tab = getLLVMStyleWithColumns(42);
14410   Tab.IndentWidth = 8;
14411   Tab.UseTab = FormatStyle::UT_Never;
14412   Tab.TabWidth = 0;
14413   EXPECT_EQ("void a(){\n"
14414             "    // line starts with '\t'\n"
14415             "};",
14416             format("void a(){\n"
14417                    "\t// line starts with '\t'\n"
14418                    "};",
14419                    Tab));
14420 
14421   EXPECT_EQ("void a(){\n"
14422             "    // line starts with '\t'\n"
14423             "};",
14424             format("void a(){\n"
14425                    "\t\t// line starts with '\t'\n"
14426                    "};",
14427                    Tab));
14428 
14429   Tab.UseTab = FormatStyle::UT_ForIndentation;
14430   EXPECT_EQ("void a(){\n"
14431             "    // line starts with '\t'\n"
14432             "};",
14433             format("void a(){\n"
14434                    "\t// line starts with '\t'\n"
14435                    "};",
14436                    Tab));
14437 
14438   EXPECT_EQ("void a(){\n"
14439             "    // line starts with '\t'\n"
14440             "};",
14441             format("void a(){\n"
14442                    "\t\t// line starts with '\t'\n"
14443                    "};",
14444                    Tab));
14445 
14446   Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
14447   EXPECT_EQ("void a(){\n"
14448             "    // line starts with '\t'\n"
14449             "};",
14450             format("void a(){\n"
14451                    "\t// line starts with '\t'\n"
14452                    "};",
14453                    Tab));
14454 
14455   EXPECT_EQ("void a(){\n"
14456             "    // line starts with '\t'\n"
14457             "};",
14458             format("void a(){\n"
14459                    "\t\t// line starts with '\t'\n"
14460                    "};",
14461                    Tab));
14462 
14463   Tab.UseTab = FormatStyle::UT_AlignWithSpaces;
14464   EXPECT_EQ("void a(){\n"
14465             "    // line starts with '\t'\n"
14466             "};",
14467             format("void a(){\n"
14468                    "\t// line starts with '\t'\n"
14469                    "};",
14470                    Tab));
14471 
14472   EXPECT_EQ("void a(){\n"
14473             "    // line starts with '\t'\n"
14474             "};",
14475             format("void a(){\n"
14476                    "\t\t// line starts with '\t'\n"
14477                    "};",
14478                    Tab));
14479 
14480   Tab.UseTab = FormatStyle::UT_Always;
14481   EXPECT_EQ("void a(){\n"
14482             "// line starts with '\t'\n"
14483             "};",
14484             format("void a(){\n"
14485                    "\t// line starts with '\t'\n"
14486                    "};",
14487                    Tab));
14488 
14489   EXPECT_EQ("void a(){\n"
14490             "// line starts with '\t'\n"
14491             "};",
14492             format("void a(){\n"
14493                    "\t\t// line starts with '\t'\n"
14494                    "};",
14495                    Tab));
14496 }
14497 
14498 TEST_F(FormatTest, CalculatesOriginalColumn) {
14499   EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
14500             "q\"; /* some\n"
14501             "       comment */",
14502             format("  \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
14503                    "q\"; /* some\n"
14504                    "       comment */",
14505                    getLLVMStyle()));
14506   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
14507             "/* some\n"
14508             "   comment */",
14509             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
14510                    " /* some\n"
14511                    "    comment */",
14512                    getLLVMStyle()));
14513   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
14514             "qqq\n"
14515             "/* some\n"
14516             "   comment */",
14517             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
14518                    "qqq\n"
14519                    " /* some\n"
14520                    "    comment */",
14521                    getLLVMStyle()));
14522   EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
14523             "wwww; /* some\n"
14524             "         comment */",
14525             format("  inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
14526                    "wwww; /* some\n"
14527                    "         comment */",
14528                    getLLVMStyle()));
14529 }
14530 
14531 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
14532   FormatStyle NoSpace = getLLVMStyle();
14533   NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
14534 
14535   verifyFormat("while(true)\n"
14536                "  continue;",
14537                NoSpace);
14538   verifyFormat("for(;;)\n"
14539                "  continue;",
14540                NoSpace);
14541   verifyFormat("if(true)\n"
14542                "  f();\n"
14543                "else if(true)\n"
14544                "  f();",
14545                NoSpace);
14546   verifyFormat("do {\n"
14547                "  do_something();\n"
14548                "} while(something());",
14549                NoSpace);
14550   verifyFormat("switch(x) {\n"
14551                "default:\n"
14552                "  break;\n"
14553                "}",
14554                NoSpace);
14555   verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
14556   verifyFormat("size_t x = sizeof(x);", NoSpace);
14557   verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
14558   verifyFormat("auto f(int x) -> typeof(x);", NoSpace);
14559   verifyFormat("auto f(int x) -> _Atomic(x);", NoSpace);
14560   verifyFormat("auto f(int x) -> __underlying_type(x);", NoSpace);
14561   verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
14562   verifyFormat("alignas(128) char a[128];", NoSpace);
14563   verifyFormat("size_t x = alignof(MyType);", NoSpace);
14564   verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
14565   verifyFormat("int f() throw(Deprecated);", NoSpace);
14566   verifyFormat("typedef void (*cb)(int);", NoSpace);
14567   verifyFormat("T A::operator()();", NoSpace);
14568   verifyFormat("X A::operator++(T);", NoSpace);
14569   verifyFormat("auto lambda = []() { return 0; };", NoSpace);
14570 
14571   FormatStyle Space = getLLVMStyle();
14572   Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
14573 
14574   verifyFormat("int f ();", Space);
14575   verifyFormat("void f (int a, T b) {\n"
14576                "  while (true)\n"
14577                "    continue;\n"
14578                "}",
14579                Space);
14580   verifyFormat("if (true)\n"
14581                "  f ();\n"
14582                "else if (true)\n"
14583                "  f ();",
14584                Space);
14585   verifyFormat("do {\n"
14586                "  do_something ();\n"
14587                "} while (something ());",
14588                Space);
14589   verifyFormat("switch (x) {\n"
14590                "default:\n"
14591                "  break;\n"
14592                "}",
14593                Space);
14594   verifyFormat("A::A () : a (1) {}", Space);
14595   verifyFormat("void f () __attribute__ ((asdf));", Space);
14596   verifyFormat("*(&a + 1);\n"
14597                "&((&a)[1]);\n"
14598                "a[(b + c) * d];\n"
14599                "(((a + 1) * 2) + 3) * 4;",
14600                Space);
14601   verifyFormat("#define A(x) x", Space);
14602   verifyFormat("#define A (x) x", Space);
14603   verifyFormat("#if defined(x)\n"
14604                "#endif",
14605                Space);
14606   verifyFormat("auto i = std::make_unique<int> (5);", Space);
14607   verifyFormat("size_t x = sizeof (x);", Space);
14608   verifyFormat("auto f (int x) -> decltype (x);", Space);
14609   verifyFormat("auto f (int x) -> typeof (x);", Space);
14610   verifyFormat("auto f (int x) -> _Atomic (x);", Space);
14611   verifyFormat("auto f (int x) -> __underlying_type (x);", Space);
14612   verifyFormat("int f (T x) noexcept (x.create ());", Space);
14613   verifyFormat("alignas (128) char a[128];", Space);
14614   verifyFormat("size_t x = alignof (MyType);", Space);
14615   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
14616   verifyFormat("int f () throw (Deprecated);", Space);
14617   verifyFormat("typedef void (*cb) (int);", Space);
14618   // FIXME these tests regressed behaviour.
14619   // verifyFormat("T A::operator() ();", Space);
14620   // verifyFormat("X A::operator++ (T);", Space);
14621   verifyFormat("auto lambda = [] () { return 0; };", Space);
14622   verifyFormat("int x = int (y);", Space);
14623 
14624   FormatStyle SomeSpace = getLLVMStyle();
14625   SomeSpace.SpaceBeforeParens = FormatStyle::SBPO_NonEmptyParentheses;
14626 
14627   verifyFormat("[]() -> float {}", SomeSpace);
14628   verifyFormat("[] (auto foo) {}", SomeSpace);
14629   verifyFormat("[foo]() -> int {}", SomeSpace);
14630   verifyFormat("int f();", SomeSpace);
14631   verifyFormat("void f (int a, T b) {\n"
14632                "  while (true)\n"
14633                "    continue;\n"
14634                "}",
14635                SomeSpace);
14636   verifyFormat("if (true)\n"
14637                "  f();\n"
14638                "else if (true)\n"
14639                "  f();",
14640                SomeSpace);
14641   verifyFormat("do {\n"
14642                "  do_something();\n"
14643                "} while (something());",
14644                SomeSpace);
14645   verifyFormat("switch (x) {\n"
14646                "default:\n"
14647                "  break;\n"
14648                "}",
14649                SomeSpace);
14650   verifyFormat("A::A() : a (1) {}", SomeSpace);
14651   verifyFormat("void f() __attribute__ ((asdf));", SomeSpace);
14652   verifyFormat("*(&a + 1);\n"
14653                "&((&a)[1]);\n"
14654                "a[(b + c) * d];\n"
14655                "(((a + 1) * 2) + 3) * 4;",
14656                SomeSpace);
14657   verifyFormat("#define A(x) x", SomeSpace);
14658   verifyFormat("#define A (x) x", SomeSpace);
14659   verifyFormat("#if defined(x)\n"
14660                "#endif",
14661                SomeSpace);
14662   verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace);
14663   verifyFormat("size_t x = sizeof (x);", SomeSpace);
14664   verifyFormat("auto f (int x) -> decltype (x);", SomeSpace);
14665   verifyFormat("auto f (int x) -> typeof (x);", SomeSpace);
14666   verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace);
14667   verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace);
14668   verifyFormat("int f (T x) noexcept (x.create());", SomeSpace);
14669   verifyFormat("alignas (128) char a[128];", SomeSpace);
14670   verifyFormat("size_t x = alignof (MyType);", SomeSpace);
14671   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");",
14672                SomeSpace);
14673   verifyFormat("int f() throw (Deprecated);", SomeSpace);
14674   verifyFormat("typedef void (*cb) (int);", SomeSpace);
14675   verifyFormat("T A::operator()();", SomeSpace);
14676   // FIXME these tests regressed behaviour.
14677   // verifyFormat("X A::operator++ (T);", SomeSpace);
14678   verifyFormat("int x = int (y);", SomeSpace);
14679   verifyFormat("auto lambda = []() { return 0; };", SomeSpace);
14680 
14681   FormatStyle SpaceControlStatements = getLLVMStyle();
14682   SpaceControlStatements.SpaceBeforeParens = FormatStyle::SBPO_Custom;
14683   SpaceControlStatements.SpaceBeforeParensOptions.AfterControlStatements = true;
14684 
14685   verifyFormat("while (true)\n"
14686                "  continue;",
14687                SpaceControlStatements);
14688   verifyFormat("if (true)\n"
14689                "  f();\n"
14690                "else if (true)\n"
14691                "  f();",
14692                SpaceControlStatements);
14693   verifyFormat("for (;;) {\n"
14694                "  do_something();\n"
14695                "}",
14696                SpaceControlStatements);
14697   verifyFormat("do {\n"
14698                "  do_something();\n"
14699                "} while (something());",
14700                SpaceControlStatements);
14701   verifyFormat("switch (x) {\n"
14702                "default:\n"
14703                "  break;\n"
14704                "}",
14705                SpaceControlStatements);
14706 
14707   FormatStyle SpaceFuncDecl = getLLVMStyle();
14708   SpaceFuncDecl.SpaceBeforeParens = FormatStyle::SBPO_Custom;
14709   SpaceFuncDecl.SpaceBeforeParensOptions.AfterFunctionDeclarationName = true;
14710 
14711   verifyFormat("int f ();", SpaceFuncDecl);
14712   verifyFormat("void f(int a, T b) {}", SpaceFuncDecl);
14713   verifyFormat("A::A() : a(1) {}", SpaceFuncDecl);
14714   verifyFormat("void f () __attribute__((asdf));", SpaceFuncDecl);
14715   verifyFormat("#define A(x) x", SpaceFuncDecl);
14716   verifyFormat("#define A (x) x", SpaceFuncDecl);
14717   verifyFormat("#if defined(x)\n"
14718                "#endif",
14719                SpaceFuncDecl);
14720   verifyFormat("auto i = std::make_unique<int>(5);", SpaceFuncDecl);
14721   verifyFormat("size_t x = sizeof(x);", SpaceFuncDecl);
14722   verifyFormat("auto f (int x) -> decltype(x);", SpaceFuncDecl);
14723   verifyFormat("auto f (int x) -> typeof(x);", SpaceFuncDecl);
14724   verifyFormat("auto f (int x) -> _Atomic(x);", SpaceFuncDecl);
14725   verifyFormat("auto f (int x) -> __underlying_type(x);", SpaceFuncDecl);
14726   verifyFormat("int f (T x) noexcept(x.create());", SpaceFuncDecl);
14727   verifyFormat("alignas(128) char a[128];", SpaceFuncDecl);
14728   verifyFormat("size_t x = alignof(MyType);", SpaceFuncDecl);
14729   verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");",
14730                SpaceFuncDecl);
14731   verifyFormat("int f () throw(Deprecated);", SpaceFuncDecl);
14732   verifyFormat("typedef void (*cb)(int);", SpaceFuncDecl);
14733   // FIXME these tests regressed behaviour.
14734   // verifyFormat("T A::operator() ();", SpaceFuncDecl);
14735   // verifyFormat("X A::operator++ (T);", SpaceFuncDecl);
14736   verifyFormat("T A::operator()() {}", SpaceFuncDecl);
14737   verifyFormat("auto lambda = []() { return 0; };", SpaceFuncDecl);
14738   verifyFormat("int x = int(y);", SpaceFuncDecl);
14739   verifyFormat("M(std::size_t R, std::size_t C) : C(C), data(R) {}",
14740                SpaceFuncDecl);
14741 
14742   FormatStyle SpaceFuncDef = getLLVMStyle();
14743   SpaceFuncDef.SpaceBeforeParens = FormatStyle::SBPO_Custom;
14744   SpaceFuncDef.SpaceBeforeParensOptions.AfterFunctionDefinitionName = true;
14745 
14746   verifyFormat("int f();", SpaceFuncDef);
14747   verifyFormat("void f (int a, T b) {}", SpaceFuncDef);
14748   verifyFormat("A::A() : a(1) {}", SpaceFuncDef);
14749   verifyFormat("void f() __attribute__((asdf));", SpaceFuncDef);
14750   verifyFormat("#define A(x) x", SpaceFuncDef);
14751   verifyFormat("#define A (x) x", SpaceFuncDef);
14752   verifyFormat("#if defined(x)\n"
14753                "#endif",
14754                SpaceFuncDef);
14755   verifyFormat("auto i = std::make_unique<int>(5);", SpaceFuncDef);
14756   verifyFormat("size_t x = sizeof(x);", SpaceFuncDef);
14757   verifyFormat("auto f(int x) -> decltype(x);", SpaceFuncDef);
14758   verifyFormat("auto f(int x) -> typeof(x);", SpaceFuncDef);
14759   verifyFormat("auto f(int x) -> _Atomic(x);", SpaceFuncDef);
14760   verifyFormat("auto f(int x) -> __underlying_type(x);", SpaceFuncDef);
14761   verifyFormat("int f(T x) noexcept(x.create());", SpaceFuncDef);
14762   verifyFormat("alignas(128) char a[128];", SpaceFuncDef);
14763   verifyFormat("size_t x = alignof(MyType);", SpaceFuncDef);
14764   verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");",
14765                SpaceFuncDef);
14766   verifyFormat("int f() throw(Deprecated);", SpaceFuncDef);
14767   verifyFormat("typedef void (*cb)(int);", SpaceFuncDef);
14768   verifyFormat("T A::operator()();", SpaceFuncDef);
14769   verifyFormat("X A::operator++(T);", SpaceFuncDef);
14770   // verifyFormat("T A::operator() () {}", SpaceFuncDef);
14771   verifyFormat("auto lambda = [] () { return 0; };", SpaceFuncDef);
14772   verifyFormat("int x = int(y);", SpaceFuncDef);
14773   verifyFormat("M(std::size_t R, std::size_t C) : C(C), data(R) {}",
14774                SpaceFuncDef);
14775 
14776   FormatStyle SpaceIfMacros = getLLVMStyle();
14777   SpaceIfMacros.IfMacros.clear();
14778   SpaceIfMacros.IfMacros.push_back("MYIF");
14779   SpaceIfMacros.SpaceBeforeParens = FormatStyle::SBPO_Custom;
14780   SpaceIfMacros.SpaceBeforeParensOptions.AfterIfMacros = true;
14781   verifyFormat("MYIF (a)\n  return;", SpaceIfMacros);
14782   verifyFormat("MYIF (a)\n  return;\nelse MYIF (b)\n  return;", SpaceIfMacros);
14783   verifyFormat("MYIF (a)\n  return;\nelse\n  return;", SpaceIfMacros);
14784 
14785   FormatStyle SpaceForeachMacros = getLLVMStyle();
14786   EXPECT_EQ(SpaceForeachMacros.AllowShortBlocksOnASingleLine,
14787             FormatStyle::SBS_Never);
14788   EXPECT_EQ(SpaceForeachMacros.AllowShortLoopsOnASingleLine, false);
14789   SpaceForeachMacros.SpaceBeforeParens = FormatStyle::SBPO_Custom;
14790   SpaceForeachMacros.SpaceBeforeParensOptions.AfterForeachMacros = true;
14791   verifyFormat("for (;;) {\n"
14792                "}",
14793                SpaceForeachMacros);
14794   verifyFormat("foreach (Item *item, itemlist) {\n"
14795                "}",
14796                SpaceForeachMacros);
14797   verifyFormat("Q_FOREACH (Item *item, itemlist) {\n"
14798                "}",
14799                SpaceForeachMacros);
14800   verifyFormat("BOOST_FOREACH (Item *item, itemlist) {\n"
14801                "}",
14802                SpaceForeachMacros);
14803   verifyFormat("UNKNOWN_FOREACH(Item *item, itemlist) {}", SpaceForeachMacros);
14804 
14805   FormatStyle SomeSpace2 = getLLVMStyle();
14806   SomeSpace2.SpaceBeforeParens = FormatStyle::SBPO_Custom;
14807   SomeSpace2.SpaceBeforeParensOptions.BeforeNonEmptyParentheses = true;
14808   verifyFormat("[]() -> float {}", SomeSpace2);
14809   verifyFormat("[] (auto foo) {}", SomeSpace2);
14810   verifyFormat("[foo]() -> int {}", SomeSpace2);
14811   verifyFormat("int f();", SomeSpace2);
14812   verifyFormat("void f (int a, T b) {\n"
14813                "  while (true)\n"
14814                "    continue;\n"
14815                "}",
14816                SomeSpace2);
14817   verifyFormat("if (true)\n"
14818                "  f();\n"
14819                "else if (true)\n"
14820                "  f();",
14821                SomeSpace2);
14822   verifyFormat("do {\n"
14823                "  do_something();\n"
14824                "} while (something());",
14825                SomeSpace2);
14826   verifyFormat("switch (x) {\n"
14827                "default:\n"
14828                "  break;\n"
14829                "}",
14830                SomeSpace2);
14831   verifyFormat("A::A() : a (1) {}", SomeSpace2);
14832   verifyFormat("void f() __attribute__ ((asdf));", SomeSpace2);
14833   verifyFormat("*(&a + 1);\n"
14834                "&((&a)[1]);\n"
14835                "a[(b + c) * d];\n"
14836                "(((a + 1) * 2) + 3) * 4;",
14837                SomeSpace2);
14838   verifyFormat("#define A(x) x", SomeSpace2);
14839   verifyFormat("#define A (x) x", SomeSpace2);
14840   verifyFormat("#if defined(x)\n"
14841                "#endif",
14842                SomeSpace2);
14843   verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace2);
14844   verifyFormat("size_t x = sizeof (x);", SomeSpace2);
14845   verifyFormat("auto f (int x) -> decltype (x);", SomeSpace2);
14846   verifyFormat("auto f (int x) -> typeof (x);", SomeSpace2);
14847   verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace2);
14848   verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace2);
14849   verifyFormat("int f (T x) noexcept (x.create());", SomeSpace2);
14850   verifyFormat("alignas (128) char a[128];", SomeSpace2);
14851   verifyFormat("size_t x = alignof (MyType);", SomeSpace2);
14852   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");",
14853                SomeSpace2);
14854   verifyFormat("int f() throw (Deprecated);", SomeSpace2);
14855   verifyFormat("typedef void (*cb) (int);", SomeSpace2);
14856   verifyFormat("T A::operator()();", SomeSpace2);
14857   // verifyFormat("X A::operator++ (T);", SomeSpace2);
14858   verifyFormat("int x = int (y);", SomeSpace2);
14859   verifyFormat("auto lambda = []() { return 0; };", SomeSpace2);
14860 
14861   FormatStyle SpaceAfterOverloadedOperator = getLLVMStyle();
14862   SpaceAfterOverloadedOperator.SpaceBeforeParens = FormatStyle::SBPO_Custom;
14863   SpaceAfterOverloadedOperator.SpaceBeforeParensOptions
14864       .AfterOverloadedOperator = true;
14865 
14866   verifyFormat("auto operator++ () -> int;", SpaceAfterOverloadedOperator);
14867   verifyFormat("X A::operator++ ();", SpaceAfterOverloadedOperator);
14868   verifyFormat("some_object.operator++ ();", SpaceAfterOverloadedOperator);
14869   verifyFormat("auto func() -> int;", SpaceAfterOverloadedOperator);
14870 
14871   SpaceAfterOverloadedOperator.SpaceBeforeParensOptions
14872       .AfterOverloadedOperator = false;
14873 
14874   verifyFormat("auto operator++() -> int;", SpaceAfterOverloadedOperator);
14875   verifyFormat("X A::operator++();", SpaceAfterOverloadedOperator);
14876   verifyFormat("some_object.operator++();", SpaceAfterOverloadedOperator);
14877   verifyFormat("auto func() -> int;", SpaceAfterOverloadedOperator);
14878 }
14879 
14880 TEST_F(FormatTest, SpaceAfterLogicalNot) {
14881   FormatStyle Spaces = getLLVMStyle();
14882   Spaces.SpaceAfterLogicalNot = true;
14883 
14884   verifyFormat("bool x = ! y", Spaces);
14885   verifyFormat("if (! isFailure())", Spaces);
14886   verifyFormat("if (! (a && b))", Spaces);
14887   verifyFormat("\"Error!\"", Spaces);
14888   verifyFormat("! ! x", Spaces);
14889 }
14890 
14891 TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
14892   FormatStyle Spaces = getLLVMStyle();
14893 
14894   Spaces.SpacesInParentheses = true;
14895   verifyFormat("do_something( ::globalVar );", Spaces);
14896   verifyFormat("call( x, y, z );", Spaces);
14897   verifyFormat("call();", Spaces);
14898   verifyFormat("std::function<void( int, int )> callback;", Spaces);
14899   verifyFormat("void inFunction() { std::function<void( int, int )> fct; }",
14900                Spaces);
14901   verifyFormat("while ( (bool)1 )\n"
14902                "  continue;",
14903                Spaces);
14904   verifyFormat("for ( ;; )\n"
14905                "  continue;",
14906                Spaces);
14907   verifyFormat("if ( true )\n"
14908                "  f();\n"
14909                "else if ( true )\n"
14910                "  f();",
14911                Spaces);
14912   verifyFormat("do {\n"
14913                "  do_something( (int)i );\n"
14914                "} while ( something() );",
14915                Spaces);
14916   verifyFormat("switch ( x ) {\n"
14917                "default:\n"
14918                "  break;\n"
14919                "}",
14920                Spaces);
14921 
14922   Spaces.SpacesInParentheses = false;
14923   Spaces.SpacesInCStyleCastParentheses = true;
14924   verifyFormat("Type *A = ( Type * )P;", Spaces);
14925   verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
14926   verifyFormat("x = ( int32 )y;", Spaces);
14927   verifyFormat("int a = ( int )(2.0f);", Spaces);
14928   verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
14929   verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
14930   verifyFormat("#define x (( int )-1)", Spaces);
14931 
14932   // Run the first set of tests again with:
14933   Spaces.SpacesInParentheses = false;
14934   Spaces.SpaceInEmptyParentheses = true;
14935   Spaces.SpacesInCStyleCastParentheses = true;
14936   verifyFormat("call(x, y, z);", Spaces);
14937   verifyFormat("call( );", Spaces);
14938   verifyFormat("std::function<void(int, int)> callback;", Spaces);
14939   verifyFormat("while (( bool )1)\n"
14940                "  continue;",
14941                Spaces);
14942   verifyFormat("for (;;)\n"
14943                "  continue;",
14944                Spaces);
14945   verifyFormat("if (true)\n"
14946                "  f( );\n"
14947                "else if (true)\n"
14948                "  f( );",
14949                Spaces);
14950   verifyFormat("do {\n"
14951                "  do_something(( int )i);\n"
14952                "} while (something( ));",
14953                Spaces);
14954   verifyFormat("switch (x) {\n"
14955                "default:\n"
14956                "  break;\n"
14957                "}",
14958                Spaces);
14959 
14960   // Run the first set of tests again with:
14961   Spaces.SpaceAfterCStyleCast = true;
14962   verifyFormat("call(x, y, z);", Spaces);
14963   verifyFormat("call( );", Spaces);
14964   verifyFormat("std::function<void(int, int)> callback;", Spaces);
14965   verifyFormat("while (( bool ) 1)\n"
14966                "  continue;",
14967                Spaces);
14968   verifyFormat("for (;;)\n"
14969                "  continue;",
14970                Spaces);
14971   verifyFormat("if (true)\n"
14972                "  f( );\n"
14973                "else if (true)\n"
14974                "  f( );",
14975                Spaces);
14976   verifyFormat("do {\n"
14977                "  do_something(( int ) i);\n"
14978                "} while (something( ));",
14979                Spaces);
14980   verifyFormat("switch (x) {\n"
14981                "default:\n"
14982                "  break;\n"
14983                "}",
14984                Spaces);
14985   verifyFormat("#define CONF_BOOL(x) ( bool * ) ( void * ) (x)", Spaces);
14986   verifyFormat("#define CONF_BOOL(x) ( bool * ) (x)", Spaces);
14987   verifyFormat("#define CONF_BOOL(x) ( bool ) (x)", Spaces);
14988   verifyFormat("bool *y = ( bool * ) ( void * ) (x);", Spaces);
14989   verifyFormat("bool *y = ( bool * ) (x);", Spaces);
14990 
14991   // Run subset of tests again with:
14992   Spaces.SpacesInCStyleCastParentheses = false;
14993   Spaces.SpaceAfterCStyleCast = true;
14994   verifyFormat("while ((bool) 1)\n"
14995                "  continue;",
14996                Spaces);
14997   verifyFormat("do {\n"
14998                "  do_something((int) i);\n"
14999                "} while (something( ));",
15000                Spaces);
15001 
15002   verifyFormat("size_t idx = (size_t) (ptr - ((char *) file));", Spaces);
15003   verifyFormat("size_t idx = (size_t) a;", Spaces);
15004   verifyFormat("size_t idx = (size_t) (a - 1);", Spaces);
15005   verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces);
15006   verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces);
15007   verifyFormat("size_t idx = (*foo)(a - 1);", Spaces);
15008   verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces);
15009   verifyFormat("#define CONF_BOOL(x) (bool *) (void *) (x)", Spaces);
15010   verifyFormat("#define CONF_BOOL(x) (bool *) (void *) (int) (x)", Spaces);
15011   verifyFormat("bool *y = (bool *) (void *) (x);", Spaces);
15012   verifyFormat("bool *y = (bool *) (void *) (int) (x);", Spaces);
15013   verifyFormat("bool *y = (bool *) (void *) (int) foo(x);", Spaces);
15014   Spaces.ColumnLimit = 80;
15015   Spaces.IndentWidth = 4;
15016   Spaces.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
15017   verifyFormat("void foo( ) {\n"
15018                "    size_t foo = (*(function))(\n"
15019                "        Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, "
15020                "BarrrrrrrrrrrrLong,\n"
15021                "        FoooooooooLooooong);\n"
15022                "}",
15023                Spaces);
15024   Spaces.SpaceAfterCStyleCast = false;
15025   verifyFormat("size_t idx = (size_t)(ptr - ((char *)file));", Spaces);
15026   verifyFormat("size_t idx = (size_t)a;", Spaces);
15027   verifyFormat("size_t idx = (size_t)(a - 1);", Spaces);
15028   verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces);
15029   verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces);
15030   verifyFormat("size_t idx = (*foo)(a - 1);", Spaces);
15031   verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces);
15032 
15033   verifyFormat("void foo( ) {\n"
15034                "    size_t foo = (*(function))(\n"
15035                "        Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, "
15036                "BarrrrrrrrrrrrLong,\n"
15037                "        FoooooooooLooooong);\n"
15038                "}",
15039                Spaces);
15040 }
15041 
15042 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
15043   verifyFormat("int a[5];");
15044   verifyFormat("a[3] += 42;");
15045 
15046   FormatStyle Spaces = getLLVMStyle();
15047   Spaces.SpacesInSquareBrackets = true;
15048   // Not lambdas.
15049   verifyFormat("int a[ 5 ];", Spaces);
15050   verifyFormat("a[ 3 ] += 42;", Spaces);
15051   verifyFormat("constexpr char hello[]{\"hello\"};", Spaces);
15052   verifyFormat("double &operator[](int i) { return 0; }\n"
15053                "int i;",
15054                Spaces);
15055   verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces);
15056   verifyFormat("int i = a[ a ][ a ]->f();", Spaces);
15057   verifyFormat("int i = (*b)[ a ]->f();", Spaces);
15058   // Lambdas.
15059   verifyFormat("int c = []() -> int { return 2; }();\n", Spaces);
15060   verifyFormat("return [ i, args... ] {};", Spaces);
15061   verifyFormat("int foo = [ &bar ]() {};", Spaces);
15062   verifyFormat("int foo = [ = ]() {};", Spaces);
15063   verifyFormat("int foo = [ & ]() {};", Spaces);
15064   verifyFormat("int foo = [ =, &bar ]() {};", Spaces);
15065   verifyFormat("int foo = [ &bar, = ]() {};", Spaces);
15066 }
15067 
15068 TEST_F(FormatTest, ConfigurableSpaceBeforeBrackets) {
15069   FormatStyle NoSpaceStyle = getLLVMStyle();
15070   verifyFormat("int a[5];", NoSpaceStyle);
15071   verifyFormat("a[3] += 42;", NoSpaceStyle);
15072 
15073   verifyFormat("int a[1];", NoSpaceStyle);
15074   verifyFormat("int 1 [a];", NoSpaceStyle);
15075   verifyFormat("int a[1][2];", NoSpaceStyle);
15076   verifyFormat("a[7] = 5;", NoSpaceStyle);
15077   verifyFormat("int a = (f())[23];", NoSpaceStyle);
15078   verifyFormat("f([] {})", NoSpaceStyle);
15079 
15080   FormatStyle Space = getLLVMStyle();
15081   Space.SpaceBeforeSquareBrackets = true;
15082   verifyFormat("int c = []() -> int { return 2; }();\n", Space);
15083   verifyFormat("return [i, args...] {};", Space);
15084 
15085   verifyFormat("int a [5];", Space);
15086   verifyFormat("a [3] += 42;", Space);
15087   verifyFormat("constexpr char hello []{\"hello\"};", Space);
15088   verifyFormat("double &operator[](int i) { return 0; }\n"
15089                "int i;",
15090                Space);
15091   verifyFormat("std::unique_ptr<int []> foo() {}", Space);
15092   verifyFormat("int i = a [a][a]->f();", Space);
15093   verifyFormat("int i = (*b) [a]->f();", Space);
15094 
15095   verifyFormat("int a [1];", Space);
15096   verifyFormat("int 1 [a];", Space);
15097   verifyFormat("int a [1][2];", Space);
15098   verifyFormat("a [7] = 5;", Space);
15099   verifyFormat("int a = (f()) [23];", Space);
15100   verifyFormat("f([] {})", Space);
15101 }
15102 
15103 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
15104   verifyFormat("int a = 5;");
15105   verifyFormat("a += 42;");
15106   verifyFormat("a or_eq 8;");
15107 
15108   FormatStyle Spaces = getLLVMStyle();
15109   Spaces.SpaceBeforeAssignmentOperators = false;
15110   verifyFormat("int a= 5;", Spaces);
15111   verifyFormat("a+= 42;", Spaces);
15112   verifyFormat("a or_eq 8;", Spaces);
15113 }
15114 
15115 TEST_F(FormatTest, ConfigurableSpaceBeforeColon) {
15116   verifyFormat("class Foo : public Bar {};");
15117   verifyFormat("Foo::Foo() : foo(1) {}");
15118   verifyFormat("for (auto a : b) {\n}");
15119   verifyFormat("int x = a ? b : c;");
15120   verifyFormat("{\n"
15121                "label0:\n"
15122                "  int x = 0;\n"
15123                "}");
15124   verifyFormat("switch (x) {\n"
15125                "case 1:\n"
15126                "default:\n"
15127                "}");
15128   verifyFormat("switch (allBraces) {\n"
15129                "case 1: {\n"
15130                "  break;\n"
15131                "}\n"
15132                "case 2: {\n"
15133                "  [[fallthrough]];\n"
15134                "}\n"
15135                "default: {\n"
15136                "  break;\n"
15137                "}\n"
15138                "}");
15139 
15140   FormatStyle CtorInitializerStyle = getLLVMStyleWithColumns(30);
15141   CtorInitializerStyle.SpaceBeforeCtorInitializerColon = false;
15142   verifyFormat("class Foo : public Bar {};", CtorInitializerStyle);
15143   verifyFormat("Foo::Foo(): foo(1) {}", CtorInitializerStyle);
15144   verifyFormat("for (auto a : b) {\n}", CtorInitializerStyle);
15145   verifyFormat("int x = a ? b : c;", CtorInitializerStyle);
15146   verifyFormat("{\n"
15147                "label1:\n"
15148                "  int x = 0;\n"
15149                "}",
15150                CtorInitializerStyle);
15151   verifyFormat("switch (x) {\n"
15152                "case 1:\n"
15153                "default:\n"
15154                "}",
15155                CtorInitializerStyle);
15156   verifyFormat("switch (allBraces) {\n"
15157                "case 1: {\n"
15158                "  break;\n"
15159                "}\n"
15160                "case 2: {\n"
15161                "  [[fallthrough]];\n"
15162                "}\n"
15163                "default: {\n"
15164                "  break;\n"
15165                "}\n"
15166                "}",
15167                CtorInitializerStyle);
15168   CtorInitializerStyle.BreakConstructorInitializers =
15169       FormatStyle::BCIS_AfterColon;
15170   verifyFormat("Fooooooooooo::Fooooooooooo():\n"
15171                "    aaaaaaaaaaaaaaaa(1),\n"
15172                "    bbbbbbbbbbbbbbbb(2) {}",
15173                CtorInitializerStyle);
15174   CtorInitializerStyle.BreakConstructorInitializers =
15175       FormatStyle::BCIS_BeforeComma;
15176   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
15177                "    : aaaaaaaaaaaaaaaa(1)\n"
15178                "    , bbbbbbbbbbbbbbbb(2) {}",
15179                CtorInitializerStyle);
15180   CtorInitializerStyle.BreakConstructorInitializers =
15181       FormatStyle::BCIS_BeforeColon;
15182   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
15183                "    : aaaaaaaaaaaaaaaa(1),\n"
15184                "      bbbbbbbbbbbbbbbb(2) {}",
15185                CtorInitializerStyle);
15186   CtorInitializerStyle.ConstructorInitializerIndentWidth = 0;
15187   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
15188                ": aaaaaaaaaaaaaaaa(1),\n"
15189                "  bbbbbbbbbbbbbbbb(2) {}",
15190                CtorInitializerStyle);
15191 
15192   FormatStyle InheritanceStyle = getLLVMStyleWithColumns(30);
15193   InheritanceStyle.SpaceBeforeInheritanceColon = false;
15194   verifyFormat("class Foo: public Bar {};", InheritanceStyle);
15195   verifyFormat("Foo::Foo() : foo(1) {}", InheritanceStyle);
15196   verifyFormat("for (auto a : b) {\n}", InheritanceStyle);
15197   verifyFormat("int x = a ? b : c;", InheritanceStyle);
15198   verifyFormat("{\n"
15199                "label2:\n"
15200                "  int x = 0;\n"
15201                "}",
15202                InheritanceStyle);
15203   verifyFormat("switch (x) {\n"
15204                "case 1:\n"
15205                "default:\n"
15206                "}",
15207                InheritanceStyle);
15208   verifyFormat("switch (allBraces) {\n"
15209                "case 1: {\n"
15210                "  break;\n"
15211                "}\n"
15212                "case 2: {\n"
15213                "  [[fallthrough]];\n"
15214                "}\n"
15215                "default: {\n"
15216                "  break;\n"
15217                "}\n"
15218                "}",
15219                InheritanceStyle);
15220   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterComma;
15221   verifyFormat("class Foooooooooooooooooooooo\n"
15222                "    : public aaaaaaaaaaaaaaaaaa,\n"
15223                "      public bbbbbbbbbbbbbbbbbb {\n"
15224                "}",
15225                InheritanceStyle);
15226   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterColon;
15227   verifyFormat("class Foooooooooooooooooooooo:\n"
15228                "    public aaaaaaaaaaaaaaaaaa,\n"
15229                "    public bbbbbbbbbbbbbbbbbb {\n"
15230                "}",
15231                InheritanceStyle);
15232   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeComma;
15233   verifyFormat("class Foooooooooooooooooooooo\n"
15234                "    : public aaaaaaaaaaaaaaaaaa\n"
15235                "    , public bbbbbbbbbbbbbbbbbb {\n"
15236                "}",
15237                InheritanceStyle);
15238   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
15239   verifyFormat("class Foooooooooooooooooooooo\n"
15240                "    : public aaaaaaaaaaaaaaaaaa,\n"
15241                "      public bbbbbbbbbbbbbbbbbb {\n"
15242                "}",
15243                InheritanceStyle);
15244   InheritanceStyle.ConstructorInitializerIndentWidth = 0;
15245   verifyFormat("class Foooooooooooooooooooooo\n"
15246                ": public aaaaaaaaaaaaaaaaaa,\n"
15247                "  public bbbbbbbbbbbbbbbbbb {}",
15248                InheritanceStyle);
15249 
15250   FormatStyle ForLoopStyle = getLLVMStyle();
15251   ForLoopStyle.SpaceBeforeRangeBasedForLoopColon = false;
15252   verifyFormat("class Foo : public Bar {};", ForLoopStyle);
15253   verifyFormat("Foo::Foo() : foo(1) {}", ForLoopStyle);
15254   verifyFormat("for (auto a: b) {\n}", ForLoopStyle);
15255   verifyFormat("int x = a ? b : c;", ForLoopStyle);
15256   verifyFormat("{\n"
15257                "label2:\n"
15258                "  int x = 0;\n"
15259                "}",
15260                ForLoopStyle);
15261   verifyFormat("switch (x) {\n"
15262                "case 1:\n"
15263                "default:\n"
15264                "}",
15265                ForLoopStyle);
15266   verifyFormat("switch (allBraces) {\n"
15267                "case 1: {\n"
15268                "  break;\n"
15269                "}\n"
15270                "case 2: {\n"
15271                "  [[fallthrough]];\n"
15272                "}\n"
15273                "default: {\n"
15274                "  break;\n"
15275                "}\n"
15276                "}",
15277                ForLoopStyle);
15278 
15279   FormatStyle CaseStyle = getLLVMStyle();
15280   CaseStyle.SpaceBeforeCaseColon = true;
15281   verifyFormat("class Foo : public Bar {};", CaseStyle);
15282   verifyFormat("Foo::Foo() : foo(1) {}", CaseStyle);
15283   verifyFormat("for (auto a : b) {\n}", CaseStyle);
15284   verifyFormat("int x = a ? b : c;", CaseStyle);
15285   verifyFormat("switch (x) {\n"
15286                "case 1 :\n"
15287                "default :\n"
15288                "}",
15289                CaseStyle);
15290   verifyFormat("switch (allBraces) {\n"
15291                "case 1 : {\n"
15292                "  break;\n"
15293                "}\n"
15294                "case 2 : {\n"
15295                "  [[fallthrough]];\n"
15296                "}\n"
15297                "default : {\n"
15298                "  break;\n"
15299                "}\n"
15300                "}",
15301                CaseStyle);
15302 
15303   FormatStyle NoSpaceStyle = getLLVMStyle();
15304   EXPECT_EQ(NoSpaceStyle.SpaceBeforeCaseColon, false);
15305   NoSpaceStyle.SpaceBeforeCtorInitializerColon = false;
15306   NoSpaceStyle.SpaceBeforeInheritanceColon = false;
15307   NoSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false;
15308   verifyFormat("class Foo: public Bar {};", NoSpaceStyle);
15309   verifyFormat("Foo::Foo(): foo(1) {}", NoSpaceStyle);
15310   verifyFormat("for (auto a: b) {\n}", NoSpaceStyle);
15311   verifyFormat("int x = a ? b : c;", NoSpaceStyle);
15312   verifyFormat("{\n"
15313                "label3:\n"
15314                "  int x = 0;\n"
15315                "}",
15316                NoSpaceStyle);
15317   verifyFormat("switch (x) {\n"
15318                "case 1:\n"
15319                "default:\n"
15320                "}",
15321                NoSpaceStyle);
15322   verifyFormat("switch (allBraces) {\n"
15323                "case 1: {\n"
15324                "  break;\n"
15325                "}\n"
15326                "case 2: {\n"
15327                "  [[fallthrough]];\n"
15328                "}\n"
15329                "default: {\n"
15330                "  break;\n"
15331                "}\n"
15332                "}",
15333                NoSpaceStyle);
15334 
15335   FormatStyle InvertedSpaceStyle = getLLVMStyle();
15336   InvertedSpaceStyle.SpaceBeforeCaseColon = true;
15337   InvertedSpaceStyle.SpaceBeforeCtorInitializerColon = false;
15338   InvertedSpaceStyle.SpaceBeforeInheritanceColon = false;
15339   InvertedSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false;
15340   verifyFormat("class Foo: public Bar {};", InvertedSpaceStyle);
15341   verifyFormat("Foo::Foo(): foo(1) {}", InvertedSpaceStyle);
15342   verifyFormat("for (auto a: b) {\n}", InvertedSpaceStyle);
15343   verifyFormat("int x = a ? b : c;", InvertedSpaceStyle);
15344   verifyFormat("{\n"
15345                "label3:\n"
15346                "  int x = 0;\n"
15347                "}",
15348                InvertedSpaceStyle);
15349   verifyFormat("switch (x) {\n"
15350                "case 1 :\n"
15351                "case 2 : {\n"
15352                "  break;\n"
15353                "}\n"
15354                "default :\n"
15355                "  break;\n"
15356                "}",
15357                InvertedSpaceStyle);
15358   verifyFormat("switch (allBraces) {\n"
15359                "case 1 : {\n"
15360                "  break;\n"
15361                "}\n"
15362                "case 2 : {\n"
15363                "  [[fallthrough]];\n"
15364                "}\n"
15365                "default : {\n"
15366                "  break;\n"
15367                "}\n"
15368                "}",
15369                InvertedSpaceStyle);
15370 }
15371 
15372 TEST_F(FormatTest, ConfigurableSpaceAroundPointerQualifiers) {
15373   FormatStyle Style = getLLVMStyle();
15374 
15375   Style.PointerAlignment = FormatStyle::PAS_Left;
15376   Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default;
15377   verifyFormat("void* const* x = NULL;", Style);
15378 
15379 #define verifyQualifierSpaces(Code, Pointers, Qualifiers)                      \
15380   do {                                                                         \
15381     Style.PointerAlignment = FormatStyle::Pointers;                            \
15382     Style.SpaceAroundPointerQualifiers = FormatStyle::Qualifiers;              \
15383     verifyFormat(Code, Style);                                                 \
15384   } while (false)
15385 
15386   verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Default);
15387   verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_Default);
15388   verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Default);
15389 
15390   verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Before);
15391   verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Before);
15392   verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Before);
15393 
15394   verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_After);
15395   verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_After);
15396   verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_After);
15397 
15398   verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_Both);
15399   verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Both);
15400   verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Both);
15401 
15402   verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Default);
15403   verifyQualifierSpaces("Foo::operator void const *();", PAS_Right,
15404                         SAPQ_Default);
15405   verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
15406                         SAPQ_Default);
15407 
15408   verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Before);
15409   verifyQualifierSpaces("Foo::operator void const *();", PAS_Right,
15410                         SAPQ_Before);
15411   verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
15412                         SAPQ_Before);
15413 
15414   verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_After);
15415   verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_After);
15416   verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
15417                         SAPQ_After);
15418 
15419   verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_Both);
15420   verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_Both);
15421   verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, SAPQ_Both);
15422 
15423 #undef verifyQualifierSpaces
15424 
15425   FormatStyle Spaces = getLLVMStyle();
15426   Spaces.AttributeMacros.push_back("qualified");
15427   Spaces.PointerAlignment = FormatStyle::PAS_Right;
15428   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default;
15429   verifyFormat("SomeType *volatile *a = NULL;", Spaces);
15430   verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces);
15431   verifyFormat("std::vector<SomeType *const *> x;", Spaces);
15432   verifyFormat("std::vector<SomeType *qualified *> x;", Spaces);
15433   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
15434   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before;
15435   verifyFormat("SomeType * volatile *a = NULL;", Spaces);
15436   verifyFormat("SomeType * __attribute__((attr)) *a = NULL;", Spaces);
15437   verifyFormat("std::vector<SomeType * const *> x;", Spaces);
15438   verifyFormat("std::vector<SomeType * qualified *> x;", Spaces);
15439   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
15440 
15441   // Check that SAPQ_Before doesn't result in extra spaces for PAS_Left.
15442   Spaces.PointerAlignment = FormatStyle::PAS_Left;
15443   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before;
15444   verifyFormat("SomeType* volatile* a = NULL;", Spaces);
15445   verifyFormat("SomeType* __attribute__((attr))* a = NULL;", Spaces);
15446   verifyFormat("std::vector<SomeType* const*> x;", Spaces);
15447   verifyFormat("std::vector<SomeType* qualified*> x;", Spaces);
15448   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
15449   // However, setting it to SAPQ_After should add spaces after __attribute, etc.
15450   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After;
15451   verifyFormat("SomeType* volatile * a = NULL;", Spaces);
15452   verifyFormat("SomeType* __attribute__((attr)) * a = NULL;", Spaces);
15453   verifyFormat("std::vector<SomeType* const *> x;", Spaces);
15454   verifyFormat("std::vector<SomeType* qualified *> x;", Spaces);
15455   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
15456 
15457   // PAS_Middle should not have any noticeable changes even for SAPQ_Both
15458   Spaces.PointerAlignment = FormatStyle::PAS_Middle;
15459   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After;
15460   verifyFormat("SomeType * volatile * a = NULL;", Spaces);
15461   verifyFormat("SomeType * __attribute__((attr)) * a = NULL;", Spaces);
15462   verifyFormat("std::vector<SomeType * const *> x;", Spaces);
15463   verifyFormat("std::vector<SomeType * qualified *> x;", Spaces);
15464   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
15465 }
15466 
15467 TEST_F(FormatTest, AlignConsecutiveMacros) {
15468   FormatStyle Style = getLLVMStyle();
15469   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
15470   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
15471   Style.AlignConsecutiveMacros = FormatStyle::ACS_None;
15472 
15473   verifyFormat("#define a 3\n"
15474                "#define bbbb 4\n"
15475                "#define ccc (5)",
15476                Style);
15477 
15478   verifyFormat("#define f(x) (x * x)\n"
15479                "#define fff(x, y, z) (x * y + z)\n"
15480                "#define ffff(x, y) (x - y)",
15481                Style);
15482 
15483   verifyFormat("#define foo(x, y) (x + y)\n"
15484                "#define bar (5, 6)(2 + 2)",
15485                Style);
15486 
15487   verifyFormat("#define a 3\n"
15488                "#define bbbb 4\n"
15489                "#define ccc (5)\n"
15490                "#define f(x) (x * x)\n"
15491                "#define fff(x, y, z) (x * y + z)\n"
15492                "#define ffff(x, y) (x - y)",
15493                Style);
15494 
15495   Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
15496   verifyFormat("#define a    3\n"
15497                "#define bbbb 4\n"
15498                "#define ccc  (5)",
15499                Style);
15500 
15501   verifyFormat("#define f(x)         (x * x)\n"
15502                "#define fff(x, y, z) (x * y + z)\n"
15503                "#define ffff(x, y)   (x - y)",
15504                Style);
15505 
15506   verifyFormat("#define foo(x, y) (x + y)\n"
15507                "#define bar       (5, 6)(2 + 2)",
15508                Style);
15509 
15510   verifyFormat("#define a            3\n"
15511                "#define bbbb         4\n"
15512                "#define ccc          (5)\n"
15513                "#define f(x)         (x * x)\n"
15514                "#define fff(x, y, z) (x * y + z)\n"
15515                "#define ffff(x, y)   (x - y)",
15516                Style);
15517 
15518   verifyFormat("#define a         5\n"
15519                "#define foo(x, y) (x + y)\n"
15520                "#define CCC       (6)\n"
15521                "auto lambda = []() {\n"
15522                "  auto  ii = 0;\n"
15523                "  float j  = 0;\n"
15524                "  return 0;\n"
15525                "};\n"
15526                "int   i  = 0;\n"
15527                "float i2 = 0;\n"
15528                "auto  v  = type{\n"
15529                "    i = 1,   //\n"
15530                "    (i = 2), //\n"
15531                "    i = 3    //\n"
15532                "};",
15533                Style);
15534 
15535   Style.AlignConsecutiveMacros = FormatStyle::ACS_None;
15536   Style.ColumnLimit = 20;
15537 
15538   verifyFormat("#define a          \\\n"
15539                "  \"aabbbbbbbbbbbb\"\n"
15540                "#define D          \\\n"
15541                "  \"aabbbbbbbbbbbb\" \\\n"
15542                "  \"ccddeeeeeeeee\"\n"
15543                "#define B          \\\n"
15544                "  \"QQQQQQQQQQQQQ\"  \\\n"
15545                "  \"FFFFFFFFFFFFF\"  \\\n"
15546                "  \"LLLLLLLL\"\n",
15547                Style);
15548 
15549   Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
15550   verifyFormat("#define a          \\\n"
15551                "  \"aabbbbbbbbbbbb\"\n"
15552                "#define D          \\\n"
15553                "  \"aabbbbbbbbbbbb\" \\\n"
15554                "  \"ccddeeeeeeeee\"\n"
15555                "#define B          \\\n"
15556                "  \"QQQQQQQQQQQQQ\"  \\\n"
15557                "  \"FFFFFFFFFFFFF\"  \\\n"
15558                "  \"LLLLLLLL\"\n",
15559                Style);
15560 
15561   // Test across comments
15562   Style.MaxEmptyLinesToKeep = 10;
15563   Style.ReflowComments = false;
15564   Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossComments;
15565   EXPECT_EQ("#define a    3\n"
15566             "// line comment\n"
15567             "#define bbbb 4\n"
15568             "#define ccc  (5)",
15569             format("#define a 3\n"
15570                    "// line comment\n"
15571                    "#define bbbb 4\n"
15572                    "#define ccc (5)",
15573                    Style));
15574 
15575   EXPECT_EQ("#define a    3\n"
15576             "/* block comment */\n"
15577             "#define bbbb 4\n"
15578             "#define ccc  (5)",
15579             format("#define a  3\n"
15580                    "/* block comment */\n"
15581                    "#define bbbb 4\n"
15582                    "#define ccc (5)",
15583                    Style));
15584 
15585   EXPECT_EQ("#define a    3\n"
15586             "/* multi-line *\n"
15587             " * block comment */\n"
15588             "#define bbbb 4\n"
15589             "#define ccc  (5)",
15590             format("#define a 3\n"
15591                    "/* multi-line *\n"
15592                    " * block comment */\n"
15593                    "#define bbbb 4\n"
15594                    "#define ccc (5)",
15595                    Style));
15596 
15597   EXPECT_EQ("#define a    3\n"
15598             "// multi-line line comment\n"
15599             "//\n"
15600             "#define bbbb 4\n"
15601             "#define ccc  (5)",
15602             format("#define a  3\n"
15603                    "// multi-line line comment\n"
15604                    "//\n"
15605                    "#define bbbb 4\n"
15606                    "#define ccc (5)",
15607                    Style));
15608 
15609   EXPECT_EQ("#define a 3\n"
15610             "// empty lines still break.\n"
15611             "\n"
15612             "#define bbbb 4\n"
15613             "#define ccc  (5)",
15614             format("#define a     3\n"
15615                    "// empty lines still break.\n"
15616                    "\n"
15617                    "#define bbbb     4\n"
15618                    "#define ccc  (5)",
15619                    Style));
15620 
15621   // Test across empty lines
15622   Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossEmptyLines;
15623   EXPECT_EQ("#define a    3\n"
15624             "\n"
15625             "#define bbbb 4\n"
15626             "#define ccc  (5)",
15627             format("#define a 3\n"
15628                    "\n"
15629                    "#define bbbb 4\n"
15630                    "#define ccc (5)",
15631                    Style));
15632 
15633   EXPECT_EQ("#define a    3\n"
15634             "\n"
15635             "\n"
15636             "\n"
15637             "#define bbbb 4\n"
15638             "#define ccc  (5)",
15639             format("#define a        3\n"
15640                    "\n"
15641                    "\n"
15642                    "\n"
15643                    "#define bbbb 4\n"
15644                    "#define ccc (5)",
15645                    Style));
15646 
15647   EXPECT_EQ("#define a 3\n"
15648             "// comments should break alignment\n"
15649             "//\n"
15650             "#define bbbb 4\n"
15651             "#define ccc  (5)",
15652             format("#define a        3\n"
15653                    "// comments should break alignment\n"
15654                    "//\n"
15655                    "#define bbbb 4\n"
15656                    "#define ccc (5)",
15657                    Style));
15658 
15659   // Test across empty lines and comments
15660   Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossEmptyLinesAndComments;
15661   verifyFormat("#define a    3\n"
15662                "\n"
15663                "// line comment\n"
15664                "#define bbbb 4\n"
15665                "#define ccc  (5)",
15666                Style);
15667 
15668   EXPECT_EQ("#define a    3\n"
15669             "\n"
15670             "\n"
15671             "/* multi-line *\n"
15672             " * block comment */\n"
15673             "\n"
15674             "\n"
15675             "#define bbbb 4\n"
15676             "#define ccc  (5)",
15677             format("#define a 3\n"
15678                    "\n"
15679                    "\n"
15680                    "/* multi-line *\n"
15681                    " * block comment */\n"
15682                    "\n"
15683                    "\n"
15684                    "#define bbbb 4\n"
15685                    "#define ccc (5)",
15686                    Style));
15687 
15688   EXPECT_EQ("#define a    3\n"
15689             "\n"
15690             "\n"
15691             "/* multi-line *\n"
15692             " * block comment */\n"
15693             "\n"
15694             "\n"
15695             "#define bbbb 4\n"
15696             "#define ccc  (5)",
15697             format("#define a 3\n"
15698                    "\n"
15699                    "\n"
15700                    "/* multi-line *\n"
15701                    " * block comment */\n"
15702                    "\n"
15703                    "\n"
15704                    "#define bbbb 4\n"
15705                    "#define ccc       (5)",
15706                    Style));
15707 }
15708 
15709 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLines) {
15710   FormatStyle Alignment = getLLVMStyle();
15711   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
15712   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_AcrossEmptyLines;
15713 
15714   Alignment.MaxEmptyLinesToKeep = 10;
15715   /* Test alignment across empty lines */
15716   EXPECT_EQ("int a           = 5;\n"
15717             "\n"
15718             "int oneTwoThree = 123;",
15719             format("int a       = 5;\n"
15720                    "\n"
15721                    "int oneTwoThree= 123;",
15722                    Alignment));
15723   EXPECT_EQ("int a           = 5;\n"
15724             "int one         = 1;\n"
15725             "\n"
15726             "int oneTwoThree = 123;",
15727             format("int a = 5;\n"
15728                    "int one = 1;\n"
15729                    "\n"
15730                    "int oneTwoThree = 123;",
15731                    Alignment));
15732   EXPECT_EQ("int a           = 5;\n"
15733             "int one         = 1;\n"
15734             "\n"
15735             "int oneTwoThree = 123;\n"
15736             "int oneTwo      = 12;",
15737             format("int a = 5;\n"
15738                    "int one = 1;\n"
15739                    "\n"
15740                    "int oneTwoThree = 123;\n"
15741                    "int oneTwo = 12;",
15742                    Alignment));
15743 
15744   /* Test across comments */
15745   EXPECT_EQ("int a = 5;\n"
15746             "/* block comment */\n"
15747             "int oneTwoThree = 123;",
15748             format("int a = 5;\n"
15749                    "/* block comment */\n"
15750                    "int oneTwoThree=123;",
15751                    Alignment));
15752 
15753   EXPECT_EQ("int a = 5;\n"
15754             "// line comment\n"
15755             "int oneTwoThree = 123;",
15756             format("int a = 5;\n"
15757                    "// line comment\n"
15758                    "int oneTwoThree=123;",
15759                    Alignment));
15760 
15761   /* Test across comments and newlines */
15762   EXPECT_EQ("int a = 5;\n"
15763             "\n"
15764             "/* block comment */\n"
15765             "int oneTwoThree = 123;",
15766             format("int a = 5;\n"
15767                    "\n"
15768                    "/* block comment */\n"
15769                    "int oneTwoThree=123;",
15770                    Alignment));
15771 
15772   EXPECT_EQ("int a = 5;\n"
15773             "\n"
15774             "// line comment\n"
15775             "int oneTwoThree = 123;",
15776             format("int a = 5;\n"
15777                    "\n"
15778                    "// line comment\n"
15779                    "int oneTwoThree=123;",
15780                    Alignment));
15781 }
15782 
15783 TEST_F(FormatTest, AlignConsecutiveDeclarationsAcrossEmptyLinesAndComments) {
15784   FormatStyle Alignment = getLLVMStyle();
15785   Alignment.AlignConsecutiveDeclarations =
15786       FormatStyle::ACS_AcrossEmptyLinesAndComments;
15787   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
15788 
15789   Alignment.MaxEmptyLinesToKeep = 10;
15790   /* Test alignment across empty lines */
15791   EXPECT_EQ("int         a = 5;\n"
15792             "\n"
15793             "float const oneTwoThree = 123;",
15794             format("int a = 5;\n"
15795                    "\n"
15796                    "float const oneTwoThree = 123;",
15797                    Alignment));
15798   EXPECT_EQ("int         a = 5;\n"
15799             "float const one = 1;\n"
15800             "\n"
15801             "int         oneTwoThree = 123;",
15802             format("int a = 5;\n"
15803                    "float const one = 1;\n"
15804                    "\n"
15805                    "int oneTwoThree = 123;",
15806                    Alignment));
15807 
15808   /* Test across comments */
15809   EXPECT_EQ("float const a = 5;\n"
15810             "/* block comment */\n"
15811             "int         oneTwoThree = 123;",
15812             format("float const a = 5;\n"
15813                    "/* block comment */\n"
15814                    "int oneTwoThree=123;",
15815                    Alignment));
15816 
15817   EXPECT_EQ("float const a = 5;\n"
15818             "// line comment\n"
15819             "int         oneTwoThree = 123;",
15820             format("float const a = 5;\n"
15821                    "// line comment\n"
15822                    "int oneTwoThree=123;",
15823                    Alignment));
15824 
15825   /* Test across comments and newlines */
15826   EXPECT_EQ("float const a = 5;\n"
15827             "\n"
15828             "/* block comment */\n"
15829             "int         oneTwoThree = 123;",
15830             format("float const a = 5;\n"
15831                    "\n"
15832                    "/* block comment */\n"
15833                    "int         oneTwoThree=123;",
15834                    Alignment));
15835 
15836   EXPECT_EQ("float const a = 5;\n"
15837             "\n"
15838             "// line comment\n"
15839             "int         oneTwoThree = 123;",
15840             format("float const a = 5;\n"
15841                    "\n"
15842                    "// line comment\n"
15843                    "int oneTwoThree=123;",
15844                    Alignment));
15845 }
15846 
15847 TEST_F(FormatTest, AlignConsecutiveBitFieldsAcrossEmptyLinesAndComments) {
15848   FormatStyle Alignment = getLLVMStyle();
15849   Alignment.AlignConsecutiveBitFields =
15850       FormatStyle::ACS_AcrossEmptyLinesAndComments;
15851 
15852   Alignment.MaxEmptyLinesToKeep = 10;
15853   /* Test alignment across empty lines */
15854   EXPECT_EQ("int a            : 5;\n"
15855             "\n"
15856             "int longbitfield : 6;",
15857             format("int a : 5;\n"
15858                    "\n"
15859                    "int longbitfield : 6;",
15860                    Alignment));
15861   EXPECT_EQ("int a            : 5;\n"
15862             "int one          : 1;\n"
15863             "\n"
15864             "int longbitfield : 6;",
15865             format("int a : 5;\n"
15866                    "int one : 1;\n"
15867                    "\n"
15868                    "int longbitfield : 6;",
15869                    Alignment));
15870 
15871   /* Test across comments */
15872   EXPECT_EQ("int a            : 5;\n"
15873             "/* block comment */\n"
15874             "int longbitfield : 6;",
15875             format("int a : 5;\n"
15876                    "/* block comment */\n"
15877                    "int longbitfield : 6;",
15878                    Alignment));
15879   EXPECT_EQ("int a            : 5;\n"
15880             "int one          : 1;\n"
15881             "// line comment\n"
15882             "int longbitfield : 6;",
15883             format("int a : 5;\n"
15884                    "int one : 1;\n"
15885                    "// line comment\n"
15886                    "int longbitfield : 6;",
15887                    Alignment));
15888 
15889   /* Test across comments and newlines */
15890   EXPECT_EQ("int a            : 5;\n"
15891             "/* block comment */\n"
15892             "\n"
15893             "int longbitfield : 6;",
15894             format("int a : 5;\n"
15895                    "/* block comment */\n"
15896                    "\n"
15897                    "int longbitfield : 6;",
15898                    Alignment));
15899   EXPECT_EQ("int a            : 5;\n"
15900             "int one          : 1;\n"
15901             "\n"
15902             "// line comment\n"
15903             "\n"
15904             "int longbitfield : 6;",
15905             format("int a : 5;\n"
15906                    "int one : 1;\n"
15907                    "\n"
15908                    "// line comment \n"
15909                    "\n"
15910                    "int longbitfield : 6;",
15911                    Alignment));
15912 }
15913 
15914 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossComments) {
15915   FormatStyle Alignment = getLLVMStyle();
15916   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
15917   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_AcrossComments;
15918 
15919   Alignment.MaxEmptyLinesToKeep = 10;
15920   /* Test alignment across empty lines */
15921   EXPECT_EQ("int a = 5;\n"
15922             "\n"
15923             "int oneTwoThree = 123;",
15924             format("int a       = 5;\n"
15925                    "\n"
15926                    "int oneTwoThree= 123;",
15927                    Alignment));
15928   EXPECT_EQ("int a   = 5;\n"
15929             "int one = 1;\n"
15930             "\n"
15931             "int oneTwoThree = 123;",
15932             format("int a = 5;\n"
15933                    "int one = 1;\n"
15934                    "\n"
15935                    "int oneTwoThree = 123;",
15936                    Alignment));
15937 
15938   /* Test across comments */
15939   EXPECT_EQ("int a           = 5;\n"
15940             "/* block comment */\n"
15941             "int oneTwoThree = 123;",
15942             format("int a = 5;\n"
15943                    "/* block comment */\n"
15944                    "int oneTwoThree=123;",
15945                    Alignment));
15946 
15947   EXPECT_EQ("int a           = 5;\n"
15948             "// line comment\n"
15949             "int oneTwoThree = 123;",
15950             format("int a = 5;\n"
15951                    "// line comment\n"
15952                    "int oneTwoThree=123;",
15953                    Alignment));
15954 
15955   EXPECT_EQ("int a           = 5;\n"
15956             "/*\n"
15957             " * multi-line block comment\n"
15958             " */\n"
15959             "int oneTwoThree = 123;",
15960             format("int a = 5;\n"
15961                    "/*\n"
15962                    " * multi-line block comment\n"
15963                    " */\n"
15964                    "int oneTwoThree=123;",
15965                    Alignment));
15966 
15967   EXPECT_EQ("int a           = 5;\n"
15968             "//\n"
15969             "// multi-line line comment\n"
15970             "//\n"
15971             "int oneTwoThree = 123;",
15972             format("int a = 5;\n"
15973                    "//\n"
15974                    "// multi-line line comment\n"
15975                    "//\n"
15976                    "int oneTwoThree=123;",
15977                    Alignment));
15978 
15979   /* Test across comments and newlines */
15980   EXPECT_EQ("int a = 5;\n"
15981             "\n"
15982             "/* block comment */\n"
15983             "int oneTwoThree = 123;",
15984             format("int a = 5;\n"
15985                    "\n"
15986                    "/* block comment */\n"
15987                    "int oneTwoThree=123;",
15988                    Alignment));
15989 
15990   EXPECT_EQ("int a = 5;\n"
15991             "\n"
15992             "// line comment\n"
15993             "int oneTwoThree = 123;",
15994             format("int a = 5;\n"
15995                    "\n"
15996                    "// line comment\n"
15997                    "int oneTwoThree=123;",
15998                    Alignment));
15999 }
16000 
16001 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLinesAndComments) {
16002   FormatStyle Alignment = getLLVMStyle();
16003   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
16004   Alignment.AlignConsecutiveAssignments =
16005       FormatStyle::ACS_AcrossEmptyLinesAndComments;
16006   verifyFormat("int a           = 5;\n"
16007                "int oneTwoThree = 123;",
16008                Alignment);
16009   verifyFormat("int a           = method();\n"
16010                "int oneTwoThree = 133;",
16011                Alignment);
16012   verifyFormat("a &= 5;\n"
16013                "bcd *= 5;\n"
16014                "ghtyf += 5;\n"
16015                "dvfvdb -= 5;\n"
16016                "a /= 5;\n"
16017                "vdsvsv %= 5;\n"
16018                "sfdbddfbdfbb ^= 5;\n"
16019                "dvsdsv |= 5;\n"
16020                "int dsvvdvsdvvv = 123;",
16021                Alignment);
16022   verifyFormat("int i = 1, j = 10;\n"
16023                "something = 2000;",
16024                Alignment);
16025   verifyFormat("something = 2000;\n"
16026                "int i = 1, j = 10;\n",
16027                Alignment);
16028   verifyFormat("something = 2000;\n"
16029                "another   = 911;\n"
16030                "int i = 1, j = 10;\n"
16031                "oneMore = 1;\n"
16032                "i       = 2;",
16033                Alignment);
16034   verifyFormat("int a   = 5;\n"
16035                "int one = 1;\n"
16036                "method();\n"
16037                "int oneTwoThree = 123;\n"
16038                "int oneTwo      = 12;",
16039                Alignment);
16040   verifyFormat("int oneTwoThree = 123;\n"
16041                "int oneTwo      = 12;\n"
16042                "method();\n",
16043                Alignment);
16044   verifyFormat("int oneTwoThree = 123; // comment\n"
16045                "int oneTwo      = 12;  // comment",
16046                Alignment);
16047 
16048   // Bug 25167
16049   /* Uncomment when fixed
16050     verifyFormat("#if A\n"
16051                  "#else\n"
16052                  "int aaaaaaaa = 12;\n"
16053                  "#endif\n"
16054                  "#if B\n"
16055                  "#else\n"
16056                  "int a = 12;\n"
16057                  "#endif\n",
16058                  Alignment);
16059     verifyFormat("enum foo {\n"
16060                  "#if A\n"
16061                  "#else\n"
16062                  "  aaaaaaaa = 12;\n"
16063                  "#endif\n"
16064                  "#if B\n"
16065                  "#else\n"
16066                  "  a = 12;\n"
16067                  "#endif\n"
16068                  "};\n",
16069                  Alignment);
16070   */
16071 
16072   Alignment.MaxEmptyLinesToKeep = 10;
16073   /* Test alignment across empty lines */
16074   EXPECT_EQ("int a           = 5;\n"
16075             "\n"
16076             "int oneTwoThree = 123;",
16077             format("int a       = 5;\n"
16078                    "\n"
16079                    "int oneTwoThree= 123;",
16080                    Alignment));
16081   EXPECT_EQ("int a           = 5;\n"
16082             "int one         = 1;\n"
16083             "\n"
16084             "int oneTwoThree = 123;",
16085             format("int a = 5;\n"
16086                    "int one = 1;\n"
16087                    "\n"
16088                    "int oneTwoThree = 123;",
16089                    Alignment));
16090   EXPECT_EQ("int a           = 5;\n"
16091             "int one         = 1;\n"
16092             "\n"
16093             "int oneTwoThree = 123;\n"
16094             "int oneTwo      = 12;",
16095             format("int a = 5;\n"
16096                    "int one = 1;\n"
16097                    "\n"
16098                    "int oneTwoThree = 123;\n"
16099                    "int oneTwo = 12;",
16100                    Alignment));
16101 
16102   /* Test across comments */
16103   EXPECT_EQ("int a           = 5;\n"
16104             "/* block comment */\n"
16105             "int oneTwoThree = 123;",
16106             format("int a = 5;\n"
16107                    "/* block comment */\n"
16108                    "int oneTwoThree=123;",
16109                    Alignment));
16110 
16111   EXPECT_EQ("int a           = 5;\n"
16112             "// line comment\n"
16113             "int oneTwoThree = 123;",
16114             format("int a = 5;\n"
16115                    "// line comment\n"
16116                    "int oneTwoThree=123;",
16117                    Alignment));
16118 
16119   /* Test across comments and newlines */
16120   EXPECT_EQ("int a           = 5;\n"
16121             "\n"
16122             "/* block comment */\n"
16123             "int oneTwoThree = 123;",
16124             format("int a = 5;\n"
16125                    "\n"
16126                    "/* block comment */\n"
16127                    "int oneTwoThree=123;",
16128                    Alignment));
16129 
16130   EXPECT_EQ("int a           = 5;\n"
16131             "\n"
16132             "// line comment\n"
16133             "int oneTwoThree = 123;",
16134             format("int a = 5;\n"
16135                    "\n"
16136                    "// line comment\n"
16137                    "int oneTwoThree=123;",
16138                    Alignment));
16139 
16140   EXPECT_EQ("int a           = 5;\n"
16141             "//\n"
16142             "// multi-line line comment\n"
16143             "//\n"
16144             "int oneTwoThree = 123;",
16145             format("int a = 5;\n"
16146                    "//\n"
16147                    "// multi-line line comment\n"
16148                    "//\n"
16149                    "int oneTwoThree=123;",
16150                    Alignment));
16151 
16152   EXPECT_EQ("int a           = 5;\n"
16153             "/*\n"
16154             " *  multi-line block comment\n"
16155             " */\n"
16156             "int oneTwoThree = 123;",
16157             format("int a = 5;\n"
16158                    "/*\n"
16159                    " *  multi-line block comment\n"
16160                    " */\n"
16161                    "int oneTwoThree=123;",
16162                    Alignment));
16163 
16164   EXPECT_EQ("int a           = 5;\n"
16165             "\n"
16166             "/* block comment */\n"
16167             "\n"
16168             "\n"
16169             "\n"
16170             "int oneTwoThree = 123;",
16171             format("int a = 5;\n"
16172                    "\n"
16173                    "/* block comment */\n"
16174                    "\n"
16175                    "\n"
16176                    "\n"
16177                    "int oneTwoThree=123;",
16178                    Alignment));
16179 
16180   EXPECT_EQ("int a           = 5;\n"
16181             "\n"
16182             "// line comment\n"
16183             "\n"
16184             "\n"
16185             "\n"
16186             "int oneTwoThree = 123;",
16187             format("int a = 5;\n"
16188                    "\n"
16189                    "// line comment\n"
16190                    "\n"
16191                    "\n"
16192                    "\n"
16193                    "int oneTwoThree=123;",
16194                    Alignment));
16195 
16196   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
16197   verifyFormat("#define A \\\n"
16198                "  int aaaa       = 12; \\\n"
16199                "  int b          = 23; \\\n"
16200                "  int ccc        = 234; \\\n"
16201                "  int dddddddddd = 2345;",
16202                Alignment);
16203   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
16204   verifyFormat("#define A               \\\n"
16205                "  int aaaa       = 12;  \\\n"
16206                "  int b          = 23;  \\\n"
16207                "  int ccc        = 234; \\\n"
16208                "  int dddddddddd = 2345;",
16209                Alignment);
16210   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
16211   verifyFormat("#define A                                                      "
16212                "                \\\n"
16213                "  int aaaa       = 12;                                         "
16214                "                \\\n"
16215                "  int b          = 23;                                         "
16216                "                \\\n"
16217                "  int ccc        = 234;                                        "
16218                "                \\\n"
16219                "  int dddddddddd = 2345;",
16220                Alignment);
16221   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
16222                "k = 4, int l = 5,\n"
16223                "                  int m = 6) {\n"
16224                "  int j      = 10;\n"
16225                "  otherThing = 1;\n"
16226                "}",
16227                Alignment);
16228   verifyFormat("void SomeFunction(int parameter = 0) {\n"
16229                "  int i   = 1;\n"
16230                "  int j   = 2;\n"
16231                "  int big = 10000;\n"
16232                "}",
16233                Alignment);
16234   verifyFormat("class C {\n"
16235                "public:\n"
16236                "  int i            = 1;\n"
16237                "  virtual void f() = 0;\n"
16238                "};",
16239                Alignment);
16240   verifyFormat("int i = 1;\n"
16241                "if (SomeType t = getSomething()) {\n"
16242                "}\n"
16243                "int j   = 2;\n"
16244                "int big = 10000;",
16245                Alignment);
16246   verifyFormat("int j = 7;\n"
16247                "for (int k = 0; k < N; ++k) {\n"
16248                "}\n"
16249                "int j   = 2;\n"
16250                "int big = 10000;\n"
16251                "}",
16252                Alignment);
16253   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
16254   verifyFormat("int i = 1;\n"
16255                "LooooooooooongType loooooooooooooooooooooongVariable\n"
16256                "    = someLooooooooooooooooongFunction();\n"
16257                "int j = 2;",
16258                Alignment);
16259   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
16260   verifyFormat("int i = 1;\n"
16261                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
16262                "    someLooooooooooooooooongFunction();\n"
16263                "int j = 2;",
16264                Alignment);
16265 
16266   verifyFormat("auto lambda = []() {\n"
16267                "  auto i = 0;\n"
16268                "  return 0;\n"
16269                "};\n"
16270                "int i  = 0;\n"
16271                "auto v = type{\n"
16272                "    i = 1,   //\n"
16273                "    (i = 2), //\n"
16274                "    i = 3    //\n"
16275                "};",
16276                Alignment);
16277 
16278   verifyFormat(
16279       "int i      = 1;\n"
16280       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
16281       "                          loooooooooooooooooooooongParameterB);\n"
16282       "int j      = 2;",
16283       Alignment);
16284 
16285   verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
16286                "          typename B   = very_long_type_name_1,\n"
16287                "          typename T_2 = very_long_type_name_2>\n"
16288                "auto foo() {}\n",
16289                Alignment);
16290   verifyFormat("int a, b = 1;\n"
16291                "int c  = 2;\n"
16292                "int dd = 3;\n",
16293                Alignment);
16294   verifyFormat("int aa       = ((1 > 2) ? 3 : 4);\n"
16295                "float b[1][] = {{3.f}};\n",
16296                Alignment);
16297   verifyFormat("for (int i = 0; i < 1; i++)\n"
16298                "  int x = 1;\n",
16299                Alignment);
16300   verifyFormat("for (i = 0; i < 1; i++)\n"
16301                "  x = 1;\n"
16302                "y = 1;\n",
16303                Alignment);
16304 
16305   Alignment.ReflowComments = true;
16306   Alignment.ColumnLimit = 50;
16307   EXPECT_EQ("int x   = 0;\n"
16308             "int yy  = 1; /// specificlennospace\n"
16309             "int zzz = 2;\n",
16310             format("int x   = 0;\n"
16311                    "int yy  = 1; ///specificlennospace\n"
16312                    "int zzz = 2;\n",
16313                    Alignment));
16314 }
16315 
16316 TEST_F(FormatTest, AlignConsecutiveAssignments) {
16317   FormatStyle Alignment = getLLVMStyle();
16318   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
16319   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
16320   verifyFormat("int a = 5;\n"
16321                "int oneTwoThree = 123;",
16322                Alignment);
16323   verifyFormat("int a = 5;\n"
16324                "int oneTwoThree = 123;",
16325                Alignment);
16326 
16327   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16328   verifyFormat("int a           = 5;\n"
16329                "int oneTwoThree = 123;",
16330                Alignment);
16331   verifyFormat("int a           = method();\n"
16332                "int oneTwoThree = 133;",
16333                Alignment);
16334   verifyFormat("a &= 5;\n"
16335                "bcd *= 5;\n"
16336                "ghtyf += 5;\n"
16337                "dvfvdb -= 5;\n"
16338                "a /= 5;\n"
16339                "vdsvsv %= 5;\n"
16340                "sfdbddfbdfbb ^= 5;\n"
16341                "dvsdsv |= 5;\n"
16342                "int dsvvdvsdvvv = 123;",
16343                Alignment);
16344   verifyFormat("int i = 1, j = 10;\n"
16345                "something = 2000;",
16346                Alignment);
16347   verifyFormat("something = 2000;\n"
16348                "int i = 1, j = 10;\n",
16349                Alignment);
16350   verifyFormat("something = 2000;\n"
16351                "another   = 911;\n"
16352                "int i = 1, j = 10;\n"
16353                "oneMore = 1;\n"
16354                "i       = 2;",
16355                Alignment);
16356   verifyFormat("int a   = 5;\n"
16357                "int one = 1;\n"
16358                "method();\n"
16359                "int oneTwoThree = 123;\n"
16360                "int oneTwo      = 12;",
16361                Alignment);
16362   verifyFormat("int oneTwoThree = 123;\n"
16363                "int oneTwo      = 12;\n"
16364                "method();\n",
16365                Alignment);
16366   verifyFormat("int oneTwoThree = 123; // comment\n"
16367                "int oneTwo      = 12;  // comment",
16368                Alignment);
16369   verifyFormat("int f()         = default;\n"
16370                "int &operator() = default;\n"
16371                "int &operator=() {",
16372                Alignment);
16373   verifyFormat("int f()         = delete;\n"
16374                "int &operator() = delete;\n"
16375                "int &operator=() {",
16376                Alignment);
16377   verifyFormat("int f()         = default; // comment\n"
16378                "int &operator() = default; // comment\n"
16379                "int &operator=() {",
16380                Alignment);
16381   verifyFormat("int f()         = default;\n"
16382                "int &operator() = default;\n"
16383                "int &operator==() {",
16384                Alignment);
16385   verifyFormat("int f()         = default;\n"
16386                "int &operator() = default;\n"
16387                "int &operator<=() {",
16388                Alignment);
16389   verifyFormat("int f()         = default;\n"
16390                "int &operator() = default;\n"
16391                "int &operator!=() {",
16392                Alignment);
16393   verifyFormat("int f()         = default;\n"
16394                "int &operator() = default;\n"
16395                "int &operator=();",
16396                Alignment);
16397   verifyFormat("int f()         = delete;\n"
16398                "int &operator() = delete;\n"
16399                "int &operator=();",
16400                Alignment);
16401   verifyFormat("/* long long padding */ int f() = default;\n"
16402                "int &operator()                 = default;\n"
16403                "int &operator/**/ =();",
16404                Alignment);
16405   // https://llvm.org/PR33697
16406   FormatStyle AlignmentWithPenalty = getLLVMStyle();
16407   AlignmentWithPenalty.AlignConsecutiveAssignments =
16408       FormatStyle::ACS_Consecutive;
16409   AlignmentWithPenalty.PenaltyReturnTypeOnItsOwnLine = 5000;
16410   verifyFormat("class SSSSSSSSSSSSSSSSSSSSSSSSSSSS {\n"
16411                "  void f() = delete;\n"
16412                "  SSSSSSSSSSSSSSSSSSSSSSSSSSSS &operator=(\n"
16413                "      const SSSSSSSSSSSSSSSSSSSSSSSSSSSS &other) = delete;\n"
16414                "};\n",
16415                AlignmentWithPenalty);
16416 
16417   // Bug 25167
16418   /* Uncomment when fixed
16419     verifyFormat("#if A\n"
16420                  "#else\n"
16421                  "int aaaaaaaa = 12;\n"
16422                  "#endif\n"
16423                  "#if B\n"
16424                  "#else\n"
16425                  "int a = 12;\n"
16426                  "#endif\n",
16427                  Alignment);
16428     verifyFormat("enum foo {\n"
16429                  "#if A\n"
16430                  "#else\n"
16431                  "  aaaaaaaa = 12;\n"
16432                  "#endif\n"
16433                  "#if B\n"
16434                  "#else\n"
16435                  "  a = 12;\n"
16436                  "#endif\n"
16437                  "};\n",
16438                  Alignment);
16439   */
16440 
16441   EXPECT_EQ("int a = 5;\n"
16442             "\n"
16443             "int oneTwoThree = 123;",
16444             format("int a       = 5;\n"
16445                    "\n"
16446                    "int oneTwoThree= 123;",
16447                    Alignment));
16448   EXPECT_EQ("int a   = 5;\n"
16449             "int one = 1;\n"
16450             "\n"
16451             "int oneTwoThree = 123;",
16452             format("int a = 5;\n"
16453                    "int one = 1;\n"
16454                    "\n"
16455                    "int oneTwoThree = 123;",
16456                    Alignment));
16457   EXPECT_EQ("int a   = 5;\n"
16458             "int one = 1;\n"
16459             "\n"
16460             "int oneTwoThree = 123;\n"
16461             "int oneTwo      = 12;",
16462             format("int a = 5;\n"
16463                    "int one = 1;\n"
16464                    "\n"
16465                    "int oneTwoThree = 123;\n"
16466                    "int oneTwo = 12;",
16467                    Alignment));
16468   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
16469   verifyFormat("#define A \\\n"
16470                "  int aaaa       = 12; \\\n"
16471                "  int b          = 23; \\\n"
16472                "  int ccc        = 234; \\\n"
16473                "  int dddddddddd = 2345;",
16474                Alignment);
16475   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
16476   verifyFormat("#define A               \\\n"
16477                "  int aaaa       = 12;  \\\n"
16478                "  int b          = 23;  \\\n"
16479                "  int ccc        = 234; \\\n"
16480                "  int dddddddddd = 2345;",
16481                Alignment);
16482   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
16483   verifyFormat("#define A                                                      "
16484                "                \\\n"
16485                "  int aaaa       = 12;                                         "
16486                "                \\\n"
16487                "  int b          = 23;                                         "
16488                "                \\\n"
16489                "  int ccc        = 234;                                        "
16490                "                \\\n"
16491                "  int dddddddddd = 2345;",
16492                Alignment);
16493   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
16494                "k = 4, int l = 5,\n"
16495                "                  int m = 6) {\n"
16496                "  int j      = 10;\n"
16497                "  otherThing = 1;\n"
16498                "}",
16499                Alignment);
16500   verifyFormat("void SomeFunction(int parameter = 0) {\n"
16501                "  int i   = 1;\n"
16502                "  int j   = 2;\n"
16503                "  int big = 10000;\n"
16504                "}",
16505                Alignment);
16506   verifyFormat("class C {\n"
16507                "public:\n"
16508                "  int i            = 1;\n"
16509                "  virtual void f() = 0;\n"
16510                "};",
16511                Alignment);
16512   verifyFormat("int i = 1;\n"
16513                "if (SomeType t = getSomething()) {\n"
16514                "}\n"
16515                "int j   = 2;\n"
16516                "int big = 10000;",
16517                Alignment);
16518   verifyFormat("int j = 7;\n"
16519                "for (int k = 0; k < N; ++k) {\n"
16520                "}\n"
16521                "int j   = 2;\n"
16522                "int big = 10000;\n"
16523                "}",
16524                Alignment);
16525   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
16526   verifyFormat("int i = 1;\n"
16527                "LooooooooooongType loooooooooooooooooooooongVariable\n"
16528                "    = someLooooooooooooooooongFunction();\n"
16529                "int j = 2;",
16530                Alignment);
16531   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
16532   verifyFormat("int i = 1;\n"
16533                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
16534                "    someLooooooooooooooooongFunction();\n"
16535                "int j = 2;",
16536                Alignment);
16537 
16538   verifyFormat("auto lambda = []() {\n"
16539                "  auto i = 0;\n"
16540                "  return 0;\n"
16541                "};\n"
16542                "int i  = 0;\n"
16543                "auto v = type{\n"
16544                "    i = 1,   //\n"
16545                "    (i = 2), //\n"
16546                "    i = 3    //\n"
16547                "};",
16548                Alignment);
16549 
16550   verifyFormat(
16551       "int i      = 1;\n"
16552       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
16553       "                          loooooooooooooooooooooongParameterB);\n"
16554       "int j      = 2;",
16555       Alignment);
16556 
16557   verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
16558                "          typename B   = very_long_type_name_1,\n"
16559                "          typename T_2 = very_long_type_name_2>\n"
16560                "auto foo() {}\n",
16561                Alignment);
16562   verifyFormat("int a, b = 1;\n"
16563                "int c  = 2;\n"
16564                "int dd = 3;\n",
16565                Alignment);
16566   verifyFormat("int aa       = ((1 > 2) ? 3 : 4);\n"
16567                "float b[1][] = {{3.f}};\n",
16568                Alignment);
16569   verifyFormat("for (int i = 0; i < 1; i++)\n"
16570                "  int x = 1;\n",
16571                Alignment);
16572   verifyFormat("for (i = 0; i < 1; i++)\n"
16573                "  x = 1;\n"
16574                "y = 1;\n",
16575                Alignment);
16576 
16577   EXPECT_EQ(Alignment.ReflowComments, true);
16578   Alignment.ColumnLimit = 50;
16579   EXPECT_EQ("int x   = 0;\n"
16580             "int yy  = 1; /// specificlennospace\n"
16581             "int zzz = 2;\n",
16582             format("int x   = 0;\n"
16583                    "int yy  = 1; ///specificlennospace\n"
16584                    "int zzz = 2;\n",
16585                    Alignment));
16586 
16587   verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n"
16588                "auto b                     = [] {\n"
16589                "  f();\n"
16590                "  return;\n"
16591                "};",
16592                Alignment);
16593   verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n"
16594                "auto b                     = g([] {\n"
16595                "  f();\n"
16596                "  return;\n"
16597                "});",
16598                Alignment);
16599   verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n"
16600                "auto b                     = g(param, [] {\n"
16601                "  f();\n"
16602                "  return;\n"
16603                "});",
16604                Alignment);
16605   verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n"
16606                "auto b                     = [] {\n"
16607                "  if (condition) {\n"
16608                "    return;\n"
16609                "  }\n"
16610                "};",
16611                Alignment);
16612 
16613   verifyFormat("auto b = f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
16614                "           ccc ? aaaaa : bbbbb,\n"
16615                "           dddddddddddddddddddddddddd);",
16616                Alignment);
16617   // FIXME: https://llvm.org/PR53497
16618   // verifyFormat("auto aaaaaaaaaaaa = f();\n"
16619   //              "auto b            = f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
16620   //              "    ccc ? aaaaa : bbbbb,\n"
16621   //              "    dddddddddddddddddddddddddd);",
16622   //              Alignment);
16623 }
16624 
16625 TEST_F(FormatTest, AlignConsecutiveBitFields) {
16626   FormatStyle Alignment = getLLVMStyle();
16627   Alignment.AlignConsecutiveBitFields = FormatStyle::ACS_Consecutive;
16628   verifyFormat("int const a     : 5;\n"
16629                "int oneTwoThree : 23;",
16630                Alignment);
16631 
16632   // Initializers are allowed starting with c++2a
16633   verifyFormat("int const a     : 5 = 1;\n"
16634                "int oneTwoThree : 23 = 0;",
16635                Alignment);
16636 
16637   Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
16638   verifyFormat("int const a           : 5;\n"
16639                "int       oneTwoThree : 23;",
16640                Alignment);
16641 
16642   verifyFormat("int const a           : 5;  // comment\n"
16643                "int       oneTwoThree : 23; // comment",
16644                Alignment);
16645 
16646   verifyFormat("int const a           : 5 = 1;\n"
16647                "int       oneTwoThree : 23 = 0;",
16648                Alignment);
16649 
16650   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16651   verifyFormat("int const a           : 5  = 1;\n"
16652                "int       oneTwoThree : 23 = 0;",
16653                Alignment);
16654   verifyFormat("int const a           : 5  = {1};\n"
16655                "int       oneTwoThree : 23 = 0;",
16656                Alignment);
16657 
16658   Alignment.BitFieldColonSpacing = FormatStyle::BFCS_None;
16659   verifyFormat("int const a          :5;\n"
16660                "int       oneTwoThree:23;",
16661                Alignment);
16662 
16663   Alignment.BitFieldColonSpacing = FormatStyle::BFCS_Before;
16664   verifyFormat("int const a           :5;\n"
16665                "int       oneTwoThree :23;",
16666                Alignment);
16667 
16668   Alignment.BitFieldColonSpacing = FormatStyle::BFCS_After;
16669   verifyFormat("int const a          : 5;\n"
16670                "int       oneTwoThree: 23;",
16671                Alignment);
16672 
16673   // Known limitations: ':' is only recognized as a bitfield colon when
16674   // followed by a number.
16675   /*
16676   verifyFormat("int oneTwoThree : SOME_CONSTANT;\n"
16677                "int a           : 5;",
16678                Alignment);
16679   */
16680 }
16681 
16682 TEST_F(FormatTest, AlignConsecutiveDeclarations) {
16683   FormatStyle Alignment = getLLVMStyle();
16684   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
16685   Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_None;
16686   Alignment.PointerAlignment = FormatStyle::PAS_Right;
16687   verifyFormat("float const a = 5;\n"
16688                "int oneTwoThree = 123;",
16689                Alignment);
16690   verifyFormat("int a = 5;\n"
16691                "float const oneTwoThree = 123;",
16692                Alignment);
16693 
16694   Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
16695   verifyFormat("float const a = 5;\n"
16696                "int         oneTwoThree = 123;",
16697                Alignment);
16698   verifyFormat("int         a = method();\n"
16699                "float const oneTwoThree = 133;",
16700                Alignment);
16701   verifyFormat("int i = 1, j = 10;\n"
16702                "something = 2000;",
16703                Alignment);
16704   verifyFormat("something = 2000;\n"
16705                "int i = 1, j = 10;\n",
16706                Alignment);
16707   verifyFormat("float      something = 2000;\n"
16708                "double     another = 911;\n"
16709                "int        i = 1, j = 10;\n"
16710                "const int *oneMore = 1;\n"
16711                "unsigned   i = 2;",
16712                Alignment);
16713   verifyFormat("float a = 5;\n"
16714                "int   one = 1;\n"
16715                "method();\n"
16716                "const double       oneTwoThree = 123;\n"
16717                "const unsigned int oneTwo = 12;",
16718                Alignment);
16719   verifyFormat("int      oneTwoThree{0}; // comment\n"
16720                "unsigned oneTwo;         // comment",
16721                Alignment);
16722   verifyFormat("unsigned int       *a;\n"
16723                "int                *b;\n"
16724                "unsigned int Const *c;\n"
16725                "unsigned int const *d;\n"
16726                "unsigned int Const &e;\n"
16727                "unsigned int const &f;",
16728                Alignment);
16729   verifyFormat("Const unsigned int *c;\n"
16730                "const unsigned int *d;\n"
16731                "Const unsigned int &e;\n"
16732                "const unsigned int &f;\n"
16733                "const unsigned      g;\n"
16734                "Const unsigned      h;",
16735                Alignment);
16736   EXPECT_EQ("float const a = 5;\n"
16737             "\n"
16738             "int oneTwoThree = 123;",
16739             format("float const   a = 5;\n"
16740                    "\n"
16741                    "int           oneTwoThree= 123;",
16742                    Alignment));
16743   EXPECT_EQ("float a = 5;\n"
16744             "int   one = 1;\n"
16745             "\n"
16746             "unsigned oneTwoThree = 123;",
16747             format("float    a = 5;\n"
16748                    "int      one = 1;\n"
16749                    "\n"
16750                    "unsigned oneTwoThree = 123;",
16751                    Alignment));
16752   EXPECT_EQ("float a = 5;\n"
16753             "int   one = 1;\n"
16754             "\n"
16755             "unsigned oneTwoThree = 123;\n"
16756             "int      oneTwo = 12;",
16757             format("float    a = 5;\n"
16758                    "int one = 1;\n"
16759                    "\n"
16760                    "unsigned oneTwoThree = 123;\n"
16761                    "int oneTwo = 12;",
16762                    Alignment));
16763   // Function prototype alignment
16764   verifyFormat("int    a();\n"
16765                "double b();",
16766                Alignment);
16767   verifyFormat("int    a(int x);\n"
16768                "double b();",
16769                Alignment);
16770   unsigned OldColumnLimit = Alignment.ColumnLimit;
16771   // We need to set ColumnLimit to zero, in order to stress nested alignments,
16772   // otherwise the function parameters will be re-flowed onto a single line.
16773   Alignment.ColumnLimit = 0;
16774   EXPECT_EQ("int    a(int   x,\n"
16775             "         float y);\n"
16776             "double b(int    x,\n"
16777             "         double y);",
16778             format("int a(int x,\n"
16779                    " float y);\n"
16780                    "double b(int x,\n"
16781                    " double y);",
16782                    Alignment));
16783   // This ensures that function parameters of function declarations are
16784   // correctly indented when their owning functions are indented.
16785   // The failure case here is for 'double y' to not be indented enough.
16786   EXPECT_EQ("double a(int x);\n"
16787             "int    b(int    y,\n"
16788             "         double z);",
16789             format("double a(int x);\n"
16790                    "int b(int y,\n"
16791                    " double z);",
16792                    Alignment));
16793   // Set ColumnLimit low so that we induce wrapping immediately after
16794   // the function name and opening paren.
16795   Alignment.ColumnLimit = 13;
16796   verifyFormat("int function(\n"
16797                "    int  x,\n"
16798                "    bool y);",
16799                Alignment);
16800   Alignment.ColumnLimit = OldColumnLimit;
16801   // Ensure function pointers don't screw up recursive alignment
16802   verifyFormat("int    a(int x, void (*fp)(int y));\n"
16803                "double b();",
16804                Alignment);
16805   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16806   // Ensure recursive alignment is broken by function braces, so that the
16807   // "a = 1" does not align with subsequent assignments inside the function
16808   // body.
16809   verifyFormat("int func(int a = 1) {\n"
16810                "  int b  = 2;\n"
16811                "  int cc = 3;\n"
16812                "}",
16813                Alignment);
16814   verifyFormat("float      something = 2000;\n"
16815                "double     another   = 911;\n"
16816                "int        i = 1, j = 10;\n"
16817                "const int *oneMore = 1;\n"
16818                "unsigned   i       = 2;",
16819                Alignment);
16820   verifyFormat("int      oneTwoThree = {0}; // comment\n"
16821                "unsigned oneTwo      = 0;   // comment",
16822                Alignment);
16823   // Make sure that scope is correctly tracked, in the absence of braces
16824   verifyFormat("for (int i = 0; i < n; i++)\n"
16825                "  j = i;\n"
16826                "double x = 1;\n",
16827                Alignment);
16828   verifyFormat("if (int i = 0)\n"
16829                "  j = i;\n"
16830                "double x = 1;\n",
16831                Alignment);
16832   // Ensure operator[] and operator() are comprehended
16833   verifyFormat("struct test {\n"
16834                "  long long int foo();\n"
16835                "  int           operator[](int a);\n"
16836                "  double        bar();\n"
16837                "};\n",
16838                Alignment);
16839   verifyFormat("struct test {\n"
16840                "  long long int foo();\n"
16841                "  int           operator()(int a);\n"
16842                "  double        bar();\n"
16843                "};\n",
16844                Alignment);
16845   // http://llvm.org/PR52914
16846   verifyFormat("char *a[]     = {\"a\", // comment\n"
16847                "                 \"bb\"};\n"
16848                "int   bbbbbbb = 0;",
16849                Alignment);
16850 
16851   // PAS_Right
16852   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
16853             "  int const i   = 1;\n"
16854             "  int      *j   = 2;\n"
16855             "  int       big = 10000;\n"
16856             "\n"
16857             "  unsigned oneTwoThree = 123;\n"
16858             "  int      oneTwo      = 12;\n"
16859             "  method();\n"
16860             "  float k  = 2;\n"
16861             "  int   ll = 10000;\n"
16862             "}",
16863             format("void SomeFunction(int parameter= 0) {\n"
16864                    " int const  i= 1;\n"
16865                    "  int *j=2;\n"
16866                    " int big  =  10000;\n"
16867                    "\n"
16868                    "unsigned oneTwoThree  =123;\n"
16869                    "int oneTwo = 12;\n"
16870                    "  method();\n"
16871                    "float k= 2;\n"
16872                    "int ll=10000;\n"
16873                    "}",
16874                    Alignment));
16875   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
16876             "  int const i   = 1;\n"
16877             "  int     **j   = 2, ***k;\n"
16878             "  int      &k   = i;\n"
16879             "  int     &&l   = i + j;\n"
16880             "  int       big = 10000;\n"
16881             "\n"
16882             "  unsigned oneTwoThree = 123;\n"
16883             "  int      oneTwo      = 12;\n"
16884             "  method();\n"
16885             "  float k  = 2;\n"
16886             "  int   ll = 10000;\n"
16887             "}",
16888             format("void SomeFunction(int parameter= 0) {\n"
16889                    " int const  i= 1;\n"
16890                    "  int **j=2,***k;\n"
16891                    "int &k=i;\n"
16892                    "int &&l=i+j;\n"
16893                    " int big  =  10000;\n"
16894                    "\n"
16895                    "unsigned oneTwoThree  =123;\n"
16896                    "int oneTwo = 12;\n"
16897                    "  method();\n"
16898                    "float k= 2;\n"
16899                    "int ll=10000;\n"
16900                    "}",
16901                    Alignment));
16902   // variables are aligned at their name, pointers are at the right most
16903   // position
16904   verifyFormat("int   *a;\n"
16905                "int  **b;\n"
16906                "int ***c;\n"
16907                "int    foobar;\n",
16908                Alignment);
16909 
16910   // PAS_Left
16911   FormatStyle AlignmentLeft = Alignment;
16912   AlignmentLeft.PointerAlignment = FormatStyle::PAS_Left;
16913   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
16914             "  int const i   = 1;\n"
16915             "  int*      j   = 2;\n"
16916             "  int       big = 10000;\n"
16917             "\n"
16918             "  unsigned oneTwoThree = 123;\n"
16919             "  int      oneTwo      = 12;\n"
16920             "  method();\n"
16921             "  float k  = 2;\n"
16922             "  int   ll = 10000;\n"
16923             "}",
16924             format("void SomeFunction(int parameter= 0) {\n"
16925                    " int const  i= 1;\n"
16926                    "  int *j=2;\n"
16927                    " int big  =  10000;\n"
16928                    "\n"
16929                    "unsigned oneTwoThree  =123;\n"
16930                    "int oneTwo = 12;\n"
16931                    "  method();\n"
16932                    "float k= 2;\n"
16933                    "int ll=10000;\n"
16934                    "}",
16935                    AlignmentLeft));
16936   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
16937             "  int const i   = 1;\n"
16938             "  int**     j   = 2;\n"
16939             "  int&      k   = i;\n"
16940             "  int&&     l   = i + j;\n"
16941             "  int       big = 10000;\n"
16942             "\n"
16943             "  unsigned oneTwoThree = 123;\n"
16944             "  int      oneTwo      = 12;\n"
16945             "  method();\n"
16946             "  float k  = 2;\n"
16947             "  int   ll = 10000;\n"
16948             "}",
16949             format("void SomeFunction(int parameter= 0) {\n"
16950                    " int const  i= 1;\n"
16951                    "  int **j=2;\n"
16952                    "int &k=i;\n"
16953                    "int &&l=i+j;\n"
16954                    " int big  =  10000;\n"
16955                    "\n"
16956                    "unsigned oneTwoThree  =123;\n"
16957                    "int oneTwo = 12;\n"
16958                    "  method();\n"
16959                    "float k= 2;\n"
16960                    "int ll=10000;\n"
16961                    "}",
16962                    AlignmentLeft));
16963   // variables are aligned at their name, pointers are at the left most position
16964   verifyFormat("int*   a;\n"
16965                "int**  b;\n"
16966                "int*** c;\n"
16967                "int    foobar;\n",
16968                AlignmentLeft);
16969 
16970   // PAS_Middle
16971   FormatStyle AlignmentMiddle = Alignment;
16972   AlignmentMiddle.PointerAlignment = FormatStyle::PAS_Middle;
16973   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
16974             "  int const i   = 1;\n"
16975             "  int *     j   = 2;\n"
16976             "  int       big = 10000;\n"
16977             "\n"
16978             "  unsigned oneTwoThree = 123;\n"
16979             "  int      oneTwo      = 12;\n"
16980             "  method();\n"
16981             "  float k  = 2;\n"
16982             "  int   ll = 10000;\n"
16983             "}",
16984             format("void SomeFunction(int parameter= 0) {\n"
16985                    " int const  i= 1;\n"
16986                    "  int *j=2;\n"
16987                    " int big  =  10000;\n"
16988                    "\n"
16989                    "unsigned oneTwoThree  =123;\n"
16990                    "int oneTwo = 12;\n"
16991                    "  method();\n"
16992                    "float k= 2;\n"
16993                    "int ll=10000;\n"
16994                    "}",
16995                    AlignmentMiddle));
16996   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
16997             "  int const i   = 1;\n"
16998             "  int **    j   = 2, ***k;\n"
16999             "  int &     k   = i;\n"
17000             "  int &&    l   = i + j;\n"
17001             "  int       big = 10000;\n"
17002             "\n"
17003             "  unsigned oneTwoThree = 123;\n"
17004             "  int      oneTwo      = 12;\n"
17005             "  method();\n"
17006             "  float k  = 2;\n"
17007             "  int   ll = 10000;\n"
17008             "}",
17009             format("void SomeFunction(int parameter= 0) {\n"
17010                    " int const  i= 1;\n"
17011                    "  int **j=2,***k;\n"
17012                    "int &k=i;\n"
17013                    "int &&l=i+j;\n"
17014                    " int big  =  10000;\n"
17015                    "\n"
17016                    "unsigned oneTwoThree  =123;\n"
17017                    "int oneTwo = 12;\n"
17018                    "  method();\n"
17019                    "float k= 2;\n"
17020                    "int ll=10000;\n"
17021                    "}",
17022                    AlignmentMiddle));
17023   // variables are aligned at their name, pointers are in the middle
17024   verifyFormat("int *   a;\n"
17025                "int *   b;\n"
17026                "int *** c;\n"
17027                "int     foobar;\n",
17028                AlignmentMiddle);
17029 
17030   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
17031   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
17032   verifyFormat("#define A \\\n"
17033                "  int       aaaa = 12; \\\n"
17034                "  float     b = 23; \\\n"
17035                "  const int ccc = 234; \\\n"
17036                "  unsigned  dddddddddd = 2345;",
17037                Alignment);
17038   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
17039   verifyFormat("#define A              \\\n"
17040                "  int       aaaa = 12; \\\n"
17041                "  float     b = 23;    \\\n"
17042                "  const int ccc = 234; \\\n"
17043                "  unsigned  dddddddddd = 2345;",
17044                Alignment);
17045   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
17046   Alignment.ColumnLimit = 30;
17047   verifyFormat("#define A                    \\\n"
17048                "  int       aaaa = 12;       \\\n"
17049                "  float     b = 23;          \\\n"
17050                "  const int ccc = 234;       \\\n"
17051                "  int       dddddddddd = 2345;",
17052                Alignment);
17053   Alignment.ColumnLimit = 80;
17054   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
17055                "k = 4, int l = 5,\n"
17056                "                  int m = 6) {\n"
17057                "  const int j = 10;\n"
17058                "  otherThing = 1;\n"
17059                "}",
17060                Alignment);
17061   verifyFormat("void SomeFunction(int parameter = 0) {\n"
17062                "  int const i = 1;\n"
17063                "  int      *j = 2;\n"
17064                "  int       big = 10000;\n"
17065                "}",
17066                Alignment);
17067   verifyFormat("class C {\n"
17068                "public:\n"
17069                "  int          i = 1;\n"
17070                "  virtual void f() = 0;\n"
17071                "};",
17072                Alignment);
17073   verifyFormat("float i = 1;\n"
17074                "if (SomeType t = getSomething()) {\n"
17075                "}\n"
17076                "const unsigned j = 2;\n"
17077                "int            big = 10000;",
17078                Alignment);
17079   verifyFormat("float j = 7;\n"
17080                "for (int k = 0; k < N; ++k) {\n"
17081                "}\n"
17082                "unsigned j = 2;\n"
17083                "int      big = 10000;\n"
17084                "}",
17085                Alignment);
17086   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
17087   verifyFormat("float              i = 1;\n"
17088                "LooooooooooongType loooooooooooooooooooooongVariable\n"
17089                "    = someLooooooooooooooooongFunction();\n"
17090                "int j = 2;",
17091                Alignment);
17092   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
17093   verifyFormat("int                i = 1;\n"
17094                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
17095                "    someLooooooooooooooooongFunction();\n"
17096                "int j = 2;",
17097                Alignment);
17098 
17099   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
17100   verifyFormat("auto lambda = []() {\n"
17101                "  auto  ii = 0;\n"
17102                "  float j  = 0;\n"
17103                "  return 0;\n"
17104                "};\n"
17105                "int   i  = 0;\n"
17106                "float i2 = 0;\n"
17107                "auto  v  = type{\n"
17108                "    i = 1,   //\n"
17109                "    (i = 2), //\n"
17110                "    i = 3    //\n"
17111                "};",
17112                Alignment);
17113   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
17114 
17115   verifyFormat(
17116       "int      i = 1;\n"
17117       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
17118       "                          loooooooooooooooooooooongParameterB);\n"
17119       "int      j = 2;",
17120       Alignment);
17121 
17122   // Test interactions with ColumnLimit and AlignConsecutiveAssignments:
17123   // We expect declarations and assignments to align, as long as it doesn't
17124   // exceed the column limit, starting a new alignment sequence whenever it
17125   // happens.
17126   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
17127   Alignment.ColumnLimit = 30;
17128   verifyFormat("float    ii              = 1;\n"
17129                "unsigned j               = 2;\n"
17130                "int someVerylongVariable = 1;\n"
17131                "AnotherLongType  ll = 123456;\n"
17132                "VeryVeryLongType k  = 2;\n"
17133                "int              myvar = 1;",
17134                Alignment);
17135   Alignment.ColumnLimit = 80;
17136   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
17137 
17138   verifyFormat(
17139       "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n"
17140       "          typename LongType, typename B>\n"
17141       "auto foo() {}\n",
17142       Alignment);
17143   verifyFormat("float a, b = 1;\n"
17144                "int   c = 2;\n"
17145                "int   dd = 3;\n",
17146                Alignment);
17147   verifyFormat("int   aa = ((1 > 2) ? 3 : 4);\n"
17148                "float b[1][] = {{3.f}};\n",
17149                Alignment);
17150   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
17151   verifyFormat("float a, b = 1;\n"
17152                "int   c  = 2;\n"
17153                "int   dd = 3;\n",
17154                Alignment);
17155   verifyFormat("int   aa     = ((1 > 2) ? 3 : 4);\n"
17156                "float b[1][] = {{3.f}};\n",
17157                Alignment);
17158   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
17159 
17160   Alignment.ColumnLimit = 30;
17161   Alignment.BinPackParameters = false;
17162   verifyFormat("void foo(float     a,\n"
17163                "         float     b,\n"
17164                "         int       c,\n"
17165                "         uint32_t *d) {\n"
17166                "  int   *e = 0;\n"
17167                "  float  f = 0;\n"
17168                "  double g = 0;\n"
17169                "}\n"
17170                "void bar(ino_t     a,\n"
17171                "         int       b,\n"
17172                "         uint32_t *c,\n"
17173                "         bool      d) {}\n",
17174                Alignment);
17175   Alignment.BinPackParameters = true;
17176   Alignment.ColumnLimit = 80;
17177 
17178   // Bug 33507
17179   Alignment.PointerAlignment = FormatStyle::PAS_Middle;
17180   verifyFormat(
17181       "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n"
17182       "  static const Version verVs2017;\n"
17183       "  return true;\n"
17184       "});\n",
17185       Alignment);
17186   Alignment.PointerAlignment = FormatStyle::PAS_Right;
17187 
17188   // See llvm.org/PR35641
17189   Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
17190   verifyFormat("int func() { //\n"
17191                "  int      b;\n"
17192                "  unsigned c;\n"
17193                "}",
17194                Alignment);
17195 
17196   // See PR37175
17197   FormatStyle Style = getMozillaStyle();
17198   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
17199   EXPECT_EQ("DECOR1 /**/ int8_t /**/ DECOR2 /**/\n"
17200             "foo(int a);",
17201             format("DECOR1 /**/ int8_t /**/ DECOR2 /**/ foo (int a);", Style));
17202 
17203   Alignment.PointerAlignment = FormatStyle::PAS_Left;
17204   verifyFormat("unsigned int*       a;\n"
17205                "int*                b;\n"
17206                "unsigned int Const* c;\n"
17207                "unsigned int const* d;\n"
17208                "unsigned int Const& e;\n"
17209                "unsigned int const& f;",
17210                Alignment);
17211   verifyFormat("Const unsigned int* c;\n"
17212                "const unsigned int* d;\n"
17213                "Const unsigned int& e;\n"
17214                "const unsigned int& f;\n"
17215                "const unsigned      g;\n"
17216                "Const unsigned      h;",
17217                Alignment);
17218 
17219   Alignment.PointerAlignment = FormatStyle::PAS_Middle;
17220   verifyFormat("unsigned int *       a;\n"
17221                "int *                b;\n"
17222                "unsigned int Const * c;\n"
17223                "unsigned int const * d;\n"
17224                "unsigned int Const & e;\n"
17225                "unsigned int const & f;",
17226                Alignment);
17227   verifyFormat("Const unsigned int * c;\n"
17228                "const unsigned int * d;\n"
17229                "Const unsigned int & e;\n"
17230                "const unsigned int & f;\n"
17231                "const unsigned       g;\n"
17232                "Const unsigned       h;",
17233                Alignment);
17234 }
17235 
17236 TEST_F(FormatTest, AlignWithLineBreaks) {
17237   auto Style = getLLVMStyleWithColumns(120);
17238 
17239   EXPECT_EQ(Style.AlignConsecutiveAssignments, FormatStyle::ACS_None);
17240   EXPECT_EQ(Style.AlignConsecutiveDeclarations, FormatStyle::ACS_None);
17241   verifyFormat("void foo() {\n"
17242                "  int myVar = 5;\n"
17243                "  double x = 3.14;\n"
17244                "  auto str = \"Hello \"\n"
17245                "             \"World\";\n"
17246                "  auto s = \"Hello \"\n"
17247                "           \"Again\";\n"
17248                "}",
17249                Style);
17250 
17251   // clang-format off
17252   verifyFormat("void foo() {\n"
17253                "  const int capacityBefore = Entries.capacity();\n"
17254                "  const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
17255                "                                            std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
17256                "  const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
17257                "                                          std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
17258                "}",
17259                Style);
17260   // clang-format on
17261 
17262   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
17263   verifyFormat("void foo() {\n"
17264                "  int myVar = 5;\n"
17265                "  double x  = 3.14;\n"
17266                "  auto str  = \"Hello \"\n"
17267                "              \"World\";\n"
17268                "  auto s    = \"Hello \"\n"
17269                "              \"Again\";\n"
17270                "}",
17271                Style);
17272 
17273   // clang-format off
17274   verifyFormat("void foo() {\n"
17275                "  const int capacityBefore = Entries.capacity();\n"
17276                "  const auto newEntry      = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
17277                "                                                 std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
17278                "  const X newEntry2        = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
17279                "                                                 std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
17280                "}",
17281                Style);
17282   // clang-format on
17283 
17284   Style.AlignConsecutiveAssignments = FormatStyle::ACS_None;
17285   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
17286   verifyFormat("void foo() {\n"
17287                "  int    myVar = 5;\n"
17288                "  double x = 3.14;\n"
17289                "  auto   str = \"Hello \"\n"
17290                "               \"World\";\n"
17291                "  auto   s = \"Hello \"\n"
17292                "             \"Again\";\n"
17293                "}",
17294                Style);
17295 
17296   // clang-format off
17297   verifyFormat("void foo() {\n"
17298                "  const int  capacityBefore = Entries.capacity();\n"
17299                "  const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
17300                "                                            std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
17301                "  const X    newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
17302                "                                             std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
17303                "}",
17304                Style);
17305   // clang-format on
17306 
17307   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
17308   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
17309 
17310   verifyFormat("void foo() {\n"
17311                "  int    myVar = 5;\n"
17312                "  double x     = 3.14;\n"
17313                "  auto   str   = \"Hello \"\n"
17314                "                 \"World\";\n"
17315                "  auto   s     = \"Hello \"\n"
17316                "                 \"Again\";\n"
17317                "}",
17318                Style);
17319 
17320   // clang-format off
17321   verifyFormat("void foo() {\n"
17322                "  const int  capacityBefore = Entries.capacity();\n"
17323                "  const auto newEntry       = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
17324                "                                                  std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
17325                "  const X    newEntry2      = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
17326                "                                                  std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
17327                "}",
17328                Style);
17329   // clang-format on
17330 
17331   Style = getLLVMStyleWithColumns(120);
17332   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
17333   Style.ContinuationIndentWidth = 4;
17334   Style.IndentWidth = 4;
17335 
17336   // clang-format off
17337   verifyFormat("void SomeFunc() {\n"
17338                "    newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n"
17339                "                                                        seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
17340                "    newWatcher.maxAge     = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n"
17341                "                                                        seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
17342                "    newWatcher.max        = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n"
17343                "                                                        seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
17344                "}",
17345                Style);
17346   // clang-format on
17347 
17348   Style.BinPackArguments = false;
17349 
17350   // clang-format off
17351   verifyFormat("void SomeFunc() {\n"
17352                "    newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(\n"
17353                "        FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
17354                "    newWatcher.maxAge     = ToLegacyTimestamp(GetMaxAge(\n"
17355                "        FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
17356                "    newWatcher.max        = ToLegacyTimestamp(GetMaxAge(\n"
17357                "        FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
17358                "}",
17359                Style);
17360   // clang-format on
17361 }
17362 
17363 TEST_F(FormatTest, AlignWithInitializerPeriods) {
17364   auto Style = getLLVMStyleWithColumns(60);
17365 
17366   verifyFormat("void foo1(void) {\n"
17367                "  BYTE p[1] = 1;\n"
17368                "  A B = {.one_foooooooooooooooo = 2,\n"
17369                "         .two_fooooooooooooo = 3,\n"
17370                "         .three_fooooooooooooo = 4};\n"
17371                "  BYTE payload = 2;\n"
17372                "}",
17373                Style);
17374 
17375   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
17376   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_None;
17377   verifyFormat("void foo2(void) {\n"
17378                "  BYTE p[1]    = 1;\n"
17379                "  A B          = {.one_foooooooooooooooo = 2,\n"
17380                "                  .two_fooooooooooooo    = 3,\n"
17381                "                  .three_fooooooooooooo  = 4};\n"
17382                "  BYTE payload = 2;\n"
17383                "}",
17384                Style);
17385 
17386   Style.AlignConsecutiveAssignments = FormatStyle::ACS_None;
17387   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
17388   verifyFormat("void foo3(void) {\n"
17389                "  BYTE p[1] = 1;\n"
17390                "  A    B = {.one_foooooooooooooooo = 2,\n"
17391                "            .two_fooooooooooooo = 3,\n"
17392                "            .three_fooooooooooooo = 4};\n"
17393                "  BYTE payload = 2;\n"
17394                "}",
17395                Style);
17396 
17397   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
17398   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
17399   verifyFormat("void foo4(void) {\n"
17400                "  BYTE p[1]    = 1;\n"
17401                "  A    B       = {.one_foooooooooooooooo = 2,\n"
17402                "                  .two_fooooooooooooo    = 3,\n"
17403                "                  .three_fooooooooooooo  = 4};\n"
17404                "  BYTE payload = 2;\n"
17405                "}",
17406                Style);
17407 }
17408 
17409 TEST_F(FormatTest, LinuxBraceBreaking) {
17410   FormatStyle LinuxBraceStyle = getLLVMStyle();
17411   LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
17412   verifyFormat("namespace a\n"
17413                "{\n"
17414                "class A\n"
17415                "{\n"
17416                "  void f()\n"
17417                "  {\n"
17418                "    if (true) {\n"
17419                "      a();\n"
17420                "      b();\n"
17421                "    } else {\n"
17422                "      a();\n"
17423                "    }\n"
17424                "  }\n"
17425                "  void g() { return; }\n"
17426                "};\n"
17427                "struct B {\n"
17428                "  int x;\n"
17429                "};\n"
17430                "} // namespace a\n",
17431                LinuxBraceStyle);
17432   verifyFormat("enum X {\n"
17433                "  Y = 0,\n"
17434                "}\n",
17435                LinuxBraceStyle);
17436   verifyFormat("struct S {\n"
17437                "  int Type;\n"
17438                "  union {\n"
17439                "    int x;\n"
17440                "    double y;\n"
17441                "  } Value;\n"
17442                "  class C\n"
17443                "  {\n"
17444                "    MyFavoriteType Value;\n"
17445                "  } Class;\n"
17446                "}\n",
17447                LinuxBraceStyle);
17448 }
17449 
17450 TEST_F(FormatTest, MozillaBraceBreaking) {
17451   FormatStyle MozillaBraceStyle = getLLVMStyle();
17452   MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
17453   MozillaBraceStyle.FixNamespaceComments = false;
17454   verifyFormat("namespace a {\n"
17455                "class A\n"
17456                "{\n"
17457                "  void f()\n"
17458                "  {\n"
17459                "    if (true) {\n"
17460                "      a();\n"
17461                "      b();\n"
17462                "    }\n"
17463                "  }\n"
17464                "  void g() { return; }\n"
17465                "};\n"
17466                "enum E\n"
17467                "{\n"
17468                "  A,\n"
17469                "  // foo\n"
17470                "  B,\n"
17471                "  C\n"
17472                "};\n"
17473                "struct B\n"
17474                "{\n"
17475                "  int x;\n"
17476                "};\n"
17477                "}\n",
17478                MozillaBraceStyle);
17479   verifyFormat("struct S\n"
17480                "{\n"
17481                "  int Type;\n"
17482                "  union\n"
17483                "  {\n"
17484                "    int x;\n"
17485                "    double y;\n"
17486                "  } Value;\n"
17487                "  class C\n"
17488                "  {\n"
17489                "    MyFavoriteType Value;\n"
17490                "  } Class;\n"
17491                "}\n",
17492                MozillaBraceStyle);
17493 }
17494 
17495 TEST_F(FormatTest, StroustrupBraceBreaking) {
17496   FormatStyle StroustrupBraceStyle = getLLVMStyle();
17497   StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
17498   verifyFormat("namespace a {\n"
17499                "class A {\n"
17500                "  void f()\n"
17501                "  {\n"
17502                "    if (true) {\n"
17503                "      a();\n"
17504                "      b();\n"
17505                "    }\n"
17506                "  }\n"
17507                "  void g() { return; }\n"
17508                "};\n"
17509                "struct B {\n"
17510                "  int x;\n"
17511                "};\n"
17512                "} // namespace a\n",
17513                StroustrupBraceStyle);
17514 
17515   verifyFormat("void foo()\n"
17516                "{\n"
17517                "  if (a) {\n"
17518                "    a();\n"
17519                "  }\n"
17520                "  else {\n"
17521                "    b();\n"
17522                "  }\n"
17523                "}\n",
17524                StroustrupBraceStyle);
17525 
17526   verifyFormat("#ifdef _DEBUG\n"
17527                "int foo(int i = 0)\n"
17528                "#else\n"
17529                "int foo(int i = 5)\n"
17530                "#endif\n"
17531                "{\n"
17532                "  return i;\n"
17533                "}",
17534                StroustrupBraceStyle);
17535 
17536   verifyFormat("void foo() {}\n"
17537                "void bar()\n"
17538                "#ifdef _DEBUG\n"
17539                "{\n"
17540                "  foo();\n"
17541                "}\n"
17542                "#else\n"
17543                "{\n"
17544                "}\n"
17545                "#endif",
17546                StroustrupBraceStyle);
17547 
17548   verifyFormat("void foobar() { int i = 5; }\n"
17549                "#ifdef _DEBUG\n"
17550                "void bar() {}\n"
17551                "#else\n"
17552                "void bar() { foobar(); }\n"
17553                "#endif",
17554                StroustrupBraceStyle);
17555 }
17556 
17557 TEST_F(FormatTest, AllmanBraceBreaking) {
17558   FormatStyle AllmanBraceStyle = getLLVMStyle();
17559   AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
17560 
17561   EXPECT_EQ("namespace a\n"
17562             "{\n"
17563             "void f();\n"
17564             "void g();\n"
17565             "} // namespace a\n",
17566             format("namespace a\n"
17567                    "{\n"
17568                    "void f();\n"
17569                    "void g();\n"
17570                    "}\n",
17571                    AllmanBraceStyle));
17572 
17573   verifyFormat("namespace a\n"
17574                "{\n"
17575                "class A\n"
17576                "{\n"
17577                "  void f()\n"
17578                "  {\n"
17579                "    if (true)\n"
17580                "    {\n"
17581                "      a();\n"
17582                "      b();\n"
17583                "    }\n"
17584                "  }\n"
17585                "  void g() { return; }\n"
17586                "};\n"
17587                "struct B\n"
17588                "{\n"
17589                "  int x;\n"
17590                "};\n"
17591                "union C\n"
17592                "{\n"
17593                "};\n"
17594                "} // namespace a",
17595                AllmanBraceStyle);
17596 
17597   verifyFormat("void f()\n"
17598                "{\n"
17599                "  if (true)\n"
17600                "  {\n"
17601                "    a();\n"
17602                "  }\n"
17603                "  else if (false)\n"
17604                "  {\n"
17605                "    b();\n"
17606                "  }\n"
17607                "  else\n"
17608                "  {\n"
17609                "    c();\n"
17610                "  }\n"
17611                "}\n",
17612                AllmanBraceStyle);
17613 
17614   verifyFormat("void f()\n"
17615                "{\n"
17616                "  for (int i = 0; i < 10; ++i)\n"
17617                "  {\n"
17618                "    a();\n"
17619                "  }\n"
17620                "  while (false)\n"
17621                "  {\n"
17622                "    b();\n"
17623                "  }\n"
17624                "  do\n"
17625                "  {\n"
17626                "    c();\n"
17627                "  } while (false)\n"
17628                "}\n",
17629                AllmanBraceStyle);
17630 
17631   verifyFormat("void f(int a)\n"
17632                "{\n"
17633                "  switch (a)\n"
17634                "  {\n"
17635                "  case 0:\n"
17636                "    break;\n"
17637                "  case 1:\n"
17638                "  {\n"
17639                "    break;\n"
17640                "  }\n"
17641                "  case 2:\n"
17642                "  {\n"
17643                "  }\n"
17644                "  break;\n"
17645                "  default:\n"
17646                "    break;\n"
17647                "  }\n"
17648                "}\n",
17649                AllmanBraceStyle);
17650 
17651   verifyFormat("enum X\n"
17652                "{\n"
17653                "  Y = 0,\n"
17654                "}\n",
17655                AllmanBraceStyle);
17656   verifyFormat("enum X\n"
17657                "{\n"
17658                "  Y = 0\n"
17659                "}\n",
17660                AllmanBraceStyle);
17661 
17662   verifyFormat("@interface BSApplicationController ()\n"
17663                "{\n"
17664                "@private\n"
17665                "  id _extraIvar;\n"
17666                "}\n"
17667                "@end\n",
17668                AllmanBraceStyle);
17669 
17670   verifyFormat("#ifdef _DEBUG\n"
17671                "int foo(int i = 0)\n"
17672                "#else\n"
17673                "int foo(int i = 5)\n"
17674                "#endif\n"
17675                "{\n"
17676                "  return i;\n"
17677                "}",
17678                AllmanBraceStyle);
17679 
17680   verifyFormat("void foo() {}\n"
17681                "void bar()\n"
17682                "#ifdef _DEBUG\n"
17683                "{\n"
17684                "  foo();\n"
17685                "}\n"
17686                "#else\n"
17687                "{\n"
17688                "}\n"
17689                "#endif",
17690                AllmanBraceStyle);
17691 
17692   verifyFormat("void foobar() { int i = 5; }\n"
17693                "#ifdef _DEBUG\n"
17694                "void bar() {}\n"
17695                "#else\n"
17696                "void bar() { foobar(); }\n"
17697                "#endif",
17698                AllmanBraceStyle);
17699 
17700   EXPECT_EQ(AllmanBraceStyle.AllowShortLambdasOnASingleLine,
17701             FormatStyle::SLS_All);
17702 
17703   verifyFormat("[](int i) { return i + 2; };\n"
17704                "[](int i, int j)\n"
17705                "{\n"
17706                "  auto x = i + j;\n"
17707                "  auto y = i * j;\n"
17708                "  return x ^ y;\n"
17709                "};\n"
17710                "void foo()\n"
17711                "{\n"
17712                "  auto shortLambda = [](int i) { return i + 2; };\n"
17713                "  auto longLambda = [](int i, int j)\n"
17714                "  {\n"
17715                "    auto x = i + j;\n"
17716                "    auto y = i * j;\n"
17717                "    return x ^ y;\n"
17718                "  };\n"
17719                "}",
17720                AllmanBraceStyle);
17721 
17722   AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
17723 
17724   verifyFormat("[](int i)\n"
17725                "{\n"
17726                "  return i + 2;\n"
17727                "};\n"
17728                "[](int i, int j)\n"
17729                "{\n"
17730                "  auto x = i + j;\n"
17731                "  auto y = i * j;\n"
17732                "  return x ^ y;\n"
17733                "};\n"
17734                "void foo()\n"
17735                "{\n"
17736                "  auto shortLambda = [](int i)\n"
17737                "  {\n"
17738                "    return i + 2;\n"
17739                "  };\n"
17740                "  auto longLambda = [](int i, int j)\n"
17741                "  {\n"
17742                "    auto x = i + j;\n"
17743                "    auto y = i * j;\n"
17744                "    return x ^ y;\n"
17745                "  };\n"
17746                "}",
17747                AllmanBraceStyle);
17748 
17749   // Reset
17750   AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_All;
17751 
17752   // This shouldn't affect ObjC blocks..
17753   verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
17754                "  // ...\n"
17755                "  int i;\n"
17756                "}];",
17757                AllmanBraceStyle);
17758   verifyFormat("void (^block)(void) = ^{\n"
17759                "  // ...\n"
17760                "  int i;\n"
17761                "};",
17762                AllmanBraceStyle);
17763   // .. or dict literals.
17764   verifyFormat("void f()\n"
17765                "{\n"
17766                "  // ...\n"
17767                "  [object someMethod:@{@\"a\" : @\"b\"}];\n"
17768                "}",
17769                AllmanBraceStyle);
17770   verifyFormat("void f()\n"
17771                "{\n"
17772                "  // ...\n"
17773                "  [object someMethod:@{a : @\"b\"}];\n"
17774                "}",
17775                AllmanBraceStyle);
17776   verifyFormat("int f()\n"
17777                "{ // comment\n"
17778                "  return 42;\n"
17779                "}",
17780                AllmanBraceStyle);
17781 
17782   AllmanBraceStyle.ColumnLimit = 19;
17783   verifyFormat("void f() { int i; }", AllmanBraceStyle);
17784   AllmanBraceStyle.ColumnLimit = 18;
17785   verifyFormat("void f()\n"
17786                "{\n"
17787                "  int i;\n"
17788                "}",
17789                AllmanBraceStyle);
17790   AllmanBraceStyle.ColumnLimit = 80;
17791 
17792   FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle;
17793   BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine =
17794       FormatStyle::SIS_WithoutElse;
17795   BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
17796   verifyFormat("void f(bool b)\n"
17797                "{\n"
17798                "  if (b)\n"
17799                "  {\n"
17800                "    return;\n"
17801                "  }\n"
17802                "}\n",
17803                BreakBeforeBraceShortIfs);
17804   verifyFormat("void f(bool b)\n"
17805                "{\n"
17806                "  if constexpr (b)\n"
17807                "  {\n"
17808                "    return;\n"
17809                "  }\n"
17810                "}\n",
17811                BreakBeforeBraceShortIfs);
17812   verifyFormat("void f(bool b)\n"
17813                "{\n"
17814                "  if CONSTEXPR (b)\n"
17815                "  {\n"
17816                "    return;\n"
17817                "  }\n"
17818                "}\n",
17819                BreakBeforeBraceShortIfs);
17820   verifyFormat("void f(bool b)\n"
17821                "{\n"
17822                "  if (b) return;\n"
17823                "}\n",
17824                BreakBeforeBraceShortIfs);
17825   verifyFormat("void f(bool b)\n"
17826                "{\n"
17827                "  if constexpr (b) return;\n"
17828                "}\n",
17829                BreakBeforeBraceShortIfs);
17830   verifyFormat("void f(bool b)\n"
17831                "{\n"
17832                "  if CONSTEXPR (b) return;\n"
17833                "}\n",
17834                BreakBeforeBraceShortIfs);
17835   verifyFormat("void f(bool b)\n"
17836                "{\n"
17837                "  while (b)\n"
17838                "  {\n"
17839                "    return;\n"
17840                "  }\n"
17841                "}\n",
17842                BreakBeforeBraceShortIfs);
17843 }
17844 
17845 TEST_F(FormatTest, WhitesmithsBraceBreaking) {
17846   FormatStyle WhitesmithsBraceStyle = getLLVMStyleWithColumns(0);
17847   WhitesmithsBraceStyle.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
17848 
17849   // Make a few changes to the style for testing purposes
17850   WhitesmithsBraceStyle.AllowShortFunctionsOnASingleLine =
17851       FormatStyle::SFS_Empty;
17852   WhitesmithsBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
17853 
17854   // FIXME: this test case can't decide whether there should be a blank line
17855   // after the ~D() line or not. It adds one if one doesn't exist in the test
17856   // and it removes the line if one exists.
17857   /*
17858   verifyFormat("class A;\n"
17859                "namespace B\n"
17860                "  {\n"
17861                "class C;\n"
17862                "// Comment\n"
17863                "class D\n"
17864                "  {\n"
17865                "public:\n"
17866                "  D();\n"
17867                "  ~D() {}\n"
17868                "private:\n"
17869                "  enum E\n"
17870                "    {\n"
17871                "    F\n"
17872                "    }\n"
17873                "  };\n"
17874                "  } // namespace B\n",
17875                WhitesmithsBraceStyle);
17876   */
17877 
17878   WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_None;
17879   verifyFormat("namespace a\n"
17880                "  {\n"
17881                "class A\n"
17882                "  {\n"
17883                "  void f()\n"
17884                "    {\n"
17885                "    if (true)\n"
17886                "      {\n"
17887                "      a();\n"
17888                "      b();\n"
17889                "      }\n"
17890                "    }\n"
17891                "  void g()\n"
17892                "    {\n"
17893                "    return;\n"
17894                "    }\n"
17895                "  };\n"
17896                "struct B\n"
17897                "  {\n"
17898                "  int x;\n"
17899                "  };\n"
17900                "  } // namespace a",
17901                WhitesmithsBraceStyle);
17902 
17903   verifyFormat("namespace a\n"
17904                "  {\n"
17905                "namespace b\n"
17906                "  {\n"
17907                "class A\n"
17908                "  {\n"
17909                "  void f()\n"
17910                "    {\n"
17911                "    if (true)\n"
17912                "      {\n"
17913                "      a();\n"
17914                "      b();\n"
17915                "      }\n"
17916                "    }\n"
17917                "  void g()\n"
17918                "    {\n"
17919                "    return;\n"
17920                "    }\n"
17921                "  };\n"
17922                "struct B\n"
17923                "  {\n"
17924                "  int x;\n"
17925                "  };\n"
17926                "  } // namespace b\n"
17927                "  } // namespace a",
17928                WhitesmithsBraceStyle);
17929 
17930   WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_Inner;
17931   verifyFormat("namespace a\n"
17932                "  {\n"
17933                "namespace b\n"
17934                "  {\n"
17935                "  class A\n"
17936                "    {\n"
17937                "    void f()\n"
17938                "      {\n"
17939                "      if (true)\n"
17940                "        {\n"
17941                "        a();\n"
17942                "        b();\n"
17943                "        }\n"
17944                "      }\n"
17945                "    void g()\n"
17946                "      {\n"
17947                "      return;\n"
17948                "      }\n"
17949                "    };\n"
17950                "  struct B\n"
17951                "    {\n"
17952                "    int x;\n"
17953                "    };\n"
17954                "  } // namespace b\n"
17955                "  } // namespace a",
17956                WhitesmithsBraceStyle);
17957 
17958   WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_All;
17959   verifyFormat("namespace a\n"
17960                "  {\n"
17961                "  namespace b\n"
17962                "    {\n"
17963                "    class A\n"
17964                "      {\n"
17965                "      void f()\n"
17966                "        {\n"
17967                "        if (true)\n"
17968                "          {\n"
17969                "          a();\n"
17970                "          b();\n"
17971                "          }\n"
17972                "        }\n"
17973                "      void g()\n"
17974                "        {\n"
17975                "        return;\n"
17976                "        }\n"
17977                "      };\n"
17978                "    struct B\n"
17979                "      {\n"
17980                "      int x;\n"
17981                "      };\n"
17982                "    } // namespace b\n"
17983                "  }   // namespace a",
17984                WhitesmithsBraceStyle);
17985 
17986   verifyFormat("void f()\n"
17987                "  {\n"
17988                "  if (true)\n"
17989                "    {\n"
17990                "    a();\n"
17991                "    }\n"
17992                "  else if (false)\n"
17993                "    {\n"
17994                "    b();\n"
17995                "    }\n"
17996                "  else\n"
17997                "    {\n"
17998                "    c();\n"
17999                "    }\n"
18000                "  }\n",
18001                WhitesmithsBraceStyle);
18002 
18003   verifyFormat("void f()\n"
18004                "  {\n"
18005                "  for (int i = 0; i < 10; ++i)\n"
18006                "    {\n"
18007                "    a();\n"
18008                "    }\n"
18009                "  while (false)\n"
18010                "    {\n"
18011                "    b();\n"
18012                "    }\n"
18013                "  do\n"
18014                "    {\n"
18015                "    c();\n"
18016                "    } while (false)\n"
18017                "  }\n",
18018                WhitesmithsBraceStyle);
18019 
18020   WhitesmithsBraceStyle.IndentCaseLabels = true;
18021   verifyFormat("void switchTest1(int a)\n"
18022                "  {\n"
18023                "  switch (a)\n"
18024                "    {\n"
18025                "    case 2:\n"
18026                "      {\n"
18027                "      }\n"
18028                "      break;\n"
18029                "    }\n"
18030                "  }\n",
18031                WhitesmithsBraceStyle);
18032 
18033   verifyFormat("void switchTest2(int a)\n"
18034                "  {\n"
18035                "  switch (a)\n"
18036                "    {\n"
18037                "    case 0:\n"
18038                "      break;\n"
18039                "    case 1:\n"
18040                "      {\n"
18041                "      break;\n"
18042                "      }\n"
18043                "    case 2:\n"
18044                "      {\n"
18045                "      }\n"
18046                "      break;\n"
18047                "    default:\n"
18048                "      break;\n"
18049                "    }\n"
18050                "  }\n",
18051                WhitesmithsBraceStyle);
18052 
18053   verifyFormat("void switchTest3(int a)\n"
18054                "  {\n"
18055                "  switch (a)\n"
18056                "    {\n"
18057                "    case 0:\n"
18058                "      {\n"
18059                "      foo(x);\n"
18060                "      }\n"
18061                "      break;\n"
18062                "    default:\n"
18063                "      {\n"
18064                "      foo(1);\n"
18065                "      }\n"
18066                "      break;\n"
18067                "    }\n"
18068                "  }\n",
18069                WhitesmithsBraceStyle);
18070 
18071   WhitesmithsBraceStyle.IndentCaseLabels = false;
18072 
18073   verifyFormat("void switchTest4(int a)\n"
18074                "  {\n"
18075                "  switch (a)\n"
18076                "    {\n"
18077                "  case 2:\n"
18078                "    {\n"
18079                "    }\n"
18080                "    break;\n"
18081                "    }\n"
18082                "  }\n",
18083                WhitesmithsBraceStyle);
18084 
18085   verifyFormat("void switchTest5(int a)\n"
18086                "  {\n"
18087                "  switch (a)\n"
18088                "    {\n"
18089                "  case 0:\n"
18090                "    break;\n"
18091                "  case 1:\n"
18092                "    {\n"
18093                "    foo();\n"
18094                "    break;\n"
18095                "    }\n"
18096                "  case 2:\n"
18097                "    {\n"
18098                "    }\n"
18099                "    break;\n"
18100                "  default:\n"
18101                "    break;\n"
18102                "    }\n"
18103                "  }\n",
18104                WhitesmithsBraceStyle);
18105 
18106   verifyFormat("void switchTest6(int a)\n"
18107                "  {\n"
18108                "  switch (a)\n"
18109                "    {\n"
18110                "  case 0:\n"
18111                "    {\n"
18112                "    foo(x);\n"
18113                "    }\n"
18114                "    break;\n"
18115                "  default:\n"
18116                "    {\n"
18117                "    foo(1);\n"
18118                "    }\n"
18119                "    break;\n"
18120                "    }\n"
18121                "  }\n",
18122                WhitesmithsBraceStyle);
18123 
18124   verifyFormat("enum X\n"
18125                "  {\n"
18126                "  Y = 0, // testing\n"
18127                "  }\n",
18128                WhitesmithsBraceStyle);
18129 
18130   verifyFormat("enum X\n"
18131                "  {\n"
18132                "  Y = 0\n"
18133                "  }\n",
18134                WhitesmithsBraceStyle);
18135   verifyFormat("enum X\n"
18136                "  {\n"
18137                "  Y = 0,\n"
18138                "  Z = 1\n"
18139                "  };\n",
18140                WhitesmithsBraceStyle);
18141 
18142   verifyFormat("@interface BSApplicationController ()\n"
18143                "  {\n"
18144                "@private\n"
18145                "  id _extraIvar;\n"
18146                "  }\n"
18147                "@end\n",
18148                WhitesmithsBraceStyle);
18149 
18150   verifyFormat("#ifdef _DEBUG\n"
18151                "int foo(int i = 0)\n"
18152                "#else\n"
18153                "int foo(int i = 5)\n"
18154                "#endif\n"
18155                "  {\n"
18156                "  return i;\n"
18157                "  }",
18158                WhitesmithsBraceStyle);
18159 
18160   verifyFormat("void foo() {}\n"
18161                "void bar()\n"
18162                "#ifdef _DEBUG\n"
18163                "  {\n"
18164                "  foo();\n"
18165                "  }\n"
18166                "#else\n"
18167                "  {\n"
18168                "  }\n"
18169                "#endif",
18170                WhitesmithsBraceStyle);
18171 
18172   verifyFormat("void foobar()\n"
18173                "  {\n"
18174                "  int i = 5;\n"
18175                "  }\n"
18176                "#ifdef _DEBUG\n"
18177                "void bar()\n"
18178                "  {\n"
18179                "  }\n"
18180                "#else\n"
18181                "void bar()\n"
18182                "  {\n"
18183                "  foobar();\n"
18184                "  }\n"
18185                "#endif",
18186                WhitesmithsBraceStyle);
18187 
18188   // This shouldn't affect ObjC blocks..
18189   verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
18190                "  // ...\n"
18191                "  int i;\n"
18192                "}];",
18193                WhitesmithsBraceStyle);
18194   verifyFormat("void (^block)(void) = ^{\n"
18195                "  // ...\n"
18196                "  int i;\n"
18197                "};",
18198                WhitesmithsBraceStyle);
18199   // .. or dict literals.
18200   verifyFormat("void f()\n"
18201                "  {\n"
18202                "  [object someMethod:@{@\"a\" : @\"b\"}];\n"
18203                "  }",
18204                WhitesmithsBraceStyle);
18205 
18206   verifyFormat("int f()\n"
18207                "  { // comment\n"
18208                "  return 42;\n"
18209                "  }",
18210                WhitesmithsBraceStyle);
18211 
18212   FormatStyle BreakBeforeBraceShortIfs = WhitesmithsBraceStyle;
18213   BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine =
18214       FormatStyle::SIS_OnlyFirstIf;
18215   BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
18216   verifyFormat("void f(bool b)\n"
18217                "  {\n"
18218                "  if (b)\n"
18219                "    {\n"
18220                "    return;\n"
18221                "    }\n"
18222                "  }\n",
18223                BreakBeforeBraceShortIfs);
18224   verifyFormat("void f(bool b)\n"
18225                "  {\n"
18226                "  if (b) return;\n"
18227                "  }\n",
18228                BreakBeforeBraceShortIfs);
18229   verifyFormat("void f(bool b)\n"
18230                "  {\n"
18231                "  while (b)\n"
18232                "    {\n"
18233                "    return;\n"
18234                "    }\n"
18235                "  }\n",
18236                BreakBeforeBraceShortIfs);
18237 }
18238 
18239 TEST_F(FormatTest, GNUBraceBreaking) {
18240   FormatStyle GNUBraceStyle = getLLVMStyle();
18241   GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
18242   verifyFormat("namespace a\n"
18243                "{\n"
18244                "class A\n"
18245                "{\n"
18246                "  void f()\n"
18247                "  {\n"
18248                "    int a;\n"
18249                "    {\n"
18250                "      int b;\n"
18251                "    }\n"
18252                "    if (true)\n"
18253                "      {\n"
18254                "        a();\n"
18255                "        b();\n"
18256                "      }\n"
18257                "  }\n"
18258                "  void g() { return; }\n"
18259                "}\n"
18260                "} // namespace a",
18261                GNUBraceStyle);
18262 
18263   verifyFormat("void f()\n"
18264                "{\n"
18265                "  if (true)\n"
18266                "    {\n"
18267                "      a();\n"
18268                "    }\n"
18269                "  else if (false)\n"
18270                "    {\n"
18271                "      b();\n"
18272                "    }\n"
18273                "  else\n"
18274                "    {\n"
18275                "      c();\n"
18276                "    }\n"
18277                "}\n",
18278                GNUBraceStyle);
18279 
18280   verifyFormat("void f()\n"
18281                "{\n"
18282                "  for (int i = 0; i < 10; ++i)\n"
18283                "    {\n"
18284                "      a();\n"
18285                "    }\n"
18286                "  while (false)\n"
18287                "    {\n"
18288                "      b();\n"
18289                "    }\n"
18290                "  do\n"
18291                "    {\n"
18292                "      c();\n"
18293                "    }\n"
18294                "  while (false);\n"
18295                "}\n",
18296                GNUBraceStyle);
18297 
18298   verifyFormat("void f(int a)\n"
18299                "{\n"
18300                "  switch (a)\n"
18301                "    {\n"
18302                "    case 0:\n"
18303                "      break;\n"
18304                "    case 1:\n"
18305                "      {\n"
18306                "        break;\n"
18307                "      }\n"
18308                "    case 2:\n"
18309                "      {\n"
18310                "      }\n"
18311                "      break;\n"
18312                "    default:\n"
18313                "      break;\n"
18314                "    }\n"
18315                "}\n",
18316                GNUBraceStyle);
18317 
18318   verifyFormat("enum X\n"
18319                "{\n"
18320                "  Y = 0,\n"
18321                "}\n",
18322                GNUBraceStyle);
18323 
18324   verifyFormat("@interface BSApplicationController ()\n"
18325                "{\n"
18326                "@private\n"
18327                "  id _extraIvar;\n"
18328                "}\n"
18329                "@end\n",
18330                GNUBraceStyle);
18331 
18332   verifyFormat("#ifdef _DEBUG\n"
18333                "int foo(int i = 0)\n"
18334                "#else\n"
18335                "int foo(int i = 5)\n"
18336                "#endif\n"
18337                "{\n"
18338                "  return i;\n"
18339                "}",
18340                GNUBraceStyle);
18341 
18342   verifyFormat("void foo() {}\n"
18343                "void bar()\n"
18344                "#ifdef _DEBUG\n"
18345                "{\n"
18346                "  foo();\n"
18347                "}\n"
18348                "#else\n"
18349                "{\n"
18350                "}\n"
18351                "#endif",
18352                GNUBraceStyle);
18353 
18354   verifyFormat("void foobar() { int i = 5; }\n"
18355                "#ifdef _DEBUG\n"
18356                "void bar() {}\n"
18357                "#else\n"
18358                "void bar() { foobar(); }\n"
18359                "#endif",
18360                GNUBraceStyle);
18361 }
18362 
18363 TEST_F(FormatTest, WebKitBraceBreaking) {
18364   FormatStyle WebKitBraceStyle = getLLVMStyle();
18365   WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit;
18366   WebKitBraceStyle.FixNamespaceComments = false;
18367   verifyFormat("namespace a {\n"
18368                "class A {\n"
18369                "  void f()\n"
18370                "  {\n"
18371                "    if (true) {\n"
18372                "      a();\n"
18373                "      b();\n"
18374                "    }\n"
18375                "  }\n"
18376                "  void g() { return; }\n"
18377                "};\n"
18378                "enum E {\n"
18379                "  A,\n"
18380                "  // foo\n"
18381                "  B,\n"
18382                "  C\n"
18383                "};\n"
18384                "struct B {\n"
18385                "  int x;\n"
18386                "};\n"
18387                "}\n",
18388                WebKitBraceStyle);
18389   verifyFormat("struct S {\n"
18390                "  int Type;\n"
18391                "  union {\n"
18392                "    int x;\n"
18393                "    double y;\n"
18394                "  } Value;\n"
18395                "  class C {\n"
18396                "    MyFavoriteType Value;\n"
18397                "  } Class;\n"
18398                "};\n",
18399                WebKitBraceStyle);
18400 }
18401 
18402 TEST_F(FormatTest, CatchExceptionReferenceBinding) {
18403   verifyFormat("void f() {\n"
18404                "  try {\n"
18405                "  } catch (const Exception &e) {\n"
18406                "  }\n"
18407                "}\n",
18408                getLLVMStyle());
18409 }
18410 
18411 TEST_F(FormatTest, CatchAlignArrayOfStructuresRightAlignment) {
18412   auto Style = getLLVMStyle();
18413   Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
18414   Style.AlignConsecutiveAssignments =
18415       FormatStyle::AlignConsecutiveStyle::ACS_Consecutive;
18416   Style.AlignConsecutiveDeclarations =
18417       FormatStyle::AlignConsecutiveStyle::ACS_Consecutive;
18418   verifyFormat("struct test demo[] = {\n"
18419                "    {56,    23, \"hello\"},\n"
18420                "    {-1, 93463, \"world\"},\n"
18421                "    { 7,     5,    \"!!\"}\n"
18422                "};\n",
18423                Style);
18424 
18425   verifyFormat("struct test demo[] = {\n"
18426                "    {56,    23, \"hello\"}, // first line\n"
18427                "    {-1, 93463, \"world\"}, // second line\n"
18428                "    { 7,     5,    \"!!\"}  // third line\n"
18429                "};\n",
18430                Style);
18431 
18432   verifyFormat("struct test demo[4] = {\n"
18433                "    { 56,    23, 21,       \"oh\"}, // first line\n"
18434                "    { -1, 93463, 22,       \"my\"}, // second line\n"
18435                "    {  7,     5,  1, \"goodness\"}  // third line\n"
18436                "    {234,     5,  1, \"gracious\"}  // fourth line\n"
18437                "};\n",
18438                Style);
18439 
18440   verifyFormat("struct test demo[3] = {\n"
18441                "    {56,    23, \"hello\"},\n"
18442                "    {-1, 93463, \"world\"},\n"
18443                "    { 7,     5,    \"!!\"}\n"
18444                "};\n",
18445                Style);
18446 
18447   verifyFormat("struct test demo[3] = {\n"
18448                "    {int{56},    23, \"hello\"},\n"
18449                "    {int{-1}, 93463, \"world\"},\n"
18450                "    { int{7},     5,    \"!!\"}\n"
18451                "};\n",
18452                Style);
18453 
18454   verifyFormat("struct test demo[] = {\n"
18455                "    {56,    23, \"hello\"},\n"
18456                "    {-1, 93463, \"world\"},\n"
18457                "    { 7,     5,    \"!!\"},\n"
18458                "};\n",
18459                Style);
18460 
18461   verifyFormat("test demo[] = {\n"
18462                "    {56,    23, \"hello\"},\n"
18463                "    {-1, 93463, \"world\"},\n"
18464                "    { 7,     5,    \"!!\"},\n"
18465                "};\n",
18466                Style);
18467 
18468   verifyFormat("demo = std::array<struct test, 3>{\n"
18469                "    test{56,    23, \"hello\"},\n"
18470                "    test{-1, 93463, \"world\"},\n"
18471                "    test{ 7,     5,    \"!!\"},\n"
18472                "};\n",
18473                Style);
18474 
18475   verifyFormat("test demo[] = {\n"
18476                "    {56,    23, \"hello\"},\n"
18477                "#if X\n"
18478                "    {-1, 93463, \"world\"},\n"
18479                "#endif\n"
18480                "    { 7,     5,    \"!!\"}\n"
18481                "};\n",
18482                Style);
18483 
18484   verifyFormat(
18485       "test demo[] = {\n"
18486       "    { 7,    23,\n"
18487       "     \"hello world i am a very long line that really, in any\"\n"
18488       "     \"just world, ought to be split over multiple lines\"},\n"
18489       "    {-1, 93463,                                  \"world\"},\n"
18490       "    {56,     5,                                     \"!!\"}\n"
18491       "};\n",
18492       Style);
18493 
18494   verifyFormat("return GradForUnaryCwise(g, {\n"
18495                "                                {{\"sign\"}, \"Sign\",  "
18496                "  {\"x\", \"dy\"}},\n"
18497                "                                {  {\"dx\"},  \"Mul\", {\"dy\""
18498                ", \"sign\"}},\n"
18499                "});\n",
18500                Style);
18501 
18502   Style.ColumnLimit = 0;
18503   EXPECT_EQ(
18504       "test demo[] = {\n"
18505       "    {56,    23, \"hello world i am a very long line that really, "
18506       "in any just world, ought to be split over multiple lines\"},\n"
18507       "    {-1, 93463,                                                  "
18508       "                                                 \"world\"},\n"
18509       "    { 7,     5,                                                  "
18510       "                                                    \"!!\"},\n"
18511       "};",
18512       format("test demo[] = {{56, 23, \"hello world i am a very long line "
18513              "that really, in any just world, ought to be split over multiple "
18514              "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
18515              Style));
18516 
18517   Style.ColumnLimit = 80;
18518   verifyFormat("test demo[] = {\n"
18519                "    {56,    23, /* a comment */ \"hello\"},\n"
18520                "    {-1, 93463,                 \"world\"},\n"
18521                "    { 7,     5,                    \"!!\"}\n"
18522                "};\n",
18523                Style);
18524 
18525   verifyFormat("test demo[] = {\n"
18526                "    {56,    23,                    \"hello\"},\n"
18527                "    {-1, 93463, \"world\" /* comment here */},\n"
18528                "    { 7,     5,                       \"!!\"}\n"
18529                "};\n",
18530                Style);
18531 
18532   verifyFormat("test demo[] = {\n"
18533                "    {56, /* a comment */ 23, \"hello\"},\n"
18534                "    {-1,              93463, \"world\"},\n"
18535                "    { 7,                  5,    \"!!\"}\n"
18536                "};\n",
18537                Style);
18538 
18539   Style.ColumnLimit = 20;
18540   EXPECT_EQ(
18541       "demo = std::array<\n"
18542       "    struct test, 3>{\n"
18543       "    test{\n"
18544       "         56,    23,\n"
18545       "         \"hello \"\n"
18546       "         \"world i \"\n"
18547       "         \"am a very \"\n"
18548       "         \"long line \"\n"
18549       "         \"that \"\n"
18550       "         \"really, \"\n"
18551       "         \"in any \"\n"
18552       "         \"just \"\n"
18553       "         \"world, \"\n"
18554       "         \"ought to \"\n"
18555       "         \"be split \"\n"
18556       "         \"over \"\n"
18557       "         \"multiple \"\n"
18558       "         \"lines\"},\n"
18559       "    test{-1, 93463,\n"
18560       "         \"world\"},\n"
18561       "    test{ 7,     5,\n"
18562       "         \"!!\"   },\n"
18563       "};",
18564       format("demo = std::array<struct test, 3>{test{56, 23, \"hello world "
18565              "i am a very long line that really, in any just world, ought "
18566              "to be split over multiple lines\"},test{-1, 93463, \"world\"},"
18567              "test{7, 5, \"!!\"},};",
18568              Style));
18569   // This caused a core dump by enabling Alignment in the LLVMStyle globally
18570   Style = getLLVMStyleWithColumns(50);
18571   Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
18572   verifyFormat("static A x = {\n"
18573                "    {{init1, init2, init3, init4},\n"
18574                "     {init1, init2, init3, init4}}\n"
18575                "};",
18576                Style);
18577   Style.ColumnLimit = 100;
18578   EXPECT_EQ(
18579       "test demo[] = {\n"
18580       "    {56,    23,\n"
18581       "     \"hello world i am a very long line that really, in any just world"
18582       ", ought to be split over \"\n"
18583       "     \"multiple lines\"  },\n"
18584       "    {-1, 93463, \"world\"},\n"
18585       "    { 7,     5,    \"!!\"},\n"
18586       "};",
18587       format("test demo[] = {{56, 23, \"hello world i am a very long line "
18588              "that really, in any just world, ought to be split over multiple "
18589              "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
18590              Style));
18591 
18592   Style = getLLVMStyleWithColumns(50);
18593   Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
18594   Style.AlignConsecutiveAssignments =
18595       FormatStyle::AlignConsecutiveStyle::ACS_Consecutive;
18596   Style.AlignConsecutiveDeclarations =
18597       FormatStyle::AlignConsecutiveStyle::ACS_Consecutive;
18598   verifyFormat("struct test demo[] = {\n"
18599                "    {56,    23, \"hello\"},\n"
18600                "    {-1, 93463, \"world\"},\n"
18601                "    { 7,     5,    \"!!\"}\n"
18602                "};\n"
18603                "static A x = {\n"
18604                "    {{init1, init2, init3, init4},\n"
18605                "     {init1, init2, init3, init4}}\n"
18606                "};",
18607                Style);
18608   Style.ColumnLimit = 100;
18609   Style.AlignConsecutiveAssignments =
18610       FormatStyle::AlignConsecutiveStyle::ACS_AcrossComments;
18611   Style.AlignConsecutiveDeclarations =
18612       FormatStyle::AlignConsecutiveStyle::ACS_AcrossComments;
18613   verifyFormat("struct test demo[] = {\n"
18614                "    {56,    23, \"hello\"},\n"
18615                "    {-1, 93463, \"world\"},\n"
18616                "    { 7,     5,    \"!!\"}\n"
18617                "};\n"
18618                "struct test demo[4] = {\n"
18619                "    { 56,    23, 21,       \"oh\"}, // first line\n"
18620                "    { -1, 93463, 22,       \"my\"}, // second line\n"
18621                "    {  7,     5,  1, \"goodness\"}  // third line\n"
18622                "    {234,     5,  1, \"gracious\"}  // fourth line\n"
18623                "};\n",
18624                Style);
18625   EXPECT_EQ(
18626       "test demo[] = {\n"
18627       "    {56,\n"
18628       "     \"hello world i am a very long line that really, in any just world"
18629       ", ought to be split over \"\n"
18630       "     \"multiple lines\",    23},\n"
18631       "    {-1,      \"world\", 93463},\n"
18632       "    { 7,         \"!!\",     5},\n"
18633       "};",
18634       format("test demo[] = {{56, \"hello world i am a very long line "
18635              "that really, in any just world, ought to be split over multiple "
18636              "lines\", 23},{-1, \"world\", 93463},{7, \"!!\", 5},};",
18637              Style));
18638 }
18639 
18640 TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) {
18641   auto Style = getLLVMStyle();
18642   Style.AlignArrayOfStructures = FormatStyle::AIAS_Left;
18643   /* FIXME: This case gets misformatted.
18644   verifyFormat("auto foo = Items{\n"
18645                "    Section{0, bar(), },\n"
18646                "    Section{1, boo()  }\n"
18647                "};\n",
18648                Style);
18649   */
18650   verifyFormat("auto foo = Items{\n"
18651                "    Section{\n"
18652                "            0, bar(),\n"
18653                "            }\n"
18654                "};\n",
18655                Style);
18656   verifyFormat("struct test demo[] = {\n"
18657                "    {56, 23,    \"hello\"},\n"
18658                "    {-1, 93463, \"world\"},\n"
18659                "    {7,  5,     \"!!\"   }\n"
18660                "};\n",
18661                Style);
18662   verifyFormat("struct test demo[] = {\n"
18663                "    {56, 23,    \"hello\"}, // first line\n"
18664                "    {-1, 93463, \"world\"}, // second line\n"
18665                "    {7,  5,     \"!!\"   }  // third line\n"
18666                "};\n",
18667                Style);
18668   verifyFormat("struct test demo[4] = {\n"
18669                "    {56,  23,    21, \"oh\"      }, // first line\n"
18670                "    {-1,  93463, 22, \"my\"      }, // second line\n"
18671                "    {7,   5,     1,  \"goodness\"}  // third line\n"
18672                "    {234, 5,     1,  \"gracious\"}  // fourth line\n"
18673                "};\n",
18674                Style);
18675   verifyFormat("struct test demo[3] = {\n"
18676                "    {56, 23,    \"hello\"},\n"
18677                "    {-1, 93463, \"world\"},\n"
18678                "    {7,  5,     \"!!\"   }\n"
18679                "};\n",
18680                Style);
18681 
18682   verifyFormat("struct test demo[3] = {\n"
18683                "    {int{56}, 23,    \"hello\"},\n"
18684                "    {int{-1}, 93463, \"world\"},\n"
18685                "    {int{7},  5,     \"!!\"   }\n"
18686                "};\n",
18687                Style);
18688   verifyFormat("struct test demo[] = {\n"
18689                "    {56, 23,    \"hello\"},\n"
18690                "    {-1, 93463, \"world\"},\n"
18691                "    {7,  5,     \"!!\"   },\n"
18692                "};\n",
18693                Style);
18694   verifyFormat("test demo[] = {\n"
18695                "    {56, 23,    \"hello\"},\n"
18696                "    {-1, 93463, \"world\"},\n"
18697                "    {7,  5,     \"!!\"   },\n"
18698                "};\n",
18699                Style);
18700   verifyFormat("demo = std::array<struct test, 3>{\n"
18701                "    test{56, 23,    \"hello\"},\n"
18702                "    test{-1, 93463, \"world\"},\n"
18703                "    test{7,  5,     \"!!\"   },\n"
18704                "};\n",
18705                Style);
18706   verifyFormat("test demo[] = {\n"
18707                "    {56, 23,    \"hello\"},\n"
18708                "#if X\n"
18709                "    {-1, 93463, \"world\"},\n"
18710                "#endif\n"
18711                "    {7,  5,     \"!!\"   }\n"
18712                "};\n",
18713                Style);
18714   verifyFormat(
18715       "test demo[] = {\n"
18716       "    {7,  23,\n"
18717       "     \"hello world i am a very long line that really, in any\"\n"
18718       "     \"just world, ought to be split over multiple lines\"},\n"
18719       "    {-1, 93463, \"world\"                                 },\n"
18720       "    {56, 5,     \"!!\"                                    }\n"
18721       "};\n",
18722       Style);
18723 
18724   verifyFormat("return GradForUnaryCwise(g, {\n"
18725                "                                {{\"sign\"}, \"Sign\", {\"x\", "
18726                "\"dy\"}   },\n"
18727                "                                {{\"dx\"},   \"Mul\",  "
18728                "{\"dy\", \"sign\"}},\n"
18729                "});\n",
18730                Style);
18731 
18732   Style.ColumnLimit = 0;
18733   EXPECT_EQ(
18734       "test demo[] = {\n"
18735       "    {56, 23,    \"hello world i am a very long line that really, in any "
18736       "just world, ought to be split over multiple lines\"},\n"
18737       "    {-1, 93463, \"world\"                                               "
18738       "                                                   },\n"
18739       "    {7,  5,     \"!!\"                                                  "
18740       "                                                   },\n"
18741       "};",
18742       format("test demo[] = {{56, 23, \"hello world i am a very long line "
18743              "that really, in any just world, ought to be split over multiple "
18744              "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
18745              Style));
18746 
18747   Style.ColumnLimit = 80;
18748   verifyFormat("test demo[] = {\n"
18749                "    {56, 23,    /* a comment */ \"hello\"},\n"
18750                "    {-1, 93463, \"world\"                },\n"
18751                "    {7,  5,     \"!!\"                   }\n"
18752                "};\n",
18753                Style);
18754 
18755   verifyFormat("test demo[] = {\n"
18756                "    {56, 23,    \"hello\"                   },\n"
18757                "    {-1, 93463, \"world\" /* comment here */},\n"
18758                "    {7,  5,     \"!!\"                      }\n"
18759                "};\n",
18760                Style);
18761 
18762   verifyFormat("test demo[] = {\n"
18763                "    {56, /* a comment */ 23, \"hello\"},\n"
18764                "    {-1, 93463,              \"world\"},\n"
18765                "    {7,  5,                  \"!!\"   }\n"
18766                "};\n",
18767                Style);
18768 
18769   Style.ColumnLimit = 20;
18770   EXPECT_EQ(
18771       "demo = std::array<\n"
18772       "    struct test, 3>{\n"
18773       "    test{\n"
18774       "         56, 23,\n"
18775       "         \"hello \"\n"
18776       "         \"world i \"\n"
18777       "         \"am a very \"\n"
18778       "         \"long line \"\n"
18779       "         \"that \"\n"
18780       "         \"really, \"\n"
18781       "         \"in any \"\n"
18782       "         \"just \"\n"
18783       "         \"world, \"\n"
18784       "         \"ought to \"\n"
18785       "         \"be split \"\n"
18786       "         \"over \"\n"
18787       "         \"multiple \"\n"
18788       "         \"lines\"},\n"
18789       "    test{-1, 93463,\n"
18790       "         \"world\"},\n"
18791       "    test{7,  5,\n"
18792       "         \"!!\"   },\n"
18793       "};",
18794       format("demo = std::array<struct test, 3>{test{56, 23, \"hello world "
18795              "i am a very long line that really, in any just world, ought "
18796              "to be split over multiple lines\"},test{-1, 93463, \"world\"},"
18797              "test{7, 5, \"!!\"},};",
18798              Style));
18799 
18800   // This caused a core dump by enabling Alignment in the LLVMStyle globally
18801   Style = getLLVMStyleWithColumns(50);
18802   Style.AlignArrayOfStructures = FormatStyle::AIAS_Left;
18803   verifyFormat("static A x = {\n"
18804                "    {{init1, init2, init3, init4},\n"
18805                "     {init1, init2, init3, init4}}\n"
18806                "};",
18807                Style);
18808   Style.ColumnLimit = 100;
18809   EXPECT_EQ(
18810       "test demo[] = {\n"
18811       "    {56, 23,\n"
18812       "     \"hello world i am a very long line that really, in any just world"
18813       ", ought to be split over \"\n"
18814       "     \"multiple lines\"  },\n"
18815       "    {-1, 93463, \"world\"},\n"
18816       "    {7,  5,     \"!!\"   },\n"
18817       "};",
18818       format("test demo[] = {{56, 23, \"hello world i am a very long line "
18819              "that really, in any just world, ought to be split over multiple "
18820              "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
18821              Style));
18822 }
18823 
18824 TEST_F(FormatTest, UnderstandsPragmas) {
18825   verifyFormat("#pragma omp reduction(| : var)");
18826   verifyFormat("#pragma omp reduction(+ : var)");
18827 
18828   EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string "
18829             "(including parentheses).",
18830             format("#pragma    mark   Any non-hyphenated or hyphenated string "
18831                    "(including parentheses)."));
18832 }
18833 
18834 TEST_F(FormatTest, UnderstandPragmaOption) {
18835   verifyFormat("#pragma option -C -A");
18836 
18837   EXPECT_EQ("#pragma option -C -A", format("#pragma    option   -C   -A"));
18838 }
18839 
18840 TEST_F(FormatTest, OptimizeBreakPenaltyVsExcess) {
18841   FormatStyle Style = getLLVMStyleWithColumns(20);
18842 
18843   // See PR41213
18844   EXPECT_EQ("/*\n"
18845             " *\t9012345\n"
18846             " * /8901\n"
18847             " */",
18848             format("/*\n"
18849                    " *\t9012345 /8901\n"
18850                    " */",
18851                    Style));
18852   EXPECT_EQ("/*\n"
18853             " *345678\n"
18854             " *\t/8901\n"
18855             " */",
18856             format("/*\n"
18857                    " *345678\t/8901\n"
18858                    " */",
18859                    Style));
18860 
18861   verifyFormat("int a; // the\n"
18862                "       // comment",
18863                Style);
18864   EXPECT_EQ("int a; /* first line\n"
18865             "        * second\n"
18866             "        * line third\n"
18867             "        * line\n"
18868             "        */",
18869             format("int a; /* first line\n"
18870                    "        * second\n"
18871                    "        * line third\n"
18872                    "        * line\n"
18873                    "        */",
18874                    Style));
18875   EXPECT_EQ("int a; // first line\n"
18876             "       // second\n"
18877             "       // line third\n"
18878             "       // line",
18879             format("int a; // first line\n"
18880                    "       // second line\n"
18881                    "       // third line",
18882                    Style));
18883 
18884   Style.PenaltyExcessCharacter = 90;
18885   verifyFormat("int a; // the comment", Style);
18886   EXPECT_EQ("int a; // the comment\n"
18887             "       // aaa",
18888             format("int a; // the comment aaa", Style));
18889   EXPECT_EQ("int a; /* first line\n"
18890             "        * second line\n"
18891             "        * third line\n"
18892             "        */",
18893             format("int a; /* first line\n"
18894                    "        * second line\n"
18895                    "        * third line\n"
18896                    "        */",
18897                    Style));
18898   EXPECT_EQ("int a; // first line\n"
18899             "       // second line\n"
18900             "       // third line",
18901             format("int a; // first line\n"
18902                    "       // second line\n"
18903                    "       // third line",
18904                    Style));
18905   // FIXME: Investigate why this is not getting the same layout as the test
18906   // above.
18907   EXPECT_EQ("int a; /* first line\n"
18908             "        * second line\n"
18909             "        * third line\n"
18910             "        */",
18911             format("int a; /* first line second line third line"
18912                    "\n*/",
18913                    Style));
18914 
18915   EXPECT_EQ("// foo bar baz bazfoo\n"
18916             "// foo bar foo bar\n",
18917             format("// foo bar baz bazfoo\n"
18918                    "// foo bar foo           bar\n",
18919                    Style));
18920   EXPECT_EQ("// foo bar baz bazfoo\n"
18921             "// foo bar foo bar\n",
18922             format("// foo bar baz      bazfoo\n"
18923                    "// foo            bar foo bar\n",
18924                    Style));
18925 
18926   // FIXME: Optimally, we'd keep bazfoo on the first line and reflow bar to the
18927   // next one.
18928   EXPECT_EQ("// foo bar baz bazfoo\n"
18929             "// bar foo bar\n",
18930             format("// foo bar baz      bazfoo bar\n"
18931                    "// foo            bar\n",
18932                    Style));
18933 
18934   EXPECT_EQ("// foo bar baz bazfoo\n"
18935             "// foo bar baz bazfoo\n"
18936             "// bar foo bar\n",
18937             format("// foo bar baz      bazfoo\n"
18938                    "// foo bar baz      bazfoo bar\n"
18939                    "// foo bar\n",
18940                    Style));
18941 
18942   EXPECT_EQ("// foo bar baz bazfoo\n"
18943             "// foo bar baz bazfoo\n"
18944             "// bar foo bar\n",
18945             format("// foo bar baz      bazfoo\n"
18946                    "// foo bar baz      bazfoo bar\n"
18947                    "// foo           bar\n",
18948                    Style));
18949 
18950   // Make sure we do not keep protruding characters if strict mode reflow is
18951   // cheaper than keeping protruding characters.
18952   Style.ColumnLimit = 21;
18953   EXPECT_EQ(
18954       "// foo foo foo foo\n"
18955       "// foo foo foo foo\n"
18956       "// foo foo foo foo\n",
18957       format("// foo foo foo foo foo foo foo foo foo foo foo foo\n", Style));
18958 
18959   EXPECT_EQ("int a = /* long block\n"
18960             "           comment */\n"
18961             "    42;",
18962             format("int a = /* long block comment */ 42;", Style));
18963 }
18964 
18965 TEST_F(FormatTest, BreakPenaltyAfterLParen) {
18966   FormatStyle Style = getLLVMStyle();
18967   Style.ColumnLimit = 8;
18968   Style.PenaltyExcessCharacter = 15;
18969   verifyFormat("int foo(\n"
18970                "    int aaaaaaaaaaaaaaaaaaaaaaaa);",
18971                Style);
18972   Style.PenaltyBreakOpenParenthesis = 200;
18973   EXPECT_EQ("int foo(int aaaaaaaaaaaaaaaaaaaaaaaa);",
18974             format("int foo(\n"
18975                    "    int aaaaaaaaaaaaaaaaaaaaaaaa);",
18976                    Style));
18977 }
18978 
18979 TEST_F(FormatTest, BreakPenaltyAfterCastLParen) {
18980   FormatStyle Style = getLLVMStyle();
18981   Style.ColumnLimit = 5;
18982   Style.PenaltyExcessCharacter = 150;
18983   verifyFormat("foo((\n"
18984                "    int)aaaaaaaaaaaaaaaaaaaaaaaa);",
18985 
18986                Style);
18987   Style.PenaltyBreakOpenParenthesis = 100000;
18988   EXPECT_EQ("foo((int)\n"
18989             "        aaaaaaaaaaaaaaaaaaaaaaaa);",
18990             format("foo((\n"
18991                    "int)aaaaaaaaaaaaaaaaaaaaaaaa);",
18992                    Style));
18993 }
18994 
18995 TEST_F(FormatTest, BreakPenaltyAfterForLoopLParen) {
18996   FormatStyle Style = getLLVMStyle();
18997   Style.ColumnLimit = 4;
18998   Style.PenaltyExcessCharacter = 100;
18999   verifyFormat("for (\n"
19000                "    int iiiiiiiiiiiiiiiii =\n"
19001                "        0;\n"
19002                "    iiiiiiiiiiiiiiiii <\n"
19003                "    2;\n"
19004                "    iiiiiiiiiiiiiiiii++) {\n"
19005                "}",
19006 
19007                Style);
19008   Style.PenaltyBreakOpenParenthesis = 1250;
19009   EXPECT_EQ("for (int iiiiiiiiiiiiiiiii =\n"
19010             "         0;\n"
19011             "     iiiiiiiiiiiiiiiii <\n"
19012             "     2;\n"
19013             "     iiiiiiiiiiiiiiiii++) {\n"
19014             "}",
19015             format("for (\n"
19016                    "    int iiiiiiiiiiiiiiiii =\n"
19017                    "        0;\n"
19018                    "    iiiiiiiiiiiiiiiii <\n"
19019                    "    2;\n"
19020                    "    iiiiiiiiiiiiiiiii++) {\n"
19021                    "}",
19022                    Style));
19023 }
19024 
19025 #define EXPECT_ALL_STYLES_EQUAL(Styles)                                        \
19026   for (size_t i = 1; i < Styles.size(); ++i)                                   \
19027   EXPECT_EQ(Styles[0], Styles[i])                                              \
19028       << "Style #" << i << " of " << Styles.size() << " differs from Style #0"
19029 
19030 TEST_F(FormatTest, GetsPredefinedStyleByName) {
19031   SmallVector<FormatStyle, 3> Styles;
19032   Styles.resize(3);
19033 
19034   Styles[0] = getLLVMStyle();
19035   EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1]));
19036   EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2]));
19037   EXPECT_ALL_STYLES_EQUAL(Styles);
19038 
19039   Styles[0] = getGoogleStyle();
19040   EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1]));
19041   EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2]));
19042   EXPECT_ALL_STYLES_EQUAL(Styles);
19043 
19044   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
19045   EXPECT_TRUE(
19046       getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1]));
19047   EXPECT_TRUE(
19048       getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2]));
19049   EXPECT_ALL_STYLES_EQUAL(Styles);
19050 
19051   Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp);
19052   EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1]));
19053   EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2]));
19054   EXPECT_ALL_STYLES_EQUAL(Styles);
19055 
19056   Styles[0] = getMozillaStyle();
19057   EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1]));
19058   EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2]));
19059   EXPECT_ALL_STYLES_EQUAL(Styles);
19060 
19061   Styles[0] = getWebKitStyle();
19062   EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1]));
19063   EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2]));
19064   EXPECT_ALL_STYLES_EQUAL(Styles);
19065 
19066   Styles[0] = getGNUStyle();
19067   EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1]));
19068   EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2]));
19069   EXPECT_ALL_STYLES_EQUAL(Styles);
19070 
19071   EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0]));
19072 }
19073 
19074 TEST_F(FormatTest, GetsCorrectBasedOnStyle) {
19075   SmallVector<FormatStyle, 8> Styles;
19076   Styles.resize(2);
19077 
19078   Styles[0] = getGoogleStyle();
19079   Styles[1] = getLLVMStyle();
19080   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
19081   EXPECT_ALL_STYLES_EQUAL(Styles);
19082 
19083   Styles.resize(5);
19084   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
19085   Styles[1] = getLLVMStyle();
19086   Styles[1].Language = FormatStyle::LK_JavaScript;
19087   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
19088 
19089   Styles[2] = getLLVMStyle();
19090   Styles[2].Language = FormatStyle::LK_JavaScript;
19091   EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n"
19092                                   "BasedOnStyle: Google",
19093                                   &Styles[2])
19094                    .value());
19095 
19096   Styles[3] = getLLVMStyle();
19097   Styles[3].Language = FormatStyle::LK_JavaScript;
19098   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n"
19099                                   "Language: JavaScript",
19100                                   &Styles[3])
19101                    .value());
19102 
19103   Styles[4] = getLLVMStyle();
19104   Styles[4].Language = FormatStyle::LK_JavaScript;
19105   EXPECT_EQ(0, parseConfiguration("---\n"
19106                                   "BasedOnStyle: LLVM\n"
19107                                   "IndentWidth: 123\n"
19108                                   "---\n"
19109                                   "BasedOnStyle: Google\n"
19110                                   "Language: JavaScript",
19111                                   &Styles[4])
19112                    .value());
19113   EXPECT_ALL_STYLES_EQUAL(Styles);
19114 }
19115 
19116 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME)                             \
19117   Style.FIELD = false;                                                         \
19118   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value());      \
19119   EXPECT_TRUE(Style.FIELD);                                                    \
19120   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value());     \
19121   EXPECT_FALSE(Style.FIELD);
19122 
19123 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD)
19124 
19125 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME)              \
19126   Style.STRUCT.FIELD = false;                                                  \
19127   EXPECT_EQ(0,                                                                 \
19128             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": true", &Style)   \
19129                 .value());                                                     \
19130   EXPECT_TRUE(Style.STRUCT.FIELD);                                             \
19131   EXPECT_EQ(0,                                                                 \
19132             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": false", &Style)  \
19133                 .value());                                                     \
19134   EXPECT_FALSE(Style.STRUCT.FIELD);
19135 
19136 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD)                                 \
19137   CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD)
19138 
19139 #define CHECK_PARSE(TEXT, FIELD, VALUE)                                        \
19140   EXPECT_NE(VALUE, Style.FIELD) << "Initial value already the same!";          \
19141   EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value());                      \
19142   EXPECT_EQ(VALUE, Style.FIELD) << "Unexpected value after parsing!"
19143 
19144 TEST_F(FormatTest, ParsesConfigurationBools) {
19145   FormatStyle Style = {};
19146   Style.Language = FormatStyle::LK_Cpp;
19147   CHECK_PARSE_BOOL(AlignTrailingComments);
19148   CHECK_PARSE_BOOL(AllowAllArgumentsOnNextLine);
19149   CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
19150   CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine);
19151   CHECK_PARSE_BOOL(AllowShortEnumsOnASingleLine);
19152   CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
19153   CHECK_PARSE_BOOL(BinPackArguments);
19154   CHECK_PARSE_BOOL(BinPackParameters);
19155   CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations);
19156   CHECK_PARSE_BOOL(BreakBeforeConceptDeclarations);
19157   CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
19158   CHECK_PARSE_BOOL(BreakStringLiterals);
19159   CHECK_PARSE_BOOL(CompactNamespaces);
19160   CHECK_PARSE_BOOL(DeriveLineEnding);
19161   CHECK_PARSE_BOOL(DerivePointerAlignment);
19162   CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding");
19163   CHECK_PARSE_BOOL(DisableFormat);
19164   CHECK_PARSE_BOOL(IndentAccessModifiers);
19165   CHECK_PARSE_BOOL(IndentCaseLabels);
19166   CHECK_PARSE_BOOL(IndentCaseBlocks);
19167   CHECK_PARSE_BOOL(IndentGotoLabels);
19168   CHECK_PARSE_BOOL(IndentRequires);
19169   CHECK_PARSE_BOOL(IndentWrappedFunctionNames);
19170   CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks);
19171   CHECK_PARSE_BOOL(ObjCSpaceAfterProperty);
19172   CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
19173   CHECK_PARSE_BOOL(Cpp11BracedListStyle);
19174   CHECK_PARSE_BOOL(ReflowComments);
19175   CHECK_PARSE_BOOL(RemoveBracesLLVM);
19176   CHECK_PARSE_BOOL(SortUsingDeclarations);
19177   CHECK_PARSE_BOOL(SpacesInParentheses);
19178   CHECK_PARSE_BOOL(SpacesInSquareBrackets);
19179   CHECK_PARSE_BOOL(SpacesInConditionalStatement);
19180   CHECK_PARSE_BOOL(SpaceInEmptyBlock);
19181   CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
19182   CHECK_PARSE_BOOL(SpacesInContainerLiterals);
19183   CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
19184   CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
19185   CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword);
19186   CHECK_PARSE_BOOL(SpaceAfterLogicalNot);
19187   CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
19188   CHECK_PARSE_BOOL(SpaceBeforeCaseColon);
19189   CHECK_PARSE_BOOL(SpaceBeforeCpp11BracedList);
19190   CHECK_PARSE_BOOL(SpaceBeforeCtorInitializerColon);
19191   CHECK_PARSE_BOOL(SpaceBeforeInheritanceColon);
19192   CHECK_PARSE_BOOL(SpaceBeforeRangeBasedForLoopColon);
19193   CHECK_PARSE_BOOL(SpaceBeforeSquareBrackets);
19194   CHECK_PARSE_BOOL(UseCRLF);
19195 
19196   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterCaseLabel);
19197   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass);
19198   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum);
19199   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction);
19200   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace);
19201   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration);
19202   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct);
19203   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion);
19204   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterExternBlock);
19205   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch);
19206   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse);
19207   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeLambdaBody);
19208   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeWhile);
19209   CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces);
19210   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyFunction);
19211   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyRecord);
19212   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyNamespace);
19213   CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterControlStatements);
19214   CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterForeachMacros);
19215   CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions,
19216                           AfterFunctionDeclarationName);
19217   CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions,
19218                           AfterFunctionDefinitionName);
19219   CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterIfMacros);
19220   CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterOverloadedOperator);
19221   CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, BeforeNonEmptyParentheses);
19222 }
19223 
19224 #undef CHECK_PARSE_BOOL
19225 
19226 TEST_F(FormatTest, ParsesConfiguration) {
19227   FormatStyle Style = {};
19228   Style.Language = FormatStyle::LK_Cpp;
19229   CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
19230   CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",
19231               ConstructorInitializerIndentWidth, 1234u);
19232   CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u);
19233   CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
19234   CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
19235   CHECK_PARSE("PenaltyBreakAssignment: 1234", PenaltyBreakAssignment, 1234u);
19236   CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234",
19237               PenaltyBreakBeforeFirstCallParameter, 1234u);
19238   CHECK_PARSE("PenaltyBreakTemplateDeclaration: 1234",
19239               PenaltyBreakTemplateDeclaration, 1234u);
19240   CHECK_PARSE("PenaltyBreakOpenParenthesis: 1234", PenaltyBreakOpenParenthesis,
19241               1234u);
19242   CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
19243   CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
19244               PenaltyReturnTypeOnItsOwnLine, 1234u);
19245   CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
19246               SpacesBeforeTrailingComments, 1234u);
19247   CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
19248   CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u);
19249   CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$");
19250 
19251   Style.QualifierAlignment = FormatStyle::QAS_Right;
19252   CHECK_PARSE("QualifierAlignment: Leave", QualifierAlignment,
19253               FormatStyle::QAS_Leave);
19254   CHECK_PARSE("QualifierAlignment: Right", QualifierAlignment,
19255               FormatStyle::QAS_Right);
19256   CHECK_PARSE("QualifierAlignment: Left", QualifierAlignment,
19257               FormatStyle::QAS_Left);
19258   CHECK_PARSE("QualifierAlignment: Custom", QualifierAlignment,
19259               FormatStyle::QAS_Custom);
19260 
19261   Style.QualifierOrder.clear();
19262   CHECK_PARSE("QualifierOrder: [ const, volatile, type ]", QualifierOrder,
19263               std::vector<std::string>({"const", "volatile", "type"}));
19264   Style.QualifierOrder.clear();
19265   CHECK_PARSE("QualifierOrder: [const, type]", QualifierOrder,
19266               std::vector<std::string>({"const", "type"}));
19267   Style.QualifierOrder.clear();
19268   CHECK_PARSE("QualifierOrder: [volatile, type]", QualifierOrder,
19269               std::vector<std::string>({"volatile", "type"}));
19270 
19271   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
19272   CHECK_PARSE("AlignConsecutiveAssignments: None", AlignConsecutiveAssignments,
19273               FormatStyle::ACS_None);
19274   CHECK_PARSE("AlignConsecutiveAssignments: Consecutive",
19275               AlignConsecutiveAssignments, FormatStyle::ACS_Consecutive);
19276   CHECK_PARSE("AlignConsecutiveAssignments: AcrossEmptyLines",
19277               AlignConsecutiveAssignments, FormatStyle::ACS_AcrossEmptyLines);
19278   CHECK_PARSE("AlignConsecutiveAssignments: AcrossEmptyLinesAndComments",
19279               AlignConsecutiveAssignments,
19280               FormatStyle::ACS_AcrossEmptyLinesAndComments);
19281   // For backwards compability, false / true should still parse
19282   CHECK_PARSE("AlignConsecutiveAssignments: false", AlignConsecutiveAssignments,
19283               FormatStyle::ACS_None);
19284   CHECK_PARSE("AlignConsecutiveAssignments: true", AlignConsecutiveAssignments,
19285               FormatStyle::ACS_Consecutive);
19286 
19287   Style.AlignConsecutiveBitFields = FormatStyle::ACS_Consecutive;
19288   CHECK_PARSE("AlignConsecutiveBitFields: None", AlignConsecutiveBitFields,
19289               FormatStyle::ACS_None);
19290   CHECK_PARSE("AlignConsecutiveBitFields: Consecutive",
19291               AlignConsecutiveBitFields, FormatStyle::ACS_Consecutive);
19292   CHECK_PARSE("AlignConsecutiveBitFields: AcrossEmptyLines",
19293               AlignConsecutiveBitFields, FormatStyle::ACS_AcrossEmptyLines);
19294   CHECK_PARSE("AlignConsecutiveBitFields: AcrossEmptyLinesAndComments",
19295               AlignConsecutiveBitFields,
19296               FormatStyle::ACS_AcrossEmptyLinesAndComments);
19297   // For backwards compability, false / true should still parse
19298   CHECK_PARSE("AlignConsecutiveBitFields: false", AlignConsecutiveBitFields,
19299               FormatStyle::ACS_None);
19300   CHECK_PARSE("AlignConsecutiveBitFields: true", AlignConsecutiveBitFields,
19301               FormatStyle::ACS_Consecutive);
19302 
19303   Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
19304   CHECK_PARSE("AlignConsecutiveMacros: None", AlignConsecutiveMacros,
19305               FormatStyle::ACS_None);
19306   CHECK_PARSE("AlignConsecutiveMacros: Consecutive", AlignConsecutiveMacros,
19307               FormatStyle::ACS_Consecutive);
19308   CHECK_PARSE("AlignConsecutiveMacros: AcrossEmptyLines",
19309               AlignConsecutiveMacros, FormatStyle::ACS_AcrossEmptyLines);
19310   CHECK_PARSE("AlignConsecutiveMacros: AcrossEmptyLinesAndComments",
19311               AlignConsecutiveMacros,
19312               FormatStyle::ACS_AcrossEmptyLinesAndComments);
19313   // For backwards compability, false / true should still parse
19314   CHECK_PARSE("AlignConsecutiveMacros: false", AlignConsecutiveMacros,
19315               FormatStyle::ACS_None);
19316   CHECK_PARSE("AlignConsecutiveMacros: true", AlignConsecutiveMacros,
19317               FormatStyle::ACS_Consecutive);
19318 
19319   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
19320   CHECK_PARSE("AlignConsecutiveDeclarations: None",
19321               AlignConsecutiveDeclarations, FormatStyle::ACS_None);
19322   CHECK_PARSE("AlignConsecutiveDeclarations: Consecutive",
19323               AlignConsecutiveDeclarations, FormatStyle::ACS_Consecutive);
19324   CHECK_PARSE("AlignConsecutiveDeclarations: AcrossEmptyLines",
19325               AlignConsecutiveDeclarations, FormatStyle::ACS_AcrossEmptyLines);
19326   CHECK_PARSE("AlignConsecutiveDeclarations: AcrossEmptyLinesAndComments",
19327               AlignConsecutiveDeclarations,
19328               FormatStyle::ACS_AcrossEmptyLinesAndComments);
19329   // For backwards compability, false / true should still parse
19330   CHECK_PARSE("AlignConsecutiveDeclarations: false",
19331               AlignConsecutiveDeclarations, FormatStyle::ACS_None);
19332   CHECK_PARSE("AlignConsecutiveDeclarations: true",
19333               AlignConsecutiveDeclarations, FormatStyle::ACS_Consecutive);
19334 
19335   Style.PointerAlignment = FormatStyle::PAS_Middle;
19336   CHECK_PARSE("PointerAlignment: Left", PointerAlignment,
19337               FormatStyle::PAS_Left);
19338   CHECK_PARSE("PointerAlignment: Right", PointerAlignment,
19339               FormatStyle::PAS_Right);
19340   CHECK_PARSE("PointerAlignment: Middle", PointerAlignment,
19341               FormatStyle::PAS_Middle);
19342   Style.ReferenceAlignment = FormatStyle::RAS_Middle;
19343   CHECK_PARSE("ReferenceAlignment: Pointer", ReferenceAlignment,
19344               FormatStyle::RAS_Pointer);
19345   CHECK_PARSE("ReferenceAlignment: Left", ReferenceAlignment,
19346               FormatStyle::RAS_Left);
19347   CHECK_PARSE("ReferenceAlignment: Right", ReferenceAlignment,
19348               FormatStyle::RAS_Right);
19349   CHECK_PARSE("ReferenceAlignment: Middle", ReferenceAlignment,
19350               FormatStyle::RAS_Middle);
19351   // For backward compatibility:
19352   CHECK_PARSE("PointerBindsToType: Left", PointerAlignment,
19353               FormatStyle::PAS_Left);
19354   CHECK_PARSE("PointerBindsToType: Right", PointerAlignment,
19355               FormatStyle::PAS_Right);
19356   CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment,
19357               FormatStyle::PAS_Middle);
19358 
19359   Style.Standard = FormatStyle::LS_Auto;
19360   CHECK_PARSE("Standard: c++03", Standard, FormatStyle::LS_Cpp03);
19361   CHECK_PARSE("Standard: c++11", Standard, FormatStyle::LS_Cpp11);
19362   CHECK_PARSE("Standard: c++14", Standard, FormatStyle::LS_Cpp14);
19363   CHECK_PARSE("Standard: c++17", Standard, FormatStyle::LS_Cpp17);
19364   CHECK_PARSE("Standard: c++20", Standard, FormatStyle::LS_Cpp20);
19365   CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
19366   CHECK_PARSE("Standard: Latest", Standard, FormatStyle::LS_Latest);
19367   // Legacy aliases:
19368   CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03);
19369   CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Latest);
19370   CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
19371   CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
19372 
19373   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
19374   CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment",
19375               BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment);
19376   CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators,
19377               FormatStyle::BOS_None);
19378   CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators,
19379               FormatStyle::BOS_All);
19380   // For backward compatibility:
19381   CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators,
19382               FormatStyle::BOS_None);
19383   CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators,
19384               FormatStyle::BOS_All);
19385 
19386   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
19387   CHECK_PARSE("BreakConstructorInitializers: BeforeComma",
19388               BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma);
19389   CHECK_PARSE("BreakConstructorInitializers: AfterColon",
19390               BreakConstructorInitializers, FormatStyle::BCIS_AfterColon);
19391   CHECK_PARSE("BreakConstructorInitializers: BeforeColon",
19392               BreakConstructorInitializers, FormatStyle::BCIS_BeforeColon);
19393   // For backward compatibility:
19394   CHECK_PARSE("BreakConstructorInitializersBeforeComma: true",
19395               BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma);
19396 
19397   Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
19398   CHECK_PARSE("BreakInheritanceList: AfterComma", BreakInheritanceList,
19399               FormatStyle::BILS_AfterComma);
19400   CHECK_PARSE("BreakInheritanceList: BeforeComma", BreakInheritanceList,
19401               FormatStyle::BILS_BeforeComma);
19402   CHECK_PARSE("BreakInheritanceList: AfterColon", BreakInheritanceList,
19403               FormatStyle::BILS_AfterColon);
19404   CHECK_PARSE("BreakInheritanceList: BeforeColon", BreakInheritanceList,
19405               FormatStyle::BILS_BeforeColon);
19406   // For backward compatibility:
19407   CHECK_PARSE("BreakBeforeInheritanceComma: true", BreakInheritanceList,
19408               FormatStyle::BILS_BeforeComma);
19409 
19410   Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack;
19411   CHECK_PARSE("PackConstructorInitializers: Never", PackConstructorInitializers,
19412               FormatStyle::PCIS_Never);
19413   CHECK_PARSE("PackConstructorInitializers: BinPack",
19414               PackConstructorInitializers, FormatStyle::PCIS_BinPack);
19415   CHECK_PARSE("PackConstructorInitializers: CurrentLine",
19416               PackConstructorInitializers, FormatStyle::PCIS_CurrentLine);
19417   CHECK_PARSE("PackConstructorInitializers: NextLine",
19418               PackConstructorInitializers, FormatStyle::PCIS_NextLine);
19419   // For backward compatibility:
19420   CHECK_PARSE("BasedOnStyle: Google\n"
19421               "ConstructorInitializerAllOnOneLineOrOnePerLine: true\n"
19422               "AllowAllConstructorInitializersOnNextLine: false",
19423               PackConstructorInitializers, FormatStyle::PCIS_CurrentLine);
19424   Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
19425   CHECK_PARSE("BasedOnStyle: Google\n"
19426               "ConstructorInitializerAllOnOneLineOrOnePerLine: false",
19427               PackConstructorInitializers, FormatStyle::PCIS_BinPack);
19428   CHECK_PARSE("ConstructorInitializerAllOnOneLineOrOnePerLine: true\n"
19429               "AllowAllConstructorInitializersOnNextLine: true",
19430               PackConstructorInitializers, FormatStyle::PCIS_NextLine);
19431   Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack;
19432   CHECK_PARSE("ConstructorInitializerAllOnOneLineOrOnePerLine: true\n"
19433               "AllowAllConstructorInitializersOnNextLine: false",
19434               PackConstructorInitializers, FormatStyle::PCIS_CurrentLine);
19435 
19436   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
19437   CHECK_PARSE("EmptyLineBeforeAccessModifier: Never",
19438               EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Never);
19439   CHECK_PARSE("EmptyLineBeforeAccessModifier: Leave",
19440               EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Leave);
19441   CHECK_PARSE("EmptyLineBeforeAccessModifier: LogicalBlock",
19442               EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_LogicalBlock);
19443   CHECK_PARSE("EmptyLineBeforeAccessModifier: Always",
19444               EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Always);
19445 
19446   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
19447   CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket,
19448               FormatStyle::BAS_Align);
19449   CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket,
19450               FormatStyle::BAS_DontAlign);
19451   CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket,
19452               FormatStyle::BAS_AlwaysBreak);
19453   CHECK_PARSE("AlignAfterOpenBracket: BlockIndent", AlignAfterOpenBracket,
19454               FormatStyle::BAS_BlockIndent);
19455   // For backward compatibility:
19456   CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket,
19457               FormatStyle::BAS_DontAlign);
19458   CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket,
19459               FormatStyle::BAS_Align);
19460 
19461   Style.AlignEscapedNewlines = FormatStyle::ENAS_Left;
19462   CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines,
19463               FormatStyle::ENAS_DontAlign);
19464   CHECK_PARSE("AlignEscapedNewlines: Left", AlignEscapedNewlines,
19465               FormatStyle::ENAS_Left);
19466   CHECK_PARSE("AlignEscapedNewlines: Right", AlignEscapedNewlines,
19467               FormatStyle::ENAS_Right);
19468   // For backward compatibility:
19469   CHECK_PARSE("AlignEscapedNewlinesLeft: true", AlignEscapedNewlines,
19470               FormatStyle::ENAS_Left);
19471   CHECK_PARSE("AlignEscapedNewlinesLeft: false", AlignEscapedNewlines,
19472               FormatStyle::ENAS_Right);
19473 
19474   Style.AlignOperands = FormatStyle::OAS_Align;
19475   CHECK_PARSE("AlignOperands: DontAlign", AlignOperands,
19476               FormatStyle::OAS_DontAlign);
19477   CHECK_PARSE("AlignOperands: Align", AlignOperands, FormatStyle::OAS_Align);
19478   CHECK_PARSE("AlignOperands: AlignAfterOperator", AlignOperands,
19479               FormatStyle::OAS_AlignAfterOperator);
19480   // For backward compatibility:
19481   CHECK_PARSE("AlignOperands: false", AlignOperands,
19482               FormatStyle::OAS_DontAlign);
19483   CHECK_PARSE("AlignOperands: true", AlignOperands, FormatStyle::OAS_Align);
19484 
19485   Style.UseTab = FormatStyle::UT_ForIndentation;
19486   CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
19487   CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
19488   CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
19489   CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab,
19490               FormatStyle::UT_ForContinuationAndIndentation);
19491   CHECK_PARSE("UseTab: AlignWithSpaces", UseTab,
19492               FormatStyle::UT_AlignWithSpaces);
19493   // For backward compatibility:
19494   CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never);
19495   CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always);
19496 
19497   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty;
19498   CHECK_PARSE("AllowShortBlocksOnASingleLine: Never",
19499               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never);
19500   CHECK_PARSE("AllowShortBlocksOnASingleLine: Empty",
19501               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Empty);
19502   CHECK_PARSE("AllowShortBlocksOnASingleLine: Always",
19503               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always);
19504   // For backward compatibility:
19505   CHECK_PARSE("AllowShortBlocksOnASingleLine: false",
19506               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never);
19507   CHECK_PARSE("AllowShortBlocksOnASingleLine: true",
19508               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always);
19509 
19510   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
19511   CHECK_PARSE("AllowShortFunctionsOnASingleLine: None",
19512               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
19513   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline",
19514               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline);
19515   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty",
19516               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty);
19517   CHECK_PARSE("AllowShortFunctionsOnASingleLine: All",
19518               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
19519   // For backward compatibility:
19520   CHECK_PARSE("AllowShortFunctionsOnASingleLine: false",
19521               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
19522   CHECK_PARSE("AllowShortFunctionsOnASingleLine: true",
19523               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
19524 
19525   Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Both;
19526   CHECK_PARSE("SpaceAroundPointerQualifiers: Default",
19527               SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Default);
19528   CHECK_PARSE("SpaceAroundPointerQualifiers: Before",
19529               SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Before);
19530   CHECK_PARSE("SpaceAroundPointerQualifiers: After",
19531               SpaceAroundPointerQualifiers, FormatStyle::SAPQ_After);
19532   CHECK_PARSE("SpaceAroundPointerQualifiers: Both",
19533               SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Both);
19534 
19535   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
19536   CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens,
19537               FormatStyle::SBPO_Never);
19538   CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens,
19539               FormatStyle::SBPO_Always);
19540   CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens,
19541               FormatStyle::SBPO_ControlStatements);
19542   CHECK_PARSE("SpaceBeforeParens: ControlStatementsExceptControlMacros",
19543               SpaceBeforeParens,
19544               FormatStyle::SBPO_ControlStatementsExceptControlMacros);
19545   CHECK_PARSE("SpaceBeforeParens: NonEmptyParentheses", SpaceBeforeParens,
19546               FormatStyle::SBPO_NonEmptyParentheses);
19547   CHECK_PARSE("SpaceBeforeParens: Custom", SpaceBeforeParens,
19548               FormatStyle::SBPO_Custom);
19549   // For backward compatibility:
19550   CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens,
19551               FormatStyle::SBPO_Never);
19552   CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens,
19553               FormatStyle::SBPO_ControlStatements);
19554   CHECK_PARSE("SpaceBeforeParens: ControlStatementsExceptForEachMacros",
19555               SpaceBeforeParens,
19556               FormatStyle::SBPO_ControlStatementsExceptControlMacros);
19557 
19558   Style.ColumnLimit = 123;
19559   FormatStyle BaseStyle = getLLVMStyle();
19560   CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
19561   CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
19562 
19563   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
19564   CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
19565               FormatStyle::BS_Attach);
19566   CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
19567               FormatStyle::BS_Linux);
19568   CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces,
19569               FormatStyle::BS_Mozilla);
19570   CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
19571               FormatStyle::BS_Stroustrup);
19572   CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces,
19573               FormatStyle::BS_Allman);
19574   CHECK_PARSE("BreakBeforeBraces: Whitesmiths", BreakBeforeBraces,
19575               FormatStyle::BS_Whitesmiths);
19576   CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU);
19577   CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces,
19578               FormatStyle::BS_WebKit);
19579   CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces,
19580               FormatStyle::BS_Custom);
19581 
19582   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never;
19583   CHECK_PARSE("BraceWrapping:\n"
19584               "  AfterControlStatement: MultiLine",
19585               BraceWrapping.AfterControlStatement,
19586               FormatStyle::BWACS_MultiLine);
19587   CHECK_PARSE("BraceWrapping:\n"
19588               "  AfterControlStatement: Always",
19589               BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always);
19590   CHECK_PARSE("BraceWrapping:\n"
19591               "  AfterControlStatement: Never",
19592               BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never);
19593   // For backward compatibility:
19594   CHECK_PARSE("BraceWrapping:\n"
19595               "  AfterControlStatement: true",
19596               BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always);
19597   CHECK_PARSE("BraceWrapping:\n"
19598               "  AfterControlStatement: false",
19599               BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never);
19600 
19601   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
19602   CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType,
19603               FormatStyle::RTBS_None);
19604   CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType,
19605               FormatStyle::RTBS_All);
19606   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel",
19607               AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel);
19608   CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions",
19609               AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions);
19610   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions",
19611               AlwaysBreakAfterReturnType,
19612               FormatStyle::RTBS_TopLevelDefinitions);
19613 
19614   Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
19615   CHECK_PARSE("AlwaysBreakTemplateDeclarations: No",
19616               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_No);
19617   CHECK_PARSE("AlwaysBreakTemplateDeclarations: MultiLine",
19618               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine);
19619   CHECK_PARSE("AlwaysBreakTemplateDeclarations: Yes",
19620               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes);
19621   CHECK_PARSE("AlwaysBreakTemplateDeclarations: false",
19622               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine);
19623   CHECK_PARSE("AlwaysBreakTemplateDeclarations: true",
19624               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes);
19625 
19626   Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
19627   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None",
19628               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None);
19629   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All",
19630               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All);
19631   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel",
19632               AlwaysBreakAfterDefinitionReturnType,
19633               FormatStyle::DRTBS_TopLevel);
19634 
19635   Style.NamespaceIndentation = FormatStyle::NI_All;
19636   CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
19637               FormatStyle::NI_None);
19638   CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation,
19639               FormatStyle::NI_Inner);
19640   CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
19641               FormatStyle::NI_All);
19642 
19643   Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_OnlyFirstIf;
19644   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Never",
19645               AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never);
19646   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: WithoutElse",
19647               AllowShortIfStatementsOnASingleLine,
19648               FormatStyle::SIS_WithoutElse);
19649   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: OnlyFirstIf",
19650               AllowShortIfStatementsOnASingleLine,
19651               FormatStyle::SIS_OnlyFirstIf);
19652   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: AllIfsAndElse",
19653               AllowShortIfStatementsOnASingleLine,
19654               FormatStyle::SIS_AllIfsAndElse);
19655   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Always",
19656               AllowShortIfStatementsOnASingleLine,
19657               FormatStyle::SIS_OnlyFirstIf);
19658   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: false",
19659               AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never);
19660   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: true",
19661               AllowShortIfStatementsOnASingleLine,
19662               FormatStyle::SIS_WithoutElse);
19663 
19664   Style.IndentExternBlock = FormatStyle::IEBS_NoIndent;
19665   CHECK_PARSE("IndentExternBlock: AfterExternBlock", IndentExternBlock,
19666               FormatStyle::IEBS_AfterExternBlock);
19667   CHECK_PARSE("IndentExternBlock: Indent", IndentExternBlock,
19668               FormatStyle::IEBS_Indent);
19669   CHECK_PARSE("IndentExternBlock: NoIndent", IndentExternBlock,
19670               FormatStyle::IEBS_NoIndent);
19671   CHECK_PARSE("IndentExternBlock: true", IndentExternBlock,
19672               FormatStyle::IEBS_Indent);
19673   CHECK_PARSE("IndentExternBlock: false", IndentExternBlock,
19674               FormatStyle::IEBS_NoIndent);
19675 
19676   Style.BitFieldColonSpacing = FormatStyle::BFCS_None;
19677   CHECK_PARSE("BitFieldColonSpacing: Both", BitFieldColonSpacing,
19678               FormatStyle::BFCS_Both);
19679   CHECK_PARSE("BitFieldColonSpacing: None", BitFieldColonSpacing,
19680               FormatStyle::BFCS_None);
19681   CHECK_PARSE("BitFieldColonSpacing: Before", BitFieldColonSpacing,
19682               FormatStyle::BFCS_Before);
19683   CHECK_PARSE("BitFieldColonSpacing: After", BitFieldColonSpacing,
19684               FormatStyle::BFCS_After);
19685 
19686   Style.SortJavaStaticImport = FormatStyle::SJSIO_Before;
19687   CHECK_PARSE("SortJavaStaticImport: After", SortJavaStaticImport,
19688               FormatStyle::SJSIO_After);
19689   CHECK_PARSE("SortJavaStaticImport: Before", SortJavaStaticImport,
19690               FormatStyle::SJSIO_Before);
19691 
19692   // FIXME: This is required because parsing a configuration simply overwrites
19693   // the first N elements of the list instead of resetting it.
19694   Style.ForEachMacros.clear();
19695   std::vector<std::string> BoostForeach;
19696   BoostForeach.push_back("BOOST_FOREACH");
19697   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach);
19698   std::vector<std::string> BoostAndQForeach;
19699   BoostAndQForeach.push_back("BOOST_FOREACH");
19700   BoostAndQForeach.push_back("Q_FOREACH");
19701   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
19702               BoostAndQForeach);
19703 
19704   Style.IfMacros.clear();
19705   std::vector<std::string> CustomIfs;
19706   CustomIfs.push_back("MYIF");
19707   CHECK_PARSE("IfMacros: [MYIF]", IfMacros, CustomIfs);
19708 
19709   Style.AttributeMacros.clear();
19710   CHECK_PARSE("BasedOnStyle: LLVM", AttributeMacros,
19711               std::vector<std::string>{"__capability"});
19712   CHECK_PARSE("AttributeMacros: [attr1, attr2]", AttributeMacros,
19713               std::vector<std::string>({"attr1", "attr2"}));
19714 
19715   Style.StatementAttributeLikeMacros.clear();
19716   CHECK_PARSE("StatementAttributeLikeMacros: [emit,Q_EMIT]",
19717               StatementAttributeLikeMacros,
19718               std::vector<std::string>({"emit", "Q_EMIT"}));
19719 
19720   Style.StatementMacros.clear();
19721   CHECK_PARSE("StatementMacros: [QUNUSED]", StatementMacros,
19722               std::vector<std::string>{"QUNUSED"});
19723   CHECK_PARSE("StatementMacros: [QUNUSED, QT_REQUIRE_VERSION]", StatementMacros,
19724               std::vector<std::string>({"QUNUSED", "QT_REQUIRE_VERSION"}));
19725 
19726   Style.NamespaceMacros.clear();
19727   CHECK_PARSE("NamespaceMacros: [TESTSUITE]", NamespaceMacros,
19728               std::vector<std::string>{"TESTSUITE"});
19729   CHECK_PARSE("NamespaceMacros: [TESTSUITE, SUITE]", NamespaceMacros,
19730               std::vector<std::string>({"TESTSUITE", "SUITE"}));
19731 
19732   Style.WhitespaceSensitiveMacros.clear();
19733   CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE]",
19734               WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"});
19735   CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE, ASSERT]",
19736               WhitespaceSensitiveMacros,
19737               std::vector<std::string>({"STRINGIZE", "ASSERT"}));
19738   Style.WhitespaceSensitiveMacros.clear();
19739   CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE']",
19740               WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"});
19741   CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE', 'ASSERT']",
19742               WhitespaceSensitiveMacros,
19743               std::vector<std::string>({"STRINGIZE", "ASSERT"}));
19744 
19745   Style.IncludeStyle.IncludeCategories.clear();
19746   std::vector<tooling::IncludeStyle::IncludeCategory> ExpectedCategories = {
19747       {"abc/.*", 2, 0, false}, {".*", 1, 0, true}};
19748   CHECK_PARSE("IncludeCategories:\n"
19749               "  - Regex: abc/.*\n"
19750               "    Priority: 2\n"
19751               "  - Regex: .*\n"
19752               "    Priority: 1\n"
19753               "    CaseSensitive: true\n",
19754               IncludeStyle.IncludeCategories, ExpectedCategories);
19755   CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeStyle.IncludeIsMainRegex,
19756               "abc$");
19757   CHECK_PARSE("IncludeIsMainSourceRegex: 'abc$'",
19758               IncludeStyle.IncludeIsMainSourceRegex, "abc$");
19759 
19760   Style.SortIncludes = FormatStyle::SI_Never;
19761   CHECK_PARSE("SortIncludes: true", SortIncludes,
19762               FormatStyle::SI_CaseSensitive);
19763   CHECK_PARSE("SortIncludes: false", SortIncludes, FormatStyle::SI_Never);
19764   CHECK_PARSE("SortIncludes: CaseInsensitive", SortIncludes,
19765               FormatStyle::SI_CaseInsensitive);
19766   CHECK_PARSE("SortIncludes: CaseSensitive", SortIncludes,
19767               FormatStyle::SI_CaseSensitive);
19768   CHECK_PARSE("SortIncludes: Never", SortIncludes, FormatStyle::SI_Never);
19769 
19770   Style.RawStringFormats.clear();
19771   std::vector<FormatStyle::RawStringFormat> ExpectedRawStringFormats = {
19772       {
19773           FormatStyle::LK_TextProto,
19774           {"pb", "proto"},
19775           {"PARSE_TEXT_PROTO"},
19776           /*CanonicalDelimiter=*/"",
19777           "llvm",
19778       },
19779       {
19780           FormatStyle::LK_Cpp,
19781           {"cc", "cpp"},
19782           {"C_CODEBLOCK", "CPPEVAL"},
19783           /*CanonicalDelimiter=*/"cc",
19784           /*BasedOnStyle=*/"",
19785       },
19786   };
19787 
19788   CHECK_PARSE("RawStringFormats:\n"
19789               "  - Language: TextProto\n"
19790               "    Delimiters:\n"
19791               "      - 'pb'\n"
19792               "      - 'proto'\n"
19793               "    EnclosingFunctions:\n"
19794               "      - 'PARSE_TEXT_PROTO'\n"
19795               "    BasedOnStyle: llvm\n"
19796               "  - Language: Cpp\n"
19797               "    Delimiters:\n"
19798               "      - 'cc'\n"
19799               "      - 'cpp'\n"
19800               "    EnclosingFunctions:\n"
19801               "      - 'C_CODEBLOCK'\n"
19802               "      - 'CPPEVAL'\n"
19803               "    CanonicalDelimiter: 'cc'",
19804               RawStringFormats, ExpectedRawStringFormats);
19805 
19806   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
19807               "  Minimum: 0\n"
19808               "  Maximum: 0",
19809               SpacesInLineCommentPrefix.Minimum, 0u);
19810   EXPECT_EQ(Style.SpacesInLineCommentPrefix.Maximum, 0u);
19811   Style.SpacesInLineCommentPrefix.Minimum = 1;
19812   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
19813               "  Minimum: 2",
19814               SpacesInLineCommentPrefix.Minimum, 0u);
19815   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
19816               "  Maximum: -1",
19817               SpacesInLineCommentPrefix.Maximum, -1u);
19818   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
19819               "  Minimum: 2",
19820               SpacesInLineCommentPrefix.Minimum, 2u);
19821   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
19822               "  Maximum: 1",
19823               SpacesInLineCommentPrefix.Maximum, 1u);
19824   EXPECT_EQ(Style.SpacesInLineCommentPrefix.Minimum, 1u);
19825 
19826   Style.SpacesInAngles = FormatStyle::SIAS_Always;
19827   CHECK_PARSE("SpacesInAngles: Never", SpacesInAngles, FormatStyle::SIAS_Never);
19828   CHECK_PARSE("SpacesInAngles: Always", SpacesInAngles,
19829               FormatStyle::SIAS_Always);
19830   CHECK_PARSE("SpacesInAngles: Leave", SpacesInAngles, FormatStyle::SIAS_Leave);
19831   // For backward compatibility:
19832   CHECK_PARSE("SpacesInAngles: false", SpacesInAngles, FormatStyle::SIAS_Never);
19833   CHECK_PARSE("SpacesInAngles: true", SpacesInAngles, FormatStyle::SIAS_Always);
19834 }
19835 
19836 TEST_F(FormatTest, ParsesConfigurationWithLanguages) {
19837   FormatStyle Style = {};
19838   Style.Language = FormatStyle::LK_Cpp;
19839   CHECK_PARSE("Language: Cpp\n"
19840               "IndentWidth: 12",
19841               IndentWidth, 12u);
19842   EXPECT_EQ(parseConfiguration("Language: JavaScript\n"
19843                                "IndentWidth: 34",
19844                                &Style),
19845             ParseError::Unsuitable);
19846   FormatStyle BinPackedTCS = {};
19847   BinPackedTCS.Language = FormatStyle::LK_JavaScript;
19848   EXPECT_EQ(parseConfiguration("BinPackArguments: true\n"
19849                                "InsertTrailingCommas: Wrapped",
19850                                &BinPackedTCS),
19851             ParseError::BinPackTrailingCommaConflict);
19852   EXPECT_EQ(12u, Style.IndentWidth);
19853   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
19854   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
19855 
19856   Style.Language = FormatStyle::LK_JavaScript;
19857   CHECK_PARSE("Language: JavaScript\n"
19858               "IndentWidth: 12",
19859               IndentWidth, 12u);
19860   CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u);
19861   EXPECT_EQ(parseConfiguration("Language: Cpp\n"
19862                                "IndentWidth: 34",
19863                                &Style),
19864             ParseError::Unsuitable);
19865   EXPECT_EQ(23u, Style.IndentWidth);
19866   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
19867   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
19868 
19869   CHECK_PARSE("BasedOnStyle: LLVM\n"
19870               "IndentWidth: 67",
19871               IndentWidth, 67u);
19872 
19873   CHECK_PARSE("---\n"
19874               "Language: JavaScript\n"
19875               "IndentWidth: 12\n"
19876               "---\n"
19877               "Language: Cpp\n"
19878               "IndentWidth: 34\n"
19879               "...\n",
19880               IndentWidth, 12u);
19881 
19882   Style.Language = FormatStyle::LK_Cpp;
19883   CHECK_PARSE("---\n"
19884               "Language: JavaScript\n"
19885               "IndentWidth: 12\n"
19886               "---\n"
19887               "Language: Cpp\n"
19888               "IndentWidth: 34\n"
19889               "...\n",
19890               IndentWidth, 34u);
19891   CHECK_PARSE("---\n"
19892               "IndentWidth: 78\n"
19893               "---\n"
19894               "Language: JavaScript\n"
19895               "IndentWidth: 56\n"
19896               "...\n",
19897               IndentWidth, 78u);
19898 
19899   Style.ColumnLimit = 123;
19900   Style.IndentWidth = 234;
19901   Style.BreakBeforeBraces = FormatStyle::BS_Linux;
19902   Style.TabWidth = 345;
19903   EXPECT_FALSE(parseConfiguration("---\n"
19904                                   "IndentWidth: 456\n"
19905                                   "BreakBeforeBraces: Allman\n"
19906                                   "---\n"
19907                                   "Language: JavaScript\n"
19908                                   "IndentWidth: 111\n"
19909                                   "TabWidth: 111\n"
19910                                   "---\n"
19911                                   "Language: Cpp\n"
19912                                   "BreakBeforeBraces: Stroustrup\n"
19913                                   "TabWidth: 789\n"
19914                                   "...\n",
19915                                   &Style));
19916   EXPECT_EQ(123u, Style.ColumnLimit);
19917   EXPECT_EQ(456u, Style.IndentWidth);
19918   EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces);
19919   EXPECT_EQ(789u, Style.TabWidth);
19920 
19921   EXPECT_EQ(parseConfiguration("---\n"
19922                                "Language: JavaScript\n"
19923                                "IndentWidth: 56\n"
19924                                "---\n"
19925                                "IndentWidth: 78\n"
19926                                "...\n",
19927                                &Style),
19928             ParseError::Error);
19929   EXPECT_EQ(parseConfiguration("---\n"
19930                                "Language: JavaScript\n"
19931                                "IndentWidth: 56\n"
19932                                "---\n"
19933                                "Language: JavaScript\n"
19934                                "IndentWidth: 78\n"
19935                                "...\n",
19936                                &Style),
19937             ParseError::Error);
19938 
19939   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
19940 }
19941 
19942 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) {
19943   FormatStyle Style = {};
19944   Style.Language = FormatStyle::LK_JavaScript;
19945   Style.BreakBeforeTernaryOperators = true;
19946   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value());
19947   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
19948 
19949   Style.BreakBeforeTernaryOperators = true;
19950   EXPECT_EQ(0, parseConfiguration("---\n"
19951                                   "BasedOnStyle: Google\n"
19952                                   "---\n"
19953                                   "Language: JavaScript\n"
19954                                   "IndentWidth: 76\n"
19955                                   "...\n",
19956                                   &Style)
19957                    .value());
19958   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
19959   EXPECT_EQ(76u, Style.IndentWidth);
19960   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
19961 }
19962 
19963 TEST_F(FormatTest, ConfigurationRoundTripTest) {
19964   FormatStyle Style = getLLVMStyle();
19965   std::string YAML = configurationAsText(Style);
19966   FormatStyle ParsedStyle = {};
19967   ParsedStyle.Language = FormatStyle::LK_Cpp;
19968   EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
19969   EXPECT_EQ(Style, ParsedStyle);
19970 }
19971 
19972 TEST_F(FormatTest, WorksFor8bitEncodings) {
19973   EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
19974             "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
19975             "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
19976             "\"\xef\xee\xf0\xf3...\"",
19977             format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
19978                    "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
19979                    "\xef\xee\xf0\xf3...\"",
19980                    getLLVMStyleWithColumns(12)));
19981 }
19982 
19983 TEST_F(FormatTest, HandlesUTF8BOM) {
19984   EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf"));
19985   EXPECT_EQ("\xef\xbb\xbf#include <iostream>",
19986             format("\xef\xbb\xbf#include <iostream>"));
19987   EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>",
19988             format("\xef\xbb\xbf\n#include <iostream>"));
19989 }
19990 
19991 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
19992 #if !defined(_MSC_VER)
19993 
19994 TEST_F(FormatTest, CountsUTF8CharactersProperly) {
19995   verifyFormat("\"Однажды в студёную зимнюю пору...\"",
19996                getLLVMStyleWithColumns(35));
19997   verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
19998                getLLVMStyleWithColumns(31));
19999   verifyFormat("// Однажды в студёную зимнюю пору...",
20000                getLLVMStyleWithColumns(36));
20001   verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32));
20002   verifyFormat("/* Однажды в студёную зимнюю пору... */",
20003                getLLVMStyleWithColumns(39));
20004   verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
20005                getLLVMStyleWithColumns(35));
20006 }
20007 
20008 TEST_F(FormatTest, SplitsUTF8Strings) {
20009   // Non-printable characters' width is currently considered to be the length in
20010   // bytes in UTF8. The characters can be displayed in very different manner
20011   // (zero-width, single width with a substitution glyph, expanded to their code
20012   // (e.g. "<8d>"), so there's no single correct way to handle them.
20013   EXPECT_EQ("\"aaaaÄ\"\n"
20014             "\"\xc2\x8d\";",
20015             format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
20016   EXPECT_EQ("\"aaaaaaaÄ\"\n"
20017             "\"\xc2\x8d\";",
20018             format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
20019   EXPECT_EQ("\"Однажды, в \"\n"
20020             "\"студёную \"\n"
20021             "\"зимнюю \"\n"
20022             "\"пору,\"",
20023             format("\"Однажды, в студёную зимнюю пору,\"",
20024                    getLLVMStyleWithColumns(13)));
20025   EXPECT_EQ(
20026       "\"一 二 三 \"\n"
20027       "\"四 五六 \"\n"
20028       "\"七 八 九 \"\n"
20029       "\"十\"",
20030       format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11)));
20031   EXPECT_EQ("\"一\t\"\n"
20032             "\"二 \t\"\n"
20033             "\"三 四 \"\n"
20034             "\"五\t\"\n"
20035             "\"六 \t\"\n"
20036             "\"七 \"\n"
20037             "\"八九十\tqq\"",
20038             format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
20039                    getLLVMStyleWithColumns(11)));
20040 
20041   // UTF8 character in an escape sequence.
20042   EXPECT_EQ("\"aaaaaa\"\n"
20043             "\"\\\xC2\x8D\"",
20044             format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10)));
20045 }
20046 
20047 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
20048   EXPECT_EQ("const char *sssss =\n"
20049             "    \"一二三四五六七八\\\n"
20050             " 九 十\";",
20051             format("const char *sssss = \"一二三四五六七八\\\n"
20052                    " 九 十\";",
20053                    getLLVMStyleWithColumns(30)));
20054 }
20055 
20056 TEST_F(FormatTest, SplitsUTF8LineComments) {
20057   EXPECT_EQ("// aaaaÄ\xc2\x8d",
20058             format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10)));
20059   EXPECT_EQ("// Я из лесу\n"
20060             "// вышел; был\n"
20061             "// сильный\n"
20062             "// мороз.",
20063             format("// Я из лесу вышел; был сильный мороз.",
20064                    getLLVMStyleWithColumns(13)));
20065   EXPECT_EQ("// 一二三\n"
20066             "// 四五六七\n"
20067             "// 八  九\n"
20068             "// 十",
20069             format("// 一二三 四五六七 八  九 十", getLLVMStyleWithColumns(9)));
20070 }
20071 
20072 TEST_F(FormatTest, SplitsUTF8BlockComments) {
20073   EXPECT_EQ("/* Гляжу,\n"
20074             " * поднимается\n"
20075             " * медленно в\n"
20076             " * гору\n"
20077             " * Лошадка,\n"
20078             " * везущая\n"
20079             " * хворосту\n"
20080             " * воз. */",
20081             format("/* Гляжу, поднимается медленно в гору\n"
20082                    " * Лошадка, везущая хворосту воз. */",
20083                    getLLVMStyleWithColumns(13)));
20084   EXPECT_EQ(
20085       "/* 一二三\n"
20086       " * 四五六七\n"
20087       " * 八  九\n"
20088       " * 十  */",
20089       format("/* 一二三 四五六七 八  九 十  */", getLLVMStyleWithColumns(9)));
20090   EXPECT_EQ("/* �������� ��������\n"
20091             " * ��������\n"
20092             " * ������-�� */",
20093             format("/* �������� �������� �������� ������-�� */", getLLVMStyleWithColumns(12)));
20094 }
20095 
20096 #endif // _MSC_VER
20097 
20098 TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
20099   FormatStyle Style = getLLVMStyle();
20100 
20101   Style.ConstructorInitializerIndentWidth = 4;
20102   verifyFormat(
20103       "SomeClass::Constructor()\n"
20104       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
20105       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
20106       Style);
20107 
20108   Style.ConstructorInitializerIndentWidth = 2;
20109   verifyFormat(
20110       "SomeClass::Constructor()\n"
20111       "  : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
20112       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
20113       Style);
20114 
20115   Style.ConstructorInitializerIndentWidth = 0;
20116   verifyFormat(
20117       "SomeClass::Constructor()\n"
20118       ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
20119       "  aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
20120       Style);
20121   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
20122   verifyFormat(
20123       "SomeLongTemplateVariableName<\n"
20124       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>",
20125       Style);
20126   verifyFormat("bool smaller = 1 < "
20127                "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
20128                "                       "
20129                "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
20130                Style);
20131 
20132   Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
20133   verifyFormat("SomeClass::Constructor() :\n"
20134                "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa),\n"
20135                "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa) {}",
20136                Style);
20137 }
20138 
20139 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {
20140   FormatStyle Style = getLLVMStyle();
20141   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
20142   Style.ConstructorInitializerIndentWidth = 4;
20143   verifyFormat("SomeClass::Constructor()\n"
20144                "    : a(a)\n"
20145                "    , b(b)\n"
20146                "    , c(c) {}",
20147                Style);
20148   verifyFormat("SomeClass::Constructor()\n"
20149                "    : a(a) {}",
20150                Style);
20151 
20152   Style.ColumnLimit = 0;
20153   verifyFormat("SomeClass::Constructor()\n"
20154                "    : a(a) {}",
20155                Style);
20156   verifyFormat("SomeClass::Constructor() noexcept\n"
20157                "    : a(a) {}",
20158                Style);
20159   verifyFormat("SomeClass::Constructor()\n"
20160                "    : a(a)\n"
20161                "    , b(b)\n"
20162                "    , c(c) {}",
20163                Style);
20164   verifyFormat("SomeClass::Constructor()\n"
20165                "    : a(a) {\n"
20166                "  foo();\n"
20167                "  bar();\n"
20168                "}",
20169                Style);
20170 
20171   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
20172   verifyFormat("SomeClass::Constructor()\n"
20173                "    : a(a)\n"
20174                "    , b(b)\n"
20175                "    , c(c) {\n}",
20176                Style);
20177   verifyFormat("SomeClass::Constructor()\n"
20178                "    : a(a) {\n}",
20179                Style);
20180 
20181   Style.ColumnLimit = 80;
20182   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
20183   Style.ConstructorInitializerIndentWidth = 2;
20184   verifyFormat("SomeClass::Constructor()\n"
20185                "  : a(a)\n"
20186                "  , b(b)\n"
20187                "  , c(c) {}",
20188                Style);
20189 
20190   Style.ConstructorInitializerIndentWidth = 0;
20191   verifyFormat("SomeClass::Constructor()\n"
20192                ": a(a)\n"
20193                ", b(b)\n"
20194                ", c(c) {}",
20195                Style);
20196 
20197   Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
20198   Style.ConstructorInitializerIndentWidth = 4;
20199   verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
20200   verifyFormat(
20201       "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n",
20202       Style);
20203   verifyFormat(
20204       "SomeClass::Constructor()\n"
20205       "    : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
20206       Style);
20207   Style.ConstructorInitializerIndentWidth = 4;
20208   Style.ColumnLimit = 60;
20209   verifyFormat("SomeClass::Constructor()\n"
20210                "    : aaaaaaaa(aaaaaaaa)\n"
20211                "    , aaaaaaaa(aaaaaaaa)\n"
20212                "    , aaaaaaaa(aaaaaaaa) {}",
20213                Style);
20214 }
20215 
20216 TEST_F(FormatTest, ConstructorInitializersWithPreprocessorDirective) {
20217   FormatStyle Style = getLLVMStyle();
20218   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
20219   Style.ConstructorInitializerIndentWidth = 4;
20220   verifyFormat("SomeClass::Constructor()\n"
20221                "    : a{a}\n"
20222                "    , b{b} {}",
20223                Style);
20224   verifyFormat("SomeClass::Constructor()\n"
20225                "    : a{a}\n"
20226                "#if CONDITION\n"
20227                "    , b{b}\n"
20228                "#endif\n"
20229                "{\n}",
20230                Style);
20231   Style.ConstructorInitializerIndentWidth = 2;
20232   verifyFormat("SomeClass::Constructor()\n"
20233                "#if CONDITION\n"
20234                "  : a{a}\n"
20235                "#endif\n"
20236                "  , b{b}\n"
20237                "  , c{c} {\n}",
20238                Style);
20239   Style.ConstructorInitializerIndentWidth = 0;
20240   verifyFormat("SomeClass::Constructor()\n"
20241                ": a{a}\n"
20242                "#ifdef CONDITION\n"
20243                ", b{b}\n"
20244                "#else\n"
20245                ", c{c}\n"
20246                "#endif\n"
20247                ", d{d} {\n}",
20248                Style);
20249   Style.ConstructorInitializerIndentWidth = 4;
20250   verifyFormat("SomeClass::Constructor()\n"
20251                "    : a{a}\n"
20252                "#if WINDOWS\n"
20253                "#if DEBUG\n"
20254                "    , b{0}\n"
20255                "#else\n"
20256                "    , b{1}\n"
20257                "#endif\n"
20258                "#else\n"
20259                "#if DEBUG\n"
20260                "    , b{2}\n"
20261                "#else\n"
20262                "    , b{3}\n"
20263                "#endif\n"
20264                "#endif\n"
20265                "{\n}",
20266                Style);
20267   verifyFormat("SomeClass::Constructor()\n"
20268                "    : a{a}\n"
20269                "#if WINDOWS\n"
20270                "    , b{0}\n"
20271                "#if DEBUG\n"
20272                "    , c{0}\n"
20273                "#else\n"
20274                "    , c{1}\n"
20275                "#endif\n"
20276                "#else\n"
20277                "#if DEBUG\n"
20278                "    , c{2}\n"
20279                "#else\n"
20280                "    , c{3}\n"
20281                "#endif\n"
20282                "    , b{1}\n"
20283                "#endif\n"
20284                "{\n}",
20285                Style);
20286 }
20287 
20288 TEST_F(FormatTest, Destructors) {
20289   verifyFormat("void F(int &i) { i.~int(); }");
20290   verifyFormat("void F(int &i) { i->~int(); }");
20291 }
20292 
20293 TEST_F(FormatTest, FormatsWithWebKitStyle) {
20294   FormatStyle Style = getWebKitStyle();
20295 
20296   // Don't indent in outer namespaces.
20297   verifyFormat("namespace outer {\n"
20298                "int i;\n"
20299                "namespace inner {\n"
20300                "    int i;\n"
20301                "} // namespace inner\n"
20302                "} // namespace outer\n"
20303                "namespace other_outer {\n"
20304                "int i;\n"
20305                "}",
20306                Style);
20307 
20308   // Don't indent case labels.
20309   verifyFormat("switch (variable) {\n"
20310                "case 1:\n"
20311                "case 2:\n"
20312                "    doSomething();\n"
20313                "    break;\n"
20314                "default:\n"
20315                "    ++variable;\n"
20316                "}",
20317                Style);
20318 
20319   // Wrap before binary operators.
20320   EXPECT_EQ("void f()\n"
20321             "{\n"
20322             "    if (aaaaaaaaaaaaaaaa\n"
20323             "        && bbbbbbbbbbbbbbbbbbbbbbbb\n"
20324             "        && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
20325             "        return;\n"
20326             "}",
20327             format("void f() {\n"
20328                    "if (aaaaaaaaaaaaaaaa\n"
20329                    "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
20330                    "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
20331                    "return;\n"
20332                    "}",
20333                    Style));
20334 
20335   // Allow functions on a single line.
20336   verifyFormat("void f() { return; }", Style);
20337 
20338   // Allow empty blocks on a single line and insert a space in empty blocks.
20339   EXPECT_EQ("void f() { }", format("void f() {}", Style));
20340   EXPECT_EQ("while (true) { }", format("while (true) {}", Style));
20341   // However, don't merge non-empty short loops.
20342   EXPECT_EQ("while (true) {\n"
20343             "    continue;\n"
20344             "}",
20345             format("while (true) { continue; }", Style));
20346 
20347   // Constructor initializers are formatted one per line with the "," on the
20348   // new line.
20349   verifyFormat("Constructor()\n"
20350                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
20351                "    , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
20352                "          aaaaaaaaaaaaaa)\n"
20353                "    , aaaaaaaaaaaaaaaaaaaaaaa()\n"
20354                "{\n"
20355                "}",
20356                Style);
20357   verifyFormat("SomeClass::Constructor()\n"
20358                "    : a(a)\n"
20359                "{\n"
20360                "}",
20361                Style);
20362   EXPECT_EQ("SomeClass::Constructor()\n"
20363             "    : a(a)\n"
20364             "{\n"
20365             "}",
20366             format("SomeClass::Constructor():a(a){}", Style));
20367   verifyFormat("SomeClass::Constructor()\n"
20368                "    : a(a)\n"
20369                "    , b(b)\n"
20370                "    , c(c)\n"
20371                "{\n"
20372                "}",
20373                Style);
20374   verifyFormat("SomeClass::Constructor()\n"
20375                "    : a(a)\n"
20376                "{\n"
20377                "    foo();\n"
20378                "    bar();\n"
20379                "}",
20380                Style);
20381 
20382   // Access specifiers should be aligned left.
20383   verifyFormat("class C {\n"
20384                "public:\n"
20385                "    int i;\n"
20386                "};",
20387                Style);
20388 
20389   // Do not align comments.
20390   verifyFormat("int a; // Do not\n"
20391                "double b; // align comments.",
20392                Style);
20393 
20394   // Do not align operands.
20395   EXPECT_EQ("ASSERT(aaaa\n"
20396             "    || bbbb);",
20397             format("ASSERT ( aaaa\n||bbbb);", Style));
20398 
20399   // Accept input's line breaks.
20400   EXPECT_EQ("if (aaaaaaaaaaaaaaa\n"
20401             "    || bbbbbbbbbbbbbbb) {\n"
20402             "    i++;\n"
20403             "}",
20404             format("if (aaaaaaaaaaaaaaa\n"
20405                    "|| bbbbbbbbbbbbbbb) { i++; }",
20406                    Style));
20407   EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
20408             "    i++;\n"
20409             "}",
20410             format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
20411 
20412   // Don't automatically break all macro definitions (llvm.org/PR17842).
20413   verifyFormat("#define aNumber 10", Style);
20414   // However, generally keep the line breaks that the user authored.
20415   EXPECT_EQ("#define aNumber \\\n"
20416             "    10",
20417             format("#define aNumber \\\n"
20418                    " 10",
20419                    Style));
20420 
20421   // Keep empty and one-element array literals on a single line.
20422   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
20423             "                                  copyItems:YES];",
20424             format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
20425                    "copyItems:YES];",
20426                    Style));
20427   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
20428             "                                  copyItems:YES];",
20429             format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
20430                    "             copyItems:YES];",
20431                    Style));
20432   // FIXME: This does not seem right, there should be more indentation before
20433   // the array literal's entries. Nested blocks have the same problem.
20434   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
20435             "    @\"a\",\n"
20436             "    @\"a\"\n"
20437             "]\n"
20438             "                                  copyItems:YES];",
20439             format("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
20440                    "     @\"a\",\n"
20441                    "     @\"a\"\n"
20442                    "     ]\n"
20443                    "       copyItems:YES];",
20444                    Style));
20445   EXPECT_EQ(
20446       "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
20447       "                                  copyItems:YES];",
20448       format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
20449              "   copyItems:YES];",
20450              Style));
20451 
20452   verifyFormat("[self.a b:c c:d];", Style);
20453   EXPECT_EQ("[self.a b:c\n"
20454             "        c:d];",
20455             format("[self.a b:c\n"
20456                    "c:d];",
20457                    Style));
20458 }
20459 
20460 TEST_F(FormatTest, FormatsLambdas) {
20461   verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n");
20462   verifyFormat(
20463       "int c = [b]() mutable noexcept { return [&b] { return b++; }(); }();\n");
20464   verifyFormat("int c = [&] { [=] { return b++; }(); }();\n");
20465   verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n");
20466   verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n");
20467   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n");
20468   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n");
20469   verifyFormat("auto c = [a = [b = 42] {}] {};\n");
20470   verifyFormat("auto c = [a = &i + 10, b = [] {}] {};\n");
20471   verifyFormat("int x = f(*+[] {});");
20472   verifyFormat("void f() {\n"
20473                "  other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
20474                "}\n");
20475   verifyFormat("void f() {\n"
20476                "  other(x.begin(), //\n"
20477                "        x.end(),   //\n"
20478                "        [&](int, int) { return 1; });\n"
20479                "}\n");
20480   verifyFormat("void f() {\n"
20481                "  other.other.other.other.other(\n"
20482                "      x.begin(), x.end(),\n"
20483                "      [something, rather](int, int, int, int, int, int, int) { "
20484                "return 1; });\n"
20485                "}\n");
20486   verifyFormat(
20487       "void f() {\n"
20488       "  other.other.other.other.other(\n"
20489       "      x.begin(), x.end(),\n"
20490       "      [something, rather](int, int, int, int, int, int, int) {\n"
20491       "        //\n"
20492       "      });\n"
20493       "}\n");
20494   verifyFormat("SomeFunction([]() { // A cool function...\n"
20495                "  return 43;\n"
20496                "});");
20497   EXPECT_EQ("SomeFunction([]() {\n"
20498             "#define A a\n"
20499             "  return 43;\n"
20500             "});",
20501             format("SomeFunction([](){\n"
20502                    "#define A a\n"
20503                    "return 43;\n"
20504                    "});"));
20505   verifyFormat("void f() {\n"
20506                "  SomeFunction([](decltype(x), A *a) {});\n"
20507                "  SomeFunction([](typeof(x), A *a) {});\n"
20508                "  SomeFunction([](_Atomic(x), A *a) {});\n"
20509                "  SomeFunction([](__underlying_type(x), A *a) {});\n"
20510                "}");
20511   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
20512                "    [](const aaaaaaaaaa &a) { return a; });");
20513   verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
20514                "  SomeOtherFunctioooooooooooooooooooooooooon();\n"
20515                "});");
20516   verifyFormat("Constructor()\n"
20517                "    : Field([] { // comment\n"
20518                "        int i;\n"
20519                "      }) {}");
20520   verifyFormat("auto my_lambda = [](const string &some_parameter) {\n"
20521                "  return some_parameter.size();\n"
20522                "};");
20523   verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n"
20524                "    [](const string &s) { return s; };");
20525   verifyFormat("int i = aaaaaa ? 1 //\n"
20526                "               : [] {\n"
20527                "                   return 2; //\n"
20528                "                 }();");
20529   verifyFormat("llvm::errs() << \"number of twos is \"\n"
20530                "             << std::count_if(v.begin(), v.end(), [](int x) {\n"
20531                "                  return x == 2; // force break\n"
20532                "                });");
20533   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
20534                "    [=](int iiiiiiiiiiii) {\n"
20535                "      return aaaaaaaaaaaaaaaaaaaaaaa !=\n"
20536                "             aaaaaaaaaaaaaaaaaaaaaaa;\n"
20537                "    });",
20538                getLLVMStyleWithColumns(60));
20539 
20540   verifyFormat("SomeFunction({[&] {\n"
20541                "                // comment\n"
20542                "              },\n"
20543                "              [&] {\n"
20544                "                // comment\n"
20545                "              }});");
20546   verifyFormat("SomeFunction({[&] {\n"
20547                "  // comment\n"
20548                "}});");
20549   verifyFormat(
20550       "virtual aaaaaaaaaaaaaaaa(\n"
20551       "    std::function<bool()> bbbbbbbbbbbb = [&]() { return true; },\n"
20552       "    aaaaa aaaaaaaaa);");
20553 
20554   // Lambdas with return types.
20555   verifyFormat("int c = []() -> int { return 2; }();\n");
20556   verifyFormat("int c = []() -> int * { return 2; }();\n");
20557   verifyFormat("int c = []() -> vector<int> { return {2}; }();\n");
20558   verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
20559   verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
20560   verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
20561   verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
20562   verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
20563   verifyFormat("[a, a]() -> a<1> {};");
20564   verifyFormat("[]() -> foo<5 + 2> { return {}; };");
20565   verifyFormat("[]() -> foo<5 - 2> { return {}; };");
20566   verifyFormat("[]() -> foo<5 / 2> { return {}; };");
20567   verifyFormat("[]() -> foo<5 * 2> { return {}; };");
20568   verifyFormat("[]() -> foo<5 % 2> { return {}; };");
20569   verifyFormat("[]() -> foo<5 << 2> { return {}; };");
20570   verifyFormat("[]() -> foo<!5> { return {}; };");
20571   verifyFormat("[]() -> foo<~5> { return {}; };");
20572   verifyFormat("[]() -> foo<5 | 2> { return {}; };");
20573   verifyFormat("[]() -> foo<5 || 2> { return {}; };");
20574   verifyFormat("[]() -> foo<5 & 2> { return {}; };");
20575   verifyFormat("[]() -> foo<5 && 2> { return {}; };");
20576   verifyFormat("[]() -> foo<5 == 2> { return {}; };");
20577   verifyFormat("[]() -> foo<5 != 2> { return {}; };");
20578   verifyFormat("[]() -> foo<5 >= 2> { return {}; };");
20579   verifyFormat("[]() -> foo<5 <= 2> { return {}; };");
20580   verifyFormat("[]() -> foo<5 < 2> { return {}; };");
20581   verifyFormat("[]() -> foo<2 ? 1 : 0> { return {}; };");
20582   verifyFormat("namespace bar {\n"
20583                "// broken:\n"
20584                "auto foo{[]() -> foo<5 + 2> { return {}; }};\n"
20585                "} // namespace bar");
20586   verifyFormat("namespace bar {\n"
20587                "// broken:\n"
20588                "auto foo{[]() -> foo<5 - 2> { return {}; }};\n"
20589                "} // namespace bar");
20590   verifyFormat("namespace bar {\n"
20591                "// broken:\n"
20592                "auto foo{[]() -> foo<5 / 2> { return {}; }};\n"
20593                "} // namespace bar");
20594   verifyFormat("namespace bar {\n"
20595                "// broken:\n"
20596                "auto foo{[]() -> foo<5 * 2> { return {}; }};\n"
20597                "} // namespace bar");
20598   verifyFormat("namespace bar {\n"
20599                "// broken:\n"
20600                "auto foo{[]() -> foo<5 % 2> { return {}; }};\n"
20601                "} // namespace bar");
20602   verifyFormat("namespace bar {\n"
20603                "// broken:\n"
20604                "auto foo{[]() -> foo<5 << 2> { return {}; }};\n"
20605                "} // namespace bar");
20606   verifyFormat("namespace bar {\n"
20607                "// broken:\n"
20608                "auto foo{[]() -> foo<!5> { return {}; }};\n"
20609                "} // namespace bar");
20610   verifyFormat("namespace bar {\n"
20611                "// broken:\n"
20612                "auto foo{[]() -> foo<~5> { return {}; }};\n"
20613                "} // namespace bar");
20614   verifyFormat("namespace bar {\n"
20615                "// broken:\n"
20616                "auto foo{[]() -> foo<5 | 2> { return {}; }};\n"
20617                "} // namespace bar");
20618   verifyFormat("namespace bar {\n"
20619                "// broken:\n"
20620                "auto foo{[]() -> foo<5 || 2> { return {}; }};\n"
20621                "} // namespace bar");
20622   verifyFormat("namespace bar {\n"
20623                "// broken:\n"
20624                "auto foo{[]() -> foo<5 & 2> { return {}; }};\n"
20625                "} // namespace bar");
20626   verifyFormat("namespace bar {\n"
20627                "// broken:\n"
20628                "auto foo{[]() -> foo<5 && 2> { return {}; }};\n"
20629                "} // namespace bar");
20630   verifyFormat("namespace bar {\n"
20631                "// broken:\n"
20632                "auto foo{[]() -> foo<5 == 2> { return {}; }};\n"
20633                "} // namespace bar");
20634   verifyFormat("namespace bar {\n"
20635                "// broken:\n"
20636                "auto foo{[]() -> foo<5 != 2> { return {}; }};\n"
20637                "} // namespace bar");
20638   verifyFormat("namespace bar {\n"
20639                "// broken:\n"
20640                "auto foo{[]() -> foo<5 >= 2> { return {}; }};\n"
20641                "} // namespace bar");
20642   verifyFormat("namespace bar {\n"
20643                "// broken:\n"
20644                "auto foo{[]() -> foo<5 <= 2> { return {}; }};\n"
20645                "} // namespace bar");
20646   verifyFormat("namespace bar {\n"
20647                "// broken:\n"
20648                "auto foo{[]() -> foo<5 < 2> { return {}; }};\n"
20649                "} // namespace bar");
20650   verifyFormat("namespace bar {\n"
20651                "// broken:\n"
20652                "auto foo{[]() -> foo<2 ? 1 : 0> { return {}; }};\n"
20653                "} // namespace bar");
20654   verifyFormat("[]() -> a<1> {};");
20655   verifyFormat("[]() -> a<1> { ; };");
20656   verifyFormat("[]() -> a<1> { ; }();");
20657   verifyFormat("[a, a]() -> a<true> {};");
20658   verifyFormat("[]() -> a<true> {};");
20659   verifyFormat("[]() -> a<true> { ; };");
20660   verifyFormat("[]() -> a<true> { ; }();");
20661   verifyFormat("[a, a]() -> a<false> {};");
20662   verifyFormat("[]() -> a<false> {};");
20663   verifyFormat("[]() -> a<false> { ; };");
20664   verifyFormat("[]() -> a<false> { ; }();");
20665   verifyFormat("auto foo{[]() -> foo<false> { ; }};");
20666   verifyFormat("namespace bar {\n"
20667                "auto foo{[]() -> foo<false> { ; }};\n"
20668                "} // namespace bar");
20669   verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
20670                "                   int j) -> int {\n"
20671                "  return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
20672                "};");
20673   verifyFormat(
20674       "aaaaaaaaaaaaaaaaaaaaaa(\n"
20675       "    [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n"
20676       "      return aaaaaaaaaaaaaaaaa;\n"
20677       "    });",
20678       getLLVMStyleWithColumns(70));
20679   verifyFormat("[]() //\n"
20680                "    -> int {\n"
20681                "  return 1; //\n"
20682                "};");
20683   verifyFormat("[]() -> Void<T...> {};");
20684   verifyFormat("[a, b]() -> Tuple<T...> { return {}; };");
20685   verifyFormat("SomeFunction({[]() -> int[] { return {}; }});");
20686   verifyFormat("SomeFunction({[]() -> int *[] { return {}; }});");
20687   verifyFormat("SomeFunction({[]() -> int (*)[] { return {}; }});");
20688   verifyFormat("SomeFunction({[]() -> ns::type<int (*)[]> { return {}; }});");
20689   verifyFormat("return int{[x = x]() { return x; }()};");
20690 
20691   // Lambdas with explicit template argument lists.
20692   verifyFormat(
20693       "auto L = []<template <typename> class T, class U>(T<U> &&a) {};\n");
20694   verifyFormat("auto L = []<class T>(T) {\n"
20695                "  {\n"
20696                "    f();\n"
20697                "    g();\n"
20698                "  }\n"
20699                "};\n");
20700   verifyFormat("auto L = []<class... T>(T...) {\n"
20701                "  {\n"
20702                "    f();\n"
20703                "    g();\n"
20704                "  }\n"
20705                "};\n");
20706   verifyFormat("auto L = []<typename... T>(T...) {\n"
20707                "  {\n"
20708                "    f();\n"
20709                "    g();\n"
20710                "  }\n"
20711                "};\n");
20712   verifyFormat("auto L = []<template <typename...> class T>(T...) {\n"
20713                "  {\n"
20714                "    f();\n"
20715                "    g();\n"
20716                "  }\n"
20717                "};\n");
20718   verifyFormat("auto L = []</*comment*/ class... T>(T...) {\n"
20719                "  {\n"
20720                "    f();\n"
20721                "    g();\n"
20722                "  }\n"
20723                "};\n");
20724 
20725   // Multiple lambdas in the same parentheses change indentation rules. These
20726   // lambdas are forced to start on new lines.
20727   verifyFormat("SomeFunction(\n"
20728                "    []() {\n"
20729                "      //\n"
20730                "    },\n"
20731                "    []() {\n"
20732                "      //\n"
20733                "    });");
20734 
20735   // A lambda passed as arg0 is always pushed to the next line.
20736   verifyFormat("SomeFunction(\n"
20737                "    [this] {\n"
20738                "      //\n"
20739                "    },\n"
20740                "    1);\n");
20741 
20742   // A multi-line lambda passed as arg1 forces arg0 to be pushed out, just like
20743   // the arg0 case above.
20744   auto Style = getGoogleStyle();
20745   Style.BinPackArguments = false;
20746   verifyFormat("SomeFunction(\n"
20747                "    a,\n"
20748                "    [this] {\n"
20749                "      //\n"
20750                "    },\n"
20751                "    b);\n",
20752                Style);
20753   verifyFormat("SomeFunction(\n"
20754                "    a,\n"
20755                "    [this] {\n"
20756                "      //\n"
20757                "    },\n"
20758                "    b);\n");
20759 
20760   // A lambda with a very long line forces arg0 to be pushed out irrespective of
20761   // the BinPackArguments value (as long as the code is wide enough).
20762   verifyFormat(
20763       "something->SomeFunction(\n"
20764       "    a,\n"
20765       "    [this] {\n"
20766       "      "
20767       "D0000000000000000000000000000000000000000000000000000000000001();\n"
20768       "    },\n"
20769       "    b);\n");
20770 
20771   // A multi-line lambda is pulled up as long as the introducer fits on the
20772   // previous line and there are no further args.
20773   verifyFormat("function(1, [this, that] {\n"
20774                "  //\n"
20775                "});\n");
20776   verifyFormat("function([this, that] {\n"
20777                "  //\n"
20778                "});\n");
20779   // FIXME: this format is not ideal and we should consider forcing the first
20780   // arg onto its own line.
20781   verifyFormat("function(a, b, c, //\n"
20782                "         d, [this, that] {\n"
20783                "           //\n"
20784                "         });\n");
20785 
20786   // Multiple lambdas are treated correctly even when there is a short arg0.
20787   verifyFormat("SomeFunction(\n"
20788                "    1,\n"
20789                "    [this] {\n"
20790                "      //\n"
20791                "    },\n"
20792                "    [this] {\n"
20793                "      //\n"
20794                "    },\n"
20795                "    1);\n");
20796 
20797   // More complex introducers.
20798   verifyFormat("return [i, args...] {};");
20799 
20800   // Not lambdas.
20801   verifyFormat("constexpr char hello[]{\"hello\"};");
20802   verifyFormat("double &operator[](int i) { return 0; }\n"
20803                "int i;");
20804   verifyFormat("std::unique_ptr<int[]> foo() {}");
20805   verifyFormat("int i = a[a][a]->f();");
20806   verifyFormat("int i = (*b)[a]->f();");
20807 
20808   // Other corner cases.
20809   verifyFormat("void f() {\n"
20810                "  bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
20811                "  );\n"
20812                "}");
20813 
20814   // Lambdas created through weird macros.
20815   verifyFormat("void f() {\n"
20816                "  MACRO((const AA &a) { return 1; });\n"
20817                "  MACRO((AA &a) { return 1; });\n"
20818                "}");
20819 
20820   verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
20821                "      doo_dah();\n"
20822                "      doo_dah();\n"
20823                "    })) {\n"
20824                "}");
20825   verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n"
20826                "                doo_dah();\n"
20827                "                doo_dah();\n"
20828                "              })) {\n"
20829                "}");
20830   verifyFormat("if CONSTEXPR (blah_blah(whatever, whatever, [] {\n"
20831                "                doo_dah();\n"
20832                "                doo_dah();\n"
20833                "              })) {\n"
20834                "}");
20835   verifyFormat("auto lambda = []() {\n"
20836                "  int a = 2\n"
20837                "#if A\n"
20838                "          + 2\n"
20839                "#endif\n"
20840                "      ;\n"
20841                "};");
20842 
20843   // Lambdas with complex multiline introducers.
20844   verifyFormat(
20845       "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
20846       "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n"
20847       "        -> ::std::unordered_set<\n"
20848       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
20849       "      //\n"
20850       "    });");
20851 
20852   FormatStyle DoNotMerge = getLLVMStyle();
20853   DoNotMerge.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
20854   verifyFormat("auto c = []() {\n"
20855                "  return b;\n"
20856                "};",
20857                "auto c = []() { return b; };", DoNotMerge);
20858   verifyFormat("auto c = []() {\n"
20859                "};",
20860                " auto c = []() {};", DoNotMerge);
20861 
20862   FormatStyle MergeEmptyOnly = getLLVMStyle();
20863   MergeEmptyOnly.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Empty;
20864   verifyFormat("auto c = []() {\n"
20865                "  return b;\n"
20866                "};",
20867                "auto c = []() {\n"
20868                "  return b;\n"
20869                " };",
20870                MergeEmptyOnly);
20871   verifyFormat("auto c = []() {};",
20872                "auto c = []() {\n"
20873                "};",
20874                MergeEmptyOnly);
20875 
20876   FormatStyle MergeInline = getLLVMStyle();
20877   MergeInline.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Inline;
20878   verifyFormat("auto c = []() {\n"
20879                "  return b;\n"
20880                "};",
20881                "auto c = []() { return b; };", MergeInline);
20882   verifyFormat("function([]() { return b; })", "function([]() { return b; })",
20883                MergeInline);
20884   verifyFormat("function([]() { return b; }, a)",
20885                "function([]() { return b; }, a)", MergeInline);
20886   verifyFormat("function(a, []() { return b; })",
20887                "function(a, []() { return b; })", MergeInline);
20888 
20889   // Check option "BraceWrapping.BeforeLambdaBody" and different state of
20890   // AllowShortLambdasOnASingleLine
20891   FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle();
20892   LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
20893   LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
20894   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
20895       FormatStyle::ShortLambdaStyle::SLS_None;
20896   verifyFormat("FctWithOneNestedLambdaInline_SLS_None(\n"
20897                "    []()\n"
20898                "    {\n"
20899                "      return 17;\n"
20900                "    });",
20901                LLVMWithBeforeLambdaBody);
20902   verifyFormat("FctWithOneNestedLambdaEmpty_SLS_None(\n"
20903                "    []()\n"
20904                "    {\n"
20905                "    });",
20906                LLVMWithBeforeLambdaBody);
20907   verifyFormat("auto fct_SLS_None = []()\n"
20908                "{\n"
20909                "  return 17;\n"
20910                "};",
20911                LLVMWithBeforeLambdaBody);
20912   verifyFormat("TwoNestedLambdas_SLS_None(\n"
20913                "    []()\n"
20914                "    {\n"
20915                "      return Call(\n"
20916                "          []()\n"
20917                "          {\n"
20918                "            return 17;\n"
20919                "          });\n"
20920                "    });",
20921                LLVMWithBeforeLambdaBody);
20922   verifyFormat("void Fct() {\n"
20923                "  return {[]()\n"
20924                "          {\n"
20925                "            return 17;\n"
20926                "          }};\n"
20927                "}",
20928                LLVMWithBeforeLambdaBody);
20929 
20930   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
20931       FormatStyle::ShortLambdaStyle::SLS_Empty;
20932   verifyFormat("FctWithOneNestedLambdaInline_SLS_Empty(\n"
20933                "    []()\n"
20934                "    {\n"
20935                "      return 17;\n"
20936                "    });",
20937                LLVMWithBeforeLambdaBody);
20938   verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Empty([]() {});",
20939                LLVMWithBeforeLambdaBody);
20940   verifyFormat("FctWithOneNestedLambdaEmptyInsideAVeryVeryVeryVeryVeryVeryVeryL"
20941                "ongFunctionName_SLS_Empty(\n"
20942                "    []() {});",
20943                LLVMWithBeforeLambdaBody);
20944   verifyFormat("FctWithMultipleParams_SLS_Empty(A, B,\n"
20945                "                                []()\n"
20946                "                                {\n"
20947                "                                  return 17;\n"
20948                "                                });",
20949                LLVMWithBeforeLambdaBody);
20950   verifyFormat("auto fct_SLS_Empty = []()\n"
20951                "{\n"
20952                "  return 17;\n"
20953                "};",
20954                LLVMWithBeforeLambdaBody);
20955   verifyFormat("TwoNestedLambdas_SLS_Empty(\n"
20956                "    []()\n"
20957                "    {\n"
20958                "      return Call([]() {});\n"
20959                "    });",
20960                LLVMWithBeforeLambdaBody);
20961   verifyFormat("TwoNestedLambdas_SLS_Empty(A,\n"
20962                "                           []()\n"
20963                "                           {\n"
20964                "                             return Call([]() {});\n"
20965                "                           });",
20966                LLVMWithBeforeLambdaBody);
20967   verifyFormat(
20968       "FctWithLongLineInLambda_SLS_Empty(\n"
20969       "    []()\n"
20970       "    {\n"
20971       "      return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
20972       "                               AndShouldNotBeConsiderAsInline,\n"
20973       "                               LambdaBodyMustBeBreak);\n"
20974       "    });",
20975       LLVMWithBeforeLambdaBody);
20976 
20977   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
20978       FormatStyle::ShortLambdaStyle::SLS_Inline;
20979   verifyFormat("FctWithOneNestedLambdaInline_SLS_Inline([]() { return 17; });",
20980                LLVMWithBeforeLambdaBody);
20981   verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Inline([]() {});",
20982                LLVMWithBeforeLambdaBody);
20983   verifyFormat("auto fct_SLS_Inline = []()\n"
20984                "{\n"
20985                "  return 17;\n"
20986                "};",
20987                LLVMWithBeforeLambdaBody);
20988   verifyFormat("TwoNestedLambdas_SLS_Inline([]() { return Call([]() { return "
20989                "17; }); });",
20990                LLVMWithBeforeLambdaBody);
20991   verifyFormat(
20992       "FctWithLongLineInLambda_SLS_Inline(\n"
20993       "    []()\n"
20994       "    {\n"
20995       "      return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
20996       "                               AndShouldNotBeConsiderAsInline,\n"
20997       "                               LambdaBodyMustBeBreak);\n"
20998       "    });",
20999       LLVMWithBeforeLambdaBody);
21000   verifyFormat("FctWithMultipleParams_SLS_Inline("
21001                "VeryLongParameterThatShouldAskToBeOnMultiLine,\n"
21002                "                                 []() { return 17; });",
21003                LLVMWithBeforeLambdaBody);
21004   verifyFormat(
21005       "FctWithMultipleParams_SLS_Inline(FirstParam, []() { return 17; });",
21006       LLVMWithBeforeLambdaBody);
21007 
21008   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
21009       FormatStyle::ShortLambdaStyle::SLS_All;
21010   verifyFormat("FctWithOneNestedLambdaInline_SLS_All([]() { return 17; });",
21011                LLVMWithBeforeLambdaBody);
21012   verifyFormat("FctWithOneNestedLambdaEmpty_SLS_All([]() {});",
21013                LLVMWithBeforeLambdaBody);
21014   verifyFormat("auto fct_SLS_All = []() { return 17; };",
21015                LLVMWithBeforeLambdaBody);
21016   verifyFormat("FctWithOneParam_SLS_All(\n"
21017                "    []()\n"
21018                "    {\n"
21019                "      // A cool function...\n"
21020                "      return 43;\n"
21021                "    });",
21022                LLVMWithBeforeLambdaBody);
21023   verifyFormat("FctWithMultipleParams_SLS_All("
21024                "VeryLongParameterThatShouldAskToBeOnMultiLine,\n"
21025                "                              []() { return 17; });",
21026                LLVMWithBeforeLambdaBody);
21027   verifyFormat("FctWithMultipleParams_SLS_All(A, []() { return 17; });",
21028                LLVMWithBeforeLambdaBody);
21029   verifyFormat("FctWithMultipleParams_SLS_All(A, B, []() { return 17; });",
21030                LLVMWithBeforeLambdaBody);
21031   verifyFormat(
21032       "FctWithLongLineInLambda_SLS_All(\n"
21033       "    []()\n"
21034       "    {\n"
21035       "      return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
21036       "                               AndShouldNotBeConsiderAsInline,\n"
21037       "                               LambdaBodyMustBeBreak);\n"
21038       "    });",
21039       LLVMWithBeforeLambdaBody);
21040   verifyFormat(
21041       "auto fct_SLS_All = []()\n"
21042       "{\n"
21043       "  return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
21044       "                           AndShouldNotBeConsiderAsInline,\n"
21045       "                           LambdaBodyMustBeBreak);\n"
21046       "};",
21047       LLVMWithBeforeLambdaBody);
21048   LLVMWithBeforeLambdaBody.BinPackParameters = false;
21049   verifyFormat("FctAllOnSameLine_SLS_All([]() { return S; }, Fst, Second);",
21050                LLVMWithBeforeLambdaBody);
21051   verifyFormat(
21052       "FctWithLongLineInLambda_SLS_All([]() { return SomeValueNotSoLong; },\n"
21053       "                                FirstParam,\n"
21054       "                                SecondParam,\n"
21055       "                                ThirdParam,\n"
21056       "                                FourthParam);",
21057       LLVMWithBeforeLambdaBody);
21058   verifyFormat("FctWithLongLineInLambda_SLS_All(\n"
21059                "    []() { return "
21060                "SomeValueVeryVeryVeryVeryVeryVeryVeryVeryVeryLong; },\n"
21061                "    FirstParam,\n"
21062                "    SecondParam,\n"
21063                "    ThirdParam,\n"
21064                "    FourthParam);",
21065                LLVMWithBeforeLambdaBody);
21066   verifyFormat(
21067       "FctWithLongLineInLambda_SLS_All(FirstParam,\n"
21068       "                                SecondParam,\n"
21069       "                                ThirdParam,\n"
21070       "                                FourthParam,\n"
21071       "                                []() { return SomeValueNotSoLong; });",
21072       LLVMWithBeforeLambdaBody);
21073   verifyFormat("FctWithLongLineInLambda_SLS_All(\n"
21074                "    []()\n"
21075                "    {\n"
21076                "      return "
21077                "HereAVeryLongLineThatWillBeFormattedOnMultipleLineAndShouldNotB"
21078                "eConsiderAsInline;\n"
21079                "    });",
21080                LLVMWithBeforeLambdaBody);
21081   verifyFormat(
21082       "FctWithLongLineInLambda_SLS_All(\n"
21083       "    []()\n"
21084       "    {\n"
21085       "      return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
21086       "                               AndShouldNotBeConsiderAsInline,\n"
21087       "                               LambdaBodyMustBeBreak);\n"
21088       "    });",
21089       LLVMWithBeforeLambdaBody);
21090   verifyFormat("FctWithTwoParams_SLS_All(\n"
21091                "    []()\n"
21092                "    {\n"
21093                "      // A cool function...\n"
21094                "      return 43;\n"
21095                "    },\n"
21096                "    87);",
21097                LLVMWithBeforeLambdaBody);
21098   verifyFormat("FctWithTwoParams_SLS_All([]() { return 43; }, 87);",
21099                LLVMWithBeforeLambdaBody);
21100   verifyFormat("FctWithOneNestedLambdas_SLS_All([]() { return 17; });",
21101                LLVMWithBeforeLambdaBody);
21102   verifyFormat(
21103       "TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; }); });",
21104       LLVMWithBeforeLambdaBody);
21105   verifyFormat("TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; "
21106                "}); }, x);",
21107                LLVMWithBeforeLambdaBody);
21108   verifyFormat("TwoNestedLambdas_SLS_All(\n"
21109                "    []()\n"
21110                "    {\n"
21111                "      // A cool function...\n"
21112                "      return Call([]() { return 17; });\n"
21113                "    });",
21114                LLVMWithBeforeLambdaBody);
21115   verifyFormat("TwoNestedLambdas_SLS_All(\n"
21116                "    []()\n"
21117                "    {\n"
21118                "      return Call(\n"
21119                "          []()\n"
21120                "          {\n"
21121                "            // A cool function...\n"
21122                "            return 17;\n"
21123                "          });\n"
21124                "    });",
21125                LLVMWithBeforeLambdaBody);
21126 
21127   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
21128       FormatStyle::ShortLambdaStyle::SLS_None;
21129 
21130   verifyFormat("auto select = [this]() -> const Library::Object *\n"
21131                "{\n"
21132                "  return MyAssignment::SelectFromList(this);\n"
21133                "};\n",
21134                LLVMWithBeforeLambdaBody);
21135 
21136   verifyFormat("auto select = [this]() -> const Library::Object &\n"
21137                "{\n"
21138                "  return MyAssignment::SelectFromList(this);\n"
21139                "};\n",
21140                LLVMWithBeforeLambdaBody);
21141 
21142   verifyFormat("auto select = [this]() -> std::unique_ptr<Object>\n"
21143                "{\n"
21144                "  return MyAssignment::SelectFromList(this);\n"
21145                "};\n",
21146                LLVMWithBeforeLambdaBody);
21147 
21148   verifyFormat("namespace test {\n"
21149                "class Test {\n"
21150                "public:\n"
21151                "  Test() = default;\n"
21152                "};\n"
21153                "} // namespace test",
21154                LLVMWithBeforeLambdaBody);
21155 
21156   // Lambdas with different indentation styles.
21157   Style = getLLVMStyleWithColumns(100);
21158   EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n"
21159             "  return promise.then(\n"
21160             "      [this, &someVariable, someObject = "
21161             "std::mv(s)](std::vector<int> evaluated) mutable {\n"
21162             "        return someObject.startAsyncAction().then(\n"
21163             "            [this, &someVariable](AsyncActionResult result) "
21164             "mutable { result.processMore(); });\n"
21165             "      });\n"
21166             "}\n",
21167             format("SomeResult doSomething(SomeObject promise) {\n"
21168                    "  return promise.then([this, &someVariable, someObject = "
21169                    "std::mv(s)](std::vector<int> evaluated) mutable {\n"
21170                    "    return someObject.startAsyncAction().then([this, "
21171                    "&someVariable](AsyncActionResult result) mutable {\n"
21172                    "      result.processMore();\n"
21173                    "    });\n"
21174                    "  });\n"
21175                    "}\n",
21176                    Style));
21177   Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope;
21178   verifyFormat("test() {\n"
21179                "  ([]() -> {\n"
21180                "    int b = 32;\n"
21181                "    return 3;\n"
21182                "  }).foo();\n"
21183                "}",
21184                Style);
21185   verifyFormat("test() {\n"
21186                "  []() -> {\n"
21187                "    int b = 32;\n"
21188                "    return 3;\n"
21189                "  }\n"
21190                "}",
21191                Style);
21192   verifyFormat("std::sort(v.begin(), v.end(),\n"
21193                "          [](const auto &someLongArgumentName, const auto "
21194                "&someOtherLongArgumentName) {\n"
21195                "  return someLongArgumentName.someMemberVariable < "
21196                "someOtherLongArgumentName.someMemberVariable;\n"
21197                "});",
21198                Style);
21199   verifyFormat("test() {\n"
21200                "  (\n"
21201                "      []() -> {\n"
21202                "        int b = 32;\n"
21203                "        return 3;\n"
21204                "      },\n"
21205                "      foo, bar)\n"
21206                "      .foo();\n"
21207                "}",
21208                Style);
21209   verifyFormat("test() {\n"
21210                "  ([]() -> {\n"
21211                "    int b = 32;\n"
21212                "    return 3;\n"
21213                "  })\n"
21214                "      .foo()\n"
21215                "      .bar();\n"
21216                "}",
21217                Style);
21218   EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n"
21219             "  return promise.then(\n"
21220             "      [this, &someVariable, someObject = "
21221             "std::mv(s)](std::vector<int> evaluated) mutable {\n"
21222             "    return someObject.startAsyncAction().then(\n"
21223             "        [this, &someVariable](AsyncActionResult result) mutable { "
21224             "result.processMore(); });\n"
21225             "  });\n"
21226             "}\n",
21227             format("SomeResult doSomething(SomeObject promise) {\n"
21228                    "  return promise.then([this, &someVariable, someObject = "
21229                    "std::mv(s)](std::vector<int> evaluated) mutable {\n"
21230                    "    return someObject.startAsyncAction().then([this, "
21231                    "&someVariable](AsyncActionResult result) mutable {\n"
21232                    "      result.processMore();\n"
21233                    "    });\n"
21234                    "  });\n"
21235                    "}\n",
21236                    Style));
21237   EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n"
21238             "  return promise.then([this, &someVariable] {\n"
21239             "    return someObject.startAsyncAction().then(\n"
21240             "        [this, &someVariable](AsyncActionResult result) mutable { "
21241             "result.processMore(); });\n"
21242             "  });\n"
21243             "}\n",
21244             format("SomeResult doSomething(SomeObject promise) {\n"
21245                    "  return promise.then([this, &someVariable] {\n"
21246                    "    return someObject.startAsyncAction().then([this, "
21247                    "&someVariable](AsyncActionResult result) mutable {\n"
21248                    "      result.processMore();\n"
21249                    "    });\n"
21250                    "  });\n"
21251                    "}\n",
21252                    Style));
21253   Style = getGoogleStyle();
21254   Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope;
21255   EXPECT_EQ("#define A                                       \\\n"
21256             "  [] {                                          \\\n"
21257             "    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(        \\\n"
21258             "        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
21259             "      }",
21260             format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
21261                    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
21262                    Style));
21263   // TODO: The current formatting has a minor issue that's not worth fixing
21264   // right now whereby the closing brace is indented relative to the signature
21265   // instead of being aligned. This only happens with macros.
21266 }
21267 
21268 TEST_F(FormatTest, LambdaWithLineComments) {
21269   FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle();
21270   LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
21271   LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
21272   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
21273       FormatStyle::ShortLambdaStyle::SLS_All;
21274 
21275   verifyFormat("auto k = []() { return; }", LLVMWithBeforeLambdaBody);
21276   verifyFormat("auto k = []() // comment\n"
21277                "{ return; }",
21278                LLVMWithBeforeLambdaBody);
21279   verifyFormat("auto k = []() /* comment */ { return; }",
21280                LLVMWithBeforeLambdaBody);
21281   verifyFormat("auto k = []() /* comment */ /* comment */ { return; }",
21282                LLVMWithBeforeLambdaBody);
21283   verifyFormat("auto k = []() // X\n"
21284                "{ return; }",
21285                LLVMWithBeforeLambdaBody);
21286   verifyFormat(
21287       "auto k = []() // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"
21288       "{ return; }",
21289       LLVMWithBeforeLambdaBody);
21290 }
21291 
21292 TEST_F(FormatTest, EmptyLinesInLambdas) {
21293   verifyFormat("auto lambda = []() {\n"
21294                "  x(); //\n"
21295                "};",
21296                "auto lambda = []() {\n"
21297                "\n"
21298                "  x(); //\n"
21299                "\n"
21300                "};");
21301 }
21302 
21303 TEST_F(FormatTest, FormatsBlocks) {
21304   FormatStyle ShortBlocks = getLLVMStyle();
21305   ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
21306   verifyFormat("int (^Block)(int, int);", ShortBlocks);
21307   verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
21308   verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
21309   verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks);
21310   verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks);
21311   verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks);
21312 
21313   verifyFormat("foo(^{ bar(); });", ShortBlocks);
21314   verifyFormat("foo(a, ^{ bar(); });", ShortBlocks);
21315   verifyFormat("{ void (^block)(Object *x); }", ShortBlocks);
21316 
21317   verifyFormat("[operation setCompletionBlock:^{\n"
21318                "  [self onOperationDone];\n"
21319                "}];");
21320   verifyFormat("int i = {[operation setCompletionBlock:^{\n"
21321                "  [self onOperationDone];\n"
21322                "}]};");
21323   verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
21324                "  f();\n"
21325                "}];");
21326   verifyFormat("int a = [operation block:^int(int *i) {\n"
21327                "  return 1;\n"
21328                "}];");
21329   verifyFormat("[myObject doSomethingWith:arg1\n"
21330                "                      aaa:^int(int *a) {\n"
21331                "                        return 1;\n"
21332                "                      }\n"
21333                "                      bbb:f(a * bbbbbbbb)];");
21334 
21335   verifyFormat("[operation setCompletionBlock:^{\n"
21336                "  [self.delegate newDataAvailable];\n"
21337                "}];",
21338                getLLVMStyleWithColumns(60));
21339   verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
21340                "  NSString *path = [self sessionFilePath];\n"
21341                "  if (path) {\n"
21342                "    // ...\n"
21343                "  }\n"
21344                "});");
21345   verifyFormat("[[SessionService sharedService]\n"
21346                "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
21347                "      if (window) {\n"
21348                "        [self windowDidLoad:window];\n"
21349                "      } else {\n"
21350                "        [self errorLoadingWindow];\n"
21351                "      }\n"
21352                "    }];");
21353   verifyFormat("void (^largeBlock)(void) = ^{\n"
21354                "  // ...\n"
21355                "};\n",
21356                getLLVMStyleWithColumns(40));
21357   verifyFormat("[[SessionService sharedService]\n"
21358                "    loadWindowWithCompletionBlock: //\n"
21359                "        ^(SessionWindow *window) {\n"
21360                "          if (window) {\n"
21361                "            [self windowDidLoad:window];\n"
21362                "          } else {\n"
21363                "            [self errorLoadingWindow];\n"
21364                "          }\n"
21365                "        }];",
21366                getLLVMStyleWithColumns(60));
21367   verifyFormat("[myObject doSomethingWith:arg1\n"
21368                "    firstBlock:^(Foo *a) {\n"
21369                "      // ...\n"
21370                "      int i;\n"
21371                "    }\n"
21372                "    secondBlock:^(Bar *b) {\n"
21373                "      // ...\n"
21374                "      int i;\n"
21375                "    }\n"
21376                "    thirdBlock:^Foo(Bar *b) {\n"
21377                "      // ...\n"
21378                "      int i;\n"
21379                "    }];");
21380   verifyFormat("[myObject doSomethingWith:arg1\n"
21381                "               firstBlock:-1\n"
21382                "              secondBlock:^(Bar *b) {\n"
21383                "                // ...\n"
21384                "                int i;\n"
21385                "              }];");
21386 
21387   verifyFormat("f(^{\n"
21388                "  @autoreleasepool {\n"
21389                "    if (a) {\n"
21390                "      g();\n"
21391                "    }\n"
21392                "  }\n"
21393                "});");
21394   verifyFormat("Block b = ^int *(A *a, B *b) {}");
21395   verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n"
21396                "};");
21397 
21398   FormatStyle FourIndent = getLLVMStyle();
21399   FourIndent.ObjCBlockIndentWidth = 4;
21400   verifyFormat("[operation setCompletionBlock:^{\n"
21401                "    [self onOperationDone];\n"
21402                "}];",
21403                FourIndent);
21404 }
21405 
21406 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) {
21407   FormatStyle ZeroColumn = getLLVMStyleWithColumns(0);
21408 
21409   verifyFormat("[[SessionService sharedService] "
21410                "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
21411                "  if (window) {\n"
21412                "    [self windowDidLoad:window];\n"
21413                "  } else {\n"
21414                "    [self errorLoadingWindow];\n"
21415                "  }\n"
21416                "}];",
21417                ZeroColumn);
21418   EXPECT_EQ("[[SessionService sharedService]\n"
21419             "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
21420             "      if (window) {\n"
21421             "        [self windowDidLoad:window];\n"
21422             "      } else {\n"
21423             "        [self errorLoadingWindow];\n"
21424             "      }\n"
21425             "    }];",
21426             format("[[SessionService sharedService]\n"
21427                    "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
21428                    "                if (window) {\n"
21429                    "    [self windowDidLoad:window];\n"
21430                    "  } else {\n"
21431                    "    [self errorLoadingWindow];\n"
21432                    "  }\n"
21433                    "}];",
21434                    ZeroColumn));
21435   verifyFormat("[myObject doSomethingWith:arg1\n"
21436                "    firstBlock:^(Foo *a) {\n"
21437                "      // ...\n"
21438                "      int i;\n"
21439                "    }\n"
21440                "    secondBlock:^(Bar *b) {\n"
21441                "      // ...\n"
21442                "      int i;\n"
21443                "    }\n"
21444                "    thirdBlock:^Foo(Bar *b) {\n"
21445                "      // ...\n"
21446                "      int i;\n"
21447                "    }];",
21448                ZeroColumn);
21449   verifyFormat("f(^{\n"
21450                "  @autoreleasepool {\n"
21451                "    if (a) {\n"
21452                "      g();\n"
21453                "    }\n"
21454                "  }\n"
21455                "});",
21456                ZeroColumn);
21457   verifyFormat("void (^largeBlock)(void) = ^{\n"
21458                "  // ...\n"
21459                "};",
21460                ZeroColumn);
21461 
21462   ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
21463   EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };",
21464             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
21465   ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
21466   EXPECT_EQ("void (^largeBlock)(void) = ^{\n"
21467             "  int i;\n"
21468             "};",
21469             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
21470 }
21471 
21472 TEST_F(FormatTest, SupportsCRLF) {
21473   EXPECT_EQ("int a;\r\n"
21474             "int b;\r\n"
21475             "int c;\r\n",
21476             format("int a;\r\n"
21477                    "  int b;\r\n"
21478                    "    int c;\r\n",
21479                    getLLVMStyle()));
21480   EXPECT_EQ("int a;\r\n"
21481             "int b;\r\n"
21482             "int c;\r\n",
21483             format("int a;\r\n"
21484                    "  int b;\n"
21485                    "    int c;\r\n",
21486                    getLLVMStyle()));
21487   EXPECT_EQ("int a;\n"
21488             "int b;\n"
21489             "int c;\n",
21490             format("int a;\r\n"
21491                    "  int b;\n"
21492                    "    int c;\n",
21493                    getLLVMStyle()));
21494   EXPECT_EQ("\"aaaaaaa \"\r\n"
21495             "\"bbbbbbb\";\r\n",
21496             format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
21497   EXPECT_EQ("#define A \\\r\n"
21498             "  b;      \\\r\n"
21499             "  c;      \\\r\n"
21500             "  d;\r\n",
21501             format("#define A \\\r\n"
21502                    "  b; \\\r\n"
21503                    "  c; d; \r\n",
21504                    getGoogleStyle()));
21505 
21506   EXPECT_EQ("/*\r\n"
21507             "multi line block comments\r\n"
21508             "should not introduce\r\n"
21509             "an extra carriage return\r\n"
21510             "*/\r\n",
21511             format("/*\r\n"
21512                    "multi line block comments\r\n"
21513                    "should not introduce\r\n"
21514                    "an extra carriage return\r\n"
21515                    "*/\r\n"));
21516   EXPECT_EQ("/*\r\n"
21517             "\r\n"
21518             "*/",
21519             format("/*\r\n"
21520                    "    \r\r\r\n"
21521                    "*/"));
21522 
21523   FormatStyle style = getLLVMStyle();
21524 
21525   style.DeriveLineEnding = true;
21526   style.UseCRLF = false;
21527   EXPECT_EQ("union FooBarBazQux {\n"
21528             "  int foo;\n"
21529             "  int bar;\n"
21530             "  int baz;\n"
21531             "};",
21532             format("union FooBarBazQux {\r\n"
21533                    "  int foo;\n"
21534                    "  int bar;\r\n"
21535                    "  int baz;\n"
21536                    "};",
21537                    style));
21538   style.UseCRLF = true;
21539   EXPECT_EQ("union FooBarBazQux {\r\n"
21540             "  int foo;\r\n"
21541             "  int bar;\r\n"
21542             "  int baz;\r\n"
21543             "};",
21544             format("union FooBarBazQux {\r\n"
21545                    "  int foo;\n"
21546                    "  int bar;\r\n"
21547                    "  int baz;\n"
21548                    "};",
21549                    style));
21550 
21551   style.DeriveLineEnding = false;
21552   style.UseCRLF = false;
21553   EXPECT_EQ("union FooBarBazQux {\n"
21554             "  int foo;\n"
21555             "  int bar;\n"
21556             "  int baz;\n"
21557             "  int qux;\n"
21558             "};",
21559             format("union FooBarBazQux {\r\n"
21560                    "  int foo;\n"
21561                    "  int bar;\r\n"
21562                    "  int baz;\n"
21563                    "  int qux;\r\n"
21564                    "};",
21565                    style));
21566   style.UseCRLF = true;
21567   EXPECT_EQ("union FooBarBazQux {\r\n"
21568             "  int foo;\r\n"
21569             "  int bar;\r\n"
21570             "  int baz;\r\n"
21571             "  int qux;\r\n"
21572             "};",
21573             format("union FooBarBazQux {\r\n"
21574                    "  int foo;\n"
21575                    "  int bar;\r\n"
21576                    "  int baz;\n"
21577                    "  int qux;\n"
21578                    "};",
21579                    style));
21580 
21581   style.DeriveLineEnding = true;
21582   style.UseCRLF = false;
21583   EXPECT_EQ("union FooBarBazQux {\r\n"
21584             "  int foo;\r\n"
21585             "  int bar;\r\n"
21586             "  int baz;\r\n"
21587             "  int qux;\r\n"
21588             "};",
21589             format("union FooBarBazQux {\r\n"
21590                    "  int foo;\n"
21591                    "  int bar;\r\n"
21592                    "  int baz;\n"
21593                    "  int qux;\r\n"
21594                    "};",
21595                    style));
21596   style.UseCRLF = true;
21597   EXPECT_EQ("union FooBarBazQux {\n"
21598             "  int foo;\n"
21599             "  int bar;\n"
21600             "  int baz;\n"
21601             "  int qux;\n"
21602             "};",
21603             format("union FooBarBazQux {\r\n"
21604                    "  int foo;\n"
21605                    "  int bar;\r\n"
21606                    "  int baz;\n"
21607                    "  int qux;\n"
21608                    "};",
21609                    style));
21610 }
21611 
21612 TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
21613   verifyFormat("MY_CLASS(C) {\n"
21614                "  int i;\n"
21615                "  int j;\n"
21616                "};");
21617 }
21618 
21619 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
21620   FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
21621   TwoIndent.ContinuationIndentWidth = 2;
21622 
21623   EXPECT_EQ("int i =\n"
21624             "  longFunction(\n"
21625             "    arg);",
21626             format("int i = longFunction(arg);", TwoIndent));
21627 
21628   FormatStyle SixIndent = getLLVMStyleWithColumns(20);
21629   SixIndent.ContinuationIndentWidth = 6;
21630 
21631   EXPECT_EQ("int i =\n"
21632             "      longFunction(\n"
21633             "            arg);",
21634             format("int i = longFunction(arg);", SixIndent));
21635 }
21636 
21637 TEST_F(FormatTest, WrappedClosingParenthesisIndent) {
21638   FormatStyle Style = getLLVMStyle();
21639   verifyFormat("int Foo::getter(\n"
21640                "    //\n"
21641                ") const {\n"
21642                "  return foo;\n"
21643                "}",
21644                Style);
21645   verifyFormat("void Foo::setter(\n"
21646                "    //\n"
21647                ") {\n"
21648                "  foo = 1;\n"
21649                "}",
21650                Style);
21651 }
21652 
21653 TEST_F(FormatTest, SpacesInAngles) {
21654   FormatStyle Spaces = getLLVMStyle();
21655   Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
21656 
21657   verifyFormat("vector< ::std::string > x1;", Spaces);
21658   verifyFormat("Foo< int, Bar > x2;", Spaces);
21659   verifyFormat("Foo< ::int, ::Bar > x3;", Spaces);
21660 
21661   verifyFormat("static_cast< int >(arg);", Spaces);
21662   verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
21663   verifyFormat("f< int, float >();", Spaces);
21664   verifyFormat("template <> g() {}", Spaces);
21665   verifyFormat("template < std::vector< int > > f() {}", Spaces);
21666   verifyFormat("std::function< void(int, int) > fct;", Spaces);
21667   verifyFormat("void inFunction() { std::function< void(int, int) > fct; }",
21668                Spaces);
21669 
21670   Spaces.Standard = FormatStyle::LS_Cpp03;
21671   Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
21672   verifyFormat("A< A< int > >();", Spaces);
21673 
21674   Spaces.SpacesInAngles = FormatStyle::SIAS_Never;
21675   verifyFormat("A<A<int> >();", Spaces);
21676 
21677   Spaces.SpacesInAngles = FormatStyle::SIAS_Leave;
21678   verifyFormat("vector< ::std::string> x4;", "vector<::std::string> x4;",
21679                Spaces);
21680   verifyFormat("vector< ::std::string > x4;", "vector<::std::string > x4;",
21681                Spaces);
21682 
21683   verifyFormat("A<A<int> >();", Spaces);
21684   verifyFormat("A<A<int> >();", "A<A<int>>();", Spaces);
21685   verifyFormat("A< A< int > >();", Spaces);
21686 
21687   Spaces.Standard = FormatStyle::LS_Cpp11;
21688   Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
21689   verifyFormat("A< A< int > >();", Spaces);
21690 
21691   Spaces.SpacesInAngles = FormatStyle::SIAS_Never;
21692   verifyFormat("vector<::std::string> x4;", Spaces);
21693   verifyFormat("vector<int> x5;", Spaces);
21694   verifyFormat("Foo<int, Bar> x6;", Spaces);
21695   verifyFormat("Foo<::int, ::Bar> x7;", Spaces);
21696 
21697   verifyFormat("A<A<int>>();", Spaces);
21698 
21699   Spaces.SpacesInAngles = FormatStyle::SIAS_Leave;
21700   verifyFormat("vector<::std::string> x4;", Spaces);
21701   verifyFormat("vector< ::std::string > x4;", Spaces);
21702   verifyFormat("vector<int> x5;", Spaces);
21703   verifyFormat("vector< int > x5;", Spaces);
21704   verifyFormat("Foo<int, Bar> x6;", Spaces);
21705   verifyFormat("Foo< int, Bar > x6;", Spaces);
21706   verifyFormat("Foo<::int, ::Bar> x7;", Spaces);
21707   verifyFormat("Foo< ::int, ::Bar > x7;", Spaces);
21708 
21709   verifyFormat("A<A<int>>();", Spaces);
21710   verifyFormat("A< A< int > >();", Spaces);
21711   verifyFormat("A<A<int > >();", Spaces);
21712   verifyFormat("A< A< int>>();", Spaces);
21713 
21714   Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
21715   verifyFormat("// clang-format off\n"
21716                "foo<<<1, 1>>>();\n"
21717                "// clang-format on\n",
21718                Spaces);
21719   verifyFormat("// clang-format off\n"
21720                "foo< < <1, 1> > >();\n"
21721                "// clang-format on\n",
21722                Spaces);
21723 }
21724 
21725 TEST_F(FormatTest, SpaceAfterTemplateKeyword) {
21726   FormatStyle Style = getLLVMStyle();
21727   Style.SpaceAfterTemplateKeyword = false;
21728   verifyFormat("template<int> void foo();", Style);
21729 }
21730 
21731 TEST_F(FormatTest, TripleAngleBrackets) {
21732   verifyFormat("f<<<1, 1>>>();");
21733   verifyFormat("f<<<1, 1, 1, s>>>();");
21734   verifyFormat("f<<<a, b, c, d>>>();");
21735   EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();"));
21736   verifyFormat("f<param><<<1, 1>>>();");
21737   verifyFormat("f<1><<<1, 1>>>();");
21738   EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();"));
21739   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
21740                "aaaaaaaaaaa<<<\n    1, 1>>>();");
21741   verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n"
21742                "    <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();");
21743 }
21744 
21745 TEST_F(FormatTest, MergeLessLessAtEnd) {
21746   verifyFormat("<<");
21747   EXPECT_EQ("< < <", format("\\\n<<<"));
21748   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
21749                "aaallvm::outs() <<");
21750   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
21751                "aaaallvm::outs()\n    <<");
21752 }
21753 
21754 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
21755   std::string code = "#if A\n"
21756                      "#if B\n"
21757                      "a.\n"
21758                      "#endif\n"
21759                      "    a = 1;\n"
21760                      "#else\n"
21761                      "#endif\n"
21762                      "#if C\n"
21763                      "#else\n"
21764                      "#endif\n";
21765   EXPECT_EQ(code, format(code));
21766 }
21767 
21768 TEST_F(FormatTest, HandleConflictMarkers) {
21769   // Git/SVN conflict markers.
21770   EXPECT_EQ("int a;\n"
21771             "void f() {\n"
21772             "  callme(some(parameter1,\n"
21773             "<<<<<<< text by the vcs\n"
21774             "              parameter2),\n"
21775             "||||||| text by the vcs\n"
21776             "              parameter2),\n"
21777             "         parameter3,\n"
21778             "======= text by the vcs\n"
21779             "              parameter2, parameter3),\n"
21780             ">>>>>>> text by the vcs\n"
21781             "         otherparameter);\n",
21782             format("int a;\n"
21783                    "void f() {\n"
21784                    "  callme(some(parameter1,\n"
21785                    "<<<<<<< text by the vcs\n"
21786                    "  parameter2),\n"
21787                    "||||||| text by the vcs\n"
21788                    "  parameter2),\n"
21789                    "  parameter3,\n"
21790                    "======= text by the vcs\n"
21791                    "  parameter2,\n"
21792                    "  parameter3),\n"
21793                    ">>>>>>> text by the vcs\n"
21794                    "  otherparameter);\n"));
21795 
21796   // Perforce markers.
21797   EXPECT_EQ("void f() {\n"
21798             "  function(\n"
21799             ">>>> text by the vcs\n"
21800             "      parameter,\n"
21801             "==== text by the vcs\n"
21802             "      parameter,\n"
21803             "==== text by the vcs\n"
21804             "      parameter,\n"
21805             "<<<< text by the vcs\n"
21806             "      parameter);\n",
21807             format("void f() {\n"
21808                    "  function(\n"
21809                    ">>>> text by the vcs\n"
21810                    "  parameter,\n"
21811                    "==== text by the vcs\n"
21812                    "  parameter,\n"
21813                    "==== text by the vcs\n"
21814                    "  parameter,\n"
21815                    "<<<< text by the vcs\n"
21816                    "  parameter);\n"));
21817 
21818   EXPECT_EQ("<<<<<<<\n"
21819             "|||||||\n"
21820             "=======\n"
21821             ">>>>>>>",
21822             format("<<<<<<<\n"
21823                    "|||||||\n"
21824                    "=======\n"
21825                    ">>>>>>>"));
21826 
21827   EXPECT_EQ("<<<<<<<\n"
21828             "|||||||\n"
21829             "int i;\n"
21830             "=======\n"
21831             ">>>>>>>",
21832             format("<<<<<<<\n"
21833                    "|||||||\n"
21834                    "int i;\n"
21835                    "=======\n"
21836                    ">>>>>>>"));
21837 
21838   // FIXME: Handle parsing of macros around conflict markers correctly:
21839   EXPECT_EQ("#define Macro \\\n"
21840             "<<<<<<<\n"
21841             "Something \\\n"
21842             "|||||||\n"
21843             "Else \\\n"
21844             "=======\n"
21845             "Other \\\n"
21846             ">>>>>>>\n"
21847             "    End int i;\n",
21848             format("#define Macro \\\n"
21849                    "<<<<<<<\n"
21850                    "  Something \\\n"
21851                    "|||||||\n"
21852                    "  Else \\\n"
21853                    "=======\n"
21854                    "  Other \\\n"
21855                    ">>>>>>>\n"
21856                    "  End\n"
21857                    "int i;\n"));
21858 
21859   verifyFormat(R"(====
21860 #ifdef A
21861 a
21862 #else
21863 b
21864 #endif
21865 )");
21866 }
21867 
21868 TEST_F(FormatTest, DisableRegions) {
21869   EXPECT_EQ("int i;\n"
21870             "// clang-format off\n"
21871             "  int j;\n"
21872             "// clang-format on\n"
21873             "int k;",
21874             format(" int  i;\n"
21875                    "   // clang-format off\n"
21876                    "  int j;\n"
21877                    " // clang-format on\n"
21878                    "   int   k;"));
21879   EXPECT_EQ("int i;\n"
21880             "/* clang-format off */\n"
21881             "  int j;\n"
21882             "/* clang-format on */\n"
21883             "int k;",
21884             format(" int  i;\n"
21885                    "   /* clang-format off */\n"
21886                    "  int j;\n"
21887                    " /* clang-format on */\n"
21888                    "   int   k;"));
21889 
21890   // Don't reflow comments within disabled regions.
21891   EXPECT_EQ("// clang-format off\n"
21892             "// long long long long long long line\n"
21893             "/* clang-format on */\n"
21894             "/* long long long\n"
21895             " * long long long\n"
21896             " * line */\n"
21897             "int i;\n"
21898             "/* clang-format off */\n"
21899             "/* long long long long long long line */\n",
21900             format("// clang-format off\n"
21901                    "// long long long long long long line\n"
21902                    "/* clang-format on */\n"
21903                    "/* long long long long long long line */\n"
21904                    "int i;\n"
21905                    "/* clang-format off */\n"
21906                    "/* long long long long long long line */\n",
21907                    getLLVMStyleWithColumns(20)));
21908 }
21909 
21910 TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
21911   format("? ) =");
21912   verifyNoCrash("#define a\\\n /**/}");
21913 }
21914 
21915 TEST_F(FormatTest, FormatsTableGenCode) {
21916   FormatStyle Style = getLLVMStyle();
21917   Style.Language = FormatStyle::LK_TableGen;
21918   verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style);
21919 }
21920 
21921 TEST_F(FormatTest, ArrayOfTemplates) {
21922   EXPECT_EQ("auto a = new unique_ptr<int>[10];",
21923             format("auto a = new unique_ptr<int > [ 10];"));
21924 
21925   FormatStyle Spaces = getLLVMStyle();
21926   Spaces.SpacesInSquareBrackets = true;
21927   EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];",
21928             format("auto a = new unique_ptr<int > [10];", Spaces));
21929 }
21930 
21931 TEST_F(FormatTest, ArrayAsTemplateType) {
21932   EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;",
21933             format("auto a = unique_ptr < Foo < Bar>[ 10]> ;"));
21934 
21935   FormatStyle Spaces = getLLVMStyle();
21936   Spaces.SpacesInSquareBrackets = true;
21937   EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;",
21938             format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces));
21939 }
21940 
21941 TEST_F(FormatTest, NoSpaceAfterSuper) { verifyFormat("__super::FooBar();"); }
21942 
21943 TEST(FormatStyle, GetStyleWithEmptyFileName) {
21944   llvm::vfs::InMemoryFileSystem FS;
21945   auto Style1 = getStyle("file", "", "Google", "", &FS);
21946   ASSERT_TRUE((bool)Style1);
21947   ASSERT_EQ(*Style1, getGoogleStyle());
21948 }
21949 
21950 TEST(FormatStyle, GetStyleOfFile) {
21951   llvm::vfs::InMemoryFileSystem FS;
21952   // Test 1: format file in the same directory.
21953   ASSERT_TRUE(
21954       FS.addFile("/a/.clang-format", 0,
21955                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM")));
21956   ASSERT_TRUE(
21957       FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
21958   auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS);
21959   ASSERT_TRUE((bool)Style1);
21960   ASSERT_EQ(*Style1, getLLVMStyle());
21961 
21962   // Test 2.1: fallback to default.
21963   ASSERT_TRUE(
21964       FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
21965   auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS);
21966   ASSERT_TRUE((bool)Style2);
21967   ASSERT_EQ(*Style2, getMozillaStyle());
21968 
21969   // Test 2.2: no format on 'none' fallback style.
21970   Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
21971   ASSERT_TRUE((bool)Style2);
21972   ASSERT_EQ(*Style2, getNoStyle());
21973 
21974   // Test 2.3: format if config is found with no based style while fallback is
21975   // 'none'.
21976   ASSERT_TRUE(FS.addFile("/b/.clang-format", 0,
21977                          llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2")));
21978   Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
21979   ASSERT_TRUE((bool)Style2);
21980   ASSERT_EQ(*Style2, getLLVMStyle());
21981 
21982   // Test 2.4: format if yaml with no based style, while fallback is 'none'.
21983   Style2 = getStyle("{}", "a.h", "none", "", &FS);
21984   ASSERT_TRUE((bool)Style2);
21985   ASSERT_EQ(*Style2, getLLVMStyle());
21986 
21987   // Test 3: format file in parent directory.
21988   ASSERT_TRUE(
21989       FS.addFile("/c/.clang-format", 0,
21990                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google")));
21991   ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0,
21992                          llvm::MemoryBuffer::getMemBuffer("int i;")));
21993   auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS);
21994   ASSERT_TRUE((bool)Style3);
21995   ASSERT_EQ(*Style3, getGoogleStyle());
21996 
21997   // Test 4: error on invalid fallback style
21998   auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS);
21999   ASSERT_FALSE((bool)Style4);
22000   llvm::consumeError(Style4.takeError());
22001 
22002   // Test 5: error on invalid yaml on command line
22003   auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS);
22004   ASSERT_FALSE((bool)Style5);
22005   llvm::consumeError(Style5.takeError());
22006 
22007   // Test 6: error on invalid style
22008   auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS);
22009   ASSERT_FALSE((bool)Style6);
22010   llvm::consumeError(Style6.takeError());
22011 
22012   // Test 7: found config file, error on parsing it
22013   ASSERT_TRUE(
22014       FS.addFile("/d/.clang-format", 0,
22015                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n"
22016                                                   "InvalidKey: InvalidValue")));
22017   ASSERT_TRUE(
22018       FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
22019   auto Style7a = getStyle("file", "/d/.clang-format", "LLVM", "", &FS);
22020   ASSERT_FALSE((bool)Style7a);
22021   llvm::consumeError(Style7a.takeError());
22022 
22023   auto Style7b = getStyle("file", "/d/.clang-format", "LLVM", "", &FS, true);
22024   ASSERT_TRUE((bool)Style7b);
22025 
22026   // Test 8: inferred per-language defaults apply.
22027   auto StyleTd = getStyle("file", "x.td", "llvm", "", &FS);
22028   ASSERT_TRUE((bool)StyleTd);
22029   ASSERT_EQ(*StyleTd, getLLVMStyle(FormatStyle::LK_TableGen));
22030 
22031   // Test 9.1.1: overwriting a file style, when no parent file exists with no
22032   // fallback style.
22033   ASSERT_TRUE(FS.addFile(
22034       "/e/sub/.clang-format", 0,
22035       llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: InheritParentConfig\n"
22036                                        "ColumnLimit: 20")));
22037   ASSERT_TRUE(FS.addFile("/e/sub/code.cpp", 0,
22038                          llvm::MemoryBuffer::getMemBuffer("int i;")));
22039   auto Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS);
22040   ASSERT_TRUE(static_cast<bool>(Style9));
22041   ASSERT_EQ(*Style9, [] {
22042     auto Style = getNoStyle();
22043     Style.ColumnLimit = 20;
22044     return Style;
22045   }());
22046 
22047   // Test 9.1.2: propagate more than one level with no parent file.
22048   ASSERT_TRUE(FS.addFile("/e/sub/sub/code.cpp", 0,
22049                          llvm::MemoryBuffer::getMemBuffer("int i;")));
22050   ASSERT_TRUE(FS.addFile("/e/sub/sub/.clang-format", 0,
22051                          llvm::MemoryBuffer::getMemBuffer(
22052                              "BasedOnStyle: InheritParentConfig\n"
22053                              "WhitespaceSensitiveMacros: ['FOO', 'BAR']")));
22054   std::vector<std::string> NonDefaultWhiteSpaceMacros{"FOO", "BAR"};
22055 
22056   ASSERT_NE(Style9->WhitespaceSensitiveMacros, NonDefaultWhiteSpaceMacros);
22057   Style9 = getStyle("file", "/e/sub/sub/code.cpp", "none", "", &FS);
22058   ASSERT_TRUE(static_cast<bool>(Style9));
22059   ASSERT_EQ(*Style9, [&NonDefaultWhiteSpaceMacros] {
22060     auto Style = getNoStyle();
22061     Style.ColumnLimit = 20;
22062     Style.WhitespaceSensitiveMacros = NonDefaultWhiteSpaceMacros;
22063     return Style;
22064   }());
22065 
22066   // Test 9.2: with LLVM fallback style
22067   Style9 = getStyle("file", "/e/sub/code.cpp", "LLVM", "", &FS);
22068   ASSERT_TRUE(static_cast<bool>(Style9));
22069   ASSERT_EQ(*Style9, [] {
22070     auto Style = getLLVMStyle();
22071     Style.ColumnLimit = 20;
22072     return Style;
22073   }());
22074 
22075   // Test 9.3: with a parent file
22076   ASSERT_TRUE(
22077       FS.addFile("/e/.clang-format", 0,
22078                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google\n"
22079                                                   "UseTab: Always")));
22080   Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS);
22081   ASSERT_TRUE(static_cast<bool>(Style9));
22082   ASSERT_EQ(*Style9, [] {
22083     auto Style = getGoogleStyle();
22084     Style.ColumnLimit = 20;
22085     Style.UseTab = FormatStyle::UT_Always;
22086     return Style;
22087   }());
22088 
22089   // Test 9.4: propagate more than one level with a parent file.
22090   const auto SubSubStyle = [&NonDefaultWhiteSpaceMacros] {
22091     auto Style = getGoogleStyle();
22092     Style.ColumnLimit = 20;
22093     Style.UseTab = FormatStyle::UT_Always;
22094     Style.WhitespaceSensitiveMacros = NonDefaultWhiteSpaceMacros;
22095     return Style;
22096   }();
22097 
22098   ASSERT_NE(Style9->WhitespaceSensitiveMacros, NonDefaultWhiteSpaceMacros);
22099   Style9 = getStyle("file", "/e/sub/sub/code.cpp", "none", "", &FS);
22100   ASSERT_TRUE(static_cast<bool>(Style9));
22101   ASSERT_EQ(*Style9, SubSubStyle);
22102 
22103   // Test 9.5: use InheritParentConfig as style name
22104   Style9 =
22105       getStyle("inheritparentconfig", "/e/sub/sub/code.cpp", "none", "", &FS);
22106   ASSERT_TRUE(static_cast<bool>(Style9));
22107   ASSERT_EQ(*Style9, SubSubStyle);
22108 
22109   // Test 9.6: use command line style with inheritance
22110   Style9 = getStyle("{BasedOnStyle: InheritParentConfig}", "/e/sub/code.cpp",
22111                     "none", "", &FS);
22112   ASSERT_TRUE(static_cast<bool>(Style9));
22113   ASSERT_EQ(*Style9, SubSubStyle);
22114 
22115   // Test 9.7: use command line style with inheritance and own config
22116   Style9 = getStyle("{BasedOnStyle: InheritParentConfig, "
22117                     "WhitespaceSensitiveMacros: ['FOO', 'BAR']}",
22118                     "/e/sub/code.cpp", "none", "", &FS);
22119   ASSERT_TRUE(static_cast<bool>(Style9));
22120   ASSERT_EQ(*Style9, SubSubStyle);
22121 
22122   // Test 9.8: use inheritance from a file without BasedOnStyle
22123   ASSERT_TRUE(FS.addFile("/e/withoutbase/.clang-format", 0,
22124                          llvm::MemoryBuffer::getMemBuffer("ColumnLimit: 123")));
22125   ASSERT_TRUE(
22126       FS.addFile("/e/withoutbase/sub/.clang-format", 0,
22127                  llvm::MemoryBuffer::getMemBuffer(
22128                      "BasedOnStyle: InheritParentConfig\nIndentWidth: 7")));
22129   // Make sure we do not use the fallback style
22130   Style9 = getStyle("file", "/e/withoutbase/code.cpp", "google", "", &FS);
22131   ASSERT_TRUE(static_cast<bool>(Style9));
22132   ASSERT_EQ(*Style9, [] {
22133     auto Style = getLLVMStyle();
22134     Style.ColumnLimit = 123;
22135     return Style;
22136   }());
22137 
22138   Style9 = getStyle("file", "/e/withoutbase/sub/code.cpp", "google", "", &FS);
22139   ASSERT_TRUE(static_cast<bool>(Style9));
22140   ASSERT_EQ(*Style9, [] {
22141     auto Style = getLLVMStyle();
22142     Style.ColumnLimit = 123;
22143     Style.IndentWidth = 7;
22144     return Style;
22145   }());
22146 
22147   // Test 9.9: use inheritance from a specific config file.
22148   Style9 = getStyle("file:/e/sub/sub/.clang-format", "/e/sub/sub/code.cpp",
22149                     "none", "", &FS);
22150   ASSERT_TRUE(static_cast<bool>(Style9));
22151   ASSERT_EQ(*Style9, SubSubStyle);
22152 }
22153 
22154 TEST(FormatStyle, GetStyleOfSpecificFile) {
22155   llvm::vfs::InMemoryFileSystem FS;
22156   // Specify absolute path to a format file in a parent directory.
22157   ASSERT_TRUE(
22158       FS.addFile("/e/.clang-format", 0,
22159                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM")));
22160   ASSERT_TRUE(
22161       FS.addFile("/e/explicit.clang-format", 0,
22162                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google")));
22163   ASSERT_TRUE(FS.addFile("/e/sub/sub/sub/test.cpp", 0,
22164                          llvm::MemoryBuffer::getMemBuffer("int i;")));
22165   auto Style = getStyle("file:/e/explicit.clang-format",
22166                         "/e/sub/sub/sub/test.cpp", "LLVM", "", &FS);
22167   ASSERT_TRUE(static_cast<bool>(Style));
22168   ASSERT_EQ(*Style, getGoogleStyle());
22169 
22170   // Specify relative path to a format file.
22171   ASSERT_TRUE(
22172       FS.addFile("../../e/explicit.clang-format", 0,
22173                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google")));
22174   Style = getStyle("file:../../e/explicit.clang-format",
22175                    "/e/sub/sub/sub/test.cpp", "LLVM", "", &FS);
22176   ASSERT_TRUE(static_cast<bool>(Style));
22177   ASSERT_EQ(*Style, getGoogleStyle());
22178 
22179   // Specify path to a format file that does not exist.
22180   Style = getStyle("file:/e/missing.clang-format", "/e/sub/sub/sub/test.cpp",
22181                    "LLVM", "", &FS);
22182   ASSERT_FALSE(static_cast<bool>(Style));
22183   llvm::consumeError(Style.takeError());
22184 
22185   // Specify path to a file on the filesystem.
22186   SmallString<128> FormatFilePath;
22187   std::error_code ECF = llvm::sys::fs::createTemporaryFile(
22188       "FormatFileTest", "tpl", FormatFilePath);
22189   EXPECT_FALSE((bool)ECF);
22190   llvm::raw_fd_ostream FormatFileTest(FormatFilePath, ECF);
22191   EXPECT_FALSE((bool)ECF);
22192   FormatFileTest << "BasedOnStyle: Google\n";
22193   FormatFileTest.close();
22194 
22195   SmallString<128> TestFilePath;
22196   std::error_code ECT =
22197       llvm::sys::fs::createTemporaryFile("CodeFileTest", "cc", TestFilePath);
22198   EXPECT_FALSE((bool)ECT);
22199   llvm::raw_fd_ostream CodeFileTest(TestFilePath, ECT);
22200   CodeFileTest << "int i;\n";
22201   CodeFileTest.close();
22202 
22203   std::string format_file_arg = std::string("file:") + FormatFilePath.c_str();
22204   Style = getStyle(format_file_arg, TestFilePath, "LLVM", "", nullptr);
22205 
22206   llvm::sys::fs::remove(FormatFilePath.c_str());
22207   llvm::sys::fs::remove(TestFilePath.c_str());
22208   ASSERT_TRUE(static_cast<bool>(Style));
22209   ASSERT_EQ(*Style, getGoogleStyle());
22210 }
22211 
22212 TEST_F(ReplacementTest, FormatCodeAfterReplacements) {
22213   // Column limit is 20.
22214   std::string Code = "Type *a =\n"
22215                      "    new Type();\n"
22216                      "g(iiiii, 0, jjjjj,\n"
22217                      "  0, kkkkk, 0, mm);\n"
22218                      "int  bad     = format   ;";
22219   std::string Expected = "auto a = new Type();\n"
22220                          "g(iiiii, nullptr,\n"
22221                          "  jjjjj, nullptr,\n"
22222                          "  kkkkk, nullptr,\n"
22223                          "  mm);\n"
22224                          "int  bad     = format   ;";
22225   FileID ID = Context.createInMemoryFile("format.cpp", Code);
22226   tooling::Replacements Replaces = toReplacements(
22227       {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6,
22228                             "auto "),
22229        tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1,
22230                             "nullptr"),
22231        tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1,
22232                             "nullptr"),
22233        tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1,
22234                             "nullptr")});
22235 
22236   FormatStyle Style = getLLVMStyle();
22237   Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility.
22238   auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
22239   EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
22240       << llvm::toString(FormattedReplaces.takeError()) << "\n";
22241   auto Result = applyAllReplacements(Code, *FormattedReplaces);
22242   EXPECT_TRUE(static_cast<bool>(Result));
22243   EXPECT_EQ(Expected, *Result);
22244 }
22245 
22246 TEST_F(ReplacementTest, SortIncludesAfterReplacement) {
22247   std::string Code = "#include \"a.h\"\n"
22248                      "#include \"c.h\"\n"
22249                      "\n"
22250                      "int main() {\n"
22251                      "  return 0;\n"
22252                      "}";
22253   std::string Expected = "#include \"a.h\"\n"
22254                          "#include \"b.h\"\n"
22255                          "#include \"c.h\"\n"
22256                          "\n"
22257                          "int main() {\n"
22258                          "  return 0;\n"
22259                          "}";
22260   FileID ID = Context.createInMemoryFile("fix.cpp", Code);
22261   tooling::Replacements Replaces = toReplacements(
22262       {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0,
22263                             "#include \"b.h\"\n")});
22264 
22265   FormatStyle Style = getLLVMStyle();
22266   Style.SortIncludes = FormatStyle::SI_CaseSensitive;
22267   auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
22268   EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
22269       << llvm::toString(FormattedReplaces.takeError()) << "\n";
22270   auto Result = applyAllReplacements(Code, *FormattedReplaces);
22271   EXPECT_TRUE(static_cast<bool>(Result));
22272   EXPECT_EQ(Expected, *Result);
22273 }
22274 
22275 TEST_F(FormatTest, FormatSortsUsingDeclarations) {
22276   EXPECT_EQ("using std::cin;\n"
22277             "using std::cout;",
22278             format("using std::cout;\n"
22279                    "using std::cin;",
22280                    getGoogleStyle()));
22281 }
22282 
22283 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) {
22284   FormatStyle Style = getLLVMStyle();
22285   Style.Standard = FormatStyle::LS_Cpp03;
22286   // cpp03 recognize this string as identifier u8 and literal character 'a'
22287   EXPECT_EQ("auto c = u8 'a';", format("auto c = u8'a';", Style));
22288 }
22289 
22290 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) {
22291   // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers
22292   // all modes, including C++11, C++14 and C++17
22293   EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';"));
22294 }
22295 
22296 TEST_F(FormatTest, DoNotFormatLikelyXml) {
22297   EXPECT_EQ("<!-- ;> -->", format("<!-- ;> -->", getGoogleStyle()));
22298   EXPECT_EQ(" <!-- >; -->", format(" <!-- >; -->", getGoogleStyle()));
22299 }
22300 
22301 TEST_F(FormatTest, StructuredBindings) {
22302   // Structured bindings is a C++17 feature.
22303   // all modes, including C++11, C++14 and C++17
22304   verifyFormat("auto [a, b] = f();");
22305   EXPECT_EQ("auto [a, b] = f();", format("auto[a, b] = f();"));
22306   EXPECT_EQ("const auto [a, b] = f();", format("const   auto[a, b] = f();"));
22307   EXPECT_EQ("auto const [a, b] = f();", format("auto  const[a, b] = f();"));
22308   EXPECT_EQ("auto const volatile [a, b] = f();",
22309             format("auto  const   volatile[a, b] = f();"));
22310   EXPECT_EQ("auto [a, b, c] = f();", format("auto   [  a  ,  b,c   ] = f();"));
22311   EXPECT_EQ("auto &[a, b, c] = f();",
22312             format("auto   &[  a  ,  b,c   ] = f();"));
22313   EXPECT_EQ("auto &&[a, b, c] = f();",
22314             format("auto   &&[  a  ,  b,c   ] = f();"));
22315   EXPECT_EQ("auto const &[a, b] = f();", format("auto  const&[a, b] = f();"));
22316   EXPECT_EQ("auto const volatile &&[a, b] = f();",
22317             format("auto  const  volatile  &&[a, b] = f();"));
22318   EXPECT_EQ("auto const &&[a, b] = f();",
22319             format("auto  const   &&  [a, b] = f();"));
22320   EXPECT_EQ("const auto &[a, b] = f();",
22321             format("const  auto  &  [a, b] = f();"));
22322   EXPECT_EQ("const auto volatile &&[a, b] = f();",
22323             format("const  auto   volatile  &&[a, b] = f();"));
22324   EXPECT_EQ("volatile const auto &&[a, b] = f();",
22325             format("volatile  const  auto   &&[a, b] = f();"));
22326   EXPECT_EQ("const auto &&[a, b] = f();",
22327             format("const  auto  &&  [a, b] = f();"));
22328 
22329   // Make sure we don't mistake structured bindings for lambdas.
22330   FormatStyle PointerMiddle = getLLVMStyle();
22331   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
22332   verifyFormat("auto [a1, b]{A * i};", getGoogleStyle());
22333   verifyFormat("auto [a2, b]{A * i};", getLLVMStyle());
22334   verifyFormat("auto [a3, b]{A * i};", PointerMiddle);
22335   verifyFormat("auto const [a1, b]{A * i};", getGoogleStyle());
22336   verifyFormat("auto const [a2, b]{A * i};", getLLVMStyle());
22337   verifyFormat("auto const [a3, b]{A * i};", PointerMiddle);
22338   verifyFormat("auto const& [a1, b]{A * i};", getGoogleStyle());
22339   verifyFormat("auto const &[a2, b]{A * i};", getLLVMStyle());
22340   verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle);
22341   verifyFormat("auto const&& [a1, b]{A * i};", getGoogleStyle());
22342   verifyFormat("auto const &&[a2, b]{A * i};", getLLVMStyle());
22343   verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle);
22344 
22345   EXPECT_EQ("for (const auto &&[a, b] : some_range) {\n}",
22346             format("for (const auto   &&   [a, b] : some_range) {\n}"));
22347   EXPECT_EQ("for (const auto &[a, b] : some_range) {\n}",
22348             format("for (const auto   &   [a, b] : some_range) {\n}"));
22349   EXPECT_EQ("for (const auto [a, b] : some_range) {\n}",
22350             format("for (const auto[a, b] : some_range) {\n}"));
22351   EXPECT_EQ("auto [x, y](expr);", format("auto[x,y]  (expr);"));
22352   EXPECT_EQ("auto &[x, y](expr);", format("auto  &  [x,y]  (expr);"));
22353   EXPECT_EQ("auto &&[x, y](expr);", format("auto  &&  [x,y]  (expr);"));
22354   EXPECT_EQ("auto const &[x, y](expr);",
22355             format("auto  const  &  [x,y]  (expr);"));
22356   EXPECT_EQ("auto const &&[x, y](expr);",
22357             format("auto  const  &&  [x,y]  (expr);"));
22358   EXPECT_EQ("auto [x, y]{expr};", format("auto[x,y]     {expr};"));
22359   EXPECT_EQ("auto const &[x, y]{expr};",
22360             format("auto  const  &  [x,y]  {expr};"));
22361   EXPECT_EQ("auto const &&[x, y]{expr};",
22362             format("auto  const  &&  [x,y]  {expr};"));
22363 
22364   FormatStyle Spaces = getLLVMStyle();
22365   Spaces.SpacesInSquareBrackets = true;
22366   verifyFormat("auto [ a, b ] = f();", Spaces);
22367   verifyFormat("auto &&[ a, b ] = f();", Spaces);
22368   verifyFormat("auto &[ a, b ] = f();", Spaces);
22369   verifyFormat("auto const &&[ a, b ] = f();", Spaces);
22370   verifyFormat("auto const &[ a, b ] = f();", Spaces);
22371 }
22372 
22373 TEST_F(FormatTest, FileAndCode) {
22374   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.cc", ""));
22375   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.m", ""));
22376   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.mm", ""));
22377   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", ""));
22378   EXPECT_EQ(FormatStyle::LK_ObjC,
22379             guessLanguage("foo.h", "@interface Foo\n@end\n"));
22380   EXPECT_EQ(
22381       FormatStyle::LK_ObjC,
22382       guessLanguage("foo.h", "#define TRY(x, y) @try { x; } @finally { y; }"));
22383   EXPECT_EQ(FormatStyle::LK_ObjC,
22384             guessLanguage("foo.h", "#define AVAIL(x) @available(x, *))"));
22385   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@class Foo;"));
22386   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo", ""));
22387   EXPECT_EQ(FormatStyle::LK_ObjC,
22388             guessLanguage("foo", "@interface Foo\n@end\n"));
22389   EXPECT_EQ(FormatStyle::LK_ObjC,
22390             guessLanguage("foo.h", "int DoStuff(CGRect rect);\n"));
22391   EXPECT_EQ(
22392       FormatStyle::LK_ObjC,
22393       guessLanguage("foo.h",
22394                     "#define MY_POINT_MAKE(x, y) CGPointMake((x), (y));\n"));
22395   EXPECT_EQ(
22396       FormatStyle::LK_Cpp,
22397       guessLanguage("foo.h", "#define FOO(...) auto bar = [] __VA_ARGS__;"));
22398 }
22399 
22400 TEST_F(FormatTest, GuessLanguageWithCpp11AttributeSpecifiers) {
22401   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[noreturn]];"));
22402   EXPECT_EQ(FormatStyle::LK_ObjC,
22403             guessLanguage("foo.h", "array[[calculator getIndex]];"));
22404   EXPECT_EQ(FormatStyle::LK_Cpp,
22405             guessLanguage("foo.h", "[[noreturn, deprecated(\"so sorry\")]];"));
22406   EXPECT_EQ(
22407       FormatStyle::LK_Cpp,
22408       guessLanguage("foo.h", "[[noreturn, deprecated(\"gone, sorry\")]];"));
22409   EXPECT_EQ(FormatStyle::LK_ObjC,
22410             guessLanguage("foo.h", "[[noreturn foo] bar];"));
22411   EXPECT_EQ(FormatStyle::LK_Cpp,
22412             guessLanguage("foo.h", "[[clang::fallthrough]];"));
22413   EXPECT_EQ(FormatStyle::LK_ObjC,
22414             guessLanguage("foo.h", "[[clang:fallthrough] foo];"));
22415   EXPECT_EQ(FormatStyle::LK_Cpp,
22416             guessLanguage("foo.h", "[[gsl::suppress(\"type\")]];"));
22417   EXPECT_EQ(FormatStyle::LK_Cpp,
22418             guessLanguage("foo.h", "[[using clang: fallthrough]];"));
22419   EXPECT_EQ(FormatStyle::LK_ObjC,
22420             guessLanguage("foo.h", "[[abusing clang:fallthrough] bar];"));
22421   EXPECT_EQ(FormatStyle::LK_Cpp,
22422             guessLanguage("foo.h", "[[using gsl: suppress(\"type\")]];"));
22423   EXPECT_EQ(
22424       FormatStyle::LK_Cpp,
22425       guessLanguage("foo.h", "for (auto &&[endpoint, stream] : streams_)"));
22426   EXPECT_EQ(
22427       FormatStyle::LK_Cpp,
22428       guessLanguage("foo.h",
22429                     "[[clang::callable_when(\"unconsumed\", \"unknown\")]]"));
22430   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[foo::bar, ...]]"));
22431 }
22432 
22433 TEST_F(FormatTest, GuessLanguageWithCaret) {
22434   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^);"));
22435   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^, Bar);"));
22436   EXPECT_EQ(FormatStyle::LK_ObjC,
22437             guessLanguage("foo.h", "int(^)(char, float);"));
22438   EXPECT_EQ(FormatStyle::LK_ObjC,
22439             guessLanguage("foo.h", "int(^foo)(char, float);"));
22440   EXPECT_EQ(FormatStyle::LK_ObjC,
22441             guessLanguage("foo.h", "int(^foo[10])(char, float);"));
22442   EXPECT_EQ(FormatStyle::LK_ObjC,
22443             guessLanguage("foo.h", "int(^foo[kNumEntries])(char, float);"));
22444   EXPECT_EQ(
22445       FormatStyle::LK_ObjC,
22446       guessLanguage("foo.h", "int(^foo[(kNumEntries + 10)])(char, float);"));
22447 }
22448 
22449 TEST_F(FormatTest, GuessLanguageWithPragmas) {
22450   EXPECT_EQ(FormatStyle::LK_Cpp,
22451             guessLanguage("foo.h", "__pragma(warning(disable:))"));
22452   EXPECT_EQ(FormatStyle::LK_Cpp,
22453             guessLanguage("foo.h", "#pragma(warning(disable:))"));
22454   EXPECT_EQ(FormatStyle::LK_Cpp,
22455             guessLanguage("foo.h", "_Pragma(warning(disable:))"));
22456 }
22457 
22458 TEST_F(FormatTest, FormatsInlineAsmSymbolicNames) {
22459   // ASM symbolic names are identifiers that must be surrounded by [] without
22460   // space in between:
22461   // https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#InputOperands
22462 
22463   // Example from https://bugs.llvm.org/show_bug.cgi?id=45108.
22464   verifyFormat(R"(//
22465 asm volatile("mrs %x[result], FPCR" : [result] "=r"(result));
22466 )");
22467 
22468   // A list of several ASM symbolic names.
22469   verifyFormat(R"(asm("mov %[e], %[d]" : [d] "=rm"(d), [e] "rm"(*e));)");
22470 
22471   // ASM symbolic names in inline ASM with inputs and outputs.
22472   verifyFormat(R"(//
22473 asm("cmoveq %1, %2, %[result]"
22474     : [result] "=r"(result)
22475     : "r"(test), "r"(new), "[result]"(old));
22476 )");
22477 
22478   // ASM symbolic names in inline ASM with no outputs.
22479   verifyFormat(R"(asm("mov %[e], %[d]" : : [d] "=rm"(d), [e] "rm"(*e));)");
22480 }
22481 
22482 TEST_F(FormatTest, GuessedLanguageWithInlineAsmClobbers) {
22483   EXPECT_EQ(FormatStyle::LK_Cpp,
22484             guessLanguage("foo.h", "void f() {\n"
22485                                    "  asm (\"mov %[e], %[d]\"\n"
22486                                    "     : [d] \"=rm\" (d)\n"
22487                                    "       [e] \"rm\" (*e));\n"
22488                                    "}"));
22489   EXPECT_EQ(FormatStyle::LK_Cpp,
22490             guessLanguage("foo.h", "void f() {\n"
22491                                    "  _asm (\"mov %[e], %[d]\"\n"
22492                                    "     : [d] \"=rm\" (d)\n"
22493                                    "       [e] \"rm\" (*e));\n"
22494                                    "}"));
22495   EXPECT_EQ(FormatStyle::LK_Cpp,
22496             guessLanguage("foo.h", "void f() {\n"
22497                                    "  __asm (\"mov %[e], %[d]\"\n"
22498                                    "     : [d] \"=rm\" (d)\n"
22499                                    "       [e] \"rm\" (*e));\n"
22500                                    "}"));
22501   EXPECT_EQ(FormatStyle::LK_Cpp,
22502             guessLanguage("foo.h", "void f() {\n"
22503                                    "  __asm__ (\"mov %[e], %[d]\"\n"
22504                                    "     : [d] \"=rm\" (d)\n"
22505                                    "       [e] \"rm\" (*e));\n"
22506                                    "}"));
22507   EXPECT_EQ(FormatStyle::LK_Cpp,
22508             guessLanguage("foo.h", "void f() {\n"
22509                                    "  asm (\"mov %[e], %[d]\"\n"
22510                                    "     : [d] \"=rm\" (d),\n"
22511                                    "       [e] \"rm\" (*e));\n"
22512                                    "}"));
22513   EXPECT_EQ(FormatStyle::LK_Cpp,
22514             guessLanguage("foo.h", "void f() {\n"
22515                                    "  asm volatile (\"mov %[e], %[d]\"\n"
22516                                    "     : [d] \"=rm\" (d)\n"
22517                                    "       [e] \"rm\" (*e));\n"
22518                                    "}"));
22519 }
22520 
22521 TEST_F(FormatTest, GuessLanguageWithChildLines) {
22522   EXPECT_EQ(FormatStyle::LK_Cpp,
22523             guessLanguage("foo.h", "#define FOO ({ std::string s; })"));
22524   EXPECT_EQ(FormatStyle::LK_ObjC,
22525             guessLanguage("foo.h", "#define FOO ({ NSString *s; })"));
22526   EXPECT_EQ(
22527       FormatStyle::LK_Cpp,
22528       guessLanguage("foo.h", "#define FOO ({ foo(); ({ std::string s; }) })"));
22529   EXPECT_EQ(
22530       FormatStyle::LK_ObjC,
22531       guessLanguage("foo.h", "#define FOO ({ foo(); ({ NSString *s; }) })"));
22532 }
22533 
22534 TEST_F(FormatTest, TypenameMacros) {
22535   std::vector<std::string> TypenameMacros = {"STACK_OF", "LIST", "TAILQ_ENTRY"};
22536 
22537   // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=30353
22538   FormatStyle Google = getGoogleStyleWithColumns(0);
22539   Google.TypenameMacros = TypenameMacros;
22540   verifyFormat("struct foo {\n"
22541                "  int bar;\n"
22542                "  TAILQ_ENTRY(a) bleh;\n"
22543                "};",
22544                Google);
22545 
22546   FormatStyle Macros = getLLVMStyle();
22547   Macros.TypenameMacros = TypenameMacros;
22548 
22549   verifyFormat("STACK_OF(int) a;", Macros);
22550   verifyFormat("STACK_OF(int) *a;", Macros);
22551   verifyFormat("STACK_OF(int const *) *a;", Macros);
22552   verifyFormat("STACK_OF(int *const) *a;", Macros);
22553   verifyFormat("STACK_OF(int, string) a;", Macros);
22554   verifyFormat("STACK_OF(LIST(int)) a;", Macros);
22555   verifyFormat("STACK_OF(LIST(int)) a, b;", Macros);
22556   verifyFormat("for (LIST(int) *a = NULL; a;) {\n}", Macros);
22557   verifyFormat("STACK_OF(int) f(LIST(int) *arg);", Macros);
22558   verifyFormat("vector<LIST(uint64_t) *attr> x;", Macros);
22559   verifyFormat("vector<LIST(uint64_t) *const> f(LIST(uint64_t) *arg);", Macros);
22560 
22561   Macros.PointerAlignment = FormatStyle::PAS_Left;
22562   verifyFormat("STACK_OF(int)* a;", Macros);
22563   verifyFormat("STACK_OF(int*)* a;", Macros);
22564   verifyFormat("x = (STACK_OF(uint64_t))*a;", Macros);
22565   verifyFormat("x = (STACK_OF(uint64_t))&a;", Macros);
22566   verifyFormat("vector<STACK_OF(uint64_t)* attr> x;", Macros);
22567 }
22568 
22569 TEST_F(FormatTest, AtomicQualifier) {
22570   // Check that we treate _Atomic as a type and not a function call
22571   FormatStyle Google = getGoogleStyleWithColumns(0);
22572   verifyFormat("struct foo {\n"
22573                "  int a1;\n"
22574                "  _Atomic(a) a2;\n"
22575                "  _Atomic(_Atomic(int) *const) a3;\n"
22576                "};",
22577                Google);
22578   verifyFormat("_Atomic(uint64_t) a;");
22579   verifyFormat("_Atomic(uint64_t) *a;");
22580   verifyFormat("_Atomic(uint64_t const *) *a;");
22581   verifyFormat("_Atomic(uint64_t *const) *a;");
22582   verifyFormat("_Atomic(const uint64_t *) *a;");
22583   verifyFormat("_Atomic(uint64_t) a;");
22584   verifyFormat("_Atomic(_Atomic(uint64_t)) a;");
22585   verifyFormat("_Atomic(_Atomic(uint64_t)) a, b;");
22586   verifyFormat("for (_Atomic(uint64_t) *a = NULL; a;) {\n}");
22587   verifyFormat("_Atomic(uint64_t) f(_Atomic(uint64_t) *arg);");
22588 
22589   verifyFormat("_Atomic(uint64_t) *s(InitValue);");
22590   verifyFormat("_Atomic(uint64_t) *s{InitValue};");
22591   FormatStyle Style = getLLVMStyle();
22592   Style.PointerAlignment = FormatStyle::PAS_Left;
22593   verifyFormat("_Atomic(uint64_t)* s(InitValue);", Style);
22594   verifyFormat("_Atomic(uint64_t)* s{InitValue};", Style);
22595   verifyFormat("_Atomic(int)* a;", Style);
22596   verifyFormat("_Atomic(int*)* a;", Style);
22597   verifyFormat("vector<_Atomic(uint64_t)* attr> x;", Style);
22598 
22599   Style.SpacesInCStyleCastParentheses = true;
22600   Style.SpacesInParentheses = false;
22601   verifyFormat("x = ( _Atomic(uint64_t) )*a;", Style);
22602   Style.SpacesInCStyleCastParentheses = false;
22603   Style.SpacesInParentheses = true;
22604   verifyFormat("x = (_Atomic( uint64_t ))*a;", Style);
22605   verifyFormat("x = (_Atomic( uint64_t ))&a;", Style);
22606 }
22607 
22608 TEST_F(FormatTest, AmbersandInLamda) {
22609   // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=41899
22610   FormatStyle AlignStyle = getLLVMStyle();
22611   AlignStyle.PointerAlignment = FormatStyle::PAS_Left;
22612   verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle);
22613   AlignStyle.PointerAlignment = FormatStyle::PAS_Right;
22614   verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle);
22615 }
22616 
22617 TEST_F(FormatTest, SpacesInConditionalStatement) {
22618   FormatStyle Spaces = getLLVMStyle();
22619   Spaces.IfMacros.clear();
22620   Spaces.IfMacros.push_back("MYIF");
22621   Spaces.SpacesInConditionalStatement = true;
22622   verifyFormat("for ( int i = 0; i; i++ )\n  continue;", Spaces);
22623   verifyFormat("if ( !a )\n  return;", Spaces);
22624   verifyFormat("if ( a )\n  return;", Spaces);
22625   verifyFormat("if constexpr ( a )\n  return;", Spaces);
22626   verifyFormat("MYIF ( a )\n  return;", Spaces);
22627   verifyFormat("MYIF ( a )\n  return;\nelse MYIF ( b )\n  return;", Spaces);
22628   verifyFormat("MYIF ( a )\n  return;\nelse\n  return;", Spaces);
22629   verifyFormat("switch ( a )\ncase 1:\n  return;", Spaces);
22630   verifyFormat("while ( a )\n  return;", Spaces);
22631   verifyFormat("while ( (a && b) )\n  return;", Spaces);
22632   verifyFormat("do {\n} while ( 1 != 0 );", Spaces);
22633   verifyFormat("try {\n} catch ( const std::exception & ) {\n}", Spaces);
22634   // Check that space on the left of "::" is inserted as expected at beginning
22635   // of condition.
22636   verifyFormat("while ( ::func() )\n  return;", Spaces);
22637 
22638   // Check impact of ControlStatementsExceptControlMacros is honored.
22639   Spaces.SpaceBeforeParens =
22640       FormatStyle::SBPO_ControlStatementsExceptControlMacros;
22641   verifyFormat("MYIF( a )\n  return;", Spaces);
22642   verifyFormat("MYIF( a )\n  return;\nelse MYIF( b )\n  return;", Spaces);
22643   verifyFormat("MYIF( a )\n  return;\nelse\n  return;", Spaces);
22644 }
22645 
22646 TEST_F(FormatTest, AlternativeOperators) {
22647   // Test case for ensuring alternate operators are not
22648   // combined with their right most neighbour.
22649   verifyFormat("int a and b;");
22650   verifyFormat("int a and_eq b;");
22651   verifyFormat("int a bitand b;");
22652   verifyFormat("int a bitor b;");
22653   verifyFormat("int a compl b;");
22654   verifyFormat("int a not b;");
22655   verifyFormat("int a not_eq b;");
22656   verifyFormat("int a or b;");
22657   verifyFormat("int a xor b;");
22658   verifyFormat("int a xor_eq b;");
22659   verifyFormat("return this not_eq bitand other;");
22660   verifyFormat("bool operator not_eq(const X bitand other)");
22661 
22662   verifyFormat("int a and 5;");
22663   verifyFormat("int a and_eq 5;");
22664   verifyFormat("int a bitand 5;");
22665   verifyFormat("int a bitor 5;");
22666   verifyFormat("int a compl 5;");
22667   verifyFormat("int a not 5;");
22668   verifyFormat("int a not_eq 5;");
22669   verifyFormat("int a or 5;");
22670   verifyFormat("int a xor 5;");
22671   verifyFormat("int a xor_eq 5;");
22672 
22673   verifyFormat("int a compl(5);");
22674   verifyFormat("int a not(5);");
22675 
22676   /* FIXME handle alternate tokens
22677    * https://en.cppreference.com/w/cpp/language/operator_alternative
22678   // alternative tokens
22679   verifyFormat("compl foo();");     //  ~foo();
22680   verifyFormat("foo() <%%>;");      // foo();
22681   verifyFormat("void foo() <%%>;"); // void foo(){}
22682   verifyFormat("int a <:1:>;");     // int a[1];[
22683   verifyFormat("%:define ABC abc"); // #define ABC abc
22684   verifyFormat("%:%:");             // ##
22685   */
22686 }
22687 
22688 TEST_F(FormatTest, STLWhileNotDefineChed) {
22689   verifyFormat("#if defined(while)\n"
22690                "#define while EMIT WARNING C4005\n"
22691                "#endif // while");
22692 }
22693 
22694 TEST_F(FormatTest, OperatorSpacing) {
22695   FormatStyle Style = getLLVMStyle();
22696   Style.PointerAlignment = FormatStyle::PAS_Right;
22697   verifyFormat("Foo::operator*();", Style);
22698   verifyFormat("Foo::operator void *();", Style);
22699   verifyFormat("Foo::operator void **();", Style);
22700   verifyFormat("Foo::operator void *&();", Style);
22701   verifyFormat("Foo::operator void *&&();", Style);
22702   verifyFormat("Foo::operator void const *();", Style);
22703   verifyFormat("Foo::operator void const **();", Style);
22704   verifyFormat("Foo::operator void const *&();", Style);
22705   verifyFormat("Foo::operator void const *&&();", Style);
22706   verifyFormat("Foo::operator()(void *);", Style);
22707   verifyFormat("Foo::operator*(void *);", Style);
22708   verifyFormat("Foo::operator*();", Style);
22709   verifyFormat("Foo::operator**();", Style);
22710   verifyFormat("Foo::operator&();", Style);
22711   verifyFormat("Foo::operator<int> *();", Style);
22712   verifyFormat("Foo::operator<Foo> *();", Style);
22713   verifyFormat("Foo::operator<int> **();", Style);
22714   verifyFormat("Foo::operator<Foo> **();", Style);
22715   verifyFormat("Foo::operator<int> &();", Style);
22716   verifyFormat("Foo::operator<Foo> &();", Style);
22717   verifyFormat("Foo::operator<int> &&();", Style);
22718   verifyFormat("Foo::operator<Foo> &&();", Style);
22719   verifyFormat("Foo::operator<int> *&();", Style);
22720   verifyFormat("Foo::operator<Foo> *&();", Style);
22721   verifyFormat("Foo::operator<int> *&&();", Style);
22722   verifyFormat("Foo::operator<Foo> *&&();", Style);
22723   verifyFormat("operator*(int (*)(), class Foo);", Style);
22724 
22725   verifyFormat("Foo::operator&();", Style);
22726   verifyFormat("Foo::operator void &();", Style);
22727   verifyFormat("Foo::operator void const &();", Style);
22728   verifyFormat("Foo::operator()(void &);", Style);
22729   verifyFormat("Foo::operator&(void &);", Style);
22730   verifyFormat("Foo::operator&();", Style);
22731   verifyFormat("operator&(int (&)(), class Foo);", Style);
22732   verifyFormat("operator&&(int (&)(), class Foo);", Style);
22733 
22734   verifyFormat("Foo::operator&&();", Style);
22735   verifyFormat("Foo::operator**();", Style);
22736   verifyFormat("Foo::operator void &&();", Style);
22737   verifyFormat("Foo::operator void const &&();", Style);
22738   verifyFormat("Foo::operator()(void &&);", Style);
22739   verifyFormat("Foo::operator&&(void &&);", Style);
22740   verifyFormat("Foo::operator&&();", Style);
22741   verifyFormat("operator&&(int (&&)(), class Foo);", Style);
22742   verifyFormat("operator const nsTArrayRight<E> &()", Style);
22743   verifyFormat("[[nodiscard]] operator const nsTArrayRight<E, Allocator> &()",
22744                Style);
22745   verifyFormat("operator void **()", Style);
22746   verifyFormat("operator const FooRight<Object> &()", Style);
22747   verifyFormat("operator const FooRight<Object> *()", Style);
22748   verifyFormat("operator const FooRight<Object> **()", Style);
22749   verifyFormat("operator const FooRight<Object> *&()", Style);
22750   verifyFormat("operator const FooRight<Object> *&&()", Style);
22751 
22752   Style.PointerAlignment = FormatStyle::PAS_Left;
22753   verifyFormat("Foo::operator*();", Style);
22754   verifyFormat("Foo::operator**();", Style);
22755   verifyFormat("Foo::operator void*();", Style);
22756   verifyFormat("Foo::operator void**();", Style);
22757   verifyFormat("Foo::operator void*&();", Style);
22758   verifyFormat("Foo::operator void*&&();", Style);
22759   verifyFormat("Foo::operator void const*();", Style);
22760   verifyFormat("Foo::operator void const**();", Style);
22761   verifyFormat("Foo::operator void const*&();", Style);
22762   verifyFormat("Foo::operator void const*&&();", Style);
22763   verifyFormat("Foo::operator/*comment*/ void*();", Style);
22764   verifyFormat("Foo::operator/*a*/ const /*b*/ void*();", Style);
22765   verifyFormat("Foo::operator/*a*/ volatile /*b*/ void*();", Style);
22766   verifyFormat("Foo::operator()(void*);", Style);
22767   verifyFormat("Foo::operator*(void*);", Style);
22768   verifyFormat("Foo::operator*();", Style);
22769   verifyFormat("Foo::operator<int>*();", Style);
22770   verifyFormat("Foo::operator<Foo>*();", Style);
22771   verifyFormat("Foo::operator<int>**();", Style);
22772   verifyFormat("Foo::operator<Foo>**();", Style);
22773   verifyFormat("Foo::operator<Foo>*&();", Style);
22774   verifyFormat("Foo::operator<int>&();", Style);
22775   verifyFormat("Foo::operator<Foo>&();", Style);
22776   verifyFormat("Foo::operator<int>&&();", Style);
22777   verifyFormat("Foo::operator<Foo>&&();", Style);
22778   verifyFormat("Foo::operator<int>*&();", Style);
22779   verifyFormat("Foo::operator<Foo>*&();", Style);
22780   verifyFormat("operator*(int (*)(), class Foo);", Style);
22781 
22782   verifyFormat("Foo::operator&();", Style);
22783   verifyFormat("Foo::operator void&();", Style);
22784   verifyFormat("Foo::operator void const&();", Style);
22785   verifyFormat("Foo::operator/*comment*/ void&();", Style);
22786   verifyFormat("Foo::operator/*a*/ const /*b*/ void&();", Style);
22787   verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&();", Style);
22788   verifyFormat("Foo::operator()(void&);", Style);
22789   verifyFormat("Foo::operator&(void&);", Style);
22790   verifyFormat("Foo::operator&();", Style);
22791   verifyFormat("operator&(int (&)(), class Foo);", Style);
22792   verifyFormat("operator&(int (&&)(), class Foo);", Style);
22793   verifyFormat("operator&&(int (&&)(), class Foo);", Style);
22794 
22795   verifyFormat("Foo::operator&&();", Style);
22796   verifyFormat("Foo::operator void&&();", Style);
22797   verifyFormat("Foo::operator void const&&();", Style);
22798   verifyFormat("Foo::operator/*comment*/ void&&();", Style);
22799   verifyFormat("Foo::operator/*a*/ const /*b*/ void&&();", Style);
22800   verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&&();", Style);
22801   verifyFormat("Foo::operator()(void&&);", Style);
22802   verifyFormat("Foo::operator&&(void&&);", Style);
22803   verifyFormat("Foo::operator&&();", Style);
22804   verifyFormat("operator&&(int (&&)(), class Foo);", Style);
22805   verifyFormat("operator const nsTArrayLeft<E>&()", Style);
22806   verifyFormat("[[nodiscard]] operator const nsTArrayLeft<E, Allocator>&()",
22807                Style);
22808   verifyFormat("operator void**()", Style);
22809   verifyFormat("operator const FooLeft<Object>&()", Style);
22810   verifyFormat("operator const FooLeft<Object>*()", Style);
22811   verifyFormat("operator const FooLeft<Object>**()", Style);
22812   verifyFormat("operator const FooLeft<Object>*&()", Style);
22813   verifyFormat("operator const FooLeft<Object>*&&()", Style);
22814 
22815   // PR45107
22816   verifyFormat("operator Vector<String>&();", Style);
22817   verifyFormat("operator const Vector<String>&();", Style);
22818   verifyFormat("operator foo::Bar*();", Style);
22819   verifyFormat("operator const Foo<X>::Bar<Y>*();", Style);
22820   verifyFormat("operator/*a*/ const /*b*/ Foo /*c*/<X> /*d*/ ::Bar<Y>*();",
22821                Style);
22822 
22823   Style.PointerAlignment = FormatStyle::PAS_Middle;
22824   verifyFormat("Foo::operator*();", Style);
22825   verifyFormat("Foo::operator void *();", Style);
22826   verifyFormat("Foo::operator()(void *);", Style);
22827   verifyFormat("Foo::operator*(void *);", Style);
22828   verifyFormat("Foo::operator*();", Style);
22829   verifyFormat("operator*(int (*)(), class Foo);", Style);
22830 
22831   verifyFormat("Foo::operator&();", Style);
22832   verifyFormat("Foo::operator void &();", Style);
22833   verifyFormat("Foo::operator void const &();", Style);
22834   verifyFormat("Foo::operator()(void &);", Style);
22835   verifyFormat("Foo::operator&(void &);", Style);
22836   verifyFormat("Foo::operator&();", Style);
22837   verifyFormat("operator&(int (&)(), class Foo);", Style);
22838 
22839   verifyFormat("Foo::operator&&();", Style);
22840   verifyFormat("Foo::operator void &&();", Style);
22841   verifyFormat("Foo::operator void const &&();", Style);
22842   verifyFormat("Foo::operator()(void &&);", Style);
22843   verifyFormat("Foo::operator&&(void &&);", Style);
22844   verifyFormat("Foo::operator&&();", Style);
22845   verifyFormat("operator&&(int (&&)(), class Foo);", Style);
22846 }
22847 
22848 TEST_F(FormatTest, OperatorPassedAsAFunctionPtr) {
22849   FormatStyle Style = getLLVMStyle();
22850   // PR46157
22851   verifyFormat("foo(operator+, -42);", Style);
22852   verifyFormat("foo(operator++, -42);", Style);
22853   verifyFormat("foo(operator--, -42);", Style);
22854   verifyFormat("foo(-42, operator--);", Style);
22855   verifyFormat("foo(-42, operator, );", Style);
22856   verifyFormat("foo(operator, , -42);", Style);
22857 }
22858 
22859 TEST_F(FormatTest, WhitespaceSensitiveMacros) {
22860   FormatStyle Style = getLLVMStyle();
22861   Style.WhitespaceSensitiveMacros.push_back("FOO");
22862 
22863   // Don't use the helpers here, since 'mess up' will change the whitespace
22864   // and these are all whitespace sensitive by definition
22865   EXPECT_EQ("FOO(String-ized&Messy+But(: :Still)=Intentional);",
22866             format("FOO(String-ized&Messy+But(: :Still)=Intentional);", Style));
22867   EXPECT_EQ(
22868       "FOO(String-ized&Messy+But\\(: :Still)=Intentional);",
22869       format("FOO(String-ized&Messy+But\\(: :Still)=Intentional);", Style));
22870   EXPECT_EQ("FOO(String-ized&Messy+But,: :Still=Intentional);",
22871             format("FOO(String-ized&Messy+But,: :Still=Intentional);", Style));
22872   EXPECT_EQ("FOO(String-ized&Messy+But,: :\n"
22873             "       Still=Intentional);",
22874             format("FOO(String-ized&Messy+But,: :\n"
22875                    "       Still=Intentional);",
22876                    Style));
22877   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
22878   EXPECT_EQ("FOO(String-ized=&Messy+But,: :\n"
22879             "       Still=Intentional);",
22880             format("FOO(String-ized=&Messy+But,: :\n"
22881                    "       Still=Intentional);",
22882                    Style));
22883 
22884   Style.ColumnLimit = 21;
22885   EXPECT_EQ("FOO(String-ized&Messy+But: :Still=Intentional);",
22886             format("FOO(String-ized&Messy+But: :Still=Intentional);", Style));
22887 }
22888 
22889 TEST_F(FormatTest, VeryLongNamespaceCommentSplit) {
22890   // These tests are not in NamespaceFixer because that doesn't
22891   // test its interaction with line wrapping
22892   FormatStyle Style = getLLVMStyleWithColumns(80);
22893   verifyFormat("namespace {\n"
22894                "int i;\n"
22895                "int j;\n"
22896                "} // namespace",
22897                Style);
22898 
22899   verifyFormat("namespace AAA {\n"
22900                "int i;\n"
22901                "int j;\n"
22902                "} // namespace AAA",
22903                Style);
22904 
22905   EXPECT_EQ("namespace Averyveryveryverylongnamespace {\n"
22906             "int i;\n"
22907             "int j;\n"
22908             "} // namespace Averyveryveryverylongnamespace",
22909             format("namespace Averyveryveryverylongnamespace {\n"
22910                    "int i;\n"
22911                    "int j;\n"
22912                    "}",
22913                    Style));
22914 
22915   EXPECT_EQ(
22916       "namespace "
22917       "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n"
22918       "    went::mad::now {\n"
22919       "int i;\n"
22920       "int j;\n"
22921       "} // namespace\n"
22922       "  // "
22923       "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::"
22924       "went::mad::now",
22925       format("namespace "
22926              "would::it::save::you::a::lot::of::time::if_::i::"
22927              "just::gave::up::and_::went::mad::now {\n"
22928              "int i;\n"
22929              "int j;\n"
22930              "}",
22931              Style));
22932 
22933   // This used to duplicate the comment again and again on subsequent runs
22934   EXPECT_EQ(
22935       "namespace "
22936       "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n"
22937       "    went::mad::now {\n"
22938       "int i;\n"
22939       "int j;\n"
22940       "} // namespace\n"
22941       "  // "
22942       "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::"
22943       "went::mad::now",
22944       format("namespace "
22945              "would::it::save::you::a::lot::of::time::if_::i::"
22946              "just::gave::up::and_::went::mad::now {\n"
22947              "int i;\n"
22948              "int j;\n"
22949              "} // namespace\n"
22950              "  // "
22951              "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::"
22952              "and_::went::mad::now",
22953              Style));
22954 }
22955 
22956 TEST_F(FormatTest, LikelyUnlikely) {
22957   FormatStyle Style = getLLVMStyle();
22958 
22959   verifyFormat("if (argc > 5) [[unlikely]] {\n"
22960                "  return 29;\n"
22961                "}",
22962                Style);
22963 
22964   verifyFormat("if (argc > 5) [[likely]] {\n"
22965                "  return 29;\n"
22966                "}",
22967                Style);
22968 
22969   verifyFormat("if (argc > 5) [[unlikely]] {\n"
22970                "  return 29;\n"
22971                "} else [[likely]] {\n"
22972                "  return 42;\n"
22973                "}\n",
22974                Style);
22975 
22976   verifyFormat("if (argc > 5) [[unlikely]] {\n"
22977                "  return 29;\n"
22978                "} else if (argc > 10) [[likely]] {\n"
22979                "  return 99;\n"
22980                "} else {\n"
22981                "  return 42;\n"
22982                "}\n",
22983                Style);
22984 
22985   verifyFormat("if (argc > 5) [[gnu::unused]] {\n"
22986                "  return 29;\n"
22987                "}",
22988                Style);
22989 
22990   verifyFormat("if (argc > 5) [[unlikely]]\n"
22991                "  return 29;\n",
22992                Style);
22993   verifyFormat("if (argc > 5) [[likely]]\n"
22994                "  return 29;\n",
22995                Style);
22996 
22997   Style.AttributeMacros.push_back("UNLIKELY");
22998   Style.AttributeMacros.push_back("LIKELY");
22999   verifyFormat("if (argc > 5) UNLIKELY\n"
23000                "  return 29;\n",
23001                Style);
23002 
23003   verifyFormat("if (argc > 5) UNLIKELY {\n"
23004                "  return 29;\n"
23005                "}",
23006                Style);
23007   verifyFormat("if (argc > 5) UNLIKELY {\n"
23008                "  return 29;\n"
23009                "} else [[likely]] {\n"
23010                "  return 42;\n"
23011                "}\n",
23012                Style);
23013   verifyFormat("if (argc > 5) UNLIKELY {\n"
23014                "  return 29;\n"
23015                "} else LIKELY {\n"
23016                "  return 42;\n"
23017                "}\n",
23018                Style);
23019   verifyFormat("if (argc > 5) [[unlikely]] {\n"
23020                "  return 29;\n"
23021                "} else LIKELY {\n"
23022                "  return 42;\n"
23023                "}\n",
23024                Style);
23025 }
23026 
23027 TEST_F(FormatTest, PenaltyIndentedWhitespace) {
23028   verifyFormat("Constructor()\n"
23029                "    : aaaaaa(aaaaaa), aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
23030                "                          aaaa(aaaaaaaaaaaaaaaaaa, "
23031                "aaaaaaaaaaaaaaaaaat))");
23032   verifyFormat("Constructor()\n"
23033                "    : aaaaaaaaaaaaa(aaaaaa), "
23034                "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)");
23035 
23036   FormatStyle StyleWithWhitespacePenalty = getLLVMStyle();
23037   StyleWithWhitespacePenalty.PenaltyIndentedWhitespace = 5;
23038   verifyFormat("Constructor()\n"
23039                "    : aaaaaa(aaaaaa),\n"
23040                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
23041                "          aaaa(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaat))",
23042                StyleWithWhitespacePenalty);
23043   verifyFormat("Constructor()\n"
23044                "    : aaaaaaaaaaaaa(aaaaaa), "
23045                "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)",
23046                StyleWithWhitespacePenalty);
23047 }
23048 
23049 TEST_F(FormatTest, LLVMDefaultStyle) {
23050   FormatStyle Style = getLLVMStyle();
23051   verifyFormat("extern \"C\" {\n"
23052                "int foo();\n"
23053                "}",
23054                Style);
23055 }
23056 TEST_F(FormatTest, GNUDefaultStyle) {
23057   FormatStyle Style = getGNUStyle();
23058   verifyFormat("extern \"C\"\n"
23059                "{\n"
23060                "  int foo ();\n"
23061                "}",
23062                Style);
23063 }
23064 TEST_F(FormatTest, MozillaDefaultStyle) {
23065   FormatStyle Style = getMozillaStyle();
23066   verifyFormat("extern \"C\"\n"
23067                "{\n"
23068                "  int foo();\n"
23069                "}",
23070                Style);
23071 }
23072 TEST_F(FormatTest, GoogleDefaultStyle) {
23073   FormatStyle Style = getGoogleStyle();
23074   verifyFormat("extern \"C\" {\n"
23075                "int foo();\n"
23076                "}",
23077                Style);
23078 }
23079 TEST_F(FormatTest, ChromiumDefaultStyle) {
23080   FormatStyle Style = getChromiumStyle(FormatStyle::LanguageKind::LK_Cpp);
23081   verifyFormat("extern \"C\" {\n"
23082                "int foo();\n"
23083                "}",
23084                Style);
23085 }
23086 TEST_F(FormatTest, MicrosoftDefaultStyle) {
23087   FormatStyle Style = getMicrosoftStyle(FormatStyle::LanguageKind::LK_Cpp);
23088   verifyFormat("extern \"C\"\n"
23089                "{\n"
23090                "    int foo();\n"
23091                "}",
23092                Style);
23093 }
23094 TEST_F(FormatTest, WebKitDefaultStyle) {
23095   FormatStyle Style = getWebKitStyle();
23096   verifyFormat("extern \"C\" {\n"
23097                "int foo();\n"
23098                "}",
23099                Style);
23100 }
23101 
23102 TEST_F(FormatTest, ConceptsAndRequires) {
23103   FormatStyle Style = getLLVMStyle();
23104   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
23105 
23106   verifyFormat("template <typename T>\n"
23107                "concept Hashable = requires(T a) {\n"
23108                "  { std::hash<T>{}(a) } -> std::convertible_to<std::size_t>;\n"
23109                "};",
23110                Style);
23111   verifyFormat("template <typename T>\n"
23112                "concept EqualityComparable = requires(T a, T b) {\n"
23113                "  { a == b } -> bool;\n"
23114                "};",
23115                Style);
23116   verifyFormat("template <typename T>\n"
23117                "concept EqualityComparable = requires(T a, T b) {\n"
23118                "  { a == b } -> bool;\n"
23119                "  { a != b } -> bool;\n"
23120                "};",
23121                Style);
23122   verifyFormat("template <typename T>\n"
23123                "concept EqualityComparable = requires(T a, T b) {\n"
23124                "  { a == b } -> bool;\n"
23125                "  { a != b } -> bool;\n"
23126                "};",
23127                Style);
23128 
23129   verifyFormat("template <typename It>\n"
23130                "requires Iterator<It>\n"
23131                "void sort(It begin, It end) {\n"
23132                "  //....\n"
23133                "}",
23134                Style);
23135 
23136   verifyFormat("template <typename T>\n"
23137                "concept Large = sizeof(T) > 10;",
23138                Style);
23139 
23140   verifyFormat("template <typename T, typename U>\n"
23141                "concept FooableWith = requires(T t, U u) {\n"
23142                "  typename T::foo_type;\n"
23143                "  { t.foo(u) } -> typename T::foo_type;\n"
23144                "  t++;\n"
23145                "};\n"
23146                "void doFoo(FooableWith<int> auto t) {\n"
23147                "  t.foo(3);\n"
23148                "}",
23149                Style);
23150   verifyFormat("template <typename T>\n"
23151                "concept Context = sizeof(T) == 1;",
23152                Style);
23153   verifyFormat("template <typename T>\n"
23154                "concept Context = is_specialization_of_v<context, T>;",
23155                Style);
23156   verifyFormat("template <typename T>\n"
23157                "concept Node = std::is_object_v<T>;",
23158                Style);
23159   verifyFormat("template <typename T>\n"
23160                "concept Tree = true;",
23161                Style);
23162 
23163   verifyFormat("template <typename T> int g(T i) requires Concept1<I> {\n"
23164                "  //...\n"
23165                "}",
23166                Style);
23167 
23168   verifyFormat(
23169       "template <typename T> int g(T i) requires Concept1<I> && Concept2<I> {\n"
23170       "  //...\n"
23171       "}",
23172       Style);
23173 
23174   verifyFormat(
23175       "template <typename T> int g(T i) requires Concept1<I> || Concept2<I> {\n"
23176       "  //...\n"
23177       "}",
23178       Style);
23179 
23180   verifyFormat("template <typename T>\n"
23181                "veryveryvery_long_return_type g(T i) requires Concept1<I> || "
23182                "Concept2<I> {\n"
23183                "  //...\n"
23184                "}",
23185                Style);
23186 
23187   verifyFormat("template <typename T>\n"
23188                "veryveryvery_long_return_type g(T i) requires Concept1<I> && "
23189                "Concept2<I> {\n"
23190                "  //...\n"
23191                "}",
23192                Style);
23193 
23194   verifyFormat(
23195       "template <typename T>\n"
23196       "veryveryvery_long_return_type g(T i) requires Concept1 && Concept2 {\n"
23197       "  //...\n"
23198       "}",
23199       Style);
23200 
23201   verifyFormat(
23202       "template <typename T>\n"
23203       "veryveryvery_long_return_type g(T i) requires Concept1 || Concept2 {\n"
23204       "  //...\n"
23205       "}",
23206       Style);
23207 
23208   verifyFormat("template <typename It>\n"
23209                "requires Foo<It>() && Bar<It> {\n"
23210                "  //....\n"
23211                "}",
23212                Style);
23213 
23214   verifyFormat("template <typename It>\n"
23215                "requires Foo<Bar<It>>() && Bar<Foo<It, It>> {\n"
23216                "  //....\n"
23217                "}",
23218                Style);
23219 
23220   verifyFormat("template <typename It>\n"
23221                "requires Foo<Bar<It, It>>() && Bar<Foo<It, It>> {\n"
23222                "  //....\n"
23223                "}",
23224                Style);
23225 
23226   verifyFormat(
23227       "template <typename It>\n"
23228       "requires Foo<Bar<It>, Baz<It>>() && Bar<Foo<It>, Baz<It, It>> {\n"
23229       "  //....\n"
23230       "}",
23231       Style);
23232 
23233   Style.IndentRequires = true;
23234   verifyFormat("template <typename It>\n"
23235                "  requires Iterator<It>\n"
23236                "void sort(It begin, It end) {\n"
23237                "  //....\n"
23238                "}",
23239                Style);
23240   verifyFormat("template <std::size index_>\n"
23241                "  requires(index_ < sizeof...(Children_))\n"
23242                "Tree auto &child() {\n"
23243                "  // ...\n"
23244                "}",
23245                Style);
23246 
23247   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
23248   verifyFormat("template <typename T>\n"
23249                "concept Hashable = requires (T a) {\n"
23250                "  { std::hash<T>{}(a) } -> std::convertible_to<std::size_t>;\n"
23251                "};",
23252                Style);
23253 
23254   verifyFormat("template <class T = void>\n"
23255                "  requires EqualityComparable<T> || Same<T, void>\n"
23256                "struct equal_to;",
23257                Style);
23258 
23259   verifyFormat("template <class T>\n"
23260                "  requires requires {\n"
23261                "    T{};\n"
23262                "    T (int);\n"
23263                "  }\n",
23264                Style);
23265 
23266   Style.ColumnLimit = 78;
23267   verifyFormat("template <typename T>\n"
23268                "concept Context = Traits<typename T::traits_type> and\n"
23269                "    Interface<typename T::interface_type> and\n"
23270                "    Request<typename T::request_type> and\n"
23271                "    Response<typename T::response_type> and\n"
23272                "    ContextExtension<typename T::extension_type> and\n"
23273                "    ::std::is_copy_constructable<T> and "
23274                "::std::is_move_constructable<T> and\n"
23275                "    requires (T c) {\n"
23276                "  { c.response; } -> Response;\n"
23277                "} and requires (T c) {\n"
23278                "  { c.request; } -> Request;\n"
23279                "}\n",
23280                Style);
23281 
23282   verifyFormat("template <typename T>\n"
23283                "concept Context = Traits<typename T::traits_type> or\n"
23284                "    Interface<typename T::interface_type> or\n"
23285                "    Request<typename T::request_type> or\n"
23286                "    Response<typename T::response_type> or\n"
23287                "    ContextExtension<typename T::extension_type> or\n"
23288                "    ::std::is_copy_constructable<T> or "
23289                "::std::is_move_constructable<T> or\n"
23290                "    requires (T c) {\n"
23291                "  { c.response; } -> Response;\n"
23292                "} or requires (T c) {\n"
23293                "  { c.request; } -> Request;\n"
23294                "}\n",
23295                Style);
23296 
23297   verifyFormat("template <typename T>\n"
23298                "concept Context = Traits<typename T::traits_type> &&\n"
23299                "    Interface<typename T::interface_type> &&\n"
23300                "    Request<typename T::request_type> &&\n"
23301                "    Response<typename T::response_type> &&\n"
23302                "    ContextExtension<typename T::extension_type> &&\n"
23303                "    ::std::is_copy_constructable<T> && "
23304                "::std::is_move_constructable<T> &&\n"
23305                "    requires (T c) {\n"
23306                "  { c.response; } -> Response;\n"
23307                "} && requires (T c) {\n"
23308                "  { c.request; } -> Request;\n"
23309                "}\n",
23310                Style);
23311 
23312   verifyFormat("template <typename T>\nconcept someConcept = Constraint1<T> && "
23313                "Constraint2<T>;");
23314 
23315   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
23316   Style.BraceWrapping.AfterFunction = true;
23317   Style.BraceWrapping.AfterClass = true;
23318   Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
23319   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
23320   verifyFormat("void Foo () requires (std::copyable<T>)\n"
23321                "{\n"
23322                "  return\n"
23323                "}\n",
23324                Style);
23325 
23326   verifyFormat("void Foo () requires std::copyable<T>\n"
23327                "{\n"
23328                "  return\n"
23329                "}\n",
23330                Style);
23331 
23332   verifyFormat("template <std::semiregular F, std::semiregular... Args>\n"
23333                "  requires (std::invocable<F, std::invoke_result_t<Args>...>)\n"
23334                "struct constant;",
23335                Style);
23336 
23337   verifyFormat("template <std::semiregular F, std::semiregular... Args>\n"
23338                "  requires std::invocable<F, std::invoke_result_t<Args>...>\n"
23339                "struct constant;",
23340                Style);
23341 
23342   verifyFormat("template <class T>\n"
23343                "class plane_with_very_very_very_long_name\n"
23344                "{\n"
23345                "  constexpr plane_with_very_very_very_long_name () requires "
23346                "std::copyable<T>\n"
23347                "      : plane_with_very_very_very_long_name (1)\n"
23348                "  {\n"
23349                "  }\n"
23350                "}\n",
23351                Style);
23352 
23353   verifyFormat("template <class T>\n"
23354                "class plane_with_long_name\n"
23355                "{\n"
23356                "  constexpr plane_with_long_name () requires std::copyable<T>\n"
23357                "      : plane_with_long_name (1)\n"
23358                "  {\n"
23359                "  }\n"
23360                "}\n",
23361                Style);
23362 
23363   Style.BreakBeforeConceptDeclarations = false;
23364   verifyFormat("template <typename T> concept Tree = true;", Style);
23365 
23366   Style.IndentRequires = false;
23367   verifyFormat("template <std::semiregular F, std::semiregular... Args>\n"
23368                "requires (std::invocable<F, std::invoke_result_t<Args>...>) "
23369                "struct constant;",
23370                Style);
23371 }
23372 
23373 TEST_F(FormatTest, StatementAttributeLikeMacros) {
23374   FormatStyle Style = getLLVMStyle();
23375   StringRef Source = "void Foo::slot() {\n"
23376                      "  unsigned char MyChar = 'x';\n"
23377                      "  emit signal(MyChar);\n"
23378                      "  Q_EMIT signal(MyChar);\n"
23379                      "}";
23380 
23381   EXPECT_EQ(Source, format(Source, Style));
23382 
23383   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
23384   EXPECT_EQ("void Foo::slot() {\n"
23385             "  unsigned char MyChar = 'x';\n"
23386             "  emit          signal(MyChar);\n"
23387             "  Q_EMIT signal(MyChar);\n"
23388             "}",
23389             format(Source, Style));
23390 
23391   Style.StatementAttributeLikeMacros.push_back("emit");
23392   EXPECT_EQ(Source, format(Source, Style));
23393 
23394   Style.StatementAttributeLikeMacros = {};
23395   EXPECT_EQ("void Foo::slot() {\n"
23396             "  unsigned char MyChar = 'x';\n"
23397             "  emit          signal(MyChar);\n"
23398             "  Q_EMIT        signal(MyChar);\n"
23399             "}",
23400             format(Source, Style));
23401 }
23402 
23403 TEST_F(FormatTest, IndentAccessModifiers) {
23404   FormatStyle Style = getLLVMStyle();
23405   Style.IndentAccessModifiers = true;
23406   // Members are *two* levels below the record;
23407   // Style.IndentWidth == 2, thus yielding a 4 spaces wide indentation.
23408   verifyFormat("class C {\n"
23409                "    int i;\n"
23410                "};\n",
23411                Style);
23412   verifyFormat("union C {\n"
23413                "    int i;\n"
23414                "    unsigned u;\n"
23415                "};\n",
23416                Style);
23417   // Access modifiers should be indented one level below the record.
23418   verifyFormat("class C {\n"
23419                "  public:\n"
23420                "    int i;\n"
23421                "};\n",
23422                Style);
23423   verifyFormat("struct S {\n"
23424                "  private:\n"
23425                "    class C {\n"
23426                "        int j;\n"
23427                "\n"
23428                "      public:\n"
23429                "        C();\n"
23430                "    };\n"
23431                "\n"
23432                "  public:\n"
23433                "    int i;\n"
23434                "};\n",
23435                Style);
23436   // Enumerations are not records and should be unaffected.
23437   Style.AllowShortEnumsOnASingleLine = false;
23438   verifyFormat("enum class E {\n"
23439                "  A,\n"
23440                "  B\n"
23441                "};\n",
23442                Style);
23443   // Test with a different indentation width;
23444   // also proves that the result is Style.AccessModifierOffset agnostic.
23445   Style.IndentWidth = 3;
23446   verifyFormat("class C {\n"
23447                "   public:\n"
23448                "      int i;\n"
23449                "};\n",
23450                Style);
23451 }
23452 
23453 TEST_F(FormatTest, LimitlessStringsAndComments) {
23454   auto Style = getLLVMStyleWithColumns(0);
23455   constexpr StringRef Code =
23456       "/**\n"
23457       " * This is a multiline comment with quite some long lines, at least for "
23458       "the LLVM Style.\n"
23459       " * We will redo this with strings and line comments. Just to  check if "
23460       "everything is working.\n"
23461       " */\n"
23462       "bool foo() {\n"
23463       "  /* Single line multi line comment. */\n"
23464       "  const std::string String = \"This is a multiline string with quite "
23465       "some long lines, at least for the LLVM Style.\"\n"
23466       "                             \"We already did it with multi line "
23467       "comments, and we will do it with line comments. Just to check if "
23468       "everything is working.\";\n"
23469       "  // This is a line comment (block) with quite some long lines, at "
23470       "least for the LLVM Style.\n"
23471       "  // We already did this with multi line comments and strings. Just to "
23472       "check if everything is working.\n"
23473       "  const std::string SmallString = \"Hello World\";\n"
23474       "  // Small line comment\n"
23475       "  return String.size() > SmallString.size();\n"
23476       "}";
23477   EXPECT_EQ(Code, format(Code, Style));
23478 }
23479 
23480 TEST_F(FormatTest, FormatDecayCopy) {
23481   // error cases from unit tests
23482   verifyFormat("foo(auto())");
23483   verifyFormat("foo(auto{})");
23484   verifyFormat("foo(auto({}))");
23485   verifyFormat("foo(auto{{}})");
23486 
23487   verifyFormat("foo(auto(1))");
23488   verifyFormat("foo(auto{1})");
23489   verifyFormat("foo(new auto(1))");
23490   verifyFormat("foo(new auto{1})");
23491   verifyFormat("decltype(auto(1)) x;");
23492   verifyFormat("decltype(auto{1}) x;");
23493   verifyFormat("auto(x);");
23494   verifyFormat("auto{x};");
23495   verifyFormat("new auto{x};");
23496   verifyFormat("auto{x} = y;");
23497   verifyFormat("auto(x) = y;"); // actually a declaration, but this is clearly
23498                                 // the user's own fault
23499   verifyFormat("integral auto(x) = y;"); // actually a declaration, but this is
23500                                          // clearly the user's own fault
23501   verifyFormat("auto(*p)() = f;");       // actually a declaration; TODO FIXME
23502 }
23503 
23504 TEST_F(FormatTest, Cpp20ModulesSupport) {
23505   FormatStyle Style = getLLVMStyle();
23506   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
23507   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
23508 
23509   verifyFormat("export import foo;", Style);
23510   verifyFormat("export import foo:bar;", Style);
23511   verifyFormat("export import foo.bar;", Style);
23512   verifyFormat("export import foo.bar:baz;", Style);
23513   verifyFormat("export import :bar;", Style);
23514   verifyFormat("export module foo:bar;", Style);
23515   verifyFormat("export module foo;", Style);
23516   verifyFormat("export module foo.bar;", Style);
23517   verifyFormat("export module foo.bar:baz;", Style);
23518   verifyFormat("export import <string_view>;", Style);
23519 
23520   verifyFormat("export type_name var;", Style);
23521   verifyFormat("template <class T> export using A = B<T>;", Style);
23522   verifyFormat("export using A = B;", Style);
23523   verifyFormat("export int func() {\n"
23524                "  foo();\n"
23525                "}",
23526                Style);
23527   verifyFormat("export struct {\n"
23528                "  int foo;\n"
23529                "};",
23530                Style);
23531   verifyFormat("export {\n"
23532                "  int foo;\n"
23533                "};",
23534                Style);
23535   verifyFormat("export export char const *hello() { return \"hello\"; }");
23536 
23537   verifyFormat("import bar;", Style);
23538   verifyFormat("import foo.bar;", Style);
23539   verifyFormat("import foo:bar;", Style);
23540   verifyFormat("import :bar;", Style);
23541   verifyFormat("import <ctime>;", Style);
23542   verifyFormat("import \"header\";", Style);
23543 
23544   verifyFormat("module foo;", Style);
23545   verifyFormat("module foo:bar;", Style);
23546   verifyFormat("module foo.bar;", Style);
23547   verifyFormat("module;", Style);
23548 
23549   verifyFormat("export namespace hi {\n"
23550                "const char *sayhi();\n"
23551                "}",
23552                Style);
23553 
23554   verifyFormat("module :private;", Style);
23555   verifyFormat("import <foo/bar.h>;", Style);
23556   verifyFormat("import foo...bar;", Style);
23557   verifyFormat("import ..........;", Style);
23558   verifyFormat("module foo:private;", Style);
23559   verifyFormat("import a", Style);
23560   verifyFormat("module a", Style);
23561   verifyFormat("export import a", Style);
23562   verifyFormat("export module a", Style);
23563 
23564   verifyFormat("import", Style);
23565   verifyFormat("module", Style);
23566   verifyFormat("export", Style);
23567 }
23568 
23569 TEST_F(FormatTest, CoroutineForCoawait) {
23570   FormatStyle Style = getLLVMStyle();
23571   verifyFormat("for co_await (auto x : range())\n  ;");
23572   verifyFormat("for (auto i : arr) {\n"
23573                "}",
23574                Style);
23575   verifyFormat("for co_await (auto i : arr) {\n"
23576                "}",
23577                Style);
23578   verifyFormat("for co_await (auto i : foo(T{})) {\n"
23579                "}",
23580                Style);
23581 }
23582 
23583 TEST_F(FormatTest, CoroutineCoAwait) {
23584   verifyFormat("int x = co_await foo();");
23585   verifyFormat("int x = (co_await foo());");
23586   verifyFormat("co_await (42);");
23587   verifyFormat("void operator co_await(int);");
23588   verifyFormat("void operator co_await(a);");
23589   verifyFormat("co_await a;");
23590   verifyFormat("co_await missing_await_resume{};");
23591   verifyFormat("co_await a; // comment");
23592   verifyFormat("void test0() { co_await a; }");
23593   verifyFormat("co_await co_await co_await foo();");
23594   verifyFormat("co_await foo().bar();");
23595   verifyFormat("co_await [this]() -> Task { co_return x; }");
23596   verifyFormat("co_await [this](int a, int b) -> Task { co_return co_await "
23597                "foo(); }(x, y);");
23598 
23599   FormatStyle Style = getLLVMStyleWithColumns(40);
23600   verifyFormat("co_await [this](int a, int b) -> Task {\n"
23601                "  co_return co_await foo();\n"
23602                "}(x, y);",
23603                Style);
23604   verifyFormat("co_await;");
23605 }
23606 
23607 TEST_F(FormatTest, CoroutineCoYield) {
23608   verifyFormat("int x = co_yield foo();");
23609   verifyFormat("int x = (co_yield foo());");
23610   verifyFormat("co_yield (42);");
23611   verifyFormat("co_yield {42};");
23612   verifyFormat("co_yield 42;");
23613   verifyFormat("co_yield n++;");
23614   verifyFormat("co_yield ++n;");
23615   verifyFormat("co_yield;");
23616 }
23617 
23618 TEST_F(FormatTest, CoroutineCoReturn) {
23619   verifyFormat("co_return (42);");
23620   verifyFormat("co_return;");
23621   verifyFormat("co_return {};");
23622   verifyFormat("co_return x;");
23623   verifyFormat("co_return co_await foo();");
23624   verifyFormat("co_return co_yield foo();");
23625 }
23626 
23627 TEST_F(FormatTest, EmptyShortBlock) {
23628   auto Style = getLLVMStyle();
23629   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty;
23630 
23631   verifyFormat("try {\n"
23632                "  doA();\n"
23633                "} catch (Exception &e) {\n"
23634                "  e.printStackTrace();\n"
23635                "}\n",
23636                Style);
23637 
23638   verifyFormat("try {\n"
23639                "  doA();\n"
23640                "} catch (Exception &e) {}\n",
23641                Style);
23642 }
23643 
23644 TEST_F(FormatTest, ShortTemplatedArgumentLists) {
23645   auto Style = getLLVMStyle();
23646 
23647   verifyFormat("template <> struct S : Template<int (*)[]> {};\n", Style);
23648   verifyFormat("template <> struct S : Template<int (*)[10]> {};\n", Style);
23649   verifyFormat("struct Y : X<[] { return 0; }> {};", Style);
23650   verifyFormat("struct Y<[] { return 0; }> {};", Style);
23651 
23652   verifyFormat("struct Z : X<decltype([] { return 0; }){}> {};", Style);
23653 }
23654 
23655 TEST_F(FormatTest, RemoveBraces) {
23656   FormatStyle Style = getLLVMStyle();
23657   Style.RemoveBracesLLVM = true;
23658 
23659   // The following eight test cases are fully-braced versions of the examples at
23660   // "llvm.org/docs/CodingStandards.html#don-t-use-braces-on-simple-single-
23661   // statement-bodies-of-if-else-loop-statements".
23662 
23663   // 1. Omit the braces, since the body is simple and clearly associated with
23664   // the if.
23665   verifyFormat("if (isa<FunctionDecl>(D))\n"
23666                "  handleFunctionDecl(D);\n"
23667                "else if (isa<VarDecl>(D))\n"
23668                "  handleVarDecl(D);",
23669                "if (isa<FunctionDecl>(D)) {\n"
23670                "  handleFunctionDecl(D);\n"
23671                "} else if (isa<VarDecl>(D)) {\n"
23672                "  handleVarDecl(D);\n"
23673                "}",
23674                Style);
23675 
23676   // 2. Here we document the condition itself and not the body.
23677   verifyFormat("if (isa<VarDecl>(D)) {\n"
23678                "  // It is necessary that we explain the situation with this\n"
23679                "  // surprisingly long comment, so it would be unclear\n"
23680                "  // without the braces whether the following statement is in\n"
23681                "  // the scope of the `if`.\n"
23682                "  // Because the condition is documented, we can't really\n"
23683                "  // hoist this comment that applies to the body above the\n"
23684                "  // if.\n"
23685                "  handleOtherDecl(D);\n"
23686                "}",
23687                Style);
23688 
23689   // 3. Use braces on the outer `if` to avoid a potential dangling else
23690   // situation.
23691   verifyFormat("if (isa<VarDecl>(D)) {\n"
23692                "  for (auto *A : D.attrs())\n"
23693                "    if (shouldProcessAttr(A))\n"
23694                "      handleAttr(A);\n"
23695                "}",
23696                "if (isa<VarDecl>(D)) {\n"
23697                "  for (auto *A : D.attrs()) {\n"
23698                "    if (shouldProcessAttr(A)) {\n"
23699                "      handleAttr(A);\n"
23700                "    }\n"
23701                "  }\n"
23702                "}",
23703                Style);
23704 
23705   // 4. Use braces for the `if` block to keep it uniform with the else block.
23706   verifyFormat("if (isa<FunctionDecl>(D)) {\n"
23707                "  handleFunctionDecl(D);\n"
23708                "} else {\n"
23709                "  // In this else case, it is necessary that we explain the\n"
23710                "  // situation with this surprisingly long comment, so it\n"
23711                "  // would be unclear without the braces whether the\n"
23712                "  // following statement is in the scope of the `if`.\n"
23713                "  handleOtherDecl(D);\n"
23714                "}",
23715                Style);
23716 
23717   // 5. This should also omit braces.  The `for` loop contains only a single
23718   // statement, so it shouldn't have braces.  The `if` also only contains a
23719   // single simple statement (the for loop), so it also should omit braces.
23720   verifyFormat("if (isa<FunctionDecl>(D))\n"
23721                "  for (auto *A : D.attrs())\n"
23722                "    handleAttr(A);",
23723                "if (isa<FunctionDecl>(D)) {\n"
23724                "  for (auto *A : D.attrs()) {\n"
23725                "    handleAttr(A);\n"
23726                "  }\n"
23727                "}",
23728                Style);
23729 
23730   // 6. Use braces for the outer `if` since the nested `for` is braced.
23731   verifyFormat("if (isa<FunctionDecl>(D)) {\n"
23732                "  for (auto *A : D.attrs()) {\n"
23733                "    // In this for loop body, it is necessary that we explain\n"
23734                "    // the situation with this surprisingly long comment,\n"
23735                "    // forcing braces on the `for` block.\n"
23736                "    handleAttr(A);\n"
23737                "  }\n"
23738                "}",
23739                Style);
23740 
23741   // 7. Use braces on the outer block because there are more than two levels of
23742   // nesting.
23743   verifyFormat("if (isa<FunctionDecl>(D)) {\n"
23744                "  for (auto *A : D.attrs())\n"
23745                "    for (ssize_t i : llvm::seq<ssize_t>(count))\n"
23746                "      handleAttrOnDecl(D, A, i);\n"
23747                "}",
23748                "if (isa<FunctionDecl>(D)) {\n"
23749                "  for (auto *A : D.attrs()) {\n"
23750                "    for (ssize_t i : llvm::seq<ssize_t>(count)) {\n"
23751                "      handleAttrOnDecl(D, A, i);\n"
23752                "    }\n"
23753                "  }\n"
23754                "}",
23755                Style);
23756 
23757   // 8. Use braces on the outer block because of a nested `if`, otherwise the
23758   // compiler would warn: `add explicit braces to avoid dangling else`
23759   verifyFormat("if (auto *D = dyn_cast<FunctionDecl>(D)) {\n"
23760                "  if (shouldProcess(D))\n"
23761                "    handleVarDecl(D);\n"
23762                "  else\n"
23763                "    markAsIgnored(D);\n"
23764                "}",
23765                "if (auto *D = dyn_cast<FunctionDecl>(D)) {\n"
23766                "  if (shouldProcess(D)) {\n"
23767                "    handleVarDecl(D);\n"
23768                "  } else {\n"
23769                "    markAsIgnored(D);\n"
23770                "  }\n"
23771                "}",
23772                Style);
23773 
23774   verifyFormat("if (a)\n"
23775                "  b; // comment\n"
23776                "else if (c)\n"
23777                "  d; /* comment */\n"
23778                "else\n"
23779                "  e;",
23780                "if (a) {\n"
23781                "  b; // comment\n"
23782                "} else if (c) {\n"
23783                "  d; /* comment */\n"
23784                "} else {\n"
23785                "  e;\n"
23786                "}",
23787                Style);
23788 
23789   verifyFormat("if (a) {\n"
23790                "  b;\n"
23791                "  c;\n"
23792                "} else if (d) {\n"
23793                "  e;\n"
23794                "}",
23795                Style);
23796 
23797   verifyFormat("if (a) {\n"
23798                "#undef NDEBUG\n"
23799                "  b;\n"
23800                "} else {\n"
23801                "  c;\n"
23802                "}",
23803                Style);
23804 
23805   verifyFormat("if (a) {\n"
23806                "  // comment\n"
23807                "} else if (b) {\n"
23808                "  c;\n"
23809                "}",
23810                Style);
23811 
23812   verifyFormat("if (a) {\n"
23813                "  b;\n"
23814                "} else {\n"
23815                "  { c; }\n"
23816                "}",
23817                Style);
23818 
23819   verifyFormat("if (a) {\n"
23820                "  if (b) // comment\n"
23821                "    c;\n"
23822                "} else if (d) {\n"
23823                "  e;\n"
23824                "}",
23825                "if (a) {\n"
23826                "  if (b) { // comment\n"
23827                "    c;\n"
23828                "  }\n"
23829                "} else if (d) {\n"
23830                "  e;\n"
23831                "}",
23832                Style);
23833 
23834   verifyFormat("if (a) {\n"
23835                "  if (b) {\n"
23836                "    c;\n"
23837                "    // comment\n"
23838                "  } else if (d) {\n"
23839                "    e;\n"
23840                "  }\n"
23841                "}",
23842                Style);
23843 
23844   verifyFormat("if (a) {\n"
23845                "  if (b)\n"
23846                "    c;\n"
23847                "}",
23848                "if (a) {\n"
23849                "  if (b) {\n"
23850                "    c;\n"
23851                "  }\n"
23852                "}",
23853                Style);
23854 
23855   verifyFormat("if (a)\n"
23856                "  if (b)\n"
23857                "    c;\n"
23858                "  else\n"
23859                "    d;\n"
23860                "else\n"
23861                "  e;",
23862                "if (a) {\n"
23863                "  if (b) {\n"
23864                "    c;\n"
23865                "  } else {\n"
23866                "    d;\n"
23867                "  }\n"
23868                "} else {\n"
23869                "  e;\n"
23870                "}",
23871                Style);
23872 
23873   verifyFormat("if (a) {\n"
23874                "  // comment\n"
23875                "  if (b)\n"
23876                "    c;\n"
23877                "  else if (d)\n"
23878                "    e;\n"
23879                "} else {\n"
23880                "  g;\n"
23881                "}",
23882                "if (a) {\n"
23883                "  // comment\n"
23884                "  if (b) {\n"
23885                "    c;\n"
23886                "  } else if (d) {\n"
23887                "    e;\n"
23888                "  }\n"
23889                "} else {\n"
23890                "  g;\n"
23891                "}",
23892                Style);
23893 
23894   verifyFormat("if (a)\n"
23895                "  b;\n"
23896                "else if (c)\n"
23897                "  d;\n"
23898                "else\n"
23899                "  e;",
23900                "if (a) {\n"
23901                "  b;\n"
23902                "} else {\n"
23903                "  if (c) {\n"
23904                "    d;\n"
23905                "  } else {\n"
23906                "    e;\n"
23907                "  }\n"
23908                "}",
23909                Style);
23910 
23911   verifyFormat("if (a) {\n"
23912                "  if (b)\n"
23913                "    c;\n"
23914                "  else if (d)\n"
23915                "    e;\n"
23916                "} else {\n"
23917                "  g;\n"
23918                "}",
23919                "if (a) {\n"
23920                "  if (b)\n"
23921                "    c;\n"
23922                "  else {\n"
23923                "    if (d)\n"
23924                "      e;\n"
23925                "  }\n"
23926                "} else {\n"
23927                "  g;\n"
23928                "}",
23929                Style);
23930 
23931   verifyFormat("if (a)\n"
23932                "  b;\n"
23933                "else if (c)\n"
23934                "  while (d)\n"
23935                "    e;\n"
23936                "// comment",
23937                "if (a)\n"
23938                "{\n"
23939                "  b;\n"
23940                "} else if (c) {\n"
23941                "  while (d) {\n"
23942                "    e;\n"
23943                "  }\n"
23944                "}\n"
23945                "// comment",
23946                Style);
23947 
23948   verifyFormat("if (a) {\n"
23949                "  b;\n"
23950                "} else if (c) {\n"
23951                "  d;\n"
23952                "} else {\n"
23953                "  e;\n"
23954                "  g;\n"
23955                "}",
23956                Style);
23957 
23958   verifyFormat("if (a) {\n"
23959                "  b;\n"
23960                "} else if (c) {\n"
23961                "  d;\n"
23962                "} else {\n"
23963                "  e;\n"
23964                "} // comment",
23965                Style);
23966 
23967   verifyFormat("int abs = [](int i) {\n"
23968                "  if (i >= 0)\n"
23969                "    return i;\n"
23970                "  return -i;\n"
23971                "};",
23972                "int abs = [](int i) {\n"
23973                "  if (i >= 0) {\n"
23974                "    return i;\n"
23975                "  }\n"
23976                "  return -i;\n"
23977                "};",
23978                Style);
23979 
23980   // FIXME: See https://github.com/llvm/llvm-project/issues/53543.
23981 #if 0
23982   Style.ColumnLimit = 65;
23983 
23984   verifyFormat("if (condition) {\n"
23985                "  ff(Indices,\n"
23986                "     [&](unsigned LHSI, unsigned RHSI) { return true; });\n"
23987                "} else {\n"
23988                "  ff(Indices,\n"
23989                "     [&](unsigned LHSI, unsigned RHSI) { return true; });\n"
23990                "}",
23991                Style);
23992 
23993   Style.ColumnLimit = 20;
23994 
23995   verifyFormat("if (a) {\n"
23996                "  b = c + // 1 -\n"
23997                "      d;\n"
23998                "}",
23999                Style);
24000 
24001   verifyFormat("if (a) {\n"
24002                "  b = c >= 0 ? d\n"
24003                "             : e;\n"
24004                "}",
24005                "if (a) {\n"
24006                "  b = c >= 0 ? d : e;\n"
24007                "}",
24008                Style);
24009 #endif
24010 
24011   Style.ColumnLimit = 20;
24012 
24013   verifyFormat("if (a)\n"
24014                "  b = c > 0 ? d : e;",
24015                "if (a) {\n"
24016                "  b = c > 0 ? d : e;\n"
24017                "}",
24018                Style);
24019 
24020   Style.ColumnLimit = 0;
24021 
24022   verifyFormat("if (a)\n"
24023                "  b234567890223456789032345678904234567890 = "
24024                "c234567890223456789032345678904234567890;",
24025                "if (a) {\n"
24026                "  b234567890223456789032345678904234567890 = "
24027                "c234567890223456789032345678904234567890;\n"
24028                "}",
24029                Style);
24030 }
24031 
24032 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndent) {
24033   auto Style = getLLVMStyle();
24034 
24035   StringRef Short = "functionCall(paramA, paramB, paramC);\n"
24036                     "void functionDecl(int a, int b, int c);";
24037 
24038   StringRef Medium = "functionCall(paramA, paramB, paramC, paramD, paramE, "
24039                      "paramF, paramG, paramH, paramI);\n"
24040                      "void functionDecl(int argumentA, int argumentB, int "
24041                      "argumentC, int argumentD, int argumentE);";
24042 
24043   verifyFormat(Short, Style);
24044 
24045   StringRef NoBreak = "functionCall(paramA, paramB, paramC, paramD, paramE, "
24046                       "paramF, paramG, paramH,\n"
24047                       "             paramI);\n"
24048                       "void functionDecl(int argumentA, int argumentB, int "
24049                       "argumentC, int argumentD,\n"
24050                       "                  int argumentE);";
24051 
24052   verifyFormat(NoBreak, Medium, Style);
24053   verifyFormat(NoBreak,
24054                "functionCall(\n"
24055                "    paramA,\n"
24056                "    paramB,\n"
24057                "    paramC,\n"
24058                "    paramD,\n"
24059                "    paramE,\n"
24060                "    paramF,\n"
24061                "    paramG,\n"
24062                "    paramH,\n"
24063                "    paramI\n"
24064                ");\n"
24065                "void functionDecl(\n"
24066                "    int argumentA,\n"
24067                "    int argumentB,\n"
24068                "    int argumentC,\n"
24069                "    int argumentD,\n"
24070                "    int argumentE\n"
24071                ");",
24072                Style);
24073 
24074   verifyFormat("outerFunctionCall(nestedFunctionCall(argument1),\n"
24075                "                  nestedLongFunctionCall(argument1, "
24076                "argument2, argument3,\n"
24077                "                                         argument4, "
24078                "argument5));",
24079                Style);
24080 
24081   Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
24082 
24083   verifyFormat(Short, Style);
24084   verifyFormat(
24085       "functionCall(\n"
24086       "    paramA, paramB, paramC, paramD, paramE, paramF, paramG, paramH, "
24087       "paramI\n"
24088       ");\n"
24089       "void functionDecl(\n"
24090       "    int argumentA, int argumentB, int argumentC, int argumentD, int "
24091       "argumentE\n"
24092       ");",
24093       Medium, Style);
24094 
24095   Style.AllowAllArgumentsOnNextLine = false;
24096   Style.AllowAllParametersOfDeclarationOnNextLine = false;
24097 
24098   verifyFormat(Short, Style);
24099   verifyFormat(
24100       "functionCall(\n"
24101       "    paramA, paramB, paramC, paramD, paramE, paramF, paramG, paramH, "
24102       "paramI\n"
24103       ");\n"
24104       "void functionDecl(\n"
24105       "    int argumentA, int argumentB, int argumentC, int argumentD, int "
24106       "argumentE\n"
24107       ");",
24108       Medium, Style);
24109 
24110   Style.BinPackArguments = false;
24111   Style.BinPackParameters = false;
24112 
24113   verifyFormat(Short, Style);
24114 
24115   verifyFormat("functionCall(\n"
24116                "    paramA,\n"
24117                "    paramB,\n"
24118                "    paramC,\n"
24119                "    paramD,\n"
24120                "    paramE,\n"
24121                "    paramF,\n"
24122                "    paramG,\n"
24123                "    paramH,\n"
24124                "    paramI\n"
24125                ");\n"
24126                "void functionDecl(\n"
24127                "    int argumentA,\n"
24128                "    int argumentB,\n"
24129                "    int argumentC,\n"
24130                "    int argumentD,\n"
24131                "    int argumentE\n"
24132                ");",
24133                Medium, Style);
24134 
24135   verifyFormat("outerFunctionCall(\n"
24136                "    nestedFunctionCall(argument1),\n"
24137                "    nestedLongFunctionCall(\n"
24138                "        argument1,\n"
24139                "        argument2,\n"
24140                "        argument3,\n"
24141                "        argument4,\n"
24142                "        argument5\n"
24143                "    )\n"
24144                ");",
24145                Style);
24146 
24147   verifyFormat("int a = (int)b;", Style);
24148   verifyFormat("int a = (int)b;",
24149                "int a = (\n"
24150                "    int\n"
24151                ") b;",
24152                Style);
24153 
24154   verifyFormat("return (true);", Style);
24155   verifyFormat("return (true);",
24156                "return (\n"
24157                "    true\n"
24158                ");",
24159                Style);
24160 
24161   verifyFormat("void foo();", Style);
24162   verifyFormat("void foo();",
24163                "void foo(\n"
24164                ");",
24165                Style);
24166 
24167   verifyFormat("void foo() {}", Style);
24168   verifyFormat("void foo() {}",
24169                "void foo(\n"
24170                ") {\n"
24171                "}",
24172                Style);
24173 
24174   verifyFormat("auto string = std::string();", Style);
24175   verifyFormat("auto string = std::string();",
24176                "auto string = std::string(\n"
24177                ");",
24178                Style);
24179 
24180   verifyFormat("void (*functionPointer)() = nullptr;", Style);
24181   verifyFormat("void (*functionPointer)() = nullptr;",
24182                "void (\n"
24183                "    *functionPointer\n"
24184                ")\n"
24185                "(\n"
24186                ") = nullptr;",
24187                Style);
24188 }
24189 
24190 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentIfStatement) {
24191   auto Style = getLLVMStyle();
24192 
24193   verifyFormat("if (foo()) {\n"
24194                "  return;\n"
24195                "}",
24196                Style);
24197 
24198   verifyFormat("if (quitelongarg !=\n"
24199                "    (alsolongarg - 1)) { // ABC is a very longgggggggggggg "
24200                "comment\n"
24201                "  return;\n"
24202                "}",
24203                Style);
24204 
24205   Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
24206 
24207   verifyFormat("if (foo()) {\n"
24208                "  return;\n"
24209                "}",
24210                Style);
24211 
24212   verifyFormat("if (quitelongarg !=\n"
24213                "    (alsolongarg - 1)) { // ABC is a very longgggggggggggg "
24214                "comment\n"
24215                "  return;\n"
24216                "}",
24217                Style);
24218 }
24219 
24220 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentForStatement) {
24221   auto Style = getLLVMStyle();
24222 
24223   verifyFormat("for (int i = 0; i < 5; ++i) {\n"
24224                "  doSomething();\n"
24225                "}",
24226                Style);
24227 
24228   verifyFormat("for (int myReallyLongCountVariable = 0; "
24229                "myReallyLongCountVariable < count;\n"
24230                "     myReallyLongCountVariable++) {\n"
24231                "  doSomething();\n"
24232                "}",
24233                Style);
24234 
24235   Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
24236 
24237   verifyFormat("for (int i = 0; i < 5; ++i) {\n"
24238                "  doSomething();\n"
24239                "}",
24240                Style);
24241 
24242   verifyFormat("for (int myReallyLongCountVariable = 0; "
24243                "myReallyLongCountVariable < count;\n"
24244                "     myReallyLongCountVariable++) {\n"
24245                "  doSomething();\n"
24246                "}",
24247                Style);
24248 }
24249 
24250 TEST_F(FormatTest, UnderstandsDigraphs) {
24251   verifyFormat("int arr<:5:> = {};");
24252   verifyFormat("int arr[5] = <%%>;");
24253   verifyFormat("int arr<:::qualified_variable:> = {};");
24254   verifyFormat("int arr[::qualified_variable] = <%%>;");
24255   verifyFormat("%:include <header>");
24256   verifyFormat("%:define A x##y");
24257   verifyFormat("#define A x%:%:y");
24258 }
24259 
24260 } // namespace
24261 } // namespace format
24262 } // namespace clang
24263