1 //===- unittest/Format/FormatTest.cpp - Formatting unit tests -------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "clang/Format/Format.h"
10 
11 #include "../Tooling/ReplacementTest.h"
12 #include "FormatTestUtils.h"
13 
14 #include "llvm/Support/Debug.h"
15 #include "llvm/Support/MemoryBuffer.h"
16 #include "gtest/gtest.h"
17 
18 #define DEBUG_TYPE "format-test"
19 
20 using clang::tooling::ReplacementTest;
21 using clang::tooling::toReplacements;
22 using testing::ScopedTrace;
23 
24 namespace clang {
25 namespace format {
26 namespace {
27 
28 FormatStyle getGoogleStyle() { return getGoogleStyle(FormatStyle::LK_Cpp); }
29 
30 class FormatTest : public ::testing::Test {
31 protected:
32   enum StatusCheck { SC_ExpectComplete, SC_ExpectIncomplete, SC_DoNotCheck };
33 
34   std::string format(llvm::StringRef Code,
35                      const FormatStyle &Style = getLLVMStyle(),
36                      StatusCheck CheckComplete = SC_ExpectComplete) {
37     LLVM_DEBUG(llvm::errs() << "---\n");
38     LLVM_DEBUG(llvm::errs() << Code << "\n\n");
39     std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size()));
40     FormattingAttemptStatus Status;
41     tooling::Replacements Replaces =
42         reformat(Style, Code, Ranges, "<stdin>", &Status);
43     if (CheckComplete != SC_DoNotCheck) {
44       bool ExpectedCompleteFormat = CheckComplete == SC_ExpectComplete;
45       EXPECT_EQ(ExpectedCompleteFormat, Status.FormatComplete)
46           << Code << "\n\n";
47     }
48     ReplacementCount = Replaces.size();
49     auto Result = applyAllReplacements(Code, Replaces);
50     EXPECT_TRUE(static_cast<bool>(Result));
51     LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
52     return *Result;
53   }
54 
55   FormatStyle getStyleWithColumns(FormatStyle Style, unsigned ColumnLimit) {
56     Style.ColumnLimit = ColumnLimit;
57     return Style;
58   }
59 
60   FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) {
61     return getStyleWithColumns(getLLVMStyle(), ColumnLimit);
62   }
63 
64   FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) {
65     return getStyleWithColumns(getGoogleStyle(), ColumnLimit);
66   }
67 
68   void _verifyFormat(const char *File, int Line, llvm::StringRef Expected,
69                      llvm::StringRef Code,
70                      const FormatStyle &Style = getLLVMStyle()) {
71     ScopedTrace t(File, Line, ::testing::Message() << Code.str());
72     EXPECT_EQ(Expected.str(), format(Expected, Style))
73         << "Expected code is not stable";
74     EXPECT_EQ(Expected.str(), format(Code, Style));
75     if (Style.Language == FormatStyle::LK_Cpp) {
76       // Objective-C++ is a superset of C++, so everything checked for C++
77       // needs to be checked for Objective-C++ as well.
78       FormatStyle ObjCStyle = Style;
79       ObjCStyle.Language = FormatStyle::LK_ObjC;
80       EXPECT_EQ(Expected.str(), format(test::messUp(Code), ObjCStyle));
81     }
82   }
83 
84   void _verifyFormat(const char *File, int Line, llvm::StringRef Code,
85                      const FormatStyle &Style = getLLVMStyle()) {
86     _verifyFormat(File, Line, Code, test::messUp(Code), Style);
87   }
88 
89   void _verifyIncompleteFormat(const char *File, int Line, llvm::StringRef Code,
90                                const FormatStyle &Style = getLLVMStyle()) {
91     ScopedTrace t(File, Line, ::testing::Message() << Code.str());
92     EXPECT_EQ(Code.str(),
93               format(test::messUp(Code), Style, SC_ExpectIncomplete));
94   }
95 
96   void _verifyIndependentOfContext(const char *File, int Line,
97                                    llvm::StringRef Text,
98                                    const FormatStyle &Style = getLLVMStyle()) {
99     _verifyFormat(File, Line, Text, Style);
100     _verifyFormat(File, Line, llvm::Twine("void f() { " + Text + " }").str(),
101                   Style);
102   }
103 
104   /// \brief Verify that clang-format does not crash on the given input.
105   void verifyNoCrash(llvm::StringRef Code,
106                      const FormatStyle &Style = getLLVMStyle()) {
107     format(Code, Style, SC_DoNotCheck);
108   }
109 
110   int ReplacementCount;
111 };
112 
113 #define verifyIndependentOfContext(...)                                        \
114   _verifyIndependentOfContext(__FILE__, __LINE__, __VA_ARGS__)
115 #define verifyIncompleteFormat(...)                                            \
116   _verifyIncompleteFormat(__FILE__, __LINE__, __VA_ARGS__)
117 #define verifyFormat(...) _verifyFormat(__FILE__, __LINE__, __VA_ARGS__)
118 #define verifyGoogleFormat(Code) verifyFormat(Code, getGoogleStyle())
119 
120 TEST_F(FormatTest, MessUp) {
121   EXPECT_EQ("1 2 3", test::messUp("1 2 3"));
122   EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n"));
123   EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc"));
124   EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc"));
125   EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne"));
126 }
127 
128 TEST_F(FormatTest, DefaultLLVMStyleIsCpp) {
129   EXPECT_EQ(FormatStyle::LK_Cpp, getLLVMStyle().Language);
130 }
131 
132 TEST_F(FormatTest, LLVMStyleOverride) {
133   EXPECT_EQ(FormatStyle::LK_Proto,
134             getLLVMStyle(FormatStyle::LK_Proto).Language);
135 }
136 
137 //===----------------------------------------------------------------------===//
138 // Basic function tests.
139 //===----------------------------------------------------------------------===//
140 
141 TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) {
142   EXPECT_EQ(";", format(";"));
143 }
144 
145 TEST_F(FormatTest, FormatsGlobalStatementsAt0) {
146   EXPECT_EQ("int i;", format("  int i;"));
147   EXPECT_EQ("\nint i;", format(" \n\t \v \f  int i;"));
148   EXPECT_EQ("int i;\nint j;", format("    int i; int j;"));
149   EXPECT_EQ("int i;\nint j;", format("    int i;\n  int j;"));
150 }
151 
152 TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) {
153   EXPECT_EQ("int i;", format("int\ni;"));
154 }
155 
156 TEST_F(FormatTest, FormatsNestedBlockStatements) {
157   EXPECT_EQ("{\n  {\n    {}\n  }\n}", format("{{{}}}"));
158 }
159 
160 TEST_F(FormatTest, FormatsNestedCall) {
161   verifyFormat("Method(f1, f2(f3));");
162   verifyFormat("Method(f1(f2, f3()));");
163   verifyFormat("Method(f1(f2, (f3())));");
164 }
165 
166 TEST_F(FormatTest, NestedNameSpecifiers) {
167   verifyFormat("vector<::Type> v;");
168   verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())");
169   verifyFormat("static constexpr bool Bar = decltype(bar())::value;");
170   verifyFormat("static constexpr bool Bar = typeof(bar())::value;");
171   verifyFormat("static constexpr bool Bar = __underlying_type(bar())::value;");
172   verifyFormat("static constexpr bool Bar = _Atomic(bar())::value;");
173   verifyFormat("bool a = 2 < ::SomeFunction();");
174   verifyFormat("ALWAYS_INLINE ::std::string getName();");
175   verifyFormat("some::string getName();");
176 }
177 
178 TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) {
179   EXPECT_EQ("if (a) {\n"
180             "  f();\n"
181             "}",
182             format("if(a){f();}"));
183   EXPECT_EQ(4, ReplacementCount);
184   EXPECT_EQ("if (a) {\n"
185             "  f();\n"
186             "}",
187             format("if (a) {\n"
188                    "  f();\n"
189                    "}"));
190   EXPECT_EQ(0, ReplacementCount);
191   EXPECT_EQ("/*\r\n"
192             "\r\n"
193             "*/\r\n",
194             format("/*\r\n"
195                    "\r\n"
196                    "*/\r\n"));
197   EXPECT_EQ(0, ReplacementCount);
198 }
199 
200 TEST_F(FormatTest, RemovesEmptyLines) {
201   EXPECT_EQ("class C {\n"
202             "  int i;\n"
203             "};",
204             format("class C {\n"
205                    " int i;\n"
206                    "\n"
207                    "};"));
208 
209   // Don't remove empty lines at the start of namespaces or extern "C" blocks.
210   EXPECT_EQ("namespace N {\n"
211             "\n"
212             "int i;\n"
213             "}",
214             format("namespace N {\n"
215                    "\n"
216                    "int    i;\n"
217                    "}",
218                    getGoogleStyle()));
219   EXPECT_EQ("/* something */ namespace N {\n"
220             "\n"
221             "int i;\n"
222             "}",
223             format("/* something */ namespace N {\n"
224                    "\n"
225                    "int    i;\n"
226                    "}",
227                    getGoogleStyle()));
228   EXPECT_EQ("inline namespace N {\n"
229             "\n"
230             "int i;\n"
231             "}",
232             format("inline namespace N {\n"
233                    "\n"
234                    "int    i;\n"
235                    "}",
236                    getGoogleStyle()));
237   EXPECT_EQ("/* something */ inline namespace N {\n"
238             "\n"
239             "int i;\n"
240             "}",
241             format("/* something */ inline namespace N {\n"
242                    "\n"
243                    "int    i;\n"
244                    "}",
245                    getGoogleStyle()));
246   EXPECT_EQ("export namespace N {\n"
247             "\n"
248             "int i;\n"
249             "}",
250             format("export namespace N {\n"
251                    "\n"
252                    "int    i;\n"
253                    "}",
254                    getGoogleStyle()));
255   EXPECT_EQ("extern /**/ \"C\" /**/ {\n"
256             "\n"
257             "int i;\n"
258             "}",
259             format("extern /**/ \"C\" /**/ {\n"
260                    "\n"
261                    "int    i;\n"
262                    "}",
263                    getGoogleStyle()));
264 
265   auto CustomStyle = clang::format::getLLVMStyle();
266   CustomStyle.BreakBeforeBraces = clang::format::FormatStyle::BS_Custom;
267   CustomStyle.BraceWrapping.AfterNamespace = true;
268   CustomStyle.KeepEmptyLinesAtTheStartOfBlocks = false;
269   EXPECT_EQ("namespace N\n"
270             "{\n"
271             "\n"
272             "int i;\n"
273             "}",
274             format("namespace N\n"
275                    "{\n"
276                    "\n"
277                    "\n"
278                    "int    i;\n"
279                    "}",
280                    CustomStyle));
281   EXPECT_EQ("/* something */ namespace N\n"
282             "{\n"
283             "\n"
284             "int i;\n"
285             "}",
286             format("/* something */ namespace N {\n"
287                    "\n"
288                    "\n"
289                    "int    i;\n"
290                    "}",
291                    CustomStyle));
292   EXPECT_EQ("inline namespace N\n"
293             "{\n"
294             "\n"
295             "int i;\n"
296             "}",
297             format("inline namespace N\n"
298                    "{\n"
299                    "\n"
300                    "\n"
301                    "int    i;\n"
302                    "}",
303                    CustomStyle));
304   EXPECT_EQ("/* something */ inline namespace N\n"
305             "{\n"
306             "\n"
307             "int i;\n"
308             "}",
309             format("/* something */ inline namespace N\n"
310                    "{\n"
311                    "\n"
312                    "int    i;\n"
313                    "}",
314                    CustomStyle));
315   EXPECT_EQ("export namespace N\n"
316             "{\n"
317             "\n"
318             "int i;\n"
319             "}",
320             format("export namespace N\n"
321                    "{\n"
322                    "\n"
323                    "int    i;\n"
324                    "}",
325                    CustomStyle));
326   EXPECT_EQ("namespace a\n"
327             "{\n"
328             "namespace b\n"
329             "{\n"
330             "\n"
331             "class AA {};\n"
332             "\n"
333             "} // namespace b\n"
334             "} // namespace a\n",
335             format("namespace a\n"
336                    "{\n"
337                    "namespace b\n"
338                    "{\n"
339                    "\n"
340                    "\n"
341                    "class AA {};\n"
342                    "\n"
343                    "\n"
344                    "}\n"
345                    "}\n",
346                    CustomStyle));
347   EXPECT_EQ("namespace A /* comment */\n"
348             "{\n"
349             "class B {}\n"
350             "} // namespace A",
351             format("namespace A /* comment */ { class B {} }", CustomStyle));
352   EXPECT_EQ("namespace A\n"
353             "{ /* comment */\n"
354             "class B {}\n"
355             "} // namespace A",
356             format("namespace A {/* comment */ class B {} }", CustomStyle));
357   EXPECT_EQ("namespace A\n"
358             "{ /* comment */\n"
359             "\n"
360             "class B {}\n"
361             "\n"
362             ""
363             "} // namespace A",
364             format("namespace A { /* comment */\n"
365                    "\n"
366                    "\n"
367                    "class B {}\n"
368                    "\n"
369                    "\n"
370                    "}",
371                    CustomStyle));
372   EXPECT_EQ("namespace A /* comment */\n"
373             "{\n"
374             "\n"
375             "class B {}\n"
376             "\n"
377             "} // namespace A",
378             format("namespace A/* comment */ {\n"
379                    "\n"
380                    "\n"
381                    "class B {}\n"
382                    "\n"
383                    "\n"
384                    "}",
385                    CustomStyle));
386 
387   // ...but do keep inlining and removing empty lines for non-block extern "C"
388   // functions.
389   verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle());
390   EXPECT_EQ("extern \"C\" int f() {\n"
391             "  int i = 42;\n"
392             "  return i;\n"
393             "}",
394             format("extern \"C\" int f() {\n"
395                    "\n"
396                    "  int i = 42;\n"
397                    "  return i;\n"
398                    "}",
399                    getGoogleStyle()));
400 
401   // Remove empty lines at the beginning and end of blocks.
402   EXPECT_EQ("void f() {\n"
403             "\n"
404             "  if (a) {\n"
405             "\n"
406             "    f();\n"
407             "  }\n"
408             "}",
409             format("void f() {\n"
410                    "\n"
411                    "  if (a) {\n"
412                    "\n"
413                    "    f();\n"
414                    "\n"
415                    "  }\n"
416                    "\n"
417                    "}",
418                    getLLVMStyle()));
419   EXPECT_EQ("void f() {\n"
420             "  if (a) {\n"
421             "    f();\n"
422             "  }\n"
423             "}",
424             format("void f() {\n"
425                    "\n"
426                    "  if (a) {\n"
427                    "\n"
428                    "    f();\n"
429                    "\n"
430                    "  }\n"
431                    "\n"
432                    "}",
433                    getGoogleStyle()));
434 
435   // Don't remove empty lines in more complex control statements.
436   EXPECT_EQ("void f() {\n"
437             "  if (a) {\n"
438             "    f();\n"
439             "\n"
440             "  } else if (b) {\n"
441             "    f();\n"
442             "  }\n"
443             "}",
444             format("void f() {\n"
445                    "  if (a) {\n"
446                    "    f();\n"
447                    "\n"
448                    "  } else if (b) {\n"
449                    "    f();\n"
450                    "\n"
451                    "  }\n"
452                    "\n"
453                    "}"));
454 
455   // Don't remove empty lines before namespace endings.
456   FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle();
457   LLVMWithNoNamespaceFix.FixNamespaceComments = false;
458   EXPECT_EQ("namespace {\n"
459             "int i;\n"
460             "\n"
461             "}",
462             format("namespace {\n"
463                    "int i;\n"
464                    "\n"
465                    "}",
466                    LLVMWithNoNamespaceFix));
467   EXPECT_EQ("namespace {\n"
468             "int i;\n"
469             "}",
470             format("namespace {\n"
471                    "int i;\n"
472                    "}",
473                    LLVMWithNoNamespaceFix));
474   EXPECT_EQ("namespace {\n"
475             "int i;\n"
476             "\n"
477             "};",
478             format("namespace {\n"
479                    "int i;\n"
480                    "\n"
481                    "};",
482                    LLVMWithNoNamespaceFix));
483   EXPECT_EQ("namespace {\n"
484             "int i;\n"
485             "};",
486             format("namespace {\n"
487                    "int i;\n"
488                    "};",
489                    LLVMWithNoNamespaceFix));
490   EXPECT_EQ("namespace {\n"
491             "int i;\n"
492             "\n"
493             "}",
494             format("namespace {\n"
495                    "int i;\n"
496                    "\n"
497                    "}"));
498   EXPECT_EQ("namespace {\n"
499             "int i;\n"
500             "\n"
501             "} // namespace",
502             format("namespace {\n"
503                    "int i;\n"
504                    "\n"
505                    "}  // namespace"));
506 
507   FormatStyle Style = getLLVMStyle();
508   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
509   Style.MaxEmptyLinesToKeep = 2;
510   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
511   Style.BraceWrapping.AfterClass = true;
512   Style.BraceWrapping.AfterFunction = true;
513   Style.KeepEmptyLinesAtTheStartOfBlocks = false;
514 
515   EXPECT_EQ("class Foo\n"
516             "{\n"
517             "  Foo() {}\n"
518             "\n"
519             "  void funk() {}\n"
520             "};",
521             format("class Foo\n"
522                    "{\n"
523                    "  Foo()\n"
524                    "  {\n"
525                    "  }\n"
526                    "\n"
527                    "  void funk() {}\n"
528                    "};",
529                    Style));
530 }
531 
532 TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) {
533   verifyFormat("x = (a) and (b);");
534   verifyFormat("x = (a) or (b);");
535   verifyFormat("x = (a) bitand (b);");
536   verifyFormat("x = (a) bitor (b);");
537   verifyFormat("x = (a) not_eq (b);");
538   verifyFormat("x = (a) and_eq (b);");
539   verifyFormat("x = (a) or_eq (b);");
540   verifyFormat("x = (a) xor (b);");
541 }
542 
543 TEST_F(FormatTest, RecognizesUnaryOperatorKeywords) {
544   verifyFormat("x = compl(a);");
545   verifyFormat("x = not(a);");
546   verifyFormat("x = bitand(a);");
547   // Unary operator must not be merged with the next identifier
548   verifyFormat("x = compl a;");
549   verifyFormat("x = not a;");
550   verifyFormat("x = bitand a;");
551 }
552 
553 //===----------------------------------------------------------------------===//
554 // Tests for control statements.
555 //===----------------------------------------------------------------------===//
556 
557 TEST_F(FormatTest, FormatIfWithoutCompoundStatement) {
558   verifyFormat("if (true)\n  f();\ng();");
559   verifyFormat("if (a)\n  if (b)\n    if (c)\n      g();\nh();");
560   verifyFormat("if (a)\n  if (b) {\n    f();\n  }\ng();");
561   verifyFormat("if constexpr (true)\n"
562                "  f();\ng();");
563   verifyFormat("if CONSTEXPR (true)\n"
564                "  f();\ng();");
565   verifyFormat("if constexpr (a)\n"
566                "  if constexpr (b)\n"
567                "    if constexpr (c)\n"
568                "      g();\n"
569                "h();");
570   verifyFormat("if CONSTEXPR (a)\n"
571                "  if CONSTEXPR (b)\n"
572                "    if CONSTEXPR (c)\n"
573                "      g();\n"
574                "h();");
575   verifyFormat("if constexpr (a)\n"
576                "  if constexpr (b) {\n"
577                "    f();\n"
578                "  }\n"
579                "g();");
580   verifyFormat("if CONSTEXPR (a)\n"
581                "  if CONSTEXPR (b) {\n"
582                "    f();\n"
583                "  }\n"
584                "g();");
585 
586   verifyFormat("if (a)\n"
587                "  g();");
588   verifyFormat("if (a) {\n"
589                "  g()\n"
590                "};");
591   verifyFormat("if (a)\n"
592                "  g();\n"
593                "else\n"
594                "  g();");
595   verifyFormat("if (a) {\n"
596                "  g();\n"
597                "} else\n"
598                "  g();");
599   verifyFormat("if (a)\n"
600                "  g();\n"
601                "else {\n"
602                "  g();\n"
603                "}");
604   verifyFormat("if (a) {\n"
605                "  g();\n"
606                "} else {\n"
607                "  g();\n"
608                "}");
609   verifyFormat("if (a)\n"
610                "  g();\n"
611                "else if (b)\n"
612                "  g();\n"
613                "else\n"
614                "  g();");
615   verifyFormat("if (a) {\n"
616                "  g();\n"
617                "} else if (b)\n"
618                "  g();\n"
619                "else\n"
620                "  g();");
621   verifyFormat("if (a)\n"
622                "  g();\n"
623                "else if (b) {\n"
624                "  g();\n"
625                "} else\n"
626                "  g();");
627   verifyFormat("if (a)\n"
628                "  g();\n"
629                "else if (b)\n"
630                "  g();\n"
631                "else {\n"
632                "  g();\n"
633                "}");
634   verifyFormat("if (a)\n"
635                "  g();\n"
636                "else if (b) {\n"
637                "  g();\n"
638                "} else {\n"
639                "  g();\n"
640                "}");
641   verifyFormat("if (a) {\n"
642                "  g();\n"
643                "} else if (b) {\n"
644                "  g();\n"
645                "} else {\n"
646                "  g();\n"
647                "}");
648 
649   FormatStyle AllowsMergedIf = getLLVMStyle();
650   AllowsMergedIf.IfMacros.push_back("MYIF");
651   AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left;
652   AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
653       FormatStyle::SIS_WithoutElse;
654   verifyFormat("if (a)\n"
655                "  // comment\n"
656                "  f();",
657                AllowsMergedIf);
658   verifyFormat("{\n"
659                "  if (a)\n"
660                "  label:\n"
661                "    f();\n"
662                "}",
663                AllowsMergedIf);
664   verifyFormat("#define A \\\n"
665                "  if (a)  \\\n"
666                "  label:  \\\n"
667                "    f()",
668                AllowsMergedIf);
669   verifyFormat("if (a)\n"
670                "  ;",
671                AllowsMergedIf);
672   verifyFormat("if (a)\n"
673                "  if (b) return;",
674                AllowsMergedIf);
675 
676   verifyFormat("if (a) // Can't merge this\n"
677                "  f();\n",
678                AllowsMergedIf);
679   verifyFormat("if (a) /* still don't merge */\n"
680                "  f();",
681                AllowsMergedIf);
682   verifyFormat("if (a) { // Never merge this\n"
683                "  f();\n"
684                "}",
685                AllowsMergedIf);
686   verifyFormat("if (a) { /* Never merge this */\n"
687                "  f();\n"
688                "}",
689                AllowsMergedIf);
690   verifyFormat("MYIF (a)\n"
691                "  // comment\n"
692                "  f();",
693                AllowsMergedIf);
694   verifyFormat("{\n"
695                "  MYIF (a)\n"
696                "  label:\n"
697                "    f();\n"
698                "}",
699                AllowsMergedIf);
700   verifyFormat("#define A  \\\n"
701                "  MYIF (a) \\\n"
702                "  label:   \\\n"
703                "    f()",
704                AllowsMergedIf);
705   verifyFormat("MYIF (a)\n"
706                "  ;",
707                AllowsMergedIf);
708   verifyFormat("MYIF (a)\n"
709                "  MYIF (b) return;",
710                AllowsMergedIf);
711 
712   verifyFormat("MYIF (a) // Can't merge this\n"
713                "  f();\n",
714                AllowsMergedIf);
715   verifyFormat("MYIF (a) /* still don't merge */\n"
716                "  f();",
717                AllowsMergedIf);
718   verifyFormat("MYIF (a) { // Never merge this\n"
719                "  f();\n"
720                "}",
721                AllowsMergedIf);
722   verifyFormat("MYIF (a) { /* Never merge this */\n"
723                "  f();\n"
724                "}",
725                AllowsMergedIf);
726 
727   AllowsMergedIf.ColumnLimit = 14;
728   // Where line-lengths matter, a 2-letter synonym that maintains line length.
729   // Not IF to avoid any confusion that IF is somehow special.
730   AllowsMergedIf.IfMacros.push_back("FI");
731   verifyFormat("if (a) return;", AllowsMergedIf);
732   verifyFormat("if (aaaaaaaaa)\n"
733                "  return;",
734                AllowsMergedIf);
735   verifyFormat("FI (a) return;", AllowsMergedIf);
736   verifyFormat("FI (aaaaaaaaa)\n"
737                "  return;",
738                AllowsMergedIf);
739 
740   AllowsMergedIf.ColumnLimit = 13;
741   verifyFormat("if (a)\n  return;", AllowsMergedIf);
742   verifyFormat("FI (a)\n  return;", AllowsMergedIf);
743 
744   FormatStyle AllowsMergedIfElse = getLLVMStyle();
745   AllowsMergedIfElse.IfMacros.push_back("MYIF");
746   AllowsMergedIfElse.AllowShortIfStatementsOnASingleLine =
747       FormatStyle::SIS_AllIfsAndElse;
748   verifyFormat("if (a)\n"
749                "  // comment\n"
750                "  f();\n"
751                "else\n"
752                "  // comment\n"
753                "  f();",
754                AllowsMergedIfElse);
755   verifyFormat("{\n"
756                "  if (a)\n"
757                "  label:\n"
758                "    f();\n"
759                "  else\n"
760                "  label:\n"
761                "    f();\n"
762                "}",
763                AllowsMergedIfElse);
764   verifyFormat("if (a)\n"
765                "  ;\n"
766                "else\n"
767                "  ;",
768                AllowsMergedIfElse);
769   verifyFormat("if (a) {\n"
770                "} else {\n"
771                "}",
772                AllowsMergedIfElse);
773   verifyFormat("if (a) return;\n"
774                "else if (b) return;\n"
775                "else return;",
776                AllowsMergedIfElse);
777   verifyFormat("if (a) {\n"
778                "} else return;",
779                AllowsMergedIfElse);
780   verifyFormat("if (a) {\n"
781                "} else if (b) return;\n"
782                "else return;",
783                AllowsMergedIfElse);
784   verifyFormat("if (a) return;\n"
785                "else if (b) {\n"
786                "} else return;",
787                AllowsMergedIfElse);
788   verifyFormat("if (a)\n"
789                "  if (b) return;\n"
790                "  else return;",
791                AllowsMergedIfElse);
792   verifyFormat("if constexpr (a)\n"
793                "  if constexpr (b) return;\n"
794                "  else if constexpr (c) return;\n"
795                "  else return;",
796                AllowsMergedIfElse);
797   verifyFormat("MYIF (a)\n"
798                "  // comment\n"
799                "  f();\n"
800                "else\n"
801                "  // comment\n"
802                "  f();",
803                AllowsMergedIfElse);
804   verifyFormat("{\n"
805                "  MYIF (a)\n"
806                "  label:\n"
807                "    f();\n"
808                "  else\n"
809                "  label:\n"
810                "    f();\n"
811                "}",
812                AllowsMergedIfElse);
813   verifyFormat("MYIF (a)\n"
814                "  ;\n"
815                "else\n"
816                "  ;",
817                AllowsMergedIfElse);
818   verifyFormat("MYIF (a) {\n"
819                "} else {\n"
820                "}",
821                AllowsMergedIfElse);
822   verifyFormat("MYIF (a) return;\n"
823                "else MYIF (b) return;\n"
824                "else return;",
825                AllowsMergedIfElse);
826   verifyFormat("MYIF (a) {\n"
827                "} else return;",
828                AllowsMergedIfElse);
829   verifyFormat("MYIF (a) {\n"
830                "} else MYIF (b) return;\n"
831                "else return;",
832                AllowsMergedIfElse);
833   verifyFormat("MYIF (a) return;\n"
834                "else MYIF (b) {\n"
835                "} else return;",
836                AllowsMergedIfElse);
837   verifyFormat("MYIF (a)\n"
838                "  MYIF (b) return;\n"
839                "  else return;",
840                AllowsMergedIfElse);
841   verifyFormat("MYIF constexpr (a)\n"
842                "  MYIF constexpr (b) return;\n"
843                "  else MYIF constexpr (c) return;\n"
844                "  else return;",
845                AllowsMergedIfElse);
846 }
847 
848 TEST_F(FormatTest, FormatIfWithoutCompoundStatementButElseWith) {
849   FormatStyle AllowsMergedIf = getLLVMStyle();
850   AllowsMergedIf.IfMacros.push_back("MYIF");
851   AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left;
852   AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
853       FormatStyle::SIS_WithoutElse;
854   verifyFormat("if (a)\n"
855                "  f();\n"
856                "else {\n"
857                "  g();\n"
858                "}",
859                AllowsMergedIf);
860   verifyFormat("if (a)\n"
861                "  f();\n"
862                "else\n"
863                "  g();\n",
864                AllowsMergedIf);
865 
866   verifyFormat("if (a) g();", AllowsMergedIf);
867   verifyFormat("if (a) {\n"
868                "  g()\n"
869                "};",
870                AllowsMergedIf);
871   verifyFormat("if (a)\n"
872                "  g();\n"
873                "else\n"
874                "  g();",
875                AllowsMergedIf);
876   verifyFormat("if (a) {\n"
877                "  g();\n"
878                "} else\n"
879                "  g();",
880                AllowsMergedIf);
881   verifyFormat("if (a)\n"
882                "  g();\n"
883                "else {\n"
884                "  g();\n"
885                "}",
886                AllowsMergedIf);
887   verifyFormat("if (a) {\n"
888                "  g();\n"
889                "} else {\n"
890                "  g();\n"
891                "}",
892                AllowsMergedIf);
893   verifyFormat("if (a)\n"
894                "  g();\n"
895                "else if (b)\n"
896                "  g();\n"
897                "else\n"
898                "  g();",
899                AllowsMergedIf);
900   verifyFormat("if (a) {\n"
901                "  g();\n"
902                "} else if (b)\n"
903                "  g();\n"
904                "else\n"
905                "  g();",
906                AllowsMergedIf);
907   verifyFormat("if (a)\n"
908                "  g();\n"
909                "else if (b) {\n"
910                "  g();\n"
911                "} else\n"
912                "  g();",
913                AllowsMergedIf);
914   verifyFormat("if (a)\n"
915                "  g();\n"
916                "else if (b)\n"
917                "  g();\n"
918                "else {\n"
919                "  g();\n"
920                "}",
921                AllowsMergedIf);
922   verifyFormat("if (a)\n"
923                "  g();\n"
924                "else if (b) {\n"
925                "  g();\n"
926                "} else {\n"
927                "  g();\n"
928                "}",
929                AllowsMergedIf);
930   verifyFormat("if (a) {\n"
931                "  g();\n"
932                "} else if (b) {\n"
933                "  g();\n"
934                "} else {\n"
935                "  g();\n"
936                "}",
937                AllowsMergedIf);
938   verifyFormat("MYIF (a)\n"
939                "  f();\n"
940                "else {\n"
941                "  g();\n"
942                "}",
943                AllowsMergedIf);
944   verifyFormat("MYIF (a)\n"
945                "  f();\n"
946                "else\n"
947                "  g();\n",
948                AllowsMergedIf);
949 
950   verifyFormat("MYIF (a) g();", AllowsMergedIf);
951   verifyFormat("MYIF (a) {\n"
952                "  g()\n"
953                "};",
954                AllowsMergedIf);
955   verifyFormat("MYIF (a)\n"
956                "  g();\n"
957                "else\n"
958                "  g();",
959                AllowsMergedIf);
960   verifyFormat("MYIF (a) {\n"
961                "  g();\n"
962                "} else\n"
963                "  g();",
964                AllowsMergedIf);
965   verifyFormat("MYIF (a)\n"
966                "  g();\n"
967                "else {\n"
968                "  g();\n"
969                "}",
970                AllowsMergedIf);
971   verifyFormat("MYIF (a) {\n"
972                "  g();\n"
973                "} else {\n"
974                "  g();\n"
975                "}",
976                AllowsMergedIf);
977   verifyFormat("MYIF (a)\n"
978                "  g();\n"
979                "else MYIF (b)\n"
980                "  g();\n"
981                "else\n"
982                "  g();",
983                AllowsMergedIf);
984   verifyFormat("MYIF (a)\n"
985                "  g();\n"
986                "else if (b)\n"
987                "  g();\n"
988                "else\n"
989                "  g();",
990                AllowsMergedIf);
991   verifyFormat("MYIF (a) {\n"
992                "  g();\n"
993                "} else MYIF (b)\n"
994                "  g();\n"
995                "else\n"
996                "  g();",
997                AllowsMergedIf);
998   verifyFormat("MYIF (a) {\n"
999                "  g();\n"
1000                "} else if (b)\n"
1001                "  g();\n"
1002                "else\n"
1003                "  g();",
1004                AllowsMergedIf);
1005   verifyFormat("MYIF (a)\n"
1006                "  g();\n"
1007                "else MYIF (b) {\n"
1008                "  g();\n"
1009                "} else\n"
1010                "  g();",
1011                AllowsMergedIf);
1012   verifyFormat("MYIF (a)\n"
1013                "  g();\n"
1014                "else if (b) {\n"
1015                "  g();\n"
1016                "} else\n"
1017                "  g();",
1018                AllowsMergedIf);
1019   verifyFormat("MYIF (a)\n"
1020                "  g();\n"
1021                "else MYIF (b)\n"
1022                "  g();\n"
1023                "else {\n"
1024                "  g();\n"
1025                "}",
1026                AllowsMergedIf);
1027   verifyFormat("MYIF (a)\n"
1028                "  g();\n"
1029                "else if (b)\n"
1030                "  g();\n"
1031                "else {\n"
1032                "  g();\n"
1033                "}",
1034                AllowsMergedIf);
1035   verifyFormat("MYIF (a)\n"
1036                "  g();\n"
1037                "else MYIF (b) {\n"
1038                "  g();\n"
1039                "} else {\n"
1040                "  g();\n"
1041                "}",
1042                AllowsMergedIf);
1043   verifyFormat("MYIF (a)\n"
1044                "  g();\n"
1045                "else if (b) {\n"
1046                "  g();\n"
1047                "} else {\n"
1048                "  g();\n"
1049                "}",
1050                AllowsMergedIf);
1051   verifyFormat("MYIF (a) {\n"
1052                "  g();\n"
1053                "} else MYIF (b) {\n"
1054                "  g();\n"
1055                "} else {\n"
1056                "  g();\n"
1057                "}",
1058                AllowsMergedIf);
1059   verifyFormat("MYIF (a) {\n"
1060                "  g();\n"
1061                "} else if (b) {\n"
1062                "  g();\n"
1063                "} else {\n"
1064                "  g();\n"
1065                "}",
1066                AllowsMergedIf);
1067 
1068   AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
1069       FormatStyle::SIS_OnlyFirstIf;
1070 
1071   verifyFormat("if (a) f();\n"
1072                "else {\n"
1073                "  g();\n"
1074                "}",
1075                AllowsMergedIf);
1076   verifyFormat("if (a) f();\n"
1077                "else {\n"
1078                "  if (a) f();\n"
1079                "  else {\n"
1080                "    g();\n"
1081                "  }\n"
1082                "  g();\n"
1083                "}",
1084                AllowsMergedIf);
1085 
1086   verifyFormat("if (a) g();", AllowsMergedIf);
1087   verifyFormat("if (a) {\n"
1088                "  g()\n"
1089                "};",
1090                AllowsMergedIf);
1091   verifyFormat("if (a) g();\n"
1092                "else\n"
1093                "  g();",
1094                AllowsMergedIf);
1095   verifyFormat("if (a) {\n"
1096                "  g();\n"
1097                "} else\n"
1098                "  g();",
1099                AllowsMergedIf);
1100   verifyFormat("if (a) g();\n"
1101                "else {\n"
1102                "  g();\n"
1103                "}",
1104                AllowsMergedIf);
1105   verifyFormat("if (a) {\n"
1106                "  g();\n"
1107                "} else {\n"
1108                "  g();\n"
1109                "}",
1110                AllowsMergedIf);
1111   verifyFormat("if (a) g();\n"
1112                "else if (b)\n"
1113                "  g();\n"
1114                "else\n"
1115                "  g();",
1116                AllowsMergedIf);
1117   verifyFormat("if (a) {\n"
1118                "  g();\n"
1119                "} else if (b)\n"
1120                "  g();\n"
1121                "else\n"
1122                "  g();",
1123                AllowsMergedIf);
1124   verifyFormat("if (a) g();\n"
1125                "else if (b) {\n"
1126                "  g();\n"
1127                "} else\n"
1128                "  g();",
1129                AllowsMergedIf);
1130   verifyFormat("if (a) g();\n"
1131                "else if (b)\n"
1132                "  g();\n"
1133                "else {\n"
1134                "  g();\n"
1135                "}",
1136                AllowsMergedIf);
1137   verifyFormat("if (a) g();\n"
1138                "else if (b) {\n"
1139                "  g();\n"
1140                "} else {\n"
1141                "  g();\n"
1142                "}",
1143                AllowsMergedIf);
1144   verifyFormat("if (a) {\n"
1145                "  g();\n"
1146                "} else if (b) {\n"
1147                "  g();\n"
1148                "} else {\n"
1149                "  g();\n"
1150                "}",
1151                AllowsMergedIf);
1152   verifyFormat("MYIF (a) f();\n"
1153                "else {\n"
1154                "  g();\n"
1155                "}",
1156                AllowsMergedIf);
1157   verifyFormat("MYIF (a) f();\n"
1158                "else {\n"
1159                "  if (a) f();\n"
1160                "  else {\n"
1161                "    g();\n"
1162                "  }\n"
1163                "  g();\n"
1164                "}",
1165                AllowsMergedIf);
1166 
1167   verifyFormat("MYIF (a) g();", AllowsMergedIf);
1168   verifyFormat("MYIF (a) {\n"
1169                "  g()\n"
1170                "};",
1171                AllowsMergedIf);
1172   verifyFormat("MYIF (a) g();\n"
1173                "else\n"
1174                "  g();",
1175                AllowsMergedIf);
1176   verifyFormat("MYIF (a) {\n"
1177                "  g();\n"
1178                "} else\n"
1179                "  g();",
1180                AllowsMergedIf);
1181   verifyFormat("MYIF (a) g();\n"
1182                "else {\n"
1183                "  g();\n"
1184                "}",
1185                AllowsMergedIf);
1186   verifyFormat("MYIF (a) {\n"
1187                "  g();\n"
1188                "} else {\n"
1189                "  g();\n"
1190                "}",
1191                AllowsMergedIf);
1192   verifyFormat("MYIF (a) g();\n"
1193                "else MYIF (b)\n"
1194                "  g();\n"
1195                "else\n"
1196                "  g();",
1197                AllowsMergedIf);
1198   verifyFormat("MYIF (a) g();\n"
1199                "else if (b)\n"
1200                "  g();\n"
1201                "else\n"
1202                "  g();",
1203                AllowsMergedIf);
1204   verifyFormat("MYIF (a) {\n"
1205                "  g();\n"
1206                "} else MYIF (b)\n"
1207                "  g();\n"
1208                "else\n"
1209                "  g();",
1210                AllowsMergedIf);
1211   verifyFormat("MYIF (a) {\n"
1212                "  g();\n"
1213                "} else if (b)\n"
1214                "  g();\n"
1215                "else\n"
1216                "  g();",
1217                AllowsMergedIf);
1218   verifyFormat("MYIF (a) g();\n"
1219                "else MYIF (b) {\n"
1220                "  g();\n"
1221                "} else\n"
1222                "  g();",
1223                AllowsMergedIf);
1224   verifyFormat("MYIF (a) g();\n"
1225                "else if (b) {\n"
1226                "  g();\n"
1227                "} else\n"
1228                "  g();",
1229                AllowsMergedIf);
1230   verifyFormat("MYIF (a) g();\n"
1231                "else MYIF (b)\n"
1232                "  g();\n"
1233                "else {\n"
1234                "  g();\n"
1235                "}",
1236                AllowsMergedIf);
1237   verifyFormat("MYIF (a) g();\n"
1238                "else if (b)\n"
1239                "  g();\n"
1240                "else {\n"
1241                "  g();\n"
1242                "}",
1243                AllowsMergedIf);
1244   verifyFormat("MYIF (a) g();\n"
1245                "else MYIF (b) {\n"
1246                "  g();\n"
1247                "} else {\n"
1248                "  g();\n"
1249                "}",
1250                AllowsMergedIf);
1251   verifyFormat("MYIF (a) g();\n"
1252                "else if (b) {\n"
1253                "  g();\n"
1254                "} else {\n"
1255                "  g();\n"
1256                "}",
1257                AllowsMergedIf);
1258   verifyFormat("MYIF (a) {\n"
1259                "  g();\n"
1260                "} else MYIF (b) {\n"
1261                "  g();\n"
1262                "} else {\n"
1263                "  g();\n"
1264                "}",
1265                AllowsMergedIf);
1266   verifyFormat("MYIF (a) {\n"
1267                "  g();\n"
1268                "} else if (b) {\n"
1269                "  g();\n"
1270                "} else {\n"
1271                "  g();\n"
1272                "}",
1273                AllowsMergedIf);
1274 
1275   AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
1276       FormatStyle::SIS_AllIfsAndElse;
1277 
1278   verifyFormat("if (a) f();\n"
1279                "else {\n"
1280                "  g();\n"
1281                "}",
1282                AllowsMergedIf);
1283   verifyFormat("if (a) f();\n"
1284                "else {\n"
1285                "  if (a) f();\n"
1286                "  else {\n"
1287                "    g();\n"
1288                "  }\n"
1289                "  g();\n"
1290                "}",
1291                AllowsMergedIf);
1292 
1293   verifyFormat("if (a) g();", AllowsMergedIf);
1294   verifyFormat("if (a) {\n"
1295                "  g()\n"
1296                "};",
1297                AllowsMergedIf);
1298   verifyFormat("if (a) g();\n"
1299                "else g();",
1300                AllowsMergedIf);
1301   verifyFormat("if (a) {\n"
1302                "  g();\n"
1303                "} else g();",
1304                AllowsMergedIf);
1305   verifyFormat("if (a) g();\n"
1306                "else {\n"
1307                "  g();\n"
1308                "}",
1309                AllowsMergedIf);
1310   verifyFormat("if (a) {\n"
1311                "  g();\n"
1312                "} else {\n"
1313                "  g();\n"
1314                "}",
1315                AllowsMergedIf);
1316   verifyFormat("if (a) g();\n"
1317                "else if (b) g();\n"
1318                "else g();",
1319                AllowsMergedIf);
1320   verifyFormat("if (a) {\n"
1321                "  g();\n"
1322                "} else if (b) g();\n"
1323                "else g();",
1324                AllowsMergedIf);
1325   verifyFormat("if (a) g();\n"
1326                "else if (b) {\n"
1327                "  g();\n"
1328                "} else g();",
1329                AllowsMergedIf);
1330   verifyFormat("if (a) g();\n"
1331                "else if (b) g();\n"
1332                "else {\n"
1333                "  g();\n"
1334                "}",
1335                AllowsMergedIf);
1336   verifyFormat("if (a) g();\n"
1337                "else if (b) {\n"
1338                "  g();\n"
1339                "} else {\n"
1340                "  g();\n"
1341                "}",
1342                AllowsMergedIf);
1343   verifyFormat("if (a) {\n"
1344                "  g();\n"
1345                "} else if (b) {\n"
1346                "  g();\n"
1347                "} else {\n"
1348                "  g();\n"
1349                "}",
1350                AllowsMergedIf);
1351   verifyFormat("MYIF (a) f();\n"
1352                "else {\n"
1353                "  g();\n"
1354                "}",
1355                AllowsMergedIf);
1356   verifyFormat("MYIF (a) f();\n"
1357                "else {\n"
1358                "  if (a) f();\n"
1359                "  else {\n"
1360                "    g();\n"
1361                "  }\n"
1362                "  g();\n"
1363                "}",
1364                AllowsMergedIf);
1365 
1366   verifyFormat("MYIF (a) g();", AllowsMergedIf);
1367   verifyFormat("MYIF (a) {\n"
1368                "  g()\n"
1369                "};",
1370                AllowsMergedIf);
1371   verifyFormat("MYIF (a) g();\n"
1372                "else g();",
1373                AllowsMergedIf);
1374   verifyFormat("MYIF (a) {\n"
1375                "  g();\n"
1376                "} else g();",
1377                AllowsMergedIf);
1378   verifyFormat("MYIF (a) g();\n"
1379                "else {\n"
1380                "  g();\n"
1381                "}",
1382                AllowsMergedIf);
1383   verifyFormat("MYIF (a) {\n"
1384                "  g();\n"
1385                "} else {\n"
1386                "  g();\n"
1387                "}",
1388                AllowsMergedIf);
1389   verifyFormat("MYIF (a) g();\n"
1390                "else MYIF (b) g();\n"
1391                "else g();",
1392                AllowsMergedIf);
1393   verifyFormat("MYIF (a) g();\n"
1394                "else if (b) g();\n"
1395                "else g();",
1396                AllowsMergedIf);
1397   verifyFormat("MYIF (a) {\n"
1398                "  g();\n"
1399                "} else MYIF (b) g();\n"
1400                "else g();",
1401                AllowsMergedIf);
1402   verifyFormat("MYIF (a) {\n"
1403                "  g();\n"
1404                "} else if (b) g();\n"
1405                "else g();",
1406                AllowsMergedIf);
1407   verifyFormat("MYIF (a) g();\n"
1408                "else MYIF (b) {\n"
1409                "  g();\n"
1410                "} else g();",
1411                AllowsMergedIf);
1412   verifyFormat("MYIF (a) g();\n"
1413                "else if (b) {\n"
1414                "  g();\n"
1415                "} else g();",
1416                AllowsMergedIf);
1417   verifyFormat("MYIF (a) g();\n"
1418                "else MYIF (b) g();\n"
1419                "else {\n"
1420                "  g();\n"
1421                "}",
1422                AllowsMergedIf);
1423   verifyFormat("MYIF (a) g();\n"
1424                "else if (b) g();\n"
1425                "else {\n"
1426                "  g();\n"
1427                "}",
1428                AllowsMergedIf);
1429   verifyFormat("MYIF (a) g();\n"
1430                "else MYIF (b) {\n"
1431                "  g();\n"
1432                "} else {\n"
1433                "  g();\n"
1434                "}",
1435                AllowsMergedIf);
1436   verifyFormat("MYIF (a) g();\n"
1437                "else if (b) {\n"
1438                "  g();\n"
1439                "} else {\n"
1440                "  g();\n"
1441                "}",
1442                AllowsMergedIf);
1443   verifyFormat("MYIF (a) {\n"
1444                "  g();\n"
1445                "} else MYIF (b) {\n"
1446                "  g();\n"
1447                "} else {\n"
1448                "  g();\n"
1449                "}",
1450                AllowsMergedIf);
1451   verifyFormat("MYIF (a) {\n"
1452                "  g();\n"
1453                "} else if (b) {\n"
1454                "  g();\n"
1455                "} else {\n"
1456                "  g();\n"
1457                "}",
1458                AllowsMergedIf);
1459 }
1460 
1461 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
1462   FormatStyle AllowsMergedLoops = getLLVMStyle();
1463   AllowsMergedLoops.AllowShortLoopsOnASingleLine = true;
1464   verifyFormat("while (true) continue;", AllowsMergedLoops);
1465   verifyFormat("for (;;) continue;", AllowsMergedLoops);
1466   verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops);
1467   verifyFormat("while (true)\n"
1468                "  ;",
1469                AllowsMergedLoops);
1470   verifyFormat("for (;;)\n"
1471                "  ;",
1472                AllowsMergedLoops);
1473   verifyFormat("for (;;)\n"
1474                "  for (;;) continue;",
1475                AllowsMergedLoops);
1476   verifyFormat("for (;;) // Can't merge this\n"
1477                "  continue;",
1478                AllowsMergedLoops);
1479   verifyFormat("for (;;) /* still don't merge */\n"
1480                "  continue;",
1481                AllowsMergedLoops);
1482   verifyFormat("do a++;\n"
1483                "while (true);",
1484                AllowsMergedLoops);
1485   verifyFormat("do /* Don't merge */\n"
1486                "  a++;\n"
1487                "while (true);",
1488                AllowsMergedLoops);
1489   verifyFormat("do // Don't merge\n"
1490                "  a++;\n"
1491                "while (true);",
1492                AllowsMergedLoops);
1493   verifyFormat("do\n"
1494                "  // Don't merge\n"
1495                "  a++;\n"
1496                "while (true);",
1497                AllowsMergedLoops);
1498   // Without braces labels are interpreted differently.
1499   verifyFormat("{\n"
1500                "  do\n"
1501                "  label:\n"
1502                "    a++;\n"
1503                "  while (true);\n"
1504                "}",
1505                AllowsMergedLoops);
1506 }
1507 
1508 TEST_F(FormatTest, FormatShortBracedStatements) {
1509   FormatStyle AllowSimpleBracedStatements = getLLVMStyle();
1510   AllowSimpleBracedStatements.IfMacros.push_back("MYIF");
1511   // Where line-lengths matter, a 2-letter synonym that maintains line length.
1512   // Not IF to avoid any confusion that IF is somehow special.
1513   AllowSimpleBracedStatements.IfMacros.push_back("FI");
1514   AllowSimpleBracedStatements.ColumnLimit = 40;
1515   AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine =
1516       FormatStyle::SBS_Always;
1517 
1518   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
1519       FormatStyle::SIS_WithoutElse;
1520   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
1521 
1522   AllowSimpleBracedStatements.BreakBeforeBraces = FormatStyle::BS_Custom;
1523   AllowSimpleBracedStatements.BraceWrapping.AfterFunction = true;
1524   AllowSimpleBracedStatements.BraceWrapping.SplitEmptyRecord = false;
1525 
1526   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
1527   verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements);
1528   verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements);
1529   verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements);
1530   verifyFormat("MYIF constexpr (true) {}", AllowSimpleBracedStatements);
1531   verifyFormat("MYIF CONSTEXPR (true) {}", AllowSimpleBracedStatements);
1532   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
1533   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
1534   verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
1535   verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements);
1536   verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements);
1537   verifyFormat("MYIF (true) { f(); }", AllowSimpleBracedStatements);
1538   verifyFormat("MYIF constexpr (true) { f(); }", AllowSimpleBracedStatements);
1539   verifyFormat("MYIF CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements);
1540   verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
1541   verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
1542   verifyFormat("if (true) { fffffffffffffffffffffff(); }",
1543                AllowSimpleBracedStatements);
1544   verifyFormat("if (true) {\n"
1545                "  ffffffffffffffffffffffff();\n"
1546                "}",
1547                AllowSimpleBracedStatements);
1548   verifyFormat("if (true) {\n"
1549                "  ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
1550                "}",
1551                AllowSimpleBracedStatements);
1552   verifyFormat("if (true) { //\n"
1553                "  f();\n"
1554                "}",
1555                AllowSimpleBracedStatements);
1556   verifyFormat("if (true) {\n"
1557                "  f();\n"
1558                "  f();\n"
1559                "}",
1560                AllowSimpleBracedStatements);
1561   verifyFormat("if (true) {\n"
1562                "  f();\n"
1563                "} else {\n"
1564                "  f();\n"
1565                "}",
1566                AllowSimpleBracedStatements);
1567   verifyFormat("FI (true) { fffffffffffffffffffffff(); }",
1568                AllowSimpleBracedStatements);
1569   verifyFormat("MYIF (true) {\n"
1570                "  ffffffffffffffffffffffff();\n"
1571                "}",
1572                AllowSimpleBracedStatements);
1573   verifyFormat("MYIF (true) {\n"
1574                "  ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
1575                "}",
1576                AllowSimpleBracedStatements);
1577   verifyFormat("MYIF (true) { //\n"
1578                "  f();\n"
1579                "}",
1580                AllowSimpleBracedStatements);
1581   verifyFormat("MYIF (true) {\n"
1582                "  f();\n"
1583                "  f();\n"
1584                "}",
1585                AllowSimpleBracedStatements);
1586   verifyFormat("MYIF (true) {\n"
1587                "  f();\n"
1588                "} else {\n"
1589                "  f();\n"
1590                "}",
1591                AllowSimpleBracedStatements);
1592 
1593   verifyFormat("struct A2 {\n"
1594                "  int X;\n"
1595                "};",
1596                AllowSimpleBracedStatements);
1597   verifyFormat("typedef struct A2 {\n"
1598                "  int X;\n"
1599                "} A2_t;",
1600                AllowSimpleBracedStatements);
1601   verifyFormat("template <int> struct A2 {\n"
1602                "  struct B {};\n"
1603                "};",
1604                AllowSimpleBracedStatements);
1605 
1606   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
1607       FormatStyle::SIS_Never;
1608   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
1609   verifyFormat("if (true) {\n"
1610                "  f();\n"
1611                "}",
1612                AllowSimpleBracedStatements);
1613   verifyFormat("if (true) {\n"
1614                "  f();\n"
1615                "} else {\n"
1616                "  f();\n"
1617                "}",
1618                AllowSimpleBracedStatements);
1619   verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements);
1620   verifyFormat("MYIF (true) {\n"
1621                "  f();\n"
1622                "}",
1623                AllowSimpleBracedStatements);
1624   verifyFormat("MYIF (true) {\n"
1625                "  f();\n"
1626                "} else {\n"
1627                "  f();\n"
1628                "}",
1629                AllowSimpleBracedStatements);
1630 
1631   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
1632   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
1633   verifyFormat("while (true) {\n"
1634                "  f();\n"
1635                "}",
1636                AllowSimpleBracedStatements);
1637   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
1638   verifyFormat("for (;;) {\n"
1639                "  f();\n"
1640                "}",
1641                AllowSimpleBracedStatements);
1642 
1643   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
1644       FormatStyle::SIS_WithoutElse;
1645   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
1646   AllowSimpleBracedStatements.BraceWrapping.AfterControlStatement =
1647       FormatStyle::BWACS_Always;
1648 
1649   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
1650   verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements);
1651   verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements);
1652   verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements);
1653   verifyFormat("MYIF constexpr (true) {}", AllowSimpleBracedStatements);
1654   verifyFormat("MYIF CONSTEXPR (true) {}", AllowSimpleBracedStatements);
1655   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
1656   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
1657   verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
1658   verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements);
1659   verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements);
1660   verifyFormat("MYIF (true) { f(); }", AllowSimpleBracedStatements);
1661   verifyFormat("MYIF constexpr (true) { f(); }", AllowSimpleBracedStatements);
1662   verifyFormat("MYIF CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements);
1663   verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
1664   verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
1665   verifyFormat("if (true) { fffffffffffffffffffffff(); }",
1666                AllowSimpleBracedStatements);
1667   verifyFormat("if (true)\n"
1668                "{\n"
1669                "  ffffffffffffffffffffffff();\n"
1670                "}",
1671                AllowSimpleBracedStatements);
1672   verifyFormat("if (true)\n"
1673                "{\n"
1674                "  ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
1675                "}",
1676                AllowSimpleBracedStatements);
1677   verifyFormat("if (true)\n"
1678                "{ //\n"
1679                "  f();\n"
1680                "}",
1681                AllowSimpleBracedStatements);
1682   verifyFormat("if (true)\n"
1683                "{\n"
1684                "  f();\n"
1685                "  f();\n"
1686                "}",
1687                AllowSimpleBracedStatements);
1688   verifyFormat("if (true)\n"
1689                "{\n"
1690                "  f();\n"
1691                "} else\n"
1692                "{\n"
1693                "  f();\n"
1694                "}",
1695                AllowSimpleBracedStatements);
1696   verifyFormat("FI (true) { fffffffffffffffffffffff(); }",
1697                AllowSimpleBracedStatements);
1698   verifyFormat("MYIF (true)\n"
1699                "{\n"
1700                "  ffffffffffffffffffffffff();\n"
1701                "}",
1702                AllowSimpleBracedStatements);
1703   verifyFormat("MYIF (true)\n"
1704                "{\n"
1705                "  ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
1706                "}",
1707                AllowSimpleBracedStatements);
1708   verifyFormat("MYIF (true)\n"
1709                "{ //\n"
1710                "  f();\n"
1711                "}",
1712                AllowSimpleBracedStatements);
1713   verifyFormat("MYIF (true)\n"
1714                "{\n"
1715                "  f();\n"
1716                "  f();\n"
1717                "}",
1718                AllowSimpleBracedStatements);
1719   verifyFormat("MYIF (true)\n"
1720                "{\n"
1721                "  f();\n"
1722                "} else\n"
1723                "{\n"
1724                "  f();\n"
1725                "}",
1726                AllowSimpleBracedStatements);
1727 
1728   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
1729       FormatStyle::SIS_Never;
1730   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
1731   verifyFormat("if (true)\n"
1732                "{\n"
1733                "  f();\n"
1734                "}",
1735                AllowSimpleBracedStatements);
1736   verifyFormat("if (true)\n"
1737                "{\n"
1738                "  f();\n"
1739                "} else\n"
1740                "{\n"
1741                "  f();\n"
1742                "}",
1743                AllowSimpleBracedStatements);
1744   verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements);
1745   verifyFormat("MYIF (true)\n"
1746                "{\n"
1747                "  f();\n"
1748                "}",
1749                AllowSimpleBracedStatements);
1750   verifyFormat("MYIF (true)\n"
1751                "{\n"
1752                "  f();\n"
1753                "} else\n"
1754                "{\n"
1755                "  f();\n"
1756                "}",
1757                AllowSimpleBracedStatements);
1758 
1759   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
1760   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
1761   verifyFormat("while (true)\n"
1762                "{\n"
1763                "  f();\n"
1764                "}",
1765                AllowSimpleBracedStatements);
1766   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
1767   verifyFormat("for (;;)\n"
1768                "{\n"
1769                "  f();\n"
1770                "}",
1771                AllowSimpleBracedStatements);
1772 }
1773 
1774 TEST_F(FormatTest, ShortBlocksInMacrosDontMergeWithCodeAfterMacro) {
1775   FormatStyle Style = getLLVMStyleWithColumns(60);
1776   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
1777   Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse;
1778   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
1779   EXPECT_EQ("#define A                                                  \\\n"
1780             "  if (HANDLEwernufrnuLwrmviferuvnierv)                     \\\n"
1781             "  {                                                        \\\n"
1782             "    RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier;               \\\n"
1783             "  }\n"
1784             "X;",
1785             format("#define A \\\n"
1786                    "   if (HANDLEwernufrnuLwrmviferuvnierv) { \\\n"
1787                    "      RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n"
1788                    "   }\n"
1789                    "X;",
1790                    Style));
1791 }
1792 
1793 TEST_F(FormatTest, ParseIfElse) {
1794   verifyFormat("if (true)\n"
1795                "  if (true)\n"
1796                "    if (true)\n"
1797                "      f();\n"
1798                "    else\n"
1799                "      g();\n"
1800                "  else\n"
1801                "    h();\n"
1802                "else\n"
1803                "  i();");
1804   verifyFormat("if (true)\n"
1805                "  if (true)\n"
1806                "    if (true) {\n"
1807                "      if (true)\n"
1808                "        f();\n"
1809                "    } else {\n"
1810                "      g();\n"
1811                "    }\n"
1812                "  else\n"
1813                "    h();\n"
1814                "else {\n"
1815                "  i();\n"
1816                "}");
1817   verifyFormat("if (true)\n"
1818                "  if constexpr (true)\n"
1819                "    if (true) {\n"
1820                "      if constexpr (true)\n"
1821                "        f();\n"
1822                "    } else {\n"
1823                "      g();\n"
1824                "    }\n"
1825                "  else\n"
1826                "    h();\n"
1827                "else {\n"
1828                "  i();\n"
1829                "}");
1830   verifyFormat("if (true)\n"
1831                "  if CONSTEXPR (true)\n"
1832                "    if (true) {\n"
1833                "      if CONSTEXPR (true)\n"
1834                "        f();\n"
1835                "    } else {\n"
1836                "      g();\n"
1837                "    }\n"
1838                "  else\n"
1839                "    h();\n"
1840                "else {\n"
1841                "  i();\n"
1842                "}");
1843   verifyFormat("void f() {\n"
1844                "  if (a) {\n"
1845                "  } else {\n"
1846                "  }\n"
1847                "}");
1848 }
1849 
1850 TEST_F(FormatTest, ElseIf) {
1851   verifyFormat("if (a) {\n} else if (b) {\n}");
1852   verifyFormat("if (a)\n"
1853                "  f();\n"
1854                "else if (b)\n"
1855                "  g();\n"
1856                "else\n"
1857                "  h();");
1858   verifyFormat("if (a)\n"
1859                "  f();\n"
1860                "else // comment\n"
1861                "  if (b) {\n"
1862                "    g();\n"
1863                "    h();\n"
1864                "  }");
1865   verifyFormat("if constexpr (a)\n"
1866                "  f();\n"
1867                "else if constexpr (b)\n"
1868                "  g();\n"
1869                "else\n"
1870                "  h();");
1871   verifyFormat("if CONSTEXPR (a)\n"
1872                "  f();\n"
1873                "else if CONSTEXPR (b)\n"
1874                "  g();\n"
1875                "else\n"
1876                "  h();");
1877   verifyFormat("if (a) {\n"
1878                "  f();\n"
1879                "}\n"
1880                "// or else ..\n"
1881                "else {\n"
1882                "  g()\n"
1883                "}");
1884 
1885   verifyFormat("if (a) {\n"
1886                "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1887                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
1888                "}");
1889   verifyFormat("if (a) {\n"
1890                "} else if constexpr (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1891                "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
1892                "}");
1893   verifyFormat("if (a) {\n"
1894                "} else if CONSTEXPR (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1895                "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
1896                "}");
1897   verifyFormat("if (a) {\n"
1898                "} else if (\n"
1899                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
1900                "}",
1901                getLLVMStyleWithColumns(62));
1902   verifyFormat("if (a) {\n"
1903                "} else if constexpr (\n"
1904                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
1905                "}",
1906                getLLVMStyleWithColumns(62));
1907   verifyFormat("if (a) {\n"
1908                "} else if CONSTEXPR (\n"
1909                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
1910                "}",
1911                getLLVMStyleWithColumns(62));
1912 }
1913 
1914 TEST_F(FormatTest, SeparatePointerReferenceAlignment) {
1915   FormatStyle Style = getLLVMStyle();
1916   // Check first the default LLVM style
1917   // Style.PointerAlignment = FormatStyle::PAS_Right;
1918   // Style.ReferenceAlignment = FormatStyle::RAS_Pointer;
1919   verifyFormat("int *f1(int *a, int &b, int &&c);", Style);
1920   verifyFormat("int &f2(int &&c, int *a, int &b);", Style);
1921   verifyFormat("int &&f3(int &b, int &&c, int *a);", Style);
1922   verifyFormat("int *f1(int &a) const &;", Style);
1923   verifyFormat("int *f1(int &a) const & = 0;", Style);
1924   verifyFormat("int *a = f1();", Style);
1925   verifyFormat("int &b = f2();", Style);
1926   verifyFormat("int &&c = f3();", Style);
1927   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   Style.AllowShortEnumsOnASingleLine = false;
2508   verifyFormat("enum {\n"
2509                "  A,\n"
2510                "  B,\n"
2511                "  C\n"
2512                "} ShortEnum1, ShortEnum2;",
2513                Style);
2514   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
2515   Style.BraceWrapping.AfterEnum = true;
2516   verifyFormat("enum\n"
2517                "{\n"
2518                "  A,\n"
2519                "  B,\n"
2520                "  C\n"
2521                "} ShortEnum1, ShortEnum2;",
2522                Style);
2523 }
2524 
2525 TEST_F(FormatTest, ShortCaseLabels) {
2526   FormatStyle Style = getLLVMStyle();
2527   Style.AllowShortCaseLabelsOnASingleLine = true;
2528   verifyFormat("switch (a) {\n"
2529                "case 1: x = 1; break;\n"
2530                "case 2: return;\n"
2531                "case 3:\n"
2532                "case 4:\n"
2533                "case 5: return;\n"
2534                "case 6: // comment\n"
2535                "  return;\n"
2536                "case 7:\n"
2537                "  // comment\n"
2538                "  return;\n"
2539                "case 8:\n"
2540                "  x = 8; // comment\n"
2541                "  break;\n"
2542                "default: y = 1; break;\n"
2543                "}",
2544                Style);
2545   verifyFormat("switch (a) {\n"
2546                "case 0: return; // comment\n"
2547                "case 1: break;  // comment\n"
2548                "case 2: return;\n"
2549                "// comment\n"
2550                "case 3: return;\n"
2551                "// comment 1\n"
2552                "// comment 2\n"
2553                "// comment 3\n"
2554                "case 4: break; /* comment */\n"
2555                "case 5:\n"
2556                "  // comment\n"
2557                "  break;\n"
2558                "case 6: /* comment */ x = 1; break;\n"
2559                "case 7: x = /* comment */ 1; break;\n"
2560                "case 8:\n"
2561                "  x = 1; /* comment */\n"
2562                "  break;\n"
2563                "case 9:\n"
2564                "  break; // comment line 1\n"
2565                "         // comment line 2\n"
2566                "}",
2567                Style);
2568   EXPECT_EQ("switch (a) {\n"
2569             "case 1:\n"
2570             "  x = 8;\n"
2571             "  // fall through\n"
2572             "case 2: x = 8;\n"
2573             "// comment\n"
2574             "case 3:\n"
2575             "  return; /* comment line 1\n"
2576             "           * comment line 2 */\n"
2577             "case 4: i = 8;\n"
2578             "// something else\n"
2579             "#if FOO\n"
2580             "case 5: break;\n"
2581             "#endif\n"
2582             "}",
2583             format("switch (a) {\n"
2584                    "case 1: x = 8;\n"
2585                    "  // fall through\n"
2586                    "case 2:\n"
2587                    "  x = 8;\n"
2588                    "// comment\n"
2589                    "case 3:\n"
2590                    "  return; /* comment line 1\n"
2591                    "           * comment line 2 */\n"
2592                    "case 4:\n"
2593                    "  i = 8;\n"
2594                    "// something else\n"
2595                    "#if FOO\n"
2596                    "case 5: break;\n"
2597                    "#endif\n"
2598                    "}",
2599                    Style));
2600   EXPECT_EQ("switch (a) {\n"
2601             "case 0:\n"
2602             "  return; // long long long long long long long long long long "
2603             "long long comment\n"
2604             "          // line\n"
2605             "}",
2606             format("switch (a) {\n"
2607                    "case 0: return; // long long long long long long long long "
2608                    "long long long long comment line\n"
2609                    "}",
2610                    Style));
2611   EXPECT_EQ("switch (a) {\n"
2612             "case 0:\n"
2613             "  return; /* long long long long long long long long long long "
2614             "long long comment\n"
2615             "             line */\n"
2616             "}",
2617             format("switch (a) {\n"
2618                    "case 0: return; /* long long long long long long long long "
2619                    "long long long long comment line */\n"
2620                    "}",
2621                    Style));
2622   verifyFormat("switch (a) {\n"
2623                "#if FOO\n"
2624                "case 0: return 0;\n"
2625                "#endif\n"
2626                "}",
2627                Style);
2628   verifyFormat("switch (a) {\n"
2629                "case 1: {\n"
2630                "}\n"
2631                "case 2: {\n"
2632                "  return;\n"
2633                "}\n"
2634                "case 3: {\n"
2635                "  x = 1;\n"
2636                "  return;\n"
2637                "}\n"
2638                "case 4:\n"
2639                "  if (x)\n"
2640                "    return;\n"
2641                "}",
2642                Style);
2643   Style.ColumnLimit = 21;
2644   verifyFormat("switch (a) {\n"
2645                "case 1: x = 1; break;\n"
2646                "case 2: return;\n"
2647                "case 3:\n"
2648                "case 4:\n"
2649                "case 5: return;\n"
2650                "default:\n"
2651                "  y = 1;\n"
2652                "  break;\n"
2653                "}",
2654                Style);
2655   Style.ColumnLimit = 80;
2656   Style.AllowShortCaseLabelsOnASingleLine = false;
2657   Style.IndentCaseLabels = true;
2658   EXPECT_EQ("switch (n) {\n"
2659             "  default /*comments*/:\n"
2660             "    return true;\n"
2661             "  case 0:\n"
2662             "    return false;\n"
2663             "}",
2664             format("switch (n) {\n"
2665                    "default/*comments*/:\n"
2666                    "  return true;\n"
2667                    "case 0:\n"
2668                    "  return false;\n"
2669                    "}",
2670                    Style));
2671   Style.AllowShortCaseLabelsOnASingleLine = true;
2672   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
2673   Style.BraceWrapping.AfterCaseLabel = true;
2674   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
2675   EXPECT_EQ("switch (n)\n"
2676             "{\n"
2677             "  case 0:\n"
2678             "  {\n"
2679             "    return false;\n"
2680             "  }\n"
2681             "  default:\n"
2682             "  {\n"
2683             "    return true;\n"
2684             "  }\n"
2685             "}",
2686             format("switch (n) {\n"
2687                    "  case 0: {\n"
2688                    "    return false;\n"
2689                    "  }\n"
2690                    "  default:\n"
2691                    "  {\n"
2692                    "    return true;\n"
2693                    "  }\n"
2694                    "}",
2695                    Style));
2696 }
2697 
2698 TEST_F(FormatTest, FormatsLabels) {
2699   verifyFormat("void f() {\n"
2700                "  some_code();\n"
2701                "test_label:\n"
2702                "  some_other_code();\n"
2703                "  {\n"
2704                "    some_more_code();\n"
2705                "  another_label:\n"
2706                "    some_more_code();\n"
2707                "  }\n"
2708                "}");
2709   verifyFormat("{\n"
2710                "  some_code();\n"
2711                "test_label:\n"
2712                "  some_other_code();\n"
2713                "}");
2714   verifyFormat("{\n"
2715                "  some_code();\n"
2716                "test_label:;\n"
2717                "  int i = 0;\n"
2718                "}");
2719   FormatStyle Style = getLLVMStyle();
2720   Style.IndentGotoLabels = false;
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                Style);
2732   verifyFormat("{\n"
2733                "  some_code();\n"
2734                "test_label:\n"
2735                "  some_other_code();\n"
2736                "}",
2737                Style);
2738   verifyFormat("{\n"
2739                "  some_code();\n"
2740                "test_label:;\n"
2741                "  int i = 0;\n"
2742                "}");
2743 }
2744 
2745 TEST_F(FormatTest, MultiLineControlStatements) {
2746   FormatStyle Style = getLLVMStyle();
2747   Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom;
2748   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine;
2749   Style.ColumnLimit = 20;
2750   // Short lines should keep opening brace on same line.
2751   EXPECT_EQ("if (foo) {\n"
2752             "  bar();\n"
2753             "}",
2754             format("if(foo){bar();}", Style));
2755   EXPECT_EQ("if (foo) {\n"
2756             "  bar();\n"
2757             "} else {\n"
2758             "  baz();\n"
2759             "}",
2760             format("if(foo){bar();}else{baz();}", Style));
2761   EXPECT_EQ("if (foo && bar) {\n"
2762             "  baz();\n"
2763             "}",
2764             format("if(foo&&bar){baz();}", Style));
2765   EXPECT_EQ("if (foo) {\n"
2766             "  bar();\n"
2767             "} else if (baz) {\n"
2768             "  quux();\n"
2769             "}",
2770             format("if(foo){bar();}else if(baz){quux();}", Style));
2771   EXPECT_EQ(
2772       "if (foo) {\n"
2773       "  bar();\n"
2774       "} else if (baz) {\n"
2775       "  quux();\n"
2776       "} else {\n"
2777       "  foobar();\n"
2778       "}",
2779       format("if(foo){bar();}else if(baz){quux();}else{foobar();}", Style));
2780   EXPECT_EQ("for (;;) {\n"
2781             "  foo();\n"
2782             "}",
2783             format("for(;;){foo();}"));
2784   EXPECT_EQ("while (1) {\n"
2785             "  foo();\n"
2786             "}",
2787             format("while(1){foo();}", Style));
2788   EXPECT_EQ("switch (foo) {\n"
2789             "case bar:\n"
2790             "  return;\n"
2791             "}",
2792             format("switch(foo){case bar:return;}", Style));
2793   EXPECT_EQ("try {\n"
2794             "  foo();\n"
2795             "} catch (...) {\n"
2796             "  bar();\n"
2797             "}",
2798             format("try{foo();}catch(...){bar();}", Style));
2799   EXPECT_EQ("do {\n"
2800             "  foo();\n"
2801             "} while (bar &&\n"
2802             "         baz);",
2803             format("do{foo();}while(bar&&baz);", Style));
2804   // Long lines should put opening brace on new line.
2805   EXPECT_EQ("if (foo && bar &&\n"
2806             "    baz)\n"
2807             "{\n"
2808             "  quux();\n"
2809             "}",
2810             format("if(foo&&bar&&baz){quux();}", Style));
2811   EXPECT_EQ("if (foo && bar &&\n"
2812             "    baz)\n"
2813             "{\n"
2814             "  quux();\n"
2815             "}",
2816             format("if (foo && bar &&\n"
2817                    "    baz) {\n"
2818                    "  quux();\n"
2819                    "}",
2820                    Style));
2821   EXPECT_EQ("if (foo) {\n"
2822             "  bar();\n"
2823             "} else if (baz ||\n"
2824             "           quux)\n"
2825             "{\n"
2826             "  foobar();\n"
2827             "}",
2828             format("if(foo){bar();}else if(baz||quux){foobar();}", Style));
2829   EXPECT_EQ(
2830       "if (foo) {\n"
2831       "  bar();\n"
2832       "} else if (baz ||\n"
2833       "           quux)\n"
2834       "{\n"
2835       "  foobar();\n"
2836       "} else {\n"
2837       "  barbaz();\n"
2838       "}",
2839       format("if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}",
2840              Style));
2841   EXPECT_EQ("for (int i = 0;\n"
2842             "     i < 10; ++i)\n"
2843             "{\n"
2844             "  foo();\n"
2845             "}",
2846             format("for(int i=0;i<10;++i){foo();}", Style));
2847   EXPECT_EQ("foreach (int i,\n"
2848             "         list)\n"
2849             "{\n"
2850             "  foo();\n"
2851             "}",
2852             format("foreach(int i, list){foo();}", Style));
2853   Style.ColumnLimit =
2854       40; // to concentrate at brace wrapping, not line wrap due to column limit
2855   EXPECT_EQ("foreach (int i, list) {\n"
2856             "  foo();\n"
2857             "}",
2858             format("foreach(int i, list){foo();}", Style));
2859   Style.ColumnLimit =
2860       20; // to concentrate at brace wrapping, not line wrap due to column limit
2861   EXPECT_EQ("while (foo || bar ||\n"
2862             "       baz)\n"
2863             "{\n"
2864             "  quux();\n"
2865             "}",
2866             format("while(foo||bar||baz){quux();}", Style));
2867   EXPECT_EQ("switch (\n"
2868             "    foo = barbaz)\n"
2869             "{\n"
2870             "case quux:\n"
2871             "  return;\n"
2872             "}",
2873             format("switch(foo=barbaz){case quux:return;}", Style));
2874   EXPECT_EQ("try {\n"
2875             "  foo();\n"
2876             "} catch (\n"
2877             "    Exception &bar)\n"
2878             "{\n"
2879             "  baz();\n"
2880             "}",
2881             format("try{foo();}catch(Exception&bar){baz();}", Style));
2882   Style.ColumnLimit =
2883       40; // to concentrate at brace wrapping, not line wrap due to column limit
2884   EXPECT_EQ("try {\n"
2885             "  foo();\n"
2886             "} catch (Exception &bar) {\n"
2887             "  baz();\n"
2888             "}",
2889             format("try{foo();}catch(Exception&bar){baz();}", Style));
2890   Style.ColumnLimit =
2891       20; // to concentrate at brace wrapping, not line wrap due to column limit
2892 
2893   Style.BraceWrapping.BeforeElse = true;
2894   EXPECT_EQ(
2895       "if (foo) {\n"
2896       "  bar();\n"
2897       "}\n"
2898       "else if (baz ||\n"
2899       "         quux)\n"
2900       "{\n"
2901       "  foobar();\n"
2902       "}\n"
2903       "else {\n"
2904       "  barbaz();\n"
2905       "}",
2906       format("if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}",
2907              Style));
2908 
2909   Style.BraceWrapping.BeforeCatch = true;
2910   EXPECT_EQ("try {\n"
2911             "  foo();\n"
2912             "}\n"
2913             "catch (...) {\n"
2914             "  baz();\n"
2915             "}",
2916             format("try{foo();}catch(...){baz();}", Style));
2917 
2918   Style.BraceWrapping.AfterFunction = true;
2919   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine;
2920   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
2921   Style.ColumnLimit = 80;
2922   verifyFormat("void shortfunction() { bar(); }", Style);
2923 
2924   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
2925   verifyFormat("void shortfunction()\n"
2926                "{\n"
2927                "  bar();\n"
2928                "}",
2929                Style);
2930 }
2931 
2932 TEST_F(FormatTest, BeforeWhile) {
2933   FormatStyle Style = getLLVMStyle();
2934   Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom;
2935 
2936   verifyFormat("do {\n"
2937                "  foo();\n"
2938                "} while (1);",
2939                Style);
2940   Style.BraceWrapping.BeforeWhile = true;
2941   verifyFormat("do {\n"
2942                "  foo();\n"
2943                "}\n"
2944                "while (1);",
2945                Style);
2946 }
2947 
2948 //===----------------------------------------------------------------------===//
2949 // Tests for classes, namespaces, etc.
2950 //===----------------------------------------------------------------------===//
2951 
2952 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
2953   verifyFormat("class A {};");
2954 }
2955 
2956 TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
2957   verifyFormat("class A {\n"
2958                "public:\n"
2959                "public: // comment\n"
2960                "protected:\n"
2961                "private:\n"
2962                "  void f() {}\n"
2963                "};");
2964   verifyFormat("export class A {\n"
2965                "public:\n"
2966                "public: // comment\n"
2967                "protected:\n"
2968                "private:\n"
2969                "  void f() {}\n"
2970                "};");
2971   verifyGoogleFormat("class A {\n"
2972                      " public:\n"
2973                      " protected:\n"
2974                      " private:\n"
2975                      "  void f() {}\n"
2976                      "};");
2977   verifyGoogleFormat("export class A {\n"
2978                      " public:\n"
2979                      " protected:\n"
2980                      " private:\n"
2981                      "  void f() {}\n"
2982                      "};");
2983   verifyFormat("class A {\n"
2984                "public slots:\n"
2985                "  void f1() {}\n"
2986                "public Q_SLOTS:\n"
2987                "  void f2() {}\n"
2988                "protected slots:\n"
2989                "  void f3() {}\n"
2990                "protected Q_SLOTS:\n"
2991                "  void f4() {}\n"
2992                "private slots:\n"
2993                "  void f5() {}\n"
2994                "private Q_SLOTS:\n"
2995                "  void f6() {}\n"
2996                "signals:\n"
2997                "  void g1();\n"
2998                "Q_SIGNALS:\n"
2999                "  void g2();\n"
3000                "};");
3001 
3002   // Don't interpret 'signals' the wrong way.
3003   verifyFormat("signals.set();");
3004   verifyFormat("for (Signals signals : f()) {\n}");
3005   verifyFormat("{\n"
3006                "  signals.set(); // This needs indentation.\n"
3007                "}");
3008   verifyFormat("void f() {\n"
3009                "label:\n"
3010                "  signals.baz();\n"
3011                "}");
3012 }
3013 
3014 TEST_F(FormatTest, SeparatesLogicalBlocks) {
3015   EXPECT_EQ("class A {\n"
3016             "public:\n"
3017             "  void f();\n"
3018             "\n"
3019             "private:\n"
3020             "  void g() {}\n"
3021             "  // test\n"
3022             "protected:\n"
3023             "  int h;\n"
3024             "};",
3025             format("class A {\n"
3026                    "public:\n"
3027                    "void f();\n"
3028                    "private:\n"
3029                    "void g() {}\n"
3030                    "// test\n"
3031                    "protected:\n"
3032                    "int h;\n"
3033                    "};"));
3034   EXPECT_EQ("class A {\n"
3035             "protected:\n"
3036             "public:\n"
3037             "  void f();\n"
3038             "};",
3039             format("class A {\n"
3040                    "protected:\n"
3041                    "\n"
3042                    "public:\n"
3043                    "\n"
3044                    "  void f();\n"
3045                    "};"));
3046 
3047   // Even ensure proper spacing inside macros.
3048   EXPECT_EQ("#define B     \\\n"
3049             "  class A {   \\\n"
3050             "   protected: \\\n"
3051             "   public:    \\\n"
3052             "    void f(); \\\n"
3053             "  };",
3054             format("#define B     \\\n"
3055                    "  class A {   \\\n"
3056                    "   protected: \\\n"
3057                    "              \\\n"
3058                    "   public:    \\\n"
3059                    "              \\\n"
3060                    "    void f(); \\\n"
3061                    "  };",
3062                    getGoogleStyle()));
3063   // But don't remove empty lines after macros ending in access specifiers.
3064   EXPECT_EQ("#define A private:\n"
3065             "\n"
3066             "int i;",
3067             format("#define A         private:\n"
3068                    "\n"
3069                    "int              i;"));
3070 }
3071 
3072 TEST_F(FormatTest, FormatsClasses) {
3073   verifyFormat("class A : public B {};");
3074   verifyFormat("class A : public ::B {};");
3075 
3076   verifyFormat(
3077       "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3078       "                             public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
3079   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
3080                "    : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3081                "      public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
3082   verifyFormat(
3083       "class A : public B, public C, public D, public E, public F {};");
3084   verifyFormat("class AAAAAAAAAAAA : public B,\n"
3085                "                     public C,\n"
3086                "                     public D,\n"
3087                "                     public E,\n"
3088                "                     public F,\n"
3089                "                     public G {};");
3090 
3091   verifyFormat("class\n"
3092                "    ReallyReallyLongClassName {\n"
3093                "  int i;\n"
3094                "};",
3095                getLLVMStyleWithColumns(32));
3096   verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
3097                "                           aaaaaaaaaaaaaaaa> {};");
3098   verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n"
3099                "    : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n"
3100                "                                 aaaaaaaaaaaaaaaaaaaaaa> {};");
3101   verifyFormat("template <class R, class C>\n"
3102                "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n"
3103                "    : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};");
3104   verifyFormat("class ::A::B {};");
3105 }
3106 
3107 TEST_F(FormatTest, BreakInheritanceStyle) {
3108   FormatStyle StyleWithInheritanceBreakBeforeComma = getLLVMStyle();
3109   StyleWithInheritanceBreakBeforeComma.BreakInheritanceList =
3110       FormatStyle::BILS_BeforeComma;
3111   verifyFormat("class MyClass : public X {};",
3112                StyleWithInheritanceBreakBeforeComma);
3113   verifyFormat("class MyClass\n"
3114                "    : public X\n"
3115                "    , public Y {};",
3116                StyleWithInheritanceBreakBeforeComma);
3117   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA\n"
3118                "    : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n"
3119                "    , public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};",
3120                StyleWithInheritanceBreakBeforeComma);
3121   verifyFormat("struct aaaaaaaaaaaaa\n"
3122                "    : public aaaaaaaaaaaaaaaaaaa< // break\n"
3123                "          aaaaaaaaaaaaaaaa> {};",
3124                StyleWithInheritanceBreakBeforeComma);
3125 
3126   FormatStyle StyleWithInheritanceBreakAfterColon = getLLVMStyle();
3127   StyleWithInheritanceBreakAfterColon.BreakInheritanceList =
3128       FormatStyle::BILS_AfterColon;
3129   verifyFormat("class MyClass : public X {};",
3130                StyleWithInheritanceBreakAfterColon);
3131   verifyFormat("class MyClass : public X, public Y {};",
3132                StyleWithInheritanceBreakAfterColon);
3133   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA :\n"
3134                "    public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3135                "    public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};",
3136                StyleWithInheritanceBreakAfterColon);
3137   verifyFormat("struct aaaaaaaaaaaaa :\n"
3138                "    public aaaaaaaaaaaaaaaaaaa< // break\n"
3139                "        aaaaaaaaaaaaaaaa> {};",
3140                StyleWithInheritanceBreakAfterColon);
3141 
3142   FormatStyle StyleWithInheritanceBreakAfterComma = getLLVMStyle();
3143   StyleWithInheritanceBreakAfterComma.BreakInheritanceList =
3144       FormatStyle::BILS_AfterComma;
3145   verifyFormat("class MyClass : public X {};",
3146                StyleWithInheritanceBreakAfterComma);
3147   verifyFormat("class MyClass : public X,\n"
3148                "                public Y {};",
3149                StyleWithInheritanceBreakAfterComma);
3150   verifyFormat(
3151       "class AAAAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3152       "                               public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC "
3153       "{};",
3154       StyleWithInheritanceBreakAfterComma);
3155   verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
3156                "                           aaaaaaaaaaaaaaaa> {};",
3157                StyleWithInheritanceBreakAfterComma);
3158   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
3159                "    : public OnceBreak,\n"
3160                "      public AlwaysBreak,\n"
3161                "      EvenBasesFitInOneLine {};",
3162                StyleWithInheritanceBreakAfterComma);
3163 }
3164 
3165 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
3166   verifyFormat("class A {\n} a, b;");
3167   verifyFormat("struct A {\n} a, b;");
3168   verifyFormat("union A {\n} a;");
3169 }
3170 
3171 TEST_F(FormatTest, FormatsEnum) {
3172   verifyFormat("enum {\n"
3173                "  Zero,\n"
3174                "  One = 1,\n"
3175                "  Two = One + 1,\n"
3176                "  Three = (One + Two),\n"
3177                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
3178                "  Five = (One, Two, Three, Four, 5)\n"
3179                "};");
3180   verifyGoogleFormat("enum {\n"
3181                      "  Zero,\n"
3182                      "  One = 1,\n"
3183                      "  Two = One + 1,\n"
3184                      "  Three = (One + Two),\n"
3185                      "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
3186                      "  Five = (One, Two, Three, Four, 5)\n"
3187                      "};");
3188   verifyFormat("enum Enum {};");
3189   verifyFormat("enum {};");
3190   verifyFormat("enum X E {} d;");
3191   verifyFormat("enum __attribute__((...)) E {} d;");
3192   verifyFormat("enum __declspec__((...)) E {} d;");
3193   verifyFormat("enum {\n"
3194                "  Bar = Foo<int, int>::value\n"
3195                "};",
3196                getLLVMStyleWithColumns(30));
3197 
3198   verifyFormat("enum ShortEnum { A, B, C };");
3199   verifyGoogleFormat("enum ShortEnum { A, B, C };");
3200 
3201   EXPECT_EQ("enum KeepEmptyLines {\n"
3202             "  ONE,\n"
3203             "\n"
3204             "  TWO,\n"
3205             "\n"
3206             "  THREE\n"
3207             "}",
3208             format("enum KeepEmptyLines {\n"
3209                    "  ONE,\n"
3210                    "\n"
3211                    "  TWO,\n"
3212                    "\n"
3213                    "\n"
3214                    "  THREE\n"
3215                    "}"));
3216   verifyFormat("enum E { // comment\n"
3217                "  ONE,\n"
3218                "  TWO\n"
3219                "};\n"
3220                "int i;");
3221 
3222   FormatStyle EightIndent = getLLVMStyle();
3223   EightIndent.IndentWidth = 8;
3224   verifyFormat("enum {\n"
3225                "        VOID,\n"
3226                "        CHAR,\n"
3227                "        SHORT,\n"
3228                "        INT,\n"
3229                "        LONG,\n"
3230                "        SIGNED,\n"
3231                "        UNSIGNED,\n"
3232                "        BOOL,\n"
3233                "        FLOAT,\n"
3234                "        DOUBLE,\n"
3235                "        COMPLEX\n"
3236                "};",
3237                EightIndent);
3238 
3239   // Not enums.
3240   verifyFormat("enum X f() {\n"
3241                "  a();\n"
3242                "  return 42;\n"
3243                "}");
3244   verifyFormat("enum X Type::f() {\n"
3245                "  a();\n"
3246                "  return 42;\n"
3247                "}");
3248   verifyFormat("enum ::X f() {\n"
3249                "  a();\n"
3250                "  return 42;\n"
3251                "}");
3252   verifyFormat("enum ns::X f() {\n"
3253                "  a();\n"
3254                "  return 42;\n"
3255                "}");
3256 }
3257 
3258 TEST_F(FormatTest, FormatsEnumsWithErrors) {
3259   verifyFormat("enum Type {\n"
3260                "  One = 0; // These semicolons should be commas.\n"
3261                "  Two = 1;\n"
3262                "};");
3263   verifyFormat("namespace n {\n"
3264                "enum Type {\n"
3265                "  One,\n"
3266                "  Two, // missing };\n"
3267                "  int i;\n"
3268                "}\n"
3269                "void g() {}");
3270 }
3271 
3272 TEST_F(FormatTest, FormatsEnumStruct) {
3273   verifyFormat("enum struct {\n"
3274                "  Zero,\n"
3275                "  One = 1,\n"
3276                "  Two = One + 1,\n"
3277                "  Three = (One + Two),\n"
3278                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
3279                "  Five = (One, Two, Three, Four, 5)\n"
3280                "};");
3281   verifyFormat("enum struct Enum {};");
3282   verifyFormat("enum struct {};");
3283   verifyFormat("enum struct X E {} d;");
3284   verifyFormat("enum struct __attribute__((...)) E {} d;");
3285   verifyFormat("enum struct __declspec__((...)) E {} d;");
3286   verifyFormat("enum struct X f() {\n  a();\n  return 42;\n}");
3287 }
3288 
3289 TEST_F(FormatTest, FormatsEnumClass) {
3290   verifyFormat("enum class {\n"
3291                "  Zero,\n"
3292                "  One = 1,\n"
3293                "  Two = One + 1,\n"
3294                "  Three = (One + Two),\n"
3295                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
3296                "  Five = (One, Two, Three, Four, 5)\n"
3297                "};");
3298   verifyFormat("enum class Enum {};");
3299   verifyFormat("enum class {};");
3300   verifyFormat("enum class X E {} d;");
3301   verifyFormat("enum class __attribute__((...)) E {} d;");
3302   verifyFormat("enum class __declspec__((...)) E {} d;");
3303   verifyFormat("enum class X f() {\n  a();\n  return 42;\n}");
3304 }
3305 
3306 TEST_F(FormatTest, FormatsEnumTypes) {
3307   verifyFormat("enum X : int {\n"
3308                "  A, // Force multiple lines.\n"
3309                "  B\n"
3310                "};");
3311   verifyFormat("enum X : int { A, B };");
3312   verifyFormat("enum X : std::uint32_t { A, B };");
3313 }
3314 
3315 TEST_F(FormatTest, FormatsTypedefEnum) {
3316   FormatStyle Style = getLLVMStyle();
3317   Style.ColumnLimit = 40;
3318   verifyFormat("typedef enum {} EmptyEnum;");
3319   verifyFormat("typedef enum { A, B, C } ShortEnum;");
3320   verifyFormat("typedef enum {\n"
3321                "  ZERO = 0,\n"
3322                "  ONE = 1,\n"
3323                "  TWO = 2,\n"
3324                "  THREE = 3\n"
3325                "} LongEnum;",
3326                Style);
3327   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
3328   Style.BraceWrapping.AfterEnum = true;
3329   verifyFormat("typedef enum {} EmptyEnum;");
3330   verifyFormat("typedef enum { A, B, C } ShortEnum;");
3331   verifyFormat("typedef enum\n"
3332                "{\n"
3333                "  ZERO = 0,\n"
3334                "  ONE = 1,\n"
3335                "  TWO = 2,\n"
3336                "  THREE = 3\n"
3337                "} LongEnum;",
3338                Style);
3339 }
3340 
3341 TEST_F(FormatTest, FormatsNSEnums) {
3342   verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }");
3343   verifyGoogleFormat(
3344       "typedef NS_CLOSED_ENUM(NSInteger, SomeName) { AAA, BBB }");
3345   verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n"
3346                      "  // Information about someDecentlyLongValue.\n"
3347                      "  someDecentlyLongValue,\n"
3348                      "  // Information about anotherDecentlyLongValue.\n"
3349                      "  anotherDecentlyLongValue,\n"
3350                      "  // Information about aThirdDecentlyLongValue.\n"
3351                      "  aThirdDecentlyLongValue\n"
3352                      "};");
3353   verifyGoogleFormat("typedef NS_CLOSED_ENUM(NSInteger, MyType) {\n"
3354                      "  // Information about someDecentlyLongValue.\n"
3355                      "  someDecentlyLongValue,\n"
3356                      "  // Information about anotherDecentlyLongValue.\n"
3357                      "  anotherDecentlyLongValue,\n"
3358                      "  // Information about aThirdDecentlyLongValue.\n"
3359                      "  aThirdDecentlyLongValue\n"
3360                      "};");
3361   verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n"
3362                      "  a = 1,\n"
3363                      "  b = 2,\n"
3364                      "  c = 3,\n"
3365                      "};");
3366   verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n"
3367                      "  a = 1,\n"
3368                      "  b = 2,\n"
3369                      "  c = 3,\n"
3370                      "};");
3371   verifyGoogleFormat("typedef CF_CLOSED_ENUM(NSInteger, MyType) {\n"
3372                      "  a = 1,\n"
3373                      "  b = 2,\n"
3374                      "  c = 3,\n"
3375                      "};");
3376   verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n"
3377                      "  a = 1,\n"
3378                      "  b = 2,\n"
3379                      "  c = 3,\n"
3380                      "};");
3381 }
3382 
3383 TEST_F(FormatTest, FormatsBitfields) {
3384   verifyFormat("struct Bitfields {\n"
3385                "  unsigned sClass : 8;\n"
3386                "  unsigned ValueKind : 2;\n"
3387                "};");
3388   verifyFormat("struct A {\n"
3389                "  int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n"
3390                "      bbbbbbbbbbbbbbbbbbbbbbbbb;\n"
3391                "};");
3392   verifyFormat("struct MyStruct {\n"
3393                "  uchar data;\n"
3394                "  uchar : 8;\n"
3395                "  uchar : 8;\n"
3396                "  uchar other;\n"
3397                "};");
3398   FormatStyle Style = getLLVMStyle();
3399   Style.BitFieldColonSpacing = FormatStyle::BFCS_None;
3400   verifyFormat("struct Bitfields {\n"
3401                "  unsigned sClass:8;\n"
3402                "  unsigned ValueKind:2;\n"
3403                "  uchar other;\n"
3404                "};",
3405                Style);
3406   verifyFormat("struct A {\n"
3407                "  int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:1,\n"
3408                "      bbbbbbbbbbbbbbbbbbbbbbbbb:2;\n"
3409                "};",
3410                Style);
3411   Style.BitFieldColonSpacing = FormatStyle::BFCS_Before;
3412   verifyFormat("struct Bitfields {\n"
3413                "  unsigned sClass :8;\n"
3414                "  unsigned ValueKind :2;\n"
3415                "  uchar other;\n"
3416                "};",
3417                Style);
3418   Style.BitFieldColonSpacing = FormatStyle::BFCS_After;
3419   verifyFormat("struct Bitfields {\n"
3420                "  unsigned sClass: 8;\n"
3421                "  unsigned ValueKind: 2;\n"
3422                "  uchar other;\n"
3423                "};",
3424                Style);
3425 }
3426 
3427 TEST_F(FormatTest, FormatsNamespaces) {
3428   FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle();
3429   LLVMWithNoNamespaceFix.FixNamespaceComments = false;
3430 
3431   verifyFormat("namespace some_namespace {\n"
3432                "class A {};\n"
3433                "void f() { f(); }\n"
3434                "}",
3435                LLVMWithNoNamespaceFix);
3436   verifyFormat("namespace N::inline D {\n"
3437                "class A {};\n"
3438                "void f() { f(); }\n"
3439                "}",
3440                LLVMWithNoNamespaceFix);
3441   verifyFormat("namespace N::inline D::E {\n"
3442                "class A {};\n"
3443                "void f() { f(); }\n"
3444                "}",
3445                LLVMWithNoNamespaceFix);
3446   verifyFormat("namespace [[deprecated(\"foo[bar\")]] some_namespace {\n"
3447                "class A {};\n"
3448                "void f() { f(); }\n"
3449                "}",
3450                LLVMWithNoNamespaceFix);
3451   verifyFormat("/* something */ namespace some_namespace {\n"
3452                "class A {};\n"
3453                "void f() { f(); }\n"
3454                "}",
3455                LLVMWithNoNamespaceFix);
3456   verifyFormat("namespace {\n"
3457                "class A {};\n"
3458                "void f() { f(); }\n"
3459                "}",
3460                LLVMWithNoNamespaceFix);
3461   verifyFormat("/* something */ namespace {\n"
3462                "class A {};\n"
3463                "void f() { f(); }\n"
3464                "}",
3465                LLVMWithNoNamespaceFix);
3466   verifyFormat("inline namespace X {\n"
3467                "class A {};\n"
3468                "void f() { f(); }\n"
3469                "}",
3470                LLVMWithNoNamespaceFix);
3471   verifyFormat("/* something */ inline namespace X {\n"
3472                "class A {};\n"
3473                "void f() { f(); }\n"
3474                "}",
3475                LLVMWithNoNamespaceFix);
3476   verifyFormat("export namespace X {\n"
3477                "class A {};\n"
3478                "void f() { f(); }\n"
3479                "}",
3480                LLVMWithNoNamespaceFix);
3481   verifyFormat("using namespace some_namespace;\n"
3482                "class A {};\n"
3483                "void f() { f(); }",
3484                LLVMWithNoNamespaceFix);
3485 
3486   // This code is more common than we thought; if we
3487   // layout this correctly the semicolon will go into
3488   // its own line, which is undesirable.
3489   verifyFormat("namespace {};", LLVMWithNoNamespaceFix);
3490   verifyFormat("namespace {\n"
3491                "class A {};\n"
3492                "};",
3493                LLVMWithNoNamespaceFix);
3494 
3495   verifyFormat("namespace {\n"
3496                "int SomeVariable = 0; // comment\n"
3497                "} // namespace",
3498                LLVMWithNoNamespaceFix);
3499   EXPECT_EQ("#ifndef HEADER_GUARD\n"
3500             "#define HEADER_GUARD\n"
3501             "namespace my_namespace {\n"
3502             "int i;\n"
3503             "} // my_namespace\n"
3504             "#endif // HEADER_GUARD",
3505             format("#ifndef HEADER_GUARD\n"
3506                    " #define HEADER_GUARD\n"
3507                    "   namespace my_namespace {\n"
3508                    "int i;\n"
3509                    "}    // my_namespace\n"
3510                    "#endif    // HEADER_GUARD",
3511                    LLVMWithNoNamespaceFix));
3512 
3513   EXPECT_EQ("namespace A::B {\n"
3514             "class C {};\n"
3515             "}",
3516             format("namespace A::B {\n"
3517                    "class C {};\n"
3518                    "}",
3519                    LLVMWithNoNamespaceFix));
3520 
3521   FormatStyle Style = getLLVMStyle();
3522   Style.NamespaceIndentation = FormatStyle::NI_All;
3523   EXPECT_EQ("namespace out {\n"
3524             "  int i;\n"
3525             "  namespace in {\n"
3526             "    int i;\n"
3527             "  } // namespace in\n"
3528             "} // namespace out",
3529             format("namespace out {\n"
3530                    "int i;\n"
3531                    "namespace in {\n"
3532                    "int i;\n"
3533                    "} // namespace in\n"
3534                    "} // namespace out",
3535                    Style));
3536 
3537   Style.NamespaceIndentation = FormatStyle::NI_Inner;
3538   EXPECT_EQ("namespace out {\n"
3539             "int i;\n"
3540             "namespace in {\n"
3541             "  int i;\n"
3542             "} // namespace in\n"
3543             "} // namespace out",
3544             format("namespace out {\n"
3545                    "int i;\n"
3546                    "namespace in {\n"
3547                    "int i;\n"
3548                    "} // namespace in\n"
3549                    "} // namespace out",
3550                    Style));
3551 }
3552 
3553 TEST_F(FormatTest, NamespaceMacros) {
3554   FormatStyle Style = getLLVMStyle();
3555   Style.NamespaceMacros.push_back("TESTSUITE");
3556 
3557   verifyFormat("TESTSUITE(A) {\n"
3558                "int foo();\n"
3559                "} // TESTSUITE(A)",
3560                Style);
3561 
3562   verifyFormat("TESTSUITE(A, B) {\n"
3563                "int foo();\n"
3564                "} // TESTSUITE(A)",
3565                Style);
3566 
3567   // Properly indent according to NamespaceIndentation style
3568   Style.NamespaceIndentation = FormatStyle::NI_All;
3569   verifyFormat("TESTSUITE(A) {\n"
3570                "  int foo();\n"
3571                "} // TESTSUITE(A)",
3572                Style);
3573   verifyFormat("TESTSUITE(A) {\n"
3574                "  namespace B {\n"
3575                "    int foo();\n"
3576                "  } // namespace B\n"
3577                "} // TESTSUITE(A)",
3578                Style);
3579   verifyFormat("namespace A {\n"
3580                "  TESTSUITE(B) {\n"
3581                "    int foo();\n"
3582                "  } // TESTSUITE(B)\n"
3583                "} // namespace A",
3584                Style);
3585 
3586   Style.NamespaceIndentation = FormatStyle::NI_Inner;
3587   verifyFormat("TESTSUITE(A) {\n"
3588                "TESTSUITE(B) {\n"
3589                "  int foo();\n"
3590                "} // TESTSUITE(B)\n"
3591                "} // TESTSUITE(A)",
3592                Style);
3593   verifyFormat("TESTSUITE(A) {\n"
3594                "namespace B {\n"
3595                "  int foo();\n"
3596                "} // namespace B\n"
3597                "} // TESTSUITE(A)",
3598                Style);
3599   verifyFormat("namespace A {\n"
3600                "TESTSUITE(B) {\n"
3601                "  int foo();\n"
3602                "} // TESTSUITE(B)\n"
3603                "} // namespace A",
3604                Style);
3605 
3606   // Properly merge namespace-macros blocks in CompactNamespaces mode
3607   Style.NamespaceIndentation = FormatStyle::NI_None;
3608   Style.CompactNamespaces = true;
3609   verifyFormat("TESTSUITE(A) { TESTSUITE(B) {\n"
3610                "}} // TESTSUITE(A::B)",
3611                Style);
3612 
3613   EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n"
3614             "}} // TESTSUITE(out::in)",
3615             format("TESTSUITE(out) {\n"
3616                    "TESTSUITE(in) {\n"
3617                    "} // TESTSUITE(in)\n"
3618                    "} // TESTSUITE(out)",
3619                    Style));
3620 
3621   EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n"
3622             "}} // TESTSUITE(out::in)",
3623             format("TESTSUITE(out) {\n"
3624                    "TESTSUITE(in) {\n"
3625                    "} // TESTSUITE(in)\n"
3626                    "} // TESTSUITE(out)",
3627                    Style));
3628 
3629   // Do not merge different namespaces/macros
3630   EXPECT_EQ("namespace out {\n"
3631             "TESTSUITE(in) {\n"
3632             "} // TESTSUITE(in)\n"
3633             "} // namespace out",
3634             format("namespace out {\n"
3635                    "TESTSUITE(in) {\n"
3636                    "} // TESTSUITE(in)\n"
3637                    "} // namespace out",
3638                    Style));
3639   EXPECT_EQ("TESTSUITE(out) {\n"
3640             "namespace in {\n"
3641             "} // namespace in\n"
3642             "} // TESTSUITE(out)",
3643             format("TESTSUITE(out) {\n"
3644                    "namespace in {\n"
3645                    "} // namespace in\n"
3646                    "} // TESTSUITE(out)",
3647                    Style));
3648   Style.NamespaceMacros.push_back("FOOBAR");
3649   EXPECT_EQ("TESTSUITE(out) {\n"
3650             "FOOBAR(in) {\n"
3651             "} // FOOBAR(in)\n"
3652             "} // TESTSUITE(out)",
3653             format("TESTSUITE(out) {\n"
3654                    "FOOBAR(in) {\n"
3655                    "} // FOOBAR(in)\n"
3656                    "} // TESTSUITE(out)",
3657                    Style));
3658 }
3659 
3660 TEST_F(FormatTest, FormatsCompactNamespaces) {
3661   FormatStyle Style = getLLVMStyle();
3662   Style.CompactNamespaces = true;
3663   Style.NamespaceMacros.push_back("TESTSUITE");
3664 
3665   verifyFormat("namespace A { namespace B {\n"
3666                "}} // namespace A::B",
3667                Style);
3668 
3669   EXPECT_EQ("namespace out { namespace in {\n"
3670             "}} // namespace out::in",
3671             format("namespace out {\n"
3672                    "namespace in {\n"
3673                    "} // namespace in\n"
3674                    "} // namespace out",
3675                    Style));
3676 
3677   // Only namespaces which have both consecutive opening and end get compacted
3678   EXPECT_EQ("namespace out {\n"
3679             "namespace in1 {\n"
3680             "} // namespace in1\n"
3681             "namespace in2 {\n"
3682             "} // namespace in2\n"
3683             "} // namespace out",
3684             format("namespace out {\n"
3685                    "namespace in1 {\n"
3686                    "} // namespace in1\n"
3687                    "namespace in2 {\n"
3688                    "} // namespace in2\n"
3689                    "} // namespace out",
3690                    Style));
3691 
3692   EXPECT_EQ("namespace out {\n"
3693             "int i;\n"
3694             "namespace in {\n"
3695             "int j;\n"
3696             "} // namespace in\n"
3697             "int k;\n"
3698             "} // namespace out",
3699             format("namespace out { int i;\n"
3700                    "namespace in { int j; } // namespace in\n"
3701                    "int k; } // namespace out",
3702                    Style));
3703 
3704   EXPECT_EQ("namespace A { namespace B { namespace C {\n"
3705             "}}} // namespace A::B::C\n",
3706             format("namespace A { namespace B {\n"
3707                    "namespace C {\n"
3708                    "}} // namespace B::C\n"
3709                    "} // namespace A\n",
3710                    Style));
3711 
3712   Style.ColumnLimit = 40;
3713   EXPECT_EQ("namespace aaaaaaaaaa {\n"
3714             "namespace bbbbbbbbbb {\n"
3715             "}} // namespace aaaaaaaaaa::bbbbbbbbbb",
3716             format("namespace aaaaaaaaaa {\n"
3717                    "namespace bbbbbbbbbb {\n"
3718                    "} // namespace bbbbbbbbbb\n"
3719                    "} // namespace aaaaaaaaaa",
3720                    Style));
3721 
3722   EXPECT_EQ("namespace aaaaaa { namespace bbbbbb {\n"
3723             "namespace cccccc {\n"
3724             "}}} // namespace aaaaaa::bbbbbb::cccccc",
3725             format("namespace aaaaaa {\n"
3726                    "namespace bbbbbb {\n"
3727                    "namespace cccccc {\n"
3728                    "} // namespace cccccc\n"
3729                    "} // namespace bbbbbb\n"
3730                    "} // namespace aaaaaa",
3731                    Style));
3732   Style.ColumnLimit = 80;
3733 
3734   // Extra semicolon after 'inner' closing brace prevents merging
3735   EXPECT_EQ("namespace out { namespace in {\n"
3736             "}; } // namespace out::in",
3737             format("namespace out {\n"
3738                    "namespace in {\n"
3739                    "}; // namespace in\n"
3740                    "} // namespace out",
3741                    Style));
3742 
3743   // Extra semicolon after 'outer' closing brace is conserved
3744   EXPECT_EQ("namespace out { namespace in {\n"
3745             "}}; // namespace out::in",
3746             format("namespace out {\n"
3747                    "namespace in {\n"
3748                    "} // namespace in\n"
3749                    "}; // namespace out",
3750                    Style));
3751 
3752   Style.NamespaceIndentation = FormatStyle::NI_All;
3753   EXPECT_EQ("namespace out { namespace in {\n"
3754             "  int i;\n"
3755             "}} // namespace out::in",
3756             format("namespace out {\n"
3757                    "namespace in {\n"
3758                    "int i;\n"
3759                    "} // namespace in\n"
3760                    "} // namespace out",
3761                    Style));
3762   EXPECT_EQ("namespace out { namespace mid {\n"
3763             "  namespace in {\n"
3764             "    int j;\n"
3765             "  } // namespace in\n"
3766             "  int k;\n"
3767             "}} // namespace out::mid",
3768             format("namespace out { namespace mid {\n"
3769                    "namespace in { int j; } // namespace in\n"
3770                    "int k; }} // namespace out::mid",
3771                    Style));
3772 
3773   Style.NamespaceIndentation = FormatStyle::NI_Inner;
3774   EXPECT_EQ("namespace out { namespace in {\n"
3775             "  int i;\n"
3776             "}} // namespace out::in",
3777             format("namespace out {\n"
3778                    "namespace in {\n"
3779                    "int i;\n"
3780                    "} // namespace in\n"
3781                    "} // namespace out",
3782                    Style));
3783   EXPECT_EQ("namespace out { namespace mid { namespace in {\n"
3784             "  int i;\n"
3785             "}}} // namespace out::mid::in",
3786             format("namespace out {\n"
3787                    "namespace mid {\n"
3788                    "namespace in {\n"
3789                    "int i;\n"
3790                    "} // namespace in\n"
3791                    "} // namespace mid\n"
3792                    "} // namespace out",
3793                    Style));
3794 }
3795 
3796 TEST_F(FormatTest, FormatsExternC) {
3797   verifyFormat("extern \"C\" {\nint a;");
3798   verifyFormat("extern \"C\" {}");
3799   verifyFormat("extern \"C\" {\n"
3800                "int foo();\n"
3801                "}");
3802   verifyFormat("extern \"C\" int foo() {}");
3803   verifyFormat("extern \"C\" int foo();");
3804   verifyFormat("extern \"C\" int foo() {\n"
3805                "  int i = 42;\n"
3806                "  return i;\n"
3807                "}");
3808 
3809   FormatStyle Style = getLLVMStyle();
3810   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
3811   Style.BraceWrapping.AfterFunction = true;
3812   verifyFormat("extern \"C\" int foo() {}", Style);
3813   verifyFormat("extern \"C\" int foo();", Style);
3814   verifyFormat("extern \"C\" int foo()\n"
3815                "{\n"
3816                "  int i = 42;\n"
3817                "  return i;\n"
3818                "}",
3819                Style);
3820 
3821   Style.BraceWrapping.AfterExternBlock = true;
3822   Style.BraceWrapping.SplitEmptyRecord = false;
3823   verifyFormat("extern \"C\"\n"
3824                "{}",
3825                Style);
3826   verifyFormat("extern \"C\"\n"
3827                "{\n"
3828                "  int foo();\n"
3829                "}",
3830                Style);
3831 }
3832 
3833 TEST_F(FormatTest, IndentExternBlockStyle) {
3834   FormatStyle Style = getLLVMStyle();
3835   Style.IndentWidth = 2;
3836 
3837   Style.IndentExternBlock = FormatStyle::IEBS_Indent;
3838   verifyFormat("extern \"C\" { /*9*/\n}", Style);
3839   verifyFormat("extern \"C\" {\n"
3840                "  int foo10();\n"
3841                "}",
3842                Style);
3843 
3844   Style.IndentExternBlock = FormatStyle::IEBS_NoIndent;
3845   verifyFormat("extern \"C\" { /*11*/\n}", Style);
3846   verifyFormat("extern \"C\" {\n"
3847                "int foo12();\n"
3848                "}",
3849                Style);
3850 
3851   Style.IndentExternBlock = FormatStyle::IEBS_AfterExternBlock;
3852   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
3853   Style.BraceWrapping.AfterExternBlock = true;
3854   verifyFormat("extern \"C\"\n{ /*13*/\n}", Style);
3855   verifyFormat("extern \"C\"\n{\n"
3856                "  int foo14();\n"
3857                "}",
3858                Style);
3859 
3860   Style.IndentExternBlock = FormatStyle::IEBS_AfterExternBlock;
3861   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
3862   Style.BraceWrapping.AfterExternBlock = false;
3863   verifyFormat("extern \"C\" { /*15*/\n}", Style);
3864   verifyFormat("extern \"C\" {\n"
3865                "int foo16();\n"
3866                "}",
3867                Style);
3868 }
3869 
3870 TEST_F(FormatTest, FormatsInlineASM) {
3871   verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
3872   verifyFormat("asm(\"nop\" ::: \"memory\");");
3873   verifyFormat(
3874       "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
3875       "    \"cpuid\\n\\t\"\n"
3876       "    \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
3877       "    : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
3878       "    : \"a\"(value));");
3879   EXPECT_EQ(
3880       "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
3881       "  __asm {\n"
3882       "        mov     edx,[that] // vtable in edx\n"
3883       "        mov     eax,methodIndex\n"
3884       "        call    [edx][eax*4] // stdcall\n"
3885       "  }\n"
3886       "}",
3887       format("void NS_InvokeByIndex(void *that,   unsigned int methodIndex) {\n"
3888              "    __asm {\n"
3889              "        mov     edx,[that] // vtable in edx\n"
3890              "        mov     eax,methodIndex\n"
3891              "        call    [edx][eax*4] // stdcall\n"
3892              "    }\n"
3893              "}"));
3894   EXPECT_EQ("_asm {\n"
3895             "  xor eax, eax;\n"
3896             "  cpuid;\n"
3897             "}",
3898             format("_asm {\n"
3899                    "  xor eax, eax;\n"
3900                    "  cpuid;\n"
3901                    "}"));
3902   verifyFormat("void function() {\n"
3903                "  // comment\n"
3904                "  asm(\"\");\n"
3905                "}");
3906   EXPECT_EQ("__asm {\n"
3907             "}\n"
3908             "int i;",
3909             format("__asm   {\n"
3910                    "}\n"
3911                    "int   i;"));
3912 }
3913 
3914 TEST_F(FormatTest, FormatTryCatch) {
3915   verifyFormat("try {\n"
3916                "  throw a * b;\n"
3917                "} catch (int a) {\n"
3918                "  // Do nothing.\n"
3919                "} catch (...) {\n"
3920                "  exit(42);\n"
3921                "}");
3922 
3923   // Function-level try statements.
3924   verifyFormat("int f() try { return 4; } catch (...) {\n"
3925                "  return 5;\n"
3926                "}");
3927   verifyFormat("class A {\n"
3928                "  int a;\n"
3929                "  A() try : a(0) {\n"
3930                "  } catch (...) {\n"
3931                "    throw;\n"
3932                "  }\n"
3933                "};\n");
3934   verifyFormat("class A {\n"
3935                "  int a;\n"
3936                "  A() try : a(0), b{1} {\n"
3937                "  } catch (...) {\n"
3938                "    throw;\n"
3939                "  }\n"
3940                "};\n");
3941   verifyFormat("class A {\n"
3942                "  int a;\n"
3943                "  A() try : a(0), b{1}, c{2} {\n"
3944                "  } catch (...) {\n"
3945                "    throw;\n"
3946                "  }\n"
3947                "};\n");
3948   verifyFormat("class A {\n"
3949                "  int a;\n"
3950                "  A() try : a(0), b{1}, c{2} {\n"
3951                "    { // New scope.\n"
3952                "    }\n"
3953                "  } catch (...) {\n"
3954                "    throw;\n"
3955                "  }\n"
3956                "};\n");
3957 
3958   // Incomplete try-catch blocks.
3959   verifyIncompleteFormat("try {} catch (");
3960 }
3961 
3962 TEST_F(FormatTest, FormatTryAsAVariable) {
3963   verifyFormat("int try;");
3964   verifyFormat("int try, size;");
3965   verifyFormat("try = foo();");
3966   verifyFormat("if (try < size) {\n  return true;\n}");
3967 
3968   verifyFormat("int catch;");
3969   verifyFormat("int catch, size;");
3970   verifyFormat("catch = foo();");
3971   verifyFormat("if (catch < size) {\n  return true;\n}");
3972 
3973   FormatStyle Style = getLLVMStyle();
3974   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
3975   Style.BraceWrapping.AfterFunction = true;
3976   Style.BraceWrapping.BeforeCatch = true;
3977   verifyFormat("try {\n"
3978                "  int bar = 1;\n"
3979                "}\n"
3980                "catch (...) {\n"
3981                "  int bar = 1;\n"
3982                "}",
3983                Style);
3984   verifyFormat("#if NO_EX\n"
3985                "try\n"
3986                "#endif\n"
3987                "{\n"
3988                "}\n"
3989                "#if NO_EX\n"
3990                "catch (...) {\n"
3991                "}",
3992                Style);
3993   verifyFormat("try /* abc */ {\n"
3994                "  int bar = 1;\n"
3995                "}\n"
3996                "catch (...) {\n"
3997                "  int bar = 1;\n"
3998                "}",
3999                Style);
4000   verifyFormat("try\n"
4001                "// abc\n"
4002                "{\n"
4003                "  int bar = 1;\n"
4004                "}\n"
4005                "catch (...) {\n"
4006                "  int bar = 1;\n"
4007                "}",
4008                Style);
4009 }
4010 
4011 TEST_F(FormatTest, FormatSEHTryCatch) {
4012   verifyFormat("__try {\n"
4013                "  int a = b * c;\n"
4014                "} __except (EXCEPTION_EXECUTE_HANDLER) {\n"
4015                "  // Do nothing.\n"
4016                "}");
4017 
4018   verifyFormat("__try {\n"
4019                "  int a = b * c;\n"
4020                "} __finally {\n"
4021                "  // Do nothing.\n"
4022                "}");
4023 
4024   verifyFormat("DEBUG({\n"
4025                "  __try {\n"
4026                "  } __finally {\n"
4027                "  }\n"
4028                "});\n");
4029 }
4030 
4031 TEST_F(FormatTest, IncompleteTryCatchBlocks) {
4032   verifyFormat("try {\n"
4033                "  f();\n"
4034                "} catch {\n"
4035                "  g();\n"
4036                "}");
4037   verifyFormat("try {\n"
4038                "  f();\n"
4039                "} catch (A a) MACRO(x) {\n"
4040                "  g();\n"
4041                "} catch (B b) MACRO(x) {\n"
4042                "  g();\n"
4043                "}");
4044 }
4045 
4046 TEST_F(FormatTest, FormatTryCatchBraceStyles) {
4047   FormatStyle Style = getLLVMStyle();
4048   for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla,
4049                           FormatStyle::BS_WebKit}) {
4050     Style.BreakBeforeBraces = BraceStyle;
4051     verifyFormat("try {\n"
4052                  "  // something\n"
4053                  "} catch (...) {\n"
4054                  "  // something\n"
4055                  "}",
4056                  Style);
4057   }
4058   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
4059   verifyFormat("try {\n"
4060                "  // something\n"
4061                "}\n"
4062                "catch (...) {\n"
4063                "  // something\n"
4064                "}",
4065                Style);
4066   verifyFormat("__try {\n"
4067                "  // something\n"
4068                "}\n"
4069                "__finally {\n"
4070                "  // something\n"
4071                "}",
4072                Style);
4073   verifyFormat("@try {\n"
4074                "  // something\n"
4075                "}\n"
4076                "@finally {\n"
4077                "  // something\n"
4078                "}",
4079                Style);
4080   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
4081   verifyFormat("try\n"
4082                "{\n"
4083                "  // something\n"
4084                "}\n"
4085                "catch (...)\n"
4086                "{\n"
4087                "  // something\n"
4088                "}",
4089                Style);
4090   Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
4091   verifyFormat("try\n"
4092                "  {\n"
4093                "  // something white\n"
4094                "  }\n"
4095                "catch (...)\n"
4096                "  {\n"
4097                "  // something white\n"
4098                "  }",
4099                Style);
4100   Style.BreakBeforeBraces = FormatStyle::BS_GNU;
4101   verifyFormat("try\n"
4102                "  {\n"
4103                "    // something\n"
4104                "  }\n"
4105                "catch (...)\n"
4106                "  {\n"
4107                "    // something\n"
4108                "  }",
4109                Style);
4110   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4111   Style.BraceWrapping.BeforeCatch = true;
4112   verifyFormat("try {\n"
4113                "  // something\n"
4114                "}\n"
4115                "catch (...) {\n"
4116                "  // something\n"
4117                "}",
4118                Style);
4119 }
4120 
4121 TEST_F(FormatTest, StaticInitializers) {
4122   verifyFormat("static SomeClass SC = {1, 'a'};");
4123 
4124   verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n"
4125                "    100000000, "
4126                "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};");
4127 
4128   // Here, everything other than the "}" would fit on a line.
4129   verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
4130                "    10000000000000000000000000};");
4131   EXPECT_EQ("S s = {a,\n"
4132             "\n"
4133             "       b};",
4134             format("S s = {\n"
4135                    "  a,\n"
4136                    "\n"
4137                    "  b\n"
4138                    "};"));
4139 
4140   // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
4141   // line. However, the formatting looks a bit off and this probably doesn't
4142   // happen often in practice.
4143   verifyFormat("static int Variable[1] = {\n"
4144                "    {1000000000000000000000000000000000000}};",
4145                getLLVMStyleWithColumns(40));
4146 }
4147 
4148 TEST_F(FormatTest, DesignatedInitializers) {
4149   verifyFormat("const struct A a = {.a = 1, .b = 2};");
4150   verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n"
4151                "                    .bbbbbbbbbb = 2,\n"
4152                "                    .cccccccccc = 3,\n"
4153                "                    .dddddddddd = 4,\n"
4154                "                    .eeeeeeeeee = 5};");
4155   verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
4156                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
4157                "    .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
4158                "    .ccccccccccccccccccccccccccc = 3,\n"
4159                "    .ddddddddddddddddddddddddddd = 4,\n"
4160                "    .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};");
4161 
4162   verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
4163 
4164   verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
4165   verifyFormat("const struct A a = {[1] = aaaaaaaaaa,\n"
4166                "                    [2] = bbbbbbbbbb,\n"
4167                "                    [3] = cccccccccc,\n"
4168                "                    [4] = dddddddddd,\n"
4169                "                    [5] = eeeeeeeeee};");
4170   verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
4171                "    [1] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4172                "    [2] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
4173                "    [3] = cccccccccccccccccccccccccccccccccccccc,\n"
4174                "    [4] = dddddddddddddddddddddddddddddddddddddd,\n"
4175                "    [5] = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee};");
4176 }
4177 
4178 TEST_F(FormatTest, NestedStaticInitializers) {
4179   verifyFormat("static A x = {{{}}};\n");
4180   verifyFormat("static A x = {{{init1, init2, init3, init4},\n"
4181                "               {init1, init2, init3, init4}}};",
4182                getLLVMStyleWithColumns(50));
4183 
4184   verifyFormat("somes Status::global_reps[3] = {\n"
4185                "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
4186                "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
4187                "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};",
4188                getLLVMStyleWithColumns(60));
4189   verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
4190                      "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
4191                      "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
4192                      "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
4193   verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n"
4194                "                  {rect.fRight - rect.fLeft, rect.fBottom - "
4195                "rect.fTop}};");
4196 
4197   verifyFormat(
4198       "SomeArrayOfSomeType a = {\n"
4199       "    {{1, 2, 3},\n"
4200       "     {1, 2, 3},\n"
4201       "     {111111111111111111111111111111, 222222222222222222222222222222,\n"
4202       "      333333333333333333333333333333},\n"
4203       "     {1, 2, 3},\n"
4204       "     {1, 2, 3}}};");
4205   verifyFormat(
4206       "SomeArrayOfSomeType a = {\n"
4207       "    {{1, 2, 3}},\n"
4208       "    {{1, 2, 3}},\n"
4209       "    {{111111111111111111111111111111, 222222222222222222222222222222,\n"
4210       "      333333333333333333333333333333}},\n"
4211       "    {{1, 2, 3}},\n"
4212       "    {{1, 2, 3}}};");
4213 
4214   verifyFormat("struct {\n"
4215                "  unsigned bit;\n"
4216                "  const char *const name;\n"
4217                "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n"
4218                "                 {kOsWin, \"Windows\"},\n"
4219                "                 {kOsLinux, \"Linux\"},\n"
4220                "                 {kOsCrOS, \"Chrome OS\"}};");
4221   verifyFormat("struct {\n"
4222                "  unsigned bit;\n"
4223                "  const char *const name;\n"
4224                "} kBitsToOs[] = {\n"
4225                "    {kOsMac, \"Mac\"},\n"
4226                "    {kOsWin, \"Windows\"},\n"
4227                "    {kOsLinux, \"Linux\"},\n"
4228                "    {kOsCrOS, \"Chrome OS\"},\n"
4229                "};");
4230 }
4231 
4232 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
4233   verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
4234                "                      \\\n"
4235                "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
4236 }
4237 
4238 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
4239   verifyFormat("virtual void write(ELFWriter *writerrr,\n"
4240                "                   OwningPtr<FileOutputBuffer> &buffer) = 0;");
4241 
4242   // Do break defaulted and deleted functions.
4243   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
4244                "    default;",
4245                getLLVMStyleWithColumns(40));
4246   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
4247                "    delete;",
4248                getLLVMStyleWithColumns(40));
4249 }
4250 
4251 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) {
4252   verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3",
4253                getLLVMStyleWithColumns(40));
4254   verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
4255                getLLVMStyleWithColumns(40));
4256   EXPECT_EQ("#define Q                              \\\n"
4257             "  \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\"    \\\n"
4258             "  \"aaaaaaaa.cpp\"",
4259             format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
4260                    getLLVMStyleWithColumns(40)));
4261 }
4262 
4263 TEST_F(FormatTest, UnderstandsLinePPDirective) {
4264   EXPECT_EQ("# 123 \"A string literal\"",
4265             format("   #     123    \"A string literal\""));
4266 }
4267 
4268 TEST_F(FormatTest, LayoutUnknownPPDirective) {
4269   EXPECT_EQ("#;", format("#;"));
4270   verifyFormat("#\n;\n;\n;");
4271 }
4272 
4273 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
4274   EXPECT_EQ("#line 42 \"test\"\n",
4275             format("#  \\\n  line  \\\n  42  \\\n  \"test\"\n"));
4276   EXPECT_EQ("#define A B\n", format("#  \\\n define  \\\n    A  \\\n       B\n",
4277                                     getLLVMStyleWithColumns(12)));
4278 }
4279 
4280 TEST_F(FormatTest, EndOfFileEndsPPDirective) {
4281   EXPECT_EQ("#line 42 \"test\"",
4282             format("#  \\\n  line  \\\n  42  \\\n  \"test\""));
4283   EXPECT_EQ("#define A B", format("#  \\\n define  \\\n    A  \\\n       B"));
4284 }
4285 
4286 TEST_F(FormatTest, DoesntRemoveUnknownTokens) {
4287   verifyFormat("#define A \\x20");
4288   verifyFormat("#define A \\ x20");
4289   EXPECT_EQ("#define A \\ x20", format("#define A \\   x20"));
4290   verifyFormat("#define A ''");
4291   verifyFormat("#define A ''qqq");
4292   verifyFormat("#define A `qqq");
4293   verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");");
4294   EXPECT_EQ("const char *c = STRINGIFY(\n"
4295             "\\na : b);",
4296             format("const char * c = STRINGIFY(\n"
4297                    "\\na : b);"));
4298 
4299   verifyFormat("a\r\\");
4300   verifyFormat("a\v\\");
4301   verifyFormat("a\f\\");
4302 }
4303 
4304 TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) {
4305   FormatStyle style = getChromiumStyle(FormatStyle::LK_Cpp);
4306   style.IndentWidth = 4;
4307   style.PPIndentWidth = 1;
4308 
4309   style.IndentPPDirectives = FormatStyle::PPDIS_None;
4310   verifyFormat("#ifdef __linux__\n"
4311                "void foo() {\n"
4312                "    int x = 0;\n"
4313                "}\n"
4314                "#define FOO\n"
4315                "#endif\n"
4316                "void bar() {\n"
4317                "    int y = 0;\n"
4318                "}\n",
4319                style);
4320 
4321   style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
4322   verifyFormat("#ifdef __linux__\n"
4323                "void foo() {\n"
4324                "    int x = 0;\n"
4325                "}\n"
4326                "# define FOO foo\n"
4327                "#endif\n"
4328                "void bar() {\n"
4329                "    int y = 0;\n"
4330                "}\n",
4331                style);
4332 
4333   style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash;
4334   verifyFormat("#ifdef __linux__\n"
4335                "void foo() {\n"
4336                "    int x = 0;\n"
4337                "}\n"
4338                " #define FOO foo\n"
4339                "#endif\n"
4340                "void bar() {\n"
4341                "    int y = 0;\n"
4342                "}\n",
4343                style);
4344 }
4345 
4346 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
4347   verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
4348   verifyFormat("#define A( \\\n    BB)", getLLVMStyleWithColumns(12));
4349   verifyFormat("#define A( \\\n    A, B)", getLLVMStyleWithColumns(12));
4350   // FIXME: We never break before the macro name.
4351   verifyFormat("#define AA( \\\n    B)", getLLVMStyleWithColumns(12));
4352 
4353   verifyFormat("#define A A\n#define A A");
4354   verifyFormat("#define A(X) A\n#define A A");
4355 
4356   verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
4357   verifyFormat("#define Something    \\\n  Other", getLLVMStyleWithColumns(22));
4358 }
4359 
4360 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
4361   EXPECT_EQ("// somecomment\n"
4362             "#include \"a.h\"\n"
4363             "#define A(  \\\n"
4364             "    A, B)\n"
4365             "#include \"b.h\"\n"
4366             "// somecomment\n",
4367             format("  // somecomment\n"
4368                    "  #include \"a.h\"\n"
4369                    "#define A(A,\\\n"
4370                    "    B)\n"
4371                    "    #include \"b.h\"\n"
4372                    " // somecomment\n",
4373                    getLLVMStyleWithColumns(13)));
4374 }
4375 
4376 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); }
4377 
4378 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
4379   EXPECT_EQ("#define A    \\\n"
4380             "  c;         \\\n"
4381             "  e;\n"
4382             "f;",
4383             format("#define A c; e;\n"
4384                    "f;",
4385                    getLLVMStyleWithColumns(14)));
4386 }
4387 
4388 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
4389 
4390 TEST_F(FormatTest, MacroDefinitionInsideStatement) {
4391   EXPECT_EQ("int x,\n"
4392             "#define A\n"
4393             "    y;",
4394             format("int x,\n#define A\ny;"));
4395 }
4396 
4397 TEST_F(FormatTest, HashInMacroDefinition) {
4398   EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle()));
4399   verifyFormat("#define A \\\n  b #c;", getLLVMStyleWithColumns(11));
4400   verifyFormat("#define A  \\\n"
4401                "  {        \\\n"
4402                "    f(#c); \\\n"
4403                "  }",
4404                getLLVMStyleWithColumns(11));
4405 
4406   verifyFormat("#define A(X)         \\\n"
4407                "  void function##X()",
4408                getLLVMStyleWithColumns(22));
4409 
4410   verifyFormat("#define A(a, b, c)   \\\n"
4411                "  void a##b##c()",
4412                getLLVMStyleWithColumns(22));
4413 
4414   verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
4415 }
4416 
4417 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
4418   EXPECT_EQ("#define A (x)", format("#define A (x)"));
4419   EXPECT_EQ("#define A(x)", format("#define A(x)"));
4420 
4421   FormatStyle Style = getLLVMStyle();
4422   Style.SpaceBeforeParens = FormatStyle::SBPO_Never;
4423   verifyFormat("#define true ((foo)1)", Style);
4424   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
4425   verifyFormat("#define false((foo)0)", Style);
4426 }
4427 
4428 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
4429   EXPECT_EQ("#define A b;", format("#define A \\\n"
4430                                    "          \\\n"
4431                                    "  b;",
4432                                    getLLVMStyleWithColumns(25)));
4433   EXPECT_EQ("#define A \\\n"
4434             "          \\\n"
4435             "  a;      \\\n"
4436             "  b;",
4437             format("#define A \\\n"
4438                    "          \\\n"
4439                    "  a;      \\\n"
4440                    "  b;",
4441                    getLLVMStyleWithColumns(11)));
4442   EXPECT_EQ("#define A \\\n"
4443             "  a;      \\\n"
4444             "          \\\n"
4445             "  b;",
4446             format("#define A \\\n"
4447                    "  a;      \\\n"
4448                    "          \\\n"
4449                    "  b;",
4450                    getLLVMStyleWithColumns(11)));
4451 }
4452 
4453 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
4454   verifyIncompleteFormat("#define A :");
4455   verifyFormat("#define SOMECASES  \\\n"
4456                "  case 1:          \\\n"
4457                "  case 2\n",
4458                getLLVMStyleWithColumns(20));
4459   verifyFormat("#define MACRO(a) \\\n"
4460                "  if (a)         \\\n"
4461                "    f();         \\\n"
4462                "  else           \\\n"
4463                "    g()",
4464                getLLVMStyleWithColumns(18));
4465   verifyFormat("#define A template <typename T>");
4466   verifyIncompleteFormat("#define STR(x) #x\n"
4467                          "f(STR(this_is_a_string_literal{));");
4468   verifyFormat("#pragma omp threadprivate( \\\n"
4469                "    y)), // expected-warning",
4470                getLLVMStyleWithColumns(28));
4471   verifyFormat("#d, = };");
4472   verifyFormat("#if \"a");
4473   verifyIncompleteFormat("({\n"
4474                          "#define b     \\\n"
4475                          "  }           \\\n"
4476                          "  a\n"
4477                          "a",
4478                          getLLVMStyleWithColumns(15));
4479   verifyFormat("#define A     \\\n"
4480                "  {           \\\n"
4481                "    {\n"
4482                "#define B     \\\n"
4483                "  }           \\\n"
4484                "  }",
4485                getLLVMStyleWithColumns(15));
4486   verifyNoCrash("#if a\na(\n#else\n#endif\n{a");
4487   verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}");
4488   verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};");
4489   verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() {      \n)}");
4490 }
4491 
4492 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
4493   verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
4494   EXPECT_EQ("class A : public QObject {\n"
4495             "  Q_OBJECT\n"
4496             "\n"
4497             "  A() {}\n"
4498             "};",
4499             format("class A  :  public QObject {\n"
4500                    "     Q_OBJECT\n"
4501                    "\n"
4502                    "  A() {\n}\n"
4503                    "}  ;"));
4504   EXPECT_EQ("MACRO\n"
4505             "/*static*/ int i;",
4506             format("MACRO\n"
4507                    " /*static*/ int   i;"));
4508   EXPECT_EQ("SOME_MACRO\n"
4509             "namespace {\n"
4510             "void f();\n"
4511             "} // namespace",
4512             format("SOME_MACRO\n"
4513                    "  namespace    {\n"
4514                    "void   f(  );\n"
4515                    "} // namespace"));
4516   // Only if the identifier contains at least 5 characters.
4517   EXPECT_EQ("HTTP f();", format("HTTP\nf();"));
4518   EXPECT_EQ("MACRO\nf();", format("MACRO\nf();"));
4519   // Only if everything is upper case.
4520   EXPECT_EQ("class A : public QObject {\n"
4521             "  Q_Object A() {}\n"
4522             "};",
4523             format("class A  :  public QObject {\n"
4524                    "     Q_Object\n"
4525                    "  A() {\n}\n"
4526                    "}  ;"));
4527 
4528   // Only if the next line can actually start an unwrapped line.
4529   EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;",
4530             format("SOME_WEIRD_LOG_MACRO\n"
4531                    "<< SomeThing;"));
4532 
4533   verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
4534                "(n, buffers))\n",
4535                getChromiumStyle(FormatStyle::LK_Cpp));
4536 
4537   // See PR41483
4538   EXPECT_EQ("/**/ FOO(a)\n"
4539             "FOO(b)",
4540             format("/**/ FOO(a)\n"
4541                    "FOO(b)"));
4542 }
4543 
4544 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
4545   EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
4546             "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
4547             "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
4548             "class X {};\n"
4549             "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
4550             "int *createScopDetectionPass() { return 0; }",
4551             format("  INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
4552                    "  INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
4553                    "  INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
4554                    "  class X {};\n"
4555                    "  INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
4556                    "  int *createScopDetectionPass() { return 0; }"));
4557   // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
4558   // braces, so that inner block is indented one level more.
4559   EXPECT_EQ("int q() {\n"
4560             "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
4561             "  IPC_MESSAGE_HANDLER(xxx, qqq)\n"
4562             "  IPC_END_MESSAGE_MAP()\n"
4563             "}",
4564             format("int q() {\n"
4565                    "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
4566                    "    IPC_MESSAGE_HANDLER(xxx, qqq)\n"
4567                    "  IPC_END_MESSAGE_MAP()\n"
4568                    "}"));
4569 
4570   // Same inside macros.
4571   EXPECT_EQ("#define LIST(L) \\\n"
4572             "  L(A)          \\\n"
4573             "  L(B)          \\\n"
4574             "  L(C)",
4575             format("#define LIST(L) \\\n"
4576                    "  L(A) \\\n"
4577                    "  L(B) \\\n"
4578                    "  L(C)",
4579                    getGoogleStyle()));
4580 
4581   // These must not be recognized as macros.
4582   EXPECT_EQ("int q() {\n"
4583             "  f(x);\n"
4584             "  f(x) {}\n"
4585             "  f(x)->g();\n"
4586             "  f(x)->*g();\n"
4587             "  f(x).g();\n"
4588             "  f(x) = x;\n"
4589             "  f(x) += x;\n"
4590             "  f(x) -= x;\n"
4591             "  f(x) *= x;\n"
4592             "  f(x) /= x;\n"
4593             "  f(x) %= x;\n"
4594             "  f(x) &= x;\n"
4595             "  f(x) |= x;\n"
4596             "  f(x) ^= x;\n"
4597             "  f(x) >>= x;\n"
4598             "  f(x) <<= x;\n"
4599             "  f(x)[y].z();\n"
4600             "  LOG(INFO) << x;\n"
4601             "  ifstream(x) >> x;\n"
4602             "}\n",
4603             format("int q() {\n"
4604                    "  f(x)\n;\n"
4605                    "  f(x)\n {}\n"
4606                    "  f(x)\n->g();\n"
4607                    "  f(x)\n->*g();\n"
4608                    "  f(x)\n.g();\n"
4609                    "  f(x)\n = x;\n"
4610                    "  f(x)\n += x;\n"
4611                    "  f(x)\n -= x;\n"
4612                    "  f(x)\n *= x;\n"
4613                    "  f(x)\n /= x;\n"
4614                    "  f(x)\n %= x;\n"
4615                    "  f(x)\n &= x;\n"
4616                    "  f(x)\n |= x;\n"
4617                    "  f(x)\n ^= x;\n"
4618                    "  f(x)\n >>= x;\n"
4619                    "  f(x)\n <<= x;\n"
4620                    "  f(x)\n[y].z();\n"
4621                    "  LOG(INFO)\n << x;\n"
4622                    "  ifstream(x)\n >> x;\n"
4623                    "}\n"));
4624   EXPECT_EQ("int q() {\n"
4625             "  F(x)\n"
4626             "  if (1) {\n"
4627             "  }\n"
4628             "  F(x)\n"
4629             "  while (1) {\n"
4630             "  }\n"
4631             "  F(x)\n"
4632             "  G(x);\n"
4633             "  F(x)\n"
4634             "  try {\n"
4635             "    Q();\n"
4636             "  } catch (...) {\n"
4637             "  }\n"
4638             "}\n",
4639             format("int q() {\n"
4640                    "F(x)\n"
4641                    "if (1) {}\n"
4642                    "F(x)\n"
4643                    "while (1) {}\n"
4644                    "F(x)\n"
4645                    "G(x);\n"
4646                    "F(x)\n"
4647                    "try { Q(); } catch (...) {}\n"
4648                    "}\n"));
4649   EXPECT_EQ("class A {\n"
4650             "  A() : t(0) {}\n"
4651             "  A(int i) noexcept() : {}\n"
4652             "  A(X x)\n" // FIXME: function-level try blocks are broken.
4653             "  try : t(0) {\n"
4654             "  } catch (...) {\n"
4655             "  }\n"
4656             "};",
4657             format("class A {\n"
4658                    "  A()\n : t(0) {}\n"
4659                    "  A(int i)\n noexcept() : {}\n"
4660                    "  A(X x)\n"
4661                    "  try : t(0) {} catch (...) {}\n"
4662                    "};"));
4663   FormatStyle Style = getLLVMStyle();
4664   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4665   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
4666   Style.BraceWrapping.AfterFunction = true;
4667   EXPECT_EQ("void f()\n"
4668             "try\n"
4669             "{\n"
4670             "}",
4671             format("void f() try {\n"
4672                    "}",
4673                    Style));
4674   EXPECT_EQ("class SomeClass {\n"
4675             "public:\n"
4676             "  SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
4677             "};",
4678             format("class SomeClass {\n"
4679                    "public:\n"
4680                    "  SomeClass()\n"
4681                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
4682                    "};"));
4683   EXPECT_EQ("class SomeClass {\n"
4684             "public:\n"
4685             "  SomeClass()\n"
4686             "      EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
4687             "};",
4688             format("class SomeClass {\n"
4689                    "public:\n"
4690                    "  SomeClass()\n"
4691                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
4692                    "};",
4693                    getLLVMStyleWithColumns(40)));
4694 
4695   verifyFormat("MACRO(>)");
4696 
4697   // Some macros contain an implicit semicolon.
4698   Style = getLLVMStyle();
4699   Style.StatementMacros.push_back("FOO");
4700   verifyFormat("FOO(a) int b = 0;");
4701   verifyFormat("FOO(a)\n"
4702                "int b = 0;",
4703                Style);
4704   verifyFormat("FOO(a);\n"
4705                "int b = 0;",
4706                Style);
4707   verifyFormat("FOO(argc, argv, \"4.0.2\")\n"
4708                "int b = 0;",
4709                Style);
4710   verifyFormat("FOO()\n"
4711                "int b = 0;",
4712                Style);
4713   verifyFormat("FOO\n"
4714                "int b = 0;",
4715                Style);
4716   verifyFormat("void f() {\n"
4717                "  FOO(a)\n"
4718                "  return a;\n"
4719                "}",
4720                Style);
4721   verifyFormat("FOO(a)\n"
4722                "FOO(b)",
4723                Style);
4724   verifyFormat("int a = 0;\n"
4725                "FOO(b)\n"
4726                "int c = 0;",
4727                Style);
4728   verifyFormat("int a = 0;\n"
4729                "int x = FOO(a)\n"
4730                "int b = 0;",
4731                Style);
4732   verifyFormat("void foo(int a) { FOO(a) }\n"
4733                "uint32_t bar() {}",
4734                Style);
4735 }
4736 
4737 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
4738   verifyFormat("#define A \\\n"
4739                "  f({     \\\n"
4740                "    g();  \\\n"
4741                "  });",
4742                getLLVMStyleWithColumns(11));
4743 }
4744 
4745 TEST_F(FormatTest, IndentPreprocessorDirectives) {
4746   FormatStyle Style = getLLVMStyle();
4747   Style.IndentPPDirectives = FormatStyle::PPDIS_None;
4748   Style.ColumnLimit = 40;
4749   verifyFormat("#ifdef _WIN32\n"
4750                "#define A 0\n"
4751                "#ifdef VAR2\n"
4752                "#define B 1\n"
4753                "#include <someheader.h>\n"
4754                "#define MACRO                          \\\n"
4755                "  some_very_long_func_aaaaaaaaaa();\n"
4756                "#endif\n"
4757                "#else\n"
4758                "#define A 1\n"
4759                "#endif",
4760                Style);
4761   Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
4762   verifyFormat("#ifdef _WIN32\n"
4763                "#  define A 0\n"
4764                "#  ifdef VAR2\n"
4765                "#    define B 1\n"
4766                "#    include <someheader.h>\n"
4767                "#    define MACRO                      \\\n"
4768                "      some_very_long_func_aaaaaaaaaa();\n"
4769                "#  endif\n"
4770                "#else\n"
4771                "#  define A 1\n"
4772                "#endif",
4773                Style);
4774   verifyFormat("#if A\n"
4775                "#  define MACRO                        \\\n"
4776                "    void a(int x) {                    \\\n"
4777                "      b();                             \\\n"
4778                "      c();                             \\\n"
4779                "      d();                             \\\n"
4780                "      e();                             \\\n"
4781                "      f();                             \\\n"
4782                "    }\n"
4783                "#endif",
4784                Style);
4785   // Comments before include guard.
4786   verifyFormat("// file comment\n"
4787                "// file comment\n"
4788                "#ifndef HEADER_H\n"
4789                "#define HEADER_H\n"
4790                "code();\n"
4791                "#endif",
4792                Style);
4793   // Test with include guards.
4794   verifyFormat("#ifndef HEADER_H\n"
4795                "#define HEADER_H\n"
4796                "code();\n"
4797                "#endif",
4798                Style);
4799   // Include guards must have a #define with the same variable immediately
4800   // after #ifndef.
4801   verifyFormat("#ifndef NOT_GUARD\n"
4802                "#  define FOO\n"
4803                "code();\n"
4804                "#endif",
4805                Style);
4806 
4807   // Include guards must cover the entire file.
4808   verifyFormat("code();\n"
4809                "code();\n"
4810                "#ifndef NOT_GUARD\n"
4811                "#  define NOT_GUARD\n"
4812                "code();\n"
4813                "#endif",
4814                Style);
4815   verifyFormat("#ifndef NOT_GUARD\n"
4816                "#  define NOT_GUARD\n"
4817                "code();\n"
4818                "#endif\n"
4819                "code();",
4820                Style);
4821   // Test with trailing blank lines.
4822   verifyFormat("#ifndef HEADER_H\n"
4823                "#define HEADER_H\n"
4824                "code();\n"
4825                "#endif\n",
4826                Style);
4827   // Include guards don't have #else.
4828   verifyFormat("#ifndef NOT_GUARD\n"
4829                "#  define NOT_GUARD\n"
4830                "code();\n"
4831                "#else\n"
4832                "#endif",
4833                Style);
4834   verifyFormat("#ifndef NOT_GUARD\n"
4835                "#  define NOT_GUARD\n"
4836                "code();\n"
4837                "#elif FOO\n"
4838                "#endif",
4839                Style);
4840   // Non-identifier #define after potential include guard.
4841   verifyFormat("#ifndef FOO\n"
4842                "#  define 1\n"
4843                "#endif\n",
4844                Style);
4845   // #if closes past last non-preprocessor line.
4846   verifyFormat("#ifndef FOO\n"
4847                "#define FOO\n"
4848                "#if 1\n"
4849                "int i;\n"
4850                "#  define A 0\n"
4851                "#endif\n"
4852                "#endif\n",
4853                Style);
4854   // Don't crash if there is an #elif directive without a condition.
4855   verifyFormat("#if 1\n"
4856                "int x;\n"
4857                "#elif\n"
4858                "int y;\n"
4859                "#else\n"
4860                "int z;\n"
4861                "#endif",
4862                Style);
4863   // FIXME: This doesn't handle the case where there's code between the
4864   // #ifndef and #define but all other conditions hold. This is because when
4865   // the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the
4866   // previous code line yet, so we can't detect it.
4867   EXPECT_EQ("#ifndef NOT_GUARD\n"
4868             "code();\n"
4869             "#define NOT_GUARD\n"
4870             "code();\n"
4871             "#endif",
4872             format("#ifndef NOT_GUARD\n"
4873                    "code();\n"
4874                    "#  define NOT_GUARD\n"
4875                    "code();\n"
4876                    "#endif",
4877                    Style));
4878   // FIXME: This doesn't handle cases where legitimate preprocessor lines may
4879   // be outside an include guard. Examples are #pragma once and
4880   // #pragma GCC diagnostic, or anything else that does not change the meaning
4881   // of the file if it's included multiple times.
4882   EXPECT_EQ("#ifdef WIN32\n"
4883             "#  pragma once\n"
4884             "#endif\n"
4885             "#ifndef HEADER_H\n"
4886             "#  define HEADER_H\n"
4887             "code();\n"
4888             "#endif",
4889             format("#ifdef WIN32\n"
4890                    "#  pragma once\n"
4891                    "#endif\n"
4892                    "#ifndef HEADER_H\n"
4893                    "#define HEADER_H\n"
4894                    "code();\n"
4895                    "#endif",
4896                    Style));
4897   // FIXME: This does not detect when there is a single non-preprocessor line
4898   // in front of an include-guard-like structure where other conditions hold
4899   // because ScopedLineState hides the line.
4900   EXPECT_EQ("code();\n"
4901             "#ifndef HEADER_H\n"
4902             "#define HEADER_H\n"
4903             "code();\n"
4904             "#endif",
4905             format("code();\n"
4906                    "#ifndef HEADER_H\n"
4907                    "#  define HEADER_H\n"
4908                    "code();\n"
4909                    "#endif",
4910                    Style));
4911   // Keep comments aligned with #, otherwise indent comments normally. These
4912   // tests cannot use verifyFormat because messUp manipulates leading
4913   // whitespace.
4914   {
4915     const char *Expected = ""
4916                            "void f() {\n"
4917                            "#if 1\n"
4918                            "// Preprocessor aligned.\n"
4919                            "#  define A 0\n"
4920                            "  // Code. Separated by blank line.\n"
4921                            "\n"
4922                            "#  define B 0\n"
4923                            "  // Code. Not aligned with #\n"
4924                            "#  define C 0\n"
4925                            "#endif";
4926     const char *ToFormat = ""
4927                            "void f() {\n"
4928                            "#if 1\n"
4929                            "// Preprocessor aligned.\n"
4930                            "#  define A 0\n"
4931                            "// Code. Separated by blank line.\n"
4932                            "\n"
4933                            "#  define B 0\n"
4934                            "   // Code. Not aligned with #\n"
4935                            "#  define C 0\n"
4936                            "#endif";
4937     EXPECT_EQ(Expected, format(ToFormat, Style));
4938     EXPECT_EQ(Expected, format(Expected, Style));
4939   }
4940   // Keep block quotes aligned.
4941   {
4942     const char *Expected = ""
4943                            "void f() {\n"
4944                            "#if 1\n"
4945                            "/* Preprocessor aligned. */\n"
4946                            "#  define A 0\n"
4947                            "  /* Code. Separated by blank line. */\n"
4948                            "\n"
4949                            "#  define B 0\n"
4950                            "  /* Code. Not aligned with # */\n"
4951                            "#  define C 0\n"
4952                            "#endif";
4953     const char *ToFormat = ""
4954                            "void f() {\n"
4955                            "#if 1\n"
4956                            "/* Preprocessor aligned. */\n"
4957                            "#  define A 0\n"
4958                            "/* Code. Separated by blank line. */\n"
4959                            "\n"
4960                            "#  define B 0\n"
4961                            "   /* Code. Not aligned with # */\n"
4962                            "#  define C 0\n"
4963                            "#endif";
4964     EXPECT_EQ(Expected, format(ToFormat, Style));
4965     EXPECT_EQ(Expected, format(Expected, Style));
4966   }
4967   // Keep comments aligned with un-indented directives.
4968   {
4969     const char *Expected = ""
4970                            "void f() {\n"
4971                            "// Preprocessor aligned.\n"
4972                            "#define A 0\n"
4973                            "  // Code. Separated by blank line.\n"
4974                            "\n"
4975                            "#define B 0\n"
4976                            "  // Code. Not aligned with #\n"
4977                            "#define C 0\n";
4978     const char *ToFormat = ""
4979                            "void f() {\n"
4980                            "// Preprocessor aligned.\n"
4981                            "#define A 0\n"
4982                            "// Code. Separated by blank line.\n"
4983                            "\n"
4984                            "#define B 0\n"
4985                            "   // Code. Not aligned with #\n"
4986                            "#define C 0\n";
4987     EXPECT_EQ(Expected, format(ToFormat, Style));
4988     EXPECT_EQ(Expected, format(Expected, Style));
4989   }
4990   // Test AfterHash with tabs.
4991   {
4992     FormatStyle Tabbed = Style;
4993     Tabbed.UseTab = FormatStyle::UT_Always;
4994     Tabbed.IndentWidth = 8;
4995     Tabbed.TabWidth = 8;
4996     verifyFormat("#ifdef _WIN32\n"
4997                  "#\tdefine A 0\n"
4998                  "#\tifdef VAR2\n"
4999                  "#\t\tdefine B 1\n"
5000                  "#\t\tinclude <someheader.h>\n"
5001                  "#\t\tdefine MACRO          \\\n"
5002                  "\t\t\tsome_very_long_func_aaaaaaaaaa();\n"
5003                  "#\tendif\n"
5004                  "#else\n"
5005                  "#\tdefine A 1\n"
5006                  "#endif",
5007                  Tabbed);
5008   }
5009 
5010   // Regression test: Multiline-macro inside include guards.
5011   verifyFormat("#ifndef HEADER_H\n"
5012                "#define HEADER_H\n"
5013                "#define A()        \\\n"
5014                "  int i;           \\\n"
5015                "  int j;\n"
5016                "#endif // HEADER_H",
5017                getLLVMStyleWithColumns(20));
5018 
5019   Style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash;
5020   // Basic before hash indent tests
5021   verifyFormat("#ifdef _WIN32\n"
5022                "  #define A 0\n"
5023                "  #ifdef VAR2\n"
5024                "    #define B 1\n"
5025                "    #include <someheader.h>\n"
5026                "    #define MACRO                      \\\n"
5027                "      some_very_long_func_aaaaaaaaaa();\n"
5028                "  #endif\n"
5029                "#else\n"
5030                "  #define A 1\n"
5031                "#endif",
5032                Style);
5033   verifyFormat("#if A\n"
5034                "  #define MACRO                        \\\n"
5035                "    void a(int x) {                    \\\n"
5036                "      b();                             \\\n"
5037                "      c();                             \\\n"
5038                "      d();                             \\\n"
5039                "      e();                             \\\n"
5040                "      f();                             \\\n"
5041                "    }\n"
5042                "#endif",
5043                Style);
5044   // Keep comments aligned with indented directives. These
5045   // tests cannot use verifyFormat because messUp manipulates leading
5046   // whitespace.
5047   {
5048     const char *Expected = "void f() {\n"
5049                            "// Aligned to preprocessor.\n"
5050                            "#if 1\n"
5051                            "  // Aligned to code.\n"
5052                            "  int a;\n"
5053                            "  #if 1\n"
5054                            "    // Aligned to preprocessor.\n"
5055                            "    #define A 0\n"
5056                            "  // Aligned to code.\n"
5057                            "  int b;\n"
5058                            "  #endif\n"
5059                            "#endif\n"
5060                            "}";
5061     const char *ToFormat = "void f() {\n"
5062                            "// Aligned to preprocessor.\n"
5063                            "#if 1\n"
5064                            "// Aligned to code.\n"
5065                            "int a;\n"
5066                            "#if 1\n"
5067                            "// Aligned to preprocessor.\n"
5068                            "#define A 0\n"
5069                            "// Aligned to code.\n"
5070                            "int b;\n"
5071                            "#endif\n"
5072                            "#endif\n"
5073                            "}";
5074     EXPECT_EQ(Expected, format(ToFormat, Style));
5075     EXPECT_EQ(Expected, format(Expected, Style));
5076   }
5077   {
5078     const char *Expected = "void f() {\n"
5079                            "/* Aligned to preprocessor. */\n"
5080                            "#if 1\n"
5081                            "  /* Aligned to code. */\n"
5082                            "  int a;\n"
5083                            "  #if 1\n"
5084                            "    /* Aligned to preprocessor. */\n"
5085                            "    #define A 0\n"
5086                            "  /* Aligned to code. */\n"
5087                            "  int b;\n"
5088                            "  #endif\n"
5089                            "#endif\n"
5090                            "}";
5091     const char *ToFormat = "void f() {\n"
5092                            "/* Aligned to preprocessor. */\n"
5093                            "#if 1\n"
5094                            "/* Aligned to code. */\n"
5095                            "int a;\n"
5096                            "#if 1\n"
5097                            "/* Aligned to preprocessor. */\n"
5098                            "#define A 0\n"
5099                            "/* Aligned to code. */\n"
5100                            "int b;\n"
5101                            "#endif\n"
5102                            "#endif\n"
5103                            "}";
5104     EXPECT_EQ(Expected, format(ToFormat, Style));
5105     EXPECT_EQ(Expected, format(Expected, Style));
5106   }
5107 
5108   // Test single comment before preprocessor
5109   verifyFormat("// Comment\n"
5110                "\n"
5111                "#if 1\n"
5112                "#endif",
5113                Style);
5114 }
5115 
5116 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
5117   verifyFormat("{\n  { a #c; }\n}");
5118 }
5119 
5120 TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
5121   EXPECT_EQ("#define A \\\n  {       \\\n    {\nint i;",
5122             format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
5123   EXPECT_EQ("#define A \\\n  }       \\\n  }\nint i;",
5124             format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
5125 }
5126 
5127 TEST_F(FormatTest, EscapedNewlines) {
5128   FormatStyle Narrow = getLLVMStyleWithColumns(11);
5129   EXPECT_EQ("#define A \\\n  int i;  \\\n  int j;",
5130             format("#define A \\\nint i;\\\n  int j;", Narrow));
5131   EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;"));
5132   EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
5133   EXPECT_EQ("/* \\  \\  \\\n */", format("\\\n/* \\  \\  \\\n */"));
5134   EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>"));
5135 
5136   FormatStyle AlignLeft = getLLVMStyle();
5137   AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
5138   EXPECT_EQ("#define MACRO(x) \\\n"
5139             "private:         \\\n"
5140             "  int x(int a);\n",
5141             format("#define MACRO(x) \\\n"
5142                    "private:         \\\n"
5143                    "  int x(int a);\n",
5144                    AlignLeft));
5145 
5146   // CRLF line endings
5147   EXPECT_EQ("#define A \\\r\n  int i;  \\\r\n  int j;",
5148             format("#define A \\\r\nint i;\\\r\n  int j;", Narrow));
5149   EXPECT_EQ("#define A\r\n\r\nint i;", format("#define A \\\r\n\r\n int i;"));
5150   EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
5151   EXPECT_EQ("/* \\  \\  \\\r\n */", format("\\\r\n/* \\  \\  \\\r\n */"));
5152   EXPECT_EQ("<a\r\n\\\\\r\n>", format("<a\r\n\\\\\r\n>"));
5153   EXPECT_EQ("#define MACRO(x) \\\r\n"
5154             "private:         \\\r\n"
5155             "  int x(int a);\r\n",
5156             format("#define MACRO(x) \\\r\n"
5157                    "private:         \\\r\n"
5158                    "  int x(int a);\r\n",
5159                    AlignLeft));
5160 
5161   FormatStyle DontAlign = getLLVMStyle();
5162   DontAlign.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
5163   DontAlign.MaxEmptyLinesToKeep = 3;
5164   // FIXME: can't use verifyFormat here because the newline before
5165   // "public:" is not inserted the first time it's reformatted
5166   EXPECT_EQ("#define A \\\n"
5167             "  class Foo { \\\n"
5168             "    void bar(); \\\n"
5169             "\\\n"
5170             "\\\n"
5171             "\\\n"
5172             "  public: \\\n"
5173             "    void baz(); \\\n"
5174             "  };",
5175             format("#define A \\\n"
5176                    "  class Foo { \\\n"
5177                    "    void bar(); \\\n"
5178                    "\\\n"
5179                    "\\\n"
5180                    "\\\n"
5181                    "  public: \\\n"
5182                    "    void baz(); \\\n"
5183                    "  };",
5184                    DontAlign));
5185 }
5186 
5187 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
5188   verifyFormat("#define A \\\n"
5189                "  int v(  \\\n"
5190                "      a); \\\n"
5191                "  int i;",
5192                getLLVMStyleWithColumns(11));
5193 }
5194 
5195 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
5196   EXPECT_EQ(
5197       "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
5198       "                      \\\n"
5199       "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
5200       "\n"
5201       "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
5202       "    aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
5203       format("  #define   ALooooooooooooooooooooooooooooooooooooooongMacro("
5204              "\\\n"
5205              "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
5206              "  \n"
5207              "   AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
5208              "  aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
5209 }
5210 
5211 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
5212   EXPECT_EQ("int\n"
5213             "#define A\n"
5214             "    a;",
5215             format("int\n#define A\na;"));
5216   verifyFormat("functionCallTo(\n"
5217                "    someOtherFunction(\n"
5218                "        withSomeParameters, whichInSequence,\n"
5219                "        areLongerThanALine(andAnotherCall,\n"
5220                "#define A B\n"
5221                "                           withMoreParamters,\n"
5222                "                           whichStronglyInfluenceTheLayout),\n"
5223                "        andMoreParameters),\n"
5224                "    trailing);",
5225                getLLVMStyleWithColumns(69));
5226   verifyFormat("Foo::Foo()\n"
5227                "#ifdef BAR\n"
5228                "    : baz(0)\n"
5229                "#endif\n"
5230                "{\n"
5231                "}");
5232   verifyFormat("void f() {\n"
5233                "  if (true)\n"
5234                "#ifdef A\n"
5235                "    f(42);\n"
5236                "  x();\n"
5237                "#else\n"
5238                "    g();\n"
5239                "  x();\n"
5240                "#endif\n"
5241                "}");
5242   verifyFormat("void f(param1, param2,\n"
5243                "       param3,\n"
5244                "#ifdef A\n"
5245                "       param4(param5,\n"
5246                "#ifdef A1\n"
5247                "              param6,\n"
5248                "#ifdef A2\n"
5249                "              param7),\n"
5250                "#else\n"
5251                "              param8),\n"
5252                "       param9,\n"
5253                "#endif\n"
5254                "       param10,\n"
5255                "#endif\n"
5256                "       param11)\n"
5257                "#else\n"
5258                "       param12)\n"
5259                "#endif\n"
5260                "{\n"
5261                "  x();\n"
5262                "}",
5263                getLLVMStyleWithColumns(28));
5264   verifyFormat("#if 1\n"
5265                "int i;");
5266   verifyFormat("#if 1\n"
5267                "#endif\n"
5268                "#if 1\n"
5269                "#else\n"
5270                "#endif\n");
5271   verifyFormat("DEBUG({\n"
5272                "  return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5273                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
5274                "});\n"
5275                "#if a\n"
5276                "#else\n"
5277                "#endif");
5278 
5279   verifyIncompleteFormat("void f(\n"
5280                          "#if A\n"
5281                          ");\n"
5282                          "#else\n"
5283                          "#endif");
5284 }
5285 
5286 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
5287   verifyFormat("#endif\n"
5288                "#if B");
5289 }
5290 
5291 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
5292   FormatStyle SingleLine = getLLVMStyle();
5293   SingleLine.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse;
5294   verifyFormat("#if 0\n"
5295                "#elif 1\n"
5296                "#endif\n"
5297                "void foo() {\n"
5298                "  if (test) foo2();\n"
5299                "}",
5300                SingleLine);
5301 }
5302 
5303 TEST_F(FormatTest, LayoutBlockInsideParens) {
5304   verifyFormat("functionCall({ int i; });");
5305   verifyFormat("functionCall({\n"
5306                "  int i;\n"
5307                "  int j;\n"
5308                "});");
5309   verifyFormat("functionCall(\n"
5310                "    {\n"
5311                "      int i;\n"
5312                "      int j;\n"
5313                "    },\n"
5314                "    aaaa, bbbb, cccc);");
5315   verifyFormat("functionA(functionB({\n"
5316                "            int i;\n"
5317                "            int j;\n"
5318                "          }),\n"
5319                "          aaaa, bbbb, cccc);");
5320   verifyFormat("functionCall(\n"
5321                "    {\n"
5322                "      int i;\n"
5323                "      int j;\n"
5324                "    },\n"
5325                "    aaaa, bbbb, // comment\n"
5326                "    cccc);");
5327   verifyFormat("functionA(functionB({\n"
5328                "            int i;\n"
5329                "            int j;\n"
5330                "          }),\n"
5331                "          aaaa, bbbb, // comment\n"
5332                "          cccc);");
5333   verifyFormat("functionCall(aaaa, bbbb, { int i; });");
5334   verifyFormat("functionCall(aaaa, bbbb, {\n"
5335                "  int i;\n"
5336                "  int j;\n"
5337                "});");
5338   verifyFormat(
5339       "Aaa(\n" // FIXME: There shouldn't be a linebreak here.
5340       "    {\n"
5341       "      int i; // break\n"
5342       "    },\n"
5343       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
5344       "                                     ccccccccccccccccc));");
5345   verifyFormat("DEBUG({\n"
5346                "  if (a)\n"
5347                "    f();\n"
5348                "});");
5349 }
5350 
5351 TEST_F(FormatTest, LayoutBlockInsideStatement) {
5352   EXPECT_EQ("SOME_MACRO { int i; }\n"
5353             "int i;",
5354             format("  SOME_MACRO  {int i;}  int i;"));
5355 }
5356 
5357 TEST_F(FormatTest, LayoutNestedBlocks) {
5358   verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
5359                "  struct s {\n"
5360                "    int i;\n"
5361                "  };\n"
5362                "  s kBitsToOs[] = {{10}};\n"
5363                "  for (int i = 0; i < 10; ++i)\n"
5364                "    return;\n"
5365                "}");
5366   verifyFormat("call(parameter, {\n"
5367                "  something();\n"
5368                "  // Comment using all columns.\n"
5369                "  somethingelse();\n"
5370                "});",
5371                getLLVMStyleWithColumns(40));
5372   verifyFormat("DEBUG( //\n"
5373                "    { f(); }, a);");
5374   verifyFormat("DEBUG( //\n"
5375                "    {\n"
5376                "      f(); //\n"
5377                "    },\n"
5378                "    a);");
5379 
5380   EXPECT_EQ("call(parameter, {\n"
5381             "  something();\n"
5382             "  // Comment too\n"
5383             "  // looooooooooong.\n"
5384             "  somethingElse();\n"
5385             "});",
5386             format("call(parameter, {\n"
5387                    "  something();\n"
5388                    "  // Comment too looooooooooong.\n"
5389                    "  somethingElse();\n"
5390                    "});",
5391                    getLLVMStyleWithColumns(29)));
5392   EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int   i; });"));
5393   EXPECT_EQ("DEBUG({ // comment\n"
5394             "  int i;\n"
5395             "});",
5396             format("DEBUG({ // comment\n"
5397                    "int  i;\n"
5398                    "});"));
5399   EXPECT_EQ("DEBUG({\n"
5400             "  int i;\n"
5401             "\n"
5402             "  // comment\n"
5403             "  int j;\n"
5404             "});",
5405             format("DEBUG({\n"
5406                    "  int  i;\n"
5407                    "\n"
5408                    "  // comment\n"
5409                    "  int  j;\n"
5410                    "});"));
5411 
5412   verifyFormat("DEBUG({\n"
5413                "  if (a)\n"
5414                "    return;\n"
5415                "});");
5416   verifyGoogleFormat("DEBUG({\n"
5417                      "  if (a) return;\n"
5418                      "});");
5419   FormatStyle Style = getGoogleStyle();
5420   Style.ColumnLimit = 45;
5421   verifyFormat("Debug(\n"
5422                "    aaaaa,\n"
5423                "    {\n"
5424                "      if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
5425                "    },\n"
5426                "    a);",
5427                Style);
5428 
5429   verifyFormat("SomeFunction({MACRO({ return output; }), b});");
5430 
5431   verifyNoCrash("^{v^{a}}");
5432 }
5433 
5434 TEST_F(FormatTest, FormatNestedBlocksInMacros) {
5435   EXPECT_EQ("#define MACRO()                     \\\n"
5436             "  Debug(aaa, /* force line break */ \\\n"
5437             "        {                           \\\n"
5438             "          int i;                    \\\n"
5439             "          int j;                    \\\n"
5440             "        })",
5441             format("#define   MACRO()   Debug(aaa,  /* force line break */ \\\n"
5442                    "          {  int   i;  int  j;   })",
5443                    getGoogleStyle()));
5444 
5445   EXPECT_EQ("#define A                                       \\\n"
5446             "  [] {                                          \\\n"
5447             "    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(        \\\n"
5448             "        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
5449             "  }",
5450             format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
5451                    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
5452                    getGoogleStyle()));
5453 }
5454 
5455 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
5456   EXPECT_EQ("{}", format("{}"));
5457   verifyFormat("enum E {};");
5458   verifyFormat("enum E {}");
5459   FormatStyle Style = getLLVMStyle();
5460   Style.SpaceInEmptyBlock = true;
5461   EXPECT_EQ("void f() { }", format("void f() {}", Style));
5462   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty;
5463   EXPECT_EQ("while (true) { }", format("while (true) {}", Style));
5464   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
5465   Style.BraceWrapping.BeforeElse = false;
5466   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
5467   verifyFormat("if (a)\n"
5468                "{\n"
5469                "} else if (b)\n"
5470                "{\n"
5471                "} else\n"
5472                "{ }",
5473                Style);
5474   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never;
5475   verifyFormat("if (a) {\n"
5476                "} else if (b) {\n"
5477                "} else {\n"
5478                "}",
5479                Style);
5480   Style.BraceWrapping.BeforeElse = true;
5481   verifyFormat("if (a) { }\n"
5482                "else if (b) { }\n"
5483                "else { }",
5484                Style);
5485 }
5486 
5487 TEST_F(FormatTest, FormatBeginBlockEndMacros) {
5488   FormatStyle Style = getLLVMStyle();
5489   Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$";
5490   Style.MacroBlockEnd = "^[A-Z_]+_END$";
5491   verifyFormat("FOO_BEGIN\n"
5492                "  FOO_ENTRY\n"
5493                "FOO_END",
5494                Style);
5495   verifyFormat("FOO_BEGIN\n"
5496                "  NESTED_FOO_BEGIN\n"
5497                "    NESTED_FOO_ENTRY\n"
5498                "  NESTED_FOO_END\n"
5499                "FOO_END",
5500                Style);
5501   verifyFormat("FOO_BEGIN(Foo, Bar)\n"
5502                "  int x;\n"
5503                "  x = 1;\n"
5504                "FOO_END(Baz)",
5505                Style);
5506 }
5507 
5508 //===----------------------------------------------------------------------===//
5509 // Line break tests.
5510 //===----------------------------------------------------------------------===//
5511 
5512 TEST_F(FormatTest, PreventConfusingIndents) {
5513   verifyFormat(
5514       "void f() {\n"
5515       "  SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
5516       "                         parameter, parameter, parameter)),\n"
5517       "                     SecondLongCall(parameter));\n"
5518       "}");
5519   verifyFormat(
5520       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5521       "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
5522       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
5523       "    aaaaaaaaaaaaaaaaaaaaaaaa);");
5524   verifyFormat(
5525       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5526       "    [aaaaaaaaaaaaaaaaaaaaaaaa\n"
5527       "         [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
5528       "         [aaaaaaaaaaaaaaaaaaaaaaaa]];");
5529   verifyFormat(
5530       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
5531       "    aaaaaaaaaaaaaaaaaaaaaaaa<\n"
5532       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
5533       "    aaaaaaaaaaaaaaaaaaaaaaaa>;");
5534   verifyFormat("int a = bbbb && ccc &&\n"
5535                "        fffff(\n"
5536                "#define A Just forcing a new line\n"
5537                "            ddd);");
5538 }
5539 
5540 TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
5541   verifyFormat(
5542       "bool aaaaaaa =\n"
5543       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
5544       "    bbbbbbbb();");
5545   verifyFormat(
5546       "bool aaaaaaa =\n"
5547       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n"
5548       "    bbbbbbbb();");
5549 
5550   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
5551                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
5552                "    ccccccccc == ddddddddddd;");
5553   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
5554                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n"
5555                "    ccccccccc == ddddddddddd;");
5556   verifyFormat(
5557       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
5558       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n"
5559       "    ccccccccc == ddddddddddd;");
5560 
5561   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
5562                "                 aaaaaa) &&\n"
5563                "         bbbbbb && cccccc;");
5564   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
5565                "                 aaaaaa) >>\n"
5566                "         bbbbbb;");
5567   verifyFormat("aa = Whitespaces.addUntouchableComment(\n"
5568                "    SourceMgr.getSpellingColumnNumber(\n"
5569                "        TheLine.Last->FormatTok.Tok.getLocation()) -\n"
5570                "    1);");
5571 
5572   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
5573                "     bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
5574                "    cccccc) {\n}");
5575   verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
5576                "               bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n"
5577                "              cccccc) {\n}");
5578   verifyFormat("if CONSTEXPR ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
5579                "               bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n"
5580                "              cccccc) {\n}");
5581   verifyFormat("b = a &&\n"
5582                "    // Comment\n"
5583                "    b.c && d;");
5584 
5585   // If the LHS of a comparison is not a binary expression itself, the
5586   // additional linebreak confuses many people.
5587   verifyFormat(
5588       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5589       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
5590       "}");
5591   verifyFormat(
5592       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5593       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
5594       "}");
5595   verifyFormat(
5596       "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
5597       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
5598       "}");
5599   verifyFormat(
5600       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5601       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) <=> 5) {\n"
5602       "}");
5603   // Even explicit parentheses stress the precedence enough to make the
5604   // additional break unnecessary.
5605   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5606                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
5607                "}");
5608   // This cases is borderline, but with the indentation it is still readable.
5609   verifyFormat(
5610       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5611       "        aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5612       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
5613       "}",
5614       getLLVMStyleWithColumns(75));
5615 
5616   // If the LHS is a binary expression, we should still use the additional break
5617   // as otherwise the formatting hides the operator precedence.
5618   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5619                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
5620                "    5) {\n"
5621                "}");
5622   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5623                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa <=>\n"
5624                "    5) {\n"
5625                "}");
5626 
5627   FormatStyle OnePerLine = getLLVMStyle();
5628   OnePerLine.BinPackParameters = false;
5629   verifyFormat(
5630       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
5631       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
5632       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
5633       OnePerLine);
5634 
5635   verifyFormat("int i = someFunction(aaaaaaa, 0)\n"
5636                "                .aaa(aaaaaaaaaaaaa) *\n"
5637                "            aaaaaaa +\n"
5638                "        aaaaaaa;",
5639                getLLVMStyleWithColumns(40));
5640 }
5641 
5642 TEST_F(FormatTest, ExpressionIndentation) {
5643   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5644                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5645                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
5646                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
5647                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
5648                "                     bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
5649                "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
5650                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
5651                "                 ccccccccccccccccccccccccccccccccccccccccc;");
5652   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
5653                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5654                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
5655                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
5656   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5657                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
5658                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
5659                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
5660   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
5661                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
5662                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5663                "        bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
5664   verifyFormat("if () {\n"
5665                "} else if (aaaaa && bbbbb > // break\n"
5666                "                        ccccc) {\n"
5667                "}");
5668   verifyFormat("if () {\n"
5669                "} else if constexpr (aaaaa && bbbbb > // break\n"
5670                "                                  ccccc) {\n"
5671                "}");
5672   verifyFormat("if () {\n"
5673                "} else if CONSTEXPR (aaaaa && bbbbb > // break\n"
5674                "                                  ccccc) {\n"
5675                "}");
5676   verifyFormat("if () {\n"
5677                "} else if (aaaaa &&\n"
5678                "           bbbbb > // break\n"
5679                "               ccccc &&\n"
5680                "           ddddd) {\n"
5681                "}");
5682 
5683   // Presence of a trailing comment used to change indentation of b.
5684   verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
5685                "       b;\n"
5686                "return aaaaaaaaaaaaaaaaaaa +\n"
5687                "       b; //",
5688                getLLVMStyleWithColumns(30));
5689 }
5690 
5691 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
5692   // Not sure what the best system is here. Like this, the LHS can be found
5693   // immediately above an operator (everything with the same or a higher
5694   // indent). The RHS is aligned right of the operator and so compasses
5695   // everything until something with the same indent as the operator is found.
5696   // FIXME: Is this a good system?
5697   FormatStyle Style = getLLVMStyle();
5698   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
5699   verifyFormat(
5700       "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5701       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5702       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5703       "                 == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5704       "                            * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5705       "                        + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5706       "             && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5707       "                        * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5708       "                    > ccccccccccccccccccccccccccccccccccccccccc;",
5709       Style);
5710   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5711                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5712                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5713                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
5714                Style);
5715   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5716                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5717                "              * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5718                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
5719                Style);
5720   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5721                "    == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5722                "               * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5723                "           + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
5724                Style);
5725   verifyFormat("if () {\n"
5726                "} else if (aaaaa\n"
5727                "           && bbbbb // break\n"
5728                "                  > ccccc) {\n"
5729                "}",
5730                Style);
5731   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5732                "       && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
5733                Style);
5734   verifyFormat("return (a)\n"
5735                "       // comment\n"
5736                "       + b;",
5737                Style);
5738   verifyFormat(
5739       "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5740       "                 * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5741       "             + cc;",
5742       Style);
5743 
5744   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5745                "    = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
5746                Style);
5747 
5748   // Forced by comments.
5749   verifyFormat(
5750       "unsigned ContentSize =\n"
5751       "    sizeof(int16_t)   // DWARF ARange version number\n"
5752       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
5753       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
5754       "    + sizeof(int8_t); // Segment Size (in bytes)");
5755 
5756   verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
5757                "       == boost::fusion::at_c<1>(iiii).second;",
5758                Style);
5759 
5760   Style.ColumnLimit = 60;
5761   verifyFormat("zzzzzzzzzz\n"
5762                "    = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5763                "      >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
5764                Style);
5765 
5766   Style.ColumnLimit = 80;
5767   Style.IndentWidth = 4;
5768   Style.TabWidth = 4;
5769   Style.UseTab = FormatStyle::UT_Always;
5770   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
5771   Style.AlignOperands = FormatStyle::OAS_DontAlign;
5772   EXPECT_EQ("return someVeryVeryLongConditionThatBarelyFitsOnALine\n"
5773             "\t&& (someOtherLongishConditionPart1\n"
5774             "\t\t|| someOtherEvenLongerNestedConditionPart2);",
5775             format("return someVeryVeryLongConditionThatBarelyFitsOnALine && "
5776                    "(someOtherLongishConditionPart1 || "
5777                    "someOtherEvenLongerNestedConditionPart2);",
5778                    Style));
5779 }
5780 
5781 TEST_F(FormatTest, ExpressionIndentationStrictAlign) {
5782   FormatStyle Style = getLLVMStyle();
5783   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
5784   Style.AlignOperands = FormatStyle::OAS_AlignAfterOperator;
5785 
5786   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5787                "                   + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5788                "                   + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5789                "              == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5790                "                         * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5791                "                     + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5792                "          && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5793                "                     * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5794                "                 > ccccccccccccccccccccccccccccccccccccccccc;",
5795                Style);
5796   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5797                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5798                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5799                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
5800                Style);
5801   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5802                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5803                "              * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5804                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
5805                Style);
5806   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5807                "    == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5808                "               * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5809                "           + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
5810                Style);
5811   verifyFormat("if () {\n"
5812                "} else if (aaaaa\n"
5813                "           && bbbbb // break\n"
5814                "                  > ccccc) {\n"
5815                "}",
5816                Style);
5817   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5818                "    && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
5819                Style);
5820   verifyFormat("return (a)\n"
5821                "     // comment\n"
5822                "     + b;",
5823                Style);
5824   verifyFormat(
5825       "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5826       "               * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5827       "           + cc;",
5828       Style);
5829   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
5830                "     : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
5831                "                        : 3333333333333333;",
5832                Style);
5833   verifyFormat(
5834       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa    ? bbbbbbbbbbbbbbbbbb\n"
5835       "                           : ccccccccccccccc ? dddddddddddddddddd\n"
5836       "                                             : eeeeeeeeeeeeeeeeee)\n"
5837       "     : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
5838       "                        : 3333333333333333;",
5839       Style);
5840   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5841                "    = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
5842                Style);
5843 
5844   verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
5845                "    == boost::fusion::at_c<1>(iiii).second;",
5846                Style);
5847 
5848   Style.ColumnLimit = 60;
5849   verifyFormat("zzzzzzzzzzzzz\n"
5850                "    = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5851                "   >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
5852                Style);
5853 
5854   // Forced by comments.
5855   Style.ColumnLimit = 80;
5856   verifyFormat(
5857       "unsigned ContentSize\n"
5858       "    = sizeof(int16_t) // DWARF ARange version number\n"
5859       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
5860       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
5861       "    + sizeof(int8_t); // Segment Size (in bytes)",
5862       Style);
5863 
5864   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
5865   verifyFormat(
5866       "unsigned ContentSize =\n"
5867       "    sizeof(int16_t)   // DWARF ARange version number\n"
5868       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
5869       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
5870       "    + sizeof(int8_t); // Segment Size (in bytes)",
5871       Style);
5872 
5873   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
5874   verifyFormat(
5875       "unsigned ContentSize =\n"
5876       "    sizeof(int16_t)   // DWARF ARange version number\n"
5877       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
5878       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
5879       "    + sizeof(int8_t); // Segment Size (in bytes)",
5880       Style);
5881 }
5882 
5883 TEST_F(FormatTest, EnforcedOperatorWraps) {
5884   // Here we'd like to wrap after the || operators, but a comment is forcing an
5885   // earlier wrap.
5886   verifyFormat("bool x = aaaaa //\n"
5887                "         || bbbbb\n"
5888                "         //\n"
5889                "         || cccc;");
5890 }
5891 
5892 TEST_F(FormatTest, NoOperandAlignment) {
5893   FormatStyle Style = getLLVMStyle();
5894   Style.AlignOperands = FormatStyle::OAS_DontAlign;
5895   verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n"
5896                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5897                "                   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
5898                Style);
5899   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
5900   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5901                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5902                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5903                "        == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5904                "                * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5905                "            + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5906                "    && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5907                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5908                "        > ccccccccccccccccccccccccccccccccccccccccc;",
5909                Style);
5910 
5911   verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5912                "        * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5913                "    + cc;",
5914                Style);
5915   verifyFormat("int a = aa\n"
5916                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5917                "        * cccccccccccccccccccccccccccccccccccc;\n",
5918                Style);
5919 
5920   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
5921   verifyFormat("return (a > b\n"
5922                "    // comment1\n"
5923                "    // comment2\n"
5924                "    || c);",
5925                Style);
5926 }
5927 
5928 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) {
5929   FormatStyle Style = getLLVMStyle();
5930   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
5931   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5932                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5933                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
5934                Style);
5935 }
5936 
5937 TEST_F(FormatTest, AllowBinPackingInsideArguments) {
5938   FormatStyle Style = getLLVMStyle();
5939   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
5940   Style.BinPackArguments = false;
5941   Style.ColumnLimit = 40;
5942   verifyFormat("void test() {\n"
5943                "  someFunction(\n"
5944                "      this + argument + is + quite\n"
5945                "      + long + so + it + gets + wrapped\n"
5946                "      + but + remains + bin - packed);\n"
5947                "}",
5948                Style);
5949   verifyFormat("void test() {\n"
5950                "  someFunction(arg1,\n"
5951                "               this + argument + is\n"
5952                "                   + quite + long + so\n"
5953                "                   + it + gets + wrapped\n"
5954                "                   + but + remains + bin\n"
5955                "                   - packed,\n"
5956                "               arg3);\n"
5957                "}",
5958                Style);
5959   verifyFormat("void test() {\n"
5960                "  someFunction(\n"
5961                "      arg1,\n"
5962                "      this + argument + has\n"
5963                "          + anotherFunc(nested,\n"
5964                "                        calls + whose\n"
5965                "                            + arguments\n"
5966                "                            + are + also\n"
5967                "                            + wrapped,\n"
5968                "                        in + addition)\n"
5969                "          + to + being + bin - packed,\n"
5970                "      arg3);\n"
5971                "}",
5972                Style);
5973 
5974   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
5975   verifyFormat("void test() {\n"
5976                "  someFunction(\n"
5977                "      arg1,\n"
5978                "      this + argument + has +\n"
5979                "          anotherFunc(nested,\n"
5980                "                      calls + whose +\n"
5981                "                          arguments +\n"
5982                "                          are + also +\n"
5983                "                          wrapped,\n"
5984                "                      in + addition) +\n"
5985                "          to + being + bin - packed,\n"
5986                "      arg3);\n"
5987                "}",
5988                Style);
5989 }
5990 
5991 TEST_F(FormatTest, ConstructorInitializers) {
5992   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
5993   verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
5994                getLLVMStyleWithColumns(45));
5995   verifyFormat("Constructor()\n"
5996                "    : Inttializer(FitsOnTheLine) {}",
5997                getLLVMStyleWithColumns(44));
5998   verifyFormat("Constructor()\n"
5999                "    : Inttializer(FitsOnTheLine) {}",
6000                getLLVMStyleWithColumns(43));
6001 
6002   verifyFormat("template <typename T>\n"
6003                "Constructor() : Initializer(FitsOnTheLine) {}",
6004                getLLVMStyleWithColumns(45));
6005 
6006   verifyFormat(
6007       "SomeClass::Constructor()\n"
6008       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
6009 
6010   verifyFormat(
6011       "SomeClass::Constructor()\n"
6012       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6013       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
6014   verifyFormat(
6015       "SomeClass::Constructor()\n"
6016       "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6017       "      aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
6018   verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6019                "            aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
6020                "    : aaaaaaaaaa(aaaaaa) {}");
6021 
6022   verifyFormat("Constructor()\n"
6023                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6024                "      aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6025                "                               aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6026                "      aaaaaaaaaaaaaaaaaaaaaaa() {}");
6027 
6028   verifyFormat("Constructor()\n"
6029                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6030                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
6031 
6032   verifyFormat("Constructor(int Parameter = 0)\n"
6033                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
6034                "      aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
6035   verifyFormat("Constructor()\n"
6036                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
6037                "}",
6038                getLLVMStyleWithColumns(60));
6039   verifyFormat("Constructor()\n"
6040                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6041                "          aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}");
6042 
6043   // Here a line could be saved by splitting the second initializer onto two
6044   // lines, but that is not desirable.
6045   verifyFormat("Constructor()\n"
6046                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
6047                "      aaaaaaaaaaa(aaaaaaaaaaa),\n"
6048                "      aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
6049 
6050   FormatStyle OnePerLine = getLLVMStyle();
6051   OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_Never;
6052   verifyFormat("MyClass::MyClass()\n"
6053                "    : a(a),\n"
6054                "      b(b),\n"
6055                "      c(c) {}",
6056                OnePerLine);
6057   verifyFormat("MyClass::MyClass()\n"
6058                "    : a(a), // comment\n"
6059                "      b(b),\n"
6060                "      c(c) {}",
6061                OnePerLine);
6062   verifyFormat("MyClass::MyClass(int a)\n"
6063                "    : b(a),      // comment\n"
6064                "      c(a + 1) { // lined up\n"
6065                "}",
6066                OnePerLine);
6067   verifyFormat("Constructor()\n"
6068                "    : a(b, b, b) {}",
6069                OnePerLine);
6070   OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6071   OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false;
6072   verifyFormat("SomeClass::Constructor()\n"
6073                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6074                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6075                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
6076                OnePerLine);
6077   verifyFormat("SomeClass::Constructor()\n"
6078                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
6079                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6080                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
6081                OnePerLine);
6082   verifyFormat("MyClass::MyClass(int var)\n"
6083                "    : some_var_(var),            // 4 space indent\n"
6084                "      some_other_var_(var + 1) { // lined up\n"
6085                "}",
6086                OnePerLine);
6087   verifyFormat("Constructor()\n"
6088                "    : aaaaa(aaaaaa),\n"
6089                "      aaaaa(aaaaaa),\n"
6090                "      aaaaa(aaaaaa),\n"
6091                "      aaaaa(aaaaaa),\n"
6092                "      aaaaa(aaaaaa) {}",
6093                OnePerLine);
6094   verifyFormat("Constructor()\n"
6095                "    : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
6096                "            aaaaaaaaaaaaaaaaaaaaaa) {}",
6097                OnePerLine);
6098   OnePerLine.BinPackParameters = false;
6099   verifyFormat(
6100       "Constructor()\n"
6101       "    : aaaaaaaaaaaaaaaaaaaaaaaa(\n"
6102       "          aaaaaaaaaaa().aaa(),\n"
6103       "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
6104       OnePerLine);
6105   OnePerLine.ColumnLimit = 60;
6106   verifyFormat("Constructor()\n"
6107                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
6108                "      bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
6109                OnePerLine);
6110 
6111   EXPECT_EQ("Constructor()\n"
6112             "    : // Comment forcing unwanted break.\n"
6113             "      aaaa(aaaa) {}",
6114             format("Constructor() :\n"
6115                    "    // Comment forcing unwanted break.\n"
6116                    "    aaaa(aaaa) {}"));
6117 }
6118 
6119 TEST_F(FormatTest, AllowAllConstructorInitializersOnNextLine) {
6120   FormatStyle Style = getLLVMStyle();
6121   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
6122   Style.ColumnLimit = 60;
6123   Style.BinPackParameters = false;
6124 
6125   for (int i = 0; i < 4; ++i) {
6126     // Test all combinations of parameters that should not have an effect.
6127     Style.AllowAllParametersOfDeclarationOnNextLine = i & 1;
6128     Style.AllowAllArgumentsOnNextLine = i & 2;
6129 
6130     Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6131     Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
6132     verifyFormat("Constructor()\n"
6133                  "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6134                  Style);
6135     verifyFormat("Constructor() : a(a), b(b) {}", Style);
6136 
6137     Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
6138     verifyFormat("Constructor()\n"
6139                  "    : aaaaaaaaaaaaaaaaaaaa(a)\n"
6140                  "    , bbbbbbbbbbbbbbbbbbbbb(b) {}",
6141                  Style);
6142     verifyFormat("Constructor() : a(a), b(b) {}", Style);
6143 
6144     Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
6145     Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6146     verifyFormat("Constructor()\n"
6147                  "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6148                  Style);
6149 
6150     Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
6151     verifyFormat("Constructor()\n"
6152                  "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
6153                  "      bbbbbbbbbbbbbbbbbbbbb(b) {}",
6154                  Style);
6155 
6156     Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
6157     Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6158     verifyFormat("Constructor() :\n"
6159                  "    aaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6160                  Style);
6161 
6162     Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
6163     verifyFormat("Constructor() :\n"
6164                  "    aaaaaaaaaaaaaaaaaa(a),\n"
6165                  "    bbbbbbbbbbbbbbbbbbbbb(b) {}",
6166                  Style);
6167   }
6168 
6169   // Test interactions between AllowAllParametersOfDeclarationOnNextLine and
6170   // AllowAllConstructorInitializersOnNextLine in all
6171   // BreakConstructorInitializers modes
6172   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
6173   Style.AllowAllParametersOfDeclarationOnNextLine = true;
6174   verifyFormat("SomeClassWithALongName::Constructor(\n"
6175                "    int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n"
6176                "    : aaaaaaaaaaaaaaaaaaaa(a)\n"
6177                "    , bbbbbbbbbbbbbbbbbbbbb(b) {}",
6178                Style);
6179 
6180   Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6181   verifyFormat("SomeClassWithALongName::Constructor(\n"
6182                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6183                "    int bbbbbbbbbbbbb,\n"
6184                "    int cccccccccccccccc)\n"
6185                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6186                Style);
6187 
6188   Style.AllowAllParametersOfDeclarationOnNextLine = false;
6189   Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
6190   verifyFormat("SomeClassWithALongName::Constructor(\n"
6191                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6192                "    int bbbbbbbbbbbbb)\n"
6193                "    : aaaaaaaaaaaaaaaaaaaa(a)\n"
6194                "    , bbbbbbbbbbbbbbbbbbbbb(b) {}",
6195                Style);
6196 
6197   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
6198 
6199   Style.AllowAllParametersOfDeclarationOnNextLine = true;
6200   verifyFormat("SomeClassWithALongName::Constructor(\n"
6201                "    int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n"
6202                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
6203                "      bbbbbbbbbbbbbbbbbbbbb(b) {}",
6204                Style);
6205 
6206   Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6207   verifyFormat("SomeClassWithALongName::Constructor(\n"
6208                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6209                "    int bbbbbbbbbbbbb,\n"
6210                "    int cccccccccccccccc)\n"
6211                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6212                Style);
6213 
6214   Style.AllowAllParametersOfDeclarationOnNextLine = false;
6215   Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
6216   verifyFormat("SomeClassWithALongName::Constructor(\n"
6217                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6218                "    int bbbbbbbbbbbbb)\n"
6219                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
6220                "      bbbbbbbbbbbbbbbbbbbbb(b) {}",
6221                Style);
6222 
6223   Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
6224   Style.AllowAllParametersOfDeclarationOnNextLine = true;
6225   verifyFormat("SomeClassWithALongName::Constructor(\n"
6226                "    int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb) :\n"
6227                "    aaaaaaaaaaaaaaaaaaaa(a),\n"
6228                "    bbbbbbbbbbbbbbbbbbbbb(b) {}",
6229                Style);
6230 
6231   Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6232   verifyFormat("SomeClassWithALongName::Constructor(\n"
6233                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6234                "    int bbbbbbbbbbbbb,\n"
6235                "    int cccccccccccccccc) :\n"
6236                "    aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6237                Style);
6238 
6239   Style.AllowAllParametersOfDeclarationOnNextLine = false;
6240   Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
6241   verifyFormat("SomeClassWithALongName::Constructor(\n"
6242                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6243                "    int bbbbbbbbbbbbb) :\n"
6244                "    aaaaaaaaaaaaaaaaaaaa(a),\n"
6245                "    bbbbbbbbbbbbbbbbbbbbb(b) {}",
6246                Style);
6247 }
6248 
6249 TEST_F(FormatTest, AllowAllArgumentsOnNextLine) {
6250   FormatStyle Style = getLLVMStyle();
6251   Style.ColumnLimit = 60;
6252   Style.BinPackArguments = false;
6253   for (int i = 0; i < 4; ++i) {
6254     // Test all combinations of parameters that should not have an effect.
6255     Style.AllowAllParametersOfDeclarationOnNextLine = i & 1;
6256     Style.PackConstructorInitializers =
6257         i & 2 ? FormatStyle::PCIS_BinPack : FormatStyle::PCIS_Never;
6258 
6259     Style.AllowAllArgumentsOnNextLine = true;
6260     verifyFormat("void foo() {\n"
6261                  "  FunctionCallWithReallyLongName(\n"
6262                  "      aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb);\n"
6263                  "}",
6264                  Style);
6265     Style.AllowAllArgumentsOnNextLine = false;
6266     verifyFormat("void foo() {\n"
6267                  "  FunctionCallWithReallyLongName(\n"
6268                  "      aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6269                  "      bbbbbbbbbbbb);\n"
6270                  "}",
6271                  Style);
6272 
6273     Style.AllowAllArgumentsOnNextLine = true;
6274     verifyFormat("void foo() {\n"
6275                  "  auto VariableWithReallyLongName = {\n"
6276                  "      aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb};\n"
6277                  "}",
6278                  Style);
6279     Style.AllowAllArgumentsOnNextLine = false;
6280     verifyFormat("void foo() {\n"
6281                  "  auto VariableWithReallyLongName = {\n"
6282                  "      aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6283                  "      bbbbbbbbbbbb};\n"
6284                  "}",
6285                  Style);
6286   }
6287 
6288   // This parameter should not affect declarations.
6289   Style.BinPackParameters = false;
6290   Style.AllowAllArgumentsOnNextLine = false;
6291   Style.AllowAllParametersOfDeclarationOnNextLine = true;
6292   verifyFormat("void FunctionCallWithReallyLongName(\n"
6293                "    int aaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbb);",
6294                Style);
6295   Style.AllowAllParametersOfDeclarationOnNextLine = false;
6296   verifyFormat("void FunctionCallWithReallyLongName(\n"
6297                "    int aaaaaaaaaaaaaaaaaaaaaaa,\n"
6298                "    int bbbbbbbbbbbb);",
6299                Style);
6300 }
6301 
6302 TEST_F(FormatTest, AllowAllArgumentsOnNextLineDontAlign) {
6303   // Check that AllowAllArgumentsOnNextLine is respected for both BAS_DontAlign
6304   // and BAS_Align.
6305   auto Style = getLLVMStyle();
6306   Style.ColumnLimit = 35;
6307   StringRef Input = "functionCall(paramA, paramB, paramC);\n"
6308                     "void functionDecl(int A, int B, int C);";
6309   Style.AllowAllArgumentsOnNextLine = false;
6310   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
6311   EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n"
6312                       "    paramC);\n"
6313                       "void functionDecl(int A, int B,\n"
6314                       "    int C);"),
6315             format(Input, Style));
6316   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
6317   EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n"
6318                       "             paramC);\n"
6319                       "void functionDecl(int A, int B,\n"
6320                       "                  int C);"),
6321             format(Input, Style));
6322   // However, BAS_AlwaysBreak should take precedence over
6323   // AllowAllArgumentsOnNextLine.
6324   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
6325   EXPECT_EQ(StringRef("functionCall(\n"
6326                       "    paramA, paramB, paramC);\n"
6327                       "void functionDecl(\n"
6328                       "    int A, int B, int C);"),
6329             format(Input, Style));
6330 
6331   // When AllowAllArgumentsOnNextLine is set, we prefer breaking before the
6332   // first argument.
6333   Style.AllowAllArgumentsOnNextLine = true;
6334   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
6335   EXPECT_EQ(StringRef("functionCall(\n"
6336                       "    paramA, paramB, paramC);\n"
6337                       "void functionDecl(\n"
6338                       "    int A, int B, int C);"),
6339             format(Input, Style));
6340   // It wouldn't fit on one line with aligned parameters so this setting
6341   // doesn't change anything for BAS_Align.
6342   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
6343   EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n"
6344                       "             paramC);\n"
6345                       "void functionDecl(int A, int B,\n"
6346                       "                  int C);"),
6347             format(Input, Style));
6348   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
6349   EXPECT_EQ(StringRef("functionCall(\n"
6350                       "    paramA, paramB, paramC);\n"
6351                       "void functionDecl(\n"
6352                       "    int A, int B, int C);"),
6353             format(Input, Style));
6354 }
6355 
6356 TEST_F(FormatTest, BreakConstructorInitializersAfterColon) {
6357   FormatStyle Style = getLLVMStyle();
6358   Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
6359 
6360   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
6361   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}",
6362                getStyleWithColumns(Style, 45));
6363   verifyFormat("Constructor() :\n"
6364                "    Initializer(FitsOnTheLine) {}",
6365                getStyleWithColumns(Style, 44));
6366   verifyFormat("Constructor() :\n"
6367                "    Initializer(FitsOnTheLine) {}",
6368                getStyleWithColumns(Style, 43));
6369 
6370   verifyFormat("template <typename T>\n"
6371                "Constructor() : Initializer(FitsOnTheLine) {}",
6372                getStyleWithColumns(Style, 50));
6373   Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6374   verifyFormat(
6375       "SomeClass::Constructor() :\n"
6376       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
6377       Style);
6378 
6379   Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack;
6380   verifyFormat(
6381       "SomeClass::Constructor() :\n"
6382       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
6383       Style);
6384 
6385   verifyFormat(
6386       "SomeClass::Constructor() :\n"
6387       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6388       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
6389       Style);
6390   verifyFormat(
6391       "SomeClass::Constructor() :\n"
6392       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6393       "    aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
6394       Style);
6395   verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6396                "            aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
6397                "    aaaaaaaaaa(aaaaaa) {}",
6398                Style);
6399 
6400   verifyFormat("Constructor() :\n"
6401                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6402                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6403                "                             aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6404                "    aaaaaaaaaaaaaaaaaaaaaaa() {}",
6405                Style);
6406 
6407   verifyFormat("Constructor() :\n"
6408                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6409                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
6410                Style);
6411 
6412   verifyFormat("Constructor(int Parameter = 0) :\n"
6413                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
6414                "    aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}",
6415                Style);
6416   verifyFormat("Constructor() :\n"
6417                "    aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
6418                "}",
6419                getStyleWithColumns(Style, 60));
6420   verifyFormat("Constructor() :\n"
6421                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6422                "        aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}",
6423                Style);
6424 
6425   // Here a line could be saved by splitting the second initializer onto two
6426   // lines, but that is not desirable.
6427   verifyFormat("Constructor() :\n"
6428                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
6429                "    aaaaaaaaaaa(aaaaaaaaaaa),\n"
6430                "    aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
6431                Style);
6432 
6433   FormatStyle OnePerLine = Style;
6434   OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
6435   verifyFormat("SomeClass::Constructor() :\n"
6436                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6437                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6438                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
6439                OnePerLine);
6440   verifyFormat("SomeClass::Constructor() :\n"
6441                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
6442                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6443                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
6444                OnePerLine);
6445   verifyFormat("MyClass::MyClass(int var) :\n"
6446                "    some_var_(var),            // 4 space indent\n"
6447                "    some_other_var_(var + 1) { // lined up\n"
6448                "}",
6449                OnePerLine);
6450   verifyFormat("Constructor() :\n"
6451                "    aaaaa(aaaaaa),\n"
6452                "    aaaaa(aaaaaa),\n"
6453                "    aaaaa(aaaaaa),\n"
6454                "    aaaaa(aaaaaa),\n"
6455                "    aaaaa(aaaaaa) {}",
6456                OnePerLine);
6457   verifyFormat("Constructor() :\n"
6458                "    aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
6459                "          aaaaaaaaaaaaaaaaaaaaaa) {}",
6460                OnePerLine);
6461   OnePerLine.BinPackParameters = false;
6462   verifyFormat("Constructor() :\n"
6463                "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
6464                "        aaaaaaaaaaa().aaa(),\n"
6465                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
6466                OnePerLine);
6467   OnePerLine.ColumnLimit = 60;
6468   verifyFormat("Constructor() :\n"
6469                "    aaaaaaaaaaaaaaaaaaaa(a),\n"
6470                "    bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
6471                OnePerLine);
6472 
6473   EXPECT_EQ("Constructor() :\n"
6474             "    // Comment forcing unwanted break.\n"
6475             "    aaaa(aaaa) {}",
6476             format("Constructor() :\n"
6477                    "    // Comment forcing unwanted break.\n"
6478                    "    aaaa(aaaa) {}",
6479                    Style));
6480 
6481   Style.ColumnLimit = 0;
6482   verifyFormat("SomeClass::Constructor() :\n"
6483                "    a(a) {}",
6484                Style);
6485   verifyFormat("SomeClass::Constructor() noexcept :\n"
6486                "    a(a) {}",
6487                Style);
6488   verifyFormat("SomeClass::Constructor() :\n"
6489                "    a(a), b(b), c(c) {}",
6490                Style);
6491   verifyFormat("SomeClass::Constructor() :\n"
6492                "    a(a) {\n"
6493                "  foo();\n"
6494                "  bar();\n"
6495                "}",
6496                Style);
6497 
6498   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
6499   verifyFormat("SomeClass::Constructor() :\n"
6500                "    a(a), b(b), c(c) {\n"
6501                "}",
6502                Style);
6503   verifyFormat("SomeClass::Constructor() :\n"
6504                "    a(a) {\n"
6505                "}",
6506                Style);
6507 
6508   Style.ColumnLimit = 80;
6509   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
6510   Style.ConstructorInitializerIndentWidth = 2;
6511   verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}", Style);
6512   verifyFormat("SomeClass::Constructor() :\n"
6513                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6514                "  bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}",
6515                Style);
6516 
6517   // `ConstructorInitializerIndentWidth` actually applies to InheritanceList as
6518   // well
6519   Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
6520   verifyFormat(
6521       "class SomeClass\n"
6522       "  : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6523       "    public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
6524       Style);
6525   Style.BreakInheritanceList = FormatStyle::BILS_BeforeComma;
6526   verifyFormat(
6527       "class SomeClass\n"
6528       "  : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6529       "  , public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
6530       Style);
6531   Style.BreakInheritanceList = FormatStyle::BILS_AfterColon;
6532   verifyFormat(
6533       "class SomeClass :\n"
6534       "  public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6535       "  public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
6536       Style);
6537   Style.BreakInheritanceList = FormatStyle::BILS_AfterComma;
6538   verifyFormat(
6539       "class SomeClass\n"
6540       "  : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6541       "    public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
6542       Style);
6543 }
6544 
6545 #ifndef EXPENSIVE_CHECKS
6546 // Expensive checks enables libstdc++ checking which includes validating the
6547 // state of ranges used in std::priority_queue - this blows out the
6548 // runtime/scalability of the function and makes this test unacceptably slow.
6549 TEST_F(FormatTest, MemoizationTests) {
6550   // This breaks if the memoization lookup does not take \c Indent and
6551   // \c LastSpace into account.
6552   verifyFormat(
6553       "extern CFRunLoopTimerRef\n"
6554       "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
6555       "                     CFTimeInterval interval, CFOptionFlags flags,\n"
6556       "                     CFIndex order, CFRunLoopTimerCallBack callout,\n"
6557       "                     CFRunLoopTimerContext *context) {}");
6558 
6559   // Deep nesting somewhat works around our memoization.
6560   verifyFormat(
6561       "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
6562       "    aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
6563       "        aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
6564       "            aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
6565       "                aaaaa())))))))))))))))))))))))))))))))))))))));",
6566       getLLVMStyleWithColumns(65));
6567   verifyFormat(
6568       "aaaaa(\n"
6569       "    aaaaa,\n"
6570       "    aaaaa(\n"
6571       "        aaaaa,\n"
6572       "        aaaaa(\n"
6573       "            aaaaa,\n"
6574       "            aaaaa(\n"
6575       "                aaaaa,\n"
6576       "                aaaaa(\n"
6577       "                    aaaaa,\n"
6578       "                    aaaaa(\n"
6579       "                        aaaaa,\n"
6580       "                        aaaaa(\n"
6581       "                            aaaaa,\n"
6582       "                            aaaaa(\n"
6583       "                                aaaaa,\n"
6584       "                                aaaaa(\n"
6585       "                                    aaaaa,\n"
6586       "                                    aaaaa(\n"
6587       "                                        aaaaa,\n"
6588       "                                        aaaaa(\n"
6589       "                                            aaaaa,\n"
6590       "                                            aaaaa(\n"
6591       "                                                aaaaa,\n"
6592       "                                                aaaaa))))))))))));",
6593       getLLVMStyleWithColumns(65));
6594   verifyFormat(
6595       "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"
6596       "                                  a),\n"
6597       "                                a),\n"
6598       "                              a),\n"
6599       "                            a),\n"
6600       "                          a),\n"
6601       "                        a),\n"
6602       "                      a),\n"
6603       "                    a),\n"
6604       "                  a),\n"
6605       "                a),\n"
6606       "              a),\n"
6607       "            a),\n"
6608       "          a),\n"
6609       "        a),\n"
6610       "      a),\n"
6611       "    a),\n"
6612       "  a)",
6613       getLLVMStyleWithColumns(65));
6614 
6615   // This test takes VERY long when memoization is broken.
6616   FormatStyle OnePerLine = getLLVMStyle();
6617   OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6618   OnePerLine.BinPackParameters = false;
6619   std::string input = "Constructor()\n"
6620                       "    : aaaa(a,\n";
6621   for (unsigned i = 0, e = 80; i != e; ++i) {
6622     input += "           a,\n";
6623   }
6624   input += "           a) {}";
6625   verifyFormat(input, OnePerLine);
6626 }
6627 #endif
6628 
6629 TEST_F(FormatTest, BreaksAsHighAsPossible) {
6630   verifyFormat(
6631       "void f() {\n"
6632       "  if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
6633       "      (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
6634       "    f();\n"
6635       "}");
6636   verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
6637                "    Intervals[i - 1].getRange().getLast()) {\n}");
6638 }
6639 
6640 TEST_F(FormatTest, BreaksFunctionDeclarations) {
6641   // Principially, we break function declarations in a certain order:
6642   // 1) break amongst arguments.
6643   verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
6644                "                              Cccccccccccccc cccccccccccccc);");
6645   verifyFormat("template <class TemplateIt>\n"
6646                "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
6647                "                            TemplateIt *stop) {}");
6648 
6649   // 2) break after return type.
6650   verifyFormat(
6651       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6652       "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);",
6653       getGoogleStyle());
6654 
6655   // 3) break after (.
6656   verifyFormat(
6657       "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
6658       "    Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);",
6659       getGoogleStyle());
6660 
6661   // 4) break before after nested name specifiers.
6662   verifyFormat(
6663       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6664       "SomeClasssssssssssssssssssssssssssssssssssssss::\n"
6665       "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);",
6666       getGoogleStyle());
6667 
6668   // However, there are exceptions, if a sufficient amount of lines can be
6669   // saved.
6670   // FIXME: The precise cut-offs wrt. the number of saved lines might need some
6671   // more adjusting.
6672   verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
6673                "                                  Cccccccccccccc cccccccccc,\n"
6674                "                                  Cccccccccccccc cccccccccc,\n"
6675                "                                  Cccccccccccccc cccccccccc,\n"
6676                "                                  Cccccccccccccc cccccccccc);");
6677   verifyFormat(
6678       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6679       "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
6680       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
6681       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);",
6682       getGoogleStyle());
6683   verifyFormat(
6684       "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
6685       "                                          Cccccccccccccc cccccccccc,\n"
6686       "                                          Cccccccccccccc cccccccccc,\n"
6687       "                                          Cccccccccccccc cccccccccc,\n"
6688       "                                          Cccccccccccccc cccccccccc,\n"
6689       "                                          Cccccccccccccc cccccccccc,\n"
6690       "                                          Cccccccccccccc cccccccccc);");
6691   verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
6692                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
6693                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
6694                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
6695                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
6696 
6697   // Break after multi-line parameters.
6698   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6699                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6700                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6701                "    bbbb bbbb);");
6702   verifyFormat("void SomeLoooooooooooongFunction(\n"
6703                "    std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
6704                "        aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6705                "    int bbbbbbbbbbbbb);");
6706 
6707   // Treat overloaded operators like other functions.
6708   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
6709                "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
6710   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
6711                "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
6712   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
6713                "operator<<(const SomeLooooooooooooooooooooooooogType &other);");
6714   verifyGoogleFormat(
6715       "SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
6716       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
6717   verifyGoogleFormat(
6718       "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
6719       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
6720   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6721                "    int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
6722   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
6723                "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
6724   verifyGoogleFormat(
6725       "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n"
6726       "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6727       "    bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}");
6728   verifyGoogleFormat("template <typename T>\n"
6729                      "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6730                      "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n"
6731                      "    aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);");
6732 
6733   FormatStyle Style = getLLVMStyle();
6734   Style.PointerAlignment = FormatStyle::PAS_Left;
6735   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6736                "    aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}",
6737                Style);
6738   verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
6739                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
6740                Style);
6741 }
6742 
6743 TEST_F(FormatTest, DontBreakBeforeQualifiedOperator) {
6744   // Regression test for https://bugs.llvm.org/show_bug.cgi?id=40516:
6745   // Prefer keeping `::` followed by `operator` together.
6746   EXPECT_EQ("const aaaa::bbbbbbb &\n"
6747             "ccccccccc::operator++() {\n"
6748             "  stuff();\n"
6749             "}",
6750             format("const aaaa::bbbbbbb\n"
6751                    "&ccccccccc::operator++() { stuff(); }",
6752                    getLLVMStyleWithColumns(40)));
6753 }
6754 
6755 TEST_F(FormatTest, TrailingReturnType) {
6756   verifyFormat("auto foo() -> int;\n");
6757   // correct trailing return type spacing
6758   verifyFormat("auto operator->() -> int;\n");
6759   verifyFormat("auto operator++(int) -> int;\n");
6760 
6761   verifyFormat("struct S {\n"
6762                "  auto bar() const -> int;\n"
6763                "};");
6764   verifyFormat("template <size_t Order, typename T>\n"
6765                "auto load_img(const std::string &filename)\n"
6766                "    -> alias::tensor<Order, T, mem::tag::cpu> {}");
6767   verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n"
6768                "    -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}");
6769   verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}");
6770   verifyFormat("template <typename T>\n"
6771                "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n"
6772                "    -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());");
6773 
6774   // Not trailing return types.
6775   verifyFormat("void f() { auto a = b->c(); }");
6776 }
6777 
6778 TEST_F(FormatTest, DeductionGuides) {
6779   verifyFormat("template <class T> A(const T &, const T &) -> A<T &>;");
6780   verifyFormat("template <class T> explicit A(T &, T &&) -> A<T>;");
6781   verifyFormat("template <class... Ts> S(Ts...) -> S<Ts...>;");
6782   verifyFormat(
6783       "template <class... T>\n"
6784       "array(T &&...t) -> array<std::common_type_t<T...>, sizeof...(T)>;");
6785   verifyFormat("template <class T> A() -> A<decltype(p->foo<3>())>;");
6786   verifyFormat("template <class T> A() -> A<decltype(foo<traits<1>>)>;");
6787   verifyFormat("template <class T> A() -> A<sizeof(p->foo<1>)>;");
6788   verifyFormat("template <class T> A() -> A<(3 < 2)>;");
6789   verifyFormat("template <class T> A() -> A<((3) < (2))>;");
6790   verifyFormat("template <class T> x() -> x<1>;");
6791   verifyFormat("template <class T> explicit x(T &) -> x<1>;");
6792 
6793   // Ensure not deduction guides.
6794   verifyFormat("c()->f<int>();");
6795   verifyFormat("x()->foo<1>;");
6796   verifyFormat("x = p->foo<3>();");
6797   verifyFormat("x()->x<1>();");
6798   verifyFormat("x()->x<1>;");
6799 }
6800 
6801 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
6802   // Avoid breaking before trailing 'const' or other trailing annotations, if
6803   // they are not function-like.
6804   FormatStyle Style = getGoogleStyle();
6805   Style.ColumnLimit = 47;
6806   verifyFormat("void someLongFunction(\n"
6807                "    int someLoooooooooooooongParameter) const {\n}",
6808                getLLVMStyleWithColumns(47));
6809   verifyFormat("LoooooongReturnType\n"
6810                "someLoooooooongFunction() const {}",
6811                getLLVMStyleWithColumns(47));
6812   verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
6813                "    const {}",
6814                Style);
6815   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
6816                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
6817   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
6818                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
6819   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
6820                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
6821   verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
6822                "                   aaaaaaaaaaa aaaaa) const override;");
6823   verifyGoogleFormat(
6824       "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
6825       "    const override;");
6826 
6827   // Even if the first parameter has to be wrapped.
6828   verifyFormat("void someLongFunction(\n"
6829                "    int someLongParameter) const {}",
6830                getLLVMStyleWithColumns(46));
6831   verifyFormat("void someLongFunction(\n"
6832                "    int someLongParameter) const {}",
6833                Style);
6834   verifyFormat("void someLongFunction(\n"
6835                "    int someLongParameter) override {}",
6836                Style);
6837   verifyFormat("void someLongFunction(\n"
6838                "    int someLongParameter) OVERRIDE {}",
6839                Style);
6840   verifyFormat("void someLongFunction(\n"
6841                "    int someLongParameter) final {}",
6842                Style);
6843   verifyFormat("void someLongFunction(\n"
6844                "    int someLongParameter) FINAL {}",
6845                Style);
6846   verifyFormat("void someLongFunction(\n"
6847                "    int parameter) const override {}",
6848                Style);
6849 
6850   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
6851   verifyFormat("void someLongFunction(\n"
6852                "    int someLongParameter) const\n"
6853                "{\n"
6854                "}",
6855                Style);
6856 
6857   Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
6858   verifyFormat("void someLongFunction(\n"
6859                "    int someLongParameter) const\n"
6860                "  {\n"
6861                "  }",
6862                Style);
6863 
6864   // Unless these are unknown annotations.
6865   verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
6866                "                  aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
6867                "    LONG_AND_UGLY_ANNOTATION;");
6868 
6869   // Breaking before function-like trailing annotations is fine to keep them
6870   // close to their arguments.
6871   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
6872                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
6873   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
6874                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
6875   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
6876                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
6877   verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n"
6878                      "    AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);");
6879   verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});");
6880 
6881   verifyFormat(
6882       "void aaaaaaaaaaaaaaaaaa()\n"
6883       "    __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
6884       "                   aaaaaaaaaaaaaaaaaaaaaaaaa));");
6885   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6886                "    __attribute__((unused));");
6887   verifyGoogleFormat(
6888       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6889       "    GUARDED_BY(aaaaaaaaaaaa);");
6890   verifyGoogleFormat(
6891       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6892       "    GUARDED_BY(aaaaaaaaaaaa);");
6893   verifyGoogleFormat(
6894       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
6895       "    aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
6896   verifyGoogleFormat(
6897       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
6898       "    aaaaaaaaaaaaaaaaaaaaaaaaa;");
6899 }
6900 
6901 TEST_F(FormatTest, FunctionAnnotations) {
6902   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
6903                "int OldFunction(const string &parameter) {}");
6904   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
6905                "string OldFunction(const string &parameter) {}");
6906   verifyFormat("template <typename T>\n"
6907                "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
6908                "string OldFunction(const string &parameter) {}");
6909 
6910   // Not function annotations.
6911   verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6912                "                << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
6913   verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n"
6914                "       ThisIsATestWithAReallyReallyReallyReallyLongName) {}");
6915   verifyFormat("MACRO(abc).function() // wrap\n"
6916                "    << abc;");
6917   verifyFormat("MACRO(abc)->function() // wrap\n"
6918                "    << abc;");
6919   verifyFormat("MACRO(abc)::function() // wrap\n"
6920                "    << abc;");
6921 }
6922 
6923 TEST_F(FormatTest, BreaksDesireably) {
6924   verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
6925                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
6926                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
6927   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6928                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
6929                "}");
6930 
6931   verifyFormat(
6932       "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6933       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
6934 
6935   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6936                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6937                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
6938 
6939   verifyFormat(
6940       "aaaaaaaa(aaaaaaaaaaaaa,\n"
6941       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6942       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
6943       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6944       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
6945 
6946   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
6947                "    (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6948 
6949   verifyFormat(
6950       "void f() {\n"
6951       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
6952       "                                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
6953       "}");
6954   verifyFormat(
6955       "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6956       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
6957   verifyFormat(
6958       "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6959       "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
6960   verifyFormat(
6961       "aaaaaa(aaa,\n"
6962       "       new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6963       "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6964       "       aaaa);");
6965   verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
6966                "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6967                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6968 
6969   // Indent consistently independent of call expression and unary operator.
6970   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
6971                "    dddddddddddddddddddddddddddddd));");
6972   verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
6973                "    dddddddddddddddddddddddddddddd));");
6974   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
6975                "    dddddddddddddddddddddddddddddd));");
6976 
6977   // This test case breaks on an incorrect memoization, i.e. an optimization not
6978   // taking into account the StopAt value.
6979   verifyFormat(
6980       "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
6981       "       aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
6982       "       aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
6983       "       (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6984 
6985   verifyFormat("{\n  {\n    {\n"
6986                "      Annotation.SpaceRequiredBefore =\n"
6987                "          Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
6988                "          Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
6989                "    }\n  }\n}");
6990 
6991   // Break on an outer level if there was a break on an inner level.
6992   EXPECT_EQ("f(g(h(a, // comment\n"
6993             "      b, c),\n"
6994             "    d, e),\n"
6995             "  x, y);",
6996             format("f(g(h(a, // comment\n"
6997                    "    b, c), d, e), x, y);"));
6998 
6999   // Prefer breaking similar line breaks.
7000   verifyFormat(
7001       "const int kTrackingOptions = NSTrackingMouseMoved |\n"
7002       "                             NSTrackingMouseEnteredAndExited |\n"
7003       "                             NSTrackingActiveAlways;");
7004 }
7005 
7006 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) {
7007   FormatStyle NoBinPacking = getGoogleStyle();
7008   NoBinPacking.BinPackParameters = false;
7009   NoBinPacking.BinPackArguments = true;
7010   verifyFormat("void f() {\n"
7011                "  f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n"
7012                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
7013                "}",
7014                NoBinPacking);
7015   verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n"
7016                "       int aaaaaaaaaaaaaaaaaaaa,\n"
7017                "       int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
7018                NoBinPacking);
7019 
7020   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
7021   verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7022                "                        vector<int> bbbbbbbbbbbbbbb);",
7023                NoBinPacking);
7024   // FIXME: This behavior difference is probably not wanted. However, currently
7025   // we cannot distinguish BreakBeforeParameter being set because of the wrapped
7026   // template arguments from BreakBeforeParameter being set because of the
7027   // one-per-line formatting.
7028   verifyFormat(
7029       "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n"
7030       "                                             aaaaaaaaaa> aaaaaaaaaa);",
7031       NoBinPacking);
7032   verifyFormat(
7033       "void fffffffffff(\n"
7034       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n"
7035       "        aaaaaaaaaa);");
7036 }
7037 
7038 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
7039   FormatStyle NoBinPacking = getGoogleStyle();
7040   NoBinPacking.BinPackParameters = false;
7041   NoBinPacking.BinPackArguments = false;
7042   verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
7043                "  aaaaaaaaaaaaaaaaaaaa,\n"
7044                "  aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
7045                NoBinPacking);
7046   verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
7047                "        aaaaaaaaaaaaa,\n"
7048                "        aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
7049                NoBinPacking);
7050   verifyFormat(
7051       "aaaaaaaa(aaaaaaaaaaaaa,\n"
7052       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7053       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
7054       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7055       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
7056       NoBinPacking);
7057   verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
7058                "    .aaaaaaaaaaaaaaaaaa();",
7059                NoBinPacking);
7060   verifyFormat("void f() {\n"
7061                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7062                "      aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n"
7063                "}",
7064                NoBinPacking);
7065 
7066   verifyFormat(
7067       "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7068       "             aaaaaaaaaaaa,\n"
7069       "             aaaaaaaaaaaa);",
7070       NoBinPacking);
7071   verifyFormat(
7072       "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
7073       "                               ddddddddddddddddddddddddddddd),\n"
7074       "             test);",
7075       NoBinPacking);
7076 
7077   verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
7078                "            aaaaaaaaaaaaaaaaaaaaaaa,\n"
7079                "            aaaaaaaaaaaaaaaaaaaaaaa>\n"
7080                "    aaaaaaaaaaaaaaaaaa;",
7081                NoBinPacking);
7082   verifyFormat("a(\"a\"\n"
7083                "  \"a\",\n"
7084                "  a);");
7085 
7086   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
7087   verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
7088                "                aaaaaaaaa,\n"
7089                "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7090                NoBinPacking);
7091   verifyFormat(
7092       "void f() {\n"
7093       "  aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
7094       "      .aaaaaaa();\n"
7095       "}",
7096       NoBinPacking);
7097   verifyFormat(
7098       "template <class SomeType, class SomeOtherType>\n"
7099       "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
7100       NoBinPacking);
7101 }
7102 
7103 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
7104   FormatStyle Style = getLLVMStyleWithColumns(15);
7105   Style.ExperimentalAutoDetectBinPacking = true;
7106   EXPECT_EQ("aaa(aaaa,\n"
7107             "    aaaa,\n"
7108             "    aaaa);\n"
7109             "aaa(aaaa,\n"
7110             "    aaaa,\n"
7111             "    aaaa);",
7112             format("aaa(aaaa,\n" // one-per-line
7113                    "  aaaa,\n"
7114                    "    aaaa  );\n"
7115                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
7116                    Style));
7117   EXPECT_EQ("aaa(aaaa, aaaa,\n"
7118             "    aaaa);\n"
7119             "aaa(aaaa, aaaa,\n"
7120             "    aaaa);",
7121             format("aaa(aaaa,  aaaa,\n" // bin-packed
7122                    "    aaaa  );\n"
7123                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
7124                    Style));
7125 }
7126 
7127 TEST_F(FormatTest, FormatsBuilderPattern) {
7128   verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n"
7129                "    .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
7130                "    .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
7131                "    .StartsWith(\".init\", ORDER_INIT)\n"
7132                "    .StartsWith(\".fini\", ORDER_FINI)\n"
7133                "    .StartsWith(\".hash\", ORDER_HASH)\n"
7134                "    .Default(ORDER_TEXT);\n");
7135 
7136   verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
7137                "       aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
7138   verifyFormat("aaaaaaa->aaaaaaa\n"
7139                "    ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7140                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7141                "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
7142   verifyFormat(
7143       "aaaaaaa->aaaaaaa\n"
7144       "    ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7145       "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
7146   verifyFormat(
7147       "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
7148       "    aaaaaaaaaaaaaa);");
7149   verifyFormat(
7150       "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
7151       "    aaaaaa->aaaaaaaaaaaa()\n"
7152       "        ->aaaaaaaaaaaaaaaa(\n"
7153       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7154       "        ->aaaaaaaaaaaaaaaaa();");
7155   verifyGoogleFormat(
7156       "void f() {\n"
7157       "  someo->Add((new util::filetools::Handler(dir))\n"
7158       "                 ->OnEvent1(NewPermanentCallback(\n"
7159       "                     this, &HandlerHolderClass::EventHandlerCBA))\n"
7160       "                 ->OnEvent2(NewPermanentCallback(\n"
7161       "                     this, &HandlerHolderClass::EventHandlerCBB))\n"
7162       "                 ->OnEvent3(NewPermanentCallback(\n"
7163       "                     this, &HandlerHolderClass::EventHandlerCBC))\n"
7164       "                 ->OnEvent5(NewPermanentCallback(\n"
7165       "                     this, &HandlerHolderClass::EventHandlerCBD))\n"
7166       "                 ->OnEvent6(NewPermanentCallback(\n"
7167       "                     this, &HandlerHolderClass::EventHandlerCBE)));\n"
7168       "}");
7169 
7170   verifyFormat(
7171       "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
7172   verifyFormat("aaaaaaaaaaaaaaa()\n"
7173                "    .aaaaaaaaaaaaaaa()\n"
7174                "    .aaaaaaaaaaaaaaa()\n"
7175                "    .aaaaaaaaaaaaaaa()\n"
7176                "    .aaaaaaaaaaaaaaa();");
7177   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
7178                "    .aaaaaaaaaaaaaaa()\n"
7179                "    .aaaaaaaaaaaaaaa()\n"
7180                "    .aaaaaaaaaaaaaaa();");
7181   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
7182                "    .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
7183                "    .aaaaaaaaaaaaaaa();");
7184   verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
7185                "    ->aaaaaaaaaaaaaae(0)\n"
7186                "    ->aaaaaaaaaaaaaaa();");
7187 
7188   // Don't linewrap after very short segments.
7189   verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7190                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7191                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
7192   verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7193                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7194                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
7195   verifyFormat("aaa()\n"
7196                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7197                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7198                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
7199 
7200   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
7201                "    .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7202                "    .has<bbbbbbbbbbbbbbbbbbbbb>();");
7203   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
7204                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
7205                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
7206 
7207   // Prefer not to break after empty parentheses.
7208   verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
7209                "    First->LastNewlineOffset);");
7210 
7211   // Prefer not to create "hanging" indents.
7212   verifyFormat(
7213       "return !soooooooooooooome_map\n"
7214       "            .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7215       "            .second;");
7216   verifyFormat(
7217       "return aaaaaaaaaaaaaaaa\n"
7218       "    .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n"
7219       "    .aaaa(aaaaaaaaaaaaaa);");
7220   // No hanging indent here.
7221   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n"
7222                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7223   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n"
7224                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7225   verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
7226                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7227                getLLVMStyleWithColumns(60));
7228   verifyFormat("aaaaaaaaaaaaaaaaaa\n"
7229                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
7230                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7231                getLLVMStyleWithColumns(59));
7232   verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7233                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7234                "    .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7235 
7236   // Dont break if only closing statements before member call
7237   verifyFormat("test() {\n"
7238                "  ([]() -> {\n"
7239                "    int b = 32;\n"
7240                "    return 3;\n"
7241                "  }).foo();\n"
7242                "}");
7243   verifyFormat("test() {\n"
7244                "  (\n"
7245                "      []() -> {\n"
7246                "        int b = 32;\n"
7247                "        return 3;\n"
7248                "      },\n"
7249                "      foo, bar)\n"
7250                "      .foo();\n"
7251                "}");
7252   verifyFormat("test() {\n"
7253                "  ([]() -> {\n"
7254                "    int b = 32;\n"
7255                "    return 3;\n"
7256                "  })\n"
7257                "      .foo()\n"
7258                "      .bar();\n"
7259                "}");
7260   verifyFormat("test() {\n"
7261                "  ([]() -> {\n"
7262                "    int b = 32;\n"
7263                "    return 3;\n"
7264                "  })\n"
7265                "      .foo(\"aaaaaaaaaaaaaaaaa\"\n"
7266                "           \"bbbb\");\n"
7267                "}",
7268                getLLVMStyleWithColumns(30));
7269 }
7270 
7271 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
7272   verifyFormat(
7273       "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
7274       "    bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
7275   verifyFormat(
7276       "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n"
7277       "    bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}");
7278 
7279   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
7280                "    ccccccccccccccccccccccccc) {\n}");
7281   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n"
7282                "    ccccccccccccccccccccccccc) {\n}");
7283 
7284   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
7285                "    ccccccccccccccccccccccccc) {\n}");
7286   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n"
7287                "    ccccccccccccccccccccccccc) {\n}");
7288 
7289   verifyFormat(
7290       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
7291       "    ccccccccccccccccccccccccc) {\n}");
7292   verifyFormat(
7293       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n"
7294       "    ccccccccccccccccccccccccc) {\n}");
7295 
7296   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
7297                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
7298                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
7299                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
7300   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n"
7301                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n"
7302                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n"
7303                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
7304 
7305   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
7306                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
7307                "    aaaaaaaaaaaaaaa != aa) {\n}");
7308   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n"
7309                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n"
7310                "    aaaaaaaaaaaaaaa != aa) {\n}");
7311 }
7312 
7313 TEST_F(FormatTest, BreaksAfterAssignments) {
7314   verifyFormat(
7315       "unsigned Cost =\n"
7316       "    TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
7317       "                        SI->getPointerAddressSpaceee());\n");
7318   verifyFormat(
7319       "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
7320       "    Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
7321 
7322   verifyFormat(
7323       "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
7324       "    aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
7325   verifyFormat("unsigned OriginalStartColumn =\n"
7326                "    SourceMgr.getSpellingColumnNumber(\n"
7327                "        Current.FormatTok.getStartOfNonWhitespace()) -\n"
7328                "    1;");
7329 }
7330 
7331 TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) {
7332   FormatStyle Style = getLLVMStyle();
7333   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
7334                "    bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;",
7335                Style);
7336 
7337   Style.PenaltyBreakAssignment = 20;
7338   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
7339                "                                 cccccccccccccccccccccccccc;",
7340                Style);
7341 }
7342 
7343 TEST_F(FormatTest, AlignsAfterAssignments) {
7344   verifyFormat(
7345       "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7346       "             aaaaaaaaaaaaaaaaaaaaaaaaa;");
7347   verifyFormat(
7348       "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7349       "          aaaaaaaaaaaaaaaaaaaaaaaaa;");
7350   verifyFormat(
7351       "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7352       "           aaaaaaaaaaaaaaaaaaaaaaaaa;");
7353   verifyFormat(
7354       "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7355       "              aaaaaaaaaaaaaaaaaaaaaaaaa);");
7356   verifyFormat(
7357       "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
7358       "                                            aaaaaaaaaaaaaaaaaaaaaaaa +\n"
7359       "                                            aaaaaaaaaaaaaaaaaaaaaaaa;");
7360 }
7361 
7362 TEST_F(FormatTest, AlignsAfterReturn) {
7363   verifyFormat(
7364       "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7365       "       aaaaaaaaaaaaaaaaaaaaaaaaa;");
7366   verifyFormat(
7367       "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7368       "        aaaaaaaaaaaaaaaaaaaaaaaaa);");
7369   verifyFormat(
7370       "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
7371       "       aaaaaaaaaaaaaaaaaaaaaa();");
7372   verifyFormat(
7373       "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
7374       "        aaaaaaaaaaaaaaaaaaaaaa());");
7375   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7376                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7377   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7378                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
7379                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
7380   verifyFormat("return\n"
7381                "    // true if code is one of a or b.\n"
7382                "    code == a || code == b;");
7383 }
7384 
7385 TEST_F(FormatTest, AlignsAfterOpenBracket) {
7386   verifyFormat(
7387       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
7388       "                                                aaaaaaaaa aaaaaaa) {}");
7389   verifyFormat(
7390       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
7391       "                                               aaaaaaaaaaa aaaaaaaaa);");
7392   verifyFormat(
7393       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
7394       "                                             aaaaaaaaaaaaaaaaaaaaa));");
7395   FormatStyle Style = getLLVMStyle();
7396   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
7397   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7398                "    aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}",
7399                Style);
7400   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
7401                "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);",
7402                Style);
7403   verifyFormat("SomeLongVariableName->someFunction(\n"
7404                "    foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));",
7405                Style);
7406   verifyFormat(
7407       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
7408       "    aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
7409       Style);
7410   verifyFormat(
7411       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
7412       "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7413       Style);
7414   verifyFormat(
7415       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
7416       "    aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
7417       Style);
7418 
7419   verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n"
7420                "    ccccccc(aaaaaaaaaaaaaaaaa,         //\n"
7421                "        b));",
7422                Style);
7423 
7424   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
7425   Style.BinPackArguments = false;
7426   Style.BinPackParameters = false;
7427   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7428                "    aaaaaaaaaaa aaaaaaaa,\n"
7429                "    aaaaaaaaa aaaaaaa,\n"
7430                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
7431                Style);
7432   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
7433                "    aaaaaaaaaaa aaaaaaaaa,\n"
7434                "    aaaaaaaaaaa aaaaaaaaa,\n"
7435                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7436                Style);
7437   verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n"
7438                "    aaaaaaaaaaaaaaa,\n"
7439                "    aaaaaaaaaaaaaaaaaaaaa,\n"
7440                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
7441                Style);
7442   verifyFormat(
7443       "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n"
7444       "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
7445       Style);
7446   verifyFormat(
7447       "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n"
7448       "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
7449       Style);
7450   verifyFormat(
7451       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
7452       "    aaaaaaaaaaaaaaaaaaaaa(\n"
7453       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n"
7454       "    aaaaaaaaaaaaaaaa);",
7455       Style);
7456   verifyFormat(
7457       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
7458       "    aaaaaaaaaaaaaaaaaaaaa(\n"
7459       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n"
7460       "    aaaaaaaaaaaaaaaa);",
7461       Style);
7462 }
7463 
7464 TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
7465   FormatStyle Style = getLLVMStyleWithColumns(40);
7466   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
7467                "          bbbbbbbbbbbbbbbbbbbbbb);",
7468                Style);
7469   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
7470   Style.AlignOperands = FormatStyle::OAS_DontAlign;
7471   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
7472                "          bbbbbbbbbbbbbbbbbbbbbb);",
7473                Style);
7474   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
7475   Style.AlignOperands = FormatStyle::OAS_Align;
7476   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
7477                "          bbbbbbbbbbbbbbbbbbbbbb);",
7478                Style);
7479   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
7480   Style.AlignOperands = FormatStyle::OAS_DontAlign;
7481   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
7482                "    bbbbbbbbbbbbbbbbbbbbbb);",
7483                Style);
7484 }
7485 
7486 TEST_F(FormatTest, BreaksConditionalExpressions) {
7487   verifyFormat(
7488       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7489       "                               ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7490       "                               : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7491   verifyFormat(
7492       "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
7493       "     aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7494       "                                : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7495   verifyFormat(
7496       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7497       "                                   : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7498   verifyFormat("aaaa(aaaaaaaaa, aaaaaaaaa,\n"
7499                "     aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7500                "             : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7501   verifyFormat(
7502       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
7503       "                                                    : aaaaaaaaaaaaa);");
7504   verifyFormat(
7505       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7506       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7507       "                                    : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7508       "                   aaaaaaaaaaaaa);");
7509   verifyFormat(
7510       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7511       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7512       "                   aaaaaaaaaaaaa);");
7513   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7514                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7515                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7516                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7517                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7518   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7519                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7520                "           ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7521                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7522                "           : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7523                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7524                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7525   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7526                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7527                "           ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7528                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7529                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7530   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7531                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7532                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
7533   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
7534                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7535                "        ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7536                "        : aaaaaaaaaaaaaaaa;");
7537   verifyFormat(
7538       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7539       "    ? aaaaaaaaaaaaaaa\n"
7540       "    : aaaaaaaaaaaaaaa;");
7541   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
7542                "          aaaaaaaaa\n"
7543                "      ? b\n"
7544                "      : c);");
7545   verifyFormat("return aaaa == bbbb\n"
7546                "           // comment\n"
7547                "           ? aaaa\n"
7548                "           : bbbb;");
7549   verifyFormat("unsigned Indent =\n"
7550                "    format(TheLine.First,\n"
7551                "           IndentForLevel[TheLine.Level] >= 0\n"
7552                "               ? IndentForLevel[TheLine.Level]\n"
7553                "               : TheLine * 2,\n"
7554                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
7555                getLLVMStyleWithColumns(60));
7556   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
7557                "                  ? aaaaaaaaaaaaaaa\n"
7558                "                  : bbbbbbbbbbbbbbb //\n"
7559                "                        ? ccccccccccccccc\n"
7560                "                        : ddddddddddddddd;");
7561   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
7562                "                  ? aaaaaaaaaaaaaaa\n"
7563                "                  : (bbbbbbbbbbbbbbb //\n"
7564                "                         ? ccccccccccccccc\n"
7565                "                         : ddddddddddddddd);");
7566   verifyFormat(
7567       "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7568       "                                      ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7569       "                                            aaaaaaaaaaaaaaaaaaaaa +\n"
7570       "                                            aaaaaaaaaaaaaaaaaaaaa\n"
7571       "                                      : aaaaaaaaaa;");
7572   verifyFormat(
7573       "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7574       "                                   : aaaaaaaaaaaaaaaaaaaaaa\n"
7575       "                      : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
7576 
7577   FormatStyle NoBinPacking = getLLVMStyle();
7578   NoBinPacking.BinPackArguments = false;
7579   verifyFormat(
7580       "void f() {\n"
7581       "  g(aaa,\n"
7582       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
7583       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7584       "        ? aaaaaaaaaaaaaaa\n"
7585       "        : aaaaaaaaaaaaaaa);\n"
7586       "}",
7587       NoBinPacking);
7588   verifyFormat(
7589       "void f() {\n"
7590       "  g(aaa,\n"
7591       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
7592       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7593       "        ?: aaaaaaaaaaaaaaa);\n"
7594       "}",
7595       NoBinPacking);
7596 
7597   verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n"
7598                "             // comment.\n"
7599                "             ccccccccccccccccccccccccccccccccccccccc\n"
7600                "                 ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7601                "                 : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);");
7602 
7603   // Assignments in conditional expressions. Apparently not uncommon :-(.
7604   verifyFormat("return a != b\n"
7605                "           // comment\n"
7606                "           ? a = b\n"
7607                "           : a = b;");
7608   verifyFormat("return a != b\n"
7609                "           // comment\n"
7610                "           ? a = a != b\n"
7611                "                     // comment\n"
7612                "                     ? a = b\n"
7613                "                     : a\n"
7614                "           : a;\n");
7615   verifyFormat("return a != b\n"
7616                "           // comment\n"
7617                "           ? a\n"
7618                "           : a = a != b\n"
7619                "                     // comment\n"
7620                "                     ? a = b\n"
7621                "                     : a;");
7622 
7623   // Chained conditionals
7624   FormatStyle Style = getLLVMStyle();
7625   Style.ColumnLimit = 70;
7626   Style.AlignOperands = FormatStyle::OAS_Align;
7627   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7628                "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7629                "                        : 3333333333333333;",
7630                Style);
7631   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7632                "       : bbbbbbbbbb     ? 2222222222222222\n"
7633                "                        : 3333333333333333;",
7634                Style);
7635   verifyFormat("return aaaaaaaaaa         ? 1111111111111111\n"
7636                "       : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
7637                "                          : 3333333333333333;",
7638                Style);
7639   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7640                "       : bbbbbbbbbbbbbb ? 222222\n"
7641                "                        : 333333;",
7642                Style);
7643   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7644                "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7645                "       : cccccccccccccc ? 3333333333333333\n"
7646                "                        : 4444444444444444;",
7647                Style);
7648   verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc)\n"
7649                "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7650                "                        : 3333333333333333;",
7651                Style);
7652   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7653                "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7654                "                        : (aaa ? bbb : ccc);",
7655                Style);
7656   verifyFormat(
7657       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7658       "                                             : cccccccccccccccccc)\n"
7659       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7660       "                        : 3333333333333333;",
7661       Style);
7662   verifyFormat(
7663       "return aaaaaaaaa        ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7664       "                                             : cccccccccccccccccc)\n"
7665       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7666       "                        : 3333333333333333;",
7667       Style);
7668   verifyFormat(
7669       "return aaaaaaaaa        ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7670       "                                             : dddddddddddddddddd)\n"
7671       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7672       "                        : 3333333333333333;",
7673       Style);
7674   verifyFormat(
7675       "return aaaaaaaaa        ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7676       "                                             : dddddddddddddddddd)\n"
7677       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7678       "                        : 3333333333333333;",
7679       Style);
7680   verifyFormat(
7681       "return aaaaaaaaa        ? 1111111111111111\n"
7682       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7683       "                        : a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7684       "                                             : dddddddddddddddddd)\n",
7685       Style);
7686   verifyFormat(
7687       "return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7688       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7689       "                        : (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7690       "                                             : cccccccccccccccccc);",
7691       Style);
7692   verifyFormat(
7693       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7694       "                           : ccccccccccccccc ? dddddddddddddddddd\n"
7695       "                                             : eeeeeeeeeeeeeeeeee)\n"
7696       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7697       "                        : 3333333333333333;",
7698       Style);
7699   verifyFormat(
7700       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa    ? bbbbbbbbbbbbbbbbbb\n"
7701       "                           : ccccccccccccccc ? dddddddddddddddddd\n"
7702       "                                             : eeeeeeeeeeeeeeeeee)\n"
7703       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7704       "                        : 3333333333333333;",
7705       Style);
7706   verifyFormat(
7707       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7708       "                           : cccccccccccc    ? dddddddddddddddddd\n"
7709       "                                             : eeeeeeeeeeeeeeeeee)\n"
7710       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7711       "                        : 3333333333333333;",
7712       Style);
7713   verifyFormat(
7714       "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7715       "                                             : cccccccccccccccccc\n"
7716       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7717       "                        : 3333333333333333;",
7718       Style);
7719   verifyFormat(
7720       "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7721       "                          : cccccccccccccccc ? dddddddddddddddddd\n"
7722       "                                             : eeeeeeeeeeeeeeeeee\n"
7723       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7724       "                        : 3333333333333333;",
7725       Style);
7726   verifyFormat("return aaaaaaaaaaaaaaaaaaaaa\n"
7727                "           ? (aaaaaaaaaaaaaaaaaa   ? bbbbbbbbbbbbbbbbbb\n"
7728                "              : cccccccccccccccccc ? dddddddddddddddddd\n"
7729                "                                   : eeeeeeeeeeeeeeeeee)\n"
7730                "       : bbbbbbbbbbbbbbbbbbb ? 2222222222222222\n"
7731                "                             : 3333333333333333;",
7732                Style);
7733   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaa\n"
7734                "           ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7735                "             : cccccccccccccccc ? dddddddddddddddddd\n"
7736                "                                : eeeeeeeeeeeeeeeeee\n"
7737                "       : bbbbbbbbbbbbbbbbbbbbbbb ? 2222222222222222\n"
7738                "                                 : 3333333333333333;",
7739                Style);
7740 
7741   Style.AlignOperands = FormatStyle::OAS_DontAlign;
7742   Style.BreakBeforeTernaryOperators = false;
7743   // FIXME: Aligning the question marks is weird given DontAlign.
7744   // Consider disabling this alignment in this case. Also check whether this
7745   // will render the adjustment from https://reviews.llvm.org/D82199
7746   // unnecessary.
7747   verifyFormat("int x = aaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa :\n"
7748                "    bbbb                ? cccccccccccccccccc :\n"
7749                "                          ddddd;\n",
7750                Style);
7751 
7752   EXPECT_EQ(
7753       "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n"
7754       "    /*\n"
7755       "     */\n"
7756       "    function() {\n"
7757       "      try {\n"
7758       "        return JJJJJJJJJJJJJJ(\n"
7759       "            pppppppppppppppppppppppppppppppppppppppppppppppppp);\n"
7760       "      }\n"
7761       "    } :\n"
7762       "    function() {};",
7763       format(
7764           "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n"
7765           "     /*\n"
7766           "      */\n"
7767           "     function() {\n"
7768           "      try {\n"
7769           "        return JJJJJJJJJJJJJJ(\n"
7770           "            pppppppppppppppppppppppppppppppppppppppppppppppppp);\n"
7771           "      }\n"
7772           "    } :\n"
7773           "    function() {};",
7774           getGoogleStyle(FormatStyle::LK_JavaScript)));
7775 }
7776 
7777 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
7778   FormatStyle Style = getLLVMStyle();
7779   Style.BreakBeforeTernaryOperators = false;
7780   Style.ColumnLimit = 70;
7781   verifyFormat(
7782       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
7783       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
7784       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7785       Style);
7786   verifyFormat(
7787       "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
7788       "     aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
7789       "                                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7790       Style);
7791   verifyFormat(
7792       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
7793       "                                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7794       Style);
7795   verifyFormat("aaaa(aaaaaaaa, aaaaaaaaaa,\n"
7796                "     aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
7797                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7798                Style);
7799   verifyFormat(
7800       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
7801       "                                                      aaaaaaaaaaaaa);",
7802       Style);
7803   verifyFormat(
7804       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7805       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
7806       "                                      aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7807       "                   aaaaaaaaaaaaa);",
7808       Style);
7809   verifyFormat(
7810       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7811       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7812       "                   aaaaaaaaaaaaa);",
7813       Style);
7814   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
7815                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7816                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
7817                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7818                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7819                Style);
7820   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7821                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
7822                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7823                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
7824                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7825                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7826                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7827                Style);
7828   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7829                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n"
7830                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7831                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7832                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7833                Style);
7834   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
7835                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
7836                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
7837                Style);
7838   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
7839                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
7840                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
7841                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
7842                Style);
7843   verifyFormat(
7844       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
7845       "    aaaaaaaaaaaaaaa :\n"
7846       "    aaaaaaaaaaaaaaa;",
7847       Style);
7848   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
7849                "          aaaaaaaaa ?\n"
7850                "      b :\n"
7851                "      c);",
7852                Style);
7853   verifyFormat("unsigned Indent =\n"
7854                "    format(TheLine.First,\n"
7855                "           IndentForLevel[TheLine.Level] >= 0 ?\n"
7856                "               IndentForLevel[TheLine.Level] :\n"
7857                "               TheLine * 2,\n"
7858                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
7859                Style);
7860   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
7861                "                  aaaaaaaaaaaaaaa :\n"
7862                "                  bbbbbbbbbbbbbbb ? //\n"
7863                "                      ccccccccccccccc :\n"
7864                "                      ddddddddddddddd;",
7865                Style);
7866   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
7867                "                  aaaaaaaaaaaaaaa :\n"
7868                "                  (bbbbbbbbbbbbbbb ? //\n"
7869                "                       ccccccccccccccc :\n"
7870                "                       ddddddddddddddd);",
7871                Style);
7872   verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
7873                "            /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n"
7874                "            ccccccccccccccccccccccccccc;",
7875                Style);
7876   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
7877                "           aaaaa :\n"
7878                "           bbbbbbbbbbbbbbb + cccccccccccccccc;",
7879                Style);
7880 
7881   // Chained conditionals
7882   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
7883                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7884                "                          3333333333333333;",
7885                Style);
7886   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
7887                "       bbbbbbbbbb       ? 2222222222222222 :\n"
7888                "                          3333333333333333;",
7889                Style);
7890   verifyFormat("return aaaaaaaaaa       ? 1111111111111111 :\n"
7891                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7892                "                          3333333333333333;",
7893                Style);
7894   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
7895                "       bbbbbbbbbbbbbbbb ? 222222 :\n"
7896                "                          333333;",
7897                Style);
7898   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
7899                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7900                "       cccccccccccccccc ? 3333333333333333 :\n"
7901                "                          4444444444444444;",
7902                Style);
7903   verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc) :\n"
7904                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7905                "                          3333333333333333;",
7906                Style);
7907   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
7908                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7909                "                          (aaa ? bbb : ccc);",
7910                Style);
7911   verifyFormat(
7912       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7913       "                                               cccccccccccccccccc) :\n"
7914       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7915       "                          3333333333333333;",
7916       Style);
7917   verifyFormat(
7918       "return aaaaaaaaa        ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7919       "                                               cccccccccccccccccc) :\n"
7920       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7921       "                          3333333333333333;",
7922       Style);
7923   verifyFormat(
7924       "return aaaaaaaaa        ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7925       "                                               dddddddddddddddddd) :\n"
7926       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7927       "                          3333333333333333;",
7928       Style);
7929   verifyFormat(
7930       "return aaaaaaaaa        ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7931       "                                               dddddddddddddddddd) :\n"
7932       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7933       "                          3333333333333333;",
7934       Style);
7935   verifyFormat(
7936       "return aaaaaaaaa        ? 1111111111111111 :\n"
7937       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7938       "                          a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7939       "                                               dddddddddddddddddd)\n",
7940       Style);
7941   verifyFormat(
7942       "return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
7943       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7944       "                          (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7945       "                                               cccccccccccccccccc);",
7946       Style);
7947   verifyFormat(
7948       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7949       "                           ccccccccccccccccc ? dddddddddddddddddd :\n"
7950       "                                               eeeeeeeeeeeeeeeeee) :\n"
7951       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7952       "                          3333333333333333;",
7953       Style);
7954   verifyFormat(
7955       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7956       "                           ccccccccccccc     ? dddddddddddddddddd :\n"
7957       "                                               eeeeeeeeeeeeeeeeee) :\n"
7958       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7959       "                          3333333333333333;",
7960       Style);
7961   verifyFormat(
7962       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaa     ? bbbbbbbbbbbbbbbbbb :\n"
7963       "                           ccccccccccccccccc ? dddddddddddddddddd :\n"
7964       "                                               eeeeeeeeeeeeeeeeee) :\n"
7965       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7966       "                          3333333333333333;",
7967       Style);
7968   verifyFormat(
7969       "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7970       "                                               cccccccccccccccccc :\n"
7971       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7972       "                          3333333333333333;",
7973       Style);
7974   verifyFormat(
7975       "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7976       "                          cccccccccccccccccc ? dddddddddddddddddd :\n"
7977       "                                               eeeeeeeeeeeeeeeeee :\n"
7978       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7979       "                          3333333333333333;",
7980       Style);
7981   verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n"
7982                "           (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7983                "            cccccccccccccccccc ? dddddddddddddddddd :\n"
7984                "                                 eeeeeeeeeeeeeeeeee) :\n"
7985                "       bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7986                "                               3333333333333333;",
7987                Style);
7988   verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n"
7989                "           aaaaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7990                "           cccccccccccccccccccc ? dddddddddddddddddd :\n"
7991                "                                  eeeeeeeeeeeeeeeeee :\n"
7992                "       bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7993                "                               3333333333333333;",
7994                Style);
7995 }
7996 
7997 TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
7998   verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
7999                "     aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
8000   verifyFormat("bool a = true, b = false;");
8001 
8002   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
8003                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
8004                "     bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
8005                "         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
8006   verifyFormat(
8007       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
8008       "         bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
8009       "     d = e && f;");
8010   verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
8011                "          c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
8012   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
8013                "          *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
8014   verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
8015                "          ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
8016 
8017   FormatStyle Style = getGoogleStyle();
8018   Style.PointerAlignment = FormatStyle::PAS_Left;
8019   Style.DerivePointerAlignment = false;
8020   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8021                "    *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
8022                "    *b = bbbbbbbbbbbbbbbbbbb;",
8023                Style);
8024   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
8025                "          *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;",
8026                Style);
8027   verifyFormat("vector<int*> a, b;", Style);
8028   verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style);
8029 }
8030 
8031 TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
8032   verifyFormat("arr[foo ? bar : baz];");
8033   verifyFormat("f()[foo ? bar : baz];");
8034   verifyFormat("(a + b)[foo ? bar : baz];");
8035   verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
8036 }
8037 
8038 TEST_F(FormatTest, AlignsStringLiterals) {
8039   verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
8040                "                                      \"short literal\");");
8041   verifyFormat(
8042       "looooooooooooooooooooooooongFunction(\n"
8043       "    \"short literal\"\n"
8044       "    \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
8045   verifyFormat("someFunction(\"Always break between multi-line\"\n"
8046                "             \" string literals\",\n"
8047                "             and, other, parameters);");
8048   EXPECT_EQ("fun + \"1243\" /* comment */\n"
8049             "      \"5678\";",
8050             format("fun + \"1243\" /* comment */\n"
8051                    "    \"5678\";",
8052                    getLLVMStyleWithColumns(28)));
8053   EXPECT_EQ(
8054       "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
8055       "         \"aaaaaaaaaaaaaaaaaaaaa\"\n"
8056       "         \"aaaaaaaaaaaaaaaa\";",
8057       format("aaaaaa ="
8058              "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
8059              "aaaaaaaaaaaaaaaaaaaaa\" "
8060              "\"aaaaaaaaaaaaaaaa\";"));
8061   verifyFormat("a = a + \"a\"\n"
8062                "        \"a\"\n"
8063                "        \"a\";");
8064   verifyFormat("f(\"a\", \"b\"\n"
8065                "       \"c\");");
8066 
8067   verifyFormat(
8068       "#define LL_FORMAT \"ll\"\n"
8069       "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
8070       "       \"d, ddddddddd: %\" LL_FORMAT \"d\");");
8071 
8072   verifyFormat("#define A(X)          \\\n"
8073                "  \"aaaaa\" #X \"bbbbbb\" \\\n"
8074                "  \"ccccc\"",
8075                getLLVMStyleWithColumns(23));
8076   verifyFormat("#define A \"def\"\n"
8077                "f(\"abc\" A \"ghi\"\n"
8078                "  \"jkl\");");
8079 
8080   verifyFormat("f(L\"a\"\n"
8081                "  L\"b\");");
8082   verifyFormat("#define A(X)            \\\n"
8083                "  L\"aaaaa\" #X L\"bbbbbb\" \\\n"
8084                "  L\"ccccc\"",
8085                getLLVMStyleWithColumns(25));
8086 
8087   verifyFormat("f(@\"a\"\n"
8088                "  @\"b\");");
8089   verifyFormat("NSString s = @\"a\"\n"
8090                "             @\"b\"\n"
8091                "             @\"c\";");
8092   verifyFormat("NSString s = @\"a\"\n"
8093                "              \"b\"\n"
8094                "              \"c\";");
8095 }
8096 
8097 TEST_F(FormatTest, ReturnTypeBreakingStyle) {
8098   FormatStyle Style = getLLVMStyle();
8099   // No declarations or definitions should be moved to own line.
8100   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None;
8101   verifyFormat("class A {\n"
8102                "  int f() { return 1; }\n"
8103                "  int g();\n"
8104                "};\n"
8105                "int f() { return 1; }\n"
8106                "int g();\n",
8107                Style);
8108 
8109   // All declarations and definitions should have the return type moved to its
8110   // own line.
8111   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
8112   Style.TypenameMacros = {"LIST"};
8113   verifyFormat("SomeType\n"
8114                "funcdecl(LIST(uint64_t));",
8115                Style);
8116   verifyFormat("class E {\n"
8117                "  int\n"
8118                "  f() {\n"
8119                "    return 1;\n"
8120                "  }\n"
8121                "  int\n"
8122                "  g();\n"
8123                "};\n"
8124                "int\n"
8125                "f() {\n"
8126                "  return 1;\n"
8127                "}\n"
8128                "int\n"
8129                "g();\n",
8130                Style);
8131 
8132   // Top-level definitions, and no kinds of declarations should have the
8133   // return type moved to its own line.
8134   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions;
8135   verifyFormat("class B {\n"
8136                "  int f() { return 1; }\n"
8137                "  int g();\n"
8138                "};\n"
8139                "int\n"
8140                "f() {\n"
8141                "  return 1;\n"
8142                "}\n"
8143                "int g();\n",
8144                Style);
8145 
8146   // Top-level definitions and declarations should have the return type moved
8147   // to its own line.
8148   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel;
8149   verifyFormat("class C {\n"
8150                "  int f() { return 1; }\n"
8151                "  int g();\n"
8152                "};\n"
8153                "int\n"
8154                "f() {\n"
8155                "  return 1;\n"
8156                "}\n"
8157                "int\n"
8158                "g();\n",
8159                Style);
8160 
8161   // All definitions should have the return type moved to its own line, but no
8162   // kinds of declarations.
8163   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
8164   verifyFormat("class D {\n"
8165                "  int\n"
8166                "  f() {\n"
8167                "    return 1;\n"
8168                "  }\n"
8169                "  int g();\n"
8170                "};\n"
8171                "int\n"
8172                "f() {\n"
8173                "  return 1;\n"
8174                "}\n"
8175                "int g();\n",
8176                Style);
8177   verifyFormat("const char *\n"
8178                "f(void) {\n" // Break here.
8179                "  return \"\";\n"
8180                "}\n"
8181                "const char *bar(void);\n", // No break here.
8182                Style);
8183   verifyFormat("template <class T>\n"
8184                "T *\n"
8185                "f(T &c) {\n" // Break here.
8186                "  return NULL;\n"
8187                "}\n"
8188                "template <class T> T *f(T &c);\n", // No break here.
8189                Style);
8190   verifyFormat("class C {\n"
8191                "  int\n"
8192                "  operator+() {\n"
8193                "    return 1;\n"
8194                "  }\n"
8195                "  int\n"
8196                "  operator()() {\n"
8197                "    return 1;\n"
8198                "  }\n"
8199                "};\n",
8200                Style);
8201   verifyFormat("void\n"
8202                "A::operator()() {}\n"
8203                "void\n"
8204                "A::operator>>() {}\n"
8205                "void\n"
8206                "A::operator+() {}\n"
8207                "void\n"
8208                "A::operator*() {}\n"
8209                "void\n"
8210                "A::operator->() {}\n"
8211                "void\n"
8212                "A::operator void *() {}\n"
8213                "void\n"
8214                "A::operator void &() {}\n"
8215                "void\n"
8216                "A::operator void &&() {}\n"
8217                "void\n"
8218                "A::operator char *() {}\n"
8219                "void\n"
8220                "A::operator[]() {}\n"
8221                "void\n"
8222                "A::operator!() {}\n"
8223                "void\n"
8224                "A::operator**() {}\n"
8225                "void\n"
8226                "A::operator<Foo> *() {}\n"
8227                "void\n"
8228                "A::operator<Foo> **() {}\n"
8229                "void\n"
8230                "A::operator<Foo> &() {}\n"
8231                "void\n"
8232                "A::operator void **() {}\n",
8233                Style);
8234   verifyFormat("constexpr auto\n"
8235                "operator()() const -> reference {}\n"
8236                "constexpr auto\n"
8237                "operator>>() const -> reference {}\n"
8238                "constexpr auto\n"
8239                "operator+() const -> reference {}\n"
8240                "constexpr auto\n"
8241                "operator*() const -> reference {}\n"
8242                "constexpr auto\n"
8243                "operator->() const -> reference {}\n"
8244                "constexpr auto\n"
8245                "operator++() const -> reference {}\n"
8246                "constexpr auto\n"
8247                "operator void *() const -> reference {}\n"
8248                "constexpr auto\n"
8249                "operator void **() const -> reference {}\n"
8250                "constexpr auto\n"
8251                "operator void *() const -> reference {}\n"
8252                "constexpr auto\n"
8253                "operator void &() const -> reference {}\n"
8254                "constexpr auto\n"
8255                "operator void &&() const -> reference {}\n"
8256                "constexpr auto\n"
8257                "operator char *() const -> reference {}\n"
8258                "constexpr auto\n"
8259                "operator!() const -> reference {}\n"
8260                "constexpr auto\n"
8261                "operator[]() const -> reference {}\n",
8262                Style);
8263   verifyFormat("void *operator new(std::size_t s);", // No break here.
8264                Style);
8265   verifyFormat("void *\n"
8266                "operator new(std::size_t s) {}",
8267                Style);
8268   verifyFormat("void *\n"
8269                "operator delete[](void *ptr) {}",
8270                Style);
8271   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
8272   verifyFormat("const char *\n"
8273                "f(void)\n" // Break here.
8274                "{\n"
8275                "  return \"\";\n"
8276                "}\n"
8277                "const char *bar(void);\n", // No break here.
8278                Style);
8279   verifyFormat("template <class T>\n"
8280                "T *\n"     // Problem here: no line break
8281                "f(T &c)\n" // Break here.
8282                "{\n"
8283                "  return NULL;\n"
8284                "}\n"
8285                "template <class T> T *f(T &c);\n", // No break here.
8286                Style);
8287   verifyFormat("int\n"
8288                "foo(A<bool> a)\n"
8289                "{\n"
8290                "  return a;\n"
8291                "}\n",
8292                Style);
8293   verifyFormat("int\n"
8294                "foo(A<8> a)\n"
8295                "{\n"
8296                "  return a;\n"
8297                "}\n",
8298                Style);
8299   verifyFormat("int\n"
8300                "foo(A<B<bool>, 8> a)\n"
8301                "{\n"
8302                "  return a;\n"
8303                "}\n",
8304                Style);
8305   verifyFormat("int\n"
8306                "foo(A<B<8>, bool> a)\n"
8307                "{\n"
8308                "  return a;\n"
8309                "}\n",
8310                Style);
8311   verifyFormat("int\n"
8312                "foo(A<B<bool>, bool> a)\n"
8313                "{\n"
8314                "  return a;\n"
8315                "}\n",
8316                Style);
8317   verifyFormat("int\n"
8318                "foo(A<B<8>, 8> a)\n"
8319                "{\n"
8320                "  return a;\n"
8321                "}\n",
8322                Style);
8323 
8324   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
8325   Style.BraceWrapping.AfterFunction = true;
8326   verifyFormat("int f(i);\n" // No break here.
8327                "int\n"       // Break here.
8328                "f(i)\n"
8329                "{\n"
8330                "  return i + 1;\n"
8331                "}\n"
8332                "int\n" // Break here.
8333                "f(i)\n"
8334                "{\n"
8335                "  return i + 1;\n"
8336                "};",
8337                Style);
8338   verifyFormat("int f(a, b, c);\n" // No break here.
8339                "int\n"             // Break here.
8340                "f(a, b, c)\n"      // Break here.
8341                "short a, b;\n"
8342                "float c;\n"
8343                "{\n"
8344                "  return a + b < c;\n"
8345                "}\n"
8346                "int\n"        // Break here.
8347                "f(a, b, c)\n" // Break here.
8348                "short a, b;\n"
8349                "float c;\n"
8350                "{\n"
8351                "  return a + b < c;\n"
8352                "};",
8353                Style);
8354   verifyFormat("byte *\n" // Break here.
8355                "f(a)\n"   // Break here.
8356                "byte a[];\n"
8357                "{\n"
8358                "  return a;\n"
8359                "}",
8360                Style);
8361   verifyFormat("bool f(int a, int) override;\n"
8362                "Bar g(int a, Bar) final;\n"
8363                "Bar h(a, Bar) final;",
8364                Style);
8365   verifyFormat("int\n"
8366                "f(a)",
8367                Style);
8368   verifyFormat("bool\n"
8369                "f(size_t = 0, bool b = false)\n"
8370                "{\n"
8371                "  return !b;\n"
8372                "}",
8373                Style);
8374 
8375   // The return breaking style doesn't affect:
8376   // * function and object definitions with attribute-like macros
8377   verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n"
8378                "    ABSL_GUARDED_BY(mutex) = {};",
8379                getGoogleStyleWithColumns(40));
8380   verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n"
8381                "    ABSL_GUARDED_BY(mutex);  // comment",
8382                getGoogleStyleWithColumns(40));
8383   verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n"
8384                "    ABSL_GUARDED_BY(mutex1)\n"
8385                "        ABSL_GUARDED_BY(mutex2);",
8386                getGoogleStyleWithColumns(40));
8387   verifyFormat("Tttttt f(int a, int b)\n"
8388                "    ABSL_GUARDED_BY(mutex1)\n"
8389                "        ABSL_GUARDED_BY(mutex2);",
8390                getGoogleStyleWithColumns(40));
8391   // * typedefs
8392   verifyFormat("typedef ATTR(X) char x;", getGoogleStyle());
8393 
8394   Style = getGNUStyle();
8395 
8396   // Test for comments at the end of function declarations.
8397   verifyFormat("void\n"
8398                "foo (int a, /*abc*/ int b) // def\n"
8399                "{\n"
8400                "}\n",
8401                Style);
8402 
8403   verifyFormat("void\n"
8404                "foo (int a, /* abc */ int b) /* def */\n"
8405                "{\n"
8406                "}\n",
8407                Style);
8408 
8409   // Definitions that should not break after return type
8410   verifyFormat("void foo (int a, int b); // def\n", Style);
8411   verifyFormat("void foo (int a, int b); /* def */\n", Style);
8412   verifyFormat("void foo (int a, int b);\n", Style);
8413 }
8414 
8415 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
8416   FormatStyle NoBreak = getLLVMStyle();
8417   NoBreak.AlwaysBreakBeforeMultilineStrings = false;
8418   FormatStyle Break = getLLVMStyle();
8419   Break.AlwaysBreakBeforeMultilineStrings = true;
8420   verifyFormat("aaaa = \"bbbb\"\n"
8421                "       \"cccc\";",
8422                NoBreak);
8423   verifyFormat("aaaa =\n"
8424                "    \"bbbb\"\n"
8425                "    \"cccc\";",
8426                Break);
8427   verifyFormat("aaaa(\"bbbb\"\n"
8428                "     \"cccc\");",
8429                NoBreak);
8430   verifyFormat("aaaa(\n"
8431                "    \"bbbb\"\n"
8432                "    \"cccc\");",
8433                Break);
8434   verifyFormat("aaaa(qqq, \"bbbb\"\n"
8435                "          \"cccc\");",
8436                NoBreak);
8437   verifyFormat("aaaa(qqq,\n"
8438                "     \"bbbb\"\n"
8439                "     \"cccc\");",
8440                Break);
8441   verifyFormat("aaaa(qqq,\n"
8442                "     L\"bbbb\"\n"
8443                "     L\"cccc\");",
8444                Break);
8445   verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n"
8446                "                      \"bbbb\"));",
8447                Break);
8448   verifyFormat("string s = someFunction(\n"
8449                "    \"abc\"\n"
8450                "    \"abc\");",
8451                Break);
8452 
8453   // As we break before unary operators, breaking right after them is bad.
8454   verifyFormat("string foo = abc ? \"x\"\n"
8455                "                   \"blah blah blah blah blah blah\"\n"
8456                "                 : \"y\";",
8457                Break);
8458 
8459   // Don't break if there is no column gain.
8460   verifyFormat("f(\"aaaa\"\n"
8461                "  \"bbbb\");",
8462                Break);
8463 
8464   // Treat literals with escaped newlines like multi-line string literals.
8465   EXPECT_EQ("x = \"a\\\n"
8466             "b\\\n"
8467             "c\";",
8468             format("x = \"a\\\n"
8469                    "b\\\n"
8470                    "c\";",
8471                    NoBreak));
8472   EXPECT_EQ("xxxx =\n"
8473             "    \"a\\\n"
8474             "b\\\n"
8475             "c\";",
8476             format("xxxx = \"a\\\n"
8477                    "b\\\n"
8478                    "c\";",
8479                    Break));
8480 
8481   EXPECT_EQ("NSString *const kString =\n"
8482             "    @\"aaaa\"\n"
8483             "    @\"bbbb\";",
8484             format("NSString *const kString = @\"aaaa\"\n"
8485                    "@\"bbbb\";",
8486                    Break));
8487 
8488   Break.ColumnLimit = 0;
8489   verifyFormat("const char *hello = \"hello llvm\";", Break);
8490 }
8491 
8492 TEST_F(FormatTest, AlignsPipes) {
8493   verifyFormat(
8494       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8495       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8496       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8497   verifyFormat(
8498       "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
8499       "                     << aaaaaaaaaaaaaaaaaaaa;");
8500   verifyFormat(
8501       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8502       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8503   verifyFormat(
8504       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
8505       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8506   verifyFormat(
8507       "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
8508       "                \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
8509       "             << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
8510   verifyFormat(
8511       "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8512       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8513       "         << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8514   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8515                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8516                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8517                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
8518   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n"
8519                "             << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);");
8520   verifyFormat(
8521       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8522       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8523   verifyFormat(
8524       "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n"
8525       "                                       aaaaaaaaaaaaaaaaaaaaaaaaaa);");
8526 
8527   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
8528                "             << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
8529   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8530                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8531                "                    aaaaaaaaaaaaaaaaaaaaa)\n"
8532                "             << aaaaaaaaaaaaaaaaaaaaaaaaaa;");
8533   verifyFormat("LOG_IF(aaa == //\n"
8534                "       bbb)\n"
8535                "    << a << b;");
8536 
8537   // But sometimes, breaking before the first "<<" is desirable.
8538   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
8539                "    << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
8540   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
8541                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8542                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8543   verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
8544                "    << BEF << IsTemplate << Description << E->getType();");
8545   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
8546                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8547                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8548   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
8549                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8550                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8551                "    << aaa;");
8552 
8553   verifyFormat(
8554       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8555       "                    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
8556 
8557   // Incomplete string literal.
8558   EXPECT_EQ("llvm::errs() << \"\n"
8559             "             << a;",
8560             format("llvm::errs() << \"\n<<a;"));
8561 
8562   verifyFormat("void f() {\n"
8563                "  CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
8564                "      << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
8565                "}");
8566 
8567   // Handle 'endl'.
8568   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n"
8569                "             << bbbbbbbbbbbbbbbbbbbbbb << endl;");
8570   verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;");
8571 
8572   // Handle '\n'.
8573   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n"
8574                "             << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
8575   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n"
8576                "             << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';");
8577   verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n"
8578                "             << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";");
8579   verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
8580 }
8581 
8582 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) {
8583   verifyFormat("return out << \"somepacket = {\\n\"\n"
8584                "           << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
8585                "           << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
8586                "           << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
8587                "           << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
8588                "           << \"}\";");
8589 
8590   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
8591                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
8592                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
8593   verifyFormat(
8594       "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
8595       "             << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
8596       "             << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
8597       "             << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
8598       "             << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
8599   verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
8600                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
8601   verifyFormat(
8602       "void f() {\n"
8603       "  llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
8604       "               << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
8605       "}");
8606 
8607   // Breaking before the first "<<" is generally not desirable.
8608   verifyFormat(
8609       "llvm::errs()\n"
8610       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8611       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8612       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8613       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
8614       getLLVMStyleWithColumns(70));
8615   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
8616                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8617                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
8618                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8619                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
8620                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
8621                getLLVMStyleWithColumns(70));
8622 
8623   verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
8624                "           \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
8625                "           \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;");
8626   verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
8627                "                  \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
8628                "                  \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);");
8629   verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n"
8630                "           (aaaa + aaaa);",
8631                getLLVMStyleWithColumns(40));
8632   verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n"
8633                "                  (aaaaaaa + aaaaa));",
8634                getLLVMStyleWithColumns(40));
8635   verifyFormat(
8636       "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n"
8637       "                  SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n"
8638       "                  bbbbbbbbbbbbbbbbbbbbbbb);");
8639 }
8640 
8641 TEST_F(FormatTest, UnderstandsEquals) {
8642   verifyFormat(
8643       "aaaaaaaaaaaaaaaaa =\n"
8644       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8645   verifyFormat(
8646       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
8647       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
8648   verifyFormat(
8649       "if (a) {\n"
8650       "  f();\n"
8651       "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
8652       "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
8653       "}");
8654 
8655   verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
8656                "        100000000 + 10000000) {\n}");
8657 }
8658 
8659 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
8660   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
8661                "    .looooooooooooooooooooooooooooooooooooooongFunction();");
8662 
8663   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
8664                "    ->looooooooooooooooooooooooooooooooooooooongFunction();");
8665 
8666   verifyFormat(
8667       "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
8668       "                                                          Parameter2);");
8669 
8670   verifyFormat(
8671       "ShortObject->shortFunction(\n"
8672       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
8673       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
8674 
8675   verifyFormat("loooooooooooooongFunction(\n"
8676                "    LoooooooooooooongObject->looooooooooooooooongFunction());");
8677 
8678   verifyFormat(
8679       "function(LoooooooooooooooooooooooooooooooooooongObject\n"
8680       "             ->loooooooooooooooooooooooooooooooooooooooongFunction());");
8681 
8682   verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
8683                "    .WillRepeatedly(Return(SomeValue));");
8684   verifyFormat("void f() {\n"
8685                "  EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
8686                "      .Times(2)\n"
8687                "      .WillRepeatedly(Return(SomeValue));\n"
8688                "}");
8689   verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
8690                "    ccccccccccccccccccccccc);");
8691   verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8692                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8693                "          .aaaaa(aaaaa),\n"
8694                "      aaaaaaaaaaaaaaaaaaaaa);");
8695   verifyFormat("void f() {\n"
8696                "  aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8697                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
8698                "}");
8699   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8700                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8701                "    .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8702                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8703                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
8704   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8705                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8706                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8707                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
8708                "}");
8709 
8710   // Here, it is not necessary to wrap at "." or "->".
8711   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
8712                "    aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
8713   verifyFormat(
8714       "aaaaaaaaaaa->aaaaaaaaa(\n"
8715       "    aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8716       "    aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
8717 
8718   verifyFormat(
8719       "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8720       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
8721   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
8722                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
8723   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
8724                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
8725 
8726   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8727                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8728                "    .a();");
8729 
8730   FormatStyle NoBinPacking = getLLVMStyle();
8731   NoBinPacking.BinPackParameters = false;
8732   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
8733                "    .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
8734                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
8735                "                         aaaaaaaaaaaaaaaaaaa,\n"
8736                "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
8737                NoBinPacking);
8738 
8739   // If there is a subsequent call, change to hanging indentation.
8740   verifyFormat(
8741       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8742       "                         aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
8743       "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
8744   verifyFormat(
8745       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8746       "    aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
8747   verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8748                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8749                "                 .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
8750   verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8751                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8752                "               .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
8753 }
8754 
8755 TEST_F(FormatTest, WrapsTemplateDeclarations) {
8756   verifyFormat("template <typename T>\n"
8757                "virtual void loooooooooooongFunction(int Param1, int Param2);");
8758   verifyFormat("template <typename T>\n"
8759                "// T should be one of {A, B}.\n"
8760                "virtual void loooooooooooongFunction(int Param1, int Param2);");
8761   verifyFormat(
8762       "template <typename T>\n"
8763       "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
8764   verifyFormat("template <typename T>\n"
8765                "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
8766                "       int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
8767   verifyFormat(
8768       "template <typename T>\n"
8769       "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
8770       "                                      int Paaaaaaaaaaaaaaaaaaaaram2);");
8771   verifyFormat(
8772       "template <typename T>\n"
8773       "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
8774       "                    aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
8775       "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8776   verifyFormat("template <typename T>\n"
8777                "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8778                "    int aaaaaaaaaaaaaaaaaaaaaa);");
8779   verifyFormat(
8780       "template <typename T1, typename T2 = char, typename T3 = char,\n"
8781       "          typename T4 = char>\n"
8782       "void f();");
8783   verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
8784                "          template <typename> class cccccccccccccccccccccc,\n"
8785                "          typename ddddddddddddd>\n"
8786                "class C {};");
8787   verifyFormat(
8788       "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
8789       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8790 
8791   verifyFormat("void f() {\n"
8792                "  a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
8793                "      a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
8794                "}");
8795 
8796   verifyFormat("template <typename T> class C {};");
8797   verifyFormat("template <typename T> void f();");
8798   verifyFormat("template <typename T> void f() {}");
8799   verifyFormat(
8800       "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
8801       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8802       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
8803       "    new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
8804       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8805       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
8806       "        bbbbbbbbbbbbbbbbbbbbbbbb);",
8807       getLLVMStyleWithColumns(72));
8808   EXPECT_EQ("static_cast<A< //\n"
8809             "    B> *>(\n"
8810             "\n"
8811             ");",
8812             format("static_cast<A<//\n"
8813                    "    B>*>(\n"
8814                    "\n"
8815                    "    );"));
8816   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8817                "    const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
8818 
8819   FormatStyle AlwaysBreak = getLLVMStyle();
8820   AlwaysBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
8821   verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
8822   verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
8823   verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
8824   verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8825                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
8826                "    ccccccccccccccccccccccccccccccccccccccccccccccc);");
8827   verifyFormat("template <template <typename> class Fooooooo,\n"
8828                "          template <typename> class Baaaaaaar>\n"
8829                "struct C {};",
8830                AlwaysBreak);
8831   verifyFormat("template <typename T> // T can be A, B or C.\n"
8832                "struct C {};",
8833                AlwaysBreak);
8834   verifyFormat("template <enum E> class A {\n"
8835                "public:\n"
8836                "  E *f();\n"
8837                "};");
8838 
8839   FormatStyle NeverBreak = getLLVMStyle();
8840   NeverBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_No;
8841   verifyFormat("template <typename T> class C {};", NeverBreak);
8842   verifyFormat("template <typename T> void f();", NeverBreak);
8843   verifyFormat("template <typename T> void f() {}", NeverBreak);
8844   verifyFormat("template <typename T>\nvoid foo(aaaaaaaaaaaaaaaaaaaaaaaaaa "
8845                "bbbbbbbbbbbbbbbbbbbb) {}",
8846                NeverBreak);
8847   verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8848                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
8849                "    ccccccccccccccccccccccccccccccccccccccccccccccc);",
8850                NeverBreak);
8851   verifyFormat("template <template <typename> class Fooooooo,\n"
8852                "          template <typename> class Baaaaaaar>\n"
8853                "struct C {};",
8854                NeverBreak);
8855   verifyFormat("template <typename T> // T can be A, B or C.\n"
8856                "struct C {};",
8857                NeverBreak);
8858   verifyFormat("template <enum E> class A {\n"
8859                "public:\n"
8860                "  E *f();\n"
8861                "};",
8862                NeverBreak);
8863   NeverBreak.PenaltyBreakTemplateDeclaration = 100;
8864   verifyFormat("template <typename T> void\nfoo(aaaaaaaaaaaaaaaaaaaaaaaaaa "
8865                "bbbbbbbbbbbbbbbbbbbb) {}",
8866                NeverBreak);
8867 }
8868 
8869 TEST_F(FormatTest, WrapsTemplateDeclarationsWithComments) {
8870   FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp);
8871   Style.ColumnLimit = 60;
8872   EXPECT_EQ("// Baseline - no comments.\n"
8873             "template <\n"
8874             "    typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n"
8875             "void f() {}",
8876             format("// Baseline - no comments.\n"
8877                    "template <\n"
8878                    "    typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n"
8879                    "void f() {}",
8880                    Style));
8881 
8882   EXPECT_EQ("template <\n"
8883             "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value>  // trailing\n"
8884             "void f() {}",
8885             format("template <\n"
8886                    "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
8887                    "void f() {}",
8888                    Style));
8889 
8890   EXPECT_EQ(
8891       "template <\n"
8892       "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n"
8893       "void f() {}",
8894       format("template <typename aaaaaaaaaa<bbbbbbbbbbbb>::value>  /* line */\n"
8895              "void f() {}",
8896              Style));
8897 
8898   EXPECT_EQ(
8899       "template <\n"
8900       "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value>  // trailing\n"
8901       "                                               // multiline\n"
8902       "void f() {}",
8903       format("template <\n"
8904              "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
8905              "                                              // multiline\n"
8906              "void f() {}",
8907              Style));
8908 
8909   EXPECT_EQ(
8910       "template <typename aaaaaaaaaa<\n"
8911       "    bbbbbbbbbbbb>::value>  // trailing loooong\n"
8912       "void f() {}",
8913       format(
8914           "template <\n"
8915           "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing loooong\n"
8916           "void f() {}",
8917           Style));
8918 }
8919 
8920 TEST_F(FormatTest, WrapsTemplateParameters) {
8921   FormatStyle Style = getLLVMStyle();
8922   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
8923   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
8924   verifyFormat(
8925       "template <typename... a> struct q {};\n"
8926       "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
8927       "    aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
8928       "    y;",
8929       Style);
8930   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
8931   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
8932   verifyFormat(
8933       "template <typename... a> struct r {};\n"
8934       "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
8935       "    aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
8936       "    y;",
8937       Style);
8938   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
8939   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
8940   verifyFormat("template <typename... a> struct s {};\n"
8941                "extern s<\n"
8942                "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
8943                "aaaaaaaaaaaaaaaaaaaaaa,\n"
8944                "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
8945                "aaaaaaaaaaaaaaaaaaaaaa>\n"
8946                "    y;",
8947                Style);
8948   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
8949   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
8950   verifyFormat("template <typename... a> struct t {};\n"
8951                "extern t<\n"
8952                "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
8953                "aaaaaaaaaaaaaaaaaaaaaa,\n"
8954                "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
8955                "aaaaaaaaaaaaaaaaaaaaaa>\n"
8956                "    y;",
8957                Style);
8958 }
8959 
8960 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
8961   verifyFormat(
8962       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
8963       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
8964   verifyFormat(
8965       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
8966       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8967       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
8968 
8969   // FIXME: Should we have the extra indent after the second break?
8970   verifyFormat(
8971       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
8972       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
8973       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
8974 
8975   verifyFormat(
8976       "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
8977       "                    cccccccccccccccccccccccccccccccccccccccccccccc());");
8978 
8979   // Breaking at nested name specifiers is generally not desirable.
8980   verifyFormat(
8981       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8982       "    aaaaaaaaaaaaaaaaaaaaaaa);");
8983 
8984   verifyFormat("aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n"
8985                "                   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
8986                "                       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8987                "                   aaaaaaaaaaaaaaaaaaaaa);",
8988                getLLVMStyleWithColumns(74));
8989 
8990   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
8991                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8992                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
8993 }
8994 
8995 TEST_F(FormatTest, UnderstandsTemplateParameters) {
8996   verifyFormat("A<int> a;");
8997   verifyFormat("A<A<A<int>>> a;");
8998   verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
8999   verifyFormat("bool x = a < 1 || 2 > a;");
9000   verifyFormat("bool x = 5 < f<int>();");
9001   verifyFormat("bool x = f<int>() > 5;");
9002   verifyFormat("bool x = 5 < a<int>::x;");
9003   verifyFormat("bool x = a < 4 ? a > 2 : false;");
9004   verifyFormat("bool x = f() ? a < 2 : a > 2;");
9005 
9006   verifyGoogleFormat("A<A<int>> a;");
9007   verifyGoogleFormat("A<A<A<int>>> a;");
9008   verifyGoogleFormat("A<A<A<A<int>>>> a;");
9009   verifyGoogleFormat("A<A<int> > a;");
9010   verifyGoogleFormat("A<A<A<int> > > a;");
9011   verifyGoogleFormat("A<A<A<A<int> > > > a;");
9012   verifyGoogleFormat("A<::A<int>> a;");
9013   verifyGoogleFormat("A<::A> a;");
9014   verifyGoogleFormat("A< ::A> a;");
9015   verifyGoogleFormat("A< ::A<int> > a;");
9016   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
9017   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
9018   EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle()));
9019   EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle()));
9020   EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };",
9021             format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle()));
9022 
9023   verifyFormat("A<A<int>> a;", getChromiumStyle(FormatStyle::LK_Cpp));
9024 
9025   // template closer followed by a token that starts with > or =
9026   verifyFormat("bool b = a<1> > 1;");
9027   verifyFormat("bool b = a<1> >= 1;");
9028   verifyFormat("int i = a<1> >> 1;");
9029   FormatStyle Style = getLLVMStyle();
9030   Style.SpaceBeforeAssignmentOperators = false;
9031   verifyFormat("bool b= a<1> == 1;", Style);
9032   verifyFormat("a<int> = 1;", Style);
9033   verifyFormat("a<int> >>= 1;", Style);
9034 
9035   verifyFormat("test < a | b >> c;");
9036   verifyFormat("test<test<a | b>> c;");
9037   verifyFormat("test >> a >> b;");
9038   verifyFormat("test << a >> b;");
9039 
9040   verifyFormat("f<int>();");
9041   verifyFormat("template <typename T> void f() {}");
9042   verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
9043   verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : "
9044                "sizeof(char)>::type>;");
9045   verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};");
9046   verifyFormat("f(a.operator()<A>());");
9047   verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9048                "      .template operator()<A>());",
9049                getLLVMStyleWithColumns(35));
9050 
9051   // Not template parameters.
9052   verifyFormat("return a < b && c > d;");
9053   verifyFormat("void f() {\n"
9054                "  while (a < b && c > d) {\n"
9055                "  }\n"
9056                "}");
9057   verifyFormat("template <typename... Types>\n"
9058                "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
9059 
9060   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9061                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
9062                getLLVMStyleWithColumns(60));
9063   verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
9064   verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
9065   verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <");
9066   verifyFormat("some_templated_type<decltype([](int i) { return i; })>");
9067 }
9068 
9069 TEST_F(FormatTest, UnderstandsShiftOperators) {
9070   verifyFormat("if (i < x >> 1)");
9071   verifyFormat("while (i < x >> 1)");
9072   verifyFormat("for (unsigned i = 0; i < i; ++i, v = v >> 1)");
9073   verifyFormat("for (unsigned i = 0; i < x >> 1; ++i, v = v >> 1)");
9074   verifyFormat(
9075       "for (std::vector<int>::iterator i = 0; i < x >> 1; ++i, v = v >> 1)");
9076   verifyFormat("Foo.call<Bar<Function>>()");
9077   verifyFormat("if (Foo.call<Bar<Function>>() == 0)");
9078   verifyFormat("for (std::vector<std::pair<int>>::iterator i = 0; i < x >> 1; "
9079                "++i, v = v >> 1)");
9080   verifyFormat("if (w<u<v<x>>, 1>::t)");
9081 }
9082 
9083 TEST_F(FormatTest, BitshiftOperatorWidth) {
9084   EXPECT_EQ("int a = 1 << 2; /* foo\n"
9085             "                   bar */",
9086             format("int    a=1<<2;  /* foo\n"
9087                    "                   bar */"));
9088 
9089   EXPECT_EQ("int b = 256 >> 1; /* foo\n"
9090             "                     bar */",
9091             format("int  b  =256>>1 ;  /* foo\n"
9092                    "                      bar */"));
9093 }
9094 
9095 TEST_F(FormatTest, UnderstandsBinaryOperators) {
9096   verifyFormat("COMPARE(a, ==, b);");
9097   verifyFormat("auto s = sizeof...(Ts) - 1;");
9098 }
9099 
9100 TEST_F(FormatTest, UnderstandsPointersToMembers) {
9101   verifyFormat("int A::*x;");
9102   verifyFormat("int (S::*func)(void *);");
9103   verifyFormat("void f() { int (S::*func)(void *); }");
9104   verifyFormat("typedef bool *(Class::*Member)() const;");
9105   verifyFormat("void f() {\n"
9106                "  (a->*f)();\n"
9107                "  a->*x;\n"
9108                "  (a.*f)();\n"
9109                "  ((*a).*f)();\n"
9110                "  a.*x;\n"
9111                "}");
9112   verifyFormat("void f() {\n"
9113                "  (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
9114                "      aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
9115                "}");
9116   verifyFormat(
9117       "(aaaaaaaaaa->*bbbbbbb)(\n"
9118       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
9119   FormatStyle Style = getLLVMStyle();
9120   Style.PointerAlignment = FormatStyle::PAS_Left;
9121   verifyFormat("typedef bool* (Class::*Member)() const;", Style);
9122 }
9123 
9124 TEST_F(FormatTest, UnderstandsUnaryOperators) {
9125   verifyFormat("int a = -2;");
9126   verifyFormat("f(-1, -2, -3);");
9127   verifyFormat("a[-1] = 5;");
9128   verifyFormat("int a = 5 + -2;");
9129   verifyFormat("if (i == -1) {\n}");
9130   verifyFormat("if (i != -1) {\n}");
9131   verifyFormat("if (i > -1) {\n}");
9132   verifyFormat("if (i < -1) {\n}");
9133   verifyFormat("++(a->f());");
9134   verifyFormat("--(a->f());");
9135   verifyFormat("(a->f())++;");
9136   verifyFormat("a[42]++;");
9137   verifyFormat("if (!(a->f())) {\n}");
9138   verifyFormat("if (!+i) {\n}");
9139   verifyFormat("~&a;");
9140 
9141   verifyFormat("a-- > b;");
9142   verifyFormat("b ? -a : c;");
9143   verifyFormat("n * sizeof char16;");
9144   verifyFormat("n * alignof char16;", getGoogleStyle());
9145   verifyFormat("sizeof(char);");
9146   verifyFormat("alignof(char);", getGoogleStyle());
9147 
9148   verifyFormat("return -1;");
9149   verifyFormat("throw -1;");
9150   verifyFormat("switch (a) {\n"
9151                "case -1:\n"
9152                "  break;\n"
9153                "}");
9154   verifyFormat("#define X -1");
9155   verifyFormat("#define X -kConstant");
9156 
9157   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
9158   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
9159 
9160   verifyFormat("int a = /* confusing comment */ -1;");
9161   // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
9162   verifyFormat("int a = i /* confusing comment */++;");
9163 
9164   verifyFormat("co_yield -1;");
9165   verifyFormat("co_return -1;");
9166 
9167   // Check that * is not treated as a binary operator when we set
9168   // PointerAlignment as PAS_Left after a keyword and not a declaration.
9169   FormatStyle PASLeftStyle = getLLVMStyle();
9170   PASLeftStyle.PointerAlignment = FormatStyle::PAS_Left;
9171   verifyFormat("co_return *a;", PASLeftStyle);
9172   verifyFormat("co_await *a;", PASLeftStyle);
9173   verifyFormat("co_yield *a", PASLeftStyle);
9174   verifyFormat("return *a;", PASLeftStyle);
9175 }
9176 
9177 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) {
9178   verifyFormat("if (!aaaaaaaaaa( // break\n"
9179                "        aaaaa)) {\n"
9180                "}");
9181   verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
9182                "    aaaaa));");
9183   verifyFormat("*aaa = aaaaaaa( // break\n"
9184                "    bbbbbb);");
9185 }
9186 
9187 TEST_F(FormatTest, UnderstandsOverloadedOperators) {
9188   verifyFormat("bool operator<();");
9189   verifyFormat("bool operator>();");
9190   verifyFormat("bool operator=();");
9191   verifyFormat("bool operator==();");
9192   verifyFormat("bool operator!=();");
9193   verifyFormat("int operator+();");
9194   verifyFormat("int operator++();");
9195   verifyFormat("int operator++(int) volatile noexcept;");
9196   verifyFormat("bool operator,();");
9197   verifyFormat("bool operator();");
9198   verifyFormat("bool operator()();");
9199   verifyFormat("bool operator[]();");
9200   verifyFormat("operator bool();");
9201   verifyFormat("operator int();");
9202   verifyFormat("operator void *();");
9203   verifyFormat("operator SomeType<int>();");
9204   verifyFormat("operator SomeType<int, int>();");
9205   verifyFormat("operator SomeType<SomeType<int>>();");
9206   verifyFormat("void *operator new(std::size_t size);");
9207   verifyFormat("void *operator new[](std::size_t size);");
9208   verifyFormat("void operator delete(void *ptr);");
9209   verifyFormat("void operator delete[](void *ptr);");
9210   verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
9211                "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
9212   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n"
9213                "    aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;");
9214 
9215   verifyFormat(
9216       "ostream &operator<<(ostream &OutputStream,\n"
9217       "                    SomeReallyLongType WithSomeReallyLongValue);");
9218   verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
9219                "               const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
9220                "  return left.group < right.group;\n"
9221                "}");
9222   verifyFormat("SomeType &operator=(const SomeType &S);");
9223   verifyFormat("f.template operator()<int>();");
9224 
9225   verifyGoogleFormat("operator void*();");
9226   verifyGoogleFormat("operator SomeType<SomeType<int>>();");
9227   verifyGoogleFormat("operator ::A();");
9228 
9229   verifyFormat("using A::operator+;");
9230   verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n"
9231                "int i;");
9232 
9233   // Calling an operator as a member function.
9234   verifyFormat("void f() { a.operator*(); }");
9235   verifyFormat("void f() { a.operator*(b & b); }");
9236   verifyFormat("void f() { a->operator&(a * b); }");
9237   verifyFormat("void f() { NS::a.operator+(*b * *b); }");
9238   // TODO: Calling an operator as a non-member function is hard to distinguish.
9239   // https://llvm.org/PR50629
9240   // verifyFormat("void f() { operator*(a & a); }");
9241   // verifyFormat("void f() { operator&(a, b * b); }");
9242 
9243   verifyFormat("::operator delete(foo);");
9244   verifyFormat("::operator new(n * sizeof(foo));");
9245   verifyFormat("foo() { ::operator delete(foo); }");
9246   verifyFormat("foo() { ::operator new(n * sizeof(foo)); }");
9247 }
9248 
9249 TEST_F(FormatTest, UnderstandsFunctionRefQualification) {
9250   verifyFormat("Deleted &operator=(const Deleted &) & = default;");
9251   verifyFormat("Deleted &operator=(const Deleted &) && = delete;");
9252   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;");
9253   verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;");
9254   verifyFormat("Deleted &operator=(const Deleted &) &;");
9255   verifyFormat("Deleted &operator=(const Deleted &) &&;");
9256   verifyFormat("SomeType MemberFunction(const Deleted &) &;");
9257   verifyFormat("SomeType MemberFunction(const Deleted &) &&;");
9258   verifyFormat("SomeType MemberFunction(const Deleted &) && {}");
9259   verifyFormat("SomeType MemberFunction(const Deleted &) && final {}");
9260   verifyFormat("SomeType MemberFunction(const Deleted &) && override {}");
9261   verifyFormat("void Fn(T const &) const &;");
9262   verifyFormat("void Fn(T const volatile &&) const volatile &&;");
9263   verifyFormat("template <typename T>\n"
9264                "void F(T) && = delete;",
9265                getGoogleStyle());
9266 
9267   FormatStyle AlignLeft = getLLVMStyle();
9268   AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
9269   verifyFormat("void A::b() && {}", AlignLeft);
9270   verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft);
9271   verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;",
9272                AlignLeft);
9273   verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft);
9274   verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft);
9275   verifyFormat("auto Function(T t) & -> void {}", AlignLeft);
9276   verifyFormat("auto Function(T... t) & -> void {}", AlignLeft);
9277   verifyFormat("auto Function(T) & -> void {}", AlignLeft);
9278   verifyFormat("auto Function(T) & -> void;", AlignLeft);
9279   verifyFormat("void Fn(T const&) const&;", AlignLeft);
9280   verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft);
9281 
9282   FormatStyle Spaces = getLLVMStyle();
9283   Spaces.SpacesInCStyleCastParentheses = true;
9284   verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces);
9285   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces);
9286   verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces);
9287   verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces);
9288 
9289   Spaces.SpacesInCStyleCastParentheses = false;
9290   Spaces.SpacesInParentheses = true;
9291   verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces);
9292   verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;",
9293                Spaces);
9294   verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces);
9295   verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces);
9296 
9297   FormatStyle BreakTemplate = getLLVMStyle();
9298   BreakTemplate.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
9299 
9300   verifyFormat("struct f {\n"
9301                "  template <class T>\n"
9302                "  int &foo(const std::string &str) &noexcept {}\n"
9303                "};",
9304                BreakTemplate);
9305 
9306   verifyFormat("struct f {\n"
9307                "  template <class T>\n"
9308                "  int &foo(const std::string &str) &&noexcept {}\n"
9309                "};",
9310                BreakTemplate);
9311 
9312   verifyFormat("struct f {\n"
9313                "  template <class T>\n"
9314                "  int &foo(const std::string &str) const &noexcept {}\n"
9315                "};",
9316                BreakTemplate);
9317 
9318   verifyFormat("struct f {\n"
9319                "  template <class T>\n"
9320                "  int &foo(const std::string &str) const &noexcept {}\n"
9321                "};",
9322                BreakTemplate);
9323 
9324   verifyFormat("struct f {\n"
9325                "  template <class T>\n"
9326                "  auto foo(const std::string &str) &&noexcept -> int & {}\n"
9327                "};",
9328                BreakTemplate);
9329 
9330   FormatStyle AlignLeftBreakTemplate = getLLVMStyle();
9331   AlignLeftBreakTemplate.AlwaysBreakTemplateDeclarations =
9332       FormatStyle::BTDS_Yes;
9333   AlignLeftBreakTemplate.PointerAlignment = FormatStyle::PAS_Left;
9334 
9335   verifyFormat("struct f {\n"
9336                "  template <class T>\n"
9337                "  int& foo(const std::string& str) & noexcept {}\n"
9338                "};",
9339                AlignLeftBreakTemplate);
9340 
9341   verifyFormat("struct f {\n"
9342                "  template <class T>\n"
9343                "  int& foo(const std::string& str) && noexcept {}\n"
9344                "};",
9345                AlignLeftBreakTemplate);
9346 
9347   verifyFormat("struct f {\n"
9348                "  template <class T>\n"
9349                "  int& foo(const std::string& str) const& noexcept {}\n"
9350                "};",
9351                AlignLeftBreakTemplate);
9352 
9353   verifyFormat("struct f {\n"
9354                "  template <class T>\n"
9355                "  int& foo(const std::string& str) const&& noexcept {}\n"
9356                "};",
9357                AlignLeftBreakTemplate);
9358 
9359   verifyFormat("struct f {\n"
9360                "  template <class T>\n"
9361                "  auto foo(const std::string& str) && noexcept -> int& {}\n"
9362                "};",
9363                AlignLeftBreakTemplate);
9364 
9365   // The `&` in `Type&` should not be confused with a trailing `&` of
9366   // DEPRECATED(reason) member function.
9367   verifyFormat("struct f {\n"
9368                "  template <class T>\n"
9369                "  DEPRECATED(reason)\n"
9370                "  Type &foo(arguments) {}\n"
9371                "};",
9372                BreakTemplate);
9373 
9374   verifyFormat("struct f {\n"
9375                "  template <class T>\n"
9376                "  DEPRECATED(reason)\n"
9377                "  Type& foo(arguments) {}\n"
9378                "};",
9379                AlignLeftBreakTemplate);
9380 
9381   verifyFormat("void (*foopt)(int) = &func;");
9382 }
9383 
9384 TEST_F(FormatTest, UnderstandsNewAndDelete) {
9385   verifyFormat("void f() {\n"
9386                "  A *a = new A;\n"
9387                "  A *a = new (placement) A;\n"
9388                "  delete a;\n"
9389                "  delete (A *)a;\n"
9390                "}");
9391   verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
9392                "    typename aaaaaaaaaaaaaaaaaaaaaaaa();");
9393   verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
9394                "    new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
9395                "        typename aaaaaaaaaaaaaaaaaaaaaaaa();");
9396   verifyFormat("delete[] h->p;");
9397 }
9398 
9399 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
9400   verifyFormat("int *f(int *a) {}");
9401   verifyFormat("int main(int argc, char **argv) {}");
9402   verifyFormat("Test::Test(int b) : a(b * b) {}");
9403   verifyIndependentOfContext("f(a, *a);");
9404   verifyFormat("void g() { f(*a); }");
9405   verifyIndependentOfContext("int a = b * 10;");
9406   verifyIndependentOfContext("int a = 10 * b;");
9407   verifyIndependentOfContext("int a = b * c;");
9408   verifyIndependentOfContext("int a += b * c;");
9409   verifyIndependentOfContext("int a -= b * c;");
9410   verifyIndependentOfContext("int a *= b * c;");
9411   verifyIndependentOfContext("int a /= b * c;");
9412   verifyIndependentOfContext("int a = *b;");
9413   verifyIndependentOfContext("int a = *b * c;");
9414   verifyIndependentOfContext("int a = b * *c;");
9415   verifyIndependentOfContext("int a = b * (10);");
9416   verifyIndependentOfContext("S << b * (10);");
9417   verifyIndependentOfContext("return 10 * b;");
9418   verifyIndependentOfContext("return *b * *c;");
9419   verifyIndependentOfContext("return a & ~b;");
9420   verifyIndependentOfContext("f(b ? *c : *d);");
9421   verifyIndependentOfContext("int a = b ? *c : *d;");
9422   verifyIndependentOfContext("*b = a;");
9423   verifyIndependentOfContext("a * ~b;");
9424   verifyIndependentOfContext("a * !b;");
9425   verifyIndependentOfContext("a * +b;");
9426   verifyIndependentOfContext("a * -b;");
9427   verifyIndependentOfContext("a * ++b;");
9428   verifyIndependentOfContext("a * --b;");
9429   verifyIndependentOfContext("a[4] * b;");
9430   verifyIndependentOfContext("a[a * a] = 1;");
9431   verifyIndependentOfContext("f() * b;");
9432   verifyIndependentOfContext("a * [self dostuff];");
9433   verifyIndependentOfContext("int x = a * (a + b);");
9434   verifyIndependentOfContext("(a *)(a + b);");
9435   verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
9436   verifyIndependentOfContext("int *pa = (int *)&a;");
9437   verifyIndependentOfContext("return sizeof(int **);");
9438   verifyIndependentOfContext("return sizeof(int ******);");
9439   verifyIndependentOfContext("return (int **&)a;");
9440   verifyIndependentOfContext("f((*PointerToArray)[10]);");
9441   verifyFormat("void f(Type (*parameter)[10]) {}");
9442   verifyFormat("void f(Type (&parameter)[10]) {}");
9443   verifyGoogleFormat("return sizeof(int**);");
9444   verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
9445   verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
9446   verifyFormat("auto a = [](int **&, int ***) {};");
9447   verifyFormat("auto PointerBinding = [](const char *S) {};");
9448   verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
9449   verifyFormat("[](const decltype(*a) &value) {}");
9450   verifyFormat("[](const typeof(*a) &value) {}");
9451   verifyFormat("[](const _Atomic(a *) &value) {}");
9452   verifyFormat("[](const __underlying_type(a) &value) {}");
9453   verifyFormat("decltype(a * b) F();");
9454   verifyFormat("typeof(a * b) F();");
9455   verifyFormat("#define MACRO() [](A *a) { return 1; }");
9456   verifyFormat("Constructor() : member([](A *a, B *b) {}) {}");
9457   verifyIndependentOfContext("typedef void (*f)(int *a);");
9458   verifyIndependentOfContext("int i{a * b};");
9459   verifyIndependentOfContext("aaa && aaa->f();");
9460   verifyIndependentOfContext("int x = ~*p;");
9461   verifyFormat("Constructor() : a(a), area(width * height) {}");
9462   verifyFormat("Constructor() : a(a), area(a, width * height) {}");
9463   verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}");
9464   verifyFormat("void f() { f(a, c * d); }");
9465   verifyFormat("void f() { f(new a(), c * d); }");
9466   verifyFormat("void f(const MyOverride &override);");
9467   verifyFormat("void f(const MyFinal &final);");
9468   verifyIndependentOfContext("bool a = f() && override.f();");
9469   verifyIndependentOfContext("bool a = f() && final.f();");
9470 
9471   verifyIndependentOfContext("InvalidRegions[*R] = 0;");
9472 
9473   verifyIndependentOfContext("A<int *> a;");
9474   verifyIndependentOfContext("A<int **> a;");
9475   verifyIndependentOfContext("A<int *, int *> a;");
9476   verifyIndependentOfContext("A<int *[]> a;");
9477   verifyIndependentOfContext(
9478       "const char *const p = reinterpret_cast<const char *const>(q);");
9479   verifyIndependentOfContext("A<int **, int **> a;");
9480   verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
9481   verifyFormat("for (char **a = b; *a; ++a) {\n}");
9482   verifyFormat("for (; a && b;) {\n}");
9483   verifyFormat("bool foo = true && [] { return false; }();");
9484 
9485   verifyFormat(
9486       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9487       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9488 
9489   verifyGoogleFormat("int const* a = &b;");
9490   verifyGoogleFormat("**outparam = 1;");
9491   verifyGoogleFormat("*outparam = a * b;");
9492   verifyGoogleFormat("int main(int argc, char** argv) {}");
9493   verifyGoogleFormat("A<int*> a;");
9494   verifyGoogleFormat("A<int**> a;");
9495   verifyGoogleFormat("A<int*, int*> a;");
9496   verifyGoogleFormat("A<int**, int**> a;");
9497   verifyGoogleFormat("f(b ? *c : *d);");
9498   verifyGoogleFormat("int a = b ? *c : *d;");
9499   verifyGoogleFormat("Type* t = **x;");
9500   verifyGoogleFormat("Type* t = *++*x;");
9501   verifyGoogleFormat("*++*x;");
9502   verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
9503   verifyGoogleFormat("Type* t = x++ * y;");
9504   verifyGoogleFormat(
9505       "const char* const p = reinterpret_cast<const char* const>(q);");
9506   verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
9507   verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
9508   verifyGoogleFormat("template <typename T>\n"
9509                      "void f(int i = 0, SomeType** temps = NULL);");
9510 
9511   FormatStyle Left = getLLVMStyle();
9512   Left.PointerAlignment = FormatStyle::PAS_Left;
9513   verifyFormat("x = *a(x) = *a(y);", Left);
9514   verifyFormat("for (;; *a = b) {\n}", Left);
9515   verifyFormat("return *this += 1;", Left);
9516   verifyFormat("throw *x;", Left);
9517   verifyFormat("delete *x;", Left);
9518   verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left);
9519   verifyFormat("[](const decltype(*a)* ptr) {}", Left);
9520   verifyFormat("[](const typeof(*a)* ptr) {}", Left);
9521   verifyFormat("[](const _Atomic(a*)* ptr) {}", Left);
9522   verifyFormat("[](const __underlying_type(a)* ptr) {}", Left);
9523   verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left);
9524   verifyFormat("auto x(A&&, B&&, C&&) -> D;", Left);
9525   verifyFormat("auto x = [](A&&, B&&, C&&) -> D {};", Left);
9526   verifyFormat("template <class T> X(T&&, T&&, T&&) -> X<T>;", Left);
9527 
9528   verifyIndependentOfContext("a = *(x + y);");
9529   verifyIndependentOfContext("a = &(x + y);");
9530   verifyIndependentOfContext("*(x + y).call();");
9531   verifyIndependentOfContext("&(x + y)->call();");
9532   verifyFormat("void f() { &(*I).first; }");
9533 
9534   verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
9535   verifyFormat("f(* /* confusing comment */ foo);");
9536   verifyFormat("void (* /*deleter*/)(const Slice &key, void *value)");
9537   verifyFormat("void foo(int * // this is the first paramters\n"
9538                "         ,\n"
9539                "         int second);");
9540   verifyFormat("double term = a * // first\n"
9541                "              b;");
9542   verifyFormat(
9543       "int *MyValues = {\n"
9544       "    *A, // Operator detection might be confused by the '{'\n"
9545       "    *BB // Operator detection might be confused by previous comment\n"
9546       "};");
9547 
9548   verifyIndependentOfContext("if (int *a = &b)");
9549   verifyIndependentOfContext("if (int &a = *b)");
9550   verifyIndependentOfContext("if (a & b[i])");
9551   verifyIndependentOfContext("if constexpr (a & b[i])");
9552   verifyIndependentOfContext("if CONSTEXPR (a & b[i])");
9553   verifyIndependentOfContext("if (a * (b * c))");
9554   verifyIndependentOfContext("if constexpr (a * (b * c))");
9555   verifyIndependentOfContext("if CONSTEXPR (a * (b * c))");
9556   verifyIndependentOfContext("if (a::b::c::d & b[i])");
9557   verifyIndependentOfContext("if (*b[i])");
9558   verifyIndependentOfContext("if (int *a = (&b))");
9559   verifyIndependentOfContext("while (int *a = &b)");
9560   verifyIndependentOfContext("while (a * (b * c))");
9561   verifyIndependentOfContext("size = sizeof *a;");
9562   verifyIndependentOfContext("if (a && (b = c))");
9563   verifyFormat("void f() {\n"
9564                "  for (const int &v : Values) {\n"
9565                "  }\n"
9566                "}");
9567   verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
9568   verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
9569   verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
9570 
9571   verifyFormat("#define A (!a * b)");
9572   verifyFormat("#define MACRO     \\\n"
9573                "  int *i = a * b; \\\n"
9574                "  void f(a *b);",
9575                getLLVMStyleWithColumns(19));
9576 
9577   verifyIndependentOfContext("A = new SomeType *[Length];");
9578   verifyIndependentOfContext("A = new SomeType *[Length]();");
9579   verifyIndependentOfContext("T **t = new T *;");
9580   verifyIndependentOfContext("T **t = new T *();");
9581   verifyGoogleFormat("A = new SomeType*[Length]();");
9582   verifyGoogleFormat("A = new SomeType*[Length];");
9583   verifyGoogleFormat("T** t = new T*;");
9584   verifyGoogleFormat("T** t = new T*();");
9585 
9586   verifyFormat("STATIC_ASSERT((a & b) == 0);");
9587   verifyFormat("STATIC_ASSERT(0 == (a & b));");
9588   verifyFormat("template <bool a, bool b> "
9589                "typename t::if<x && y>::type f() {}");
9590   verifyFormat("template <int *y> f() {}");
9591   verifyFormat("vector<int *> v;");
9592   verifyFormat("vector<int *const> v;");
9593   verifyFormat("vector<int *const **const *> v;");
9594   verifyFormat("vector<int *volatile> v;");
9595   verifyFormat("vector<a *_Nonnull> v;");
9596   verifyFormat("vector<a *_Nullable> v;");
9597   verifyFormat("vector<a *_Null_unspecified> v;");
9598   verifyFormat("vector<a *__ptr32> v;");
9599   verifyFormat("vector<a *__ptr64> v;");
9600   verifyFormat("vector<a *__capability> v;");
9601   FormatStyle TypeMacros = getLLVMStyle();
9602   TypeMacros.TypenameMacros = {"LIST"};
9603   verifyFormat("vector<LIST(uint64_t)> v;", TypeMacros);
9604   verifyFormat("vector<LIST(uint64_t) *> v;", TypeMacros);
9605   verifyFormat("vector<LIST(uint64_t) **> v;", TypeMacros);
9606   verifyFormat("vector<LIST(uint64_t) *attr> v;", TypeMacros);
9607   verifyFormat("vector<A(uint64_t) * attr> v;", TypeMacros); // multiplication
9608 
9609   FormatStyle CustomQualifier = getLLVMStyle();
9610   // Add identifiers that should not be parsed as a qualifier by default.
9611   CustomQualifier.AttributeMacros.push_back("__my_qualifier");
9612   CustomQualifier.AttributeMacros.push_back("_My_qualifier");
9613   CustomQualifier.AttributeMacros.push_back("my_other_qualifier");
9614   verifyFormat("vector<a * __my_qualifier> parse_as_multiply;");
9615   verifyFormat("vector<a *__my_qualifier> v;", CustomQualifier);
9616   verifyFormat("vector<a * _My_qualifier> parse_as_multiply;");
9617   verifyFormat("vector<a *_My_qualifier> v;", CustomQualifier);
9618   verifyFormat("vector<a * my_other_qualifier> parse_as_multiply;");
9619   verifyFormat("vector<a *my_other_qualifier> v;", CustomQualifier);
9620   verifyFormat("vector<a * _NotAQualifier> v;");
9621   verifyFormat("vector<a * __not_a_qualifier> v;");
9622   verifyFormat("vector<a * b> v;");
9623   verifyFormat("foo<b && false>();");
9624   verifyFormat("foo<b & 1>();");
9625   verifyFormat("decltype(*::std::declval<const T &>()) void F();");
9626   verifyFormat("typeof(*::std::declval<const T &>()) void F();");
9627   verifyFormat("_Atomic(*::std::declval<const T &>()) void F();");
9628   verifyFormat("__underlying_type(*::std::declval<const T &>()) void F();");
9629   verifyFormat(
9630       "template <class T, class = typename std::enable_if<\n"
9631       "                       std::is_integral<T>::value &&\n"
9632       "                       (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
9633       "void F();",
9634       getLLVMStyleWithColumns(70));
9635   verifyFormat("template <class T,\n"
9636                "          class = typename std::enable_if<\n"
9637                "              std::is_integral<T>::value &&\n"
9638                "              (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n"
9639                "          class U>\n"
9640                "void F();",
9641                getLLVMStyleWithColumns(70));
9642   verifyFormat(
9643       "template <class T,\n"
9644       "          class = typename ::std::enable_if<\n"
9645       "              ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
9646       "void F();",
9647       getGoogleStyleWithColumns(68));
9648 
9649   verifyIndependentOfContext("MACRO(int *i);");
9650   verifyIndependentOfContext("MACRO(auto *a);");
9651   verifyIndependentOfContext("MACRO(const A *a);");
9652   verifyIndependentOfContext("MACRO(_Atomic(A) *a);");
9653   verifyIndependentOfContext("MACRO(decltype(A) *a);");
9654   verifyIndependentOfContext("MACRO(typeof(A) *a);");
9655   verifyIndependentOfContext("MACRO(__underlying_type(A) *a);");
9656   verifyIndependentOfContext("MACRO(A *const a);");
9657   verifyIndependentOfContext("MACRO(A *restrict a);");
9658   verifyIndependentOfContext("MACRO(A *__restrict__ a);");
9659   verifyIndependentOfContext("MACRO(A *__restrict a);");
9660   verifyIndependentOfContext("MACRO(A *volatile a);");
9661   verifyIndependentOfContext("MACRO(A *__volatile a);");
9662   verifyIndependentOfContext("MACRO(A *__volatile__ a);");
9663   verifyIndependentOfContext("MACRO(A *_Nonnull a);");
9664   verifyIndependentOfContext("MACRO(A *_Nullable a);");
9665   verifyIndependentOfContext("MACRO(A *_Null_unspecified a);");
9666   verifyIndependentOfContext("MACRO(A *__attribute__((foo)) a);");
9667   verifyIndependentOfContext("MACRO(A *__attribute((foo)) a);");
9668   verifyIndependentOfContext("MACRO(A *[[clang::attr]] a);");
9669   verifyIndependentOfContext("MACRO(A *[[clang::attr(\"foo\")]] a);");
9670   verifyIndependentOfContext("MACRO(A *__ptr32 a);");
9671   verifyIndependentOfContext("MACRO(A *__ptr64 a);");
9672   verifyIndependentOfContext("MACRO(A *__capability);");
9673   verifyIndependentOfContext("MACRO(A &__capability);");
9674   verifyFormat("MACRO(A *__my_qualifier);");               // type declaration
9675   verifyFormat("void f() { MACRO(A * __my_qualifier); }"); // multiplication
9676   // If we add __my_qualifier to AttributeMacros it should always be parsed as
9677   // a type declaration:
9678   verifyFormat("MACRO(A *__my_qualifier);", CustomQualifier);
9679   verifyFormat("void f() { MACRO(A *__my_qualifier); }", CustomQualifier);
9680   // Also check that TypenameMacros prevents parsing it as multiplication:
9681   verifyIndependentOfContext("MACRO(LIST(uint64_t) * a);"); // multiplication
9682   verifyIndependentOfContext("MACRO(LIST(uint64_t) *a);", TypeMacros); // type
9683 
9684   verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
9685   verifyFormat("void f() { f(float{1}, a * a); }");
9686   verifyFormat("void f() { f(float(1), a * a); }");
9687 
9688   verifyFormat("f((void (*)(int))g);");
9689   verifyFormat("f((void (&)(int))g);");
9690   verifyFormat("f((void (^)(int))g);");
9691 
9692   // FIXME: Is there a way to make this work?
9693   // verifyIndependentOfContext("MACRO(A *a);");
9694   verifyFormat("MACRO(A &B);");
9695   verifyFormat("MACRO(A *B);");
9696   verifyFormat("void f() { MACRO(A * B); }");
9697   verifyFormat("void f() { MACRO(A & B); }");
9698 
9699   // This lambda was mis-formatted after D88956 (treating it as a binop):
9700   verifyFormat("auto x = [](const decltype(x) &ptr) {};");
9701   verifyFormat("auto x = [](const decltype(x) *ptr) {};");
9702   verifyFormat("#define lambda [](const decltype(x) &ptr) {}");
9703   verifyFormat("#define lambda [](const decltype(x) *ptr) {}");
9704 
9705   verifyFormat("DatumHandle const *operator->() const { return input_; }");
9706   verifyFormat("return options != nullptr && operator==(*options);");
9707 
9708   EXPECT_EQ("#define OP(x)                                    \\\n"
9709             "  ostream &operator<<(ostream &s, const A &a) {  \\\n"
9710             "    return s << a.DebugString();                 \\\n"
9711             "  }",
9712             format("#define OP(x) \\\n"
9713                    "  ostream &operator<<(ostream &s, const A &a) { \\\n"
9714                    "    return s << a.DebugString(); \\\n"
9715                    "  }",
9716                    getLLVMStyleWithColumns(50)));
9717 
9718   // FIXME: We cannot handle this case yet; we might be able to figure out that
9719   // foo<x> d > v; doesn't make sense.
9720   verifyFormat("foo<a<b && c> d> v;");
9721 
9722   FormatStyle PointerMiddle = getLLVMStyle();
9723   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
9724   verifyFormat("delete *x;", PointerMiddle);
9725   verifyFormat("int * x;", PointerMiddle);
9726   verifyFormat("int *[] x;", PointerMiddle);
9727   verifyFormat("template <int * y> f() {}", PointerMiddle);
9728   verifyFormat("int * f(int * a) {}", PointerMiddle);
9729   verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle);
9730   verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle);
9731   verifyFormat("A<int *> a;", PointerMiddle);
9732   verifyFormat("A<int **> a;", PointerMiddle);
9733   verifyFormat("A<int *, int *> a;", PointerMiddle);
9734   verifyFormat("A<int *[]> a;", PointerMiddle);
9735   verifyFormat("A = new SomeType *[Length]();", PointerMiddle);
9736   verifyFormat("A = new SomeType *[Length];", PointerMiddle);
9737   verifyFormat("T ** t = new T *;", PointerMiddle);
9738 
9739   // Member function reference qualifiers aren't binary operators.
9740   verifyFormat("string // break\n"
9741                "operator()() & {}");
9742   verifyFormat("string // break\n"
9743                "operator()() && {}");
9744   verifyGoogleFormat("template <typename T>\n"
9745                      "auto x() & -> int {}");
9746 
9747   // Should be binary operators when used as an argument expression (overloaded
9748   // operator invoked as a member function).
9749   verifyFormat("void f() { a.operator()(a * a); }");
9750   verifyFormat("void f() { a->operator()(a & a); }");
9751   verifyFormat("void f() { a.operator()(*a & *a); }");
9752   verifyFormat("void f() { a->operator()(*a * *a); }");
9753 
9754   verifyFormat("int operator()(T (&&)[N]) { return 1; }");
9755   verifyFormat("int operator()(T (&)[N]) { return 0; }");
9756 }
9757 
9758 TEST_F(FormatTest, UnderstandsAttributes) {
9759   verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
9760   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
9761                "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
9762   FormatStyle AfterType = getLLVMStyle();
9763   AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
9764   verifyFormat("__attribute__((nodebug)) void\n"
9765                "foo() {}\n",
9766                AfterType);
9767   verifyFormat("__unused void\n"
9768                "foo() {}",
9769                AfterType);
9770 
9771   FormatStyle CustomAttrs = getLLVMStyle();
9772   CustomAttrs.AttributeMacros.push_back("__unused");
9773   CustomAttrs.AttributeMacros.push_back("__attr1");
9774   CustomAttrs.AttributeMacros.push_back("__attr2");
9775   CustomAttrs.AttributeMacros.push_back("no_underscore_attr");
9776   verifyFormat("vector<SomeType *__attribute((foo))> v;");
9777   verifyFormat("vector<SomeType *__attribute__((foo))> v;");
9778   verifyFormat("vector<SomeType * __not_attribute__((foo))> v;");
9779   // Check that it is parsed as a multiplication without AttributeMacros and
9780   // as a pointer qualifier when we add __attr1/__attr2 to AttributeMacros.
9781   verifyFormat("vector<SomeType * __attr1> v;");
9782   verifyFormat("vector<SomeType __attr1 *> v;");
9783   verifyFormat("vector<SomeType __attr1 *const> v;");
9784   verifyFormat("vector<SomeType __attr1 * __attr2> v;");
9785   verifyFormat("vector<SomeType *__attr1> v;", CustomAttrs);
9786   verifyFormat("vector<SomeType *__attr2> v;", CustomAttrs);
9787   verifyFormat("vector<SomeType *no_underscore_attr> v;", CustomAttrs);
9788   verifyFormat("vector<SomeType __attr1 *> v;", CustomAttrs);
9789   verifyFormat("vector<SomeType __attr1 *const> v;", CustomAttrs);
9790   verifyFormat("vector<SomeType __attr1 *__attr2> v;", CustomAttrs);
9791   verifyFormat("vector<SomeType __attr1 *no_underscore_attr> v;", CustomAttrs);
9792 
9793   // Check that these are not parsed as function declarations:
9794   CustomAttrs.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
9795   CustomAttrs.BreakBeforeBraces = FormatStyle::BS_Allman;
9796   verifyFormat("SomeType s(InitValue);", CustomAttrs);
9797   verifyFormat("SomeType s{InitValue};", CustomAttrs);
9798   verifyFormat("SomeType *__unused s(InitValue);", CustomAttrs);
9799   verifyFormat("SomeType *__unused s{InitValue};", CustomAttrs);
9800   verifyFormat("SomeType s __unused(InitValue);", CustomAttrs);
9801   verifyFormat("SomeType s __unused{InitValue};", CustomAttrs);
9802   verifyFormat("SomeType *__capability s(InitValue);", CustomAttrs);
9803   verifyFormat("SomeType *__capability s{InitValue};", CustomAttrs);
9804 }
9805 
9806 TEST_F(FormatTest, UnderstandsPointerQualifiersInCast) {
9807   // Check that qualifiers on pointers don't break parsing of casts.
9808   verifyFormat("x = (foo *const)*v;");
9809   verifyFormat("x = (foo *volatile)*v;");
9810   verifyFormat("x = (foo *restrict)*v;");
9811   verifyFormat("x = (foo *__attribute__((foo)))*v;");
9812   verifyFormat("x = (foo *_Nonnull)*v;");
9813   verifyFormat("x = (foo *_Nullable)*v;");
9814   verifyFormat("x = (foo *_Null_unspecified)*v;");
9815   verifyFormat("x = (foo *_Nonnull)*v;");
9816   verifyFormat("x = (foo *[[clang::attr]])*v;");
9817   verifyFormat("x = (foo *[[clang::attr(\"foo\")]])*v;");
9818   verifyFormat("x = (foo *__ptr32)*v;");
9819   verifyFormat("x = (foo *__ptr64)*v;");
9820   verifyFormat("x = (foo *__capability)*v;");
9821 
9822   // Check that we handle multiple trailing qualifiers and skip them all to
9823   // determine that the expression is a cast to a pointer type.
9824   FormatStyle LongPointerRight = getLLVMStyleWithColumns(999);
9825   FormatStyle LongPointerLeft = getLLVMStyleWithColumns(999);
9826   LongPointerLeft.PointerAlignment = FormatStyle::PAS_Left;
9827   StringRef AllQualifiers =
9828       "const volatile restrict __attribute__((foo)) _Nonnull _Null_unspecified "
9829       "_Nonnull [[clang::attr]] __ptr32 __ptr64 __capability";
9830   verifyFormat(("x = (foo *" + AllQualifiers + ")*v;").str(), LongPointerRight);
9831   verifyFormat(("x = (foo* " + AllQualifiers + ")*v;").str(), LongPointerLeft);
9832 
9833   // Also check that address-of is not parsed as a binary bitwise-and:
9834   verifyFormat("x = (foo *const)&v;");
9835   verifyFormat(("x = (foo *" + AllQualifiers + ")&v;").str(), LongPointerRight);
9836   verifyFormat(("x = (foo* " + AllQualifiers + ")&v;").str(), LongPointerLeft);
9837 
9838   // Check custom qualifiers:
9839   FormatStyle CustomQualifier = getLLVMStyleWithColumns(999);
9840   CustomQualifier.AttributeMacros.push_back("__my_qualifier");
9841   verifyFormat("x = (foo * __my_qualifier) * v;"); // not parsed as qualifier.
9842   verifyFormat("x = (foo *__my_qualifier)*v;", CustomQualifier);
9843   verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)*v;").str(),
9844                CustomQualifier);
9845   verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)&v;").str(),
9846                CustomQualifier);
9847 
9848   // Check that unknown identifiers result in binary operator parsing:
9849   verifyFormat("x = (foo * __unknown_qualifier) * v;");
9850   verifyFormat("x = (foo * __unknown_qualifier) & v;");
9851 }
9852 
9853 TEST_F(FormatTest, UnderstandsSquareAttributes) {
9854   verifyFormat("SomeType s [[unused]] (InitValue);");
9855   verifyFormat("SomeType s [[gnu::unused]] (InitValue);");
9856   verifyFormat("SomeType s [[using gnu: unused]] (InitValue);");
9857   verifyFormat("[[gsl::suppress(\"clang-tidy-check-name\")]] void f() {}");
9858   verifyFormat("void f() [[deprecated(\"so sorry\")]];");
9859   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9860                "    [[unused]] aaaaaaaaaaaaaaaaaaaaaaa(int i);");
9861   verifyFormat("[[nodiscard]] bool f() { return false; }");
9862   verifyFormat("class [[nodiscard]] f {\npublic:\n  f() {}\n}");
9863   verifyFormat("class [[deprecated(\"so sorry\")]] f {\npublic:\n  f() {}\n}");
9864   verifyFormat("class [[gnu::unused]] f {\npublic:\n  f() {}\n}");
9865 
9866   // Make sure we do not mistake attributes for array subscripts.
9867   verifyFormat("int a() {}\n"
9868                "[[unused]] int b() {}\n");
9869   verifyFormat("NSArray *arr;\n"
9870                "arr[[Foo() bar]];");
9871 
9872   // On the other hand, we still need to correctly find array subscripts.
9873   verifyFormat("int a = std::vector<int>{1, 2, 3}[0];");
9874 
9875   // Make sure that we do not mistake Objective-C method inside array literals
9876   // as attributes, even if those method names are also keywords.
9877   verifyFormat("@[ [foo bar] ];");
9878   verifyFormat("@[ [NSArray class] ];");
9879   verifyFormat("@[ [foo enum] ];");
9880 
9881   verifyFormat("template <typename T> [[nodiscard]] int a() { return 1; }");
9882 
9883   // Make sure we do not parse attributes as lambda introducers.
9884   FormatStyle MultiLineFunctions = getLLVMStyle();
9885   MultiLineFunctions.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
9886   verifyFormat("[[unused]] int b() {\n"
9887                "  return 42;\n"
9888                "}\n",
9889                MultiLineFunctions);
9890 }
9891 
9892 TEST_F(FormatTest, AttributeClass) {
9893   FormatStyle Style = getChromiumStyle(FormatStyle::LK_Cpp);
9894   verifyFormat("class S {\n"
9895                "  S(S&&) = default;\n"
9896                "};",
9897                Style);
9898   verifyFormat("class [[nodiscard]] S {\n"
9899                "  S(S&&) = default;\n"
9900                "};",
9901                Style);
9902   verifyFormat("class __attribute((maybeunused)) S {\n"
9903                "  S(S&&) = default;\n"
9904                "};",
9905                Style);
9906   verifyFormat("struct S {\n"
9907                "  S(S&&) = default;\n"
9908                "};",
9909                Style);
9910   verifyFormat("struct [[nodiscard]] S {\n"
9911                "  S(S&&) = default;\n"
9912                "};",
9913                Style);
9914 }
9915 
9916 TEST_F(FormatTest, AttributesAfterMacro) {
9917   FormatStyle Style = getLLVMStyle();
9918   verifyFormat("MACRO;\n"
9919                "__attribute__((maybe_unused)) int foo() {\n"
9920                "  //...\n"
9921                "}");
9922 
9923   verifyFormat("MACRO;\n"
9924                "[[nodiscard]] int foo() {\n"
9925                "  //...\n"
9926                "}");
9927 
9928   EXPECT_EQ("MACRO\n\n"
9929             "__attribute__((maybe_unused)) int foo() {\n"
9930             "  //...\n"
9931             "}",
9932             format("MACRO\n\n"
9933                    "__attribute__((maybe_unused)) int foo() {\n"
9934                    "  //...\n"
9935                    "}"));
9936 
9937   EXPECT_EQ("MACRO\n\n"
9938             "[[nodiscard]] int foo() {\n"
9939             "  //...\n"
9940             "}",
9941             format("MACRO\n\n"
9942                    "[[nodiscard]] int foo() {\n"
9943                    "  //...\n"
9944                    "}"));
9945 }
9946 
9947 TEST_F(FormatTest, AttributePenaltyBreaking) {
9948   FormatStyle Style = getLLVMStyle();
9949   verifyFormat("void ABCDEFGH::ABCDEFGHIJKLMN(\n"
9950                "    [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}",
9951                Style);
9952   verifyFormat("void ABCDEFGH::ABCDEFGHIJK(\n"
9953                "    [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}",
9954                Style);
9955   verifyFormat("void ABCDEFGH::ABCDEFGH([[maybe_unused]] const "
9956                "shared_ptr<ALongTypeName> &C d) {\n}",
9957                Style);
9958 }
9959 
9960 TEST_F(FormatTest, UnderstandsEllipsis) {
9961   FormatStyle Style = getLLVMStyle();
9962   verifyFormat("int printf(const char *fmt, ...);");
9963   verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
9964   verifyFormat("template <class... Ts> void Foo(Ts *...ts) {}");
9965 
9966   verifyFormat("template <int *...PP> a;", Style);
9967 
9968   Style.PointerAlignment = FormatStyle::PAS_Left;
9969   verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", Style);
9970 
9971   verifyFormat("template <int*... PP> a;", Style);
9972 
9973   Style.PointerAlignment = FormatStyle::PAS_Middle;
9974   verifyFormat("template <int *... PP> a;", Style);
9975 }
9976 
9977 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
9978   EXPECT_EQ("int *a;\n"
9979             "int *a;\n"
9980             "int *a;",
9981             format("int *a;\n"
9982                    "int* a;\n"
9983                    "int *a;",
9984                    getGoogleStyle()));
9985   EXPECT_EQ("int* a;\n"
9986             "int* a;\n"
9987             "int* a;",
9988             format("int* a;\n"
9989                    "int* a;\n"
9990                    "int *a;",
9991                    getGoogleStyle()));
9992   EXPECT_EQ("int *a;\n"
9993             "int *a;\n"
9994             "int *a;",
9995             format("int *a;\n"
9996                    "int * a;\n"
9997                    "int *  a;",
9998                    getGoogleStyle()));
9999   EXPECT_EQ("auto x = [] {\n"
10000             "  int *a;\n"
10001             "  int *a;\n"
10002             "  int *a;\n"
10003             "};",
10004             format("auto x=[]{int *a;\n"
10005                    "int * a;\n"
10006                    "int *  a;};",
10007                    getGoogleStyle()));
10008 }
10009 
10010 TEST_F(FormatTest, UnderstandsRvalueReferences) {
10011   verifyFormat("int f(int &&a) {}");
10012   verifyFormat("int f(int a, char &&b) {}");
10013   verifyFormat("void f() { int &&a = b; }");
10014   verifyGoogleFormat("int f(int a, char&& b) {}");
10015   verifyGoogleFormat("void f() { int&& a = b; }");
10016 
10017   verifyIndependentOfContext("A<int &&> a;");
10018   verifyIndependentOfContext("A<int &&, int &&> a;");
10019   verifyGoogleFormat("A<int&&> a;");
10020   verifyGoogleFormat("A<int&&, int&&> a;");
10021 
10022   // Not rvalue references:
10023   verifyFormat("template <bool B, bool C> class A {\n"
10024                "  static_assert(B && C, \"Something is wrong\");\n"
10025                "};");
10026   verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
10027   verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
10028   verifyFormat("#define A(a, b) (a && b)");
10029 }
10030 
10031 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
10032   verifyFormat("void f() {\n"
10033                "  x[aaaaaaaaa -\n"
10034                "    b] = 23;\n"
10035                "}",
10036                getLLVMStyleWithColumns(15));
10037 }
10038 
10039 TEST_F(FormatTest, FormatsCasts) {
10040   verifyFormat("Type *A = static_cast<Type *>(P);");
10041   verifyFormat("Type *A = (Type *)P;");
10042   verifyFormat("Type *A = (vector<Type *, int *>)P;");
10043   verifyFormat("int a = (int)(2.0f);");
10044   verifyFormat("int a = (int)2.0f;");
10045   verifyFormat("x[(int32)y];");
10046   verifyFormat("x = (int32)y;");
10047   verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
10048   verifyFormat("int a = (int)*b;");
10049   verifyFormat("int a = (int)2.0f;");
10050   verifyFormat("int a = (int)~0;");
10051   verifyFormat("int a = (int)++a;");
10052   verifyFormat("int a = (int)sizeof(int);");
10053   verifyFormat("int a = (int)+2;");
10054   verifyFormat("my_int a = (my_int)2.0f;");
10055   verifyFormat("my_int a = (my_int)sizeof(int);");
10056   verifyFormat("return (my_int)aaa;");
10057   verifyFormat("#define x ((int)-1)");
10058   verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
10059   verifyFormat("#define p(q) ((int *)&q)");
10060   verifyFormat("fn(a)(b) + 1;");
10061 
10062   verifyFormat("void f() { my_int a = (my_int)*b; }");
10063   verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
10064   verifyFormat("my_int a = (my_int)~0;");
10065   verifyFormat("my_int a = (my_int)++a;");
10066   verifyFormat("my_int a = (my_int)-2;");
10067   verifyFormat("my_int a = (my_int)1;");
10068   verifyFormat("my_int a = (my_int *)1;");
10069   verifyFormat("my_int a = (const my_int)-1;");
10070   verifyFormat("my_int a = (const my_int *)-1;");
10071   verifyFormat("my_int a = (my_int)(my_int)-1;");
10072   verifyFormat("my_int a = (ns::my_int)-2;");
10073   verifyFormat("case (my_int)ONE:");
10074   verifyFormat("auto x = (X)this;");
10075   // Casts in Obj-C style calls used to not be recognized as such.
10076   verifyFormat("int a = [(type*)[((type*)val) arg] arg];", getGoogleStyle());
10077 
10078   // FIXME: single value wrapped with paren will be treated as cast.
10079   verifyFormat("void f(int i = (kValue)*kMask) {}");
10080 
10081   verifyFormat("{ (void)F; }");
10082 
10083   // Don't break after a cast's
10084   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
10085                "    (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
10086                "                                   bbbbbbbbbbbbbbbbbbbbbb);");
10087 
10088   // These are not casts.
10089   verifyFormat("void f(int *) {}");
10090   verifyFormat("f(foo)->b;");
10091   verifyFormat("f(foo).b;");
10092   verifyFormat("f(foo)(b);");
10093   verifyFormat("f(foo)[b];");
10094   verifyFormat("[](foo) { return 4; }(bar);");
10095   verifyFormat("(*funptr)(foo)[4];");
10096   verifyFormat("funptrs[4](foo)[4];");
10097   verifyFormat("void f(int *);");
10098   verifyFormat("void f(int *) = 0;");
10099   verifyFormat("void f(SmallVector<int>) {}");
10100   verifyFormat("void f(SmallVector<int>);");
10101   verifyFormat("void f(SmallVector<int>) = 0;");
10102   verifyFormat("void f(int i = (kA * kB) & kMask) {}");
10103   verifyFormat("int a = sizeof(int) * b;");
10104   verifyFormat("int a = alignof(int) * b;", getGoogleStyle());
10105   verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
10106   verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
10107   verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
10108 
10109   // These are not casts, but at some point were confused with casts.
10110   verifyFormat("virtual void foo(int *) override;");
10111   verifyFormat("virtual void foo(char &) const;");
10112   verifyFormat("virtual void foo(int *a, char *) const;");
10113   verifyFormat("int a = sizeof(int *) + b;");
10114   verifyFormat("int a = alignof(int *) + b;", getGoogleStyle());
10115   verifyFormat("bool b = f(g<int>) && c;");
10116   verifyFormat("typedef void (*f)(int i) func;");
10117   verifyFormat("void operator++(int) noexcept;");
10118   verifyFormat("void operator++(int &) noexcept;");
10119   verifyFormat("void operator delete(void *, std::size_t, const std::nothrow_t "
10120                "&) noexcept;");
10121   verifyFormat(
10122       "void operator delete(std::size_t, const std::nothrow_t &) noexcept;");
10123   verifyFormat("void operator delete(const std::nothrow_t &) noexcept;");
10124   verifyFormat("void operator delete(std::nothrow_t &) noexcept;");
10125   verifyFormat("void operator delete(nothrow_t &) noexcept;");
10126   verifyFormat("void operator delete(foo &) noexcept;");
10127   verifyFormat("void operator delete(foo) noexcept;");
10128   verifyFormat("void operator delete(int) noexcept;");
10129   verifyFormat("void operator delete(int &) noexcept;");
10130   verifyFormat("void operator delete(int &) volatile noexcept;");
10131   verifyFormat("void operator delete(int &) const");
10132   verifyFormat("void operator delete(int &) = default");
10133   verifyFormat("void operator delete(int &) = delete");
10134   verifyFormat("void operator delete(int &) [[noreturn]]");
10135   verifyFormat("void operator delete(int &) throw();");
10136   verifyFormat("void operator delete(int &) throw(int);");
10137   verifyFormat("auto operator delete(int &) -> int;");
10138   verifyFormat("auto operator delete(int &) override");
10139   verifyFormat("auto operator delete(int &) final");
10140 
10141   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
10142                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
10143   // FIXME: The indentation here is not ideal.
10144   verifyFormat(
10145       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10146       "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
10147       "        [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
10148 }
10149 
10150 TEST_F(FormatTest, FormatsFunctionTypes) {
10151   verifyFormat("A<bool()> a;");
10152   verifyFormat("A<SomeType()> a;");
10153   verifyFormat("A<void (*)(int, std::string)> a;");
10154   verifyFormat("A<void *(int)>;");
10155   verifyFormat("void *(*a)(int *, SomeType *);");
10156   verifyFormat("int (*func)(void *);");
10157   verifyFormat("void f() { int (*func)(void *); }");
10158   verifyFormat("template <class CallbackClass>\n"
10159                "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
10160 
10161   verifyGoogleFormat("A<void*(int*, SomeType*)>;");
10162   verifyGoogleFormat("void* (*a)(int);");
10163   verifyGoogleFormat(
10164       "template <class CallbackClass>\n"
10165       "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
10166 
10167   // Other constructs can look somewhat like function types:
10168   verifyFormat("A<sizeof(*x)> a;");
10169   verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
10170   verifyFormat("some_var = function(*some_pointer_var)[0];");
10171   verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
10172   verifyFormat("int x = f(&h)();");
10173   verifyFormat("returnsFunction(&param1, &param2)(param);");
10174   verifyFormat("std::function<\n"
10175                "    LooooooooooongTemplatedType<\n"
10176                "        SomeType>*(\n"
10177                "        LooooooooooooooooongType type)>\n"
10178                "    function;",
10179                getGoogleStyleWithColumns(40));
10180 }
10181 
10182 TEST_F(FormatTest, FormatsPointersToArrayTypes) {
10183   verifyFormat("A (*foo_)[6];");
10184   verifyFormat("vector<int> (*foo_)[6];");
10185 }
10186 
10187 TEST_F(FormatTest, BreaksLongVariableDeclarations) {
10188   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
10189                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
10190   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
10191                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
10192   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
10193                "    *LoooooooooooooooooooooooooooooooooooooooongVariable;");
10194 
10195   // Different ways of ()-initializiation.
10196   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
10197                "    LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
10198   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
10199                "    LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
10200   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
10201                "    LoooooooooooooooooooooooooooooooooooooooongVariable({});");
10202   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
10203                "    LoooooooooooooooooooooooooooooooooooooongVariable([A a]);");
10204 
10205   // Lambdas should not confuse the variable declaration heuristic.
10206   verifyFormat("LooooooooooooooooongType\n"
10207                "    variable(nullptr, [](A *a) {});",
10208                getLLVMStyleWithColumns(40));
10209 }
10210 
10211 TEST_F(FormatTest, BreaksLongDeclarations) {
10212   verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
10213                "    AnotherNameForTheLongType;");
10214   verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
10215                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10216   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10217                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
10218   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n"
10219                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
10220   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10221                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10222   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
10223                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10224   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
10225                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10226   verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
10227                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10228   verifyFormat("typeof(LoooooooooooooooooooooooooooooooooooooooooongName)\n"
10229                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10230   verifyFormat("_Atomic(LooooooooooooooooooooooooooooooooooooooooongName)\n"
10231                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10232   verifyFormat("__underlying_type(LooooooooooooooooooooooooooooooongName)\n"
10233                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10234   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10235                "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);");
10236   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10237                "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}");
10238   FormatStyle Indented = getLLVMStyle();
10239   Indented.IndentWrappedFunctionNames = true;
10240   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10241                "    LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
10242                Indented);
10243   verifyFormat(
10244       "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10245       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
10246       Indented);
10247   verifyFormat(
10248       "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
10249       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
10250       Indented);
10251   verifyFormat(
10252       "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
10253       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
10254       Indented);
10255 
10256   // FIXME: Without the comment, this breaks after "(".
10257   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType  // break\n"
10258                "    (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();",
10259                getGoogleStyle());
10260 
10261   verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
10262                "                  int LoooooooooooooooooooongParam2) {}");
10263   verifyFormat(
10264       "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
10265       "                                   SourceLocation L, IdentifierIn *II,\n"
10266       "                                   Type *T) {}");
10267   verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
10268                "ReallyReaaallyLongFunctionName(\n"
10269                "    const std::string &SomeParameter,\n"
10270                "    const SomeType<string, SomeOtherTemplateParameter>\n"
10271                "        &ReallyReallyLongParameterName,\n"
10272                "    const SomeType<string, SomeOtherTemplateParameter>\n"
10273                "        &AnotherLongParameterName) {}");
10274   verifyFormat("template <typename A>\n"
10275                "SomeLoooooooooooooooooooooongType<\n"
10276                "    typename some_namespace::SomeOtherType<A>::Type>\n"
10277                "Function() {}");
10278 
10279   verifyGoogleFormat(
10280       "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
10281       "    aaaaaaaaaaaaaaaaaaaaaaa;");
10282   verifyGoogleFormat(
10283       "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
10284       "                                   SourceLocation L) {}");
10285   verifyGoogleFormat(
10286       "some_namespace::LongReturnType\n"
10287       "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
10288       "    int first_long_parameter, int second_parameter) {}");
10289 
10290   verifyGoogleFormat("template <typename T>\n"
10291                      "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
10292                      "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
10293   verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10294                      "                   int aaaaaaaaaaaaaaaaaaaaaaa);");
10295 
10296   verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
10297                "    const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10298                "        *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
10299   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10300                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
10301                "        aaaaaaaaaaaaaaaaaaaaaaaa);");
10302   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10303                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
10304                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n"
10305                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
10306 
10307   verifyFormat("template <typename T> // Templates on own line.\n"
10308                "static int            // Some comment.\n"
10309                "MyFunction(int a);",
10310                getLLVMStyle());
10311 }
10312 
10313 TEST_F(FormatTest, FormatsAccessModifiers) {
10314   FormatStyle Style = getLLVMStyle();
10315   EXPECT_EQ(Style.EmptyLineBeforeAccessModifier,
10316             FormatStyle::ELBAMS_LogicalBlock);
10317   verifyFormat("struct foo {\n"
10318                "private:\n"
10319                "  void f() {}\n"
10320                "\n"
10321                "private:\n"
10322                "  int i;\n"
10323                "\n"
10324                "protected:\n"
10325                "  int j;\n"
10326                "};\n",
10327                Style);
10328   verifyFormat("struct foo {\n"
10329                "private:\n"
10330                "  void f() {}\n"
10331                "\n"
10332                "private:\n"
10333                "  int i;\n"
10334                "\n"
10335                "protected:\n"
10336                "  int j;\n"
10337                "};\n",
10338                "struct foo {\n"
10339                "private:\n"
10340                "  void f() {}\n"
10341                "private:\n"
10342                "  int i;\n"
10343                "protected:\n"
10344                "  int j;\n"
10345                "};\n",
10346                Style);
10347   verifyFormat("struct foo { /* comment */\n"
10348                "private:\n"
10349                "  int i;\n"
10350                "  // comment\n"
10351                "private:\n"
10352                "  int j;\n"
10353                "};\n",
10354                Style);
10355   verifyFormat("struct foo {\n"
10356                "#ifdef FOO\n"
10357                "#endif\n"
10358                "private:\n"
10359                "  int i;\n"
10360                "#ifdef FOO\n"
10361                "private:\n"
10362                "#endif\n"
10363                "  int j;\n"
10364                "};\n",
10365                Style);
10366   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
10367   verifyFormat("struct foo {\n"
10368                "private:\n"
10369                "  void f() {}\n"
10370                "private:\n"
10371                "  int i;\n"
10372                "protected:\n"
10373                "  int j;\n"
10374                "};\n",
10375                Style);
10376   verifyFormat("struct foo {\n"
10377                "private:\n"
10378                "  void f() {}\n"
10379                "private:\n"
10380                "  int i;\n"
10381                "protected:\n"
10382                "  int j;\n"
10383                "};\n",
10384                "struct foo {\n"
10385                "\n"
10386                "private:\n"
10387                "  void f() {}\n"
10388                "\n"
10389                "private:\n"
10390                "  int i;\n"
10391                "\n"
10392                "protected:\n"
10393                "  int j;\n"
10394                "};\n",
10395                Style);
10396   verifyFormat("struct foo { /* comment */\n"
10397                "private:\n"
10398                "  int i;\n"
10399                "  // comment\n"
10400                "private:\n"
10401                "  int j;\n"
10402                "};\n",
10403                "struct foo { /* comment */\n"
10404                "\n"
10405                "private:\n"
10406                "  int i;\n"
10407                "  // comment\n"
10408                "\n"
10409                "private:\n"
10410                "  int j;\n"
10411                "};\n",
10412                Style);
10413   verifyFormat("struct foo {\n"
10414                "#ifdef FOO\n"
10415                "#endif\n"
10416                "private:\n"
10417                "  int i;\n"
10418                "#ifdef FOO\n"
10419                "private:\n"
10420                "#endif\n"
10421                "  int j;\n"
10422                "};\n",
10423                "struct foo {\n"
10424                "#ifdef FOO\n"
10425                "#endif\n"
10426                "\n"
10427                "private:\n"
10428                "  int i;\n"
10429                "#ifdef FOO\n"
10430                "\n"
10431                "private:\n"
10432                "#endif\n"
10433                "  int j;\n"
10434                "};\n",
10435                Style);
10436   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
10437   verifyFormat("struct foo {\n"
10438                "private:\n"
10439                "  void f() {}\n"
10440                "\n"
10441                "private:\n"
10442                "  int i;\n"
10443                "\n"
10444                "protected:\n"
10445                "  int j;\n"
10446                "};\n",
10447                Style);
10448   verifyFormat("struct foo {\n"
10449                "private:\n"
10450                "  void f() {}\n"
10451                "\n"
10452                "private:\n"
10453                "  int i;\n"
10454                "\n"
10455                "protected:\n"
10456                "  int j;\n"
10457                "};\n",
10458                "struct foo {\n"
10459                "private:\n"
10460                "  void f() {}\n"
10461                "private:\n"
10462                "  int i;\n"
10463                "protected:\n"
10464                "  int j;\n"
10465                "};\n",
10466                Style);
10467   verifyFormat("struct foo { /* comment */\n"
10468                "private:\n"
10469                "  int i;\n"
10470                "  // comment\n"
10471                "\n"
10472                "private:\n"
10473                "  int j;\n"
10474                "};\n",
10475                "struct foo { /* comment */\n"
10476                "private:\n"
10477                "  int i;\n"
10478                "  // comment\n"
10479                "\n"
10480                "private:\n"
10481                "  int j;\n"
10482                "};\n",
10483                Style);
10484   verifyFormat("struct foo {\n"
10485                "#ifdef FOO\n"
10486                "#endif\n"
10487                "\n"
10488                "private:\n"
10489                "  int i;\n"
10490                "#ifdef FOO\n"
10491                "\n"
10492                "private:\n"
10493                "#endif\n"
10494                "  int j;\n"
10495                "};\n",
10496                "struct foo {\n"
10497                "#ifdef FOO\n"
10498                "#endif\n"
10499                "private:\n"
10500                "  int i;\n"
10501                "#ifdef FOO\n"
10502                "private:\n"
10503                "#endif\n"
10504                "  int j;\n"
10505                "};\n",
10506                Style);
10507   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
10508   EXPECT_EQ("struct foo {\n"
10509             "\n"
10510             "private:\n"
10511             "  void f() {}\n"
10512             "\n"
10513             "private:\n"
10514             "  int i;\n"
10515             "\n"
10516             "protected:\n"
10517             "  int j;\n"
10518             "};\n",
10519             format("struct foo {\n"
10520                    "\n"
10521                    "private:\n"
10522                    "  void f() {}\n"
10523                    "\n"
10524                    "private:\n"
10525                    "  int i;\n"
10526                    "\n"
10527                    "protected:\n"
10528                    "  int j;\n"
10529                    "};\n",
10530                    Style));
10531   verifyFormat("struct foo {\n"
10532                "private:\n"
10533                "  void f() {}\n"
10534                "private:\n"
10535                "  int i;\n"
10536                "protected:\n"
10537                "  int j;\n"
10538                "};\n",
10539                Style);
10540   EXPECT_EQ("struct foo { /* comment */\n"
10541             "\n"
10542             "private:\n"
10543             "  int i;\n"
10544             "  // comment\n"
10545             "\n"
10546             "private:\n"
10547             "  int j;\n"
10548             "};\n",
10549             format("struct foo { /* comment */\n"
10550                    "\n"
10551                    "private:\n"
10552                    "  int i;\n"
10553                    "  // comment\n"
10554                    "\n"
10555                    "private:\n"
10556                    "  int j;\n"
10557                    "};\n",
10558                    Style));
10559   verifyFormat("struct foo { /* comment */\n"
10560                "private:\n"
10561                "  int i;\n"
10562                "  // comment\n"
10563                "private:\n"
10564                "  int j;\n"
10565                "};\n",
10566                Style);
10567   EXPECT_EQ("struct foo {\n"
10568             "#ifdef FOO\n"
10569             "#endif\n"
10570             "\n"
10571             "private:\n"
10572             "  int i;\n"
10573             "#ifdef FOO\n"
10574             "\n"
10575             "private:\n"
10576             "#endif\n"
10577             "  int j;\n"
10578             "};\n",
10579             format("struct foo {\n"
10580                    "#ifdef FOO\n"
10581                    "#endif\n"
10582                    "\n"
10583                    "private:\n"
10584                    "  int i;\n"
10585                    "#ifdef FOO\n"
10586                    "\n"
10587                    "private:\n"
10588                    "#endif\n"
10589                    "  int j;\n"
10590                    "};\n",
10591                    Style));
10592   verifyFormat("struct foo {\n"
10593                "#ifdef FOO\n"
10594                "#endif\n"
10595                "private:\n"
10596                "  int i;\n"
10597                "#ifdef FOO\n"
10598                "private:\n"
10599                "#endif\n"
10600                "  int j;\n"
10601                "};\n",
10602                Style);
10603 
10604   FormatStyle NoEmptyLines = getLLVMStyle();
10605   NoEmptyLines.MaxEmptyLinesToKeep = 0;
10606   verifyFormat("struct foo {\n"
10607                "private:\n"
10608                "  void f() {}\n"
10609                "\n"
10610                "private:\n"
10611                "  int i;\n"
10612                "\n"
10613                "public:\n"
10614                "protected:\n"
10615                "  int j;\n"
10616                "};\n",
10617                NoEmptyLines);
10618 
10619   NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
10620   verifyFormat("struct foo {\n"
10621                "private:\n"
10622                "  void f() {}\n"
10623                "private:\n"
10624                "  int i;\n"
10625                "public:\n"
10626                "protected:\n"
10627                "  int j;\n"
10628                "};\n",
10629                NoEmptyLines);
10630 
10631   NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
10632   verifyFormat("struct foo {\n"
10633                "private:\n"
10634                "  void f() {}\n"
10635                "\n"
10636                "private:\n"
10637                "  int i;\n"
10638                "\n"
10639                "public:\n"
10640                "\n"
10641                "protected:\n"
10642                "  int j;\n"
10643                "};\n",
10644                NoEmptyLines);
10645 }
10646 
10647 TEST_F(FormatTest, FormatsAfterAccessModifiers) {
10648 
10649   FormatStyle Style = getLLVMStyle();
10650   EXPECT_EQ(Style.EmptyLineAfterAccessModifier, FormatStyle::ELAAMS_Never);
10651   verifyFormat("struct foo {\n"
10652                "private:\n"
10653                "  void f() {}\n"
10654                "\n"
10655                "private:\n"
10656                "  int i;\n"
10657                "\n"
10658                "protected:\n"
10659                "  int j;\n"
10660                "};\n",
10661                Style);
10662 
10663   // Check if lines are removed.
10664   verifyFormat("struct foo {\n"
10665                "private:\n"
10666                "  void f() {}\n"
10667                "\n"
10668                "private:\n"
10669                "  int i;\n"
10670                "\n"
10671                "protected:\n"
10672                "  int j;\n"
10673                "};\n",
10674                "struct foo {\n"
10675                "private:\n"
10676                "\n"
10677                "  void f() {}\n"
10678                "\n"
10679                "private:\n"
10680                "\n"
10681                "  int i;\n"
10682                "\n"
10683                "protected:\n"
10684                "\n"
10685                "  int j;\n"
10686                "};\n",
10687                Style);
10688 
10689   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
10690   verifyFormat("struct foo {\n"
10691                "private:\n"
10692                "\n"
10693                "  void f() {}\n"
10694                "\n"
10695                "private:\n"
10696                "\n"
10697                "  int i;\n"
10698                "\n"
10699                "protected:\n"
10700                "\n"
10701                "  int j;\n"
10702                "};\n",
10703                Style);
10704 
10705   // Check if lines are added.
10706   verifyFormat("struct foo {\n"
10707                "private:\n"
10708                "\n"
10709                "  void f() {}\n"
10710                "\n"
10711                "private:\n"
10712                "\n"
10713                "  int i;\n"
10714                "\n"
10715                "protected:\n"
10716                "\n"
10717                "  int j;\n"
10718                "};\n",
10719                "struct foo {\n"
10720                "private:\n"
10721                "  void f() {}\n"
10722                "\n"
10723                "private:\n"
10724                "  int i;\n"
10725                "\n"
10726                "protected:\n"
10727                "  int j;\n"
10728                "};\n",
10729                Style);
10730 
10731   // Leave tests rely on the code layout, test::messUp can not be used.
10732   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
10733   Style.MaxEmptyLinesToKeep = 0u;
10734   verifyFormat("struct foo {\n"
10735                "private:\n"
10736                "  void f() {}\n"
10737                "\n"
10738                "private:\n"
10739                "  int i;\n"
10740                "\n"
10741                "protected:\n"
10742                "  int j;\n"
10743                "};\n",
10744                Style);
10745 
10746   // Check if MaxEmptyLinesToKeep is respected.
10747   EXPECT_EQ("struct foo {\n"
10748             "private:\n"
10749             "  void f() {}\n"
10750             "\n"
10751             "private:\n"
10752             "  int i;\n"
10753             "\n"
10754             "protected:\n"
10755             "  int j;\n"
10756             "};\n",
10757             format("struct foo {\n"
10758                    "private:\n"
10759                    "\n\n\n"
10760                    "  void f() {}\n"
10761                    "\n"
10762                    "private:\n"
10763                    "\n\n\n"
10764                    "  int i;\n"
10765                    "\n"
10766                    "protected:\n"
10767                    "\n\n\n"
10768                    "  int j;\n"
10769                    "};\n",
10770                    Style));
10771 
10772   Style.MaxEmptyLinesToKeep = 1u;
10773   EXPECT_EQ("struct foo {\n"
10774             "private:\n"
10775             "\n"
10776             "  void f() {}\n"
10777             "\n"
10778             "private:\n"
10779             "\n"
10780             "  int i;\n"
10781             "\n"
10782             "protected:\n"
10783             "\n"
10784             "  int j;\n"
10785             "};\n",
10786             format("struct foo {\n"
10787                    "private:\n"
10788                    "\n"
10789                    "  void f() {}\n"
10790                    "\n"
10791                    "private:\n"
10792                    "\n"
10793                    "  int i;\n"
10794                    "\n"
10795                    "protected:\n"
10796                    "\n"
10797                    "  int j;\n"
10798                    "};\n",
10799                    Style));
10800   // Check if no lines are kept.
10801   EXPECT_EQ("struct foo {\n"
10802             "private:\n"
10803             "  void f() {}\n"
10804             "\n"
10805             "private:\n"
10806             "  int i;\n"
10807             "\n"
10808             "protected:\n"
10809             "  int j;\n"
10810             "};\n",
10811             format("struct foo {\n"
10812                    "private:\n"
10813                    "  void f() {}\n"
10814                    "\n"
10815                    "private:\n"
10816                    "  int i;\n"
10817                    "\n"
10818                    "protected:\n"
10819                    "  int j;\n"
10820                    "};\n",
10821                    Style));
10822   // Check if MaxEmptyLinesToKeep is respected.
10823   EXPECT_EQ("struct foo {\n"
10824             "private:\n"
10825             "\n"
10826             "  void f() {}\n"
10827             "\n"
10828             "private:\n"
10829             "\n"
10830             "  int i;\n"
10831             "\n"
10832             "protected:\n"
10833             "\n"
10834             "  int j;\n"
10835             "};\n",
10836             format("struct foo {\n"
10837                    "private:\n"
10838                    "\n\n\n"
10839                    "  void f() {}\n"
10840                    "\n"
10841                    "private:\n"
10842                    "\n\n\n"
10843                    "  int i;\n"
10844                    "\n"
10845                    "protected:\n"
10846                    "\n\n\n"
10847                    "  int j;\n"
10848                    "};\n",
10849                    Style));
10850 
10851   Style.MaxEmptyLinesToKeep = 10u;
10852   EXPECT_EQ("struct foo {\n"
10853             "private:\n"
10854             "\n\n\n"
10855             "  void f() {}\n"
10856             "\n"
10857             "private:\n"
10858             "\n\n\n"
10859             "  int i;\n"
10860             "\n"
10861             "protected:\n"
10862             "\n\n\n"
10863             "  int j;\n"
10864             "};\n",
10865             format("struct foo {\n"
10866                    "private:\n"
10867                    "\n\n\n"
10868                    "  void f() {}\n"
10869                    "\n"
10870                    "private:\n"
10871                    "\n\n\n"
10872                    "  int i;\n"
10873                    "\n"
10874                    "protected:\n"
10875                    "\n\n\n"
10876                    "  int j;\n"
10877                    "};\n",
10878                    Style));
10879 
10880   // Test with comments.
10881   Style = getLLVMStyle();
10882   verifyFormat("struct foo {\n"
10883                "private:\n"
10884                "  // comment\n"
10885                "  void f() {}\n"
10886                "\n"
10887                "private: /* comment */\n"
10888                "  int i;\n"
10889                "};\n",
10890                Style);
10891   verifyFormat("struct foo {\n"
10892                "private:\n"
10893                "  // comment\n"
10894                "  void f() {}\n"
10895                "\n"
10896                "private: /* comment */\n"
10897                "  int i;\n"
10898                "};\n",
10899                "struct foo {\n"
10900                "private:\n"
10901                "\n"
10902                "  // comment\n"
10903                "  void f() {}\n"
10904                "\n"
10905                "private: /* comment */\n"
10906                "\n"
10907                "  int i;\n"
10908                "};\n",
10909                Style);
10910 
10911   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
10912   verifyFormat("struct foo {\n"
10913                "private:\n"
10914                "\n"
10915                "  // comment\n"
10916                "  void f() {}\n"
10917                "\n"
10918                "private: /* comment */\n"
10919                "\n"
10920                "  int i;\n"
10921                "};\n",
10922                "struct foo {\n"
10923                "private:\n"
10924                "  // comment\n"
10925                "  void f() {}\n"
10926                "\n"
10927                "private: /* comment */\n"
10928                "  int i;\n"
10929                "};\n",
10930                Style);
10931   verifyFormat("struct foo {\n"
10932                "private:\n"
10933                "\n"
10934                "  // comment\n"
10935                "  void f() {}\n"
10936                "\n"
10937                "private: /* comment */\n"
10938                "\n"
10939                "  int i;\n"
10940                "};\n",
10941                Style);
10942 
10943   // Test with preprocessor defines.
10944   Style = getLLVMStyle();
10945   verifyFormat("struct foo {\n"
10946                "private:\n"
10947                "#ifdef FOO\n"
10948                "#endif\n"
10949                "  void f() {}\n"
10950                "};\n",
10951                Style);
10952   verifyFormat("struct foo {\n"
10953                "private:\n"
10954                "#ifdef FOO\n"
10955                "#endif\n"
10956                "  void f() {}\n"
10957                "};\n",
10958                "struct foo {\n"
10959                "private:\n"
10960                "\n"
10961                "#ifdef FOO\n"
10962                "#endif\n"
10963                "  void f() {}\n"
10964                "};\n",
10965                Style);
10966 
10967   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
10968   verifyFormat("struct foo {\n"
10969                "private:\n"
10970                "\n"
10971                "#ifdef FOO\n"
10972                "#endif\n"
10973                "  void f() {}\n"
10974                "};\n",
10975                "struct foo {\n"
10976                "private:\n"
10977                "#ifdef FOO\n"
10978                "#endif\n"
10979                "  void f() {}\n"
10980                "};\n",
10981                Style);
10982   verifyFormat("struct foo {\n"
10983                "private:\n"
10984                "\n"
10985                "#ifdef FOO\n"
10986                "#endif\n"
10987                "  void f() {}\n"
10988                "};\n",
10989                Style);
10990 }
10991 
10992 TEST_F(FormatTest, FormatsAfterAndBeforeAccessModifiersInteraction) {
10993   // Combined tests of EmptyLineAfterAccessModifier and
10994   // EmptyLineBeforeAccessModifier.
10995   FormatStyle Style = getLLVMStyle();
10996   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
10997   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
10998   verifyFormat("struct foo {\n"
10999                "private:\n"
11000                "\n"
11001                "protected:\n"
11002                "};\n",
11003                Style);
11004 
11005   Style.MaxEmptyLinesToKeep = 10u;
11006   // Both remove all new lines.
11007   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
11008   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
11009   verifyFormat("struct foo {\n"
11010                "private:\n"
11011                "protected:\n"
11012                "};\n",
11013                "struct foo {\n"
11014                "private:\n"
11015                "\n\n\n"
11016                "protected:\n"
11017                "};\n",
11018                Style);
11019 
11020   // Leave tests rely on the code layout, test::messUp can not be used.
11021   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
11022   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
11023   Style.MaxEmptyLinesToKeep = 10u;
11024   EXPECT_EQ("struct foo {\n"
11025             "private:\n"
11026             "\n\n\n"
11027             "protected:\n"
11028             "};\n",
11029             format("struct foo {\n"
11030                    "private:\n"
11031                    "\n\n\n"
11032                    "protected:\n"
11033                    "};\n",
11034                    Style));
11035   Style.MaxEmptyLinesToKeep = 3u;
11036   EXPECT_EQ("struct foo {\n"
11037             "private:\n"
11038             "\n\n\n"
11039             "protected:\n"
11040             "};\n",
11041             format("struct foo {\n"
11042                    "private:\n"
11043                    "\n\n\n"
11044                    "protected:\n"
11045                    "};\n",
11046                    Style));
11047   Style.MaxEmptyLinesToKeep = 1u;
11048   EXPECT_EQ("struct foo {\n"
11049             "private:\n"
11050             "\n\n\n"
11051             "protected:\n"
11052             "};\n",
11053             format("struct foo {\n"
11054                    "private:\n"
11055                    "\n\n\n"
11056                    "protected:\n"
11057                    "};\n",
11058                    Style)); // Based on new lines in original document and not
11059                             // on the setting.
11060 
11061   Style.MaxEmptyLinesToKeep = 10u;
11062   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
11063   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
11064   // Newlines are kept if they are greater than zero,
11065   // test::messUp removes all new lines which changes the logic
11066   EXPECT_EQ("struct foo {\n"
11067             "private:\n"
11068             "\n\n\n"
11069             "protected:\n"
11070             "};\n",
11071             format("struct foo {\n"
11072                    "private:\n"
11073                    "\n\n\n"
11074                    "protected:\n"
11075                    "};\n",
11076                    Style));
11077 
11078   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
11079   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
11080   // test::messUp removes all new lines which changes the logic
11081   EXPECT_EQ("struct foo {\n"
11082             "private:\n"
11083             "\n\n\n"
11084             "protected:\n"
11085             "};\n",
11086             format("struct foo {\n"
11087                    "private:\n"
11088                    "\n\n\n"
11089                    "protected:\n"
11090                    "};\n",
11091                    Style));
11092 
11093   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
11094   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
11095   EXPECT_EQ("struct foo {\n"
11096             "private:\n"
11097             "\n\n\n"
11098             "protected:\n"
11099             "};\n",
11100             format("struct foo {\n"
11101                    "private:\n"
11102                    "\n\n\n"
11103                    "protected:\n"
11104                    "};\n",
11105                    Style)); // test::messUp removes all new lines which changes
11106                             // the logic.
11107 
11108   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
11109   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
11110   verifyFormat("struct foo {\n"
11111                "private:\n"
11112                "protected:\n"
11113                "};\n",
11114                "struct foo {\n"
11115                "private:\n"
11116                "\n\n\n"
11117                "protected:\n"
11118                "};\n",
11119                Style);
11120 
11121   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
11122   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
11123   EXPECT_EQ("struct foo {\n"
11124             "private:\n"
11125             "\n\n\n"
11126             "protected:\n"
11127             "};\n",
11128             format("struct foo {\n"
11129                    "private:\n"
11130                    "\n\n\n"
11131                    "protected:\n"
11132                    "};\n",
11133                    Style)); // test::messUp removes all new lines which changes
11134                             // the logic.
11135 
11136   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
11137   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
11138   verifyFormat("struct foo {\n"
11139                "private:\n"
11140                "protected:\n"
11141                "};\n",
11142                "struct foo {\n"
11143                "private:\n"
11144                "\n\n\n"
11145                "protected:\n"
11146                "};\n",
11147                Style);
11148 
11149   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
11150   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
11151   verifyFormat("struct foo {\n"
11152                "private:\n"
11153                "protected:\n"
11154                "};\n",
11155                "struct foo {\n"
11156                "private:\n"
11157                "\n\n\n"
11158                "protected:\n"
11159                "};\n",
11160                Style);
11161 
11162   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
11163   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
11164   verifyFormat("struct foo {\n"
11165                "private:\n"
11166                "protected:\n"
11167                "};\n",
11168                "struct foo {\n"
11169                "private:\n"
11170                "\n\n\n"
11171                "protected:\n"
11172                "};\n",
11173                Style);
11174 
11175   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
11176   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
11177   verifyFormat("struct foo {\n"
11178                "private:\n"
11179                "protected:\n"
11180                "};\n",
11181                "struct foo {\n"
11182                "private:\n"
11183                "\n\n\n"
11184                "protected:\n"
11185                "};\n",
11186                Style);
11187 }
11188 
11189 TEST_F(FormatTest, FormatsArrays) {
11190   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
11191                "                         [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
11192   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n"
11193                "                         [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;");
11194   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n"
11195                "    aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}");
11196   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
11197                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
11198   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
11199                "    [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
11200   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
11201                "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
11202                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
11203   verifyFormat(
11204       "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
11205       "             << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
11206       "                                  [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
11207   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n"
11208                "    .aaaaaaaaaaaaaaaaaaaaaa();");
11209 
11210   verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
11211                      "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
11212   verifyFormat(
11213       "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
11214       "                                  .aaaaaaa[0]\n"
11215       "                                  .aaaaaaaaaaaaaaaaaaaaaa();");
11216   verifyFormat("a[::b::c];");
11217 
11218   verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10));
11219 
11220   FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0);
11221   verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit);
11222 }
11223 
11224 TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
11225   verifyFormat("(a)->b();");
11226   verifyFormat("--a;");
11227 }
11228 
11229 TEST_F(FormatTest, HandlesIncludeDirectives) {
11230   verifyFormat("#include <string>\n"
11231                "#include <a/b/c.h>\n"
11232                "#include \"a/b/string\"\n"
11233                "#include \"string.h\"\n"
11234                "#include \"string.h\"\n"
11235                "#include <a-a>\n"
11236                "#include < path with space >\n"
11237                "#include_next <test.h>"
11238                "#include \"abc.h\" // this is included for ABC\n"
11239                "#include \"some long include\" // with a comment\n"
11240                "#include \"some very long include path\"\n"
11241                "#include <some/very/long/include/path>\n",
11242                getLLVMStyleWithColumns(35));
11243   EXPECT_EQ("#include \"a.h\"", format("#include  \"a.h\""));
11244   EXPECT_EQ("#include <a>", format("#include<a>"));
11245 
11246   verifyFormat("#import <string>");
11247   verifyFormat("#import <a/b/c.h>");
11248   verifyFormat("#import \"a/b/string\"");
11249   verifyFormat("#import \"string.h\"");
11250   verifyFormat("#import \"string.h\"");
11251   verifyFormat("#if __has_include(<strstream>)\n"
11252                "#include <strstream>\n"
11253                "#endif");
11254 
11255   verifyFormat("#define MY_IMPORT <a/b>");
11256 
11257   verifyFormat("#if __has_include(<a/b>)");
11258   verifyFormat("#if __has_include_next(<a/b>)");
11259   verifyFormat("#define F __has_include(<a/b>)");
11260   verifyFormat("#define F __has_include_next(<a/b>)");
11261 
11262   // Protocol buffer definition or missing "#".
11263   verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
11264                getLLVMStyleWithColumns(30));
11265 
11266   FormatStyle Style = getLLVMStyle();
11267   Style.AlwaysBreakBeforeMultilineStrings = true;
11268   Style.ColumnLimit = 0;
11269   verifyFormat("#import \"abc.h\"", Style);
11270 
11271   // But 'import' might also be a regular C++ namespace.
11272   verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
11273                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
11274 }
11275 
11276 //===----------------------------------------------------------------------===//
11277 // Error recovery tests.
11278 //===----------------------------------------------------------------------===//
11279 
11280 TEST_F(FormatTest, IncompleteParameterLists) {
11281   FormatStyle NoBinPacking = getLLVMStyle();
11282   NoBinPacking.BinPackParameters = false;
11283   verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
11284                "                        double *min_x,\n"
11285                "                        double *max_x,\n"
11286                "                        double *min_y,\n"
11287                "                        double *max_y,\n"
11288                "                        double *min_z,\n"
11289                "                        double *max_z, ) {}",
11290                NoBinPacking);
11291 }
11292 
11293 TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
11294   verifyFormat("void f() { return; }\n42");
11295   verifyFormat("void f() {\n"
11296                "  if (0)\n"
11297                "    return;\n"
11298                "}\n"
11299                "42");
11300   verifyFormat("void f() { return }\n42");
11301   verifyFormat("void f() {\n"
11302                "  if (0)\n"
11303                "    return\n"
11304                "}\n"
11305                "42");
11306 }
11307 
11308 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
11309   EXPECT_EQ("void f() { return }", format("void  f ( )  {  return  }"));
11310   EXPECT_EQ("void f() {\n"
11311             "  if (a)\n"
11312             "    return\n"
11313             "}",
11314             format("void  f  (  )  {  if  ( a )  return  }"));
11315   EXPECT_EQ("namespace N {\n"
11316             "void f()\n"
11317             "}",
11318             format("namespace  N  {  void f()  }"));
11319   EXPECT_EQ("namespace N {\n"
11320             "void f() {}\n"
11321             "void g()\n"
11322             "} // namespace N",
11323             format("namespace N  { void f( ) { } void g( ) }"));
11324 }
11325 
11326 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
11327   verifyFormat("int aaaaaaaa =\n"
11328                "    // Overlylongcomment\n"
11329                "    b;",
11330                getLLVMStyleWithColumns(20));
11331   verifyFormat("function(\n"
11332                "    ShortArgument,\n"
11333                "    LoooooooooooongArgument);\n",
11334                getLLVMStyleWithColumns(20));
11335 }
11336 
11337 TEST_F(FormatTest, IncorrectAccessSpecifier) {
11338   verifyFormat("public:");
11339   verifyFormat("class A {\n"
11340                "public\n"
11341                "  void f() {}\n"
11342                "};");
11343   verifyFormat("public\n"
11344                "int qwerty;");
11345   verifyFormat("public\n"
11346                "B {}");
11347   verifyFormat("public\n"
11348                "{}");
11349   verifyFormat("public\n"
11350                "B { int x; }");
11351 }
11352 
11353 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
11354   verifyFormat("{");
11355   verifyFormat("#})");
11356   verifyNoCrash("(/**/[:!] ?[).");
11357 }
11358 
11359 TEST_F(FormatTest, IncorrectUnbalancedBracesInMacrosWithUnicode) {
11360   // Found by oss-fuzz:
11361   // https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=8212
11362   FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp);
11363   Style.ColumnLimit = 60;
11364   verifyNoCrash(
11365       "\x23\x47\xff\x20\x28\xff\x3c\xff\x3f\xff\x20\x2f\x7b\x7a\xff\x20"
11366       "\xff\xff\xff\xca\xb5\xff\xff\xff\xff\x3a\x7b\x7d\xff\x20\xff\x20"
11367       "\xff\x74\xff\x20\x7d\x7d\xff\x7b\x3a\xff\x20\x71\xff\x20\xff\x0a",
11368       Style);
11369 }
11370 
11371 TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
11372   verifyFormat("do {\n}");
11373   verifyFormat("do {\n}\n"
11374                "f();");
11375   verifyFormat("do {\n}\n"
11376                "wheeee(fun);");
11377   verifyFormat("do {\n"
11378                "  f();\n"
11379                "}");
11380 }
11381 
11382 TEST_F(FormatTest, IncorrectCodeMissingParens) {
11383   verifyFormat("if {\n  foo;\n  foo();\n}");
11384   verifyFormat("switch {\n  foo;\n  foo();\n}");
11385   verifyIncompleteFormat("for {\n  foo;\n  foo();\n}");
11386   verifyFormat("while {\n  foo;\n  foo();\n}");
11387   verifyFormat("do {\n  foo;\n  foo();\n} while;");
11388 }
11389 
11390 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
11391   verifyIncompleteFormat("namespace {\n"
11392                          "class Foo { Foo (\n"
11393                          "};\n"
11394                          "} // namespace");
11395 }
11396 
11397 TEST_F(FormatTest, IncorrectCodeErrorDetection) {
11398   EXPECT_EQ("{\n  {}\n", format("{\n{\n}\n"));
11399   EXPECT_EQ("{\n  {}\n", format("{\n  {\n}\n"));
11400   EXPECT_EQ("{\n  {}\n", format("{\n  {\n  }\n"));
11401   EXPECT_EQ("{\n  {}\n}\n}\n", format("{\n  {\n    }\n  }\n}\n"));
11402 
11403   EXPECT_EQ("{\n"
11404             "  {\n"
11405             "    breakme(\n"
11406             "        qwe);\n"
11407             "  }\n",
11408             format("{\n"
11409                    "    {\n"
11410                    " breakme(qwe);\n"
11411                    "}\n",
11412                    getLLVMStyleWithColumns(10)));
11413 }
11414 
11415 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
11416   verifyFormat("int x = {\n"
11417                "    avariable,\n"
11418                "    b(alongervariable)};",
11419                getLLVMStyleWithColumns(25));
11420 }
11421 
11422 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
11423   verifyFormat("return (a)(b){1, 2, 3};");
11424 }
11425 
11426 TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
11427   verifyFormat("vector<int> x{1, 2, 3, 4};");
11428   verifyFormat("vector<int> x{\n"
11429                "    1,\n"
11430                "    2,\n"
11431                "    3,\n"
11432                "    4,\n"
11433                "};");
11434   verifyFormat("vector<T> x{{}, {}, {}, {}};");
11435   verifyFormat("f({1, 2});");
11436   verifyFormat("auto v = Foo{-1};");
11437   verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
11438   verifyFormat("Class::Class : member{1, 2, 3} {}");
11439   verifyFormat("new vector<int>{1, 2, 3};");
11440   verifyFormat("new int[3]{1, 2, 3};");
11441   verifyFormat("new int{1};");
11442   verifyFormat("return {arg1, arg2};");
11443   verifyFormat("return {arg1, SomeType{parameter}};");
11444   verifyFormat("int count = set<int>{f(), g(), h()}.size();");
11445   verifyFormat("new T{arg1, arg2};");
11446   verifyFormat("f(MyMap[{composite, key}]);");
11447   verifyFormat("class Class {\n"
11448                "  T member = {arg1, arg2};\n"
11449                "};");
11450   verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
11451   verifyFormat("const struct A a = {.a = 1, .b = 2};");
11452   verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
11453   verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
11454   verifyFormat("int a = std::is_integral<int>{} + 0;");
11455 
11456   verifyFormat("int foo(int i) { return fo1{}(i); }");
11457   verifyFormat("int foo(int i) { return fo1{}(i); }");
11458   verifyFormat("auto i = decltype(x){};");
11459   verifyFormat("auto i = typeof(x){};");
11460   verifyFormat("auto i = _Atomic(x){};");
11461   verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
11462   verifyFormat("Node n{1, Node{1000}, //\n"
11463                "       2};");
11464   verifyFormat("Aaaa aaaaaaa{\n"
11465                "    {\n"
11466                "        aaaa,\n"
11467                "    },\n"
11468                "};");
11469   verifyFormat("class C : public D {\n"
11470                "  SomeClass SC{2};\n"
11471                "};");
11472   verifyFormat("class C : public A {\n"
11473                "  class D : public B {\n"
11474                "    void f() { int i{2}; }\n"
11475                "  };\n"
11476                "};");
11477   verifyFormat("#define A {a, a},");
11478 
11479   // Avoid breaking between equal sign and opening brace
11480   FormatStyle AvoidBreakingFirstArgument = getLLVMStyle();
11481   AvoidBreakingFirstArgument.PenaltyBreakBeforeFirstCallParameter = 200;
11482   verifyFormat("const std::unordered_map<std::string, int> MyHashTable =\n"
11483                "    {{\"aaaaaaaaaaaaaaaaaaaaa\", 0},\n"
11484                "     {\"bbbbbbbbbbbbbbbbbbbbb\", 1},\n"
11485                "     {\"ccccccccccccccccccccc\", 2}};",
11486                AvoidBreakingFirstArgument);
11487 
11488   // Binpacking only if there is no trailing comma
11489   verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n"
11490                "                      cccccccccc, dddddddddd};",
11491                getLLVMStyleWithColumns(50));
11492   verifyFormat("const Aaaaaa aaaaa = {\n"
11493                "    aaaaaaaaaaa,\n"
11494                "    bbbbbbbbbbb,\n"
11495                "    ccccccccccc,\n"
11496                "    ddddddddddd,\n"
11497                "};",
11498                getLLVMStyleWithColumns(50));
11499 
11500   // Cases where distinguising braced lists and blocks is hard.
11501   verifyFormat("vector<int> v{12} GUARDED_BY(mutex);");
11502   verifyFormat("void f() {\n"
11503                "  return; // comment\n"
11504                "}\n"
11505                "SomeType t;");
11506   verifyFormat("void f() {\n"
11507                "  if (a) {\n"
11508                "    f();\n"
11509                "  }\n"
11510                "}\n"
11511                "SomeType t;");
11512 
11513   // In combination with BinPackArguments = false.
11514   FormatStyle NoBinPacking = getLLVMStyle();
11515   NoBinPacking.BinPackArguments = false;
11516   verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
11517                "                      bbbbb,\n"
11518                "                      ccccc,\n"
11519                "                      ddddd,\n"
11520                "                      eeeee,\n"
11521                "                      ffffff,\n"
11522                "                      ggggg,\n"
11523                "                      hhhhhh,\n"
11524                "                      iiiiii,\n"
11525                "                      jjjjjj,\n"
11526                "                      kkkkkk};",
11527                NoBinPacking);
11528   verifyFormat("const Aaaaaa aaaaa = {\n"
11529                "    aaaaa,\n"
11530                "    bbbbb,\n"
11531                "    ccccc,\n"
11532                "    ddddd,\n"
11533                "    eeeee,\n"
11534                "    ffffff,\n"
11535                "    ggggg,\n"
11536                "    hhhhhh,\n"
11537                "    iiiiii,\n"
11538                "    jjjjjj,\n"
11539                "    kkkkkk,\n"
11540                "};",
11541                NoBinPacking);
11542   verifyFormat(
11543       "const Aaaaaa aaaaa = {\n"
11544       "    aaaaa,  bbbbb,  ccccc,  ddddd,  eeeee,  ffffff, ggggg, hhhhhh,\n"
11545       "    iiiiii, jjjjjj, kkkkkk, aaaaa,  bbbbb,  ccccc,  ddddd, eeeee,\n"
11546       "    ffffff, ggggg,  hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
11547       "};",
11548       NoBinPacking);
11549 
11550   NoBinPacking.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
11551   EXPECT_EQ("static uint8 CddDp83848Reg[] = {\n"
11552             "    CDDDP83848_BMCR_REGISTER,\n"
11553             "    CDDDP83848_BMSR_REGISTER,\n"
11554             "    CDDDP83848_RBR_REGISTER};",
11555             format("static uint8 CddDp83848Reg[] = {CDDDP83848_BMCR_REGISTER,\n"
11556                    "                                CDDDP83848_BMSR_REGISTER,\n"
11557                    "                                CDDDP83848_RBR_REGISTER};",
11558                    NoBinPacking));
11559 
11560   // FIXME: The alignment of these trailing comments might be bad. Then again,
11561   // this might be utterly useless in real code.
11562   verifyFormat("Constructor::Constructor()\n"
11563                "    : some_value{         //\n"
11564                "                 aaaaaaa, //\n"
11565                "                 bbbbbbb} {}");
11566 
11567   // In braced lists, the first comment is always assumed to belong to the
11568   // first element. Thus, it can be moved to the next or previous line as
11569   // appropriate.
11570   EXPECT_EQ("function({// First element:\n"
11571             "          1,\n"
11572             "          // Second element:\n"
11573             "          2});",
11574             format("function({\n"
11575                    "    // First element:\n"
11576                    "    1,\n"
11577                    "    // Second element:\n"
11578                    "    2});"));
11579   EXPECT_EQ("std::vector<int> MyNumbers{\n"
11580             "    // First element:\n"
11581             "    1,\n"
11582             "    // Second element:\n"
11583             "    2};",
11584             format("std::vector<int> MyNumbers{// First element:\n"
11585                    "                           1,\n"
11586                    "                           // Second element:\n"
11587                    "                           2};",
11588                    getLLVMStyleWithColumns(30)));
11589   // A trailing comma should still lead to an enforced line break and no
11590   // binpacking.
11591   EXPECT_EQ("vector<int> SomeVector = {\n"
11592             "    // aaa\n"
11593             "    1,\n"
11594             "    2,\n"
11595             "};",
11596             format("vector<int> SomeVector = { // aaa\n"
11597                    "    1, 2, };"));
11598 
11599   // C++11 brace initializer list l-braces should not be treated any differently
11600   // when breaking before lambda bodies is enabled
11601   FormatStyle BreakBeforeLambdaBody = getLLVMStyle();
11602   BreakBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
11603   BreakBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
11604   BreakBeforeLambdaBody.AlwaysBreakBeforeMultilineStrings = true;
11605   verifyFormat(
11606       "std::runtime_error{\n"
11607       "    \"Long string which will force a break onto the next line...\"};",
11608       BreakBeforeLambdaBody);
11609 
11610   FormatStyle ExtraSpaces = getLLVMStyle();
11611   ExtraSpaces.Cpp11BracedListStyle = false;
11612   ExtraSpaces.ColumnLimit = 75;
11613   verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
11614   verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
11615   verifyFormat("f({ 1, 2 });", ExtraSpaces);
11616   verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
11617   verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
11618   verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
11619   verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
11620   verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
11621   verifyFormat("return { arg1, arg2 };", ExtraSpaces);
11622   verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
11623   verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
11624   verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
11625   verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
11626   verifyFormat("class Class {\n"
11627                "  T member = { arg1, arg2 };\n"
11628                "};",
11629                ExtraSpaces);
11630   verifyFormat(
11631       "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
11632       "                                 aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
11633       "                  : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
11634       "                                 bbbbbbbbbbbbbbbbbbbb, bbbbb };",
11635       ExtraSpaces);
11636   verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
11637   verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
11638                ExtraSpaces);
11639   verifyFormat(
11640       "someFunction(OtherParam,\n"
11641       "             BracedList{ // comment 1 (Forcing interesting break)\n"
11642       "                         param1, param2,\n"
11643       "                         // comment 2\n"
11644       "                         param3, param4 });",
11645       ExtraSpaces);
11646   verifyFormat(
11647       "std::this_thread::sleep_for(\n"
11648       "    std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
11649       ExtraSpaces);
11650   verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n"
11651                "    aaaaaaa,\n"
11652                "    aaaaaaaaaa,\n"
11653                "    aaaaa,\n"
11654                "    aaaaaaaaaaaaaaa,\n"
11655                "    aaa,\n"
11656                "    aaaaaaaaaa,\n"
11657                "    a,\n"
11658                "    aaaaaaaaaaaaaaaaaaaaa,\n"
11659                "    aaaaaaaaaaaa,\n"
11660                "    aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
11661                "    aaaaaaa,\n"
11662                "    a};");
11663   verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
11664   verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces);
11665   verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces);
11666 
11667   // Avoid breaking between initializer/equal sign and opening brace
11668   ExtraSpaces.PenaltyBreakBeforeFirstCallParameter = 200;
11669   verifyFormat("const std::unordered_map<std::string, int> MyHashTable = {\n"
11670                "  { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n"
11671                "  { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n"
11672                "  { \"ccccccccccccccccccccc\", 2 }\n"
11673                "};",
11674                ExtraSpaces);
11675   verifyFormat("const std::unordered_map<std::string, int> MyHashTable{\n"
11676                "  { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n"
11677                "  { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n"
11678                "  { \"ccccccccccccccccccccc\", 2 }\n"
11679                "};",
11680                ExtraSpaces);
11681 
11682   FormatStyle SpaceBeforeBrace = getLLVMStyle();
11683   SpaceBeforeBrace.SpaceBeforeCpp11BracedList = true;
11684   verifyFormat("vector<int> x {1, 2, 3, 4};", SpaceBeforeBrace);
11685   verifyFormat("f({}, {{}, {}}, MyMap[{k, v}]);", SpaceBeforeBrace);
11686 
11687   FormatStyle SpaceBetweenBraces = getLLVMStyle();
11688   SpaceBetweenBraces.SpacesInAngles = FormatStyle::SIAS_Always;
11689   SpaceBetweenBraces.SpacesInParentheses = true;
11690   SpaceBetweenBraces.SpacesInSquareBrackets = true;
11691   verifyFormat("vector< int > x{ 1, 2, 3, 4 };", SpaceBetweenBraces);
11692   verifyFormat("f( {}, { {}, {} }, MyMap[ { k, v } ] );", SpaceBetweenBraces);
11693   verifyFormat("vector< int > x{ // comment 1\n"
11694                "                 1, 2, 3, 4 };",
11695                SpaceBetweenBraces);
11696   SpaceBetweenBraces.ColumnLimit = 20;
11697   EXPECT_EQ("vector< int > x{\n"
11698             "    1, 2, 3, 4 };",
11699             format("vector<int>x{1,2,3,4};", SpaceBetweenBraces));
11700   SpaceBetweenBraces.ColumnLimit = 24;
11701   EXPECT_EQ("vector< int > x{ 1, 2,\n"
11702             "                 3, 4 };",
11703             format("vector<int>x{1,2,3,4};", SpaceBetweenBraces));
11704   EXPECT_EQ("vector< int > x{\n"
11705             "    1,\n"
11706             "    2,\n"
11707             "    3,\n"
11708             "    4,\n"
11709             "};",
11710             format("vector<int>x{1,2,3,4,};", SpaceBetweenBraces));
11711   verifyFormat("vector< int > x{};", SpaceBetweenBraces);
11712   SpaceBetweenBraces.SpaceInEmptyParentheses = true;
11713   verifyFormat("vector< int > x{ };", SpaceBetweenBraces);
11714 }
11715 
11716 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
11717   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11718                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11719                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11720                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11721                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11722                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
11723   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n"
11724                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11725                "                 1, 22, 333, 4444, 55555, //\n"
11726                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11727                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
11728   verifyFormat(
11729       "vector<int> x = {1,       22, 333, 4444, 55555, 666666, 7777777,\n"
11730       "                 1,       22, 333, 4444, 55555, 666666, 7777777,\n"
11731       "                 1,       22, 333, 4444, 55555, 666666, // comment\n"
11732       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
11733       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
11734       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
11735       "                 7777777};");
11736   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
11737                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
11738                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
11739   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
11740                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
11741                "    // Separating comment.\n"
11742                "    X86::R8, X86::R9, X86::R10, X86::R11, 0};");
11743   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
11744                "    // Leading comment\n"
11745                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
11746                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
11747   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
11748                "                 1, 1, 1, 1};",
11749                getLLVMStyleWithColumns(39));
11750   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
11751                "                 1, 1, 1, 1};",
11752                getLLVMStyleWithColumns(38));
11753   verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
11754                "    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
11755                getLLVMStyleWithColumns(43));
11756   verifyFormat(
11757       "static unsigned SomeValues[10][3] = {\n"
11758       "    {1, 4, 0},  {4, 9, 0},  {4, 5, 9},  {8, 5, 4}, {1, 8, 4},\n"
11759       "    {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};");
11760   verifyFormat("static auto fields = new vector<string>{\n"
11761                "    \"aaaaaaaaaaaaa\",\n"
11762                "    \"aaaaaaaaaaaaa\",\n"
11763                "    \"aaaaaaaaaaaa\",\n"
11764                "    \"aaaaaaaaaaaaaa\",\n"
11765                "    \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
11766                "    \"aaaaaaaaaaaa\",\n"
11767                "    \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
11768                "};");
11769   verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};");
11770   verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n"
11771                "                 2, bbbbbbbbbbbbbbbbbbbbbb,\n"
11772                "                 3, cccccccccccccccccccccc};",
11773                getLLVMStyleWithColumns(60));
11774 
11775   // Trailing commas.
11776   verifyFormat("vector<int> x = {\n"
11777                "    1, 1, 1, 1, 1, 1, 1, 1,\n"
11778                "};",
11779                getLLVMStyleWithColumns(39));
11780   verifyFormat("vector<int> x = {\n"
11781                "    1, 1, 1, 1, 1, 1, 1, 1, //\n"
11782                "};",
11783                getLLVMStyleWithColumns(39));
11784   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
11785                "                 1, 1, 1, 1,\n"
11786                "                 /**/ /**/};",
11787                getLLVMStyleWithColumns(39));
11788 
11789   // Trailing comment in the first line.
11790   verifyFormat("vector<int> iiiiiiiiiiiiiii = {                      //\n"
11791                "    1111111111, 2222222222, 33333333333, 4444444444, //\n"
11792                "    111111111,  222222222,  3333333333,  444444444,  //\n"
11793                "    11111111,   22222222,   333333333,   44444444};");
11794   // Trailing comment in the last line.
11795   verifyFormat("int aaaaa[] = {\n"
11796                "    1, 2, 3, // comment\n"
11797                "    4, 5, 6  // comment\n"
11798                "};");
11799 
11800   // With nested lists, we should either format one item per line or all nested
11801   // lists one on line.
11802   // FIXME: For some nested lists, we can do better.
11803   verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
11804                "        {aaaaaaaaaaaaaaaaaaa},\n"
11805                "        {aaaaaaaaaaaaaaaaaaaaa},\n"
11806                "        {aaaaaaaaaaaaaaaaa}};",
11807                getLLVMStyleWithColumns(60));
11808   verifyFormat(
11809       "SomeStruct my_struct_array = {\n"
11810       "    {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
11811       "     aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
11812       "    {aaa, aaa},\n"
11813       "    {aaa, aaa},\n"
11814       "    {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
11815       "    {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
11816       "     aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
11817 
11818   // No column layout should be used here.
11819   verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
11820                "                   bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
11821 
11822   verifyNoCrash("a<,");
11823 
11824   // No braced initializer here.
11825   verifyFormat("void f() {\n"
11826                "  struct Dummy {};\n"
11827                "  f(v);\n"
11828                "}");
11829 
11830   verifyFormat("void foo() {\n"
11831                "  { // asdf\n"
11832                "    { int a; }\n"
11833                "  }\n"
11834                "  {\n"
11835                "    { int b; }\n"
11836                "  }\n"
11837                "}");
11838   verifyFormat("namespace n {\n"
11839                "void foo() {\n"
11840                "  {\n"
11841                "    {\n"
11842                "      statement();\n"
11843                "      if (false) {\n"
11844                "      }\n"
11845                "    }\n"
11846                "  }\n"
11847                "  {}\n"
11848                "}\n"
11849                "} // namespace n");
11850 
11851   // Long lists should be formatted in columns even if they are nested.
11852   verifyFormat(
11853       "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11854       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11855       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11856       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11857       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11858       "                          1, 22, 333, 4444, 55555, 666666, 7777777});");
11859 
11860   // Allow "single-column" layout even if that violates the column limit. There
11861   // isn't going to be a better way.
11862   verifyFormat("std::vector<int> a = {\n"
11863                "    aaaaaaaa,\n"
11864                "    aaaaaaaa,\n"
11865                "    aaaaaaaa,\n"
11866                "    aaaaaaaa,\n"
11867                "    aaaaaaaaaa,\n"
11868                "    aaaaaaaa,\n"
11869                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa};",
11870                getLLVMStyleWithColumns(30));
11871   verifyFormat("vector<int> aaaa = {\n"
11872                "    aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
11873                "    aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
11874                "    aaaaaa.aaaaaaa,\n"
11875                "    aaaaaa.aaaaaaa,\n"
11876                "    aaaaaa.aaaaaaa,\n"
11877                "    aaaaaa.aaaaaaa,\n"
11878                "};");
11879 
11880   // Don't create hanging lists.
11881   verifyFormat("someFunction(Param, {List1, List2,\n"
11882                "                     List3});",
11883                getLLVMStyleWithColumns(35));
11884   verifyFormat("someFunction(Param, Param,\n"
11885                "             {List1, List2,\n"
11886                "              List3});",
11887                getLLVMStyleWithColumns(35));
11888   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n"
11889                "                               aaaaaaaaaaaaaaaaaaaaaaa);");
11890 }
11891 
11892 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
11893   FormatStyle DoNotMerge = getLLVMStyle();
11894   DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
11895 
11896   verifyFormat("void f() { return 42; }");
11897   verifyFormat("void f() {\n"
11898                "  return 42;\n"
11899                "}",
11900                DoNotMerge);
11901   verifyFormat("void f() {\n"
11902                "  // Comment\n"
11903                "}");
11904   verifyFormat("{\n"
11905                "#error {\n"
11906                "  int a;\n"
11907                "}");
11908   verifyFormat("{\n"
11909                "  int a;\n"
11910                "#error {\n"
11911                "}");
11912   verifyFormat("void f() {} // comment");
11913   verifyFormat("void f() { int a; } // comment");
11914   verifyFormat("void f() {\n"
11915                "} // comment",
11916                DoNotMerge);
11917   verifyFormat("void f() {\n"
11918                "  int a;\n"
11919                "} // comment",
11920                DoNotMerge);
11921   verifyFormat("void f() {\n"
11922                "} // comment",
11923                getLLVMStyleWithColumns(15));
11924 
11925   verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
11926   verifyFormat("void f() {\n  return 42;\n}", getLLVMStyleWithColumns(22));
11927 
11928   verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
11929   verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
11930   verifyFormat("class C {\n"
11931                "  C()\n"
11932                "      : iiiiiiii(nullptr),\n"
11933                "        kkkkkkk(nullptr),\n"
11934                "        mmmmmmm(nullptr),\n"
11935                "        nnnnnnn(nullptr) {}\n"
11936                "};",
11937                getGoogleStyle());
11938 
11939   FormatStyle NoColumnLimit = getLLVMStyle();
11940   NoColumnLimit.ColumnLimit = 0;
11941   EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit));
11942   EXPECT_EQ("class C {\n"
11943             "  A() : b(0) {}\n"
11944             "};",
11945             format("class C{A():b(0){}};", NoColumnLimit));
11946   EXPECT_EQ("A()\n"
11947             "    : b(0) {\n"
11948             "}",
11949             format("A()\n:b(0)\n{\n}", NoColumnLimit));
11950 
11951   FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
11952   DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
11953       FormatStyle::SFS_None;
11954   EXPECT_EQ("A()\n"
11955             "    : b(0) {\n"
11956             "}",
11957             format("A():b(0){}", DoNotMergeNoColumnLimit));
11958   EXPECT_EQ("A()\n"
11959             "    : b(0) {\n"
11960             "}",
11961             format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit));
11962 
11963   verifyFormat("#define A          \\\n"
11964                "  void f() {       \\\n"
11965                "    int i;         \\\n"
11966                "  }",
11967                getLLVMStyleWithColumns(20));
11968   verifyFormat("#define A           \\\n"
11969                "  void f() { int i; }",
11970                getLLVMStyleWithColumns(21));
11971   verifyFormat("#define A            \\\n"
11972                "  void f() {         \\\n"
11973                "    int i;           \\\n"
11974                "  }                  \\\n"
11975                "  int j;",
11976                getLLVMStyleWithColumns(22));
11977   verifyFormat("#define A             \\\n"
11978                "  void f() { int i; } \\\n"
11979                "  int j;",
11980                getLLVMStyleWithColumns(23));
11981 }
11982 
11983 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) {
11984   FormatStyle MergeEmptyOnly = getLLVMStyle();
11985   MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
11986   verifyFormat("class C {\n"
11987                "  int f() {}\n"
11988                "};",
11989                MergeEmptyOnly);
11990   verifyFormat("class C {\n"
11991                "  int f() {\n"
11992                "    return 42;\n"
11993                "  }\n"
11994                "};",
11995                MergeEmptyOnly);
11996   verifyFormat("int f() {}", MergeEmptyOnly);
11997   verifyFormat("int f() {\n"
11998                "  return 42;\n"
11999                "}",
12000                MergeEmptyOnly);
12001 
12002   // Also verify behavior when BraceWrapping.AfterFunction = true
12003   MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
12004   MergeEmptyOnly.BraceWrapping.AfterFunction = true;
12005   verifyFormat("int f() {}", MergeEmptyOnly);
12006   verifyFormat("class C {\n"
12007                "  int f() {}\n"
12008                "};",
12009                MergeEmptyOnly);
12010 }
12011 
12012 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
12013   FormatStyle MergeInlineOnly = getLLVMStyle();
12014   MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
12015   verifyFormat("class C {\n"
12016                "  int f() { return 42; }\n"
12017                "};",
12018                MergeInlineOnly);
12019   verifyFormat("int f() {\n"
12020                "  return 42;\n"
12021                "}",
12022                MergeInlineOnly);
12023 
12024   // SFS_Inline implies SFS_Empty
12025   verifyFormat("class C {\n"
12026                "  int f() {}\n"
12027                "};",
12028                MergeInlineOnly);
12029   verifyFormat("int f() {}", MergeInlineOnly);
12030 
12031   // Also verify behavior when BraceWrapping.AfterFunction = true
12032   MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
12033   MergeInlineOnly.BraceWrapping.AfterFunction = true;
12034   verifyFormat("class C {\n"
12035                "  int f() { return 42; }\n"
12036                "};",
12037                MergeInlineOnly);
12038   verifyFormat("int f()\n"
12039                "{\n"
12040                "  return 42;\n"
12041                "}",
12042                MergeInlineOnly);
12043 
12044   // SFS_Inline implies SFS_Empty
12045   verifyFormat("int f() {}", MergeInlineOnly);
12046   verifyFormat("class C {\n"
12047                "  int f() {}\n"
12048                "};",
12049                MergeInlineOnly);
12050 }
12051 
12052 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) {
12053   FormatStyle MergeInlineOnly = getLLVMStyle();
12054   MergeInlineOnly.AllowShortFunctionsOnASingleLine =
12055       FormatStyle::SFS_InlineOnly;
12056   verifyFormat("class C {\n"
12057                "  int f() { return 42; }\n"
12058                "};",
12059                MergeInlineOnly);
12060   verifyFormat("int f() {\n"
12061                "  return 42;\n"
12062                "}",
12063                MergeInlineOnly);
12064 
12065   // SFS_InlineOnly does not imply SFS_Empty
12066   verifyFormat("class C {\n"
12067                "  int f() {}\n"
12068                "};",
12069                MergeInlineOnly);
12070   verifyFormat("int f() {\n"
12071                "}",
12072                MergeInlineOnly);
12073 
12074   // Also verify behavior when BraceWrapping.AfterFunction = true
12075   MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
12076   MergeInlineOnly.BraceWrapping.AfterFunction = true;
12077   verifyFormat("class C {\n"
12078                "  int f() { return 42; }\n"
12079                "};",
12080                MergeInlineOnly);
12081   verifyFormat("int f()\n"
12082                "{\n"
12083                "  return 42;\n"
12084                "}",
12085                MergeInlineOnly);
12086 
12087   // SFS_InlineOnly does not imply SFS_Empty
12088   verifyFormat("int f()\n"
12089                "{\n"
12090                "}",
12091                MergeInlineOnly);
12092   verifyFormat("class C {\n"
12093                "  int f() {}\n"
12094                "};",
12095                MergeInlineOnly);
12096 }
12097 
12098 TEST_F(FormatTest, SplitEmptyFunction) {
12099   FormatStyle Style = getLLVMStyle();
12100   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
12101   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12102   Style.BraceWrapping.AfterFunction = true;
12103   Style.BraceWrapping.SplitEmptyFunction = false;
12104   Style.ColumnLimit = 40;
12105 
12106   verifyFormat("int f()\n"
12107                "{}",
12108                Style);
12109   verifyFormat("int f()\n"
12110                "{\n"
12111                "  return 42;\n"
12112                "}",
12113                Style);
12114   verifyFormat("int f()\n"
12115                "{\n"
12116                "  // some comment\n"
12117                "}",
12118                Style);
12119 
12120   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
12121   verifyFormat("int f() {}", Style);
12122   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
12123                "{}",
12124                Style);
12125   verifyFormat("int f()\n"
12126                "{\n"
12127                "  return 0;\n"
12128                "}",
12129                Style);
12130 
12131   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
12132   verifyFormat("class Foo {\n"
12133                "  int f() {}\n"
12134                "};\n",
12135                Style);
12136   verifyFormat("class Foo {\n"
12137                "  int f() { return 0; }\n"
12138                "};\n",
12139                Style);
12140   verifyFormat("class Foo {\n"
12141                "  int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
12142                "  {}\n"
12143                "};\n",
12144                Style);
12145   verifyFormat("class Foo {\n"
12146                "  int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
12147                "  {\n"
12148                "    return 0;\n"
12149                "  }\n"
12150                "};\n",
12151                Style);
12152 
12153   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
12154   verifyFormat("int f() {}", Style);
12155   verifyFormat("int f() { return 0; }", Style);
12156   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
12157                "{}",
12158                Style);
12159   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
12160                "{\n"
12161                "  return 0;\n"
12162                "}",
12163                Style);
12164 }
12165 TEST_F(FormatTest, KeepShortFunctionAfterPPElse) {
12166   FormatStyle Style = getLLVMStyle();
12167   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
12168   verifyFormat("#ifdef A\n"
12169                "int f() {}\n"
12170                "#else\n"
12171                "int g() {}\n"
12172                "#endif",
12173                Style);
12174 }
12175 
12176 TEST_F(FormatTest, SplitEmptyClass) {
12177   FormatStyle Style = getLLVMStyle();
12178   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12179   Style.BraceWrapping.AfterClass = true;
12180   Style.BraceWrapping.SplitEmptyRecord = false;
12181 
12182   verifyFormat("class Foo\n"
12183                "{};",
12184                Style);
12185   verifyFormat("/* something */ class Foo\n"
12186                "{};",
12187                Style);
12188   verifyFormat("template <typename X> class Foo\n"
12189                "{};",
12190                Style);
12191   verifyFormat("class Foo\n"
12192                "{\n"
12193                "  Foo();\n"
12194                "};",
12195                Style);
12196   verifyFormat("typedef class Foo\n"
12197                "{\n"
12198                "} Foo_t;",
12199                Style);
12200 
12201   Style.BraceWrapping.SplitEmptyRecord = true;
12202   Style.BraceWrapping.AfterStruct = true;
12203   verifyFormat("class rep\n"
12204                "{\n"
12205                "};",
12206                Style);
12207   verifyFormat("struct rep\n"
12208                "{\n"
12209                "};",
12210                Style);
12211   verifyFormat("template <typename T> class rep\n"
12212                "{\n"
12213                "};",
12214                Style);
12215   verifyFormat("template <typename T> struct rep\n"
12216                "{\n"
12217                "};",
12218                Style);
12219   verifyFormat("class rep\n"
12220                "{\n"
12221                "  int x;\n"
12222                "};",
12223                Style);
12224   verifyFormat("struct rep\n"
12225                "{\n"
12226                "  int x;\n"
12227                "};",
12228                Style);
12229   verifyFormat("template <typename T> class rep\n"
12230                "{\n"
12231                "  int x;\n"
12232                "};",
12233                Style);
12234   verifyFormat("template <typename T> struct rep\n"
12235                "{\n"
12236                "  int x;\n"
12237                "};",
12238                Style);
12239   verifyFormat("template <typename T> class rep // Foo\n"
12240                "{\n"
12241                "  int x;\n"
12242                "};",
12243                Style);
12244   verifyFormat("template <typename T> struct rep // Bar\n"
12245                "{\n"
12246                "  int x;\n"
12247                "};",
12248                Style);
12249 
12250   verifyFormat("template <typename T> class rep<T>\n"
12251                "{\n"
12252                "  int x;\n"
12253                "};",
12254                Style);
12255 
12256   verifyFormat("template <typename T> class rep<std::complex<T>>\n"
12257                "{\n"
12258                "  int x;\n"
12259                "};",
12260                Style);
12261   verifyFormat("template <typename T> class rep<std::complex<T>>\n"
12262                "{\n"
12263                "};",
12264                Style);
12265 
12266   verifyFormat("#include \"stdint.h\"\n"
12267                "namespace rep {}",
12268                Style);
12269   verifyFormat("#include <stdint.h>\n"
12270                "namespace rep {}",
12271                Style);
12272   verifyFormat("#include <stdint.h>\n"
12273                "namespace rep {}",
12274                "#include <stdint.h>\n"
12275                "namespace rep {\n"
12276                "\n"
12277                "\n"
12278                "}",
12279                Style);
12280 }
12281 
12282 TEST_F(FormatTest, SplitEmptyStruct) {
12283   FormatStyle Style = getLLVMStyle();
12284   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12285   Style.BraceWrapping.AfterStruct = true;
12286   Style.BraceWrapping.SplitEmptyRecord = false;
12287 
12288   verifyFormat("struct Foo\n"
12289                "{};",
12290                Style);
12291   verifyFormat("/* something */ struct Foo\n"
12292                "{};",
12293                Style);
12294   verifyFormat("template <typename X> struct Foo\n"
12295                "{};",
12296                Style);
12297   verifyFormat("struct Foo\n"
12298                "{\n"
12299                "  Foo();\n"
12300                "};",
12301                Style);
12302   verifyFormat("typedef struct Foo\n"
12303                "{\n"
12304                "} Foo_t;",
12305                Style);
12306   // typedef struct Bar {} Bar_t;
12307 }
12308 
12309 TEST_F(FormatTest, SplitEmptyUnion) {
12310   FormatStyle Style = getLLVMStyle();
12311   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12312   Style.BraceWrapping.AfterUnion = true;
12313   Style.BraceWrapping.SplitEmptyRecord = false;
12314 
12315   verifyFormat("union Foo\n"
12316                "{};",
12317                Style);
12318   verifyFormat("/* something */ union Foo\n"
12319                "{};",
12320                Style);
12321   verifyFormat("union Foo\n"
12322                "{\n"
12323                "  A,\n"
12324                "};",
12325                Style);
12326   verifyFormat("typedef union Foo\n"
12327                "{\n"
12328                "} Foo_t;",
12329                Style);
12330 }
12331 
12332 TEST_F(FormatTest, SplitEmptyNamespace) {
12333   FormatStyle Style = getLLVMStyle();
12334   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12335   Style.BraceWrapping.AfterNamespace = true;
12336   Style.BraceWrapping.SplitEmptyNamespace = false;
12337 
12338   verifyFormat("namespace Foo\n"
12339                "{};",
12340                Style);
12341   verifyFormat("/* something */ namespace Foo\n"
12342                "{};",
12343                Style);
12344   verifyFormat("inline namespace Foo\n"
12345                "{};",
12346                Style);
12347   verifyFormat("/* something */ inline namespace Foo\n"
12348                "{};",
12349                Style);
12350   verifyFormat("export namespace Foo\n"
12351                "{};",
12352                Style);
12353   verifyFormat("namespace Foo\n"
12354                "{\n"
12355                "void Bar();\n"
12356                "};",
12357                Style);
12358 }
12359 
12360 TEST_F(FormatTest, NeverMergeShortRecords) {
12361   FormatStyle Style = getLLVMStyle();
12362 
12363   verifyFormat("class Foo {\n"
12364                "  Foo();\n"
12365                "};",
12366                Style);
12367   verifyFormat("typedef class Foo {\n"
12368                "  Foo();\n"
12369                "} Foo_t;",
12370                Style);
12371   verifyFormat("struct Foo {\n"
12372                "  Foo();\n"
12373                "};",
12374                Style);
12375   verifyFormat("typedef struct Foo {\n"
12376                "  Foo();\n"
12377                "} Foo_t;",
12378                Style);
12379   verifyFormat("union Foo {\n"
12380                "  A,\n"
12381                "};",
12382                Style);
12383   verifyFormat("typedef union Foo {\n"
12384                "  A,\n"
12385                "} Foo_t;",
12386                Style);
12387   verifyFormat("namespace Foo {\n"
12388                "void Bar();\n"
12389                "};",
12390                Style);
12391 
12392   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12393   Style.BraceWrapping.AfterClass = true;
12394   Style.BraceWrapping.AfterStruct = true;
12395   Style.BraceWrapping.AfterUnion = true;
12396   Style.BraceWrapping.AfterNamespace = true;
12397   verifyFormat("class Foo\n"
12398                "{\n"
12399                "  Foo();\n"
12400                "};",
12401                Style);
12402   verifyFormat("typedef class Foo\n"
12403                "{\n"
12404                "  Foo();\n"
12405                "} Foo_t;",
12406                Style);
12407   verifyFormat("struct Foo\n"
12408                "{\n"
12409                "  Foo();\n"
12410                "};",
12411                Style);
12412   verifyFormat("typedef struct Foo\n"
12413                "{\n"
12414                "  Foo();\n"
12415                "} Foo_t;",
12416                Style);
12417   verifyFormat("union Foo\n"
12418                "{\n"
12419                "  A,\n"
12420                "};",
12421                Style);
12422   verifyFormat("typedef union Foo\n"
12423                "{\n"
12424                "  A,\n"
12425                "} Foo_t;",
12426                Style);
12427   verifyFormat("namespace Foo\n"
12428                "{\n"
12429                "void Bar();\n"
12430                "};",
12431                Style);
12432 }
12433 
12434 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
12435   // Elaborate type variable declarations.
12436   verifyFormat("struct foo a = {bar};\nint n;");
12437   verifyFormat("class foo a = {bar};\nint n;");
12438   verifyFormat("union foo a = {bar};\nint n;");
12439 
12440   // Elaborate types inside function definitions.
12441   verifyFormat("struct foo f() {}\nint n;");
12442   verifyFormat("class foo f() {}\nint n;");
12443   verifyFormat("union foo f() {}\nint n;");
12444 
12445   // Templates.
12446   verifyFormat("template <class X> void f() {}\nint n;");
12447   verifyFormat("template <struct X> void f() {}\nint n;");
12448   verifyFormat("template <union X> void f() {}\nint n;");
12449 
12450   // Actual definitions...
12451   verifyFormat("struct {\n} n;");
12452   verifyFormat(
12453       "template <template <class T, class Y>, class Z> class X {\n} n;");
12454   verifyFormat("union Z {\n  int n;\n} x;");
12455   verifyFormat("class MACRO Z {\n} n;");
12456   verifyFormat("class MACRO(X) Z {\n} n;");
12457   verifyFormat("class __attribute__(X) Z {\n} n;");
12458   verifyFormat("class __declspec(X) Z {\n} n;");
12459   verifyFormat("class A##B##C {\n} n;");
12460   verifyFormat("class alignas(16) Z {\n} n;");
12461   verifyFormat("class MACRO(X) alignas(16) Z {\n} n;");
12462   verifyFormat("class MACROA MACRO(X) Z {\n} n;");
12463 
12464   // Redefinition from nested context:
12465   verifyFormat("class A::B::C {\n} n;");
12466 
12467   // Template definitions.
12468   verifyFormat(
12469       "template <typename F>\n"
12470       "Matcher(const Matcher<F> &Other,\n"
12471       "        typename enable_if_c<is_base_of<F, T>::value &&\n"
12472       "                             !is_same<F, T>::value>::type * = 0)\n"
12473       "    : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
12474 
12475   // FIXME: This is still incorrectly handled at the formatter side.
12476   verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
12477   verifyFormat("int i = SomeFunction(a<b, a> b);");
12478 
12479   // FIXME:
12480   // This now gets parsed incorrectly as class definition.
12481   // verifyFormat("class A<int> f() {\n}\nint n;");
12482 
12483   // Elaborate types where incorrectly parsing the structural element would
12484   // break the indent.
12485   verifyFormat("if (true)\n"
12486                "  class X x;\n"
12487                "else\n"
12488                "  f();\n");
12489 
12490   // This is simply incomplete. Formatting is not important, but must not crash.
12491   verifyFormat("class A:");
12492 }
12493 
12494 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
12495   EXPECT_EQ("#error Leave     all         white!!!!! space* alone!\n",
12496             format("#error Leave     all         white!!!!! space* alone!\n"));
12497   EXPECT_EQ(
12498       "#warning Leave     all         white!!!!! space* alone!\n",
12499       format("#warning Leave     all         white!!!!! space* alone!\n"));
12500   EXPECT_EQ("#error 1", format("  #  error   1"));
12501   EXPECT_EQ("#warning 1", format("  #  warning 1"));
12502 }
12503 
12504 TEST_F(FormatTest, FormatHashIfExpressions) {
12505   verifyFormat("#if AAAA && BBBB");
12506   verifyFormat("#if (AAAA && BBBB)");
12507   verifyFormat("#elif (AAAA && BBBB)");
12508   // FIXME: Come up with a better indentation for #elif.
12509   verifyFormat(
12510       "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) &&  \\\n"
12511       "    defined(BBBBBBBB)\n"
12512       "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) &&  \\\n"
12513       "    defined(BBBBBBBB)\n"
12514       "#endif",
12515       getLLVMStyleWithColumns(65));
12516 }
12517 
12518 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
12519   FormatStyle AllowsMergedIf = getGoogleStyle();
12520   AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
12521       FormatStyle::SIS_WithoutElse;
12522   verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
12523   verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
12524   verifyFormat("if (true)\n#error E\n  return 42;", AllowsMergedIf);
12525   EXPECT_EQ("if (true) return 42;",
12526             format("if (true)\nreturn 42;", AllowsMergedIf));
12527   FormatStyle ShortMergedIf = AllowsMergedIf;
12528   ShortMergedIf.ColumnLimit = 25;
12529   verifyFormat("#define A \\\n"
12530                "  if (true) return 42;",
12531                ShortMergedIf);
12532   verifyFormat("#define A \\\n"
12533                "  f();    \\\n"
12534                "  if (true)\n"
12535                "#define B",
12536                ShortMergedIf);
12537   verifyFormat("#define A \\\n"
12538                "  f();    \\\n"
12539                "  if (true)\n"
12540                "g();",
12541                ShortMergedIf);
12542   verifyFormat("{\n"
12543                "#ifdef A\n"
12544                "  // Comment\n"
12545                "  if (true) continue;\n"
12546                "#endif\n"
12547                "  // Comment\n"
12548                "  if (true) continue;\n"
12549                "}",
12550                ShortMergedIf);
12551   ShortMergedIf.ColumnLimit = 33;
12552   verifyFormat("#define A \\\n"
12553                "  if constexpr (true) return 42;",
12554                ShortMergedIf);
12555   verifyFormat("#define A \\\n"
12556                "  if CONSTEXPR (true) return 42;",
12557                ShortMergedIf);
12558   ShortMergedIf.ColumnLimit = 29;
12559   verifyFormat("#define A                   \\\n"
12560                "  if (aaaaaaaaaa) return 1; \\\n"
12561                "  return 2;",
12562                ShortMergedIf);
12563   ShortMergedIf.ColumnLimit = 28;
12564   verifyFormat("#define A         \\\n"
12565                "  if (aaaaaaaaaa) \\\n"
12566                "    return 1;     \\\n"
12567                "  return 2;",
12568                ShortMergedIf);
12569   verifyFormat("#define A                \\\n"
12570                "  if constexpr (aaaaaaa) \\\n"
12571                "    return 1;            \\\n"
12572                "  return 2;",
12573                ShortMergedIf);
12574   verifyFormat("#define A                \\\n"
12575                "  if CONSTEXPR (aaaaaaa) \\\n"
12576                "    return 1;            \\\n"
12577                "  return 2;",
12578                ShortMergedIf);
12579 }
12580 
12581 TEST_F(FormatTest, FormatStarDependingOnContext) {
12582   verifyFormat("void f(int *a);");
12583   verifyFormat("void f() { f(fint * b); }");
12584   verifyFormat("class A {\n  void f(int *a);\n};");
12585   verifyFormat("class A {\n  int *a;\n};");
12586   verifyFormat("namespace a {\n"
12587                "namespace b {\n"
12588                "class A {\n"
12589                "  void f() {}\n"
12590                "  int *a;\n"
12591                "};\n"
12592                "} // namespace b\n"
12593                "} // namespace a");
12594 }
12595 
12596 TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
12597   verifyFormat("while");
12598   verifyFormat("operator");
12599 }
12600 
12601 TEST_F(FormatTest, SkipsDeeplyNestedLines) {
12602   // This code would be painfully slow to format if we didn't skip it.
12603   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
12604                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
12605                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
12606                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
12607                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
12608                    "A(1, 1)\n"
12609                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x
12610                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12611                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12612                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12613                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12614                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12615                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12616                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12617                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12618                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n");
12619   // Deeply nested part is untouched, rest is formatted.
12620   EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n",
12621             format(std::string("int    i;\n") + Code + "int    j;\n",
12622                    getLLVMStyle(), SC_ExpectIncomplete));
12623 }
12624 
12625 //===----------------------------------------------------------------------===//
12626 // Objective-C tests.
12627 //===----------------------------------------------------------------------===//
12628 
12629 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
12630   verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
12631   EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
12632             format("-(NSUInteger)indexOfObject:(id)anObject;"));
12633   EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
12634   EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
12635   EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
12636             format("-(NSInteger)Method3:(id)anObject;"));
12637   EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
12638             format("-(NSInteger)Method4:(id)anObject;"));
12639   EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
12640             format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
12641   EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
12642             format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
12643   EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject "
12644             "forAllCells:(BOOL)flag;",
12645             format("- (void)sendAction:(SEL)aSelector to:(id)anObject "
12646                    "forAllCells:(BOOL)flag;"));
12647 
12648   // Very long objectiveC method declaration.
12649   verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
12650                "    (SoooooooooooooooooooooomeType *)bbbbbbbbbb;");
12651   verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
12652                "                    inRange:(NSRange)range\n"
12653                "                   outRange:(NSRange)out_range\n"
12654                "                  outRange1:(NSRange)out_range1\n"
12655                "                  outRange2:(NSRange)out_range2\n"
12656                "                  outRange3:(NSRange)out_range3\n"
12657                "                  outRange4:(NSRange)out_range4\n"
12658                "                  outRange5:(NSRange)out_range5\n"
12659                "                  outRange6:(NSRange)out_range6\n"
12660                "                  outRange7:(NSRange)out_range7\n"
12661                "                  outRange8:(NSRange)out_range8\n"
12662                "                  outRange9:(NSRange)out_range9;");
12663 
12664   // When the function name has to be wrapped.
12665   FormatStyle Style = getLLVMStyle();
12666   // ObjC ignores IndentWrappedFunctionNames when wrapping methods
12667   // and always indents instead.
12668   Style.IndentWrappedFunctionNames = false;
12669   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
12670                "    veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
12671                "               anotherName:(NSString)bbbbbbbbbbbbbb {\n"
12672                "}",
12673                Style);
12674   Style.IndentWrappedFunctionNames = true;
12675   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
12676                "    veryLooooooooooongName:(NSString)cccccccccccccc\n"
12677                "               anotherName:(NSString)dddddddddddddd {\n"
12678                "}",
12679                Style);
12680 
12681   verifyFormat("- (int)sum:(vector<int>)numbers;");
12682   verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
12683   // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
12684   // protocol lists (but not for template classes):
12685   // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
12686 
12687   verifyFormat("- (int (*)())foo:(int (*)())f;");
12688   verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
12689 
12690   // If there's no return type (very rare in practice!), LLVM and Google style
12691   // agree.
12692   verifyFormat("- foo;");
12693   verifyFormat("- foo:(int)f;");
12694   verifyGoogleFormat("- foo:(int)foo;");
12695 }
12696 
12697 TEST_F(FormatTest, BreaksStringLiterals) {
12698   EXPECT_EQ("\"some text \"\n"
12699             "\"other\";",
12700             format("\"some text other\";", getLLVMStyleWithColumns(12)));
12701   EXPECT_EQ("\"some text \"\n"
12702             "\"other\";",
12703             format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
12704   EXPECT_EQ(
12705       "#define A  \\\n"
12706       "  \"some \"  \\\n"
12707       "  \"text \"  \\\n"
12708       "  \"other\";",
12709       format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
12710   EXPECT_EQ(
12711       "#define A  \\\n"
12712       "  \"so \"    \\\n"
12713       "  \"text \"  \\\n"
12714       "  \"other\";",
12715       format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
12716 
12717   EXPECT_EQ("\"some text\"",
12718             format("\"some text\"", getLLVMStyleWithColumns(1)));
12719   EXPECT_EQ("\"some text\"",
12720             format("\"some text\"", getLLVMStyleWithColumns(11)));
12721   EXPECT_EQ("\"some \"\n"
12722             "\"text\"",
12723             format("\"some text\"", getLLVMStyleWithColumns(10)));
12724   EXPECT_EQ("\"some \"\n"
12725             "\"text\"",
12726             format("\"some text\"", getLLVMStyleWithColumns(7)));
12727   EXPECT_EQ("\"some\"\n"
12728             "\" tex\"\n"
12729             "\"t\"",
12730             format("\"some text\"", getLLVMStyleWithColumns(6)));
12731   EXPECT_EQ("\"some\"\n"
12732             "\" tex\"\n"
12733             "\" and\"",
12734             format("\"some tex and\"", getLLVMStyleWithColumns(6)));
12735   EXPECT_EQ("\"some\"\n"
12736             "\"/tex\"\n"
12737             "\"/and\"",
12738             format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
12739 
12740   EXPECT_EQ("variable =\n"
12741             "    \"long string \"\n"
12742             "    \"literal\";",
12743             format("variable = \"long string literal\";",
12744                    getLLVMStyleWithColumns(20)));
12745 
12746   EXPECT_EQ("variable = f(\n"
12747             "    \"long string \"\n"
12748             "    \"literal\",\n"
12749             "    short,\n"
12750             "    loooooooooooooooooooong);",
12751             format("variable = f(\"long string literal\", short, "
12752                    "loooooooooooooooooooong);",
12753                    getLLVMStyleWithColumns(20)));
12754 
12755   EXPECT_EQ(
12756       "f(g(\"long string \"\n"
12757       "    \"literal\"),\n"
12758       "  b);",
12759       format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20)));
12760   EXPECT_EQ("f(g(\"long string \"\n"
12761             "    \"literal\",\n"
12762             "    a),\n"
12763             "  b);",
12764             format("f(g(\"long string literal\", a), b);",
12765                    getLLVMStyleWithColumns(20)));
12766   EXPECT_EQ(
12767       "f(\"one two\".split(\n"
12768       "    variable));",
12769       format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
12770   EXPECT_EQ("f(\"one two three four five six \"\n"
12771             "  \"seven\".split(\n"
12772             "      really_looooong_variable));",
12773             format("f(\"one two three four five six seven\"."
12774                    "split(really_looooong_variable));",
12775                    getLLVMStyleWithColumns(33)));
12776 
12777   EXPECT_EQ("f(\"some \"\n"
12778             "  \"text\",\n"
12779             "  other);",
12780             format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
12781 
12782   // Only break as a last resort.
12783   verifyFormat(
12784       "aaaaaaaaaaaaaaaaaaaa(\n"
12785       "    aaaaaaaaaaaaaaaaaaaa,\n"
12786       "    aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
12787 
12788   EXPECT_EQ("\"splitmea\"\n"
12789             "\"trandomp\"\n"
12790             "\"oint\"",
12791             format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
12792 
12793   EXPECT_EQ("\"split/\"\n"
12794             "\"pathat/\"\n"
12795             "\"slashes\"",
12796             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
12797 
12798   EXPECT_EQ("\"split/\"\n"
12799             "\"pathat/\"\n"
12800             "\"slashes\"",
12801             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
12802   EXPECT_EQ("\"split at \"\n"
12803             "\"spaces/at/\"\n"
12804             "\"slashes.at.any$\"\n"
12805             "\"non-alphanumeric%\"\n"
12806             "\"1111111111characte\"\n"
12807             "\"rs\"",
12808             format("\"split at "
12809                    "spaces/at/"
12810                    "slashes.at."
12811                    "any$non-"
12812                    "alphanumeric%"
12813                    "1111111111characte"
12814                    "rs\"",
12815                    getLLVMStyleWithColumns(20)));
12816 
12817   // Verify that splitting the strings understands
12818   // Style::AlwaysBreakBeforeMultilineStrings.
12819   EXPECT_EQ("aaaaaaaaaaaa(\n"
12820             "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
12821             "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
12822             format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa "
12823                    "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
12824                    "aaaaaaaaaaaaaaaaaaaaaa\");",
12825                    getGoogleStyle()));
12826   EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
12827             "       \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
12828             format("return \"aaaaaaaaaaaaaaaaaaaaaa "
12829                    "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
12830                    "aaaaaaaaaaaaaaaaaaaaaa\";",
12831                    getGoogleStyle()));
12832   EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
12833             "                \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
12834             format("llvm::outs() << "
12835                    "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
12836                    "aaaaaaaaaaaaaaaaaaa\";"));
12837   EXPECT_EQ("ffff(\n"
12838             "    {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
12839             "     \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
12840             format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
12841                    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
12842                    getGoogleStyle()));
12843 
12844   FormatStyle Style = getLLVMStyleWithColumns(12);
12845   Style.BreakStringLiterals = false;
12846   EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style));
12847 
12848   FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
12849   AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
12850   EXPECT_EQ("#define A \\\n"
12851             "  \"some \" \\\n"
12852             "  \"text \" \\\n"
12853             "  \"other\";",
12854             format("#define A \"some text other\";", AlignLeft));
12855 }
12856 
12857 TEST_F(FormatTest, BreaksStringLiteralsAtColumnLimit) {
12858   EXPECT_EQ("C a = \"some more \"\n"
12859             "      \"text\";",
12860             format("C a = \"some more text\";", getLLVMStyleWithColumns(18)));
12861 }
12862 
12863 TEST_F(FormatTest, FullyRemoveEmptyLines) {
12864   FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80);
12865   NoEmptyLines.MaxEmptyLinesToKeep = 0;
12866   EXPECT_EQ("int i = a(b());",
12867             format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines));
12868 }
12869 
12870 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
12871   EXPECT_EQ(
12872       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
12873       "(\n"
12874       "    \"x\t\");",
12875       format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
12876              "aaaaaaa("
12877              "\"x\t\");"));
12878 }
12879 
12880 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
12881   EXPECT_EQ(
12882       "u8\"utf8 string \"\n"
12883       "u8\"literal\";",
12884       format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
12885   EXPECT_EQ(
12886       "u\"utf16 string \"\n"
12887       "u\"literal\";",
12888       format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
12889   EXPECT_EQ(
12890       "U\"utf32 string \"\n"
12891       "U\"literal\";",
12892       format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
12893   EXPECT_EQ("L\"wide string \"\n"
12894             "L\"literal\";",
12895             format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
12896   EXPECT_EQ("@\"NSString \"\n"
12897             "@\"literal\";",
12898             format("@\"NSString literal\";", getGoogleStyleWithColumns(19)));
12899   verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26));
12900 
12901   // This input makes clang-format try to split the incomplete unicode escape
12902   // sequence, which used to lead to a crasher.
12903   verifyNoCrash(
12904       "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
12905       getLLVMStyleWithColumns(60));
12906 }
12907 
12908 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) {
12909   FormatStyle Style = getGoogleStyleWithColumns(15);
12910   EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style));
12911   EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style));
12912   EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style));
12913   EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style));
12914   EXPECT_EQ("u8R\"x(raw literal)x\";",
12915             format("u8R\"x(raw literal)x\";", Style));
12916 }
12917 
12918 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
12919   FormatStyle Style = getLLVMStyleWithColumns(20);
12920   EXPECT_EQ(
12921       "_T(\"aaaaaaaaaaaaaa\")\n"
12922       "_T(\"aaaaaaaaaaaaaa\")\n"
12923       "_T(\"aaaaaaaaaaaa\")",
12924       format("  _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
12925   EXPECT_EQ("f(x,\n"
12926             "  _T(\"aaaaaaaaaaaa\")\n"
12927             "  _T(\"aaa\"),\n"
12928             "  z);",
12929             format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style));
12930 
12931   // FIXME: Handle embedded spaces in one iteration.
12932   //  EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
12933   //            "_T(\"aaaaaaaaaaaaa\")\n"
12934   //            "_T(\"aaaaaaaaaaaaa\")\n"
12935   //            "_T(\"a\")",
12936   //            format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
12937   //                   getLLVMStyleWithColumns(20)));
12938   EXPECT_EQ(
12939       "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
12940       format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style));
12941   EXPECT_EQ("f(\n"
12942             "#if !TEST\n"
12943             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
12944             "#endif\n"
12945             ");",
12946             format("f(\n"
12947                    "#if !TEST\n"
12948                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
12949                    "#endif\n"
12950                    ");"));
12951   EXPECT_EQ("f(\n"
12952             "\n"
12953             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));",
12954             format("f(\n"
12955                    "\n"
12956                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));"));
12957   // Regression test for accessing tokens past the end of a vector in the
12958   // TokenLexer.
12959   verifyNoCrash(R"(_T(
12960 "
12961 )
12962 )");
12963 }
12964 
12965 TEST_F(FormatTest, BreaksStringLiteralOperands) {
12966   // In a function call with two operands, the second can be broken with no line
12967   // break before it.
12968   EXPECT_EQ(
12969       "func(a, \"long long \"\n"
12970       "        \"long long\");",
12971       format("func(a, \"long long long long\");", getLLVMStyleWithColumns(24)));
12972   // In a function call with three operands, the second must be broken with a
12973   // line break before it.
12974   EXPECT_EQ("func(a,\n"
12975             "     \"long long long \"\n"
12976             "     \"long\",\n"
12977             "     c);",
12978             format("func(a, \"long long long long\", c);",
12979                    getLLVMStyleWithColumns(24)));
12980   // In a function call with three operands, the third must be broken with a
12981   // line break before it.
12982   EXPECT_EQ("func(a, b,\n"
12983             "     \"long long long \"\n"
12984             "     \"long\");",
12985             format("func(a, b, \"long long long long\");",
12986                    getLLVMStyleWithColumns(24)));
12987   // In a function call with three operands, both the second and the third must
12988   // be broken with a line break before them.
12989   EXPECT_EQ("func(a,\n"
12990             "     \"long long long \"\n"
12991             "     \"long\",\n"
12992             "     \"long long long \"\n"
12993             "     \"long\");",
12994             format("func(a, \"long long long long\", \"long long long long\");",
12995                    getLLVMStyleWithColumns(24)));
12996   // In a chain of << with two operands, the second can be broken with no line
12997   // break before it.
12998   EXPECT_EQ("a << \"line line \"\n"
12999             "     \"line\";",
13000             format("a << \"line line line\";", getLLVMStyleWithColumns(20)));
13001   // In a chain of << with three operands, the second can be broken with no line
13002   // break before it.
13003   EXPECT_EQ(
13004       "abcde << \"line \"\n"
13005       "         \"line line\"\n"
13006       "      << c;",
13007       format("abcde << \"line line line\" << c;", getLLVMStyleWithColumns(20)));
13008   // In a chain of << with three operands, the third must be broken with a line
13009   // break before it.
13010   EXPECT_EQ(
13011       "a << b\n"
13012       "  << \"line line \"\n"
13013       "     \"line\";",
13014       format("a << b << \"line line line\";", getLLVMStyleWithColumns(20)));
13015   // In a chain of << with three operands, the second can be broken with no line
13016   // break before it and the third must be broken with a line break before it.
13017   EXPECT_EQ("abcd << \"line line \"\n"
13018             "        \"line\"\n"
13019             "     << \"line line \"\n"
13020             "        \"line\";",
13021             format("abcd << \"line line line\" << \"line line line\";",
13022                    getLLVMStyleWithColumns(20)));
13023   // In a chain of binary operators with two operands, the second can be broken
13024   // with no line break before it.
13025   EXPECT_EQ(
13026       "abcd + \"line line \"\n"
13027       "       \"line line\";",
13028       format("abcd + \"line line line line\";", getLLVMStyleWithColumns(20)));
13029   // In a chain of binary operators with three operands, the second must be
13030   // broken with a line break before it.
13031   EXPECT_EQ("abcd +\n"
13032             "    \"line line \"\n"
13033             "    \"line line\" +\n"
13034             "    e;",
13035             format("abcd + \"line line line line\" + e;",
13036                    getLLVMStyleWithColumns(20)));
13037   // In a function call with two operands, with AlignAfterOpenBracket enabled,
13038   // the first must be broken with a line break before it.
13039   FormatStyle Style = getLLVMStyleWithColumns(25);
13040   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
13041   EXPECT_EQ("someFunction(\n"
13042             "    \"long long long \"\n"
13043             "    \"long\",\n"
13044             "    a);",
13045             format("someFunction(\"long long long long\", a);", Style));
13046 }
13047 
13048 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
13049   EXPECT_EQ(
13050       "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
13051       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
13052       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
13053       format("aaaaaaaaaaa  =  \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
13054              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
13055              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"));
13056 }
13057 
13058 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
13059   EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);",
13060             format("f(g(R\"x(raw literal)x\",   a), b);", getGoogleStyle()));
13061   EXPECT_EQ("fffffffffff(g(R\"x(\n"
13062             "multiline raw string literal xxxxxxxxxxxxxx\n"
13063             ")x\",\n"
13064             "              a),\n"
13065             "            b);",
13066             format("fffffffffff(g(R\"x(\n"
13067                    "multiline raw string literal xxxxxxxxxxxxxx\n"
13068                    ")x\", a), b);",
13069                    getGoogleStyleWithColumns(20)));
13070   EXPECT_EQ("fffffffffff(\n"
13071             "    g(R\"x(qqq\n"
13072             "multiline raw string literal xxxxxxxxxxxxxx\n"
13073             ")x\",\n"
13074             "      a),\n"
13075             "    b);",
13076             format("fffffffffff(g(R\"x(qqq\n"
13077                    "multiline raw string literal xxxxxxxxxxxxxx\n"
13078                    ")x\", a), b);",
13079                    getGoogleStyleWithColumns(20)));
13080 
13081   EXPECT_EQ("fffffffffff(R\"x(\n"
13082             "multiline raw string literal xxxxxxxxxxxxxx\n"
13083             ")x\");",
13084             format("fffffffffff(R\"x(\n"
13085                    "multiline raw string literal xxxxxxxxxxxxxx\n"
13086                    ")x\");",
13087                    getGoogleStyleWithColumns(20)));
13088   EXPECT_EQ("fffffffffff(R\"x(\n"
13089             "multiline raw string literal xxxxxxxxxxxxxx\n"
13090             ")x\" + bbbbbb);",
13091             format("fffffffffff(R\"x(\n"
13092                    "multiline raw string literal xxxxxxxxxxxxxx\n"
13093                    ")x\" +   bbbbbb);",
13094                    getGoogleStyleWithColumns(20)));
13095   EXPECT_EQ("fffffffffff(\n"
13096             "    R\"x(\n"
13097             "multiline raw string literal xxxxxxxxxxxxxx\n"
13098             ")x\" +\n"
13099             "    bbbbbb);",
13100             format("fffffffffff(\n"
13101                    " R\"x(\n"
13102                    "multiline raw string literal xxxxxxxxxxxxxx\n"
13103                    ")x\" + bbbbbb);",
13104                    getGoogleStyleWithColumns(20)));
13105   EXPECT_EQ("fffffffffff(R\"(single line raw string)\" + bbbbbb);",
13106             format("fffffffffff(\n"
13107                    " R\"(single line raw string)\" + bbbbbb);"));
13108 }
13109 
13110 TEST_F(FormatTest, SkipsUnknownStringLiterals) {
13111   verifyFormat("string a = \"unterminated;");
13112   EXPECT_EQ("function(\"unterminated,\n"
13113             "         OtherParameter);",
13114             format("function(  \"unterminated,\n"
13115                    "    OtherParameter);"));
13116 }
13117 
13118 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
13119   FormatStyle Style = getLLVMStyle();
13120   Style.Standard = FormatStyle::LS_Cpp03;
13121   EXPECT_EQ("#define x(_a) printf(\"foo\" _a);",
13122             format("#define x(_a) printf(\"foo\"_a);", Style));
13123 }
13124 
13125 TEST_F(FormatTest, CppLexVersion) {
13126   FormatStyle Style = getLLVMStyle();
13127   // Formatting of x * y differs if x is a type.
13128   verifyFormat("void foo() { MACRO(a * b); }", Style);
13129   verifyFormat("void foo() { MACRO(int *b); }", Style);
13130 
13131   // LLVM style uses latest lexer.
13132   verifyFormat("void foo() { MACRO(char8_t *b); }", Style);
13133   Style.Standard = FormatStyle::LS_Cpp17;
13134   // But in c++17, char8_t isn't a keyword.
13135   verifyFormat("void foo() { MACRO(char8_t * b); }", Style);
13136 }
13137 
13138 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); }
13139 
13140 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
13141   EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
13142             "             \"ddeeefff\");",
13143             format("someFunction(\"aaabbbcccdddeeefff\");",
13144                    getLLVMStyleWithColumns(25)));
13145   EXPECT_EQ("someFunction1234567890(\n"
13146             "    \"aaabbbcccdddeeefff\");",
13147             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
13148                    getLLVMStyleWithColumns(26)));
13149   EXPECT_EQ("someFunction1234567890(\n"
13150             "    \"aaabbbcccdddeeeff\"\n"
13151             "    \"f\");",
13152             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
13153                    getLLVMStyleWithColumns(25)));
13154   EXPECT_EQ("someFunction1234567890(\n"
13155             "    \"aaabbbcccdddeeeff\"\n"
13156             "    \"f\");",
13157             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
13158                    getLLVMStyleWithColumns(24)));
13159   EXPECT_EQ("someFunction(\n"
13160             "    \"aaabbbcc ddde \"\n"
13161             "    \"efff\");",
13162             format("someFunction(\"aaabbbcc ddde efff\");",
13163                    getLLVMStyleWithColumns(25)));
13164   EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
13165             "             \"ddeeefff\");",
13166             format("someFunction(\"aaabbbccc ddeeefff\");",
13167                    getLLVMStyleWithColumns(25)));
13168   EXPECT_EQ("someFunction1234567890(\n"
13169             "    \"aaabb \"\n"
13170             "    \"cccdddeeefff\");",
13171             format("someFunction1234567890(\"aaabb cccdddeeefff\");",
13172                    getLLVMStyleWithColumns(25)));
13173   EXPECT_EQ("#define A          \\\n"
13174             "  string s =       \\\n"
13175             "      \"123456789\"  \\\n"
13176             "      \"0\";         \\\n"
13177             "  int i;",
13178             format("#define A string s = \"1234567890\"; int i;",
13179                    getLLVMStyleWithColumns(20)));
13180   EXPECT_EQ("someFunction(\n"
13181             "    \"aaabbbcc \"\n"
13182             "    \"dddeeefff\");",
13183             format("someFunction(\"aaabbbcc dddeeefff\");",
13184                    getLLVMStyleWithColumns(25)));
13185 }
13186 
13187 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
13188   EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3)));
13189   EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2)));
13190   EXPECT_EQ("\"test\"\n"
13191             "\"\\n\"",
13192             format("\"test\\n\"", getLLVMStyleWithColumns(7)));
13193   EXPECT_EQ("\"tes\\\\\"\n"
13194             "\"n\"",
13195             format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
13196   EXPECT_EQ("\"\\\\\\\\\"\n"
13197             "\"\\n\"",
13198             format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
13199   EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
13200   EXPECT_EQ("\"\\uff01\"\n"
13201             "\"test\"",
13202             format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
13203   EXPECT_EQ("\"\\Uff01ff02\"",
13204             format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
13205   EXPECT_EQ("\"\\x000000000001\"\n"
13206             "\"next\"",
13207             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
13208   EXPECT_EQ("\"\\x000000000001next\"",
13209             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
13210   EXPECT_EQ("\"\\x000000000001\"",
13211             format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
13212   EXPECT_EQ("\"test\"\n"
13213             "\"\\000000\"\n"
13214             "\"000001\"",
13215             format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
13216   EXPECT_EQ("\"test\\000\"\n"
13217             "\"00000000\"\n"
13218             "\"1\"",
13219             format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
13220 }
13221 
13222 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
13223   verifyFormat("void f() {\n"
13224                "  return g() {}\n"
13225                "  void h() {}");
13226   verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
13227                "g();\n"
13228                "}");
13229 }
13230 
13231 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
13232   verifyFormat(
13233       "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
13234 }
13235 
13236 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
13237   verifyFormat("class X {\n"
13238                "  void f() {\n"
13239                "  }\n"
13240                "};",
13241                getLLVMStyleWithColumns(12));
13242 }
13243 
13244 TEST_F(FormatTest, ConfigurableIndentWidth) {
13245   FormatStyle EightIndent = getLLVMStyleWithColumns(18);
13246   EightIndent.IndentWidth = 8;
13247   EightIndent.ContinuationIndentWidth = 8;
13248   verifyFormat("void f() {\n"
13249                "        someFunction();\n"
13250                "        if (true) {\n"
13251                "                f();\n"
13252                "        }\n"
13253                "}",
13254                EightIndent);
13255   verifyFormat("class X {\n"
13256                "        void f() {\n"
13257                "        }\n"
13258                "};",
13259                EightIndent);
13260   verifyFormat("int x[] = {\n"
13261                "        call(),\n"
13262                "        call()};",
13263                EightIndent);
13264 }
13265 
13266 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
13267   verifyFormat("double\n"
13268                "f();",
13269                getLLVMStyleWithColumns(8));
13270 }
13271 
13272 TEST_F(FormatTest, ConfigurableUseOfTab) {
13273   FormatStyle Tab = getLLVMStyleWithColumns(42);
13274   Tab.IndentWidth = 8;
13275   Tab.UseTab = FormatStyle::UT_Always;
13276   Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left;
13277 
13278   EXPECT_EQ("if (aaaaaaaa && // q\n"
13279             "    bb)\t\t// w\n"
13280             "\t;",
13281             format("if (aaaaaaaa &&// q\n"
13282                    "bb)// w\n"
13283                    ";",
13284                    Tab));
13285   EXPECT_EQ("if (aaa && bbb) // w\n"
13286             "\t;",
13287             format("if(aaa&&bbb)// w\n"
13288                    ";",
13289                    Tab));
13290 
13291   verifyFormat("class X {\n"
13292                "\tvoid f() {\n"
13293                "\t\tsomeFunction(parameter1,\n"
13294                "\t\t\t     parameter2);\n"
13295                "\t}\n"
13296                "};",
13297                Tab);
13298   verifyFormat("#define A                        \\\n"
13299                "\tvoid f() {               \\\n"
13300                "\t\tsomeFunction(    \\\n"
13301                "\t\t    parameter1,  \\\n"
13302                "\t\t    parameter2); \\\n"
13303                "\t}",
13304                Tab);
13305   verifyFormat("int a;\t      // x\n"
13306                "int bbbbbbbb; // x\n",
13307                Tab);
13308 
13309   Tab.TabWidth = 4;
13310   Tab.IndentWidth = 8;
13311   verifyFormat("class TabWidth4Indent8 {\n"
13312                "\t\tvoid f() {\n"
13313                "\t\t\t\tsomeFunction(parameter1,\n"
13314                "\t\t\t\t\t\t\t parameter2);\n"
13315                "\t\t}\n"
13316                "};",
13317                Tab);
13318 
13319   Tab.TabWidth = 4;
13320   Tab.IndentWidth = 4;
13321   verifyFormat("class TabWidth4Indent4 {\n"
13322                "\tvoid f() {\n"
13323                "\t\tsomeFunction(parameter1,\n"
13324                "\t\t\t\t\t parameter2);\n"
13325                "\t}\n"
13326                "};",
13327                Tab);
13328 
13329   Tab.TabWidth = 8;
13330   Tab.IndentWidth = 4;
13331   verifyFormat("class TabWidth8Indent4 {\n"
13332                "    void f() {\n"
13333                "\tsomeFunction(parameter1,\n"
13334                "\t\t     parameter2);\n"
13335                "    }\n"
13336                "};",
13337                Tab);
13338 
13339   Tab.TabWidth = 8;
13340   Tab.IndentWidth = 8;
13341   EXPECT_EQ("/*\n"
13342             "\t      a\t\tcomment\n"
13343             "\t      in multiple lines\n"
13344             "       */",
13345             format("   /*\t \t \n"
13346                    " \t \t a\t\tcomment\t \t\n"
13347                    " \t \t in multiple lines\t\n"
13348                    " \t  */",
13349                    Tab));
13350 
13351   Tab.UseTab = FormatStyle::UT_ForIndentation;
13352   verifyFormat("{\n"
13353                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13354                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13355                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13356                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13357                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13358                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13359                "};",
13360                Tab);
13361   verifyFormat("enum AA {\n"
13362                "\ta1, // Force multiple lines\n"
13363                "\ta2,\n"
13364                "\ta3\n"
13365                "};",
13366                Tab);
13367   EXPECT_EQ("if (aaaaaaaa && // q\n"
13368             "    bb)         // w\n"
13369             "\t;",
13370             format("if (aaaaaaaa &&// q\n"
13371                    "bb)// w\n"
13372                    ";",
13373                    Tab));
13374   verifyFormat("class X {\n"
13375                "\tvoid f() {\n"
13376                "\t\tsomeFunction(parameter1,\n"
13377                "\t\t             parameter2);\n"
13378                "\t}\n"
13379                "};",
13380                Tab);
13381   verifyFormat("{\n"
13382                "\tQ(\n"
13383                "\t    {\n"
13384                "\t\t    int a;\n"
13385                "\t\t    someFunction(aaaaaaaa,\n"
13386                "\t\t                 bbbbbbb);\n"
13387                "\t    },\n"
13388                "\t    p);\n"
13389                "}",
13390                Tab);
13391   EXPECT_EQ("{\n"
13392             "\t/* aaaa\n"
13393             "\t   bbbb */\n"
13394             "}",
13395             format("{\n"
13396                    "/* aaaa\n"
13397                    "   bbbb */\n"
13398                    "}",
13399                    Tab));
13400   EXPECT_EQ("{\n"
13401             "\t/*\n"
13402             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13403             "\t  bbbbbbbbbbbbb\n"
13404             "\t*/\n"
13405             "}",
13406             format("{\n"
13407                    "/*\n"
13408                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13409                    "*/\n"
13410                    "}",
13411                    Tab));
13412   EXPECT_EQ("{\n"
13413             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13414             "\t// bbbbbbbbbbbbb\n"
13415             "}",
13416             format("{\n"
13417                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13418                    "}",
13419                    Tab));
13420   EXPECT_EQ("{\n"
13421             "\t/*\n"
13422             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13423             "\t  bbbbbbbbbbbbb\n"
13424             "\t*/\n"
13425             "}",
13426             format("{\n"
13427                    "\t/*\n"
13428                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13429                    "\t*/\n"
13430                    "}",
13431                    Tab));
13432   EXPECT_EQ("{\n"
13433             "\t/*\n"
13434             "\n"
13435             "\t*/\n"
13436             "}",
13437             format("{\n"
13438                    "\t/*\n"
13439                    "\n"
13440                    "\t*/\n"
13441                    "}",
13442                    Tab));
13443   EXPECT_EQ("{\n"
13444             "\t/*\n"
13445             " asdf\n"
13446             "\t*/\n"
13447             "}",
13448             format("{\n"
13449                    "\t/*\n"
13450                    " asdf\n"
13451                    "\t*/\n"
13452                    "}",
13453                    Tab));
13454 
13455   Tab.UseTab = FormatStyle::UT_Never;
13456   EXPECT_EQ("/*\n"
13457             "              a\t\tcomment\n"
13458             "              in multiple lines\n"
13459             "       */",
13460             format("   /*\t \t \n"
13461                    " \t \t a\t\tcomment\t \t\n"
13462                    " \t \t in multiple lines\t\n"
13463                    " \t  */",
13464                    Tab));
13465   EXPECT_EQ("/* some\n"
13466             "   comment */",
13467             format(" \t \t /* some\n"
13468                    " \t \t    comment */",
13469                    Tab));
13470   EXPECT_EQ("int a; /* some\n"
13471             "   comment */",
13472             format(" \t \t int a; /* some\n"
13473                    " \t \t    comment */",
13474                    Tab));
13475 
13476   EXPECT_EQ("int a; /* some\n"
13477             "comment */",
13478             format(" \t \t int\ta; /* some\n"
13479                    " \t \t    comment */",
13480                    Tab));
13481   EXPECT_EQ("f(\"\t\t\"); /* some\n"
13482             "    comment */",
13483             format(" \t \t f(\"\t\t\"); /* some\n"
13484                    " \t \t    comment */",
13485                    Tab));
13486   EXPECT_EQ("{\n"
13487             "        /*\n"
13488             "         * Comment\n"
13489             "         */\n"
13490             "        int i;\n"
13491             "}",
13492             format("{\n"
13493                    "\t/*\n"
13494                    "\t * Comment\n"
13495                    "\t */\n"
13496                    "\t int i;\n"
13497                    "}",
13498                    Tab));
13499 
13500   Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
13501   Tab.TabWidth = 8;
13502   Tab.IndentWidth = 8;
13503   EXPECT_EQ("if (aaaaaaaa && // q\n"
13504             "    bb)         // w\n"
13505             "\t;",
13506             format("if (aaaaaaaa &&// q\n"
13507                    "bb)// w\n"
13508                    ";",
13509                    Tab));
13510   EXPECT_EQ("if (aaa && bbb) // w\n"
13511             "\t;",
13512             format("if(aaa&&bbb)// w\n"
13513                    ";",
13514                    Tab));
13515   verifyFormat("class X {\n"
13516                "\tvoid f() {\n"
13517                "\t\tsomeFunction(parameter1,\n"
13518                "\t\t\t     parameter2);\n"
13519                "\t}\n"
13520                "};",
13521                Tab);
13522   verifyFormat("#define A                        \\\n"
13523                "\tvoid f() {               \\\n"
13524                "\t\tsomeFunction(    \\\n"
13525                "\t\t    parameter1,  \\\n"
13526                "\t\t    parameter2); \\\n"
13527                "\t}",
13528                Tab);
13529   Tab.TabWidth = 4;
13530   Tab.IndentWidth = 8;
13531   verifyFormat("class TabWidth4Indent8 {\n"
13532                "\t\tvoid f() {\n"
13533                "\t\t\t\tsomeFunction(parameter1,\n"
13534                "\t\t\t\t\t\t\t parameter2);\n"
13535                "\t\t}\n"
13536                "};",
13537                Tab);
13538   Tab.TabWidth = 4;
13539   Tab.IndentWidth = 4;
13540   verifyFormat("class TabWidth4Indent4 {\n"
13541                "\tvoid f() {\n"
13542                "\t\tsomeFunction(parameter1,\n"
13543                "\t\t\t\t\t parameter2);\n"
13544                "\t}\n"
13545                "};",
13546                Tab);
13547   Tab.TabWidth = 8;
13548   Tab.IndentWidth = 4;
13549   verifyFormat("class TabWidth8Indent4 {\n"
13550                "    void f() {\n"
13551                "\tsomeFunction(parameter1,\n"
13552                "\t\t     parameter2);\n"
13553                "    }\n"
13554                "};",
13555                Tab);
13556   Tab.TabWidth = 8;
13557   Tab.IndentWidth = 8;
13558   EXPECT_EQ("/*\n"
13559             "\t      a\t\tcomment\n"
13560             "\t      in multiple lines\n"
13561             "       */",
13562             format("   /*\t \t \n"
13563                    " \t \t a\t\tcomment\t \t\n"
13564                    " \t \t in multiple lines\t\n"
13565                    " \t  */",
13566                    Tab));
13567   verifyFormat("{\n"
13568                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13569                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13570                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13571                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13572                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13573                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13574                "};",
13575                Tab);
13576   verifyFormat("enum AA {\n"
13577                "\ta1, // Force multiple lines\n"
13578                "\ta2,\n"
13579                "\ta3\n"
13580                "};",
13581                Tab);
13582   EXPECT_EQ("if (aaaaaaaa && // q\n"
13583             "    bb)         // w\n"
13584             "\t;",
13585             format("if (aaaaaaaa &&// q\n"
13586                    "bb)// w\n"
13587                    ";",
13588                    Tab));
13589   verifyFormat("class X {\n"
13590                "\tvoid f() {\n"
13591                "\t\tsomeFunction(parameter1,\n"
13592                "\t\t\t     parameter2);\n"
13593                "\t}\n"
13594                "};",
13595                Tab);
13596   verifyFormat("{\n"
13597                "\tQ(\n"
13598                "\t    {\n"
13599                "\t\t    int a;\n"
13600                "\t\t    someFunction(aaaaaaaa,\n"
13601                "\t\t\t\t bbbbbbb);\n"
13602                "\t    },\n"
13603                "\t    p);\n"
13604                "}",
13605                Tab);
13606   EXPECT_EQ("{\n"
13607             "\t/* aaaa\n"
13608             "\t   bbbb */\n"
13609             "}",
13610             format("{\n"
13611                    "/* aaaa\n"
13612                    "   bbbb */\n"
13613                    "}",
13614                    Tab));
13615   EXPECT_EQ("{\n"
13616             "\t/*\n"
13617             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13618             "\t  bbbbbbbbbbbbb\n"
13619             "\t*/\n"
13620             "}",
13621             format("{\n"
13622                    "/*\n"
13623                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13624                    "*/\n"
13625                    "}",
13626                    Tab));
13627   EXPECT_EQ("{\n"
13628             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13629             "\t// bbbbbbbbbbbbb\n"
13630             "}",
13631             format("{\n"
13632                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13633                    "}",
13634                    Tab));
13635   EXPECT_EQ("{\n"
13636             "\t/*\n"
13637             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13638             "\t  bbbbbbbbbbbbb\n"
13639             "\t*/\n"
13640             "}",
13641             format("{\n"
13642                    "\t/*\n"
13643                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13644                    "\t*/\n"
13645                    "}",
13646                    Tab));
13647   EXPECT_EQ("{\n"
13648             "\t/*\n"
13649             "\n"
13650             "\t*/\n"
13651             "}",
13652             format("{\n"
13653                    "\t/*\n"
13654                    "\n"
13655                    "\t*/\n"
13656                    "}",
13657                    Tab));
13658   EXPECT_EQ("{\n"
13659             "\t/*\n"
13660             " asdf\n"
13661             "\t*/\n"
13662             "}",
13663             format("{\n"
13664                    "\t/*\n"
13665                    " asdf\n"
13666                    "\t*/\n"
13667                    "}",
13668                    Tab));
13669   EXPECT_EQ("/* some\n"
13670             "   comment */",
13671             format(" \t \t /* some\n"
13672                    " \t \t    comment */",
13673                    Tab));
13674   EXPECT_EQ("int a; /* some\n"
13675             "   comment */",
13676             format(" \t \t int a; /* some\n"
13677                    " \t \t    comment */",
13678                    Tab));
13679   EXPECT_EQ("int a; /* some\n"
13680             "comment */",
13681             format(" \t \t int\ta; /* some\n"
13682                    " \t \t    comment */",
13683                    Tab));
13684   EXPECT_EQ("f(\"\t\t\"); /* some\n"
13685             "    comment */",
13686             format(" \t \t f(\"\t\t\"); /* some\n"
13687                    " \t \t    comment */",
13688                    Tab));
13689   EXPECT_EQ("{\n"
13690             "\t/*\n"
13691             "\t * Comment\n"
13692             "\t */\n"
13693             "\tint i;\n"
13694             "}",
13695             format("{\n"
13696                    "\t/*\n"
13697                    "\t * Comment\n"
13698                    "\t */\n"
13699                    "\t int i;\n"
13700                    "}",
13701                    Tab));
13702   Tab.TabWidth = 2;
13703   Tab.IndentWidth = 2;
13704   EXPECT_EQ("{\n"
13705             "\t/* aaaa\n"
13706             "\t\t bbbb */\n"
13707             "}",
13708             format("{\n"
13709                    "/* aaaa\n"
13710                    "\t bbbb */\n"
13711                    "}",
13712                    Tab));
13713   EXPECT_EQ("{\n"
13714             "\t/*\n"
13715             "\t\taaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13716             "\t\tbbbbbbbbbbbbb\n"
13717             "\t*/\n"
13718             "}",
13719             format("{\n"
13720                    "/*\n"
13721                    "\taaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13722                    "*/\n"
13723                    "}",
13724                    Tab));
13725   Tab.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
13726   Tab.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
13727   Tab.TabWidth = 4;
13728   Tab.IndentWidth = 4;
13729   verifyFormat("class Assign {\n"
13730                "\tvoid f() {\n"
13731                "\t\tint         x      = 123;\n"
13732                "\t\tint         random = 4;\n"
13733                "\t\tstd::string alphabet =\n"
13734                "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
13735                "\t}\n"
13736                "};",
13737                Tab);
13738 
13739   Tab.UseTab = FormatStyle::UT_AlignWithSpaces;
13740   Tab.TabWidth = 8;
13741   Tab.IndentWidth = 8;
13742   EXPECT_EQ("if (aaaaaaaa && // q\n"
13743             "    bb)         // w\n"
13744             "\t;",
13745             format("if (aaaaaaaa &&// q\n"
13746                    "bb)// w\n"
13747                    ";",
13748                    Tab));
13749   EXPECT_EQ("if (aaa && bbb) // w\n"
13750             "\t;",
13751             format("if(aaa&&bbb)// w\n"
13752                    ";",
13753                    Tab));
13754   verifyFormat("class X {\n"
13755                "\tvoid f() {\n"
13756                "\t\tsomeFunction(parameter1,\n"
13757                "\t\t             parameter2);\n"
13758                "\t}\n"
13759                "};",
13760                Tab);
13761   verifyFormat("#define A                        \\\n"
13762                "\tvoid f() {               \\\n"
13763                "\t\tsomeFunction(    \\\n"
13764                "\t\t    parameter1,  \\\n"
13765                "\t\t    parameter2); \\\n"
13766                "\t}",
13767                Tab);
13768   Tab.TabWidth = 4;
13769   Tab.IndentWidth = 8;
13770   verifyFormat("class TabWidth4Indent8 {\n"
13771                "\t\tvoid f() {\n"
13772                "\t\t\t\tsomeFunction(parameter1,\n"
13773                "\t\t\t\t             parameter2);\n"
13774                "\t\t}\n"
13775                "};",
13776                Tab);
13777   Tab.TabWidth = 4;
13778   Tab.IndentWidth = 4;
13779   verifyFormat("class TabWidth4Indent4 {\n"
13780                "\tvoid f() {\n"
13781                "\t\tsomeFunction(parameter1,\n"
13782                "\t\t             parameter2);\n"
13783                "\t}\n"
13784                "};",
13785                Tab);
13786   Tab.TabWidth = 8;
13787   Tab.IndentWidth = 4;
13788   verifyFormat("class TabWidth8Indent4 {\n"
13789                "    void f() {\n"
13790                "\tsomeFunction(parameter1,\n"
13791                "\t             parameter2);\n"
13792                "    }\n"
13793                "};",
13794                Tab);
13795   Tab.TabWidth = 8;
13796   Tab.IndentWidth = 8;
13797   EXPECT_EQ("/*\n"
13798             "              a\t\tcomment\n"
13799             "              in multiple lines\n"
13800             "       */",
13801             format("   /*\t \t \n"
13802                    " \t \t a\t\tcomment\t \t\n"
13803                    " \t \t in multiple lines\t\n"
13804                    " \t  */",
13805                    Tab));
13806   verifyFormat("{\n"
13807                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13808                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13809                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13810                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13811                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13812                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13813                "};",
13814                Tab);
13815   verifyFormat("enum AA {\n"
13816                "\ta1, // Force multiple lines\n"
13817                "\ta2,\n"
13818                "\ta3\n"
13819                "};",
13820                Tab);
13821   EXPECT_EQ("if (aaaaaaaa && // q\n"
13822             "    bb)         // w\n"
13823             "\t;",
13824             format("if (aaaaaaaa &&// q\n"
13825                    "bb)// w\n"
13826                    ";",
13827                    Tab));
13828   verifyFormat("class X {\n"
13829                "\tvoid f() {\n"
13830                "\t\tsomeFunction(parameter1,\n"
13831                "\t\t             parameter2);\n"
13832                "\t}\n"
13833                "};",
13834                Tab);
13835   verifyFormat("{\n"
13836                "\tQ(\n"
13837                "\t    {\n"
13838                "\t\t    int a;\n"
13839                "\t\t    someFunction(aaaaaaaa,\n"
13840                "\t\t                 bbbbbbb);\n"
13841                "\t    },\n"
13842                "\t    p);\n"
13843                "}",
13844                Tab);
13845   EXPECT_EQ("{\n"
13846             "\t/* aaaa\n"
13847             "\t   bbbb */\n"
13848             "}",
13849             format("{\n"
13850                    "/* aaaa\n"
13851                    "   bbbb */\n"
13852                    "}",
13853                    Tab));
13854   EXPECT_EQ("{\n"
13855             "\t/*\n"
13856             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13857             "\t  bbbbbbbbbbbbb\n"
13858             "\t*/\n"
13859             "}",
13860             format("{\n"
13861                    "/*\n"
13862                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13863                    "*/\n"
13864                    "}",
13865                    Tab));
13866   EXPECT_EQ("{\n"
13867             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13868             "\t// bbbbbbbbbbbbb\n"
13869             "}",
13870             format("{\n"
13871                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13872                    "}",
13873                    Tab));
13874   EXPECT_EQ("{\n"
13875             "\t/*\n"
13876             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13877             "\t  bbbbbbbbbbbbb\n"
13878             "\t*/\n"
13879             "}",
13880             format("{\n"
13881                    "\t/*\n"
13882                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13883                    "\t*/\n"
13884                    "}",
13885                    Tab));
13886   EXPECT_EQ("{\n"
13887             "\t/*\n"
13888             "\n"
13889             "\t*/\n"
13890             "}",
13891             format("{\n"
13892                    "\t/*\n"
13893                    "\n"
13894                    "\t*/\n"
13895                    "}",
13896                    Tab));
13897   EXPECT_EQ("{\n"
13898             "\t/*\n"
13899             " asdf\n"
13900             "\t*/\n"
13901             "}",
13902             format("{\n"
13903                    "\t/*\n"
13904                    " asdf\n"
13905                    "\t*/\n"
13906                    "}",
13907                    Tab));
13908   EXPECT_EQ("/* some\n"
13909             "   comment */",
13910             format(" \t \t /* some\n"
13911                    " \t \t    comment */",
13912                    Tab));
13913   EXPECT_EQ("int a; /* some\n"
13914             "   comment */",
13915             format(" \t \t int a; /* some\n"
13916                    " \t \t    comment */",
13917                    Tab));
13918   EXPECT_EQ("int a; /* some\n"
13919             "comment */",
13920             format(" \t \t int\ta; /* some\n"
13921                    " \t \t    comment */",
13922                    Tab));
13923   EXPECT_EQ("f(\"\t\t\"); /* some\n"
13924             "    comment */",
13925             format(" \t \t f(\"\t\t\"); /* some\n"
13926                    " \t \t    comment */",
13927                    Tab));
13928   EXPECT_EQ("{\n"
13929             "\t/*\n"
13930             "\t * Comment\n"
13931             "\t */\n"
13932             "\tint i;\n"
13933             "}",
13934             format("{\n"
13935                    "\t/*\n"
13936                    "\t * Comment\n"
13937                    "\t */\n"
13938                    "\t int i;\n"
13939                    "}",
13940                    Tab));
13941   Tab.TabWidth = 2;
13942   Tab.IndentWidth = 2;
13943   EXPECT_EQ("{\n"
13944             "\t/* aaaa\n"
13945             "\t   bbbb */\n"
13946             "}",
13947             format("{\n"
13948                    "/* aaaa\n"
13949                    "   bbbb */\n"
13950                    "}",
13951                    Tab));
13952   EXPECT_EQ("{\n"
13953             "\t/*\n"
13954             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13955             "\t  bbbbbbbbbbbbb\n"
13956             "\t*/\n"
13957             "}",
13958             format("{\n"
13959                    "/*\n"
13960                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13961                    "*/\n"
13962                    "}",
13963                    Tab));
13964   Tab.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
13965   Tab.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
13966   Tab.TabWidth = 4;
13967   Tab.IndentWidth = 4;
13968   verifyFormat("class Assign {\n"
13969                "\tvoid f() {\n"
13970                "\t\tint         x      = 123;\n"
13971                "\t\tint         random = 4;\n"
13972                "\t\tstd::string alphabet =\n"
13973                "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
13974                "\t}\n"
13975                "};",
13976                Tab);
13977   Tab.AlignOperands = FormatStyle::OAS_Align;
13978   verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb +\n"
13979                "                 cccccccccccccccccccc;",
13980                Tab);
13981   // no alignment
13982   verifyFormat("int aaaaaaaaaa =\n"
13983                "\tbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
13984                Tab);
13985   verifyFormat("return aaaaaaaaaaaaaaaa ? 111111111111111\n"
13986                "       : bbbbbbbbbbbbbb ? 222222222222222\n"
13987                "                        : 333333333333333;",
13988                Tab);
13989   Tab.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
13990   Tab.AlignOperands = FormatStyle::OAS_AlignAfterOperator;
13991   verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb\n"
13992                "               + cccccccccccccccccccc;",
13993                Tab);
13994 }
13995 
13996 TEST_F(FormatTest, ZeroTabWidth) {
13997   FormatStyle Tab = getLLVMStyleWithColumns(42);
13998   Tab.IndentWidth = 8;
13999   Tab.UseTab = FormatStyle::UT_Never;
14000   Tab.TabWidth = 0;
14001   EXPECT_EQ("void a(){\n"
14002             "    // line starts with '\t'\n"
14003             "};",
14004             format("void a(){\n"
14005                    "\t// line starts with '\t'\n"
14006                    "};",
14007                    Tab));
14008 
14009   EXPECT_EQ("void a(){\n"
14010             "    // line starts with '\t'\n"
14011             "};",
14012             format("void a(){\n"
14013                    "\t\t// line starts with '\t'\n"
14014                    "};",
14015                    Tab));
14016 
14017   Tab.UseTab = FormatStyle::UT_ForIndentation;
14018   EXPECT_EQ("void a(){\n"
14019             "    // line starts with '\t'\n"
14020             "};",
14021             format("void a(){\n"
14022                    "\t// line starts with '\t'\n"
14023                    "};",
14024                    Tab));
14025 
14026   EXPECT_EQ("void a(){\n"
14027             "    // line starts with '\t'\n"
14028             "};",
14029             format("void a(){\n"
14030                    "\t\t// line starts with '\t'\n"
14031                    "};",
14032                    Tab));
14033 
14034   Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
14035   EXPECT_EQ("void a(){\n"
14036             "    // line starts with '\t'\n"
14037             "};",
14038             format("void a(){\n"
14039                    "\t// line starts with '\t'\n"
14040                    "};",
14041                    Tab));
14042 
14043   EXPECT_EQ("void a(){\n"
14044             "    // line starts with '\t'\n"
14045             "};",
14046             format("void a(){\n"
14047                    "\t\t// line starts with '\t'\n"
14048                    "};",
14049                    Tab));
14050 
14051   Tab.UseTab = FormatStyle::UT_AlignWithSpaces;
14052   EXPECT_EQ("void a(){\n"
14053             "    // line starts with '\t'\n"
14054             "};",
14055             format("void a(){\n"
14056                    "\t// line starts with '\t'\n"
14057                    "};",
14058                    Tab));
14059 
14060   EXPECT_EQ("void a(){\n"
14061             "    // line starts with '\t'\n"
14062             "};",
14063             format("void a(){\n"
14064                    "\t\t// line starts with '\t'\n"
14065                    "};",
14066                    Tab));
14067 
14068   Tab.UseTab = FormatStyle::UT_Always;
14069   EXPECT_EQ("void a(){\n"
14070             "// line starts with '\t'\n"
14071             "};",
14072             format("void a(){\n"
14073                    "\t// line starts with '\t'\n"
14074                    "};",
14075                    Tab));
14076 
14077   EXPECT_EQ("void a(){\n"
14078             "// line starts with '\t'\n"
14079             "};",
14080             format("void a(){\n"
14081                    "\t\t// line starts with '\t'\n"
14082                    "};",
14083                    Tab));
14084 }
14085 
14086 TEST_F(FormatTest, CalculatesOriginalColumn) {
14087   EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
14088             "q\"; /* some\n"
14089             "       comment */",
14090             format("  \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
14091                    "q\"; /* some\n"
14092                    "       comment */",
14093                    getLLVMStyle()));
14094   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
14095             "/* some\n"
14096             "   comment */",
14097             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
14098                    " /* some\n"
14099                    "    comment */",
14100                    getLLVMStyle()));
14101   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
14102             "qqq\n"
14103             "/* some\n"
14104             "   comment */",
14105             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
14106                    "qqq\n"
14107                    " /* some\n"
14108                    "    comment */",
14109                    getLLVMStyle()));
14110   EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
14111             "wwww; /* some\n"
14112             "         comment */",
14113             format("  inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
14114                    "wwww; /* some\n"
14115                    "         comment */",
14116                    getLLVMStyle()));
14117 }
14118 
14119 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
14120   FormatStyle NoSpace = getLLVMStyle();
14121   NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
14122 
14123   verifyFormat("while(true)\n"
14124                "  continue;",
14125                NoSpace);
14126   verifyFormat("for(;;)\n"
14127                "  continue;",
14128                NoSpace);
14129   verifyFormat("if(true)\n"
14130                "  f();\n"
14131                "else if(true)\n"
14132                "  f();",
14133                NoSpace);
14134   verifyFormat("do {\n"
14135                "  do_something();\n"
14136                "} while(something());",
14137                NoSpace);
14138   verifyFormat("switch(x) {\n"
14139                "default:\n"
14140                "  break;\n"
14141                "}",
14142                NoSpace);
14143   verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
14144   verifyFormat("size_t x = sizeof(x);", NoSpace);
14145   verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
14146   verifyFormat("auto f(int x) -> typeof(x);", NoSpace);
14147   verifyFormat("auto f(int x) -> _Atomic(x);", NoSpace);
14148   verifyFormat("auto f(int x) -> __underlying_type(x);", NoSpace);
14149   verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
14150   verifyFormat("alignas(128) char a[128];", NoSpace);
14151   verifyFormat("size_t x = alignof(MyType);", NoSpace);
14152   verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
14153   verifyFormat("int f() throw(Deprecated);", NoSpace);
14154   verifyFormat("typedef void (*cb)(int);", NoSpace);
14155   verifyFormat("T A::operator()();", NoSpace);
14156   verifyFormat("X A::operator++(T);", NoSpace);
14157   verifyFormat("auto lambda = []() { return 0; };", NoSpace);
14158 
14159   FormatStyle Space = getLLVMStyle();
14160   Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
14161 
14162   verifyFormat("int f ();", Space);
14163   verifyFormat("void f (int a, T b) {\n"
14164                "  while (true)\n"
14165                "    continue;\n"
14166                "}",
14167                Space);
14168   verifyFormat("if (true)\n"
14169                "  f ();\n"
14170                "else if (true)\n"
14171                "  f ();",
14172                Space);
14173   verifyFormat("do {\n"
14174                "  do_something ();\n"
14175                "} while (something ());",
14176                Space);
14177   verifyFormat("switch (x) {\n"
14178                "default:\n"
14179                "  break;\n"
14180                "}",
14181                Space);
14182   verifyFormat("A::A () : a (1) {}", Space);
14183   verifyFormat("void f () __attribute__ ((asdf));", Space);
14184   verifyFormat("*(&a + 1);\n"
14185                "&((&a)[1]);\n"
14186                "a[(b + c) * d];\n"
14187                "(((a + 1) * 2) + 3) * 4;",
14188                Space);
14189   verifyFormat("#define A(x) x", Space);
14190   verifyFormat("#define A (x) x", Space);
14191   verifyFormat("#if defined(x)\n"
14192                "#endif",
14193                Space);
14194   verifyFormat("auto i = std::make_unique<int> (5);", Space);
14195   verifyFormat("size_t x = sizeof (x);", Space);
14196   verifyFormat("auto f (int x) -> decltype (x);", Space);
14197   verifyFormat("auto f (int x) -> typeof (x);", Space);
14198   verifyFormat("auto f (int x) -> _Atomic (x);", Space);
14199   verifyFormat("auto f (int x) -> __underlying_type (x);", Space);
14200   verifyFormat("int f (T x) noexcept (x.create ());", Space);
14201   verifyFormat("alignas (128) char a[128];", Space);
14202   verifyFormat("size_t x = alignof (MyType);", Space);
14203   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
14204   verifyFormat("int f () throw (Deprecated);", Space);
14205   verifyFormat("typedef void (*cb) (int);", Space);
14206   // FIXME these tests regressed behaviour.
14207   // verifyFormat("T A::operator() ();", Space);
14208   // verifyFormat("X A::operator++ (T);", Space);
14209   verifyFormat("auto lambda = [] () { return 0; };", Space);
14210   verifyFormat("int x = int (y);", Space);
14211 
14212   FormatStyle SomeSpace = getLLVMStyle();
14213   SomeSpace.SpaceBeforeParens = FormatStyle::SBPO_NonEmptyParentheses;
14214 
14215   verifyFormat("[]() -> float {}", SomeSpace);
14216   verifyFormat("[] (auto foo) {}", SomeSpace);
14217   verifyFormat("[foo]() -> int {}", SomeSpace);
14218   verifyFormat("int f();", SomeSpace);
14219   verifyFormat("void f (int a, T b) {\n"
14220                "  while (true)\n"
14221                "    continue;\n"
14222                "}",
14223                SomeSpace);
14224   verifyFormat("if (true)\n"
14225                "  f();\n"
14226                "else if (true)\n"
14227                "  f();",
14228                SomeSpace);
14229   verifyFormat("do {\n"
14230                "  do_something();\n"
14231                "} while (something());",
14232                SomeSpace);
14233   verifyFormat("switch (x) {\n"
14234                "default:\n"
14235                "  break;\n"
14236                "}",
14237                SomeSpace);
14238   verifyFormat("A::A() : a (1) {}", SomeSpace);
14239   verifyFormat("void f() __attribute__ ((asdf));", SomeSpace);
14240   verifyFormat("*(&a + 1);\n"
14241                "&((&a)[1]);\n"
14242                "a[(b + c) * d];\n"
14243                "(((a + 1) * 2) + 3) * 4;",
14244                SomeSpace);
14245   verifyFormat("#define A(x) x", SomeSpace);
14246   verifyFormat("#define A (x) x", SomeSpace);
14247   verifyFormat("#if defined(x)\n"
14248                "#endif",
14249                SomeSpace);
14250   verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace);
14251   verifyFormat("size_t x = sizeof (x);", SomeSpace);
14252   verifyFormat("auto f (int x) -> decltype (x);", SomeSpace);
14253   verifyFormat("auto f (int x) -> typeof (x);", SomeSpace);
14254   verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace);
14255   verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace);
14256   verifyFormat("int f (T x) noexcept (x.create());", SomeSpace);
14257   verifyFormat("alignas (128) char a[128];", SomeSpace);
14258   verifyFormat("size_t x = alignof (MyType);", SomeSpace);
14259   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");",
14260                SomeSpace);
14261   verifyFormat("int f() throw (Deprecated);", SomeSpace);
14262   verifyFormat("typedef void (*cb) (int);", SomeSpace);
14263   verifyFormat("T A::operator()();", SomeSpace);
14264   // FIXME these tests regressed behaviour.
14265   // verifyFormat("X A::operator++ (T);", SomeSpace);
14266   verifyFormat("int x = int (y);", SomeSpace);
14267   verifyFormat("auto lambda = []() { return 0; };", SomeSpace);
14268 
14269   FormatStyle SpaceControlStatements = getLLVMStyle();
14270   SpaceControlStatements.SpaceBeforeParens = FormatStyle::SBPO_Custom;
14271   SpaceControlStatements.SpaceBeforeParensOptions.AfterControlStatements = true;
14272 
14273   verifyFormat("while (true)\n"
14274                "  continue;",
14275                SpaceControlStatements);
14276   verifyFormat("if (true)\n"
14277                "  f();\n"
14278                "else if (true)\n"
14279                "  f();",
14280                SpaceControlStatements);
14281   verifyFormat("for (;;) {\n"
14282                "  do_something();\n"
14283                "}",
14284                SpaceControlStatements);
14285   verifyFormat("do {\n"
14286                "  do_something();\n"
14287                "} while (something());",
14288                SpaceControlStatements);
14289   verifyFormat("switch (x) {\n"
14290                "default:\n"
14291                "  break;\n"
14292                "}",
14293                SpaceControlStatements);
14294 
14295   FormatStyle SpaceFuncDecl = getLLVMStyle();
14296   SpaceFuncDecl.SpaceBeforeParens = FormatStyle::SBPO_Custom;
14297   SpaceFuncDecl.SpaceBeforeParensOptions.AfterFunctionDeclarationName = true;
14298 
14299   verifyFormat("int f ();", SpaceFuncDecl);
14300   verifyFormat("void f(int a, T b) {}", SpaceFuncDecl);
14301   verifyFormat("A::A() : a(1) {}", SpaceFuncDecl);
14302   verifyFormat("void f () __attribute__((asdf));", SpaceFuncDecl);
14303   verifyFormat("#define A(x) x", SpaceFuncDecl);
14304   verifyFormat("#define A (x) x", SpaceFuncDecl);
14305   verifyFormat("#if defined(x)\n"
14306                "#endif",
14307                SpaceFuncDecl);
14308   verifyFormat("auto i = std::make_unique<int>(5);", SpaceFuncDecl);
14309   verifyFormat("size_t x = sizeof(x);", SpaceFuncDecl);
14310   verifyFormat("auto f (int x) -> decltype(x);", SpaceFuncDecl);
14311   verifyFormat("auto f (int x) -> typeof(x);", SpaceFuncDecl);
14312   verifyFormat("auto f (int x) -> _Atomic(x);", SpaceFuncDecl);
14313   verifyFormat("auto f (int x) -> __underlying_type(x);", SpaceFuncDecl);
14314   verifyFormat("int f (T x) noexcept(x.create());", SpaceFuncDecl);
14315   verifyFormat("alignas(128) char a[128];", SpaceFuncDecl);
14316   verifyFormat("size_t x = alignof(MyType);", SpaceFuncDecl);
14317   verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");",
14318                SpaceFuncDecl);
14319   verifyFormat("int f () throw(Deprecated);", SpaceFuncDecl);
14320   verifyFormat("typedef void (*cb)(int);", SpaceFuncDecl);
14321   // FIXME these tests regressed behaviour.
14322   // verifyFormat("T A::operator() ();", SpaceFuncDecl);
14323   // verifyFormat("X A::operator++ (T);", SpaceFuncDecl);
14324   verifyFormat("T A::operator()() {}", SpaceFuncDecl);
14325   verifyFormat("auto lambda = []() { return 0; };", SpaceFuncDecl);
14326   verifyFormat("int x = int(y);", SpaceFuncDecl);
14327   verifyFormat("M(std::size_t R, std::size_t C) : C(C), data(R) {}",
14328                SpaceFuncDecl);
14329 
14330   FormatStyle SpaceFuncDef = getLLVMStyle();
14331   SpaceFuncDef.SpaceBeforeParens = FormatStyle::SBPO_Custom;
14332   SpaceFuncDef.SpaceBeforeParensOptions.AfterFunctionDefinitionName = true;
14333 
14334   verifyFormat("int f();", SpaceFuncDef);
14335   verifyFormat("void f (int a, T b) {}", SpaceFuncDef);
14336   verifyFormat("A::A() : a(1) {}", SpaceFuncDef);
14337   verifyFormat("void f() __attribute__((asdf));", SpaceFuncDef);
14338   verifyFormat("#define A(x) x", SpaceFuncDef);
14339   verifyFormat("#define A (x) x", SpaceFuncDef);
14340   verifyFormat("#if defined(x)\n"
14341                "#endif",
14342                SpaceFuncDef);
14343   verifyFormat("auto i = std::make_unique<int>(5);", SpaceFuncDef);
14344   verifyFormat("size_t x = sizeof(x);", SpaceFuncDef);
14345   verifyFormat("auto f(int x) -> decltype(x);", SpaceFuncDef);
14346   verifyFormat("auto f(int x) -> typeof(x);", SpaceFuncDef);
14347   verifyFormat("auto f(int x) -> _Atomic(x);", SpaceFuncDef);
14348   verifyFormat("auto f(int x) -> __underlying_type(x);", SpaceFuncDef);
14349   verifyFormat("int f(T x) noexcept(x.create());", SpaceFuncDef);
14350   verifyFormat("alignas(128) char a[128];", SpaceFuncDef);
14351   verifyFormat("size_t x = alignof(MyType);", SpaceFuncDef);
14352   verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");",
14353                SpaceFuncDef);
14354   verifyFormat("int f() throw(Deprecated);", SpaceFuncDef);
14355   verifyFormat("typedef void (*cb)(int);", SpaceFuncDef);
14356   verifyFormat("T A::operator()();", SpaceFuncDef);
14357   verifyFormat("X A::operator++(T);", SpaceFuncDef);
14358   // verifyFormat("T A::operator() () {}", SpaceFuncDef);
14359   verifyFormat("auto lambda = [] () { return 0; };", SpaceFuncDef);
14360   verifyFormat("int x = int(y);", SpaceFuncDef);
14361   verifyFormat("M(std::size_t R, std::size_t C) : C(C), data(R) {}",
14362                SpaceFuncDef);
14363 
14364   FormatStyle SpaceIfMacros = getLLVMStyle();
14365   SpaceIfMacros.IfMacros.clear();
14366   SpaceIfMacros.IfMacros.push_back("MYIF");
14367   SpaceIfMacros.SpaceBeforeParens = FormatStyle::SBPO_Custom;
14368   SpaceIfMacros.SpaceBeforeParensOptions.AfterIfMacros = true;
14369   verifyFormat("MYIF (a)\n  return;", SpaceIfMacros);
14370   verifyFormat("MYIF (a)\n  return;\nelse MYIF (b)\n  return;", SpaceIfMacros);
14371   verifyFormat("MYIF (a)\n  return;\nelse\n  return;", SpaceIfMacros);
14372 
14373   FormatStyle SpaceForeachMacros = getLLVMStyle();
14374   SpaceForeachMacros.SpaceBeforeParens = FormatStyle::SBPO_Custom;
14375   SpaceForeachMacros.SpaceBeforeParensOptions.AfterForeachMacros = true;
14376   verifyFormat("foreach (Item *item, itemlist) {}", SpaceForeachMacros);
14377   verifyFormat("Q_FOREACH (Item *item, itemlist) {}", SpaceForeachMacros);
14378   verifyFormat("BOOST_FOREACH (Item *item, itemlist) {}", SpaceForeachMacros);
14379   verifyFormat("UNKNOWN_FOREACH(Item *item, itemlist) {}", SpaceForeachMacros);
14380 
14381   FormatStyle SomeSpace2 = getLLVMStyle();
14382   SomeSpace2.SpaceBeforeParens = FormatStyle::SBPO_Custom;
14383   SomeSpace2.SpaceBeforeParensOptions.BeforeNonEmptyParentheses = true;
14384   verifyFormat("[]() -> float {}", SomeSpace2);
14385   verifyFormat("[] (auto foo) {}", SomeSpace2);
14386   verifyFormat("[foo]() -> int {}", SomeSpace2);
14387   verifyFormat("int f();", SomeSpace2);
14388   verifyFormat("void f (int a, T b) {\n"
14389                "  while (true)\n"
14390                "    continue;\n"
14391                "}",
14392                SomeSpace2);
14393   verifyFormat("if (true)\n"
14394                "  f();\n"
14395                "else if (true)\n"
14396                "  f();",
14397                SomeSpace2);
14398   verifyFormat("do {\n"
14399                "  do_something();\n"
14400                "} while (something());",
14401                SomeSpace2);
14402   verifyFormat("switch (x) {\n"
14403                "default:\n"
14404                "  break;\n"
14405                "}",
14406                SomeSpace2);
14407   verifyFormat("A::A() : a (1) {}", SomeSpace2);
14408   verifyFormat("void f() __attribute__ ((asdf));", SomeSpace2);
14409   verifyFormat("*(&a + 1);\n"
14410                "&((&a)[1]);\n"
14411                "a[(b + c) * d];\n"
14412                "(((a + 1) * 2) + 3) * 4;",
14413                SomeSpace2);
14414   verifyFormat("#define A(x) x", SomeSpace2);
14415   verifyFormat("#define A (x) x", SomeSpace2);
14416   verifyFormat("#if defined(x)\n"
14417                "#endif",
14418                SomeSpace2);
14419   verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace2);
14420   verifyFormat("size_t x = sizeof (x);", SomeSpace2);
14421   verifyFormat("auto f (int x) -> decltype (x);", SomeSpace2);
14422   verifyFormat("auto f (int x) -> typeof (x);", SomeSpace2);
14423   verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace2);
14424   verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace2);
14425   verifyFormat("int f (T x) noexcept (x.create());", SomeSpace2);
14426   verifyFormat("alignas (128) char a[128];", SomeSpace2);
14427   verifyFormat("size_t x = alignof (MyType);", SomeSpace2);
14428   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");",
14429                SomeSpace2);
14430   verifyFormat("int f() throw (Deprecated);", SomeSpace2);
14431   verifyFormat("typedef void (*cb) (int);", SomeSpace2);
14432   verifyFormat("T A::operator()();", SomeSpace2);
14433   // verifyFormat("X A::operator++ (T);", SomeSpace2);
14434   verifyFormat("int x = int (y);", SomeSpace2);
14435   verifyFormat("auto lambda = []() { return 0; };", SomeSpace2);
14436 }
14437 
14438 TEST_F(FormatTest, SpaceAfterLogicalNot) {
14439   FormatStyle Spaces = getLLVMStyle();
14440   Spaces.SpaceAfterLogicalNot = true;
14441 
14442   verifyFormat("bool x = ! y", Spaces);
14443   verifyFormat("if (! isFailure())", Spaces);
14444   verifyFormat("if (! (a && b))", Spaces);
14445   verifyFormat("\"Error!\"", Spaces);
14446   verifyFormat("! ! x", Spaces);
14447 }
14448 
14449 TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
14450   FormatStyle Spaces = getLLVMStyle();
14451 
14452   Spaces.SpacesInParentheses = true;
14453   verifyFormat("do_something( ::globalVar );", Spaces);
14454   verifyFormat("call( x, y, z );", Spaces);
14455   verifyFormat("call();", Spaces);
14456   verifyFormat("std::function<void( int, int )> callback;", Spaces);
14457   verifyFormat("void inFunction() { std::function<void( int, int )> fct; }",
14458                Spaces);
14459   verifyFormat("while ( (bool)1 )\n"
14460                "  continue;",
14461                Spaces);
14462   verifyFormat("for ( ;; )\n"
14463                "  continue;",
14464                Spaces);
14465   verifyFormat("if ( true )\n"
14466                "  f();\n"
14467                "else if ( true )\n"
14468                "  f();",
14469                Spaces);
14470   verifyFormat("do {\n"
14471                "  do_something( (int)i );\n"
14472                "} while ( something() );",
14473                Spaces);
14474   verifyFormat("switch ( x ) {\n"
14475                "default:\n"
14476                "  break;\n"
14477                "}",
14478                Spaces);
14479 
14480   Spaces.SpacesInParentheses = false;
14481   Spaces.SpacesInCStyleCastParentheses = true;
14482   verifyFormat("Type *A = ( Type * )P;", Spaces);
14483   verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
14484   verifyFormat("x = ( int32 )y;", Spaces);
14485   verifyFormat("int a = ( int )(2.0f);", Spaces);
14486   verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
14487   verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
14488   verifyFormat("#define x (( int )-1)", Spaces);
14489 
14490   // Run the first set of tests again with:
14491   Spaces.SpacesInParentheses = false;
14492   Spaces.SpaceInEmptyParentheses = true;
14493   Spaces.SpacesInCStyleCastParentheses = true;
14494   verifyFormat("call(x, y, z);", Spaces);
14495   verifyFormat("call( );", Spaces);
14496   verifyFormat("std::function<void(int, int)> callback;", Spaces);
14497   verifyFormat("while (( bool )1)\n"
14498                "  continue;",
14499                Spaces);
14500   verifyFormat("for (;;)\n"
14501                "  continue;",
14502                Spaces);
14503   verifyFormat("if (true)\n"
14504                "  f( );\n"
14505                "else if (true)\n"
14506                "  f( );",
14507                Spaces);
14508   verifyFormat("do {\n"
14509                "  do_something(( int )i);\n"
14510                "} while (something( ));",
14511                Spaces);
14512   verifyFormat("switch (x) {\n"
14513                "default:\n"
14514                "  break;\n"
14515                "}",
14516                Spaces);
14517 
14518   // Run the first set of tests again with:
14519   Spaces.SpaceAfterCStyleCast = true;
14520   verifyFormat("call(x, y, z);", Spaces);
14521   verifyFormat("call( );", Spaces);
14522   verifyFormat("std::function<void(int, int)> callback;", Spaces);
14523   verifyFormat("while (( bool ) 1)\n"
14524                "  continue;",
14525                Spaces);
14526   verifyFormat("for (;;)\n"
14527                "  continue;",
14528                Spaces);
14529   verifyFormat("if (true)\n"
14530                "  f( );\n"
14531                "else if (true)\n"
14532                "  f( );",
14533                Spaces);
14534   verifyFormat("do {\n"
14535                "  do_something(( int ) i);\n"
14536                "} while (something( ));",
14537                Spaces);
14538   verifyFormat("switch (x) {\n"
14539                "default:\n"
14540                "  break;\n"
14541                "}",
14542                Spaces);
14543 
14544   // Run subset of tests again with:
14545   Spaces.SpacesInCStyleCastParentheses = false;
14546   Spaces.SpaceAfterCStyleCast = true;
14547   verifyFormat("while ((bool) 1)\n"
14548                "  continue;",
14549                Spaces);
14550   verifyFormat("do {\n"
14551                "  do_something((int) i);\n"
14552                "} while (something( ));",
14553                Spaces);
14554 
14555   verifyFormat("size_t idx = (size_t) (ptr - ((char *) file));", Spaces);
14556   verifyFormat("size_t idx = (size_t) a;", Spaces);
14557   verifyFormat("size_t idx = (size_t) (a - 1);", Spaces);
14558   verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces);
14559   verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces);
14560   verifyFormat("size_t idx = (*foo)(a - 1);", Spaces);
14561   verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces);
14562   Spaces.ColumnLimit = 80;
14563   Spaces.IndentWidth = 4;
14564   Spaces.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
14565   verifyFormat("void foo( ) {\n"
14566                "    size_t foo = (*(function))(\n"
14567                "        Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, "
14568                "BarrrrrrrrrrrrLong,\n"
14569                "        FoooooooooLooooong);\n"
14570                "}",
14571                Spaces);
14572   Spaces.SpaceAfterCStyleCast = false;
14573   verifyFormat("size_t idx = (size_t)(ptr - ((char *)file));", Spaces);
14574   verifyFormat("size_t idx = (size_t)a;", Spaces);
14575   verifyFormat("size_t idx = (size_t)(a - 1);", Spaces);
14576   verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces);
14577   verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces);
14578   verifyFormat("size_t idx = (*foo)(a - 1);", Spaces);
14579   verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces);
14580 
14581   verifyFormat("void foo( ) {\n"
14582                "    size_t foo = (*(function))(\n"
14583                "        Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, "
14584                "BarrrrrrrrrrrrLong,\n"
14585                "        FoooooooooLooooong);\n"
14586                "}",
14587                Spaces);
14588 }
14589 
14590 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
14591   verifyFormat("int a[5];");
14592   verifyFormat("a[3] += 42;");
14593 
14594   FormatStyle Spaces = getLLVMStyle();
14595   Spaces.SpacesInSquareBrackets = true;
14596   // Not lambdas.
14597   verifyFormat("int a[ 5 ];", Spaces);
14598   verifyFormat("a[ 3 ] += 42;", Spaces);
14599   verifyFormat("constexpr char hello[]{\"hello\"};", Spaces);
14600   verifyFormat("double &operator[](int i) { return 0; }\n"
14601                "int i;",
14602                Spaces);
14603   verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces);
14604   verifyFormat("int i = a[ a ][ a ]->f();", Spaces);
14605   verifyFormat("int i = (*b)[ a ]->f();", Spaces);
14606   // Lambdas.
14607   verifyFormat("int c = []() -> int { return 2; }();\n", Spaces);
14608   verifyFormat("return [ i, args... ] {};", Spaces);
14609   verifyFormat("int foo = [ &bar ]() {};", Spaces);
14610   verifyFormat("int foo = [ = ]() {};", Spaces);
14611   verifyFormat("int foo = [ & ]() {};", Spaces);
14612   verifyFormat("int foo = [ =, &bar ]() {};", Spaces);
14613   verifyFormat("int foo = [ &bar, = ]() {};", Spaces);
14614 }
14615 
14616 TEST_F(FormatTest, ConfigurableSpaceBeforeBrackets) {
14617   FormatStyle NoSpaceStyle = getLLVMStyle();
14618   verifyFormat("int a[5];", NoSpaceStyle);
14619   verifyFormat("a[3] += 42;", NoSpaceStyle);
14620 
14621   verifyFormat("int a[1];", NoSpaceStyle);
14622   verifyFormat("int 1 [a];", NoSpaceStyle);
14623   verifyFormat("int a[1][2];", NoSpaceStyle);
14624   verifyFormat("a[7] = 5;", NoSpaceStyle);
14625   verifyFormat("int a = (f())[23];", NoSpaceStyle);
14626   verifyFormat("f([] {})", NoSpaceStyle);
14627 
14628   FormatStyle Space = getLLVMStyle();
14629   Space.SpaceBeforeSquareBrackets = true;
14630   verifyFormat("int c = []() -> int { return 2; }();\n", Space);
14631   verifyFormat("return [i, args...] {};", Space);
14632 
14633   verifyFormat("int a [5];", Space);
14634   verifyFormat("a [3] += 42;", Space);
14635   verifyFormat("constexpr char hello []{\"hello\"};", Space);
14636   verifyFormat("double &operator[](int i) { return 0; }\n"
14637                "int i;",
14638                Space);
14639   verifyFormat("std::unique_ptr<int []> foo() {}", Space);
14640   verifyFormat("int i = a [a][a]->f();", Space);
14641   verifyFormat("int i = (*b) [a]->f();", Space);
14642 
14643   verifyFormat("int a [1];", Space);
14644   verifyFormat("int 1 [a];", Space);
14645   verifyFormat("int a [1][2];", Space);
14646   verifyFormat("a [7] = 5;", Space);
14647   verifyFormat("int a = (f()) [23];", Space);
14648   verifyFormat("f([] {})", Space);
14649 }
14650 
14651 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
14652   verifyFormat("int a = 5;");
14653   verifyFormat("a += 42;");
14654   verifyFormat("a or_eq 8;");
14655 
14656   FormatStyle Spaces = getLLVMStyle();
14657   Spaces.SpaceBeforeAssignmentOperators = false;
14658   verifyFormat("int a= 5;", Spaces);
14659   verifyFormat("a+= 42;", Spaces);
14660   verifyFormat("a or_eq 8;", Spaces);
14661 }
14662 
14663 TEST_F(FormatTest, ConfigurableSpaceBeforeColon) {
14664   verifyFormat("class Foo : public Bar {};");
14665   verifyFormat("Foo::Foo() : foo(1) {}");
14666   verifyFormat("for (auto a : b) {\n}");
14667   verifyFormat("int x = a ? b : c;");
14668   verifyFormat("{\n"
14669                "label0:\n"
14670                "  int x = 0;\n"
14671                "}");
14672   verifyFormat("switch (x) {\n"
14673                "case 1:\n"
14674                "default:\n"
14675                "}");
14676   verifyFormat("switch (allBraces) {\n"
14677                "case 1: {\n"
14678                "  break;\n"
14679                "}\n"
14680                "case 2: {\n"
14681                "  [[fallthrough]];\n"
14682                "}\n"
14683                "default: {\n"
14684                "  break;\n"
14685                "}\n"
14686                "}");
14687 
14688   FormatStyle CtorInitializerStyle = getLLVMStyleWithColumns(30);
14689   CtorInitializerStyle.SpaceBeforeCtorInitializerColon = false;
14690   verifyFormat("class Foo : public Bar {};", CtorInitializerStyle);
14691   verifyFormat("Foo::Foo(): foo(1) {}", CtorInitializerStyle);
14692   verifyFormat("for (auto a : b) {\n}", CtorInitializerStyle);
14693   verifyFormat("int x = a ? b : c;", CtorInitializerStyle);
14694   verifyFormat("{\n"
14695                "label1:\n"
14696                "  int x = 0;\n"
14697                "}",
14698                CtorInitializerStyle);
14699   verifyFormat("switch (x) {\n"
14700                "case 1:\n"
14701                "default:\n"
14702                "}",
14703                CtorInitializerStyle);
14704   verifyFormat("switch (allBraces) {\n"
14705                "case 1: {\n"
14706                "  break;\n"
14707                "}\n"
14708                "case 2: {\n"
14709                "  [[fallthrough]];\n"
14710                "}\n"
14711                "default: {\n"
14712                "  break;\n"
14713                "}\n"
14714                "}",
14715                CtorInitializerStyle);
14716   CtorInitializerStyle.BreakConstructorInitializers =
14717       FormatStyle::BCIS_AfterColon;
14718   verifyFormat("Fooooooooooo::Fooooooooooo():\n"
14719                "    aaaaaaaaaaaaaaaa(1),\n"
14720                "    bbbbbbbbbbbbbbbb(2) {}",
14721                CtorInitializerStyle);
14722   CtorInitializerStyle.BreakConstructorInitializers =
14723       FormatStyle::BCIS_BeforeComma;
14724   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
14725                "    : aaaaaaaaaaaaaaaa(1)\n"
14726                "    , bbbbbbbbbbbbbbbb(2) {}",
14727                CtorInitializerStyle);
14728   CtorInitializerStyle.BreakConstructorInitializers =
14729       FormatStyle::BCIS_BeforeColon;
14730   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
14731                "    : aaaaaaaaaaaaaaaa(1),\n"
14732                "      bbbbbbbbbbbbbbbb(2) {}",
14733                CtorInitializerStyle);
14734   CtorInitializerStyle.ConstructorInitializerIndentWidth = 0;
14735   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
14736                ": aaaaaaaaaaaaaaaa(1),\n"
14737                "  bbbbbbbbbbbbbbbb(2) {}",
14738                CtorInitializerStyle);
14739 
14740   FormatStyle InheritanceStyle = getLLVMStyleWithColumns(30);
14741   InheritanceStyle.SpaceBeforeInheritanceColon = false;
14742   verifyFormat("class Foo: public Bar {};", InheritanceStyle);
14743   verifyFormat("Foo::Foo() : foo(1) {}", InheritanceStyle);
14744   verifyFormat("for (auto a : b) {\n}", InheritanceStyle);
14745   verifyFormat("int x = a ? b : c;", InheritanceStyle);
14746   verifyFormat("{\n"
14747                "label2:\n"
14748                "  int x = 0;\n"
14749                "}",
14750                InheritanceStyle);
14751   verifyFormat("switch (x) {\n"
14752                "case 1:\n"
14753                "default:\n"
14754                "}",
14755                InheritanceStyle);
14756   verifyFormat("switch (allBraces) {\n"
14757                "case 1: {\n"
14758                "  break;\n"
14759                "}\n"
14760                "case 2: {\n"
14761                "  [[fallthrough]];\n"
14762                "}\n"
14763                "default: {\n"
14764                "  break;\n"
14765                "}\n"
14766                "}",
14767                InheritanceStyle);
14768   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterComma;
14769   verifyFormat("class Foooooooooooooooooooooo\n"
14770                "    : public aaaaaaaaaaaaaaaaaa,\n"
14771                "      public bbbbbbbbbbbbbbbbbb {\n"
14772                "}",
14773                InheritanceStyle);
14774   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterColon;
14775   verifyFormat("class Foooooooooooooooooooooo:\n"
14776                "    public aaaaaaaaaaaaaaaaaa,\n"
14777                "    public bbbbbbbbbbbbbbbbbb {\n"
14778                "}",
14779                InheritanceStyle);
14780   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeComma;
14781   verifyFormat("class Foooooooooooooooooooooo\n"
14782                "    : public aaaaaaaaaaaaaaaaaa\n"
14783                "    , public bbbbbbbbbbbbbbbbbb {\n"
14784                "}",
14785                InheritanceStyle);
14786   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
14787   verifyFormat("class Foooooooooooooooooooooo\n"
14788                "    : public aaaaaaaaaaaaaaaaaa,\n"
14789                "      public bbbbbbbbbbbbbbbbbb {\n"
14790                "}",
14791                InheritanceStyle);
14792   InheritanceStyle.ConstructorInitializerIndentWidth = 0;
14793   verifyFormat("class Foooooooooooooooooooooo\n"
14794                ": public aaaaaaaaaaaaaaaaaa,\n"
14795                "  public bbbbbbbbbbbbbbbbbb {}",
14796                InheritanceStyle);
14797 
14798   FormatStyle ForLoopStyle = getLLVMStyle();
14799   ForLoopStyle.SpaceBeforeRangeBasedForLoopColon = false;
14800   verifyFormat("class Foo : public Bar {};", ForLoopStyle);
14801   verifyFormat("Foo::Foo() : foo(1) {}", ForLoopStyle);
14802   verifyFormat("for (auto a: b) {\n}", ForLoopStyle);
14803   verifyFormat("int x = a ? b : c;", ForLoopStyle);
14804   verifyFormat("{\n"
14805                "label2:\n"
14806                "  int x = 0;\n"
14807                "}",
14808                ForLoopStyle);
14809   verifyFormat("switch (x) {\n"
14810                "case 1:\n"
14811                "default:\n"
14812                "}",
14813                ForLoopStyle);
14814   verifyFormat("switch (allBraces) {\n"
14815                "case 1: {\n"
14816                "  break;\n"
14817                "}\n"
14818                "case 2: {\n"
14819                "  [[fallthrough]];\n"
14820                "}\n"
14821                "default: {\n"
14822                "  break;\n"
14823                "}\n"
14824                "}",
14825                ForLoopStyle);
14826 
14827   FormatStyle CaseStyle = getLLVMStyle();
14828   CaseStyle.SpaceBeforeCaseColon = true;
14829   verifyFormat("class Foo : public Bar {};", CaseStyle);
14830   verifyFormat("Foo::Foo() : foo(1) {}", CaseStyle);
14831   verifyFormat("for (auto a : b) {\n}", CaseStyle);
14832   verifyFormat("int x = a ? b : c;", CaseStyle);
14833   verifyFormat("switch (x) {\n"
14834                "case 1 :\n"
14835                "default :\n"
14836                "}",
14837                CaseStyle);
14838   verifyFormat("switch (allBraces) {\n"
14839                "case 1 : {\n"
14840                "  break;\n"
14841                "}\n"
14842                "case 2 : {\n"
14843                "  [[fallthrough]];\n"
14844                "}\n"
14845                "default : {\n"
14846                "  break;\n"
14847                "}\n"
14848                "}",
14849                CaseStyle);
14850 
14851   FormatStyle NoSpaceStyle = getLLVMStyle();
14852   EXPECT_EQ(NoSpaceStyle.SpaceBeforeCaseColon, false);
14853   NoSpaceStyle.SpaceBeforeCtorInitializerColon = false;
14854   NoSpaceStyle.SpaceBeforeInheritanceColon = false;
14855   NoSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false;
14856   verifyFormat("class Foo: public Bar {};", NoSpaceStyle);
14857   verifyFormat("Foo::Foo(): foo(1) {}", NoSpaceStyle);
14858   verifyFormat("for (auto a: b) {\n}", NoSpaceStyle);
14859   verifyFormat("int x = a ? b : c;", NoSpaceStyle);
14860   verifyFormat("{\n"
14861                "label3:\n"
14862                "  int x = 0;\n"
14863                "}",
14864                NoSpaceStyle);
14865   verifyFormat("switch (x) {\n"
14866                "case 1:\n"
14867                "default:\n"
14868                "}",
14869                NoSpaceStyle);
14870   verifyFormat("switch (allBraces) {\n"
14871                "case 1: {\n"
14872                "  break;\n"
14873                "}\n"
14874                "case 2: {\n"
14875                "  [[fallthrough]];\n"
14876                "}\n"
14877                "default: {\n"
14878                "  break;\n"
14879                "}\n"
14880                "}",
14881                NoSpaceStyle);
14882 
14883   FormatStyle InvertedSpaceStyle = getLLVMStyle();
14884   InvertedSpaceStyle.SpaceBeforeCaseColon = true;
14885   InvertedSpaceStyle.SpaceBeforeCtorInitializerColon = false;
14886   InvertedSpaceStyle.SpaceBeforeInheritanceColon = false;
14887   InvertedSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false;
14888   verifyFormat("class Foo: public Bar {};", InvertedSpaceStyle);
14889   verifyFormat("Foo::Foo(): foo(1) {}", InvertedSpaceStyle);
14890   verifyFormat("for (auto a: b) {\n}", InvertedSpaceStyle);
14891   verifyFormat("int x = a ? b : c;", InvertedSpaceStyle);
14892   verifyFormat("{\n"
14893                "label3:\n"
14894                "  int x = 0;\n"
14895                "}",
14896                InvertedSpaceStyle);
14897   verifyFormat("switch (x) {\n"
14898                "case 1 :\n"
14899                "case 2 : {\n"
14900                "  break;\n"
14901                "}\n"
14902                "default :\n"
14903                "  break;\n"
14904                "}",
14905                InvertedSpaceStyle);
14906   verifyFormat("switch (allBraces) {\n"
14907                "case 1 : {\n"
14908                "  break;\n"
14909                "}\n"
14910                "case 2 : {\n"
14911                "  [[fallthrough]];\n"
14912                "}\n"
14913                "default : {\n"
14914                "  break;\n"
14915                "}\n"
14916                "}",
14917                InvertedSpaceStyle);
14918 }
14919 
14920 TEST_F(FormatTest, ConfigurableSpaceAroundPointerQualifiers) {
14921   FormatStyle Style = getLLVMStyle();
14922 
14923   Style.PointerAlignment = FormatStyle::PAS_Left;
14924   Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default;
14925   verifyFormat("void* const* x = NULL;", Style);
14926 
14927 #define verifyQualifierSpaces(Code, Pointers, Qualifiers)                      \
14928   do {                                                                         \
14929     Style.PointerAlignment = FormatStyle::Pointers;                            \
14930     Style.SpaceAroundPointerQualifiers = FormatStyle::Qualifiers;              \
14931     verifyFormat(Code, Style);                                                 \
14932   } while (false)
14933 
14934   verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Default);
14935   verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_Default);
14936   verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Default);
14937 
14938   verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Before);
14939   verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Before);
14940   verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Before);
14941 
14942   verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_After);
14943   verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_After);
14944   verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_After);
14945 
14946   verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_Both);
14947   verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Both);
14948   verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Both);
14949 
14950   verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Default);
14951   verifyQualifierSpaces("Foo::operator void const *();", PAS_Right,
14952                         SAPQ_Default);
14953   verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
14954                         SAPQ_Default);
14955 
14956   verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Before);
14957   verifyQualifierSpaces("Foo::operator void const *();", PAS_Right,
14958                         SAPQ_Before);
14959   verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
14960                         SAPQ_Before);
14961 
14962   verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_After);
14963   verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_After);
14964   verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
14965                         SAPQ_After);
14966 
14967   verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_Both);
14968   verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_Both);
14969   verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, SAPQ_Both);
14970 
14971 #undef verifyQualifierSpaces
14972 
14973   FormatStyle Spaces = getLLVMStyle();
14974   Spaces.AttributeMacros.push_back("qualified");
14975   Spaces.PointerAlignment = FormatStyle::PAS_Right;
14976   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default;
14977   verifyFormat("SomeType *volatile *a = NULL;", Spaces);
14978   verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces);
14979   verifyFormat("std::vector<SomeType *const *> x;", Spaces);
14980   verifyFormat("std::vector<SomeType *qualified *> x;", Spaces);
14981   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
14982   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before;
14983   verifyFormat("SomeType * volatile *a = NULL;", Spaces);
14984   verifyFormat("SomeType * __attribute__((attr)) *a = NULL;", Spaces);
14985   verifyFormat("std::vector<SomeType * const *> x;", Spaces);
14986   verifyFormat("std::vector<SomeType * qualified *> x;", Spaces);
14987   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
14988 
14989   // Check that SAPQ_Before doesn't result in extra spaces for PAS_Left.
14990   Spaces.PointerAlignment = FormatStyle::PAS_Left;
14991   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before;
14992   verifyFormat("SomeType* volatile* a = NULL;", Spaces);
14993   verifyFormat("SomeType* __attribute__((attr))* a = NULL;", Spaces);
14994   verifyFormat("std::vector<SomeType* const*> x;", Spaces);
14995   verifyFormat("std::vector<SomeType* qualified*> x;", Spaces);
14996   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
14997   // However, setting it to SAPQ_After should add spaces after __attribute, etc.
14998   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After;
14999   verifyFormat("SomeType* volatile * a = NULL;", Spaces);
15000   verifyFormat("SomeType* __attribute__((attr)) * a = NULL;", Spaces);
15001   verifyFormat("std::vector<SomeType* const *> x;", Spaces);
15002   verifyFormat("std::vector<SomeType* qualified *> x;", Spaces);
15003   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
15004 
15005   // PAS_Middle should not have any noticeable changes even for SAPQ_Both
15006   Spaces.PointerAlignment = FormatStyle::PAS_Middle;
15007   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After;
15008   verifyFormat("SomeType * volatile * a = NULL;", Spaces);
15009   verifyFormat("SomeType * __attribute__((attr)) * a = NULL;", Spaces);
15010   verifyFormat("std::vector<SomeType * const *> x;", Spaces);
15011   verifyFormat("std::vector<SomeType * qualified *> x;", Spaces);
15012   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
15013 }
15014 
15015 TEST_F(FormatTest, AlignConsecutiveMacros) {
15016   FormatStyle Style = getLLVMStyle();
15017   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
15018   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
15019   Style.AlignConsecutiveMacros = FormatStyle::ACS_None;
15020 
15021   verifyFormat("#define a 3\n"
15022                "#define bbbb 4\n"
15023                "#define ccc (5)",
15024                Style);
15025 
15026   verifyFormat("#define f(x) (x * x)\n"
15027                "#define fff(x, y, z) (x * y + z)\n"
15028                "#define ffff(x, y) (x - y)",
15029                Style);
15030 
15031   verifyFormat("#define foo(x, y) (x + y)\n"
15032                "#define bar (5, 6)(2 + 2)",
15033                Style);
15034 
15035   verifyFormat("#define a 3\n"
15036                "#define bbbb 4\n"
15037                "#define ccc (5)\n"
15038                "#define f(x) (x * x)\n"
15039                "#define fff(x, y, z) (x * y + z)\n"
15040                "#define ffff(x, y) (x - y)",
15041                Style);
15042 
15043   Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
15044   verifyFormat("#define a    3\n"
15045                "#define bbbb 4\n"
15046                "#define ccc  (5)",
15047                Style);
15048 
15049   verifyFormat("#define f(x)         (x * x)\n"
15050                "#define fff(x, y, z) (x * y + z)\n"
15051                "#define ffff(x, y)   (x - y)",
15052                Style);
15053 
15054   verifyFormat("#define foo(x, y) (x + y)\n"
15055                "#define bar       (5, 6)(2 + 2)",
15056                Style);
15057 
15058   verifyFormat("#define a            3\n"
15059                "#define bbbb         4\n"
15060                "#define ccc          (5)\n"
15061                "#define f(x)         (x * x)\n"
15062                "#define fff(x, y, z) (x * y + z)\n"
15063                "#define ffff(x, y)   (x - y)",
15064                Style);
15065 
15066   verifyFormat("#define a         5\n"
15067                "#define foo(x, y) (x + y)\n"
15068                "#define CCC       (6)\n"
15069                "auto lambda = []() {\n"
15070                "  auto  ii = 0;\n"
15071                "  float j  = 0;\n"
15072                "  return 0;\n"
15073                "};\n"
15074                "int   i  = 0;\n"
15075                "float i2 = 0;\n"
15076                "auto  v  = type{\n"
15077                "    i = 1,   //\n"
15078                "    (i = 2), //\n"
15079                "    i = 3    //\n"
15080                "};",
15081                Style);
15082 
15083   Style.AlignConsecutiveMacros = FormatStyle::ACS_None;
15084   Style.ColumnLimit = 20;
15085 
15086   verifyFormat("#define a          \\\n"
15087                "  \"aabbbbbbbbbbbb\"\n"
15088                "#define D          \\\n"
15089                "  \"aabbbbbbbbbbbb\" \\\n"
15090                "  \"ccddeeeeeeeee\"\n"
15091                "#define B          \\\n"
15092                "  \"QQQQQQQQQQQQQ\"  \\\n"
15093                "  \"FFFFFFFFFFFFF\"  \\\n"
15094                "  \"LLLLLLLL\"\n",
15095                Style);
15096 
15097   Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
15098   verifyFormat("#define a          \\\n"
15099                "  \"aabbbbbbbbbbbb\"\n"
15100                "#define D          \\\n"
15101                "  \"aabbbbbbbbbbbb\" \\\n"
15102                "  \"ccddeeeeeeeee\"\n"
15103                "#define B          \\\n"
15104                "  \"QQQQQQQQQQQQQ\"  \\\n"
15105                "  \"FFFFFFFFFFFFF\"  \\\n"
15106                "  \"LLLLLLLL\"\n",
15107                Style);
15108 
15109   // Test across comments
15110   Style.MaxEmptyLinesToKeep = 10;
15111   Style.ReflowComments = false;
15112   Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossComments;
15113   EXPECT_EQ("#define a    3\n"
15114             "// line comment\n"
15115             "#define bbbb 4\n"
15116             "#define ccc  (5)",
15117             format("#define a 3\n"
15118                    "// line comment\n"
15119                    "#define bbbb 4\n"
15120                    "#define ccc (5)",
15121                    Style));
15122 
15123   EXPECT_EQ("#define a    3\n"
15124             "/* block comment */\n"
15125             "#define bbbb 4\n"
15126             "#define ccc  (5)",
15127             format("#define a  3\n"
15128                    "/* block comment */\n"
15129                    "#define bbbb 4\n"
15130                    "#define ccc (5)",
15131                    Style));
15132 
15133   EXPECT_EQ("#define a    3\n"
15134             "/* multi-line *\n"
15135             " * block comment */\n"
15136             "#define bbbb 4\n"
15137             "#define ccc  (5)",
15138             format("#define a 3\n"
15139                    "/* multi-line *\n"
15140                    " * block comment */\n"
15141                    "#define bbbb 4\n"
15142                    "#define ccc (5)",
15143                    Style));
15144 
15145   EXPECT_EQ("#define a    3\n"
15146             "// multi-line line comment\n"
15147             "//\n"
15148             "#define bbbb 4\n"
15149             "#define ccc  (5)",
15150             format("#define a  3\n"
15151                    "// multi-line line comment\n"
15152                    "//\n"
15153                    "#define bbbb 4\n"
15154                    "#define ccc (5)",
15155                    Style));
15156 
15157   EXPECT_EQ("#define a 3\n"
15158             "// empty lines still break.\n"
15159             "\n"
15160             "#define bbbb 4\n"
15161             "#define ccc  (5)",
15162             format("#define a     3\n"
15163                    "// empty lines still break.\n"
15164                    "\n"
15165                    "#define bbbb     4\n"
15166                    "#define ccc  (5)",
15167                    Style));
15168 
15169   // Test across empty lines
15170   Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossEmptyLines;
15171   EXPECT_EQ("#define a    3\n"
15172             "\n"
15173             "#define bbbb 4\n"
15174             "#define ccc  (5)",
15175             format("#define a 3\n"
15176                    "\n"
15177                    "#define bbbb 4\n"
15178                    "#define ccc (5)",
15179                    Style));
15180 
15181   EXPECT_EQ("#define a    3\n"
15182             "\n"
15183             "\n"
15184             "\n"
15185             "#define bbbb 4\n"
15186             "#define ccc  (5)",
15187             format("#define a        3\n"
15188                    "\n"
15189                    "\n"
15190                    "\n"
15191                    "#define bbbb 4\n"
15192                    "#define ccc (5)",
15193                    Style));
15194 
15195   EXPECT_EQ("#define a 3\n"
15196             "// comments should break alignment\n"
15197             "//\n"
15198             "#define bbbb 4\n"
15199             "#define ccc  (5)",
15200             format("#define a        3\n"
15201                    "// comments should break alignment\n"
15202                    "//\n"
15203                    "#define bbbb 4\n"
15204                    "#define ccc (5)",
15205                    Style));
15206 
15207   // Test across empty lines and comments
15208   Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossEmptyLinesAndComments;
15209   verifyFormat("#define a    3\n"
15210                "\n"
15211                "// line comment\n"
15212                "#define bbbb 4\n"
15213                "#define ccc  (5)",
15214                Style);
15215 
15216   EXPECT_EQ("#define a    3\n"
15217             "\n"
15218             "\n"
15219             "/* multi-line *\n"
15220             " * block comment */\n"
15221             "\n"
15222             "\n"
15223             "#define bbbb 4\n"
15224             "#define ccc  (5)",
15225             format("#define a 3\n"
15226                    "\n"
15227                    "\n"
15228                    "/* multi-line *\n"
15229                    " * block comment */\n"
15230                    "\n"
15231                    "\n"
15232                    "#define bbbb 4\n"
15233                    "#define ccc (5)",
15234                    Style));
15235 
15236   EXPECT_EQ("#define a    3\n"
15237             "\n"
15238             "\n"
15239             "/* multi-line *\n"
15240             " * block comment */\n"
15241             "\n"
15242             "\n"
15243             "#define bbbb 4\n"
15244             "#define ccc  (5)",
15245             format("#define a 3\n"
15246                    "\n"
15247                    "\n"
15248                    "/* multi-line *\n"
15249                    " * block comment */\n"
15250                    "\n"
15251                    "\n"
15252                    "#define bbbb 4\n"
15253                    "#define ccc       (5)",
15254                    Style));
15255 }
15256 
15257 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLines) {
15258   FormatStyle Alignment = getLLVMStyle();
15259   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
15260   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_AcrossEmptyLines;
15261 
15262   Alignment.MaxEmptyLinesToKeep = 10;
15263   /* Test alignment across empty lines */
15264   EXPECT_EQ("int a           = 5;\n"
15265             "\n"
15266             "int oneTwoThree = 123;",
15267             format("int a       = 5;\n"
15268                    "\n"
15269                    "int oneTwoThree= 123;",
15270                    Alignment));
15271   EXPECT_EQ("int a           = 5;\n"
15272             "int one         = 1;\n"
15273             "\n"
15274             "int oneTwoThree = 123;",
15275             format("int a = 5;\n"
15276                    "int one = 1;\n"
15277                    "\n"
15278                    "int oneTwoThree = 123;",
15279                    Alignment));
15280   EXPECT_EQ("int a           = 5;\n"
15281             "int one         = 1;\n"
15282             "\n"
15283             "int oneTwoThree = 123;\n"
15284             "int oneTwo      = 12;",
15285             format("int a = 5;\n"
15286                    "int one = 1;\n"
15287                    "\n"
15288                    "int oneTwoThree = 123;\n"
15289                    "int oneTwo = 12;",
15290                    Alignment));
15291 
15292   /* Test across comments */
15293   EXPECT_EQ("int a = 5;\n"
15294             "/* block comment */\n"
15295             "int oneTwoThree = 123;",
15296             format("int a = 5;\n"
15297                    "/* block comment */\n"
15298                    "int oneTwoThree=123;",
15299                    Alignment));
15300 
15301   EXPECT_EQ("int a = 5;\n"
15302             "// line comment\n"
15303             "int oneTwoThree = 123;",
15304             format("int a = 5;\n"
15305                    "// line comment\n"
15306                    "int oneTwoThree=123;",
15307                    Alignment));
15308 
15309   /* Test across comments and newlines */
15310   EXPECT_EQ("int a = 5;\n"
15311             "\n"
15312             "/* block comment */\n"
15313             "int oneTwoThree = 123;",
15314             format("int a = 5;\n"
15315                    "\n"
15316                    "/* block comment */\n"
15317                    "int oneTwoThree=123;",
15318                    Alignment));
15319 
15320   EXPECT_EQ("int a = 5;\n"
15321             "\n"
15322             "// line comment\n"
15323             "int oneTwoThree = 123;",
15324             format("int a = 5;\n"
15325                    "\n"
15326                    "// line comment\n"
15327                    "int oneTwoThree=123;",
15328                    Alignment));
15329 }
15330 
15331 TEST_F(FormatTest, AlignConsecutiveDeclarationsAcrossEmptyLinesAndComments) {
15332   FormatStyle Alignment = getLLVMStyle();
15333   Alignment.AlignConsecutiveDeclarations =
15334       FormatStyle::ACS_AcrossEmptyLinesAndComments;
15335   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
15336 
15337   Alignment.MaxEmptyLinesToKeep = 10;
15338   /* Test alignment across empty lines */
15339   EXPECT_EQ("int         a = 5;\n"
15340             "\n"
15341             "float const oneTwoThree = 123;",
15342             format("int a = 5;\n"
15343                    "\n"
15344                    "float const oneTwoThree = 123;",
15345                    Alignment));
15346   EXPECT_EQ("int         a = 5;\n"
15347             "float const one = 1;\n"
15348             "\n"
15349             "int         oneTwoThree = 123;",
15350             format("int a = 5;\n"
15351                    "float const one = 1;\n"
15352                    "\n"
15353                    "int oneTwoThree = 123;",
15354                    Alignment));
15355 
15356   /* Test across comments */
15357   EXPECT_EQ("float const a = 5;\n"
15358             "/* block comment */\n"
15359             "int         oneTwoThree = 123;",
15360             format("float const a = 5;\n"
15361                    "/* block comment */\n"
15362                    "int oneTwoThree=123;",
15363                    Alignment));
15364 
15365   EXPECT_EQ("float const a = 5;\n"
15366             "// line comment\n"
15367             "int         oneTwoThree = 123;",
15368             format("float const a = 5;\n"
15369                    "// line comment\n"
15370                    "int oneTwoThree=123;",
15371                    Alignment));
15372 
15373   /* Test across comments and newlines */
15374   EXPECT_EQ("float const a = 5;\n"
15375             "\n"
15376             "/* block comment */\n"
15377             "int         oneTwoThree = 123;",
15378             format("float const a = 5;\n"
15379                    "\n"
15380                    "/* block comment */\n"
15381                    "int         oneTwoThree=123;",
15382                    Alignment));
15383 
15384   EXPECT_EQ("float const a = 5;\n"
15385             "\n"
15386             "// line comment\n"
15387             "int         oneTwoThree = 123;",
15388             format("float const a = 5;\n"
15389                    "\n"
15390                    "// line comment\n"
15391                    "int oneTwoThree=123;",
15392                    Alignment));
15393 }
15394 
15395 TEST_F(FormatTest, AlignConsecutiveBitFieldsAcrossEmptyLinesAndComments) {
15396   FormatStyle Alignment = getLLVMStyle();
15397   Alignment.AlignConsecutiveBitFields =
15398       FormatStyle::ACS_AcrossEmptyLinesAndComments;
15399 
15400   Alignment.MaxEmptyLinesToKeep = 10;
15401   /* Test alignment across empty lines */
15402   EXPECT_EQ("int a            : 5;\n"
15403             "\n"
15404             "int longbitfield : 6;",
15405             format("int a : 5;\n"
15406                    "\n"
15407                    "int longbitfield : 6;",
15408                    Alignment));
15409   EXPECT_EQ("int a            : 5;\n"
15410             "int one          : 1;\n"
15411             "\n"
15412             "int longbitfield : 6;",
15413             format("int a : 5;\n"
15414                    "int one : 1;\n"
15415                    "\n"
15416                    "int longbitfield : 6;",
15417                    Alignment));
15418 
15419   /* Test across comments */
15420   EXPECT_EQ("int a            : 5;\n"
15421             "/* block comment */\n"
15422             "int longbitfield : 6;",
15423             format("int a : 5;\n"
15424                    "/* block comment */\n"
15425                    "int longbitfield : 6;",
15426                    Alignment));
15427   EXPECT_EQ("int a            : 5;\n"
15428             "int one          : 1;\n"
15429             "// line comment\n"
15430             "int longbitfield : 6;",
15431             format("int a : 5;\n"
15432                    "int one : 1;\n"
15433                    "// line comment\n"
15434                    "int longbitfield : 6;",
15435                    Alignment));
15436 
15437   /* Test across comments and newlines */
15438   EXPECT_EQ("int a            : 5;\n"
15439             "/* block comment */\n"
15440             "\n"
15441             "int longbitfield : 6;",
15442             format("int a : 5;\n"
15443                    "/* block comment */\n"
15444                    "\n"
15445                    "int longbitfield : 6;",
15446                    Alignment));
15447   EXPECT_EQ("int a            : 5;\n"
15448             "int one          : 1;\n"
15449             "\n"
15450             "// line comment\n"
15451             "\n"
15452             "int longbitfield : 6;",
15453             format("int a : 5;\n"
15454                    "int one : 1;\n"
15455                    "\n"
15456                    "// line comment \n"
15457                    "\n"
15458                    "int longbitfield : 6;",
15459                    Alignment));
15460 }
15461 
15462 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossComments) {
15463   FormatStyle Alignment = getLLVMStyle();
15464   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
15465   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_AcrossComments;
15466 
15467   Alignment.MaxEmptyLinesToKeep = 10;
15468   /* Test alignment across empty lines */
15469   EXPECT_EQ("int a = 5;\n"
15470             "\n"
15471             "int oneTwoThree = 123;",
15472             format("int a       = 5;\n"
15473                    "\n"
15474                    "int oneTwoThree= 123;",
15475                    Alignment));
15476   EXPECT_EQ("int a   = 5;\n"
15477             "int one = 1;\n"
15478             "\n"
15479             "int oneTwoThree = 123;",
15480             format("int a = 5;\n"
15481                    "int one = 1;\n"
15482                    "\n"
15483                    "int oneTwoThree = 123;",
15484                    Alignment));
15485 
15486   /* Test across comments */
15487   EXPECT_EQ("int a           = 5;\n"
15488             "/* block comment */\n"
15489             "int oneTwoThree = 123;",
15490             format("int a = 5;\n"
15491                    "/* block comment */\n"
15492                    "int oneTwoThree=123;",
15493                    Alignment));
15494 
15495   EXPECT_EQ("int a           = 5;\n"
15496             "// line comment\n"
15497             "int oneTwoThree = 123;",
15498             format("int a = 5;\n"
15499                    "// line comment\n"
15500                    "int oneTwoThree=123;",
15501                    Alignment));
15502 
15503   EXPECT_EQ("int a           = 5;\n"
15504             "/*\n"
15505             " * multi-line block comment\n"
15506             " */\n"
15507             "int oneTwoThree = 123;",
15508             format("int a = 5;\n"
15509                    "/*\n"
15510                    " * multi-line block comment\n"
15511                    " */\n"
15512                    "int oneTwoThree=123;",
15513                    Alignment));
15514 
15515   EXPECT_EQ("int a           = 5;\n"
15516             "//\n"
15517             "// multi-line line comment\n"
15518             "//\n"
15519             "int oneTwoThree = 123;",
15520             format("int a = 5;\n"
15521                    "//\n"
15522                    "// multi-line line comment\n"
15523                    "//\n"
15524                    "int oneTwoThree=123;",
15525                    Alignment));
15526 
15527   /* Test across comments and newlines */
15528   EXPECT_EQ("int a = 5;\n"
15529             "\n"
15530             "/* block comment */\n"
15531             "int oneTwoThree = 123;",
15532             format("int a = 5;\n"
15533                    "\n"
15534                    "/* block comment */\n"
15535                    "int oneTwoThree=123;",
15536                    Alignment));
15537 
15538   EXPECT_EQ("int a = 5;\n"
15539             "\n"
15540             "// line comment\n"
15541             "int oneTwoThree = 123;",
15542             format("int a = 5;\n"
15543                    "\n"
15544                    "// line comment\n"
15545                    "int oneTwoThree=123;",
15546                    Alignment));
15547 }
15548 
15549 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLinesAndComments) {
15550   FormatStyle Alignment = getLLVMStyle();
15551   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
15552   Alignment.AlignConsecutiveAssignments =
15553       FormatStyle::ACS_AcrossEmptyLinesAndComments;
15554   verifyFormat("int a           = 5;\n"
15555                "int oneTwoThree = 123;",
15556                Alignment);
15557   verifyFormat("int a           = method();\n"
15558                "int oneTwoThree = 133;",
15559                Alignment);
15560   verifyFormat("a &= 5;\n"
15561                "bcd *= 5;\n"
15562                "ghtyf += 5;\n"
15563                "dvfvdb -= 5;\n"
15564                "a /= 5;\n"
15565                "vdsvsv %= 5;\n"
15566                "sfdbddfbdfbb ^= 5;\n"
15567                "dvsdsv |= 5;\n"
15568                "int dsvvdvsdvvv = 123;",
15569                Alignment);
15570   verifyFormat("int i = 1, j = 10;\n"
15571                "something = 2000;",
15572                Alignment);
15573   verifyFormat("something = 2000;\n"
15574                "int i = 1, j = 10;\n",
15575                Alignment);
15576   verifyFormat("something = 2000;\n"
15577                "another   = 911;\n"
15578                "int i = 1, j = 10;\n"
15579                "oneMore = 1;\n"
15580                "i       = 2;",
15581                Alignment);
15582   verifyFormat("int a   = 5;\n"
15583                "int one = 1;\n"
15584                "method();\n"
15585                "int oneTwoThree = 123;\n"
15586                "int oneTwo      = 12;",
15587                Alignment);
15588   verifyFormat("int oneTwoThree = 123;\n"
15589                "int oneTwo      = 12;\n"
15590                "method();\n",
15591                Alignment);
15592   verifyFormat("int oneTwoThree = 123; // comment\n"
15593                "int oneTwo      = 12;  // comment",
15594                Alignment);
15595 
15596   // Bug 25167
15597   /* Uncomment when fixed
15598     verifyFormat("#if A\n"
15599                  "#else\n"
15600                  "int aaaaaaaa = 12;\n"
15601                  "#endif\n"
15602                  "#if B\n"
15603                  "#else\n"
15604                  "int a = 12;\n"
15605                  "#endif\n",
15606                  Alignment);
15607     verifyFormat("enum foo {\n"
15608                  "#if A\n"
15609                  "#else\n"
15610                  "  aaaaaaaa = 12;\n"
15611                  "#endif\n"
15612                  "#if B\n"
15613                  "#else\n"
15614                  "  a = 12;\n"
15615                  "#endif\n"
15616                  "};\n",
15617                  Alignment);
15618   */
15619 
15620   Alignment.MaxEmptyLinesToKeep = 10;
15621   /* Test alignment across empty lines */
15622   EXPECT_EQ("int a           = 5;\n"
15623             "\n"
15624             "int oneTwoThree = 123;",
15625             format("int a       = 5;\n"
15626                    "\n"
15627                    "int oneTwoThree= 123;",
15628                    Alignment));
15629   EXPECT_EQ("int a           = 5;\n"
15630             "int one         = 1;\n"
15631             "\n"
15632             "int oneTwoThree = 123;",
15633             format("int a = 5;\n"
15634                    "int one = 1;\n"
15635                    "\n"
15636                    "int oneTwoThree = 123;",
15637                    Alignment));
15638   EXPECT_EQ("int a           = 5;\n"
15639             "int one         = 1;\n"
15640             "\n"
15641             "int oneTwoThree = 123;\n"
15642             "int oneTwo      = 12;",
15643             format("int a = 5;\n"
15644                    "int one = 1;\n"
15645                    "\n"
15646                    "int oneTwoThree = 123;\n"
15647                    "int oneTwo = 12;",
15648                    Alignment));
15649 
15650   /* Test across comments */
15651   EXPECT_EQ("int a           = 5;\n"
15652             "/* block comment */\n"
15653             "int oneTwoThree = 123;",
15654             format("int a = 5;\n"
15655                    "/* block comment */\n"
15656                    "int oneTwoThree=123;",
15657                    Alignment));
15658 
15659   EXPECT_EQ("int a           = 5;\n"
15660             "// line comment\n"
15661             "int oneTwoThree = 123;",
15662             format("int a = 5;\n"
15663                    "// line comment\n"
15664                    "int oneTwoThree=123;",
15665                    Alignment));
15666 
15667   /* Test across comments and newlines */
15668   EXPECT_EQ("int a           = 5;\n"
15669             "\n"
15670             "/* block comment */\n"
15671             "int oneTwoThree = 123;",
15672             format("int a = 5;\n"
15673                    "\n"
15674                    "/* block comment */\n"
15675                    "int oneTwoThree=123;",
15676                    Alignment));
15677 
15678   EXPECT_EQ("int a           = 5;\n"
15679             "\n"
15680             "// line comment\n"
15681             "int oneTwoThree = 123;",
15682             format("int a = 5;\n"
15683                    "\n"
15684                    "// line comment\n"
15685                    "int oneTwoThree=123;",
15686                    Alignment));
15687 
15688   EXPECT_EQ("int a           = 5;\n"
15689             "//\n"
15690             "// multi-line line comment\n"
15691             "//\n"
15692             "int oneTwoThree = 123;",
15693             format("int a = 5;\n"
15694                    "//\n"
15695                    "// multi-line line comment\n"
15696                    "//\n"
15697                    "int oneTwoThree=123;",
15698                    Alignment));
15699 
15700   EXPECT_EQ("int a           = 5;\n"
15701             "/*\n"
15702             " *  multi-line block comment\n"
15703             " */\n"
15704             "int oneTwoThree = 123;",
15705             format("int a = 5;\n"
15706                    "/*\n"
15707                    " *  multi-line block comment\n"
15708                    " */\n"
15709                    "int oneTwoThree=123;",
15710                    Alignment));
15711 
15712   EXPECT_EQ("int a           = 5;\n"
15713             "\n"
15714             "/* block comment */\n"
15715             "\n"
15716             "\n"
15717             "\n"
15718             "int oneTwoThree = 123;",
15719             format("int a = 5;\n"
15720                    "\n"
15721                    "/* block comment */\n"
15722                    "\n"
15723                    "\n"
15724                    "\n"
15725                    "int oneTwoThree=123;",
15726                    Alignment));
15727 
15728   EXPECT_EQ("int a           = 5;\n"
15729             "\n"
15730             "// line comment\n"
15731             "\n"
15732             "\n"
15733             "\n"
15734             "int oneTwoThree = 123;",
15735             format("int a = 5;\n"
15736                    "\n"
15737                    "// line comment\n"
15738                    "\n"
15739                    "\n"
15740                    "\n"
15741                    "int oneTwoThree=123;",
15742                    Alignment));
15743 
15744   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
15745   verifyFormat("#define A \\\n"
15746                "  int aaaa       = 12; \\\n"
15747                "  int b          = 23; \\\n"
15748                "  int ccc        = 234; \\\n"
15749                "  int dddddddddd = 2345;",
15750                Alignment);
15751   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
15752   verifyFormat("#define A               \\\n"
15753                "  int aaaa       = 12;  \\\n"
15754                "  int b          = 23;  \\\n"
15755                "  int ccc        = 234; \\\n"
15756                "  int dddddddddd = 2345;",
15757                Alignment);
15758   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
15759   verifyFormat("#define A                                                      "
15760                "                \\\n"
15761                "  int aaaa       = 12;                                         "
15762                "                \\\n"
15763                "  int b          = 23;                                         "
15764                "                \\\n"
15765                "  int ccc        = 234;                                        "
15766                "                \\\n"
15767                "  int dddddddddd = 2345;",
15768                Alignment);
15769   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
15770                "k = 4, int l = 5,\n"
15771                "                  int m = 6) {\n"
15772                "  int j      = 10;\n"
15773                "  otherThing = 1;\n"
15774                "}",
15775                Alignment);
15776   verifyFormat("void SomeFunction(int parameter = 0) {\n"
15777                "  int i   = 1;\n"
15778                "  int j   = 2;\n"
15779                "  int big = 10000;\n"
15780                "}",
15781                Alignment);
15782   verifyFormat("class C {\n"
15783                "public:\n"
15784                "  int i            = 1;\n"
15785                "  virtual void f() = 0;\n"
15786                "};",
15787                Alignment);
15788   verifyFormat("int i = 1;\n"
15789                "if (SomeType t = getSomething()) {\n"
15790                "}\n"
15791                "int j   = 2;\n"
15792                "int big = 10000;",
15793                Alignment);
15794   verifyFormat("int j = 7;\n"
15795                "for (int k = 0; k < N; ++k) {\n"
15796                "}\n"
15797                "int j   = 2;\n"
15798                "int big = 10000;\n"
15799                "}",
15800                Alignment);
15801   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
15802   verifyFormat("int i = 1;\n"
15803                "LooooooooooongType loooooooooooooooooooooongVariable\n"
15804                "    = someLooooooooooooooooongFunction();\n"
15805                "int j = 2;",
15806                Alignment);
15807   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
15808   verifyFormat("int i = 1;\n"
15809                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
15810                "    someLooooooooooooooooongFunction();\n"
15811                "int j = 2;",
15812                Alignment);
15813 
15814   verifyFormat("auto lambda = []() {\n"
15815                "  auto i = 0;\n"
15816                "  return 0;\n"
15817                "};\n"
15818                "int i  = 0;\n"
15819                "auto v = type{\n"
15820                "    i = 1,   //\n"
15821                "    (i = 2), //\n"
15822                "    i = 3    //\n"
15823                "};",
15824                Alignment);
15825 
15826   verifyFormat(
15827       "int i      = 1;\n"
15828       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
15829       "                          loooooooooooooooooooooongParameterB);\n"
15830       "int j      = 2;",
15831       Alignment);
15832 
15833   verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
15834                "          typename B   = very_long_type_name_1,\n"
15835                "          typename T_2 = very_long_type_name_2>\n"
15836                "auto foo() {}\n",
15837                Alignment);
15838   verifyFormat("int a, b = 1;\n"
15839                "int c  = 2;\n"
15840                "int dd = 3;\n",
15841                Alignment);
15842   verifyFormat("int aa       = ((1 > 2) ? 3 : 4);\n"
15843                "float b[1][] = {{3.f}};\n",
15844                Alignment);
15845   verifyFormat("for (int i = 0; i < 1; i++)\n"
15846                "  int x = 1;\n",
15847                Alignment);
15848   verifyFormat("for (i = 0; i < 1; i++)\n"
15849                "  x = 1;\n"
15850                "y = 1;\n",
15851                Alignment);
15852 
15853   Alignment.ReflowComments = true;
15854   Alignment.ColumnLimit = 50;
15855   EXPECT_EQ("int x   = 0;\n"
15856             "int yy  = 1; /// specificlennospace\n"
15857             "int zzz = 2;\n",
15858             format("int x   = 0;\n"
15859                    "int yy  = 1; ///specificlennospace\n"
15860                    "int zzz = 2;\n",
15861                    Alignment));
15862 }
15863 
15864 TEST_F(FormatTest, AlignConsecutiveAssignments) {
15865   FormatStyle Alignment = getLLVMStyle();
15866   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
15867   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
15868   verifyFormat("int a = 5;\n"
15869                "int oneTwoThree = 123;",
15870                Alignment);
15871   verifyFormat("int a = 5;\n"
15872                "int oneTwoThree = 123;",
15873                Alignment);
15874 
15875   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
15876   verifyFormat("int a           = 5;\n"
15877                "int oneTwoThree = 123;",
15878                Alignment);
15879   verifyFormat("int a           = method();\n"
15880                "int oneTwoThree = 133;",
15881                Alignment);
15882   verifyFormat("a &= 5;\n"
15883                "bcd *= 5;\n"
15884                "ghtyf += 5;\n"
15885                "dvfvdb -= 5;\n"
15886                "a /= 5;\n"
15887                "vdsvsv %= 5;\n"
15888                "sfdbddfbdfbb ^= 5;\n"
15889                "dvsdsv |= 5;\n"
15890                "int dsvvdvsdvvv = 123;",
15891                Alignment);
15892   verifyFormat("int i = 1, j = 10;\n"
15893                "something = 2000;",
15894                Alignment);
15895   verifyFormat("something = 2000;\n"
15896                "int i = 1, j = 10;\n",
15897                Alignment);
15898   verifyFormat("something = 2000;\n"
15899                "another   = 911;\n"
15900                "int i = 1, j = 10;\n"
15901                "oneMore = 1;\n"
15902                "i       = 2;",
15903                Alignment);
15904   verifyFormat("int a   = 5;\n"
15905                "int one = 1;\n"
15906                "method();\n"
15907                "int oneTwoThree = 123;\n"
15908                "int oneTwo      = 12;",
15909                Alignment);
15910   verifyFormat("int oneTwoThree = 123;\n"
15911                "int oneTwo      = 12;\n"
15912                "method();\n",
15913                Alignment);
15914   verifyFormat("int oneTwoThree = 123; // comment\n"
15915                "int oneTwo      = 12;  // comment",
15916                Alignment);
15917 
15918   // Bug 25167
15919   /* Uncomment when fixed
15920     verifyFormat("#if A\n"
15921                  "#else\n"
15922                  "int aaaaaaaa = 12;\n"
15923                  "#endif\n"
15924                  "#if B\n"
15925                  "#else\n"
15926                  "int a = 12;\n"
15927                  "#endif\n",
15928                  Alignment);
15929     verifyFormat("enum foo {\n"
15930                  "#if A\n"
15931                  "#else\n"
15932                  "  aaaaaaaa = 12;\n"
15933                  "#endif\n"
15934                  "#if B\n"
15935                  "#else\n"
15936                  "  a = 12;\n"
15937                  "#endif\n"
15938                  "};\n",
15939                  Alignment);
15940   */
15941 
15942   EXPECT_EQ("int a = 5;\n"
15943             "\n"
15944             "int oneTwoThree = 123;",
15945             format("int a       = 5;\n"
15946                    "\n"
15947                    "int oneTwoThree= 123;",
15948                    Alignment));
15949   EXPECT_EQ("int a   = 5;\n"
15950             "int one = 1;\n"
15951             "\n"
15952             "int oneTwoThree = 123;",
15953             format("int a = 5;\n"
15954                    "int one = 1;\n"
15955                    "\n"
15956                    "int oneTwoThree = 123;",
15957                    Alignment));
15958   EXPECT_EQ("int a   = 5;\n"
15959             "int one = 1;\n"
15960             "\n"
15961             "int oneTwoThree = 123;\n"
15962             "int oneTwo      = 12;",
15963             format("int a = 5;\n"
15964                    "int one = 1;\n"
15965                    "\n"
15966                    "int oneTwoThree = 123;\n"
15967                    "int oneTwo = 12;",
15968                    Alignment));
15969   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
15970   verifyFormat("#define A \\\n"
15971                "  int aaaa       = 12; \\\n"
15972                "  int b          = 23; \\\n"
15973                "  int ccc        = 234; \\\n"
15974                "  int dddddddddd = 2345;",
15975                Alignment);
15976   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
15977   verifyFormat("#define A               \\\n"
15978                "  int aaaa       = 12;  \\\n"
15979                "  int b          = 23;  \\\n"
15980                "  int ccc        = 234; \\\n"
15981                "  int dddddddddd = 2345;",
15982                Alignment);
15983   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
15984   verifyFormat("#define A                                                      "
15985                "                \\\n"
15986                "  int aaaa       = 12;                                         "
15987                "                \\\n"
15988                "  int b          = 23;                                         "
15989                "                \\\n"
15990                "  int ccc        = 234;                                        "
15991                "                \\\n"
15992                "  int dddddddddd = 2345;",
15993                Alignment);
15994   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
15995                "k = 4, int l = 5,\n"
15996                "                  int m = 6) {\n"
15997                "  int j      = 10;\n"
15998                "  otherThing = 1;\n"
15999                "}",
16000                Alignment);
16001   verifyFormat("void SomeFunction(int parameter = 0) {\n"
16002                "  int i   = 1;\n"
16003                "  int j   = 2;\n"
16004                "  int big = 10000;\n"
16005                "}",
16006                Alignment);
16007   verifyFormat("class C {\n"
16008                "public:\n"
16009                "  int i            = 1;\n"
16010                "  virtual void f() = 0;\n"
16011                "};",
16012                Alignment);
16013   verifyFormat("int i = 1;\n"
16014                "if (SomeType t = getSomething()) {\n"
16015                "}\n"
16016                "int j   = 2;\n"
16017                "int big = 10000;",
16018                Alignment);
16019   verifyFormat("int j = 7;\n"
16020                "for (int k = 0; k < N; ++k) {\n"
16021                "}\n"
16022                "int j   = 2;\n"
16023                "int big = 10000;\n"
16024                "}",
16025                Alignment);
16026   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
16027   verifyFormat("int i = 1;\n"
16028                "LooooooooooongType loooooooooooooooooooooongVariable\n"
16029                "    = someLooooooooooooooooongFunction();\n"
16030                "int j = 2;",
16031                Alignment);
16032   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
16033   verifyFormat("int i = 1;\n"
16034                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
16035                "    someLooooooooooooooooongFunction();\n"
16036                "int j = 2;",
16037                Alignment);
16038 
16039   verifyFormat("auto lambda = []() {\n"
16040                "  auto i = 0;\n"
16041                "  return 0;\n"
16042                "};\n"
16043                "int i  = 0;\n"
16044                "auto v = type{\n"
16045                "    i = 1,   //\n"
16046                "    (i = 2), //\n"
16047                "    i = 3    //\n"
16048                "};",
16049                Alignment);
16050 
16051   verifyFormat(
16052       "int i      = 1;\n"
16053       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
16054       "                          loooooooooooooooooooooongParameterB);\n"
16055       "int j      = 2;",
16056       Alignment);
16057 
16058   verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
16059                "          typename B   = very_long_type_name_1,\n"
16060                "          typename T_2 = very_long_type_name_2>\n"
16061                "auto foo() {}\n",
16062                Alignment);
16063   verifyFormat("int a, b = 1;\n"
16064                "int c  = 2;\n"
16065                "int dd = 3;\n",
16066                Alignment);
16067   verifyFormat("int aa       = ((1 > 2) ? 3 : 4);\n"
16068                "float b[1][] = {{3.f}};\n",
16069                Alignment);
16070   verifyFormat("for (int i = 0; i < 1; i++)\n"
16071                "  int x = 1;\n",
16072                Alignment);
16073   verifyFormat("for (i = 0; i < 1; i++)\n"
16074                "  x = 1;\n"
16075                "y = 1;\n",
16076                Alignment);
16077 
16078   Alignment.ReflowComments = true;
16079   Alignment.ColumnLimit = 50;
16080   EXPECT_EQ("int x   = 0;\n"
16081             "int yy  = 1; /// specificlennospace\n"
16082             "int zzz = 2;\n",
16083             format("int x   = 0;\n"
16084                    "int yy  = 1; ///specificlennospace\n"
16085                    "int zzz = 2;\n",
16086                    Alignment));
16087 }
16088 
16089 TEST_F(FormatTest, AlignConsecutiveBitFields) {
16090   FormatStyle Alignment = getLLVMStyle();
16091   Alignment.AlignConsecutiveBitFields = FormatStyle::ACS_Consecutive;
16092   verifyFormat("int const a     : 5;\n"
16093                "int oneTwoThree : 23;",
16094                Alignment);
16095 
16096   // Initializers are allowed starting with c++2a
16097   verifyFormat("int const a     : 5 = 1;\n"
16098                "int oneTwoThree : 23 = 0;",
16099                Alignment);
16100 
16101   Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
16102   verifyFormat("int const a           : 5;\n"
16103                "int       oneTwoThree : 23;",
16104                Alignment);
16105 
16106   verifyFormat("int const a           : 5;  // comment\n"
16107                "int       oneTwoThree : 23; // comment",
16108                Alignment);
16109 
16110   verifyFormat("int const a           : 5 = 1;\n"
16111                "int       oneTwoThree : 23 = 0;",
16112                Alignment);
16113 
16114   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16115   verifyFormat("int const a           : 5  = 1;\n"
16116                "int       oneTwoThree : 23 = 0;",
16117                Alignment);
16118   verifyFormat("int const a           : 5  = {1};\n"
16119                "int       oneTwoThree : 23 = 0;",
16120                Alignment);
16121 
16122   Alignment.BitFieldColonSpacing = FormatStyle::BFCS_None;
16123   verifyFormat("int const a          :5;\n"
16124                "int       oneTwoThree:23;",
16125                Alignment);
16126 
16127   Alignment.BitFieldColonSpacing = FormatStyle::BFCS_Before;
16128   verifyFormat("int const a           :5;\n"
16129                "int       oneTwoThree :23;",
16130                Alignment);
16131 
16132   Alignment.BitFieldColonSpacing = FormatStyle::BFCS_After;
16133   verifyFormat("int const a          : 5;\n"
16134                "int       oneTwoThree: 23;",
16135                Alignment);
16136 
16137   // Known limitations: ':' is only recognized as a bitfield colon when
16138   // followed by a number.
16139   /*
16140   verifyFormat("int oneTwoThree : SOME_CONSTANT;\n"
16141                "int a           : 5;",
16142                Alignment);
16143   */
16144 }
16145 
16146 TEST_F(FormatTest, AlignConsecutiveDeclarations) {
16147   FormatStyle Alignment = getLLVMStyle();
16148   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
16149   Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_None;
16150   Alignment.PointerAlignment = FormatStyle::PAS_Right;
16151   verifyFormat("float const a = 5;\n"
16152                "int oneTwoThree = 123;",
16153                Alignment);
16154   verifyFormat("int a = 5;\n"
16155                "float const oneTwoThree = 123;",
16156                Alignment);
16157 
16158   Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
16159   verifyFormat("float const a = 5;\n"
16160                "int         oneTwoThree = 123;",
16161                Alignment);
16162   verifyFormat("int         a = method();\n"
16163                "float const oneTwoThree = 133;",
16164                Alignment);
16165   verifyFormat("int i = 1, j = 10;\n"
16166                "something = 2000;",
16167                Alignment);
16168   verifyFormat("something = 2000;\n"
16169                "int i = 1, j = 10;\n",
16170                Alignment);
16171   verifyFormat("float      something = 2000;\n"
16172                "double     another = 911;\n"
16173                "int        i = 1, j = 10;\n"
16174                "const int *oneMore = 1;\n"
16175                "unsigned   i = 2;",
16176                Alignment);
16177   verifyFormat("float a = 5;\n"
16178                "int   one = 1;\n"
16179                "method();\n"
16180                "const double       oneTwoThree = 123;\n"
16181                "const unsigned int oneTwo = 12;",
16182                Alignment);
16183   verifyFormat("int      oneTwoThree{0}; // comment\n"
16184                "unsigned oneTwo;         // comment",
16185                Alignment);
16186   verifyFormat("unsigned int       *a;\n"
16187                "int                *b;\n"
16188                "unsigned int Const *c;\n"
16189                "unsigned int const *d;\n"
16190                "unsigned int Const &e;\n"
16191                "unsigned int const &f;",
16192                Alignment);
16193   verifyFormat("Const unsigned int *c;\n"
16194                "const unsigned int *d;\n"
16195                "Const unsigned int &e;\n"
16196                "const unsigned int &f;\n"
16197                "const unsigned      g;\n"
16198                "Const unsigned      h;",
16199                Alignment);
16200   EXPECT_EQ("float const a = 5;\n"
16201             "\n"
16202             "int oneTwoThree = 123;",
16203             format("float const   a = 5;\n"
16204                    "\n"
16205                    "int           oneTwoThree= 123;",
16206                    Alignment));
16207   EXPECT_EQ("float a = 5;\n"
16208             "int   one = 1;\n"
16209             "\n"
16210             "unsigned oneTwoThree = 123;",
16211             format("float    a = 5;\n"
16212                    "int      one = 1;\n"
16213                    "\n"
16214                    "unsigned oneTwoThree = 123;",
16215                    Alignment));
16216   EXPECT_EQ("float a = 5;\n"
16217             "int   one = 1;\n"
16218             "\n"
16219             "unsigned oneTwoThree = 123;\n"
16220             "int      oneTwo = 12;",
16221             format("float    a = 5;\n"
16222                    "int one = 1;\n"
16223                    "\n"
16224                    "unsigned oneTwoThree = 123;\n"
16225                    "int oneTwo = 12;",
16226                    Alignment));
16227   // Function prototype alignment
16228   verifyFormat("int    a();\n"
16229                "double b();",
16230                Alignment);
16231   verifyFormat("int    a(int x);\n"
16232                "double b();",
16233                Alignment);
16234   unsigned OldColumnLimit = Alignment.ColumnLimit;
16235   // We need to set ColumnLimit to zero, in order to stress nested alignments,
16236   // otherwise the function parameters will be re-flowed onto a single line.
16237   Alignment.ColumnLimit = 0;
16238   EXPECT_EQ("int    a(int   x,\n"
16239             "         float y);\n"
16240             "double b(int    x,\n"
16241             "         double y);",
16242             format("int a(int x,\n"
16243                    " float y);\n"
16244                    "double b(int x,\n"
16245                    " double y);",
16246                    Alignment));
16247   // This ensures that function parameters of function declarations are
16248   // correctly indented when their owning functions are indented.
16249   // The failure case here is for 'double y' to not be indented enough.
16250   EXPECT_EQ("double a(int x);\n"
16251             "int    b(int    y,\n"
16252             "         double z);",
16253             format("double a(int x);\n"
16254                    "int b(int y,\n"
16255                    " double z);",
16256                    Alignment));
16257   // Set ColumnLimit low so that we induce wrapping immediately after
16258   // the function name and opening paren.
16259   Alignment.ColumnLimit = 13;
16260   verifyFormat("int function(\n"
16261                "    int  x,\n"
16262                "    bool y);",
16263                Alignment);
16264   Alignment.ColumnLimit = OldColumnLimit;
16265   // Ensure function pointers don't screw up recursive alignment
16266   verifyFormat("int    a(int x, void (*fp)(int y));\n"
16267                "double b();",
16268                Alignment);
16269   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16270   // Ensure recursive alignment is broken by function braces, so that the
16271   // "a = 1" does not align with subsequent assignments inside the function
16272   // body.
16273   verifyFormat("int func(int a = 1) {\n"
16274                "  int b  = 2;\n"
16275                "  int cc = 3;\n"
16276                "}",
16277                Alignment);
16278   verifyFormat("float      something = 2000;\n"
16279                "double     another   = 911;\n"
16280                "int        i = 1, j = 10;\n"
16281                "const int *oneMore = 1;\n"
16282                "unsigned   i       = 2;",
16283                Alignment);
16284   verifyFormat("int      oneTwoThree = {0}; // comment\n"
16285                "unsigned oneTwo      = 0;   // comment",
16286                Alignment);
16287   // Make sure that scope is correctly tracked, in the absence of braces
16288   verifyFormat("for (int i = 0; i < n; i++)\n"
16289                "  j = i;\n"
16290                "double x = 1;\n",
16291                Alignment);
16292   verifyFormat("if (int i = 0)\n"
16293                "  j = i;\n"
16294                "double x = 1;\n",
16295                Alignment);
16296   // Ensure operator[] and operator() are comprehended
16297   verifyFormat("struct test {\n"
16298                "  long long int foo();\n"
16299                "  int           operator[](int a);\n"
16300                "  double        bar();\n"
16301                "};\n",
16302                Alignment);
16303   verifyFormat("struct test {\n"
16304                "  long long int foo();\n"
16305                "  int           operator()(int a);\n"
16306                "  double        bar();\n"
16307                "};\n",
16308                Alignment);
16309 
16310   // PAS_Right
16311   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
16312             "  int const i   = 1;\n"
16313             "  int      *j   = 2;\n"
16314             "  int       big = 10000;\n"
16315             "\n"
16316             "  unsigned oneTwoThree = 123;\n"
16317             "  int      oneTwo      = 12;\n"
16318             "  method();\n"
16319             "  float k  = 2;\n"
16320             "  int   ll = 10000;\n"
16321             "}",
16322             format("void SomeFunction(int parameter= 0) {\n"
16323                    " int const  i= 1;\n"
16324                    "  int *j=2;\n"
16325                    " int big  =  10000;\n"
16326                    "\n"
16327                    "unsigned oneTwoThree  =123;\n"
16328                    "int oneTwo = 12;\n"
16329                    "  method();\n"
16330                    "float k= 2;\n"
16331                    "int ll=10000;\n"
16332                    "}",
16333                    Alignment));
16334   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
16335             "  int const i   = 1;\n"
16336             "  int     **j   = 2, ***k;\n"
16337             "  int      &k   = i;\n"
16338             "  int     &&l   = i + j;\n"
16339             "  int       big = 10000;\n"
16340             "\n"
16341             "  unsigned oneTwoThree = 123;\n"
16342             "  int      oneTwo      = 12;\n"
16343             "  method();\n"
16344             "  float k  = 2;\n"
16345             "  int   ll = 10000;\n"
16346             "}",
16347             format("void SomeFunction(int parameter= 0) {\n"
16348                    " int const  i= 1;\n"
16349                    "  int **j=2,***k;\n"
16350                    "int &k=i;\n"
16351                    "int &&l=i+j;\n"
16352                    " int big  =  10000;\n"
16353                    "\n"
16354                    "unsigned oneTwoThree  =123;\n"
16355                    "int oneTwo = 12;\n"
16356                    "  method();\n"
16357                    "float k= 2;\n"
16358                    "int ll=10000;\n"
16359                    "}",
16360                    Alignment));
16361   // variables are aligned at their name, pointers are at the right most
16362   // position
16363   verifyFormat("int   *a;\n"
16364                "int  **b;\n"
16365                "int ***c;\n"
16366                "int    foobar;\n",
16367                Alignment);
16368 
16369   // PAS_Left
16370   FormatStyle AlignmentLeft = Alignment;
16371   AlignmentLeft.PointerAlignment = FormatStyle::PAS_Left;
16372   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
16373             "  int const i   = 1;\n"
16374             "  int*      j   = 2;\n"
16375             "  int       big = 10000;\n"
16376             "\n"
16377             "  unsigned oneTwoThree = 123;\n"
16378             "  int      oneTwo      = 12;\n"
16379             "  method();\n"
16380             "  float k  = 2;\n"
16381             "  int   ll = 10000;\n"
16382             "}",
16383             format("void SomeFunction(int parameter= 0) {\n"
16384                    " int const  i= 1;\n"
16385                    "  int *j=2;\n"
16386                    " int big  =  10000;\n"
16387                    "\n"
16388                    "unsigned oneTwoThree  =123;\n"
16389                    "int oneTwo = 12;\n"
16390                    "  method();\n"
16391                    "float k= 2;\n"
16392                    "int ll=10000;\n"
16393                    "}",
16394                    AlignmentLeft));
16395   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
16396             "  int const i   = 1;\n"
16397             "  int**     j   = 2;\n"
16398             "  int&      k   = i;\n"
16399             "  int&&     l   = i + j;\n"
16400             "  int       big = 10000;\n"
16401             "\n"
16402             "  unsigned oneTwoThree = 123;\n"
16403             "  int      oneTwo      = 12;\n"
16404             "  method();\n"
16405             "  float k  = 2;\n"
16406             "  int   ll = 10000;\n"
16407             "}",
16408             format("void SomeFunction(int parameter= 0) {\n"
16409                    " int const  i= 1;\n"
16410                    "  int **j=2;\n"
16411                    "int &k=i;\n"
16412                    "int &&l=i+j;\n"
16413                    " int big  =  10000;\n"
16414                    "\n"
16415                    "unsigned oneTwoThree  =123;\n"
16416                    "int oneTwo = 12;\n"
16417                    "  method();\n"
16418                    "float k= 2;\n"
16419                    "int ll=10000;\n"
16420                    "}",
16421                    AlignmentLeft));
16422   // variables are aligned at their name, pointers are at the left most position
16423   verifyFormat("int*   a;\n"
16424                "int**  b;\n"
16425                "int*** c;\n"
16426                "int    foobar;\n",
16427                AlignmentLeft);
16428 
16429   // PAS_Middle
16430   FormatStyle AlignmentMiddle = Alignment;
16431   AlignmentMiddle.PointerAlignment = FormatStyle::PAS_Middle;
16432   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
16433             "  int const i   = 1;\n"
16434             "  int *     j   = 2;\n"
16435             "  int       big = 10000;\n"
16436             "\n"
16437             "  unsigned oneTwoThree = 123;\n"
16438             "  int      oneTwo      = 12;\n"
16439             "  method();\n"
16440             "  float k  = 2;\n"
16441             "  int   ll = 10000;\n"
16442             "}",
16443             format("void SomeFunction(int parameter= 0) {\n"
16444                    " int const  i= 1;\n"
16445                    "  int *j=2;\n"
16446                    " int big  =  10000;\n"
16447                    "\n"
16448                    "unsigned oneTwoThree  =123;\n"
16449                    "int oneTwo = 12;\n"
16450                    "  method();\n"
16451                    "float k= 2;\n"
16452                    "int ll=10000;\n"
16453                    "}",
16454                    AlignmentMiddle));
16455   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
16456             "  int const i   = 1;\n"
16457             "  int **    j   = 2, ***k;\n"
16458             "  int &     k   = i;\n"
16459             "  int &&    l   = i + j;\n"
16460             "  int       big = 10000;\n"
16461             "\n"
16462             "  unsigned oneTwoThree = 123;\n"
16463             "  int      oneTwo      = 12;\n"
16464             "  method();\n"
16465             "  float k  = 2;\n"
16466             "  int   ll = 10000;\n"
16467             "}",
16468             format("void SomeFunction(int parameter= 0) {\n"
16469                    " int const  i= 1;\n"
16470                    "  int **j=2,***k;\n"
16471                    "int &k=i;\n"
16472                    "int &&l=i+j;\n"
16473                    " int big  =  10000;\n"
16474                    "\n"
16475                    "unsigned oneTwoThree  =123;\n"
16476                    "int oneTwo = 12;\n"
16477                    "  method();\n"
16478                    "float k= 2;\n"
16479                    "int ll=10000;\n"
16480                    "}",
16481                    AlignmentMiddle));
16482   // variables are aligned at their name, pointers are in the middle
16483   verifyFormat("int *   a;\n"
16484                "int *   b;\n"
16485                "int *** c;\n"
16486                "int     foobar;\n",
16487                AlignmentMiddle);
16488 
16489   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
16490   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
16491   verifyFormat("#define A \\\n"
16492                "  int       aaaa = 12; \\\n"
16493                "  float     b = 23; \\\n"
16494                "  const int ccc = 234; \\\n"
16495                "  unsigned  dddddddddd = 2345;",
16496                Alignment);
16497   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
16498   verifyFormat("#define A              \\\n"
16499                "  int       aaaa = 12; \\\n"
16500                "  float     b = 23;    \\\n"
16501                "  const int ccc = 234; \\\n"
16502                "  unsigned  dddddddddd = 2345;",
16503                Alignment);
16504   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
16505   Alignment.ColumnLimit = 30;
16506   verifyFormat("#define A                    \\\n"
16507                "  int       aaaa = 12;       \\\n"
16508                "  float     b = 23;          \\\n"
16509                "  const int ccc = 234;       \\\n"
16510                "  int       dddddddddd = 2345;",
16511                Alignment);
16512   Alignment.ColumnLimit = 80;
16513   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
16514                "k = 4, int l = 5,\n"
16515                "                  int m = 6) {\n"
16516                "  const int j = 10;\n"
16517                "  otherThing = 1;\n"
16518                "}",
16519                Alignment);
16520   verifyFormat("void SomeFunction(int parameter = 0) {\n"
16521                "  int const i = 1;\n"
16522                "  int      *j = 2;\n"
16523                "  int       big = 10000;\n"
16524                "}",
16525                Alignment);
16526   verifyFormat("class C {\n"
16527                "public:\n"
16528                "  int          i = 1;\n"
16529                "  virtual void f() = 0;\n"
16530                "};",
16531                Alignment);
16532   verifyFormat("float i = 1;\n"
16533                "if (SomeType t = getSomething()) {\n"
16534                "}\n"
16535                "const unsigned j = 2;\n"
16536                "int            big = 10000;",
16537                Alignment);
16538   verifyFormat("float j = 7;\n"
16539                "for (int k = 0; k < N; ++k) {\n"
16540                "}\n"
16541                "unsigned j = 2;\n"
16542                "int      big = 10000;\n"
16543                "}",
16544                Alignment);
16545   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
16546   verifyFormat("float              i = 1;\n"
16547                "LooooooooooongType loooooooooooooooooooooongVariable\n"
16548                "    = someLooooooooooooooooongFunction();\n"
16549                "int j = 2;",
16550                Alignment);
16551   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
16552   verifyFormat("int                i = 1;\n"
16553                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
16554                "    someLooooooooooooooooongFunction();\n"
16555                "int j = 2;",
16556                Alignment);
16557 
16558   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16559   verifyFormat("auto lambda = []() {\n"
16560                "  auto  ii = 0;\n"
16561                "  float j  = 0;\n"
16562                "  return 0;\n"
16563                "};\n"
16564                "int   i  = 0;\n"
16565                "float i2 = 0;\n"
16566                "auto  v  = type{\n"
16567                "    i = 1,   //\n"
16568                "    (i = 2), //\n"
16569                "    i = 3    //\n"
16570                "};",
16571                Alignment);
16572   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
16573 
16574   verifyFormat(
16575       "int      i = 1;\n"
16576       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
16577       "                          loooooooooooooooooooooongParameterB);\n"
16578       "int      j = 2;",
16579       Alignment);
16580 
16581   // Test interactions with ColumnLimit and AlignConsecutiveAssignments:
16582   // We expect declarations and assignments to align, as long as it doesn't
16583   // exceed the column limit, starting a new alignment sequence whenever it
16584   // happens.
16585   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16586   Alignment.ColumnLimit = 30;
16587   verifyFormat("float    ii              = 1;\n"
16588                "unsigned j               = 2;\n"
16589                "int someVerylongVariable = 1;\n"
16590                "AnotherLongType  ll = 123456;\n"
16591                "VeryVeryLongType k  = 2;\n"
16592                "int              myvar = 1;",
16593                Alignment);
16594   Alignment.ColumnLimit = 80;
16595   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
16596 
16597   verifyFormat(
16598       "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n"
16599       "          typename LongType, typename B>\n"
16600       "auto foo() {}\n",
16601       Alignment);
16602   verifyFormat("float a, b = 1;\n"
16603                "int   c = 2;\n"
16604                "int   dd = 3;\n",
16605                Alignment);
16606   verifyFormat("int   aa = ((1 > 2) ? 3 : 4);\n"
16607                "float b[1][] = {{3.f}};\n",
16608                Alignment);
16609   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16610   verifyFormat("float a, b = 1;\n"
16611                "int   c  = 2;\n"
16612                "int   dd = 3;\n",
16613                Alignment);
16614   verifyFormat("int   aa     = ((1 > 2) ? 3 : 4);\n"
16615                "float b[1][] = {{3.f}};\n",
16616                Alignment);
16617   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
16618 
16619   Alignment.ColumnLimit = 30;
16620   Alignment.BinPackParameters = false;
16621   verifyFormat("void foo(float     a,\n"
16622                "         float     b,\n"
16623                "         int       c,\n"
16624                "         uint32_t *d) {\n"
16625                "  int   *e = 0;\n"
16626                "  float  f = 0;\n"
16627                "  double g = 0;\n"
16628                "}\n"
16629                "void bar(ino_t     a,\n"
16630                "         int       b,\n"
16631                "         uint32_t *c,\n"
16632                "         bool      d) {}\n",
16633                Alignment);
16634   Alignment.BinPackParameters = true;
16635   Alignment.ColumnLimit = 80;
16636 
16637   // Bug 33507
16638   Alignment.PointerAlignment = FormatStyle::PAS_Middle;
16639   verifyFormat(
16640       "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n"
16641       "  static const Version verVs2017;\n"
16642       "  return true;\n"
16643       "});\n",
16644       Alignment);
16645   Alignment.PointerAlignment = FormatStyle::PAS_Right;
16646 
16647   // See llvm.org/PR35641
16648   Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
16649   verifyFormat("int func() { //\n"
16650                "  int      b;\n"
16651                "  unsigned c;\n"
16652                "}",
16653                Alignment);
16654 
16655   // See PR37175
16656   FormatStyle Style = getMozillaStyle();
16657   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
16658   EXPECT_EQ("DECOR1 /**/ int8_t /**/ DECOR2 /**/\n"
16659             "foo(int a);",
16660             format("DECOR1 /**/ int8_t /**/ DECOR2 /**/ foo (int a);", Style));
16661 
16662   Alignment.PointerAlignment = FormatStyle::PAS_Left;
16663   verifyFormat("unsigned int*       a;\n"
16664                "int*                b;\n"
16665                "unsigned int Const* c;\n"
16666                "unsigned int const* d;\n"
16667                "unsigned int Const& e;\n"
16668                "unsigned int const& f;",
16669                Alignment);
16670   verifyFormat("Const unsigned int* c;\n"
16671                "const unsigned int* d;\n"
16672                "Const unsigned int& e;\n"
16673                "const unsigned int& f;\n"
16674                "const unsigned      g;\n"
16675                "Const unsigned      h;",
16676                Alignment);
16677 
16678   Alignment.PointerAlignment = FormatStyle::PAS_Middle;
16679   verifyFormat("unsigned int *       a;\n"
16680                "int *                b;\n"
16681                "unsigned int Const * c;\n"
16682                "unsigned int const * d;\n"
16683                "unsigned int Const & e;\n"
16684                "unsigned int const & f;",
16685                Alignment);
16686   verifyFormat("Const unsigned int * c;\n"
16687                "const unsigned int * d;\n"
16688                "Const unsigned int & e;\n"
16689                "const unsigned int & f;\n"
16690                "const unsigned       g;\n"
16691                "Const unsigned       h;",
16692                Alignment);
16693 }
16694 
16695 TEST_F(FormatTest, AlignWithLineBreaks) {
16696   auto Style = getLLVMStyleWithColumns(120);
16697 
16698   EXPECT_EQ(Style.AlignConsecutiveAssignments, FormatStyle::ACS_None);
16699   EXPECT_EQ(Style.AlignConsecutiveDeclarations, FormatStyle::ACS_None);
16700   verifyFormat("void foo() {\n"
16701                "  int myVar = 5;\n"
16702                "  double x = 3.14;\n"
16703                "  auto str = \"Hello \"\n"
16704                "             \"World\";\n"
16705                "  auto s = \"Hello \"\n"
16706                "           \"Again\";\n"
16707                "}",
16708                Style);
16709 
16710   // clang-format off
16711   verifyFormat("void foo() {\n"
16712                "  const int capacityBefore = Entries.capacity();\n"
16713                "  const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
16714                "                                            std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
16715                "  const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
16716                "                                          std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
16717                "}",
16718                Style);
16719   // clang-format on
16720 
16721   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16722   verifyFormat("void foo() {\n"
16723                "  int myVar = 5;\n"
16724                "  double x  = 3.14;\n"
16725                "  auto str  = \"Hello \"\n"
16726                "              \"World\";\n"
16727                "  auto s    = \"Hello \"\n"
16728                "              \"Again\";\n"
16729                "}",
16730                Style);
16731 
16732   // clang-format off
16733   verifyFormat("void foo() {\n"
16734                "  const int capacityBefore = Entries.capacity();\n"
16735                "  const auto newEntry      = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
16736                "                                                 std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
16737                "  const X newEntry2        = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
16738                "                                                 std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
16739                "}",
16740                Style);
16741   // clang-format on
16742 
16743   Style.AlignConsecutiveAssignments = FormatStyle::ACS_None;
16744   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
16745   verifyFormat("void foo() {\n"
16746                "  int    myVar = 5;\n"
16747                "  double x = 3.14;\n"
16748                "  auto   str = \"Hello \"\n"
16749                "               \"World\";\n"
16750                "  auto   s = \"Hello \"\n"
16751                "             \"Again\";\n"
16752                "}",
16753                Style);
16754 
16755   // clang-format off
16756   verifyFormat("void foo() {\n"
16757                "  const int  capacityBefore = Entries.capacity();\n"
16758                "  const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
16759                "                                            std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
16760                "  const X    newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
16761                "                                             std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
16762                "}",
16763                Style);
16764   // clang-format on
16765 
16766   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16767   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
16768 
16769   verifyFormat("void foo() {\n"
16770                "  int    myVar = 5;\n"
16771                "  double x     = 3.14;\n"
16772                "  auto   str   = \"Hello \"\n"
16773                "                 \"World\";\n"
16774                "  auto   s     = \"Hello \"\n"
16775                "                 \"Again\";\n"
16776                "}",
16777                Style);
16778 
16779   // clang-format off
16780   verifyFormat("void foo() {\n"
16781                "  const int  capacityBefore = Entries.capacity();\n"
16782                "  const auto newEntry       = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
16783                "                                                  std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
16784                "  const X    newEntry2      = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
16785                "                                                  std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
16786                "}",
16787                Style);
16788   // clang-format on
16789 
16790   Style = getLLVMStyleWithColumns(120);
16791   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16792   Style.ContinuationIndentWidth = 4;
16793   Style.IndentWidth = 4;
16794 
16795   // clang-format off
16796   verifyFormat("void SomeFunc() {\n"
16797                "    newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n"
16798                "                                                        seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
16799                "    newWatcher.maxAge     = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n"
16800                "                                                        seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
16801                "    newWatcher.max        = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n"
16802                "                                                        seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
16803                "}",
16804                Style);
16805   // clang-format on
16806 
16807   Style.BinPackArguments = false;
16808 
16809   // clang-format off
16810   verifyFormat("void SomeFunc() {\n"
16811                "    newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(\n"
16812                "        FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
16813                "    newWatcher.maxAge     = ToLegacyTimestamp(GetMaxAge(\n"
16814                "        FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
16815                "    newWatcher.max        = ToLegacyTimestamp(GetMaxAge(\n"
16816                "        FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
16817                "}",
16818                Style);
16819   // clang-format on
16820 }
16821 
16822 TEST_F(FormatTest, AlignWithInitializerPeriods) {
16823   auto Style = getLLVMStyleWithColumns(60);
16824 
16825   verifyFormat("void foo1(void) {\n"
16826                "  BYTE p[1] = 1;\n"
16827                "  A B = {.one_foooooooooooooooo = 2,\n"
16828                "         .two_fooooooooooooo = 3,\n"
16829                "         .three_fooooooooooooo = 4};\n"
16830                "  BYTE payload = 2;\n"
16831                "}",
16832                Style);
16833 
16834   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16835   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_None;
16836   verifyFormat("void foo2(void) {\n"
16837                "  BYTE p[1]    = 1;\n"
16838                "  A B          = {.one_foooooooooooooooo = 2,\n"
16839                "                  .two_fooooooooooooo    = 3,\n"
16840                "                  .three_fooooooooooooo  = 4};\n"
16841                "  BYTE payload = 2;\n"
16842                "}",
16843                Style);
16844 
16845   Style.AlignConsecutiveAssignments = FormatStyle::ACS_None;
16846   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
16847   verifyFormat("void foo3(void) {\n"
16848                "  BYTE p[1] = 1;\n"
16849                "  A    B = {.one_foooooooooooooooo = 2,\n"
16850                "            .two_fooooooooooooo = 3,\n"
16851                "            .three_fooooooooooooo = 4};\n"
16852                "  BYTE payload = 2;\n"
16853                "}",
16854                Style);
16855 
16856   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16857   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
16858   verifyFormat("void foo4(void) {\n"
16859                "  BYTE p[1]    = 1;\n"
16860                "  A    B       = {.one_foooooooooooooooo = 2,\n"
16861                "                  .two_fooooooooooooo    = 3,\n"
16862                "                  .three_fooooooooooooo  = 4};\n"
16863                "  BYTE payload = 2;\n"
16864                "}",
16865                Style);
16866 }
16867 
16868 TEST_F(FormatTest, LinuxBraceBreaking) {
16869   FormatStyle LinuxBraceStyle = getLLVMStyle();
16870   LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
16871   verifyFormat("namespace a\n"
16872                "{\n"
16873                "class A\n"
16874                "{\n"
16875                "  void f()\n"
16876                "  {\n"
16877                "    if (true) {\n"
16878                "      a();\n"
16879                "      b();\n"
16880                "    } else {\n"
16881                "      a();\n"
16882                "    }\n"
16883                "  }\n"
16884                "  void g() { return; }\n"
16885                "};\n"
16886                "struct B {\n"
16887                "  int x;\n"
16888                "};\n"
16889                "} // namespace a\n",
16890                LinuxBraceStyle);
16891   verifyFormat("enum X {\n"
16892                "  Y = 0,\n"
16893                "}\n",
16894                LinuxBraceStyle);
16895   verifyFormat("struct S {\n"
16896                "  int Type;\n"
16897                "  union {\n"
16898                "    int x;\n"
16899                "    double y;\n"
16900                "  } Value;\n"
16901                "  class C\n"
16902                "  {\n"
16903                "    MyFavoriteType Value;\n"
16904                "  } Class;\n"
16905                "}\n",
16906                LinuxBraceStyle);
16907 }
16908 
16909 TEST_F(FormatTest, MozillaBraceBreaking) {
16910   FormatStyle MozillaBraceStyle = getLLVMStyle();
16911   MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
16912   MozillaBraceStyle.FixNamespaceComments = false;
16913   verifyFormat("namespace a {\n"
16914                "class A\n"
16915                "{\n"
16916                "  void f()\n"
16917                "  {\n"
16918                "    if (true) {\n"
16919                "      a();\n"
16920                "      b();\n"
16921                "    }\n"
16922                "  }\n"
16923                "  void g() { return; }\n"
16924                "};\n"
16925                "enum E\n"
16926                "{\n"
16927                "  A,\n"
16928                "  // foo\n"
16929                "  B,\n"
16930                "  C\n"
16931                "};\n"
16932                "struct B\n"
16933                "{\n"
16934                "  int x;\n"
16935                "};\n"
16936                "}\n",
16937                MozillaBraceStyle);
16938   verifyFormat("struct S\n"
16939                "{\n"
16940                "  int Type;\n"
16941                "  union\n"
16942                "  {\n"
16943                "    int x;\n"
16944                "    double y;\n"
16945                "  } Value;\n"
16946                "  class C\n"
16947                "  {\n"
16948                "    MyFavoriteType Value;\n"
16949                "  } Class;\n"
16950                "}\n",
16951                MozillaBraceStyle);
16952 }
16953 
16954 TEST_F(FormatTest, StroustrupBraceBreaking) {
16955   FormatStyle StroustrupBraceStyle = getLLVMStyle();
16956   StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
16957   verifyFormat("namespace a {\n"
16958                "class A {\n"
16959                "  void f()\n"
16960                "  {\n"
16961                "    if (true) {\n"
16962                "      a();\n"
16963                "      b();\n"
16964                "    }\n"
16965                "  }\n"
16966                "  void g() { return; }\n"
16967                "};\n"
16968                "struct B {\n"
16969                "  int x;\n"
16970                "};\n"
16971                "} // namespace a\n",
16972                StroustrupBraceStyle);
16973 
16974   verifyFormat("void foo()\n"
16975                "{\n"
16976                "  if (a) {\n"
16977                "    a();\n"
16978                "  }\n"
16979                "  else {\n"
16980                "    b();\n"
16981                "  }\n"
16982                "}\n",
16983                StroustrupBraceStyle);
16984 
16985   verifyFormat("#ifdef _DEBUG\n"
16986                "int foo(int i = 0)\n"
16987                "#else\n"
16988                "int foo(int i = 5)\n"
16989                "#endif\n"
16990                "{\n"
16991                "  return i;\n"
16992                "}",
16993                StroustrupBraceStyle);
16994 
16995   verifyFormat("void foo() {}\n"
16996                "void bar()\n"
16997                "#ifdef _DEBUG\n"
16998                "{\n"
16999                "  foo();\n"
17000                "}\n"
17001                "#else\n"
17002                "{\n"
17003                "}\n"
17004                "#endif",
17005                StroustrupBraceStyle);
17006 
17007   verifyFormat("void foobar() { int i = 5; }\n"
17008                "#ifdef _DEBUG\n"
17009                "void bar() {}\n"
17010                "#else\n"
17011                "void bar() { foobar(); }\n"
17012                "#endif",
17013                StroustrupBraceStyle);
17014 }
17015 
17016 TEST_F(FormatTest, AllmanBraceBreaking) {
17017   FormatStyle AllmanBraceStyle = getLLVMStyle();
17018   AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
17019 
17020   EXPECT_EQ("namespace a\n"
17021             "{\n"
17022             "void f();\n"
17023             "void g();\n"
17024             "} // namespace a\n",
17025             format("namespace a\n"
17026                    "{\n"
17027                    "void f();\n"
17028                    "void g();\n"
17029                    "}\n",
17030                    AllmanBraceStyle));
17031 
17032   verifyFormat("namespace a\n"
17033                "{\n"
17034                "class A\n"
17035                "{\n"
17036                "  void f()\n"
17037                "  {\n"
17038                "    if (true)\n"
17039                "    {\n"
17040                "      a();\n"
17041                "      b();\n"
17042                "    }\n"
17043                "  }\n"
17044                "  void g() { return; }\n"
17045                "};\n"
17046                "struct B\n"
17047                "{\n"
17048                "  int x;\n"
17049                "};\n"
17050                "union C\n"
17051                "{\n"
17052                "};\n"
17053                "} // namespace a",
17054                AllmanBraceStyle);
17055 
17056   verifyFormat("void f()\n"
17057                "{\n"
17058                "  if (true)\n"
17059                "  {\n"
17060                "    a();\n"
17061                "  }\n"
17062                "  else if (false)\n"
17063                "  {\n"
17064                "    b();\n"
17065                "  }\n"
17066                "  else\n"
17067                "  {\n"
17068                "    c();\n"
17069                "  }\n"
17070                "}\n",
17071                AllmanBraceStyle);
17072 
17073   verifyFormat("void f()\n"
17074                "{\n"
17075                "  for (int i = 0; i < 10; ++i)\n"
17076                "  {\n"
17077                "    a();\n"
17078                "  }\n"
17079                "  while (false)\n"
17080                "  {\n"
17081                "    b();\n"
17082                "  }\n"
17083                "  do\n"
17084                "  {\n"
17085                "    c();\n"
17086                "  } while (false)\n"
17087                "}\n",
17088                AllmanBraceStyle);
17089 
17090   verifyFormat("void f(int a)\n"
17091                "{\n"
17092                "  switch (a)\n"
17093                "  {\n"
17094                "  case 0:\n"
17095                "    break;\n"
17096                "  case 1:\n"
17097                "  {\n"
17098                "    break;\n"
17099                "  }\n"
17100                "  case 2:\n"
17101                "  {\n"
17102                "  }\n"
17103                "  break;\n"
17104                "  default:\n"
17105                "    break;\n"
17106                "  }\n"
17107                "}\n",
17108                AllmanBraceStyle);
17109 
17110   verifyFormat("enum X\n"
17111                "{\n"
17112                "  Y = 0,\n"
17113                "}\n",
17114                AllmanBraceStyle);
17115   verifyFormat("enum X\n"
17116                "{\n"
17117                "  Y = 0\n"
17118                "}\n",
17119                AllmanBraceStyle);
17120 
17121   verifyFormat("@interface BSApplicationController ()\n"
17122                "{\n"
17123                "@private\n"
17124                "  id _extraIvar;\n"
17125                "}\n"
17126                "@end\n",
17127                AllmanBraceStyle);
17128 
17129   verifyFormat("#ifdef _DEBUG\n"
17130                "int foo(int i = 0)\n"
17131                "#else\n"
17132                "int foo(int i = 5)\n"
17133                "#endif\n"
17134                "{\n"
17135                "  return i;\n"
17136                "}",
17137                AllmanBraceStyle);
17138 
17139   verifyFormat("void foo() {}\n"
17140                "void bar()\n"
17141                "#ifdef _DEBUG\n"
17142                "{\n"
17143                "  foo();\n"
17144                "}\n"
17145                "#else\n"
17146                "{\n"
17147                "}\n"
17148                "#endif",
17149                AllmanBraceStyle);
17150 
17151   verifyFormat("void foobar() { int i = 5; }\n"
17152                "#ifdef _DEBUG\n"
17153                "void bar() {}\n"
17154                "#else\n"
17155                "void bar() { foobar(); }\n"
17156                "#endif",
17157                AllmanBraceStyle);
17158 
17159   EXPECT_EQ(AllmanBraceStyle.AllowShortLambdasOnASingleLine,
17160             FormatStyle::SLS_All);
17161 
17162   verifyFormat("[](int i) { return i + 2; };\n"
17163                "[](int i, int j)\n"
17164                "{\n"
17165                "  auto x = i + j;\n"
17166                "  auto y = i * j;\n"
17167                "  return x ^ y;\n"
17168                "};\n"
17169                "void foo()\n"
17170                "{\n"
17171                "  auto shortLambda = [](int i) { return i + 2; };\n"
17172                "  auto longLambda = [](int i, int j)\n"
17173                "  {\n"
17174                "    auto x = i + j;\n"
17175                "    auto y = i * j;\n"
17176                "    return x ^ y;\n"
17177                "  };\n"
17178                "}",
17179                AllmanBraceStyle);
17180 
17181   AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
17182 
17183   verifyFormat("[](int i)\n"
17184                "{\n"
17185                "  return i + 2;\n"
17186                "};\n"
17187                "[](int i, int j)\n"
17188                "{\n"
17189                "  auto x = i + j;\n"
17190                "  auto y = i * j;\n"
17191                "  return x ^ y;\n"
17192                "};\n"
17193                "void foo()\n"
17194                "{\n"
17195                "  auto shortLambda = [](int i)\n"
17196                "  {\n"
17197                "    return i + 2;\n"
17198                "  };\n"
17199                "  auto longLambda = [](int i, int j)\n"
17200                "  {\n"
17201                "    auto x = i + j;\n"
17202                "    auto y = i * j;\n"
17203                "    return x ^ y;\n"
17204                "  };\n"
17205                "}",
17206                AllmanBraceStyle);
17207 
17208   // Reset
17209   AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_All;
17210 
17211   // This shouldn't affect ObjC blocks..
17212   verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
17213                "  // ...\n"
17214                "  int i;\n"
17215                "}];",
17216                AllmanBraceStyle);
17217   verifyFormat("void (^block)(void) = ^{\n"
17218                "  // ...\n"
17219                "  int i;\n"
17220                "};",
17221                AllmanBraceStyle);
17222   // .. or dict literals.
17223   verifyFormat("void f()\n"
17224                "{\n"
17225                "  // ...\n"
17226                "  [object someMethod:@{@\"a\" : @\"b\"}];\n"
17227                "}",
17228                AllmanBraceStyle);
17229   verifyFormat("void f()\n"
17230                "{\n"
17231                "  // ...\n"
17232                "  [object someMethod:@{a : @\"b\"}];\n"
17233                "}",
17234                AllmanBraceStyle);
17235   verifyFormat("int f()\n"
17236                "{ // comment\n"
17237                "  return 42;\n"
17238                "}",
17239                AllmanBraceStyle);
17240 
17241   AllmanBraceStyle.ColumnLimit = 19;
17242   verifyFormat("void f() { int i; }", AllmanBraceStyle);
17243   AllmanBraceStyle.ColumnLimit = 18;
17244   verifyFormat("void f()\n"
17245                "{\n"
17246                "  int i;\n"
17247                "}",
17248                AllmanBraceStyle);
17249   AllmanBraceStyle.ColumnLimit = 80;
17250 
17251   FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle;
17252   BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine =
17253       FormatStyle::SIS_WithoutElse;
17254   BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
17255   verifyFormat("void f(bool b)\n"
17256                "{\n"
17257                "  if (b)\n"
17258                "  {\n"
17259                "    return;\n"
17260                "  }\n"
17261                "}\n",
17262                BreakBeforeBraceShortIfs);
17263   verifyFormat("void f(bool b)\n"
17264                "{\n"
17265                "  if constexpr (b)\n"
17266                "  {\n"
17267                "    return;\n"
17268                "  }\n"
17269                "}\n",
17270                BreakBeforeBraceShortIfs);
17271   verifyFormat("void f(bool b)\n"
17272                "{\n"
17273                "  if CONSTEXPR (b)\n"
17274                "  {\n"
17275                "    return;\n"
17276                "  }\n"
17277                "}\n",
17278                BreakBeforeBraceShortIfs);
17279   verifyFormat("void f(bool b)\n"
17280                "{\n"
17281                "  if (b) return;\n"
17282                "}\n",
17283                BreakBeforeBraceShortIfs);
17284   verifyFormat("void f(bool b)\n"
17285                "{\n"
17286                "  if constexpr (b) return;\n"
17287                "}\n",
17288                BreakBeforeBraceShortIfs);
17289   verifyFormat("void f(bool b)\n"
17290                "{\n"
17291                "  if CONSTEXPR (b) return;\n"
17292                "}\n",
17293                BreakBeforeBraceShortIfs);
17294   verifyFormat("void f(bool b)\n"
17295                "{\n"
17296                "  while (b)\n"
17297                "  {\n"
17298                "    return;\n"
17299                "  }\n"
17300                "}\n",
17301                BreakBeforeBraceShortIfs);
17302 }
17303 
17304 TEST_F(FormatTest, WhitesmithsBraceBreaking) {
17305   FormatStyle WhitesmithsBraceStyle = getLLVMStyle();
17306   WhitesmithsBraceStyle.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
17307 
17308   // Make a few changes to the style for testing purposes
17309   WhitesmithsBraceStyle.AllowShortFunctionsOnASingleLine =
17310       FormatStyle::SFS_Empty;
17311   WhitesmithsBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
17312   WhitesmithsBraceStyle.ColumnLimit = 0;
17313 
17314   // FIXME: this test case can't decide whether there should be a blank line
17315   // after the ~D() line or not. It adds one if one doesn't exist in the test
17316   // and it removes the line if one exists.
17317   /*
17318   verifyFormat("class A;\n"
17319                "namespace B\n"
17320                "  {\n"
17321                "class C;\n"
17322                "// Comment\n"
17323                "class D\n"
17324                "  {\n"
17325                "public:\n"
17326                "  D();\n"
17327                "  ~D() {}\n"
17328                "private:\n"
17329                "  enum E\n"
17330                "    {\n"
17331                "    F\n"
17332                "    }\n"
17333                "  };\n"
17334                "  } // namespace B\n",
17335                WhitesmithsBraceStyle);
17336   */
17337 
17338   WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_None;
17339   verifyFormat("namespace a\n"
17340                "  {\n"
17341                "class A\n"
17342                "  {\n"
17343                "  void f()\n"
17344                "    {\n"
17345                "    if (true)\n"
17346                "      {\n"
17347                "      a();\n"
17348                "      b();\n"
17349                "      }\n"
17350                "    }\n"
17351                "  void g()\n"
17352                "    {\n"
17353                "    return;\n"
17354                "    }\n"
17355                "  };\n"
17356                "struct B\n"
17357                "  {\n"
17358                "  int x;\n"
17359                "  };\n"
17360                "  } // namespace a",
17361                WhitesmithsBraceStyle);
17362 
17363   verifyFormat("namespace a\n"
17364                "  {\n"
17365                "namespace b\n"
17366                "  {\n"
17367                "class A\n"
17368                "  {\n"
17369                "  void f()\n"
17370                "    {\n"
17371                "    if (true)\n"
17372                "      {\n"
17373                "      a();\n"
17374                "      b();\n"
17375                "      }\n"
17376                "    }\n"
17377                "  void g()\n"
17378                "    {\n"
17379                "    return;\n"
17380                "    }\n"
17381                "  };\n"
17382                "struct B\n"
17383                "  {\n"
17384                "  int x;\n"
17385                "  };\n"
17386                "  } // namespace b\n"
17387                "  } // namespace a",
17388                WhitesmithsBraceStyle);
17389 
17390   WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_Inner;
17391   verifyFormat("namespace a\n"
17392                "  {\n"
17393                "namespace b\n"
17394                "  {\n"
17395                "  class A\n"
17396                "    {\n"
17397                "    void f()\n"
17398                "      {\n"
17399                "      if (true)\n"
17400                "        {\n"
17401                "        a();\n"
17402                "        b();\n"
17403                "        }\n"
17404                "      }\n"
17405                "    void g()\n"
17406                "      {\n"
17407                "      return;\n"
17408                "      }\n"
17409                "    };\n"
17410                "  struct B\n"
17411                "    {\n"
17412                "    int x;\n"
17413                "    };\n"
17414                "  } // namespace b\n"
17415                "  } // namespace a",
17416                WhitesmithsBraceStyle);
17417 
17418   WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_All;
17419   verifyFormat("namespace a\n"
17420                "  {\n"
17421                "  namespace b\n"
17422                "    {\n"
17423                "    class A\n"
17424                "      {\n"
17425                "      void f()\n"
17426                "        {\n"
17427                "        if (true)\n"
17428                "          {\n"
17429                "          a();\n"
17430                "          b();\n"
17431                "          }\n"
17432                "        }\n"
17433                "      void g()\n"
17434                "        {\n"
17435                "        return;\n"
17436                "        }\n"
17437                "      };\n"
17438                "    struct B\n"
17439                "      {\n"
17440                "      int x;\n"
17441                "      };\n"
17442                "    } // namespace b\n"
17443                "  }   // namespace a",
17444                WhitesmithsBraceStyle);
17445 
17446   verifyFormat("void f()\n"
17447                "  {\n"
17448                "  if (true)\n"
17449                "    {\n"
17450                "    a();\n"
17451                "    }\n"
17452                "  else if (false)\n"
17453                "    {\n"
17454                "    b();\n"
17455                "    }\n"
17456                "  else\n"
17457                "    {\n"
17458                "    c();\n"
17459                "    }\n"
17460                "  }\n",
17461                WhitesmithsBraceStyle);
17462 
17463   verifyFormat("void f()\n"
17464                "  {\n"
17465                "  for (int i = 0; i < 10; ++i)\n"
17466                "    {\n"
17467                "    a();\n"
17468                "    }\n"
17469                "  while (false)\n"
17470                "    {\n"
17471                "    b();\n"
17472                "    }\n"
17473                "  do\n"
17474                "    {\n"
17475                "    c();\n"
17476                "    } while (false)\n"
17477                "  }\n",
17478                WhitesmithsBraceStyle);
17479 
17480   WhitesmithsBraceStyle.IndentCaseLabels = true;
17481   verifyFormat("void switchTest1(int a)\n"
17482                "  {\n"
17483                "  switch (a)\n"
17484                "    {\n"
17485                "    case 2:\n"
17486                "      {\n"
17487                "      }\n"
17488                "      break;\n"
17489                "    }\n"
17490                "  }\n",
17491                WhitesmithsBraceStyle);
17492 
17493   verifyFormat("void switchTest2(int a)\n"
17494                "  {\n"
17495                "  switch (a)\n"
17496                "    {\n"
17497                "    case 0:\n"
17498                "      break;\n"
17499                "    case 1:\n"
17500                "      {\n"
17501                "      break;\n"
17502                "      }\n"
17503                "    case 2:\n"
17504                "      {\n"
17505                "      }\n"
17506                "      break;\n"
17507                "    default:\n"
17508                "      break;\n"
17509                "    }\n"
17510                "  }\n",
17511                WhitesmithsBraceStyle);
17512 
17513   verifyFormat("void switchTest3(int a)\n"
17514                "  {\n"
17515                "  switch (a)\n"
17516                "    {\n"
17517                "    case 0:\n"
17518                "      {\n"
17519                "      foo(x);\n"
17520                "      }\n"
17521                "      break;\n"
17522                "    default:\n"
17523                "      {\n"
17524                "      foo(1);\n"
17525                "      }\n"
17526                "      break;\n"
17527                "    }\n"
17528                "  }\n",
17529                WhitesmithsBraceStyle);
17530 
17531   WhitesmithsBraceStyle.IndentCaseLabels = false;
17532 
17533   verifyFormat("void switchTest4(int a)\n"
17534                "  {\n"
17535                "  switch (a)\n"
17536                "    {\n"
17537                "  case 2:\n"
17538                "    {\n"
17539                "    }\n"
17540                "    break;\n"
17541                "    }\n"
17542                "  }\n",
17543                WhitesmithsBraceStyle);
17544 
17545   verifyFormat("void switchTest5(int a)\n"
17546                "  {\n"
17547                "  switch (a)\n"
17548                "    {\n"
17549                "  case 0:\n"
17550                "    break;\n"
17551                "  case 1:\n"
17552                "    {\n"
17553                "    foo();\n"
17554                "    break;\n"
17555                "    }\n"
17556                "  case 2:\n"
17557                "    {\n"
17558                "    }\n"
17559                "    break;\n"
17560                "  default:\n"
17561                "    break;\n"
17562                "    }\n"
17563                "  }\n",
17564                WhitesmithsBraceStyle);
17565 
17566   verifyFormat("void switchTest6(int a)\n"
17567                "  {\n"
17568                "  switch (a)\n"
17569                "    {\n"
17570                "  case 0:\n"
17571                "    {\n"
17572                "    foo(x);\n"
17573                "    }\n"
17574                "    break;\n"
17575                "  default:\n"
17576                "    {\n"
17577                "    foo(1);\n"
17578                "    }\n"
17579                "    break;\n"
17580                "    }\n"
17581                "  }\n",
17582                WhitesmithsBraceStyle);
17583 
17584   verifyFormat("enum X\n"
17585                "  {\n"
17586                "  Y = 0, // testing\n"
17587                "  }\n",
17588                WhitesmithsBraceStyle);
17589 
17590   verifyFormat("enum X\n"
17591                "  {\n"
17592                "  Y = 0\n"
17593                "  }\n",
17594                WhitesmithsBraceStyle);
17595   verifyFormat("enum X\n"
17596                "  {\n"
17597                "  Y = 0,\n"
17598                "  Z = 1\n"
17599                "  };\n",
17600                WhitesmithsBraceStyle);
17601 
17602   verifyFormat("@interface BSApplicationController ()\n"
17603                "  {\n"
17604                "@private\n"
17605                "  id _extraIvar;\n"
17606                "  }\n"
17607                "@end\n",
17608                WhitesmithsBraceStyle);
17609 
17610   verifyFormat("#ifdef _DEBUG\n"
17611                "int foo(int i = 0)\n"
17612                "#else\n"
17613                "int foo(int i = 5)\n"
17614                "#endif\n"
17615                "  {\n"
17616                "  return i;\n"
17617                "  }",
17618                WhitesmithsBraceStyle);
17619 
17620   verifyFormat("void foo() {}\n"
17621                "void bar()\n"
17622                "#ifdef _DEBUG\n"
17623                "  {\n"
17624                "  foo();\n"
17625                "  }\n"
17626                "#else\n"
17627                "  {\n"
17628                "  }\n"
17629                "#endif",
17630                WhitesmithsBraceStyle);
17631 
17632   verifyFormat("void foobar()\n"
17633                "  {\n"
17634                "  int i = 5;\n"
17635                "  }\n"
17636                "#ifdef _DEBUG\n"
17637                "void bar()\n"
17638                "  {\n"
17639                "  }\n"
17640                "#else\n"
17641                "void bar()\n"
17642                "  {\n"
17643                "  foobar();\n"
17644                "  }\n"
17645                "#endif",
17646                WhitesmithsBraceStyle);
17647 
17648   // This shouldn't affect ObjC blocks..
17649   verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
17650                "  // ...\n"
17651                "  int i;\n"
17652                "}];",
17653                WhitesmithsBraceStyle);
17654   verifyFormat("void (^block)(void) = ^{\n"
17655                "  // ...\n"
17656                "  int i;\n"
17657                "};",
17658                WhitesmithsBraceStyle);
17659   // .. or dict literals.
17660   verifyFormat("void f()\n"
17661                "  {\n"
17662                "  [object someMethod:@{@\"a\" : @\"b\"}];\n"
17663                "  }",
17664                WhitesmithsBraceStyle);
17665 
17666   verifyFormat("int f()\n"
17667                "  { // comment\n"
17668                "  return 42;\n"
17669                "  }",
17670                WhitesmithsBraceStyle);
17671 
17672   FormatStyle BreakBeforeBraceShortIfs = WhitesmithsBraceStyle;
17673   BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine =
17674       FormatStyle::SIS_OnlyFirstIf;
17675   BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
17676   verifyFormat("void f(bool b)\n"
17677                "  {\n"
17678                "  if (b)\n"
17679                "    {\n"
17680                "    return;\n"
17681                "    }\n"
17682                "  }\n",
17683                BreakBeforeBraceShortIfs);
17684   verifyFormat("void f(bool b)\n"
17685                "  {\n"
17686                "  if (b) return;\n"
17687                "  }\n",
17688                BreakBeforeBraceShortIfs);
17689   verifyFormat("void f(bool b)\n"
17690                "  {\n"
17691                "  while (b)\n"
17692                "    {\n"
17693                "    return;\n"
17694                "    }\n"
17695                "  }\n",
17696                BreakBeforeBraceShortIfs);
17697 }
17698 
17699 TEST_F(FormatTest, GNUBraceBreaking) {
17700   FormatStyle GNUBraceStyle = getLLVMStyle();
17701   GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
17702   verifyFormat("namespace a\n"
17703                "{\n"
17704                "class A\n"
17705                "{\n"
17706                "  void f()\n"
17707                "  {\n"
17708                "    int a;\n"
17709                "    {\n"
17710                "      int b;\n"
17711                "    }\n"
17712                "    if (true)\n"
17713                "      {\n"
17714                "        a();\n"
17715                "        b();\n"
17716                "      }\n"
17717                "  }\n"
17718                "  void g() { return; }\n"
17719                "}\n"
17720                "} // namespace a",
17721                GNUBraceStyle);
17722 
17723   verifyFormat("void f()\n"
17724                "{\n"
17725                "  if (true)\n"
17726                "    {\n"
17727                "      a();\n"
17728                "    }\n"
17729                "  else if (false)\n"
17730                "    {\n"
17731                "      b();\n"
17732                "    }\n"
17733                "  else\n"
17734                "    {\n"
17735                "      c();\n"
17736                "    }\n"
17737                "}\n",
17738                GNUBraceStyle);
17739 
17740   verifyFormat("void f()\n"
17741                "{\n"
17742                "  for (int i = 0; i < 10; ++i)\n"
17743                "    {\n"
17744                "      a();\n"
17745                "    }\n"
17746                "  while (false)\n"
17747                "    {\n"
17748                "      b();\n"
17749                "    }\n"
17750                "  do\n"
17751                "    {\n"
17752                "      c();\n"
17753                "    }\n"
17754                "  while (false);\n"
17755                "}\n",
17756                GNUBraceStyle);
17757 
17758   verifyFormat("void f(int a)\n"
17759                "{\n"
17760                "  switch (a)\n"
17761                "    {\n"
17762                "    case 0:\n"
17763                "      break;\n"
17764                "    case 1:\n"
17765                "      {\n"
17766                "        break;\n"
17767                "      }\n"
17768                "    case 2:\n"
17769                "      {\n"
17770                "      }\n"
17771                "      break;\n"
17772                "    default:\n"
17773                "      break;\n"
17774                "    }\n"
17775                "}\n",
17776                GNUBraceStyle);
17777 
17778   verifyFormat("enum X\n"
17779                "{\n"
17780                "  Y = 0,\n"
17781                "}\n",
17782                GNUBraceStyle);
17783 
17784   verifyFormat("@interface BSApplicationController ()\n"
17785                "{\n"
17786                "@private\n"
17787                "  id _extraIvar;\n"
17788                "}\n"
17789                "@end\n",
17790                GNUBraceStyle);
17791 
17792   verifyFormat("#ifdef _DEBUG\n"
17793                "int foo(int i = 0)\n"
17794                "#else\n"
17795                "int foo(int i = 5)\n"
17796                "#endif\n"
17797                "{\n"
17798                "  return i;\n"
17799                "}",
17800                GNUBraceStyle);
17801 
17802   verifyFormat("void foo() {}\n"
17803                "void bar()\n"
17804                "#ifdef _DEBUG\n"
17805                "{\n"
17806                "  foo();\n"
17807                "}\n"
17808                "#else\n"
17809                "{\n"
17810                "}\n"
17811                "#endif",
17812                GNUBraceStyle);
17813 
17814   verifyFormat("void foobar() { int i = 5; }\n"
17815                "#ifdef _DEBUG\n"
17816                "void bar() {}\n"
17817                "#else\n"
17818                "void bar() { foobar(); }\n"
17819                "#endif",
17820                GNUBraceStyle);
17821 }
17822 
17823 TEST_F(FormatTest, WebKitBraceBreaking) {
17824   FormatStyle WebKitBraceStyle = getLLVMStyle();
17825   WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit;
17826   WebKitBraceStyle.FixNamespaceComments = false;
17827   verifyFormat("namespace a {\n"
17828                "class A {\n"
17829                "  void f()\n"
17830                "  {\n"
17831                "    if (true) {\n"
17832                "      a();\n"
17833                "      b();\n"
17834                "    }\n"
17835                "  }\n"
17836                "  void g() { return; }\n"
17837                "};\n"
17838                "enum E {\n"
17839                "  A,\n"
17840                "  // foo\n"
17841                "  B,\n"
17842                "  C\n"
17843                "};\n"
17844                "struct B {\n"
17845                "  int x;\n"
17846                "};\n"
17847                "}\n",
17848                WebKitBraceStyle);
17849   verifyFormat("struct S {\n"
17850                "  int Type;\n"
17851                "  union {\n"
17852                "    int x;\n"
17853                "    double y;\n"
17854                "  } Value;\n"
17855                "  class C {\n"
17856                "    MyFavoriteType Value;\n"
17857                "  } Class;\n"
17858                "};\n",
17859                WebKitBraceStyle);
17860 }
17861 
17862 TEST_F(FormatTest, CatchExceptionReferenceBinding) {
17863   verifyFormat("void f() {\n"
17864                "  try {\n"
17865                "  } catch (const Exception &e) {\n"
17866                "  }\n"
17867                "}\n",
17868                getLLVMStyle());
17869 }
17870 
17871 TEST_F(FormatTest, CatchAlignArrayOfStructuresRightAlignment) {
17872   auto Style = getLLVMStyle();
17873   Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
17874   Style.AlignConsecutiveAssignments =
17875       FormatStyle::AlignConsecutiveStyle::ACS_Consecutive;
17876   Style.AlignConsecutiveDeclarations =
17877       FormatStyle::AlignConsecutiveStyle::ACS_Consecutive;
17878   verifyFormat("struct test demo[] = {\n"
17879                "    {56,    23, \"hello\"},\n"
17880                "    {-1, 93463, \"world\"},\n"
17881                "    { 7,     5,    \"!!\"}\n"
17882                "};\n",
17883                Style);
17884 
17885   verifyFormat("struct test demo[] = {\n"
17886                "    {56,    23, \"hello\"}, // first line\n"
17887                "    {-1, 93463, \"world\"}, // second line\n"
17888                "    { 7,     5,    \"!!\"}  // third line\n"
17889                "};\n",
17890                Style);
17891 
17892   verifyFormat("struct test demo[4] = {\n"
17893                "    { 56,    23, 21,       \"oh\"}, // first line\n"
17894                "    { -1, 93463, 22,       \"my\"}, // second line\n"
17895                "    {  7,     5,  1, \"goodness\"}  // third line\n"
17896                "    {234,     5,  1, \"gracious\"}  // fourth line\n"
17897                "};\n",
17898                Style);
17899 
17900   verifyFormat("struct test demo[3] = {\n"
17901                "    {56,    23, \"hello\"},\n"
17902                "    {-1, 93463, \"world\"},\n"
17903                "    { 7,     5,    \"!!\"}\n"
17904                "};\n",
17905                Style);
17906 
17907   verifyFormat("struct test demo[3] = {\n"
17908                "    {int{56},    23, \"hello\"},\n"
17909                "    {int{-1}, 93463, \"world\"},\n"
17910                "    { int{7},     5,    \"!!\"}\n"
17911                "};\n",
17912                Style);
17913 
17914   verifyFormat("struct test demo[] = {\n"
17915                "    {56,    23, \"hello\"},\n"
17916                "    {-1, 93463, \"world\"},\n"
17917                "    { 7,     5,    \"!!\"},\n"
17918                "};\n",
17919                Style);
17920 
17921   verifyFormat("test demo[] = {\n"
17922                "    {56,    23, \"hello\"},\n"
17923                "    {-1, 93463, \"world\"},\n"
17924                "    { 7,     5,    \"!!\"},\n"
17925                "};\n",
17926                Style);
17927 
17928   verifyFormat("demo = std::array<struct test, 3>{\n"
17929                "    test{56,    23, \"hello\"},\n"
17930                "    test{-1, 93463, \"world\"},\n"
17931                "    test{ 7,     5,    \"!!\"},\n"
17932                "};\n",
17933                Style);
17934 
17935   verifyFormat("test demo[] = {\n"
17936                "    {56,    23, \"hello\"},\n"
17937                "#if X\n"
17938                "    {-1, 93463, \"world\"},\n"
17939                "#endif\n"
17940                "    { 7,     5,    \"!!\"}\n"
17941                "};\n",
17942                Style);
17943 
17944   verifyFormat(
17945       "test demo[] = {\n"
17946       "    { 7,    23,\n"
17947       "     \"hello world i am a very long line that really, in any\"\n"
17948       "     \"just world, ought to be split over multiple lines\"},\n"
17949       "    {-1, 93463,                                  \"world\"},\n"
17950       "    {56,     5,                                     \"!!\"}\n"
17951       "};\n",
17952       Style);
17953 
17954   verifyFormat("return GradForUnaryCwise(g, {\n"
17955                "                                {{\"sign\"}, \"Sign\",  "
17956                "  {\"x\", \"dy\"}},\n"
17957                "                                {  {\"dx\"},  \"Mul\", {\"dy\""
17958                ", \"sign\"}},\n"
17959                "});\n",
17960                Style);
17961 
17962   Style.ColumnLimit = 0;
17963   EXPECT_EQ(
17964       "test demo[] = {\n"
17965       "    {56,    23, \"hello world i am a very long line that really, "
17966       "in any just world, ought to be split over multiple lines\"},\n"
17967       "    {-1, 93463,                                                  "
17968       "                                                 \"world\"},\n"
17969       "    { 7,     5,                                                  "
17970       "                                                    \"!!\"},\n"
17971       "};",
17972       format("test demo[] = {{56, 23, \"hello world i am a very long line "
17973              "that really, in any just world, ought to be split over multiple "
17974              "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
17975              Style));
17976 
17977   Style.ColumnLimit = 80;
17978   verifyFormat("test demo[] = {\n"
17979                "    {56,    23, /* a comment */ \"hello\"},\n"
17980                "    {-1, 93463,                 \"world\"},\n"
17981                "    { 7,     5,                    \"!!\"}\n"
17982                "};\n",
17983                Style);
17984 
17985   verifyFormat("test demo[] = {\n"
17986                "    {56,    23,                    \"hello\"},\n"
17987                "    {-1, 93463, \"world\" /* comment here */},\n"
17988                "    { 7,     5,                       \"!!\"}\n"
17989                "};\n",
17990                Style);
17991 
17992   verifyFormat("test demo[] = {\n"
17993                "    {56, /* a comment */ 23, \"hello\"},\n"
17994                "    {-1,              93463, \"world\"},\n"
17995                "    { 7,                  5,    \"!!\"}\n"
17996                "};\n",
17997                Style);
17998 
17999   Style.ColumnLimit = 20;
18000   EXPECT_EQ(
18001       "demo = std::array<\n"
18002       "    struct test, 3>{\n"
18003       "    test{\n"
18004       "         56,    23,\n"
18005       "         \"hello \"\n"
18006       "         \"world i \"\n"
18007       "         \"am a very \"\n"
18008       "         \"long line \"\n"
18009       "         \"that \"\n"
18010       "         \"really, \"\n"
18011       "         \"in any \"\n"
18012       "         \"just \"\n"
18013       "         \"world, \"\n"
18014       "         \"ought to \"\n"
18015       "         \"be split \"\n"
18016       "         \"over \"\n"
18017       "         \"multiple \"\n"
18018       "         \"lines\"},\n"
18019       "    test{-1, 93463,\n"
18020       "         \"world\"},\n"
18021       "    test{ 7,     5,\n"
18022       "         \"!!\"   },\n"
18023       "};",
18024       format("demo = std::array<struct test, 3>{test{56, 23, \"hello world "
18025              "i am a very long line that really, in any just world, ought "
18026              "to be split over multiple lines\"},test{-1, 93463, \"world\"},"
18027              "test{7, 5, \"!!\"},};",
18028              Style));
18029   // This caused a core dump by enabling Alignment in the LLVMStyle globally
18030   Style = getLLVMStyleWithColumns(50);
18031   Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
18032   verifyFormat("static A x = {\n"
18033                "    {{init1, init2, init3, init4},\n"
18034                "     {init1, init2, init3, init4}}\n"
18035                "};",
18036                Style);
18037   Style.ColumnLimit = 100;
18038   EXPECT_EQ(
18039       "test demo[] = {\n"
18040       "    {56,    23,\n"
18041       "     \"hello world i am a very long line that really, in any just world"
18042       ", ought to be split over \"\n"
18043       "     \"multiple lines\"  },\n"
18044       "    {-1, 93463, \"world\"},\n"
18045       "    { 7,     5,    \"!!\"},\n"
18046       "};",
18047       format("test demo[] = {{56, 23, \"hello world i am a very long line "
18048              "that really, in any just world, ought to be split over multiple "
18049              "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
18050              Style));
18051 
18052   Style = getLLVMStyleWithColumns(50);
18053   Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
18054   Style.AlignConsecutiveAssignments =
18055       FormatStyle::AlignConsecutiveStyle::ACS_Consecutive;
18056   Style.AlignConsecutiveDeclarations =
18057       FormatStyle::AlignConsecutiveStyle::ACS_Consecutive;
18058   verifyFormat("struct test demo[] = {\n"
18059                "    {56,    23, \"hello\"},\n"
18060                "    {-1, 93463, \"world\"},\n"
18061                "    { 7,     5,    \"!!\"}\n"
18062                "};\n"
18063                "static A x = {\n"
18064                "    {{init1, init2, init3, init4},\n"
18065                "     {init1, init2, init3, init4}}\n"
18066                "};",
18067                Style);
18068   Style.ColumnLimit = 100;
18069   Style.AlignConsecutiveAssignments =
18070       FormatStyle::AlignConsecutiveStyle::ACS_AcrossComments;
18071   Style.AlignConsecutiveDeclarations =
18072       FormatStyle::AlignConsecutiveStyle::ACS_AcrossComments;
18073   verifyFormat("struct test demo[] = {\n"
18074                "    {56,    23, \"hello\"},\n"
18075                "    {-1, 93463, \"world\"},\n"
18076                "    { 7,     5,    \"!!\"}\n"
18077                "};\n"
18078                "struct test demo[4] = {\n"
18079                "    { 56,    23, 21,       \"oh\"}, // first line\n"
18080                "    { -1, 93463, 22,       \"my\"}, // second line\n"
18081                "    {  7,     5,  1, \"goodness\"}  // third line\n"
18082                "    {234,     5,  1, \"gracious\"}  // fourth line\n"
18083                "};\n",
18084                Style);
18085   EXPECT_EQ(
18086       "test demo[] = {\n"
18087       "    {56,\n"
18088       "     \"hello world i am a very long line that really, in any just world"
18089       ", ought to be split over \"\n"
18090       "     \"multiple lines\",    23},\n"
18091       "    {-1,      \"world\", 93463},\n"
18092       "    { 7,         \"!!\",     5},\n"
18093       "};",
18094       format("test demo[] = {{56, \"hello world i am a very long line "
18095              "that really, in any just world, ought to be split over multiple "
18096              "lines\", 23},{-1, \"world\", 93463},{7, \"!!\", 5},};",
18097              Style));
18098 }
18099 
18100 TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) {
18101   auto Style = getLLVMStyle();
18102   Style.AlignArrayOfStructures = FormatStyle::AIAS_Left;
18103   /* FIXME: This case gets misformatted.
18104   verifyFormat("auto foo = Items{\n"
18105                "    Section{0, bar(), },\n"
18106                "    Section{1, boo()  }\n"
18107                "};\n",
18108                Style);
18109   */
18110   verifyFormat("auto foo = Items{\n"
18111                "    Section{\n"
18112                "            0, bar(),\n"
18113                "            }\n"
18114                "};\n",
18115                Style);
18116   verifyFormat("struct test demo[] = {\n"
18117                "    {56, 23,    \"hello\"},\n"
18118                "    {-1, 93463, \"world\"},\n"
18119                "    {7,  5,     \"!!\"   }\n"
18120                "};\n",
18121                Style);
18122   verifyFormat("struct test demo[] = {\n"
18123                "    {56, 23,    \"hello\"}, // first line\n"
18124                "    {-1, 93463, \"world\"}, // second line\n"
18125                "    {7,  5,     \"!!\"   }  // third line\n"
18126                "};\n",
18127                Style);
18128   verifyFormat("struct test demo[4] = {\n"
18129                "    {56,  23,    21, \"oh\"      }, // first line\n"
18130                "    {-1,  93463, 22, \"my\"      }, // second line\n"
18131                "    {7,   5,     1,  \"goodness\"}  // third line\n"
18132                "    {234, 5,     1,  \"gracious\"}  // fourth line\n"
18133                "};\n",
18134                Style);
18135   verifyFormat("struct test demo[3] = {\n"
18136                "    {56, 23,    \"hello\"},\n"
18137                "    {-1, 93463, \"world\"},\n"
18138                "    {7,  5,     \"!!\"   }\n"
18139                "};\n",
18140                Style);
18141 
18142   verifyFormat("struct test demo[3] = {\n"
18143                "    {int{56}, 23,    \"hello\"},\n"
18144                "    {int{-1}, 93463, \"world\"},\n"
18145                "    {int{7},  5,     \"!!\"   }\n"
18146                "};\n",
18147                Style);
18148   verifyFormat("struct test demo[] = {\n"
18149                "    {56, 23,    \"hello\"},\n"
18150                "    {-1, 93463, \"world\"},\n"
18151                "    {7,  5,     \"!!\"   },\n"
18152                "};\n",
18153                Style);
18154   verifyFormat("test demo[] = {\n"
18155                "    {56, 23,    \"hello\"},\n"
18156                "    {-1, 93463, \"world\"},\n"
18157                "    {7,  5,     \"!!\"   },\n"
18158                "};\n",
18159                Style);
18160   verifyFormat("demo = std::array<struct test, 3>{\n"
18161                "    test{56, 23,    \"hello\"},\n"
18162                "    test{-1, 93463, \"world\"},\n"
18163                "    test{7,  5,     \"!!\"   },\n"
18164                "};\n",
18165                Style);
18166   verifyFormat("test demo[] = {\n"
18167                "    {56, 23,    \"hello\"},\n"
18168                "#if X\n"
18169                "    {-1, 93463, \"world\"},\n"
18170                "#endif\n"
18171                "    {7,  5,     \"!!\"   }\n"
18172                "};\n",
18173                Style);
18174   verifyFormat(
18175       "test demo[] = {\n"
18176       "    {7,  23,\n"
18177       "     \"hello world i am a very long line that really, in any\"\n"
18178       "     \"just world, ought to be split over multiple lines\"},\n"
18179       "    {-1, 93463, \"world\"                                 },\n"
18180       "    {56, 5,     \"!!\"                                    }\n"
18181       "};\n",
18182       Style);
18183 
18184   verifyFormat("return GradForUnaryCwise(g, {\n"
18185                "                                {{\"sign\"}, \"Sign\", {\"x\", "
18186                "\"dy\"}   },\n"
18187                "                                {{\"dx\"},   \"Mul\",  "
18188                "{\"dy\", \"sign\"}},\n"
18189                "});\n",
18190                Style);
18191 
18192   Style.ColumnLimit = 0;
18193   EXPECT_EQ(
18194       "test demo[] = {\n"
18195       "    {56, 23,    \"hello world i am a very long line that really, in any "
18196       "just world, ought to be split over multiple lines\"},\n"
18197       "    {-1, 93463, \"world\"                                               "
18198       "                                                   },\n"
18199       "    {7,  5,     \"!!\"                                                  "
18200       "                                                   },\n"
18201       "};",
18202       format("test demo[] = {{56, 23, \"hello world i am a very long line "
18203              "that really, in any just world, ought to be split over multiple "
18204              "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
18205              Style));
18206 
18207   Style.ColumnLimit = 80;
18208   verifyFormat("test demo[] = {\n"
18209                "    {56, 23,    /* a comment */ \"hello\"},\n"
18210                "    {-1, 93463, \"world\"                },\n"
18211                "    {7,  5,     \"!!\"                   }\n"
18212                "};\n",
18213                Style);
18214 
18215   verifyFormat("test demo[] = {\n"
18216                "    {56, 23,    \"hello\"                   },\n"
18217                "    {-1, 93463, \"world\" /* comment here */},\n"
18218                "    {7,  5,     \"!!\"                      }\n"
18219                "};\n",
18220                Style);
18221 
18222   verifyFormat("test demo[] = {\n"
18223                "    {56, /* a comment */ 23, \"hello\"},\n"
18224                "    {-1, 93463,              \"world\"},\n"
18225                "    {7,  5,                  \"!!\"   }\n"
18226                "};\n",
18227                Style);
18228 
18229   Style.ColumnLimit = 20;
18230   EXPECT_EQ(
18231       "demo = std::array<\n"
18232       "    struct test, 3>{\n"
18233       "    test{\n"
18234       "         56, 23,\n"
18235       "         \"hello \"\n"
18236       "         \"world i \"\n"
18237       "         \"am a very \"\n"
18238       "         \"long line \"\n"
18239       "         \"that \"\n"
18240       "         \"really, \"\n"
18241       "         \"in any \"\n"
18242       "         \"just \"\n"
18243       "         \"world, \"\n"
18244       "         \"ought to \"\n"
18245       "         \"be split \"\n"
18246       "         \"over \"\n"
18247       "         \"multiple \"\n"
18248       "         \"lines\"},\n"
18249       "    test{-1, 93463,\n"
18250       "         \"world\"},\n"
18251       "    test{7,  5,\n"
18252       "         \"!!\"   },\n"
18253       "};",
18254       format("demo = std::array<struct test, 3>{test{56, 23, \"hello world "
18255              "i am a very long line that really, in any just world, ought "
18256              "to be split over multiple lines\"},test{-1, 93463, \"world\"},"
18257              "test{7, 5, \"!!\"},};",
18258              Style));
18259 
18260   // This caused a core dump by enabling Alignment in the LLVMStyle globally
18261   Style = getLLVMStyleWithColumns(50);
18262   Style.AlignArrayOfStructures = FormatStyle::AIAS_Left;
18263   verifyFormat("static A x = {\n"
18264                "    {{init1, init2, init3, init4},\n"
18265                "     {init1, init2, init3, init4}}\n"
18266                "};",
18267                Style);
18268   Style.ColumnLimit = 100;
18269   EXPECT_EQ(
18270       "test demo[] = {\n"
18271       "    {56, 23,\n"
18272       "     \"hello world i am a very long line that really, in any just world"
18273       ", ought to be split over \"\n"
18274       "     \"multiple lines\"  },\n"
18275       "    {-1, 93463, \"world\"},\n"
18276       "    {7,  5,     \"!!\"   },\n"
18277       "};",
18278       format("test demo[] = {{56, 23, \"hello world i am a very long line "
18279              "that really, in any just world, ought to be split over multiple "
18280              "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
18281              Style));
18282 }
18283 
18284 TEST_F(FormatTest, UnderstandsPragmas) {
18285   verifyFormat("#pragma omp reduction(| : var)");
18286   verifyFormat("#pragma omp reduction(+ : var)");
18287 
18288   EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string "
18289             "(including parentheses).",
18290             format("#pragma    mark   Any non-hyphenated or hyphenated string "
18291                    "(including parentheses)."));
18292 }
18293 
18294 TEST_F(FormatTest, UnderstandPragmaOption) {
18295   verifyFormat("#pragma option -C -A");
18296 
18297   EXPECT_EQ("#pragma option -C -A", format("#pragma    option   -C   -A"));
18298 }
18299 
18300 TEST_F(FormatTest, OptimizeBreakPenaltyVsExcess) {
18301   FormatStyle Style = getLLVMStyle();
18302   Style.ColumnLimit = 20;
18303 
18304   // See PR41213
18305   EXPECT_EQ("/*\n"
18306             " *\t9012345\n"
18307             " * /8901\n"
18308             " */",
18309             format("/*\n"
18310                    " *\t9012345 /8901\n"
18311                    " */",
18312                    Style));
18313   EXPECT_EQ("/*\n"
18314             " *345678\n"
18315             " *\t/8901\n"
18316             " */",
18317             format("/*\n"
18318                    " *345678\t/8901\n"
18319                    " */",
18320                    Style));
18321 
18322   verifyFormat("int a; // the\n"
18323                "       // comment",
18324                Style);
18325   EXPECT_EQ("int a; /* first line\n"
18326             "        * second\n"
18327             "        * line third\n"
18328             "        * line\n"
18329             "        */",
18330             format("int a; /* first line\n"
18331                    "        * second\n"
18332                    "        * line third\n"
18333                    "        * line\n"
18334                    "        */",
18335                    Style));
18336   EXPECT_EQ("int a; // first line\n"
18337             "       // second\n"
18338             "       // line third\n"
18339             "       // line",
18340             format("int a; // first line\n"
18341                    "       // second line\n"
18342                    "       // third line",
18343                    Style));
18344 
18345   Style.PenaltyExcessCharacter = 90;
18346   verifyFormat("int a; // the comment", Style);
18347   EXPECT_EQ("int a; // the comment\n"
18348             "       // aaa",
18349             format("int a; // the comment aaa", Style));
18350   EXPECT_EQ("int a; /* first line\n"
18351             "        * second line\n"
18352             "        * third line\n"
18353             "        */",
18354             format("int a; /* first line\n"
18355                    "        * second line\n"
18356                    "        * third line\n"
18357                    "        */",
18358                    Style));
18359   EXPECT_EQ("int a; // first line\n"
18360             "       // second line\n"
18361             "       // third line",
18362             format("int a; // first line\n"
18363                    "       // second line\n"
18364                    "       // third line",
18365                    Style));
18366   // FIXME: Investigate why this is not getting the same layout as the test
18367   // above.
18368   EXPECT_EQ("int a; /* first line\n"
18369             "        * second line\n"
18370             "        * third line\n"
18371             "        */",
18372             format("int a; /* first line second line third line"
18373                    "\n*/",
18374                    Style));
18375 
18376   EXPECT_EQ("// foo bar baz bazfoo\n"
18377             "// foo bar foo bar\n",
18378             format("// foo bar baz bazfoo\n"
18379                    "// foo bar foo           bar\n",
18380                    Style));
18381   EXPECT_EQ("// foo bar baz bazfoo\n"
18382             "// foo bar foo bar\n",
18383             format("// foo bar baz      bazfoo\n"
18384                    "// foo            bar foo bar\n",
18385                    Style));
18386 
18387   // FIXME: Optimally, we'd keep bazfoo on the first line and reflow bar to the
18388   // next one.
18389   EXPECT_EQ("// foo bar baz bazfoo\n"
18390             "// bar foo bar\n",
18391             format("// foo bar baz      bazfoo bar\n"
18392                    "// foo            bar\n",
18393                    Style));
18394 
18395   EXPECT_EQ("// foo bar baz bazfoo\n"
18396             "// foo bar baz bazfoo\n"
18397             "// bar foo bar\n",
18398             format("// foo bar baz      bazfoo\n"
18399                    "// foo bar baz      bazfoo bar\n"
18400                    "// foo bar\n",
18401                    Style));
18402 
18403   EXPECT_EQ("// foo bar baz bazfoo\n"
18404             "// foo bar baz bazfoo\n"
18405             "// bar foo bar\n",
18406             format("// foo bar baz      bazfoo\n"
18407                    "// foo bar baz      bazfoo bar\n"
18408                    "// foo           bar\n",
18409                    Style));
18410 
18411   // Make sure we do not keep protruding characters if strict mode reflow is
18412   // cheaper than keeping protruding characters.
18413   Style.ColumnLimit = 21;
18414   EXPECT_EQ(
18415       "// foo foo foo foo\n"
18416       "// foo foo foo foo\n"
18417       "// foo foo foo foo\n",
18418       format("// foo foo foo foo foo foo foo foo foo foo foo foo\n", Style));
18419 
18420   EXPECT_EQ("int a = /* long block\n"
18421             "           comment */\n"
18422             "    42;",
18423             format("int a = /* long block comment */ 42;", Style));
18424 }
18425 
18426 #define EXPECT_ALL_STYLES_EQUAL(Styles)                                        \
18427   for (size_t i = 1; i < Styles.size(); ++i)                                   \
18428   EXPECT_EQ(Styles[0], Styles[i])                                              \
18429       << "Style #" << i << " of " << Styles.size() << " differs from Style #0"
18430 
18431 TEST_F(FormatTest, GetsPredefinedStyleByName) {
18432   SmallVector<FormatStyle, 3> Styles;
18433   Styles.resize(3);
18434 
18435   Styles[0] = getLLVMStyle();
18436   EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1]));
18437   EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2]));
18438   EXPECT_ALL_STYLES_EQUAL(Styles);
18439 
18440   Styles[0] = getGoogleStyle();
18441   EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1]));
18442   EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2]));
18443   EXPECT_ALL_STYLES_EQUAL(Styles);
18444 
18445   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
18446   EXPECT_TRUE(
18447       getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1]));
18448   EXPECT_TRUE(
18449       getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2]));
18450   EXPECT_ALL_STYLES_EQUAL(Styles);
18451 
18452   Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp);
18453   EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1]));
18454   EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2]));
18455   EXPECT_ALL_STYLES_EQUAL(Styles);
18456 
18457   Styles[0] = getMozillaStyle();
18458   EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1]));
18459   EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2]));
18460   EXPECT_ALL_STYLES_EQUAL(Styles);
18461 
18462   Styles[0] = getWebKitStyle();
18463   EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1]));
18464   EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2]));
18465   EXPECT_ALL_STYLES_EQUAL(Styles);
18466 
18467   Styles[0] = getGNUStyle();
18468   EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1]));
18469   EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2]));
18470   EXPECT_ALL_STYLES_EQUAL(Styles);
18471 
18472   EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0]));
18473 }
18474 
18475 TEST_F(FormatTest, GetsCorrectBasedOnStyle) {
18476   SmallVector<FormatStyle, 8> Styles;
18477   Styles.resize(2);
18478 
18479   Styles[0] = getGoogleStyle();
18480   Styles[1] = getLLVMStyle();
18481   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
18482   EXPECT_ALL_STYLES_EQUAL(Styles);
18483 
18484   Styles.resize(5);
18485   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
18486   Styles[1] = getLLVMStyle();
18487   Styles[1].Language = FormatStyle::LK_JavaScript;
18488   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
18489 
18490   Styles[2] = getLLVMStyle();
18491   Styles[2].Language = FormatStyle::LK_JavaScript;
18492   EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n"
18493                                   "BasedOnStyle: Google",
18494                                   &Styles[2])
18495                    .value());
18496 
18497   Styles[3] = getLLVMStyle();
18498   Styles[3].Language = FormatStyle::LK_JavaScript;
18499   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n"
18500                                   "Language: JavaScript",
18501                                   &Styles[3])
18502                    .value());
18503 
18504   Styles[4] = getLLVMStyle();
18505   Styles[4].Language = FormatStyle::LK_JavaScript;
18506   EXPECT_EQ(0, parseConfiguration("---\n"
18507                                   "BasedOnStyle: LLVM\n"
18508                                   "IndentWidth: 123\n"
18509                                   "---\n"
18510                                   "BasedOnStyle: Google\n"
18511                                   "Language: JavaScript",
18512                                   &Styles[4])
18513                    .value());
18514   EXPECT_ALL_STYLES_EQUAL(Styles);
18515 }
18516 
18517 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME)                             \
18518   Style.FIELD = false;                                                         \
18519   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value());      \
18520   EXPECT_TRUE(Style.FIELD);                                                    \
18521   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value());     \
18522   EXPECT_FALSE(Style.FIELD);
18523 
18524 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD)
18525 
18526 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME)              \
18527   Style.STRUCT.FIELD = false;                                                  \
18528   EXPECT_EQ(0,                                                                 \
18529             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": true", &Style)   \
18530                 .value());                                                     \
18531   EXPECT_TRUE(Style.STRUCT.FIELD);                                             \
18532   EXPECT_EQ(0,                                                                 \
18533             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": false", &Style)  \
18534                 .value());                                                     \
18535   EXPECT_FALSE(Style.STRUCT.FIELD);
18536 
18537 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD)                                 \
18538   CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD)
18539 
18540 #define CHECK_PARSE(TEXT, FIELD, VALUE)                                        \
18541   EXPECT_NE(VALUE, Style.FIELD) << "Initial value already the same!";          \
18542   EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value());                      \
18543   EXPECT_EQ(VALUE, Style.FIELD) << "Unexpected value after parsing!"
18544 
18545 TEST_F(FormatTest, ParsesConfigurationBools) {
18546   FormatStyle Style = {};
18547   Style.Language = FormatStyle::LK_Cpp;
18548   CHECK_PARSE_BOOL(AlignTrailingComments);
18549   CHECK_PARSE_BOOL(AllowAllArgumentsOnNextLine);
18550   CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
18551   CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine);
18552   CHECK_PARSE_BOOL(AllowShortEnumsOnASingleLine);
18553   CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
18554   CHECK_PARSE_BOOL(BinPackArguments);
18555   CHECK_PARSE_BOOL(BinPackParameters);
18556   CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations);
18557   CHECK_PARSE_BOOL(BreakBeforeConceptDeclarations);
18558   CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
18559   CHECK_PARSE_BOOL(BreakStringLiterals);
18560   CHECK_PARSE_BOOL(CompactNamespaces);
18561   CHECK_PARSE_BOOL(DeriveLineEnding);
18562   CHECK_PARSE_BOOL(DerivePointerAlignment);
18563   CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding");
18564   CHECK_PARSE_BOOL(DisableFormat);
18565   CHECK_PARSE_BOOL(IndentAccessModifiers);
18566   CHECK_PARSE_BOOL(IndentCaseLabels);
18567   CHECK_PARSE_BOOL(IndentCaseBlocks);
18568   CHECK_PARSE_BOOL(IndentGotoLabels);
18569   CHECK_PARSE_BOOL(IndentRequires);
18570   CHECK_PARSE_BOOL(IndentWrappedFunctionNames);
18571   CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks);
18572   CHECK_PARSE_BOOL(ObjCSpaceAfterProperty);
18573   CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
18574   CHECK_PARSE_BOOL(Cpp11BracedListStyle);
18575   CHECK_PARSE_BOOL(ReflowComments);
18576   CHECK_PARSE_BOOL(SortUsingDeclarations);
18577   CHECK_PARSE_BOOL(SpacesInParentheses);
18578   CHECK_PARSE_BOOL(SpacesInSquareBrackets);
18579   CHECK_PARSE_BOOL(SpacesInConditionalStatement);
18580   CHECK_PARSE_BOOL(SpaceInEmptyBlock);
18581   CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
18582   CHECK_PARSE_BOOL(SpacesInContainerLiterals);
18583   CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
18584   CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
18585   CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword);
18586   CHECK_PARSE_BOOL(SpaceAfterLogicalNot);
18587   CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
18588   CHECK_PARSE_BOOL(SpaceBeforeCaseColon);
18589   CHECK_PARSE_BOOL(SpaceBeforeCpp11BracedList);
18590   CHECK_PARSE_BOOL(SpaceBeforeCtorInitializerColon);
18591   CHECK_PARSE_BOOL(SpaceBeforeInheritanceColon);
18592   CHECK_PARSE_BOOL(SpaceBeforeRangeBasedForLoopColon);
18593   CHECK_PARSE_BOOL(SpaceBeforeSquareBrackets);
18594   CHECK_PARSE_BOOL(UseCRLF);
18595 
18596   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterCaseLabel);
18597   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass);
18598   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum);
18599   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction);
18600   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace);
18601   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration);
18602   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct);
18603   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion);
18604   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterExternBlock);
18605   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch);
18606   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse);
18607   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeLambdaBody);
18608   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeWhile);
18609   CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces);
18610   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyFunction);
18611   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyRecord);
18612   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyNamespace);
18613 }
18614 
18615 #undef CHECK_PARSE_BOOL
18616 
18617 TEST_F(FormatTest, ParsesConfiguration) {
18618   FormatStyle Style = {};
18619   Style.Language = FormatStyle::LK_Cpp;
18620   CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
18621   CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",
18622               ConstructorInitializerIndentWidth, 1234u);
18623   CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u);
18624   CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
18625   CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
18626   CHECK_PARSE("PenaltyBreakAssignment: 1234", PenaltyBreakAssignment, 1234u);
18627   CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234",
18628               PenaltyBreakBeforeFirstCallParameter, 1234u);
18629   CHECK_PARSE("PenaltyBreakTemplateDeclaration: 1234",
18630               PenaltyBreakTemplateDeclaration, 1234u);
18631   CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
18632   CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
18633               PenaltyReturnTypeOnItsOwnLine, 1234u);
18634   CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
18635               SpacesBeforeTrailingComments, 1234u);
18636   CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
18637   CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u);
18638   CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$");
18639 
18640   Style.QualifierAlignment = FormatStyle::QAS_Right;
18641   CHECK_PARSE("QualifierAlignment: Leave", QualifierAlignment,
18642               FormatStyle::QAS_Leave);
18643   CHECK_PARSE("QualifierAlignment: Right", QualifierAlignment,
18644               FormatStyle::QAS_Right);
18645   CHECK_PARSE("QualifierAlignment: Left", QualifierAlignment,
18646               FormatStyle::QAS_Left);
18647   CHECK_PARSE("QualifierAlignment: Custom", QualifierAlignment,
18648               FormatStyle::QAS_Custom);
18649 
18650   Style.QualifierOrder.clear();
18651   CHECK_PARSE("QualifierOrder: [ const, volatile, type ]", QualifierOrder,
18652               std::vector<std::string>({"const", "volatile", "type"}));
18653   Style.QualifierOrder.clear();
18654   CHECK_PARSE("QualifierOrder: [const, type]", QualifierOrder,
18655               std::vector<std::string>({"const", "type"}));
18656   Style.QualifierOrder.clear();
18657   CHECK_PARSE("QualifierOrder: [volatile, type]", QualifierOrder,
18658               std::vector<std::string>({"volatile", "type"}));
18659 
18660   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
18661   CHECK_PARSE("AlignConsecutiveAssignments: None", AlignConsecutiveAssignments,
18662               FormatStyle::ACS_None);
18663   CHECK_PARSE("AlignConsecutiveAssignments: Consecutive",
18664               AlignConsecutiveAssignments, FormatStyle::ACS_Consecutive);
18665   CHECK_PARSE("AlignConsecutiveAssignments: AcrossEmptyLines",
18666               AlignConsecutiveAssignments, FormatStyle::ACS_AcrossEmptyLines);
18667   CHECK_PARSE("AlignConsecutiveAssignments: AcrossEmptyLinesAndComments",
18668               AlignConsecutiveAssignments,
18669               FormatStyle::ACS_AcrossEmptyLinesAndComments);
18670   // For backwards compability, false / true should still parse
18671   CHECK_PARSE("AlignConsecutiveAssignments: false", AlignConsecutiveAssignments,
18672               FormatStyle::ACS_None);
18673   CHECK_PARSE("AlignConsecutiveAssignments: true", AlignConsecutiveAssignments,
18674               FormatStyle::ACS_Consecutive);
18675 
18676   Style.AlignConsecutiveBitFields = FormatStyle::ACS_Consecutive;
18677   CHECK_PARSE("AlignConsecutiveBitFields: None", AlignConsecutiveBitFields,
18678               FormatStyle::ACS_None);
18679   CHECK_PARSE("AlignConsecutiveBitFields: Consecutive",
18680               AlignConsecutiveBitFields, FormatStyle::ACS_Consecutive);
18681   CHECK_PARSE("AlignConsecutiveBitFields: AcrossEmptyLines",
18682               AlignConsecutiveBitFields, FormatStyle::ACS_AcrossEmptyLines);
18683   CHECK_PARSE("AlignConsecutiveBitFields: AcrossEmptyLinesAndComments",
18684               AlignConsecutiveBitFields,
18685               FormatStyle::ACS_AcrossEmptyLinesAndComments);
18686   // For backwards compability, false / true should still parse
18687   CHECK_PARSE("AlignConsecutiveBitFields: false", AlignConsecutiveBitFields,
18688               FormatStyle::ACS_None);
18689   CHECK_PARSE("AlignConsecutiveBitFields: true", AlignConsecutiveBitFields,
18690               FormatStyle::ACS_Consecutive);
18691 
18692   Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
18693   CHECK_PARSE("AlignConsecutiveMacros: None", AlignConsecutiveMacros,
18694               FormatStyle::ACS_None);
18695   CHECK_PARSE("AlignConsecutiveMacros: Consecutive", AlignConsecutiveMacros,
18696               FormatStyle::ACS_Consecutive);
18697   CHECK_PARSE("AlignConsecutiveMacros: AcrossEmptyLines",
18698               AlignConsecutiveMacros, FormatStyle::ACS_AcrossEmptyLines);
18699   CHECK_PARSE("AlignConsecutiveMacros: AcrossEmptyLinesAndComments",
18700               AlignConsecutiveMacros,
18701               FormatStyle::ACS_AcrossEmptyLinesAndComments);
18702   // For backwards compability, false / true should still parse
18703   CHECK_PARSE("AlignConsecutiveMacros: false", AlignConsecutiveMacros,
18704               FormatStyle::ACS_None);
18705   CHECK_PARSE("AlignConsecutiveMacros: true", AlignConsecutiveMacros,
18706               FormatStyle::ACS_Consecutive);
18707 
18708   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
18709   CHECK_PARSE("AlignConsecutiveDeclarations: None",
18710               AlignConsecutiveDeclarations, FormatStyle::ACS_None);
18711   CHECK_PARSE("AlignConsecutiveDeclarations: Consecutive",
18712               AlignConsecutiveDeclarations, FormatStyle::ACS_Consecutive);
18713   CHECK_PARSE("AlignConsecutiveDeclarations: AcrossEmptyLines",
18714               AlignConsecutiveDeclarations, FormatStyle::ACS_AcrossEmptyLines);
18715   CHECK_PARSE("AlignConsecutiveDeclarations: AcrossEmptyLinesAndComments",
18716               AlignConsecutiveDeclarations,
18717               FormatStyle::ACS_AcrossEmptyLinesAndComments);
18718   // For backwards compability, false / true should still parse
18719   CHECK_PARSE("AlignConsecutiveDeclarations: false",
18720               AlignConsecutiveDeclarations, FormatStyle::ACS_None);
18721   CHECK_PARSE("AlignConsecutiveDeclarations: true",
18722               AlignConsecutiveDeclarations, FormatStyle::ACS_Consecutive);
18723 
18724   Style.PointerAlignment = FormatStyle::PAS_Middle;
18725   CHECK_PARSE("PointerAlignment: Left", PointerAlignment,
18726               FormatStyle::PAS_Left);
18727   CHECK_PARSE("PointerAlignment: Right", PointerAlignment,
18728               FormatStyle::PAS_Right);
18729   CHECK_PARSE("PointerAlignment: Middle", PointerAlignment,
18730               FormatStyle::PAS_Middle);
18731   Style.ReferenceAlignment = FormatStyle::RAS_Middle;
18732   CHECK_PARSE("ReferenceAlignment: Pointer", ReferenceAlignment,
18733               FormatStyle::RAS_Pointer);
18734   CHECK_PARSE("ReferenceAlignment: Left", ReferenceAlignment,
18735               FormatStyle::RAS_Left);
18736   CHECK_PARSE("ReferenceAlignment: Right", ReferenceAlignment,
18737               FormatStyle::RAS_Right);
18738   CHECK_PARSE("ReferenceAlignment: Middle", ReferenceAlignment,
18739               FormatStyle::RAS_Middle);
18740   // For backward compatibility:
18741   CHECK_PARSE("PointerBindsToType: Left", PointerAlignment,
18742               FormatStyle::PAS_Left);
18743   CHECK_PARSE("PointerBindsToType: Right", PointerAlignment,
18744               FormatStyle::PAS_Right);
18745   CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment,
18746               FormatStyle::PAS_Middle);
18747 
18748   Style.Standard = FormatStyle::LS_Auto;
18749   CHECK_PARSE("Standard: c++03", Standard, FormatStyle::LS_Cpp03);
18750   CHECK_PARSE("Standard: c++11", Standard, FormatStyle::LS_Cpp11);
18751   CHECK_PARSE("Standard: c++14", Standard, FormatStyle::LS_Cpp14);
18752   CHECK_PARSE("Standard: c++17", Standard, FormatStyle::LS_Cpp17);
18753   CHECK_PARSE("Standard: c++20", Standard, FormatStyle::LS_Cpp20);
18754   CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
18755   CHECK_PARSE("Standard: Latest", Standard, FormatStyle::LS_Latest);
18756   // Legacy aliases:
18757   CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03);
18758   CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Latest);
18759   CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
18760   CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
18761 
18762   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
18763   CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment",
18764               BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment);
18765   CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators,
18766               FormatStyle::BOS_None);
18767   CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators,
18768               FormatStyle::BOS_All);
18769   // For backward compatibility:
18770   CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators,
18771               FormatStyle::BOS_None);
18772   CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators,
18773               FormatStyle::BOS_All);
18774 
18775   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
18776   CHECK_PARSE("BreakConstructorInitializers: BeforeComma",
18777               BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma);
18778   CHECK_PARSE("BreakConstructorInitializers: AfterColon",
18779               BreakConstructorInitializers, FormatStyle::BCIS_AfterColon);
18780   CHECK_PARSE("BreakConstructorInitializers: BeforeColon",
18781               BreakConstructorInitializers, FormatStyle::BCIS_BeforeColon);
18782   // For backward compatibility:
18783   CHECK_PARSE("BreakConstructorInitializersBeforeComma: true",
18784               BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma);
18785 
18786   Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
18787   CHECK_PARSE("BreakInheritanceList: AfterComma", BreakInheritanceList,
18788               FormatStyle::BILS_AfterComma);
18789   CHECK_PARSE("BreakInheritanceList: BeforeComma", BreakInheritanceList,
18790               FormatStyle::BILS_BeforeComma);
18791   CHECK_PARSE("BreakInheritanceList: AfterColon", BreakInheritanceList,
18792               FormatStyle::BILS_AfterColon);
18793   CHECK_PARSE("BreakInheritanceList: BeforeColon", BreakInheritanceList,
18794               FormatStyle::BILS_BeforeColon);
18795   // For backward compatibility:
18796   CHECK_PARSE("BreakBeforeInheritanceComma: true", BreakInheritanceList,
18797               FormatStyle::BILS_BeforeComma);
18798 
18799   Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack;
18800   CHECK_PARSE("PackConstructorInitializers: Never", PackConstructorInitializers,
18801               FormatStyle::PCIS_Never);
18802   CHECK_PARSE("PackConstructorInitializers: BinPack",
18803               PackConstructorInitializers, FormatStyle::PCIS_BinPack);
18804   CHECK_PARSE("PackConstructorInitializers: CurrentLine",
18805               PackConstructorInitializers, FormatStyle::PCIS_CurrentLine);
18806   CHECK_PARSE("PackConstructorInitializers: NextLine",
18807               PackConstructorInitializers, FormatStyle::PCIS_NextLine);
18808   // For backward compatibility:
18809   CHECK_PARSE("BasedOnStyle: Google\n"
18810               "ConstructorInitializerAllOnOneLineOrOnePerLine: true\n"
18811               "AllowAllConstructorInitializersOnNextLine: false",
18812               PackConstructorInitializers, FormatStyle::PCIS_CurrentLine);
18813   Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
18814   CHECK_PARSE("BasedOnStyle: Google\n"
18815               "ConstructorInitializerAllOnOneLineOrOnePerLine: false",
18816               PackConstructorInitializers, FormatStyle::PCIS_BinPack);
18817   CHECK_PARSE("ConstructorInitializerAllOnOneLineOrOnePerLine: true\n"
18818               "AllowAllConstructorInitializersOnNextLine: true",
18819               PackConstructorInitializers, FormatStyle::PCIS_NextLine);
18820   Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack;
18821   CHECK_PARSE("ConstructorInitializerAllOnOneLineOrOnePerLine: true\n"
18822               "AllowAllConstructorInitializersOnNextLine: false",
18823               PackConstructorInitializers, FormatStyle::PCIS_CurrentLine);
18824 
18825   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
18826   CHECK_PARSE("EmptyLineBeforeAccessModifier: Never",
18827               EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Never);
18828   CHECK_PARSE("EmptyLineBeforeAccessModifier: Leave",
18829               EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Leave);
18830   CHECK_PARSE("EmptyLineBeforeAccessModifier: LogicalBlock",
18831               EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_LogicalBlock);
18832   CHECK_PARSE("EmptyLineBeforeAccessModifier: Always",
18833               EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Always);
18834 
18835   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
18836   CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket,
18837               FormatStyle::BAS_Align);
18838   CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket,
18839               FormatStyle::BAS_DontAlign);
18840   CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket,
18841               FormatStyle::BAS_AlwaysBreak);
18842   // For backward compatibility:
18843   CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket,
18844               FormatStyle::BAS_DontAlign);
18845   CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket,
18846               FormatStyle::BAS_Align);
18847 
18848   Style.AlignEscapedNewlines = FormatStyle::ENAS_Left;
18849   CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines,
18850               FormatStyle::ENAS_DontAlign);
18851   CHECK_PARSE("AlignEscapedNewlines: Left", AlignEscapedNewlines,
18852               FormatStyle::ENAS_Left);
18853   CHECK_PARSE("AlignEscapedNewlines: Right", AlignEscapedNewlines,
18854               FormatStyle::ENAS_Right);
18855   // For backward compatibility:
18856   CHECK_PARSE("AlignEscapedNewlinesLeft: true", AlignEscapedNewlines,
18857               FormatStyle::ENAS_Left);
18858   CHECK_PARSE("AlignEscapedNewlinesLeft: false", AlignEscapedNewlines,
18859               FormatStyle::ENAS_Right);
18860 
18861   Style.AlignOperands = FormatStyle::OAS_Align;
18862   CHECK_PARSE("AlignOperands: DontAlign", AlignOperands,
18863               FormatStyle::OAS_DontAlign);
18864   CHECK_PARSE("AlignOperands: Align", AlignOperands, FormatStyle::OAS_Align);
18865   CHECK_PARSE("AlignOperands: AlignAfterOperator", AlignOperands,
18866               FormatStyle::OAS_AlignAfterOperator);
18867   // For backward compatibility:
18868   CHECK_PARSE("AlignOperands: false", AlignOperands,
18869               FormatStyle::OAS_DontAlign);
18870   CHECK_PARSE("AlignOperands: true", AlignOperands, FormatStyle::OAS_Align);
18871 
18872   Style.UseTab = FormatStyle::UT_ForIndentation;
18873   CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
18874   CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
18875   CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
18876   CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab,
18877               FormatStyle::UT_ForContinuationAndIndentation);
18878   CHECK_PARSE("UseTab: AlignWithSpaces", UseTab,
18879               FormatStyle::UT_AlignWithSpaces);
18880   // For backward compatibility:
18881   CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never);
18882   CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always);
18883 
18884   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty;
18885   CHECK_PARSE("AllowShortBlocksOnASingleLine: Never",
18886               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never);
18887   CHECK_PARSE("AllowShortBlocksOnASingleLine: Empty",
18888               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Empty);
18889   CHECK_PARSE("AllowShortBlocksOnASingleLine: Always",
18890               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always);
18891   // For backward compatibility:
18892   CHECK_PARSE("AllowShortBlocksOnASingleLine: false",
18893               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never);
18894   CHECK_PARSE("AllowShortBlocksOnASingleLine: true",
18895               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always);
18896 
18897   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
18898   CHECK_PARSE("AllowShortFunctionsOnASingleLine: None",
18899               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
18900   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline",
18901               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline);
18902   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty",
18903               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty);
18904   CHECK_PARSE("AllowShortFunctionsOnASingleLine: All",
18905               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
18906   // For backward compatibility:
18907   CHECK_PARSE("AllowShortFunctionsOnASingleLine: false",
18908               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
18909   CHECK_PARSE("AllowShortFunctionsOnASingleLine: true",
18910               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
18911 
18912   Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Both;
18913   CHECK_PARSE("SpaceAroundPointerQualifiers: Default",
18914               SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Default);
18915   CHECK_PARSE("SpaceAroundPointerQualifiers: Before",
18916               SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Before);
18917   CHECK_PARSE("SpaceAroundPointerQualifiers: After",
18918               SpaceAroundPointerQualifiers, FormatStyle::SAPQ_After);
18919   CHECK_PARSE("SpaceAroundPointerQualifiers: Both",
18920               SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Both);
18921 
18922   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
18923   CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens,
18924               FormatStyle::SBPO_Never);
18925   CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens,
18926               FormatStyle::SBPO_Always);
18927   CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens,
18928               FormatStyle::SBPO_ControlStatements);
18929   CHECK_PARSE("SpaceBeforeParens: ControlStatementsExceptControlMacros",
18930               SpaceBeforeParens,
18931               FormatStyle::SBPO_ControlStatementsExceptControlMacros);
18932   CHECK_PARSE("SpaceBeforeParens: NonEmptyParentheses", SpaceBeforeParens,
18933               FormatStyle::SBPO_NonEmptyParentheses);
18934   CHECK_PARSE("SpaceBeforeParens: Custom", SpaceBeforeParens,
18935               FormatStyle::SBPO_Custom);
18936   // For backward compatibility:
18937   CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens,
18938               FormatStyle::SBPO_Never);
18939   CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens,
18940               FormatStyle::SBPO_ControlStatements);
18941   CHECK_PARSE("SpaceBeforeParens: ControlStatementsExceptForEachMacros",
18942               SpaceBeforeParens,
18943               FormatStyle::SBPO_ControlStatementsExceptControlMacros);
18944 
18945   Style.ColumnLimit = 123;
18946   FormatStyle BaseStyle = getLLVMStyle();
18947   CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
18948   CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
18949 
18950   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
18951   CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
18952               FormatStyle::BS_Attach);
18953   CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
18954               FormatStyle::BS_Linux);
18955   CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces,
18956               FormatStyle::BS_Mozilla);
18957   CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
18958               FormatStyle::BS_Stroustrup);
18959   CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces,
18960               FormatStyle::BS_Allman);
18961   CHECK_PARSE("BreakBeforeBraces: Whitesmiths", BreakBeforeBraces,
18962               FormatStyle::BS_Whitesmiths);
18963   CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU);
18964   CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces,
18965               FormatStyle::BS_WebKit);
18966   CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces,
18967               FormatStyle::BS_Custom);
18968 
18969   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never;
18970   CHECK_PARSE("BraceWrapping:\n"
18971               "  AfterControlStatement: MultiLine",
18972               BraceWrapping.AfterControlStatement,
18973               FormatStyle::BWACS_MultiLine);
18974   CHECK_PARSE("BraceWrapping:\n"
18975               "  AfterControlStatement: Always",
18976               BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always);
18977   CHECK_PARSE("BraceWrapping:\n"
18978               "  AfterControlStatement: Never",
18979               BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never);
18980   // For backward compatibility:
18981   CHECK_PARSE("BraceWrapping:\n"
18982               "  AfterControlStatement: true",
18983               BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always);
18984   CHECK_PARSE("BraceWrapping:\n"
18985               "  AfterControlStatement: false",
18986               BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never);
18987 
18988   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
18989   CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType,
18990               FormatStyle::RTBS_None);
18991   CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType,
18992               FormatStyle::RTBS_All);
18993   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel",
18994               AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel);
18995   CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions",
18996               AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions);
18997   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions",
18998               AlwaysBreakAfterReturnType,
18999               FormatStyle::RTBS_TopLevelDefinitions);
19000 
19001   Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
19002   CHECK_PARSE("AlwaysBreakTemplateDeclarations: No",
19003               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_No);
19004   CHECK_PARSE("AlwaysBreakTemplateDeclarations: MultiLine",
19005               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine);
19006   CHECK_PARSE("AlwaysBreakTemplateDeclarations: Yes",
19007               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes);
19008   CHECK_PARSE("AlwaysBreakTemplateDeclarations: false",
19009               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine);
19010   CHECK_PARSE("AlwaysBreakTemplateDeclarations: true",
19011               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes);
19012 
19013   Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
19014   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None",
19015               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None);
19016   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All",
19017               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All);
19018   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel",
19019               AlwaysBreakAfterDefinitionReturnType,
19020               FormatStyle::DRTBS_TopLevel);
19021 
19022   Style.NamespaceIndentation = FormatStyle::NI_All;
19023   CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
19024               FormatStyle::NI_None);
19025   CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation,
19026               FormatStyle::NI_Inner);
19027   CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
19028               FormatStyle::NI_All);
19029 
19030   Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_OnlyFirstIf;
19031   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Never",
19032               AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never);
19033   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: WithoutElse",
19034               AllowShortIfStatementsOnASingleLine,
19035               FormatStyle::SIS_WithoutElse);
19036   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: OnlyFirstIf",
19037               AllowShortIfStatementsOnASingleLine,
19038               FormatStyle::SIS_OnlyFirstIf);
19039   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: AllIfsAndElse",
19040               AllowShortIfStatementsOnASingleLine,
19041               FormatStyle::SIS_AllIfsAndElse);
19042   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Always",
19043               AllowShortIfStatementsOnASingleLine,
19044               FormatStyle::SIS_OnlyFirstIf);
19045   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: false",
19046               AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never);
19047   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: true",
19048               AllowShortIfStatementsOnASingleLine,
19049               FormatStyle::SIS_WithoutElse);
19050 
19051   Style.IndentExternBlock = FormatStyle::IEBS_NoIndent;
19052   CHECK_PARSE("IndentExternBlock: AfterExternBlock", IndentExternBlock,
19053               FormatStyle::IEBS_AfterExternBlock);
19054   CHECK_PARSE("IndentExternBlock: Indent", IndentExternBlock,
19055               FormatStyle::IEBS_Indent);
19056   CHECK_PARSE("IndentExternBlock: NoIndent", IndentExternBlock,
19057               FormatStyle::IEBS_NoIndent);
19058   CHECK_PARSE("IndentExternBlock: true", IndentExternBlock,
19059               FormatStyle::IEBS_Indent);
19060   CHECK_PARSE("IndentExternBlock: false", IndentExternBlock,
19061               FormatStyle::IEBS_NoIndent);
19062 
19063   Style.BitFieldColonSpacing = FormatStyle::BFCS_None;
19064   CHECK_PARSE("BitFieldColonSpacing: Both", BitFieldColonSpacing,
19065               FormatStyle::BFCS_Both);
19066   CHECK_PARSE("BitFieldColonSpacing: None", BitFieldColonSpacing,
19067               FormatStyle::BFCS_None);
19068   CHECK_PARSE("BitFieldColonSpacing: Before", BitFieldColonSpacing,
19069               FormatStyle::BFCS_Before);
19070   CHECK_PARSE("BitFieldColonSpacing: After", BitFieldColonSpacing,
19071               FormatStyle::BFCS_After);
19072 
19073   Style.SortJavaStaticImport = FormatStyle::SJSIO_Before;
19074   CHECK_PARSE("SortJavaStaticImport: After", SortJavaStaticImport,
19075               FormatStyle::SJSIO_After);
19076   CHECK_PARSE("SortJavaStaticImport: Before", SortJavaStaticImport,
19077               FormatStyle::SJSIO_Before);
19078 
19079   // FIXME: This is required because parsing a configuration simply overwrites
19080   // the first N elements of the list instead of resetting it.
19081   Style.ForEachMacros.clear();
19082   std::vector<std::string> BoostForeach;
19083   BoostForeach.push_back("BOOST_FOREACH");
19084   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach);
19085   std::vector<std::string> BoostAndQForeach;
19086   BoostAndQForeach.push_back("BOOST_FOREACH");
19087   BoostAndQForeach.push_back("Q_FOREACH");
19088   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
19089               BoostAndQForeach);
19090 
19091   Style.IfMacros.clear();
19092   std::vector<std::string> CustomIfs;
19093   CustomIfs.push_back("MYIF");
19094   CHECK_PARSE("IfMacros: [MYIF]", IfMacros, CustomIfs);
19095 
19096   Style.AttributeMacros.clear();
19097   CHECK_PARSE("BasedOnStyle: LLVM", AttributeMacros,
19098               std::vector<std::string>{"__capability"});
19099   CHECK_PARSE("AttributeMacros: [attr1, attr2]", AttributeMacros,
19100               std::vector<std::string>({"attr1", "attr2"}));
19101 
19102   Style.StatementAttributeLikeMacros.clear();
19103   CHECK_PARSE("StatementAttributeLikeMacros: [emit,Q_EMIT]",
19104               StatementAttributeLikeMacros,
19105               std::vector<std::string>({"emit", "Q_EMIT"}));
19106 
19107   Style.StatementMacros.clear();
19108   CHECK_PARSE("StatementMacros: [QUNUSED]", StatementMacros,
19109               std::vector<std::string>{"QUNUSED"});
19110   CHECK_PARSE("StatementMacros: [QUNUSED, QT_REQUIRE_VERSION]", StatementMacros,
19111               std::vector<std::string>({"QUNUSED", "QT_REQUIRE_VERSION"}));
19112 
19113   Style.NamespaceMacros.clear();
19114   CHECK_PARSE("NamespaceMacros: [TESTSUITE]", NamespaceMacros,
19115               std::vector<std::string>{"TESTSUITE"});
19116   CHECK_PARSE("NamespaceMacros: [TESTSUITE, SUITE]", NamespaceMacros,
19117               std::vector<std::string>({"TESTSUITE", "SUITE"}));
19118 
19119   Style.WhitespaceSensitiveMacros.clear();
19120   CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE]",
19121               WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"});
19122   CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE, ASSERT]",
19123               WhitespaceSensitiveMacros,
19124               std::vector<std::string>({"STRINGIZE", "ASSERT"}));
19125   Style.WhitespaceSensitiveMacros.clear();
19126   CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE']",
19127               WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"});
19128   CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE', 'ASSERT']",
19129               WhitespaceSensitiveMacros,
19130               std::vector<std::string>({"STRINGIZE", "ASSERT"}));
19131 
19132   Style.IncludeStyle.IncludeCategories.clear();
19133   std::vector<tooling::IncludeStyle::IncludeCategory> ExpectedCategories = {
19134       {"abc/.*", 2, 0, false}, {".*", 1, 0, true}};
19135   CHECK_PARSE("IncludeCategories:\n"
19136               "  - Regex: abc/.*\n"
19137               "    Priority: 2\n"
19138               "  - Regex: .*\n"
19139               "    Priority: 1\n"
19140               "    CaseSensitive: true\n",
19141               IncludeStyle.IncludeCategories, ExpectedCategories);
19142   CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeStyle.IncludeIsMainRegex,
19143               "abc$");
19144   CHECK_PARSE("IncludeIsMainSourceRegex: 'abc$'",
19145               IncludeStyle.IncludeIsMainSourceRegex, "abc$");
19146 
19147   Style.SortIncludes = FormatStyle::SI_Never;
19148   CHECK_PARSE("SortIncludes: true", SortIncludes,
19149               FormatStyle::SI_CaseSensitive);
19150   CHECK_PARSE("SortIncludes: false", SortIncludes, FormatStyle::SI_Never);
19151   CHECK_PARSE("SortIncludes: CaseInsensitive", SortIncludes,
19152               FormatStyle::SI_CaseInsensitive);
19153   CHECK_PARSE("SortIncludes: CaseSensitive", SortIncludes,
19154               FormatStyle::SI_CaseSensitive);
19155   CHECK_PARSE("SortIncludes: Never", SortIncludes, FormatStyle::SI_Never);
19156 
19157   Style.RawStringFormats.clear();
19158   std::vector<FormatStyle::RawStringFormat> ExpectedRawStringFormats = {
19159       {
19160           FormatStyle::LK_TextProto,
19161           {"pb", "proto"},
19162           {"PARSE_TEXT_PROTO"},
19163           /*CanonicalDelimiter=*/"",
19164           "llvm",
19165       },
19166       {
19167           FormatStyle::LK_Cpp,
19168           {"cc", "cpp"},
19169           {"C_CODEBLOCK", "CPPEVAL"},
19170           /*CanonicalDelimiter=*/"cc",
19171           /*BasedOnStyle=*/"",
19172       },
19173   };
19174 
19175   CHECK_PARSE("RawStringFormats:\n"
19176               "  - Language: TextProto\n"
19177               "    Delimiters:\n"
19178               "      - 'pb'\n"
19179               "      - 'proto'\n"
19180               "    EnclosingFunctions:\n"
19181               "      - 'PARSE_TEXT_PROTO'\n"
19182               "    BasedOnStyle: llvm\n"
19183               "  - Language: Cpp\n"
19184               "    Delimiters:\n"
19185               "      - 'cc'\n"
19186               "      - 'cpp'\n"
19187               "    EnclosingFunctions:\n"
19188               "      - 'C_CODEBLOCK'\n"
19189               "      - 'CPPEVAL'\n"
19190               "    CanonicalDelimiter: 'cc'",
19191               RawStringFormats, ExpectedRawStringFormats);
19192 
19193   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
19194               "  Minimum: 0\n"
19195               "  Maximum: 0",
19196               SpacesInLineCommentPrefix.Minimum, 0u);
19197   EXPECT_EQ(Style.SpacesInLineCommentPrefix.Maximum, 0u);
19198   Style.SpacesInLineCommentPrefix.Minimum = 1;
19199   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
19200               "  Minimum: 2",
19201               SpacesInLineCommentPrefix.Minimum, 0u);
19202   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
19203               "  Maximum: -1",
19204               SpacesInLineCommentPrefix.Maximum, -1u);
19205   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
19206               "  Minimum: 2",
19207               SpacesInLineCommentPrefix.Minimum, 2u);
19208   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
19209               "  Maximum: 1",
19210               SpacesInLineCommentPrefix.Maximum, 1u);
19211   EXPECT_EQ(Style.SpacesInLineCommentPrefix.Minimum, 1u);
19212 
19213   Style.SpacesInAngles = FormatStyle::SIAS_Always;
19214   CHECK_PARSE("SpacesInAngles: Never", SpacesInAngles, FormatStyle::SIAS_Never);
19215   CHECK_PARSE("SpacesInAngles: Always", SpacesInAngles,
19216               FormatStyle::SIAS_Always);
19217   CHECK_PARSE("SpacesInAngles: Leave", SpacesInAngles, FormatStyle::SIAS_Leave);
19218   // For backward compatibility:
19219   CHECK_PARSE("SpacesInAngles: false", SpacesInAngles, FormatStyle::SIAS_Never);
19220   CHECK_PARSE("SpacesInAngles: true", SpacesInAngles, FormatStyle::SIAS_Always);
19221 }
19222 
19223 TEST_F(FormatTest, ParsesConfigurationWithLanguages) {
19224   FormatStyle Style = {};
19225   Style.Language = FormatStyle::LK_Cpp;
19226   CHECK_PARSE("Language: Cpp\n"
19227               "IndentWidth: 12",
19228               IndentWidth, 12u);
19229   EXPECT_EQ(parseConfiguration("Language: JavaScript\n"
19230                                "IndentWidth: 34",
19231                                &Style),
19232             ParseError::Unsuitable);
19233   FormatStyle BinPackedTCS = {};
19234   BinPackedTCS.Language = FormatStyle::LK_JavaScript;
19235   EXPECT_EQ(parseConfiguration("BinPackArguments: true\n"
19236                                "InsertTrailingCommas: Wrapped",
19237                                &BinPackedTCS),
19238             ParseError::BinPackTrailingCommaConflict);
19239   EXPECT_EQ(12u, Style.IndentWidth);
19240   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
19241   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
19242 
19243   Style.Language = FormatStyle::LK_JavaScript;
19244   CHECK_PARSE("Language: JavaScript\n"
19245               "IndentWidth: 12",
19246               IndentWidth, 12u);
19247   CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u);
19248   EXPECT_EQ(parseConfiguration("Language: Cpp\n"
19249                                "IndentWidth: 34",
19250                                &Style),
19251             ParseError::Unsuitable);
19252   EXPECT_EQ(23u, Style.IndentWidth);
19253   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
19254   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
19255 
19256   CHECK_PARSE("BasedOnStyle: LLVM\n"
19257               "IndentWidth: 67",
19258               IndentWidth, 67u);
19259 
19260   CHECK_PARSE("---\n"
19261               "Language: JavaScript\n"
19262               "IndentWidth: 12\n"
19263               "---\n"
19264               "Language: Cpp\n"
19265               "IndentWidth: 34\n"
19266               "...\n",
19267               IndentWidth, 12u);
19268 
19269   Style.Language = FormatStyle::LK_Cpp;
19270   CHECK_PARSE("---\n"
19271               "Language: JavaScript\n"
19272               "IndentWidth: 12\n"
19273               "---\n"
19274               "Language: Cpp\n"
19275               "IndentWidth: 34\n"
19276               "...\n",
19277               IndentWidth, 34u);
19278   CHECK_PARSE("---\n"
19279               "IndentWidth: 78\n"
19280               "---\n"
19281               "Language: JavaScript\n"
19282               "IndentWidth: 56\n"
19283               "...\n",
19284               IndentWidth, 78u);
19285 
19286   Style.ColumnLimit = 123;
19287   Style.IndentWidth = 234;
19288   Style.BreakBeforeBraces = FormatStyle::BS_Linux;
19289   Style.TabWidth = 345;
19290   EXPECT_FALSE(parseConfiguration("---\n"
19291                                   "IndentWidth: 456\n"
19292                                   "BreakBeforeBraces: Allman\n"
19293                                   "---\n"
19294                                   "Language: JavaScript\n"
19295                                   "IndentWidth: 111\n"
19296                                   "TabWidth: 111\n"
19297                                   "---\n"
19298                                   "Language: Cpp\n"
19299                                   "BreakBeforeBraces: Stroustrup\n"
19300                                   "TabWidth: 789\n"
19301                                   "...\n",
19302                                   &Style));
19303   EXPECT_EQ(123u, Style.ColumnLimit);
19304   EXPECT_EQ(456u, Style.IndentWidth);
19305   EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces);
19306   EXPECT_EQ(789u, Style.TabWidth);
19307 
19308   EXPECT_EQ(parseConfiguration("---\n"
19309                                "Language: JavaScript\n"
19310                                "IndentWidth: 56\n"
19311                                "---\n"
19312                                "IndentWidth: 78\n"
19313                                "...\n",
19314                                &Style),
19315             ParseError::Error);
19316   EXPECT_EQ(parseConfiguration("---\n"
19317                                "Language: JavaScript\n"
19318                                "IndentWidth: 56\n"
19319                                "---\n"
19320                                "Language: JavaScript\n"
19321                                "IndentWidth: 78\n"
19322                                "...\n",
19323                                &Style),
19324             ParseError::Error);
19325 
19326   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
19327 }
19328 
19329 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) {
19330   FormatStyle Style = {};
19331   Style.Language = FormatStyle::LK_JavaScript;
19332   Style.BreakBeforeTernaryOperators = true;
19333   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value());
19334   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
19335 
19336   Style.BreakBeforeTernaryOperators = true;
19337   EXPECT_EQ(0, parseConfiguration("---\n"
19338                                   "BasedOnStyle: Google\n"
19339                                   "---\n"
19340                                   "Language: JavaScript\n"
19341                                   "IndentWidth: 76\n"
19342                                   "...\n",
19343                                   &Style)
19344                    .value());
19345   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
19346   EXPECT_EQ(76u, Style.IndentWidth);
19347   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
19348 }
19349 
19350 TEST_F(FormatTest, ConfigurationRoundTripTest) {
19351   FormatStyle Style = getLLVMStyle();
19352   std::string YAML = configurationAsText(Style);
19353   FormatStyle ParsedStyle = {};
19354   ParsedStyle.Language = FormatStyle::LK_Cpp;
19355   EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
19356   EXPECT_EQ(Style, ParsedStyle);
19357 }
19358 
19359 TEST_F(FormatTest, WorksFor8bitEncodings) {
19360   EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
19361             "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
19362             "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
19363             "\"\xef\xee\xf0\xf3...\"",
19364             format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
19365                    "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
19366                    "\xef\xee\xf0\xf3...\"",
19367                    getLLVMStyleWithColumns(12)));
19368 }
19369 
19370 TEST_F(FormatTest, HandlesUTF8BOM) {
19371   EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf"));
19372   EXPECT_EQ("\xef\xbb\xbf#include <iostream>",
19373             format("\xef\xbb\xbf#include <iostream>"));
19374   EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>",
19375             format("\xef\xbb\xbf\n#include <iostream>"));
19376 }
19377 
19378 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
19379 #if !defined(_MSC_VER)
19380 
19381 TEST_F(FormatTest, CountsUTF8CharactersProperly) {
19382   verifyFormat("\"Однажды в студёную зимнюю пору...\"",
19383                getLLVMStyleWithColumns(35));
19384   verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
19385                getLLVMStyleWithColumns(31));
19386   verifyFormat("// Однажды в студёную зимнюю пору...",
19387                getLLVMStyleWithColumns(36));
19388   verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32));
19389   verifyFormat("/* Однажды в студёную зимнюю пору... */",
19390                getLLVMStyleWithColumns(39));
19391   verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
19392                getLLVMStyleWithColumns(35));
19393 }
19394 
19395 TEST_F(FormatTest, SplitsUTF8Strings) {
19396   // Non-printable characters' width is currently considered to be the length in
19397   // bytes in UTF8. The characters can be displayed in very different manner
19398   // (zero-width, single width with a substitution glyph, expanded to their code
19399   // (e.g. "<8d>"), so there's no single correct way to handle them.
19400   EXPECT_EQ("\"aaaaÄ\"\n"
19401             "\"\xc2\x8d\";",
19402             format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
19403   EXPECT_EQ("\"aaaaaaaÄ\"\n"
19404             "\"\xc2\x8d\";",
19405             format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
19406   EXPECT_EQ("\"Однажды, в \"\n"
19407             "\"студёную \"\n"
19408             "\"зимнюю \"\n"
19409             "\"пору,\"",
19410             format("\"Однажды, в студёную зимнюю пору,\"",
19411                    getLLVMStyleWithColumns(13)));
19412   EXPECT_EQ(
19413       "\"一 二 三 \"\n"
19414       "\"四 五六 \"\n"
19415       "\"七 八 九 \"\n"
19416       "\"十\"",
19417       format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11)));
19418   EXPECT_EQ("\"一\t\"\n"
19419             "\"二 \t\"\n"
19420             "\"三 四 \"\n"
19421             "\"五\t\"\n"
19422             "\"六 \t\"\n"
19423             "\"七 \"\n"
19424             "\"八九十\tqq\"",
19425             format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
19426                    getLLVMStyleWithColumns(11)));
19427 
19428   // UTF8 character in an escape sequence.
19429   EXPECT_EQ("\"aaaaaa\"\n"
19430             "\"\\\xC2\x8D\"",
19431             format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10)));
19432 }
19433 
19434 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
19435   EXPECT_EQ("const char *sssss =\n"
19436             "    \"一二三四五六七八\\\n"
19437             " 九 十\";",
19438             format("const char *sssss = \"一二三四五六七八\\\n"
19439                    " 九 十\";",
19440                    getLLVMStyleWithColumns(30)));
19441 }
19442 
19443 TEST_F(FormatTest, SplitsUTF8LineComments) {
19444   EXPECT_EQ("// aaaaÄ\xc2\x8d",
19445             format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10)));
19446   EXPECT_EQ("// Я из лесу\n"
19447             "// вышел; был\n"
19448             "// сильный\n"
19449             "// мороз.",
19450             format("// Я из лесу вышел; был сильный мороз.",
19451                    getLLVMStyleWithColumns(13)));
19452   EXPECT_EQ("// 一二三\n"
19453             "// 四五六七\n"
19454             "// 八  九\n"
19455             "// 十",
19456             format("// 一二三 四五六七 八  九 十", getLLVMStyleWithColumns(9)));
19457 }
19458 
19459 TEST_F(FormatTest, SplitsUTF8BlockComments) {
19460   EXPECT_EQ("/* Гляжу,\n"
19461             " * поднимается\n"
19462             " * медленно в\n"
19463             " * гору\n"
19464             " * Лошадка,\n"
19465             " * везущая\n"
19466             " * хворосту\n"
19467             " * воз. */",
19468             format("/* Гляжу, поднимается медленно в гору\n"
19469                    " * Лошадка, везущая хворосту воз. */",
19470                    getLLVMStyleWithColumns(13)));
19471   EXPECT_EQ(
19472       "/* 一二三\n"
19473       " * 四五六七\n"
19474       " * 八  九\n"
19475       " * 十  */",
19476       format("/* 一二三 四五六七 八  九 十  */", getLLVMStyleWithColumns(9)));
19477   EXPECT_EQ("/* �������� ��������\n"
19478             " * ��������\n"
19479             " * ������-�� */",
19480             format("/* �������� �������� �������� ������-�� */", getLLVMStyleWithColumns(12)));
19481 }
19482 
19483 #endif // _MSC_VER
19484 
19485 TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
19486   FormatStyle Style = getLLVMStyle();
19487 
19488   Style.ConstructorInitializerIndentWidth = 4;
19489   verifyFormat(
19490       "SomeClass::Constructor()\n"
19491       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
19492       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
19493       Style);
19494 
19495   Style.ConstructorInitializerIndentWidth = 2;
19496   verifyFormat(
19497       "SomeClass::Constructor()\n"
19498       "  : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
19499       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
19500       Style);
19501 
19502   Style.ConstructorInitializerIndentWidth = 0;
19503   verifyFormat(
19504       "SomeClass::Constructor()\n"
19505       ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
19506       "  aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
19507       Style);
19508   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
19509   verifyFormat(
19510       "SomeLongTemplateVariableName<\n"
19511       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>",
19512       Style);
19513   verifyFormat("bool smaller = 1 < "
19514                "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
19515                "                       "
19516                "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
19517                Style);
19518 
19519   Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
19520   verifyFormat("SomeClass::Constructor() :\n"
19521                "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa),\n"
19522                "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa) {}",
19523                Style);
19524 }
19525 
19526 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {
19527   FormatStyle Style = getLLVMStyle();
19528   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
19529   Style.ConstructorInitializerIndentWidth = 4;
19530   verifyFormat("SomeClass::Constructor()\n"
19531                "    : a(a)\n"
19532                "    , b(b)\n"
19533                "    , c(c) {}",
19534                Style);
19535   verifyFormat("SomeClass::Constructor()\n"
19536                "    : a(a) {}",
19537                Style);
19538 
19539   Style.ColumnLimit = 0;
19540   verifyFormat("SomeClass::Constructor()\n"
19541                "    : a(a) {}",
19542                Style);
19543   verifyFormat("SomeClass::Constructor() noexcept\n"
19544                "    : a(a) {}",
19545                Style);
19546   verifyFormat("SomeClass::Constructor()\n"
19547                "    : a(a)\n"
19548                "    , b(b)\n"
19549                "    , c(c) {}",
19550                Style);
19551   verifyFormat("SomeClass::Constructor()\n"
19552                "    : a(a) {\n"
19553                "  foo();\n"
19554                "  bar();\n"
19555                "}",
19556                Style);
19557 
19558   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
19559   verifyFormat("SomeClass::Constructor()\n"
19560                "    : a(a)\n"
19561                "    , b(b)\n"
19562                "    , c(c) {\n}",
19563                Style);
19564   verifyFormat("SomeClass::Constructor()\n"
19565                "    : a(a) {\n}",
19566                Style);
19567 
19568   Style.ColumnLimit = 80;
19569   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
19570   Style.ConstructorInitializerIndentWidth = 2;
19571   verifyFormat("SomeClass::Constructor()\n"
19572                "  : a(a)\n"
19573                "  , b(b)\n"
19574                "  , c(c) {}",
19575                Style);
19576 
19577   Style.ConstructorInitializerIndentWidth = 0;
19578   verifyFormat("SomeClass::Constructor()\n"
19579                ": a(a)\n"
19580                ", b(b)\n"
19581                ", c(c) {}",
19582                Style);
19583 
19584   Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
19585   Style.ConstructorInitializerIndentWidth = 4;
19586   verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
19587   verifyFormat(
19588       "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n",
19589       Style);
19590   verifyFormat(
19591       "SomeClass::Constructor()\n"
19592       "    : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
19593       Style);
19594   Style.ConstructorInitializerIndentWidth = 4;
19595   Style.ColumnLimit = 60;
19596   verifyFormat("SomeClass::Constructor()\n"
19597                "    : aaaaaaaa(aaaaaaaa)\n"
19598                "    , aaaaaaaa(aaaaaaaa)\n"
19599                "    , aaaaaaaa(aaaaaaaa) {}",
19600                Style);
19601 }
19602 
19603 TEST_F(FormatTest, ConstructorInitializersWithPreprocessorDirective) {
19604   FormatStyle Style = getLLVMStyle();
19605   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
19606   Style.ConstructorInitializerIndentWidth = 4;
19607   verifyFormat("SomeClass::Constructor()\n"
19608                "    : a{a}\n"
19609                "    , b{b} {}",
19610                Style);
19611   verifyFormat("SomeClass::Constructor()\n"
19612                "    : a{a}\n"
19613                "#if CONDITION\n"
19614                "    , b{b}\n"
19615                "#endif\n"
19616                "{\n}",
19617                Style);
19618   Style.ConstructorInitializerIndentWidth = 2;
19619   verifyFormat("SomeClass::Constructor()\n"
19620                "#if CONDITION\n"
19621                "  : a{a}\n"
19622                "#endif\n"
19623                "  , b{b}\n"
19624                "  , c{c} {\n}",
19625                Style);
19626   Style.ConstructorInitializerIndentWidth = 0;
19627   verifyFormat("SomeClass::Constructor()\n"
19628                ": a{a}\n"
19629                "#ifdef CONDITION\n"
19630                ", b{b}\n"
19631                "#else\n"
19632                ", c{c}\n"
19633                "#endif\n"
19634                ", d{d} {\n}",
19635                Style);
19636   Style.ConstructorInitializerIndentWidth = 4;
19637   verifyFormat("SomeClass::Constructor()\n"
19638                "    : a{a}\n"
19639                "#if WINDOWS\n"
19640                "#if DEBUG\n"
19641                "    , b{0}\n"
19642                "#else\n"
19643                "    , b{1}\n"
19644                "#endif\n"
19645                "#else\n"
19646                "#if DEBUG\n"
19647                "    , b{2}\n"
19648                "#else\n"
19649                "    , b{3}\n"
19650                "#endif\n"
19651                "#endif\n"
19652                "{\n}",
19653                Style);
19654   verifyFormat("SomeClass::Constructor()\n"
19655                "    : a{a}\n"
19656                "#if WINDOWS\n"
19657                "    , b{0}\n"
19658                "#if DEBUG\n"
19659                "    , c{0}\n"
19660                "#else\n"
19661                "    , c{1}\n"
19662                "#endif\n"
19663                "#else\n"
19664                "#if DEBUG\n"
19665                "    , c{2}\n"
19666                "#else\n"
19667                "    , c{3}\n"
19668                "#endif\n"
19669                "    , b{1}\n"
19670                "#endif\n"
19671                "{\n}",
19672                Style);
19673 }
19674 
19675 TEST_F(FormatTest, Destructors) {
19676   verifyFormat("void F(int &i) { i.~int(); }");
19677   verifyFormat("void F(int &i) { i->~int(); }");
19678 }
19679 
19680 TEST_F(FormatTest, FormatsWithWebKitStyle) {
19681   FormatStyle Style = getWebKitStyle();
19682 
19683   // Don't indent in outer namespaces.
19684   verifyFormat("namespace outer {\n"
19685                "int i;\n"
19686                "namespace inner {\n"
19687                "    int i;\n"
19688                "} // namespace inner\n"
19689                "} // namespace outer\n"
19690                "namespace other_outer {\n"
19691                "int i;\n"
19692                "}",
19693                Style);
19694 
19695   // Don't indent case labels.
19696   verifyFormat("switch (variable) {\n"
19697                "case 1:\n"
19698                "case 2:\n"
19699                "    doSomething();\n"
19700                "    break;\n"
19701                "default:\n"
19702                "    ++variable;\n"
19703                "}",
19704                Style);
19705 
19706   // Wrap before binary operators.
19707   EXPECT_EQ("void f()\n"
19708             "{\n"
19709             "    if (aaaaaaaaaaaaaaaa\n"
19710             "        && bbbbbbbbbbbbbbbbbbbbbbbb\n"
19711             "        && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
19712             "        return;\n"
19713             "}",
19714             format("void f() {\n"
19715                    "if (aaaaaaaaaaaaaaaa\n"
19716                    "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
19717                    "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
19718                    "return;\n"
19719                    "}",
19720                    Style));
19721 
19722   // Allow functions on a single line.
19723   verifyFormat("void f() { return; }", Style);
19724 
19725   // Allow empty blocks on a single line and insert a space in empty blocks.
19726   EXPECT_EQ("void f() { }", format("void f() {}", Style));
19727   EXPECT_EQ("while (true) { }", format("while (true) {}", Style));
19728   // However, don't merge non-empty short loops.
19729   EXPECT_EQ("while (true) {\n"
19730             "    continue;\n"
19731             "}",
19732             format("while (true) { continue; }", Style));
19733 
19734   // Constructor initializers are formatted one per line with the "," on the
19735   // new line.
19736   verifyFormat("Constructor()\n"
19737                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
19738                "    , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
19739                "          aaaaaaaaaaaaaa)\n"
19740                "    , aaaaaaaaaaaaaaaaaaaaaaa()\n"
19741                "{\n"
19742                "}",
19743                Style);
19744   verifyFormat("SomeClass::Constructor()\n"
19745                "    : a(a)\n"
19746                "{\n"
19747                "}",
19748                Style);
19749   EXPECT_EQ("SomeClass::Constructor()\n"
19750             "    : a(a)\n"
19751             "{\n"
19752             "}",
19753             format("SomeClass::Constructor():a(a){}", Style));
19754   verifyFormat("SomeClass::Constructor()\n"
19755                "    : a(a)\n"
19756                "    , b(b)\n"
19757                "    , c(c)\n"
19758                "{\n"
19759                "}",
19760                Style);
19761   verifyFormat("SomeClass::Constructor()\n"
19762                "    : a(a)\n"
19763                "{\n"
19764                "    foo();\n"
19765                "    bar();\n"
19766                "}",
19767                Style);
19768 
19769   // Access specifiers should be aligned left.
19770   verifyFormat("class C {\n"
19771                "public:\n"
19772                "    int i;\n"
19773                "};",
19774                Style);
19775 
19776   // Do not align comments.
19777   verifyFormat("int a; // Do not\n"
19778                "double b; // align comments.",
19779                Style);
19780 
19781   // Do not align operands.
19782   EXPECT_EQ("ASSERT(aaaa\n"
19783             "    || bbbb);",
19784             format("ASSERT ( aaaa\n||bbbb);", Style));
19785 
19786   // Accept input's line breaks.
19787   EXPECT_EQ("if (aaaaaaaaaaaaaaa\n"
19788             "    || bbbbbbbbbbbbbbb) {\n"
19789             "    i++;\n"
19790             "}",
19791             format("if (aaaaaaaaaaaaaaa\n"
19792                    "|| bbbbbbbbbbbbbbb) { i++; }",
19793                    Style));
19794   EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
19795             "    i++;\n"
19796             "}",
19797             format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
19798 
19799   // Don't automatically break all macro definitions (llvm.org/PR17842).
19800   verifyFormat("#define aNumber 10", Style);
19801   // However, generally keep the line breaks that the user authored.
19802   EXPECT_EQ("#define aNumber \\\n"
19803             "    10",
19804             format("#define aNumber \\\n"
19805                    " 10",
19806                    Style));
19807 
19808   // Keep empty and one-element array literals on a single line.
19809   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
19810             "                                  copyItems:YES];",
19811             format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
19812                    "copyItems:YES];",
19813                    Style));
19814   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
19815             "                                  copyItems:YES];",
19816             format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
19817                    "             copyItems:YES];",
19818                    Style));
19819   // FIXME: This does not seem right, there should be more indentation before
19820   // the array literal's entries. Nested blocks have the same problem.
19821   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
19822             "    @\"a\",\n"
19823             "    @\"a\"\n"
19824             "]\n"
19825             "                                  copyItems:YES];",
19826             format("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
19827                    "     @\"a\",\n"
19828                    "     @\"a\"\n"
19829                    "     ]\n"
19830                    "       copyItems:YES];",
19831                    Style));
19832   EXPECT_EQ(
19833       "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
19834       "                                  copyItems:YES];",
19835       format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
19836              "   copyItems:YES];",
19837              Style));
19838 
19839   verifyFormat("[self.a b:c c:d];", Style);
19840   EXPECT_EQ("[self.a b:c\n"
19841             "        c:d];",
19842             format("[self.a b:c\n"
19843                    "c:d];",
19844                    Style));
19845 }
19846 
19847 TEST_F(FormatTest, FormatsLambdas) {
19848   verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n");
19849   verifyFormat(
19850       "int c = [b]() mutable noexcept { return [&b] { return b++; }(); }();\n");
19851   verifyFormat("int c = [&] { [=] { return b++; }(); }();\n");
19852   verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n");
19853   verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n");
19854   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n");
19855   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n");
19856   verifyFormat("auto c = [a = [b = 42] {}] {};\n");
19857   verifyFormat("auto c = [a = &i + 10, b = [] {}] {};\n");
19858   verifyFormat("int x = f(*+[] {});");
19859   verifyFormat("void f() {\n"
19860                "  other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
19861                "}\n");
19862   verifyFormat("void f() {\n"
19863                "  other(x.begin(), //\n"
19864                "        x.end(),   //\n"
19865                "        [&](int, int) { return 1; });\n"
19866                "}\n");
19867   verifyFormat("void f() {\n"
19868                "  other.other.other.other.other(\n"
19869                "      x.begin(), x.end(),\n"
19870                "      [something, rather](int, int, int, int, int, int, int) { "
19871                "return 1; });\n"
19872                "}\n");
19873   verifyFormat(
19874       "void f() {\n"
19875       "  other.other.other.other.other(\n"
19876       "      x.begin(), x.end(),\n"
19877       "      [something, rather](int, int, int, int, int, int, int) {\n"
19878       "        //\n"
19879       "      });\n"
19880       "}\n");
19881   verifyFormat("SomeFunction([]() { // A cool function...\n"
19882                "  return 43;\n"
19883                "});");
19884   EXPECT_EQ("SomeFunction([]() {\n"
19885             "#define A a\n"
19886             "  return 43;\n"
19887             "});",
19888             format("SomeFunction([](){\n"
19889                    "#define A a\n"
19890                    "return 43;\n"
19891                    "});"));
19892   verifyFormat("void f() {\n"
19893                "  SomeFunction([](decltype(x), A *a) {});\n"
19894                "  SomeFunction([](typeof(x), A *a) {});\n"
19895                "  SomeFunction([](_Atomic(x), A *a) {});\n"
19896                "  SomeFunction([](__underlying_type(x), A *a) {});\n"
19897                "}");
19898   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
19899                "    [](const aaaaaaaaaa &a) { return a; });");
19900   verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
19901                "  SomeOtherFunctioooooooooooooooooooooooooon();\n"
19902                "});");
19903   verifyFormat("Constructor()\n"
19904                "    : Field([] { // comment\n"
19905                "        int i;\n"
19906                "      }) {}");
19907   verifyFormat("auto my_lambda = [](const string &some_parameter) {\n"
19908                "  return some_parameter.size();\n"
19909                "};");
19910   verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n"
19911                "    [](const string &s) { return s; };");
19912   verifyFormat("int i = aaaaaa ? 1 //\n"
19913                "               : [] {\n"
19914                "                   return 2; //\n"
19915                "                 }();");
19916   verifyFormat("llvm::errs() << \"number of twos is \"\n"
19917                "             << std::count_if(v.begin(), v.end(), [](int x) {\n"
19918                "                  return x == 2; // force break\n"
19919                "                });");
19920   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
19921                "    [=](int iiiiiiiiiiii) {\n"
19922                "      return aaaaaaaaaaaaaaaaaaaaaaa !=\n"
19923                "             aaaaaaaaaaaaaaaaaaaaaaa;\n"
19924                "    });",
19925                getLLVMStyleWithColumns(60));
19926 
19927   verifyFormat("SomeFunction({[&] {\n"
19928                "                // comment\n"
19929                "              },\n"
19930                "              [&] {\n"
19931                "                // comment\n"
19932                "              }});");
19933   verifyFormat("SomeFunction({[&] {\n"
19934                "  // comment\n"
19935                "}});");
19936   verifyFormat(
19937       "virtual aaaaaaaaaaaaaaaa(\n"
19938       "    std::function<bool()> bbbbbbbbbbbb = [&]() { return true; },\n"
19939       "    aaaaa aaaaaaaaa);");
19940 
19941   // Lambdas with return types.
19942   verifyFormat("int c = []() -> int { return 2; }();\n");
19943   verifyFormat("int c = []() -> int * { return 2; }();\n");
19944   verifyFormat("int c = []() -> vector<int> { return {2}; }();\n");
19945   verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
19946   verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
19947   verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
19948   verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
19949   verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
19950   verifyFormat("[a, a]() -> a<1> {};");
19951   verifyFormat("[]() -> foo<5 + 2> { return {}; };");
19952   verifyFormat("[]() -> foo<5 - 2> { return {}; };");
19953   verifyFormat("[]() -> foo<5 / 2> { return {}; };");
19954   verifyFormat("[]() -> foo<5 * 2> { return {}; };");
19955   verifyFormat("[]() -> foo<5 % 2> { return {}; };");
19956   verifyFormat("[]() -> foo<5 << 2> { return {}; };");
19957   verifyFormat("[]() -> foo<!5> { return {}; };");
19958   verifyFormat("[]() -> foo<~5> { return {}; };");
19959   verifyFormat("[]() -> foo<5 | 2> { return {}; };");
19960   verifyFormat("[]() -> foo<5 || 2> { return {}; };");
19961   verifyFormat("[]() -> foo<5 & 2> { return {}; };");
19962   verifyFormat("[]() -> foo<5 && 2> { return {}; };");
19963   verifyFormat("[]() -> foo<5 == 2> { return {}; };");
19964   verifyFormat("[]() -> foo<5 != 2> { return {}; };");
19965   verifyFormat("[]() -> foo<5 >= 2> { return {}; };");
19966   verifyFormat("[]() -> foo<5 <= 2> { return {}; };");
19967   verifyFormat("[]() -> foo<5 < 2> { return {}; };");
19968   verifyFormat("[]() -> foo<2 ? 1 : 0> { return {}; };");
19969   verifyFormat("namespace bar {\n"
19970                "// broken:\n"
19971                "auto foo{[]() -> foo<5 + 2> { return {}; }};\n"
19972                "} // namespace bar");
19973   verifyFormat("namespace bar {\n"
19974                "// broken:\n"
19975                "auto foo{[]() -> foo<5 - 2> { return {}; }};\n"
19976                "} // namespace bar");
19977   verifyFormat("namespace bar {\n"
19978                "// broken:\n"
19979                "auto foo{[]() -> foo<5 / 2> { return {}; }};\n"
19980                "} // namespace bar");
19981   verifyFormat("namespace bar {\n"
19982                "// broken:\n"
19983                "auto foo{[]() -> foo<5 * 2> { return {}; }};\n"
19984                "} // namespace bar");
19985   verifyFormat("namespace bar {\n"
19986                "// broken:\n"
19987                "auto foo{[]() -> foo<5 % 2> { return {}; }};\n"
19988                "} // namespace bar");
19989   verifyFormat("namespace bar {\n"
19990                "// broken:\n"
19991                "auto foo{[]() -> foo<5 << 2> { return {}; }};\n"
19992                "} // namespace bar");
19993   verifyFormat("namespace bar {\n"
19994                "// broken:\n"
19995                "auto foo{[]() -> foo<!5> { return {}; }};\n"
19996                "} // namespace bar");
19997   verifyFormat("namespace bar {\n"
19998                "// broken:\n"
19999                "auto foo{[]() -> foo<~5> { return {}; }};\n"
20000                "} // namespace bar");
20001   verifyFormat("namespace bar {\n"
20002                "// broken:\n"
20003                "auto foo{[]() -> foo<5 | 2> { return {}; }};\n"
20004                "} // namespace bar");
20005   verifyFormat("namespace bar {\n"
20006                "// broken:\n"
20007                "auto foo{[]() -> foo<5 || 2> { return {}; }};\n"
20008                "} // namespace bar");
20009   verifyFormat("namespace bar {\n"
20010                "// broken:\n"
20011                "auto foo{[]() -> foo<5 & 2> { return {}; }};\n"
20012                "} // namespace bar");
20013   verifyFormat("namespace bar {\n"
20014                "// broken:\n"
20015                "auto foo{[]() -> foo<5 && 2> { return {}; }};\n"
20016                "} // namespace bar");
20017   verifyFormat("namespace bar {\n"
20018                "// broken:\n"
20019                "auto foo{[]() -> foo<5 == 2> { return {}; }};\n"
20020                "} // namespace bar");
20021   verifyFormat("namespace bar {\n"
20022                "// broken:\n"
20023                "auto foo{[]() -> foo<5 != 2> { return {}; }};\n"
20024                "} // namespace bar");
20025   verifyFormat("namespace bar {\n"
20026                "// broken:\n"
20027                "auto foo{[]() -> foo<5 >= 2> { return {}; }};\n"
20028                "} // namespace bar");
20029   verifyFormat("namespace bar {\n"
20030                "// broken:\n"
20031                "auto foo{[]() -> foo<5 <= 2> { return {}; }};\n"
20032                "} // namespace bar");
20033   verifyFormat("namespace bar {\n"
20034                "// broken:\n"
20035                "auto foo{[]() -> foo<5 < 2> { return {}; }};\n"
20036                "} // namespace bar");
20037   verifyFormat("namespace bar {\n"
20038                "// broken:\n"
20039                "auto foo{[]() -> foo<2 ? 1 : 0> { return {}; }};\n"
20040                "} // namespace bar");
20041   verifyFormat("[]() -> a<1> {};");
20042   verifyFormat("[]() -> a<1> { ; };");
20043   verifyFormat("[]() -> a<1> { ; }();");
20044   verifyFormat("[a, a]() -> a<true> {};");
20045   verifyFormat("[]() -> a<true> {};");
20046   verifyFormat("[]() -> a<true> { ; };");
20047   verifyFormat("[]() -> a<true> { ; }();");
20048   verifyFormat("[a, a]() -> a<false> {};");
20049   verifyFormat("[]() -> a<false> {};");
20050   verifyFormat("[]() -> a<false> { ; };");
20051   verifyFormat("[]() -> a<false> { ; }();");
20052   verifyFormat("auto foo{[]() -> foo<false> { ; }};");
20053   verifyFormat("namespace bar {\n"
20054                "auto foo{[]() -> foo<false> { ; }};\n"
20055                "} // namespace bar");
20056   verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
20057                "                   int j) -> int {\n"
20058                "  return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
20059                "};");
20060   verifyFormat(
20061       "aaaaaaaaaaaaaaaaaaaaaa(\n"
20062       "    [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n"
20063       "      return aaaaaaaaaaaaaaaaa;\n"
20064       "    });",
20065       getLLVMStyleWithColumns(70));
20066   verifyFormat("[]() //\n"
20067                "    -> int {\n"
20068                "  return 1; //\n"
20069                "};");
20070   verifyFormat("[]() -> Void<T...> {};");
20071   verifyFormat("[a, b]() -> Tuple<T...> { return {}; };");
20072 
20073   // Lambdas with explicit template argument lists.
20074   verifyFormat(
20075       "auto L = []<template <typename> class T, class U>(T<U> &&a) {};\n");
20076 
20077   // Multiple lambdas in the same parentheses change indentation rules. These
20078   // lambdas are forced to start on new lines.
20079   verifyFormat("SomeFunction(\n"
20080                "    []() {\n"
20081                "      //\n"
20082                "    },\n"
20083                "    []() {\n"
20084                "      //\n"
20085                "    });");
20086 
20087   // A lambda passed as arg0 is always pushed to the next line.
20088   verifyFormat("SomeFunction(\n"
20089                "    [this] {\n"
20090                "      //\n"
20091                "    },\n"
20092                "    1);\n");
20093 
20094   // A multi-line lambda passed as arg1 forces arg0 to be pushed out, just like
20095   // the arg0 case above.
20096   auto Style = getGoogleStyle();
20097   Style.BinPackArguments = false;
20098   verifyFormat("SomeFunction(\n"
20099                "    a,\n"
20100                "    [this] {\n"
20101                "      //\n"
20102                "    },\n"
20103                "    b);\n",
20104                Style);
20105   verifyFormat("SomeFunction(\n"
20106                "    a,\n"
20107                "    [this] {\n"
20108                "      //\n"
20109                "    },\n"
20110                "    b);\n");
20111 
20112   // A lambda with a very long line forces arg0 to be pushed out irrespective of
20113   // the BinPackArguments value (as long as the code is wide enough).
20114   verifyFormat(
20115       "something->SomeFunction(\n"
20116       "    a,\n"
20117       "    [this] {\n"
20118       "      "
20119       "D0000000000000000000000000000000000000000000000000000000000001();\n"
20120       "    },\n"
20121       "    b);\n");
20122 
20123   // A multi-line lambda is pulled up as long as the introducer fits on the
20124   // previous line and there are no further args.
20125   verifyFormat("function(1, [this, that] {\n"
20126                "  //\n"
20127                "});\n");
20128   verifyFormat("function([this, that] {\n"
20129                "  //\n"
20130                "});\n");
20131   // FIXME: this format is not ideal and we should consider forcing the first
20132   // arg onto its own line.
20133   verifyFormat("function(a, b, c, //\n"
20134                "         d, [this, that] {\n"
20135                "           //\n"
20136                "         });\n");
20137 
20138   // Multiple lambdas are treated correctly even when there is a short arg0.
20139   verifyFormat("SomeFunction(\n"
20140                "    1,\n"
20141                "    [this] {\n"
20142                "      //\n"
20143                "    },\n"
20144                "    [this] {\n"
20145                "      //\n"
20146                "    },\n"
20147                "    1);\n");
20148 
20149   // More complex introducers.
20150   verifyFormat("return [i, args...] {};");
20151 
20152   // Not lambdas.
20153   verifyFormat("constexpr char hello[]{\"hello\"};");
20154   verifyFormat("double &operator[](int i) { return 0; }\n"
20155                "int i;");
20156   verifyFormat("std::unique_ptr<int[]> foo() {}");
20157   verifyFormat("int i = a[a][a]->f();");
20158   verifyFormat("int i = (*b)[a]->f();");
20159 
20160   // Other corner cases.
20161   verifyFormat("void f() {\n"
20162                "  bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
20163                "  );\n"
20164                "}");
20165 
20166   // Lambdas created through weird macros.
20167   verifyFormat("void f() {\n"
20168                "  MACRO((const AA &a) { return 1; });\n"
20169                "  MACRO((AA &a) { return 1; });\n"
20170                "}");
20171 
20172   verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
20173                "      doo_dah();\n"
20174                "      doo_dah();\n"
20175                "    })) {\n"
20176                "}");
20177   verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n"
20178                "                doo_dah();\n"
20179                "                doo_dah();\n"
20180                "              })) {\n"
20181                "}");
20182   verifyFormat("if CONSTEXPR (blah_blah(whatever, whatever, [] {\n"
20183                "                doo_dah();\n"
20184                "                doo_dah();\n"
20185                "              })) {\n"
20186                "}");
20187   verifyFormat("auto lambda = []() {\n"
20188                "  int a = 2\n"
20189                "#if A\n"
20190                "          + 2\n"
20191                "#endif\n"
20192                "      ;\n"
20193                "};");
20194 
20195   // Lambdas with complex multiline introducers.
20196   verifyFormat(
20197       "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
20198       "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n"
20199       "        -> ::std::unordered_set<\n"
20200       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
20201       "      //\n"
20202       "    });");
20203 
20204   FormatStyle DoNotMerge = getLLVMStyle();
20205   DoNotMerge.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
20206   verifyFormat("auto c = []() {\n"
20207                "  return b;\n"
20208                "};",
20209                "auto c = []() { return b; };", DoNotMerge);
20210   verifyFormat("auto c = []() {\n"
20211                "};",
20212                " auto c = []() {};", DoNotMerge);
20213 
20214   FormatStyle MergeEmptyOnly = getLLVMStyle();
20215   MergeEmptyOnly.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Empty;
20216   verifyFormat("auto c = []() {\n"
20217                "  return b;\n"
20218                "};",
20219                "auto c = []() {\n"
20220                "  return b;\n"
20221                " };",
20222                MergeEmptyOnly);
20223   verifyFormat("auto c = []() {};",
20224                "auto c = []() {\n"
20225                "};",
20226                MergeEmptyOnly);
20227 
20228   FormatStyle MergeInline = getLLVMStyle();
20229   MergeInline.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Inline;
20230   verifyFormat("auto c = []() {\n"
20231                "  return b;\n"
20232                "};",
20233                "auto c = []() { return b; };", MergeInline);
20234   verifyFormat("function([]() { return b; })", "function([]() { return b; })",
20235                MergeInline);
20236   verifyFormat("function([]() { return b; }, a)",
20237                "function([]() { return b; }, a)", MergeInline);
20238   verifyFormat("function(a, []() { return b; })",
20239                "function(a, []() { return b; })", MergeInline);
20240 
20241   // Check option "BraceWrapping.BeforeLambdaBody" and different state of
20242   // AllowShortLambdasOnASingleLine
20243   FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle();
20244   LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
20245   LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
20246   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
20247       FormatStyle::ShortLambdaStyle::SLS_None;
20248   verifyFormat("FctWithOneNestedLambdaInline_SLS_None(\n"
20249                "    []()\n"
20250                "    {\n"
20251                "      return 17;\n"
20252                "    });",
20253                LLVMWithBeforeLambdaBody);
20254   verifyFormat("FctWithOneNestedLambdaEmpty_SLS_None(\n"
20255                "    []()\n"
20256                "    {\n"
20257                "    });",
20258                LLVMWithBeforeLambdaBody);
20259   verifyFormat("auto fct_SLS_None = []()\n"
20260                "{\n"
20261                "  return 17;\n"
20262                "};",
20263                LLVMWithBeforeLambdaBody);
20264   verifyFormat("TwoNestedLambdas_SLS_None(\n"
20265                "    []()\n"
20266                "    {\n"
20267                "      return Call(\n"
20268                "          []()\n"
20269                "          {\n"
20270                "            return 17;\n"
20271                "          });\n"
20272                "    });",
20273                LLVMWithBeforeLambdaBody);
20274   verifyFormat("void Fct() {\n"
20275                "  return {[]()\n"
20276                "          {\n"
20277                "            return 17;\n"
20278                "          }};\n"
20279                "}",
20280                LLVMWithBeforeLambdaBody);
20281 
20282   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
20283       FormatStyle::ShortLambdaStyle::SLS_Empty;
20284   verifyFormat("FctWithOneNestedLambdaInline_SLS_Empty(\n"
20285                "    []()\n"
20286                "    {\n"
20287                "      return 17;\n"
20288                "    });",
20289                LLVMWithBeforeLambdaBody);
20290   verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Empty([]() {});",
20291                LLVMWithBeforeLambdaBody);
20292   verifyFormat("FctWithOneNestedLambdaEmptyInsideAVeryVeryVeryVeryVeryVeryVeryL"
20293                "ongFunctionName_SLS_Empty(\n"
20294                "    []() {});",
20295                LLVMWithBeforeLambdaBody);
20296   verifyFormat("FctWithMultipleParams_SLS_Empty(A, B,\n"
20297                "                                []()\n"
20298                "                                {\n"
20299                "                                  return 17;\n"
20300                "                                });",
20301                LLVMWithBeforeLambdaBody);
20302   verifyFormat("auto fct_SLS_Empty = []()\n"
20303                "{\n"
20304                "  return 17;\n"
20305                "};",
20306                LLVMWithBeforeLambdaBody);
20307   verifyFormat("TwoNestedLambdas_SLS_Empty(\n"
20308                "    []()\n"
20309                "    {\n"
20310                "      return Call([]() {});\n"
20311                "    });",
20312                LLVMWithBeforeLambdaBody);
20313   verifyFormat("TwoNestedLambdas_SLS_Empty(A,\n"
20314                "                           []()\n"
20315                "                           {\n"
20316                "                             return Call([]() {});\n"
20317                "                           });",
20318                LLVMWithBeforeLambdaBody);
20319   verifyFormat(
20320       "FctWithLongLineInLambda_SLS_Empty(\n"
20321       "    []()\n"
20322       "    {\n"
20323       "      return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
20324       "                               AndShouldNotBeConsiderAsInline,\n"
20325       "                               LambdaBodyMustBeBreak);\n"
20326       "    });",
20327       LLVMWithBeforeLambdaBody);
20328 
20329   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
20330       FormatStyle::ShortLambdaStyle::SLS_Inline;
20331   verifyFormat("FctWithOneNestedLambdaInline_SLS_Inline([]() { return 17; });",
20332                LLVMWithBeforeLambdaBody);
20333   verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Inline([]() {});",
20334                LLVMWithBeforeLambdaBody);
20335   verifyFormat("auto fct_SLS_Inline = []()\n"
20336                "{\n"
20337                "  return 17;\n"
20338                "};",
20339                LLVMWithBeforeLambdaBody);
20340   verifyFormat("TwoNestedLambdas_SLS_Inline([]() { return Call([]() { return "
20341                "17; }); });",
20342                LLVMWithBeforeLambdaBody);
20343   verifyFormat(
20344       "FctWithLongLineInLambda_SLS_Inline(\n"
20345       "    []()\n"
20346       "    {\n"
20347       "      return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
20348       "                               AndShouldNotBeConsiderAsInline,\n"
20349       "                               LambdaBodyMustBeBreak);\n"
20350       "    });",
20351       LLVMWithBeforeLambdaBody);
20352   verifyFormat("FctWithMultipleParams_SLS_Inline("
20353                "VeryLongParameterThatShouldAskToBeOnMultiLine,\n"
20354                "                                 []() { return 17; });",
20355                LLVMWithBeforeLambdaBody);
20356   verifyFormat(
20357       "FctWithMultipleParams_SLS_Inline(FirstParam, []() { return 17; });",
20358       LLVMWithBeforeLambdaBody);
20359 
20360   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
20361       FormatStyle::ShortLambdaStyle::SLS_All;
20362   verifyFormat("FctWithOneNestedLambdaInline_SLS_All([]() { return 17; });",
20363                LLVMWithBeforeLambdaBody);
20364   verifyFormat("FctWithOneNestedLambdaEmpty_SLS_All([]() {});",
20365                LLVMWithBeforeLambdaBody);
20366   verifyFormat("auto fct_SLS_All = []() { return 17; };",
20367                LLVMWithBeforeLambdaBody);
20368   verifyFormat("FctWithOneParam_SLS_All(\n"
20369                "    []()\n"
20370                "    {\n"
20371                "      // A cool function...\n"
20372                "      return 43;\n"
20373                "    });",
20374                LLVMWithBeforeLambdaBody);
20375   verifyFormat("FctWithMultipleParams_SLS_All("
20376                "VeryLongParameterThatShouldAskToBeOnMultiLine,\n"
20377                "                              []() { return 17; });",
20378                LLVMWithBeforeLambdaBody);
20379   verifyFormat("FctWithMultipleParams_SLS_All(A, []() { return 17; });",
20380                LLVMWithBeforeLambdaBody);
20381   verifyFormat("FctWithMultipleParams_SLS_All(A, B, []() { return 17; });",
20382                LLVMWithBeforeLambdaBody);
20383   verifyFormat(
20384       "FctWithLongLineInLambda_SLS_All(\n"
20385       "    []()\n"
20386       "    {\n"
20387       "      return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
20388       "                               AndShouldNotBeConsiderAsInline,\n"
20389       "                               LambdaBodyMustBeBreak);\n"
20390       "    });",
20391       LLVMWithBeforeLambdaBody);
20392   verifyFormat(
20393       "auto fct_SLS_All = []()\n"
20394       "{\n"
20395       "  return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
20396       "                           AndShouldNotBeConsiderAsInline,\n"
20397       "                           LambdaBodyMustBeBreak);\n"
20398       "};",
20399       LLVMWithBeforeLambdaBody);
20400   LLVMWithBeforeLambdaBody.BinPackParameters = false;
20401   verifyFormat("FctAllOnSameLine_SLS_All([]() { return S; }, Fst, Second);",
20402                LLVMWithBeforeLambdaBody);
20403   verifyFormat(
20404       "FctWithLongLineInLambda_SLS_All([]() { return SomeValueNotSoLong; },\n"
20405       "                                FirstParam,\n"
20406       "                                SecondParam,\n"
20407       "                                ThirdParam,\n"
20408       "                                FourthParam);",
20409       LLVMWithBeforeLambdaBody);
20410   verifyFormat("FctWithLongLineInLambda_SLS_All(\n"
20411                "    []() { return "
20412                "SomeValueVeryVeryVeryVeryVeryVeryVeryVeryVeryLong; },\n"
20413                "    FirstParam,\n"
20414                "    SecondParam,\n"
20415                "    ThirdParam,\n"
20416                "    FourthParam);",
20417                LLVMWithBeforeLambdaBody);
20418   verifyFormat(
20419       "FctWithLongLineInLambda_SLS_All(FirstParam,\n"
20420       "                                SecondParam,\n"
20421       "                                ThirdParam,\n"
20422       "                                FourthParam,\n"
20423       "                                []() { return SomeValueNotSoLong; });",
20424       LLVMWithBeforeLambdaBody);
20425   verifyFormat("FctWithLongLineInLambda_SLS_All(\n"
20426                "    []()\n"
20427                "    {\n"
20428                "      return "
20429                "HereAVeryLongLineThatWillBeFormattedOnMultipleLineAndShouldNotB"
20430                "eConsiderAsInline;\n"
20431                "    });",
20432                LLVMWithBeforeLambdaBody);
20433   verifyFormat(
20434       "FctWithLongLineInLambda_SLS_All(\n"
20435       "    []()\n"
20436       "    {\n"
20437       "      return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
20438       "                               AndShouldNotBeConsiderAsInline,\n"
20439       "                               LambdaBodyMustBeBreak);\n"
20440       "    });",
20441       LLVMWithBeforeLambdaBody);
20442   verifyFormat("FctWithTwoParams_SLS_All(\n"
20443                "    []()\n"
20444                "    {\n"
20445                "      // A cool function...\n"
20446                "      return 43;\n"
20447                "    },\n"
20448                "    87);",
20449                LLVMWithBeforeLambdaBody);
20450   verifyFormat("FctWithTwoParams_SLS_All([]() { return 43; }, 87);",
20451                LLVMWithBeforeLambdaBody);
20452   verifyFormat("FctWithOneNestedLambdas_SLS_All([]() { return 17; });",
20453                LLVMWithBeforeLambdaBody);
20454   verifyFormat(
20455       "TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; }); });",
20456       LLVMWithBeforeLambdaBody);
20457   verifyFormat("TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; "
20458                "}); }, x);",
20459                LLVMWithBeforeLambdaBody);
20460   verifyFormat("TwoNestedLambdas_SLS_All(\n"
20461                "    []()\n"
20462                "    {\n"
20463                "      // A cool function...\n"
20464                "      return Call([]() { return 17; });\n"
20465                "    });",
20466                LLVMWithBeforeLambdaBody);
20467   verifyFormat("TwoNestedLambdas_SLS_All(\n"
20468                "    []()\n"
20469                "    {\n"
20470                "      return Call(\n"
20471                "          []()\n"
20472                "          {\n"
20473                "            // A cool function...\n"
20474                "            return 17;\n"
20475                "          });\n"
20476                "    });",
20477                LLVMWithBeforeLambdaBody);
20478 
20479   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
20480       FormatStyle::ShortLambdaStyle::SLS_None;
20481 
20482   verifyFormat("auto select = [this]() -> const Library::Object *\n"
20483                "{\n"
20484                "  return MyAssignment::SelectFromList(this);\n"
20485                "};\n",
20486                LLVMWithBeforeLambdaBody);
20487 
20488   verifyFormat("auto select = [this]() -> const Library::Object &\n"
20489                "{\n"
20490                "  return MyAssignment::SelectFromList(this);\n"
20491                "};\n",
20492                LLVMWithBeforeLambdaBody);
20493 
20494   verifyFormat("auto select = [this]() -> std::unique_ptr<Object>\n"
20495                "{\n"
20496                "  return MyAssignment::SelectFromList(this);\n"
20497                "};\n",
20498                LLVMWithBeforeLambdaBody);
20499 
20500   verifyFormat("namespace test {\n"
20501                "class Test {\n"
20502                "public:\n"
20503                "  Test() = default;\n"
20504                "};\n"
20505                "} // namespace test",
20506                LLVMWithBeforeLambdaBody);
20507 
20508   // Lambdas with different indentation styles.
20509   Style = getLLVMStyleWithColumns(100);
20510   EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n"
20511             "  return promise.then(\n"
20512             "      [this, &someVariable, someObject = "
20513             "std::mv(s)](std::vector<int> evaluated) mutable {\n"
20514             "        return someObject.startAsyncAction().then(\n"
20515             "            [this, &someVariable](AsyncActionResult result) "
20516             "mutable { result.processMore(); });\n"
20517             "      });\n"
20518             "}\n",
20519             format("SomeResult doSomething(SomeObject promise) {\n"
20520                    "  return promise.then([this, &someVariable, someObject = "
20521                    "std::mv(s)](std::vector<int> evaluated) mutable {\n"
20522                    "    return someObject.startAsyncAction().then([this, "
20523                    "&someVariable](AsyncActionResult result) mutable {\n"
20524                    "      result.processMore();\n"
20525                    "    });\n"
20526                    "  });\n"
20527                    "}\n",
20528                    Style));
20529   Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope;
20530   verifyFormat("test() {\n"
20531                "  ([]() -> {\n"
20532                "    int b = 32;\n"
20533                "    return 3;\n"
20534                "  }).foo();\n"
20535                "}",
20536                Style);
20537   verifyFormat("test() {\n"
20538                "  []() -> {\n"
20539                "    int b = 32;\n"
20540                "    return 3;\n"
20541                "  }\n"
20542                "}",
20543                Style);
20544   verifyFormat("std::sort(v.begin(), v.end(),\n"
20545                "          [](const auto &someLongArgumentName, const auto "
20546                "&someOtherLongArgumentName) {\n"
20547                "  return someLongArgumentName.someMemberVariable < "
20548                "someOtherLongArgumentName.someMemberVariable;\n"
20549                "});",
20550                Style);
20551   verifyFormat("test() {\n"
20552                "  (\n"
20553                "      []() -> {\n"
20554                "        int b = 32;\n"
20555                "        return 3;\n"
20556                "      },\n"
20557                "      foo, bar)\n"
20558                "      .foo();\n"
20559                "}",
20560                Style);
20561   verifyFormat("test() {\n"
20562                "  ([]() -> {\n"
20563                "    int b = 32;\n"
20564                "    return 3;\n"
20565                "  })\n"
20566                "      .foo()\n"
20567                "      .bar();\n"
20568                "}",
20569                Style);
20570   EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n"
20571             "  return promise.then(\n"
20572             "      [this, &someVariable, someObject = "
20573             "std::mv(s)](std::vector<int> evaluated) mutable {\n"
20574             "    return someObject.startAsyncAction().then(\n"
20575             "        [this, &someVariable](AsyncActionResult result) mutable { "
20576             "result.processMore(); });\n"
20577             "  });\n"
20578             "}\n",
20579             format("SomeResult doSomething(SomeObject promise) {\n"
20580                    "  return promise.then([this, &someVariable, someObject = "
20581                    "std::mv(s)](std::vector<int> evaluated) mutable {\n"
20582                    "    return someObject.startAsyncAction().then([this, "
20583                    "&someVariable](AsyncActionResult result) mutable {\n"
20584                    "      result.processMore();\n"
20585                    "    });\n"
20586                    "  });\n"
20587                    "}\n",
20588                    Style));
20589   EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n"
20590             "  return promise.then([this, &someVariable] {\n"
20591             "    return someObject.startAsyncAction().then(\n"
20592             "        [this, &someVariable](AsyncActionResult result) mutable { "
20593             "result.processMore(); });\n"
20594             "  });\n"
20595             "}\n",
20596             format("SomeResult doSomething(SomeObject promise) {\n"
20597                    "  return promise.then([this, &someVariable] {\n"
20598                    "    return someObject.startAsyncAction().then([this, "
20599                    "&someVariable](AsyncActionResult result) mutable {\n"
20600                    "      result.processMore();\n"
20601                    "    });\n"
20602                    "  });\n"
20603                    "}\n",
20604                    Style));
20605   Style = getGoogleStyle();
20606   Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope;
20607   EXPECT_EQ("#define A                                       \\\n"
20608             "  [] {                                          \\\n"
20609             "    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(        \\\n"
20610             "        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
20611             "      }",
20612             format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
20613                    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
20614                    Style));
20615   // TODO: The current formatting has a minor issue that's not worth fixing
20616   // right now whereby the closing brace is indented relative to the signature
20617   // instead of being aligned. This only happens with macros.
20618 }
20619 
20620 TEST_F(FormatTest, LambdaWithLineComments) {
20621   FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle();
20622   LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
20623   LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
20624   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
20625       FormatStyle::ShortLambdaStyle::SLS_All;
20626 
20627   verifyFormat("auto k = []() { return; }", LLVMWithBeforeLambdaBody);
20628   verifyFormat("auto k = []() // comment\n"
20629                "{ return; }",
20630                LLVMWithBeforeLambdaBody);
20631   verifyFormat("auto k = []() /* comment */ { return; }",
20632                LLVMWithBeforeLambdaBody);
20633   verifyFormat("auto k = []() /* comment */ /* comment */ { return; }",
20634                LLVMWithBeforeLambdaBody);
20635   verifyFormat("auto k = []() // X\n"
20636                "{ return; }",
20637                LLVMWithBeforeLambdaBody);
20638   verifyFormat(
20639       "auto k = []() // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"
20640       "{ return; }",
20641       LLVMWithBeforeLambdaBody);
20642 }
20643 
20644 TEST_F(FormatTest, EmptyLinesInLambdas) {
20645   verifyFormat("auto lambda = []() {\n"
20646                "  x(); //\n"
20647                "};",
20648                "auto lambda = []() {\n"
20649                "\n"
20650                "  x(); //\n"
20651                "\n"
20652                "};");
20653 }
20654 
20655 TEST_F(FormatTest, FormatsBlocks) {
20656   FormatStyle ShortBlocks = getLLVMStyle();
20657   ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
20658   verifyFormat("int (^Block)(int, int);", ShortBlocks);
20659   verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
20660   verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
20661   verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks);
20662   verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks);
20663   verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks);
20664 
20665   verifyFormat("foo(^{ bar(); });", ShortBlocks);
20666   verifyFormat("foo(a, ^{ bar(); });", ShortBlocks);
20667   verifyFormat("{ void (^block)(Object *x); }", ShortBlocks);
20668 
20669   verifyFormat("[operation setCompletionBlock:^{\n"
20670                "  [self onOperationDone];\n"
20671                "}];");
20672   verifyFormat("int i = {[operation setCompletionBlock:^{\n"
20673                "  [self onOperationDone];\n"
20674                "}]};");
20675   verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
20676                "  f();\n"
20677                "}];");
20678   verifyFormat("int a = [operation block:^int(int *i) {\n"
20679                "  return 1;\n"
20680                "}];");
20681   verifyFormat("[myObject doSomethingWith:arg1\n"
20682                "                      aaa:^int(int *a) {\n"
20683                "                        return 1;\n"
20684                "                      }\n"
20685                "                      bbb:f(a * bbbbbbbb)];");
20686 
20687   verifyFormat("[operation setCompletionBlock:^{\n"
20688                "  [self.delegate newDataAvailable];\n"
20689                "}];",
20690                getLLVMStyleWithColumns(60));
20691   verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
20692                "  NSString *path = [self sessionFilePath];\n"
20693                "  if (path) {\n"
20694                "    // ...\n"
20695                "  }\n"
20696                "});");
20697   verifyFormat("[[SessionService sharedService]\n"
20698                "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
20699                "      if (window) {\n"
20700                "        [self windowDidLoad:window];\n"
20701                "      } else {\n"
20702                "        [self errorLoadingWindow];\n"
20703                "      }\n"
20704                "    }];");
20705   verifyFormat("void (^largeBlock)(void) = ^{\n"
20706                "  // ...\n"
20707                "};\n",
20708                getLLVMStyleWithColumns(40));
20709   verifyFormat("[[SessionService sharedService]\n"
20710                "    loadWindowWithCompletionBlock: //\n"
20711                "        ^(SessionWindow *window) {\n"
20712                "          if (window) {\n"
20713                "            [self windowDidLoad:window];\n"
20714                "          } else {\n"
20715                "            [self errorLoadingWindow];\n"
20716                "          }\n"
20717                "        }];",
20718                getLLVMStyleWithColumns(60));
20719   verifyFormat("[myObject doSomethingWith:arg1\n"
20720                "    firstBlock:^(Foo *a) {\n"
20721                "      // ...\n"
20722                "      int i;\n"
20723                "    }\n"
20724                "    secondBlock:^(Bar *b) {\n"
20725                "      // ...\n"
20726                "      int i;\n"
20727                "    }\n"
20728                "    thirdBlock:^Foo(Bar *b) {\n"
20729                "      // ...\n"
20730                "      int i;\n"
20731                "    }];");
20732   verifyFormat("[myObject doSomethingWith:arg1\n"
20733                "               firstBlock:-1\n"
20734                "              secondBlock:^(Bar *b) {\n"
20735                "                // ...\n"
20736                "                int i;\n"
20737                "              }];");
20738 
20739   verifyFormat("f(^{\n"
20740                "  @autoreleasepool {\n"
20741                "    if (a) {\n"
20742                "      g();\n"
20743                "    }\n"
20744                "  }\n"
20745                "});");
20746   verifyFormat("Block b = ^int *(A *a, B *b) {}");
20747   verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n"
20748                "};");
20749 
20750   FormatStyle FourIndent = getLLVMStyle();
20751   FourIndent.ObjCBlockIndentWidth = 4;
20752   verifyFormat("[operation setCompletionBlock:^{\n"
20753                "    [self onOperationDone];\n"
20754                "}];",
20755                FourIndent);
20756 }
20757 
20758 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) {
20759   FormatStyle ZeroColumn = getLLVMStyle();
20760   ZeroColumn.ColumnLimit = 0;
20761 
20762   verifyFormat("[[SessionService sharedService] "
20763                "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
20764                "  if (window) {\n"
20765                "    [self windowDidLoad:window];\n"
20766                "  } else {\n"
20767                "    [self errorLoadingWindow];\n"
20768                "  }\n"
20769                "}];",
20770                ZeroColumn);
20771   EXPECT_EQ("[[SessionService sharedService]\n"
20772             "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
20773             "      if (window) {\n"
20774             "        [self windowDidLoad:window];\n"
20775             "      } else {\n"
20776             "        [self errorLoadingWindow];\n"
20777             "      }\n"
20778             "    }];",
20779             format("[[SessionService sharedService]\n"
20780                    "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
20781                    "                if (window) {\n"
20782                    "    [self windowDidLoad:window];\n"
20783                    "  } else {\n"
20784                    "    [self errorLoadingWindow];\n"
20785                    "  }\n"
20786                    "}];",
20787                    ZeroColumn));
20788   verifyFormat("[myObject doSomethingWith:arg1\n"
20789                "    firstBlock:^(Foo *a) {\n"
20790                "      // ...\n"
20791                "      int i;\n"
20792                "    }\n"
20793                "    secondBlock:^(Bar *b) {\n"
20794                "      // ...\n"
20795                "      int i;\n"
20796                "    }\n"
20797                "    thirdBlock:^Foo(Bar *b) {\n"
20798                "      // ...\n"
20799                "      int i;\n"
20800                "    }];",
20801                ZeroColumn);
20802   verifyFormat("f(^{\n"
20803                "  @autoreleasepool {\n"
20804                "    if (a) {\n"
20805                "      g();\n"
20806                "    }\n"
20807                "  }\n"
20808                "});",
20809                ZeroColumn);
20810   verifyFormat("void (^largeBlock)(void) = ^{\n"
20811                "  // ...\n"
20812                "};",
20813                ZeroColumn);
20814 
20815   ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
20816   EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };",
20817             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
20818   ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
20819   EXPECT_EQ("void (^largeBlock)(void) = ^{\n"
20820             "  int i;\n"
20821             "};",
20822             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
20823 }
20824 
20825 TEST_F(FormatTest, SupportsCRLF) {
20826   EXPECT_EQ("int a;\r\n"
20827             "int b;\r\n"
20828             "int c;\r\n",
20829             format("int a;\r\n"
20830                    "  int b;\r\n"
20831                    "    int c;\r\n",
20832                    getLLVMStyle()));
20833   EXPECT_EQ("int a;\r\n"
20834             "int b;\r\n"
20835             "int c;\r\n",
20836             format("int a;\r\n"
20837                    "  int b;\n"
20838                    "    int c;\r\n",
20839                    getLLVMStyle()));
20840   EXPECT_EQ("int a;\n"
20841             "int b;\n"
20842             "int c;\n",
20843             format("int a;\r\n"
20844                    "  int b;\n"
20845                    "    int c;\n",
20846                    getLLVMStyle()));
20847   EXPECT_EQ("\"aaaaaaa \"\r\n"
20848             "\"bbbbbbb\";\r\n",
20849             format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
20850   EXPECT_EQ("#define A \\\r\n"
20851             "  b;      \\\r\n"
20852             "  c;      \\\r\n"
20853             "  d;\r\n",
20854             format("#define A \\\r\n"
20855                    "  b; \\\r\n"
20856                    "  c; d; \r\n",
20857                    getGoogleStyle()));
20858 
20859   EXPECT_EQ("/*\r\n"
20860             "multi line block comments\r\n"
20861             "should not introduce\r\n"
20862             "an extra carriage return\r\n"
20863             "*/\r\n",
20864             format("/*\r\n"
20865                    "multi line block comments\r\n"
20866                    "should not introduce\r\n"
20867                    "an extra carriage return\r\n"
20868                    "*/\r\n"));
20869   EXPECT_EQ("/*\r\n"
20870             "\r\n"
20871             "*/",
20872             format("/*\r\n"
20873                    "    \r\r\r\n"
20874                    "*/"));
20875 
20876   FormatStyle style = getLLVMStyle();
20877 
20878   style.DeriveLineEnding = true;
20879   style.UseCRLF = false;
20880   EXPECT_EQ("union FooBarBazQux {\n"
20881             "  int foo;\n"
20882             "  int bar;\n"
20883             "  int baz;\n"
20884             "};",
20885             format("union FooBarBazQux {\r\n"
20886                    "  int foo;\n"
20887                    "  int bar;\r\n"
20888                    "  int baz;\n"
20889                    "};",
20890                    style));
20891   style.UseCRLF = true;
20892   EXPECT_EQ("union FooBarBazQux {\r\n"
20893             "  int foo;\r\n"
20894             "  int bar;\r\n"
20895             "  int baz;\r\n"
20896             "};",
20897             format("union FooBarBazQux {\r\n"
20898                    "  int foo;\n"
20899                    "  int bar;\r\n"
20900                    "  int baz;\n"
20901                    "};",
20902                    style));
20903 
20904   style.DeriveLineEnding = false;
20905   style.UseCRLF = false;
20906   EXPECT_EQ("union FooBarBazQux {\n"
20907             "  int foo;\n"
20908             "  int bar;\n"
20909             "  int baz;\n"
20910             "  int qux;\n"
20911             "};",
20912             format("union FooBarBazQux {\r\n"
20913                    "  int foo;\n"
20914                    "  int bar;\r\n"
20915                    "  int baz;\n"
20916                    "  int qux;\r\n"
20917                    "};",
20918                    style));
20919   style.UseCRLF = true;
20920   EXPECT_EQ("union FooBarBazQux {\r\n"
20921             "  int foo;\r\n"
20922             "  int bar;\r\n"
20923             "  int baz;\r\n"
20924             "  int qux;\r\n"
20925             "};",
20926             format("union FooBarBazQux {\r\n"
20927                    "  int foo;\n"
20928                    "  int bar;\r\n"
20929                    "  int baz;\n"
20930                    "  int qux;\n"
20931                    "};",
20932                    style));
20933 
20934   style.DeriveLineEnding = true;
20935   style.UseCRLF = false;
20936   EXPECT_EQ("union FooBarBazQux {\r\n"
20937             "  int foo;\r\n"
20938             "  int bar;\r\n"
20939             "  int baz;\r\n"
20940             "  int qux;\r\n"
20941             "};",
20942             format("union FooBarBazQux {\r\n"
20943                    "  int foo;\n"
20944                    "  int bar;\r\n"
20945                    "  int baz;\n"
20946                    "  int qux;\r\n"
20947                    "};",
20948                    style));
20949   style.UseCRLF = true;
20950   EXPECT_EQ("union FooBarBazQux {\n"
20951             "  int foo;\n"
20952             "  int bar;\n"
20953             "  int baz;\n"
20954             "  int qux;\n"
20955             "};",
20956             format("union FooBarBazQux {\r\n"
20957                    "  int foo;\n"
20958                    "  int bar;\r\n"
20959                    "  int baz;\n"
20960                    "  int qux;\n"
20961                    "};",
20962                    style));
20963 }
20964 
20965 TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
20966   verifyFormat("MY_CLASS(C) {\n"
20967                "  int i;\n"
20968                "  int j;\n"
20969                "};");
20970 }
20971 
20972 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
20973   FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
20974   TwoIndent.ContinuationIndentWidth = 2;
20975 
20976   EXPECT_EQ("int i =\n"
20977             "  longFunction(\n"
20978             "    arg);",
20979             format("int i = longFunction(arg);", TwoIndent));
20980 
20981   FormatStyle SixIndent = getLLVMStyleWithColumns(20);
20982   SixIndent.ContinuationIndentWidth = 6;
20983 
20984   EXPECT_EQ("int i =\n"
20985             "      longFunction(\n"
20986             "            arg);",
20987             format("int i = longFunction(arg);", SixIndent));
20988 }
20989 
20990 TEST_F(FormatTest, WrappedClosingParenthesisIndent) {
20991   FormatStyle Style = getLLVMStyle();
20992   verifyFormat("int Foo::getter(\n"
20993                "    //\n"
20994                ") const {\n"
20995                "  return foo;\n"
20996                "}",
20997                Style);
20998   verifyFormat("void Foo::setter(\n"
20999                "    //\n"
21000                ") {\n"
21001                "  foo = 1;\n"
21002                "}",
21003                Style);
21004 }
21005 
21006 TEST_F(FormatTest, SpacesInAngles) {
21007   FormatStyle Spaces = getLLVMStyle();
21008   Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
21009 
21010   verifyFormat("vector< ::std::string > x1;", Spaces);
21011   verifyFormat("Foo< int, Bar > x2;", Spaces);
21012   verifyFormat("Foo< ::int, ::Bar > x3;", Spaces);
21013 
21014   verifyFormat("static_cast< int >(arg);", Spaces);
21015   verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
21016   verifyFormat("f< int, float >();", Spaces);
21017   verifyFormat("template <> g() {}", Spaces);
21018   verifyFormat("template < std::vector< int > > f() {}", Spaces);
21019   verifyFormat("std::function< void(int, int) > fct;", Spaces);
21020   verifyFormat("void inFunction() { std::function< void(int, int) > fct; }",
21021                Spaces);
21022 
21023   Spaces.Standard = FormatStyle::LS_Cpp03;
21024   Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
21025   verifyFormat("A< A< int > >();", Spaces);
21026 
21027   Spaces.SpacesInAngles = FormatStyle::SIAS_Never;
21028   verifyFormat("A<A<int> >();", Spaces);
21029 
21030   Spaces.SpacesInAngles = FormatStyle::SIAS_Leave;
21031   verifyFormat("vector< ::std::string> x4;", "vector<::std::string> x4;",
21032                Spaces);
21033   verifyFormat("vector< ::std::string > x4;", "vector<::std::string > x4;",
21034                Spaces);
21035 
21036   verifyFormat("A<A<int> >();", Spaces);
21037   verifyFormat("A<A<int> >();", "A<A<int>>();", Spaces);
21038   verifyFormat("A< A< int > >();", Spaces);
21039 
21040   Spaces.Standard = FormatStyle::LS_Cpp11;
21041   Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
21042   verifyFormat("A< A< int > >();", Spaces);
21043 
21044   Spaces.SpacesInAngles = FormatStyle::SIAS_Never;
21045   verifyFormat("vector<::std::string> x4;", Spaces);
21046   verifyFormat("vector<int> x5;", Spaces);
21047   verifyFormat("Foo<int, Bar> x6;", Spaces);
21048   verifyFormat("Foo<::int, ::Bar> x7;", Spaces);
21049 
21050   verifyFormat("A<A<int>>();", Spaces);
21051 
21052   Spaces.SpacesInAngles = FormatStyle::SIAS_Leave;
21053   verifyFormat("vector<::std::string> x4;", Spaces);
21054   verifyFormat("vector< ::std::string > x4;", Spaces);
21055   verifyFormat("vector<int> x5;", Spaces);
21056   verifyFormat("vector< int > x5;", Spaces);
21057   verifyFormat("Foo<int, Bar> x6;", Spaces);
21058   verifyFormat("Foo< int, Bar > x6;", Spaces);
21059   verifyFormat("Foo<::int, ::Bar> x7;", Spaces);
21060   verifyFormat("Foo< ::int, ::Bar > x7;", Spaces);
21061 
21062   verifyFormat("A<A<int>>();", Spaces);
21063   verifyFormat("A< A< int > >();", Spaces);
21064   verifyFormat("A<A<int > >();", Spaces);
21065   verifyFormat("A< A< int>>();", Spaces);
21066 }
21067 
21068 TEST_F(FormatTest, SpaceAfterTemplateKeyword) {
21069   FormatStyle Style = getLLVMStyle();
21070   Style.SpaceAfterTemplateKeyword = false;
21071   verifyFormat("template<int> void foo();", Style);
21072 }
21073 
21074 TEST_F(FormatTest, TripleAngleBrackets) {
21075   verifyFormat("f<<<1, 1>>>();");
21076   verifyFormat("f<<<1, 1, 1, s>>>();");
21077   verifyFormat("f<<<a, b, c, d>>>();");
21078   EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();"));
21079   verifyFormat("f<param><<<1, 1>>>();");
21080   verifyFormat("f<1><<<1, 1>>>();");
21081   EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();"));
21082   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
21083                "aaaaaaaaaaa<<<\n    1, 1>>>();");
21084   verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n"
21085                "    <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();");
21086 }
21087 
21088 TEST_F(FormatTest, MergeLessLessAtEnd) {
21089   verifyFormat("<<");
21090   EXPECT_EQ("< < <", format("\\\n<<<"));
21091   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
21092                "aaallvm::outs() <<");
21093   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
21094                "aaaallvm::outs()\n    <<");
21095 }
21096 
21097 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
21098   std::string code = "#if A\n"
21099                      "#if B\n"
21100                      "a.\n"
21101                      "#endif\n"
21102                      "    a = 1;\n"
21103                      "#else\n"
21104                      "#endif\n"
21105                      "#if C\n"
21106                      "#else\n"
21107                      "#endif\n";
21108   EXPECT_EQ(code, format(code));
21109 }
21110 
21111 TEST_F(FormatTest, HandleConflictMarkers) {
21112   // Git/SVN conflict markers.
21113   EXPECT_EQ("int a;\n"
21114             "void f() {\n"
21115             "  callme(some(parameter1,\n"
21116             "<<<<<<< text by the vcs\n"
21117             "              parameter2),\n"
21118             "||||||| text by the vcs\n"
21119             "              parameter2),\n"
21120             "         parameter3,\n"
21121             "======= text by the vcs\n"
21122             "              parameter2, parameter3),\n"
21123             ">>>>>>> text by the vcs\n"
21124             "         otherparameter);\n",
21125             format("int a;\n"
21126                    "void f() {\n"
21127                    "  callme(some(parameter1,\n"
21128                    "<<<<<<< text by the vcs\n"
21129                    "  parameter2),\n"
21130                    "||||||| text by the vcs\n"
21131                    "  parameter2),\n"
21132                    "  parameter3,\n"
21133                    "======= text by the vcs\n"
21134                    "  parameter2,\n"
21135                    "  parameter3),\n"
21136                    ">>>>>>> text by the vcs\n"
21137                    "  otherparameter);\n"));
21138 
21139   // Perforce markers.
21140   EXPECT_EQ("void f() {\n"
21141             "  function(\n"
21142             ">>>> text by the vcs\n"
21143             "      parameter,\n"
21144             "==== text by the vcs\n"
21145             "      parameter,\n"
21146             "==== text by the vcs\n"
21147             "      parameter,\n"
21148             "<<<< text by the vcs\n"
21149             "      parameter);\n",
21150             format("void f() {\n"
21151                    "  function(\n"
21152                    ">>>> text by the vcs\n"
21153                    "  parameter,\n"
21154                    "==== text by the vcs\n"
21155                    "  parameter,\n"
21156                    "==== text by the vcs\n"
21157                    "  parameter,\n"
21158                    "<<<< text by the vcs\n"
21159                    "  parameter);\n"));
21160 
21161   EXPECT_EQ("<<<<<<<\n"
21162             "|||||||\n"
21163             "=======\n"
21164             ">>>>>>>",
21165             format("<<<<<<<\n"
21166                    "|||||||\n"
21167                    "=======\n"
21168                    ">>>>>>>"));
21169 
21170   EXPECT_EQ("<<<<<<<\n"
21171             "|||||||\n"
21172             "int i;\n"
21173             "=======\n"
21174             ">>>>>>>",
21175             format("<<<<<<<\n"
21176                    "|||||||\n"
21177                    "int i;\n"
21178                    "=======\n"
21179                    ">>>>>>>"));
21180 
21181   // FIXME: Handle parsing of macros around conflict markers correctly:
21182   EXPECT_EQ("#define Macro \\\n"
21183             "<<<<<<<\n"
21184             "Something \\\n"
21185             "|||||||\n"
21186             "Else \\\n"
21187             "=======\n"
21188             "Other \\\n"
21189             ">>>>>>>\n"
21190             "    End int i;\n",
21191             format("#define Macro \\\n"
21192                    "<<<<<<<\n"
21193                    "  Something \\\n"
21194                    "|||||||\n"
21195                    "  Else \\\n"
21196                    "=======\n"
21197                    "  Other \\\n"
21198                    ">>>>>>>\n"
21199                    "  End\n"
21200                    "int i;\n"));
21201 
21202   verifyFormat(R"(====
21203 #ifdef A
21204 a
21205 #else
21206 b
21207 #endif
21208 )");
21209 }
21210 
21211 TEST_F(FormatTest, DisableRegions) {
21212   EXPECT_EQ("int i;\n"
21213             "// clang-format off\n"
21214             "  int j;\n"
21215             "// clang-format on\n"
21216             "int k;",
21217             format(" int  i;\n"
21218                    "   // clang-format off\n"
21219                    "  int j;\n"
21220                    " // clang-format on\n"
21221                    "   int   k;"));
21222   EXPECT_EQ("int i;\n"
21223             "/* clang-format off */\n"
21224             "  int j;\n"
21225             "/* clang-format on */\n"
21226             "int k;",
21227             format(" int  i;\n"
21228                    "   /* clang-format off */\n"
21229                    "  int j;\n"
21230                    " /* clang-format on */\n"
21231                    "   int   k;"));
21232 
21233   // Don't reflow comments within disabled regions.
21234   EXPECT_EQ("// clang-format off\n"
21235             "// long long long long long long line\n"
21236             "/* clang-format on */\n"
21237             "/* long long long\n"
21238             " * long long long\n"
21239             " * line */\n"
21240             "int i;\n"
21241             "/* clang-format off */\n"
21242             "/* long long long long long long line */\n",
21243             format("// clang-format off\n"
21244                    "// long long long long long long line\n"
21245                    "/* clang-format on */\n"
21246                    "/* long long long long long long line */\n"
21247                    "int i;\n"
21248                    "/* clang-format off */\n"
21249                    "/* long long long long long long line */\n",
21250                    getLLVMStyleWithColumns(20)));
21251 }
21252 
21253 TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
21254   format("? ) =");
21255   verifyNoCrash("#define a\\\n /**/}");
21256 }
21257 
21258 TEST_F(FormatTest, FormatsTableGenCode) {
21259   FormatStyle Style = getLLVMStyle();
21260   Style.Language = FormatStyle::LK_TableGen;
21261   verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style);
21262 }
21263 
21264 TEST_F(FormatTest, ArrayOfTemplates) {
21265   EXPECT_EQ("auto a = new unique_ptr<int>[10];",
21266             format("auto a = new unique_ptr<int > [ 10];"));
21267 
21268   FormatStyle Spaces = getLLVMStyle();
21269   Spaces.SpacesInSquareBrackets = true;
21270   EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];",
21271             format("auto a = new unique_ptr<int > [10];", Spaces));
21272 }
21273 
21274 TEST_F(FormatTest, ArrayAsTemplateType) {
21275   EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;",
21276             format("auto a = unique_ptr < Foo < Bar>[ 10]> ;"));
21277 
21278   FormatStyle Spaces = getLLVMStyle();
21279   Spaces.SpacesInSquareBrackets = true;
21280   EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;",
21281             format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces));
21282 }
21283 
21284 TEST_F(FormatTest, NoSpaceAfterSuper) { verifyFormat("__super::FooBar();"); }
21285 
21286 TEST(FormatStyle, GetStyleWithEmptyFileName) {
21287   llvm::vfs::InMemoryFileSystem FS;
21288   auto Style1 = getStyle("file", "", "Google", "", &FS);
21289   ASSERT_TRUE((bool)Style1);
21290   ASSERT_EQ(*Style1, getGoogleStyle());
21291 }
21292 
21293 TEST(FormatStyle, GetStyleOfFile) {
21294   llvm::vfs::InMemoryFileSystem FS;
21295   // Test 1: format file in the same directory.
21296   ASSERT_TRUE(
21297       FS.addFile("/a/.clang-format", 0,
21298                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM")));
21299   ASSERT_TRUE(
21300       FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
21301   auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS);
21302   ASSERT_TRUE((bool)Style1);
21303   ASSERT_EQ(*Style1, getLLVMStyle());
21304 
21305   // Test 2.1: fallback to default.
21306   ASSERT_TRUE(
21307       FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
21308   auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS);
21309   ASSERT_TRUE((bool)Style2);
21310   ASSERT_EQ(*Style2, getMozillaStyle());
21311 
21312   // Test 2.2: no format on 'none' fallback style.
21313   Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
21314   ASSERT_TRUE((bool)Style2);
21315   ASSERT_EQ(*Style2, getNoStyle());
21316 
21317   // Test 2.3: format if config is found with no based style while fallback is
21318   // 'none'.
21319   ASSERT_TRUE(FS.addFile("/b/.clang-format", 0,
21320                          llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2")));
21321   Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
21322   ASSERT_TRUE((bool)Style2);
21323   ASSERT_EQ(*Style2, getLLVMStyle());
21324 
21325   // Test 2.4: format if yaml with no based style, while fallback is 'none'.
21326   Style2 = getStyle("{}", "a.h", "none", "", &FS);
21327   ASSERT_TRUE((bool)Style2);
21328   ASSERT_EQ(*Style2, getLLVMStyle());
21329 
21330   // Test 3: format file in parent directory.
21331   ASSERT_TRUE(
21332       FS.addFile("/c/.clang-format", 0,
21333                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google")));
21334   ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0,
21335                          llvm::MemoryBuffer::getMemBuffer("int i;")));
21336   auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS);
21337   ASSERT_TRUE((bool)Style3);
21338   ASSERT_EQ(*Style3, getGoogleStyle());
21339 
21340   // Test 4: error on invalid fallback style
21341   auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS);
21342   ASSERT_FALSE((bool)Style4);
21343   llvm::consumeError(Style4.takeError());
21344 
21345   // Test 5: error on invalid yaml on command line
21346   auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS);
21347   ASSERT_FALSE((bool)Style5);
21348   llvm::consumeError(Style5.takeError());
21349 
21350   // Test 6: error on invalid style
21351   auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS);
21352   ASSERT_FALSE((bool)Style6);
21353   llvm::consumeError(Style6.takeError());
21354 
21355   // Test 7: found config file, error on parsing it
21356   ASSERT_TRUE(
21357       FS.addFile("/d/.clang-format", 0,
21358                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n"
21359                                                   "InvalidKey: InvalidValue")));
21360   ASSERT_TRUE(
21361       FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
21362   auto Style7a = getStyle("file", "/d/.clang-format", "LLVM", "", &FS);
21363   ASSERT_FALSE((bool)Style7a);
21364   llvm::consumeError(Style7a.takeError());
21365 
21366   auto Style7b = getStyle("file", "/d/.clang-format", "LLVM", "", &FS, true);
21367   ASSERT_TRUE((bool)Style7b);
21368 
21369   // Test 8: inferred per-language defaults apply.
21370   auto StyleTd = getStyle("file", "x.td", "llvm", "", &FS);
21371   ASSERT_TRUE((bool)StyleTd);
21372   ASSERT_EQ(*StyleTd, getLLVMStyle(FormatStyle::LK_TableGen));
21373 
21374   // Test 9.1: overwriting a file style, when parent no file exists with no
21375   // fallback style
21376   ASSERT_TRUE(FS.addFile(
21377       "/e/sub/.clang-format", 0,
21378       llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: InheritParentConfig\n"
21379                                        "ColumnLimit: 20")));
21380   ASSERT_TRUE(FS.addFile("/e/sub/code.cpp", 0,
21381                          llvm::MemoryBuffer::getMemBuffer("int i;")));
21382   auto Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS);
21383   ASSERT_TRUE(static_cast<bool>(Style9));
21384   ASSERT_EQ(*Style9, [] {
21385     auto Style = getNoStyle();
21386     Style.ColumnLimit = 20;
21387     return Style;
21388   }());
21389 
21390   // Test 9.2: with LLVM fallback style
21391   Style9 = getStyle("file", "/e/sub/code.cpp", "LLVM", "", &FS);
21392   ASSERT_TRUE(static_cast<bool>(Style9));
21393   ASSERT_EQ(*Style9, [] {
21394     auto Style = getLLVMStyle();
21395     Style.ColumnLimit = 20;
21396     return Style;
21397   }());
21398 
21399   // Test 9.3: with a parent file
21400   ASSERT_TRUE(
21401       FS.addFile("/e/.clang-format", 0,
21402                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google\n"
21403                                                   "UseTab: Always")));
21404   Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS);
21405   ASSERT_TRUE(static_cast<bool>(Style9));
21406   ASSERT_EQ(*Style9, [] {
21407     auto Style = getGoogleStyle();
21408     Style.ColumnLimit = 20;
21409     Style.UseTab = FormatStyle::UT_Always;
21410     return Style;
21411   }());
21412 
21413   // Test 9.4: propagate more than one level
21414   ASSERT_TRUE(FS.addFile("/e/sub/sub/code.cpp", 0,
21415                          llvm::MemoryBuffer::getMemBuffer("int i;")));
21416   ASSERT_TRUE(FS.addFile("/e/sub/sub/.clang-format", 0,
21417                          llvm::MemoryBuffer::getMemBuffer(
21418                              "BasedOnStyle: InheritParentConfig\n"
21419                              "WhitespaceSensitiveMacros: ['FOO', 'BAR']")));
21420   std::vector<std::string> NonDefaultWhiteSpaceMacros{"FOO", "BAR"};
21421 
21422   const auto SubSubStyle = [&NonDefaultWhiteSpaceMacros] {
21423     auto Style = getGoogleStyle();
21424     Style.ColumnLimit = 20;
21425     Style.UseTab = FormatStyle::UT_Always;
21426     Style.WhitespaceSensitiveMacros = NonDefaultWhiteSpaceMacros;
21427     return Style;
21428   }();
21429 
21430   ASSERT_NE(Style9->WhitespaceSensitiveMacros, NonDefaultWhiteSpaceMacros);
21431   Style9 = getStyle("file", "/e/sub/sub/code.cpp", "none", "", &FS);
21432   ASSERT_TRUE(static_cast<bool>(Style9));
21433   ASSERT_EQ(*Style9, SubSubStyle);
21434 
21435   // Test 9.5: use InheritParentConfig as style name
21436   Style9 =
21437       getStyle("inheritparentconfig", "/e/sub/sub/code.cpp", "none", "", &FS);
21438   ASSERT_TRUE(static_cast<bool>(Style9));
21439   ASSERT_EQ(*Style9, SubSubStyle);
21440 
21441   // Test 9.6: use command line style with inheritance
21442   Style9 = getStyle("{BasedOnStyle: InheritParentConfig}", "/e/sub/code.cpp",
21443                     "none", "", &FS);
21444   ASSERT_TRUE(static_cast<bool>(Style9));
21445   ASSERT_EQ(*Style9, SubSubStyle);
21446 
21447   // Test 9.7: use command line style with inheritance and own config
21448   Style9 = getStyle("{BasedOnStyle: InheritParentConfig, "
21449                     "WhitespaceSensitiveMacros: ['FOO', 'BAR']}",
21450                     "/e/sub/code.cpp", "none", "", &FS);
21451   ASSERT_TRUE(static_cast<bool>(Style9));
21452   ASSERT_EQ(*Style9, SubSubStyle);
21453 
21454   // Test 9.8: use inheritance from a file without BasedOnStyle
21455   ASSERT_TRUE(FS.addFile("/e/withoutbase/.clang-format", 0,
21456                          llvm::MemoryBuffer::getMemBuffer("ColumnLimit: 123")));
21457   ASSERT_TRUE(
21458       FS.addFile("/e/withoutbase/sub/.clang-format", 0,
21459                  llvm::MemoryBuffer::getMemBuffer(
21460                      "BasedOnStyle: InheritParentConfig\nIndentWidth: 7")));
21461   // Make sure we do not use the fallback style
21462   Style9 = getStyle("file", "/e/withoutbase/code.cpp", "google", "", &FS);
21463   ASSERT_TRUE(static_cast<bool>(Style9));
21464   ASSERT_EQ(*Style9, [] {
21465     auto Style = getLLVMStyle();
21466     Style.ColumnLimit = 123;
21467     return Style;
21468   }());
21469 
21470   Style9 = getStyle("file", "/e/withoutbase/sub/code.cpp", "google", "", &FS);
21471   ASSERT_TRUE(static_cast<bool>(Style9));
21472   ASSERT_EQ(*Style9, [] {
21473     auto Style = getLLVMStyle();
21474     Style.ColumnLimit = 123;
21475     Style.IndentWidth = 7;
21476     return Style;
21477   }());
21478 }
21479 
21480 TEST_F(ReplacementTest, FormatCodeAfterReplacements) {
21481   // Column limit is 20.
21482   std::string Code = "Type *a =\n"
21483                      "    new Type();\n"
21484                      "g(iiiii, 0, jjjjj,\n"
21485                      "  0, kkkkk, 0, mm);\n"
21486                      "int  bad     = format   ;";
21487   std::string Expected = "auto a = new Type();\n"
21488                          "g(iiiii, nullptr,\n"
21489                          "  jjjjj, nullptr,\n"
21490                          "  kkkkk, nullptr,\n"
21491                          "  mm);\n"
21492                          "int  bad     = format   ;";
21493   FileID ID = Context.createInMemoryFile("format.cpp", Code);
21494   tooling::Replacements Replaces = toReplacements(
21495       {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6,
21496                             "auto "),
21497        tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1,
21498                             "nullptr"),
21499        tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1,
21500                             "nullptr"),
21501        tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1,
21502                             "nullptr")});
21503 
21504   format::FormatStyle Style = format::getLLVMStyle();
21505   Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility.
21506   auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
21507   EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
21508       << llvm::toString(FormattedReplaces.takeError()) << "\n";
21509   auto Result = applyAllReplacements(Code, *FormattedReplaces);
21510   EXPECT_TRUE(static_cast<bool>(Result));
21511   EXPECT_EQ(Expected, *Result);
21512 }
21513 
21514 TEST_F(ReplacementTest, SortIncludesAfterReplacement) {
21515   std::string Code = "#include \"a.h\"\n"
21516                      "#include \"c.h\"\n"
21517                      "\n"
21518                      "int main() {\n"
21519                      "  return 0;\n"
21520                      "}";
21521   std::string Expected = "#include \"a.h\"\n"
21522                          "#include \"b.h\"\n"
21523                          "#include \"c.h\"\n"
21524                          "\n"
21525                          "int main() {\n"
21526                          "  return 0;\n"
21527                          "}";
21528   FileID ID = Context.createInMemoryFile("fix.cpp", Code);
21529   tooling::Replacements Replaces = toReplacements(
21530       {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0,
21531                             "#include \"b.h\"\n")});
21532 
21533   format::FormatStyle Style = format::getLLVMStyle();
21534   Style.SortIncludes = FormatStyle::SI_CaseSensitive;
21535   auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
21536   EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
21537       << llvm::toString(FormattedReplaces.takeError()) << "\n";
21538   auto Result = applyAllReplacements(Code, *FormattedReplaces);
21539   EXPECT_TRUE(static_cast<bool>(Result));
21540   EXPECT_EQ(Expected, *Result);
21541 }
21542 
21543 TEST_F(FormatTest, FormatSortsUsingDeclarations) {
21544   EXPECT_EQ("using std::cin;\n"
21545             "using std::cout;",
21546             format("using std::cout;\n"
21547                    "using std::cin;",
21548                    getGoogleStyle()));
21549 }
21550 
21551 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) {
21552   format::FormatStyle Style = format::getLLVMStyle();
21553   Style.Standard = FormatStyle::LS_Cpp03;
21554   // cpp03 recognize this string as identifier u8 and literal character 'a'
21555   EXPECT_EQ("auto c = u8 'a';", format("auto c = u8'a';", Style));
21556 }
21557 
21558 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) {
21559   // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers
21560   // all modes, including C++11, C++14 and C++17
21561   EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';"));
21562 }
21563 
21564 TEST_F(FormatTest, DoNotFormatLikelyXml) {
21565   EXPECT_EQ("<!-- ;> -->", format("<!-- ;> -->", getGoogleStyle()));
21566   EXPECT_EQ(" <!-- >; -->", format(" <!-- >; -->", getGoogleStyle()));
21567 }
21568 
21569 TEST_F(FormatTest, StructuredBindings) {
21570   // Structured bindings is a C++17 feature.
21571   // all modes, including C++11, C++14 and C++17
21572   verifyFormat("auto [a, b] = f();");
21573   EXPECT_EQ("auto [a, b] = f();", format("auto[a, b] = f();"));
21574   EXPECT_EQ("const auto [a, b] = f();", format("const   auto[a, b] = f();"));
21575   EXPECT_EQ("auto const [a, b] = f();", format("auto  const[a, b] = f();"));
21576   EXPECT_EQ("auto const volatile [a, b] = f();",
21577             format("auto  const   volatile[a, b] = f();"));
21578   EXPECT_EQ("auto [a, b, c] = f();", format("auto   [  a  ,  b,c   ] = f();"));
21579   EXPECT_EQ("auto &[a, b, c] = f();",
21580             format("auto   &[  a  ,  b,c   ] = f();"));
21581   EXPECT_EQ("auto &&[a, b, c] = f();",
21582             format("auto   &&[  a  ,  b,c   ] = f();"));
21583   EXPECT_EQ("auto const &[a, b] = f();", format("auto  const&[a, b] = f();"));
21584   EXPECT_EQ("auto const volatile &&[a, b] = f();",
21585             format("auto  const  volatile  &&[a, b] = f();"));
21586   EXPECT_EQ("auto const &&[a, b] = f();",
21587             format("auto  const   &&  [a, b] = f();"));
21588   EXPECT_EQ("const auto &[a, b] = f();",
21589             format("const  auto  &  [a, b] = f();"));
21590   EXPECT_EQ("const auto volatile &&[a, b] = f();",
21591             format("const  auto   volatile  &&[a, b] = f();"));
21592   EXPECT_EQ("volatile const auto &&[a, b] = f();",
21593             format("volatile  const  auto   &&[a, b] = f();"));
21594   EXPECT_EQ("const auto &&[a, b] = f();",
21595             format("const  auto  &&  [a, b] = f();"));
21596 
21597   // Make sure we don't mistake structured bindings for lambdas.
21598   FormatStyle PointerMiddle = getLLVMStyle();
21599   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
21600   verifyFormat("auto [a1, b]{A * i};", getGoogleStyle());
21601   verifyFormat("auto [a2, b]{A * i};", getLLVMStyle());
21602   verifyFormat("auto [a3, b]{A * i};", PointerMiddle);
21603   verifyFormat("auto const [a1, b]{A * i};", getGoogleStyle());
21604   verifyFormat("auto const [a2, b]{A * i};", getLLVMStyle());
21605   verifyFormat("auto const [a3, b]{A * i};", PointerMiddle);
21606   verifyFormat("auto const& [a1, b]{A * i};", getGoogleStyle());
21607   verifyFormat("auto const &[a2, b]{A * i};", getLLVMStyle());
21608   verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle);
21609   verifyFormat("auto const&& [a1, b]{A * i};", getGoogleStyle());
21610   verifyFormat("auto const &&[a2, b]{A * i};", getLLVMStyle());
21611   verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle);
21612 
21613   EXPECT_EQ("for (const auto &&[a, b] : some_range) {\n}",
21614             format("for (const auto   &&   [a, b] : some_range) {\n}"));
21615   EXPECT_EQ("for (const auto &[a, b] : some_range) {\n}",
21616             format("for (const auto   &   [a, b] : some_range) {\n}"));
21617   EXPECT_EQ("for (const auto [a, b] : some_range) {\n}",
21618             format("for (const auto[a, b] : some_range) {\n}"));
21619   EXPECT_EQ("auto [x, y](expr);", format("auto[x,y]  (expr);"));
21620   EXPECT_EQ("auto &[x, y](expr);", format("auto  &  [x,y]  (expr);"));
21621   EXPECT_EQ("auto &&[x, y](expr);", format("auto  &&  [x,y]  (expr);"));
21622   EXPECT_EQ("auto const &[x, y](expr);",
21623             format("auto  const  &  [x,y]  (expr);"));
21624   EXPECT_EQ("auto const &&[x, y](expr);",
21625             format("auto  const  &&  [x,y]  (expr);"));
21626   EXPECT_EQ("auto [x, y]{expr};", format("auto[x,y]     {expr};"));
21627   EXPECT_EQ("auto const &[x, y]{expr};",
21628             format("auto  const  &  [x,y]  {expr};"));
21629   EXPECT_EQ("auto const &&[x, y]{expr};",
21630             format("auto  const  &&  [x,y]  {expr};"));
21631 
21632   format::FormatStyle Spaces = format::getLLVMStyle();
21633   Spaces.SpacesInSquareBrackets = true;
21634   verifyFormat("auto [ a, b ] = f();", Spaces);
21635   verifyFormat("auto &&[ a, b ] = f();", Spaces);
21636   verifyFormat("auto &[ a, b ] = f();", Spaces);
21637   verifyFormat("auto const &&[ a, b ] = f();", Spaces);
21638   verifyFormat("auto const &[ a, b ] = f();", Spaces);
21639 }
21640 
21641 TEST_F(FormatTest, FileAndCode) {
21642   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.cc", ""));
21643   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.m", ""));
21644   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.mm", ""));
21645   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", ""));
21646   EXPECT_EQ(FormatStyle::LK_ObjC,
21647             guessLanguage("foo.h", "@interface Foo\n@end\n"));
21648   EXPECT_EQ(
21649       FormatStyle::LK_ObjC,
21650       guessLanguage("foo.h", "#define TRY(x, y) @try { x; } @finally { y; }"));
21651   EXPECT_EQ(FormatStyle::LK_ObjC,
21652             guessLanguage("foo.h", "#define AVAIL(x) @available(x, *))"));
21653   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@class Foo;"));
21654   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo", ""));
21655   EXPECT_EQ(FormatStyle::LK_ObjC,
21656             guessLanguage("foo", "@interface Foo\n@end\n"));
21657   EXPECT_EQ(FormatStyle::LK_ObjC,
21658             guessLanguage("foo.h", "int DoStuff(CGRect rect);\n"));
21659   EXPECT_EQ(
21660       FormatStyle::LK_ObjC,
21661       guessLanguage("foo.h",
21662                     "#define MY_POINT_MAKE(x, y) CGPointMake((x), (y));\n"));
21663   EXPECT_EQ(
21664       FormatStyle::LK_Cpp,
21665       guessLanguage("foo.h", "#define FOO(...) auto bar = [] __VA_ARGS__;"));
21666 }
21667 
21668 TEST_F(FormatTest, GuessLanguageWithCpp11AttributeSpecifiers) {
21669   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[noreturn]];"));
21670   EXPECT_EQ(FormatStyle::LK_ObjC,
21671             guessLanguage("foo.h", "array[[calculator getIndex]];"));
21672   EXPECT_EQ(FormatStyle::LK_Cpp,
21673             guessLanguage("foo.h", "[[noreturn, deprecated(\"so sorry\")]];"));
21674   EXPECT_EQ(
21675       FormatStyle::LK_Cpp,
21676       guessLanguage("foo.h", "[[noreturn, deprecated(\"gone, sorry\")]];"));
21677   EXPECT_EQ(FormatStyle::LK_ObjC,
21678             guessLanguage("foo.h", "[[noreturn foo] bar];"));
21679   EXPECT_EQ(FormatStyle::LK_Cpp,
21680             guessLanguage("foo.h", "[[clang::fallthrough]];"));
21681   EXPECT_EQ(FormatStyle::LK_ObjC,
21682             guessLanguage("foo.h", "[[clang:fallthrough] foo];"));
21683   EXPECT_EQ(FormatStyle::LK_Cpp,
21684             guessLanguage("foo.h", "[[gsl::suppress(\"type\")]];"));
21685   EXPECT_EQ(FormatStyle::LK_Cpp,
21686             guessLanguage("foo.h", "[[using clang: fallthrough]];"));
21687   EXPECT_EQ(FormatStyle::LK_ObjC,
21688             guessLanguage("foo.h", "[[abusing clang:fallthrough] bar];"));
21689   EXPECT_EQ(FormatStyle::LK_Cpp,
21690             guessLanguage("foo.h", "[[using gsl: suppress(\"type\")]];"));
21691   EXPECT_EQ(
21692       FormatStyle::LK_Cpp,
21693       guessLanguage("foo.h", "for (auto &&[endpoint, stream] : streams_)"));
21694   EXPECT_EQ(
21695       FormatStyle::LK_Cpp,
21696       guessLanguage("foo.h",
21697                     "[[clang::callable_when(\"unconsumed\", \"unknown\")]]"));
21698   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[foo::bar, ...]]"));
21699 }
21700 
21701 TEST_F(FormatTest, GuessLanguageWithCaret) {
21702   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^);"));
21703   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^, Bar);"));
21704   EXPECT_EQ(FormatStyle::LK_ObjC,
21705             guessLanguage("foo.h", "int(^)(char, float);"));
21706   EXPECT_EQ(FormatStyle::LK_ObjC,
21707             guessLanguage("foo.h", "int(^foo)(char, float);"));
21708   EXPECT_EQ(FormatStyle::LK_ObjC,
21709             guessLanguage("foo.h", "int(^foo[10])(char, float);"));
21710   EXPECT_EQ(FormatStyle::LK_ObjC,
21711             guessLanguage("foo.h", "int(^foo[kNumEntries])(char, float);"));
21712   EXPECT_EQ(
21713       FormatStyle::LK_ObjC,
21714       guessLanguage("foo.h", "int(^foo[(kNumEntries + 10)])(char, float);"));
21715 }
21716 
21717 TEST_F(FormatTest, GuessLanguageWithPragmas) {
21718   EXPECT_EQ(FormatStyle::LK_Cpp,
21719             guessLanguage("foo.h", "__pragma(warning(disable:))"));
21720   EXPECT_EQ(FormatStyle::LK_Cpp,
21721             guessLanguage("foo.h", "#pragma(warning(disable:))"));
21722   EXPECT_EQ(FormatStyle::LK_Cpp,
21723             guessLanguage("foo.h", "_Pragma(warning(disable:))"));
21724 }
21725 
21726 TEST_F(FormatTest, FormatsInlineAsmSymbolicNames) {
21727   // ASM symbolic names are identifiers that must be surrounded by [] without
21728   // space in between:
21729   // https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#InputOperands
21730 
21731   // Example from https://bugs.llvm.org/show_bug.cgi?id=45108.
21732   verifyFormat(R"(//
21733 asm volatile("mrs %x[result], FPCR" : [result] "=r"(result));
21734 )");
21735 
21736   // A list of several ASM symbolic names.
21737   verifyFormat(R"(asm("mov %[e], %[d]" : [d] "=rm"(d), [e] "rm"(*e));)");
21738 
21739   // ASM symbolic names in inline ASM with inputs and outputs.
21740   verifyFormat(R"(//
21741 asm("cmoveq %1, %2, %[result]"
21742     : [result] "=r"(result)
21743     : "r"(test), "r"(new), "[result]"(old));
21744 )");
21745 
21746   // ASM symbolic names in inline ASM with no outputs.
21747   verifyFormat(R"(asm("mov %[e], %[d]" : : [d] "=rm"(d), [e] "rm"(*e));)");
21748 }
21749 
21750 TEST_F(FormatTest, GuessedLanguageWithInlineAsmClobbers) {
21751   EXPECT_EQ(FormatStyle::LK_Cpp,
21752             guessLanguage("foo.h", "void f() {\n"
21753                                    "  asm (\"mov %[e], %[d]\"\n"
21754                                    "     : [d] \"=rm\" (d)\n"
21755                                    "       [e] \"rm\" (*e));\n"
21756                                    "}"));
21757   EXPECT_EQ(FormatStyle::LK_Cpp,
21758             guessLanguage("foo.h", "void f() {\n"
21759                                    "  _asm (\"mov %[e], %[d]\"\n"
21760                                    "     : [d] \"=rm\" (d)\n"
21761                                    "       [e] \"rm\" (*e));\n"
21762                                    "}"));
21763   EXPECT_EQ(FormatStyle::LK_Cpp,
21764             guessLanguage("foo.h", "void f() {\n"
21765                                    "  __asm (\"mov %[e], %[d]\"\n"
21766                                    "     : [d] \"=rm\" (d)\n"
21767                                    "       [e] \"rm\" (*e));\n"
21768                                    "}"));
21769   EXPECT_EQ(FormatStyle::LK_Cpp,
21770             guessLanguage("foo.h", "void f() {\n"
21771                                    "  __asm__ (\"mov %[e], %[d]\"\n"
21772                                    "     : [d] \"=rm\" (d)\n"
21773                                    "       [e] \"rm\" (*e));\n"
21774                                    "}"));
21775   EXPECT_EQ(FormatStyle::LK_Cpp,
21776             guessLanguage("foo.h", "void f() {\n"
21777                                    "  asm (\"mov %[e], %[d]\"\n"
21778                                    "     : [d] \"=rm\" (d),\n"
21779                                    "       [e] \"rm\" (*e));\n"
21780                                    "}"));
21781   EXPECT_EQ(FormatStyle::LK_Cpp,
21782             guessLanguage("foo.h", "void f() {\n"
21783                                    "  asm volatile (\"mov %[e], %[d]\"\n"
21784                                    "     : [d] \"=rm\" (d)\n"
21785                                    "       [e] \"rm\" (*e));\n"
21786                                    "}"));
21787 }
21788 
21789 TEST_F(FormatTest, GuessLanguageWithChildLines) {
21790   EXPECT_EQ(FormatStyle::LK_Cpp,
21791             guessLanguage("foo.h", "#define FOO ({ std::string s; })"));
21792   EXPECT_EQ(FormatStyle::LK_ObjC,
21793             guessLanguage("foo.h", "#define FOO ({ NSString *s; })"));
21794   EXPECT_EQ(
21795       FormatStyle::LK_Cpp,
21796       guessLanguage("foo.h", "#define FOO ({ foo(); ({ std::string s; }) })"));
21797   EXPECT_EQ(
21798       FormatStyle::LK_ObjC,
21799       guessLanguage("foo.h", "#define FOO ({ foo(); ({ NSString *s; }) })"));
21800 }
21801 
21802 TEST_F(FormatTest, TypenameMacros) {
21803   std::vector<std::string> TypenameMacros = {"STACK_OF", "LIST", "TAILQ_ENTRY"};
21804 
21805   // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=30353
21806   FormatStyle Google = getGoogleStyleWithColumns(0);
21807   Google.TypenameMacros = TypenameMacros;
21808   verifyFormat("struct foo {\n"
21809                "  int bar;\n"
21810                "  TAILQ_ENTRY(a) bleh;\n"
21811                "};",
21812                Google);
21813 
21814   FormatStyle Macros = getLLVMStyle();
21815   Macros.TypenameMacros = TypenameMacros;
21816 
21817   verifyFormat("STACK_OF(int) a;", Macros);
21818   verifyFormat("STACK_OF(int) *a;", Macros);
21819   verifyFormat("STACK_OF(int const *) *a;", Macros);
21820   verifyFormat("STACK_OF(int *const) *a;", Macros);
21821   verifyFormat("STACK_OF(int, string) a;", Macros);
21822   verifyFormat("STACK_OF(LIST(int)) a;", Macros);
21823   verifyFormat("STACK_OF(LIST(int)) a, b;", Macros);
21824   verifyFormat("for (LIST(int) *a = NULL; a;) {\n}", Macros);
21825   verifyFormat("STACK_OF(int) f(LIST(int) *arg);", Macros);
21826   verifyFormat("vector<LIST(uint64_t) *attr> x;", Macros);
21827   verifyFormat("vector<LIST(uint64_t) *const> f(LIST(uint64_t) *arg);", Macros);
21828 
21829   Macros.PointerAlignment = FormatStyle::PAS_Left;
21830   verifyFormat("STACK_OF(int)* a;", Macros);
21831   verifyFormat("STACK_OF(int*)* a;", Macros);
21832   verifyFormat("x = (STACK_OF(uint64_t))*a;", Macros);
21833   verifyFormat("x = (STACK_OF(uint64_t))&a;", Macros);
21834   verifyFormat("vector<STACK_OF(uint64_t)* attr> x;", Macros);
21835 }
21836 
21837 TEST_F(FormatTest, AtomicQualifier) {
21838   // Check that we treate _Atomic as a type and not a function call
21839   FormatStyle Google = getGoogleStyleWithColumns(0);
21840   verifyFormat("struct foo {\n"
21841                "  int a1;\n"
21842                "  _Atomic(a) a2;\n"
21843                "  _Atomic(_Atomic(int) *const) a3;\n"
21844                "};",
21845                Google);
21846   verifyFormat("_Atomic(uint64_t) a;");
21847   verifyFormat("_Atomic(uint64_t) *a;");
21848   verifyFormat("_Atomic(uint64_t const *) *a;");
21849   verifyFormat("_Atomic(uint64_t *const) *a;");
21850   verifyFormat("_Atomic(const uint64_t *) *a;");
21851   verifyFormat("_Atomic(uint64_t) a;");
21852   verifyFormat("_Atomic(_Atomic(uint64_t)) a;");
21853   verifyFormat("_Atomic(_Atomic(uint64_t)) a, b;");
21854   verifyFormat("for (_Atomic(uint64_t) *a = NULL; a;) {\n}");
21855   verifyFormat("_Atomic(uint64_t) f(_Atomic(uint64_t) *arg);");
21856 
21857   verifyFormat("_Atomic(uint64_t) *s(InitValue);");
21858   verifyFormat("_Atomic(uint64_t) *s{InitValue};");
21859   FormatStyle Style = getLLVMStyle();
21860   Style.PointerAlignment = FormatStyle::PAS_Left;
21861   verifyFormat("_Atomic(uint64_t)* s(InitValue);", Style);
21862   verifyFormat("_Atomic(uint64_t)* s{InitValue};", Style);
21863   verifyFormat("_Atomic(int)* a;", Style);
21864   verifyFormat("_Atomic(int*)* a;", Style);
21865   verifyFormat("vector<_Atomic(uint64_t)* attr> x;", Style);
21866 
21867   Style.SpacesInCStyleCastParentheses = true;
21868   Style.SpacesInParentheses = false;
21869   verifyFormat("x = ( _Atomic(uint64_t) )*a;", Style);
21870   Style.SpacesInCStyleCastParentheses = false;
21871   Style.SpacesInParentheses = true;
21872   verifyFormat("x = (_Atomic( uint64_t ))*a;", Style);
21873   verifyFormat("x = (_Atomic( uint64_t ))&a;", Style);
21874 }
21875 
21876 TEST_F(FormatTest, AmbersandInLamda) {
21877   // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=41899
21878   FormatStyle AlignStyle = getLLVMStyle();
21879   AlignStyle.PointerAlignment = FormatStyle::PAS_Left;
21880   verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle);
21881   AlignStyle.PointerAlignment = FormatStyle::PAS_Right;
21882   verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle);
21883 }
21884 
21885 TEST_F(FormatTest, SpacesInConditionalStatement) {
21886   FormatStyle Spaces = getLLVMStyle();
21887   Spaces.IfMacros.clear();
21888   Spaces.IfMacros.push_back("MYIF");
21889   Spaces.SpacesInConditionalStatement = true;
21890   verifyFormat("for ( int i = 0; i; i++ )\n  continue;", Spaces);
21891   verifyFormat("if ( !a )\n  return;", Spaces);
21892   verifyFormat("if ( a )\n  return;", Spaces);
21893   verifyFormat("if constexpr ( a )\n  return;", Spaces);
21894   verifyFormat("MYIF ( a )\n  return;", Spaces);
21895   verifyFormat("MYIF ( a )\n  return;\nelse MYIF ( b )\n  return;", Spaces);
21896   verifyFormat("MYIF ( a )\n  return;\nelse\n  return;", Spaces);
21897   verifyFormat("switch ( a )\ncase 1:\n  return;", Spaces);
21898   verifyFormat("while ( a )\n  return;", Spaces);
21899   verifyFormat("while ( (a && b) )\n  return;", Spaces);
21900   verifyFormat("do {\n} while ( 1 != 0 );", Spaces);
21901   verifyFormat("try {\n} catch ( const std::exception & ) {\n}", Spaces);
21902   // Check that space on the left of "::" is inserted as expected at beginning
21903   // of condition.
21904   verifyFormat("while ( ::func() )\n  return;", Spaces);
21905 
21906   // Check impact of ControlStatementsExceptControlMacros is honored.
21907   Spaces.SpaceBeforeParens =
21908       FormatStyle::SBPO_ControlStatementsExceptControlMacros;
21909   verifyFormat("MYIF( a )\n  return;", Spaces);
21910   verifyFormat("MYIF( a )\n  return;\nelse MYIF( b )\n  return;", Spaces);
21911   verifyFormat("MYIF( a )\n  return;\nelse\n  return;", Spaces);
21912 }
21913 
21914 TEST_F(FormatTest, AlternativeOperators) {
21915   // Test case for ensuring alternate operators are not
21916   // combined with their right most neighbour.
21917   verifyFormat("int a and b;");
21918   verifyFormat("int a and_eq b;");
21919   verifyFormat("int a bitand b;");
21920   verifyFormat("int a bitor b;");
21921   verifyFormat("int a compl b;");
21922   verifyFormat("int a not b;");
21923   verifyFormat("int a not_eq b;");
21924   verifyFormat("int a or b;");
21925   verifyFormat("int a xor b;");
21926   verifyFormat("int a xor_eq b;");
21927   verifyFormat("return this not_eq bitand other;");
21928   verifyFormat("bool operator not_eq(const X bitand other)");
21929 
21930   verifyFormat("int a and 5;");
21931   verifyFormat("int a and_eq 5;");
21932   verifyFormat("int a bitand 5;");
21933   verifyFormat("int a bitor 5;");
21934   verifyFormat("int a compl 5;");
21935   verifyFormat("int a not 5;");
21936   verifyFormat("int a not_eq 5;");
21937   verifyFormat("int a or 5;");
21938   verifyFormat("int a xor 5;");
21939   verifyFormat("int a xor_eq 5;");
21940 
21941   verifyFormat("int a compl(5);");
21942   verifyFormat("int a not(5);");
21943 
21944   /* FIXME handle alternate tokens
21945    * https://en.cppreference.com/w/cpp/language/operator_alternative
21946   // alternative tokens
21947   verifyFormat("compl foo();");     //  ~foo();
21948   verifyFormat("foo() <%%>;");      // foo();
21949   verifyFormat("void foo() <%%>;"); // void foo(){}
21950   verifyFormat("int a <:1:>;");     // int a[1];[
21951   verifyFormat("%:define ABC abc"); // #define ABC abc
21952   verifyFormat("%:%:");             // ##
21953   */
21954 }
21955 
21956 TEST_F(FormatTest, STLWhileNotDefineChed) {
21957   verifyFormat("#if defined(while)\n"
21958                "#define while EMIT WARNING C4005\n"
21959                "#endif // while");
21960 }
21961 
21962 TEST_F(FormatTest, OperatorSpacing) {
21963   FormatStyle Style = getLLVMStyle();
21964   Style.PointerAlignment = FormatStyle::PAS_Right;
21965   verifyFormat("Foo::operator*();", Style);
21966   verifyFormat("Foo::operator void *();", Style);
21967   verifyFormat("Foo::operator void **();", Style);
21968   verifyFormat("Foo::operator void *&();", Style);
21969   verifyFormat("Foo::operator void *&&();", Style);
21970   verifyFormat("Foo::operator void const *();", Style);
21971   verifyFormat("Foo::operator void const **();", Style);
21972   verifyFormat("Foo::operator void const *&();", Style);
21973   verifyFormat("Foo::operator void const *&&();", Style);
21974   verifyFormat("Foo::operator()(void *);", Style);
21975   verifyFormat("Foo::operator*(void *);", Style);
21976   verifyFormat("Foo::operator*();", Style);
21977   verifyFormat("Foo::operator**();", Style);
21978   verifyFormat("Foo::operator&();", Style);
21979   verifyFormat("Foo::operator<int> *();", Style);
21980   verifyFormat("Foo::operator<Foo> *();", Style);
21981   verifyFormat("Foo::operator<int> **();", Style);
21982   verifyFormat("Foo::operator<Foo> **();", Style);
21983   verifyFormat("Foo::operator<int> &();", Style);
21984   verifyFormat("Foo::operator<Foo> &();", Style);
21985   verifyFormat("Foo::operator<int> &&();", Style);
21986   verifyFormat("Foo::operator<Foo> &&();", Style);
21987   verifyFormat("Foo::operator<int> *&();", Style);
21988   verifyFormat("Foo::operator<Foo> *&();", Style);
21989   verifyFormat("Foo::operator<int> *&&();", Style);
21990   verifyFormat("Foo::operator<Foo> *&&();", Style);
21991   verifyFormat("operator*(int (*)(), class Foo);", Style);
21992 
21993   verifyFormat("Foo::operator&();", Style);
21994   verifyFormat("Foo::operator void &();", Style);
21995   verifyFormat("Foo::operator void const &();", Style);
21996   verifyFormat("Foo::operator()(void &);", Style);
21997   verifyFormat("Foo::operator&(void &);", Style);
21998   verifyFormat("Foo::operator&();", Style);
21999   verifyFormat("operator&(int (&)(), class Foo);", Style);
22000   verifyFormat("operator&&(int (&)(), class Foo);", Style);
22001 
22002   verifyFormat("Foo::operator&&();", Style);
22003   verifyFormat("Foo::operator**();", Style);
22004   verifyFormat("Foo::operator void &&();", Style);
22005   verifyFormat("Foo::operator void const &&();", Style);
22006   verifyFormat("Foo::operator()(void &&);", Style);
22007   verifyFormat("Foo::operator&&(void &&);", Style);
22008   verifyFormat("Foo::operator&&();", Style);
22009   verifyFormat("operator&&(int (&&)(), class Foo);", Style);
22010   verifyFormat("operator const nsTArrayRight<E> &()", Style);
22011   verifyFormat("[[nodiscard]] operator const nsTArrayRight<E, Allocator> &()",
22012                Style);
22013   verifyFormat("operator void **()", Style);
22014   verifyFormat("operator const FooRight<Object> &()", Style);
22015   verifyFormat("operator const FooRight<Object> *()", Style);
22016   verifyFormat("operator const FooRight<Object> **()", Style);
22017   verifyFormat("operator const FooRight<Object> *&()", Style);
22018   verifyFormat("operator const FooRight<Object> *&&()", Style);
22019 
22020   Style.PointerAlignment = FormatStyle::PAS_Left;
22021   verifyFormat("Foo::operator*();", Style);
22022   verifyFormat("Foo::operator**();", Style);
22023   verifyFormat("Foo::operator void*();", Style);
22024   verifyFormat("Foo::operator void**();", Style);
22025   verifyFormat("Foo::operator void*&();", Style);
22026   verifyFormat("Foo::operator void*&&();", Style);
22027   verifyFormat("Foo::operator void const*();", Style);
22028   verifyFormat("Foo::operator void const**();", Style);
22029   verifyFormat("Foo::operator void const*&();", Style);
22030   verifyFormat("Foo::operator void const*&&();", Style);
22031   verifyFormat("Foo::operator/*comment*/ void*();", Style);
22032   verifyFormat("Foo::operator/*a*/ const /*b*/ void*();", Style);
22033   verifyFormat("Foo::operator/*a*/ volatile /*b*/ void*();", Style);
22034   verifyFormat("Foo::operator()(void*);", Style);
22035   verifyFormat("Foo::operator*(void*);", Style);
22036   verifyFormat("Foo::operator*();", Style);
22037   verifyFormat("Foo::operator<int>*();", Style);
22038   verifyFormat("Foo::operator<Foo>*();", Style);
22039   verifyFormat("Foo::operator<int>**();", Style);
22040   verifyFormat("Foo::operator<Foo>**();", Style);
22041   verifyFormat("Foo::operator<Foo>*&();", Style);
22042   verifyFormat("Foo::operator<int>&();", Style);
22043   verifyFormat("Foo::operator<Foo>&();", Style);
22044   verifyFormat("Foo::operator<int>&&();", Style);
22045   verifyFormat("Foo::operator<Foo>&&();", Style);
22046   verifyFormat("Foo::operator<int>*&();", Style);
22047   verifyFormat("Foo::operator<Foo>*&();", Style);
22048   verifyFormat("operator*(int (*)(), class Foo);", Style);
22049 
22050   verifyFormat("Foo::operator&();", Style);
22051   verifyFormat("Foo::operator void&();", Style);
22052   verifyFormat("Foo::operator void const&();", Style);
22053   verifyFormat("Foo::operator/*comment*/ void&();", Style);
22054   verifyFormat("Foo::operator/*a*/ const /*b*/ void&();", Style);
22055   verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&();", Style);
22056   verifyFormat("Foo::operator()(void&);", Style);
22057   verifyFormat("Foo::operator&(void&);", Style);
22058   verifyFormat("Foo::operator&();", Style);
22059   verifyFormat("operator&(int (&)(), class Foo);", Style);
22060   verifyFormat("operator&(int (&&)(), class Foo);", Style);
22061   verifyFormat("operator&&(int (&&)(), class Foo);", Style);
22062 
22063   verifyFormat("Foo::operator&&();", Style);
22064   verifyFormat("Foo::operator void&&();", Style);
22065   verifyFormat("Foo::operator void const&&();", Style);
22066   verifyFormat("Foo::operator/*comment*/ void&&();", Style);
22067   verifyFormat("Foo::operator/*a*/ const /*b*/ void&&();", Style);
22068   verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&&();", Style);
22069   verifyFormat("Foo::operator()(void&&);", Style);
22070   verifyFormat("Foo::operator&&(void&&);", Style);
22071   verifyFormat("Foo::operator&&();", Style);
22072   verifyFormat("operator&&(int (&&)(), class Foo);", Style);
22073   verifyFormat("operator const nsTArrayLeft<E>&()", Style);
22074   verifyFormat("[[nodiscard]] operator const nsTArrayLeft<E, Allocator>&()",
22075                Style);
22076   verifyFormat("operator void**()", Style);
22077   verifyFormat("operator const FooLeft<Object>&()", Style);
22078   verifyFormat("operator const FooLeft<Object>*()", Style);
22079   verifyFormat("operator const FooLeft<Object>**()", Style);
22080   verifyFormat("operator const FooLeft<Object>*&()", Style);
22081   verifyFormat("operator const FooLeft<Object>*&&()", Style);
22082 
22083   // PR45107
22084   verifyFormat("operator Vector<String>&();", Style);
22085   verifyFormat("operator const Vector<String>&();", Style);
22086   verifyFormat("operator foo::Bar*();", Style);
22087   verifyFormat("operator const Foo<X>::Bar<Y>*();", Style);
22088   verifyFormat("operator/*a*/ const /*b*/ Foo /*c*/<X> /*d*/ ::Bar<Y>*();",
22089                Style);
22090 
22091   Style.PointerAlignment = FormatStyle::PAS_Middle;
22092   verifyFormat("Foo::operator*();", Style);
22093   verifyFormat("Foo::operator void *();", Style);
22094   verifyFormat("Foo::operator()(void *);", Style);
22095   verifyFormat("Foo::operator*(void *);", Style);
22096   verifyFormat("Foo::operator*();", Style);
22097   verifyFormat("operator*(int (*)(), class Foo);", Style);
22098 
22099   verifyFormat("Foo::operator&();", Style);
22100   verifyFormat("Foo::operator void &();", Style);
22101   verifyFormat("Foo::operator void const &();", Style);
22102   verifyFormat("Foo::operator()(void &);", Style);
22103   verifyFormat("Foo::operator&(void &);", Style);
22104   verifyFormat("Foo::operator&();", Style);
22105   verifyFormat("operator&(int (&)(), class Foo);", Style);
22106 
22107   verifyFormat("Foo::operator&&();", Style);
22108   verifyFormat("Foo::operator void &&();", Style);
22109   verifyFormat("Foo::operator void const &&();", Style);
22110   verifyFormat("Foo::operator()(void &&);", Style);
22111   verifyFormat("Foo::operator&&(void &&);", Style);
22112   verifyFormat("Foo::operator&&();", Style);
22113   verifyFormat("operator&&(int (&&)(), class Foo);", Style);
22114 }
22115 
22116 TEST_F(FormatTest, OperatorPassedAsAFunctionPtr) {
22117   FormatStyle Style = getLLVMStyle();
22118   // PR46157
22119   verifyFormat("foo(operator+, -42);", Style);
22120   verifyFormat("foo(operator++, -42);", Style);
22121   verifyFormat("foo(operator--, -42);", Style);
22122   verifyFormat("foo(-42, operator--);", Style);
22123   verifyFormat("foo(-42, operator, );", Style);
22124   verifyFormat("foo(operator, , -42);", Style);
22125 }
22126 
22127 TEST_F(FormatTest, WhitespaceSensitiveMacros) {
22128   FormatStyle Style = getLLVMStyle();
22129   Style.WhitespaceSensitiveMacros.push_back("FOO");
22130 
22131   // Don't use the helpers here, since 'mess up' will change the whitespace
22132   // and these are all whitespace sensitive by definition
22133   EXPECT_EQ("FOO(String-ized&Messy+But(: :Still)=Intentional);",
22134             format("FOO(String-ized&Messy+But(: :Still)=Intentional);", Style));
22135   EXPECT_EQ(
22136       "FOO(String-ized&Messy+But\\(: :Still)=Intentional);",
22137       format("FOO(String-ized&Messy+But\\(: :Still)=Intentional);", Style));
22138   EXPECT_EQ("FOO(String-ized&Messy+But,: :Still=Intentional);",
22139             format("FOO(String-ized&Messy+But,: :Still=Intentional);", Style));
22140   EXPECT_EQ("FOO(String-ized&Messy+But,: :\n"
22141             "       Still=Intentional);",
22142             format("FOO(String-ized&Messy+But,: :\n"
22143                    "       Still=Intentional);",
22144                    Style));
22145   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
22146   EXPECT_EQ("FOO(String-ized=&Messy+But,: :\n"
22147             "       Still=Intentional);",
22148             format("FOO(String-ized=&Messy+But,: :\n"
22149                    "       Still=Intentional);",
22150                    Style));
22151 
22152   Style.ColumnLimit = 21;
22153   EXPECT_EQ("FOO(String-ized&Messy+But: :Still=Intentional);",
22154             format("FOO(String-ized&Messy+But: :Still=Intentional);", Style));
22155 }
22156 
22157 TEST_F(FormatTest, VeryLongNamespaceCommentSplit) {
22158   // These tests are not in NamespaceFixer because that doesn't
22159   // test its interaction with line wrapping
22160   FormatStyle Style = getLLVMStyle();
22161   Style.ColumnLimit = 80;
22162   verifyFormat("namespace {\n"
22163                "int i;\n"
22164                "int j;\n"
22165                "} // namespace",
22166                Style);
22167 
22168   verifyFormat("namespace AAA {\n"
22169                "int i;\n"
22170                "int j;\n"
22171                "} // namespace AAA",
22172                Style);
22173 
22174   EXPECT_EQ("namespace Averyveryveryverylongnamespace {\n"
22175             "int i;\n"
22176             "int j;\n"
22177             "} // namespace Averyveryveryverylongnamespace",
22178             format("namespace Averyveryveryverylongnamespace {\n"
22179                    "int i;\n"
22180                    "int j;\n"
22181                    "}",
22182                    Style));
22183 
22184   EXPECT_EQ(
22185       "namespace "
22186       "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n"
22187       "    went::mad::now {\n"
22188       "int i;\n"
22189       "int j;\n"
22190       "} // namespace\n"
22191       "  // "
22192       "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::"
22193       "went::mad::now",
22194       format("namespace "
22195              "would::it::save::you::a::lot::of::time::if_::i::"
22196              "just::gave::up::and_::went::mad::now {\n"
22197              "int i;\n"
22198              "int j;\n"
22199              "}",
22200              Style));
22201 
22202   // This used to duplicate the comment again and again on subsequent runs
22203   EXPECT_EQ(
22204       "namespace "
22205       "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n"
22206       "    went::mad::now {\n"
22207       "int i;\n"
22208       "int j;\n"
22209       "} // namespace\n"
22210       "  // "
22211       "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::"
22212       "went::mad::now",
22213       format("namespace "
22214              "would::it::save::you::a::lot::of::time::if_::i::"
22215              "just::gave::up::and_::went::mad::now {\n"
22216              "int i;\n"
22217              "int j;\n"
22218              "} // namespace\n"
22219              "  // "
22220              "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::"
22221              "and_::went::mad::now",
22222              Style));
22223 }
22224 
22225 TEST_F(FormatTest, LikelyUnlikely) {
22226   FormatStyle Style = getLLVMStyle();
22227 
22228   verifyFormat("if (argc > 5) [[unlikely]] {\n"
22229                "  return 29;\n"
22230                "}",
22231                Style);
22232 
22233   verifyFormat("if (argc > 5) [[likely]] {\n"
22234                "  return 29;\n"
22235                "}",
22236                Style);
22237 
22238   verifyFormat("if (argc > 5) [[unlikely]] {\n"
22239                "  return 29;\n"
22240                "} else [[likely]] {\n"
22241                "  return 42;\n"
22242                "}\n",
22243                Style);
22244 
22245   verifyFormat("if (argc > 5) [[unlikely]] {\n"
22246                "  return 29;\n"
22247                "} else if (argc > 10) [[likely]] {\n"
22248                "  return 99;\n"
22249                "} else {\n"
22250                "  return 42;\n"
22251                "}\n",
22252                Style);
22253 
22254   verifyFormat("if (argc > 5) [[gnu::unused]] {\n"
22255                "  return 29;\n"
22256                "}",
22257                Style);
22258 }
22259 
22260 TEST_F(FormatTest, PenaltyIndentedWhitespace) {
22261   verifyFormat("Constructor()\n"
22262                "    : aaaaaa(aaaaaa), aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
22263                "                          aaaa(aaaaaaaaaaaaaaaaaa, "
22264                "aaaaaaaaaaaaaaaaaat))");
22265   verifyFormat("Constructor()\n"
22266                "    : aaaaaaaaaaaaa(aaaaaa), "
22267                "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)");
22268 
22269   FormatStyle StyleWithWhitespacePenalty = getLLVMStyle();
22270   StyleWithWhitespacePenalty.PenaltyIndentedWhitespace = 5;
22271   verifyFormat("Constructor()\n"
22272                "    : aaaaaa(aaaaaa),\n"
22273                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
22274                "          aaaa(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaat))",
22275                StyleWithWhitespacePenalty);
22276   verifyFormat("Constructor()\n"
22277                "    : aaaaaaaaaaaaa(aaaaaa), "
22278                "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)",
22279                StyleWithWhitespacePenalty);
22280 }
22281 
22282 TEST_F(FormatTest, LLVMDefaultStyle) {
22283   FormatStyle Style = getLLVMStyle();
22284   verifyFormat("extern \"C\" {\n"
22285                "int foo();\n"
22286                "}",
22287                Style);
22288 }
22289 TEST_F(FormatTest, GNUDefaultStyle) {
22290   FormatStyle Style = getGNUStyle();
22291   verifyFormat("extern \"C\"\n"
22292                "{\n"
22293                "  int foo ();\n"
22294                "}",
22295                Style);
22296 }
22297 TEST_F(FormatTest, MozillaDefaultStyle) {
22298   FormatStyle Style = getMozillaStyle();
22299   verifyFormat("extern \"C\"\n"
22300                "{\n"
22301                "  int foo();\n"
22302                "}",
22303                Style);
22304 }
22305 TEST_F(FormatTest, GoogleDefaultStyle) {
22306   FormatStyle Style = getGoogleStyle();
22307   verifyFormat("extern \"C\" {\n"
22308                "int foo();\n"
22309                "}",
22310                Style);
22311 }
22312 TEST_F(FormatTest, ChromiumDefaultStyle) {
22313   FormatStyle Style = getChromiumStyle(FormatStyle::LanguageKind::LK_Cpp);
22314   verifyFormat("extern \"C\" {\n"
22315                "int foo();\n"
22316                "}",
22317                Style);
22318 }
22319 TEST_F(FormatTest, MicrosoftDefaultStyle) {
22320   FormatStyle Style = getMicrosoftStyle(FormatStyle::LanguageKind::LK_Cpp);
22321   verifyFormat("extern \"C\"\n"
22322                "{\n"
22323                "    int foo();\n"
22324                "}",
22325                Style);
22326 }
22327 TEST_F(FormatTest, WebKitDefaultStyle) {
22328   FormatStyle Style = getWebKitStyle();
22329   verifyFormat("extern \"C\" {\n"
22330                "int foo();\n"
22331                "}",
22332                Style);
22333 }
22334 
22335 TEST_F(FormatTest, ConceptsAndRequires) {
22336   FormatStyle Style = getLLVMStyle();
22337   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
22338 
22339   verifyFormat("template <typename T>\n"
22340                "concept Hashable = requires(T a) {\n"
22341                "  { std::hash<T>{}(a) } -> std::convertible_to<std::size_t>;\n"
22342                "};",
22343                Style);
22344   verifyFormat("template <typename T>\n"
22345                "concept EqualityComparable = requires(T a, T b) {\n"
22346                "  { a == b } -> bool;\n"
22347                "};",
22348                Style);
22349   verifyFormat("template <typename T>\n"
22350                "concept EqualityComparable = requires(T a, T b) {\n"
22351                "  { a == b } -> bool;\n"
22352                "  { a != b } -> bool;\n"
22353                "};",
22354                Style);
22355   verifyFormat("template <typename T>\n"
22356                "concept EqualityComparable = requires(T a, T b) {\n"
22357                "  { a == b } -> bool;\n"
22358                "  { a != b } -> bool;\n"
22359                "};",
22360                Style);
22361 
22362   verifyFormat("template <typename It>\n"
22363                "requires Iterator<It>\n"
22364                "void sort(It begin, It end) {\n"
22365                "  //....\n"
22366                "}",
22367                Style);
22368 
22369   verifyFormat("template <typename T>\n"
22370                "concept Large = sizeof(T) > 10;",
22371                Style);
22372 
22373   verifyFormat("template <typename T, typename U>\n"
22374                "concept FooableWith = requires(T t, U u) {\n"
22375                "  typename T::foo_type;\n"
22376                "  { t.foo(u) } -> typename T::foo_type;\n"
22377                "  t++;\n"
22378                "};\n"
22379                "void doFoo(FooableWith<int> auto t) {\n"
22380                "  t.foo(3);\n"
22381                "}",
22382                Style);
22383   verifyFormat("template <typename T>\n"
22384                "concept Context = sizeof(T) == 1;",
22385                Style);
22386   verifyFormat("template <typename T>\n"
22387                "concept Context = is_specialization_of_v<context, T>;",
22388                Style);
22389   verifyFormat("template <typename T>\n"
22390                "concept Node = std::is_object_v<T>;",
22391                Style);
22392   verifyFormat("template <typename T>\n"
22393                "concept Tree = true;",
22394                Style);
22395 
22396   verifyFormat("template <typename T> int g(T i) requires Concept1<I> {\n"
22397                "  //...\n"
22398                "}",
22399                Style);
22400 
22401   verifyFormat(
22402       "template <typename T> int g(T i) requires Concept1<I> && Concept2<I> {\n"
22403       "  //...\n"
22404       "}",
22405       Style);
22406 
22407   verifyFormat(
22408       "template <typename T> int g(T i) requires Concept1<I> || Concept2<I> {\n"
22409       "  //...\n"
22410       "}",
22411       Style);
22412 
22413   verifyFormat("template <typename T>\n"
22414                "veryveryvery_long_return_type g(T i) requires Concept1<I> || "
22415                "Concept2<I> {\n"
22416                "  //...\n"
22417                "}",
22418                Style);
22419 
22420   verifyFormat("template <typename T>\n"
22421                "veryveryvery_long_return_type g(T i) requires Concept1<I> && "
22422                "Concept2<I> {\n"
22423                "  //...\n"
22424                "}",
22425                Style);
22426 
22427   verifyFormat(
22428       "template <typename T>\n"
22429       "veryveryvery_long_return_type g(T i) requires Concept1 && Concept2 {\n"
22430       "  //...\n"
22431       "}",
22432       Style);
22433 
22434   verifyFormat(
22435       "template <typename T>\n"
22436       "veryveryvery_long_return_type g(T i) requires Concept1 || Concept2 {\n"
22437       "  //...\n"
22438       "}",
22439       Style);
22440 
22441   verifyFormat("template <typename It>\n"
22442                "requires Foo<It>() && Bar<It> {\n"
22443                "  //....\n"
22444                "}",
22445                Style);
22446 
22447   verifyFormat("template <typename It>\n"
22448                "requires Foo<Bar<It>>() && Bar<Foo<It, It>> {\n"
22449                "  //....\n"
22450                "}",
22451                Style);
22452 
22453   verifyFormat("template <typename It>\n"
22454                "requires Foo<Bar<It, It>>() && Bar<Foo<It, It>> {\n"
22455                "  //....\n"
22456                "}",
22457                Style);
22458 
22459   verifyFormat(
22460       "template <typename It>\n"
22461       "requires Foo<Bar<It>, Baz<It>>() && Bar<Foo<It>, Baz<It, It>> {\n"
22462       "  //....\n"
22463       "}",
22464       Style);
22465 
22466   Style.IndentRequires = true;
22467   verifyFormat("template <typename It>\n"
22468                "  requires Iterator<It>\n"
22469                "void sort(It begin, It end) {\n"
22470                "  //....\n"
22471                "}",
22472                Style);
22473   verifyFormat("template <std::size index_>\n"
22474                "  requires(index_ < sizeof...(Children_))\n"
22475                "Tree auto &child() {\n"
22476                "  // ...\n"
22477                "}",
22478                Style);
22479 
22480   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
22481   verifyFormat("template <typename T>\n"
22482                "concept Hashable = requires (T a) {\n"
22483                "  { std::hash<T>{}(a) } -> std::convertible_to<std::size_t>;\n"
22484                "};",
22485                Style);
22486 
22487   verifyFormat("template <class T = void>\n"
22488                "  requires EqualityComparable<T> || Same<T, void>\n"
22489                "struct equal_to;",
22490                Style);
22491 
22492   verifyFormat("template <class T>\n"
22493                "  requires requires {\n"
22494                "    T{};\n"
22495                "    T (int);\n"
22496                "  }\n",
22497                Style);
22498 
22499   Style.ColumnLimit = 78;
22500   verifyFormat("template <typename T>\n"
22501                "concept Context = Traits<typename T::traits_type> and\n"
22502                "    Interface<typename T::interface_type> and\n"
22503                "    Request<typename T::request_type> and\n"
22504                "    Response<typename T::response_type> and\n"
22505                "    ContextExtension<typename T::extension_type> and\n"
22506                "    ::std::is_copy_constructable<T> and "
22507                "::std::is_move_constructable<T> and\n"
22508                "    requires (T c) {\n"
22509                "  { c.response; } -> Response;\n"
22510                "} and requires (T c) {\n"
22511                "  { c.request; } -> Request;\n"
22512                "}\n",
22513                Style);
22514 
22515   verifyFormat("template <typename T>\n"
22516                "concept Context = Traits<typename T::traits_type> or\n"
22517                "    Interface<typename T::interface_type> or\n"
22518                "    Request<typename T::request_type> or\n"
22519                "    Response<typename T::response_type> or\n"
22520                "    ContextExtension<typename T::extension_type> or\n"
22521                "    ::std::is_copy_constructable<T> or "
22522                "::std::is_move_constructable<T> or\n"
22523                "    requires (T c) {\n"
22524                "  { c.response; } -> Response;\n"
22525                "} or requires (T c) {\n"
22526                "  { c.request; } -> Request;\n"
22527                "}\n",
22528                Style);
22529 
22530   verifyFormat("template <typename T>\n"
22531                "concept Context = Traits<typename T::traits_type> &&\n"
22532                "    Interface<typename T::interface_type> &&\n"
22533                "    Request<typename T::request_type> &&\n"
22534                "    Response<typename T::response_type> &&\n"
22535                "    ContextExtension<typename T::extension_type> &&\n"
22536                "    ::std::is_copy_constructable<T> && "
22537                "::std::is_move_constructable<T> &&\n"
22538                "    requires (T c) {\n"
22539                "  { c.response; } -> Response;\n"
22540                "} && requires (T c) {\n"
22541                "  { c.request; } -> Request;\n"
22542                "}\n",
22543                Style);
22544 
22545   verifyFormat("template <typename T>\nconcept someConcept = Constraint1<T> && "
22546                "Constraint2<T>;");
22547 
22548   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
22549   Style.BraceWrapping.AfterFunction = true;
22550   Style.BraceWrapping.AfterClass = true;
22551   Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
22552   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
22553   verifyFormat("void Foo () requires (std::copyable<T>)\n"
22554                "{\n"
22555                "  return\n"
22556                "}\n",
22557                Style);
22558 
22559   verifyFormat("void Foo () requires std::copyable<T>\n"
22560                "{\n"
22561                "  return\n"
22562                "}\n",
22563                Style);
22564 
22565   verifyFormat("template <std::semiregular F, std::semiregular... Args>\n"
22566                "  requires (std::invocable<F, std::invoke_result_t<Args>...>)\n"
22567                "struct constant;",
22568                Style);
22569 
22570   verifyFormat("template <std::semiregular F, std::semiregular... Args>\n"
22571                "  requires std::invocable<F, std::invoke_result_t<Args>...>\n"
22572                "struct constant;",
22573                Style);
22574 
22575   verifyFormat("template <class T>\n"
22576                "class plane_with_very_very_very_long_name\n"
22577                "{\n"
22578                "  constexpr plane_with_very_very_very_long_name () requires "
22579                "std::copyable<T>\n"
22580                "      : plane_with_very_very_very_long_name (1)\n"
22581                "  {\n"
22582                "  }\n"
22583                "}\n",
22584                Style);
22585 
22586   verifyFormat("template <class T>\n"
22587                "class plane_with_long_name\n"
22588                "{\n"
22589                "  constexpr plane_with_long_name () requires std::copyable<T>\n"
22590                "      : plane_with_long_name (1)\n"
22591                "  {\n"
22592                "  }\n"
22593                "}\n",
22594                Style);
22595 
22596   Style.BreakBeforeConceptDeclarations = false;
22597   verifyFormat("template <typename T> concept Tree = true;", Style);
22598 
22599   Style.IndentRequires = false;
22600   verifyFormat("template <std::semiregular F, std::semiregular... Args>\n"
22601                "requires (std::invocable<F, std::invoke_result_t<Args>...>) "
22602                "struct constant;",
22603                Style);
22604 }
22605 
22606 TEST_F(FormatTest, StatementAttributeLikeMacros) {
22607   FormatStyle Style = getLLVMStyle();
22608   StringRef Source = "void Foo::slot() {\n"
22609                      "  unsigned char MyChar = 'x';\n"
22610                      "  emit signal(MyChar);\n"
22611                      "  Q_EMIT signal(MyChar);\n"
22612                      "}";
22613 
22614   EXPECT_EQ(Source, format(Source, Style));
22615 
22616   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
22617   EXPECT_EQ("void Foo::slot() {\n"
22618             "  unsigned char MyChar = 'x';\n"
22619             "  emit          signal(MyChar);\n"
22620             "  Q_EMIT signal(MyChar);\n"
22621             "}",
22622             format(Source, Style));
22623 
22624   Style.StatementAttributeLikeMacros.push_back("emit");
22625   EXPECT_EQ(Source, format(Source, Style));
22626 
22627   Style.StatementAttributeLikeMacros = {};
22628   EXPECT_EQ("void Foo::slot() {\n"
22629             "  unsigned char MyChar = 'x';\n"
22630             "  emit          signal(MyChar);\n"
22631             "  Q_EMIT        signal(MyChar);\n"
22632             "}",
22633             format(Source, Style));
22634 }
22635 
22636 TEST_F(FormatTest, IndentAccessModifiers) {
22637   FormatStyle Style = getLLVMStyle();
22638   Style.IndentAccessModifiers = true;
22639   // Members are *two* levels below the record;
22640   // Style.IndentWidth == 2, thus yielding a 4 spaces wide indentation.
22641   verifyFormat("class C {\n"
22642                "    int i;\n"
22643                "};\n",
22644                Style);
22645   verifyFormat("union C {\n"
22646                "    int i;\n"
22647                "    unsigned u;\n"
22648                "};\n",
22649                Style);
22650   // Access modifiers should be indented one level below the record.
22651   verifyFormat("class C {\n"
22652                "  public:\n"
22653                "    int i;\n"
22654                "};\n",
22655                Style);
22656   verifyFormat("struct S {\n"
22657                "  private:\n"
22658                "    class C {\n"
22659                "        int j;\n"
22660                "\n"
22661                "      public:\n"
22662                "        C();\n"
22663                "    };\n"
22664                "\n"
22665                "  public:\n"
22666                "    int i;\n"
22667                "};\n",
22668                Style);
22669   // Enumerations are not records and should be unaffected.
22670   Style.AllowShortEnumsOnASingleLine = false;
22671   verifyFormat("enum class E {\n"
22672                "  A,\n"
22673                "  B\n"
22674                "};\n",
22675                Style);
22676   // Test with a different indentation width;
22677   // also proves that the result is Style.AccessModifierOffset agnostic.
22678   Style.IndentWidth = 3;
22679   verifyFormat("class C {\n"
22680                "   public:\n"
22681                "      int i;\n"
22682                "};\n",
22683                Style);
22684 }
22685 
22686 TEST_F(FormatTest, LimitlessStringsAndComments) {
22687   auto Style = getLLVMStyleWithColumns(0);
22688   constexpr StringRef Code =
22689       "/**\n"
22690       " * This is a multiline comment with quite some long lines, at least for "
22691       "the LLVM Style.\n"
22692       " * We will redo this with strings and line comments. Just to  check if "
22693       "everything is working.\n"
22694       " */\n"
22695       "bool foo() {\n"
22696       "  /* Single line multi line comment. */\n"
22697       "  const std::string String = \"This is a multiline string with quite "
22698       "some long lines, at least for the LLVM Style.\"\n"
22699       "                             \"We already did it with multi line "
22700       "comments, and we will do it with line comments. Just to check if "
22701       "everything is working.\";\n"
22702       "  // This is a line comment (block) with quite some long lines, at "
22703       "least for the LLVM Style.\n"
22704       "  // We already did this with multi line comments and strings. Just to "
22705       "check if everything is working.\n"
22706       "  const std::string SmallString = \"Hello World\";\n"
22707       "  // Small line comment\n"
22708       "  return String.size() > SmallString.size();\n"
22709       "}";
22710   EXPECT_EQ(Code, format(Code, Style));
22711 }
22712 
22713 TEST_F(FormatTest, FormatDecayCopy) {
22714   // error cases from unit tests
22715   verifyFormat("foo(auto())");
22716   verifyFormat("foo(auto{})");
22717   verifyFormat("foo(auto({}))");
22718   verifyFormat("foo(auto{{}})");
22719 
22720   verifyFormat("foo(auto(1))");
22721   verifyFormat("foo(auto{1})");
22722   verifyFormat("foo(new auto(1))");
22723   verifyFormat("foo(new auto{1})");
22724   verifyFormat("decltype(auto(1)) x;");
22725   verifyFormat("decltype(auto{1}) x;");
22726   verifyFormat("auto(x);");
22727   verifyFormat("auto{x};");
22728   verifyFormat("new auto{x};");
22729   verifyFormat("auto{x} = y;");
22730   verifyFormat("auto(x) = y;"); // actually a declaration, but this is clearly
22731                                 // the user's own fault
22732   verifyFormat("integral auto(x) = y;"); // actually a declaration, but this is
22733                                          // clearly the user's own fault
22734   verifyFormat("auto(*p)() = f;");       // actually a declaration; TODO FIXME
22735 }
22736 
22737 TEST_F(FormatTest, Cpp20ModulesSupport) {
22738   FormatStyle Style = getLLVMStyle();
22739   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
22740   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
22741 
22742   verifyFormat("export import foo;", Style);
22743   verifyFormat("export import foo:bar;", Style);
22744   verifyFormat("export import foo.bar;", Style);
22745   verifyFormat("export import foo.bar:baz;", Style);
22746   verifyFormat("export import :bar;", Style);
22747   verifyFormat("export module foo:bar;", Style);
22748   verifyFormat("export module foo;", Style);
22749   verifyFormat("export module foo.bar;", Style);
22750   verifyFormat("export module foo.bar:baz;", Style);
22751   verifyFormat("export import <string_view>;", Style);
22752 
22753   verifyFormat("export type_name var;", Style);
22754   verifyFormat("template <class T> export using A = B<T>;", Style);
22755   verifyFormat("export using A = B;", Style);
22756   verifyFormat("export int func() {\n"
22757                "  foo();\n"
22758                "}",
22759                Style);
22760   verifyFormat("export struct {\n"
22761                "  int foo;\n"
22762                "};",
22763                Style);
22764   verifyFormat("export {\n"
22765                "  int foo;\n"
22766                "};",
22767                Style);
22768   verifyFormat("export export char const *hello() { return \"hello\"; }");
22769 
22770   verifyFormat("import bar;", Style);
22771   verifyFormat("import foo.bar;", Style);
22772   verifyFormat("import foo:bar;", Style);
22773   verifyFormat("import :bar;", Style);
22774   verifyFormat("import <ctime>;", Style);
22775   verifyFormat("import \"header\";", Style);
22776 
22777   verifyFormat("module foo;", Style);
22778   verifyFormat("module foo:bar;", Style);
22779   verifyFormat("module foo.bar;", Style);
22780   verifyFormat("module;", Style);
22781 
22782   verifyFormat("export namespace hi {\n"
22783                "const char *sayhi();\n"
22784                "}",
22785                Style);
22786 
22787   verifyFormat("module :private;", Style);
22788   verifyFormat("import <foo/bar.h>;", Style);
22789   verifyFormat("import foo...bar;", Style);
22790   verifyFormat("import ..........;", Style);
22791   verifyFormat("module foo:private;", Style);
22792   verifyFormat("import a", Style);
22793   verifyFormat("module a", Style);
22794   verifyFormat("export import a", Style);
22795   verifyFormat("export module a", Style);
22796 
22797   verifyFormat("import", Style);
22798   verifyFormat("module", Style);
22799   verifyFormat("export", Style);
22800 }
22801 
22802 TEST_F(FormatTest, CoroutineForCoawait) {
22803   FormatStyle Style = getLLVMStyle();
22804   verifyFormat("for co_await (auto x : range())\n  ;");
22805   verifyFormat("for (auto i : arr) {\n"
22806                "}",
22807                Style);
22808   verifyFormat("for co_await (auto i : arr) {\n"
22809                "}",
22810                Style);
22811   verifyFormat("for co_await (auto i : foo(T{})) {\n"
22812                "}",
22813                Style);
22814 }
22815 
22816 TEST_F(FormatTest, CoroutineCoAwait) {
22817   verifyFormat("int x = co_await foo();");
22818   verifyFormat("int x = (co_await foo());");
22819   verifyFormat("co_await (42);");
22820   verifyFormat("void operator co_await(int);");
22821   verifyFormat("void operator co_await(a);");
22822   verifyFormat("co_await a;");
22823   verifyFormat("co_await missing_await_resume{};");
22824   verifyFormat("co_await a; // comment");
22825   verifyFormat("void test0() { co_await a; }");
22826   verifyFormat("co_await co_await co_await foo();");
22827   verifyFormat("co_await foo().bar();");
22828   verifyFormat("co_await [this]() -> Task { co_return x; }");
22829   verifyFormat("co_await [this](int a, int b) -> Task { co_return co_await "
22830                "foo(); }(x, y);");
22831 
22832   FormatStyle Style = getLLVMStyle();
22833   Style.ColumnLimit = 40;
22834   verifyFormat("co_await [this](int a, int b) -> Task {\n"
22835                "  co_return co_await foo();\n"
22836                "}(x, y);",
22837                Style);
22838   verifyFormat("co_await;");
22839 }
22840 
22841 TEST_F(FormatTest, CoroutineCoYield) {
22842   verifyFormat("int x = co_yield foo();");
22843   verifyFormat("int x = (co_yield foo());");
22844   verifyFormat("co_yield (42);");
22845   verifyFormat("co_yield {42};");
22846   verifyFormat("co_yield 42;");
22847   verifyFormat("co_yield n++;");
22848   verifyFormat("co_yield ++n;");
22849   verifyFormat("co_yield;");
22850 }
22851 
22852 TEST_F(FormatTest, CoroutineCoReturn) {
22853   verifyFormat("co_return (42);");
22854   verifyFormat("co_return;");
22855   verifyFormat("co_return {};");
22856   verifyFormat("co_return x;");
22857   verifyFormat("co_return co_await foo();");
22858   verifyFormat("co_return co_yield foo();");
22859 }
22860 
22861 } // namespace
22862 } // namespace format
22863 } // namespace clang
22864