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("while (true)\n"
1468                "  ;",
1469                AllowsMergedLoops);
1470   verifyFormat("for (;;)\n"
1471                "  ;",
1472                AllowsMergedLoops);
1473   verifyFormat("for (;;)\n"
1474                "  for (;;) continue;",
1475                AllowsMergedLoops);
1476   verifyFormat("for (;;) // Can't merge this\n"
1477                "  continue;",
1478                AllowsMergedLoops);
1479   verifyFormat("for (;;) /* still don't merge */\n"
1480                "  continue;",
1481                AllowsMergedLoops);
1482   verifyFormat("do a++;\n"
1483                "while (true);",
1484                AllowsMergedLoops);
1485   verifyFormat("do /* Don't merge */\n"
1486                "  a++;\n"
1487                "while (true);",
1488                AllowsMergedLoops);
1489   verifyFormat("do // Don't merge\n"
1490                "  a++;\n"
1491                "while (true);",
1492                AllowsMergedLoops);
1493   verifyFormat("do\n"
1494                "  // Don't merge\n"
1495                "  a++;\n"
1496                "while (true);",
1497                AllowsMergedLoops);
1498   // Without braces labels are interpreted differently.
1499   verifyFormat("{\n"
1500                "  do\n"
1501                "  label:\n"
1502                "    a++;\n"
1503                "  while (true);\n"
1504                "}",
1505                AllowsMergedLoops);
1506 }
1507 
1508 TEST_F(FormatTest, FormatShortBracedStatements) {
1509   FormatStyle AllowSimpleBracedStatements = getLLVMStyle();
1510   AllowSimpleBracedStatements.IfMacros.push_back("MYIF");
1511   // Where line-lengths matter, a 2-letter synonym that maintains line length.
1512   // Not IF to avoid any confusion that IF is somehow special.
1513   AllowSimpleBracedStatements.IfMacros.push_back("FI");
1514   AllowSimpleBracedStatements.ColumnLimit = 40;
1515   AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine =
1516       FormatStyle::SBS_Always;
1517 
1518   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
1519       FormatStyle::SIS_WithoutElse;
1520   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
1521 
1522   AllowSimpleBracedStatements.BreakBeforeBraces = FormatStyle::BS_Custom;
1523   AllowSimpleBracedStatements.BraceWrapping.AfterFunction = true;
1524   AllowSimpleBracedStatements.BraceWrapping.SplitEmptyRecord = false;
1525 
1526   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
1527   verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements);
1528   verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements);
1529   verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements);
1530   verifyFormat("MYIF constexpr (true) {}", AllowSimpleBracedStatements);
1531   verifyFormat("MYIF CONSTEXPR (true) {}", AllowSimpleBracedStatements);
1532   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
1533   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
1534   verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
1535   verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements);
1536   verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements);
1537   verifyFormat("MYIF (true) { f(); }", AllowSimpleBracedStatements);
1538   verifyFormat("MYIF constexpr (true) { f(); }", AllowSimpleBracedStatements);
1539   verifyFormat("MYIF CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements);
1540   verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
1541   verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
1542   verifyFormat("if (true) { fffffffffffffffffffffff(); }",
1543                AllowSimpleBracedStatements);
1544   verifyFormat("if (true) {\n"
1545                "  ffffffffffffffffffffffff();\n"
1546                "}",
1547                AllowSimpleBracedStatements);
1548   verifyFormat("if (true) {\n"
1549                "  ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
1550                "}",
1551                AllowSimpleBracedStatements);
1552   verifyFormat("if (true) { //\n"
1553                "  f();\n"
1554                "}",
1555                AllowSimpleBracedStatements);
1556   verifyFormat("if (true) {\n"
1557                "  f();\n"
1558                "  f();\n"
1559                "}",
1560                AllowSimpleBracedStatements);
1561   verifyFormat("if (true) {\n"
1562                "  f();\n"
1563                "} else {\n"
1564                "  f();\n"
1565                "}",
1566                AllowSimpleBracedStatements);
1567   verifyFormat("FI (true) { fffffffffffffffffffffff(); }",
1568                AllowSimpleBracedStatements);
1569   verifyFormat("MYIF (true) {\n"
1570                "  ffffffffffffffffffffffff();\n"
1571                "}",
1572                AllowSimpleBracedStatements);
1573   verifyFormat("MYIF (true) {\n"
1574                "  ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
1575                "}",
1576                AllowSimpleBracedStatements);
1577   verifyFormat("MYIF (true) { //\n"
1578                "  f();\n"
1579                "}",
1580                AllowSimpleBracedStatements);
1581   verifyFormat("MYIF (true) {\n"
1582                "  f();\n"
1583                "  f();\n"
1584                "}",
1585                AllowSimpleBracedStatements);
1586   verifyFormat("MYIF (true) {\n"
1587                "  f();\n"
1588                "} else {\n"
1589                "  f();\n"
1590                "}",
1591                AllowSimpleBracedStatements);
1592 
1593   verifyFormat("struct A2 {\n"
1594                "  int X;\n"
1595                "};",
1596                AllowSimpleBracedStatements);
1597   verifyFormat("typedef struct A2 {\n"
1598                "  int X;\n"
1599                "} A2_t;",
1600                AllowSimpleBracedStatements);
1601   verifyFormat("template <int> struct A2 {\n"
1602                "  struct B {};\n"
1603                "};",
1604                AllowSimpleBracedStatements);
1605 
1606   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
1607       FormatStyle::SIS_Never;
1608   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
1609   verifyFormat("if (true) {\n"
1610                "  f();\n"
1611                "}",
1612                AllowSimpleBracedStatements);
1613   verifyFormat("if (true) {\n"
1614                "  f();\n"
1615                "} else {\n"
1616                "  f();\n"
1617                "}",
1618                AllowSimpleBracedStatements);
1619   verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements);
1620   verifyFormat("MYIF (true) {\n"
1621                "  f();\n"
1622                "}",
1623                AllowSimpleBracedStatements);
1624   verifyFormat("MYIF (true) {\n"
1625                "  f();\n"
1626                "} else {\n"
1627                "  f();\n"
1628                "}",
1629                AllowSimpleBracedStatements);
1630 
1631   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
1632   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
1633   verifyFormat("while (true) {\n"
1634                "  f();\n"
1635                "}",
1636                AllowSimpleBracedStatements);
1637   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
1638   verifyFormat("for (;;) {\n"
1639                "  f();\n"
1640                "}",
1641                AllowSimpleBracedStatements);
1642 
1643   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
1644       FormatStyle::SIS_WithoutElse;
1645   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
1646   AllowSimpleBracedStatements.BraceWrapping.AfterControlStatement =
1647       FormatStyle::BWACS_Always;
1648 
1649   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
1650   verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements);
1651   verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements);
1652   verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements);
1653   verifyFormat("MYIF constexpr (true) {}", AllowSimpleBracedStatements);
1654   verifyFormat("MYIF CONSTEXPR (true) {}", AllowSimpleBracedStatements);
1655   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
1656   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
1657   verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
1658   verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements);
1659   verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements);
1660   verifyFormat("MYIF (true) { f(); }", AllowSimpleBracedStatements);
1661   verifyFormat("MYIF constexpr (true) { f(); }", AllowSimpleBracedStatements);
1662   verifyFormat("MYIF CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements);
1663   verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
1664   verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
1665   verifyFormat("if (true) { fffffffffffffffffffffff(); }",
1666                AllowSimpleBracedStatements);
1667   verifyFormat("if (true)\n"
1668                "{\n"
1669                "  ffffffffffffffffffffffff();\n"
1670                "}",
1671                AllowSimpleBracedStatements);
1672   verifyFormat("if (true)\n"
1673                "{\n"
1674                "  ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
1675                "}",
1676                AllowSimpleBracedStatements);
1677   verifyFormat("if (true)\n"
1678                "{ //\n"
1679                "  f();\n"
1680                "}",
1681                AllowSimpleBracedStatements);
1682   verifyFormat("if (true)\n"
1683                "{\n"
1684                "  f();\n"
1685                "  f();\n"
1686                "}",
1687                AllowSimpleBracedStatements);
1688   verifyFormat("if (true)\n"
1689                "{\n"
1690                "  f();\n"
1691                "} else\n"
1692                "{\n"
1693                "  f();\n"
1694                "}",
1695                AllowSimpleBracedStatements);
1696   verifyFormat("FI (true) { fffffffffffffffffffffff(); }",
1697                AllowSimpleBracedStatements);
1698   verifyFormat("MYIF (true)\n"
1699                "{\n"
1700                "  ffffffffffffffffffffffff();\n"
1701                "}",
1702                AllowSimpleBracedStatements);
1703   verifyFormat("MYIF (true)\n"
1704                "{\n"
1705                "  ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
1706                "}",
1707                AllowSimpleBracedStatements);
1708   verifyFormat("MYIF (true)\n"
1709                "{ //\n"
1710                "  f();\n"
1711                "}",
1712                AllowSimpleBracedStatements);
1713   verifyFormat("MYIF (true)\n"
1714                "{\n"
1715                "  f();\n"
1716                "  f();\n"
1717                "}",
1718                AllowSimpleBracedStatements);
1719   verifyFormat("MYIF (true)\n"
1720                "{\n"
1721                "  f();\n"
1722                "} else\n"
1723                "{\n"
1724                "  f();\n"
1725                "}",
1726                AllowSimpleBracedStatements);
1727 
1728   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
1729       FormatStyle::SIS_Never;
1730   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
1731   verifyFormat("if (true)\n"
1732                "{\n"
1733                "  f();\n"
1734                "}",
1735                AllowSimpleBracedStatements);
1736   verifyFormat("if (true)\n"
1737                "{\n"
1738                "  f();\n"
1739                "} else\n"
1740                "{\n"
1741                "  f();\n"
1742                "}",
1743                AllowSimpleBracedStatements);
1744   verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements);
1745   verifyFormat("MYIF (true)\n"
1746                "{\n"
1747                "  f();\n"
1748                "}",
1749                AllowSimpleBracedStatements);
1750   verifyFormat("MYIF (true)\n"
1751                "{\n"
1752                "  f();\n"
1753                "} else\n"
1754                "{\n"
1755                "  f();\n"
1756                "}",
1757                AllowSimpleBracedStatements);
1758 
1759   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
1760   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
1761   verifyFormat("while (true)\n"
1762                "{\n"
1763                "  f();\n"
1764                "}",
1765                AllowSimpleBracedStatements);
1766   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
1767   verifyFormat("for (;;)\n"
1768                "{\n"
1769                "  f();\n"
1770                "}",
1771                AllowSimpleBracedStatements);
1772 }
1773 
1774 TEST_F(FormatTest, ShortBlocksInMacrosDontMergeWithCodeAfterMacro) {
1775   FormatStyle Style = getLLVMStyleWithColumns(60);
1776   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
1777   Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse;
1778   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
1779   EXPECT_EQ("#define A                                                  \\\n"
1780             "  if (HANDLEwernufrnuLwrmviferuvnierv)                     \\\n"
1781             "  {                                                        \\\n"
1782             "    RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier;               \\\n"
1783             "  }\n"
1784             "X;",
1785             format("#define A \\\n"
1786                    "   if (HANDLEwernufrnuLwrmviferuvnierv) { \\\n"
1787                    "      RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n"
1788                    "   }\n"
1789                    "X;",
1790                    Style));
1791 }
1792 
1793 TEST_F(FormatTest, ParseIfElse) {
1794   verifyFormat("if (true)\n"
1795                "  if (true)\n"
1796                "    if (true)\n"
1797                "      f();\n"
1798                "    else\n"
1799                "      g();\n"
1800                "  else\n"
1801                "    h();\n"
1802                "else\n"
1803                "  i();");
1804   verifyFormat("if (true)\n"
1805                "  if (true)\n"
1806                "    if (true) {\n"
1807                "      if (true)\n"
1808                "        f();\n"
1809                "    } else {\n"
1810                "      g();\n"
1811                "    }\n"
1812                "  else\n"
1813                "    h();\n"
1814                "else {\n"
1815                "  i();\n"
1816                "}");
1817   verifyFormat("if (true)\n"
1818                "  if constexpr (true)\n"
1819                "    if (true) {\n"
1820                "      if constexpr (true)\n"
1821                "        f();\n"
1822                "    } else {\n"
1823                "      g();\n"
1824                "    }\n"
1825                "  else\n"
1826                "    h();\n"
1827                "else {\n"
1828                "  i();\n"
1829                "}");
1830   verifyFormat("if (true)\n"
1831                "  if CONSTEXPR (true)\n"
1832                "    if (true) {\n"
1833                "      if CONSTEXPR (true)\n"
1834                "        f();\n"
1835                "    } else {\n"
1836                "      g();\n"
1837                "    }\n"
1838                "  else\n"
1839                "    h();\n"
1840                "else {\n"
1841                "  i();\n"
1842                "}");
1843   verifyFormat("void f() {\n"
1844                "  if (a) {\n"
1845                "  } else {\n"
1846                "  }\n"
1847                "}");
1848 }
1849 
1850 TEST_F(FormatTest, ElseIf) {
1851   verifyFormat("if (a) {\n} else if (b) {\n}");
1852   verifyFormat("if (a)\n"
1853                "  f();\n"
1854                "else if (b)\n"
1855                "  g();\n"
1856                "else\n"
1857                "  h();");
1858   verifyFormat("if (a)\n"
1859                "  f();\n"
1860                "else // comment\n"
1861                "  if (b) {\n"
1862                "    g();\n"
1863                "    h();\n"
1864                "  }");
1865   verifyFormat("if constexpr (a)\n"
1866                "  f();\n"
1867                "else if constexpr (b)\n"
1868                "  g();\n"
1869                "else\n"
1870                "  h();");
1871   verifyFormat("if CONSTEXPR (a)\n"
1872                "  f();\n"
1873                "else if CONSTEXPR (b)\n"
1874                "  g();\n"
1875                "else\n"
1876                "  h();");
1877   verifyFormat("if (a) {\n"
1878                "  f();\n"
1879                "}\n"
1880                "// or else ..\n"
1881                "else {\n"
1882                "  g()\n"
1883                "}");
1884 
1885   verifyFormat("if (a) {\n"
1886                "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1887                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
1888                "}");
1889   verifyFormat("if (a) {\n"
1890                "} else if constexpr (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1891                "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
1892                "}");
1893   verifyFormat("if (a) {\n"
1894                "} else if CONSTEXPR (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1895                "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
1896                "}");
1897   verifyFormat("if (a) {\n"
1898                "} else if (\n"
1899                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
1900                "}",
1901                getLLVMStyleWithColumns(62));
1902   verifyFormat("if (a) {\n"
1903                "} else if constexpr (\n"
1904                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
1905                "}",
1906                getLLVMStyleWithColumns(62));
1907   verifyFormat("if (a) {\n"
1908                "} else if CONSTEXPR (\n"
1909                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
1910                "}",
1911                getLLVMStyleWithColumns(62));
1912 }
1913 
1914 TEST_F(FormatTest, SeparatePointerReferenceAlignment) {
1915   FormatStyle Style = getLLVMStyle();
1916   // Check first the default LLVM style
1917   // Style.PointerAlignment = FormatStyle::PAS_Right;
1918   // Style.ReferenceAlignment = FormatStyle::RAS_Pointer;
1919   verifyFormat("int *f1(int *a, int &b, int &&c);", Style);
1920   verifyFormat("int &f2(int &&c, int *a, int &b);", Style);
1921   verifyFormat("int &&f3(int &b, int &&c, int *a);", Style);
1922   verifyFormat("int *f1(int &a) const &;", Style);
1923   verifyFormat("int *f1(int &a) const & = 0;", Style);
1924   verifyFormat("int *a = f1();", Style);
1925   verifyFormat("int &b = f2();", Style);
1926   verifyFormat("int &&c = f3();", Style);
1927   verifyFormat("for (auto a = 0, b = 0; const auto &c : {1, 2, 3})", Style);
1928   verifyFormat("for (auto a = 0, b = 0; const int &c : {1, 2, 3})", Style);
1929   verifyFormat("for (auto a = 0, b = 0; const Foo &c : {1, 2, 3})", Style);
1930   verifyFormat("for (auto a = 0, b = 0; const Foo *c : {1, 2, 3})", Style);
1931   verifyFormat("for (int a = 0, b = 0; const auto &c : {1, 2, 3})", Style);
1932   verifyFormat("for (int a = 0, b = 0; const int &c : {1, 2, 3})", Style);
1933   verifyFormat("for (int a = 0, b = 0; const Foo &c : {1, 2, 3})", Style);
1934   verifyFormat("for (int a = 0, b++; const auto &c : {1, 2, 3})", Style);
1935   verifyFormat("for (int a = 0, b++; const int &c : {1, 2, 3})", Style);
1936   verifyFormat("for (int a = 0, b++; const Foo &c : {1, 2, 3})", Style);
1937   verifyFormat("for (auto x = 0; auto &c : {1, 2, 3})", Style);
1938   verifyFormat("for (auto x = 0; int &c : {1, 2, 3})", Style);
1939   verifyFormat("for (int x = 0; auto &c : {1, 2, 3})", Style);
1940   verifyFormat("for (int x = 0; int &c : {1, 2, 3})", Style);
1941   verifyFormat("for (f(); auto &c : {1, 2, 3})", Style);
1942   verifyFormat("for (f(); int &c : {1, 2, 3})", Style);
1943 
1944   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
1945   verifyFormat("Const unsigned int *c;\n"
1946                "const unsigned int *d;\n"
1947                "Const unsigned int &e;\n"
1948                "const unsigned int &f;\n"
1949                "const unsigned    &&g;\n"
1950                "Const unsigned      h;",
1951                Style);
1952 
1953   Style.PointerAlignment = FormatStyle::PAS_Left;
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& = 0;", Style);
1959   verifyFormat("int* a = f1();", Style);
1960   verifyFormat("int& b = f2();", Style);
1961   verifyFormat("int&& c = f3();", Style);
1962   verifyFormat("for (auto a = 0, b = 0; const auto& c : {1, 2, 3})", Style);
1963   verifyFormat("for (auto a = 0, b = 0; const int& c : {1, 2, 3})", Style);
1964   verifyFormat("for (auto a = 0, b = 0; const Foo& c : {1, 2, 3})", Style);
1965   verifyFormat("for (auto a = 0, b = 0; const Foo* c : {1, 2, 3})", Style);
1966   verifyFormat("for (int a = 0, b = 0; const auto& c : {1, 2, 3})", Style);
1967   verifyFormat("for (int a = 0, b = 0; const int& c : {1, 2, 3})", Style);
1968   verifyFormat("for (int a = 0, b = 0; const Foo& 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 (int a = 0, b++; const Foo* c : {1, 2, 3})", Style);
1974   verifyFormat("for (auto x = 0; auto& c : {1, 2, 3})", Style);
1975   verifyFormat("for (auto x = 0; int& c : {1, 2, 3})", Style);
1976   verifyFormat("for (int x = 0; auto& c : {1, 2, 3})", Style);
1977   verifyFormat("for (int x = 0; int& c : {1, 2, 3})", Style);
1978   verifyFormat("for (f(); auto& c : {1, 2, 3})", Style);
1979   verifyFormat("for (f(); int& c : {1, 2, 3})", Style);
1980 
1981   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
1982   verifyFormat("Const unsigned int* c;\n"
1983                "const unsigned int* d;\n"
1984                "Const unsigned int& e;\n"
1985                "const unsigned int& f;\n"
1986                "const unsigned&&    g;\n"
1987                "Const unsigned      h;",
1988                Style);
1989 
1990   Style.PointerAlignment = FormatStyle::PAS_Right;
1991   Style.ReferenceAlignment = FormatStyle::RAS_Left;
1992   verifyFormat("int *f1(int *a, int& b, int&& c);", Style);
1993   verifyFormat("int& f2(int&& c, int *a, int& b);", Style);
1994   verifyFormat("int&& f3(int& b, int&& c, int *a);", 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 Foo *c : {1, 2, 3})", Style);
1999   verifyFormat("for (int a = 0, b = 0; const Foo *c : {1, 2, 3})", Style);
2000   verifyFormat("for (int a = 0, b++; const Foo *c : {1, 2, 3})", Style);
2001 
2002   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
2003   verifyFormat("Const unsigned int *c;\n"
2004                "const unsigned int *d;\n"
2005                "Const unsigned int& e;\n"
2006                "const unsigned int& f;\n"
2007                "const unsigned      g;\n"
2008                "Const unsigned      h;",
2009                Style);
2010 
2011   Style.PointerAlignment = FormatStyle::PAS_Left;
2012   Style.ReferenceAlignment = FormatStyle::RAS_Middle;
2013   verifyFormat("int* f1(int* a, int & b, int && c);", Style);
2014   verifyFormat("int & f2(int && c, int* a, int & b);", Style);
2015   verifyFormat("int && f3(int & b, int && c, int* a);", Style);
2016   verifyFormat("int* a = f1();", Style);
2017   verifyFormat("int & b = f2();", Style);
2018   verifyFormat("int && c = f3();", Style);
2019   verifyFormat("for (auto a = 0, b = 0; const auto & c : {1, 2, 3})", Style);
2020   verifyFormat("for (auto a = 0, b = 0; const int & c : {1, 2, 3})", Style);
2021   verifyFormat("for (auto a = 0, b = 0; const Foo & c : {1, 2, 3})", Style);
2022   verifyFormat("for (auto a = 0, b = 0; const Foo* c : {1, 2, 3})", Style);
2023   verifyFormat("for (int a = 0, b++; const auto & c : {1, 2, 3})", Style);
2024   verifyFormat("for (int a = 0, b++; const int & c : {1, 2, 3})", Style);
2025   verifyFormat("for (int a = 0, b++; const Foo & c : {1, 2, 3})", Style);
2026   verifyFormat("for (int a = 0, b++; const Foo* c : {1, 2, 3})", Style);
2027   verifyFormat("for (auto x = 0; auto & c : {1, 2, 3})", Style);
2028   verifyFormat("for (auto x = 0; int & c : {1, 2, 3})", Style);
2029   verifyFormat("for (int x = 0; auto & c : {1, 2, 3})", Style);
2030   verifyFormat("for (int x = 0; int & c : {1, 2, 3})", Style);
2031   verifyFormat("for (f(); auto & c : {1, 2, 3})", Style);
2032   verifyFormat("for (f(); int & c : {1, 2, 3})", Style);
2033 
2034   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
2035   verifyFormat("Const unsigned int*  c;\n"
2036                "const unsigned int*  d;\n"
2037                "Const unsigned int & e;\n"
2038                "const unsigned int & f;\n"
2039                "const unsigned &&    g;\n"
2040                "Const unsigned       h;",
2041                Style);
2042 
2043   Style.PointerAlignment = FormatStyle::PAS_Middle;
2044   Style.ReferenceAlignment = FormatStyle::RAS_Right;
2045   verifyFormat("int * f1(int * a, int &b, int &&c);", Style);
2046   verifyFormat("int &f2(int &&c, int * a, int &b);", Style);
2047   verifyFormat("int &&f3(int &b, int &&c, int * a);", Style);
2048   verifyFormat("int * a = f1();", Style);
2049   verifyFormat("int &b = f2();", Style);
2050   verifyFormat("int &&c = f3();", Style);
2051   verifyFormat("for (auto a = 0, b = 0; const Foo * c : {1, 2, 3})", Style);
2052   verifyFormat("for (int a = 0, b = 0; const Foo * c : {1, 2, 3})", Style);
2053   verifyFormat("for (int a = 0, b++; const Foo * c : {1, 2, 3})", Style);
2054 
2055   // FIXME: we don't handle this yet, so output may be arbitrary until it's
2056   // specifically handled
2057   // verifyFormat("int Add2(BTree * &Root, char * szToAdd)", Style);
2058 }
2059 
2060 TEST_F(FormatTest, FormatsForLoop) {
2061   verifyFormat(
2062       "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
2063       "     ++VeryVeryLongLoopVariable)\n"
2064       "  ;");
2065   verifyFormat("for (;;)\n"
2066                "  f();");
2067   verifyFormat("for (;;) {\n}");
2068   verifyFormat("for (;;) {\n"
2069                "  f();\n"
2070                "}");
2071   verifyFormat("for (int i = 0; (i < 10); ++i) {\n}");
2072 
2073   verifyFormat(
2074       "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
2075       "                                          E = UnwrappedLines.end();\n"
2076       "     I != E; ++I) {\n}");
2077 
2078   verifyFormat(
2079       "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
2080       "     ++IIIII) {\n}");
2081   verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n"
2082                "         aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n"
2083                "     aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}");
2084   verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n"
2085                "         I = FD->getDeclsInPrototypeScope().begin(),\n"
2086                "         E = FD->getDeclsInPrototypeScope().end();\n"
2087                "     I != E; ++I) {\n}");
2088   verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n"
2089                "         I = Container.begin(),\n"
2090                "         E = Container.end();\n"
2091                "     I != E; ++I) {\n}",
2092                getLLVMStyleWithColumns(76));
2093 
2094   verifyFormat(
2095       "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
2096       "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n"
2097       "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2098       "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
2099       "     ++aaaaaaaaaaa) {\n}");
2100   verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
2101                "                bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n"
2102                "     ++i) {\n}");
2103   verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n"
2104                "     aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
2105                "}");
2106   verifyFormat("for (some_namespace::SomeIterator iter( // force break\n"
2107                "         aaaaaaaaaa);\n"
2108                "     iter; ++iter) {\n"
2109                "}");
2110   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2111                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
2112                "     aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n"
2113                "     ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {");
2114 
2115   // These should not be formatted as Objective-C for-in loops.
2116   verifyFormat("for (Foo *x = 0; x != in; x++) {\n}");
2117   verifyFormat("Foo *x;\nfor (x = 0; x != in; x++) {\n}");
2118   verifyFormat("Foo *x;\nfor (x in y) {\n}");
2119   verifyFormat(
2120       "for (const Foo<Bar> &baz = in.value(); !baz.at_end(); ++baz) {\n}");
2121 
2122   FormatStyle NoBinPacking = getLLVMStyle();
2123   NoBinPacking.BinPackParameters = false;
2124   verifyFormat("for (int aaaaaaaaaaa = 1;\n"
2125                "     aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n"
2126                "                                           aaaaaaaaaaaaaaaa,\n"
2127                "                                           aaaaaaaaaaaaaaaa,\n"
2128                "                                           aaaaaaaaaaaaaaaa);\n"
2129                "     aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
2130                "}",
2131                NoBinPacking);
2132   verifyFormat(
2133       "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
2134       "                                          E = UnwrappedLines.end();\n"
2135       "     I != E;\n"
2136       "     ++I) {\n}",
2137       NoBinPacking);
2138 
2139   FormatStyle AlignLeft = getLLVMStyle();
2140   AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
2141   verifyFormat("for (A* a = start; a < end; ++a, ++value) {\n}", AlignLeft);
2142 }
2143 
2144 TEST_F(FormatTest, RangeBasedForLoops) {
2145   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
2146                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
2147   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n"
2148                "     aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}");
2149   verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n"
2150                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
2151   verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n"
2152                "     aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}");
2153 }
2154 
2155 TEST_F(FormatTest, ForEachLoops) {
2156   verifyFormat("void f() {\n"
2157                "  foreach (Item *item, itemlist) {}\n"
2158                "  Q_FOREACH (Item *item, itemlist) {}\n"
2159                "  BOOST_FOREACH (Item *item, itemlist) {}\n"
2160                "  UNKNOWN_FORACH(Item * item, itemlist) {}\n"
2161                "}");
2162 
2163   FormatStyle Style = getLLVMStyle();
2164   Style.SpaceBeforeParens =
2165       FormatStyle::SBPO_ControlStatementsExceptControlMacros;
2166   verifyFormat("void f() {\n"
2167                "  foreach(Item *item, itemlist) {}\n"
2168                "  Q_FOREACH(Item *item, itemlist) {}\n"
2169                "  BOOST_FOREACH(Item *item, itemlist) {}\n"
2170                "  UNKNOWN_FORACH(Item * item, itemlist) {}\n"
2171                "}",
2172                Style);
2173 
2174   // As function-like macros.
2175   verifyFormat("#define foreach(x, y)\n"
2176                "#define Q_FOREACH(x, y)\n"
2177                "#define BOOST_FOREACH(x, y)\n"
2178                "#define UNKNOWN_FOREACH(x, y)\n");
2179 
2180   // Not as function-like macros.
2181   verifyFormat("#define foreach (x, y)\n"
2182                "#define Q_FOREACH (x, y)\n"
2183                "#define BOOST_FOREACH (x, y)\n"
2184                "#define UNKNOWN_FOREACH (x, y)\n");
2185 
2186   // handle microsoft non standard extension
2187   verifyFormat("for each (char c in x->MyStringProperty)");
2188 }
2189 
2190 TEST_F(FormatTest, FormatsWhileLoop) {
2191   verifyFormat("while (true) {\n}");
2192   verifyFormat("while (true)\n"
2193                "  f();");
2194   verifyFormat("while () {\n}");
2195   verifyFormat("while () {\n"
2196                "  f();\n"
2197                "}");
2198 }
2199 
2200 TEST_F(FormatTest, FormatsDoWhile) {
2201   verifyFormat("do {\n"
2202                "  do_something();\n"
2203                "} while (something());");
2204   verifyFormat("do\n"
2205                "  do_something();\n"
2206                "while (something());");
2207 }
2208 
2209 TEST_F(FormatTest, FormatsSwitchStatement) {
2210   verifyFormat("switch (x) {\n"
2211                "case 1:\n"
2212                "  f();\n"
2213                "  break;\n"
2214                "case kFoo:\n"
2215                "case ns::kBar:\n"
2216                "case kBaz:\n"
2217                "  break;\n"
2218                "default:\n"
2219                "  g();\n"
2220                "  break;\n"
2221                "}");
2222   verifyFormat("switch (x) {\n"
2223                "case 1: {\n"
2224                "  f();\n"
2225                "  break;\n"
2226                "}\n"
2227                "case 2: {\n"
2228                "  break;\n"
2229                "}\n"
2230                "}");
2231   verifyFormat("switch (x) {\n"
2232                "case 1: {\n"
2233                "  f();\n"
2234                "  {\n"
2235                "    g();\n"
2236                "    h();\n"
2237                "  }\n"
2238                "  break;\n"
2239                "}\n"
2240                "}");
2241   verifyFormat("switch (x) {\n"
2242                "case 1: {\n"
2243                "  f();\n"
2244                "  if (foo) {\n"
2245                "    g();\n"
2246                "    h();\n"
2247                "  }\n"
2248                "  break;\n"
2249                "}\n"
2250                "}");
2251   verifyFormat("switch (x) {\n"
2252                "case 1: {\n"
2253                "  f();\n"
2254                "  g();\n"
2255                "} break;\n"
2256                "}");
2257   verifyFormat("switch (test)\n"
2258                "  ;");
2259   verifyFormat("switch (x) {\n"
2260                "default: {\n"
2261                "  // Do nothing.\n"
2262                "}\n"
2263                "}");
2264   verifyFormat("switch (x) {\n"
2265                "// comment\n"
2266                "// if 1, do f()\n"
2267                "case 1:\n"
2268                "  f();\n"
2269                "}");
2270   verifyFormat("switch (x) {\n"
2271                "case 1:\n"
2272                "  // Do amazing stuff\n"
2273                "  {\n"
2274                "    f();\n"
2275                "    g();\n"
2276                "  }\n"
2277                "  break;\n"
2278                "}");
2279   verifyFormat("#define A          \\\n"
2280                "  switch (x) {     \\\n"
2281                "  case a:          \\\n"
2282                "    foo = b;       \\\n"
2283                "  }",
2284                getLLVMStyleWithColumns(20));
2285   verifyFormat("#define OPERATION_CASE(name)           \\\n"
2286                "  case OP_name:                        \\\n"
2287                "    return operations::Operation##name\n",
2288                getLLVMStyleWithColumns(40));
2289   verifyFormat("switch (x) {\n"
2290                "case 1:;\n"
2291                "default:;\n"
2292                "  int i;\n"
2293                "}");
2294 
2295   verifyGoogleFormat("switch (x) {\n"
2296                      "  case 1:\n"
2297                      "    f();\n"
2298                      "    break;\n"
2299                      "  case kFoo:\n"
2300                      "  case ns::kBar:\n"
2301                      "  case kBaz:\n"
2302                      "    break;\n"
2303                      "  default:\n"
2304                      "    g();\n"
2305                      "    break;\n"
2306                      "}");
2307   verifyGoogleFormat("switch (x) {\n"
2308                      "  case 1: {\n"
2309                      "    f();\n"
2310                      "    break;\n"
2311                      "  }\n"
2312                      "}");
2313   verifyGoogleFormat("switch (test)\n"
2314                      "  ;");
2315 
2316   verifyGoogleFormat("#define OPERATION_CASE(name) \\\n"
2317                      "  case OP_name:              \\\n"
2318                      "    return operations::Operation##name\n");
2319   verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n"
2320                      "  // Get the correction operation class.\n"
2321                      "  switch (OpCode) {\n"
2322                      "    CASE(Add);\n"
2323                      "    CASE(Subtract);\n"
2324                      "    default:\n"
2325                      "      return operations::Unknown;\n"
2326                      "  }\n"
2327                      "#undef OPERATION_CASE\n"
2328                      "}");
2329   verifyFormat("DEBUG({\n"
2330                "  switch (x) {\n"
2331                "  case A:\n"
2332                "    f();\n"
2333                "    break;\n"
2334                "    // fallthrough\n"
2335                "  case B:\n"
2336                "    g();\n"
2337                "    break;\n"
2338                "  }\n"
2339                "});");
2340   EXPECT_EQ("DEBUG({\n"
2341             "  switch (x) {\n"
2342             "  case A:\n"
2343             "    f();\n"
2344             "    break;\n"
2345             "  // On B:\n"
2346             "  case B:\n"
2347             "    g();\n"
2348             "    break;\n"
2349             "  }\n"
2350             "});",
2351             format("DEBUG({\n"
2352                    "  switch (x) {\n"
2353                    "  case A:\n"
2354                    "    f();\n"
2355                    "    break;\n"
2356                    "  // On B:\n"
2357                    "  case B:\n"
2358                    "    g();\n"
2359                    "    break;\n"
2360                    "  }\n"
2361                    "});",
2362                    getLLVMStyle()));
2363   EXPECT_EQ("switch (n) {\n"
2364             "case 0: {\n"
2365             "  return false;\n"
2366             "}\n"
2367             "default: {\n"
2368             "  return true;\n"
2369             "}\n"
2370             "}",
2371             format("switch (n)\n"
2372                    "{\n"
2373                    "case 0: {\n"
2374                    "  return false;\n"
2375                    "}\n"
2376                    "default: {\n"
2377                    "  return true;\n"
2378                    "}\n"
2379                    "}",
2380                    getLLVMStyle()));
2381   verifyFormat("switch (a) {\n"
2382                "case (b):\n"
2383                "  return;\n"
2384                "}");
2385 
2386   verifyFormat("switch (a) {\n"
2387                "case some_namespace::\n"
2388                "    some_constant:\n"
2389                "  return;\n"
2390                "}",
2391                getLLVMStyleWithColumns(34));
2392 
2393   FormatStyle Style = getLLVMStyle();
2394   Style.IndentCaseLabels = true;
2395   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
2396   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
2397   Style.BraceWrapping.AfterCaseLabel = true;
2398   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
2399   EXPECT_EQ("switch (n)\n"
2400             "{\n"
2401             "  case 0:\n"
2402             "  {\n"
2403             "    return false;\n"
2404             "  }\n"
2405             "  default:\n"
2406             "  {\n"
2407             "    return true;\n"
2408             "  }\n"
2409             "}",
2410             format("switch (n) {\n"
2411                    "  case 0: {\n"
2412                    "    return false;\n"
2413                    "  }\n"
2414                    "  default: {\n"
2415                    "    return true;\n"
2416                    "  }\n"
2417                    "}",
2418                    Style));
2419   Style.BraceWrapping.AfterCaseLabel = false;
2420   EXPECT_EQ("switch (n)\n"
2421             "{\n"
2422             "  case 0: {\n"
2423             "    return false;\n"
2424             "  }\n"
2425             "  default: {\n"
2426             "    return true;\n"
2427             "  }\n"
2428             "}",
2429             format("switch (n) {\n"
2430                    "  case 0:\n"
2431                    "  {\n"
2432                    "    return false;\n"
2433                    "  }\n"
2434                    "  default:\n"
2435                    "  {\n"
2436                    "    return true;\n"
2437                    "  }\n"
2438                    "}",
2439                    Style));
2440   Style.IndentCaseLabels = false;
2441   Style.IndentCaseBlocks = true;
2442   EXPECT_EQ("switch (n)\n"
2443             "{\n"
2444             "case 0:\n"
2445             "  {\n"
2446             "    return false;\n"
2447             "  }\n"
2448             "case 1:\n"
2449             "  break;\n"
2450             "default:\n"
2451             "  {\n"
2452             "    return true;\n"
2453             "  }\n"
2454             "}",
2455             format("switch (n) {\n"
2456                    "case 0: {\n"
2457                    "  return false;\n"
2458                    "}\n"
2459                    "case 1:\n"
2460                    "  break;\n"
2461                    "default: {\n"
2462                    "  return true;\n"
2463                    "}\n"
2464                    "}",
2465                    Style));
2466   Style.IndentCaseLabels = true;
2467   Style.IndentCaseBlocks = true;
2468   EXPECT_EQ("switch (n)\n"
2469             "{\n"
2470             "  case 0:\n"
2471             "    {\n"
2472             "      return false;\n"
2473             "    }\n"
2474             "  case 1:\n"
2475             "    break;\n"
2476             "  default:\n"
2477             "    {\n"
2478             "      return true;\n"
2479             "    }\n"
2480             "}",
2481             format("switch (n) {\n"
2482                    "case 0: {\n"
2483                    "  return false;\n"
2484                    "}\n"
2485                    "case 1:\n"
2486                    "  break;\n"
2487                    "default: {\n"
2488                    "  return true;\n"
2489                    "}\n"
2490                    "}",
2491                    Style));
2492 }
2493 
2494 TEST_F(FormatTest, CaseRanges) {
2495   verifyFormat("switch (x) {\n"
2496                "case 'A' ... 'Z':\n"
2497                "case 1 ... 5:\n"
2498                "case a ... b:\n"
2499                "  break;\n"
2500                "}");
2501 }
2502 
2503 TEST_F(FormatTest, ShortEnums) {
2504   FormatStyle Style = getLLVMStyle();
2505   Style.AllowShortEnumsOnASingleLine = true;
2506   verifyFormat("enum { A, B, C } ShortEnum1, ShortEnum2;", Style);
2507   verifyFormat("typedef enum { A, B, C } ShortEnum1, ShortEnum2;", Style);
2508   Style.AllowShortEnumsOnASingleLine = false;
2509   verifyFormat("enum {\n"
2510                "  A,\n"
2511                "  B,\n"
2512                "  C\n"
2513                "} ShortEnum1, ShortEnum2;",
2514                Style);
2515   verifyFormat("typedef enum {\n"
2516                "  A,\n"
2517                "  B,\n"
2518                "  C\n"
2519                "} ShortEnum1, ShortEnum2;",
2520                Style);
2521   verifyFormat("enum {\n"
2522                "  A,\n"
2523                "} ShortEnum1, ShortEnum2;",
2524                Style);
2525   verifyFormat("typedef enum {\n"
2526                "  A,\n"
2527                "} ShortEnum1, ShortEnum2;",
2528                Style);
2529   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
2530   Style.BraceWrapping.AfterEnum = true;
2531   verifyFormat("enum\n"
2532                "{\n"
2533                "  A,\n"
2534                "  B,\n"
2535                "  C\n"
2536                "} ShortEnum1, ShortEnum2;",
2537                Style);
2538   verifyFormat("typedef enum\n"
2539                "{\n"
2540                "  A,\n"
2541                "  B,\n"
2542                "  C\n"
2543                "} ShortEnum1, ShortEnum2;",
2544                Style);
2545 }
2546 
2547 TEST_F(FormatTest, ShortCaseLabels) {
2548   FormatStyle Style = getLLVMStyle();
2549   Style.AllowShortCaseLabelsOnASingleLine = true;
2550   verifyFormat("switch (a) {\n"
2551                "case 1: x = 1; break;\n"
2552                "case 2: return;\n"
2553                "case 3:\n"
2554                "case 4:\n"
2555                "case 5: return;\n"
2556                "case 6: // comment\n"
2557                "  return;\n"
2558                "case 7:\n"
2559                "  // comment\n"
2560                "  return;\n"
2561                "case 8:\n"
2562                "  x = 8; // comment\n"
2563                "  break;\n"
2564                "default: y = 1; break;\n"
2565                "}",
2566                Style);
2567   verifyFormat("switch (a) {\n"
2568                "case 0: return; // comment\n"
2569                "case 1: break;  // comment\n"
2570                "case 2: return;\n"
2571                "// comment\n"
2572                "case 3: return;\n"
2573                "// comment 1\n"
2574                "// comment 2\n"
2575                "// comment 3\n"
2576                "case 4: break; /* comment */\n"
2577                "case 5:\n"
2578                "  // comment\n"
2579                "  break;\n"
2580                "case 6: /* comment */ x = 1; break;\n"
2581                "case 7: x = /* comment */ 1; break;\n"
2582                "case 8:\n"
2583                "  x = 1; /* comment */\n"
2584                "  break;\n"
2585                "case 9:\n"
2586                "  break; // comment line 1\n"
2587                "         // comment line 2\n"
2588                "}",
2589                Style);
2590   EXPECT_EQ("switch (a) {\n"
2591             "case 1:\n"
2592             "  x = 8;\n"
2593             "  // fall through\n"
2594             "case 2: x = 8;\n"
2595             "// comment\n"
2596             "case 3:\n"
2597             "  return; /* comment line 1\n"
2598             "           * comment line 2 */\n"
2599             "case 4: i = 8;\n"
2600             "// something else\n"
2601             "#if FOO\n"
2602             "case 5: break;\n"
2603             "#endif\n"
2604             "}",
2605             format("switch (a) {\n"
2606                    "case 1: x = 8;\n"
2607                    "  // fall through\n"
2608                    "case 2:\n"
2609                    "  x = 8;\n"
2610                    "// comment\n"
2611                    "case 3:\n"
2612                    "  return; /* comment line 1\n"
2613                    "           * comment line 2 */\n"
2614                    "case 4:\n"
2615                    "  i = 8;\n"
2616                    "// something else\n"
2617                    "#if FOO\n"
2618                    "case 5: break;\n"
2619                    "#endif\n"
2620                    "}",
2621                    Style));
2622   EXPECT_EQ("switch (a) {\n"
2623             "case 0:\n"
2624             "  return; // long long long long long long long long long long "
2625             "long long comment\n"
2626             "          // line\n"
2627             "}",
2628             format("switch (a) {\n"
2629                    "case 0: return; // long long long long long long long long "
2630                    "long long long long comment line\n"
2631                    "}",
2632                    Style));
2633   EXPECT_EQ("switch (a) {\n"
2634             "case 0:\n"
2635             "  return; /* long long long long long long long long long long "
2636             "long long comment\n"
2637             "             line */\n"
2638             "}",
2639             format("switch (a) {\n"
2640                    "case 0: return; /* long long long long long long long long "
2641                    "long long long long comment line */\n"
2642                    "}",
2643                    Style));
2644   verifyFormat("switch (a) {\n"
2645                "#if FOO\n"
2646                "case 0: return 0;\n"
2647                "#endif\n"
2648                "}",
2649                Style);
2650   verifyFormat("switch (a) {\n"
2651                "case 1: {\n"
2652                "}\n"
2653                "case 2: {\n"
2654                "  return;\n"
2655                "}\n"
2656                "case 3: {\n"
2657                "  x = 1;\n"
2658                "  return;\n"
2659                "}\n"
2660                "case 4:\n"
2661                "  if (x)\n"
2662                "    return;\n"
2663                "}",
2664                Style);
2665   Style.ColumnLimit = 21;
2666   verifyFormat("switch (a) {\n"
2667                "case 1: x = 1; break;\n"
2668                "case 2: return;\n"
2669                "case 3:\n"
2670                "case 4:\n"
2671                "case 5: return;\n"
2672                "default:\n"
2673                "  y = 1;\n"
2674                "  break;\n"
2675                "}",
2676                Style);
2677   Style.ColumnLimit = 80;
2678   Style.AllowShortCaseLabelsOnASingleLine = false;
2679   Style.IndentCaseLabels = true;
2680   EXPECT_EQ("switch (n) {\n"
2681             "  default /*comments*/:\n"
2682             "    return true;\n"
2683             "  case 0:\n"
2684             "    return false;\n"
2685             "}",
2686             format("switch (n) {\n"
2687                    "default/*comments*/:\n"
2688                    "  return true;\n"
2689                    "case 0:\n"
2690                    "  return false;\n"
2691                    "}",
2692                    Style));
2693   Style.AllowShortCaseLabelsOnASingleLine = true;
2694   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
2695   Style.BraceWrapping.AfterCaseLabel = true;
2696   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
2697   EXPECT_EQ("switch (n)\n"
2698             "{\n"
2699             "  case 0:\n"
2700             "  {\n"
2701             "    return false;\n"
2702             "  }\n"
2703             "  default:\n"
2704             "  {\n"
2705             "    return true;\n"
2706             "  }\n"
2707             "}",
2708             format("switch (n) {\n"
2709                    "  case 0: {\n"
2710                    "    return false;\n"
2711                    "  }\n"
2712                    "  default:\n"
2713                    "  {\n"
2714                    "    return true;\n"
2715                    "  }\n"
2716                    "}",
2717                    Style));
2718 }
2719 
2720 TEST_F(FormatTest, FormatsLabels) {
2721   verifyFormat("void f() {\n"
2722                "  some_code();\n"
2723                "test_label:\n"
2724                "  some_other_code();\n"
2725                "  {\n"
2726                "    some_more_code();\n"
2727                "  another_label:\n"
2728                "    some_more_code();\n"
2729                "  }\n"
2730                "}");
2731   verifyFormat("{\n"
2732                "  some_code();\n"
2733                "test_label:\n"
2734                "  some_other_code();\n"
2735                "}");
2736   verifyFormat("{\n"
2737                "  some_code();\n"
2738                "test_label:;\n"
2739                "  int i = 0;\n"
2740                "}");
2741   FormatStyle Style = getLLVMStyle();
2742   Style.IndentGotoLabels = false;
2743   verifyFormat("void f() {\n"
2744                "  some_code();\n"
2745                "test_label:\n"
2746                "  some_other_code();\n"
2747                "  {\n"
2748                "    some_more_code();\n"
2749                "another_label:\n"
2750                "    some_more_code();\n"
2751                "  }\n"
2752                "}",
2753                Style);
2754   verifyFormat("{\n"
2755                "  some_code();\n"
2756                "test_label:\n"
2757                "  some_other_code();\n"
2758                "}",
2759                Style);
2760   verifyFormat("{\n"
2761                "  some_code();\n"
2762                "test_label:;\n"
2763                "  int i = 0;\n"
2764                "}");
2765 }
2766 
2767 TEST_F(FormatTest, MultiLineControlStatements) {
2768   FormatStyle Style = getLLVMStyleWithColumns(20);
2769   Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom;
2770   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine;
2771   // Short lines should keep opening brace on same line.
2772   EXPECT_EQ("if (foo) {\n"
2773             "  bar();\n"
2774             "}",
2775             format("if(foo){bar();}", Style));
2776   EXPECT_EQ("if (foo) {\n"
2777             "  bar();\n"
2778             "} else {\n"
2779             "  baz();\n"
2780             "}",
2781             format("if(foo){bar();}else{baz();}", Style));
2782   EXPECT_EQ("if (foo && bar) {\n"
2783             "  baz();\n"
2784             "}",
2785             format("if(foo&&bar){baz();}", Style));
2786   EXPECT_EQ("if (foo) {\n"
2787             "  bar();\n"
2788             "} else if (baz) {\n"
2789             "  quux();\n"
2790             "}",
2791             format("if(foo){bar();}else if(baz){quux();}", Style));
2792   EXPECT_EQ(
2793       "if (foo) {\n"
2794       "  bar();\n"
2795       "} else if (baz) {\n"
2796       "  quux();\n"
2797       "} else {\n"
2798       "  foobar();\n"
2799       "}",
2800       format("if(foo){bar();}else if(baz){quux();}else{foobar();}", Style));
2801   EXPECT_EQ("for (;;) {\n"
2802             "  foo();\n"
2803             "}",
2804             format("for(;;){foo();}"));
2805   EXPECT_EQ("while (1) {\n"
2806             "  foo();\n"
2807             "}",
2808             format("while(1){foo();}", Style));
2809   EXPECT_EQ("switch (foo) {\n"
2810             "case bar:\n"
2811             "  return;\n"
2812             "}",
2813             format("switch(foo){case bar:return;}", Style));
2814   EXPECT_EQ("try {\n"
2815             "  foo();\n"
2816             "} catch (...) {\n"
2817             "  bar();\n"
2818             "}",
2819             format("try{foo();}catch(...){bar();}", Style));
2820   EXPECT_EQ("do {\n"
2821             "  foo();\n"
2822             "} while (bar &&\n"
2823             "         baz);",
2824             format("do{foo();}while(bar&&baz);", Style));
2825   // Long lines should put opening brace on new line.
2826   EXPECT_EQ("if (foo && bar &&\n"
2827             "    baz)\n"
2828             "{\n"
2829             "  quux();\n"
2830             "}",
2831             format("if(foo&&bar&&baz){quux();}", Style));
2832   EXPECT_EQ("if (foo && bar &&\n"
2833             "    baz)\n"
2834             "{\n"
2835             "  quux();\n"
2836             "}",
2837             format("if (foo && bar &&\n"
2838                    "    baz) {\n"
2839                    "  quux();\n"
2840                    "}",
2841                    Style));
2842   EXPECT_EQ("if (foo) {\n"
2843             "  bar();\n"
2844             "} else if (baz ||\n"
2845             "           quux)\n"
2846             "{\n"
2847             "  foobar();\n"
2848             "}",
2849             format("if(foo){bar();}else if(baz||quux){foobar();}", Style));
2850   EXPECT_EQ(
2851       "if (foo) {\n"
2852       "  bar();\n"
2853       "} else if (baz ||\n"
2854       "           quux)\n"
2855       "{\n"
2856       "  foobar();\n"
2857       "} else {\n"
2858       "  barbaz();\n"
2859       "}",
2860       format("if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}",
2861              Style));
2862   EXPECT_EQ("for (int i = 0;\n"
2863             "     i < 10; ++i)\n"
2864             "{\n"
2865             "  foo();\n"
2866             "}",
2867             format("for(int i=0;i<10;++i){foo();}", Style));
2868   EXPECT_EQ("foreach (int i,\n"
2869             "         list)\n"
2870             "{\n"
2871             "  foo();\n"
2872             "}",
2873             format("foreach(int i, list){foo();}", Style));
2874   Style.ColumnLimit =
2875       40; // to concentrate at brace wrapping, not line wrap due to column limit
2876   EXPECT_EQ("foreach (int i, list) {\n"
2877             "  foo();\n"
2878             "}",
2879             format("foreach(int i, list){foo();}", Style));
2880   Style.ColumnLimit =
2881       20; // to concentrate at brace wrapping, not line wrap due to column limit
2882   EXPECT_EQ("while (foo || bar ||\n"
2883             "       baz)\n"
2884             "{\n"
2885             "  quux();\n"
2886             "}",
2887             format("while(foo||bar||baz){quux();}", Style));
2888   EXPECT_EQ("switch (\n"
2889             "    foo = barbaz)\n"
2890             "{\n"
2891             "case quux:\n"
2892             "  return;\n"
2893             "}",
2894             format("switch(foo=barbaz){case quux:return;}", Style));
2895   EXPECT_EQ("try {\n"
2896             "  foo();\n"
2897             "} catch (\n"
2898             "    Exception &bar)\n"
2899             "{\n"
2900             "  baz();\n"
2901             "}",
2902             format("try{foo();}catch(Exception&bar){baz();}", Style));
2903   Style.ColumnLimit =
2904       40; // to concentrate at brace wrapping, not line wrap due to column limit
2905   EXPECT_EQ("try {\n"
2906             "  foo();\n"
2907             "} catch (Exception &bar) {\n"
2908             "  baz();\n"
2909             "}",
2910             format("try{foo();}catch(Exception&bar){baz();}", Style));
2911   Style.ColumnLimit =
2912       20; // to concentrate at brace wrapping, not line wrap due to column limit
2913 
2914   Style.BraceWrapping.BeforeElse = true;
2915   EXPECT_EQ(
2916       "if (foo) {\n"
2917       "  bar();\n"
2918       "}\n"
2919       "else if (baz ||\n"
2920       "         quux)\n"
2921       "{\n"
2922       "  foobar();\n"
2923       "}\n"
2924       "else {\n"
2925       "  barbaz();\n"
2926       "}",
2927       format("if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}",
2928              Style));
2929 
2930   Style.BraceWrapping.BeforeCatch = true;
2931   EXPECT_EQ("try {\n"
2932             "  foo();\n"
2933             "}\n"
2934             "catch (...) {\n"
2935             "  baz();\n"
2936             "}",
2937             format("try{foo();}catch(...){baz();}", Style));
2938 
2939   Style.BraceWrapping.AfterFunction = true;
2940   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine;
2941   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
2942   Style.ColumnLimit = 80;
2943   verifyFormat("void shortfunction() { bar(); }", Style);
2944 
2945   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
2946   verifyFormat("void shortfunction()\n"
2947                "{\n"
2948                "  bar();\n"
2949                "}",
2950                Style);
2951 }
2952 
2953 TEST_F(FormatTest, BeforeWhile) {
2954   FormatStyle Style = getLLVMStyle();
2955   Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom;
2956 
2957   verifyFormat("do {\n"
2958                "  foo();\n"
2959                "} while (1);",
2960                Style);
2961   Style.BraceWrapping.BeforeWhile = true;
2962   verifyFormat("do {\n"
2963                "  foo();\n"
2964                "}\n"
2965                "while (1);",
2966                Style);
2967 }
2968 
2969 //===----------------------------------------------------------------------===//
2970 // Tests for classes, namespaces, etc.
2971 //===----------------------------------------------------------------------===//
2972 
2973 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
2974   verifyFormat("class A {};");
2975 }
2976 
2977 TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
2978   verifyFormat("class A {\n"
2979                "public:\n"
2980                "public: // comment\n"
2981                "protected:\n"
2982                "private:\n"
2983                "  void f() {}\n"
2984                "};");
2985   verifyFormat("export class A {\n"
2986                "public:\n"
2987                "public: // comment\n"
2988                "protected:\n"
2989                "private:\n"
2990                "  void f() {}\n"
2991                "};");
2992   verifyGoogleFormat("class A {\n"
2993                      " public:\n"
2994                      " protected:\n"
2995                      " private:\n"
2996                      "  void f() {}\n"
2997                      "};");
2998   verifyGoogleFormat("export class A {\n"
2999                      " public:\n"
3000                      " protected:\n"
3001                      " private:\n"
3002                      "  void f() {}\n"
3003                      "};");
3004   verifyFormat("class A {\n"
3005                "public slots:\n"
3006                "  void f1() {}\n"
3007                "public Q_SLOTS:\n"
3008                "  void f2() {}\n"
3009                "protected slots:\n"
3010                "  void f3() {}\n"
3011                "protected Q_SLOTS:\n"
3012                "  void f4() {}\n"
3013                "private slots:\n"
3014                "  void f5() {}\n"
3015                "private Q_SLOTS:\n"
3016                "  void f6() {}\n"
3017                "signals:\n"
3018                "  void g1();\n"
3019                "Q_SIGNALS:\n"
3020                "  void g2();\n"
3021                "};");
3022 
3023   // Don't interpret 'signals' the wrong way.
3024   verifyFormat("signals.set();");
3025   verifyFormat("for (Signals signals : f()) {\n}");
3026   verifyFormat("{\n"
3027                "  signals.set(); // This needs indentation.\n"
3028                "}");
3029   verifyFormat("void f() {\n"
3030                "label:\n"
3031                "  signals.baz();\n"
3032                "}");
3033 }
3034 
3035 TEST_F(FormatTest, SeparatesLogicalBlocks) {
3036   EXPECT_EQ("class A {\n"
3037             "public:\n"
3038             "  void f();\n"
3039             "\n"
3040             "private:\n"
3041             "  void g() {}\n"
3042             "  // test\n"
3043             "protected:\n"
3044             "  int h;\n"
3045             "};",
3046             format("class A {\n"
3047                    "public:\n"
3048                    "void f();\n"
3049                    "private:\n"
3050                    "void g() {}\n"
3051                    "// test\n"
3052                    "protected:\n"
3053                    "int h;\n"
3054                    "};"));
3055   EXPECT_EQ("class A {\n"
3056             "protected:\n"
3057             "public:\n"
3058             "  void f();\n"
3059             "};",
3060             format("class A {\n"
3061                    "protected:\n"
3062                    "\n"
3063                    "public:\n"
3064                    "\n"
3065                    "  void f();\n"
3066                    "};"));
3067 
3068   // Even ensure proper spacing inside macros.
3069   EXPECT_EQ("#define B     \\\n"
3070             "  class A {   \\\n"
3071             "   protected: \\\n"
3072             "   public:    \\\n"
3073             "    void f(); \\\n"
3074             "  };",
3075             format("#define B     \\\n"
3076                    "  class A {   \\\n"
3077                    "   protected: \\\n"
3078                    "              \\\n"
3079                    "   public:    \\\n"
3080                    "              \\\n"
3081                    "    void f(); \\\n"
3082                    "  };",
3083                    getGoogleStyle()));
3084   // But don't remove empty lines after macros ending in access specifiers.
3085   EXPECT_EQ("#define A private:\n"
3086             "\n"
3087             "int i;",
3088             format("#define A         private:\n"
3089                    "\n"
3090                    "int              i;"));
3091 }
3092 
3093 TEST_F(FormatTest, FormatsClasses) {
3094   verifyFormat("class A : public B {};");
3095   verifyFormat("class A : public ::B {};");
3096 
3097   verifyFormat(
3098       "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3099       "                             public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
3100   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
3101                "    : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3102                "      public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
3103   verifyFormat(
3104       "class A : public B, public C, public D, public E, public F {};");
3105   verifyFormat("class AAAAAAAAAAAA : public B,\n"
3106                "                     public C,\n"
3107                "                     public D,\n"
3108                "                     public E,\n"
3109                "                     public F,\n"
3110                "                     public G {};");
3111 
3112   verifyFormat("class\n"
3113                "    ReallyReallyLongClassName {\n"
3114                "  int i;\n"
3115                "};",
3116                getLLVMStyleWithColumns(32));
3117   verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
3118                "                           aaaaaaaaaaaaaaaa> {};");
3119   verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n"
3120                "    : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n"
3121                "                                 aaaaaaaaaaaaaaaaaaaaaa> {};");
3122   verifyFormat("template <class R, class C>\n"
3123                "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n"
3124                "    : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};");
3125   verifyFormat("class ::A::B {};");
3126 }
3127 
3128 TEST_F(FormatTest, BreakInheritanceStyle) {
3129   FormatStyle StyleWithInheritanceBreakBeforeComma = getLLVMStyle();
3130   StyleWithInheritanceBreakBeforeComma.BreakInheritanceList =
3131       FormatStyle::BILS_BeforeComma;
3132   verifyFormat("class MyClass : public X {};",
3133                StyleWithInheritanceBreakBeforeComma);
3134   verifyFormat("class MyClass\n"
3135                "    : public X\n"
3136                "    , public Y {};",
3137                StyleWithInheritanceBreakBeforeComma);
3138   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA\n"
3139                "    : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n"
3140                "    , public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};",
3141                StyleWithInheritanceBreakBeforeComma);
3142   verifyFormat("struct aaaaaaaaaaaaa\n"
3143                "    : public aaaaaaaaaaaaaaaaaaa< // break\n"
3144                "          aaaaaaaaaaaaaaaa> {};",
3145                StyleWithInheritanceBreakBeforeComma);
3146 
3147   FormatStyle StyleWithInheritanceBreakAfterColon = getLLVMStyle();
3148   StyleWithInheritanceBreakAfterColon.BreakInheritanceList =
3149       FormatStyle::BILS_AfterColon;
3150   verifyFormat("class MyClass : public X {};",
3151                StyleWithInheritanceBreakAfterColon);
3152   verifyFormat("class MyClass : public X, public Y {};",
3153                StyleWithInheritanceBreakAfterColon);
3154   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA :\n"
3155                "    public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3156                "    public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};",
3157                StyleWithInheritanceBreakAfterColon);
3158   verifyFormat("struct aaaaaaaaaaaaa :\n"
3159                "    public aaaaaaaaaaaaaaaaaaa< // break\n"
3160                "        aaaaaaaaaaaaaaaa> {};",
3161                StyleWithInheritanceBreakAfterColon);
3162 
3163   FormatStyle StyleWithInheritanceBreakAfterComma = getLLVMStyle();
3164   StyleWithInheritanceBreakAfterComma.BreakInheritanceList =
3165       FormatStyle::BILS_AfterComma;
3166   verifyFormat("class MyClass : public X {};",
3167                StyleWithInheritanceBreakAfterComma);
3168   verifyFormat("class MyClass : public X,\n"
3169                "                public Y {};",
3170                StyleWithInheritanceBreakAfterComma);
3171   verifyFormat(
3172       "class AAAAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3173       "                               public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC "
3174       "{};",
3175       StyleWithInheritanceBreakAfterComma);
3176   verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
3177                "                           aaaaaaaaaaaaaaaa> {};",
3178                StyleWithInheritanceBreakAfterComma);
3179   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
3180                "    : public OnceBreak,\n"
3181                "      public AlwaysBreak,\n"
3182                "      EvenBasesFitInOneLine {};",
3183                StyleWithInheritanceBreakAfterComma);
3184 }
3185 
3186 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
3187   verifyFormat("class A {\n} a, b;");
3188   verifyFormat("struct A {\n} a, b;");
3189   verifyFormat("union A {\n} a;");
3190 }
3191 
3192 TEST_F(FormatTest, FormatsEnum) {
3193   verifyFormat("enum {\n"
3194                "  Zero,\n"
3195                "  One = 1,\n"
3196                "  Two = One + 1,\n"
3197                "  Three = (One + Two),\n"
3198                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
3199                "  Five = (One, Two, Three, Four, 5)\n"
3200                "};");
3201   verifyGoogleFormat("enum {\n"
3202                      "  Zero,\n"
3203                      "  One = 1,\n"
3204                      "  Two = One + 1,\n"
3205                      "  Three = (One + Two),\n"
3206                      "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
3207                      "  Five = (One, Two, Three, Four, 5)\n"
3208                      "};");
3209   verifyFormat("enum Enum {};");
3210   verifyFormat("enum {};");
3211   verifyFormat("enum X E {} d;");
3212   verifyFormat("enum __attribute__((...)) E {} d;");
3213   verifyFormat("enum __declspec__((...)) E {} d;");
3214   verifyFormat("enum {\n"
3215                "  Bar = Foo<int, int>::value\n"
3216                "};",
3217                getLLVMStyleWithColumns(30));
3218 
3219   verifyFormat("enum ShortEnum { A, B, C };");
3220   verifyGoogleFormat("enum ShortEnum { A, B, C };");
3221 
3222   EXPECT_EQ("enum KeepEmptyLines {\n"
3223             "  ONE,\n"
3224             "\n"
3225             "  TWO,\n"
3226             "\n"
3227             "  THREE\n"
3228             "}",
3229             format("enum KeepEmptyLines {\n"
3230                    "  ONE,\n"
3231                    "\n"
3232                    "  TWO,\n"
3233                    "\n"
3234                    "\n"
3235                    "  THREE\n"
3236                    "}"));
3237   verifyFormat("enum E { // comment\n"
3238                "  ONE,\n"
3239                "  TWO\n"
3240                "};\n"
3241                "int i;");
3242 
3243   FormatStyle EightIndent = getLLVMStyle();
3244   EightIndent.IndentWidth = 8;
3245   verifyFormat("enum {\n"
3246                "        VOID,\n"
3247                "        CHAR,\n"
3248                "        SHORT,\n"
3249                "        INT,\n"
3250                "        LONG,\n"
3251                "        SIGNED,\n"
3252                "        UNSIGNED,\n"
3253                "        BOOL,\n"
3254                "        FLOAT,\n"
3255                "        DOUBLE,\n"
3256                "        COMPLEX\n"
3257                "};",
3258                EightIndent);
3259 
3260   // Not enums.
3261   verifyFormat("enum X f() {\n"
3262                "  a();\n"
3263                "  return 42;\n"
3264                "}");
3265   verifyFormat("enum X Type::f() {\n"
3266                "  a();\n"
3267                "  return 42;\n"
3268                "}");
3269   verifyFormat("enum ::X f() {\n"
3270                "  a();\n"
3271                "  return 42;\n"
3272                "}");
3273   verifyFormat("enum ns::X f() {\n"
3274                "  a();\n"
3275                "  return 42;\n"
3276                "}");
3277 }
3278 
3279 TEST_F(FormatTest, FormatsEnumsWithErrors) {
3280   verifyFormat("enum Type {\n"
3281                "  One = 0; // These semicolons should be commas.\n"
3282                "  Two = 1;\n"
3283                "};");
3284   verifyFormat("namespace n {\n"
3285                "enum Type {\n"
3286                "  One,\n"
3287                "  Two, // missing };\n"
3288                "  int i;\n"
3289                "}\n"
3290                "void g() {}");
3291 }
3292 
3293 TEST_F(FormatTest, FormatsEnumStruct) {
3294   verifyFormat("enum struct {\n"
3295                "  Zero,\n"
3296                "  One = 1,\n"
3297                "  Two = One + 1,\n"
3298                "  Three = (One + Two),\n"
3299                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
3300                "  Five = (One, Two, Three, Four, 5)\n"
3301                "};");
3302   verifyFormat("enum struct Enum {};");
3303   verifyFormat("enum struct {};");
3304   verifyFormat("enum struct X E {} d;");
3305   verifyFormat("enum struct __attribute__((...)) E {} d;");
3306   verifyFormat("enum struct __declspec__((...)) E {} d;");
3307   verifyFormat("enum struct X f() {\n  a();\n  return 42;\n}");
3308 }
3309 
3310 TEST_F(FormatTest, FormatsEnumClass) {
3311   verifyFormat("enum class {\n"
3312                "  Zero,\n"
3313                "  One = 1,\n"
3314                "  Two = One + 1,\n"
3315                "  Three = (One + Two),\n"
3316                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
3317                "  Five = (One, Two, Three, Four, 5)\n"
3318                "};");
3319   verifyFormat("enum class Enum {};");
3320   verifyFormat("enum class {};");
3321   verifyFormat("enum class X E {} d;");
3322   verifyFormat("enum class __attribute__((...)) E {} d;");
3323   verifyFormat("enum class __declspec__((...)) E {} d;");
3324   verifyFormat("enum class X f() {\n  a();\n  return 42;\n}");
3325 }
3326 
3327 TEST_F(FormatTest, FormatsEnumTypes) {
3328   verifyFormat("enum X : int {\n"
3329                "  A, // Force multiple lines.\n"
3330                "  B\n"
3331                "};");
3332   verifyFormat("enum X : int { A, B };");
3333   verifyFormat("enum X : std::uint32_t { A, B };");
3334 }
3335 
3336 TEST_F(FormatTest, FormatsTypedefEnum) {
3337   FormatStyle Style = getLLVMStyleWithColumns(40);
3338   verifyFormat("typedef enum {} EmptyEnum;");
3339   verifyFormat("typedef enum { A, B, C } ShortEnum;");
3340   verifyFormat("typedef enum {\n"
3341                "  ZERO = 0,\n"
3342                "  ONE = 1,\n"
3343                "  TWO = 2,\n"
3344                "  THREE = 3\n"
3345                "} LongEnum;",
3346                Style);
3347   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
3348   Style.BraceWrapping.AfterEnum = true;
3349   verifyFormat("typedef enum {} EmptyEnum;");
3350   verifyFormat("typedef enum { A, B, C } ShortEnum;");
3351   verifyFormat("typedef enum\n"
3352                "{\n"
3353                "  ZERO = 0,\n"
3354                "  ONE = 1,\n"
3355                "  TWO = 2,\n"
3356                "  THREE = 3\n"
3357                "} LongEnum;",
3358                Style);
3359 }
3360 
3361 TEST_F(FormatTest, FormatsNSEnums) {
3362   verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }");
3363   verifyGoogleFormat(
3364       "typedef NS_CLOSED_ENUM(NSInteger, SomeName) { AAA, BBB }");
3365   verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n"
3366                      "  // Information about someDecentlyLongValue.\n"
3367                      "  someDecentlyLongValue,\n"
3368                      "  // Information about anotherDecentlyLongValue.\n"
3369                      "  anotherDecentlyLongValue,\n"
3370                      "  // Information about aThirdDecentlyLongValue.\n"
3371                      "  aThirdDecentlyLongValue\n"
3372                      "};");
3373   verifyGoogleFormat("typedef NS_CLOSED_ENUM(NSInteger, MyType) {\n"
3374                      "  // Information about someDecentlyLongValue.\n"
3375                      "  someDecentlyLongValue,\n"
3376                      "  // Information about anotherDecentlyLongValue.\n"
3377                      "  anotherDecentlyLongValue,\n"
3378                      "  // Information about aThirdDecentlyLongValue.\n"
3379                      "  aThirdDecentlyLongValue\n"
3380                      "};");
3381   verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n"
3382                      "  a = 1,\n"
3383                      "  b = 2,\n"
3384                      "  c = 3,\n"
3385                      "};");
3386   verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n"
3387                      "  a = 1,\n"
3388                      "  b = 2,\n"
3389                      "  c = 3,\n"
3390                      "};");
3391   verifyGoogleFormat("typedef CF_CLOSED_ENUM(NSInteger, MyType) {\n"
3392                      "  a = 1,\n"
3393                      "  b = 2,\n"
3394                      "  c = 3,\n"
3395                      "};");
3396   verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n"
3397                      "  a = 1,\n"
3398                      "  b = 2,\n"
3399                      "  c = 3,\n"
3400                      "};");
3401 }
3402 
3403 TEST_F(FormatTest, FormatsBitfields) {
3404   verifyFormat("struct Bitfields {\n"
3405                "  unsigned sClass : 8;\n"
3406                "  unsigned ValueKind : 2;\n"
3407                "};");
3408   verifyFormat("struct A {\n"
3409                "  int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n"
3410                "      bbbbbbbbbbbbbbbbbbbbbbbbb;\n"
3411                "};");
3412   verifyFormat("struct MyStruct {\n"
3413                "  uchar data;\n"
3414                "  uchar : 8;\n"
3415                "  uchar : 8;\n"
3416                "  uchar other;\n"
3417                "};");
3418   FormatStyle Style = getLLVMStyle();
3419   Style.BitFieldColonSpacing = FormatStyle::BFCS_None;
3420   verifyFormat("struct Bitfields {\n"
3421                "  unsigned sClass:8;\n"
3422                "  unsigned ValueKind:2;\n"
3423                "  uchar other;\n"
3424                "};",
3425                Style);
3426   verifyFormat("struct A {\n"
3427                "  int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:1,\n"
3428                "      bbbbbbbbbbbbbbbbbbbbbbbbb:2;\n"
3429                "};",
3430                Style);
3431   Style.BitFieldColonSpacing = FormatStyle::BFCS_Before;
3432   verifyFormat("struct Bitfields {\n"
3433                "  unsigned sClass :8;\n"
3434                "  unsigned ValueKind :2;\n"
3435                "  uchar other;\n"
3436                "};",
3437                Style);
3438   Style.BitFieldColonSpacing = FormatStyle::BFCS_After;
3439   verifyFormat("struct Bitfields {\n"
3440                "  unsigned sClass: 8;\n"
3441                "  unsigned ValueKind: 2;\n"
3442                "  uchar other;\n"
3443                "};",
3444                Style);
3445 }
3446 
3447 TEST_F(FormatTest, FormatsNamespaces) {
3448   FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle();
3449   LLVMWithNoNamespaceFix.FixNamespaceComments = false;
3450 
3451   verifyFormat("namespace some_namespace {\n"
3452                "class A {};\n"
3453                "void f() { f(); }\n"
3454                "}",
3455                LLVMWithNoNamespaceFix);
3456   verifyFormat("namespace N::inline D {\n"
3457                "class A {};\n"
3458                "void f() { f(); }\n"
3459                "}",
3460                LLVMWithNoNamespaceFix);
3461   verifyFormat("namespace N::inline D::E {\n"
3462                "class A {};\n"
3463                "void f() { f(); }\n"
3464                "}",
3465                LLVMWithNoNamespaceFix);
3466   verifyFormat("namespace [[deprecated(\"foo[bar\")]] some_namespace {\n"
3467                "class A {};\n"
3468                "void f() { f(); }\n"
3469                "}",
3470                LLVMWithNoNamespaceFix);
3471   verifyFormat("/* something */ namespace some_namespace {\n"
3472                "class A {};\n"
3473                "void f() { f(); }\n"
3474                "}",
3475                LLVMWithNoNamespaceFix);
3476   verifyFormat("namespace {\n"
3477                "class A {};\n"
3478                "void f() { f(); }\n"
3479                "}",
3480                LLVMWithNoNamespaceFix);
3481   verifyFormat("/* something */ namespace {\n"
3482                "class A {};\n"
3483                "void f() { f(); }\n"
3484                "}",
3485                LLVMWithNoNamespaceFix);
3486   verifyFormat("inline namespace X {\n"
3487                "class A {};\n"
3488                "void f() { f(); }\n"
3489                "}",
3490                LLVMWithNoNamespaceFix);
3491   verifyFormat("/* something */ inline namespace X {\n"
3492                "class A {};\n"
3493                "void f() { f(); }\n"
3494                "}",
3495                LLVMWithNoNamespaceFix);
3496   verifyFormat("export namespace X {\n"
3497                "class A {};\n"
3498                "void f() { f(); }\n"
3499                "}",
3500                LLVMWithNoNamespaceFix);
3501   verifyFormat("using namespace some_namespace;\n"
3502                "class A {};\n"
3503                "void f() { f(); }",
3504                LLVMWithNoNamespaceFix);
3505 
3506   // This code is more common than we thought; if we
3507   // layout this correctly the semicolon will go into
3508   // its own line, which is undesirable.
3509   verifyFormat("namespace {};", LLVMWithNoNamespaceFix);
3510   verifyFormat("namespace {\n"
3511                "class A {};\n"
3512                "};",
3513                LLVMWithNoNamespaceFix);
3514 
3515   verifyFormat("namespace {\n"
3516                "int SomeVariable = 0; // comment\n"
3517                "} // namespace",
3518                LLVMWithNoNamespaceFix);
3519   EXPECT_EQ("#ifndef HEADER_GUARD\n"
3520             "#define HEADER_GUARD\n"
3521             "namespace my_namespace {\n"
3522             "int i;\n"
3523             "} // my_namespace\n"
3524             "#endif // HEADER_GUARD",
3525             format("#ifndef HEADER_GUARD\n"
3526                    " #define HEADER_GUARD\n"
3527                    "   namespace my_namespace {\n"
3528                    "int i;\n"
3529                    "}    // my_namespace\n"
3530                    "#endif    // HEADER_GUARD",
3531                    LLVMWithNoNamespaceFix));
3532 
3533   EXPECT_EQ("namespace A::B {\n"
3534             "class C {};\n"
3535             "}",
3536             format("namespace A::B {\n"
3537                    "class C {};\n"
3538                    "}",
3539                    LLVMWithNoNamespaceFix));
3540 
3541   FormatStyle Style = getLLVMStyle();
3542   Style.NamespaceIndentation = FormatStyle::NI_All;
3543   EXPECT_EQ("namespace out {\n"
3544             "  int i;\n"
3545             "  namespace in {\n"
3546             "    int i;\n"
3547             "  } // namespace in\n"
3548             "} // namespace out",
3549             format("namespace out {\n"
3550                    "int i;\n"
3551                    "namespace in {\n"
3552                    "int i;\n"
3553                    "} // namespace in\n"
3554                    "} // namespace out",
3555                    Style));
3556 
3557   Style.NamespaceIndentation = FormatStyle::NI_Inner;
3558   EXPECT_EQ("namespace out {\n"
3559             "int i;\n"
3560             "namespace in {\n"
3561             "  int i;\n"
3562             "} // namespace in\n"
3563             "} // namespace out",
3564             format("namespace out {\n"
3565                    "int i;\n"
3566                    "namespace in {\n"
3567                    "int i;\n"
3568                    "} // namespace in\n"
3569                    "} // namespace out",
3570                    Style));
3571 
3572   Style.NamespaceIndentation = FormatStyle::NI_None;
3573   verifyFormat("template <class T>\n"
3574                "concept a_concept = X<>;\n"
3575                "namespace B {\n"
3576                "struct b_struct {};\n"
3577                "} // namespace B\n",
3578                Style);
3579   verifyFormat("template <int I> constexpr void foo requires(I == 42) {}\n"
3580                "namespace ns {\n"
3581                "void foo() {}\n"
3582                "} // namespace ns\n",
3583                Style);
3584 }
3585 
3586 TEST_F(FormatTest, NamespaceMacros) {
3587   FormatStyle Style = getLLVMStyle();
3588   Style.NamespaceMacros.push_back("TESTSUITE");
3589 
3590   verifyFormat("TESTSUITE(A) {\n"
3591                "int foo();\n"
3592                "} // TESTSUITE(A)",
3593                Style);
3594 
3595   verifyFormat("TESTSUITE(A, B) {\n"
3596                "int foo();\n"
3597                "} // TESTSUITE(A)",
3598                Style);
3599 
3600   // Properly indent according to NamespaceIndentation style
3601   Style.NamespaceIndentation = FormatStyle::NI_All;
3602   verifyFormat("TESTSUITE(A) {\n"
3603                "  int foo();\n"
3604                "} // TESTSUITE(A)",
3605                Style);
3606   verifyFormat("TESTSUITE(A) {\n"
3607                "  namespace B {\n"
3608                "    int foo();\n"
3609                "  } // namespace B\n"
3610                "} // TESTSUITE(A)",
3611                Style);
3612   verifyFormat("namespace A {\n"
3613                "  TESTSUITE(B) {\n"
3614                "    int foo();\n"
3615                "  } // TESTSUITE(B)\n"
3616                "} // namespace A",
3617                Style);
3618 
3619   Style.NamespaceIndentation = FormatStyle::NI_Inner;
3620   verifyFormat("TESTSUITE(A) {\n"
3621                "TESTSUITE(B) {\n"
3622                "  int foo();\n"
3623                "} // TESTSUITE(B)\n"
3624                "} // TESTSUITE(A)",
3625                Style);
3626   verifyFormat("TESTSUITE(A) {\n"
3627                "namespace B {\n"
3628                "  int foo();\n"
3629                "} // namespace B\n"
3630                "} // TESTSUITE(A)",
3631                Style);
3632   verifyFormat("namespace A {\n"
3633                "TESTSUITE(B) {\n"
3634                "  int foo();\n"
3635                "} // TESTSUITE(B)\n"
3636                "} // namespace A",
3637                Style);
3638 
3639   // Properly merge namespace-macros blocks in CompactNamespaces mode
3640   Style.NamespaceIndentation = FormatStyle::NI_None;
3641   Style.CompactNamespaces = true;
3642   verifyFormat("TESTSUITE(A) { TESTSUITE(B) {\n"
3643                "}} // TESTSUITE(A::B)",
3644                Style);
3645 
3646   EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n"
3647             "}} // TESTSUITE(out::in)",
3648             format("TESTSUITE(out) {\n"
3649                    "TESTSUITE(in) {\n"
3650                    "} // TESTSUITE(in)\n"
3651                    "} // TESTSUITE(out)",
3652                    Style));
3653 
3654   EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n"
3655             "}} // TESTSUITE(out::in)",
3656             format("TESTSUITE(out) {\n"
3657                    "TESTSUITE(in) {\n"
3658                    "} // TESTSUITE(in)\n"
3659                    "} // TESTSUITE(out)",
3660                    Style));
3661 
3662   // Do not merge different namespaces/macros
3663   EXPECT_EQ("namespace out {\n"
3664             "TESTSUITE(in) {\n"
3665             "} // TESTSUITE(in)\n"
3666             "} // namespace out",
3667             format("namespace out {\n"
3668                    "TESTSUITE(in) {\n"
3669                    "} // TESTSUITE(in)\n"
3670                    "} // namespace out",
3671                    Style));
3672   EXPECT_EQ("TESTSUITE(out) {\n"
3673             "namespace in {\n"
3674             "} // namespace in\n"
3675             "} // TESTSUITE(out)",
3676             format("TESTSUITE(out) {\n"
3677                    "namespace in {\n"
3678                    "} // namespace in\n"
3679                    "} // TESTSUITE(out)",
3680                    Style));
3681   Style.NamespaceMacros.push_back("FOOBAR");
3682   EXPECT_EQ("TESTSUITE(out) {\n"
3683             "FOOBAR(in) {\n"
3684             "} // FOOBAR(in)\n"
3685             "} // TESTSUITE(out)",
3686             format("TESTSUITE(out) {\n"
3687                    "FOOBAR(in) {\n"
3688                    "} // FOOBAR(in)\n"
3689                    "} // TESTSUITE(out)",
3690                    Style));
3691 }
3692 
3693 TEST_F(FormatTest, FormatsCompactNamespaces) {
3694   FormatStyle Style = getLLVMStyle();
3695   Style.CompactNamespaces = true;
3696   Style.NamespaceMacros.push_back("TESTSUITE");
3697 
3698   verifyFormat("namespace A { namespace B {\n"
3699                "}} // namespace A::B",
3700                Style);
3701 
3702   EXPECT_EQ("namespace out { namespace in {\n"
3703             "}} // namespace out::in",
3704             format("namespace out {\n"
3705                    "namespace in {\n"
3706                    "} // namespace in\n"
3707                    "} // namespace out",
3708                    Style));
3709 
3710   // Only namespaces which have both consecutive opening and end get compacted
3711   EXPECT_EQ("namespace out {\n"
3712             "namespace in1 {\n"
3713             "} // namespace in1\n"
3714             "namespace in2 {\n"
3715             "} // namespace in2\n"
3716             "} // namespace out",
3717             format("namespace out {\n"
3718                    "namespace in1 {\n"
3719                    "} // namespace in1\n"
3720                    "namespace in2 {\n"
3721                    "} // namespace in2\n"
3722                    "} // namespace out",
3723                    Style));
3724 
3725   EXPECT_EQ("namespace out {\n"
3726             "int i;\n"
3727             "namespace in {\n"
3728             "int j;\n"
3729             "} // namespace in\n"
3730             "int k;\n"
3731             "} // namespace out",
3732             format("namespace out { int i;\n"
3733                    "namespace in { int j; } // namespace in\n"
3734                    "int k; } // namespace out",
3735                    Style));
3736 
3737   EXPECT_EQ("namespace A { namespace B { namespace C {\n"
3738             "}}} // namespace A::B::C\n",
3739             format("namespace A { namespace B {\n"
3740                    "namespace C {\n"
3741                    "}} // namespace B::C\n"
3742                    "} // namespace A\n",
3743                    Style));
3744 
3745   Style.ColumnLimit = 40;
3746   EXPECT_EQ("namespace aaaaaaaaaa {\n"
3747             "namespace bbbbbbbbbb {\n"
3748             "}} // namespace aaaaaaaaaa::bbbbbbbbbb",
3749             format("namespace aaaaaaaaaa {\n"
3750                    "namespace bbbbbbbbbb {\n"
3751                    "} // namespace bbbbbbbbbb\n"
3752                    "} // namespace aaaaaaaaaa",
3753                    Style));
3754 
3755   EXPECT_EQ("namespace aaaaaa { namespace bbbbbb {\n"
3756             "namespace cccccc {\n"
3757             "}}} // namespace aaaaaa::bbbbbb::cccccc",
3758             format("namespace aaaaaa {\n"
3759                    "namespace bbbbbb {\n"
3760                    "namespace cccccc {\n"
3761                    "} // namespace cccccc\n"
3762                    "} // namespace bbbbbb\n"
3763                    "} // namespace aaaaaa",
3764                    Style));
3765   Style.ColumnLimit = 80;
3766 
3767   // Extra semicolon after 'inner' closing brace prevents merging
3768   EXPECT_EQ("namespace out { namespace in {\n"
3769             "}; } // namespace out::in",
3770             format("namespace out {\n"
3771                    "namespace in {\n"
3772                    "}; // namespace in\n"
3773                    "} // namespace out",
3774                    Style));
3775 
3776   // Extra semicolon after 'outer' closing brace is conserved
3777   EXPECT_EQ("namespace out { namespace in {\n"
3778             "}}; // namespace out::in",
3779             format("namespace out {\n"
3780                    "namespace in {\n"
3781                    "} // namespace in\n"
3782                    "}; // namespace out",
3783                    Style));
3784 
3785   Style.NamespaceIndentation = FormatStyle::NI_All;
3786   EXPECT_EQ("namespace out { namespace in {\n"
3787             "  int i;\n"
3788             "}} // namespace out::in",
3789             format("namespace out {\n"
3790                    "namespace in {\n"
3791                    "int i;\n"
3792                    "} // namespace in\n"
3793                    "} // namespace out",
3794                    Style));
3795   EXPECT_EQ("namespace out { namespace mid {\n"
3796             "  namespace in {\n"
3797             "    int j;\n"
3798             "  } // namespace in\n"
3799             "  int k;\n"
3800             "}} // namespace out::mid",
3801             format("namespace out { namespace mid {\n"
3802                    "namespace in { int j; } // namespace in\n"
3803                    "int k; }} // namespace out::mid",
3804                    Style));
3805 
3806   Style.NamespaceIndentation = FormatStyle::NI_Inner;
3807   EXPECT_EQ("namespace out { namespace in {\n"
3808             "  int i;\n"
3809             "}} // namespace out::in",
3810             format("namespace out {\n"
3811                    "namespace in {\n"
3812                    "int i;\n"
3813                    "} // namespace in\n"
3814                    "} // namespace out",
3815                    Style));
3816   EXPECT_EQ("namespace out { namespace mid { namespace in {\n"
3817             "  int i;\n"
3818             "}}} // namespace out::mid::in",
3819             format("namespace out {\n"
3820                    "namespace mid {\n"
3821                    "namespace in {\n"
3822                    "int i;\n"
3823                    "} // namespace in\n"
3824                    "} // namespace mid\n"
3825                    "} // namespace out",
3826                    Style));
3827 }
3828 
3829 TEST_F(FormatTest, FormatsExternC) {
3830   verifyFormat("extern \"C\" {\nint a;");
3831   verifyFormat("extern \"C\" {}");
3832   verifyFormat("extern \"C\" {\n"
3833                "int foo();\n"
3834                "}");
3835   verifyFormat("extern \"C\" int foo() {}");
3836   verifyFormat("extern \"C\" int foo();");
3837   verifyFormat("extern \"C\" int foo() {\n"
3838                "  int i = 42;\n"
3839                "  return i;\n"
3840                "}");
3841 
3842   FormatStyle Style = getLLVMStyle();
3843   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
3844   Style.BraceWrapping.AfterFunction = true;
3845   verifyFormat("extern \"C\" int foo() {}", Style);
3846   verifyFormat("extern \"C\" int foo();", Style);
3847   verifyFormat("extern \"C\" int foo()\n"
3848                "{\n"
3849                "  int i = 42;\n"
3850                "  return i;\n"
3851                "}",
3852                Style);
3853 
3854   Style.BraceWrapping.AfterExternBlock = true;
3855   Style.BraceWrapping.SplitEmptyRecord = false;
3856   verifyFormat("extern \"C\"\n"
3857                "{}",
3858                Style);
3859   verifyFormat("extern \"C\"\n"
3860                "{\n"
3861                "  int foo();\n"
3862                "}",
3863                Style);
3864 }
3865 
3866 TEST_F(FormatTest, IndentExternBlockStyle) {
3867   FormatStyle Style = getLLVMStyle();
3868   Style.IndentWidth = 2;
3869 
3870   Style.IndentExternBlock = FormatStyle::IEBS_Indent;
3871   verifyFormat("extern \"C\" { /*9*/\n"
3872                "}",
3873                Style);
3874   verifyFormat("extern \"C\" {\n"
3875                "  int foo10();\n"
3876                "}",
3877                Style);
3878 
3879   Style.IndentExternBlock = FormatStyle::IEBS_NoIndent;
3880   verifyFormat("extern \"C\" { /*11*/\n"
3881                "}",
3882                Style);
3883   verifyFormat("extern \"C\" {\n"
3884                "int foo12();\n"
3885                "}",
3886                Style);
3887 
3888   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
3889   Style.BraceWrapping.AfterExternBlock = true;
3890   Style.IndentExternBlock = FormatStyle::IEBS_Indent;
3891   verifyFormat("extern \"C\"\n"
3892                "{ /*13*/\n"
3893                "}",
3894                Style);
3895   verifyFormat("extern \"C\"\n{\n"
3896                "  int foo14();\n"
3897                "}",
3898                Style);
3899 
3900   Style.BraceWrapping.AfterExternBlock = false;
3901   Style.IndentExternBlock = FormatStyle::IEBS_NoIndent;
3902   verifyFormat("extern \"C\" { /*15*/\n"
3903                "}",
3904                Style);
3905   verifyFormat("extern \"C\" {\n"
3906                "int foo16();\n"
3907                "}",
3908                Style);
3909 
3910   Style.BraceWrapping.AfterExternBlock = true;
3911   verifyFormat("extern \"C\"\n"
3912                "{ /*13*/\n"
3913                "}",
3914                Style);
3915   verifyFormat("extern \"C\"\n"
3916                "{\n"
3917                "int foo14();\n"
3918                "}",
3919                Style);
3920 
3921   Style.IndentExternBlock = FormatStyle::IEBS_Indent;
3922   verifyFormat("extern \"C\"\n"
3923                "{ /*13*/\n"
3924                "}",
3925                Style);
3926   verifyFormat("extern \"C\"\n"
3927                "{\n"
3928                "  int foo14();\n"
3929                "}",
3930                Style);
3931 }
3932 
3933 TEST_F(FormatTest, FormatsInlineASM) {
3934   verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
3935   verifyFormat("asm(\"nop\" ::: \"memory\");");
3936   verifyFormat(
3937       "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
3938       "    \"cpuid\\n\\t\"\n"
3939       "    \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
3940       "    : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
3941       "    : \"a\"(value));");
3942   EXPECT_EQ(
3943       "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
3944       "  __asm {\n"
3945       "        mov     edx,[that] // vtable in edx\n"
3946       "        mov     eax,methodIndex\n"
3947       "        call    [edx][eax*4] // stdcall\n"
3948       "  }\n"
3949       "}",
3950       format("void NS_InvokeByIndex(void *that,   unsigned int methodIndex) {\n"
3951              "    __asm {\n"
3952              "        mov     edx,[that] // vtable in edx\n"
3953              "        mov     eax,methodIndex\n"
3954              "        call    [edx][eax*4] // stdcall\n"
3955              "    }\n"
3956              "}"));
3957   EXPECT_EQ("_asm {\n"
3958             "  xor eax, eax;\n"
3959             "  cpuid;\n"
3960             "}",
3961             format("_asm {\n"
3962                    "  xor eax, eax;\n"
3963                    "  cpuid;\n"
3964                    "}"));
3965   verifyFormat("void function() {\n"
3966                "  // comment\n"
3967                "  asm(\"\");\n"
3968                "}");
3969   EXPECT_EQ("__asm {\n"
3970             "}\n"
3971             "int i;",
3972             format("__asm   {\n"
3973                    "}\n"
3974                    "int   i;"));
3975 }
3976 
3977 TEST_F(FormatTest, FormatTryCatch) {
3978   verifyFormat("try {\n"
3979                "  throw a * b;\n"
3980                "} catch (int a) {\n"
3981                "  // Do nothing.\n"
3982                "} catch (...) {\n"
3983                "  exit(42);\n"
3984                "}");
3985 
3986   // Function-level try statements.
3987   verifyFormat("int f() try { return 4; } catch (...) {\n"
3988                "  return 5;\n"
3989                "}");
3990   verifyFormat("class A {\n"
3991                "  int a;\n"
3992                "  A() try : a(0) {\n"
3993                "  } catch (...) {\n"
3994                "    throw;\n"
3995                "  }\n"
3996                "};\n");
3997   verifyFormat("class A {\n"
3998                "  int a;\n"
3999                "  A() try : a(0), b{1} {\n"
4000                "  } catch (...) {\n"
4001                "    throw;\n"
4002                "  }\n"
4003                "};\n");
4004   verifyFormat("class A {\n"
4005                "  int a;\n"
4006                "  A() try : a(0), b{1}, c{2} {\n"
4007                "  } catch (...) {\n"
4008                "    throw;\n"
4009                "  }\n"
4010                "};\n");
4011   verifyFormat("class A {\n"
4012                "  int a;\n"
4013                "  A() try : a(0), b{1}, c{2} {\n"
4014                "    { // New scope.\n"
4015                "    }\n"
4016                "  } catch (...) {\n"
4017                "    throw;\n"
4018                "  }\n"
4019                "};\n");
4020 
4021   // Incomplete try-catch blocks.
4022   verifyIncompleteFormat("try {} catch (");
4023 }
4024 
4025 TEST_F(FormatTest, FormatTryAsAVariable) {
4026   verifyFormat("int try;");
4027   verifyFormat("int try, size;");
4028   verifyFormat("try = foo();");
4029   verifyFormat("if (try < size) {\n  return true;\n}");
4030 
4031   verifyFormat("int catch;");
4032   verifyFormat("int catch, size;");
4033   verifyFormat("catch = foo();");
4034   verifyFormat("if (catch < size) {\n  return true;\n}");
4035 
4036   FormatStyle Style = getLLVMStyle();
4037   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4038   Style.BraceWrapping.AfterFunction = true;
4039   Style.BraceWrapping.BeforeCatch = true;
4040   verifyFormat("try {\n"
4041                "  int bar = 1;\n"
4042                "}\n"
4043                "catch (...) {\n"
4044                "  int bar = 1;\n"
4045                "}",
4046                Style);
4047   verifyFormat("#if NO_EX\n"
4048                "try\n"
4049                "#endif\n"
4050                "{\n"
4051                "}\n"
4052                "#if NO_EX\n"
4053                "catch (...) {\n"
4054                "}",
4055                Style);
4056   verifyFormat("try /* abc */ {\n"
4057                "  int bar = 1;\n"
4058                "}\n"
4059                "catch (...) {\n"
4060                "  int bar = 1;\n"
4061                "}",
4062                Style);
4063   verifyFormat("try\n"
4064                "// abc\n"
4065                "{\n"
4066                "  int bar = 1;\n"
4067                "}\n"
4068                "catch (...) {\n"
4069                "  int bar = 1;\n"
4070                "}",
4071                Style);
4072 }
4073 
4074 TEST_F(FormatTest, FormatSEHTryCatch) {
4075   verifyFormat("__try {\n"
4076                "  int a = b * c;\n"
4077                "} __except (EXCEPTION_EXECUTE_HANDLER) {\n"
4078                "  // Do nothing.\n"
4079                "}");
4080 
4081   verifyFormat("__try {\n"
4082                "  int a = b * c;\n"
4083                "} __finally {\n"
4084                "  // Do nothing.\n"
4085                "}");
4086 
4087   verifyFormat("DEBUG({\n"
4088                "  __try {\n"
4089                "  } __finally {\n"
4090                "  }\n"
4091                "});\n");
4092 }
4093 
4094 TEST_F(FormatTest, IncompleteTryCatchBlocks) {
4095   verifyFormat("try {\n"
4096                "  f();\n"
4097                "} catch {\n"
4098                "  g();\n"
4099                "}");
4100   verifyFormat("try {\n"
4101                "  f();\n"
4102                "} catch (A a) MACRO(x) {\n"
4103                "  g();\n"
4104                "} catch (B b) MACRO(x) {\n"
4105                "  g();\n"
4106                "}");
4107 }
4108 
4109 TEST_F(FormatTest, FormatTryCatchBraceStyles) {
4110   FormatStyle Style = getLLVMStyle();
4111   for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla,
4112                           FormatStyle::BS_WebKit}) {
4113     Style.BreakBeforeBraces = BraceStyle;
4114     verifyFormat("try {\n"
4115                  "  // something\n"
4116                  "} catch (...) {\n"
4117                  "  // something\n"
4118                  "}",
4119                  Style);
4120   }
4121   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
4122   verifyFormat("try {\n"
4123                "  // something\n"
4124                "}\n"
4125                "catch (...) {\n"
4126                "  // something\n"
4127                "}",
4128                Style);
4129   verifyFormat("__try {\n"
4130                "  // something\n"
4131                "}\n"
4132                "__finally {\n"
4133                "  // something\n"
4134                "}",
4135                Style);
4136   verifyFormat("@try {\n"
4137                "  // something\n"
4138                "}\n"
4139                "@finally {\n"
4140                "  // something\n"
4141                "}",
4142                Style);
4143   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
4144   verifyFormat("try\n"
4145                "{\n"
4146                "  // something\n"
4147                "}\n"
4148                "catch (...)\n"
4149                "{\n"
4150                "  // something\n"
4151                "}",
4152                Style);
4153   Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
4154   verifyFormat("try\n"
4155                "  {\n"
4156                "  // something white\n"
4157                "  }\n"
4158                "catch (...)\n"
4159                "  {\n"
4160                "  // something white\n"
4161                "  }",
4162                Style);
4163   Style.BreakBeforeBraces = FormatStyle::BS_GNU;
4164   verifyFormat("try\n"
4165                "  {\n"
4166                "    // something\n"
4167                "  }\n"
4168                "catch (...)\n"
4169                "  {\n"
4170                "    // something\n"
4171                "  }",
4172                Style);
4173   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4174   Style.BraceWrapping.BeforeCatch = true;
4175   verifyFormat("try {\n"
4176                "  // something\n"
4177                "}\n"
4178                "catch (...) {\n"
4179                "  // something\n"
4180                "}",
4181                Style);
4182 }
4183 
4184 TEST_F(FormatTest, StaticInitializers) {
4185   verifyFormat("static SomeClass SC = {1, 'a'};");
4186 
4187   verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n"
4188                "    100000000, "
4189                "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};");
4190 
4191   // Here, everything other than the "}" would fit on a line.
4192   verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
4193                "    10000000000000000000000000};");
4194   EXPECT_EQ("S s = {a,\n"
4195             "\n"
4196             "       b};",
4197             format("S s = {\n"
4198                    "  a,\n"
4199                    "\n"
4200                    "  b\n"
4201                    "};"));
4202 
4203   // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
4204   // line. However, the formatting looks a bit off and this probably doesn't
4205   // happen often in practice.
4206   verifyFormat("static int Variable[1] = {\n"
4207                "    {1000000000000000000000000000000000000}};",
4208                getLLVMStyleWithColumns(40));
4209 }
4210 
4211 TEST_F(FormatTest, DesignatedInitializers) {
4212   verifyFormat("const struct A a = {.a = 1, .b = 2};");
4213   verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n"
4214                "                    .bbbbbbbbbb = 2,\n"
4215                "                    .cccccccccc = 3,\n"
4216                "                    .dddddddddd = 4,\n"
4217                "                    .eeeeeeeeee = 5};");
4218   verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
4219                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
4220                "    .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
4221                "    .ccccccccccccccccccccccccccc = 3,\n"
4222                "    .ddddddddddddddddddddddddddd = 4,\n"
4223                "    .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};");
4224 
4225   verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
4226 
4227   verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
4228   verifyFormat("const struct A a = {[1] = aaaaaaaaaa,\n"
4229                "                    [2] = bbbbbbbbbb,\n"
4230                "                    [3] = cccccccccc,\n"
4231                "                    [4] = dddddddddd,\n"
4232                "                    [5] = eeeeeeeeee};");
4233   verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
4234                "    [1] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4235                "    [2] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
4236                "    [3] = cccccccccccccccccccccccccccccccccccccc,\n"
4237                "    [4] = dddddddddddddddddddddddddddddddddddddd,\n"
4238                "    [5] = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee};");
4239 }
4240 
4241 TEST_F(FormatTest, NestedStaticInitializers) {
4242   verifyFormat("static A x = {{{}}};\n");
4243   verifyFormat("static A x = {{{init1, init2, init3, init4},\n"
4244                "               {init1, init2, init3, init4}}};",
4245                getLLVMStyleWithColumns(50));
4246 
4247   verifyFormat("somes Status::global_reps[3] = {\n"
4248                "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
4249                "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
4250                "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};",
4251                getLLVMStyleWithColumns(60));
4252   verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
4253                      "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
4254                      "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
4255                      "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
4256   verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n"
4257                "                  {rect.fRight - rect.fLeft, rect.fBottom - "
4258                "rect.fTop}};");
4259 
4260   verifyFormat(
4261       "SomeArrayOfSomeType a = {\n"
4262       "    {{1, 2, 3},\n"
4263       "     {1, 2, 3},\n"
4264       "     {111111111111111111111111111111, 222222222222222222222222222222,\n"
4265       "      333333333333333333333333333333},\n"
4266       "     {1, 2, 3},\n"
4267       "     {1, 2, 3}}};");
4268   verifyFormat(
4269       "SomeArrayOfSomeType a = {\n"
4270       "    {{1, 2, 3}},\n"
4271       "    {{1, 2, 3}},\n"
4272       "    {{111111111111111111111111111111, 222222222222222222222222222222,\n"
4273       "      333333333333333333333333333333}},\n"
4274       "    {{1, 2, 3}},\n"
4275       "    {{1, 2, 3}}};");
4276 
4277   verifyFormat("struct {\n"
4278                "  unsigned bit;\n"
4279                "  const char *const name;\n"
4280                "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n"
4281                "                 {kOsWin, \"Windows\"},\n"
4282                "                 {kOsLinux, \"Linux\"},\n"
4283                "                 {kOsCrOS, \"Chrome OS\"}};");
4284   verifyFormat("struct {\n"
4285                "  unsigned bit;\n"
4286                "  const char *const name;\n"
4287                "} kBitsToOs[] = {\n"
4288                "    {kOsMac, \"Mac\"},\n"
4289                "    {kOsWin, \"Windows\"},\n"
4290                "    {kOsLinux, \"Linux\"},\n"
4291                "    {kOsCrOS, \"Chrome OS\"},\n"
4292                "};");
4293 }
4294 
4295 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
4296   verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
4297                "                      \\\n"
4298                "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
4299 }
4300 
4301 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
4302   verifyFormat("virtual void write(ELFWriter *writerrr,\n"
4303                "                   OwningPtr<FileOutputBuffer> &buffer) = 0;");
4304 
4305   // Do break defaulted and deleted functions.
4306   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
4307                "    default;",
4308                getLLVMStyleWithColumns(40));
4309   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
4310                "    delete;",
4311                getLLVMStyleWithColumns(40));
4312 }
4313 
4314 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) {
4315   verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3",
4316                getLLVMStyleWithColumns(40));
4317   verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
4318                getLLVMStyleWithColumns(40));
4319   EXPECT_EQ("#define Q                              \\\n"
4320             "  \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\"    \\\n"
4321             "  \"aaaaaaaa.cpp\"",
4322             format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
4323                    getLLVMStyleWithColumns(40)));
4324 }
4325 
4326 TEST_F(FormatTest, UnderstandsLinePPDirective) {
4327   EXPECT_EQ("# 123 \"A string literal\"",
4328             format("   #     123    \"A string literal\""));
4329 }
4330 
4331 TEST_F(FormatTest, LayoutUnknownPPDirective) {
4332   EXPECT_EQ("#;", format("#;"));
4333   verifyFormat("#\n;\n;\n;");
4334 }
4335 
4336 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
4337   EXPECT_EQ("#line 42 \"test\"\n",
4338             format("#  \\\n  line  \\\n  42  \\\n  \"test\"\n"));
4339   EXPECT_EQ("#define A B\n", format("#  \\\n define  \\\n    A  \\\n       B\n",
4340                                     getLLVMStyleWithColumns(12)));
4341 }
4342 
4343 TEST_F(FormatTest, EndOfFileEndsPPDirective) {
4344   EXPECT_EQ("#line 42 \"test\"",
4345             format("#  \\\n  line  \\\n  42  \\\n  \"test\""));
4346   EXPECT_EQ("#define A B", format("#  \\\n define  \\\n    A  \\\n       B"));
4347 }
4348 
4349 TEST_F(FormatTest, DoesntRemoveUnknownTokens) {
4350   verifyFormat("#define A \\x20");
4351   verifyFormat("#define A \\ x20");
4352   EXPECT_EQ("#define A \\ x20", format("#define A \\   x20"));
4353   verifyFormat("#define A ''");
4354   verifyFormat("#define A ''qqq");
4355   verifyFormat("#define A `qqq");
4356   verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");");
4357   EXPECT_EQ("const char *c = STRINGIFY(\n"
4358             "\\na : b);",
4359             format("const char * c = STRINGIFY(\n"
4360                    "\\na : b);"));
4361 
4362   verifyFormat("a\r\\");
4363   verifyFormat("a\v\\");
4364   verifyFormat("a\f\\");
4365 }
4366 
4367 TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) {
4368   FormatStyle style = getChromiumStyle(FormatStyle::LK_Cpp);
4369   style.IndentWidth = 4;
4370   style.PPIndentWidth = 1;
4371 
4372   style.IndentPPDirectives = FormatStyle::PPDIS_None;
4373   verifyFormat("#ifdef __linux__\n"
4374                "void foo() {\n"
4375                "    int x = 0;\n"
4376                "}\n"
4377                "#define FOO\n"
4378                "#endif\n"
4379                "void bar() {\n"
4380                "    int y = 0;\n"
4381                "}\n",
4382                style);
4383 
4384   style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
4385   verifyFormat("#ifdef __linux__\n"
4386                "void foo() {\n"
4387                "    int x = 0;\n"
4388                "}\n"
4389                "# define FOO foo\n"
4390                "#endif\n"
4391                "void bar() {\n"
4392                "    int y = 0;\n"
4393                "}\n",
4394                style);
4395 
4396   style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash;
4397   verifyFormat("#ifdef __linux__\n"
4398                "void foo() {\n"
4399                "    int x = 0;\n"
4400                "}\n"
4401                " #define FOO foo\n"
4402                "#endif\n"
4403                "void bar() {\n"
4404                "    int y = 0;\n"
4405                "}\n",
4406                style);
4407 }
4408 
4409 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
4410   verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
4411   verifyFormat("#define A( \\\n    BB)", getLLVMStyleWithColumns(12));
4412   verifyFormat("#define A( \\\n    A, B)", getLLVMStyleWithColumns(12));
4413   // FIXME: We never break before the macro name.
4414   verifyFormat("#define AA( \\\n    B)", getLLVMStyleWithColumns(12));
4415 
4416   verifyFormat("#define A A\n#define A A");
4417   verifyFormat("#define A(X) A\n#define A A");
4418 
4419   verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
4420   verifyFormat("#define Something    \\\n  Other", getLLVMStyleWithColumns(22));
4421 }
4422 
4423 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
4424   EXPECT_EQ("// somecomment\n"
4425             "#include \"a.h\"\n"
4426             "#define A(  \\\n"
4427             "    A, B)\n"
4428             "#include \"b.h\"\n"
4429             "// somecomment\n",
4430             format("  // somecomment\n"
4431                    "  #include \"a.h\"\n"
4432                    "#define A(A,\\\n"
4433                    "    B)\n"
4434                    "    #include \"b.h\"\n"
4435                    " // somecomment\n",
4436                    getLLVMStyleWithColumns(13)));
4437 }
4438 
4439 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); }
4440 
4441 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
4442   EXPECT_EQ("#define A    \\\n"
4443             "  c;         \\\n"
4444             "  e;\n"
4445             "f;",
4446             format("#define A c; e;\n"
4447                    "f;",
4448                    getLLVMStyleWithColumns(14)));
4449 }
4450 
4451 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
4452 
4453 TEST_F(FormatTest, MacroDefinitionInsideStatement) {
4454   EXPECT_EQ("int x,\n"
4455             "#define A\n"
4456             "    y;",
4457             format("int x,\n#define A\ny;"));
4458 }
4459 
4460 TEST_F(FormatTest, HashInMacroDefinition) {
4461   EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle()));
4462   EXPECT_EQ("#define A(c) u#c", format("#define A(c) u#c", getLLVMStyle()));
4463   EXPECT_EQ("#define A(c) U#c", format("#define A(c) U#c", getLLVMStyle()));
4464   EXPECT_EQ("#define A(c) u8#c", format("#define A(c) u8#c", getLLVMStyle()));
4465   EXPECT_EQ("#define A(c) LR#c", format("#define A(c) LR#c", getLLVMStyle()));
4466   EXPECT_EQ("#define A(c) uR#c", format("#define A(c) uR#c", getLLVMStyle()));
4467   EXPECT_EQ("#define A(c) UR#c", format("#define A(c) UR#c", getLLVMStyle()));
4468   EXPECT_EQ("#define A(c) u8R#c", format("#define A(c) u8R#c", getLLVMStyle()));
4469   verifyFormat("#define A \\\n  b #c;", getLLVMStyleWithColumns(11));
4470   verifyFormat("#define A  \\\n"
4471                "  {        \\\n"
4472                "    f(#c); \\\n"
4473                "  }",
4474                getLLVMStyleWithColumns(11));
4475 
4476   verifyFormat("#define A(X)         \\\n"
4477                "  void function##X()",
4478                getLLVMStyleWithColumns(22));
4479 
4480   verifyFormat("#define A(a, b, c)   \\\n"
4481                "  void a##b##c()",
4482                getLLVMStyleWithColumns(22));
4483 
4484   verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
4485 }
4486 
4487 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
4488   EXPECT_EQ("#define A (x)", format("#define A (x)"));
4489   EXPECT_EQ("#define A(x)", format("#define A(x)"));
4490 
4491   FormatStyle Style = getLLVMStyle();
4492   Style.SpaceBeforeParens = FormatStyle::SBPO_Never;
4493   verifyFormat("#define true ((foo)1)", Style);
4494   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
4495   verifyFormat("#define false((foo)0)", Style);
4496 }
4497 
4498 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
4499   EXPECT_EQ("#define A b;", format("#define A \\\n"
4500                                    "          \\\n"
4501                                    "  b;",
4502                                    getLLVMStyleWithColumns(25)));
4503   EXPECT_EQ("#define A \\\n"
4504             "          \\\n"
4505             "  a;      \\\n"
4506             "  b;",
4507             format("#define A \\\n"
4508                    "          \\\n"
4509                    "  a;      \\\n"
4510                    "  b;",
4511                    getLLVMStyleWithColumns(11)));
4512   EXPECT_EQ("#define A \\\n"
4513             "  a;      \\\n"
4514             "          \\\n"
4515             "  b;",
4516             format("#define A \\\n"
4517                    "  a;      \\\n"
4518                    "          \\\n"
4519                    "  b;",
4520                    getLLVMStyleWithColumns(11)));
4521 }
4522 
4523 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
4524   verifyIncompleteFormat("#define A :");
4525   verifyFormat("#define SOMECASES  \\\n"
4526                "  case 1:          \\\n"
4527                "  case 2\n",
4528                getLLVMStyleWithColumns(20));
4529   verifyFormat("#define MACRO(a) \\\n"
4530                "  if (a)         \\\n"
4531                "    f();         \\\n"
4532                "  else           \\\n"
4533                "    g()",
4534                getLLVMStyleWithColumns(18));
4535   verifyFormat("#define A template <typename T>");
4536   verifyIncompleteFormat("#define STR(x) #x\n"
4537                          "f(STR(this_is_a_string_literal{));");
4538   verifyFormat("#pragma omp threadprivate( \\\n"
4539                "    y)), // expected-warning",
4540                getLLVMStyleWithColumns(28));
4541   verifyFormat("#d, = };");
4542   verifyFormat("#if \"a");
4543   verifyIncompleteFormat("({\n"
4544                          "#define b     \\\n"
4545                          "  }           \\\n"
4546                          "  a\n"
4547                          "a",
4548                          getLLVMStyleWithColumns(15));
4549   verifyFormat("#define A     \\\n"
4550                "  {           \\\n"
4551                "    {\n"
4552                "#define B     \\\n"
4553                "  }           \\\n"
4554                "  }",
4555                getLLVMStyleWithColumns(15));
4556   verifyNoCrash("#if a\na(\n#else\n#endif\n{a");
4557   verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}");
4558   verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};");
4559   verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() {      \n)}");
4560 }
4561 
4562 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
4563   verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
4564   EXPECT_EQ("class A : public QObject {\n"
4565             "  Q_OBJECT\n"
4566             "\n"
4567             "  A() {}\n"
4568             "};",
4569             format("class A  :  public QObject {\n"
4570                    "     Q_OBJECT\n"
4571                    "\n"
4572                    "  A() {\n}\n"
4573                    "}  ;"));
4574   EXPECT_EQ("MACRO\n"
4575             "/*static*/ int i;",
4576             format("MACRO\n"
4577                    " /*static*/ int   i;"));
4578   EXPECT_EQ("SOME_MACRO\n"
4579             "namespace {\n"
4580             "void f();\n"
4581             "} // namespace",
4582             format("SOME_MACRO\n"
4583                    "  namespace    {\n"
4584                    "void   f(  );\n"
4585                    "} // namespace"));
4586   // Only if the identifier contains at least 5 characters.
4587   EXPECT_EQ("HTTP f();", format("HTTP\nf();"));
4588   EXPECT_EQ("MACRO\nf();", format("MACRO\nf();"));
4589   // Only if everything is upper case.
4590   EXPECT_EQ("class A : public QObject {\n"
4591             "  Q_Object A() {}\n"
4592             "};",
4593             format("class A  :  public QObject {\n"
4594                    "     Q_Object\n"
4595                    "  A() {\n}\n"
4596                    "}  ;"));
4597 
4598   // Only if the next line can actually start an unwrapped line.
4599   EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;",
4600             format("SOME_WEIRD_LOG_MACRO\n"
4601                    "<< SomeThing;"));
4602 
4603   verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
4604                "(n, buffers))\n",
4605                getChromiumStyle(FormatStyle::LK_Cpp));
4606 
4607   // See PR41483
4608   EXPECT_EQ("/**/ FOO(a)\n"
4609             "FOO(b)",
4610             format("/**/ FOO(a)\n"
4611                    "FOO(b)"));
4612 }
4613 
4614 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
4615   EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
4616             "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
4617             "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
4618             "class X {};\n"
4619             "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
4620             "int *createScopDetectionPass() { return 0; }",
4621             format("  INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
4622                    "  INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
4623                    "  INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
4624                    "  class X {};\n"
4625                    "  INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
4626                    "  int *createScopDetectionPass() { return 0; }"));
4627   // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
4628   // braces, so that inner block is indented one level more.
4629   EXPECT_EQ("int q() {\n"
4630             "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
4631             "  IPC_MESSAGE_HANDLER(xxx, qqq)\n"
4632             "  IPC_END_MESSAGE_MAP()\n"
4633             "}",
4634             format("int q() {\n"
4635                    "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
4636                    "    IPC_MESSAGE_HANDLER(xxx, qqq)\n"
4637                    "  IPC_END_MESSAGE_MAP()\n"
4638                    "}"));
4639 
4640   // Same inside macros.
4641   EXPECT_EQ("#define LIST(L) \\\n"
4642             "  L(A)          \\\n"
4643             "  L(B)          \\\n"
4644             "  L(C)",
4645             format("#define LIST(L) \\\n"
4646                    "  L(A) \\\n"
4647                    "  L(B) \\\n"
4648                    "  L(C)",
4649                    getGoogleStyle()));
4650 
4651   // These must not be recognized as macros.
4652   EXPECT_EQ("int q() {\n"
4653             "  f(x);\n"
4654             "  f(x) {}\n"
4655             "  f(x)->g();\n"
4656             "  f(x)->*g();\n"
4657             "  f(x).g();\n"
4658             "  f(x) = x;\n"
4659             "  f(x) += x;\n"
4660             "  f(x) -= x;\n"
4661             "  f(x) *= x;\n"
4662             "  f(x) /= x;\n"
4663             "  f(x) %= x;\n"
4664             "  f(x) &= x;\n"
4665             "  f(x) |= x;\n"
4666             "  f(x) ^= x;\n"
4667             "  f(x) >>= x;\n"
4668             "  f(x) <<= x;\n"
4669             "  f(x)[y].z();\n"
4670             "  LOG(INFO) << x;\n"
4671             "  ifstream(x) >> x;\n"
4672             "}\n",
4673             format("int q() {\n"
4674                    "  f(x)\n;\n"
4675                    "  f(x)\n {}\n"
4676                    "  f(x)\n->g();\n"
4677                    "  f(x)\n->*g();\n"
4678                    "  f(x)\n.g();\n"
4679                    "  f(x)\n = x;\n"
4680                    "  f(x)\n += x;\n"
4681                    "  f(x)\n -= x;\n"
4682                    "  f(x)\n *= x;\n"
4683                    "  f(x)\n /= x;\n"
4684                    "  f(x)\n %= x;\n"
4685                    "  f(x)\n &= x;\n"
4686                    "  f(x)\n |= x;\n"
4687                    "  f(x)\n ^= x;\n"
4688                    "  f(x)\n >>= x;\n"
4689                    "  f(x)\n <<= x;\n"
4690                    "  f(x)\n[y].z();\n"
4691                    "  LOG(INFO)\n << x;\n"
4692                    "  ifstream(x)\n >> x;\n"
4693                    "}\n"));
4694   EXPECT_EQ("int q() {\n"
4695             "  F(x)\n"
4696             "  if (1) {\n"
4697             "  }\n"
4698             "  F(x)\n"
4699             "  while (1) {\n"
4700             "  }\n"
4701             "  F(x)\n"
4702             "  G(x);\n"
4703             "  F(x)\n"
4704             "  try {\n"
4705             "    Q();\n"
4706             "  } catch (...) {\n"
4707             "  }\n"
4708             "}\n",
4709             format("int q() {\n"
4710                    "F(x)\n"
4711                    "if (1) {}\n"
4712                    "F(x)\n"
4713                    "while (1) {}\n"
4714                    "F(x)\n"
4715                    "G(x);\n"
4716                    "F(x)\n"
4717                    "try { Q(); } catch (...) {}\n"
4718                    "}\n"));
4719   EXPECT_EQ("class A {\n"
4720             "  A() : t(0) {}\n"
4721             "  A(int i) noexcept() : {}\n"
4722             "  A(X x)\n" // FIXME: function-level try blocks are broken.
4723             "  try : t(0) {\n"
4724             "  } catch (...) {\n"
4725             "  }\n"
4726             "};",
4727             format("class A {\n"
4728                    "  A()\n : t(0) {}\n"
4729                    "  A(int i)\n noexcept() : {}\n"
4730                    "  A(X x)\n"
4731                    "  try : t(0) {} catch (...) {}\n"
4732                    "};"));
4733   FormatStyle Style = getLLVMStyle();
4734   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4735   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
4736   Style.BraceWrapping.AfterFunction = true;
4737   EXPECT_EQ("void f()\n"
4738             "try\n"
4739             "{\n"
4740             "}",
4741             format("void f() try {\n"
4742                    "}",
4743                    Style));
4744   EXPECT_EQ("class SomeClass {\n"
4745             "public:\n"
4746             "  SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
4747             "};",
4748             format("class SomeClass {\n"
4749                    "public:\n"
4750                    "  SomeClass()\n"
4751                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
4752                    "};"));
4753   EXPECT_EQ("class SomeClass {\n"
4754             "public:\n"
4755             "  SomeClass()\n"
4756             "      EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
4757             "};",
4758             format("class SomeClass {\n"
4759                    "public:\n"
4760                    "  SomeClass()\n"
4761                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
4762                    "};",
4763                    getLLVMStyleWithColumns(40)));
4764 
4765   verifyFormat("MACRO(>)");
4766 
4767   // Some macros contain an implicit semicolon.
4768   Style = getLLVMStyle();
4769   Style.StatementMacros.push_back("FOO");
4770   verifyFormat("FOO(a) int b = 0;");
4771   verifyFormat("FOO(a)\n"
4772                "int b = 0;",
4773                Style);
4774   verifyFormat("FOO(a);\n"
4775                "int b = 0;",
4776                Style);
4777   verifyFormat("FOO(argc, argv, \"4.0.2\")\n"
4778                "int b = 0;",
4779                Style);
4780   verifyFormat("FOO()\n"
4781                "int b = 0;",
4782                Style);
4783   verifyFormat("FOO\n"
4784                "int b = 0;",
4785                Style);
4786   verifyFormat("void f() {\n"
4787                "  FOO(a)\n"
4788                "  return a;\n"
4789                "}",
4790                Style);
4791   verifyFormat("FOO(a)\n"
4792                "FOO(b)",
4793                Style);
4794   verifyFormat("int a = 0;\n"
4795                "FOO(b)\n"
4796                "int c = 0;",
4797                Style);
4798   verifyFormat("int a = 0;\n"
4799                "int x = FOO(a)\n"
4800                "int b = 0;",
4801                Style);
4802   verifyFormat("void foo(int a) { FOO(a) }\n"
4803                "uint32_t bar() {}",
4804                Style);
4805 }
4806 
4807 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
4808   verifyFormat("#define A \\\n"
4809                "  f({     \\\n"
4810                "    g();  \\\n"
4811                "  });",
4812                getLLVMStyleWithColumns(11));
4813 }
4814 
4815 TEST_F(FormatTest, IndentPreprocessorDirectives) {
4816   FormatStyle Style = getLLVMStyleWithColumns(40);
4817   Style.IndentPPDirectives = FormatStyle::PPDIS_None;
4818   verifyFormat("#ifdef _WIN32\n"
4819                "#define A 0\n"
4820                "#ifdef VAR2\n"
4821                "#define B 1\n"
4822                "#include <someheader.h>\n"
4823                "#define MACRO                          \\\n"
4824                "  some_very_long_func_aaaaaaaaaa();\n"
4825                "#endif\n"
4826                "#else\n"
4827                "#define A 1\n"
4828                "#endif",
4829                Style);
4830   Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
4831   verifyFormat("#ifdef _WIN32\n"
4832                "#  define A 0\n"
4833                "#  ifdef VAR2\n"
4834                "#    define B 1\n"
4835                "#    include <someheader.h>\n"
4836                "#    define MACRO                      \\\n"
4837                "      some_very_long_func_aaaaaaaaaa();\n"
4838                "#  endif\n"
4839                "#else\n"
4840                "#  define A 1\n"
4841                "#endif",
4842                Style);
4843   verifyFormat("#if A\n"
4844                "#  define MACRO                        \\\n"
4845                "    void a(int x) {                    \\\n"
4846                "      b();                             \\\n"
4847                "      c();                             \\\n"
4848                "      d();                             \\\n"
4849                "      e();                             \\\n"
4850                "      f();                             \\\n"
4851                "    }\n"
4852                "#endif",
4853                Style);
4854   // Comments before include guard.
4855   verifyFormat("// file comment\n"
4856                "// file comment\n"
4857                "#ifndef HEADER_H\n"
4858                "#define HEADER_H\n"
4859                "code();\n"
4860                "#endif",
4861                Style);
4862   // Test with include guards.
4863   verifyFormat("#ifndef HEADER_H\n"
4864                "#define HEADER_H\n"
4865                "code();\n"
4866                "#endif",
4867                Style);
4868   // Include guards must have a #define with the same variable immediately
4869   // after #ifndef.
4870   verifyFormat("#ifndef NOT_GUARD\n"
4871                "#  define FOO\n"
4872                "code();\n"
4873                "#endif",
4874                Style);
4875 
4876   // Include guards must cover the entire file.
4877   verifyFormat("code();\n"
4878                "code();\n"
4879                "#ifndef NOT_GUARD\n"
4880                "#  define NOT_GUARD\n"
4881                "code();\n"
4882                "#endif",
4883                Style);
4884   verifyFormat("#ifndef NOT_GUARD\n"
4885                "#  define NOT_GUARD\n"
4886                "code();\n"
4887                "#endif\n"
4888                "code();",
4889                Style);
4890   // Test with trailing blank lines.
4891   verifyFormat("#ifndef HEADER_H\n"
4892                "#define HEADER_H\n"
4893                "code();\n"
4894                "#endif\n",
4895                Style);
4896   // Include guards don't have #else.
4897   verifyFormat("#ifndef NOT_GUARD\n"
4898                "#  define NOT_GUARD\n"
4899                "code();\n"
4900                "#else\n"
4901                "#endif",
4902                Style);
4903   verifyFormat("#ifndef NOT_GUARD\n"
4904                "#  define NOT_GUARD\n"
4905                "code();\n"
4906                "#elif FOO\n"
4907                "#endif",
4908                Style);
4909   // Non-identifier #define after potential include guard.
4910   verifyFormat("#ifndef FOO\n"
4911                "#  define 1\n"
4912                "#endif\n",
4913                Style);
4914   // #if closes past last non-preprocessor line.
4915   verifyFormat("#ifndef FOO\n"
4916                "#define FOO\n"
4917                "#if 1\n"
4918                "int i;\n"
4919                "#  define A 0\n"
4920                "#endif\n"
4921                "#endif\n",
4922                Style);
4923   // Don't crash if there is an #elif directive without a condition.
4924   verifyFormat("#if 1\n"
4925                "int x;\n"
4926                "#elif\n"
4927                "int y;\n"
4928                "#else\n"
4929                "int z;\n"
4930                "#endif",
4931                Style);
4932   // FIXME: This doesn't handle the case where there's code between the
4933   // #ifndef and #define but all other conditions hold. This is because when
4934   // the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the
4935   // previous code line yet, so we can't detect it.
4936   EXPECT_EQ("#ifndef NOT_GUARD\n"
4937             "code();\n"
4938             "#define NOT_GUARD\n"
4939             "code();\n"
4940             "#endif",
4941             format("#ifndef NOT_GUARD\n"
4942                    "code();\n"
4943                    "#  define NOT_GUARD\n"
4944                    "code();\n"
4945                    "#endif",
4946                    Style));
4947   // FIXME: This doesn't handle cases where legitimate preprocessor lines may
4948   // be outside an include guard. Examples are #pragma once and
4949   // #pragma GCC diagnostic, or anything else that does not change the meaning
4950   // of the file if it's included multiple times.
4951   EXPECT_EQ("#ifdef WIN32\n"
4952             "#  pragma once\n"
4953             "#endif\n"
4954             "#ifndef HEADER_H\n"
4955             "#  define HEADER_H\n"
4956             "code();\n"
4957             "#endif",
4958             format("#ifdef WIN32\n"
4959                    "#  pragma once\n"
4960                    "#endif\n"
4961                    "#ifndef HEADER_H\n"
4962                    "#define HEADER_H\n"
4963                    "code();\n"
4964                    "#endif",
4965                    Style));
4966   // FIXME: This does not detect when there is a single non-preprocessor line
4967   // in front of an include-guard-like structure where other conditions hold
4968   // because ScopedLineState hides the line.
4969   EXPECT_EQ("code();\n"
4970             "#ifndef HEADER_H\n"
4971             "#define HEADER_H\n"
4972             "code();\n"
4973             "#endif",
4974             format("code();\n"
4975                    "#ifndef HEADER_H\n"
4976                    "#  define HEADER_H\n"
4977                    "code();\n"
4978                    "#endif",
4979                    Style));
4980   // Keep comments aligned with #, otherwise indent comments normally. These
4981   // tests cannot use verifyFormat because messUp manipulates leading
4982   // whitespace.
4983   {
4984     const char *Expected = ""
4985                            "void f() {\n"
4986                            "#if 1\n"
4987                            "// Preprocessor aligned.\n"
4988                            "#  define A 0\n"
4989                            "  // Code. Separated by blank line.\n"
4990                            "\n"
4991                            "#  define B 0\n"
4992                            "  // Code. Not aligned with #\n"
4993                            "#  define C 0\n"
4994                            "#endif";
4995     const char *ToFormat = ""
4996                            "void f() {\n"
4997                            "#if 1\n"
4998                            "// Preprocessor aligned.\n"
4999                            "#  define A 0\n"
5000                            "// Code. Separated by blank line.\n"
5001                            "\n"
5002                            "#  define B 0\n"
5003                            "   // Code. Not aligned with #\n"
5004                            "#  define C 0\n"
5005                            "#endif";
5006     EXPECT_EQ(Expected, format(ToFormat, Style));
5007     EXPECT_EQ(Expected, format(Expected, Style));
5008   }
5009   // Keep block quotes aligned.
5010   {
5011     const char *Expected = ""
5012                            "void f() {\n"
5013                            "#if 1\n"
5014                            "/* Preprocessor aligned. */\n"
5015                            "#  define A 0\n"
5016                            "  /* Code. Separated by blank line. */\n"
5017                            "\n"
5018                            "#  define B 0\n"
5019                            "  /* Code. Not aligned with # */\n"
5020                            "#  define C 0\n"
5021                            "#endif";
5022     const char *ToFormat = ""
5023                            "void f() {\n"
5024                            "#if 1\n"
5025                            "/* Preprocessor aligned. */\n"
5026                            "#  define A 0\n"
5027                            "/* Code. Separated by blank line. */\n"
5028                            "\n"
5029                            "#  define B 0\n"
5030                            "   /* Code. Not aligned with # */\n"
5031                            "#  define C 0\n"
5032                            "#endif";
5033     EXPECT_EQ(Expected, format(ToFormat, Style));
5034     EXPECT_EQ(Expected, format(Expected, Style));
5035   }
5036   // Keep comments aligned with un-indented directives.
5037   {
5038     const char *Expected = ""
5039                            "void f() {\n"
5040                            "// Preprocessor aligned.\n"
5041                            "#define A 0\n"
5042                            "  // Code. Separated by blank line.\n"
5043                            "\n"
5044                            "#define B 0\n"
5045                            "  // Code. Not aligned with #\n"
5046                            "#define C 0\n";
5047     const char *ToFormat = ""
5048                            "void f() {\n"
5049                            "// Preprocessor aligned.\n"
5050                            "#define A 0\n"
5051                            "// Code. Separated by blank line.\n"
5052                            "\n"
5053                            "#define B 0\n"
5054                            "   // Code. Not aligned with #\n"
5055                            "#define C 0\n";
5056     EXPECT_EQ(Expected, format(ToFormat, Style));
5057     EXPECT_EQ(Expected, format(Expected, Style));
5058   }
5059   // Test AfterHash with tabs.
5060   {
5061     FormatStyle Tabbed = Style;
5062     Tabbed.UseTab = FormatStyle::UT_Always;
5063     Tabbed.IndentWidth = 8;
5064     Tabbed.TabWidth = 8;
5065     verifyFormat("#ifdef _WIN32\n"
5066                  "#\tdefine A 0\n"
5067                  "#\tifdef VAR2\n"
5068                  "#\t\tdefine B 1\n"
5069                  "#\t\tinclude <someheader.h>\n"
5070                  "#\t\tdefine MACRO          \\\n"
5071                  "\t\t\tsome_very_long_func_aaaaaaaaaa();\n"
5072                  "#\tendif\n"
5073                  "#else\n"
5074                  "#\tdefine A 1\n"
5075                  "#endif",
5076                  Tabbed);
5077   }
5078 
5079   // Regression test: Multiline-macro inside include guards.
5080   verifyFormat("#ifndef HEADER_H\n"
5081                "#define HEADER_H\n"
5082                "#define A()        \\\n"
5083                "  int i;           \\\n"
5084                "  int j;\n"
5085                "#endif // HEADER_H",
5086                getLLVMStyleWithColumns(20));
5087 
5088   Style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash;
5089   // Basic before hash indent tests
5090   verifyFormat("#ifdef _WIN32\n"
5091                "  #define A 0\n"
5092                "  #ifdef VAR2\n"
5093                "    #define B 1\n"
5094                "    #include <someheader.h>\n"
5095                "    #define MACRO                      \\\n"
5096                "      some_very_long_func_aaaaaaaaaa();\n"
5097                "  #endif\n"
5098                "#else\n"
5099                "  #define A 1\n"
5100                "#endif",
5101                Style);
5102   verifyFormat("#if A\n"
5103                "  #define MACRO                        \\\n"
5104                "    void a(int x) {                    \\\n"
5105                "      b();                             \\\n"
5106                "      c();                             \\\n"
5107                "      d();                             \\\n"
5108                "      e();                             \\\n"
5109                "      f();                             \\\n"
5110                "    }\n"
5111                "#endif",
5112                Style);
5113   // Keep comments aligned with indented directives. These
5114   // tests cannot use verifyFormat because messUp manipulates leading
5115   // whitespace.
5116   {
5117     const char *Expected = "void f() {\n"
5118                            "// Aligned to preprocessor.\n"
5119                            "#if 1\n"
5120                            "  // Aligned to code.\n"
5121                            "  int a;\n"
5122                            "  #if 1\n"
5123                            "    // Aligned to preprocessor.\n"
5124                            "    #define A 0\n"
5125                            "  // Aligned to code.\n"
5126                            "  int b;\n"
5127                            "  #endif\n"
5128                            "#endif\n"
5129                            "}";
5130     const char *ToFormat = "void f() {\n"
5131                            "// Aligned to preprocessor.\n"
5132                            "#if 1\n"
5133                            "// Aligned to code.\n"
5134                            "int a;\n"
5135                            "#if 1\n"
5136                            "// Aligned to preprocessor.\n"
5137                            "#define A 0\n"
5138                            "// Aligned to code.\n"
5139                            "int b;\n"
5140                            "#endif\n"
5141                            "#endif\n"
5142                            "}";
5143     EXPECT_EQ(Expected, format(ToFormat, Style));
5144     EXPECT_EQ(Expected, format(Expected, Style));
5145   }
5146   {
5147     const char *Expected = "void f() {\n"
5148                            "/* Aligned to preprocessor. */\n"
5149                            "#if 1\n"
5150                            "  /* Aligned to code. */\n"
5151                            "  int a;\n"
5152                            "  #if 1\n"
5153                            "    /* Aligned to preprocessor. */\n"
5154                            "    #define A 0\n"
5155                            "  /* Aligned to code. */\n"
5156                            "  int b;\n"
5157                            "  #endif\n"
5158                            "#endif\n"
5159                            "}";
5160     const char *ToFormat = "void f() {\n"
5161                            "/* Aligned to preprocessor. */\n"
5162                            "#if 1\n"
5163                            "/* Aligned to code. */\n"
5164                            "int a;\n"
5165                            "#if 1\n"
5166                            "/* Aligned to preprocessor. */\n"
5167                            "#define A 0\n"
5168                            "/* Aligned to code. */\n"
5169                            "int b;\n"
5170                            "#endif\n"
5171                            "#endif\n"
5172                            "}";
5173     EXPECT_EQ(Expected, format(ToFormat, Style));
5174     EXPECT_EQ(Expected, format(Expected, Style));
5175   }
5176 
5177   // Test single comment before preprocessor
5178   verifyFormat("// Comment\n"
5179                "\n"
5180                "#if 1\n"
5181                "#endif",
5182                Style);
5183 }
5184 
5185 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
5186   verifyFormat("{\n  { a #c; }\n}");
5187 }
5188 
5189 TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
5190   EXPECT_EQ("#define A \\\n  {       \\\n    {\nint i;",
5191             format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
5192   EXPECT_EQ("#define A \\\n  }       \\\n  }\nint i;",
5193             format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
5194 }
5195 
5196 TEST_F(FormatTest, EscapedNewlines) {
5197   FormatStyle Narrow = getLLVMStyleWithColumns(11);
5198   EXPECT_EQ("#define A \\\n  int i;  \\\n  int j;",
5199             format("#define A \\\nint i;\\\n  int j;", Narrow));
5200   EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;"));
5201   EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
5202   EXPECT_EQ("/* \\  \\  \\\n */", format("\\\n/* \\  \\  \\\n */"));
5203   EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>"));
5204 
5205   FormatStyle AlignLeft = getLLVMStyle();
5206   AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
5207   EXPECT_EQ("#define MACRO(x) \\\n"
5208             "private:         \\\n"
5209             "  int x(int a);\n",
5210             format("#define MACRO(x) \\\n"
5211                    "private:         \\\n"
5212                    "  int x(int a);\n",
5213                    AlignLeft));
5214 
5215   // CRLF line endings
5216   EXPECT_EQ("#define A \\\r\n  int i;  \\\r\n  int j;",
5217             format("#define A \\\r\nint i;\\\r\n  int j;", Narrow));
5218   EXPECT_EQ("#define A\r\n\r\nint i;", format("#define A \\\r\n\r\n int i;"));
5219   EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
5220   EXPECT_EQ("/* \\  \\  \\\r\n */", format("\\\r\n/* \\  \\  \\\r\n */"));
5221   EXPECT_EQ("<a\r\n\\\\\r\n>", format("<a\r\n\\\\\r\n>"));
5222   EXPECT_EQ("#define MACRO(x) \\\r\n"
5223             "private:         \\\r\n"
5224             "  int x(int a);\r\n",
5225             format("#define MACRO(x) \\\r\n"
5226                    "private:         \\\r\n"
5227                    "  int x(int a);\r\n",
5228                    AlignLeft));
5229 
5230   FormatStyle DontAlign = getLLVMStyle();
5231   DontAlign.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
5232   DontAlign.MaxEmptyLinesToKeep = 3;
5233   // FIXME: can't use verifyFormat here because the newline before
5234   // "public:" is not inserted the first time it's reformatted
5235   EXPECT_EQ("#define A \\\n"
5236             "  class Foo { \\\n"
5237             "    void bar(); \\\n"
5238             "\\\n"
5239             "\\\n"
5240             "\\\n"
5241             "  public: \\\n"
5242             "    void baz(); \\\n"
5243             "  };",
5244             format("#define A \\\n"
5245                    "  class Foo { \\\n"
5246                    "    void bar(); \\\n"
5247                    "\\\n"
5248                    "\\\n"
5249                    "\\\n"
5250                    "  public: \\\n"
5251                    "    void baz(); \\\n"
5252                    "  };",
5253                    DontAlign));
5254 }
5255 
5256 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
5257   verifyFormat("#define A \\\n"
5258                "  int v(  \\\n"
5259                "      a); \\\n"
5260                "  int i;",
5261                getLLVMStyleWithColumns(11));
5262 }
5263 
5264 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
5265   EXPECT_EQ(
5266       "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
5267       "                      \\\n"
5268       "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
5269       "\n"
5270       "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
5271       "    aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
5272       format("  #define   ALooooooooooooooooooooooooooooooooooooooongMacro("
5273              "\\\n"
5274              "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
5275              "  \n"
5276              "   AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
5277              "  aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
5278 }
5279 
5280 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
5281   EXPECT_EQ("int\n"
5282             "#define A\n"
5283             "    a;",
5284             format("int\n#define A\na;"));
5285   verifyFormat("functionCallTo(\n"
5286                "    someOtherFunction(\n"
5287                "        withSomeParameters, whichInSequence,\n"
5288                "        areLongerThanALine(andAnotherCall,\n"
5289                "#define A B\n"
5290                "                           withMoreParamters,\n"
5291                "                           whichStronglyInfluenceTheLayout),\n"
5292                "        andMoreParameters),\n"
5293                "    trailing);",
5294                getLLVMStyleWithColumns(69));
5295   verifyFormat("Foo::Foo()\n"
5296                "#ifdef BAR\n"
5297                "    : baz(0)\n"
5298                "#endif\n"
5299                "{\n"
5300                "}");
5301   verifyFormat("void f() {\n"
5302                "  if (true)\n"
5303                "#ifdef A\n"
5304                "    f(42);\n"
5305                "  x();\n"
5306                "#else\n"
5307                "    g();\n"
5308                "  x();\n"
5309                "#endif\n"
5310                "}");
5311   verifyFormat("void f(param1, param2,\n"
5312                "       param3,\n"
5313                "#ifdef A\n"
5314                "       param4(param5,\n"
5315                "#ifdef A1\n"
5316                "              param6,\n"
5317                "#ifdef A2\n"
5318                "              param7),\n"
5319                "#else\n"
5320                "              param8),\n"
5321                "       param9,\n"
5322                "#endif\n"
5323                "       param10,\n"
5324                "#endif\n"
5325                "       param11)\n"
5326                "#else\n"
5327                "       param12)\n"
5328                "#endif\n"
5329                "{\n"
5330                "  x();\n"
5331                "}",
5332                getLLVMStyleWithColumns(28));
5333   verifyFormat("#if 1\n"
5334                "int i;");
5335   verifyFormat("#if 1\n"
5336                "#endif\n"
5337                "#if 1\n"
5338                "#else\n"
5339                "#endif\n");
5340   verifyFormat("DEBUG({\n"
5341                "  return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5342                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
5343                "});\n"
5344                "#if a\n"
5345                "#else\n"
5346                "#endif");
5347 
5348   verifyIncompleteFormat("void f(\n"
5349                          "#if A\n"
5350                          ");\n"
5351                          "#else\n"
5352                          "#endif");
5353 }
5354 
5355 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
5356   verifyFormat("#endif\n"
5357                "#if B");
5358 }
5359 
5360 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
5361   FormatStyle SingleLine = getLLVMStyle();
5362   SingleLine.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse;
5363   verifyFormat("#if 0\n"
5364                "#elif 1\n"
5365                "#endif\n"
5366                "void foo() {\n"
5367                "  if (test) foo2();\n"
5368                "}",
5369                SingleLine);
5370 }
5371 
5372 TEST_F(FormatTest, LayoutBlockInsideParens) {
5373   verifyFormat("functionCall({ int i; });");
5374   verifyFormat("functionCall({\n"
5375                "  int i;\n"
5376                "  int j;\n"
5377                "});");
5378   verifyFormat("functionCall(\n"
5379                "    {\n"
5380                "      int i;\n"
5381                "      int j;\n"
5382                "    },\n"
5383                "    aaaa, bbbb, cccc);");
5384   verifyFormat("functionA(functionB({\n"
5385                "            int i;\n"
5386                "            int j;\n"
5387                "          }),\n"
5388                "          aaaa, bbbb, cccc);");
5389   verifyFormat("functionCall(\n"
5390                "    {\n"
5391                "      int i;\n"
5392                "      int j;\n"
5393                "    },\n"
5394                "    aaaa, bbbb, // comment\n"
5395                "    cccc);");
5396   verifyFormat("functionA(functionB({\n"
5397                "            int i;\n"
5398                "            int j;\n"
5399                "          }),\n"
5400                "          aaaa, bbbb, // comment\n"
5401                "          cccc);");
5402   verifyFormat("functionCall(aaaa, bbbb, { int i; });");
5403   verifyFormat("functionCall(aaaa, bbbb, {\n"
5404                "  int i;\n"
5405                "  int j;\n"
5406                "});");
5407   verifyFormat(
5408       "Aaa(\n" // FIXME: There shouldn't be a linebreak here.
5409       "    {\n"
5410       "      int i; // break\n"
5411       "    },\n"
5412       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
5413       "                                     ccccccccccccccccc));");
5414   verifyFormat("DEBUG({\n"
5415                "  if (a)\n"
5416                "    f();\n"
5417                "});");
5418 }
5419 
5420 TEST_F(FormatTest, LayoutBlockInsideStatement) {
5421   EXPECT_EQ("SOME_MACRO { int i; }\n"
5422             "int i;",
5423             format("  SOME_MACRO  {int i;}  int i;"));
5424 }
5425 
5426 TEST_F(FormatTest, LayoutNestedBlocks) {
5427   verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
5428                "  struct s {\n"
5429                "    int i;\n"
5430                "  };\n"
5431                "  s kBitsToOs[] = {{10}};\n"
5432                "  for (int i = 0; i < 10; ++i)\n"
5433                "    return;\n"
5434                "}");
5435   verifyFormat("call(parameter, {\n"
5436                "  something();\n"
5437                "  // Comment using all columns.\n"
5438                "  somethingelse();\n"
5439                "});",
5440                getLLVMStyleWithColumns(40));
5441   verifyFormat("DEBUG( //\n"
5442                "    { f(); }, a);");
5443   verifyFormat("DEBUG( //\n"
5444                "    {\n"
5445                "      f(); //\n"
5446                "    },\n"
5447                "    a);");
5448 
5449   EXPECT_EQ("call(parameter, {\n"
5450             "  something();\n"
5451             "  // Comment too\n"
5452             "  // looooooooooong.\n"
5453             "  somethingElse();\n"
5454             "});",
5455             format("call(parameter, {\n"
5456                    "  something();\n"
5457                    "  // Comment too looooooooooong.\n"
5458                    "  somethingElse();\n"
5459                    "});",
5460                    getLLVMStyleWithColumns(29)));
5461   EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int   i; });"));
5462   EXPECT_EQ("DEBUG({ // comment\n"
5463             "  int i;\n"
5464             "});",
5465             format("DEBUG({ // comment\n"
5466                    "int  i;\n"
5467                    "});"));
5468   EXPECT_EQ("DEBUG({\n"
5469             "  int i;\n"
5470             "\n"
5471             "  // comment\n"
5472             "  int j;\n"
5473             "});",
5474             format("DEBUG({\n"
5475                    "  int  i;\n"
5476                    "\n"
5477                    "  // comment\n"
5478                    "  int  j;\n"
5479                    "});"));
5480 
5481   verifyFormat("DEBUG({\n"
5482                "  if (a)\n"
5483                "    return;\n"
5484                "});");
5485   verifyGoogleFormat("DEBUG({\n"
5486                      "  if (a) return;\n"
5487                      "});");
5488   FormatStyle Style = getGoogleStyle();
5489   Style.ColumnLimit = 45;
5490   verifyFormat("Debug(\n"
5491                "    aaaaa,\n"
5492                "    {\n"
5493                "      if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
5494                "    },\n"
5495                "    a);",
5496                Style);
5497 
5498   verifyFormat("SomeFunction({MACRO({ return output; }), b});");
5499 
5500   verifyNoCrash("^{v^{a}}");
5501 }
5502 
5503 TEST_F(FormatTest, FormatNestedBlocksInMacros) {
5504   EXPECT_EQ("#define MACRO()                     \\\n"
5505             "  Debug(aaa, /* force line break */ \\\n"
5506             "        {                           \\\n"
5507             "          int i;                    \\\n"
5508             "          int j;                    \\\n"
5509             "        })",
5510             format("#define   MACRO()   Debug(aaa,  /* force line break */ \\\n"
5511                    "          {  int   i;  int  j;   })",
5512                    getGoogleStyle()));
5513 
5514   EXPECT_EQ("#define A                                       \\\n"
5515             "  [] {                                          \\\n"
5516             "    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(        \\\n"
5517             "        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
5518             "  }",
5519             format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
5520                    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
5521                    getGoogleStyle()));
5522 }
5523 
5524 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
5525   EXPECT_EQ("{}", format("{}"));
5526   verifyFormat("enum E {};");
5527   verifyFormat("enum E {}");
5528   FormatStyle Style = getLLVMStyle();
5529   Style.SpaceInEmptyBlock = true;
5530   EXPECT_EQ("void f() { }", format("void f() {}", Style));
5531   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty;
5532   EXPECT_EQ("while (true) { }", format("while (true) {}", Style));
5533   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
5534   Style.BraceWrapping.BeforeElse = false;
5535   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
5536   verifyFormat("if (a)\n"
5537                "{\n"
5538                "} else if (b)\n"
5539                "{\n"
5540                "} else\n"
5541                "{ }",
5542                Style);
5543   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never;
5544   verifyFormat("if (a) {\n"
5545                "} else if (b) {\n"
5546                "} else {\n"
5547                "}",
5548                Style);
5549   Style.BraceWrapping.BeforeElse = true;
5550   verifyFormat("if (a) { }\n"
5551                "else if (b) { }\n"
5552                "else { }",
5553                Style);
5554 }
5555 
5556 TEST_F(FormatTest, FormatBeginBlockEndMacros) {
5557   FormatStyle Style = getLLVMStyle();
5558   Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$";
5559   Style.MacroBlockEnd = "^[A-Z_]+_END$";
5560   verifyFormat("FOO_BEGIN\n"
5561                "  FOO_ENTRY\n"
5562                "FOO_END",
5563                Style);
5564   verifyFormat("FOO_BEGIN\n"
5565                "  NESTED_FOO_BEGIN\n"
5566                "    NESTED_FOO_ENTRY\n"
5567                "  NESTED_FOO_END\n"
5568                "FOO_END",
5569                Style);
5570   verifyFormat("FOO_BEGIN(Foo, Bar)\n"
5571                "  int x;\n"
5572                "  x = 1;\n"
5573                "FOO_END(Baz)",
5574                Style);
5575 }
5576 
5577 //===----------------------------------------------------------------------===//
5578 // Line break tests.
5579 //===----------------------------------------------------------------------===//
5580 
5581 TEST_F(FormatTest, PreventConfusingIndents) {
5582   verifyFormat(
5583       "void f() {\n"
5584       "  SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
5585       "                         parameter, parameter, parameter)),\n"
5586       "                     SecondLongCall(parameter));\n"
5587       "}");
5588   verifyFormat(
5589       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5590       "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
5591       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
5592       "    aaaaaaaaaaaaaaaaaaaaaaaa);");
5593   verifyFormat(
5594       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5595       "    [aaaaaaaaaaaaaaaaaaaaaaaa\n"
5596       "         [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
5597       "         [aaaaaaaaaaaaaaaaaaaaaaaa]];");
5598   verifyFormat(
5599       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
5600       "    aaaaaaaaaaaaaaaaaaaaaaaa<\n"
5601       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
5602       "    aaaaaaaaaaaaaaaaaaaaaaaa>;");
5603   verifyFormat("int a = bbbb && ccc &&\n"
5604                "        fffff(\n"
5605                "#define A Just forcing a new line\n"
5606                "            ddd);");
5607 }
5608 
5609 TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
5610   verifyFormat(
5611       "bool aaaaaaa =\n"
5612       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
5613       "    bbbbbbbb();");
5614   verifyFormat(
5615       "bool aaaaaaa =\n"
5616       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n"
5617       "    bbbbbbbb();");
5618 
5619   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
5620                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
5621                "    ccccccccc == ddddddddddd;");
5622   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
5623                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n"
5624                "    ccccccccc == ddddddddddd;");
5625   verifyFormat(
5626       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
5627       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n"
5628       "    ccccccccc == ddddddddddd;");
5629 
5630   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
5631                "                 aaaaaa) &&\n"
5632                "         bbbbbb && cccccc;");
5633   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
5634                "                 aaaaaa) >>\n"
5635                "         bbbbbb;");
5636   verifyFormat("aa = Whitespaces.addUntouchableComment(\n"
5637                "    SourceMgr.getSpellingColumnNumber(\n"
5638                "        TheLine.Last->FormatTok.Tok.getLocation()) -\n"
5639                "    1);");
5640 
5641   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
5642                "     bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
5643                "    cccccc) {\n}");
5644   verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
5645                "               bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n"
5646                "              cccccc) {\n}");
5647   verifyFormat("if CONSTEXPR ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
5648                "               bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n"
5649                "              cccccc) {\n}");
5650   verifyFormat("b = a &&\n"
5651                "    // Comment\n"
5652                "    b.c && d;");
5653 
5654   // If the LHS of a comparison is not a binary expression itself, the
5655   // additional linebreak confuses many people.
5656   verifyFormat(
5657       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5658       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
5659       "}");
5660   verifyFormat(
5661       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5662       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
5663       "}");
5664   verifyFormat(
5665       "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
5666       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
5667       "}");
5668   verifyFormat(
5669       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5670       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) <=> 5) {\n"
5671       "}");
5672   // Even explicit parentheses stress the precedence enough to make the
5673   // additional break unnecessary.
5674   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5675                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
5676                "}");
5677   // This cases is borderline, but with the indentation it is still readable.
5678   verifyFormat(
5679       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5680       "        aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5681       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
5682       "}",
5683       getLLVMStyleWithColumns(75));
5684 
5685   // If the LHS is a binary expression, we should still use the additional break
5686   // as otherwise the formatting hides the operator precedence.
5687   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5688                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
5689                "    5) {\n"
5690                "}");
5691   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5692                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa <=>\n"
5693                "    5) {\n"
5694                "}");
5695 
5696   FormatStyle OnePerLine = getLLVMStyle();
5697   OnePerLine.BinPackParameters = false;
5698   verifyFormat(
5699       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
5700       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
5701       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
5702       OnePerLine);
5703 
5704   verifyFormat("int i = someFunction(aaaaaaa, 0)\n"
5705                "                .aaa(aaaaaaaaaaaaa) *\n"
5706                "            aaaaaaa +\n"
5707                "        aaaaaaa;",
5708                getLLVMStyleWithColumns(40));
5709 }
5710 
5711 TEST_F(FormatTest, ExpressionIndentation) {
5712   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5713                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5714                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
5715                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
5716                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
5717                "                     bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
5718                "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
5719                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
5720                "                 ccccccccccccccccccccccccccccccccccccccccc;");
5721   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
5722                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5723                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
5724                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
5725   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5726                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
5727                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
5728                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
5729   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
5730                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
5731                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5732                "        bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
5733   verifyFormat("if () {\n"
5734                "} else if (aaaaa && bbbbb > // break\n"
5735                "                        ccccc) {\n"
5736                "}");
5737   verifyFormat("if () {\n"
5738                "} else if constexpr (aaaaa && bbbbb > // break\n"
5739                "                                  ccccc) {\n"
5740                "}");
5741   verifyFormat("if () {\n"
5742                "} else if CONSTEXPR (aaaaa && bbbbb > // break\n"
5743                "                                  ccccc) {\n"
5744                "}");
5745   verifyFormat("if () {\n"
5746                "} else if (aaaaa &&\n"
5747                "           bbbbb > // break\n"
5748                "               ccccc &&\n"
5749                "           ddddd) {\n"
5750                "}");
5751 
5752   // Presence of a trailing comment used to change indentation of b.
5753   verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
5754                "       b;\n"
5755                "return aaaaaaaaaaaaaaaaaaa +\n"
5756                "       b; //",
5757                getLLVMStyleWithColumns(30));
5758 }
5759 
5760 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
5761   // Not sure what the best system is here. Like this, the LHS can be found
5762   // immediately above an operator (everything with the same or a higher
5763   // indent). The RHS is aligned right of the operator and so compasses
5764   // everything until something with the same indent as the operator is found.
5765   // FIXME: Is this a good system?
5766   FormatStyle Style = getLLVMStyle();
5767   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
5768   verifyFormat(
5769       "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5770       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5771       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5772       "                 == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5773       "                            * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5774       "                        + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5775       "             && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5776       "                        * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5777       "                    > ccccccccccccccccccccccccccccccccccccccccc;",
5778       Style);
5779   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5780                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5781                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5782                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
5783                Style);
5784   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5785                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5786                "              * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5787                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
5788                Style);
5789   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5790                "    == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5791                "               * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5792                "           + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
5793                Style);
5794   verifyFormat("if () {\n"
5795                "} else if (aaaaa\n"
5796                "           && bbbbb // break\n"
5797                "                  > ccccc) {\n"
5798                "}",
5799                Style);
5800   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5801                "       && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
5802                Style);
5803   verifyFormat("return (a)\n"
5804                "       // comment\n"
5805                "       + b;",
5806                Style);
5807   verifyFormat(
5808       "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5809       "                 * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5810       "             + cc;",
5811       Style);
5812 
5813   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5814                "    = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
5815                Style);
5816 
5817   // Forced by comments.
5818   verifyFormat(
5819       "unsigned ContentSize =\n"
5820       "    sizeof(int16_t)   // DWARF ARange version number\n"
5821       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
5822       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
5823       "    + sizeof(int8_t); // Segment Size (in bytes)");
5824 
5825   verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
5826                "       == boost::fusion::at_c<1>(iiii).second;",
5827                Style);
5828 
5829   Style.ColumnLimit = 60;
5830   verifyFormat("zzzzzzzzzz\n"
5831                "    = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5832                "      >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
5833                Style);
5834 
5835   Style.ColumnLimit = 80;
5836   Style.IndentWidth = 4;
5837   Style.TabWidth = 4;
5838   Style.UseTab = FormatStyle::UT_Always;
5839   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
5840   Style.AlignOperands = FormatStyle::OAS_DontAlign;
5841   EXPECT_EQ("return someVeryVeryLongConditionThatBarelyFitsOnALine\n"
5842             "\t&& (someOtherLongishConditionPart1\n"
5843             "\t\t|| someOtherEvenLongerNestedConditionPart2);",
5844             format("return someVeryVeryLongConditionThatBarelyFitsOnALine && "
5845                    "(someOtherLongishConditionPart1 || "
5846                    "someOtherEvenLongerNestedConditionPart2);",
5847                    Style));
5848 }
5849 
5850 TEST_F(FormatTest, ExpressionIndentationStrictAlign) {
5851   FormatStyle Style = getLLVMStyle();
5852   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
5853   Style.AlignOperands = FormatStyle::OAS_AlignAfterOperator;
5854 
5855   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5856                "                   + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5857                "                   + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5858                "              == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5859                "                         * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5860                "                     + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5861                "          && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5862                "                     * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5863                "                 > ccccccccccccccccccccccccccccccccccccccccc;",
5864                Style);
5865   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5866                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5867                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5868                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
5869                Style);
5870   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5871                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5872                "              * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5873                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
5874                Style);
5875   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5876                "    == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5877                "               * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5878                "           + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
5879                Style);
5880   verifyFormat("if () {\n"
5881                "} else if (aaaaa\n"
5882                "           && bbbbb // break\n"
5883                "                  > ccccc) {\n"
5884                "}",
5885                Style);
5886   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5887                "    && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
5888                Style);
5889   verifyFormat("return (a)\n"
5890                "     // comment\n"
5891                "     + b;",
5892                Style);
5893   verifyFormat(
5894       "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5895       "               * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5896       "           + cc;",
5897       Style);
5898   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
5899                "     : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
5900                "                        : 3333333333333333;",
5901                Style);
5902   verifyFormat(
5903       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa    ? bbbbbbbbbbbbbbbbbb\n"
5904       "                           : ccccccccccccccc ? dddddddddddddddddd\n"
5905       "                                             : eeeeeeeeeeeeeeeeee)\n"
5906       "     : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
5907       "                        : 3333333333333333;",
5908       Style);
5909   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5910                "    = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
5911                Style);
5912 
5913   verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
5914                "    == boost::fusion::at_c<1>(iiii).second;",
5915                Style);
5916 
5917   Style.ColumnLimit = 60;
5918   verifyFormat("zzzzzzzzzzzzz\n"
5919                "    = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5920                "   >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
5921                Style);
5922 
5923   // Forced by comments.
5924   Style.ColumnLimit = 80;
5925   verifyFormat(
5926       "unsigned ContentSize\n"
5927       "    = sizeof(int16_t) // DWARF ARange version number\n"
5928       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
5929       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
5930       "    + sizeof(int8_t); // Segment Size (in bytes)",
5931       Style);
5932 
5933   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
5934   verifyFormat(
5935       "unsigned ContentSize =\n"
5936       "    sizeof(int16_t)   // DWARF ARange version number\n"
5937       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
5938       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
5939       "    + sizeof(int8_t); // Segment Size (in bytes)",
5940       Style);
5941 
5942   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
5943   verifyFormat(
5944       "unsigned ContentSize =\n"
5945       "    sizeof(int16_t)   // DWARF ARange version number\n"
5946       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
5947       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
5948       "    + sizeof(int8_t); // Segment Size (in bytes)",
5949       Style);
5950 }
5951 
5952 TEST_F(FormatTest, EnforcedOperatorWraps) {
5953   // Here we'd like to wrap after the || operators, but a comment is forcing an
5954   // earlier wrap.
5955   verifyFormat("bool x = aaaaa //\n"
5956                "         || bbbbb\n"
5957                "         //\n"
5958                "         || cccc;");
5959 }
5960 
5961 TEST_F(FormatTest, NoOperandAlignment) {
5962   FormatStyle Style = getLLVMStyle();
5963   Style.AlignOperands = FormatStyle::OAS_DontAlign;
5964   verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n"
5965                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5966                "                   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
5967                Style);
5968   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
5969   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5970                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5971                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5972                "        == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5973                "                * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5974                "            + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5975                "    && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5976                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5977                "        > ccccccccccccccccccccccccccccccccccccccccc;",
5978                Style);
5979 
5980   verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5981                "        * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5982                "    + cc;",
5983                Style);
5984   verifyFormat("int a = aa\n"
5985                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5986                "        * cccccccccccccccccccccccccccccccccccc;\n",
5987                Style);
5988 
5989   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
5990   verifyFormat("return (a > b\n"
5991                "    // comment1\n"
5992                "    // comment2\n"
5993                "    || c);",
5994                Style);
5995 }
5996 
5997 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) {
5998   FormatStyle Style = getLLVMStyle();
5999   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
6000   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
6001                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6002                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
6003                Style);
6004 }
6005 
6006 TEST_F(FormatTest, AllowBinPackingInsideArguments) {
6007   FormatStyle Style = getLLVMStyleWithColumns(40);
6008   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
6009   Style.BinPackArguments = false;
6010   verifyFormat("void test() {\n"
6011                "  someFunction(\n"
6012                "      this + argument + is + quite\n"
6013                "      + long + so + it + gets + wrapped\n"
6014                "      + but + remains + bin - packed);\n"
6015                "}",
6016                Style);
6017   verifyFormat("void test() {\n"
6018                "  someFunction(arg1,\n"
6019                "               this + argument + is\n"
6020                "                   + quite + long + so\n"
6021                "                   + it + gets + wrapped\n"
6022                "                   + but + remains + bin\n"
6023                "                   - packed,\n"
6024                "               arg3);\n"
6025                "}",
6026                Style);
6027   verifyFormat("void test() {\n"
6028                "  someFunction(\n"
6029                "      arg1,\n"
6030                "      this + argument + has\n"
6031                "          + anotherFunc(nested,\n"
6032                "                        calls + whose\n"
6033                "                            + arguments\n"
6034                "                            + are + also\n"
6035                "                            + wrapped,\n"
6036                "                        in + addition)\n"
6037                "          + to + being + bin - packed,\n"
6038                "      arg3);\n"
6039                "}",
6040                Style);
6041 
6042   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
6043   verifyFormat("void test() {\n"
6044                "  someFunction(\n"
6045                "      arg1,\n"
6046                "      this + argument + has +\n"
6047                "          anotherFunc(nested,\n"
6048                "                      calls + whose +\n"
6049                "                          arguments +\n"
6050                "                          are + also +\n"
6051                "                          wrapped,\n"
6052                "                      in + addition) +\n"
6053                "          to + being + bin - packed,\n"
6054                "      arg3);\n"
6055                "}",
6056                Style);
6057 }
6058 
6059 TEST_F(FormatTest, ConstructorInitializers) {
6060   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
6061   verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
6062                getLLVMStyleWithColumns(45));
6063   verifyFormat("Constructor()\n"
6064                "    : Inttializer(FitsOnTheLine) {}",
6065                getLLVMStyleWithColumns(44));
6066   verifyFormat("Constructor()\n"
6067                "    : Inttializer(FitsOnTheLine) {}",
6068                getLLVMStyleWithColumns(43));
6069 
6070   verifyFormat("template <typename T>\n"
6071                "Constructor() : Initializer(FitsOnTheLine) {}",
6072                getLLVMStyleWithColumns(45));
6073 
6074   verifyFormat(
6075       "SomeClass::Constructor()\n"
6076       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
6077 
6078   verifyFormat(
6079       "SomeClass::Constructor()\n"
6080       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6081       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
6082   verifyFormat(
6083       "SomeClass::Constructor()\n"
6084       "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6085       "      aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
6086   verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6087                "            aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
6088                "    : aaaaaaaaaa(aaaaaa) {}");
6089 
6090   verifyFormat("Constructor()\n"
6091                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6092                "      aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6093                "                               aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6094                "      aaaaaaaaaaaaaaaaaaaaaaa() {}");
6095 
6096   verifyFormat("Constructor()\n"
6097                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6098                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
6099 
6100   verifyFormat("Constructor(int Parameter = 0)\n"
6101                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
6102                "      aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
6103   verifyFormat("Constructor()\n"
6104                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
6105                "}",
6106                getLLVMStyleWithColumns(60));
6107   verifyFormat("Constructor()\n"
6108                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6109                "          aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}");
6110 
6111   // Here a line could be saved by splitting the second initializer onto two
6112   // lines, but that is not desirable.
6113   verifyFormat("Constructor()\n"
6114                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
6115                "      aaaaaaaaaaa(aaaaaaaaaaa),\n"
6116                "      aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
6117 
6118   FormatStyle OnePerLine = getLLVMStyle();
6119   OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_Never;
6120   verifyFormat("MyClass::MyClass()\n"
6121                "    : a(a),\n"
6122                "      b(b),\n"
6123                "      c(c) {}",
6124                OnePerLine);
6125   verifyFormat("MyClass::MyClass()\n"
6126                "    : a(a), // comment\n"
6127                "      b(b),\n"
6128                "      c(c) {}",
6129                OnePerLine);
6130   verifyFormat("MyClass::MyClass(int a)\n"
6131                "    : b(a),      // comment\n"
6132                "      c(a + 1) { // lined up\n"
6133                "}",
6134                OnePerLine);
6135   verifyFormat("Constructor()\n"
6136                "    : a(b, b, b) {}",
6137                OnePerLine);
6138   OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6139   OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false;
6140   verifyFormat("SomeClass::Constructor()\n"
6141                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6142                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6143                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
6144                OnePerLine);
6145   verifyFormat("SomeClass::Constructor()\n"
6146                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
6147                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6148                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
6149                OnePerLine);
6150   verifyFormat("MyClass::MyClass(int var)\n"
6151                "    : some_var_(var),            // 4 space indent\n"
6152                "      some_other_var_(var + 1) { // lined up\n"
6153                "}",
6154                OnePerLine);
6155   verifyFormat("Constructor()\n"
6156                "    : aaaaa(aaaaaa),\n"
6157                "      aaaaa(aaaaaa),\n"
6158                "      aaaaa(aaaaaa),\n"
6159                "      aaaaa(aaaaaa),\n"
6160                "      aaaaa(aaaaaa) {}",
6161                OnePerLine);
6162   verifyFormat("Constructor()\n"
6163                "    : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
6164                "            aaaaaaaaaaaaaaaaaaaaaa) {}",
6165                OnePerLine);
6166   OnePerLine.BinPackParameters = false;
6167   verifyFormat(
6168       "Constructor()\n"
6169       "    : aaaaaaaaaaaaaaaaaaaaaaaa(\n"
6170       "          aaaaaaaaaaa().aaa(),\n"
6171       "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
6172       OnePerLine);
6173   OnePerLine.ColumnLimit = 60;
6174   verifyFormat("Constructor()\n"
6175                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
6176                "      bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
6177                OnePerLine);
6178 
6179   EXPECT_EQ("Constructor()\n"
6180             "    : // Comment forcing unwanted break.\n"
6181             "      aaaa(aaaa) {}",
6182             format("Constructor() :\n"
6183                    "    // Comment forcing unwanted break.\n"
6184                    "    aaaa(aaaa) {}"));
6185 }
6186 
6187 TEST_F(FormatTest, AllowAllConstructorInitializersOnNextLine) {
6188   FormatStyle Style = getLLVMStyleWithColumns(60);
6189   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
6190   Style.BinPackParameters = false;
6191 
6192   for (int i = 0; i < 4; ++i) {
6193     // Test all combinations of parameters that should not have an effect.
6194     Style.AllowAllParametersOfDeclarationOnNextLine = i & 1;
6195     Style.AllowAllArgumentsOnNextLine = i & 2;
6196 
6197     Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6198     Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
6199     verifyFormat("Constructor()\n"
6200                  "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6201                  Style);
6202     verifyFormat("Constructor() : a(a), b(b) {}", Style);
6203 
6204     Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
6205     verifyFormat("Constructor()\n"
6206                  "    : aaaaaaaaaaaaaaaaaaaa(a)\n"
6207                  "    , bbbbbbbbbbbbbbbbbbbbb(b) {}",
6208                  Style);
6209     verifyFormat("Constructor() : a(a), b(b) {}", Style);
6210 
6211     Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
6212     Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6213     verifyFormat("Constructor()\n"
6214                  "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6215                  Style);
6216 
6217     Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
6218     verifyFormat("Constructor()\n"
6219                  "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
6220                  "      bbbbbbbbbbbbbbbbbbbbb(b) {}",
6221                  Style);
6222 
6223     Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
6224     Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6225     verifyFormat("Constructor() :\n"
6226                  "    aaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6227                  Style);
6228 
6229     Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
6230     verifyFormat("Constructor() :\n"
6231                  "    aaaaaaaaaaaaaaaaaa(a),\n"
6232                  "    bbbbbbbbbbbbbbbbbbbbb(b) {}",
6233                  Style);
6234   }
6235 
6236   // Test interactions between AllowAllParametersOfDeclarationOnNextLine and
6237   // AllowAllConstructorInitializersOnNextLine in all
6238   // BreakConstructorInitializers modes
6239   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
6240   Style.AllowAllParametersOfDeclarationOnNextLine = true;
6241   verifyFormat("SomeClassWithALongName::Constructor(\n"
6242                "    int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n"
6243                "    : aaaaaaaaaaaaaaaaaaaa(a)\n"
6244                "    , bbbbbbbbbbbbbbbbbbbbb(b) {}",
6245                Style);
6246 
6247   Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6248   verifyFormat("SomeClassWithALongName::Constructor(\n"
6249                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6250                "    int bbbbbbbbbbbbb,\n"
6251                "    int cccccccccccccccc)\n"
6252                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6253                Style);
6254 
6255   Style.AllowAllParametersOfDeclarationOnNextLine = false;
6256   Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
6257   verifyFormat("SomeClassWithALongName::Constructor(\n"
6258                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6259                "    int bbbbbbbbbbbbb)\n"
6260                "    : aaaaaaaaaaaaaaaaaaaa(a)\n"
6261                "    , bbbbbbbbbbbbbbbbbbbbb(b) {}",
6262                Style);
6263 
6264   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
6265 
6266   Style.AllowAllParametersOfDeclarationOnNextLine = true;
6267   verifyFormat("SomeClassWithALongName::Constructor(\n"
6268                "    int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n"
6269                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
6270                "      bbbbbbbbbbbbbbbbbbbbb(b) {}",
6271                Style);
6272 
6273   Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6274   verifyFormat("SomeClassWithALongName::Constructor(\n"
6275                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6276                "    int bbbbbbbbbbbbb,\n"
6277                "    int cccccccccccccccc)\n"
6278                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6279                Style);
6280 
6281   Style.AllowAllParametersOfDeclarationOnNextLine = false;
6282   Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
6283   verifyFormat("SomeClassWithALongName::Constructor(\n"
6284                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6285                "    int bbbbbbbbbbbbb)\n"
6286                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
6287                "      bbbbbbbbbbbbbbbbbbbbb(b) {}",
6288                Style);
6289 
6290   Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
6291   Style.AllowAllParametersOfDeclarationOnNextLine = true;
6292   verifyFormat("SomeClassWithALongName::Constructor(\n"
6293                "    int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb) :\n"
6294                "    aaaaaaaaaaaaaaaaaaaa(a),\n"
6295                "    bbbbbbbbbbbbbbbbbbbbb(b) {}",
6296                Style);
6297 
6298   Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6299   verifyFormat("SomeClassWithALongName::Constructor(\n"
6300                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6301                "    int bbbbbbbbbbbbb,\n"
6302                "    int cccccccccccccccc) :\n"
6303                "    aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6304                Style);
6305 
6306   Style.AllowAllParametersOfDeclarationOnNextLine = false;
6307   Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
6308   verifyFormat("SomeClassWithALongName::Constructor(\n"
6309                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6310                "    int bbbbbbbbbbbbb) :\n"
6311                "    aaaaaaaaaaaaaaaaaaaa(a),\n"
6312                "    bbbbbbbbbbbbbbbbbbbbb(b) {}",
6313                Style);
6314 }
6315 
6316 TEST_F(FormatTest, AllowAllArgumentsOnNextLine) {
6317   FormatStyle Style = getLLVMStyleWithColumns(60);
6318   Style.BinPackArguments = false;
6319   for (int i = 0; i < 4; ++i) {
6320     // Test all combinations of parameters that should not have an effect.
6321     Style.AllowAllParametersOfDeclarationOnNextLine = i & 1;
6322     Style.PackConstructorInitializers =
6323         i & 2 ? FormatStyle::PCIS_BinPack : FormatStyle::PCIS_Never;
6324 
6325     Style.AllowAllArgumentsOnNextLine = true;
6326     verifyFormat("void foo() {\n"
6327                  "  FunctionCallWithReallyLongName(\n"
6328                  "      aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb);\n"
6329                  "}",
6330                  Style);
6331     Style.AllowAllArgumentsOnNextLine = false;
6332     verifyFormat("void foo() {\n"
6333                  "  FunctionCallWithReallyLongName(\n"
6334                  "      aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6335                  "      bbbbbbbbbbbb);\n"
6336                  "}",
6337                  Style);
6338 
6339     Style.AllowAllArgumentsOnNextLine = true;
6340     verifyFormat("void foo() {\n"
6341                  "  auto VariableWithReallyLongName = {\n"
6342                  "      aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb};\n"
6343                  "}",
6344                  Style);
6345     Style.AllowAllArgumentsOnNextLine = false;
6346     verifyFormat("void foo() {\n"
6347                  "  auto VariableWithReallyLongName = {\n"
6348                  "      aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6349                  "      bbbbbbbbbbbb};\n"
6350                  "}",
6351                  Style);
6352   }
6353 
6354   // This parameter should not affect declarations.
6355   Style.BinPackParameters = false;
6356   Style.AllowAllArgumentsOnNextLine = false;
6357   Style.AllowAllParametersOfDeclarationOnNextLine = true;
6358   verifyFormat("void FunctionCallWithReallyLongName(\n"
6359                "    int aaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbb);",
6360                Style);
6361   Style.AllowAllParametersOfDeclarationOnNextLine = false;
6362   verifyFormat("void FunctionCallWithReallyLongName(\n"
6363                "    int aaaaaaaaaaaaaaaaaaaaaaa,\n"
6364                "    int bbbbbbbbbbbb);",
6365                Style);
6366 }
6367 
6368 TEST_F(FormatTest, AllowAllArgumentsOnNextLineDontAlign) {
6369   // Check that AllowAllArgumentsOnNextLine is respected for both BAS_DontAlign
6370   // and BAS_Align.
6371   FormatStyle Style = getLLVMStyleWithColumns(35);
6372   StringRef Input = "functionCall(paramA, paramB, paramC);\n"
6373                     "void functionDecl(int A, int B, int C);";
6374   Style.AllowAllArgumentsOnNextLine = false;
6375   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
6376   EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n"
6377                       "    paramC);\n"
6378                       "void functionDecl(int A, int B,\n"
6379                       "    int C);"),
6380             format(Input, Style));
6381   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
6382   EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n"
6383                       "             paramC);\n"
6384                       "void functionDecl(int A, int B,\n"
6385                       "                  int C);"),
6386             format(Input, Style));
6387   // However, BAS_AlwaysBreak should take precedence over
6388   // AllowAllArgumentsOnNextLine.
6389   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
6390   EXPECT_EQ(StringRef("functionCall(\n"
6391                       "    paramA, paramB, paramC);\n"
6392                       "void functionDecl(\n"
6393                       "    int A, int B, int C);"),
6394             format(Input, Style));
6395 
6396   // When AllowAllArgumentsOnNextLine is set, we prefer breaking before the
6397   // first argument.
6398   Style.AllowAllArgumentsOnNextLine = true;
6399   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
6400   EXPECT_EQ(StringRef("functionCall(\n"
6401                       "    paramA, paramB, paramC);\n"
6402                       "void functionDecl(\n"
6403                       "    int A, int B, int C);"),
6404             format(Input, Style));
6405   // It wouldn't fit on one line with aligned parameters so this setting
6406   // doesn't change anything for BAS_Align.
6407   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
6408   EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n"
6409                       "             paramC);\n"
6410                       "void functionDecl(int A, int B,\n"
6411                       "                  int C);"),
6412             format(Input, Style));
6413   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
6414   EXPECT_EQ(StringRef("functionCall(\n"
6415                       "    paramA, paramB, paramC);\n"
6416                       "void functionDecl(\n"
6417                       "    int A, int B, int C);"),
6418             format(Input, Style));
6419 }
6420 
6421 TEST_F(FormatTest, BreakConstructorInitializersAfterColon) {
6422   FormatStyle Style = getLLVMStyle();
6423   Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
6424 
6425   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
6426   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}",
6427                getStyleWithColumns(Style, 45));
6428   verifyFormat("Constructor() :\n"
6429                "    Initializer(FitsOnTheLine) {}",
6430                getStyleWithColumns(Style, 44));
6431   verifyFormat("Constructor() :\n"
6432                "    Initializer(FitsOnTheLine) {}",
6433                getStyleWithColumns(Style, 43));
6434 
6435   verifyFormat("template <typename T>\n"
6436                "Constructor() : Initializer(FitsOnTheLine) {}",
6437                getStyleWithColumns(Style, 50));
6438   Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6439   verifyFormat(
6440       "SomeClass::Constructor() :\n"
6441       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
6442       Style);
6443 
6444   Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack;
6445   verifyFormat(
6446       "SomeClass::Constructor() :\n"
6447       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
6448       Style);
6449 
6450   verifyFormat(
6451       "SomeClass::Constructor() :\n"
6452       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6453       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
6454       Style);
6455   verifyFormat(
6456       "SomeClass::Constructor() :\n"
6457       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6458       "    aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
6459       Style);
6460   verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6461                "            aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
6462                "    aaaaaaaaaa(aaaaaa) {}",
6463                Style);
6464 
6465   verifyFormat("Constructor() :\n"
6466                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6467                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6468                "                             aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6469                "    aaaaaaaaaaaaaaaaaaaaaaa() {}",
6470                Style);
6471 
6472   verifyFormat("Constructor() :\n"
6473                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6474                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
6475                Style);
6476 
6477   verifyFormat("Constructor(int Parameter = 0) :\n"
6478                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
6479                "    aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}",
6480                Style);
6481   verifyFormat("Constructor() :\n"
6482                "    aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
6483                "}",
6484                getStyleWithColumns(Style, 60));
6485   verifyFormat("Constructor() :\n"
6486                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6487                "        aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}",
6488                Style);
6489 
6490   // Here a line could be saved by splitting the second initializer onto two
6491   // lines, but that is not desirable.
6492   verifyFormat("Constructor() :\n"
6493                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
6494                "    aaaaaaaaaaa(aaaaaaaaaaa),\n"
6495                "    aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
6496                Style);
6497 
6498   FormatStyle OnePerLine = Style;
6499   OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
6500   verifyFormat("SomeClass::Constructor() :\n"
6501                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6502                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6503                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
6504                OnePerLine);
6505   verifyFormat("SomeClass::Constructor() :\n"
6506                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
6507                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6508                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
6509                OnePerLine);
6510   verifyFormat("MyClass::MyClass(int var) :\n"
6511                "    some_var_(var),            // 4 space indent\n"
6512                "    some_other_var_(var + 1) { // lined up\n"
6513                "}",
6514                OnePerLine);
6515   verifyFormat("Constructor() :\n"
6516                "    aaaaa(aaaaaa),\n"
6517                "    aaaaa(aaaaaa),\n"
6518                "    aaaaa(aaaaaa),\n"
6519                "    aaaaa(aaaaaa),\n"
6520                "    aaaaa(aaaaaa) {}",
6521                OnePerLine);
6522   verifyFormat("Constructor() :\n"
6523                "    aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
6524                "          aaaaaaaaaaaaaaaaaaaaaa) {}",
6525                OnePerLine);
6526   OnePerLine.BinPackParameters = false;
6527   verifyFormat("Constructor() :\n"
6528                "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
6529                "        aaaaaaaaaaa().aaa(),\n"
6530                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
6531                OnePerLine);
6532   OnePerLine.ColumnLimit = 60;
6533   verifyFormat("Constructor() :\n"
6534                "    aaaaaaaaaaaaaaaaaaaa(a),\n"
6535                "    bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
6536                OnePerLine);
6537 
6538   EXPECT_EQ("Constructor() :\n"
6539             "    // Comment forcing unwanted break.\n"
6540             "    aaaa(aaaa) {}",
6541             format("Constructor() :\n"
6542                    "    // Comment forcing unwanted break.\n"
6543                    "    aaaa(aaaa) {}",
6544                    Style));
6545 
6546   Style.ColumnLimit = 0;
6547   verifyFormat("SomeClass::Constructor() :\n"
6548                "    a(a) {}",
6549                Style);
6550   verifyFormat("SomeClass::Constructor() noexcept :\n"
6551                "    a(a) {}",
6552                Style);
6553   verifyFormat("SomeClass::Constructor() :\n"
6554                "    a(a), b(b), c(c) {}",
6555                Style);
6556   verifyFormat("SomeClass::Constructor() :\n"
6557                "    a(a) {\n"
6558                "  foo();\n"
6559                "  bar();\n"
6560                "}",
6561                Style);
6562 
6563   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
6564   verifyFormat("SomeClass::Constructor() :\n"
6565                "    a(a), b(b), c(c) {\n"
6566                "}",
6567                Style);
6568   verifyFormat("SomeClass::Constructor() :\n"
6569                "    a(a) {\n"
6570                "}",
6571                Style);
6572 
6573   Style.ColumnLimit = 80;
6574   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
6575   Style.ConstructorInitializerIndentWidth = 2;
6576   verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}", Style);
6577   verifyFormat("SomeClass::Constructor() :\n"
6578                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6579                "  bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}",
6580                Style);
6581 
6582   // `ConstructorInitializerIndentWidth` actually applies to InheritanceList as
6583   // well
6584   Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
6585   verifyFormat(
6586       "class SomeClass\n"
6587       "  : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6588       "    public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
6589       Style);
6590   Style.BreakInheritanceList = FormatStyle::BILS_BeforeComma;
6591   verifyFormat(
6592       "class SomeClass\n"
6593       "  : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6594       "  , public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
6595       Style);
6596   Style.BreakInheritanceList = FormatStyle::BILS_AfterColon;
6597   verifyFormat(
6598       "class SomeClass :\n"
6599       "  public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6600       "  public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
6601       Style);
6602   Style.BreakInheritanceList = FormatStyle::BILS_AfterComma;
6603   verifyFormat(
6604       "class SomeClass\n"
6605       "  : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6606       "    public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
6607       Style);
6608 }
6609 
6610 #ifndef EXPENSIVE_CHECKS
6611 // Expensive checks enables libstdc++ checking which includes validating the
6612 // state of ranges used in std::priority_queue - this blows out the
6613 // runtime/scalability of the function and makes this test unacceptably slow.
6614 TEST_F(FormatTest, MemoizationTests) {
6615   // This breaks if the memoization lookup does not take \c Indent and
6616   // \c LastSpace into account.
6617   verifyFormat(
6618       "extern CFRunLoopTimerRef\n"
6619       "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
6620       "                     CFTimeInterval interval, CFOptionFlags flags,\n"
6621       "                     CFIndex order, CFRunLoopTimerCallBack callout,\n"
6622       "                     CFRunLoopTimerContext *context) {}");
6623 
6624   // Deep nesting somewhat works around our memoization.
6625   verifyFormat(
6626       "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
6627       "    aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
6628       "        aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
6629       "            aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
6630       "                aaaaa())))))))))))))))))))))))))))))))))))))));",
6631       getLLVMStyleWithColumns(65));
6632   verifyFormat(
6633       "aaaaa(\n"
6634       "    aaaaa,\n"
6635       "    aaaaa(\n"
6636       "        aaaaa,\n"
6637       "        aaaaa(\n"
6638       "            aaaaa,\n"
6639       "            aaaaa(\n"
6640       "                aaaaa,\n"
6641       "                aaaaa(\n"
6642       "                    aaaaa,\n"
6643       "                    aaaaa(\n"
6644       "                        aaaaa,\n"
6645       "                        aaaaa(\n"
6646       "                            aaaaa,\n"
6647       "                            aaaaa(\n"
6648       "                                aaaaa,\n"
6649       "                                aaaaa(\n"
6650       "                                    aaaaa,\n"
6651       "                                    aaaaa(\n"
6652       "                                        aaaaa,\n"
6653       "                                        aaaaa(\n"
6654       "                                            aaaaa,\n"
6655       "                                            aaaaa(\n"
6656       "                                                aaaaa,\n"
6657       "                                                aaaaa))))))))))));",
6658       getLLVMStyleWithColumns(65));
6659   verifyFormat(
6660       "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"
6661       "                                  a),\n"
6662       "                                a),\n"
6663       "                              a),\n"
6664       "                            a),\n"
6665       "                          a),\n"
6666       "                        a),\n"
6667       "                      a),\n"
6668       "                    a),\n"
6669       "                  a),\n"
6670       "                a),\n"
6671       "              a),\n"
6672       "            a),\n"
6673       "          a),\n"
6674       "        a),\n"
6675       "      a),\n"
6676       "    a),\n"
6677       "  a)",
6678       getLLVMStyleWithColumns(65));
6679 
6680   // This test takes VERY long when memoization is broken.
6681   FormatStyle OnePerLine = getLLVMStyle();
6682   OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6683   OnePerLine.BinPackParameters = false;
6684   std::string input = "Constructor()\n"
6685                       "    : aaaa(a,\n";
6686   for (unsigned i = 0, e = 80; i != e; ++i) {
6687     input += "           a,\n";
6688   }
6689   input += "           a) {}";
6690   verifyFormat(input, OnePerLine);
6691 }
6692 #endif
6693 
6694 TEST_F(FormatTest, BreaksAsHighAsPossible) {
6695   verifyFormat(
6696       "void f() {\n"
6697       "  if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
6698       "      (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
6699       "    f();\n"
6700       "}");
6701   verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
6702                "    Intervals[i - 1].getRange().getLast()) {\n}");
6703 }
6704 
6705 TEST_F(FormatTest, BreaksFunctionDeclarations) {
6706   // Principially, we break function declarations in a certain order:
6707   // 1) break amongst arguments.
6708   verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
6709                "                              Cccccccccccccc cccccccccccccc);");
6710   verifyFormat("template <class TemplateIt>\n"
6711                "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
6712                "                            TemplateIt *stop) {}");
6713 
6714   // 2) break after return type.
6715   verifyFormat(
6716       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6717       "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);",
6718       getGoogleStyle());
6719 
6720   // 3) break after (.
6721   verifyFormat(
6722       "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
6723       "    Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);",
6724       getGoogleStyle());
6725 
6726   // 4) break before after nested name specifiers.
6727   verifyFormat(
6728       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6729       "SomeClasssssssssssssssssssssssssssssssssssssss::\n"
6730       "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);",
6731       getGoogleStyle());
6732 
6733   // However, there are exceptions, if a sufficient amount of lines can be
6734   // saved.
6735   // FIXME: The precise cut-offs wrt. the number of saved lines might need some
6736   // more adjusting.
6737   verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
6738                "                                  Cccccccccccccc cccccccccc,\n"
6739                "                                  Cccccccccccccc cccccccccc,\n"
6740                "                                  Cccccccccccccc cccccccccc,\n"
6741                "                                  Cccccccccccccc cccccccccc);");
6742   verifyFormat(
6743       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6744       "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
6745       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
6746       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);",
6747       getGoogleStyle());
6748   verifyFormat(
6749       "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
6750       "                                          Cccccccccccccc cccccccccc,\n"
6751       "                                          Cccccccccccccc cccccccccc,\n"
6752       "                                          Cccccccccccccc cccccccccc,\n"
6753       "                                          Cccccccccccccc cccccccccc,\n"
6754       "                                          Cccccccccccccc cccccccccc,\n"
6755       "                                          Cccccccccccccc cccccccccc);");
6756   verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
6757                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
6758                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
6759                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
6760                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
6761 
6762   // Break after multi-line parameters.
6763   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6764                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6765                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6766                "    bbbb bbbb);");
6767   verifyFormat("void SomeLoooooooooooongFunction(\n"
6768                "    std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
6769                "        aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6770                "    int bbbbbbbbbbbbb);");
6771 
6772   // Treat overloaded operators like other functions.
6773   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
6774                "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
6775   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
6776                "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
6777   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
6778                "operator<<(const SomeLooooooooooooooooooooooooogType &other);");
6779   verifyGoogleFormat(
6780       "SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
6781       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
6782   verifyGoogleFormat(
6783       "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
6784       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
6785   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6786                "    int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
6787   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
6788                "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
6789   verifyGoogleFormat(
6790       "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n"
6791       "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6792       "    bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}");
6793   verifyGoogleFormat("template <typename T>\n"
6794                      "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6795                      "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n"
6796                      "    aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);");
6797 
6798   FormatStyle Style = getLLVMStyle();
6799   Style.PointerAlignment = FormatStyle::PAS_Left;
6800   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6801                "    aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}",
6802                Style);
6803   verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
6804                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
6805                Style);
6806 }
6807 
6808 TEST_F(FormatTest, DontBreakBeforeQualifiedOperator) {
6809   // Regression test for https://bugs.llvm.org/show_bug.cgi?id=40516:
6810   // Prefer keeping `::` followed by `operator` together.
6811   EXPECT_EQ("const aaaa::bbbbbbb &\n"
6812             "ccccccccc::operator++() {\n"
6813             "  stuff();\n"
6814             "}",
6815             format("const aaaa::bbbbbbb\n"
6816                    "&ccccccccc::operator++() { stuff(); }",
6817                    getLLVMStyleWithColumns(40)));
6818 }
6819 
6820 TEST_F(FormatTest, TrailingReturnType) {
6821   verifyFormat("auto foo() -> int;\n");
6822   // correct trailing return type spacing
6823   verifyFormat("auto operator->() -> int;\n");
6824   verifyFormat("auto operator++(int) -> int;\n");
6825 
6826   verifyFormat("struct S {\n"
6827                "  auto bar() const -> int;\n"
6828                "};");
6829   verifyFormat("template <size_t Order, typename T>\n"
6830                "auto load_img(const std::string &filename)\n"
6831                "    -> alias::tensor<Order, T, mem::tag::cpu> {}");
6832   verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n"
6833                "    -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}");
6834   verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}");
6835   verifyFormat("template <typename T>\n"
6836                "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n"
6837                "    -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());");
6838 
6839   // Not trailing return types.
6840   verifyFormat("void f() { auto a = b->c(); }");
6841   verifyFormat("auto a = p->foo();");
6842   verifyFormat("int a = p->foo();");
6843   verifyFormat("auto lmbd = [] NOEXCEPT -> int { return 0; };");
6844 }
6845 
6846 TEST_F(FormatTest, DeductionGuides) {
6847   verifyFormat("template <class T> A(const T &, const T &) -> A<T &>;");
6848   verifyFormat("template <class T> explicit A(T &, T &&) -> A<T>;");
6849   verifyFormat("template <class... Ts> S(Ts...) -> S<Ts...>;");
6850   verifyFormat(
6851       "template <class... T>\n"
6852       "array(T &&...t) -> array<std::common_type_t<T...>, sizeof...(T)>;");
6853   verifyFormat("template <class T> A() -> A<decltype(p->foo<3>())>;");
6854   verifyFormat("template <class T> A() -> A<decltype(foo<traits<1>>)>;");
6855   verifyFormat("template <class T> A() -> A<sizeof(p->foo<1>)>;");
6856   verifyFormat("template <class T> A() -> A<(3 < 2)>;");
6857   verifyFormat("template <class T> A() -> A<((3) < (2))>;");
6858   verifyFormat("template <class T> x() -> x<1>;");
6859   verifyFormat("template <class T> explicit x(T &) -> x<1>;");
6860 
6861   // Ensure not deduction guides.
6862   verifyFormat("c()->f<int>();");
6863   verifyFormat("x()->foo<1>;");
6864   verifyFormat("x = p->foo<3>();");
6865   verifyFormat("x()->x<1>();");
6866   verifyFormat("x()->x<1>;");
6867 }
6868 
6869 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
6870   // Avoid breaking before trailing 'const' or other trailing annotations, if
6871   // they are not function-like.
6872   FormatStyle Style = getGoogleStyleWithColumns(47);
6873   verifyFormat("void someLongFunction(\n"
6874                "    int someLoooooooooooooongParameter) const {\n}",
6875                getLLVMStyleWithColumns(47));
6876   verifyFormat("LoooooongReturnType\n"
6877                "someLoooooooongFunction() const {}",
6878                getLLVMStyleWithColumns(47));
6879   verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
6880                "    const {}",
6881                Style);
6882   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
6883                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
6884   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
6885                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
6886   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
6887                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
6888   verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
6889                "                   aaaaaaaaaaa aaaaa) const override;");
6890   verifyGoogleFormat(
6891       "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
6892       "    const override;");
6893 
6894   // Even if the first parameter has to be wrapped.
6895   verifyFormat("void someLongFunction(\n"
6896                "    int someLongParameter) const {}",
6897                getLLVMStyleWithColumns(46));
6898   verifyFormat("void someLongFunction(\n"
6899                "    int someLongParameter) const {}",
6900                Style);
6901   verifyFormat("void someLongFunction(\n"
6902                "    int someLongParameter) override {}",
6903                Style);
6904   verifyFormat("void someLongFunction(\n"
6905                "    int someLongParameter) OVERRIDE {}",
6906                Style);
6907   verifyFormat("void someLongFunction(\n"
6908                "    int someLongParameter) final {}",
6909                Style);
6910   verifyFormat("void someLongFunction(\n"
6911                "    int someLongParameter) FINAL {}",
6912                Style);
6913   verifyFormat("void someLongFunction(\n"
6914                "    int parameter) const override {}",
6915                Style);
6916 
6917   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
6918   verifyFormat("void someLongFunction(\n"
6919                "    int someLongParameter) const\n"
6920                "{\n"
6921                "}",
6922                Style);
6923 
6924   Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
6925   verifyFormat("void someLongFunction(\n"
6926                "    int someLongParameter) const\n"
6927                "  {\n"
6928                "  }",
6929                Style);
6930 
6931   // Unless these are unknown annotations.
6932   verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
6933                "                  aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
6934                "    LONG_AND_UGLY_ANNOTATION;");
6935 
6936   // Breaking before function-like trailing annotations is fine to keep them
6937   // close to their arguments.
6938   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
6939                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
6940   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
6941                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
6942   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
6943                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
6944   verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n"
6945                      "    AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);");
6946   verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});");
6947 
6948   verifyFormat(
6949       "void aaaaaaaaaaaaaaaaaa()\n"
6950       "    __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
6951       "                   aaaaaaaaaaaaaaaaaaaaaaaaa));");
6952   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6953                "    __attribute__((unused));");
6954   verifyGoogleFormat(
6955       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6956       "    GUARDED_BY(aaaaaaaaaaaa);");
6957   verifyGoogleFormat(
6958       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6959       "    GUARDED_BY(aaaaaaaaaaaa);");
6960   verifyGoogleFormat(
6961       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
6962       "    aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
6963   verifyGoogleFormat(
6964       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
6965       "    aaaaaaaaaaaaaaaaaaaaaaaaa;");
6966 }
6967 
6968 TEST_F(FormatTest, FunctionAnnotations) {
6969   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
6970                "int OldFunction(const string &parameter) {}");
6971   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
6972                "string OldFunction(const string &parameter) {}");
6973   verifyFormat("template <typename T>\n"
6974                "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
6975                "string OldFunction(const string &parameter) {}");
6976 
6977   // Not function annotations.
6978   verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6979                "                << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
6980   verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n"
6981                "       ThisIsATestWithAReallyReallyReallyReallyLongName) {}");
6982   verifyFormat("MACRO(abc).function() // wrap\n"
6983                "    << abc;");
6984   verifyFormat("MACRO(abc)->function() // wrap\n"
6985                "    << abc;");
6986   verifyFormat("MACRO(abc)::function() // wrap\n"
6987                "    << abc;");
6988 }
6989 
6990 TEST_F(FormatTest, BreaksDesireably) {
6991   verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
6992                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
6993                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
6994   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6995                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
6996                "}");
6997 
6998   verifyFormat(
6999       "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7000       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
7001 
7002   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7003                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7004                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
7005 
7006   verifyFormat(
7007       "aaaaaaaa(aaaaaaaaaaaaa,\n"
7008       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7009       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
7010       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7011       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
7012 
7013   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
7014                "    (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7015 
7016   verifyFormat(
7017       "void f() {\n"
7018       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
7019       "                                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
7020       "}");
7021   verifyFormat(
7022       "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7023       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
7024   verifyFormat(
7025       "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7026       "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
7027   verifyFormat(
7028       "aaaaaa(aaa,\n"
7029       "       new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7030       "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7031       "       aaaa);");
7032   verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7033                "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7034                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7035 
7036   // Indent consistently independent of call expression and unary operator.
7037   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
7038                "    dddddddddddddddddddddddddddddd));");
7039   verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
7040                "    dddddddddddddddddddddddddddddd));");
7041   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
7042                "    dddddddddddddddddddddddddddddd));");
7043 
7044   // This test case breaks on an incorrect memoization, i.e. an optimization not
7045   // taking into account the StopAt value.
7046   verifyFormat(
7047       "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
7048       "       aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
7049       "       aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
7050       "       (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7051 
7052   verifyFormat("{\n  {\n    {\n"
7053                "      Annotation.SpaceRequiredBefore =\n"
7054                "          Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
7055                "          Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
7056                "    }\n  }\n}");
7057 
7058   // Break on an outer level if there was a break on an inner level.
7059   EXPECT_EQ("f(g(h(a, // comment\n"
7060             "      b, c),\n"
7061             "    d, e),\n"
7062             "  x, y);",
7063             format("f(g(h(a, // comment\n"
7064                    "    b, c), d, e), x, y);"));
7065 
7066   // Prefer breaking similar line breaks.
7067   verifyFormat(
7068       "const int kTrackingOptions = NSTrackingMouseMoved |\n"
7069       "                             NSTrackingMouseEnteredAndExited |\n"
7070       "                             NSTrackingActiveAlways;");
7071 }
7072 
7073 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) {
7074   FormatStyle NoBinPacking = getGoogleStyle();
7075   NoBinPacking.BinPackParameters = false;
7076   NoBinPacking.BinPackArguments = true;
7077   verifyFormat("void f() {\n"
7078                "  f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n"
7079                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
7080                "}",
7081                NoBinPacking);
7082   verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n"
7083                "       int aaaaaaaaaaaaaaaaaaaa,\n"
7084                "       int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
7085                NoBinPacking);
7086 
7087   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
7088   verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7089                "                        vector<int> bbbbbbbbbbbbbbb);",
7090                NoBinPacking);
7091   // FIXME: This behavior difference is probably not wanted. However, currently
7092   // we cannot distinguish BreakBeforeParameter being set because of the wrapped
7093   // template arguments from BreakBeforeParameter being set because of the
7094   // one-per-line formatting.
7095   verifyFormat(
7096       "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n"
7097       "                                             aaaaaaaaaa> aaaaaaaaaa);",
7098       NoBinPacking);
7099   verifyFormat(
7100       "void fffffffffff(\n"
7101       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n"
7102       "        aaaaaaaaaa);");
7103 }
7104 
7105 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
7106   FormatStyle NoBinPacking = getGoogleStyle();
7107   NoBinPacking.BinPackParameters = false;
7108   NoBinPacking.BinPackArguments = false;
7109   verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
7110                "  aaaaaaaaaaaaaaaaaaaa,\n"
7111                "  aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
7112                NoBinPacking);
7113   verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
7114                "        aaaaaaaaaaaaa,\n"
7115                "        aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
7116                NoBinPacking);
7117   verifyFormat(
7118       "aaaaaaaa(aaaaaaaaaaaaa,\n"
7119       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7120       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
7121       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7122       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
7123       NoBinPacking);
7124   verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
7125                "    .aaaaaaaaaaaaaaaaaa();",
7126                NoBinPacking);
7127   verifyFormat("void f() {\n"
7128                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7129                "      aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n"
7130                "}",
7131                NoBinPacking);
7132 
7133   verifyFormat(
7134       "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7135       "             aaaaaaaaaaaa,\n"
7136       "             aaaaaaaaaaaa);",
7137       NoBinPacking);
7138   verifyFormat(
7139       "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
7140       "                               ddddddddddddddddddddddddddddd),\n"
7141       "             test);",
7142       NoBinPacking);
7143 
7144   verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
7145                "            aaaaaaaaaaaaaaaaaaaaaaa,\n"
7146                "            aaaaaaaaaaaaaaaaaaaaaaa>\n"
7147                "    aaaaaaaaaaaaaaaaaa;",
7148                NoBinPacking);
7149   verifyFormat("a(\"a\"\n"
7150                "  \"a\",\n"
7151                "  a);");
7152 
7153   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
7154   verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
7155                "                aaaaaaaaa,\n"
7156                "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7157                NoBinPacking);
7158   verifyFormat(
7159       "void f() {\n"
7160       "  aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
7161       "      .aaaaaaa();\n"
7162       "}",
7163       NoBinPacking);
7164   verifyFormat(
7165       "template <class SomeType, class SomeOtherType>\n"
7166       "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
7167       NoBinPacking);
7168 }
7169 
7170 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
7171   FormatStyle Style = getLLVMStyleWithColumns(15);
7172   Style.ExperimentalAutoDetectBinPacking = true;
7173   EXPECT_EQ("aaa(aaaa,\n"
7174             "    aaaa,\n"
7175             "    aaaa);\n"
7176             "aaa(aaaa,\n"
7177             "    aaaa,\n"
7178             "    aaaa);",
7179             format("aaa(aaaa,\n" // one-per-line
7180                    "  aaaa,\n"
7181                    "    aaaa  );\n"
7182                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
7183                    Style));
7184   EXPECT_EQ("aaa(aaaa, aaaa,\n"
7185             "    aaaa);\n"
7186             "aaa(aaaa, aaaa,\n"
7187             "    aaaa);",
7188             format("aaa(aaaa,  aaaa,\n" // bin-packed
7189                    "    aaaa  );\n"
7190                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
7191                    Style));
7192 }
7193 
7194 TEST_F(FormatTest, FormatsBuilderPattern) {
7195   verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n"
7196                "    .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
7197                "    .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
7198                "    .StartsWith(\".init\", ORDER_INIT)\n"
7199                "    .StartsWith(\".fini\", ORDER_FINI)\n"
7200                "    .StartsWith(\".hash\", ORDER_HASH)\n"
7201                "    .Default(ORDER_TEXT);\n");
7202 
7203   verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
7204                "       aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
7205   verifyFormat("aaaaaaa->aaaaaaa\n"
7206                "    ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7207                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7208                "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
7209   verifyFormat(
7210       "aaaaaaa->aaaaaaa\n"
7211       "    ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7212       "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
7213   verifyFormat(
7214       "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
7215       "    aaaaaaaaaaaaaa);");
7216   verifyFormat(
7217       "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
7218       "    aaaaaa->aaaaaaaaaaaa()\n"
7219       "        ->aaaaaaaaaaaaaaaa(\n"
7220       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7221       "        ->aaaaaaaaaaaaaaaaa();");
7222   verifyGoogleFormat(
7223       "void f() {\n"
7224       "  someo->Add((new util::filetools::Handler(dir))\n"
7225       "                 ->OnEvent1(NewPermanentCallback(\n"
7226       "                     this, &HandlerHolderClass::EventHandlerCBA))\n"
7227       "                 ->OnEvent2(NewPermanentCallback(\n"
7228       "                     this, &HandlerHolderClass::EventHandlerCBB))\n"
7229       "                 ->OnEvent3(NewPermanentCallback(\n"
7230       "                     this, &HandlerHolderClass::EventHandlerCBC))\n"
7231       "                 ->OnEvent5(NewPermanentCallback(\n"
7232       "                     this, &HandlerHolderClass::EventHandlerCBD))\n"
7233       "                 ->OnEvent6(NewPermanentCallback(\n"
7234       "                     this, &HandlerHolderClass::EventHandlerCBE)));\n"
7235       "}");
7236 
7237   verifyFormat(
7238       "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
7239   verifyFormat("aaaaaaaaaaaaaaa()\n"
7240                "    .aaaaaaaaaaaaaaa()\n"
7241                "    .aaaaaaaaaaaaaaa()\n"
7242                "    .aaaaaaaaaaaaaaa()\n"
7243                "    .aaaaaaaaaaaaaaa();");
7244   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
7245                "    .aaaaaaaaaaaaaaa()\n"
7246                "    .aaaaaaaaaaaaaaa()\n"
7247                "    .aaaaaaaaaaaaaaa();");
7248   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
7249                "    .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
7250                "    .aaaaaaaaaaaaaaa();");
7251   verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
7252                "    ->aaaaaaaaaaaaaae(0)\n"
7253                "    ->aaaaaaaaaaaaaaa();");
7254 
7255   // Don't linewrap after very short segments.
7256   verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7257                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7258                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
7259   verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7260                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7261                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
7262   verifyFormat("aaa()\n"
7263                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7264                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7265                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
7266 
7267   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
7268                "    .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7269                "    .has<bbbbbbbbbbbbbbbbbbbbb>();");
7270   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
7271                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
7272                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
7273 
7274   // Prefer not to break after empty parentheses.
7275   verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
7276                "    First->LastNewlineOffset);");
7277 
7278   // Prefer not to create "hanging" indents.
7279   verifyFormat(
7280       "return !soooooooooooooome_map\n"
7281       "            .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7282       "            .second;");
7283   verifyFormat(
7284       "return aaaaaaaaaaaaaaaa\n"
7285       "    .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n"
7286       "    .aaaa(aaaaaaaaaaaaaa);");
7287   // No hanging indent here.
7288   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n"
7289                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7290   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n"
7291                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7292   verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
7293                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7294                getLLVMStyleWithColumns(60));
7295   verifyFormat("aaaaaaaaaaaaaaaaaa\n"
7296                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
7297                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7298                getLLVMStyleWithColumns(59));
7299   verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7300                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7301                "    .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7302 
7303   // Dont break if only closing statements before member call
7304   verifyFormat("test() {\n"
7305                "  ([]() -> {\n"
7306                "    int b = 32;\n"
7307                "    return 3;\n"
7308                "  }).foo();\n"
7309                "}");
7310   verifyFormat("test() {\n"
7311                "  (\n"
7312                "      []() -> {\n"
7313                "        int b = 32;\n"
7314                "        return 3;\n"
7315                "      },\n"
7316                "      foo, bar)\n"
7317                "      .foo();\n"
7318                "}");
7319   verifyFormat("test() {\n"
7320                "  ([]() -> {\n"
7321                "    int b = 32;\n"
7322                "    return 3;\n"
7323                "  })\n"
7324                "      .foo()\n"
7325                "      .bar();\n"
7326                "}");
7327   verifyFormat("test() {\n"
7328                "  ([]() -> {\n"
7329                "    int b = 32;\n"
7330                "    return 3;\n"
7331                "  })\n"
7332                "      .foo(\"aaaaaaaaaaaaaaaaa\"\n"
7333                "           \"bbbb\");\n"
7334                "}",
7335                getLLVMStyleWithColumns(30));
7336 }
7337 
7338 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
7339   verifyFormat(
7340       "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
7341       "    bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
7342   verifyFormat(
7343       "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n"
7344       "    bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}");
7345 
7346   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
7347                "    ccccccccccccccccccccccccc) {\n}");
7348   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n"
7349                "    ccccccccccccccccccccccccc) {\n}");
7350 
7351   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
7352                "    ccccccccccccccccccccccccc) {\n}");
7353   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n"
7354                "    ccccccccccccccccccccccccc) {\n}");
7355 
7356   verifyFormat(
7357       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
7358       "    ccccccccccccccccccccccccc) {\n}");
7359   verifyFormat(
7360       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n"
7361       "    ccccccccccccccccccccccccc) {\n}");
7362 
7363   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
7364                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
7365                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
7366                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
7367   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n"
7368                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n"
7369                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n"
7370                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
7371 
7372   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
7373                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
7374                "    aaaaaaaaaaaaaaa != aa) {\n}");
7375   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n"
7376                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n"
7377                "    aaaaaaaaaaaaaaa != aa) {\n}");
7378 }
7379 
7380 TEST_F(FormatTest, BreaksAfterAssignments) {
7381   verifyFormat(
7382       "unsigned Cost =\n"
7383       "    TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
7384       "                        SI->getPointerAddressSpaceee());\n");
7385   verifyFormat(
7386       "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
7387       "    Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
7388 
7389   verifyFormat(
7390       "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
7391       "    aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
7392   verifyFormat("unsigned OriginalStartColumn =\n"
7393                "    SourceMgr.getSpellingColumnNumber(\n"
7394                "        Current.FormatTok.getStartOfNonWhitespace()) -\n"
7395                "    1;");
7396 }
7397 
7398 TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) {
7399   FormatStyle Style = getLLVMStyle();
7400   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
7401                "    bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;",
7402                Style);
7403 
7404   Style.PenaltyBreakAssignment = 20;
7405   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
7406                "                                 cccccccccccccccccccccccccc;",
7407                Style);
7408 }
7409 
7410 TEST_F(FormatTest, AlignsAfterAssignments) {
7411   verifyFormat(
7412       "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7413       "             aaaaaaaaaaaaaaaaaaaaaaaaa;");
7414   verifyFormat(
7415       "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7416       "          aaaaaaaaaaaaaaaaaaaaaaaaa;");
7417   verifyFormat(
7418       "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7419       "           aaaaaaaaaaaaaaaaaaaaaaaaa;");
7420   verifyFormat(
7421       "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7422       "              aaaaaaaaaaaaaaaaaaaaaaaaa);");
7423   verifyFormat(
7424       "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
7425       "                                            aaaaaaaaaaaaaaaaaaaaaaaa +\n"
7426       "                                            aaaaaaaaaaaaaaaaaaaaaaaa;");
7427 }
7428 
7429 TEST_F(FormatTest, AlignsAfterReturn) {
7430   verifyFormat(
7431       "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7432       "       aaaaaaaaaaaaaaaaaaaaaaaaa;");
7433   verifyFormat(
7434       "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7435       "        aaaaaaaaaaaaaaaaaaaaaaaaa);");
7436   verifyFormat(
7437       "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
7438       "       aaaaaaaaaaaaaaaaaaaaaa();");
7439   verifyFormat(
7440       "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
7441       "        aaaaaaaaaaaaaaaaaaaaaa());");
7442   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7443                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7444   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7445                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
7446                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
7447   verifyFormat("return\n"
7448                "    // true if code is one of a or b.\n"
7449                "    code == a || code == b;");
7450 }
7451 
7452 TEST_F(FormatTest, AlignsAfterOpenBracket) {
7453   verifyFormat(
7454       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
7455       "                                                aaaaaaaaa aaaaaaa) {}");
7456   verifyFormat(
7457       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
7458       "                                               aaaaaaaaaaa aaaaaaaaa);");
7459   verifyFormat(
7460       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
7461       "                                             aaaaaaaaaaaaaaaaaaaaa));");
7462   FormatStyle Style = getLLVMStyle();
7463   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
7464   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7465                "    aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}",
7466                Style);
7467   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
7468                "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);",
7469                Style);
7470   verifyFormat("SomeLongVariableName->someFunction(\n"
7471                "    foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));",
7472                Style);
7473   verifyFormat(
7474       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
7475       "    aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
7476       Style);
7477   verifyFormat(
7478       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
7479       "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7480       Style);
7481   verifyFormat(
7482       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
7483       "    aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
7484       Style);
7485 
7486   verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n"
7487                "    ccccccc(aaaaaaaaaaaaaaaaa,         //\n"
7488                "        b));",
7489                Style);
7490 
7491   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
7492   Style.BinPackArguments = false;
7493   Style.BinPackParameters = false;
7494   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7495                "    aaaaaaaaaaa aaaaaaaa,\n"
7496                "    aaaaaaaaa aaaaaaa,\n"
7497                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
7498                Style);
7499   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
7500                "    aaaaaaaaaaa aaaaaaaaa,\n"
7501                "    aaaaaaaaaaa aaaaaaaaa,\n"
7502                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7503                Style);
7504   verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n"
7505                "    aaaaaaaaaaaaaaa,\n"
7506                "    aaaaaaaaaaaaaaaaaaaaa,\n"
7507                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
7508                Style);
7509   verifyFormat(
7510       "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n"
7511       "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
7512       Style);
7513   verifyFormat(
7514       "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n"
7515       "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
7516       Style);
7517   verifyFormat(
7518       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
7519       "    aaaaaaaaaaaaaaaaaaaaa(\n"
7520       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n"
7521       "    aaaaaaaaaaaaaaaa);",
7522       Style);
7523   verifyFormat(
7524       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
7525       "    aaaaaaaaaaaaaaaaaaaaa(\n"
7526       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n"
7527       "    aaaaaaaaaaaaaaaa);",
7528       Style);
7529 }
7530 
7531 TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
7532   FormatStyle Style = getLLVMStyleWithColumns(40);
7533   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
7534                "          bbbbbbbbbbbbbbbbbbbbbb);",
7535                Style);
7536   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
7537   Style.AlignOperands = FormatStyle::OAS_DontAlign;
7538   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
7539                "          bbbbbbbbbbbbbbbbbbbbbb);",
7540                Style);
7541   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
7542   Style.AlignOperands = FormatStyle::OAS_Align;
7543   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
7544                "          bbbbbbbbbbbbbbbbbbbbbb);",
7545                Style);
7546   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
7547   Style.AlignOperands = FormatStyle::OAS_DontAlign;
7548   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
7549                "    bbbbbbbbbbbbbbbbbbbbbb);",
7550                Style);
7551 }
7552 
7553 TEST_F(FormatTest, BreaksConditionalExpressions) {
7554   verifyFormat(
7555       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7556       "                               ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7557       "                               : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7558   verifyFormat(
7559       "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
7560       "     aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7561       "                                : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7562   verifyFormat(
7563       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7564       "                                   : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7565   verifyFormat("aaaa(aaaaaaaaa, aaaaaaaaa,\n"
7566                "     aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7567                "             : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7568   verifyFormat(
7569       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
7570       "                                                    : aaaaaaaaaaaaa);");
7571   verifyFormat(
7572       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7573       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7574       "                                    : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7575       "                   aaaaaaaaaaaaa);");
7576   verifyFormat(
7577       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7578       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7579       "                   aaaaaaaaaaaaa);");
7580   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7581                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7582                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7583                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7584                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7585   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7586                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7587                "           ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7588                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7589                "           : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7590                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7591                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7592   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7593                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7594                "           ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7595                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7596                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7597   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7598                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7599                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
7600   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
7601                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7602                "        ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7603                "        : aaaaaaaaaaaaaaaa;");
7604   verifyFormat(
7605       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7606       "    ? aaaaaaaaaaaaaaa\n"
7607       "    : aaaaaaaaaaaaaaa;");
7608   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
7609                "          aaaaaaaaa\n"
7610                "      ? b\n"
7611                "      : c);");
7612   verifyFormat("return aaaa == bbbb\n"
7613                "           // comment\n"
7614                "           ? aaaa\n"
7615                "           : bbbb;");
7616   verifyFormat("unsigned Indent =\n"
7617                "    format(TheLine.First,\n"
7618                "           IndentForLevel[TheLine.Level] >= 0\n"
7619                "               ? IndentForLevel[TheLine.Level]\n"
7620                "               : TheLine * 2,\n"
7621                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
7622                getLLVMStyleWithColumns(60));
7623   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
7624                "                  ? aaaaaaaaaaaaaaa\n"
7625                "                  : bbbbbbbbbbbbbbb //\n"
7626                "                        ? ccccccccccccccc\n"
7627                "                        : ddddddddddddddd;");
7628   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
7629                "                  ? aaaaaaaaaaaaaaa\n"
7630                "                  : (bbbbbbbbbbbbbbb //\n"
7631                "                         ? ccccccccccccccc\n"
7632                "                         : ddddddddddddddd);");
7633   verifyFormat(
7634       "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7635       "                                      ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7636       "                                            aaaaaaaaaaaaaaaaaaaaa +\n"
7637       "                                            aaaaaaaaaaaaaaaaaaaaa\n"
7638       "                                      : aaaaaaaaaa;");
7639   verifyFormat(
7640       "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7641       "                                   : aaaaaaaaaaaaaaaaaaaaaa\n"
7642       "                      : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
7643 
7644   FormatStyle NoBinPacking = getLLVMStyle();
7645   NoBinPacking.BinPackArguments = false;
7646   verifyFormat(
7647       "void f() {\n"
7648       "  g(aaa,\n"
7649       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
7650       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7651       "        ? aaaaaaaaaaaaaaa\n"
7652       "        : aaaaaaaaaaaaaaa);\n"
7653       "}",
7654       NoBinPacking);
7655   verifyFormat(
7656       "void f() {\n"
7657       "  g(aaa,\n"
7658       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
7659       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7660       "        ?: aaaaaaaaaaaaaaa);\n"
7661       "}",
7662       NoBinPacking);
7663 
7664   verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n"
7665                "             // comment.\n"
7666                "             ccccccccccccccccccccccccccccccccccccccc\n"
7667                "                 ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7668                "                 : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);");
7669 
7670   // Assignments in conditional expressions. Apparently not uncommon :-(.
7671   verifyFormat("return a != b\n"
7672                "           // comment\n"
7673                "           ? a = b\n"
7674                "           : a = b;");
7675   verifyFormat("return a != b\n"
7676                "           // comment\n"
7677                "           ? a = a != b\n"
7678                "                     // comment\n"
7679                "                     ? a = b\n"
7680                "                     : a\n"
7681                "           : a;\n");
7682   verifyFormat("return a != b\n"
7683                "           // comment\n"
7684                "           ? a\n"
7685                "           : a = a != b\n"
7686                "                     // comment\n"
7687                "                     ? a = b\n"
7688                "                     : a;");
7689 
7690   // Chained conditionals
7691   FormatStyle Style = getLLVMStyleWithColumns(70);
7692   Style.AlignOperands = FormatStyle::OAS_Align;
7693   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7694                "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7695                "                        : 3333333333333333;",
7696                Style);
7697   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7698                "       : bbbbbbbbbb     ? 2222222222222222\n"
7699                "                        : 3333333333333333;",
7700                Style);
7701   verifyFormat("return aaaaaaaaaa         ? 1111111111111111\n"
7702                "       : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
7703                "                          : 3333333333333333;",
7704                Style);
7705   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7706                "       : bbbbbbbbbbbbbb ? 222222\n"
7707                "                        : 333333;",
7708                Style);
7709   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7710                "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7711                "       : cccccccccccccc ? 3333333333333333\n"
7712                "                        : 4444444444444444;",
7713                Style);
7714   verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc)\n"
7715                "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7716                "                        : 3333333333333333;",
7717                Style);
7718   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7719                "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7720                "                        : (aaa ? bbb : ccc);",
7721                Style);
7722   verifyFormat(
7723       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7724       "                                             : cccccccccccccccccc)\n"
7725       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7726       "                        : 3333333333333333;",
7727       Style);
7728   verifyFormat(
7729       "return aaaaaaaaa        ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7730       "                                             : cccccccccccccccccc)\n"
7731       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7732       "                        : 3333333333333333;",
7733       Style);
7734   verifyFormat(
7735       "return aaaaaaaaa        ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7736       "                                             : dddddddddddddddddd)\n"
7737       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7738       "                        : 3333333333333333;",
7739       Style);
7740   verifyFormat(
7741       "return aaaaaaaaa        ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7742       "                                             : dddddddddddddddddd)\n"
7743       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7744       "                        : 3333333333333333;",
7745       Style);
7746   verifyFormat(
7747       "return aaaaaaaaa        ? 1111111111111111\n"
7748       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7749       "                        : a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7750       "                                             : dddddddddddddddddd)\n",
7751       Style);
7752   verifyFormat(
7753       "return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7754       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7755       "                        : (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7756       "                                             : cccccccccccccccccc);",
7757       Style);
7758   verifyFormat(
7759       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7760       "                           : ccccccccccccccc ? dddddddddddddddddd\n"
7761       "                                             : eeeeeeeeeeeeeeeeee)\n"
7762       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7763       "                        : 3333333333333333;",
7764       Style);
7765   verifyFormat(
7766       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa    ? bbbbbbbbbbbbbbbbbb\n"
7767       "                           : ccccccccccccccc ? dddddddddddddddddd\n"
7768       "                                             : eeeeeeeeeeeeeeeeee)\n"
7769       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7770       "                        : 3333333333333333;",
7771       Style);
7772   verifyFormat(
7773       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7774       "                           : cccccccccccc    ? dddddddddddddddddd\n"
7775       "                                             : eeeeeeeeeeeeeeeeee)\n"
7776       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7777       "                        : 3333333333333333;",
7778       Style);
7779   verifyFormat(
7780       "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7781       "                                             : cccccccccccccccccc\n"
7782       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7783       "                        : 3333333333333333;",
7784       Style);
7785   verifyFormat(
7786       "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7787       "                          : cccccccccccccccc ? dddddddddddddddddd\n"
7788       "                                             : eeeeeeeeeeeeeeeeee\n"
7789       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7790       "                        : 3333333333333333;",
7791       Style);
7792   verifyFormat("return aaaaaaaaaaaaaaaaaaaaa\n"
7793                "           ? (aaaaaaaaaaaaaaaaaa   ? bbbbbbbbbbbbbbbbbb\n"
7794                "              : cccccccccccccccccc ? dddddddddddddddddd\n"
7795                "                                   : eeeeeeeeeeeeeeeeee)\n"
7796                "       : bbbbbbbbbbbbbbbbbbb ? 2222222222222222\n"
7797                "                             : 3333333333333333;",
7798                Style);
7799   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaa\n"
7800                "           ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7801                "             : cccccccccccccccc ? dddddddddddddddddd\n"
7802                "                                : eeeeeeeeeeeeeeeeee\n"
7803                "       : bbbbbbbbbbbbbbbbbbbbbbb ? 2222222222222222\n"
7804                "                                 : 3333333333333333;",
7805                Style);
7806 
7807   Style.AlignOperands = FormatStyle::OAS_DontAlign;
7808   Style.BreakBeforeTernaryOperators = false;
7809   // FIXME: Aligning the question marks is weird given DontAlign.
7810   // Consider disabling this alignment in this case. Also check whether this
7811   // will render the adjustment from https://reviews.llvm.org/D82199
7812   // unnecessary.
7813   verifyFormat("int x = aaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa :\n"
7814                "    bbbb                ? cccccccccccccccccc :\n"
7815                "                          ddddd;\n",
7816                Style);
7817 
7818   EXPECT_EQ(
7819       "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n"
7820       "    /*\n"
7821       "     */\n"
7822       "    function() {\n"
7823       "      try {\n"
7824       "        return JJJJJJJJJJJJJJ(\n"
7825       "            pppppppppppppppppppppppppppppppppppppppppppppppppp);\n"
7826       "      }\n"
7827       "    } :\n"
7828       "    function() {};",
7829       format(
7830           "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n"
7831           "     /*\n"
7832           "      */\n"
7833           "     function() {\n"
7834           "      try {\n"
7835           "        return JJJJJJJJJJJJJJ(\n"
7836           "            pppppppppppppppppppppppppppppppppppppppppppppppppp);\n"
7837           "      }\n"
7838           "    } :\n"
7839           "    function() {};",
7840           getGoogleStyle(FormatStyle::LK_JavaScript)));
7841 }
7842 
7843 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
7844   FormatStyle Style = getLLVMStyleWithColumns(70);
7845   Style.BreakBeforeTernaryOperators = false;
7846   verifyFormat(
7847       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
7848       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
7849       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7850       Style);
7851   verifyFormat(
7852       "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
7853       "     aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
7854       "                                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7855       Style);
7856   verifyFormat(
7857       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
7858       "                                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7859       Style);
7860   verifyFormat("aaaa(aaaaaaaa, aaaaaaaaaa,\n"
7861                "     aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
7862                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7863                Style);
7864   verifyFormat(
7865       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
7866       "                                                      aaaaaaaaaaaaa);",
7867       Style);
7868   verifyFormat(
7869       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7870       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
7871       "                                      aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7872       "                   aaaaaaaaaaaaa);",
7873       Style);
7874   verifyFormat(
7875       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7876       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7877       "                   aaaaaaaaaaaaa);",
7878       Style);
7879   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
7880                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7881                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
7882                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7883                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7884                Style);
7885   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7886                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
7887                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7888                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
7889                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7890                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7891                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7892                Style);
7893   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7894                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n"
7895                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7896                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7897                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7898                Style);
7899   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
7900                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
7901                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
7902                Style);
7903   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
7904                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
7905                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
7906                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
7907                Style);
7908   verifyFormat(
7909       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
7910       "    aaaaaaaaaaaaaaa :\n"
7911       "    aaaaaaaaaaaaaaa;",
7912       Style);
7913   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
7914                "          aaaaaaaaa ?\n"
7915                "      b :\n"
7916                "      c);",
7917                Style);
7918   verifyFormat("unsigned Indent =\n"
7919                "    format(TheLine.First,\n"
7920                "           IndentForLevel[TheLine.Level] >= 0 ?\n"
7921                "               IndentForLevel[TheLine.Level] :\n"
7922                "               TheLine * 2,\n"
7923                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
7924                Style);
7925   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
7926                "                  aaaaaaaaaaaaaaa :\n"
7927                "                  bbbbbbbbbbbbbbb ? //\n"
7928                "                      ccccccccccccccc :\n"
7929                "                      ddddddddddddddd;",
7930                Style);
7931   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
7932                "                  aaaaaaaaaaaaaaa :\n"
7933                "                  (bbbbbbbbbbbbbbb ? //\n"
7934                "                       ccccccccccccccc :\n"
7935                "                       ddddddddddddddd);",
7936                Style);
7937   verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
7938                "            /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n"
7939                "            ccccccccccccccccccccccccccc;",
7940                Style);
7941   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
7942                "           aaaaa :\n"
7943                "           bbbbbbbbbbbbbbb + cccccccccccccccc;",
7944                Style);
7945 
7946   // Chained conditionals
7947   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
7948                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7949                "                          3333333333333333;",
7950                Style);
7951   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
7952                "       bbbbbbbbbb       ? 2222222222222222 :\n"
7953                "                          3333333333333333;",
7954                Style);
7955   verifyFormat("return aaaaaaaaaa       ? 1111111111111111 :\n"
7956                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7957                "                          3333333333333333;",
7958                Style);
7959   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
7960                "       bbbbbbbbbbbbbbbb ? 222222 :\n"
7961                "                          333333;",
7962                Style);
7963   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
7964                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7965                "       cccccccccccccccc ? 3333333333333333 :\n"
7966                "                          4444444444444444;",
7967                Style);
7968   verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc) :\n"
7969                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7970                "                          3333333333333333;",
7971                Style);
7972   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
7973                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7974                "                          (aaa ? bbb : ccc);",
7975                Style);
7976   verifyFormat(
7977       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7978       "                                               cccccccccccccccccc) :\n"
7979       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7980       "                          3333333333333333;",
7981       Style);
7982   verifyFormat(
7983       "return aaaaaaaaa        ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7984       "                                               cccccccccccccccccc) :\n"
7985       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7986       "                          3333333333333333;",
7987       Style);
7988   verifyFormat(
7989       "return aaaaaaaaa        ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7990       "                                               dddddddddddddddddd) :\n"
7991       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7992       "                          3333333333333333;",
7993       Style);
7994   verifyFormat(
7995       "return aaaaaaaaa        ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7996       "                                               dddddddddddddddddd) :\n"
7997       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7998       "                          3333333333333333;",
7999       Style);
8000   verifyFormat(
8001       "return aaaaaaaaa        ? 1111111111111111 :\n"
8002       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8003       "                          a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8004       "                                               dddddddddddddddddd)\n",
8005       Style);
8006   verifyFormat(
8007       "return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
8008       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8009       "                          (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8010       "                                               cccccccccccccccccc);",
8011       Style);
8012   verifyFormat(
8013       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8014       "                           ccccccccccccccccc ? dddddddddddddddddd :\n"
8015       "                                               eeeeeeeeeeeeeeeeee) :\n"
8016       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8017       "                          3333333333333333;",
8018       Style);
8019   verifyFormat(
8020       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8021       "                           ccccccccccccc     ? dddddddddddddddddd :\n"
8022       "                                               eeeeeeeeeeeeeeeeee) :\n"
8023       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8024       "                          3333333333333333;",
8025       Style);
8026   verifyFormat(
8027       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaa     ? bbbbbbbbbbbbbbbbbb :\n"
8028       "                           ccccccccccccccccc ? dddddddddddddddddd :\n"
8029       "                                               eeeeeeeeeeeeeeeeee) :\n"
8030       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8031       "                          3333333333333333;",
8032       Style);
8033   verifyFormat(
8034       "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8035       "                                               cccccccccccccccccc :\n"
8036       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8037       "                          3333333333333333;",
8038       Style);
8039   verifyFormat(
8040       "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8041       "                          cccccccccccccccccc ? dddddddddddddddddd :\n"
8042       "                                               eeeeeeeeeeeeeeeeee :\n"
8043       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8044       "                          3333333333333333;",
8045       Style);
8046   verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n"
8047                "           (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8048                "            cccccccccccccccccc ? dddddddddddddddddd :\n"
8049                "                                 eeeeeeeeeeeeeeeeee) :\n"
8050                "       bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8051                "                               3333333333333333;",
8052                Style);
8053   verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n"
8054                "           aaaaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8055                "           cccccccccccccccccccc ? dddddddddddddddddd :\n"
8056                "                                  eeeeeeeeeeeeeeeeee :\n"
8057                "       bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8058                "                               3333333333333333;",
8059                Style);
8060 }
8061 
8062 TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
8063   verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
8064                "     aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
8065   verifyFormat("bool a = true, b = false;");
8066 
8067   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
8068                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
8069                "     bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
8070                "         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
8071   verifyFormat(
8072       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
8073       "         bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
8074       "     d = e && f;");
8075   verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
8076                "          c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
8077   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
8078                "          *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
8079   verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
8080                "          ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
8081 
8082   FormatStyle Style = getGoogleStyle();
8083   Style.PointerAlignment = FormatStyle::PAS_Left;
8084   Style.DerivePointerAlignment = false;
8085   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8086                "    *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
8087                "    *b = bbbbbbbbbbbbbbbbbbb;",
8088                Style);
8089   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
8090                "          *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;",
8091                Style);
8092   verifyFormat("vector<int*> a, b;", Style);
8093   verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style);
8094 }
8095 
8096 TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
8097   verifyFormat("arr[foo ? bar : baz];");
8098   verifyFormat("f()[foo ? bar : baz];");
8099   verifyFormat("(a + b)[foo ? bar : baz];");
8100   verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
8101 }
8102 
8103 TEST_F(FormatTest, AlignsStringLiterals) {
8104   verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
8105                "                                      \"short literal\");");
8106   verifyFormat(
8107       "looooooooooooooooooooooooongFunction(\n"
8108       "    \"short literal\"\n"
8109       "    \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
8110   verifyFormat("someFunction(\"Always break between multi-line\"\n"
8111                "             \" string literals\",\n"
8112                "             and, other, parameters);");
8113   EXPECT_EQ("fun + \"1243\" /* comment */\n"
8114             "      \"5678\";",
8115             format("fun + \"1243\" /* comment */\n"
8116                    "    \"5678\";",
8117                    getLLVMStyleWithColumns(28)));
8118   EXPECT_EQ(
8119       "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
8120       "         \"aaaaaaaaaaaaaaaaaaaaa\"\n"
8121       "         \"aaaaaaaaaaaaaaaa\";",
8122       format("aaaaaa ="
8123              "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
8124              "aaaaaaaaaaaaaaaaaaaaa\" "
8125              "\"aaaaaaaaaaaaaaaa\";"));
8126   verifyFormat("a = a + \"a\"\n"
8127                "        \"a\"\n"
8128                "        \"a\";");
8129   verifyFormat("f(\"a\", \"b\"\n"
8130                "       \"c\");");
8131 
8132   verifyFormat(
8133       "#define LL_FORMAT \"ll\"\n"
8134       "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
8135       "       \"d, ddddddddd: %\" LL_FORMAT \"d\");");
8136 
8137   verifyFormat("#define A(X)          \\\n"
8138                "  \"aaaaa\" #X \"bbbbbb\" \\\n"
8139                "  \"ccccc\"",
8140                getLLVMStyleWithColumns(23));
8141   verifyFormat("#define A \"def\"\n"
8142                "f(\"abc\" A \"ghi\"\n"
8143                "  \"jkl\");");
8144 
8145   verifyFormat("f(L\"a\"\n"
8146                "  L\"b\");");
8147   verifyFormat("#define A(X)            \\\n"
8148                "  L\"aaaaa\" #X L\"bbbbbb\" \\\n"
8149                "  L\"ccccc\"",
8150                getLLVMStyleWithColumns(25));
8151 
8152   verifyFormat("f(@\"a\"\n"
8153                "  @\"b\");");
8154   verifyFormat("NSString s = @\"a\"\n"
8155                "             @\"b\"\n"
8156                "             @\"c\";");
8157   verifyFormat("NSString s = @\"a\"\n"
8158                "              \"b\"\n"
8159                "              \"c\";");
8160 }
8161 
8162 TEST_F(FormatTest, ReturnTypeBreakingStyle) {
8163   FormatStyle Style = getLLVMStyle();
8164   // No declarations or definitions should be moved to own line.
8165   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None;
8166   verifyFormat("class A {\n"
8167                "  int f() { return 1; }\n"
8168                "  int g();\n"
8169                "};\n"
8170                "int f() { return 1; }\n"
8171                "int g();\n",
8172                Style);
8173 
8174   // All declarations and definitions should have the return type moved to its
8175   // own line.
8176   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
8177   Style.TypenameMacros = {"LIST"};
8178   verifyFormat("SomeType\n"
8179                "funcdecl(LIST(uint64_t));",
8180                Style);
8181   verifyFormat("class E {\n"
8182                "  int\n"
8183                "  f() {\n"
8184                "    return 1;\n"
8185                "  }\n"
8186                "  int\n"
8187                "  g();\n"
8188                "};\n"
8189                "int\n"
8190                "f() {\n"
8191                "  return 1;\n"
8192                "}\n"
8193                "int\n"
8194                "g();\n",
8195                Style);
8196 
8197   // Top-level definitions, and no kinds of declarations should have the
8198   // return type moved to its own line.
8199   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions;
8200   verifyFormat("class B {\n"
8201                "  int f() { return 1; }\n"
8202                "  int g();\n"
8203                "};\n"
8204                "int\n"
8205                "f() {\n"
8206                "  return 1;\n"
8207                "}\n"
8208                "int g();\n",
8209                Style);
8210 
8211   // Top-level definitions and declarations should have the return type moved
8212   // to its own line.
8213   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel;
8214   verifyFormat("class C {\n"
8215                "  int f() { return 1; }\n"
8216                "  int g();\n"
8217                "};\n"
8218                "int\n"
8219                "f() {\n"
8220                "  return 1;\n"
8221                "}\n"
8222                "int\n"
8223                "g();\n",
8224                Style);
8225 
8226   // All definitions should have the return type moved to its own line, but no
8227   // kinds of declarations.
8228   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
8229   verifyFormat("class D {\n"
8230                "  int\n"
8231                "  f() {\n"
8232                "    return 1;\n"
8233                "  }\n"
8234                "  int g();\n"
8235                "};\n"
8236                "int\n"
8237                "f() {\n"
8238                "  return 1;\n"
8239                "}\n"
8240                "int g();\n",
8241                Style);
8242   verifyFormat("const char *\n"
8243                "f(void) {\n" // Break here.
8244                "  return \"\";\n"
8245                "}\n"
8246                "const char *bar(void);\n", // No break here.
8247                Style);
8248   verifyFormat("template <class T>\n"
8249                "T *\n"
8250                "f(T &c) {\n" // Break here.
8251                "  return NULL;\n"
8252                "}\n"
8253                "template <class T> T *f(T &c);\n", // No break here.
8254                Style);
8255   verifyFormat("class C {\n"
8256                "  int\n"
8257                "  operator+() {\n"
8258                "    return 1;\n"
8259                "  }\n"
8260                "  int\n"
8261                "  operator()() {\n"
8262                "    return 1;\n"
8263                "  }\n"
8264                "};\n",
8265                Style);
8266   verifyFormat("void\n"
8267                "A::operator()() {}\n"
8268                "void\n"
8269                "A::operator>>() {}\n"
8270                "void\n"
8271                "A::operator+() {}\n"
8272                "void\n"
8273                "A::operator*() {}\n"
8274                "void\n"
8275                "A::operator->() {}\n"
8276                "void\n"
8277                "A::operator void *() {}\n"
8278                "void\n"
8279                "A::operator void &() {}\n"
8280                "void\n"
8281                "A::operator void &&() {}\n"
8282                "void\n"
8283                "A::operator char *() {}\n"
8284                "void\n"
8285                "A::operator[]() {}\n"
8286                "void\n"
8287                "A::operator!() {}\n"
8288                "void\n"
8289                "A::operator**() {}\n"
8290                "void\n"
8291                "A::operator<Foo> *() {}\n"
8292                "void\n"
8293                "A::operator<Foo> **() {}\n"
8294                "void\n"
8295                "A::operator<Foo> &() {}\n"
8296                "void\n"
8297                "A::operator void **() {}\n",
8298                Style);
8299   verifyFormat("constexpr auto\n"
8300                "operator()() const -> reference {}\n"
8301                "constexpr auto\n"
8302                "operator>>() const -> reference {}\n"
8303                "constexpr auto\n"
8304                "operator+() const -> reference {}\n"
8305                "constexpr auto\n"
8306                "operator*() const -> reference {}\n"
8307                "constexpr auto\n"
8308                "operator->() const -> reference {}\n"
8309                "constexpr auto\n"
8310                "operator++() const -> reference {}\n"
8311                "constexpr auto\n"
8312                "operator void *() const -> reference {}\n"
8313                "constexpr auto\n"
8314                "operator void **() const -> reference {}\n"
8315                "constexpr auto\n"
8316                "operator void *() const -> reference {}\n"
8317                "constexpr auto\n"
8318                "operator void &() const -> reference {}\n"
8319                "constexpr auto\n"
8320                "operator void &&() const -> reference {}\n"
8321                "constexpr auto\n"
8322                "operator char *() const -> reference {}\n"
8323                "constexpr auto\n"
8324                "operator!() const -> reference {}\n"
8325                "constexpr auto\n"
8326                "operator[]() const -> reference {}\n",
8327                Style);
8328   verifyFormat("void *operator new(std::size_t s);", // No break here.
8329                Style);
8330   verifyFormat("void *\n"
8331                "operator new(std::size_t s) {}",
8332                Style);
8333   verifyFormat("void *\n"
8334                "operator delete[](void *ptr) {}",
8335                Style);
8336   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
8337   verifyFormat("const char *\n"
8338                "f(void)\n" // Break here.
8339                "{\n"
8340                "  return \"\";\n"
8341                "}\n"
8342                "const char *bar(void);\n", // No break here.
8343                Style);
8344   verifyFormat("template <class T>\n"
8345                "T *\n"     // Problem here: no line break
8346                "f(T &c)\n" // Break here.
8347                "{\n"
8348                "  return NULL;\n"
8349                "}\n"
8350                "template <class T> T *f(T &c);\n", // No break here.
8351                Style);
8352   verifyFormat("int\n"
8353                "foo(A<bool> a)\n"
8354                "{\n"
8355                "  return a;\n"
8356                "}\n",
8357                Style);
8358   verifyFormat("int\n"
8359                "foo(A<8> a)\n"
8360                "{\n"
8361                "  return a;\n"
8362                "}\n",
8363                Style);
8364   verifyFormat("int\n"
8365                "foo(A<B<bool>, 8> a)\n"
8366                "{\n"
8367                "  return a;\n"
8368                "}\n",
8369                Style);
8370   verifyFormat("int\n"
8371                "foo(A<B<8>, bool> a)\n"
8372                "{\n"
8373                "  return a;\n"
8374                "}\n",
8375                Style);
8376   verifyFormat("int\n"
8377                "foo(A<B<bool>, bool> a)\n"
8378                "{\n"
8379                "  return a;\n"
8380                "}\n",
8381                Style);
8382   verifyFormat("int\n"
8383                "foo(A<B<8>, 8> a)\n"
8384                "{\n"
8385                "  return a;\n"
8386                "}\n",
8387                Style);
8388 
8389   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
8390   Style.BraceWrapping.AfterFunction = true;
8391   verifyFormat("int f(i);\n" // No break here.
8392                "int\n"       // Break here.
8393                "f(i)\n"
8394                "{\n"
8395                "  return i + 1;\n"
8396                "}\n"
8397                "int\n" // Break here.
8398                "f(i)\n"
8399                "{\n"
8400                "  return i + 1;\n"
8401                "};",
8402                Style);
8403   verifyFormat("int f(a, b, c);\n" // No break here.
8404                "int\n"             // Break here.
8405                "f(a, b, c)\n"      // Break here.
8406                "short a, b;\n"
8407                "float c;\n"
8408                "{\n"
8409                "  return a + b < c;\n"
8410                "}\n"
8411                "int\n"        // Break here.
8412                "f(a, b, c)\n" // Break here.
8413                "short a, b;\n"
8414                "float c;\n"
8415                "{\n"
8416                "  return a + b < c;\n"
8417                "};",
8418                Style);
8419   verifyFormat("byte *\n" // Break here.
8420                "f(a)\n"   // Break here.
8421                "byte a[];\n"
8422                "{\n"
8423                "  return a;\n"
8424                "}",
8425                Style);
8426   verifyFormat("bool f(int a, int) override;\n"
8427                "Bar g(int a, Bar) final;\n"
8428                "Bar h(a, Bar) final;",
8429                Style);
8430   verifyFormat("int\n"
8431                "f(a)",
8432                Style);
8433   verifyFormat("bool\n"
8434                "f(size_t = 0, bool b = false)\n"
8435                "{\n"
8436                "  return !b;\n"
8437                "}",
8438                Style);
8439 
8440   // The return breaking style doesn't affect:
8441   // * function and object definitions with attribute-like macros
8442   verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n"
8443                "    ABSL_GUARDED_BY(mutex) = {};",
8444                getGoogleStyleWithColumns(40));
8445   verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n"
8446                "    ABSL_GUARDED_BY(mutex);  // comment",
8447                getGoogleStyleWithColumns(40));
8448   verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n"
8449                "    ABSL_GUARDED_BY(mutex1)\n"
8450                "        ABSL_GUARDED_BY(mutex2);",
8451                getGoogleStyleWithColumns(40));
8452   verifyFormat("Tttttt f(int a, int b)\n"
8453                "    ABSL_GUARDED_BY(mutex1)\n"
8454                "        ABSL_GUARDED_BY(mutex2);",
8455                getGoogleStyleWithColumns(40));
8456   // * typedefs
8457   verifyFormat("typedef ATTR(X) char x;", getGoogleStyle());
8458 
8459   Style = getGNUStyle();
8460 
8461   // Test for comments at the end of function declarations.
8462   verifyFormat("void\n"
8463                "foo (int a, /*abc*/ int b) // def\n"
8464                "{\n"
8465                "}\n",
8466                Style);
8467 
8468   verifyFormat("void\n"
8469                "foo (int a, /* abc */ int b) /* def */\n"
8470                "{\n"
8471                "}\n",
8472                Style);
8473 
8474   // Definitions that should not break after return type
8475   verifyFormat("void foo (int a, int b); // def\n", Style);
8476   verifyFormat("void foo (int a, int b); /* def */\n", Style);
8477   verifyFormat("void foo (int a, int b);\n", Style);
8478 }
8479 
8480 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
8481   FormatStyle NoBreak = getLLVMStyle();
8482   NoBreak.AlwaysBreakBeforeMultilineStrings = false;
8483   FormatStyle Break = getLLVMStyle();
8484   Break.AlwaysBreakBeforeMultilineStrings = true;
8485   verifyFormat("aaaa = \"bbbb\"\n"
8486                "       \"cccc\";",
8487                NoBreak);
8488   verifyFormat("aaaa =\n"
8489                "    \"bbbb\"\n"
8490                "    \"cccc\";",
8491                Break);
8492   verifyFormat("aaaa(\"bbbb\"\n"
8493                "     \"cccc\");",
8494                NoBreak);
8495   verifyFormat("aaaa(\n"
8496                "    \"bbbb\"\n"
8497                "    \"cccc\");",
8498                Break);
8499   verifyFormat("aaaa(qqq, \"bbbb\"\n"
8500                "          \"cccc\");",
8501                NoBreak);
8502   verifyFormat("aaaa(qqq,\n"
8503                "     \"bbbb\"\n"
8504                "     \"cccc\");",
8505                Break);
8506   verifyFormat("aaaa(qqq,\n"
8507                "     L\"bbbb\"\n"
8508                "     L\"cccc\");",
8509                Break);
8510   verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n"
8511                "                      \"bbbb\"));",
8512                Break);
8513   verifyFormat("string s = someFunction(\n"
8514                "    \"abc\"\n"
8515                "    \"abc\");",
8516                Break);
8517 
8518   // As we break before unary operators, breaking right after them is bad.
8519   verifyFormat("string foo = abc ? \"x\"\n"
8520                "                   \"blah blah blah blah blah blah\"\n"
8521                "                 : \"y\";",
8522                Break);
8523 
8524   // Don't break if there is no column gain.
8525   verifyFormat("f(\"aaaa\"\n"
8526                "  \"bbbb\");",
8527                Break);
8528 
8529   // Treat literals with escaped newlines like multi-line string literals.
8530   EXPECT_EQ("x = \"a\\\n"
8531             "b\\\n"
8532             "c\";",
8533             format("x = \"a\\\n"
8534                    "b\\\n"
8535                    "c\";",
8536                    NoBreak));
8537   EXPECT_EQ("xxxx =\n"
8538             "    \"a\\\n"
8539             "b\\\n"
8540             "c\";",
8541             format("xxxx = \"a\\\n"
8542                    "b\\\n"
8543                    "c\";",
8544                    Break));
8545 
8546   EXPECT_EQ("NSString *const kString =\n"
8547             "    @\"aaaa\"\n"
8548             "    @\"bbbb\";",
8549             format("NSString *const kString = @\"aaaa\"\n"
8550                    "@\"bbbb\";",
8551                    Break));
8552 
8553   Break.ColumnLimit = 0;
8554   verifyFormat("const char *hello = \"hello llvm\";", Break);
8555 }
8556 
8557 TEST_F(FormatTest, AlignsPipes) {
8558   verifyFormat(
8559       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8560       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8561       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8562   verifyFormat(
8563       "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
8564       "                     << aaaaaaaaaaaaaaaaaaaa;");
8565   verifyFormat(
8566       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8567       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8568   verifyFormat(
8569       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
8570       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8571   verifyFormat(
8572       "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
8573       "                \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
8574       "             << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
8575   verifyFormat(
8576       "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8577       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8578       "         << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8579   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8580                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8581                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8582                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
8583   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n"
8584                "             << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);");
8585   verifyFormat(
8586       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8587       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8588   verifyFormat(
8589       "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n"
8590       "                                       aaaaaaaaaaaaaaaaaaaaaaaaaa);");
8591 
8592   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
8593                "             << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
8594   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8595                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8596                "                    aaaaaaaaaaaaaaaaaaaaa)\n"
8597                "             << aaaaaaaaaaaaaaaaaaaaaaaaaa;");
8598   verifyFormat("LOG_IF(aaa == //\n"
8599                "       bbb)\n"
8600                "    << a << b;");
8601 
8602   // But sometimes, breaking before the first "<<" is desirable.
8603   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
8604                "    << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
8605   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
8606                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8607                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8608   verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
8609                "    << BEF << IsTemplate << Description << E->getType();");
8610   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
8611                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8612                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8613   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
8614                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8615                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8616                "    << aaa;");
8617 
8618   verifyFormat(
8619       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8620       "                    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
8621 
8622   // Incomplete string literal.
8623   EXPECT_EQ("llvm::errs() << \"\n"
8624             "             << a;",
8625             format("llvm::errs() << \"\n<<a;"));
8626 
8627   verifyFormat("void f() {\n"
8628                "  CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
8629                "      << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
8630                "}");
8631 
8632   // Handle 'endl'.
8633   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n"
8634                "             << bbbbbbbbbbbbbbbbbbbbbb << endl;");
8635   verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;");
8636 
8637   // Handle '\n'.
8638   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n"
8639                "             << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
8640   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n"
8641                "             << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';");
8642   verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n"
8643                "             << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";");
8644   verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
8645 }
8646 
8647 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) {
8648   verifyFormat("return out << \"somepacket = {\\n\"\n"
8649                "           << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
8650                "           << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
8651                "           << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
8652                "           << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
8653                "           << \"}\";");
8654 
8655   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
8656                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
8657                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
8658   verifyFormat(
8659       "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
8660       "             << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
8661       "             << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
8662       "             << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
8663       "             << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
8664   verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
8665                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
8666   verifyFormat(
8667       "void f() {\n"
8668       "  llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
8669       "               << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
8670       "}");
8671 
8672   // Breaking before the first "<<" is generally not desirable.
8673   verifyFormat(
8674       "llvm::errs()\n"
8675       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8676       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8677       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8678       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
8679       getLLVMStyleWithColumns(70));
8680   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
8681                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8682                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
8683                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8684                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
8685                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
8686                getLLVMStyleWithColumns(70));
8687 
8688   verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
8689                "           \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
8690                "           \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;");
8691   verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
8692                "                  \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
8693                "                  \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);");
8694   verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n"
8695                "           (aaaa + aaaa);",
8696                getLLVMStyleWithColumns(40));
8697   verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n"
8698                "                  (aaaaaaa + aaaaa));",
8699                getLLVMStyleWithColumns(40));
8700   verifyFormat(
8701       "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n"
8702       "                  SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n"
8703       "                  bbbbbbbbbbbbbbbbbbbbbbb);");
8704 }
8705 
8706 TEST_F(FormatTest, UnderstandsEquals) {
8707   verifyFormat(
8708       "aaaaaaaaaaaaaaaaa =\n"
8709       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8710   verifyFormat(
8711       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
8712       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
8713   verifyFormat(
8714       "if (a) {\n"
8715       "  f();\n"
8716       "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
8717       "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
8718       "}");
8719 
8720   verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
8721                "        100000000 + 10000000) {\n}");
8722 }
8723 
8724 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
8725   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
8726                "    .looooooooooooooooooooooooooooooooooooooongFunction();");
8727 
8728   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
8729                "    ->looooooooooooooooooooooooooooooooooooooongFunction();");
8730 
8731   verifyFormat(
8732       "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
8733       "                                                          Parameter2);");
8734 
8735   verifyFormat(
8736       "ShortObject->shortFunction(\n"
8737       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
8738       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
8739 
8740   verifyFormat("loooooooooooooongFunction(\n"
8741                "    LoooooooooooooongObject->looooooooooooooooongFunction());");
8742 
8743   verifyFormat(
8744       "function(LoooooooooooooooooooooooooooooooooooongObject\n"
8745       "             ->loooooooooooooooooooooooooooooooooooooooongFunction());");
8746 
8747   verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
8748                "    .WillRepeatedly(Return(SomeValue));");
8749   verifyFormat("void f() {\n"
8750                "  EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
8751                "      .Times(2)\n"
8752                "      .WillRepeatedly(Return(SomeValue));\n"
8753                "}");
8754   verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
8755                "    ccccccccccccccccccccccc);");
8756   verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8757                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8758                "          .aaaaa(aaaaa),\n"
8759                "      aaaaaaaaaaaaaaaaaaaaa);");
8760   verifyFormat("void f() {\n"
8761                "  aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8762                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
8763                "}");
8764   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8765                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8766                "    .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8767                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8768                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
8769   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8770                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8771                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8772                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
8773                "}");
8774 
8775   // Here, it is not necessary to wrap at "." or "->".
8776   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
8777                "    aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
8778   verifyFormat(
8779       "aaaaaaaaaaa->aaaaaaaaa(\n"
8780       "    aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8781       "    aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
8782 
8783   verifyFormat(
8784       "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8785       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
8786   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
8787                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
8788   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
8789                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
8790 
8791   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8792                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8793                "    .a();");
8794 
8795   FormatStyle NoBinPacking = getLLVMStyle();
8796   NoBinPacking.BinPackParameters = false;
8797   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
8798                "    .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
8799                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
8800                "                         aaaaaaaaaaaaaaaaaaa,\n"
8801                "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
8802                NoBinPacking);
8803 
8804   // If there is a subsequent call, change to hanging indentation.
8805   verifyFormat(
8806       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8807       "                         aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
8808       "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
8809   verifyFormat(
8810       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8811       "    aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
8812   verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8813                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8814                "                 .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
8815   verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8816                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8817                "               .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
8818 }
8819 
8820 TEST_F(FormatTest, WrapsTemplateDeclarations) {
8821   verifyFormat("template <typename T>\n"
8822                "virtual void loooooooooooongFunction(int Param1, int Param2);");
8823   verifyFormat("template <typename T>\n"
8824                "// T should be one of {A, B}.\n"
8825                "virtual void loooooooooooongFunction(int Param1, int Param2);");
8826   verifyFormat(
8827       "template <typename T>\n"
8828       "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
8829   verifyFormat("template <typename T>\n"
8830                "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
8831                "       int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
8832   verifyFormat(
8833       "template <typename T>\n"
8834       "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
8835       "                                      int Paaaaaaaaaaaaaaaaaaaaram2);");
8836   verifyFormat(
8837       "template <typename T>\n"
8838       "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
8839       "                    aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
8840       "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8841   verifyFormat("template <typename T>\n"
8842                "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8843                "    int aaaaaaaaaaaaaaaaaaaaaa);");
8844   verifyFormat(
8845       "template <typename T1, typename T2 = char, typename T3 = char,\n"
8846       "          typename T4 = char>\n"
8847       "void f();");
8848   verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
8849                "          template <typename> class cccccccccccccccccccccc,\n"
8850                "          typename ddddddddddddd>\n"
8851                "class C {};");
8852   verifyFormat(
8853       "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
8854       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8855 
8856   verifyFormat("void f() {\n"
8857                "  a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
8858                "      a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
8859                "}");
8860 
8861   verifyFormat("template <typename T> class C {};");
8862   verifyFormat("template <typename T> void f();");
8863   verifyFormat("template <typename T> void f() {}");
8864   verifyFormat(
8865       "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
8866       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8867       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
8868       "    new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
8869       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8870       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
8871       "        bbbbbbbbbbbbbbbbbbbbbbbb);",
8872       getLLVMStyleWithColumns(72));
8873   EXPECT_EQ("static_cast<A< //\n"
8874             "    B> *>(\n"
8875             "\n"
8876             ");",
8877             format("static_cast<A<//\n"
8878                    "    B>*>(\n"
8879                    "\n"
8880                    "    );"));
8881   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8882                "    const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
8883 
8884   FormatStyle AlwaysBreak = getLLVMStyle();
8885   AlwaysBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
8886   verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
8887   verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
8888   verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
8889   verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8890                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
8891                "    ccccccccccccccccccccccccccccccccccccccccccccccc);");
8892   verifyFormat("template <template <typename> class Fooooooo,\n"
8893                "          template <typename> class Baaaaaaar>\n"
8894                "struct C {};",
8895                AlwaysBreak);
8896   verifyFormat("template <typename T> // T can be A, B or C.\n"
8897                "struct C {};",
8898                AlwaysBreak);
8899   verifyFormat("template <enum E> class A {\n"
8900                "public:\n"
8901                "  E *f();\n"
8902                "};");
8903 
8904   FormatStyle NeverBreak = getLLVMStyle();
8905   NeverBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_No;
8906   verifyFormat("template <typename T> class C {};", NeverBreak);
8907   verifyFormat("template <typename T> void f();", NeverBreak);
8908   verifyFormat("template <typename T> void f() {}", NeverBreak);
8909   verifyFormat("template <typename T>\nvoid foo(aaaaaaaaaaaaaaaaaaaaaaaaaa "
8910                "bbbbbbbbbbbbbbbbbbbb) {}",
8911                NeverBreak);
8912   verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8913                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
8914                "    ccccccccccccccccccccccccccccccccccccccccccccccc);",
8915                NeverBreak);
8916   verifyFormat("template <template <typename> class Fooooooo,\n"
8917                "          template <typename> class Baaaaaaar>\n"
8918                "struct C {};",
8919                NeverBreak);
8920   verifyFormat("template <typename T> // T can be A, B or C.\n"
8921                "struct C {};",
8922                NeverBreak);
8923   verifyFormat("template <enum E> class A {\n"
8924                "public:\n"
8925                "  E *f();\n"
8926                "};",
8927                NeverBreak);
8928   NeverBreak.PenaltyBreakTemplateDeclaration = 100;
8929   verifyFormat("template <typename T> void\nfoo(aaaaaaaaaaaaaaaaaaaaaaaaaa "
8930                "bbbbbbbbbbbbbbbbbbbb) {}",
8931                NeverBreak);
8932 }
8933 
8934 TEST_F(FormatTest, WrapsTemplateDeclarationsWithComments) {
8935   FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp);
8936   Style.ColumnLimit = 60;
8937   EXPECT_EQ("// Baseline - no comments.\n"
8938             "template <\n"
8939             "    typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n"
8940             "void f() {}",
8941             format("// Baseline - no comments.\n"
8942                    "template <\n"
8943                    "    typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n"
8944                    "void f() {}",
8945                    Style));
8946 
8947   EXPECT_EQ("template <\n"
8948             "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value>  // trailing\n"
8949             "void f() {}",
8950             format("template <\n"
8951                    "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
8952                    "void f() {}",
8953                    Style));
8954 
8955   EXPECT_EQ(
8956       "template <\n"
8957       "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n"
8958       "void f() {}",
8959       format("template <typename aaaaaaaaaa<bbbbbbbbbbbb>::value>  /* line */\n"
8960              "void f() {}",
8961              Style));
8962 
8963   EXPECT_EQ(
8964       "template <\n"
8965       "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value>  // trailing\n"
8966       "                                               // multiline\n"
8967       "void f() {}",
8968       format("template <\n"
8969              "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
8970              "                                              // multiline\n"
8971              "void f() {}",
8972              Style));
8973 
8974   EXPECT_EQ(
8975       "template <typename aaaaaaaaaa<\n"
8976       "    bbbbbbbbbbbb>::value>  // trailing loooong\n"
8977       "void f() {}",
8978       format(
8979           "template <\n"
8980           "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing loooong\n"
8981           "void f() {}",
8982           Style));
8983 }
8984 
8985 TEST_F(FormatTest, WrapsTemplateParameters) {
8986   FormatStyle Style = getLLVMStyle();
8987   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
8988   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
8989   verifyFormat(
8990       "template <typename... a> struct q {};\n"
8991       "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
8992       "    aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
8993       "    y;",
8994       Style);
8995   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
8996   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
8997   verifyFormat(
8998       "template <typename... a> struct r {};\n"
8999       "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
9000       "    aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
9001       "    y;",
9002       Style);
9003   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
9004   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
9005   verifyFormat("template <typename... a> struct s {};\n"
9006                "extern s<\n"
9007                "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
9008                "aaaaaaaaaaaaaaaaaaaaaa,\n"
9009                "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
9010                "aaaaaaaaaaaaaaaaaaaaaa>\n"
9011                "    y;",
9012                Style);
9013   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
9014   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
9015   verifyFormat("template <typename... a> struct t {};\n"
9016                "extern t<\n"
9017                "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
9018                "aaaaaaaaaaaaaaaaaaaaaa,\n"
9019                "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
9020                "aaaaaaaaaaaaaaaaaaaaaa>\n"
9021                "    y;",
9022                Style);
9023 }
9024 
9025 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
9026   verifyFormat(
9027       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
9028       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
9029   verifyFormat(
9030       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
9031       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9032       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
9033 
9034   // FIXME: Should we have the extra indent after the second break?
9035   verifyFormat(
9036       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
9037       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
9038       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
9039 
9040   verifyFormat(
9041       "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
9042       "                    cccccccccccccccccccccccccccccccccccccccccccccc());");
9043 
9044   // Breaking at nested name specifiers is generally not desirable.
9045   verifyFormat(
9046       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9047       "    aaaaaaaaaaaaaaaaaaaaaaa);");
9048 
9049   verifyFormat("aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n"
9050                "                   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
9051                "                       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9052                "                   aaaaaaaaaaaaaaaaaaaaa);",
9053                getLLVMStyleWithColumns(74));
9054 
9055   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
9056                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9057                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
9058 }
9059 
9060 TEST_F(FormatTest, UnderstandsTemplateParameters) {
9061   verifyFormat("A<int> a;");
9062   verifyFormat("A<A<A<int>>> a;");
9063   verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
9064   verifyFormat("bool x = a < 1 || 2 > a;");
9065   verifyFormat("bool x = 5 < f<int>();");
9066   verifyFormat("bool x = f<int>() > 5;");
9067   verifyFormat("bool x = 5 < a<int>::x;");
9068   verifyFormat("bool x = a < 4 ? a > 2 : false;");
9069   verifyFormat("bool x = f() ? a < 2 : a > 2;");
9070 
9071   verifyGoogleFormat("A<A<int>> a;");
9072   verifyGoogleFormat("A<A<A<int>>> a;");
9073   verifyGoogleFormat("A<A<A<A<int>>>> a;");
9074   verifyGoogleFormat("A<A<int> > a;");
9075   verifyGoogleFormat("A<A<A<int> > > a;");
9076   verifyGoogleFormat("A<A<A<A<int> > > > a;");
9077   verifyGoogleFormat("A<::A<int>> a;");
9078   verifyGoogleFormat("A<::A> a;");
9079   verifyGoogleFormat("A< ::A> a;");
9080   verifyGoogleFormat("A< ::A<int> > a;");
9081   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
9082   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
9083   EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle()));
9084   EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle()));
9085   EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };",
9086             format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle()));
9087 
9088   verifyFormat("A<A<int>> a;", getChromiumStyle(FormatStyle::LK_Cpp));
9089 
9090   // template closer followed by a token that starts with > or =
9091   verifyFormat("bool b = a<1> > 1;");
9092   verifyFormat("bool b = a<1> >= 1;");
9093   verifyFormat("int i = a<1> >> 1;");
9094   FormatStyle Style = getLLVMStyle();
9095   Style.SpaceBeforeAssignmentOperators = false;
9096   verifyFormat("bool b= a<1> == 1;", Style);
9097   verifyFormat("a<int> = 1;", Style);
9098   verifyFormat("a<int> >>= 1;", Style);
9099 
9100   verifyFormat("test < a | b >> c;");
9101   verifyFormat("test<test<a | b>> c;");
9102   verifyFormat("test >> a >> b;");
9103   verifyFormat("test << a >> b;");
9104 
9105   verifyFormat("f<int>();");
9106   verifyFormat("template <typename T> void f() {}");
9107   verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
9108   verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : "
9109                "sizeof(char)>::type>;");
9110   verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};");
9111   verifyFormat("f(a.operator()<A>());");
9112   verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9113                "      .template operator()<A>());",
9114                getLLVMStyleWithColumns(35));
9115 
9116   // Not template parameters.
9117   verifyFormat("return a < b && c > d;");
9118   verifyFormat("void f() {\n"
9119                "  while (a < b && c > d) {\n"
9120                "  }\n"
9121                "}");
9122   verifyFormat("template <typename... Types>\n"
9123                "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
9124 
9125   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9126                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
9127                getLLVMStyleWithColumns(60));
9128   verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
9129   verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
9130   verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <");
9131   verifyFormat("some_templated_type<decltype([](int i) { return i; })>");
9132 }
9133 
9134 TEST_F(FormatTest, UnderstandsShiftOperators) {
9135   verifyFormat("if (i < x >> 1)");
9136   verifyFormat("while (i < x >> 1)");
9137   verifyFormat("for (unsigned i = 0; i < i; ++i, v = v >> 1)");
9138   verifyFormat("for (unsigned i = 0; i < x >> 1; ++i, v = v >> 1)");
9139   verifyFormat(
9140       "for (std::vector<int>::iterator i = 0; i < x >> 1; ++i, v = v >> 1)");
9141   verifyFormat("Foo.call<Bar<Function>>()");
9142   verifyFormat("if (Foo.call<Bar<Function>>() == 0)");
9143   verifyFormat("for (std::vector<std::pair<int>>::iterator i = 0; i < x >> 1; "
9144                "++i, v = v >> 1)");
9145   verifyFormat("if (w<u<v<x>>, 1>::t)");
9146 }
9147 
9148 TEST_F(FormatTest, BitshiftOperatorWidth) {
9149   EXPECT_EQ("int a = 1 << 2; /* foo\n"
9150             "                   bar */",
9151             format("int    a=1<<2;  /* foo\n"
9152                    "                   bar */"));
9153 
9154   EXPECT_EQ("int b = 256 >> 1; /* foo\n"
9155             "                     bar */",
9156             format("int  b  =256>>1 ;  /* foo\n"
9157                    "                      bar */"));
9158 }
9159 
9160 TEST_F(FormatTest, UnderstandsBinaryOperators) {
9161   verifyFormat("COMPARE(a, ==, b);");
9162   verifyFormat("auto s = sizeof...(Ts) - 1;");
9163 }
9164 
9165 TEST_F(FormatTest, UnderstandsPointersToMembers) {
9166   verifyFormat("int A::*x;");
9167   verifyFormat("int (S::*func)(void *);");
9168   verifyFormat("void f() { int (S::*func)(void *); }");
9169   verifyFormat("typedef bool *(Class::*Member)() const;");
9170   verifyFormat("void f() {\n"
9171                "  (a->*f)();\n"
9172                "  a->*x;\n"
9173                "  (a.*f)();\n"
9174                "  ((*a).*f)();\n"
9175                "  a.*x;\n"
9176                "}");
9177   verifyFormat("void f() {\n"
9178                "  (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
9179                "      aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
9180                "}");
9181   verifyFormat(
9182       "(aaaaaaaaaa->*bbbbbbb)(\n"
9183       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
9184   FormatStyle Style = getLLVMStyle();
9185   Style.PointerAlignment = FormatStyle::PAS_Left;
9186   verifyFormat("typedef bool* (Class::*Member)() const;", Style);
9187 }
9188 
9189 TEST_F(FormatTest, UnderstandsUnaryOperators) {
9190   verifyFormat("int a = -2;");
9191   verifyFormat("f(-1, -2, -3);");
9192   verifyFormat("a[-1] = 5;");
9193   verifyFormat("int a = 5 + -2;");
9194   verifyFormat("if (i == -1) {\n}");
9195   verifyFormat("if (i != -1) {\n}");
9196   verifyFormat("if (i > -1) {\n}");
9197   verifyFormat("if (i < -1) {\n}");
9198   verifyFormat("++(a->f());");
9199   verifyFormat("--(a->f());");
9200   verifyFormat("(a->f())++;");
9201   verifyFormat("a[42]++;");
9202   verifyFormat("if (!(a->f())) {\n}");
9203   verifyFormat("if (!+i) {\n}");
9204   verifyFormat("~&a;");
9205 
9206   verifyFormat("a-- > b;");
9207   verifyFormat("b ? -a : c;");
9208   verifyFormat("n * sizeof char16;");
9209   verifyFormat("n * alignof char16;", getGoogleStyle());
9210   verifyFormat("sizeof(char);");
9211   verifyFormat("alignof(char);", getGoogleStyle());
9212 
9213   verifyFormat("return -1;");
9214   verifyFormat("throw -1;");
9215   verifyFormat("switch (a) {\n"
9216                "case -1:\n"
9217                "  break;\n"
9218                "}");
9219   verifyFormat("#define X -1");
9220   verifyFormat("#define X -kConstant");
9221 
9222   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
9223   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
9224 
9225   verifyFormat("int a = /* confusing comment */ -1;");
9226   // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
9227   verifyFormat("int a = i /* confusing comment */++;");
9228 
9229   verifyFormat("co_yield -1;");
9230   verifyFormat("co_return -1;");
9231 
9232   // Check that * is not treated as a binary operator when we set
9233   // PointerAlignment as PAS_Left after a keyword and not a declaration.
9234   FormatStyle PASLeftStyle = getLLVMStyle();
9235   PASLeftStyle.PointerAlignment = FormatStyle::PAS_Left;
9236   verifyFormat("co_return *a;", PASLeftStyle);
9237   verifyFormat("co_await *a;", PASLeftStyle);
9238   verifyFormat("co_yield *a", PASLeftStyle);
9239   verifyFormat("return *a;", PASLeftStyle);
9240 }
9241 
9242 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) {
9243   verifyFormat("if (!aaaaaaaaaa( // break\n"
9244                "        aaaaa)) {\n"
9245                "}");
9246   verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
9247                "    aaaaa));");
9248   verifyFormat("*aaa = aaaaaaa( // break\n"
9249                "    bbbbbb);");
9250 }
9251 
9252 TEST_F(FormatTest, UnderstandsOverloadedOperators) {
9253   verifyFormat("bool operator<();");
9254   verifyFormat("bool operator>();");
9255   verifyFormat("bool operator=();");
9256   verifyFormat("bool operator==();");
9257   verifyFormat("bool operator!=();");
9258   verifyFormat("int operator+();");
9259   verifyFormat("int operator++();");
9260   verifyFormat("int operator++(int) volatile noexcept;");
9261   verifyFormat("bool operator,();");
9262   verifyFormat("bool operator();");
9263   verifyFormat("bool operator()();");
9264   verifyFormat("bool operator[]();");
9265   verifyFormat("operator bool();");
9266   verifyFormat("operator int();");
9267   verifyFormat("operator void *();");
9268   verifyFormat("operator SomeType<int>();");
9269   verifyFormat("operator SomeType<int, int>();");
9270   verifyFormat("operator SomeType<SomeType<int>>();");
9271   verifyFormat("void *operator new(std::size_t size);");
9272   verifyFormat("void *operator new[](std::size_t size);");
9273   verifyFormat("void operator delete(void *ptr);");
9274   verifyFormat("void operator delete[](void *ptr);");
9275   verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
9276                "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
9277   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n"
9278                "    aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;");
9279 
9280   verifyFormat(
9281       "ostream &operator<<(ostream &OutputStream,\n"
9282       "                    SomeReallyLongType WithSomeReallyLongValue);");
9283   verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
9284                "               const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
9285                "  return left.group < right.group;\n"
9286                "}");
9287   verifyFormat("SomeType &operator=(const SomeType &S);");
9288   verifyFormat("f.template operator()<int>();");
9289 
9290   verifyGoogleFormat("operator void*();");
9291   verifyGoogleFormat("operator SomeType<SomeType<int>>();");
9292   verifyGoogleFormat("operator ::A();");
9293 
9294   verifyFormat("using A::operator+;");
9295   verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n"
9296                "int i;");
9297 
9298   // Calling an operator as a member function.
9299   verifyFormat("void f() { a.operator*(); }");
9300   verifyFormat("void f() { a.operator*(b & b); }");
9301   verifyFormat("void f() { a->operator&(a * b); }");
9302   verifyFormat("void f() { NS::a.operator+(*b * *b); }");
9303   // TODO: Calling an operator as a non-member function is hard to distinguish.
9304   // https://llvm.org/PR50629
9305   // verifyFormat("void f() { operator*(a & a); }");
9306   // verifyFormat("void f() { operator&(a, b * b); }");
9307 
9308   verifyFormat("::operator delete(foo);");
9309   verifyFormat("::operator new(n * sizeof(foo));");
9310   verifyFormat("foo() { ::operator delete(foo); }");
9311   verifyFormat("foo() { ::operator new(n * sizeof(foo)); }");
9312 }
9313 
9314 TEST_F(FormatTest, UnderstandsFunctionRefQualification) {
9315   verifyFormat("Deleted &operator=(const Deleted &) & = default;");
9316   verifyFormat("Deleted &operator=(const Deleted &) && = delete;");
9317   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;");
9318   verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;");
9319   verifyFormat("Deleted &operator=(const Deleted &) &;");
9320   verifyFormat("Deleted &operator=(const Deleted &) &&;");
9321   verifyFormat("SomeType MemberFunction(const Deleted &) &;");
9322   verifyFormat("SomeType MemberFunction(const Deleted &) &&;");
9323   verifyFormat("SomeType MemberFunction(const Deleted &) && {}");
9324   verifyFormat("SomeType MemberFunction(const Deleted &) && final {}");
9325   verifyFormat("SomeType MemberFunction(const Deleted &) && override {}");
9326   verifyFormat("void Fn(T const &) const &;");
9327   verifyFormat("void Fn(T const volatile &&) const volatile &&;");
9328   verifyFormat("template <typename T>\n"
9329                "void F(T) && = delete;",
9330                getGoogleStyle());
9331 
9332   FormatStyle AlignLeft = getLLVMStyle();
9333   AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
9334   verifyFormat("void A::b() && {}", AlignLeft);
9335   verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft);
9336   verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;",
9337                AlignLeft);
9338   verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft);
9339   verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft);
9340   verifyFormat("auto Function(T t) & -> void {}", AlignLeft);
9341   verifyFormat("auto Function(T... t) & -> void {}", AlignLeft);
9342   verifyFormat("auto Function(T) & -> void {}", AlignLeft);
9343   verifyFormat("auto Function(T) & -> void;", AlignLeft);
9344   verifyFormat("void Fn(T const&) const&;", AlignLeft);
9345   verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft);
9346 
9347   FormatStyle Spaces = getLLVMStyle();
9348   Spaces.SpacesInCStyleCastParentheses = true;
9349   verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces);
9350   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces);
9351   verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces);
9352   verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces);
9353 
9354   Spaces.SpacesInCStyleCastParentheses = false;
9355   Spaces.SpacesInParentheses = true;
9356   verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces);
9357   verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;",
9358                Spaces);
9359   verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces);
9360   verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces);
9361 
9362   FormatStyle BreakTemplate = getLLVMStyle();
9363   BreakTemplate.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
9364 
9365   verifyFormat("struct f {\n"
9366                "  template <class T>\n"
9367                "  int &foo(const std::string &str) &noexcept {}\n"
9368                "};",
9369                BreakTemplate);
9370 
9371   verifyFormat("struct f {\n"
9372                "  template <class T>\n"
9373                "  int &foo(const std::string &str) &&noexcept {}\n"
9374                "};",
9375                BreakTemplate);
9376 
9377   verifyFormat("struct f {\n"
9378                "  template <class T>\n"
9379                "  int &foo(const std::string &str) const &noexcept {}\n"
9380                "};",
9381                BreakTemplate);
9382 
9383   verifyFormat("struct f {\n"
9384                "  template <class T>\n"
9385                "  int &foo(const std::string &str) const &noexcept {}\n"
9386                "};",
9387                BreakTemplate);
9388 
9389   verifyFormat("struct f {\n"
9390                "  template <class T>\n"
9391                "  auto foo(const std::string &str) &&noexcept -> int & {}\n"
9392                "};",
9393                BreakTemplate);
9394 
9395   FormatStyle AlignLeftBreakTemplate = getLLVMStyle();
9396   AlignLeftBreakTemplate.AlwaysBreakTemplateDeclarations =
9397       FormatStyle::BTDS_Yes;
9398   AlignLeftBreakTemplate.PointerAlignment = FormatStyle::PAS_Left;
9399 
9400   verifyFormat("struct f {\n"
9401                "  template <class T>\n"
9402                "  int& foo(const std::string& str) & noexcept {}\n"
9403                "};",
9404                AlignLeftBreakTemplate);
9405 
9406   verifyFormat("struct f {\n"
9407                "  template <class T>\n"
9408                "  int& foo(const std::string& str) && noexcept {}\n"
9409                "};",
9410                AlignLeftBreakTemplate);
9411 
9412   verifyFormat("struct f {\n"
9413                "  template <class T>\n"
9414                "  int& foo(const std::string& str) const& noexcept {}\n"
9415                "};",
9416                AlignLeftBreakTemplate);
9417 
9418   verifyFormat("struct f {\n"
9419                "  template <class T>\n"
9420                "  int& foo(const std::string& str) const&& noexcept {}\n"
9421                "};",
9422                AlignLeftBreakTemplate);
9423 
9424   verifyFormat("struct f {\n"
9425                "  template <class T>\n"
9426                "  auto foo(const std::string& str) && noexcept -> int& {}\n"
9427                "};",
9428                AlignLeftBreakTemplate);
9429 
9430   // The `&` in `Type&` should not be confused with a trailing `&` of
9431   // DEPRECATED(reason) member function.
9432   verifyFormat("struct f {\n"
9433                "  template <class T>\n"
9434                "  DEPRECATED(reason)\n"
9435                "  Type &foo(arguments) {}\n"
9436                "};",
9437                BreakTemplate);
9438 
9439   verifyFormat("struct f {\n"
9440                "  template <class T>\n"
9441                "  DEPRECATED(reason)\n"
9442                "  Type& foo(arguments) {}\n"
9443                "};",
9444                AlignLeftBreakTemplate);
9445 
9446   verifyFormat("void (*foopt)(int) = &func;");
9447 }
9448 
9449 TEST_F(FormatTest, UnderstandsNewAndDelete) {
9450   verifyFormat("void f() {\n"
9451                "  A *a = new A;\n"
9452                "  A *a = new (placement) A;\n"
9453                "  delete a;\n"
9454                "  delete (A *)a;\n"
9455                "}");
9456   verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
9457                "    typename aaaaaaaaaaaaaaaaaaaaaaaa();");
9458   verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
9459                "    new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
9460                "        typename aaaaaaaaaaaaaaaaaaaaaaaa();");
9461   verifyFormat("delete[] h->p;");
9462 }
9463 
9464 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
9465   verifyFormat("int *f(int *a) {}");
9466   verifyFormat("int main(int argc, char **argv) {}");
9467   verifyFormat("Test::Test(int b) : a(b * b) {}");
9468   verifyIndependentOfContext("f(a, *a);");
9469   verifyFormat("void g() { f(*a); }");
9470   verifyIndependentOfContext("int a = b * 10;");
9471   verifyIndependentOfContext("int a = 10 * b;");
9472   verifyIndependentOfContext("int a = b * c;");
9473   verifyIndependentOfContext("int a += b * c;");
9474   verifyIndependentOfContext("int a -= b * c;");
9475   verifyIndependentOfContext("int a *= b * c;");
9476   verifyIndependentOfContext("int a /= b * c;");
9477   verifyIndependentOfContext("int a = *b;");
9478   verifyIndependentOfContext("int a = *b * c;");
9479   verifyIndependentOfContext("int a = b * *c;");
9480   verifyIndependentOfContext("int a = b * (10);");
9481   verifyIndependentOfContext("S << b * (10);");
9482   verifyIndependentOfContext("return 10 * b;");
9483   verifyIndependentOfContext("return *b * *c;");
9484   verifyIndependentOfContext("return a & ~b;");
9485   verifyIndependentOfContext("f(b ? *c : *d);");
9486   verifyIndependentOfContext("int a = b ? *c : *d;");
9487   verifyIndependentOfContext("*b = a;");
9488   verifyIndependentOfContext("a * ~b;");
9489   verifyIndependentOfContext("a * !b;");
9490   verifyIndependentOfContext("a * +b;");
9491   verifyIndependentOfContext("a * -b;");
9492   verifyIndependentOfContext("a * ++b;");
9493   verifyIndependentOfContext("a * --b;");
9494   verifyIndependentOfContext("a[4] * b;");
9495   verifyIndependentOfContext("a[a * a] = 1;");
9496   verifyIndependentOfContext("f() * b;");
9497   verifyIndependentOfContext("a * [self dostuff];");
9498   verifyIndependentOfContext("int x = a * (a + b);");
9499   verifyIndependentOfContext("(a *)(a + b);");
9500   verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
9501   verifyIndependentOfContext("int *pa = (int *)&a;");
9502   verifyIndependentOfContext("return sizeof(int **);");
9503   verifyIndependentOfContext("return sizeof(int ******);");
9504   verifyIndependentOfContext("return (int **&)a;");
9505   verifyIndependentOfContext("f((*PointerToArray)[10]);");
9506   verifyFormat("void f(Type (*parameter)[10]) {}");
9507   verifyFormat("void f(Type (&parameter)[10]) {}");
9508   verifyGoogleFormat("return sizeof(int**);");
9509   verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
9510   verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
9511   verifyFormat("auto a = [](int **&, int ***) {};");
9512   verifyFormat("auto PointerBinding = [](const char *S) {};");
9513   verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
9514   verifyFormat("[](const decltype(*a) &value) {}");
9515   verifyFormat("[](const typeof(*a) &value) {}");
9516   verifyFormat("[](const _Atomic(a *) &value) {}");
9517   verifyFormat("[](const __underlying_type(a) &value) {}");
9518   verifyFormat("decltype(a * b) F();");
9519   verifyFormat("typeof(a * b) F();");
9520   verifyFormat("#define MACRO() [](A *a) { return 1; }");
9521   verifyFormat("Constructor() : member([](A *a, B *b) {}) {}");
9522   verifyIndependentOfContext("typedef void (*f)(int *a);");
9523   verifyIndependentOfContext("int i{a * b};");
9524   verifyIndependentOfContext("aaa && aaa->f();");
9525   verifyIndependentOfContext("int x = ~*p;");
9526   verifyFormat("Constructor() : a(a), area(width * height) {}");
9527   verifyFormat("Constructor() : a(a), area(a, width * height) {}");
9528   verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}");
9529   verifyFormat("void f() { f(a, c * d); }");
9530   verifyFormat("void f() { f(new a(), c * d); }");
9531   verifyFormat("void f(const MyOverride &override);");
9532   verifyFormat("void f(const MyFinal &final);");
9533   verifyIndependentOfContext("bool a = f() && override.f();");
9534   verifyIndependentOfContext("bool a = f() && final.f();");
9535 
9536   verifyIndependentOfContext("InvalidRegions[*R] = 0;");
9537 
9538   verifyIndependentOfContext("A<int *> a;");
9539   verifyIndependentOfContext("A<int **> a;");
9540   verifyIndependentOfContext("A<int *, int *> a;");
9541   verifyIndependentOfContext("A<int *[]> a;");
9542   verifyIndependentOfContext(
9543       "const char *const p = reinterpret_cast<const char *const>(q);");
9544   verifyIndependentOfContext("A<int **, int **> a;");
9545   verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
9546   verifyFormat("for (char **a = b; *a; ++a) {\n}");
9547   verifyFormat("for (; a && b;) {\n}");
9548   verifyFormat("bool foo = true && [] { return false; }();");
9549 
9550   verifyFormat(
9551       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9552       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9553 
9554   verifyGoogleFormat("int const* a = &b;");
9555   verifyGoogleFormat("**outparam = 1;");
9556   verifyGoogleFormat("*outparam = a * b;");
9557   verifyGoogleFormat("int main(int argc, char** argv) {}");
9558   verifyGoogleFormat("A<int*> a;");
9559   verifyGoogleFormat("A<int**> a;");
9560   verifyGoogleFormat("A<int*, int*> a;");
9561   verifyGoogleFormat("A<int**, int**> a;");
9562   verifyGoogleFormat("f(b ? *c : *d);");
9563   verifyGoogleFormat("int a = b ? *c : *d;");
9564   verifyGoogleFormat("Type* t = **x;");
9565   verifyGoogleFormat("Type* t = *++*x;");
9566   verifyGoogleFormat("*++*x;");
9567   verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
9568   verifyGoogleFormat("Type* t = x++ * y;");
9569   verifyGoogleFormat(
9570       "const char* const p = reinterpret_cast<const char* const>(q);");
9571   verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
9572   verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
9573   verifyGoogleFormat("template <typename T>\n"
9574                      "void f(int i = 0, SomeType** temps = NULL);");
9575 
9576   FormatStyle Left = getLLVMStyle();
9577   Left.PointerAlignment = FormatStyle::PAS_Left;
9578   verifyFormat("x = *a(x) = *a(y);", Left);
9579   verifyFormat("for (;; *a = b) {\n}", Left);
9580   verifyFormat("return *this += 1;", Left);
9581   verifyFormat("throw *x;", Left);
9582   verifyFormat("delete *x;", Left);
9583   verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left);
9584   verifyFormat("[](const decltype(*a)* ptr) {}", Left);
9585   verifyFormat("[](const typeof(*a)* ptr) {}", Left);
9586   verifyFormat("[](const _Atomic(a*)* ptr) {}", Left);
9587   verifyFormat("[](const __underlying_type(a)* ptr) {}", Left);
9588   verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left);
9589   verifyFormat("auto x(A&&, B&&, C&&) -> D;", Left);
9590   verifyFormat("auto x = [](A&&, B&&, C&&) -> D {};", Left);
9591   verifyFormat("template <class T> X(T&&, T&&, T&&) -> X<T>;", Left);
9592 
9593   verifyIndependentOfContext("a = *(x + y);");
9594   verifyIndependentOfContext("a = &(x + y);");
9595   verifyIndependentOfContext("*(x + y).call();");
9596   verifyIndependentOfContext("&(x + y)->call();");
9597   verifyFormat("void f() { &(*I).first; }");
9598 
9599   verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
9600   verifyFormat("f(* /* confusing comment */ foo);");
9601   verifyFormat("void (* /*deleter*/)(const Slice &key, void *value)");
9602   verifyFormat("void foo(int * // this is the first paramters\n"
9603                "         ,\n"
9604                "         int second);");
9605   verifyFormat("double term = a * // first\n"
9606                "              b;");
9607   verifyFormat(
9608       "int *MyValues = {\n"
9609       "    *A, // Operator detection might be confused by the '{'\n"
9610       "    *BB // Operator detection might be confused by previous comment\n"
9611       "};");
9612 
9613   verifyIndependentOfContext("if (int *a = &b)");
9614   verifyIndependentOfContext("if (int &a = *b)");
9615   verifyIndependentOfContext("if (a & b[i])");
9616   verifyIndependentOfContext("if constexpr (a & b[i])");
9617   verifyIndependentOfContext("if CONSTEXPR (a & b[i])");
9618   verifyIndependentOfContext("if (a * (b * c))");
9619   verifyIndependentOfContext("if constexpr (a * (b * c))");
9620   verifyIndependentOfContext("if CONSTEXPR (a * (b * c))");
9621   verifyIndependentOfContext("if (a::b::c::d & b[i])");
9622   verifyIndependentOfContext("if (*b[i])");
9623   verifyIndependentOfContext("if (int *a = (&b))");
9624   verifyIndependentOfContext("while (int *a = &b)");
9625   verifyIndependentOfContext("while (a * (b * c))");
9626   verifyIndependentOfContext("size = sizeof *a;");
9627   verifyIndependentOfContext("if (a && (b = c))");
9628   verifyFormat("void f() {\n"
9629                "  for (const int &v : Values) {\n"
9630                "  }\n"
9631                "}");
9632   verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
9633   verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
9634   verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
9635 
9636   verifyFormat("#define A (!a * b)");
9637   verifyFormat("#define MACRO     \\\n"
9638                "  int *i = a * b; \\\n"
9639                "  void f(a *b);",
9640                getLLVMStyleWithColumns(19));
9641 
9642   verifyIndependentOfContext("A = new SomeType *[Length];");
9643   verifyIndependentOfContext("A = new SomeType *[Length]();");
9644   verifyIndependentOfContext("T **t = new T *;");
9645   verifyIndependentOfContext("T **t = new T *();");
9646   verifyGoogleFormat("A = new SomeType*[Length]();");
9647   verifyGoogleFormat("A = new SomeType*[Length];");
9648   verifyGoogleFormat("T** t = new T*;");
9649   verifyGoogleFormat("T** t = new T*();");
9650 
9651   verifyFormat("STATIC_ASSERT((a & b) == 0);");
9652   verifyFormat("STATIC_ASSERT(0 == (a & b));");
9653   verifyFormat("template <bool a, bool b> "
9654                "typename t::if<x && y>::type f() {}");
9655   verifyFormat("template <int *y> f() {}");
9656   verifyFormat("vector<int *> v;");
9657   verifyFormat("vector<int *const> v;");
9658   verifyFormat("vector<int *const **const *> v;");
9659   verifyFormat("vector<int *volatile> v;");
9660   verifyFormat("vector<a *_Nonnull> v;");
9661   verifyFormat("vector<a *_Nullable> v;");
9662   verifyFormat("vector<a *_Null_unspecified> v;");
9663   verifyFormat("vector<a *__ptr32> v;");
9664   verifyFormat("vector<a *__ptr64> v;");
9665   verifyFormat("vector<a *__capability> v;");
9666   FormatStyle TypeMacros = getLLVMStyle();
9667   TypeMacros.TypenameMacros = {"LIST"};
9668   verifyFormat("vector<LIST(uint64_t)> v;", TypeMacros);
9669   verifyFormat("vector<LIST(uint64_t) *> v;", TypeMacros);
9670   verifyFormat("vector<LIST(uint64_t) **> v;", TypeMacros);
9671   verifyFormat("vector<LIST(uint64_t) *attr> v;", TypeMacros);
9672   verifyFormat("vector<A(uint64_t) * attr> v;", TypeMacros); // multiplication
9673 
9674   FormatStyle CustomQualifier = getLLVMStyle();
9675   // Add identifiers that should not be parsed as a qualifier by default.
9676   CustomQualifier.AttributeMacros.push_back("__my_qualifier");
9677   CustomQualifier.AttributeMacros.push_back("_My_qualifier");
9678   CustomQualifier.AttributeMacros.push_back("my_other_qualifier");
9679   verifyFormat("vector<a * __my_qualifier> parse_as_multiply;");
9680   verifyFormat("vector<a *__my_qualifier> v;", CustomQualifier);
9681   verifyFormat("vector<a * _My_qualifier> parse_as_multiply;");
9682   verifyFormat("vector<a *_My_qualifier> v;", CustomQualifier);
9683   verifyFormat("vector<a * my_other_qualifier> parse_as_multiply;");
9684   verifyFormat("vector<a *my_other_qualifier> v;", CustomQualifier);
9685   verifyFormat("vector<a * _NotAQualifier> v;");
9686   verifyFormat("vector<a * __not_a_qualifier> v;");
9687   verifyFormat("vector<a * b> v;");
9688   verifyFormat("foo<b && false>();");
9689   verifyFormat("foo<b & 1>();");
9690   verifyFormat("decltype(*::std::declval<const T &>()) void F();");
9691   verifyFormat("typeof(*::std::declval<const T &>()) void F();");
9692   verifyFormat("_Atomic(*::std::declval<const T &>()) void F();");
9693   verifyFormat("__underlying_type(*::std::declval<const T &>()) void F();");
9694   verifyFormat(
9695       "template <class T, class = typename std::enable_if<\n"
9696       "                       std::is_integral<T>::value &&\n"
9697       "                       (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
9698       "void F();",
9699       getLLVMStyleWithColumns(70));
9700   verifyFormat("template <class T,\n"
9701                "          class = typename std::enable_if<\n"
9702                "              std::is_integral<T>::value &&\n"
9703                "              (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n"
9704                "          class U>\n"
9705                "void F();",
9706                getLLVMStyleWithColumns(70));
9707   verifyFormat(
9708       "template <class T,\n"
9709       "          class = typename ::std::enable_if<\n"
9710       "              ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
9711       "void F();",
9712       getGoogleStyleWithColumns(68));
9713 
9714   verifyIndependentOfContext("MACRO(int *i);");
9715   verifyIndependentOfContext("MACRO(auto *a);");
9716   verifyIndependentOfContext("MACRO(const A *a);");
9717   verifyIndependentOfContext("MACRO(_Atomic(A) *a);");
9718   verifyIndependentOfContext("MACRO(decltype(A) *a);");
9719   verifyIndependentOfContext("MACRO(typeof(A) *a);");
9720   verifyIndependentOfContext("MACRO(__underlying_type(A) *a);");
9721   verifyIndependentOfContext("MACRO(A *const a);");
9722   verifyIndependentOfContext("MACRO(A *restrict a);");
9723   verifyIndependentOfContext("MACRO(A *__restrict__ a);");
9724   verifyIndependentOfContext("MACRO(A *__restrict a);");
9725   verifyIndependentOfContext("MACRO(A *volatile a);");
9726   verifyIndependentOfContext("MACRO(A *__volatile a);");
9727   verifyIndependentOfContext("MACRO(A *__volatile__ a);");
9728   verifyIndependentOfContext("MACRO(A *_Nonnull a);");
9729   verifyIndependentOfContext("MACRO(A *_Nullable a);");
9730   verifyIndependentOfContext("MACRO(A *_Null_unspecified a);");
9731   verifyIndependentOfContext("MACRO(A *__attribute__((foo)) a);");
9732   verifyIndependentOfContext("MACRO(A *__attribute((foo)) a);");
9733   verifyIndependentOfContext("MACRO(A *[[clang::attr]] a);");
9734   verifyIndependentOfContext("MACRO(A *[[clang::attr(\"foo\")]] a);");
9735   verifyIndependentOfContext("MACRO(A *__ptr32 a);");
9736   verifyIndependentOfContext("MACRO(A *__ptr64 a);");
9737   verifyIndependentOfContext("MACRO(A *__capability);");
9738   verifyIndependentOfContext("MACRO(A &__capability);");
9739   verifyFormat("MACRO(A *__my_qualifier);");               // type declaration
9740   verifyFormat("void f() { MACRO(A * __my_qualifier); }"); // multiplication
9741   // If we add __my_qualifier to AttributeMacros it should always be parsed as
9742   // a type declaration:
9743   verifyFormat("MACRO(A *__my_qualifier);", CustomQualifier);
9744   verifyFormat("void f() { MACRO(A *__my_qualifier); }", CustomQualifier);
9745   // Also check that TypenameMacros prevents parsing it as multiplication:
9746   verifyIndependentOfContext("MACRO(LIST(uint64_t) * a);"); // multiplication
9747   verifyIndependentOfContext("MACRO(LIST(uint64_t) *a);", TypeMacros); // type
9748 
9749   verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
9750   verifyFormat("void f() { f(float{1}, a * a); }");
9751   verifyFormat("void f() { f(float(1), a * a); }");
9752 
9753   verifyFormat("f((void (*)(int))g);");
9754   verifyFormat("f((void (&)(int))g);");
9755   verifyFormat("f((void (^)(int))g);");
9756 
9757   // FIXME: Is there a way to make this work?
9758   // verifyIndependentOfContext("MACRO(A *a);");
9759   verifyFormat("MACRO(A &B);");
9760   verifyFormat("MACRO(A *B);");
9761   verifyFormat("void f() { MACRO(A * B); }");
9762   verifyFormat("void f() { MACRO(A & B); }");
9763 
9764   // This lambda was mis-formatted after D88956 (treating it as a binop):
9765   verifyFormat("auto x = [](const decltype(x) &ptr) {};");
9766   verifyFormat("auto x = [](const decltype(x) *ptr) {};");
9767   verifyFormat("#define lambda [](const decltype(x) &ptr) {}");
9768   verifyFormat("#define lambda [](const decltype(x) *ptr) {}");
9769 
9770   verifyFormat("DatumHandle const *operator->() const { return input_; }");
9771   verifyFormat("return options != nullptr && operator==(*options);");
9772 
9773   EXPECT_EQ("#define OP(x)                                    \\\n"
9774             "  ostream &operator<<(ostream &s, const A &a) {  \\\n"
9775             "    return s << a.DebugString();                 \\\n"
9776             "  }",
9777             format("#define OP(x) \\\n"
9778                    "  ostream &operator<<(ostream &s, const A &a) { \\\n"
9779                    "    return s << a.DebugString(); \\\n"
9780                    "  }",
9781                    getLLVMStyleWithColumns(50)));
9782 
9783   // FIXME: We cannot handle this case yet; we might be able to figure out that
9784   // foo<x> d > v; doesn't make sense.
9785   verifyFormat("foo<a<b && c> d> v;");
9786 
9787   FormatStyle PointerMiddle = getLLVMStyle();
9788   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
9789   verifyFormat("delete *x;", PointerMiddle);
9790   verifyFormat("int * x;", PointerMiddle);
9791   verifyFormat("int *[] x;", PointerMiddle);
9792   verifyFormat("template <int * y> f() {}", PointerMiddle);
9793   verifyFormat("int * f(int * a) {}", PointerMiddle);
9794   verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle);
9795   verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle);
9796   verifyFormat("A<int *> a;", PointerMiddle);
9797   verifyFormat("A<int **> a;", PointerMiddle);
9798   verifyFormat("A<int *, int *> a;", PointerMiddle);
9799   verifyFormat("A<int *[]> a;", PointerMiddle);
9800   verifyFormat("A = new SomeType *[Length]();", PointerMiddle);
9801   verifyFormat("A = new SomeType *[Length];", PointerMiddle);
9802   verifyFormat("T ** t = new T *;", PointerMiddle);
9803 
9804   // Member function reference qualifiers aren't binary operators.
9805   verifyFormat("string // break\n"
9806                "operator()() & {}");
9807   verifyFormat("string // break\n"
9808                "operator()() && {}");
9809   verifyGoogleFormat("template <typename T>\n"
9810                      "auto x() & -> int {}");
9811 
9812   // Should be binary operators when used as an argument expression (overloaded
9813   // operator invoked as a member function).
9814   verifyFormat("void f() { a.operator()(a * a); }");
9815   verifyFormat("void f() { a->operator()(a & a); }");
9816   verifyFormat("void f() { a.operator()(*a & *a); }");
9817   verifyFormat("void f() { a->operator()(*a * *a); }");
9818 
9819   verifyFormat("int operator()(T (&&)[N]) { return 1; }");
9820   verifyFormat("int operator()(T (&)[N]) { return 0; }");
9821 }
9822 
9823 TEST_F(FormatTest, UnderstandsAttributes) {
9824   verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
9825   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
9826                "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
9827   FormatStyle AfterType = getLLVMStyle();
9828   AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
9829   verifyFormat("__attribute__((nodebug)) void\n"
9830                "foo() {}\n",
9831                AfterType);
9832   verifyFormat("__unused void\n"
9833                "foo() {}",
9834                AfterType);
9835 
9836   FormatStyle CustomAttrs = getLLVMStyle();
9837   CustomAttrs.AttributeMacros.push_back("__unused");
9838   CustomAttrs.AttributeMacros.push_back("__attr1");
9839   CustomAttrs.AttributeMacros.push_back("__attr2");
9840   CustomAttrs.AttributeMacros.push_back("no_underscore_attr");
9841   verifyFormat("vector<SomeType *__attribute((foo))> v;");
9842   verifyFormat("vector<SomeType *__attribute__((foo))> v;");
9843   verifyFormat("vector<SomeType * __not_attribute__((foo))> v;");
9844   // Check that it is parsed as a multiplication without AttributeMacros and
9845   // as a pointer qualifier when we add __attr1/__attr2 to AttributeMacros.
9846   verifyFormat("vector<SomeType * __attr1> v;");
9847   verifyFormat("vector<SomeType __attr1 *> v;");
9848   verifyFormat("vector<SomeType __attr1 *const> v;");
9849   verifyFormat("vector<SomeType __attr1 * __attr2> v;");
9850   verifyFormat("vector<SomeType *__attr1> v;", CustomAttrs);
9851   verifyFormat("vector<SomeType *__attr2> v;", CustomAttrs);
9852   verifyFormat("vector<SomeType *no_underscore_attr> v;", CustomAttrs);
9853   verifyFormat("vector<SomeType __attr1 *> v;", CustomAttrs);
9854   verifyFormat("vector<SomeType __attr1 *const> v;", CustomAttrs);
9855   verifyFormat("vector<SomeType __attr1 *__attr2> v;", CustomAttrs);
9856   verifyFormat("vector<SomeType __attr1 *no_underscore_attr> v;", CustomAttrs);
9857 
9858   // Check that these are not parsed as function declarations:
9859   CustomAttrs.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
9860   CustomAttrs.BreakBeforeBraces = FormatStyle::BS_Allman;
9861   verifyFormat("SomeType s(InitValue);", CustomAttrs);
9862   verifyFormat("SomeType s{InitValue};", CustomAttrs);
9863   verifyFormat("SomeType *__unused s(InitValue);", CustomAttrs);
9864   verifyFormat("SomeType *__unused s{InitValue};", CustomAttrs);
9865   verifyFormat("SomeType s __unused(InitValue);", CustomAttrs);
9866   verifyFormat("SomeType s __unused{InitValue};", CustomAttrs);
9867   verifyFormat("SomeType *__capability s(InitValue);", CustomAttrs);
9868   verifyFormat("SomeType *__capability s{InitValue};", CustomAttrs);
9869 }
9870 
9871 TEST_F(FormatTest, UnderstandsPointerQualifiersInCast) {
9872   // Check that qualifiers on pointers don't break parsing of casts.
9873   verifyFormat("x = (foo *const)*v;");
9874   verifyFormat("x = (foo *volatile)*v;");
9875   verifyFormat("x = (foo *restrict)*v;");
9876   verifyFormat("x = (foo *__attribute__((foo)))*v;");
9877   verifyFormat("x = (foo *_Nonnull)*v;");
9878   verifyFormat("x = (foo *_Nullable)*v;");
9879   verifyFormat("x = (foo *_Null_unspecified)*v;");
9880   verifyFormat("x = (foo *_Nonnull)*v;");
9881   verifyFormat("x = (foo *[[clang::attr]])*v;");
9882   verifyFormat("x = (foo *[[clang::attr(\"foo\")]])*v;");
9883   verifyFormat("x = (foo *__ptr32)*v;");
9884   verifyFormat("x = (foo *__ptr64)*v;");
9885   verifyFormat("x = (foo *__capability)*v;");
9886 
9887   // Check that we handle multiple trailing qualifiers and skip them all to
9888   // determine that the expression is a cast to a pointer type.
9889   FormatStyle LongPointerRight = getLLVMStyleWithColumns(999);
9890   FormatStyle LongPointerLeft = getLLVMStyleWithColumns(999);
9891   LongPointerLeft.PointerAlignment = FormatStyle::PAS_Left;
9892   StringRef AllQualifiers =
9893       "const volatile restrict __attribute__((foo)) _Nonnull _Null_unspecified "
9894       "_Nonnull [[clang::attr]] __ptr32 __ptr64 __capability";
9895   verifyFormat(("x = (foo *" + AllQualifiers + ")*v;").str(), LongPointerRight);
9896   verifyFormat(("x = (foo* " + AllQualifiers + ")*v;").str(), LongPointerLeft);
9897 
9898   // Also check that address-of is not parsed as a binary bitwise-and:
9899   verifyFormat("x = (foo *const)&v;");
9900   verifyFormat(("x = (foo *" + AllQualifiers + ")&v;").str(), LongPointerRight);
9901   verifyFormat(("x = (foo* " + AllQualifiers + ")&v;").str(), LongPointerLeft);
9902 
9903   // Check custom qualifiers:
9904   FormatStyle CustomQualifier = getLLVMStyleWithColumns(999);
9905   CustomQualifier.AttributeMacros.push_back("__my_qualifier");
9906   verifyFormat("x = (foo * __my_qualifier) * v;"); // not parsed as qualifier.
9907   verifyFormat("x = (foo *__my_qualifier)*v;", CustomQualifier);
9908   verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)*v;").str(),
9909                CustomQualifier);
9910   verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)&v;").str(),
9911                CustomQualifier);
9912 
9913   // Check that unknown identifiers result in binary operator parsing:
9914   verifyFormat("x = (foo * __unknown_qualifier) * v;");
9915   verifyFormat("x = (foo * __unknown_qualifier) & v;");
9916 }
9917 
9918 TEST_F(FormatTest, UnderstandsSquareAttributes) {
9919   verifyFormat("SomeType s [[unused]] (InitValue);");
9920   verifyFormat("SomeType s [[gnu::unused]] (InitValue);");
9921   verifyFormat("SomeType s [[using gnu: unused]] (InitValue);");
9922   verifyFormat("[[gsl::suppress(\"clang-tidy-check-name\")]] void f() {}");
9923   verifyFormat("void f() [[deprecated(\"so sorry\")]];");
9924   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9925                "    [[unused]] aaaaaaaaaaaaaaaaaaaaaaa(int i);");
9926   verifyFormat("[[nodiscard]] bool f() { return false; }");
9927   verifyFormat("class [[nodiscard]] f {\npublic:\n  f() {}\n}");
9928   verifyFormat("class [[deprecated(\"so sorry\")]] f {\npublic:\n  f() {}\n}");
9929   verifyFormat("class [[gnu::unused]] f {\npublic:\n  f() {}\n}");
9930 
9931   // Make sure we do not mistake attributes for array subscripts.
9932   verifyFormat("int a() {}\n"
9933                "[[unused]] int b() {}\n");
9934   verifyFormat("NSArray *arr;\n"
9935                "arr[[Foo() bar]];");
9936 
9937   // On the other hand, we still need to correctly find array subscripts.
9938   verifyFormat("int a = std::vector<int>{1, 2, 3}[0];");
9939 
9940   // Make sure that we do not mistake Objective-C method inside array literals
9941   // as attributes, even if those method names are also keywords.
9942   verifyFormat("@[ [foo bar] ];");
9943   verifyFormat("@[ [NSArray class] ];");
9944   verifyFormat("@[ [foo enum] ];");
9945 
9946   verifyFormat("template <typename T> [[nodiscard]] int a() { return 1; }");
9947 
9948   // Make sure we do not parse attributes as lambda introducers.
9949   FormatStyle MultiLineFunctions = getLLVMStyle();
9950   MultiLineFunctions.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
9951   verifyFormat("[[unused]] int b() {\n"
9952                "  return 42;\n"
9953                "}\n",
9954                MultiLineFunctions);
9955 }
9956 
9957 TEST_F(FormatTest, AttributeClass) {
9958   FormatStyle Style = getChromiumStyle(FormatStyle::LK_Cpp);
9959   verifyFormat("class S {\n"
9960                "  S(S&&) = default;\n"
9961                "};",
9962                Style);
9963   verifyFormat("class [[nodiscard]] S {\n"
9964                "  S(S&&) = default;\n"
9965                "};",
9966                Style);
9967   verifyFormat("class __attribute((maybeunused)) S {\n"
9968                "  S(S&&) = default;\n"
9969                "};",
9970                Style);
9971   verifyFormat("struct S {\n"
9972                "  S(S&&) = default;\n"
9973                "};",
9974                Style);
9975   verifyFormat("struct [[nodiscard]] S {\n"
9976                "  S(S&&) = default;\n"
9977                "};",
9978                Style);
9979 }
9980 
9981 TEST_F(FormatTest, AttributesAfterMacro) {
9982   FormatStyle Style = getLLVMStyle();
9983   verifyFormat("MACRO;\n"
9984                "__attribute__((maybe_unused)) int foo() {\n"
9985                "  //...\n"
9986                "}");
9987 
9988   verifyFormat("MACRO;\n"
9989                "[[nodiscard]] int foo() {\n"
9990                "  //...\n"
9991                "}");
9992 
9993   EXPECT_EQ("MACRO\n\n"
9994             "__attribute__((maybe_unused)) int foo() {\n"
9995             "  //...\n"
9996             "}",
9997             format("MACRO\n\n"
9998                    "__attribute__((maybe_unused)) int foo() {\n"
9999                    "  //...\n"
10000                    "}"));
10001 
10002   EXPECT_EQ("MACRO\n\n"
10003             "[[nodiscard]] int foo() {\n"
10004             "  //...\n"
10005             "}",
10006             format("MACRO\n\n"
10007                    "[[nodiscard]] int foo() {\n"
10008                    "  //...\n"
10009                    "}"));
10010 }
10011 
10012 TEST_F(FormatTest, AttributePenaltyBreaking) {
10013   FormatStyle Style = getLLVMStyle();
10014   verifyFormat("void ABCDEFGH::ABCDEFGHIJKLMN(\n"
10015                "    [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}",
10016                Style);
10017   verifyFormat("void ABCDEFGH::ABCDEFGHIJK(\n"
10018                "    [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}",
10019                Style);
10020   verifyFormat("void ABCDEFGH::ABCDEFGH([[maybe_unused]] const "
10021                "shared_ptr<ALongTypeName> &C d) {\n}",
10022                Style);
10023 }
10024 
10025 TEST_F(FormatTest, UnderstandsEllipsis) {
10026   FormatStyle Style = getLLVMStyle();
10027   verifyFormat("int printf(const char *fmt, ...);");
10028   verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
10029   verifyFormat("template <class... Ts> void Foo(Ts *...ts) {}");
10030 
10031   verifyFormat("template <int *...PP> a;", Style);
10032 
10033   Style.PointerAlignment = FormatStyle::PAS_Left;
10034   verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", Style);
10035 
10036   verifyFormat("template <int*... PP> a;", Style);
10037 
10038   Style.PointerAlignment = FormatStyle::PAS_Middle;
10039   verifyFormat("template <int *... PP> a;", Style);
10040 }
10041 
10042 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
10043   EXPECT_EQ("int *a;\n"
10044             "int *a;\n"
10045             "int *a;",
10046             format("int *a;\n"
10047                    "int* a;\n"
10048                    "int *a;",
10049                    getGoogleStyle()));
10050   EXPECT_EQ("int* a;\n"
10051             "int* a;\n"
10052             "int* a;",
10053             format("int* a;\n"
10054                    "int* a;\n"
10055                    "int *a;",
10056                    getGoogleStyle()));
10057   EXPECT_EQ("int *a;\n"
10058             "int *a;\n"
10059             "int *a;",
10060             format("int *a;\n"
10061                    "int * a;\n"
10062                    "int *  a;",
10063                    getGoogleStyle()));
10064   EXPECT_EQ("auto x = [] {\n"
10065             "  int *a;\n"
10066             "  int *a;\n"
10067             "  int *a;\n"
10068             "};",
10069             format("auto x=[]{int *a;\n"
10070                    "int * a;\n"
10071                    "int *  a;};",
10072                    getGoogleStyle()));
10073 }
10074 
10075 TEST_F(FormatTest, UnderstandsRvalueReferences) {
10076   verifyFormat("int f(int &&a) {}");
10077   verifyFormat("int f(int a, char &&b) {}");
10078   verifyFormat("void f() { int &&a = b; }");
10079   verifyGoogleFormat("int f(int a, char&& b) {}");
10080   verifyGoogleFormat("void f() { int&& a = b; }");
10081 
10082   verifyIndependentOfContext("A<int &&> a;");
10083   verifyIndependentOfContext("A<int &&, int &&> a;");
10084   verifyGoogleFormat("A<int&&> a;");
10085   verifyGoogleFormat("A<int&&, int&&> a;");
10086 
10087   // Not rvalue references:
10088   verifyFormat("template <bool B, bool C> class A {\n"
10089                "  static_assert(B && C, \"Something is wrong\");\n"
10090                "};");
10091   verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
10092   verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
10093   verifyFormat("#define A(a, b) (a && b)");
10094 }
10095 
10096 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
10097   verifyFormat("void f() {\n"
10098                "  x[aaaaaaaaa -\n"
10099                "    b] = 23;\n"
10100                "}",
10101                getLLVMStyleWithColumns(15));
10102 }
10103 
10104 TEST_F(FormatTest, FormatsCasts) {
10105   verifyFormat("Type *A = static_cast<Type *>(P);");
10106   verifyFormat("Type *A = (Type *)P;");
10107   verifyFormat("Type *A = (vector<Type *, int *>)P;");
10108   verifyFormat("int a = (int)(2.0f);");
10109   verifyFormat("int a = (int)2.0f;");
10110   verifyFormat("x[(int32)y];");
10111   verifyFormat("x = (int32)y;");
10112   verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
10113   verifyFormat("int a = (int)*b;");
10114   verifyFormat("int a = (int)2.0f;");
10115   verifyFormat("int a = (int)~0;");
10116   verifyFormat("int a = (int)++a;");
10117   verifyFormat("int a = (int)sizeof(int);");
10118   verifyFormat("int a = (int)+2;");
10119   verifyFormat("my_int a = (my_int)2.0f;");
10120   verifyFormat("my_int a = (my_int)sizeof(int);");
10121   verifyFormat("return (my_int)aaa;");
10122   verifyFormat("#define x ((int)-1)");
10123   verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
10124   verifyFormat("#define p(q) ((int *)&q)");
10125   verifyFormat("fn(a)(b) + 1;");
10126 
10127   verifyFormat("void f() { my_int a = (my_int)*b; }");
10128   verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
10129   verifyFormat("my_int a = (my_int)~0;");
10130   verifyFormat("my_int a = (my_int)++a;");
10131   verifyFormat("my_int a = (my_int)-2;");
10132   verifyFormat("my_int a = (my_int)1;");
10133   verifyFormat("my_int a = (my_int *)1;");
10134   verifyFormat("my_int a = (const my_int)-1;");
10135   verifyFormat("my_int a = (const my_int *)-1;");
10136   verifyFormat("my_int a = (my_int)(my_int)-1;");
10137   verifyFormat("my_int a = (ns::my_int)-2;");
10138   verifyFormat("case (my_int)ONE:");
10139   verifyFormat("auto x = (X)this;");
10140   // Casts in Obj-C style calls used to not be recognized as such.
10141   verifyFormat("int a = [(type*)[((type*)val) arg] arg];", getGoogleStyle());
10142 
10143   // FIXME: single value wrapped with paren will be treated as cast.
10144   verifyFormat("void f(int i = (kValue)*kMask) {}");
10145 
10146   verifyFormat("{ (void)F; }");
10147 
10148   // Don't break after a cast's
10149   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
10150                "    (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
10151                "                                   bbbbbbbbbbbbbbbbbbbbbb);");
10152 
10153   // These are not casts.
10154   verifyFormat("void f(int *) {}");
10155   verifyFormat("f(foo)->b;");
10156   verifyFormat("f(foo).b;");
10157   verifyFormat("f(foo)(b);");
10158   verifyFormat("f(foo)[b];");
10159   verifyFormat("[](foo) { return 4; }(bar);");
10160   verifyFormat("(*funptr)(foo)[4];");
10161   verifyFormat("funptrs[4](foo)[4];");
10162   verifyFormat("void f(int *);");
10163   verifyFormat("void f(int *) = 0;");
10164   verifyFormat("void f(SmallVector<int>) {}");
10165   verifyFormat("void f(SmallVector<int>);");
10166   verifyFormat("void f(SmallVector<int>) = 0;");
10167   verifyFormat("void f(int i = (kA * kB) & kMask) {}");
10168   verifyFormat("int a = sizeof(int) * b;");
10169   verifyFormat("int a = alignof(int) * b;", getGoogleStyle());
10170   verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
10171   verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
10172   verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
10173 
10174   // These are not casts, but at some point were confused with casts.
10175   verifyFormat("virtual void foo(int *) override;");
10176   verifyFormat("virtual void foo(char &) const;");
10177   verifyFormat("virtual void foo(int *a, char *) const;");
10178   verifyFormat("int a = sizeof(int *) + b;");
10179   verifyFormat("int a = alignof(int *) + b;", getGoogleStyle());
10180   verifyFormat("bool b = f(g<int>) && c;");
10181   verifyFormat("typedef void (*f)(int i) func;");
10182   verifyFormat("void operator++(int) noexcept;");
10183   verifyFormat("void operator++(int &) noexcept;");
10184   verifyFormat("void operator delete(void *, std::size_t, const std::nothrow_t "
10185                "&) noexcept;");
10186   verifyFormat(
10187       "void operator delete(std::size_t, const std::nothrow_t &) noexcept;");
10188   verifyFormat("void operator delete(const std::nothrow_t &) noexcept;");
10189   verifyFormat("void operator delete(std::nothrow_t &) noexcept;");
10190   verifyFormat("void operator delete(nothrow_t &) noexcept;");
10191   verifyFormat("void operator delete(foo &) noexcept;");
10192   verifyFormat("void operator delete(foo) noexcept;");
10193   verifyFormat("void operator delete(int) noexcept;");
10194   verifyFormat("void operator delete(int &) noexcept;");
10195   verifyFormat("void operator delete(int &) volatile noexcept;");
10196   verifyFormat("void operator delete(int &) const");
10197   verifyFormat("void operator delete(int &) = default");
10198   verifyFormat("void operator delete(int &) = delete");
10199   verifyFormat("void operator delete(int &) [[noreturn]]");
10200   verifyFormat("void operator delete(int &) throw();");
10201   verifyFormat("void operator delete(int &) throw(int);");
10202   verifyFormat("auto operator delete(int &) -> int;");
10203   verifyFormat("auto operator delete(int &) override");
10204   verifyFormat("auto operator delete(int &) final");
10205 
10206   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
10207                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
10208   // FIXME: The indentation here is not ideal.
10209   verifyFormat(
10210       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10211       "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
10212       "        [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
10213 }
10214 
10215 TEST_F(FormatTest, FormatsFunctionTypes) {
10216   verifyFormat("A<bool()> a;");
10217   verifyFormat("A<SomeType()> a;");
10218   verifyFormat("A<void (*)(int, std::string)> a;");
10219   verifyFormat("A<void *(int)>;");
10220   verifyFormat("void *(*a)(int *, SomeType *);");
10221   verifyFormat("int (*func)(void *);");
10222   verifyFormat("void f() { int (*func)(void *); }");
10223   verifyFormat("template <class CallbackClass>\n"
10224                "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
10225 
10226   verifyGoogleFormat("A<void*(int*, SomeType*)>;");
10227   verifyGoogleFormat("void* (*a)(int);");
10228   verifyGoogleFormat(
10229       "template <class CallbackClass>\n"
10230       "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
10231 
10232   // Other constructs can look somewhat like function types:
10233   verifyFormat("A<sizeof(*x)> a;");
10234   verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
10235   verifyFormat("some_var = function(*some_pointer_var)[0];");
10236   verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
10237   verifyFormat("int x = f(&h)();");
10238   verifyFormat("returnsFunction(&param1, &param2)(param);");
10239   verifyFormat("std::function<\n"
10240                "    LooooooooooongTemplatedType<\n"
10241                "        SomeType>*(\n"
10242                "        LooooooooooooooooongType type)>\n"
10243                "    function;",
10244                getGoogleStyleWithColumns(40));
10245 }
10246 
10247 TEST_F(FormatTest, FormatsPointersToArrayTypes) {
10248   verifyFormat("A (*foo_)[6];");
10249   verifyFormat("vector<int> (*foo_)[6];");
10250 }
10251 
10252 TEST_F(FormatTest, BreaksLongVariableDeclarations) {
10253   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
10254                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
10255   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
10256                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
10257   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
10258                "    *LoooooooooooooooooooooooooooooooooooooooongVariable;");
10259 
10260   // Different ways of ()-initializiation.
10261   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
10262                "    LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
10263   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
10264                "    LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
10265   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
10266                "    LoooooooooooooooooooooooooooooooooooooooongVariable({});");
10267   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
10268                "    LoooooooooooooooooooooooooooooooooooooongVariable([A a]);");
10269 
10270   // Lambdas should not confuse the variable declaration heuristic.
10271   verifyFormat("LooooooooooooooooongType\n"
10272                "    variable(nullptr, [](A *a) {});",
10273                getLLVMStyleWithColumns(40));
10274 }
10275 
10276 TEST_F(FormatTest, BreaksLongDeclarations) {
10277   verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
10278                "    AnotherNameForTheLongType;");
10279   verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
10280                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10281   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10282                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
10283   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n"
10284                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
10285   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10286                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10287   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
10288                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10289   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
10290                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10291   verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
10292                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10293   verifyFormat("typeof(LoooooooooooooooooooooooooooooooooooooooooongName)\n"
10294                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10295   verifyFormat("_Atomic(LooooooooooooooooooooooooooooooooooooooooongName)\n"
10296                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10297   verifyFormat("__underlying_type(LooooooooooooooooooooooooooooooongName)\n"
10298                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10299   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10300                "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);");
10301   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10302                "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}");
10303   FormatStyle Indented = getLLVMStyle();
10304   Indented.IndentWrappedFunctionNames = true;
10305   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10306                "    LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
10307                Indented);
10308   verifyFormat(
10309       "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10310       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
10311       Indented);
10312   verifyFormat(
10313       "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
10314       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
10315       Indented);
10316   verifyFormat(
10317       "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
10318       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
10319       Indented);
10320 
10321   // FIXME: Without the comment, this breaks after "(".
10322   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType  // break\n"
10323                "    (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();",
10324                getGoogleStyle());
10325 
10326   verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
10327                "                  int LoooooooooooooooooooongParam2) {}");
10328   verifyFormat(
10329       "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
10330       "                                   SourceLocation L, IdentifierIn *II,\n"
10331       "                                   Type *T) {}");
10332   verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
10333                "ReallyReaaallyLongFunctionName(\n"
10334                "    const std::string &SomeParameter,\n"
10335                "    const SomeType<string, SomeOtherTemplateParameter>\n"
10336                "        &ReallyReallyLongParameterName,\n"
10337                "    const SomeType<string, SomeOtherTemplateParameter>\n"
10338                "        &AnotherLongParameterName) {}");
10339   verifyFormat("template <typename A>\n"
10340                "SomeLoooooooooooooooooooooongType<\n"
10341                "    typename some_namespace::SomeOtherType<A>::Type>\n"
10342                "Function() {}");
10343 
10344   verifyGoogleFormat(
10345       "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
10346       "    aaaaaaaaaaaaaaaaaaaaaaa;");
10347   verifyGoogleFormat(
10348       "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
10349       "                                   SourceLocation L) {}");
10350   verifyGoogleFormat(
10351       "some_namespace::LongReturnType\n"
10352       "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
10353       "    int first_long_parameter, int second_parameter) {}");
10354 
10355   verifyGoogleFormat("template <typename T>\n"
10356                      "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
10357                      "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
10358   verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10359                      "                   int aaaaaaaaaaaaaaaaaaaaaaa);");
10360 
10361   verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
10362                "    const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10363                "        *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
10364   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10365                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
10366                "        aaaaaaaaaaaaaaaaaaaaaaaa);");
10367   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10368                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
10369                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n"
10370                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
10371 
10372   verifyFormat("template <typename T> // Templates on own line.\n"
10373                "static int            // Some comment.\n"
10374                "MyFunction(int a);",
10375                getLLVMStyle());
10376 }
10377 
10378 TEST_F(FormatTest, FormatsAccessModifiers) {
10379   FormatStyle Style = getLLVMStyle();
10380   EXPECT_EQ(Style.EmptyLineBeforeAccessModifier,
10381             FormatStyle::ELBAMS_LogicalBlock);
10382   verifyFormat("struct foo {\n"
10383                "private:\n"
10384                "  void f() {}\n"
10385                "\n"
10386                "private:\n"
10387                "  int i;\n"
10388                "\n"
10389                "protected:\n"
10390                "  int j;\n"
10391                "};\n",
10392                Style);
10393   verifyFormat("struct foo {\n"
10394                "private:\n"
10395                "  void f() {}\n"
10396                "\n"
10397                "private:\n"
10398                "  int i;\n"
10399                "\n"
10400                "protected:\n"
10401                "  int j;\n"
10402                "};\n",
10403                "struct foo {\n"
10404                "private:\n"
10405                "  void f() {}\n"
10406                "private:\n"
10407                "  int i;\n"
10408                "protected:\n"
10409                "  int j;\n"
10410                "};\n",
10411                Style);
10412   verifyFormat("struct foo { /* comment */\n"
10413                "private:\n"
10414                "  int i;\n"
10415                "  // comment\n"
10416                "private:\n"
10417                "  int j;\n"
10418                "};\n",
10419                Style);
10420   verifyFormat("struct foo {\n"
10421                "#ifdef FOO\n"
10422                "#endif\n"
10423                "private:\n"
10424                "  int i;\n"
10425                "#ifdef FOO\n"
10426                "private:\n"
10427                "#endif\n"
10428                "  int j;\n"
10429                "};\n",
10430                Style);
10431   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
10432   verifyFormat("struct foo {\n"
10433                "private:\n"
10434                "  void f() {}\n"
10435                "private:\n"
10436                "  int i;\n"
10437                "protected:\n"
10438                "  int j;\n"
10439                "};\n",
10440                Style);
10441   verifyFormat("struct foo {\n"
10442                "private:\n"
10443                "  void f() {}\n"
10444                "private:\n"
10445                "  int i;\n"
10446                "protected:\n"
10447                "  int j;\n"
10448                "};\n",
10449                "struct foo {\n"
10450                "\n"
10451                "private:\n"
10452                "  void f() {}\n"
10453                "\n"
10454                "private:\n"
10455                "  int i;\n"
10456                "\n"
10457                "protected:\n"
10458                "  int j;\n"
10459                "};\n",
10460                Style);
10461   verifyFormat("struct foo { /* comment */\n"
10462                "private:\n"
10463                "  int i;\n"
10464                "  // comment\n"
10465                "private:\n"
10466                "  int j;\n"
10467                "};\n",
10468                "struct foo { /* comment */\n"
10469                "\n"
10470                "private:\n"
10471                "  int i;\n"
10472                "  // comment\n"
10473                "\n"
10474                "private:\n"
10475                "  int j;\n"
10476                "};\n",
10477                Style);
10478   verifyFormat("struct foo {\n"
10479                "#ifdef FOO\n"
10480                "#endif\n"
10481                "private:\n"
10482                "  int i;\n"
10483                "#ifdef FOO\n"
10484                "private:\n"
10485                "#endif\n"
10486                "  int j;\n"
10487                "};\n",
10488                "struct foo {\n"
10489                "#ifdef FOO\n"
10490                "#endif\n"
10491                "\n"
10492                "private:\n"
10493                "  int i;\n"
10494                "#ifdef FOO\n"
10495                "\n"
10496                "private:\n"
10497                "#endif\n"
10498                "  int j;\n"
10499                "};\n",
10500                Style);
10501   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
10502   verifyFormat("struct foo {\n"
10503                "private:\n"
10504                "  void f() {}\n"
10505                "\n"
10506                "private:\n"
10507                "  int i;\n"
10508                "\n"
10509                "protected:\n"
10510                "  int j;\n"
10511                "};\n",
10512                Style);
10513   verifyFormat("struct foo {\n"
10514                "private:\n"
10515                "  void f() {}\n"
10516                "\n"
10517                "private:\n"
10518                "  int i;\n"
10519                "\n"
10520                "protected:\n"
10521                "  int j;\n"
10522                "};\n",
10523                "struct foo {\n"
10524                "private:\n"
10525                "  void f() {}\n"
10526                "private:\n"
10527                "  int i;\n"
10528                "protected:\n"
10529                "  int j;\n"
10530                "};\n",
10531                Style);
10532   verifyFormat("struct foo { /* comment */\n"
10533                "private:\n"
10534                "  int i;\n"
10535                "  // comment\n"
10536                "\n"
10537                "private:\n"
10538                "  int j;\n"
10539                "};\n",
10540                "struct foo { /* comment */\n"
10541                "private:\n"
10542                "  int i;\n"
10543                "  // comment\n"
10544                "\n"
10545                "private:\n"
10546                "  int j;\n"
10547                "};\n",
10548                Style);
10549   verifyFormat("struct foo {\n"
10550                "#ifdef FOO\n"
10551                "#endif\n"
10552                "\n"
10553                "private:\n"
10554                "  int i;\n"
10555                "#ifdef FOO\n"
10556                "\n"
10557                "private:\n"
10558                "#endif\n"
10559                "  int j;\n"
10560                "};\n",
10561                "struct foo {\n"
10562                "#ifdef FOO\n"
10563                "#endif\n"
10564                "private:\n"
10565                "  int i;\n"
10566                "#ifdef FOO\n"
10567                "private:\n"
10568                "#endif\n"
10569                "  int j;\n"
10570                "};\n",
10571                Style);
10572   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
10573   EXPECT_EQ("struct foo {\n"
10574             "\n"
10575             "private:\n"
10576             "  void f() {}\n"
10577             "\n"
10578             "private:\n"
10579             "  int i;\n"
10580             "\n"
10581             "protected:\n"
10582             "  int j;\n"
10583             "};\n",
10584             format("struct foo {\n"
10585                    "\n"
10586                    "private:\n"
10587                    "  void f() {}\n"
10588                    "\n"
10589                    "private:\n"
10590                    "  int i;\n"
10591                    "\n"
10592                    "protected:\n"
10593                    "  int j;\n"
10594                    "};\n",
10595                    Style));
10596   verifyFormat("struct foo {\n"
10597                "private:\n"
10598                "  void f() {}\n"
10599                "private:\n"
10600                "  int i;\n"
10601                "protected:\n"
10602                "  int j;\n"
10603                "};\n",
10604                Style);
10605   EXPECT_EQ("struct foo { /* comment */\n"
10606             "\n"
10607             "private:\n"
10608             "  int i;\n"
10609             "  // comment\n"
10610             "\n"
10611             "private:\n"
10612             "  int j;\n"
10613             "};\n",
10614             format("struct foo { /* comment */\n"
10615                    "\n"
10616                    "private:\n"
10617                    "  int i;\n"
10618                    "  // comment\n"
10619                    "\n"
10620                    "private:\n"
10621                    "  int j;\n"
10622                    "};\n",
10623                    Style));
10624   verifyFormat("struct foo { /* comment */\n"
10625                "private:\n"
10626                "  int i;\n"
10627                "  // comment\n"
10628                "private:\n"
10629                "  int j;\n"
10630                "};\n",
10631                Style);
10632   EXPECT_EQ("struct foo {\n"
10633             "#ifdef FOO\n"
10634             "#endif\n"
10635             "\n"
10636             "private:\n"
10637             "  int i;\n"
10638             "#ifdef FOO\n"
10639             "\n"
10640             "private:\n"
10641             "#endif\n"
10642             "  int j;\n"
10643             "};\n",
10644             format("struct foo {\n"
10645                    "#ifdef FOO\n"
10646                    "#endif\n"
10647                    "\n"
10648                    "private:\n"
10649                    "  int i;\n"
10650                    "#ifdef FOO\n"
10651                    "\n"
10652                    "private:\n"
10653                    "#endif\n"
10654                    "  int j;\n"
10655                    "};\n",
10656                    Style));
10657   verifyFormat("struct foo {\n"
10658                "#ifdef FOO\n"
10659                "#endif\n"
10660                "private:\n"
10661                "  int i;\n"
10662                "#ifdef FOO\n"
10663                "private:\n"
10664                "#endif\n"
10665                "  int j;\n"
10666                "};\n",
10667                Style);
10668 
10669   FormatStyle NoEmptyLines = getLLVMStyle();
10670   NoEmptyLines.MaxEmptyLinesToKeep = 0;
10671   verifyFormat("struct foo {\n"
10672                "private:\n"
10673                "  void f() {}\n"
10674                "\n"
10675                "private:\n"
10676                "  int i;\n"
10677                "\n"
10678                "public:\n"
10679                "protected:\n"
10680                "  int j;\n"
10681                "};\n",
10682                NoEmptyLines);
10683 
10684   NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
10685   verifyFormat("struct foo {\n"
10686                "private:\n"
10687                "  void f() {}\n"
10688                "private:\n"
10689                "  int i;\n"
10690                "public:\n"
10691                "protected:\n"
10692                "  int j;\n"
10693                "};\n",
10694                NoEmptyLines);
10695 
10696   NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
10697   verifyFormat("struct foo {\n"
10698                "private:\n"
10699                "  void f() {}\n"
10700                "\n"
10701                "private:\n"
10702                "  int i;\n"
10703                "\n"
10704                "public:\n"
10705                "\n"
10706                "protected:\n"
10707                "  int j;\n"
10708                "};\n",
10709                NoEmptyLines);
10710 }
10711 
10712 TEST_F(FormatTest, FormatsAfterAccessModifiers) {
10713 
10714   FormatStyle Style = getLLVMStyle();
10715   EXPECT_EQ(Style.EmptyLineAfterAccessModifier, FormatStyle::ELAAMS_Never);
10716   verifyFormat("struct foo {\n"
10717                "private:\n"
10718                "  void f() {}\n"
10719                "\n"
10720                "private:\n"
10721                "  int i;\n"
10722                "\n"
10723                "protected:\n"
10724                "  int j;\n"
10725                "};\n",
10726                Style);
10727 
10728   // Check if lines are removed.
10729   verifyFormat("struct foo {\n"
10730                "private:\n"
10731                "  void f() {}\n"
10732                "\n"
10733                "private:\n"
10734                "  int i;\n"
10735                "\n"
10736                "protected:\n"
10737                "  int j;\n"
10738                "};\n",
10739                "struct foo {\n"
10740                "private:\n"
10741                "\n"
10742                "  void f() {}\n"
10743                "\n"
10744                "private:\n"
10745                "\n"
10746                "  int i;\n"
10747                "\n"
10748                "protected:\n"
10749                "\n"
10750                "  int j;\n"
10751                "};\n",
10752                Style);
10753 
10754   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
10755   verifyFormat("struct foo {\n"
10756                "private:\n"
10757                "\n"
10758                "  void f() {}\n"
10759                "\n"
10760                "private:\n"
10761                "\n"
10762                "  int i;\n"
10763                "\n"
10764                "protected:\n"
10765                "\n"
10766                "  int j;\n"
10767                "};\n",
10768                Style);
10769 
10770   // Check if lines are added.
10771   verifyFormat("struct foo {\n"
10772                "private:\n"
10773                "\n"
10774                "  void f() {}\n"
10775                "\n"
10776                "private:\n"
10777                "\n"
10778                "  int i;\n"
10779                "\n"
10780                "protected:\n"
10781                "\n"
10782                "  int j;\n"
10783                "};\n",
10784                "struct foo {\n"
10785                "private:\n"
10786                "  void f() {}\n"
10787                "\n"
10788                "private:\n"
10789                "  int i;\n"
10790                "\n"
10791                "protected:\n"
10792                "  int j;\n"
10793                "};\n",
10794                Style);
10795 
10796   // Leave tests rely on the code layout, test::messUp can not be used.
10797   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
10798   Style.MaxEmptyLinesToKeep = 0u;
10799   verifyFormat("struct foo {\n"
10800                "private:\n"
10801                "  void f() {}\n"
10802                "\n"
10803                "private:\n"
10804                "  int i;\n"
10805                "\n"
10806                "protected:\n"
10807                "  int j;\n"
10808                "};\n",
10809                Style);
10810 
10811   // Check if MaxEmptyLinesToKeep is respected.
10812   EXPECT_EQ("struct foo {\n"
10813             "private:\n"
10814             "  void f() {}\n"
10815             "\n"
10816             "private:\n"
10817             "  int i;\n"
10818             "\n"
10819             "protected:\n"
10820             "  int j;\n"
10821             "};\n",
10822             format("struct foo {\n"
10823                    "private:\n"
10824                    "\n\n\n"
10825                    "  void f() {}\n"
10826                    "\n"
10827                    "private:\n"
10828                    "\n\n\n"
10829                    "  int i;\n"
10830                    "\n"
10831                    "protected:\n"
10832                    "\n\n\n"
10833                    "  int j;\n"
10834                    "};\n",
10835                    Style));
10836 
10837   Style.MaxEmptyLinesToKeep = 1u;
10838   EXPECT_EQ("struct foo {\n"
10839             "private:\n"
10840             "\n"
10841             "  void f() {}\n"
10842             "\n"
10843             "private:\n"
10844             "\n"
10845             "  int i;\n"
10846             "\n"
10847             "protected:\n"
10848             "\n"
10849             "  int j;\n"
10850             "};\n",
10851             format("struct foo {\n"
10852                    "private:\n"
10853                    "\n"
10854                    "  void f() {}\n"
10855                    "\n"
10856                    "private:\n"
10857                    "\n"
10858                    "  int i;\n"
10859                    "\n"
10860                    "protected:\n"
10861                    "\n"
10862                    "  int j;\n"
10863                    "};\n",
10864                    Style));
10865   // Check if no lines are kept.
10866   EXPECT_EQ("struct foo {\n"
10867             "private:\n"
10868             "  void f() {}\n"
10869             "\n"
10870             "private:\n"
10871             "  int i;\n"
10872             "\n"
10873             "protected:\n"
10874             "  int j;\n"
10875             "};\n",
10876             format("struct foo {\n"
10877                    "private:\n"
10878                    "  void f() {}\n"
10879                    "\n"
10880                    "private:\n"
10881                    "  int i;\n"
10882                    "\n"
10883                    "protected:\n"
10884                    "  int j;\n"
10885                    "};\n",
10886                    Style));
10887   // Check if MaxEmptyLinesToKeep is respected.
10888   EXPECT_EQ("struct foo {\n"
10889             "private:\n"
10890             "\n"
10891             "  void f() {}\n"
10892             "\n"
10893             "private:\n"
10894             "\n"
10895             "  int i;\n"
10896             "\n"
10897             "protected:\n"
10898             "\n"
10899             "  int j;\n"
10900             "};\n",
10901             format("struct foo {\n"
10902                    "private:\n"
10903                    "\n\n\n"
10904                    "  void f() {}\n"
10905                    "\n"
10906                    "private:\n"
10907                    "\n\n\n"
10908                    "  int i;\n"
10909                    "\n"
10910                    "protected:\n"
10911                    "\n\n\n"
10912                    "  int j;\n"
10913                    "};\n",
10914                    Style));
10915 
10916   Style.MaxEmptyLinesToKeep = 10u;
10917   EXPECT_EQ("struct foo {\n"
10918             "private:\n"
10919             "\n\n\n"
10920             "  void f() {}\n"
10921             "\n"
10922             "private:\n"
10923             "\n\n\n"
10924             "  int i;\n"
10925             "\n"
10926             "protected:\n"
10927             "\n\n\n"
10928             "  int j;\n"
10929             "};\n",
10930             format("struct foo {\n"
10931                    "private:\n"
10932                    "\n\n\n"
10933                    "  void f() {}\n"
10934                    "\n"
10935                    "private:\n"
10936                    "\n\n\n"
10937                    "  int i;\n"
10938                    "\n"
10939                    "protected:\n"
10940                    "\n\n\n"
10941                    "  int j;\n"
10942                    "};\n",
10943                    Style));
10944 
10945   // Test with comments.
10946   Style = getLLVMStyle();
10947   verifyFormat("struct foo {\n"
10948                "private:\n"
10949                "  // comment\n"
10950                "  void f() {}\n"
10951                "\n"
10952                "private: /* comment */\n"
10953                "  int i;\n"
10954                "};\n",
10955                Style);
10956   verifyFormat("struct foo {\n"
10957                "private:\n"
10958                "  // comment\n"
10959                "  void f() {}\n"
10960                "\n"
10961                "private: /* comment */\n"
10962                "  int i;\n"
10963                "};\n",
10964                "struct foo {\n"
10965                "private:\n"
10966                "\n"
10967                "  // comment\n"
10968                "  void f() {}\n"
10969                "\n"
10970                "private: /* comment */\n"
10971                "\n"
10972                "  int i;\n"
10973                "};\n",
10974                Style);
10975 
10976   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
10977   verifyFormat("struct foo {\n"
10978                "private:\n"
10979                "\n"
10980                "  // comment\n"
10981                "  void f() {}\n"
10982                "\n"
10983                "private: /* comment */\n"
10984                "\n"
10985                "  int i;\n"
10986                "};\n",
10987                "struct foo {\n"
10988                "private:\n"
10989                "  // comment\n"
10990                "  void f() {}\n"
10991                "\n"
10992                "private: /* comment */\n"
10993                "  int i;\n"
10994                "};\n",
10995                Style);
10996   verifyFormat("struct foo {\n"
10997                "private:\n"
10998                "\n"
10999                "  // comment\n"
11000                "  void f() {}\n"
11001                "\n"
11002                "private: /* comment */\n"
11003                "\n"
11004                "  int i;\n"
11005                "};\n",
11006                Style);
11007 
11008   // Test with preprocessor defines.
11009   Style = getLLVMStyle();
11010   verifyFormat("struct foo {\n"
11011                "private:\n"
11012                "#ifdef FOO\n"
11013                "#endif\n"
11014                "  void f() {}\n"
11015                "};\n",
11016                Style);
11017   verifyFormat("struct foo {\n"
11018                "private:\n"
11019                "#ifdef FOO\n"
11020                "#endif\n"
11021                "  void f() {}\n"
11022                "};\n",
11023                "struct foo {\n"
11024                "private:\n"
11025                "\n"
11026                "#ifdef FOO\n"
11027                "#endif\n"
11028                "  void f() {}\n"
11029                "};\n",
11030                Style);
11031 
11032   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
11033   verifyFormat("struct foo {\n"
11034                "private:\n"
11035                "\n"
11036                "#ifdef FOO\n"
11037                "#endif\n"
11038                "  void f() {}\n"
11039                "};\n",
11040                "struct foo {\n"
11041                "private:\n"
11042                "#ifdef FOO\n"
11043                "#endif\n"
11044                "  void f() {}\n"
11045                "};\n",
11046                Style);
11047   verifyFormat("struct foo {\n"
11048                "private:\n"
11049                "\n"
11050                "#ifdef FOO\n"
11051                "#endif\n"
11052                "  void f() {}\n"
11053                "};\n",
11054                Style);
11055 }
11056 
11057 TEST_F(FormatTest, FormatsAfterAndBeforeAccessModifiersInteraction) {
11058   // Combined tests of EmptyLineAfterAccessModifier and
11059   // EmptyLineBeforeAccessModifier.
11060   FormatStyle Style = getLLVMStyle();
11061   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
11062   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
11063   verifyFormat("struct foo {\n"
11064                "private:\n"
11065                "\n"
11066                "protected:\n"
11067                "};\n",
11068                Style);
11069 
11070   Style.MaxEmptyLinesToKeep = 10u;
11071   // Both remove all new lines.
11072   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
11073   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
11074   verifyFormat("struct foo {\n"
11075                "private:\n"
11076                "protected:\n"
11077                "};\n",
11078                "struct foo {\n"
11079                "private:\n"
11080                "\n\n\n"
11081                "protected:\n"
11082                "};\n",
11083                Style);
11084 
11085   // Leave tests rely on the code layout, test::messUp can not be used.
11086   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
11087   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
11088   Style.MaxEmptyLinesToKeep = 10u;
11089   EXPECT_EQ("struct foo {\n"
11090             "private:\n"
11091             "\n\n\n"
11092             "protected:\n"
11093             "};\n",
11094             format("struct foo {\n"
11095                    "private:\n"
11096                    "\n\n\n"
11097                    "protected:\n"
11098                    "};\n",
11099                    Style));
11100   Style.MaxEmptyLinesToKeep = 3u;
11101   EXPECT_EQ("struct foo {\n"
11102             "private:\n"
11103             "\n\n\n"
11104             "protected:\n"
11105             "};\n",
11106             format("struct foo {\n"
11107                    "private:\n"
11108                    "\n\n\n"
11109                    "protected:\n"
11110                    "};\n",
11111                    Style));
11112   Style.MaxEmptyLinesToKeep = 1u;
11113   EXPECT_EQ("struct foo {\n"
11114             "private:\n"
11115             "\n\n\n"
11116             "protected:\n"
11117             "};\n",
11118             format("struct foo {\n"
11119                    "private:\n"
11120                    "\n\n\n"
11121                    "protected:\n"
11122                    "};\n",
11123                    Style)); // Based on new lines in original document and not
11124                             // on the setting.
11125 
11126   Style.MaxEmptyLinesToKeep = 10u;
11127   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
11128   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
11129   // Newlines are kept if they are greater than zero,
11130   // test::messUp removes all new lines which changes the logic
11131   EXPECT_EQ("struct foo {\n"
11132             "private:\n"
11133             "\n\n\n"
11134             "protected:\n"
11135             "};\n",
11136             format("struct foo {\n"
11137                    "private:\n"
11138                    "\n\n\n"
11139                    "protected:\n"
11140                    "};\n",
11141                    Style));
11142 
11143   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
11144   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
11145   // test::messUp removes all new lines which changes the logic
11146   EXPECT_EQ("struct foo {\n"
11147             "private:\n"
11148             "\n\n\n"
11149             "protected:\n"
11150             "};\n",
11151             format("struct foo {\n"
11152                    "private:\n"
11153                    "\n\n\n"
11154                    "protected:\n"
11155                    "};\n",
11156                    Style));
11157 
11158   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
11159   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
11160   EXPECT_EQ("struct foo {\n"
11161             "private:\n"
11162             "\n\n\n"
11163             "protected:\n"
11164             "};\n",
11165             format("struct foo {\n"
11166                    "private:\n"
11167                    "\n\n\n"
11168                    "protected:\n"
11169                    "};\n",
11170                    Style)); // test::messUp removes all new lines which changes
11171                             // the logic.
11172 
11173   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
11174   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
11175   verifyFormat("struct foo {\n"
11176                "private:\n"
11177                "protected:\n"
11178                "};\n",
11179                "struct foo {\n"
11180                "private:\n"
11181                "\n\n\n"
11182                "protected:\n"
11183                "};\n",
11184                Style);
11185 
11186   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
11187   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
11188   EXPECT_EQ("struct foo {\n"
11189             "private:\n"
11190             "\n\n\n"
11191             "protected:\n"
11192             "};\n",
11193             format("struct foo {\n"
11194                    "private:\n"
11195                    "\n\n\n"
11196                    "protected:\n"
11197                    "};\n",
11198                    Style)); // test::messUp removes all new lines which changes
11199                             // the logic.
11200 
11201   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
11202   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
11203   verifyFormat("struct foo {\n"
11204                "private:\n"
11205                "protected:\n"
11206                "};\n",
11207                "struct foo {\n"
11208                "private:\n"
11209                "\n\n\n"
11210                "protected:\n"
11211                "};\n",
11212                Style);
11213 
11214   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
11215   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
11216   verifyFormat("struct foo {\n"
11217                "private:\n"
11218                "protected:\n"
11219                "};\n",
11220                "struct foo {\n"
11221                "private:\n"
11222                "\n\n\n"
11223                "protected:\n"
11224                "};\n",
11225                Style);
11226 
11227   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
11228   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
11229   verifyFormat("struct foo {\n"
11230                "private:\n"
11231                "protected:\n"
11232                "};\n",
11233                "struct foo {\n"
11234                "private:\n"
11235                "\n\n\n"
11236                "protected:\n"
11237                "};\n",
11238                Style);
11239 
11240   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
11241   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
11242   verifyFormat("struct foo {\n"
11243                "private:\n"
11244                "protected:\n"
11245                "};\n",
11246                "struct foo {\n"
11247                "private:\n"
11248                "\n\n\n"
11249                "protected:\n"
11250                "};\n",
11251                Style);
11252 }
11253 
11254 TEST_F(FormatTest, FormatsArrays) {
11255   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
11256                "                         [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
11257   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n"
11258                "                         [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;");
11259   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n"
11260                "    aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}");
11261   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
11262                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
11263   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
11264                "    [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
11265   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
11266                "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
11267                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
11268   verifyFormat(
11269       "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
11270       "             << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
11271       "                                  [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
11272   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n"
11273                "    .aaaaaaaaaaaaaaaaaaaaaa();");
11274 
11275   verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
11276                      "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
11277   verifyFormat(
11278       "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
11279       "                                  .aaaaaaa[0]\n"
11280       "                                  .aaaaaaaaaaaaaaaaaaaaaa();");
11281   verifyFormat("a[::b::c];");
11282 
11283   verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10));
11284 
11285   FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0);
11286   verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit);
11287 }
11288 
11289 TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
11290   verifyFormat("(a)->b();");
11291   verifyFormat("--a;");
11292 }
11293 
11294 TEST_F(FormatTest, HandlesIncludeDirectives) {
11295   verifyFormat("#include <string>\n"
11296                "#include <a/b/c.h>\n"
11297                "#include \"a/b/string\"\n"
11298                "#include \"string.h\"\n"
11299                "#include \"string.h\"\n"
11300                "#include <a-a>\n"
11301                "#include < path with space >\n"
11302                "#include_next <test.h>"
11303                "#include \"abc.h\" // this is included for ABC\n"
11304                "#include \"some long include\" // with a comment\n"
11305                "#include \"some very long include path\"\n"
11306                "#include <some/very/long/include/path>\n",
11307                getLLVMStyleWithColumns(35));
11308   EXPECT_EQ("#include \"a.h\"", format("#include  \"a.h\""));
11309   EXPECT_EQ("#include <a>", format("#include<a>"));
11310 
11311   verifyFormat("#import <string>");
11312   verifyFormat("#import <a/b/c.h>");
11313   verifyFormat("#import \"a/b/string\"");
11314   verifyFormat("#import \"string.h\"");
11315   verifyFormat("#import \"string.h\"");
11316   verifyFormat("#if __has_include(<strstream>)\n"
11317                "#include <strstream>\n"
11318                "#endif");
11319 
11320   verifyFormat("#define MY_IMPORT <a/b>");
11321 
11322   verifyFormat("#if __has_include(<a/b>)");
11323   verifyFormat("#if __has_include_next(<a/b>)");
11324   verifyFormat("#define F __has_include(<a/b>)");
11325   verifyFormat("#define F __has_include_next(<a/b>)");
11326 
11327   // Protocol buffer definition or missing "#".
11328   verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
11329                getLLVMStyleWithColumns(30));
11330 
11331   FormatStyle Style = getLLVMStyle();
11332   Style.AlwaysBreakBeforeMultilineStrings = true;
11333   Style.ColumnLimit = 0;
11334   verifyFormat("#import \"abc.h\"", Style);
11335 
11336   // But 'import' might also be a regular C++ namespace.
11337   verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
11338                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
11339 }
11340 
11341 //===----------------------------------------------------------------------===//
11342 // Error recovery tests.
11343 //===----------------------------------------------------------------------===//
11344 
11345 TEST_F(FormatTest, IncompleteParameterLists) {
11346   FormatStyle NoBinPacking = getLLVMStyle();
11347   NoBinPacking.BinPackParameters = false;
11348   verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
11349                "                        double *min_x,\n"
11350                "                        double *max_x,\n"
11351                "                        double *min_y,\n"
11352                "                        double *max_y,\n"
11353                "                        double *min_z,\n"
11354                "                        double *max_z, ) {}",
11355                NoBinPacking);
11356 }
11357 
11358 TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
11359   verifyFormat("void f() { return; }\n42");
11360   verifyFormat("void f() {\n"
11361                "  if (0)\n"
11362                "    return;\n"
11363                "}\n"
11364                "42");
11365   verifyFormat("void f() { return }\n42");
11366   verifyFormat("void f() {\n"
11367                "  if (0)\n"
11368                "    return\n"
11369                "}\n"
11370                "42");
11371 }
11372 
11373 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
11374   EXPECT_EQ("void f() { return }", format("void  f ( )  {  return  }"));
11375   EXPECT_EQ("void f() {\n"
11376             "  if (a)\n"
11377             "    return\n"
11378             "}",
11379             format("void  f  (  )  {  if  ( a )  return  }"));
11380   EXPECT_EQ("namespace N {\n"
11381             "void f()\n"
11382             "}",
11383             format("namespace  N  {  void f()  }"));
11384   EXPECT_EQ("namespace N {\n"
11385             "void f() {}\n"
11386             "void g()\n"
11387             "} // namespace N",
11388             format("namespace N  { void f( ) { } void g( ) }"));
11389 }
11390 
11391 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
11392   verifyFormat("int aaaaaaaa =\n"
11393                "    // Overlylongcomment\n"
11394                "    b;",
11395                getLLVMStyleWithColumns(20));
11396   verifyFormat("function(\n"
11397                "    ShortArgument,\n"
11398                "    LoooooooooooongArgument);\n",
11399                getLLVMStyleWithColumns(20));
11400 }
11401 
11402 TEST_F(FormatTest, IncorrectAccessSpecifier) {
11403   verifyFormat("public:");
11404   verifyFormat("class A {\n"
11405                "public\n"
11406                "  void f() {}\n"
11407                "};");
11408   verifyFormat("public\n"
11409                "int qwerty;");
11410   verifyFormat("public\n"
11411                "B {}");
11412   verifyFormat("public\n"
11413                "{}");
11414   verifyFormat("public\n"
11415                "B { int x; }");
11416 }
11417 
11418 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
11419   verifyFormat("{");
11420   verifyFormat("#})");
11421   verifyNoCrash("(/**/[:!] ?[).");
11422 }
11423 
11424 TEST_F(FormatTest, IncorrectUnbalancedBracesInMacrosWithUnicode) {
11425   // Found by oss-fuzz:
11426   // https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=8212
11427   FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp);
11428   Style.ColumnLimit = 60;
11429   verifyNoCrash(
11430       "\x23\x47\xff\x20\x28\xff\x3c\xff\x3f\xff\x20\x2f\x7b\x7a\xff\x20"
11431       "\xff\xff\xff\xca\xb5\xff\xff\xff\xff\x3a\x7b\x7d\xff\x20\xff\x20"
11432       "\xff\x74\xff\x20\x7d\x7d\xff\x7b\x3a\xff\x20\x71\xff\x20\xff\x0a",
11433       Style);
11434 }
11435 
11436 TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
11437   verifyFormat("do {\n}");
11438   verifyFormat("do {\n}\n"
11439                "f();");
11440   verifyFormat("do {\n}\n"
11441                "wheeee(fun);");
11442   verifyFormat("do {\n"
11443                "  f();\n"
11444                "}");
11445 }
11446 
11447 TEST_F(FormatTest, IncorrectCodeMissingParens) {
11448   verifyFormat("if {\n  foo;\n  foo();\n}");
11449   verifyFormat("switch {\n  foo;\n  foo();\n}");
11450   verifyIncompleteFormat("for {\n  foo;\n  foo();\n}");
11451   verifyFormat("while {\n  foo;\n  foo();\n}");
11452   verifyFormat("do {\n  foo;\n  foo();\n} while;");
11453 }
11454 
11455 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
11456   verifyIncompleteFormat("namespace {\n"
11457                          "class Foo { Foo (\n"
11458                          "};\n"
11459                          "} // namespace");
11460 }
11461 
11462 TEST_F(FormatTest, IncorrectCodeErrorDetection) {
11463   EXPECT_EQ("{\n  {}\n", format("{\n{\n}\n"));
11464   EXPECT_EQ("{\n  {}\n", format("{\n  {\n}\n"));
11465   EXPECT_EQ("{\n  {}\n", format("{\n  {\n  }\n"));
11466   EXPECT_EQ("{\n  {}\n}\n}\n", format("{\n  {\n    }\n  }\n}\n"));
11467 
11468   EXPECT_EQ("{\n"
11469             "  {\n"
11470             "    breakme(\n"
11471             "        qwe);\n"
11472             "  }\n",
11473             format("{\n"
11474                    "    {\n"
11475                    " breakme(qwe);\n"
11476                    "}\n",
11477                    getLLVMStyleWithColumns(10)));
11478 }
11479 
11480 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
11481   verifyFormat("int x = {\n"
11482                "    avariable,\n"
11483                "    b(alongervariable)};",
11484                getLLVMStyleWithColumns(25));
11485 }
11486 
11487 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
11488   verifyFormat("return (a)(b){1, 2, 3};");
11489 }
11490 
11491 TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
11492   verifyFormat("vector<int> x{1, 2, 3, 4};");
11493   verifyFormat("vector<int> x{\n"
11494                "    1,\n"
11495                "    2,\n"
11496                "    3,\n"
11497                "    4,\n"
11498                "};");
11499   verifyFormat("vector<T> x{{}, {}, {}, {}};");
11500   verifyFormat("f({1, 2});");
11501   verifyFormat("auto v = Foo{-1};");
11502   verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
11503   verifyFormat("Class::Class : member{1, 2, 3} {}");
11504   verifyFormat("new vector<int>{1, 2, 3};");
11505   verifyFormat("new int[3]{1, 2, 3};");
11506   verifyFormat("new int{1};");
11507   verifyFormat("return {arg1, arg2};");
11508   verifyFormat("return {arg1, SomeType{parameter}};");
11509   verifyFormat("int count = set<int>{f(), g(), h()}.size();");
11510   verifyFormat("new T{arg1, arg2};");
11511   verifyFormat("f(MyMap[{composite, key}]);");
11512   verifyFormat("class Class {\n"
11513                "  T member = {arg1, arg2};\n"
11514                "};");
11515   verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
11516   verifyFormat("const struct A a = {.a = 1, .b = 2};");
11517   verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
11518   verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
11519   verifyFormat("int a = std::is_integral<int>{} + 0;");
11520 
11521   verifyFormat("int foo(int i) { return fo1{}(i); }");
11522   verifyFormat("int foo(int i) { return fo1{}(i); }");
11523   verifyFormat("auto i = decltype(x){};");
11524   verifyFormat("auto i = typeof(x){};");
11525   verifyFormat("auto i = _Atomic(x){};");
11526   verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
11527   verifyFormat("Node n{1, Node{1000}, //\n"
11528                "       2};");
11529   verifyFormat("Aaaa aaaaaaa{\n"
11530                "    {\n"
11531                "        aaaa,\n"
11532                "    },\n"
11533                "};");
11534   verifyFormat("class C : public D {\n"
11535                "  SomeClass SC{2};\n"
11536                "};");
11537   verifyFormat("class C : public A {\n"
11538                "  class D : public B {\n"
11539                "    void f() { int i{2}; }\n"
11540                "  };\n"
11541                "};");
11542   verifyFormat("#define A {a, a},");
11543   // Don't confuse braced list initializers with compound statements.
11544   verifyFormat(
11545       "class A {\n"
11546       "  A() : a{} {}\n"
11547       "  A(int b) : b(b) {}\n"
11548       "  A(int a, int b) : a(a), bs{{bs...}} { f(); }\n"
11549       "  int a, b;\n"
11550       "  explicit Expr(const Scalar<Result> &x) : u{Constant<Result>{x}} {}\n"
11551       "  explicit Expr(Scalar<Result> &&x) : u{Constant<Result>{std::move(x)}} "
11552       "{}\n"
11553       "};");
11554 
11555   // Avoid breaking between equal sign and opening brace
11556   FormatStyle AvoidBreakingFirstArgument = getLLVMStyle();
11557   AvoidBreakingFirstArgument.PenaltyBreakBeforeFirstCallParameter = 200;
11558   verifyFormat("const std::unordered_map<std::string, int> MyHashTable =\n"
11559                "    {{\"aaaaaaaaaaaaaaaaaaaaa\", 0},\n"
11560                "     {\"bbbbbbbbbbbbbbbbbbbbb\", 1},\n"
11561                "     {\"ccccccccccccccccccccc\", 2}};",
11562                AvoidBreakingFirstArgument);
11563 
11564   // Binpacking only if there is no trailing comma
11565   verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n"
11566                "                      cccccccccc, dddddddddd};",
11567                getLLVMStyleWithColumns(50));
11568   verifyFormat("const Aaaaaa aaaaa = {\n"
11569                "    aaaaaaaaaaa,\n"
11570                "    bbbbbbbbbbb,\n"
11571                "    ccccccccccc,\n"
11572                "    ddddddddddd,\n"
11573                "};",
11574                getLLVMStyleWithColumns(50));
11575 
11576   // Cases where distinguising braced lists and blocks is hard.
11577   verifyFormat("vector<int> v{12} GUARDED_BY(mutex);");
11578   verifyFormat("void f() {\n"
11579                "  return; // comment\n"
11580                "}\n"
11581                "SomeType t;");
11582   verifyFormat("void f() {\n"
11583                "  if (a) {\n"
11584                "    f();\n"
11585                "  }\n"
11586                "}\n"
11587                "SomeType t;");
11588 
11589   // In combination with BinPackArguments = false.
11590   FormatStyle NoBinPacking = getLLVMStyle();
11591   NoBinPacking.BinPackArguments = false;
11592   verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
11593                "                      bbbbb,\n"
11594                "                      ccccc,\n"
11595                "                      ddddd,\n"
11596                "                      eeeee,\n"
11597                "                      ffffff,\n"
11598                "                      ggggg,\n"
11599                "                      hhhhhh,\n"
11600                "                      iiiiii,\n"
11601                "                      jjjjjj,\n"
11602                "                      kkkkkk};",
11603                NoBinPacking);
11604   verifyFormat("const Aaaaaa aaaaa = {\n"
11605                "    aaaaa,\n"
11606                "    bbbbb,\n"
11607                "    ccccc,\n"
11608                "    ddddd,\n"
11609                "    eeeee,\n"
11610                "    ffffff,\n"
11611                "    ggggg,\n"
11612                "    hhhhhh,\n"
11613                "    iiiiii,\n"
11614                "    jjjjjj,\n"
11615                "    kkkkkk,\n"
11616                "};",
11617                NoBinPacking);
11618   verifyFormat(
11619       "const Aaaaaa aaaaa = {\n"
11620       "    aaaaa,  bbbbb,  ccccc,  ddddd,  eeeee,  ffffff, ggggg, hhhhhh,\n"
11621       "    iiiiii, jjjjjj, kkkkkk, aaaaa,  bbbbb,  ccccc,  ddddd, eeeee,\n"
11622       "    ffffff, ggggg,  hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
11623       "};",
11624       NoBinPacking);
11625 
11626   NoBinPacking.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
11627   EXPECT_EQ("static uint8 CddDp83848Reg[] = {\n"
11628             "    CDDDP83848_BMCR_REGISTER,\n"
11629             "    CDDDP83848_BMSR_REGISTER,\n"
11630             "    CDDDP83848_RBR_REGISTER};",
11631             format("static uint8 CddDp83848Reg[] = {CDDDP83848_BMCR_REGISTER,\n"
11632                    "                                CDDDP83848_BMSR_REGISTER,\n"
11633                    "                                CDDDP83848_RBR_REGISTER};",
11634                    NoBinPacking));
11635 
11636   // FIXME: The alignment of these trailing comments might be bad. Then again,
11637   // this might be utterly useless in real code.
11638   verifyFormat("Constructor::Constructor()\n"
11639                "    : some_value{         //\n"
11640                "                 aaaaaaa, //\n"
11641                "                 bbbbbbb} {}");
11642 
11643   // In braced lists, the first comment is always assumed to belong to the
11644   // first element. Thus, it can be moved to the next or previous line as
11645   // appropriate.
11646   EXPECT_EQ("function({// First element:\n"
11647             "          1,\n"
11648             "          // Second element:\n"
11649             "          2});",
11650             format("function({\n"
11651                    "    // First element:\n"
11652                    "    1,\n"
11653                    "    // Second element:\n"
11654                    "    2});"));
11655   EXPECT_EQ("std::vector<int> MyNumbers{\n"
11656             "    // First element:\n"
11657             "    1,\n"
11658             "    // Second element:\n"
11659             "    2};",
11660             format("std::vector<int> MyNumbers{// First element:\n"
11661                    "                           1,\n"
11662                    "                           // Second element:\n"
11663                    "                           2};",
11664                    getLLVMStyleWithColumns(30)));
11665   // A trailing comma should still lead to an enforced line break and no
11666   // binpacking.
11667   EXPECT_EQ("vector<int> SomeVector = {\n"
11668             "    // aaa\n"
11669             "    1,\n"
11670             "    2,\n"
11671             "};",
11672             format("vector<int> SomeVector = { // aaa\n"
11673                    "    1, 2, };"));
11674 
11675   // C++11 brace initializer list l-braces should not be treated any differently
11676   // when breaking before lambda bodies is enabled
11677   FormatStyle BreakBeforeLambdaBody = getLLVMStyle();
11678   BreakBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
11679   BreakBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
11680   BreakBeforeLambdaBody.AlwaysBreakBeforeMultilineStrings = true;
11681   verifyFormat(
11682       "std::runtime_error{\n"
11683       "    \"Long string which will force a break onto the next line...\"};",
11684       BreakBeforeLambdaBody);
11685 
11686   FormatStyle ExtraSpaces = getLLVMStyle();
11687   ExtraSpaces.Cpp11BracedListStyle = false;
11688   ExtraSpaces.ColumnLimit = 75;
11689   verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
11690   verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
11691   verifyFormat("f({ 1, 2 });", ExtraSpaces);
11692   verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
11693   verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
11694   verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
11695   verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
11696   verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
11697   verifyFormat("return { arg1, arg2 };", ExtraSpaces);
11698   verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
11699   verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
11700   verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
11701   verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
11702   verifyFormat("class Class {\n"
11703                "  T member = { arg1, arg2 };\n"
11704                "};",
11705                ExtraSpaces);
11706   verifyFormat(
11707       "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
11708       "                                 aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
11709       "                  : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
11710       "                                 bbbbbbbbbbbbbbbbbbbb, bbbbb };",
11711       ExtraSpaces);
11712   verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
11713   verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
11714                ExtraSpaces);
11715   verifyFormat(
11716       "someFunction(OtherParam,\n"
11717       "             BracedList{ // comment 1 (Forcing interesting break)\n"
11718       "                         param1, param2,\n"
11719       "                         // comment 2\n"
11720       "                         param3, param4 });",
11721       ExtraSpaces);
11722   verifyFormat(
11723       "std::this_thread::sleep_for(\n"
11724       "    std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
11725       ExtraSpaces);
11726   verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n"
11727                "    aaaaaaa,\n"
11728                "    aaaaaaaaaa,\n"
11729                "    aaaaa,\n"
11730                "    aaaaaaaaaaaaaaa,\n"
11731                "    aaa,\n"
11732                "    aaaaaaaaaa,\n"
11733                "    a,\n"
11734                "    aaaaaaaaaaaaaaaaaaaaa,\n"
11735                "    aaaaaaaaaaaa,\n"
11736                "    aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
11737                "    aaaaaaa,\n"
11738                "    a};");
11739   verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
11740   verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces);
11741   verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces);
11742 
11743   // Avoid breaking between initializer/equal sign and opening brace
11744   ExtraSpaces.PenaltyBreakBeforeFirstCallParameter = 200;
11745   verifyFormat("const std::unordered_map<std::string, int> MyHashTable = {\n"
11746                "  { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n"
11747                "  { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n"
11748                "  { \"ccccccccccccccccccccc\", 2 }\n"
11749                "};",
11750                ExtraSpaces);
11751   verifyFormat("const std::unordered_map<std::string, int> MyHashTable{\n"
11752                "  { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n"
11753                "  { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n"
11754                "  { \"ccccccccccccccccccccc\", 2 }\n"
11755                "};",
11756                ExtraSpaces);
11757 
11758   FormatStyle SpaceBeforeBrace = getLLVMStyle();
11759   SpaceBeforeBrace.SpaceBeforeCpp11BracedList = true;
11760   verifyFormat("vector<int> x {1, 2, 3, 4};", SpaceBeforeBrace);
11761   verifyFormat("f({}, {{}, {}}, MyMap[{k, v}]);", SpaceBeforeBrace);
11762 
11763   FormatStyle SpaceBetweenBraces = getLLVMStyle();
11764   SpaceBetweenBraces.SpacesInAngles = FormatStyle::SIAS_Always;
11765   SpaceBetweenBraces.SpacesInParentheses = true;
11766   SpaceBetweenBraces.SpacesInSquareBrackets = true;
11767   verifyFormat("vector< int > x{ 1, 2, 3, 4 };", SpaceBetweenBraces);
11768   verifyFormat("f( {}, { {}, {} }, MyMap[ { k, v } ] );", SpaceBetweenBraces);
11769   verifyFormat("vector< int > x{ // comment 1\n"
11770                "                 1, 2, 3, 4 };",
11771                SpaceBetweenBraces);
11772   SpaceBetweenBraces.ColumnLimit = 20;
11773   EXPECT_EQ("vector< int > x{\n"
11774             "    1, 2, 3, 4 };",
11775             format("vector<int>x{1,2,3,4};", SpaceBetweenBraces));
11776   SpaceBetweenBraces.ColumnLimit = 24;
11777   EXPECT_EQ("vector< int > x{ 1, 2,\n"
11778             "                 3, 4 };",
11779             format("vector<int>x{1,2,3,4};", SpaceBetweenBraces));
11780   EXPECT_EQ("vector< int > x{\n"
11781             "    1,\n"
11782             "    2,\n"
11783             "    3,\n"
11784             "    4,\n"
11785             "};",
11786             format("vector<int>x{1,2,3,4,};", SpaceBetweenBraces));
11787   verifyFormat("vector< int > x{};", SpaceBetweenBraces);
11788   SpaceBetweenBraces.SpaceInEmptyParentheses = true;
11789   verifyFormat("vector< int > x{ };", SpaceBetweenBraces);
11790 }
11791 
11792 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
11793   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11794                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11795                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11796                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11797                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11798                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
11799   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n"
11800                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11801                "                 1, 22, 333, 4444, 55555, //\n"
11802                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11803                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
11804   verifyFormat(
11805       "vector<int> x = {1,       22, 333, 4444, 55555, 666666, 7777777,\n"
11806       "                 1,       22, 333, 4444, 55555, 666666, 7777777,\n"
11807       "                 1,       22, 333, 4444, 55555, 666666, // comment\n"
11808       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
11809       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
11810       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
11811       "                 7777777};");
11812   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
11813                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
11814                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
11815   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
11816                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
11817                "    // Separating comment.\n"
11818                "    X86::R8, X86::R9, X86::R10, X86::R11, 0};");
11819   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
11820                "    // Leading comment\n"
11821                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
11822                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
11823   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
11824                "                 1, 1, 1, 1};",
11825                getLLVMStyleWithColumns(39));
11826   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
11827                "                 1, 1, 1, 1};",
11828                getLLVMStyleWithColumns(38));
11829   verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
11830                "    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
11831                getLLVMStyleWithColumns(43));
11832   verifyFormat(
11833       "static unsigned SomeValues[10][3] = {\n"
11834       "    {1, 4, 0},  {4, 9, 0},  {4, 5, 9},  {8, 5, 4}, {1, 8, 4},\n"
11835       "    {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};");
11836   verifyFormat("static auto fields = new vector<string>{\n"
11837                "    \"aaaaaaaaaaaaa\",\n"
11838                "    \"aaaaaaaaaaaaa\",\n"
11839                "    \"aaaaaaaaaaaa\",\n"
11840                "    \"aaaaaaaaaaaaaa\",\n"
11841                "    \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
11842                "    \"aaaaaaaaaaaa\",\n"
11843                "    \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
11844                "};");
11845   verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};");
11846   verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n"
11847                "                 2, bbbbbbbbbbbbbbbbbbbbbb,\n"
11848                "                 3, cccccccccccccccccccccc};",
11849                getLLVMStyleWithColumns(60));
11850 
11851   // Trailing commas.
11852   verifyFormat("vector<int> x = {\n"
11853                "    1, 1, 1, 1, 1, 1, 1, 1,\n"
11854                "};",
11855                getLLVMStyleWithColumns(39));
11856   verifyFormat("vector<int> x = {\n"
11857                "    1, 1, 1, 1, 1, 1, 1, 1, //\n"
11858                "};",
11859                getLLVMStyleWithColumns(39));
11860   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
11861                "                 1, 1, 1, 1,\n"
11862                "                 /**/ /**/};",
11863                getLLVMStyleWithColumns(39));
11864 
11865   // Trailing comment in the first line.
11866   verifyFormat("vector<int> iiiiiiiiiiiiiii = {                      //\n"
11867                "    1111111111, 2222222222, 33333333333, 4444444444, //\n"
11868                "    111111111,  222222222,  3333333333,  444444444,  //\n"
11869                "    11111111,   22222222,   333333333,   44444444};");
11870   // Trailing comment in the last line.
11871   verifyFormat("int aaaaa[] = {\n"
11872                "    1, 2, 3, // comment\n"
11873                "    4, 5, 6  // comment\n"
11874                "};");
11875 
11876   // With nested lists, we should either format one item per line or all nested
11877   // lists one on line.
11878   // FIXME: For some nested lists, we can do better.
11879   verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
11880                "        {aaaaaaaaaaaaaaaaaaa},\n"
11881                "        {aaaaaaaaaaaaaaaaaaaaa},\n"
11882                "        {aaaaaaaaaaaaaaaaa}};",
11883                getLLVMStyleWithColumns(60));
11884   verifyFormat(
11885       "SomeStruct my_struct_array = {\n"
11886       "    {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
11887       "     aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
11888       "    {aaa, aaa},\n"
11889       "    {aaa, aaa},\n"
11890       "    {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
11891       "    {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
11892       "     aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
11893 
11894   // No column layout should be used here.
11895   verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
11896                "                   bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
11897 
11898   verifyNoCrash("a<,");
11899 
11900   // No braced initializer here.
11901   verifyFormat("void f() {\n"
11902                "  struct Dummy {};\n"
11903                "  f(v);\n"
11904                "}");
11905 
11906   // Long lists should be formatted in columns even if they are nested.
11907   verifyFormat(
11908       "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11909       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11910       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11911       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11912       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11913       "                          1, 22, 333, 4444, 55555, 666666, 7777777});");
11914 
11915   // Allow "single-column" layout even if that violates the column limit. There
11916   // isn't going to be a better way.
11917   verifyFormat("std::vector<int> a = {\n"
11918                "    aaaaaaaa,\n"
11919                "    aaaaaaaa,\n"
11920                "    aaaaaaaa,\n"
11921                "    aaaaaaaa,\n"
11922                "    aaaaaaaaaa,\n"
11923                "    aaaaaaaa,\n"
11924                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa};",
11925                getLLVMStyleWithColumns(30));
11926   verifyFormat("vector<int> aaaa = {\n"
11927                "    aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
11928                "    aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
11929                "    aaaaaa.aaaaaaa,\n"
11930                "    aaaaaa.aaaaaaa,\n"
11931                "    aaaaaa.aaaaaaa,\n"
11932                "    aaaaaa.aaaaaaa,\n"
11933                "};");
11934 
11935   // Don't create hanging lists.
11936   verifyFormat("someFunction(Param, {List1, List2,\n"
11937                "                     List3});",
11938                getLLVMStyleWithColumns(35));
11939   verifyFormat("someFunction(Param, Param,\n"
11940                "             {List1, List2,\n"
11941                "              List3});",
11942                getLLVMStyleWithColumns(35));
11943   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n"
11944                "                               aaaaaaaaaaaaaaaaaaaaaaa);");
11945 }
11946 
11947 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
11948   FormatStyle DoNotMerge = getLLVMStyle();
11949   DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
11950 
11951   verifyFormat("void f() { return 42; }");
11952   verifyFormat("void f() {\n"
11953                "  return 42;\n"
11954                "}",
11955                DoNotMerge);
11956   verifyFormat("void f() {\n"
11957                "  // Comment\n"
11958                "}");
11959   verifyFormat("{\n"
11960                "#error {\n"
11961                "  int a;\n"
11962                "}");
11963   verifyFormat("{\n"
11964                "  int a;\n"
11965                "#error {\n"
11966                "}");
11967   verifyFormat("void f() {} // comment");
11968   verifyFormat("void f() { int a; } // comment");
11969   verifyFormat("void f() {\n"
11970                "} // comment",
11971                DoNotMerge);
11972   verifyFormat("void f() {\n"
11973                "  int a;\n"
11974                "} // comment",
11975                DoNotMerge);
11976   verifyFormat("void f() {\n"
11977                "} // comment",
11978                getLLVMStyleWithColumns(15));
11979 
11980   verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
11981   verifyFormat("void f() {\n  return 42;\n}", getLLVMStyleWithColumns(22));
11982 
11983   verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
11984   verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
11985   verifyFormat("class C {\n"
11986                "  C()\n"
11987                "      : iiiiiiii(nullptr),\n"
11988                "        kkkkkkk(nullptr),\n"
11989                "        mmmmmmm(nullptr),\n"
11990                "        nnnnnnn(nullptr) {}\n"
11991                "};",
11992                getGoogleStyle());
11993 
11994   FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0);
11995   EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit));
11996   EXPECT_EQ("class C {\n"
11997             "  A() : b(0) {}\n"
11998             "};",
11999             format("class C{A():b(0){}};", NoColumnLimit));
12000   EXPECT_EQ("A()\n"
12001             "    : b(0) {\n"
12002             "}",
12003             format("A()\n:b(0)\n{\n}", NoColumnLimit));
12004 
12005   FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
12006   DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
12007       FormatStyle::SFS_None;
12008   EXPECT_EQ("A()\n"
12009             "    : b(0) {\n"
12010             "}",
12011             format("A():b(0){}", DoNotMergeNoColumnLimit));
12012   EXPECT_EQ("A()\n"
12013             "    : b(0) {\n"
12014             "}",
12015             format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit));
12016 
12017   verifyFormat("#define A          \\\n"
12018                "  void f() {       \\\n"
12019                "    int i;         \\\n"
12020                "  }",
12021                getLLVMStyleWithColumns(20));
12022   verifyFormat("#define A           \\\n"
12023                "  void f() { int i; }",
12024                getLLVMStyleWithColumns(21));
12025   verifyFormat("#define A            \\\n"
12026                "  void f() {         \\\n"
12027                "    int i;           \\\n"
12028                "  }                  \\\n"
12029                "  int j;",
12030                getLLVMStyleWithColumns(22));
12031   verifyFormat("#define A             \\\n"
12032                "  void f() { int i; } \\\n"
12033                "  int j;",
12034                getLLVMStyleWithColumns(23));
12035 }
12036 
12037 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) {
12038   FormatStyle MergeEmptyOnly = getLLVMStyle();
12039   MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
12040   verifyFormat("class C {\n"
12041                "  int f() {}\n"
12042                "};",
12043                MergeEmptyOnly);
12044   verifyFormat("class C {\n"
12045                "  int f() {\n"
12046                "    return 42;\n"
12047                "  }\n"
12048                "};",
12049                MergeEmptyOnly);
12050   verifyFormat("int f() {}", MergeEmptyOnly);
12051   verifyFormat("int f() {\n"
12052                "  return 42;\n"
12053                "}",
12054                MergeEmptyOnly);
12055 
12056   // Also verify behavior when BraceWrapping.AfterFunction = true
12057   MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
12058   MergeEmptyOnly.BraceWrapping.AfterFunction = true;
12059   verifyFormat("int f() {}", MergeEmptyOnly);
12060   verifyFormat("class C {\n"
12061                "  int f() {}\n"
12062                "};",
12063                MergeEmptyOnly);
12064 }
12065 
12066 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
12067   FormatStyle MergeInlineOnly = getLLVMStyle();
12068   MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
12069   verifyFormat("class C {\n"
12070                "  int f() { return 42; }\n"
12071                "};",
12072                MergeInlineOnly);
12073   verifyFormat("int f() {\n"
12074                "  return 42;\n"
12075                "}",
12076                MergeInlineOnly);
12077 
12078   // SFS_Inline implies SFS_Empty
12079   verifyFormat("class C {\n"
12080                "  int f() {}\n"
12081                "};",
12082                MergeInlineOnly);
12083   verifyFormat("int f() {}", MergeInlineOnly);
12084 
12085   // Also verify behavior when BraceWrapping.AfterFunction = true
12086   MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
12087   MergeInlineOnly.BraceWrapping.AfterFunction = true;
12088   verifyFormat("class C {\n"
12089                "  int f() { return 42; }\n"
12090                "};",
12091                MergeInlineOnly);
12092   verifyFormat("int f()\n"
12093                "{\n"
12094                "  return 42;\n"
12095                "}",
12096                MergeInlineOnly);
12097 
12098   // SFS_Inline implies SFS_Empty
12099   verifyFormat("int f() {}", MergeInlineOnly);
12100   verifyFormat("class C {\n"
12101                "  int f() {}\n"
12102                "};",
12103                MergeInlineOnly);
12104 }
12105 
12106 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) {
12107   FormatStyle MergeInlineOnly = getLLVMStyle();
12108   MergeInlineOnly.AllowShortFunctionsOnASingleLine =
12109       FormatStyle::SFS_InlineOnly;
12110   verifyFormat("class C {\n"
12111                "  int f() { return 42; }\n"
12112                "};",
12113                MergeInlineOnly);
12114   verifyFormat("int f() {\n"
12115                "  return 42;\n"
12116                "}",
12117                MergeInlineOnly);
12118 
12119   // SFS_InlineOnly does not imply SFS_Empty
12120   verifyFormat("class C {\n"
12121                "  int f() {}\n"
12122                "};",
12123                MergeInlineOnly);
12124   verifyFormat("int f() {\n"
12125                "}",
12126                MergeInlineOnly);
12127 
12128   // Also verify behavior when BraceWrapping.AfterFunction = true
12129   MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
12130   MergeInlineOnly.BraceWrapping.AfterFunction = true;
12131   verifyFormat("class C {\n"
12132                "  int f() { return 42; }\n"
12133                "};",
12134                MergeInlineOnly);
12135   verifyFormat("int f()\n"
12136                "{\n"
12137                "  return 42;\n"
12138                "}",
12139                MergeInlineOnly);
12140 
12141   // SFS_InlineOnly does not imply SFS_Empty
12142   verifyFormat("int f()\n"
12143                "{\n"
12144                "}",
12145                MergeInlineOnly);
12146   verifyFormat("class C {\n"
12147                "  int f() {}\n"
12148                "};",
12149                MergeInlineOnly);
12150 }
12151 
12152 TEST_F(FormatTest, SplitEmptyFunction) {
12153   FormatStyle Style = getLLVMStyleWithColumns(40);
12154   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
12155   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12156   Style.BraceWrapping.AfterFunction = true;
12157   Style.BraceWrapping.SplitEmptyFunction = false;
12158 
12159   verifyFormat("int f()\n"
12160                "{}",
12161                Style);
12162   verifyFormat("int f()\n"
12163                "{\n"
12164                "  return 42;\n"
12165                "}",
12166                Style);
12167   verifyFormat("int f()\n"
12168                "{\n"
12169                "  // some comment\n"
12170                "}",
12171                Style);
12172 
12173   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
12174   verifyFormat("int f() {}", Style);
12175   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
12176                "{}",
12177                Style);
12178   verifyFormat("int f()\n"
12179                "{\n"
12180                "  return 0;\n"
12181                "}",
12182                Style);
12183 
12184   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
12185   verifyFormat("class Foo {\n"
12186                "  int f() {}\n"
12187                "};\n",
12188                Style);
12189   verifyFormat("class Foo {\n"
12190                "  int f() { return 0; }\n"
12191                "};\n",
12192                Style);
12193   verifyFormat("class Foo {\n"
12194                "  int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
12195                "  {}\n"
12196                "};\n",
12197                Style);
12198   verifyFormat("class Foo {\n"
12199                "  int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
12200                "  {\n"
12201                "    return 0;\n"
12202                "  }\n"
12203                "};\n",
12204                Style);
12205 
12206   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
12207   verifyFormat("int f() {}", Style);
12208   verifyFormat("int f() { return 0; }", Style);
12209   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
12210                "{}",
12211                Style);
12212   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
12213                "{\n"
12214                "  return 0;\n"
12215                "}",
12216                Style);
12217 }
12218 
12219 TEST_F(FormatTest, SplitEmptyFunctionButNotRecord) {
12220   FormatStyle Style = getLLVMStyleWithColumns(40);
12221   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
12222   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12223   Style.BraceWrapping.AfterFunction = true;
12224   Style.BraceWrapping.SplitEmptyFunction = true;
12225   Style.BraceWrapping.SplitEmptyRecord = false;
12226 
12227   verifyFormat("class C {};", Style);
12228   verifyFormat("struct C {};", Style);
12229   verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
12230                "       int bbbbbbbbbbbbbbbbbbbbbbbb)\n"
12231                "{\n"
12232                "}",
12233                Style);
12234   verifyFormat("class C {\n"
12235                "  C()\n"
12236                "      : aaaaaaaaaaaaaaaaaaaaaaaaaaaa(),\n"
12237                "        bbbbbbbbbbbbbbbbbbb()\n"
12238                "  {\n"
12239                "  }\n"
12240                "  void\n"
12241                "  m(int aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
12242                "    int bbbbbbbbbbbbbbbbbbbbbbbb)\n"
12243                "  {\n"
12244                "  }\n"
12245                "};",
12246                Style);
12247 }
12248 
12249 TEST_F(FormatTest, KeepShortFunctionAfterPPElse) {
12250   FormatStyle Style = getLLVMStyle();
12251   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
12252   verifyFormat("#ifdef A\n"
12253                "int f() {}\n"
12254                "#else\n"
12255                "int g() {}\n"
12256                "#endif",
12257                Style);
12258 }
12259 
12260 TEST_F(FormatTest, SplitEmptyClass) {
12261   FormatStyle Style = getLLVMStyle();
12262   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12263   Style.BraceWrapping.AfterClass = true;
12264   Style.BraceWrapping.SplitEmptyRecord = false;
12265 
12266   verifyFormat("class Foo\n"
12267                "{};",
12268                Style);
12269   verifyFormat("/* something */ class Foo\n"
12270                "{};",
12271                Style);
12272   verifyFormat("template <typename X> class Foo\n"
12273                "{};",
12274                Style);
12275   verifyFormat("class Foo\n"
12276                "{\n"
12277                "  Foo();\n"
12278                "};",
12279                Style);
12280   verifyFormat("typedef class Foo\n"
12281                "{\n"
12282                "} Foo_t;",
12283                Style);
12284 
12285   Style.BraceWrapping.SplitEmptyRecord = true;
12286   Style.BraceWrapping.AfterStruct = true;
12287   verifyFormat("class rep\n"
12288                "{\n"
12289                "};",
12290                Style);
12291   verifyFormat("struct rep\n"
12292                "{\n"
12293                "};",
12294                Style);
12295   verifyFormat("template <typename T> class rep\n"
12296                "{\n"
12297                "};",
12298                Style);
12299   verifyFormat("template <typename T> struct rep\n"
12300                "{\n"
12301                "};",
12302                Style);
12303   verifyFormat("class rep\n"
12304                "{\n"
12305                "  int x;\n"
12306                "};",
12307                Style);
12308   verifyFormat("struct rep\n"
12309                "{\n"
12310                "  int x;\n"
12311                "};",
12312                Style);
12313   verifyFormat("template <typename T> class rep\n"
12314                "{\n"
12315                "  int x;\n"
12316                "};",
12317                Style);
12318   verifyFormat("template <typename T> struct rep\n"
12319                "{\n"
12320                "  int x;\n"
12321                "};",
12322                Style);
12323   verifyFormat("template <typename T> class rep // Foo\n"
12324                "{\n"
12325                "  int x;\n"
12326                "};",
12327                Style);
12328   verifyFormat("template <typename T> struct rep // Bar\n"
12329                "{\n"
12330                "  int x;\n"
12331                "};",
12332                Style);
12333 
12334   verifyFormat("template <typename T> class rep<T>\n"
12335                "{\n"
12336                "  int x;\n"
12337                "};",
12338                Style);
12339 
12340   verifyFormat("template <typename T> class rep<std::complex<T>>\n"
12341                "{\n"
12342                "  int x;\n"
12343                "};",
12344                Style);
12345   verifyFormat("template <typename T> class rep<std::complex<T>>\n"
12346                "{\n"
12347                "};",
12348                Style);
12349 
12350   verifyFormat("#include \"stdint.h\"\n"
12351                "namespace rep {}",
12352                Style);
12353   verifyFormat("#include <stdint.h>\n"
12354                "namespace rep {}",
12355                Style);
12356   verifyFormat("#include <stdint.h>\n"
12357                "namespace rep {}",
12358                "#include <stdint.h>\n"
12359                "namespace rep {\n"
12360                "\n"
12361                "\n"
12362                "}",
12363                Style);
12364 }
12365 
12366 TEST_F(FormatTest, SplitEmptyStruct) {
12367   FormatStyle Style = getLLVMStyle();
12368   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12369   Style.BraceWrapping.AfterStruct = true;
12370   Style.BraceWrapping.SplitEmptyRecord = false;
12371 
12372   verifyFormat("struct Foo\n"
12373                "{};",
12374                Style);
12375   verifyFormat("/* something */ struct Foo\n"
12376                "{};",
12377                Style);
12378   verifyFormat("template <typename X> struct Foo\n"
12379                "{};",
12380                Style);
12381   verifyFormat("struct Foo\n"
12382                "{\n"
12383                "  Foo();\n"
12384                "};",
12385                Style);
12386   verifyFormat("typedef struct Foo\n"
12387                "{\n"
12388                "} Foo_t;",
12389                Style);
12390   // typedef struct Bar {} Bar_t;
12391 }
12392 
12393 TEST_F(FormatTest, SplitEmptyUnion) {
12394   FormatStyle Style = getLLVMStyle();
12395   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12396   Style.BraceWrapping.AfterUnion = true;
12397   Style.BraceWrapping.SplitEmptyRecord = false;
12398 
12399   verifyFormat("union Foo\n"
12400                "{};",
12401                Style);
12402   verifyFormat("/* something */ union Foo\n"
12403                "{};",
12404                Style);
12405   verifyFormat("union Foo\n"
12406                "{\n"
12407                "  A,\n"
12408                "};",
12409                Style);
12410   verifyFormat("typedef union Foo\n"
12411                "{\n"
12412                "} Foo_t;",
12413                Style);
12414 }
12415 
12416 TEST_F(FormatTest, SplitEmptyNamespace) {
12417   FormatStyle Style = getLLVMStyle();
12418   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12419   Style.BraceWrapping.AfterNamespace = true;
12420   Style.BraceWrapping.SplitEmptyNamespace = false;
12421 
12422   verifyFormat("namespace Foo\n"
12423                "{};",
12424                Style);
12425   verifyFormat("/* something */ namespace Foo\n"
12426                "{};",
12427                Style);
12428   verifyFormat("inline namespace Foo\n"
12429                "{};",
12430                Style);
12431   verifyFormat("/* something */ inline namespace Foo\n"
12432                "{};",
12433                Style);
12434   verifyFormat("export namespace Foo\n"
12435                "{};",
12436                Style);
12437   verifyFormat("namespace Foo\n"
12438                "{\n"
12439                "void Bar();\n"
12440                "};",
12441                Style);
12442 }
12443 
12444 TEST_F(FormatTest, NeverMergeShortRecords) {
12445   FormatStyle Style = getLLVMStyle();
12446 
12447   verifyFormat("class Foo {\n"
12448                "  Foo();\n"
12449                "};",
12450                Style);
12451   verifyFormat("typedef class Foo {\n"
12452                "  Foo();\n"
12453                "} Foo_t;",
12454                Style);
12455   verifyFormat("struct Foo {\n"
12456                "  Foo();\n"
12457                "};",
12458                Style);
12459   verifyFormat("typedef struct Foo {\n"
12460                "  Foo();\n"
12461                "} Foo_t;",
12462                Style);
12463   verifyFormat("union Foo {\n"
12464                "  A,\n"
12465                "};",
12466                Style);
12467   verifyFormat("typedef union Foo {\n"
12468                "  A,\n"
12469                "} Foo_t;",
12470                Style);
12471   verifyFormat("namespace Foo {\n"
12472                "void Bar();\n"
12473                "};",
12474                Style);
12475 
12476   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12477   Style.BraceWrapping.AfterClass = true;
12478   Style.BraceWrapping.AfterStruct = true;
12479   Style.BraceWrapping.AfterUnion = true;
12480   Style.BraceWrapping.AfterNamespace = true;
12481   verifyFormat("class Foo\n"
12482                "{\n"
12483                "  Foo();\n"
12484                "};",
12485                Style);
12486   verifyFormat("typedef class Foo\n"
12487                "{\n"
12488                "  Foo();\n"
12489                "} Foo_t;",
12490                Style);
12491   verifyFormat("struct Foo\n"
12492                "{\n"
12493                "  Foo();\n"
12494                "};",
12495                Style);
12496   verifyFormat("typedef struct Foo\n"
12497                "{\n"
12498                "  Foo();\n"
12499                "} Foo_t;",
12500                Style);
12501   verifyFormat("union Foo\n"
12502                "{\n"
12503                "  A,\n"
12504                "};",
12505                Style);
12506   verifyFormat("typedef union Foo\n"
12507                "{\n"
12508                "  A,\n"
12509                "} Foo_t;",
12510                Style);
12511   verifyFormat("namespace Foo\n"
12512                "{\n"
12513                "void Bar();\n"
12514                "};",
12515                Style);
12516 }
12517 
12518 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
12519   // Elaborate type variable declarations.
12520   verifyFormat("struct foo a = {bar};\nint n;");
12521   verifyFormat("class foo a = {bar};\nint n;");
12522   verifyFormat("union foo a = {bar};\nint n;");
12523 
12524   // Elaborate types inside function definitions.
12525   verifyFormat("struct foo f() {}\nint n;");
12526   verifyFormat("class foo f() {}\nint n;");
12527   verifyFormat("union foo f() {}\nint n;");
12528 
12529   // Templates.
12530   verifyFormat("template <class X> void f() {}\nint n;");
12531   verifyFormat("template <struct X> void f() {}\nint n;");
12532   verifyFormat("template <union X> void f() {}\nint n;");
12533 
12534   // Actual definitions...
12535   verifyFormat("struct {\n} n;");
12536   verifyFormat(
12537       "template <template <class T, class Y>, class Z> class X {\n} n;");
12538   verifyFormat("union Z {\n  int n;\n} x;");
12539   verifyFormat("class MACRO Z {\n} n;");
12540   verifyFormat("class MACRO(X) Z {\n} n;");
12541   verifyFormat("class __attribute__(X) Z {\n} n;");
12542   verifyFormat("class __declspec(X) Z {\n} n;");
12543   verifyFormat("class A##B##C {\n} n;");
12544   verifyFormat("class alignas(16) Z {\n} n;");
12545   verifyFormat("class MACRO(X) alignas(16) Z {\n} n;");
12546   verifyFormat("class MACROA MACRO(X) Z {\n} n;");
12547 
12548   // Redefinition from nested context:
12549   verifyFormat("class A::B::C {\n} n;");
12550 
12551   // Template definitions.
12552   verifyFormat(
12553       "template <typename F>\n"
12554       "Matcher(const Matcher<F> &Other,\n"
12555       "        typename enable_if_c<is_base_of<F, T>::value &&\n"
12556       "                             !is_same<F, T>::value>::type * = 0)\n"
12557       "    : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
12558 
12559   // FIXME: This is still incorrectly handled at the formatter side.
12560   verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
12561   verifyFormat("int i = SomeFunction(a<b, a> b);");
12562 
12563   // FIXME:
12564   // This now gets parsed incorrectly as class definition.
12565   // verifyFormat("class A<int> f() {\n}\nint n;");
12566 
12567   // Elaborate types where incorrectly parsing the structural element would
12568   // break the indent.
12569   verifyFormat("if (true)\n"
12570                "  class X x;\n"
12571                "else\n"
12572                "  f();\n");
12573 
12574   // This is simply incomplete. Formatting is not important, but must not crash.
12575   verifyFormat("class A:");
12576 }
12577 
12578 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
12579   EXPECT_EQ("#error Leave     all         white!!!!! space* alone!\n",
12580             format("#error Leave     all         white!!!!! space* alone!\n"));
12581   EXPECT_EQ(
12582       "#warning Leave     all         white!!!!! space* alone!\n",
12583       format("#warning Leave     all         white!!!!! space* alone!\n"));
12584   EXPECT_EQ("#error 1", format("  #  error   1"));
12585   EXPECT_EQ("#warning 1", format("  #  warning 1"));
12586 }
12587 
12588 TEST_F(FormatTest, FormatHashIfExpressions) {
12589   verifyFormat("#if AAAA && BBBB");
12590   verifyFormat("#if (AAAA && BBBB)");
12591   verifyFormat("#elif (AAAA && BBBB)");
12592   // FIXME: Come up with a better indentation for #elif.
12593   verifyFormat(
12594       "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) &&  \\\n"
12595       "    defined(BBBBBBBB)\n"
12596       "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) &&  \\\n"
12597       "    defined(BBBBBBBB)\n"
12598       "#endif",
12599       getLLVMStyleWithColumns(65));
12600 }
12601 
12602 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
12603   FormatStyle AllowsMergedIf = getGoogleStyle();
12604   AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
12605       FormatStyle::SIS_WithoutElse;
12606   verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
12607   verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
12608   verifyFormat("if (true)\n#error E\n  return 42;", AllowsMergedIf);
12609   EXPECT_EQ("if (true) return 42;",
12610             format("if (true)\nreturn 42;", AllowsMergedIf));
12611   FormatStyle ShortMergedIf = AllowsMergedIf;
12612   ShortMergedIf.ColumnLimit = 25;
12613   verifyFormat("#define A \\\n"
12614                "  if (true) return 42;",
12615                ShortMergedIf);
12616   verifyFormat("#define A \\\n"
12617                "  f();    \\\n"
12618                "  if (true)\n"
12619                "#define B",
12620                ShortMergedIf);
12621   verifyFormat("#define A \\\n"
12622                "  f();    \\\n"
12623                "  if (true)\n"
12624                "g();",
12625                ShortMergedIf);
12626   verifyFormat("{\n"
12627                "#ifdef A\n"
12628                "  // Comment\n"
12629                "  if (true) continue;\n"
12630                "#endif\n"
12631                "  // Comment\n"
12632                "  if (true) continue;\n"
12633                "}",
12634                ShortMergedIf);
12635   ShortMergedIf.ColumnLimit = 33;
12636   verifyFormat("#define A \\\n"
12637                "  if constexpr (true) return 42;",
12638                ShortMergedIf);
12639   verifyFormat("#define A \\\n"
12640                "  if CONSTEXPR (true) return 42;",
12641                ShortMergedIf);
12642   ShortMergedIf.ColumnLimit = 29;
12643   verifyFormat("#define A                   \\\n"
12644                "  if (aaaaaaaaaa) return 1; \\\n"
12645                "  return 2;",
12646                ShortMergedIf);
12647   ShortMergedIf.ColumnLimit = 28;
12648   verifyFormat("#define A         \\\n"
12649                "  if (aaaaaaaaaa) \\\n"
12650                "    return 1;     \\\n"
12651                "  return 2;",
12652                ShortMergedIf);
12653   verifyFormat("#define A                \\\n"
12654                "  if constexpr (aaaaaaa) \\\n"
12655                "    return 1;            \\\n"
12656                "  return 2;",
12657                ShortMergedIf);
12658   verifyFormat("#define A                \\\n"
12659                "  if CONSTEXPR (aaaaaaa) \\\n"
12660                "    return 1;            \\\n"
12661                "  return 2;",
12662                ShortMergedIf);
12663 }
12664 
12665 TEST_F(FormatTest, FormatStarDependingOnContext) {
12666   verifyFormat("void f(int *a);");
12667   verifyFormat("void f() { f(fint * b); }");
12668   verifyFormat("class A {\n  void f(int *a);\n};");
12669   verifyFormat("class A {\n  int *a;\n};");
12670   verifyFormat("namespace a {\n"
12671                "namespace b {\n"
12672                "class A {\n"
12673                "  void f() {}\n"
12674                "  int *a;\n"
12675                "};\n"
12676                "} // namespace b\n"
12677                "} // namespace a");
12678 }
12679 
12680 TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
12681   verifyFormat("while");
12682   verifyFormat("operator");
12683 }
12684 
12685 TEST_F(FormatTest, SkipsDeeplyNestedLines) {
12686   // This code would be painfully slow to format if we didn't skip it.
12687   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
12688                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
12689                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
12690                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
12691                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
12692                    "A(1, 1)\n"
12693                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x
12694                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12695                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12696                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12697                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12698                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12699                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12700                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12701                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12702                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n");
12703   // Deeply nested part is untouched, rest is formatted.
12704   EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n",
12705             format(std::string("int    i;\n") + Code + "int    j;\n",
12706                    getLLVMStyle(), SC_ExpectIncomplete));
12707 }
12708 
12709 //===----------------------------------------------------------------------===//
12710 // Objective-C tests.
12711 //===----------------------------------------------------------------------===//
12712 
12713 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
12714   verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
12715   EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
12716             format("-(NSUInteger)indexOfObject:(id)anObject;"));
12717   EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
12718   EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
12719   EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
12720             format("-(NSInteger)Method3:(id)anObject;"));
12721   EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
12722             format("-(NSInteger)Method4:(id)anObject;"));
12723   EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
12724             format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
12725   EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
12726             format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
12727   EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject "
12728             "forAllCells:(BOOL)flag;",
12729             format("- (void)sendAction:(SEL)aSelector to:(id)anObject "
12730                    "forAllCells:(BOOL)flag;"));
12731 
12732   // Very long objectiveC method declaration.
12733   verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
12734                "    (SoooooooooooooooooooooomeType *)bbbbbbbbbb;");
12735   verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
12736                "                    inRange:(NSRange)range\n"
12737                "                   outRange:(NSRange)out_range\n"
12738                "                  outRange1:(NSRange)out_range1\n"
12739                "                  outRange2:(NSRange)out_range2\n"
12740                "                  outRange3:(NSRange)out_range3\n"
12741                "                  outRange4:(NSRange)out_range4\n"
12742                "                  outRange5:(NSRange)out_range5\n"
12743                "                  outRange6:(NSRange)out_range6\n"
12744                "                  outRange7:(NSRange)out_range7\n"
12745                "                  outRange8:(NSRange)out_range8\n"
12746                "                  outRange9:(NSRange)out_range9;");
12747 
12748   // When the function name has to be wrapped.
12749   FormatStyle Style = getLLVMStyle();
12750   // ObjC ignores IndentWrappedFunctionNames when wrapping methods
12751   // and always indents instead.
12752   Style.IndentWrappedFunctionNames = false;
12753   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
12754                "    veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
12755                "               anotherName:(NSString)bbbbbbbbbbbbbb {\n"
12756                "}",
12757                Style);
12758   Style.IndentWrappedFunctionNames = true;
12759   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
12760                "    veryLooooooooooongName:(NSString)cccccccccccccc\n"
12761                "               anotherName:(NSString)dddddddddddddd {\n"
12762                "}",
12763                Style);
12764 
12765   verifyFormat("- (int)sum:(vector<int>)numbers;");
12766   verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
12767   // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
12768   // protocol lists (but not for template classes):
12769   // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
12770 
12771   verifyFormat("- (int (*)())foo:(int (*)())f;");
12772   verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
12773 
12774   // If there's no return type (very rare in practice!), LLVM and Google style
12775   // agree.
12776   verifyFormat("- foo;");
12777   verifyFormat("- foo:(int)f;");
12778   verifyGoogleFormat("- foo:(int)foo;");
12779 }
12780 
12781 TEST_F(FormatTest, BreaksStringLiterals) {
12782   EXPECT_EQ("\"some text \"\n"
12783             "\"other\";",
12784             format("\"some text other\";", getLLVMStyleWithColumns(12)));
12785   EXPECT_EQ("\"some text \"\n"
12786             "\"other\";",
12787             format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
12788   EXPECT_EQ(
12789       "#define A  \\\n"
12790       "  \"some \"  \\\n"
12791       "  \"text \"  \\\n"
12792       "  \"other\";",
12793       format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
12794   EXPECT_EQ(
12795       "#define A  \\\n"
12796       "  \"so \"    \\\n"
12797       "  \"text \"  \\\n"
12798       "  \"other\";",
12799       format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
12800 
12801   EXPECT_EQ("\"some text\"",
12802             format("\"some text\"", getLLVMStyleWithColumns(1)));
12803   EXPECT_EQ("\"some text\"",
12804             format("\"some text\"", getLLVMStyleWithColumns(11)));
12805   EXPECT_EQ("\"some \"\n"
12806             "\"text\"",
12807             format("\"some text\"", getLLVMStyleWithColumns(10)));
12808   EXPECT_EQ("\"some \"\n"
12809             "\"text\"",
12810             format("\"some text\"", getLLVMStyleWithColumns(7)));
12811   EXPECT_EQ("\"some\"\n"
12812             "\" tex\"\n"
12813             "\"t\"",
12814             format("\"some text\"", getLLVMStyleWithColumns(6)));
12815   EXPECT_EQ("\"some\"\n"
12816             "\" tex\"\n"
12817             "\" and\"",
12818             format("\"some tex and\"", getLLVMStyleWithColumns(6)));
12819   EXPECT_EQ("\"some\"\n"
12820             "\"/tex\"\n"
12821             "\"/and\"",
12822             format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
12823 
12824   EXPECT_EQ("variable =\n"
12825             "    \"long string \"\n"
12826             "    \"literal\";",
12827             format("variable = \"long string literal\";",
12828                    getLLVMStyleWithColumns(20)));
12829 
12830   EXPECT_EQ("variable = f(\n"
12831             "    \"long string \"\n"
12832             "    \"literal\",\n"
12833             "    short,\n"
12834             "    loooooooooooooooooooong);",
12835             format("variable = f(\"long string literal\", short, "
12836                    "loooooooooooooooooooong);",
12837                    getLLVMStyleWithColumns(20)));
12838 
12839   EXPECT_EQ(
12840       "f(g(\"long string \"\n"
12841       "    \"literal\"),\n"
12842       "  b);",
12843       format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20)));
12844   EXPECT_EQ("f(g(\"long string \"\n"
12845             "    \"literal\",\n"
12846             "    a),\n"
12847             "  b);",
12848             format("f(g(\"long string literal\", a), b);",
12849                    getLLVMStyleWithColumns(20)));
12850   EXPECT_EQ(
12851       "f(\"one two\".split(\n"
12852       "    variable));",
12853       format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
12854   EXPECT_EQ("f(\"one two three four five six \"\n"
12855             "  \"seven\".split(\n"
12856             "      really_looooong_variable));",
12857             format("f(\"one two three four five six seven\"."
12858                    "split(really_looooong_variable));",
12859                    getLLVMStyleWithColumns(33)));
12860 
12861   EXPECT_EQ("f(\"some \"\n"
12862             "  \"text\",\n"
12863             "  other);",
12864             format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
12865 
12866   // Only break as a last resort.
12867   verifyFormat(
12868       "aaaaaaaaaaaaaaaaaaaa(\n"
12869       "    aaaaaaaaaaaaaaaaaaaa,\n"
12870       "    aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
12871 
12872   EXPECT_EQ("\"splitmea\"\n"
12873             "\"trandomp\"\n"
12874             "\"oint\"",
12875             format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
12876 
12877   EXPECT_EQ("\"split/\"\n"
12878             "\"pathat/\"\n"
12879             "\"slashes\"",
12880             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
12881 
12882   EXPECT_EQ("\"split/\"\n"
12883             "\"pathat/\"\n"
12884             "\"slashes\"",
12885             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
12886   EXPECT_EQ("\"split at \"\n"
12887             "\"spaces/at/\"\n"
12888             "\"slashes.at.any$\"\n"
12889             "\"non-alphanumeric%\"\n"
12890             "\"1111111111characte\"\n"
12891             "\"rs\"",
12892             format("\"split at "
12893                    "spaces/at/"
12894                    "slashes.at."
12895                    "any$non-"
12896                    "alphanumeric%"
12897                    "1111111111characte"
12898                    "rs\"",
12899                    getLLVMStyleWithColumns(20)));
12900 
12901   // Verify that splitting the strings understands
12902   // Style::AlwaysBreakBeforeMultilineStrings.
12903   EXPECT_EQ("aaaaaaaaaaaa(\n"
12904             "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
12905             "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
12906             format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa "
12907                    "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
12908                    "aaaaaaaaaaaaaaaaaaaaaa\");",
12909                    getGoogleStyle()));
12910   EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
12911             "       \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
12912             format("return \"aaaaaaaaaaaaaaaaaaaaaa "
12913                    "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
12914                    "aaaaaaaaaaaaaaaaaaaaaa\";",
12915                    getGoogleStyle()));
12916   EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
12917             "                \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
12918             format("llvm::outs() << "
12919                    "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
12920                    "aaaaaaaaaaaaaaaaaaa\";"));
12921   EXPECT_EQ("ffff(\n"
12922             "    {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
12923             "     \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
12924             format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
12925                    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
12926                    getGoogleStyle()));
12927 
12928   FormatStyle Style = getLLVMStyleWithColumns(12);
12929   Style.BreakStringLiterals = false;
12930   EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style));
12931 
12932   FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
12933   AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
12934   EXPECT_EQ("#define A \\\n"
12935             "  \"some \" \\\n"
12936             "  \"text \" \\\n"
12937             "  \"other\";",
12938             format("#define A \"some text other\";", AlignLeft));
12939 }
12940 
12941 TEST_F(FormatTest, BreaksStringLiteralsAtColumnLimit) {
12942   EXPECT_EQ("C a = \"some more \"\n"
12943             "      \"text\";",
12944             format("C a = \"some more text\";", getLLVMStyleWithColumns(18)));
12945 }
12946 
12947 TEST_F(FormatTest, FullyRemoveEmptyLines) {
12948   FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80);
12949   NoEmptyLines.MaxEmptyLinesToKeep = 0;
12950   EXPECT_EQ("int i = a(b());",
12951             format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines));
12952 }
12953 
12954 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
12955   EXPECT_EQ(
12956       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
12957       "(\n"
12958       "    \"x\t\");",
12959       format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
12960              "aaaaaaa("
12961              "\"x\t\");"));
12962 }
12963 
12964 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
12965   EXPECT_EQ(
12966       "u8\"utf8 string \"\n"
12967       "u8\"literal\";",
12968       format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
12969   EXPECT_EQ(
12970       "u\"utf16 string \"\n"
12971       "u\"literal\";",
12972       format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
12973   EXPECT_EQ(
12974       "U\"utf32 string \"\n"
12975       "U\"literal\";",
12976       format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
12977   EXPECT_EQ("L\"wide string \"\n"
12978             "L\"literal\";",
12979             format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
12980   EXPECT_EQ("@\"NSString \"\n"
12981             "@\"literal\";",
12982             format("@\"NSString literal\";", getGoogleStyleWithColumns(19)));
12983   verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26));
12984 
12985   // This input makes clang-format try to split the incomplete unicode escape
12986   // sequence, which used to lead to a crasher.
12987   verifyNoCrash(
12988       "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
12989       getLLVMStyleWithColumns(60));
12990 }
12991 
12992 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) {
12993   FormatStyle Style = getGoogleStyleWithColumns(15);
12994   EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style));
12995   EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style));
12996   EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style));
12997   EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style));
12998   EXPECT_EQ("u8R\"x(raw literal)x\";",
12999             format("u8R\"x(raw literal)x\";", Style));
13000 }
13001 
13002 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
13003   FormatStyle Style = getLLVMStyleWithColumns(20);
13004   EXPECT_EQ(
13005       "_T(\"aaaaaaaaaaaaaa\")\n"
13006       "_T(\"aaaaaaaaaaaaaa\")\n"
13007       "_T(\"aaaaaaaaaaaa\")",
13008       format("  _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
13009   EXPECT_EQ("f(x,\n"
13010             "  _T(\"aaaaaaaaaaaa\")\n"
13011             "  _T(\"aaa\"),\n"
13012             "  z);",
13013             format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style));
13014 
13015   // FIXME: Handle embedded spaces in one iteration.
13016   //  EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
13017   //            "_T(\"aaaaaaaaaaaaa\")\n"
13018   //            "_T(\"aaaaaaaaaaaaa\")\n"
13019   //            "_T(\"a\")",
13020   //            format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
13021   //                   getLLVMStyleWithColumns(20)));
13022   EXPECT_EQ(
13023       "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
13024       format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style));
13025   EXPECT_EQ("f(\n"
13026             "#if !TEST\n"
13027             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
13028             "#endif\n"
13029             ");",
13030             format("f(\n"
13031                    "#if !TEST\n"
13032                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
13033                    "#endif\n"
13034                    ");"));
13035   EXPECT_EQ("f(\n"
13036             "\n"
13037             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));",
13038             format("f(\n"
13039                    "\n"
13040                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));"));
13041   // Regression test for accessing tokens past the end of a vector in the
13042   // TokenLexer.
13043   verifyNoCrash(R"(_T(
13044 "
13045 )
13046 )");
13047 }
13048 
13049 TEST_F(FormatTest, BreaksStringLiteralOperands) {
13050   // In a function call with two operands, the second can be broken with no line
13051   // break before it.
13052   EXPECT_EQ(
13053       "func(a, \"long long \"\n"
13054       "        \"long long\");",
13055       format("func(a, \"long long long long\");", getLLVMStyleWithColumns(24)));
13056   // In a function call with three operands, the second must be broken with a
13057   // line break before it.
13058   EXPECT_EQ("func(a,\n"
13059             "     \"long long long \"\n"
13060             "     \"long\",\n"
13061             "     c);",
13062             format("func(a, \"long long long long\", c);",
13063                    getLLVMStyleWithColumns(24)));
13064   // In a function call with three operands, the third must be broken with a
13065   // line break before it.
13066   EXPECT_EQ("func(a, b,\n"
13067             "     \"long long long \"\n"
13068             "     \"long\");",
13069             format("func(a, b, \"long long long long\");",
13070                    getLLVMStyleWithColumns(24)));
13071   // In a function call with three operands, both the second and the third must
13072   // be broken with a line break before them.
13073   EXPECT_EQ("func(a,\n"
13074             "     \"long long long \"\n"
13075             "     \"long\",\n"
13076             "     \"long long long \"\n"
13077             "     \"long\");",
13078             format("func(a, \"long long long long\", \"long long long long\");",
13079                    getLLVMStyleWithColumns(24)));
13080   // In a chain of << with two operands, the second can be broken with no line
13081   // break before it.
13082   EXPECT_EQ("a << \"line line \"\n"
13083             "     \"line\";",
13084             format("a << \"line line line\";", getLLVMStyleWithColumns(20)));
13085   // In a chain of << with three operands, the second can be broken with no line
13086   // break before it.
13087   EXPECT_EQ(
13088       "abcde << \"line \"\n"
13089       "         \"line line\"\n"
13090       "      << c;",
13091       format("abcde << \"line line line\" << c;", getLLVMStyleWithColumns(20)));
13092   // In a chain of << with three operands, the third must be broken with a line
13093   // break before it.
13094   EXPECT_EQ(
13095       "a << b\n"
13096       "  << \"line line \"\n"
13097       "     \"line\";",
13098       format("a << b << \"line line line\";", getLLVMStyleWithColumns(20)));
13099   // In a chain of << with three operands, the second can be broken with no line
13100   // break before it and the third must be broken with a line break before it.
13101   EXPECT_EQ("abcd << \"line line \"\n"
13102             "        \"line\"\n"
13103             "     << \"line line \"\n"
13104             "        \"line\";",
13105             format("abcd << \"line line line\" << \"line line line\";",
13106                    getLLVMStyleWithColumns(20)));
13107   // In a chain of binary operators with two operands, the second can be broken
13108   // with no line break before it.
13109   EXPECT_EQ(
13110       "abcd + \"line line \"\n"
13111       "       \"line line\";",
13112       format("abcd + \"line line line line\";", getLLVMStyleWithColumns(20)));
13113   // In a chain of binary operators with three operands, the second must be
13114   // broken with a line break before it.
13115   EXPECT_EQ("abcd +\n"
13116             "    \"line line \"\n"
13117             "    \"line line\" +\n"
13118             "    e;",
13119             format("abcd + \"line line line line\" + e;",
13120                    getLLVMStyleWithColumns(20)));
13121   // In a function call with two operands, with AlignAfterOpenBracket enabled,
13122   // the first must be broken with a line break before it.
13123   FormatStyle Style = getLLVMStyleWithColumns(25);
13124   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
13125   EXPECT_EQ("someFunction(\n"
13126             "    \"long long long \"\n"
13127             "    \"long\",\n"
13128             "    a);",
13129             format("someFunction(\"long long long long\", a);", Style));
13130 }
13131 
13132 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
13133   EXPECT_EQ(
13134       "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
13135       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
13136       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
13137       format("aaaaaaaaaaa  =  \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
13138              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
13139              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"));
13140 }
13141 
13142 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
13143   EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);",
13144             format("f(g(R\"x(raw literal)x\",   a), b);", getGoogleStyle()));
13145   EXPECT_EQ("fffffffffff(g(R\"x(\n"
13146             "multiline raw string literal xxxxxxxxxxxxxx\n"
13147             ")x\",\n"
13148             "              a),\n"
13149             "            b);",
13150             format("fffffffffff(g(R\"x(\n"
13151                    "multiline raw string literal xxxxxxxxxxxxxx\n"
13152                    ")x\", a), b);",
13153                    getGoogleStyleWithColumns(20)));
13154   EXPECT_EQ("fffffffffff(\n"
13155             "    g(R\"x(qqq\n"
13156             "multiline raw string literal xxxxxxxxxxxxxx\n"
13157             ")x\",\n"
13158             "      a),\n"
13159             "    b);",
13160             format("fffffffffff(g(R\"x(qqq\n"
13161                    "multiline raw string literal xxxxxxxxxxxxxx\n"
13162                    ")x\", a), b);",
13163                    getGoogleStyleWithColumns(20)));
13164 
13165   EXPECT_EQ("fffffffffff(R\"x(\n"
13166             "multiline raw string literal xxxxxxxxxxxxxx\n"
13167             ")x\");",
13168             format("fffffffffff(R\"x(\n"
13169                    "multiline raw string literal xxxxxxxxxxxxxx\n"
13170                    ")x\");",
13171                    getGoogleStyleWithColumns(20)));
13172   EXPECT_EQ("fffffffffff(R\"x(\n"
13173             "multiline raw string literal xxxxxxxxxxxxxx\n"
13174             ")x\" + bbbbbb);",
13175             format("fffffffffff(R\"x(\n"
13176                    "multiline raw string literal xxxxxxxxxxxxxx\n"
13177                    ")x\" +   bbbbbb);",
13178                    getGoogleStyleWithColumns(20)));
13179   EXPECT_EQ("fffffffffff(\n"
13180             "    R\"x(\n"
13181             "multiline raw string literal xxxxxxxxxxxxxx\n"
13182             ")x\" +\n"
13183             "    bbbbbb);",
13184             format("fffffffffff(\n"
13185                    " R\"x(\n"
13186                    "multiline raw string literal xxxxxxxxxxxxxx\n"
13187                    ")x\" + bbbbbb);",
13188                    getGoogleStyleWithColumns(20)));
13189   EXPECT_EQ("fffffffffff(R\"(single line raw string)\" + bbbbbb);",
13190             format("fffffffffff(\n"
13191                    " R\"(single line raw string)\" + bbbbbb);"));
13192 }
13193 
13194 TEST_F(FormatTest, SkipsUnknownStringLiterals) {
13195   verifyFormat("string a = \"unterminated;");
13196   EXPECT_EQ("function(\"unterminated,\n"
13197             "         OtherParameter);",
13198             format("function(  \"unterminated,\n"
13199                    "    OtherParameter);"));
13200 }
13201 
13202 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
13203   FormatStyle Style = getLLVMStyle();
13204   Style.Standard = FormatStyle::LS_Cpp03;
13205   EXPECT_EQ("#define x(_a) printf(\"foo\" _a);",
13206             format("#define x(_a) printf(\"foo\"_a);", Style));
13207 }
13208 
13209 TEST_F(FormatTest, CppLexVersion) {
13210   FormatStyle Style = getLLVMStyle();
13211   // Formatting of x * y differs if x is a type.
13212   verifyFormat("void foo() { MACRO(a * b); }", Style);
13213   verifyFormat("void foo() { MACRO(int *b); }", Style);
13214 
13215   // LLVM style uses latest lexer.
13216   verifyFormat("void foo() { MACRO(char8_t *b); }", Style);
13217   Style.Standard = FormatStyle::LS_Cpp17;
13218   // But in c++17, char8_t isn't a keyword.
13219   verifyFormat("void foo() { MACRO(char8_t * b); }", Style);
13220 }
13221 
13222 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); }
13223 
13224 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
13225   EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
13226             "             \"ddeeefff\");",
13227             format("someFunction(\"aaabbbcccdddeeefff\");",
13228                    getLLVMStyleWithColumns(25)));
13229   EXPECT_EQ("someFunction1234567890(\n"
13230             "    \"aaabbbcccdddeeefff\");",
13231             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
13232                    getLLVMStyleWithColumns(26)));
13233   EXPECT_EQ("someFunction1234567890(\n"
13234             "    \"aaabbbcccdddeeeff\"\n"
13235             "    \"f\");",
13236             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
13237                    getLLVMStyleWithColumns(25)));
13238   EXPECT_EQ("someFunction1234567890(\n"
13239             "    \"aaabbbcccdddeeeff\"\n"
13240             "    \"f\");",
13241             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
13242                    getLLVMStyleWithColumns(24)));
13243   EXPECT_EQ("someFunction(\n"
13244             "    \"aaabbbcc ddde \"\n"
13245             "    \"efff\");",
13246             format("someFunction(\"aaabbbcc ddde efff\");",
13247                    getLLVMStyleWithColumns(25)));
13248   EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
13249             "             \"ddeeefff\");",
13250             format("someFunction(\"aaabbbccc ddeeefff\");",
13251                    getLLVMStyleWithColumns(25)));
13252   EXPECT_EQ("someFunction1234567890(\n"
13253             "    \"aaabb \"\n"
13254             "    \"cccdddeeefff\");",
13255             format("someFunction1234567890(\"aaabb cccdddeeefff\");",
13256                    getLLVMStyleWithColumns(25)));
13257   EXPECT_EQ("#define A          \\\n"
13258             "  string s =       \\\n"
13259             "      \"123456789\"  \\\n"
13260             "      \"0\";         \\\n"
13261             "  int i;",
13262             format("#define A string s = \"1234567890\"; int i;",
13263                    getLLVMStyleWithColumns(20)));
13264   EXPECT_EQ("someFunction(\n"
13265             "    \"aaabbbcc \"\n"
13266             "    \"dddeeefff\");",
13267             format("someFunction(\"aaabbbcc dddeeefff\");",
13268                    getLLVMStyleWithColumns(25)));
13269 }
13270 
13271 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
13272   EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3)));
13273   EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2)));
13274   EXPECT_EQ("\"test\"\n"
13275             "\"\\n\"",
13276             format("\"test\\n\"", getLLVMStyleWithColumns(7)));
13277   EXPECT_EQ("\"tes\\\\\"\n"
13278             "\"n\"",
13279             format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
13280   EXPECT_EQ("\"\\\\\\\\\"\n"
13281             "\"\\n\"",
13282             format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
13283   EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
13284   EXPECT_EQ("\"\\uff01\"\n"
13285             "\"test\"",
13286             format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
13287   EXPECT_EQ("\"\\Uff01ff02\"",
13288             format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
13289   EXPECT_EQ("\"\\x000000000001\"\n"
13290             "\"next\"",
13291             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
13292   EXPECT_EQ("\"\\x000000000001next\"",
13293             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
13294   EXPECT_EQ("\"\\x000000000001\"",
13295             format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
13296   EXPECT_EQ("\"test\"\n"
13297             "\"\\000000\"\n"
13298             "\"000001\"",
13299             format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
13300   EXPECT_EQ("\"test\\000\"\n"
13301             "\"00000000\"\n"
13302             "\"1\"",
13303             format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
13304 }
13305 
13306 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
13307   verifyFormat("void f() {\n"
13308                "  return g() {}\n"
13309                "  void h() {}");
13310   verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
13311                "g();\n"
13312                "}");
13313 }
13314 
13315 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
13316   verifyFormat(
13317       "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
13318 }
13319 
13320 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
13321   verifyFormat("class X {\n"
13322                "  void f() {\n"
13323                "  }\n"
13324                "};",
13325                getLLVMStyleWithColumns(12));
13326 }
13327 
13328 TEST_F(FormatTest, ConfigurableIndentWidth) {
13329   FormatStyle EightIndent = getLLVMStyleWithColumns(18);
13330   EightIndent.IndentWidth = 8;
13331   EightIndent.ContinuationIndentWidth = 8;
13332   verifyFormat("void f() {\n"
13333                "        someFunction();\n"
13334                "        if (true) {\n"
13335                "                f();\n"
13336                "        }\n"
13337                "}",
13338                EightIndent);
13339   verifyFormat("class X {\n"
13340                "        void f() {\n"
13341                "        }\n"
13342                "};",
13343                EightIndent);
13344   verifyFormat("int x[] = {\n"
13345                "        call(),\n"
13346                "        call()};",
13347                EightIndent);
13348 }
13349 
13350 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
13351   verifyFormat("double\n"
13352                "f();",
13353                getLLVMStyleWithColumns(8));
13354 }
13355 
13356 TEST_F(FormatTest, ConfigurableUseOfTab) {
13357   FormatStyle Tab = getLLVMStyleWithColumns(42);
13358   Tab.IndentWidth = 8;
13359   Tab.UseTab = FormatStyle::UT_Always;
13360   Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left;
13361 
13362   EXPECT_EQ("if (aaaaaaaa && // q\n"
13363             "    bb)\t\t// w\n"
13364             "\t;",
13365             format("if (aaaaaaaa &&// q\n"
13366                    "bb)// w\n"
13367                    ";",
13368                    Tab));
13369   EXPECT_EQ("if (aaa && bbb) // w\n"
13370             "\t;",
13371             format("if(aaa&&bbb)// w\n"
13372                    ";",
13373                    Tab));
13374 
13375   verifyFormat("class X {\n"
13376                "\tvoid f() {\n"
13377                "\t\tsomeFunction(parameter1,\n"
13378                "\t\t\t     parameter2);\n"
13379                "\t}\n"
13380                "};",
13381                Tab);
13382   verifyFormat("#define A                        \\\n"
13383                "\tvoid f() {               \\\n"
13384                "\t\tsomeFunction(    \\\n"
13385                "\t\t    parameter1,  \\\n"
13386                "\t\t    parameter2); \\\n"
13387                "\t}",
13388                Tab);
13389   verifyFormat("int a;\t      // x\n"
13390                "int bbbbbbbb; // x\n",
13391                Tab);
13392 
13393   Tab.TabWidth = 4;
13394   Tab.IndentWidth = 8;
13395   verifyFormat("class TabWidth4Indent8 {\n"
13396                "\t\tvoid f() {\n"
13397                "\t\t\t\tsomeFunction(parameter1,\n"
13398                "\t\t\t\t\t\t\t parameter2);\n"
13399                "\t\t}\n"
13400                "};",
13401                Tab);
13402 
13403   Tab.TabWidth = 4;
13404   Tab.IndentWidth = 4;
13405   verifyFormat("class TabWidth4Indent4 {\n"
13406                "\tvoid f() {\n"
13407                "\t\tsomeFunction(parameter1,\n"
13408                "\t\t\t\t\t parameter2);\n"
13409                "\t}\n"
13410                "};",
13411                Tab);
13412 
13413   Tab.TabWidth = 8;
13414   Tab.IndentWidth = 4;
13415   verifyFormat("class TabWidth8Indent4 {\n"
13416                "    void f() {\n"
13417                "\tsomeFunction(parameter1,\n"
13418                "\t\t     parameter2);\n"
13419                "    }\n"
13420                "};",
13421                Tab);
13422 
13423   Tab.TabWidth = 8;
13424   Tab.IndentWidth = 8;
13425   EXPECT_EQ("/*\n"
13426             "\t      a\t\tcomment\n"
13427             "\t      in multiple lines\n"
13428             "       */",
13429             format("   /*\t \t \n"
13430                    " \t \t a\t\tcomment\t \t\n"
13431                    " \t \t in multiple lines\t\n"
13432                    " \t  */",
13433                    Tab));
13434 
13435   Tab.UseTab = FormatStyle::UT_ForIndentation;
13436   verifyFormat("{\n"
13437                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13438                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13439                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13440                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13441                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13442                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13443                "};",
13444                Tab);
13445   verifyFormat("enum AA {\n"
13446                "\ta1, // Force multiple lines\n"
13447                "\ta2,\n"
13448                "\ta3\n"
13449                "};",
13450                Tab);
13451   EXPECT_EQ("if (aaaaaaaa && // q\n"
13452             "    bb)         // w\n"
13453             "\t;",
13454             format("if (aaaaaaaa &&// q\n"
13455                    "bb)// w\n"
13456                    ";",
13457                    Tab));
13458   verifyFormat("class X {\n"
13459                "\tvoid f() {\n"
13460                "\t\tsomeFunction(parameter1,\n"
13461                "\t\t             parameter2);\n"
13462                "\t}\n"
13463                "};",
13464                Tab);
13465   verifyFormat("{\n"
13466                "\tQ(\n"
13467                "\t    {\n"
13468                "\t\t    int a;\n"
13469                "\t\t    someFunction(aaaaaaaa,\n"
13470                "\t\t                 bbbbbbb);\n"
13471                "\t    },\n"
13472                "\t    p);\n"
13473                "}",
13474                Tab);
13475   EXPECT_EQ("{\n"
13476             "\t/* aaaa\n"
13477             "\t   bbbb */\n"
13478             "}",
13479             format("{\n"
13480                    "/* aaaa\n"
13481                    "   bbbb */\n"
13482                    "}",
13483                    Tab));
13484   EXPECT_EQ("{\n"
13485             "\t/*\n"
13486             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13487             "\t  bbbbbbbbbbbbb\n"
13488             "\t*/\n"
13489             "}",
13490             format("{\n"
13491                    "/*\n"
13492                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13493                    "*/\n"
13494                    "}",
13495                    Tab));
13496   EXPECT_EQ("{\n"
13497             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13498             "\t// bbbbbbbbbbbbb\n"
13499             "}",
13500             format("{\n"
13501                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13502                    "}",
13503                    Tab));
13504   EXPECT_EQ("{\n"
13505             "\t/*\n"
13506             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13507             "\t  bbbbbbbbbbbbb\n"
13508             "\t*/\n"
13509             "}",
13510             format("{\n"
13511                    "\t/*\n"
13512                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13513                    "\t*/\n"
13514                    "}",
13515                    Tab));
13516   EXPECT_EQ("{\n"
13517             "\t/*\n"
13518             "\n"
13519             "\t*/\n"
13520             "}",
13521             format("{\n"
13522                    "\t/*\n"
13523                    "\n"
13524                    "\t*/\n"
13525                    "}",
13526                    Tab));
13527   EXPECT_EQ("{\n"
13528             "\t/*\n"
13529             " asdf\n"
13530             "\t*/\n"
13531             "}",
13532             format("{\n"
13533                    "\t/*\n"
13534                    " asdf\n"
13535                    "\t*/\n"
13536                    "}",
13537                    Tab));
13538 
13539   verifyFormat("void f() {\n"
13540                "\treturn true ? aaaaaaaaaaaaaaaaaa\n"
13541                "\t            : bbbbbbbbbbbbbbbbbb\n"
13542                "}",
13543                Tab);
13544   FormatStyle TabNoBreak = Tab;
13545   TabNoBreak.BreakBeforeTernaryOperators = false;
13546   verifyFormat("void f() {\n"
13547                "\treturn true ? aaaaaaaaaaaaaaaaaa :\n"
13548                "\t              bbbbbbbbbbbbbbbbbb\n"
13549                "}",
13550                TabNoBreak);
13551   verifyFormat("void f() {\n"
13552                "\treturn true ?\n"
13553                "\t           aaaaaaaaaaaaaaaaaaaa :\n"
13554                "\t           bbbbbbbbbbbbbbbbbbbb\n"
13555                "}",
13556                TabNoBreak);
13557 
13558   Tab.UseTab = FormatStyle::UT_Never;
13559   EXPECT_EQ("/*\n"
13560             "              a\t\tcomment\n"
13561             "              in multiple lines\n"
13562             "       */",
13563             format("   /*\t \t \n"
13564                    " \t \t a\t\tcomment\t \t\n"
13565                    " \t \t in multiple lines\t\n"
13566                    " \t  */",
13567                    Tab));
13568   EXPECT_EQ("/* some\n"
13569             "   comment */",
13570             format(" \t \t /* some\n"
13571                    " \t \t    comment */",
13572                    Tab));
13573   EXPECT_EQ("int a; /* some\n"
13574             "   comment */",
13575             format(" \t \t int a; /* some\n"
13576                    " \t \t    comment */",
13577                    Tab));
13578 
13579   EXPECT_EQ("int a; /* some\n"
13580             "comment */",
13581             format(" \t \t int\ta; /* some\n"
13582                    " \t \t    comment */",
13583                    Tab));
13584   EXPECT_EQ("f(\"\t\t\"); /* some\n"
13585             "    comment */",
13586             format(" \t \t f(\"\t\t\"); /* some\n"
13587                    " \t \t    comment */",
13588                    Tab));
13589   EXPECT_EQ("{\n"
13590             "        /*\n"
13591             "         * Comment\n"
13592             "         */\n"
13593             "        int i;\n"
13594             "}",
13595             format("{\n"
13596                    "\t/*\n"
13597                    "\t * Comment\n"
13598                    "\t */\n"
13599                    "\t int i;\n"
13600                    "}",
13601                    Tab));
13602 
13603   Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
13604   Tab.TabWidth = 8;
13605   Tab.IndentWidth = 8;
13606   EXPECT_EQ("if (aaaaaaaa && // q\n"
13607             "    bb)         // w\n"
13608             "\t;",
13609             format("if (aaaaaaaa &&// q\n"
13610                    "bb)// w\n"
13611                    ";",
13612                    Tab));
13613   EXPECT_EQ("if (aaa && bbb) // w\n"
13614             "\t;",
13615             format("if(aaa&&bbb)// w\n"
13616                    ";",
13617                    Tab));
13618   verifyFormat("class X {\n"
13619                "\tvoid f() {\n"
13620                "\t\tsomeFunction(parameter1,\n"
13621                "\t\t\t     parameter2);\n"
13622                "\t}\n"
13623                "};",
13624                Tab);
13625   verifyFormat("#define A                        \\\n"
13626                "\tvoid f() {               \\\n"
13627                "\t\tsomeFunction(    \\\n"
13628                "\t\t    parameter1,  \\\n"
13629                "\t\t    parameter2); \\\n"
13630                "\t}",
13631                Tab);
13632   Tab.TabWidth = 4;
13633   Tab.IndentWidth = 8;
13634   verifyFormat("class TabWidth4Indent8 {\n"
13635                "\t\tvoid f() {\n"
13636                "\t\t\t\tsomeFunction(parameter1,\n"
13637                "\t\t\t\t\t\t\t parameter2);\n"
13638                "\t\t}\n"
13639                "};",
13640                Tab);
13641   Tab.TabWidth = 4;
13642   Tab.IndentWidth = 4;
13643   verifyFormat("class TabWidth4Indent4 {\n"
13644                "\tvoid f() {\n"
13645                "\t\tsomeFunction(parameter1,\n"
13646                "\t\t\t\t\t parameter2);\n"
13647                "\t}\n"
13648                "};",
13649                Tab);
13650   Tab.TabWidth = 8;
13651   Tab.IndentWidth = 4;
13652   verifyFormat("class TabWidth8Indent4 {\n"
13653                "    void f() {\n"
13654                "\tsomeFunction(parameter1,\n"
13655                "\t\t     parameter2);\n"
13656                "    }\n"
13657                "};",
13658                Tab);
13659   Tab.TabWidth = 8;
13660   Tab.IndentWidth = 8;
13661   EXPECT_EQ("/*\n"
13662             "\t      a\t\tcomment\n"
13663             "\t      in multiple lines\n"
13664             "       */",
13665             format("   /*\t \t \n"
13666                    " \t \t a\t\tcomment\t \t\n"
13667                    " \t \t in multiple lines\t\n"
13668                    " \t  */",
13669                    Tab));
13670   verifyFormat("{\n"
13671                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13672                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13673                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13674                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13675                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13676                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13677                "};",
13678                Tab);
13679   verifyFormat("enum AA {\n"
13680                "\ta1, // Force multiple lines\n"
13681                "\ta2,\n"
13682                "\ta3\n"
13683                "};",
13684                Tab);
13685   EXPECT_EQ("if (aaaaaaaa && // q\n"
13686             "    bb)         // w\n"
13687             "\t;",
13688             format("if (aaaaaaaa &&// q\n"
13689                    "bb)// w\n"
13690                    ";",
13691                    Tab));
13692   verifyFormat("class X {\n"
13693                "\tvoid f() {\n"
13694                "\t\tsomeFunction(parameter1,\n"
13695                "\t\t\t     parameter2);\n"
13696                "\t}\n"
13697                "};",
13698                Tab);
13699   verifyFormat("{\n"
13700                "\tQ(\n"
13701                "\t    {\n"
13702                "\t\t    int a;\n"
13703                "\t\t    someFunction(aaaaaaaa,\n"
13704                "\t\t\t\t bbbbbbb);\n"
13705                "\t    },\n"
13706                "\t    p);\n"
13707                "}",
13708                Tab);
13709   EXPECT_EQ("{\n"
13710             "\t/* aaaa\n"
13711             "\t   bbbb */\n"
13712             "}",
13713             format("{\n"
13714                    "/* aaaa\n"
13715                    "   bbbb */\n"
13716                    "}",
13717                    Tab));
13718   EXPECT_EQ("{\n"
13719             "\t/*\n"
13720             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13721             "\t  bbbbbbbbbbbbb\n"
13722             "\t*/\n"
13723             "}",
13724             format("{\n"
13725                    "/*\n"
13726                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13727                    "*/\n"
13728                    "}",
13729                    Tab));
13730   EXPECT_EQ("{\n"
13731             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13732             "\t// bbbbbbbbbbbbb\n"
13733             "}",
13734             format("{\n"
13735                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13736                    "}",
13737                    Tab));
13738   EXPECT_EQ("{\n"
13739             "\t/*\n"
13740             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13741             "\t  bbbbbbbbbbbbb\n"
13742             "\t*/\n"
13743             "}",
13744             format("{\n"
13745                    "\t/*\n"
13746                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13747                    "\t*/\n"
13748                    "}",
13749                    Tab));
13750   EXPECT_EQ("{\n"
13751             "\t/*\n"
13752             "\n"
13753             "\t*/\n"
13754             "}",
13755             format("{\n"
13756                    "\t/*\n"
13757                    "\n"
13758                    "\t*/\n"
13759                    "}",
13760                    Tab));
13761   EXPECT_EQ("{\n"
13762             "\t/*\n"
13763             " asdf\n"
13764             "\t*/\n"
13765             "}",
13766             format("{\n"
13767                    "\t/*\n"
13768                    " asdf\n"
13769                    "\t*/\n"
13770                    "}",
13771                    Tab));
13772   EXPECT_EQ("/* some\n"
13773             "   comment */",
13774             format(" \t \t /* some\n"
13775                    " \t \t    comment */",
13776                    Tab));
13777   EXPECT_EQ("int a; /* some\n"
13778             "   comment */",
13779             format(" \t \t int a; /* some\n"
13780                    " \t \t    comment */",
13781                    Tab));
13782   EXPECT_EQ("int a; /* some\n"
13783             "comment */",
13784             format(" \t \t int\ta; /* some\n"
13785                    " \t \t    comment */",
13786                    Tab));
13787   EXPECT_EQ("f(\"\t\t\"); /* some\n"
13788             "    comment */",
13789             format(" \t \t f(\"\t\t\"); /* some\n"
13790                    " \t \t    comment */",
13791                    Tab));
13792   EXPECT_EQ("{\n"
13793             "\t/*\n"
13794             "\t * Comment\n"
13795             "\t */\n"
13796             "\tint i;\n"
13797             "}",
13798             format("{\n"
13799                    "\t/*\n"
13800                    "\t * Comment\n"
13801                    "\t */\n"
13802                    "\t int i;\n"
13803                    "}",
13804                    Tab));
13805   Tab.TabWidth = 2;
13806   Tab.IndentWidth = 2;
13807   EXPECT_EQ("{\n"
13808             "\t/* aaaa\n"
13809             "\t\t bbbb */\n"
13810             "}",
13811             format("{\n"
13812                    "/* aaaa\n"
13813                    "\t bbbb */\n"
13814                    "}",
13815                    Tab));
13816   EXPECT_EQ("{\n"
13817             "\t/*\n"
13818             "\t\taaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13819             "\t\tbbbbbbbbbbbbb\n"
13820             "\t*/\n"
13821             "}",
13822             format("{\n"
13823                    "/*\n"
13824                    "\taaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13825                    "*/\n"
13826                    "}",
13827                    Tab));
13828   Tab.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
13829   Tab.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
13830   Tab.TabWidth = 4;
13831   Tab.IndentWidth = 4;
13832   verifyFormat("class Assign {\n"
13833                "\tvoid f() {\n"
13834                "\t\tint         x      = 123;\n"
13835                "\t\tint         random = 4;\n"
13836                "\t\tstd::string alphabet =\n"
13837                "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
13838                "\t}\n"
13839                "};",
13840                Tab);
13841 
13842   Tab.UseTab = FormatStyle::UT_AlignWithSpaces;
13843   Tab.TabWidth = 8;
13844   Tab.IndentWidth = 8;
13845   EXPECT_EQ("if (aaaaaaaa && // q\n"
13846             "    bb)         // w\n"
13847             "\t;",
13848             format("if (aaaaaaaa &&// q\n"
13849                    "bb)// w\n"
13850                    ";",
13851                    Tab));
13852   EXPECT_EQ("if (aaa && bbb) // w\n"
13853             "\t;",
13854             format("if(aaa&&bbb)// w\n"
13855                    ";",
13856                    Tab));
13857   verifyFormat("class X {\n"
13858                "\tvoid f() {\n"
13859                "\t\tsomeFunction(parameter1,\n"
13860                "\t\t             parameter2);\n"
13861                "\t}\n"
13862                "};",
13863                Tab);
13864   verifyFormat("#define A                        \\\n"
13865                "\tvoid f() {               \\\n"
13866                "\t\tsomeFunction(    \\\n"
13867                "\t\t    parameter1,  \\\n"
13868                "\t\t    parameter2); \\\n"
13869                "\t}",
13870                Tab);
13871   Tab.TabWidth = 4;
13872   Tab.IndentWidth = 8;
13873   verifyFormat("class TabWidth4Indent8 {\n"
13874                "\t\tvoid f() {\n"
13875                "\t\t\t\tsomeFunction(parameter1,\n"
13876                "\t\t\t\t             parameter2);\n"
13877                "\t\t}\n"
13878                "};",
13879                Tab);
13880   Tab.TabWidth = 4;
13881   Tab.IndentWidth = 4;
13882   verifyFormat("class TabWidth4Indent4 {\n"
13883                "\tvoid f() {\n"
13884                "\t\tsomeFunction(parameter1,\n"
13885                "\t\t             parameter2);\n"
13886                "\t}\n"
13887                "};",
13888                Tab);
13889   Tab.TabWidth = 8;
13890   Tab.IndentWidth = 4;
13891   verifyFormat("class TabWidth8Indent4 {\n"
13892                "    void f() {\n"
13893                "\tsomeFunction(parameter1,\n"
13894                "\t             parameter2);\n"
13895                "    }\n"
13896                "};",
13897                Tab);
13898   Tab.TabWidth = 8;
13899   Tab.IndentWidth = 8;
13900   EXPECT_EQ("/*\n"
13901             "              a\t\tcomment\n"
13902             "              in multiple lines\n"
13903             "       */",
13904             format("   /*\t \t \n"
13905                    " \t \t a\t\tcomment\t \t\n"
13906                    " \t \t in multiple lines\t\n"
13907                    " \t  */",
13908                    Tab));
13909   verifyFormat("{\n"
13910                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13911                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13912                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13913                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13914                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13915                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13916                "};",
13917                Tab);
13918   verifyFormat("enum AA {\n"
13919                "\ta1, // Force multiple lines\n"
13920                "\ta2,\n"
13921                "\ta3\n"
13922                "};",
13923                Tab);
13924   EXPECT_EQ("if (aaaaaaaa && // q\n"
13925             "    bb)         // w\n"
13926             "\t;",
13927             format("if (aaaaaaaa &&// q\n"
13928                    "bb)// w\n"
13929                    ";",
13930                    Tab));
13931   verifyFormat("class X {\n"
13932                "\tvoid f() {\n"
13933                "\t\tsomeFunction(parameter1,\n"
13934                "\t\t             parameter2);\n"
13935                "\t}\n"
13936                "};",
13937                Tab);
13938   verifyFormat("{\n"
13939                "\tQ(\n"
13940                "\t    {\n"
13941                "\t\t    int a;\n"
13942                "\t\t    someFunction(aaaaaaaa,\n"
13943                "\t\t                 bbbbbbb);\n"
13944                "\t    },\n"
13945                "\t    p);\n"
13946                "}",
13947                Tab);
13948   EXPECT_EQ("{\n"
13949             "\t/* aaaa\n"
13950             "\t   bbbb */\n"
13951             "}",
13952             format("{\n"
13953                    "/* aaaa\n"
13954                    "   bbbb */\n"
13955                    "}",
13956                    Tab));
13957   EXPECT_EQ("{\n"
13958             "\t/*\n"
13959             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13960             "\t  bbbbbbbbbbbbb\n"
13961             "\t*/\n"
13962             "}",
13963             format("{\n"
13964                    "/*\n"
13965                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13966                    "*/\n"
13967                    "}",
13968                    Tab));
13969   EXPECT_EQ("{\n"
13970             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13971             "\t// bbbbbbbbbbbbb\n"
13972             "}",
13973             format("{\n"
13974                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13975                    "}",
13976                    Tab));
13977   EXPECT_EQ("{\n"
13978             "\t/*\n"
13979             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13980             "\t  bbbbbbbbbbbbb\n"
13981             "\t*/\n"
13982             "}",
13983             format("{\n"
13984                    "\t/*\n"
13985                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13986                    "\t*/\n"
13987                    "}",
13988                    Tab));
13989   EXPECT_EQ("{\n"
13990             "\t/*\n"
13991             "\n"
13992             "\t*/\n"
13993             "}",
13994             format("{\n"
13995                    "\t/*\n"
13996                    "\n"
13997                    "\t*/\n"
13998                    "}",
13999                    Tab));
14000   EXPECT_EQ("{\n"
14001             "\t/*\n"
14002             " asdf\n"
14003             "\t*/\n"
14004             "}",
14005             format("{\n"
14006                    "\t/*\n"
14007                    " asdf\n"
14008                    "\t*/\n"
14009                    "}",
14010                    Tab));
14011   EXPECT_EQ("/* some\n"
14012             "   comment */",
14013             format(" \t \t /* some\n"
14014                    " \t \t    comment */",
14015                    Tab));
14016   EXPECT_EQ("int a; /* some\n"
14017             "   comment */",
14018             format(" \t \t int a; /* some\n"
14019                    " \t \t    comment */",
14020                    Tab));
14021   EXPECT_EQ("int a; /* some\n"
14022             "comment */",
14023             format(" \t \t int\ta; /* some\n"
14024                    " \t \t    comment */",
14025                    Tab));
14026   EXPECT_EQ("f(\"\t\t\"); /* some\n"
14027             "    comment */",
14028             format(" \t \t f(\"\t\t\"); /* some\n"
14029                    " \t \t    comment */",
14030                    Tab));
14031   EXPECT_EQ("{\n"
14032             "\t/*\n"
14033             "\t * Comment\n"
14034             "\t */\n"
14035             "\tint i;\n"
14036             "}",
14037             format("{\n"
14038                    "\t/*\n"
14039                    "\t * Comment\n"
14040                    "\t */\n"
14041                    "\t int i;\n"
14042                    "}",
14043                    Tab));
14044   Tab.TabWidth = 2;
14045   Tab.IndentWidth = 2;
14046   EXPECT_EQ("{\n"
14047             "\t/* aaaa\n"
14048             "\t   bbbb */\n"
14049             "}",
14050             format("{\n"
14051                    "/* aaaa\n"
14052                    "   bbbb */\n"
14053                    "}",
14054                    Tab));
14055   EXPECT_EQ("{\n"
14056             "\t/*\n"
14057             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
14058             "\t  bbbbbbbbbbbbb\n"
14059             "\t*/\n"
14060             "}",
14061             format("{\n"
14062                    "/*\n"
14063                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
14064                    "*/\n"
14065                    "}",
14066                    Tab));
14067   Tab.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
14068   Tab.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
14069   Tab.TabWidth = 4;
14070   Tab.IndentWidth = 4;
14071   verifyFormat("class Assign {\n"
14072                "\tvoid f() {\n"
14073                "\t\tint         x      = 123;\n"
14074                "\t\tint         random = 4;\n"
14075                "\t\tstd::string alphabet =\n"
14076                "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
14077                "\t}\n"
14078                "};",
14079                Tab);
14080   Tab.AlignOperands = FormatStyle::OAS_Align;
14081   verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb +\n"
14082                "                 cccccccccccccccccccc;",
14083                Tab);
14084   // no alignment
14085   verifyFormat("int aaaaaaaaaa =\n"
14086                "\tbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
14087                Tab);
14088   verifyFormat("return aaaaaaaaaaaaaaaa ? 111111111111111\n"
14089                "       : bbbbbbbbbbbbbb ? 222222222222222\n"
14090                "                        : 333333333333333;",
14091                Tab);
14092   Tab.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
14093   Tab.AlignOperands = FormatStyle::OAS_AlignAfterOperator;
14094   verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb\n"
14095                "               + cccccccccccccccccccc;",
14096                Tab);
14097 }
14098 
14099 TEST_F(FormatTest, ZeroTabWidth) {
14100   FormatStyle Tab = getLLVMStyleWithColumns(42);
14101   Tab.IndentWidth = 8;
14102   Tab.UseTab = FormatStyle::UT_Never;
14103   Tab.TabWidth = 0;
14104   EXPECT_EQ("void a(){\n"
14105             "    // line starts with '\t'\n"
14106             "};",
14107             format("void a(){\n"
14108                    "\t// line starts with '\t'\n"
14109                    "};",
14110                    Tab));
14111 
14112   EXPECT_EQ("void a(){\n"
14113             "    // line starts with '\t'\n"
14114             "};",
14115             format("void a(){\n"
14116                    "\t\t// line starts with '\t'\n"
14117                    "};",
14118                    Tab));
14119 
14120   Tab.UseTab = FormatStyle::UT_ForIndentation;
14121   EXPECT_EQ("void a(){\n"
14122             "    // line starts with '\t'\n"
14123             "};",
14124             format("void a(){\n"
14125                    "\t// line starts with '\t'\n"
14126                    "};",
14127                    Tab));
14128 
14129   EXPECT_EQ("void a(){\n"
14130             "    // line starts with '\t'\n"
14131             "};",
14132             format("void a(){\n"
14133                    "\t\t// line starts with '\t'\n"
14134                    "};",
14135                    Tab));
14136 
14137   Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
14138   EXPECT_EQ("void a(){\n"
14139             "    // line starts with '\t'\n"
14140             "};",
14141             format("void a(){\n"
14142                    "\t// line starts with '\t'\n"
14143                    "};",
14144                    Tab));
14145 
14146   EXPECT_EQ("void a(){\n"
14147             "    // line starts with '\t'\n"
14148             "};",
14149             format("void a(){\n"
14150                    "\t\t// line starts with '\t'\n"
14151                    "};",
14152                    Tab));
14153 
14154   Tab.UseTab = FormatStyle::UT_AlignWithSpaces;
14155   EXPECT_EQ("void a(){\n"
14156             "    // line starts with '\t'\n"
14157             "};",
14158             format("void a(){\n"
14159                    "\t// line starts with '\t'\n"
14160                    "};",
14161                    Tab));
14162 
14163   EXPECT_EQ("void a(){\n"
14164             "    // line starts with '\t'\n"
14165             "};",
14166             format("void a(){\n"
14167                    "\t\t// line starts with '\t'\n"
14168                    "};",
14169                    Tab));
14170 
14171   Tab.UseTab = FormatStyle::UT_Always;
14172   EXPECT_EQ("void a(){\n"
14173             "// line starts with '\t'\n"
14174             "};",
14175             format("void a(){\n"
14176                    "\t// line starts with '\t'\n"
14177                    "};",
14178                    Tab));
14179 
14180   EXPECT_EQ("void a(){\n"
14181             "// line starts with '\t'\n"
14182             "};",
14183             format("void a(){\n"
14184                    "\t\t// line starts with '\t'\n"
14185                    "};",
14186                    Tab));
14187 }
14188 
14189 TEST_F(FormatTest, CalculatesOriginalColumn) {
14190   EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
14191             "q\"; /* some\n"
14192             "       comment */",
14193             format("  \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
14194                    "q\"; /* some\n"
14195                    "       comment */",
14196                    getLLVMStyle()));
14197   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
14198             "/* some\n"
14199             "   comment */",
14200             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
14201                    " /* some\n"
14202                    "    comment */",
14203                    getLLVMStyle()));
14204   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
14205             "qqq\n"
14206             "/* some\n"
14207             "   comment */",
14208             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
14209                    "qqq\n"
14210                    " /* some\n"
14211                    "    comment */",
14212                    getLLVMStyle()));
14213   EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
14214             "wwww; /* some\n"
14215             "         comment */",
14216             format("  inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
14217                    "wwww; /* some\n"
14218                    "         comment */",
14219                    getLLVMStyle()));
14220 }
14221 
14222 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
14223   FormatStyle NoSpace = getLLVMStyle();
14224   NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
14225 
14226   verifyFormat("while(true)\n"
14227                "  continue;",
14228                NoSpace);
14229   verifyFormat("for(;;)\n"
14230                "  continue;",
14231                NoSpace);
14232   verifyFormat("if(true)\n"
14233                "  f();\n"
14234                "else if(true)\n"
14235                "  f();",
14236                NoSpace);
14237   verifyFormat("do {\n"
14238                "  do_something();\n"
14239                "} while(something());",
14240                NoSpace);
14241   verifyFormat("switch(x) {\n"
14242                "default:\n"
14243                "  break;\n"
14244                "}",
14245                NoSpace);
14246   verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
14247   verifyFormat("size_t x = sizeof(x);", NoSpace);
14248   verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
14249   verifyFormat("auto f(int x) -> typeof(x);", NoSpace);
14250   verifyFormat("auto f(int x) -> _Atomic(x);", NoSpace);
14251   verifyFormat("auto f(int x) -> __underlying_type(x);", NoSpace);
14252   verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
14253   verifyFormat("alignas(128) char a[128];", NoSpace);
14254   verifyFormat("size_t x = alignof(MyType);", NoSpace);
14255   verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
14256   verifyFormat("int f() throw(Deprecated);", NoSpace);
14257   verifyFormat("typedef void (*cb)(int);", NoSpace);
14258   verifyFormat("T A::operator()();", NoSpace);
14259   verifyFormat("X A::operator++(T);", NoSpace);
14260   verifyFormat("auto lambda = []() { return 0; };", NoSpace);
14261 
14262   FormatStyle Space = getLLVMStyle();
14263   Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
14264 
14265   verifyFormat("int f ();", Space);
14266   verifyFormat("void f (int a, T b) {\n"
14267                "  while (true)\n"
14268                "    continue;\n"
14269                "}",
14270                Space);
14271   verifyFormat("if (true)\n"
14272                "  f ();\n"
14273                "else if (true)\n"
14274                "  f ();",
14275                Space);
14276   verifyFormat("do {\n"
14277                "  do_something ();\n"
14278                "} while (something ());",
14279                Space);
14280   verifyFormat("switch (x) {\n"
14281                "default:\n"
14282                "  break;\n"
14283                "}",
14284                Space);
14285   verifyFormat("A::A () : a (1) {}", Space);
14286   verifyFormat("void f () __attribute__ ((asdf));", Space);
14287   verifyFormat("*(&a + 1);\n"
14288                "&((&a)[1]);\n"
14289                "a[(b + c) * d];\n"
14290                "(((a + 1) * 2) + 3) * 4;",
14291                Space);
14292   verifyFormat("#define A(x) x", Space);
14293   verifyFormat("#define A (x) x", Space);
14294   verifyFormat("#if defined(x)\n"
14295                "#endif",
14296                Space);
14297   verifyFormat("auto i = std::make_unique<int> (5);", Space);
14298   verifyFormat("size_t x = sizeof (x);", Space);
14299   verifyFormat("auto f (int x) -> decltype (x);", Space);
14300   verifyFormat("auto f (int x) -> typeof (x);", Space);
14301   verifyFormat("auto f (int x) -> _Atomic (x);", Space);
14302   verifyFormat("auto f (int x) -> __underlying_type (x);", Space);
14303   verifyFormat("int f (T x) noexcept (x.create ());", Space);
14304   verifyFormat("alignas (128) char a[128];", Space);
14305   verifyFormat("size_t x = alignof (MyType);", Space);
14306   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
14307   verifyFormat("int f () throw (Deprecated);", Space);
14308   verifyFormat("typedef void (*cb) (int);", Space);
14309   // FIXME these tests regressed behaviour.
14310   // verifyFormat("T A::operator() ();", Space);
14311   // verifyFormat("X A::operator++ (T);", Space);
14312   verifyFormat("auto lambda = [] () { return 0; };", Space);
14313   verifyFormat("int x = int (y);", Space);
14314 
14315   FormatStyle SomeSpace = getLLVMStyle();
14316   SomeSpace.SpaceBeforeParens = FormatStyle::SBPO_NonEmptyParentheses;
14317 
14318   verifyFormat("[]() -> float {}", SomeSpace);
14319   verifyFormat("[] (auto foo) {}", SomeSpace);
14320   verifyFormat("[foo]() -> int {}", SomeSpace);
14321   verifyFormat("int f();", SomeSpace);
14322   verifyFormat("void f (int a, T b) {\n"
14323                "  while (true)\n"
14324                "    continue;\n"
14325                "}",
14326                SomeSpace);
14327   verifyFormat("if (true)\n"
14328                "  f();\n"
14329                "else if (true)\n"
14330                "  f();",
14331                SomeSpace);
14332   verifyFormat("do {\n"
14333                "  do_something();\n"
14334                "} while (something());",
14335                SomeSpace);
14336   verifyFormat("switch (x) {\n"
14337                "default:\n"
14338                "  break;\n"
14339                "}",
14340                SomeSpace);
14341   verifyFormat("A::A() : a (1) {}", SomeSpace);
14342   verifyFormat("void f() __attribute__ ((asdf));", SomeSpace);
14343   verifyFormat("*(&a + 1);\n"
14344                "&((&a)[1]);\n"
14345                "a[(b + c) * d];\n"
14346                "(((a + 1) * 2) + 3) * 4;",
14347                SomeSpace);
14348   verifyFormat("#define A(x) x", SomeSpace);
14349   verifyFormat("#define A (x) x", SomeSpace);
14350   verifyFormat("#if defined(x)\n"
14351                "#endif",
14352                SomeSpace);
14353   verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace);
14354   verifyFormat("size_t x = sizeof (x);", SomeSpace);
14355   verifyFormat("auto f (int x) -> decltype (x);", SomeSpace);
14356   verifyFormat("auto f (int x) -> typeof (x);", SomeSpace);
14357   verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace);
14358   verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace);
14359   verifyFormat("int f (T x) noexcept (x.create());", SomeSpace);
14360   verifyFormat("alignas (128) char a[128];", SomeSpace);
14361   verifyFormat("size_t x = alignof (MyType);", SomeSpace);
14362   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");",
14363                SomeSpace);
14364   verifyFormat("int f() throw (Deprecated);", SomeSpace);
14365   verifyFormat("typedef void (*cb) (int);", SomeSpace);
14366   verifyFormat("T A::operator()();", SomeSpace);
14367   // FIXME these tests regressed behaviour.
14368   // verifyFormat("X A::operator++ (T);", SomeSpace);
14369   verifyFormat("int x = int (y);", SomeSpace);
14370   verifyFormat("auto lambda = []() { return 0; };", SomeSpace);
14371 
14372   FormatStyle SpaceControlStatements = getLLVMStyle();
14373   SpaceControlStatements.SpaceBeforeParens = FormatStyle::SBPO_Custom;
14374   SpaceControlStatements.SpaceBeforeParensOptions.AfterControlStatements = true;
14375 
14376   verifyFormat("while (true)\n"
14377                "  continue;",
14378                SpaceControlStatements);
14379   verifyFormat("if (true)\n"
14380                "  f();\n"
14381                "else if (true)\n"
14382                "  f();",
14383                SpaceControlStatements);
14384   verifyFormat("for (;;) {\n"
14385                "  do_something();\n"
14386                "}",
14387                SpaceControlStatements);
14388   verifyFormat("do {\n"
14389                "  do_something();\n"
14390                "} while (something());",
14391                SpaceControlStatements);
14392   verifyFormat("switch (x) {\n"
14393                "default:\n"
14394                "  break;\n"
14395                "}",
14396                SpaceControlStatements);
14397 
14398   FormatStyle SpaceFuncDecl = getLLVMStyle();
14399   SpaceFuncDecl.SpaceBeforeParens = FormatStyle::SBPO_Custom;
14400   SpaceFuncDecl.SpaceBeforeParensOptions.AfterFunctionDeclarationName = true;
14401 
14402   verifyFormat("int f ();", SpaceFuncDecl);
14403   verifyFormat("void f(int a, T b) {}", SpaceFuncDecl);
14404   verifyFormat("A::A() : a(1) {}", SpaceFuncDecl);
14405   verifyFormat("void f () __attribute__((asdf));", SpaceFuncDecl);
14406   verifyFormat("#define A(x) x", SpaceFuncDecl);
14407   verifyFormat("#define A (x) x", SpaceFuncDecl);
14408   verifyFormat("#if defined(x)\n"
14409                "#endif",
14410                SpaceFuncDecl);
14411   verifyFormat("auto i = std::make_unique<int>(5);", SpaceFuncDecl);
14412   verifyFormat("size_t x = sizeof(x);", SpaceFuncDecl);
14413   verifyFormat("auto f (int x) -> decltype(x);", SpaceFuncDecl);
14414   verifyFormat("auto f (int x) -> typeof(x);", SpaceFuncDecl);
14415   verifyFormat("auto f (int x) -> _Atomic(x);", SpaceFuncDecl);
14416   verifyFormat("auto f (int x) -> __underlying_type(x);", SpaceFuncDecl);
14417   verifyFormat("int f (T x) noexcept(x.create());", SpaceFuncDecl);
14418   verifyFormat("alignas(128) char a[128];", SpaceFuncDecl);
14419   verifyFormat("size_t x = alignof(MyType);", SpaceFuncDecl);
14420   verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");",
14421                SpaceFuncDecl);
14422   verifyFormat("int f () throw(Deprecated);", SpaceFuncDecl);
14423   verifyFormat("typedef void (*cb)(int);", SpaceFuncDecl);
14424   // FIXME these tests regressed behaviour.
14425   // verifyFormat("T A::operator() ();", SpaceFuncDecl);
14426   // verifyFormat("X A::operator++ (T);", SpaceFuncDecl);
14427   verifyFormat("T A::operator()() {}", SpaceFuncDecl);
14428   verifyFormat("auto lambda = []() { return 0; };", SpaceFuncDecl);
14429   verifyFormat("int x = int(y);", SpaceFuncDecl);
14430   verifyFormat("M(std::size_t R, std::size_t C) : C(C), data(R) {}",
14431                SpaceFuncDecl);
14432 
14433   FormatStyle SpaceFuncDef = getLLVMStyle();
14434   SpaceFuncDef.SpaceBeforeParens = FormatStyle::SBPO_Custom;
14435   SpaceFuncDef.SpaceBeforeParensOptions.AfterFunctionDefinitionName = true;
14436 
14437   verifyFormat("int f();", SpaceFuncDef);
14438   verifyFormat("void f (int a, T b) {}", SpaceFuncDef);
14439   verifyFormat("A::A() : a(1) {}", SpaceFuncDef);
14440   verifyFormat("void f() __attribute__((asdf));", SpaceFuncDef);
14441   verifyFormat("#define A(x) x", SpaceFuncDef);
14442   verifyFormat("#define A (x) x", SpaceFuncDef);
14443   verifyFormat("#if defined(x)\n"
14444                "#endif",
14445                SpaceFuncDef);
14446   verifyFormat("auto i = std::make_unique<int>(5);", SpaceFuncDef);
14447   verifyFormat("size_t x = sizeof(x);", SpaceFuncDef);
14448   verifyFormat("auto f(int x) -> decltype(x);", SpaceFuncDef);
14449   verifyFormat("auto f(int x) -> typeof(x);", SpaceFuncDef);
14450   verifyFormat("auto f(int x) -> _Atomic(x);", SpaceFuncDef);
14451   verifyFormat("auto f(int x) -> __underlying_type(x);", SpaceFuncDef);
14452   verifyFormat("int f(T x) noexcept(x.create());", SpaceFuncDef);
14453   verifyFormat("alignas(128) char a[128];", SpaceFuncDef);
14454   verifyFormat("size_t x = alignof(MyType);", SpaceFuncDef);
14455   verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");",
14456                SpaceFuncDef);
14457   verifyFormat("int f() throw(Deprecated);", SpaceFuncDef);
14458   verifyFormat("typedef void (*cb)(int);", SpaceFuncDef);
14459   verifyFormat("T A::operator()();", SpaceFuncDef);
14460   verifyFormat("X A::operator++(T);", SpaceFuncDef);
14461   // verifyFormat("T A::operator() () {}", SpaceFuncDef);
14462   verifyFormat("auto lambda = [] () { return 0; };", SpaceFuncDef);
14463   verifyFormat("int x = int(y);", SpaceFuncDef);
14464   verifyFormat("M(std::size_t R, std::size_t C) : C(C), data(R) {}",
14465                SpaceFuncDef);
14466 
14467   FormatStyle SpaceIfMacros = getLLVMStyle();
14468   SpaceIfMacros.IfMacros.clear();
14469   SpaceIfMacros.IfMacros.push_back("MYIF");
14470   SpaceIfMacros.SpaceBeforeParens = FormatStyle::SBPO_Custom;
14471   SpaceIfMacros.SpaceBeforeParensOptions.AfterIfMacros = true;
14472   verifyFormat("MYIF (a)\n  return;", SpaceIfMacros);
14473   verifyFormat("MYIF (a)\n  return;\nelse MYIF (b)\n  return;", SpaceIfMacros);
14474   verifyFormat("MYIF (a)\n  return;\nelse\n  return;", SpaceIfMacros);
14475 
14476   FormatStyle SpaceForeachMacros = getLLVMStyle();
14477   SpaceForeachMacros.SpaceBeforeParens = FormatStyle::SBPO_Custom;
14478   SpaceForeachMacros.SpaceBeforeParensOptions.AfterForeachMacros = true;
14479   verifyFormat("foreach (Item *item, itemlist) {}", SpaceForeachMacros);
14480   verifyFormat("Q_FOREACH (Item *item, itemlist) {}", SpaceForeachMacros);
14481   verifyFormat("BOOST_FOREACH (Item *item, itemlist) {}", SpaceForeachMacros);
14482   verifyFormat("UNKNOWN_FOREACH(Item *item, itemlist) {}", SpaceForeachMacros);
14483 
14484   FormatStyle SomeSpace2 = getLLVMStyle();
14485   SomeSpace2.SpaceBeforeParens = FormatStyle::SBPO_Custom;
14486   SomeSpace2.SpaceBeforeParensOptions.BeforeNonEmptyParentheses = true;
14487   verifyFormat("[]() -> float {}", SomeSpace2);
14488   verifyFormat("[] (auto foo) {}", SomeSpace2);
14489   verifyFormat("[foo]() -> int {}", SomeSpace2);
14490   verifyFormat("int f();", SomeSpace2);
14491   verifyFormat("void f (int a, T b) {\n"
14492                "  while (true)\n"
14493                "    continue;\n"
14494                "}",
14495                SomeSpace2);
14496   verifyFormat("if (true)\n"
14497                "  f();\n"
14498                "else if (true)\n"
14499                "  f();",
14500                SomeSpace2);
14501   verifyFormat("do {\n"
14502                "  do_something();\n"
14503                "} while (something());",
14504                SomeSpace2);
14505   verifyFormat("switch (x) {\n"
14506                "default:\n"
14507                "  break;\n"
14508                "}",
14509                SomeSpace2);
14510   verifyFormat("A::A() : a (1) {}", SomeSpace2);
14511   verifyFormat("void f() __attribute__ ((asdf));", SomeSpace2);
14512   verifyFormat("*(&a + 1);\n"
14513                "&((&a)[1]);\n"
14514                "a[(b + c) * d];\n"
14515                "(((a + 1) * 2) + 3) * 4;",
14516                SomeSpace2);
14517   verifyFormat("#define A(x) x", SomeSpace2);
14518   verifyFormat("#define A (x) x", SomeSpace2);
14519   verifyFormat("#if defined(x)\n"
14520                "#endif",
14521                SomeSpace2);
14522   verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace2);
14523   verifyFormat("size_t x = sizeof (x);", SomeSpace2);
14524   verifyFormat("auto f (int x) -> decltype (x);", SomeSpace2);
14525   verifyFormat("auto f (int x) -> typeof (x);", SomeSpace2);
14526   verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace2);
14527   verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace2);
14528   verifyFormat("int f (T x) noexcept (x.create());", SomeSpace2);
14529   verifyFormat("alignas (128) char a[128];", SomeSpace2);
14530   verifyFormat("size_t x = alignof (MyType);", SomeSpace2);
14531   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");",
14532                SomeSpace2);
14533   verifyFormat("int f() throw (Deprecated);", SomeSpace2);
14534   verifyFormat("typedef void (*cb) (int);", SomeSpace2);
14535   verifyFormat("T A::operator()();", SomeSpace2);
14536   // verifyFormat("X A::operator++ (T);", SomeSpace2);
14537   verifyFormat("int x = int (y);", SomeSpace2);
14538   verifyFormat("auto lambda = []() { return 0; };", SomeSpace2);
14539 
14540   FormatStyle SpaceAfterOverloadedOperator = getLLVMStyle();
14541   SpaceAfterOverloadedOperator.SpaceBeforeParens = FormatStyle::SBPO_Custom;
14542   SpaceAfterOverloadedOperator.SpaceBeforeParensOptions
14543       .AfterOverloadedOperator = true;
14544 
14545   verifyFormat("auto operator++ () -> int;", SpaceAfterOverloadedOperator);
14546   verifyFormat("X A::operator++ ();", SpaceAfterOverloadedOperator);
14547   verifyFormat("some_object.operator++ ();", SpaceAfterOverloadedOperator);
14548   verifyFormat("auto func() -> int;", SpaceAfterOverloadedOperator);
14549 
14550   SpaceAfterOverloadedOperator.SpaceBeforeParensOptions
14551       .AfterOverloadedOperator = false;
14552 
14553   verifyFormat("auto operator++() -> int;", SpaceAfterOverloadedOperator);
14554   verifyFormat("X A::operator++();", SpaceAfterOverloadedOperator);
14555   verifyFormat("some_object.operator++();", SpaceAfterOverloadedOperator);
14556   verifyFormat("auto func() -> int;", SpaceAfterOverloadedOperator);
14557 }
14558 
14559 TEST_F(FormatTest, SpaceAfterLogicalNot) {
14560   FormatStyle Spaces = getLLVMStyle();
14561   Spaces.SpaceAfterLogicalNot = true;
14562 
14563   verifyFormat("bool x = ! y", Spaces);
14564   verifyFormat("if (! isFailure())", Spaces);
14565   verifyFormat("if (! (a && b))", Spaces);
14566   verifyFormat("\"Error!\"", Spaces);
14567   verifyFormat("! ! x", Spaces);
14568 }
14569 
14570 TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
14571   FormatStyle Spaces = getLLVMStyle();
14572 
14573   Spaces.SpacesInParentheses = true;
14574   verifyFormat("do_something( ::globalVar );", Spaces);
14575   verifyFormat("call( x, y, z );", Spaces);
14576   verifyFormat("call();", Spaces);
14577   verifyFormat("std::function<void( int, int )> callback;", Spaces);
14578   verifyFormat("void inFunction() { std::function<void( int, int )> fct; }",
14579                Spaces);
14580   verifyFormat("while ( (bool)1 )\n"
14581                "  continue;",
14582                Spaces);
14583   verifyFormat("for ( ;; )\n"
14584                "  continue;",
14585                Spaces);
14586   verifyFormat("if ( true )\n"
14587                "  f();\n"
14588                "else if ( true )\n"
14589                "  f();",
14590                Spaces);
14591   verifyFormat("do {\n"
14592                "  do_something( (int)i );\n"
14593                "} while ( something() );",
14594                Spaces);
14595   verifyFormat("switch ( x ) {\n"
14596                "default:\n"
14597                "  break;\n"
14598                "}",
14599                Spaces);
14600 
14601   Spaces.SpacesInParentheses = false;
14602   Spaces.SpacesInCStyleCastParentheses = true;
14603   verifyFormat("Type *A = ( Type * )P;", Spaces);
14604   verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
14605   verifyFormat("x = ( int32 )y;", Spaces);
14606   verifyFormat("int a = ( int )(2.0f);", Spaces);
14607   verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
14608   verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
14609   verifyFormat("#define x (( int )-1)", Spaces);
14610 
14611   // Run the first set of tests again with:
14612   Spaces.SpacesInParentheses = false;
14613   Spaces.SpaceInEmptyParentheses = true;
14614   Spaces.SpacesInCStyleCastParentheses = true;
14615   verifyFormat("call(x, y, z);", Spaces);
14616   verifyFormat("call( );", Spaces);
14617   verifyFormat("std::function<void(int, int)> callback;", Spaces);
14618   verifyFormat("while (( bool )1)\n"
14619                "  continue;",
14620                Spaces);
14621   verifyFormat("for (;;)\n"
14622                "  continue;",
14623                Spaces);
14624   verifyFormat("if (true)\n"
14625                "  f( );\n"
14626                "else if (true)\n"
14627                "  f( );",
14628                Spaces);
14629   verifyFormat("do {\n"
14630                "  do_something(( int )i);\n"
14631                "} while (something( ));",
14632                Spaces);
14633   verifyFormat("switch (x) {\n"
14634                "default:\n"
14635                "  break;\n"
14636                "}",
14637                Spaces);
14638 
14639   // Run the first set of tests again with:
14640   Spaces.SpaceAfterCStyleCast = true;
14641   verifyFormat("call(x, y, z);", Spaces);
14642   verifyFormat("call( );", Spaces);
14643   verifyFormat("std::function<void(int, int)> callback;", Spaces);
14644   verifyFormat("while (( bool ) 1)\n"
14645                "  continue;",
14646                Spaces);
14647   verifyFormat("for (;;)\n"
14648                "  continue;",
14649                Spaces);
14650   verifyFormat("if (true)\n"
14651                "  f( );\n"
14652                "else if (true)\n"
14653                "  f( );",
14654                Spaces);
14655   verifyFormat("do {\n"
14656                "  do_something(( int ) i);\n"
14657                "} while (something( ));",
14658                Spaces);
14659   verifyFormat("switch (x) {\n"
14660                "default:\n"
14661                "  break;\n"
14662                "}",
14663                Spaces);
14664 
14665   // Run subset of tests again with:
14666   Spaces.SpacesInCStyleCastParentheses = false;
14667   Spaces.SpaceAfterCStyleCast = true;
14668   verifyFormat("while ((bool) 1)\n"
14669                "  continue;",
14670                Spaces);
14671   verifyFormat("do {\n"
14672                "  do_something((int) i);\n"
14673                "} while (something( ));",
14674                Spaces);
14675 
14676   verifyFormat("size_t idx = (size_t) (ptr - ((char *) file));", Spaces);
14677   verifyFormat("size_t idx = (size_t) a;", Spaces);
14678   verifyFormat("size_t idx = (size_t) (a - 1);", Spaces);
14679   verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces);
14680   verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces);
14681   verifyFormat("size_t idx = (*foo)(a - 1);", Spaces);
14682   verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces);
14683   Spaces.ColumnLimit = 80;
14684   Spaces.IndentWidth = 4;
14685   Spaces.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
14686   verifyFormat("void foo( ) {\n"
14687                "    size_t foo = (*(function))(\n"
14688                "        Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, "
14689                "BarrrrrrrrrrrrLong,\n"
14690                "        FoooooooooLooooong);\n"
14691                "}",
14692                Spaces);
14693   Spaces.SpaceAfterCStyleCast = false;
14694   verifyFormat("size_t idx = (size_t)(ptr - ((char *)file));", Spaces);
14695   verifyFormat("size_t idx = (size_t)a;", Spaces);
14696   verifyFormat("size_t idx = (size_t)(a - 1);", Spaces);
14697   verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces);
14698   verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces);
14699   verifyFormat("size_t idx = (*foo)(a - 1);", Spaces);
14700   verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces);
14701 
14702   verifyFormat("void foo( ) {\n"
14703                "    size_t foo = (*(function))(\n"
14704                "        Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, "
14705                "BarrrrrrrrrrrrLong,\n"
14706                "        FoooooooooLooooong);\n"
14707                "}",
14708                Spaces);
14709 }
14710 
14711 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
14712   verifyFormat("int a[5];");
14713   verifyFormat("a[3] += 42;");
14714 
14715   FormatStyle Spaces = getLLVMStyle();
14716   Spaces.SpacesInSquareBrackets = true;
14717   // Not lambdas.
14718   verifyFormat("int a[ 5 ];", Spaces);
14719   verifyFormat("a[ 3 ] += 42;", Spaces);
14720   verifyFormat("constexpr char hello[]{\"hello\"};", Spaces);
14721   verifyFormat("double &operator[](int i) { return 0; }\n"
14722                "int i;",
14723                Spaces);
14724   verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces);
14725   verifyFormat("int i = a[ a ][ a ]->f();", Spaces);
14726   verifyFormat("int i = (*b)[ a ]->f();", Spaces);
14727   // Lambdas.
14728   verifyFormat("int c = []() -> int { return 2; }();\n", Spaces);
14729   verifyFormat("return [ i, args... ] {};", Spaces);
14730   verifyFormat("int foo = [ &bar ]() {};", Spaces);
14731   verifyFormat("int foo = [ = ]() {};", Spaces);
14732   verifyFormat("int foo = [ & ]() {};", Spaces);
14733   verifyFormat("int foo = [ =, &bar ]() {};", Spaces);
14734   verifyFormat("int foo = [ &bar, = ]() {};", Spaces);
14735 }
14736 
14737 TEST_F(FormatTest, ConfigurableSpaceBeforeBrackets) {
14738   FormatStyle NoSpaceStyle = getLLVMStyle();
14739   verifyFormat("int a[5];", NoSpaceStyle);
14740   verifyFormat("a[3] += 42;", NoSpaceStyle);
14741 
14742   verifyFormat("int a[1];", NoSpaceStyle);
14743   verifyFormat("int 1 [a];", NoSpaceStyle);
14744   verifyFormat("int a[1][2];", NoSpaceStyle);
14745   verifyFormat("a[7] = 5;", NoSpaceStyle);
14746   verifyFormat("int a = (f())[23];", NoSpaceStyle);
14747   verifyFormat("f([] {})", NoSpaceStyle);
14748 
14749   FormatStyle Space = getLLVMStyle();
14750   Space.SpaceBeforeSquareBrackets = true;
14751   verifyFormat("int c = []() -> int { return 2; }();\n", Space);
14752   verifyFormat("return [i, args...] {};", Space);
14753 
14754   verifyFormat("int a [5];", Space);
14755   verifyFormat("a [3] += 42;", Space);
14756   verifyFormat("constexpr char hello []{\"hello\"};", Space);
14757   verifyFormat("double &operator[](int i) { return 0; }\n"
14758                "int i;",
14759                Space);
14760   verifyFormat("std::unique_ptr<int []> foo() {}", Space);
14761   verifyFormat("int i = a [a][a]->f();", Space);
14762   verifyFormat("int i = (*b) [a]->f();", Space);
14763 
14764   verifyFormat("int a [1];", Space);
14765   verifyFormat("int 1 [a];", Space);
14766   verifyFormat("int a [1][2];", Space);
14767   verifyFormat("a [7] = 5;", Space);
14768   verifyFormat("int a = (f()) [23];", Space);
14769   verifyFormat("f([] {})", Space);
14770 }
14771 
14772 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
14773   verifyFormat("int a = 5;");
14774   verifyFormat("a += 42;");
14775   verifyFormat("a or_eq 8;");
14776 
14777   FormatStyle Spaces = getLLVMStyle();
14778   Spaces.SpaceBeforeAssignmentOperators = false;
14779   verifyFormat("int a= 5;", Spaces);
14780   verifyFormat("a+= 42;", Spaces);
14781   verifyFormat("a or_eq 8;", Spaces);
14782 }
14783 
14784 TEST_F(FormatTest, ConfigurableSpaceBeforeColon) {
14785   verifyFormat("class Foo : public Bar {};");
14786   verifyFormat("Foo::Foo() : foo(1) {}");
14787   verifyFormat("for (auto a : b) {\n}");
14788   verifyFormat("int x = a ? b : c;");
14789   verifyFormat("{\n"
14790                "label0:\n"
14791                "  int x = 0;\n"
14792                "}");
14793   verifyFormat("switch (x) {\n"
14794                "case 1:\n"
14795                "default:\n"
14796                "}");
14797   verifyFormat("switch (allBraces) {\n"
14798                "case 1: {\n"
14799                "  break;\n"
14800                "}\n"
14801                "case 2: {\n"
14802                "  [[fallthrough]];\n"
14803                "}\n"
14804                "default: {\n"
14805                "  break;\n"
14806                "}\n"
14807                "}");
14808 
14809   FormatStyle CtorInitializerStyle = getLLVMStyleWithColumns(30);
14810   CtorInitializerStyle.SpaceBeforeCtorInitializerColon = false;
14811   verifyFormat("class Foo : public Bar {};", CtorInitializerStyle);
14812   verifyFormat("Foo::Foo(): foo(1) {}", CtorInitializerStyle);
14813   verifyFormat("for (auto a : b) {\n}", CtorInitializerStyle);
14814   verifyFormat("int x = a ? b : c;", CtorInitializerStyle);
14815   verifyFormat("{\n"
14816                "label1:\n"
14817                "  int x = 0;\n"
14818                "}",
14819                CtorInitializerStyle);
14820   verifyFormat("switch (x) {\n"
14821                "case 1:\n"
14822                "default:\n"
14823                "}",
14824                CtorInitializerStyle);
14825   verifyFormat("switch (allBraces) {\n"
14826                "case 1: {\n"
14827                "  break;\n"
14828                "}\n"
14829                "case 2: {\n"
14830                "  [[fallthrough]];\n"
14831                "}\n"
14832                "default: {\n"
14833                "  break;\n"
14834                "}\n"
14835                "}",
14836                CtorInitializerStyle);
14837   CtorInitializerStyle.BreakConstructorInitializers =
14838       FormatStyle::BCIS_AfterColon;
14839   verifyFormat("Fooooooooooo::Fooooooooooo():\n"
14840                "    aaaaaaaaaaaaaaaa(1),\n"
14841                "    bbbbbbbbbbbbbbbb(2) {}",
14842                CtorInitializerStyle);
14843   CtorInitializerStyle.BreakConstructorInitializers =
14844       FormatStyle::BCIS_BeforeComma;
14845   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
14846                "    : aaaaaaaaaaaaaaaa(1)\n"
14847                "    , bbbbbbbbbbbbbbbb(2) {}",
14848                CtorInitializerStyle);
14849   CtorInitializerStyle.BreakConstructorInitializers =
14850       FormatStyle::BCIS_BeforeColon;
14851   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
14852                "    : aaaaaaaaaaaaaaaa(1),\n"
14853                "      bbbbbbbbbbbbbbbb(2) {}",
14854                CtorInitializerStyle);
14855   CtorInitializerStyle.ConstructorInitializerIndentWidth = 0;
14856   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
14857                ": aaaaaaaaaaaaaaaa(1),\n"
14858                "  bbbbbbbbbbbbbbbb(2) {}",
14859                CtorInitializerStyle);
14860 
14861   FormatStyle InheritanceStyle = getLLVMStyleWithColumns(30);
14862   InheritanceStyle.SpaceBeforeInheritanceColon = false;
14863   verifyFormat("class Foo: public Bar {};", InheritanceStyle);
14864   verifyFormat("Foo::Foo() : foo(1) {}", InheritanceStyle);
14865   verifyFormat("for (auto a : b) {\n}", InheritanceStyle);
14866   verifyFormat("int x = a ? b : c;", InheritanceStyle);
14867   verifyFormat("{\n"
14868                "label2:\n"
14869                "  int x = 0;\n"
14870                "}",
14871                InheritanceStyle);
14872   verifyFormat("switch (x) {\n"
14873                "case 1:\n"
14874                "default:\n"
14875                "}",
14876                InheritanceStyle);
14877   verifyFormat("switch (allBraces) {\n"
14878                "case 1: {\n"
14879                "  break;\n"
14880                "}\n"
14881                "case 2: {\n"
14882                "  [[fallthrough]];\n"
14883                "}\n"
14884                "default: {\n"
14885                "  break;\n"
14886                "}\n"
14887                "}",
14888                InheritanceStyle);
14889   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterComma;
14890   verifyFormat("class Foooooooooooooooooooooo\n"
14891                "    : public aaaaaaaaaaaaaaaaaa,\n"
14892                "      public bbbbbbbbbbbbbbbbbb {\n"
14893                "}",
14894                InheritanceStyle);
14895   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterColon;
14896   verifyFormat("class Foooooooooooooooooooooo:\n"
14897                "    public aaaaaaaaaaaaaaaaaa,\n"
14898                "    public bbbbbbbbbbbbbbbbbb {\n"
14899                "}",
14900                InheritanceStyle);
14901   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeComma;
14902   verifyFormat("class Foooooooooooooooooooooo\n"
14903                "    : public aaaaaaaaaaaaaaaaaa\n"
14904                "    , public bbbbbbbbbbbbbbbbbb {\n"
14905                "}",
14906                InheritanceStyle);
14907   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
14908   verifyFormat("class Foooooooooooooooooooooo\n"
14909                "    : public aaaaaaaaaaaaaaaaaa,\n"
14910                "      public bbbbbbbbbbbbbbbbbb {\n"
14911                "}",
14912                InheritanceStyle);
14913   InheritanceStyle.ConstructorInitializerIndentWidth = 0;
14914   verifyFormat("class Foooooooooooooooooooooo\n"
14915                ": public aaaaaaaaaaaaaaaaaa,\n"
14916                "  public bbbbbbbbbbbbbbbbbb {}",
14917                InheritanceStyle);
14918 
14919   FormatStyle ForLoopStyle = getLLVMStyle();
14920   ForLoopStyle.SpaceBeforeRangeBasedForLoopColon = false;
14921   verifyFormat("class Foo : public Bar {};", ForLoopStyle);
14922   verifyFormat("Foo::Foo() : foo(1) {}", ForLoopStyle);
14923   verifyFormat("for (auto a: b) {\n}", ForLoopStyle);
14924   verifyFormat("int x = a ? b : c;", ForLoopStyle);
14925   verifyFormat("{\n"
14926                "label2:\n"
14927                "  int x = 0;\n"
14928                "}",
14929                ForLoopStyle);
14930   verifyFormat("switch (x) {\n"
14931                "case 1:\n"
14932                "default:\n"
14933                "}",
14934                ForLoopStyle);
14935   verifyFormat("switch (allBraces) {\n"
14936                "case 1: {\n"
14937                "  break;\n"
14938                "}\n"
14939                "case 2: {\n"
14940                "  [[fallthrough]];\n"
14941                "}\n"
14942                "default: {\n"
14943                "  break;\n"
14944                "}\n"
14945                "}",
14946                ForLoopStyle);
14947 
14948   FormatStyle CaseStyle = getLLVMStyle();
14949   CaseStyle.SpaceBeforeCaseColon = true;
14950   verifyFormat("class Foo : public Bar {};", CaseStyle);
14951   verifyFormat("Foo::Foo() : foo(1) {}", CaseStyle);
14952   verifyFormat("for (auto a : b) {\n}", CaseStyle);
14953   verifyFormat("int x = a ? b : c;", CaseStyle);
14954   verifyFormat("switch (x) {\n"
14955                "case 1 :\n"
14956                "default :\n"
14957                "}",
14958                CaseStyle);
14959   verifyFormat("switch (allBraces) {\n"
14960                "case 1 : {\n"
14961                "  break;\n"
14962                "}\n"
14963                "case 2 : {\n"
14964                "  [[fallthrough]];\n"
14965                "}\n"
14966                "default : {\n"
14967                "  break;\n"
14968                "}\n"
14969                "}",
14970                CaseStyle);
14971 
14972   FormatStyle NoSpaceStyle = getLLVMStyle();
14973   EXPECT_EQ(NoSpaceStyle.SpaceBeforeCaseColon, false);
14974   NoSpaceStyle.SpaceBeforeCtorInitializerColon = false;
14975   NoSpaceStyle.SpaceBeforeInheritanceColon = false;
14976   NoSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false;
14977   verifyFormat("class Foo: public Bar {};", NoSpaceStyle);
14978   verifyFormat("Foo::Foo(): foo(1) {}", NoSpaceStyle);
14979   verifyFormat("for (auto a: b) {\n}", NoSpaceStyle);
14980   verifyFormat("int x = a ? b : c;", NoSpaceStyle);
14981   verifyFormat("{\n"
14982                "label3:\n"
14983                "  int x = 0;\n"
14984                "}",
14985                NoSpaceStyle);
14986   verifyFormat("switch (x) {\n"
14987                "case 1:\n"
14988                "default:\n"
14989                "}",
14990                NoSpaceStyle);
14991   verifyFormat("switch (allBraces) {\n"
14992                "case 1: {\n"
14993                "  break;\n"
14994                "}\n"
14995                "case 2: {\n"
14996                "  [[fallthrough]];\n"
14997                "}\n"
14998                "default: {\n"
14999                "  break;\n"
15000                "}\n"
15001                "}",
15002                NoSpaceStyle);
15003 
15004   FormatStyle InvertedSpaceStyle = getLLVMStyle();
15005   InvertedSpaceStyle.SpaceBeforeCaseColon = true;
15006   InvertedSpaceStyle.SpaceBeforeCtorInitializerColon = false;
15007   InvertedSpaceStyle.SpaceBeforeInheritanceColon = false;
15008   InvertedSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false;
15009   verifyFormat("class Foo: public Bar {};", InvertedSpaceStyle);
15010   verifyFormat("Foo::Foo(): foo(1) {}", InvertedSpaceStyle);
15011   verifyFormat("for (auto a: b) {\n}", InvertedSpaceStyle);
15012   verifyFormat("int x = a ? b : c;", InvertedSpaceStyle);
15013   verifyFormat("{\n"
15014                "label3:\n"
15015                "  int x = 0;\n"
15016                "}",
15017                InvertedSpaceStyle);
15018   verifyFormat("switch (x) {\n"
15019                "case 1 :\n"
15020                "case 2 : {\n"
15021                "  break;\n"
15022                "}\n"
15023                "default :\n"
15024                "  break;\n"
15025                "}",
15026                InvertedSpaceStyle);
15027   verifyFormat("switch (allBraces) {\n"
15028                "case 1 : {\n"
15029                "  break;\n"
15030                "}\n"
15031                "case 2 : {\n"
15032                "  [[fallthrough]];\n"
15033                "}\n"
15034                "default : {\n"
15035                "  break;\n"
15036                "}\n"
15037                "}",
15038                InvertedSpaceStyle);
15039 }
15040 
15041 TEST_F(FormatTest, ConfigurableSpaceAroundPointerQualifiers) {
15042   FormatStyle Style = getLLVMStyle();
15043 
15044   Style.PointerAlignment = FormatStyle::PAS_Left;
15045   Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default;
15046   verifyFormat("void* const* x = NULL;", Style);
15047 
15048 #define verifyQualifierSpaces(Code, Pointers, Qualifiers)                      \
15049   do {                                                                         \
15050     Style.PointerAlignment = FormatStyle::Pointers;                            \
15051     Style.SpaceAroundPointerQualifiers = FormatStyle::Qualifiers;              \
15052     verifyFormat(Code, Style);                                                 \
15053   } while (false)
15054 
15055   verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Default);
15056   verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_Default);
15057   verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Default);
15058 
15059   verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Before);
15060   verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Before);
15061   verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Before);
15062 
15063   verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_After);
15064   verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_After);
15065   verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_After);
15066 
15067   verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_Both);
15068   verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Both);
15069   verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Both);
15070 
15071   verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Default);
15072   verifyQualifierSpaces("Foo::operator void const *();", PAS_Right,
15073                         SAPQ_Default);
15074   verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
15075                         SAPQ_Default);
15076 
15077   verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Before);
15078   verifyQualifierSpaces("Foo::operator void const *();", PAS_Right,
15079                         SAPQ_Before);
15080   verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
15081                         SAPQ_Before);
15082 
15083   verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_After);
15084   verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_After);
15085   verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
15086                         SAPQ_After);
15087 
15088   verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_Both);
15089   verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_Both);
15090   verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, SAPQ_Both);
15091 
15092 #undef verifyQualifierSpaces
15093 
15094   FormatStyle Spaces = getLLVMStyle();
15095   Spaces.AttributeMacros.push_back("qualified");
15096   Spaces.PointerAlignment = FormatStyle::PAS_Right;
15097   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default;
15098   verifyFormat("SomeType *volatile *a = NULL;", Spaces);
15099   verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces);
15100   verifyFormat("std::vector<SomeType *const *> x;", Spaces);
15101   verifyFormat("std::vector<SomeType *qualified *> x;", Spaces);
15102   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
15103   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before;
15104   verifyFormat("SomeType * volatile *a = NULL;", Spaces);
15105   verifyFormat("SomeType * __attribute__((attr)) *a = NULL;", Spaces);
15106   verifyFormat("std::vector<SomeType * const *> x;", Spaces);
15107   verifyFormat("std::vector<SomeType * qualified *> x;", Spaces);
15108   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
15109 
15110   // Check that SAPQ_Before doesn't result in extra spaces for PAS_Left.
15111   Spaces.PointerAlignment = FormatStyle::PAS_Left;
15112   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before;
15113   verifyFormat("SomeType* volatile* a = NULL;", Spaces);
15114   verifyFormat("SomeType* __attribute__((attr))* a = NULL;", Spaces);
15115   verifyFormat("std::vector<SomeType* const*> x;", Spaces);
15116   verifyFormat("std::vector<SomeType* qualified*> x;", Spaces);
15117   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
15118   // However, setting it to SAPQ_After should add spaces after __attribute, etc.
15119   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After;
15120   verifyFormat("SomeType* volatile * a = NULL;", Spaces);
15121   verifyFormat("SomeType* __attribute__((attr)) * a = NULL;", Spaces);
15122   verifyFormat("std::vector<SomeType* const *> x;", Spaces);
15123   verifyFormat("std::vector<SomeType* qualified *> x;", Spaces);
15124   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
15125 
15126   // PAS_Middle should not have any noticeable changes even for SAPQ_Both
15127   Spaces.PointerAlignment = FormatStyle::PAS_Middle;
15128   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After;
15129   verifyFormat("SomeType * volatile * a = NULL;", Spaces);
15130   verifyFormat("SomeType * __attribute__((attr)) * a = NULL;", Spaces);
15131   verifyFormat("std::vector<SomeType * const *> x;", Spaces);
15132   verifyFormat("std::vector<SomeType * qualified *> x;", Spaces);
15133   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
15134 }
15135 
15136 TEST_F(FormatTest, AlignConsecutiveMacros) {
15137   FormatStyle Style = getLLVMStyle();
15138   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
15139   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
15140   Style.AlignConsecutiveMacros = FormatStyle::ACS_None;
15141 
15142   verifyFormat("#define a 3\n"
15143                "#define bbbb 4\n"
15144                "#define ccc (5)",
15145                Style);
15146 
15147   verifyFormat("#define f(x) (x * x)\n"
15148                "#define fff(x, y, z) (x * y + z)\n"
15149                "#define ffff(x, y) (x - y)",
15150                Style);
15151 
15152   verifyFormat("#define foo(x, y) (x + y)\n"
15153                "#define bar (5, 6)(2 + 2)",
15154                Style);
15155 
15156   verifyFormat("#define a 3\n"
15157                "#define bbbb 4\n"
15158                "#define ccc (5)\n"
15159                "#define f(x) (x * x)\n"
15160                "#define fff(x, y, z) (x * y + z)\n"
15161                "#define ffff(x, y) (x - y)",
15162                Style);
15163 
15164   Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
15165   verifyFormat("#define a    3\n"
15166                "#define bbbb 4\n"
15167                "#define ccc  (5)",
15168                Style);
15169 
15170   verifyFormat("#define f(x)         (x * x)\n"
15171                "#define fff(x, y, z) (x * y + z)\n"
15172                "#define ffff(x, y)   (x - y)",
15173                Style);
15174 
15175   verifyFormat("#define foo(x, y) (x + y)\n"
15176                "#define bar       (5, 6)(2 + 2)",
15177                Style);
15178 
15179   verifyFormat("#define a            3\n"
15180                "#define bbbb         4\n"
15181                "#define ccc          (5)\n"
15182                "#define f(x)         (x * x)\n"
15183                "#define fff(x, y, z) (x * y + z)\n"
15184                "#define ffff(x, y)   (x - y)",
15185                Style);
15186 
15187   verifyFormat("#define a         5\n"
15188                "#define foo(x, y) (x + y)\n"
15189                "#define CCC       (6)\n"
15190                "auto lambda = []() {\n"
15191                "  auto  ii = 0;\n"
15192                "  float j  = 0;\n"
15193                "  return 0;\n"
15194                "};\n"
15195                "int   i  = 0;\n"
15196                "float i2 = 0;\n"
15197                "auto  v  = type{\n"
15198                "    i = 1,   //\n"
15199                "    (i = 2), //\n"
15200                "    i = 3    //\n"
15201                "};",
15202                Style);
15203 
15204   Style.AlignConsecutiveMacros = FormatStyle::ACS_None;
15205   Style.ColumnLimit = 20;
15206 
15207   verifyFormat("#define a          \\\n"
15208                "  \"aabbbbbbbbbbbb\"\n"
15209                "#define D          \\\n"
15210                "  \"aabbbbbbbbbbbb\" \\\n"
15211                "  \"ccddeeeeeeeee\"\n"
15212                "#define B          \\\n"
15213                "  \"QQQQQQQQQQQQQ\"  \\\n"
15214                "  \"FFFFFFFFFFFFF\"  \\\n"
15215                "  \"LLLLLLLL\"\n",
15216                Style);
15217 
15218   Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
15219   verifyFormat("#define a          \\\n"
15220                "  \"aabbbbbbbbbbbb\"\n"
15221                "#define D          \\\n"
15222                "  \"aabbbbbbbbbbbb\" \\\n"
15223                "  \"ccddeeeeeeeee\"\n"
15224                "#define B          \\\n"
15225                "  \"QQQQQQQQQQQQQ\"  \\\n"
15226                "  \"FFFFFFFFFFFFF\"  \\\n"
15227                "  \"LLLLLLLL\"\n",
15228                Style);
15229 
15230   // Test across comments
15231   Style.MaxEmptyLinesToKeep = 10;
15232   Style.ReflowComments = false;
15233   Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossComments;
15234   EXPECT_EQ("#define a    3\n"
15235             "// line comment\n"
15236             "#define bbbb 4\n"
15237             "#define ccc  (5)",
15238             format("#define a 3\n"
15239                    "// line comment\n"
15240                    "#define bbbb 4\n"
15241                    "#define ccc (5)",
15242                    Style));
15243 
15244   EXPECT_EQ("#define a    3\n"
15245             "/* block comment */\n"
15246             "#define bbbb 4\n"
15247             "#define ccc  (5)",
15248             format("#define a  3\n"
15249                    "/* block comment */\n"
15250                    "#define bbbb 4\n"
15251                    "#define ccc (5)",
15252                    Style));
15253 
15254   EXPECT_EQ("#define a    3\n"
15255             "/* multi-line *\n"
15256             " * block comment */\n"
15257             "#define bbbb 4\n"
15258             "#define ccc  (5)",
15259             format("#define a 3\n"
15260                    "/* multi-line *\n"
15261                    " * block comment */\n"
15262                    "#define bbbb 4\n"
15263                    "#define ccc (5)",
15264                    Style));
15265 
15266   EXPECT_EQ("#define a    3\n"
15267             "// multi-line line comment\n"
15268             "//\n"
15269             "#define bbbb 4\n"
15270             "#define ccc  (5)",
15271             format("#define a  3\n"
15272                    "// multi-line line comment\n"
15273                    "//\n"
15274                    "#define bbbb 4\n"
15275                    "#define ccc (5)",
15276                    Style));
15277 
15278   EXPECT_EQ("#define a 3\n"
15279             "// empty lines still break.\n"
15280             "\n"
15281             "#define bbbb 4\n"
15282             "#define ccc  (5)",
15283             format("#define a     3\n"
15284                    "// empty lines still break.\n"
15285                    "\n"
15286                    "#define bbbb     4\n"
15287                    "#define ccc  (5)",
15288                    Style));
15289 
15290   // Test across empty lines
15291   Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossEmptyLines;
15292   EXPECT_EQ("#define a    3\n"
15293             "\n"
15294             "#define bbbb 4\n"
15295             "#define ccc  (5)",
15296             format("#define a 3\n"
15297                    "\n"
15298                    "#define bbbb 4\n"
15299                    "#define ccc (5)",
15300                    Style));
15301 
15302   EXPECT_EQ("#define a    3\n"
15303             "\n"
15304             "\n"
15305             "\n"
15306             "#define bbbb 4\n"
15307             "#define ccc  (5)",
15308             format("#define a        3\n"
15309                    "\n"
15310                    "\n"
15311                    "\n"
15312                    "#define bbbb 4\n"
15313                    "#define ccc (5)",
15314                    Style));
15315 
15316   EXPECT_EQ("#define a 3\n"
15317             "// comments should break alignment\n"
15318             "//\n"
15319             "#define bbbb 4\n"
15320             "#define ccc  (5)",
15321             format("#define a        3\n"
15322                    "// comments should break alignment\n"
15323                    "//\n"
15324                    "#define bbbb 4\n"
15325                    "#define ccc (5)",
15326                    Style));
15327 
15328   // Test across empty lines and comments
15329   Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossEmptyLinesAndComments;
15330   verifyFormat("#define a    3\n"
15331                "\n"
15332                "// line comment\n"
15333                "#define bbbb 4\n"
15334                "#define ccc  (5)",
15335                Style);
15336 
15337   EXPECT_EQ("#define a    3\n"
15338             "\n"
15339             "\n"
15340             "/* multi-line *\n"
15341             " * block comment */\n"
15342             "\n"
15343             "\n"
15344             "#define bbbb 4\n"
15345             "#define ccc  (5)",
15346             format("#define a 3\n"
15347                    "\n"
15348                    "\n"
15349                    "/* multi-line *\n"
15350                    " * block comment */\n"
15351                    "\n"
15352                    "\n"
15353                    "#define bbbb 4\n"
15354                    "#define ccc (5)",
15355                    Style));
15356 
15357   EXPECT_EQ("#define a    3\n"
15358             "\n"
15359             "\n"
15360             "/* multi-line *\n"
15361             " * block comment */\n"
15362             "\n"
15363             "\n"
15364             "#define bbbb 4\n"
15365             "#define ccc  (5)",
15366             format("#define a 3\n"
15367                    "\n"
15368                    "\n"
15369                    "/* multi-line *\n"
15370                    " * block comment */\n"
15371                    "\n"
15372                    "\n"
15373                    "#define bbbb 4\n"
15374                    "#define ccc       (5)",
15375                    Style));
15376 }
15377 
15378 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLines) {
15379   FormatStyle Alignment = getLLVMStyle();
15380   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
15381   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_AcrossEmptyLines;
15382 
15383   Alignment.MaxEmptyLinesToKeep = 10;
15384   /* Test alignment across empty lines */
15385   EXPECT_EQ("int a           = 5;\n"
15386             "\n"
15387             "int oneTwoThree = 123;",
15388             format("int a       = 5;\n"
15389                    "\n"
15390                    "int oneTwoThree= 123;",
15391                    Alignment));
15392   EXPECT_EQ("int a           = 5;\n"
15393             "int one         = 1;\n"
15394             "\n"
15395             "int oneTwoThree = 123;",
15396             format("int a = 5;\n"
15397                    "int one = 1;\n"
15398                    "\n"
15399                    "int oneTwoThree = 123;",
15400                    Alignment));
15401   EXPECT_EQ("int a           = 5;\n"
15402             "int one         = 1;\n"
15403             "\n"
15404             "int oneTwoThree = 123;\n"
15405             "int oneTwo      = 12;",
15406             format("int a = 5;\n"
15407                    "int one = 1;\n"
15408                    "\n"
15409                    "int oneTwoThree = 123;\n"
15410                    "int oneTwo = 12;",
15411                    Alignment));
15412 
15413   /* Test across comments */
15414   EXPECT_EQ("int a = 5;\n"
15415             "/* block comment */\n"
15416             "int oneTwoThree = 123;",
15417             format("int a = 5;\n"
15418                    "/* block comment */\n"
15419                    "int oneTwoThree=123;",
15420                    Alignment));
15421 
15422   EXPECT_EQ("int a = 5;\n"
15423             "// line comment\n"
15424             "int oneTwoThree = 123;",
15425             format("int a = 5;\n"
15426                    "// line comment\n"
15427                    "int oneTwoThree=123;",
15428                    Alignment));
15429 
15430   /* Test across comments and newlines */
15431   EXPECT_EQ("int a = 5;\n"
15432             "\n"
15433             "/* block comment */\n"
15434             "int oneTwoThree = 123;",
15435             format("int a = 5;\n"
15436                    "\n"
15437                    "/* block comment */\n"
15438                    "int oneTwoThree=123;",
15439                    Alignment));
15440 
15441   EXPECT_EQ("int a = 5;\n"
15442             "\n"
15443             "// line comment\n"
15444             "int oneTwoThree = 123;",
15445             format("int a = 5;\n"
15446                    "\n"
15447                    "// line comment\n"
15448                    "int oneTwoThree=123;",
15449                    Alignment));
15450 }
15451 
15452 TEST_F(FormatTest, AlignConsecutiveDeclarationsAcrossEmptyLinesAndComments) {
15453   FormatStyle Alignment = getLLVMStyle();
15454   Alignment.AlignConsecutiveDeclarations =
15455       FormatStyle::ACS_AcrossEmptyLinesAndComments;
15456   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
15457 
15458   Alignment.MaxEmptyLinesToKeep = 10;
15459   /* Test alignment across empty lines */
15460   EXPECT_EQ("int         a = 5;\n"
15461             "\n"
15462             "float const oneTwoThree = 123;",
15463             format("int a = 5;\n"
15464                    "\n"
15465                    "float const oneTwoThree = 123;",
15466                    Alignment));
15467   EXPECT_EQ("int         a = 5;\n"
15468             "float const one = 1;\n"
15469             "\n"
15470             "int         oneTwoThree = 123;",
15471             format("int a = 5;\n"
15472                    "float const one = 1;\n"
15473                    "\n"
15474                    "int oneTwoThree = 123;",
15475                    Alignment));
15476 
15477   /* Test across comments */
15478   EXPECT_EQ("float const a = 5;\n"
15479             "/* block comment */\n"
15480             "int         oneTwoThree = 123;",
15481             format("float const a = 5;\n"
15482                    "/* block comment */\n"
15483                    "int oneTwoThree=123;",
15484                    Alignment));
15485 
15486   EXPECT_EQ("float const a = 5;\n"
15487             "// line comment\n"
15488             "int         oneTwoThree = 123;",
15489             format("float const a = 5;\n"
15490                    "// line comment\n"
15491                    "int oneTwoThree=123;",
15492                    Alignment));
15493 
15494   /* Test across comments and newlines */
15495   EXPECT_EQ("float const a = 5;\n"
15496             "\n"
15497             "/* block comment */\n"
15498             "int         oneTwoThree = 123;",
15499             format("float const a = 5;\n"
15500                    "\n"
15501                    "/* block comment */\n"
15502                    "int         oneTwoThree=123;",
15503                    Alignment));
15504 
15505   EXPECT_EQ("float const a = 5;\n"
15506             "\n"
15507             "// line comment\n"
15508             "int         oneTwoThree = 123;",
15509             format("float const a = 5;\n"
15510                    "\n"
15511                    "// line comment\n"
15512                    "int oneTwoThree=123;",
15513                    Alignment));
15514 }
15515 
15516 TEST_F(FormatTest, AlignConsecutiveBitFieldsAcrossEmptyLinesAndComments) {
15517   FormatStyle Alignment = getLLVMStyle();
15518   Alignment.AlignConsecutiveBitFields =
15519       FormatStyle::ACS_AcrossEmptyLinesAndComments;
15520 
15521   Alignment.MaxEmptyLinesToKeep = 10;
15522   /* Test alignment across empty lines */
15523   EXPECT_EQ("int a            : 5;\n"
15524             "\n"
15525             "int longbitfield : 6;",
15526             format("int a : 5;\n"
15527                    "\n"
15528                    "int longbitfield : 6;",
15529                    Alignment));
15530   EXPECT_EQ("int a            : 5;\n"
15531             "int one          : 1;\n"
15532             "\n"
15533             "int longbitfield : 6;",
15534             format("int a : 5;\n"
15535                    "int one : 1;\n"
15536                    "\n"
15537                    "int longbitfield : 6;",
15538                    Alignment));
15539 
15540   /* Test across comments */
15541   EXPECT_EQ("int a            : 5;\n"
15542             "/* block comment */\n"
15543             "int longbitfield : 6;",
15544             format("int a : 5;\n"
15545                    "/* block comment */\n"
15546                    "int longbitfield : 6;",
15547                    Alignment));
15548   EXPECT_EQ("int a            : 5;\n"
15549             "int one          : 1;\n"
15550             "// line comment\n"
15551             "int longbitfield : 6;",
15552             format("int a : 5;\n"
15553                    "int one : 1;\n"
15554                    "// line comment\n"
15555                    "int longbitfield : 6;",
15556                    Alignment));
15557 
15558   /* Test across comments and newlines */
15559   EXPECT_EQ("int a            : 5;\n"
15560             "/* block comment */\n"
15561             "\n"
15562             "int longbitfield : 6;",
15563             format("int a : 5;\n"
15564                    "/* block comment */\n"
15565                    "\n"
15566                    "int longbitfield : 6;",
15567                    Alignment));
15568   EXPECT_EQ("int a            : 5;\n"
15569             "int one          : 1;\n"
15570             "\n"
15571             "// line comment\n"
15572             "\n"
15573             "int longbitfield : 6;",
15574             format("int a : 5;\n"
15575                    "int one : 1;\n"
15576                    "\n"
15577                    "// line comment \n"
15578                    "\n"
15579                    "int longbitfield : 6;",
15580                    Alignment));
15581 }
15582 
15583 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossComments) {
15584   FormatStyle Alignment = getLLVMStyle();
15585   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
15586   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_AcrossComments;
15587 
15588   Alignment.MaxEmptyLinesToKeep = 10;
15589   /* Test alignment across empty lines */
15590   EXPECT_EQ("int a = 5;\n"
15591             "\n"
15592             "int oneTwoThree = 123;",
15593             format("int a       = 5;\n"
15594                    "\n"
15595                    "int oneTwoThree= 123;",
15596                    Alignment));
15597   EXPECT_EQ("int a   = 5;\n"
15598             "int one = 1;\n"
15599             "\n"
15600             "int oneTwoThree = 123;",
15601             format("int a = 5;\n"
15602                    "int one = 1;\n"
15603                    "\n"
15604                    "int oneTwoThree = 123;",
15605                    Alignment));
15606 
15607   /* Test across comments */
15608   EXPECT_EQ("int a           = 5;\n"
15609             "/* block comment */\n"
15610             "int oneTwoThree = 123;",
15611             format("int a = 5;\n"
15612                    "/* block comment */\n"
15613                    "int oneTwoThree=123;",
15614                    Alignment));
15615 
15616   EXPECT_EQ("int a           = 5;\n"
15617             "// line comment\n"
15618             "int oneTwoThree = 123;",
15619             format("int a = 5;\n"
15620                    "// line comment\n"
15621                    "int oneTwoThree=123;",
15622                    Alignment));
15623 
15624   EXPECT_EQ("int a           = 5;\n"
15625             "/*\n"
15626             " * multi-line block comment\n"
15627             " */\n"
15628             "int oneTwoThree = 123;",
15629             format("int a = 5;\n"
15630                    "/*\n"
15631                    " * multi-line block comment\n"
15632                    " */\n"
15633                    "int oneTwoThree=123;",
15634                    Alignment));
15635 
15636   EXPECT_EQ("int a           = 5;\n"
15637             "//\n"
15638             "// multi-line line comment\n"
15639             "//\n"
15640             "int oneTwoThree = 123;",
15641             format("int a = 5;\n"
15642                    "//\n"
15643                    "// multi-line line comment\n"
15644                    "//\n"
15645                    "int oneTwoThree=123;",
15646                    Alignment));
15647 
15648   /* Test across comments and newlines */
15649   EXPECT_EQ("int a = 5;\n"
15650             "\n"
15651             "/* block comment */\n"
15652             "int oneTwoThree = 123;",
15653             format("int a = 5;\n"
15654                    "\n"
15655                    "/* block comment */\n"
15656                    "int oneTwoThree=123;",
15657                    Alignment));
15658 
15659   EXPECT_EQ("int a = 5;\n"
15660             "\n"
15661             "// line comment\n"
15662             "int oneTwoThree = 123;",
15663             format("int a = 5;\n"
15664                    "\n"
15665                    "// line comment\n"
15666                    "int oneTwoThree=123;",
15667                    Alignment));
15668 }
15669 
15670 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLinesAndComments) {
15671   FormatStyle Alignment = getLLVMStyle();
15672   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
15673   Alignment.AlignConsecutiveAssignments =
15674       FormatStyle::ACS_AcrossEmptyLinesAndComments;
15675   verifyFormat("int a           = 5;\n"
15676                "int oneTwoThree = 123;",
15677                Alignment);
15678   verifyFormat("int a           = method();\n"
15679                "int oneTwoThree = 133;",
15680                Alignment);
15681   verifyFormat("a &= 5;\n"
15682                "bcd *= 5;\n"
15683                "ghtyf += 5;\n"
15684                "dvfvdb -= 5;\n"
15685                "a /= 5;\n"
15686                "vdsvsv %= 5;\n"
15687                "sfdbddfbdfbb ^= 5;\n"
15688                "dvsdsv |= 5;\n"
15689                "int dsvvdvsdvvv = 123;",
15690                Alignment);
15691   verifyFormat("int i = 1, j = 10;\n"
15692                "something = 2000;",
15693                Alignment);
15694   verifyFormat("something = 2000;\n"
15695                "int i = 1, j = 10;\n",
15696                Alignment);
15697   verifyFormat("something = 2000;\n"
15698                "another   = 911;\n"
15699                "int i = 1, j = 10;\n"
15700                "oneMore = 1;\n"
15701                "i       = 2;",
15702                Alignment);
15703   verifyFormat("int a   = 5;\n"
15704                "int one = 1;\n"
15705                "method();\n"
15706                "int oneTwoThree = 123;\n"
15707                "int oneTwo      = 12;",
15708                Alignment);
15709   verifyFormat("int oneTwoThree = 123;\n"
15710                "int oneTwo      = 12;\n"
15711                "method();\n",
15712                Alignment);
15713   verifyFormat("int oneTwoThree = 123; // comment\n"
15714                "int oneTwo      = 12;  // comment",
15715                Alignment);
15716 
15717   // Bug 25167
15718   /* Uncomment when fixed
15719     verifyFormat("#if A\n"
15720                  "#else\n"
15721                  "int aaaaaaaa = 12;\n"
15722                  "#endif\n"
15723                  "#if B\n"
15724                  "#else\n"
15725                  "int a = 12;\n"
15726                  "#endif\n",
15727                  Alignment);
15728     verifyFormat("enum foo {\n"
15729                  "#if A\n"
15730                  "#else\n"
15731                  "  aaaaaaaa = 12;\n"
15732                  "#endif\n"
15733                  "#if B\n"
15734                  "#else\n"
15735                  "  a = 12;\n"
15736                  "#endif\n"
15737                  "};\n",
15738                  Alignment);
15739   */
15740 
15741   Alignment.MaxEmptyLinesToKeep = 10;
15742   /* Test alignment across empty lines */
15743   EXPECT_EQ("int a           = 5;\n"
15744             "\n"
15745             "int oneTwoThree = 123;",
15746             format("int a       = 5;\n"
15747                    "\n"
15748                    "int oneTwoThree= 123;",
15749                    Alignment));
15750   EXPECT_EQ("int a           = 5;\n"
15751             "int one         = 1;\n"
15752             "\n"
15753             "int oneTwoThree = 123;",
15754             format("int a = 5;\n"
15755                    "int one = 1;\n"
15756                    "\n"
15757                    "int oneTwoThree = 123;",
15758                    Alignment));
15759   EXPECT_EQ("int a           = 5;\n"
15760             "int one         = 1;\n"
15761             "\n"
15762             "int oneTwoThree = 123;\n"
15763             "int oneTwo      = 12;",
15764             format("int a = 5;\n"
15765                    "int one = 1;\n"
15766                    "\n"
15767                    "int oneTwoThree = 123;\n"
15768                    "int oneTwo = 12;",
15769                    Alignment));
15770 
15771   /* Test across comments */
15772   EXPECT_EQ("int a           = 5;\n"
15773             "/* block comment */\n"
15774             "int oneTwoThree = 123;",
15775             format("int a = 5;\n"
15776                    "/* block comment */\n"
15777                    "int oneTwoThree=123;",
15778                    Alignment));
15779 
15780   EXPECT_EQ("int a           = 5;\n"
15781             "// line comment\n"
15782             "int oneTwoThree = 123;",
15783             format("int a = 5;\n"
15784                    "// line comment\n"
15785                    "int oneTwoThree=123;",
15786                    Alignment));
15787 
15788   /* Test across comments and newlines */
15789   EXPECT_EQ("int a           = 5;\n"
15790             "\n"
15791             "/* block comment */\n"
15792             "int oneTwoThree = 123;",
15793             format("int a = 5;\n"
15794                    "\n"
15795                    "/* block comment */\n"
15796                    "int oneTwoThree=123;",
15797                    Alignment));
15798 
15799   EXPECT_EQ("int a           = 5;\n"
15800             "\n"
15801             "// line comment\n"
15802             "int oneTwoThree = 123;",
15803             format("int a = 5;\n"
15804                    "\n"
15805                    "// line comment\n"
15806                    "int oneTwoThree=123;",
15807                    Alignment));
15808 
15809   EXPECT_EQ("int a           = 5;\n"
15810             "//\n"
15811             "// multi-line line comment\n"
15812             "//\n"
15813             "int oneTwoThree = 123;",
15814             format("int a = 5;\n"
15815                    "//\n"
15816                    "// multi-line line comment\n"
15817                    "//\n"
15818                    "int oneTwoThree=123;",
15819                    Alignment));
15820 
15821   EXPECT_EQ("int a           = 5;\n"
15822             "/*\n"
15823             " *  multi-line block comment\n"
15824             " */\n"
15825             "int oneTwoThree = 123;",
15826             format("int a = 5;\n"
15827                    "/*\n"
15828                    " *  multi-line block comment\n"
15829                    " */\n"
15830                    "int oneTwoThree=123;",
15831                    Alignment));
15832 
15833   EXPECT_EQ("int a           = 5;\n"
15834             "\n"
15835             "/* block comment */\n"
15836             "\n"
15837             "\n"
15838             "\n"
15839             "int oneTwoThree = 123;",
15840             format("int a = 5;\n"
15841                    "\n"
15842                    "/* block comment */\n"
15843                    "\n"
15844                    "\n"
15845                    "\n"
15846                    "int oneTwoThree=123;",
15847                    Alignment));
15848 
15849   EXPECT_EQ("int a           = 5;\n"
15850             "\n"
15851             "// line comment\n"
15852             "\n"
15853             "\n"
15854             "\n"
15855             "int oneTwoThree = 123;",
15856             format("int a = 5;\n"
15857                    "\n"
15858                    "// line comment\n"
15859                    "\n"
15860                    "\n"
15861                    "\n"
15862                    "int oneTwoThree=123;",
15863                    Alignment));
15864 
15865   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
15866   verifyFormat("#define A \\\n"
15867                "  int aaaa       = 12; \\\n"
15868                "  int b          = 23; \\\n"
15869                "  int ccc        = 234; \\\n"
15870                "  int dddddddddd = 2345;",
15871                Alignment);
15872   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
15873   verifyFormat("#define A               \\\n"
15874                "  int aaaa       = 12;  \\\n"
15875                "  int b          = 23;  \\\n"
15876                "  int ccc        = 234; \\\n"
15877                "  int dddddddddd = 2345;",
15878                Alignment);
15879   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
15880   verifyFormat("#define A                                                      "
15881                "                \\\n"
15882                "  int aaaa       = 12;                                         "
15883                "                \\\n"
15884                "  int b          = 23;                                         "
15885                "                \\\n"
15886                "  int ccc        = 234;                                        "
15887                "                \\\n"
15888                "  int dddddddddd = 2345;",
15889                Alignment);
15890   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
15891                "k = 4, int l = 5,\n"
15892                "                  int m = 6) {\n"
15893                "  int j      = 10;\n"
15894                "  otherThing = 1;\n"
15895                "}",
15896                Alignment);
15897   verifyFormat("void SomeFunction(int parameter = 0) {\n"
15898                "  int i   = 1;\n"
15899                "  int j   = 2;\n"
15900                "  int big = 10000;\n"
15901                "}",
15902                Alignment);
15903   verifyFormat("class C {\n"
15904                "public:\n"
15905                "  int i            = 1;\n"
15906                "  virtual void f() = 0;\n"
15907                "};",
15908                Alignment);
15909   verifyFormat("int i = 1;\n"
15910                "if (SomeType t = getSomething()) {\n"
15911                "}\n"
15912                "int j   = 2;\n"
15913                "int big = 10000;",
15914                Alignment);
15915   verifyFormat("int j = 7;\n"
15916                "for (int k = 0; k < N; ++k) {\n"
15917                "}\n"
15918                "int j   = 2;\n"
15919                "int big = 10000;\n"
15920                "}",
15921                Alignment);
15922   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
15923   verifyFormat("int i = 1;\n"
15924                "LooooooooooongType loooooooooooooooooooooongVariable\n"
15925                "    = someLooooooooooooooooongFunction();\n"
15926                "int j = 2;",
15927                Alignment);
15928   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
15929   verifyFormat("int i = 1;\n"
15930                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
15931                "    someLooooooooooooooooongFunction();\n"
15932                "int j = 2;",
15933                Alignment);
15934 
15935   verifyFormat("auto lambda = []() {\n"
15936                "  auto i = 0;\n"
15937                "  return 0;\n"
15938                "};\n"
15939                "int i  = 0;\n"
15940                "auto v = type{\n"
15941                "    i = 1,   //\n"
15942                "    (i = 2), //\n"
15943                "    i = 3    //\n"
15944                "};",
15945                Alignment);
15946 
15947   verifyFormat(
15948       "int i      = 1;\n"
15949       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
15950       "                          loooooooooooooooooooooongParameterB);\n"
15951       "int j      = 2;",
15952       Alignment);
15953 
15954   verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
15955                "          typename B   = very_long_type_name_1,\n"
15956                "          typename T_2 = very_long_type_name_2>\n"
15957                "auto foo() {}\n",
15958                Alignment);
15959   verifyFormat("int a, b = 1;\n"
15960                "int c  = 2;\n"
15961                "int dd = 3;\n",
15962                Alignment);
15963   verifyFormat("int aa       = ((1 > 2) ? 3 : 4);\n"
15964                "float b[1][] = {{3.f}};\n",
15965                Alignment);
15966   verifyFormat("for (int i = 0; i < 1; i++)\n"
15967                "  int x = 1;\n",
15968                Alignment);
15969   verifyFormat("for (i = 0; i < 1; i++)\n"
15970                "  x = 1;\n"
15971                "y = 1;\n",
15972                Alignment);
15973 
15974   Alignment.ReflowComments = true;
15975   Alignment.ColumnLimit = 50;
15976   EXPECT_EQ("int x   = 0;\n"
15977             "int yy  = 1; /// specificlennospace\n"
15978             "int zzz = 2;\n",
15979             format("int x   = 0;\n"
15980                    "int yy  = 1; ///specificlennospace\n"
15981                    "int zzz = 2;\n",
15982                    Alignment));
15983 }
15984 
15985 TEST_F(FormatTest, AlignConsecutiveAssignments) {
15986   FormatStyle Alignment = getLLVMStyle();
15987   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
15988   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
15989   verifyFormat("int a = 5;\n"
15990                "int oneTwoThree = 123;",
15991                Alignment);
15992   verifyFormat("int a = 5;\n"
15993                "int oneTwoThree = 123;",
15994                Alignment);
15995 
15996   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
15997   verifyFormat("int a           = 5;\n"
15998                "int oneTwoThree = 123;",
15999                Alignment);
16000   verifyFormat("int a           = method();\n"
16001                "int oneTwoThree = 133;",
16002                Alignment);
16003   verifyFormat("a &= 5;\n"
16004                "bcd *= 5;\n"
16005                "ghtyf += 5;\n"
16006                "dvfvdb -= 5;\n"
16007                "a /= 5;\n"
16008                "vdsvsv %= 5;\n"
16009                "sfdbddfbdfbb ^= 5;\n"
16010                "dvsdsv |= 5;\n"
16011                "int dsvvdvsdvvv = 123;",
16012                Alignment);
16013   verifyFormat("int i = 1, j = 10;\n"
16014                "something = 2000;",
16015                Alignment);
16016   verifyFormat("something = 2000;\n"
16017                "int i = 1, j = 10;\n",
16018                Alignment);
16019   verifyFormat("something = 2000;\n"
16020                "another   = 911;\n"
16021                "int i = 1, j = 10;\n"
16022                "oneMore = 1;\n"
16023                "i       = 2;",
16024                Alignment);
16025   verifyFormat("int a   = 5;\n"
16026                "int one = 1;\n"
16027                "method();\n"
16028                "int oneTwoThree = 123;\n"
16029                "int oneTwo      = 12;",
16030                Alignment);
16031   verifyFormat("int oneTwoThree = 123;\n"
16032                "int oneTwo      = 12;\n"
16033                "method();\n",
16034                Alignment);
16035   verifyFormat("int oneTwoThree = 123; // comment\n"
16036                "int oneTwo      = 12;  // comment",
16037                Alignment);
16038 
16039   // Bug 25167
16040   /* Uncomment when fixed
16041     verifyFormat("#if A\n"
16042                  "#else\n"
16043                  "int aaaaaaaa = 12;\n"
16044                  "#endif\n"
16045                  "#if B\n"
16046                  "#else\n"
16047                  "int a = 12;\n"
16048                  "#endif\n",
16049                  Alignment);
16050     verifyFormat("enum foo {\n"
16051                  "#if A\n"
16052                  "#else\n"
16053                  "  aaaaaaaa = 12;\n"
16054                  "#endif\n"
16055                  "#if B\n"
16056                  "#else\n"
16057                  "  a = 12;\n"
16058                  "#endif\n"
16059                  "};\n",
16060                  Alignment);
16061   */
16062 
16063   EXPECT_EQ("int a = 5;\n"
16064             "\n"
16065             "int oneTwoThree = 123;",
16066             format("int a       = 5;\n"
16067                    "\n"
16068                    "int oneTwoThree= 123;",
16069                    Alignment));
16070   EXPECT_EQ("int a   = 5;\n"
16071             "int one = 1;\n"
16072             "\n"
16073             "int oneTwoThree = 123;",
16074             format("int a = 5;\n"
16075                    "int one = 1;\n"
16076                    "\n"
16077                    "int oneTwoThree = 123;",
16078                    Alignment));
16079   EXPECT_EQ("int a   = 5;\n"
16080             "int one = 1;\n"
16081             "\n"
16082             "int oneTwoThree = 123;\n"
16083             "int oneTwo      = 12;",
16084             format("int a = 5;\n"
16085                    "int one = 1;\n"
16086                    "\n"
16087                    "int oneTwoThree = 123;\n"
16088                    "int oneTwo = 12;",
16089                    Alignment));
16090   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
16091   verifyFormat("#define A \\\n"
16092                "  int aaaa       = 12; \\\n"
16093                "  int b          = 23; \\\n"
16094                "  int ccc        = 234; \\\n"
16095                "  int dddddddddd = 2345;",
16096                Alignment);
16097   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
16098   verifyFormat("#define A               \\\n"
16099                "  int aaaa       = 12;  \\\n"
16100                "  int b          = 23;  \\\n"
16101                "  int ccc        = 234; \\\n"
16102                "  int dddddddddd = 2345;",
16103                Alignment);
16104   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
16105   verifyFormat("#define A                                                      "
16106                "                \\\n"
16107                "  int aaaa       = 12;                                         "
16108                "                \\\n"
16109                "  int b          = 23;                                         "
16110                "                \\\n"
16111                "  int ccc        = 234;                                        "
16112                "                \\\n"
16113                "  int dddddddddd = 2345;",
16114                Alignment);
16115   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
16116                "k = 4, int l = 5,\n"
16117                "                  int m = 6) {\n"
16118                "  int j      = 10;\n"
16119                "  otherThing = 1;\n"
16120                "}",
16121                Alignment);
16122   verifyFormat("void SomeFunction(int parameter = 0) {\n"
16123                "  int i   = 1;\n"
16124                "  int j   = 2;\n"
16125                "  int big = 10000;\n"
16126                "}",
16127                Alignment);
16128   verifyFormat("class C {\n"
16129                "public:\n"
16130                "  int i            = 1;\n"
16131                "  virtual void f() = 0;\n"
16132                "};",
16133                Alignment);
16134   verifyFormat("int i = 1;\n"
16135                "if (SomeType t = getSomething()) {\n"
16136                "}\n"
16137                "int j   = 2;\n"
16138                "int big = 10000;",
16139                Alignment);
16140   verifyFormat("int j = 7;\n"
16141                "for (int k = 0; k < N; ++k) {\n"
16142                "}\n"
16143                "int j   = 2;\n"
16144                "int big = 10000;\n"
16145                "}",
16146                Alignment);
16147   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
16148   verifyFormat("int i = 1;\n"
16149                "LooooooooooongType loooooooooooooooooooooongVariable\n"
16150                "    = someLooooooooooooooooongFunction();\n"
16151                "int j = 2;",
16152                Alignment);
16153   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
16154   verifyFormat("int i = 1;\n"
16155                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
16156                "    someLooooooooooooooooongFunction();\n"
16157                "int j = 2;",
16158                Alignment);
16159 
16160   verifyFormat("auto lambda = []() {\n"
16161                "  auto i = 0;\n"
16162                "  return 0;\n"
16163                "};\n"
16164                "int i  = 0;\n"
16165                "auto v = type{\n"
16166                "    i = 1,   //\n"
16167                "    (i = 2), //\n"
16168                "    i = 3    //\n"
16169                "};",
16170                Alignment);
16171 
16172   verifyFormat(
16173       "int i      = 1;\n"
16174       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
16175       "                          loooooooooooooooooooooongParameterB);\n"
16176       "int j      = 2;",
16177       Alignment);
16178 
16179   verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
16180                "          typename B   = very_long_type_name_1,\n"
16181                "          typename T_2 = very_long_type_name_2>\n"
16182                "auto foo() {}\n",
16183                Alignment);
16184   verifyFormat("int a, b = 1;\n"
16185                "int c  = 2;\n"
16186                "int dd = 3;\n",
16187                Alignment);
16188   verifyFormat("int aa       = ((1 > 2) ? 3 : 4);\n"
16189                "float b[1][] = {{3.f}};\n",
16190                Alignment);
16191   verifyFormat("for (int i = 0; i < 1; i++)\n"
16192                "  int x = 1;\n",
16193                Alignment);
16194   verifyFormat("for (i = 0; i < 1; i++)\n"
16195                "  x = 1;\n"
16196                "y = 1;\n",
16197                Alignment);
16198 
16199   Alignment.ReflowComments = true;
16200   Alignment.ColumnLimit = 50;
16201   EXPECT_EQ("int x   = 0;\n"
16202             "int yy  = 1; /// specificlennospace\n"
16203             "int zzz = 2;\n",
16204             format("int x   = 0;\n"
16205                    "int yy  = 1; ///specificlennospace\n"
16206                    "int zzz = 2;\n",
16207                    Alignment));
16208 }
16209 
16210 TEST_F(FormatTest, AlignConsecutiveBitFields) {
16211   FormatStyle Alignment = getLLVMStyle();
16212   Alignment.AlignConsecutiveBitFields = FormatStyle::ACS_Consecutive;
16213   verifyFormat("int const a     : 5;\n"
16214                "int oneTwoThree : 23;",
16215                Alignment);
16216 
16217   // Initializers are allowed starting with c++2a
16218   verifyFormat("int const a     : 5 = 1;\n"
16219                "int oneTwoThree : 23 = 0;",
16220                Alignment);
16221 
16222   Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
16223   verifyFormat("int const a           : 5;\n"
16224                "int       oneTwoThree : 23;",
16225                Alignment);
16226 
16227   verifyFormat("int const a           : 5;  // comment\n"
16228                "int       oneTwoThree : 23; // comment",
16229                Alignment);
16230 
16231   verifyFormat("int const a           : 5 = 1;\n"
16232                "int       oneTwoThree : 23 = 0;",
16233                Alignment);
16234 
16235   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16236   verifyFormat("int const a           : 5  = 1;\n"
16237                "int       oneTwoThree : 23 = 0;",
16238                Alignment);
16239   verifyFormat("int const a           : 5  = {1};\n"
16240                "int       oneTwoThree : 23 = 0;",
16241                Alignment);
16242 
16243   Alignment.BitFieldColonSpacing = FormatStyle::BFCS_None;
16244   verifyFormat("int const a          :5;\n"
16245                "int       oneTwoThree:23;",
16246                Alignment);
16247 
16248   Alignment.BitFieldColonSpacing = FormatStyle::BFCS_Before;
16249   verifyFormat("int const a           :5;\n"
16250                "int       oneTwoThree :23;",
16251                Alignment);
16252 
16253   Alignment.BitFieldColonSpacing = FormatStyle::BFCS_After;
16254   verifyFormat("int const a          : 5;\n"
16255                "int       oneTwoThree: 23;",
16256                Alignment);
16257 
16258   // Known limitations: ':' is only recognized as a bitfield colon when
16259   // followed by a number.
16260   /*
16261   verifyFormat("int oneTwoThree : SOME_CONSTANT;\n"
16262                "int a           : 5;",
16263                Alignment);
16264   */
16265 }
16266 
16267 TEST_F(FormatTest, AlignConsecutiveDeclarations) {
16268   FormatStyle Alignment = getLLVMStyle();
16269   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
16270   Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_None;
16271   Alignment.PointerAlignment = FormatStyle::PAS_Right;
16272   verifyFormat("float const a = 5;\n"
16273                "int oneTwoThree = 123;",
16274                Alignment);
16275   verifyFormat("int a = 5;\n"
16276                "float const oneTwoThree = 123;",
16277                Alignment);
16278 
16279   Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
16280   verifyFormat("float const a = 5;\n"
16281                "int         oneTwoThree = 123;",
16282                Alignment);
16283   verifyFormat("int         a = method();\n"
16284                "float const oneTwoThree = 133;",
16285                Alignment);
16286   verifyFormat("int i = 1, j = 10;\n"
16287                "something = 2000;",
16288                Alignment);
16289   verifyFormat("something = 2000;\n"
16290                "int i = 1, j = 10;\n",
16291                Alignment);
16292   verifyFormat("float      something = 2000;\n"
16293                "double     another = 911;\n"
16294                "int        i = 1, j = 10;\n"
16295                "const int *oneMore = 1;\n"
16296                "unsigned   i = 2;",
16297                Alignment);
16298   verifyFormat("float a = 5;\n"
16299                "int   one = 1;\n"
16300                "method();\n"
16301                "const double       oneTwoThree = 123;\n"
16302                "const unsigned int oneTwo = 12;",
16303                Alignment);
16304   verifyFormat("int      oneTwoThree{0}; // comment\n"
16305                "unsigned oneTwo;         // comment",
16306                Alignment);
16307   verifyFormat("unsigned int       *a;\n"
16308                "int                *b;\n"
16309                "unsigned int Const *c;\n"
16310                "unsigned int const *d;\n"
16311                "unsigned int Const &e;\n"
16312                "unsigned int const &f;",
16313                Alignment);
16314   verifyFormat("Const unsigned int *c;\n"
16315                "const unsigned int *d;\n"
16316                "Const unsigned int &e;\n"
16317                "const unsigned int &f;\n"
16318                "const unsigned      g;\n"
16319                "Const unsigned      h;",
16320                Alignment);
16321   EXPECT_EQ("float const a = 5;\n"
16322             "\n"
16323             "int oneTwoThree = 123;",
16324             format("float const   a = 5;\n"
16325                    "\n"
16326                    "int           oneTwoThree= 123;",
16327                    Alignment));
16328   EXPECT_EQ("float a = 5;\n"
16329             "int   one = 1;\n"
16330             "\n"
16331             "unsigned oneTwoThree = 123;",
16332             format("float    a = 5;\n"
16333                    "int      one = 1;\n"
16334                    "\n"
16335                    "unsigned oneTwoThree = 123;",
16336                    Alignment));
16337   EXPECT_EQ("float a = 5;\n"
16338             "int   one = 1;\n"
16339             "\n"
16340             "unsigned oneTwoThree = 123;\n"
16341             "int      oneTwo = 12;",
16342             format("float    a = 5;\n"
16343                    "int one = 1;\n"
16344                    "\n"
16345                    "unsigned oneTwoThree = 123;\n"
16346                    "int oneTwo = 12;",
16347                    Alignment));
16348   // Function prototype alignment
16349   verifyFormat("int    a();\n"
16350                "double b();",
16351                Alignment);
16352   verifyFormat("int    a(int x);\n"
16353                "double b();",
16354                Alignment);
16355   unsigned OldColumnLimit = Alignment.ColumnLimit;
16356   // We need to set ColumnLimit to zero, in order to stress nested alignments,
16357   // otherwise the function parameters will be re-flowed onto a single line.
16358   Alignment.ColumnLimit = 0;
16359   EXPECT_EQ("int    a(int   x,\n"
16360             "         float y);\n"
16361             "double b(int    x,\n"
16362             "         double y);",
16363             format("int a(int x,\n"
16364                    " float y);\n"
16365                    "double b(int x,\n"
16366                    " double y);",
16367                    Alignment));
16368   // This ensures that function parameters of function declarations are
16369   // correctly indented when their owning functions are indented.
16370   // The failure case here is for 'double y' to not be indented enough.
16371   EXPECT_EQ("double a(int x);\n"
16372             "int    b(int    y,\n"
16373             "         double z);",
16374             format("double a(int x);\n"
16375                    "int b(int y,\n"
16376                    " double z);",
16377                    Alignment));
16378   // Set ColumnLimit low so that we induce wrapping immediately after
16379   // the function name and opening paren.
16380   Alignment.ColumnLimit = 13;
16381   verifyFormat("int function(\n"
16382                "    int  x,\n"
16383                "    bool y);",
16384                Alignment);
16385   Alignment.ColumnLimit = OldColumnLimit;
16386   // Ensure function pointers don't screw up recursive alignment
16387   verifyFormat("int    a(int x, void (*fp)(int y));\n"
16388                "double b();",
16389                Alignment);
16390   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16391   // Ensure recursive alignment is broken by function braces, so that the
16392   // "a = 1" does not align with subsequent assignments inside the function
16393   // body.
16394   verifyFormat("int func(int a = 1) {\n"
16395                "  int b  = 2;\n"
16396                "  int cc = 3;\n"
16397                "}",
16398                Alignment);
16399   verifyFormat("float      something = 2000;\n"
16400                "double     another   = 911;\n"
16401                "int        i = 1, j = 10;\n"
16402                "const int *oneMore = 1;\n"
16403                "unsigned   i       = 2;",
16404                Alignment);
16405   verifyFormat("int      oneTwoThree = {0}; // comment\n"
16406                "unsigned oneTwo      = 0;   // comment",
16407                Alignment);
16408   // Make sure that scope is correctly tracked, in the absence of braces
16409   verifyFormat("for (int i = 0; i < n; i++)\n"
16410                "  j = i;\n"
16411                "double x = 1;\n",
16412                Alignment);
16413   verifyFormat("if (int i = 0)\n"
16414                "  j = i;\n"
16415                "double x = 1;\n",
16416                Alignment);
16417   // Ensure operator[] and operator() are comprehended
16418   verifyFormat("struct test {\n"
16419                "  long long int foo();\n"
16420                "  int           operator[](int a);\n"
16421                "  double        bar();\n"
16422                "};\n",
16423                Alignment);
16424   verifyFormat("struct test {\n"
16425                "  long long int foo();\n"
16426                "  int           operator()(int a);\n"
16427                "  double        bar();\n"
16428                "};\n",
16429                Alignment);
16430   // http://llvm.org/PR52914
16431   verifyFormat("char *a[]     = {\"a\", // comment\n"
16432                "                 \"bb\"};\n"
16433                "int   bbbbbbb = 0;",
16434                Alignment);
16435 
16436   // PAS_Right
16437   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
16438             "  int const i   = 1;\n"
16439             "  int      *j   = 2;\n"
16440             "  int       big = 10000;\n"
16441             "\n"
16442             "  unsigned oneTwoThree = 123;\n"
16443             "  int      oneTwo      = 12;\n"
16444             "  method();\n"
16445             "  float k  = 2;\n"
16446             "  int   ll = 10000;\n"
16447             "}",
16448             format("void SomeFunction(int parameter= 0) {\n"
16449                    " int const  i= 1;\n"
16450                    "  int *j=2;\n"
16451                    " int big  =  10000;\n"
16452                    "\n"
16453                    "unsigned oneTwoThree  =123;\n"
16454                    "int oneTwo = 12;\n"
16455                    "  method();\n"
16456                    "float k= 2;\n"
16457                    "int ll=10000;\n"
16458                    "}",
16459                    Alignment));
16460   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
16461             "  int const i   = 1;\n"
16462             "  int     **j   = 2, ***k;\n"
16463             "  int      &k   = i;\n"
16464             "  int     &&l   = i + j;\n"
16465             "  int       big = 10000;\n"
16466             "\n"
16467             "  unsigned oneTwoThree = 123;\n"
16468             "  int      oneTwo      = 12;\n"
16469             "  method();\n"
16470             "  float k  = 2;\n"
16471             "  int   ll = 10000;\n"
16472             "}",
16473             format("void SomeFunction(int parameter= 0) {\n"
16474                    " int const  i= 1;\n"
16475                    "  int **j=2,***k;\n"
16476                    "int &k=i;\n"
16477                    "int &&l=i+j;\n"
16478                    " int big  =  10000;\n"
16479                    "\n"
16480                    "unsigned oneTwoThree  =123;\n"
16481                    "int oneTwo = 12;\n"
16482                    "  method();\n"
16483                    "float k= 2;\n"
16484                    "int ll=10000;\n"
16485                    "}",
16486                    Alignment));
16487   // variables are aligned at their name, pointers are at the right most
16488   // position
16489   verifyFormat("int   *a;\n"
16490                "int  **b;\n"
16491                "int ***c;\n"
16492                "int    foobar;\n",
16493                Alignment);
16494 
16495   // PAS_Left
16496   FormatStyle AlignmentLeft = Alignment;
16497   AlignmentLeft.PointerAlignment = FormatStyle::PAS_Left;
16498   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
16499             "  int const i   = 1;\n"
16500             "  int*      j   = 2;\n"
16501             "  int       big = 10000;\n"
16502             "\n"
16503             "  unsigned oneTwoThree = 123;\n"
16504             "  int      oneTwo      = 12;\n"
16505             "  method();\n"
16506             "  float k  = 2;\n"
16507             "  int   ll = 10000;\n"
16508             "}",
16509             format("void SomeFunction(int parameter= 0) {\n"
16510                    " int const  i= 1;\n"
16511                    "  int *j=2;\n"
16512                    " int big  =  10000;\n"
16513                    "\n"
16514                    "unsigned oneTwoThree  =123;\n"
16515                    "int oneTwo = 12;\n"
16516                    "  method();\n"
16517                    "float k= 2;\n"
16518                    "int ll=10000;\n"
16519                    "}",
16520                    AlignmentLeft));
16521   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
16522             "  int const i   = 1;\n"
16523             "  int**     j   = 2;\n"
16524             "  int&      k   = i;\n"
16525             "  int&&     l   = i + j;\n"
16526             "  int       big = 10000;\n"
16527             "\n"
16528             "  unsigned oneTwoThree = 123;\n"
16529             "  int      oneTwo      = 12;\n"
16530             "  method();\n"
16531             "  float k  = 2;\n"
16532             "  int   ll = 10000;\n"
16533             "}",
16534             format("void SomeFunction(int parameter= 0) {\n"
16535                    " int const  i= 1;\n"
16536                    "  int **j=2;\n"
16537                    "int &k=i;\n"
16538                    "int &&l=i+j;\n"
16539                    " int big  =  10000;\n"
16540                    "\n"
16541                    "unsigned oneTwoThree  =123;\n"
16542                    "int oneTwo = 12;\n"
16543                    "  method();\n"
16544                    "float k= 2;\n"
16545                    "int ll=10000;\n"
16546                    "}",
16547                    AlignmentLeft));
16548   // variables are aligned at their name, pointers are at the left most position
16549   verifyFormat("int*   a;\n"
16550                "int**  b;\n"
16551                "int*** c;\n"
16552                "int    foobar;\n",
16553                AlignmentLeft);
16554 
16555   // PAS_Middle
16556   FormatStyle AlignmentMiddle = Alignment;
16557   AlignmentMiddle.PointerAlignment = FormatStyle::PAS_Middle;
16558   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
16559             "  int const i   = 1;\n"
16560             "  int *     j   = 2;\n"
16561             "  int       big = 10000;\n"
16562             "\n"
16563             "  unsigned oneTwoThree = 123;\n"
16564             "  int      oneTwo      = 12;\n"
16565             "  method();\n"
16566             "  float k  = 2;\n"
16567             "  int   ll = 10000;\n"
16568             "}",
16569             format("void SomeFunction(int parameter= 0) {\n"
16570                    " int const  i= 1;\n"
16571                    "  int *j=2;\n"
16572                    " int big  =  10000;\n"
16573                    "\n"
16574                    "unsigned oneTwoThree  =123;\n"
16575                    "int oneTwo = 12;\n"
16576                    "  method();\n"
16577                    "float k= 2;\n"
16578                    "int ll=10000;\n"
16579                    "}",
16580                    AlignmentMiddle));
16581   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
16582             "  int const i   = 1;\n"
16583             "  int **    j   = 2, ***k;\n"
16584             "  int &     k   = i;\n"
16585             "  int &&    l   = i + j;\n"
16586             "  int       big = 10000;\n"
16587             "\n"
16588             "  unsigned oneTwoThree = 123;\n"
16589             "  int      oneTwo      = 12;\n"
16590             "  method();\n"
16591             "  float k  = 2;\n"
16592             "  int   ll = 10000;\n"
16593             "}",
16594             format("void SomeFunction(int parameter= 0) {\n"
16595                    " int const  i= 1;\n"
16596                    "  int **j=2,***k;\n"
16597                    "int &k=i;\n"
16598                    "int &&l=i+j;\n"
16599                    " int big  =  10000;\n"
16600                    "\n"
16601                    "unsigned oneTwoThree  =123;\n"
16602                    "int oneTwo = 12;\n"
16603                    "  method();\n"
16604                    "float k= 2;\n"
16605                    "int ll=10000;\n"
16606                    "}",
16607                    AlignmentMiddle));
16608   // variables are aligned at their name, pointers are in the middle
16609   verifyFormat("int *   a;\n"
16610                "int *   b;\n"
16611                "int *** c;\n"
16612                "int     foobar;\n",
16613                AlignmentMiddle);
16614 
16615   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
16616   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
16617   verifyFormat("#define A \\\n"
16618                "  int       aaaa = 12; \\\n"
16619                "  float     b = 23; \\\n"
16620                "  const int ccc = 234; \\\n"
16621                "  unsigned  dddddddddd = 2345;",
16622                Alignment);
16623   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
16624   verifyFormat("#define A              \\\n"
16625                "  int       aaaa = 12; \\\n"
16626                "  float     b = 23;    \\\n"
16627                "  const int ccc = 234; \\\n"
16628                "  unsigned  dddddddddd = 2345;",
16629                Alignment);
16630   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
16631   Alignment.ColumnLimit = 30;
16632   verifyFormat("#define A                    \\\n"
16633                "  int       aaaa = 12;       \\\n"
16634                "  float     b = 23;          \\\n"
16635                "  const int ccc = 234;       \\\n"
16636                "  int       dddddddddd = 2345;",
16637                Alignment);
16638   Alignment.ColumnLimit = 80;
16639   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
16640                "k = 4, int l = 5,\n"
16641                "                  int m = 6) {\n"
16642                "  const int j = 10;\n"
16643                "  otherThing = 1;\n"
16644                "}",
16645                Alignment);
16646   verifyFormat("void SomeFunction(int parameter = 0) {\n"
16647                "  int const i = 1;\n"
16648                "  int      *j = 2;\n"
16649                "  int       big = 10000;\n"
16650                "}",
16651                Alignment);
16652   verifyFormat("class C {\n"
16653                "public:\n"
16654                "  int          i = 1;\n"
16655                "  virtual void f() = 0;\n"
16656                "};",
16657                Alignment);
16658   verifyFormat("float i = 1;\n"
16659                "if (SomeType t = getSomething()) {\n"
16660                "}\n"
16661                "const unsigned j = 2;\n"
16662                "int            big = 10000;",
16663                Alignment);
16664   verifyFormat("float j = 7;\n"
16665                "for (int k = 0; k < N; ++k) {\n"
16666                "}\n"
16667                "unsigned j = 2;\n"
16668                "int      big = 10000;\n"
16669                "}",
16670                Alignment);
16671   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
16672   verifyFormat("float              i = 1;\n"
16673                "LooooooooooongType loooooooooooooooooooooongVariable\n"
16674                "    = someLooooooooooooooooongFunction();\n"
16675                "int j = 2;",
16676                Alignment);
16677   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
16678   verifyFormat("int                i = 1;\n"
16679                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
16680                "    someLooooooooooooooooongFunction();\n"
16681                "int j = 2;",
16682                Alignment);
16683 
16684   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16685   verifyFormat("auto lambda = []() {\n"
16686                "  auto  ii = 0;\n"
16687                "  float j  = 0;\n"
16688                "  return 0;\n"
16689                "};\n"
16690                "int   i  = 0;\n"
16691                "float i2 = 0;\n"
16692                "auto  v  = type{\n"
16693                "    i = 1,   //\n"
16694                "    (i = 2), //\n"
16695                "    i = 3    //\n"
16696                "};",
16697                Alignment);
16698   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
16699 
16700   verifyFormat(
16701       "int      i = 1;\n"
16702       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
16703       "                          loooooooooooooooooooooongParameterB);\n"
16704       "int      j = 2;",
16705       Alignment);
16706 
16707   // Test interactions with ColumnLimit and AlignConsecutiveAssignments:
16708   // We expect declarations and assignments to align, as long as it doesn't
16709   // exceed the column limit, starting a new alignment sequence whenever it
16710   // happens.
16711   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16712   Alignment.ColumnLimit = 30;
16713   verifyFormat("float    ii              = 1;\n"
16714                "unsigned j               = 2;\n"
16715                "int someVerylongVariable = 1;\n"
16716                "AnotherLongType  ll = 123456;\n"
16717                "VeryVeryLongType k  = 2;\n"
16718                "int              myvar = 1;",
16719                Alignment);
16720   Alignment.ColumnLimit = 80;
16721   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
16722 
16723   verifyFormat(
16724       "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n"
16725       "          typename LongType, typename B>\n"
16726       "auto foo() {}\n",
16727       Alignment);
16728   verifyFormat("float a, b = 1;\n"
16729                "int   c = 2;\n"
16730                "int   dd = 3;\n",
16731                Alignment);
16732   verifyFormat("int   aa = ((1 > 2) ? 3 : 4);\n"
16733                "float b[1][] = {{3.f}};\n",
16734                Alignment);
16735   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16736   verifyFormat("float a, b = 1;\n"
16737                "int   c  = 2;\n"
16738                "int   dd = 3;\n",
16739                Alignment);
16740   verifyFormat("int   aa     = ((1 > 2) ? 3 : 4);\n"
16741                "float b[1][] = {{3.f}};\n",
16742                Alignment);
16743   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
16744 
16745   Alignment.ColumnLimit = 30;
16746   Alignment.BinPackParameters = false;
16747   verifyFormat("void foo(float     a,\n"
16748                "         float     b,\n"
16749                "         int       c,\n"
16750                "         uint32_t *d) {\n"
16751                "  int   *e = 0;\n"
16752                "  float  f = 0;\n"
16753                "  double g = 0;\n"
16754                "}\n"
16755                "void bar(ino_t     a,\n"
16756                "         int       b,\n"
16757                "         uint32_t *c,\n"
16758                "         bool      d) {}\n",
16759                Alignment);
16760   Alignment.BinPackParameters = true;
16761   Alignment.ColumnLimit = 80;
16762 
16763   // Bug 33507
16764   Alignment.PointerAlignment = FormatStyle::PAS_Middle;
16765   verifyFormat(
16766       "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n"
16767       "  static const Version verVs2017;\n"
16768       "  return true;\n"
16769       "});\n",
16770       Alignment);
16771   Alignment.PointerAlignment = FormatStyle::PAS_Right;
16772 
16773   // See llvm.org/PR35641
16774   Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
16775   verifyFormat("int func() { //\n"
16776                "  int      b;\n"
16777                "  unsigned c;\n"
16778                "}",
16779                Alignment);
16780 
16781   // See PR37175
16782   FormatStyle Style = getMozillaStyle();
16783   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
16784   EXPECT_EQ("DECOR1 /**/ int8_t /**/ DECOR2 /**/\n"
16785             "foo(int a);",
16786             format("DECOR1 /**/ int8_t /**/ DECOR2 /**/ foo (int a);", Style));
16787 
16788   Alignment.PointerAlignment = FormatStyle::PAS_Left;
16789   verifyFormat("unsigned int*       a;\n"
16790                "int*                b;\n"
16791                "unsigned int Const* c;\n"
16792                "unsigned int const* d;\n"
16793                "unsigned int Const& e;\n"
16794                "unsigned int const& f;",
16795                Alignment);
16796   verifyFormat("Const unsigned int* c;\n"
16797                "const unsigned int* d;\n"
16798                "Const unsigned int& e;\n"
16799                "const unsigned int& f;\n"
16800                "const unsigned      g;\n"
16801                "Const unsigned      h;",
16802                Alignment);
16803 
16804   Alignment.PointerAlignment = FormatStyle::PAS_Middle;
16805   verifyFormat("unsigned int *       a;\n"
16806                "int *                b;\n"
16807                "unsigned int Const * c;\n"
16808                "unsigned int const * d;\n"
16809                "unsigned int Const & e;\n"
16810                "unsigned int const & f;",
16811                Alignment);
16812   verifyFormat("Const unsigned int * c;\n"
16813                "const unsigned int * d;\n"
16814                "Const unsigned int & e;\n"
16815                "const unsigned int & f;\n"
16816                "const unsigned       g;\n"
16817                "Const unsigned       h;",
16818                Alignment);
16819 }
16820 
16821 TEST_F(FormatTest, AlignWithLineBreaks) {
16822   auto Style = getLLVMStyleWithColumns(120);
16823 
16824   EXPECT_EQ(Style.AlignConsecutiveAssignments, FormatStyle::ACS_None);
16825   EXPECT_EQ(Style.AlignConsecutiveDeclarations, FormatStyle::ACS_None);
16826   verifyFormat("void foo() {\n"
16827                "  int myVar = 5;\n"
16828                "  double x = 3.14;\n"
16829                "  auto str = \"Hello \"\n"
16830                "             \"World\";\n"
16831                "  auto s = \"Hello \"\n"
16832                "           \"Again\";\n"
16833                "}",
16834                Style);
16835 
16836   // clang-format off
16837   verifyFormat("void foo() {\n"
16838                "  const int capacityBefore = Entries.capacity();\n"
16839                "  const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
16840                "                                            std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
16841                "  const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
16842                "                                          std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
16843                "}",
16844                Style);
16845   // clang-format on
16846 
16847   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16848   verifyFormat("void foo() {\n"
16849                "  int myVar = 5;\n"
16850                "  double x  = 3.14;\n"
16851                "  auto str  = \"Hello \"\n"
16852                "              \"World\";\n"
16853                "  auto s    = \"Hello \"\n"
16854                "              \"Again\";\n"
16855                "}",
16856                Style);
16857 
16858   // clang-format off
16859   verifyFormat("void foo() {\n"
16860                "  const int capacityBefore = Entries.capacity();\n"
16861                "  const auto newEntry      = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
16862                "                                                 std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
16863                "  const X newEntry2        = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
16864                "                                                 std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
16865                "}",
16866                Style);
16867   // clang-format on
16868 
16869   Style.AlignConsecutiveAssignments = FormatStyle::ACS_None;
16870   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
16871   verifyFormat("void foo() {\n"
16872                "  int    myVar = 5;\n"
16873                "  double x = 3.14;\n"
16874                "  auto   str = \"Hello \"\n"
16875                "               \"World\";\n"
16876                "  auto   s = \"Hello \"\n"
16877                "             \"Again\";\n"
16878                "}",
16879                Style);
16880 
16881   // clang-format off
16882   verifyFormat("void foo() {\n"
16883                "  const int  capacityBefore = Entries.capacity();\n"
16884                "  const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
16885                "                                            std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
16886                "  const X    newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
16887                "                                             std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
16888                "}",
16889                Style);
16890   // clang-format on
16891 
16892   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16893   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
16894 
16895   verifyFormat("void foo() {\n"
16896                "  int    myVar = 5;\n"
16897                "  double x     = 3.14;\n"
16898                "  auto   str   = \"Hello \"\n"
16899                "                 \"World\";\n"
16900                "  auto   s     = \"Hello \"\n"
16901                "                 \"Again\";\n"
16902                "}",
16903                Style);
16904 
16905   // clang-format off
16906   verifyFormat("void foo() {\n"
16907                "  const int  capacityBefore = Entries.capacity();\n"
16908                "  const auto newEntry       = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
16909                "                                                  std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
16910                "  const X    newEntry2      = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
16911                "                                                  std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
16912                "}",
16913                Style);
16914   // clang-format on
16915 
16916   Style = getLLVMStyleWithColumns(120);
16917   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16918   Style.ContinuationIndentWidth = 4;
16919   Style.IndentWidth = 4;
16920 
16921   // clang-format off
16922   verifyFormat("void SomeFunc() {\n"
16923                "    newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n"
16924                "                                                        seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
16925                "    newWatcher.maxAge     = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n"
16926                "                                                        seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
16927                "    newWatcher.max        = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n"
16928                "                                                        seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
16929                "}",
16930                Style);
16931   // clang-format on
16932 
16933   Style.BinPackArguments = false;
16934 
16935   // clang-format off
16936   verifyFormat("void SomeFunc() {\n"
16937                "    newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(\n"
16938                "        FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
16939                "    newWatcher.maxAge     = ToLegacyTimestamp(GetMaxAge(\n"
16940                "        FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
16941                "    newWatcher.max        = ToLegacyTimestamp(GetMaxAge(\n"
16942                "        FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
16943                "}",
16944                Style);
16945   // clang-format on
16946 }
16947 
16948 TEST_F(FormatTest, AlignWithInitializerPeriods) {
16949   auto Style = getLLVMStyleWithColumns(60);
16950 
16951   verifyFormat("void foo1(void) {\n"
16952                "  BYTE p[1] = 1;\n"
16953                "  A B = {.one_foooooooooooooooo = 2,\n"
16954                "         .two_fooooooooooooo = 3,\n"
16955                "         .three_fooooooooooooo = 4};\n"
16956                "  BYTE payload = 2;\n"
16957                "}",
16958                Style);
16959 
16960   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16961   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_None;
16962   verifyFormat("void foo2(void) {\n"
16963                "  BYTE p[1]    = 1;\n"
16964                "  A B          = {.one_foooooooooooooooo = 2,\n"
16965                "                  .two_fooooooooooooo    = 3,\n"
16966                "                  .three_fooooooooooooo  = 4};\n"
16967                "  BYTE payload = 2;\n"
16968                "}",
16969                Style);
16970 
16971   Style.AlignConsecutiveAssignments = FormatStyle::ACS_None;
16972   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
16973   verifyFormat("void foo3(void) {\n"
16974                "  BYTE p[1] = 1;\n"
16975                "  A    B = {.one_foooooooooooooooo = 2,\n"
16976                "            .two_fooooooooooooo = 3,\n"
16977                "            .three_fooooooooooooo = 4};\n"
16978                "  BYTE payload = 2;\n"
16979                "}",
16980                Style);
16981 
16982   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16983   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
16984   verifyFormat("void foo4(void) {\n"
16985                "  BYTE p[1]    = 1;\n"
16986                "  A    B       = {.one_foooooooooooooooo = 2,\n"
16987                "                  .two_fooooooooooooo    = 3,\n"
16988                "                  .three_fooooooooooooo  = 4};\n"
16989                "  BYTE payload = 2;\n"
16990                "}",
16991                Style);
16992 }
16993 
16994 TEST_F(FormatTest, LinuxBraceBreaking) {
16995   FormatStyle LinuxBraceStyle = getLLVMStyle();
16996   LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
16997   verifyFormat("namespace a\n"
16998                "{\n"
16999                "class A\n"
17000                "{\n"
17001                "  void f()\n"
17002                "  {\n"
17003                "    if (true) {\n"
17004                "      a();\n"
17005                "      b();\n"
17006                "    } else {\n"
17007                "      a();\n"
17008                "    }\n"
17009                "  }\n"
17010                "  void g() { return; }\n"
17011                "};\n"
17012                "struct B {\n"
17013                "  int x;\n"
17014                "};\n"
17015                "} // namespace a\n",
17016                LinuxBraceStyle);
17017   verifyFormat("enum X {\n"
17018                "  Y = 0,\n"
17019                "}\n",
17020                LinuxBraceStyle);
17021   verifyFormat("struct S {\n"
17022                "  int Type;\n"
17023                "  union {\n"
17024                "    int x;\n"
17025                "    double y;\n"
17026                "  } Value;\n"
17027                "  class C\n"
17028                "  {\n"
17029                "    MyFavoriteType Value;\n"
17030                "  } Class;\n"
17031                "}\n",
17032                LinuxBraceStyle);
17033 }
17034 
17035 TEST_F(FormatTest, MozillaBraceBreaking) {
17036   FormatStyle MozillaBraceStyle = getLLVMStyle();
17037   MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
17038   MozillaBraceStyle.FixNamespaceComments = false;
17039   verifyFormat("namespace a {\n"
17040                "class A\n"
17041                "{\n"
17042                "  void f()\n"
17043                "  {\n"
17044                "    if (true) {\n"
17045                "      a();\n"
17046                "      b();\n"
17047                "    }\n"
17048                "  }\n"
17049                "  void g() { return; }\n"
17050                "};\n"
17051                "enum E\n"
17052                "{\n"
17053                "  A,\n"
17054                "  // foo\n"
17055                "  B,\n"
17056                "  C\n"
17057                "};\n"
17058                "struct B\n"
17059                "{\n"
17060                "  int x;\n"
17061                "};\n"
17062                "}\n",
17063                MozillaBraceStyle);
17064   verifyFormat("struct S\n"
17065                "{\n"
17066                "  int Type;\n"
17067                "  union\n"
17068                "  {\n"
17069                "    int x;\n"
17070                "    double y;\n"
17071                "  } Value;\n"
17072                "  class C\n"
17073                "  {\n"
17074                "    MyFavoriteType Value;\n"
17075                "  } Class;\n"
17076                "}\n",
17077                MozillaBraceStyle);
17078 }
17079 
17080 TEST_F(FormatTest, StroustrupBraceBreaking) {
17081   FormatStyle StroustrupBraceStyle = getLLVMStyle();
17082   StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
17083   verifyFormat("namespace a {\n"
17084                "class A {\n"
17085                "  void f()\n"
17086                "  {\n"
17087                "    if (true) {\n"
17088                "      a();\n"
17089                "      b();\n"
17090                "    }\n"
17091                "  }\n"
17092                "  void g() { return; }\n"
17093                "};\n"
17094                "struct B {\n"
17095                "  int x;\n"
17096                "};\n"
17097                "} // namespace a\n",
17098                StroustrupBraceStyle);
17099 
17100   verifyFormat("void foo()\n"
17101                "{\n"
17102                "  if (a) {\n"
17103                "    a();\n"
17104                "  }\n"
17105                "  else {\n"
17106                "    b();\n"
17107                "  }\n"
17108                "}\n",
17109                StroustrupBraceStyle);
17110 
17111   verifyFormat("#ifdef _DEBUG\n"
17112                "int foo(int i = 0)\n"
17113                "#else\n"
17114                "int foo(int i = 5)\n"
17115                "#endif\n"
17116                "{\n"
17117                "  return i;\n"
17118                "}",
17119                StroustrupBraceStyle);
17120 
17121   verifyFormat("void foo() {}\n"
17122                "void bar()\n"
17123                "#ifdef _DEBUG\n"
17124                "{\n"
17125                "  foo();\n"
17126                "}\n"
17127                "#else\n"
17128                "{\n"
17129                "}\n"
17130                "#endif",
17131                StroustrupBraceStyle);
17132 
17133   verifyFormat("void foobar() { int i = 5; }\n"
17134                "#ifdef _DEBUG\n"
17135                "void bar() {}\n"
17136                "#else\n"
17137                "void bar() { foobar(); }\n"
17138                "#endif",
17139                StroustrupBraceStyle);
17140 }
17141 
17142 TEST_F(FormatTest, AllmanBraceBreaking) {
17143   FormatStyle AllmanBraceStyle = getLLVMStyle();
17144   AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
17145 
17146   EXPECT_EQ("namespace a\n"
17147             "{\n"
17148             "void f();\n"
17149             "void g();\n"
17150             "} // namespace a\n",
17151             format("namespace a\n"
17152                    "{\n"
17153                    "void f();\n"
17154                    "void g();\n"
17155                    "}\n",
17156                    AllmanBraceStyle));
17157 
17158   verifyFormat("namespace a\n"
17159                "{\n"
17160                "class A\n"
17161                "{\n"
17162                "  void f()\n"
17163                "  {\n"
17164                "    if (true)\n"
17165                "    {\n"
17166                "      a();\n"
17167                "      b();\n"
17168                "    }\n"
17169                "  }\n"
17170                "  void g() { return; }\n"
17171                "};\n"
17172                "struct B\n"
17173                "{\n"
17174                "  int x;\n"
17175                "};\n"
17176                "union C\n"
17177                "{\n"
17178                "};\n"
17179                "} // namespace a",
17180                AllmanBraceStyle);
17181 
17182   verifyFormat("void f()\n"
17183                "{\n"
17184                "  if (true)\n"
17185                "  {\n"
17186                "    a();\n"
17187                "  }\n"
17188                "  else if (false)\n"
17189                "  {\n"
17190                "    b();\n"
17191                "  }\n"
17192                "  else\n"
17193                "  {\n"
17194                "    c();\n"
17195                "  }\n"
17196                "}\n",
17197                AllmanBraceStyle);
17198 
17199   verifyFormat("void f()\n"
17200                "{\n"
17201                "  for (int i = 0; i < 10; ++i)\n"
17202                "  {\n"
17203                "    a();\n"
17204                "  }\n"
17205                "  while (false)\n"
17206                "  {\n"
17207                "    b();\n"
17208                "  }\n"
17209                "  do\n"
17210                "  {\n"
17211                "    c();\n"
17212                "  } while (false)\n"
17213                "}\n",
17214                AllmanBraceStyle);
17215 
17216   verifyFormat("void f(int a)\n"
17217                "{\n"
17218                "  switch (a)\n"
17219                "  {\n"
17220                "  case 0:\n"
17221                "    break;\n"
17222                "  case 1:\n"
17223                "  {\n"
17224                "    break;\n"
17225                "  }\n"
17226                "  case 2:\n"
17227                "  {\n"
17228                "  }\n"
17229                "  break;\n"
17230                "  default:\n"
17231                "    break;\n"
17232                "  }\n"
17233                "}\n",
17234                AllmanBraceStyle);
17235 
17236   verifyFormat("enum X\n"
17237                "{\n"
17238                "  Y = 0,\n"
17239                "}\n",
17240                AllmanBraceStyle);
17241   verifyFormat("enum X\n"
17242                "{\n"
17243                "  Y = 0\n"
17244                "}\n",
17245                AllmanBraceStyle);
17246 
17247   verifyFormat("@interface BSApplicationController ()\n"
17248                "{\n"
17249                "@private\n"
17250                "  id _extraIvar;\n"
17251                "}\n"
17252                "@end\n",
17253                AllmanBraceStyle);
17254 
17255   verifyFormat("#ifdef _DEBUG\n"
17256                "int foo(int i = 0)\n"
17257                "#else\n"
17258                "int foo(int i = 5)\n"
17259                "#endif\n"
17260                "{\n"
17261                "  return i;\n"
17262                "}",
17263                AllmanBraceStyle);
17264 
17265   verifyFormat("void foo() {}\n"
17266                "void bar()\n"
17267                "#ifdef _DEBUG\n"
17268                "{\n"
17269                "  foo();\n"
17270                "}\n"
17271                "#else\n"
17272                "{\n"
17273                "}\n"
17274                "#endif",
17275                AllmanBraceStyle);
17276 
17277   verifyFormat("void foobar() { int i = 5; }\n"
17278                "#ifdef _DEBUG\n"
17279                "void bar() {}\n"
17280                "#else\n"
17281                "void bar() { foobar(); }\n"
17282                "#endif",
17283                AllmanBraceStyle);
17284 
17285   EXPECT_EQ(AllmanBraceStyle.AllowShortLambdasOnASingleLine,
17286             FormatStyle::SLS_All);
17287 
17288   verifyFormat("[](int i) { return i + 2; };\n"
17289                "[](int i, int j)\n"
17290                "{\n"
17291                "  auto x = i + j;\n"
17292                "  auto y = i * j;\n"
17293                "  return x ^ y;\n"
17294                "};\n"
17295                "void foo()\n"
17296                "{\n"
17297                "  auto shortLambda = [](int i) { return i + 2; };\n"
17298                "  auto longLambda = [](int i, int j)\n"
17299                "  {\n"
17300                "    auto x = i + j;\n"
17301                "    auto y = i * j;\n"
17302                "    return x ^ y;\n"
17303                "  };\n"
17304                "}",
17305                AllmanBraceStyle);
17306 
17307   AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
17308 
17309   verifyFormat("[](int i)\n"
17310                "{\n"
17311                "  return i + 2;\n"
17312                "};\n"
17313                "[](int i, int j)\n"
17314                "{\n"
17315                "  auto x = i + j;\n"
17316                "  auto y = i * j;\n"
17317                "  return x ^ y;\n"
17318                "};\n"
17319                "void foo()\n"
17320                "{\n"
17321                "  auto shortLambda = [](int i)\n"
17322                "  {\n"
17323                "    return i + 2;\n"
17324                "  };\n"
17325                "  auto longLambda = [](int i, int j)\n"
17326                "  {\n"
17327                "    auto x = i + j;\n"
17328                "    auto y = i * j;\n"
17329                "    return x ^ y;\n"
17330                "  };\n"
17331                "}",
17332                AllmanBraceStyle);
17333 
17334   // Reset
17335   AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_All;
17336 
17337   // This shouldn't affect ObjC blocks..
17338   verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
17339                "  // ...\n"
17340                "  int i;\n"
17341                "}];",
17342                AllmanBraceStyle);
17343   verifyFormat("void (^block)(void) = ^{\n"
17344                "  // ...\n"
17345                "  int i;\n"
17346                "};",
17347                AllmanBraceStyle);
17348   // .. or dict literals.
17349   verifyFormat("void f()\n"
17350                "{\n"
17351                "  // ...\n"
17352                "  [object someMethod:@{@\"a\" : @\"b\"}];\n"
17353                "}",
17354                AllmanBraceStyle);
17355   verifyFormat("void f()\n"
17356                "{\n"
17357                "  // ...\n"
17358                "  [object someMethod:@{a : @\"b\"}];\n"
17359                "}",
17360                AllmanBraceStyle);
17361   verifyFormat("int f()\n"
17362                "{ // comment\n"
17363                "  return 42;\n"
17364                "}",
17365                AllmanBraceStyle);
17366 
17367   AllmanBraceStyle.ColumnLimit = 19;
17368   verifyFormat("void f() { int i; }", AllmanBraceStyle);
17369   AllmanBraceStyle.ColumnLimit = 18;
17370   verifyFormat("void f()\n"
17371                "{\n"
17372                "  int i;\n"
17373                "}",
17374                AllmanBraceStyle);
17375   AllmanBraceStyle.ColumnLimit = 80;
17376 
17377   FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle;
17378   BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine =
17379       FormatStyle::SIS_WithoutElse;
17380   BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
17381   verifyFormat("void f(bool b)\n"
17382                "{\n"
17383                "  if (b)\n"
17384                "  {\n"
17385                "    return;\n"
17386                "  }\n"
17387                "}\n",
17388                BreakBeforeBraceShortIfs);
17389   verifyFormat("void f(bool b)\n"
17390                "{\n"
17391                "  if constexpr (b)\n"
17392                "  {\n"
17393                "    return;\n"
17394                "  }\n"
17395                "}\n",
17396                BreakBeforeBraceShortIfs);
17397   verifyFormat("void f(bool b)\n"
17398                "{\n"
17399                "  if CONSTEXPR (b)\n"
17400                "  {\n"
17401                "    return;\n"
17402                "  }\n"
17403                "}\n",
17404                BreakBeforeBraceShortIfs);
17405   verifyFormat("void f(bool b)\n"
17406                "{\n"
17407                "  if (b) return;\n"
17408                "}\n",
17409                BreakBeforeBraceShortIfs);
17410   verifyFormat("void f(bool b)\n"
17411                "{\n"
17412                "  if constexpr (b) return;\n"
17413                "}\n",
17414                BreakBeforeBraceShortIfs);
17415   verifyFormat("void f(bool b)\n"
17416                "{\n"
17417                "  if CONSTEXPR (b) return;\n"
17418                "}\n",
17419                BreakBeforeBraceShortIfs);
17420   verifyFormat("void f(bool b)\n"
17421                "{\n"
17422                "  while (b)\n"
17423                "  {\n"
17424                "    return;\n"
17425                "  }\n"
17426                "}\n",
17427                BreakBeforeBraceShortIfs);
17428 }
17429 
17430 TEST_F(FormatTest, WhitesmithsBraceBreaking) {
17431   FormatStyle WhitesmithsBraceStyle = getLLVMStyleWithColumns(0);
17432   WhitesmithsBraceStyle.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
17433 
17434   // Make a few changes to the style for testing purposes
17435   WhitesmithsBraceStyle.AllowShortFunctionsOnASingleLine =
17436       FormatStyle::SFS_Empty;
17437   WhitesmithsBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
17438 
17439   // FIXME: this test case can't decide whether there should be a blank line
17440   // after the ~D() line or not. It adds one if one doesn't exist in the test
17441   // and it removes the line if one exists.
17442   /*
17443   verifyFormat("class A;\n"
17444                "namespace B\n"
17445                "  {\n"
17446                "class C;\n"
17447                "// Comment\n"
17448                "class D\n"
17449                "  {\n"
17450                "public:\n"
17451                "  D();\n"
17452                "  ~D() {}\n"
17453                "private:\n"
17454                "  enum E\n"
17455                "    {\n"
17456                "    F\n"
17457                "    }\n"
17458                "  };\n"
17459                "  } // namespace B\n",
17460                WhitesmithsBraceStyle);
17461   */
17462 
17463   WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_None;
17464   verifyFormat("namespace a\n"
17465                "  {\n"
17466                "class A\n"
17467                "  {\n"
17468                "  void f()\n"
17469                "    {\n"
17470                "    if (true)\n"
17471                "      {\n"
17472                "      a();\n"
17473                "      b();\n"
17474                "      }\n"
17475                "    }\n"
17476                "  void g()\n"
17477                "    {\n"
17478                "    return;\n"
17479                "    }\n"
17480                "  };\n"
17481                "struct B\n"
17482                "  {\n"
17483                "  int x;\n"
17484                "  };\n"
17485                "  } // namespace a",
17486                WhitesmithsBraceStyle);
17487 
17488   verifyFormat("namespace a\n"
17489                "  {\n"
17490                "namespace b\n"
17491                "  {\n"
17492                "class A\n"
17493                "  {\n"
17494                "  void f()\n"
17495                "    {\n"
17496                "    if (true)\n"
17497                "      {\n"
17498                "      a();\n"
17499                "      b();\n"
17500                "      }\n"
17501                "    }\n"
17502                "  void g()\n"
17503                "    {\n"
17504                "    return;\n"
17505                "    }\n"
17506                "  };\n"
17507                "struct B\n"
17508                "  {\n"
17509                "  int x;\n"
17510                "  };\n"
17511                "  } // namespace b\n"
17512                "  } // namespace a",
17513                WhitesmithsBraceStyle);
17514 
17515   WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_Inner;
17516   verifyFormat("namespace a\n"
17517                "  {\n"
17518                "namespace b\n"
17519                "  {\n"
17520                "  class A\n"
17521                "    {\n"
17522                "    void f()\n"
17523                "      {\n"
17524                "      if (true)\n"
17525                "        {\n"
17526                "        a();\n"
17527                "        b();\n"
17528                "        }\n"
17529                "      }\n"
17530                "    void g()\n"
17531                "      {\n"
17532                "      return;\n"
17533                "      }\n"
17534                "    };\n"
17535                "  struct B\n"
17536                "    {\n"
17537                "    int x;\n"
17538                "    };\n"
17539                "  } // namespace b\n"
17540                "  } // namespace a",
17541                WhitesmithsBraceStyle);
17542 
17543   WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_All;
17544   verifyFormat("namespace a\n"
17545                "  {\n"
17546                "  namespace b\n"
17547                "    {\n"
17548                "    class A\n"
17549                "      {\n"
17550                "      void f()\n"
17551                "        {\n"
17552                "        if (true)\n"
17553                "          {\n"
17554                "          a();\n"
17555                "          b();\n"
17556                "          }\n"
17557                "        }\n"
17558                "      void g()\n"
17559                "        {\n"
17560                "        return;\n"
17561                "        }\n"
17562                "      };\n"
17563                "    struct B\n"
17564                "      {\n"
17565                "      int x;\n"
17566                "      };\n"
17567                "    } // namespace b\n"
17568                "  }   // namespace a",
17569                WhitesmithsBraceStyle);
17570 
17571   verifyFormat("void f()\n"
17572                "  {\n"
17573                "  if (true)\n"
17574                "    {\n"
17575                "    a();\n"
17576                "    }\n"
17577                "  else if (false)\n"
17578                "    {\n"
17579                "    b();\n"
17580                "    }\n"
17581                "  else\n"
17582                "    {\n"
17583                "    c();\n"
17584                "    }\n"
17585                "  }\n",
17586                WhitesmithsBraceStyle);
17587 
17588   verifyFormat("void f()\n"
17589                "  {\n"
17590                "  for (int i = 0; i < 10; ++i)\n"
17591                "    {\n"
17592                "    a();\n"
17593                "    }\n"
17594                "  while (false)\n"
17595                "    {\n"
17596                "    b();\n"
17597                "    }\n"
17598                "  do\n"
17599                "    {\n"
17600                "    c();\n"
17601                "    } while (false)\n"
17602                "  }\n",
17603                WhitesmithsBraceStyle);
17604 
17605   WhitesmithsBraceStyle.IndentCaseLabels = true;
17606   verifyFormat("void switchTest1(int a)\n"
17607                "  {\n"
17608                "  switch (a)\n"
17609                "    {\n"
17610                "    case 2:\n"
17611                "      {\n"
17612                "      }\n"
17613                "      break;\n"
17614                "    }\n"
17615                "  }\n",
17616                WhitesmithsBraceStyle);
17617 
17618   verifyFormat("void switchTest2(int a)\n"
17619                "  {\n"
17620                "  switch (a)\n"
17621                "    {\n"
17622                "    case 0:\n"
17623                "      break;\n"
17624                "    case 1:\n"
17625                "      {\n"
17626                "      break;\n"
17627                "      }\n"
17628                "    case 2:\n"
17629                "      {\n"
17630                "      }\n"
17631                "      break;\n"
17632                "    default:\n"
17633                "      break;\n"
17634                "    }\n"
17635                "  }\n",
17636                WhitesmithsBraceStyle);
17637 
17638   verifyFormat("void switchTest3(int a)\n"
17639                "  {\n"
17640                "  switch (a)\n"
17641                "    {\n"
17642                "    case 0:\n"
17643                "      {\n"
17644                "      foo(x);\n"
17645                "      }\n"
17646                "      break;\n"
17647                "    default:\n"
17648                "      {\n"
17649                "      foo(1);\n"
17650                "      }\n"
17651                "      break;\n"
17652                "    }\n"
17653                "  }\n",
17654                WhitesmithsBraceStyle);
17655 
17656   WhitesmithsBraceStyle.IndentCaseLabels = false;
17657 
17658   verifyFormat("void switchTest4(int a)\n"
17659                "  {\n"
17660                "  switch (a)\n"
17661                "    {\n"
17662                "  case 2:\n"
17663                "    {\n"
17664                "    }\n"
17665                "    break;\n"
17666                "    }\n"
17667                "  }\n",
17668                WhitesmithsBraceStyle);
17669 
17670   verifyFormat("void switchTest5(int a)\n"
17671                "  {\n"
17672                "  switch (a)\n"
17673                "    {\n"
17674                "  case 0:\n"
17675                "    break;\n"
17676                "  case 1:\n"
17677                "    {\n"
17678                "    foo();\n"
17679                "    break;\n"
17680                "    }\n"
17681                "  case 2:\n"
17682                "    {\n"
17683                "    }\n"
17684                "    break;\n"
17685                "  default:\n"
17686                "    break;\n"
17687                "    }\n"
17688                "  }\n",
17689                WhitesmithsBraceStyle);
17690 
17691   verifyFormat("void switchTest6(int a)\n"
17692                "  {\n"
17693                "  switch (a)\n"
17694                "    {\n"
17695                "  case 0:\n"
17696                "    {\n"
17697                "    foo(x);\n"
17698                "    }\n"
17699                "    break;\n"
17700                "  default:\n"
17701                "    {\n"
17702                "    foo(1);\n"
17703                "    }\n"
17704                "    break;\n"
17705                "    }\n"
17706                "  }\n",
17707                WhitesmithsBraceStyle);
17708 
17709   verifyFormat("enum X\n"
17710                "  {\n"
17711                "  Y = 0, // testing\n"
17712                "  }\n",
17713                WhitesmithsBraceStyle);
17714 
17715   verifyFormat("enum X\n"
17716                "  {\n"
17717                "  Y = 0\n"
17718                "  }\n",
17719                WhitesmithsBraceStyle);
17720   verifyFormat("enum X\n"
17721                "  {\n"
17722                "  Y = 0,\n"
17723                "  Z = 1\n"
17724                "  };\n",
17725                WhitesmithsBraceStyle);
17726 
17727   verifyFormat("@interface BSApplicationController ()\n"
17728                "  {\n"
17729                "@private\n"
17730                "  id _extraIvar;\n"
17731                "  }\n"
17732                "@end\n",
17733                WhitesmithsBraceStyle);
17734 
17735   verifyFormat("#ifdef _DEBUG\n"
17736                "int foo(int i = 0)\n"
17737                "#else\n"
17738                "int foo(int i = 5)\n"
17739                "#endif\n"
17740                "  {\n"
17741                "  return i;\n"
17742                "  }",
17743                WhitesmithsBraceStyle);
17744 
17745   verifyFormat("void foo() {}\n"
17746                "void bar()\n"
17747                "#ifdef _DEBUG\n"
17748                "  {\n"
17749                "  foo();\n"
17750                "  }\n"
17751                "#else\n"
17752                "  {\n"
17753                "  }\n"
17754                "#endif",
17755                WhitesmithsBraceStyle);
17756 
17757   verifyFormat("void foobar()\n"
17758                "  {\n"
17759                "  int i = 5;\n"
17760                "  }\n"
17761                "#ifdef _DEBUG\n"
17762                "void bar()\n"
17763                "  {\n"
17764                "  }\n"
17765                "#else\n"
17766                "void bar()\n"
17767                "  {\n"
17768                "  foobar();\n"
17769                "  }\n"
17770                "#endif",
17771                WhitesmithsBraceStyle);
17772 
17773   // This shouldn't affect ObjC blocks..
17774   verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
17775                "  // ...\n"
17776                "  int i;\n"
17777                "}];",
17778                WhitesmithsBraceStyle);
17779   verifyFormat("void (^block)(void) = ^{\n"
17780                "  // ...\n"
17781                "  int i;\n"
17782                "};",
17783                WhitesmithsBraceStyle);
17784   // .. or dict literals.
17785   verifyFormat("void f()\n"
17786                "  {\n"
17787                "  [object someMethod:@{@\"a\" : @\"b\"}];\n"
17788                "  }",
17789                WhitesmithsBraceStyle);
17790 
17791   verifyFormat("int f()\n"
17792                "  { // comment\n"
17793                "  return 42;\n"
17794                "  }",
17795                WhitesmithsBraceStyle);
17796 
17797   FormatStyle BreakBeforeBraceShortIfs = WhitesmithsBraceStyle;
17798   BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine =
17799       FormatStyle::SIS_OnlyFirstIf;
17800   BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
17801   verifyFormat("void f(bool b)\n"
17802                "  {\n"
17803                "  if (b)\n"
17804                "    {\n"
17805                "    return;\n"
17806                "    }\n"
17807                "  }\n",
17808                BreakBeforeBraceShortIfs);
17809   verifyFormat("void f(bool b)\n"
17810                "  {\n"
17811                "  if (b) return;\n"
17812                "  }\n",
17813                BreakBeforeBraceShortIfs);
17814   verifyFormat("void f(bool b)\n"
17815                "  {\n"
17816                "  while (b)\n"
17817                "    {\n"
17818                "    return;\n"
17819                "    }\n"
17820                "  }\n",
17821                BreakBeforeBraceShortIfs);
17822 }
17823 
17824 TEST_F(FormatTest, GNUBraceBreaking) {
17825   FormatStyle GNUBraceStyle = getLLVMStyle();
17826   GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
17827   verifyFormat("namespace a\n"
17828                "{\n"
17829                "class A\n"
17830                "{\n"
17831                "  void f()\n"
17832                "  {\n"
17833                "    int a;\n"
17834                "    {\n"
17835                "      int b;\n"
17836                "    }\n"
17837                "    if (true)\n"
17838                "      {\n"
17839                "        a();\n"
17840                "        b();\n"
17841                "      }\n"
17842                "  }\n"
17843                "  void g() { return; }\n"
17844                "}\n"
17845                "} // namespace a",
17846                GNUBraceStyle);
17847 
17848   verifyFormat("void f()\n"
17849                "{\n"
17850                "  if (true)\n"
17851                "    {\n"
17852                "      a();\n"
17853                "    }\n"
17854                "  else if (false)\n"
17855                "    {\n"
17856                "      b();\n"
17857                "    }\n"
17858                "  else\n"
17859                "    {\n"
17860                "      c();\n"
17861                "    }\n"
17862                "}\n",
17863                GNUBraceStyle);
17864 
17865   verifyFormat("void f()\n"
17866                "{\n"
17867                "  for (int i = 0; i < 10; ++i)\n"
17868                "    {\n"
17869                "      a();\n"
17870                "    }\n"
17871                "  while (false)\n"
17872                "    {\n"
17873                "      b();\n"
17874                "    }\n"
17875                "  do\n"
17876                "    {\n"
17877                "      c();\n"
17878                "    }\n"
17879                "  while (false);\n"
17880                "}\n",
17881                GNUBraceStyle);
17882 
17883   verifyFormat("void f(int a)\n"
17884                "{\n"
17885                "  switch (a)\n"
17886                "    {\n"
17887                "    case 0:\n"
17888                "      break;\n"
17889                "    case 1:\n"
17890                "      {\n"
17891                "        break;\n"
17892                "      }\n"
17893                "    case 2:\n"
17894                "      {\n"
17895                "      }\n"
17896                "      break;\n"
17897                "    default:\n"
17898                "      break;\n"
17899                "    }\n"
17900                "}\n",
17901                GNUBraceStyle);
17902 
17903   verifyFormat("enum X\n"
17904                "{\n"
17905                "  Y = 0,\n"
17906                "}\n",
17907                GNUBraceStyle);
17908 
17909   verifyFormat("@interface BSApplicationController ()\n"
17910                "{\n"
17911                "@private\n"
17912                "  id _extraIvar;\n"
17913                "}\n"
17914                "@end\n",
17915                GNUBraceStyle);
17916 
17917   verifyFormat("#ifdef _DEBUG\n"
17918                "int foo(int i = 0)\n"
17919                "#else\n"
17920                "int foo(int i = 5)\n"
17921                "#endif\n"
17922                "{\n"
17923                "  return i;\n"
17924                "}",
17925                GNUBraceStyle);
17926 
17927   verifyFormat("void foo() {}\n"
17928                "void bar()\n"
17929                "#ifdef _DEBUG\n"
17930                "{\n"
17931                "  foo();\n"
17932                "}\n"
17933                "#else\n"
17934                "{\n"
17935                "}\n"
17936                "#endif",
17937                GNUBraceStyle);
17938 
17939   verifyFormat("void foobar() { int i = 5; }\n"
17940                "#ifdef _DEBUG\n"
17941                "void bar() {}\n"
17942                "#else\n"
17943                "void bar() { foobar(); }\n"
17944                "#endif",
17945                GNUBraceStyle);
17946 }
17947 
17948 TEST_F(FormatTest, WebKitBraceBreaking) {
17949   FormatStyle WebKitBraceStyle = getLLVMStyle();
17950   WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit;
17951   WebKitBraceStyle.FixNamespaceComments = false;
17952   verifyFormat("namespace a {\n"
17953                "class A {\n"
17954                "  void f()\n"
17955                "  {\n"
17956                "    if (true) {\n"
17957                "      a();\n"
17958                "      b();\n"
17959                "    }\n"
17960                "  }\n"
17961                "  void g() { return; }\n"
17962                "};\n"
17963                "enum E {\n"
17964                "  A,\n"
17965                "  // foo\n"
17966                "  B,\n"
17967                "  C\n"
17968                "};\n"
17969                "struct B {\n"
17970                "  int x;\n"
17971                "};\n"
17972                "}\n",
17973                WebKitBraceStyle);
17974   verifyFormat("struct S {\n"
17975                "  int Type;\n"
17976                "  union {\n"
17977                "    int x;\n"
17978                "    double y;\n"
17979                "  } Value;\n"
17980                "  class C {\n"
17981                "    MyFavoriteType Value;\n"
17982                "  } Class;\n"
17983                "};\n",
17984                WebKitBraceStyle);
17985 }
17986 
17987 TEST_F(FormatTest, CatchExceptionReferenceBinding) {
17988   verifyFormat("void f() {\n"
17989                "  try {\n"
17990                "  } catch (const Exception &e) {\n"
17991                "  }\n"
17992                "}\n",
17993                getLLVMStyle());
17994 }
17995 
17996 TEST_F(FormatTest, CatchAlignArrayOfStructuresRightAlignment) {
17997   auto Style = getLLVMStyle();
17998   Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
17999   Style.AlignConsecutiveAssignments =
18000       FormatStyle::AlignConsecutiveStyle::ACS_Consecutive;
18001   Style.AlignConsecutiveDeclarations =
18002       FormatStyle::AlignConsecutiveStyle::ACS_Consecutive;
18003   verifyFormat("struct test demo[] = {\n"
18004                "    {56,    23, \"hello\"},\n"
18005                "    {-1, 93463, \"world\"},\n"
18006                "    { 7,     5,    \"!!\"}\n"
18007                "};\n",
18008                Style);
18009 
18010   verifyFormat("struct test demo[] = {\n"
18011                "    {56,    23, \"hello\"}, // first line\n"
18012                "    {-1, 93463, \"world\"}, // second line\n"
18013                "    { 7,     5,    \"!!\"}  // third line\n"
18014                "};\n",
18015                Style);
18016 
18017   verifyFormat("struct test demo[4] = {\n"
18018                "    { 56,    23, 21,       \"oh\"}, // first line\n"
18019                "    { -1, 93463, 22,       \"my\"}, // second line\n"
18020                "    {  7,     5,  1, \"goodness\"}  // third line\n"
18021                "    {234,     5,  1, \"gracious\"}  // fourth line\n"
18022                "};\n",
18023                Style);
18024 
18025   verifyFormat("struct test demo[3] = {\n"
18026                "    {56,    23, \"hello\"},\n"
18027                "    {-1, 93463, \"world\"},\n"
18028                "    { 7,     5,    \"!!\"}\n"
18029                "};\n",
18030                Style);
18031 
18032   verifyFormat("struct test demo[3] = {\n"
18033                "    {int{56},    23, \"hello\"},\n"
18034                "    {int{-1}, 93463, \"world\"},\n"
18035                "    { int{7},     5,    \"!!\"}\n"
18036                "};\n",
18037                Style);
18038 
18039   verifyFormat("struct test demo[] = {\n"
18040                "    {56,    23, \"hello\"},\n"
18041                "    {-1, 93463, \"world\"},\n"
18042                "    { 7,     5,    \"!!\"},\n"
18043                "};\n",
18044                Style);
18045 
18046   verifyFormat("test demo[] = {\n"
18047                "    {56,    23, \"hello\"},\n"
18048                "    {-1, 93463, \"world\"},\n"
18049                "    { 7,     5,    \"!!\"},\n"
18050                "};\n",
18051                Style);
18052 
18053   verifyFormat("demo = std::array<struct test, 3>{\n"
18054                "    test{56,    23, \"hello\"},\n"
18055                "    test{-1, 93463, \"world\"},\n"
18056                "    test{ 7,     5,    \"!!\"},\n"
18057                "};\n",
18058                Style);
18059 
18060   verifyFormat("test demo[] = {\n"
18061                "    {56,    23, \"hello\"},\n"
18062                "#if X\n"
18063                "    {-1, 93463, \"world\"},\n"
18064                "#endif\n"
18065                "    { 7,     5,    \"!!\"}\n"
18066                "};\n",
18067                Style);
18068 
18069   verifyFormat(
18070       "test demo[] = {\n"
18071       "    { 7,    23,\n"
18072       "     \"hello world i am a very long line that really, in any\"\n"
18073       "     \"just world, ought to be split over multiple lines\"},\n"
18074       "    {-1, 93463,                                  \"world\"},\n"
18075       "    {56,     5,                                     \"!!\"}\n"
18076       "};\n",
18077       Style);
18078 
18079   verifyFormat("return GradForUnaryCwise(g, {\n"
18080                "                                {{\"sign\"}, \"Sign\",  "
18081                "  {\"x\", \"dy\"}},\n"
18082                "                                {  {\"dx\"},  \"Mul\", {\"dy\""
18083                ", \"sign\"}},\n"
18084                "});\n",
18085                Style);
18086 
18087   Style.ColumnLimit = 0;
18088   EXPECT_EQ(
18089       "test demo[] = {\n"
18090       "    {56,    23, \"hello world i am a very long line that really, "
18091       "in any just world, ought to be split over multiple lines\"},\n"
18092       "    {-1, 93463,                                                  "
18093       "                                                 \"world\"},\n"
18094       "    { 7,     5,                                                  "
18095       "                                                    \"!!\"},\n"
18096       "};",
18097       format("test demo[] = {{56, 23, \"hello world i am a very long line "
18098              "that really, in any just world, ought to be split over multiple "
18099              "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
18100              Style));
18101 
18102   Style.ColumnLimit = 80;
18103   verifyFormat("test demo[] = {\n"
18104                "    {56,    23, /* a comment */ \"hello\"},\n"
18105                "    {-1, 93463,                 \"world\"},\n"
18106                "    { 7,     5,                    \"!!\"}\n"
18107                "};\n",
18108                Style);
18109 
18110   verifyFormat("test demo[] = {\n"
18111                "    {56,    23,                    \"hello\"},\n"
18112                "    {-1, 93463, \"world\" /* comment here */},\n"
18113                "    { 7,     5,                       \"!!\"}\n"
18114                "};\n",
18115                Style);
18116 
18117   verifyFormat("test demo[] = {\n"
18118                "    {56, /* a comment */ 23, \"hello\"},\n"
18119                "    {-1,              93463, \"world\"},\n"
18120                "    { 7,                  5,    \"!!\"}\n"
18121                "};\n",
18122                Style);
18123 
18124   Style.ColumnLimit = 20;
18125   EXPECT_EQ(
18126       "demo = std::array<\n"
18127       "    struct test, 3>{\n"
18128       "    test{\n"
18129       "         56,    23,\n"
18130       "         \"hello \"\n"
18131       "         \"world i \"\n"
18132       "         \"am a very \"\n"
18133       "         \"long line \"\n"
18134       "         \"that \"\n"
18135       "         \"really, \"\n"
18136       "         \"in any \"\n"
18137       "         \"just \"\n"
18138       "         \"world, \"\n"
18139       "         \"ought to \"\n"
18140       "         \"be split \"\n"
18141       "         \"over \"\n"
18142       "         \"multiple \"\n"
18143       "         \"lines\"},\n"
18144       "    test{-1, 93463,\n"
18145       "         \"world\"},\n"
18146       "    test{ 7,     5,\n"
18147       "         \"!!\"   },\n"
18148       "};",
18149       format("demo = std::array<struct test, 3>{test{56, 23, \"hello world "
18150              "i am a very long line that really, in any just world, ought "
18151              "to be split over multiple lines\"},test{-1, 93463, \"world\"},"
18152              "test{7, 5, \"!!\"},};",
18153              Style));
18154   // This caused a core dump by enabling Alignment in the LLVMStyle globally
18155   Style = getLLVMStyleWithColumns(50);
18156   Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
18157   verifyFormat("static A x = {\n"
18158                "    {{init1, init2, init3, init4},\n"
18159                "     {init1, init2, init3, init4}}\n"
18160                "};",
18161                Style);
18162   Style.ColumnLimit = 100;
18163   EXPECT_EQ(
18164       "test demo[] = {\n"
18165       "    {56,    23,\n"
18166       "     \"hello world i am a very long line that really, in any just world"
18167       ", ought to be split over \"\n"
18168       "     \"multiple lines\"  },\n"
18169       "    {-1, 93463, \"world\"},\n"
18170       "    { 7,     5,    \"!!\"},\n"
18171       "};",
18172       format("test demo[] = {{56, 23, \"hello world i am a very long line "
18173              "that really, in any just world, ought to be split over multiple "
18174              "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
18175              Style));
18176 
18177   Style = getLLVMStyleWithColumns(50);
18178   Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
18179   Style.AlignConsecutiveAssignments =
18180       FormatStyle::AlignConsecutiveStyle::ACS_Consecutive;
18181   Style.AlignConsecutiveDeclarations =
18182       FormatStyle::AlignConsecutiveStyle::ACS_Consecutive;
18183   verifyFormat("struct test demo[] = {\n"
18184                "    {56,    23, \"hello\"},\n"
18185                "    {-1, 93463, \"world\"},\n"
18186                "    { 7,     5,    \"!!\"}\n"
18187                "};\n"
18188                "static A x = {\n"
18189                "    {{init1, init2, init3, init4},\n"
18190                "     {init1, init2, init3, init4}}\n"
18191                "};",
18192                Style);
18193   Style.ColumnLimit = 100;
18194   Style.AlignConsecutiveAssignments =
18195       FormatStyle::AlignConsecutiveStyle::ACS_AcrossComments;
18196   Style.AlignConsecutiveDeclarations =
18197       FormatStyle::AlignConsecutiveStyle::ACS_AcrossComments;
18198   verifyFormat("struct test demo[] = {\n"
18199                "    {56,    23, \"hello\"},\n"
18200                "    {-1, 93463, \"world\"},\n"
18201                "    { 7,     5,    \"!!\"}\n"
18202                "};\n"
18203                "struct test demo[4] = {\n"
18204                "    { 56,    23, 21,       \"oh\"}, // first line\n"
18205                "    { -1, 93463, 22,       \"my\"}, // second line\n"
18206                "    {  7,     5,  1, \"goodness\"}  // third line\n"
18207                "    {234,     5,  1, \"gracious\"}  // fourth line\n"
18208                "};\n",
18209                Style);
18210   EXPECT_EQ(
18211       "test demo[] = {\n"
18212       "    {56,\n"
18213       "     \"hello world i am a very long line that really, in any just world"
18214       ", ought to be split over \"\n"
18215       "     \"multiple lines\",    23},\n"
18216       "    {-1,      \"world\", 93463},\n"
18217       "    { 7,         \"!!\",     5},\n"
18218       "};",
18219       format("test demo[] = {{56, \"hello world i am a very long line "
18220              "that really, in any just world, ought to be split over multiple "
18221              "lines\", 23},{-1, \"world\", 93463},{7, \"!!\", 5},};",
18222              Style));
18223 }
18224 
18225 TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) {
18226   auto Style = getLLVMStyle();
18227   Style.AlignArrayOfStructures = FormatStyle::AIAS_Left;
18228   /* FIXME: This case gets misformatted.
18229   verifyFormat("auto foo = Items{\n"
18230                "    Section{0, bar(), },\n"
18231                "    Section{1, boo()  }\n"
18232                "};\n",
18233                Style);
18234   */
18235   verifyFormat("auto foo = Items{\n"
18236                "    Section{\n"
18237                "            0, bar(),\n"
18238                "            }\n"
18239                "};\n",
18240                Style);
18241   verifyFormat("struct test demo[] = {\n"
18242                "    {56, 23,    \"hello\"},\n"
18243                "    {-1, 93463, \"world\"},\n"
18244                "    {7,  5,     \"!!\"   }\n"
18245                "};\n",
18246                Style);
18247   verifyFormat("struct test demo[] = {\n"
18248                "    {56, 23,    \"hello\"}, // first line\n"
18249                "    {-1, 93463, \"world\"}, // second line\n"
18250                "    {7,  5,     \"!!\"   }  // third line\n"
18251                "};\n",
18252                Style);
18253   verifyFormat("struct test demo[4] = {\n"
18254                "    {56,  23,    21, \"oh\"      }, // first line\n"
18255                "    {-1,  93463, 22, \"my\"      }, // second line\n"
18256                "    {7,   5,     1,  \"goodness\"}  // third line\n"
18257                "    {234, 5,     1,  \"gracious\"}  // fourth line\n"
18258                "};\n",
18259                Style);
18260   verifyFormat("struct test demo[3] = {\n"
18261                "    {56, 23,    \"hello\"},\n"
18262                "    {-1, 93463, \"world\"},\n"
18263                "    {7,  5,     \"!!\"   }\n"
18264                "};\n",
18265                Style);
18266 
18267   verifyFormat("struct test demo[3] = {\n"
18268                "    {int{56}, 23,    \"hello\"},\n"
18269                "    {int{-1}, 93463, \"world\"},\n"
18270                "    {int{7},  5,     \"!!\"   }\n"
18271                "};\n",
18272                Style);
18273   verifyFormat("struct test demo[] = {\n"
18274                "    {56, 23,    \"hello\"},\n"
18275                "    {-1, 93463, \"world\"},\n"
18276                "    {7,  5,     \"!!\"   },\n"
18277                "};\n",
18278                Style);
18279   verifyFormat("test demo[] = {\n"
18280                "    {56, 23,    \"hello\"},\n"
18281                "    {-1, 93463, \"world\"},\n"
18282                "    {7,  5,     \"!!\"   },\n"
18283                "};\n",
18284                Style);
18285   verifyFormat("demo = std::array<struct test, 3>{\n"
18286                "    test{56, 23,    \"hello\"},\n"
18287                "    test{-1, 93463, \"world\"},\n"
18288                "    test{7,  5,     \"!!\"   },\n"
18289                "};\n",
18290                Style);
18291   verifyFormat("test demo[] = {\n"
18292                "    {56, 23,    \"hello\"},\n"
18293                "#if X\n"
18294                "    {-1, 93463, \"world\"},\n"
18295                "#endif\n"
18296                "    {7,  5,     \"!!\"   }\n"
18297                "};\n",
18298                Style);
18299   verifyFormat(
18300       "test demo[] = {\n"
18301       "    {7,  23,\n"
18302       "     \"hello world i am a very long line that really, in any\"\n"
18303       "     \"just world, ought to be split over multiple lines\"},\n"
18304       "    {-1, 93463, \"world\"                                 },\n"
18305       "    {56, 5,     \"!!\"                                    }\n"
18306       "};\n",
18307       Style);
18308 
18309   verifyFormat("return GradForUnaryCwise(g, {\n"
18310                "                                {{\"sign\"}, \"Sign\", {\"x\", "
18311                "\"dy\"}   },\n"
18312                "                                {{\"dx\"},   \"Mul\",  "
18313                "{\"dy\", \"sign\"}},\n"
18314                "});\n",
18315                Style);
18316 
18317   Style.ColumnLimit = 0;
18318   EXPECT_EQ(
18319       "test demo[] = {\n"
18320       "    {56, 23,    \"hello world i am a very long line that really, in any "
18321       "just world, ought to be split over multiple lines\"},\n"
18322       "    {-1, 93463, \"world\"                                               "
18323       "                                                   },\n"
18324       "    {7,  5,     \"!!\"                                                  "
18325       "                                                   },\n"
18326       "};",
18327       format("test demo[] = {{56, 23, \"hello world i am a very long line "
18328              "that really, in any just world, ought to be split over multiple "
18329              "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
18330              Style));
18331 
18332   Style.ColumnLimit = 80;
18333   verifyFormat("test demo[] = {\n"
18334                "    {56, 23,    /* a comment */ \"hello\"},\n"
18335                "    {-1, 93463, \"world\"                },\n"
18336                "    {7,  5,     \"!!\"                   }\n"
18337                "};\n",
18338                Style);
18339 
18340   verifyFormat("test demo[] = {\n"
18341                "    {56, 23,    \"hello\"                   },\n"
18342                "    {-1, 93463, \"world\" /* comment here */},\n"
18343                "    {7,  5,     \"!!\"                      }\n"
18344                "};\n",
18345                Style);
18346 
18347   verifyFormat("test demo[] = {\n"
18348                "    {56, /* a comment */ 23, \"hello\"},\n"
18349                "    {-1, 93463,              \"world\"},\n"
18350                "    {7,  5,                  \"!!\"   }\n"
18351                "};\n",
18352                Style);
18353 
18354   Style.ColumnLimit = 20;
18355   EXPECT_EQ(
18356       "demo = std::array<\n"
18357       "    struct test, 3>{\n"
18358       "    test{\n"
18359       "         56, 23,\n"
18360       "         \"hello \"\n"
18361       "         \"world i \"\n"
18362       "         \"am a very \"\n"
18363       "         \"long line \"\n"
18364       "         \"that \"\n"
18365       "         \"really, \"\n"
18366       "         \"in any \"\n"
18367       "         \"just \"\n"
18368       "         \"world, \"\n"
18369       "         \"ought to \"\n"
18370       "         \"be split \"\n"
18371       "         \"over \"\n"
18372       "         \"multiple \"\n"
18373       "         \"lines\"},\n"
18374       "    test{-1, 93463,\n"
18375       "         \"world\"},\n"
18376       "    test{7,  5,\n"
18377       "         \"!!\"   },\n"
18378       "};",
18379       format("demo = std::array<struct test, 3>{test{56, 23, \"hello world "
18380              "i am a very long line that really, in any just world, ought "
18381              "to be split over multiple lines\"},test{-1, 93463, \"world\"},"
18382              "test{7, 5, \"!!\"},};",
18383              Style));
18384 
18385   // This caused a core dump by enabling Alignment in the LLVMStyle globally
18386   Style = getLLVMStyleWithColumns(50);
18387   Style.AlignArrayOfStructures = FormatStyle::AIAS_Left;
18388   verifyFormat("static A x = {\n"
18389                "    {{init1, init2, init3, init4},\n"
18390                "     {init1, init2, init3, init4}}\n"
18391                "};",
18392                Style);
18393   Style.ColumnLimit = 100;
18394   EXPECT_EQ(
18395       "test demo[] = {\n"
18396       "    {56, 23,\n"
18397       "     \"hello world i am a very long line that really, in any just world"
18398       ", ought to be split over \"\n"
18399       "     \"multiple lines\"  },\n"
18400       "    {-1, 93463, \"world\"},\n"
18401       "    {7,  5,     \"!!\"   },\n"
18402       "};",
18403       format("test demo[] = {{56, 23, \"hello world i am a very long line "
18404              "that really, in any just world, ought to be split over multiple "
18405              "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
18406              Style));
18407 }
18408 
18409 TEST_F(FormatTest, UnderstandsPragmas) {
18410   verifyFormat("#pragma omp reduction(| : var)");
18411   verifyFormat("#pragma omp reduction(+ : var)");
18412 
18413   EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string "
18414             "(including parentheses).",
18415             format("#pragma    mark   Any non-hyphenated or hyphenated string "
18416                    "(including parentheses)."));
18417 }
18418 
18419 TEST_F(FormatTest, UnderstandPragmaOption) {
18420   verifyFormat("#pragma option -C -A");
18421 
18422   EXPECT_EQ("#pragma option -C -A", format("#pragma    option   -C   -A"));
18423 }
18424 
18425 TEST_F(FormatTest, OptimizeBreakPenaltyVsExcess) {
18426   FormatStyle Style = getLLVMStyleWithColumns(20);
18427 
18428   // See PR41213
18429   EXPECT_EQ("/*\n"
18430             " *\t9012345\n"
18431             " * /8901\n"
18432             " */",
18433             format("/*\n"
18434                    " *\t9012345 /8901\n"
18435                    " */",
18436                    Style));
18437   EXPECT_EQ("/*\n"
18438             " *345678\n"
18439             " *\t/8901\n"
18440             " */",
18441             format("/*\n"
18442                    " *345678\t/8901\n"
18443                    " */",
18444                    Style));
18445 
18446   verifyFormat("int a; // the\n"
18447                "       // comment",
18448                Style);
18449   EXPECT_EQ("int a; /* first line\n"
18450             "        * second\n"
18451             "        * line third\n"
18452             "        * line\n"
18453             "        */",
18454             format("int a; /* first line\n"
18455                    "        * second\n"
18456                    "        * line third\n"
18457                    "        * line\n"
18458                    "        */",
18459                    Style));
18460   EXPECT_EQ("int a; // first line\n"
18461             "       // second\n"
18462             "       // line third\n"
18463             "       // line",
18464             format("int a; // first line\n"
18465                    "       // second line\n"
18466                    "       // third line",
18467                    Style));
18468 
18469   Style.PenaltyExcessCharacter = 90;
18470   verifyFormat("int a; // the comment", Style);
18471   EXPECT_EQ("int a; // the comment\n"
18472             "       // aaa",
18473             format("int a; // the comment aaa", Style));
18474   EXPECT_EQ("int a; /* first line\n"
18475             "        * second line\n"
18476             "        * third line\n"
18477             "        */",
18478             format("int a; /* first line\n"
18479                    "        * second line\n"
18480                    "        * third line\n"
18481                    "        */",
18482                    Style));
18483   EXPECT_EQ("int a; // first line\n"
18484             "       // second line\n"
18485             "       // third line",
18486             format("int a; // first line\n"
18487                    "       // second line\n"
18488                    "       // third line",
18489                    Style));
18490   // FIXME: Investigate why this is not getting the same layout as the test
18491   // above.
18492   EXPECT_EQ("int a; /* first line\n"
18493             "        * second line\n"
18494             "        * third line\n"
18495             "        */",
18496             format("int a; /* first line second line third line"
18497                    "\n*/",
18498                    Style));
18499 
18500   EXPECT_EQ("// foo bar baz bazfoo\n"
18501             "// foo bar foo bar\n",
18502             format("// foo bar baz bazfoo\n"
18503                    "// foo bar foo           bar\n",
18504                    Style));
18505   EXPECT_EQ("// foo bar baz bazfoo\n"
18506             "// foo bar foo bar\n",
18507             format("// foo bar baz      bazfoo\n"
18508                    "// foo            bar foo bar\n",
18509                    Style));
18510 
18511   // FIXME: Optimally, we'd keep bazfoo on the first line and reflow bar to the
18512   // next one.
18513   EXPECT_EQ("// foo bar baz bazfoo\n"
18514             "// bar foo bar\n",
18515             format("// foo bar baz      bazfoo bar\n"
18516                    "// foo            bar\n",
18517                    Style));
18518 
18519   EXPECT_EQ("// foo bar baz bazfoo\n"
18520             "// foo bar baz bazfoo\n"
18521             "// bar foo bar\n",
18522             format("// foo bar baz      bazfoo\n"
18523                    "// foo bar baz      bazfoo bar\n"
18524                    "// foo bar\n",
18525                    Style));
18526 
18527   EXPECT_EQ("// foo bar baz bazfoo\n"
18528             "// foo bar baz bazfoo\n"
18529             "// bar foo bar\n",
18530             format("// foo bar baz      bazfoo\n"
18531                    "// foo bar baz      bazfoo bar\n"
18532                    "// foo           bar\n",
18533                    Style));
18534 
18535   // Make sure we do not keep protruding characters if strict mode reflow is
18536   // cheaper than keeping protruding characters.
18537   Style.ColumnLimit = 21;
18538   EXPECT_EQ(
18539       "// foo foo foo foo\n"
18540       "// foo foo foo foo\n"
18541       "// foo foo foo foo\n",
18542       format("// foo foo foo foo foo foo foo foo foo foo foo foo\n", Style));
18543 
18544   EXPECT_EQ("int a = /* long block\n"
18545             "           comment */\n"
18546             "    42;",
18547             format("int a = /* long block comment */ 42;", Style));
18548 }
18549 
18550 TEST_F(FormatTest, BreakPenaltyAfterLParen) {
18551   FormatStyle Style = getLLVMStyle();
18552   Style.ColumnLimit = 8;
18553   Style.PenaltyExcessCharacter = 15;
18554   verifyFormat("int foo(\n"
18555                "    int aaaaaaaaaaaaaaaaaaaaaaaa);",
18556                Style);
18557   Style.PenaltyBreakOpenParenthesis = 200;
18558   EXPECT_EQ("int foo(int aaaaaaaaaaaaaaaaaaaaaaaa);",
18559             format("int foo(\n"
18560                    "    int aaaaaaaaaaaaaaaaaaaaaaaa);",
18561                    Style));
18562 }
18563 
18564 TEST_F(FormatTest, BreakPenaltyAfterCastLParen) {
18565   FormatStyle Style = getLLVMStyle();
18566   Style.ColumnLimit = 5;
18567   Style.PenaltyExcessCharacter = 150;
18568   verifyFormat("foo((\n"
18569                "    int)aaaaaaaaaaaaaaaaaaaaaaaa);",
18570 
18571                Style);
18572   Style.PenaltyBreakOpenParenthesis = 100000;
18573   EXPECT_EQ("foo((int)\n"
18574             "        aaaaaaaaaaaaaaaaaaaaaaaa);",
18575             format("foo((\n"
18576                    "int)aaaaaaaaaaaaaaaaaaaaaaaa);",
18577                    Style));
18578 }
18579 
18580 TEST_F(FormatTest, BreakPenaltyAfterForLoopLParen) {
18581   FormatStyle Style = getLLVMStyle();
18582   Style.ColumnLimit = 4;
18583   Style.PenaltyExcessCharacter = 100;
18584   verifyFormat("for (\n"
18585                "    int iiiiiiiiiiiiiiiii =\n"
18586                "        0;\n"
18587                "    iiiiiiiiiiiiiiiii <\n"
18588                "    2;\n"
18589                "    iiiiiiiiiiiiiiiii++) {\n"
18590                "}",
18591 
18592                Style);
18593   Style.PenaltyBreakOpenParenthesis = 1250;
18594   EXPECT_EQ("for (int iiiiiiiiiiiiiiiii =\n"
18595             "         0;\n"
18596             "     iiiiiiiiiiiiiiiii <\n"
18597             "     2;\n"
18598             "     iiiiiiiiiiiiiiiii++) {\n"
18599             "}",
18600             format("for (\n"
18601                    "    int iiiiiiiiiiiiiiiii =\n"
18602                    "        0;\n"
18603                    "    iiiiiiiiiiiiiiiii <\n"
18604                    "    2;\n"
18605                    "    iiiiiiiiiiiiiiiii++) {\n"
18606                    "}",
18607                    Style));
18608 }
18609 
18610 #define EXPECT_ALL_STYLES_EQUAL(Styles)                                        \
18611   for (size_t i = 1; i < Styles.size(); ++i)                                   \
18612   EXPECT_EQ(Styles[0], Styles[i])                                              \
18613       << "Style #" << i << " of " << Styles.size() << " differs from Style #0"
18614 
18615 TEST_F(FormatTest, GetsPredefinedStyleByName) {
18616   SmallVector<FormatStyle, 3> Styles;
18617   Styles.resize(3);
18618 
18619   Styles[0] = getLLVMStyle();
18620   EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1]));
18621   EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2]));
18622   EXPECT_ALL_STYLES_EQUAL(Styles);
18623 
18624   Styles[0] = getGoogleStyle();
18625   EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1]));
18626   EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2]));
18627   EXPECT_ALL_STYLES_EQUAL(Styles);
18628 
18629   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
18630   EXPECT_TRUE(
18631       getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1]));
18632   EXPECT_TRUE(
18633       getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2]));
18634   EXPECT_ALL_STYLES_EQUAL(Styles);
18635 
18636   Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp);
18637   EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1]));
18638   EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2]));
18639   EXPECT_ALL_STYLES_EQUAL(Styles);
18640 
18641   Styles[0] = getMozillaStyle();
18642   EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1]));
18643   EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2]));
18644   EXPECT_ALL_STYLES_EQUAL(Styles);
18645 
18646   Styles[0] = getWebKitStyle();
18647   EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1]));
18648   EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2]));
18649   EXPECT_ALL_STYLES_EQUAL(Styles);
18650 
18651   Styles[0] = getGNUStyle();
18652   EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1]));
18653   EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2]));
18654   EXPECT_ALL_STYLES_EQUAL(Styles);
18655 
18656   EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0]));
18657 }
18658 
18659 TEST_F(FormatTest, GetsCorrectBasedOnStyle) {
18660   SmallVector<FormatStyle, 8> Styles;
18661   Styles.resize(2);
18662 
18663   Styles[0] = getGoogleStyle();
18664   Styles[1] = getLLVMStyle();
18665   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
18666   EXPECT_ALL_STYLES_EQUAL(Styles);
18667 
18668   Styles.resize(5);
18669   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
18670   Styles[1] = getLLVMStyle();
18671   Styles[1].Language = FormatStyle::LK_JavaScript;
18672   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
18673 
18674   Styles[2] = getLLVMStyle();
18675   Styles[2].Language = FormatStyle::LK_JavaScript;
18676   EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n"
18677                                   "BasedOnStyle: Google",
18678                                   &Styles[2])
18679                    .value());
18680 
18681   Styles[3] = getLLVMStyle();
18682   Styles[3].Language = FormatStyle::LK_JavaScript;
18683   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n"
18684                                   "Language: JavaScript",
18685                                   &Styles[3])
18686                    .value());
18687 
18688   Styles[4] = getLLVMStyle();
18689   Styles[4].Language = FormatStyle::LK_JavaScript;
18690   EXPECT_EQ(0, parseConfiguration("---\n"
18691                                   "BasedOnStyle: LLVM\n"
18692                                   "IndentWidth: 123\n"
18693                                   "---\n"
18694                                   "BasedOnStyle: Google\n"
18695                                   "Language: JavaScript",
18696                                   &Styles[4])
18697                    .value());
18698   EXPECT_ALL_STYLES_EQUAL(Styles);
18699 }
18700 
18701 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME)                             \
18702   Style.FIELD = false;                                                         \
18703   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value());      \
18704   EXPECT_TRUE(Style.FIELD);                                                    \
18705   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value());     \
18706   EXPECT_FALSE(Style.FIELD);
18707 
18708 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD)
18709 
18710 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME)              \
18711   Style.STRUCT.FIELD = false;                                                  \
18712   EXPECT_EQ(0,                                                                 \
18713             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": true", &Style)   \
18714                 .value());                                                     \
18715   EXPECT_TRUE(Style.STRUCT.FIELD);                                             \
18716   EXPECT_EQ(0,                                                                 \
18717             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": false", &Style)  \
18718                 .value());                                                     \
18719   EXPECT_FALSE(Style.STRUCT.FIELD);
18720 
18721 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD)                                 \
18722   CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD)
18723 
18724 #define CHECK_PARSE(TEXT, FIELD, VALUE)                                        \
18725   EXPECT_NE(VALUE, Style.FIELD) << "Initial value already the same!";          \
18726   EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value());                      \
18727   EXPECT_EQ(VALUE, Style.FIELD) << "Unexpected value after parsing!"
18728 
18729 TEST_F(FormatTest, ParsesConfigurationBools) {
18730   FormatStyle Style = {};
18731   Style.Language = FormatStyle::LK_Cpp;
18732   CHECK_PARSE_BOOL(AlignTrailingComments);
18733   CHECK_PARSE_BOOL(AllowAllArgumentsOnNextLine);
18734   CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
18735   CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine);
18736   CHECK_PARSE_BOOL(AllowShortEnumsOnASingleLine);
18737   CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
18738   CHECK_PARSE_BOOL(BinPackArguments);
18739   CHECK_PARSE_BOOL(BinPackParameters);
18740   CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations);
18741   CHECK_PARSE_BOOL(BreakBeforeConceptDeclarations);
18742   CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
18743   CHECK_PARSE_BOOL(BreakStringLiterals);
18744   CHECK_PARSE_BOOL(CompactNamespaces);
18745   CHECK_PARSE_BOOL(DeriveLineEnding);
18746   CHECK_PARSE_BOOL(DerivePointerAlignment);
18747   CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding");
18748   CHECK_PARSE_BOOL(DisableFormat);
18749   CHECK_PARSE_BOOL(IndentAccessModifiers);
18750   CHECK_PARSE_BOOL(IndentCaseLabels);
18751   CHECK_PARSE_BOOL(IndentCaseBlocks);
18752   CHECK_PARSE_BOOL(IndentGotoLabels);
18753   CHECK_PARSE_BOOL(IndentRequires);
18754   CHECK_PARSE_BOOL(IndentWrappedFunctionNames);
18755   CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks);
18756   CHECK_PARSE_BOOL(ObjCSpaceAfterProperty);
18757   CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
18758   CHECK_PARSE_BOOL(Cpp11BracedListStyle);
18759   CHECK_PARSE_BOOL(ReflowComments);
18760   CHECK_PARSE_BOOL(SortUsingDeclarations);
18761   CHECK_PARSE_BOOL(SpacesInParentheses);
18762   CHECK_PARSE_BOOL(SpacesInSquareBrackets);
18763   CHECK_PARSE_BOOL(SpacesInConditionalStatement);
18764   CHECK_PARSE_BOOL(SpaceInEmptyBlock);
18765   CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
18766   CHECK_PARSE_BOOL(SpacesInContainerLiterals);
18767   CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
18768   CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
18769   CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword);
18770   CHECK_PARSE_BOOL(SpaceAfterLogicalNot);
18771   CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
18772   CHECK_PARSE_BOOL(SpaceBeforeCaseColon);
18773   CHECK_PARSE_BOOL(SpaceBeforeCpp11BracedList);
18774   CHECK_PARSE_BOOL(SpaceBeforeCtorInitializerColon);
18775   CHECK_PARSE_BOOL(SpaceBeforeInheritanceColon);
18776   CHECK_PARSE_BOOL(SpaceBeforeRangeBasedForLoopColon);
18777   CHECK_PARSE_BOOL(SpaceBeforeSquareBrackets);
18778   CHECK_PARSE_BOOL(UseCRLF);
18779 
18780   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterCaseLabel);
18781   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass);
18782   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum);
18783   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction);
18784   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace);
18785   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration);
18786   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct);
18787   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion);
18788   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterExternBlock);
18789   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch);
18790   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse);
18791   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeLambdaBody);
18792   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeWhile);
18793   CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces);
18794   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyFunction);
18795   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyRecord);
18796   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyNamespace);
18797   CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterControlStatements);
18798   CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterForeachMacros);
18799   CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions,
18800                           AfterFunctionDeclarationName);
18801   CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions,
18802                           AfterFunctionDefinitionName);
18803   CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterIfMacros);
18804   CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterOverloadedOperator);
18805   CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, BeforeNonEmptyParentheses);
18806 }
18807 
18808 #undef CHECK_PARSE_BOOL
18809 
18810 TEST_F(FormatTest, ParsesConfiguration) {
18811   FormatStyle Style = {};
18812   Style.Language = FormatStyle::LK_Cpp;
18813   CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
18814   CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",
18815               ConstructorInitializerIndentWidth, 1234u);
18816   CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u);
18817   CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
18818   CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
18819   CHECK_PARSE("PenaltyBreakAssignment: 1234", PenaltyBreakAssignment, 1234u);
18820   CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234",
18821               PenaltyBreakBeforeFirstCallParameter, 1234u);
18822   CHECK_PARSE("PenaltyBreakTemplateDeclaration: 1234",
18823               PenaltyBreakTemplateDeclaration, 1234u);
18824   CHECK_PARSE("PenaltyBreakOpenParenthesis: 1234", PenaltyBreakOpenParenthesis,
18825               1234u);
18826   CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
18827   CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
18828               PenaltyReturnTypeOnItsOwnLine, 1234u);
18829   CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
18830               SpacesBeforeTrailingComments, 1234u);
18831   CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
18832   CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u);
18833   CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$");
18834 
18835   Style.QualifierAlignment = FormatStyle::QAS_Right;
18836   CHECK_PARSE("QualifierAlignment: Leave", QualifierAlignment,
18837               FormatStyle::QAS_Leave);
18838   CHECK_PARSE("QualifierAlignment: Right", QualifierAlignment,
18839               FormatStyle::QAS_Right);
18840   CHECK_PARSE("QualifierAlignment: Left", QualifierAlignment,
18841               FormatStyle::QAS_Left);
18842   CHECK_PARSE("QualifierAlignment: Custom", QualifierAlignment,
18843               FormatStyle::QAS_Custom);
18844 
18845   Style.QualifierOrder.clear();
18846   CHECK_PARSE("QualifierOrder: [ const, volatile, type ]", QualifierOrder,
18847               std::vector<std::string>({"const", "volatile", "type"}));
18848   Style.QualifierOrder.clear();
18849   CHECK_PARSE("QualifierOrder: [const, type]", QualifierOrder,
18850               std::vector<std::string>({"const", "type"}));
18851   Style.QualifierOrder.clear();
18852   CHECK_PARSE("QualifierOrder: [volatile, type]", QualifierOrder,
18853               std::vector<std::string>({"volatile", "type"}));
18854 
18855   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
18856   CHECK_PARSE("AlignConsecutiveAssignments: None", AlignConsecutiveAssignments,
18857               FormatStyle::ACS_None);
18858   CHECK_PARSE("AlignConsecutiveAssignments: Consecutive",
18859               AlignConsecutiveAssignments, FormatStyle::ACS_Consecutive);
18860   CHECK_PARSE("AlignConsecutiveAssignments: AcrossEmptyLines",
18861               AlignConsecutiveAssignments, FormatStyle::ACS_AcrossEmptyLines);
18862   CHECK_PARSE("AlignConsecutiveAssignments: AcrossEmptyLinesAndComments",
18863               AlignConsecutiveAssignments,
18864               FormatStyle::ACS_AcrossEmptyLinesAndComments);
18865   // For backwards compability, false / true should still parse
18866   CHECK_PARSE("AlignConsecutiveAssignments: false", AlignConsecutiveAssignments,
18867               FormatStyle::ACS_None);
18868   CHECK_PARSE("AlignConsecutiveAssignments: true", AlignConsecutiveAssignments,
18869               FormatStyle::ACS_Consecutive);
18870 
18871   Style.AlignConsecutiveBitFields = FormatStyle::ACS_Consecutive;
18872   CHECK_PARSE("AlignConsecutiveBitFields: None", AlignConsecutiveBitFields,
18873               FormatStyle::ACS_None);
18874   CHECK_PARSE("AlignConsecutiveBitFields: Consecutive",
18875               AlignConsecutiveBitFields, FormatStyle::ACS_Consecutive);
18876   CHECK_PARSE("AlignConsecutiveBitFields: AcrossEmptyLines",
18877               AlignConsecutiveBitFields, FormatStyle::ACS_AcrossEmptyLines);
18878   CHECK_PARSE("AlignConsecutiveBitFields: AcrossEmptyLinesAndComments",
18879               AlignConsecutiveBitFields,
18880               FormatStyle::ACS_AcrossEmptyLinesAndComments);
18881   // For backwards compability, false / true should still parse
18882   CHECK_PARSE("AlignConsecutiveBitFields: false", AlignConsecutiveBitFields,
18883               FormatStyle::ACS_None);
18884   CHECK_PARSE("AlignConsecutiveBitFields: true", AlignConsecutiveBitFields,
18885               FormatStyle::ACS_Consecutive);
18886 
18887   Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
18888   CHECK_PARSE("AlignConsecutiveMacros: None", AlignConsecutiveMacros,
18889               FormatStyle::ACS_None);
18890   CHECK_PARSE("AlignConsecutiveMacros: Consecutive", AlignConsecutiveMacros,
18891               FormatStyle::ACS_Consecutive);
18892   CHECK_PARSE("AlignConsecutiveMacros: AcrossEmptyLines",
18893               AlignConsecutiveMacros, FormatStyle::ACS_AcrossEmptyLines);
18894   CHECK_PARSE("AlignConsecutiveMacros: AcrossEmptyLinesAndComments",
18895               AlignConsecutiveMacros,
18896               FormatStyle::ACS_AcrossEmptyLinesAndComments);
18897   // For backwards compability, false / true should still parse
18898   CHECK_PARSE("AlignConsecutiveMacros: false", AlignConsecutiveMacros,
18899               FormatStyle::ACS_None);
18900   CHECK_PARSE("AlignConsecutiveMacros: true", AlignConsecutiveMacros,
18901               FormatStyle::ACS_Consecutive);
18902 
18903   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
18904   CHECK_PARSE("AlignConsecutiveDeclarations: None",
18905               AlignConsecutiveDeclarations, FormatStyle::ACS_None);
18906   CHECK_PARSE("AlignConsecutiveDeclarations: Consecutive",
18907               AlignConsecutiveDeclarations, FormatStyle::ACS_Consecutive);
18908   CHECK_PARSE("AlignConsecutiveDeclarations: AcrossEmptyLines",
18909               AlignConsecutiveDeclarations, FormatStyle::ACS_AcrossEmptyLines);
18910   CHECK_PARSE("AlignConsecutiveDeclarations: AcrossEmptyLinesAndComments",
18911               AlignConsecutiveDeclarations,
18912               FormatStyle::ACS_AcrossEmptyLinesAndComments);
18913   // For backwards compability, false / true should still parse
18914   CHECK_PARSE("AlignConsecutiveDeclarations: false",
18915               AlignConsecutiveDeclarations, FormatStyle::ACS_None);
18916   CHECK_PARSE("AlignConsecutiveDeclarations: true",
18917               AlignConsecutiveDeclarations, FormatStyle::ACS_Consecutive);
18918 
18919   Style.PointerAlignment = FormatStyle::PAS_Middle;
18920   CHECK_PARSE("PointerAlignment: Left", PointerAlignment,
18921               FormatStyle::PAS_Left);
18922   CHECK_PARSE("PointerAlignment: Right", PointerAlignment,
18923               FormatStyle::PAS_Right);
18924   CHECK_PARSE("PointerAlignment: Middle", PointerAlignment,
18925               FormatStyle::PAS_Middle);
18926   Style.ReferenceAlignment = FormatStyle::RAS_Middle;
18927   CHECK_PARSE("ReferenceAlignment: Pointer", ReferenceAlignment,
18928               FormatStyle::RAS_Pointer);
18929   CHECK_PARSE("ReferenceAlignment: Left", ReferenceAlignment,
18930               FormatStyle::RAS_Left);
18931   CHECK_PARSE("ReferenceAlignment: Right", ReferenceAlignment,
18932               FormatStyle::RAS_Right);
18933   CHECK_PARSE("ReferenceAlignment: Middle", ReferenceAlignment,
18934               FormatStyle::RAS_Middle);
18935   // For backward compatibility:
18936   CHECK_PARSE("PointerBindsToType: Left", PointerAlignment,
18937               FormatStyle::PAS_Left);
18938   CHECK_PARSE("PointerBindsToType: Right", PointerAlignment,
18939               FormatStyle::PAS_Right);
18940   CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment,
18941               FormatStyle::PAS_Middle);
18942 
18943   Style.Standard = FormatStyle::LS_Auto;
18944   CHECK_PARSE("Standard: c++03", Standard, FormatStyle::LS_Cpp03);
18945   CHECK_PARSE("Standard: c++11", Standard, FormatStyle::LS_Cpp11);
18946   CHECK_PARSE("Standard: c++14", Standard, FormatStyle::LS_Cpp14);
18947   CHECK_PARSE("Standard: c++17", Standard, FormatStyle::LS_Cpp17);
18948   CHECK_PARSE("Standard: c++20", Standard, FormatStyle::LS_Cpp20);
18949   CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
18950   CHECK_PARSE("Standard: Latest", Standard, FormatStyle::LS_Latest);
18951   // Legacy aliases:
18952   CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03);
18953   CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Latest);
18954   CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
18955   CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
18956 
18957   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
18958   CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment",
18959               BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment);
18960   CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators,
18961               FormatStyle::BOS_None);
18962   CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators,
18963               FormatStyle::BOS_All);
18964   // For backward compatibility:
18965   CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators,
18966               FormatStyle::BOS_None);
18967   CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators,
18968               FormatStyle::BOS_All);
18969 
18970   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
18971   CHECK_PARSE("BreakConstructorInitializers: BeforeComma",
18972               BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma);
18973   CHECK_PARSE("BreakConstructorInitializers: AfterColon",
18974               BreakConstructorInitializers, FormatStyle::BCIS_AfterColon);
18975   CHECK_PARSE("BreakConstructorInitializers: BeforeColon",
18976               BreakConstructorInitializers, FormatStyle::BCIS_BeforeColon);
18977   // For backward compatibility:
18978   CHECK_PARSE("BreakConstructorInitializersBeforeComma: true",
18979               BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma);
18980 
18981   Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
18982   CHECK_PARSE("BreakInheritanceList: AfterComma", BreakInheritanceList,
18983               FormatStyle::BILS_AfterComma);
18984   CHECK_PARSE("BreakInheritanceList: BeforeComma", BreakInheritanceList,
18985               FormatStyle::BILS_BeforeComma);
18986   CHECK_PARSE("BreakInheritanceList: AfterColon", BreakInheritanceList,
18987               FormatStyle::BILS_AfterColon);
18988   CHECK_PARSE("BreakInheritanceList: BeforeColon", BreakInheritanceList,
18989               FormatStyle::BILS_BeforeColon);
18990   // For backward compatibility:
18991   CHECK_PARSE("BreakBeforeInheritanceComma: true", BreakInheritanceList,
18992               FormatStyle::BILS_BeforeComma);
18993 
18994   Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack;
18995   CHECK_PARSE("PackConstructorInitializers: Never", PackConstructorInitializers,
18996               FormatStyle::PCIS_Never);
18997   CHECK_PARSE("PackConstructorInitializers: BinPack",
18998               PackConstructorInitializers, FormatStyle::PCIS_BinPack);
18999   CHECK_PARSE("PackConstructorInitializers: CurrentLine",
19000               PackConstructorInitializers, FormatStyle::PCIS_CurrentLine);
19001   CHECK_PARSE("PackConstructorInitializers: NextLine",
19002               PackConstructorInitializers, FormatStyle::PCIS_NextLine);
19003   // For backward compatibility:
19004   CHECK_PARSE("BasedOnStyle: Google\n"
19005               "ConstructorInitializerAllOnOneLineOrOnePerLine: true\n"
19006               "AllowAllConstructorInitializersOnNextLine: false",
19007               PackConstructorInitializers, FormatStyle::PCIS_CurrentLine);
19008   Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
19009   CHECK_PARSE("BasedOnStyle: Google\n"
19010               "ConstructorInitializerAllOnOneLineOrOnePerLine: false",
19011               PackConstructorInitializers, FormatStyle::PCIS_BinPack);
19012   CHECK_PARSE("ConstructorInitializerAllOnOneLineOrOnePerLine: true\n"
19013               "AllowAllConstructorInitializersOnNextLine: true",
19014               PackConstructorInitializers, FormatStyle::PCIS_NextLine);
19015   Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack;
19016   CHECK_PARSE("ConstructorInitializerAllOnOneLineOrOnePerLine: true\n"
19017               "AllowAllConstructorInitializersOnNextLine: false",
19018               PackConstructorInitializers, FormatStyle::PCIS_CurrentLine);
19019 
19020   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
19021   CHECK_PARSE("EmptyLineBeforeAccessModifier: Never",
19022               EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Never);
19023   CHECK_PARSE("EmptyLineBeforeAccessModifier: Leave",
19024               EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Leave);
19025   CHECK_PARSE("EmptyLineBeforeAccessModifier: LogicalBlock",
19026               EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_LogicalBlock);
19027   CHECK_PARSE("EmptyLineBeforeAccessModifier: Always",
19028               EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Always);
19029 
19030   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
19031   CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket,
19032               FormatStyle::BAS_Align);
19033   CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket,
19034               FormatStyle::BAS_DontAlign);
19035   CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket,
19036               FormatStyle::BAS_AlwaysBreak);
19037   // For backward compatibility:
19038   CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket,
19039               FormatStyle::BAS_DontAlign);
19040   CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket,
19041               FormatStyle::BAS_Align);
19042 
19043   Style.AlignEscapedNewlines = FormatStyle::ENAS_Left;
19044   CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines,
19045               FormatStyle::ENAS_DontAlign);
19046   CHECK_PARSE("AlignEscapedNewlines: Left", AlignEscapedNewlines,
19047               FormatStyle::ENAS_Left);
19048   CHECK_PARSE("AlignEscapedNewlines: Right", AlignEscapedNewlines,
19049               FormatStyle::ENAS_Right);
19050   // For backward compatibility:
19051   CHECK_PARSE("AlignEscapedNewlinesLeft: true", AlignEscapedNewlines,
19052               FormatStyle::ENAS_Left);
19053   CHECK_PARSE("AlignEscapedNewlinesLeft: false", AlignEscapedNewlines,
19054               FormatStyle::ENAS_Right);
19055 
19056   Style.AlignOperands = FormatStyle::OAS_Align;
19057   CHECK_PARSE("AlignOperands: DontAlign", AlignOperands,
19058               FormatStyle::OAS_DontAlign);
19059   CHECK_PARSE("AlignOperands: Align", AlignOperands, FormatStyle::OAS_Align);
19060   CHECK_PARSE("AlignOperands: AlignAfterOperator", AlignOperands,
19061               FormatStyle::OAS_AlignAfterOperator);
19062   // For backward compatibility:
19063   CHECK_PARSE("AlignOperands: false", AlignOperands,
19064               FormatStyle::OAS_DontAlign);
19065   CHECK_PARSE("AlignOperands: true", AlignOperands, FormatStyle::OAS_Align);
19066 
19067   Style.UseTab = FormatStyle::UT_ForIndentation;
19068   CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
19069   CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
19070   CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
19071   CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab,
19072               FormatStyle::UT_ForContinuationAndIndentation);
19073   CHECK_PARSE("UseTab: AlignWithSpaces", UseTab,
19074               FormatStyle::UT_AlignWithSpaces);
19075   // For backward compatibility:
19076   CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never);
19077   CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always);
19078 
19079   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty;
19080   CHECK_PARSE("AllowShortBlocksOnASingleLine: Never",
19081               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never);
19082   CHECK_PARSE("AllowShortBlocksOnASingleLine: Empty",
19083               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Empty);
19084   CHECK_PARSE("AllowShortBlocksOnASingleLine: Always",
19085               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always);
19086   // For backward compatibility:
19087   CHECK_PARSE("AllowShortBlocksOnASingleLine: false",
19088               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never);
19089   CHECK_PARSE("AllowShortBlocksOnASingleLine: true",
19090               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always);
19091 
19092   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
19093   CHECK_PARSE("AllowShortFunctionsOnASingleLine: None",
19094               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
19095   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline",
19096               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline);
19097   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty",
19098               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty);
19099   CHECK_PARSE("AllowShortFunctionsOnASingleLine: All",
19100               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
19101   // For backward compatibility:
19102   CHECK_PARSE("AllowShortFunctionsOnASingleLine: false",
19103               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
19104   CHECK_PARSE("AllowShortFunctionsOnASingleLine: true",
19105               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
19106 
19107   Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Both;
19108   CHECK_PARSE("SpaceAroundPointerQualifiers: Default",
19109               SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Default);
19110   CHECK_PARSE("SpaceAroundPointerQualifiers: Before",
19111               SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Before);
19112   CHECK_PARSE("SpaceAroundPointerQualifiers: After",
19113               SpaceAroundPointerQualifiers, FormatStyle::SAPQ_After);
19114   CHECK_PARSE("SpaceAroundPointerQualifiers: Both",
19115               SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Both);
19116 
19117   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
19118   CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens,
19119               FormatStyle::SBPO_Never);
19120   CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens,
19121               FormatStyle::SBPO_Always);
19122   CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens,
19123               FormatStyle::SBPO_ControlStatements);
19124   CHECK_PARSE("SpaceBeforeParens: ControlStatementsExceptControlMacros",
19125               SpaceBeforeParens,
19126               FormatStyle::SBPO_ControlStatementsExceptControlMacros);
19127   CHECK_PARSE("SpaceBeforeParens: NonEmptyParentheses", SpaceBeforeParens,
19128               FormatStyle::SBPO_NonEmptyParentheses);
19129   CHECK_PARSE("SpaceBeforeParens: Custom", SpaceBeforeParens,
19130               FormatStyle::SBPO_Custom);
19131   // For backward compatibility:
19132   CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens,
19133               FormatStyle::SBPO_Never);
19134   CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens,
19135               FormatStyle::SBPO_ControlStatements);
19136   CHECK_PARSE("SpaceBeforeParens: ControlStatementsExceptForEachMacros",
19137               SpaceBeforeParens,
19138               FormatStyle::SBPO_ControlStatementsExceptControlMacros);
19139 
19140   Style.ColumnLimit = 123;
19141   FormatStyle BaseStyle = getLLVMStyle();
19142   CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
19143   CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
19144 
19145   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
19146   CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
19147               FormatStyle::BS_Attach);
19148   CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
19149               FormatStyle::BS_Linux);
19150   CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces,
19151               FormatStyle::BS_Mozilla);
19152   CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
19153               FormatStyle::BS_Stroustrup);
19154   CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces,
19155               FormatStyle::BS_Allman);
19156   CHECK_PARSE("BreakBeforeBraces: Whitesmiths", BreakBeforeBraces,
19157               FormatStyle::BS_Whitesmiths);
19158   CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU);
19159   CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces,
19160               FormatStyle::BS_WebKit);
19161   CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces,
19162               FormatStyle::BS_Custom);
19163 
19164   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never;
19165   CHECK_PARSE("BraceWrapping:\n"
19166               "  AfterControlStatement: MultiLine",
19167               BraceWrapping.AfterControlStatement,
19168               FormatStyle::BWACS_MultiLine);
19169   CHECK_PARSE("BraceWrapping:\n"
19170               "  AfterControlStatement: Always",
19171               BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always);
19172   CHECK_PARSE("BraceWrapping:\n"
19173               "  AfterControlStatement: Never",
19174               BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never);
19175   // For backward compatibility:
19176   CHECK_PARSE("BraceWrapping:\n"
19177               "  AfterControlStatement: true",
19178               BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always);
19179   CHECK_PARSE("BraceWrapping:\n"
19180               "  AfterControlStatement: false",
19181               BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never);
19182 
19183   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
19184   CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType,
19185               FormatStyle::RTBS_None);
19186   CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType,
19187               FormatStyle::RTBS_All);
19188   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel",
19189               AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel);
19190   CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions",
19191               AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions);
19192   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions",
19193               AlwaysBreakAfterReturnType,
19194               FormatStyle::RTBS_TopLevelDefinitions);
19195 
19196   Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
19197   CHECK_PARSE("AlwaysBreakTemplateDeclarations: No",
19198               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_No);
19199   CHECK_PARSE("AlwaysBreakTemplateDeclarations: MultiLine",
19200               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine);
19201   CHECK_PARSE("AlwaysBreakTemplateDeclarations: Yes",
19202               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes);
19203   CHECK_PARSE("AlwaysBreakTemplateDeclarations: false",
19204               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine);
19205   CHECK_PARSE("AlwaysBreakTemplateDeclarations: true",
19206               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes);
19207 
19208   Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
19209   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None",
19210               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None);
19211   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All",
19212               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All);
19213   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel",
19214               AlwaysBreakAfterDefinitionReturnType,
19215               FormatStyle::DRTBS_TopLevel);
19216 
19217   Style.NamespaceIndentation = FormatStyle::NI_All;
19218   CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
19219               FormatStyle::NI_None);
19220   CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation,
19221               FormatStyle::NI_Inner);
19222   CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
19223               FormatStyle::NI_All);
19224 
19225   Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_OnlyFirstIf;
19226   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Never",
19227               AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never);
19228   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: WithoutElse",
19229               AllowShortIfStatementsOnASingleLine,
19230               FormatStyle::SIS_WithoutElse);
19231   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: OnlyFirstIf",
19232               AllowShortIfStatementsOnASingleLine,
19233               FormatStyle::SIS_OnlyFirstIf);
19234   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: AllIfsAndElse",
19235               AllowShortIfStatementsOnASingleLine,
19236               FormatStyle::SIS_AllIfsAndElse);
19237   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Always",
19238               AllowShortIfStatementsOnASingleLine,
19239               FormatStyle::SIS_OnlyFirstIf);
19240   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: false",
19241               AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never);
19242   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: true",
19243               AllowShortIfStatementsOnASingleLine,
19244               FormatStyle::SIS_WithoutElse);
19245 
19246   Style.IndentExternBlock = FormatStyle::IEBS_NoIndent;
19247   CHECK_PARSE("IndentExternBlock: AfterExternBlock", IndentExternBlock,
19248               FormatStyle::IEBS_AfterExternBlock);
19249   CHECK_PARSE("IndentExternBlock: Indent", IndentExternBlock,
19250               FormatStyle::IEBS_Indent);
19251   CHECK_PARSE("IndentExternBlock: NoIndent", IndentExternBlock,
19252               FormatStyle::IEBS_NoIndent);
19253   CHECK_PARSE("IndentExternBlock: true", IndentExternBlock,
19254               FormatStyle::IEBS_Indent);
19255   CHECK_PARSE("IndentExternBlock: false", IndentExternBlock,
19256               FormatStyle::IEBS_NoIndent);
19257 
19258   Style.BitFieldColonSpacing = FormatStyle::BFCS_None;
19259   CHECK_PARSE("BitFieldColonSpacing: Both", BitFieldColonSpacing,
19260               FormatStyle::BFCS_Both);
19261   CHECK_PARSE("BitFieldColonSpacing: None", BitFieldColonSpacing,
19262               FormatStyle::BFCS_None);
19263   CHECK_PARSE("BitFieldColonSpacing: Before", BitFieldColonSpacing,
19264               FormatStyle::BFCS_Before);
19265   CHECK_PARSE("BitFieldColonSpacing: After", BitFieldColonSpacing,
19266               FormatStyle::BFCS_After);
19267 
19268   Style.SortJavaStaticImport = FormatStyle::SJSIO_Before;
19269   CHECK_PARSE("SortJavaStaticImport: After", SortJavaStaticImport,
19270               FormatStyle::SJSIO_After);
19271   CHECK_PARSE("SortJavaStaticImport: Before", SortJavaStaticImport,
19272               FormatStyle::SJSIO_Before);
19273 
19274   // FIXME: This is required because parsing a configuration simply overwrites
19275   // the first N elements of the list instead of resetting it.
19276   Style.ForEachMacros.clear();
19277   std::vector<std::string> BoostForeach;
19278   BoostForeach.push_back("BOOST_FOREACH");
19279   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach);
19280   std::vector<std::string> BoostAndQForeach;
19281   BoostAndQForeach.push_back("BOOST_FOREACH");
19282   BoostAndQForeach.push_back("Q_FOREACH");
19283   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
19284               BoostAndQForeach);
19285 
19286   Style.IfMacros.clear();
19287   std::vector<std::string> CustomIfs;
19288   CustomIfs.push_back("MYIF");
19289   CHECK_PARSE("IfMacros: [MYIF]", IfMacros, CustomIfs);
19290 
19291   Style.AttributeMacros.clear();
19292   CHECK_PARSE("BasedOnStyle: LLVM", AttributeMacros,
19293               std::vector<std::string>{"__capability"});
19294   CHECK_PARSE("AttributeMacros: [attr1, attr2]", AttributeMacros,
19295               std::vector<std::string>({"attr1", "attr2"}));
19296 
19297   Style.StatementAttributeLikeMacros.clear();
19298   CHECK_PARSE("StatementAttributeLikeMacros: [emit,Q_EMIT]",
19299               StatementAttributeLikeMacros,
19300               std::vector<std::string>({"emit", "Q_EMIT"}));
19301 
19302   Style.StatementMacros.clear();
19303   CHECK_PARSE("StatementMacros: [QUNUSED]", StatementMacros,
19304               std::vector<std::string>{"QUNUSED"});
19305   CHECK_PARSE("StatementMacros: [QUNUSED, QT_REQUIRE_VERSION]", StatementMacros,
19306               std::vector<std::string>({"QUNUSED", "QT_REQUIRE_VERSION"}));
19307 
19308   Style.NamespaceMacros.clear();
19309   CHECK_PARSE("NamespaceMacros: [TESTSUITE]", NamespaceMacros,
19310               std::vector<std::string>{"TESTSUITE"});
19311   CHECK_PARSE("NamespaceMacros: [TESTSUITE, SUITE]", NamespaceMacros,
19312               std::vector<std::string>({"TESTSUITE", "SUITE"}));
19313 
19314   Style.WhitespaceSensitiveMacros.clear();
19315   CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE]",
19316               WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"});
19317   CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE, ASSERT]",
19318               WhitespaceSensitiveMacros,
19319               std::vector<std::string>({"STRINGIZE", "ASSERT"}));
19320   Style.WhitespaceSensitiveMacros.clear();
19321   CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE']",
19322               WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"});
19323   CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE', 'ASSERT']",
19324               WhitespaceSensitiveMacros,
19325               std::vector<std::string>({"STRINGIZE", "ASSERT"}));
19326 
19327   Style.IncludeStyle.IncludeCategories.clear();
19328   std::vector<tooling::IncludeStyle::IncludeCategory> ExpectedCategories = {
19329       {"abc/.*", 2, 0, false}, {".*", 1, 0, true}};
19330   CHECK_PARSE("IncludeCategories:\n"
19331               "  - Regex: abc/.*\n"
19332               "    Priority: 2\n"
19333               "  - Regex: .*\n"
19334               "    Priority: 1\n"
19335               "    CaseSensitive: true\n",
19336               IncludeStyle.IncludeCategories, ExpectedCategories);
19337   CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeStyle.IncludeIsMainRegex,
19338               "abc$");
19339   CHECK_PARSE("IncludeIsMainSourceRegex: 'abc$'",
19340               IncludeStyle.IncludeIsMainSourceRegex, "abc$");
19341 
19342   Style.SortIncludes = FormatStyle::SI_Never;
19343   CHECK_PARSE("SortIncludes: true", SortIncludes,
19344               FormatStyle::SI_CaseSensitive);
19345   CHECK_PARSE("SortIncludes: false", SortIncludes, FormatStyle::SI_Never);
19346   CHECK_PARSE("SortIncludes: CaseInsensitive", SortIncludes,
19347               FormatStyle::SI_CaseInsensitive);
19348   CHECK_PARSE("SortIncludes: CaseSensitive", SortIncludes,
19349               FormatStyle::SI_CaseSensitive);
19350   CHECK_PARSE("SortIncludes: Never", SortIncludes, FormatStyle::SI_Never);
19351 
19352   Style.RawStringFormats.clear();
19353   std::vector<FormatStyle::RawStringFormat> ExpectedRawStringFormats = {
19354       {
19355           FormatStyle::LK_TextProto,
19356           {"pb", "proto"},
19357           {"PARSE_TEXT_PROTO"},
19358           /*CanonicalDelimiter=*/"",
19359           "llvm",
19360       },
19361       {
19362           FormatStyle::LK_Cpp,
19363           {"cc", "cpp"},
19364           {"C_CODEBLOCK", "CPPEVAL"},
19365           /*CanonicalDelimiter=*/"cc",
19366           /*BasedOnStyle=*/"",
19367       },
19368   };
19369 
19370   CHECK_PARSE("RawStringFormats:\n"
19371               "  - Language: TextProto\n"
19372               "    Delimiters:\n"
19373               "      - 'pb'\n"
19374               "      - 'proto'\n"
19375               "    EnclosingFunctions:\n"
19376               "      - 'PARSE_TEXT_PROTO'\n"
19377               "    BasedOnStyle: llvm\n"
19378               "  - Language: Cpp\n"
19379               "    Delimiters:\n"
19380               "      - 'cc'\n"
19381               "      - 'cpp'\n"
19382               "    EnclosingFunctions:\n"
19383               "      - 'C_CODEBLOCK'\n"
19384               "      - 'CPPEVAL'\n"
19385               "    CanonicalDelimiter: 'cc'",
19386               RawStringFormats, ExpectedRawStringFormats);
19387 
19388   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
19389               "  Minimum: 0\n"
19390               "  Maximum: 0",
19391               SpacesInLineCommentPrefix.Minimum, 0u);
19392   EXPECT_EQ(Style.SpacesInLineCommentPrefix.Maximum, 0u);
19393   Style.SpacesInLineCommentPrefix.Minimum = 1;
19394   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
19395               "  Minimum: 2",
19396               SpacesInLineCommentPrefix.Minimum, 0u);
19397   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
19398               "  Maximum: -1",
19399               SpacesInLineCommentPrefix.Maximum, -1u);
19400   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
19401               "  Minimum: 2",
19402               SpacesInLineCommentPrefix.Minimum, 2u);
19403   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
19404               "  Maximum: 1",
19405               SpacesInLineCommentPrefix.Maximum, 1u);
19406   EXPECT_EQ(Style.SpacesInLineCommentPrefix.Minimum, 1u);
19407 
19408   Style.SpacesInAngles = FormatStyle::SIAS_Always;
19409   CHECK_PARSE("SpacesInAngles: Never", SpacesInAngles, FormatStyle::SIAS_Never);
19410   CHECK_PARSE("SpacesInAngles: Always", SpacesInAngles,
19411               FormatStyle::SIAS_Always);
19412   CHECK_PARSE("SpacesInAngles: Leave", SpacesInAngles, FormatStyle::SIAS_Leave);
19413   // For backward compatibility:
19414   CHECK_PARSE("SpacesInAngles: false", SpacesInAngles, FormatStyle::SIAS_Never);
19415   CHECK_PARSE("SpacesInAngles: true", SpacesInAngles, FormatStyle::SIAS_Always);
19416 }
19417 
19418 TEST_F(FormatTest, ParsesConfigurationWithLanguages) {
19419   FormatStyle Style = {};
19420   Style.Language = FormatStyle::LK_Cpp;
19421   CHECK_PARSE("Language: Cpp\n"
19422               "IndentWidth: 12",
19423               IndentWidth, 12u);
19424   EXPECT_EQ(parseConfiguration("Language: JavaScript\n"
19425                                "IndentWidth: 34",
19426                                &Style),
19427             ParseError::Unsuitable);
19428   FormatStyle BinPackedTCS = {};
19429   BinPackedTCS.Language = FormatStyle::LK_JavaScript;
19430   EXPECT_EQ(parseConfiguration("BinPackArguments: true\n"
19431                                "InsertTrailingCommas: Wrapped",
19432                                &BinPackedTCS),
19433             ParseError::BinPackTrailingCommaConflict);
19434   EXPECT_EQ(12u, Style.IndentWidth);
19435   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
19436   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
19437 
19438   Style.Language = FormatStyle::LK_JavaScript;
19439   CHECK_PARSE("Language: JavaScript\n"
19440               "IndentWidth: 12",
19441               IndentWidth, 12u);
19442   CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u);
19443   EXPECT_EQ(parseConfiguration("Language: Cpp\n"
19444                                "IndentWidth: 34",
19445                                &Style),
19446             ParseError::Unsuitable);
19447   EXPECT_EQ(23u, Style.IndentWidth);
19448   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
19449   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
19450 
19451   CHECK_PARSE("BasedOnStyle: LLVM\n"
19452               "IndentWidth: 67",
19453               IndentWidth, 67u);
19454 
19455   CHECK_PARSE("---\n"
19456               "Language: JavaScript\n"
19457               "IndentWidth: 12\n"
19458               "---\n"
19459               "Language: Cpp\n"
19460               "IndentWidth: 34\n"
19461               "...\n",
19462               IndentWidth, 12u);
19463 
19464   Style.Language = FormatStyle::LK_Cpp;
19465   CHECK_PARSE("---\n"
19466               "Language: JavaScript\n"
19467               "IndentWidth: 12\n"
19468               "---\n"
19469               "Language: Cpp\n"
19470               "IndentWidth: 34\n"
19471               "...\n",
19472               IndentWidth, 34u);
19473   CHECK_PARSE("---\n"
19474               "IndentWidth: 78\n"
19475               "---\n"
19476               "Language: JavaScript\n"
19477               "IndentWidth: 56\n"
19478               "...\n",
19479               IndentWidth, 78u);
19480 
19481   Style.ColumnLimit = 123;
19482   Style.IndentWidth = 234;
19483   Style.BreakBeforeBraces = FormatStyle::BS_Linux;
19484   Style.TabWidth = 345;
19485   EXPECT_FALSE(parseConfiguration("---\n"
19486                                   "IndentWidth: 456\n"
19487                                   "BreakBeforeBraces: Allman\n"
19488                                   "---\n"
19489                                   "Language: JavaScript\n"
19490                                   "IndentWidth: 111\n"
19491                                   "TabWidth: 111\n"
19492                                   "---\n"
19493                                   "Language: Cpp\n"
19494                                   "BreakBeforeBraces: Stroustrup\n"
19495                                   "TabWidth: 789\n"
19496                                   "...\n",
19497                                   &Style));
19498   EXPECT_EQ(123u, Style.ColumnLimit);
19499   EXPECT_EQ(456u, Style.IndentWidth);
19500   EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces);
19501   EXPECT_EQ(789u, Style.TabWidth);
19502 
19503   EXPECT_EQ(parseConfiguration("---\n"
19504                                "Language: JavaScript\n"
19505                                "IndentWidth: 56\n"
19506                                "---\n"
19507                                "IndentWidth: 78\n"
19508                                "...\n",
19509                                &Style),
19510             ParseError::Error);
19511   EXPECT_EQ(parseConfiguration("---\n"
19512                                "Language: JavaScript\n"
19513                                "IndentWidth: 56\n"
19514                                "---\n"
19515                                "Language: JavaScript\n"
19516                                "IndentWidth: 78\n"
19517                                "...\n",
19518                                &Style),
19519             ParseError::Error);
19520 
19521   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
19522 }
19523 
19524 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) {
19525   FormatStyle Style = {};
19526   Style.Language = FormatStyle::LK_JavaScript;
19527   Style.BreakBeforeTernaryOperators = true;
19528   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value());
19529   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
19530 
19531   Style.BreakBeforeTernaryOperators = true;
19532   EXPECT_EQ(0, parseConfiguration("---\n"
19533                                   "BasedOnStyle: Google\n"
19534                                   "---\n"
19535                                   "Language: JavaScript\n"
19536                                   "IndentWidth: 76\n"
19537                                   "...\n",
19538                                   &Style)
19539                    .value());
19540   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
19541   EXPECT_EQ(76u, Style.IndentWidth);
19542   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
19543 }
19544 
19545 TEST_F(FormatTest, ConfigurationRoundTripTest) {
19546   FormatStyle Style = getLLVMStyle();
19547   std::string YAML = configurationAsText(Style);
19548   FormatStyle ParsedStyle = {};
19549   ParsedStyle.Language = FormatStyle::LK_Cpp;
19550   EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
19551   EXPECT_EQ(Style, ParsedStyle);
19552 }
19553 
19554 TEST_F(FormatTest, WorksFor8bitEncodings) {
19555   EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
19556             "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
19557             "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
19558             "\"\xef\xee\xf0\xf3...\"",
19559             format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
19560                    "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
19561                    "\xef\xee\xf0\xf3...\"",
19562                    getLLVMStyleWithColumns(12)));
19563 }
19564 
19565 TEST_F(FormatTest, HandlesUTF8BOM) {
19566   EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf"));
19567   EXPECT_EQ("\xef\xbb\xbf#include <iostream>",
19568             format("\xef\xbb\xbf#include <iostream>"));
19569   EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>",
19570             format("\xef\xbb\xbf\n#include <iostream>"));
19571 }
19572 
19573 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
19574 #if !defined(_MSC_VER)
19575 
19576 TEST_F(FormatTest, CountsUTF8CharactersProperly) {
19577   verifyFormat("\"Однажды в студёную зимнюю пору...\"",
19578                getLLVMStyleWithColumns(35));
19579   verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
19580                getLLVMStyleWithColumns(31));
19581   verifyFormat("// Однажды в студёную зимнюю пору...",
19582                getLLVMStyleWithColumns(36));
19583   verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32));
19584   verifyFormat("/* Однажды в студёную зимнюю пору... */",
19585                getLLVMStyleWithColumns(39));
19586   verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
19587                getLLVMStyleWithColumns(35));
19588 }
19589 
19590 TEST_F(FormatTest, SplitsUTF8Strings) {
19591   // Non-printable characters' width is currently considered to be the length in
19592   // bytes in UTF8. The characters can be displayed in very different manner
19593   // (zero-width, single width with a substitution glyph, expanded to their code
19594   // (e.g. "<8d>"), so there's no single correct way to handle them.
19595   EXPECT_EQ("\"aaaaÄ\"\n"
19596             "\"\xc2\x8d\";",
19597             format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
19598   EXPECT_EQ("\"aaaaaaaÄ\"\n"
19599             "\"\xc2\x8d\";",
19600             format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
19601   EXPECT_EQ("\"Однажды, в \"\n"
19602             "\"студёную \"\n"
19603             "\"зимнюю \"\n"
19604             "\"пору,\"",
19605             format("\"Однажды, в студёную зимнюю пору,\"",
19606                    getLLVMStyleWithColumns(13)));
19607   EXPECT_EQ(
19608       "\"一 二 三 \"\n"
19609       "\"四 五六 \"\n"
19610       "\"七 八 九 \"\n"
19611       "\"十\"",
19612       format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11)));
19613   EXPECT_EQ("\"一\t\"\n"
19614             "\"二 \t\"\n"
19615             "\"三 四 \"\n"
19616             "\"五\t\"\n"
19617             "\"六 \t\"\n"
19618             "\"七 \"\n"
19619             "\"八九十\tqq\"",
19620             format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
19621                    getLLVMStyleWithColumns(11)));
19622 
19623   // UTF8 character in an escape sequence.
19624   EXPECT_EQ("\"aaaaaa\"\n"
19625             "\"\\\xC2\x8D\"",
19626             format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10)));
19627 }
19628 
19629 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
19630   EXPECT_EQ("const char *sssss =\n"
19631             "    \"一二三四五六七八\\\n"
19632             " 九 十\";",
19633             format("const char *sssss = \"一二三四五六七八\\\n"
19634                    " 九 十\";",
19635                    getLLVMStyleWithColumns(30)));
19636 }
19637 
19638 TEST_F(FormatTest, SplitsUTF8LineComments) {
19639   EXPECT_EQ("// aaaaÄ\xc2\x8d",
19640             format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10)));
19641   EXPECT_EQ("// Я из лесу\n"
19642             "// вышел; был\n"
19643             "// сильный\n"
19644             "// мороз.",
19645             format("// Я из лесу вышел; был сильный мороз.",
19646                    getLLVMStyleWithColumns(13)));
19647   EXPECT_EQ("// 一二三\n"
19648             "// 四五六七\n"
19649             "// 八  九\n"
19650             "// 十",
19651             format("// 一二三 四五六七 八  九 十", getLLVMStyleWithColumns(9)));
19652 }
19653 
19654 TEST_F(FormatTest, SplitsUTF8BlockComments) {
19655   EXPECT_EQ("/* Гляжу,\n"
19656             " * поднимается\n"
19657             " * медленно в\n"
19658             " * гору\n"
19659             " * Лошадка,\n"
19660             " * везущая\n"
19661             " * хворосту\n"
19662             " * воз. */",
19663             format("/* Гляжу, поднимается медленно в гору\n"
19664                    " * Лошадка, везущая хворосту воз. */",
19665                    getLLVMStyleWithColumns(13)));
19666   EXPECT_EQ(
19667       "/* 一二三\n"
19668       " * 四五六七\n"
19669       " * 八  九\n"
19670       " * 十  */",
19671       format("/* 一二三 四五六七 八  九 十  */", getLLVMStyleWithColumns(9)));
19672   EXPECT_EQ("/* �������� ��������\n"
19673             " * ��������\n"
19674             " * ������-�� */",
19675             format("/* �������� �������� �������� ������-�� */", getLLVMStyleWithColumns(12)));
19676 }
19677 
19678 #endif // _MSC_VER
19679 
19680 TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
19681   FormatStyle Style = getLLVMStyle();
19682 
19683   Style.ConstructorInitializerIndentWidth = 4;
19684   verifyFormat(
19685       "SomeClass::Constructor()\n"
19686       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
19687       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
19688       Style);
19689 
19690   Style.ConstructorInitializerIndentWidth = 2;
19691   verifyFormat(
19692       "SomeClass::Constructor()\n"
19693       "  : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
19694       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
19695       Style);
19696 
19697   Style.ConstructorInitializerIndentWidth = 0;
19698   verifyFormat(
19699       "SomeClass::Constructor()\n"
19700       ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
19701       "  aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
19702       Style);
19703   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
19704   verifyFormat(
19705       "SomeLongTemplateVariableName<\n"
19706       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>",
19707       Style);
19708   verifyFormat("bool smaller = 1 < "
19709                "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
19710                "                       "
19711                "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
19712                Style);
19713 
19714   Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
19715   verifyFormat("SomeClass::Constructor() :\n"
19716                "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa),\n"
19717                "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa) {}",
19718                Style);
19719 }
19720 
19721 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {
19722   FormatStyle Style = getLLVMStyle();
19723   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
19724   Style.ConstructorInitializerIndentWidth = 4;
19725   verifyFormat("SomeClass::Constructor()\n"
19726                "    : a(a)\n"
19727                "    , b(b)\n"
19728                "    , c(c) {}",
19729                Style);
19730   verifyFormat("SomeClass::Constructor()\n"
19731                "    : a(a) {}",
19732                Style);
19733 
19734   Style.ColumnLimit = 0;
19735   verifyFormat("SomeClass::Constructor()\n"
19736                "    : a(a) {}",
19737                Style);
19738   verifyFormat("SomeClass::Constructor() noexcept\n"
19739                "    : a(a) {}",
19740                Style);
19741   verifyFormat("SomeClass::Constructor()\n"
19742                "    : a(a)\n"
19743                "    , b(b)\n"
19744                "    , c(c) {}",
19745                Style);
19746   verifyFormat("SomeClass::Constructor()\n"
19747                "    : a(a) {\n"
19748                "  foo();\n"
19749                "  bar();\n"
19750                "}",
19751                Style);
19752 
19753   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
19754   verifyFormat("SomeClass::Constructor()\n"
19755                "    : a(a)\n"
19756                "    , b(b)\n"
19757                "    , c(c) {\n}",
19758                Style);
19759   verifyFormat("SomeClass::Constructor()\n"
19760                "    : a(a) {\n}",
19761                Style);
19762 
19763   Style.ColumnLimit = 80;
19764   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
19765   Style.ConstructorInitializerIndentWidth = 2;
19766   verifyFormat("SomeClass::Constructor()\n"
19767                "  : a(a)\n"
19768                "  , b(b)\n"
19769                "  , c(c) {}",
19770                Style);
19771 
19772   Style.ConstructorInitializerIndentWidth = 0;
19773   verifyFormat("SomeClass::Constructor()\n"
19774                ": a(a)\n"
19775                ", b(b)\n"
19776                ", c(c) {}",
19777                Style);
19778 
19779   Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
19780   Style.ConstructorInitializerIndentWidth = 4;
19781   verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
19782   verifyFormat(
19783       "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n",
19784       Style);
19785   verifyFormat(
19786       "SomeClass::Constructor()\n"
19787       "    : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
19788       Style);
19789   Style.ConstructorInitializerIndentWidth = 4;
19790   Style.ColumnLimit = 60;
19791   verifyFormat("SomeClass::Constructor()\n"
19792                "    : aaaaaaaa(aaaaaaaa)\n"
19793                "    , aaaaaaaa(aaaaaaaa)\n"
19794                "    , aaaaaaaa(aaaaaaaa) {}",
19795                Style);
19796 }
19797 
19798 TEST_F(FormatTest, ConstructorInitializersWithPreprocessorDirective) {
19799   FormatStyle Style = getLLVMStyle();
19800   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
19801   Style.ConstructorInitializerIndentWidth = 4;
19802   verifyFormat("SomeClass::Constructor()\n"
19803                "    : a{a}\n"
19804                "    , b{b} {}",
19805                Style);
19806   verifyFormat("SomeClass::Constructor()\n"
19807                "    : a{a}\n"
19808                "#if CONDITION\n"
19809                "    , b{b}\n"
19810                "#endif\n"
19811                "{\n}",
19812                Style);
19813   Style.ConstructorInitializerIndentWidth = 2;
19814   verifyFormat("SomeClass::Constructor()\n"
19815                "#if CONDITION\n"
19816                "  : a{a}\n"
19817                "#endif\n"
19818                "  , b{b}\n"
19819                "  , c{c} {\n}",
19820                Style);
19821   Style.ConstructorInitializerIndentWidth = 0;
19822   verifyFormat("SomeClass::Constructor()\n"
19823                ": a{a}\n"
19824                "#ifdef CONDITION\n"
19825                ", b{b}\n"
19826                "#else\n"
19827                ", c{c}\n"
19828                "#endif\n"
19829                ", d{d} {\n}",
19830                Style);
19831   Style.ConstructorInitializerIndentWidth = 4;
19832   verifyFormat("SomeClass::Constructor()\n"
19833                "    : a{a}\n"
19834                "#if WINDOWS\n"
19835                "#if DEBUG\n"
19836                "    , b{0}\n"
19837                "#else\n"
19838                "    , b{1}\n"
19839                "#endif\n"
19840                "#else\n"
19841                "#if DEBUG\n"
19842                "    , b{2}\n"
19843                "#else\n"
19844                "    , b{3}\n"
19845                "#endif\n"
19846                "#endif\n"
19847                "{\n}",
19848                Style);
19849   verifyFormat("SomeClass::Constructor()\n"
19850                "    : a{a}\n"
19851                "#if WINDOWS\n"
19852                "    , b{0}\n"
19853                "#if DEBUG\n"
19854                "    , c{0}\n"
19855                "#else\n"
19856                "    , c{1}\n"
19857                "#endif\n"
19858                "#else\n"
19859                "#if DEBUG\n"
19860                "    , c{2}\n"
19861                "#else\n"
19862                "    , c{3}\n"
19863                "#endif\n"
19864                "    , b{1}\n"
19865                "#endif\n"
19866                "{\n}",
19867                Style);
19868 }
19869 
19870 TEST_F(FormatTest, Destructors) {
19871   verifyFormat("void F(int &i) { i.~int(); }");
19872   verifyFormat("void F(int &i) { i->~int(); }");
19873 }
19874 
19875 TEST_F(FormatTest, FormatsWithWebKitStyle) {
19876   FormatStyle Style = getWebKitStyle();
19877 
19878   // Don't indent in outer namespaces.
19879   verifyFormat("namespace outer {\n"
19880                "int i;\n"
19881                "namespace inner {\n"
19882                "    int i;\n"
19883                "} // namespace inner\n"
19884                "} // namespace outer\n"
19885                "namespace other_outer {\n"
19886                "int i;\n"
19887                "}",
19888                Style);
19889 
19890   // Don't indent case labels.
19891   verifyFormat("switch (variable) {\n"
19892                "case 1:\n"
19893                "case 2:\n"
19894                "    doSomething();\n"
19895                "    break;\n"
19896                "default:\n"
19897                "    ++variable;\n"
19898                "}",
19899                Style);
19900 
19901   // Wrap before binary operators.
19902   EXPECT_EQ("void f()\n"
19903             "{\n"
19904             "    if (aaaaaaaaaaaaaaaa\n"
19905             "        && bbbbbbbbbbbbbbbbbbbbbbbb\n"
19906             "        && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
19907             "        return;\n"
19908             "}",
19909             format("void f() {\n"
19910                    "if (aaaaaaaaaaaaaaaa\n"
19911                    "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
19912                    "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
19913                    "return;\n"
19914                    "}",
19915                    Style));
19916 
19917   // Allow functions on a single line.
19918   verifyFormat("void f() { return; }", Style);
19919 
19920   // Allow empty blocks on a single line and insert a space in empty blocks.
19921   EXPECT_EQ("void f() { }", format("void f() {}", Style));
19922   EXPECT_EQ("while (true) { }", format("while (true) {}", Style));
19923   // However, don't merge non-empty short loops.
19924   EXPECT_EQ("while (true) {\n"
19925             "    continue;\n"
19926             "}",
19927             format("while (true) { continue; }", Style));
19928 
19929   // Constructor initializers are formatted one per line with the "," on the
19930   // new line.
19931   verifyFormat("Constructor()\n"
19932                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
19933                "    , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
19934                "          aaaaaaaaaaaaaa)\n"
19935                "    , aaaaaaaaaaaaaaaaaaaaaaa()\n"
19936                "{\n"
19937                "}",
19938                Style);
19939   verifyFormat("SomeClass::Constructor()\n"
19940                "    : a(a)\n"
19941                "{\n"
19942                "}",
19943                Style);
19944   EXPECT_EQ("SomeClass::Constructor()\n"
19945             "    : a(a)\n"
19946             "{\n"
19947             "}",
19948             format("SomeClass::Constructor():a(a){}", Style));
19949   verifyFormat("SomeClass::Constructor()\n"
19950                "    : a(a)\n"
19951                "    , b(b)\n"
19952                "    , c(c)\n"
19953                "{\n"
19954                "}",
19955                Style);
19956   verifyFormat("SomeClass::Constructor()\n"
19957                "    : a(a)\n"
19958                "{\n"
19959                "    foo();\n"
19960                "    bar();\n"
19961                "}",
19962                Style);
19963 
19964   // Access specifiers should be aligned left.
19965   verifyFormat("class C {\n"
19966                "public:\n"
19967                "    int i;\n"
19968                "};",
19969                Style);
19970 
19971   // Do not align comments.
19972   verifyFormat("int a; // Do not\n"
19973                "double b; // align comments.",
19974                Style);
19975 
19976   // Do not align operands.
19977   EXPECT_EQ("ASSERT(aaaa\n"
19978             "    || bbbb);",
19979             format("ASSERT ( aaaa\n||bbbb);", Style));
19980 
19981   // Accept input's line breaks.
19982   EXPECT_EQ("if (aaaaaaaaaaaaaaa\n"
19983             "    || bbbbbbbbbbbbbbb) {\n"
19984             "    i++;\n"
19985             "}",
19986             format("if (aaaaaaaaaaaaaaa\n"
19987                    "|| bbbbbbbbbbbbbbb) { i++; }",
19988                    Style));
19989   EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
19990             "    i++;\n"
19991             "}",
19992             format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
19993 
19994   // Don't automatically break all macro definitions (llvm.org/PR17842).
19995   verifyFormat("#define aNumber 10", Style);
19996   // However, generally keep the line breaks that the user authored.
19997   EXPECT_EQ("#define aNumber \\\n"
19998             "    10",
19999             format("#define aNumber \\\n"
20000                    " 10",
20001                    Style));
20002 
20003   // Keep empty and one-element array literals on a single line.
20004   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
20005             "                                  copyItems:YES];",
20006             format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
20007                    "copyItems:YES];",
20008                    Style));
20009   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
20010             "                                  copyItems:YES];",
20011             format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
20012                    "             copyItems:YES];",
20013                    Style));
20014   // FIXME: This does not seem right, there should be more indentation before
20015   // the array literal's entries. Nested blocks have the same problem.
20016   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
20017             "    @\"a\",\n"
20018             "    @\"a\"\n"
20019             "]\n"
20020             "                                  copyItems:YES];",
20021             format("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
20022                    "     @\"a\",\n"
20023                    "     @\"a\"\n"
20024                    "     ]\n"
20025                    "       copyItems:YES];",
20026                    Style));
20027   EXPECT_EQ(
20028       "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
20029       "                                  copyItems:YES];",
20030       format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
20031              "   copyItems:YES];",
20032              Style));
20033 
20034   verifyFormat("[self.a b:c c:d];", Style);
20035   EXPECT_EQ("[self.a b:c\n"
20036             "        c:d];",
20037             format("[self.a b:c\n"
20038                    "c:d];",
20039                    Style));
20040 }
20041 
20042 TEST_F(FormatTest, FormatsLambdas) {
20043   verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n");
20044   verifyFormat(
20045       "int c = [b]() mutable noexcept { return [&b] { return b++; }(); }();\n");
20046   verifyFormat("int c = [&] { [=] { return b++; }(); }();\n");
20047   verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n");
20048   verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n");
20049   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n");
20050   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n");
20051   verifyFormat("auto c = [a = [b = 42] {}] {};\n");
20052   verifyFormat("auto c = [a = &i + 10, b = [] {}] {};\n");
20053   verifyFormat("int x = f(*+[] {});");
20054   verifyFormat("void f() {\n"
20055                "  other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
20056                "}\n");
20057   verifyFormat("void f() {\n"
20058                "  other(x.begin(), //\n"
20059                "        x.end(),   //\n"
20060                "        [&](int, int) { return 1; });\n"
20061                "}\n");
20062   verifyFormat("void f() {\n"
20063                "  other.other.other.other.other(\n"
20064                "      x.begin(), x.end(),\n"
20065                "      [something, rather](int, int, int, int, int, int, int) { "
20066                "return 1; });\n"
20067                "}\n");
20068   verifyFormat(
20069       "void f() {\n"
20070       "  other.other.other.other.other(\n"
20071       "      x.begin(), x.end(),\n"
20072       "      [something, rather](int, int, int, int, int, int, int) {\n"
20073       "        //\n"
20074       "      });\n"
20075       "}\n");
20076   verifyFormat("SomeFunction([]() { // A cool function...\n"
20077                "  return 43;\n"
20078                "});");
20079   EXPECT_EQ("SomeFunction([]() {\n"
20080             "#define A a\n"
20081             "  return 43;\n"
20082             "});",
20083             format("SomeFunction([](){\n"
20084                    "#define A a\n"
20085                    "return 43;\n"
20086                    "});"));
20087   verifyFormat("void f() {\n"
20088                "  SomeFunction([](decltype(x), A *a) {});\n"
20089                "  SomeFunction([](typeof(x), A *a) {});\n"
20090                "  SomeFunction([](_Atomic(x), A *a) {});\n"
20091                "  SomeFunction([](__underlying_type(x), A *a) {});\n"
20092                "}");
20093   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
20094                "    [](const aaaaaaaaaa &a) { return a; });");
20095   verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
20096                "  SomeOtherFunctioooooooooooooooooooooooooon();\n"
20097                "});");
20098   verifyFormat("Constructor()\n"
20099                "    : Field([] { // comment\n"
20100                "        int i;\n"
20101                "      }) {}");
20102   verifyFormat("auto my_lambda = [](const string &some_parameter) {\n"
20103                "  return some_parameter.size();\n"
20104                "};");
20105   verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n"
20106                "    [](const string &s) { return s; };");
20107   verifyFormat("int i = aaaaaa ? 1 //\n"
20108                "               : [] {\n"
20109                "                   return 2; //\n"
20110                "                 }();");
20111   verifyFormat("llvm::errs() << \"number of twos is \"\n"
20112                "             << std::count_if(v.begin(), v.end(), [](int x) {\n"
20113                "                  return x == 2; // force break\n"
20114                "                });");
20115   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
20116                "    [=](int iiiiiiiiiiii) {\n"
20117                "      return aaaaaaaaaaaaaaaaaaaaaaa !=\n"
20118                "             aaaaaaaaaaaaaaaaaaaaaaa;\n"
20119                "    });",
20120                getLLVMStyleWithColumns(60));
20121 
20122   verifyFormat("SomeFunction({[&] {\n"
20123                "                // comment\n"
20124                "              },\n"
20125                "              [&] {\n"
20126                "                // comment\n"
20127                "              }});");
20128   verifyFormat("SomeFunction({[&] {\n"
20129                "  // comment\n"
20130                "}});");
20131   verifyFormat(
20132       "virtual aaaaaaaaaaaaaaaa(\n"
20133       "    std::function<bool()> bbbbbbbbbbbb = [&]() { return true; },\n"
20134       "    aaaaa aaaaaaaaa);");
20135 
20136   // Lambdas with return types.
20137   verifyFormat("int c = []() -> int { return 2; }();\n");
20138   verifyFormat("int c = []() -> int * { return 2; }();\n");
20139   verifyFormat("int c = []() -> vector<int> { return {2}; }();\n");
20140   verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
20141   verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
20142   verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
20143   verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
20144   verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
20145   verifyFormat("[a, a]() -> a<1> {};");
20146   verifyFormat("[]() -> foo<5 + 2> { return {}; };");
20147   verifyFormat("[]() -> foo<5 - 2> { return {}; };");
20148   verifyFormat("[]() -> foo<5 / 2> { return {}; };");
20149   verifyFormat("[]() -> foo<5 * 2> { return {}; };");
20150   verifyFormat("[]() -> foo<5 % 2> { return {}; };");
20151   verifyFormat("[]() -> foo<5 << 2> { return {}; };");
20152   verifyFormat("[]() -> foo<!5> { return {}; };");
20153   verifyFormat("[]() -> foo<~5> { return {}; };");
20154   verifyFormat("[]() -> foo<5 | 2> { return {}; };");
20155   verifyFormat("[]() -> foo<5 || 2> { return {}; };");
20156   verifyFormat("[]() -> foo<5 & 2> { return {}; };");
20157   verifyFormat("[]() -> foo<5 && 2> { return {}; };");
20158   verifyFormat("[]() -> foo<5 == 2> { return {}; };");
20159   verifyFormat("[]() -> foo<5 != 2> { return {}; };");
20160   verifyFormat("[]() -> foo<5 >= 2> { return {}; };");
20161   verifyFormat("[]() -> foo<5 <= 2> { return {}; };");
20162   verifyFormat("[]() -> foo<5 < 2> { return {}; };");
20163   verifyFormat("[]() -> foo<2 ? 1 : 0> { return {}; };");
20164   verifyFormat("namespace bar {\n"
20165                "// broken:\n"
20166                "auto foo{[]() -> foo<5 + 2> { return {}; }};\n"
20167                "} // namespace bar");
20168   verifyFormat("namespace bar {\n"
20169                "// broken:\n"
20170                "auto foo{[]() -> foo<5 - 2> { return {}; }};\n"
20171                "} // namespace bar");
20172   verifyFormat("namespace bar {\n"
20173                "// broken:\n"
20174                "auto foo{[]() -> foo<5 / 2> { return {}; }};\n"
20175                "} // namespace bar");
20176   verifyFormat("namespace bar {\n"
20177                "// broken:\n"
20178                "auto foo{[]() -> foo<5 * 2> { return {}; }};\n"
20179                "} // namespace bar");
20180   verifyFormat("namespace bar {\n"
20181                "// broken:\n"
20182                "auto foo{[]() -> foo<5 % 2> { return {}; }};\n"
20183                "} // namespace bar");
20184   verifyFormat("namespace bar {\n"
20185                "// broken:\n"
20186                "auto foo{[]() -> foo<5 << 2> { return {}; }};\n"
20187                "} // namespace bar");
20188   verifyFormat("namespace bar {\n"
20189                "// broken:\n"
20190                "auto foo{[]() -> foo<!5> { return {}; }};\n"
20191                "} // namespace bar");
20192   verifyFormat("namespace bar {\n"
20193                "// broken:\n"
20194                "auto foo{[]() -> foo<~5> { return {}; }};\n"
20195                "} // namespace bar");
20196   verifyFormat("namespace bar {\n"
20197                "// broken:\n"
20198                "auto foo{[]() -> foo<5 | 2> { return {}; }};\n"
20199                "} // namespace bar");
20200   verifyFormat("namespace bar {\n"
20201                "// broken:\n"
20202                "auto foo{[]() -> foo<5 || 2> { return {}; }};\n"
20203                "} // namespace bar");
20204   verifyFormat("namespace bar {\n"
20205                "// broken:\n"
20206                "auto foo{[]() -> foo<5 & 2> { return {}; }};\n"
20207                "} // namespace bar");
20208   verifyFormat("namespace bar {\n"
20209                "// broken:\n"
20210                "auto foo{[]() -> foo<5 && 2> { return {}; }};\n"
20211                "} // namespace bar");
20212   verifyFormat("namespace bar {\n"
20213                "// broken:\n"
20214                "auto foo{[]() -> foo<5 == 2> { return {}; }};\n"
20215                "} // namespace bar");
20216   verifyFormat("namespace bar {\n"
20217                "// broken:\n"
20218                "auto foo{[]() -> foo<5 != 2> { return {}; }};\n"
20219                "} // namespace bar");
20220   verifyFormat("namespace bar {\n"
20221                "// broken:\n"
20222                "auto foo{[]() -> foo<5 >= 2> { return {}; }};\n"
20223                "} // namespace bar");
20224   verifyFormat("namespace bar {\n"
20225                "// broken:\n"
20226                "auto foo{[]() -> foo<5 <= 2> { return {}; }};\n"
20227                "} // namespace bar");
20228   verifyFormat("namespace bar {\n"
20229                "// broken:\n"
20230                "auto foo{[]() -> foo<5 < 2> { return {}; }};\n"
20231                "} // namespace bar");
20232   verifyFormat("namespace bar {\n"
20233                "// broken:\n"
20234                "auto foo{[]() -> foo<2 ? 1 : 0> { return {}; }};\n"
20235                "} // namespace bar");
20236   verifyFormat("[]() -> a<1> {};");
20237   verifyFormat("[]() -> a<1> { ; };");
20238   verifyFormat("[]() -> a<1> { ; }();");
20239   verifyFormat("[a, a]() -> a<true> {};");
20240   verifyFormat("[]() -> a<true> {};");
20241   verifyFormat("[]() -> a<true> { ; };");
20242   verifyFormat("[]() -> a<true> { ; }();");
20243   verifyFormat("[a, a]() -> a<false> {};");
20244   verifyFormat("[]() -> a<false> {};");
20245   verifyFormat("[]() -> a<false> { ; };");
20246   verifyFormat("[]() -> a<false> { ; }();");
20247   verifyFormat("auto foo{[]() -> foo<false> { ; }};");
20248   verifyFormat("namespace bar {\n"
20249                "auto foo{[]() -> foo<false> { ; }};\n"
20250                "} // namespace bar");
20251   verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
20252                "                   int j) -> int {\n"
20253                "  return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
20254                "};");
20255   verifyFormat(
20256       "aaaaaaaaaaaaaaaaaaaaaa(\n"
20257       "    [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n"
20258       "      return aaaaaaaaaaaaaaaaa;\n"
20259       "    });",
20260       getLLVMStyleWithColumns(70));
20261   verifyFormat("[]() //\n"
20262                "    -> int {\n"
20263                "  return 1; //\n"
20264                "};");
20265   verifyFormat("[]() -> Void<T...> {};");
20266   verifyFormat("[a, b]() -> Tuple<T...> { return {}; };");
20267   verifyFormat("SomeFunction({[]() -> int[] { return {}; }});");
20268   verifyFormat("SomeFunction({[]() -> int *[] { return {}; }});");
20269   verifyFormat("SomeFunction({[]() -> int (*)[] { return {}; }});");
20270   verifyFormat("SomeFunction({[]() -> ns::type<int (*)[]> { return {}; }});");
20271   verifyFormat("return int{[x = x]() { return x; }()};");
20272 
20273   // Lambdas with explicit template argument lists.
20274   verifyFormat(
20275       "auto L = []<template <typename> class T, class U>(T<U> &&a) {};\n");
20276 
20277   // Multiple lambdas in the same parentheses change indentation rules. These
20278   // lambdas are forced to start on new lines.
20279   verifyFormat("SomeFunction(\n"
20280                "    []() {\n"
20281                "      //\n"
20282                "    },\n"
20283                "    []() {\n"
20284                "      //\n"
20285                "    });");
20286 
20287   // A lambda passed as arg0 is always pushed to the next line.
20288   verifyFormat("SomeFunction(\n"
20289                "    [this] {\n"
20290                "      //\n"
20291                "    },\n"
20292                "    1);\n");
20293 
20294   // A multi-line lambda passed as arg1 forces arg0 to be pushed out, just like
20295   // the arg0 case above.
20296   auto Style = getGoogleStyle();
20297   Style.BinPackArguments = false;
20298   verifyFormat("SomeFunction(\n"
20299                "    a,\n"
20300                "    [this] {\n"
20301                "      //\n"
20302                "    },\n"
20303                "    b);\n",
20304                Style);
20305   verifyFormat("SomeFunction(\n"
20306                "    a,\n"
20307                "    [this] {\n"
20308                "      //\n"
20309                "    },\n"
20310                "    b);\n");
20311 
20312   // A lambda with a very long line forces arg0 to be pushed out irrespective of
20313   // the BinPackArguments value (as long as the code is wide enough).
20314   verifyFormat(
20315       "something->SomeFunction(\n"
20316       "    a,\n"
20317       "    [this] {\n"
20318       "      "
20319       "D0000000000000000000000000000000000000000000000000000000000001();\n"
20320       "    },\n"
20321       "    b);\n");
20322 
20323   // A multi-line lambda is pulled up as long as the introducer fits on the
20324   // previous line and there are no further args.
20325   verifyFormat("function(1, [this, that] {\n"
20326                "  //\n"
20327                "});\n");
20328   verifyFormat("function([this, that] {\n"
20329                "  //\n"
20330                "});\n");
20331   // FIXME: this format is not ideal and we should consider forcing the first
20332   // arg onto its own line.
20333   verifyFormat("function(a, b, c, //\n"
20334                "         d, [this, that] {\n"
20335                "           //\n"
20336                "         });\n");
20337 
20338   // Multiple lambdas are treated correctly even when there is a short arg0.
20339   verifyFormat("SomeFunction(\n"
20340                "    1,\n"
20341                "    [this] {\n"
20342                "      //\n"
20343                "    },\n"
20344                "    [this] {\n"
20345                "      //\n"
20346                "    },\n"
20347                "    1);\n");
20348 
20349   // More complex introducers.
20350   verifyFormat("return [i, args...] {};");
20351 
20352   // Not lambdas.
20353   verifyFormat("constexpr char hello[]{\"hello\"};");
20354   verifyFormat("double &operator[](int i) { return 0; }\n"
20355                "int i;");
20356   verifyFormat("std::unique_ptr<int[]> foo() {}");
20357   verifyFormat("int i = a[a][a]->f();");
20358   verifyFormat("int i = (*b)[a]->f();");
20359 
20360   // Other corner cases.
20361   verifyFormat("void f() {\n"
20362                "  bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
20363                "  );\n"
20364                "}");
20365 
20366   // Lambdas created through weird macros.
20367   verifyFormat("void f() {\n"
20368                "  MACRO((const AA &a) { return 1; });\n"
20369                "  MACRO((AA &a) { return 1; });\n"
20370                "}");
20371 
20372   verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
20373                "      doo_dah();\n"
20374                "      doo_dah();\n"
20375                "    })) {\n"
20376                "}");
20377   verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n"
20378                "                doo_dah();\n"
20379                "                doo_dah();\n"
20380                "              })) {\n"
20381                "}");
20382   verifyFormat("if CONSTEXPR (blah_blah(whatever, whatever, [] {\n"
20383                "                doo_dah();\n"
20384                "                doo_dah();\n"
20385                "              })) {\n"
20386                "}");
20387   verifyFormat("auto lambda = []() {\n"
20388                "  int a = 2\n"
20389                "#if A\n"
20390                "          + 2\n"
20391                "#endif\n"
20392                "      ;\n"
20393                "};");
20394 
20395   // Lambdas with complex multiline introducers.
20396   verifyFormat(
20397       "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
20398       "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n"
20399       "        -> ::std::unordered_set<\n"
20400       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
20401       "      //\n"
20402       "    });");
20403 
20404   FormatStyle DoNotMerge = getLLVMStyle();
20405   DoNotMerge.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
20406   verifyFormat("auto c = []() {\n"
20407                "  return b;\n"
20408                "};",
20409                "auto c = []() { return b; };", DoNotMerge);
20410   verifyFormat("auto c = []() {\n"
20411                "};",
20412                " auto c = []() {};", DoNotMerge);
20413 
20414   FormatStyle MergeEmptyOnly = getLLVMStyle();
20415   MergeEmptyOnly.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Empty;
20416   verifyFormat("auto c = []() {\n"
20417                "  return b;\n"
20418                "};",
20419                "auto c = []() {\n"
20420                "  return b;\n"
20421                " };",
20422                MergeEmptyOnly);
20423   verifyFormat("auto c = []() {};",
20424                "auto c = []() {\n"
20425                "};",
20426                MergeEmptyOnly);
20427 
20428   FormatStyle MergeInline = getLLVMStyle();
20429   MergeInline.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Inline;
20430   verifyFormat("auto c = []() {\n"
20431                "  return b;\n"
20432                "};",
20433                "auto c = []() { return b; };", MergeInline);
20434   verifyFormat("function([]() { return b; })", "function([]() { return b; })",
20435                MergeInline);
20436   verifyFormat("function([]() { return b; }, a)",
20437                "function([]() { return b; }, a)", MergeInline);
20438   verifyFormat("function(a, []() { return b; })",
20439                "function(a, []() { return b; })", MergeInline);
20440 
20441   // Check option "BraceWrapping.BeforeLambdaBody" and different state of
20442   // AllowShortLambdasOnASingleLine
20443   FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle();
20444   LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
20445   LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
20446   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
20447       FormatStyle::ShortLambdaStyle::SLS_None;
20448   verifyFormat("FctWithOneNestedLambdaInline_SLS_None(\n"
20449                "    []()\n"
20450                "    {\n"
20451                "      return 17;\n"
20452                "    });",
20453                LLVMWithBeforeLambdaBody);
20454   verifyFormat("FctWithOneNestedLambdaEmpty_SLS_None(\n"
20455                "    []()\n"
20456                "    {\n"
20457                "    });",
20458                LLVMWithBeforeLambdaBody);
20459   verifyFormat("auto fct_SLS_None = []()\n"
20460                "{\n"
20461                "  return 17;\n"
20462                "};",
20463                LLVMWithBeforeLambdaBody);
20464   verifyFormat("TwoNestedLambdas_SLS_None(\n"
20465                "    []()\n"
20466                "    {\n"
20467                "      return Call(\n"
20468                "          []()\n"
20469                "          {\n"
20470                "            return 17;\n"
20471                "          });\n"
20472                "    });",
20473                LLVMWithBeforeLambdaBody);
20474   verifyFormat("void Fct() {\n"
20475                "  return {[]()\n"
20476                "          {\n"
20477                "            return 17;\n"
20478                "          }};\n"
20479                "}",
20480                LLVMWithBeforeLambdaBody);
20481 
20482   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
20483       FormatStyle::ShortLambdaStyle::SLS_Empty;
20484   verifyFormat("FctWithOneNestedLambdaInline_SLS_Empty(\n"
20485                "    []()\n"
20486                "    {\n"
20487                "      return 17;\n"
20488                "    });",
20489                LLVMWithBeforeLambdaBody);
20490   verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Empty([]() {});",
20491                LLVMWithBeforeLambdaBody);
20492   verifyFormat("FctWithOneNestedLambdaEmptyInsideAVeryVeryVeryVeryVeryVeryVeryL"
20493                "ongFunctionName_SLS_Empty(\n"
20494                "    []() {});",
20495                LLVMWithBeforeLambdaBody);
20496   verifyFormat("FctWithMultipleParams_SLS_Empty(A, B,\n"
20497                "                                []()\n"
20498                "                                {\n"
20499                "                                  return 17;\n"
20500                "                                });",
20501                LLVMWithBeforeLambdaBody);
20502   verifyFormat("auto fct_SLS_Empty = []()\n"
20503                "{\n"
20504                "  return 17;\n"
20505                "};",
20506                LLVMWithBeforeLambdaBody);
20507   verifyFormat("TwoNestedLambdas_SLS_Empty(\n"
20508                "    []()\n"
20509                "    {\n"
20510                "      return Call([]() {});\n"
20511                "    });",
20512                LLVMWithBeforeLambdaBody);
20513   verifyFormat("TwoNestedLambdas_SLS_Empty(A,\n"
20514                "                           []()\n"
20515                "                           {\n"
20516                "                             return Call([]() {});\n"
20517                "                           });",
20518                LLVMWithBeforeLambdaBody);
20519   verifyFormat(
20520       "FctWithLongLineInLambda_SLS_Empty(\n"
20521       "    []()\n"
20522       "    {\n"
20523       "      return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
20524       "                               AndShouldNotBeConsiderAsInline,\n"
20525       "                               LambdaBodyMustBeBreak);\n"
20526       "    });",
20527       LLVMWithBeforeLambdaBody);
20528 
20529   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
20530       FormatStyle::ShortLambdaStyle::SLS_Inline;
20531   verifyFormat("FctWithOneNestedLambdaInline_SLS_Inline([]() { return 17; });",
20532                LLVMWithBeforeLambdaBody);
20533   verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Inline([]() {});",
20534                LLVMWithBeforeLambdaBody);
20535   verifyFormat("auto fct_SLS_Inline = []()\n"
20536                "{\n"
20537                "  return 17;\n"
20538                "};",
20539                LLVMWithBeforeLambdaBody);
20540   verifyFormat("TwoNestedLambdas_SLS_Inline([]() { return Call([]() { return "
20541                "17; }); });",
20542                LLVMWithBeforeLambdaBody);
20543   verifyFormat(
20544       "FctWithLongLineInLambda_SLS_Inline(\n"
20545       "    []()\n"
20546       "    {\n"
20547       "      return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
20548       "                               AndShouldNotBeConsiderAsInline,\n"
20549       "                               LambdaBodyMustBeBreak);\n"
20550       "    });",
20551       LLVMWithBeforeLambdaBody);
20552   verifyFormat("FctWithMultipleParams_SLS_Inline("
20553                "VeryLongParameterThatShouldAskToBeOnMultiLine,\n"
20554                "                                 []() { return 17; });",
20555                LLVMWithBeforeLambdaBody);
20556   verifyFormat(
20557       "FctWithMultipleParams_SLS_Inline(FirstParam, []() { return 17; });",
20558       LLVMWithBeforeLambdaBody);
20559 
20560   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
20561       FormatStyle::ShortLambdaStyle::SLS_All;
20562   verifyFormat("FctWithOneNestedLambdaInline_SLS_All([]() { return 17; });",
20563                LLVMWithBeforeLambdaBody);
20564   verifyFormat("FctWithOneNestedLambdaEmpty_SLS_All([]() {});",
20565                LLVMWithBeforeLambdaBody);
20566   verifyFormat("auto fct_SLS_All = []() { return 17; };",
20567                LLVMWithBeforeLambdaBody);
20568   verifyFormat("FctWithOneParam_SLS_All(\n"
20569                "    []()\n"
20570                "    {\n"
20571                "      // A cool function...\n"
20572                "      return 43;\n"
20573                "    });",
20574                LLVMWithBeforeLambdaBody);
20575   verifyFormat("FctWithMultipleParams_SLS_All("
20576                "VeryLongParameterThatShouldAskToBeOnMultiLine,\n"
20577                "                              []() { return 17; });",
20578                LLVMWithBeforeLambdaBody);
20579   verifyFormat("FctWithMultipleParams_SLS_All(A, []() { return 17; });",
20580                LLVMWithBeforeLambdaBody);
20581   verifyFormat("FctWithMultipleParams_SLS_All(A, B, []() { return 17; });",
20582                LLVMWithBeforeLambdaBody);
20583   verifyFormat(
20584       "FctWithLongLineInLambda_SLS_All(\n"
20585       "    []()\n"
20586       "    {\n"
20587       "      return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
20588       "                               AndShouldNotBeConsiderAsInline,\n"
20589       "                               LambdaBodyMustBeBreak);\n"
20590       "    });",
20591       LLVMWithBeforeLambdaBody);
20592   verifyFormat(
20593       "auto fct_SLS_All = []()\n"
20594       "{\n"
20595       "  return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
20596       "                           AndShouldNotBeConsiderAsInline,\n"
20597       "                           LambdaBodyMustBeBreak);\n"
20598       "};",
20599       LLVMWithBeforeLambdaBody);
20600   LLVMWithBeforeLambdaBody.BinPackParameters = false;
20601   verifyFormat("FctAllOnSameLine_SLS_All([]() { return S; }, Fst, Second);",
20602                LLVMWithBeforeLambdaBody);
20603   verifyFormat(
20604       "FctWithLongLineInLambda_SLS_All([]() { return SomeValueNotSoLong; },\n"
20605       "                                FirstParam,\n"
20606       "                                SecondParam,\n"
20607       "                                ThirdParam,\n"
20608       "                                FourthParam);",
20609       LLVMWithBeforeLambdaBody);
20610   verifyFormat("FctWithLongLineInLambda_SLS_All(\n"
20611                "    []() { return "
20612                "SomeValueVeryVeryVeryVeryVeryVeryVeryVeryVeryLong; },\n"
20613                "    FirstParam,\n"
20614                "    SecondParam,\n"
20615                "    ThirdParam,\n"
20616                "    FourthParam);",
20617                LLVMWithBeforeLambdaBody);
20618   verifyFormat(
20619       "FctWithLongLineInLambda_SLS_All(FirstParam,\n"
20620       "                                SecondParam,\n"
20621       "                                ThirdParam,\n"
20622       "                                FourthParam,\n"
20623       "                                []() { return SomeValueNotSoLong; });",
20624       LLVMWithBeforeLambdaBody);
20625   verifyFormat("FctWithLongLineInLambda_SLS_All(\n"
20626                "    []()\n"
20627                "    {\n"
20628                "      return "
20629                "HereAVeryLongLineThatWillBeFormattedOnMultipleLineAndShouldNotB"
20630                "eConsiderAsInline;\n"
20631                "    });",
20632                LLVMWithBeforeLambdaBody);
20633   verifyFormat(
20634       "FctWithLongLineInLambda_SLS_All(\n"
20635       "    []()\n"
20636       "    {\n"
20637       "      return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
20638       "                               AndShouldNotBeConsiderAsInline,\n"
20639       "                               LambdaBodyMustBeBreak);\n"
20640       "    });",
20641       LLVMWithBeforeLambdaBody);
20642   verifyFormat("FctWithTwoParams_SLS_All(\n"
20643                "    []()\n"
20644                "    {\n"
20645                "      // A cool function...\n"
20646                "      return 43;\n"
20647                "    },\n"
20648                "    87);",
20649                LLVMWithBeforeLambdaBody);
20650   verifyFormat("FctWithTwoParams_SLS_All([]() { return 43; }, 87);",
20651                LLVMWithBeforeLambdaBody);
20652   verifyFormat("FctWithOneNestedLambdas_SLS_All([]() { return 17; });",
20653                LLVMWithBeforeLambdaBody);
20654   verifyFormat(
20655       "TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; }); });",
20656       LLVMWithBeforeLambdaBody);
20657   verifyFormat("TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; "
20658                "}); }, x);",
20659                LLVMWithBeforeLambdaBody);
20660   verifyFormat("TwoNestedLambdas_SLS_All(\n"
20661                "    []()\n"
20662                "    {\n"
20663                "      // A cool function...\n"
20664                "      return Call([]() { return 17; });\n"
20665                "    });",
20666                LLVMWithBeforeLambdaBody);
20667   verifyFormat("TwoNestedLambdas_SLS_All(\n"
20668                "    []()\n"
20669                "    {\n"
20670                "      return Call(\n"
20671                "          []()\n"
20672                "          {\n"
20673                "            // A cool function...\n"
20674                "            return 17;\n"
20675                "          });\n"
20676                "    });",
20677                LLVMWithBeforeLambdaBody);
20678 
20679   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
20680       FormatStyle::ShortLambdaStyle::SLS_None;
20681 
20682   verifyFormat("auto select = [this]() -> const Library::Object *\n"
20683                "{\n"
20684                "  return MyAssignment::SelectFromList(this);\n"
20685                "};\n",
20686                LLVMWithBeforeLambdaBody);
20687 
20688   verifyFormat("auto select = [this]() -> const Library::Object &\n"
20689                "{\n"
20690                "  return MyAssignment::SelectFromList(this);\n"
20691                "};\n",
20692                LLVMWithBeforeLambdaBody);
20693 
20694   verifyFormat("auto select = [this]() -> std::unique_ptr<Object>\n"
20695                "{\n"
20696                "  return MyAssignment::SelectFromList(this);\n"
20697                "};\n",
20698                LLVMWithBeforeLambdaBody);
20699 
20700   verifyFormat("namespace test {\n"
20701                "class Test {\n"
20702                "public:\n"
20703                "  Test() = default;\n"
20704                "};\n"
20705                "} // namespace test",
20706                LLVMWithBeforeLambdaBody);
20707 
20708   // Lambdas with different indentation styles.
20709   Style = getLLVMStyleWithColumns(100);
20710   EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n"
20711             "  return promise.then(\n"
20712             "      [this, &someVariable, someObject = "
20713             "std::mv(s)](std::vector<int> evaluated) mutable {\n"
20714             "        return someObject.startAsyncAction().then(\n"
20715             "            [this, &someVariable](AsyncActionResult result) "
20716             "mutable { result.processMore(); });\n"
20717             "      });\n"
20718             "}\n",
20719             format("SomeResult doSomething(SomeObject promise) {\n"
20720                    "  return promise.then([this, &someVariable, someObject = "
20721                    "std::mv(s)](std::vector<int> evaluated) mutable {\n"
20722                    "    return someObject.startAsyncAction().then([this, "
20723                    "&someVariable](AsyncActionResult result) mutable {\n"
20724                    "      result.processMore();\n"
20725                    "    });\n"
20726                    "  });\n"
20727                    "}\n",
20728                    Style));
20729   Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope;
20730   verifyFormat("test() {\n"
20731                "  ([]() -> {\n"
20732                "    int b = 32;\n"
20733                "    return 3;\n"
20734                "  }).foo();\n"
20735                "}",
20736                Style);
20737   verifyFormat("test() {\n"
20738                "  []() -> {\n"
20739                "    int b = 32;\n"
20740                "    return 3;\n"
20741                "  }\n"
20742                "}",
20743                Style);
20744   verifyFormat("std::sort(v.begin(), v.end(),\n"
20745                "          [](const auto &someLongArgumentName, const auto "
20746                "&someOtherLongArgumentName) {\n"
20747                "  return someLongArgumentName.someMemberVariable < "
20748                "someOtherLongArgumentName.someMemberVariable;\n"
20749                "});",
20750                Style);
20751   verifyFormat("test() {\n"
20752                "  (\n"
20753                "      []() -> {\n"
20754                "        int b = 32;\n"
20755                "        return 3;\n"
20756                "      },\n"
20757                "      foo, bar)\n"
20758                "      .foo();\n"
20759                "}",
20760                Style);
20761   verifyFormat("test() {\n"
20762                "  ([]() -> {\n"
20763                "    int b = 32;\n"
20764                "    return 3;\n"
20765                "  })\n"
20766                "      .foo()\n"
20767                "      .bar();\n"
20768                "}",
20769                Style);
20770   EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n"
20771             "  return promise.then(\n"
20772             "      [this, &someVariable, someObject = "
20773             "std::mv(s)](std::vector<int> evaluated) mutable {\n"
20774             "    return someObject.startAsyncAction().then(\n"
20775             "        [this, &someVariable](AsyncActionResult result) mutable { "
20776             "result.processMore(); });\n"
20777             "  });\n"
20778             "}\n",
20779             format("SomeResult doSomething(SomeObject promise) {\n"
20780                    "  return promise.then([this, &someVariable, someObject = "
20781                    "std::mv(s)](std::vector<int> evaluated) mutable {\n"
20782                    "    return someObject.startAsyncAction().then([this, "
20783                    "&someVariable](AsyncActionResult result) mutable {\n"
20784                    "      result.processMore();\n"
20785                    "    });\n"
20786                    "  });\n"
20787                    "}\n",
20788                    Style));
20789   EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n"
20790             "  return promise.then([this, &someVariable] {\n"
20791             "    return someObject.startAsyncAction().then(\n"
20792             "        [this, &someVariable](AsyncActionResult result) mutable { "
20793             "result.processMore(); });\n"
20794             "  });\n"
20795             "}\n",
20796             format("SomeResult doSomething(SomeObject promise) {\n"
20797                    "  return promise.then([this, &someVariable] {\n"
20798                    "    return someObject.startAsyncAction().then([this, "
20799                    "&someVariable](AsyncActionResult result) mutable {\n"
20800                    "      result.processMore();\n"
20801                    "    });\n"
20802                    "  });\n"
20803                    "}\n",
20804                    Style));
20805   Style = getGoogleStyle();
20806   Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope;
20807   EXPECT_EQ("#define A                                       \\\n"
20808             "  [] {                                          \\\n"
20809             "    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(        \\\n"
20810             "        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
20811             "      }",
20812             format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
20813                    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
20814                    Style));
20815   // TODO: The current formatting has a minor issue that's not worth fixing
20816   // right now whereby the closing brace is indented relative to the signature
20817   // instead of being aligned. This only happens with macros.
20818 }
20819 
20820 TEST_F(FormatTest, LambdaWithLineComments) {
20821   FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle();
20822   LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
20823   LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
20824   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
20825       FormatStyle::ShortLambdaStyle::SLS_All;
20826 
20827   verifyFormat("auto k = []() { return; }", LLVMWithBeforeLambdaBody);
20828   verifyFormat("auto k = []() // comment\n"
20829                "{ return; }",
20830                LLVMWithBeforeLambdaBody);
20831   verifyFormat("auto k = []() /* comment */ { return; }",
20832                LLVMWithBeforeLambdaBody);
20833   verifyFormat("auto k = []() /* comment */ /* comment */ { return; }",
20834                LLVMWithBeforeLambdaBody);
20835   verifyFormat("auto k = []() // X\n"
20836                "{ return; }",
20837                LLVMWithBeforeLambdaBody);
20838   verifyFormat(
20839       "auto k = []() // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"
20840       "{ return; }",
20841       LLVMWithBeforeLambdaBody);
20842 }
20843 
20844 TEST_F(FormatTest, EmptyLinesInLambdas) {
20845   verifyFormat("auto lambda = []() {\n"
20846                "  x(); //\n"
20847                "};",
20848                "auto lambda = []() {\n"
20849                "\n"
20850                "  x(); //\n"
20851                "\n"
20852                "};");
20853 }
20854 
20855 TEST_F(FormatTest, FormatsBlocks) {
20856   FormatStyle ShortBlocks = getLLVMStyle();
20857   ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
20858   verifyFormat("int (^Block)(int, int);", ShortBlocks);
20859   verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
20860   verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
20861   verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks);
20862   verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks);
20863   verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks);
20864 
20865   verifyFormat("foo(^{ bar(); });", ShortBlocks);
20866   verifyFormat("foo(a, ^{ bar(); });", ShortBlocks);
20867   verifyFormat("{ void (^block)(Object *x); }", ShortBlocks);
20868 
20869   verifyFormat("[operation setCompletionBlock:^{\n"
20870                "  [self onOperationDone];\n"
20871                "}];");
20872   verifyFormat("int i = {[operation setCompletionBlock:^{\n"
20873                "  [self onOperationDone];\n"
20874                "}]};");
20875   verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
20876                "  f();\n"
20877                "}];");
20878   verifyFormat("int a = [operation block:^int(int *i) {\n"
20879                "  return 1;\n"
20880                "}];");
20881   verifyFormat("[myObject doSomethingWith:arg1\n"
20882                "                      aaa:^int(int *a) {\n"
20883                "                        return 1;\n"
20884                "                      }\n"
20885                "                      bbb:f(a * bbbbbbbb)];");
20886 
20887   verifyFormat("[operation setCompletionBlock:^{\n"
20888                "  [self.delegate newDataAvailable];\n"
20889                "}];",
20890                getLLVMStyleWithColumns(60));
20891   verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
20892                "  NSString *path = [self sessionFilePath];\n"
20893                "  if (path) {\n"
20894                "    // ...\n"
20895                "  }\n"
20896                "});");
20897   verifyFormat("[[SessionService sharedService]\n"
20898                "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
20899                "      if (window) {\n"
20900                "        [self windowDidLoad:window];\n"
20901                "      } else {\n"
20902                "        [self errorLoadingWindow];\n"
20903                "      }\n"
20904                "    }];");
20905   verifyFormat("void (^largeBlock)(void) = ^{\n"
20906                "  // ...\n"
20907                "};\n",
20908                getLLVMStyleWithColumns(40));
20909   verifyFormat("[[SessionService sharedService]\n"
20910                "    loadWindowWithCompletionBlock: //\n"
20911                "        ^(SessionWindow *window) {\n"
20912                "          if (window) {\n"
20913                "            [self windowDidLoad:window];\n"
20914                "          } else {\n"
20915                "            [self errorLoadingWindow];\n"
20916                "          }\n"
20917                "        }];",
20918                getLLVMStyleWithColumns(60));
20919   verifyFormat("[myObject doSomethingWith:arg1\n"
20920                "    firstBlock:^(Foo *a) {\n"
20921                "      // ...\n"
20922                "      int i;\n"
20923                "    }\n"
20924                "    secondBlock:^(Bar *b) {\n"
20925                "      // ...\n"
20926                "      int i;\n"
20927                "    }\n"
20928                "    thirdBlock:^Foo(Bar *b) {\n"
20929                "      // ...\n"
20930                "      int i;\n"
20931                "    }];");
20932   verifyFormat("[myObject doSomethingWith:arg1\n"
20933                "               firstBlock:-1\n"
20934                "              secondBlock:^(Bar *b) {\n"
20935                "                // ...\n"
20936                "                int i;\n"
20937                "              }];");
20938 
20939   verifyFormat("f(^{\n"
20940                "  @autoreleasepool {\n"
20941                "    if (a) {\n"
20942                "      g();\n"
20943                "    }\n"
20944                "  }\n"
20945                "});");
20946   verifyFormat("Block b = ^int *(A *a, B *b) {}");
20947   verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n"
20948                "};");
20949 
20950   FormatStyle FourIndent = getLLVMStyle();
20951   FourIndent.ObjCBlockIndentWidth = 4;
20952   verifyFormat("[operation setCompletionBlock:^{\n"
20953                "    [self onOperationDone];\n"
20954                "}];",
20955                FourIndent);
20956 }
20957 
20958 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) {
20959   FormatStyle ZeroColumn = getLLVMStyleWithColumns(0);
20960 
20961   verifyFormat("[[SessionService sharedService] "
20962                "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
20963                "  if (window) {\n"
20964                "    [self windowDidLoad:window];\n"
20965                "  } else {\n"
20966                "    [self errorLoadingWindow];\n"
20967                "  }\n"
20968                "}];",
20969                ZeroColumn);
20970   EXPECT_EQ("[[SessionService sharedService]\n"
20971             "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
20972             "      if (window) {\n"
20973             "        [self windowDidLoad:window];\n"
20974             "      } else {\n"
20975             "        [self errorLoadingWindow];\n"
20976             "      }\n"
20977             "    }];",
20978             format("[[SessionService sharedService]\n"
20979                    "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
20980                    "                if (window) {\n"
20981                    "    [self windowDidLoad:window];\n"
20982                    "  } else {\n"
20983                    "    [self errorLoadingWindow];\n"
20984                    "  }\n"
20985                    "}];",
20986                    ZeroColumn));
20987   verifyFormat("[myObject doSomethingWith:arg1\n"
20988                "    firstBlock:^(Foo *a) {\n"
20989                "      // ...\n"
20990                "      int i;\n"
20991                "    }\n"
20992                "    secondBlock:^(Bar *b) {\n"
20993                "      // ...\n"
20994                "      int i;\n"
20995                "    }\n"
20996                "    thirdBlock:^Foo(Bar *b) {\n"
20997                "      // ...\n"
20998                "      int i;\n"
20999                "    }];",
21000                ZeroColumn);
21001   verifyFormat("f(^{\n"
21002                "  @autoreleasepool {\n"
21003                "    if (a) {\n"
21004                "      g();\n"
21005                "    }\n"
21006                "  }\n"
21007                "});",
21008                ZeroColumn);
21009   verifyFormat("void (^largeBlock)(void) = ^{\n"
21010                "  // ...\n"
21011                "};",
21012                ZeroColumn);
21013 
21014   ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
21015   EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };",
21016             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
21017   ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
21018   EXPECT_EQ("void (^largeBlock)(void) = ^{\n"
21019             "  int i;\n"
21020             "};",
21021             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
21022 }
21023 
21024 TEST_F(FormatTest, SupportsCRLF) {
21025   EXPECT_EQ("int a;\r\n"
21026             "int b;\r\n"
21027             "int c;\r\n",
21028             format("int a;\r\n"
21029                    "  int b;\r\n"
21030                    "    int c;\r\n",
21031                    getLLVMStyle()));
21032   EXPECT_EQ("int a;\r\n"
21033             "int b;\r\n"
21034             "int c;\r\n",
21035             format("int a;\r\n"
21036                    "  int b;\n"
21037                    "    int c;\r\n",
21038                    getLLVMStyle()));
21039   EXPECT_EQ("int a;\n"
21040             "int b;\n"
21041             "int c;\n",
21042             format("int a;\r\n"
21043                    "  int b;\n"
21044                    "    int c;\n",
21045                    getLLVMStyle()));
21046   EXPECT_EQ("\"aaaaaaa \"\r\n"
21047             "\"bbbbbbb\";\r\n",
21048             format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
21049   EXPECT_EQ("#define A \\\r\n"
21050             "  b;      \\\r\n"
21051             "  c;      \\\r\n"
21052             "  d;\r\n",
21053             format("#define A \\\r\n"
21054                    "  b; \\\r\n"
21055                    "  c; d; \r\n",
21056                    getGoogleStyle()));
21057 
21058   EXPECT_EQ("/*\r\n"
21059             "multi line block comments\r\n"
21060             "should not introduce\r\n"
21061             "an extra carriage return\r\n"
21062             "*/\r\n",
21063             format("/*\r\n"
21064                    "multi line block comments\r\n"
21065                    "should not introduce\r\n"
21066                    "an extra carriage return\r\n"
21067                    "*/\r\n"));
21068   EXPECT_EQ("/*\r\n"
21069             "\r\n"
21070             "*/",
21071             format("/*\r\n"
21072                    "    \r\r\r\n"
21073                    "*/"));
21074 
21075   FormatStyle style = getLLVMStyle();
21076 
21077   style.DeriveLineEnding = true;
21078   style.UseCRLF = false;
21079   EXPECT_EQ("union FooBarBazQux {\n"
21080             "  int foo;\n"
21081             "  int bar;\n"
21082             "  int baz;\n"
21083             "};",
21084             format("union FooBarBazQux {\r\n"
21085                    "  int foo;\n"
21086                    "  int bar;\r\n"
21087                    "  int baz;\n"
21088                    "};",
21089                    style));
21090   style.UseCRLF = true;
21091   EXPECT_EQ("union FooBarBazQux {\r\n"
21092             "  int foo;\r\n"
21093             "  int bar;\r\n"
21094             "  int baz;\r\n"
21095             "};",
21096             format("union FooBarBazQux {\r\n"
21097                    "  int foo;\n"
21098                    "  int bar;\r\n"
21099                    "  int baz;\n"
21100                    "};",
21101                    style));
21102 
21103   style.DeriveLineEnding = false;
21104   style.UseCRLF = false;
21105   EXPECT_EQ("union FooBarBazQux {\n"
21106             "  int foo;\n"
21107             "  int bar;\n"
21108             "  int baz;\n"
21109             "  int qux;\n"
21110             "};",
21111             format("union FooBarBazQux {\r\n"
21112                    "  int foo;\n"
21113                    "  int bar;\r\n"
21114                    "  int baz;\n"
21115                    "  int qux;\r\n"
21116                    "};",
21117                    style));
21118   style.UseCRLF = true;
21119   EXPECT_EQ("union FooBarBazQux {\r\n"
21120             "  int foo;\r\n"
21121             "  int bar;\r\n"
21122             "  int baz;\r\n"
21123             "  int qux;\r\n"
21124             "};",
21125             format("union FooBarBazQux {\r\n"
21126                    "  int foo;\n"
21127                    "  int bar;\r\n"
21128                    "  int baz;\n"
21129                    "  int qux;\n"
21130                    "};",
21131                    style));
21132 
21133   style.DeriveLineEnding = true;
21134   style.UseCRLF = false;
21135   EXPECT_EQ("union FooBarBazQux {\r\n"
21136             "  int foo;\r\n"
21137             "  int bar;\r\n"
21138             "  int baz;\r\n"
21139             "  int qux;\r\n"
21140             "};",
21141             format("union FooBarBazQux {\r\n"
21142                    "  int foo;\n"
21143                    "  int bar;\r\n"
21144                    "  int baz;\n"
21145                    "  int qux;\r\n"
21146                    "};",
21147                    style));
21148   style.UseCRLF = true;
21149   EXPECT_EQ("union FooBarBazQux {\n"
21150             "  int foo;\n"
21151             "  int bar;\n"
21152             "  int baz;\n"
21153             "  int qux;\n"
21154             "};",
21155             format("union FooBarBazQux {\r\n"
21156                    "  int foo;\n"
21157                    "  int bar;\r\n"
21158                    "  int baz;\n"
21159                    "  int qux;\n"
21160                    "};",
21161                    style));
21162 }
21163 
21164 TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
21165   verifyFormat("MY_CLASS(C) {\n"
21166                "  int i;\n"
21167                "  int j;\n"
21168                "};");
21169 }
21170 
21171 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
21172   FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
21173   TwoIndent.ContinuationIndentWidth = 2;
21174 
21175   EXPECT_EQ("int i =\n"
21176             "  longFunction(\n"
21177             "    arg);",
21178             format("int i = longFunction(arg);", TwoIndent));
21179 
21180   FormatStyle SixIndent = getLLVMStyleWithColumns(20);
21181   SixIndent.ContinuationIndentWidth = 6;
21182 
21183   EXPECT_EQ("int i =\n"
21184             "      longFunction(\n"
21185             "            arg);",
21186             format("int i = longFunction(arg);", SixIndent));
21187 }
21188 
21189 TEST_F(FormatTest, WrappedClosingParenthesisIndent) {
21190   FormatStyle Style = getLLVMStyle();
21191   verifyFormat("int Foo::getter(\n"
21192                "    //\n"
21193                ") const {\n"
21194                "  return foo;\n"
21195                "}",
21196                Style);
21197   verifyFormat("void Foo::setter(\n"
21198                "    //\n"
21199                ") {\n"
21200                "  foo = 1;\n"
21201                "}",
21202                Style);
21203 }
21204 
21205 TEST_F(FormatTest, SpacesInAngles) {
21206   FormatStyle Spaces = getLLVMStyle();
21207   Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
21208 
21209   verifyFormat("vector< ::std::string > x1;", Spaces);
21210   verifyFormat("Foo< int, Bar > x2;", Spaces);
21211   verifyFormat("Foo< ::int, ::Bar > x3;", Spaces);
21212 
21213   verifyFormat("static_cast< int >(arg);", Spaces);
21214   verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
21215   verifyFormat("f< int, float >();", Spaces);
21216   verifyFormat("template <> g() {}", Spaces);
21217   verifyFormat("template < std::vector< int > > f() {}", Spaces);
21218   verifyFormat("std::function< void(int, int) > fct;", Spaces);
21219   verifyFormat("void inFunction() { std::function< void(int, int) > fct; }",
21220                Spaces);
21221 
21222   Spaces.Standard = FormatStyle::LS_Cpp03;
21223   Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
21224   verifyFormat("A< A< int > >();", Spaces);
21225 
21226   Spaces.SpacesInAngles = FormatStyle::SIAS_Never;
21227   verifyFormat("A<A<int> >();", Spaces);
21228 
21229   Spaces.SpacesInAngles = FormatStyle::SIAS_Leave;
21230   verifyFormat("vector< ::std::string> x4;", "vector<::std::string> x4;",
21231                Spaces);
21232   verifyFormat("vector< ::std::string > x4;", "vector<::std::string > x4;",
21233                Spaces);
21234 
21235   verifyFormat("A<A<int> >();", Spaces);
21236   verifyFormat("A<A<int> >();", "A<A<int>>();", Spaces);
21237   verifyFormat("A< A< int > >();", Spaces);
21238 
21239   Spaces.Standard = FormatStyle::LS_Cpp11;
21240   Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
21241   verifyFormat("A< A< int > >();", Spaces);
21242 
21243   Spaces.SpacesInAngles = FormatStyle::SIAS_Never;
21244   verifyFormat("vector<::std::string> x4;", Spaces);
21245   verifyFormat("vector<int> x5;", Spaces);
21246   verifyFormat("Foo<int, Bar> x6;", Spaces);
21247   verifyFormat("Foo<::int, ::Bar> x7;", Spaces);
21248 
21249   verifyFormat("A<A<int>>();", Spaces);
21250 
21251   Spaces.SpacesInAngles = FormatStyle::SIAS_Leave;
21252   verifyFormat("vector<::std::string> x4;", Spaces);
21253   verifyFormat("vector< ::std::string > x4;", Spaces);
21254   verifyFormat("vector<int> x5;", Spaces);
21255   verifyFormat("vector< int > x5;", Spaces);
21256   verifyFormat("Foo<int, Bar> x6;", Spaces);
21257   verifyFormat("Foo< int, Bar > x6;", Spaces);
21258   verifyFormat("Foo<::int, ::Bar> x7;", Spaces);
21259   verifyFormat("Foo< ::int, ::Bar > x7;", Spaces);
21260 
21261   verifyFormat("A<A<int>>();", Spaces);
21262   verifyFormat("A< A< int > >();", Spaces);
21263   verifyFormat("A<A<int > >();", Spaces);
21264   verifyFormat("A< A< int>>();", Spaces);
21265 
21266   Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
21267   verifyFormat("// clang-format off\n"
21268                "foo<<<1, 1>>>();\n"
21269                "// clang-format on\n",
21270                Spaces);
21271   verifyFormat("// clang-format off\n"
21272                "foo< < <1, 1> > >();\n"
21273                "// clang-format on\n",
21274                Spaces);
21275 }
21276 
21277 TEST_F(FormatTest, SpaceAfterTemplateKeyword) {
21278   FormatStyle Style = getLLVMStyle();
21279   Style.SpaceAfterTemplateKeyword = false;
21280   verifyFormat("template<int> void foo();", Style);
21281 }
21282 
21283 TEST_F(FormatTest, TripleAngleBrackets) {
21284   verifyFormat("f<<<1, 1>>>();");
21285   verifyFormat("f<<<1, 1, 1, s>>>();");
21286   verifyFormat("f<<<a, b, c, d>>>();");
21287   EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();"));
21288   verifyFormat("f<param><<<1, 1>>>();");
21289   verifyFormat("f<1><<<1, 1>>>();");
21290   EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();"));
21291   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
21292                "aaaaaaaaaaa<<<\n    1, 1>>>();");
21293   verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n"
21294                "    <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();");
21295 }
21296 
21297 TEST_F(FormatTest, MergeLessLessAtEnd) {
21298   verifyFormat("<<");
21299   EXPECT_EQ("< < <", format("\\\n<<<"));
21300   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
21301                "aaallvm::outs() <<");
21302   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
21303                "aaaallvm::outs()\n    <<");
21304 }
21305 
21306 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
21307   std::string code = "#if A\n"
21308                      "#if B\n"
21309                      "a.\n"
21310                      "#endif\n"
21311                      "    a = 1;\n"
21312                      "#else\n"
21313                      "#endif\n"
21314                      "#if C\n"
21315                      "#else\n"
21316                      "#endif\n";
21317   EXPECT_EQ(code, format(code));
21318 }
21319 
21320 TEST_F(FormatTest, HandleConflictMarkers) {
21321   // Git/SVN conflict markers.
21322   EXPECT_EQ("int a;\n"
21323             "void f() {\n"
21324             "  callme(some(parameter1,\n"
21325             "<<<<<<< text by the vcs\n"
21326             "              parameter2),\n"
21327             "||||||| text by the vcs\n"
21328             "              parameter2),\n"
21329             "         parameter3,\n"
21330             "======= text by the vcs\n"
21331             "              parameter2, parameter3),\n"
21332             ">>>>>>> text by the vcs\n"
21333             "         otherparameter);\n",
21334             format("int a;\n"
21335                    "void f() {\n"
21336                    "  callme(some(parameter1,\n"
21337                    "<<<<<<< text by the vcs\n"
21338                    "  parameter2),\n"
21339                    "||||||| text by the vcs\n"
21340                    "  parameter2),\n"
21341                    "  parameter3,\n"
21342                    "======= text by the vcs\n"
21343                    "  parameter2,\n"
21344                    "  parameter3),\n"
21345                    ">>>>>>> text by the vcs\n"
21346                    "  otherparameter);\n"));
21347 
21348   // Perforce markers.
21349   EXPECT_EQ("void f() {\n"
21350             "  function(\n"
21351             ">>>> text by the vcs\n"
21352             "      parameter,\n"
21353             "==== text by the vcs\n"
21354             "      parameter,\n"
21355             "==== text by the vcs\n"
21356             "      parameter,\n"
21357             "<<<< text by the vcs\n"
21358             "      parameter);\n",
21359             format("void f() {\n"
21360                    "  function(\n"
21361                    ">>>> text by the vcs\n"
21362                    "  parameter,\n"
21363                    "==== text by the vcs\n"
21364                    "  parameter,\n"
21365                    "==== text by the vcs\n"
21366                    "  parameter,\n"
21367                    "<<<< text by the vcs\n"
21368                    "  parameter);\n"));
21369 
21370   EXPECT_EQ("<<<<<<<\n"
21371             "|||||||\n"
21372             "=======\n"
21373             ">>>>>>>",
21374             format("<<<<<<<\n"
21375                    "|||||||\n"
21376                    "=======\n"
21377                    ">>>>>>>"));
21378 
21379   EXPECT_EQ("<<<<<<<\n"
21380             "|||||||\n"
21381             "int i;\n"
21382             "=======\n"
21383             ">>>>>>>",
21384             format("<<<<<<<\n"
21385                    "|||||||\n"
21386                    "int i;\n"
21387                    "=======\n"
21388                    ">>>>>>>"));
21389 
21390   // FIXME: Handle parsing of macros around conflict markers correctly:
21391   EXPECT_EQ("#define Macro \\\n"
21392             "<<<<<<<\n"
21393             "Something \\\n"
21394             "|||||||\n"
21395             "Else \\\n"
21396             "=======\n"
21397             "Other \\\n"
21398             ">>>>>>>\n"
21399             "    End int i;\n",
21400             format("#define Macro \\\n"
21401                    "<<<<<<<\n"
21402                    "  Something \\\n"
21403                    "|||||||\n"
21404                    "  Else \\\n"
21405                    "=======\n"
21406                    "  Other \\\n"
21407                    ">>>>>>>\n"
21408                    "  End\n"
21409                    "int i;\n"));
21410 
21411   verifyFormat(R"(====
21412 #ifdef A
21413 a
21414 #else
21415 b
21416 #endif
21417 )");
21418 }
21419 
21420 TEST_F(FormatTest, DisableRegions) {
21421   EXPECT_EQ("int i;\n"
21422             "// clang-format off\n"
21423             "  int j;\n"
21424             "// clang-format on\n"
21425             "int k;",
21426             format(" int  i;\n"
21427                    "   // clang-format off\n"
21428                    "  int j;\n"
21429                    " // clang-format on\n"
21430                    "   int   k;"));
21431   EXPECT_EQ("int i;\n"
21432             "/* clang-format off */\n"
21433             "  int j;\n"
21434             "/* clang-format on */\n"
21435             "int k;",
21436             format(" int  i;\n"
21437                    "   /* clang-format off */\n"
21438                    "  int j;\n"
21439                    " /* clang-format on */\n"
21440                    "   int   k;"));
21441 
21442   // Don't reflow comments within disabled regions.
21443   EXPECT_EQ("// clang-format off\n"
21444             "// long long long long long long line\n"
21445             "/* clang-format on */\n"
21446             "/* long long long\n"
21447             " * long long long\n"
21448             " * line */\n"
21449             "int i;\n"
21450             "/* clang-format off */\n"
21451             "/* long long long long long long line */\n",
21452             format("// clang-format off\n"
21453                    "// long long long long long long line\n"
21454                    "/* clang-format on */\n"
21455                    "/* long long long long long long line */\n"
21456                    "int i;\n"
21457                    "/* clang-format off */\n"
21458                    "/* long long long long long long line */\n",
21459                    getLLVMStyleWithColumns(20)));
21460 }
21461 
21462 TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
21463   format("? ) =");
21464   verifyNoCrash("#define a\\\n /**/}");
21465 }
21466 
21467 TEST_F(FormatTest, FormatsTableGenCode) {
21468   FormatStyle Style = getLLVMStyle();
21469   Style.Language = FormatStyle::LK_TableGen;
21470   verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style);
21471 }
21472 
21473 TEST_F(FormatTest, ArrayOfTemplates) {
21474   EXPECT_EQ("auto a = new unique_ptr<int>[10];",
21475             format("auto a = new unique_ptr<int > [ 10];"));
21476 
21477   FormatStyle Spaces = getLLVMStyle();
21478   Spaces.SpacesInSquareBrackets = true;
21479   EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];",
21480             format("auto a = new unique_ptr<int > [10];", Spaces));
21481 }
21482 
21483 TEST_F(FormatTest, ArrayAsTemplateType) {
21484   EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;",
21485             format("auto a = unique_ptr < Foo < Bar>[ 10]> ;"));
21486 
21487   FormatStyle Spaces = getLLVMStyle();
21488   Spaces.SpacesInSquareBrackets = true;
21489   EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;",
21490             format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces));
21491 }
21492 
21493 TEST_F(FormatTest, NoSpaceAfterSuper) { verifyFormat("__super::FooBar();"); }
21494 
21495 TEST(FormatStyle, GetStyleWithEmptyFileName) {
21496   llvm::vfs::InMemoryFileSystem FS;
21497   auto Style1 = getStyle("file", "", "Google", "", &FS);
21498   ASSERT_TRUE((bool)Style1);
21499   ASSERT_EQ(*Style1, getGoogleStyle());
21500 }
21501 
21502 TEST(FormatStyle, GetStyleOfFile) {
21503   llvm::vfs::InMemoryFileSystem FS;
21504   // Test 1: format file in the same directory.
21505   ASSERT_TRUE(
21506       FS.addFile("/a/.clang-format", 0,
21507                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM")));
21508   ASSERT_TRUE(
21509       FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
21510   auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS);
21511   ASSERT_TRUE((bool)Style1);
21512   ASSERT_EQ(*Style1, getLLVMStyle());
21513 
21514   // Test 2.1: fallback to default.
21515   ASSERT_TRUE(
21516       FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
21517   auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS);
21518   ASSERT_TRUE((bool)Style2);
21519   ASSERT_EQ(*Style2, getMozillaStyle());
21520 
21521   // Test 2.2: no format on 'none' fallback style.
21522   Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
21523   ASSERT_TRUE((bool)Style2);
21524   ASSERT_EQ(*Style2, getNoStyle());
21525 
21526   // Test 2.3: format if config is found with no based style while fallback is
21527   // 'none'.
21528   ASSERT_TRUE(FS.addFile("/b/.clang-format", 0,
21529                          llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2")));
21530   Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
21531   ASSERT_TRUE((bool)Style2);
21532   ASSERT_EQ(*Style2, getLLVMStyle());
21533 
21534   // Test 2.4: format if yaml with no based style, while fallback is 'none'.
21535   Style2 = getStyle("{}", "a.h", "none", "", &FS);
21536   ASSERT_TRUE((bool)Style2);
21537   ASSERT_EQ(*Style2, getLLVMStyle());
21538 
21539   // Test 3: format file in parent directory.
21540   ASSERT_TRUE(
21541       FS.addFile("/c/.clang-format", 0,
21542                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google")));
21543   ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0,
21544                          llvm::MemoryBuffer::getMemBuffer("int i;")));
21545   auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS);
21546   ASSERT_TRUE((bool)Style3);
21547   ASSERT_EQ(*Style3, getGoogleStyle());
21548 
21549   // Test 4: error on invalid fallback style
21550   auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS);
21551   ASSERT_FALSE((bool)Style4);
21552   llvm::consumeError(Style4.takeError());
21553 
21554   // Test 5: error on invalid yaml on command line
21555   auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS);
21556   ASSERT_FALSE((bool)Style5);
21557   llvm::consumeError(Style5.takeError());
21558 
21559   // Test 6: error on invalid style
21560   auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS);
21561   ASSERT_FALSE((bool)Style6);
21562   llvm::consumeError(Style6.takeError());
21563 
21564   // Test 7: found config file, error on parsing it
21565   ASSERT_TRUE(
21566       FS.addFile("/d/.clang-format", 0,
21567                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n"
21568                                                   "InvalidKey: InvalidValue")));
21569   ASSERT_TRUE(
21570       FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
21571   auto Style7a = getStyle("file", "/d/.clang-format", "LLVM", "", &FS);
21572   ASSERT_FALSE((bool)Style7a);
21573   llvm::consumeError(Style7a.takeError());
21574 
21575   auto Style7b = getStyle("file", "/d/.clang-format", "LLVM", "", &FS, true);
21576   ASSERT_TRUE((bool)Style7b);
21577 
21578   // Test 8: inferred per-language defaults apply.
21579   auto StyleTd = getStyle("file", "x.td", "llvm", "", &FS);
21580   ASSERT_TRUE((bool)StyleTd);
21581   ASSERT_EQ(*StyleTd, getLLVMStyle(FormatStyle::LK_TableGen));
21582 
21583   // Test 9.1.1: overwriting a file style, when no parent file exists with no
21584   // fallback style.
21585   ASSERT_TRUE(FS.addFile(
21586       "/e/sub/.clang-format", 0,
21587       llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: InheritParentConfig\n"
21588                                        "ColumnLimit: 20")));
21589   ASSERT_TRUE(FS.addFile("/e/sub/code.cpp", 0,
21590                          llvm::MemoryBuffer::getMemBuffer("int i;")));
21591   auto Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS);
21592   ASSERT_TRUE(static_cast<bool>(Style9));
21593   ASSERT_EQ(*Style9, [] {
21594     auto Style = getNoStyle();
21595     Style.ColumnLimit = 20;
21596     return Style;
21597   }());
21598 
21599   // Test 9.1.2: propagate more than one level with no parent file.
21600   ASSERT_TRUE(FS.addFile("/e/sub/sub/code.cpp", 0,
21601                          llvm::MemoryBuffer::getMemBuffer("int i;")));
21602   ASSERT_TRUE(FS.addFile("/e/sub/sub/.clang-format", 0,
21603                          llvm::MemoryBuffer::getMemBuffer(
21604                              "BasedOnStyle: InheritParentConfig\n"
21605                              "WhitespaceSensitiveMacros: ['FOO', 'BAR']")));
21606   std::vector<std::string> NonDefaultWhiteSpaceMacros{"FOO", "BAR"};
21607 
21608   ASSERT_NE(Style9->WhitespaceSensitiveMacros, NonDefaultWhiteSpaceMacros);
21609   Style9 = getStyle("file", "/e/sub/sub/code.cpp", "none", "", &FS);
21610   ASSERT_TRUE(static_cast<bool>(Style9));
21611   ASSERT_EQ(*Style9, [&NonDefaultWhiteSpaceMacros] {
21612     auto Style = getNoStyle();
21613     Style.ColumnLimit = 20;
21614     Style.WhitespaceSensitiveMacros = NonDefaultWhiteSpaceMacros;
21615     return Style;
21616   }());
21617 
21618   // Test 9.2: with LLVM fallback style
21619   Style9 = getStyle("file", "/e/sub/code.cpp", "LLVM", "", &FS);
21620   ASSERT_TRUE(static_cast<bool>(Style9));
21621   ASSERT_EQ(*Style9, [] {
21622     auto Style = getLLVMStyle();
21623     Style.ColumnLimit = 20;
21624     return Style;
21625   }());
21626 
21627   // Test 9.3: with a parent file
21628   ASSERT_TRUE(
21629       FS.addFile("/e/.clang-format", 0,
21630                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google\n"
21631                                                   "UseTab: Always")));
21632   Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS);
21633   ASSERT_TRUE(static_cast<bool>(Style9));
21634   ASSERT_EQ(*Style9, [] {
21635     auto Style = getGoogleStyle();
21636     Style.ColumnLimit = 20;
21637     Style.UseTab = FormatStyle::UT_Always;
21638     return Style;
21639   }());
21640 
21641   // Test 9.4: propagate more than one level with a parent file.
21642   const auto SubSubStyle = [&NonDefaultWhiteSpaceMacros] {
21643     auto Style = getGoogleStyle();
21644     Style.ColumnLimit = 20;
21645     Style.UseTab = FormatStyle::UT_Always;
21646     Style.WhitespaceSensitiveMacros = NonDefaultWhiteSpaceMacros;
21647     return Style;
21648   }();
21649 
21650   ASSERT_NE(Style9->WhitespaceSensitiveMacros, NonDefaultWhiteSpaceMacros);
21651   Style9 = getStyle("file", "/e/sub/sub/code.cpp", "none", "", &FS);
21652   ASSERT_TRUE(static_cast<bool>(Style9));
21653   ASSERT_EQ(*Style9, SubSubStyle);
21654 
21655   // Test 9.5: use InheritParentConfig as style name
21656   Style9 =
21657       getStyle("inheritparentconfig", "/e/sub/sub/code.cpp", "none", "", &FS);
21658   ASSERT_TRUE(static_cast<bool>(Style9));
21659   ASSERT_EQ(*Style9, SubSubStyle);
21660 
21661   // Test 9.6: use command line style with inheritance
21662   Style9 = getStyle("{BasedOnStyle: InheritParentConfig}", "/e/sub/code.cpp",
21663                     "none", "", &FS);
21664   ASSERT_TRUE(static_cast<bool>(Style9));
21665   ASSERT_EQ(*Style9, SubSubStyle);
21666 
21667   // Test 9.7: use command line style with inheritance and own config
21668   Style9 = getStyle("{BasedOnStyle: InheritParentConfig, "
21669                     "WhitespaceSensitiveMacros: ['FOO', 'BAR']}",
21670                     "/e/sub/code.cpp", "none", "", &FS);
21671   ASSERT_TRUE(static_cast<bool>(Style9));
21672   ASSERT_EQ(*Style9, SubSubStyle);
21673 
21674   // Test 9.8: use inheritance from a file without BasedOnStyle
21675   ASSERT_TRUE(FS.addFile("/e/withoutbase/.clang-format", 0,
21676                          llvm::MemoryBuffer::getMemBuffer("ColumnLimit: 123")));
21677   ASSERT_TRUE(
21678       FS.addFile("/e/withoutbase/sub/.clang-format", 0,
21679                  llvm::MemoryBuffer::getMemBuffer(
21680                      "BasedOnStyle: InheritParentConfig\nIndentWidth: 7")));
21681   // Make sure we do not use the fallback style
21682   Style9 = getStyle("file", "/e/withoutbase/code.cpp", "google", "", &FS);
21683   ASSERT_TRUE(static_cast<bool>(Style9));
21684   ASSERT_EQ(*Style9, [] {
21685     auto Style = getLLVMStyle();
21686     Style.ColumnLimit = 123;
21687     return Style;
21688   }());
21689 
21690   Style9 = getStyle("file", "/e/withoutbase/sub/code.cpp", "google", "", &FS);
21691   ASSERT_TRUE(static_cast<bool>(Style9));
21692   ASSERT_EQ(*Style9, [] {
21693     auto Style = getLLVMStyle();
21694     Style.ColumnLimit = 123;
21695     Style.IndentWidth = 7;
21696     return Style;
21697   }());
21698 
21699   // Test 9.9: use inheritance from a specific config file.
21700   Style9 = getStyle("file:/e/sub/sub/.clang-format", "/e/sub/sub/code.cpp",
21701                     "none", "", &FS);
21702   ASSERT_TRUE(static_cast<bool>(Style9));
21703   ASSERT_EQ(*Style9, SubSubStyle);
21704 }
21705 
21706 TEST(FormatStyle, GetStyleOfSpecificFile) {
21707   llvm::vfs::InMemoryFileSystem FS;
21708   // Specify absolute path to a format file in a parent directory.
21709   ASSERT_TRUE(
21710       FS.addFile("/e/.clang-format", 0,
21711                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM")));
21712   ASSERT_TRUE(
21713       FS.addFile("/e/explicit.clang-format", 0,
21714                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google")));
21715   ASSERT_TRUE(FS.addFile("/e/sub/sub/sub/test.cpp", 0,
21716                          llvm::MemoryBuffer::getMemBuffer("int i;")));
21717   auto Style = getStyle("file:/e/explicit.clang-format",
21718                         "/e/sub/sub/sub/test.cpp", "LLVM", "", &FS);
21719   ASSERT_TRUE(static_cast<bool>(Style));
21720   ASSERT_EQ(*Style, getGoogleStyle());
21721 
21722   // Specify relative path to a format file.
21723   ASSERT_TRUE(
21724       FS.addFile("../../e/explicit.clang-format", 0,
21725                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google")));
21726   Style = getStyle("file:../../e/explicit.clang-format",
21727                    "/e/sub/sub/sub/test.cpp", "LLVM", "", &FS);
21728   ASSERT_TRUE(static_cast<bool>(Style));
21729   ASSERT_EQ(*Style, getGoogleStyle());
21730 
21731   // Specify path to a format file that does not exist.
21732   Style = getStyle("file:/e/missing.clang-format", "/e/sub/sub/sub/test.cpp",
21733                    "LLVM", "", &FS);
21734   ASSERT_FALSE(static_cast<bool>(Style));
21735   llvm::consumeError(Style.takeError());
21736 
21737   // Specify path to a file on the filesystem.
21738   SmallString<128> FormatFilePath;
21739   std::error_code ECF = llvm::sys::fs::createTemporaryFile(
21740       "FormatFileTest", "tpl", FormatFilePath);
21741   EXPECT_FALSE((bool)ECF);
21742   llvm::raw_fd_ostream FormatFileTest(FormatFilePath, ECF);
21743   EXPECT_FALSE((bool)ECF);
21744   FormatFileTest << "BasedOnStyle: Google\n";
21745   FormatFileTest.close();
21746 
21747   SmallString<128> TestFilePath;
21748   std::error_code ECT =
21749       llvm::sys::fs::createTemporaryFile("CodeFileTest", "cc", TestFilePath);
21750   EXPECT_FALSE((bool)ECT);
21751   llvm::raw_fd_ostream CodeFileTest(TestFilePath, ECT);
21752   CodeFileTest << "int i;\n";
21753   CodeFileTest.close();
21754 
21755   std::string format_file_arg = std::string("file:") + FormatFilePath.c_str();
21756   Style = getStyle(format_file_arg, TestFilePath, "LLVM", "", nullptr);
21757 
21758   llvm::sys::fs::remove(FormatFilePath.c_str());
21759   llvm::sys::fs::remove(TestFilePath.c_str());
21760   ASSERT_TRUE(static_cast<bool>(Style));
21761   ASSERT_EQ(*Style, getGoogleStyle());
21762 }
21763 
21764 TEST_F(ReplacementTest, FormatCodeAfterReplacements) {
21765   // Column limit is 20.
21766   std::string Code = "Type *a =\n"
21767                      "    new Type();\n"
21768                      "g(iiiii, 0, jjjjj,\n"
21769                      "  0, kkkkk, 0, mm);\n"
21770                      "int  bad     = format   ;";
21771   std::string Expected = "auto a = new Type();\n"
21772                          "g(iiiii, nullptr,\n"
21773                          "  jjjjj, nullptr,\n"
21774                          "  kkkkk, nullptr,\n"
21775                          "  mm);\n"
21776                          "int  bad     = format   ;";
21777   FileID ID = Context.createInMemoryFile("format.cpp", Code);
21778   tooling::Replacements Replaces = toReplacements(
21779       {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6,
21780                             "auto "),
21781        tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1,
21782                             "nullptr"),
21783        tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1,
21784                             "nullptr"),
21785        tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1,
21786                             "nullptr")});
21787 
21788   FormatStyle Style = getLLVMStyle();
21789   Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility.
21790   auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
21791   EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
21792       << llvm::toString(FormattedReplaces.takeError()) << "\n";
21793   auto Result = applyAllReplacements(Code, *FormattedReplaces);
21794   EXPECT_TRUE(static_cast<bool>(Result));
21795   EXPECT_EQ(Expected, *Result);
21796 }
21797 
21798 TEST_F(ReplacementTest, SortIncludesAfterReplacement) {
21799   std::string Code = "#include \"a.h\"\n"
21800                      "#include \"c.h\"\n"
21801                      "\n"
21802                      "int main() {\n"
21803                      "  return 0;\n"
21804                      "}";
21805   std::string Expected = "#include \"a.h\"\n"
21806                          "#include \"b.h\"\n"
21807                          "#include \"c.h\"\n"
21808                          "\n"
21809                          "int main() {\n"
21810                          "  return 0;\n"
21811                          "}";
21812   FileID ID = Context.createInMemoryFile("fix.cpp", Code);
21813   tooling::Replacements Replaces = toReplacements(
21814       {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0,
21815                             "#include \"b.h\"\n")});
21816 
21817   FormatStyle Style = getLLVMStyle();
21818   Style.SortIncludes = FormatStyle::SI_CaseSensitive;
21819   auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
21820   EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
21821       << llvm::toString(FormattedReplaces.takeError()) << "\n";
21822   auto Result = applyAllReplacements(Code, *FormattedReplaces);
21823   EXPECT_TRUE(static_cast<bool>(Result));
21824   EXPECT_EQ(Expected, *Result);
21825 }
21826 
21827 TEST_F(FormatTest, FormatSortsUsingDeclarations) {
21828   EXPECT_EQ("using std::cin;\n"
21829             "using std::cout;",
21830             format("using std::cout;\n"
21831                    "using std::cin;",
21832                    getGoogleStyle()));
21833 }
21834 
21835 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) {
21836   FormatStyle Style = getLLVMStyle();
21837   Style.Standard = FormatStyle::LS_Cpp03;
21838   // cpp03 recognize this string as identifier u8 and literal character 'a'
21839   EXPECT_EQ("auto c = u8 'a';", format("auto c = u8'a';", Style));
21840 }
21841 
21842 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) {
21843   // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers
21844   // all modes, including C++11, C++14 and C++17
21845   EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';"));
21846 }
21847 
21848 TEST_F(FormatTest, DoNotFormatLikelyXml) {
21849   EXPECT_EQ("<!-- ;> -->", format("<!-- ;> -->", getGoogleStyle()));
21850   EXPECT_EQ(" <!-- >; -->", format(" <!-- >; -->", getGoogleStyle()));
21851 }
21852 
21853 TEST_F(FormatTest, StructuredBindings) {
21854   // Structured bindings is a C++17 feature.
21855   // all modes, including C++11, C++14 and C++17
21856   verifyFormat("auto [a, b] = f();");
21857   EXPECT_EQ("auto [a, b] = f();", format("auto[a, b] = f();"));
21858   EXPECT_EQ("const auto [a, b] = f();", format("const   auto[a, b] = f();"));
21859   EXPECT_EQ("auto const [a, b] = f();", format("auto  const[a, b] = f();"));
21860   EXPECT_EQ("auto const volatile [a, b] = f();",
21861             format("auto  const   volatile[a, b] = f();"));
21862   EXPECT_EQ("auto [a, b, c] = f();", format("auto   [  a  ,  b,c   ] = f();"));
21863   EXPECT_EQ("auto &[a, b, c] = f();",
21864             format("auto   &[  a  ,  b,c   ] = f();"));
21865   EXPECT_EQ("auto &&[a, b, c] = f();",
21866             format("auto   &&[  a  ,  b,c   ] = f();"));
21867   EXPECT_EQ("auto const &[a, b] = f();", format("auto  const&[a, b] = f();"));
21868   EXPECT_EQ("auto const volatile &&[a, b] = f();",
21869             format("auto  const  volatile  &&[a, b] = f();"));
21870   EXPECT_EQ("auto const &&[a, b] = f();",
21871             format("auto  const   &&  [a, b] = f();"));
21872   EXPECT_EQ("const auto &[a, b] = f();",
21873             format("const  auto  &  [a, b] = f();"));
21874   EXPECT_EQ("const auto volatile &&[a, b] = f();",
21875             format("const  auto   volatile  &&[a, b] = f();"));
21876   EXPECT_EQ("volatile const auto &&[a, b] = f();",
21877             format("volatile  const  auto   &&[a, b] = f();"));
21878   EXPECT_EQ("const auto &&[a, b] = f();",
21879             format("const  auto  &&  [a, b] = f();"));
21880 
21881   // Make sure we don't mistake structured bindings for lambdas.
21882   FormatStyle PointerMiddle = getLLVMStyle();
21883   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
21884   verifyFormat("auto [a1, b]{A * i};", getGoogleStyle());
21885   verifyFormat("auto [a2, b]{A * i};", getLLVMStyle());
21886   verifyFormat("auto [a3, b]{A * i};", PointerMiddle);
21887   verifyFormat("auto const [a1, b]{A * i};", getGoogleStyle());
21888   verifyFormat("auto const [a2, b]{A * i};", getLLVMStyle());
21889   verifyFormat("auto const [a3, b]{A * i};", PointerMiddle);
21890   verifyFormat("auto const& [a1, b]{A * i};", getGoogleStyle());
21891   verifyFormat("auto const &[a2, b]{A * i};", getLLVMStyle());
21892   verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle);
21893   verifyFormat("auto const&& [a1, b]{A * i};", getGoogleStyle());
21894   verifyFormat("auto const &&[a2, b]{A * i};", getLLVMStyle());
21895   verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle);
21896 
21897   EXPECT_EQ("for (const auto &&[a, b] : some_range) {\n}",
21898             format("for (const auto   &&   [a, b] : some_range) {\n}"));
21899   EXPECT_EQ("for (const auto &[a, b] : some_range) {\n}",
21900             format("for (const auto   &   [a, b] : some_range) {\n}"));
21901   EXPECT_EQ("for (const auto [a, b] : some_range) {\n}",
21902             format("for (const auto[a, b] : some_range) {\n}"));
21903   EXPECT_EQ("auto [x, y](expr);", format("auto[x,y]  (expr);"));
21904   EXPECT_EQ("auto &[x, y](expr);", format("auto  &  [x,y]  (expr);"));
21905   EXPECT_EQ("auto &&[x, y](expr);", format("auto  &&  [x,y]  (expr);"));
21906   EXPECT_EQ("auto const &[x, y](expr);",
21907             format("auto  const  &  [x,y]  (expr);"));
21908   EXPECT_EQ("auto const &&[x, y](expr);",
21909             format("auto  const  &&  [x,y]  (expr);"));
21910   EXPECT_EQ("auto [x, y]{expr};", format("auto[x,y]     {expr};"));
21911   EXPECT_EQ("auto const &[x, y]{expr};",
21912             format("auto  const  &  [x,y]  {expr};"));
21913   EXPECT_EQ("auto const &&[x, y]{expr};",
21914             format("auto  const  &&  [x,y]  {expr};"));
21915 
21916   FormatStyle Spaces = getLLVMStyle();
21917   Spaces.SpacesInSquareBrackets = true;
21918   verifyFormat("auto [ a, b ] = f();", Spaces);
21919   verifyFormat("auto &&[ a, b ] = f();", Spaces);
21920   verifyFormat("auto &[ a, b ] = f();", Spaces);
21921   verifyFormat("auto const &&[ a, b ] = f();", Spaces);
21922   verifyFormat("auto const &[ a, b ] = f();", Spaces);
21923 }
21924 
21925 TEST_F(FormatTest, FileAndCode) {
21926   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.cc", ""));
21927   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.m", ""));
21928   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.mm", ""));
21929   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", ""));
21930   EXPECT_EQ(FormatStyle::LK_ObjC,
21931             guessLanguage("foo.h", "@interface Foo\n@end\n"));
21932   EXPECT_EQ(
21933       FormatStyle::LK_ObjC,
21934       guessLanguage("foo.h", "#define TRY(x, y) @try { x; } @finally { y; }"));
21935   EXPECT_EQ(FormatStyle::LK_ObjC,
21936             guessLanguage("foo.h", "#define AVAIL(x) @available(x, *))"));
21937   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@class Foo;"));
21938   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo", ""));
21939   EXPECT_EQ(FormatStyle::LK_ObjC,
21940             guessLanguage("foo", "@interface Foo\n@end\n"));
21941   EXPECT_EQ(FormatStyle::LK_ObjC,
21942             guessLanguage("foo.h", "int DoStuff(CGRect rect);\n"));
21943   EXPECT_EQ(
21944       FormatStyle::LK_ObjC,
21945       guessLanguage("foo.h",
21946                     "#define MY_POINT_MAKE(x, y) CGPointMake((x), (y));\n"));
21947   EXPECT_EQ(
21948       FormatStyle::LK_Cpp,
21949       guessLanguage("foo.h", "#define FOO(...) auto bar = [] __VA_ARGS__;"));
21950 }
21951 
21952 TEST_F(FormatTest, GuessLanguageWithCpp11AttributeSpecifiers) {
21953   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[noreturn]];"));
21954   EXPECT_EQ(FormatStyle::LK_ObjC,
21955             guessLanguage("foo.h", "array[[calculator getIndex]];"));
21956   EXPECT_EQ(FormatStyle::LK_Cpp,
21957             guessLanguage("foo.h", "[[noreturn, deprecated(\"so sorry\")]];"));
21958   EXPECT_EQ(
21959       FormatStyle::LK_Cpp,
21960       guessLanguage("foo.h", "[[noreturn, deprecated(\"gone, sorry\")]];"));
21961   EXPECT_EQ(FormatStyle::LK_ObjC,
21962             guessLanguage("foo.h", "[[noreturn foo] bar];"));
21963   EXPECT_EQ(FormatStyle::LK_Cpp,
21964             guessLanguage("foo.h", "[[clang::fallthrough]];"));
21965   EXPECT_EQ(FormatStyle::LK_ObjC,
21966             guessLanguage("foo.h", "[[clang:fallthrough] foo];"));
21967   EXPECT_EQ(FormatStyle::LK_Cpp,
21968             guessLanguage("foo.h", "[[gsl::suppress(\"type\")]];"));
21969   EXPECT_EQ(FormatStyle::LK_Cpp,
21970             guessLanguage("foo.h", "[[using clang: fallthrough]];"));
21971   EXPECT_EQ(FormatStyle::LK_ObjC,
21972             guessLanguage("foo.h", "[[abusing clang:fallthrough] bar];"));
21973   EXPECT_EQ(FormatStyle::LK_Cpp,
21974             guessLanguage("foo.h", "[[using gsl: suppress(\"type\")]];"));
21975   EXPECT_EQ(
21976       FormatStyle::LK_Cpp,
21977       guessLanguage("foo.h", "for (auto &&[endpoint, stream] : streams_)"));
21978   EXPECT_EQ(
21979       FormatStyle::LK_Cpp,
21980       guessLanguage("foo.h",
21981                     "[[clang::callable_when(\"unconsumed\", \"unknown\")]]"));
21982   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[foo::bar, ...]]"));
21983 }
21984 
21985 TEST_F(FormatTest, GuessLanguageWithCaret) {
21986   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^);"));
21987   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^, Bar);"));
21988   EXPECT_EQ(FormatStyle::LK_ObjC,
21989             guessLanguage("foo.h", "int(^)(char, float);"));
21990   EXPECT_EQ(FormatStyle::LK_ObjC,
21991             guessLanguage("foo.h", "int(^foo)(char, float);"));
21992   EXPECT_EQ(FormatStyle::LK_ObjC,
21993             guessLanguage("foo.h", "int(^foo[10])(char, float);"));
21994   EXPECT_EQ(FormatStyle::LK_ObjC,
21995             guessLanguage("foo.h", "int(^foo[kNumEntries])(char, float);"));
21996   EXPECT_EQ(
21997       FormatStyle::LK_ObjC,
21998       guessLanguage("foo.h", "int(^foo[(kNumEntries + 10)])(char, float);"));
21999 }
22000 
22001 TEST_F(FormatTest, GuessLanguageWithPragmas) {
22002   EXPECT_EQ(FormatStyle::LK_Cpp,
22003             guessLanguage("foo.h", "__pragma(warning(disable:))"));
22004   EXPECT_EQ(FormatStyle::LK_Cpp,
22005             guessLanguage("foo.h", "#pragma(warning(disable:))"));
22006   EXPECT_EQ(FormatStyle::LK_Cpp,
22007             guessLanguage("foo.h", "_Pragma(warning(disable:))"));
22008 }
22009 
22010 TEST_F(FormatTest, FormatsInlineAsmSymbolicNames) {
22011   // ASM symbolic names are identifiers that must be surrounded by [] without
22012   // space in between:
22013   // https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#InputOperands
22014 
22015   // Example from https://bugs.llvm.org/show_bug.cgi?id=45108.
22016   verifyFormat(R"(//
22017 asm volatile("mrs %x[result], FPCR" : [result] "=r"(result));
22018 )");
22019 
22020   // A list of several ASM symbolic names.
22021   verifyFormat(R"(asm("mov %[e], %[d]" : [d] "=rm"(d), [e] "rm"(*e));)");
22022 
22023   // ASM symbolic names in inline ASM with inputs and outputs.
22024   verifyFormat(R"(//
22025 asm("cmoveq %1, %2, %[result]"
22026     : [result] "=r"(result)
22027     : "r"(test), "r"(new), "[result]"(old));
22028 )");
22029 
22030   // ASM symbolic names in inline ASM with no outputs.
22031   verifyFormat(R"(asm("mov %[e], %[d]" : : [d] "=rm"(d), [e] "rm"(*e));)");
22032 }
22033 
22034 TEST_F(FormatTest, GuessedLanguageWithInlineAsmClobbers) {
22035   EXPECT_EQ(FormatStyle::LK_Cpp,
22036             guessLanguage("foo.h", "void f() {\n"
22037                                    "  asm (\"mov %[e], %[d]\"\n"
22038                                    "     : [d] \"=rm\" (d)\n"
22039                                    "       [e] \"rm\" (*e));\n"
22040                                    "}"));
22041   EXPECT_EQ(FormatStyle::LK_Cpp,
22042             guessLanguage("foo.h", "void f() {\n"
22043                                    "  _asm (\"mov %[e], %[d]\"\n"
22044                                    "     : [d] \"=rm\" (d)\n"
22045                                    "       [e] \"rm\" (*e));\n"
22046                                    "}"));
22047   EXPECT_EQ(FormatStyle::LK_Cpp,
22048             guessLanguage("foo.h", "void f() {\n"
22049                                    "  __asm (\"mov %[e], %[d]\"\n"
22050                                    "     : [d] \"=rm\" (d)\n"
22051                                    "       [e] \"rm\" (*e));\n"
22052                                    "}"));
22053   EXPECT_EQ(FormatStyle::LK_Cpp,
22054             guessLanguage("foo.h", "void f() {\n"
22055                                    "  __asm__ (\"mov %[e], %[d]\"\n"
22056                                    "     : [d] \"=rm\" (d)\n"
22057                                    "       [e] \"rm\" (*e));\n"
22058                                    "}"));
22059   EXPECT_EQ(FormatStyle::LK_Cpp,
22060             guessLanguage("foo.h", "void f() {\n"
22061                                    "  asm (\"mov %[e], %[d]\"\n"
22062                                    "     : [d] \"=rm\" (d),\n"
22063                                    "       [e] \"rm\" (*e));\n"
22064                                    "}"));
22065   EXPECT_EQ(FormatStyle::LK_Cpp,
22066             guessLanguage("foo.h", "void f() {\n"
22067                                    "  asm volatile (\"mov %[e], %[d]\"\n"
22068                                    "     : [d] \"=rm\" (d)\n"
22069                                    "       [e] \"rm\" (*e));\n"
22070                                    "}"));
22071 }
22072 
22073 TEST_F(FormatTest, GuessLanguageWithChildLines) {
22074   EXPECT_EQ(FormatStyle::LK_Cpp,
22075             guessLanguage("foo.h", "#define FOO ({ std::string s; })"));
22076   EXPECT_EQ(FormatStyle::LK_ObjC,
22077             guessLanguage("foo.h", "#define FOO ({ NSString *s; })"));
22078   EXPECT_EQ(
22079       FormatStyle::LK_Cpp,
22080       guessLanguage("foo.h", "#define FOO ({ foo(); ({ std::string s; }) })"));
22081   EXPECT_EQ(
22082       FormatStyle::LK_ObjC,
22083       guessLanguage("foo.h", "#define FOO ({ foo(); ({ NSString *s; }) })"));
22084 }
22085 
22086 TEST_F(FormatTest, TypenameMacros) {
22087   std::vector<std::string> TypenameMacros = {"STACK_OF", "LIST", "TAILQ_ENTRY"};
22088 
22089   // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=30353
22090   FormatStyle Google = getGoogleStyleWithColumns(0);
22091   Google.TypenameMacros = TypenameMacros;
22092   verifyFormat("struct foo {\n"
22093                "  int bar;\n"
22094                "  TAILQ_ENTRY(a) bleh;\n"
22095                "};",
22096                Google);
22097 
22098   FormatStyle Macros = getLLVMStyle();
22099   Macros.TypenameMacros = TypenameMacros;
22100 
22101   verifyFormat("STACK_OF(int) a;", Macros);
22102   verifyFormat("STACK_OF(int) *a;", Macros);
22103   verifyFormat("STACK_OF(int const *) *a;", Macros);
22104   verifyFormat("STACK_OF(int *const) *a;", Macros);
22105   verifyFormat("STACK_OF(int, string) a;", Macros);
22106   verifyFormat("STACK_OF(LIST(int)) a;", Macros);
22107   verifyFormat("STACK_OF(LIST(int)) a, b;", Macros);
22108   verifyFormat("for (LIST(int) *a = NULL; a;) {\n}", Macros);
22109   verifyFormat("STACK_OF(int) f(LIST(int) *arg);", Macros);
22110   verifyFormat("vector<LIST(uint64_t) *attr> x;", Macros);
22111   verifyFormat("vector<LIST(uint64_t) *const> f(LIST(uint64_t) *arg);", Macros);
22112 
22113   Macros.PointerAlignment = FormatStyle::PAS_Left;
22114   verifyFormat("STACK_OF(int)* a;", Macros);
22115   verifyFormat("STACK_OF(int*)* a;", Macros);
22116   verifyFormat("x = (STACK_OF(uint64_t))*a;", Macros);
22117   verifyFormat("x = (STACK_OF(uint64_t))&a;", Macros);
22118   verifyFormat("vector<STACK_OF(uint64_t)* attr> x;", Macros);
22119 }
22120 
22121 TEST_F(FormatTest, AtomicQualifier) {
22122   // Check that we treate _Atomic as a type and not a function call
22123   FormatStyle Google = getGoogleStyleWithColumns(0);
22124   verifyFormat("struct foo {\n"
22125                "  int a1;\n"
22126                "  _Atomic(a) a2;\n"
22127                "  _Atomic(_Atomic(int) *const) a3;\n"
22128                "};",
22129                Google);
22130   verifyFormat("_Atomic(uint64_t) a;");
22131   verifyFormat("_Atomic(uint64_t) *a;");
22132   verifyFormat("_Atomic(uint64_t const *) *a;");
22133   verifyFormat("_Atomic(uint64_t *const) *a;");
22134   verifyFormat("_Atomic(const uint64_t *) *a;");
22135   verifyFormat("_Atomic(uint64_t) a;");
22136   verifyFormat("_Atomic(_Atomic(uint64_t)) a;");
22137   verifyFormat("_Atomic(_Atomic(uint64_t)) a, b;");
22138   verifyFormat("for (_Atomic(uint64_t) *a = NULL; a;) {\n}");
22139   verifyFormat("_Atomic(uint64_t) f(_Atomic(uint64_t) *arg);");
22140 
22141   verifyFormat("_Atomic(uint64_t) *s(InitValue);");
22142   verifyFormat("_Atomic(uint64_t) *s{InitValue};");
22143   FormatStyle Style = getLLVMStyle();
22144   Style.PointerAlignment = FormatStyle::PAS_Left;
22145   verifyFormat("_Atomic(uint64_t)* s(InitValue);", Style);
22146   verifyFormat("_Atomic(uint64_t)* s{InitValue};", Style);
22147   verifyFormat("_Atomic(int)* a;", Style);
22148   verifyFormat("_Atomic(int*)* a;", Style);
22149   verifyFormat("vector<_Atomic(uint64_t)* attr> x;", Style);
22150 
22151   Style.SpacesInCStyleCastParentheses = true;
22152   Style.SpacesInParentheses = false;
22153   verifyFormat("x = ( _Atomic(uint64_t) )*a;", Style);
22154   Style.SpacesInCStyleCastParentheses = false;
22155   Style.SpacesInParentheses = true;
22156   verifyFormat("x = (_Atomic( uint64_t ))*a;", Style);
22157   verifyFormat("x = (_Atomic( uint64_t ))&a;", Style);
22158 }
22159 
22160 TEST_F(FormatTest, AmbersandInLamda) {
22161   // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=41899
22162   FormatStyle AlignStyle = getLLVMStyle();
22163   AlignStyle.PointerAlignment = FormatStyle::PAS_Left;
22164   verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle);
22165   AlignStyle.PointerAlignment = FormatStyle::PAS_Right;
22166   verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle);
22167 }
22168 
22169 TEST_F(FormatTest, SpacesInConditionalStatement) {
22170   FormatStyle Spaces = getLLVMStyle();
22171   Spaces.IfMacros.clear();
22172   Spaces.IfMacros.push_back("MYIF");
22173   Spaces.SpacesInConditionalStatement = true;
22174   verifyFormat("for ( int i = 0; i; i++ )\n  continue;", Spaces);
22175   verifyFormat("if ( !a )\n  return;", Spaces);
22176   verifyFormat("if ( a )\n  return;", Spaces);
22177   verifyFormat("if constexpr ( a )\n  return;", Spaces);
22178   verifyFormat("MYIF ( a )\n  return;", Spaces);
22179   verifyFormat("MYIF ( a )\n  return;\nelse MYIF ( b )\n  return;", Spaces);
22180   verifyFormat("MYIF ( a )\n  return;\nelse\n  return;", Spaces);
22181   verifyFormat("switch ( a )\ncase 1:\n  return;", Spaces);
22182   verifyFormat("while ( a )\n  return;", Spaces);
22183   verifyFormat("while ( (a && b) )\n  return;", Spaces);
22184   verifyFormat("do {\n} while ( 1 != 0 );", Spaces);
22185   verifyFormat("try {\n} catch ( const std::exception & ) {\n}", Spaces);
22186   // Check that space on the left of "::" is inserted as expected at beginning
22187   // of condition.
22188   verifyFormat("while ( ::func() )\n  return;", Spaces);
22189 
22190   // Check impact of ControlStatementsExceptControlMacros is honored.
22191   Spaces.SpaceBeforeParens =
22192       FormatStyle::SBPO_ControlStatementsExceptControlMacros;
22193   verifyFormat("MYIF( a )\n  return;", Spaces);
22194   verifyFormat("MYIF( a )\n  return;\nelse MYIF( b )\n  return;", Spaces);
22195   verifyFormat("MYIF( a )\n  return;\nelse\n  return;", Spaces);
22196 }
22197 
22198 TEST_F(FormatTest, AlternativeOperators) {
22199   // Test case for ensuring alternate operators are not
22200   // combined with their right most neighbour.
22201   verifyFormat("int a and b;");
22202   verifyFormat("int a and_eq b;");
22203   verifyFormat("int a bitand b;");
22204   verifyFormat("int a bitor b;");
22205   verifyFormat("int a compl b;");
22206   verifyFormat("int a not b;");
22207   verifyFormat("int a not_eq b;");
22208   verifyFormat("int a or b;");
22209   verifyFormat("int a xor b;");
22210   verifyFormat("int a xor_eq b;");
22211   verifyFormat("return this not_eq bitand other;");
22212   verifyFormat("bool operator not_eq(const X bitand other)");
22213 
22214   verifyFormat("int a and 5;");
22215   verifyFormat("int a and_eq 5;");
22216   verifyFormat("int a bitand 5;");
22217   verifyFormat("int a bitor 5;");
22218   verifyFormat("int a compl 5;");
22219   verifyFormat("int a not 5;");
22220   verifyFormat("int a not_eq 5;");
22221   verifyFormat("int a or 5;");
22222   verifyFormat("int a xor 5;");
22223   verifyFormat("int a xor_eq 5;");
22224 
22225   verifyFormat("int a compl(5);");
22226   verifyFormat("int a not(5);");
22227 
22228   /* FIXME handle alternate tokens
22229    * https://en.cppreference.com/w/cpp/language/operator_alternative
22230   // alternative tokens
22231   verifyFormat("compl foo();");     //  ~foo();
22232   verifyFormat("foo() <%%>;");      // foo();
22233   verifyFormat("void foo() <%%>;"); // void foo(){}
22234   verifyFormat("int a <:1:>;");     // int a[1];[
22235   verifyFormat("%:define ABC abc"); // #define ABC abc
22236   verifyFormat("%:%:");             // ##
22237   */
22238 }
22239 
22240 TEST_F(FormatTest, STLWhileNotDefineChed) {
22241   verifyFormat("#if defined(while)\n"
22242                "#define while EMIT WARNING C4005\n"
22243                "#endif // while");
22244 }
22245 
22246 TEST_F(FormatTest, OperatorSpacing) {
22247   FormatStyle Style = getLLVMStyle();
22248   Style.PointerAlignment = FormatStyle::PAS_Right;
22249   verifyFormat("Foo::operator*();", Style);
22250   verifyFormat("Foo::operator void *();", Style);
22251   verifyFormat("Foo::operator void **();", Style);
22252   verifyFormat("Foo::operator void *&();", Style);
22253   verifyFormat("Foo::operator void *&&();", Style);
22254   verifyFormat("Foo::operator void const *();", Style);
22255   verifyFormat("Foo::operator void const **();", Style);
22256   verifyFormat("Foo::operator void const *&();", Style);
22257   verifyFormat("Foo::operator void const *&&();", Style);
22258   verifyFormat("Foo::operator()(void *);", Style);
22259   verifyFormat("Foo::operator*(void *);", Style);
22260   verifyFormat("Foo::operator*();", Style);
22261   verifyFormat("Foo::operator**();", Style);
22262   verifyFormat("Foo::operator&();", Style);
22263   verifyFormat("Foo::operator<int> *();", Style);
22264   verifyFormat("Foo::operator<Foo> *();", Style);
22265   verifyFormat("Foo::operator<int> **();", Style);
22266   verifyFormat("Foo::operator<Foo> **();", Style);
22267   verifyFormat("Foo::operator<int> &();", Style);
22268   verifyFormat("Foo::operator<Foo> &();", Style);
22269   verifyFormat("Foo::operator<int> &&();", Style);
22270   verifyFormat("Foo::operator<Foo> &&();", Style);
22271   verifyFormat("Foo::operator<int> *&();", Style);
22272   verifyFormat("Foo::operator<Foo> *&();", Style);
22273   verifyFormat("Foo::operator<int> *&&();", Style);
22274   verifyFormat("Foo::operator<Foo> *&&();", Style);
22275   verifyFormat("operator*(int (*)(), class Foo);", Style);
22276 
22277   verifyFormat("Foo::operator&();", Style);
22278   verifyFormat("Foo::operator void &();", Style);
22279   verifyFormat("Foo::operator void const &();", Style);
22280   verifyFormat("Foo::operator()(void &);", Style);
22281   verifyFormat("Foo::operator&(void &);", Style);
22282   verifyFormat("Foo::operator&();", Style);
22283   verifyFormat("operator&(int (&)(), class Foo);", Style);
22284   verifyFormat("operator&&(int (&)(), class Foo);", Style);
22285 
22286   verifyFormat("Foo::operator&&();", Style);
22287   verifyFormat("Foo::operator**();", Style);
22288   verifyFormat("Foo::operator void &&();", Style);
22289   verifyFormat("Foo::operator void const &&();", Style);
22290   verifyFormat("Foo::operator()(void &&);", Style);
22291   verifyFormat("Foo::operator&&(void &&);", Style);
22292   verifyFormat("Foo::operator&&();", Style);
22293   verifyFormat("operator&&(int (&&)(), class Foo);", Style);
22294   verifyFormat("operator const nsTArrayRight<E> &()", Style);
22295   verifyFormat("[[nodiscard]] operator const nsTArrayRight<E, Allocator> &()",
22296                Style);
22297   verifyFormat("operator void **()", Style);
22298   verifyFormat("operator const FooRight<Object> &()", Style);
22299   verifyFormat("operator const FooRight<Object> *()", Style);
22300   verifyFormat("operator const FooRight<Object> **()", Style);
22301   verifyFormat("operator const FooRight<Object> *&()", Style);
22302   verifyFormat("operator const FooRight<Object> *&&()", Style);
22303 
22304   Style.PointerAlignment = FormatStyle::PAS_Left;
22305   verifyFormat("Foo::operator*();", Style);
22306   verifyFormat("Foo::operator**();", Style);
22307   verifyFormat("Foo::operator void*();", Style);
22308   verifyFormat("Foo::operator void**();", Style);
22309   verifyFormat("Foo::operator void*&();", Style);
22310   verifyFormat("Foo::operator void*&&();", Style);
22311   verifyFormat("Foo::operator void const*();", Style);
22312   verifyFormat("Foo::operator void const**();", Style);
22313   verifyFormat("Foo::operator void const*&();", Style);
22314   verifyFormat("Foo::operator void const*&&();", Style);
22315   verifyFormat("Foo::operator/*comment*/ void*();", Style);
22316   verifyFormat("Foo::operator/*a*/ const /*b*/ void*();", Style);
22317   verifyFormat("Foo::operator/*a*/ volatile /*b*/ void*();", Style);
22318   verifyFormat("Foo::operator()(void*);", Style);
22319   verifyFormat("Foo::operator*(void*);", Style);
22320   verifyFormat("Foo::operator*();", Style);
22321   verifyFormat("Foo::operator<int>*();", Style);
22322   verifyFormat("Foo::operator<Foo>*();", Style);
22323   verifyFormat("Foo::operator<int>**();", Style);
22324   verifyFormat("Foo::operator<Foo>**();", Style);
22325   verifyFormat("Foo::operator<Foo>*&();", Style);
22326   verifyFormat("Foo::operator<int>&();", Style);
22327   verifyFormat("Foo::operator<Foo>&();", Style);
22328   verifyFormat("Foo::operator<int>&&();", Style);
22329   verifyFormat("Foo::operator<Foo>&&();", Style);
22330   verifyFormat("Foo::operator<int>*&();", Style);
22331   verifyFormat("Foo::operator<Foo>*&();", Style);
22332   verifyFormat("operator*(int (*)(), class Foo);", Style);
22333 
22334   verifyFormat("Foo::operator&();", Style);
22335   verifyFormat("Foo::operator void&();", Style);
22336   verifyFormat("Foo::operator void const&();", Style);
22337   verifyFormat("Foo::operator/*comment*/ void&();", Style);
22338   verifyFormat("Foo::operator/*a*/ const /*b*/ void&();", Style);
22339   verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&();", Style);
22340   verifyFormat("Foo::operator()(void&);", Style);
22341   verifyFormat("Foo::operator&(void&);", Style);
22342   verifyFormat("Foo::operator&();", Style);
22343   verifyFormat("operator&(int (&)(), class Foo);", Style);
22344   verifyFormat("operator&(int (&&)(), class Foo);", Style);
22345   verifyFormat("operator&&(int (&&)(), class Foo);", Style);
22346 
22347   verifyFormat("Foo::operator&&();", Style);
22348   verifyFormat("Foo::operator void&&();", Style);
22349   verifyFormat("Foo::operator void const&&();", Style);
22350   verifyFormat("Foo::operator/*comment*/ void&&();", Style);
22351   verifyFormat("Foo::operator/*a*/ const /*b*/ void&&();", Style);
22352   verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&&();", Style);
22353   verifyFormat("Foo::operator()(void&&);", Style);
22354   verifyFormat("Foo::operator&&(void&&);", Style);
22355   verifyFormat("Foo::operator&&();", Style);
22356   verifyFormat("operator&&(int (&&)(), class Foo);", Style);
22357   verifyFormat("operator const nsTArrayLeft<E>&()", Style);
22358   verifyFormat("[[nodiscard]] operator const nsTArrayLeft<E, Allocator>&()",
22359                Style);
22360   verifyFormat("operator void**()", Style);
22361   verifyFormat("operator const FooLeft<Object>&()", Style);
22362   verifyFormat("operator const FooLeft<Object>*()", Style);
22363   verifyFormat("operator const FooLeft<Object>**()", Style);
22364   verifyFormat("operator const FooLeft<Object>*&()", Style);
22365   verifyFormat("operator const FooLeft<Object>*&&()", Style);
22366 
22367   // PR45107
22368   verifyFormat("operator Vector<String>&();", Style);
22369   verifyFormat("operator const Vector<String>&();", Style);
22370   verifyFormat("operator foo::Bar*();", Style);
22371   verifyFormat("operator const Foo<X>::Bar<Y>*();", Style);
22372   verifyFormat("operator/*a*/ const /*b*/ Foo /*c*/<X> /*d*/ ::Bar<Y>*();",
22373                Style);
22374 
22375   Style.PointerAlignment = FormatStyle::PAS_Middle;
22376   verifyFormat("Foo::operator*();", Style);
22377   verifyFormat("Foo::operator void *();", Style);
22378   verifyFormat("Foo::operator()(void *);", Style);
22379   verifyFormat("Foo::operator*(void *);", Style);
22380   verifyFormat("Foo::operator*();", Style);
22381   verifyFormat("operator*(int (*)(), class Foo);", Style);
22382 
22383   verifyFormat("Foo::operator&();", Style);
22384   verifyFormat("Foo::operator void &();", Style);
22385   verifyFormat("Foo::operator void const &();", Style);
22386   verifyFormat("Foo::operator()(void &);", Style);
22387   verifyFormat("Foo::operator&(void &);", Style);
22388   verifyFormat("Foo::operator&();", Style);
22389   verifyFormat("operator&(int (&)(), class Foo);", Style);
22390 
22391   verifyFormat("Foo::operator&&();", Style);
22392   verifyFormat("Foo::operator void &&();", Style);
22393   verifyFormat("Foo::operator void const &&();", Style);
22394   verifyFormat("Foo::operator()(void &&);", Style);
22395   verifyFormat("Foo::operator&&(void &&);", Style);
22396   verifyFormat("Foo::operator&&();", Style);
22397   verifyFormat("operator&&(int (&&)(), class Foo);", Style);
22398 }
22399 
22400 TEST_F(FormatTest, OperatorPassedAsAFunctionPtr) {
22401   FormatStyle Style = getLLVMStyle();
22402   // PR46157
22403   verifyFormat("foo(operator+, -42);", Style);
22404   verifyFormat("foo(operator++, -42);", Style);
22405   verifyFormat("foo(operator--, -42);", Style);
22406   verifyFormat("foo(-42, operator--);", Style);
22407   verifyFormat("foo(-42, operator, );", Style);
22408   verifyFormat("foo(operator, , -42);", Style);
22409 }
22410 
22411 TEST_F(FormatTest, WhitespaceSensitiveMacros) {
22412   FormatStyle Style = getLLVMStyle();
22413   Style.WhitespaceSensitiveMacros.push_back("FOO");
22414 
22415   // Don't use the helpers here, since 'mess up' will change the whitespace
22416   // and these are all whitespace sensitive by definition
22417   EXPECT_EQ("FOO(String-ized&Messy+But(: :Still)=Intentional);",
22418             format("FOO(String-ized&Messy+But(: :Still)=Intentional);", Style));
22419   EXPECT_EQ(
22420       "FOO(String-ized&Messy+But\\(: :Still)=Intentional);",
22421       format("FOO(String-ized&Messy+But\\(: :Still)=Intentional);", Style));
22422   EXPECT_EQ("FOO(String-ized&Messy+But,: :Still=Intentional);",
22423             format("FOO(String-ized&Messy+But,: :Still=Intentional);", Style));
22424   EXPECT_EQ("FOO(String-ized&Messy+But,: :\n"
22425             "       Still=Intentional);",
22426             format("FOO(String-ized&Messy+But,: :\n"
22427                    "       Still=Intentional);",
22428                    Style));
22429   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
22430   EXPECT_EQ("FOO(String-ized=&Messy+But,: :\n"
22431             "       Still=Intentional);",
22432             format("FOO(String-ized=&Messy+But,: :\n"
22433                    "       Still=Intentional);",
22434                    Style));
22435 
22436   Style.ColumnLimit = 21;
22437   EXPECT_EQ("FOO(String-ized&Messy+But: :Still=Intentional);",
22438             format("FOO(String-ized&Messy+But: :Still=Intentional);", Style));
22439 }
22440 
22441 TEST_F(FormatTest, VeryLongNamespaceCommentSplit) {
22442   // These tests are not in NamespaceFixer because that doesn't
22443   // test its interaction with line wrapping
22444   FormatStyle Style = getLLVMStyleWithColumns(80);
22445   verifyFormat("namespace {\n"
22446                "int i;\n"
22447                "int j;\n"
22448                "} // namespace",
22449                Style);
22450 
22451   verifyFormat("namespace AAA {\n"
22452                "int i;\n"
22453                "int j;\n"
22454                "} // namespace AAA",
22455                Style);
22456 
22457   EXPECT_EQ("namespace Averyveryveryverylongnamespace {\n"
22458             "int i;\n"
22459             "int j;\n"
22460             "} // namespace Averyveryveryverylongnamespace",
22461             format("namespace Averyveryveryverylongnamespace {\n"
22462                    "int i;\n"
22463                    "int j;\n"
22464                    "}",
22465                    Style));
22466 
22467   EXPECT_EQ(
22468       "namespace "
22469       "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n"
22470       "    went::mad::now {\n"
22471       "int i;\n"
22472       "int j;\n"
22473       "} // namespace\n"
22474       "  // "
22475       "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::"
22476       "went::mad::now",
22477       format("namespace "
22478              "would::it::save::you::a::lot::of::time::if_::i::"
22479              "just::gave::up::and_::went::mad::now {\n"
22480              "int i;\n"
22481              "int j;\n"
22482              "}",
22483              Style));
22484 
22485   // This used to duplicate the comment again and again on subsequent runs
22486   EXPECT_EQ(
22487       "namespace "
22488       "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n"
22489       "    went::mad::now {\n"
22490       "int i;\n"
22491       "int j;\n"
22492       "} // namespace\n"
22493       "  // "
22494       "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::"
22495       "went::mad::now",
22496       format("namespace "
22497              "would::it::save::you::a::lot::of::time::if_::i::"
22498              "just::gave::up::and_::went::mad::now {\n"
22499              "int i;\n"
22500              "int j;\n"
22501              "} // namespace\n"
22502              "  // "
22503              "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::"
22504              "and_::went::mad::now",
22505              Style));
22506 }
22507 
22508 TEST_F(FormatTest, LikelyUnlikely) {
22509   FormatStyle Style = getLLVMStyle();
22510 
22511   verifyFormat("if (argc > 5) [[unlikely]] {\n"
22512                "  return 29;\n"
22513                "}",
22514                Style);
22515 
22516   verifyFormat("if (argc > 5) [[likely]] {\n"
22517                "  return 29;\n"
22518                "}",
22519                Style);
22520 
22521   verifyFormat("if (argc > 5) [[unlikely]] {\n"
22522                "  return 29;\n"
22523                "} else [[likely]] {\n"
22524                "  return 42;\n"
22525                "}\n",
22526                Style);
22527 
22528   verifyFormat("if (argc > 5) [[unlikely]] {\n"
22529                "  return 29;\n"
22530                "} else if (argc > 10) [[likely]] {\n"
22531                "  return 99;\n"
22532                "} else {\n"
22533                "  return 42;\n"
22534                "}\n",
22535                Style);
22536 
22537   verifyFormat("if (argc > 5) [[gnu::unused]] {\n"
22538                "  return 29;\n"
22539                "}",
22540                Style);
22541 
22542   verifyFormat("if (argc > 5) [[unlikely]]\n"
22543                "  return 29;\n",
22544                Style);
22545   verifyFormat("if (argc > 5) [[likely]]\n"
22546                "  return 29;\n",
22547                Style);
22548 
22549   Style.AttributeMacros.push_back("UNLIKELY");
22550   Style.AttributeMacros.push_back("LIKELY");
22551   verifyFormat("if (argc > 5) UNLIKELY\n"
22552                "  return 29;\n",
22553                Style);
22554 
22555   verifyFormat("if (argc > 5) UNLIKELY {\n"
22556                "  return 29;\n"
22557                "}",
22558                Style);
22559   verifyFormat("if (argc > 5) UNLIKELY {\n"
22560                "  return 29;\n"
22561                "} else [[likely]] {\n"
22562                "  return 42;\n"
22563                "}\n",
22564                Style);
22565   verifyFormat("if (argc > 5) UNLIKELY {\n"
22566                "  return 29;\n"
22567                "} else LIKELY {\n"
22568                "  return 42;\n"
22569                "}\n",
22570                Style);
22571   verifyFormat("if (argc > 5) [[unlikely]] {\n"
22572                "  return 29;\n"
22573                "} else LIKELY {\n"
22574                "  return 42;\n"
22575                "}\n",
22576                Style);
22577 }
22578 
22579 TEST_F(FormatTest, PenaltyIndentedWhitespace) {
22580   verifyFormat("Constructor()\n"
22581                "    : aaaaaa(aaaaaa), aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
22582                "                          aaaa(aaaaaaaaaaaaaaaaaa, "
22583                "aaaaaaaaaaaaaaaaaat))");
22584   verifyFormat("Constructor()\n"
22585                "    : aaaaaaaaaaaaa(aaaaaa), "
22586                "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)");
22587 
22588   FormatStyle StyleWithWhitespacePenalty = getLLVMStyle();
22589   StyleWithWhitespacePenalty.PenaltyIndentedWhitespace = 5;
22590   verifyFormat("Constructor()\n"
22591                "    : aaaaaa(aaaaaa),\n"
22592                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
22593                "          aaaa(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaat))",
22594                StyleWithWhitespacePenalty);
22595   verifyFormat("Constructor()\n"
22596                "    : aaaaaaaaaaaaa(aaaaaa), "
22597                "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)",
22598                StyleWithWhitespacePenalty);
22599 }
22600 
22601 TEST_F(FormatTest, LLVMDefaultStyle) {
22602   FormatStyle Style = getLLVMStyle();
22603   verifyFormat("extern \"C\" {\n"
22604                "int foo();\n"
22605                "}",
22606                Style);
22607 }
22608 TEST_F(FormatTest, GNUDefaultStyle) {
22609   FormatStyle Style = getGNUStyle();
22610   verifyFormat("extern \"C\"\n"
22611                "{\n"
22612                "  int foo ();\n"
22613                "}",
22614                Style);
22615 }
22616 TEST_F(FormatTest, MozillaDefaultStyle) {
22617   FormatStyle Style = getMozillaStyle();
22618   verifyFormat("extern \"C\"\n"
22619                "{\n"
22620                "  int foo();\n"
22621                "}",
22622                Style);
22623 }
22624 TEST_F(FormatTest, GoogleDefaultStyle) {
22625   FormatStyle Style = getGoogleStyle();
22626   verifyFormat("extern \"C\" {\n"
22627                "int foo();\n"
22628                "}",
22629                Style);
22630 }
22631 TEST_F(FormatTest, ChromiumDefaultStyle) {
22632   FormatStyle Style = getChromiumStyle(FormatStyle::LanguageKind::LK_Cpp);
22633   verifyFormat("extern \"C\" {\n"
22634                "int foo();\n"
22635                "}",
22636                Style);
22637 }
22638 TEST_F(FormatTest, MicrosoftDefaultStyle) {
22639   FormatStyle Style = getMicrosoftStyle(FormatStyle::LanguageKind::LK_Cpp);
22640   verifyFormat("extern \"C\"\n"
22641                "{\n"
22642                "    int foo();\n"
22643                "}",
22644                Style);
22645 }
22646 TEST_F(FormatTest, WebKitDefaultStyle) {
22647   FormatStyle Style = getWebKitStyle();
22648   verifyFormat("extern \"C\" {\n"
22649                "int foo();\n"
22650                "}",
22651                Style);
22652 }
22653 
22654 TEST_F(FormatTest, ConceptsAndRequires) {
22655   FormatStyle Style = getLLVMStyle();
22656   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
22657 
22658   verifyFormat("template <typename T>\n"
22659                "concept Hashable = requires(T a) {\n"
22660                "  { std::hash<T>{}(a) } -> std::convertible_to<std::size_t>;\n"
22661                "};",
22662                Style);
22663   verifyFormat("template <typename T>\n"
22664                "concept EqualityComparable = requires(T a, T b) {\n"
22665                "  { a == b } -> bool;\n"
22666                "};",
22667                Style);
22668   verifyFormat("template <typename T>\n"
22669                "concept EqualityComparable = requires(T a, T b) {\n"
22670                "  { a == b } -> bool;\n"
22671                "  { a != b } -> bool;\n"
22672                "};",
22673                Style);
22674   verifyFormat("template <typename T>\n"
22675                "concept EqualityComparable = requires(T a, T b) {\n"
22676                "  { a == b } -> bool;\n"
22677                "  { a != b } -> bool;\n"
22678                "};",
22679                Style);
22680 
22681   verifyFormat("template <typename It>\n"
22682                "requires Iterator<It>\n"
22683                "void sort(It begin, It end) {\n"
22684                "  //....\n"
22685                "}",
22686                Style);
22687 
22688   verifyFormat("template <typename T>\n"
22689                "concept Large = sizeof(T) > 10;",
22690                Style);
22691 
22692   verifyFormat("template <typename T, typename U>\n"
22693                "concept FooableWith = requires(T t, U u) {\n"
22694                "  typename T::foo_type;\n"
22695                "  { t.foo(u) } -> typename T::foo_type;\n"
22696                "  t++;\n"
22697                "};\n"
22698                "void doFoo(FooableWith<int> auto t) {\n"
22699                "  t.foo(3);\n"
22700                "}",
22701                Style);
22702   verifyFormat("template <typename T>\n"
22703                "concept Context = sizeof(T) == 1;",
22704                Style);
22705   verifyFormat("template <typename T>\n"
22706                "concept Context = is_specialization_of_v<context, T>;",
22707                Style);
22708   verifyFormat("template <typename T>\n"
22709                "concept Node = std::is_object_v<T>;",
22710                Style);
22711   verifyFormat("template <typename T>\n"
22712                "concept Tree = true;",
22713                Style);
22714 
22715   verifyFormat("template <typename T> int g(T i) requires Concept1<I> {\n"
22716                "  //...\n"
22717                "}",
22718                Style);
22719 
22720   verifyFormat(
22721       "template <typename T> int g(T i) requires Concept1<I> && Concept2<I> {\n"
22722       "  //...\n"
22723       "}",
22724       Style);
22725 
22726   verifyFormat(
22727       "template <typename T> int g(T i) requires Concept1<I> || Concept2<I> {\n"
22728       "  //...\n"
22729       "}",
22730       Style);
22731 
22732   verifyFormat("template <typename T>\n"
22733                "veryveryvery_long_return_type g(T i) requires Concept1<I> || "
22734                "Concept2<I> {\n"
22735                "  //...\n"
22736                "}",
22737                Style);
22738 
22739   verifyFormat("template <typename T>\n"
22740                "veryveryvery_long_return_type g(T i) requires Concept1<I> && "
22741                "Concept2<I> {\n"
22742                "  //...\n"
22743                "}",
22744                Style);
22745 
22746   verifyFormat(
22747       "template <typename T>\n"
22748       "veryveryvery_long_return_type g(T i) requires Concept1 && Concept2 {\n"
22749       "  //...\n"
22750       "}",
22751       Style);
22752 
22753   verifyFormat(
22754       "template <typename T>\n"
22755       "veryveryvery_long_return_type g(T i) requires Concept1 || Concept2 {\n"
22756       "  //...\n"
22757       "}",
22758       Style);
22759 
22760   verifyFormat("template <typename It>\n"
22761                "requires Foo<It>() && Bar<It> {\n"
22762                "  //....\n"
22763                "}",
22764                Style);
22765 
22766   verifyFormat("template <typename It>\n"
22767                "requires Foo<Bar<It>>() && Bar<Foo<It, It>> {\n"
22768                "  //....\n"
22769                "}",
22770                Style);
22771 
22772   verifyFormat("template <typename It>\n"
22773                "requires Foo<Bar<It, It>>() && Bar<Foo<It, It>> {\n"
22774                "  //....\n"
22775                "}",
22776                Style);
22777 
22778   verifyFormat(
22779       "template <typename It>\n"
22780       "requires Foo<Bar<It>, Baz<It>>() && Bar<Foo<It>, Baz<It, It>> {\n"
22781       "  //....\n"
22782       "}",
22783       Style);
22784 
22785   Style.IndentRequires = true;
22786   verifyFormat("template <typename It>\n"
22787                "  requires Iterator<It>\n"
22788                "void sort(It begin, It end) {\n"
22789                "  //....\n"
22790                "}",
22791                Style);
22792   verifyFormat("template <std::size index_>\n"
22793                "  requires(index_ < sizeof...(Children_))\n"
22794                "Tree auto &child() {\n"
22795                "  // ...\n"
22796                "}",
22797                Style);
22798 
22799   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
22800   verifyFormat("template <typename T>\n"
22801                "concept Hashable = requires (T a) {\n"
22802                "  { std::hash<T>{}(a) } -> std::convertible_to<std::size_t>;\n"
22803                "};",
22804                Style);
22805 
22806   verifyFormat("template <class T = void>\n"
22807                "  requires EqualityComparable<T> || Same<T, void>\n"
22808                "struct equal_to;",
22809                Style);
22810 
22811   verifyFormat("template <class T>\n"
22812                "  requires requires {\n"
22813                "    T{};\n"
22814                "    T (int);\n"
22815                "  }\n",
22816                Style);
22817 
22818   Style.ColumnLimit = 78;
22819   verifyFormat("template <typename T>\n"
22820                "concept Context = Traits<typename T::traits_type> and\n"
22821                "    Interface<typename T::interface_type> and\n"
22822                "    Request<typename T::request_type> and\n"
22823                "    Response<typename T::response_type> and\n"
22824                "    ContextExtension<typename T::extension_type> and\n"
22825                "    ::std::is_copy_constructable<T> and "
22826                "::std::is_move_constructable<T> and\n"
22827                "    requires (T c) {\n"
22828                "  { c.response; } -> Response;\n"
22829                "} and requires (T c) {\n"
22830                "  { c.request; } -> Request;\n"
22831                "}\n",
22832                Style);
22833 
22834   verifyFormat("template <typename T>\n"
22835                "concept Context = Traits<typename T::traits_type> or\n"
22836                "    Interface<typename T::interface_type> or\n"
22837                "    Request<typename T::request_type> or\n"
22838                "    Response<typename T::response_type> or\n"
22839                "    ContextExtension<typename T::extension_type> or\n"
22840                "    ::std::is_copy_constructable<T> or "
22841                "::std::is_move_constructable<T> or\n"
22842                "    requires (T c) {\n"
22843                "  { c.response; } -> Response;\n"
22844                "} or requires (T c) {\n"
22845                "  { c.request; } -> Request;\n"
22846                "}\n",
22847                Style);
22848 
22849   verifyFormat("template <typename T>\n"
22850                "concept Context = Traits<typename T::traits_type> &&\n"
22851                "    Interface<typename T::interface_type> &&\n"
22852                "    Request<typename T::request_type> &&\n"
22853                "    Response<typename T::response_type> &&\n"
22854                "    ContextExtension<typename T::extension_type> &&\n"
22855                "    ::std::is_copy_constructable<T> && "
22856                "::std::is_move_constructable<T> &&\n"
22857                "    requires (T c) {\n"
22858                "  { c.response; } -> Response;\n"
22859                "} && requires (T c) {\n"
22860                "  { c.request; } -> Request;\n"
22861                "}\n",
22862                Style);
22863 
22864   verifyFormat("template <typename T>\nconcept someConcept = Constraint1<T> && "
22865                "Constraint2<T>;");
22866 
22867   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
22868   Style.BraceWrapping.AfterFunction = true;
22869   Style.BraceWrapping.AfterClass = true;
22870   Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
22871   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
22872   verifyFormat("void Foo () requires (std::copyable<T>)\n"
22873                "{\n"
22874                "  return\n"
22875                "}\n",
22876                Style);
22877 
22878   verifyFormat("void Foo () requires std::copyable<T>\n"
22879                "{\n"
22880                "  return\n"
22881                "}\n",
22882                Style);
22883 
22884   verifyFormat("template <std::semiregular F, std::semiregular... Args>\n"
22885                "  requires (std::invocable<F, std::invoke_result_t<Args>...>)\n"
22886                "struct constant;",
22887                Style);
22888 
22889   verifyFormat("template <std::semiregular F, std::semiregular... Args>\n"
22890                "  requires std::invocable<F, std::invoke_result_t<Args>...>\n"
22891                "struct constant;",
22892                Style);
22893 
22894   verifyFormat("template <class T>\n"
22895                "class plane_with_very_very_very_long_name\n"
22896                "{\n"
22897                "  constexpr plane_with_very_very_very_long_name () requires "
22898                "std::copyable<T>\n"
22899                "      : plane_with_very_very_very_long_name (1)\n"
22900                "  {\n"
22901                "  }\n"
22902                "}\n",
22903                Style);
22904 
22905   verifyFormat("template <class T>\n"
22906                "class plane_with_long_name\n"
22907                "{\n"
22908                "  constexpr plane_with_long_name () requires std::copyable<T>\n"
22909                "      : plane_with_long_name (1)\n"
22910                "  {\n"
22911                "  }\n"
22912                "}\n",
22913                Style);
22914 
22915   Style.BreakBeforeConceptDeclarations = false;
22916   verifyFormat("template <typename T> concept Tree = true;", Style);
22917 
22918   Style.IndentRequires = false;
22919   verifyFormat("template <std::semiregular F, std::semiregular... Args>\n"
22920                "requires (std::invocable<F, std::invoke_result_t<Args>...>) "
22921                "struct constant;",
22922                Style);
22923 }
22924 
22925 TEST_F(FormatTest, StatementAttributeLikeMacros) {
22926   FormatStyle Style = getLLVMStyle();
22927   StringRef Source = "void Foo::slot() {\n"
22928                      "  unsigned char MyChar = 'x';\n"
22929                      "  emit signal(MyChar);\n"
22930                      "  Q_EMIT signal(MyChar);\n"
22931                      "}";
22932 
22933   EXPECT_EQ(Source, format(Source, Style));
22934 
22935   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
22936   EXPECT_EQ("void Foo::slot() {\n"
22937             "  unsigned char MyChar = 'x';\n"
22938             "  emit          signal(MyChar);\n"
22939             "  Q_EMIT signal(MyChar);\n"
22940             "}",
22941             format(Source, Style));
22942 
22943   Style.StatementAttributeLikeMacros.push_back("emit");
22944   EXPECT_EQ(Source, format(Source, Style));
22945 
22946   Style.StatementAttributeLikeMacros = {};
22947   EXPECT_EQ("void Foo::slot() {\n"
22948             "  unsigned char MyChar = 'x';\n"
22949             "  emit          signal(MyChar);\n"
22950             "  Q_EMIT        signal(MyChar);\n"
22951             "}",
22952             format(Source, Style));
22953 }
22954 
22955 TEST_F(FormatTest, IndentAccessModifiers) {
22956   FormatStyle Style = getLLVMStyle();
22957   Style.IndentAccessModifiers = true;
22958   // Members are *two* levels below the record;
22959   // Style.IndentWidth == 2, thus yielding a 4 spaces wide indentation.
22960   verifyFormat("class C {\n"
22961                "    int i;\n"
22962                "};\n",
22963                Style);
22964   verifyFormat("union C {\n"
22965                "    int i;\n"
22966                "    unsigned u;\n"
22967                "};\n",
22968                Style);
22969   // Access modifiers should be indented one level below the record.
22970   verifyFormat("class C {\n"
22971                "  public:\n"
22972                "    int i;\n"
22973                "};\n",
22974                Style);
22975   verifyFormat("struct S {\n"
22976                "  private:\n"
22977                "    class C {\n"
22978                "        int j;\n"
22979                "\n"
22980                "      public:\n"
22981                "        C();\n"
22982                "    };\n"
22983                "\n"
22984                "  public:\n"
22985                "    int i;\n"
22986                "};\n",
22987                Style);
22988   // Enumerations are not records and should be unaffected.
22989   Style.AllowShortEnumsOnASingleLine = false;
22990   verifyFormat("enum class E {\n"
22991                "  A,\n"
22992                "  B\n"
22993                "};\n",
22994                Style);
22995   // Test with a different indentation width;
22996   // also proves that the result is Style.AccessModifierOffset agnostic.
22997   Style.IndentWidth = 3;
22998   verifyFormat("class C {\n"
22999                "   public:\n"
23000                "      int i;\n"
23001                "};\n",
23002                Style);
23003 }
23004 
23005 TEST_F(FormatTest, LimitlessStringsAndComments) {
23006   auto Style = getLLVMStyleWithColumns(0);
23007   constexpr StringRef Code =
23008       "/**\n"
23009       " * This is a multiline comment with quite some long lines, at least for "
23010       "the LLVM Style.\n"
23011       " * We will redo this with strings and line comments. Just to  check if "
23012       "everything is working.\n"
23013       " */\n"
23014       "bool foo() {\n"
23015       "  /* Single line multi line comment. */\n"
23016       "  const std::string String = \"This is a multiline string with quite "
23017       "some long lines, at least for the LLVM Style.\"\n"
23018       "                             \"We already did it with multi line "
23019       "comments, and we will do it with line comments. Just to check if "
23020       "everything is working.\";\n"
23021       "  // This is a line comment (block) with quite some long lines, at "
23022       "least for the LLVM Style.\n"
23023       "  // We already did this with multi line comments and strings. Just to "
23024       "check if everything is working.\n"
23025       "  const std::string SmallString = \"Hello World\";\n"
23026       "  // Small line comment\n"
23027       "  return String.size() > SmallString.size();\n"
23028       "}";
23029   EXPECT_EQ(Code, format(Code, Style));
23030 }
23031 
23032 TEST_F(FormatTest, FormatDecayCopy) {
23033   // error cases from unit tests
23034   verifyFormat("foo(auto())");
23035   verifyFormat("foo(auto{})");
23036   verifyFormat("foo(auto({}))");
23037   verifyFormat("foo(auto{{}})");
23038 
23039   verifyFormat("foo(auto(1))");
23040   verifyFormat("foo(auto{1})");
23041   verifyFormat("foo(new auto(1))");
23042   verifyFormat("foo(new auto{1})");
23043   verifyFormat("decltype(auto(1)) x;");
23044   verifyFormat("decltype(auto{1}) x;");
23045   verifyFormat("auto(x);");
23046   verifyFormat("auto{x};");
23047   verifyFormat("new auto{x};");
23048   verifyFormat("auto{x} = y;");
23049   verifyFormat("auto(x) = y;"); // actually a declaration, but this is clearly
23050                                 // the user's own fault
23051   verifyFormat("integral auto(x) = y;"); // actually a declaration, but this is
23052                                          // clearly the user's own fault
23053   verifyFormat("auto(*p)() = f;");       // actually a declaration; TODO FIXME
23054 }
23055 
23056 TEST_F(FormatTest, Cpp20ModulesSupport) {
23057   FormatStyle Style = getLLVMStyle();
23058   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
23059   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
23060 
23061   verifyFormat("export import foo;", Style);
23062   verifyFormat("export import foo:bar;", Style);
23063   verifyFormat("export import foo.bar;", Style);
23064   verifyFormat("export import foo.bar:baz;", Style);
23065   verifyFormat("export import :bar;", Style);
23066   verifyFormat("export module foo:bar;", Style);
23067   verifyFormat("export module foo;", Style);
23068   verifyFormat("export module foo.bar;", Style);
23069   verifyFormat("export module foo.bar:baz;", Style);
23070   verifyFormat("export import <string_view>;", Style);
23071 
23072   verifyFormat("export type_name var;", Style);
23073   verifyFormat("template <class T> export using A = B<T>;", Style);
23074   verifyFormat("export using A = B;", Style);
23075   verifyFormat("export int func() {\n"
23076                "  foo();\n"
23077                "}",
23078                Style);
23079   verifyFormat("export struct {\n"
23080                "  int foo;\n"
23081                "};",
23082                Style);
23083   verifyFormat("export {\n"
23084                "  int foo;\n"
23085                "};",
23086                Style);
23087   verifyFormat("export export char const *hello() { return \"hello\"; }");
23088 
23089   verifyFormat("import bar;", Style);
23090   verifyFormat("import foo.bar;", Style);
23091   verifyFormat("import foo:bar;", Style);
23092   verifyFormat("import :bar;", Style);
23093   verifyFormat("import <ctime>;", Style);
23094   verifyFormat("import \"header\";", Style);
23095 
23096   verifyFormat("module foo;", Style);
23097   verifyFormat("module foo:bar;", Style);
23098   verifyFormat("module foo.bar;", Style);
23099   verifyFormat("module;", Style);
23100 
23101   verifyFormat("export namespace hi {\n"
23102                "const char *sayhi();\n"
23103                "}",
23104                Style);
23105 
23106   verifyFormat("module :private;", Style);
23107   verifyFormat("import <foo/bar.h>;", Style);
23108   verifyFormat("import foo...bar;", Style);
23109   verifyFormat("import ..........;", Style);
23110   verifyFormat("module foo:private;", Style);
23111   verifyFormat("import a", Style);
23112   verifyFormat("module a", Style);
23113   verifyFormat("export import a", Style);
23114   verifyFormat("export module a", Style);
23115 
23116   verifyFormat("import", Style);
23117   verifyFormat("module", Style);
23118   verifyFormat("export", Style);
23119 }
23120 
23121 TEST_F(FormatTest, CoroutineForCoawait) {
23122   FormatStyle Style = getLLVMStyle();
23123   verifyFormat("for co_await (auto x : range())\n  ;");
23124   verifyFormat("for (auto i : arr) {\n"
23125                "}",
23126                Style);
23127   verifyFormat("for co_await (auto i : arr) {\n"
23128                "}",
23129                Style);
23130   verifyFormat("for co_await (auto i : foo(T{})) {\n"
23131                "}",
23132                Style);
23133 }
23134 
23135 TEST_F(FormatTest, CoroutineCoAwait) {
23136   verifyFormat("int x = co_await foo();");
23137   verifyFormat("int x = (co_await foo());");
23138   verifyFormat("co_await (42);");
23139   verifyFormat("void operator co_await(int);");
23140   verifyFormat("void operator co_await(a);");
23141   verifyFormat("co_await a;");
23142   verifyFormat("co_await missing_await_resume{};");
23143   verifyFormat("co_await a; // comment");
23144   verifyFormat("void test0() { co_await a; }");
23145   verifyFormat("co_await co_await co_await foo();");
23146   verifyFormat("co_await foo().bar();");
23147   verifyFormat("co_await [this]() -> Task { co_return x; }");
23148   verifyFormat("co_await [this](int a, int b) -> Task { co_return co_await "
23149                "foo(); }(x, y);");
23150 
23151   FormatStyle Style = getLLVMStyleWithColumns(40);
23152   verifyFormat("co_await [this](int a, int b) -> Task {\n"
23153                "  co_return co_await foo();\n"
23154                "}(x, y);",
23155                Style);
23156   verifyFormat("co_await;");
23157 }
23158 
23159 TEST_F(FormatTest, CoroutineCoYield) {
23160   verifyFormat("int x = co_yield foo();");
23161   verifyFormat("int x = (co_yield foo());");
23162   verifyFormat("co_yield (42);");
23163   verifyFormat("co_yield {42};");
23164   verifyFormat("co_yield 42;");
23165   verifyFormat("co_yield n++;");
23166   verifyFormat("co_yield ++n;");
23167   verifyFormat("co_yield;");
23168 }
23169 
23170 TEST_F(FormatTest, CoroutineCoReturn) {
23171   verifyFormat("co_return (42);");
23172   verifyFormat("co_return;");
23173   verifyFormat("co_return {};");
23174   verifyFormat("co_return x;");
23175   verifyFormat("co_return co_await foo();");
23176   verifyFormat("co_return co_yield foo();");
23177 }
23178 
23179 TEST_F(FormatTest, EmptyShortBlock) {
23180   auto Style = getLLVMStyle();
23181   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty;
23182 
23183   verifyFormat("try {\n"
23184                "  doA();\n"
23185                "} catch (Exception &e) {\n"
23186                "  e.printStackTrace();\n"
23187                "}\n",
23188                Style);
23189 
23190   verifyFormat("try {\n"
23191                "  doA();\n"
23192                "} catch (Exception &e) {}\n",
23193                Style);
23194 }
23195 
23196 } // namespace
23197 } // namespace format
23198 } // namespace clang
23199