1 //===- unittest/Format/FormatTest.cpp - Formatting unit tests -------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "clang/Format/Format.h"
10 
11 #include "../Tooling/ReplacementTest.h"
12 #include "FormatTestUtils.h"
13 
14 #include "llvm/Support/Debug.h"
15 #include "llvm/Support/MemoryBuffer.h"
16 #include "gtest/gtest.h"
17 
18 #define DEBUG_TYPE "format-test"
19 
20 using clang::tooling::ReplacementTest;
21 using clang::tooling::toReplacements;
22 using testing::ScopedTrace;
23 
24 namespace clang {
25 namespace format {
26 namespace {
27 
28 FormatStyle getGoogleStyle() { return getGoogleStyle(FormatStyle::LK_Cpp); }
29 
30 class FormatTest : public ::testing::Test {
31 protected:
32   enum StatusCheck { SC_ExpectComplete, SC_ExpectIncomplete, SC_DoNotCheck };
33 
34   std::string format(llvm::StringRef Code,
35                      const FormatStyle &Style = getLLVMStyle(),
36                      StatusCheck CheckComplete = SC_ExpectComplete) {
37     LLVM_DEBUG(llvm::errs() << "---\n");
38     LLVM_DEBUG(llvm::errs() << Code << "\n\n");
39     std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size()));
40     FormattingAttemptStatus Status;
41     tooling::Replacements Replaces =
42         reformat(Style, Code, Ranges, "<stdin>", &Status);
43     if (CheckComplete != SC_DoNotCheck) {
44       bool ExpectedCompleteFormat = CheckComplete == SC_ExpectComplete;
45       EXPECT_EQ(ExpectedCompleteFormat, Status.FormatComplete)
46           << Code << "\n\n";
47     }
48     ReplacementCount = Replaces.size();
49     auto Result = applyAllReplacements(Code, Replaces);
50     EXPECT_TRUE(static_cast<bool>(Result));
51     LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
52     return *Result;
53   }
54 
55   FormatStyle getStyleWithColumns(FormatStyle Style, unsigned ColumnLimit) {
56     Style.ColumnLimit = ColumnLimit;
57     return Style;
58   }
59 
60   FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) {
61     return getStyleWithColumns(getLLVMStyle(), ColumnLimit);
62   }
63 
64   FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) {
65     return getStyleWithColumns(getGoogleStyle(), ColumnLimit);
66   }
67 
68   void _verifyFormat(const char *File, int Line, llvm::StringRef Expected,
69                      llvm::StringRef Code,
70                      const FormatStyle &Style = getLLVMStyle()) {
71     ScopedTrace t(File, Line, ::testing::Message() << Code.str());
72     EXPECT_EQ(Expected.str(), format(Expected, Style))
73         << "Expected code is not stable";
74     EXPECT_EQ(Expected.str(), format(Code, Style));
75     if (Style.Language == FormatStyle::LK_Cpp) {
76       // Objective-C++ is a superset of C++, so everything checked for C++
77       // needs to be checked for Objective-C++ as well.
78       FormatStyle ObjCStyle = Style;
79       ObjCStyle.Language = FormatStyle::LK_ObjC;
80       EXPECT_EQ(Expected.str(), format(test::messUp(Code), ObjCStyle));
81     }
82   }
83 
84   void _verifyFormat(const char *File, int Line, llvm::StringRef Code,
85                      const FormatStyle &Style = getLLVMStyle()) {
86     _verifyFormat(File, Line, Code, test::messUp(Code), Style);
87   }
88 
89   void _verifyIncompleteFormat(const char *File, int Line, llvm::StringRef Code,
90                                const FormatStyle &Style = getLLVMStyle()) {
91     ScopedTrace t(File, Line, ::testing::Message() << Code.str());
92     EXPECT_EQ(Code.str(),
93               format(test::messUp(Code), Style, SC_ExpectIncomplete));
94   }
95 
96   void _verifyIndependentOfContext(const char *File, int Line,
97                                    llvm::StringRef Text,
98                                    const FormatStyle &Style = getLLVMStyle()) {
99     _verifyFormat(File, Line, Text, Style);
100     _verifyFormat(File, Line, llvm::Twine("void f() { " + Text + " }").str(),
101                   Style);
102   }
103 
104   /// \brief Verify that clang-format does not crash on the given input.
105   void verifyNoCrash(llvm::StringRef Code,
106                      const FormatStyle &Style = getLLVMStyle()) {
107     format(Code, Style, SC_DoNotCheck);
108   }
109 
110   int ReplacementCount;
111 };
112 
113 #define verifyIndependentOfContext(...)                                        \
114   _verifyIndependentOfContext(__FILE__, __LINE__, __VA_ARGS__)
115 #define verifyIncompleteFormat(...)                                            \
116   _verifyIncompleteFormat(__FILE__, __LINE__, __VA_ARGS__)
117 #define verifyFormat(...) _verifyFormat(__FILE__, __LINE__, __VA_ARGS__)
118 #define verifyGoogleFormat(Code) verifyFormat(Code, getGoogleStyle())
119 
120 TEST_F(FormatTest, MessUp) {
121   EXPECT_EQ("1 2 3", test::messUp("1 2 3"));
122   EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n"));
123   EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc"));
124   EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc"));
125   EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne"));
126 }
127 
128 TEST_F(FormatTest, DefaultLLVMStyleIsCpp) {
129   EXPECT_EQ(FormatStyle::LK_Cpp, getLLVMStyle().Language);
130 }
131 
132 TEST_F(FormatTest, LLVMStyleOverride) {
133   EXPECT_EQ(FormatStyle::LK_Proto,
134             getLLVMStyle(FormatStyle::LK_Proto).Language);
135 }
136 
137 //===----------------------------------------------------------------------===//
138 // Basic function tests.
139 //===----------------------------------------------------------------------===//
140 
141 TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) {
142   EXPECT_EQ(";", format(";"));
143 }
144 
145 TEST_F(FormatTest, FormatsGlobalStatementsAt0) {
146   EXPECT_EQ("int i;", format("  int i;"));
147   EXPECT_EQ("\nint i;", format(" \n\t \v \f  int i;"));
148   EXPECT_EQ("int i;\nint j;", format("    int i; int j;"));
149   EXPECT_EQ("int i;\nint j;", format("    int i;\n  int j;"));
150 }
151 
152 TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) {
153   EXPECT_EQ("int i;", format("int\ni;"));
154 }
155 
156 TEST_F(FormatTest, FormatsNestedBlockStatements) {
157   EXPECT_EQ("{\n  {\n    {}\n  }\n}", format("{{{}}}"));
158 }
159 
160 TEST_F(FormatTest, FormatsNestedCall) {
161   verifyFormat("Method(f1, f2(f3));");
162   verifyFormat("Method(f1(f2, f3()));");
163   verifyFormat("Method(f1(f2, (f3())));");
164 }
165 
166 TEST_F(FormatTest, NestedNameSpecifiers) {
167   verifyFormat("vector<::Type> v;");
168   verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())");
169   verifyFormat("static constexpr bool Bar = decltype(bar())::value;");
170   verifyFormat("static constexpr bool Bar = typeof(bar())::value;");
171   verifyFormat("static constexpr bool Bar = __underlying_type(bar())::value;");
172   verifyFormat("static constexpr bool Bar = _Atomic(bar())::value;");
173   verifyFormat("bool a = 2 < ::SomeFunction();");
174   verifyFormat("ALWAYS_INLINE ::std::string getName();");
175   verifyFormat("some::string getName();");
176 }
177 
178 TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) {
179   EXPECT_EQ("if (a) {\n"
180             "  f();\n"
181             "}",
182             format("if(a){f();}"));
183   EXPECT_EQ(4, ReplacementCount);
184   EXPECT_EQ("if (a) {\n"
185             "  f();\n"
186             "}",
187             format("if (a) {\n"
188                    "  f();\n"
189                    "}"));
190   EXPECT_EQ(0, ReplacementCount);
191   EXPECT_EQ("/*\r\n"
192             "\r\n"
193             "*/\r\n",
194             format("/*\r\n"
195                    "\r\n"
196                    "*/\r\n"));
197   EXPECT_EQ(0, ReplacementCount);
198 }
199 
200 TEST_F(FormatTest, RemovesEmptyLines) {
201   EXPECT_EQ("class C {\n"
202             "  int i;\n"
203             "};",
204             format("class C {\n"
205                    " int i;\n"
206                    "\n"
207                    "};"));
208 
209   // Don't remove empty lines at the start of namespaces or extern "C" blocks.
210   EXPECT_EQ("namespace N {\n"
211             "\n"
212             "int i;\n"
213             "}",
214             format("namespace N {\n"
215                    "\n"
216                    "int    i;\n"
217                    "}",
218                    getGoogleStyle()));
219   EXPECT_EQ("/* something */ namespace N {\n"
220             "\n"
221             "int i;\n"
222             "}",
223             format("/* something */ namespace N {\n"
224                    "\n"
225                    "int    i;\n"
226                    "}",
227                    getGoogleStyle()));
228   EXPECT_EQ("inline namespace N {\n"
229             "\n"
230             "int i;\n"
231             "}",
232             format("inline namespace N {\n"
233                    "\n"
234                    "int    i;\n"
235                    "}",
236                    getGoogleStyle()));
237   EXPECT_EQ("/* something */ inline namespace N {\n"
238             "\n"
239             "int i;\n"
240             "}",
241             format("/* something */ inline namespace N {\n"
242                    "\n"
243                    "int    i;\n"
244                    "}",
245                    getGoogleStyle()));
246   EXPECT_EQ("export namespace N {\n"
247             "\n"
248             "int i;\n"
249             "}",
250             format("export namespace N {\n"
251                    "\n"
252                    "int    i;\n"
253                    "}",
254                    getGoogleStyle()));
255   EXPECT_EQ("extern /**/ \"C\" /**/ {\n"
256             "\n"
257             "int i;\n"
258             "}",
259             format("extern /**/ \"C\" /**/ {\n"
260                    "\n"
261                    "int    i;\n"
262                    "}",
263                    getGoogleStyle()));
264 
265   auto CustomStyle = getLLVMStyle();
266   CustomStyle.BreakBeforeBraces = FormatStyle::BS_Custom;
267   CustomStyle.BraceWrapping.AfterNamespace = true;
268   CustomStyle.KeepEmptyLinesAtTheStartOfBlocks = false;
269   EXPECT_EQ("namespace N\n"
270             "{\n"
271             "\n"
272             "int i;\n"
273             "}",
274             format("namespace N\n"
275                    "{\n"
276                    "\n"
277                    "\n"
278                    "int    i;\n"
279                    "}",
280                    CustomStyle));
281   EXPECT_EQ("/* something */ namespace N\n"
282             "{\n"
283             "\n"
284             "int i;\n"
285             "}",
286             format("/* something */ namespace N {\n"
287                    "\n"
288                    "\n"
289                    "int    i;\n"
290                    "}",
291                    CustomStyle));
292   EXPECT_EQ("inline namespace N\n"
293             "{\n"
294             "\n"
295             "int i;\n"
296             "}",
297             format("inline namespace N\n"
298                    "{\n"
299                    "\n"
300                    "\n"
301                    "int    i;\n"
302                    "}",
303                    CustomStyle));
304   EXPECT_EQ("/* something */ inline namespace N\n"
305             "{\n"
306             "\n"
307             "int i;\n"
308             "}",
309             format("/* something */ inline namespace N\n"
310                    "{\n"
311                    "\n"
312                    "int    i;\n"
313                    "}",
314                    CustomStyle));
315   EXPECT_EQ("export namespace N\n"
316             "{\n"
317             "\n"
318             "int i;\n"
319             "}",
320             format("export namespace N\n"
321                    "{\n"
322                    "\n"
323                    "int    i;\n"
324                    "}",
325                    CustomStyle));
326   EXPECT_EQ("namespace a\n"
327             "{\n"
328             "namespace b\n"
329             "{\n"
330             "\n"
331             "class AA {};\n"
332             "\n"
333             "} // namespace b\n"
334             "} // namespace a\n",
335             format("namespace a\n"
336                    "{\n"
337                    "namespace b\n"
338                    "{\n"
339                    "\n"
340                    "\n"
341                    "class AA {};\n"
342                    "\n"
343                    "\n"
344                    "}\n"
345                    "}\n",
346                    CustomStyle));
347   EXPECT_EQ("namespace A /* comment */\n"
348             "{\n"
349             "class B {}\n"
350             "} // namespace A",
351             format("namespace A /* comment */ { class B {} }", CustomStyle));
352   EXPECT_EQ("namespace A\n"
353             "{ /* comment */\n"
354             "class B {}\n"
355             "} // namespace A",
356             format("namespace A {/* comment */ class B {} }", CustomStyle));
357   EXPECT_EQ("namespace A\n"
358             "{ /* comment */\n"
359             "\n"
360             "class B {}\n"
361             "\n"
362             ""
363             "} // namespace A",
364             format("namespace A { /* comment */\n"
365                    "\n"
366                    "\n"
367                    "class B {}\n"
368                    "\n"
369                    "\n"
370                    "}",
371                    CustomStyle));
372   EXPECT_EQ("namespace A /* comment */\n"
373             "{\n"
374             "\n"
375             "class B {}\n"
376             "\n"
377             "} // namespace A",
378             format("namespace A/* comment */ {\n"
379                    "\n"
380                    "\n"
381                    "class B {}\n"
382                    "\n"
383                    "\n"
384                    "}",
385                    CustomStyle));
386 
387   // ...but do keep inlining and removing empty lines for non-block extern "C"
388   // functions.
389   verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle());
390   EXPECT_EQ("extern \"C\" int f() {\n"
391             "  int i = 42;\n"
392             "  return i;\n"
393             "}",
394             format("extern \"C\" int f() {\n"
395                    "\n"
396                    "  int i = 42;\n"
397                    "  return i;\n"
398                    "}",
399                    getGoogleStyle()));
400 
401   // Remove empty lines at the beginning and end of blocks.
402   EXPECT_EQ("void f() {\n"
403             "\n"
404             "  if (a) {\n"
405             "\n"
406             "    f();\n"
407             "  }\n"
408             "}",
409             format("void f() {\n"
410                    "\n"
411                    "  if (a) {\n"
412                    "\n"
413                    "    f();\n"
414                    "\n"
415                    "  }\n"
416                    "\n"
417                    "}",
418                    getLLVMStyle()));
419   EXPECT_EQ("void f() {\n"
420             "  if (a) {\n"
421             "    f();\n"
422             "  }\n"
423             "}",
424             format("void f() {\n"
425                    "\n"
426                    "  if (a) {\n"
427                    "\n"
428                    "    f();\n"
429                    "\n"
430                    "  }\n"
431                    "\n"
432                    "}",
433                    getGoogleStyle()));
434 
435   // Don't remove empty lines in more complex control statements.
436   EXPECT_EQ("void f() {\n"
437             "  if (a) {\n"
438             "    f();\n"
439             "\n"
440             "  } else if (b) {\n"
441             "    f();\n"
442             "  }\n"
443             "}",
444             format("void f() {\n"
445                    "  if (a) {\n"
446                    "    f();\n"
447                    "\n"
448                    "  } else if (b) {\n"
449                    "    f();\n"
450                    "\n"
451                    "  }\n"
452                    "\n"
453                    "}"));
454 
455   // Don't remove empty lines before namespace endings.
456   FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle();
457   LLVMWithNoNamespaceFix.FixNamespaceComments = false;
458   EXPECT_EQ("namespace {\n"
459             "int i;\n"
460             "\n"
461             "}",
462             format("namespace {\n"
463                    "int i;\n"
464                    "\n"
465                    "}",
466                    LLVMWithNoNamespaceFix));
467   EXPECT_EQ("namespace {\n"
468             "int i;\n"
469             "}",
470             format("namespace {\n"
471                    "int i;\n"
472                    "}",
473                    LLVMWithNoNamespaceFix));
474   EXPECT_EQ("namespace {\n"
475             "int i;\n"
476             "\n"
477             "};",
478             format("namespace {\n"
479                    "int i;\n"
480                    "\n"
481                    "};",
482                    LLVMWithNoNamespaceFix));
483   EXPECT_EQ("namespace {\n"
484             "int i;\n"
485             "};",
486             format("namespace {\n"
487                    "int i;\n"
488                    "};",
489                    LLVMWithNoNamespaceFix));
490   EXPECT_EQ("namespace {\n"
491             "int i;\n"
492             "\n"
493             "}",
494             format("namespace {\n"
495                    "int i;\n"
496                    "\n"
497                    "}"));
498   EXPECT_EQ("namespace {\n"
499             "int i;\n"
500             "\n"
501             "} // namespace",
502             format("namespace {\n"
503                    "int i;\n"
504                    "\n"
505                    "}  // namespace"));
506 
507   FormatStyle Style = getLLVMStyle();
508   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
509   Style.MaxEmptyLinesToKeep = 2;
510   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
511   Style.BraceWrapping.AfterClass = true;
512   Style.BraceWrapping.AfterFunction = true;
513   Style.KeepEmptyLinesAtTheStartOfBlocks = false;
514 
515   EXPECT_EQ("class Foo\n"
516             "{\n"
517             "  Foo() {}\n"
518             "\n"
519             "  void funk() {}\n"
520             "};",
521             format("class Foo\n"
522                    "{\n"
523                    "  Foo()\n"
524                    "  {\n"
525                    "  }\n"
526                    "\n"
527                    "  void funk() {}\n"
528                    "};",
529                    Style));
530 }
531 
532 TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) {
533   verifyFormat("x = (a) and (b);");
534   verifyFormat("x = (a) or (b);");
535   verifyFormat("x = (a) bitand (b);");
536   verifyFormat("x = (a) bitor (b);");
537   verifyFormat("x = (a) not_eq (b);");
538   verifyFormat("x = (a) and_eq (b);");
539   verifyFormat("x = (a) or_eq (b);");
540   verifyFormat("x = (a) xor (b);");
541 }
542 
543 TEST_F(FormatTest, RecognizesUnaryOperatorKeywords) {
544   verifyFormat("x = compl(a);");
545   verifyFormat("x = not(a);");
546   verifyFormat("x = bitand(a);");
547   // Unary operator must not be merged with the next identifier
548   verifyFormat("x = compl a;");
549   verifyFormat("x = not a;");
550   verifyFormat("x = bitand a;");
551 }
552 
553 //===----------------------------------------------------------------------===//
554 // Tests for control statements.
555 //===----------------------------------------------------------------------===//
556 
557 TEST_F(FormatTest, FormatIfWithoutCompoundStatement) {
558   verifyFormat("if (true)\n  f();\ng();");
559   verifyFormat("if (a)\n  if (b)\n    if (c)\n      g();\nh();");
560   verifyFormat("if (a)\n  if (b) {\n    f();\n  }\ng();");
561   verifyFormat("if constexpr (true)\n"
562                "  f();\ng();");
563   verifyFormat("if CONSTEXPR (true)\n"
564                "  f();\ng();");
565   verifyFormat("if constexpr (a)\n"
566                "  if constexpr (b)\n"
567                "    if constexpr (c)\n"
568                "      g();\n"
569                "h();");
570   verifyFormat("if CONSTEXPR (a)\n"
571                "  if CONSTEXPR (b)\n"
572                "    if CONSTEXPR (c)\n"
573                "      g();\n"
574                "h();");
575   verifyFormat("if constexpr (a)\n"
576                "  if constexpr (b) {\n"
577                "    f();\n"
578                "  }\n"
579                "g();");
580   verifyFormat("if CONSTEXPR (a)\n"
581                "  if CONSTEXPR (b) {\n"
582                "    f();\n"
583                "  }\n"
584                "g();");
585 
586   verifyFormat("if (a)\n"
587                "  g();");
588   verifyFormat("if (a) {\n"
589                "  g()\n"
590                "};");
591   verifyFormat("if (a)\n"
592                "  g();\n"
593                "else\n"
594                "  g();");
595   verifyFormat("if (a) {\n"
596                "  g();\n"
597                "} else\n"
598                "  g();");
599   verifyFormat("if (a)\n"
600                "  g();\n"
601                "else {\n"
602                "  g();\n"
603                "}");
604   verifyFormat("if (a) {\n"
605                "  g();\n"
606                "} else {\n"
607                "  g();\n"
608                "}");
609   verifyFormat("if (a)\n"
610                "  g();\n"
611                "else if (b)\n"
612                "  g();\n"
613                "else\n"
614                "  g();");
615   verifyFormat("if (a) {\n"
616                "  g();\n"
617                "} else if (b)\n"
618                "  g();\n"
619                "else\n"
620                "  g();");
621   verifyFormat("if (a)\n"
622                "  g();\n"
623                "else if (b) {\n"
624                "  g();\n"
625                "} else\n"
626                "  g();");
627   verifyFormat("if (a)\n"
628                "  g();\n"
629                "else if (b)\n"
630                "  g();\n"
631                "else {\n"
632                "  g();\n"
633                "}");
634   verifyFormat("if (a)\n"
635                "  g();\n"
636                "else if (b) {\n"
637                "  g();\n"
638                "} else {\n"
639                "  g();\n"
640                "}");
641   verifyFormat("if (a) {\n"
642                "  g();\n"
643                "} else if (b) {\n"
644                "  g();\n"
645                "} else {\n"
646                "  g();\n"
647                "}");
648 
649   FormatStyle AllowsMergedIf = getLLVMStyle();
650   AllowsMergedIf.IfMacros.push_back("MYIF");
651   AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left;
652   AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
653       FormatStyle::SIS_WithoutElse;
654   verifyFormat("if (a)\n"
655                "  // comment\n"
656                "  f();",
657                AllowsMergedIf);
658   verifyFormat("{\n"
659                "  if (a)\n"
660                "  label:\n"
661                "    f();\n"
662                "}",
663                AllowsMergedIf);
664   verifyFormat("#define A \\\n"
665                "  if (a)  \\\n"
666                "  label:  \\\n"
667                "    f()",
668                AllowsMergedIf);
669   verifyFormat("if (a)\n"
670                "  ;",
671                AllowsMergedIf);
672   verifyFormat("if (a)\n"
673                "  if (b) return;",
674                AllowsMergedIf);
675 
676   verifyFormat("if (a) // Can't merge this\n"
677                "  f();\n",
678                AllowsMergedIf);
679   verifyFormat("if (a) /* still don't merge */\n"
680                "  f();",
681                AllowsMergedIf);
682   verifyFormat("if (a) { // Never merge this\n"
683                "  f();\n"
684                "}",
685                AllowsMergedIf);
686   verifyFormat("if (a) { /* Never merge this */\n"
687                "  f();\n"
688                "}",
689                AllowsMergedIf);
690   verifyFormat("MYIF (a)\n"
691                "  // comment\n"
692                "  f();",
693                AllowsMergedIf);
694   verifyFormat("{\n"
695                "  MYIF (a)\n"
696                "  label:\n"
697                "    f();\n"
698                "}",
699                AllowsMergedIf);
700   verifyFormat("#define A  \\\n"
701                "  MYIF (a) \\\n"
702                "  label:   \\\n"
703                "    f()",
704                AllowsMergedIf);
705   verifyFormat("MYIF (a)\n"
706                "  ;",
707                AllowsMergedIf);
708   verifyFormat("MYIF (a)\n"
709                "  MYIF (b) return;",
710                AllowsMergedIf);
711 
712   verifyFormat("MYIF (a) // Can't merge this\n"
713                "  f();\n",
714                AllowsMergedIf);
715   verifyFormat("MYIF (a) /* still don't merge */\n"
716                "  f();",
717                AllowsMergedIf);
718   verifyFormat("MYIF (a) { // Never merge this\n"
719                "  f();\n"
720                "}",
721                AllowsMergedIf);
722   verifyFormat("MYIF (a) { /* Never merge this */\n"
723                "  f();\n"
724                "}",
725                AllowsMergedIf);
726 
727   AllowsMergedIf.ColumnLimit = 14;
728   // Where line-lengths matter, a 2-letter synonym that maintains line length.
729   // Not IF to avoid any confusion that IF is somehow special.
730   AllowsMergedIf.IfMacros.push_back("FI");
731   verifyFormat("if (a) return;", AllowsMergedIf);
732   verifyFormat("if (aaaaaaaaa)\n"
733                "  return;",
734                AllowsMergedIf);
735   verifyFormat("FI (a) return;", AllowsMergedIf);
736   verifyFormat("FI (aaaaaaaaa)\n"
737                "  return;",
738                AllowsMergedIf);
739 
740   AllowsMergedIf.ColumnLimit = 13;
741   verifyFormat("if (a)\n  return;", AllowsMergedIf);
742   verifyFormat("FI (a)\n  return;", AllowsMergedIf);
743 
744   FormatStyle AllowsMergedIfElse = getLLVMStyle();
745   AllowsMergedIfElse.IfMacros.push_back("MYIF");
746   AllowsMergedIfElse.AllowShortIfStatementsOnASingleLine =
747       FormatStyle::SIS_AllIfsAndElse;
748   verifyFormat("if (a)\n"
749                "  // comment\n"
750                "  f();\n"
751                "else\n"
752                "  // comment\n"
753                "  f();",
754                AllowsMergedIfElse);
755   verifyFormat("{\n"
756                "  if (a)\n"
757                "  label:\n"
758                "    f();\n"
759                "  else\n"
760                "  label:\n"
761                "    f();\n"
762                "}",
763                AllowsMergedIfElse);
764   verifyFormat("if (a)\n"
765                "  ;\n"
766                "else\n"
767                "  ;",
768                AllowsMergedIfElse);
769   verifyFormat("if (a) {\n"
770                "} else {\n"
771                "}",
772                AllowsMergedIfElse);
773   verifyFormat("if (a) return;\n"
774                "else if (b) return;\n"
775                "else return;",
776                AllowsMergedIfElse);
777   verifyFormat("if (a) {\n"
778                "} else return;",
779                AllowsMergedIfElse);
780   verifyFormat("if (a) {\n"
781                "} else if (b) return;\n"
782                "else return;",
783                AllowsMergedIfElse);
784   verifyFormat("if (a) return;\n"
785                "else if (b) {\n"
786                "} else return;",
787                AllowsMergedIfElse);
788   verifyFormat("if (a)\n"
789                "  if (b) return;\n"
790                "  else return;",
791                AllowsMergedIfElse);
792   verifyFormat("if constexpr (a)\n"
793                "  if constexpr (b) return;\n"
794                "  else if constexpr (c) return;\n"
795                "  else return;",
796                AllowsMergedIfElse);
797   verifyFormat("MYIF (a)\n"
798                "  // comment\n"
799                "  f();\n"
800                "else\n"
801                "  // comment\n"
802                "  f();",
803                AllowsMergedIfElse);
804   verifyFormat("{\n"
805                "  MYIF (a)\n"
806                "  label:\n"
807                "    f();\n"
808                "  else\n"
809                "  label:\n"
810                "    f();\n"
811                "}",
812                AllowsMergedIfElse);
813   verifyFormat("MYIF (a)\n"
814                "  ;\n"
815                "else\n"
816                "  ;",
817                AllowsMergedIfElse);
818   verifyFormat("MYIF (a) {\n"
819                "} else {\n"
820                "}",
821                AllowsMergedIfElse);
822   verifyFormat("MYIF (a) return;\n"
823                "else MYIF (b) return;\n"
824                "else return;",
825                AllowsMergedIfElse);
826   verifyFormat("MYIF (a) {\n"
827                "} else return;",
828                AllowsMergedIfElse);
829   verifyFormat("MYIF (a) {\n"
830                "} else MYIF (b) return;\n"
831                "else return;",
832                AllowsMergedIfElse);
833   verifyFormat("MYIF (a) return;\n"
834                "else MYIF (b) {\n"
835                "} else return;",
836                AllowsMergedIfElse);
837   verifyFormat("MYIF (a)\n"
838                "  MYIF (b) return;\n"
839                "  else return;",
840                AllowsMergedIfElse);
841   verifyFormat("MYIF constexpr (a)\n"
842                "  MYIF constexpr (b) return;\n"
843                "  else MYIF constexpr (c) return;\n"
844                "  else return;",
845                AllowsMergedIfElse);
846 }
847 
848 TEST_F(FormatTest, FormatIfWithoutCompoundStatementButElseWith) {
849   FormatStyle AllowsMergedIf = getLLVMStyle();
850   AllowsMergedIf.IfMacros.push_back("MYIF");
851   AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left;
852   AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
853       FormatStyle::SIS_WithoutElse;
854   verifyFormat("if (a)\n"
855                "  f();\n"
856                "else {\n"
857                "  g();\n"
858                "}",
859                AllowsMergedIf);
860   verifyFormat("if (a)\n"
861                "  f();\n"
862                "else\n"
863                "  g();\n",
864                AllowsMergedIf);
865 
866   verifyFormat("if (a) g();", AllowsMergedIf);
867   verifyFormat("if (a) {\n"
868                "  g()\n"
869                "};",
870                AllowsMergedIf);
871   verifyFormat("if (a)\n"
872                "  g();\n"
873                "else\n"
874                "  g();",
875                AllowsMergedIf);
876   verifyFormat("if (a) {\n"
877                "  g();\n"
878                "} else\n"
879                "  g();",
880                AllowsMergedIf);
881   verifyFormat("if (a)\n"
882                "  g();\n"
883                "else {\n"
884                "  g();\n"
885                "}",
886                AllowsMergedIf);
887   verifyFormat("if (a) {\n"
888                "  g();\n"
889                "} else {\n"
890                "  g();\n"
891                "}",
892                AllowsMergedIf);
893   verifyFormat("if (a)\n"
894                "  g();\n"
895                "else if (b)\n"
896                "  g();\n"
897                "else\n"
898                "  g();",
899                AllowsMergedIf);
900   verifyFormat("if (a) {\n"
901                "  g();\n"
902                "} else if (b)\n"
903                "  g();\n"
904                "else\n"
905                "  g();",
906                AllowsMergedIf);
907   verifyFormat("if (a)\n"
908                "  g();\n"
909                "else if (b) {\n"
910                "  g();\n"
911                "} else\n"
912                "  g();",
913                AllowsMergedIf);
914   verifyFormat("if (a)\n"
915                "  g();\n"
916                "else if (b)\n"
917                "  g();\n"
918                "else {\n"
919                "  g();\n"
920                "}",
921                AllowsMergedIf);
922   verifyFormat("if (a)\n"
923                "  g();\n"
924                "else if (b) {\n"
925                "  g();\n"
926                "} else {\n"
927                "  g();\n"
928                "}",
929                AllowsMergedIf);
930   verifyFormat("if (a) {\n"
931                "  g();\n"
932                "} else if (b) {\n"
933                "  g();\n"
934                "} else {\n"
935                "  g();\n"
936                "}",
937                AllowsMergedIf);
938   verifyFormat("MYIF (a)\n"
939                "  f();\n"
940                "else {\n"
941                "  g();\n"
942                "}",
943                AllowsMergedIf);
944   verifyFormat("MYIF (a)\n"
945                "  f();\n"
946                "else\n"
947                "  g();\n",
948                AllowsMergedIf);
949 
950   verifyFormat("MYIF (a) g();", AllowsMergedIf);
951   verifyFormat("MYIF (a) {\n"
952                "  g()\n"
953                "};",
954                AllowsMergedIf);
955   verifyFormat("MYIF (a)\n"
956                "  g();\n"
957                "else\n"
958                "  g();",
959                AllowsMergedIf);
960   verifyFormat("MYIF (a) {\n"
961                "  g();\n"
962                "} else\n"
963                "  g();",
964                AllowsMergedIf);
965   verifyFormat("MYIF (a)\n"
966                "  g();\n"
967                "else {\n"
968                "  g();\n"
969                "}",
970                AllowsMergedIf);
971   verifyFormat("MYIF (a) {\n"
972                "  g();\n"
973                "} else {\n"
974                "  g();\n"
975                "}",
976                AllowsMergedIf);
977   verifyFormat("MYIF (a)\n"
978                "  g();\n"
979                "else MYIF (b)\n"
980                "  g();\n"
981                "else\n"
982                "  g();",
983                AllowsMergedIf);
984   verifyFormat("MYIF (a)\n"
985                "  g();\n"
986                "else if (b)\n"
987                "  g();\n"
988                "else\n"
989                "  g();",
990                AllowsMergedIf);
991   verifyFormat("MYIF (a) {\n"
992                "  g();\n"
993                "} else MYIF (b)\n"
994                "  g();\n"
995                "else\n"
996                "  g();",
997                AllowsMergedIf);
998   verifyFormat("MYIF (a) {\n"
999                "  g();\n"
1000                "} else if (b)\n"
1001                "  g();\n"
1002                "else\n"
1003                "  g();",
1004                AllowsMergedIf);
1005   verifyFormat("MYIF (a)\n"
1006                "  g();\n"
1007                "else MYIF (b) {\n"
1008                "  g();\n"
1009                "} else\n"
1010                "  g();",
1011                AllowsMergedIf);
1012   verifyFormat("MYIF (a)\n"
1013                "  g();\n"
1014                "else if (b) {\n"
1015                "  g();\n"
1016                "} else\n"
1017                "  g();",
1018                AllowsMergedIf);
1019   verifyFormat("MYIF (a)\n"
1020                "  g();\n"
1021                "else MYIF (b)\n"
1022                "  g();\n"
1023                "else {\n"
1024                "  g();\n"
1025                "}",
1026                AllowsMergedIf);
1027   verifyFormat("MYIF (a)\n"
1028                "  g();\n"
1029                "else if (b)\n"
1030                "  g();\n"
1031                "else {\n"
1032                "  g();\n"
1033                "}",
1034                AllowsMergedIf);
1035   verifyFormat("MYIF (a)\n"
1036                "  g();\n"
1037                "else MYIF (b) {\n"
1038                "  g();\n"
1039                "} else {\n"
1040                "  g();\n"
1041                "}",
1042                AllowsMergedIf);
1043   verifyFormat("MYIF (a)\n"
1044                "  g();\n"
1045                "else if (b) {\n"
1046                "  g();\n"
1047                "} else {\n"
1048                "  g();\n"
1049                "}",
1050                AllowsMergedIf);
1051   verifyFormat("MYIF (a) {\n"
1052                "  g();\n"
1053                "} else MYIF (b) {\n"
1054                "  g();\n"
1055                "} else {\n"
1056                "  g();\n"
1057                "}",
1058                AllowsMergedIf);
1059   verifyFormat("MYIF (a) {\n"
1060                "  g();\n"
1061                "} else if (b) {\n"
1062                "  g();\n"
1063                "} else {\n"
1064                "  g();\n"
1065                "}",
1066                AllowsMergedIf);
1067 
1068   AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
1069       FormatStyle::SIS_OnlyFirstIf;
1070 
1071   verifyFormat("if (a) f();\n"
1072                "else {\n"
1073                "  g();\n"
1074                "}",
1075                AllowsMergedIf);
1076   verifyFormat("if (a) f();\n"
1077                "else {\n"
1078                "  if (a) f();\n"
1079                "  else {\n"
1080                "    g();\n"
1081                "  }\n"
1082                "  g();\n"
1083                "}",
1084                AllowsMergedIf);
1085 
1086   verifyFormat("if (a) g();", AllowsMergedIf);
1087   verifyFormat("if (a) {\n"
1088                "  g()\n"
1089                "};",
1090                AllowsMergedIf);
1091   verifyFormat("if (a) g();\n"
1092                "else\n"
1093                "  g();",
1094                AllowsMergedIf);
1095   verifyFormat("if (a) {\n"
1096                "  g();\n"
1097                "} else\n"
1098                "  g();",
1099                AllowsMergedIf);
1100   verifyFormat("if (a) g();\n"
1101                "else {\n"
1102                "  g();\n"
1103                "}",
1104                AllowsMergedIf);
1105   verifyFormat("if (a) {\n"
1106                "  g();\n"
1107                "} else {\n"
1108                "  g();\n"
1109                "}",
1110                AllowsMergedIf);
1111   verifyFormat("if (a) g();\n"
1112                "else if (b)\n"
1113                "  g();\n"
1114                "else\n"
1115                "  g();",
1116                AllowsMergedIf);
1117   verifyFormat("if (a) {\n"
1118                "  g();\n"
1119                "} else if (b)\n"
1120                "  g();\n"
1121                "else\n"
1122                "  g();",
1123                AllowsMergedIf);
1124   verifyFormat("if (a) g();\n"
1125                "else if (b) {\n"
1126                "  g();\n"
1127                "} else\n"
1128                "  g();",
1129                AllowsMergedIf);
1130   verifyFormat("if (a) g();\n"
1131                "else if (b)\n"
1132                "  g();\n"
1133                "else {\n"
1134                "  g();\n"
1135                "}",
1136                AllowsMergedIf);
1137   verifyFormat("if (a) g();\n"
1138                "else if (b) {\n"
1139                "  g();\n"
1140                "} else {\n"
1141                "  g();\n"
1142                "}",
1143                AllowsMergedIf);
1144   verifyFormat("if (a) {\n"
1145                "  g();\n"
1146                "} else if (b) {\n"
1147                "  g();\n"
1148                "} else {\n"
1149                "  g();\n"
1150                "}",
1151                AllowsMergedIf);
1152   verifyFormat("MYIF (a) f();\n"
1153                "else {\n"
1154                "  g();\n"
1155                "}",
1156                AllowsMergedIf);
1157   verifyFormat("MYIF (a) f();\n"
1158                "else {\n"
1159                "  if (a) f();\n"
1160                "  else {\n"
1161                "    g();\n"
1162                "  }\n"
1163                "  g();\n"
1164                "}",
1165                AllowsMergedIf);
1166 
1167   verifyFormat("MYIF (a) g();", AllowsMergedIf);
1168   verifyFormat("MYIF (a) {\n"
1169                "  g()\n"
1170                "};",
1171                AllowsMergedIf);
1172   verifyFormat("MYIF (a) g();\n"
1173                "else\n"
1174                "  g();",
1175                AllowsMergedIf);
1176   verifyFormat("MYIF (a) {\n"
1177                "  g();\n"
1178                "} else\n"
1179                "  g();",
1180                AllowsMergedIf);
1181   verifyFormat("MYIF (a) g();\n"
1182                "else {\n"
1183                "  g();\n"
1184                "}",
1185                AllowsMergedIf);
1186   verifyFormat("MYIF (a) {\n"
1187                "  g();\n"
1188                "} else {\n"
1189                "  g();\n"
1190                "}",
1191                AllowsMergedIf);
1192   verifyFormat("MYIF (a) g();\n"
1193                "else MYIF (b)\n"
1194                "  g();\n"
1195                "else\n"
1196                "  g();",
1197                AllowsMergedIf);
1198   verifyFormat("MYIF (a) g();\n"
1199                "else if (b)\n"
1200                "  g();\n"
1201                "else\n"
1202                "  g();",
1203                AllowsMergedIf);
1204   verifyFormat("MYIF (a) {\n"
1205                "  g();\n"
1206                "} else MYIF (b)\n"
1207                "  g();\n"
1208                "else\n"
1209                "  g();",
1210                AllowsMergedIf);
1211   verifyFormat("MYIF (a) {\n"
1212                "  g();\n"
1213                "} else if (b)\n"
1214                "  g();\n"
1215                "else\n"
1216                "  g();",
1217                AllowsMergedIf);
1218   verifyFormat("MYIF (a) g();\n"
1219                "else MYIF (b) {\n"
1220                "  g();\n"
1221                "} else\n"
1222                "  g();",
1223                AllowsMergedIf);
1224   verifyFormat("MYIF (a) g();\n"
1225                "else if (b) {\n"
1226                "  g();\n"
1227                "} else\n"
1228                "  g();",
1229                AllowsMergedIf);
1230   verifyFormat("MYIF (a) g();\n"
1231                "else MYIF (b)\n"
1232                "  g();\n"
1233                "else {\n"
1234                "  g();\n"
1235                "}",
1236                AllowsMergedIf);
1237   verifyFormat("MYIF (a) g();\n"
1238                "else if (b)\n"
1239                "  g();\n"
1240                "else {\n"
1241                "  g();\n"
1242                "}",
1243                AllowsMergedIf);
1244   verifyFormat("MYIF (a) g();\n"
1245                "else MYIF (b) {\n"
1246                "  g();\n"
1247                "} else {\n"
1248                "  g();\n"
1249                "}",
1250                AllowsMergedIf);
1251   verifyFormat("MYIF (a) g();\n"
1252                "else if (b) {\n"
1253                "  g();\n"
1254                "} else {\n"
1255                "  g();\n"
1256                "}",
1257                AllowsMergedIf);
1258   verifyFormat("MYIF (a) {\n"
1259                "  g();\n"
1260                "} else MYIF (b) {\n"
1261                "  g();\n"
1262                "} else {\n"
1263                "  g();\n"
1264                "}",
1265                AllowsMergedIf);
1266   verifyFormat("MYIF (a) {\n"
1267                "  g();\n"
1268                "} else if (b) {\n"
1269                "  g();\n"
1270                "} else {\n"
1271                "  g();\n"
1272                "}",
1273                AllowsMergedIf);
1274 
1275   AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
1276       FormatStyle::SIS_AllIfsAndElse;
1277 
1278   verifyFormat("if (a) f();\n"
1279                "else {\n"
1280                "  g();\n"
1281                "}",
1282                AllowsMergedIf);
1283   verifyFormat("if (a) f();\n"
1284                "else {\n"
1285                "  if (a) f();\n"
1286                "  else {\n"
1287                "    g();\n"
1288                "  }\n"
1289                "  g();\n"
1290                "}",
1291                AllowsMergedIf);
1292 
1293   verifyFormat("if (a) g();", AllowsMergedIf);
1294   verifyFormat("if (a) {\n"
1295                "  g()\n"
1296                "};",
1297                AllowsMergedIf);
1298   verifyFormat("if (a) g();\n"
1299                "else g();",
1300                AllowsMergedIf);
1301   verifyFormat("if (a) {\n"
1302                "  g();\n"
1303                "} else g();",
1304                AllowsMergedIf);
1305   verifyFormat("if (a) g();\n"
1306                "else {\n"
1307                "  g();\n"
1308                "}",
1309                AllowsMergedIf);
1310   verifyFormat("if (a) {\n"
1311                "  g();\n"
1312                "} else {\n"
1313                "  g();\n"
1314                "}",
1315                AllowsMergedIf);
1316   verifyFormat("if (a) g();\n"
1317                "else if (b) g();\n"
1318                "else g();",
1319                AllowsMergedIf);
1320   verifyFormat("if (a) {\n"
1321                "  g();\n"
1322                "} else if (b) g();\n"
1323                "else g();",
1324                AllowsMergedIf);
1325   verifyFormat("if (a) g();\n"
1326                "else if (b) {\n"
1327                "  g();\n"
1328                "} else g();",
1329                AllowsMergedIf);
1330   verifyFormat("if (a) g();\n"
1331                "else if (b) g();\n"
1332                "else {\n"
1333                "  g();\n"
1334                "}",
1335                AllowsMergedIf);
1336   verifyFormat("if (a) g();\n"
1337                "else if (b) {\n"
1338                "  g();\n"
1339                "} else {\n"
1340                "  g();\n"
1341                "}",
1342                AllowsMergedIf);
1343   verifyFormat("if (a) {\n"
1344                "  g();\n"
1345                "} else if (b) {\n"
1346                "  g();\n"
1347                "} else {\n"
1348                "  g();\n"
1349                "}",
1350                AllowsMergedIf);
1351   verifyFormat("MYIF (a) f();\n"
1352                "else {\n"
1353                "  g();\n"
1354                "}",
1355                AllowsMergedIf);
1356   verifyFormat("MYIF (a) f();\n"
1357                "else {\n"
1358                "  if (a) f();\n"
1359                "  else {\n"
1360                "    g();\n"
1361                "  }\n"
1362                "  g();\n"
1363                "}",
1364                AllowsMergedIf);
1365 
1366   verifyFormat("MYIF (a) g();", AllowsMergedIf);
1367   verifyFormat("MYIF (a) {\n"
1368                "  g()\n"
1369                "};",
1370                AllowsMergedIf);
1371   verifyFormat("MYIF (a) g();\n"
1372                "else g();",
1373                AllowsMergedIf);
1374   verifyFormat("MYIF (a) {\n"
1375                "  g();\n"
1376                "} else g();",
1377                AllowsMergedIf);
1378   verifyFormat("MYIF (a) g();\n"
1379                "else {\n"
1380                "  g();\n"
1381                "}",
1382                AllowsMergedIf);
1383   verifyFormat("MYIF (a) {\n"
1384                "  g();\n"
1385                "} else {\n"
1386                "  g();\n"
1387                "}",
1388                AllowsMergedIf);
1389   verifyFormat("MYIF (a) g();\n"
1390                "else MYIF (b) g();\n"
1391                "else g();",
1392                AllowsMergedIf);
1393   verifyFormat("MYIF (a) g();\n"
1394                "else if (b) g();\n"
1395                "else g();",
1396                AllowsMergedIf);
1397   verifyFormat("MYIF (a) {\n"
1398                "  g();\n"
1399                "} else MYIF (b) g();\n"
1400                "else g();",
1401                AllowsMergedIf);
1402   verifyFormat("MYIF (a) {\n"
1403                "  g();\n"
1404                "} else if (b) g();\n"
1405                "else g();",
1406                AllowsMergedIf);
1407   verifyFormat("MYIF (a) g();\n"
1408                "else MYIF (b) {\n"
1409                "  g();\n"
1410                "} else g();",
1411                AllowsMergedIf);
1412   verifyFormat("MYIF (a) g();\n"
1413                "else if (b) {\n"
1414                "  g();\n"
1415                "} else g();",
1416                AllowsMergedIf);
1417   verifyFormat("MYIF (a) g();\n"
1418                "else MYIF (b) g();\n"
1419                "else {\n"
1420                "  g();\n"
1421                "}",
1422                AllowsMergedIf);
1423   verifyFormat("MYIF (a) g();\n"
1424                "else if (b) g();\n"
1425                "else {\n"
1426                "  g();\n"
1427                "}",
1428                AllowsMergedIf);
1429   verifyFormat("MYIF (a) g();\n"
1430                "else MYIF (b) {\n"
1431                "  g();\n"
1432                "} else {\n"
1433                "  g();\n"
1434                "}",
1435                AllowsMergedIf);
1436   verifyFormat("MYIF (a) g();\n"
1437                "else if (b) {\n"
1438                "  g();\n"
1439                "} else {\n"
1440                "  g();\n"
1441                "}",
1442                AllowsMergedIf);
1443   verifyFormat("MYIF (a) {\n"
1444                "  g();\n"
1445                "} else MYIF (b) {\n"
1446                "  g();\n"
1447                "} else {\n"
1448                "  g();\n"
1449                "}",
1450                AllowsMergedIf);
1451   verifyFormat("MYIF (a) {\n"
1452                "  g();\n"
1453                "} else if (b) {\n"
1454                "  g();\n"
1455                "} else {\n"
1456                "  g();\n"
1457                "}",
1458                AllowsMergedIf);
1459 }
1460 
1461 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
1462   FormatStyle AllowsMergedLoops = getLLVMStyle();
1463   AllowsMergedLoops.AllowShortLoopsOnASingleLine = true;
1464   verifyFormat("while (true) continue;", AllowsMergedLoops);
1465   verifyFormat("for (;;) continue;", AllowsMergedLoops);
1466   verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops);
1467   verifyFormat("BOOST_FOREACH (int &v, vec) v *= 2;", AllowsMergedLoops);
1468   verifyFormat("while (true)\n"
1469                "  ;",
1470                AllowsMergedLoops);
1471   verifyFormat("for (;;)\n"
1472                "  ;",
1473                AllowsMergedLoops);
1474   verifyFormat("for (;;)\n"
1475                "  for (;;) continue;",
1476                AllowsMergedLoops);
1477   verifyFormat("for (;;)\n"
1478                "  while (true) continue;",
1479                AllowsMergedLoops);
1480   verifyFormat("while (true)\n"
1481                "  for (;;) continue;",
1482                AllowsMergedLoops);
1483   verifyFormat("BOOST_FOREACH (int &v, vec)\n"
1484                "  for (;;) continue;",
1485                AllowsMergedLoops);
1486   verifyFormat("for (;;)\n"
1487                "  BOOST_FOREACH (int &v, vec) continue;",
1488                AllowsMergedLoops);
1489   verifyFormat("for (;;) // Can't merge this\n"
1490                "  continue;",
1491                AllowsMergedLoops);
1492   verifyFormat("for (;;) /* still don't merge */\n"
1493                "  continue;",
1494                AllowsMergedLoops);
1495   verifyFormat("do a++;\n"
1496                "while (true);",
1497                AllowsMergedLoops);
1498   verifyFormat("do /* Don't merge */\n"
1499                "  a++;\n"
1500                "while (true);",
1501                AllowsMergedLoops);
1502   verifyFormat("do // Don't merge\n"
1503                "  a++;\n"
1504                "while (true);",
1505                AllowsMergedLoops);
1506   verifyFormat("do\n"
1507                "  // Don't merge\n"
1508                "  a++;\n"
1509                "while (true);",
1510                AllowsMergedLoops);
1511   // Without braces labels are interpreted differently.
1512   verifyFormat("{\n"
1513                "  do\n"
1514                "  label:\n"
1515                "    a++;\n"
1516                "  while (true);\n"
1517                "}",
1518                AllowsMergedLoops);
1519 }
1520 
1521 TEST_F(FormatTest, FormatShortBracedStatements) {
1522   FormatStyle AllowSimpleBracedStatements = getLLVMStyle();
1523   EXPECT_EQ(AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine, false);
1524   EXPECT_EQ(AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine,
1525             FormatStyle::SIS_Never);
1526   EXPECT_EQ(AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine, false);
1527   EXPECT_EQ(AllowSimpleBracedStatements.BraceWrapping.AfterFunction, false);
1528   verifyFormat("for (;;) {\n"
1529                "  f();\n"
1530                "}");
1531   verifyFormat("/*comment*/ for (;;) {\n"
1532                "  f();\n"
1533                "}");
1534   verifyFormat("BOOST_FOREACH (int v, vec) {\n"
1535                "  f();\n"
1536                "}");
1537   verifyFormat("/*comment*/ BOOST_FOREACH (int v, vec) {\n"
1538                "  f();\n"
1539                "}");
1540   verifyFormat("while (true) {\n"
1541                "  f();\n"
1542                "}");
1543   verifyFormat("/*comment*/ while (true) {\n"
1544                "  f();\n"
1545                "}");
1546   verifyFormat("if (true) {\n"
1547                "  f();\n"
1548                "}");
1549   verifyFormat("/*comment*/ if (true) {\n"
1550                "  f();\n"
1551                "}");
1552 
1553   AllowSimpleBracedStatements.IfMacros.push_back("MYIF");
1554   // Where line-lengths matter, a 2-letter synonym that maintains line length.
1555   // Not IF to avoid any confusion that IF is somehow special.
1556   AllowSimpleBracedStatements.IfMacros.push_back("FI");
1557   AllowSimpleBracedStatements.ColumnLimit = 40;
1558   AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine =
1559       FormatStyle::SBS_Always;
1560 
1561   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
1562       FormatStyle::SIS_WithoutElse;
1563   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
1564 
1565   AllowSimpleBracedStatements.BreakBeforeBraces = FormatStyle::BS_Custom;
1566   AllowSimpleBracedStatements.BraceWrapping.AfterFunction = true;
1567   AllowSimpleBracedStatements.BraceWrapping.SplitEmptyRecord = false;
1568 
1569   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
1570   verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements);
1571   verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements);
1572   verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements);
1573   verifyFormat("MYIF constexpr (true) {}", AllowSimpleBracedStatements);
1574   verifyFormat("MYIF CONSTEXPR (true) {}", AllowSimpleBracedStatements);
1575   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
1576   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
1577   verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
1578   verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements);
1579   verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements);
1580   verifyFormat("MYIF (true) { f(); }", AllowSimpleBracedStatements);
1581   verifyFormat("MYIF constexpr (true) { f(); }", AllowSimpleBracedStatements);
1582   verifyFormat("MYIF CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements);
1583   verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
1584   verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
1585   verifyFormat("if (true) { fffffffffffffffffffffff(); }",
1586                AllowSimpleBracedStatements);
1587   verifyFormat("if (true) {\n"
1588                "  ffffffffffffffffffffffff();\n"
1589                "}",
1590                AllowSimpleBracedStatements);
1591   verifyFormat("if (true) {\n"
1592                "  ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
1593                "}",
1594                AllowSimpleBracedStatements);
1595   verifyFormat("if (true) { //\n"
1596                "  f();\n"
1597                "}",
1598                AllowSimpleBracedStatements);
1599   verifyFormat("if (true) {\n"
1600                "  f();\n"
1601                "  f();\n"
1602                "}",
1603                AllowSimpleBracedStatements);
1604   verifyFormat("if (true) {\n"
1605                "  f();\n"
1606                "} else {\n"
1607                "  f();\n"
1608                "}",
1609                AllowSimpleBracedStatements);
1610   verifyFormat("FI (true) { fffffffffffffffffffffff(); }",
1611                AllowSimpleBracedStatements);
1612   verifyFormat("MYIF (true) {\n"
1613                "  ffffffffffffffffffffffff();\n"
1614                "}",
1615                AllowSimpleBracedStatements);
1616   verifyFormat("MYIF (true) {\n"
1617                "  ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
1618                "}",
1619                AllowSimpleBracedStatements);
1620   verifyFormat("MYIF (true) { //\n"
1621                "  f();\n"
1622                "}",
1623                AllowSimpleBracedStatements);
1624   verifyFormat("MYIF (true) {\n"
1625                "  f();\n"
1626                "  f();\n"
1627                "}",
1628                AllowSimpleBracedStatements);
1629   verifyFormat("MYIF (true) {\n"
1630                "  f();\n"
1631                "} else {\n"
1632                "  f();\n"
1633                "}",
1634                AllowSimpleBracedStatements);
1635 
1636   verifyFormat("struct A2 {\n"
1637                "  int X;\n"
1638                "};",
1639                AllowSimpleBracedStatements);
1640   verifyFormat("typedef struct A2 {\n"
1641                "  int X;\n"
1642                "} A2_t;",
1643                AllowSimpleBracedStatements);
1644   verifyFormat("template <int> struct A2 {\n"
1645                "  struct B {};\n"
1646                "};",
1647                AllowSimpleBracedStatements);
1648 
1649   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
1650       FormatStyle::SIS_Never;
1651   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
1652   verifyFormat("if (true) {\n"
1653                "  f();\n"
1654                "}",
1655                AllowSimpleBracedStatements);
1656   verifyFormat("if (true) {\n"
1657                "  f();\n"
1658                "} else {\n"
1659                "  f();\n"
1660                "}",
1661                AllowSimpleBracedStatements);
1662   verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements);
1663   verifyFormat("MYIF (true) {\n"
1664                "  f();\n"
1665                "}",
1666                AllowSimpleBracedStatements);
1667   verifyFormat("MYIF (true) {\n"
1668                "  f();\n"
1669                "} else {\n"
1670                "  f();\n"
1671                "}",
1672                AllowSimpleBracedStatements);
1673 
1674   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
1675   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
1676   verifyFormat("while (true) {\n"
1677                "  f();\n"
1678                "}",
1679                AllowSimpleBracedStatements);
1680   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
1681   verifyFormat("for (;;) {\n"
1682                "  f();\n"
1683                "}",
1684                AllowSimpleBracedStatements);
1685   verifyFormat("BOOST_FOREACH (int v, vec) {}", AllowSimpleBracedStatements);
1686   verifyFormat("BOOST_FOREACH (int v, vec) {\n"
1687                "  f();\n"
1688                "}",
1689                AllowSimpleBracedStatements);
1690 
1691   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
1692       FormatStyle::SIS_WithoutElse;
1693   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
1694   AllowSimpleBracedStatements.BraceWrapping.AfterControlStatement =
1695       FormatStyle::BWACS_Always;
1696 
1697   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
1698   verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements);
1699   verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements);
1700   verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements);
1701   verifyFormat("MYIF constexpr (true) {}", AllowSimpleBracedStatements);
1702   verifyFormat("MYIF CONSTEXPR (true) {}", AllowSimpleBracedStatements);
1703   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
1704   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
1705   verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
1706   verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements);
1707   verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements);
1708   verifyFormat("MYIF (true) { f(); }", AllowSimpleBracedStatements);
1709   verifyFormat("MYIF constexpr (true) { f(); }", AllowSimpleBracedStatements);
1710   verifyFormat("MYIF CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements);
1711   verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
1712   verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
1713   verifyFormat("if (true) { fffffffffffffffffffffff(); }",
1714                AllowSimpleBracedStatements);
1715   verifyFormat("if (true)\n"
1716                "{\n"
1717                "  ffffffffffffffffffffffff();\n"
1718                "}",
1719                AllowSimpleBracedStatements);
1720   verifyFormat("if (true)\n"
1721                "{\n"
1722                "  ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
1723                "}",
1724                AllowSimpleBracedStatements);
1725   verifyFormat("if (true)\n"
1726                "{ //\n"
1727                "  f();\n"
1728                "}",
1729                AllowSimpleBracedStatements);
1730   verifyFormat("if (true)\n"
1731                "{\n"
1732                "  f();\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("FI (true) { fffffffffffffffffffffff(); }",
1745                AllowSimpleBracedStatements);
1746   verifyFormat("MYIF (true)\n"
1747                "{\n"
1748                "  ffffffffffffffffffffffff();\n"
1749                "}",
1750                AllowSimpleBracedStatements);
1751   verifyFormat("MYIF (true)\n"
1752                "{\n"
1753                "  ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
1754                "}",
1755                AllowSimpleBracedStatements);
1756   verifyFormat("MYIF (true)\n"
1757                "{ //\n"
1758                "  f();\n"
1759                "}",
1760                AllowSimpleBracedStatements);
1761   verifyFormat("MYIF (true)\n"
1762                "{\n"
1763                "  f();\n"
1764                "  f();\n"
1765                "}",
1766                AllowSimpleBracedStatements);
1767   verifyFormat("MYIF (true)\n"
1768                "{\n"
1769                "  f();\n"
1770                "} else\n"
1771                "{\n"
1772                "  f();\n"
1773                "}",
1774                AllowSimpleBracedStatements);
1775 
1776   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
1777       FormatStyle::SIS_Never;
1778   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
1779   verifyFormat("if (true)\n"
1780                "{\n"
1781                "  f();\n"
1782                "}",
1783                AllowSimpleBracedStatements);
1784   verifyFormat("if (true)\n"
1785                "{\n"
1786                "  f();\n"
1787                "} else\n"
1788                "{\n"
1789                "  f();\n"
1790                "}",
1791                AllowSimpleBracedStatements);
1792   verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements);
1793   verifyFormat("MYIF (true)\n"
1794                "{\n"
1795                "  f();\n"
1796                "}",
1797                AllowSimpleBracedStatements);
1798   verifyFormat("MYIF (true)\n"
1799                "{\n"
1800                "  f();\n"
1801                "} else\n"
1802                "{\n"
1803                "  f();\n"
1804                "}",
1805                AllowSimpleBracedStatements);
1806 
1807   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
1808   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
1809   verifyFormat("while (true)\n"
1810                "{\n"
1811                "  f();\n"
1812                "}",
1813                AllowSimpleBracedStatements);
1814   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
1815   verifyFormat("for (;;)\n"
1816                "{\n"
1817                "  f();\n"
1818                "}",
1819                AllowSimpleBracedStatements);
1820   verifyFormat("BOOST_FOREACH (int v, vec) {}", AllowSimpleBracedStatements);
1821   verifyFormat("BOOST_FOREACH (int v, vec)\n"
1822                "{\n"
1823                "  f();\n"
1824                "}",
1825                AllowSimpleBracedStatements);
1826 }
1827 
1828 TEST_F(FormatTest, UnderstandsMacros) {
1829   verifyFormat("#define A (parentheses)");
1830   verifyFormat("/* comment */ #define A (parentheses)");
1831   verifyFormat("/* comment */ /* another comment */ #define A (parentheses)");
1832   // Even the partial code should never be merged.
1833   EXPECT_EQ("/* comment */ #define A (parentheses)\n"
1834             "#",
1835             format("/* comment */ #define A (parentheses)\n"
1836                    "#"));
1837   verifyFormat("/* comment */ #define A (parentheses)\n"
1838                "#\n");
1839   verifyFormat("/* comment */ #define A (parentheses)\n"
1840                "#define B (parentheses)");
1841   verifyFormat("#define true ((int)1)");
1842   verifyFormat("#define and(x)");
1843   verifyFormat("#define if(x) x");
1844   verifyFormat("#define return(x) (x)");
1845   verifyFormat("#define while(x) for (; x;)");
1846   verifyFormat("#define xor(x) (^(x))");
1847   verifyFormat("#define __except(x)");
1848   verifyFormat("#define __try(x)");
1849 
1850   FormatStyle Style = getLLVMStyle();
1851   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
1852   Style.BraceWrapping.AfterFunction = true;
1853   // Test that a macro definition never gets merged with the following
1854   // definition.
1855   // FIXME: The AAA macro definition probably should not be split into 3 lines.
1856   verifyFormat("#define AAA                                                    "
1857                "                \\\n"
1858                "  N                                                            "
1859                "                \\\n"
1860                "  {\n"
1861                "#define BBB }\n",
1862                Style);
1863   // verifyFormat("#define AAA N { //\n", Style);
1864 }
1865 
1866 TEST_F(FormatTest, ShortBlocksInMacrosDontMergeWithCodeAfterMacro) {
1867   FormatStyle Style = getLLVMStyleWithColumns(60);
1868   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
1869   Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse;
1870   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
1871   EXPECT_EQ("#define A                                                  \\\n"
1872             "  if (HANDLEwernufrnuLwrmviferuvnierv)                     \\\n"
1873             "  {                                                        \\\n"
1874             "    RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier;               \\\n"
1875             "  }\n"
1876             "X;",
1877             format("#define A \\\n"
1878                    "   if (HANDLEwernufrnuLwrmviferuvnierv) { \\\n"
1879                    "      RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n"
1880                    "   }\n"
1881                    "X;",
1882                    Style));
1883 }
1884 
1885 TEST_F(FormatTest, ParseIfElse) {
1886   verifyFormat("if (true)\n"
1887                "  if (true)\n"
1888                "    if (true)\n"
1889                "      f();\n"
1890                "    else\n"
1891                "      g();\n"
1892                "  else\n"
1893                "    h();\n"
1894                "else\n"
1895                "  i();");
1896   verifyFormat("if (true)\n"
1897                "  if (true)\n"
1898                "    if (true) {\n"
1899                "      if (true)\n"
1900                "        f();\n"
1901                "    } else {\n"
1902                "      g();\n"
1903                "    }\n"
1904                "  else\n"
1905                "    h();\n"
1906                "else {\n"
1907                "  i();\n"
1908                "}");
1909   verifyFormat("if (true)\n"
1910                "  if constexpr (true)\n"
1911                "    if (true) {\n"
1912                "      if constexpr (true)\n"
1913                "        f();\n"
1914                "    } else {\n"
1915                "      g();\n"
1916                "    }\n"
1917                "  else\n"
1918                "    h();\n"
1919                "else {\n"
1920                "  i();\n"
1921                "}");
1922   verifyFormat("if (true)\n"
1923                "  if CONSTEXPR (true)\n"
1924                "    if (true) {\n"
1925                "      if CONSTEXPR (true)\n"
1926                "        f();\n"
1927                "    } else {\n"
1928                "      g();\n"
1929                "    }\n"
1930                "  else\n"
1931                "    h();\n"
1932                "else {\n"
1933                "  i();\n"
1934                "}");
1935   verifyFormat("void f() {\n"
1936                "  if (a) {\n"
1937                "  } else {\n"
1938                "  }\n"
1939                "}");
1940 }
1941 
1942 TEST_F(FormatTest, ElseIf) {
1943   verifyFormat("if (a) {\n} else if (b) {\n}");
1944   verifyFormat("if (a)\n"
1945                "  f();\n"
1946                "else if (b)\n"
1947                "  g();\n"
1948                "else\n"
1949                "  h();");
1950   verifyFormat("if (a)\n"
1951                "  f();\n"
1952                "else // comment\n"
1953                "  if (b) {\n"
1954                "    g();\n"
1955                "    h();\n"
1956                "  }");
1957   verifyFormat("if constexpr (a)\n"
1958                "  f();\n"
1959                "else if constexpr (b)\n"
1960                "  g();\n"
1961                "else\n"
1962                "  h();");
1963   verifyFormat("if CONSTEXPR (a)\n"
1964                "  f();\n"
1965                "else if CONSTEXPR (b)\n"
1966                "  g();\n"
1967                "else\n"
1968                "  h();");
1969   verifyFormat("if (a) {\n"
1970                "  f();\n"
1971                "}\n"
1972                "// or else ..\n"
1973                "else {\n"
1974                "  g()\n"
1975                "}");
1976 
1977   verifyFormat("if (a) {\n"
1978                "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1979                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
1980                "}");
1981   verifyFormat("if (a) {\n"
1982                "} else if constexpr (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1983                "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
1984                "}");
1985   verifyFormat("if (a) {\n"
1986                "} else if CONSTEXPR (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1987                "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
1988                "}");
1989   verifyFormat("if (a) {\n"
1990                "} else if (\n"
1991                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
1992                "}",
1993                getLLVMStyleWithColumns(62));
1994   verifyFormat("if (a) {\n"
1995                "} else if constexpr (\n"
1996                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
1997                "}",
1998                getLLVMStyleWithColumns(62));
1999   verifyFormat("if (a) {\n"
2000                "} else if CONSTEXPR (\n"
2001                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
2002                "}",
2003                getLLVMStyleWithColumns(62));
2004 }
2005 
2006 TEST_F(FormatTest, SeparatePointerReferenceAlignment) {
2007   FormatStyle Style = getLLVMStyle();
2008   EXPECT_EQ(Style.PointerAlignment, FormatStyle::PAS_Right);
2009   EXPECT_EQ(Style.ReferenceAlignment, FormatStyle::RAS_Pointer);
2010   verifyFormat("int *f1(int *a, int &b, int &&c);", Style);
2011   verifyFormat("int &f2(int &&c, int *a, int &b);", Style);
2012   verifyFormat("int &&f3(int &b, int &&c, int *a);", Style);
2013   verifyFormat("int *f1(int &a) const &;", Style);
2014   verifyFormat("int *f1(int &a) const & = 0;", Style);
2015   verifyFormat("int *a = f1();", Style);
2016   verifyFormat("int &b = f2();", Style);
2017   verifyFormat("int &&c = f3();", Style);
2018   verifyFormat("for (auto a = 0, b = 0; const auto &c : {1, 2, 3})", Style);
2019   verifyFormat("for (auto a = 0, b = 0; const int &c : {1, 2, 3})", Style);
2020   verifyFormat("for (auto a = 0, b = 0; const Foo &c : {1, 2, 3})", Style);
2021   verifyFormat("for (auto a = 0, b = 0; const Foo *c : {1, 2, 3})", Style);
2022   verifyFormat("for (int a = 0, b = 0; const auto &c : {1, 2, 3})", Style);
2023   verifyFormat("for (int a = 0, b = 0; const int &c : {1, 2, 3})", Style);
2024   verifyFormat("for (int a = 0, b = 0; const Foo &c : {1, 2, 3})", Style);
2025   verifyFormat("for (int a = 0, b++; const auto &c : {1, 2, 3})", Style);
2026   verifyFormat("for (int a = 0, b++; const int &c : {1, 2, 3})", Style);
2027   verifyFormat("for (int a = 0, b++; const Foo &c : {1, 2, 3})", Style);
2028   verifyFormat("for (auto x = 0; auto &c : {1, 2, 3})", Style);
2029   verifyFormat("for (auto x = 0; int &c : {1, 2, 3})", Style);
2030   verifyFormat("for (int x = 0; auto &c : {1, 2, 3})", Style);
2031   verifyFormat("for (int x = 0; int &c : {1, 2, 3})", Style);
2032   verifyFormat("for (f(); auto &c : {1, 2, 3})", Style);
2033   verifyFormat("for (f(); int &c : {1, 2, 3})", Style);
2034   verifyFormat(
2035       "function<int(int &)> res1 = [](int &a) { return 0000000000000; },\n"
2036       "                     res2 = [](int &a) { return 0000000000000; };",
2037       Style);
2038 
2039   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
2040   verifyFormat("Const unsigned int *c;\n"
2041                "const unsigned int *d;\n"
2042                "Const unsigned int &e;\n"
2043                "const unsigned int &f;\n"
2044                "const unsigned    &&g;\n"
2045                "Const unsigned      h;",
2046                Style);
2047 
2048   Style.PointerAlignment = FormatStyle::PAS_Left;
2049   Style.ReferenceAlignment = FormatStyle::RAS_Pointer;
2050   verifyFormat("int* f1(int* a, int& b, int&& c);", Style);
2051   verifyFormat("int& f2(int&& c, int* a, int& b);", Style);
2052   verifyFormat("int&& f3(int& b, int&& c, int* a);", Style);
2053   verifyFormat("int* f1(int& a) const& = 0;", Style);
2054   verifyFormat("int* a = f1();", Style);
2055   verifyFormat("int& b = f2();", Style);
2056   verifyFormat("int&& c = f3();", Style);
2057   verifyFormat("for (auto a = 0, b = 0; const auto& c : {1, 2, 3})", Style);
2058   verifyFormat("for (auto a = 0, b = 0; const int& c : {1, 2, 3})", Style);
2059   verifyFormat("for (auto a = 0, b = 0; const Foo& c : {1, 2, 3})", Style);
2060   verifyFormat("for (auto a = 0, b = 0; const Foo* c : {1, 2, 3})", Style);
2061   verifyFormat("for (int a = 0, b = 0; const auto& c : {1, 2, 3})", Style);
2062   verifyFormat("for (int a = 0, b = 0; const int& c : {1, 2, 3})", Style);
2063   verifyFormat("for (int a = 0, b = 0; const Foo& c : {1, 2, 3})", Style);
2064   verifyFormat("for (int a = 0, b = 0; const Foo* c : {1, 2, 3})", Style);
2065   verifyFormat("for (int a = 0, b++; const auto& c : {1, 2, 3})", Style);
2066   verifyFormat("for (int a = 0, b++; const int& c : {1, 2, 3})", Style);
2067   verifyFormat("for (int a = 0, b++; const Foo& c : {1, 2, 3})", Style);
2068   verifyFormat("for (int a = 0, b++; const Foo* c : {1, 2, 3})", Style);
2069   verifyFormat("for (auto x = 0; auto& c : {1, 2, 3})", Style);
2070   verifyFormat("for (auto x = 0; int& c : {1, 2, 3})", Style);
2071   verifyFormat("for (int x = 0; auto& c : {1, 2, 3})", Style);
2072   verifyFormat("for (int x = 0; int& c : {1, 2, 3})", Style);
2073   verifyFormat("for (f(); auto& c : {1, 2, 3})", Style);
2074   verifyFormat("for (f(); int& c : {1, 2, 3})", Style);
2075   verifyFormat(
2076       "function<int(int&)> res1 = [](int& a) { return 0000000000000; },\n"
2077       "                    res2 = [](int& a) { return 0000000000000; };",
2078       Style);
2079 
2080   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
2081   verifyFormat("Const unsigned int* c;\n"
2082                "const unsigned int* d;\n"
2083                "Const unsigned int& e;\n"
2084                "const unsigned int& f;\n"
2085                "const unsigned&&    g;\n"
2086                "Const unsigned      h;",
2087                Style);
2088 
2089   Style.PointerAlignment = FormatStyle::PAS_Right;
2090   Style.ReferenceAlignment = FormatStyle::RAS_Left;
2091   verifyFormat("int *f1(int *a, int& b, int&& c);", Style);
2092   verifyFormat("int& f2(int&& c, int *a, int& b);", Style);
2093   verifyFormat("int&& f3(int& b, int&& c, int *a);", Style);
2094   verifyFormat("int *a = f1();", Style);
2095   verifyFormat("int& b = f2();", Style);
2096   verifyFormat("int&& c = f3();", Style);
2097   verifyFormat("for (auto a = 0, b = 0; const Foo *c : {1, 2, 3})", Style);
2098   verifyFormat("for (int a = 0, b = 0; const Foo *c : {1, 2, 3})", Style);
2099   verifyFormat("for (int a = 0, b++; const Foo *c : {1, 2, 3})", Style);
2100 
2101   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
2102   verifyFormat("Const unsigned int *c;\n"
2103                "const unsigned int *d;\n"
2104                "Const unsigned int& e;\n"
2105                "const unsigned int& f;\n"
2106                "const unsigned      g;\n"
2107                "Const unsigned      h;",
2108                Style);
2109 
2110   Style.PointerAlignment = FormatStyle::PAS_Left;
2111   Style.ReferenceAlignment = FormatStyle::RAS_Middle;
2112   verifyFormat("int* f1(int* a, int & b, int && c);", Style);
2113   verifyFormat("int & f2(int && c, int* a, int & b);", Style);
2114   verifyFormat("int && f3(int & b, int && c, int* a);", Style);
2115   verifyFormat("int* a = f1();", Style);
2116   verifyFormat("int & b = f2();", Style);
2117   verifyFormat("int && c = f3();", Style);
2118   verifyFormat("for (auto a = 0, b = 0; const auto & c : {1, 2, 3})", Style);
2119   verifyFormat("for (auto a = 0, b = 0; const int & c : {1, 2, 3})", Style);
2120   verifyFormat("for (auto a = 0, b = 0; const Foo & c : {1, 2, 3})", Style);
2121   verifyFormat("for (auto a = 0, b = 0; const Foo* c : {1, 2, 3})", Style);
2122   verifyFormat("for (int a = 0, b++; const auto & c : {1, 2, 3})", Style);
2123   verifyFormat("for (int a = 0, b++; const int & c : {1, 2, 3})", Style);
2124   verifyFormat("for (int a = 0, b++; const Foo & c : {1, 2, 3})", Style);
2125   verifyFormat("for (int a = 0, b++; const Foo* c : {1, 2, 3})", Style);
2126   verifyFormat("for (auto x = 0; auto & c : {1, 2, 3})", Style);
2127   verifyFormat("for (auto x = 0; int & c : {1, 2, 3})", Style);
2128   verifyFormat("for (int x = 0; auto & c : {1, 2, 3})", Style);
2129   verifyFormat("for (int x = 0; int & c : {1, 2, 3})", Style);
2130   verifyFormat("for (f(); auto & c : {1, 2, 3})", Style);
2131   verifyFormat("for (f(); int & c : {1, 2, 3})", Style);
2132   verifyFormat(
2133       "function<int(int &)> res1 = [](int & a) { return 0000000000000; },\n"
2134       "                     res2 = [](int & a) { return 0000000000000; };",
2135       Style);
2136 
2137   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
2138   verifyFormat("Const unsigned int*  c;\n"
2139                "const unsigned int*  d;\n"
2140                "Const unsigned int & e;\n"
2141                "const unsigned int & f;\n"
2142                "const unsigned &&    g;\n"
2143                "Const unsigned       h;",
2144                Style);
2145 
2146   Style.PointerAlignment = FormatStyle::PAS_Middle;
2147   Style.ReferenceAlignment = FormatStyle::RAS_Right;
2148   verifyFormat("int * f1(int * a, int &b, int &&c);", Style);
2149   verifyFormat("int &f2(int &&c, int * a, int &b);", Style);
2150   verifyFormat("int &&f3(int &b, int &&c, int * a);", Style);
2151   verifyFormat("int * a = f1();", Style);
2152   verifyFormat("int &b = f2();", Style);
2153   verifyFormat("int &&c = f3();", Style);
2154   verifyFormat("for (auto a = 0, b = 0; const Foo * c : {1, 2, 3})", Style);
2155   verifyFormat("for (int a = 0, b = 0; const Foo * c : {1, 2, 3})", Style);
2156   verifyFormat("for (int a = 0, b++; const Foo * c : {1, 2, 3})", Style);
2157 
2158   // FIXME: we don't handle this yet, so output may be arbitrary until it's
2159   // specifically handled
2160   // verifyFormat("int Add2(BTree * &Root, char * szToAdd)", Style);
2161 }
2162 
2163 TEST_F(FormatTest, FormatsForLoop) {
2164   verifyFormat(
2165       "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
2166       "     ++VeryVeryLongLoopVariable)\n"
2167       "  ;");
2168   verifyFormat("for (;;)\n"
2169                "  f();");
2170   verifyFormat("for (;;) {\n}");
2171   verifyFormat("for (;;) {\n"
2172                "  f();\n"
2173                "}");
2174   verifyFormat("for (int i = 0; (i < 10); ++i) {\n}");
2175 
2176   verifyFormat(
2177       "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
2178       "                                          E = UnwrappedLines.end();\n"
2179       "     I != E; ++I) {\n}");
2180 
2181   verifyFormat(
2182       "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
2183       "     ++IIIII) {\n}");
2184   verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n"
2185                "         aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n"
2186                "     aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}");
2187   verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n"
2188                "         I = FD->getDeclsInPrototypeScope().begin(),\n"
2189                "         E = FD->getDeclsInPrototypeScope().end();\n"
2190                "     I != E; ++I) {\n}");
2191   verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n"
2192                "         I = Container.begin(),\n"
2193                "         E = Container.end();\n"
2194                "     I != E; ++I) {\n}",
2195                getLLVMStyleWithColumns(76));
2196 
2197   verifyFormat(
2198       "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
2199       "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n"
2200       "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2201       "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
2202       "     ++aaaaaaaaaaa) {\n}");
2203   verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
2204                "                bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n"
2205                "     ++i) {\n}");
2206   verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n"
2207                "     aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
2208                "}");
2209   verifyFormat("for (some_namespace::SomeIterator iter( // force break\n"
2210                "         aaaaaaaaaa);\n"
2211                "     iter; ++iter) {\n"
2212                "}");
2213   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2214                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
2215                "     aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n"
2216                "     ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {");
2217 
2218   // These should not be formatted as Objective-C for-in loops.
2219   verifyFormat("for (Foo *x = 0; x != in; x++) {\n}");
2220   verifyFormat("Foo *x;\nfor (x = 0; x != in; x++) {\n}");
2221   verifyFormat("Foo *x;\nfor (x in y) {\n}");
2222   verifyFormat(
2223       "for (const Foo<Bar> &baz = in.value(); !baz.at_end(); ++baz) {\n}");
2224 
2225   FormatStyle NoBinPacking = getLLVMStyle();
2226   NoBinPacking.BinPackParameters = false;
2227   verifyFormat("for (int aaaaaaaaaaa = 1;\n"
2228                "     aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n"
2229                "                                           aaaaaaaaaaaaaaaa,\n"
2230                "                                           aaaaaaaaaaaaaaaa,\n"
2231                "                                           aaaaaaaaaaaaaaaa);\n"
2232                "     aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
2233                "}",
2234                NoBinPacking);
2235   verifyFormat(
2236       "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
2237       "                                          E = UnwrappedLines.end();\n"
2238       "     I != E;\n"
2239       "     ++I) {\n}",
2240       NoBinPacking);
2241 
2242   FormatStyle AlignLeft = getLLVMStyle();
2243   AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
2244   verifyFormat("for (A* a = start; a < end; ++a, ++value) {\n}", AlignLeft);
2245 }
2246 
2247 TEST_F(FormatTest, RangeBasedForLoops) {
2248   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
2249                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
2250   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n"
2251                "     aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}");
2252   verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n"
2253                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
2254   verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n"
2255                "     aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}");
2256 }
2257 
2258 TEST_F(FormatTest, ForEachLoops) {
2259   FormatStyle Style = getLLVMStyle();
2260   EXPECT_EQ(Style.AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never);
2261   EXPECT_EQ(Style.AllowShortLoopsOnASingleLine, false);
2262   verifyFormat("void f() {\n"
2263                "  for (;;) {\n"
2264                "  }\n"
2265                "  foreach (Item *item, itemlist) {\n"
2266                "  }\n"
2267                "  Q_FOREACH (Item *item, itemlist) {\n"
2268                "  }\n"
2269                "  BOOST_FOREACH (Item *item, itemlist) {\n"
2270                "  }\n"
2271                "  UNKNOWN_FOREACH(Item * item, itemlist) {}\n"
2272                "}",
2273                Style);
2274   verifyFormat("void f() {\n"
2275                "  for (;;)\n"
2276                "    int j = 1;\n"
2277                "  Q_FOREACH (int v, vec)\n"
2278                "    v *= 2;\n"
2279                "  for (;;) {\n"
2280                "    int j = 1;\n"
2281                "  }\n"
2282                "  Q_FOREACH (int v, vec) {\n"
2283                "    v *= 2;\n"
2284                "  }\n"
2285                "}",
2286                Style);
2287 
2288   FormatStyle ShortBlocks = getLLVMStyle();
2289   ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
2290   EXPECT_EQ(ShortBlocks.AllowShortLoopsOnASingleLine, false);
2291   verifyFormat("void f() {\n"
2292                "  for (;;)\n"
2293                "    int j = 1;\n"
2294                "  Q_FOREACH (int &v, vec)\n"
2295                "    v *= 2;\n"
2296                "  for (;;) {\n"
2297                "    int j = 1;\n"
2298                "  }\n"
2299                "  Q_FOREACH (int &v, vec) {\n"
2300                "    int j = 1;\n"
2301                "  }\n"
2302                "}",
2303                ShortBlocks);
2304 
2305   FormatStyle ShortLoops = getLLVMStyle();
2306   ShortLoops.AllowShortLoopsOnASingleLine = true;
2307   EXPECT_EQ(ShortLoops.AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never);
2308   verifyFormat("void f() {\n"
2309                "  for (;;) int j = 1;\n"
2310                "  Q_FOREACH (int &v, vec) int j = 1;\n"
2311                "  for (;;) {\n"
2312                "    int j = 1;\n"
2313                "  }\n"
2314                "  Q_FOREACH (int &v, vec) {\n"
2315                "    int j = 1;\n"
2316                "  }\n"
2317                "}",
2318                ShortLoops);
2319 
2320   FormatStyle ShortBlocksAndLoops = getLLVMStyle();
2321   ShortBlocksAndLoops.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
2322   ShortBlocksAndLoops.AllowShortLoopsOnASingleLine = true;
2323   verifyFormat("void f() {\n"
2324                "  for (;;) int j = 1;\n"
2325                "  Q_FOREACH (int &v, vec) int j = 1;\n"
2326                "  for (;;) { int j = 1; }\n"
2327                "  Q_FOREACH (int &v, vec) { int j = 1; }\n"
2328                "}",
2329                ShortBlocksAndLoops);
2330 
2331   Style.SpaceBeforeParens =
2332       FormatStyle::SBPO_ControlStatementsExceptControlMacros;
2333   verifyFormat("void f() {\n"
2334                "  for (;;) {\n"
2335                "  }\n"
2336                "  foreach(Item *item, itemlist) {\n"
2337                "  }\n"
2338                "  Q_FOREACH(Item *item, itemlist) {\n"
2339                "  }\n"
2340                "  BOOST_FOREACH(Item *item, itemlist) {\n"
2341                "  }\n"
2342                "  UNKNOWN_FOREACH(Item * item, itemlist) {}\n"
2343                "}",
2344                Style);
2345 
2346   // As function-like macros.
2347   verifyFormat("#define foreach(x, y)\n"
2348                "#define Q_FOREACH(x, y)\n"
2349                "#define BOOST_FOREACH(x, y)\n"
2350                "#define UNKNOWN_FOREACH(x, y)\n");
2351 
2352   // Not as function-like macros.
2353   verifyFormat("#define foreach (x, y)\n"
2354                "#define Q_FOREACH (x, y)\n"
2355                "#define BOOST_FOREACH (x, y)\n"
2356                "#define UNKNOWN_FOREACH (x, y)\n");
2357 
2358   // handle microsoft non standard extension
2359   verifyFormat("for each (char c in x->MyStringProperty)");
2360 }
2361 
2362 TEST_F(FormatTest, FormatsWhileLoop) {
2363   verifyFormat("while (true) {\n}");
2364   verifyFormat("while (true)\n"
2365                "  f();");
2366   verifyFormat("while () {\n}");
2367   verifyFormat("while () {\n"
2368                "  f();\n"
2369                "}");
2370 }
2371 
2372 TEST_F(FormatTest, FormatsDoWhile) {
2373   verifyFormat("do {\n"
2374                "  do_something();\n"
2375                "} while (something());");
2376   verifyFormat("do\n"
2377                "  do_something();\n"
2378                "while (something());");
2379 }
2380 
2381 TEST_F(FormatTest, FormatsSwitchStatement) {
2382   verifyFormat("switch (x) {\n"
2383                "case 1:\n"
2384                "  f();\n"
2385                "  break;\n"
2386                "case kFoo:\n"
2387                "case ns::kBar:\n"
2388                "case kBaz:\n"
2389                "  break;\n"
2390                "default:\n"
2391                "  g();\n"
2392                "  break;\n"
2393                "}");
2394   verifyFormat("switch (x) {\n"
2395                "case 1: {\n"
2396                "  f();\n"
2397                "  break;\n"
2398                "}\n"
2399                "case 2: {\n"
2400                "  break;\n"
2401                "}\n"
2402                "}");
2403   verifyFormat("switch (x) {\n"
2404                "case 1: {\n"
2405                "  f();\n"
2406                "  {\n"
2407                "    g();\n"
2408                "    h();\n"
2409                "  }\n"
2410                "  break;\n"
2411                "}\n"
2412                "}");
2413   verifyFormat("switch (x) {\n"
2414                "case 1: {\n"
2415                "  f();\n"
2416                "  if (foo) {\n"
2417                "    g();\n"
2418                "    h();\n"
2419                "  }\n"
2420                "  break;\n"
2421                "}\n"
2422                "}");
2423   verifyFormat("switch (x) {\n"
2424                "case 1: {\n"
2425                "  f();\n"
2426                "  g();\n"
2427                "} break;\n"
2428                "}");
2429   verifyFormat("switch (test)\n"
2430                "  ;");
2431   verifyFormat("switch (x) {\n"
2432                "default: {\n"
2433                "  // Do nothing.\n"
2434                "}\n"
2435                "}");
2436   verifyFormat("switch (x) {\n"
2437                "// comment\n"
2438                "// if 1, do f()\n"
2439                "case 1:\n"
2440                "  f();\n"
2441                "}");
2442   verifyFormat("switch (x) {\n"
2443                "case 1:\n"
2444                "  // Do amazing stuff\n"
2445                "  {\n"
2446                "    f();\n"
2447                "    g();\n"
2448                "  }\n"
2449                "  break;\n"
2450                "}");
2451   verifyFormat("#define A          \\\n"
2452                "  switch (x) {     \\\n"
2453                "  case a:          \\\n"
2454                "    foo = b;       \\\n"
2455                "  }",
2456                getLLVMStyleWithColumns(20));
2457   verifyFormat("#define OPERATION_CASE(name)           \\\n"
2458                "  case OP_name:                        \\\n"
2459                "    return operations::Operation##name\n",
2460                getLLVMStyleWithColumns(40));
2461   verifyFormat("switch (x) {\n"
2462                "case 1:;\n"
2463                "default:;\n"
2464                "  int i;\n"
2465                "}");
2466 
2467   verifyGoogleFormat("switch (x) {\n"
2468                      "  case 1:\n"
2469                      "    f();\n"
2470                      "    break;\n"
2471                      "  case kFoo:\n"
2472                      "  case ns::kBar:\n"
2473                      "  case kBaz:\n"
2474                      "    break;\n"
2475                      "  default:\n"
2476                      "    g();\n"
2477                      "    break;\n"
2478                      "}");
2479   verifyGoogleFormat("switch (x) {\n"
2480                      "  case 1: {\n"
2481                      "    f();\n"
2482                      "    break;\n"
2483                      "  }\n"
2484                      "}");
2485   verifyGoogleFormat("switch (test)\n"
2486                      "  ;");
2487 
2488   verifyGoogleFormat("#define OPERATION_CASE(name) \\\n"
2489                      "  case OP_name:              \\\n"
2490                      "    return operations::Operation##name\n");
2491   verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n"
2492                      "  // Get the correction operation class.\n"
2493                      "  switch (OpCode) {\n"
2494                      "    CASE(Add);\n"
2495                      "    CASE(Subtract);\n"
2496                      "    default:\n"
2497                      "      return operations::Unknown;\n"
2498                      "  }\n"
2499                      "#undef OPERATION_CASE\n"
2500                      "}");
2501   verifyFormat("DEBUG({\n"
2502                "  switch (x) {\n"
2503                "  case A:\n"
2504                "    f();\n"
2505                "    break;\n"
2506                "    // fallthrough\n"
2507                "  case B:\n"
2508                "    g();\n"
2509                "    break;\n"
2510                "  }\n"
2511                "});");
2512   EXPECT_EQ("DEBUG({\n"
2513             "  switch (x) {\n"
2514             "  case A:\n"
2515             "    f();\n"
2516             "    break;\n"
2517             "  // On B:\n"
2518             "  case B:\n"
2519             "    g();\n"
2520             "    break;\n"
2521             "  }\n"
2522             "});",
2523             format("DEBUG({\n"
2524                    "  switch (x) {\n"
2525                    "  case A:\n"
2526                    "    f();\n"
2527                    "    break;\n"
2528                    "  // On B:\n"
2529                    "  case B:\n"
2530                    "    g();\n"
2531                    "    break;\n"
2532                    "  }\n"
2533                    "});",
2534                    getLLVMStyle()));
2535   EXPECT_EQ("switch (n) {\n"
2536             "case 0: {\n"
2537             "  return false;\n"
2538             "}\n"
2539             "default: {\n"
2540             "  return true;\n"
2541             "}\n"
2542             "}",
2543             format("switch (n)\n"
2544                    "{\n"
2545                    "case 0: {\n"
2546                    "  return false;\n"
2547                    "}\n"
2548                    "default: {\n"
2549                    "  return true;\n"
2550                    "}\n"
2551                    "}",
2552                    getLLVMStyle()));
2553   verifyFormat("switch (a) {\n"
2554                "case (b):\n"
2555                "  return;\n"
2556                "}");
2557 
2558   verifyFormat("switch (a) {\n"
2559                "case some_namespace::\n"
2560                "    some_constant:\n"
2561                "  return;\n"
2562                "}",
2563                getLLVMStyleWithColumns(34));
2564 
2565   FormatStyle Style = getLLVMStyle();
2566   Style.IndentCaseLabels = true;
2567   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
2568   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
2569   Style.BraceWrapping.AfterCaseLabel = true;
2570   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
2571   EXPECT_EQ("switch (n)\n"
2572             "{\n"
2573             "  case 0:\n"
2574             "  {\n"
2575             "    return false;\n"
2576             "  }\n"
2577             "  default:\n"
2578             "  {\n"
2579             "    return true;\n"
2580             "  }\n"
2581             "}",
2582             format("switch (n) {\n"
2583                    "  case 0: {\n"
2584                    "    return false;\n"
2585                    "  }\n"
2586                    "  default: {\n"
2587                    "    return true;\n"
2588                    "  }\n"
2589                    "}",
2590                    Style));
2591   Style.BraceWrapping.AfterCaseLabel = false;
2592   EXPECT_EQ("switch (n)\n"
2593             "{\n"
2594             "  case 0: {\n"
2595             "    return false;\n"
2596             "  }\n"
2597             "  default: {\n"
2598             "    return true;\n"
2599             "  }\n"
2600             "}",
2601             format("switch (n) {\n"
2602                    "  case 0:\n"
2603                    "  {\n"
2604                    "    return false;\n"
2605                    "  }\n"
2606                    "  default:\n"
2607                    "  {\n"
2608                    "    return true;\n"
2609                    "  }\n"
2610                    "}",
2611                    Style));
2612   Style.IndentCaseLabels = false;
2613   Style.IndentCaseBlocks = true;
2614   EXPECT_EQ("switch (n)\n"
2615             "{\n"
2616             "case 0:\n"
2617             "  {\n"
2618             "    return false;\n"
2619             "  }\n"
2620             "case 1:\n"
2621             "  break;\n"
2622             "default:\n"
2623             "  {\n"
2624             "    return true;\n"
2625             "  }\n"
2626             "}",
2627             format("switch (n) {\n"
2628                    "case 0: {\n"
2629                    "  return false;\n"
2630                    "}\n"
2631                    "case 1:\n"
2632                    "  break;\n"
2633                    "default: {\n"
2634                    "  return true;\n"
2635                    "}\n"
2636                    "}",
2637                    Style));
2638   Style.IndentCaseLabels = true;
2639   Style.IndentCaseBlocks = true;
2640   EXPECT_EQ("switch (n)\n"
2641             "{\n"
2642             "  case 0:\n"
2643             "    {\n"
2644             "      return false;\n"
2645             "    }\n"
2646             "  case 1:\n"
2647             "    break;\n"
2648             "  default:\n"
2649             "    {\n"
2650             "      return true;\n"
2651             "    }\n"
2652             "}",
2653             format("switch (n) {\n"
2654                    "case 0: {\n"
2655                    "  return false;\n"
2656                    "}\n"
2657                    "case 1:\n"
2658                    "  break;\n"
2659                    "default: {\n"
2660                    "  return true;\n"
2661                    "}\n"
2662                    "}",
2663                    Style));
2664 }
2665 
2666 TEST_F(FormatTest, CaseRanges) {
2667   verifyFormat("switch (x) {\n"
2668                "case 'A' ... 'Z':\n"
2669                "case 1 ... 5:\n"
2670                "case a ... b:\n"
2671                "  break;\n"
2672                "}");
2673 }
2674 
2675 TEST_F(FormatTest, ShortEnums) {
2676   FormatStyle Style = getLLVMStyle();
2677   Style.AllowShortEnumsOnASingleLine = true;
2678   verifyFormat("enum { A, B, C } ShortEnum1, ShortEnum2;", Style);
2679   verifyFormat("typedef enum { A, B, C } ShortEnum1, ShortEnum2;", Style);
2680   Style.AllowShortEnumsOnASingleLine = false;
2681   verifyFormat("enum {\n"
2682                "  A,\n"
2683                "  B,\n"
2684                "  C\n"
2685                "} ShortEnum1, ShortEnum2;",
2686                Style);
2687   verifyFormat("typedef enum {\n"
2688                "  A,\n"
2689                "  B,\n"
2690                "  C\n"
2691                "} ShortEnum1, ShortEnum2;",
2692                Style);
2693   verifyFormat("enum {\n"
2694                "  A,\n"
2695                "} ShortEnum1, ShortEnum2;",
2696                Style);
2697   verifyFormat("typedef enum {\n"
2698                "  A,\n"
2699                "} ShortEnum1, ShortEnum2;",
2700                Style);
2701   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
2702   Style.BraceWrapping.AfterEnum = true;
2703   verifyFormat("enum\n"
2704                "{\n"
2705                "  A,\n"
2706                "  B,\n"
2707                "  C\n"
2708                "} ShortEnum1, ShortEnum2;",
2709                Style);
2710   verifyFormat("typedef enum\n"
2711                "{\n"
2712                "  A,\n"
2713                "  B,\n"
2714                "  C\n"
2715                "} ShortEnum1, ShortEnum2;",
2716                Style);
2717 }
2718 
2719 TEST_F(FormatTest, ShortCaseLabels) {
2720   FormatStyle Style = getLLVMStyle();
2721   Style.AllowShortCaseLabelsOnASingleLine = true;
2722   verifyFormat("switch (a) {\n"
2723                "case 1: x = 1; break;\n"
2724                "case 2: return;\n"
2725                "case 3:\n"
2726                "case 4:\n"
2727                "case 5: return;\n"
2728                "case 6: // comment\n"
2729                "  return;\n"
2730                "case 7:\n"
2731                "  // comment\n"
2732                "  return;\n"
2733                "case 8:\n"
2734                "  x = 8; // comment\n"
2735                "  break;\n"
2736                "default: y = 1; break;\n"
2737                "}",
2738                Style);
2739   verifyFormat("switch (a) {\n"
2740                "case 0: return; // comment\n"
2741                "case 1: break;  // comment\n"
2742                "case 2: return;\n"
2743                "// comment\n"
2744                "case 3: return;\n"
2745                "// comment 1\n"
2746                "// comment 2\n"
2747                "// comment 3\n"
2748                "case 4: break; /* comment */\n"
2749                "case 5:\n"
2750                "  // comment\n"
2751                "  break;\n"
2752                "case 6: /* comment */ x = 1; break;\n"
2753                "case 7: x = /* comment */ 1; break;\n"
2754                "case 8:\n"
2755                "  x = 1; /* comment */\n"
2756                "  break;\n"
2757                "case 9:\n"
2758                "  break; // comment line 1\n"
2759                "         // comment line 2\n"
2760                "}",
2761                Style);
2762   EXPECT_EQ("switch (a) {\n"
2763             "case 1:\n"
2764             "  x = 8;\n"
2765             "  // fall through\n"
2766             "case 2: x = 8;\n"
2767             "// comment\n"
2768             "case 3:\n"
2769             "  return; /* comment line 1\n"
2770             "           * comment line 2 */\n"
2771             "case 4: i = 8;\n"
2772             "// something else\n"
2773             "#if FOO\n"
2774             "case 5: break;\n"
2775             "#endif\n"
2776             "}",
2777             format("switch (a) {\n"
2778                    "case 1: x = 8;\n"
2779                    "  // fall through\n"
2780                    "case 2:\n"
2781                    "  x = 8;\n"
2782                    "// comment\n"
2783                    "case 3:\n"
2784                    "  return; /* comment line 1\n"
2785                    "           * comment line 2 */\n"
2786                    "case 4:\n"
2787                    "  i = 8;\n"
2788                    "// something else\n"
2789                    "#if FOO\n"
2790                    "case 5: break;\n"
2791                    "#endif\n"
2792                    "}",
2793                    Style));
2794   EXPECT_EQ("switch (a) {\n"
2795             "case 0:\n"
2796             "  return; // long long long long long long long long long long "
2797             "long long comment\n"
2798             "          // line\n"
2799             "}",
2800             format("switch (a) {\n"
2801                    "case 0: return; // long long long long long long long long "
2802                    "long long long long comment line\n"
2803                    "}",
2804                    Style));
2805   EXPECT_EQ("switch (a) {\n"
2806             "case 0:\n"
2807             "  return; /* long long long long long long long long long long "
2808             "long long comment\n"
2809             "             line */\n"
2810             "}",
2811             format("switch (a) {\n"
2812                    "case 0: return; /* long long long long long long long long "
2813                    "long long long long comment line */\n"
2814                    "}",
2815                    Style));
2816   verifyFormat("switch (a) {\n"
2817                "#if FOO\n"
2818                "case 0: return 0;\n"
2819                "#endif\n"
2820                "}",
2821                Style);
2822   verifyFormat("switch (a) {\n"
2823                "case 1: {\n"
2824                "}\n"
2825                "case 2: {\n"
2826                "  return;\n"
2827                "}\n"
2828                "case 3: {\n"
2829                "  x = 1;\n"
2830                "  return;\n"
2831                "}\n"
2832                "case 4:\n"
2833                "  if (x)\n"
2834                "    return;\n"
2835                "}",
2836                Style);
2837   Style.ColumnLimit = 21;
2838   verifyFormat("switch (a) {\n"
2839                "case 1: x = 1; break;\n"
2840                "case 2: return;\n"
2841                "case 3:\n"
2842                "case 4:\n"
2843                "case 5: return;\n"
2844                "default:\n"
2845                "  y = 1;\n"
2846                "  break;\n"
2847                "}",
2848                Style);
2849   Style.ColumnLimit = 80;
2850   Style.AllowShortCaseLabelsOnASingleLine = false;
2851   Style.IndentCaseLabels = true;
2852   EXPECT_EQ("switch (n) {\n"
2853             "  default /*comments*/:\n"
2854             "    return true;\n"
2855             "  case 0:\n"
2856             "    return false;\n"
2857             "}",
2858             format("switch (n) {\n"
2859                    "default/*comments*/:\n"
2860                    "  return true;\n"
2861                    "case 0:\n"
2862                    "  return false;\n"
2863                    "}",
2864                    Style));
2865   Style.AllowShortCaseLabelsOnASingleLine = true;
2866   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
2867   Style.BraceWrapping.AfterCaseLabel = true;
2868   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
2869   EXPECT_EQ("switch (n)\n"
2870             "{\n"
2871             "  case 0:\n"
2872             "  {\n"
2873             "    return false;\n"
2874             "  }\n"
2875             "  default:\n"
2876             "  {\n"
2877             "    return true;\n"
2878             "  }\n"
2879             "}",
2880             format("switch (n) {\n"
2881                    "  case 0: {\n"
2882                    "    return false;\n"
2883                    "  }\n"
2884                    "  default:\n"
2885                    "  {\n"
2886                    "    return true;\n"
2887                    "  }\n"
2888                    "}",
2889                    Style));
2890 }
2891 
2892 TEST_F(FormatTest, FormatsLabels) {
2893   verifyFormat("void f() {\n"
2894                "  some_code();\n"
2895                "test_label:\n"
2896                "  some_other_code();\n"
2897                "  {\n"
2898                "    some_more_code();\n"
2899                "  another_label:\n"
2900                "    some_more_code();\n"
2901                "  }\n"
2902                "}");
2903   verifyFormat("{\n"
2904                "  some_code();\n"
2905                "test_label:\n"
2906                "  some_other_code();\n"
2907                "}");
2908   verifyFormat("{\n"
2909                "  some_code();\n"
2910                "test_label:;\n"
2911                "  int i = 0;\n"
2912                "}");
2913   FormatStyle Style = getLLVMStyle();
2914   Style.IndentGotoLabels = false;
2915   verifyFormat("void f() {\n"
2916                "  some_code();\n"
2917                "test_label:\n"
2918                "  some_other_code();\n"
2919                "  {\n"
2920                "    some_more_code();\n"
2921                "another_label:\n"
2922                "    some_more_code();\n"
2923                "  }\n"
2924                "}",
2925                Style);
2926   verifyFormat("{\n"
2927                "  some_code();\n"
2928                "test_label:\n"
2929                "  some_other_code();\n"
2930                "}",
2931                Style);
2932   verifyFormat("{\n"
2933                "  some_code();\n"
2934                "test_label:;\n"
2935                "  int i = 0;\n"
2936                "}");
2937 }
2938 
2939 TEST_F(FormatTest, MultiLineControlStatements) {
2940   FormatStyle Style = getLLVMStyleWithColumns(20);
2941   Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom;
2942   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine;
2943   // Short lines should keep opening brace on same line.
2944   EXPECT_EQ("if (foo) {\n"
2945             "  bar();\n"
2946             "}",
2947             format("if(foo){bar();}", Style));
2948   EXPECT_EQ("if (foo) {\n"
2949             "  bar();\n"
2950             "} else {\n"
2951             "  baz();\n"
2952             "}",
2953             format("if(foo){bar();}else{baz();}", Style));
2954   EXPECT_EQ("if (foo && bar) {\n"
2955             "  baz();\n"
2956             "}",
2957             format("if(foo&&bar){baz();}", Style));
2958   EXPECT_EQ("if (foo) {\n"
2959             "  bar();\n"
2960             "} else if (baz) {\n"
2961             "  quux();\n"
2962             "}",
2963             format("if(foo){bar();}else if(baz){quux();}", Style));
2964   EXPECT_EQ(
2965       "if (foo) {\n"
2966       "  bar();\n"
2967       "} else if (baz) {\n"
2968       "  quux();\n"
2969       "} else {\n"
2970       "  foobar();\n"
2971       "}",
2972       format("if(foo){bar();}else if(baz){quux();}else{foobar();}", Style));
2973   EXPECT_EQ("for (;;) {\n"
2974             "  foo();\n"
2975             "}",
2976             format("for(;;){foo();}"));
2977   EXPECT_EQ("while (1) {\n"
2978             "  foo();\n"
2979             "}",
2980             format("while(1){foo();}", Style));
2981   EXPECT_EQ("switch (foo) {\n"
2982             "case bar:\n"
2983             "  return;\n"
2984             "}",
2985             format("switch(foo){case bar:return;}", Style));
2986   EXPECT_EQ("try {\n"
2987             "  foo();\n"
2988             "} catch (...) {\n"
2989             "  bar();\n"
2990             "}",
2991             format("try{foo();}catch(...){bar();}", Style));
2992   EXPECT_EQ("do {\n"
2993             "  foo();\n"
2994             "} while (bar &&\n"
2995             "         baz);",
2996             format("do{foo();}while(bar&&baz);", Style));
2997   // Long lines should put opening brace on new line.
2998   EXPECT_EQ("if (foo && bar &&\n"
2999             "    baz)\n"
3000             "{\n"
3001             "  quux();\n"
3002             "}",
3003             format("if(foo&&bar&&baz){quux();}", Style));
3004   EXPECT_EQ("if (foo && bar &&\n"
3005             "    baz)\n"
3006             "{\n"
3007             "  quux();\n"
3008             "}",
3009             format("if (foo && bar &&\n"
3010                    "    baz) {\n"
3011                    "  quux();\n"
3012                    "}",
3013                    Style));
3014   EXPECT_EQ("if (foo) {\n"
3015             "  bar();\n"
3016             "} else if (baz ||\n"
3017             "           quux)\n"
3018             "{\n"
3019             "  foobar();\n"
3020             "}",
3021             format("if(foo){bar();}else if(baz||quux){foobar();}", Style));
3022   EXPECT_EQ(
3023       "if (foo) {\n"
3024       "  bar();\n"
3025       "} else if (baz ||\n"
3026       "           quux)\n"
3027       "{\n"
3028       "  foobar();\n"
3029       "} else {\n"
3030       "  barbaz();\n"
3031       "}",
3032       format("if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}",
3033              Style));
3034   EXPECT_EQ("for (int i = 0;\n"
3035             "     i < 10; ++i)\n"
3036             "{\n"
3037             "  foo();\n"
3038             "}",
3039             format("for(int i=0;i<10;++i){foo();}", Style));
3040   EXPECT_EQ("foreach (int i,\n"
3041             "         list)\n"
3042             "{\n"
3043             "  foo();\n"
3044             "}",
3045             format("foreach(int i, list){foo();}", Style));
3046   Style.ColumnLimit =
3047       40; // to concentrate at brace wrapping, not line wrap due to column limit
3048   EXPECT_EQ("foreach (int i, list) {\n"
3049             "  foo();\n"
3050             "}",
3051             format("foreach(int i, list){foo();}", Style));
3052   Style.ColumnLimit =
3053       20; // to concentrate at brace wrapping, not line wrap due to column limit
3054   EXPECT_EQ("while (foo || bar ||\n"
3055             "       baz)\n"
3056             "{\n"
3057             "  quux();\n"
3058             "}",
3059             format("while(foo||bar||baz){quux();}", Style));
3060   EXPECT_EQ("switch (\n"
3061             "    foo = barbaz)\n"
3062             "{\n"
3063             "case quux:\n"
3064             "  return;\n"
3065             "}",
3066             format("switch(foo=barbaz){case quux:return;}", Style));
3067   EXPECT_EQ("try {\n"
3068             "  foo();\n"
3069             "} catch (\n"
3070             "    Exception &bar)\n"
3071             "{\n"
3072             "  baz();\n"
3073             "}",
3074             format("try{foo();}catch(Exception&bar){baz();}", Style));
3075   Style.ColumnLimit =
3076       40; // to concentrate at brace wrapping, not line wrap due to column limit
3077   EXPECT_EQ("try {\n"
3078             "  foo();\n"
3079             "} catch (Exception &bar) {\n"
3080             "  baz();\n"
3081             "}",
3082             format("try{foo();}catch(Exception&bar){baz();}", Style));
3083   Style.ColumnLimit =
3084       20; // to concentrate at brace wrapping, not line wrap due to column limit
3085 
3086   Style.BraceWrapping.BeforeElse = true;
3087   EXPECT_EQ(
3088       "if (foo) {\n"
3089       "  bar();\n"
3090       "}\n"
3091       "else if (baz ||\n"
3092       "         quux)\n"
3093       "{\n"
3094       "  foobar();\n"
3095       "}\n"
3096       "else {\n"
3097       "  barbaz();\n"
3098       "}",
3099       format("if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}",
3100              Style));
3101 
3102   Style.BraceWrapping.BeforeCatch = true;
3103   EXPECT_EQ("try {\n"
3104             "  foo();\n"
3105             "}\n"
3106             "catch (...) {\n"
3107             "  baz();\n"
3108             "}",
3109             format("try{foo();}catch(...){baz();}", Style));
3110 
3111   Style.BraceWrapping.AfterFunction = true;
3112   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine;
3113   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
3114   Style.ColumnLimit = 80;
3115   verifyFormat("void shortfunction() { bar(); }", Style);
3116 
3117   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
3118   verifyFormat("void shortfunction()\n"
3119                "{\n"
3120                "  bar();\n"
3121                "}",
3122                Style);
3123 }
3124 
3125 TEST_F(FormatTest, BeforeWhile) {
3126   FormatStyle Style = getLLVMStyle();
3127   Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom;
3128 
3129   verifyFormat("do {\n"
3130                "  foo();\n"
3131                "} while (1);",
3132                Style);
3133   Style.BraceWrapping.BeforeWhile = true;
3134   verifyFormat("do {\n"
3135                "  foo();\n"
3136                "}\n"
3137                "while (1);",
3138                Style);
3139 }
3140 
3141 //===----------------------------------------------------------------------===//
3142 // Tests for classes, namespaces, etc.
3143 //===----------------------------------------------------------------------===//
3144 
3145 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
3146   verifyFormat("class A {};");
3147 }
3148 
3149 TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
3150   verifyFormat("class A {\n"
3151                "public:\n"
3152                "public: // comment\n"
3153                "protected:\n"
3154                "private:\n"
3155                "  void f() {}\n"
3156                "};");
3157   verifyFormat("export class A {\n"
3158                "public:\n"
3159                "public: // comment\n"
3160                "protected:\n"
3161                "private:\n"
3162                "  void f() {}\n"
3163                "};");
3164   verifyGoogleFormat("class A {\n"
3165                      " public:\n"
3166                      " protected:\n"
3167                      " private:\n"
3168                      "  void f() {}\n"
3169                      "};");
3170   verifyGoogleFormat("export class A {\n"
3171                      " public:\n"
3172                      " protected:\n"
3173                      " private:\n"
3174                      "  void f() {}\n"
3175                      "};");
3176   verifyFormat("class A {\n"
3177                "public slots:\n"
3178                "  void f1() {}\n"
3179                "public Q_SLOTS:\n"
3180                "  void f2() {}\n"
3181                "protected slots:\n"
3182                "  void f3() {}\n"
3183                "protected Q_SLOTS:\n"
3184                "  void f4() {}\n"
3185                "private slots:\n"
3186                "  void f5() {}\n"
3187                "private Q_SLOTS:\n"
3188                "  void f6() {}\n"
3189                "signals:\n"
3190                "  void g1();\n"
3191                "Q_SIGNALS:\n"
3192                "  void g2();\n"
3193                "};");
3194 
3195   // Don't interpret 'signals' the wrong way.
3196   verifyFormat("signals.set();");
3197   verifyFormat("for (Signals signals : f()) {\n}");
3198   verifyFormat("{\n"
3199                "  signals.set(); // This needs indentation.\n"
3200                "}");
3201   verifyFormat("void f() {\n"
3202                "label:\n"
3203                "  signals.baz();\n"
3204                "}");
3205   verifyFormat("private[1];");
3206   verifyFormat("testArray[public] = 1;");
3207   verifyFormat("public();");
3208   verifyFormat("myFunc(public);");
3209   verifyFormat("std::vector<int> testVec = {private};");
3210   verifyFormat("private.p = 1;");
3211   verifyFormat("void function(private...){};");
3212   verifyFormat("if (private && public)\n");
3213   verifyFormat("private &= true;");
3214   verifyFormat("int x = private * public;");
3215   verifyFormat("public *= private;");
3216   verifyFormat("int x = public + private;");
3217   verifyFormat("private++;");
3218   verifyFormat("++private;");
3219   verifyFormat("public += private;");
3220   verifyFormat("public = public - private;");
3221   verifyFormat("public->foo();");
3222   verifyFormat("private--;");
3223   verifyFormat("--private;");
3224   verifyFormat("public -= 1;");
3225   verifyFormat("if (!private && !public)\n");
3226   verifyFormat("public != private;");
3227   verifyFormat("int x = public / private;");
3228   verifyFormat("public /= 2;");
3229   verifyFormat("public = public % 2;");
3230   verifyFormat("public %= 2;");
3231   verifyFormat("if (public < private)\n");
3232   verifyFormat("public << private;");
3233   verifyFormat("public <<= private;");
3234   verifyFormat("if (public > private)\n");
3235   verifyFormat("public >> private;");
3236   verifyFormat("public >>= private;");
3237   verifyFormat("public ^ private;");
3238   verifyFormat("public ^= private;");
3239   verifyFormat("public | private;");
3240   verifyFormat("public |= private;");
3241   verifyFormat("auto x = private ? 1 : 2;");
3242   verifyFormat("if (public == private)\n");
3243   verifyFormat("void foo(public, private)");
3244   verifyFormat("public::foo();");
3245 }
3246 
3247 TEST_F(FormatTest, SeparatesLogicalBlocks) {
3248   EXPECT_EQ("class A {\n"
3249             "public:\n"
3250             "  void f();\n"
3251             "\n"
3252             "private:\n"
3253             "  void g() {}\n"
3254             "  // test\n"
3255             "protected:\n"
3256             "  int h;\n"
3257             "};",
3258             format("class A {\n"
3259                    "public:\n"
3260                    "void f();\n"
3261                    "private:\n"
3262                    "void g() {}\n"
3263                    "// test\n"
3264                    "protected:\n"
3265                    "int h;\n"
3266                    "};"));
3267   EXPECT_EQ("class A {\n"
3268             "protected:\n"
3269             "public:\n"
3270             "  void f();\n"
3271             "};",
3272             format("class A {\n"
3273                    "protected:\n"
3274                    "\n"
3275                    "public:\n"
3276                    "\n"
3277                    "  void f();\n"
3278                    "};"));
3279 
3280   // Even ensure proper spacing inside macros.
3281   EXPECT_EQ("#define B     \\\n"
3282             "  class A {   \\\n"
3283             "   protected: \\\n"
3284             "   public:    \\\n"
3285             "    void f(); \\\n"
3286             "  };",
3287             format("#define B     \\\n"
3288                    "  class A {   \\\n"
3289                    "   protected: \\\n"
3290                    "              \\\n"
3291                    "   public:    \\\n"
3292                    "              \\\n"
3293                    "    void f(); \\\n"
3294                    "  };",
3295                    getGoogleStyle()));
3296   // But don't remove empty lines after macros ending in access specifiers.
3297   EXPECT_EQ("#define A private:\n"
3298             "\n"
3299             "int i;",
3300             format("#define A         private:\n"
3301                    "\n"
3302                    "int              i;"));
3303 }
3304 
3305 TEST_F(FormatTest, FormatsClasses) {
3306   verifyFormat("class A : public B {};");
3307   verifyFormat("class A : public ::B {};");
3308 
3309   verifyFormat(
3310       "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3311       "                             public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
3312   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
3313                "    : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3314                "      public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
3315   verifyFormat(
3316       "class A : public B, public C, public D, public E, public F {};");
3317   verifyFormat("class AAAAAAAAAAAA : public B,\n"
3318                "                     public C,\n"
3319                "                     public D,\n"
3320                "                     public E,\n"
3321                "                     public F,\n"
3322                "                     public G {};");
3323 
3324   verifyFormat("class\n"
3325                "    ReallyReallyLongClassName {\n"
3326                "  int i;\n"
3327                "};",
3328                getLLVMStyleWithColumns(32));
3329   verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
3330                "                           aaaaaaaaaaaaaaaa> {};");
3331   verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n"
3332                "    : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n"
3333                "                                 aaaaaaaaaaaaaaaaaaaaaa> {};");
3334   verifyFormat("template <class R, class C>\n"
3335                "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n"
3336                "    : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};");
3337   verifyFormat("class ::A::B {};");
3338 }
3339 
3340 TEST_F(FormatTest, BreakInheritanceStyle) {
3341   FormatStyle StyleWithInheritanceBreakBeforeComma = getLLVMStyle();
3342   StyleWithInheritanceBreakBeforeComma.BreakInheritanceList =
3343       FormatStyle::BILS_BeforeComma;
3344   verifyFormat("class MyClass : public X {};",
3345                StyleWithInheritanceBreakBeforeComma);
3346   verifyFormat("class MyClass\n"
3347                "    : public X\n"
3348                "    , public Y {};",
3349                StyleWithInheritanceBreakBeforeComma);
3350   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA\n"
3351                "    : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n"
3352                "    , public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};",
3353                StyleWithInheritanceBreakBeforeComma);
3354   verifyFormat("struct aaaaaaaaaaaaa\n"
3355                "    : public aaaaaaaaaaaaaaaaaaa< // break\n"
3356                "          aaaaaaaaaaaaaaaa> {};",
3357                StyleWithInheritanceBreakBeforeComma);
3358 
3359   FormatStyle StyleWithInheritanceBreakAfterColon = getLLVMStyle();
3360   StyleWithInheritanceBreakAfterColon.BreakInheritanceList =
3361       FormatStyle::BILS_AfterColon;
3362   verifyFormat("class MyClass : public X {};",
3363                StyleWithInheritanceBreakAfterColon);
3364   verifyFormat("class MyClass : public X, public Y {};",
3365                StyleWithInheritanceBreakAfterColon);
3366   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA :\n"
3367                "    public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3368                "    public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};",
3369                StyleWithInheritanceBreakAfterColon);
3370   verifyFormat("struct aaaaaaaaaaaaa :\n"
3371                "    public aaaaaaaaaaaaaaaaaaa< // break\n"
3372                "        aaaaaaaaaaaaaaaa> {};",
3373                StyleWithInheritanceBreakAfterColon);
3374 
3375   FormatStyle StyleWithInheritanceBreakAfterComma = getLLVMStyle();
3376   StyleWithInheritanceBreakAfterComma.BreakInheritanceList =
3377       FormatStyle::BILS_AfterComma;
3378   verifyFormat("class MyClass : public X {};",
3379                StyleWithInheritanceBreakAfterComma);
3380   verifyFormat("class MyClass : public X,\n"
3381                "                public Y {};",
3382                StyleWithInheritanceBreakAfterComma);
3383   verifyFormat(
3384       "class AAAAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3385       "                               public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC "
3386       "{};",
3387       StyleWithInheritanceBreakAfterComma);
3388   verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
3389                "                           aaaaaaaaaaaaaaaa> {};",
3390                StyleWithInheritanceBreakAfterComma);
3391   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
3392                "    : public OnceBreak,\n"
3393                "      public AlwaysBreak,\n"
3394                "      EvenBasesFitInOneLine {};",
3395                StyleWithInheritanceBreakAfterComma);
3396 }
3397 
3398 TEST_F(FormatTest, FormatsVariableDeclarationsAfterRecord) {
3399   verifyFormat("class A {\n} a, b;");
3400   verifyFormat("struct A {\n} a, b;");
3401   verifyFormat("union A {\n} a, b;");
3402 
3403   verifyFormat("constexpr class A {\n} a, b;");
3404   verifyFormat("constexpr struct A {\n} a, b;");
3405   verifyFormat("constexpr union A {\n} a, b;");
3406 
3407   verifyFormat("namespace {\nclass A {\n} a, b;\n} // namespace");
3408   verifyFormat("namespace {\nstruct A {\n} a, b;\n} // namespace");
3409   verifyFormat("namespace {\nunion A {\n} a, b;\n} // namespace");
3410 
3411   verifyFormat("namespace {\nconstexpr class A {\n} a, b;\n} // namespace");
3412   verifyFormat("namespace {\nconstexpr struct A {\n} a, b;\n} // namespace");
3413   verifyFormat("namespace {\nconstexpr union A {\n} a, b;\n} // namespace");
3414 
3415   verifyFormat("namespace ns {\n"
3416                "class {\n"
3417                "} a, b;\n"
3418                "} // namespace ns");
3419   verifyFormat("namespace ns {\n"
3420                "const class {\n"
3421                "} a, b;\n"
3422                "} // namespace ns");
3423   verifyFormat("namespace ns {\n"
3424                "constexpr class C {\n"
3425                "} a, b;\n"
3426                "} // namespace ns");
3427   verifyFormat("namespace ns {\n"
3428                "class { /* comment */\n"
3429                "} a, b;\n"
3430                "} // namespace ns");
3431   verifyFormat("namespace ns {\n"
3432                "const class { /* comment */\n"
3433                "} a, b;\n"
3434                "} // namespace ns");
3435 }
3436 
3437 TEST_F(FormatTest, FormatsEnum) {
3438   verifyFormat("enum {\n"
3439                "  Zero,\n"
3440                "  One = 1,\n"
3441                "  Two = One + 1,\n"
3442                "  Three = (One + Two),\n"
3443                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
3444                "  Five = (One, Two, Three, Four, 5)\n"
3445                "};");
3446   verifyGoogleFormat("enum {\n"
3447                      "  Zero,\n"
3448                      "  One = 1,\n"
3449                      "  Two = One + 1,\n"
3450                      "  Three = (One + Two),\n"
3451                      "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
3452                      "  Five = (One, Two, Three, Four, 5)\n"
3453                      "};");
3454   verifyFormat("enum Enum {};");
3455   verifyFormat("enum {};");
3456   verifyFormat("enum X E {} d;");
3457   verifyFormat("enum __attribute__((...)) E {} d;");
3458   verifyFormat("enum __declspec__((...)) E {} d;");
3459   verifyFormat("enum {\n"
3460                "  Bar = Foo<int, int>::value\n"
3461                "};",
3462                getLLVMStyleWithColumns(30));
3463 
3464   verifyFormat("enum ShortEnum { A, B, C };");
3465   verifyGoogleFormat("enum ShortEnum { A, B, C };");
3466 
3467   EXPECT_EQ("enum KeepEmptyLines {\n"
3468             "  ONE,\n"
3469             "\n"
3470             "  TWO,\n"
3471             "\n"
3472             "  THREE\n"
3473             "}",
3474             format("enum KeepEmptyLines {\n"
3475                    "  ONE,\n"
3476                    "\n"
3477                    "  TWO,\n"
3478                    "\n"
3479                    "\n"
3480                    "  THREE\n"
3481                    "}"));
3482   verifyFormat("enum E { // comment\n"
3483                "  ONE,\n"
3484                "  TWO\n"
3485                "};\n"
3486                "int i;");
3487 
3488   FormatStyle EightIndent = getLLVMStyle();
3489   EightIndent.IndentWidth = 8;
3490   verifyFormat("enum {\n"
3491                "        VOID,\n"
3492                "        CHAR,\n"
3493                "        SHORT,\n"
3494                "        INT,\n"
3495                "        LONG,\n"
3496                "        SIGNED,\n"
3497                "        UNSIGNED,\n"
3498                "        BOOL,\n"
3499                "        FLOAT,\n"
3500                "        DOUBLE,\n"
3501                "        COMPLEX\n"
3502                "};",
3503                EightIndent);
3504 
3505   // Not enums.
3506   verifyFormat("enum X f() {\n"
3507                "  a();\n"
3508                "  return 42;\n"
3509                "}");
3510   verifyFormat("enum X Type::f() {\n"
3511                "  a();\n"
3512                "  return 42;\n"
3513                "}");
3514   verifyFormat("enum ::X f() {\n"
3515                "  a();\n"
3516                "  return 42;\n"
3517                "}");
3518   verifyFormat("enum ns::X f() {\n"
3519                "  a();\n"
3520                "  return 42;\n"
3521                "}");
3522 }
3523 
3524 TEST_F(FormatTest, FormatsEnumsWithErrors) {
3525   verifyFormat("enum Type {\n"
3526                "  One = 0; // These semicolons should be commas.\n"
3527                "  Two = 1;\n"
3528                "};");
3529   verifyFormat("namespace n {\n"
3530                "enum Type {\n"
3531                "  One,\n"
3532                "  Two, // missing };\n"
3533                "  int i;\n"
3534                "}\n"
3535                "void g() {}");
3536 }
3537 
3538 TEST_F(FormatTest, FormatsEnumStruct) {
3539   verifyFormat("enum struct {\n"
3540                "  Zero,\n"
3541                "  One = 1,\n"
3542                "  Two = One + 1,\n"
3543                "  Three = (One + Two),\n"
3544                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
3545                "  Five = (One, Two, Three, Four, 5)\n"
3546                "};");
3547   verifyFormat("enum struct Enum {};");
3548   verifyFormat("enum struct {};");
3549   verifyFormat("enum struct X E {} d;");
3550   verifyFormat("enum struct __attribute__((...)) E {} d;");
3551   verifyFormat("enum struct __declspec__((...)) E {} d;");
3552   verifyFormat("enum struct X f() {\n  a();\n  return 42;\n}");
3553 }
3554 
3555 TEST_F(FormatTest, FormatsEnumClass) {
3556   verifyFormat("enum class {\n"
3557                "  Zero,\n"
3558                "  One = 1,\n"
3559                "  Two = One + 1,\n"
3560                "  Three = (One + Two),\n"
3561                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
3562                "  Five = (One, Two, Three, Four, 5)\n"
3563                "};");
3564   verifyFormat("enum class Enum {};");
3565   verifyFormat("enum class {};");
3566   verifyFormat("enum class X E {} d;");
3567   verifyFormat("enum class __attribute__((...)) E {} d;");
3568   verifyFormat("enum class __declspec__((...)) E {} d;");
3569   verifyFormat("enum class X f() {\n  a();\n  return 42;\n}");
3570 }
3571 
3572 TEST_F(FormatTest, FormatsEnumTypes) {
3573   verifyFormat("enum X : int {\n"
3574                "  A, // Force multiple lines.\n"
3575                "  B\n"
3576                "};");
3577   verifyFormat("enum X : int { A, B };");
3578   verifyFormat("enum X : std::uint32_t { A, B };");
3579 }
3580 
3581 TEST_F(FormatTest, FormatsTypedefEnum) {
3582   FormatStyle Style = getLLVMStyleWithColumns(40);
3583   verifyFormat("typedef enum {} EmptyEnum;");
3584   verifyFormat("typedef enum { A, B, C } ShortEnum;");
3585   verifyFormat("typedef enum {\n"
3586                "  ZERO = 0,\n"
3587                "  ONE = 1,\n"
3588                "  TWO = 2,\n"
3589                "  THREE = 3\n"
3590                "} LongEnum;",
3591                Style);
3592   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
3593   Style.BraceWrapping.AfterEnum = true;
3594   verifyFormat("typedef enum {} EmptyEnum;");
3595   verifyFormat("typedef enum { A, B, C } ShortEnum;");
3596   verifyFormat("typedef enum\n"
3597                "{\n"
3598                "  ZERO = 0,\n"
3599                "  ONE = 1,\n"
3600                "  TWO = 2,\n"
3601                "  THREE = 3\n"
3602                "} LongEnum;",
3603                Style);
3604 }
3605 
3606 TEST_F(FormatTest, FormatsNSEnums) {
3607   verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }");
3608   verifyGoogleFormat(
3609       "typedef NS_CLOSED_ENUM(NSInteger, SomeName) { AAA, BBB }");
3610   verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n"
3611                      "  // Information about someDecentlyLongValue.\n"
3612                      "  someDecentlyLongValue,\n"
3613                      "  // Information about anotherDecentlyLongValue.\n"
3614                      "  anotherDecentlyLongValue,\n"
3615                      "  // Information about aThirdDecentlyLongValue.\n"
3616                      "  aThirdDecentlyLongValue\n"
3617                      "};");
3618   verifyGoogleFormat("typedef NS_CLOSED_ENUM(NSInteger, MyType) {\n"
3619                      "  // Information about someDecentlyLongValue.\n"
3620                      "  someDecentlyLongValue,\n"
3621                      "  // Information about anotherDecentlyLongValue.\n"
3622                      "  anotherDecentlyLongValue,\n"
3623                      "  // Information about aThirdDecentlyLongValue.\n"
3624                      "  aThirdDecentlyLongValue\n"
3625                      "};");
3626   verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n"
3627                      "  a = 1,\n"
3628                      "  b = 2,\n"
3629                      "  c = 3,\n"
3630                      "};");
3631   verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n"
3632                      "  a = 1,\n"
3633                      "  b = 2,\n"
3634                      "  c = 3,\n"
3635                      "};");
3636   verifyGoogleFormat("typedef CF_CLOSED_ENUM(NSInteger, MyType) {\n"
3637                      "  a = 1,\n"
3638                      "  b = 2,\n"
3639                      "  c = 3,\n"
3640                      "};");
3641   verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n"
3642                      "  a = 1,\n"
3643                      "  b = 2,\n"
3644                      "  c = 3,\n"
3645                      "};");
3646 }
3647 
3648 TEST_F(FormatTest, FormatsBitfields) {
3649   verifyFormat("struct Bitfields {\n"
3650                "  unsigned sClass : 8;\n"
3651                "  unsigned ValueKind : 2;\n"
3652                "};");
3653   verifyFormat("struct A {\n"
3654                "  int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n"
3655                "      bbbbbbbbbbbbbbbbbbbbbbbbb;\n"
3656                "};");
3657   verifyFormat("struct MyStruct {\n"
3658                "  uchar data;\n"
3659                "  uchar : 8;\n"
3660                "  uchar : 8;\n"
3661                "  uchar other;\n"
3662                "};");
3663   FormatStyle Style = getLLVMStyle();
3664   Style.BitFieldColonSpacing = FormatStyle::BFCS_None;
3665   verifyFormat("struct Bitfields {\n"
3666                "  unsigned sClass:8;\n"
3667                "  unsigned ValueKind:2;\n"
3668                "  uchar other;\n"
3669                "};",
3670                Style);
3671   verifyFormat("struct A {\n"
3672                "  int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:1,\n"
3673                "      bbbbbbbbbbbbbbbbbbbbbbbbb:2;\n"
3674                "};",
3675                Style);
3676   Style.BitFieldColonSpacing = FormatStyle::BFCS_Before;
3677   verifyFormat("struct Bitfields {\n"
3678                "  unsigned sClass :8;\n"
3679                "  unsigned ValueKind :2;\n"
3680                "  uchar other;\n"
3681                "};",
3682                Style);
3683   Style.BitFieldColonSpacing = FormatStyle::BFCS_After;
3684   verifyFormat("struct Bitfields {\n"
3685                "  unsigned sClass: 8;\n"
3686                "  unsigned ValueKind: 2;\n"
3687                "  uchar other;\n"
3688                "};",
3689                Style);
3690 }
3691 
3692 TEST_F(FormatTest, FormatsNamespaces) {
3693   FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle();
3694   LLVMWithNoNamespaceFix.FixNamespaceComments = false;
3695 
3696   verifyFormat("namespace some_namespace {\n"
3697                "class A {};\n"
3698                "void f() { f(); }\n"
3699                "}",
3700                LLVMWithNoNamespaceFix);
3701   verifyFormat("namespace N::inline D {\n"
3702                "class A {};\n"
3703                "void f() { f(); }\n"
3704                "}",
3705                LLVMWithNoNamespaceFix);
3706   verifyFormat("namespace N::inline D::E {\n"
3707                "class A {};\n"
3708                "void f() { f(); }\n"
3709                "}",
3710                LLVMWithNoNamespaceFix);
3711   verifyFormat("namespace [[deprecated(\"foo[bar\")]] some_namespace {\n"
3712                "class A {};\n"
3713                "void f() { f(); }\n"
3714                "}",
3715                LLVMWithNoNamespaceFix);
3716   verifyFormat("/* something */ namespace some_namespace {\n"
3717                "class A {};\n"
3718                "void f() { f(); }\n"
3719                "}",
3720                LLVMWithNoNamespaceFix);
3721   verifyFormat("namespace {\n"
3722                "class A {};\n"
3723                "void f() { f(); }\n"
3724                "}",
3725                LLVMWithNoNamespaceFix);
3726   verifyFormat("/* something */ namespace {\n"
3727                "class A {};\n"
3728                "void f() { f(); }\n"
3729                "}",
3730                LLVMWithNoNamespaceFix);
3731   verifyFormat("inline namespace X {\n"
3732                "class A {};\n"
3733                "void f() { f(); }\n"
3734                "}",
3735                LLVMWithNoNamespaceFix);
3736   verifyFormat("/* something */ inline namespace X {\n"
3737                "class A {};\n"
3738                "void f() { f(); }\n"
3739                "}",
3740                LLVMWithNoNamespaceFix);
3741   verifyFormat("export namespace X {\n"
3742                "class A {};\n"
3743                "void f() { f(); }\n"
3744                "}",
3745                LLVMWithNoNamespaceFix);
3746   verifyFormat("using namespace some_namespace;\n"
3747                "class A {};\n"
3748                "void f() { f(); }",
3749                LLVMWithNoNamespaceFix);
3750 
3751   // This code is more common than we thought; if we
3752   // layout this correctly the semicolon will go into
3753   // its own line, which is undesirable.
3754   verifyFormat("namespace {};", LLVMWithNoNamespaceFix);
3755   verifyFormat("namespace {\n"
3756                "class A {};\n"
3757                "};",
3758                LLVMWithNoNamespaceFix);
3759 
3760   verifyFormat("namespace {\n"
3761                "int SomeVariable = 0; // comment\n"
3762                "} // namespace",
3763                LLVMWithNoNamespaceFix);
3764   EXPECT_EQ("#ifndef HEADER_GUARD\n"
3765             "#define HEADER_GUARD\n"
3766             "namespace my_namespace {\n"
3767             "int i;\n"
3768             "} // my_namespace\n"
3769             "#endif // HEADER_GUARD",
3770             format("#ifndef HEADER_GUARD\n"
3771                    " #define HEADER_GUARD\n"
3772                    "   namespace my_namespace {\n"
3773                    "int i;\n"
3774                    "}    // my_namespace\n"
3775                    "#endif    // HEADER_GUARD",
3776                    LLVMWithNoNamespaceFix));
3777 
3778   EXPECT_EQ("namespace A::B {\n"
3779             "class C {};\n"
3780             "}",
3781             format("namespace A::B {\n"
3782                    "class C {};\n"
3783                    "}",
3784                    LLVMWithNoNamespaceFix));
3785 
3786   FormatStyle Style = getLLVMStyle();
3787   Style.NamespaceIndentation = FormatStyle::NI_All;
3788   EXPECT_EQ("namespace out {\n"
3789             "  int i;\n"
3790             "  namespace in {\n"
3791             "    int i;\n"
3792             "  } // namespace in\n"
3793             "} // namespace out",
3794             format("namespace out {\n"
3795                    "int i;\n"
3796                    "namespace in {\n"
3797                    "int i;\n"
3798                    "} // namespace in\n"
3799                    "} // namespace out",
3800                    Style));
3801 
3802   FormatStyle ShortInlineFunctions = getLLVMStyle();
3803   ShortInlineFunctions.NamespaceIndentation = FormatStyle::NI_All;
3804   ShortInlineFunctions.AllowShortFunctionsOnASingleLine =
3805       FormatStyle::SFS_Inline;
3806   verifyFormat("namespace {\n"
3807                "  void f() {\n"
3808                "    return;\n"
3809                "  }\n"
3810                "} // namespace\n",
3811                ShortInlineFunctions);
3812   verifyFormat("namespace { /* comment */\n"
3813                "  void f() {\n"
3814                "    return;\n"
3815                "  }\n"
3816                "} // namespace\n",
3817                ShortInlineFunctions);
3818   verifyFormat("namespace { // comment\n"
3819                "  void f() {\n"
3820                "    return;\n"
3821                "  }\n"
3822                "} // namespace\n",
3823                ShortInlineFunctions);
3824   verifyFormat("namespace {\n"
3825                "  int some_int;\n"
3826                "  void f() {\n"
3827                "    return;\n"
3828                "  }\n"
3829                "} // namespace\n",
3830                ShortInlineFunctions);
3831   verifyFormat("namespace interface {\n"
3832                "  void f() {\n"
3833                "    return;\n"
3834                "  }\n"
3835                "} // namespace interface\n",
3836                ShortInlineFunctions);
3837   verifyFormat("namespace {\n"
3838                "  class X {\n"
3839                "    void f() { return; }\n"
3840                "  };\n"
3841                "} // namespace\n",
3842                ShortInlineFunctions);
3843   verifyFormat("namespace {\n"
3844                "  class X { /* comment */\n"
3845                "    void f() { return; }\n"
3846                "  };\n"
3847                "} // namespace\n",
3848                ShortInlineFunctions);
3849   verifyFormat("namespace {\n"
3850                "  class X { // comment\n"
3851                "    void f() { return; }\n"
3852                "  };\n"
3853                "} // namespace\n",
3854                ShortInlineFunctions);
3855   verifyFormat("namespace {\n"
3856                "  struct X {\n"
3857                "    void f() { return; }\n"
3858                "  };\n"
3859                "} // namespace\n",
3860                ShortInlineFunctions);
3861   verifyFormat("namespace {\n"
3862                "  union X {\n"
3863                "    void f() { return; }\n"
3864                "  };\n"
3865                "} // namespace\n",
3866                ShortInlineFunctions);
3867   verifyFormat("extern \"C\" {\n"
3868                "void f() {\n"
3869                "  return;\n"
3870                "}\n"
3871                "} // namespace\n",
3872                ShortInlineFunctions);
3873   verifyFormat("namespace {\n"
3874                "  class X {\n"
3875                "    void f() { return; }\n"
3876                "  } x;\n"
3877                "} // namespace\n",
3878                ShortInlineFunctions);
3879   verifyFormat("namespace {\n"
3880                "  [[nodiscard]] class X {\n"
3881                "    void f() { return; }\n"
3882                "  };\n"
3883                "} // namespace\n",
3884                ShortInlineFunctions);
3885   verifyFormat("namespace {\n"
3886                "  static class X {\n"
3887                "    void f() { return; }\n"
3888                "  } x;\n"
3889                "} // namespace\n",
3890                ShortInlineFunctions);
3891   verifyFormat("namespace {\n"
3892                "  constexpr class X {\n"
3893                "    void f() { return; }\n"
3894                "  } x;\n"
3895                "} // namespace\n",
3896                ShortInlineFunctions);
3897 
3898   ShortInlineFunctions.IndentExternBlock = FormatStyle::IEBS_Indent;
3899   verifyFormat("extern \"C\" {\n"
3900                "  void f() {\n"
3901                "    return;\n"
3902                "  }\n"
3903                "} // namespace\n",
3904                ShortInlineFunctions);
3905 
3906   Style.NamespaceIndentation = FormatStyle::NI_Inner;
3907   EXPECT_EQ("namespace out {\n"
3908             "int i;\n"
3909             "namespace in {\n"
3910             "  int i;\n"
3911             "} // namespace in\n"
3912             "} // namespace out",
3913             format("namespace out {\n"
3914                    "int i;\n"
3915                    "namespace in {\n"
3916                    "int i;\n"
3917                    "} // namespace in\n"
3918                    "} // namespace out",
3919                    Style));
3920 
3921   Style.NamespaceIndentation = FormatStyle::NI_None;
3922   verifyFormat("template <class T>\n"
3923                "concept a_concept = X<>;\n"
3924                "namespace B {\n"
3925                "struct b_struct {};\n"
3926                "} // namespace B\n",
3927                Style);
3928   verifyFormat("template <int I>\n"
3929                "constexpr void foo()\n"
3930                "  requires(I == 42)\n"
3931                "{}\n"
3932                "namespace ns {\n"
3933                "void foo() {}\n"
3934                "} // namespace ns\n",
3935                Style);
3936 }
3937 
3938 TEST_F(FormatTest, NamespaceMacros) {
3939   FormatStyle Style = getLLVMStyle();
3940   Style.NamespaceMacros.push_back("TESTSUITE");
3941 
3942   verifyFormat("TESTSUITE(A) {\n"
3943                "int foo();\n"
3944                "} // TESTSUITE(A)",
3945                Style);
3946 
3947   verifyFormat("TESTSUITE(A, B) {\n"
3948                "int foo();\n"
3949                "} // TESTSUITE(A)",
3950                Style);
3951 
3952   // Properly indent according to NamespaceIndentation style
3953   Style.NamespaceIndentation = FormatStyle::NI_All;
3954   verifyFormat("TESTSUITE(A) {\n"
3955                "  int foo();\n"
3956                "} // TESTSUITE(A)",
3957                Style);
3958   verifyFormat("TESTSUITE(A) {\n"
3959                "  namespace B {\n"
3960                "    int foo();\n"
3961                "  } // namespace B\n"
3962                "} // TESTSUITE(A)",
3963                Style);
3964   verifyFormat("namespace A {\n"
3965                "  TESTSUITE(B) {\n"
3966                "    int foo();\n"
3967                "  } // TESTSUITE(B)\n"
3968                "} // namespace A",
3969                Style);
3970 
3971   Style.NamespaceIndentation = FormatStyle::NI_Inner;
3972   verifyFormat("TESTSUITE(A) {\n"
3973                "TESTSUITE(B) {\n"
3974                "  int foo();\n"
3975                "} // TESTSUITE(B)\n"
3976                "} // TESTSUITE(A)",
3977                Style);
3978   verifyFormat("TESTSUITE(A) {\n"
3979                "namespace B {\n"
3980                "  int foo();\n"
3981                "} // namespace B\n"
3982                "} // TESTSUITE(A)",
3983                Style);
3984   verifyFormat("namespace A {\n"
3985                "TESTSUITE(B) {\n"
3986                "  int foo();\n"
3987                "} // TESTSUITE(B)\n"
3988                "} // namespace A",
3989                Style);
3990 
3991   // Properly merge namespace-macros blocks in CompactNamespaces mode
3992   Style.NamespaceIndentation = FormatStyle::NI_None;
3993   Style.CompactNamespaces = true;
3994   verifyFormat("TESTSUITE(A) { TESTSUITE(B) {\n"
3995                "}} // TESTSUITE(A::B)",
3996                Style);
3997 
3998   EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n"
3999             "}} // TESTSUITE(out::in)",
4000             format("TESTSUITE(out) {\n"
4001                    "TESTSUITE(in) {\n"
4002                    "} // TESTSUITE(in)\n"
4003                    "} // TESTSUITE(out)",
4004                    Style));
4005 
4006   EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n"
4007             "}} // TESTSUITE(out::in)",
4008             format("TESTSUITE(out) {\n"
4009                    "TESTSUITE(in) {\n"
4010                    "} // TESTSUITE(in)\n"
4011                    "} // TESTSUITE(out)",
4012                    Style));
4013 
4014   // Do not merge different namespaces/macros
4015   EXPECT_EQ("namespace out {\n"
4016             "TESTSUITE(in) {\n"
4017             "} // TESTSUITE(in)\n"
4018             "} // namespace out",
4019             format("namespace out {\n"
4020                    "TESTSUITE(in) {\n"
4021                    "} // TESTSUITE(in)\n"
4022                    "} // namespace out",
4023                    Style));
4024   EXPECT_EQ("TESTSUITE(out) {\n"
4025             "namespace in {\n"
4026             "} // namespace in\n"
4027             "} // TESTSUITE(out)",
4028             format("TESTSUITE(out) {\n"
4029                    "namespace in {\n"
4030                    "} // namespace in\n"
4031                    "} // TESTSUITE(out)",
4032                    Style));
4033   Style.NamespaceMacros.push_back("FOOBAR");
4034   EXPECT_EQ("TESTSUITE(out) {\n"
4035             "FOOBAR(in) {\n"
4036             "} // FOOBAR(in)\n"
4037             "} // TESTSUITE(out)",
4038             format("TESTSUITE(out) {\n"
4039                    "FOOBAR(in) {\n"
4040                    "} // FOOBAR(in)\n"
4041                    "} // TESTSUITE(out)",
4042                    Style));
4043 }
4044 
4045 TEST_F(FormatTest, FormatsCompactNamespaces) {
4046   FormatStyle Style = getLLVMStyle();
4047   Style.CompactNamespaces = true;
4048   Style.NamespaceMacros.push_back("TESTSUITE");
4049 
4050   verifyFormat("namespace A { namespace B {\n"
4051                "}} // namespace A::B",
4052                Style);
4053 
4054   EXPECT_EQ("namespace out { namespace in {\n"
4055             "}} // namespace out::in",
4056             format("namespace out {\n"
4057                    "namespace in {\n"
4058                    "} // namespace in\n"
4059                    "} // namespace out",
4060                    Style));
4061 
4062   // Only namespaces which have both consecutive opening and end get compacted
4063   EXPECT_EQ("namespace out {\n"
4064             "namespace in1 {\n"
4065             "} // namespace in1\n"
4066             "namespace in2 {\n"
4067             "} // namespace in2\n"
4068             "} // namespace out",
4069             format("namespace out {\n"
4070                    "namespace in1 {\n"
4071                    "} // namespace in1\n"
4072                    "namespace in2 {\n"
4073                    "} // namespace in2\n"
4074                    "} // namespace out",
4075                    Style));
4076 
4077   EXPECT_EQ("namespace out {\n"
4078             "int i;\n"
4079             "namespace in {\n"
4080             "int j;\n"
4081             "} // namespace in\n"
4082             "int k;\n"
4083             "} // namespace out",
4084             format("namespace out { int i;\n"
4085                    "namespace in { int j; } // namespace in\n"
4086                    "int k; } // namespace out",
4087                    Style));
4088 
4089   EXPECT_EQ("namespace A { namespace B { namespace C {\n"
4090             "}}} // namespace A::B::C\n",
4091             format("namespace A { namespace B {\n"
4092                    "namespace C {\n"
4093                    "}} // namespace B::C\n"
4094                    "} // namespace A\n",
4095                    Style));
4096 
4097   Style.ColumnLimit = 40;
4098   EXPECT_EQ("namespace aaaaaaaaaa {\n"
4099             "namespace bbbbbbbbbb {\n"
4100             "}} // namespace aaaaaaaaaa::bbbbbbbbbb",
4101             format("namespace aaaaaaaaaa {\n"
4102                    "namespace bbbbbbbbbb {\n"
4103                    "} // namespace bbbbbbbbbb\n"
4104                    "} // namespace aaaaaaaaaa",
4105                    Style));
4106 
4107   EXPECT_EQ("namespace aaaaaa { namespace bbbbbb {\n"
4108             "namespace cccccc {\n"
4109             "}}} // namespace aaaaaa::bbbbbb::cccccc",
4110             format("namespace aaaaaa {\n"
4111                    "namespace bbbbbb {\n"
4112                    "namespace cccccc {\n"
4113                    "} // namespace cccccc\n"
4114                    "} // namespace bbbbbb\n"
4115                    "} // namespace aaaaaa",
4116                    Style));
4117   Style.ColumnLimit = 80;
4118 
4119   // Extra semicolon after 'inner' closing brace prevents merging
4120   EXPECT_EQ("namespace out { namespace in {\n"
4121             "}; } // namespace out::in",
4122             format("namespace out {\n"
4123                    "namespace in {\n"
4124                    "}; // namespace in\n"
4125                    "} // namespace out",
4126                    Style));
4127 
4128   // Extra semicolon after 'outer' closing brace is conserved
4129   EXPECT_EQ("namespace out { namespace in {\n"
4130             "}}; // namespace out::in",
4131             format("namespace out {\n"
4132                    "namespace in {\n"
4133                    "} // namespace in\n"
4134                    "}; // namespace out",
4135                    Style));
4136 
4137   Style.NamespaceIndentation = FormatStyle::NI_All;
4138   EXPECT_EQ("namespace out { namespace in {\n"
4139             "  int i;\n"
4140             "}} // namespace out::in",
4141             format("namespace out {\n"
4142                    "namespace in {\n"
4143                    "int i;\n"
4144                    "} // namespace in\n"
4145                    "} // namespace out",
4146                    Style));
4147   EXPECT_EQ("namespace out { namespace mid {\n"
4148             "  namespace in {\n"
4149             "    int j;\n"
4150             "  } // namespace in\n"
4151             "  int k;\n"
4152             "}} // namespace out::mid",
4153             format("namespace out { namespace mid {\n"
4154                    "namespace in { int j; } // namespace in\n"
4155                    "int k; }} // namespace out::mid",
4156                    Style));
4157 
4158   Style.NamespaceIndentation = FormatStyle::NI_Inner;
4159   EXPECT_EQ("namespace out { namespace in {\n"
4160             "  int i;\n"
4161             "}} // namespace out::in",
4162             format("namespace out {\n"
4163                    "namespace in {\n"
4164                    "int i;\n"
4165                    "} // namespace in\n"
4166                    "} // namespace out",
4167                    Style));
4168   EXPECT_EQ("namespace out { namespace mid { namespace in {\n"
4169             "  int i;\n"
4170             "}}} // namespace out::mid::in",
4171             format("namespace out {\n"
4172                    "namespace mid {\n"
4173                    "namespace in {\n"
4174                    "int i;\n"
4175                    "} // namespace in\n"
4176                    "} // namespace mid\n"
4177                    "} // namespace out",
4178                    Style));
4179 
4180   Style.CompactNamespaces = true;
4181   Style.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
4182   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4183   Style.BraceWrapping.BeforeLambdaBody = true;
4184   verifyFormat("namespace out { namespace in {\n"
4185                "}} // namespace out::in",
4186                Style);
4187   EXPECT_EQ("namespace out { namespace in {\n"
4188             "}} // namespace out::in",
4189             format("namespace out {\n"
4190                    "namespace in {\n"
4191                    "} // namespace in\n"
4192                    "} // namespace out",
4193                    Style));
4194 }
4195 
4196 TEST_F(FormatTest, FormatsExternC) {
4197   verifyFormat("extern \"C\" {\nint a;");
4198   verifyFormat("extern \"C\" {}");
4199   verifyFormat("extern \"C\" {\n"
4200                "int foo();\n"
4201                "}");
4202   verifyFormat("extern \"C\" int foo() {}");
4203   verifyFormat("extern \"C\" int foo();");
4204   verifyFormat("extern \"C\" int foo() {\n"
4205                "  int i = 42;\n"
4206                "  return i;\n"
4207                "}");
4208 
4209   FormatStyle Style = getLLVMStyle();
4210   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4211   Style.BraceWrapping.AfterFunction = true;
4212   verifyFormat("extern \"C\" int foo() {}", Style);
4213   verifyFormat("extern \"C\" int foo();", Style);
4214   verifyFormat("extern \"C\" int foo()\n"
4215                "{\n"
4216                "  int i = 42;\n"
4217                "  return i;\n"
4218                "}",
4219                Style);
4220 
4221   Style.BraceWrapping.AfterExternBlock = true;
4222   Style.BraceWrapping.SplitEmptyRecord = false;
4223   verifyFormat("extern \"C\"\n"
4224                "{}",
4225                Style);
4226   verifyFormat("extern \"C\"\n"
4227                "{\n"
4228                "  int foo();\n"
4229                "}",
4230                Style);
4231 }
4232 
4233 TEST_F(FormatTest, IndentExternBlockStyle) {
4234   FormatStyle Style = getLLVMStyle();
4235   Style.IndentWidth = 2;
4236 
4237   Style.IndentExternBlock = FormatStyle::IEBS_Indent;
4238   verifyFormat("extern \"C\" { /*9*/\n"
4239                "}",
4240                Style);
4241   verifyFormat("extern \"C\" {\n"
4242                "  int foo10();\n"
4243                "}",
4244                Style);
4245 
4246   Style.IndentExternBlock = FormatStyle::IEBS_NoIndent;
4247   verifyFormat("extern \"C\" { /*11*/\n"
4248                "}",
4249                Style);
4250   verifyFormat("extern \"C\" {\n"
4251                "int foo12();\n"
4252                "}",
4253                Style);
4254 
4255   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4256   Style.BraceWrapping.AfterExternBlock = true;
4257   Style.IndentExternBlock = FormatStyle::IEBS_Indent;
4258   verifyFormat("extern \"C\"\n"
4259                "{ /*13*/\n"
4260                "}",
4261                Style);
4262   verifyFormat("extern \"C\"\n{\n"
4263                "  int foo14();\n"
4264                "}",
4265                Style);
4266 
4267   Style.BraceWrapping.AfterExternBlock = false;
4268   Style.IndentExternBlock = FormatStyle::IEBS_NoIndent;
4269   verifyFormat("extern \"C\" { /*15*/\n"
4270                "}",
4271                Style);
4272   verifyFormat("extern \"C\" {\n"
4273                "int foo16();\n"
4274                "}",
4275                Style);
4276 
4277   Style.BraceWrapping.AfterExternBlock = true;
4278   verifyFormat("extern \"C\"\n"
4279                "{ /*13*/\n"
4280                "}",
4281                Style);
4282   verifyFormat("extern \"C\"\n"
4283                "{\n"
4284                "int foo14();\n"
4285                "}",
4286                Style);
4287 
4288   Style.IndentExternBlock = FormatStyle::IEBS_Indent;
4289   verifyFormat("extern \"C\"\n"
4290                "{ /*13*/\n"
4291                "}",
4292                Style);
4293   verifyFormat("extern \"C\"\n"
4294                "{\n"
4295                "  int foo14();\n"
4296                "}",
4297                Style);
4298 }
4299 
4300 TEST_F(FormatTest, FormatsInlineASM) {
4301   verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
4302   verifyFormat("asm(\"nop\" ::: \"memory\");");
4303   verifyFormat(
4304       "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
4305       "    \"cpuid\\n\\t\"\n"
4306       "    \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
4307       "    : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
4308       "    : \"a\"(value));");
4309   EXPECT_EQ(
4310       "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
4311       "  __asm {\n"
4312       "        mov     edx,[that] // vtable in edx\n"
4313       "        mov     eax,methodIndex\n"
4314       "        call    [edx][eax*4] // stdcall\n"
4315       "  }\n"
4316       "}",
4317       format("void NS_InvokeByIndex(void *that,   unsigned int methodIndex) {\n"
4318              "    __asm {\n"
4319              "        mov     edx,[that] // vtable in edx\n"
4320              "        mov     eax,methodIndex\n"
4321              "        call    [edx][eax*4] // stdcall\n"
4322              "    }\n"
4323              "}"));
4324   EXPECT_EQ("_asm {\n"
4325             "  xor eax, eax;\n"
4326             "  cpuid;\n"
4327             "}",
4328             format("_asm {\n"
4329                    "  xor eax, eax;\n"
4330                    "  cpuid;\n"
4331                    "}"));
4332   verifyFormat("void function() {\n"
4333                "  // comment\n"
4334                "  asm(\"\");\n"
4335                "}");
4336   EXPECT_EQ("__asm {\n"
4337             "}\n"
4338             "int i;",
4339             format("__asm   {\n"
4340                    "}\n"
4341                    "int   i;"));
4342 }
4343 
4344 TEST_F(FormatTest, FormatTryCatch) {
4345   verifyFormat("try {\n"
4346                "  throw a * b;\n"
4347                "} catch (int a) {\n"
4348                "  // Do nothing.\n"
4349                "} catch (...) {\n"
4350                "  exit(42);\n"
4351                "}");
4352 
4353   // Function-level try statements.
4354   verifyFormat("int f() try { return 4; } catch (...) {\n"
4355                "  return 5;\n"
4356                "}");
4357   verifyFormat("class A {\n"
4358                "  int a;\n"
4359                "  A() try : a(0) {\n"
4360                "  } catch (...) {\n"
4361                "    throw;\n"
4362                "  }\n"
4363                "};\n");
4364   verifyFormat("class A {\n"
4365                "  int a;\n"
4366                "  A() try : a(0), b{1} {\n"
4367                "  } catch (...) {\n"
4368                "    throw;\n"
4369                "  }\n"
4370                "};\n");
4371   verifyFormat("class A {\n"
4372                "  int a;\n"
4373                "  A() try : a(0), b{1}, c{2} {\n"
4374                "  } catch (...) {\n"
4375                "    throw;\n"
4376                "  }\n"
4377                "};\n");
4378   verifyFormat("class A {\n"
4379                "  int a;\n"
4380                "  A() try : a(0), b{1}, c{2} {\n"
4381                "    { // New scope.\n"
4382                "    }\n"
4383                "  } catch (...) {\n"
4384                "    throw;\n"
4385                "  }\n"
4386                "};\n");
4387 
4388   // Incomplete try-catch blocks.
4389   verifyIncompleteFormat("try {} catch (");
4390 }
4391 
4392 TEST_F(FormatTest, FormatTryAsAVariable) {
4393   verifyFormat("int try;");
4394   verifyFormat("int try, size;");
4395   verifyFormat("try = foo();");
4396   verifyFormat("if (try < size) {\n  return true;\n}");
4397 
4398   verifyFormat("int catch;");
4399   verifyFormat("int catch, size;");
4400   verifyFormat("catch = foo();");
4401   verifyFormat("if (catch < size) {\n  return true;\n}");
4402 
4403   FormatStyle Style = getLLVMStyle();
4404   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4405   Style.BraceWrapping.AfterFunction = true;
4406   Style.BraceWrapping.BeforeCatch = true;
4407   verifyFormat("try {\n"
4408                "  int bar = 1;\n"
4409                "}\n"
4410                "catch (...) {\n"
4411                "  int bar = 1;\n"
4412                "}",
4413                Style);
4414   verifyFormat("#if NO_EX\n"
4415                "try\n"
4416                "#endif\n"
4417                "{\n"
4418                "}\n"
4419                "#if NO_EX\n"
4420                "catch (...) {\n"
4421                "}",
4422                Style);
4423   verifyFormat("try /* abc */ {\n"
4424                "  int bar = 1;\n"
4425                "}\n"
4426                "catch (...) {\n"
4427                "  int bar = 1;\n"
4428                "}",
4429                Style);
4430   verifyFormat("try\n"
4431                "// abc\n"
4432                "{\n"
4433                "  int bar = 1;\n"
4434                "}\n"
4435                "catch (...) {\n"
4436                "  int bar = 1;\n"
4437                "}",
4438                Style);
4439 }
4440 
4441 TEST_F(FormatTest, FormatSEHTryCatch) {
4442   verifyFormat("__try {\n"
4443                "  int a = b * c;\n"
4444                "} __except (EXCEPTION_EXECUTE_HANDLER) {\n"
4445                "  // Do nothing.\n"
4446                "}");
4447 
4448   verifyFormat("__try {\n"
4449                "  int a = b * c;\n"
4450                "} __finally {\n"
4451                "  // Do nothing.\n"
4452                "}");
4453 
4454   verifyFormat("DEBUG({\n"
4455                "  __try {\n"
4456                "  } __finally {\n"
4457                "  }\n"
4458                "});\n");
4459 }
4460 
4461 TEST_F(FormatTest, IncompleteTryCatchBlocks) {
4462   verifyFormat("try {\n"
4463                "  f();\n"
4464                "} catch {\n"
4465                "  g();\n"
4466                "}");
4467   verifyFormat("try {\n"
4468                "  f();\n"
4469                "} catch (A a) MACRO(x) {\n"
4470                "  g();\n"
4471                "} catch (B b) MACRO(x) {\n"
4472                "  g();\n"
4473                "}");
4474 }
4475 
4476 TEST_F(FormatTest, FormatTryCatchBraceStyles) {
4477   FormatStyle Style = getLLVMStyle();
4478   for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla,
4479                           FormatStyle::BS_WebKit}) {
4480     Style.BreakBeforeBraces = BraceStyle;
4481     verifyFormat("try {\n"
4482                  "  // something\n"
4483                  "} catch (...) {\n"
4484                  "  // something\n"
4485                  "}",
4486                  Style);
4487   }
4488   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
4489   verifyFormat("try {\n"
4490                "  // something\n"
4491                "}\n"
4492                "catch (...) {\n"
4493                "  // something\n"
4494                "}",
4495                Style);
4496   verifyFormat("__try {\n"
4497                "  // something\n"
4498                "}\n"
4499                "__finally {\n"
4500                "  // something\n"
4501                "}",
4502                Style);
4503   verifyFormat("@try {\n"
4504                "  // something\n"
4505                "}\n"
4506                "@finally {\n"
4507                "  // something\n"
4508                "}",
4509                Style);
4510   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
4511   verifyFormat("try\n"
4512                "{\n"
4513                "  // something\n"
4514                "}\n"
4515                "catch (...)\n"
4516                "{\n"
4517                "  // something\n"
4518                "}",
4519                Style);
4520   Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
4521   verifyFormat("try\n"
4522                "  {\n"
4523                "  // something white\n"
4524                "  }\n"
4525                "catch (...)\n"
4526                "  {\n"
4527                "  // something white\n"
4528                "  }",
4529                Style);
4530   Style.BreakBeforeBraces = FormatStyle::BS_GNU;
4531   verifyFormat("try\n"
4532                "  {\n"
4533                "    // something\n"
4534                "  }\n"
4535                "catch (...)\n"
4536                "  {\n"
4537                "    // something\n"
4538                "  }",
4539                Style);
4540   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4541   Style.BraceWrapping.BeforeCatch = true;
4542   verifyFormat("try {\n"
4543                "  // something\n"
4544                "}\n"
4545                "catch (...) {\n"
4546                "  // something\n"
4547                "}",
4548                Style);
4549 }
4550 
4551 TEST_F(FormatTest, StaticInitializers) {
4552   verifyFormat("static SomeClass SC = {1, 'a'};");
4553 
4554   verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n"
4555                "    100000000, "
4556                "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};");
4557 
4558   // Here, everything other than the "}" would fit on a line.
4559   verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
4560                "    10000000000000000000000000};");
4561   EXPECT_EQ("S s = {a,\n"
4562             "\n"
4563             "       b};",
4564             format("S s = {\n"
4565                    "  a,\n"
4566                    "\n"
4567                    "  b\n"
4568                    "};"));
4569 
4570   // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
4571   // line. However, the formatting looks a bit off and this probably doesn't
4572   // happen often in practice.
4573   verifyFormat("static int Variable[1] = {\n"
4574                "    {1000000000000000000000000000000000000}};",
4575                getLLVMStyleWithColumns(40));
4576 }
4577 
4578 TEST_F(FormatTest, DesignatedInitializers) {
4579   verifyFormat("const struct A a = {.a = 1, .b = 2};");
4580   verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n"
4581                "                    .bbbbbbbbbb = 2,\n"
4582                "                    .cccccccccc = 3,\n"
4583                "                    .dddddddddd = 4,\n"
4584                "                    .eeeeeeeeee = 5};");
4585   verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
4586                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
4587                "    .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
4588                "    .ccccccccccccccccccccccccccc = 3,\n"
4589                "    .ddddddddddddddddddddddddddd = 4,\n"
4590                "    .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};");
4591 
4592   verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
4593 
4594   verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
4595   verifyFormat("const struct A a = {[1] = aaaaaaaaaa,\n"
4596                "                    [2] = bbbbbbbbbb,\n"
4597                "                    [3] = cccccccccc,\n"
4598                "                    [4] = dddddddddd,\n"
4599                "                    [5] = eeeeeeeeee};");
4600   verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
4601                "    [1] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4602                "    [2] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
4603                "    [3] = cccccccccccccccccccccccccccccccccccccc,\n"
4604                "    [4] = dddddddddddddddddddddddddddddddddddddd,\n"
4605                "    [5] = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee};");
4606 }
4607 
4608 TEST_F(FormatTest, NestedStaticInitializers) {
4609   verifyFormat("static A x = {{{}}};\n");
4610   verifyFormat("static A x = {{{init1, init2, init3, init4},\n"
4611                "               {init1, init2, init3, init4}}};",
4612                getLLVMStyleWithColumns(50));
4613 
4614   verifyFormat("somes Status::global_reps[3] = {\n"
4615                "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
4616                "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
4617                "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};",
4618                getLLVMStyleWithColumns(60));
4619   verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
4620                      "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
4621                      "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
4622                      "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
4623   verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n"
4624                "                  {rect.fRight - rect.fLeft, rect.fBottom - "
4625                "rect.fTop}};");
4626 
4627   verifyFormat(
4628       "SomeArrayOfSomeType a = {\n"
4629       "    {{1, 2, 3},\n"
4630       "     {1, 2, 3},\n"
4631       "     {111111111111111111111111111111, 222222222222222222222222222222,\n"
4632       "      333333333333333333333333333333},\n"
4633       "     {1, 2, 3},\n"
4634       "     {1, 2, 3}}};");
4635   verifyFormat(
4636       "SomeArrayOfSomeType a = {\n"
4637       "    {{1, 2, 3}},\n"
4638       "    {{1, 2, 3}},\n"
4639       "    {{111111111111111111111111111111, 222222222222222222222222222222,\n"
4640       "      333333333333333333333333333333}},\n"
4641       "    {{1, 2, 3}},\n"
4642       "    {{1, 2, 3}}};");
4643 
4644   verifyFormat("struct {\n"
4645                "  unsigned bit;\n"
4646                "  const char *const name;\n"
4647                "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n"
4648                "                 {kOsWin, \"Windows\"},\n"
4649                "                 {kOsLinux, \"Linux\"},\n"
4650                "                 {kOsCrOS, \"Chrome OS\"}};");
4651   verifyFormat("struct {\n"
4652                "  unsigned bit;\n"
4653                "  const char *const name;\n"
4654                "} kBitsToOs[] = {\n"
4655                "    {kOsMac, \"Mac\"},\n"
4656                "    {kOsWin, \"Windows\"},\n"
4657                "    {kOsLinux, \"Linux\"},\n"
4658                "    {kOsCrOS, \"Chrome OS\"},\n"
4659                "};");
4660 }
4661 
4662 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
4663   verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
4664                "                      \\\n"
4665                "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
4666 }
4667 
4668 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
4669   verifyFormat("virtual void write(ELFWriter *writerrr,\n"
4670                "                   OwningPtr<FileOutputBuffer> &buffer) = 0;");
4671 
4672   // Do break defaulted and deleted functions.
4673   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
4674                "    default;",
4675                getLLVMStyleWithColumns(40));
4676   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
4677                "    delete;",
4678                getLLVMStyleWithColumns(40));
4679 }
4680 
4681 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) {
4682   verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3",
4683                getLLVMStyleWithColumns(40));
4684   verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
4685                getLLVMStyleWithColumns(40));
4686   EXPECT_EQ("#define Q                              \\\n"
4687             "  \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\"    \\\n"
4688             "  \"aaaaaaaa.cpp\"",
4689             format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
4690                    getLLVMStyleWithColumns(40)));
4691 }
4692 
4693 TEST_F(FormatTest, UnderstandsLinePPDirective) {
4694   EXPECT_EQ("# 123 \"A string literal\"",
4695             format("   #     123    \"A string literal\""));
4696 }
4697 
4698 TEST_F(FormatTest, LayoutUnknownPPDirective) {
4699   EXPECT_EQ("#;", format("#;"));
4700   verifyFormat("#\n;\n;\n;");
4701 }
4702 
4703 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
4704   EXPECT_EQ("#line 42 \"test\"\n",
4705             format("#  \\\n  line  \\\n  42  \\\n  \"test\"\n"));
4706   EXPECT_EQ("#define A B\n", format("#  \\\n define  \\\n    A  \\\n       B\n",
4707                                     getLLVMStyleWithColumns(12)));
4708 }
4709 
4710 TEST_F(FormatTest, EndOfFileEndsPPDirective) {
4711   EXPECT_EQ("#line 42 \"test\"",
4712             format("#  \\\n  line  \\\n  42  \\\n  \"test\""));
4713   EXPECT_EQ("#define A B", format("#  \\\n define  \\\n    A  \\\n       B"));
4714 }
4715 
4716 TEST_F(FormatTest, DoesntRemoveUnknownTokens) {
4717   verifyFormat("#define A \\x20");
4718   verifyFormat("#define A \\ x20");
4719   EXPECT_EQ("#define A \\ x20", format("#define A \\   x20"));
4720   verifyFormat("#define A ''");
4721   verifyFormat("#define A ''qqq");
4722   verifyFormat("#define A `qqq");
4723   verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");");
4724   EXPECT_EQ("const char *c = STRINGIFY(\n"
4725             "\\na : b);",
4726             format("const char * c = STRINGIFY(\n"
4727                    "\\na : b);"));
4728 
4729   verifyFormat("a\r\\");
4730   verifyFormat("a\v\\");
4731   verifyFormat("a\f\\");
4732 }
4733 
4734 TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) {
4735   FormatStyle style = getChromiumStyle(FormatStyle::LK_Cpp);
4736   style.IndentWidth = 4;
4737   style.PPIndentWidth = 1;
4738 
4739   style.IndentPPDirectives = FormatStyle::PPDIS_None;
4740   verifyFormat("#ifdef __linux__\n"
4741                "void foo() {\n"
4742                "    int x = 0;\n"
4743                "}\n"
4744                "#define FOO\n"
4745                "#endif\n"
4746                "void bar() {\n"
4747                "    int y = 0;\n"
4748                "}\n",
4749                style);
4750 
4751   style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
4752   verifyFormat("#ifdef __linux__\n"
4753                "void foo() {\n"
4754                "    int x = 0;\n"
4755                "}\n"
4756                "# define FOO foo\n"
4757                "#endif\n"
4758                "void bar() {\n"
4759                "    int y = 0;\n"
4760                "}\n",
4761                style);
4762 
4763   style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash;
4764   verifyFormat("#ifdef __linux__\n"
4765                "void foo() {\n"
4766                "    int x = 0;\n"
4767                "}\n"
4768                " #define FOO foo\n"
4769                "#endif\n"
4770                "void bar() {\n"
4771                "    int y = 0;\n"
4772                "}\n",
4773                style);
4774 }
4775 
4776 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
4777   verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
4778   verifyFormat("#define A( \\\n    BB)", getLLVMStyleWithColumns(12));
4779   verifyFormat("#define A( \\\n    A, B)", getLLVMStyleWithColumns(12));
4780   // FIXME: We never break before the macro name.
4781   verifyFormat("#define AA( \\\n    B)", getLLVMStyleWithColumns(12));
4782 
4783   verifyFormat("#define A A\n#define A A");
4784   verifyFormat("#define A(X) A\n#define A A");
4785 
4786   verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
4787   verifyFormat("#define Something    \\\n  Other", getLLVMStyleWithColumns(22));
4788 }
4789 
4790 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
4791   EXPECT_EQ("// somecomment\n"
4792             "#include \"a.h\"\n"
4793             "#define A(  \\\n"
4794             "    A, B)\n"
4795             "#include \"b.h\"\n"
4796             "// somecomment\n",
4797             format("  // somecomment\n"
4798                    "  #include \"a.h\"\n"
4799                    "#define A(A,\\\n"
4800                    "    B)\n"
4801                    "    #include \"b.h\"\n"
4802                    " // somecomment\n",
4803                    getLLVMStyleWithColumns(13)));
4804 }
4805 
4806 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); }
4807 
4808 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
4809   EXPECT_EQ("#define A    \\\n"
4810             "  c;         \\\n"
4811             "  e;\n"
4812             "f;",
4813             format("#define A c; e;\n"
4814                    "f;",
4815                    getLLVMStyleWithColumns(14)));
4816 }
4817 
4818 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
4819 
4820 TEST_F(FormatTest, MacroDefinitionInsideStatement) {
4821   EXPECT_EQ("int x,\n"
4822             "#define A\n"
4823             "    y;",
4824             format("int x,\n#define A\ny;"));
4825 }
4826 
4827 TEST_F(FormatTest, HashInMacroDefinition) {
4828   EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle()));
4829   EXPECT_EQ("#define A(c) u#c", format("#define A(c) u#c", getLLVMStyle()));
4830   EXPECT_EQ("#define A(c) U#c", format("#define A(c) U#c", getLLVMStyle()));
4831   EXPECT_EQ("#define A(c) u8#c", format("#define A(c) u8#c", getLLVMStyle()));
4832   EXPECT_EQ("#define A(c) LR#c", format("#define A(c) LR#c", getLLVMStyle()));
4833   EXPECT_EQ("#define A(c) uR#c", format("#define A(c) uR#c", getLLVMStyle()));
4834   EXPECT_EQ("#define A(c) UR#c", format("#define A(c) UR#c", getLLVMStyle()));
4835   EXPECT_EQ("#define A(c) u8R#c", format("#define A(c) u8R#c", getLLVMStyle()));
4836   verifyFormat("#define A \\\n  b #c;", getLLVMStyleWithColumns(11));
4837   verifyFormat("#define A  \\\n"
4838                "  {        \\\n"
4839                "    f(#c); \\\n"
4840                "  }",
4841                getLLVMStyleWithColumns(11));
4842 
4843   verifyFormat("#define A(X)         \\\n"
4844                "  void function##X()",
4845                getLLVMStyleWithColumns(22));
4846 
4847   verifyFormat("#define A(a, b, c)   \\\n"
4848                "  void a##b##c()",
4849                getLLVMStyleWithColumns(22));
4850 
4851   verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
4852 }
4853 
4854 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
4855   EXPECT_EQ("#define A (x)", format("#define A (x)"));
4856   EXPECT_EQ("#define A(x)", format("#define A(x)"));
4857 
4858   FormatStyle Style = getLLVMStyle();
4859   Style.SpaceBeforeParens = FormatStyle::SBPO_Never;
4860   verifyFormat("#define true ((foo)1)", Style);
4861   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
4862   verifyFormat("#define false((foo)0)", Style);
4863 }
4864 
4865 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
4866   EXPECT_EQ("#define A b;", format("#define A \\\n"
4867                                    "          \\\n"
4868                                    "  b;",
4869                                    getLLVMStyleWithColumns(25)));
4870   EXPECT_EQ("#define A \\\n"
4871             "          \\\n"
4872             "  a;      \\\n"
4873             "  b;",
4874             format("#define A \\\n"
4875                    "          \\\n"
4876                    "  a;      \\\n"
4877                    "  b;",
4878                    getLLVMStyleWithColumns(11)));
4879   EXPECT_EQ("#define A \\\n"
4880             "  a;      \\\n"
4881             "          \\\n"
4882             "  b;",
4883             format("#define A \\\n"
4884                    "  a;      \\\n"
4885                    "          \\\n"
4886                    "  b;",
4887                    getLLVMStyleWithColumns(11)));
4888 }
4889 
4890 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
4891   verifyIncompleteFormat("#define A :");
4892   verifyFormat("#define SOMECASES  \\\n"
4893                "  case 1:          \\\n"
4894                "  case 2\n",
4895                getLLVMStyleWithColumns(20));
4896   verifyFormat("#define MACRO(a) \\\n"
4897                "  if (a)         \\\n"
4898                "    f();         \\\n"
4899                "  else           \\\n"
4900                "    g()",
4901                getLLVMStyleWithColumns(18));
4902   verifyFormat("#define A template <typename T>");
4903   verifyIncompleteFormat("#define STR(x) #x\n"
4904                          "f(STR(this_is_a_string_literal{));");
4905   verifyFormat("#pragma omp threadprivate( \\\n"
4906                "    y)), // expected-warning",
4907                getLLVMStyleWithColumns(28));
4908   verifyFormat("#d, = };");
4909   verifyFormat("#if \"a");
4910   verifyIncompleteFormat("({\n"
4911                          "#define b     \\\n"
4912                          "  }           \\\n"
4913                          "  a\n"
4914                          "a",
4915                          getLLVMStyleWithColumns(15));
4916   verifyFormat("#define A     \\\n"
4917                "  {           \\\n"
4918                "    {\n"
4919                "#define B     \\\n"
4920                "  }           \\\n"
4921                "  }",
4922                getLLVMStyleWithColumns(15));
4923   verifyNoCrash("#if a\na(\n#else\n#endif\n{a");
4924   verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}");
4925   verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};");
4926   verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() {      \n)}");
4927 }
4928 
4929 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
4930   verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
4931   EXPECT_EQ("class A : public QObject {\n"
4932             "  Q_OBJECT\n"
4933             "\n"
4934             "  A() {}\n"
4935             "};",
4936             format("class A  :  public QObject {\n"
4937                    "     Q_OBJECT\n"
4938                    "\n"
4939                    "  A() {\n}\n"
4940                    "}  ;"));
4941   EXPECT_EQ("MACRO\n"
4942             "/*static*/ int i;",
4943             format("MACRO\n"
4944                    " /*static*/ int   i;"));
4945   EXPECT_EQ("SOME_MACRO\n"
4946             "namespace {\n"
4947             "void f();\n"
4948             "} // namespace",
4949             format("SOME_MACRO\n"
4950                    "  namespace    {\n"
4951                    "void   f(  );\n"
4952                    "} // namespace"));
4953   // Only if the identifier contains at least 5 characters.
4954   EXPECT_EQ("HTTP f();", format("HTTP\nf();"));
4955   EXPECT_EQ("MACRO\nf();", format("MACRO\nf();"));
4956   // Only if everything is upper case.
4957   EXPECT_EQ("class A : public QObject {\n"
4958             "  Q_Object A() {}\n"
4959             "};",
4960             format("class A  :  public QObject {\n"
4961                    "     Q_Object\n"
4962                    "  A() {\n}\n"
4963                    "}  ;"));
4964 
4965   // Only if the next line can actually start an unwrapped line.
4966   EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;",
4967             format("SOME_WEIRD_LOG_MACRO\n"
4968                    "<< SomeThing;"));
4969 
4970   verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
4971                "(n, buffers))\n",
4972                getChromiumStyle(FormatStyle::LK_Cpp));
4973 
4974   // See PR41483
4975   EXPECT_EQ("/**/ FOO(a)\n"
4976             "FOO(b)",
4977             format("/**/ FOO(a)\n"
4978                    "FOO(b)"));
4979 }
4980 
4981 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
4982   EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
4983             "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
4984             "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
4985             "class X {};\n"
4986             "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
4987             "int *createScopDetectionPass() { return 0; }",
4988             format("  INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
4989                    "  INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
4990                    "  INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
4991                    "  class X {};\n"
4992                    "  INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
4993                    "  int *createScopDetectionPass() { return 0; }"));
4994   // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
4995   // braces, so that inner block is indented one level more.
4996   EXPECT_EQ("int q() {\n"
4997             "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
4998             "  IPC_MESSAGE_HANDLER(xxx, qqq)\n"
4999             "  IPC_END_MESSAGE_MAP()\n"
5000             "}",
5001             format("int q() {\n"
5002                    "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
5003                    "    IPC_MESSAGE_HANDLER(xxx, qqq)\n"
5004                    "  IPC_END_MESSAGE_MAP()\n"
5005                    "}"));
5006 
5007   // Same inside macros.
5008   EXPECT_EQ("#define LIST(L) \\\n"
5009             "  L(A)          \\\n"
5010             "  L(B)          \\\n"
5011             "  L(C)",
5012             format("#define LIST(L) \\\n"
5013                    "  L(A) \\\n"
5014                    "  L(B) \\\n"
5015                    "  L(C)",
5016                    getGoogleStyle()));
5017 
5018   // These must not be recognized as macros.
5019   EXPECT_EQ("int q() {\n"
5020             "  f(x);\n"
5021             "  f(x) {}\n"
5022             "  f(x)->g();\n"
5023             "  f(x)->*g();\n"
5024             "  f(x).g();\n"
5025             "  f(x) = x;\n"
5026             "  f(x) += x;\n"
5027             "  f(x) -= x;\n"
5028             "  f(x) *= x;\n"
5029             "  f(x) /= x;\n"
5030             "  f(x) %= x;\n"
5031             "  f(x) &= x;\n"
5032             "  f(x) |= x;\n"
5033             "  f(x) ^= x;\n"
5034             "  f(x) >>= x;\n"
5035             "  f(x) <<= x;\n"
5036             "  f(x)[y].z();\n"
5037             "  LOG(INFO) << x;\n"
5038             "  ifstream(x) >> x;\n"
5039             "}\n",
5040             format("int q() {\n"
5041                    "  f(x)\n;\n"
5042                    "  f(x)\n {}\n"
5043                    "  f(x)\n->g();\n"
5044                    "  f(x)\n->*g();\n"
5045                    "  f(x)\n.g();\n"
5046                    "  f(x)\n = x;\n"
5047                    "  f(x)\n += x;\n"
5048                    "  f(x)\n -= x;\n"
5049                    "  f(x)\n *= x;\n"
5050                    "  f(x)\n /= x;\n"
5051                    "  f(x)\n %= x;\n"
5052                    "  f(x)\n &= x;\n"
5053                    "  f(x)\n |= x;\n"
5054                    "  f(x)\n ^= x;\n"
5055                    "  f(x)\n >>= x;\n"
5056                    "  f(x)\n <<= x;\n"
5057                    "  f(x)\n[y].z();\n"
5058                    "  LOG(INFO)\n << x;\n"
5059                    "  ifstream(x)\n >> x;\n"
5060                    "}\n"));
5061   EXPECT_EQ("int q() {\n"
5062             "  F(x)\n"
5063             "  if (1) {\n"
5064             "  }\n"
5065             "  F(x)\n"
5066             "  while (1) {\n"
5067             "  }\n"
5068             "  F(x)\n"
5069             "  G(x);\n"
5070             "  F(x)\n"
5071             "  try {\n"
5072             "    Q();\n"
5073             "  } catch (...) {\n"
5074             "  }\n"
5075             "}\n",
5076             format("int q() {\n"
5077                    "F(x)\n"
5078                    "if (1) {}\n"
5079                    "F(x)\n"
5080                    "while (1) {}\n"
5081                    "F(x)\n"
5082                    "G(x);\n"
5083                    "F(x)\n"
5084                    "try { Q(); } catch (...) {}\n"
5085                    "}\n"));
5086   EXPECT_EQ("class A {\n"
5087             "  A() : t(0) {}\n"
5088             "  A(int i) noexcept() : {}\n"
5089             "  A(X x)\n" // FIXME: function-level try blocks are broken.
5090             "  try : t(0) {\n"
5091             "  } catch (...) {\n"
5092             "  }\n"
5093             "};",
5094             format("class A {\n"
5095                    "  A()\n : t(0) {}\n"
5096                    "  A(int i)\n noexcept() : {}\n"
5097                    "  A(X x)\n"
5098                    "  try : t(0) {} catch (...) {}\n"
5099                    "};"));
5100   FormatStyle Style = getLLVMStyle();
5101   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
5102   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
5103   Style.BraceWrapping.AfterFunction = true;
5104   EXPECT_EQ("void f()\n"
5105             "try\n"
5106             "{\n"
5107             "}",
5108             format("void f() try {\n"
5109                    "}",
5110                    Style));
5111   EXPECT_EQ("class SomeClass {\n"
5112             "public:\n"
5113             "  SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
5114             "};",
5115             format("class SomeClass {\n"
5116                    "public:\n"
5117                    "  SomeClass()\n"
5118                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
5119                    "};"));
5120   EXPECT_EQ("class SomeClass {\n"
5121             "public:\n"
5122             "  SomeClass()\n"
5123             "      EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
5124             "};",
5125             format("class SomeClass {\n"
5126                    "public:\n"
5127                    "  SomeClass()\n"
5128                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
5129                    "};",
5130                    getLLVMStyleWithColumns(40)));
5131 
5132   verifyFormat("MACRO(>)");
5133 
5134   // Some macros contain an implicit semicolon.
5135   Style = getLLVMStyle();
5136   Style.StatementMacros.push_back("FOO");
5137   verifyFormat("FOO(a) int b = 0;");
5138   verifyFormat("FOO(a)\n"
5139                "int b = 0;",
5140                Style);
5141   verifyFormat("FOO(a);\n"
5142                "int b = 0;",
5143                Style);
5144   verifyFormat("FOO(argc, argv, \"4.0.2\")\n"
5145                "int b = 0;",
5146                Style);
5147   verifyFormat("FOO()\n"
5148                "int b = 0;",
5149                Style);
5150   verifyFormat("FOO\n"
5151                "int b = 0;",
5152                Style);
5153   verifyFormat("void f() {\n"
5154                "  FOO(a)\n"
5155                "  return a;\n"
5156                "}",
5157                Style);
5158   verifyFormat("FOO(a)\n"
5159                "FOO(b)",
5160                Style);
5161   verifyFormat("int a = 0;\n"
5162                "FOO(b)\n"
5163                "int c = 0;",
5164                Style);
5165   verifyFormat("int a = 0;\n"
5166                "int x = FOO(a)\n"
5167                "int b = 0;",
5168                Style);
5169   verifyFormat("void foo(int a) { FOO(a) }\n"
5170                "uint32_t bar() {}",
5171                Style);
5172 }
5173 
5174 TEST_F(FormatTest, FormatsMacrosWithZeroColumnWidth) {
5175   FormatStyle ZeroColumn = getLLVMStyleWithColumns(0);
5176 
5177   verifyFormat("#define A LOOOOOOOOOOOOOOOOOOONG() LOOOOOOOOOOOOOOOOOOONG()",
5178                ZeroColumn);
5179 }
5180 
5181 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
5182   verifyFormat("#define A \\\n"
5183                "  f({     \\\n"
5184                "    g();  \\\n"
5185                "  });",
5186                getLLVMStyleWithColumns(11));
5187 }
5188 
5189 TEST_F(FormatTest, IndentPreprocessorDirectives) {
5190   FormatStyle Style = getLLVMStyleWithColumns(40);
5191   Style.IndentPPDirectives = FormatStyle::PPDIS_None;
5192   verifyFormat("#ifdef _WIN32\n"
5193                "#define A 0\n"
5194                "#ifdef VAR2\n"
5195                "#define B 1\n"
5196                "#include <someheader.h>\n"
5197                "#define MACRO                          \\\n"
5198                "  some_very_long_func_aaaaaaaaaa();\n"
5199                "#endif\n"
5200                "#else\n"
5201                "#define A 1\n"
5202                "#endif",
5203                Style);
5204   Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
5205   verifyFormat("#ifdef _WIN32\n"
5206                "#  define A 0\n"
5207                "#  ifdef VAR2\n"
5208                "#    define B 1\n"
5209                "#    include <someheader.h>\n"
5210                "#    define MACRO                      \\\n"
5211                "      some_very_long_func_aaaaaaaaaa();\n"
5212                "#  endif\n"
5213                "#else\n"
5214                "#  define A 1\n"
5215                "#endif",
5216                Style);
5217   verifyFormat("#if A\n"
5218                "#  define MACRO                        \\\n"
5219                "    void a(int x) {                    \\\n"
5220                "      b();                             \\\n"
5221                "      c();                             \\\n"
5222                "      d();                             \\\n"
5223                "      e();                             \\\n"
5224                "      f();                             \\\n"
5225                "    }\n"
5226                "#endif",
5227                Style);
5228   // Comments before include guard.
5229   verifyFormat("// file comment\n"
5230                "// file comment\n"
5231                "#ifndef HEADER_H\n"
5232                "#define HEADER_H\n"
5233                "code();\n"
5234                "#endif",
5235                Style);
5236   // Test with include guards.
5237   verifyFormat("#ifndef HEADER_H\n"
5238                "#define HEADER_H\n"
5239                "code();\n"
5240                "#endif",
5241                Style);
5242   // Include guards must have a #define with the same variable immediately
5243   // after #ifndef.
5244   verifyFormat("#ifndef NOT_GUARD\n"
5245                "#  define FOO\n"
5246                "code();\n"
5247                "#endif",
5248                Style);
5249 
5250   // Include guards must cover the entire file.
5251   verifyFormat("code();\n"
5252                "code();\n"
5253                "#ifndef NOT_GUARD\n"
5254                "#  define NOT_GUARD\n"
5255                "code();\n"
5256                "#endif",
5257                Style);
5258   verifyFormat("#ifndef NOT_GUARD\n"
5259                "#  define NOT_GUARD\n"
5260                "code();\n"
5261                "#endif\n"
5262                "code();",
5263                Style);
5264   // Test with trailing blank lines.
5265   verifyFormat("#ifndef HEADER_H\n"
5266                "#define HEADER_H\n"
5267                "code();\n"
5268                "#endif\n",
5269                Style);
5270   // Include guards don't have #else.
5271   verifyFormat("#ifndef NOT_GUARD\n"
5272                "#  define NOT_GUARD\n"
5273                "code();\n"
5274                "#else\n"
5275                "#endif",
5276                Style);
5277   verifyFormat("#ifndef NOT_GUARD\n"
5278                "#  define NOT_GUARD\n"
5279                "code();\n"
5280                "#elif FOO\n"
5281                "#endif",
5282                Style);
5283   // Non-identifier #define after potential include guard.
5284   verifyFormat("#ifndef FOO\n"
5285                "#  define 1\n"
5286                "#endif\n",
5287                Style);
5288   // #if closes past last non-preprocessor line.
5289   verifyFormat("#ifndef FOO\n"
5290                "#define FOO\n"
5291                "#if 1\n"
5292                "int i;\n"
5293                "#  define A 0\n"
5294                "#endif\n"
5295                "#endif\n",
5296                Style);
5297   // Don't crash if there is an #elif directive without a condition.
5298   verifyFormat("#if 1\n"
5299                "int x;\n"
5300                "#elif\n"
5301                "int y;\n"
5302                "#else\n"
5303                "int z;\n"
5304                "#endif",
5305                Style);
5306   // FIXME: This doesn't handle the case where there's code between the
5307   // #ifndef and #define but all other conditions hold. This is because when
5308   // the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the
5309   // previous code line yet, so we can't detect it.
5310   EXPECT_EQ("#ifndef NOT_GUARD\n"
5311             "code();\n"
5312             "#define NOT_GUARD\n"
5313             "code();\n"
5314             "#endif",
5315             format("#ifndef NOT_GUARD\n"
5316                    "code();\n"
5317                    "#  define NOT_GUARD\n"
5318                    "code();\n"
5319                    "#endif",
5320                    Style));
5321   // FIXME: This doesn't handle cases where legitimate preprocessor lines may
5322   // be outside an include guard. Examples are #pragma once and
5323   // #pragma GCC diagnostic, or anything else that does not change the meaning
5324   // of the file if it's included multiple times.
5325   EXPECT_EQ("#ifdef WIN32\n"
5326             "#  pragma once\n"
5327             "#endif\n"
5328             "#ifndef HEADER_H\n"
5329             "#  define HEADER_H\n"
5330             "code();\n"
5331             "#endif",
5332             format("#ifdef WIN32\n"
5333                    "#  pragma once\n"
5334                    "#endif\n"
5335                    "#ifndef HEADER_H\n"
5336                    "#define HEADER_H\n"
5337                    "code();\n"
5338                    "#endif",
5339                    Style));
5340   // FIXME: This does not detect when there is a single non-preprocessor line
5341   // in front of an include-guard-like structure where other conditions hold
5342   // because ScopedLineState hides the line.
5343   EXPECT_EQ("code();\n"
5344             "#ifndef HEADER_H\n"
5345             "#define HEADER_H\n"
5346             "code();\n"
5347             "#endif",
5348             format("code();\n"
5349                    "#ifndef HEADER_H\n"
5350                    "#  define HEADER_H\n"
5351                    "code();\n"
5352                    "#endif",
5353                    Style));
5354   // Keep comments aligned with #, otherwise indent comments normally. These
5355   // tests cannot use verifyFormat because messUp manipulates leading
5356   // whitespace.
5357   {
5358     const char *Expected = ""
5359                            "void f() {\n"
5360                            "#if 1\n"
5361                            "// Preprocessor aligned.\n"
5362                            "#  define A 0\n"
5363                            "  // Code. Separated by blank line.\n"
5364                            "\n"
5365                            "#  define B 0\n"
5366                            "  // Code. Not aligned with #\n"
5367                            "#  define C 0\n"
5368                            "#endif";
5369     const char *ToFormat = ""
5370                            "void f() {\n"
5371                            "#if 1\n"
5372                            "// Preprocessor aligned.\n"
5373                            "#  define A 0\n"
5374                            "// Code. Separated by blank line.\n"
5375                            "\n"
5376                            "#  define B 0\n"
5377                            "   // Code. Not aligned with #\n"
5378                            "#  define C 0\n"
5379                            "#endif";
5380     EXPECT_EQ(Expected, format(ToFormat, Style));
5381     EXPECT_EQ(Expected, format(Expected, Style));
5382   }
5383   // Keep block quotes aligned.
5384   {
5385     const char *Expected = ""
5386                            "void f() {\n"
5387                            "#if 1\n"
5388                            "/* Preprocessor aligned. */\n"
5389                            "#  define A 0\n"
5390                            "  /* Code. Separated by blank line. */\n"
5391                            "\n"
5392                            "#  define B 0\n"
5393                            "  /* Code. Not aligned with # */\n"
5394                            "#  define C 0\n"
5395                            "#endif";
5396     const char *ToFormat = ""
5397                            "void f() {\n"
5398                            "#if 1\n"
5399                            "/* Preprocessor aligned. */\n"
5400                            "#  define A 0\n"
5401                            "/* Code. Separated by blank line. */\n"
5402                            "\n"
5403                            "#  define B 0\n"
5404                            "   /* Code. Not aligned with # */\n"
5405                            "#  define C 0\n"
5406                            "#endif";
5407     EXPECT_EQ(Expected, format(ToFormat, Style));
5408     EXPECT_EQ(Expected, format(Expected, Style));
5409   }
5410   // Keep comments aligned with un-indented directives.
5411   {
5412     const char *Expected = ""
5413                            "void f() {\n"
5414                            "// Preprocessor aligned.\n"
5415                            "#define A 0\n"
5416                            "  // Code. Separated by blank line.\n"
5417                            "\n"
5418                            "#define B 0\n"
5419                            "  // Code. Not aligned with #\n"
5420                            "#define C 0\n";
5421     const char *ToFormat = ""
5422                            "void f() {\n"
5423                            "// Preprocessor aligned.\n"
5424                            "#define A 0\n"
5425                            "// Code. Separated by blank line.\n"
5426                            "\n"
5427                            "#define B 0\n"
5428                            "   // Code. Not aligned with #\n"
5429                            "#define C 0\n";
5430     EXPECT_EQ(Expected, format(ToFormat, Style));
5431     EXPECT_EQ(Expected, format(Expected, Style));
5432   }
5433   // Test AfterHash with tabs.
5434   {
5435     FormatStyle Tabbed = Style;
5436     Tabbed.UseTab = FormatStyle::UT_Always;
5437     Tabbed.IndentWidth = 8;
5438     Tabbed.TabWidth = 8;
5439     verifyFormat("#ifdef _WIN32\n"
5440                  "#\tdefine A 0\n"
5441                  "#\tifdef VAR2\n"
5442                  "#\t\tdefine B 1\n"
5443                  "#\t\tinclude <someheader.h>\n"
5444                  "#\t\tdefine MACRO          \\\n"
5445                  "\t\t\tsome_very_long_func_aaaaaaaaaa();\n"
5446                  "#\tendif\n"
5447                  "#else\n"
5448                  "#\tdefine A 1\n"
5449                  "#endif",
5450                  Tabbed);
5451   }
5452 
5453   // Regression test: Multiline-macro inside include guards.
5454   verifyFormat("#ifndef HEADER_H\n"
5455                "#define HEADER_H\n"
5456                "#define A()        \\\n"
5457                "  int i;           \\\n"
5458                "  int j;\n"
5459                "#endif // HEADER_H",
5460                getLLVMStyleWithColumns(20));
5461 
5462   Style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash;
5463   // Basic before hash indent tests
5464   verifyFormat("#ifdef _WIN32\n"
5465                "  #define A 0\n"
5466                "  #ifdef VAR2\n"
5467                "    #define B 1\n"
5468                "    #include <someheader.h>\n"
5469                "    #define MACRO                      \\\n"
5470                "      some_very_long_func_aaaaaaaaaa();\n"
5471                "  #endif\n"
5472                "#else\n"
5473                "  #define A 1\n"
5474                "#endif",
5475                Style);
5476   verifyFormat("#if A\n"
5477                "  #define MACRO                        \\\n"
5478                "    void a(int x) {                    \\\n"
5479                "      b();                             \\\n"
5480                "      c();                             \\\n"
5481                "      d();                             \\\n"
5482                "      e();                             \\\n"
5483                "      f();                             \\\n"
5484                "    }\n"
5485                "#endif",
5486                Style);
5487   // Keep comments aligned with indented directives. These
5488   // tests cannot use verifyFormat because messUp manipulates leading
5489   // whitespace.
5490   {
5491     const char *Expected = "void f() {\n"
5492                            "// Aligned to preprocessor.\n"
5493                            "#if 1\n"
5494                            "  // Aligned to code.\n"
5495                            "  int a;\n"
5496                            "  #if 1\n"
5497                            "    // Aligned to preprocessor.\n"
5498                            "    #define A 0\n"
5499                            "  // Aligned to code.\n"
5500                            "  int b;\n"
5501                            "  #endif\n"
5502                            "#endif\n"
5503                            "}";
5504     const char *ToFormat = "void f() {\n"
5505                            "// Aligned to preprocessor.\n"
5506                            "#if 1\n"
5507                            "// Aligned to code.\n"
5508                            "int a;\n"
5509                            "#if 1\n"
5510                            "// Aligned to preprocessor.\n"
5511                            "#define A 0\n"
5512                            "// Aligned to code.\n"
5513                            "int b;\n"
5514                            "#endif\n"
5515                            "#endif\n"
5516                            "}";
5517     EXPECT_EQ(Expected, format(ToFormat, Style));
5518     EXPECT_EQ(Expected, format(Expected, Style));
5519   }
5520   {
5521     const char *Expected = "void f() {\n"
5522                            "/* Aligned to preprocessor. */\n"
5523                            "#if 1\n"
5524                            "  /* Aligned to code. */\n"
5525                            "  int a;\n"
5526                            "  #if 1\n"
5527                            "    /* Aligned to preprocessor. */\n"
5528                            "    #define A 0\n"
5529                            "  /* Aligned to code. */\n"
5530                            "  int b;\n"
5531                            "  #endif\n"
5532                            "#endif\n"
5533                            "}";
5534     const char *ToFormat = "void f() {\n"
5535                            "/* Aligned to preprocessor. */\n"
5536                            "#if 1\n"
5537                            "/* Aligned to code. */\n"
5538                            "int a;\n"
5539                            "#if 1\n"
5540                            "/* Aligned to preprocessor. */\n"
5541                            "#define A 0\n"
5542                            "/* Aligned to code. */\n"
5543                            "int b;\n"
5544                            "#endif\n"
5545                            "#endif\n"
5546                            "}";
5547     EXPECT_EQ(Expected, format(ToFormat, Style));
5548     EXPECT_EQ(Expected, format(Expected, Style));
5549   }
5550 
5551   // Test single comment before preprocessor
5552   verifyFormat("// Comment\n"
5553                "\n"
5554                "#if 1\n"
5555                "#endif",
5556                Style);
5557 }
5558 
5559 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
5560   verifyFormat("{\n  { a #c; }\n}");
5561 }
5562 
5563 TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
5564   EXPECT_EQ("#define A \\\n  {       \\\n    {\nint i;",
5565             format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
5566   EXPECT_EQ("#define A \\\n  }       \\\n  }\nint i;",
5567             format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
5568 }
5569 
5570 TEST_F(FormatTest, EscapedNewlines) {
5571   FormatStyle Narrow = getLLVMStyleWithColumns(11);
5572   EXPECT_EQ("#define A \\\n  int i;  \\\n  int j;",
5573             format("#define A \\\nint i;\\\n  int j;", Narrow));
5574   EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;"));
5575   EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
5576   EXPECT_EQ("/* \\  \\  \\\n */", format("\\\n/* \\  \\  \\\n */"));
5577   EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>"));
5578 
5579   FormatStyle AlignLeft = getLLVMStyle();
5580   AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
5581   EXPECT_EQ("#define MACRO(x) \\\n"
5582             "private:         \\\n"
5583             "  int x(int a);\n",
5584             format("#define MACRO(x) \\\n"
5585                    "private:         \\\n"
5586                    "  int x(int a);\n",
5587                    AlignLeft));
5588 
5589   // CRLF line endings
5590   EXPECT_EQ("#define A \\\r\n  int i;  \\\r\n  int j;",
5591             format("#define A \\\r\nint i;\\\r\n  int j;", Narrow));
5592   EXPECT_EQ("#define A\r\n\r\nint i;", format("#define A \\\r\n\r\n int i;"));
5593   EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
5594   EXPECT_EQ("/* \\  \\  \\\r\n */", format("\\\r\n/* \\  \\  \\\r\n */"));
5595   EXPECT_EQ("<a\r\n\\\\\r\n>", format("<a\r\n\\\\\r\n>"));
5596   EXPECT_EQ("#define MACRO(x) \\\r\n"
5597             "private:         \\\r\n"
5598             "  int x(int a);\r\n",
5599             format("#define MACRO(x) \\\r\n"
5600                    "private:         \\\r\n"
5601                    "  int x(int a);\r\n",
5602                    AlignLeft));
5603 
5604   FormatStyle DontAlign = getLLVMStyle();
5605   DontAlign.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
5606   DontAlign.MaxEmptyLinesToKeep = 3;
5607   // FIXME: can't use verifyFormat here because the newline before
5608   // "public:" is not inserted the first time it's reformatted
5609   EXPECT_EQ("#define A \\\n"
5610             "  class Foo { \\\n"
5611             "    void bar(); \\\n"
5612             "\\\n"
5613             "\\\n"
5614             "\\\n"
5615             "  public: \\\n"
5616             "    void baz(); \\\n"
5617             "  };",
5618             format("#define A \\\n"
5619                    "  class Foo { \\\n"
5620                    "    void bar(); \\\n"
5621                    "\\\n"
5622                    "\\\n"
5623                    "\\\n"
5624                    "  public: \\\n"
5625                    "    void baz(); \\\n"
5626                    "  };",
5627                    DontAlign));
5628 }
5629 
5630 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
5631   verifyFormat("#define A \\\n"
5632                "  int v(  \\\n"
5633                "      a); \\\n"
5634                "  int i;",
5635                getLLVMStyleWithColumns(11));
5636 }
5637 
5638 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
5639   EXPECT_EQ(
5640       "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
5641       "                      \\\n"
5642       "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
5643       "\n"
5644       "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
5645       "    aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
5646       format("  #define   ALooooooooooooooooooooooooooooooooooooooongMacro("
5647              "\\\n"
5648              "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
5649              "  \n"
5650              "   AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
5651              "  aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
5652 }
5653 
5654 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
5655   EXPECT_EQ("int\n"
5656             "#define A\n"
5657             "    a;",
5658             format("int\n#define A\na;"));
5659   verifyFormat("functionCallTo(\n"
5660                "    someOtherFunction(\n"
5661                "        withSomeParameters, whichInSequence,\n"
5662                "        areLongerThanALine(andAnotherCall,\n"
5663                "#define A B\n"
5664                "                           withMoreParamters,\n"
5665                "                           whichStronglyInfluenceTheLayout),\n"
5666                "        andMoreParameters),\n"
5667                "    trailing);",
5668                getLLVMStyleWithColumns(69));
5669   verifyFormat("Foo::Foo()\n"
5670                "#ifdef BAR\n"
5671                "    : baz(0)\n"
5672                "#endif\n"
5673                "{\n"
5674                "}");
5675   verifyFormat("void f() {\n"
5676                "  if (true)\n"
5677                "#ifdef A\n"
5678                "    f(42);\n"
5679                "  x();\n"
5680                "#else\n"
5681                "    g();\n"
5682                "  x();\n"
5683                "#endif\n"
5684                "}");
5685   verifyFormat("void f(param1, param2,\n"
5686                "       param3,\n"
5687                "#ifdef A\n"
5688                "       param4(param5,\n"
5689                "#ifdef A1\n"
5690                "              param6,\n"
5691                "#ifdef A2\n"
5692                "              param7),\n"
5693                "#else\n"
5694                "              param8),\n"
5695                "       param9,\n"
5696                "#endif\n"
5697                "       param10,\n"
5698                "#endif\n"
5699                "       param11)\n"
5700                "#else\n"
5701                "       param12)\n"
5702                "#endif\n"
5703                "{\n"
5704                "  x();\n"
5705                "}",
5706                getLLVMStyleWithColumns(28));
5707   verifyFormat("#if 1\n"
5708                "int i;");
5709   verifyFormat("#if 1\n"
5710                "#endif\n"
5711                "#if 1\n"
5712                "#else\n"
5713                "#endif\n");
5714   verifyFormat("DEBUG({\n"
5715                "  return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5716                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
5717                "});\n"
5718                "#if a\n"
5719                "#else\n"
5720                "#endif");
5721 
5722   verifyIncompleteFormat("void f(\n"
5723                          "#if A\n"
5724                          ");\n"
5725                          "#else\n"
5726                          "#endif");
5727 }
5728 
5729 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
5730   verifyFormat("#endif\n"
5731                "#if B");
5732 }
5733 
5734 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
5735   FormatStyle SingleLine = getLLVMStyle();
5736   SingleLine.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse;
5737   verifyFormat("#if 0\n"
5738                "#elif 1\n"
5739                "#endif\n"
5740                "void foo() {\n"
5741                "  if (test) foo2();\n"
5742                "}",
5743                SingleLine);
5744 }
5745 
5746 TEST_F(FormatTest, LayoutBlockInsideParens) {
5747   verifyFormat("functionCall({ int i; });");
5748   verifyFormat("functionCall({\n"
5749                "  int i;\n"
5750                "  int j;\n"
5751                "});");
5752   verifyFormat("functionCall(\n"
5753                "    {\n"
5754                "      int i;\n"
5755                "      int j;\n"
5756                "    },\n"
5757                "    aaaa, bbbb, cccc);");
5758   verifyFormat("functionA(functionB({\n"
5759                "            int i;\n"
5760                "            int j;\n"
5761                "          }),\n"
5762                "          aaaa, bbbb, cccc);");
5763   verifyFormat("functionCall(\n"
5764                "    {\n"
5765                "      int i;\n"
5766                "      int j;\n"
5767                "    },\n"
5768                "    aaaa, bbbb, // comment\n"
5769                "    cccc);");
5770   verifyFormat("functionA(functionB({\n"
5771                "            int i;\n"
5772                "            int j;\n"
5773                "          }),\n"
5774                "          aaaa, bbbb, // comment\n"
5775                "          cccc);");
5776   verifyFormat("functionCall(aaaa, bbbb, { int i; });");
5777   verifyFormat("functionCall(aaaa, bbbb, {\n"
5778                "  int i;\n"
5779                "  int j;\n"
5780                "});");
5781   verifyFormat(
5782       "Aaa(\n" // FIXME: There shouldn't be a linebreak here.
5783       "    {\n"
5784       "      int i; // break\n"
5785       "    },\n"
5786       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
5787       "                                     ccccccccccccccccc));");
5788   verifyFormat("DEBUG({\n"
5789                "  if (a)\n"
5790                "    f();\n"
5791                "});");
5792 }
5793 
5794 TEST_F(FormatTest, LayoutBlockInsideStatement) {
5795   EXPECT_EQ("SOME_MACRO { int i; }\n"
5796             "int i;",
5797             format("  SOME_MACRO  {int i;}  int i;"));
5798 }
5799 
5800 TEST_F(FormatTest, LayoutNestedBlocks) {
5801   verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
5802                "  struct s {\n"
5803                "    int i;\n"
5804                "  };\n"
5805                "  s kBitsToOs[] = {{10}};\n"
5806                "  for (int i = 0; i < 10; ++i)\n"
5807                "    return;\n"
5808                "}");
5809   verifyFormat("call(parameter, {\n"
5810                "  something();\n"
5811                "  // Comment using all columns.\n"
5812                "  somethingelse();\n"
5813                "});",
5814                getLLVMStyleWithColumns(40));
5815   verifyFormat("DEBUG( //\n"
5816                "    { f(); }, a);");
5817   verifyFormat("DEBUG( //\n"
5818                "    {\n"
5819                "      f(); //\n"
5820                "    },\n"
5821                "    a);");
5822 
5823   EXPECT_EQ("call(parameter, {\n"
5824             "  something();\n"
5825             "  // Comment too\n"
5826             "  // looooooooooong.\n"
5827             "  somethingElse();\n"
5828             "});",
5829             format("call(parameter, {\n"
5830                    "  something();\n"
5831                    "  // Comment too looooooooooong.\n"
5832                    "  somethingElse();\n"
5833                    "});",
5834                    getLLVMStyleWithColumns(29)));
5835   EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int   i; });"));
5836   EXPECT_EQ("DEBUG({ // comment\n"
5837             "  int i;\n"
5838             "});",
5839             format("DEBUG({ // comment\n"
5840                    "int  i;\n"
5841                    "});"));
5842   EXPECT_EQ("DEBUG({\n"
5843             "  int i;\n"
5844             "\n"
5845             "  // comment\n"
5846             "  int j;\n"
5847             "});",
5848             format("DEBUG({\n"
5849                    "  int  i;\n"
5850                    "\n"
5851                    "  // comment\n"
5852                    "  int  j;\n"
5853                    "});"));
5854 
5855   verifyFormat("DEBUG({\n"
5856                "  if (a)\n"
5857                "    return;\n"
5858                "});");
5859   verifyGoogleFormat("DEBUG({\n"
5860                      "  if (a) return;\n"
5861                      "});");
5862   FormatStyle Style = getGoogleStyle();
5863   Style.ColumnLimit = 45;
5864   verifyFormat("Debug(\n"
5865                "    aaaaa,\n"
5866                "    {\n"
5867                "      if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
5868                "    },\n"
5869                "    a);",
5870                Style);
5871 
5872   verifyFormat("SomeFunction({MACRO({ return output; }), b});");
5873 
5874   verifyNoCrash("^{v^{a}}");
5875 }
5876 
5877 TEST_F(FormatTest, FormatNestedBlocksInMacros) {
5878   EXPECT_EQ("#define MACRO()                     \\\n"
5879             "  Debug(aaa, /* force line break */ \\\n"
5880             "        {                           \\\n"
5881             "          int i;                    \\\n"
5882             "          int j;                    \\\n"
5883             "        })",
5884             format("#define   MACRO()   Debug(aaa,  /* force line break */ \\\n"
5885                    "          {  int   i;  int  j;   })",
5886                    getGoogleStyle()));
5887 
5888   EXPECT_EQ("#define A                                       \\\n"
5889             "  [] {                                          \\\n"
5890             "    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(        \\\n"
5891             "        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
5892             "  }",
5893             format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
5894                    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
5895                    getGoogleStyle()));
5896 }
5897 
5898 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
5899   EXPECT_EQ("{}", format("{}"));
5900   verifyFormat("enum E {};");
5901   verifyFormat("enum E {}");
5902   FormatStyle Style = getLLVMStyle();
5903   Style.SpaceInEmptyBlock = true;
5904   EXPECT_EQ("void f() { }", format("void f() {}", Style));
5905   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty;
5906   EXPECT_EQ("while (true) { }", format("while (true) {}", Style));
5907   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
5908   Style.BraceWrapping.BeforeElse = false;
5909   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
5910   verifyFormat("if (a)\n"
5911                "{\n"
5912                "} else if (b)\n"
5913                "{\n"
5914                "} else\n"
5915                "{ }",
5916                Style);
5917   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never;
5918   verifyFormat("if (a) {\n"
5919                "} else if (b) {\n"
5920                "} else {\n"
5921                "}",
5922                Style);
5923   Style.BraceWrapping.BeforeElse = true;
5924   verifyFormat("if (a) { }\n"
5925                "else if (b) { }\n"
5926                "else { }",
5927                Style);
5928 }
5929 
5930 TEST_F(FormatTest, FormatBeginBlockEndMacros) {
5931   FormatStyle Style = getLLVMStyle();
5932   Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$";
5933   Style.MacroBlockEnd = "^[A-Z_]+_END$";
5934   verifyFormat("FOO_BEGIN\n"
5935                "  FOO_ENTRY\n"
5936                "FOO_END",
5937                Style);
5938   verifyFormat("FOO_BEGIN\n"
5939                "  NESTED_FOO_BEGIN\n"
5940                "    NESTED_FOO_ENTRY\n"
5941                "  NESTED_FOO_END\n"
5942                "FOO_END",
5943                Style);
5944   verifyFormat("FOO_BEGIN(Foo, Bar)\n"
5945                "  int x;\n"
5946                "  x = 1;\n"
5947                "FOO_END(Baz)",
5948                Style);
5949 }
5950 
5951 //===----------------------------------------------------------------------===//
5952 // Line break tests.
5953 //===----------------------------------------------------------------------===//
5954 
5955 TEST_F(FormatTest, PreventConfusingIndents) {
5956   verifyFormat(
5957       "void f() {\n"
5958       "  SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
5959       "                         parameter, parameter, parameter)),\n"
5960       "                     SecondLongCall(parameter));\n"
5961       "}");
5962   verifyFormat(
5963       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5964       "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
5965       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
5966       "    aaaaaaaaaaaaaaaaaaaaaaaa);");
5967   verifyFormat(
5968       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5969       "    [aaaaaaaaaaaaaaaaaaaaaaaa\n"
5970       "         [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
5971       "         [aaaaaaaaaaaaaaaaaaaaaaaa]];");
5972   verifyFormat(
5973       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
5974       "    aaaaaaaaaaaaaaaaaaaaaaaa<\n"
5975       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
5976       "    aaaaaaaaaaaaaaaaaaaaaaaa>;");
5977   verifyFormat("int a = bbbb && ccc &&\n"
5978                "        fffff(\n"
5979                "#define A Just forcing a new line\n"
5980                "            ddd);");
5981 }
5982 
5983 TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
5984   verifyFormat(
5985       "bool aaaaaaa =\n"
5986       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
5987       "    bbbbbbbb();");
5988   verifyFormat(
5989       "bool aaaaaaa =\n"
5990       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n"
5991       "    bbbbbbbb();");
5992 
5993   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
5994                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
5995                "    ccccccccc == ddddddddddd;");
5996   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
5997                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n"
5998                "    ccccccccc == ddddddddddd;");
5999   verifyFormat(
6000       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
6001       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n"
6002       "    ccccccccc == ddddddddddd;");
6003 
6004   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
6005                "                 aaaaaa) &&\n"
6006                "         bbbbbb && cccccc;");
6007   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
6008                "                 aaaaaa) >>\n"
6009                "         bbbbbb;");
6010   verifyFormat("aa = Whitespaces.addUntouchableComment(\n"
6011                "    SourceMgr.getSpellingColumnNumber(\n"
6012                "        TheLine.Last->FormatTok.Tok.getLocation()) -\n"
6013                "    1);");
6014 
6015   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
6016                "     bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
6017                "    cccccc) {\n}");
6018   verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
6019                "               bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n"
6020                "              cccccc) {\n}");
6021   verifyFormat("if CONSTEXPR ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
6022                "               bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n"
6023                "              cccccc) {\n}");
6024   verifyFormat("b = a &&\n"
6025                "    // Comment\n"
6026                "    b.c && d;");
6027 
6028   // If the LHS of a comparison is not a binary expression itself, the
6029   // additional linebreak confuses many people.
6030   verifyFormat(
6031       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6032       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
6033       "}");
6034   verifyFormat(
6035       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6036       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
6037       "}");
6038   verifyFormat(
6039       "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
6040       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
6041       "}");
6042   verifyFormat(
6043       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6044       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) <=> 5) {\n"
6045       "}");
6046   // Even explicit parentheses stress the precedence enough to make the
6047   // additional break unnecessary.
6048   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
6049                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
6050                "}");
6051   // This cases is borderline, but with the indentation it is still readable.
6052   verifyFormat(
6053       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6054       "        aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
6055       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
6056       "}",
6057       getLLVMStyleWithColumns(75));
6058 
6059   // If the LHS is a binary expression, we should still use the additional break
6060   // as otherwise the formatting hides the operator precedence.
6061   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
6062                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
6063                "    5) {\n"
6064                "}");
6065   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
6066                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa <=>\n"
6067                "    5) {\n"
6068                "}");
6069 
6070   FormatStyle OnePerLine = getLLVMStyle();
6071   OnePerLine.BinPackParameters = false;
6072   verifyFormat(
6073       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
6074       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
6075       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
6076       OnePerLine);
6077 
6078   verifyFormat("int i = someFunction(aaaaaaa, 0)\n"
6079                "                .aaa(aaaaaaaaaaaaa) *\n"
6080                "            aaaaaaa +\n"
6081                "        aaaaaaa;",
6082                getLLVMStyleWithColumns(40));
6083 }
6084 
6085 TEST_F(FormatTest, ExpressionIndentation) {
6086   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
6087                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
6088                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
6089                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
6090                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
6091                "                     bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
6092                "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
6093                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
6094                "                 ccccccccccccccccccccccccccccccccccccccccc;");
6095   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
6096                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
6097                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
6098                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
6099   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
6100                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
6101                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
6102                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
6103   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
6104                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
6105                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
6106                "        bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
6107   verifyFormat("if () {\n"
6108                "} else if (aaaaa && bbbbb > // break\n"
6109                "                        ccccc) {\n"
6110                "}");
6111   verifyFormat("if () {\n"
6112                "} else if constexpr (aaaaa && bbbbb > // break\n"
6113                "                                  ccccc) {\n"
6114                "}");
6115   verifyFormat("if () {\n"
6116                "} else if CONSTEXPR (aaaaa && bbbbb > // break\n"
6117                "                                  ccccc) {\n"
6118                "}");
6119   verifyFormat("if () {\n"
6120                "} else if (aaaaa &&\n"
6121                "           bbbbb > // break\n"
6122                "               ccccc &&\n"
6123                "           ddddd) {\n"
6124                "}");
6125 
6126   // Presence of a trailing comment used to change indentation of b.
6127   verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
6128                "       b;\n"
6129                "return aaaaaaaaaaaaaaaaaaa +\n"
6130                "       b; //",
6131                getLLVMStyleWithColumns(30));
6132 }
6133 
6134 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
6135   // Not sure what the best system is here. Like this, the LHS can be found
6136   // immediately above an operator (everything with the same or a higher
6137   // indent). The RHS is aligned right of the operator and so compasses
6138   // everything until something with the same indent as the operator is found.
6139   // FIXME: Is this a good system?
6140   FormatStyle Style = getLLVMStyle();
6141   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
6142   verifyFormat(
6143       "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6144       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6145       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6146       "                 == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6147       "                            * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6148       "                        + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6149       "             && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6150       "                        * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6151       "                    > ccccccccccccccccccccccccccccccccccccccccc;",
6152       Style);
6153   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6154                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6155                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6156                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
6157                Style);
6158   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6159                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6160                "              * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6161                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
6162                Style);
6163   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6164                "    == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6165                "               * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6166                "           + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
6167                Style);
6168   verifyFormat("if () {\n"
6169                "} else if (aaaaa\n"
6170                "           && bbbbb // break\n"
6171                "                  > ccccc) {\n"
6172                "}",
6173                Style);
6174   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6175                "       && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
6176                Style);
6177   verifyFormat("return (a)\n"
6178                "       // comment\n"
6179                "       + b;",
6180                Style);
6181   verifyFormat(
6182       "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6183       "                 * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6184       "             + cc;",
6185       Style);
6186 
6187   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6188                "    = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
6189                Style);
6190 
6191   // Forced by comments.
6192   verifyFormat(
6193       "unsigned ContentSize =\n"
6194       "    sizeof(int16_t)   // DWARF ARange version number\n"
6195       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
6196       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
6197       "    + sizeof(int8_t); // Segment Size (in bytes)");
6198 
6199   verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
6200                "       == boost::fusion::at_c<1>(iiii).second;",
6201                Style);
6202 
6203   Style.ColumnLimit = 60;
6204   verifyFormat("zzzzzzzzzz\n"
6205                "    = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6206                "      >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
6207                Style);
6208 
6209   Style.ColumnLimit = 80;
6210   Style.IndentWidth = 4;
6211   Style.TabWidth = 4;
6212   Style.UseTab = FormatStyle::UT_Always;
6213   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
6214   Style.AlignOperands = FormatStyle::OAS_DontAlign;
6215   EXPECT_EQ("return someVeryVeryLongConditionThatBarelyFitsOnALine\n"
6216             "\t&& (someOtherLongishConditionPart1\n"
6217             "\t\t|| someOtherEvenLongerNestedConditionPart2);",
6218             format("return someVeryVeryLongConditionThatBarelyFitsOnALine && "
6219                    "(someOtherLongishConditionPart1 || "
6220                    "someOtherEvenLongerNestedConditionPart2);",
6221                    Style));
6222 }
6223 
6224 TEST_F(FormatTest, ExpressionIndentationStrictAlign) {
6225   FormatStyle Style = getLLVMStyle();
6226   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
6227   Style.AlignOperands = FormatStyle::OAS_AlignAfterOperator;
6228 
6229   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6230                "                   + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6231                "                   + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6232                "              == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6233                "                         * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6234                "                     + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6235                "          && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6236                "                     * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6237                "                 > ccccccccccccccccccccccccccccccccccccccccc;",
6238                Style);
6239   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6240                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6241                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6242                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
6243                Style);
6244   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6245                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6246                "              * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6247                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
6248                Style);
6249   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6250                "    == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6251                "               * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6252                "           + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
6253                Style);
6254   verifyFormat("if () {\n"
6255                "} else if (aaaaa\n"
6256                "           && bbbbb // break\n"
6257                "                  > ccccc) {\n"
6258                "}",
6259                Style);
6260   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6261                "    && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
6262                Style);
6263   verifyFormat("return (a)\n"
6264                "     // comment\n"
6265                "     + b;",
6266                Style);
6267   verifyFormat(
6268       "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6269       "               * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6270       "           + cc;",
6271       Style);
6272   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
6273                "     : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
6274                "                        : 3333333333333333;",
6275                Style);
6276   verifyFormat(
6277       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa    ? bbbbbbbbbbbbbbbbbb\n"
6278       "                           : ccccccccccccccc ? dddddddddddddddddd\n"
6279       "                                             : eeeeeeeeeeeeeeeeee)\n"
6280       "     : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
6281       "                        : 3333333333333333;",
6282       Style);
6283   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6284                "    = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
6285                Style);
6286 
6287   verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
6288                "    == boost::fusion::at_c<1>(iiii).second;",
6289                Style);
6290 
6291   Style.ColumnLimit = 60;
6292   verifyFormat("zzzzzzzzzzzzz\n"
6293                "    = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6294                "   >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
6295                Style);
6296 
6297   // Forced by comments.
6298   Style.ColumnLimit = 80;
6299   verifyFormat(
6300       "unsigned ContentSize\n"
6301       "    = sizeof(int16_t) // DWARF ARange version number\n"
6302       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
6303       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
6304       "    + sizeof(int8_t); // Segment Size (in bytes)",
6305       Style);
6306 
6307   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
6308   verifyFormat(
6309       "unsigned ContentSize =\n"
6310       "    sizeof(int16_t)   // DWARF ARange version number\n"
6311       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
6312       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
6313       "    + sizeof(int8_t); // Segment Size (in bytes)",
6314       Style);
6315 
6316   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
6317   verifyFormat(
6318       "unsigned ContentSize =\n"
6319       "    sizeof(int16_t)   // DWARF ARange version number\n"
6320       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
6321       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
6322       "    + sizeof(int8_t); // Segment Size (in bytes)",
6323       Style);
6324 }
6325 
6326 TEST_F(FormatTest, EnforcedOperatorWraps) {
6327   // Here we'd like to wrap after the || operators, but a comment is forcing an
6328   // earlier wrap.
6329   verifyFormat("bool x = aaaaa //\n"
6330                "         || bbbbb\n"
6331                "         //\n"
6332                "         || cccc;");
6333 }
6334 
6335 TEST_F(FormatTest, NoOperandAlignment) {
6336   FormatStyle Style = getLLVMStyle();
6337   Style.AlignOperands = FormatStyle::OAS_DontAlign;
6338   verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n"
6339                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
6340                "                   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
6341                Style);
6342   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
6343   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6344                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6345                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6346                "        == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6347                "                * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6348                "            + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6349                "    && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6350                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6351                "        > ccccccccccccccccccccccccccccccccccccccccc;",
6352                Style);
6353 
6354   verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6355                "        * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6356                "    + cc;",
6357                Style);
6358   verifyFormat("int a = aa\n"
6359                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6360                "        * cccccccccccccccccccccccccccccccccccc;\n",
6361                Style);
6362 
6363   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
6364   verifyFormat("return (a > b\n"
6365                "    // comment1\n"
6366                "    // comment2\n"
6367                "    || c);",
6368                Style);
6369 }
6370 
6371 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) {
6372   FormatStyle Style = getLLVMStyle();
6373   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
6374   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
6375                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6376                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
6377                Style);
6378 }
6379 
6380 TEST_F(FormatTest, AllowBinPackingInsideArguments) {
6381   FormatStyle Style = getLLVMStyleWithColumns(40);
6382   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
6383   Style.BinPackArguments = false;
6384   verifyFormat("void test() {\n"
6385                "  someFunction(\n"
6386                "      this + argument + is + quite\n"
6387                "      + long + so + it + gets + wrapped\n"
6388                "      + but + remains + bin - packed);\n"
6389                "}",
6390                Style);
6391   verifyFormat("void test() {\n"
6392                "  someFunction(arg1,\n"
6393                "               this + argument + is\n"
6394                "                   + quite + long + so\n"
6395                "                   + it + gets + wrapped\n"
6396                "                   + but + remains + bin\n"
6397                "                   - packed,\n"
6398                "               arg3);\n"
6399                "}",
6400                Style);
6401   verifyFormat("void test() {\n"
6402                "  someFunction(\n"
6403                "      arg1,\n"
6404                "      this + argument + has\n"
6405                "          + anotherFunc(nested,\n"
6406                "                        calls + whose\n"
6407                "                            + arguments\n"
6408                "                            + are + also\n"
6409                "                            + wrapped,\n"
6410                "                        in + addition)\n"
6411                "          + to + being + bin - packed,\n"
6412                "      arg3);\n"
6413                "}",
6414                Style);
6415 
6416   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
6417   verifyFormat("void test() {\n"
6418                "  someFunction(\n"
6419                "      arg1,\n"
6420                "      this + argument + has +\n"
6421                "          anotherFunc(nested,\n"
6422                "                      calls + whose +\n"
6423                "                          arguments +\n"
6424                "                          are + also +\n"
6425                "                          wrapped,\n"
6426                "                      in + addition) +\n"
6427                "          to + being + bin - packed,\n"
6428                "      arg3);\n"
6429                "}",
6430                Style);
6431 }
6432 
6433 TEST_F(FormatTest, ConstructorInitializers) {
6434   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
6435   verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
6436                getLLVMStyleWithColumns(45));
6437   verifyFormat("Constructor()\n"
6438                "    : Inttializer(FitsOnTheLine) {}",
6439                getLLVMStyleWithColumns(44));
6440   verifyFormat("Constructor()\n"
6441                "    : Inttializer(FitsOnTheLine) {}",
6442                getLLVMStyleWithColumns(43));
6443 
6444   verifyFormat("template <typename T>\n"
6445                "Constructor() : Initializer(FitsOnTheLine) {}",
6446                getLLVMStyleWithColumns(45));
6447 
6448   verifyFormat(
6449       "SomeClass::Constructor()\n"
6450       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
6451 
6452   verifyFormat(
6453       "SomeClass::Constructor()\n"
6454       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6455       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
6456   verifyFormat(
6457       "SomeClass::Constructor()\n"
6458       "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6459       "      aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
6460   verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6461                "            aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
6462                "    : aaaaaaaaaa(aaaaaa) {}");
6463 
6464   verifyFormat("Constructor()\n"
6465                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6466                "      aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6467                "                               aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6468                "      aaaaaaaaaaaaaaaaaaaaaaa() {}");
6469 
6470   verifyFormat("Constructor()\n"
6471                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6472                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
6473 
6474   verifyFormat("Constructor(int Parameter = 0)\n"
6475                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
6476                "      aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
6477   verifyFormat("Constructor()\n"
6478                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
6479                "}",
6480                getLLVMStyleWithColumns(60));
6481   verifyFormat("Constructor()\n"
6482                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6483                "          aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}");
6484 
6485   // Here a line could be saved by splitting the second initializer onto two
6486   // lines, but that is not desirable.
6487   verifyFormat("Constructor()\n"
6488                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
6489                "      aaaaaaaaaaa(aaaaaaaaaaa),\n"
6490                "      aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
6491 
6492   FormatStyle OnePerLine = getLLVMStyle();
6493   OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_Never;
6494   verifyFormat("MyClass::MyClass()\n"
6495                "    : a(a),\n"
6496                "      b(b),\n"
6497                "      c(c) {}",
6498                OnePerLine);
6499   verifyFormat("MyClass::MyClass()\n"
6500                "    : a(a), // comment\n"
6501                "      b(b),\n"
6502                "      c(c) {}",
6503                OnePerLine);
6504   verifyFormat("MyClass::MyClass(int a)\n"
6505                "    : b(a),      // comment\n"
6506                "      c(a + 1) { // lined up\n"
6507                "}",
6508                OnePerLine);
6509   verifyFormat("Constructor()\n"
6510                "    : a(b, b, b) {}",
6511                OnePerLine);
6512   OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6513   OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false;
6514   verifyFormat("SomeClass::Constructor()\n"
6515                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6516                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6517                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
6518                OnePerLine);
6519   verifyFormat("SomeClass::Constructor()\n"
6520                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
6521                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6522                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
6523                OnePerLine);
6524   verifyFormat("MyClass::MyClass(int var)\n"
6525                "    : some_var_(var),            // 4 space indent\n"
6526                "      some_other_var_(var + 1) { // lined up\n"
6527                "}",
6528                OnePerLine);
6529   verifyFormat("Constructor()\n"
6530                "    : aaaaa(aaaaaa),\n"
6531                "      aaaaa(aaaaaa),\n"
6532                "      aaaaa(aaaaaa),\n"
6533                "      aaaaa(aaaaaa),\n"
6534                "      aaaaa(aaaaaa) {}",
6535                OnePerLine);
6536   verifyFormat("Constructor()\n"
6537                "    : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
6538                "            aaaaaaaaaaaaaaaaaaaaaa) {}",
6539                OnePerLine);
6540   OnePerLine.BinPackParameters = false;
6541   verifyFormat(
6542       "Constructor()\n"
6543       "    : aaaaaaaaaaaaaaaaaaaaaaaa(\n"
6544       "          aaaaaaaaaaa().aaa(),\n"
6545       "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
6546       OnePerLine);
6547   OnePerLine.ColumnLimit = 60;
6548   verifyFormat("Constructor()\n"
6549                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
6550                "      bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
6551                OnePerLine);
6552 
6553   EXPECT_EQ("Constructor()\n"
6554             "    : // Comment forcing unwanted break.\n"
6555             "      aaaa(aaaa) {}",
6556             format("Constructor() :\n"
6557                    "    // Comment forcing unwanted break.\n"
6558                    "    aaaa(aaaa) {}"));
6559 }
6560 
6561 TEST_F(FormatTest, AllowAllConstructorInitializersOnNextLine) {
6562   FormatStyle Style = getLLVMStyleWithColumns(60);
6563   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
6564   Style.BinPackParameters = false;
6565 
6566   for (int i = 0; i < 4; ++i) {
6567     // Test all combinations of parameters that should not have an effect.
6568     Style.AllowAllParametersOfDeclarationOnNextLine = i & 1;
6569     Style.AllowAllArgumentsOnNextLine = i & 2;
6570 
6571     Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6572     Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
6573     verifyFormat("Constructor()\n"
6574                  "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6575                  Style);
6576     verifyFormat("Constructor() : a(a), b(b) {}", Style);
6577 
6578     Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
6579     verifyFormat("Constructor()\n"
6580                  "    : aaaaaaaaaaaaaaaaaaaa(a)\n"
6581                  "    , bbbbbbbbbbbbbbbbbbbbb(b) {}",
6582                  Style);
6583     verifyFormat("Constructor() : a(a), b(b) {}", Style);
6584 
6585     Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
6586     Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6587     verifyFormat("Constructor()\n"
6588                  "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6589                  Style);
6590 
6591     Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
6592     verifyFormat("Constructor()\n"
6593                  "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
6594                  "      bbbbbbbbbbbbbbbbbbbbb(b) {}",
6595                  Style);
6596 
6597     Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
6598     Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6599     verifyFormat("Constructor() :\n"
6600                  "    aaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6601                  Style);
6602 
6603     Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
6604     verifyFormat("Constructor() :\n"
6605                  "    aaaaaaaaaaaaaaaaaa(a),\n"
6606                  "    bbbbbbbbbbbbbbbbbbbbb(b) {}",
6607                  Style);
6608   }
6609 
6610   // Test interactions between AllowAllParametersOfDeclarationOnNextLine and
6611   // AllowAllConstructorInitializersOnNextLine in all
6612   // BreakConstructorInitializers modes
6613   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
6614   Style.AllowAllParametersOfDeclarationOnNextLine = true;
6615   verifyFormat("SomeClassWithALongName::Constructor(\n"
6616                "    int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n"
6617                "    : aaaaaaaaaaaaaaaaaaaa(a)\n"
6618                "    , bbbbbbbbbbbbbbbbbbbbb(b) {}",
6619                Style);
6620 
6621   Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6622   verifyFormat("SomeClassWithALongName::Constructor(\n"
6623                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6624                "    int bbbbbbbbbbbbb,\n"
6625                "    int cccccccccccccccc)\n"
6626                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6627                Style);
6628 
6629   Style.AllowAllParametersOfDeclarationOnNextLine = false;
6630   Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
6631   verifyFormat("SomeClassWithALongName::Constructor(\n"
6632                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6633                "    int bbbbbbbbbbbbb)\n"
6634                "    : aaaaaaaaaaaaaaaaaaaa(a)\n"
6635                "    , bbbbbbbbbbbbbbbbbbbbb(b) {}",
6636                Style);
6637 
6638   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
6639 
6640   Style.AllowAllParametersOfDeclarationOnNextLine = true;
6641   verifyFormat("SomeClassWithALongName::Constructor(\n"
6642                "    int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n"
6643                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
6644                "      bbbbbbbbbbbbbbbbbbbbb(b) {}",
6645                Style);
6646 
6647   Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6648   verifyFormat("SomeClassWithALongName::Constructor(\n"
6649                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6650                "    int bbbbbbbbbbbbb,\n"
6651                "    int cccccccccccccccc)\n"
6652                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6653                Style);
6654 
6655   Style.AllowAllParametersOfDeclarationOnNextLine = false;
6656   Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
6657   verifyFormat("SomeClassWithALongName::Constructor(\n"
6658                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6659                "    int bbbbbbbbbbbbb)\n"
6660                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
6661                "      bbbbbbbbbbbbbbbbbbbbb(b) {}",
6662                Style);
6663 
6664   Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
6665   Style.AllowAllParametersOfDeclarationOnNextLine = true;
6666   verifyFormat("SomeClassWithALongName::Constructor(\n"
6667                "    int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb) :\n"
6668                "    aaaaaaaaaaaaaaaaaaaa(a),\n"
6669                "    bbbbbbbbbbbbbbbbbbbbb(b) {}",
6670                Style);
6671 
6672   Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6673   verifyFormat("SomeClassWithALongName::Constructor(\n"
6674                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6675                "    int bbbbbbbbbbbbb,\n"
6676                "    int cccccccccccccccc) :\n"
6677                "    aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6678                Style);
6679 
6680   Style.AllowAllParametersOfDeclarationOnNextLine = false;
6681   Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
6682   verifyFormat("SomeClassWithALongName::Constructor(\n"
6683                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6684                "    int bbbbbbbbbbbbb) :\n"
6685                "    aaaaaaaaaaaaaaaaaaaa(a),\n"
6686                "    bbbbbbbbbbbbbbbbbbbbb(b) {}",
6687                Style);
6688 }
6689 
6690 TEST_F(FormatTest, AllowAllArgumentsOnNextLine) {
6691   FormatStyle Style = getLLVMStyleWithColumns(60);
6692   Style.BinPackArguments = false;
6693   for (int i = 0; i < 4; ++i) {
6694     // Test all combinations of parameters that should not have an effect.
6695     Style.AllowAllParametersOfDeclarationOnNextLine = i & 1;
6696     Style.PackConstructorInitializers =
6697         i & 2 ? FormatStyle::PCIS_BinPack : FormatStyle::PCIS_Never;
6698 
6699     Style.AllowAllArgumentsOnNextLine = true;
6700     verifyFormat("void foo() {\n"
6701                  "  FunctionCallWithReallyLongName(\n"
6702                  "      aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb);\n"
6703                  "}",
6704                  Style);
6705     Style.AllowAllArgumentsOnNextLine = false;
6706     verifyFormat("void foo() {\n"
6707                  "  FunctionCallWithReallyLongName(\n"
6708                  "      aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6709                  "      bbbbbbbbbbbb);\n"
6710                  "}",
6711                  Style);
6712 
6713     Style.AllowAllArgumentsOnNextLine = true;
6714     verifyFormat("void foo() {\n"
6715                  "  auto VariableWithReallyLongName = {\n"
6716                  "      aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb};\n"
6717                  "}",
6718                  Style);
6719     Style.AllowAllArgumentsOnNextLine = false;
6720     verifyFormat("void foo() {\n"
6721                  "  auto VariableWithReallyLongName = {\n"
6722                  "      aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6723                  "      bbbbbbbbbbbb};\n"
6724                  "}",
6725                  Style);
6726   }
6727 
6728   // This parameter should not affect declarations.
6729   Style.BinPackParameters = false;
6730   Style.AllowAllArgumentsOnNextLine = false;
6731   Style.AllowAllParametersOfDeclarationOnNextLine = true;
6732   verifyFormat("void FunctionCallWithReallyLongName(\n"
6733                "    int aaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbb);",
6734                Style);
6735   Style.AllowAllParametersOfDeclarationOnNextLine = false;
6736   verifyFormat("void FunctionCallWithReallyLongName(\n"
6737                "    int aaaaaaaaaaaaaaaaaaaaaaa,\n"
6738                "    int bbbbbbbbbbbb);",
6739                Style);
6740 }
6741 
6742 TEST_F(FormatTest, AllowAllArgumentsOnNextLineDontAlign) {
6743   // Check that AllowAllArgumentsOnNextLine is respected for both BAS_DontAlign
6744   // and BAS_Align.
6745   FormatStyle Style = getLLVMStyleWithColumns(35);
6746   StringRef Input = "functionCall(paramA, paramB, paramC);\n"
6747                     "void functionDecl(int A, int B, int C);";
6748   Style.AllowAllArgumentsOnNextLine = false;
6749   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
6750   EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n"
6751                       "    paramC);\n"
6752                       "void functionDecl(int A, int B,\n"
6753                       "    int C);"),
6754             format(Input, Style));
6755   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
6756   EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n"
6757                       "             paramC);\n"
6758                       "void functionDecl(int A, int B,\n"
6759                       "                  int C);"),
6760             format(Input, Style));
6761   // However, BAS_AlwaysBreak should take precedence over
6762   // AllowAllArgumentsOnNextLine.
6763   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
6764   EXPECT_EQ(StringRef("functionCall(\n"
6765                       "    paramA, paramB, paramC);\n"
6766                       "void functionDecl(\n"
6767                       "    int A, int B, int C);"),
6768             format(Input, Style));
6769 
6770   // When AllowAllArgumentsOnNextLine is set, we prefer breaking before the
6771   // first argument.
6772   Style.AllowAllArgumentsOnNextLine = true;
6773   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
6774   EXPECT_EQ(StringRef("functionCall(\n"
6775                       "    paramA, paramB, paramC);\n"
6776                       "void functionDecl(\n"
6777                       "    int A, int B, int C);"),
6778             format(Input, Style));
6779   // It wouldn't fit on one line with aligned parameters so this setting
6780   // doesn't change anything for BAS_Align.
6781   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
6782   EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n"
6783                       "             paramC);\n"
6784                       "void functionDecl(int A, int B,\n"
6785                       "                  int C);"),
6786             format(Input, Style));
6787   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
6788   EXPECT_EQ(StringRef("functionCall(\n"
6789                       "    paramA, paramB, paramC);\n"
6790                       "void functionDecl(\n"
6791                       "    int A, int B, int C);"),
6792             format(Input, Style));
6793 }
6794 
6795 TEST_F(FormatTest, BreakConstructorInitializersAfterColon) {
6796   FormatStyle Style = getLLVMStyle();
6797   Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
6798 
6799   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
6800   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}",
6801                getStyleWithColumns(Style, 45));
6802   verifyFormat("Constructor() :\n"
6803                "    Initializer(FitsOnTheLine) {}",
6804                getStyleWithColumns(Style, 44));
6805   verifyFormat("Constructor() :\n"
6806                "    Initializer(FitsOnTheLine) {}",
6807                getStyleWithColumns(Style, 43));
6808 
6809   verifyFormat("template <typename T>\n"
6810                "Constructor() : Initializer(FitsOnTheLine) {}",
6811                getStyleWithColumns(Style, 50));
6812   Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6813   verifyFormat(
6814       "SomeClass::Constructor() :\n"
6815       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
6816       Style);
6817 
6818   Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack;
6819   verifyFormat(
6820       "SomeClass::Constructor() :\n"
6821       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
6822       Style);
6823 
6824   verifyFormat(
6825       "SomeClass::Constructor() :\n"
6826       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6827       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
6828       Style);
6829   verifyFormat(
6830       "SomeClass::Constructor() :\n"
6831       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6832       "    aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
6833       Style);
6834   verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6835                "            aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
6836                "    aaaaaaaaaa(aaaaaa) {}",
6837                Style);
6838 
6839   verifyFormat("Constructor() :\n"
6840                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6841                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6842                "                             aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6843                "    aaaaaaaaaaaaaaaaaaaaaaa() {}",
6844                Style);
6845 
6846   verifyFormat("Constructor() :\n"
6847                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6848                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
6849                Style);
6850 
6851   verifyFormat("Constructor(int Parameter = 0) :\n"
6852                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
6853                "    aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}",
6854                Style);
6855   verifyFormat("Constructor() :\n"
6856                "    aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
6857                "}",
6858                getStyleWithColumns(Style, 60));
6859   verifyFormat("Constructor() :\n"
6860                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6861                "        aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}",
6862                Style);
6863 
6864   // Here a line could be saved by splitting the second initializer onto two
6865   // lines, but that is not desirable.
6866   verifyFormat("Constructor() :\n"
6867                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
6868                "    aaaaaaaaaaa(aaaaaaaaaaa),\n"
6869                "    aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
6870                Style);
6871 
6872   FormatStyle OnePerLine = Style;
6873   OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
6874   verifyFormat("SomeClass::Constructor() :\n"
6875                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6876                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6877                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
6878                OnePerLine);
6879   verifyFormat("SomeClass::Constructor() :\n"
6880                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
6881                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6882                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
6883                OnePerLine);
6884   verifyFormat("MyClass::MyClass(int var) :\n"
6885                "    some_var_(var),            // 4 space indent\n"
6886                "    some_other_var_(var + 1) { // lined up\n"
6887                "}",
6888                OnePerLine);
6889   verifyFormat("Constructor() :\n"
6890                "    aaaaa(aaaaaa),\n"
6891                "    aaaaa(aaaaaa),\n"
6892                "    aaaaa(aaaaaa),\n"
6893                "    aaaaa(aaaaaa),\n"
6894                "    aaaaa(aaaaaa) {}",
6895                OnePerLine);
6896   verifyFormat("Constructor() :\n"
6897                "    aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
6898                "          aaaaaaaaaaaaaaaaaaaaaa) {}",
6899                OnePerLine);
6900   OnePerLine.BinPackParameters = false;
6901   verifyFormat("Constructor() :\n"
6902                "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
6903                "        aaaaaaaaaaa().aaa(),\n"
6904                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
6905                OnePerLine);
6906   OnePerLine.ColumnLimit = 60;
6907   verifyFormat("Constructor() :\n"
6908                "    aaaaaaaaaaaaaaaaaaaa(a),\n"
6909                "    bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
6910                OnePerLine);
6911 
6912   EXPECT_EQ("Constructor() :\n"
6913             "    // Comment forcing unwanted break.\n"
6914             "    aaaa(aaaa) {}",
6915             format("Constructor() :\n"
6916                    "    // Comment forcing unwanted break.\n"
6917                    "    aaaa(aaaa) {}",
6918                    Style));
6919 
6920   Style.ColumnLimit = 0;
6921   verifyFormat("SomeClass::Constructor() :\n"
6922                "    a(a) {}",
6923                Style);
6924   verifyFormat("SomeClass::Constructor() noexcept :\n"
6925                "    a(a) {}",
6926                Style);
6927   verifyFormat("SomeClass::Constructor() :\n"
6928                "    a(a), b(b), c(c) {}",
6929                Style);
6930   verifyFormat("SomeClass::Constructor() :\n"
6931                "    a(a) {\n"
6932                "  foo();\n"
6933                "  bar();\n"
6934                "}",
6935                Style);
6936 
6937   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
6938   verifyFormat("SomeClass::Constructor() :\n"
6939                "    a(a), b(b), c(c) {\n"
6940                "}",
6941                Style);
6942   verifyFormat("SomeClass::Constructor() :\n"
6943                "    a(a) {\n"
6944                "}",
6945                Style);
6946 
6947   Style.ColumnLimit = 80;
6948   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
6949   Style.ConstructorInitializerIndentWidth = 2;
6950   verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}", Style);
6951   verifyFormat("SomeClass::Constructor() :\n"
6952                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6953                "  bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}",
6954                Style);
6955 
6956   // `ConstructorInitializerIndentWidth` actually applies to InheritanceList as
6957   // well
6958   Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
6959   verifyFormat(
6960       "class SomeClass\n"
6961       "  : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6962       "    public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
6963       Style);
6964   Style.BreakInheritanceList = FormatStyle::BILS_BeforeComma;
6965   verifyFormat(
6966       "class SomeClass\n"
6967       "  : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6968       "  , public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
6969       Style);
6970   Style.BreakInheritanceList = FormatStyle::BILS_AfterColon;
6971   verifyFormat(
6972       "class SomeClass :\n"
6973       "  public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6974       "  public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
6975       Style);
6976   Style.BreakInheritanceList = FormatStyle::BILS_AfterComma;
6977   verifyFormat(
6978       "class SomeClass\n"
6979       "  : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6980       "    public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
6981       Style);
6982 }
6983 
6984 #ifndef EXPENSIVE_CHECKS
6985 // Expensive checks enables libstdc++ checking which includes validating the
6986 // state of ranges used in std::priority_queue - this blows out the
6987 // runtime/scalability of the function and makes this test unacceptably slow.
6988 TEST_F(FormatTest, MemoizationTests) {
6989   // This breaks if the memoization lookup does not take \c Indent and
6990   // \c LastSpace into account.
6991   verifyFormat(
6992       "extern CFRunLoopTimerRef\n"
6993       "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
6994       "                     CFTimeInterval interval, CFOptionFlags flags,\n"
6995       "                     CFIndex order, CFRunLoopTimerCallBack callout,\n"
6996       "                     CFRunLoopTimerContext *context) {}");
6997 
6998   // Deep nesting somewhat works around our memoization.
6999   verifyFormat(
7000       "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
7001       "    aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
7002       "        aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
7003       "            aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
7004       "                aaaaa())))))))))))))))))))))))))))))))))))))));",
7005       getLLVMStyleWithColumns(65));
7006   verifyFormat(
7007       "aaaaa(\n"
7008       "    aaaaa,\n"
7009       "    aaaaa(\n"
7010       "        aaaaa,\n"
7011       "        aaaaa(\n"
7012       "            aaaaa,\n"
7013       "            aaaaa(\n"
7014       "                aaaaa,\n"
7015       "                aaaaa(\n"
7016       "                    aaaaa,\n"
7017       "                    aaaaa(\n"
7018       "                        aaaaa,\n"
7019       "                        aaaaa(\n"
7020       "                            aaaaa,\n"
7021       "                            aaaaa(\n"
7022       "                                aaaaa,\n"
7023       "                                aaaaa(\n"
7024       "                                    aaaaa,\n"
7025       "                                    aaaaa(\n"
7026       "                                        aaaaa,\n"
7027       "                                        aaaaa(\n"
7028       "                                            aaaaa,\n"
7029       "                                            aaaaa(\n"
7030       "                                                aaaaa,\n"
7031       "                                                aaaaa))))))))))));",
7032       getLLVMStyleWithColumns(65));
7033   verifyFormat(
7034       "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"
7035       "                                  a),\n"
7036       "                                a),\n"
7037       "                              a),\n"
7038       "                            a),\n"
7039       "                          a),\n"
7040       "                        a),\n"
7041       "                      a),\n"
7042       "                    a),\n"
7043       "                  a),\n"
7044       "                a),\n"
7045       "              a),\n"
7046       "            a),\n"
7047       "          a),\n"
7048       "        a),\n"
7049       "      a),\n"
7050       "    a),\n"
7051       "  a)",
7052       getLLVMStyleWithColumns(65));
7053 
7054   // This test takes VERY long when memoization is broken.
7055   FormatStyle OnePerLine = getLLVMStyle();
7056   OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
7057   OnePerLine.BinPackParameters = false;
7058   std::string input = "Constructor()\n"
7059                       "    : aaaa(a,\n";
7060   for (unsigned i = 0, e = 80; i != e; ++i) {
7061     input += "           a,\n";
7062   }
7063   input += "           a) {}";
7064   verifyFormat(input, OnePerLine);
7065 }
7066 #endif
7067 
7068 TEST_F(FormatTest, BreaksAsHighAsPossible) {
7069   verifyFormat(
7070       "void f() {\n"
7071       "  if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
7072       "      (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
7073       "    f();\n"
7074       "}");
7075   verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
7076                "    Intervals[i - 1].getRange().getLast()) {\n}");
7077 }
7078 
7079 TEST_F(FormatTest, BreaksFunctionDeclarations) {
7080   // Principially, we break function declarations in a certain order:
7081   // 1) break amongst arguments.
7082   verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
7083                "                              Cccccccccccccc cccccccccccccc);");
7084   verifyFormat("template <class TemplateIt>\n"
7085                "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
7086                "                            TemplateIt *stop) {}");
7087 
7088   // 2) break after return type.
7089   verifyFormat(
7090       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7091       "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);",
7092       getGoogleStyle());
7093 
7094   // 3) break after (.
7095   verifyFormat(
7096       "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
7097       "    Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);",
7098       getGoogleStyle());
7099 
7100   // 4) break before after nested name specifiers.
7101   verifyFormat(
7102       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7103       "SomeClasssssssssssssssssssssssssssssssssssssss::\n"
7104       "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);",
7105       getGoogleStyle());
7106 
7107   // However, there are exceptions, if a sufficient amount of lines can be
7108   // saved.
7109   // FIXME: The precise cut-offs wrt. the number of saved lines might need some
7110   // more adjusting.
7111   verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
7112                "                                  Cccccccccccccc cccccccccc,\n"
7113                "                                  Cccccccccccccc cccccccccc,\n"
7114                "                                  Cccccccccccccc cccccccccc,\n"
7115                "                                  Cccccccccccccc cccccccccc);");
7116   verifyFormat(
7117       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7118       "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
7119       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
7120       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);",
7121       getGoogleStyle());
7122   verifyFormat(
7123       "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
7124       "                                          Cccccccccccccc cccccccccc,\n"
7125       "                                          Cccccccccccccc cccccccccc,\n"
7126       "                                          Cccccccccccccc cccccccccc,\n"
7127       "                                          Cccccccccccccc cccccccccc,\n"
7128       "                                          Cccccccccccccc cccccccccc,\n"
7129       "                                          Cccccccccccccc cccccccccc);");
7130   verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
7131                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
7132                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
7133                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
7134                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
7135 
7136   // Break after multi-line parameters.
7137   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7138                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7139                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7140                "    bbbb bbbb);");
7141   verifyFormat("void SomeLoooooooooooongFunction(\n"
7142                "    std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
7143                "        aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7144                "    int bbbbbbbbbbbbb);");
7145 
7146   // Treat overloaded operators like other functions.
7147   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
7148                "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
7149   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
7150                "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
7151   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
7152                "operator<<(const SomeLooooooooooooooooooooooooogType &other);");
7153   verifyGoogleFormat(
7154       "SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
7155       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
7156   verifyGoogleFormat(
7157       "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
7158       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
7159   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7160                "    int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
7161   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
7162                "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
7163   verifyGoogleFormat(
7164       "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n"
7165       "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7166       "    bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}");
7167   verifyGoogleFormat("template <typename T>\n"
7168                      "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7169                      "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n"
7170                      "    aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);");
7171 
7172   FormatStyle Style = getLLVMStyle();
7173   Style.PointerAlignment = FormatStyle::PAS_Left;
7174   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7175                "    aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}",
7176                Style);
7177   verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
7178                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
7179                Style);
7180 }
7181 
7182 TEST_F(FormatTest, DontBreakBeforeQualifiedOperator) {
7183   // Regression test for https://bugs.llvm.org/show_bug.cgi?id=40516:
7184   // Prefer keeping `::` followed by `operator` together.
7185   EXPECT_EQ("const aaaa::bbbbbbb &\n"
7186             "ccccccccc::operator++() {\n"
7187             "  stuff();\n"
7188             "}",
7189             format("const aaaa::bbbbbbb\n"
7190                    "&ccccccccc::operator++() { stuff(); }",
7191                    getLLVMStyleWithColumns(40)));
7192 }
7193 
7194 TEST_F(FormatTest, TrailingReturnType) {
7195   verifyFormat("auto foo() -> int;\n");
7196   // correct trailing return type spacing
7197   verifyFormat("auto operator->() -> int;\n");
7198   verifyFormat("auto operator++(int) -> int;\n");
7199 
7200   verifyFormat("struct S {\n"
7201                "  auto bar() const -> int;\n"
7202                "};");
7203   verifyFormat("template <size_t Order, typename T>\n"
7204                "auto load_img(const std::string &filename)\n"
7205                "    -> alias::tensor<Order, T, mem::tag::cpu> {}");
7206   verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n"
7207                "    -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}");
7208   verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}");
7209   verifyFormat("template <typename T>\n"
7210                "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n"
7211                "    -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());");
7212 
7213   // Not trailing return types.
7214   verifyFormat("void f() { auto a = b->c(); }");
7215   verifyFormat("auto a = p->foo();");
7216   verifyFormat("int a = p->foo();");
7217   verifyFormat("auto lmbd = [] NOEXCEPT -> int { return 0; };");
7218 }
7219 
7220 TEST_F(FormatTest, DeductionGuides) {
7221   verifyFormat("template <class T> A(const T &, const T &) -> A<T &>;");
7222   verifyFormat("template <class T> explicit A(T &, T &&) -> A<T>;");
7223   verifyFormat("template <class... Ts> S(Ts...) -> S<Ts...>;");
7224   verifyFormat(
7225       "template <class... T>\n"
7226       "array(T &&...t) -> array<std::common_type_t<T...>, sizeof...(T)>;");
7227   verifyFormat("template <class T> A() -> A<decltype(p->foo<3>())>;");
7228   verifyFormat("template <class T> A() -> A<decltype(foo<traits<1>>)>;");
7229   verifyFormat("template <class T> A() -> A<sizeof(p->foo<1>)>;");
7230   verifyFormat("template <class T> A() -> A<(3 < 2)>;");
7231   verifyFormat("template <class T> A() -> A<((3) < (2))>;");
7232   verifyFormat("template <class T> x() -> x<1>;");
7233   verifyFormat("template <class T> explicit x(T &) -> x<1>;");
7234 
7235   // Ensure not deduction guides.
7236   verifyFormat("c()->f<int>();");
7237   verifyFormat("x()->foo<1>;");
7238   verifyFormat("x = p->foo<3>();");
7239   verifyFormat("x()->x<1>();");
7240   verifyFormat("x()->x<1>;");
7241 }
7242 
7243 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
7244   // Avoid breaking before trailing 'const' or other trailing annotations, if
7245   // they are not function-like.
7246   FormatStyle Style = getGoogleStyleWithColumns(47);
7247   verifyFormat("void someLongFunction(\n"
7248                "    int someLoooooooooooooongParameter) const {\n}",
7249                getLLVMStyleWithColumns(47));
7250   verifyFormat("LoooooongReturnType\n"
7251                "someLoooooooongFunction() const {}",
7252                getLLVMStyleWithColumns(47));
7253   verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
7254                "    const {}",
7255                Style);
7256   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
7257                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
7258   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
7259                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
7260   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
7261                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
7262   verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
7263                "                   aaaaaaaaaaa aaaaa) const override;");
7264   verifyGoogleFormat(
7265       "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7266       "    const override;");
7267 
7268   // Even if the first parameter has to be wrapped.
7269   verifyFormat("void someLongFunction(\n"
7270                "    int someLongParameter) const {}",
7271                getLLVMStyleWithColumns(46));
7272   verifyFormat("void someLongFunction(\n"
7273                "    int someLongParameter) const {}",
7274                Style);
7275   verifyFormat("void someLongFunction(\n"
7276                "    int someLongParameter) override {}",
7277                Style);
7278   verifyFormat("void someLongFunction(\n"
7279                "    int someLongParameter) OVERRIDE {}",
7280                Style);
7281   verifyFormat("void someLongFunction(\n"
7282                "    int someLongParameter) final {}",
7283                Style);
7284   verifyFormat("void someLongFunction(\n"
7285                "    int someLongParameter) FINAL {}",
7286                Style);
7287   verifyFormat("void someLongFunction(\n"
7288                "    int parameter) const override {}",
7289                Style);
7290 
7291   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
7292   verifyFormat("void someLongFunction(\n"
7293                "    int someLongParameter) const\n"
7294                "{\n"
7295                "}",
7296                Style);
7297 
7298   Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
7299   verifyFormat("void someLongFunction(\n"
7300                "    int someLongParameter) const\n"
7301                "  {\n"
7302                "  }",
7303                Style);
7304 
7305   // Unless these are unknown annotations.
7306   verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
7307                "                  aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7308                "    LONG_AND_UGLY_ANNOTATION;");
7309 
7310   // Breaking before function-like trailing annotations is fine to keep them
7311   // close to their arguments.
7312   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7313                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
7314   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
7315                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
7316   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
7317                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
7318   verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n"
7319                      "    AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);");
7320   verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});");
7321 
7322   verifyFormat(
7323       "void aaaaaaaaaaaaaaaaaa()\n"
7324       "    __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
7325       "                   aaaaaaaaaaaaaaaaaaaaaaaaa));");
7326   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7327                "    __attribute__((unused));");
7328   verifyGoogleFormat(
7329       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7330       "    GUARDED_BY(aaaaaaaaaaaa);");
7331   verifyGoogleFormat(
7332       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7333       "    GUARDED_BY(aaaaaaaaaaaa);");
7334   verifyGoogleFormat(
7335       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
7336       "    aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
7337   verifyGoogleFormat(
7338       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
7339       "    aaaaaaaaaaaaaaaaaaaaaaaaa;");
7340 }
7341 
7342 TEST_F(FormatTest, FunctionAnnotations) {
7343   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
7344                "int OldFunction(const string &parameter) {}");
7345   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
7346                "string OldFunction(const string &parameter) {}");
7347   verifyFormat("template <typename T>\n"
7348                "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
7349                "string OldFunction(const string &parameter) {}");
7350 
7351   // Not function annotations.
7352   verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7353                "                << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
7354   verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n"
7355                "       ThisIsATestWithAReallyReallyReallyReallyLongName) {}");
7356   verifyFormat("MACRO(abc).function() // wrap\n"
7357                "    << abc;");
7358   verifyFormat("MACRO(abc)->function() // wrap\n"
7359                "    << abc;");
7360   verifyFormat("MACRO(abc)::function() // wrap\n"
7361                "    << abc;");
7362 }
7363 
7364 TEST_F(FormatTest, BreaksDesireably) {
7365   verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
7366                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
7367                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
7368   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7369                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
7370                "}");
7371 
7372   verifyFormat(
7373       "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7374       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
7375 
7376   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7377                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7378                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
7379 
7380   verifyFormat(
7381       "aaaaaaaa(aaaaaaaaaaaaa,\n"
7382       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7383       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
7384       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7385       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
7386 
7387   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
7388                "    (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7389 
7390   verifyFormat(
7391       "void f() {\n"
7392       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
7393       "                                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
7394       "}");
7395   verifyFormat(
7396       "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7397       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
7398   verifyFormat(
7399       "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7400       "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
7401   verifyFormat(
7402       "aaaaaa(aaa,\n"
7403       "       new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7404       "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7405       "       aaaa);");
7406   verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7407                "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7408                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7409 
7410   // Indent consistently independent of call expression and unary operator.
7411   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
7412                "    dddddddddddddddddddddddddddddd));");
7413   verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
7414                "    dddddddddddddddddddddddddddddd));");
7415   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
7416                "    dddddddddddddddddddddddddddddd));");
7417 
7418   // This test case breaks on an incorrect memoization, i.e. an optimization not
7419   // taking into account the StopAt value.
7420   verifyFormat(
7421       "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
7422       "       aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
7423       "       aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
7424       "       (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7425 
7426   verifyFormat("{\n  {\n    {\n"
7427                "      Annotation.SpaceRequiredBefore =\n"
7428                "          Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
7429                "          Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
7430                "    }\n  }\n}");
7431 
7432   // Break on an outer level if there was a break on an inner level.
7433   EXPECT_EQ("f(g(h(a, // comment\n"
7434             "      b, c),\n"
7435             "    d, e),\n"
7436             "  x, y);",
7437             format("f(g(h(a, // comment\n"
7438                    "    b, c), d, e), x, y);"));
7439 
7440   // Prefer breaking similar line breaks.
7441   verifyFormat(
7442       "const int kTrackingOptions = NSTrackingMouseMoved |\n"
7443       "                             NSTrackingMouseEnteredAndExited |\n"
7444       "                             NSTrackingActiveAlways;");
7445 }
7446 
7447 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) {
7448   FormatStyle NoBinPacking = getGoogleStyle();
7449   NoBinPacking.BinPackParameters = false;
7450   NoBinPacking.BinPackArguments = true;
7451   verifyFormat("void f() {\n"
7452                "  f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n"
7453                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
7454                "}",
7455                NoBinPacking);
7456   verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n"
7457                "       int aaaaaaaaaaaaaaaaaaaa,\n"
7458                "       int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
7459                NoBinPacking);
7460 
7461   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
7462   verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7463                "                        vector<int> bbbbbbbbbbbbbbb);",
7464                NoBinPacking);
7465   // FIXME: This behavior difference is probably not wanted. However, currently
7466   // we cannot distinguish BreakBeforeParameter being set because of the wrapped
7467   // template arguments from BreakBeforeParameter being set because of the
7468   // one-per-line formatting.
7469   verifyFormat(
7470       "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n"
7471       "                                             aaaaaaaaaa> aaaaaaaaaa);",
7472       NoBinPacking);
7473   verifyFormat(
7474       "void fffffffffff(\n"
7475       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n"
7476       "        aaaaaaaaaa);");
7477 }
7478 
7479 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
7480   FormatStyle NoBinPacking = getGoogleStyle();
7481   NoBinPacking.BinPackParameters = false;
7482   NoBinPacking.BinPackArguments = false;
7483   verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
7484                "  aaaaaaaaaaaaaaaaaaaa,\n"
7485                "  aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
7486                NoBinPacking);
7487   verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
7488                "        aaaaaaaaaaaaa,\n"
7489                "        aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
7490                NoBinPacking);
7491   verifyFormat(
7492       "aaaaaaaa(aaaaaaaaaaaaa,\n"
7493       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7494       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
7495       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7496       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
7497       NoBinPacking);
7498   verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
7499                "    .aaaaaaaaaaaaaaaaaa();",
7500                NoBinPacking);
7501   verifyFormat("void f() {\n"
7502                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7503                "      aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n"
7504                "}",
7505                NoBinPacking);
7506 
7507   verifyFormat(
7508       "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7509       "             aaaaaaaaaaaa,\n"
7510       "             aaaaaaaaaaaa);",
7511       NoBinPacking);
7512   verifyFormat(
7513       "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
7514       "                               ddddddddddddddddddddddddddddd),\n"
7515       "             test);",
7516       NoBinPacking);
7517 
7518   verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
7519                "            aaaaaaaaaaaaaaaaaaaaaaa,\n"
7520                "            aaaaaaaaaaaaaaaaaaaaaaa>\n"
7521                "    aaaaaaaaaaaaaaaaaa;",
7522                NoBinPacking);
7523   verifyFormat("a(\"a\"\n"
7524                "  \"a\",\n"
7525                "  a);");
7526 
7527   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
7528   verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
7529                "                aaaaaaaaa,\n"
7530                "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7531                NoBinPacking);
7532   verifyFormat(
7533       "void f() {\n"
7534       "  aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
7535       "      .aaaaaaa();\n"
7536       "}",
7537       NoBinPacking);
7538   verifyFormat(
7539       "template <class SomeType, class SomeOtherType>\n"
7540       "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
7541       NoBinPacking);
7542 }
7543 
7544 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
7545   FormatStyle Style = getLLVMStyleWithColumns(15);
7546   Style.ExperimentalAutoDetectBinPacking = true;
7547   EXPECT_EQ("aaa(aaaa,\n"
7548             "    aaaa,\n"
7549             "    aaaa);\n"
7550             "aaa(aaaa,\n"
7551             "    aaaa,\n"
7552             "    aaaa);",
7553             format("aaa(aaaa,\n" // one-per-line
7554                    "  aaaa,\n"
7555                    "    aaaa  );\n"
7556                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
7557                    Style));
7558   EXPECT_EQ("aaa(aaaa, aaaa,\n"
7559             "    aaaa);\n"
7560             "aaa(aaaa, aaaa,\n"
7561             "    aaaa);",
7562             format("aaa(aaaa,  aaaa,\n" // bin-packed
7563                    "    aaaa  );\n"
7564                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
7565                    Style));
7566 }
7567 
7568 TEST_F(FormatTest, FormatsBuilderPattern) {
7569   verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n"
7570                "    .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
7571                "    .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
7572                "    .StartsWith(\".init\", ORDER_INIT)\n"
7573                "    .StartsWith(\".fini\", ORDER_FINI)\n"
7574                "    .StartsWith(\".hash\", ORDER_HASH)\n"
7575                "    .Default(ORDER_TEXT);\n");
7576 
7577   verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
7578                "       aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
7579   verifyFormat("aaaaaaa->aaaaaaa\n"
7580                "    ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7581                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7582                "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
7583   verifyFormat(
7584       "aaaaaaa->aaaaaaa\n"
7585       "    ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7586       "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
7587   verifyFormat(
7588       "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
7589       "    aaaaaaaaaaaaaa);");
7590   verifyFormat(
7591       "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
7592       "    aaaaaa->aaaaaaaaaaaa()\n"
7593       "        ->aaaaaaaaaaaaaaaa(\n"
7594       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7595       "        ->aaaaaaaaaaaaaaaaa();");
7596   verifyGoogleFormat(
7597       "void f() {\n"
7598       "  someo->Add((new util::filetools::Handler(dir))\n"
7599       "                 ->OnEvent1(NewPermanentCallback(\n"
7600       "                     this, &HandlerHolderClass::EventHandlerCBA))\n"
7601       "                 ->OnEvent2(NewPermanentCallback(\n"
7602       "                     this, &HandlerHolderClass::EventHandlerCBB))\n"
7603       "                 ->OnEvent3(NewPermanentCallback(\n"
7604       "                     this, &HandlerHolderClass::EventHandlerCBC))\n"
7605       "                 ->OnEvent5(NewPermanentCallback(\n"
7606       "                     this, &HandlerHolderClass::EventHandlerCBD))\n"
7607       "                 ->OnEvent6(NewPermanentCallback(\n"
7608       "                     this, &HandlerHolderClass::EventHandlerCBE)));\n"
7609       "}");
7610 
7611   verifyFormat(
7612       "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
7613   verifyFormat("aaaaaaaaaaaaaaa()\n"
7614                "    .aaaaaaaaaaaaaaa()\n"
7615                "    .aaaaaaaaaaaaaaa()\n"
7616                "    .aaaaaaaaaaaaaaa()\n"
7617                "    .aaaaaaaaaaaaaaa();");
7618   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
7619                "    .aaaaaaaaaaaaaaa()\n"
7620                "    .aaaaaaaaaaaaaaa()\n"
7621                "    .aaaaaaaaaaaaaaa();");
7622   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
7623                "    .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
7624                "    .aaaaaaaaaaaaaaa();");
7625   verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
7626                "    ->aaaaaaaaaaaaaae(0)\n"
7627                "    ->aaaaaaaaaaaaaaa();");
7628 
7629   // Don't linewrap after very short segments.
7630   verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7631                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7632                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
7633   verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7634                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7635                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
7636   verifyFormat("aaa()\n"
7637                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7638                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7639                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
7640 
7641   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
7642                "    .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7643                "    .has<bbbbbbbbbbbbbbbbbbbbb>();");
7644   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
7645                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
7646                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
7647 
7648   // Prefer not to break after empty parentheses.
7649   verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
7650                "    First->LastNewlineOffset);");
7651 
7652   // Prefer not to create "hanging" indents.
7653   verifyFormat(
7654       "return !soooooooooooooome_map\n"
7655       "            .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7656       "            .second;");
7657   verifyFormat(
7658       "return aaaaaaaaaaaaaaaa\n"
7659       "    .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n"
7660       "    .aaaa(aaaaaaaaaaaaaa);");
7661   // No hanging indent here.
7662   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n"
7663                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7664   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n"
7665                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7666   verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
7667                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7668                getLLVMStyleWithColumns(60));
7669   verifyFormat("aaaaaaaaaaaaaaaaaa\n"
7670                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
7671                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7672                getLLVMStyleWithColumns(59));
7673   verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7674                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7675                "    .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7676 
7677   // Dont break if only closing statements before member call
7678   verifyFormat("test() {\n"
7679                "  ([]() -> {\n"
7680                "    int b = 32;\n"
7681                "    return 3;\n"
7682                "  }).foo();\n"
7683                "}");
7684   verifyFormat("test() {\n"
7685                "  (\n"
7686                "      []() -> {\n"
7687                "        int b = 32;\n"
7688                "        return 3;\n"
7689                "      },\n"
7690                "      foo, bar)\n"
7691                "      .foo();\n"
7692                "}");
7693   verifyFormat("test() {\n"
7694                "  ([]() -> {\n"
7695                "    int b = 32;\n"
7696                "    return 3;\n"
7697                "  })\n"
7698                "      .foo()\n"
7699                "      .bar();\n"
7700                "}");
7701   verifyFormat("test() {\n"
7702                "  ([]() -> {\n"
7703                "    int b = 32;\n"
7704                "    return 3;\n"
7705                "  })\n"
7706                "      .foo(\"aaaaaaaaaaaaaaaaa\"\n"
7707                "           \"bbbb\");\n"
7708                "}",
7709                getLLVMStyleWithColumns(30));
7710 }
7711 
7712 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
7713   verifyFormat(
7714       "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
7715       "    bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
7716   verifyFormat(
7717       "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n"
7718       "    bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}");
7719 
7720   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
7721                "    ccccccccccccccccccccccccc) {\n}");
7722   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n"
7723                "    ccccccccccccccccccccccccc) {\n}");
7724 
7725   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
7726                "    ccccccccccccccccccccccccc) {\n}");
7727   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n"
7728                "    ccccccccccccccccccccccccc) {\n}");
7729 
7730   verifyFormat(
7731       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
7732       "    ccccccccccccccccccccccccc) {\n}");
7733   verifyFormat(
7734       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n"
7735       "    ccccccccccccccccccccccccc) {\n}");
7736 
7737   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
7738                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
7739                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
7740                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
7741   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n"
7742                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n"
7743                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n"
7744                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
7745 
7746   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
7747                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
7748                "    aaaaaaaaaaaaaaa != aa) {\n}");
7749   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n"
7750                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n"
7751                "    aaaaaaaaaaaaaaa != aa) {\n}");
7752 }
7753 
7754 TEST_F(FormatTest, BreaksAfterAssignments) {
7755   verifyFormat(
7756       "unsigned Cost =\n"
7757       "    TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
7758       "                        SI->getPointerAddressSpaceee());\n");
7759   verifyFormat(
7760       "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
7761       "    Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
7762 
7763   verifyFormat(
7764       "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
7765       "    aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
7766   verifyFormat("unsigned OriginalStartColumn =\n"
7767                "    SourceMgr.getSpellingColumnNumber(\n"
7768                "        Current.FormatTok.getStartOfNonWhitespace()) -\n"
7769                "    1;");
7770 }
7771 
7772 TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) {
7773   FormatStyle Style = getLLVMStyle();
7774   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
7775                "    bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;",
7776                Style);
7777 
7778   Style.PenaltyBreakAssignment = 20;
7779   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
7780                "                                 cccccccccccccccccccccccccc;",
7781                Style);
7782 }
7783 
7784 TEST_F(FormatTest, AlignsAfterAssignments) {
7785   verifyFormat(
7786       "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7787       "             aaaaaaaaaaaaaaaaaaaaaaaaa;");
7788   verifyFormat(
7789       "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7790       "          aaaaaaaaaaaaaaaaaaaaaaaaa;");
7791   verifyFormat(
7792       "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7793       "           aaaaaaaaaaaaaaaaaaaaaaaaa;");
7794   verifyFormat(
7795       "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7796       "              aaaaaaaaaaaaaaaaaaaaaaaaa);");
7797   verifyFormat(
7798       "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
7799       "                                            aaaaaaaaaaaaaaaaaaaaaaaa +\n"
7800       "                                            aaaaaaaaaaaaaaaaaaaaaaaa;");
7801 }
7802 
7803 TEST_F(FormatTest, AlignsAfterReturn) {
7804   verifyFormat(
7805       "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7806       "       aaaaaaaaaaaaaaaaaaaaaaaaa;");
7807   verifyFormat(
7808       "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7809       "        aaaaaaaaaaaaaaaaaaaaaaaaa);");
7810   verifyFormat(
7811       "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
7812       "       aaaaaaaaaaaaaaaaaaaaaa();");
7813   verifyFormat(
7814       "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
7815       "        aaaaaaaaaaaaaaaaaaaaaa());");
7816   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7817                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7818   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7819                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
7820                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
7821   verifyFormat("return\n"
7822                "    // true if code is one of a or b.\n"
7823                "    code == a || code == b;");
7824 }
7825 
7826 TEST_F(FormatTest, AlignsAfterOpenBracket) {
7827   verifyFormat(
7828       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
7829       "                                                aaaaaaaaa aaaaaaa) {}");
7830   verifyFormat(
7831       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
7832       "                                               aaaaaaaaaaa aaaaaaaaa);");
7833   verifyFormat(
7834       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
7835       "                                             aaaaaaaaaaaaaaaaaaaaa));");
7836   FormatStyle Style = getLLVMStyle();
7837   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
7838   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7839                "    aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}",
7840                Style);
7841   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
7842                "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);",
7843                Style);
7844   verifyFormat("SomeLongVariableName->someFunction(\n"
7845                "    foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));",
7846                Style);
7847   verifyFormat(
7848       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
7849       "    aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
7850       Style);
7851   verifyFormat(
7852       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
7853       "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7854       Style);
7855   verifyFormat(
7856       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
7857       "    aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
7858       Style);
7859 
7860   verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n"
7861                "    ccccccc(aaaaaaaaaaaaaaaaa,         //\n"
7862                "        b));",
7863                Style);
7864 
7865   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
7866   Style.BinPackArguments = false;
7867   Style.BinPackParameters = false;
7868   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7869                "    aaaaaaaaaaa aaaaaaaa,\n"
7870                "    aaaaaaaaa aaaaaaa,\n"
7871                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
7872                Style);
7873   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
7874                "    aaaaaaaaaaa aaaaaaaaa,\n"
7875                "    aaaaaaaaaaa aaaaaaaaa,\n"
7876                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7877                Style);
7878   verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n"
7879                "    aaaaaaaaaaaaaaa,\n"
7880                "    aaaaaaaaaaaaaaaaaaaaa,\n"
7881                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
7882                Style);
7883   verifyFormat(
7884       "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n"
7885       "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
7886       Style);
7887   verifyFormat(
7888       "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n"
7889       "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
7890       Style);
7891   verifyFormat(
7892       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
7893       "    aaaaaaaaaaaaaaaaaaaaa(\n"
7894       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n"
7895       "    aaaaaaaaaaaaaaaa);",
7896       Style);
7897   verifyFormat(
7898       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
7899       "    aaaaaaaaaaaaaaaaaaaaa(\n"
7900       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n"
7901       "    aaaaaaaaaaaaaaaa);",
7902       Style);
7903 }
7904 
7905 TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
7906   FormatStyle Style = getLLVMStyleWithColumns(40);
7907   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
7908                "          bbbbbbbbbbbbbbbbbbbbbb);",
7909                Style);
7910   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
7911   Style.AlignOperands = FormatStyle::OAS_DontAlign;
7912   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
7913                "          bbbbbbbbbbbbbbbbbbbbbb);",
7914                Style);
7915   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
7916   Style.AlignOperands = FormatStyle::OAS_Align;
7917   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
7918                "          bbbbbbbbbbbbbbbbbbbbbb);",
7919                Style);
7920   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
7921   Style.AlignOperands = FormatStyle::OAS_DontAlign;
7922   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
7923                "    bbbbbbbbbbbbbbbbbbbbbb);",
7924                Style);
7925 }
7926 
7927 TEST_F(FormatTest, BreaksConditionalExpressions) {
7928   verifyFormat(
7929       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7930       "                               ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7931       "                               : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7932   verifyFormat(
7933       "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
7934       "     aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7935       "                                : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7936   verifyFormat(
7937       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7938       "                                   : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7939   verifyFormat("aaaa(aaaaaaaaa, aaaaaaaaa,\n"
7940                "     aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7941                "             : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7942   verifyFormat(
7943       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
7944       "                                                    : aaaaaaaaaaaaa);");
7945   verifyFormat(
7946       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7947       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7948       "                                    : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7949       "                   aaaaaaaaaaaaa);");
7950   verifyFormat(
7951       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7952       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7953       "                   aaaaaaaaaaaaa);");
7954   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7955                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7956                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7957                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7958                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7959   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7960                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7961                "           ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7962                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7963                "           : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7964                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7965                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7966   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7967                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7968                "           ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7969                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7970                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7971   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7972                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7973                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
7974   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
7975                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7976                "        ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7977                "        : aaaaaaaaaaaaaaaa;");
7978   verifyFormat(
7979       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7980       "    ? aaaaaaaaaaaaaaa\n"
7981       "    : aaaaaaaaaaaaaaa;");
7982   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
7983                "          aaaaaaaaa\n"
7984                "      ? b\n"
7985                "      : c);");
7986   verifyFormat("return aaaa == bbbb\n"
7987                "           // comment\n"
7988                "           ? aaaa\n"
7989                "           : bbbb;");
7990   verifyFormat("unsigned Indent =\n"
7991                "    format(TheLine.First,\n"
7992                "           IndentForLevel[TheLine.Level] >= 0\n"
7993                "               ? IndentForLevel[TheLine.Level]\n"
7994                "               : TheLine * 2,\n"
7995                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
7996                getLLVMStyleWithColumns(60));
7997   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
7998                "                  ? aaaaaaaaaaaaaaa\n"
7999                "                  : bbbbbbbbbbbbbbb //\n"
8000                "                        ? ccccccccccccccc\n"
8001                "                        : ddddddddddddddd;");
8002   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
8003                "                  ? aaaaaaaaaaaaaaa\n"
8004                "                  : (bbbbbbbbbbbbbbb //\n"
8005                "                         ? ccccccccccccccc\n"
8006                "                         : ddddddddddddddd);");
8007   verifyFormat(
8008       "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8009       "                                      ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
8010       "                                            aaaaaaaaaaaaaaaaaaaaa +\n"
8011       "                                            aaaaaaaaaaaaaaaaaaaaa\n"
8012       "                                      : aaaaaaaaaa;");
8013   verifyFormat(
8014       "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8015       "                                   : aaaaaaaaaaaaaaaaaaaaaa\n"
8016       "                      : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8017 
8018   FormatStyle NoBinPacking = getLLVMStyle();
8019   NoBinPacking.BinPackArguments = false;
8020   verifyFormat(
8021       "void f() {\n"
8022       "  g(aaa,\n"
8023       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
8024       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8025       "        ? aaaaaaaaaaaaaaa\n"
8026       "        : aaaaaaaaaaaaaaa);\n"
8027       "}",
8028       NoBinPacking);
8029   verifyFormat(
8030       "void f() {\n"
8031       "  g(aaa,\n"
8032       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
8033       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8034       "        ?: aaaaaaaaaaaaaaa);\n"
8035       "}",
8036       NoBinPacking);
8037 
8038   verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n"
8039                "             // comment.\n"
8040                "             ccccccccccccccccccccccccccccccccccccccc\n"
8041                "                 ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8042                "                 : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);");
8043 
8044   // Assignments in conditional expressions. Apparently not uncommon :-(.
8045   verifyFormat("return a != b\n"
8046                "           // comment\n"
8047                "           ? a = b\n"
8048                "           : a = b;");
8049   verifyFormat("return a != b\n"
8050                "           // comment\n"
8051                "           ? a = a != b\n"
8052                "                     // comment\n"
8053                "                     ? a = b\n"
8054                "                     : a\n"
8055                "           : a;\n");
8056   verifyFormat("return a != b\n"
8057                "           // comment\n"
8058                "           ? a\n"
8059                "           : a = a != b\n"
8060                "                     // comment\n"
8061                "                     ? a = b\n"
8062                "                     : a;");
8063 
8064   // Chained conditionals
8065   FormatStyle Style = getLLVMStyleWithColumns(70);
8066   Style.AlignOperands = FormatStyle::OAS_Align;
8067   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
8068                "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
8069                "                        : 3333333333333333;",
8070                Style);
8071   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
8072                "       : bbbbbbbbbb     ? 2222222222222222\n"
8073                "                        : 3333333333333333;",
8074                Style);
8075   verifyFormat("return aaaaaaaaaa         ? 1111111111111111\n"
8076                "       : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
8077                "                          : 3333333333333333;",
8078                Style);
8079   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
8080                "       : bbbbbbbbbbbbbb ? 222222\n"
8081                "                        : 333333;",
8082                Style);
8083   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
8084                "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
8085                "       : cccccccccccccc ? 3333333333333333\n"
8086                "                        : 4444444444444444;",
8087                Style);
8088   verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc)\n"
8089                "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
8090                "                        : 3333333333333333;",
8091                Style);
8092   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
8093                "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
8094                "                        : (aaa ? bbb : ccc);",
8095                Style);
8096   verifyFormat(
8097       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
8098       "                                             : cccccccccccccccccc)\n"
8099       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
8100       "                        : 3333333333333333;",
8101       Style);
8102   verifyFormat(
8103       "return aaaaaaaaa        ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
8104       "                                             : cccccccccccccccccc)\n"
8105       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
8106       "                        : 3333333333333333;",
8107       Style);
8108   verifyFormat(
8109       "return aaaaaaaaa        ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
8110       "                                             : dddddddddddddddddd)\n"
8111       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
8112       "                        : 3333333333333333;",
8113       Style);
8114   verifyFormat(
8115       "return aaaaaaaaa        ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
8116       "                                             : dddddddddddddddddd)\n"
8117       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
8118       "                        : 3333333333333333;",
8119       Style);
8120   verifyFormat(
8121       "return aaaaaaaaa        ? 1111111111111111\n"
8122       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
8123       "                        : a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
8124       "                                             : dddddddddddddddddd)\n",
8125       Style);
8126   verifyFormat(
8127       "return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
8128       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
8129       "                        : (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
8130       "                                             : cccccccccccccccccc);",
8131       Style);
8132   verifyFormat(
8133       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
8134       "                           : ccccccccccccccc ? dddddddddddddddddd\n"
8135       "                                             : eeeeeeeeeeeeeeeeee)\n"
8136       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
8137       "                        : 3333333333333333;",
8138       Style);
8139   verifyFormat(
8140       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa    ? bbbbbbbbbbbbbbbbbb\n"
8141       "                           : ccccccccccccccc ? dddddddddddddddddd\n"
8142       "                                             : eeeeeeeeeeeeeeeeee)\n"
8143       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
8144       "                        : 3333333333333333;",
8145       Style);
8146   verifyFormat(
8147       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
8148       "                           : cccccccccccc    ? dddddddddddddddddd\n"
8149       "                                             : eeeeeeeeeeeeeeeeee)\n"
8150       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
8151       "                        : 3333333333333333;",
8152       Style);
8153   verifyFormat(
8154       "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
8155       "                                             : cccccccccccccccccc\n"
8156       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
8157       "                        : 3333333333333333;",
8158       Style);
8159   verifyFormat(
8160       "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
8161       "                          : cccccccccccccccc ? dddddddddddddddddd\n"
8162       "                                             : eeeeeeeeeeeeeeeeee\n"
8163       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
8164       "                        : 3333333333333333;",
8165       Style);
8166   verifyFormat("return aaaaaaaaaaaaaaaaaaaaa\n"
8167                "           ? (aaaaaaaaaaaaaaaaaa   ? bbbbbbbbbbbbbbbbbb\n"
8168                "              : cccccccccccccccccc ? dddddddddddddddddd\n"
8169                "                                   : eeeeeeeeeeeeeeeeee)\n"
8170                "       : bbbbbbbbbbbbbbbbbbb ? 2222222222222222\n"
8171                "                             : 3333333333333333;",
8172                Style);
8173   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaa\n"
8174                "           ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
8175                "             : cccccccccccccccc ? dddddddddddddddddd\n"
8176                "                                : eeeeeeeeeeeeeeeeee\n"
8177                "       : bbbbbbbbbbbbbbbbbbbbbbb ? 2222222222222222\n"
8178                "                                 : 3333333333333333;",
8179                Style);
8180 
8181   Style.AlignOperands = FormatStyle::OAS_DontAlign;
8182   Style.BreakBeforeTernaryOperators = false;
8183   // FIXME: Aligning the question marks is weird given DontAlign.
8184   // Consider disabling this alignment in this case. Also check whether this
8185   // will render the adjustment from https://reviews.llvm.org/D82199
8186   // unnecessary.
8187   verifyFormat("int x = aaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa :\n"
8188                "    bbbb                ? cccccccccccccccccc :\n"
8189                "                          ddddd;\n",
8190                Style);
8191 
8192   EXPECT_EQ(
8193       "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n"
8194       "    /*\n"
8195       "     */\n"
8196       "    function() {\n"
8197       "      try {\n"
8198       "        return JJJJJJJJJJJJJJ(\n"
8199       "            pppppppppppppppppppppppppppppppppppppppppppppppppp);\n"
8200       "      }\n"
8201       "    } :\n"
8202       "    function() {};",
8203       format(
8204           "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n"
8205           "     /*\n"
8206           "      */\n"
8207           "     function() {\n"
8208           "      try {\n"
8209           "        return JJJJJJJJJJJJJJ(\n"
8210           "            pppppppppppppppppppppppppppppppppppppppppppppppppp);\n"
8211           "      }\n"
8212           "    } :\n"
8213           "    function() {};",
8214           getGoogleStyle(FormatStyle::LK_JavaScript)));
8215 }
8216 
8217 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
8218   FormatStyle Style = getLLVMStyleWithColumns(70);
8219   Style.BreakBeforeTernaryOperators = false;
8220   verifyFormat(
8221       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
8222       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
8223       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
8224       Style);
8225   verifyFormat(
8226       "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
8227       "     aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
8228       "                                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
8229       Style);
8230   verifyFormat(
8231       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
8232       "                                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
8233       Style);
8234   verifyFormat("aaaa(aaaaaaaa, aaaaaaaaaa,\n"
8235                "     aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
8236                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
8237                Style);
8238   verifyFormat(
8239       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
8240       "                                                      aaaaaaaaaaaaa);",
8241       Style);
8242   verifyFormat(
8243       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8244       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
8245       "                                      aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8246       "                   aaaaaaaaaaaaa);",
8247       Style);
8248   verifyFormat(
8249       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8250       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8251       "                   aaaaaaaaaaaaa);",
8252       Style);
8253   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
8254                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8255                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
8256                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8257                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
8258                Style);
8259   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8260                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
8261                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8262                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
8263                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8264                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
8265                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
8266                Style);
8267   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8268                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n"
8269                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8270                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
8271                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
8272                Style);
8273   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
8274                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
8275                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
8276                Style);
8277   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
8278                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
8279                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
8280                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
8281                Style);
8282   verifyFormat(
8283       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
8284       "    aaaaaaaaaaaaaaa :\n"
8285       "    aaaaaaaaaaaaaaa;",
8286       Style);
8287   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
8288                "          aaaaaaaaa ?\n"
8289                "      b :\n"
8290                "      c);",
8291                Style);
8292   verifyFormat("unsigned Indent =\n"
8293                "    format(TheLine.First,\n"
8294                "           IndentForLevel[TheLine.Level] >= 0 ?\n"
8295                "               IndentForLevel[TheLine.Level] :\n"
8296                "               TheLine * 2,\n"
8297                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
8298                Style);
8299   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
8300                "                  aaaaaaaaaaaaaaa :\n"
8301                "                  bbbbbbbbbbbbbbb ? //\n"
8302                "                      ccccccccccccccc :\n"
8303                "                      ddddddddddddddd;",
8304                Style);
8305   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
8306                "                  aaaaaaaaaaaaaaa :\n"
8307                "                  (bbbbbbbbbbbbbbb ? //\n"
8308                "                       ccccccccccccccc :\n"
8309                "                       ddddddddddddddd);",
8310                Style);
8311   verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
8312                "            /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n"
8313                "            ccccccccccccccccccccccccccc;",
8314                Style);
8315   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
8316                "           aaaaa :\n"
8317                "           bbbbbbbbbbbbbbb + cccccccccccccccc;",
8318                Style);
8319 
8320   // Chained conditionals
8321   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
8322                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8323                "                          3333333333333333;",
8324                Style);
8325   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
8326                "       bbbbbbbbbb       ? 2222222222222222 :\n"
8327                "                          3333333333333333;",
8328                Style);
8329   verifyFormat("return aaaaaaaaaa       ? 1111111111111111 :\n"
8330                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8331                "                          3333333333333333;",
8332                Style);
8333   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
8334                "       bbbbbbbbbbbbbbbb ? 222222 :\n"
8335                "                          333333;",
8336                Style);
8337   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
8338                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8339                "       cccccccccccccccc ? 3333333333333333 :\n"
8340                "                          4444444444444444;",
8341                Style);
8342   verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc) :\n"
8343                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8344                "                          3333333333333333;",
8345                Style);
8346   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
8347                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8348                "                          (aaa ? bbb : ccc);",
8349                Style);
8350   verifyFormat(
8351       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8352       "                                               cccccccccccccccccc) :\n"
8353       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8354       "                          3333333333333333;",
8355       Style);
8356   verifyFormat(
8357       "return aaaaaaaaa        ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8358       "                                               cccccccccccccccccc) :\n"
8359       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8360       "                          3333333333333333;",
8361       Style);
8362   verifyFormat(
8363       "return aaaaaaaaa        ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8364       "                                               dddddddddddddddddd) :\n"
8365       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8366       "                          3333333333333333;",
8367       Style);
8368   verifyFormat(
8369       "return aaaaaaaaa        ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8370       "                                               dddddddddddddddddd) :\n"
8371       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8372       "                          3333333333333333;",
8373       Style);
8374   verifyFormat(
8375       "return aaaaaaaaa        ? 1111111111111111 :\n"
8376       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8377       "                          a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8378       "                                               dddddddddddddddddd)\n",
8379       Style);
8380   verifyFormat(
8381       "return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
8382       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8383       "                          (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8384       "                                               cccccccccccccccccc);",
8385       Style);
8386   verifyFormat(
8387       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8388       "                           ccccccccccccccccc ? dddddddddddddddddd :\n"
8389       "                                               eeeeeeeeeeeeeeeeee) :\n"
8390       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8391       "                          3333333333333333;",
8392       Style);
8393   verifyFormat(
8394       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8395       "                           ccccccccccccc     ? dddddddddddddddddd :\n"
8396       "                                               eeeeeeeeeeeeeeeeee) :\n"
8397       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8398       "                          3333333333333333;",
8399       Style);
8400   verifyFormat(
8401       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaa     ? bbbbbbbbbbbbbbbbbb :\n"
8402       "                           ccccccccccccccccc ? dddddddddddddddddd :\n"
8403       "                                               eeeeeeeeeeeeeeeeee) :\n"
8404       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8405       "                          3333333333333333;",
8406       Style);
8407   verifyFormat(
8408       "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8409       "                                               cccccccccccccccccc :\n"
8410       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8411       "                          3333333333333333;",
8412       Style);
8413   verifyFormat(
8414       "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8415       "                          cccccccccccccccccc ? dddddddddddddddddd :\n"
8416       "                                               eeeeeeeeeeeeeeeeee :\n"
8417       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8418       "                          3333333333333333;",
8419       Style);
8420   verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n"
8421                "           (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8422                "            cccccccccccccccccc ? dddddddddddddddddd :\n"
8423                "                                 eeeeeeeeeeeeeeeeee) :\n"
8424                "       bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8425                "                               3333333333333333;",
8426                Style);
8427   verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n"
8428                "           aaaaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8429                "           cccccccccccccccccccc ? dddddddddddddddddd :\n"
8430                "                                  eeeeeeeeeeeeeeeeee :\n"
8431                "       bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8432                "                               3333333333333333;",
8433                Style);
8434 }
8435 
8436 TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
8437   verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
8438                "     aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
8439   verifyFormat("bool a = true, b = false;");
8440 
8441   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
8442                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
8443                "     bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
8444                "         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
8445   verifyFormat(
8446       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
8447       "         bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
8448       "     d = e && f;");
8449   verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
8450                "          c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
8451   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
8452                "          *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
8453   verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
8454                "          ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
8455 
8456   FormatStyle Style = getGoogleStyle();
8457   Style.PointerAlignment = FormatStyle::PAS_Left;
8458   Style.DerivePointerAlignment = false;
8459   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8460                "    *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
8461                "    *b = bbbbbbbbbbbbbbbbbbb;",
8462                Style);
8463   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
8464                "          *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;",
8465                Style);
8466   verifyFormat("vector<int*> a, b;", Style);
8467   verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style);
8468   verifyFormat("/*comment*/ for (int *p, *q; p != q; p = p->next) {\n}", Style);
8469   verifyFormat("if (int *p, *q; p != q) {\n  p = p->next;\n}", Style);
8470   verifyFormat("/*comment*/ if (int *p, *q; p != q) {\n  p = p->next;\n}",
8471                Style);
8472   verifyFormat("switch (int *p, *q; p != q) {\n  default:\n    break;\n}",
8473                Style);
8474   verifyFormat(
8475       "/*comment*/ switch (int *p, *q; p != q) {\n  default:\n    break;\n}",
8476       Style);
8477 
8478   verifyFormat("if ([](int* p, int* q) {}()) {\n}", Style);
8479   verifyFormat("for ([](int* p, int* q) {}();;) {\n}", Style);
8480   verifyFormat("for (; [](int* p, int* q) {}();) {\n}", Style);
8481   verifyFormat("for (;; [](int* p, int* q) {}()) {\n}", Style);
8482   verifyFormat("switch ([](int* p, int* q) {}()) {\n  default:\n    break;\n}",
8483                Style);
8484 }
8485 
8486 TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
8487   verifyFormat("arr[foo ? bar : baz];");
8488   verifyFormat("f()[foo ? bar : baz];");
8489   verifyFormat("(a + b)[foo ? bar : baz];");
8490   verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
8491 }
8492 
8493 TEST_F(FormatTest, AlignsStringLiterals) {
8494   verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
8495                "                                      \"short literal\");");
8496   verifyFormat(
8497       "looooooooooooooooooooooooongFunction(\n"
8498       "    \"short literal\"\n"
8499       "    \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
8500   verifyFormat("someFunction(\"Always break between multi-line\"\n"
8501                "             \" string literals\",\n"
8502                "             and, other, parameters);");
8503   EXPECT_EQ("fun + \"1243\" /* comment */\n"
8504             "      \"5678\";",
8505             format("fun + \"1243\" /* comment */\n"
8506                    "    \"5678\";",
8507                    getLLVMStyleWithColumns(28)));
8508   EXPECT_EQ(
8509       "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
8510       "         \"aaaaaaaaaaaaaaaaaaaaa\"\n"
8511       "         \"aaaaaaaaaaaaaaaa\";",
8512       format("aaaaaa ="
8513              "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
8514              "aaaaaaaaaaaaaaaaaaaaa\" "
8515              "\"aaaaaaaaaaaaaaaa\";"));
8516   verifyFormat("a = a + \"a\"\n"
8517                "        \"a\"\n"
8518                "        \"a\";");
8519   verifyFormat("f(\"a\", \"b\"\n"
8520                "       \"c\");");
8521 
8522   verifyFormat(
8523       "#define LL_FORMAT \"ll\"\n"
8524       "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
8525       "       \"d, ddddddddd: %\" LL_FORMAT \"d\");");
8526 
8527   verifyFormat("#define A(X)          \\\n"
8528                "  \"aaaaa\" #X \"bbbbbb\" \\\n"
8529                "  \"ccccc\"",
8530                getLLVMStyleWithColumns(23));
8531   verifyFormat("#define A \"def\"\n"
8532                "f(\"abc\" A \"ghi\"\n"
8533                "  \"jkl\");");
8534 
8535   verifyFormat("f(L\"a\"\n"
8536                "  L\"b\");");
8537   verifyFormat("#define A(X)            \\\n"
8538                "  L\"aaaaa\" #X L\"bbbbbb\" \\\n"
8539                "  L\"ccccc\"",
8540                getLLVMStyleWithColumns(25));
8541 
8542   verifyFormat("f(@\"a\"\n"
8543                "  @\"b\");");
8544   verifyFormat("NSString s = @\"a\"\n"
8545                "             @\"b\"\n"
8546                "             @\"c\";");
8547   verifyFormat("NSString s = @\"a\"\n"
8548                "              \"b\"\n"
8549                "              \"c\";");
8550 }
8551 
8552 TEST_F(FormatTest, ReturnTypeBreakingStyle) {
8553   FormatStyle Style = getLLVMStyle();
8554   // No declarations or definitions should be moved to own line.
8555   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None;
8556   verifyFormat("class A {\n"
8557                "  int f() { return 1; }\n"
8558                "  int g();\n"
8559                "};\n"
8560                "int f() { return 1; }\n"
8561                "int g();\n",
8562                Style);
8563 
8564   // All declarations and definitions should have the return type moved to its
8565   // own line.
8566   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
8567   Style.TypenameMacros = {"LIST"};
8568   verifyFormat("SomeType\n"
8569                "funcdecl(LIST(uint64_t));",
8570                Style);
8571   verifyFormat("class E {\n"
8572                "  int\n"
8573                "  f() {\n"
8574                "    return 1;\n"
8575                "  }\n"
8576                "  int\n"
8577                "  g();\n"
8578                "};\n"
8579                "int\n"
8580                "f() {\n"
8581                "  return 1;\n"
8582                "}\n"
8583                "int\n"
8584                "g();\n",
8585                Style);
8586 
8587   // Top-level definitions, and no kinds of declarations should have the
8588   // return type moved to its own line.
8589   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions;
8590   verifyFormat("class B {\n"
8591                "  int f() { return 1; }\n"
8592                "  int g();\n"
8593                "};\n"
8594                "int\n"
8595                "f() {\n"
8596                "  return 1;\n"
8597                "}\n"
8598                "int g();\n",
8599                Style);
8600 
8601   // Top-level definitions and declarations should have the return type moved
8602   // to its own line.
8603   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel;
8604   verifyFormat("class C {\n"
8605                "  int f() { return 1; }\n"
8606                "  int g();\n"
8607                "};\n"
8608                "int\n"
8609                "f() {\n"
8610                "  return 1;\n"
8611                "}\n"
8612                "int\n"
8613                "g();\n",
8614                Style);
8615 
8616   // All definitions should have the return type moved to its own line, but no
8617   // kinds of declarations.
8618   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
8619   verifyFormat("class D {\n"
8620                "  int\n"
8621                "  f() {\n"
8622                "    return 1;\n"
8623                "  }\n"
8624                "  int g();\n"
8625                "};\n"
8626                "int\n"
8627                "f() {\n"
8628                "  return 1;\n"
8629                "}\n"
8630                "int g();\n",
8631                Style);
8632   verifyFormat("const char *\n"
8633                "f(void) {\n" // Break here.
8634                "  return \"\";\n"
8635                "}\n"
8636                "const char *bar(void);\n", // No break here.
8637                Style);
8638   verifyFormat("template <class T>\n"
8639                "T *\n"
8640                "f(T &c) {\n" // Break here.
8641                "  return NULL;\n"
8642                "}\n"
8643                "template <class T> T *f(T &c);\n", // No break here.
8644                Style);
8645   verifyFormat("class C {\n"
8646                "  int\n"
8647                "  operator+() {\n"
8648                "    return 1;\n"
8649                "  }\n"
8650                "  int\n"
8651                "  operator()() {\n"
8652                "    return 1;\n"
8653                "  }\n"
8654                "};\n",
8655                Style);
8656   verifyFormat("void\n"
8657                "A::operator()() {}\n"
8658                "void\n"
8659                "A::operator>>() {}\n"
8660                "void\n"
8661                "A::operator+() {}\n"
8662                "void\n"
8663                "A::operator*() {}\n"
8664                "void\n"
8665                "A::operator->() {}\n"
8666                "void\n"
8667                "A::operator void *() {}\n"
8668                "void\n"
8669                "A::operator void &() {}\n"
8670                "void\n"
8671                "A::operator void &&() {}\n"
8672                "void\n"
8673                "A::operator char *() {}\n"
8674                "void\n"
8675                "A::operator[]() {}\n"
8676                "void\n"
8677                "A::operator!() {}\n"
8678                "void\n"
8679                "A::operator**() {}\n"
8680                "void\n"
8681                "A::operator<Foo> *() {}\n"
8682                "void\n"
8683                "A::operator<Foo> **() {}\n"
8684                "void\n"
8685                "A::operator<Foo> &() {}\n"
8686                "void\n"
8687                "A::operator void **() {}\n",
8688                Style);
8689   verifyFormat("constexpr auto\n"
8690                "operator()() const -> reference {}\n"
8691                "constexpr auto\n"
8692                "operator>>() const -> reference {}\n"
8693                "constexpr auto\n"
8694                "operator+() const -> reference {}\n"
8695                "constexpr auto\n"
8696                "operator*() const -> reference {}\n"
8697                "constexpr auto\n"
8698                "operator->() const -> reference {}\n"
8699                "constexpr auto\n"
8700                "operator++() const -> reference {}\n"
8701                "constexpr auto\n"
8702                "operator void *() const -> reference {}\n"
8703                "constexpr auto\n"
8704                "operator void **() const -> reference {}\n"
8705                "constexpr auto\n"
8706                "operator void *() const -> reference {}\n"
8707                "constexpr auto\n"
8708                "operator void &() const -> reference {}\n"
8709                "constexpr auto\n"
8710                "operator void &&() const -> reference {}\n"
8711                "constexpr auto\n"
8712                "operator char *() const -> reference {}\n"
8713                "constexpr auto\n"
8714                "operator!() const -> reference {}\n"
8715                "constexpr auto\n"
8716                "operator[]() const -> reference {}\n",
8717                Style);
8718   verifyFormat("void *operator new(std::size_t s);", // No break here.
8719                Style);
8720   verifyFormat("void *\n"
8721                "operator new(std::size_t s) {}",
8722                Style);
8723   verifyFormat("void *\n"
8724                "operator delete[](void *ptr) {}",
8725                Style);
8726   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
8727   verifyFormat("const char *\n"
8728                "f(void)\n" // Break here.
8729                "{\n"
8730                "  return \"\";\n"
8731                "}\n"
8732                "const char *bar(void);\n", // No break here.
8733                Style);
8734   verifyFormat("template <class T>\n"
8735                "T *\n"     // Problem here: no line break
8736                "f(T &c)\n" // Break here.
8737                "{\n"
8738                "  return NULL;\n"
8739                "}\n"
8740                "template <class T> T *f(T &c);\n", // No break here.
8741                Style);
8742   verifyFormat("int\n"
8743                "foo(A<bool> a)\n"
8744                "{\n"
8745                "  return a;\n"
8746                "}\n",
8747                Style);
8748   verifyFormat("int\n"
8749                "foo(A<8> a)\n"
8750                "{\n"
8751                "  return a;\n"
8752                "}\n",
8753                Style);
8754   verifyFormat("int\n"
8755                "foo(A<B<bool>, 8> a)\n"
8756                "{\n"
8757                "  return a;\n"
8758                "}\n",
8759                Style);
8760   verifyFormat("int\n"
8761                "foo(A<B<8>, bool> a)\n"
8762                "{\n"
8763                "  return a;\n"
8764                "}\n",
8765                Style);
8766   verifyFormat("int\n"
8767                "foo(A<B<bool>, bool> a)\n"
8768                "{\n"
8769                "  return a;\n"
8770                "}\n",
8771                Style);
8772   verifyFormat("int\n"
8773                "foo(A<B<8>, 8> a)\n"
8774                "{\n"
8775                "  return a;\n"
8776                "}\n",
8777                Style);
8778 
8779   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
8780   Style.BraceWrapping.AfterFunction = true;
8781   verifyFormat("int f(i);\n" // No break here.
8782                "int\n"       // Break here.
8783                "f(i)\n"
8784                "{\n"
8785                "  return i + 1;\n"
8786                "}\n"
8787                "int\n" // Break here.
8788                "f(i)\n"
8789                "{\n"
8790                "  return i + 1;\n"
8791                "};",
8792                Style);
8793   verifyFormat("int f(a, b, c);\n" // No break here.
8794                "int\n"             // Break here.
8795                "f(a, b, c)\n"      // Break here.
8796                "short a, b;\n"
8797                "float c;\n"
8798                "{\n"
8799                "  return a + b < c;\n"
8800                "}\n"
8801                "int\n"        // Break here.
8802                "f(a, b, c)\n" // Break here.
8803                "short a, b;\n"
8804                "float c;\n"
8805                "{\n"
8806                "  return a + b < c;\n"
8807                "};",
8808                Style);
8809   verifyFormat("byte *\n" // Break here.
8810                "f(a)\n"   // Break here.
8811                "byte a[];\n"
8812                "{\n"
8813                "  return a;\n"
8814                "}",
8815                Style);
8816   verifyFormat("bool f(int a, int) override;\n"
8817                "Bar g(int a, Bar) final;\n"
8818                "Bar h(a, Bar) final;",
8819                Style);
8820   verifyFormat("int\n"
8821                "f(a)",
8822                Style);
8823   verifyFormat("bool\n"
8824                "f(size_t = 0, bool b = false)\n"
8825                "{\n"
8826                "  return !b;\n"
8827                "}",
8828                Style);
8829 
8830   // The return breaking style doesn't affect:
8831   // * function and object definitions with attribute-like macros
8832   verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n"
8833                "    ABSL_GUARDED_BY(mutex) = {};",
8834                getGoogleStyleWithColumns(40));
8835   verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n"
8836                "    ABSL_GUARDED_BY(mutex);  // comment",
8837                getGoogleStyleWithColumns(40));
8838   verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n"
8839                "    ABSL_GUARDED_BY(mutex1)\n"
8840                "        ABSL_GUARDED_BY(mutex2);",
8841                getGoogleStyleWithColumns(40));
8842   verifyFormat("Tttttt f(int a, int b)\n"
8843                "    ABSL_GUARDED_BY(mutex1)\n"
8844                "        ABSL_GUARDED_BY(mutex2);",
8845                getGoogleStyleWithColumns(40));
8846   // * typedefs
8847   verifyFormat("typedef ATTR(X) char x;", getGoogleStyle());
8848 
8849   Style = getGNUStyle();
8850 
8851   // Test for comments at the end of function declarations.
8852   verifyFormat("void\n"
8853                "foo (int a, /*abc*/ int b) // def\n"
8854                "{\n"
8855                "}\n",
8856                Style);
8857 
8858   verifyFormat("void\n"
8859                "foo (int a, /* abc */ int b) /* def */\n"
8860                "{\n"
8861                "}\n",
8862                Style);
8863 
8864   // Definitions that should not break after return type
8865   verifyFormat("void foo (int a, int b); // def\n", Style);
8866   verifyFormat("void foo (int a, int b); /* def */\n", Style);
8867   verifyFormat("void foo (int a, int b);\n", Style);
8868 }
8869 
8870 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
8871   FormatStyle NoBreak = getLLVMStyle();
8872   NoBreak.AlwaysBreakBeforeMultilineStrings = false;
8873   FormatStyle Break = getLLVMStyle();
8874   Break.AlwaysBreakBeforeMultilineStrings = true;
8875   verifyFormat("aaaa = \"bbbb\"\n"
8876                "       \"cccc\";",
8877                NoBreak);
8878   verifyFormat("aaaa =\n"
8879                "    \"bbbb\"\n"
8880                "    \"cccc\";",
8881                Break);
8882   verifyFormat("aaaa(\"bbbb\"\n"
8883                "     \"cccc\");",
8884                NoBreak);
8885   verifyFormat("aaaa(\n"
8886                "    \"bbbb\"\n"
8887                "    \"cccc\");",
8888                Break);
8889   verifyFormat("aaaa(qqq, \"bbbb\"\n"
8890                "          \"cccc\");",
8891                NoBreak);
8892   verifyFormat("aaaa(qqq,\n"
8893                "     \"bbbb\"\n"
8894                "     \"cccc\");",
8895                Break);
8896   verifyFormat("aaaa(qqq,\n"
8897                "     L\"bbbb\"\n"
8898                "     L\"cccc\");",
8899                Break);
8900   verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n"
8901                "                      \"bbbb\"));",
8902                Break);
8903   verifyFormat("string s = someFunction(\n"
8904                "    \"abc\"\n"
8905                "    \"abc\");",
8906                Break);
8907 
8908   // As we break before unary operators, breaking right after them is bad.
8909   verifyFormat("string foo = abc ? \"x\"\n"
8910                "                   \"blah blah blah blah blah blah\"\n"
8911                "                 : \"y\";",
8912                Break);
8913 
8914   // Don't break if there is no column gain.
8915   verifyFormat("f(\"aaaa\"\n"
8916                "  \"bbbb\");",
8917                Break);
8918 
8919   // Treat literals with escaped newlines like multi-line string literals.
8920   EXPECT_EQ("x = \"a\\\n"
8921             "b\\\n"
8922             "c\";",
8923             format("x = \"a\\\n"
8924                    "b\\\n"
8925                    "c\";",
8926                    NoBreak));
8927   EXPECT_EQ("xxxx =\n"
8928             "    \"a\\\n"
8929             "b\\\n"
8930             "c\";",
8931             format("xxxx = \"a\\\n"
8932                    "b\\\n"
8933                    "c\";",
8934                    Break));
8935 
8936   EXPECT_EQ("NSString *const kString =\n"
8937             "    @\"aaaa\"\n"
8938             "    @\"bbbb\";",
8939             format("NSString *const kString = @\"aaaa\"\n"
8940                    "@\"bbbb\";",
8941                    Break));
8942 
8943   Break.ColumnLimit = 0;
8944   verifyFormat("const char *hello = \"hello llvm\";", Break);
8945 }
8946 
8947 TEST_F(FormatTest, AlignsPipes) {
8948   verifyFormat(
8949       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8950       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8951       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8952   verifyFormat(
8953       "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
8954       "                     << aaaaaaaaaaaaaaaaaaaa;");
8955   verifyFormat(
8956       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8957       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8958   verifyFormat(
8959       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
8960       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8961   verifyFormat(
8962       "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
8963       "                \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
8964       "             << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
8965   verifyFormat(
8966       "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8967       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8968       "         << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8969   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8970                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8971                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8972                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
8973   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n"
8974                "             << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);");
8975   verifyFormat(
8976       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8977       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8978   verifyFormat(
8979       "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n"
8980       "                                       aaaaaaaaaaaaaaaaaaaaaaaaaa);");
8981 
8982   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
8983                "             << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
8984   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8985                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8986                "                    aaaaaaaaaaaaaaaaaaaaa)\n"
8987                "             << aaaaaaaaaaaaaaaaaaaaaaaaaa;");
8988   verifyFormat("LOG_IF(aaa == //\n"
8989                "       bbb)\n"
8990                "    << a << b;");
8991 
8992   // But sometimes, breaking before the first "<<" is desirable.
8993   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
8994                "    << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
8995   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
8996                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8997                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8998   verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
8999                "    << BEF << IsTemplate << Description << E->getType();");
9000   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
9001                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9002                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9003   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
9004                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9005                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9006                "    << aaa;");
9007 
9008   verifyFormat(
9009       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9010       "                    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
9011 
9012   // Incomplete string literal.
9013   EXPECT_EQ("llvm::errs() << \"\n"
9014             "             << a;",
9015             format("llvm::errs() << \"\n<<a;"));
9016 
9017   verifyFormat("void f() {\n"
9018                "  CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
9019                "      << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
9020                "}");
9021 
9022   // Handle 'endl'.
9023   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n"
9024                "             << bbbbbbbbbbbbbbbbbbbbbb << endl;");
9025   verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;");
9026 
9027   // Handle '\n'.
9028   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n"
9029                "             << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
9030   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n"
9031                "             << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';");
9032   verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n"
9033                "             << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";");
9034   verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
9035 }
9036 
9037 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) {
9038   verifyFormat("return out << \"somepacket = {\\n\"\n"
9039                "           << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
9040                "           << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
9041                "           << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
9042                "           << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
9043                "           << \"}\";");
9044 
9045   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
9046                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
9047                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
9048   verifyFormat(
9049       "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
9050       "             << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
9051       "             << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
9052       "             << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
9053       "             << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
9054   verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
9055                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
9056   verifyFormat(
9057       "void f() {\n"
9058       "  llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
9059       "               << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
9060       "}");
9061 
9062   // Breaking before the first "<<" is generally not desirable.
9063   verifyFormat(
9064       "llvm::errs()\n"
9065       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9066       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9067       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9068       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
9069       getLLVMStyleWithColumns(70));
9070   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
9071                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9072                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
9073                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9074                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
9075                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
9076                getLLVMStyleWithColumns(70));
9077 
9078   verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
9079                "           \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
9080                "           \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;");
9081   verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
9082                "                  \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
9083                "                  \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);");
9084   verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n"
9085                "           (aaaa + aaaa);",
9086                getLLVMStyleWithColumns(40));
9087   verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n"
9088                "                  (aaaaaaa + aaaaa));",
9089                getLLVMStyleWithColumns(40));
9090   verifyFormat(
9091       "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n"
9092       "                  SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n"
9093       "                  bbbbbbbbbbbbbbbbbbbbbbb);");
9094 }
9095 
9096 TEST_F(FormatTest, UnderstandsEquals) {
9097   verifyFormat(
9098       "aaaaaaaaaaaaaaaaa =\n"
9099       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
9100   verifyFormat(
9101       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
9102       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
9103   verifyFormat(
9104       "if (a) {\n"
9105       "  f();\n"
9106       "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
9107       "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
9108       "}");
9109 
9110   verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
9111                "        100000000 + 10000000) {\n}");
9112 }
9113 
9114 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
9115   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
9116                "    .looooooooooooooooooooooooooooooooooooooongFunction();");
9117 
9118   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
9119                "    ->looooooooooooooooooooooooooooooooooooooongFunction();");
9120 
9121   verifyFormat(
9122       "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
9123       "                                                          Parameter2);");
9124 
9125   verifyFormat(
9126       "ShortObject->shortFunction(\n"
9127       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
9128       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
9129 
9130   verifyFormat("loooooooooooooongFunction(\n"
9131                "    LoooooooooooooongObject->looooooooooooooooongFunction());");
9132 
9133   verifyFormat(
9134       "function(LoooooooooooooooooooooooooooooooooooongObject\n"
9135       "             ->loooooooooooooooooooooooooooooooooooooooongFunction());");
9136 
9137   verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
9138                "    .WillRepeatedly(Return(SomeValue));");
9139   verifyFormat("void f() {\n"
9140                "  EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
9141                "      .Times(2)\n"
9142                "      .WillRepeatedly(Return(SomeValue));\n"
9143                "}");
9144   verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
9145                "    ccccccccccccccccccccccc);");
9146   verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9147                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9148                "          .aaaaa(aaaaa),\n"
9149                "      aaaaaaaaaaaaaaaaaaaaa);");
9150   verifyFormat("void f() {\n"
9151                "  aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9152                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
9153                "}");
9154   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9155                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9156                "    .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9157                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9158                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
9159   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9160                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9161                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9162                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
9163                "}");
9164 
9165   // Here, it is not necessary to wrap at "." or "->".
9166   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
9167                "    aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
9168   verifyFormat(
9169       "aaaaaaaaaaa->aaaaaaaaa(\n"
9170       "    aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9171       "    aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
9172 
9173   verifyFormat(
9174       "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9175       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
9176   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
9177                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
9178   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
9179                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
9180 
9181   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9182                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9183                "    .a();");
9184 
9185   FormatStyle NoBinPacking = getLLVMStyle();
9186   NoBinPacking.BinPackParameters = false;
9187   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
9188                "    .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
9189                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
9190                "                         aaaaaaaaaaaaaaaaaaa,\n"
9191                "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9192                NoBinPacking);
9193 
9194   // If there is a subsequent call, change to hanging indentation.
9195   verifyFormat(
9196       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9197       "                         aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
9198       "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
9199   verifyFormat(
9200       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9201       "    aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
9202   verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9203                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9204                "                 .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
9205   verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9206                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9207                "               .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
9208 }
9209 
9210 TEST_F(FormatTest, WrapsTemplateDeclarations) {
9211   verifyFormat("template <typename T>\n"
9212                "virtual void loooooooooooongFunction(int Param1, int Param2);");
9213   verifyFormat("template <typename T>\n"
9214                "// T should be one of {A, B}.\n"
9215                "virtual void loooooooooooongFunction(int Param1, int Param2);");
9216   verifyFormat(
9217       "template <typename T>\n"
9218       "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
9219   verifyFormat("template <typename T>\n"
9220                "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
9221                "       int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
9222   verifyFormat(
9223       "template <typename T>\n"
9224       "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
9225       "                                      int Paaaaaaaaaaaaaaaaaaaaram2);");
9226   verifyFormat(
9227       "template <typename T>\n"
9228       "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
9229       "                    aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
9230       "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9231   verifyFormat("template <typename T>\n"
9232                "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9233                "    int aaaaaaaaaaaaaaaaaaaaaa);");
9234   verifyFormat(
9235       "template <typename T1, typename T2 = char, typename T3 = char,\n"
9236       "          typename T4 = char>\n"
9237       "void f();");
9238   verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
9239                "          template <typename> class cccccccccccccccccccccc,\n"
9240                "          typename ddddddddddddd>\n"
9241                "class C {};");
9242   verifyFormat(
9243       "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
9244       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9245 
9246   verifyFormat("void f() {\n"
9247                "  a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
9248                "      a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
9249                "}");
9250 
9251   verifyFormat("template <typename T> class C {};");
9252   verifyFormat("template <typename T> void f();");
9253   verifyFormat("template <typename T> void f() {}");
9254   verifyFormat(
9255       "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
9256       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9257       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
9258       "    new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
9259       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9260       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
9261       "        bbbbbbbbbbbbbbbbbbbbbbbb);",
9262       getLLVMStyleWithColumns(72));
9263   EXPECT_EQ("static_cast<A< //\n"
9264             "    B> *>(\n"
9265             "\n"
9266             ");",
9267             format("static_cast<A<//\n"
9268                    "    B>*>(\n"
9269                    "\n"
9270                    "    );"));
9271   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9272                "    const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
9273 
9274   FormatStyle AlwaysBreak = getLLVMStyle();
9275   AlwaysBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
9276   verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
9277   verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
9278   verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
9279   verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9280                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
9281                "    ccccccccccccccccccccccccccccccccccccccccccccccc);");
9282   verifyFormat("template <template <typename> class Fooooooo,\n"
9283                "          template <typename> class Baaaaaaar>\n"
9284                "struct C {};",
9285                AlwaysBreak);
9286   verifyFormat("template <typename T> // T can be A, B or C.\n"
9287                "struct C {};",
9288                AlwaysBreak);
9289   verifyFormat("template <enum E> class A {\n"
9290                "public:\n"
9291                "  E *f();\n"
9292                "};");
9293 
9294   FormatStyle NeverBreak = getLLVMStyle();
9295   NeverBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_No;
9296   verifyFormat("template <typename T> class C {};", NeverBreak);
9297   verifyFormat("template <typename T> void f();", NeverBreak);
9298   verifyFormat("template <typename T> void f() {}", NeverBreak);
9299   verifyFormat("template <typename T>\nvoid foo(aaaaaaaaaaaaaaaaaaaaaaaaaa "
9300                "bbbbbbbbbbbbbbbbbbbb) {}",
9301                NeverBreak);
9302   verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9303                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
9304                "    ccccccccccccccccccccccccccccccccccccccccccccccc);",
9305                NeverBreak);
9306   verifyFormat("template <template <typename> class Fooooooo,\n"
9307                "          template <typename> class Baaaaaaar>\n"
9308                "struct C {};",
9309                NeverBreak);
9310   verifyFormat("template <typename T> // T can be A, B or C.\n"
9311                "struct C {};",
9312                NeverBreak);
9313   verifyFormat("template <enum E> class A {\n"
9314                "public:\n"
9315                "  E *f();\n"
9316                "};",
9317                NeverBreak);
9318   NeverBreak.PenaltyBreakTemplateDeclaration = 100;
9319   verifyFormat("template <typename T> void\nfoo(aaaaaaaaaaaaaaaaaaaaaaaaaa "
9320                "bbbbbbbbbbbbbbbbbbbb) {}",
9321                NeverBreak);
9322 }
9323 
9324 TEST_F(FormatTest, WrapsTemplateDeclarationsWithComments) {
9325   FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp);
9326   Style.ColumnLimit = 60;
9327   EXPECT_EQ("// Baseline - no comments.\n"
9328             "template <\n"
9329             "    typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n"
9330             "void f() {}",
9331             format("// Baseline - no comments.\n"
9332                    "template <\n"
9333                    "    typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n"
9334                    "void f() {}",
9335                    Style));
9336 
9337   EXPECT_EQ("template <\n"
9338             "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value>  // trailing\n"
9339             "void f() {}",
9340             format("template <\n"
9341                    "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
9342                    "void f() {}",
9343                    Style));
9344 
9345   EXPECT_EQ(
9346       "template <\n"
9347       "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n"
9348       "void f() {}",
9349       format("template <typename aaaaaaaaaa<bbbbbbbbbbbb>::value>  /* line */\n"
9350              "void f() {}",
9351              Style));
9352 
9353   EXPECT_EQ(
9354       "template <\n"
9355       "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value>  // trailing\n"
9356       "                                               // multiline\n"
9357       "void f() {}",
9358       format("template <\n"
9359              "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
9360              "                                              // multiline\n"
9361              "void f() {}",
9362              Style));
9363 
9364   EXPECT_EQ(
9365       "template <typename aaaaaaaaaa<\n"
9366       "    bbbbbbbbbbbb>::value>  // trailing loooong\n"
9367       "void f() {}",
9368       format(
9369           "template <\n"
9370           "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing loooong\n"
9371           "void f() {}",
9372           Style));
9373 }
9374 
9375 TEST_F(FormatTest, WrapsTemplateParameters) {
9376   FormatStyle Style = getLLVMStyle();
9377   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
9378   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
9379   verifyFormat(
9380       "template <typename... a> struct q {};\n"
9381       "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
9382       "    aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
9383       "    y;",
9384       Style);
9385   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
9386   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
9387   verifyFormat(
9388       "template <typename... a> struct r {};\n"
9389       "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
9390       "    aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
9391       "    y;",
9392       Style);
9393   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
9394   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
9395   verifyFormat("template <typename... a> struct s {};\n"
9396                "extern s<\n"
9397                "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
9398                "aaaaaaaaaaaaaaaaaaaaaa,\n"
9399                "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
9400                "aaaaaaaaaaaaaaaaaaaaaa>\n"
9401                "    y;",
9402                Style);
9403   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
9404   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
9405   verifyFormat("template <typename... a> struct t {};\n"
9406                "extern t<\n"
9407                "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
9408                "aaaaaaaaaaaaaaaaaaaaaa,\n"
9409                "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
9410                "aaaaaaaaaaaaaaaaaaaaaa>\n"
9411                "    y;",
9412                Style);
9413 }
9414 
9415 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
9416   verifyFormat(
9417       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
9418       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
9419   verifyFormat(
9420       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
9421       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9422       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
9423 
9424   // FIXME: Should we have the extra indent after the second break?
9425   verifyFormat(
9426       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
9427       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
9428       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
9429 
9430   verifyFormat(
9431       "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
9432       "                    cccccccccccccccccccccccccccccccccccccccccccccc());");
9433 
9434   // Breaking at nested name specifiers is generally not desirable.
9435   verifyFormat(
9436       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9437       "    aaaaaaaaaaaaaaaaaaaaaaa);");
9438 
9439   verifyFormat("aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n"
9440                "                   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
9441                "                       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9442                "                   aaaaaaaaaaaaaaaaaaaaa);",
9443                getLLVMStyleWithColumns(74));
9444 
9445   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
9446                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9447                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
9448 }
9449 
9450 TEST_F(FormatTest, UnderstandsTemplateParameters) {
9451   verifyFormat("A<int> a;");
9452   verifyFormat("A<A<A<int>>> a;");
9453   verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
9454   verifyFormat("bool x = a < 1 || 2 > a;");
9455   verifyFormat("bool x = 5 < f<int>();");
9456   verifyFormat("bool x = f<int>() > 5;");
9457   verifyFormat("bool x = 5 < a<int>::x;");
9458   verifyFormat("bool x = a < 4 ? a > 2 : false;");
9459   verifyFormat("bool x = f() ? a < 2 : a > 2;");
9460 
9461   verifyGoogleFormat("A<A<int>> a;");
9462   verifyGoogleFormat("A<A<A<int>>> a;");
9463   verifyGoogleFormat("A<A<A<A<int>>>> a;");
9464   verifyGoogleFormat("A<A<int> > a;");
9465   verifyGoogleFormat("A<A<A<int> > > a;");
9466   verifyGoogleFormat("A<A<A<A<int> > > > a;");
9467   verifyGoogleFormat("A<::A<int>> a;");
9468   verifyGoogleFormat("A<::A> a;");
9469   verifyGoogleFormat("A< ::A> a;");
9470   verifyGoogleFormat("A< ::A<int> > a;");
9471   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
9472   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
9473   EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle()));
9474   EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle()));
9475   EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };",
9476             format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle()));
9477 
9478   verifyFormat("A<A<int>> a;", getChromiumStyle(FormatStyle::LK_Cpp));
9479 
9480   // template closer followed by a token that starts with > or =
9481   verifyFormat("bool b = a<1> > 1;");
9482   verifyFormat("bool b = a<1> >= 1;");
9483   verifyFormat("int i = a<1> >> 1;");
9484   FormatStyle Style = getLLVMStyle();
9485   Style.SpaceBeforeAssignmentOperators = false;
9486   verifyFormat("bool b= a<1> == 1;", Style);
9487   verifyFormat("a<int> = 1;", Style);
9488   verifyFormat("a<int> >>= 1;", Style);
9489 
9490   verifyFormat("test < a | b >> c;");
9491   verifyFormat("test<test<a | b>> c;");
9492   verifyFormat("test >> a >> b;");
9493   verifyFormat("test << a >> b;");
9494 
9495   verifyFormat("f<int>();");
9496   verifyFormat("template <typename T> void f() {}");
9497   verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
9498   verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : "
9499                "sizeof(char)>::type>;");
9500   verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};");
9501   verifyFormat("f(a.operator()<A>());");
9502   verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9503                "      .template operator()<A>());",
9504                getLLVMStyleWithColumns(35));
9505 
9506   // Not template parameters.
9507   verifyFormat("return a < b && c > d;");
9508   verifyFormat("void f() {\n"
9509                "  while (a < b && c > d) {\n"
9510                "  }\n"
9511                "}");
9512   verifyFormat("template <typename... Types>\n"
9513                "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
9514 
9515   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9516                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
9517                getLLVMStyleWithColumns(60));
9518   verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
9519   verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
9520   verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <");
9521   verifyFormat("some_templated_type<decltype([](int i) { return i; })>");
9522 }
9523 
9524 TEST_F(FormatTest, UnderstandsShiftOperators) {
9525   verifyFormat("if (i < x >> 1)");
9526   verifyFormat("while (i < x >> 1)");
9527   verifyFormat("for (unsigned i = 0; i < i; ++i, v = v >> 1)");
9528   verifyFormat("for (unsigned i = 0; i < x >> 1; ++i, v = v >> 1)");
9529   verifyFormat(
9530       "for (std::vector<int>::iterator i = 0; i < x >> 1; ++i, v = v >> 1)");
9531   verifyFormat("Foo.call<Bar<Function>>()");
9532   verifyFormat("if (Foo.call<Bar<Function>>() == 0)");
9533   verifyFormat("for (std::vector<std::pair<int>>::iterator i = 0; i < x >> 1; "
9534                "++i, v = v >> 1)");
9535   verifyFormat("if (w<u<v<x>>, 1>::t)");
9536 }
9537 
9538 TEST_F(FormatTest, BitshiftOperatorWidth) {
9539   EXPECT_EQ("int a = 1 << 2; /* foo\n"
9540             "                   bar */",
9541             format("int    a=1<<2;  /* foo\n"
9542                    "                   bar */"));
9543 
9544   EXPECT_EQ("int b = 256 >> 1; /* foo\n"
9545             "                     bar */",
9546             format("int  b  =256>>1 ;  /* foo\n"
9547                    "                      bar */"));
9548 }
9549 
9550 TEST_F(FormatTest, UnderstandsBinaryOperators) {
9551   verifyFormat("COMPARE(a, ==, b);");
9552   verifyFormat("auto s = sizeof...(Ts) - 1;");
9553 }
9554 
9555 TEST_F(FormatTest, UnderstandsPointersToMembers) {
9556   verifyFormat("int A::*x;");
9557   verifyFormat("int (S::*func)(void *);");
9558   verifyFormat("void f() { int (S::*func)(void *); }");
9559   verifyFormat("typedef bool *(Class::*Member)() const;");
9560   verifyFormat("void f() {\n"
9561                "  (a->*f)();\n"
9562                "  a->*x;\n"
9563                "  (a.*f)();\n"
9564                "  ((*a).*f)();\n"
9565                "  a.*x;\n"
9566                "}");
9567   verifyFormat("void f() {\n"
9568                "  (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
9569                "      aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
9570                "}");
9571   verifyFormat(
9572       "(aaaaaaaaaa->*bbbbbbb)(\n"
9573       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
9574   FormatStyle Style = getLLVMStyle();
9575   Style.PointerAlignment = FormatStyle::PAS_Left;
9576   verifyFormat("typedef bool* (Class::*Member)() const;", Style);
9577 }
9578 
9579 TEST_F(FormatTest, UnderstandsUnaryOperators) {
9580   verifyFormat("int a = -2;");
9581   verifyFormat("f(-1, -2, -3);");
9582   verifyFormat("a[-1] = 5;");
9583   verifyFormat("int a = 5 + -2;");
9584   verifyFormat("if (i == -1) {\n}");
9585   verifyFormat("if (i != -1) {\n}");
9586   verifyFormat("if (i > -1) {\n}");
9587   verifyFormat("if (i < -1) {\n}");
9588   verifyFormat("++(a->f());");
9589   verifyFormat("--(a->f());");
9590   verifyFormat("(a->f())++;");
9591   verifyFormat("a[42]++;");
9592   verifyFormat("if (!(a->f())) {\n}");
9593   verifyFormat("if (!+i) {\n}");
9594   verifyFormat("~&a;");
9595 
9596   verifyFormat("a-- > b;");
9597   verifyFormat("b ? -a : c;");
9598   verifyFormat("n * sizeof char16;");
9599   verifyFormat("n * alignof char16;", getGoogleStyle());
9600   verifyFormat("sizeof(char);");
9601   verifyFormat("alignof(char);", getGoogleStyle());
9602 
9603   verifyFormat("return -1;");
9604   verifyFormat("throw -1;");
9605   verifyFormat("switch (a) {\n"
9606                "case -1:\n"
9607                "  break;\n"
9608                "}");
9609   verifyFormat("#define X -1");
9610   verifyFormat("#define X -kConstant");
9611 
9612   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
9613   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
9614 
9615   verifyFormat("int a = /* confusing comment */ -1;");
9616   // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
9617   verifyFormat("int a = i /* confusing comment */++;");
9618 
9619   verifyFormat("co_yield -1;");
9620   verifyFormat("co_return -1;");
9621 
9622   // Check that * is not treated as a binary operator when we set
9623   // PointerAlignment as PAS_Left after a keyword and not a declaration.
9624   FormatStyle PASLeftStyle = getLLVMStyle();
9625   PASLeftStyle.PointerAlignment = FormatStyle::PAS_Left;
9626   verifyFormat("co_return *a;", PASLeftStyle);
9627   verifyFormat("co_await *a;", PASLeftStyle);
9628   verifyFormat("co_yield *a", PASLeftStyle);
9629   verifyFormat("return *a;", PASLeftStyle);
9630 }
9631 
9632 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) {
9633   verifyFormat("if (!aaaaaaaaaa( // break\n"
9634                "        aaaaa)) {\n"
9635                "}");
9636   verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
9637                "    aaaaa));");
9638   verifyFormat("*aaa = aaaaaaa( // break\n"
9639                "    bbbbbb);");
9640 }
9641 
9642 TEST_F(FormatTest, UnderstandsOverloadedOperators) {
9643   verifyFormat("bool operator<();");
9644   verifyFormat("bool operator>();");
9645   verifyFormat("bool operator=();");
9646   verifyFormat("bool operator==();");
9647   verifyFormat("bool operator!=();");
9648   verifyFormat("int operator+();");
9649   verifyFormat("int operator++();");
9650   verifyFormat("int operator++(int) volatile noexcept;");
9651   verifyFormat("bool operator,();");
9652   verifyFormat("bool operator();");
9653   verifyFormat("bool operator()();");
9654   verifyFormat("bool operator[]();");
9655   verifyFormat("operator bool();");
9656   verifyFormat("operator int();");
9657   verifyFormat("operator void *();");
9658   verifyFormat("operator SomeType<int>();");
9659   verifyFormat("operator SomeType<int, int>();");
9660   verifyFormat("operator SomeType<SomeType<int>>();");
9661   verifyFormat("operator< <>();");
9662   verifyFormat("operator<< <>();");
9663   verifyFormat("< <>");
9664 
9665   verifyFormat("void *operator new(std::size_t size);");
9666   verifyFormat("void *operator new[](std::size_t size);");
9667   verifyFormat("void operator delete(void *ptr);");
9668   verifyFormat("void operator delete[](void *ptr);");
9669   verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
9670                "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
9671   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n"
9672                "    aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;");
9673 
9674   verifyFormat(
9675       "ostream &operator<<(ostream &OutputStream,\n"
9676       "                    SomeReallyLongType WithSomeReallyLongValue);");
9677   verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
9678                "               const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
9679                "  return left.group < right.group;\n"
9680                "}");
9681   verifyFormat("SomeType &operator=(const SomeType &S);");
9682   verifyFormat("f.template operator()<int>();");
9683 
9684   verifyGoogleFormat("operator void*();");
9685   verifyGoogleFormat("operator SomeType<SomeType<int>>();");
9686   verifyGoogleFormat("operator ::A();");
9687 
9688   verifyFormat("using A::operator+;");
9689   verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n"
9690                "int i;");
9691 
9692   // Calling an operator as a member function.
9693   verifyFormat("void f() { a.operator*(); }");
9694   verifyFormat("void f() { a.operator*(b & b); }");
9695   verifyFormat("void f() { a->operator&(a * b); }");
9696   verifyFormat("void f() { NS::a.operator+(*b * *b); }");
9697   // TODO: Calling an operator as a non-member function is hard to distinguish.
9698   // https://llvm.org/PR50629
9699   // verifyFormat("void f() { operator*(a & a); }");
9700   // verifyFormat("void f() { operator&(a, b * b); }");
9701 
9702   verifyFormat("::operator delete(foo);");
9703   verifyFormat("::operator new(n * sizeof(foo));");
9704   verifyFormat("foo() { ::operator delete(foo); }");
9705   verifyFormat("foo() { ::operator new(n * sizeof(foo)); }");
9706 }
9707 
9708 TEST_F(FormatTest, UnderstandsFunctionRefQualification) {
9709   verifyFormat("void A::b() && {}");
9710   verifyFormat("void A::b() &&noexcept {}");
9711   verifyFormat("Deleted &operator=(const Deleted &) & = default;");
9712   verifyFormat("Deleted &operator=(const Deleted &) && = delete;");
9713   verifyFormat("Deleted &operator=(const Deleted &) &noexcept = default;");
9714   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;");
9715   verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;");
9716   verifyFormat("Deleted &operator=(const Deleted &) &;");
9717   verifyFormat("Deleted &operator=(const Deleted &) &&;");
9718   verifyFormat("SomeType MemberFunction(const Deleted &) &;");
9719   verifyFormat("SomeType MemberFunction(const Deleted &) &&;");
9720   verifyFormat("SomeType MemberFunction(const Deleted &) && {}");
9721   verifyFormat("SomeType MemberFunction(const Deleted &) && final {}");
9722   verifyFormat("SomeType MemberFunction(const Deleted &) && override {}");
9723   verifyFormat("SomeType MemberFunction(const Deleted &) &&noexcept {}");
9724   verifyFormat("void Fn(T const &) const &;");
9725   verifyFormat("void Fn(T const volatile &&) const volatile &&;");
9726   verifyFormat("void Fn(T const volatile &&) const volatile &&noexcept;");
9727   verifyFormat("template <typename T>\n"
9728                "void F(T) && = delete;",
9729                getGoogleStyle());
9730 
9731   FormatStyle AlignLeft = getLLVMStyle();
9732   AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
9733   verifyFormat("void A::b() && {}", AlignLeft);
9734   verifyFormat("void A::b() && noexcept {}", AlignLeft);
9735   verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft);
9736   verifyFormat("Deleted& operator=(const Deleted&) & noexcept = default;",
9737                AlignLeft);
9738   verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;",
9739                AlignLeft);
9740   verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft);
9741   verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft);
9742   verifyFormat("auto Function(T t) & -> void {}", AlignLeft);
9743   verifyFormat("auto Function(T... t) & -> void {}", AlignLeft);
9744   verifyFormat("auto Function(T) & -> void {}", AlignLeft);
9745   verifyFormat("auto Function(T) & -> void;", AlignLeft);
9746   verifyFormat("void Fn(T const&) const&;", AlignLeft);
9747   verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft);
9748   verifyFormat("void Fn(T const volatile&&) const volatile&& noexcept;",
9749                AlignLeft);
9750 
9751   FormatStyle AlignMiddle = getLLVMStyle();
9752   AlignMiddle.PointerAlignment = FormatStyle::PAS_Middle;
9753   verifyFormat("void A::b() && {}", AlignMiddle);
9754   verifyFormat("void A::b() && noexcept {}", AlignMiddle);
9755   verifyFormat("Deleted & operator=(const Deleted &) & = default;",
9756                AlignMiddle);
9757   verifyFormat("Deleted & operator=(const Deleted &) & noexcept = default;",
9758                AlignMiddle);
9759   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;",
9760                AlignMiddle);
9761   verifyFormat("Deleted & operator=(const Deleted &) &;", AlignMiddle);
9762   verifyFormat("SomeType MemberFunction(const Deleted &) &;", AlignMiddle);
9763   verifyFormat("auto Function(T t) & -> void {}", AlignMiddle);
9764   verifyFormat("auto Function(T... t) & -> void {}", AlignMiddle);
9765   verifyFormat("auto Function(T) & -> void {}", AlignMiddle);
9766   verifyFormat("auto Function(T) & -> void;", AlignMiddle);
9767   verifyFormat("void Fn(T const &) const &;", AlignMiddle);
9768   verifyFormat("void Fn(T const volatile &&) const volatile &&;", AlignMiddle);
9769   verifyFormat("void Fn(T const volatile &&) const volatile && noexcept;",
9770                AlignMiddle);
9771 
9772   FormatStyle Spaces = getLLVMStyle();
9773   Spaces.SpacesInCStyleCastParentheses = true;
9774   verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces);
9775   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces);
9776   verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces);
9777   verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces);
9778 
9779   Spaces.SpacesInCStyleCastParentheses = false;
9780   Spaces.SpacesInParentheses = true;
9781   verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces);
9782   verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;",
9783                Spaces);
9784   verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces);
9785   verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces);
9786 
9787   FormatStyle BreakTemplate = getLLVMStyle();
9788   BreakTemplate.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
9789 
9790   verifyFormat("struct f {\n"
9791                "  template <class T>\n"
9792                "  int &foo(const std::string &str) &noexcept {}\n"
9793                "};",
9794                BreakTemplate);
9795 
9796   verifyFormat("struct f {\n"
9797                "  template <class T>\n"
9798                "  int &foo(const std::string &str) &&noexcept {}\n"
9799                "};",
9800                BreakTemplate);
9801 
9802   verifyFormat("struct f {\n"
9803                "  template <class T>\n"
9804                "  int &foo(const std::string &str) const &noexcept {}\n"
9805                "};",
9806                BreakTemplate);
9807 
9808   verifyFormat("struct f {\n"
9809                "  template <class T>\n"
9810                "  int &foo(const std::string &str) const &noexcept {}\n"
9811                "};",
9812                BreakTemplate);
9813 
9814   verifyFormat("struct f {\n"
9815                "  template <class T>\n"
9816                "  auto foo(const std::string &str) &&noexcept -> int & {}\n"
9817                "};",
9818                BreakTemplate);
9819 
9820   FormatStyle AlignLeftBreakTemplate = getLLVMStyle();
9821   AlignLeftBreakTemplate.AlwaysBreakTemplateDeclarations =
9822       FormatStyle::BTDS_Yes;
9823   AlignLeftBreakTemplate.PointerAlignment = FormatStyle::PAS_Left;
9824 
9825   verifyFormat("struct f {\n"
9826                "  template <class T>\n"
9827                "  int& foo(const std::string& str) & noexcept {}\n"
9828                "};",
9829                AlignLeftBreakTemplate);
9830 
9831   verifyFormat("struct f {\n"
9832                "  template <class T>\n"
9833                "  int& foo(const std::string& str) && noexcept {}\n"
9834                "};",
9835                AlignLeftBreakTemplate);
9836 
9837   verifyFormat("struct f {\n"
9838                "  template <class T>\n"
9839                "  int& foo(const std::string& str) const& noexcept {}\n"
9840                "};",
9841                AlignLeftBreakTemplate);
9842 
9843   verifyFormat("struct f {\n"
9844                "  template <class T>\n"
9845                "  int& foo(const std::string& str) const&& noexcept {}\n"
9846                "};",
9847                AlignLeftBreakTemplate);
9848 
9849   verifyFormat("struct f {\n"
9850                "  template <class T>\n"
9851                "  auto foo(const std::string& str) && noexcept -> int& {}\n"
9852                "};",
9853                AlignLeftBreakTemplate);
9854 
9855   // The `&` in `Type&` should not be confused with a trailing `&` of
9856   // DEPRECATED(reason) member function.
9857   verifyFormat("struct f {\n"
9858                "  template <class T>\n"
9859                "  DEPRECATED(reason)\n"
9860                "  Type &foo(arguments) {}\n"
9861                "};",
9862                BreakTemplate);
9863 
9864   verifyFormat("struct f {\n"
9865                "  template <class T>\n"
9866                "  DEPRECATED(reason)\n"
9867                "  Type& foo(arguments) {}\n"
9868                "};",
9869                AlignLeftBreakTemplate);
9870 
9871   verifyFormat("void (*foopt)(int) = &func;");
9872 
9873   FormatStyle DerivePointerAlignment = getLLVMStyle();
9874   DerivePointerAlignment.DerivePointerAlignment = true;
9875   // There's always a space between the function and its trailing qualifiers.
9876   // This isn't evidence for PAS_Right (or for PAS_Left).
9877   std::string Prefix = "void a() &;\n"
9878                        "void b() &;\n";
9879   verifyFormat(Prefix + "int* x;", DerivePointerAlignment);
9880   verifyFormat(Prefix + "int *x;", DerivePointerAlignment);
9881   // Same if the function is an overloaded operator, and with &&.
9882   Prefix = "void operator()() &&;\n"
9883            "void operator()() &&;\n";
9884   verifyFormat(Prefix + "int* x;", DerivePointerAlignment);
9885   verifyFormat(Prefix + "int *x;", DerivePointerAlignment);
9886   // However a space between cv-qualifiers and ref-qualifiers *is* evidence.
9887   Prefix = "void a() const &;\n"
9888            "void b() const &;\n";
9889   EXPECT_EQ(Prefix + "int *x;",
9890             format(Prefix + "int* x;", DerivePointerAlignment));
9891 }
9892 
9893 TEST_F(FormatTest, UnderstandsNewAndDelete) {
9894   verifyFormat("void f() {\n"
9895                "  A *a = new A;\n"
9896                "  A *a = new (placement) A;\n"
9897                "  delete a;\n"
9898                "  delete (A *)a;\n"
9899                "}");
9900   verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
9901                "    typename aaaaaaaaaaaaaaaaaaaaaaaa();");
9902   verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
9903                "    new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
9904                "        typename aaaaaaaaaaaaaaaaaaaaaaaa();");
9905   verifyFormat("delete[] h->p;");
9906   verifyFormat("delete[] (void *)p;");
9907 
9908   verifyFormat("void operator delete(void *foo) ATTRIB;");
9909   verifyFormat("void operator new(void *foo) ATTRIB;");
9910   verifyFormat("void operator delete[](void *foo) ATTRIB;");
9911   verifyFormat("void operator delete(void *ptr) noexcept;");
9912 }
9913 
9914 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
9915   verifyFormat("int *f(int *a) {}");
9916   verifyFormat("int main(int argc, char **argv) {}");
9917   verifyFormat("Test::Test(int b) : a(b * b) {}");
9918   verifyIndependentOfContext("f(a, *a);");
9919   verifyFormat("void g() { f(*a); }");
9920   verifyIndependentOfContext("int a = b * 10;");
9921   verifyIndependentOfContext("int a = 10 * b;");
9922   verifyIndependentOfContext("int a = b * c;");
9923   verifyIndependentOfContext("int a += b * c;");
9924   verifyIndependentOfContext("int a -= b * c;");
9925   verifyIndependentOfContext("int a *= b * c;");
9926   verifyIndependentOfContext("int a /= b * c;");
9927   verifyIndependentOfContext("int a = *b;");
9928   verifyIndependentOfContext("int a = *b * c;");
9929   verifyIndependentOfContext("int a = b * *c;");
9930   verifyIndependentOfContext("int a = b * (10);");
9931   verifyIndependentOfContext("S << b * (10);");
9932   verifyIndependentOfContext("return 10 * b;");
9933   verifyIndependentOfContext("return *b * *c;");
9934   verifyIndependentOfContext("return a & ~b;");
9935   verifyIndependentOfContext("f(b ? *c : *d);");
9936   verifyIndependentOfContext("int a = b ? *c : *d;");
9937   verifyIndependentOfContext("*b = a;");
9938   verifyIndependentOfContext("a * ~b;");
9939   verifyIndependentOfContext("a * !b;");
9940   verifyIndependentOfContext("a * +b;");
9941   verifyIndependentOfContext("a * -b;");
9942   verifyIndependentOfContext("a * ++b;");
9943   verifyIndependentOfContext("a * --b;");
9944   verifyIndependentOfContext("a[4] * b;");
9945   verifyIndependentOfContext("a[a * a] = 1;");
9946   verifyIndependentOfContext("f() * b;");
9947   verifyIndependentOfContext("a * [self dostuff];");
9948   verifyIndependentOfContext("int x = a * (a + b);");
9949   verifyIndependentOfContext("(a *)(a + b);");
9950   verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
9951   verifyIndependentOfContext("int *pa = (int *)&a;");
9952   verifyIndependentOfContext("return sizeof(int **);");
9953   verifyIndependentOfContext("return sizeof(int ******);");
9954   verifyIndependentOfContext("return (int **&)a;");
9955   verifyIndependentOfContext("f((*PointerToArray)[10]);");
9956   verifyFormat("void f(Type (*parameter)[10]) {}");
9957   verifyFormat("void f(Type (&parameter)[10]) {}");
9958   verifyGoogleFormat("return sizeof(int**);");
9959   verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
9960   verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
9961   verifyFormat("auto a = [](int **&, int ***) {};");
9962   verifyFormat("auto PointerBinding = [](const char *S) {};");
9963   verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
9964   verifyFormat("[](const decltype(*a) &value) {}");
9965   verifyFormat("[](const typeof(*a) &value) {}");
9966   verifyFormat("[](const _Atomic(a *) &value) {}");
9967   verifyFormat("[](const __underlying_type(a) &value) {}");
9968   verifyFormat("decltype(a * b) F();");
9969   verifyFormat("typeof(a * b) F();");
9970   verifyFormat("#define MACRO() [](A *a) { return 1; }");
9971   verifyFormat("Constructor() : member([](A *a, B *b) {}) {}");
9972   verifyIndependentOfContext("typedef void (*f)(int *a);");
9973   verifyIndependentOfContext("int i{a * b};");
9974   verifyIndependentOfContext("aaa && aaa->f();");
9975   verifyIndependentOfContext("int x = ~*p;");
9976   verifyFormat("Constructor() : a(a), area(width * height) {}");
9977   verifyFormat("Constructor() : a(a), area(a, width * height) {}");
9978   verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}");
9979   verifyFormat("void f() { f(a, c * d); }");
9980   verifyFormat("void f() { f(new a(), c * d); }");
9981   verifyFormat("void f(const MyOverride &override);");
9982   verifyFormat("void f(const MyFinal &final);");
9983   verifyIndependentOfContext("bool a = f() && override.f();");
9984   verifyIndependentOfContext("bool a = f() && final.f();");
9985 
9986   verifyIndependentOfContext("InvalidRegions[*R] = 0;");
9987 
9988   verifyIndependentOfContext("A<int *> a;");
9989   verifyIndependentOfContext("A<int **> a;");
9990   verifyIndependentOfContext("A<int *, int *> a;");
9991   verifyIndependentOfContext("A<int *[]> a;");
9992   verifyIndependentOfContext(
9993       "const char *const p = reinterpret_cast<const char *const>(q);");
9994   verifyIndependentOfContext("A<int **, int **> a;");
9995   verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
9996   verifyFormat("for (char **a = b; *a; ++a) {\n}");
9997   verifyFormat("for (; a && b;) {\n}");
9998   verifyFormat("bool foo = true && [] { return false; }();");
9999 
10000   verifyFormat(
10001       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10002       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
10003 
10004   verifyGoogleFormat("int const* a = &b;");
10005   verifyGoogleFormat("**outparam = 1;");
10006   verifyGoogleFormat("*outparam = a * b;");
10007   verifyGoogleFormat("int main(int argc, char** argv) {}");
10008   verifyGoogleFormat("A<int*> a;");
10009   verifyGoogleFormat("A<int**> a;");
10010   verifyGoogleFormat("A<int*, int*> a;");
10011   verifyGoogleFormat("A<int**, int**> a;");
10012   verifyGoogleFormat("f(b ? *c : *d);");
10013   verifyGoogleFormat("int a = b ? *c : *d;");
10014   verifyGoogleFormat("Type* t = **x;");
10015   verifyGoogleFormat("Type* t = *++*x;");
10016   verifyGoogleFormat("*++*x;");
10017   verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
10018   verifyGoogleFormat("Type* t = x++ * y;");
10019   verifyGoogleFormat(
10020       "const char* const p = reinterpret_cast<const char* const>(q);");
10021   verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
10022   verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
10023   verifyGoogleFormat("template <typename T>\n"
10024                      "void f(int i = 0, SomeType** temps = NULL);");
10025 
10026   FormatStyle Left = getLLVMStyle();
10027   Left.PointerAlignment = FormatStyle::PAS_Left;
10028   verifyFormat("x = *a(x) = *a(y);", Left);
10029   verifyFormat("for (;; *a = b) {\n}", Left);
10030   verifyFormat("return *this += 1;", Left);
10031   verifyFormat("throw *x;", Left);
10032   verifyFormat("delete *x;", Left);
10033   verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left);
10034   verifyFormat("[](const decltype(*a)* ptr) {}", Left);
10035   verifyFormat("[](const typeof(*a)* ptr) {}", Left);
10036   verifyFormat("[](const _Atomic(a*)* ptr) {}", Left);
10037   verifyFormat("[](const __underlying_type(a)* ptr) {}", Left);
10038   verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left);
10039   verifyFormat("auto x(A&&, B&&, C&&) -> D;", Left);
10040   verifyFormat("auto x = [](A&&, B&&, C&&) -> D {};", Left);
10041   verifyFormat("template <class T> X(T&&, T&&, T&&) -> X<T>;", Left);
10042 
10043   verifyIndependentOfContext("a = *(x + y);");
10044   verifyIndependentOfContext("a = &(x + y);");
10045   verifyIndependentOfContext("*(x + y).call();");
10046   verifyIndependentOfContext("&(x + y)->call();");
10047   verifyFormat("void f() { &(*I).first; }");
10048 
10049   verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
10050   verifyFormat("f(* /* confusing comment */ foo);");
10051   verifyFormat("void (* /*deleter*/)(const Slice &key, void *value)");
10052   verifyFormat("void foo(int * // this is the first paramters\n"
10053                "         ,\n"
10054                "         int second);");
10055   verifyFormat("double term = a * // first\n"
10056                "              b;");
10057   verifyFormat(
10058       "int *MyValues = {\n"
10059       "    *A, // Operator detection might be confused by the '{'\n"
10060       "    *BB // Operator detection might be confused by previous comment\n"
10061       "};");
10062 
10063   verifyIndependentOfContext("if (int *a = &b)");
10064   verifyIndependentOfContext("if (int &a = *b)");
10065   verifyIndependentOfContext("if (a & b[i])");
10066   verifyIndependentOfContext("if constexpr (a & b[i])");
10067   verifyIndependentOfContext("if CONSTEXPR (a & b[i])");
10068   verifyIndependentOfContext("if (a * (b * c))");
10069   verifyIndependentOfContext("if constexpr (a * (b * c))");
10070   verifyIndependentOfContext("if CONSTEXPR (a * (b * c))");
10071   verifyIndependentOfContext("if (a::b::c::d & b[i])");
10072   verifyIndependentOfContext("if (*b[i])");
10073   verifyIndependentOfContext("if (int *a = (&b))");
10074   verifyIndependentOfContext("while (int *a = &b)");
10075   verifyIndependentOfContext("while (a * (b * c))");
10076   verifyIndependentOfContext("size = sizeof *a;");
10077   verifyIndependentOfContext("if (a && (b = c))");
10078   verifyFormat("void f() {\n"
10079                "  for (const int &v : Values) {\n"
10080                "  }\n"
10081                "}");
10082   verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
10083   verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
10084   verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
10085 
10086   verifyFormat("#define A (!a * b)");
10087   verifyFormat("#define MACRO     \\\n"
10088                "  int *i = a * b; \\\n"
10089                "  void f(a *b);",
10090                getLLVMStyleWithColumns(19));
10091 
10092   verifyIndependentOfContext("A = new SomeType *[Length];");
10093   verifyIndependentOfContext("A = new SomeType *[Length]();");
10094   verifyIndependentOfContext("T **t = new T *;");
10095   verifyIndependentOfContext("T **t = new T *();");
10096   verifyGoogleFormat("A = new SomeType*[Length]();");
10097   verifyGoogleFormat("A = new SomeType*[Length];");
10098   verifyGoogleFormat("T** t = new T*;");
10099   verifyGoogleFormat("T** t = new T*();");
10100 
10101   verifyFormat("STATIC_ASSERT((a & b) == 0);");
10102   verifyFormat("STATIC_ASSERT(0 == (a & b));");
10103   verifyFormat("template <bool a, bool b> "
10104                "typename t::if<x && y>::type f() {}");
10105   verifyFormat("template <int *y> f() {}");
10106   verifyFormat("vector<int *> v;");
10107   verifyFormat("vector<int *const> v;");
10108   verifyFormat("vector<int *const **const *> v;");
10109   verifyFormat("vector<int *volatile> v;");
10110   verifyFormat("vector<a *_Nonnull> v;");
10111   verifyFormat("vector<a *_Nullable> v;");
10112   verifyFormat("vector<a *_Null_unspecified> v;");
10113   verifyFormat("vector<a *__ptr32> v;");
10114   verifyFormat("vector<a *__ptr64> v;");
10115   verifyFormat("vector<a *__capability> v;");
10116   FormatStyle TypeMacros = getLLVMStyle();
10117   TypeMacros.TypenameMacros = {"LIST"};
10118   verifyFormat("vector<LIST(uint64_t)> v;", TypeMacros);
10119   verifyFormat("vector<LIST(uint64_t) *> v;", TypeMacros);
10120   verifyFormat("vector<LIST(uint64_t) **> v;", TypeMacros);
10121   verifyFormat("vector<LIST(uint64_t) *attr> v;", TypeMacros);
10122   verifyFormat("vector<A(uint64_t) * attr> v;", TypeMacros); // multiplication
10123 
10124   FormatStyle CustomQualifier = getLLVMStyle();
10125   // Add identifiers that should not be parsed as a qualifier by default.
10126   CustomQualifier.AttributeMacros.push_back("__my_qualifier");
10127   CustomQualifier.AttributeMacros.push_back("_My_qualifier");
10128   CustomQualifier.AttributeMacros.push_back("my_other_qualifier");
10129   verifyFormat("vector<a * __my_qualifier> parse_as_multiply;");
10130   verifyFormat("vector<a *__my_qualifier> v;", CustomQualifier);
10131   verifyFormat("vector<a * _My_qualifier> parse_as_multiply;");
10132   verifyFormat("vector<a *_My_qualifier> v;", CustomQualifier);
10133   verifyFormat("vector<a * my_other_qualifier> parse_as_multiply;");
10134   verifyFormat("vector<a *my_other_qualifier> v;", CustomQualifier);
10135   verifyFormat("vector<a * _NotAQualifier> v;");
10136   verifyFormat("vector<a * __not_a_qualifier> v;");
10137   verifyFormat("vector<a * b> v;");
10138   verifyFormat("foo<b && false>();");
10139   verifyFormat("foo<b & 1>();");
10140   verifyFormat("decltype(*::std::declval<const T &>()) void F();");
10141   verifyFormat("typeof(*::std::declval<const T &>()) void F();");
10142   verifyFormat("_Atomic(*::std::declval<const T &>()) void F();");
10143   verifyFormat("__underlying_type(*::std::declval<const T &>()) void F();");
10144   verifyFormat(
10145       "template <class T, class = typename std::enable_if<\n"
10146       "                       std::is_integral<T>::value &&\n"
10147       "                       (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
10148       "void F();",
10149       getLLVMStyleWithColumns(70));
10150   verifyFormat("template <class T,\n"
10151                "          class = typename std::enable_if<\n"
10152                "              std::is_integral<T>::value &&\n"
10153                "              (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n"
10154                "          class U>\n"
10155                "void F();",
10156                getLLVMStyleWithColumns(70));
10157   verifyFormat(
10158       "template <class T,\n"
10159       "          class = typename ::std::enable_if<\n"
10160       "              ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
10161       "void F();",
10162       getGoogleStyleWithColumns(68));
10163 
10164   verifyIndependentOfContext("MACRO(int *i);");
10165   verifyIndependentOfContext("MACRO(auto *a);");
10166   verifyIndependentOfContext("MACRO(const A *a);");
10167   verifyIndependentOfContext("MACRO(_Atomic(A) *a);");
10168   verifyIndependentOfContext("MACRO(decltype(A) *a);");
10169   verifyIndependentOfContext("MACRO(typeof(A) *a);");
10170   verifyIndependentOfContext("MACRO(__underlying_type(A) *a);");
10171   verifyIndependentOfContext("MACRO(A *const a);");
10172   verifyIndependentOfContext("MACRO(A *restrict a);");
10173   verifyIndependentOfContext("MACRO(A *__restrict__ a);");
10174   verifyIndependentOfContext("MACRO(A *__restrict a);");
10175   verifyIndependentOfContext("MACRO(A *volatile a);");
10176   verifyIndependentOfContext("MACRO(A *__volatile a);");
10177   verifyIndependentOfContext("MACRO(A *__volatile__ a);");
10178   verifyIndependentOfContext("MACRO(A *_Nonnull a);");
10179   verifyIndependentOfContext("MACRO(A *_Nullable a);");
10180   verifyIndependentOfContext("MACRO(A *_Null_unspecified a);");
10181   verifyIndependentOfContext("MACRO(A *__attribute__((foo)) a);");
10182   verifyIndependentOfContext("MACRO(A *__attribute((foo)) a);");
10183   verifyIndependentOfContext("MACRO(A *[[clang::attr]] a);");
10184   verifyIndependentOfContext("MACRO(A *[[clang::attr(\"foo\")]] a);");
10185   verifyIndependentOfContext("MACRO(A *__ptr32 a);");
10186   verifyIndependentOfContext("MACRO(A *__ptr64 a);");
10187   verifyIndependentOfContext("MACRO(A *__capability);");
10188   verifyIndependentOfContext("MACRO(A &__capability);");
10189   verifyFormat("MACRO(A *__my_qualifier);");               // type declaration
10190   verifyFormat("void f() { MACRO(A * __my_qualifier); }"); // multiplication
10191   // If we add __my_qualifier to AttributeMacros it should always be parsed as
10192   // a type declaration:
10193   verifyFormat("MACRO(A *__my_qualifier);", CustomQualifier);
10194   verifyFormat("void f() { MACRO(A *__my_qualifier); }", CustomQualifier);
10195   // Also check that TypenameMacros prevents parsing it as multiplication:
10196   verifyIndependentOfContext("MACRO(LIST(uint64_t) * a);"); // multiplication
10197   verifyIndependentOfContext("MACRO(LIST(uint64_t) *a);", TypeMacros); // type
10198 
10199   verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
10200   verifyFormat("void f() { f(float{1}, a * a); }");
10201   verifyFormat("void f() { f(float(1), a * a); }");
10202 
10203   verifyFormat("f((void (*)(int))g);");
10204   verifyFormat("f((void (&)(int))g);");
10205   verifyFormat("f((void (^)(int))g);");
10206 
10207   // FIXME: Is there a way to make this work?
10208   // verifyIndependentOfContext("MACRO(A *a);");
10209   verifyFormat("MACRO(A &B);");
10210   verifyFormat("MACRO(A *B);");
10211   verifyFormat("void f() { MACRO(A * B); }");
10212   verifyFormat("void f() { MACRO(A & B); }");
10213 
10214   // This lambda was mis-formatted after D88956 (treating it as a binop):
10215   verifyFormat("auto x = [](const decltype(x) &ptr) {};");
10216   verifyFormat("auto x = [](const decltype(x) *ptr) {};");
10217   verifyFormat("#define lambda [](const decltype(x) &ptr) {}");
10218   verifyFormat("#define lambda [](const decltype(x) *ptr) {}");
10219 
10220   verifyFormat("DatumHandle const *operator->() const { return input_; }");
10221   verifyFormat("return options != nullptr && operator==(*options);");
10222 
10223   EXPECT_EQ("#define OP(x)                                    \\\n"
10224             "  ostream &operator<<(ostream &s, const A &a) {  \\\n"
10225             "    return s << a.DebugString();                 \\\n"
10226             "  }",
10227             format("#define OP(x) \\\n"
10228                    "  ostream &operator<<(ostream &s, const A &a) { \\\n"
10229                    "    return s << a.DebugString(); \\\n"
10230                    "  }",
10231                    getLLVMStyleWithColumns(50)));
10232 
10233   // FIXME: We cannot handle this case yet; we might be able to figure out that
10234   // foo<x> d > v; doesn't make sense.
10235   verifyFormat("foo<a<b && c> d> v;");
10236 
10237   FormatStyle PointerMiddle = getLLVMStyle();
10238   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
10239   verifyFormat("delete *x;", PointerMiddle);
10240   verifyFormat("int * x;", PointerMiddle);
10241   verifyFormat("int *[] x;", PointerMiddle);
10242   verifyFormat("template <int * y> f() {}", PointerMiddle);
10243   verifyFormat("int * f(int * a) {}", PointerMiddle);
10244   verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle);
10245   verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle);
10246   verifyFormat("A<int *> a;", PointerMiddle);
10247   verifyFormat("A<int **> a;", PointerMiddle);
10248   verifyFormat("A<int *, int *> a;", PointerMiddle);
10249   verifyFormat("A<int *[]> a;", PointerMiddle);
10250   verifyFormat("A = new SomeType *[Length]();", PointerMiddle);
10251   verifyFormat("A = new SomeType *[Length];", PointerMiddle);
10252   verifyFormat("T ** t = new T *;", PointerMiddle);
10253 
10254   // Member function reference qualifiers aren't binary operators.
10255   verifyFormat("string // break\n"
10256                "operator()() & {}");
10257   verifyFormat("string // break\n"
10258                "operator()() && {}");
10259   verifyGoogleFormat("template <typename T>\n"
10260                      "auto x() & -> int {}");
10261 
10262   // Should be binary operators when used as an argument expression (overloaded
10263   // operator invoked as a member function).
10264   verifyFormat("void f() { a.operator()(a * a); }");
10265   verifyFormat("void f() { a->operator()(a & a); }");
10266   verifyFormat("void f() { a.operator()(*a & *a); }");
10267   verifyFormat("void f() { a->operator()(*a * *a); }");
10268 
10269   verifyFormat("int operator()(T (&&)[N]) { return 1; }");
10270   verifyFormat("int operator()(T (&)[N]) { return 0; }");
10271 }
10272 
10273 TEST_F(FormatTest, UnderstandsAttributes) {
10274   verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
10275   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
10276                "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
10277   verifyFormat("__attribute__((nodebug)) ::qualified_type f();");
10278   FormatStyle AfterType = getLLVMStyle();
10279   AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
10280   verifyFormat("__attribute__((nodebug)) void\n"
10281                "foo() {}\n",
10282                AfterType);
10283   verifyFormat("__unused void\n"
10284                "foo() {}",
10285                AfterType);
10286 
10287   FormatStyle CustomAttrs = getLLVMStyle();
10288   CustomAttrs.AttributeMacros.push_back("__unused");
10289   CustomAttrs.AttributeMacros.push_back("__attr1");
10290   CustomAttrs.AttributeMacros.push_back("__attr2");
10291   CustomAttrs.AttributeMacros.push_back("no_underscore_attr");
10292   verifyFormat("vector<SomeType *__attribute((foo))> v;");
10293   verifyFormat("vector<SomeType *__attribute__((foo))> v;");
10294   verifyFormat("vector<SomeType * __not_attribute__((foo))> v;");
10295   // Check that it is parsed as a multiplication without AttributeMacros and
10296   // as a pointer qualifier when we add __attr1/__attr2 to AttributeMacros.
10297   verifyFormat("vector<SomeType * __attr1> v;");
10298   verifyFormat("vector<SomeType __attr1 *> v;");
10299   verifyFormat("vector<SomeType __attr1 *const> v;");
10300   verifyFormat("vector<SomeType __attr1 * __attr2> v;");
10301   verifyFormat("vector<SomeType *__attr1> v;", CustomAttrs);
10302   verifyFormat("vector<SomeType *__attr2> v;", CustomAttrs);
10303   verifyFormat("vector<SomeType *no_underscore_attr> v;", CustomAttrs);
10304   verifyFormat("vector<SomeType __attr1 *> v;", CustomAttrs);
10305   verifyFormat("vector<SomeType __attr1 *const> v;", CustomAttrs);
10306   verifyFormat("vector<SomeType __attr1 *__attr2> v;", CustomAttrs);
10307   verifyFormat("vector<SomeType __attr1 *no_underscore_attr> v;", CustomAttrs);
10308 
10309   // Check that these are not parsed as function declarations:
10310   CustomAttrs.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
10311   CustomAttrs.BreakBeforeBraces = FormatStyle::BS_Allman;
10312   verifyFormat("SomeType s(InitValue);", CustomAttrs);
10313   verifyFormat("SomeType s{InitValue};", CustomAttrs);
10314   verifyFormat("SomeType *__unused s(InitValue);", CustomAttrs);
10315   verifyFormat("SomeType *__unused s{InitValue};", CustomAttrs);
10316   verifyFormat("SomeType s __unused(InitValue);", CustomAttrs);
10317   verifyFormat("SomeType s __unused{InitValue};", CustomAttrs);
10318   verifyFormat("SomeType *__capability s(InitValue);", CustomAttrs);
10319   verifyFormat("SomeType *__capability s{InitValue};", CustomAttrs);
10320 }
10321 
10322 TEST_F(FormatTest, UnderstandsPointerQualifiersInCast) {
10323   // Check that qualifiers on pointers don't break parsing of casts.
10324   verifyFormat("x = (foo *const)*v;");
10325   verifyFormat("x = (foo *volatile)*v;");
10326   verifyFormat("x = (foo *restrict)*v;");
10327   verifyFormat("x = (foo *__attribute__((foo)))*v;");
10328   verifyFormat("x = (foo *_Nonnull)*v;");
10329   verifyFormat("x = (foo *_Nullable)*v;");
10330   verifyFormat("x = (foo *_Null_unspecified)*v;");
10331   verifyFormat("x = (foo *_Nonnull)*v;");
10332   verifyFormat("x = (foo *[[clang::attr]])*v;");
10333   verifyFormat("x = (foo *[[clang::attr(\"foo\")]])*v;");
10334   verifyFormat("x = (foo *__ptr32)*v;");
10335   verifyFormat("x = (foo *__ptr64)*v;");
10336   verifyFormat("x = (foo *__capability)*v;");
10337 
10338   // Check that we handle multiple trailing qualifiers and skip them all to
10339   // determine that the expression is a cast to a pointer type.
10340   FormatStyle LongPointerRight = getLLVMStyleWithColumns(999);
10341   FormatStyle LongPointerLeft = getLLVMStyleWithColumns(999);
10342   LongPointerLeft.PointerAlignment = FormatStyle::PAS_Left;
10343   StringRef AllQualifiers =
10344       "const volatile restrict __attribute__((foo)) _Nonnull _Null_unspecified "
10345       "_Nonnull [[clang::attr]] __ptr32 __ptr64 __capability";
10346   verifyFormat(("x = (foo *" + AllQualifiers + ")*v;").str(), LongPointerRight);
10347   verifyFormat(("x = (foo* " + AllQualifiers + ")*v;").str(), LongPointerLeft);
10348 
10349   // Also check that address-of is not parsed as a binary bitwise-and:
10350   verifyFormat("x = (foo *const)&v;");
10351   verifyFormat(("x = (foo *" + AllQualifiers + ")&v;").str(), LongPointerRight);
10352   verifyFormat(("x = (foo* " + AllQualifiers + ")&v;").str(), LongPointerLeft);
10353 
10354   // Check custom qualifiers:
10355   FormatStyle CustomQualifier = getLLVMStyleWithColumns(999);
10356   CustomQualifier.AttributeMacros.push_back("__my_qualifier");
10357   verifyFormat("x = (foo * __my_qualifier) * v;"); // not parsed as qualifier.
10358   verifyFormat("x = (foo *__my_qualifier)*v;", CustomQualifier);
10359   verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)*v;").str(),
10360                CustomQualifier);
10361   verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)&v;").str(),
10362                CustomQualifier);
10363 
10364   // Check that unknown identifiers result in binary operator parsing:
10365   verifyFormat("x = (foo * __unknown_qualifier) * v;");
10366   verifyFormat("x = (foo * __unknown_qualifier) & v;");
10367 }
10368 
10369 TEST_F(FormatTest, UnderstandsSquareAttributes) {
10370   verifyFormat("SomeType s [[unused]] (InitValue);");
10371   verifyFormat("SomeType s [[gnu::unused]] (InitValue);");
10372   verifyFormat("SomeType s [[using gnu: unused]] (InitValue);");
10373   verifyFormat("[[gsl::suppress(\"clang-tidy-check-name\")]] void f() {}");
10374   verifyFormat("void f() [[deprecated(\"so sorry\")]];");
10375   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10376                "    [[unused]] aaaaaaaaaaaaaaaaaaaaaaa(int i);");
10377   verifyFormat("[[nodiscard]] bool f() { return false; }");
10378   verifyFormat("class [[nodiscard]] f {\npublic:\n  f() {}\n}");
10379   verifyFormat("class [[deprecated(\"so sorry\")]] f {\npublic:\n  f() {}\n}");
10380   verifyFormat("class [[gnu::unused]] f {\npublic:\n  f() {}\n}");
10381   verifyFormat("[[nodiscard]] ::qualified_type f();");
10382 
10383   // Make sure we do not mistake attributes for array subscripts.
10384   verifyFormat("int a() {}\n"
10385                "[[unused]] int b() {}\n");
10386   verifyFormat("NSArray *arr;\n"
10387                "arr[[Foo() bar]];");
10388 
10389   // On the other hand, we still need to correctly find array subscripts.
10390   verifyFormat("int a = std::vector<int>{1, 2, 3}[0];");
10391 
10392   // Make sure that we do not mistake Objective-C method inside array literals
10393   // as attributes, even if those method names are also keywords.
10394   verifyFormat("@[ [foo bar] ];");
10395   verifyFormat("@[ [NSArray class] ];");
10396   verifyFormat("@[ [foo enum] ];");
10397 
10398   verifyFormat("template <typename T> [[nodiscard]] int a() { return 1; }");
10399 
10400   // Make sure we do not parse attributes as lambda introducers.
10401   FormatStyle MultiLineFunctions = getLLVMStyle();
10402   MultiLineFunctions.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
10403   verifyFormat("[[unused]] int b() {\n"
10404                "  return 42;\n"
10405                "}\n",
10406                MultiLineFunctions);
10407 }
10408 
10409 TEST_F(FormatTest, AttributeClass) {
10410   FormatStyle Style = getChromiumStyle(FormatStyle::LK_Cpp);
10411   verifyFormat("class S {\n"
10412                "  S(S&&) = default;\n"
10413                "};",
10414                Style);
10415   verifyFormat("class [[nodiscard]] S {\n"
10416                "  S(S&&) = default;\n"
10417                "};",
10418                Style);
10419   verifyFormat("class __attribute((maybeunused)) S {\n"
10420                "  S(S&&) = default;\n"
10421                "};",
10422                Style);
10423   verifyFormat("struct S {\n"
10424                "  S(S&&) = default;\n"
10425                "};",
10426                Style);
10427   verifyFormat("struct [[nodiscard]] S {\n"
10428                "  S(S&&) = default;\n"
10429                "};",
10430                Style);
10431 }
10432 
10433 TEST_F(FormatTest, AttributesAfterMacro) {
10434   FormatStyle Style = getLLVMStyle();
10435   verifyFormat("MACRO;\n"
10436                "__attribute__((maybe_unused)) int foo() {\n"
10437                "  //...\n"
10438                "}");
10439 
10440   verifyFormat("MACRO;\n"
10441                "[[nodiscard]] int foo() {\n"
10442                "  //...\n"
10443                "}");
10444 
10445   EXPECT_EQ("MACRO\n\n"
10446             "__attribute__((maybe_unused)) int foo() {\n"
10447             "  //...\n"
10448             "}",
10449             format("MACRO\n\n"
10450                    "__attribute__((maybe_unused)) int foo() {\n"
10451                    "  //...\n"
10452                    "}"));
10453 
10454   EXPECT_EQ("MACRO\n\n"
10455             "[[nodiscard]] int foo() {\n"
10456             "  //...\n"
10457             "}",
10458             format("MACRO\n\n"
10459                    "[[nodiscard]] int foo() {\n"
10460                    "  //...\n"
10461                    "}"));
10462 }
10463 
10464 TEST_F(FormatTest, AttributePenaltyBreaking) {
10465   FormatStyle Style = getLLVMStyle();
10466   verifyFormat("void ABCDEFGH::ABCDEFGHIJKLMN(\n"
10467                "    [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}",
10468                Style);
10469   verifyFormat("void ABCDEFGH::ABCDEFGHIJK(\n"
10470                "    [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}",
10471                Style);
10472   verifyFormat("void ABCDEFGH::ABCDEFGH([[maybe_unused]] const "
10473                "shared_ptr<ALongTypeName> &C d) {\n}",
10474                Style);
10475 }
10476 
10477 TEST_F(FormatTest, UnderstandsEllipsis) {
10478   FormatStyle Style = getLLVMStyle();
10479   verifyFormat("int printf(const char *fmt, ...);");
10480   verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
10481   verifyFormat("template <class... Ts> void Foo(Ts *...ts) {}");
10482 
10483   verifyFormat("template <int *...PP> a;", Style);
10484 
10485   Style.PointerAlignment = FormatStyle::PAS_Left;
10486   verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", Style);
10487 
10488   verifyFormat("template <int*... PP> a;", Style);
10489 
10490   Style.PointerAlignment = FormatStyle::PAS_Middle;
10491   verifyFormat("template <int *... PP> a;", Style);
10492 }
10493 
10494 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
10495   EXPECT_EQ("int *a;\n"
10496             "int *a;\n"
10497             "int *a;",
10498             format("int *a;\n"
10499                    "int* a;\n"
10500                    "int *a;",
10501                    getGoogleStyle()));
10502   EXPECT_EQ("int* a;\n"
10503             "int* a;\n"
10504             "int* a;",
10505             format("int* a;\n"
10506                    "int* a;\n"
10507                    "int *a;",
10508                    getGoogleStyle()));
10509   EXPECT_EQ("int *a;\n"
10510             "int *a;\n"
10511             "int *a;",
10512             format("int *a;\n"
10513                    "int * a;\n"
10514                    "int *  a;",
10515                    getGoogleStyle()));
10516   EXPECT_EQ("auto x = [] {\n"
10517             "  int *a;\n"
10518             "  int *a;\n"
10519             "  int *a;\n"
10520             "};",
10521             format("auto x=[]{int *a;\n"
10522                    "int * a;\n"
10523                    "int *  a;};",
10524                    getGoogleStyle()));
10525 }
10526 
10527 TEST_F(FormatTest, UnderstandsRvalueReferences) {
10528   verifyFormat("int f(int &&a) {}");
10529   verifyFormat("int f(int a, char &&b) {}");
10530   verifyFormat("void f() { int &&a = b; }");
10531   verifyGoogleFormat("int f(int a, char&& b) {}");
10532   verifyGoogleFormat("void f() { int&& a = b; }");
10533 
10534   verifyIndependentOfContext("A<int &&> a;");
10535   verifyIndependentOfContext("A<int &&, int &&> a;");
10536   verifyGoogleFormat("A<int&&> a;");
10537   verifyGoogleFormat("A<int&&, int&&> a;");
10538 
10539   // Not rvalue references:
10540   verifyFormat("template <bool B, bool C> class A {\n"
10541                "  static_assert(B && C, \"Something is wrong\");\n"
10542                "};");
10543   verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
10544   verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
10545   verifyFormat("#define A(a, b) (a && b)");
10546 }
10547 
10548 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
10549   verifyFormat("void f() {\n"
10550                "  x[aaaaaaaaa -\n"
10551                "    b] = 23;\n"
10552                "}",
10553                getLLVMStyleWithColumns(15));
10554 }
10555 
10556 TEST_F(FormatTest, FormatsCasts) {
10557   verifyFormat("Type *A = static_cast<Type *>(P);");
10558   verifyFormat("Type *A = (Type *)P;");
10559   verifyFormat("Type *A = (vector<Type *, int *>)P;");
10560   verifyFormat("int a = (int)(2.0f);");
10561   verifyFormat("int a = (int)2.0f;");
10562   verifyFormat("x[(int32)y];");
10563   verifyFormat("x = (int32)y;");
10564   verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
10565   verifyFormat("int a = (int)*b;");
10566   verifyFormat("int a = (int)2.0f;");
10567   verifyFormat("int a = (int)~0;");
10568   verifyFormat("int a = (int)++a;");
10569   verifyFormat("int a = (int)sizeof(int);");
10570   verifyFormat("int a = (int)+2;");
10571   verifyFormat("my_int a = (my_int)2.0f;");
10572   verifyFormat("my_int a = (my_int)sizeof(int);");
10573   verifyFormat("return (my_int)aaa;");
10574   verifyFormat("#define x ((int)-1)");
10575   verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
10576   verifyFormat("#define p(q) ((int *)&q)");
10577   verifyFormat("fn(a)(b) + 1;");
10578 
10579   verifyFormat("void f() { my_int a = (my_int)*b; }");
10580   verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
10581   verifyFormat("my_int a = (my_int)~0;");
10582   verifyFormat("my_int a = (my_int)++a;");
10583   verifyFormat("my_int a = (my_int)-2;");
10584   verifyFormat("my_int a = (my_int)1;");
10585   verifyFormat("my_int a = (my_int *)1;");
10586   verifyFormat("my_int a = (const my_int)-1;");
10587   verifyFormat("my_int a = (const my_int *)-1;");
10588   verifyFormat("my_int a = (my_int)(my_int)-1;");
10589   verifyFormat("my_int a = (ns::my_int)-2;");
10590   verifyFormat("case (my_int)ONE:");
10591   verifyFormat("auto x = (X)this;");
10592   // Casts in Obj-C style calls used to not be recognized as such.
10593   verifyFormat("int a = [(type*)[((type*)val) arg] arg];", getGoogleStyle());
10594 
10595   // FIXME: single value wrapped with paren will be treated as cast.
10596   verifyFormat("void f(int i = (kValue)*kMask) {}");
10597 
10598   verifyFormat("{ (void)F; }");
10599 
10600   // Don't break after a cast's
10601   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
10602                "    (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
10603                "                                   bbbbbbbbbbbbbbbbbbbbbb);");
10604 
10605   verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(x)");
10606   verifyFormat("#define CONF_BOOL(x) (bool *)(x)");
10607   verifyFormat("#define CONF_BOOL(x) (bool)(x)");
10608   verifyFormat("bool *y = (bool *)(void *)(x);");
10609   verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(int)(x)");
10610   verifyFormat("bool *y = (bool *)(void *)(int)(x);");
10611   verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(int)foo(x)");
10612   verifyFormat("bool *y = (bool *)(void *)(int)foo(x);");
10613 
10614   // These are not casts.
10615   verifyFormat("void f(int *) {}");
10616   verifyFormat("f(foo)->b;");
10617   verifyFormat("f(foo).b;");
10618   verifyFormat("f(foo)(b);");
10619   verifyFormat("f(foo)[b];");
10620   verifyFormat("[](foo) { return 4; }(bar);");
10621   verifyFormat("(*funptr)(foo)[4];");
10622   verifyFormat("funptrs[4](foo)[4];");
10623   verifyFormat("void f(int *);");
10624   verifyFormat("void f(int *) = 0;");
10625   verifyFormat("void f(SmallVector<int>) {}");
10626   verifyFormat("void f(SmallVector<int>);");
10627   verifyFormat("void f(SmallVector<int>) = 0;");
10628   verifyFormat("void f(int i = (kA * kB) & kMask) {}");
10629   verifyFormat("int a = sizeof(int) * b;");
10630   verifyFormat("int a = alignof(int) * b;", getGoogleStyle());
10631   verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
10632   verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
10633   verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
10634 
10635   // These are not casts, but at some point were confused with casts.
10636   verifyFormat("virtual void foo(int *) override;");
10637   verifyFormat("virtual void foo(char &) const;");
10638   verifyFormat("virtual void foo(int *a, char *) const;");
10639   verifyFormat("int a = sizeof(int *) + b;");
10640   verifyFormat("int a = alignof(int *) + b;", getGoogleStyle());
10641   verifyFormat("bool b = f(g<int>) && c;");
10642   verifyFormat("typedef void (*f)(int i) func;");
10643   verifyFormat("void operator++(int) noexcept;");
10644   verifyFormat("void operator++(int &) noexcept;");
10645   verifyFormat("void operator delete(void *, std::size_t, const std::nothrow_t "
10646                "&) noexcept;");
10647   verifyFormat(
10648       "void operator delete(std::size_t, const std::nothrow_t &) noexcept;");
10649   verifyFormat("void operator delete(const std::nothrow_t &) noexcept;");
10650   verifyFormat("void operator delete(std::nothrow_t &) noexcept;");
10651   verifyFormat("void operator delete(nothrow_t &) noexcept;");
10652   verifyFormat("void operator delete(foo &) noexcept;");
10653   verifyFormat("void operator delete(foo) noexcept;");
10654   verifyFormat("void operator delete(int) noexcept;");
10655   verifyFormat("void operator delete(int &) noexcept;");
10656   verifyFormat("void operator delete(int &) volatile noexcept;");
10657   verifyFormat("void operator delete(int &) const");
10658   verifyFormat("void operator delete(int &) = default");
10659   verifyFormat("void operator delete(int &) = delete");
10660   verifyFormat("void operator delete(int &) [[noreturn]]");
10661   verifyFormat("void operator delete(int &) throw();");
10662   verifyFormat("void operator delete(int &) throw(int);");
10663   verifyFormat("auto operator delete(int &) -> int;");
10664   verifyFormat("auto operator delete(int &) override");
10665   verifyFormat("auto operator delete(int &) final");
10666 
10667   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
10668                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
10669   // FIXME: The indentation here is not ideal.
10670   verifyFormat(
10671       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10672       "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
10673       "        [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
10674 }
10675 
10676 TEST_F(FormatTest, FormatsFunctionTypes) {
10677   verifyFormat("A<bool()> a;");
10678   verifyFormat("A<SomeType()> a;");
10679   verifyFormat("A<void (*)(int, std::string)> a;");
10680   verifyFormat("A<void *(int)>;");
10681   verifyFormat("void *(*a)(int *, SomeType *);");
10682   verifyFormat("int (*func)(void *);");
10683   verifyFormat("void f() { int (*func)(void *); }");
10684   verifyFormat("template <class CallbackClass>\n"
10685                "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
10686 
10687   verifyGoogleFormat("A<void*(int*, SomeType*)>;");
10688   verifyGoogleFormat("void* (*a)(int);");
10689   verifyGoogleFormat(
10690       "template <class CallbackClass>\n"
10691       "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
10692 
10693   // Other constructs can look somewhat like function types:
10694   verifyFormat("A<sizeof(*x)> a;");
10695   verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
10696   verifyFormat("some_var = function(*some_pointer_var)[0];");
10697   verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
10698   verifyFormat("int x = f(&h)();");
10699   verifyFormat("returnsFunction(&param1, &param2)(param);");
10700   verifyFormat("std::function<\n"
10701                "    LooooooooooongTemplatedType<\n"
10702                "        SomeType>*(\n"
10703                "        LooooooooooooooooongType type)>\n"
10704                "    function;",
10705                getGoogleStyleWithColumns(40));
10706 }
10707 
10708 TEST_F(FormatTest, FormatsPointersToArrayTypes) {
10709   verifyFormat("A (*foo_)[6];");
10710   verifyFormat("vector<int> (*foo_)[6];");
10711 }
10712 
10713 TEST_F(FormatTest, BreaksLongVariableDeclarations) {
10714   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
10715                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
10716   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
10717                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
10718   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
10719                "    *LoooooooooooooooooooooooooooooooooooooooongVariable;");
10720 
10721   // Different ways of ()-initializiation.
10722   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
10723                "    LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
10724   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
10725                "    LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
10726   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
10727                "    LoooooooooooooooooooooooooooooooooooooooongVariable({});");
10728   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
10729                "    LoooooooooooooooooooooooooooooooooooooongVariable([A a]);");
10730 
10731   // Lambdas should not confuse the variable declaration heuristic.
10732   verifyFormat("LooooooooooooooooongType\n"
10733                "    variable(nullptr, [](A *a) {});",
10734                getLLVMStyleWithColumns(40));
10735 }
10736 
10737 TEST_F(FormatTest, BreaksLongDeclarations) {
10738   verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
10739                "    AnotherNameForTheLongType;");
10740   verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
10741                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10742   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10743                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
10744   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n"
10745                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
10746   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10747                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10748   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
10749                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10750   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
10751                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10752   verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
10753                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10754   verifyFormat("typeof(LoooooooooooooooooooooooooooooooooooooooooongName)\n"
10755                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10756   verifyFormat("_Atomic(LooooooooooooooooooooooooooooooooooooooooongName)\n"
10757                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10758   verifyFormat("__underlying_type(LooooooooooooooooooooooooooooooongName)\n"
10759                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10760   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10761                "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);");
10762   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10763                "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}");
10764   FormatStyle Indented = getLLVMStyle();
10765   Indented.IndentWrappedFunctionNames = true;
10766   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10767                "    LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
10768                Indented);
10769   verifyFormat(
10770       "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10771       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
10772       Indented);
10773   verifyFormat(
10774       "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
10775       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
10776       Indented);
10777   verifyFormat(
10778       "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
10779       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
10780       Indented);
10781 
10782   // FIXME: Without the comment, this breaks after "(".
10783   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType  // break\n"
10784                "    (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();",
10785                getGoogleStyle());
10786 
10787   verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
10788                "                  int LoooooooooooooooooooongParam2) {}");
10789   verifyFormat(
10790       "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
10791       "                                   SourceLocation L, IdentifierIn *II,\n"
10792       "                                   Type *T) {}");
10793   verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
10794                "ReallyReaaallyLongFunctionName(\n"
10795                "    const std::string &SomeParameter,\n"
10796                "    const SomeType<string, SomeOtherTemplateParameter>\n"
10797                "        &ReallyReallyLongParameterName,\n"
10798                "    const SomeType<string, SomeOtherTemplateParameter>\n"
10799                "        &AnotherLongParameterName) {}");
10800   verifyFormat("template <typename A>\n"
10801                "SomeLoooooooooooooooooooooongType<\n"
10802                "    typename some_namespace::SomeOtherType<A>::Type>\n"
10803                "Function() {}");
10804 
10805   verifyGoogleFormat(
10806       "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
10807       "    aaaaaaaaaaaaaaaaaaaaaaa;");
10808   verifyGoogleFormat(
10809       "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
10810       "                                   SourceLocation L) {}");
10811   verifyGoogleFormat(
10812       "some_namespace::LongReturnType\n"
10813       "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
10814       "    int first_long_parameter, int second_parameter) {}");
10815 
10816   verifyGoogleFormat("template <typename T>\n"
10817                      "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
10818                      "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
10819   verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10820                      "                   int aaaaaaaaaaaaaaaaaaaaaaa);");
10821 
10822   verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
10823                "    const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10824                "        *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
10825   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10826                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
10827                "        aaaaaaaaaaaaaaaaaaaaaaaa);");
10828   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10829                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
10830                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n"
10831                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
10832 
10833   verifyFormat("template <typename T> // Templates on own line.\n"
10834                "static int            // Some comment.\n"
10835                "MyFunction(int a);",
10836                getLLVMStyle());
10837 }
10838 
10839 TEST_F(FormatTest, FormatsAccessModifiers) {
10840   FormatStyle Style = getLLVMStyle();
10841   EXPECT_EQ(Style.EmptyLineBeforeAccessModifier,
10842             FormatStyle::ELBAMS_LogicalBlock);
10843   verifyFormat("struct foo {\n"
10844                "private:\n"
10845                "  void f() {}\n"
10846                "\n"
10847                "private:\n"
10848                "  int i;\n"
10849                "\n"
10850                "protected:\n"
10851                "  int j;\n"
10852                "};\n",
10853                Style);
10854   verifyFormat("struct foo {\n"
10855                "private:\n"
10856                "  void f() {}\n"
10857                "\n"
10858                "private:\n"
10859                "  int i;\n"
10860                "\n"
10861                "protected:\n"
10862                "  int j;\n"
10863                "};\n",
10864                "struct foo {\n"
10865                "private:\n"
10866                "  void f() {}\n"
10867                "private:\n"
10868                "  int i;\n"
10869                "protected:\n"
10870                "  int j;\n"
10871                "};\n",
10872                Style);
10873   verifyFormat("struct foo { /* comment */\n"
10874                "private:\n"
10875                "  int i;\n"
10876                "  // comment\n"
10877                "private:\n"
10878                "  int j;\n"
10879                "};\n",
10880                Style);
10881   verifyFormat("struct foo {\n"
10882                "#ifdef FOO\n"
10883                "#endif\n"
10884                "private:\n"
10885                "  int i;\n"
10886                "#ifdef FOO\n"
10887                "private:\n"
10888                "#endif\n"
10889                "  int j;\n"
10890                "};\n",
10891                Style);
10892   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
10893   verifyFormat("struct foo {\n"
10894                "private:\n"
10895                "  void f() {}\n"
10896                "private:\n"
10897                "  int i;\n"
10898                "protected:\n"
10899                "  int j;\n"
10900                "};\n",
10901                Style);
10902   verifyFormat("struct foo {\n"
10903                "private:\n"
10904                "  void f() {}\n"
10905                "private:\n"
10906                "  int i;\n"
10907                "protected:\n"
10908                "  int j;\n"
10909                "};\n",
10910                "struct foo {\n"
10911                "\n"
10912                "private:\n"
10913                "  void f() {}\n"
10914                "\n"
10915                "private:\n"
10916                "  int i;\n"
10917                "\n"
10918                "protected:\n"
10919                "  int j;\n"
10920                "};\n",
10921                Style);
10922   verifyFormat("struct foo { /* comment */\n"
10923                "private:\n"
10924                "  int i;\n"
10925                "  // comment\n"
10926                "private:\n"
10927                "  int j;\n"
10928                "};\n",
10929                "struct foo { /* comment */\n"
10930                "\n"
10931                "private:\n"
10932                "  int i;\n"
10933                "  // comment\n"
10934                "\n"
10935                "private:\n"
10936                "  int j;\n"
10937                "};\n",
10938                Style);
10939   verifyFormat("struct foo {\n"
10940                "#ifdef FOO\n"
10941                "#endif\n"
10942                "private:\n"
10943                "  int i;\n"
10944                "#ifdef FOO\n"
10945                "private:\n"
10946                "#endif\n"
10947                "  int j;\n"
10948                "};\n",
10949                "struct foo {\n"
10950                "#ifdef FOO\n"
10951                "#endif\n"
10952                "\n"
10953                "private:\n"
10954                "  int i;\n"
10955                "#ifdef FOO\n"
10956                "\n"
10957                "private:\n"
10958                "#endif\n"
10959                "  int j;\n"
10960                "};\n",
10961                Style);
10962   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
10963   verifyFormat("struct foo {\n"
10964                "private:\n"
10965                "  void f() {}\n"
10966                "\n"
10967                "private:\n"
10968                "  int i;\n"
10969                "\n"
10970                "protected:\n"
10971                "  int j;\n"
10972                "};\n",
10973                Style);
10974   verifyFormat("struct foo {\n"
10975                "private:\n"
10976                "  void f() {}\n"
10977                "\n"
10978                "private:\n"
10979                "  int i;\n"
10980                "\n"
10981                "protected:\n"
10982                "  int j;\n"
10983                "};\n",
10984                "struct foo {\n"
10985                "private:\n"
10986                "  void f() {}\n"
10987                "private:\n"
10988                "  int i;\n"
10989                "protected:\n"
10990                "  int j;\n"
10991                "};\n",
10992                Style);
10993   verifyFormat("struct foo { /* comment */\n"
10994                "private:\n"
10995                "  int i;\n"
10996                "  // comment\n"
10997                "\n"
10998                "private:\n"
10999                "  int j;\n"
11000                "};\n",
11001                "struct foo { /* comment */\n"
11002                "private:\n"
11003                "  int i;\n"
11004                "  // comment\n"
11005                "\n"
11006                "private:\n"
11007                "  int j;\n"
11008                "};\n",
11009                Style);
11010   verifyFormat("struct foo {\n"
11011                "#ifdef FOO\n"
11012                "#endif\n"
11013                "\n"
11014                "private:\n"
11015                "  int i;\n"
11016                "#ifdef FOO\n"
11017                "\n"
11018                "private:\n"
11019                "#endif\n"
11020                "  int j;\n"
11021                "};\n",
11022                "struct foo {\n"
11023                "#ifdef FOO\n"
11024                "#endif\n"
11025                "private:\n"
11026                "  int i;\n"
11027                "#ifdef FOO\n"
11028                "private:\n"
11029                "#endif\n"
11030                "  int j;\n"
11031                "};\n",
11032                Style);
11033   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
11034   EXPECT_EQ("struct foo {\n"
11035             "\n"
11036             "private:\n"
11037             "  void f() {}\n"
11038             "\n"
11039             "private:\n"
11040             "  int i;\n"
11041             "\n"
11042             "protected:\n"
11043             "  int j;\n"
11044             "};\n",
11045             format("struct foo {\n"
11046                    "\n"
11047                    "private:\n"
11048                    "  void f() {}\n"
11049                    "\n"
11050                    "private:\n"
11051                    "  int i;\n"
11052                    "\n"
11053                    "protected:\n"
11054                    "  int j;\n"
11055                    "};\n",
11056                    Style));
11057   verifyFormat("struct foo {\n"
11058                "private:\n"
11059                "  void f() {}\n"
11060                "private:\n"
11061                "  int i;\n"
11062                "protected:\n"
11063                "  int j;\n"
11064                "};\n",
11065                Style);
11066   EXPECT_EQ("struct foo { /* comment */\n"
11067             "\n"
11068             "private:\n"
11069             "  int i;\n"
11070             "  // comment\n"
11071             "\n"
11072             "private:\n"
11073             "  int j;\n"
11074             "};\n",
11075             format("struct foo { /* comment */\n"
11076                    "\n"
11077                    "private:\n"
11078                    "  int i;\n"
11079                    "  // comment\n"
11080                    "\n"
11081                    "private:\n"
11082                    "  int j;\n"
11083                    "};\n",
11084                    Style));
11085   verifyFormat("struct foo { /* comment */\n"
11086                "private:\n"
11087                "  int i;\n"
11088                "  // comment\n"
11089                "private:\n"
11090                "  int j;\n"
11091                "};\n",
11092                Style);
11093   EXPECT_EQ("struct foo {\n"
11094             "#ifdef FOO\n"
11095             "#endif\n"
11096             "\n"
11097             "private:\n"
11098             "  int i;\n"
11099             "#ifdef FOO\n"
11100             "\n"
11101             "private:\n"
11102             "#endif\n"
11103             "  int j;\n"
11104             "};\n",
11105             format("struct foo {\n"
11106                    "#ifdef FOO\n"
11107                    "#endif\n"
11108                    "\n"
11109                    "private:\n"
11110                    "  int i;\n"
11111                    "#ifdef FOO\n"
11112                    "\n"
11113                    "private:\n"
11114                    "#endif\n"
11115                    "  int j;\n"
11116                    "};\n",
11117                    Style));
11118   verifyFormat("struct foo {\n"
11119                "#ifdef FOO\n"
11120                "#endif\n"
11121                "private:\n"
11122                "  int i;\n"
11123                "#ifdef FOO\n"
11124                "private:\n"
11125                "#endif\n"
11126                "  int j;\n"
11127                "};\n",
11128                Style);
11129 
11130   FormatStyle NoEmptyLines = getLLVMStyle();
11131   NoEmptyLines.MaxEmptyLinesToKeep = 0;
11132   verifyFormat("struct foo {\n"
11133                "private:\n"
11134                "  void f() {}\n"
11135                "\n"
11136                "private:\n"
11137                "  int i;\n"
11138                "\n"
11139                "public:\n"
11140                "protected:\n"
11141                "  int j;\n"
11142                "};\n",
11143                NoEmptyLines);
11144 
11145   NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
11146   verifyFormat("struct foo {\n"
11147                "private:\n"
11148                "  void f() {}\n"
11149                "private:\n"
11150                "  int i;\n"
11151                "public:\n"
11152                "protected:\n"
11153                "  int j;\n"
11154                "};\n",
11155                NoEmptyLines);
11156 
11157   NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
11158   verifyFormat("struct foo {\n"
11159                "private:\n"
11160                "  void f() {}\n"
11161                "\n"
11162                "private:\n"
11163                "  int i;\n"
11164                "\n"
11165                "public:\n"
11166                "\n"
11167                "protected:\n"
11168                "  int j;\n"
11169                "};\n",
11170                NoEmptyLines);
11171 }
11172 
11173 TEST_F(FormatTest, FormatsAfterAccessModifiers) {
11174 
11175   FormatStyle Style = getLLVMStyle();
11176   EXPECT_EQ(Style.EmptyLineAfterAccessModifier, FormatStyle::ELAAMS_Never);
11177   verifyFormat("struct foo {\n"
11178                "private:\n"
11179                "  void f() {}\n"
11180                "\n"
11181                "private:\n"
11182                "  int i;\n"
11183                "\n"
11184                "protected:\n"
11185                "  int j;\n"
11186                "};\n",
11187                Style);
11188 
11189   // Check if lines are removed.
11190   verifyFormat("struct foo {\n"
11191                "private:\n"
11192                "  void f() {}\n"
11193                "\n"
11194                "private:\n"
11195                "  int i;\n"
11196                "\n"
11197                "protected:\n"
11198                "  int j;\n"
11199                "};\n",
11200                "struct foo {\n"
11201                "private:\n"
11202                "\n"
11203                "  void f() {}\n"
11204                "\n"
11205                "private:\n"
11206                "\n"
11207                "  int i;\n"
11208                "\n"
11209                "protected:\n"
11210                "\n"
11211                "  int j;\n"
11212                "};\n",
11213                Style);
11214 
11215   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
11216   verifyFormat("struct foo {\n"
11217                "private:\n"
11218                "\n"
11219                "  void f() {}\n"
11220                "\n"
11221                "private:\n"
11222                "\n"
11223                "  int i;\n"
11224                "\n"
11225                "protected:\n"
11226                "\n"
11227                "  int j;\n"
11228                "};\n",
11229                Style);
11230 
11231   // Check if lines are added.
11232   verifyFormat("struct foo {\n"
11233                "private:\n"
11234                "\n"
11235                "  void f() {}\n"
11236                "\n"
11237                "private:\n"
11238                "\n"
11239                "  int i;\n"
11240                "\n"
11241                "protected:\n"
11242                "\n"
11243                "  int j;\n"
11244                "};\n",
11245                "struct foo {\n"
11246                "private:\n"
11247                "  void f() {}\n"
11248                "\n"
11249                "private:\n"
11250                "  int i;\n"
11251                "\n"
11252                "protected:\n"
11253                "  int j;\n"
11254                "};\n",
11255                Style);
11256 
11257   // Leave tests rely on the code layout, test::messUp can not be used.
11258   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
11259   Style.MaxEmptyLinesToKeep = 0u;
11260   verifyFormat("struct foo {\n"
11261                "private:\n"
11262                "  void f() {}\n"
11263                "\n"
11264                "private:\n"
11265                "  int i;\n"
11266                "\n"
11267                "protected:\n"
11268                "  int j;\n"
11269                "};\n",
11270                Style);
11271 
11272   // Check if MaxEmptyLinesToKeep is respected.
11273   EXPECT_EQ("struct foo {\n"
11274             "private:\n"
11275             "  void f() {}\n"
11276             "\n"
11277             "private:\n"
11278             "  int i;\n"
11279             "\n"
11280             "protected:\n"
11281             "  int j;\n"
11282             "};\n",
11283             format("struct foo {\n"
11284                    "private:\n"
11285                    "\n\n\n"
11286                    "  void f() {}\n"
11287                    "\n"
11288                    "private:\n"
11289                    "\n\n\n"
11290                    "  int i;\n"
11291                    "\n"
11292                    "protected:\n"
11293                    "\n\n\n"
11294                    "  int j;\n"
11295                    "};\n",
11296                    Style));
11297 
11298   Style.MaxEmptyLinesToKeep = 1u;
11299   EXPECT_EQ("struct foo {\n"
11300             "private:\n"
11301             "\n"
11302             "  void f() {}\n"
11303             "\n"
11304             "private:\n"
11305             "\n"
11306             "  int i;\n"
11307             "\n"
11308             "protected:\n"
11309             "\n"
11310             "  int j;\n"
11311             "};\n",
11312             format("struct foo {\n"
11313                    "private:\n"
11314                    "\n"
11315                    "  void f() {}\n"
11316                    "\n"
11317                    "private:\n"
11318                    "\n"
11319                    "  int i;\n"
11320                    "\n"
11321                    "protected:\n"
11322                    "\n"
11323                    "  int j;\n"
11324                    "};\n",
11325                    Style));
11326   // Check if no lines are kept.
11327   EXPECT_EQ("struct foo {\n"
11328             "private:\n"
11329             "  void f() {}\n"
11330             "\n"
11331             "private:\n"
11332             "  int i;\n"
11333             "\n"
11334             "protected:\n"
11335             "  int j;\n"
11336             "};\n",
11337             format("struct foo {\n"
11338                    "private:\n"
11339                    "  void f() {}\n"
11340                    "\n"
11341                    "private:\n"
11342                    "  int i;\n"
11343                    "\n"
11344                    "protected:\n"
11345                    "  int j;\n"
11346                    "};\n",
11347                    Style));
11348   // Check if MaxEmptyLinesToKeep is respected.
11349   EXPECT_EQ("struct foo {\n"
11350             "private:\n"
11351             "\n"
11352             "  void f() {}\n"
11353             "\n"
11354             "private:\n"
11355             "\n"
11356             "  int i;\n"
11357             "\n"
11358             "protected:\n"
11359             "\n"
11360             "  int j;\n"
11361             "};\n",
11362             format("struct foo {\n"
11363                    "private:\n"
11364                    "\n\n\n"
11365                    "  void f() {}\n"
11366                    "\n"
11367                    "private:\n"
11368                    "\n\n\n"
11369                    "  int i;\n"
11370                    "\n"
11371                    "protected:\n"
11372                    "\n\n\n"
11373                    "  int j;\n"
11374                    "};\n",
11375                    Style));
11376 
11377   Style.MaxEmptyLinesToKeep = 10u;
11378   EXPECT_EQ("struct foo {\n"
11379             "private:\n"
11380             "\n\n\n"
11381             "  void f() {}\n"
11382             "\n"
11383             "private:\n"
11384             "\n\n\n"
11385             "  int i;\n"
11386             "\n"
11387             "protected:\n"
11388             "\n\n\n"
11389             "  int j;\n"
11390             "};\n",
11391             format("struct foo {\n"
11392                    "private:\n"
11393                    "\n\n\n"
11394                    "  void f() {}\n"
11395                    "\n"
11396                    "private:\n"
11397                    "\n\n\n"
11398                    "  int i;\n"
11399                    "\n"
11400                    "protected:\n"
11401                    "\n\n\n"
11402                    "  int j;\n"
11403                    "};\n",
11404                    Style));
11405 
11406   // Test with comments.
11407   Style = getLLVMStyle();
11408   verifyFormat("struct foo {\n"
11409                "private:\n"
11410                "  // comment\n"
11411                "  void f() {}\n"
11412                "\n"
11413                "private: /* comment */\n"
11414                "  int i;\n"
11415                "};\n",
11416                Style);
11417   verifyFormat("struct foo {\n"
11418                "private:\n"
11419                "  // comment\n"
11420                "  void f() {}\n"
11421                "\n"
11422                "private: /* comment */\n"
11423                "  int i;\n"
11424                "};\n",
11425                "struct foo {\n"
11426                "private:\n"
11427                "\n"
11428                "  // comment\n"
11429                "  void f() {}\n"
11430                "\n"
11431                "private: /* comment */\n"
11432                "\n"
11433                "  int i;\n"
11434                "};\n",
11435                Style);
11436 
11437   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
11438   verifyFormat("struct foo {\n"
11439                "private:\n"
11440                "\n"
11441                "  // comment\n"
11442                "  void f() {}\n"
11443                "\n"
11444                "private: /* comment */\n"
11445                "\n"
11446                "  int i;\n"
11447                "};\n",
11448                "struct foo {\n"
11449                "private:\n"
11450                "  // comment\n"
11451                "  void f() {}\n"
11452                "\n"
11453                "private: /* comment */\n"
11454                "  int i;\n"
11455                "};\n",
11456                Style);
11457   verifyFormat("struct foo {\n"
11458                "private:\n"
11459                "\n"
11460                "  // comment\n"
11461                "  void f() {}\n"
11462                "\n"
11463                "private: /* comment */\n"
11464                "\n"
11465                "  int i;\n"
11466                "};\n",
11467                Style);
11468 
11469   // Test with preprocessor defines.
11470   Style = getLLVMStyle();
11471   verifyFormat("struct foo {\n"
11472                "private:\n"
11473                "#ifdef FOO\n"
11474                "#endif\n"
11475                "  void f() {}\n"
11476                "};\n",
11477                Style);
11478   verifyFormat("struct foo {\n"
11479                "private:\n"
11480                "#ifdef FOO\n"
11481                "#endif\n"
11482                "  void f() {}\n"
11483                "};\n",
11484                "struct foo {\n"
11485                "private:\n"
11486                "\n"
11487                "#ifdef FOO\n"
11488                "#endif\n"
11489                "  void f() {}\n"
11490                "};\n",
11491                Style);
11492 
11493   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
11494   verifyFormat("struct foo {\n"
11495                "private:\n"
11496                "\n"
11497                "#ifdef FOO\n"
11498                "#endif\n"
11499                "  void f() {}\n"
11500                "};\n",
11501                "struct foo {\n"
11502                "private:\n"
11503                "#ifdef FOO\n"
11504                "#endif\n"
11505                "  void f() {}\n"
11506                "};\n",
11507                Style);
11508   verifyFormat("struct foo {\n"
11509                "private:\n"
11510                "\n"
11511                "#ifdef FOO\n"
11512                "#endif\n"
11513                "  void f() {}\n"
11514                "};\n",
11515                Style);
11516 }
11517 
11518 TEST_F(FormatTest, FormatsAfterAndBeforeAccessModifiersInteraction) {
11519   // Combined tests of EmptyLineAfterAccessModifier and
11520   // EmptyLineBeforeAccessModifier.
11521   FormatStyle Style = getLLVMStyle();
11522   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
11523   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
11524   verifyFormat("struct foo {\n"
11525                "private:\n"
11526                "\n"
11527                "protected:\n"
11528                "};\n",
11529                Style);
11530 
11531   Style.MaxEmptyLinesToKeep = 10u;
11532   // Both remove all new lines.
11533   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
11534   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
11535   verifyFormat("struct foo {\n"
11536                "private:\n"
11537                "protected:\n"
11538                "};\n",
11539                "struct foo {\n"
11540                "private:\n"
11541                "\n\n\n"
11542                "protected:\n"
11543                "};\n",
11544                Style);
11545 
11546   // Leave tests rely on the code layout, test::messUp can not be used.
11547   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
11548   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
11549   Style.MaxEmptyLinesToKeep = 10u;
11550   EXPECT_EQ("struct foo {\n"
11551             "private:\n"
11552             "\n\n\n"
11553             "protected:\n"
11554             "};\n",
11555             format("struct foo {\n"
11556                    "private:\n"
11557                    "\n\n\n"
11558                    "protected:\n"
11559                    "};\n",
11560                    Style));
11561   Style.MaxEmptyLinesToKeep = 3u;
11562   EXPECT_EQ("struct foo {\n"
11563             "private:\n"
11564             "\n\n\n"
11565             "protected:\n"
11566             "};\n",
11567             format("struct foo {\n"
11568                    "private:\n"
11569                    "\n\n\n"
11570                    "protected:\n"
11571                    "};\n",
11572                    Style));
11573   Style.MaxEmptyLinesToKeep = 1u;
11574   EXPECT_EQ("struct foo {\n"
11575             "private:\n"
11576             "\n\n\n"
11577             "protected:\n"
11578             "};\n",
11579             format("struct foo {\n"
11580                    "private:\n"
11581                    "\n\n\n"
11582                    "protected:\n"
11583                    "};\n",
11584                    Style)); // Based on new lines in original document and not
11585                             // on the setting.
11586 
11587   Style.MaxEmptyLinesToKeep = 10u;
11588   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
11589   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
11590   // Newlines are kept if they are greater than zero,
11591   // test::messUp removes all new lines which changes the logic
11592   EXPECT_EQ("struct foo {\n"
11593             "private:\n"
11594             "\n\n\n"
11595             "protected:\n"
11596             "};\n",
11597             format("struct foo {\n"
11598                    "private:\n"
11599                    "\n\n\n"
11600                    "protected:\n"
11601                    "};\n",
11602                    Style));
11603 
11604   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
11605   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
11606   // test::messUp removes all new lines which changes the logic
11607   EXPECT_EQ("struct foo {\n"
11608             "private:\n"
11609             "\n\n\n"
11610             "protected:\n"
11611             "};\n",
11612             format("struct foo {\n"
11613                    "private:\n"
11614                    "\n\n\n"
11615                    "protected:\n"
11616                    "};\n",
11617                    Style));
11618 
11619   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
11620   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
11621   EXPECT_EQ("struct foo {\n"
11622             "private:\n"
11623             "\n\n\n"
11624             "protected:\n"
11625             "};\n",
11626             format("struct foo {\n"
11627                    "private:\n"
11628                    "\n\n\n"
11629                    "protected:\n"
11630                    "};\n",
11631                    Style)); // test::messUp removes all new lines which changes
11632                             // the logic.
11633 
11634   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
11635   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
11636   verifyFormat("struct foo {\n"
11637                "private:\n"
11638                "protected:\n"
11639                "};\n",
11640                "struct foo {\n"
11641                "private:\n"
11642                "\n\n\n"
11643                "protected:\n"
11644                "};\n",
11645                Style);
11646 
11647   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
11648   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
11649   EXPECT_EQ("struct foo {\n"
11650             "private:\n"
11651             "\n\n\n"
11652             "protected:\n"
11653             "};\n",
11654             format("struct foo {\n"
11655                    "private:\n"
11656                    "\n\n\n"
11657                    "protected:\n"
11658                    "};\n",
11659                    Style)); // test::messUp removes all new lines which changes
11660                             // the logic.
11661 
11662   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
11663   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
11664   verifyFormat("struct foo {\n"
11665                "private:\n"
11666                "protected:\n"
11667                "};\n",
11668                "struct foo {\n"
11669                "private:\n"
11670                "\n\n\n"
11671                "protected:\n"
11672                "};\n",
11673                Style);
11674 
11675   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
11676   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
11677   verifyFormat("struct foo {\n"
11678                "private:\n"
11679                "protected:\n"
11680                "};\n",
11681                "struct foo {\n"
11682                "private:\n"
11683                "\n\n\n"
11684                "protected:\n"
11685                "};\n",
11686                Style);
11687 
11688   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
11689   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
11690   verifyFormat("struct foo {\n"
11691                "private:\n"
11692                "protected:\n"
11693                "};\n",
11694                "struct foo {\n"
11695                "private:\n"
11696                "\n\n\n"
11697                "protected:\n"
11698                "};\n",
11699                Style);
11700 
11701   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
11702   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
11703   verifyFormat("struct foo {\n"
11704                "private:\n"
11705                "protected:\n"
11706                "};\n",
11707                "struct foo {\n"
11708                "private:\n"
11709                "\n\n\n"
11710                "protected:\n"
11711                "};\n",
11712                Style);
11713 }
11714 
11715 TEST_F(FormatTest, FormatsArrays) {
11716   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
11717                "                         [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
11718   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n"
11719                "                         [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;");
11720   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n"
11721                "    aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}");
11722   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
11723                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
11724   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
11725                "    [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
11726   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
11727                "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
11728                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
11729   verifyFormat(
11730       "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
11731       "             << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
11732       "                                  [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
11733   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n"
11734                "    .aaaaaaaaaaaaaaaaaaaaaa();");
11735 
11736   verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
11737                      "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
11738   verifyFormat(
11739       "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
11740       "                                  .aaaaaaa[0]\n"
11741       "                                  .aaaaaaaaaaaaaaaaaaaaaa();");
11742   verifyFormat("a[::b::c];");
11743 
11744   verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10));
11745 
11746   FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0);
11747   verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit);
11748 }
11749 
11750 TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
11751   verifyFormat("(a)->b();");
11752   verifyFormat("--a;");
11753 }
11754 
11755 TEST_F(FormatTest, HandlesIncludeDirectives) {
11756   verifyFormat("#include <string>\n"
11757                "#include <a/b/c.h>\n"
11758                "#include \"a/b/string\"\n"
11759                "#include \"string.h\"\n"
11760                "#include \"string.h\"\n"
11761                "#include <a-a>\n"
11762                "#include < path with space >\n"
11763                "#include_next <test.h>"
11764                "#include \"abc.h\" // this is included for ABC\n"
11765                "#include \"some long include\" // with a comment\n"
11766                "#include \"some very long include path\"\n"
11767                "#include <some/very/long/include/path>\n",
11768                getLLVMStyleWithColumns(35));
11769   EXPECT_EQ("#include \"a.h\"", format("#include  \"a.h\""));
11770   EXPECT_EQ("#include <a>", format("#include<a>"));
11771 
11772   verifyFormat("#import <string>");
11773   verifyFormat("#import <a/b/c.h>");
11774   verifyFormat("#import \"a/b/string\"");
11775   verifyFormat("#import \"string.h\"");
11776   verifyFormat("#import \"string.h\"");
11777   verifyFormat("#if __has_include(<strstream>)\n"
11778                "#include <strstream>\n"
11779                "#endif");
11780 
11781   verifyFormat("#define MY_IMPORT <a/b>");
11782 
11783   verifyFormat("#if __has_include(<a/b>)");
11784   verifyFormat("#if __has_include_next(<a/b>)");
11785   verifyFormat("#define F __has_include(<a/b>)");
11786   verifyFormat("#define F __has_include_next(<a/b>)");
11787 
11788   // Protocol buffer definition or missing "#".
11789   verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
11790                getLLVMStyleWithColumns(30));
11791 
11792   FormatStyle Style = getLLVMStyle();
11793   Style.AlwaysBreakBeforeMultilineStrings = true;
11794   Style.ColumnLimit = 0;
11795   verifyFormat("#import \"abc.h\"", Style);
11796 
11797   // But 'import' might also be a regular C++ namespace.
11798   verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
11799                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
11800 }
11801 
11802 //===----------------------------------------------------------------------===//
11803 // Error recovery tests.
11804 //===----------------------------------------------------------------------===//
11805 
11806 TEST_F(FormatTest, IncompleteParameterLists) {
11807   FormatStyle NoBinPacking = getLLVMStyle();
11808   NoBinPacking.BinPackParameters = false;
11809   verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
11810                "                        double *min_x,\n"
11811                "                        double *max_x,\n"
11812                "                        double *min_y,\n"
11813                "                        double *max_y,\n"
11814                "                        double *min_z,\n"
11815                "                        double *max_z, ) {}",
11816                NoBinPacking);
11817 }
11818 
11819 TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
11820   verifyFormat("void f() { return; }\n42");
11821   verifyFormat("void f() {\n"
11822                "  if (0)\n"
11823                "    return;\n"
11824                "}\n"
11825                "42");
11826   verifyFormat("void f() { return }\n42");
11827   verifyFormat("void f() {\n"
11828                "  if (0)\n"
11829                "    return\n"
11830                "}\n"
11831                "42");
11832 }
11833 
11834 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
11835   EXPECT_EQ("void f() { return }", format("void  f ( )  {  return  }"));
11836   EXPECT_EQ("void f() {\n"
11837             "  if (a)\n"
11838             "    return\n"
11839             "}",
11840             format("void  f  (  )  {  if  ( a )  return  }"));
11841   EXPECT_EQ("namespace N {\n"
11842             "void f()\n"
11843             "}",
11844             format("namespace  N  {  void f()  }"));
11845   EXPECT_EQ("namespace N {\n"
11846             "void f() {}\n"
11847             "void g()\n"
11848             "} // namespace N",
11849             format("namespace N  { void f( ) { } void g( ) }"));
11850 }
11851 
11852 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
11853   verifyFormat("int aaaaaaaa =\n"
11854                "    // Overlylongcomment\n"
11855                "    b;",
11856                getLLVMStyleWithColumns(20));
11857   verifyFormat("function(\n"
11858                "    ShortArgument,\n"
11859                "    LoooooooooooongArgument);\n",
11860                getLLVMStyleWithColumns(20));
11861 }
11862 
11863 TEST_F(FormatTest, IncorrectAccessSpecifier) {
11864   verifyFormat("public:");
11865   verifyFormat("class A {\n"
11866                "public\n"
11867                "  void f() {}\n"
11868                "};");
11869   verifyFormat("public\n"
11870                "int qwerty;");
11871   verifyFormat("public\n"
11872                "B {}");
11873   verifyFormat("public\n"
11874                "{}");
11875   verifyFormat("public\n"
11876                "B { int x; }");
11877 }
11878 
11879 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
11880   verifyFormat("{");
11881   verifyFormat("#})");
11882   verifyNoCrash("(/**/[:!] ?[).");
11883 }
11884 
11885 TEST_F(FormatTest, IncorrectUnbalancedBracesInMacrosWithUnicode) {
11886   // Found by oss-fuzz:
11887   // https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=8212
11888   FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp);
11889   Style.ColumnLimit = 60;
11890   verifyNoCrash(
11891       "\x23\x47\xff\x20\x28\xff\x3c\xff\x3f\xff\x20\x2f\x7b\x7a\xff\x20"
11892       "\xff\xff\xff\xca\xb5\xff\xff\xff\xff\x3a\x7b\x7d\xff\x20\xff\x20"
11893       "\xff\x74\xff\x20\x7d\x7d\xff\x7b\x3a\xff\x20\x71\xff\x20\xff\x0a",
11894       Style);
11895 }
11896 
11897 TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
11898   verifyFormat("do {\n}");
11899   verifyFormat("do {\n}\n"
11900                "f();");
11901   verifyFormat("do {\n}\n"
11902                "wheeee(fun);");
11903   verifyFormat("do {\n"
11904                "  f();\n"
11905                "}");
11906 }
11907 
11908 TEST_F(FormatTest, IncorrectCodeMissingParens) {
11909   verifyFormat("if {\n  foo;\n  foo();\n}");
11910   verifyFormat("switch {\n  foo;\n  foo();\n}");
11911   verifyIncompleteFormat("for {\n  foo;\n  foo();\n}");
11912   verifyFormat("while {\n  foo;\n  foo();\n}");
11913   verifyFormat("do {\n  foo;\n  foo();\n} while;");
11914 }
11915 
11916 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
11917   verifyIncompleteFormat("namespace {\n"
11918                          "class Foo { Foo (\n"
11919                          "};\n"
11920                          "} // namespace");
11921 }
11922 
11923 TEST_F(FormatTest, IncorrectCodeErrorDetection) {
11924   EXPECT_EQ("{\n  {}\n", format("{\n{\n}\n"));
11925   EXPECT_EQ("{\n  {}\n", format("{\n  {\n}\n"));
11926   EXPECT_EQ("{\n  {}\n", format("{\n  {\n  }\n"));
11927   EXPECT_EQ("{\n  {}\n}\n}\n", format("{\n  {\n    }\n  }\n}\n"));
11928 
11929   EXPECT_EQ("{\n"
11930             "  {\n"
11931             "    breakme(\n"
11932             "        qwe);\n"
11933             "  }\n",
11934             format("{\n"
11935                    "    {\n"
11936                    " breakme(qwe);\n"
11937                    "}\n",
11938                    getLLVMStyleWithColumns(10)));
11939 }
11940 
11941 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
11942   verifyFormat("int x = {\n"
11943                "    avariable,\n"
11944                "    b(alongervariable)};",
11945                getLLVMStyleWithColumns(25));
11946 }
11947 
11948 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
11949   verifyFormat("return (a)(b){1, 2, 3};");
11950 }
11951 
11952 TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
11953   verifyFormat("vector<int> x{1, 2, 3, 4};");
11954   verifyFormat("vector<int> x{\n"
11955                "    1,\n"
11956                "    2,\n"
11957                "    3,\n"
11958                "    4,\n"
11959                "};");
11960   verifyFormat("vector<T> x{{}, {}, {}, {}};");
11961   verifyFormat("f({1, 2});");
11962   verifyFormat("auto v = Foo{-1};");
11963   verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
11964   verifyFormat("Class::Class : member{1, 2, 3} {}");
11965   verifyFormat("new vector<int>{1, 2, 3};");
11966   verifyFormat("new int[3]{1, 2, 3};");
11967   verifyFormat("new int{1};");
11968   verifyFormat("return {arg1, arg2};");
11969   verifyFormat("return {arg1, SomeType{parameter}};");
11970   verifyFormat("int count = set<int>{f(), g(), h()}.size();");
11971   verifyFormat("new T{arg1, arg2};");
11972   verifyFormat("f(MyMap[{composite, key}]);");
11973   verifyFormat("class Class {\n"
11974                "  T member = {arg1, arg2};\n"
11975                "};");
11976   verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
11977   verifyFormat("const struct A a = {.a = 1, .b = 2};");
11978   verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
11979   verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
11980   verifyFormat("int a = std::is_integral<int>{} + 0;");
11981 
11982   verifyFormat("int foo(int i) { return fo1{}(i); }");
11983   verifyFormat("int foo(int i) { return fo1{}(i); }");
11984   verifyFormat("auto i = decltype(x){};");
11985   verifyFormat("auto i = typeof(x){};");
11986   verifyFormat("auto i = _Atomic(x){};");
11987   verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
11988   verifyFormat("Node n{1, Node{1000}, //\n"
11989                "       2};");
11990   verifyFormat("Aaaa aaaaaaa{\n"
11991                "    {\n"
11992                "        aaaa,\n"
11993                "    },\n"
11994                "};");
11995   verifyFormat("class C : public D {\n"
11996                "  SomeClass SC{2};\n"
11997                "};");
11998   verifyFormat("class C : public A {\n"
11999                "  class D : public B {\n"
12000                "    void f() { int i{2}; }\n"
12001                "  };\n"
12002                "};");
12003   verifyFormat("#define A {a, a},");
12004   // Don't confuse braced list initializers with compound statements.
12005   verifyFormat(
12006       "class A {\n"
12007       "  A() : a{} {}\n"
12008       "  A(int b) : b(b) {}\n"
12009       "  A(int a, int b) : a(a), bs{{bs...}} { f(); }\n"
12010       "  int a, b;\n"
12011       "  explicit Expr(const Scalar<Result> &x) : u{Constant<Result>{x}} {}\n"
12012       "  explicit Expr(Scalar<Result> &&x) : u{Constant<Result>{std::move(x)}} "
12013       "{}\n"
12014       "};");
12015 
12016   // Avoid breaking between equal sign and opening brace
12017   FormatStyle AvoidBreakingFirstArgument = getLLVMStyle();
12018   AvoidBreakingFirstArgument.PenaltyBreakBeforeFirstCallParameter = 200;
12019   verifyFormat("const std::unordered_map<std::string, int> MyHashTable =\n"
12020                "    {{\"aaaaaaaaaaaaaaaaaaaaa\", 0},\n"
12021                "     {\"bbbbbbbbbbbbbbbbbbbbb\", 1},\n"
12022                "     {\"ccccccccccccccccccccc\", 2}};",
12023                AvoidBreakingFirstArgument);
12024 
12025   // Binpacking only if there is no trailing comma
12026   verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n"
12027                "                      cccccccccc, dddddddddd};",
12028                getLLVMStyleWithColumns(50));
12029   verifyFormat("const Aaaaaa aaaaa = {\n"
12030                "    aaaaaaaaaaa,\n"
12031                "    bbbbbbbbbbb,\n"
12032                "    ccccccccccc,\n"
12033                "    ddddddddddd,\n"
12034                "};",
12035                getLLVMStyleWithColumns(50));
12036 
12037   // Cases where distinguising braced lists and blocks is hard.
12038   verifyFormat("vector<int> v{12} GUARDED_BY(mutex);");
12039   verifyFormat("void f() {\n"
12040                "  return; // comment\n"
12041                "}\n"
12042                "SomeType t;");
12043   verifyFormat("void f() {\n"
12044                "  if (a) {\n"
12045                "    f();\n"
12046                "  }\n"
12047                "}\n"
12048                "SomeType t;");
12049 
12050   // In combination with BinPackArguments = false.
12051   FormatStyle NoBinPacking = getLLVMStyle();
12052   NoBinPacking.BinPackArguments = false;
12053   verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
12054                "                      bbbbb,\n"
12055                "                      ccccc,\n"
12056                "                      ddddd,\n"
12057                "                      eeeee,\n"
12058                "                      ffffff,\n"
12059                "                      ggggg,\n"
12060                "                      hhhhhh,\n"
12061                "                      iiiiii,\n"
12062                "                      jjjjjj,\n"
12063                "                      kkkkkk};",
12064                NoBinPacking);
12065   verifyFormat("const Aaaaaa aaaaa = {\n"
12066                "    aaaaa,\n"
12067                "    bbbbb,\n"
12068                "    ccccc,\n"
12069                "    ddddd,\n"
12070                "    eeeee,\n"
12071                "    ffffff,\n"
12072                "    ggggg,\n"
12073                "    hhhhhh,\n"
12074                "    iiiiii,\n"
12075                "    jjjjjj,\n"
12076                "    kkkkkk,\n"
12077                "};",
12078                NoBinPacking);
12079   verifyFormat(
12080       "const Aaaaaa aaaaa = {\n"
12081       "    aaaaa,  bbbbb,  ccccc,  ddddd,  eeeee,  ffffff, ggggg, hhhhhh,\n"
12082       "    iiiiii, jjjjjj, kkkkkk, aaaaa,  bbbbb,  ccccc,  ddddd, eeeee,\n"
12083       "    ffffff, ggggg,  hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
12084       "};",
12085       NoBinPacking);
12086 
12087   NoBinPacking.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
12088   EXPECT_EQ("static uint8 CddDp83848Reg[] = {\n"
12089             "    CDDDP83848_BMCR_REGISTER,\n"
12090             "    CDDDP83848_BMSR_REGISTER,\n"
12091             "    CDDDP83848_RBR_REGISTER};",
12092             format("static uint8 CddDp83848Reg[] = {CDDDP83848_BMCR_REGISTER,\n"
12093                    "                                CDDDP83848_BMSR_REGISTER,\n"
12094                    "                                CDDDP83848_RBR_REGISTER};",
12095                    NoBinPacking));
12096 
12097   // FIXME: The alignment of these trailing comments might be bad. Then again,
12098   // this might be utterly useless in real code.
12099   verifyFormat("Constructor::Constructor()\n"
12100                "    : some_value{         //\n"
12101                "                 aaaaaaa, //\n"
12102                "                 bbbbbbb} {}");
12103 
12104   // In braced lists, the first comment is always assumed to belong to the
12105   // first element. Thus, it can be moved to the next or previous line as
12106   // appropriate.
12107   EXPECT_EQ("function({// First element:\n"
12108             "          1,\n"
12109             "          // Second element:\n"
12110             "          2});",
12111             format("function({\n"
12112                    "    // First element:\n"
12113                    "    1,\n"
12114                    "    // Second element:\n"
12115                    "    2});"));
12116   EXPECT_EQ("std::vector<int> MyNumbers{\n"
12117             "    // First element:\n"
12118             "    1,\n"
12119             "    // Second element:\n"
12120             "    2};",
12121             format("std::vector<int> MyNumbers{// First element:\n"
12122                    "                           1,\n"
12123                    "                           // Second element:\n"
12124                    "                           2};",
12125                    getLLVMStyleWithColumns(30)));
12126   // A trailing comma should still lead to an enforced line break and no
12127   // binpacking.
12128   EXPECT_EQ("vector<int> SomeVector = {\n"
12129             "    // aaa\n"
12130             "    1,\n"
12131             "    2,\n"
12132             "};",
12133             format("vector<int> SomeVector = { // aaa\n"
12134                    "    1, 2, };"));
12135 
12136   // C++11 brace initializer list l-braces should not be treated any differently
12137   // when breaking before lambda bodies is enabled
12138   FormatStyle BreakBeforeLambdaBody = getLLVMStyle();
12139   BreakBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
12140   BreakBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
12141   BreakBeforeLambdaBody.AlwaysBreakBeforeMultilineStrings = true;
12142   verifyFormat(
12143       "std::runtime_error{\n"
12144       "    \"Long string which will force a break onto the next line...\"};",
12145       BreakBeforeLambdaBody);
12146 
12147   FormatStyle ExtraSpaces = getLLVMStyle();
12148   ExtraSpaces.Cpp11BracedListStyle = false;
12149   ExtraSpaces.ColumnLimit = 75;
12150   verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
12151   verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
12152   verifyFormat("f({ 1, 2 });", ExtraSpaces);
12153   verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
12154   verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
12155   verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
12156   verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
12157   verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
12158   verifyFormat("return { arg1, arg2 };", ExtraSpaces);
12159   verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
12160   verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
12161   verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
12162   verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
12163   verifyFormat("class Class {\n"
12164                "  T member = { arg1, arg2 };\n"
12165                "};",
12166                ExtraSpaces);
12167   verifyFormat(
12168       "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
12169       "                                 aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
12170       "                  : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
12171       "                                 bbbbbbbbbbbbbbbbbbbb, bbbbb };",
12172       ExtraSpaces);
12173   verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
12174   verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
12175                ExtraSpaces);
12176   verifyFormat(
12177       "someFunction(OtherParam,\n"
12178       "             BracedList{ // comment 1 (Forcing interesting break)\n"
12179       "                         param1, param2,\n"
12180       "                         // comment 2\n"
12181       "                         param3, param4 });",
12182       ExtraSpaces);
12183   verifyFormat(
12184       "std::this_thread::sleep_for(\n"
12185       "    std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
12186       ExtraSpaces);
12187   verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n"
12188                "    aaaaaaa,\n"
12189                "    aaaaaaaaaa,\n"
12190                "    aaaaa,\n"
12191                "    aaaaaaaaaaaaaaa,\n"
12192                "    aaa,\n"
12193                "    aaaaaaaaaa,\n"
12194                "    a,\n"
12195                "    aaaaaaaaaaaaaaaaaaaaa,\n"
12196                "    aaaaaaaaaaaa,\n"
12197                "    aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
12198                "    aaaaaaa,\n"
12199                "    a};");
12200   verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
12201   verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces);
12202   verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces);
12203 
12204   // Avoid breaking between initializer/equal sign and opening brace
12205   ExtraSpaces.PenaltyBreakBeforeFirstCallParameter = 200;
12206   verifyFormat("const std::unordered_map<std::string, int> MyHashTable = {\n"
12207                "  { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n"
12208                "  { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n"
12209                "  { \"ccccccccccccccccccccc\", 2 }\n"
12210                "};",
12211                ExtraSpaces);
12212   verifyFormat("const std::unordered_map<std::string, int> MyHashTable{\n"
12213                "  { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n"
12214                "  { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n"
12215                "  { \"ccccccccccccccccccccc\", 2 }\n"
12216                "};",
12217                ExtraSpaces);
12218 
12219   FormatStyle SpaceBeforeBrace = getLLVMStyle();
12220   SpaceBeforeBrace.SpaceBeforeCpp11BracedList = true;
12221   verifyFormat("vector<int> x {1, 2, 3, 4};", SpaceBeforeBrace);
12222   verifyFormat("f({}, {{}, {}}, MyMap[{k, v}]);", SpaceBeforeBrace);
12223 
12224   FormatStyle SpaceBetweenBraces = getLLVMStyle();
12225   SpaceBetweenBraces.SpacesInAngles = FormatStyle::SIAS_Always;
12226   SpaceBetweenBraces.SpacesInParentheses = true;
12227   SpaceBetweenBraces.SpacesInSquareBrackets = true;
12228   verifyFormat("vector< int > x{ 1, 2, 3, 4 };", SpaceBetweenBraces);
12229   verifyFormat("f( {}, { {}, {} }, MyMap[ { k, v } ] );", SpaceBetweenBraces);
12230   verifyFormat("vector< int > x{ // comment 1\n"
12231                "                 1, 2, 3, 4 };",
12232                SpaceBetweenBraces);
12233   SpaceBetweenBraces.ColumnLimit = 20;
12234   EXPECT_EQ("vector< int > x{\n"
12235             "    1, 2, 3, 4 };",
12236             format("vector<int>x{1,2,3,4};", SpaceBetweenBraces));
12237   SpaceBetweenBraces.ColumnLimit = 24;
12238   EXPECT_EQ("vector< int > x{ 1, 2,\n"
12239             "                 3, 4 };",
12240             format("vector<int>x{1,2,3,4};", SpaceBetweenBraces));
12241   EXPECT_EQ("vector< int > x{\n"
12242             "    1,\n"
12243             "    2,\n"
12244             "    3,\n"
12245             "    4,\n"
12246             "};",
12247             format("vector<int>x{1,2,3,4,};", SpaceBetweenBraces));
12248   verifyFormat("vector< int > x{};", SpaceBetweenBraces);
12249   SpaceBetweenBraces.SpaceInEmptyParentheses = true;
12250   verifyFormat("vector< int > x{ };", SpaceBetweenBraces);
12251 }
12252 
12253 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
12254   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
12255                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
12256                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
12257                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
12258                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
12259                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
12260   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n"
12261                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
12262                "                 1, 22, 333, 4444, 55555, //\n"
12263                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
12264                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
12265   verifyFormat(
12266       "vector<int> x = {1,       22, 333, 4444, 55555, 666666, 7777777,\n"
12267       "                 1,       22, 333, 4444, 55555, 666666, 7777777,\n"
12268       "                 1,       22, 333, 4444, 55555, 666666, // comment\n"
12269       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
12270       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
12271       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
12272       "                 7777777};");
12273   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
12274                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
12275                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
12276   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
12277                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
12278                "    // Separating comment.\n"
12279                "    X86::R8, X86::R9, X86::R10, X86::R11, 0};");
12280   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
12281                "    // Leading comment\n"
12282                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
12283                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
12284   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
12285                "                 1, 1, 1, 1};",
12286                getLLVMStyleWithColumns(39));
12287   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
12288                "                 1, 1, 1, 1};",
12289                getLLVMStyleWithColumns(38));
12290   verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
12291                "    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
12292                getLLVMStyleWithColumns(43));
12293   verifyFormat(
12294       "static unsigned SomeValues[10][3] = {\n"
12295       "    {1, 4, 0},  {4, 9, 0},  {4, 5, 9},  {8, 5, 4}, {1, 8, 4},\n"
12296       "    {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};");
12297   verifyFormat("static auto fields = new vector<string>{\n"
12298                "    \"aaaaaaaaaaaaa\",\n"
12299                "    \"aaaaaaaaaaaaa\",\n"
12300                "    \"aaaaaaaaaaaa\",\n"
12301                "    \"aaaaaaaaaaaaaa\",\n"
12302                "    \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
12303                "    \"aaaaaaaaaaaa\",\n"
12304                "    \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
12305                "};");
12306   verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};");
12307   verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n"
12308                "                 2, bbbbbbbbbbbbbbbbbbbbbb,\n"
12309                "                 3, cccccccccccccccccccccc};",
12310                getLLVMStyleWithColumns(60));
12311 
12312   // Trailing commas.
12313   verifyFormat("vector<int> x = {\n"
12314                "    1, 1, 1, 1, 1, 1, 1, 1,\n"
12315                "};",
12316                getLLVMStyleWithColumns(39));
12317   verifyFormat("vector<int> x = {\n"
12318                "    1, 1, 1, 1, 1, 1, 1, 1, //\n"
12319                "};",
12320                getLLVMStyleWithColumns(39));
12321   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
12322                "                 1, 1, 1, 1,\n"
12323                "                 /**/ /**/};",
12324                getLLVMStyleWithColumns(39));
12325 
12326   // Trailing comment in the first line.
12327   verifyFormat("vector<int> iiiiiiiiiiiiiii = {                      //\n"
12328                "    1111111111, 2222222222, 33333333333, 4444444444, //\n"
12329                "    111111111,  222222222,  3333333333,  444444444,  //\n"
12330                "    11111111,   22222222,   333333333,   44444444};");
12331   // Trailing comment in the last line.
12332   verifyFormat("int aaaaa[] = {\n"
12333                "    1, 2, 3, // comment\n"
12334                "    4, 5, 6  // comment\n"
12335                "};");
12336 
12337   // With nested lists, we should either format one item per line or all nested
12338   // lists one on line.
12339   // FIXME: For some nested lists, we can do better.
12340   verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
12341                "        {aaaaaaaaaaaaaaaaaaa},\n"
12342                "        {aaaaaaaaaaaaaaaaaaaaa},\n"
12343                "        {aaaaaaaaaaaaaaaaa}};",
12344                getLLVMStyleWithColumns(60));
12345   verifyFormat(
12346       "SomeStruct my_struct_array = {\n"
12347       "    {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
12348       "     aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
12349       "    {aaa, aaa},\n"
12350       "    {aaa, aaa},\n"
12351       "    {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
12352       "    {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
12353       "     aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
12354 
12355   // No column layout should be used here.
12356   verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
12357                "                   bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
12358 
12359   verifyNoCrash("a<,");
12360 
12361   // No braced initializer here.
12362   verifyFormat("void f() {\n"
12363                "  struct Dummy {};\n"
12364                "  f(v);\n"
12365                "}");
12366 
12367   // Long lists should be formatted in columns even if they are nested.
12368   verifyFormat(
12369       "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n"
12370       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
12371       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
12372       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
12373       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
12374       "                          1, 22, 333, 4444, 55555, 666666, 7777777});");
12375 
12376   // Allow "single-column" layout even if that violates the column limit. There
12377   // isn't going to be a better way.
12378   verifyFormat("std::vector<int> a = {\n"
12379                "    aaaaaaaa,\n"
12380                "    aaaaaaaa,\n"
12381                "    aaaaaaaa,\n"
12382                "    aaaaaaaa,\n"
12383                "    aaaaaaaaaa,\n"
12384                "    aaaaaaaa,\n"
12385                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa};",
12386                getLLVMStyleWithColumns(30));
12387   verifyFormat("vector<int> aaaa = {\n"
12388                "    aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
12389                "    aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
12390                "    aaaaaa.aaaaaaa,\n"
12391                "    aaaaaa.aaaaaaa,\n"
12392                "    aaaaaa.aaaaaaa,\n"
12393                "    aaaaaa.aaaaaaa,\n"
12394                "};");
12395 
12396   // Don't create hanging lists.
12397   verifyFormat("someFunction(Param, {List1, List2,\n"
12398                "                     List3});",
12399                getLLVMStyleWithColumns(35));
12400   verifyFormat("someFunction(Param, Param,\n"
12401                "             {List1, List2,\n"
12402                "              List3});",
12403                getLLVMStyleWithColumns(35));
12404   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n"
12405                "                               aaaaaaaaaaaaaaaaaaaaaaa);");
12406 }
12407 
12408 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
12409   FormatStyle DoNotMerge = getLLVMStyle();
12410   DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
12411 
12412   verifyFormat("void f() { return 42; }");
12413   verifyFormat("void f() {\n"
12414                "  return 42;\n"
12415                "}",
12416                DoNotMerge);
12417   verifyFormat("void f() {\n"
12418                "  // Comment\n"
12419                "}");
12420   verifyFormat("{\n"
12421                "#error {\n"
12422                "  int a;\n"
12423                "}");
12424   verifyFormat("{\n"
12425                "  int a;\n"
12426                "#error {\n"
12427                "}");
12428   verifyFormat("void f() {} // comment");
12429   verifyFormat("void f() { int a; } // comment");
12430   verifyFormat("void f() {\n"
12431                "} // comment",
12432                DoNotMerge);
12433   verifyFormat("void f() {\n"
12434                "  int a;\n"
12435                "} // comment",
12436                DoNotMerge);
12437   verifyFormat("void f() {\n"
12438                "} // comment",
12439                getLLVMStyleWithColumns(15));
12440 
12441   verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
12442   verifyFormat("void f() {\n  return 42;\n}", getLLVMStyleWithColumns(22));
12443 
12444   verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
12445   verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
12446   verifyFormat("class C {\n"
12447                "  C()\n"
12448                "      : iiiiiiii(nullptr),\n"
12449                "        kkkkkkk(nullptr),\n"
12450                "        mmmmmmm(nullptr),\n"
12451                "        nnnnnnn(nullptr) {}\n"
12452                "};",
12453                getGoogleStyle());
12454 
12455   FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0);
12456   EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit));
12457   EXPECT_EQ("class C {\n"
12458             "  A() : b(0) {}\n"
12459             "};",
12460             format("class C{A():b(0){}};", NoColumnLimit));
12461   EXPECT_EQ("A()\n"
12462             "    : b(0) {\n"
12463             "}",
12464             format("A()\n:b(0)\n{\n}", NoColumnLimit));
12465 
12466   FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
12467   DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
12468       FormatStyle::SFS_None;
12469   EXPECT_EQ("A()\n"
12470             "    : b(0) {\n"
12471             "}",
12472             format("A():b(0){}", DoNotMergeNoColumnLimit));
12473   EXPECT_EQ("A()\n"
12474             "    : b(0) {\n"
12475             "}",
12476             format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit));
12477 
12478   verifyFormat("#define A          \\\n"
12479                "  void f() {       \\\n"
12480                "    int i;         \\\n"
12481                "  }",
12482                getLLVMStyleWithColumns(20));
12483   verifyFormat("#define A           \\\n"
12484                "  void f() { int i; }",
12485                getLLVMStyleWithColumns(21));
12486   verifyFormat("#define A            \\\n"
12487                "  void f() {         \\\n"
12488                "    int i;           \\\n"
12489                "  }                  \\\n"
12490                "  int j;",
12491                getLLVMStyleWithColumns(22));
12492   verifyFormat("#define A             \\\n"
12493                "  void f() { int i; } \\\n"
12494                "  int j;",
12495                getLLVMStyleWithColumns(23));
12496 }
12497 
12498 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) {
12499   FormatStyle MergeEmptyOnly = getLLVMStyle();
12500   MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
12501   verifyFormat("class C {\n"
12502                "  int f() {}\n"
12503                "};",
12504                MergeEmptyOnly);
12505   verifyFormat("class C {\n"
12506                "  int f() {\n"
12507                "    return 42;\n"
12508                "  }\n"
12509                "};",
12510                MergeEmptyOnly);
12511   verifyFormat("int f() {}", MergeEmptyOnly);
12512   verifyFormat("int f() {\n"
12513                "  return 42;\n"
12514                "}",
12515                MergeEmptyOnly);
12516 
12517   // Also verify behavior when BraceWrapping.AfterFunction = true
12518   MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
12519   MergeEmptyOnly.BraceWrapping.AfterFunction = true;
12520   verifyFormat("int f() {}", MergeEmptyOnly);
12521   verifyFormat("class C {\n"
12522                "  int f() {}\n"
12523                "};",
12524                MergeEmptyOnly);
12525 }
12526 
12527 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
12528   FormatStyle MergeInlineOnly = getLLVMStyle();
12529   MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
12530   verifyFormat("class C {\n"
12531                "  int f() { return 42; }\n"
12532                "};",
12533                MergeInlineOnly);
12534   verifyFormat("int f() {\n"
12535                "  return 42;\n"
12536                "}",
12537                MergeInlineOnly);
12538 
12539   // SFS_Inline implies SFS_Empty
12540   verifyFormat("class C {\n"
12541                "  int f() {}\n"
12542                "};",
12543                MergeInlineOnly);
12544   verifyFormat("int f() {}", MergeInlineOnly);
12545 
12546   // Also verify behavior when BraceWrapping.AfterFunction = true
12547   MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
12548   MergeInlineOnly.BraceWrapping.AfterFunction = true;
12549   verifyFormat("class C {\n"
12550                "  int f() { return 42; }\n"
12551                "};",
12552                MergeInlineOnly);
12553   verifyFormat("int f()\n"
12554                "{\n"
12555                "  return 42;\n"
12556                "}",
12557                MergeInlineOnly);
12558 
12559   // SFS_Inline implies SFS_Empty
12560   verifyFormat("int f() {}", MergeInlineOnly);
12561   verifyFormat("class C {\n"
12562                "  int f() {}\n"
12563                "};",
12564                MergeInlineOnly);
12565 
12566   MergeInlineOnly.BraceWrapping.AfterClass = true;
12567   MergeInlineOnly.BraceWrapping.AfterStruct = true;
12568   verifyFormat("class C\n"
12569                "{\n"
12570                "  int f() { return 42; }\n"
12571                "};",
12572                MergeInlineOnly);
12573   verifyFormat("struct C\n"
12574                "{\n"
12575                "  int f() { return 42; }\n"
12576                "};",
12577                MergeInlineOnly);
12578   verifyFormat("int f()\n"
12579                "{\n"
12580                "  return 42;\n"
12581                "}",
12582                MergeInlineOnly);
12583   verifyFormat("int f() {}", MergeInlineOnly);
12584   verifyFormat("class C\n"
12585                "{\n"
12586                "  int f() { return 42; }\n"
12587                "};",
12588                MergeInlineOnly);
12589   verifyFormat("struct C\n"
12590                "{\n"
12591                "  int f() { return 42; }\n"
12592                "};",
12593                MergeInlineOnly);
12594   verifyFormat("struct C\n"
12595                "// comment\n"
12596                "/* comment */\n"
12597                "// comment\n"
12598                "{\n"
12599                "  int f() { return 42; }\n"
12600                "};",
12601                MergeInlineOnly);
12602   verifyFormat("/* comment */ struct C\n"
12603                "{\n"
12604                "  int f() { return 42; }\n"
12605                "};",
12606                MergeInlineOnly);
12607 }
12608 
12609 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) {
12610   FormatStyle MergeInlineOnly = getLLVMStyle();
12611   MergeInlineOnly.AllowShortFunctionsOnASingleLine =
12612       FormatStyle::SFS_InlineOnly;
12613   verifyFormat("class C {\n"
12614                "  int f() { return 42; }\n"
12615                "};",
12616                MergeInlineOnly);
12617   verifyFormat("int f() {\n"
12618                "  return 42;\n"
12619                "}",
12620                MergeInlineOnly);
12621 
12622   // SFS_InlineOnly does not imply SFS_Empty
12623   verifyFormat("class C {\n"
12624                "  int f() {}\n"
12625                "};",
12626                MergeInlineOnly);
12627   verifyFormat("int f() {\n"
12628                "}",
12629                MergeInlineOnly);
12630 
12631   // Also verify behavior when BraceWrapping.AfterFunction = true
12632   MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
12633   MergeInlineOnly.BraceWrapping.AfterFunction = true;
12634   verifyFormat("class C {\n"
12635                "  int f() { return 42; }\n"
12636                "};",
12637                MergeInlineOnly);
12638   verifyFormat("int f()\n"
12639                "{\n"
12640                "  return 42;\n"
12641                "}",
12642                MergeInlineOnly);
12643 
12644   // SFS_InlineOnly does not imply SFS_Empty
12645   verifyFormat("int f()\n"
12646                "{\n"
12647                "}",
12648                MergeInlineOnly);
12649   verifyFormat("class C {\n"
12650                "  int f() {}\n"
12651                "};",
12652                MergeInlineOnly);
12653 }
12654 
12655 TEST_F(FormatTest, SplitEmptyFunction) {
12656   FormatStyle Style = getLLVMStyleWithColumns(40);
12657   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
12658   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12659   Style.BraceWrapping.AfterFunction = true;
12660   Style.BraceWrapping.SplitEmptyFunction = false;
12661 
12662   verifyFormat("int f()\n"
12663                "{}",
12664                Style);
12665   verifyFormat("int f()\n"
12666                "{\n"
12667                "  return 42;\n"
12668                "}",
12669                Style);
12670   verifyFormat("int f()\n"
12671                "{\n"
12672                "  // some comment\n"
12673                "}",
12674                Style);
12675 
12676   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
12677   verifyFormat("int f() {}", Style);
12678   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
12679                "{}",
12680                Style);
12681   verifyFormat("int f()\n"
12682                "{\n"
12683                "  return 0;\n"
12684                "}",
12685                Style);
12686 
12687   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
12688   verifyFormat("class Foo {\n"
12689                "  int f() {}\n"
12690                "};\n",
12691                Style);
12692   verifyFormat("class Foo {\n"
12693                "  int f() { return 0; }\n"
12694                "};\n",
12695                Style);
12696   verifyFormat("class Foo {\n"
12697                "  int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
12698                "  {}\n"
12699                "};\n",
12700                Style);
12701   verifyFormat("class Foo {\n"
12702                "  int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
12703                "  {\n"
12704                "    return 0;\n"
12705                "  }\n"
12706                "};\n",
12707                Style);
12708 
12709   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
12710   verifyFormat("int f() {}", Style);
12711   verifyFormat("int f() { return 0; }", Style);
12712   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
12713                "{}",
12714                Style);
12715   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
12716                "{\n"
12717                "  return 0;\n"
12718                "}",
12719                Style);
12720 }
12721 
12722 TEST_F(FormatTest, SplitEmptyFunctionButNotRecord) {
12723   FormatStyle Style = getLLVMStyleWithColumns(40);
12724   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
12725   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12726   Style.BraceWrapping.AfterFunction = true;
12727   Style.BraceWrapping.SplitEmptyFunction = true;
12728   Style.BraceWrapping.SplitEmptyRecord = false;
12729 
12730   verifyFormat("class C {};", Style);
12731   verifyFormat("struct C {};", Style);
12732   verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
12733                "       int bbbbbbbbbbbbbbbbbbbbbbbb)\n"
12734                "{\n"
12735                "}",
12736                Style);
12737   verifyFormat("class C {\n"
12738                "  C()\n"
12739                "      : aaaaaaaaaaaaaaaaaaaaaaaaaaaa(),\n"
12740                "        bbbbbbbbbbbbbbbbbbb()\n"
12741                "  {\n"
12742                "  }\n"
12743                "  void\n"
12744                "  m(int aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
12745                "    int bbbbbbbbbbbbbbbbbbbbbbbb)\n"
12746                "  {\n"
12747                "  }\n"
12748                "};",
12749                Style);
12750 }
12751 
12752 TEST_F(FormatTest, KeepShortFunctionAfterPPElse) {
12753   FormatStyle Style = getLLVMStyle();
12754   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
12755   verifyFormat("#ifdef A\n"
12756                "int f() {}\n"
12757                "#else\n"
12758                "int g() {}\n"
12759                "#endif",
12760                Style);
12761 }
12762 
12763 TEST_F(FormatTest, SplitEmptyClass) {
12764   FormatStyle Style = getLLVMStyle();
12765   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12766   Style.BraceWrapping.AfterClass = true;
12767   Style.BraceWrapping.SplitEmptyRecord = false;
12768 
12769   verifyFormat("class Foo\n"
12770                "{};",
12771                Style);
12772   verifyFormat("/* something */ class Foo\n"
12773                "{};",
12774                Style);
12775   verifyFormat("template <typename X> class Foo\n"
12776                "{};",
12777                Style);
12778   verifyFormat("class Foo\n"
12779                "{\n"
12780                "  Foo();\n"
12781                "};",
12782                Style);
12783   verifyFormat("typedef class Foo\n"
12784                "{\n"
12785                "} Foo_t;",
12786                Style);
12787 
12788   Style.BraceWrapping.SplitEmptyRecord = true;
12789   Style.BraceWrapping.AfterStruct = true;
12790   verifyFormat("class rep\n"
12791                "{\n"
12792                "};",
12793                Style);
12794   verifyFormat("struct rep\n"
12795                "{\n"
12796                "};",
12797                Style);
12798   verifyFormat("template <typename T> class rep\n"
12799                "{\n"
12800                "};",
12801                Style);
12802   verifyFormat("template <typename T> struct rep\n"
12803                "{\n"
12804                "};",
12805                Style);
12806   verifyFormat("class rep\n"
12807                "{\n"
12808                "  int x;\n"
12809                "};",
12810                Style);
12811   verifyFormat("struct rep\n"
12812                "{\n"
12813                "  int x;\n"
12814                "};",
12815                Style);
12816   verifyFormat("template <typename T> class rep\n"
12817                "{\n"
12818                "  int x;\n"
12819                "};",
12820                Style);
12821   verifyFormat("template <typename T> struct rep\n"
12822                "{\n"
12823                "  int x;\n"
12824                "};",
12825                Style);
12826   verifyFormat("template <typename T> class rep // Foo\n"
12827                "{\n"
12828                "  int x;\n"
12829                "};",
12830                Style);
12831   verifyFormat("template <typename T> struct rep // Bar\n"
12832                "{\n"
12833                "  int x;\n"
12834                "};",
12835                Style);
12836 
12837   verifyFormat("template <typename T> class rep<T>\n"
12838                "{\n"
12839                "  int x;\n"
12840                "};",
12841                Style);
12842 
12843   verifyFormat("template <typename T> class rep<std::complex<T>>\n"
12844                "{\n"
12845                "  int x;\n"
12846                "};",
12847                Style);
12848   verifyFormat("template <typename T> class rep<std::complex<T>>\n"
12849                "{\n"
12850                "};",
12851                Style);
12852 
12853   verifyFormat("#include \"stdint.h\"\n"
12854                "namespace rep {}",
12855                Style);
12856   verifyFormat("#include <stdint.h>\n"
12857                "namespace rep {}",
12858                Style);
12859   verifyFormat("#include <stdint.h>\n"
12860                "namespace rep {}",
12861                "#include <stdint.h>\n"
12862                "namespace rep {\n"
12863                "\n"
12864                "\n"
12865                "}",
12866                Style);
12867 }
12868 
12869 TEST_F(FormatTest, SplitEmptyStruct) {
12870   FormatStyle Style = getLLVMStyle();
12871   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12872   Style.BraceWrapping.AfterStruct = true;
12873   Style.BraceWrapping.SplitEmptyRecord = false;
12874 
12875   verifyFormat("struct Foo\n"
12876                "{};",
12877                Style);
12878   verifyFormat("/* something */ struct Foo\n"
12879                "{};",
12880                Style);
12881   verifyFormat("template <typename X> struct Foo\n"
12882                "{};",
12883                Style);
12884   verifyFormat("struct Foo\n"
12885                "{\n"
12886                "  Foo();\n"
12887                "};",
12888                Style);
12889   verifyFormat("typedef struct Foo\n"
12890                "{\n"
12891                "} Foo_t;",
12892                Style);
12893   // typedef struct Bar {} Bar_t;
12894 }
12895 
12896 TEST_F(FormatTest, SplitEmptyUnion) {
12897   FormatStyle Style = getLLVMStyle();
12898   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12899   Style.BraceWrapping.AfterUnion = true;
12900   Style.BraceWrapping.SplitEmptyRecord = false;
12901 
12902   verifyFormat("union Foo\n"
12903                "{};",
12904                Style);
12905   verifyFormat("/* something */ union Foo\n"
12906                "{};",
12907                Style);
12908   verifyFormat("union Foo\n"
12909                "{\n"
12910                "  A,\n"
12911                "};",
12912                Style);
12913   verifyFormat("typedef union Foo\n"
12914                "{\n"
12915                "} Foo_t;",
12916                Style);
12917 }
12918 
12919 TEST_F(FormatTest, SplitEmptyNamespace) {
12920   FormatStyle Style = getLLVMStyle();
12921   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12922   Style.BraceWrapping.AfterNamespace = true;
12923   Style.BraceWrapping.SplitEmptyNamespace = false;
12924 
12925   verifyFormat("namespace Foo\n"
12926                "{};",
12927                Style);
12928   verifyFormat("/* something */ namespace Foo\n"
12929                "{};",
12930                Style);
12931   verifyFormat("inline namespace Foo\n"
12932                "{};",
12933                Style);
12934   verifyFormat("/* something */ inline namespace Foo\n"
12935                "{};",
12936                Style);
12937   verifyFormat("export namespace Foo\n"
12938                "{};",
12939                Style);
12940   verifyFormat("namespace Foo\n"
12941                "{\n"
12942                "void Bar();\n"
12943                "};",
12944                Style);
12945 }
12946 
12947 TEST_F(FormatTest, NeverMergeShortRecords) {
12948   FormatStyle Style = getLLVMStyle();
12949 
12950   verifyFormat("class Foo {\n"
12951                "  Foo();\n"
12952                "};",
12953                Style);
12954   verifyFormat("typedef class Foo {\n"
12955                "  Foo();\n"
12956                "} Foo_t;",
12957                Style);
12958   verifyFormat("struct Foo {\n"
12959                "  Foo();\n"
12960                "};",
12961                Style);
12962   verifyFormat("typedef struct Foo {\n"
12963                "  Foo();\n"
12964                "} Foo_t;",
12965                Style);
12966   verifyFormat("union Foo {\n"
12967                "  A,\n"
12968                "};",
12969                Style);
12970   verifyFormat("typedef union Foo {\n"
12971                "  A,\n"
12972                "} Foo_t;",
12973                Style);
12974   verifyFormat("namespace Foo {\n"
12975                "void Bar();\n"
12976                "};",
12977                Style);
12978 
12979   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12980   Style.BraceWrapping.AfterClass = true;
12981   Style.BraceWrapping.AfterStruct = true;
12982   Style.BraceWrapping.AfterUnion = true;
12983   Style.BraceWrapping.AfterNamespace = true;
12984   verifyFormat("class Foo\n"
12985                "{\n"
12986                "  Foo();\n"
12987                "};",
12988                Style);
12989   verifyFormat("typedef class Foo\n"
12990                "{\n"
12991                "  Foo();\n"
12992                "} Foo_t;",
12993                Style);
12994   verifyFormat("struct Foo\n"
12995                "{\n"
12996                "  Foo();\n"
12997                "};",
12998                Style);
12999   verifyFormat("typedef struct Foo\n"
13000                "{\n"
13001                "  Foo();\n"
13002                "} Foo_t;",
13003                Style);
13004   verifyFormat("union Foo\n"
13005                "{\n"
13006                "  A,\n"
13007                "};",
13008                Style);
13009   verifyFormat("typedef union Foo\n"
13010                "{\n"
13011                "  A,\n"
13012                "} Foo_t;",
13013                Style);
13014   verifyFormat("namespace Foo\n"
13015                "{\n"
13016                "void Bar();\n"
13017                "};",
13018                Style);
13019 }
13020 
13021 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
13022   // Elaborate type variable declarations.
13023   verifyFormat("struct foo a = {bar};\nint n;");
13024   verifyFormat("class foo a = {bar};\nint n;");
13025   verifyFormat("union foo a = {bar};\nint n;");
13026 
13027   // Elaborate types inside function definitions.
13028   verifyFormat("struct foo f() {}\nint n;");
13029   verifyFormat("class foo f() {}\nint n;");
13030   verifyFormat("union foo f() {}\nint n;");
13031 
13032   // Templates.
13033   verifyFormat("template <class X> void f() {}\nint n;");
13034   verifyFormat("template <struct X> void f() {}\nint n;");
13035   verifyFormat("template <union X> void f() {}\nint n;");
13036 
13037   // Actual definitions...
13038   verifyFormat("struct {\n} n;");
13039   verifyFormat(
13040       "template <template <class T, class Y>, class Z> class X {\n} n;");
13041   verifyFormat("union Z {\n  int n;\n} x;");
13042   verifyFormat("class MACRO Z {\n} n;");
13043   verifyFormat("class MACRO(X) Z {\n} n;");
13044   verifyFormat("class __attribute__(X) Z {\n} n;");
13045   verifyFormat("class __declspec(X) Z {\n} n;");
13046   verifyFormat("class A##B##C {\n} n;");
13047   verifyFormat("class alignas(16) Z {\n} n;");
13048   verifyFormat("class MACRO(X) alignas(16) Z {\n} n;");
13049   verifyFormat("class MACROA MACRO(X) Z {\n} n;");
13050 
13051   // Redefinition from nested context:
13052   verifyFormat("class A::B::C {\n} n;");
13053 
13054   // Template definitions.
13055   verifyFormat(
13056       "template <typename F>\n"
13057       "Matcher(const Matcher<F> &Other,\n"
13058       "        typename enable_if_c<is_base_of<F, T>::value &&\n"
13059       "                             !is_same<F, T>::value>::type * = 0)\n"
13060       "    : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
13061 
13062   // FIXME: This is still incorrectly handled at the formatter side.
13063   verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
13064   verifyFormat("int i = SomeFunction(a<b, a> b);");
13065 
13066   // FIXME:
13067   // This now gets parsed incorrectly as class definition.
13068   // verifyFormat("class A<int> f() {\n}\nint n;");
13069 
13070   // Elaborate types where incorrectly parsing the structural element would
13071   // break the indent.
13072   verifyFormat("if (true)\n"
13073                "  class X x;\n"
13074                "else\n"
13075                "  f();\n");
13076 
13077   // This is simply incomplete. Formatting is not important, but must not crash.
13078   verifyFormat("class A:");
13079 }
13080 
13081 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
13082   EXPECT_EQ("#error Leave     all         white!!!!! space* alone!\n",
13083             format("#error Leave     all         white!!!!! space* alone!\n"));
13084   EXPECT_EQ(
13085       "#warning Leave     all         white!!!!! space* alone!\n",
13086       format("#warning Leave     all         white!!!!! space* alone!\n"));
13087   EXPECT_EQ("#error 1", format("  #  error   1"));
13088   EXPECT_EQ("#warning 1", format("  #  warning 1"));
13089 }
13090 
13091 TEST_F(FormatTest, FormatHashIfExpressions) {
13092   verifyFormat("#if AAAA && BBBB");
13093   verifyFormat("#if (AAAA && BBBB)");
13094   verifyFormat("#elif (AAAA && BBBB)");
13095   // FIXME: Come up with a better indentation for #elif.
13096   verifyFormat(
13097       "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) &&  \\\n"
13098       "    defined(BBBBBBBB)\n"
13099       "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) &&  \\\n"
13100       "    defined(BBBBBBBB)\n"
13101       "#endif",
13102       getLLVMStyleWithColumns(65));
13103 }
13104 
13105 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
13106   FormatStyle AllowsMergedIf = getGoogleStyle();
13107   AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
13108       FormatStyle::SIS_WithoutElse;
13109   verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
13110   verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
13111   verifyFormat("if (true)\n#error E\n  return 42;", AllowsMergedIf);
13112   EXPECT_EQ("if (true) return 42;",
13113             format("if (true)\nreturn 42;", AllowsMergedIf));
13114   FormatStyle ShortMergedIf = AllowsMergedIf;
13115   ShortMergedIf.ColumnLimit = 25;
13116   verifyFormat("#define A \\\n"
13117                "  if (true) return 42;",
13118                ShortMergedIf);
13119   verifyFormat("#define A \\\n"
13120                "  f();    \\\n"
13121                "  if (true)\n"
13122                "#define B",
13123                ShortMergedIf);
13124   verifyFormat("#define A \\\n"
13125                "  f();    \\\n"
13126                "  if (true)\n"
13127                "g();",
13128                ShortMergedIf);
13129   verifyFormat("{\n"
13130                "#ifdef A\n"
13131                "  // Comment\n"
13132                "  if (true) continue;\n"
13133                "#endif\n"
13134                "  // Comment\n"
13135                "  if (true) continue;\n"
13136                "}",
13137                ShortMergedIf);
13138   ShortMergedIf.ColumnLimit = 33;
13139   verifyFormat("#define A \\\n"
13140                "  if constexpr (true) return 42;",
13141                ShortMergedIf);
13142   verifyFormat("#define A \\\n"
13143                "  if CONSTEXPR (true) return 42;",
13144                ShortMergedIf);
13145   ShortMergedIf.ColumnLimit = 29;
13146   verifyFormat("#define A                   \\\n"
13147                "  if (aaaaaaaaaa) return 1; \\\n"
13148                "  return 2;",
13149                ShortMergedIf);
13150   ShortMergedIf.ColumnLimit = 28;
13151   verifyFormat("#define A         \\\n"
13152                "  if (aaaaaaaaaa) \\\n"
13153                "    return 1;     \\\n"
13154                "  return 2;",
13155                ShortMergedIf);
13156   verifyFormat("#define A                \\\n"
13157                "  if constexpr (aaaaaaa) \\\n"
13158                "    return 1;            \\\n"
13159                "  return 2;",
13160                ShortMergedIf);
13161   verifyFormat("#define A                \\\n"
13162                "  if CONSTEXPR (aaaaaaa) \\\n"
13163                "    return 1;            \\\n"
13164                "  return 2;",
13165                ShortMergedIf);
13166 }
13167 
13168 TEST_F(FormatTest, FormatStarDependingOnContext) {
13169   verifyFormat("void f(int *a);");
13170   verifyFormat("void f() { f(fint * b); }");
13171   verifyFormat("class A {\n  void f(int *a);\n};");
13172   verifyFormat("class A {\n  int *a;\n};");
13173   verifyFormat("namespace a {\n"
13174                "namespace b {\n"
13175                "class A {\n"
13176                "  void f() {}\n"
13177                "  int *a;\n"
13178                "};\n"
13179                "} // namespace b\n"
13180                "} // namespace a");
13181 }
13182 
13183 TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
13184   verifyFormat("while");
13185   verifyFormat("operator");
13186 }
13187 
13188 TEST_F(FormatTest, SkipsDeeplyNestedLines) {
13189   // This code would be painfully slow to format if we didn't skip it.
13190   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
13191                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
13192                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
13193                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
13194                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
13195                    "A(1, 1)\n"
13196                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x
13197                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
13198                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
13199                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
13200                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
13201                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
13202                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
13203                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
13204                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
13205                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n");
13206   // Deeply nested part is untouched, rest is formatted.
13207   EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n",
13208             format(std::string("int    i;\n") + Code + "int    j;\n",
13209                    getLLVMStyle(), SC_ExpectIncomplete));
13210 }
13211 
13212 //===----------------------------------------------------------------------===//
13213 // Objective-C tests.
13214 //===----------------------------------------------------------------------===//
13215 
13216 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
13217   verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
13218   EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
13219             format("-(NSUInteger)indexOfObject:(id)anObject;"));
13220   EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
13221   EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
13222   EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
13223             format("-(NSInteger)Method3:(id)anObject;"));
13224   EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
13225             format("-(NSInteger)Method4:(id)anObject;"));
13226   EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
13227             format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
13228   EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
13229             format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
13230   EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject "
13231             "forAllCells:(BOOL)flag;",
13232             format("- (void)sendAction:(SEL)aSelector to:(id)anObject "
13233                    "forAllCells:(BOOL)flag;"));
13234 
13235   // Very long objectiveC method declaration.
13236   verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
13237                "    (SoooooooooooooooooooooomeType *)bbbbbbbbbb;");
13238   verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
13239                "                    inRange:(NSRange)range\n"
13240                "                   outRange:(NSRange)out_range\n"
13241                "                  outRange1:(NSRange)out_range1\n"
13242                "                  outRange2:(NSRange)out_range2\n"
13243                "                  outRange3:(NSRange)out_range3\n"
13244                "                  outRange4:(NSRange)out_range4\n"
13245                "                  outRange5:(NSRange)out_range5\n"
13246                "                  outRange6:(NSRange)out_range6\n"
13247                "                  outRange7:(NSRange)out_range7\n"
13248                "                  outRange8:(NSRange)out_range8\n"
13249                "                  outRange9:(NSRange)out_range9;");
13250 
13251   // When the function name has to be wrapped.
13252   FormatStyle Style = getLLVMStyle();
13253   // ObjC ignores IndentWrappedFunctionNames when wrapping methods
13254   // and always indents instead.
13255   Style.IndentWrappedFunctionNames = false;
13256   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
13257                "    veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
13258                "               anotherName:(NSString)bbbbbbbbbbbbbb {\n"
13259                "}",
13260                Style);
13261   Style.IndentWrappedFunctionNames = true;
13262   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
13263                "    veryLooooooooooongName:(NSString)cccccccccccccc\n"
13264                "               anotherName:(NSString)dddddddddddddd {\n"
13265                "}",
13266                Style);
13267 
13268   verifyFormat("- (int)sum:(vector<int>)numbers;");
13269   verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
13270   // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
13271   // protocol lists (but not for template classes):
13272   // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
13273 
13274   verifyFormat("- (int (*)())foo:(int (*)())f;");
13275   verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
13276 
13277   // If there's no return type (very rare in practice!), LLVM and Google style
13278   // agree.
13279   verifyFormat("- foo;");
13280   verifyFormat("- foo:(int)f;");
13281   verifyGoogleFormat("- foo:(int)foo;");
13282 }
13283 
13284 TEST_F(FormatTest, BreaksStringLiterals) {
13285   EXPECT_EQ("\"some text \"\n"
13286             "\"other\";",
13287             format("\"some text other\";", getLLVMStyleWithColumns(12)));
13288   EXPECT_EQ("\"some text \"\n"
13289             "\"other\";",
13290             format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
13291   EXPECT_EQ(
13292       "#define A  \\\n"
13293       "  \"some \"  \\\n"
13294       "  \"text \"  \\\n"
13295       "  \"other\";",
13296       format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
13297   EXPECT_EQ(
13298       "#define A  \\\n"
13299       "  \"so \"    \\\n"
13300       "  \"text \"  \\\n"
13301       "  \"other\";",
13302       format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
13303 
13304   EXPECT_EQ("\"some text\"",
13305             format("\"some text\"", getLLVMStyleWithColumns(1)));
13306   EXPECT_EQ("\"some text\"",
13307             format("\"some text\"", getLLVMStyleWithColumns(11)));
13308   EXPECT_EQ("\"some \"\n"
13309             "\"text\"",
13310             format("\"some text\"", getLLVMStyleWithColumns(10)));
13311   EXPECT_EQ("\"some \"\n"
13312             "\"text\"",
13313             format("\"some text\"", getLLVMStyleWithColumns(7)));
13314   EXPECT_EQ("\"some\"\n"
13315             "\" tex\"\n"
13316             "\"t\"",
13317             format("\"some text\"", getLLVMStyleWithColumns(6)));
13318   EXPECT_EQ("\"some\"\n"
13319             "\" tex\"\n"
13320             "\" and\"",
13321             format("\"some tex and\"", getLLVMStyleWithColumns(6)));
13322   EXPECT_EQ("\"some\"\n"
13323             "\"/tex\"\n"
13324             "\"/and\"",
13325             format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
13326 
13327   EXPECT_EQ("variable =\n"
13328             "    \"long string \"\n"
13329             "    \"literal\";",
13330             format("variable = \"long string literal\";",
13331                    getLLVMStyleWithColumns(20)));
13332 
13333   EXPECT_EQ("variable = f(\n"
13334             "    \"long string \"\n"
13335             "    \"literal\",\n"
13336             "    short,\n"
13337             "    loooooooooooooooooooong);",
13338             format("variable = f(\"long string literal\", short, "
13339                    "loooooooooooooooooooong);",
13340                    getLLVMStyleWithColumns(20)));
13341 
13342   EXPECT_EQ(
13343       "f(g(\"long string \"\n"
13344       "    \"literal\"),\n"
13345       "  b);",
13346       format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20)));
13347   EXPECT_EQ("f(g(\"long string \"\n"
13348             "    \"literal\",\n"
13349             "    a),\n"
13350             "  b);",
13351             format("f(g(\"long string literal\", a), b);",
13352                    getLLVMStyleWithColumns(20)));
13353   EXPECT_EQ(
13354       "f(\"one two\".split(\n"
13355       "    variable));",
13356       format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
13357   EXPECT_EQ("f(\"one two three four five six \"\n"
13358             "  \"seven\".split(\n"
13359             "      really_looooong_variable));",
13360             format("f(\"one two three four five six seven\"."
13361                    "split(really_looooong_variable));",
13362                    getLLVMStyleWithColumns(33)));
13363 
13364   EXPECT_EQ("f(\"some \"\n"
13365             "  \"text\",\n"
13366             "  other);",
13367             format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
13368 
13369   // Only break as a last resort.
13370   verifyFormat(
13371       "aaaaaaaaaaaaaaaaaaaa(\n"
13372       "    aaaaaaaaaaaaaaaaaaaa,\n"
13373       "    aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
13374 
13375   EXPECT_EQ("\"splitmea\"\n"
13376             "\"trandomp\"\n"
13377             "\"oint\"",
13378             format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
13379 
13380   EXPECT_EQ("\"split/\"\n"
13381             "\"pathat/\"\n"
13382             "\"slashes\"",
13383             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
13384 
13385   EXPECT_EQ("\"split/\"\n"
13386             "\"pathat/\"\n"
13387             "\"slashes\"",
13388             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
13389   EXPECT_EQ("\"split at \"\n"
13390             "\"spaces/at/\"\n"
13391             "\"slashes.at.any$\"\n"
13392             "\"non-alphanumeric%\"\n"
13393             "\"1111111111characte\"\n"
13394             "\"rs\"",
13395             format("\"split at "
13396                    "spaces/at/"
13397                    "slashes.at."
13398                    "any$non-"
13399                    "alphanumeric%"
13400                    "1111111111characte"
13401                    "rs\"",
13402                    getLLVMStyleWithColumns(20)));
13403 
13404   // Verify that splitting the strings understands
13405   // Style::AlwaysBreakBeforeMultilineStrings.
13406   EXPECT_EQ("aaaaaaaaaaaa(\n"
13407             "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
13408             "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
13409             format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa "
13410                    "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
13411                    "aaaaaaaaaaaaaaaaaaaaaa\");",
13412                    getGoogleStyle()));
13413   EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
13414             "       \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
13415             format("return \"aaaaaaaaaaaaaaaaaaaaaa "
13416                    "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
13417                    "aaaaaaaaaaaaaaaaaaaaaa\";",
13418                    getGoogleStyle()));
13419   EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
13420             "                \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
13421             format("llvm::outs() << "
13422                    "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
13423                    "aaaaaaaaaaaaaaaaaaa\";"));
13424   EXPECT_EQ("ffff(\n"
13425             "    {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
13426             "     \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
13427             format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
13428                    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
13429                    getGoogleStyle()));
13430 
13431   FormatStyle Style = getLLVMStyleWithColumns(12);
13432   Style.BreakStringLiterals = false;
13433   EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style));
13434 
13435   FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
13436   AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
13437   EXPECT_EQ("#define A \\\n"
13438             "  \"some \" \\\n"
13439             "  \"text \" \\\n"
13440             "  \"other\";",
13441             format("#define A \"some text other\";", AlignLeft));
13442 }
13443 
13444 TEST_F(FormatTest, BreaksStringLiteralsAtColumnLimit) {
13445   EXPECT_EQ("C a = \"some more \"\n"
13446             "      \"text\";",
13447             format("C a = \"some more text\";", getLLVMStyleWithColumns(18)));
13448 }
13449 
13450 TEST_F(FormatTest, FullyRemoveEmptyLines) {
13451   FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80);
13452   NoEmptyLines.MaxEmptyLinesToKeep = 0;
13453   EXPECT_EQ("int i = a(b());",
13454             format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines));
13455 }
13456 
13457 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
13458   EXPECT_EQ(
13459       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
13460       "(\n"
13461       "    \"x\t\");",
13462       format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
13463              "aaaaaaa("
13464              "\"x\t\");"));
13465 }
13466 
13467 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
13468   EXPECT_EQ(
13469       "u8\"utf8 string \"\n"
13470       "u8\"literal\";",
13471       format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
13472   EXPECT_EQ(
13473       "u\"utf16 string \"\n"
13474       "u\"literal\";",
13475       format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
13476   EXPECT_EQ(
13477       "U\"utf32 string \"\n"
13478       "U\"literal\";",
13479       format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
13480   EXPECT_EQ("L\"wide string \"\n"
13481             "L\"literal\";",
13482             format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
13483   EXPECT_EQ("@\"NSString \"\n"
13484             "@\"literal\";",
13485             format("@\"NSString literal\";", getGoogleStyleWithColumns(19)));
13486   verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26));
13487 
13488   // This input makes clang-format try to split the incomplete unicode escape
13489   // sequence, which used to lead to a crasher.
13490   verifyNoCrash(
13491       "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
13492       getLLVMStyleWithColumns(60));
13493 }
13494 
13495 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) {
13496   FormatStyle Style = getGoogleStyleWithColumns(15);
13497   EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style));
13498   EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style));
13499   EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style));
13500   EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style));
13501   EXPECT_EQ("u8R\"x(raw literal)x\";",
13502             format("u8R\"x(raw literal)x\";", Style));
13503 }
13504 
13505 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
13506   FormatStyle Style = getLLVMStyleWithColumns(20);
13507   EXPECT_EQ(
13508       "_T(\"aaaaaaaaaaaaaa\")\n"
13509       "_T(\"aaaaaaaaaaaaaa\")\n"
13510       "_T(\"aaaaaaaaaaaa\")",
13511       format("  _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
13512   EXPECT_EQ("f(x,\n"
13513             "  _T(\"aaaaaaaaaaaa\")\n"
13514             "  _T(\"aaa\"),\n"
13515             "  z);",
13516             format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style));
13517 
13518   // FIXME: Handle embedded spaces in one iteration.
13519   //  EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
13520   //            "_T(\"aaaaaaaaaaaaa\")\n"
13521   //            "_T(\"aaaaaaaaaaaaa\")\n"
13522   //            "_T(\"a\")",
13523   //            format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
13524   //                   getLLVMStyleWithColumns(20)));
13525   EXPECT_EQ(
13526       "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
13527       format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style));
13528   EXPECT_EQ("f(\n"
13529             "#if !TEST\n"
13530             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
13531             "#endif\n"
13532             ");",
13533             format("f(\n"
13534                    "#if !TEST\n"
13535                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
13536                    "#endif\n"
13537                    ");"));
13538   EXPECT_EQ("f(\n"
13539             "\n"
13540             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));",
13541             format("f(\n"
13542                    "\n"
13543                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));"));
13544   // Regression test for accessing tokens past the end of a vector in the
13545   // TokenLexer.
13546   verifyNoCrash(R"(_T(
13547 "
13548 )
13549 )");
13550 }
13551 
13552 TEST_F(FormatTest, BreaksStringLiteralOperands) {
13553   // In a function call with two operands, the second can be broken with no line
13554   // break before it.
13555   EXPECT_EQ(
13556       "func(a, \"long long \"\n"
13557       "        \"long long\");",
13558       format("func(a, \"long long long long\");", getLLVMStyleWithColumns(24)));
13559   // In a function call with three operands, the second must be broken with a
13560   // line break before it.
13561   EXPECT_EQ("func(a,\n"
13562             "     \"long long long \"\n"
13563             "     \"long\",\n"
13564             "     c);",
13565             format("func(a, \"long long long long\", c);",
13566                    getLLVMStyleWithColumns(24)));
13567   // In a function call with three operands, the third must be broken with a
13568   // line break before it.
13569   EXPECT_EQ("func(a, b,\n"
13570             "     \"long long long \"\n"
13571             "     \"long\");",
13572             format("func(a, b, \"long long long long\");",
13573                    getLLVMStyleWithColumns(24)));
13574   // In a function call with three operands, both the second and the third must
13575   // be broken with a line break before them.
13576   EXPECT_EQ("func(a,\n"
13577             "     \"long long long \"\n"
13578             "     \"long\",\n"
13579             "     \"long long long \"\n"
13580             "     \"long\");",
13581             format("func(a, \"long long long long\", \"long long long long\");",
13582                    getLLVMStyleWithColumns(24)));
13583   // In a chain of << with two operands, the second can be broken with no line
13584   // break before it.
13585   EXPECT_EQ("a << \"line line \"\n"
13586             "     \"line\";",
13587             format("a << \"line line line\";", getLLVMStyleWithColumns(20)));
13588   // In a chain of << with three operands, the second can be broken with no line
13589   // break before it.
13590   EXPECT_EQ(
13591       "abcde << \"line \"\n"
13592       "         \"line line\"\n"
13593       "      << c;",
13594       format("abcde << \"line line line\" << c;", getLLVMStyleWithColumns(20)));
13595   // In a chain of << with three operands, the third must be broken with a line
13596   // break before it.
13597   EXPECT_EQ(
13598       "a << b\n"
13599       "  << \"line line \"\n"
13600       "     \"line\";",
13601       format("a << b << \"line line line\";", getLLVMStyleWithColumns(20)));
13602   // In a chain of << with three operands, the second can be broken with no line
13603   // break before it and the third must be broken with a line break before it.
13604   EXPECT_EQ("abcd << \"line line \"\n"
13605             "        \"line\"\n"
13606             "     << \"line line \"\n"
13607             "        \"line\";",
13608             format("abcd << \"line line line\" << \"line line line\";",
13609                    getLLVMStyleWithColumns(20)));
13610   // In a chain of binary operators with two operands, the second can be broken
13611   // with no line break before it.
13612   EXPECT_EQ(
13613       "abcd + \"line line \"\n"
13614       "       \"line line\";",
13615       format("abcd + \"line line line line\";", getLLVMStyleWithColumns(20)));
13616   // In a chain of binary operators with three operands, the second must be
13617   // broken with a line break before it.
13618   EXPECT_EQ("abcd +\n"
13619             "    \"line line \"\n"
13620             "    \"line line\" +\n"
13621             "    e;",
13622             format("abcd + \"line line line line\" + e;",
13623                    getLLVMStyleWithColumns(20)));
13624   // In a function call with two operands, with AlignAfterOpenBracket enabled,
13625   // the first must be broken with a line break before it.
13626   FormatStyle Style = getLLVMStyleWithColumns(25);
13627   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
13628   EXPECT_EQ("someFunction(\n"
13629             "    \"long long long \"\n"
13630             "    \"long\",\n"
13631             "    a);",
13632             format("someFunction(\"long long long long\", a);", Style));
13633 }
13634 
13635 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
13636   EXPECT_EQ(
13637       "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
13638       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
13639       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
13640       format("aaaaaaaaaaa  =  \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
13641              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
13642              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"));
13643 }
13644 
13645 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
13646   EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);",
13647             format("f(g(R\"x(raw literal)x\",   a), b);", getGoogleStyle()));
13648   EXPECT_EQ("fffffffffff(g(R\"x(\n"
13649             "multiline raw string literal xxxxxxxxxxxxxx\n"
13650             ")x\",\n"
13651             "              a),\n"
13652             "            b);",
13653             format("fffffffffff(g(R\"x(\n"
13654                    "multiline raw string literal xxxxxxxxxxxxxx\n"
13655                    ")x\", a), b);",
13656                    getGoogleStyleWithColumns(20)));
13657   EXPECT_EQ("fffffffffff(\n"
13658             "    g(R\"x(qqq\n"
13659             "multiline raw string literal xxxxxxxxxxxxxx\n"
13660             ")x\",\n"
13661             "      a),\n"
13662             "    b);",
13663             format("fffffffffff(g(R\"x(qqq\n"
13664                    "multiline raw string literal xxxxxxxxxxxxxx\n"
13665                    ")x\", a), b);",
13666                    getGoogleStyleWithColumns(20)));
13667 
13668   EXPECT_EQ("fffffffffff(R\"x(\n"
13669             "multiline raw string literal xxxxxxxxxxxxxx\n"
13670             ")x\");",
13671             format("fffffffffff(R\"x(\n"
13672                    "multiline raw string literal xxxxxxxxxxxxxx\n"
13673                    ")x\");",
13674                    getGoogleStyleWithColumns(20)));
13675   EXPECT_EQ("fffffffffff(R\"x(\n"
13676             "multiline raw string literal xxxxxxxxxxxxxx\n"
13677             ")x\" + bbbbbb);",
13678             format("fffffffffff(R\"x(\n"
13679                    "multiline raw string literal xxxxxxxxxxxxxx\n"
13680                    ")x\" +   bbbbbb);",
13681                    getGoogleStyleWithColumns(20)));
13682   EXPECT_EQ("fffffffffff(\n"
13683             "    R\"x(\n"
13684             "multiline raw string literal xxxxxxxxxxxxxx\n"
13685             ")x\" +\n"
13686             "    bbbbbb);",
13687             format("fffffffffff(\n"
13688                    " R\"x(\n"
13689                    "multiline raw string literal xxxxxxxxxxxxxx\n"
13690                    ")x\" + bbbbbb);",
13691                    getGoogleStyleWithColumns(20)));
13692   EXPECT_EQ("fffffffffff(R\"(single line raw string)\" + bbbbbb);",
13693             format("fffffffffff(\n"
13694                    " R\"(single line raw string)\" + bbbbbb);"));
13695 }
13696 
13697 TEST_F(FormatTest, SkipsUnknownStringLiterals) {
13698   verifyFormat("string a = \"unterminated;");
13699   EXPECT_EQ("function(\"unterminated,\n"
13700             "         OtherParameter);",
13701             format("function(  \"unterminated,\n"
13702                    "    OtherParameter);"));
13703 }
13704 
13705 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
13706   FormatStyle Style = getLLVMStyle();
13707   Style.Standard = FormatStyle::LS_Cpp03;
13708   EXPECT_EQ("#define x(_a) printf(\"foo\" _a);",
13709             format("#define x(_a) printf(\"foo\"_a);", Style));
13710 }
13711 
13712 TEST_F(FormatTest, CppLexVersion) {
13713   FormatStyle Style = getLLVMStyle();
13714   // Formatting of x * y differs if x is a type.
13715   verifyFormat("void foo() { MACRO(a * b); }", Style);
13716   verifyFormat("void foo() { MACRO(int *b); }", Style);
13717 
13718   // LLVM style uses latest lexer.
13719   verifyFormat("void foo() { MACRO(char8_t *b); }", Style);
13720   Style.Standard = FormatStyle::LS_Cpp17;
13721   // But in c++17, char8_t isn't a keyword.
13722   verifyFormat("void foo() { MACRO(char8_t * b); }", Style);
13723 }
13724 
13725 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); }
13726 
13727 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
13728   EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
13729             "             \"ddeeefff\");",
13730             format("someFunction(\"aaabbbcccdddeeefff\");",
13731                    getLLVMStyleWithColumns(25)));
13732   EXPECT_EQ("someFunction1234567890(\n"
13733             "    \"aaabbbcccdddeeefff\");",
13734             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
13735                    getLLVMStyleWithColumns(26)));
13736   EXPECT_EQ("someFunction1234567890(\n"
13737             "    \"aaabbbcccdddeeeff\"\n"
13738             "    \"f\");",
13739             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
13740                    getLLVMStyleWithColumns(25)));
13741   EXPECT_EQ("someFunction1234567890(\n"
13742             "    \"aaabbbcccdddeeeff\"\n"
13743             "    \"f\");",
13744             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
13745                    getLLVMStyleWithColumns(24)));
13746   EXPECT_EQ("someFunction(\n"
13747             "    \"aaabbbcc ddde \"\n"
13748             "    \"efff\");",
13749             format("someFunction(\"aaabbbcc ddde efff\");",
13750                    getLLVMStyleWithColumns(25)));
13751   EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
13752             "             \"ddeeefff\");",
13753             format("someFunction(\"aaabbbccc ddeeefff\");",
13754                    getLLVMStyleWithColumns(25)));
13755   EXPECT_EQ("someFunction1234567890(\n"
13756             "    \"aaabb \"\n"
13757             "    \"cccdddeeefff\");",
13758             format("someFunction1234567890(\"aaabb cccdddeeefff\");",
13759                    getLLVMStyleWithColumns(25)));
13760   EXPECT_EQ("#define A          \\\n"
13761             "  string s =       \\\n"
13762             "      \"123456789\"  \\\n"
13763             "      \"0\";         \\\n"
13764             "  int i;",
13765             format("#define A string s = \"1234567890\"; int i;",
13766                    getLLVMStyleWithColumns(20)));
13767   EXPECT_EQ("someFunction(\n"
13768             "    \"aaabbbcc \"\n"
13769             "    \"dddeeefff\");",
13770             format("someFunction(\"aaabbbcc dddeeefff\");",
13771                    getLLVMStyleWithColumns(25)));
13772 }
13773 
13774 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
13775   EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3)));
13776   EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2)));
13777   EXPECT_EQ("\"test\"\n"
13778             "\"\\n\"",
13779             format("\"test\\n\"", getLLVMStyleWithColumns(7)));
13780   EXPECT_EQ("\"tes\\\\\"\n"
13781             "\"n\"",
13782             format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
13783   EXPECT_EQ("\"\\\\\\\\\"\n"
13784             "\"\\n\"",
13785             format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
13786   EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
13787   EXPECT_EQ("\"\\uff01\"\n"
13788             "\"test\"",
13789             format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
13790   EXPECT_EQ("\"\\Uff01ff02\"",
13791             format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
13792   EXPECT_EQ("\"\\x000000000001\"\n"
13793             "\"next\"",
13794             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
13795   EXPECT_EQ("\"\\x000000000001next\"",
13796             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
13797   EXPECT_EQ("\"\\x000000000001\"",
13798             format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
13799   EXPECT_EQ("\"test\"\n"
13800             "\"\\000000\"\n"
13801             "\"000001\"",
13802             format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
13803   EXPECT_EQ("\"test\\000\"\n"
13804             "\"00000000\"\n"
13805             "\"1\"",
13806             format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
13807 }
13808 
13809 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
13810   verifyFormat("void f() {\n"
13811                "  return g() {}\n"
13812                "  void h() {}");
13813   verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
13814                "g();\n"
13815                "}");
13816 }
13817 
13818 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
13819   verifyFormat(
13820       "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
13821 }
13822 
13823 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
13824   verifyFormat("class X {\n"
13825                "  void f() {\n"
13826                "  }\n"
13827                "};",
13828                getLLVMStyleWithColumns(12));
13829 }
13830 
13831 TEST_F(FormatTest, ConfigurableIndentWidth) {
13832   FormatStyle EightIndent = getLLVMStyleWithColumns(18);
13833   EightIndent.IndentWidth = 8;
13834   EightIndent.ContinuationIndentWidth = 8;
13835   verifyFormat("void f() {\n"
13836                "        someFunction();\n"
13837                "        if (true) {\n"
13838                "                f();\n"
13839                "        }\n"
13840                "}",
13841                EightIndent);
13842   verifyFormat("class X {\n"
13843                "        void f() {\n"
13844                "        }\n"
13845                "};",
13846                EightIndent);
13847   verifyFormat("int x[] = {\n"
13848                "        call(),\n"
13849                "        call()};",
13850                EightIndent);
13851 }
13852 
13853 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
13854   verifyFormat("double\n"
13855                "f();",
13856                getLLVMStyleWithColumns(8));
13857 }
13858 
13859 TEST_F(FormatTest, ConfigurableUseOfTab) {
13860   FormatStyle Tab = getLLVMStyleWithColumns(42);
13861   Tab.IndentWidth = 8;
13862   Tab.UseTab = FormatStyle::UT_Always;
13863   Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left;
13864 
13865   EXPECT_EQ("if (aaaaaaaa && // q\n"
13866             "    bb)\t\t// w\n"
13867             "\t;",
13868             format("if (aaaaaaaa &&// q\n"
13869                    "bb)// w\n"
13870                    ";",
13871                    Tab));
13872   EXPECT_EQ("if (aaa && bbb) // w\n"
13873             "\t;",
13874             format("if(aaa&&bbb)// w\n"
13875                    ";",
13876                    Tab));
13877 
13878   verifyFormat("class X {\n"
13879                "\tvoid f() {\n"
13880                "\t\tsomeFunction(parameter1,\n"
13881                "\t\t\t     parameter2);\n"
13882                "\t}\n"
13883                "};",
13884                Tab);
13885   verifyFormat("#define A                        \\\n"
13886                "\tvoid f() {               \\\n"
13887                "\t\tsomeFunction(    \\\n"
13888                "\t\t    parameter1,  \\\n"
13889                "\t\t    parameter2); \\\n"
13890                "\t}",
13891                Tab);
13892   verifyFormat("int a;\t      // x\n"
13893                "int bbbbbbbb; // x\n",
13894                Tab);
13895 
13896   Tab.TabWidth = 4;
13897   Tab.IndentWidth = 8;
13898   verifyFormat("class TabWidth4Indent8 {\n"
13899                "\t\tvoid f() {\n"
13900                "\t\t\t\tsomeFunction(parameter1,\n"
13901                "\t\t\t\t\t\t\t parameter2);\n"
13902                "\t\t}\n"
13903                "};",
13904                Tab);
13905 
13906   Tab.TabWidth = 4;
13907   Tab.IndentWidth = 4;
13908   verifyFormat("class TabWidth4Indent4 {\n"
13909                "\tvoid f() {\n"
13910                "\t\tsomeFunction(parameter1,\n"
13911                "\t\t\t\t\t parameter2);\n"
13912                "\t}\n"
13913                "};",
13914                Tab);
13915 
13916   Tab.TabWidth = 8;
13917   Tab.IndentWidth = 4;
13918   verifyFormat("class TabWidth8Indent4 {\n"
13919                "    void f() {\n"
13920                "\tsomeFunction(parameter1,\n"
13921                "\t\t     parameter2);\n"
13922                "    }\n"
13923                "};",
13924                Tab);
13925 
13926   Tab.TabWidth = 8;
13927   Tab.IndentWidth = 8;
13928   EXPECT_EQ("/*\n"
13929             "\t      a\t\tcomment\n"
13930             "\t      in multiple lines\n"
13931             "       */",
13932             format("   /*\t \t \n"
13933                    " \t \t a\t\tcomment\t \t\n"
13934                    " \t \t in multiple lines\t\n"
13935                    " \t  */",
13936                    Tab));
13937 
13938   Tab.UseTab = FormatStyle::UT_ForIndentation;
13939   verifyFormat("{\n"
13940                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13941                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13942                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13943                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13944                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13945                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13946                "};",
13947                Tab);
13948   verifyFormat("enum AA {\n"
13949                "\ta1, // Force multiple lines\n"
13950                "\ta2,\n"
13951                "\ta3\n"
13952                "};",
13953                Tab);
13954   EXPECT_EQ("if (aaaaaaaa && // q\n"
13955             "    bb)         // w\n"
13956             "\t;",
13957             format("if (aaaaaaaa &&// q\n"
13958                    "bb)// w\n"
13959                    ";",
13960                    Tab));
13961   verifyFormat("class X {\n"
13962                "\tvoid f() {\n"
13963                "\t\tsomeFunction(parameter1,\n"
13964                "\t\t             parameter2);\n"
13965                "\t}\n"
13966                "};",
13967                Tab);
13968   verifyFormat("{\n"
13969                "\tQ(\n"
13970                "\t    {\n"
13971                "\t\t    int a;\n"
13972                "\t\t    someFunction(aaaaaaaa,\n"
13973                "\t\t                 bbbbbbb);\n"
13974                "\t    },\n"
13975                "\t    p);\n"
13976                "}",
13977                Tab);
13978   EXPECT_EQ("{\n"
13979             "\t/* aaaa\n"
13980             "\t   bbbb */\n"
13981             "}",
13982             format("{\n"
13983                    "/* aaaa\n"
13984                    "   bbbb */\n"
13985                    "}",
13986                    Tab));
13987   EXPECT_EQ("{\n"
13988             "\t/*\n"
13989             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13990             "\t  bbbbbbbbbbbbb\n"
13991             "\t*/\n"
13992             "}",
13993             format("{\n"
13994                    "/*\n"
13995                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13996                    "*/\n"
13997                    "}",
13998                    Tab));
13999   EXPECT_EQ("{\n"
14000             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
14001             "\t// bbbbbbbbbbbbb\n"
14002             "}",
14003             format("{\n"
14004                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
14005                    "}",
14006                    Tab));
14007   EXPECT_EQ("{\n"
14008             "\t/*\n"
14009             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
14010             "\t  bbbbbbbbbbbbb\n"
14011             "\t*/\n"
14012             "}",
14013             format("{\n"
14014                    "\t/*\n"
14015                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
14016                    "\t*/\n"
14017                    "}",
14018                    Tab));
14019   EXPECT_EQ("{\n"
14020             "\t/*\n"
14021             "\n"
14022             "\t*/\n"
14023             "}",
14024             format("{\n"
14025                    "\t/*\n"
14026                    "\n"
14027                    "\t*/\n"
14028                    "}",
14029                    Tab));
14030   EXPECT_EQ("{\n"
14031             "\t/*\n"
14032             " asdf\n"
14033             "\t*/\n"
14034             "}",
14035             format("{\n"
14036                    "\t/*\n"
14037                    " asdf\n"
14038                    "\t*/\n"
14039                    "}",
14040                    Tab));
14041 
14042   verifyFormat("void f() {\n"
14043                "\treturn true ? aaaaaaaaaaaaaaaaaa\n"
14044                "\t            : bbbbbbbbbbbbbbbbbb\n"
14045                "}",
14046                Tab);
14047   FormatStyle TabNoBreak = Tab;
14048   TabNoBreak.BreakBeforeTernaryOperators = false;
14049   verifyFormat("void f() {\n"
14050                "\treturn true ? aaaaaaaaaaaaaaaaaa :\n"
14051                "\t              bbbbbbbbbbbbbbbbbb\n"
14052                "}",
14053                TabNoBreak);
14054   verifyFormat("void f() {\n"
14055                "\treturn true ?\n"
14056                "\t           aaaaaaaaaaaaaaaaaaaa :\n"
14057                "\t           bbbbbbbbbbbbbbbbbbbb\n"
14058                "}",
14059                TabNoBreak);
14060 
14061   Tab.UseTab = FormatStyle::UT_Never;
14062   EXPECT_EQ("/*\n"
14063             "              a\t\tcomment\n"
14064             "              in multiple lines\n"
14065             "       */",
14066             format("   /*\t \t \n"
14067                    " \t \t a\t\tcomment\t \t\n"
14068                    " \t \t in multiple lines\t\n"
14069                    " \t  */",
14070                    Tab));
14071   EXPECT_EQ("/* some\n"
14072             "   comment */",
14073             format(" \t \t /* some\n"
14074                    " \t \t    comment */",
14075                    Tab));
14076   EXPECT_EQ("int a; /* some\n"
14077             "   comment */",
14078             format(" \t \t int a; /* some\n"
14079                    " \t \t    comment */",
14080                    Tab));
14081 
14082   EXPECT_EQ("int a; /* some\n"
14083             "comment */",
14084             format(" \t \t int\ta; /* some\n"
14085                    " \t \t    comment */",
14086                    Tab));
14087   EXPECT_EQ("f(\"\t\t\"); /* some\n"
14088             "    comment */",
14089             format(" \t \t f(\"\t\t\"); /* some\n"
14090                    " \t \t    comment */",
14091                    Tab));
14092   EXPECT_EQ("{\n"
14093             "        /*\n"
14094             "         * Comment\n"
14095             "         */\n"
14096             "        int i;\n"
14097             "}",
14098             format("{\n"
14099                    "\t/*\n"
14100                    "\t * Comment\n"
14101                    "\t */\n"
14102                    "\t int i;\n"
14103                    "}",
14104                    Tab));
14105 
14106   Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
14107   Tab.TabWidth = 8;
14108   Tab.IndentWidth = 8;
14109   EXPECT_EQ("if (aaaaaaaa && // q\n"
14110             "    bb)         // w\n"
14111             "\t;",
14112             format("if (aaaaaaaa &&// q\n"
14113                    "bb)// w\n"
14114                    ";",
14115                    Tab));
14116   EXPECT_EQ("if (aaa && bbb) // w\n"
14117             "\t;",
14118             format("if(aaa&&bbb)// w\n"
14119                    ";",
14120                    Tab));
14121   verifyFormat("class X {\n"
14122                "\tvoid f() {\n"
14123                "\t\tsomeFunction(parameter1,\n"
14124                "\t\t\t     parameter2);\n"
14125                "\t}\n"
14126                "};",
14127                Tab);
14128   verifyFormat("#define A                        \\\n"
14129                "\tvoid f() {               \\\n"
14130                "\t\tsomeFunction(    \\\n"
14131                "\t\t    parameter1,  \\\n"
14132                "\t\t    parameter2); \\\n"
14133                "\t}",
14134                Tab);
14135   Tab.TabWidth = 4;
14136   Tab.IndentWidth = 8;
14137   verifyFormat("class TabWidth4Indent8 {\n"
14138                "\t\tvoid f() {\n"
14139                "\t\t\t\tsomeFunction(parameter1,\n"
14140                "\t\t\t\t\t\t\t parameter2);\n"
14141                "\t\t}\n"
14142                "};",
14143                Tab);
14144   Tab.TabWidth = 4;
14145   Tab.IndentWidth = 4;
14146   verifyFormat("class TabWidth4Indent4 {\n"
14147                "\tvoid f() {\n"
14148                "\t\tsomeFunction(parameter1,\n"
14149                "\t\t\t\t\t parameter2);\n"
14150                "\t}\n"
14151                "};",
14152                Tab);
14153   Tab.TabWidth = 8;
14154   Tab.IndentWidth = 4;
14155   verifyFormat("class TabWidth8Indent4 {\n"
14156                "    void f() {\n"
14157                "\tsomeFunction(parameter1,\n"
14158                "\t\t     parameter2);\n"
14159                "    }\n"
14160                "};",
14161                Tab);
14162   Tab.TabWidth = 8;
14163   Tab.IndentWidth = 8;
14164   EXPECT_EQ("/*\n"
14165             "\t      a\t\tcomment\n"
14166             "\t      in multiple lines\n"
14167             "       */",
14168             format("   /*\t \t \n"
14169                    " \t \t a\t\tcomment\t \t\n"
14170                    " \t \t in multiple lines\t\n"
14171                    " \t  */",
14172                    Tab));
14173   verifyFormat("{\n"
14174                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
14175                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
14176                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
14177                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
14178                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
14179                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
14180                "};",
14181                Tab);
14182   verifyFormat("enum AA {\n"
14183                "\ta1, // Force multiple lines\n"
14184                "\ta2,\n"
14185                "\ta3\n"
14186                "};",
14187                Tab);
14188   EXPECT_EQ("if (aaaaaaaa && // q\n"
14189             "    bb)         // w\n"
14190             "\t;",
14191             format("if (aaaaaaaa &&// q\n"
14192                    "bb)// w\n"
14193                    ";",
14194                    Tab));
14195   verifyFormat("class X {\n"
14196                "\tvoid f() {\n"
14197                "\t\tsomeFunction(parameter1,\n"
14198                "\t\t\t     parameter2);\n"
14199                "\t}\n"
14200                "};",
14201                Tab);
14202   verifyFormat("{\n"
14203                "\tQ(\n"
14204                "\t    {\n"
14205                "\t\t    int a;\n"
14206                "\t\t    someFunction(aaaaaaaa,\n"
14207                "\t\t\t\t bbbbbbb);\n"
14208                "\t    },\n"
14209                "\t    p);\n"
14210                "}",
14211                Tab);
14212   EXPECT_EQ("{\n"
14213             "\t/* aaaa\n"
14214             "\t   bbbb */\n"
14215             "}",
14216             format("{\n"
14217                    "/* aaaa\n"
14218                    "   bbbb */\n"
14219                    "}",
14220                    Tab));
14221   EXPECT_EQ("{\n"
14222             "\t/*\n"
14223             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
14224             "\t  bbbbbbbbbbbbb\n"
14225             "\t*/\n"
14226             "}",
14227             format("{\n"
14228                    "/*\n"
14229                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
14230                    "*/\n"
14231                    "}",
14232                    Tab));
14233   EXPECT_EQ("{\n"
14234             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
14235             "\t// bbbbbbbbbbbbb\n"
14236             "}",
14237             format("{\n"
14238                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
14239                    "}",
14240                    Tab));
14241   EXPECT_EQ("{\n"
14242             "\t/*\n"
14243             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
14244             "\t  bbbbbbbbbbbbb\n"
14245             "\t*/\n"
14246             "}",
14247             format("{\n"
14248                    "\t/*\n"
14249                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
14250                    "\t*/\n"
14251                    "}",
14252                    Tab));
14253   EXPECT_EQ("{\n"
14254             "\t/*\n"
14255             "\n"
14256             "\t*/\n"
14257             "}",
14258             format("{\n"
14259                    "\t/*\n"
14260                    "\n"
14261                    "\t*/\n"
14262                    "}",
14263                    Tab));
14264   EXPECT_EQ("{\n"
14265             "\t/*\n"
14266             " asdf\n"
14267             "\t*/\n"
14268             "}",
14269             format("{\n"
14270                    "\t/*\n"
14271                    " asdf\n"
14272                    "\t*/\n"
14273                    "}",
14274                    Tab));
14275   EXPECT_EQ("/* some\n"
14276             "   comment */",
14277             format(" \t \t /* some\n"
14278                    " \t \t    comment */",
14279                    Tab));
14280   EXPECT_EQ("int a; /* some\n"
14281             "   comment */",
14282             format(" \t \t int a; /* some\n"
14283                    " \t \t    comment */",
14284                    Tab));
14285   EXPECT_EQ("int a; /* some\n"
14286             "comment */",
14287             format(" \t \t int\ta; /* some\n"
14288                    " \t \t    comment */",
14289                    Tab));
14290   EXPECT_EQ("f(\"\t\t\"); /* some\n"
14291             "    comment */",
14292             format(" \t \t f(\"\t\t\"); /* some\n"
14293                    " \t \t    comment */",
14294                    Tab));
14295   EXPECT_EQ("{\n"
14296             "\t/*\n"
14297             "\t * Comment\n"
14298             "\t */\n"
14299             "\tint i;\n"
14300             "}",
14301             format("{\n"
14302                    "\t/*\n"
14303                    "\t * Comment\n"
14304                    "\t */\n"
14305                    "\t int i;\n"
14306                    "}",
14307                    Tab));
14308   Tab.TabWidth = 2;
14309   Tab.IndentWidth = 2;
14310   EXPECT_EQ("{\n"
14311             "\t/* aaaa\n"
14312             "\t\t bbbb */\n"
14313             "}",
14314             format("{\n"
14315                    "/* aaaa\n"
14316                    "\t bbbb */\n"
14317                    "}",
14318                    Tab));
14319   EXPECT_EQ("{\n"
14320             "\t/*\n"
14321             "\t\taaaaaaaaaaaaaaaaaaaaaaaaaa\n"
14322             "\t\tbbbbbbbbbbbbb\n"
14323             "\t*/\n"
14324             "}",
14325             format("{\n"
14326                    "/*\n"
14327                    "\taaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
14328                    "*/\n"
14329                    "}",
14330                    Tab));
14331   Tab.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
14332   Tab.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
14333   Tab.TabWidth = 4;
14334   Tab.IndentWidth = 4;
14335   verifyFormat("class Assign {\n"
14336                "\tvoid f() {\n"
14337                "\t\tint         x      = 123;\n"
14338                "\t\tint         random = 4;\n"
14339                "\t\tstd::string alphabet =\n"
14340                "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
14341                "\t}\n"
14342                "};",
14343                Tab);
14344 
14345   Tab.UseTab = FormatStyle::UT_AlignWithSpaces;
14346   Tab.TabWidth = 8;
14347   Tab.IndentWidth = 8;
14348   EXPECT_EQ("if (aaaaaaaa && // q\n"
14349             "    bb)         // w\n"
14350             "\t;",
14351             format("if (aaaaaaaa &&// q\n"
14352                    "bb)// w\n"
14353                    ";",
14354                    Tab));
14355   EXPECT_EQ("if (aaa && bbb) // w\n"
14356             "\t;",
14357             format("if(aaa&&bbb)// w\n"
14358                    ";",
14359                    Tab));
14360   verifyFormat("class X {\n"
14361                "\tvoid f() {\n"
14362                "\t\tsomeFunction(parameter1,\n"
14363                "\t\t             parameter2);\n"
14364                "\t}\n"
14365                "};",
14366                Tab);
14367   verifyFormat("#define A                        \\\n"
14368                "\tvoid f() {               \\\n"
14369                "\t\tsomeFunction(    \\\n"
14370                "\t\t    parameter1,  \\\n"
14371                "\t\t    parameter2); \\\n"
14372                "\t}",
14373                Tab);
14374   Tab.TabWidth = 4;
14375   Tab.IndentWidth = 8;
14376   verifyFormat("class TabWidth4Indent8 {\n"
14377                "\t\tvoid f() {\n"
14378                "\t\t\t\tsomeFunction(parameter1,\n"
14379                "\t\t\t\t             parameter2);\n"
14380                "\t\t}\n"
14381                "};",
14382                Tab);
14383   Tab.TabWidth = 4;
14384   Tab.IndentWidth = 4;
14385   verifyFormat("class TabWidth4Indent4 {\n"
14386                "\tvoid f() {\n"
14387                "\t\tsomeFunction(parameter1,\n"
14388                "\t\t             parameter2);\n"
14389                "\t}\n"
14390                "};",
14391                Tab);
14392   Tab.TabWidth = 8;
14393   Tab.IndentWidth = 4;
14394   verifyFormat("class TabWidth8Indent4 {\n"
14395                "    void f() {\n"
14396                "\tsomeFunction(parameter1,\n"
14397                "\t             parameter2);\n"
14398                "    }\n"
14399                "};",
14400                Tab);
14401   Tab.TabWidth = 8;
14402   Tab.IndentWidth = 8;
14403   EXPECT_EQ("/*\n"
14404             "              a\t\tcomment\n"
14405             "              in multiple lines\n"
14406             "       */",
14407             format("   /*\t \t \n"
14408                    " \t \t a\t\tcomment\t \t\n"
14409                    " \t \t in multiple lines\t\n"
14410                    " \t  */",
14411                    Tab));
14412   verifyFormat("{\n"
14413                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
14414                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
14415                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
14416                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
14417                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
14418                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
14419                "};",
14420                Tab);
14421   verifyFormat("enum AA {\n"
14422                "\ta1, // Force multiple lines\n"
14423                "\ta2,\n"
14424                "\ta3\n"
14425                "};",
14426                Tab);
14427   EXPECT_EQ("if (aaaaaaaa && // q\n"
14428             "    bb)         // w\n"
14429             "\t;",
14430             format("if (aaaaaaaa &&// q\n"
14431                    "bb)// w\n"
14432                    ";",
14433                    Tab));
14434   verifyFormat("class X {\n"
14435                "\tvoid f() {\n"
14436                "\t\tsomeFunction(parameter1,\n"
14437                "\t\t             parameter2);\n"
14438                "\t}\n"
14439                "};",
14440                Tab);
14441   verifyFormat("{\n"
14442                "\tQ(\n"
14443                "\t    {\n"
14444                "\t\t    int a;\n"
14445                "\t\t    someFunction(aaaaaaaa,\n"
14446                "\t\t                 bbbbbbb);\n"
14447                "\t    },\n"
14448                "\t    p);\n"
14449                "}",
14450                Tab);
14451   EXPECT_EQ("{\n"
14452             "\t/* aaaa\n"
14453             "\t   bbbb */\n"
14454             "}",
14455             format("{\n"
14456                    "/* aaaa\n"
14457                    "   bbbb */\n"
14458                    "}",
14459                    Tab));
14460   EXPECT_EQ("{\n"
14461             "\t/*\n"
14462             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
14463             "\t  bbbbbbbbbbbbb\n"
14464             "\t*/\n"
14465             "}",
14466             format("{\n"
14467                    "/*\n"
14468                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
14469                    "*/\n"
14470                    "}",
14471                    Tab));
14472   EXPECT_EQ("{\n"
14473             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
14474             "\t// bbbbbbbbbbbbb\n"
14475             "}",
14476             format("{\n"
14477                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
14478                    "}",
14479                    Tab));
14480   EXPECT_EQ("{\n"
14481             "\t/*\n"
14482             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
14483             "\t  bbbbbbbbbbbbb\n"
14484             "\t*/\n"
14485             "}",
14486             format("{\n"
14487                    "\t/*\n"
14488                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
14489                    "\t*/\n"
14490                    "}",
14491                    Tab));
14492   EXPECT_EQ("{\n"
14493             "\t/*\n"
14494             "\n"
14495             "\t*/\n"
14496             "}",
14497             format("{\n"
14498                    "\t/*\n"
14499                    "\n"
14500                    "\t*/\n"
14501                    "}",
14502                    Tab));
14503   EXPECT_EQ("{\n"
14504             "\t/*\n"
14505             " asdf\n"
14506             "\t*/\n"
14507             "}",
14508             format("{\n"
14509                    "\t/*\n"
14510                    " asdf\n"
14511                    "\t*/\n"
14512                    "}",
14513                    Tab));
14514   EXPECT_EQ("/* some\n"
14515             "   comment */",
14516             format(" \t \t /* some\n"
14517                    " \t \t    comment */",
14518                    Tab));
14519   EXPECT_EQ("int a; /* some\n"
14520             "   comment */",
14521             format(" \t \t int a; /* some\n"
14522                    " \t \t    comment */",
14523                    Tab));
14524   EXPECT_EQ("int a; /* some\n"
14525             "comment */",
14526             format(" \t \t int\ta; /* some\n"
14527                    " \t \t    comment */",
14528                    Tab));
14529   EXPECT_EQ("f(\"\t\t\"); /* some\n"
14530             "    comment */",
14531             format(" \t \t f(\"\t\t\"); /* some\n"
14532                    " \t \t    comment */",
14533                    Tab));
14534   EXPECT_EQ("{\n"
14535             "\t/*\n"
14536             "\t * Comment\n"
14537             "\t */\n"
14538             "\tint i;\n"
14539             "}",
14540             format("{\n"
14541                    "\t/*\n"
14542                    "\t * Comment\n"
14543                    "\t */\n"
14544                    "\t int i;\n"
14545                    "}",
14546                    Tab));
14547   Tab.TabWidth = 2;
14548   Tab.IndentWidth = 2;
14549   EXPECT_EQ("{\n"
14550             "\t/* aaaa\n"
14551             "\t   bbbb */\n"
14552             "}",
14553             format("{\n"
14554                    "/* aaaa\n"
14555                    "   bbbb */\n"
14556                    "}",
14557                    Tab));
14558   EXPECT_EQ("{\n"
14559             "\t/*\n"
14560             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
14561             "\t  bbbbbbbbbbbbb\n"
14562             "\t*/\n"
14563             "}",
14564             format("{\n"
14565                    "/*\n"
14566                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
14567                    "*/\n"
14568                    "}",
14569                    Tab));
14570   Tab.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
14571   Tab.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
14572   Tab.TabWidth = 4;
14573   Tab.IndentWidth = 4;
14574   verifyFormat("class Assign {\n"
14575                "\tvoid f() {\n"
14576                "\t\tint         x      = 123;\n"
14577                "\t\tint         random = 4;\n"
14578                "\t\tstd::string alphabet =\n"
14579                "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
14580                "\t}\n"
14581                "};",
14582                Tab);
14583   Tab.AlignOperands = FormatStyle::OAS_Align;
14584   verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb +\n"
14585                "                 cccccccccccccccccccc;",
14586                Tab);
14587   // no alignment
14588   verifyFormat("int aaaaaaaaaa =\n"
14589                "\tbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
14590                Tab);
14591   verifyFormat("return aaaaaaaaaaaaaaaa ? 111111111111111\n"
14592                "       : bbbbbbbbbbbbbb ? 222222222222222\n"
14593                "                        : 333333333333333;",
14594                Tab);
14595   Tab.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
14596   Tab.AlignOperands = FormatStyle::OAS_AlignAfterOperator;
14597   verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb\n"
14598                "               + cccccccccccccccccccc;",
14599                Tab);
14600 }
14601 
14602 TEST_F(FormatTest, ZeroTabWidth) {
14603   FormatStyle Tab = getLLVMStyleWithColumns(42);
14604   Tab.IndentWidth = 8;
14605   Tab.UseTab = FormatStyle::UT_Never;
14606   Tab.TabWidth = 0;
14607   EXPECT_EQ("void a(){\n"
14608             "    // line starts with '\t'\n"
14609             "};",
14610             format("void a(){\n"
14611                    "\t// line starts with '\t'\n"
14612                    "};",
14613                    Tab));
14614 
14615   EXPECT_EQ("void a(){\n"
14616             "    // line starts with '\t'\n"
14617             "};",
14618             format("void a(){\n"
14619                    "\t\t// line starts with '\t'\n"
14620                    "};",
14621                    Tab));
14622 
14623   Tab.UseTab = FormatStyle::UT_ForIndentation;
14624   EXPECT_EQ("void a(){\n"
14625             "    // line starts with '\t'\n"
14626             "};",
14627             format("void a(){\n"
14628                    "\t// line starts with '\t'\n"
14629                    "};",
14630                    Tab));
14631 
14632   EXPECT_EQ("void a(){\n"
14633             "    // line starts with '\t'\n"
14634             "};",
14635             format("void a(){\n"
14636                    "\t\t// line starts with '\t'\n"
14637                    "};",
14638                    Tab));
14639 
14640   Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
14641   EXPECT_EQ("void a(){\n"
14642             "    // line starts with '\t'\n"
14643             "};",
14644             format("void a(){\n"
14645                    "\t// line starts with '\t'\n"
14646                    "};",
14647                    Tab));
14648 
14649   EXPECT_EQ("void a(){\n"
14650             "    // line starts with '\t'\n"
14651             "};",
14652             format("void a(){\n"
14653                    "\t\t// line starts with '\t'\n"
14654                    "};",
14655                    Tab));
14656 
14657   Tab.UseTab = FormatStyle::UT_AlignWithSpaces;
14658   EXPECT_EQ("void a(){\n"
14659             "    // line starts with '\t'\n"
14660             "};",
14661             format("void a(){\n"
14662                    "\t// line starts with '\t'\n"
14663                    "};",
14664                    Tab));
14665 
14666   EXPECT_EQ("void a(){\n"
14667             "    // line starts with '\t'\n"
14668             "};",
14669             format("void a(){\n"
14670                    "\t\t// line starts with '\t'\n"
14671                    "};",
14672                    Tab));
14673 
14674   Tab.UseTab = FormatStyle::UT_Always;
14675   EXPECT_EQ("void a(){\n"
14676             "// line starts with '\t'\n"
14677             "};",
14678             format("void a(){\n"
14679                    "\t// line starts with '\t'\n"
14680                    "};",
14681                    Tab));
14682 
14683   EXPECT_EQ("void a(){\n"
14684             "// line starts with '\t'\n"
14685             "};",
14686             format("void a(){\n"
14687                    "\t\t// line starts with '\t'\n"
14688                    "};",
14689                    Tab));
14690 }
14691 
14692 TEST_F(FormatTest, CalculatesOriginalColumn) {
14693   EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
14694             "q\"; /* some\n"
14695             "       comment */",
14696             format("  \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
14697                    "q\"; /* some\n"
14698                    "       comment */",
14699                    getLLVMStyle()));
14700   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
14701             "/* some\n"
14702             "   comment */",
14703             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
14704                    " /* some\n"
14705                    "    comment */",
14706                    getLLVMStyle()));
14707   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
14708             "qqq\n"
14709             "/* some\n"
14710             "   comment */",
14711             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
14712                    "qqq\n"
14713                    " /* some\n"
14714                    "    comment */",
14715                    getLLVMStyle()));
14716   EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
14717             "wwww; /* some\n"
14718             "         comment */",
14719             format("  inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
14720                    "wwww; /* some\n"
14721                    "         comment */",
14722                    getLLVMStyle()));
14723 }
14724 
14725 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
14726   FormatStyle NoSpace = getLLVMStyle();
14727   NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
14728 
14729   verifyFormat("while(true)\n"
14730                "  continue;",
14731                NoSpace);
14732   verifyFormat("for(;;)\n"
14733                "  continue;",
14734                NoSpace);
14735   verifyFormat("if(true)\n"
14736                "  f();\n"
14737                "else if(true)\n"
14738                "  f();",
14739                NoSpace);
14740   verifyFormat("do {\n"
14741                "  do_something();\n"
14742                "} while(something());",
14743                NoSpace);
14744   verifyFormat("switch(x) {\n"
14745                "default:\n"
14746                "  break;\n"
14747                "}",
14748                NoSpace);
14749   verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
14750   verifyFormat("size_t x = sizeof(x);", NoSpace);
14751   verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
14752   verifyFormat("auto f(int x) -> typeof(x);", NoSpace);
14753   verifyFormat("auto f(int x) -> _Atomic(x);", NoSpace);
14754   verifyFormat("auto f(int x) -> __underlying_type(x);", NoSpace);
14755   verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
14756   verifyFormat("alignas(128) char a[128];", NoSpace);
14757   verifyFormat("size_t x = alignof(MyType);", NoSpace);
14758   verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
14759   verifyFormat("int f() throw(Deprecated);", NoSpace);
14760   verifyFormat("typedef void (*cb)(int);", NoSpace);
14761   verifyFormat("T A::operator()();", NoSpace);
14762   verifyFormat("X A::operator++(T);", NoSpace);
14763   verifyFormat("auto lambda = []() { return 0; };", NoSpace);
14764 
14765   FormatStyle Space = getLLVMStyle();
14766   Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
14767 
14768   verifyFormat("int f ();", Space);
14769   verifyFormat("void f (int a, T b) {\n"
14770                "  while (true)\n"
14771                "    continue;\n"
14772                "}",
14773                Space);
14774   verifyFormat("if (true)\n"
14775                "  f ();\n"
14776                "else if (true)\n"
14777                "  f ();",
14778                Space);
14779   verifyFormat("do {\n"
14780                "  do_something ();\n"
14781                "} while (something ());",
14782                Space);
14783   verifyFormat("switch (x) {\n"
14784                "default:\n"
14785                "  break;\n"
14786                "}",
14787                Space);
14788   verifyFormat("A::A () : a (1) {}", Space);
14789   verifyFormat("void f () __attribute__ ((asdf));", Space);
14790   verifyFormat("*(&a + 1);\n"
14791                "&((&a)[1]);\n"
14792                "a[(b + c) * d];\n"
14793                "(((a + 1) * 2) + 3) * 4;",
14794                Space);
14795   verifyFormat("#define A(x) x", Space);
14796   verifyFormat("#define A (x) x", Space);
14797   verifyFormat("#if defined(x)\n"
14798                "#endif",
14799                Space);
14800   verifyFormat("auto i = std::make_unique<int> (5);", Space);
14801   verifyFormat("size_t x = sizeof (x);", Space);
14802   verifyFormat("auto f (int x) -> decltype (x);", Space);
14803   verifyFormat("auto f (int x) -> typeof (x);", Space);
14804   verifyFormat("auto f (int x) -> _Atomic (x);", Space);
14805   verifyFormat("auto f (int x) -> __underlying_type (x);", Space);
14806   verifyFormat("int f (T x) noexcept (x.create ());", Space);
14807   verifyFormat("alignas (128) char a[128];", Space);
14808   verifyFormat("size_t x = alignof (MyType);", Space);
14809   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
14810   verifyFormat("int f () throw (Deprecated);", Space);
14811   verifyFormat("typedef void (*cb) (int);", Space);
14812   // FIXME these tests regressed behaviour.
14813   // verifyFormat("T A::operator() ();", Space);
14814   // verifyFormat("X A::operator++ (T);", Space);
14815   verifyFormat("auto lambda = [] () { return 0; };", Space);
14816   verifyFormat("int x = int (y);", Space);
14817 
14818   FormatStyle SomeSpace = getLLVMStyle();
14819   SomeSpace.SpaceBeforeParens = FormatStyle::SBPO_NonEmptyParentheses;
14820 
14821   verifyFormat("[]() -> float {}", SomeSpace);
14822   verifyFormat("[] (auto foo) {}", SomeSpace);
14823   verifyFormat("[foo]() -> int {}", SomeSpace);
14824   verifyFormat("int f();", SomeSpace);
14825   verifyFormat("void f (int a, T b) {\n"
14826                "  while (true)\n"
14827                "    continue;\n"
14828                "}",
14829                SomeSpace);
14830   verifyFormat("if (true)\n"
14831                "  f();\n"
14832                "else if (true)\n"
14833                "  f();",
14834                SomeSpace);
14835   verifyFormat("do {\n"
14836                "  do_something();\n"
14837                "} while (something());",
14838                SomeSpace);
14839   verifyFormat("switch (x) {\n"
14840                "default:\n"
14841                "  break;\n"
14842                "}",
14843                SomeSpace);
14844   verifyFormat("A::A() : a (1) {}", SomeSpace);
14845   verifyFormat("void f() __attribute__ ((asdf));", SomeSpace);
14846   verifyFormat("*(&a + 1);\n"
14847                "&((&a)[1]);\n"
14848                "a[(b + c) * d];\n"
14849                "(((a + 1) * 2) + 3) * 4;",
14850                SomeSpace);
14851   verifyFormat("#define A(x) x", SomeSpace);
14852   verifyFormat("#define A (x) x", SomeSpace);
14853   verifyFormat("#if defined(x)\n"
14854                "#endif",
14855                SomeSpace);
14856   verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace);
14857   verifyFormat("size_t x = sizeof (x);", SomeSpace);
14858   verifyFormat("auto f (int x) -> decltype (x);", SomeSpace);
14859   verifyFormat("auto f (int x) -> typeof (x);", SomeSpace);
14860   verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace);
14861   verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace);
14862   verifyFormat("int f (T x) noexcept (x.create());", SomeSpace);
14863   verifyFormat("alignas (128) char a[128];", SomeSpace);
14864   verifyFormat("size_t x = alignof (MyType);", SomeSpace);
14865   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");",
14866                SomeSpace);
14867   verifyFormat("int f() throw (Deprecated);", SomeSpace);
14868   verifyFormat("typedef void (*cb) (int);", SomeSpace);
14869   verifyFormat("T A::operator()();", SomeSpace);
14870   // FIXME these tests regressed behaviour.
14871   // verifyFormat("X A::operator++ (T);", SomeSpace);
14872   verifyFormat("int x = int (y);", SomeSpace);
14873   verifyFormat("auto lambda = []() { return 0; };", SomeSpace);
14874 
14875   FormatStyle SpaceControlStatements = getLLVMStyle();
14876   SpaceControlStatements.SpaceBeforeParens = FormatStyle::SBPO_Custom;
14877   SpaceControlStatements.SpaceBeforeParensOptions.AfterControlStatements = true;
14878 
14879   verifyFormat("while (true)\n"
14880                "  continue;",
14881                SpaceControlStatements);
14882   verifyFormat("if (true)\n"
14883                "  f();\n"
14884                "else if (true)\n"
14885                "  f();",
14886                SpaceControlStatements);
14887   verifyFormat("for (;;) {\n"
14888                "  do_something();\n"
14889                "}",
14890                SpaceControlStatements);
14891   verifyFormat("do {\n"
14892                "  do_something();\n"
14893                "} while (something());",
14894                SpaceControlStatements);
14895   verifyFormat("switch (x) {\n"
14896                "default:\n"
14897                "  break;\n"
14898                "}",
14899                SpaceControlStatements);
14900 
14901   FormatStyle SpaceFuncDecl = getLLVMStyle();
14902   SpaceFuncDecl.SpaceBeforeParens = FormatStyle::SBPO_Custom;
14903   SpaceFuncDecl.SpaceBeforeParensOptions.AfterFunctionDeclarationName = true;
14904 
14905   verifyFormat("int f ();", SpaceFuncDecl);
14906   verifyFormat("void f(int a, T b) {}", SpaceFuncDecl);
14907   verifyFormat("A::A() : a(1) {}", SpaceFuncDecl);
14908   verifyFormat("void f () __attribute__((asdf));", SpaceFuncDecl);
14909   verifyFormat("#define A(x) x", SpaceFuncDecl);
14910   verifyFormat("#define A (x) x", SpaceFuncDecl);
14911   verifyFormat("#if defined(x)\n"
14912                "#endif",
14913                SpaceFuncDecl);
14914   verifyFormat("auto i = std::make_unique<int>(5);", SpaceFuncDecl);
14915   verifyFormat("size_t x = sizeof(x);", SpaceFuncDecl);
14916   verifyFormat("auto f (int x) -> decltype(x);", SpaceFuncDecl);
14917   verifyFormat("auto f (int x) -> typeof(x);", SpaceFuncDecl);
14918   verifyFormat("auto f (int x) -> _Atomic(x);", SpaceFuncDecl);
14919   verifyFormat("auto f (int x) -> __underlying_type(x);", SpaceFuncDecl);
14920   verifyFormat("int f (T x) noexcept(x.create());", SpaceFuncDecl);
14921   verifyFormat("alignas(128) char a[128];", SpaceFuncDecl);
14922   verifyFormat("size_t x = alignof(MyType);", SpaceFuncDecl);
14923   verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");",
14924                SpaceFuncDecl);
14925   verifyFormat("int f () throw(Deprecated);", SpaceFuncDecl);
14926   verifyFormat("typedef void (*cb)(int);", SpaceFuncDecl);
14927   // FIXME these tests regressed behaviour.
14928   // verifyFormat("T A::operator() ();", SpaceFuncDecl);
14929   // verifyFormat("X A::operator++ (T);", SpaceFuncDecl);
14930   verifyFormat("T A::operator()() {}", SpaceFuncDecl);
14931   verifyFormat("auto lambda = []() { return 0; };", SpaceFuncDecl);
14932   verifyFormat("int x = int(y);", SpaceFuncDecl);
14933   verifyFormat("M(std::size_t R, std::size_t C) : C(C), data(R) {}",
14934                SpaceFuncDecl);
14935 
14936   FormatStyle SpaceFuncDef = getLLVMStyle();
14937   SpaceFuncDef.SpaceBeforeParens = FormatStyle::SBPO_Custom;
14938   SpaceFuncDef.SpaceBeforeParensOptions.AfterFunctionDefinitionName = true;
14939 
14940   verifyFormat("int f();", SpaceFuncDef);
14941   verifyFormat("void f (int a, T b) {}", SpaceFuncDef);
14942   verifyFormat("A::A() : a(1) {}", SpaceFuncDef);
14943   verifyFormat("void f() __attribute__((asdf));", SpaceFuncDef);
14944   verifyFormat("#define A(x) x", SpaceFuncDef);
14945   verifyFormat("#define A (x) x", SpaceFuncDef);
14946   verifyFormat("#if defined(x)\n"
14947                "#endif",
14948                SpaceFuncDef);
14949   verifyFormat("auto i = std::make_unique<int>(5);", SpaceFuncDef);
14950   verifyFormat("size_t x = sizeof(x);", SpaceFuncDef);
14951   verifyFormat("auto f(int x) -> decltype(x);", SpaceFuncDef);
14952   verifyFormat("auto f(int x) -> typeof(x);", SpaceFuncDef);
14953   verifyFormat("auto f(int x) -> _Atomic(x);", SpaceFuncDef);
14954   verifyFormat("auto f(int x) -> __underlying_type(x);", SpaceFuncDef);
14955   verifyFormat("int f(T x) noexcept(x.create());", SpaceFuncDef);
14956   verifyFormat("alignas(128) char a[128];", SpaceFuncDef);
14957   verifyFormat("size_t x = alignof(MyType);", SpaceFuncDef);
14958   verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");",
14959                SpaceFuncDef);
14960   verifyFormat("int f() throw(Deprecated);", SpaceFuncDef);
14961   verifyFormat("typedef void (*cb)(int);", SpaceFuncDef);
14962   verifyFormat("T A::operator()();", SpaceFuncDef);
14963   verifyFormat("X A::operator++(T);", SpaceFuncDef);
14964   // verifyFormat("T A::operator() () {}", SpaceFuncDef);
14965   verifyFormat("auto lambda = [] () { return 0; };", SpaceFuncDef);
14966   verifyFormat("int x = int(y);", SpaceFuncDef);
14967   verifyFormat("M(std::size_t R, std::size_t C) : C(C), data(R) {}",
14968                SpaceFuncDef);
14969 
14970   FormatStyle SpaceIfMacros = getLLVMStyle();
14971   SpaceIfMacros.IfMacros.clear();
14972   SpaceIfMacros.IfMacros.push_back("MYIF");
14973   SpaceIfMacros.SpaceBeforeParens = FormatStyle::SBPO_Custom;
14974   SpaceIfMacros.SpaceBeforeParensOptions.AfterIfMacros = true;
14975   verifyFormat("MYIF (a)\n  return;", SpaceIfMacros);
14976   verifyFormat("MYIF (a)\n  return;\nelse MYIF (b)\n  return;", SpaceIfMacros);
14977   verifyFormat("MYIF (a)\n  return;\nelse\n  return;", SpaceIfMacros);
14978 
14979   FormatStyle SpaceForeachMacros = getLLVMStyle();
14980   EXPECT_EQ(SpaceForeachMacros.AllowShortBlocksOnASingleLine,
14981             FormatStyle::SBS_Never);
14982   EXPECT_EQ(SpaceForeachMacros.AllowShortLoopsOnASingleLine, false);
14983   SpaceForeachMacros.SpaceBeforeParens = FormatStyle::SBPO_Custom;
14984   SpaceForeachMacros.SpaceBeforeParensOptions.AfterForeachMacros = true;
14985   verifyFormat("for (;;) {\n"
14986                "}",
14987                SpaceForeachMacros);
14988   verifyFormat("foreach (Item *item, itemlist) {\n"
14989                "}",
14990                SpaceForeachMacros);
14991   verifyFormat("Q_FOREACH (Item *item, itemlist) {\n"
14992                "}",
14993                SpaceForeachMacros);
14994   verifyFormat("BOOST_FOREACH (Item *item, itemlist) {\n"
14995                "}",
14996                SpaceForeachMacros);
14997   verifyFormat("UNKNOWN_FOREACH(Item *item, itemlist) {}", SpaceForeachMacros);
14998 
14999   FormatStyle SomeSpace2 = getLLVMStyle();
15000   SomeSpace2.SpaceBeforeParens = FormatStyle::SBPO_Custom;
15001   SomeSpace2.SpaceBeforeParensOptions.BeforeNonEmptyParentheses = true;
15002   verifyFormat("[]() -> float {}", SomeSpace2);
15003   verifyFormat("[] (auto foo) {}", SomeSpace2);
15004   verifyFormat("[foo]() -> int {}", SomeSpace2);
15005   verifyFormat("int f();", SomeSpace2);
15006   verifyFormat("void f (int a, T b) {\n"
15007                "  while (true)\n"
15008                "    continue;\n"
15009                "}",
15010                SomeSpace2);
15011   verifyFormat("if (true)\n"
15012                "  f();\n"
15013                "else if (true)\n"
15014                "  f();",
15015                SomeSpace2);
15016   verifyFormat("do {\n"
15017                "  do_something();\n"
15018                "} while (something());",
15019                SomeSpace2);
15020   verifyFormat("switch (x) {\n"
15021                "default:\n"
15022                "  break;\n"
15023                "}",
15024                SomeSpace2);
15025   verifyFormat("A::A() : a (1) {}", SomeSpace2);
15026   verifyFormat("void f() __attribute__ ((asdf));", SomeSpace2);
15027   verifyFormat("*(&a + 1);\n"
15028                "&((&a)[1]);\n"
15029                "a[(b + c) * d];\n"
15030                "(((a + 1) * 2) + 3) * 4;",
15031                SomeSpace2);
15032   verifyFormat("#define A(x) x", SomeSpace2);
15033   verifyFormat("#define A (x) x", SomeSpace2);
15034   verifyFormat("#if defined(x)\n"
15035                "#endif",
15036                SomeSpace2);
15037   verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace2);
15038   verifyFormat("size_t x = sizeof (x);", SomeSpace2);
15039   verifyFormat("auto f (int x) -> decltype (x);", SomeSpace2);
15040   verifyFormat("auto f (int x) -> typeof (x);", SomeSpace2);
15041   verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace2);
15042   verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace2);
15043   verifyFormat("int f (T x) noexcept (x.create());", SomeSpace2);
15044   verifyFormat("alignas (128) char a[128];", SomeSpace2);
15045   verifyFormat("size_t x = alignof (MyType);", SomeSpace2);
15046   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");",
15047                SomeSpace2);
15048   verifyFormat("int f() throw (Deprecated);", SomeSpace2);
15049   verifyFormat("typedef void (*cb) (int);", SomeSpace2);
15050   verifyFormat("T A::operator()();", SomeSpace2);
15051   // verifyFormat("X A::operator++ (T);", SomeSpace2);
15052   verifyFormat("int x = int (y);", SomeSpace2);
15053   verifyFormat("auto lambda = []() { return 0; };", SomeSpace2);
15054 
15055   FormatStyle SpaceAfterOverloadedOperator = getLLVMStyle();
15056   SpaceAfterOverloadedOperator.SpaceBeforeParens = FormatStyle::SBPO_Custom;
15057   SpaceAfterOverloadedOperator.SpaceBeforeParensOptions
15058       .AfterOverloadedOperator = true;
15059 
15060   verifyFormat("auto operator++ () -> int;", SpaceAfterOverloadedOperator);
15061   verifyFormat("X A::operator++ ();", SpaceAfterOverloadedOperator);
15062   verifyFormat("some_object.operator++ ();", SpaceAfterOverloadedOperator);
15063   verifyFormat("auto func() -> int;", SpaceAfterOverloadedOperator);
15064 
15065   SpaceAfterOverloadedOperator.SpaceBeforeParensOptions
15066       .AfterOverloadedOperator = false;
15067 
15068   verifyFormat("auto operator++() -> int;", SpaceAfterOverloadedOperator);
15069   verifyFormat("X A::operator++();", SpaceAfterOverloadedOperator);
15070   verifyFormat("some_object.operator++();", SpaceAfterOverloadedOperator);
15071   verifyFormat("auto func() -> int;", SpaceAfterOverloadedOperator);
15072 
15073   auto SpaceAfterRequires = getLLVMStyle();
15074   SpaceAfterRequires.SpaceBeforeParens = FormatStyle::SBPO_Custom;
15075   EXPECT_FALSE(
15076       SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInClause);
15077   EXPECT_FALSE(
15078       SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInExpression);
15079   verifyFormat("void f(auto x)\n"
15080                "  requires requires(int i) { x + i; }\n"
15081                "{}",
15082                SpaceAfterRequires);
15083   verifyFormat("void f(auto x)\n"
15084                "  requires(requires(int i) { x + i; })\n"
15085                "{}",
15086                SpaceAfterRequires);
15087   verifyFormat("if (requires(int i) { x + i; })\n"
15088                "  return;",
15089                SpaceAfterRequires);
15090   verifyFormat("bool b = requires(int i) { x + i; };", SpaceAfterRequires);
15091   verifyFormat("template <typename T>\n"
15092                "  requires(Foo<T>)\n"
15093                "class Bar;",
15094                SpaceAfterRequires);
15095 
15096   SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInClause = true;
15097   verifyFormat("void f(auto x)\n"
15098                "  requires requires(int i) { x + i; }\n"
15099                "{}",
15100                SpaceAfterRequires);
15101   verifyFormat("void f(auto x)\n"
15102                "  requires (requires(int i) { x + i; })\n"
15103                "{}",
15104                SpaceAfterRequires);
15105   verifyFormat("if (requires(int i) { x + i; })\n"
15106                "  return;",
15107                SpaceAfterRequires);
15108   verifyFormat("bool b = requires(int i) { x + i; };", SpaceAfterRequires);
15109   verifyFormat("template <typename T>\n"
15110                "  requires (Foo<T>)\n"
15111                "class Bar;",
15112                SpaceAfterRequires);
15113 
15114   SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInClause = false;
15115   SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInExpression = true;
15116   verifyFormat("void f(auto x)\n"
15117                "  requires requires (int i) { x + i; }\n"
15118                "{}",
15119                SpaceAfterRequires);
15120   verifyFormat("void f(auto x)\n"
15121                "  requires(requires (int i) { x + i; })\n"
15122                "{}",
15123                SpaceAfterRequires);
15124   verifyFormat("if (requires (int i) { x + i; })\n"
15125                "  return;",
15126                SpaceAfterRequires);
15127   verifyFormat("bool b = requires (int i) { x + i; };", SpaceAfterRequires);
15128   verifyFormat("template <typename T>\n"
15129                "  requires(Foo<T>)\n"
15130                "class Bar;",
15131                SpaceAfterRequires);
15132 
15133   SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInClause = true;
15134   verifyFormat("void f(auto x)\n"
15135                "  requires requires (int i) { x + i; }\n"
15136                "{}",
15137                SpaceAfterRequires);
15138   verifyFormat("void f(auto x)\n"
15139                "  requires (requires (int i) { x + i; })\n"
15140                "{}",
15141                SpaceAfterRequires);
15142   verifyFormat("if (requires (int i) { x + i; })\n"
15143                "  return;",
15144                SpaceAfterRequires);
15145   verifyFormat("bool b = requires (int i) { x + i; };", SpaceAfterRequires);
15146   verifyFormat("template <typename T>\n"
15147                "  requires (Foo<T>)\n"
15148                "class Bar;",
15149                SpaceAfterRequires);
15150 }
15151 
15152 TEST_F(FormatTest, SpaceAfterLogicalNot) {
15153   FormatStyle Spaces = getLLVMStyle();
15154   Spaces.SpaceAfterLogicalNot = true;
15155 
15156   verifyFormat("bool x = ! y", Spaces);
15157   verifyFormat("if (! isFailure())", Spaces);
15158   verifyFormat("if (! (a && b))", Spaces);
15159   verifyFormat("\"Error!\"", Spaces);
15160   verifyFormat("! ! x", Spaces);
15161 }
15162 
15163 TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
15164   FormatStyle Spaces = getLLVMStyle();
15165 
15166   Spaces.SpacesInParentheses = true;
15167   verifyFormat("do_something( ::globalVar );", Spaces);
15168   verifyFormat("call( x, y, z );", Spaces);
15169   verifyFormat("call();", Spaces);
15170   verifyFormat("std::function<void( int, int )> callback;", Spaces);
15171   verifyFormat("void inFunction() { std::function<void( int, int )> fct; }",
15172                Spaces);
15173   verifyFormat("while ( (bool)1 )\n"
15174                "  continue;",
15175                Spaces);
15176   verifyFormat("for ( ;; )\n"
15177                "  continue;",
15178                Spaces);
15179   verifyFormat("if ( true )\n"
15180                "  f();\n"
15181                "else if ( true )\n"
15182                "  f();",
15183                Spaces);
15184   verifyFormat("do {\n"
15185                "  do_something( (int)i );\n"
15186                "} while ( something() );",
15187                Spaces);
15188   verifyFormat("switch ( x ) {\n"
15189                "default:\n"
15190                "  break;\n"
15191                "}",
15192                Spaces);
15193 
15194   Spaces.SpacesInParentheses = false;
15195   Spaces.SpacesInCStyleCastParentheses = true;
15196   verifyFormat("Type *A = ( Type * )P;", Spaces);
15197   verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
15198   verifyFormat("x = ( int32 )y;", Spaces);
15199   verifyFormat("int a = ( int )(2.0f);", Spaces);
15200   verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
15201   verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
15202   verifyFormat("#define x (( int )-1)", Spaces);
15203 
15204   // Run the first set of tests again with:
15205   Spaces.SpacesInParentheses = false;
15206   Spaces.SpaceInEmptyParentheses = true;
15207   Spaces.SpacesInCStyleCastParentheses = true;
15208   verifyFormat("call(x, y, z);", Spaces);
15209   verifyFormat("call( );", Spaces);
15210   verifyFormat("std::function<void(int, int)> callback;", Spaces);
15211   verifyFormat("while (( bool )1)\n"
15212                "  continue;",
15213                Spaces);
15214   verifyFormat("for (;;)\n"
15215                "  continue;",
15216                Spaces);
15217   verifyFormat("if (true)\n"
15218                "  f( );\n"
15219                "else if (true)\n"
15220                "  f( );",
15221                Spaces);
15222   verifyFormat("do {\n"
15223                "  do_something(( int )i);\n"
15224                "} while (something( ));",
15225                Spaces);
15226   verifyFormat("switch (x) {\n"
15227                "default:\n"
15228                "  break;\n"
15229                "}",
15230                Spaces);
15231 
15232   // Run the first set of tests again with:
15233   Spaces.SpaceAfterCStyleCast = true;
15234   verifyFormat("call(x, y, z);", Spaces);
15235   verifyFormat("call( );", Spaces);
15236   verifyFormat("std::function<void(int, int)> callback;", Spaces);
15237   verifyFormat("while (( bool ) 1)\n"
15238                "  continue;",
15239                Spaces);
15240   verifyFormat("for (;;)\n"
15241                "  continue;",
15242                Spaces);
15243   verifyFormat("if (true)\n"
15244                "  f( );\n"
15245                "else if (true)\n"
15246                "  f( );",
15247                Spaces);
15248   verifyFormat("do {\n"
15249                "  do_something(( int ) i);\n"
15250                "} while (something( ));",
15251                Spaces);
15252   verifyFormat("switch (x) {\n"
15253                "default:\n"
15254                "  break;\n"
15255                "}",
15256                Spaces);
15257   verifyFormat("#define CONF_BOOL(x) ( bool * ) ( void * ) (x)", Spaces);
15258   verifyFormat("#define CONF_BOOL(x) ( bool * ) (x)", Spaces);
15259   verifyFormat("#define CONF_BOOL(x) ( bool ) (x)", Spaces);
15260   verifyFormat("bool *y = ( bool * ) ( void * ) (x);", Spaces);
15261   verifyFormat("bool *y = ( bool * ) (x);", Spaces);
15262 
15263   // Run subset of tests again with:
15264   Spaces.SpacesInCStyleCastParentheses = false;
15265   Spaces.SpaceAfterCStyleCast = true;
15266   verifyFormat("while ((bool) 1)\n"
15267                "  continue;",
15268                Spaces);
15269   verifyFormat("do {\n"
15270                "  do_something((int) i);\n"
15271                "} while (something( ));",
15272                Spaces);
15273 
15274   verifyFormat("size_t idx = (size_t) (ptr - ((char *) file));", Spaces);
15275   verifyFormat("size_t idx = (size_t) a;", Spaces);
15276   verifyFormat("size_t idx = (size_t) (a - 1);", Spaces);
15277   verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces);
15278   verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces);
15279   verifyFormat("size_t idx = (*foo)(a - 1);", Spaces);
15280   verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces);
15281   verifyFormat("#define CONF_BOOL(x) (bool *) (void *) (x)", Spaces);
15282   verifyFormat("#define CONF_BOOL(x) (bool *) (void *) (int) (x)", Spaces);
15283   verifyFormat("bool *y = (bool *) (void *) (x);", Spaces);
15284   verifyFormat("bool *y = (bool *) (void *) (int) (x);", Spaces);
15285   verifyFormat("bool *y = (bool *) (void *) (int) foo(x);", Spaces);
15286   Spaces.ColumnLimit = 80;
15287   Spaces.IndentWidth = 4;
15288   Spaces.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
15289   verifyFormat("void foo( ) {\n"
15290                "    size_t foo = (*(function))(\n"
15291                "        Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, "
15292                "BarrrrrrrrrrrrLong,\n"
15293                "        FoooooooooLooooong);\n"
15294                "}",
15295                Spaces);
15296   Spaces.SpaceAfterCStyleCast = false;
15297   verifyFormat("size_t idx = (size_t)(ptr - ((char *)file));", Spaces);
15298   verifyFormat("size_t idx = (size_t)a;", Spaces);
15299   verifyFormat("size_t idx = (size_t)(a - 1);", Spaces);
15300   verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces);
15301   verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces);
15302   verifyFormat("size_t idx = (*foo)(a - 1);", Spaces);
15303   verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces);
15304 
15305   verifyFormat("void foo( ) {\n"
15306                "    size_t foo = (*(function))(\n"
15307                "        Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, "
15308                "BarrrrrrrrrrrrLong,\n"
15309                "        FoooooooooLooooong);\n"
15310                "}",
15311                Spaces);
15312 }
15313 
15314 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
15315   verifyFormat("int a[5];");
15316   verifyFormat("a[3] += 42;");
15317 
15318   FormatStyle Spaces = getLLVMStyle();
15319   Spaces.SpacesInSquareBrackets = true;
15320   // Not lambdas.
15321   verifyFormat("int a[ 5 ];", Spaces);
15322   verifyFormat("a[ 3 ] += 42;", Spaces);
15323   verifyFormat("constexpr char hello[]{\"hello\"};", Spaces);
15324   verifyFormat("double &operator[](int i) { return 0; }\n"
15325                "int i;",
15326                Spaces);
15327   verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces);
15328   verifyFormat("int i = a[ a ][ a ]->f();", Spaces);
15329   verifyFormat("int i = (*b)[ a ]->f();", Spaces);
15330   // Lambdas.
15331   verifyFormat("int c = []() -> int { return 2; }();\n", Spaces);
15332   verifyFormat("return [ i, args... ] {};", Spaces);
15333   verifyFormat("int foo = [ &bar ]() {};", Spaces);
15334   verifyFormat("int foo = [ = ]() {};", Spaces);
15335   verifyFormat("int foo = [ & ]() {};", Spaces);
15336   verifyFormat("int foo = [ =, &bar ]() {};", Spaces);
15337   verifyFormat("int foo = [ &bar, = ]() {};", Spaces);
15338 }
15339 
15340 TEST_F(FormatTest, ConfigurableSpaceBeforeBrackets) {
15341   FormatStyle NoSpaceStyle = getLLVMStyle();
15342   verifyFormat("int a[5];", NoSpaceStyle);
15343   verifyFormat("a[3] += 42;", NoSpaceStyle);
15344 
15345   verifyFormat("int a[1];", NoSpaceStyle);
15346   verifyFormat("int 1 [a];", NoSpaceStyle);
15347   verifyFormat("int a[1][2];", NoSpaceStyle);
15348   verifyFormat("a[7] = 5;", NoSpaceStyle);
15349   verifyFormat("int a = (f())[23];", NoSpaceStyle);
15350   verifyFormat("f([] {})", NoSpaceStyle);
15351 
15352   FormatStyle Space = getLLVMStyle();
15353   Space.SpaceBeforeSquareBrackets = true;
15354   verifyFormat("int c = []() -> int { return 2; }();\n", Space);
15355   verifyFormat("return [i, args...] {};", Space);
15356 
15357   verifyFormat("int a [5];", Space);
15358   verifyFormat("a [3] += 42;", Space);
15359   verifyFormat("constexpr char hello []{\"hello\"};", Space);
15360   verifyFormat("double &operator[](int i) { return 0; }\n"
15361                "int i;",
15362                Space);
15363   verifyFormat("std::unique_ptr<int []> foo() {}", Space);
15364   verifyFormat("int i = a [a][a]->f();", Space);
15365   verifyFormat("int i = (*b) [a]->f();", Space);
15366 
15367   verifyFormat("int a [1];", Space);
15368   verifyFormat("int 1 [a];", Space);
15369   verifyFormat("int a [1][2];", Space);
15370   verifyFormat("a [7] = 5;", Space);
15371   verifyFormat("int a = (f()) [23];", Space);
15372   verifyFormat("f([] {})", Space);
15373 }
15374 
15375 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
15376   verifyFormat("int a = 5;");
15377   verifyFormat("a += 42;");
15378   verifyFormat("a or_eq 8;");
15379 
15380   FormatStyle Spaces = getLLVMStyle();
15381   Spaces.SpaceBeforeAssignmentOperators = false;
15382   verifyFormat("int a= 5;", Spaces);
15383   verifyFormat("a+= 42;", Spaces);
15384   verifyFormat("a or_eq 8;", Spaces);
15385 }
15386 
15387 TEST_F(FormatTest, ConfigurableSpaceBeforeColon) {
15388   verifyFormat("class Foo : public Bar {};");
15389   verifyFormat("Foo::Foo() : foo(1) {}");
15390   verifyFormat("for (auto a : b) {\n}");
15391   verifyFormat("int x = a ? b : c;");
15392   verifyFormat("{\n"
15393                "label0:\n"
15394                "  int x = 0;\n"
15395                "}");
15396   verifyFormat("switch (x) {\n"
15397                "case 1:\n"
15398                "default:\n"
15399                "}");
15400   verifyFormat("switch (allBraces) {\n"
15401                "case 1: {\n"
15402                "  break;\n"
15403                "}\n"
15404                "case 2: {\n"
15405                "  [[fallthrough]];\n"
15406                "}\n"
15407                "default: {\n"
15408                "  break;\n"
15409                "}\n"
15410                "}");
15411 
15412   FormatStyle CtorInitializerStyle = getLLVMStyleWithColumns(30);
15413   CtorInitializerStyle.SpaceBeforeCtorInitializerColon = false;
15414   verifyFormat("class Foo : public Bar {};", CtorInitializerStyle);
15415   verifyFormat("Foo::Foo(): foo(1) {}", CtorInitializerStyle);
15416   verifyFormat("for (auto a : b) {\n}", CtorInitializerStyle);
15417   verifyFormat("int x = a ? b : c;", CtorInitializerStyle);
15418   verifyFormat("{\n"
15419                "label1:\n"
15420                "  int x = 0;\n"
15421                "}",
15422                CtorInitializerStyle);
15423   verifyFormat("switch (x) {\n"
15424                "case 1:\n"
15425                "default:\n"
15426                "}",
15427                CtorInitializerStyle);
15428   verifyFormat("switch (allBraces) {\n"
15429                "case 1: {\n"
15430                "  break;\n"
15431                "}\n"
15432                "case 2: {\n"
15433                "  [[fallthrough]];\n"
15434                "}\n"
15435                "default: {\n"
15436                "  break;\n"
15437                "}\n"
15438                "}",
15439                CtorInitializerStyle);
15440   CtorInitializerStyle.BreakConstructorInitializers =
15441       FormatStyle::BCIS_AfterColon;
15442   verifyFormat("Fooooooooooo::Fooooooooooo():\n"
15443                "    aaaaaaaaaaaaaaaa(1),\n"
15444                "    bbbbbbbbbbbbbbbb(2) {}",
15445                CtorInitializerStyle);
15446   CtorInitializerStyle.BreakConstructorInitializers =
15447       FormatStyle::BCIS_BeforeComma;
15448   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
15449                "    : aaaaaaaaaaaaaaaa(1)\n"
15450                "    , bbbbbbbbbbbbbbbb(2) {}",
15451                CtorInitializerStyle);
15452   CtorInitializerStyle.BreakConstructorInitializers =
15453       FormatStyle::BCIS_BeforeColon;
15454   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
15455                "    : aaaaaaaaaaaaaaaa(1),\n"
15456                "      bbbbbbbbbbbbbbbb(2) {}",
15457                CtorInitializerStyle);
15458   CtorInitializerStyle.ConstructorInitializerIndentWidth = 0;
15459   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
15460                ": aaaaaaaaaaaaaaaa(1),\n"
15461                "  bbbbbbbbbbbbbbbb(2) {}",
15462                CtorInitializerStyle);
15463 
15464   FormatStyle InheritanceStyle = getLLVMStyleWithColumns(30);
15465   InheritanceStyle.SpaceBeforeInheritanceColon = false;
15466   verifyFormat("class Foo: public Bar {};", InheritanceStyle);
15467   verifyFormat("Foo::Foo() : foo(1) {}", InheritanceStyle);
15468   verifyFormat("for (auto a : b) {\n}", InheritanceStyle);
15469   verifyFormat("int x = a ? b : c;", InheritanceStyle);
15470   verifyFormat("{\n"
15471                "label2:\n"
15472                "  int x = 0;\n"
15473                "}",
15474                InheritanceStyle);
15475   verifyFormat("switch (x) {\n"
15476                "case 1:\n"
15477                "default:\n"
15478                "}",
15479                InheritanceStyle);
15480   verifyFormat("switch (allBraces) {\n"
15481                "case 1: {\n"
15482                "  break;\n"
15483                "}\n"
15484                "case 2: {\n"
15485                "  [[fallthrough]];\n"
15486                "}\n"
15487                "default: {\n"
15488                "  break;\n"
15489                "}\n"
15490                "}",
15491                InheritanceStyle);
15492   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterComma;
15493   verifyFormat("class Foooooooooooooooooooooo\n"
15494                "    : public aaaaaaaaaaaaaaaaaa,\n"
15495                "      public bbbbbbbbbbbbbbbbbb {\n"
15496                "}",
15497                InheritanceStyle);
15498   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterColon;
15499   verifyFormat("class Foooooooooooooooooooooo:\n"
15500                "    public aaaaaaaaaaaaaaaaaa,\n"
15501                "    public bbbbbbbbbbbbbbbbbb {\n"
15502                "}",
15503                InheritanceStyle);
15504   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeComma;
15505   verifyFormat("class Foooooooooooooooooooooo\n"
15506                "    : public aaaaaaaaaaaaaaaaaa\n"
15507                "    , public bbbbbbbbbbbbbbbbbb {\n"
15508                "}",
15509                InheritanceStyle);
15510   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
15511   verifyFormat("class Foooooooooooooooooooooo\n"
15512                "    : public aaaaaaaaaaaaaaaaaa,\n"
15513                "      public bbbbbbbbbbbbbbbbbb {\n"
15514                "}",
15515                InheritanceStyle);
15516   InheritanceStyle.ConstructorInitializerIndentWidth = 0;
15517   verifyFormat("class Foooooooooooooooooooooo\n"
15518                ": public aaaaaaaaaaaaaaaaaa,\n"
15519                "  public bbbbbbbbbbbbbbbbbb {}",
15520                InheritanceStyle);
15521 
15522   FormatStyle ForLoopStyle = getLLVMStyle();
15523   ForLoopStyle.SpaceBeforeRangeBasedForLoopColon = false;
15524   verifyFormat("class Foo : public Bar {};", ForLoopStyle);
15525   verifyFormat("Foo::Foo() : foo(1) {}", ForLoopStyle);
15526   verifyFormat("for (auto a: b) {\n}", ForLoopStyle);
15527   verifyFormat("int x = a ? b : c;", ForLoopStyle);
15528   verifyFormat("{\n"
15529                "label2:\n"
15530                "  int x = 0;\n"
15531                "}",
15532                ForLoopStyle);
15533   verifyFormat("switch (x) {\n"
15534                "case 1:\n"
15535                "default:\n"
15536                "}",
15537                ForLoopStyle);
15538   verifyFormat("switch (allBraces) {\n"
15539                "case 1: {\n"
15540                "  break;\n"
15541                "}\n"
15542                "case 2: {\n"
15543                "  [[fallthrough]];\n"
15544                "}\n"
15545                "default: {\n"
15546                "  break;\n"
15547                "}\n"
15548                "}",
15549                ForLoopStyle);
15550 
15551   FormatStyle CaseStyle = getLLVMStyle();
15552   CaseStyle.SpaceBeforeCaseColon = true;
15553   verifyFormat("class Foo : public Bar {};", CaseStyle);
15554   verifyFormat("Foo::Foo() : foo(1) {}", CaseStyle);
15555   verifyFormat("for (auto a : b) {\n}", CaseStyle);
15556   verifyFormat("int x = a ? b : c;", CaseStyle);
15557   verifyFormat("switch (x) {\n"
15558                "case 1 :\n"
15559                "default :\n"
15560                "}",
15561                CaseStyle);
15562   verifyFormat("switch (allBraces) {\n"
15563                "case 1 : {\n"
15564                "  break;\n"
15565                "}\n"
15566                "case 2 : {\n"
15567                "  [[fallthrough]];\n"
15568                "}\n"
15569                "default : {\n"
15570                "  break;\n"
15571                "}\n"
15572                "}",
15573                CaseStyle);
15574 
15575   FormatStyle NoSpaceStyle = getLLVMStyle();
15576   EXPECT_EQ(NoSpaceStyle.SpaceBeforeCaseColon, false);
15577   NoSpaceStyle.SpaceBeforeCtorInitializerColon = false;
15578   NoSpaceStyle.SpaceBeforeInheritanceColon = false;
15579   NoSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false;
15580   verifyFormat("class Foo: public Bar {};", NoSpaceStyle);
15581   verifyFormat("Foo::Foo(): foo(1) {}", NoSpaceStyle);
15582   verifyFormat("for (auto a: b) {\n}", NoSpaceStyle);
15583   verifyFormat("int x = a ? b : c;", NoSpaceStyle);
15584   verifyFormat("{\n"
15585                "label3:\n"
15586                "  int x = 0;\n"
15587                "}",
15588                NoSpaceStyle);
15589   verifyFormat("switch (x) {\n"
15590                "case 1:\n"
15591                "default:\n"
15592                "}",
15593                NoSpaceStyle);
15594   verifyFormat("switch (allBraces) {\n"
15595                "case 1: {\n"
15596                "  break;\n"
15597                "}\n"
15598                "case 2: {\n"
15599                "  [[fallthrough]];\n"
15600                "}\n"
15601                "default: {\n"
15602                "  break;\n"
15603                "}\n"
15604                "}",
15605                NoSpaceStyle);
15606 
15607   FormatStyle InvertedSpaceStyle = getLLVMStyle();
15608   InvertedSpaceStyle.SpaceBeforeCaseColon = true;
15609   InvertedSpaceStyle.SpaceBeforeCtorInitializerColon = false;
15610   InvertedSpaceStyle.SpaceBeforeInheritanceColon = false;
15611   InvertedSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false;
15612   verifyFormat("class Foo: public Bar {};", InvertedSpaceStyle);
15613   verifyFormat("Foo::Foo(): foo(1) {}", InvertedSpaceStyle);
15614   verifyFormat("for (auto a: b) {\n}", InvertedSpaceStyle);
15615   verifyFormat("int x = a ? b : c;", InvertedSpaceStyle);
15616   verifyFormat("{\n"
15617                "label3:\n"
15618                "  int x = 0;\n"
15619                "}",
15620                InvertedSpaceStyle);
15621   verifyFormat("switch (x) {\n"
15622                "case 1 :\n"
15623                "case 2 : {\n"
15624                "  break;\n"
15625                "}\n"
15626                "default :\n"
15627                "  break;\n"
15628                "}",
15629                InvertedSpaceStyle);
15630   verifyFormat("switch (allBraces) {\n"
15631                "case 1 : {\n"
15632                "  break;\n"
15633                "}\n"
15634                "case 2 : {\n"
15635                "  [[fallthrough]];\n"
15636                "}\n"
15637                "default : {\n"
15638                "  break;\n"
15639                "}\n"
15640                "}",
15641                InvertedSpaceStyle);
15642 }
15643 
15644 TEST_F(FormatTest, ConfigurableSpaceAroundPointerQualifiers) {
15645   FormatStyle Style = getLLVMStyle();
15646 
15647   Style.PointerAlignment = FormatStyle::PAS_Left;
15648   Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default;
15649   verifyFormat("void* const* x = NULL;", Style);
15650 
15651 #define verifyQualifierSpaces(Code, Pointers, Qualifiers)                      \
15652   do {                                                                         \
15653     Style.PointerAlignment = FormatStyle::Pointers;                            \
15654     Style.SpaceAroundPointerQualifiers = FormatStyle::Qualifiers;              \
15655     verifyFormat(Code, Style);                                                 \
15656   } while (false)
15657 
15658   verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Default);
15659   verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_Default);
15660   verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Default);
15661 
15662   verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Before);
15663   verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Before);
15664   verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Before);
15665 
15666   verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_After);
15667   verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_After);
15668   verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_After);
15669 
15670   verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_Both);
15671   verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Both);
15672   verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Both);
15673 
15674   verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Default);
15675   verifyQualifierSpaces("Foo::operator void const *();", PAS_Right,
15676                         SAPQ_Default);
15677   verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
15678                         SAPQ_Default);
15679 
15680   verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Before);
15681   verifyQualifierSpaces("Foo::operator void const *();", PAS_Right,
15682                         SAPQ_Before);
15683   verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
15684                         SAPQ_Before);
15685 
15686   verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_After);
15687   verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_After);
15688   verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
15689                         SAPQ_After);
15690 
15691   verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_Both);
15692   verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_Both);
15693   verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, SAPQ_Both);
15694 
15695 #undef verifyQualifierSpaces
15696 
15697   FormatStyle Spaces = getLLVMStyle();
15698   Spaces.AttributeMacros.push_back("qualified");
15699   Spaces.PointerAlignment = FormatStyle::PAS_Right;
15700   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default;
15701   verifyFormat("SomeType *volatile *a = NULL;", Spaces);
15702   verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces);
15703   verifyFormat("std::vector<SomeType *const *> x;", Spaces);
15704   verifyFormat("std::vector<SomeType *qualified *> x;", Spaces);
15705   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
15706   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before;
15707   verifyFormat("SomeType * volatile *a = NULL;", Spaces);
15708   verifyFormat("SomeType * __attribute__((attr)) *a = NULL;", Spaces);
15709   verifyFormat("std::vector<SomeType * const *> x;", Spaces);
15710   verifyFormat("std::vector<SomeType * qualified *> x;", Spaces);
15711   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
15712 
15713   // Check that SAPQ_Before doesn't result in extra spaces for PAS_Left.
15714   Spaces.PointerAlignment = FormatStyle::PAS_Left;
15715   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before;
15716   verifyFormat("SomeType* volatile* a = NULL;", Spaces);
15717   verifyFormat("SomeType* __attribute__((attr))* a = NULL;", Spaces);
15718   verifyFormat("std::vector<SomeType* const*> x;", Spaces);
15719   verifyFormat("std::vector<SomeType* qualified*> x;", Spaces);
15720   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
15721   // However, setting it to SAPQ_After should add spaces after __attribute, etc.
15722   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After;
15723   verifyFormat("SomeType* volatile * a = NULL;", Spaces);
15724   verifyFormat("SomeType* __attribute__((attr)) * a = NULL;", Spaces);
15725   verifyFormat("std::vector<SomeType* const *> x;", Spaces);
15726   verifyFormat("std::vector<SomeType* qualified *> x;", Spaces);
15727   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
15728 
15729   // PAS_Middle should not have any noticeable changes even for SAPQ_Both
15730   Spaces.PointerAlignment = FormatStyle::PAS_Middle;
15731   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After;
15732   verifyFormat("SomeType * volatile * a = NULL;", Spaces);
15733   verifyFormat("SomeType * __attribute__((attr)) * a = NULL;", Spaces);
15734   verifyFormat("std::vector<SomeType * const *> x;", Spaces);
15735   verifyFormat("std::vector<SomeType * qualified *> x;", Spaces);
15736   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
15737 }
15738 
15739 TEST_F(FormatTest, AlignConsecutiveMacros) {
15740   FormatStyle Style = getLLVMStyle();
15741   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
15742   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
15743   Style.AlignConsecutiveMacros = FormatStyle::ACS_None;
15744 
15745   verifyFormat("#define a 3\n"
15746                "#define bbbb 4\n"
15747                "#define ccc (5)",
15748                Style);
15749 
15750   verifyFormat("#define f(x) (x * x)\n"
15751                "#define fff(x, y, z) (x * y + z)\n"
15752                "#define ffff(x, y) (x - y)",
15753                Style);
15754 
15755   verifyFormat("#define foo(x, y) (x + y)\n"
15756                "#define bar (5, 6)(2 + 2)",
15757                Style);
15758 
15759   verifyFormat("#define a 3\n"
15760                "#define bbbb 4\n"
15761                "#define ccc (5)\n"
15762                "#define f(x) (x * x)\n"
15763                "#define fff(x, y, z) (x * y + z)\n"
15764                "#define ffff(x, y) (x - y)",
15765                Style);
15766 
15767   Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
15768   verifyFormat("#define a    3\n"
15769                "#define bbbb 4\n"
15770                "#define ccc  (5)",
15771                Style);
15772 
15773   verifyFormat("#define f(x)         (x * x)\n"
15774                "#define fff(x, y, z) (x * y + z)\n"
15775                "#define ffff(x, y)   (x - y)",
15776                Style);
15777 
15778   verifyFormat("#define foo(x, y) (x + y)\n"
15779                "#define bar       (5, 6)(2 + 2)",
15780                Style);
15781 
15782   verifyFormat("#define a            3\n"
15783                "#define bbbb         4\n"
15784                "#define ccc          (5)\n"
15785                "#define f(x)         (x * x)\n"
15786                "#define fff(x, y, z) (x * y + z)\n"
15787                "#define ffff(x, y)   (x - y)",
15788                Style);
15789 
15790   verifyFormat("#define a         5\n"
15791                "#define foo(x, y) (x + y)\n"
15792                "#define CCC       (6)\n"
15793                "auto lambda = []() {\n"
15794                "  auto  ii = 0;\n"
15795                "  float j  = 0;\n"
15796                "  return 0;\n"
15797                "};\n"
15798                "int   i  = 0;\n"
15799                "float i2 = 0;\n"
15800                "auto  v  = type{\n"
15801                "    i = 1,   //\n"
15802                "    (i = 2), //\n"
15803                "    i = 3    //\n"
15804                "};",
15805                Style);
15806 
15807   Style.AlignConsecutiveMacros = FormatStyle::ACS_None;
15808   Style.ColumnLimit = 20;
15809 
15810   verifyFormat("#define a          \\\n"
15811                "  \"aabbbbbbbbbbbb\"\n"
15812                "#define D          \\\n"
15813                "  \"aabbbbbbbbbbbb\" \\\n"
15814                "  \"ccddeeeeeeeee\"\n"
15815                "#define B          \\\n"
15816                "  \"QQQQQQQQQQQQQ\"  \\\n"
15817                "  \"FFFFFFFFFFFFF\"  \\\n"
15818                "  \"LLLLLLLL\"\n",
15819                Style);
15820 
15821   Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
15822   verifyFormat("#define a          \\\n"
15823                "  \"aabbbbbbbbbbbb\"\n"
15824                "#define D          \\\n"
15825                "  \"aabbbbbbbbbbbb\" \\\n"
15826                "  \"ccddeeeeeeeee\"\n"
15827                "#define B          \\\n"
15828                "  \"QQQQQQQQQQQQQ\"  \\\n"
15829                "  \"FFFFFFFFFFFFF\"  \\\n"
15830                "  \"LLLLLLLL\"\n",
15831                Style);
15832 
15833   // Test across comments
15834   Style.MaxEmptyLinesToKeep = 10;
15835   Style.ReflowComments = false;
15836   Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossComments;
15837   EXPECT_EQ("#define a    3\n"
15838             "// line comment\n"
15839             "#define bbbb 4\n"
15840             "#define ccc  (5)",
15841             format("#define a 3\n"
15842                    "// line comment\n"
15843                    "#define bbbb 4\n"
15844                    "#define ccc (5)",
15845                    Style));
15846 
15847   EXPECT_EQ("#define a    3\n"
15848             "/* block comment */\n"
15849             "#define bbbb 4\n"
15850             "#define ccc  (5)",
15851             format("#define a  3\n"
15852                    "/* block comment */\n"
15853                    "#define bbbb 4\n"
15854                    "#define ccc (5)",
15855                    Style));
15856 
15857   EXPECT_EQ("#define a    3\n"
15858             "/* multi-line *\n"
15859             " * block comment */\n"
15860             "#define bbbb 4\n"
15861             "#define ccc  (5)",
15862             format("#define a 3\n"
15863                    "/* multi-line *\n"
15864                    " * block comment */\n"
15865                    "#define bbbb 4\n"
15866                    "#define ccc (5)",
15867                    Style));
15868 
15869   EXPECT_EQ("#define a    3\n"
15870             "// multi-line line comment\n"
15871             "//\n"
15872             "#define bbbb 4\n"
15873             "#define ccc  (5)",
15874             format("#define a  3\n"
15875                    "// multi-line line comment\n"
15876                    "//\n"
15877                    "#define bbbb 4\n"
15878                    "#define ccc (5)",
15879                    Style));
15880 
15881   EXPECT_EQ("#define a 3\n"
15882             "// empty lines still break.\n"
15883             "\n"
15884             "#define bbbb 4\n"
15885             "#define ccc  (5)",
15886             format("#define a     3\n"
15887                    "// empty lines still break.\n"
15888                    "\n"
15889                    "#define bbbb     4\n"
15890                    "#define ccc  (5)",
15891                    Style));
15892 
15893   // Test across empty lines
15894   Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossEmptyLines;
15895   EXPECT_EQ("#define a    3\n"
15896             "\n"
15897             "#define bbbb 4\n"
15898             "#define ccc  (5)",
15899             format("#define a 3\n"
15900                    "\n"
15901                    "#define bbbb 4\n"
15902                    "#define ccc (5)",
15903                    Style));
15904 
15905   EXPECT_EQ("#define a    3\n"
15906             "\n"
15907             "\n"
15908             "\n"
15909             "#define bbbb 4\n"
15910             "#define ccc  (5)",
15911             format("#define a        3\n"
15912                    "\n"
15913                    "\n"
15914                    "\n"
15915                    "#define bbbb 4\n"
15916                    "#define ccc (5)",
15917                    Style));
15918 
15919   EXPECT_EQ("#define a 3\n"
15920             "// comments should break alignment\n"
15921             "//\n"
15922             "#define bbbb 4\n"
15923             "#define ccc  (5)",
15924             format("#define a        3\n"
15925                    "// comments should break alignment\n"
15926                    "//\n"
15927                    "#define bbbb 4\n"
15928                    "#define ccc (5)",
15929                    Style));
15930 
15931   // Test across empty lines and comments
15932   Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossEmptyLinesAndComments;
15933   verifyFormat("#define a    3\n"
15934                "\n"
15935                "// line comment\n"
15936                "#define bbbb 4\n"
15937                "#define ccc  (5)",
15938                Style);
15939 
15940   EXPECT_EQ("#define a    3\n"
15941             "\n"
15942             "\n"
15943             "/* multi-line *\n"
15944             " * block comment */\n"
15945             "\n"
15946             "\n"
15947             "#define bbbb 4\n"
15948             "#define ccc  (5)",
15949             format("#define a 3\n"
15950                    "\n"
15951                    "\n"
15952                    "/* multi-line *\n"
15953                    " * block comment */\n"
15954                    "\n"
15955                    "\n"
15956                    "#define bbbb 4\n"
15957                    "#define ccc (5)",
15958                    Style));
15959 
15960   EXPECT_EQ("#define a    3\n"
15961             "\n"
15962             "\n"
15963             "/* multi-line *\n"
15964             " * block comment */\n"
15965             "\n"
15966             "\n"
15967             "#define bbbb 4\n"
15968             "#define ccc  (5)",
15969             format("#define a 3\n"
15970                    "\n"
15971                    "\n"
15972                    "/* multi-line *\n"
15973                    " * block comment */\n"
15974                    "\n"
15975                    "\n"
15976                    "#define bbbb 4\n"
15977                    "#define ccc       (5)",
15978                    Style));
15979 }
15980 
15981 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLines) {
15982   FormatStyle Alignment = getLLVMStyle();
15983   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
15984   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_AcrossEmptyLines;
15985 
15986   Alignment.MaxEmptyLinesToKeep = 10;
15987   /* Test alignment across empty lines */
15988   EXPECT_EQ("int a           = 5;\n"
15989             "\n"
15990             "int oneTwoThree = 123;",
15991             format("int a       = 5;\n"
15992                    "\n"
15993                    "int oneTwoThree= 123;",
15994                    Alignment));
15995   EXPECT_EQ("int a           = 5;\n"
15996             "int one         = 1;\n"
15997             "\n"
15998             "int oneTwoThree = 123;",
15999             format("int a = 5;\n"
16000                    "int one = 1;\n"
16001                    "\n"
16002                    "int oneTwoThree = 123;",
16003                    Alignment));
16004   EXPECT_EQ("int a           = 5;\n"
16005             "int one         = 1;\n"
16006             "\n"
16007             "int oneTwoThree = 123;\n"
16008             "int oneTwo      = 12;",
16009             format("int a = 5;\n"
16010                    "int one = 1;\n"
16011                    "\n"
16012                    "int oneTwoThree = 123;\n"
16013                    "int oneTwo = 12;",
16014                    Alignment));
16015 
16016   /* Test across comments */
16017   EXPECT_EQ("int a = 5;\n"
16018             "/* block comment */\n"
16019             "int oneTwoThree = 123;",
16020             format("int a = 5;\n"
16021                    "/* block comment */\n"
16022                    "int oneTwoThree=123;",
16023                    Alignment));
16024 
16025   EXPECT_EQ("int a = 5;\n"
16026             "// line comment\n"
16027             "int oneTwoThree = 123;",
16028             format("int a = 5;\n"
16029                    "// line comment\n"
16030                    "int oneTwoThree=123;",
16031                    Alignment));
16032 
16033   /* Test across comments and newlines */
16034   EXPECT_EQ("int a = 5;\n"
16035             "\n"
16036             "/* block comment */\n"
16037             "int oneTwoThree = 123;",
16038             format("int a = 5;\n"
16039                    "\n"
16040                    "/* block comment */\n"
16041                    "int oneTwoThree=123;",
16042                    Alignment));
16043 
16044   EXPECT_EQ("int a = 5;\n"
16045             "\n"
16046             "// line comment\n"
16047             "int oneTwoThree = 123;",
16048             format("int a = 5;\n"
16049                    "\n"
16050                    "// line comment\n"
16051                    "int oneTwoThree=123;",
16052                    Alignment));
16053 }
16054 
16055 TEST_F(FormatTest, AlignConsecutiveDeclarationsAcrossEmptyLinesAndComments) {
16056   FormatStyle Alignment = getLLVMStyle();
16057   Alignment.AlignConsecutiveDeclarations =
16058       FormatStyle::ACS_AcrossEmptyLinesAndComments;
16059   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
16060 
16061   Alignment.MaxEmptyLinesToKeep = 10;
16062   /* Test alignment across empty lines */
16063   EXPECT_EQ("int         a = 5;\n"
16064             "\n"
16065             "float const oneTwoThree = 123;",
16066             format("int a = 5;\n"
16067                    "\n"
16068                    "float const oneTwoThree = 123;",
16069                    Alignment));
16070   EXPECT_EQ("int         a = 5;\n"
16071             "float const one = 1;\n"
16072             "\n"
16073             "int         oneTwoThree = 123;",
16074             format("int a = 5;\n"
16075                    "float const one = 1;\n"
16076                    "\n"
16077                    "int oneTwoThree = 123;",
16078                    Alignment));
16079 
16080   /* Test across comments */
16081   EXPECT_EQ("float const a = 5;\n"
16082             "/* block comment */\n"
16083             "int         oneTwoThree = 123;",
16084             format("float const a = 5;\n"
16085                    "/* block comment */\n"
16086                    "int oneTwoThree=123;",
16087                    Alignment));
16088 
16089   EXPECT_EQ("float const a = 5;\n"
16090             "// line comment\n"
16091             "int         oneTwoThree = 123;",
16092             format("float const a = 5;\n"
16093                    "// line comment\n"
16094                    "int oneTwoThree=123;",
16095                    Alignment));
16096 
16097   /* Test across comments and newlines */
16098   EXPECT_EQ("float const a = 5;\n"
16099             "\n"
16100             "/* block comment */\n"
16101             "int         oneTwoThree = 123;",
16102             format("float const a = 5;\n"
16103                    "\n"
16104                    "/* block comment */\n"
16105                    "int         oneTwoThree=123;",
16106                    Alignment));
16107 
16108   EXPECT_EQ("float const a = 5;\n"
16109             "\n"
16110             "// line comment\n"
16111             "int         oneTwoThree = 123;",
16112             format("float const a = 5;\n"
16113                    "\n"
16114                    "// line comment\n"
16115                    "int oneTwoThree=123;",
16116                    Alignment));
16117 }
16118 
16119 TEST_F(FormatTest, AlignConsecutiveBitFieldsAcrossEmptyLinesAndComments) {
16120   FormatStyle Alignment = getLLVMStyle();
16121   Alignment.AlignConsecutiveBitFields =
16122       FormatStyle::ACS_AcrossEmptyLinesAndComments;
16123 
16124   Alignment.MaxEmptyLinesToKeep = 10;
16125   /* Test alignment across empty lines */
16126   EXPECT_EQ("int a            : 5;\n"
16127             "\n"
16128             "int longbitfield : 6;",
16129             format("int a : 5;\n"
16130                    "\n"
16131                    "int longbitfield : 6;",
16132                    Alignment));
16133   EXPECT_EQ("int a            : 5;\n"
16134             "int one          : 1;\n"
16135             "\n"
16136             "int longbitfield : 6;",
16137             format("int a : 5;\n"
16138                    "int one : 1;\n"
16139                    "\n"
16140                    "int longbitfield : 6;",
16141                    Alignment));
16142 
16143   /* Test across comments */
16144   EXPECT_EQ("int a            : 5;\n"
16145             "/* block comment */\n"
16146             "int longbitfield : 6;",
16147             format("int a : 5;\n"
16148                    "/* block comment */\n"
16149                    "int longbitfield : 6;",
16150                    Alignment));
16151   EXPECT_EQ("int a            : 5;\n"
16152             "int one          : 1;\n"
16153             "// line comment\n"
16154             "int longbitfield : 6;",
16155             format("int a : 5;\n"
16156                    "int one : 1;\n"
16157                    "// line comment\n"
16158                    "int longbitfield : 6;",
16159                    Alignment));
16160 
16161   /* Test across comments and newlines */
16162   EXPECT_EQ("int a            : 5;\n"
16163             "/* block comment */\n"
16164             "\n"
16165             "int longbitfield : 6;",
16166             format("int a : 5;\n"
16167                    "/* block comment */\n"
16168                    "\n"
16169                    "int longbitfield : 6;",
16170                    Alignment));
16171   EXPECT_EQ("int a            : 5;\n"
16172             "int one          : 1;\n"
16173             "\n"
16174             "// line comment\n"
16175             "\n"
16176             "int longbitfield : 6;",
16177             format("int a : 5;\n"
16178                    "int one : 1;\n"
16179                    "\n"
16180                    "// line comment \n"
16181                    "\n"
16182                    "int longbitfield : 6;",
16183                    Alignment));
16184 }
16185 
16186 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossComments) {
16187   FormatStyle Alignment = getLLVMStyle();
16188   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
16189   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_AcrossComments;
16190 
16191   Alignment.MaxEmptyLinesToKeep = 10;
16192   /* Test alignment across empty lines */
16193   EXPECT_EQ("int a = 5;\n"
16194             "\n"
16195             "int oneTwoThree = 123;",
16196             format("int a       = 5;\n"
16197                    "\n"
16198                    "int oneTwoThree= 123;",
16199                    Alignment));
16200   EXPECT_EQ("int a   = 5;\n"
16201             "int one = 1;\n"
16202             "\n"
16203             "int oneTwoThree = 123;",
16204             format("int a = 5;\n"
16205                    "int one = 1;\n"
16206                    "\n"
16207                    "int oneTwoThree = 123;",
16208                    Alignment));
16209 
16210   /* Test across comments */
16211   EXPECT_EQ("int a           = 5;\n"
16212             "/* block comment */\n"
16213             "int oneTwoThree = 123;",
16214             format("int a = 5;\n"
16215                    "/* block comment */\n"
16216                    "int oneTwoThree=123;",
16217                    Alignment));
16218 
16219   EXPECT_EQ("int a           = 5;\n"
16220             "// line comment\n"
16221             "int oneTwoThree = 123;",
16222             format("int a = 5;\n"
16223                    "// line comment\n"
16224                    "int oneTwoThree=123;",
16225                    Alignment));
16226 
16227   EXPECT_EQ("int a           = 5;\n"
16228             "/*\n"
16229             " * multi-line block comment\n"
16230             " */\n"
16231             "int oneTwoThree = 123;",
16232             format("int a = 5;\n"
16233                    "/*\n"
16234                    " * multi-line block comment\n"
16235                    " */\n"
16236                    "int oneTwoThree=123;",
16237                    Alignment));
16238 
16239   EXPECT_EQ("int a           = 5;\n"
16240             "//\n"
16241             "// multi-line line comment\n"
16242             "//\n"
16243             "int oneTwoThree = 123;",
16244             format("int a = 5;\n"
16245                    "//\n"
16246                    "// multi-line line comment\n"
16247                    "//\n"
16248                    "int oneTwoThree=123;",
16249                    Alignment));
16250 
16251   /* Test across comments and newlines */
16252   EXPECT_EQ("int a = 5;\n"
16253             "\n"
16254             "/* block comment */\n"
16255             "int oneTwoThree = 123;",
16256             format("int a = 5;\n"
16257                    "\n"
16258                    "/* block comment */\n"
16259                    "int oneTwoThree=123;",
16260                    Alignment));
16261 
16262   EXPECT_EQ("int a = 5;\n"
16263             "\n"
16264             "// line comment\n"
16265             "int oneTwoThree = 123;",
16266             format("int a = 5;\n"
16267                    "\n"
16268                    "// line comment\n"
16269                    "int oneTwoThree=123;",
16270                    Alignment));
16271 }
16272 
16273 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLinesAndComments) {
16274   FormatStyle Alignment = getLLVMStyle();
16275   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
16276   Alignment.AlignConsecutiveAssignments =
16277       FormatStyle::ACS_AcrossEmptyLinesAndComments;
16278   verifyFormat("int a           = 5;\n"
16279                "int oneTwoThree = 123;",
16280                Alignment);
16281   verifyFormat("int a           = method();\n"
16282                "int oneTwoThree = 133;",
16283                Alignment);
16284   verifyFormat("a &= 5;\n"
16285                "bcd *= 5;\n"
16286                "ghtyf += 5;\n"
16287                "dvfvdb -= 5;\n"
16288                "a /= 5;\n"
16289                "vdsvsv %= 5;\n"
16290                "sfdbddfbdfbb ^= 5;\n"
16291                "dvsdsv |= 5;\n"
16292                "int dsvvdvsdvvv = 123;",
16293                Alignment);
16294   verifyFormat("int i = 1, j = 10;\n"
16295                "something = 2000;",
16296                Alignment);
16297   verifyFormat("something = 2000;\n"
16298                "int i = 1, j = 10;\n",
16299                Alignment);
16300   verifyFormat("something = 2000;\n"
16301                "another   = 911;\n"
16302                "int i = 1, j = 10;\n"
16303                "oneMore = 1;\n"
16304                "i       = 2;",
16305                Alignment);
16306   verifyFormat("int a   = 5;\n"
16307                "int one = 1;\n"
16308                "method();\n"
16309                "int oneTwoThree = 123;\n"
16310                "int oneTwo      = 12;",
16311                Alignment);
16312   verifyFormat("int oneTwoThree = 123;\n"
16313                "int oneTwo      = 12;\n"
16314                "method();\n",
16315                Alignment);
16316   verifyFormat("int oneTwoThree = 123; // comment\n"
16317                "int oneTwo      = 12;  // comment",
16318                Alignment);
16319 
16320   // Bug 25167
16321   /* Uncomment when fixed
16322     verifyFormat("#if A\n"
16323                  "#else\n"
16324                  "int aaaaaaaa = 12;\n"
16325                  "#endif\n"
16326                  "#if B\n"
16327                  "#else\n"
16328                  "int a = 12;\n"
16329                  "#endif\n",
16330                  Alignment);
16331     verifyFormat("enum foo {\n"
16332                  "#if A\n"
16333                  "#else\n"
16334                  "  aaaaaaaa = 12;\n"
16335                  "#endif\n"
16336                  "#if B\n"
16337                  "#else\n"
16338                  "  a = 12;\n"
16339                  "#endif\n"
16340                  "};\n",
16341                  Alignment);
16342   */
16343 
16344   Alignment.MaxEmptyLinesToKeep = 10;
16345   /* Test alignment across empty lines */
16346   EXPECT_EQ("int a           = 5;\n"
16347             "\n"
16348             "int oneTwoThree = 123;",
16349             format("int a       = 5;\n"
16350                    "\n"
16351                    "int oneTwoThree= 123;",
16352                    Alignment));
16353   EXPECT_EQ("int a           = 5;\n"
16354             "int one         = 1;\n"
16355             "\n"
16356             "int oneTwoThree = 123;",
16357             format("int a = 5;\n"
16358                    "int one = 1;\n"
16359                    "\n"
16360                    "int oneTwoThree = 123;",
16361                    Alignment));
16362   EXPECT_EQ("int a           = 5;\n"
16363             "int one         = 1;\n"
16364             "\n"
16365             "int oneTwoThree = 123;\n"
16366             "int oneTwo      = 12;",
16367             format("int a = 5;\n"
16368                    "int one = 1;\n"
16369                    "\n"
16370                    "int oneTwoThree = 123;\n"
16371                    "int oneTwo = 12;",
16372                    Alignment));
16373 
16374   /* Test across comments */
16375   EXPECT_EQ("int a           = 5;\n"
16376             "/* block comment */\n"
16377             "int oneTwoThree = 123;",
16378             format("int a = 5;\n"
16379                    "/* block comment */\n"
16380                    "int oneTwoThree=123;",
16381                    Alignment));
16382 
16383   EXPECT_EQ("int a           = 5;\n"
16384             "// line comment\n"
16385             "int oneTwoThree = 123;",
16386             format("int a = 5;\n"
16387                    "// line comment\n"
16388                    "int oneTwoThree=123;",
16389                    Alignment));
16390 
16391   /* Test across comments and newlines */
16392   EXPECT_EQ("int a           = 5;\n"
16393             "\n"
16394             "/* block comment */\n"
16395             "int oneTwoThree = 123;",
16396             format("int a = 5;\n"
16397                    "\n"
16398                    "/* block comment */\n"
16399                    "int oneTwoThree=123;",
16400                    Alignment));
16401 
16402   EXPECT_EQ("int a           = 5;\n"
16403             "\n"
16404             "// line comment\n"
16405             "int oneTwoThree = 123;",
16406             format("int a = 5;\n"
16407                    "\n"
16408                    "// line comment\n"
16409                    "int oneTwoThree=123;",
16410                    Alignment));
16411 
16412   EXPECT_EQ("int a           = 5;\n"
16413             "//\n"
16414             "// multi-line line comment\n"
16415             "//\n"
16416             "int oneTwoThree = 123;",
16417             format("int a = 5;\n"
16418                    "//\n"
16419                    "// multi-line line comment\n"
16420                    "//\n"
16421                    "int oneTwoThree=123;",
16422                    Alignment));
16423 
16424   EXPECT_EQ("int a           = 5;\n"
16425             "/*\n"
16426             " *  multi-line block comment\n"
16427             " */\n"
16428             "int oneTwoThree = 123;",
16429             format("int a = 5;\n"
16430                    "/*\n"
16431                    " *  multi-line block comment\n"
16432                    " */\n"
16433                    "int oneTwoThree=123;",
16434                    Alignment));
16435 
16436   EXPECT_EQ("int a           = 5;\n"
16437             "\n"
16438             "/* block comment */\n"
16439             "\n"
16440             "\n"
16441             "\n"
16442             "int oneTwoThree = 123;",
16443             format("int a = 5;\n"
16444                    "\n"
16445                    "/* block comment */\n"
16446                    "\n"
16447                    "\n"
16448                    "\n"
16449                    "int oneTwoThree=123;",
16450                    Alignment));
16451 
16452   EXPECT_EQ("int a           = 5;\n"
16453             "\n"
16454             "// line comment\n"
16455             "\n"
16456             "\n"
16457             "\n"
16458             "int oneTwoThree = 123;",
16459             format("int a = 5;\n"
16460                    "\n"
16461                    "// line comment\n"
16462                    "\n"
16463                    "\n"
16464                    "\n"
16465                    "int oneTwoThree=123;",
16466                    Alignment));
16467 
16468   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
16469   verifyFormat("#define A \\\n"
16470                "  int aaaa       = 12; \\\n"
16471                "  int b          = 23; \\\n"
16472                "  int ccc        = 234; \\\n"
16473                "  int dddddddddd = 2345;",
16474                Alignment);
16475   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
16476   verifyFormat("#define A               \\\n"
16477                "  int aaaa       = 12;  \\\n"
16478                "  int b          = 23;  \\\n"
16479                "  int ccc        = 234; \\\n"
16480                "  int dddddddddd = 2345;",
16481                Alignment);
16482   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
16483   verifyFormat("#define A                                                      "
16484                "                \\\n"
16485                "  int aaaa       = 12;                                         "
16486                "                \\\n"
16487                "  int b          = 23;                                         "
16488                "                \\\n"
16489                "  int ccc        = 234;                                        "
16490                "                \\\n"
16491                "  int dddddddddd = 2345;",
16492                Alignment);
16493   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
16494                "k = 4, int l = 5,\n"
16495                "                  int m = 6) {\n"
16496                "  int j      = 10;\n"
16497                "  otherThing = 1;\n"
16498                "}",
16499                Alignment);
16500   verifyFormat("void SomeFunction(int parameter = 0) {\n"
16501                "  int i   = 1;\n"
16502                "  int j   = 2;\n"
16503                "  int big = 10000;\n"
16504                "}",
16505                Alignment);
16506   verifyFormat("class C {\n"
16507                "public:\n"
16508                "  int i            = 1;\n"
16509                "  virtual void f() = 0;\n"
16510                "};",
16511                Alignment);
16512   verifyFormat("int i = 1;\n"
16513                "if (SomeType t = getSomething()) {\n"
16514                "}\n"
16515                "int j   = 2;\n"
16516                "int big = 10000;",
16517                Alignment);
16518   verifyFormat("int j = 7;\n"
16519                "for (int k = 0; k < N; ++k) {\n"
16520                "}\n"
16521                "int j   = 2;\n"
16522                "int big = 10000;\n"
16523                "}",
16524                Alignment);
16525   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
16526   verifyFormat("int i = 1;\n"
16527                "LooooooooooongType loooooooooooooooooooooongVariable\n"
16528                "    = someLooooooooooooooooongFunction();\n"
16529                "int j = 2;",
16530                Alignment);
16531   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
16532   verifyFormat("int i = 1;\n"
16533                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
16534                "    someLooooooooooooooooongFunction();\n"
16535                "int j = 2;",
16536                Alignment);
16537 
16538   verifyFormat("auto lambda = []() {\n"
16539                "  auto i = 0;\n"
16540                "  return 0;\n"
16541                "};\n"
16542                "int i  = 0;\n"
16543                "auto v = type{\n"
16544                "    i = 1,   //\n"
16545                "    (i = 2), //\n"
16546                "    i = 3    //\n"
16547                "};",
16548                Alignment);
16549 
16550   verifyFormat(
16551       "int i      = 1;\n"
16552       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
16553       "                          loooooooooooooooooooooongParameterB);\n"
16554       "int j      = 2;",
16555       Alignment);
16556 
16557   verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
16558                "          typename B   = very_long_type_name_1,\n"
16559                "          typename T_2 = very_long_type_name_2>\n"
16560                "auto foo() {}\n",
16561                Alignment);
16562   verifyFormat("int a, b = 1;\n"
16563                "int c  = 2;\n"
16564                "int dd = 3;\n",
16565                Alignment);
16566   verifyFormat("int aa       = ((1 > 2) ? 3 : 4);\n"
16567                "float b[1][] = {{3.f}};\n",
16568                Alignment);
16569   verifyFormat("for (int i = 0; i < 1; i++)\n"
16570                "  int x = 1;\n",
16571                Alignment);
16572   verifyFormat("for (i = 0; i < 1; i++)\n"
16573                "  x = 1;\n"
16574                "y = 1;\n",
16575                Alignment);
16576 
16577   Alignment.ReflowComments = true;
16578   Alignment.ColumnLimit = 50;
16579   EXPECT_EQ("int x   = 0;\n"
16580             "int yy  = 1; /// specificlennospace\n"
16581             "int zzz = 2;\n",
16582             format("int x   = 0;\n"
16583                    "int yy  = 1; ///specificlennospace\n"
16584                    "int zzz = 2;\n",
16585                    Alignment));
16586 }
16587 
16588 TEST_F(FormatTest, AlignConsecutiveAssignments) {
16589   FormatStyle Alignment = getLLVMStyle();
16590   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
16591   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
16592   verifyFormat("int a = 5;\n"
16593                "int oneTwoThree = 123;",
16594                Alignment);
16595   verifyFormat("int a = 5;\n"
16596                "int oneTwoThree = 123;",
16597                Alignment);
16598 
16599   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16600   verifyFormat("int a           = 5;\n"
16601                "int oneTwoThree = 123;",
16602                Alignment);
16603   verifyFormat("int a           = method();\n"
16604                "int oneTwoThree = 133;",
16605                Alignment);
16606   verifyFormat("a &= 5;\n"
16607                "bcd *= 5;\n"
16608                "ghtyf += 5;\n"
16609                "dvfvdb -= 5;\n"
16610                "a /= 5;\n"
16611                "vdsvsv %= 5;\n"
16612                "sfdbddfbdfbb ^= 5;\n"
16613                "dvsdsv |= 5;\n"
16614                "int dsvvdvsdvvv = 123;",
16615                Alignment);
16616   verifyFormat("int i = 1, j = 10;\n"
16617                "something = 2000;",
16618                Alignment);
16619   verifyFormat("something = 2000;\n"
16620                "int i = 1, j = 10;\n",
16621                Alignment);
16622   verifyFormat("something = 2000;\n"
16623                "another   = 911;\n"
16624                "int i = 1, j = 10;\n"
16625                "oneMore = 1;\n"
16626                "i       = 2;",
16627                Alignment);
16628   verifyFormat("int a   = 5;\n"
16629                "int one = 1;\n"
16630                "method();\n"
16631                "int oneTwoThree = 123;\n"
16632                "int oneTwo      = 12;",
16633                Alignment);
16634   verifyFormat("int oneTwoThree = 123;\n"
16635                "int oneTwo      = 12;\n"
16636                "method();\n",
16637                Alignment);
16638   verifyFormat("int oneTwoThree = 123; // comment\n"
16639                "int oneTwo      = 12;  // comment",
16640                Alignment);
16641   verifyFormat("int f()         = default;\n"
16642                "int &operator() = default;\n"
16643                "int &operator=() {",
16644                Alignment);
16645   verifyFormat("int f()         = delete;\n"
16646                "int &operator() = delete;\n"
16647                "int &operator=() {",
16648                Alignment);
16649   verifyFormat("int f()         = default; // comment\n"
16650                "int &operator() = default; // comment\n"
16651                "int &operator=() {",
16652                Alignment);
16653   verifyFormat("int f()         = default;\n"
16654                "int &operator() = default;\n"
16655                "int &operator==() {",
16656                Alignment);
16657   verifyFormat("int f()         = default;\n"
16658                "int &operator() = default;\n"
16659                "int &operator<=() {",
16660                Alignment);
16661   verifyFormat("int f()         = default;\n"
16662                "int &operator() = default;\n"
16663                "int &operator!=() {",
16664                Alignment);
16665   verifyFormat("int f()         = default;\n"
16666                "int &operator() = default;\n"
16667                "int &operator=();",
16668                Alignment);
16669   verifyFormat("int f()         = delete;\n"
16670                "int &operator() = delete;\n"
16671                "int &operator=();",
16672                Alignment);
16673   verifyFormat("/* long long padding */ int f() = default;\n"
16674                "int &operator()                 = default;\n"
16675                "int &operator/**/ =();",
16676                Alignment);
16677   // https://llvm.org/PR33697
16678   FormatStyle AlignmentWithPenalty = getLLVMStyle();
16679   AlignmentWithPenalty.AlignConsecutiveAssignments =
16680       FormatStyle::ACS_Consecutive;
16681   AlignmentWithPenalty.PenaltyReturnTypeOnItsOwnLine = 5000;
16682   verifyFormat("class SSSSSSSSSSSSSSSSSSSSSSSSSSSS {\n"
16683                "  void f() = delete;\n"
16684                "  SSSSSSSSSSSSSSSSSSSSSSSSSSSS &operator=(\n"
16685                "      const SSSSSSSSSSSSSSSSSSSSSSSSSSSS &other) = delete;\n"
16686                "};\n",
16687                AlignmentWithPenalty);
16688 
16689   // Bug 25167
16690   /* Uncomment when fixed
16691     verifyFormat("#if A\n"
16692                  "#else\n"
16693                  "int aaaaaaaa = 12;\n"
16694                  "#endif\n"
16695                  "#if B\n"
16696                  "#else\n"
16697                  "int a = 12;\n"
16698                  "#endif\n",
16699                  Alignment);
16700     verifyFormat("enum foo {\n"
16701                  "#if A\n"
16702                  "#else\n"
16703                  "  aaaaaaaa = 12;\n"
16704                  "#endif\n"
16705                  "#if B\n"
16706                  "#else\n"
16707                  "  a = 12;\n"
16708                  "#endif\n"
16709                  "};\n",
16710                  Alignment);
16711   */
16712 
16713   EXPECT_EQ("int a = 5;\n"
16714             "\n"
16715             "int oneTwoThree = 123;",
16716             format("int a       = 5;\n"
16717                    "\n"
16718                    "int oneTwoThree= 123;",
16719                    Alignment));
16720   EXPECT_EQ("int a   = 5;\n"
16721             "int one = 1;\n"
16722             "\n"
16723             "int oneTwoThree = 123;",
16724             format("int a = 5;\n"
16725                    "int one = 1;\n"
16726                    "\n"
16727                    "int oneTwoThree = 123;",
16728                    Alignment));
16729   EXPECT_EQ("int a   = 5;\n"
16730             "int one = 1;\n"
16731             "\n"
16732             "int oneTwoThree = 123;\n"
16733             "int oneTwo      = 12;",
16734             format("int a = 5;\n"
16735                    "int one = 1;\n"
16736                    "\n"
16737                    "int oneTwoThree = 123;\n"
16738                    "int oneTwo = 12;",
16739                    Alignment));
16740   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
16741   verifyFormat("#define A \\\n"
16742                "  int aaaa       = 12; \\\n"
16743                "  int b          = 23; \\\n"
16744                "  int ccc        = 234; \\\n"
16745                "  int dddddddddd = 2345;",
16746                Alignment);
16747   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
16748   verifyFormat("#define A               \\\n"
16749                "  int aaaa       = 12;  \\\n"
16750                "  int b          = 23;  \\\n"
16751                "  int ccc        = 234; \\\n"
16752                "  int dddddddddd = 2345;",
16753                Alignment);
16754   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
16755   verifyFormat("#define A                                                      "
16756                "                \\\n"
16757                "  int aaaa       = 12;                                         "
16758                "                \\\n"
16759                "  int b          = 23;                                         "
16760                "                \\\n"
16761                "  int ccc        = 234;                                        "
16762                "                \\\n"
16763                "  int dddddddddd = 2345;",
16764                Alignment);
16765   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
16766                "k = 4, int l = 5,\n"
16767                "                  int m = 6) {\n"
16768                "  int j      = 10;\n"
16769                "  otherThing = 1;\n"
16770                "}",
16771                Alignment);
16772   verifyFormat("void SomeFunction(int parameter = 0) {\n"
16773                "  int i   = 1;\n"
16774                "  int j   = 2;\n"
16775                "  int big = 10000;\n"
16776                "}",
16777                Alignment);
16778   verifyFormat("class C {\n"
16779                "public:\n"
16780                "  int i            = 1;\n"
16781                "  virtual void f() = 0;\n"
16782                "};",
16783                Alignment);
16784   verifyFormat("int i = 1;\n"
16785                "if (SomeType t = getSomething()) {\n"
16786                "}\n"
16787                "int j   = 2;\n"
16788                "int big = 10000;",
16789                Alignment);
16790   verifyFormat("int j = 7;\n"
16791                "for (int k = 0; k < N; ++k) {\n"
16792                "}\n"
16793                "int j   = 2;\n"
16794                "int big = 10000;\n"
16795                "}",
16796                Alignment);
16797   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
16798   verifyFormat("int i = 1;\n"
16799                "LooooooooooongType loooooooooooooooooooooongVariable\n"
16800                "    = someLooooooooooooooooongFunction();\n"
16801                "int j = 2;",
16802                Alignment);
16803   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
16804   verifyFormat("int i = 1;\n"
16805                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
16806                "    someLooooooooooooooooongFunction();\n"
16807                "int j = 2;",
16808                Alignment);
16809 
16810   verifyFormat("auto lambda = []() {\n"
16811                "  auto i = 0;\n"
16812                "  return 0;\n"
16813                "};\n"
16814                "int i  = 0;\n"
16815                "auto v = type{\n"
16816                "    i = 1,   //\n"
16817                "    (i = 2), //\n"
16818                "    i = 3    //\n"
16819                "};",
16820                Alignment);
16821 
16822   verifyFormat(
16823       "int i      = 1;\n"
16824       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
16825       "                          loooooooooooooooooooooongParameterB);\n"
16826       "int j      = 2;",
16827       Alignment);
16828 
16829   verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
16830                "          typename B   = very_long_type_name_1,\n"
16831                "          typename T_2 = very_long_type_name_2>\n"
16832                "auto foo() {}\n",
16833                Alignment);
16834   verifyFormat("int a, b = 1;\n"
16835                "int c  = 2;\n"
16836                "int dd = 3;\n",
16837                Alignment);
16838   verifyFormat("int aa       = ((1 > 2) ? 3 : 4);\n"
16839                "float b[1][] = {{3.f}};\n",
16840                Alignment);
16841   verifyFormat("for (int i = 0; i < 1; i++)\n"
16842                "  int x = 1;\n",
16843                Alignment);
16844   verifyFormat("for (i = 0; i < 1; i++)\n"
16845                "  x = 1;\n"
16846                "y = 1;\n",
16847                Alignment);
16848 
16849   EXPECT_EQ(Alignment.ReflowComments, true);
16850   Alignment.ColumnLimit = 50;
16851   EXPECT_EQ("int x   = 0;\n"
16852             "int yy  = 1; /// specificlennospace\n"
16853             "int zzz = 2;\n",
16854             format("int x   = 0;\n"
16855                    "int yy  = 1; ///specificlennospace\n"
16856                    "int zzz = 2;\n",
16857                    Alignment));
16858 
16859   verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n"
16860                "auto b                     = [] {\n"
16861                "  f();\n"
16862                "  return;\n"
16863                "};",
16864                Alignment);
16865   verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n"
16866                "auto b                     = g([] {\n"
16867                "  f();\n"
16868                "  return;\n"
16869                "});",
16870                Alignment);
16871   verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n"
16872                "auto b                     = g(param, [] {\n"
16873                "  f();\n"
16874                "  return;\n"
16875                "});",
16876                Alignment);
16877   verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n"
16878                "auto b                     = [] {\n"
16879                "  if (condition) {\n"
16880                "    return;\n"
16881                "  }\n"
16882                "};",
16883                Alignment);
16884 
16885   verifyFormat("auto b = f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
16886                "           ccc ? aaaaa : bbbbb,\n"
16887                "           dddddddddddddddddddddddddd);",
16888                Alignment);
16889   // FIXME: https://llvm.org/PR53497
16890   // verifyFormat("auto aaaaaaaaaaaa = f();\n"
16891   //              "auto b            = f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
16892   //              "    ccc ? aaaaa : bbbbb,\n"
16893   //              "    dddddddddddddddddddddddddd);",
16894   //              Alignment);
16895 }
16896 
16897 TEST_F(FormatTest, AlignConsecutiveBitFields) {
16898   FormatStyle Alignment = getLLVMStyle();
16899   Alignment.AlignConsecutiveBitFields = FormatStyle::ACS_Consecutive;
16900   verifyFormat("int const a     : 5;\n"
16901                "int oneTwoThree : 23;",
16902                Alignment);
16903 
16904   // Initializers are allowed starting with c++2a
16905   verifyFormat("int const a     : 5 = 1;\n"
16906                "int oneTwoThree : 23 = 0;",
16907                Alignment);
16908 
16909   Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
16910   verifyFormat("int const a           : 5;\n"
16911                "int       oneTwoThree : 23;",
16912                Alignment);
16913 
16914   verifyFormat("int const a           : 5;  // comment\n"
16915                "int       oneTwoThree : 23; // comment",
16916                Alignment);
16917 
16918   verifyFormat("int const a           : 5 = 1;\n"
16919                "int       oneTwoThree : 23 = 0;",
16920                Alignment);
16921 
16922   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16923   verifyFormat("int const a           : 5  = 1;\n"
16924                "int       oneTwoThree : 23 = 0;",
16925                Alignment);
16926   verifyFormat("int const a           : 5  = {1};\n"
16927                "int       oneTwoThree : 23 = 0;",
16928                Alignment);
16929 
16930   Alignment.BitFieldColonSpacing = FormatStyle::BFCS_None;
16931   verifyFormat("int const a          :5;\n"
16932                "int       oneTwoThree:23;",
16933                Alignment);
16934 
16935   Alignment.BitFieldColonSpacing = FormatStyle::BFCS_Before;
16936   verifyFormat("int const a           :5;\n"
16937                "int       oneTwoThree :23;",
16938                Alignment);
16939 
16940   Alignment.BitFieldColonSpacing = FormatStyle::BFCS_After;
16941   verifyFormat("int const a          : 5;\n"
16942                "int       oneTwoThree: 23;",
16943                Alignment);
16944 
16945   // Known limitations: ':' is only recognized as a bitfield colon when
16946   // followed by a number.
16947   /*
16948   verifyFormat("int oneTwoThree : SOME_CONSTANT;\n"
16949                "int a           : 5;",
16950                Alignment);
16951   */
16952 }
16953 
16954 TEST_F(FormatTest, AlignConsecutiveDeclarations) {
16955   FormatStyle Alignment = getLLVMStyle();
16956   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
16957   Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_None;
16958   Alignment.PointerAlignment = FormatStyle::PAS_Right;
16959   verifyFormat("float const a = 5;\n"
16960                "int oneTwoThree = 123;",
16961                Alignment);
16962   verifyFormat("int a = 5;\n"
16963                "float const oneTwoThree = 123;",
16964                Alignment);
16965 
16966   Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
16967   verifyFormat("float const a = 5;\n"
16968                "int         oneTwoThree = 123;",
16969                Alignment);
16970   verifyFormat("int         a = method();\n"
16971                "float const oneTwoThree = 133;",
16972                Alignment);
16973   verifyFormat("int i = 1, j = 10;\n"
16974                "something = 2000;",
16975                Alignment);
16976   verifyFormat("something = 2000;\n"
16977                "int i = 1, j = 10;\n",
16978                Alignment);
16979   verifyFormat("float      something = 2000;\n"
16980                "double     another = 911;\n"
16981                "int        i = 1, j = 10;\n"
16982                "const int *oneMore = 1;\n"
16983                "unsigned   i = 2;",
16984                Alignment);
16985   verifyFormat("float a = 5;\n"
16986                "int   one = 1;\n"
16987                "method();\n"
16988                "const double       oneTwoThree = 123;\n"
16989                "const unsigned int oneTwo = 12;",
16990                Alignment);
16991   verifyFormat("int      oneTwoThree{0}; // comment\n"
16992                "unsigned oneTwo;         // comment",
16993                Alignment);
16994   verifyFormat("unsigned int       *a;\n"
16995                "int                *b;\n"
16996                "unsigned int Const *c;\n"
16997                "unsigned int const *d;\n"
16998                "unsigned int Const &e;\n"
16999                "unsigned int const &f;",
17000                Alignment);
17001   verifyFormat("Const unsigned int *c;\n"
17002                "const unsigned int *d;\n"
17003                "Const unsigned int &e;\n"
17004                "const unsigned int &f;\n"
17005                "const unsigned      g;\n"
17006                "Const unsigned      h;",
17007                Alignment);
17008   EXPECT_EQ("float const a = 5;\n"
17009             "\n"
17010             "int oneTwoThree = 123;",
17011             format("float const   a = 5;\n"
17012                    "\n"
17013                    "int           oneTwoThree= 123;",
17014                    Alignment));
17015   EXPECT_EQ("float a = 5;\n"
17016             "int   one = 1;\n"
17017             "\n"
17018             "unsigned oneTwoThree = 123;",
17019             format("float    a = 5;\n"
17020                    "int      one = 1;\n"
17021                    "\n"
17022                    "unsigned oneTwoThree = 123;",
17023                    Alignment));
17024   EXPECT_EQ("float a = 5;\n"
17025             "int   one = 1;\n"
17026             "\n"
17027             "unsigned oneTwoThree = 123;\n"
17028             "int      oneTwo = 12;",
17029             format("float    a = 5;\n"
17030                    "int one = 1;\n"
17031                    "\n"
17032                    "unsigned oneTwoThree = 123;\n"
17033                    "int oneTwo = 12;",
17034                    Alignment));
17035   // Function prototype alignment
17036   verifyFormat("int    a();\n"
17037                "double b();",
17038                Alignment);
17039   verifyFormat("int    a(int x);\n"
17040                "double b();",
17041                Alignment);
17042   unsigned OldColumnLimit = Alignment.ColumnLimit;
17043   // We need to set ColumnLimit to zero, in order to stress nested alignments,
17044   // otherwise the function parameters will be re-flowed onto a single line.
17045   Alignment.ColumnLimit = 0;
17046   EXPECT_EQ("int    a(int   x,\n"
17047             "         float y);\n"
17048             "double b(int    x,\n"
17049             "         double y);",
17050             format("int a(int x,\n"
17051                    " float y);\n"
17052                    "double b(int x,\n"
17053                    " double y);",
17054                    Alignment));
17055   // This ensures that function parameters of function declarations are
17056   // correctly indented when their owning functions are indented.
17057   // The failure case here is for 'double y' to not be indented enough.
17058   EXPECT_EQ("double a(int x);\n"
17059             "int    b(int    y,\n"
17060             "         double z);",
17061             format("double a(int x);\n"
17062                    "int b(int y,\n"
17063                    " double z);",
17064                    Alignment));
17065   // Set ColumnLimit low so that we induce wrapping immediately after
17066   // the function name and opening paren.
17067   Alignment.ColumnLimit = 13;
17068   verifyFormat("int function(\n"
17069                "    int  x,\n"
17070                "    bool y);",
17071                Alignment);
17072   Alignment.ColumnLimit = OldColumnLimit;
17073   // Ensure function pointers don't screw up recursive alignment
17074   verifyFormat("int    a(int x, void (*fp)(int y));\n"
17075                "double b();",
17076                Alignment);
17077   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
17078   // Ensure recursive alignment is broken by function braces, so that the
17079   // "a = 1" does not align with subsequent assignments inside the function
17080   // body.
17081   verifyFormat("int func(int a = 1) {\n"
17082                "  int b  = 2;\n"
17083                "  int cc = 3;\n"
17084                "}",
17085                Alignment);
17086   verifyFormat("float      something = 2000;\n"
17087                "double     another   = 911;\n"
17088                "int        i = 1, j = 10;\n"
17089                "const int *oneMore = 1;\n"
17090                "unsigned   i       = 2;",
17091                Alignment);
17092   verifyFormat("int      oneTwoThree = {0}; // comment\n"
17093                "unsigned oneTwo      = 0;   // comment",
17094                Alignment);
17095   // Make sure that scope is correctly tracked, in the absence of braces
17096   verifyFormat("for (int i = 0; i < n; i++)\n"
17097                "  j = i;\n"
17098                "double x = 1;\n",
17099                Alignment);
17100   verifyFormat("if (int i = 0)\n"
17101                "  j = i;\n"
17102                "double x = 1;\n",
17103                Alignment);
17104   // Ensure operator[] and operator() are comprehended
17105   verifyFormat("struct test {\n"
17106                "  long long int foo();\n"
17107                "  int           operator[](int a);\n"
17108                "  double        bar();\n"
17109                "};\n",
17110                Alignment);
17111   verifyFormat("struct test {\n"
17112                "  long long int foo();\n"
17113                "  int           operator()(int a);\n"
17114                "  double        bar();\n"
17115                "};\n",
17116                Alignment);
17117   // http://llvm.org/PR52914
17118   verifyFormat("char *a[]     = {\"a\", // comment\n"
17119                "                 \"bb\"};\n"
17120                "int   bbbbbbb = 0;",
17121                Alignment);
17122 
17123   // PAS_Right
17124   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
17125             "  int const i   = 1;\n"
17126             "  int      *j   = 2;\n"
17127             "  int       big = 10000;\n"
17128             "\n"
17129             "  unsigned oneTwoThree = 123;\n"
17130             "  int      oneTwo      = 12;\n"
17131             "  method();\n"
17132             "  float k  = 2;\n"
17133             "  int   ll = 10000;\n"
17134             "}",
17135             format("void SomeFunction(int parameter= 0) {\n"
17136                    " int const  i= 1;\n"
17137                    "  int *j=2;\n"
17138                    " int big  =  10000;\n"
17139                    "\n"
17140                    "unsigned oneTwoThree  =123;\n"
17141                    "int oneTwo = 12;\n"
17142                    "  method();\n"
17143                    "float k= 2;\n"
17144                    "int ll=10000;\n"
17145                    "}",
17146                    Alignment));
17147   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
17148             "  int const i   = 1;\n"
17149             "  int     **j   = 2, ***k;\n"
17150             "  int      &k   = i;\n"
17151             "  int     &&l   = i + j;\n"
17152             "  int       big = 10000;\n"
17153             "\n"
17154             "  unsigned oneTwoThree = 123;\n"
17155             "  int      oneTwo      = 12;\n"
17156             "  method();\n"
17157             "  float k  = 2;\n"
17158             "  int   ll = 10000;\n"
17159             "}",
17160             format("void SomeFunction(int parameter= 0) {\n"
17161                    " int const  i= 1;\n"
17162                    "  int **j=2,***k;\n"
17163                    "int &k=i;\n"
17164                    "int &&l=i+j;\n"
17165                    " int big  =  10000;\n"
17166                    "\n"
17167                    "unsigned oneTwoThree  =123;\n"
17168                    "int oneTwo = 12;\n"
17169                    "  method();\n"
17170                    "float k= 2;\n"
17171                    "int ll=10000;\n"
17172                    "}",
17173                    Alignment));
17174   // variables are aligned at their name, pointers are at the right most
17175   // position
17176   verifyFormat("int   *a;\n"
17177                "int  **b;\n"
17178                "int ***c;\n"
17179                "int    foobar;\n",
17180                Alignment);
17181 
17182   // PAS_Left
17183   FormatStyle AlignmentLeft = Alignment;
17184   AlignmentLeft.PointerAlignment = FormatStyle::PAS_Left;
17185   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
17186             "  int const i   = 1;\n"
17187             "  int*      j   = 2;\n"
17188             "  int       big = 10000;\n"
17189             "\n"
17190             "  unsigned oneTwoThree = 123;\n"
17191             "  int      oneTwo      = 12;\n"
17192             "  method();\n"
17193             "  float k  = 2;\n"
17194             "  int   ll = 10000;\n"
17195             "}",
17196             format("void SomeFunction(int parameter= 0) {\n"
17197                    " int const  i= 1;\n"
17198                    "  int *j=2;\n"
17199                    " int big  =  10000;\n"
17200                    "\n"
17201                    "unsigned oneTwoThree  =123;\n"
17202                    "int oneTwo = 12;\n"
17203                    "  method();\n"
17204                    "float k= 2;\n"
17205                    "int ll=10000;\n"
17206                    "}",
17207                    AlignmentLeft));
17208   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
17209             "  int const i   = 1;\n"
17210             "  int**     j   = 2;\n"
17211             "  int&      k   = i;\n"
17212             "  int&&     l   = i + j;\n"
17213             "  int       big = 10000;\n"
17214             "\n"
17215             "  unsigned oneTwoThree = 123;\n"
17216             "  int      oneTwo      = 12;\n"
17217             "  method();\n"
17218             "  float k  = 2;\n"
17219             "  int   ll = 10000;\n"
17220             "}",
17221             format("void SomeFunction(int parameter= 0) {\n"
17222                    " int const  i= 1;\n"
17223                    "  int **j=2;\n"
17224                    "int &k=i;\n"
17225                    "int &&l=i+j;\n"
17226                    " int big  =  10000;\n"
17227                    "\n"
17228                    "unsigned oneTwoThree  =123;\n"
17229                    "int oneTwo = 12;\n"
17230                    "  method();\n"
17231                    "float k= 2;\n"
17232                    "int ll=10000;\n"
17233                    "}",
17234                    AlignmentLeft));
17235   // variables are aligned at their name, pointers are at the left most position
17236   verifyFormat("int*   a;\n"
17237                "int**  b;\n"
17238                "int*** c;\n"
17239                "int    foobar;\n",
17240                AlignmentLeft);
17241 
17242   // PAS_Middle
17243   FormatStyle AlignmentMiddle = Alignment;
17244   AlignmentMiddle.PointerAlignment = FormatStyle::PAS_Middle;
17245   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
17246             "  int const i   = 1;\n"
17247             "  int *     j   = 2;\n"
17248             "  int       big = 10000;\n"
17249             "\n"
17250             "  unsigned oneTwoThree = 123;\n"
17251             "  int      oneTwo      = 12;\n"
17252             "  method();\n"
17253             "  float k  = 2;\n"
17254             "  int   ll = 10000;\n"
17255             "}",
17256             format("void SomeFunction(int parameter= 0) {\n"
17257                    " int const  i= 1;\n"
17258                    "  int *j=2;\n"
17259                    " int big  =  10000;\n"
17260                    "\n"
17261                    "unsigned oneTwoThree  =123;\n"
17262                    "int oneTwo = 12;\n"
17263                    "  method();\n"
17264                    "float k= 2;\n"
17265                    "int ll=10000;\n"
17266                    "}",
17267                    AlignmentMiddle));
17268   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
17269             "  int const i   = 1;\n"
17270             "  int **    j   = 2, ***k;\n"
17271             "  int &     k   = i;\n"
17272             "  int &&    l   = i + j;\n"
17273             "  int       big = 10000;\n"
17274             "\n"
17275             "  unsigned oneTwoThree = 123;\n"
17276             "  int      oneTwo      = 12;\n"
17277             "  method();\n"
17278             "  float k  = 2;\n"
17279             "  int   ll = 10000;\n"
17280             "}",
17281             format("void SomeFunction(int parameter= 0) {\n"
17282                    " int const  i= 1;\n"
17283                    "  int **j=2,***k;\n"
17284                    "int &k=i;\n"
17285                    "int &&l=i+j;\n"
17286                    " int big  =  10000;\n"
17287                    "\n"
17288                    "unsigned oneTwoThree  =123;\n"
17289                    "int oneTwo = 12;\n"
17290                    "  method();\n"
17291                    "float k= 2;\n"
17292                    "int ll=10000;\n"
17293                    "}",
17294                    AlignmentMiddle));
17295   // variables are aligned at their name, pointers are in the middle
17296   verifyFormat("int *   a;\n"
17297                "int *   b;\n"
17298                "int *** c;\n"
17299                "int     foobar;\n",
17300                AlignmentMiddle);
17301 
17302   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
17303   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
17304   verifyFormat("#define A \\\n"
17305                "  int       aaaa = 12; \\\n"
17306                "  float     b = 23; \\\n"
17307                "  const int ccc = 234; \\\n"
17308                "  unsigned  dddddddddd = 2345;",
17309                Alignment);
17310   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
17311   verifyFormat("#define A              \\\n"
17312                "  int       aaaa = 12; \\\n"
17313                "  float     b = 23;    \\\n"
17314                "  const int ccc = 234; \\\n"
17315                "  unsigned  dddddddddd = 2345;",
17316                Alignment);
17317   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
17318   Alignment.ColumnLimit = 30;
17319   verifyFormat("#define A                    \\\n"
17320                "  int       aaaa = 12;       \\\n"
17321                "  float     b = 23;          \\\n"
17322                "  const int ccc = 234;       \\\n"
17323                "  int       dddddddddd = 2345;",
17324                Alignment);
17325   Alignment.ColumnLimit = 80;
17326   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
17327                "k = 4, int l = 5,\n"
17328                "                  int m = 6) {\n"
17329                "  const int j = 10;\n"
17330                "  otherThing = 1;\n"
17331                "}",
17332                Alignment);
17333   verifyFormat("void SomeFunction(int parameter = 0) {\n"
17334                "  int const i = 1;\n"
17335                "  int      *j = 2;\n"
17336                "  int       big = 10000;\n"
17337                "}",
17338                Alignment);
17339   verifyFormat("class C {\n"
17340                "public:\n"
17341                "  int          i = 1;\n"
17342                "  virtual void f() = 0;\n"
17343                "};",
17344                Alignment);
17345   verifyFormat("float i = 1;\n"
17346                "if (SomeType t = getSomething()) {\n"
17347                "}\n"
17348                "const unsigned j = 2;\n"
17349                "int            big = 10000;",
17350                Alignment);
17351   verifyFormat("float j = 7;\n"
17352                "for (int k = 0; k < N; ++k) {\n"
17353                "}\n"
17354                "unsigned j = 2;\n"
17355                "int      big = 10000;\n"
17356                "}",
17357                Alignment);
17358   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
17359   verifyFormat("float              i = 1;\n"
17360                "LooooooooooongType loooooooooooooooooooooongVariable\n"
17361                "    = someLooooooooooooooooongFunction();\n"
17362                "int j = 2;",
17363                Alignment);
17364   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
17365   verifyFormat("int                i = 1;\n"
17366                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
17367                "    someLooooooooooooooooongFunction();\n"
17368                "int j = 2;",
17369                Alignment);
17370 
17371   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
17372   verifyFormat("auto lambda = []() {\n"
17373                "  auto  ii = 0;\n"
17374                "  float j  = 0;\n"
17375                "  return 0;\n"
17376                "};\n"
17377                "int   i  = 0;\n"
17378                "float i2 = 0;\n"
17379                "auto  v  = type{\n"
17380                "    i = 1,   //\n"
17381                "    (i = 2), //\n"
17382                "    i = 3    //\n"
17383                "};",
17384                Alignment);
17385   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
17386 
17387   verifyFormat(
17388       "int      i = 1;\n"
17389       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
17390       "                          loooooooooooooooooooooongParameterB);\n"
17391       "int      j = 2;",
17392       Alignment);
17393 
17394   // Test interactions with ColumnLimit and AlignConsecutiveAssignments:
17395   // We expect declarations and assignments to align, as long as it doesn't
17396   // exceed the column limit, starting a new alignment sequence whenever it
17397   // happens.
17398   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
17399   Alignment.ColumnLimit = 30;
17400   verifyFormat("float    ii              = 1;\n"
17401                "unsigned j               = 2;\n"
17402                "int someVerylongVariable = 1;\n"
17403                "AnotherLongType  ll = 123456;\n"
17404                "VeryVeryLongType k  = 2;\n"
17405                "int              myvar = 1;",
17406                Alignment);
17407   Alignment.ColumnLimit = 80;
17408   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
17409 
17410   verifyFormat(
17411       "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n"
17412       "          typename LongType, typename B>\n"
17413       "auto foo() {}\n",
17414       Alignment);
17415   verifyFormat("float a, b = 1;\n"
17416                "int   c = 2;\n"
17417                "int   dd = 3;\n",
17418                Alignment);
17419   verifyFormat("int   aa = ((1 > 2) ? 3 : 4);\n"
17420                "float b[1][] = {{3.f}};\n",
17421                Alignment);
17422   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
17423   verifyFormat("float a, b = 1;\n"
17424                "int   c  = 2;\n"
17425                "int   dd = 3;\n",
17426                Alignment);
17427   verifyFormat("int   aa     = ((1 > 2) ? 3 : 4);\n"
17428                "float b[1][] = {{3.f}};\n",
17429                Alignment);
17430   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
17431 
17432   Alignment.ColumnLimit = 30;
17433   Alignment.BinPackParameters = false;
17434   verifyFormat("void foo(float     a,\n"
17435                "         float     b,\n"
17436                "         int       c,\n"
17437                "         uint32_t *d) {\n"
17438                "  int   *e = 0;\n"
17439                "  float  f = 0;\n"
17440                "  double g = 0;\n"
17441                "}\n"
17442                "void bar(ino_t     a,\n"
17443                "         int       b,\n"
17444                "         uint32_t *c,\n"
17445                "         bool      d) {}\n",
17446                Alignment);
17447   Alignment.BinPackParameters = true;
17448   Alignment.ColumnLimit = 80;
17449 
17450   // Bug 33507
17451   Alignment.PointerAlignment = FormatStyle::PAS_Middle;
17452   verifyFormat(
17453       "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n"
17454       "  static const Version verVs2017;\n"
17455       "  return true;\n"
17456       "});\n",
17457       Alignment);
17458   Alignment.PointerAlignment = FormatStyle::PAS_Right;
17459 
17460   // See llvm.org/PR35641
17461   Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
17462   verifyFormat("int func() { //\n"
17463                "  int      b;\n"
17464                "  unsigned c;\n"
17465                "}",
17466                Alignment);
17467 
17468   // See PR37175
17469   FormatStyle Style = getMozillaStyle();
17470   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
17471   EXPECT_EQ("DECOR1 /**/ int8_t /**/ DECOR2 /**/\n"
17472             "foo(int a);",
17473             format("DECOR1 /**/ int8_t /**/ DECOR2 /**/ foo (int a);", Style));
17474 
17475   Alignment.PointerAlignment = FormatStyle::PAS_Left;
17476   verifyFormat("unsigned int*       a;\n"
17477                "int*                b;\n"
17478                "unsigned int Const* c;\n"
17479                "unsigned int const* d;\n"
17480                "unsigned int Const& e;\n"
17481                "unsigned int const& f;",
17482                Alignment);
17483   verifyFormat("Const unsigned int* c;\n"
17484                "const unsigned int* d;\n"
17485                "Const unsigned int& e;\n"
17486                "const unsigned int& f;\n"
17487                "const unsigned      g;\n"
17488                "Const unsigned      h;",
17489                Alignment);
17490 
17491   Alignment.PointerAlignment = FormatStyle::PAS_Middle;
17492   verifyFormat("unsigned int *       a;\n"
17493                "int *                b;\n"
17494                "unsigned int Const * c;\n"
17495                "unsigned int const * d;\n"
17496                "unsigned int Const & e;\n"
17497                "unsigned int const & f;",
17498                Alignment);
17499   verifyFormat("Const unsigned int * c;\n"
17500                "const unsigned int * d;\n"
17501                "Const unsigned int & e;\n"
17502                "const unsigned int & f;\n"
17503                "const unsigned       g;\n"
17504                "Const unsigned       h;",
17505                Alignment);
17506 
17507   // See PR46529
17508   FormatStyle BracedAlign = getLLVMStyle();
17509   BracedAlign.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
17510   verifyFormat("const auto result{[]() {\n"
17511                "  const auto something = 1;\n"
17512                "  return 2;\n"
17513                "}};",
17514                BracedAlign);
17515   verifyFormat("int foo{[]() {\n"
17516                "  int bar{0};\n"
17517                "  return 0;\n"
17518                "}()};",
17519                BracedAlign);
17520   BracedAlign.Cpp11BracedListStyle = false;
17521   verifyFormat("const auto result{ []() {\n"
17522                "  const auto something = 1;\n"
17523                "  return 2;\n"
17524                "} };",
17525                BracedAlign);
17526   verifyFormat("int foo{ []() {\n"
17527                "  int bar{ 0 };\n"
17528                "  return 0;\n"
17529                "}() };",
17530                BracedAlign);
17531 }
17532 
17533 TEST_F(FormatTest, AlignWithLineBreaks) {
17534   auto Style = getLLVMStyleWithColumns(120);
17535 
17536   EXPECT_EQ(Style.AlignConsecutiveAssignments, FormatStyle::ACS_None);
17537   EXPECT_EQ(Style.AlignConsecutiveDeclarations, FormatStyle::ACS_None);
17538   verifyFormat("void foo() {\n"
17539                "  int myVar = 5;\n"
17540                "  double x = 3.14;\n"
17541                "  auto str = \"Hello \"\n"
17542                "             \"World\";\n"
17543                "  auto s = \"Hello \"\n"
17544                "           \"Again\";\n"
17545                "}",
17546                Style);
17547 
17548   // clang-format off
17549   verifyFormat("void foo() {\n"
17550                "  const int capacityBefore = Entries.capacity();\n"
17551                "  const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
17552                "                                            std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
17553                "  const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
17554                "                                          std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
17555                "}",
17556                Style);
17557   // clang-format on
17558 
17559   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
17560   verifyFormat("void foo() {\n"
17561                "  int myVar = 5;\n"
17562                "  double x  = 3.14;\n"
17563                "  auto str  = \"Hello \"\n"
17564                "              \"World\";\n"
17565                "  auto s    = \"Hello \"\n"
17566                "              \"Again\";\n"
17567                "}",
17568                Style);
17569 
17570   // clang-format off
17571   verifyFormat("void foo() {\n"
17572                "  const int capacityBefore = Entries.capacity();\n"
17573                "  const auto newEntry      = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
17574                "                                                 std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
17575                "  const X newEntry2        = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
17576                "                                                 std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
17577                "}",
17578                Style);
17579   // clang-format on
17580 
17581   Style.AlignConsecutiveAssignments = FormatStyle::ACS_None;
17582   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
17583   verifyFormat("void foo() {\n"
17584                "  int    myVar = 5;\n"
17585                "  double x = 3.14;\n"
17586                "  auto   str = \"Hello \"\n"
17587                "               \"World\";\n"
17588                "  auto   s = \"Hello \"\n"
17589                "             \"Again\";\n"
17590                "}",
17591                Style);
17592 
17593   // clang-format off
17594   verifyFormat("void foo() {\n"
17595                "  const int  capacityBefore = Entries.capacity();\n"
17596                "  const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
17597                "                                            std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
17598                "  const X    newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
17599                "                                             std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
17600                "}",
17601                Style);
17602   // clang-format on
17603 
17604   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
17605   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
17606 
17607   verifyFormat("void foo() {\n"
17608                "  int    myVar = 5;\n"
17609                "  double x     = 3.14;\n"
17610                "  auto   str   = \"Hello \"\n"
17611                "                 \"World\";\n"
17612                "  auto   s     = \"Hello \"\n"
17613                "                 \"Again\";\n"
17614                "}",
17615                Style);
17616 
17617   // clang-format off
17618   verifyFormat("void foo() {\n"
17619                "  const int  capacityBefore = Entries.capacity();\n"
17620                "  const auto newEntry       = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
17621                "                                                  std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
17622                "  const X    newEntry2      = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
17623                "                                                  std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
17624                "}",
17625                Style);
17626   // clang-format on
17627 
17628   Style = getLLVMStyleWithColumns(120);
17629   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
17630   Style.ContinuationIndentWidth = 4;
17631   Style.IndentWidth = 4;
17632 
17633   // clang-format off
17634   verifyFormat("void SomeFunc() {\n"
17635                "    newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n"
17636                "                                                        seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
17637                "    newWatcher.maxAge     = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n"
17638                "                                                        seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
17639                "    newWatcher.max        = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n"
17640                "                                                        seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
17641                "}",
17642                Style);
17643   // clang-format on
17644 
17645   Style.BinPackArguments = false;
17646 
17647   // clang-format off
17648   verifyFormat("void SomeFunc() {\n"
17649                "    newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(\n"
17650                "        FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
17651                "    newWatcher.maxAge     = ToLegacyTimestamp(GetMaxAge(\n"
17652                "        FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
17653                "    newWatcher.max        = ToLegacyTimestamp(GetMaxAge(\n"
17654                "        FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
17655                "}",
17656                Style);
17657   // clang-format on
17658 }
17659 
17660 TEST_F(FormatTest, AlignWithInitializerPeriods) {
17661   auto Style = getLLVMStyleWithColumns(60);
17662 
17663   verifyFormat("void foo1(void) {\n"
17664                "  BYTE p[1] = 1;\n"
17665                "  A B = {.one_foooooooooooooooo = 2,\n"
17666                "         .two_fooooooooooooo = 3,\n"
17667                "         .three_fooooooooooooo = 4};\n"
17668                "  BYTE payload = 2;\n"
17669                "}",
17670                Style);
17671 
17672   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
17673   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_None;
17674   verifyFormat("void foo2(void) {\n"
17675                "  BYTE p[1]    = 1;\n"
17676                "  A B          = {.one_foooooooooooooooo = 2,\n"
17677                "                  .two_fooooooooooooo    = 3,\n"
17678                "                  .three_fooooooooooooo  = 4};\n"
17679                "  BYTE payload = 2;\n"
17680                "}",
17681                Style);
17682 
17683   Style.AlignConsecutiveAssignments = FormatStyle::ACS_None;
17684   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
17685   verifyFormat("void foo3(void) {\n"
17686                "  BYTE p[1] = 1;\n"
17687                "  A    B = {.one_foooooooooooooooo = 2,\n"
17688                "            .two_fooooooooooooo = 3,\n"
17689                "            .three_fooooooooooooo = 4};\n"
17690                "  BYTE payload = 2;\n"
17691                "}",
17692                Style);
17693 
17694   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
17695   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
17696   verifyFormat("void foo4(void) {\n"
17697                "  BYTE p[1]    = 1;\n"
17698                "  A    B       = {.one_foooooooooooooooo = 2,\n"
17699                "                  .two_fooooooooooooo    = 3,\n"
17700                "                  .three_fooooooooooooo  = 4};\n"
17701                "  BYTE payload = 2;\n"
17702                "}",
17703                Style);
17704 }
17705 
17706 TEST_F(FormatTest, LinuxBraceBreaking) {
17707   FormatStyle LinuxBraceStyle = getLLVMStyle();
17708   LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
17709   verifyFormat("namespace a\n"
17710                "{\n"
17711                "class A\n"
17712                "{\n"
17713                "  void f()\n"
17714                "  {\n"
17715                "    if (true) {\n"
17716                "      a();\n"
17717                "      b();\n"
17718                "    } else {\n"
17719                "      a();\n"
17720                "    }\n"
17721                "  }\n"
17722                "  void g() { return; }\n"
17723                "};\n"
17724                "struct B {\n"
17725                "  int x;\n"
17726                "};\n"
17727                "} // namespace a\n",
17728                LinuxBraceStyle);
17729   verifyFormat("enum X {\n"
17730                "  Y = 0,\n"
17731                "}\n",
17732                LinuxBraceStyle);
17733   verifyFormat("struct S {\n"
17734                "  int Type;\n"
17735                "  union {\n"
17736                "    int x;\n"
17737                "    double y;\n"
17738                "  } Value;\n"
17739                "  class C\n"
17740                "  {\n"
17741                "    MyFavoriteType Value;\n"
17742                "  } Class;\n"
17743                "}\n",
17744                LinuxBraceStyle);
17745 }
17746 
17747 TEST_F(FormatTest, MozillaBraceBreaking) {
17748   FormatStyle MozillaBraceStyle = getLLVMStyle();
17749   MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
17750   MozillaBraceStyle.FixNamespaceComments = false;
17751   verifyFormat("namespace a {\n"
17752                "class A\n"
17753                "{\n"
17754                "  void f()\n"
17755                "  {\n"
17756                "    if (true) {\n"
17757                "      a();\n"
17758                "      b();\n"
17759                "    }\n"
17760                "  }\n"
17761                "  void g() { return; }\n"
17762                "};\n"
17763                "enum E\n"
17764                "{\n"
17765                "  A,\n"
17766                "  // foo\n"
17767                "  B,\n"
17768                "  C\n"
17769                "};\n"
17770                "struct B\n"
17771                "{\n"
17772                "  int x;\n"
17773                "};\n"
17774                "}\n",
17775                MozillaBraceStyle);
17776   verifyFormat("struct S\n"
17777                "{\n"
17778                "  int Type;\n"
17779                "  union\n"
17780                "  {\n"
17781                "    int x;\n"
17782                "    double y;\n"
17783                "  } Value;\n"
17784                "  class C\n"
17785                "  {\n"
17786                "    MyFavoriteType Value;\n"
17787                "  } Class;\n"
17788                "}\n",
17789                MozillaBraceStyle);
17790 }
17791 
17792 TEST_F(FormatTest, StroustrupBraceBreaking) {
17793   FormatStyle StroustrupBraceStyle = getLLVMStyle();
17794   StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
17795   verifyFormat("namespace a {\n"
17796                "class A {\n"
17797                "  void f()\n"
17798                "  {\n"
17799                "    if (true) {\n"
17800                "      a();\n"
17801                "      b();\n"
17802                "    }\n"
17803                "  }\n"
17804                "  void g() { return; }\n"
17805                "};\n"
17806                "struct B {\n"
17807                "  int x;\n"
17808                "};\n"
17809                "} // namespace a\n",
17810                StroustrupBraceStyle);
17811 
17812   verifyFormat("void foo()\n"
17813                "{\n"
17814                "  if (a) {\n"
17815                "    a();\n"
17816                "  }\n"
17817                "  else {\n"
17818                "    b();\n"
17819                "  }\n"
17820                "}\n",
17821                StroustrupBraceStyle);
17822 
17823   verifyFormat("#ifdef _DEBUG\n"
17824                "int foo(int i = 0)\n"
17825                "#else\n"
17826                "int foo(int i = 5)\n"
17827                "#endif\n"
17828                "{\n"
17829                "  return i;\n"
17830                "}",
17831                StroustrupBraceStyle);
17832 
17833   verifyFormat("void foo() {}\n"
17834                "void bar()\n"
17835                "#ifdef _DEBUG\n"
17836                "{\n"
17837                "  foo();\n"
17838                "}\n"
17839                "#else\n"
17840                "{\n"
17841                "}\n"
17842                "#endif",
17843                StroustrupBraceStyle);
17844 
17845   verifyFormat("void foobar() { int i = 5; }\n"
17846                "#ifdef _DEBUG\n"
17847                "void bar() {}\n"
17848                "#else\n"
17849                "void bar() { foobar(); }\n"
17850                "#endif",
17851                StroustrupBraceStyle);
17852 }
17853 
17854 TEST_F(FormatTest, AllmanBraceBreaking) {
17855   FormatStyle AllmanBraceStyle = getLLVMStyle();
17856   AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
17857 
17858   EXPECT_EQ("namespace a\n"
17859             "{\n"
17860             "void f();\n"
17861             "void g();\n"
17862             "} // namespace a\n",
17863             format("namespace a\n"
17864                    "{\n"
17865                    "void f();\n"
17866                    "void g();\n"
17867                    "}\n",
17868                    AllmanBraceStyle));
17869 
17870   verifyFormat("namespace a\n"
17871                "{\n"
17872                "class A\n"
17873                "{\n"
17874                "  void f()\n"
17875                "  {\n"
17876                "    if (true)\n"
17877                "    {\n"
17878                "      a();\n"
17879                "      b();\n"
17880                "    }\n"
17881                "  }\n"
17882                "  void g() { return; }\n"
17883                "};\n"
17884                "struct B\n"
17885                "{\n"
17886                "  int x;\n"
17887                "};\n"
17888                "union C\n"
17889                "{\n"
17890                "};\n"
17891                "} // namespace a",
17892                AllmanBraceStyle);
17893 
17894   verifyFormat("void f()\n"
17895                "{\n"
17896                "  if (true)\n"
17897                "  {\n"
17898                "    a();\n"
17899                "  }\n"
17900                "  else if (false)\n"
17901                "  {\n"
17902                "    b();\n"
17903                "  }\n"
17904                "  else\n"
17905                "  {\n"
17906                "    c();\n"
17907                "  }\n"
17908                "}\n",
17909                AllmanBraceStyle);
17910 
17911   verifyFormat("void f()\n"
17912                "{\n"
17913                "  for (int i = 0; i < 10; ++i)\n"
17914                "  {\n"
17915                "    a();\n"
17916                "  }\n"
17917                "  while (false)\n"
17918                "  {\n"
17919                "    b();\n"
17920                "  }\n"
17921                "  do\n"
17922                "  {\n"
17923                "    c();\n"
17924                "  } while (false)\n"
17925                "}\n",
17926                AllmanBraceStyle);
17927 
17928   verifyFormat("void f(int a)\n"
17929                "{\n"
17930                "  switch (a)\n"
17931                "  {\n"
17932                "  case 0:\n"
17933                "    break;\n"
17934                "  case 1:\n"
17935                "  {\n"
17936                "    break;\n"
17937                "  }\n"
17938                "  case 2:\n"
17939                "  {\n"
17940                "  }\n"
17941                "  break;\n"
17942                "  default:\n"
17943                "    break;\n"
17944                "  }\n"
17945                "}\n",
17946                AllmanBraceStyle);
17947 
17948   verifyFormat("enum X\n"
17949                "{\n"
17950                "  Y = 0,\n"
17951                "}\n",
17952                AllmanBraceStyle);
17953   verifyFormat("enum X\n"
17954                "{\n"
17955                "  Y = 0\n"
17956                "}\n",
17957                AllmanBraceStyle);
17958 
17959   verifyFormat("@interface BSApplicationController ()\n"
17960                "{\n"
17961                "@private\n"
17962                "  id _extraIvar;\n"
17963                "}\n"
17964                "@end\n",
17965                AllmanBraceStyle);
17966 
17967   verifyFormat("#ifdef _DEBUG\n"
17968                "int foo(int i = 0)\n"
17969                "#else\n"
17970                "int foo(int i = 5)\n"
17971                "#endif\n"
17972                "{\n"
17973                "  return i;\n"
17974                "}",
17975                AllmanBraceStyle);
17976 
17977   verifyFormat("void foo() {}\n"
17978                "void bar()\n"
17979                "#ifdef _DEBUG\n"
17980                "{\n"
17981                "  foo();\n"
17982                "}\n"
17983                "#else\n"
17984                "{\n"
17985                "}\n"
17986                "#endif",
17987                AllmanBraceStyle);
17988 
17989   verifyFormat("void foobar() { int i = 5; }\n"
17990                "#ifdef _DEBUG\n"
17991                "void bar() {}\n"
17992                "#else\n"
17993                "void bar() { foobar(); }\n"
17994                "#endif",
17995                AllmanBraceStyle);
17996 
17997   EXPECT_EQ(AllmanBraceStyle.AllowShortLambdasOnASingleLine,
17998             FormatStyle::SLS_All);
17999 
18000   verifyFormat("[](int i) { return i + 2; };\n"
18001                "[](int i, int j)\n"
18002                "{\n"
18003                "  auto x = i + j;\n"
18004                "  auto y = i * j;\n"
18005                "  return x ^ y;\n"
18006                "};\n"
18007                "void foo()\n"
18008                "{\n"
18009                "  auto shortLambda = [](int i) { return i + 2; };\n"
18010                "  auto longLambda = [](int i, int j)\n"
18011                "  {\n"
18012                "    auto x = i + j;\n"
18013                "    auto y = i * j;\n"
18014                "    return x ^ y;\n"
18015                "  };\n"
18016                "}",
18017                AllmanBraceStyle);
18018 
18019   AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
18020 
18021   verifyFormat("[](int i)\n"
18022                "{\n"
18023                "  return i + 2;\n"
18024                "};\n"
18025                "[](int i, int j)\n"
18026                "{\n"
18027                "  auto x = i + j;\n"
18028                "  auto y = i * j;\n"
18029                "  return x ^ y;\n"
18030                "};\n"
18031                "void foo()\n"
18032                "{\n"
18033                "  auto shortLambda = [](int i)\n"
18034                "  {\n"
18035                "    return i + 2;\n"
18036                "  };\n"
18037                "  auto longLambda = [](int i, int j)\n"
18038                "  {\n"
18039                "    auto x = i + j;\n"
18040                "    auto y = i * j;\n"
18041                "    return x ^ y;\n"
18042                "  };\n"
18043                "}",
18044                AllmanBraceStyle);
18045 
18046   // Reset
18047   AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_All;
18048 
18049   // This shouldn't affect ObjC blocks..
18050   verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
18051                "  // ...\n"
18052                "  int i;\n"
18053                "}];",
18054                AllmanBraceStyle);
18055   verifyFormat("void (^block)(void) = ^{\n"
18056                "  // ...\n"
18057                "  int i;\n"
18058                "};",
18059                AllmanBraceStyle);
18060   // .. or dict literals.
18061   verifyFormat("void f()\n"
18062                "{\n"
18063                "  // ...\n"
18064                "  [object someMethod:@{@\"a\" : @\"b\"}];\n"
18065                "}",
18066                AllmanBraceStyle);
18067   verifyFormat("void f()\n"
18068                "{\n"
18069                "  // ...\n"
18070                "  [object someMethod:@{a : @\"b\"}];\n"
18071                "}",
18072                AllmanBraceStyle);
18073   verifyFormat("int f()\n"
18074                "{ // comment\n"
18075                "  return 42;\n"
18076                "}",
18077                AllmanBraceStyle);
18078 
18079   AllmanBraceStyle.ColumnLimit = 19;
18080   verifyFormat("void f() { int i; }", AllmanBraceStyle);
18081   AllmanBraceStyle.ColumnLimit = 18;
18082   verifyFormat("void f()\n"
18083                "{\n"
18084                "  int i;\n"
18085                "}",
18086                AllmanBraceStyle);
18087   AllmanBraceStyle.ColumnLimit = 80;
18088 
18089   FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle;
18090   BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine =
18091       FormatStyle::SIS_WithoutElse;
18092   BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
18093   verifyFormat("void f(bool b)\n"
18094                "{\n"
18095                "  if (b)\n"
18096                "  {\n"
18097                "    return;\n"
18098                "  }\n"
18099                "}\n",
18100                BreakBeforeBraceShortIfs);
18101   verifyFormat("void f(bool b)\n"
18102                "{\n"
18103                "  if constexpr (b)\n"
18104                "  {\n"
18105                "    return;\n"
18106                "  }\n"
18107                "}\n",
18108                BreakBeforeBraceShortIfs);
18109   verifyFormat("void f(bool b)\n"
18110                "{\n"
18111                "  if CONSTEXPR (b)\n"
18112                "  {\n"
18113                "    return;\n"
18114                "  }\n"
18115                "}\n",
18116                BreakBeforeBraceShortIfs);
18117   verifyFormat("void f(bool b)\n"
18118                "{\n"
18119                "  if (b) return;\n"
18120                "}\n",
18121                BreakBeforeBraceShortIfs);
18122   verifyFormat("void f(bool b)\n"
18123                "{\n"
18124                "  if constexpr (b) return;\n"
18125                "}\n",
18126                BreakBeforeBraceShortIfs);
18127   verifyFormat("void f(bool b)\n"
18128                "{\n"
18129                "  if CONSTEXPR (b) return;\n"
18130                "}\n",
18131                BreakBeforeBraceShortIfs);
18132   verifyFormat("void f(bool b)\n"
18133                "{\n"
18134                "  while (b)\n"
18135                "  {\n"
18136                "    return;\n"
18137                "  }\n"
18138                "}\n",
18139                BreakBeforeBraceShortIfs);
18140 }
18141 
18142 TEST_F(FormatTest, WhitesmithsBraceBreaking) {
18143   FormatStyle WhitesmithsBraceStyle = getLLVMStyleWithColumns(0);
18144   WhitesmithsBraceStyle.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
18145 
18146   // Make a few changes to the style for testing purposes
18147   WhitesmithsBraceStyle.AllowShortFunctionsOnASingleLine =
18148       FormatStyle::SFS_Empty;
18149   WhitesmithsBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
18150 
18151   // FIXME: this test case can't decide whether there should be a blank line
18152   // after the ~D() line or not. It adds one if one doesn't exist in the test
18153   // and it removes the line if one exists.
18154   /*
18155   verifyFormat("class A;\n"
18156                "namespace B\n"
18157                "  {\n"
18158                "class C;\n"
18159                "// Comment\n"
18160                "class D\n"
18161                "  {\n"
18162                "public:\n"
18163                "  D();\n"
18164                "  ~D() {}\n"
18165                "private:\n"
18166                "  enum E\n"
18167                "    {\n"
18168                "    F\n"
18169                "    }\n"
18170                "  };\n"
18171                "  } // namespace B\n",
18172                WhitesmithsBraceStyle);
18173   */
18174 
18175   WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_None;
18176   verifyFormat("namespace a\n"
18177                "  {\n"
18178                "class A\n"
18179                "  {\n"
18180                "  void f()\n"
18181                "    {\n"
18182                "    if (true)\n"
18183                "      {\n"
18184                "      a();\n"
18185                "      b();\n"
18186                "      }\n"
18187                "    }\n"
18188                "  void g()\n"
18189                "    {\n"
18190                "    return;\n"
18191                "    }\n"
18192                "  };\n"
18193                "struct B\n"
18194                "  {\n"
18195                "  int x;\n"
18196                "  };\n"
18197                "  } // namespace a",
18198                WhitesmithsBraceStyle);
18199 
18200   verifyFormat("namespace a\n"
18201                "  {\n"
18202                "namespace b\n"
18203                "  {\n"
18204                "class A\n"
18205                "  {\n"
18206                "  void f()\n"
18207                "    {\n"
18208                "    if (true)\n"
18209                "      {\n"
18210                "      a();\n"
18211                "      b();\n"
18212                "      }\n"
18213                "    }\n"
18214                "  void g()\n"
18215                "    {\n"
18216                "    return;\n"
18217                "    }\n"
18218                "  };\n"
18219                "struct B\n"
18220                "  {\n"
18221                "  int x;\n"
18222                "  };\n"
18223                "  } // namespace b\n"
18224                "  } // namespace a",
18225                WhitesmithsBraceStyle);
18226 
18227   WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_Inner;
18228   verifyFormat("namespace a\n"
18229                "  {\n"
18230                "namespace b\n"
18231                "  {\n"
18232                "  class A\n"
18233                "    {\n"
18234                "    void f()\n"
18235                "      {\n"
18236                "      if (true)\n"
18237                "        {\n"
18238                "        a();\n"
18239                "        b();\n"
18240                "        }\n"
18241                "      }\n"
18242                "    void g()\n"
18243                "      {\n"
18244                "      return;\n"
18245                "      }\n"
18246                "    };\n"
18247                "  struct B\n"
18248                "    {\n"
18249                "    int x;\n"
18250                "    };\n"
18251                "  } // namespace b\n"
18252                "  } // namespace a",
18253                WhitesmithsBraceStyle);
18254 
18255   WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_All;
18256   verifyFormat("namespace a\n"
18257                "  {\n"
18258                "  namespace b\n"
18259                "    {\n"
18260                "    class A\n"
18261                "      {\n"
18262                "      void f()\n"
18263                "        {\n"
18264                "        if (true)\n"
18265                "          {\n"
18266                "          a();\n"
18267                "          b();\n"
18268                "          }\n"
18269                "        }\n"
18270                "      void g()\n"
18271                "        {\n"
18272                "        return;\n"
18273                "        }\n"
18274                "      };\n"
18275                "    struct B\n"
18276                "      {\n"
18277                "      int x;\n"
18278                "      };\n"
18279                "    } // namespace b\n"
18280                "  }   // namespace a",
18281                WhitesmithsBraceStyle);
18282 
18283   verifyFormat("void f()\n"
18284                "  {\n"
18285                "  if (true)\n"
18286                "    {\n"
18287                "    a();\n"
18288                "    }\n"
18289                "  else if (false)\n"
18290                "    {\n"
18291                "    b();\n"
18292                "    }\n"
18293                "  else\n"
18294                "    {\n"
18295                "    c();\n"
18296                "    }\n"
18297                "  }\n",
18298                WhitesmithsBraceStyle);
18299 
18300   verifyFormat("void f()\n"
18301                "  {\n"
18302                "  for (int i = 0; i < 10; ++i)\n"
18303                "    {\n"
18304                "    a();\n"
18305                "    }\n"
18306                "  while (false)\n"
18307                "    {\n"
18308                "    b();\n"
18309                "    }\n"
18310                "  do\n"
18311                "    {\n"
18312                "    c();\n"
18313                "    } while (false)\n"
18314                "  }\n",
18315                WhitesmithsBraceStyle);
18316 
18317   WhitesmithsBraceStyle.IndentCaseLabels = true;
18318   verifyFormat("void switchTest1(int a)\n"
18319                "  {\n"
18320                "  switch (a)\n"
18321                "    {\n"
18322                "    case 2:\n"
18323                "      {\n"
18324                "      }\n"
18325                "      break;\n"
18326                "    }\n"
18327                "  }\n",
18328                WhitesmithsBraceStyle);
18329 
18330   verifyFormat("void switchTest2(int a)\n"
18331                "  {\n"
18332                "  switch (a)\n"
18333                "    {\n"
18334                "    case 0:\n"
18335                "      break;\n"
18336                "    case 1:\n"
18337                "      {\n"
18338                "      break;\n"
18339                "      }\n"
18340                "    case 2:\n"
18341                "      {\n"
18342                "      }\n"
18343                "      break;\n"
18344                "    default:\n"
18345                "      break;\n"
18346                "    }\n"
18347                "  }\n",
18348                WhitesmithsBraceStyle);
18349 
18350   verifyFormat("void switchTest3(int a)\n"
18351                "  {\n"
18352                "  switch (a)\n"
18353                "    {\n"
18354                "    case 0:\n"
18355                "      {\n"
18356                "      foo(x);\n"
18357                "      }\n"
18358                "      break;\n"
18359                "    default:\n"
18360                "      {\n"
18361                "      foo(1);\n"
18362                "      }\n"
18363                "      break;\n"
18364                "    }\n"
18365                "  }\n",
18366                WhitesmithsBraceStyle);
18367 
18368   WhitesmithsBraceStyle.IndentCaseLabels = false;
18369 
18370   verifyFormat("void switchTest4(int a)\n"
18371                "  {\n"
18372                "  switch (a)\n"
18373                "    {\n"
18374                "  case 2:\n"
18375                "    {\n"
18376                "    }\n"
18377                "    break;\n"
18378                "    }\n"
18379                "  }\n",
18380                WhitesmithsBraceStyle);
18381 
18382   verifyFormat("void switchTest5(int a)\n"
18383                "  {\n"
18384                "  switch (a)\n"
18385                "    {\n"
18386                "  case 0:\n"
18387                "    break;\n"
18388                "  case 1:\n"
18389                "    {\n"
18390                "    foo();\n"
18391                "    break;\n"
18392                "    }\n"
18393                "  case 2:\n"
18394                "    {\n"
18395                "    }\n"
18396                "    break;\n"
18397                "  default:\n"
18398                "    break;\n"
18399                "    }\n"
18400                "  }\n",
18401                WhitesmithsBraceStyle);
18402 
18403   verifyFormat("void switchTest6(int a)\n"
18404                "  {\n"
18405                "  switch (a)\n"
18406                "    {\n"
18407                "  case 0:\n"
18408                "    {\n"
18409                "    foo(x);\n"
18410                "    }\n"
18411                "    break;\n"
18412                "  default:\n"
18413                "    {\n"
18414                "    foo(1);\n"
18415                "    }\n"
18416                "    break;\n"
18417                "    }\n"
18418                "  }\n",
18419                WhitesmithsBraceStyle);
18420 
18421   verifyFormat("enum X\n"
18422                "  {\n"
18423                "  Y = 0, // testing\n"
18424                "  }\n",
18425                WhitesmithsBraceStyle);
18426 
18427   verifyFormat("enum X\n"
18428                "  {\n"
18429                "  Y = 0\n"
18430                "  }\n",
18431                WhitesmithsBraceStyle);
18432   verifyFormat("enum X\n"
18433                "  {\n"
18434                "  Y = 0,\n"
18435                "  Z = 1\n"
18436                "  };\n",
18437                WhitesmithsBraceStyle);
18438 
18439   verifyFormat("@interface BSApplicationController ()\n"
18440                "  {\n"
18441                "@private\n"
18442                "  id _extraIvar;\n"
18443                "  }\n"
18444                "@end\n",
18445                WhitesmithsBraceStyle);
18446 
18447   verifyFormat("#ifdef _DEBUG\n"
18448                "int foo(int i = 0)\n"
18449                "#else\n"
18450                "int foo(int i = 5)\n"
18451                "#endif\n"
18452                "  {\n"
18453                "  return i;\n"
18454                "  }",
18455                WhitesmithsBraceStyle);
18456 
18457   verifyFormat("void foo() {}\n"
18458                "void bar()\n"
18459                "#ifdef _DEBUG\n"
18460                "  {\n"
18461                "  foo();\n"
18462                "  }\n"
18463                "#else\n"
18464                "  {\n"
18465                "  }\n"
18466                "#endif",
18467                WhitesmithsBraceStyle);
18468 
18469   verifyFormat("void foobar()\n"
18470                "  {\n"
18471                "  int i = 5;\n"
18472                "  }\n"
18473                "#ifdef _DEBUG\n"
18474                "void bar()\n"
18475                "  {\n"
18476                "  }\n"
18477                "#else\n"
18478                "void bar()\n"
18479                "  {\n"
18480                "  foobar();\n"
18481                "  }\n"
18482                "#endif",
18483                WhitesmithsBraceStyle);
18484 
18485   // This shouldn't affect ObjC blocks..
18486   verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
18487                "  // ...\n"
18488                "  int i;\n"
18489                "}];",
18490                WhitesmithsBraceStyle);
18491   verifyFormat("void (^block)(void) = ^{\n"
18492                "  // ...\n"
18493                "  int i;\n"
18494                "};",
18495                WhitesmithsBraceStyle);
18496   // .. or dict literals.
18497   verifyFormat("void f()\n"
18498                "  {\n"
18499                "  [object someMethod:@{@\"a\" : @\"b\"}];\n"
18500                "  }",
18501                WhitesmithsBraceStyle);
18502 
18503   verifyFormat("int f()\n"
18504                "  { // comment\n"
18505                "  return 42;\n"
18506                "  }",
18507                WhitesmithsBraceStyle);
18508 
18509   FormatStyle BreakBeforeBraceShortIfs = WhitesmithsBraceStyle;
18510   BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine =
18511       FormatStyle::SIS_OnlyFirstIf;
18512   BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
18513   verifyFormat("void f(bool b)\n"
18514                "  {\n"
18515                "  if (b)\n"
18516                "    {\n"
18517                "    return;\n"
18518                "    }\n"
18519                "  }\n",
18520                BreakBeforeBraceShortIfs);
18521   verifyFormat("void f(bool b)\n"
18522                "  {\n"
18523                "  if (b) return;\n"
18524                "  }\n",
18525                BreakBeforeBraceShortIfs);
18526   verifyFormat("void f(bool b)\n"
18527                "  {\n"
18528                "  while (b)\n"
18529                "    {\n"
18530                "    return;\n"
18531                "    }\n"
18532                "  }\n",
18533                BreakBeforeBraceShortIfs);
18534 }
18535 
18536 TEST_F(FormatTest, GNUBraceBreaking) {
18537   FormatStyle GNUBraceStyle = getLLVMStyle();
18538   GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
18539   verifyFormat("namespace a\n"
18540                "{\n"
18541                "class A\n"
18542                "{\n"
18543                "  void f()\n"
18544                "  {\n"
18545                "    int a;\n"
18546                "    {\n"
18547                "      int b;\n"
18548                "    }\n"
18549                "    if (true)\n"
18550                "      {\n"
18551                "        a();\n"
18552                "        b();\n"
18553                "      }\n"
18554                "  }\n"
18555                "  void g() { return; }\n"
18556                "}\n"
18557                "} // namespace a",
18558                GNUBraceStyle);
18559 
18560   verifyFormat("void f()\n"
18561                "{\n"
18562                "  if (true)\n"
18563                "    {\n"
18564                "      a();\n"
18565                "    }\n"
18566                "  else if (false)\n"
18567                "    {\n"
18568                "      b();\n"
18569                "    }\n"
18570                "  else\n"
18571                "    {\n"
18572                "      c();\n"
18573                "    }\n"
18574                "}\n",
18575                GNUBraceStyle);
18576 
18577   verifyFormat("void f()\n"
18578                "{\n"
18579                "  for (int i = 0; i < 10; ++i)\n"
18580                "    {\n"
18581                "      a();\n"
18582                "    }\n"
18583                "  while (false)\n"
18584                "    {\n"
18585                "      b();\n"
18586                "    }\n"
18587                "  do\n"
18588                "    {\n"
18589                "      c();\n"
18590                "    }\n"
18591                "  while (false);\n"
18592                "}\n",
18593                GNUBraceStyle);
18594 
18595   verifyFormat("void f(int a)\n"
18596                "{\n"
18597                "  switch (a)\n"
18598                "    {\n"
18599                "    case 0:\n"
18600                "      break;\n"
18601                "    case 1:\n"
18602                "      {\n"
18603                "        break;\n"
18604                "      }\n"
18605                "    case 2:\n"
18606                "      {\n"
18607                "      }\n"
18608                "      break;\n"
18609                "    default:\n"
18610                "      break;\n"
18611                "    }\n"
18612                "}\n",
18613                GNUBraceStyle);
18614 
18615   verifyFormat("enum X\n"
18616                "{\n"
18617                "  Y = 0,\n"
18618                "}\n",
18619                GNUBraceStyle);
18620 
18621   verifyFormat("@interface BSApplicationController ()\n"
18622                "{\n"
18623                "@private\n"
18624                "  id _extraIvar;\n"
18625                "}\n"
18626                "@end\n",
18627                GNUBraceStyle);
18628 
18629   verifyFormat("#ifdef _DEBUG\n"
18630                "int foo(int i = 0)\n"
18631                "#else\n"
18632                "int foo(int i = 5)\n"
18633                "#endif\n"
18634                "{\n"
18635                "  return i;\n"
18636                "}",
18637                GNUBraceStyle);
18638 
18639   verifyFormat("void foo() {}\n"
18640                "void bar()\n"
18641                "#ifdef _DEBUG\n"
18642                "{\n"
18643                "  foo();\n"
18644                "}\n"
18645                "#else\n"
18646                "{\n"
18647                "}\n"
18648                "#endif",
18649                GNUBraceStyle);
18650 
18651   verifyFormat("void foobar() { int i = 5; }\n"
18652                "#ifdef _DEBUG\n"
18653                "void bar() {}\n"
18654                "#else\n"
18655                "void bar() { foobar(); }\n"
18656                "#endif",
18657                GNUBraceStyle);
18658 }
18659 
18660 TEST_F(FormatTest, WebKitBraceBreaking) {
18661   FormatStyle WebKitBraceStyle = getLLVMStyle();
18662   WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit;
18663   WebKitBraceStyle.FixNamespaceComments = false;
18664   verifyFormat("namespace a {\n"
18665                "class A {\n"
18666                "  void f()\n"
18667                "  {\n"
18668                "    if (true) {\n"
18669                "      a();\n"
18670                "      b();\n"
18671                "    }\n"
18672                "  }\n"
18673                "  void g() { return; }\n"
18674                "};\n"
18675                "enum E {\n"
18676                "  A,\n"
18677                "  // foo\n"
18678                "  B,\n"
18679                "  C\n"
18680                "};\n"
18681                "struct B {\n"
18682                "  int x;\n"
18683                "};\n"
18684                "}\n",
18685                WebKitBraceStyle);
18686   verifyFormat("struct S {\n"
18687                "  int Type;\n"
18688                "  union {\n"
18689                "    int x;\n"
18690                "    double y;\n"
18691                "  } Value;\n"
18692                "  class C {\n"
18693                "    MyFavoriteType Value;\n"
18694                "  } Class;\n"
18695                "};\n",
18696                WebKitBraceStyle);
18697 }
18698 
18699 TEST_F(FormatTest, CatchExceptionReferenceBinding) {
18700   verifyFormat("void f() {\n"
18701                "  try {\n"
18702                "  } catch (const Exception &e) {\n"
18703                "  }\n"
18704                "}\n",
18705                getLLVMStyle());
18706 }
18707 
18708 TEST_F(FormatTest, CatchAlignArrayOfStructuresRightAlignment) {
18709   auto Style = getLLVMStyle();
18710   Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
18711   Style.AlignConsecutiveAssignments =
18712       FormatStyle::AlignConsecutiveStyle::ACS_Consecutive;
18713   Style.AlignConsecutiveDeclarations =
18714       FormatStyle::AlignConsecutiveStyle::ACS_Consecutive;
18715   verifyFormat("struct test demo[] = {\n"
18716                "    {56,    23, \"hello\"},\n"
18717                "    {-1, 93463, \"world\"},\n"
18718                "    { 7,     5,    \"!!\"}\n"
18719                "};\n",
18720                Style);
18721 
18722   verifyFormat("struct test demo[] = {\n"
18723                "    {56,    23, \"hello\"}, // first line\n"
18724                "    {-1, 93463, \"world\"}, // second line\n"
18725                "    { 7,     5,    \"!!\"}  // third line\n"
18726                "};\n",
18727                Style);
18728 
18729   verifyFormat("struct test demo[4] = {\n"
18730                "    { 56,    23, 21,       \"oh\"}, // first line\n"
18731                "    { -1, 93463, 22,       \"my\"}, // second line\n"
18732                "    {  7,     5,  1, \"goodness\"}  // third line\n"
18733                "    {234,     5,  1, \"gracious\"}  // fourth line\n"
18734                "};\n",
18735                Style);
18736 
18737   verifyFormat("struct test demo[3] = {\n"
18738                "    {56,    23, \"hello\"},\n"
18739                "    {-1, 93463, \"world\"},\n"
18740                "    { 7,     5,    \"!!\"}\n"
18741                "};\n",
18742                Style);
18743 
18744   verifyFormat("struct test demo[3] = {\n"
18745                "    {int{56},    23, \"hello\"},\n"
18746                "    {int{-1}, 93463, \"world\"},\n"
18747                "    { int{7},     5,    \"!!\"}\n"
18748                "};\n",
18749                Style);
18750 
18751   verifyFormat("struct test demo[] = {\n"
18752                "    {56,    23, \"hello\"},\n"
18753                "    {-1, 93463, \"world\"},\n"
18754                "    { 7,     5,    \"!!\"},\n"
18755                "};\n",
18756                Style);
18757 
18758   verifyFormat("test demo[] = {\n"
18759                "    {56,    23, \"hello\"},\n"
18760                "    {-1, 93463, \"world\"},\n"
18761                "    { 7,     5,    \"!!\"},\n"
18762                "};\n",
18763                Style);
18764 
18765   verifyFormat("demo = std::array<struct test, 3>{\n"
18766                "    test{56,    23, \"hello\"},\n"
18767                "    test{-1, 93463, \"world\"},\n"
18768                "    test{ 7,     5,    \"!!\"},\n"
18769                "};\n",
18770                Style);
18771 
18772   verifyFormat("test demo[] = {\n"
18773                "    {56,    23, \"hello\"},\n"
18774                "#if X\n"
18775                "    {-1, 93463, \"world\"},\n"
18776                "#endif\n"
18777                "    { 7,     5,    \"!!\"}\n"
18778                "};\n",
18779                Style);
18780 
18781   verifyFormat(
18782       "test demo[] = {\n"
18783       "    { 7,    23,\n"
18784       "     \"hello world i am a very long line that really, in any\"\n"
18785       "     \"just world, ought to be split over multiple lines\"},\n"
18786       "    {-1, 93463,                                  \"world\"},\n"
18787       "    {56,     5,                                     \"!!\"}\n"
18788       "};\n",
18789       Style);
18790 
18791   verifyFormat("return GradForUnaryCwise(g, {\n"
18792                "                                {{\"sign\"}, \"Sign\",  "
18793                "  {\"x\", \"dy\"}},\n"
18794                "                                {  {\"dx\"},  \"Mul\", {\"dy\""
18795                ", \"sign\"}},\n"
18796                "});\n",
18797                Style);
18798 
18799   Style.ColumnLimit = 0;
18800   EXPECT_EQ(
18801       "test demo[] = {\n"
18802       "    {56,    23, \"hello world i am a very long line that really, "
18803       "in any just world, ought to be split over multiple lines\"},\n"
18804       "    {-1, 93463,                                                  "
18805       "                                                 \"world\"},\n"
18806       "    { 7,     5,                                                  "
18807       "                                                    \"!!\"},\n"
18808       "};",
18809       format("test demo[] = {{56, 23, \"hello world i am a very long line "
18810              "that really, in any just world, ought to be split over multiple "
18811              "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
18812              Style));
18813 
18814   Style.ColumnLimit = 80;
18815   verifyFormat("test demo[] = {\n"
18816                "    {56,    23, /* a comment */ \"hello\"},\n"
18817                "    {-1, 93463,                 \"world\"},\n"
18818                "    { 7,     5,                    \"!!\"}\n"
18819                "};\n",
18820                Style);
18821 
18822   verifyFormat("test demo[] = {\n"
18823                "    {56,    23,                    \"hello\"},\n"
18824                "    {-1, 93463, \"world\" /* comment here */},\n"
18825                "    { 7,     5,                       \"!!\"}\n"
18826                "};\n",
18827                Style);
18828 
18829   verifyFormat("test demo[] = {\n"
18830                "    {56, /* a comment */ 23, \"hello\"},\n"
18831                "    {-1,              93463, \"world\"},\n"
18832                "    { 7,                  5,    \"!!\"}\n"
18833                "};\n",
18834                Style);
18835 
18836   Style.ColumnLimit = 20;
18837   EXPECT_EQ(
18838       "demo = std::array<\n"
18839       "    struct test, 3>{\n"
18840       "    test{\n"
18841       "         56,    23,\n"
18842       "         \"hello \"\n"
18843       "         \"world i \"\n"
18844       "         \"am a very \"\n"
18845       "         \"long line \"\n"
18846       "         \"that \"\n"
18847       "         \"really, \"\n"
18848       "         \"in any \"\n"
18849       "         \"just \"\n"
18850       "         \"world, \"\n"
18851       "         \"ought to \"\n"
18852       "         \"be split \"\n"
18853       "         \"over \"\n"
18854       "         \"multiple \"\n"
18855       "         \"lines\"},\n"
18856       "    test{-1, 93463,\n"
18857       "         \"world\"},\n"
18858       "    test{ 7,     5,\n"
18859       "         \"!!\"   },\n"
18860       "};",
18861       format("demo = std::array<struct test, 3>{test{56, 23, \"hello world "
18862              "i am a very long line that really, in any just world, ought "
18863              "to be split over multiple lines\"},test{-1, 93463, \"world\"},"
18864              "test{7, 5, \"!!\"},};",
18865              Style));
18866   // This caused a core dump by enabling Alignment in the LLVMStyle globally
18867   Style = getLLVMStyleWithColumns(50);
18868   Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
18869   verifyFormat("static A x = {\n"
18870                "    {{init1, init2, init3, init4},\n"
18871                "     {init1, init2, init3, init4}}\n"
18872                "};",
18873                Style);
18874   Style.ColumnLimit = 100;
18875   EXPECT_EQ(
18876       "test demo[] = {\n"
18877       "    {56,    23,\n"
18878       "     \"hello world i am a very long line that really, in any just world"
18879       ", ought to be split over \"\n"
18880       "     \"multiple lines\"  },\n"
18881       "    {-1, 93463, \"world\"},\n"
18882       "    { 7,     5,    \"!!\"},\n"
18883       "};",
18884       format("test demo[] = {{56, 23, \"hello world i am a very long line "
18885              "that really, in any just world, ought to be split over multiple "
18886              "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
18887              Style));
18888 
18889   Style = getLLVMStyleWithColumns(50);
18890   Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
18891   Style.AlignConsecutiveAssignments =
18892       FormatStyle::AlignConsecutiveStyle::ACS_Consecutive;
18893   Style.AlignConsecutiveDeclarations =
18894       FormatStyle::AlignConsecutiveStyle::ACS_Consecutive;
18895   verifyFormat("struct test demo[] = {\n"
18896                "    {56,    23, \"hello\"},\n"
18897                "    {-1, 93463, \"world\"},\n"
18898                "    { 7,     5,    \"!!\"}\n"
18899                "};\n"
18900                "static A x = {\n"
18901                "    {{init1, init2, init3, init4},\n"
18902                "     {init1, init2, init3, init4}}\n"
18903                "};",
18904                Style);
18905   Style.ColumnLimit = 100;
18906   Style.AlignConsecutiveAssignments =
18907       FormatStyle::AlignConsecutiveStyle::ACS_AcrossComments;
18908   Style.AlignConsecutiveDeclarations =
18909       FormatStyle::AlignConsecutiveStyle::ACS_AcrossComments;
18910   verifyFormat("struct test demo[] = {\n"
18911                "    {56,    23, \"hello\"},\n"
18912                "    {-1, 93463, \"world\"},\n"
18913                "    { 7,     5,    \"!!\"}\n"
18914                "};\n"
18915                "struct test demo[4] = {\n"
18916                "    { 56,    23, 21,       \"oh\"}, // first line\n"
18917                "    { -1, 93463, 22,       \"my\"}, // second line\n"
18918                "    {  7,     5,  1, \"goodness\"}  // third line\n"
18919                "    {234,     5,  1, \"gracious\"}  // fourth line\n"
18920                "};\n",
18921                Style);
18922   EXPECT_EQ(
18923       "test demo[] = {\n"
18924       "    {56,\n"
18925       "     \"hello world i am a very long line that really, in any just world"
18926       ", ought to be split over \"\n"
18927       "     \"multiple lines\",    23},\n"
18928       "    {-1,      \"world\", 93463},\n"
18929       "    { 7,         \"!!\",     5},\n"
18930       "};",
18931       format("test demo[] = {{56, \"hello world i am a very long line "
18932              "that really, in any just world, ought to be split over multiple "
18933              "lines\", 23},{-1, \"world\", 93463},{7, \"!!\", 5},};",
18934              Style));
18935 }
18936 
18937 TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) {
18938   auto Style = getLLVMStyle();
18939   Style.AlignArrayOfStructures = FormatStyle::AIAS_Left;
18940   /* FIXME: This case gets misformatted.
18941   verifyFormat("auto foo = Items{\n"
18942                "    Section{0, bar(), },\n"
18943                "    Section{1, boo()  }\n"
18944                "};\n",
18945                Style);
18946   */
18947   verifyFormat("auto foo = Items{\n"
18948                "    Section{\n"
18949                "            0, bar(),\n"
18950                "            }\n"
18951                "};\n",
18952                Style);
18953   verifyFormat("struct test demo[] = {\n"
18954                "    {56, 23,    \"hello\"},\n"
18955                "    {-1, 93463, \"world\"},\n"
18956                "    {7,  5,     \"!!\"   }\n"
18957                "};\n",
18958                Style);
18959   verifyFormat("struct test demo[] = {\n"
18960                "    {56, 23,    \"hello\"}, // first line\n"
18961                "    {-1, 93463, \"world\"}, // second line\n"
18962                "    {7,  5,     \"!!\"   }  // third line\n"
18963                "};\n",
18964                Style);
18965   verifyFormat("struct test demo[4] = {\n"
18966                "    {56,  23,    21, \"oh\"      }, // first line\n"
18967                "    {-1,  93463, 22, \"my\"      }, // second line\n"
18968                "    {7,   5,     1,  \"goodness\"}  // third line\n"
18969                "    {234, 5,     1,  \"gracious\"}  // fourth line\n"
18970                "};\n",
18971                Style);
18972   verifyFormat("struct test demo[3] = {\n"
18973                "    {56, 23,    \"hello\"},\n"
18974                "    {-1, 93463, \"world\"},\n"
18975                "    {7,  5,     \"!!\"   }\n"
18976                "};\n",
18977                Style);
18978 
18979   verifyFormat("struct test demo[3] = {\n"
18980                "    {int{56}, 23,    \"hello\"},\n"
18981                "    {int{-1}, 93463, \"world\"},\n"
18982                "    {int{7},  5,     \"!!\"   }\n"
18983                "};\n",
18984                Style);
18985   verifyFormat("struct test demo[] = {\n"
18986                "    {56, 23,    \"hello\"},\n"
18987                "    {-1, 93463, \"world\"},\n"
18988                "    {7,  5,     \"!!\"   },\n"
18989                "};\n",
18990                Style);
18991   verifyFormat("test demo[] = {\n"
18992                "    {56, 23,    \"hello\"},\n"
18993                "    {-1, 93463, \"world\"},\n"
18994                "    {7,  5,     \"!!\"   },\n"
18995                "};\n",
18996                Style);
18997   verifyFormat("demo = std::array<struct test, 3>{\n"
18998                "    test{56, 23,    \"hello\"},\n"
18999                "    test{-1, 93463, \"world\"},\n"
19000                "    test{7,  5,     \"!!\"   },\n"
19001                "};\n",
19002                Style);
19003   verifyFormat("test demo[] = {\n"
19004                "    {56, 23,    \"hello\"},\n"
19005                "#if X\n"
19006                "    {-1, 93463, \"world\"},\n"
19007                "#endif\n"
19008                "    {7,  5,     \"!!\"   }\n"
19009                "};\n",
19010                Style);
19011   verifyFormat(
19012       "test demo[] = {\n"
19013       "    {7,  23,\n"
19014       "     \"hello world i am a very long line that really, in any\"\n"
19015       "     \"just world, ought to be split over multiple lines\"},\n"
19016       "    {-1, 93463, \"world\"                                 },\n"
19017       "    {56, 5,     \"!!\"                                    }\n"
19018       "};\n",
19019       Style);
19020 
19021   verifyFormat("return GradForUnaryCwise(g, {\n"
19022                "                                {{\"sign\"}, \"Sign\", {\"x\", "
19023                "\"dy\"}   },\n"
19024                "                                {{\"dx\"},   \"Mul\",  "
19025                "{\"dy\", \"sign\"}},\n"
19026                "});\n",
19027                Style);
19028 
19029   Style.ColumnLimit = 0;
19030   EXPECT_EQ(
19031       "test demo[] = {\n"
19032       "    {56, 23,    \"hello world i am a very long line that really, in any "
19033       "just world, ought to be split over multiple lines\"},\n"
19034       "    {-1, 93463, \"world\"                                               "
19035       "                                                   },\n"
19036       "    {7,  5,     \"!!\"                                                  "
19037       "                                                   },\n"
19038       "};",
19039       format("test demo[] = {{56, 23, \"hello world i am a very long line "
19040              "that really, in any just world, ought to be split over multiple "
19041              "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
19042              Style));
19043 
19044   Style.ColumnLimit = 80;
19045   verifyFormat("test demo[] = {\n"
19046                "    {56, 23,    /* a comment */ \"hello\"},\n"
19047                "    {-1, 93463, \"world\"                },\n"
19048                "    {7,  5,     \"!!\"                   }\n"
19049                "};\n",
19050                Style);
19051 
19052   verifyFormat("test demo[] = {\n"
19053                "    {56, 23,    \"hello\"                   },\n"
19054                "    {-1, 93463, \"world\" /* comment here */},\n"
19055                "    {7,  5,     \"!!\"                      }\n"
19056                "};\n",
19057                Style);
19058 
19059   verifyFormat("test demo[] = {\n"
19060                "    {56, /* a comment */ 23, \"hello\"},\n"
19061                "    {-1, 93463,              \"world\"},\n"
19062                "    {7,  5,                  \"!!\"   }\n"
19063                "};\n",
19064                Style);
19065 
19066   Style.ColumnLimit = 20;
19067   EXPECT_EQ(
19068       "demo = std::array<\n"
19069       "    struct test, 3>{\n"
19070       "    test{\n"
19071       "         56, 23,\n"
19072       "         \"hello \"\n"
19073       "         \"world i \"\n"
19074       "         \"am a very \"\n"
19075       "         \"long line \"\n"
19076       "         \"that \"\n"
19077       "         \"really, \"\n"
19078       "         \"in any \"\n"
19079       "         \"just \"\n"
19080       "         \"world, \"\n"
19081       "         \"ought to \"\n"
19082       "         \"be split \"\n"
19083       "         \"over \"\n"
19084       "         \"multiple \"\n"
19085       "         \"lines\"},\n"
19086       "    test{-1, 93463,\n"
19087       "         \"world\"},\n"
19088       "    test{7,  5,\n"
19089       "         \"!!\"   },\n"
19090       "};",
19091       format("demo = std::array<struct test, 3>{test{56, 23, \"hello world "
19092              "i am a very long line that really, in any just world, ought "
19093              "to be split over multiple lines\"},test{-1, 93463, \"world\"},"
19094              "test{7, 5, \"!!\"},};",
19095              Style));
19096 
19097   // This caused a core dump by enabling Alignment in the LLVMStyle globally
19098   Style = getLLVMStyleWithColumns(50);
19099   Style.AlignArrayOfStructures = FormatStyle::AIAS_Left;
19100   verifyFormat("static A x = {\n"
19101                "    {{init1, init2, init3, init4},\n"
19102                "     {init1, init2, init3, init4}}\n"
19103                "};",
19104                Style);
19105   Style.ColumnLimit = 100;
19106   EXPECT_EQ(
19107       "test demo[] = {\n"
19108       "    {56, 23,\n"
19109       "     \"hello world i am a very long line that really, in any just world"
19110       ", ought to be split over \"\n"
19111       "     \"multiple lines\"  },\n"
19112       "    {-1, 93463, \"world\"},\n"
19113       "    {7,  5,     \"!!\"   },\n"
19114       "};",
19115       format("test demo[] = {{56, 23, \"hello world i am a very long line "
19116              "that really, in any just world, ought to be split over multiple "
19117              "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
19118              Style));
19119 }
19120 
19121 TEST_F(FormatTest, UnderstandsPragmas) {
19122   verifyFormat("#pragma omp reduction(| : var)");
19123   verifyFormat("#pragma omp reduction(+ : var)");
19124 
19125   EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string "
19126             "(including parentheses).",
19127             format("#pragma    mark   Any non-hyphenated or hyphenated string "
19128                    "(including parentheses)."));
19129 }
19130 
19131 TEST_F(FormatTest, UnderstandPragmaOption) {
19132   verifyFormat("#pragma option -C -A");
19133 
19134   EXPECT_EQ("#pragma option -C -A", format("#pragma    option   -C   -A"));
19135 }
19136 
19137 TEST_F(FormatTest, OptimizeBreakPenaltyVsExcess) {
19138   FormatStyle Style = getLLVMStyleWithColumns(20);
19139 
19140   // See PR41213
19141   EXPECT_EQ("/*\n"
19142             " *\t9012345\n"
19143             " * /8901\n"
19144             " */",
19145             format("/*\n"
19146                    " *\t9012345 /8901\n"
19147                    " */",
19148                    Style));
19149   EXPECT_EQ("/*\n"
19150             " *345678\n"
19151             " *\t/8901\n"
19152             " */",
19153             format("/*\n"
19154                    " *345678\t/8901\n"
19155                    " */",
19156                    Style));
19157 
19158   verifyFormat("int a; // the\n"
19159                "       // comment",
19160                Style);
19161   EXPECT_EQ("int a; /* first line\n"
19162             "        * second\n"
19163             "        * line third\n"
19164             "        * line\n"
19165             "        */",
19166             format("int a; /* first line\n"
19167                    "        * second\n"
19168                    "        * line third\n"
19169                    "        * line\n"
19170                    "        */",
19171                    Style));
19172   EXPECT_EQ("int a; // first line\n"
19173             "       // second\n"
19174             "       // line third\n"
19175             "       // line",
19176             format("int a; // first line\n"
19177                    "       // second line\n"
19178                    "       // third line",
19179                    Style));
19180 
19181   Style.PenaltyExcessCharacter = 90;
19182   verifyFormat("int a; // the comment", Style);
19183   EXPECT_EQ("int a; // the comment\n"
19184             "       // aaa",
19185             format("int a; // the comment aaa", Style));
19186   EXPECT_EQ("int a; /* first line\n"
19187             "        * second line\n"
19188             "        * third line\n"
19189             "        */",
19190             format("int a; /* first line\n"
19191                    "        * second line\n"
19192                    "        * third line\n"
19193                    "        */",
19194                    Style));
19195   EXPECT_EQ("int a; // first line\n"
19196             "       // second line\n"
19197             "       // third line",
19198             format("int a; // first line\n"
19199                    "       // second line\n"
19200                    "       // third line",
19201                    Style));
19202   // FIXME: Investigate why this is not getting the same layout as the test
19203   // above.
19204   EXPECT_EQ("int a; /* first line\n"
19205             "        * second line\n"
19206             "        * third line\n"
19207             "        */",
19208             format("int a; /* first line second line third line"
19209                    "\n*/",
19210                    Style));
19211 
19212   EXPECT_EQ("// foo bar baz bazfoo\n"
19213             "// foo bar foo bar\n",
19214             format("// foo bar baz bazfoo\n"
19215                    "// foo bar foo           bar\n",
19216                    Style));
19217   EXPECT_EQ("// foo bar baz bazfoo\n"
19218             "// foo bar foo bar\n",
19219             format("// foo bar baz      bazfoo\n"
19220                    "// foo            bar foo bar\n",
19221                    Style));
19222 
19223   // FIXME: Optimally, we'd keep bazfoo on the first line and reflow bar to the
19224   // next one.
19225   EXPECT_EQ("// foo bar baz bazfoo\n"
19226             "// bar foo bar\n",
19227             format("// foo bar baz      bazfoo bar\n"
19228                    "// foo            bar\n",
19229                    Style));
19230 
19231   EXPECT_EQ("// foo bar baz bazfoo\n"
19232             "// foo bar baz bazfoo\n"
19233             "// bar foo bar\n",
19234             format("// foo bar baz      bazfoo\n"
19235                    "// foo bar baz      bazfoo bar\n"
19236                    "// foo bar\n",
19237                    Style));
19238 
19239   EXPECT_EQ("// foo bar baz bazfoo\n"
19240             "// foo bar baz bazfoo\n"
19241             "// bar foo bar\n",
19242             format("// foo bar baz      bazfoo\n"
19243                    "// foo bar baz      bazfoo bar\n"
19244                    "// foo           bar\n",
19245                    Style));
19246 
19247   // Make sure we do not keep protruding characters if strict mode reflow is
19248   // cheaper than keeping protruding characters.
19249   Style.ColumnLimit = 21;
19250   EXPECT_EQ(
19251       "// foo foo foo foo\n"
19252       "// foo foo foo foo\n"
19253       "// foo foo foo foo\n",
19254       format("// foo foo foo foo foo foo foo foo foo foo foo foo\n", Style));
19255 
19256   EXPECT_EQ("int a = /* long block\n"
19257             "           comment */\n"
19258             "    42;",
19259             format("int a = /* long block comment */ 42;", Style));
19260 }
19261 
19262 TEST_F(FormatTest, BreakPenaltyAfterLParen) {
19263   FormatStyle Style = getLLVMStyle();
19264   Style.ColumnLimit = 8;
19265   Style.PenaltyExcessCharacter = 15;
19266   verifyFormat("int foo(\n"
19267                "    int aaaaaaaaaaaaaaaaaaaaaaaa);",
19268                Style);
19269   Style.PenaltyBreakOpenParenthesis = 200;
19270   EXPECT_EQ("int foo(int aaaaaaaaaaaaaaaaaaaaaaaa);",
19271             format("int foo(\n"
19272                    "    int aaaaaaaaaaaaaaaaaaaaaaaa);",
19273                    Style));
19274 }
19275 
19276 TEST_F(FormatTest, BreakPenaltyAfterCastLParen) {
19277   FormatStyle Style = getLLVMStyle();
19278   Style.ColumnLimit = 5;
19279   Style.PenaltyExcessCharacter = 150;
19280   verifyFormat("foo((\n"
19281                "    int)aaaaaaaaaaaaaaaaaaaaaaaa);",
19282 
19283                Style);
19284   Style.PenaltyBreakOpenParenthesis = 100000;
19285   EXPECT_EQ("foo((int)\n"
19286             "        aaaaaaaaaaaaaaaaaaaaaaaa);",
19287             format("foo((\n"
19288                    "int)aaaaaaaaaaaaaaaaaaaaaaaa);",
19289                    Style));
19290 }
19291 
19292 TEST_F(FormatTest, BreakPenaltyAfterForLoopLParen) {
19293   FormatStyle Style = getLLVMStyle();
19294   Style.ColumnLimit = 4;
19295   Style.PenaltyExcessCharacter = 100;
19296   verifyFormat("for (\n"
19297                "    int iiiiiiiiiiiiiiiii =\n"
19298                "        0;\n"
19299                "    iiiiiiiiiiiiiiiii <\n"
19300                "    2;\n"
19301                "    iiiiiiiiiiiiiiiii++) {\n"
19302                "}",
19303 
19304                Style);
19305   Style.PenaltyBreakOpenParenthesis = 1250;
19306   EXPECT_EQ("for (int iiiiiiiiiiiiiiiii =\n"
19307             "         0;\n"
19308             "     iiiiiiiiiiiiiiiii <\n"
19309             "     2;\n"
19310             "     iiiiiiiiiiiiiiiii++) {\n"
19311             "}",
19312             format("for (\n"
19313                    "    int iiiiiiiiiiiiiiiii =\n"
19314                    "        0;\n"
19315                    "    iiiiiiiiiiiiiiiii <\n"
19316                    "    2;\n"
19317                    "    iiiiiiiiiiiiiiiii++) {\n"
19318                    "}",
19319                    Style));
19320 }
19321 
19322 #define EXPECT_ALL_STYLES_EQUAL(Styles)                                        \
19323   for (size_t i = 1; i < Styles.size(); ++i)                                   \
19324   EXPECT_EQ(Styles[0], Styles[i])                                              \
19325       << "Style #" << i << " of " << Styles.size() << " differs from Style #0"
19326 
19327 TEST_F(FormatTest, GetsPredefinedStyleByName) {
19328   SmallVector<FormatStyle, 3> Styles;
19329   Styles.resize(3);
19330 
19331   Styles[0] = getLLVMStyle();
19332   EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1]));
19333   EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2]));
19334   EXPECT_ALL_STYLES_EQUAL(Styles);
19335 
19336   Styles[0] = getGoogleStyle();
19337   EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1]));
19338   EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2]));
19339   EXPECT_ALL_STYLES_EQUAL(Styles);
19340 
19341   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
19342   EXPECT_TRUE(
19343       getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1]));
19344   EXPECT_TRUE(
19345       getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2]));
19346   EXPECT_ALL_STYLES_EQUAL(Styles);
19347 
19348   Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp);
19349   EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1]));
19350   EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2]));
19351   EXPECT_ALL_STYLES_EQUAL(Styles);
19352 
19353   Styles[0] = getMozillaStyle();
19354   EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1]));
19355   EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2]));
19356   EXPECT_ALL_STYLES_EQUAL(Styles);
19357 
19358   Styles[0] = getWebKitStyle();
19359   EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1]));
19360   EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2]));
19361   EXPECT_ALL_STYLES_EQUAL(Styles);
19362 
19363   Styles[0] = getGNUStyle();
19364   EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1]));
19365   EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2]));
19366   EXPECT_ALL_STYLES_EQUAL(Styles);
19367 
19368   EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0]));
19369 }
19370 
19371 TEST_F(FormatTest, GetsCorrectBasedOnStyle) {
19372   SmallVector<FormatStyle, 8> Styles;
19373   Styles.resize(2);
19374 
19375   Styles[0] = getGoogleStyle();
19376   Styles[1] = getLLVMStyle();
19377   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
19378   EXPECT_ALL_STYLES_EQUAL(Styles);
19379 
19380   Styles.resize(5);
19381   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
19382   Styles[1] = getLLVMStyle();
19383   Styles[1].Language = FormatStyle::LK_JavaScript;
19384   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
19385 
19386   Styles[2] = getLLVMStyle();
19387   Styles[2].Language = FormatStyle::LK_JavaScript;
19388   EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n"
19389                                   "BasedOnStyle: Google",
19390                                   &Styles[2])
19391                    .value());
19392 
19393   Styles[3] = getLLVMStyle();
19394   Styles[3].Language = FormatStyle::LK_JavaScript;
19395   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n"
19396                                   "Language: JavaScript",
19397                                   &Styles[3])
19398                    .value());
19399 
19400   Styles[4] = getLLVMStyle();
19401   Styles[4].Language = FormatStyle::LK_JavaScript;
19402   EXPECT_EQ(0, parseConfiguration("---\n"
19403                                   "BasedOnStyle: LLVM\n"
19404                                   "IndentWidth: 123\n"
19405                                   "---\n"
19406                                   "BasedOnStyle: Google\n"
19407                                   "Language: JavaScript",
19408                                   &Styles[4])
19409                    .value());
19410   EXPECT_ALL_STYLES_EQUAL(Styles);
19411 }
19412 
19413 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME)                             \
19414   Style.FIELD = false;                                                         \
19415   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value());      \
19416   EXPECT_TRUE(Style.FIELD);                                                    \
19417   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value());     \
19418   EXPECT_FALSE(Style.FIELD);
19419 
19420 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD)
19421 
19422 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME)              \
19423   Style.STRUCT.FIELD = false;                                                  \
19424   EXPECT_EQ(0,                                                                 \
19425             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": true", &Style)   \
19426                 .value());                                                     \
19427   EXPECT_TRUE(Style.STRUCT.FIELD);                                             \
19428   EXPECT_EQ(0,                                                                 \
19429             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": false", &Style)  \
19430                 .value());                                                     \
19431   EXPECT_FALSE(Style.STRUCT.FIELD);
19432 
19433 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD)                                 \
19434   CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD)
19435 
19436 #define CHECK_PARSE(TEXT, FIELD, VALUE)                                        \
19437   EXPECT_NE(VALUE, Style.FIELD) << "Initial value already the same!";          \
19438   EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value());                      \
19439   EXPECT_EQ(VALUE, Style.FIELD) << "Unexpected value after parsing!"
19440 
19441 TEST_F(FormatTest, ParsesConfigurationBools) {
19442   FormatStyle Style = {};
19443   Style.Language = FormatStyle::LK_Cpp;
19444   CHECK_PARSE_BOOL(AlignTrailingComments);
19445   CHECK_PARSE_BOOL(AllowAllArgumentsOnNextLine);
19446   CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
19447   CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine);
19448   CHECK_PARSE_BOOL(AllowShortEnumsOnASingleLine);
19449   CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
19450   CHECK_PARSE_BOOL(BinPackArguments);
19451   CHECK_PARSE_BOOL(BinPackParameters);
19452   CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations);
19453   CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
19454   CHECK_PARSE_BOOL(BreakStringLiterals);
19455   CHECK_PARSE_BOOL(CompactNamespaces);
19456   CHECK_PARSE_BOOL(DeriveLineEnding);
19457   CHECK_PARSE_BOOL(DerivePointerAlignment);
19458   CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding");
19459   CHECK_PARSE_BOOL(DisableFormat);
19460   CHECK_PARSE_BOOL(IndentAccessModifiers);
19461   CHECK_PARSE_BOOL(IndentCaseLabels);
19462   CHECK_PARSE_BOOL(IndentCaseBlocks);
19463   CHECK_PARSE_BOOL(IndentGotoLabels);
19464   CHECK_PARSE_BOOL_FIELD(IndentRequiresClause, "IndentRequires");
19465   CHECK_PARSE_BOOL(IndentRequiresClause);
19466   CHECK_PARSE_BOOL(IndentWrappedFunctionNames);
19467   CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks);
19468   CHECK_PARSE_BOOL(ObjCSpaceAfterProperty);
19469   CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
19470   CHECK_PARSE_BOOL(Cpp11BracedListStyle);
19471   CHECK_PARSE_BOOL(ReflowComments);
19472   CHECK_PARSE_BOOL(RemoveBracesLLVM);
19473   CHECK_PARSE_BOOL(SortUsingDeclarations);
19474   CHECK_PARSE_BOOL(SpacesInParentheses);
19475   CHECK_PARSE_BOOL(SpacesInSquareBrackets);
19476   CHECK_PARSE_BOOL(SpacesInConditionalStatement);
19477   CHECK_PARSE_BOOL(SpaceInEmptyBlock);
19478   CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
19479   CHECK_PARSE_BOOL(SpacesInContainerLiterals);
19480   CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
19481   CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
19482   CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword);
19483   CHECK_PARSE_BOOL(SpaceAfterLogicalNot);
19484   CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
19485   CHECK_PARSE_BOOL(SpaceBeforeCaseColon);
19486   CHECK_PARSE_BOOL(SpaceBeforeCpp11BracedList);
19487   CHECK_PARSE_BOOL(SpaceBeforeCtorInitializerColon);
19488   CHECK_PARSE_BOOL(SpaceBeforeInheritanceColon);
19489   CHECK_PARSE_BOOL(SpaceBeforeRangeBasedForLoopColon);
19490   CHECK_PARSE_BOOL(SpaceBeforeSquareBrackets);
19491   CHECK_PARSE_BOOL(UseCRLF);
19492 
19493   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterCaseLabel);
19494   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass);
19495   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum);
19496   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction);
19497   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace);
19498   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration);
19499   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct);
19500   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion);
19501   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterExternBlock);
19502   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch);
19503   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse);
19504   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeLambdaBody);
19505   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeWhile);
19506   CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces);
19507   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyFunction);
19508   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyRecord);
19509   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyNamespace);
19510   CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterControlStatements);
19511   CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterForeachMacros);
19512   CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions,
19513                           AfterFunctionDeclarationName);
19514   CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions,
19515                           AfterFunctionDefinitionName);
19516   CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterIfMacros);
19517   CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterOverloadedOperator);
19518   CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, BeforeNonEmptyParentheses);
19519 }
19520 
19521 #undef CHECK_PARSE_BOOL
19522 
19523 TEST_F(FormatTest, ParsesConfiguration) {
19524   FormatStyle Style = {};
19525   Style.Language = FormatStyle::LK_Cpp;
19526   CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
19527   CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",
19528               ConstructorInitializerIndentWidth, 1234u);
19529   CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u);
19530   CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
19531   CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
19532   CHECK_PARSE("PenaltyBreakAssignment: 1234", PenaltyBreakAssignment, 1234u);
19533   CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234",
19534               PenaltyBreakBeforeFirstCallParameter, 1234u);
19535   CHECK_PARSE("PenaltyBreakTemplateDeclaration: 1234",
19536               PenaltyBreakTemplateDeclaration, 1234u);
19537   CHECK_PARSE("PenaltyBreakOpenParenthesis: 1234", PenaltyBreakOpenParenthesis,
19538               1234u);
19539   CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
19540   CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
19541               PenaltyReturnTypeOnItsOwnLine, 1234u);
19542   CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
19543               SpacesBeforeTrailingComments, 1234u);
19544   CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
19545   CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u);
19546   CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$");
19547 
19548   Style.QualifierAlignment = FormatStyle::QAS_Right;
19549   CHECK_PARSE("QualifierAlignment: Leave", QualifierAlignment,
19550               FormatStyle::QAS_Leave);
19551   CHECK_PARSE("QualifierAlignment: Right", QualifierAlignment,
19552               FormatStyle::QAS_Right);
19553   CHECK_PARSE("QualifierAlignment: Left", QualifierAlignment,
19554               FormatStyle::QAS_Left);
19555   CHECK_PARSE("QualifierAlignment: Custom", QualifierAlignment,
19556               FormatStyle::QAS_Custom);
19557 
19558   Style.QualifierOrder.clear();
19559   CHECK_PARSE("QualifierOrder: [ const, volatile, type ]", QualifierOrder,
19560               std::vector<std::string>({"const", "volatile", "type"}));
19561   Style.QualifierOrder.clear();
19562   CHECK_PARSE("QualifierOrder: [const, type]", QualifierOrder,
19563               std::vector<std::string>({"const", "type"}));
19564   Style.QualifierOrder.clear();
19565   CHECK_PARSE("QualifierOrder: [volatile, type]", QualifierOrder,
19566               std::vector<std::string>({"volatile", "type"}));
19567 
19568   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
19569   CHECK_PARSE("AlignConsecutiveAssignments: None", AlignConsecutiveAssignments,
19570               FormatStyle::ACS_None);
19571   CHECK_PARSE("AlignConsecutiveAssignments: Consecutive",
19572               AlignConsecutiveAssignments, FormatStyle::ACS_Consecutive);
19573   CHECK_PARSE("AlignConsecutiveAssignments: AcrossEmptyLines",
19574               AlignConsecutiveAssignments, FormatStyle::ACS_AcrossEmptyLines);
19575   CHECK_PARSE("AlignConsecutiveAssignments: AcrossEmptyLinesAndComments",
19576               AlignConsecutiveAssignments,
19577               FormatStyle::ACS_AcrossEmptyLinesAndComments);
19578   // For backwards compability, false / true should still parse
19579   CHECK_PARSE("AlignConsecutiveAssignments: false", AlignConsecutiveAssignments,
19580               FormatStyle::ACS_None);
19581   CHECK_PARSE("AlignConsecutiveAssignments: true", AlignConsecutiveAssignments,
19582               FormatStyle::ACS_Consecutive);
19583 
19584   Style.AlignConsecutiveBitFields = FormatStyle::ACS_Consecutive;
19585   CHECK_PARSE("AlignConsecutiveBitFields: None", AlignConsecutiveBitFields,
19586               FormatStyle::ACS_None);
19587   CHECK_PARSE("AlignConsecutiveBitFields: Consecutive",
19588               AlignConsecutiveBitFields, FormatStyle::ACS_Consecutive);
19589   CHECK_PARSE("AlignConsecutiveBitFields: AcrossEmptyLines",
19590               AlignConsecutiveBitFields, FormatStyle::ACS_AcrossEmptyLines);
19591   CHECK_PARSE("AlignConsecutiveBitFields: AcrossEmptyLinesAndComments",
19592               AlignConsecutiveBitFields,
19593               FormatStyle::ACS_AcrossEmptyLinesAndComments);
19594   // For backwards compability, false / true should still parse
19595   CHECK_PARSE("AlignConsecutiveBitFields: false", AlignConsecutiveBitFields,
19596               FormatStyle::ACS_None);
19597   CHECK_PARSE("AlignConsecutiveBitFields: true", AlignConsecutiveBitFields,
19598               FormatStyle::ACS_Consecutive);
19599 
19600   Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
19601   CHECK_PARSE("AlignConsecutiveMacros: None", AlignConsecutiveMacros,
19602               FormatStyle::ACS_None);
19603   CHECK_PARSE("AlignConsecutiveMacros: Consecutive", AlignConsecutiveMacros,
19604               FormatStyle::ACS_Consecutive);
19605   CHECK_PARSE("AlignConsecutiveMacros: AcrossEmptyLines",
19606               AlignConsecutiveMacros, FormatStyle::ACS_AcrossEmptyLines);
19607   CHECK_PARSE("AlignConsecutiveMacros: AcrossEmptyLinesAndComments",
19608               AlignConsecutiveMacros,
19609               FormatStyle::ACS_AcrossEmptyLinesAndComments);
19610   // For backwards compability, false / true should still parse
19611   CHECK_PARSE("AlignConsecutiveMacros: false", AlignConsecutiveMacros,
19612               FormatStyle::ACS_None);
19613   CHECK_PARSE("AlignConsecutiveMacros: true", AlignConsecutiveMacros,
19614               FormatStyle::ACS_Consecutive);
19615 
19616   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
19617   CHECK_PARSE("AlignConsecutiveDeclarations: None",
19618               AlignConsecutiveDeclarations, FormatStyle::ACS_None);
19619   CHECK_PARSE("AlignConsecutiveDeclarations: Consecutive",
19620               AlignConsecutiveDeclarations, FormatStyle::ACS_Consecutive);
19621   CHECK_PARSE("AlignConsecutiveDeclarations: AcrossEmptyLines",
19622               AlignConsecutiveDeclarations, FormatStyle::ACS_AcrossEmptyLines);
19623   CHECK_PARSE("AlignConsecutiveDeclarations: AcrossEmptyLinesAndComments",
19624               AlignConsecutiveDeclarations,
19625               FormatStyle::ACS_AcrossEmptyLinesAndComments);
19626   // For backwards compability, false / true should still parse
19627   CHECK_PARSE("AlignConsecutiveDeclarations: false",
19628               AlignConsecutiveDeclarations, FormatStyle::ACS_None);
19629   CHECK_PARSE("AlignConsecutiveDeclarations: true",
19630               AlignConsecutiveDeclarations, FormatStyle::ACS_Consecutive);
19631 
19632   Style.PointerAlignment = FormatStyle::PAS_Middle;
19633   CHECK_PARSE("PointerAlignment: Left", PointerAlignment,
19634               FormatStyle::PAS_Left);
19635   CHECK_PARSE("PointerAlignment: Right", PointerAlignment,
19636               FormatStyle::PAS_Right);
19637   CHECK_PARSE("PointerAlignment: Middle", PointerAlignment,
19638               FormatStyle::PAS_Middle);
19639   Style.ReferenceAlignment = FormatStyle::RAS_Middle;
19640   CHECK_PARSE("ReferenceAlignment: Pointer", ReferenceAlignment,
19641               FormatStyle::RAS_Pointer);
19642   CHECK_PARSE("ReferenceAlignment: Left", ReferenceAlignment,
19643               FormatStyle::RAS_Left);
19644   CHECK_PARSE("ReferenceAlignment: Right", ReferenceAlignment,
19645               FormatStyle::RAS_Right);
19646   CHECK_PARSE("ReferenceAlignment: Middle", ReferenceAlignment,
19647               FormatStyle::RAS_Middle);
19648   // For backward compatibility:
19649   CHECK_PARSE("PointerBindsToType: Left", PointerAlignment,
19650               FormatStyle::PAS_Left);
19651   CHECK_PARSE("PointerBindsToType: Right", PointerAlignment,
19652               FormatStyle::PAS_Right);
19653   CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment,
19654               FormatStyle::PAS_Middle);
19655 
19656   Style.Standard = FormatStyle::LS_Auto;
19657   CHECK_PARSE("Standard: c++03", Standard, FormatStyle::LS_Cpp03);
19658   CHECK_PARSE("Standard: c++11", Standard, FormatStyle::LS_Cpp11);
19659   CHECK_PARSE("Standard: c++14", Standard, FormatStyle::LS_Cpp14);
19660   CHECK_PARSE("Standard: c++17", Standard, FormatStyle::LS_Cpp17);
19661   CHECK_PARSE("Standard: c++20", Standard, FormatStyle::LS_Cpp20);
19662   CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
19663   CHECK_PARSE("Standard: Latest", Standard, FormatStyle::LS_Latest);
19664   // Legacy aliases:
19665   CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03);
19666   CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Latest);
19667   CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
19668   CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
19669 
19670   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
19671   CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment",
19672               BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment);
19673   CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators,
19674               FormatStyle::BOS_None);
19675   CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators,
19676               FormatStyle::BOS_All);
19677   // For backward compatibility:
19678   CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators,
19679               FormatStyle::BOS_None);
19680   CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators,
19681               FormatStyle::BOS_All);
19682 
19683   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
19684   CHECK_PARSE("BreakConstructorInitializers: BeforeComma",
19685               BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma);
19686   CHECK_PARSE("BreakConstructorInitializers: AfterColon",
19687               BreakConstructorInitializers, FormatStyle::BCIS_AfterColon);
19688   CHECK_PARSE("BreakConstructorInitializers: BeforeColon",
19689               BreakConstructorInitializers, FormatStyle::BCIS_BeforeColon);
19690   // For backward compatibility:
19691   CHECK_PARSE("BreakConstructorInitializersBeforeComma: true",
19692               BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma);
19693 
19694   Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
19695   CHECK_PARSE("BreakInheritanceList: AfterComma", BreakInheritanceList,
19696               FormatStyle::BILS_AfterComma);
19697   CHECK_PARSE("BreakInheritanceList: BeforeComma", BreakInheritanceList,
19698               FormatStyle::BILS_BeforeComma);
19699   CHECK_PARSE("BreakInheritanceList: AfterColon", BreakInheritanceList,
19700               FormatStyle::BILS_AfterColon);
19701   CHECK_PARSE("BreakInheritanceList: BeforeColon", BreakInheritanceList,
19702               FormatStyle::BILS_BeforeColon);
19703   // For backward compatibility:
19704   CHECK_PARSE("BreakBeforeInheritanceComma: true", BreakInheritanceList,
19705               FormatStyle::BILS_BeforeComma);
19706 
19707   Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack;
19708   CHECK_PARSE("PackConstructorInitializers: Never", PackConstructorInitializers,
19709               FormatStyle::PCIS_Never);
19710   CHECK_PARSE("PackConstructorInitializers: BinPack",
19711               PackConstructorInitializers, FormatStyle::PCIS_BinPack);
19712   CHECK_PARSE("PackConstructorInitializers: CurrentLine",
19713               PackConstructorInitializers, FormatStyle::PCIS_CurrentLine);
19714   CHECK_PARSE("PackConstructorInitializers: NextLine",
19715               PackConstructorInitializers, FormatStyle::PCIS_NextLine);
19716   // For backward compatibility:
19717   CHECK_PARSE("BasedOnStyle: Google\n"
19718               "ConstructorInitializerAllOnOneLineOrOnePerLine: true\n"
19719               "AllowAllConstructorInitializersOnNextLine: false",
19720               PackConstructorInitializers, FormatStyle::PCIS_CurrentLine);
19721   Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
19722   CHECK_PARSE("BasedOnStyle: Google\n"
19723               "ConstructorInitializerAllOnOneLineOrOnePerLine: false",
19724               PackConstructorInitializers, FormatStyle::PCIS_BinPack);
19725   CHECK_PARSE("ConstructorInitializerAllOnOneLineOrOnePerLine: true\n"
19726               "AllowAllConstructorInitializersOnNextLine: true",
19727               PackConstructorInitializers, FormatStyle::PCIS_NextLine);
19728   Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack;
19729   CHECK_PARSE("ConstructorInitializerAllOnOneLineOrOnePerLine: true\n"
19730               "AllowAllConstructorInitializersOnNextLine: false",
19731               PackConstructorInitializers, FormatStyle::PCIS_CurrentLine);
19732 
19733   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
19734   CHECK_PARSE("EmptyLineBeforeAccessModifier: Never",
19735               EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Never);
19736   CHECK_PARSE("EmptyLineBeforeAccessModifier: Leave",
19737               EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Leave);
19738   CHECK_PARSE("EmptyLineBeforeAccessModifier: LogicalBlock",
19739               EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_LogicalBlock);
19740   CHECK_PARSE("EmptyLineBeforeAccessModifier: Always",
19741               EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Always);
19742 
19743   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
19744   CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket,
19745               FormatStyle::BAS_Align);
19746   CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket,
19747               FormatStyle::BAS_DontAlign);
19748   CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket,
19749               FormatStyle::BAS_AlwaysBreak);
19750   CHECK_PARSE("AlignAfterOpenBracket: BlockIndent", AlignAfterOpenBracket,
19751               FormatStyle::BAS_BlockIndent);
19752   // For backward compatibility:
19753   CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket,
19754               FormatStyle::BAS_DontAlign);
19755   CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket,
19756               FormatStyle::BAS_Align);
19757 
19758   Style.AlignEscapedNewlines = FormatStyle::ENAS_Left;
19759   CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines,
19760               FormatStyle::ENAS_DontAlign);
19761   CHECK_PARSE("AlignEscapedNewlines: Left", AlignEscapedNewlines,
19762               FormatStyle::ENAS_Left);
19763   CHECK_PARSE("AlignEscapedNewlines: Right", AlignEscapedNewlines,
19764               FormatStyle::ENAS_Right);
19765   // For backward compatibility:
19766   CHECK_PARSE("AlignEscapedNewlinesLeft: true", AlignEscapedNewlines,
19767               FormatStyle::ENAS_Left);
19768   CHECK_PARSE("AlignEscapedNewlinesLeft: false", AlignEscapedNewlines,
19769               FormatStyle::ENAS_Right);
19770 
19771   Style.AlignOperands = FormatStyle::OAS_Align;
19772   CHECK_PARSE("AlignOperands: DontAlign", AlignOperands,
19773               FormatStyle::OAS_DontAlign);
19774   CHECK_PARSE("AlignOperands: Align", AlignOperands, FormatStyle::OAS_Align);
19775   CHECK_PARSE("AlignOperands: AlignAfterOperator", AlignOperands,
19776               FormatStyle::OAS_AlignAfterOperator);
19777   // For backward compatibility:
19778   CHECK_PARSE("AlignOperands: false", AlignOperands,
19779               FormatStyle::OAS_DontAlign);
19780   CHECK_PARSE("AlignOperands: true", AlignOperands, FormatStyle::OAS_Align);
19781 
19782   Style.UseTab = FormatStyle::UT_ForIndentation;
19783   CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
19784   CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
19785   CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
19786   CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab,
19787               FormatStyle::UT_ForContinuationAndIndentation);
19788   CHECK_PARSE("UseTab: AlignWithSpaces", UseTab,
19789               FormatStyle::UT_AlignWithSpaces);
19790   // For backward compatibility:
19791   CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never);
19792   CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always);
19793 
19794   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty;
19795   CHECK_PARSE("AllowShortBlocksOnASingleLine: Never",
19796               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never);
19797   CHECK_PARSE("AllowShortBlocksOnASingleLine: Empty",
19798               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Empty);
19799   CHECK_PARSE("AllowShortBlocksOnASingleLine: Always",
19800               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always);
19801   // For backward compatibility:
19802   CHECK_PARSE("AllowShortBlocksOnASingleLine: false",
19803               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never);
19804   CHECK_PARSE("AllowShortBlocksOnASingleLine: true",
19805               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always);
19806 
19807   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
19808   CHECK_PARSE("AllowShortFunctionsOnASingleLine: None",
19809               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
19810   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline",
19811               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline);
19812   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty",
19813               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty);
19814   CHECK_PARSE("AllowShortFunctionsOnASingleLine: All",
19815               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
19816   // For backward compatibility:
19817   CHECK_PARSE("AllowShortFunctionsOnASingleLine: false",
19818               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
19819   CHECK_PARSE("AllowShortFunctionsOnASingleLine: true",
19820               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
19821 
19822   Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Both;
19823   CHECK_PARSE("SpaceAroundPointerQualifiers: Default",
19824               SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Default);
19825   CHECK_PARSE("SpaceAroundPointerQualifiers: Before",
19826               SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Before);
19827   CHECK_PARSE("SpaceAroundPointerQualifiers: After",
19828               SpaceAroundPointerQualifiers, FormatStyle::SAPQ_After);
19829   CHECK_PARSE("SpaceAroundPointerQualifiers: Both",
19830               SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Both);
19831 
19832   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
19833   CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens,
19834               FormatStyle::SBPO_Never);
19835   CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens,
19836               FormatStyle::SBPO_Always);
19837   CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens,
19838               FormatStyle::SBPO_ControlStatements);
19839   CHECK_PARSE("SpaceBeforeParens: ControlStatementsExceptControlMacros",
19840               SpaceBeforeParens,
19841               FormatStyle::SBPO_ControlStatementsExceptControlMacros);
19842   CHECK_PARSE("SpaceBeforeParens: NonEmptyParentheses", SpaceBeforeParens,
19843               FormatStyle::SBPO_NonEmptyParentheses);
19844   CHECK_PARSE("SpaceBeforeParens: Custom", SpaceBeforeParens,
19845               FormatStyle::SBPO_Custom);
19846   // For backward compatibility:
19847   CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens,
19848               FormatStyle::SBPO_Never);
19849   CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens,
19850               FormatStyle::SBPO_ControlStatements);
19851   CHECK_PARSE("SpaceBeforeParens: ControlStatementsExceptForEachMacros",
19852               SpaceBeforeParens,
19853               FormatStyle::SBPO_ControlStatementsExceptControlMacros);
19854 
19855   Style.ColumnLimit = 123;
19856   FormatStyle BaseStyle = getLLVMStyle();
19857   CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
19858   CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
19859 
19860   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
19861   CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
19862               FormatStyle::BS_Attach);
19863   CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
19864               FormatStyle::BS_Linux);
19865   CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces,
19866               FormatStyle::BS_Mozilla);
19867   CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
19868               FormatStyle::BS_Stroustrup);
19869   CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces,
19870               FormatStyle::BS_Allman);
19871   CHECK_PARSE("BreakBeforeBraces: Whitesmiths", BreakBeforeBraces,
19872               FormatStyle::BS_Whitesmiths);
19873   CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU);
19874   CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces,
19875               FormatStyle::BS_WebKit);
19876   CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces,
19877               FormatStyle::BS_Custom);
19878 
19879   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never;
19880   CHECK_PARSE("BraceWrapping:\n"
19881               "  AfterControlStatement: MultiLine",
19882               BraceWrapping.AfterControlStatement,
19883               FormatStyle::BWACS_MultiLine);
19884   CHECK_PARSE("BraceWrapping:\n"
19885               "  AfterControlStatement: Always",
19886               BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always);
19887   CHECK_PARSE("BraceWrapping:\n"
19888               "  AfterControlStatement: Never",
19889               BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never);
19890   // For backward compatibility:
19891   CHECK_PARSE("BraceWrapping:\n"
19892               "  AfterControlStatement: true",
19893               BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always);
19894   CHECK_PARSE("BraceWrapping:\n"
19895               "  AfterControlStatement: false",
19896               BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never);
19897 
19898   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
19899   CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType,
19900               FormatStyle::RTBS_None);
19901   CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType,
19902               FormatStyle::RTBS_All);
19903   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel",
19904               AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel);
19905   CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions",
19906               AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions);
19907   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions",
19908               AlwaysBreakAfterReturnType,
19909               FormatStyle::RTBS_TopLevelDefinitions);
19910 
19911   Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
19912   CHECK_PARSE("AlwaysBreakTemplateDeclarations: No",
19913               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_No);
19914   CHECK_PARSE("AlwaysBreakTemplateDeclarations: MultiLine",
19915               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine);
19916   CHECK_PARSE("AlwaysBreakTemplateDeclarations: Yes",
19917               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes);
19918   CHECK_PARSE("AlwaysBreakTemplateDeclarations: false",
19919               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine);
19920   CHECK_PARSE("AlwaysBreakTemplateDeclarations: true",
19921               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes);
19922 
19923   Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
19924   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None",
19925               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None);
19926   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All",
19927               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All);
19928   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel",
19929               AlwaysBreakAfterDefinitionReturnType,
19930               FormatStyle::DRTBS_TopLevel);
19931 
19932   Style.NamespaceIndentation = FormatStyle::NI_All;
19933   CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
19934               FormatStyle::NI_None);
19935   CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation,
19936               FormatStyle::NI_Inner);
19937   CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
19938               FormatStyle::NI_All);
19939 
19940   Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_OnlyFirstIf;
19941   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Never",
19942               AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never);
19943   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: WithoutElse",
19944               AllowShortIfStatementsOnASingleLine,
19945               FormatStyle::SIS_WithoutElse);
19946   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: OnlyFirstIf",
19947               AllowShortIfStatementsOnASingleLine,
19948               FormatStyle::SIS_OnlyFirstIf);
19949   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: AllIfsAndElse",
19950               AllowShortIfStatementsOnASingleLine,
19951               FormatStyle::SIS_AllIfsAndElse);
19952   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Always",
19953               AllowShortIfStatementsOnASingleLine,
19954               FormatStyle::SIS_OnlyFirstIf);
19955   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: false",
19956               AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never);
19957   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: true",
19958               AllowShortIfStatementsOnASingleLine,
19959               FormatStyle::SIS_WithoutElse);
19960 
19961   Style.IndentExternBlock = FormatStyle::IEBS_NoIndent;
19962   CHECK_PARSE("IndentExternBlock: AfterExternBlock", IndentExternBlock,
19963               FormatStyle::IEBS_AfterExternBlock);
19964   CHECK_PARSE("IndentExternBlock: Indent", IndentExternBlock,
19965               FormatStyle::IEBS_Indent);
19966   CHECK_PARSE("IndentExternBlock: NoIndent", IndentExternBlock,
19967               FormatStyle::IEBS_NoIndent);
19968   CHECK_PARSE("IndentExternBlock: true", IndentExternBlock,
19969               FormatStyle::IEBS_Indent);
19970   CHECK_PARSE("IndentExternBlock: false", IndentExternBlock,
19971               FormatStyle::IEBS_NoIndent);
19972 
19973   Style.BitFieldColonSpacing = FormatStyle::BFCS_None;
19974   CHECK_PARSE("BitFieldColonSpacing: Both", BitFieldColonSpacing,
19975               FormatStyle::BFCS_Both);
19976   CHECK_PARSE("BitFieldColonSpacing: None", BitFieldColonSpacing,
19977               FormatStyle::BFCS_None);
19978   CHECK_PARSE("BitFieldColonSpacing: Before", BitFieldColonSpacing,
19979               FormatStyle::BFCS_Before);
19980   CHECK_PARSE("BitFieldColonSpacing: After", BitFieldColonSpacing,
19981               FormatStyle::BFCS_After);
19982 
19983   Style.SortJavaStaticImport = FormatStyle::SJSIO_Before;
19984   CHECK_PARSE("SortJavaStaticImport: After", SortJavaStaticImport,
19985               FormatStyle::SJSIO_After);
19986   CHECK_PARSE("SortJavaStaticImport: Before", SortJavaStaticImport,
19987               FormatStyle::SJSIO_Before);
19988 
19989   // FIXME: This is required because parsing a configuration simply overwrites
19990   // the first N elements of the list instead of resetting it.
19991   Style.ForEachMacros.clear();
19992   std::vector<std::string> BoostForeach;
19993   BoostForeach.push_back("BOOST_FOREACH");
19994   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach);
19995   std::vector<std::string> BoostAndQForeach;
19996   BoostAndQForeach.push_back("BOOST_FOREACH");
19997   BoostAndQForeach.push_back("Q_FOREACH");
19998   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
19999               BoostAndQForeach);
20000 
20001   Style.IfMacros.clear();
20002   std::vector<std::string> CustomIfs;
20003   CustomIfs.push_back("MYIF");
20004   CHECK_PARSE("IfMacros: [MYIF]", IfMacros, CustomIfs);
20005 
20006   Style.AttributeMacros.clear();
20007   CHECK_PARSE("BasedOnStyle: LLVM", AttributeMacros,
20008               std::vector<std::string>{"__capability"});
20009   CHECK_PARSE("AttributeMacros: [attr1, attr2]", AttributeMacros,
20010               std::vector<std::string>({"attr1", "attr2"}));
20011 
20012   Style.StatementAttributeLikeMacros.clear();
20013   CHECK_PARSE("StatementAttributeLikeMacros: [emit,Q_EMIT]",
20014               StatementAttributeLikeMacros,
20015               std::vector<std::string>({"emit", "Q_EMIT"}));
20016 
20017   Style.StatementMacros.clear();
20018   CHECK_PARSE("StatementMacros: [QUNUSED]", StatementMacros,
20019               std::vector<std::string>{"QUNUSED"});
20020   CHECK_PARSE("StatementMacros: [QUNUSED, QT_REQUIRE_VERSION]", StatementMacros,
20021               std::vector<std::string>({"QUNUSED", "QT_REQUIRE_VERSION"}));
20022 
20023   Style.NamespaceMacros.clear();
20024   CHECK_PARSE("NamespaceMacros: [TESTSUITE]", NamespaceMacros,
20025               std::vector<std::string>{"TESTSUITE"});
20026   CHECK_PARSE("NamespaceMacros: [TESTSUITE, SUITE]", NamespaceMacros,
20027               std::vector<std::string>({"TESTSUITE", "SUITE"}));
20028 
20029   Style.WhitespaceSensitiveMacros.clear();
20030   CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE]",
20031               WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"});
20032   CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE, ASSERT]",
20033               WhitespaceSensitiveMacros,
20034               std::vector<std::string>({"STRINGIZE", "ASSERT"}));
20035   Style.WhitespaceSensitiveMacros.clear();
20036   CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE']",
20037               WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"});
20038   CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE', 'ASSERT']",
20039               WhitespaceSensitiveMacros,
20040               std::vector<std::string>({"STRINGIZE", "ASSERT"}));
20041 
20042   Style.IncludeStyle.IncludeCategories.clear();
20043   std::vector<tooling::IncludeStyle::IncludeCategory> ExpectedCategories = {
20044       {"abc/.*", 2, 0, false}, {".*", 1, 0, true}};
20045   CHECK_PARSE("IncludeCategories:\n"
20046               "  - Regex: abc/.*\n"
20047               "    Priority: 2\n"
20048               "  - Regex: .*\n"
20049               "    Priority: 1\n"
20050               "    CaseSensitive: true\n",
20051               IncludeStyle.IncludeCategories, ExpectedCategories);
20052   CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeStyle.IncludeIsMainRegex,
20053               "abc$");
20054   CHECK_PARSE("IncludeIsMainSourceRegex: 'abc$'",
20055               IncludeStyle.IncludeIsMainSourceRegex, "abc$");
20056 
20057   Style.SortIncludes = FormatStyle::SI_Never;
20058   CHECK_PARSE("SortIncludes: true", SortIncludes,
20059               FormatStyle::SI_CaseSensitive);
20060   CHECK_PARSE("SortIncludes: false", SortIncludes, FormatStyle::SI_Never);
20061   CHECK_PARSE("SortIncludes: CaseInsensitive", SortIncludes,
20062               FormatStyle::SI_CaseInsensitive);
20063   CHECK_PARSE("SortIncludes: CaseSensitive", SortIncludes,
20064               FormatStyle::SI_CaseSensitive);
20065   CHECK_PARSE("SortIncludes: Never", SortIncludes, FormatStyle::SI_Never);
20066 
20067   Style.RawStringFormats.clear();
20068   std::vector<FormatStyle::RawStringFormat> ExpectedRawStringFormats = {
20069       {
20070           FormatStyle::LK_TextProto,
20071           {"pb", "proto"},
20072           {"PARSE_TEXT_PROTO"},
20073           /*CanonicalDelimiter=*/"",
20074           "llvm",
20075       },
20076       {
20077           FormatStyle::LK_Cpp,
20078           {"cc", "cpp"},
20079           {"C_CODEBLOCK", "CPPEVAL"},
20080           /*CanonicalDelimiter=*/"cc",
20081           /*BasedOnStyle=*/"",
20082       },
20083   };
20084 
20085   CHECK_PARSE("RawStringFormats:\n"
20086               "  - Language: TextProto\n"
20087               "    Delimiters:\n"
20088               "      - 'pb'\n"
20089               "      - 'proto'\n"
20090               "    EnclosingFunctions:\n"
20091               "      - 'PARSE_TEXT_PROTO'\n"
20092               "    BasedOnStyle: llvm\n"
20093               "  - Language: Cpp\n"
20094               "    Delimiters:\n"
20095               "      - 'cc'\n"
20096               "      - 'cpp'\n"
20097               "    EnclosingFunctions:\n"
20098               "      - 'C_CODEBLOCK'\n"
20099               "      - 'CPPEVAL'\n"
20100               "    CanonicalDelimiter: 'cc'",
20101               RawStringFormats, ExpectedRawStringFormats);
20102 
20103   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
20104               "  Minimum: 0\n"
20105               "  Maximum: 0",
20106               SpacesInLineCommentPrefix.Minimum, 0u);
20107   EXPECT_EQ(Style.SpacesInLineCommentPrefix.Maximum, 0u);
20108   Style.SpacesInLineCommentPrefix.Minimum = 1;
20109   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
20110               "  Minimum: 2",
20111               SpacesInLineCommentPrefix.Minimum, 0u);
20112   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
20113               "  Maximum: -1",
20114               SpacesInLineCommentPrefix.Maximum, -1u);
20115   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
20116               "  Minimum: 2",
20117               SpacesInLineCommentPrefix.Minimum, 2u);
20118   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
20119               "  Maximum: 1",
20120               SpacesInLineCommentPrefix.Maximum, 1u);
20121   EXPECT_EQ(Style.SpacesInLineCommentPrefix.Minimum, 1u);
20122 
20123   Style.SpacesInAngles = FormatStyle::SIAS_Always;
20124   CHECK_PARSE("SpacesInAngles: Never", SpacesInAngles, FormatStyle::SIAS_Never);
20125   CHECK_PARSE("SpacesInAngles: Always", SpacesInAngles,
20126               FormatStyle::SIAS_Always);
20127   CHECK_PARSE("SpacesInAngles: Leave", SpacesInAngles, FormatStyle::SIAS_Leave);
20128   // For backward compatibility:
20129   CHECK_PARSE("SpacesInAngles: false", SpacesInAngles, FormatStyle::SIAS_Never);
20130   CHECK_PARSE("SpacesInAngles: true", SpacesInAngles, FormatStyle::SIAS_Always);
20131 
20132   CHECK_PARSE("RequiresClausePosition: WithPreceding", RequiresClausePosition,
20133               FormatStyle::RCPS_WithPreceding);
20134   CHECK_PARSE("RequiresClausePosition: WithFollowing", RequiresClausePosition,
20135               FormatStyle::RCPS_WithFollowing);
20136   CHECK_PARSE("RequiresClausePosition: SingleLine", RequiresClausePosition,
20137               FormatStyle::RCPS_SingleLine);
20138   CHECK_PARSE("RequiresClausePosition: OwnLine", RequiresClausePosition,
20139               FormatStyle::RCPS_OwnLine);
20140 
20141   CHECK_PARSE("BreakBeforeConceptDeclarations: Never",
20142               BreakBeforeConceptDeclarations, FormatStyle::BBCDS_Never);
20143   CHECK_PARSE("BreakBeforeConceptDeclarations: Always",
20144               BreakBeforeConceptDeclarations, FormatStyle::BBCDS_Always);
20145   CHECK_PARSE("BreakBeforeConceptDeclarations: Allowed",
20146               BreakBeforeConceptDeclarations, FormatStyle::BBCDS_Allowed);
20147   // For backward compatibility:
20148   CHECK_PARSE("BreakBeforeConceptDeclarations: true",
20149               BreakBeforeConceptDeclarations, FormatStyle::BBCDS_Always);
20150   CHECK_PARSE("BreakBeforeConceptDeclarations: false",
20151               BreakBeforeConceptDeclarations, FormatStyle::BBCDS_Allowed);
20152 }
20153 
20154 TEST_F(FormatTest, ParsesConfigurationWithLanguages) {
20155   FormatStyle Style = {};
20156   Style.Language = FormatStyle::LK_Cpp;
20157   CHECK_PARSE("Language: Cpp\n"
20158               "IndentWidth: 12",
20159               IndentWidth, 12u);
20160   EXPECT_EQ(parseConfiguration("Language: JavaScript\n"
20161                                "IndentWidth: 34",
20162                                &Style),
20163             ParseError::Unsuitable);
20164   FormatStyle BinPackedTCS = {};
20165   BinPackedTCS.Language = FormatStyle::LK_JavaScript;
20166   EXPECT_EQ(parseConfiguration("BinPackArguments: true\n"
20167                                "InsertTrailingCommas: Wrapped",
20168                                &BinPackedTCS),
20169             ParseError::BinPackTrailingCommaConflict);
20170   EXPECT_EQ(12u, Style.IndentWidth);
20171   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
20172   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
20173 
20174   Style.Language = FormatStyle::LK_JavaScript;
20175   CHECK_PARSE("Language: JavaScript\n"
20176               "IndentWidth: 12",
20177               IndentWidth, 12u);
20178   CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u);
20179   EXPECT_EQ(parseConfiguration("Language: Cpp\n"
20180                                "IndentWidth: 34",
20181                                &Style),
20182             ParseError::Unsuitable);
20183   EXPECT_EQ(23u, Style.IndentWidth);
20184   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
20185   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
20186 
20187   CHECK_PARSE("BasedOnStyle: LLVM\n"
20188               "IndentWidth: 67",
20189               IndentWidth, 67u);
20190 
20191   CHECK_PARSE("---\n"
20192               "Language: JavaScript\n"
20193               "IndentWidth: 12\n"
20194               "---\n"
20195               "Language: Cpp\n"
20196               "IndentWidth: 34\n"
20197               "...\n",
20198               IndentWidth, 12u);
20199 
20200   Style.Language = FormatStyle::LK_Cpp;
20201   CHECK_PARSE("---\n"
20202               "Language: JavaScript\n"
20203               "IndentWidth: 12\n"
20204               "---\n"
20205               "Language: Cpp\n"
20206               "IndentWidth: 34\n"
20207               "...\n",
20208               IndentWidth, 34u);
20209   CHECK_PARSE("---\n"
20210               "IndentWidth: 78\n"
20211               "---\n"
20212               "Language: JavaScript\n"
20213               "IndentWidth: 56\n"
20214               "...\n",
20215               IndentWidth, 78u);
20216 
20217   Style.ColumnLimit = 123;
20218   Style.IndentWidth = 234;
20219   Style.BreakBeforeBraces = FormatStyle::BS_Linux;
20220   Style.TabWidth = 345;
20221   EXPECT_FALSE(parseConfiguration("---\n"
20222                                   "IndentWidth: 456\n"
20223                                   "BreakBeforeBraces: Allman\n"
20224                                   "---\n"
20225                                   "Language: JavaScript\n"
20226                                   "IndentWidth: 111\n"
20227                                   "TabWidth: 111\n"
20228                                   "---\n"
20229                                   "Language: Cpp\n"
20230                                   "BreakBeforeBraces: Stroustrup\n"
20231                                   "TabWidth: 789\n"
20232                                   "...\n",
20233                                   &Style));
20234   EXPECT_EQ(123u, Style.ColumnLimit);
20235   EXPECT_EQ(456u, Style.IndentWidth);
20236   EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces);
20237   EXPECT_EQ(789u, Style.TabWidth);
20238 
20239   EXPECT_EQ(parseConfiguration("---\n"
20240                                "Language: JavaScript\n"
20241                                "IndentWidth: 56\n"
20242                                "---\n"
20243                                "IndentWidth: 78\n"
20244                                "...\n",
20245                                &Style),
20246             ParseError::Error);
20247   EXPECT_EQ(parseConfiguration("---\n"
20248                                "Language: JavaScript\n"
20249                                "IndentWidth: 56\n"
20250                                "---\n"
20251                                "Language: JavaScript\n"
20252                                "IndentWidth: 78\n"
20253                                "...\n",
20254                                &Style),
20255             ParseError::Error);
20256 
20257   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
20258 }
20259 
20260 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) {
20261   FormatStyle Style = {};
20262   Style.Language = FormatStyle::LK_JavaScript;
20263   Style.BreakBeforeTernaryOperators = true;
20264   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value());
20265   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
20266 
20267   Style.BreakBeforeTernaryOperators = true;
20268   EXPECT_EQ(0, parseConfiguration("---\n"
20269                                   "BasedOnStyle: Google\n"
20270                                   "---\n"
20271                                   "Language: JavaScript\n"
20272                                   "IndentWidth: 76\n"
20273                                   "...\n",
20274                                   &Style)
20275                    .value());
20276   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
20277   EXPECT_EQ(76u, Style.IndentWidth);
20278   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
20279 }
20280 
20281 TEST_F(FormatTest, ConfigurationRoundTripTest) {
20282   FormatStyle Style = getLLVMStyle();
20283   std::string YAML = configurationAsText(Style);
20284   FormatStyle ParsedStyle = {};
20285   ParsedStyle.Language = FormatStyle::LK_Cpp;
20286   EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
20287   EXPECT_EQ(Style, ParsedStyle);
20288 }
20289 
20290 TEST_F(FormatTest, WorksFor8bitEncodings) {
20291   EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
20292             "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
20293             "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
20294             "\"\xef\xee\xf0\xf3...\"",
20295             format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
20296                    "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
20297                    "\xef\xee\xf0\xf3...\"",
20298                    getLLVMStyleWithColumns(12)));
20299 }
20300 
20301 TEST_F(FormatTest, HandlesUTF8BOM) {
20302   EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf"));
20303   EXPECT_EQ("\xef\xbb\xbf#include <iostream>",
20304             format("\xef\xbb\xbf#include <iostream>"));
20305   EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>",
20306             format("\xef\xbb\xbf\n#include <iostream>"));
20307 }
20308 
20309 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
20310 #if !defined(_MSC_VER)
20311 
20312 TEST_F(FormatTest, CountsUTF8CharactersProperly) {
20313   verifyFormat("\"Однажды в студёную зимнюю пору...\"",
20314                getLLVMStyleWithColumns(35));
20315   verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
20316                getLLVMStyleWithColumns(31));
20317   verifyFormat("// Однажды в студёную зимнюю пору...",
20318                getLLVMStyleWithColumns(36));
20319   verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32));
20320   verifyFormat("/* Однажды в студёную зимнюю пору... */",
20321                getLLVMStyleWithColumns(39));
20322   verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
20323                getLLVMStyleWithColumns(35));
20324 }
20325 
20326 TEST_F(FormatTest, SplitsUTF8Strings) {
20327   // Non-printable characters' width is currently considered to be the length in
20328   // bytes in UTF8. The characters can be displayed in very different manner
20329   // (zero-width, single width with a substitution glyph, expanded to their code
20330   // (e.g. "<8d>"), so there's no single correct way to handle them.
20331   EXPECT_EQ("\"aaaaÄ\"\n"
20332             "\"\xc2\x8d\";",
20333             format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
20334   EXPECT_EQ("\"aaaaaaaÄ\"\n"
20335             "\"\xc2\x8d\";",
20336             format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
20337   EXPECT_EQ("\"Однажды, в \"\n"
20338             "\"студёную \"\n"
20339             "\"зимнюю \"\n"
20340             "\"пору,\"",
20341             format("\"Однажды, в студёную зимнюю пору,\"",
20342                    getLLVMStyleWithColumns(13)));
20343   EXPECT_EQ(
20344       "\"一 二 三 \"\n"
20345       "\"四 五六 \"\n"
20346       "\"七 八 九 \"\n"
20347       "\"十\"",
20348       format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11)));
20349   EXPECT_EQ("\"一\t\"\n"
20350             "\"二 \t\"\n"
20351             "\"三 四 \"\n"
20352             "\"五\t\"\n"
20353             "\"六 \t\"\n"
20354             "\"七 \"\n"
20355             "\"八九十\tqq\"",
20356             format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
20357                    getLLVMStyleWithColumns(11)));
20358 
20359   // UTF8 character in an escape sequence.
20360   EXPECT_EQ("\"aaaaaa\"\n"
20361             "\"\\\xC2\x8D\"",
20362             format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10)));
20363 }
20364 
20365 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
20366   EXPECT_EQ("const char *sssss =\n"
20367             "    \"一二三四五六七八\\\n"
20368             " 九 十\";",
20369             format("const char *sssss = \"一二三四五六七八\\\n"
20370                    " 九 十\";",
20371                    getLLVMStyleWithColumns(30)));
20372 }
20373 
20374 TEST_F(FormatTest, SplitsUTF8LineComments) {
20375   EXPECT_EQ("// aaaaÄ\xc2\x8d",
20376             format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10)));
20377   EXPECT_EQ("// Я из лесу\n"
20378             "// вышел; был\n"
20379             "// сильный\n"
20380             "// мороз.",
20381             format("// Я из лесу вышел; был сильный мороз.",
20382                    getLLVMStyleWithColumns(13)));
20383   EXPECT_EQ("// 一二三\n"
20384             "// 四五六七\n"
20385             "// 八  九\n"
20386             "// 十",
20387             format("// 一二三 四五六七 八  九 十", getLLVMStyleWithColumns(9)));
20388 }
20389 
20390 TEST_F(FormatTest, SplitsUTF8BlockComments) {
20391   EXPECT_EQ("/* Гляжу,\n"
20392             " * поднимается\n"
20393             " * медленно в\n"
20394             " * гору\n"
20395             " * Лошадка,\n"
20396             " * везущая\n"
20397             " * хворосту\n"
20398             " * воз. */",
20399             format("/* Гляжу, поднимается медленно в гору\n"
20400                    " * Лошадка, везущая хворосту воз. */",
20401                    getLLVMStyleWithColumns(13)));
20402   EXPECT_EQ(
20403       "/* 一二三\n"
20404       " * 四五六七\n"
20405       " * 八  九\n"
20406       " * 十  */",
20407       format("/* 一二三 四五六七 八  九 十  */", getLLVMStyleWithColumns(9)));
20408   EXPECT_EQ("/* �������� ��������\n"
20409             " * ��������\n"
20410             " * ������-�� */",
20411             format("/* �������� �������� �������� ������-�� */", getLLVMStyleWithColumns(12)));
20412 }
20413 
20414 #endif // _MSC_VER
20415 
20416 TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
20417   FormatStyle Style = getLLVMStyle();
20418 
20419   Style.ConstructorInitializerIndentWidth = 4;
20420   verifyFormat(
20421       "SomeClass::Constructor()\n"
20422       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
20423       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
20424       Style);
20425 
20426   Style.ConstructorInitializerIndentWidth = 2;
20427   verifyFormat(
20428       "SomeClass::Constructor()\n"
20429       "  : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
20430       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
20431       Style);
20432 
20433   Style.ConstructorInitializerIndentWidth = 0;
20434   verifyFormat(
20435       "SomeClass::Constructor()\n"
20436       ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
20437       "  aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
20438       Style);
20439   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
20440   verifyFormat(
20441       "SomeLongTemplateVariableName<\n"
20442       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>",
20443       Style);
20444   verifyFormat("bool smaller = 1 < "
20445                "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
20446                "                       "
20447                "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
20448                Style);
20449 
20450   Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
20451   verifyFormat("SomeClass::Constructor() :\n"
20452                "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa),\n"
20453                "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa) {}",
20454                Style);
20455 }
20456 
20457 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {
20458   FormatStyle Style = getLLVMStyle();
20459   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
20460   Style.ConstructorInitializerIndentWidth = 4;
20461   verifyFormat("SomeClass::Constructor()\n"
20462                "    : a(a)\n"
20463                "    , b(b)\n"
20464                "    , c(c) {}",
20465                Style);
20466   verifyFormat("SomeClass::Constructor()\n"
20467                "    : a(a) {}",
20468                Style);
20469 
20470   Style.ColumnLimit = 0;
20471   verifyFormat("SomeClass::Constructor()\n"
20472                "    : a(a) {}",
20473                Style);
20474   verifyFormat("SomeClass::Constructor() noexcept\n"
20475                "    : a(a) {}",
20476                Style);
20477   verifyFormat("SomeClass::Constructor()\n"
20478                "    : a(a)\n"
20479                "    , b(b)\n"
20480                "    , c(c) {}",
20481                Style);
20482   verifyFormat("SomeClass::Constructor()\n"
20483                "    : a(a) {\n"
20484                "  foo();\n"
20485                "  bar();\n"
20486                "}",
20487                Style);
20488 
20489   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
20490   verifyFormat("SomeClass::Constructor()\n"
20491                "    : a(a)\n"
20492                "    , b(b)\n"
20493                "    , c(c) {\n}",
20494                Style);
20495   verifyFormat("SomeClass::Constructor()\n"
20496                "    : a(a) {\n}",
20497                Style);
20498 
20499   Style.ColumnLimit = 80;
20500   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
20501   Style.ConstructorInitializerIndentWidth = 2;
20502   verifyFormat("SomeClass::Constructor()\n"
20503                "  : a(a)\n"
20504                "  , b(b)\n"
20505                "  , c(c) {}",
20506                Style);
20507 
20508   Style.ConstructorInitializerIndentWidth = 0;
20509   verifyFormat("SomeClass::Constructor()\n"
20510                ": a(a)\n"
20511                ", b(b)\n"
20512                ", c(c) {}",
20513                Style);
20514 
20515   Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
20516   Style.ConstructorInitializerIndentWidth = 4;
20517   verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
20518   verifyFormat(
20519       "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n",
20520       Style);
20521   verifyFormat(
20522       "SomeClass::Constructor()\n"
20523       "    : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
20524       Style);
20525   Style.ConstructorInitializerIndentWidth = 4;
20526   Style.ColumnLimit = 60;
20527   verifyFormat("SomeClass::Constructor()\n"
20528                "    : aaaaaaaa(aaaaaaaa)\n"
20529                "    , aaaaaaaa(aaaaaaaa)\n"
20530                "    , aaaaaaaa(aaaaaaaa) {}",
20531                Style);
20532 }
20533 
20534 TEST_F(FormatTest, ConstructorInitializersWithPreprocessorDirective) {
20535   FormatStyle Style = getLLVMStyle();
20536   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
20537   Style.ConstructorInitializerIndentWidth = 4;
20538   verifyFormat("SomeClass::Constructor()\n"
20539                "    : a{a}\n"
20540                "    , b{b} {}",
20541                Style);
20542   verifyFormat("SomeClass::Constructor()\n"
20543                "    : a{a}\n"
20544                "#if CONDITION\n"
20545                "    , b{b}\n"
20546                "#endif\n"
20547                "{\n}",
20548                Style);
20549   Style.ConstructorInitializerIndentWidth = 2;
20550   verifyFormat("SomeClass::Constructor()\n"
20551                "#if CONDITION\n"
20552                "  : a{a}\n"
20553                "#endif\n"
20554                "  , b{b}\n"
20555                "  , c{c} {\n}",
20556                Style);
20557   Style.ConstructorInitializerIndentWidth = 0;
20558   verifyFormat("SomeClass::Constructor()\n"
20559                ": a{a}\n"
20560                "#ifdef CONDITION\n"
20561                ", b{b}\n"
20562                "#else\n"
20563                ", c{c}\n"
20564                "#endif\n"
20565                ", d{d} {\n}",
20566                Style);
20567   Style.ConstructorInitializerIndentWidth = 4;
20568   verifyFormat("SomeClass::Constructor()\n"
20569                "    : a{a}\n"
20570                "#if WINDOWS\n"
20571                "#if DEBUG\n"
20572                "    , b{0}\n"
20573                "#else\n"
20574                "    , b{1}\n"
20575                "#endif\n"
20576                "#else\n"
20577                "#if DEBUG\n"
20578                "    , b{2}\n"
20579                "#else\n"
20580                "    , b{3}\n"
20581                "#endif\n"
20582                "#endif\n"
20583                "{\n}",
20584                Style);
20585   verifyFormat("SomeClass::Constructor()\n"
20586                "    : a{a}\n"
20587                "#if WINDOWS\n"
20588                "    , b{0}\n"
20589                "#if DEBUG\n"
20590                "    , c{0}\n"
20591                "#else\n"
20592                "    , c{1}\n"
20593                "#endif\n"
20594                "#else\n"
20595                "#if DEBUG\n"
20596                "    , c{2}\n"
20597                "#else\n"
20598                "    , c{3}\n"
20599                "#endif\n"
20600                "    , b{1}\n"
20601                "#endif\n"
20602                "{\n}",
20603                Style);
20604 }
20605 
20606 TEST_F(FormatTest, Destructors) {
20607   verifyFormat("void F(int &i) { i.~int(); }");
20608   verifyFormat("void F(int &i) { i->~int(); }");
20609 }
20610 
20611 TEST_F(FormatTest, FormatsWithWebKitStyle) {
20612   FormatStyle Style = getWebKitStyle();
20613 
20614   // Don't indent in outer namespaces.
20615   verifyFormat("namespace outer {\n"
20616                "int i;\n"
20617                "namespace inner {\n"
20618                "    int i;\n"
20619                "} // namespace inner\n"
20620                "} // namespace outer\n"
20621                "namespace other_outer {\n"
20622                "int i;\n"
20623                "}",
20624                Style);
20625 
20626   // Don't indent case labels.
20627   verifyFormat("switch (variable) {\n"
20628                "case 1:\n"
20629                "case 2:\n"
20630                "    doSomething();\n"
20631                "    break;\n"
20632                "default:\n"
20633                "    ++variable;\n"
20634                "}",
20635                Style);
20636 
20637   // Wrap before binary operators.
20638   EXPECT_EQ("void f()\n"
20639             "{\n"
20640             "    if (aaaaaaaaaaaaaaaa\n"
20641             "        && bbbbbbbbbbbbbbbbbbbbbbbb\n"
20642             "        && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
20643             "        return;\n"
20644             "}",
20645             format("void f() {\n"
20646                    "if (aaaaaaaaaaaaaaaa\n"
20647                    "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
20648                    "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
20649                    "return;\n"
20650                    "}",
20651                    Style));
20652 
20653   // Allow functions on a single line.
20654   verifyFormat("void f() { return; }", Style);
20655 
20656   // Allow empty blocks on a single line and insert a space in empty blocks.
20657   EXPECT_EQ("void f() { }", format("void f() {}", Style));
20658   EXPECT_EQ("while (true) { }", format("while (true) {}", Style));
20659   // However, don't merge non-empty short loops.
20660   EXPECT_EQ("while (true) {\n"
20661             "    continue;\n"
20662             "}",
20663             format("while (true) { continue; }", Style));
20664 
20665   // Constructor initializers are formatted one per line with the "," on the
20666   // new line.
20667   verifyFormat("Constructor()\n"
20668                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
20669                "    , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
20670                "          aaaaaaaaaaaaaa)\n"
20671                "    , aaaaaaaaaaaaaaaaaaaaaaa()\n"
20672                "{\n"
20673                "}",
20674                Style);
20675   verifyFormat("SomeClass::Constructor()\n"
20676                "    : a(a)\n"
20677                "{\n"
20678                "}",
20679                Style);
20680   EXPECT_EQ("SomeClass::Constructor()\n"
20681             "    : a(a)\n"
20682             "{\n"
20683             "}",
20684             format("SomeClass::Constructor():a(a){}", Style));
20685   verifyFormat("SomeClass::Constructor()\n"
20686                "    : a(a)\n"
20687                "    , b(b)\n"
20688                "    , c(c)\n"
20689                "{\n"
20690                "}",
20691                Style);
20692   verifyFormat("SomeClass::Constructor()\n"
20693                "    : a(a)\n"
20694                "{\n"
20695                "    foo();\n"
20696                "    bar();\n"
20697                "}",
20698                Style);
20699 
20700   // Access specifiers should be aligned left.
20701   verifyFormat("class C {\n"
20702                "public:\n"
20703                "    int i;\n"
20704                "};",
20705                Style);
20706 
20707   // Do not align comments.
20708   verifyFormat("int a; // Do not\n"
20709                "double b; // align comments.",
20710                Style);
20711 
20712   // Do not align operands.
20713   EXPECT_EQ("ASSERT(aaaa\n"
20714             "    || bbbb);",
20715             format("ASSERT ( aaaa\n||bbbb);", Style));
20716 
20717   // Accept input's line breaks.
20718   EXPECT_EQ("if (aaaaaaaaaaaaaaa\n"
20719             "    || bbbbbbbbbbbbbbb) {\n"
20720             "    i++;\n"
20721             "}",
20722             format("if (aaaaaaaaaaaaaaa\n"
20723                    "|| bbbbbbbbbbbbbbb) { i++; }",
20724                    Style));
20725   EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
20726             "    i++;\n"
20727             "}",
20728             format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
20729 
20730   // Don't automatically break all macro definitions (llvm.org/PR17842).
20731   verifyFormat("#define aNumber 10", Style);
20732   // However, generally keep the line breaks that the user authored.
20733   EXPECT_EQ("#define aNumber \\\n"
20734             "    10",
20735             format("#define aNumber \\\n"
20736                    " 10",
20737                    Style));
20738 
20739   // Keep empty and one-element array literals on a single line.
20740   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
20741             "                                  copyItems:YES];",
20742             format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
20743                    "copyItems:YES];",
20744                    Style));
20745   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
20746             "                                  copyItems:YES];",
20747             format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
20748                    "             copyItems:YES];",
20749                    Style));
20750   // FIXME: This does not seem right, there should be more indentation before
20751   // the array literal's entries. Nested blocks have the same problem.
20752   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
20753             "    @\"a\",\n"
20754             "    @\"a\"\n"
20755             "]\n"
20756             "                                  copyItems:YES];",
20757             format("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
20758                    "     @\"a\",\n"
20759                    "     @\"a\"\n"
20760                    "     ]\n"
20761                    "       copyItems:YES];",
20762                    Style));
20763   EXPECT_EQ(
20764       "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
20765       "                                  copyItems:YES];",
20766       format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
20767              "   copyItems:YES];",
20768              Style));
20769 
20770   verifyFormat("[self.a b:c c:d];", Style);
20771   EXPECT_EQ("[self.a b:c\n"
20772             "        c:d];",
20773             format("[self.a b:c\n"
20774                    "c:d];",
20775                    Style));
20776 }
20777 
20778 TEST_F(FormatTest, FormatsLambdas) {
20779   verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n");
20780   verifyFormat(
20781       "int c = [b]() mutable noexcept { return [&b] { return b++; }(); }();\n");
20782   verifyFormat("int c = [&] { [=] { return b++; }(); }();\n");
20783   verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n");
20784   verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n");
20785   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n");
20786   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n");
20787   verifyFormat("auto c = [a = [b = 42] {}] {};\n");
20788   verifyFormat("auto c = [a = &i + 10, b = [] {}] {};\n");
20789   verifyFormat("int x = f(*+[] {});");
20790   verifyFormat("void f() {\n"
20791                "  other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
20792                "}\n");
20793   verifyFormat("void f() {\n"
20794                "  other(x.begin(), //\n"
20795                "        x.end(),   //\n"
20796                "        [&](int, int) { return 1; });\n"
20797                "}\n");
20798   verifyFormat("void f() {\n"
20799                "  other.other.other.other.other(\n"
20800                "      x.begin(), x.end(),\n"
20801                "      [something, rather](int, int, int, int, int, int, int) { "
20802                "return 1; });\n"
20803                "}\n");
20804   verifyFormat(
20805       "void f() {\n"
20806       "  other.other.other.other.other(\n"
20807       "      x.begin(), x.end(),\n"
20808       "      [something, rather](int, int, int, int, int, int, int) {\n"
20809       "        //\n"
20810       "      });\n"
20811       "}\n");
20812   verifyFormat("SomeFunction([]() { // A cool function...\n"
20813                "  return 43;\n"
20814                "});");
20815   EXPECT_EQ("SomeFunction([]() {\n"
20816             "#define A a\n"
20817             "  return 43;\n"
20818             "});",
20819             format("SomeFunction([](){\n"
20820                    "#define A a\n"
20821                    "return 43;\n"
20822                    "});"));
20823   verifyFormat("void f() {\n"
20824                "  SomeFunction([](decltype(x), A *a) {});\n"
20825                "  SomeFunction([](typeof(x), A *a) {});\n"
20826                "  SomeFunction([](_Atomic(x), A *a) {});\n"
20827                "  SomeFunction([](__underlying_type(x), A *a) {});\n"
20828                "}");
20829   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
20830                "    [](const aaaaaaaaaa &a) { return a; });");
20831   verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
20832                "  SomeOtherFunctioooooooooooooooooooooooooon();\n"
20833                "});");
20834   verifyFormat("Constructor()\n"
20835                "    : Field([] { // comment\n"
20836                "        int i;\n"
20837                "      }) {}");
20838   verifyFormat("auto my_lambda = [](const string &some_parameter) {\n"
20839                "  return some_parameter.size();\n"
20840                "};");
20841   verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n"
20842                "    [](const string &s) { return s; };");
20843   verifyFormat("int i = aaaaaa ? 1 //\n"
20844                "               : [] {\n"
20845                "                   return 2; //\n"
20846                "                 }();");
20847   verifyFormat("llvm::errs() << \"number of twos is \"\n"
20848                "             << std::count_if(v.begin(), v.end(), [](int x) {\n"
20849                "                  return x == 2; // force break\n"
20850                "                });");
20851   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
20852                "    [=](int iiiiiiiiiiii) {\n"
20853                "      return aaaaaaaaaaaaaaaaaaaaaaa !=\n"
20854                "             aaaaaaaaaaaaaaaaaaaaaaa;\n"
20855                "    });",
20856                getLLVMStyleWithColumns(60));
20857 
20858   verifyFormat("SomeFunction({[&] {\n"
20859                "                // comment\n"
20860                "              },\n"
20861                "              [&] {\n"
20862                "                // comment\n"
20863                "              }});");
20864   verifyFormat("SomeFunction({[&] {\n"
20865                "  // comment\n"
20866                "}});");
20867   verifyFormat(
20868       "virtual aaaaaaaaaaaaaaaa(\n"
20869       "    std::function<bool()> bbbbbbbbbbbb = [&]() { return true; },\n"
20870       "    aaaaa aaaaaaaaa);");
20871 
20872   // Lambdas with return types.
20873   verifyFormat("int c = []() -> int { return 2; }();\n");
20874   verifyFormat("int c = []() -> int * { return 2; }();\n");
20875   verifyFormat("int c = []() -> vector<int> { return {2}; }();\n");
20876   verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
20877   verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
20878   verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
20879   verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
20880   verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
20881   verifyFormat("[a, a]() -> a<1> {};");
20882   verifyFormat("[]() -> foo<5 + 2> { return {}; };");
20883   verifyFormat("[]() -> foo<5 - 2> { return {}; };");
20884   verifyFormat("[]() -> foo<5 / 2> { return {}; };");
20885   verifyFormat("[]() -> foo<5 * 2> { return {}; };");
20886   verifyFormat("[]() -> foo<5 % 2> { return {}; };");
20887   verifyFormat("[]() -> foo<5 << 2> { return {}; };");
20888   verifyFormat("[]() -> foo<!5> { return {}; };");
20889   verifyFormat("[]() -> foo<~5> { return {}; };");
20890   verifyFormat("[]() -> foo<5 | 2> { return {}; };");
20891   verifyFormat("[]() -> foo<5 || 2> { return {}; };");
20892   verifyFormat("[]() -> foo<5 & 2> { return {}; };");
20893   verifyFormat("[]() -> foo<5 && 2> { return {}; };");
20894   verifyFormat("[]() -> foo<5 == 2> { return {}; };");
20895   verifyFormat("[]() -> foo<5 != 2> { return {}; };");
20896   verifyFormat("[]() -> foo<5 >= 2> { return {}; };");
20897   verifyFormat("[]() -> foo<5 <= 2> { return {}; };");
20898   verifyFormat("[]() -> foo<5 < 2> { return {}; };");
20899   verifyFormat("[]() -> foo<2 ? 1 : 0> { return {}; };");
20900   verifyFormat("namespace bar {\n"
20901                "// broken:\n"
20902                "auto foo{[]() -> foo<5 + 2> { return {}; }};\n"
20903                "} // namespace bar");
20904   verifyFormat("namespace bar {\n"
20905                "// broken:\n"
20906                "auto foo{[]() -> foo<5 - 2> { return {}; }};\n"
20907                "} // namespace bar");
20908   verifyFormat("namespace bar {\n"
20909                "// broken:\n"
20910                "auto foo{[]() -> foo<5 / 2> { return {}; }};\n"
20911                "} // namespace bar");
20912   verifyFormat("namespace bar {\n"
20913                "// broken:\n"
20914                "auto foo{[]() -> foo<5 * 2> { return {}; }};\n"
20915                "} // namespace bar");
20916   verifyFormat("namespace bar {\n"
20917                "// broken:\n"
20918                "auto foo{[]() -> foo<5 % 2> { return {}; }};\n"
20919                "} // namespace bar");
20920   verifyFormat("namespace bar {\n"
20921                "// broken:\n"
20922                "auto foo{[]() -> foo<5 << 2> { return {}; }};\n"
20923                "} // namespace bar");
20924   verifyFormat("namespace bar {\n"
20925                "// broken:\n"
20926                "auto foo{[]() -> foo<!5> { return {}; }};\n"
20927                "} // namespace bar");
20928   verifyFormat("namespace bar {\n"
20929                "// broken:\n"
20930                "auto foo{[]() -> foo<~5> { return {}; }};\n"
20931                "} // namespace bar");
20932   verifyFormat("namespace bar {\n"
20933                "// broken:\n"
20934                "auto foo{[]() -> foo<5 | 2> { return {}; }};\n"
20935                "} // namespace bar");
20936   verifyFormat("namespace bar {\n"
20937                "// broken:\n"
20938                "auto foo{[]() -> foo<5 || 2> { return {}; }};\n"
20939                "} // namespace bar");
20940   verifyFormat("namespace bar {\n"
20941                "// broken:\n"
20942                "auto foo{[]() -> foo<5 & 2> { return {}; }};\n"
20943                "} // namespace bar");
20944   verifyFormat("namespace bar {\n"
20945                "// broken:\n"
20946                "auto foo{[]() -> foo<5 && 2> { return {}; }};\n"
20947                "} // namespace bar");
20948   verifyFormat("namespace bar {\n"
20949                "// broken:\n"
20950                "auto foo{[]() -> foo<5 == 2> { return {}; }};\n"
20951                "} // namespace bar");
20952   verifyFormat("namespace bar {\n"
20953                "// broken:\n"
20954                "auto foo{[]() -> foo<5 != 2> { return {}; }};\n"
20955                "} // namespace bar");
20956   verifyFormat("namespace bar {\n"
20957                "// broken:\n"
20958                "auto foo{[]() -> foo<5 >= 2> { return {}; }};\n"
20959                "} // namespace bar");
20960   verifyFormat("namespace bar {\n"
20961                "// broken:\n"
20962                "auto foo{[]() -> foo<5 <= 2> { return {}; }};\n"
20963                "} // namespace bar");
20964   verifyFormat("namespace bar {\n"
20965                "// broken:\n"
20966                "auto foo{[]() -> foo<5 < 2> { return {}; }};\n"
20967                "} // namespace bar");
20968   verifyFormat("namespace bar {\n"
20969                "// broken:\n"
20970                "auto foo{[]() -> foo<2 ? 1 : 0> { return {}; }};\n"
20971                "} // namespace bar");
20972   verifyFormat("[]() -> a<1> {};");
20973   verifyFormat("[]() -> a<1> { ; };");
20974   verifyFormat("[]() -> a<1> { ; }();");
20975   verifyFormat("[a, a]() -> a<true> {};");
20976   verifyFormat("[]() -> a<true> {};");
20977   verifyFormat("[]() -> a<true> { ; };");
20978   verifyFormat("[]() -> a<true> { ; }();");
20979   verifyFormat("[a, a]() -> a<false> {};");
20980   verifyFormat("[]() -> a<false> {};");
20981   verifyFormat("[]() -> a<false> { ; };");
20982   verifyFormat("[]() -> a<false> { ; }();");
20983   verifyFormat("auto foo{[]() -> foo<false> { ; }};");
20984   verifyFormat("namespace bar {\n"
20985                "auto foo{[]() -> foo<false> { ; }};\n"
20986                "} // namespace bar");
20987   verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
20988                "                   int j) -> int {\n"
20989                "  return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
20990                "};");
20991   verifyFormat(
20992       "aaaaaaaaaaaaaaaaaaaaaa(\n"
20993       "    [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n"
20994       "      return aaaaaaaaaaaaaaaaa;\n"
20995       "    });",
20996       getLLVMStyleWithColumns(70));
20997   verifyFormat("[]() //\n"
20998                "    -> int {\n"
20999                "  return 1; //\n"
21000                "};");
21001   verifyFormat("[]() -> Void<T...> {};");
21002   verifyFormat("[a, b]() -> Tuple<T...> { return {}; };");
21003   verifyFormat("SomeFunction({[]() -> int[] { return {}; }});");
21004   verifyFormat("SomeFunction({[]() -> int *[] { return {}; }});");
21005   verifyFormat("SomeFunction({[]() -> int (*)[] { return {}; }});");
21006   verifyFormat("SomeFunction({[]() -> ns::type<int (*)[]> { return {}; }});");
21007   verifyFormat("return int{[x = x]() { return x; }()};");
21008 
21009   // Lambdas with explicit template argument lists.
21010   verifyFormat(
21011       "auto L = []<template <typename> class T, class U>(T<U> &&a) {};\n");
21012   verifyFormat("auto L = []<class T>(T) {\n"
21013                "  {\n"
21014                "    f();\n"
21015                "    g();\n"
21016                "  }\n"
21017                "};\n");
21018   verifyFormat("auto L = []<class... T>(T...) {\n"
21019                "  {\n"
21020                "    f();\n"
21021                "    g();\n"
21022                "  }\n"
21023                "};\n");
21024   verifyFormat("auto L = []<typename... T>(T...) {\n"
21025                "  {\n"
21026                "    f();\n"
21027                "    g();\n"
21028                "  }\n"
21029                "};\n");
21030   verifyFormat("auto L = []<template <typename...> class T>(T...) {\n"
21031                "  {\n"
21032                "    f();\n"
21033                "    g();\n"
21034                "  }\n"
21035                "};\n");
21036   verifyFormat("auto L = []</*comment*/ class... T>(T...) {\n"
21037                "  {\n"
21038                "    f();\n"
21039                "    g();\n"
21040                "  }\n"
21041                "};\n");
21042 
21043   // Multiple lambdas in the same parentheses change indentation rules. These
21044   // lambdas are forced to start on new lines.
21045   verifyFormat("SomeFunction(\n"
21046                "    []() {\n"
21047                "      //\n"
21048                "    },\n"
21049                "    []() {\n"
21050                "      //\n"
21051                "    });");
21052 
21053   // A lambda passed as arg0 is always pushed to the next line.
21054   verifyFormat("SomeFunction(\n"
21055                "    [this] {\n"
21056                "      //\n"
21057                "    },\n"
21058                "    1);\n");
21059 
21060   // A multi-line lambda passed as arg1 forces arg0 to be pushed out, just like
21061   // the arg0 case above.
21062   auto Style = getGoogleStyle();
21063   Style.BinPackArguments = false;
21064   verifyFormat("SomeFunction(\n"
21065                "    a,\n"
21066                "    [this] {\n"
21067                "      //\n"
21068                "    },\n"
21069                "    b);\n",
21070                Style);
21071   verifyFormat("SomeFunction(\n"
21072                "    a,\n"
21073                "    [this] {\n"
21074                "      //\n"
21075                "    },\n"
21076                "    b);\n");
21077 
21078   // A lambda with a very long line forces arg0 to be pushed out irrespective of
21079   // the BinPackArguments value (as long as the code is wide enough).
21080   verifyFormat(
21081       "something->SomeFunction(\n"
21082       "    a,\n"
21083       "    [this] {\n"
21084       "      "
21085       "D0000000000000000000000000000000000000000000000000000000000001();\n"
21086       "    },\n"
21087       "    b);\n");
21088 
21089   // A multi-line lambda is pulled up as long as the introducer fits on the
21090   // previous line and there are no further args.
21091   verifyFormat("function(1, [this, that] {\n"
21092                "  //\n"
21093                "});\n");
21094   verifyFormat("function([this, that] {\n"
21095                "  //\n"
21096                "});\n");
21097   // FIXME: this format is not ideal and we should consider forcing the first
21098   // arg onto its own line.
21099   verifyFormat("function(a, b, c, //\n"
21100                "         d, [this, that] {\n"
21101                "           //\n"
21102                "         });\n");
21103 
21104   // Multiple lambdas are treated correctly even when there is a short arg0.
21105   verifyFormat("SomeFunction(\n"
21106                "    1,\n"
21107                "    [this] {\n"
21108                "      //\n"
21109                "    },\n"
21110                "    [this] {\n"
21111                "      //\n"
21112                "    },\n"
21113                "    1);\n");
21114 
21115   // More complex introducers.
21116   verifyFormat("return [i, args...] {};");
21117 
21118   // Not lambdas.
21119   verifyFormat("constexpr char hello[]{\"hello\"};");
21120   verifyFormat("double &operator[](int i) { return 0; }\n"
21121                "int i;");
21122   verifyFormat("std::unique_ptr<int[]> foo() {}");
21123   verifyFormat("int i = a[a][a]->f();");
21124   verifyFormat("int i = (*b)[a]->f();");
21125 
21126   // Other corner cases.
21127   verifyFormat("void f() {\n"
21128                "  bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
21129                "  );\n"
21130                "}");
21131 
21132   // Lambdas created through weird macros.
21133   verifyFormat("void f() {\n"
21134                "  MACRO((const AA &a) { return 1; });\n"
21135                "  MACRO((AA &a) { return 1; });\n"
21136                "}");
21137 
21138   verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
21139                "      doo_dah();\n"
21140                "      doo_dah();\n"
21141                "    })) {\n"
21142                "}");
21143   verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n"
21144                "                doo_dah();\n"
21145                "                doo_dah();\n"
21146                "              })) {\n"
21147                "}");
21148   verifyFormat("if CONSTEXPR (blah_blah(whatever, whatever, [] {\n"
21149                "                doo_dah();\n"
21150                "                doo_dah();\n"
21151                "              })) {\n"
21152                "}");
21153   verifyFormat("auto lambda = []() {\n"
21154                "  int a = 2\n"
21155                "#if A\n"
21156                "          + 2\n"
21157                "#endif\n"
21158                "      ;\n"
21159                "};");
21160 
21161   // Lambdas with complex multiline introducers.
21162   verifyFormat(
21163       "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
21164       "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n"
21165       "        -> ::std::unordered_set<\n"
21166       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
21167       "      //\n"
21168       "    });");
21169 
21170   FormatStyle DoNotMerge = getLLVMStyle();
21171   DoNotMerge.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
21172   verifyFormat("auto c = []() {\n"
21173                "  return b;\n"
21174                "};",
21175                "auto c = []() { return b; };", DoNotMerge);
21176   verifyFormat("auto c = []() {\n"
21177                "};",
21178                " auto c = []() {};", DoNotMerge);
21179 
21180   FormatStyle MergeEmptyOnly = getLLVMStyle();
21181   MergeEmptyOnly.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Empty;
21182   verifyFormat("auto c = []() {\n"
21183                "  return b;\n"
21184                "};",
21185                "auto c = []() {\n"
21186                "  return b;\n"
21187                " };",
21188                MergeEmptyOnly);
21189   verifyFormat("auto c = []() {};",
21190                "auto c = []() {\n"
21191                "};",
21192                MergeEmptyOnly);
21193 
21194   FormatStyle MergeInline = getLLVMStyle();
21195   MergeInline.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Inline;
21196   verifyFormat("auto c = []() {\n"
21197                "  return b;\n"
21198                "};",
21199                "auto c = []() { return b; };", MergeInline);
21200   verifyFormat("function([]() { return b; })", "function([]() { return b; })",
21201                MergeInline);
21202   verifyFormat("function([]() { return b; }, a)",
21203                "function([]() { return b; }, a)", MergeInline);
21204   verifyFormat("function(a, []() { return b; })",
21205                "function(a, []() { return b; })", MergeInline);
21206 
21207   // Check option "BraceWrapping.BeforeLambdaBody" and different state of
21208   // AllowShortLambdasOnASingleLine
21209   FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle();
21210   LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
21211   LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
21212   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
21213       FormatStyle::ShortLambdaStyle::SLS_None;
21214   verifyFormat("FctWithOneNestedLambdaInline_SLS_None(\n"
21215                "    []()\n"
21216                "    {\n"
21217                "      return 17;\n"
21218                "    });",
21219                LLVMWithBeforeLambdaBody);
21220   verifyFormat("FctWithOneNestedLambdaEmpty_SLS_None(\n"
21221                "    []()\n"
21222                "    {\n"
21223                "    });",
21224                LLVMWithBeforeLambdaBody);
21225   verifyFormat("auto fct_SLS_None = []()\n"
21226                "{\n"
21227                "  return 17;\n"
21228                "};",
21229                LLVMWithBeforeLambdaBody);
21230   verifyFormat("TwoNestedLambdas_SLS_None(\n"
21231                "    []()\n"
21232                "    {\n"
21233                "      return Call(\n"
21234                "          []()\n"
21235                "          {\n"
21236                "            return 17;\n"
21237                "          });\n"
21238                "    });",
21239                LLVMWithBeforeLambdaBody);
21240   verifyFormat("void Fct() {\n"
21241                "  return {[]()\n"
21242                "          {\n"
21243                "            return 17;\n"
21244                "          }};\n"
21245                "}",
21246                LLVMWithBeforeLambdaBody);
21247 
21248   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
21249       FormatStyle::ShortLambdaStyle::SLS_Empty;
21250   verifyFormat("FctWithOneNestedLambdaInline_SLS_Empty(\n"
21251                "    []()\n"
21252                "    {\n"
21253                "      return 17;\n"
21254                "    });",
21255                LLVMWithBeforeLambdaBody);
21256   verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Empty([]() {});",
21257                LLVMWithBeforeLambdaBody);
21258   verifyFormat("FctWithOneNestedLambdaEmptyInsideAVeryVeryVeryVeryVeryVeryVeryL"
21259                "ongFunctionName_SLS_Empty(\n"
21260                "    []() {});",
21261                LLVMWithBeforeLambdaBody);
21262   verifyFormat("FctWithMultipleParams_SLS_Empty(A, B,\n"
21263                "                                []()\n"
21264                "                                {\n"
21265                "                                  return 17;\n"
21266                "                                });",
21267                LLVMWithBeforeLambdaBody);
21268   verifyFormat("auto fct_SLS_Empty = []()\n"
21269                "{\n"
21270                "  return 17;\n"
21271                "};",
21272                LLVMWithBeforeLambdaBody);
21273   verifyFormat("TwoNestedLambdas_SLS_Empty(\n"
21274                "    []()\n"
21275                "    {\n"
21276                "      return Call([]() {});\n"
21277                "    });",
21278                LLVMWithBeforeLambdaBody);
21279   verifyFormat("TwoNestedLambdas_SLS_Empty(A,\n"
21280                "                           []()\n"
21281                "                           {\n"
21282                "                             return Call([]() {});\n"
21283                "                           });",
21284                LLVMWithBeforeLambdaBody);
21285   verifyFormat(
21286       "FctWithLongLineInLambda_SLS_Empty(\n"
21287       "    []()\n"
21288       "    {\n"
21289       "      return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
21290       "                               AndShouldNotBeConsiderAsInline,\n"
21291       "                               LambdaBodyMustBeBreak);\n"
21292       "    });",
21293       LLVMWithBeforeLambdaBody);
21294 
21295   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
21296       FormatStyle::ShortLambdaStyle::SLS_Inline;
21297   verifyFormat("FctWithOneNestedLambdaInline_SLS_Inline([]() { return 17; });",
21298                LLVMWithBeforeLambdaBody);
21299   verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Inline([]() {});",
21300                LLVMWithBeforeLambdaBody);
21301   verifyFormat("auto fct_SLS_Inline = []()\n"
21302                "{\n"
21303                "  return 17;\n"
21304                "};",
21305                LLVMWithBeforeLambdaBody);
21306   verifyFormat("TwoNestedLambdas_SLS_Inline([]() { return Call([]() { return "
21307                "17; }); });",
21308                LLVMWithBeforeLambdaBody);
21309   verifyFormat(
21310       "FctWithLongLineInLambda_SLS_Inline(\n"
21311       "    []()\n"
21312       "    {\n"
21313       "      return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
21314       "                               AndShouldNotBeConsiderAsInline,\n"
21315       "                               LambdaBodyMustBeBreak);\n"
21316       "    });",
21317       LLVMWithBeforeLambdaBody);
21318   verifyFormat("FctWithMultipleParams_SLS_Inline("
21319                "VeryLongParameterThatShouldAskToBeOnMultiLine,\n"
21320                "                                 []() { return 17; });",
21321                LLVMWithBeforeLambdaBody);
21322   verifyFormat(
21323       "FctWithMultipleParams_SLS_Inline(FirstParam, []() { return 17; });",
21324       LLVMWithBeforeLambdaBody);
21325 
21326   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
21327       FormatStyle::ShortLambdaStyle::SLS_All;
21328   verifyFormat("FctWithOneNestedLambdaInline_SLS_All([]() { return 17; });",
21329                LLVMWithBeforeLambdaBody);
21330   verifyFormat("FctWithOneNestedLambdaEmpty_SLS_All([]() {});",
21331                LLVMWithBeforeLambdaBody);
21332   verifyFormat("auto fct_SLS_All = []() { return 17; };",
21333                LLVMWithBeforeLambdaBody);
21334   verifyFormat("FctWithOneParam_SLS_All(\n"
21335                "    []()\n"
21336                "    {\n"
21337                "      // A cool function...\n"
21338                "      return 43;\n"
21339                "    });",
21340                LLVMWithBeforeLambdaBody);
21341   verifyFormat("FctWithMultipleParams_SLS_All("
21342                "VeryLongParameterThatShouldAskToBeOnMultiLine,\n"
21343                "                              []() { return 17; });",
21344                LLVMWithBeforeLambdaBody);
21345   verifyFormat("FctWithMultipleParams_SLS_All(A, []() { return 17; });",
21346                LLVMWithBeforeLambdaBody);
21347   verifyFormat("FctWithMultipleParams_SLS_All(A, B, []() { return 17; });",
21348                LLVMWithBeforeLambdaBody);
21349   verifyFormat(
21350       "FctWithLongLineInLambda_SLS_All(\n"
21351       "    []()\n"
21352       "    {\n"
21353       "      return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
21354       "                               AndShouldNotBeConsiderAsInline,\n"
21355       "                               LambdaBodyMustBeBreak);\n"
21356       "    });",
21357       LLVMWithBeforeLambdaBody);
21358   verifyFormat(
21359       "auto fct_SLS_All = []()\n"
21360       "{\n"
21361       "  return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
21362       "                           AndShouldNotBeConsiderAsInline,\n"
21363       "                           LambdaBodyMustBeBreak);\n"
21364       "};",
21365       LLVMWithBeforeLambdaBody);
21366   LLVMWithBeforeLambdaBody.BinPackParameters = false;
21367   verifyFormat("FctAllOnSameLine_SLS_All([]() { return S; }, Fst, Second);",
21368                LLVMWithBeforeLambdaBody);
21369   verifyFormat(
21370       "FctWithLongLineInLambda_SLS_All([]() { return SomeValueNotSoLong; },\n"
21371       "                                FirstParam,\n"
21372       "                                SecondParam,\n"
21373       "                                ThirdParam,\n"
21374       "                                FourthParam);",
21375       LLVMWithBeforeLambdaBody);
21376   verifyFormat("FctWithLongLineInLambda_SLS_All(\n"
21377                "    []() { return "
21378                "SomeValueVeryVeryVeryVeryVeryVeryVeryVeryVeryLong; },\n"
21379                "    FirstParam,\n"
21380                "    SecondParam,\n"
21381                "    ThirdParam,\n"
21382                "    FourthParam);",
21383                LLVMWithBeforeLambdaBody);
21384   verifyFormat(
21385       "FctWithLongLineInLambda_SLS_All(FirstParam,\n"
21386       "                                SecondParam,\n"
21387       "                                ThirdParam,\n"
21388       "                                FourthParam,\n"
21389       "                                []() { return SomeValueNotSoLong; });",
21390       LLVMWithBeforeLambdaBody);
21391   verifyFormat("FctWithLongLineInLambda_SLS_All(\n"
21392                "    []()\n"
21393                "    {\n"
21394                "      return "
21395                "HereAVeryLongLineThatWillBeFormattedOnMultipleLineAndShouldNotB"
21396                "eConsiderAsInline;\n"
21397                "    });",
21398                LLVMWithBeforeLambdaBody);
21399   verifyFormat(
21400       "FctWithLongLineInLambda_SLS_All(\n"
21401       "    []()\n"
21402       "    {\n"
21403       "      return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
21404       "                               AndShouldNotBeConsiderAsInline,\n"
21405       "                               LambdaBodyMustBeBreak);\n"
21406       "    });",
21407       LLVMWithBeforeLambdaBody);
21408   verifyFormat("FctWithTwoParams_SLS_All(\n"
21409                "    []()\n"
21410                "    {\n"
21411                "      // A cool function...\n"
21412                "      return 43;\n"
21413                "    },\n"
21414                "    87);",
21415                LLVMWithBeforeLambdaBody);
21416   verifyFormat("FctWithTwoParams_SLS_All([]() { return 43; }, 87);",
21417                LLVMWithBeforeLambdaBody);
21418   verifyFormat("FctWithOneNestedLambdas_SLS_All([]() { return 17; });",
21419                LLVMWithBeforeLambdaBody);
21420   verifyFormat(
21421       "TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; }); });",
21422       LLVMWithBeforeLambdaBody);
21423   verifyFormat("TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; "
21424                "}); }, x);",
21425                LLVMWithBeforeLambdaBody);
21426   verifyFormat("TwoNestedLambdas_SLS_All(\n"
21427                "    []()\n"
21428                "    {\n"
21429                "      // A cool function...\n"
21430                "      return Call([]() { return 17; });\n"
21431                "    });",
21432                LLVMWithBeforeLambdaBody);
21433   verifyFormat("TwoNestedLambdas_SLS_All(\n"
21434                "    []()\n"
21435                "    {\n"
21436                "      return Call(\n"
21437                "          []()\n"
21438                "          {\n"
21439                "            // A cool function...\n"
21440                "            return 17;\n"
21441                "          });\n"
21442                "    });",
21443                LLVMWithBeforeLambdaBody);
21444 
21445   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
21446       FormatStyle::ShortLambdaStyle::SLS_None;
21447 
21448   verifyFormat("auto select = [this]() -> const Library::Object *\n"
21449                "{\n"
21450                "  return MyAssignment::SelectFromList(this);\n"
21451                "};\n",
21452                LLVMWithBeforeLambdaBody);
21453 
21454   verifyFormat("auto select = [this]() -> const Library::Object &\n"
21455                "{\n"
21456                "  return MyAssignment::SelectFromList(this);\n"
21457                "};\n",
21458                LLVMWithBeforeLambdaBody);
21459 
21460   verifyFormat("auto select = [this]() -> std::unique_ptr<Object>\n"
21461                "{\n"
21462                "  return MyAssignment::SelectFromList(this);\n"
21463                "};\n",
21464                LLVMWithBeforeLambdaBody);
21465 
21466   verifyFormat("namespace test {\n"
21467                "class Test {\n"
21468                "public:\n"
21469                "  Test() = default;\n"
21470                "};\n"
21471                "} // namespace test",
21472                LLVMWithBeforeLambdaBody);
21473 
21474   // Lambdas with different indentation styles.
21475   Style = getLLVMStyleWithColumns(100);
21476   EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n"
21477             "  return promise.then(\n"
21478             "      [this, &someVariable, someObject = "
21479             "std::mv(s)](std::vector<int> evaluated) mutable {\n"
21480             "        return someObject.startAsyncAction().then(\n"
21481             "            [this, &someVariable](AsyncActionResult result) "
21482             "mutable { result.processMore(); });\n"
21483             "      });\n"
21484             "}\n",
21485             format("SomeResult doSomething(SomeObject promise) {\n"
21486                    "  return promise.then([this, &someVariable, someObject = "
21487                    "std::mv(s)](std::vector<int> evaluated) mutable {\n"
21488                    "    return someObject.startAsyncAction().then([this, "
21489                    "&someVariable](AsyncActionResult result) mutable {\n"
21490                    "      result.processMore();\n"
21491                    "    });\n"
21492                    "  });\n"
21493                    "}\n",
21494                    Style));
21495   Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope;
21496   verifyFormat("test() {\n"
21497                "  ([]() -> {\n"
21498                "    int b = 32;\n"
21499                "    return 3;\n"
21500                "  }).foo();\n"
21501                "}",
21502                Style);
21503   verifyFormat("test() {\n"
21504                "  []() -> {\n"
21505                "    int b = 32;\n"
21506                "    return 3;\n"
21507                "  }\n"
21508                "}",
21509                Style);
21510   verifyFormat("std::sort(v.begin(), v.end(),\n"
21511                "          [](const auto &someLongArgumentName, const auto "
21512                "&someOtherLongArgumentName) {\n"
21513                "  return someLongArgumentName.someMemberVariable < "
21514                "someOtherLongArgumentName.someMemberVariable;\n"
21515                "});",
21516                Style);
21517   verifyFormat("test() {\n"
21518                "  (\n"
21519                "      []() -> {\n"
21520                "        int b = 32;\n"
21521                "        return 3;\n"
21522                "      },\n"
21523                "      foo, bar)\n"
21524                "      .foo();\n"
21525                "}",
21526                Style);
21527   verifyFormat("test() {\n"
21528                "  ([]() -> {\n"
21529                "    int b = 32;\n"
21530                "    return 3;\n"
21531                "  })\n"
21532                "      .foo()\n"
21533                "      .bar();\n"
21534                "}",
21535                Style);
21536   EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n"
21537             "  return promise.then(\n"
21538             "      [this, &someVariable, someObject = "
21539             "std::mv(s)](std::vector<int> evaluated) mutable {\n"
21540             "    return someObject.startAsyncAction().then(\n"
21541             "        [this, &someVariable](AsyncActionResult result) mutable { "
21542             "result.processMore(); });\n"
21543             "  });\n"
21544             "}\n",
21545             format("SomeResult doSomething(SomeObject promise) {\n"
21546                    "  return promise.then([this, &someVariable, someObject = "
21547                    "std::mv(s)](std::vector<int> evaluated) mutable {\n"
21548                    "    return someObject.startAsyncAction().then([this, "
21549                    "&someVariable](AsyncActionResult result) mutable {\n"
21550                    "      result.processMore();\n"
21551                    "    });\n"
21552                    "  });\n"
21553                    "}\n",
21554                    Style));
21555   EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n"
21556             "  return promise.then([this, &someVariable] {\n"
21557             "    return someObject.startAsyncAction().then(\n"
21558             "        [this, &someVariable](AsyncActionResult result) mutable { "
21559             "result.processMore(); });\n"
21560             "  });\n"
21561             "}\n",
21562             format("SomeResult doSomething(SomeObject promise) {\n"
21563                    "  return promise.then([this, &someVariable] {\n"
21564                    "    return someObject.startAsyncAction().then([this, "
21565                    "&someVariable](AsyncActionResult result) mutable {\n"
21566                    "      result.processMore();\n"
21567                    "    });\n"
21568                    "  });\n"
21569                    "}\n",
21570                    Style));
21571   Style = getGoogleStyle();
21572   Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope;
21573   EXPECT_EQ("#define A                                       \\\n"
21574             "  [] {                                          \\\n"
21575             "    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(        \\\n"
21576             "        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
21577             "      }",
21578             format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
21579                    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
21580                    Style));
21581   // TODO: The current formatting has a minor issue that's not worth fixing
21582   // right now whereby the closing brace is indented relative to the signature
21583   // instead of being aligned. This only happens with macros.
21584 }
21585 
21586 TEST_F(FormatTest, LambdaWithLineComments) {
21587   FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle();
21588   LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
21589   LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
21590   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
21591       FormatStyle::ShortLambdaStyle::SLS_All;
21592 
21593   verifyFormat("auto k = []() { return; }", LLVMWithBeforeLambdaBody);
21594   verifyFormat("auto k = []() // comment\n"
21595                "{ return; }",
21596                LLVMWithBeforeLambdaBody);
21597   verifyFormat("auto k = []() /* comment */ { return; }",
21598                LLVMWithBeforeLambdaBody);
21599   verifyFormat("auto k = []() /* comment */ /* comment */ { return; }",
21600                LLVMWithBeforeLambdaBody);
21601   verifyFormat("auto k = []() // X\n"
21602                "{ return; }",
21603                LLVMWithBeforeLambdaBody);
21604   verifyFormat(
21605       "auto k = []() // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"
21606       "{ return; }",
21607       LLVMWithBeforeLambdaBody);
21608 }
21609 
21610 TEST_F(FormatTest, EmptyLinesInLambdas) {
21611   verifyFormat("auto lambda = []() {\n"
21612                "  x(); //\n"
21613                "};",
21614                "auto lambda = []() {\n"
21615                "\n"
21616                "  x(); //\n"
21617                "\n"
21618                "};");
21619 }
21620 
21621 TEST_F(FormatTest, FormatsBlocks) {
21622   FormatStyle ShortBlocks = getLLVMStyle();
21623   ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
21624   verifyFormat("int (^Block)(int, int);", ShortBlocks);
21625   verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
21626   verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
21627   verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks);
21628   verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks);
21629   verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks);
21630 
21631   verifyFormat("foo(^{ bar(); });", ShortBlocks);
21632   verifyFormat("foo(a, ^{ bar(); });", ShortBlocks);
21633   verifyFormat("{ void (^block)(Object *x); }", ShortBlocks);
21634 
21635   verifyFormat("[operation setCompletionBlock:^{\n"
21636                "  [self onOperationDone];\n"
21637                "}];");
21638   verifyFormat("int i = {[operation setCompletionBlock:^{\n"
21639                "  [self onOperationDone];\n"
21640                "}]};");
21641   verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
21642                "  f();\n"
21643                "}];");
21644   verifyFormat("int a = [operation block:^int(int *i) {\n"
21645                "  return 1;\n"
21646                "}];");
21647   verifyFormat("[myObject doSomethingWith:arg1\n"
21648                "                      aaa:^int(int *a) {\n"
21649                "                        return 1;\n"
21650                "                      }\n"
21651                "                      bbb:f(a * bbbbbbbb)];");
21652 
21653   verifyFormat("[operation setCompletionBlock:^{\n"
21654                "  [self.delegate newDataAvailable];\n"
21655                "}];",
21656                getLLVMStyleWithColumns(60));
21657   verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
21658                "  NSString *path = [self sessionFilePath];\n"
21659                "  if (path) {\n"
21660                "    // ...\n"
21661                "  }\n"
21662                "});");
21663   verifyFormat("[[SessionService sharedService]\n"
21664                "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
21665                "      if (window) {\n"
21666                "        [self windowDidLoad:window];\n"
21667                "      } else {\n"
21668                "        [self errorLoadingWindow];\n"
21669                "      }\n"
21670                "    }];");
21671   verifyFormat("void (^largeBlock)(void) = ^{\n"
21672                "  // ...\n"
21673                "};\n",
21674                getLLVMStyleWithColumns(40));
21675   verifyFormat("[[SessionService sharedService]\n"
21676                "    loadWindowWithCompletionBlock: //\n"
21677                "        ^(SessionWindow *window) {\n"
21678                "          if (window) {\n"
21679                "            [self windowDidLoad:window];\n"
21680                "          } else {\n"
21681                "            [self errorLoadingWindow];\n"
21682                "          }\n"
21683                "        }];",
21684                getLLVMStyleWithColumns(60));
21685   verifyFormat("[myObject doSomethingWith:arg1\n"
21686                "    firstBlock:^(Foo *a) {\n"
21687                "      // ...\n"
21688                "      int i;\n"
21689                "    }\n"
21690                "    secondBlock:^(Bar *b) {\n"
21691                "      // ...\n"
21692                "      int i;\n"
21693                "    }\n"
21694                "    thirdBlock:^Foo(Bar *b) {\n"
21695                "      // ...\n"
21696                "      int i;\n"
21697                "    }];");
21698   verifyFormat("[myObject doSomethingWith:arg1\n"
21699                "               firstBlock:-1\n"
21700                "              secondBlock:^(Bar *b) {\n"
21701                "                // ...\n"
21702                "                int i;\n"
21703                "              }];");
21704 
21705   verifyFormat("f(^{\n"
21706                "  @autoreleasepool {\n"
21707                "    if (a) {\n"
21708                "      g();\n"
21709                "    }\n"
21710                "  }\n"
21711                "});");
21712   verifyFormat("Block b = ^int *(A *a, B *b) {}");
21713   verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n"
21714                "};");
21715 
21716   FormatStyle FourIndent = getLLVMStyle();
21717   FourIndent.ObjCBlockIndentWidth = 4;
21718   verifyFormat("[operation setCompletionBlock:^{\n"
21719                "    [self onOperationDone];\n"
21720                "}];",
21721                FourIndent);
21722 }
21723 
21724 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) {
21725   FormatStyle ZeroColumn = getLLVMStyleWithColumns(0);
21726 
21727   verifyFormat("[[SessionService sharedService] "
21728                "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
21729                "  if (window) {\n"
21730                "    [self windowDidLoad:window];\n"
21731                "  } else {\n"
21732                "    [self errorLoadingWindow];\n"
21733                "  }\n"
21734                "}];",
21735                ZeroColumn);
21736   EXPECT_EQ("[[SessionService sharedService]\n"
21737             "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
21738             "      if (window) {\n"
21739             "        [self windowDidLoad:window];\n"
21740             "      } else {\n"
21741             "        [self errorLoadingWindow];\n"
21742             "      }\n"
21743             "    }];",
21744             format("[[SessionService sharedService]\n"
21745                    "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
21746                    "                if (window) {\n"
21747                    "    [self windowDidLoad:window];\n"
21748                    "  } else {\n"
21749                    "    [self errorLoadingWindow];\n"
21750                    "  }\n"
21751                    "}];",
21752                    ZeroColumn));
21753   verifyFormat("[myObject doSomethingWith:arg1\n"
21754                "    firstBlock:^(Foo *a) {\n"
21755                "      // ...\n"
21756                "      int i;\n"
21757                "    }\n"
21758                "    secondBlock:^(Bar *b) {\n"
21759                "      // ...\n"
21760                "      int i;\n"
21761                "    }\n"
21762                "    thirdBlock:^Foo(Bar *b) {\n"
21763                "      // ...\n"
21764                "      int i;\n"
21765                "    }];",
21766                ZeroColumn);
21767   verifyFormat("f(^{\n"
21768                "  @autoreleasepool {\n"
21769                "    if (a) {\n"
21770                "      g();\n"
21771                "    }\n"
21772                "  }\n"
21773                "});",
21774                ZeroColumn);
21775   verifyFormat("void (^largeBlock)(void) = ^{\n"
21776                "  // ...\n"
21777                "};",
21778                ZeroColumn);
21779 
21780   ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
21781   EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };",
21782             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
21783   ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
21784   EXPECT_EQ("void (^largeBlock)(void) = ^{\n"
21785             "  int i;\n"
21786             "};",
21787             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
21788 }
21789 
21790 TEST_F(FormatTest, SupportsCRLF) {
21791   EXPECT_EQ("int a;\r\n"
21792             "int b;\r\n"
21793             "int c;\r\n",
21794             format("int a;\r\n"
21795                    "  int b;\r\n"
21796                    "    int c;\r\n",
21797                    getLLVMStyle()));
21798   EXPECT_EQ("int a;\r\n"
21799             "int b;\r\n"
21800             "int c;\r\n",
21801             format("int a;\r\n"
21802                    "  int b;\n"
21803                    "    int c;\r\n",
21804                    getLLVMStyle()));
21805   EXPECT_EQ("int a;\n"
21806             "int b;\n"
21807             "int c;\n",
21808             format("int a;\r\n"
21809                    "  int b;\n"
21810                    "    int c;\n",
21811                    getLLVMStyle()));
21812   EXPECT_EQ("\"aaaaaaa \"\r\n"
21813             "\"bbbbbbb\";\r\n",
21814             format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
21815   EXPECT_EQ("#define A \\\r\n"
21816             "  b;      \\\r\n"
21817             "  c;      \\\r\n"
21818             "  d;\r\n",
21819             format("#define A \\\r\n"
21820                    "  b; \\\r\n"
21821                    "  c; d; \r\n",
21822                    getGoogleStyle()));
21823 
21824   EXPECT_EQ("/*\r\n"
21825             "multi line block comments\r\n"
21826             "should not introduce\r\n"
21827             "an extra carriage return\r\n"
21828             "*/\r\n",
21829             format("/*\r\n"
21830                    "multi line block comments\r\n"
21831                    "should not introduce\r\n"
21832                    "an extra carriage return\r\n"
21833                    "*/\r\n"));
21834   EXPECT_EQ("/*\r\n"
21835             "\r\n"
21836             "*/",
21837             format("/*\r\n"
21838                    "    \r\r\r\n"
21839                    "*/"));
21840 
21841   FormatStyle style = getLLVMStyle();
21842 
21843   style.DeriveLineEnding = true;
21844   style.UseCRLF = false;
21845   EXPECT_EQ("union FooBarBazQux {\n"
21846             "  int foo;\n"
21847             "  int bar;\n"
21848             "  int baz;\n"
21849             "};",
21850             format("union FooBarBazQux {\r\n"
21851                    "  int foo;\n"
21852                    "  int bar;\r\n"
21853                    "  int baz;\n"
21854                    "};",
21855                    style));
21856   style.UseCRLF = true;
21857   EXPECT_EQ("union FooBarBazQux {\r\n"
21858             "  int foo;\r\n"
21859             "  int bar;\r\n"
21860             "  int baz;\r\n"
21861             "};",
21862             format("union FooBarBazQux {\r\n"
21863                    "  int foo;\n"
21864                    "  int bar;\r\n"
21865                    "  int baz;\n"
21866                    "};",
21867                    style));
21868 
21869   style.DeriveLineEnding = false;
21870   style.UseCRLF = false;
21871   EXPECT_EQ("union FooBarBazQux {\n"
21872             "  int foo;\n"
21873             "  int bar;\n"
21874             "  int baz;\n"
21875             "  int qux;\n"
21876             "};",
21877             format("union FooBarBazQux {\r\n"
21878                    "  int foo;\n"
21879                    "  int bar;\r\n"
21880                    "  int baz;\n"
21881                    "  int qux;\r\n"
21882                    "};",
21883                    style));
21884   style.UseCRLF = true;
21885   EXPECT_EQ("union FooBarBazQux {\r\n"
21886             "  int foo;\r\n"
21887             "  int bar;\r\n"
21888             "  int baz;\r\n"
21889             "  int qux;\r\n"
21890             "};",
21891             format("union FooBarBazQux {\r\n"
21892                    "  int foo;\n"
21893                    "  int bar;\r\n"
21894                    "  int baz;\n"
21895                    "  int qux;\n"
21896                    "};",
21897                    style));
21898 
21899   style.DeriveLineEnding = true;
21900   style.UseCRLF = false;
21901   EXPECT_EQ("union FooBarBazQux {\r\n"
21902             "  int foo;\r\n"
21903             "  int bar;\r\n"
21904             "  int baz;\r\n"
21905             "  int qux;\r\n"
21906             "};",
21907             format("union FooBarBazQux {\r\n"
21908                    "  int foo;\n"
21909                    "  int bar;\r\n"
21910                    "  int baz;\n"
21911                    "  int qux;\r\n"
21912                    "};",
21913                    style));
21914   style.UseCRLF = true;
21915   EXPECT_EQ("union FooBarBazQux {\n"
21916             "  int foo;\n"
21917             "  int bar;\n"
21918             "  int baz;\n"
21919             "  int qux;\n"
21920             "};",
21921             format("union FooBarBazQux {\r\n"
21922                    "  int foo;\n"
21923                    "  int bar;\r\n"
21924                    "  int baz;\n"
21925                    "  int qux;\n"
21926                    "};",
21927                    style));
21928 }
21929 
21930 TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
21931   verifyFormat("MY_CLASS(C) {\n"
21932                "  int i;\n"
21933                "  int j;\n"
21934                "};");
21935 }
21936 
21937 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
21938   FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
21939   TwoIndent.ContinuationIndentWidth = 2;
21940 
21941   EXPECT_EQ("int i =\n"
21942             "  longFunction(\n"
21943             "    arg);",
21944             format("int i = longFunction(arg);", TwoIndent));
21945 
21946   FormatStyle SixIndent = getLLVMStyleWithColumns(20);
21947   SixIndent.ContinuationIndentWidth = 6;
21948 
21949   EXPECT_EQ("int i =\n"
21950             "      longFunction(\n"
21951             "            arg);",
21952             format("int i = longFunction(arg);", SixIndent));
21953 }
21954 
21955 TEST_F(FormatTest, WrappedClosingParenthesisIndent) {
21956   FormatStyle Style = getLLVMStyle();
21957   verifyFormat("int Foo::getter(\n"
21958                "    //\n"
21959                ") const {\n"
21960                "  return foo;\n"
21961                "}",
21962                Style);
21963   verifyFormat("void Foo::setter(\n"
21964                "    //\n"
21965                ") {\n"
21966                "  foo = 1;\n"
21967                "}",
21968                Style);
21969 }
21970 
21971 TEST_F(FormatTest, SpacesInAngles) {
21972   FormatStyle Spaces = getLLVMStyle();
21973   Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
21974 
21975   verifyFormat("vector< ::std::string > x1;", Spaces);
21976   verifyFormat("Foo< int, Bar > x2;", Spaces);
21977   verifyFormat("Foo< ::int, ::Bar > x3;", Spaces);
21978 
21979   verifyFormat("static_cast< int >(arg);", Spaces);
21980   verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
21981   verifyFormat("f< int, float >();", Spaces);
21982   verifyFormat("template <> g() {}", Spaces);
21983   verifyFormat("template < std::vector< int > > f() {}", Spaces);
21984   verifyFormat("std::function< void(int, int) > fct;", Spaces);
21985   verifyFormat("void inFunction() { std::function< void(int, int) > fct; }",
21986                Spaces);
21987 
21988   Spaces.Standard = FormatStyle::LS_Cpp03;
21989   Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
21990   verifyFormat("A< A< int > >();", Spaces);
21991 
21992   Spaces.SpacesInAngles = FormatStyle::SIAS_Never;
21993   verifyFormat("A<A<int> >();", Spaces);
21994 
21995   Spaces.SpacesInAngles = FormatStyle::SIAS_Leave;
21996   verifyFormat("vector< ::std::string> x4;", "vector<::std::string> x4;",
21997                Spaces);
21998   verifyFormat("vector< ::std::string > x4;", "vector<::std::string > x4;",
21999                Spaces);
22000 
22001   verifyFormat("A<A<int> >();", Spaces);
22002   verifyFormat("A<A<int> >();", "A<A<int>>();", Spaces);
22003   verifyFormat("A< A< int > >();", Spaces);
22004 
22005   Spaces.Standard = FormatStyle::LS_Cpp11;
22006   Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
22007   verifyFormat("A< A< int > >();", Spaces);
22008 
22009   Spaces.SpacesInAngles = FormatStyle::SIAS_Never;
22010   verifyFormat("vector<::std::string> x4;", Spaces);
22011   verifyFormat("vector<int> x5;", Spaces);
22012   verifyFormat("Foo<int, Bar> x6;", Spaces);
22013   verifyFormat("Foo<::int, ::Bar> x7;", Spaces);
22014 
22015   verifyFormat("A<A<int>>();", Spaces);
22016 
22017   Spaces.SpacesInAngles = FormatStyle::SIAS_Leave;
22018   verifyFormat("vector<::std::string> x4;", Spaces);
22019   verifyFormat("vector< ::std::string > x4;", Spaces);
22020   verifyFormat("vector<int> x5;", Spaces);
22021   verifyFormat("vector< int > x5;", Spaces);
22022   verifyFormat("Foo<int, Bar> x6;", Spaces);
22023   verifyFormat("Foo< int, Bar > x6;", Spaces);
22024   verifyFormat("Foo<::int, ::Bar> x7;", Spaces);
22025   verifyFormat("Foo< ::int, ::Bar > x7;", Spaces);
22026 
22027   verifyFormat("A<A<int>>();", Spaces);
22028   verifyFormat("A< A< int > >();", Spaces);
22029   verifyFormat("A<A<int > >();", Spaces);
22030   verifyFormat("A< A< int>>();", Spaces);
22031 
22032   Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
22033   verifyFormat("// clang-format off\n"
22034                "foo<<<1, 1>>>();\n"
22035                "// clang-format on\n",
22036                Spaces);
22037   verifyFormat("// clang-format off\n"
22038                "foo< < <1, 1> > >();\n"
22039                "// clang-format on\n",
22040                Spaces);
22041 }
22042 
22043 TEST_F(FormatTest, SpaceAfterTemplateKeyword) {
22044   FormatStyle Style = getLLVMStyle();
22045   Style.SpaceAfterTemplateKeyword = false;
22046   verifyFormat("template<int> void foo();", Style);
22047 }
22048 
22049 TEST_F(FormatTest, TripleAngleBrackets) {
22050   verifyFormat("f<<<1, 1>>>();");
22051   verifyFormat("f<<<1, 1, 1, s>>>();");
22052   verifyFormat("f<<<a, b, c, d>>>();");
22053   EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();"));
22054   verifyFormat("f<param><<<1, 1>>>();");
22055   verifyFormat("f<1><<<1, 1>>>();");
22056   EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();"));
22057   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
22058                "aaaaaaaaaaa<<<\n    1, 1>>>();");
22059   verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n"
22060                "    <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();");
22061 }
22062 
22063 TEST_F(FormatTest, MergeLessLessAtEnd) {
22064   verifyFormat("<<");
22065   EXPECT_EQ("< < <", format("\\\n<<<"));
22066   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
22067                "aaallvm::outs() <<");
22068   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
22069                "aaaallvm::outs()\n    <<");
22070 }
22071 
22072 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
22073   std::string code = "#if A\n"
22074                      "#if B\n"
22075                      "a.\n"
22076                      "#endif\n"
22077                      "    a = 1;\n"
22078                      "#else\n"
22079                      "#endif\n"
22080                      "#if C\n"
22081                      "#else\n"
22082                      "#endif\n";
22083   EXPECT_EQ(code, format(code));
22084 }
22085 
22086 TEST_F(FormatTest, HandleConflictMarkers) {
22087   // Git/SVN conflict markers.
22088   EXPECT_EQ("int a;\n"
22089             "void f() {\n"
22090             "  callme(some(parameter1,\n"
22091             "<<<<<<< text by the vcs\n"
22092             "              parameter2),\n"
22093             "||||||| text by the vcs\n"
22094             "              parameter2),\n"
22095             "         parameter3,\n"
22096             "======= text by the vcs\n"
22097             "              parameter2, parameter3),\n"
22098             ">>>>>>> text by the vcs\n"
22099             "         otherparameter);\n",
22100             format("int a;\n"
22101                    "void f() {\n"
22102                    "  callme(some(parameter1,\n"
22103                    "<<<<<<< text by the vcs\n"
22104                    "  parameter2),\n"
22105                    "||||||| text by the vcs\n"
22106                    "  parameter2),\n"
22107                    "  parameter3,\n"
22108                    "======= text by the vcs\n"
22109                    "  parameter2,\n"
22110                    "  parameter3),\n"
22111                    ">>>>>>> text by the vcs\n"
22112                    "  otherparameter);\n"));
22113 
22114   // Perforce markers.
22115   EXPECT_EQ("void f() {\n"
22116             "  function(\n"
22117             ">>>> text by the vcs\n"
22118             "      parameter,\n"
22119             "==== text by the vcs\n"
22120             "      parameter,\n"
22121             "==== text by the vcs\n"
22122             "      parameter,\n"
22123             "<<<< text by the vcs\n"
22124             "      parameter);\n",
22125             format("void f() {\n"
22126                    "  function(\n"
22127                    ">>>> text by the vcs\n"
22128                    "  parameter,\n"
22129                    "==== text by the vcs\n"
22130                    "  parameter,\n"
22131                    "==== text by the vcs\n"
22132                    "  parameter,\n"
22133                    "<<<< text by the vcs\n"
22134                    "  parameter);\n"));
22135 
22136   EXPECT_EQ("<<<<<<<\n"
22137             "|||||||\n"
22138             "=======\n"
22139             ">>>>>>>",
22140             format("<<<<<<<\n"
22141                    "|||||||\n"
22142                    "=======\n"
22143                    ">>>>>>>"));
22144 
22145   EXPECT_EQ("<<<<<<<\n"
22146             "|||||||\n"
22147             "int i;\n"
22148             "=======\n"
22149             ">>>>>>>",
22150             format("<<<<<<<\n"
22151                    "|||||||\n"
22152                    "int i;\n"
22153                    "=======\n"
22154                    ">>>>>>>"));
22155 
22156   // FIXME: Handle parsing of macros around conflict markers correctly:
22157   EXPECT_EQ("#define Macro \\\n"
22158             "<<<<<<<\n"
22159             "Something \\\n"
22160             "|||||||\n"
22161             "Else \\\n"
22162             "=======\n"
22163             "Other \\\n"
22164             ">>>>>>>\n"
22165             "    End int i;\n",
22166             format("#define Macro \\\n"
22167                    "<<<<<<<\n"
22168                    "  Something \\\n"
22169                    "|||||||\n"
22170                    "  Else \\\n"
22171                    "=======\n"
22172                    "  Other \\\n"
22173                    ">>>>>>>\n"
22174                    "  End\n"
22175                    "int i;\n"));
22176 
22177   verifyFormat(R"(====
22178 #ifdef A
22179 a
22180 #else
22181 b
22182 #endif
22183 )");
22184 }
22185 
22186 TEST_F(FormatTest, DisableRegions) {
22187   EXPECT_EQ("int i;\n"
22188             "// clang-format off\n"
22189             "  int j;\n"
22190             "// clang-format on\n"
22191             "int k;",
22192             format(" int  i;\n"
22193                    "   // clang-format off\n"
22194                    "  int j;\n"
22195                    " // clang-format on\n"
22196                    "   int   k;"));
22197   EXPECT_EQ("int i;\n"
22198             "/* clang-format off */\n"
22199             "  int j;\n"
22200             "/* clang-format on */\n"
22201             "int k;",
22202             format(" int  i;\n"
22203                    "   /* clang-format off */\n"
22204                    "  int j;\n"
22205                    " /* clang-format on */\n"
22206                    "   int   k;"));
22207 
22208   // Don't reflow comments within disabled regions.
22209   EXPECT_EQ("// clang-format off\n"
22210             "// long long long long long long line\n"
22211             "/* clang-format on */\n"
22212             "/* long long long\n"
22213             " * long long long\n"
22214             " * line */\n"
22215             "int i;\n"
22216             "/* clang-format off */\n"
22217             "/* long long long long long long line */\n",
22218             format("// clang-format off\n"
22219                    "// long long long long long long line\n"
22220                    "/* clang-format on */\n"
22221                    "/* long long long long long long line */\n"
22222                    "int i;\n"
22223                    "/* clang-format off */\n"
22224                    "/* long long long long long long line */\n",
22225                    getLLVMStyleWithColumns(20)));
22226 }
22227 
22228 TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
22229   format("? ) =");
22230   verifyNoCrash("#define a\\\n /**/}");
22231 }
22232 
22233 TEST_F(FormatTest, FormatsTableGenCode) {
22234   FormatStyle Style = getLLVMStyle();
22235   Style.Language = FormatStyle::LK_TableGen;
22236   verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style);
22237 }
22238 
22239 TEST_F(FormatTest, ArrayOfTemplates) {
22240   EXPECT_EQ("auto a = new unique_ptr<int>[10];",
22241             format("auto a = new unique_ptr<int > [ 10];"));
22242 
22243   FormatStyle Spaces = getLLVMStyle();
22244   Spaces.SpacesInSquareBrackets = true;
22245   EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];",
22246             format("auto a = new unique_ptr<int > [10];", Spaces));
22247 }
22248 
22249 TEST_F(FormatTest, ArrayAsTemplateType) {
22250   EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;",
22251             format("auto a = unique_ptr < Foo < Bar>[ 10]> ;"));
22252 
22253   FormatStyle Spaces = getLLVMStyle();
22254   Spaces.SpacesInSquareBrackets = true;
22255   EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;",
22256             format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces));
22257 }
22258 
22259 TEST_F(FormatTest, NoSpaceAfterSuper) { verifyFormat("__super::FooBar();"); }
22260 
22261 TEST(FormatStyle, GetStyleWithEmptyFileName) {
22262   llvm::vfs::InMemoryFileSystem FS;
22263   auto Style1 = getStyle("file", "", "Google", "", &FS);
22264   ASSERT_TRUE((bool)Style1);
22265   ASSERT_EQ(*Style1, getGoogleStyle());
22266 }
22267 
22268 TEST(FormatStyle, GetStyleOfFile) {
22269   llvm::vfs::InMemoryFileSystem FS;
22270   // Test 1: format file in the same directory.
22271   ASSERT_TRUE(
22272       FS.addFile("/a/.clang-format", 0,
22273                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM")));
22274   ASSERT_TRUE(
22275       FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
22276   auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS);
22277   ASSERT_TRUE((bool)Style1);
22278   ASSERT_EQ(*Style1, getLLVMStyle());
22279 
22280   // Test 2.1: fallback to default.
22281   ASSERT_TRUE(
22282       FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
22283   auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS);
22284   ASSERT_TRUE((bool)Style2);
22285   ASSERT_EQ(*Style2, getMozillaStyle());
22286 
22287   // Test 2.2: no format on 'none' fallback style.
22288   Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
22289   ASSERT_TRUE((bool)Style2);
22290   ASSERT_EQ(*Style2, getNoStyle());
22291 
22292   // Test 2.3: format if config is found with no based style while fallback is
22293   // 'none'.
22294   ASSERT_TRUE(FS.addFile("/b/.clang-format", 0,
22295                          llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2")));
22296   Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
22297   ASSERT_TRUE((bool)Style2);
22298   ASSERT_EQ(*Style2, getLLVMStyle());
22299 
22300   // Test 2.4: format if yaml with no based style, while fallback is 'none'.
22301   Style2 = getStyle("{}", "a.h", "none", "", &FS);
22302   ASSERT_TRUE((bool)Style2);
22303   ASSERT_EQ(*Style2, getLLVMStyle());
22304 
22305   // Test 3: format file in parent directory.
22306   ASSERT_TRUE(
22307       FS.addFile("/c/.clang-format", 0,
22308                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google")));
22309   ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0,
22310                          llvm::MemoryBuffer::getMemBuffer("int i;")));
22311   auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS);
22312   ASSERT_TRUE((bool)Style3);
22313   ASSERT_EQ(*Style3, getGoogleStyle());
22314 
22315   // Test 4: error on invalid fallback style
22316   auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS);
22317   ASSERT_FALSE((bool)Style4);
22318   llvm::consumeError(Style4.takeError());
22319 
22320   // Test 5: error on invalid yaml on command line
22321   auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS);
22322   ASSERT_FALSE((bool)Style5);
22323   llvm::consumeError(Style5.takeError());
22324 
22325   // Test 6: error on invalid style
22326   auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS);
22327   ASSERT_FALSE((bool)Style6);
22328   llvm::consumeError(Style6.takeError());
22329 
22330   // Test 7: found config file, error on parsing it
22331   ASSERT_TRUE(
22332       FS.addFile("/d/.clang-format", 0,
22333                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n"
22334                                                   "InvalidKey: InvalidValue")));
22335   ASSERT_TRUE(
22336       FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
22337   auto Style7a = getStyle("file", "/d/.clang-format", "LLVM", "", &FS);
22338   ASSERT_FALSE((bool)Style7a);
22339   llvm::consumeError(Style7a.takeError());
22340 
22341   auto Style7b = getStyle("file", "/d/.clang-format", "LLVM", "", &FS, true);
22342   ASSERT_TRUE((bool)Style7b);
22343 
22344   // Test 8: inferred per-language defaults apply.
22345   auto StyleTd = getStyle("file", "x.td", "llvm", "", &FS);
22346   ASSERT_TRUE((bool)StyleTd);
22347   ASSERT_EQ(*StyleTd, getLLVMStyle(FormatStyle::LK_TableGen));
22348 
22349   // Test 9.1.1: overwriting a file style, when no parent file exists with no
22350   // fallback style.
22351   ASSERT_TRUE(FS.addFile(
22352       "/e/sub/.clang-format", 0,
22353       llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: InheritParentConfig\n"
22354                                        "ColumnLimit: 20")));
22355   ASSERT_TRUE(FS.addFile("/e/sub/code.cpp", 0,
22356                          llvm::MemoryBuffer::getMemBuffer("int i;")));
22357   auto Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS);
22358   ASSERT_TRUE(static_cast<bool>(Style9));
22359   ASSERT_EQ(*Style9, [] {
22360     auto Style = getNoStyle();
22361     Style.ColumnLimit = 20;
22362     return Style;
22363   }());
22364 
22365   // Test 9.1.2: propagate more than one level with no parent file.
22366   ASSERT_TRUE(FS.addFile("/e/sub/sub/code.cpp", 0,
22367                          llvm::MemoryBuffer::getMemBuffer("int i;")));
22368   ASSERT_TRUE(FS.addFile("/e/sub/sub/.clang-format", 0,
22369                          llvm::MemoryBuffer::getMemBuffer(
22370                              "BasedOnStyle: InheritParentConfig\n"
22371                              "WhitespaceSensitiveMacros: ['FOO', 'BAR']")));
22372   std::vector<std::string> NonDefaultWhiteSpaceMacros{"FOO", "BAR"};
22373 
22374   ASSERT_NE(Style9->WhitespaceSensitiveMacros, NonDefaultWhiteSpaceMacros);
22375   Style9 = getStyle("file", "/e/sub/sub/code.cpp", "none", "", &FS);
22376   ASSERT_TRUE(static_cast<bool>(Style9));
22377   ASSERT_EQ(*Style9, [&NonDefaultWhiteSpaceMacros] {
22378     auto Style = getNoStyle();
22379     Style.ColumnLimit = 20;
22380     Style.WhitespaceSensitiveMacros = NonDefaultWhiteSpaceMacros;
22381     return Style;
22382   }());
22383 
22384   // Test 9.2: with LLVM fallback style
22385   Style9 = getStyle("file", "/e/sub/code.cpp", "LLVM", "", &FS);
22386   ASSERT_TRUE(static_cast<bool>(Style9));
22387   ASSERT_EQ(*Style9, [] {
22388     auto Style = getLLVMStyle();
22389     Style.ColumnLimit = 20;
22390     return Style;
22391   }());
22392 
22393   // Test 9.3: with a parent file
22394   ASSERT_TRUE(
22395       FS.addFile("/e/.clang-format", 0,
22396                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google\n"
22397                                                   "UseTab: Always")));
22398   Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS);
22399   ASSERT_TRUE(static_cast<bool>(Style9));
22400   ASSERT_EQ(*Style9, [] {
22401     auto Style = getGoogleStyle();
22402     Style.ColumnLimit = 20;
22403     Style.UseTab = FormatStyle::UT_Always;
22404     return Style;
22405   }());
22406 
22407   // Test 9.4: propagate more than one level with a parent file.
22408   const auto SubSubStyle = [&NonDefaultWhiteSpaceMacros] {
22409     auto Style = getGoogleStyle();
22410     Style.ColumnLimit = 20;
22411     Style.UseTab = FormatStyle::UT_Always;
22412     Style.WhitespaceSensitiveMacros = NonDefaultWhiteSpaceMacros;
22413     return Style;
22414   }();
22415 
22416   ASSERT_NE(Style9->WhitespaceSensitiveMacros, NonDefaultWhiteSpaceMacros);
22417   Style9 = getStyle("file", "/e/sub/sub/code.cpp", "none", "", &FS);
22418   ASSERT_TRUE(static_cast<bool>(Style9));
22419   ASSERT_EQ(*Style9, SubSubStyle);
22420 
22421   // Test 9.5: use InheritParentConfig as style name
22422   Style9 =
22423       getStyle("inheritparentconfig", "/e/sub/sub/code.cpp", "none", "", &FS);
22424   ASSERT_TRUE(static_cast<bool>(Style9));
22425   ASSERT_EQ(*Style9, SubSubStyle);
22426 
22427   // Test 9.6: use command line style with inheritance
22428   Style9 = getStyle("{BasedOnStyle: InheritParentConfig}", "/e/sub/code.cpp",
22429                     "none", "", &FS);
22430   ASSERT_TRUE(static_cast<bool>(Style9));
22431   ASSERT_EQ(*Style9, SubSubStyle);
22432 
22433   // Test 9.7: use command line style with inheritance and own config
22434   Style9 = getStyle("{BasedOnStyle: InheritParentConfig, "
22435                     "WhitespaceSensitiveMacros: ['FOO', 'BAR']}",
22436                     "/e/sub/code.cpp", "none", "", &FS);
22437   ASSERT_TRUE(static_cast<bool>(Style9));
22438   ASSERT_EQ(*Style9, SubSubStyle);
22439 
22440   // Test 9.8: use inheritance from a file without BasedOnStyle
22441   ASSERT_TRUE(FS.addFile("/e/withoutbase/.clang-format", 0,
22442                          llvm::MemoryBuffer::getMemBuffer("ColumnLimit: 123")));
22443   ASSERT_TRUE(
22444       FS.addFile("/e/withoutbase/sub/.clang-format", 0,
22445                  llvm::MemoryBuffer::getMemBuffer(
22446                      "BasedOnStyle: InheritParentConfig\nIndentWidth: 7")));
22447   // Make sure we do not use the fallback style
22448   Style9 = getStyle("file", "/e/withoutbase/code.cpp", "google", "", &FS);
22449   ASSERT_TRUE(static_cast<bool>(Style9));
22450   ASSERT_EQ(*Style9, [] {
22451     auto Style = getLLVMStyle();
22452     Style.ColumnLimit = 123;
22453     return Style;
22454   }());
22455 
22456   Style9 = getStyle("file", "/e/withoutbase/sub/code.cpp", "google", "", &FS);
22457   ASSERT_TRUE(static_cast<bool>(Style9));
22458   ASSERT_EQ(*Style9, [] {
22459     auto Style = getLLVMStyle();
22460     Style.ColumnLimit = 123;
22461     Style.IndentWidth = 7;
22462     return Style;
22463   }());
22464 
22465   // Test 9.9: use inheritance from a specific config file.
22466   Style9 = getStyle("file:/e/sub/sub/.clang-format", "/e/sub/sub/code.cpp",
22467                     "none", "", &FS);
22468   ASSERT_TRUE(static_cast<bool>(Style9));
22469   ASSERT_EQ(*Style9, SubSubStyle);
22470 }
22471 
22472 TEST(FormatStyle, GetStyleOfSpecificFile) {
22473   llvm::vfs::InMemoryFileSystem FS;
22474   // Specify absolute path to a format file in a parent directory.
22475   ASSERT_TRUE(
22476       FS.addFile("/e/.clang-format", 0,
22477                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM")));
22478   ASSERT_TRUE(
22479       FS.addFile("/e/explicit.clang-format", 0,
22480                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google")));
22481   ASSERT_TRUE(FS.addFile("/e/sub/sub/sub/test.cpp", 0,
22482                          llvm::MemoryBuffer::getMemBuffer("int i;")));
22483   auto Style = getStyle("file:/e/explicit.clang-format",
22484                         "/e/sub/sub/sub/test.cpp", "LLVM", "", &FS);
22485   ASSERT_TRUE(static_cast<bool>(Style));
22486   ASSERT_EQ(*Style, getGoogleStyle());
22487 
22488   // Specify relative path to a format file.
22489   ASSERT_TRUE(
22490       FS.addFile("../../e/explicit.clang-format", 0,
22491                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google")));
22492   Style = getStyle("file:../../e/explicit.clang-format",
22493                    "/e/sub/sub/sub/test.cpp", "LLVM", "", &FS);
22494   ASSERT_TRUE(static_cast<bool>(Style));
22495   ASSERT_EQ(*Style, getGoogleStyle());
22496 
22497   // Specify path to a format file that does not exist.
22498   Style = getStyle("file:/e/missing.clang-format", "/e/sub/sub/sub/test.cpp",
22499                    "LLVM", "", &FS);
22500   ASSERT_FALSE(static_cast<bool>(Style));
22501   llvm::consumeError(Style.takeError());
22502 
22503   // Specify path to a file on the filesystem.
22504   SmallString<128> FormatFilePath;
22505   std::error_code ECF = llvm::sys::fs::createTemporaryFile(
22506       "FormatFileTest", "tpl", FormatFilePath);
22507   EXPECT_FALSE((bool)ECF);
22508   llvm::raw_fd_ostream FormatFileTest(FormatFilePath, ECF);
22509   EXPECT_FALSE((bool)ECF);
22510   FormatFileTest << "BasedOnStyle: Google\n";
22511   FormatFileTest.close();
22512 
22513   SmallString<128> TestFilePath;
22514   std::error_code ECT =
22515       llvm::sys::fs::createTemporaryFile("CodeFileTest", "cc", TestFilePath);
22516   EXPECT_FALSE((bool)ECT);
22517   llvm::raw_fd_ostream CodeFileTest(TestFilePath, ECT);
22518   CodeFileTest << "int i;\n";
22519   CodeFileTest.close();
22520 
22521   std::string format_file_arg = std::string("file:") + FormatFilePath.c_str();
22522   Style = getStyle(format_file_arg, TestFilePath, "LLVM", "", nullptr);
22523 
22524   llvm::sys::fs::remove(FormatFilePath.c_str());
22525   llvm::sys::fs::remove(TestFilePath.c_str());
22526   ASSERT_TRUE(static_cast<bool>(Style));
22527   ASSERT_EQ(*Style, getGoogleStyle());
22528 }
22529 
22530 TEST_F(ReplacementTest, FormatCodeAfterReplacements) {
22531   // Column limit is 20.
22532   std::string Code = "Type *a =\n"
22533                      "    new Type();\n"
22534                      "g(iiiii, 0, jjjjj,\n"
22535                      "  0, kkkkk, 0, mm);\n"
22536                      "int  bad     = format   ;";
22537   std::string Expected = "auto a = new Type();\n"
22538                          "g(iiiii, nullptr,\n"
22539                          "  jjjjj, nullptr,\n"
22540                          "  kkkkk, nullptr,\n"
22541                          "  mm);\n"
22542                          "int  bad     = format   ;";
22543   FileID ID = Context.createInMemoryFile("format.cpp", Code);
22544   tooling::Replacements Replaces = toReplacements(
22545       {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6,
22546                             "auto "),
22547        tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1,
22548                             "nullptr"),
22549        tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1,
22550                             "nullptr"),
22551        tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1,
22552                             "nullptr")});
22553 
22554   FormatStyle Style = getLLVMStyle();
22555   Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility.
22556   auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
22557   EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
22558       << llvm::toString(FormattedReplaces.takeError()) << "\n";
22559   auto Result = applyAllReplacements(Code, *FormattedReplaces);
22560   EXPECT_TRUE(static_cast<bool>(Result));
22561   EXPECT_EQ(Expected, *Result);
22562 }
22563 
22564 TEST_F(ReplacementTest, SortIncludesAfterReplacement) {
22565   std::string Code = "#include \"a.h\"\n"
22566                      "#include \"c.h\"\n"
22567                      "\n"
22568                      "int main() {\n"
22569                      "  return 0;\n"
22570                      "}";
22571   std::string Expected = "#include \"a.h\"\n"
22572                          "#include \"b.h\"\n"
22573                          "#include \"c.h\"\n"
22574                          "\n"
22575                          "int main() {\n"
22576                          "  return 0;\n"
22577                          "}";
22578   FileID ID = Context.createInMemoryFile("fix.cpp", Code);
22579   tooling::Replacements Replaces = toReplacements(
22580       {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0,
22581                             "#include \"b.h\"\n")});
22582 
22583   FormatStyle Style = getLLVMStyle();
22584   Style.SortIncludes = FormatStyle::SI_CaseSensitive;
22585   auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
22586   EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
22587       << llvm::toString(FormattedReplaces.takeError()) << "\n";
22588   auto Result = applyAllReplacements(Code, *FormattedReplaces);
22589   EXPECT_TRUE(static_cast<bool>(Result));
22590   EXPECT_EQ(Expected, *Result);
22591 }
22592 
22593 TEST_F(FormatTest, FormatSortsUsingDeclarations) {
22594   EXPECT_EQ("using std::cin;\n"
22595             "using std::cout;",
22596             format("using std::cout;\n"
22597                    "using std::cin;",
22598                    getGoogleStyle()));
22599 }
22600 
22601 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) {
22602   FormatStyle Style = getLLVMStyle();
22603   Style.Standard = FormatStyle::LS_Cpp03;
22604   // cpp03 recognize this string as identifier u8 and literal character 'a'
22605   EXPECT_EQ("auto c = u8 'a';", format("auto c = u8'a';", Style));
22606 }
22607 
22608 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) {
22609   // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers
22610   // all modes, including C++11, C++14 and C++17
22611   EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';"));
22612 }
22613 
22614 TEST_F(FormatTest, DoNotFormatLikelyXml) {
22615   EXPECT_EQ("<!-- ;> -->", format("<!-- ;> -->", getGoogleStyle()));
22616   EXPECT_EQ(" <!-- >; -->", format(" <!-- >; -->", getGoogleStyle()));
22617 }
22618 
22619 TEST_F(FormatTest, StructuredBindings) {
22620   // Structured bindings is a C++17 feature.
22621   // all modes, including C++11, C++14 and C++17
22622   verifyFormat("auto [a, b] = f();");
22623   EXPECT_EQ("auto [a, b] = f();", format("auto[a, b] = f();"));
22624   EXPECT_EQ("const auto [a, b] = f();", format("const   auto[a, b] = f();"));
22625   EXPECT_EQ("auto const [a, b] = f();", format("auto  const[a, b] = f();"));
22626   EXPECT_EQ("auto const volatile [a, b] = f();",
22627             format("auto  const   volatile[a, b] = f();"));
22628   EXPECT_EQ("auto [a, b, c] = f();", format("auto   [  a  ,  b,c   ] = f();"));
22629   EXPECT_EQ("auto &[a, b, c] = f();",
22630             format("auto   &[  a  ,  b,c   ] = f();"));
22631   EXPECT_EQ("auto &&[a, b, c] = f();",
22632             format("auto   &&[  a  ,  b,c   ] = f();"));
22633   EXPECT_EQ("auto const &[a, b] = f();", format("auto  const&[a, b] = f();"));
22634   EXPECT_EQ("auto const volatile &&[a, b] = f();",
22635             format("auto  const  volatile  &&[a, b] = f();"));
22636   EXPECT_EQ("auto const &&[a, b] = f();",
22637             format("auto  const   &&  [a, b] = f();"));
22638   EXPECT_EQ("const auto &[a, b] = f();",
22639             format("const  auto  &  [a, b] = f();"));
22640   EXPECT_EQ("const auto volatile &&[a, b] = f();",
22641             format("const  auto   volatile  &&[a, b] = f();"));
22642   EXPECT_EQ("volatile const auto &&[a, b] = f();",
22643             format("volatile  const  auto   &&[a, b] = f();"));
22644   EXPECT_EQ("const auto &&[a, b] = f();",
22645             format("const  auto  &&  [a, b] = f();"));
22646 
22647   // Make sure we don't mistake structured bindings for lambdas.
22648   FormatStyle PointerMiddle = getLLVMStyle();
22649   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
22650   verifyFormat("auto [a1, b]{A * i};", getGoogleStyle());
22651   verifyFormat("auto [a2, b]{A * i};", getLLVMStyle());
22652   verifyFormat("auto [a3, b]{A * i};", PointerMiddle);
22653   verifyFormat("auto const [a1, b]{A * i};", getGoogleStyle());
22654   verifyFormat("auto const [a2, b]{A * i};", getLLVMStyle());
22655   verifyFormat("auto const [a3, b]{A * i};", PointerMiddle);
22656   verifyFormat("auto const& [a1, b]{A * i};", getGoogleStyle());
22657   verifyFormat("auto const &[a2, b]{A * i};", getLLVMStyle());
22658   verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle);
22659   verifyFormat("auto const&& [a1, b]{A * i};", getGoogleStyle());
22660   verifyFormat("auto const &&[a2, b]{A * i};", getLLVMStyle());
22661   verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle);
22662 
22663   EXPECT_EQ("for (const auto &&[a, b] : some_range) {\n}",
22664             format("for (const auto   &&   [a, b] : some_range) {\n}"));
22665   EXPECT_EQ("for (const auto &[a, b] : some_range) {\n}",
22666             format("for (const auto   &   [a, b] : some_range) {\n}"));
22667   EXPECT_EQ("for (const auto [a, b] : some_range) {\n}",
22668             format("for (const auto[a, b] : some_range) {\n}"));
22669   EXPECT_EQ("auto [x, y](expr);", format("auto[x,y]  (expr);"));
22670   EXPECT_EQ("auto &[x, y](expr);", format("auto  &  [x,y]  (expr);"));
22671   EXPECT_EQ("auto &&[x, y](expr);", format("auto  &&  [x,y]  (expr);"));
22672   EXPECT_EQ("auto const &[x, y](expr);",
22673             format("auto  const  &  [x,y]  (expr);"));
22674   EXPECT_EQ("auto const &&[x, y](expr);",
22675             format("auto  const  &&  [x,y]  (expr);"));
22676   EXPECT_EQ("auto [x, y]{expr};", format("auto[x,y]     {expr};"));
22677   EXPECT_EQ("auto const &[x, y]{expr};",
22678             format("auto  const  &  [x,y]  {expr};"));
22679   EXPECT_EQ("auto const &&[x, y]{expr};",
22680             format("auto  const  &&  [x,y]  {expr};"));
22681 
22682   FormatStyle Spaces = getLLVMStyle();
22683   Spaces.SpacesInSquareBrackets = true;
22684   verifyFormat("auto [ a, b ] = f();", Spaces);
22685   verifyFormat("auto &&[ a, b ] = f();", Spaces);
22686   verifyFormat("auto &[ a, b ] = f();", Spaces);
22687   verifyFormat("auto const &&[ a, b ] = f();", Spaces);
22688   verifyFormat("auto const &[ a, b ] = f();", Spaces);
22689 }
22690 
22691 TEST_F(FormatTest, FileAndCode) {
22692   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.cc", ""));
22693   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.m", ""));
22694   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.mm", ""));
22695   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", ""));
22696   EXPECT_EQ(FormatStyle::LK_ObjC,
22697             guessLanguage("foo.h", "@interface Foo\n@end\n"));
22698   EXPECT_EQ(
22699       FormatStyle::LK_ObjC,
22700       guessLanguage("foo.h", "#define TRY(x, y) @try { x; } @finally { y; }"));
22701   EXPECT_EQ(FormatStyle::LK_ObjC,
22702             guessLanguage("foo.h", "#define AVAIL(x) @available(x, *))"));
22703   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@class Foo;"));
22704   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo", ""));
22705   EXPECT_EQ(FormatStyle::LK_ObjC,
22706             guessLanguage("foo", "@interface Foo\n@end\n"));
22707   EXPECT_EQ(FormatStyle::LK_ObjC,
22708             guessLanguage("foo.h", "int DoStuff(CGRect rect);\n"));
22709   EXPECT_EQ(
22710       FormatStyle::LK_ObjC,
22711       guessLanguage("foo.h",
22712                     "#define MY_POINT_MAKE(x, y) CGPointMake((x), (y));\n"));
22713   EXPECT_EQ(
22714       FormatStyle::LK_Cpp,
22715       guessLanguage("foo.h", "#define FOO(...) auto bar = [] __VA_ARGS__;"));
22716 }
22717 
22718 TEST_F(FormatTest, GuessLanguageWithCpp11AttributeSpecifiers) {
22719   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[noreturn]];"));
22720   EXPECT_EQ(FormatStyle::LK_ObjC,
22721             guessLanguage("foo.h", "array[[calculator getIndex]];"));
22722   EXPECT_EQ(FormatStyle::LK_Cpp,
22723             guessLanguage("foo.h", "[[noreturn, deprecated(\"so sorry\")]];"));
22724   EXPECT_EQ(
22725       FormatStyle::LK_Cpp,
22726       guessLanguage("foo.h", "[[noreturn, deprecated(\"gone, sorry\")]];"));
22727   EXPECT_EQ(FormatStyle::LK_ObjC,
22728             guessLanguage("foo.h", "[[noreturn foo] bar];"));
22729   EXPECT_EQ(FormatStyle::LK_Cpp,
22730             guessLanguage("foo.h", "[[clang::fallthrough]];"));
22731   EXPECT_EQ(FormatStyle::LK_ObjC,
22732             guessLanguage("foo.h", "[[clang:fallthrough] foo];"));
22733   EXPECT_EQ(FormatStyle::LK_Cpp,
22734             guessLanguage("foo.h", "[[gsl::suppress(\"type\")]];"));
22735   EXPECT_EQ(FormatStyle::LK_Cpp,
22736             guessLanguage("foo.h", "[[using clang: fallthrough]];"));
22737   EXPECT_EQ(FormatStyle::LK_ObjC,
22738             guessLanguage("foo.h", "[[abusing clang:fallthrough] bar];"));
22739   EXPECT_EQ(FormatStyle::LK_Cpp,
22740             guessLanguage("foo.h", "[[using gsl: suppress(\"type\")]];"));
22741   EXPECT_EQ(
22742       FormatStyle::LK_Cpp,
22743       guessLanguage("foo.h", "for (auto &&[endpoint, stream] : streams_)"));
22744   EXPECT_EQ(
22745       FormatStyle::LK_Cpp,
22746       guessLanguage("foo.h",
22747                     "[[clang::callable_when(\"unconsumed\", \"unknown\")]]"));
22748   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[foo::bar, ...]]"));
22749 }
22750 
22751 TEST_F(FormatTest, GuessLanguageWithCaret) {
22752   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^);"));
22753   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^, Bar);"));
22754   EXPECT_EQ(FormatStyle::LK_ObjC,
22755             guessLanguage("foo.h", "int(^)(char, float);"));
22756   EXPECT_EQ(FormatStyle::LK_ObjC,
22757             guessLanguage("foo.h", "int(^foo)(char, float);"));
22758   EXPECT_EQ(FormatStyle::LK_ObjC,
22759             guessLanguage("foo.h", "int(^foo[10])(char, float);"));
22760   EXPECT_EQ(FormatStyle::LK_ObjC,
22761             guessLanguage("foo.h", "int(^foo[kNumEntries])(char, float);"));
22762   EXPECT_EQ(
22763       FormatStyle::LK_ObjC,
22764       guessLanguage("foo.h", "int(^foo[(kNumEntries + 10)])(char, float);"));
22765 }
22766 
22767 TEST_F(FormatTest, GuessLanguageWithPragmas) {
22768   EXPECT_EQ(FormatStyle::LK_Cpp,
22769             guessLanguage("foo.h", "__pragma(warning(disable:))"));
22770   EXPECT_EQ(FormatStyle::LK_Cpp,
22771             guessLanguage("foo.h", "#pragma(warning(disable:))"));
22772   EXPECT_EQ(FormatStyle::LK_Cpp,
22773             guessLanguage("foo.h", "_Pragma(warning(disable:))"));
22774 }
22775 
22776 TEST_F(FormatTest, FormatsInlineAsmSymbolicNames) {
22777   // ASM symbolic names are identifiers that must be surrounded by [] without
22778   // space in between:
22779   // https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#InputOperands
22780 
22781   // Example from https://bugs.llvm.org/show_bug.cgi?id=45108.
22782   verifyFormat(R"(//
22783 asm volatile("mrs %x[result], FPCR" : [result] "=r"(result));
22784 )");
22785 
22786   // A list of several ASM symbolic names.
22787   verifyFormat(R"(asm("mov %[e], %[d]" : [d] "=rm"(d), [e] "rm"(*e));)");
22788 
22789   // ASM symbolic names in inline ASM with inputs and outputs.
22790   verifyFormat(R"(//
22791 asm("cmoveq %1, %2, %[result]"
22792     : [result] "=r"(result)
22793     : "r"(test), "r"(new), "[result]"(old));
22794 )");
22795 
22796   // ASM symbolic names in inline ASM with no outputs.
22797   verifyFormat(R"(asm("mov %[e], %[d]" : : [d] "=rm"(d), [e] "rm"(*e));)");
22798 }
22799 
22800 TEST_F(FormatTest, GuessedLanguageWithInlineAsmClobbers) {
22801   EXPECT_EQ(FormatStyle::LK_Cpp,
22802             guessLanguage("foo.h", "void f() {\n"
22803                                    "  asm (\"mov %[e], %[d]\"\n"
22804                                    "     : [d] \"=rm\" (d)\n"
22805                                    "       [e] \"rm\" (*e));\n"
22806                                    "}"));
22807   EXPECT_EQ(FormatStyle::LK_Cpp,
22808             guessLanguage("foo.h", "void f() {\n"
22809                                    "  _asm (\"mov %[e], %[d]\"\n"
22810                                    "     : [d] \"=rm\" (d)\n"
22811                                    "       [e] \"rm\" (*e));\n"
22812                                    "}"));
22813   EXPECT_EQ(FormatStyle::LK_Cpp,
22814             guessLanguage("foo.h", "void f() {\n"
22815                                    "  __asm (\"mov %[e], %[d]\"\n"
22816                                    "     : [d] \"=rm\" (d)\n"
22817                                    "       [e] \"rm\" (*e));\n"
22818                                    "}"));
22819   EXPECT_EQ(FormatStyle::LK_Cpp,
22820             guessLanguage("foo.h", "void f() {\n"
22821                                    "  __asm__ (\"mov %[e], %[d]\"\n"
22822                                    "     : [d] \"=rm\" (d)\n"
22823                                    "       [e] \"rm\" (*e));\n"
22824                                    "}"));
22825   EXPECT_EQ(FormatStyle::LK_Cpp,
22826             guessLanguage("foo.h", "void f() {\n"
22827                                    "  asm (\"mov %[e], %[d]\"\n"
22828                                    "     : [d] \"=rm\" (d),\n"
22829                                    "       [e] \"rm\" (*e));\n"
22830                                    "}"));
22831   EXPECT_EQ(FormatStyle::LK_Cpp,
22832             guessLanguage("foo.h", "void f() {\n"
22833                                    "  asm volatile (\"mov %[e], %[d]\"\n"
22834                                    "     : [d] \"=rm\" (d)\n"
22835                                    "       [e] \"rm\" (*e));\n"
22836                                    "}"));
22837 }
22838 
22839 TEST_F(FormatTest, GuessLanguageWithChildLines) {
22840   EXPECT_EQ(FormatStyle::LK_Cpp,
22841             guessLanguage("foo.h", "#define FOO ({ std::string s; })"));
22842   EXPECT_EQ(FormatStyle::LK_ObjC,
22843             guessLanguage("foo.h", "#define FOO ({ NSString *s; })"));
22844   EXPECT_EQ(
22845       FormatStyle::LK_Cpp,
22846       guessLanguage("foo.h", "#define FOO ({ foo(); ({ std::string s; }) })"));
22847   EXPECT_EQ(
22848       FormatStyle::LK_ObjC,
22849       guessLanguage("foo.h", "#define FOO ({ foo(); ({ NSString *s; }) })"));
22850 }
22851 
22852 TEST_F(FormatTest, TypenameMacros) {
22853   std::vector<std::string> TypenameMacros = {"STACK_OF", "LIST", "TAILQ_ENTRY"};
22854 
22855   // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=30353
22856   FormatStyle Google = getGoogleStyleWithColumns(0);
22857   Google.TypenameMacros = TypenameMacros;
22858   verifyFormat("struct foo {\n"
22859                "  int bar;\n"
22860                "  TAILQ_ENTRY(a) bleh;\n"
22861                "};",
22862                Google);
22863 
22864   FormatStyle Macros = getLLVMStyle();
22865   Macros.TypenameMacros = TypenameMacros;
22866 
22867   verifyFormat("STACK_OF(int) a;", Macros);
22868   verifyFormat("STACK_OF(int) *a;", Macros);
22869   verifyFormat("STACK_OF(int const *) *a;", Macros);
22870   verifyFormat("STACK_OF(int *const) *a;", Macros);
22871   verifyFormat("STACK_OF(int, string) a;", Macros);
22872   verifyFormat("STACK_OF(LIST(int)) a;", Macros);
22873   verifyFormat("STACK_OF(LIST(int)) a, b;", Macros);
22874   verifyFormat("for (LIST(int) *a = NULL; a;) {\n}", Macros);
22875   verifyFormat("STACK_OF(int) f(LIST(int) *arg);", Macros);
22876   verifyFormat("vector<LIST(uint64_t) *attr> x;", Macros);
22877   verifyFormat("vector<LIST(uint64_t) *const> f(LIST(uint64_t) *arg);", Macros);
22878 
22879   Macros.PointerAlignment = FormatStyle::PAS_Left;
22880   verifyFormat("STACK_OF(int)* a;", Macros);
22881   verifyFormat("STACK_OF(int*)* a;", Macros);
22882   verifyFormat("x = (STACK_OF(uint64_t))*a;", Macros);
22883   verifyFormat("x = (STACK_OF(uint64_t))&a;", Macros);
22884   verifyFormat("vector<STACK_OF(uint64_t)* attr> x;", Macros);
22885 }
22886 
22887 TEST_F(FormatTest, AtomicQualifier) {
22888   // Check that we treate _Atomic as a type and not a function call
22889   FormatStyle Google = getGoogleStyleWithColumns(0);
22890   verifyFormat("struct foo {\n"
22891                "  int a1;\n"
22892                "  _Atomic(a) a2;\n"
22893                "  _Atomic(_Atomic(int) *const) a3;\n"
22894                "};",
22895                Google);
22896   verifyFormat("_Atomic(uint64_t) a;");
22897   verifyFormat("_Atomic(uint64_t) *a;");
22898   verifyFormat("_Atomic(uint64_t const *) *a;");
22899   verifyFormat("_Atomic(uint64_t *const) *a;");
22900   verifyFormat("_Atomic(const uint64_t *) *a;");
22901   verifyFormat("_Atomic(uint64_t) a;");
22902   verifyFormat("_Atomic(_Atomic(uint64_t)) a;");
22903   verifyFormat("_Atomic(_Atomic(uint64_t)) a, b;");
22904   verifyFormat("for (_Atomic(uint64_t) *a = NULL; a;) {\n}");
22905   verifyFormat("_Atomic(uint64_t) f(_Atomic(uint64_t) *arg);");
22906 
22907   verifyFormat("_Atomic(uint64_t) *s(InitValue);");
22908   verifyFormat("_Atomic(uint64_t) *s{InitValue};");
22909   FormatStyle Style = getLLVMStyle();
22910   Style.PointerAlignment = FormatStyle::PAS_Left;
22911   verifyFormat("_Atomic(uint64_t)* s(InitValue);", Style);
22912   verifyFormat("_Atomic(uint64_t)* s{InitValue};", Style);
22913   verifyFormat("_Atomic(int)* a;", Style);
22914   verifyFormat("_Atomic(int*)* a;", Style);
22915   verifyFormat("vector<_Atomic(uint64_t)* attr> x;", Style);
22916 
22917   Style.SpacesInCStyleCastParentheses = true;
22918   Style.SpacesInParentheses = false;
22919   verifyFormat("x = ( _Atomic(uint64_t) )*a;", Style);
22920   Style.SpacesInCStyleCastParentheses = false;
22921   Style.SpacesInParentheses = true;
22922   verifyFormat("x = (_Atomic( uint64_t ))*a;", Style);
22923   verifyFormat("x = (_Atomic( uint64_t ))&a;", Style);
22924 }
22925 
22926 TEST_F(FormatTest, AmbersandInLamda) {
22927   // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=41899
22928   FormatStyle AlignStyle = getLLVMStyle();
22929   AlignStyle.PointerAlignment = FormatStyle::PAS_Left;
22930   verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle);
22931   AlignStyle.PointerAlignment = FormatStyle::PAS_Right;
22932   verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle);
22933 }
22934 
22935 TEST_F(FormatTest, SpacesInConditionalStatement) {
22936   FormatStyle Spaces = getLLVMStyle();
22937   Spaces.IfMacros.clear();
22938   Spaces.IfMacros.push_back("MYIF");
22939   Spaces.SpacesInConditionalStatement = true;
22940   verifyFormat("for ( int i = 0; i; i++ )\n  continue;", Spaces);
22941   verifyFormat("if ( !a )\n  return;", Spaces);
22942   verifyFormat("if ( a )\n  return;", Spaces);
22943   verifyFormat("if constexpr ( a )\n  return;", Spaces);
22944   verifyFormat("MYIF ( a )\n  return;", Spaces);
22945   verifyFormat("MYIF ( a )\n  return;\nelse MYIF ( b )\n  return;", Spaces);
22946   verifyFormat("MYIF ( a )\n  return;\nelse\n  return;", Spaces);
22947   verifyFormat("switch ( a )\ncase 1:\n  return;", Spaces);
22948   verifyFormat("while ( a )\n  return;", Spaces);
22949   verifyFormat("while ( (a && b) )\n  return;", Spaces);
22950   verifyFormat("do {\n} while ( 1 != 0 );", Spaces);
22951   verifyFormat("try {\n} catch ( const std::exception & ) {\n}", Spaces);
22952   // Check that space on the left of "::" is inserted as expected at beginning
22953   // of condition.
22954   verifyFormat("while ( ::func() )\n  return;", Spaces);
22955 
22956   // Check impact of ControlStatementsExceptControlMacros is honored.
22957   Spaces.SpaceBeforeParens =
22958       FormatStyle::SBPO_ControlStatementsExceptControlMacros;
22959   verifyFormat("MYIF( a )\n  return;", Spaces);
22960   verifyFormat("MYIF( a )\n  return;\nelse MYIF( b )\n  return;", Spaces);
22961   verifyFormat("MYIF( a )\n  return;\nelse\n  return;", Spaces);
22962 }
22963 
22964 TEST_F(FormatTest, AlternativeOperators) {
22965   // Test case for ensuring alternate operators are not
22966   // combined with their right most neighbour.
22967   verifyFormat("int a and b;");
22968   verifyFormat("int a and_eq b;");
22969   verifyFormat("int a bitand b;");
22970   verifyFormat("int a bitor b;");
22971   verifyFormat("int a compl b;");
22972   verifyFormat("int a not b;");
22973   verifyFormat("int a not_eq b;");
22974   verifyFormat("int a or b;");
22975   verifyFormat("int a xor b;");
22976   verifyFormat("int a xor_eq b;");
22977   verifyFormat("return this not_eq bitand other;");
22978   verifyFormat("bool operator not_eq(const X bitand other)");
22979 
22980   verifyFormat("int a and 5;");
22981   verifyFormat("int a and_eq 5;");
22982   verifyFormat("int a bitand 5;");
22983   verifyFormat("int a bitor 5;");
22984   verifyFormat("int a compl 5;");
22985   verifyFormat("int a not 5;");
22986   verifyFormat("int a not_eq 5;");
22987   verifyFormat("int a or 5;");
22988   verifyFormat("int a xor 5;");
22989   verifyFormat("int a xor_eq 5;");
22990 
22991   verifyFormat("int a compl(5);");
22992   verifyFormat("int a not(5);");
22993 
22994   /* FIXME handle alternate tokens
22995    * https://en.cppreference.com/w/cpp/language/operator_alternative
22996   // alternative tokens
22997   verifyFormat("compl foo();");     //  ~foo();
22998   verifyFormat("foo() <%%>;");      // foo();
22999   verifyFormat("void foo() <%%>;"); // void foo(){}
23000   verifyFormat("int a <:1:>;");     // int a[1];[
23001   verifyFormat("%:define ABC abc"); // #define ABC abc
23002   verifyFormat("%:%:");             // ##
23003   */
23004 }
23005 
23006 TEST_F(FormatTest, STLWhileNotDefineChed) {
23007   verifyFormat("#if defined(while)\n"
23008                "#define while EMIT WARNING C4005\n"
23009                "#endif // while");
23010 }
23011 
23012 TEST_F(FormatTest, OperatorSpacing) {
23013   FormatStyle Style = getLLVMStyle();
23014   Style.PointerAlignment = FormatStyle::PAS_Right;
23015   verifyFormat("Foo::operator*();", Style);
23016   verifyFormat("Foo::operator void *();", Style);
23017   verifyFormat("Foo::operator void **();", Style);
23018   verifyFormat("Foo::operator void *&();", Style);
23019   verifyFormat("Foo::operator void *&&();", Style);
23020   verifyFormat("Foo::operator void const *();", Style);
23021   verifyFormat("Foo::operator void const **();", Style);
23022   verifyFormat("Foo::operator void const *&();", Style);
23023   verifyFormat("Foo::operator void const *&&();", Style);
23024   verifyFormat("Foo::operator()(void *);", Style);
23025   verifyFormat("Foo::operator*(void *);", Style);
23026   verifyFormat("Foo::operator*();", Style);
23027   verifyFormat("Foo::operator**();", Style);
23028   verifyFormat("Foo::operator&();", Style);
23029   verifyFormat("Foo::operator<int> *();", Style);
23030   verifyFormat("Foo::operator<Foo> *();", Style);
23031   verifyFormat("Foo::operator<int> **();", Style);
23032   verifyFormat("Foo::operator<Foo> **();", Style);
23033   verifyFormat("Foo::operator<int> &();", Style);
23034   verifyFormat("Foo::operator<Foo> &();", Style);
23035   verifyFormat("Foo::operator<int> &&();", Style);
23036   verifyFormat("Foo::operator<Foo> &&();", Style);
23037   verifyFormat("Foo::operator<int> *&();", Style);
23038   verifyFormat("Foo::operator<Foo> *&();", Style);
23039   verifyFormat("Foo::operator<int> *&&();", Style);
23040   verifyFormat("Foo::operator<Foo> *&&();", Style);
23041   verifyFormat("operator*(int (*)(), class Foo);", Style);
23042 
23043   verifyFormat("Foo::operator&();", Style);
23044   verifyFormat("Foo::operator void &();", Style);
23045   verifyFormat("Foo::operator void const &();", Style);
23046   verifyFormat("Foo::operator()(void &);", Style);
23047   verifyFormat("Foo::operator&(void &);", Style);
23048   verifyFormat("Foo::operator&();", Style);
23049   verifyFormat("operator&(int (&)(), class Foo);", Style);
23050   verifyFormat("operator&&(int (&)(), class Foo);", Style);
23051 
23052   verifyFormat("Foo::operator&&();", Style);
23053   verifyFormat("Foo::operator**();", Style);
23054   verifyFormat("Foo::operator void &&();", Style);
23055   verifyFormat("Foo::operator void const &&();", Style);
23056   verifyFormat("Foo::operator()(void &&);", Style);
23057   verifyFormat("Foo::operator&&(void &&);", Style);
23058   verifyFormat("Foo::operator&&();", Style);
23059   verifyFormat("operator&&(int (&&)(), class Foo);", Style);
23060   verifyFormat("operator const nsTArrayRight<E> &()", Style);
23061   verifyFormat("[[nodiscard]] operator const nsTArrayRight<E, Allocator> &()",
23062                Style);
23063   verifyFormat("operator void **()", Style);
23064   verifyFormat("operator const FooRight<Object> &()", Style);
23065   verifyFormat("operator const FooRight<Object> *()", Style);
23066   verifyFormat("operator const FooRight<Object> **()", Style);
23067   verifyFormat("operator const FooRight<Object> *&()", Style);
23068   verifyFormat("operator const FooRight<Object> *&&()", Style);
23069 
23070   Style.PointerAlignment = FormatStyle::PAS_Left;
23071   verifyFormat("Foo::operator*();", Style);
23072   verifyFormat("Foo::operator**();", Style);
23073   verifyFormat("Foo::operator void*();", Style);
23074   verifyFormat("Foo::operator void**();", Style);
23075   verifyFormat("Foo::operator void*&();", Style);
23076   verifyFormat("Foo::operator void*&&();", Style);
23077   verifyFormat("Foo::operator void const*();", Style);
23078   verifyFormat("Foo::operator void const**();", Style);
23079   verifyFormat("Foo::operator void const*&();", Style);
23080   verifyFormat("Foo::operator void const*&&();", Style);
23081   verifyFormat("Foo::operator/*comment*/ void*();", Style);
23082   verifyFormat("Foo::operator/*a*/ const /*b*/ void*();", Style);
23083   verifyFormat("Foo::operator/*a*/ volatile /*b*/ void*();", Style);
23084   verifyFormat("Foo::operator()(void*);", Style);
23085   verifyFormat("Foo::operator*(void*);", Style);
23086   verifyFormat("Foo::operator*();", Style);
23087   verifyFormat("Foo::operator<int>*();", Style);
23088   verifyFormat("Foo::operator<Foo>*();", Style);
23089   verifyFormat("Foo::operator<int>**();", Style);
23090   verifyFormat("Foo::operator<Foo>**();", Style);
23091   verifyFormat("Foo::operator<Foo>*&();", Style);
23092   verifyFormat("Foo::operator<int>&();", Style);
23093   verifyFormat("Foo::operator<Foo>&();", Style);
23094   verifyFormat("Foo::operator<int>&&();", Style);
23095   verifyFormat("Foo::operator<Foo>&&();", Style);
23096   verifyFormat("Foo::operator<int>*&();", Style);
23097   verifyFormat("Foo::operator<Foo>*&();", Style);
23098   verifyFormat("operator*(int (*)(), class Foo);", Style);
23099 
23100   verifyFormat("Foo::operator&();", Style);
23101   verifyFormat("Foo::operator void&();", Style);
23102   verifyFormat("Foo::operator void const&();", Style);
23103   verifyFormat("Foo::operator/*comment*/ void&();", Style);
23104   verifyFormat("Foo::operator/*a*/ const /*b*/ void&();", Style);
23105   verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&();", Style);
23106   verifyFormat("Foo::operator()(void&);", Style);
23107   verifyFormat("Foo::operator&(void&);", Style);
23108   verifyFormat("Foo::operator&();", Style);
23109   verifyFormat("operator&(int (&)(), class Foo);", Style);
23110   verifyFormat("operator&(int (&&)(), class Foo);", Style);
23111   verifyFormat("operator&&(int (&&)(), class Foo);", Style);
23112 
23113   verifyFormat("Foo::operator&&();", Style);
23114   verifyFormat("Foo::operator void&&();", Style);
23115   verifyFormat("Foo::operator void const&&();", Style);
23116   verifyFormat("Foo::operator/*comment*/ void&&();", Style);
23117   verifyFormat("Foo::operator/*a*/ const /*b*/ void&&();", Style);
23118   verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&&();", Style);
23119   verifyFormat("Foo::operator()(void&&);", Style);
23120   verifyFormat("Foo::operator&&(void&&);", Style);
23121   verifyFormat("Foo::operator&&();", Style);
23122   verifyFormat("operator&&(int (&&)(), class Foo);", Style);
23123   verifyFormat("operator const nsTArrayLeft<E>&()", Style);
23124   verifyFormat("[[nodiscard]] operator const nsTArrayLeft<E, Allocator>&()",
23125                Style);
23126   verifyFormat("operator void**()", Style);
23127   verifyFormat("operator const FooLeft<Object>&()", Style);
23128   verifyFormat("operator const FooLeft<Object>*()", Style);
23129   verifyFormat("operator const FooLeft<Object>**()", Style);
23130   verifyFormat("operator const FooLeft<Object>*&()", Style);
23131   verifyFormat("operator const FooLeft<Object>*&&()", Style);
23132 
23133   // PR45107
23134   verifyFormat("operator Vector<String>&();", Style);
23135   verifyFormat("operator const Vector<String>&();", Style);
23136   verifyFormat("operator foo::Bar*();", Style);
23137   verifyFormat("operator const Foo<X>::Bar<Y>*();", Style);
23138   verifyFormat("operator/*a*/ const /*b*/ Foo /*c*/<X> /*d*/ ::Bar<Y>*();",
23139                Style);
23140 
23141   Style.PointerAlignment = FormatStyle::PAS_Middle;
23142   verifyFormat("Foo::operator*();", Style);
23143   verifyFormat("Foo::operator void *();", Style);
23144   verifyFormat("Foo::operator()(void *);", Style);
23145   verifyFormat("Foo::operator*(void *);", Style);
23146   verifyFormat("Foo::operator*();", Style);
23147   verifyFormat("operator*(int (*)(), class Foo);", Style);
23148 
23149   verifyFormat("Foo::operator&();", Style);
23150   verifyFormat("Foo::operator void &();", Style);
23151   verifyFormat("Foo::operator void const &();", Style);
23152   verifyFormat("Foo::operator()(void &);", Style);
23153   verifyFormat("Foo::operator&(void &);", Style);
23154   verifyFormat("Foo::operator&();", Style);
23155   verifyFormat("operator&(int (&)(), class Foo);", Style);
23156 
23157   verifyFormat("Foo::operator&&();", Style);
23158   verifyFormat("Foo::operator void &&();", Style);
23159   verifyFormat("Foo::operator void const &&();", Style);
23160   verifyFormat("Foo::operator()(void &&);", Style);
23161   verifyFormat("Foo::operator&&(void &&);", Style);
23162   verifyFormat("Foo::operator&&();", Style);
23163   verifyFormat("operator&&(int (&&)(), class Foo);", Style);
23164 }
23165 
23166 TEST_F(FormatTest, OperatorPassedAsAFunctionPtr) {
23167   FormatStyle Style = getLLVMStyle();
23168   // PR46157
23169   verifyFormat("foo(operator+, -42);", Style);
23170   verifyFormat("foo(operator++, -42);", Style);
23171   verifyFormat("foo(operator--, -42);", Style);
23172   verifyFormat("foo(-42, operator--);", Style);
23173   verifyFormat("foo(-42, operator, );", Style);
23174   verifyFormat("foo(operator, , -42);", Style);
23175 }
23176 
23177 TEST_F(FormatTest, WhitespaceSensitiveMacros) {
23178   FormatStyle Style = getLLVMStyle();
23179   Style.WhitespaceSensitiveMacros.push_back("FOO");
23180 
23181   // Don't use the helpers here, since 'mess up' will change the whitespace
23182   // and these are all whitespace sensitive by definition
23183   EXPECT_EQ("FOO(String-ized&Messy+But(: :Still)=Intentional);",
23184             format("FOO(String-ized&Messy+But(: :Still)=Intentional);", Style));
23185   EXPECT_EQ(
23186       "FOO(String-ized&Messy+But\\(: :Still)=Intentional);",
23187       format("FOO(String-ized&Messy+But\\(: :Still)=Intentional);", Style));
23188   EXPECT_EQ("FOO(String-ized&Messy+But,: :Still=Intentional);",
23189             format("FOO(String-ized&Messy+But,: :Still=Intentional);", Style));
23190   EXPECT_EQ("FOO(String-ized&Messy+But,: :\n"
23191             "       Still=Intentional);",
23192             format("FOO(String-ized&Messy+But,: :\n"
23193                    "       Still=Intentional);",
23194                    Style));
23195   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
23196   EXPECT_EQ("FOO(String-ized=&Messy+But,: :\n"
23197             "       Still=Intentional);",
23198             format("FOO(String-ized=&Messy+But,: :\n"
23199                    "       Still=Intentional);",
23200                    Style));
23201 
23202   Style.ColumnLimit = 21;
23203   EXPECT_EQ("FOO(String-ized&Messy+But: :Still=Intentional);",
23204             format("FOO(String-ized&Messy+But: :Still=Intentional);", Style));
23205 }
23206 
23207 TEST_F(FormatTest, VeryLongNamespaceCommentSplit) {
23208   // These tests are not in NamespaceFixer because that doesn't
23209   // test its interaction with line wrapping
23210   FormatStyle Style = getLLVMStyleWithColumns(80);
23211   verifyFormat("namespace {\n"
23212                "int i;\n"
23213                "int j;\n"
23214                "} // namespace",
23215                Style);
23216 
23217   verifyFormat("namespace AAA {\n"
23218                "int i;\n"
23219                "int j;\n"
23220                "} // namespace AAA",
23221                Style);
23222 
23223   EXPECT_EQ("namespace Averyveryveryverylongnamespace {\n"
23224             "int i;\n"
23225             "int j;\n"
23226             "} // namespace Averyveryveryverylongnamespace",
23227             format("namespace Averyveryveryverylongnamespace {\n"
23228                    "int i;\n"
23229                    "int j;\n"
23230                    "}",
23231                    Style));
23232 
23233   EXPECT_EQ(
23234       "namespace "
23235       "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n"
23236       "    went::mad::now {\n"
23237       "int i;\n"
23238       "int j;\n"
23239       "} // namespace\n"
23240       "  // "
23241       "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::"
23242       "went::mad::now",
23243       format("namespace "
23244              "would::it::save::you::a::lot::of::time::if_::i::"
23245              "just::gave::up::and_::went::mad::now {\n"
23246              "int i;\n"
23247              "int j;\n"
23248              "}",
23249              Style));
23250 
23251   // This used to duplicate the comment again and again on subsequent runs
23252   EXPECT_EQ(
23253       "namespace "
23254       "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n"
23255       "    went::mad::now {\n"
23256       "int i;\n"
23257       "int j;\n"
23258       "} // namespace\n"
23259       "  // "
23260       "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::"
23261       "went::mad::now",
23262       format("namespace "
23263              "would::it::save::you::a::lot::of::time::if_::i::"
23264              "just::gave::up::and_::went::mad::now {\n"
23265              "int i;\n"
23266              "int j;\n"
23267              "} // namespace\n"
23268              "  // "
23269              "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::"
23270              "and_::went::mad::now",
23271              Style));
23272 }
23273 
23274 TEST_F(FormatTest, LikelyUnlikely) {
23275   FormatStyle Style = getLLVMStyle();
23276 
23277   verifyFormat("if (argc > 5) [[unlikely]] {\n"
23278                "  return 29;\n"
23279                "}",
23280                Style);
23281 
23282   verifyFormat("if (argc > 5) [[likely]] {\n"
23283                "  return 29;\n"
23284                "}",
23285                Style);
23286 
23287   verifyFormat("if (argc > 5) [[unlikely]] {\n"
23288                "  return 29;\n"
23289                "} else [[likely]] {\n"
23290                "  return 42;\n"
23291                "}\n",
23292                Style);
23293 
23294   verifyFormat("if (argc > 5) [[unlikely]] {\n"
23295                "  return 29;\n"
23296                "} else if (argc > 10) [[likely]] {\n"
23297                "  return 99;\n"
23298                "} else {\n"
23299                "  return 42;\n"
23300                "}\n",
23301                Style);
23302 
23303   verifyFormat("if (argc > 5) [[gnu::unused]] {\n"
23304                "  return 29;\n"
23305                "}",
23306                Style);
23307 
23308   verifyFormat("if (argc > 5) [[unlikely]]\n"
23309                "  return 29;\n",
23310                Style);
23311   verifyFormat("if (argc > 5) [[likely]]\n"
23312                "  return 29;\n",
23313                Style);
23314 
23315   Style.AttributeMacros.push_back("UNLIKELY");
23316   Style.AttributeMacros.push_back("LIKELY");
23317   verifyFormat("if (argc > 5) UNLIKELY\n"
23318                "  return 29;\n",
23319                Style);
23320 
23321   verifyFormat("if (argc > 5) UNLIKELY {\n"
23322                "  return 29;\n"
23323                "}",
23324                Style);
23325   verifyFormat("if (argc > 5) UNLIKELY {\n"
23326                "  return 29;\n"
23327                "} else [[likely]] {\n"
23328                "  return 42;\n"
23329                "}\n",
23330                Style);
23331   verifyFormat("if (argc > 5) UNLIKELY {\n"
23332                "  return 29;\n"
23333                "} else LIKELY {\n"
23334                "  return 42;\n"
23335                "}\n",
23336                Style);
23337   verifyFormat("if (argc > 5) [[unlikely]] {\n"
23338                "  return 29;\n"
23339                "} else LIKELY {\n"
23340                "  return 42;\n"
23341                "}\n",
23342                Style);
23343 }
23344 
23345 TEST_F(FormatTest, PenaltyIndentedWhitespace) {
23346   verifyFormat("Constructor()\n"
23347                "    : aaaaaa(aaaaaa), aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
23348                "                          aaaa(aaaaaaaaaaaaaaaaaa, "
23349                "aaaaaaaaaaaaaaaaaat))");
23350   verifyFormat("Constructor()\n"
23351                "    : aaaaaaaaaaaaa(aaaaaa), "
23352                "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)");
23353 
23354   FormatStyle StyleWithWhitespacePenalty = getLLVMStyle();
23355   StyleWithWhitespacePenalty.PenaltyIndentedWhitespace = 5;
23356   verifyFormat("Constructor()\n"
23357                "    : aaaaaa(aaaaaa),\n"
23358                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
23359                "          aaaa(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaat))",
23360                StyleWithWhitespacePenalty);
23361   verifyFormat("Constructor()\n"
23362                "    : aaaaaaaaaaaaa(aaaaaa), "
23363                "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)",
23364                StyleWithWhitespacePenalty);
23365 }
23366 
23367 TEST_F(FormatTest, LLVMDefaultStyle) {
23368   FormatStyle Style = getLLVMStyle();
23369   verifyFormat("extern \"C\" {\n"
23370                "int foo();\n"
23371                "}",
23372                Style);
23373 }
23374 TEST_F(FormatTest, GNUDefaultStyle) {
23375   FormatStyle Style = getGNUStyle();
23376   verifyFormat("extern \"C\"\n"
23377                "{\n"
23378                "  int foo ();\n"
23379                "}",
23380                Style);
23381 }
23382 TEST_F(FormatTest, MozillaDefaultStyle) {
23383   FormatStyle Style = getMozillaStyle();
23384   verifyFormat("extern \"C\"\n"
23385                "{\n"
23386                "  int foo();\n"
23387                "}",
23388                Style);
23389 }
23390 TEST_F(FormatTest, GoogleDefaultStyle) {
23391   FormatStyle Style = getGoogleStyle();
23392   verifyFormat("extern \"C\" {\n"
23393                "int foo();\n"
23394                "}",
23395                Style);
23396 }
23397 TEST_F(FormatTest, ChromiumDefaultStyle) {
23398   FormatStyle Style = getChromiumStyle(FormatStyle::LanguageKind::LK_Cpp);
23399   verifyFormat("extern \"C\" {\n"
23400                "int foo();\n"
23401                "}",
23402                Style);
23403 }
23404 TEST_F(FormatTest, MicrosoftDefaultStyle) {
23405   FormatStyle Style = getMicrosoftStyle(FormatStyle::LanguageKind::LK_Cpp);
23406   verifyFormat("extern \"C\"\n"
23407                "{\n"
23408                "    int foo();\n"
23409                "}",
23410                Style);
23411 }
23412 TEST_F(FormatTest, WebKitDefaultStyle) {
23413   FormatStyle Style = getWebKitStyle();
23414   verifyFormat("extern \"C\" {\n"
23415                "int foo();\n"
23416                "}",
23417                Style);
23418 }
23419 
23420 TEST_F(FormatTest, Concepts) {
23421   EXPECT_EQ(getLLVMStyle().BreakBeforeConceptDeclarations,
23422             FormatStyle::BBCDS_Always);
23423   verifyFormat("template <typename T>\n"
23424                "concept True = true;");
23425 
23426   verifyFormat("template <typename T>\n"
23427                "concept C = ((false || foo()) && C2<T>) ||\n"
23428                "            (std::trait<T>::value && Baz) || sizeof(T) >= 6;",
23429                getLLVMStyleWithColumns(60));
23430 
23431   verifyFormat("template <typename T>\n"
23432                "concept DelayedCheck = true && requires(T t) { t.bar(); } && "
23433                "sizeof(T) <= 8;");
23434 
23435   verifyFormat("template <typename T>\n"
23436                "concept DelayedCheck = true && requires(T t) {\n"
23437                "                                 t.bar();\n"
23438                "                                 t.baz();\n"
23439                "                               } && sizeof(T) <= 8;");
23440 
23441   verifyFormat("template <typename T>\n"
23442                "concept DelayedCheck = true && requires(T t) { // Comment\n"
23443                "                                 t.bar();\n"
23444                "                                 t.baz();\n"
23445                "                               } && sizeof(T) <= 8;");
23446 
23447   verifyFormat("template <typename T>\n"
23448                "concept DelayedCheck = false || requires(T t) { t.bar(); } && "
23449                "sizeof(T) <= 8;");
23450 
23451   verifyFormat("template <typename T>\n"
23452                "concept DelayedCheck = !!false || requires(T t) { t.bar(); } "
23453                "&& sizeof(T) <= 8;");
23454 
23455   verifyFormat(
23456       "template <typename T>\n"
23457       "concept DelayedCheck = static_cast<bool>(0) ||\n"
23458       "                       requires(T t) { t.bar(); } && sizeof(T) <= 8;");
23459 
23460   verifyFormat("template <typename T>\n"
23461                "concept DelayedCheck = bool(0) || requires(T t) { t.bar(); } "
23462                "&& sizeof(T) <= 8;");
23463 
23464   verifyFormat(
23465       "template <typename T>\n"
23466       "concept DelayedCheck = (bool)(0) ||\n"
23467       "                       requires(T t) { t.bar(); } && sizeof(T) <= 8;");
23468 
23469   verifyFormat("template <typename T>\n"
23470                "concept DelayedCheck = (bool)0 || requires(T t) { t.bar(); } "
23471                "&& sizeof(T) <= 8;");
23472 
23473   verifyFormat("template <typename T>\n"
23474                "concept Size = sizeof(T) >= 5 && requires(T t) { t.bar(); } && "
23475                "sizeof(T) <= 8;");
23476 
23477   verifyFormat("template <typename T>\n"
23478                "concept Size = 2 < 5 && 2 <= 5 && 8 >= 5 && 8 > 5 &&\n"
23479                "               requires(T t) {\n"
23480                "                 t.bar();\n"
23481                "                 t.baz();\n"
23482                "               } && sizeof(T) <= 8 && !(4 < 3);",
23483                getLLVMStyleWithColumns(60));
23484 
23485   verifyFormat("template <typename T>\n"
23486                "concept TrueOrNot = IsAlwaysTrue || IsNeverTrue;");
23487 
23488   verifyFormat("template <typename T>\n"
23489                "concept C = foo();");
23490 
23491   verifyFormat("template <typename T>\n"
23492                "concept C = foo(T());");
23493 
23494   verifyFormat("template <typename T>\n"
23495                "concept C = foo(T{});");
23496 
23497   verifyFormat("template <typename T>\n"
23498                "concept Size = V<sizeof(T)>::Value > 5;");
23499 
23500   verifyFormat("template <typename T>\n"
23501                "concept True = S<T>::Value;");
23502 
23503   verifyFormat(
23504       "template <typename T>\n"
23505       "concept C = []() { return true; }() && requires(T t) { t.bar(); } &&\n"
23506       "            sizeof(T) <= 8;");
23507 
23508   // FIXME: This is misformatted because the fake l paren starts at bool, not at
23509   // the lambda l square.
23510   verifyFormat("template <typename T>\n"
23511                "concept C = [] -> bool { return true; }() && requires(T t) { "
23512                "t.bar(); } &&\n"
23513                "                      sizeof(T) <= 8;");
23514 
23515   verifyFormat(
23516       "template <typename T>\n"
23517       "concept C = decltype([]() { return std::true_type{}; }())::value &&\n"
23518       "            requires(T t) { t.bar(); } && sizeof(T) <= 8;");
23519 
23520   verifyFormat("template <typename T>\n"
23521                "concept C = decltype([]() { return std::true_type{}; "
23522                "}())::value && requires(T t) { t.bar(); } && sizeof(T) <= 8;",
23523                getLLVMStyleWithColumns(120));
23524 
23525   verifyFormat("template <typename T>\n"
23526                "concept C = decltype([]() -> std::true_type { return {}; "
23527                "}())::value &&\n"
23528                "            requires(T t) { t.bar(); } && sizeof(T) <= 8;");
23529 
23530   verifyFormat("template <typename T>\n"
23531                "concept C = true;\n"
23532                "Foo Bar;");
23533 
23534   verifyFormat("template <typename T>\n"
23535                "concept Hashable = requires(T a) {\n"
23536                "                     { std::hash<T>{}(a) } -> "
23537                "std::convertible_to<std::size_t>;\n"
23538                "                   };");
23539 
23540   verifyFormat(
23541       "template <typename T>\n"
23542       "concept EqualityComparable = requires(T a, T b) {\n"
23543       "                               { a == b } -> std::same_as<bool>;\n"
23544       "                             };");
23545 
23546   verifyFormat(
23547       "template <typename T>\n"
23548       "concept EqualityComparable = requires(T a, T b) {\n"
23549       "                               { a == b } -> std::same_as<bool>;\n"
23550       "                               { a != b } -> std::same_as<bool>;\n"
23551       "                             };");
23552 
23553   verifyFormat("template <typename T>\n"
23554                "concept WeakEqualityComparable = requires(T a, T b) {\n"
23555                "                                   { a == b };\n"
23556                "                                   { a != b };\n"
23557                "                                 };");
23558 
23559   verifyFormat("template <typename T>\n"
23560                "concept HasSizeT = requires { typename T::size_t; };");
23561 
23562   verifyFormat("template <typename T>\n"
23563                "concept Semiregular =\n"
23564                "    DefaultConstructible<T> && CopyConstructible<T> && "
23565                "CopyAssignable<T> &&\n"
23566                "    requires(T a, std::size_t n) {\n"
23567                "      requires Same<T *, decltype(&a)>;\n"
23568                "      { a.~T() } noexcept;\n"
23569                "      requires Same<T *, decltype(new T)>;\n"
23570                "      requires Same<T *, decltype(new T[n])>;\n"
23571                "      { delete new T; };\n"
23572                "      { delete new T[n]; };\n"
23573                "    };");
23574 
23575   verifyFormat("template <typename T>\n"
23576                "concept Semiregular =\n"
23577                "    requires(T a, std::size_t n) {\n"
23578                "      requires Same<T *, decltype(&a)>;\n"
23579                "      { a.~T() } noexcept;\n"
23580                "      requires Same<T *, decltype(new T)>;\n"
23581                "      requires Same<T *, decltype(new T[n])>;\n"
23582                "      { delete new T; };\n"
23583                "      { delete new T[n]; };\n"
23584                "      { new T } -> std::same_as<T *>;\n"
23585                "    } && DefaultConstructible<T> && CopyConstructible<T> && "
23586                "CopyAssignable<T>;");
23587 
23588   verifyFormat(
23589       "template <typename T>\n"
23590       "concept Semiregular =\n"
23591       "    DefaultConstructible<T> && requires(T a, std::size_t n) {\n"
23592       "                                 requires Same<T *, decltype(&a)>;\n"
23593       "                                 { a.~T() } noexcept;\n"
23594       "                                 requires Same<T *, decltype(new T)>;\n"
23595       "                                 requires Same<T *, decltype(new "
23596       "T[n])>;\n"
23597       "                                 { delete new T; };\n"
23598       "                                 { delete new T[n]; };\n"
23599       "                               } && CopyConstructible<T> && "
23600       "CopyAssignable<T>;");
23601 
23602   verifyFormat("template <typename T>\n"
23603                "concept Two = requires(T t) {\n"
23604                "                { t.foo() } -> std::same_as<Bar>;\n"
23605                "              } && requires(T &&t) {\n"
23606                "                     { t.foo() } -> std::same_as<Bar &&>;\n"
23607                "                   };");
23608 
23609   verifyFormat(
23610       "template <typename T>\n"
23611       "concept C = requires(T x) {\n"
23612       "              { *x } -> std::convertible_to<typename T::inner>;\n"
23613       "              { x + 1 } noexcept -> std::same_as<int>;\n"
23614       "              { x * 1 } -> std::convertible_to<T>;\n"
23615       "            };");
23616 
23617   verifyFormat(
23618       "template <typename T, typename U = T>\n"
23619       "concept Swappable = requires(T &&t, U &&u) {\n"
23620       "                      swap(std::forward<T>(t), std::forward<U>(u));\n"
23621       "                      swap(std::forward<U>(u), std::forward<T>(t));\n"
23622       "                    };");
23623 
23624   verifyFormat("template <typename T, typename U>\n"
23625                "concept Common = requires(T &&t, U &&u) {\n"
23626                "                   typename CommonType<T, U>;\n"
23627                "                   { CommonType<T, U>(std::forward<T>(t)) };\n"
23628                "                 };");
23629 
23630   verifyFormat("template <typename T, typename U>\n"
23631                "concept Common = requires(T &&t, U &&u) {\n"
23632                "                   typename CommonType<T, U>;\n"
23633                "                   { CommonType<T, U>{std::forward<T>(t)} };\n"
23634                "                 };");
23635 
23636   verifyFormat(
23637       "template <typename T>\n"
23638       "concept C = requires(T t) {\n"
23639       "              requires Bar<T> && Foo<T>;\n"
23640       "              requires((trait<T> && Baz) || (T2<T> && Foo<T>));\n"
23641       "            };");
23642 
23643   verifyFormat("template <typename T>\n"
23644                "concept HasFoo = requires(T t) {\n"
23645                "                   { t.foo() };\n"
23646                "                   t.foo();\n"
23647                "                 };\n"
23648                "template <typename T>\n"
23649                "concept HasBar = requires(T t) {\n"
23650                "                   { t.bar() };\n"
23651                "                   t.bar();\n"
23652                "                 };");
23653 
23654   verifyFormat("template <typename T>\n"
23655                "concept Large = sizeof(T) > 10;");
23656 
23657   verifyFormat("template <typename T, typename U>\n"
23658                "concept FooableWith = requires(T t, U u) {\n"
23659                "                        typename T::foo_type;\n"
23660                "                        { t.foo(u) } -> typename T::foo_type;\n"
23661                "                        t++;\n"
23662                "                      };\n"
23663                "void doFoo(FooableWith<int> auto t) { t.foo(3); }");
23664 
23665   verifyFormat("template <typename T>\n"
23666                "concept Context = is_specialization_of_v<context, T>;");
23667 
23668   verifyFormat("template <typename T>\n"
23669                "concept Node = std::is_object_v<T>;");
23670 
23671   auto Style = getLLVMStyle();
23672   Style.BreakBeforeConceptDeclarations = FormatStyle::BBCDS_Allowed;
23673 
23674   verifyFormat(
23675       "template <typename T>\n"
23676       "concept C = requires(T t) {\n"
23677       "              requires Bar<T> && Foo<T>;\n"
23678       "              requires((trait<T> && Baz) || (T2<T> && Foo<T>));\n"
23679       "            };",
23680       Style);
23681 
23682   verifyFormat("template <typename T>\n"
23683                "concept HasFoo = requires(T t) {\n"
23684                "                   { t.foo() };\n"
23685                "                   t.foo();\n"
23686                "                 };\n"
23687                "template <typename T>\n"
23688                "concept HasBar = requires(T t) {\n"
23689                "                   { t.bar() };\n"
23690                "                   t.bar();\n"
23691                "                 };",
23692                Style);
23693 
23694   verifyFormat("template <typename T> concept True = true;", Style);
23695 
23696   verifyFormat("template <typename T>\n"
23697                "concept C = decltype([]() -> std::true_type { return {}; "
23698                "}())::value &&\n"
23699                "            requires(T t) { t.bar(); } && sizeof(T) <= 8;",
23700                Style);
23701 
23702   verifyFormat("template <typename T>\n"
23703                "concept Semiregular =\n"
23704                "    DefaultConstructible<T> && CopyConstructible<T> && "
23705                "CopyAssignable<T> &&\n"
23706                "    requires(T a, std::size_t n) {\n"
23707                "      requires Same<T *, decltype(&a)>;\n"
23708                "      { a.~T() } noexcept;\n"
23709                "      requires Same<T *, decltype(new T)>;\n"
23710                "      requires Same<T *, decltype(new T[n])>;\n"
23711                "      { delete new T; };\n"
23712                "      { delete new T[n]; };\n"
23713                "    };",
23714                Style);
23715 
23716   Style.BreakBeforeConceptDeclarations = FormatStyle::BBCDS_Never;
23717 
23718   verifyFormat("template <typename T> concept C =\n"
23719                "    requires(T t) {\n"
23720                "      requires Bar<T> && Foo<T>;\n"
23721                "      requires((trait<T> && Baz) || (T2<T> && Foo<T>));\n"
23722                "    };",
23723                Style);
23724 
23725   verifyFormat("template <typename T> concept HasFoo = requires(T t) {\n"
23726                "                                         { t.foo() };\n"
23727                "                                         t.foo();\n"
23728                "                                       };\n"
23729                "template <typename T> concept HasBar = requires(T t) {\n"
23730                "                                         { t.bar() };\n"
23731                "                                         t.bar();\n"
23732                "                                       };",
23733                Style);
23734 
23735   verifyFormat("template <typename T> concept True = true;", Style);
23736 
23737   verifyFormat(
23738       "template <typename T> concept C = decltype([]() -> std::true_type {\n"
23739       "                                    return {};\n"
23740       "                                  }())::value\n"
23741       "                                  && requires(T t) { t.bar(); } &&\n"
23742       "                                  sizeof(T) <= 8;",
23743       Style);
23744 
23745   verifyFormat("template <typename T> concept Semiregular =\n"
23746                "    DefaultConstructible<T> && CopyConstructible<T> && "
23747                "CopyAssignable<T> &&\n"
23748                "    requires(T a, std::size_t n) {\n"
23749                "      requires Same<T *, decltype(&a)>;\n"
23750                "      { a.~T() } noexcept;\n"
23751                "      requires Same<T *, decltype(new T)>;\n"
23752                "      requires Same<T *, decltype(new T[n])>;\n"
23753                "      { delete new T; };\n"
23754                "      { delete new T[n]; };\n"
23755                "    };",
23756                Style);
23757 
23758   // The following tests are invalid C++, we just want to make sure we don't
23759   // assert.
23760   verifyFormat("template <typename T>\n"
23761                "concept C = requires C2<T>;");
23762 
23763   verifyFormat("template <typename T>\n"
23764                "concept C = 5 + 4;");
23765 
23766   verifyFormat("template <typename T>\n"
23767                "concept C =\n"
23768                "class X;");
23769 
23770   verifyFormat("template <typename T>\n"
23771                "concept C = [] && true;");
23772 
23773   verifyFormat("template <typename T>\n"
23774                "concept C = [] && requires(T t) { typename T::size_type; };");
23775 }
23776 
23777 TEST_F(FormatTest, RequiresClauses) {
23778   auto Style = getLLVMStyle();
23779   EXPECT_EQ(Style.RequiresClausePosition, FormatStyle::RCPS_OwnLine);
23780   EXPECT_EQ(Style.IndentRequiresClause, true);
23781 
23782   verifyFormat("template <typename T>\n"
23783                "  requires(Foo<T> && std::trait<T>)\n"
23784                "struct Bar;",
23785                Style);
23786 
23787   verifyFormat("template <typename T>\n"
23788                "  requires(Foo<T> && std::trait<T>)\n"
23789                "class Bar {\n"
23790                "public:\n"
23791                "  Bar(T t);\n"
23792                "  bool baz();\n"
23793                "};",
23794                Style);
23795 
23796   verifyFormat(
23797       "template <typename T>\n"
23798       "  requires requires(T &&t) {\n"
23799       "             typename T::I;\n"
23800       "             requires(F<typename T::I> && std::trait<typename T::I>);\n"
23801       "           }\n"
23802       "Bar(T) -> Bar<typename T::I>;",
23803       Style);
23804 
23805   verifyFormat("template <typename T>\n"
23806                "  requires(Foo<T> && std::trait<T>)\n"
23807                "constexpr T MyGlobal;",
23808                Style);
23809 
23810   verifyFormat("template <typename T>\n"
23811                "  requires Foo<T> && requires(T t) {\n"
23812                "                       { t.baz() } -> std::same_as<bool>;\n"
23813                "                       requires std::same_as<T::Factor, int>;\n"
23814                "                     }\n"
23815                "inline int bar(T t) {\n"
23816                "  return t.baz() ? T::Factor : 5;\n"
23817                "}",
23818                Style);
23819 
23820   verifyFormat("template <typename T>\n"
23821                "inline int bar(T t)\n"
23822                "  requires Foo<T> && requires(T t) {\n"
23823                "                       { t.baz() } -> std::same_as<bool>;\n"
23824                "                       requires std::same_as<T::Factor, int>;\n"
23825                "                     }\n"
23826                "{\n"
23827                "  return t.baz() ? T::Factor : 5;\n"
23828                "}",
23829                Style);
23830 
23831   verifyFormat("template <typename T>\n"
23832                "  requires F<T>\n"
23833                "int bar(T t) {\n"
23834                "  return 5;\n"
23835                "}",
23836                Style);
23837 
23838   verifyFormat("template <typename T>\n"
23839                "int bar(T t)\n"
23840                "  requires F<T>\n"
23841                "{\n"
23842                "  return 5;\n"
23843                "}",
23844                Style);
23845 
23846   Style.IndentRequiresClause = false;
23847   verifyFormat("template <typename T>\n"
23848                "requires F<T>\n"
23849                "int bar(T t) {\n"
23850                "  return 5;\n"
23851                "}",
23852                Style);
23853 
23854   verifyFormat("template <typename T>\n"
23855                "int bar(T t)\n"
23856                "requires F<T>\n"
23857                "{\n"
23858                "  return 5;\n"
23859                "}",
23860                Style);
23861 
23862   Style.RequiresClausePosition = FormatStyle::RCPS_SingleLine;
23863   verifyFormat("template <typename T> requires Foo<T> struct Bar {};\n"
23864                "template <typename T> requires Foo<T> void bar() {}\n"
23865                "template <typename T> void bar() requires Foo<T> {}\n"
23866                "template <typename T> requires Foo<T> Bar(T) -> Bar<T>;",
23867                Style);
23868 
23869   auto ColumnStyle = Style;
23870   ColumnStyle.ColumnLimit = 40;
23871   verifyFormat("template <typename AAAAAAA>\n"
23872                "requires Foo<T> struct Bar {};\n"
23873                "template <typename AAAAAAA>\n"
23874                "requires Foo<T> void bar() {}\n"
23875                "template <typename AAAAAAA>\n"
23876                "void bar() requires Foo<T> {}\n"
23877                "template <typename AAAAAAA>\n"
23878                "requires Foo<T> Baz(T) -> Baz<T>;",
23879                ColumnStyle);
23880 
23881   verifyFormat("template <typename T>\n"
23882                "requires Foo<AAAAAAA> struct Bar {};\n"
23883                "template <typename T>\n"
23884                "requires Foo<AAAAAAA> void bar() {}\n"
23885                "template <typename T>\n"
23886                "void bar() requires Foo<AAAAAAA> {}\n"
23887                "template <typename T>\n"
23888                "requires Foo<AAAAAAA> Bar(T) -> Bar<T>;",
23889                ColumnStyle);
23890 
23891   verifyFormat("template <typename AAAAAAA>\n"
23892                "requires Foo<AAAAAAAAAAAAAAAA>\n"
23893                "struct Bar {};\n"
23894                "template <typename AAAAAAA>\n"
23895                "requires Foo<AAAAAAAAAAAAAAAA>\n"
23896                "void bar() {}\n"
23897                "template <typename AAAAAAA>\n"
23898                "void bar()\n"
23899                "    requires Foo<AAAAAAAAAAAAAAAA> {}\n"
23900                "template <typename AAAAAAA>\n"
23901                "requires Foo<AAAAAAAA> Bar(T) -> Bar<T>;\n"
23902                "template <typename AAAAAAA>\n"
23903                "requires Foo<AAAAAAAAAAAAAAAA>\n"
23904                "Bar(T) -> Bar<T>;",
23905                ColumnStyle);
23906 
23907   Style.RequiresClausePosition = FormatStyle::RCPS_WithFollowing;
23908   ColumnStyle.RequiresClausePosition = FormatStyle::RCPS_WithFollowing;
23909 
23910   verifyFormat("template <typename T>\n"
23911                "requires Foo<T> struct Bar {};\n"
23912                "template <typename T>\n"
23913                "requires Foo<T> void bar() {}\n"
23914                "template <typename T>\n"
23915                "void bar()\n"
23916                "requires Foo<T> {}\n"
23917                "template <typename T>\n"
23918                "requires Foo<T> Bar(T) -> Bar<T>;",
23919                Style);
23920 
23921   verifyFormat("template <typename AAAAAAA>\n"
23922                "requires Foo<AAAAAAAAAAAAAAAA>\n"
23923                "struct Bar {};\n"
23924                "template <typename AAAAAAA>\n"
23925                "requires Foo<AAAAAAAAAAAAAAAA>\n"
23926                "void bar() {}\n"
23927                "template <typename AAAAAAA>\n"
23928                "void bar()\n"
23929                "requires Foo<AAAAAAAAAAAAAAAA> {}\n"
23930                "template <typename AAAAAAA>\n"
23931                "requires Foo<AAAAAAAA> Bar(T) -> Bar<T>;\n"
23932                "template <typename AAAAAAA>\n"
23933                "requires Foo<AAAAAAAAAAAAAAAA>\n"
23934                "Bar(T) -> Bar<T>;",
23935                ColumnStyle);
23936 
23937   Style.IndentRequiresClause = true;
23938   ColumnStyle.IndentRequiresClause = true;
23939 
23940   verifyFormat("template <typename T>\n"
23941                "  requires Foo<T> struct Bar {};\n"
23942                "template <typename T>\n"
23943                "  requires Foo<T> void bar() {}\n"
23944                "template <typename T>\n"
23945                "void bar()\n"
23946                "  requires Foo<T> {}\n"
23947                "template <typename T>\n"
23948                "  requires Foo<T> Bar(T) -> Bar<T>;",
23949                Style);
23950 
23951   verifyFormat("template <typename AAAAAAA>\n"
23952                "  requires Foo<AAAAAAAAAAAAAAAA>\n"
23953                "struct Bar {};\n"
23954                "template <typename AAAAAAA>\n"
23955                "  requires Foo<AAAAAAAAAAAAAAAA>\n"
23956                "void bar() {}\n"
23957                "template <typename AAAAAAA>\n"
23958                "void bar()\n"
23959                "  requires Foo<AAAAAAAAAAAAAAAA> {}\n"
23960                "template <typename AAAAAAA>\n"
23961                "  requires Foo<AAAAAA> Bar(T) -> Bar<T>;\n"
23962                "template <typename AAAAAAA>\n"
23963                "  requires Foo<AAAAAAAAAAAAAAAA>\n"
23964                "Bar(T) -> Bar<T>;",
23965                ColumnStyle);
23966 
23967   Style.RequiresClausePosition = FormatStyle::RCPS_WithPreceding;
23968   ColumnStyle.RequiresClausePosition = FormatStyle::RCPS_WithPreceding;
23969 
23970   verifyFormat("template <typename T> requires Foo<T>\n"
23971                "struct Bar {};\n"
23972                "template <typename T> requires Foo<T>\n"
23973                "void bar() {}\n"
23974                "template <typename T>\n"
23975                "void bar() requires Foo<T>\n"
23976                "{}\n"
23977                "template <typename T> requires Foo<T>\n"
23978                "Bar(T) -> Bar<T>;",
23979                Style);
23980 
23981   verifyFormat("template <typename AAAAAAA>\n"
23982                "requires Foo<AAAAAAAAAAAAAAAA>\n"
23983                "struct Bar {};\n"
23984                "template <typename AAAAAAA>\n"
23985                "requires Foo<AAAAAAAAAAAAAAAA>\n"
23986                "void bar() {}\n"
23987                "template <typename AAAAAAA>\n"
23988                "void bar()\n"
23989                "    requires Foo<AAAAAAAAAAAAAAAA>\n"
23990                "{}\n"
23991                "template <typename AAAAAAA>\n"
23992                "requires Foo<AAAAAAAA>\n"
23993                "Bar(T) -> Bar<T>;\n"
23994                "template <typename AAAAAAA>\n"
23995                "requires Foo<AAAAAAAAAAAAAAAA>\n"
23996                "Bar(T) -> Bar<T>;",
23997                ColumnStyle);
23998 }
23999 
24000 TEST_F(FormatTest, StatementAttributeLikeMacros) {
24001   FormatStyle Style = getLLVMStyle();
24002   StringRef Source = "void Foo::slot() {\n"
24003                      "  unsigned char MyChar = 'x';\n"
24004                      "  emit signal(MyChar);\n"
24005                      "  Q_EMIT signal(MyChar);\n"
24006                      "}";
24007 
24008   EXPECT_EQ(Source, format(Source, Style));
24009 
24010   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
24011   EXPECT_EQ("void Foo::slot() {\n"
24012             "  unsigned char MyChar = 'x';\n"
24013             "  emit          signal(MyChar);\n"
24014             "  Q_EMIT signal(MyChar);\n"
24015             "}",
24016             format(Source, Style));
24017 
24018   Style.StatementAttributeLikeMacros.push_back("emit");
24019   EXPECT_EQ(Source, format(Source, Style));
24020 
24021   Style.StatementAttributeLikeMacros = {};
24022   EXPECT_EQ("void Foo::slot() {\n"
24023             "  unsigned char MyChar = 'x';\n"
24024             "  emit          signal(MyChar);\n"
24025             "  Q_EMIT        signal(MyChar);\n"
24026             "}",
24027             format(Source, Style));
24028 }
24029 
24030 TEST_F(FormatTest, IndentAccessModifiers) {
24031   FormatStyle Style = getLLVMStyle();
24032   Style.IndentAccessModifiers = true;
24033   // Members are *two* levels below the record;
24034   // Style.IndentWidth == 2, thus yielding a 4 spaces wide indentation.
24035   verifyFormat("class C {\n"
24036                "    int i;\n"
24037                "};\n",
24038                Style);
24039   verifyFormat("union C {\n"
24040                "    int i;\n"
24041                "    unsigned u;\n"
24042                "};\n",
24043                Style);
24044   // Access modifiers should be indented one level below the record.
24045   verifyFormat("class C {\n"
24046                "  public:\n"
24047                "    int i;\n"
24048                "};\n",
24049                Style);
24050   verifyFormat("struct S {\n"
24051                "  private:\n"
24052                "    class C {\n"
24053                "        int j;\n"
24054                "\n"
24055                "      public:\n"
24056                "        C();\n"
24057                "    };\n"
24058                "\n"
24059                "  public:\n"
24060                "    int i;\n"
24061                "};\n",
24062                Style);
24063   // Enumerations are not records and should be unaffected.
24064   Style.AllowShortEnumsOnASingleLine = false;
24065   verifyFormat("enum class E {\n"
24066                "  A,\n"
24067                "  B\n"
24068                "};\n",
24069                Style);
24070   // Test with a different indentation width;
24071   // also proves that the result is Style.AccessModifierOffset agnostic.
24072   Style.IndentWidth = 3;
24073   verifyFormat("class C {\n"
24074                "   public:\n"
24075                "      int i;\n"
24076                "};\n",
24077                Style);
24078 }
24079 
24080 TEST_F(FormatTest, LimitlessStringsAndComments) {
24081   auto Style = getLLVMStyleWithColumns(0);
24082   constexpr StringRef Code =
24083       "/**\n"
24084       " * This is a multiline comment with quite some long lines, at least for "
24085       "the LLVM Style.\n"
24086       " * We will redo this with strings and line comments. Just to  check if "
24087       "everything is working.\n"
24088       " */\n"
24089       "bool foo() {\n"
24090       "  /* Single line multi line comment. */\n"
24091       "  const std::string String = \"This is a multiline string with quite "
24092       "some long lines, at least for the LLVM Style.\"\n"
24093       "                             \"We already did it with multi line "
24094       "comments, and we will do it with line comments. Just to check if "
24095       "everything is working.\";\n"
24096       "  // This is a line comment (block) with quite some long lines, at "
24097       "least for the LLVM Style.\n"
24098       "  // We already did this with multi line comments and strings. Just to "
24099       "check if everything is working.\n"
24100       "  const std::string SmallString = \"Hello World\";\n"
24101       "  // Small line comment\n"
24102       "  return String.size() > SmallString.size();\n"
24103       "}";
24104   EXPECT_EQ(Code, format(Code, Style));
24105 }
24106 
24107 TEST_F(FormatTest, FormatDecayCopy) {
24108   // error cases from unit tests
24109   verifyFormat("foo(auto())");
24110   verifyFormat("foo(auto{})");
24111   verifyFormat("foo(auto({}))");
24112   verifyFormat("foo(auto{{}})");
24113 
24114   verifyFormat("foo(auto(1))");
24115   verifyFormat("foo(auto{1})");
24116   verifyFormat("foo(new auto(1))");
24117   verifyFormat("foo(new auto{1})");
24118   verifyFormat("decltype(auto(1)) x;");
24119   verifyFormat("decltype(auto{1}) x;");
24120   verifyFormat("auto(x);");
24121   verifyFormat("auto{x};");
24122   verifyFormat("new auto{x};");
24123   verifyFormat("auto{x} = y;");
24124   verifyFormat("auto(x) = y;"); // actually a declaration, but this is clearly
24125                                 // the user's own fault
24126   verifyFormat("integral auto(x) = y;"); // actually a declaration, but this is
24127                                          // clearly the user's own fault
24128   verifyFormat("auto(*p)() = f;");       // actually a declaration; TODO FIXME
24129 }
24130 
24131 TEST_F(FormatTest, Cpp20ModulesSupport) {
24132   FormatStyle Style = getLLVMStyle();
24133   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
24134   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
24135 
24136   verifyFormat("export import foo;", Style);
24137   verifyFormat("export import foo:bar;", Style);
24138   verifyFormat("export import foo.bar;", Style);
24139   verifyFormat("export import foo.bar:baz;", Style);
24140   verifyFormat("export import :bar;", Style);
24141   verifyFormat("export module foo:bar;", Style);
24142   verifyFormat("export module foo;", Style);
24143   verifyFormat("export module foo.bar;", Style);
24144   verifyFormat("export module foo.bar:baz;", Style);
24145   verifyFormat("export import <string_view>;", Style);
24146 
24147   verifyFormat("export type_name var;", Style);
24148   verifyFormat("template <class T> export using A = B<T>;", Style);
24149   verifyFormat("export using A = B;", Style);
24150   verifyFormat("export int func() {\n"
24151                "  foo();\n"
24152                "}",
24153                Style);
24154   verifyFormat("export struct {\n"
24155                "  int foo;\n"
24156                "};",
24157                Style);
24158   verifyFormat("export {\n"
24159                "  int foo;\n"
24160                "};",
24161                Style);
24162   verifyFormat("export export char const *hello() { return \"hello\"; }");
24163 
24164   verifyFormat("import bar;", Style);
24165   verifyFormat("import foo.bar;", Style);
24166   verifyFormat("import foo:bar;", Style);
24167   verifyFormat("import :bar;", Style);
24168   verifyFormat("import <ctime>;", Style);
24169   verifyFormat("import \"header\";", Style);
24170 
24171   verifyFormat("module foo;", Style);
24172   verifyFormat("module foo:bar;", Style);
24173   verifyFormat("module foo.bar;", Style);
24174   verifyFormat("module;", Style);
24175 
24176   verifyFormat("export namespace hi {\n"
24177                "const char *sayhi();\n"
24178                "}",
24179                Style);
24180 
24181   verifyFormat("module :private;", Style);
24182   verifyFormat("import <foo/bar.h>;", Style);
24183   verifyFormat("import foo...bar;", Style);
24184   verifyFormat("import ..........;", Style);
24185   verifyFormat("module foo:private;", Style);
24186   verifyFormat("import a", Style);
24187   verifyFormat("module a", Style);
24188   verifyFormat("export import a", Style);
24189   verifyFormat("export module a", Style);
24190 
24191   verifyFormat("import", Style);
24192   verifyFormat("module", Style);
24193   verifyFormat("export", Style);
24194 }
24195 
24196 TEST_F(FormatTest, CoroutineForCoawait) {
24197   FormatStyle Style = getLLVMStyle();
24198   verifyFormat("for co_await (auto x : range())\n  ;");
24199   verifyFormat("for (auto i : arr) {\n"
24200                "}",
24201                Style);
24202   verifyFormat("for co_await (auto i : arr) {\n"
24203                "}",
24204                Style);
24205   verifyFormat("for co_await (auto i : foo(T{})) {\n"
24206                "}",
24207                Style);
24208 }
24209 
24210 TEST_F(FormatTest, CoroutineCoAwait) {
24211   verifyFormat("int x = co_await foo();");
24212   verifyFormat("int x = (co_await foo());");
24213   verifyFormat("co_await (42);");
24214   verifyFormat("void operator co_await(int);");
24215   verifyFormat("void operator co_await(a);");
24216   verifyFormat("co_await a;");
24217   verifyFormat("co_await missing_await_resume{};");
24218   verifyFormat("co_await a; // comment");
24219   verifyFormat("void test0() { co_await a; }");
24220   verifyFormat("co_await co_await co_await foo();");
24221   verifyFormat("co_await foo().bar();");
24222   verifyFormat("co_await [this]() -> Task { co_return x; }");
24223   verifyFormat("co_await [this](int a, int b) -> Task { co_return co_await "
24224                "foo(); }(x, y);");
24225 
24226   FormatStyle Style = getLLVMStyleWithColumns(40);
24227   verifyFormat("co_await [this](int a, int b) -> Task {\n"
24228                "  co_return co_await foo();\n"
24229                "}(x, y);",
24230                Style);
24231   verifyFormat("co_await;");
24232 }
24233 
24234 TEST_F(FormatTest, CoroutineCoYield) {
24235   verifyFormat("int x = co_yield foo();");
24236   verifyFormat("int x = (co_yield foo());");
24237   verifyFormat("co_yield (42);");
24238   verifyFormat("co_yield {42};");
24239   verifyFormat("co_yield 42;");
24240   verifyFormat("co_yield n++;");
24241   verifyFormat("co_yield ++n;");
24242   verifyFormat("co_yield;");
24243 }
24244 
24245 TEST_F(FormatTest, CoroutineCoReturn) {
24246   verifyFormat("co_return (42);");
24247   verifyFormat("co_return;");
24248   verifyFormat("co_return {};");
24249   verifyFormat("co_return x;");
24250   verifyFormat("co_return co_await foo();");
24251   verifyFormat("co_return co_yield foo();");
24252 }
24253 
24254 TEST_F(FormatTest, EmptyShortBlock) {
24255   auto Style = getLLVMStyle();
24256   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty;
24257 
24258   verifyFormat("try {\n"
24259                "  doA();\n"
24260                "} catch (Exception &e) {\n"
24261                "  e.printStackTrace();\n"
24262                "}\n",
24263                Style);
24264 
24265   verifyFormat("try {\n"
24266                "  doA();\n"
24267                "} catch (Exception &e) {}\n",
24268                Style);
24269 }
24270 
24271 TEST_F(FormatTest, ShortTemplatedArgumentLists) {
24272   auto Style = getLLVMStyle();
24273 
24274   verifyFormat("template <> struct S : Template<int (*)[]> {};\n", Style);
24275   verifyFormat("template <> struct S : Template<int (*)[10]> {};\n", Style);
24276   verifyFormat("struct Y : X<[] { return 0; }> {};", Style);
24277   verifyFormat("struct Y<[] { return 0; }> {};", Style);
24278 
24279   verifyFormat("struct Z : X<decltype([] { return 0; }){}> {};", Style);
24280   verifyFormat("template <int N> struct Foo<char[N]> {};", Style);
24281 }
24282 
24283 TEST_F(FormatTest, RemoveBraces) {
24284   FormatStyle Style = getLLVMStyle();
24285   Style.RemoveBracesLLVM = true;
24286 
24287   // The following eight test cases are fully-braced versions of the examples at
24288   // "llvm.org/docs/CodingStandards.html#don-t-use-braces-on-simple-single-
24289   // statement-bodies-of-if-else-loop-statements".
24290 
24291   // 1. Omit the braces, since the body is simple and clearly associated with
24292   // the if.
24293   verifyFormat("if (isa<FunctionDecl>(D))\n"
24294                "  handleFunctionDecl(D);\n"
24295                "else if (isa<VarDecl>(D))\n"
24296                "  handleVarDecl(D);",
24297                "if (isa<FunctionDecl>(D)) {\n"
24298                "  handleFunctionDecl(D);\n"
24299                "} else if (isa<VarDecl>(D)) {\n"
24300                "  handleVarDecl(D);\n"
24301                "}",
24302                Style);
24303 
24304   // 2. Here we document the condition itself and not the body.
24305   verifyFormat("if (isa<VarDecl>(D)) {\n"
24306                "  // It is necessary that we explain the situation with this\n"
24307                "  // surprisingly long comment, so it would be unclear\n"
24308                "  // without the braces whether the following statement is in\n"
24309                "  // the scope of the `if`.\n"
24310                "  // Because the condition is documented, we can't really\n"
24311                "  // hoist this comment that applies to the body above the\n"
24312                "  // if.\n"
24313                "  handleOtherDecl(D);\n"
24314                "}",
24315                Style);
24316 
24317   // 3. Use braces on the outer `if` to avoid a potential dangling else
24318   // situation.
24319   verifyFormat("if (isa<VarDecl>(D)) {\n"
24320                "  for (auto *A : D.attrs())\n"
24321                "    if (shouldProcessAttr(A))\n"
24322                "      handleAttr(A);\n"
24323                "}",
24324                "if (isa<VarDecl>(D)) {\n"
24325                "  for (auto *A : D.attrs()) {\n"
24326                "    if (shouldProcessAttr(A)) {\n"
24327                "      handleAttr(A);\n"
24328                "    }\n"
24329                "  }\n"
24330                "}",
24331                Style);
24332 
24333   // 4. Use braces for the `if` block to keep it uniform with the else block.
24334   verifyFormat("if (isa<FunctionDecl>(D)) {\n"
24335                "  handleFunctionDecl(D);\n"
24336                "} else {\n"
24337                "  // In this else case, it is necessary that we explain the\n"
24338                "  // situation with this surprisingly long comment, so it\n"
24339                "  // would be unclear without the braces whether the\n"
24340                "  // following statement is in the scope of the `if`.\n"
24341                "  handleOtherDecl(D);\n"
24342                "}",
24343                Style);
24344 
24345   // 5. This should also omit braces.  The `for` loop contains only a single
24346   // statement, so it shouldn't have braces.  The `if` also only contains a
24347   // single simple statement (the for loop), so it also should omit braces.
24348   verifyFormat("if (isa<FunctionDecl>(D))\n"
24349                "  for (auto *A : D.attrs())\n"
24350                "    handleAttr(A);",
24351                "if (isa<FunctionDecl>(D)) {\n"
24352                "  for (auto *A : D.attrs()) {\n"
24353                "    handleAttr(A);\n"
24354                "  }\n"
24355                "}",
24356                Style);
24357 
24358   // 6. Use braces for the outer `if` since the nested `for` is braced.
24359   verifyFormat("if (isa<FunctionDecl>(D)) {\n"
24360                "  for (auto *A : D.attrs()) {\n"
24361                "    // In this for loop body, it is necessary that we explain\n"
24362                "    // the situation with this surprisingly long comment,\n"
24363                "    // forcing braces on the `for` block.\n"
24364                "    handleAttr(A);\n"
24365                "  }\n"
24366                "}",
24367                Style);
24368 
24369   // 7. Use braces on the outer block because there are more than two levels of
24370   // nesting.
24371   verifyFormat("if (isa<FunctionDecl>(D)) {\n"
24372                "  for (auto *A : D.attrs())\n"
24373                "    for (ssize_t i : llvm::seq<ssize_t>(count))\n"
24374                "      handleAttrOnDecl(D, A, i);\n"
24375                "}",
24376                "if (isa<FunctionDecl>(D)) {\n"
24377                "  for (auto *A : D.attrs()) {\n"
24378                "    for (ssize_t i : llvm::seq<ssize_t>(count)) {\n"
24379                "      handleAttrOnDecl(D, A, i);\n"
24380                "    }\n"
24381                "  }\n"
24382                "}",
24383                Style);
24384 
24385   // 8. Use braces on the outer block because of a nested `if`, otherwise the
24386   // compiler would warn: `add explicit braces to avoid dangling else`
24387   verifyFormat("if (auto *D = dyn_cast<FunctionDecl>(D)) {\n"
24388                "  if (shouldProcess(D))\n"
24389                "    handleVarDecl(D);\n"
24390                "  else\n"
24391                "    markAsIgnored(D);\n"
24392                "}",
24393                "if (auto *D = dyn_cast<FunctionDecl>(D)) {\n"
24394                "  if (shouldProcess(D)) {\n"
24395                "    handleVarDecl(D);\n"
24396                "  } else {\n"
24397                "    markAsIgnored(D);\n"
24398                "  }\n"
24399                "}",
24400                Style);
24401 
24402   verifyFormat("if (a)\n"
24403                "  b; // comment\n"
24404                "else if (c)\n"
24405                "  d; /* comment */\n"
24406                "else\n"
24407                "  e;",
24408                "if (a) {\n"
24409                "  b; // comment\n"
24410                "} else if (c) {\n"
24411                "  d; /* comment */\n"
24412                "} else {\n"
24413                "  e;\n"
24414                "}",
24415                Style);
24416 
24417   verifyFormat("if (a) {\n"
24418                "  b;\n"
24419                "  c;\n"
24420                "} else if (d) {\n"
24421                "  e;\n"
24422                "}",
24423                Style);
24424 
24425   verifyFormat("if (a) {\n"
24426                "#undef NDEBUG\n"
24427                "  b;\n"
24428                "} else {\n"
24429                "  c;\n"
24430                "}",
24431                Style);
24432 
24433   verifyFormat("if (a) {\n"
24434                "  // comment\n"
24435                "} else if (b) {\n"
24436                "  c;\n"
24437                "}",
24438                Style);
24439 
24440   verifyFormat("if (a) {\n"
24441                "  b;\n"
24442                "} else {\n"
24443                "  { c; }\n"
24444                "}",
24445                Style);
24446 
24447   verifyFormat("if (a) {\n"
24448                "  if (b) // comment\n"
24449                "    c;\n"
24450                "} else if (d) {\n"
24451                "  e;\n"
24452                "}",
24453                "if (a) {\n"
24454                "  if (b) { // comment\n"
24455                "    c;\n"
24456                "  }\n"
24457                "} else if (d) {\n"
24458                "  e;\n"
24459                "}",
24460                Style);
24461 
24462   verifyFormat("if (a) {\n"
24463                "  if (b) {\n"
24464                "    c;\n"
24465                "    // comment\n"
24466                "  } else if (d) {\n"
24467                "    e;\n"
24468                "  }\n"
24469                "}",
24470                Style);
24471 
24472   verifyFormat("if (a) {\n"
24473                "  if (b)\n"
24474                "    c;\n"
24475                "}",
24476                "if (a) {\n"
24477                "  if (b) {\n"
24478                "    c;\n"
24479                "  }\n"
24480                "}",
24481                Style);
24482 
24483   verifyFormat("if (a)\n"
24484                "  if (b)\n"
24485                "    c;\n"
24486                "  else\n"
24487                "    d;\n"
24488                "else\n"
24489                "  e;",
24490                "if (a) {\n"
24491                "  if (b) {\n"
24492                "    c;\n"
24493                "  } else {\n"
24494                "    d;\n"
24495                "  }\n"
24496                "} else {\n"
24497                "  e;\n"
24498                "}",
24499                Style);
24500 
24501   verifyFormat("if (a) {\n"
24502                "  // comment\n"
24503                "  if (b)\n"
24504                "    c;\n"
24505                "  else if (d)\n"
24506                "    e;\n"
24507                "} else {\n"
24508                "  g;\n"
24509                "}",
24510                "if (a) {\n"
24511                "  // comment\n"
24512                "  if (b) {\n"
24513                "    c;\n"
24514                "  } else if (d) {\n"
24515                "    e;\n"
24516                "  }\n"
24517                "} else {\n"
24518                "  g;\n"
24519                "}",
24520                Style);
24521 
24522   verifyFormat("if (a)\n"
24523                "  b;\n"
24524                "else if (c)\n"
24525                "  d;\n"
24526                "else\n"
24527                "  e;",
24528                "if (a) {\n"
24529                "  b;\n"
24530                "} else {\n"
24531                "  if (c) {\n"
24532                "    d;\n"
24533                "  } else {\n"
24534                "    e;\n"
24535                "  }\n"
24536                "}",
24537                Style);
24538 
24539   verifyFormat("if (a) {\n"
24540                "  if (b)\n"
24541                "    c;\n"
24542                "  else if (d)\n"
24543                "    e;\n"
24544                "} else {\n"
24545                "  g;\n"
24546                "}",
24547                "if (a) {\n"
24548                "  if (b)\n"
24549                "    c;\n"
24550                "  else {\n"
24551                "    if (d)\n"
24552                "      e;\n"
24553                "  }\n"
24554                "} else {\n"
24555                "  g;\n"
24556                "}",
24557                Style);
24558 
24559   verifyFormat("if (a)\n"
24560                "  b;\n"
24561                "else if (c)\n"
24562                "  while (d)\n"
24563                "    e;\n"
24564                "// comment",
24565                "if (a)\n"
24566                "{\n"
24567                "  b;\n"
24568                "} else if (c) {\n"
24569                "  while (d) {\n"
24570                "    e;\n"
24571                "  }\n"
24572                "}\n"
24573                "// comment",
24574                Style);
24575 
24576   verifyFormat("if (a) {\n"
24577                "  b;\n"
24578                "} else if (c) {\n"
24579                "  d;\n"
24580                "} else {\n"
24581                "  e;\n"
24582                "  g;\n"
24583                "}",
24584                Style);
24585 
24586   verifyFormat("if (a) {\n"
24587                "  b;\n"
24588                "} else if (c) {\n"
24589                "  d;\n"
24590                "} else {\n"
24591                "  e;\n"
24592                "} // comment",
24593                Style);
24594 
24595   verifyFormat("int abs = [](int i) {\n"
24596                "  if (i >= 0)\n"
24597                "    return i;\n"
24598                "  return -i;\n"
24599                "};",
24600                "int abs = [](int i) {\n"
24601                "  if (i >= 0) {\n"
24602                "    return i;\n"
24603                "  }\n"
24604                "  return -i;\n"
24605                "};",
24606                Style);
24607 
24608   // FIXME: See https://github.com/llvm/llvm-project/issues/53543.
24609 #if 0
24610   Style.ColumnLimit = 65;
24611 
24612   verifyFormat("if (condition) {\n"
24613                "  ff(Indices,\n"
24614                "     [&](unsigned LHSI, unsigned RHSI) { return true; });\n"
24615                "} else {\n"
24616                "  ff(Indices,\n"
24617                "     [&](unsigned LHSI, unsigned RHSI) { return true; });\n"
24618                "}",
24619                Style);
24620 
24621   Style.ColumnLimit = 20;
24622 
24623   verifyFormat("if (a) {\n"
24624                "  b = c + // 1 -\n"
24625                "      d;\n"
24626                "}",
24627                Style);
24628 
24629   verifyFormat("if (a) {\n"
24630                "  b = c >= 0 ? d\n"
24631                "             : e;\n"
24632                "}",
24633                "if (a) {\n"
24634                "  b = c >= 0 ? d : e;\n"
24635                "}",
24636                Style);
24637 #endif
24638 
24639   Style.ColumnLimit = 20;
24640 
24641   verifyFormat("if (a)\n"
24642                "  b = c > 0 ? d : e;",
24643                "if (a) {\n"
24644                "  b = c > 0 ? d : e;\n"
24645                "}",
24646                Style);
24647 
24648   Style.ColumnLimit = 0;
24649 
24650   verifyFormat("if (a)\n"
24651                "  b234567890223456789032345678904234567890 = "
24652                "c234567890223456789032345678904234567890;",
24653                "if (a) {\n"
24654                "  b234567890223456789032345678904234567890 = "
24655                "c234567890223456789032345678904234567890;\n"
24656                "}",
24657                Style);
24658 }
24659 
24660 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndent) {
24661   auto Style = getLLVMStyle();
24662 
24663   StringRef Short = "functionCall(paramA, paramB, paramC);\n"
24664                     "void functionDecl(int a, int b, int c);";
24665 
24666   StringRef Medium = "functionCall(paramA, paramB, paramC, paramD, paramE, "
24667                      "paramF, paramG, paramH, paramI);\n"
24668                      "void functionDecl(int argumentA, int argumentB, int "
24669                      "argumentC, int argumentD, int argumentE);";
24670 
24671   verifyFormat(Short, Style);
24672 
24673   StringRef NoBreak = "functionCall(paramA, paramB, paramC, paramD, paramE, "
24674                       "paramF, paramG, paramH,\n"
24675                       "             paramI);\n"
24676                       "void functionDecl(int argumentA, int argumentB, int "
24677                       "argumentC, int argumentD,\n"
24678                       "                  int argumentE);";
24679 
24680   verifyFormat(NoBreak, Medium, Style);
24681   verifyFormat(NoBreak,
24682                "functionCall(\n"
24683                "    paramA,\n"
24684                "    paramB,\n"
24685                "    paramC,\n"
24686                "    paramD,\n"
24687                "    paramE,\n"
24688                "    paramF,\n"
24689                "    paramG,\n"
24690                "    paramH,\n"
24691                "    paramI\n"
24692                ");\n"
24693                "void functionDecl(\n"
24694                "    int argumentA,\n"
24695                "    int argumentB,\n"
24696                "    int argumentC,\n"
24697                "    int argumentD,\n"
24698                "    int argumentE\n"
24699                ");",
24700                Style);
24701 
24702   verifyFormat("outerFunctionCall(nestedFunctionCall(argument1),\n"
24703                "                  nestedLongFunctionCall(argument1, "
24704                "argument2, argument3,\n"
24705                "                                         argument4, "
24706                "argument5));",
24707                Style);
24708 
24709   Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
24710 
24711   verifyFormat(Short, Style);
24712   verifyFormat(
24713       "functionCall(\n"
24714       "    paramA, paramB, paramC, paramD, paramE, paramF, paramG, paramH, "
24715       "paramI\n"
24716       ");\n"
24717       "void functionDecl(\n"
24718       "    int argumentA, int argumentB, int argumentC, int argumentD, int "
24719       "argumentE\n"
24720       ");",
24721       Medium, Style);
24722 
24723   Style.AllowAllArgumentsOnNextLine = false;
24724   Style.AllowAllParametersOfDeclarationOnNextLine = false;
24725 
24726   verifyFormat(Short, Style);
24727   verifyFormat(
24728       "functionCall(\n"
24729       "    paramA, paramB, paramC, paramD, paramE, paramF, paramG, paramH, "
24730       "paramI\n"
24731       ");\n"
24732       "void functionDecl(\n"
24733       "    int argumentA, int argumentB, int argumentC, int argumentD, int "
24734       "argumentE\n"
24735       ");",
24736       Medium, Style);
24737 
24738   Style.BinPackArguments = false;
24739   Style.BinPackParameters = false;
24740 
24741   verifyFormat(Short, Style);
24742 
24743   verifyFormat("functionCall(\n"
24744                "    paramA,\n"
24745                "    paramB,\n"
24746                "    paramC,\n"
24747                "    paramD,\n"
24748                "    paramE,\n"
24749                "    paramF,\n"
24750                "    paramG,\n"
24751                "    paramH,\n"
24752                "    paramI\n"
24753                ");\n"
24754                "void functionDecl(\n"
24755                "    int argumentA,\n"
24756                "    int argumentB,\n"
24757                "    int argumentC,\n"
24758                "    int argumentD,\n"
24759                "    int argumentE\n"
24760                ");",
24761                Medium, Style);
24762 
24763   verifyFormat("outerFunctionCall(\n"
24764                "    nestedFunctionCall(argument1),\n"
24765                "    nestedLongFunctionCall(\n"
24766                "        argument1,\n"
24767                "        argument2,\n"
24768                "        argument3,\n"
24769                "        argument4,\n"
24770                "        argument5\n"
24771                "    )\n"
24772                ");",
24773                Style);
24774 
24775   verifyFormat("int a = (int)b;", Style);
24776   verifyFormat("int a = (int)b;",
24777                "int a = (\n"
24778                "    int\n"
24779                ") b;",
24780                Style);
24781 
24782   verifyFormat("return (true);", Style);
24783   verifyFormat("return (true);",
24784                "return (\n"
24785                "    true\n"
24786                ");",
24787                Style);
24788 
24789   verifyFormat("void foo();", Style);
24790   verifyFormat("void foo();",
24791                "void foo(\n"
24792                ");",
24793                Style);
24794 
24795   verifyFormat("void foo() {}", Style);
24796   verifyFormat("void foo() {}",
24797                "void foo(\n"
24798                ") {\n"
24799                "}",
24800                Style);
24801 
24802   verifyFormat("auto string = std::string();", Style);
24803   verifyFormat("auto string = std::string();",
24804                "auto string = std::string(\n"
24805                ");",
24806                Style);
24807 
24808   verifyFormat("void (*functionPointer)() = nullptr;", Style);
24809   verifyFormat("void (*functionPointer)() = nullptr;",
24810                "void (\n"
24811                "    *functionPointer\n"
24812                ")\n"
24813                "(\n"
24814                ") = nullptr;",
24815                Style);
24816 }
24817 
24818 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentIfStatement) {
24819   auto Style = getLLVMStyle();
24820 
24821   verifyFormat("if (foo()) {\n"
24822                "  return;\n"
24823                "}",
24824                Style);
24825 
24826   verifyFormat("if (quitelongarg !=\n"
24827                "    (alsolongarg - 1)) { // ABC is a very longgggggggggggg "
24828                "comment\n"
24829                "  return;\n"
24830                "}",
24831                Style);
24832 
24833   Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
24834 
24835   verifyFormat("if (foo()) {\n"
24836                "  return;\n"
24837                "}",
24838                Style);
24839 
24840   verifyFormat("if (quitelongarg !=\n"
24841                "    (alsolongarg - 1)) { // ABC is a very longgggggggggggg "
24842                "comment\n"
24843                "  return;\n"
24844                "}",
24845                Style);
24846 }
24847 
24848 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentForStatement) {
24849   auto Style = getLLVMStyle();
24850 
24851   verifyFormat("for (int i = 0; i < 5; ++i) {\n"
24852                "  doSomething();\n"
24853                "}",
24854                Style);
24855 
24856   verifyFormat("for (int myReallyLongCountVariable = 0; "
24857                "myReallyLongCountVariable < count;\n"
24858                "     myReallyLongCountVariable++) {\n"
24859                "  doSomething();\n"
24860                "}",
24861                Style);
24862 
24863   Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
24864 
24865   verifyFormat("for (int i = 0; i < 5; ++i) {\n"
24866                "  doSomething();\n"
24867                "}",
24868                Style);
24869 
24870   verifyFormat("for (int myReallyLongCountVariable = 0; "
24871                "myReallyLongCountVariable < count;\n"
24872                "     myReallyLongCountVariable++) {\n"
24873                "  doSomething();\n"
24874                "}",
24875                Style);
24876 }
24877 
24878 TEST_F(FormatTest, UnderstandsDigraphs) {
24879   verifyFormat("int arr<:5:> = {};");
24880   verifyFormat("int arr[5] = <%%>;");
24881   verifyFormat("int arr<:::qualified_variable:> = {};");
24882   verifyFormat("int arr[::qualified_variable] = <%%>;");
24883   verifyFormat("%:include <header>");
24884   verifyFormat("%:define A x##y");
24885   verifyFormat("#define A x%:%:y");
24886 }
24887 
24888 } // namespace
24889 } // namespace format
24890 } // namespace clang
24891