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, FormatsVariableDeclarationsAfterStructOrClass) {
3399   verifyFormat("class A {\n} a, b;");
3400   verifyFormat("struct A {\n} a, b;");
3401   verifyFormat("union A {\n} a;");
3402 }
3403 
3404 TEST_F(FormatTest, FormatsEnum) {
3405   verifyFormat("enum {\n"
3406                "  Zero,\n"
3407                "  One = 1,\n"
3408                "  Two = One + 1,\n"
3409                "  Three = (One + Two),\n"
3410                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
3411                "  Five = (One, Two, Three, Four, 5)\n"
3412                "};");
3413   verifyGoogleFormat("enum {\n"
3414                      "  Zero,\n"
3415                      "  One = 1,\n"
3416                      "  Two = One + 1,\n"
3417                      "  Three = (One + Two),\n"
3418                      "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
3419                      "  Five = (One, Two, Three, Four, 5)\n"
3420                      "};");
3421   verifyFormat("enum Enum {};");
3422   verifyFormat("enum {};");
3423   verifyFormat("enum X E {} d;");
3424   verifyFormat("enum __attribute__((...)) E {} d;");
3425   verifyFormat("enum __declspec__((...)) E {} d;");
3426   verifyFormat("enum {\n"
3427                "  Bar = Foo<int, int>::value\n"
3428                "};",
3429                getLLVMStyleWithColumns(30));
3430 
3431   verifyFormat("enum ShortEnum { A, B, C };");
3432   verifyGoogleFormat("enum ShortEnum { A, B, C };");
3433 
3434   EXPECT_EQ("enum KeepEmptyLines {\n"
3435             "  ONE,\n"
3436             "\n"
3437             "  TWO,\n"
3438             "\n"
3439             "  THREE\n"
3440             "}",
3441             format("enum KeepEmptyLines {\n"
3442                    "  ONE,\n"
3443                    "\n"
3444                    "  TWO,\n"
3445                    "\n"
3446                    "\n"
3447                    "  THREE\n"
3448                    "}"));
3449   verifyFormat("enum E { // comment\n"
3450                "  ONE,\n"
3451                "  TWO\n"
3452                "};\n"
3453                "int i;");
3454 
3455   FormatStyle EightIndent = getLLVMStyle();
3456   EightIndent.IndentWidth = 8;
3457   verifyFormat("enum {\n"
3458                "        VOID,\n"
3459                "        CHAR,\n"
3460                "        SHORT,\n"
3461                "        INT,\n"
3462                "        LONG,\n"
3463                "        SIGNED,\n"
3464                "        UNSIGNED,\n"
3465                "        BOOL,\n"
3466                "        FLOAT,\n"
3467                "        DOUBLE,\n"
3468                "        COMPLEX\n"
3469                "};",
3470                EightIndent);
3471 
3472   // Not enums.
3473   verifyFormat("enum X f() {\n"
3474                "  a();\n"
3475                "  return 42;\n"
3476                "}");
3477   verifyFormat("enum X Type::f() {\n"
3478                "  a();\n"
3479                "  return 42;\n"
3480                "}");
3481   verifyFormat("enum ::X f() {\n"
3482                "  a();\n"
3483                "  return 42;\n"
3484                "}");
3485   verifyFormat("enum ns::X f() {\n"
3486                "  a();\n"
3487                "  return 42;\n"
3488                "}");
3489 }
3490 
3491 TEST_F(FormatTest, FormatsEnumsWithErrors) {
3492   verifyFormat("enum Type {\n"
3493                "  One = 0; // These semicolons should be commas.\n"
3494                "  Two = 1;\n"
3495                "};");
3496   verifyFormat("namespace n {\n"
3497                "enum Type {\n"
3498                "  One,\n"
3499                "  Two, // missing };\n"
3500                "  int i;\n"
3501                "}\n"
3502                "void g() {}");
3503 }
3504 
3505 TEST_F(FormatTest, FormatsEnumStruct) {
3506   verifyFormat("enum struct {\n"
3507                "  Zero,\n"
3508                "  One = 1,\n"
3509                "  Two = One + 1,\n"
3510                "  Three = (One + Two),\n"
3511                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
3512                "  Five = (One, Two, Three, Four, 5)\n"
3513                "};");
3514   verifyFormat("enum struct Enum {};");
3515   verifyFormat("enum struct {};");
3516   verifyFormat("enum struct X E {} d;");
3517   verifyFormat("enum struct __attribute__((...)) E {} d;");
3518   verifyFormat("enum struct __declspec__((...)) E {} d;");
3519   verifyFormat("enum struct X f() {\n  a();\n  return 42;\n}");
3520 }
3521 
3522 TEST_F(FormatTest, FormatsEnumClass) {
3523   verifyFormat("enum class {\n"
3524                "  Zero,\n"
3525                "  One = 1,\n"
3526                "  Two = One + 1,\n"
3527                "  Three = (One + Two),\n"
3528                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
3529                "  Five = (One, Two, Three, Four, 5)\n"
3530                "};");
3531   verifyFormat("enum class Enum {};");
3532   verifyFormat("enum class {};");
3533   verifyFormat("enum class X E {} d;");
3534   verifyFormat("enum class __attribute__((...)) E {} d;");
3535   verifyFormat("enum class __declspec__((...)) E {} d;");
3536   verifyFormat("enum class X f() {\n  a();\n  return 42;\n}");
3537 }
3538 
3539 TEST_F(FormatTest, FormatsEnumTypes) {
3540   verifyFormat("enum X : int {\n"
3541                "  A, // Force multiple lines.\n"
3542                "  B\n"
3543                "};");
3544   verifyFormat("enum X : int { A, B };");
3545   verifyFormat("enum X : std::uint32_t { A, B };");
3546 }
3547 
3548 TEST_F(FormatTest, FormatsTypedefEnum) {
3549   FormatStyle Style = getLLVMStyleWithColumns(40);
3550   verifyFormat("typedef enum {} EmptyEnum;");
3551   verifyFormat("typedef enum { A, B, C } ShortEnum;");
3552   verifyFormat("typedef enum {\n"
3553                "  ZERO = 0,\n"
3554                "  ONE = 1,\n"
3555                "  TWO = 2,\n"
3556                "  THREE = 3\n"
3557                "} LongEnum;",
3558                Style);
3559   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
3560   Style.BraceWrapping.AfterEnum = true;
3561   verifyFormat("typedef enum {} EmptyEnum;");
3562   verifyFormat("typedef enum { A, B, C } ShortEnum;");
3563   verifyFormat("typedef enum\n"
3564                "{\n"
3565                "  ZERO = 0,\n"
3566                "  ONE = 1,\n"
3567                "  TWO = 2,\n"
3568                "  THREE = 3\n"
3569                "} LongEnum;",
3570                Style);
3571 }
3572 
3573 TEST_F(FormatTest, FormatsNSEnums) {
3574   verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }");
3575   verifyGoogleFormat(
3576       "typedef NS_CLOSED_ENUM(NSInteger, SomeName) { AAA, BBB }");
3577   verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n"
3578                      "  // Information about someDecentlyLongValue.\n"
3579                      "  someDecentlyLongValue,\n"
3580                      "  // Information about anotherDecentlyLongValue.\n"
3581                      "  anotherDecentlyLongValue,\n"
3582                      "  // Information about aThirdDecentlyLongValue.\n"
3583                      "  aThirdDecentlyLongValue\n"
3584                      "};");
3585   verifyGoogleFormat("typedef NS_CLOSED_ENUM(NSInteger, MyType) {\n"
3586                      "  // Information about someDecentlyLongValue.\n"
3587                      "  someDecentlyLongValue,\n"
3588                      "  // Information about anotherDecentlyLongValue.\n"
3589                      "  anotherDecentlyLongValue,\n"
3590                      "  // Information about aThirdDecentlyLongValue.\n"
3591                      "  aThirdDecentlyLongValue\n"
3592                      "};");
3593   verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n"
3594                      "  a = 1,\n"
3595                      "  b = 2,\n"
3596                      "  c = 3,\n"
3597                      "};");
3598   verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n"
3599                      "  a = 1,\n"
3600                      "  b = 2,\n"
3601                      "  c = 3,\n"
3602                      "};");
3603   verifyGoogleFormat("typedef CF_CLOSED_ENUM(NSInteger, MyType) {\n"
3604                      "  a = 1,\n"
3605                      "  b = 2,\n"
3606                      "  c = 3,\n"
3607                      "};");
3608   verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n"
3609                      "  a = 1,\n"
3610                      "  b = 2,\n"
3611                      "  c = 3,\n"
3612                      "};");
3613 }
3614 
3615 TEST_F(FormatTest, FormatsBitfields) {
3616   verifyFormat("struct Bitfields {\n"
3617                "  unsigned sClass : 8;\n"
3618                "  unsigned ValueKind : 2;\n"
3619                "};");
3620   verifyFormat("struct A {\n"
3621                "  int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n"
3622                "      bbbbbbbbbbbbbbbbbbbbbbbbb;\n"
3623                "};");
3624   verifyFormat("struct MyStruct {\n"
3625                "  uchar data;\n"
3626                "  uchar : 8;\n"
3627                "  uchar : 8;\n"
3628                "  uchar other;\n"
3629                "};");
3630   FormatStyle Style = getLLVMStyle();
3631   Style.BitFieldColonSpacing = FormatStyle::BFCS_None;
3632   verifyFormat("struct Bitfields {\n"
3633                "  unsigned sClass:8;\n"
3634                "  unsigned ValueKind:2;\n"
3635                "  uchar other;\n"
3636                "};",
3637                Style);
3638   verifyFormat("struct A {\n"
3639                "  int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:1,\n"
3640                "      bbbbbbbbbbbbbbbbbbbbbbbbb:2;\n"
3641                "};",
3642                Style);
3643   Style.BitFieldColonSpacing = FormatStyle::BFCS_Before;
3644   verifyFormat("struct Bitfields {\n"
3645                "  unsigned sClass :8;\n"
3646                "  unsigned ValueKind :2;\n"
3647                "  uchar other;\n"
3648                "};",
3649                Style);
3650   Style.BitFieldColonSpacing = FormatStyle::BFCS_After;
3651   verifyFormat("struct Bitfields {\n"
3652                "  unsigned sClass: 8;\n"
3653                "  unsigned ValueKind: 2;\n"
3654                "  uchar other;\n"
3655                "};",
3656                Style);
3657 }
3658 
3659 TEST_F(FormatTest, FormatsNamespaces) {
3660   FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle();
3661   LLVMWithNoNamespaceFix.FixNamespaceComments = false;
3662 
3663   verifyFormat("namespace some_namespace {\n"
3664                "class A {};\n"
3665                "void f() { f(); }\n"
3666                "}",
3667                LLVMWithNoNamespaceFix);
3668   verifyFormat("namespace N::inline D {\n"
3669                "class A {};\n"
3670                "void f() { f(); }\n"
3671                "}",
3672                LLVMWithNoNamespaceFix);
3673   verifyFormat("namespace N::inline D::E {\n"
3674                "class A {};\n"
3675                "void f() { f(); }\n"
3676                "}",
3677                LLVMWithNoNamespaceFix);
3678   verifyFormat("namespace [[deprecated(\"foo[bar\")]] some_namespace {\n"
3679                "class A {};\n"
3680                "void f() { f(); }\n"
3681                "}",
3682                LLVMWithNoNamespaceFix);
3683   verifyFormat("/* something */ namespace some_namespace {\n"
3684                "class A {};\n"
3685                "void f() { f(); }\n"
3686                "}",
3687                LLVMWithNoNamespaceFix);
3688   verifyFormat("namespace {\n"
3689                "class A {};\n"
3690                "void f() { f(); }\n"
3691                "}",
3692                LLVMWithNoNamespaceFix);
3693   verifyFormat("/* something */ namespace {\n"
3694                "class A {};\n"
3695                "void f() { f(); }\n"
3696                "}",
3697                LLVMWithNoNamespaceFix);
3698   verifyFormat("inline namespace X {\n"
3699                "class A {};\n"
3700                "void f() { f(); }\n"
3701                "}",
3702                LLVMWithNoNamespaceFix);
3703   verifyFormat("/* something */ inline namespace X {\n"
3704                "class A {};\n"
3705                "void f() { f(); }\n"
3706                "}",
3707                LLVMWithNoNamespaceFix);
3708   verifyFormat("export namespace X {\n"
3709                "class A {};\n"
3710                "void f() { f(); }\n"
3711                "}",
3712                LLVMWithNoNamespaceFix);
3713   verifyFormat("using namespace some_namespace;\n"
3714                "class A {};\n"
3715                "void f() { f(); }",
3716                LLVMWithNoNamespaceFix);
3717 
3718   // This code is more common than we thought; if we
3719   // layout this correctly the semicolon will go into
3720   // its own line, which is undesirable.
3721   verifyFormat("namespace {};", LLVMWithNoNamespaceFix);
3722   verifyFormat("namespace {\n"
3723                "class A {};\n"
3724                "};",
3725                LLVMWithNoNamespaceFix);
3726 
3727   verifyFormat("namespace {\n"
3728                "int SomeVariable = 0; // comment\n"
3729                "} // namespace",
3730                LLVMWithNoNamespaceFix);
3731   EXPECT_EQ("#ifndef HEADER_GUARD\n"
3732             "#define HEADER_GUARD\n"
3733             "namespace my_namespace {\n"
3734             "int i;\n"
3735             "} // my_namespace\n"
3736             "#endif // HEADER_GUARD",
3737             format("#ifndef HEADER_GUARD\n"
3738                    " #define HEADER_GUARD\n"
3739                    "   namespace my_namespace {\n"
3740                    "int i;\n"
3741                    "}    // my_namespace\n"
3742                    "#endif    // HEADER_GUARD",
3743                    LLVMWithNoNamespaceFix));
3744 
3745   EXPECT_EQ("namespace A::B {\n"
3746             "class C {};\n"
3747             "}",
3748             format("namespace A::B {\n"
3749                    "class C {};\n"
3750                    "}",
3751                    LLVMWithNoNamespaceFix));
3752 
3753   FormatStyle Style = getLLVMStyle();
3754   Style.NamespaceIndentation = FormatStyle::NI_All;
3755   EXPECT_EQ("namespace out {\n"
3756             "  int i;\n"
3757             "  namespace in {\n"
3758             "    int i;\n"
3759             "  } // namespace in\n"
3760             "} // namespace out",
3761             format("namespace out {\n"
3762                    "int i;\n"
3763                    "namespace in {\n"
3764                    "int i;\n"
3765                    "} // namespace in\n"
3766                    "} // namespace out",
3767                    Style));
3768 
3769   FormatStyle ShortInlineFunctions = getLLVMStyle();
3770   ShortInlineFunctions.NamespaceIndentation = FormatStyle::NI_All;
3771   ShortInlineFunctions.AllowShortFunctionsOnASingleLine =
3772       FormatStyle::SFS_Inline;
3773   verifyFormat("namespace {\n"
3774                "  void f() {\n"
3775                "    return;\n"
3776                "  }\n"
3777                "} // namespace\n",
3778                ShortInlineFunctions);
3779   verifyFormat("namespace {\n"
3780                "  int some_int;\n"
3781                "  void f() {\n"
3782                "    return;\n"
3783                "  }\n"
3784                "} // namespace\n",
3785                ShortInlineFunctions);
3786   verifyFormat("namespace interface {\n"
3787                "  void f() {\n"
3788                "    return;\n"
3789                "  }\n"
3790                "} // namespace interface\n",
3791                ShortInlineFunctions);
3792   verifyFormat("namespace {\n"
3793                "  class X {\n"
3794                "    void f() { return; }\n"
3795                "  };\n"
3796                "} // namespace\n",
3797                ShortInlineFunctions);
3798   verifyFormat("namespace {\n"
3799                "  struct X {\n"
3800                "    void f() { return; }\n"
3801                "  };\n"
3802                "} // namespace\n",
3803                ShortInlineFunctions);
3804   verifyFormat("namespace {\n"
3805                "  union X {\n"
3806                "    void f() { return; }\n"
3807                "  };\n"
3808                "} // namespace\n",
3809                ShortInlineFunctions);
3810   verifyFormat("extern \"C\" {\n"
3811                "void f() {\n"
3812                "  return;\n"
3813                "}\n"
3814                "} // namespace\n",
3815                ShortInlineFunctions);
3816   verifyFormat("namespace {\n"
3817                "  class X {\n"
3818                "    void f() { return; }\n"
3819                "  } x;\n"
3820                "} // namespace\n",
3821                ShortInlineFunctions);
3822   verifyFormat("namespace {\n"
3823                "  [[nodiscard]] class X {\n"
3824                "    void f() { return; }\n"
3825                "  };\n"
3826                "} // namespace\n",
3827                ShortInlineFunctions);
3828   verifyFormat("namespace {\n"
3829                "  static class X {\n"
3830                "    void f() { return; }\n"
3831                "  } x;\n"
3832                "} // namespace\n",
3833                ShortInlineFunctions);
3834   verifyFormat("namespace {\n"
3835                "  constexpr class X {\n"
3836                "    void f() { return; }\n"
3837                "  } x;\n"
3838                "} // namespace\n",
3839                ShortInlineFunctions);
3840 
3841   ShortInlineFunctions.IndentExternBlock = FormatStyle::IEBS_Indent;
3842   verifyFormat("extern \"C\" {\n"
3843                "  void f() {\n"
3844                "    return;\n"
3845                "  }\n"
3846                "} // namespace\n",
3847                ShortInlineFunctions);
3848 
3849   Style.NamespaceIndentation = FormatStyle::NI_Inner;
3850   EXPECT_EQ("namespace out {\n"
3851             "int i;\n"
3852             "namespace in {\n"
3853             "  int i;\n"
3854             "} // namespace in\n"
3855             "} // namespace out",
3856             format("namespace out {\n"
3857                    "int i;\n"
3858                    "namespace in {\n"
3859                    "int i;\n"
3860                    "} // namespace in\n"
3861                    "} // namespace out",
3862                    Style));
3863 
3864   Style.NamespaceIndentation = FormatStyle::NI_None;
3865   verifyFormat("template <class T>\n"
3866                "concept a_concept = X<>;\n"
3867                "namespace B {\n"
3868                "struct b_struct {};\n"
3869                "} // namespace B\n",
3870                Style);
3871   verifyFormat("template <int I>\n"
3872                "constexpr void foo()\n"
3873                "  requires(I == 42)\n"
3874                "{}\n"
3875                "namespace ns {\n"
3876                "void foo() {}\n"
3877                "} // namespace ns\n",
3878                Style);
3879 }
3880 
3881 TEST_F(FormatTest, NamespaceMacros) {
3882   FormatStyle Style = getLLVMStyle();
3883   Style.NamespaceMacros.push_back("TESTSUITE");
3884 
3885   verifyFormat("TESTSUITE(A) {\n"
3886                "int foo();\n"
3887                "} // TESTSUITE(A)",
3888                Style);
3889 
3890   verifyFormat("TESTSUITE(A, B) {\n"
3891                "int foo();\n"
3892                "} // TESTSUITE(A)",
3893                Style);
3894 
3895   // Properly indent according to NamespaceIndentation style
3896   Style.NamespaceIndentation = FormatStyle::NI_All;
3897   verifyFormat("TESTSUITE(A) {\n"
3898                "  int foo();\n"
3899                "} // TESTSUITE(A)",
3900                Style);
3901   verifyFormat("TESTSUITE(A) {\n"
3902                "  namespace B {\n"
3903                "    int foo();\n"
3904                "  } // namespace B\n"
3905                "} // TESTSUITE(A)",
3906                Style);
3907   verifyFormat("namespace A {\n"
3908                "  TESTSUITE(B) {\n"
3909                "    int foo();\n"
3910                "  } // TESTSUITE(B)\n"
3911                "} // namespace A",
3912                Style);
3913 
3914   Style.NamespaceIndentation = FormatStyle::NI_Inner;
3915   verifyFormat("TESTSUITE(A) {\n"
3916                "TESTSUITE(B) {\n"
3917                "  int foo();\n"
3918                "} // TESTSUITE(B)\n"
3919                "} // TESTSUITE(A)",
3920                Style);
3921   verifyFormat("TESTSUITE(A) {\n"
3922                "namespace B {\n"
3923                "  int foo();\n"
3924                "} // namespace B\n"
3925                "} // TESTSUITE(A)",
3926                Style);
3927   verifyFormat("namespace A {\n"
3928                "TESTSUITE(B) {\n"
3929                "  int foo();\n"
3930                "} // TESTSUITE(B)\n"
3931                "} // namespace A",
3932                Style);
3933 
3934   // Properly merge namespace-macros blocks in CompactNamespaces mode
3935   Style.NamespaceIndentation = FormatStyle::NI_None;
3936   Style.CompactNamespaces = true;
3937   verifyFormat("TESTSUITE(A) { TESTSUITE(B) {\n"
3938                "}} // TESTSUITE(A::B)",
3939                Style);
3940 
3941   EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n"
3942             "}} // TESTSUITE(out::in)",
3943             format("TESTSUITE(out) {\n"
3944                    "TESTSUITE(in) {\n"
3945                    "} // TESTSUITE(in)\n"
3946                    "} // TESTSUITE(out)",
3947                    Style));
3948 
3949   EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n"
3950             "}} // TESTSUITE(out::in)",
3951             format("TESTSUITE(out) {\n"
3952                    "TESTSUITE(in) {\n"
3953                    "} // TESTSUITE(in)\n"
3954                    "} // TESTSUITE(out)",
3955                    Style));
3956 
3957   // Do not merge different namespaces/macros
3958   EXPECT_EQ("namespace out {\n"
3959             "TESTSUITE(in) {\n"
3960             "} // TESTSUITE(in)\n"
3961             "} // namespace out",
3962             format("namespace out {\n"
3963                    "TESTSUITE(in) {\n"
3964                    "} // TESTSUITE(in)\n"
3965                    "} // namespace out",
3966                    Style));
3967   EXPECT_EQ("TESTSUITE(out) {\n"
3968             "namespace in {\n"
3969             "} // namespace in\n"
3970             "} // TESTSUITE(out)",
3971             format("TESTSUITE(out) {\n"
3972                    "namespace in {\n"
3973                    "} // namespace in\n"
3974                    "} // TESTSUITE(out)",
3975                    Style));
3976   Style.NamespaceMacros.push_back("FOOBAR");
3977   EXPECT_EQ("TESTSUITE(out) {\n"
3978             "FOOBAR(in) {\n"
3979             "} // FOOBAR(in)\n"
3980             "} // TESTSUITE(out)",
3981             format("TESTSUITE(out) {\n"
3982                    "FOOBAR(in) {\n"
3983                    "} // FOOBAR(in)\n"
3984                    "} // TESTSUITE(out)",
3985                    Style));
3986 }
3987 
3988 TEST_F(FormatTest, FormatsCompactNamespaces) {
3989   FormatStyle Style = getLLVMStyle();
3990   Style.CompactNamespaces = true;
3991   Style.NamespaceMacros.push_back("TESTSUITE");
3992 
3993   verifyFormat("namespace A { namespace B {\n"
3994                "}} // namespace A::B",
3995                Style);
3996 
3997   EXPECT_EQ("namespace out { namespace in {\n"
3998             "}} // namespace out::in",
3999             format("namespace out {\n"
4000                    "namespace in {\n"
4001                    "} // namespace in\n"
4002                    "} // namespace out",
4003                    Style));
4004 
4005   // Only namespaces which have both consecutive opening and end get compacted
4006   EXPECT_EQ("namespace out {\n"
4007             "namespace in1 {\n"
4008             "} // namespace in1\n"
4009             "namespace in2 {\n"
4010             "} // namespace in2\n"
4011             "} // namespace out",
4012             format("namespace out {\n"
4013                    "namespace in1 {\n"
4014                    "} // namespace in1\n"
4015                    "namespace in2 {\n"
4016                    "} // namespace in2\n"
4017                    "} // namespace out",
4018                    Style));
4019 
4020   EXPECT_EQ("namespace out {\n"
4021             "int i;\n"
4022             "namespace in {\n"
4023             "int j;\n"
4024             "} // namespace in\n"
4025             "int k;\n"
4026             "} // namespace out",
4027             format("namespace out { int i;\n"
4028                    "namespace in { int j; } // namespace in\n"
4029                    "int k; } // namespace out",
4030                    Style));
4031 
4032   EXPECT_EQ("namespace A { namespace B { namespace C {\n"
4033             "}}} // namespace A::B::C\n",
4034             format("namespace A { namespace B {\n"
4035                    "namespace C {\n"
4036                    "}} // namespace B::C\n"
4037                    "} // namespace A\n",
4038                    Style));
4039 
4040   Style.ColumnLimit = 40;
4041   EXPECT_EQ("namespace aaaaaaaaaa {\n"
4042             "namespace bbbbbbbbbb {\n"
4043             "}} // namespace aaaaaaaaaa::bbbbbbbbbb",
4044             format("namespace aaaaaaaaaa {\n"
4045                    "namespace bbbbbbbbbb {\n"
4046                    "} // namespace bbbbbbbbbb\n"
4047                    "} // namespace aaaaaaaaaa",
4048                    Style));
4049 
4050   EXPECT_EQ("namespace aaaaaa { namespace bbbbbb {\n"
4051             "namespace cccccc {\n"
4052             "}}} // namespace aaaaaa::bbbbbb::cccccc",
4053             format("namespace aaaaaa {\n"
4054                    "namespace bbbbbb {\n"
4055                    "namespace cccccc {\n"
4056                    "} // namespace cccccc\n"
4057                    "} // namespace bbbbbb\n"
4058                    "} // namespace aaaaaa",
4059                    Style));
4060   Style.ColumnLimit = 80;
4061 
4062   // Extra semicolon after 'inner' closing brace prevents merging
4063   EXPECT_EQ("namespace out { namespace in {\n"
4064             "}; } // namespace out::in",
4065             format("namespace out {\n"
4066                    "namespace in {\n"
4067                    "}; // namespace in\n"
4068                    "} // namespace out",
4069                    Style));
4070 
4071   // Extra semicolon after 'outer' closing brace is conserved
4072   EXPECT_EQ("namespace out { namespace in {\n"
4073             "}}; // namespace out::in",
4074             format("namespace out {\n"
4075                    "namespace in {\n"
4076                    "} // namespace in\n"
4077                    "}; // namespace out",
4078                    Style));
4079 
4080   Style.NamespaceIndentation = FormatStyle::NI_All;
4081   EXPECT_EQ("namespace out { namespace in {\n"
4082             "  int i;\n"
4083             "}} // namespace out::in",
4084             format("namespace out {\n"
4085                    "namespace in {\n"
4086                    "int i;\n"
4087                    "} // namespace in\n"
4088                    "} // namespace out",
4089                    Style));
4090   EXPECT_EQ("namespace out { namespace mid {\n"
4091             "  namespace in {\n"
4092             "    int j;\n"
4093             "  } // namespace in\n"
4094             "  int k;\n"
4095             "}} // namespace out::mid",
4096             format("namespace out { namespace mid {\n"
4097                    "namespace in { int j; } // namespace in\n"
4098                    "int k; }} // namespace out::mid",
4099                    Style));
4100 
4101   Style.NamespaceIndentation = FormatStyle::NI_Inner;
4102   EXPECT_EQ("namespace out { namespace in {\n"
4103             "  int i;\n"
4104             "}} // namespace out::in",
4105             format("namespace out {\n"
4106                    "namespace in {\n"
4107                    "int i;\n"
4108                    "} // namespace in\n"
4109                    "} // namespace out",
4110                    Style));
4111   EXPECT_EQ("namespace out { namespace mid { namespace in {\n"
4112             "  int i;\n"
4113             "}}} // namespace out::mid::in",
4114             format("namespace out {\n"
4115                    "namespace mid {\n"
4116                    "namespace in {\n"
4117                    "int i;\n"
4118                    "} // namespace in\n"
4119                    "} // namespace mid\n"
4120                    "} // namespace out",
4121                    Style));
4122 
4123   Style.CompactNamespaces = true;
4124   Style.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
4125   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4126   Style.BraceWrapping.BeforeLambdaBody = true;
4127   verifyFormat("namespace out { namespace in {\n"
4128                "}} // namespace out::in",
4129                Style);
4130   EXPECT_EQ("namespace out { namespace in {\n"
4131             "}} // namespace out::in",
4132             format("namespace out {\n"
4133                    "namespace in {\n"
4134                    "} // namespace in\n"
4135                    "} // namespace out",
4136                    Style));
4137 }
4138 
4139 TEST_F(FormatTest, FormatsExternC) {
4140   verifyFormat("extern \"C\" {\nint a;");
4141   verifyFormat("extern \"C\" {}");
4142   verifyFormat("extern \"C\" {\n"
4143                "int foo();\n"
4144                "}");
4145   verifyFormat("extern \"C\" int foo() {}");
4146   verifyFormat("extern \"C\" int foo();");
4147   verifyFormat("extern \"C\" int foo() {\n"
4148                "  int i = 42;\n"
4149                "  return i;\n"
4150                "}");
4151 
4152   FormatStyle Style = getLLVMStyle();
4153   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4154   Style.BraceWrapping.AfterFunction = true;
4155   verifyFormat("extern \"C\" int foo() {}", Style);
4156   verifyFormat("extern \"C\" int foo();", Style);
4157   verifyFormat("extern \"C\" int foo()\n"
4158                "{\n"
4159                "  int i = 42;\n"
4160                "  return i;\n"
4161                "}",
4162                Style);
4163 
4164   Style.BraceWrapping.AfterExternBlock = true;
4165   Style.BraceWrapping.SplitEmptyRecord = false;
4166   verifyFormat("extern \"C\"\n"
4167                "{}",
4168                Style);
4169   verifyFormat("extern \"C\"\n"
4170                "{\n"
4171                "  int foo();\n"
4172                "}",
4173                Style);
4174 }
4175 
4176 TEST_F(FormatTest, IndentExternBlockStyle) {
4177   FormatStyle Style = getLLVMStyle();
4178   Style.IndentWidth = 2;
4179 
4180   Style.IndentExternBlock = FormatStyle::IEBS_Indent;
4181   verifyFormat("extern \"C\" { /*9*/\n"
4182                "}",
4183                Style);
4184   verifyFormat("extern \"C\" {\n"
4185                "  int foo10();\n"
4186                "}",
4187                Style);
4188 
4189   Style.IndentExternBlock = FormatStyle::IEBS_NoIndent;
4190   verifyFormat("extern \"C\" { /*11*/\n"
4191                "}",
4192                Style);
4193   verifyFormat("extern \"C\" {\n"
4194                "int foo12();\n"
4195                "}",
4196                Style);
4197 
4198   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4199   Style.BraceWrapping.AfterExternBlock = true;
4200   Style.IndentExternBlock = FormatStyle::IEBS_Indent;
4201   verifyFormat("extern \"C\"\n"
4202                "{ /*13*/\n"
4203                "}",
4204                Style);
4205   verifyFormat("extern \"C\"\n{\n"
4206                "  int foo14();\n"
4207                "}",
4208                Style);
4209 
4210   Style.BraceWrapping.AfterExternBlock = false;
4211   Style.IndentExternBlock = FormatStyle::IEBS_NoIndent;
4212   verifyFormat("extern \"C\" { /*15*/\n"
4213                "}",
4214                Style);
4215   verifyFormat("extern \"C\" {\n"
4216                "int foo16();\n"
4217                "}",
4218                Style);
4219 
4220   Style.BraceWrapping.AfterExternBlock = true;
4221   verifyFormat("extern \"C\"\n"
4222                "{ /*13*/\n"
4223                "}",
4224                Style);
4225   verifyFormat("extern \"C\"\n"
4226                "{\n"
4227                "int foo14();\n"
4228                "}",
4229                Style);
4230 
4231   Style.IndentExternBlock = FormatStyle::IEBS_Indent;
4232   verifyFormat("extern \"C\"\n"
4233                "{ /*13*/\n"
4234                "}",
4235                Style);
4236   verifyFormat("extern \"C\"\n"
4237                "{\n"
4238                "  int foo14();\n"
4239                "}",
4240                Style);
4241 }
4242 
4243 TEST_F(FormatTest, FormatsInlineASM) {
4244   verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
4245   verifyFormat("asm(\"nop\" ::: \"memory\");");
4246   verifyFormat(
4247       "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
4248       "    \"cpuid\\n\\t\"\n"
4249       "    \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
4250       "    : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
4251       "    : \"a\"(value));");
4252   EXPECT_EQ(
4253       "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
4254       "  __asm {\n"
4255       "        mov     edx,[that] // vtable in edx\n"
4256       "        mov     eax,methodIndex\n"
4257       "        call    [edx][eax*4] // stdcall\n"
4258       "  }\n"
4259       "}",
4260       format("void NS_InvokeByIndex(void *that,   unsigned int methodIndex) {\n"
4261              "    __asm {\n"
4262              "        mov     edx,[that] // vtable in edx\n"
4263              "        mov     eax,methodIndex\n"
4264              "        call    [edx][eax*4] // stdcall\n"
4265              "    }\n"
4266              "}"));
4267   EXPECT_EQ("_asm {\n"
4268             "  xor eax, eax;\n"
4269             "  cpuid;\n"
4270             "}",
4271             format("_asm {\n"
4272                    "  xor eax, eax;\n"
4273                    "  cpuid;\n"
4274                    "}"));
4275   verifyFormat("void function() {\n"
4276                "  // comment\n"
4277                "  asm(\"\");\n"
4278                "}");
4279   EXPECT_EQ("__asm {\n"
4280             "}\n"
4281             "int i;",
4282             format("__asm   {\n"
4283                    "}\n"
4284                    "int   i;"));
4285 }
4286 
4287 TEST_F(FormatTest, FormatTryCatch) {
4288   verifyFormat("try {\n"
4289                "  throw a * b;\n"
4290                "} catch (int a) {\n"
4291                "  // Do nothing.\n"
4292                "} catch (...) {\n"
4293                "  exit(42);\n"
4294                "}");
4295 
4296   // Function-level try statements.
4297   verifyFormat("int f() try { return 4; } catch (...) {\n"
4298                "  return 5;\n"
4299                "}");
4300   verifyFormat("class A {\n"
4301                "  int a;\n"
4302                "  A() try : a(0) {\n"
4303                "  } catch (...) {\n"
4304                "    throw;\n"
4305                "  }\n"
4306                "};\n");
4307   verifyFormat("class A {\n"
4308                "  int a;\n"
4309                "  A() try : a(0), b{1} {\n"
4310                "  } catch (...) {\n"
4311                "    throw;\n"
4312                "  }\n"
4313                "};\n");
4314   verifyFormat("class A {\n"
4315                "  int a;\n"
4316                "  A() try : a(0), b{1}, c{2} {\n"
4317                "  } catch (...) {\n"
4318                "    throw;\n"
4319                "  }\n"
4320                "};\n");
4321   verifyFormat("class A {\n"
4322                "  int a;\n"
4323                "  A() try : a(0), b{1}, c{2} {\n"
4324                "    { // New scope.\n"
4325                "    }\n"
4326                "  } catch (...) {\n"
4327                "    throw;\n"
4328                "  }\n"
4329                "};\n");
4330 
4331   // Incomplete try-catch blocks.
4332   verifyIncompleteFormat("try {} catch (");
4333 }
4334 
4335 TEST_F(FormatTest, FormatTryAsAVariable) {
4336   verifyFormat("int try;");
4337   verifyFormat("int try, size;");
4338   verifyFormat("try = foo();");
4339   verifyFormat("if (try < size) {\n  return true;\n}");
4340 
4341   verifyFormat("int catch;");
4342   verifyFormat("int catch, size;");
4343   verifyFormat("catch = foo();");
4344   verifyFormat("if (catch < size) {\n  return true;\n}");
4345 
4346   FormatStyle Style = getLLVMStyle();
4347   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4348   Style.BraceWrapping.AfterFunction = true;
4349   Style.BraceWrapping.BeforeCatch = true;
4350   verifyFormat("try {\n"
4351                "  int bar = 1;\n"
4352                "}\n"
4353                "catch (...) {\n"
4354                "  int bar = 1;\n"
4355                "}",
4356                Style);
4357   verifyFormat("#if NO_EX\n"
4358                "try\n"
4359                "#endif\n"
4360                "{\n"
4361                "}\n"
4362                "#if NO_EX\n"
4363                "catch (...) {\n"
4364                "}",
4365                Style);
4366   verifyFormat("try /* abc */ {\n"
4367                "  int bar = 1;\n"
4368                "}\n"
4369                "catch (...) {\n"
4370                "  int bar = 1;\n"
4371                "}",
4372                Style);
4373   verifyFormat("try\n"
4374                "// abc\n"
4375                "{\n"
4376                "  int bar = 1;\n"
4377                "}\n"
4378                "catch (...) {\n"
4379                "  int bar = 1;\n"
4380                "}",
4381                Style);
4382 }
4383 
4384 TEST_F(FormatTest, FormatSEHTryCatch) {
4385   verifyFormat("__try {\n"
4386                "  int a = b * c;\n"
4387                "} __except (EXCEPTION_EXECUTE_HANDLER) {\n"
4388                "  // Do nothing.\n"
4389                "}");
4390 
4391   verifyFormat("__try {\n"
4392                "  int a = b * c;\n"
4393                "} __finally {\n"
4394                "  // Do nothing.\n"
4395                "}");
4396 
4397   verifyFormat("DEBUG({\n"
4398                "  __try {\n"
4399                "  } __finally {\n"
4400                "  }\n"
4401                "});\n");
4402 }
4403 
4404 TEST_F(FormatTest, IncompleteTryCatchBlocks) {
4405   verifyFormat("try {\n"
4406                "  f();\n"
4407                "} catch {\n"
4408                "  g();\n"
4409                "}");
4410   verifyFormat("try {\n"
4411                "  f();\n"
4412                "} catch (A a) MACRO(x) {\n"
4413                "  g();\n"
4414                "} catch (B b) MACRO(x) {\n"
4415                "  g();\n"
4416                "}");
4417 }
4418 
4419 TEST_F(FormatTest, FormatTryCatchBraceStyles) {
4420   FormatStyle Style = getLLVMStyle();
4421   for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla,
4422                           FormatStyle::BS_WebKit}) {
4423     Style.BreakBeforeBraces = BraceStyle;
4424     verifyFormat("try {\n"
4425                  "  // something\n"
4426                  "} catch (...) {\n"
4427                  "  // something\n"
4428                  "}",
4429                  Style);
4430   }
4431   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
4432   verifyFormat("try {\n"
4433                "  // something\n"
4434                "}\n"
4435                "catch (...) {\n"
4436                "  // something\n"
4437                "}",
4438                Style);
4439   verifyFormat("__try {\n"
4440                "  // something\n"
4441                "}\n"
4442                "__finally {\n"
4443                "  // something\n"
4444                "}",
4445                Style);
4446   verifyFormat("@try {\n"
4447                "  // something\n"
4448                "}\n"
4449                "@finally {\n"
4450                "  // something\n"
4451                "}",
4452                Style);
4453   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
4454   verifyFormat("try\n"
4455                "{\n"
4456                "  // something\n"
4457                "}\n"
4458                "catch (...)\n"
4459                "{\n"
4460                "  // something\n"
4461                "}",
4462                Style);
4463   Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
4464   verifyFormat("try\n"
4465                "  {\n"
4466                "  // something white\n"
4467                "  }\n"
4468                "catch (...)\n"
4469                "  {\n"
4470                "  // something white\n"
4471                "  }",
4472                Style);
4473   Style.BreakBeforeBraces = FormatStyle::BS_GNU;
4474   verifyFormat("try\n"
4475                "  {\n"
4476                "    // something\n"
4477                "  }\n"
4478                "catch (...)\n"
4479                "  {\n"
4480                "    // something\n"
4481                "  }",
4482                Style);
4483   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4484   Style.BraceWrapping.BeforeCatch = true;
4485   verifyFormat("try {\n"
4486                "  // something\n"
4487                "}\n"
4488                "catch (...) {\n"
4489                "  // something\n"
4490                "}",
4491                Style);
4492 }
4493 
4494 TEST_F(FormatTest, StaticInitializers) {
4495   verifyFormat("static SomeClass SC = {1, 'a'};");
4496 
4497   verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n"
4498                "    100000000, "
4499                "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};");
4500 
4501   // Here, everything other than the "}" would fit on a line.
4502   verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
4503                "    10000000000000000000000000};");
4504   EXPECT_EQ("S s = {a,\n"
4505             "\n"
4506             "       b};",
4507             format("S s = {\n"
4508                    "  a,\n"
4509                    "\n"
4510                    "  b\n"
4511                    "};"));
4512 
4513   // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
4514   // line. However, the formatting looks a bit off and this probably doesn't
4515   // happen often in practice.
4516   verifyFormat("static int Variable[1] = {\n"
4517                "    {1000000000000000000000000000000000000}};",
4518                getLLVMStyleWithColumns(40));
4519 }
4520 
4521 TEST_F(FormatTest, DesignatedInitializers) {
4522   verifyFormat("const struct A a = {.a = 1, .b = 2};");
4523   verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n"
4524                "                    .bbbbbbbbbb = 2,\n"
4525                "                    .cccccccccc = 3,\n"
4526                "                    .dddddddddd = 4,\n"
4527                "                    .eeeeeeeeee = 5};");
4528   verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
4529                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
4530                "    .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
4531                "    .ccccccccccccccccccccccccccc = 3,\n"
4532                "    .ddddddddddddddddddddddddddd = 4,\n"
4533                "    .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};");
4534 
4535   verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
4536 
4537   verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
4538   verifyFormat("const struct A a = {[1] = aaaaaaaaaa,\n"
4539                "                    [2] = bbbbbbbbbb,\n"
4540                "                    [3] = cccccccccc,\n"
4541                "                    [4] = dddddddddd,\n"
4542                "                    [5] = eeeeeeeeee};");
4543   verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
4544                "    [1] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4545                "    [2] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
4546                "    [3] = cccccccccccccccccccccccccccccccccccccc,\n"
4547                "    [4] = dddddddddddddddddddddddddddddddddddddd,\n"
4548                "    [5] = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee};");
4549 }
4550 
4551 TEST_F(FormatTest, NestedStaticInitializers) {
4552   verifyFormat("static A x = {{{}}};\n");
4553   verifyFormat("static A x = {{{init1, init2, init3, init4},\n"
4554                "               {init1, init2, init3, init4}}};",
4555                getLLVMStyleWithColumns(50));
4556 
4557   verifyFormat("somes Status::global_reps[3] = {\n"
4558                "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
4559                "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
4560                "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};",
4561                getLLVMStyleWithColumns(60));
4562   verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
4563                      "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
4564                      "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
4565                      "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
4566   verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n"
4567                "                  {rect.fRight - rect.fLeft, rect.fBottom - "
4568                "rect.fTop}};");
4569 
4570   verifyFormat(
4571       "SomeArrayOfSomeType a = {\n"
4572       "    {{1, 2, 3},\n"
4573       "     {1, 2, 3},\n"
4574       "     {111111111111111111111111111111, 222222222222222222222222222222,\n"
4575       "      333333333333333333333333333333},\n"
4576       "     {1, 2, 3},\n"
4577       "     {1, 2, 3}}};");
4578   verifyFormat(
4579       "SomeArrayOfSomeType a = {\n"
4580       "    {{1, 2, 3}},\n"
4581       "    {{1, 2, 3}},\n"
4582       "    {{111111111111111111111111111111, 222222222222222222222222222222,\n"
4583       "      333333333333333333333333333333}},\n"
4584       "    {{1, 2, 3}},\n"
4585       "    {{1, 2, 3}}};");
4586 
4587   verifyFormat("struct {\n"
4588                "  unsigned bit;\n"
4589                "  const char *const name;\n"
4590                "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n"
4591                "                 {kOsWin, \"Windows\"},\n"
4592                "                 {kOsLinux, \"Linux\"},\n"
4593                "                 {kOsCrOS, \"Chrome OS\"}};");
4594   verifyFormat("struct {\n"
4595                "  unsigned bit;\n"
4596                "  const char *const name;\n"
4597                "} kBitsToOs[] = {\n"
4598                "    {kOsMac, \"Mac\"},\n"
4599                "    {kOsWin, \"Windows\"},\n"
4600                "    {kOsLinux, \"Linux\"},\n"
4601                "    {kOsCrOS, \"Chrome OS\"},\n"
4602                "};");
4603 }
4604 
4605 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
4606   verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
4607                "                      \\\n"
4608                "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
4609 }
4610 
4611 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
4612   verifyFormat("virtual void write(ELFWriter *writerrr,\n"
4613                "                   OwningPtr<FileOutputBuffer> &buffer) = 0;");
4614 
4615   // Do break defaulted and deleted functions.
4616   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
4617                "    default;",
4618                getLLVMStyleWithColumns(40));
4619   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
4620                "    delete;",
4621                getLLVMStyleWithColumns(40));
4622 }
4623 
4624 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) {
4625   verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3",
4626                getLLVMStyleWithColumns(40));
4627   verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
4628                getLLVMStyleWithColumns(40));
4629   EXPECT_EQ("#define Q                              \\\n"
4630             "  \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\"    \\\n"
4631             "  \"aaaaaaaa.cpp\"",
4632             format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
4633                    getLLVMStyleWithColumns(40)));
4634 }
4635 
4636 TEST_F(FormatTest, UnderstandsLinePPDirective) {
4637   EXPECT_EQ("# 123 \"A string literal\"",
4638             format("   #     123    \"A string literal\""));
4639 }
4640 
4641 TEST_F(FormatTest, LayoutUnknownPPDirective) {
4642   EXPECT_EQ("#;", format("#;"));
4643   verifyFormat("#\n;\n;\n;");
4644 }
4645 
4646 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
4647   EXPECT_EQ("#line 42 \"test\"\n",
4648             format("#  \\\n  line  \\\n  42  \\\n  \"test\"\n"));
4649   EXPECT_EQ("#define A B\n", format("#  \\\n define  \\\n    A  \\\n       B\n",
4650                                     getLLVMStyleWithColumns(12)));
4651 }
4652 
4653 TEST_F(FormatTest, EndOfFileEndsPPDirective) {
4654   EXPECT_EQ("#line 42 \"test\"",
4655             format("#  \\\n  line  \\\n  42  \\\n  \"test\""));
4656   EXPECT_EQ("#define A B", format("#  \\\n define  \\\n    A  \\\n       B"));
4657 }
4658 
4659 TEST_F(FormatTest, DoesntRemoveUnknownTokens) {
4660   verifyFormat("#define A \\x20");
4661   verifyFormat("#define A \\ x20");
4662   EXPECT_EQ("#define A \\ x20", format("#define A \\   x20"));
4663   verifyFormat("#define A ''");
4664   verifyFormat("#define A ''qqq");
4665   verifyFormat("#define A `qqq");
4666   verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");");
4667   EXPECT_EQ("const char *c = STRINGIFY(\n"
4668             "\\na : b);",
4669             format("const char * c = STRINGIFY(\n"
4670                    "\\na : b);"));
4671 
4672   verifyFormat("a\r\\");
4673   verifyFormat("a\v\\");
4674   verifyFormat("a\f\\");
4675 }
4676 
4677 TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) {
4678   FormatStyle style = getChromiumStyle(FormatStyle::LK_Cpp);
4679   style.IndentWidth = 4;
4680   style.PPIndentWidth = 1;
4681 
4682   style.IndentPPDirectives = FormatStyle::PPDIS_None;
4683   verifyFormat("#ifdef __linux__\n"
4684                "void foo() {\n"
4685                "    int x = 0;\n"
4686                "}\n"
4687                "#define FOO\n"
4688                "#endif\n"
4689                "void bar() {\n"
4690                "    int y = 0;\n"
4691                "}\n",
4692                style);
4693 
4694   style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
4695   verifyFormat("#ifdef __linux__\n"
4696                "void foo() {\n"
4697                "    int x = 0;\n"
4698                "}\n"
4699                "# define FOO foo\n"
4700                "#endif\n"
4701                "void bar() {\n"
4702                "    int y = 0;\n"
4703                "}\n",
4704                style);
4705 
4706   style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash;
4707   verifyFormat("#ifdef __linux__\n"
4708                "void foo() {\n"
4709                "    int x = 0;\n"
4710                "}\n"
4711                " #define FOO foo\n"
4712                "#endif\n"
4713                "void bar() {\n"
4714                "    int y = 0;\n"
4715                "}\n",
4716                style);
4717 }
4718 
4719 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
4720   verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
4721   verifyFormat("#define A( \\\n    BB)", getLLVMStyleWithColumns(12));
4722   verifyFormat("#define A( \\\n    A, B)", getLLVMStyleWithColumns(12));
4723   // FIXME: We never break before the macro name.
4724   verifyFormat("#define AA( \\\n    B)", getLLVMStyleWithColumns(12));
4725 
4726   verifyFormat("#define A A\n#define A A");
4727   verifyFormat("#define A(X) A\n#define A A");
4728 
4729   verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
4730   verifyFormat("#define Something    \\\n  Other", getLLVMStyleWithColumns(22));
4731 }
4732 
4733 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
4734   EXPECT_EQ("// somecomment\n"
4735             "#include \"a.h\"\n"
4736             "#define A(  \\\n"
4737             "    A, B)\n"
4738             "#include \"b.h\"\n"
4739             "// somecomment\n",
4740             format("  // somecomment\n"
4741                    "  #include \"a.h\"\n"
4742                    "#define A(A,\\\n"
4743                    "    B)\n"
4744                    "    #include \"b.h\"\n"
4745                    " // somecomment\n",
4746                    getLLVMStyleWithColumns(13)));
4747 }
4748 
4749 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); }
4750 
4751 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
4752   EXPECT_EQ("#define A    \\\n"
4753             "  c;         \\\n"
4754             "  e;\n"
4755             "f;",
4756             format("#define A c; e;\n"
4757                    "f;",
4758                    getLLVMStyleWithColumns(14)));
4759 }
4760 
4761 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
4762 
4763 TEST_F(FormatTest, MacroDefinitionInsideStatement) {
4764   EXPECT_EQ("int x,\n"
4765             "#define A\n"
4766             "    y;",
4767             format("int x,\n#define A\ny;"));
4768 }
4769 
4770 TEST_F(FormatTest, HashInMacroDefinition) {
4771   EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle()));
4772   EXPECT_EQ("#define A(c) u#c", format("#define A(c) u#c", getLLVMStyle()));
4773   EXPECT_EQ("#define A(c) U#c", format("#define A(c) U#c", getLLVMStyle()));
4774   EXPECT_EQ("#define A(c) u8#c", format("#define A(c) u8#c", getLLVMStyle()));
4775   EXPECT_EQ("#define A(c) LR#c", format("#define A(c) LR#c", getLLVMStyle()));
4776   EXPECT_EQ("#define A(c) uR#c", format("#define A(c) uR#c", getLLVMStyle()));
4777   EXPECT_EQ("#define A(c) UR#c", format("#define A(c) UR#c", getLLVMStyle()));
4778   EXPECT_EQ("#define A(c) u8R#c", format("#define A(c) u8R#c", getLLVMStyle()));
4779   verifyFormat("#define A \\\n  b #c;", getLLVMStyleWithColumns(11));
4780   verifyFormat("#define A  \\\n"
4781                "  {        \\\n"
4782                "    f(#c); \\\n"
4783                "  }",
4784                getLLVMStyleWithColumns(11));
4785 
4786   verifyFormat("#define A(X)         \\\n"
4787                "  void function##X()",
4788                getLLVMStyleWithColumns(22));
4789 
4790   verifyFormat("#define A(a, b, c)   \\\n"
4791                "  void a##b##c()",
4792                getLLVMStyleWithColumns(22));
4793 
4794   verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
4795 }
4796 
4797 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
4798   EXPECT_EQ("#define A (x)", format("#define A (x)"));
4799   EXPECT_EQ("#define A(x)", format("#define A(x)"));
4800 
4801   FormatStyle Style = getLLVMStyle();
4802   Style.SpaceBeforeParens = FormatStyle::SBPO_Never;
4803   verifyFormat("#define true ((foo)1)", Style);
4804   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
4805   verifyFormat("#define false((foo)0)", Style);
4806 }
4807 
4808 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
4809   EXPECT_EQ("#define A b;", format("#define A \\\n"
4810                                    "          \\\n"
4811                                    "  b;",
4812                                    getLLVMStyleWithColumns(25)));
4813   EXPECT_EQ("#define A \\\n"
4814             "          \\\n"
4815             "  a;      \\\n"
4816             "  b;",
4817             format("#define A \\\n"
4818                    "          \\\n"
4819                    "  a;      \\\n"
4820                    "  b;",
4821                    getLLVMStyleWithColumns(11)));
4822   EXPECT_EQ("#define A \\\n"
4823             "  a;      \\\n"
4824             "          \\\n"
4825             "  b;",
4826             format("#define A \\\n"
4827                    "  a;      \\\n"
4828                    "          \\\n"
4829                    "  b;",
4830                    getLLVMStyleWithColumns(11)));
4831 }
4832 
4833 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
4834   verifyIncompleteFormat("#define A :");
4835   verifyFormat("#define SOMECASES  \\\n"
4836                "  case 1:          \\\n"
4837                "  case 2\n",
4838                getLLVMStyleWithColumns(20));
4839   verifyFormat("#define MACRO(a) \\\n"
4840                "  if (a)         \\\n"
4841                "    f();         \\\n"
4842                "  else           \\\n"
4843                "    g()",
4844                getLLVMStyleWithColumns(18));
4845   verifyFormat("#define A template <typename T>");
4846   verifyIncompleteFormat("#define STR(x) #x\n"
4847                          "f(STR(this_is_a_string_literal{));");
4848   verifyFormat("#pragma omp threadprivate( \\\n"
4849                "    y)), // expected-warning",
4850                getLLVMStyleWithColumns(28));
4851   verifyFormat("#d, = };");
4852   verifyFormat("#if \"a");
4853   verifyIncompleteFormat("({\n"
4854                          "#define b     \\\n"
4855                          "  }           \\\n"
4856                          "  a\n"
4857                          "a",
4858                          getLLVMStyleWithColumns(15));
4859   verifyFormat("#define A     \\\n"
4860                "  {           \\\n"
4861                "    {\n"
4862                "#define B     \\\n"
4863                "  }           \\\n"
4864                "  }",
4865                getLLVMStyleWithColumns(15));
4866   verifyNoCrash("#if a\na(\n#else\n#endif\n{a");
4867   verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}");
4868   verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};");
4869   verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() {      \n)}");
4870 }
4871 
4872 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
4873   verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
4874   EXPECT_EQ("class A : public QObject {\n"
4875             "  Q_OBJECT\n"
4876             "\n"
4877             "  A() {}\n"
4878             "};",
4879             format("class A  :  public QObject {\n"
4880                    "     Q_OBJECT\n"
4881                    "\n"
4882                    "  A() {\n}\n"
4883                    "}  ;"));
4884   EXPECT_EQ("MACRO\n"
4885             "/*static*/ int i;",
4886             format("MACRO\n"
4887                    " /*static*/ int   i;"));
4888   EXPECT_EQ("SOME_MACRO\n"
4889             "namespace {\n"
4890             "void f();\n"
4891             "} // namespace",
4892             format("SOME_MACRO\n"
4893                    "  namespace    {\n"
4894                    "void   f(  );\n"
4895                    "} // namespace"));
4896   // Only if the identifier contains at least 5 characters.
4897   EXPECT_EQ("HTTP f();", format("HTTP\nf();"));
4898   EXPECT_EQ("MACRO\nf();", format("MACRO\nf();"));
4899   // Only if everything is upper case.
4900   EXPECT_EQ("class A : public QObject {\n"
4901             "  Q_Object A() {}\n"
4902             "};",
4903             format("class A  :  public QObject {\n"
4904                    "     Q_Object\n"
4905                    "  A() {\n}\n"
4906                    "}  ;"));
4907 
4908   // Only if the next line can actually start an unwrapped line.
4909   EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;",
4910             format("SOME_WEIRD_LOG_MACRO\n"
4911                    "<< SomeThing;"));
4912 
4913   verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
4914                "(n, buffers))\n",
4915                getChromiumStyle(FormatStyle::LK_Cpp));
4916 
4917   // See PR41483
4918   EXPECT_EQ("/**/ FOO(a)\n"
4919             "FOO(b)",
4920             format("/**/ FOO(a)\n"
4921                    "FOO(b)"));
4922 }
4923 
4924 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
4925   EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
4926             "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
4927             "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
4928             "class X {};\n"
4929             "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
4930             "int *createScopDetectionPass() { return 0; }",
4931             format("  INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
4932                    "  INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
4933                    "  INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
4934                    "  class X {};\n"
4935                    "  INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
4936                    "  int *createScopDetectionPass() { return 0; }"));
4937   // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
4938   // braces, so that inner block is indented one level more.
4939   EXPECT_EQ("int q() {\n"
4940             "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
4941             "  IPC_MESSAGE_HANDLER(xxx, qqq)\n"
4942             "  IPC_END_MESSAGE_MAP()\n"
4943             "}",
4944             format("int q() {\n"
4945                    "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
4946                    "    IPC_MESSAGE_HANDLER(xxx, qqq)\n"
4947                    "  IPC_END_MESSAGE_MAP()\n"
4948                    "}"));
4949 
4950   // Same inside macros.
4951   EXPECT_EQ("#define LIST(L) \\\n"
4952             "  L(A)          \\\n"
4953             "  L(B)          \\\n"
4954             "  L(C)",
4955             format("#define LIST(L) \\\n"
4956                    "  L(A) \\\n"
4957                    "  L(B) \\\n"
4958                    "  L(C)",
4959                    getGoogleStyle()));
4960 
4961   // These must not be recognized as macros.
4962   EXPECT_EQ("int q() {\n"
4963             "  f(x);\n"
4964             "  f(x) {}\n"
4965             "  f(x)->g();\n"
4966             "  f(x)->*g();\n"
4967             "  f(x).g();\n"
4968             "  f(x) = x;\n"
4969             "  f(x) += x;\n"
4970             "  f(x) -= x;\n"
4971             "  f(x) *= x;\n"
4972             "  f(x) /= x;\n"
4973             "  f(x) %= x;\n"
4974             "  f(x) &= x;\n"
4975             "  f(x) |= x;\n"
4976             "  f(x) ^= x;\n"
4977             "  f(x) >>= x;\n"
4978             "  f(x) <<= x;\n"
4979             "  f(x)[y].z();\n"
4980             "  LOG(INFO) << x;\n"
4981             "  ifstream(x) >> x;\n"
4982             "}\n",
4983             format("int q() {\n"
4984                    "  f(x)\n;\n"
4985                    "  f(x)\n {}\n"
4986                    "  f(x)\n->g();\n"
4987                    "  f(x)\n->*g();\n"
4988                    "  f(x)\n.g();\n"
4989                    "  f(x)\n = x;\n"
4990                    "  f(x)\n += x;\n"
4991                    "  f(x)\n -= x;\n"
4992                    "  f(x)\n *= x;\n"
4993                    "  f(x)\n /= x;\n"
4994                    "  f(x)\n %= x;\n"
4995                    "  f(x)\n &= x;\n"
4996                    "  f(x)\n |= x;\n"
4997                    "  f(x)\n ^= x;\n"
4998                    "  f(x)\n >>= x;\n"
4999                    "  f(x)\n <<= x;\n"
5000                    "  f(x)\n[y].z();\n"
5001                    "  LOG(INFO)\n << x;\n"
5002                    "  ifstream(x)\n >> x;\n"
5003                    "}\n"));
5004   EXPECT_EQ("int q() {\n"
5005             "  F(x)\n"
5006             "  if (1) {\n"
5007             "  }\n"
5008             "  F(x)\n"
5009             "  while (1) {\n"
5010             "  }\n"
5011             "  F(x)\n"
5012             "  G(x);\n"
5013             "  F(x)\n"
5014             "  try {\n"
5015             "    Q();\n"
5016             "  } catch (...) {\n"
5017             "  }\n"
5018             "}\n",
5019             format("int q() {\n"
5020                    "F(x)\n"
5021                    "if (1) {}\n"
5022                    "F(x)\n"
5023                    "while (1) {}\n"
5024                    "F(x)\n"
5025                    "G(x);\n"
5026                    "F(x)\n"
5027                    "try { Q(); } catch (...) {}\n"
5028                    "}\n"));
5029   EXPECT_EQ("class A {\n"
5030             "  A() : t(0) {}\n"
5031             "  A(int i) noexcept() : {}\n"
5032             "  A(X x)\n" // FIXME: function-level try blocks are broken.
5033             "  try : t(0) {\n"
5034             "  } catch (...) {\n"
5035             "  }\n"
5036             "};",
5037             format("class A {\n"
5038                    "  A()\n : t(0) {}\n"
5039                    "  A(int i)\n noexcept() : {}\n"
5040                    "  A(X x)\n"
5041                    "  try : t(0) {} catch (...) {}\n"
5042                    "};"));
5043   FormatStyle Style = getLLVMStyle();
5044   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
5045   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
5046   Style.BraceWrapping.AfterFunction = true;
5047   EXPECT_EQ("void f()\n"
5048             "try\n"
5049             "{\n"
5050             "}",
5051             format("void f() try {\n"
5052                    "}",
5053                    Style));
5054   EXPECT_EQ("class SomeClass {\n"
5055             "public:\n"
5056             "  SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
5057             "};",
5058             format("class SomeClass {\n"
5059                    "public:\n"
5060                    "  SomeClass()\n"
5061                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
5062                    "};"));
5063   EXPECT_EQ("class SomeClass {\n"
5064             "public:\n"
5065             "  SomeClass()\n"
5066             "      EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
5067             "};",
5068             format("class SomeClass {\n"
5069                    "public:\n"
5070                    "  SomeClass()\n"
5071                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
5072                    "};",
5073                    getLLVMStyleWithColumns(40)));
5074 
5075   verifyFormat("MACRO(>)");
5076 
5077   // Some macros contain an implicit semicolon.
5078   Style = getLLVMStyle();
5079   Style.StatementMacros.push_back("FOO");
5080   verifyFormat("FOO(a) int b = 0;");
5081   verifyFormat("FOO(a)\n"
5082                "int b = 0;",
5083                Style);
5084   verifyFormat("FOO(a);\n"
5085                "int b = 0;",
5086                Style);
5087   verifyFormat("FOO(argc, argv, \"4.0.2\")\n"
5088                "int b = 0;",
5089                Style);
5090   verifyFormat("FOO()\n"
5091                "int b = 0;",
5092                Style);
5093   verifyFormat("FOO\n"
5094                "int b = 0;",
5095                Style);
5096   verifyFormat("void f() {\n"
5097                "  FOO(a)\n"
5098                "  return a;\n"
5099                "}",
5100                Style);
5101   verifyFormat("FOO(a)\n"
5102                "FOO(b)",
5103                Style);
5104   verifyFormat("int a = 0;\n"
5105                "FOO(b)\n"
5106                "int c = 0;",
5107                Style);
5108   verifyFormat("int a = 0;\n"
5109                "int x = FOO(a)\n"
5110                "int b = 0;",
5111                Style);
5112   verifyFormat("void foo(int a) { FOO(a) }\n"
5113                "uint32_t bar() {}",
5114                Style);
5115 }
5116 
5117 TEST_F(FormatTest, FormatsMacrosWithZeroColumnWidth) {
5118   FormatStyle ZeroColumn = getLLVMStyleWithColumns(0);
5119 
5120   verifyFormat("#define A LOOOOOOOOOOOOOOOOOOONG() LOOOOOOOOOOOOOOOOOOONG()",
5121                ZeroColumn);
5122 }
5123 
5124 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
5125   verifyFormat("#define A \\\n"
5126                "  f({     \\\n"
5127                "    g();  \\\n"
5128                "  });",
5129                getLLVMStyleWithColumns(11));
5130 }
5131 
5132 TEST_F(FormatTest, IndentPreprocessorDirectives) {
5133   FormatStyle Style = getLLVMStyleWithColumns(40);
5134   Style.IndentPPDirectives = FormatStyle::PPDIS_None;
5135   verifyFormat("#ifdef _WIN32\n"
5136                "#define A 0\n"
5137                "#ifdef VAR2\n"
5138                "#define B 1\n"
5139                "#include <someheader.h>\n"
5140                "#define MACRO                          \\\n"
5141                "  some_very_long_func_aaaaaaaaaa();\n"
5142                "#endif\n"
5143                "#else\n"
5144                "#define A 1\n"
5145                "#endif",
5146                Style);
5147   Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
5148   verifyFormat("#ifdef _WIN32\n"
5149                "#  define A 0\n"
5150                "#  ifdef VAR2\n"
5151                "#    define B 1\n"
5152                "#    include <someheader.h>\n"
5153                "#    define MACRO                      \\\n"
5154                "      some_very_long_func_aaaaaaaaaa();\n"
5155                "#  endif\n"
5156                "#else\n"
5157                "#  define A 1\n"
5158                "#endif",
5159                Style);
5160   verifyFormat("#if A\n"
5161                "#  define MACRO                        \\\n"
5162                "    void a(int x) {                    \\\n"
5163                "      b();                             \\\n"
5164                "      c();                             \\\n"
5165                "      d();                             \\\n"
5166                "      e();                             \\\n"
5167                "      f();                             \\\n"
5168                "    }\n"
5169                "#endif",
5170                Style);
5171   // Comments before include guard.
5172   verifyFormat("// file comment\n"
5173                "// file comment\n"
5174                "#ifndef HEADER_H\n"
5175                "#define HEADER_H\n"
5176                "code();\n"
5177                "#endif",
5178                Style);
5179   // Test with include guards.
5180   verifyFormat("#ifndef HEADER_H\n"
5181                "#define HEADER_H\n"
5182                "code();\n"
5183                "#endif",
5184                Style);
5185   // Include guards must have a #define with the same variable immediately
5186   // after #ifndef.
5187   verifyFormat("#ifndef NOT_GUARD\n"
5188                "#  define FOO\n"
5189                "code();\n"
5190                "#endif",
5191                Style);
5192 
5193   // Include guards must cover the entire file.
5194   verifyFormat("code();\n"
5195                "code();\n"
5196                "#ifndef NOT_GUARD\n"
5197                "#  define NOT_GUARD\n"
5198                "code();\n"
5199                "#endif",
5200                Style);
5201   verifyFormat("#ifndef NOT_GUARD\n"
5202                "#  define NOT_GUARD\n"
5203                "code();\n"
5204                "#endif\n"
5205                "code();",
5206                Style);
5207   // Test with trailing blank lines.
5208   verifyFormat("#ifndef HEADER_H\n"
5209                "#define HEADER_H\n"
5210                "code();\n"
5211                "#endif\n",
5212                Style);
5213   // Include guards don't have #else.
5214   verifyFormat("#ifndef NOT_GUARD\n"
5215                "#  define NOT_GUARD\n"
5216                "code();\n"
5217                "#else\n"
5218                "#endif",
5219                Style);
5220   verifyFormat("#ifndef NOT_GUARD\n"
5221                "#  define NOT_GUARD\n"
5222                "code();\n"
5223                "#elif FOO\n"
5224                "#endif",
5225                Style);
5226   // Non-identifier #define after potential include guard.
5227   verifyFormat("#ifndef FOO\n"
5228                "#  define 1\n"
5229                "#endif\n",
5230                Style);
5231   // #if closes past last non-preprocessor line.
5232   verifyFormat("#ifndef FOO\n"
5233                "#define FOO\n"
5234                "#if 1\n"
5235                "int i;\n"
5236                "#  define A 0\n"
5237                "#endif\n"
5238                "#endif\n",
5239                Style);
5240   // Don't crash if there is an #elif directive without a condition.
5241   verifyFormat("#if 1\n"
5242                "int x;\n"
5243                "#elif\n"
5244                "int y;\n"
5245                "#else\n"
5246                "int z;\n"
5247                "#endif",
5248                Style);
5249   // FIXME: This doesn't handle the case where there's code between the
5250   // #ifndef and #define but all other conditions hold. This is because when
5251   // the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the
5252   // previous code line yet, so we can't detect it.
5253   EXPECT_EQ("#ifndef NOT_GUARD\n"
5254             "code();\n"
5255             "#define NOT_GUARD\n"
5256             "code();\n"
5257             "#endif",
5258             format("#ifndef NOT_GUARD\n"
5259                    "code();\n"
5260                    "#  define NOT_GUARD\n"
5261                    "code();\n"
5262                    "#endif",
5263                    Style));
5264   // FIXME: This doesn't handle cases where legitimate preprocessor lines may
5265   // be outside an include guard. Examples are #pragma once and
5266   // #pragma GCC diagnostic, or anything else that does not change the meaning
5267   // of the file if it's included multiple times.
5268   EXPECT_EQ("#ifdef WIN32\n"
5269             "#  pragma once\n"
5270             "#endif\n"
5271             "#ifndef HEADER_H\n"
5272             "#  define HEADER_H\n"
5273             "code();\n"
5274             "#endif",
5275             format("#ifdef WIN32\n"
5276                    "#  pragma once\n"
5277                    "#endif\n"
5278                    "#ifndef HEADER_H\n"
5279                    "#define HEADER_H\n"
5280                    "code();\n"
5281                    "#endif",
5282                    Style));
5283   // FIXME: This does not detect when there is a single non-preprocessor line
5284   // in front of an include-guard-like structure where other conditions hold
5285   // because ScopedLineState hides the line.
5286   EXPECT_EQ("code();\n"
5287             "#ifndef HEADER_H\n"
5288             "#define HEADER_H\n"
5289             "code();\n"
5290             "#endif",
5291             format("code();\n"
5292                    "#ifndef HEADER_H\n"
5293                    "#  define HEADER_H\n"
5294                    "code();\n"
5295                    "#endif",
5296                    Style));
5297   // Keep comments aligned with #, otherwise indent comments normally. These
5298   // tests cannot use verifyFormat because messUp manipulates leading
5299   // whitespace.
5300   {
5301     const char *Expected = ""
5302                            "void f() {\n"
5303                            "#if 1\n"
5304                            "// Preprocessor aligned.\n"
5305                            "#  define A 0\n"
5306                            "  // Code. Separated by blank line.\n"
5307                            "\n"
5308                            "#  define B 0\n"
5309                            "  // Code. Not aligned with #\n"
5310                            "#  define C 0\n"
5311                            "#endif";
5312     const char *ToFormat = ""
5313                            "void f() {\n"
5314                            "#if 1\n"
5315                            "// Preprocessor aligned.\n"
5316                            "#  define A 0\n"
5317                            "// Code. Separated by blank line.\n"
5318                            "\n"
5319                            "#  define B 0\n"
5320                            "   // Code. Not aligned with #\n"
5321                            "#  define C 0\n"
5322                            "#endif";
5323     EXPECT_EQ(Expected, format(ToFormat, Style));
5324     EXPECT_EQ(Expected, format(Expected, Style));
5325   }
5326   // Keep block quotes aligned.
5327   {
5328     const char *Expected = ""
5329                            "void f() {\n"
5330                            "#if 1\n"
5331                            "/* Preprocessor aligned. */\n"
5332                            "#  define A 0\n"
5333                            "  /* Code. Separated by blank line. */\n"
5334                            "\n"
5335                            "#  define B 0\n"
5336                            "  /* Code. Not aligned with # */\n"
5337                            "#  define C 0\n"
5338                            "#endif";
5339     const char *ToFormat = ""
5340                            "void f() {\n"
5341                            "#if 1\n"
5342                            "/* Preprocessor aligned. */\n"
5343                            "#  define A 0\n"
5344                            "/* Code. Separated by blank line. */\n"
5345                            "\n"
5346                            "#  define B 0\n"
5347                            "   /* Code. Not aligned with # */\n"
5348                            "#  define C 0\n"
5349                            "#endif";
5350     EXPECT_EQ(Expected, format(ToFormat, Style));
5351     EXPECT_EQ(Expected, format(Expected, Style));
5352   }
5353   // Keep comments aligned with un-indented directives.
5354   {
5355     const char *Expected = ""
5356                            "void f() {\n"
5357                            "// Preprocessor aligned.\n"
5358                            "#define A 0\n"
5359                            "  // Code. Separated by blank line.\n"
5360                            "\n"
5361                            "#define B 0\n"
5362                            "  // Code. Not aligned with #\n"
5363                            "#define C 0\n";
5364     const char *ToFormat = ""
5365                            "void f() {\n"
5366                            "// Preprocessor aligned.\n"
5367                            "#define A 0\n"
5368                            "// Code. Separated by blank line.\n"
5369                            "\n"
5370                            "#define B 0\n"
5371                            "   // Code. Not aligned with #\n"
5372                            "#define C 0\n";
5373     EXPECT_EQ(Expected, format(ToFormat, Style));
5374     EXPECT_EQ(Expected, format(Expected, Style));
5375   }
5376   // Test AfterHash with tabs.
5377   {
5378     FormatStyle Tabbed = Style;
5379     Tabbed.UseTab = FormatStyle::UT_Always;
5380     Tabbed.IndentWidth = 8;
5381     Tabbed.TabWidth = 8;
5382     verifyFormat("#ifdef _WIN32\n"
5383                  "#\tdefine A 0\n"
5384                  "#\tifdef VAR2\n"
5385                  "#\t\tdefine B 1\n"
5386                  "#\t\tinclude <someheader.h>\n"
5387                  "#\t\tdefine MACRO          \\\n"
5388                  "\t\t\tsome_very_long_func_aaaaaaaaaa();\n"
5389                  "#\tendif\n"
5390                  "#else\n"
5391                  "#\tdefine A 1\n"
5392                  "#endif",
5393                  Tabbed);
5394   }
5395 
5396   // Regression test: Multiline-macro inside include guards.
5397   verifyFormat("#ifndef HEADER_H\n"
5398                "#define HEADER_H\n"
5399                "#define A()        \\\n"
5400                "  int i;           \\\n"
5401                "  int j;\n"
5402                "#endif // HEADER_H",
5403                getLLVMStyleWithColumns(20));
5404 
5405   Style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash;
5406   // Basic before hash indent tests
5407   verifyFormat("#ifdef _WIN32\n"
5408                "  #define A 0\n"
5409                "  #ifdef VAR2\n"
5410                "    #define B 1\n"
5411                "    #include <someheader.h>\n"
5412                "    #define MACRO                      \\\n"
5413                "      some_very_long_func_aaaaaaaaaa();\n"
5414                "  #endif\n"
5415                "#else\n"
5416                "  #define A 1\n"
5417                "#endif",
5418                Style);
5419   verifyFormat("#if A\n"
5420                "  #define MACRO                        \\\n"
5421                "    void a(int x) {                    \\\n"
5422                "      b();                             \\\n"
5423                "      c();                             \\\n"
5424                "      d();                             \\\n"
5425                "      e();                             \\\n"
5426                "      f();                             \\\n"
5427                "    }\n"
5428                "#endif",
5429                Style);
5430   // Keep comments aligned with indented directives. These
5431   // tests cannot use verifyFormat because messUp manipulates leading
5432   // whitespace.
5433   {
5434     const char *Expected = "void f() {\n"
5435                            "// Aligned to preprocessor.\n"
5436                            "#if 1\n"
5437                            "  // Aligned to code.\n"
5438                            "  int a;\n"
5439                            "  #if 1\n"
5440                            "    // Aligned to preprocessor.\n"
5441                            "    #define A 0\n"
5442                            "  // Aligned to code.\n"
5443                            "  int b;\n"
5444                            "  #endif\n"
5445                            "#endif\n"
5446                            "}";
5447     const char *ToFormat = "void f() {\n"
5448                            "// Aligned to preprocessor.\n"
5449                            "#if 1\n"
5450                            "// Aligned to code.\n"
5451                            "int a;\n"
5452                            "#if 1\n"
5453                            "// Aligned to preprocessor.\n"
5454                            "#define A 0\n"
5455                            "// Aligned to code.\n"
5456                            "int b;\n"
5457                            "#endif\n"
5458                            "#endif\n"
5459                            "}";
5460     EXPECT_EQ(Expected, format(ToFormat, Style));
5461     EXPECT_EQ(Expected, format(Expected, Style));
5462   }
5463   {
5464     const char *Expected = "void f() {\n"
5465                            "/* Aligned to preprocessor. */\n"
5466                            "#if 1\n"
5467                            "  /* Aligned to code. */\n"
5468                            "  int a;\n"
5469                            "  #if 1\n"
5470                            "    /* Aligned to preprocessor. */\n"
5471                            "    #define A 0\n"
5472                            "  /* Aligned to code. */\n"
5473                            "  int b;\n"
5474                            "  #endif\n"
5475                            "#endif\n"
5476                            "}";
5477     const char *ToFormat = "void f() {\n"
5478                            "/* Aligned to preprocessor. */\n"
5479                            "#if 1\n"
5480                            "/* Aligned to code. */\n"
5481                            "int a;\n"
5482                            "#if 1\n"
5483                            "/* Aligned to preprocessor. */\n"
5484                            "#define A 0\n"
5485                            "/* Aligned to code. */\n"
5486                            "int b;\n"
5487                            "#endif\n"
5488                            "#endif\n"
5489                            "}";
5490     EXPECT_EQ(Expected, format(ToFormat, Style));
5491     EXPECT_EQ(Expected, format(Expected, Style));
5492   }
5493 
5494   // Test single comment before preprocessor
5495   verifyFormat("// Comment\n"
5496                "\n"
5497                "#if 1\n"
5498                "#endif",
5499                Style);
5500 }
5501 
5502 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
5503   verifyFormat("{\n  { a #c; }\n}");
5504 }
5505 
5506 TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
5507   EXPECT_EQ("#define A \\\n  {       \\\n    {\nint i;",
5508             format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
5509   EXPECT_EQ("#define A \\\n  }       \\\n  }\nint i;",
5510             format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
5511 }
5512 
5513 TEST_F(FormatTest, EscapedNewlines) {
5514   FormatStyle Narrow = getLLVMStyleWithColumns(11);
5515   EXPECT_EQ("#define A \\\n  int i;  \\\n  int j;",
5516             format("#define A \\\nint i;\\\n  int j;", Narrow));
5517   EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;"));
5518   EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
5519   EXPECT_EQ("/* \\  \\  \\\n */", format("\\\n/* \\  \\  \\\n */"));
5520   EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>"));
5521 
5522   FormatStyle AlignLeft = getLLVMStyle();
5523   AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
5524   EXPECT_EQ("#define MACRO(x) \\\n"
5525             "private:         \\\n"
5526             "  int x(int a);\n",
5527             format("#define MACRO(x) \\\n"
5528                    "private:         \\\n"
5529                    "  int x(int a);\n",
5530                    AlignLeft));
5531 
5532   // CRLF line endings
5533   EXPECT_EQ("#define A \\\r\n  int i;  \\\r\n  int j;",
5534             format("#define A \\\r\nint i;\\\r\n  int j;", Narrow));
5535   EXPECT_EQ("#define A\r\n\r\nint i;", format("#define A \\\r\n\r\n int i;"));
5536   EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
5537   EXPECT_EQ("/* \\  \\  \\\r\n */", format("\\\r\n/* \\  \\  \\\r\n */"));
5538   EXPECT_EQ("<a\r\n\\\\\r\n>", format("<a\r\n\\\\\r\n>"));
5539   EXPECT_EQ("#define MACRO(x) \\\r\n"
5540             "private:         \\\r\n"
5541             "  int x(int a);\r\n",
5542             format("#define MACRO(x) \\\r\n"
5543                    "private:         \\\r\n"
5544                    "  int x(int a);\r\n",
5545                    AlignLeft));
5546 
5547   FormatStyle DontAlign = getLLVMStyle();
5548   DontAlign.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
5549   DontAlign.MaxEmptyLinesToKeep = 3;
5550   // FIXME: can't use verifyFormat here because the newline before
5551   // "public:" is not inserted the first time it's reformatted
5552   EXPECT_EQ("#define A \\\n"
5553             "  class Foo { \\\n"
5554             "    void bar(); \\\n"
5555             "\\\n"
5556             "\\\n"
5557             "\\\n"
5558             "  public: \\\n"
5559             "    void baz(); \\\n"
5560             "  };",
5561             format("#define A \\\n"
5562                    "  class Foo { \\\n"
5563                    "    void bar(); \\\n"
5564                    "\\\n"
5565                    "\\\n"
5566                    "\\\n"
5567                    "  public: \\\n"
5568                    "    void baz(); \\\n"
5569                    "  };",
5570                    DontAlign));
5571 }
5572 
5573 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
5574   verifyFormat("#define A \\\n"
5575                "  int v(  \\\n"
5576                "      a); \\\n"
5577                "  int i;",
5578                getLLVMStyleWithColumns(11));
5579 }
5580 
5581 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
5582   EXPECT_EQ(
5583       "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
5584       "                      \\\n"
5585       "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
5586       "\n"
5587       "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
5588       "    aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
5589       format("  #define   ALooooooooooooooooooooooooooooooooooooooongMacro("
5590              "\\\n"
5591              "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
5592              "  \n"
5593              "   AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
5594              "  aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
5595 }
5596 
5597 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
5598   EXPECT_EQ("int\n"
5599             "#define A\n"
5600             "    a;",
5601             format("int\n#define A\na;"));
5602   verifyFormat("functionCallTo(\n"
5603                "    someOtherFunction(\n"
5604                "        withSomeParameters, whichInSequence,\n"
5605                "        areLongerThanALine(andAnotherCall,\n"
5606                "#define A B\n"
5607                "                           withMoreParamters,\n"
5608                "                           whichStronglyInfluenceTheLayout),\n"
5609                "        andMoreParameters),\n"
5610                "    trailing);",
5611                getLLVMStyleWithColumns(69));
5612   verifyFormat("Foo::Foo()\n"
5613                "#ifdef BAR\n"
5614                "    : baz(0)\n"
5615                "#endif\n"
5616                "{\n"
5617                "}");
5618   verifyFormat("void f() {\n"
5619                "  if (true)\n"
5620                "#ifdef A\n"
5621                "    f(42);\n"
5622                "  x();\n"
5623                "#else\n"
5624                "    g();\n"
5625                "  x();\n"
5626                "#endif\n"
5627                "}");
5628   verifyFormat("void f(param1, param2,\n"
5629                "       param3,\n"
5630                "#ifdef A\n"
5631                "       param4(param5,\n"
5632                "#ifdef A1\n"
5633                "              param6,\n"
5634                "#ifdef A2\n"
5635                "              param7),\n"
5636                "#else\n"
5637                "              param8),\n"
5638                "       param9,\n"
5639                "#endif\n"
5640                "       param10,\n"
5641                "#endif\n"
5642                "       param11)\n"
5643                "#else\n"
5644                "       param12)\n"
5645                "#endif\n"
5646                "{\n"
5647                "  x();\n"
5648                "}",
5649                getLLVMStyleWithColumns(28));
5650   verifyFormat("#if 1\n"
5651                "int i;");
5652   verifyFormat("#if 1\n"
5653                "#endif\n"
5654                "#if 1\n"
5655                "#else\n"
5656                "#endif\n");
5657   verifyFormat("DEBUG({\n"
5658                "  return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5659                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
5660                "});\n"
5661                "#if a\n"
5662                "#else\n"
5663                "#endif");
5664 
5665   verifyIncompleteFormat("void f(\n"
5666                          "#if A\n"
5667                          ");\n"
5668                          "#else\n"
5669                          "#endif");
5670 }
5671 
5672 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
5673   verifyFormat("#endif\n"
5674                "#if B");
5675 }
5676 
5677 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
5678   FormatStyle SingleLine = getLLVMStyle();
5679   SingleLine.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse;
5680   verifyFormat("#if 0\n"
5681                "#elif 1\n"
5682                "#endif\n"
5683                "void foo() {\n"
5684                "  if (test) foo2();\n"
5685                "}",
5686                SingleLine);
5687 }
5688 
5689 TEST_F(FormatTest, LayoutBlockInsideParens) {
5690   verifyFormat("functionCall({ int i; });");
5691   verifyFormat("functionCall({\n"
5692                "  int i;\n"
5693                "  int j;\n"
5694                "});");
5695   verifyFormat("functionCall(\n"
5696                "    {\n"
5697                "      int i;\n"
5698                "      int j;\n"
5699                "    },\n"
5700                "    aaaa, bbbb, cccc);");
5701   verifyFormat("functionA(functionB({\n"
5702                "            int i;\n"
5703                "            int j;\n"
5704                "          }),\n"
5705                "          aaaa, bbbb, cccc);");
5706   verifyFormat("functionCall(\n"
5707                "    {\n"
5708                "      int i;\n"
5709                "      int j;\n"
5710                "    },\n"
5711                "    aaaa, bbbb, // comment\n"
5712                "    cccc);");
5713   verifyFormat("functionA(functionB({\n"
5714                "            int i;\n"
5715                "            int j;\n"
5716                "          }),\n"
5717                "          aaaa, bbbb, // comment\n"
5718                "          cccc);");
5719   verifyFormat("functionCall(aaaa, bbbb, { int i; });");
5720   verifyFormat("functionCall(aaaa, bbbb, {\n"
5721                "  int i;\n"
5722                "  int j;\n"
5723                "});");
5724   verifyFormat(
5725       "Aaa(\n" // FIXME: There shouldn't be a linebreak here.
5726       "    {\n"
5727       "      int i; // break\n"
5728       "    },\n"
5729       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
5730       "                                     ccccccccccccccccc));");
5731   verifyFormat("DEBUG({\n"
5732                "  if (a)\n"
5733                "    f();\n"
5734                "});");
5735 }
5736 
5737 TEST_F(FormatTest, LayoutBlockInsideStatement) {
5738   EXPECT_EQ("SOME_MACRO { int i; }\n"
5739             "int i;",
5740             format("  SOME_MACRO  {int i;}  int i;"));
5741 }
5742 
5743 TEST_F(FormatTest, LayoutNestedBlocks) {
5744   verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
5745                "  struct s {\n"
5746                "    int i;\n"
5747                "  };\n"
5748                "  s kBitsToOs[] = {{10}};\n"
5749                "  for (int i = 0; i < 10; ++i)\n"
5750                "    return;\n"
5751                "}");
5752   verifyFormat("call(parameter, {\n"
5753                "  something();\n"
5754                "  // Comment using all columns.\n"
5755                "  somethingelse();\n"
5756                "});",
5757                getLLVMStyleWithColumns(40));
5758   verifyFormat("DEBUG( //\n"
5759                "    { f(); }, a);");
5760   verifyFormat("DEBUG( //\n"
5761                "    {\n"
5762                "      f(); //\n"
5763                "    },\n"
5764                "    a);");
5765 
5766   EXPECT_EQ("call(parameter, {\n"
5767             "  something();\n"
5768             "  // Comment too\n"
5769             "  // looooooooooong.\n"
5770             "  somethingElse();\n"
5771             "});",
5772             format("call(parameter, {\n"
5773                    "  something();\n"
5774                    "  // Comment too looooooooooong.\n"
5775                    "  somethingElse();\n"
5776                    "});",
5777                    getLLVMStyleWithColumns(29)));
5778   EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int   i; });"));
5779   EXPECT_EQ("DEBUG({ // comment\n"
5780             "  int i;\n"
5781             "});",
5782             format("DEBUG({ // comment\n"
5783                    "int  i;\n"
5784                    "});"));
5785   EXPECT_EQ("DEBUG({\n"
5786             "  int i;\n"
5787             "\n"
5788             "  // comment\n"
5789             "  int j;\n"
5790             "});",
5791             format("DEBUG({\n"
5792                    "  int  i;\n"
5793                    "\n"
5794                    "  // comment\n"
5795                    "  int  j;\n"
5796                    "});"));
5797 
5798   verifyFormat("DEBUG({\n"
5799                "  if (a)\n"
5800                "    return;\n"
5801                "});");
5802   verifyGoogleFormat("DEBUG({\n"
5803                      "  if (a) return;\n"
5804                      "});");
5805   FormatStyle Style = getGoogleStyle();
5806   Style.ColumnLimit = 45;
5807   verifyFormat("Debug(\n"
5808                "    aaaaa,\n"
5809                "    {\n"
5810                "      if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
5811                "    },\n"
5812                "    a);",
5813                Style);
5814 
5815   verifyFormat("SomeFunction({MACRO({ return output; }), b});");
5816 
5817   verifyNoCrash("^{v^{a}}");
5818 }
5819 
5820 TEST_F(FormatTest, FormatNestedBlocksInMacros) {
5821   EXPECT_EQ("#define MACRO()                     \\\n"
5822             "  Debug(aaa, /* force line break */ \\\n"
5823             "        {                           \\\n"
5824             "          int i;                    \\\n"
5825             "          int j;                    \\\n"
5826             "        })",
5827             format("#define   MACRO()   Debug(aaa,  /* force line break */ \\\n"
5828                    "          {  int   i;  int  j;   })",
5829                    getGoogleStyle()));
5830 
5831   EXPECT_EQ("#define A                                       \\\n"
5832             "  [] {                                          \\\n"
5833             "    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(        \\\n"
5834             "        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
5835             "  }",
5836             format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
5837                    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
5838                    getGoogleStyle()));
5839 }
5840 
5841 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
5842   EXPECT_EQ("{}", format("{}"));
5843   verifyFormat("enum E {};");
5844   verifyFormat("enum E {}");
5845   FormatStyle Style = getLLVMStyle();
5846   Style.SpaceInEmptyBlock = true;
5847   EXPECT_EQ("void f() { }", format("void f() {}", Style));
5848   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty;
5849   EXPECT_EQ("while (true) { }", format("while (true) {}", Style));
5850   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
5851   Style.BraceWrapping.BeforeElse = false;
5852   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
5853   verifyFormat("if (a)\n"
5854                "{\n"
5855                "} else if (b)\n"
5856                "{\n"
5857                "} else\n"
5858                "{ }",
5859                Style);
5860   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never;
5861   verifyFormat("if (a) {\n"
5862                "} else if (b) {\n"
5863                "} else {\n"
5864                "}",
5865                Style);
5866   Style.BraceWrapping.BeforeElse = true;
5867   verifyFormat("if (a) { }\n"
5868                "else if (b) { }\n"
5869                "else { }",
5870                Style);
5871 }
5872 
5873 TEST_F(FormatTest, FormatBeginBlockEndMacros) {
5874   FormatStyle Style = getLLVMStyle();
5875   Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$";
5876   Style.MacroBlockEnd = "^[A-Z_]+_END$";
5877   verifyFormat("FOO_BEGIN\n"
5878                "  FOO_ENTRY\n"
5879                "FOO_END",
5880                Style);
5881   verifyFormat("FOO_BEGIN\n"
5882                "  NESTED_FOO_BEGIN\n"
5883                "    NESTED_FOO_ENTRY\n"
5884                "  NESTED_FOO_END\n"
5885                "FOO_END",
5886                Style);
5887   verifyFormat("FOO_BEGIN(Foo, Bar)\n"
5888                "  int x;\n"
5889                "  x = 1;\n"
5890                "FOO_END(Baz)",
5891                Style);
5892 }
5893 
5894 //===----------------------------------------------------------------------===//
5895 // Line break tests.
5896 //===----------------------------------------------------------------------===//
5897 
5898 TEST_F(FormatTest, PreventConfusingIndents) {
5899   verifyFormat(
5900       "void f() {\n"
5901       "  SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
5902       "                         parameter, parameter, parameter)),\n"
5903       "                     SecondLongCall(parameter));\n"
5904       "}");
5905   verifyFormat(
5906       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5907       "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
5908       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
5909       "    aaaaaaaaaaaaaaaaaaaaaaaa);");
5910   verifyFormat(
5911       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5912       "    [aaaaaaaaaaaaaaaaaaaaaaaa\n"
5913       "         [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
5914       "         [aaaaaaaaaaaaaaaaaaaaaaaa]];");
5915   verifyFormat(
5916       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
5917       "    aaaaaaaaaaaaaaaaaaaaaaaa<\n"
5918       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
5919       "    aaaaaaaaaaaaaaaaaaaaaaaa>;");
5920   verifyFormat("int a = bbbb && ccc &&\n"
5921                "        fffff(\n"
5922                "#define A Just forcing a new line\n"
5923                "            ddd);");
5924 }
5925 
5926 TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
5927   verifyFormat(
5928       "bool aaaaaaa =\n"
5929       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
5930       "    bbbbbbbb();");
5931   verifyFormat(
5932       "bool aaaaaaa =\n"
5933       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n"
5934       "    bbbbbbbb();");
5935 
5936   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
5937                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
5938                "    ccccccccc == ddddddddddd;");
5939   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
5940                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n"
5941                "    ccccccccc == ddddddddddd;");
5942   verifyFormat(
5943       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
5944       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n"
5945       "    ccccccccc == ddddddddddd;");
5946 
5947   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
5948                "                 aaaaaa) &&\n"
5949                "         bbbbbb && cccccc;");
5950   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
5951                "                 aaaaaa) >>\n"
5952                "         bbbbbb;");
5953   verifyFormat("aa = Whitespaces.addUntouchableComment(\n"
5954                "    SourceMgr.getSpellingColumnNumber(\n"
5955                "        TheLine.Last->FormatTok.Tok.getLocation()) -\n"
5956                "    1);");
5957 
5958   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
5959                "     bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
5960                "    cccccc) {\n}");
5961   verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
5962                "               bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n"
5963                "              cccccc) {\n}");
5964   verifyFormat("if CONSTEXPR ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
5965                "               bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n"
5966                "              cccccc) {\n}");
5967   verifyFormat("b = a &&\n"
5968                "    // Comment\n"
5969                "    b.c && d;");
5970 
5971   // If the LHS of a comparison is not a binary expression itself, the
5972   // additional linebreak confuses many people.
5973   verifyFormat(
5974       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5975       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
5976       "}");
5977   verifyFormat(
5978       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5979       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
5980       "}");
5981   verifyFormat(
5982       "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
5983       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
5984       "}");
5985   verifyFormat(
5986       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5987       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) <=> 5) {\n"
5988       "}");
5989   // Even explicit parentheses stress the precedence enough to make the
5990   // additional break unnecessary.
5991   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5992                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
5993                "}");
5994   // This cases is borderline, but with the indentation it is still readable.
5995   verifyFormat(
5996       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5997       "        aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5998       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
5999       "}",
6000       getLLVMStyleWithColumns(75));
6001 
6002   // If the LHS is a binary expression, we should still use the additional break
6003   // as otherwise the formatting hides the operator precedence.
6004   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
6005                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
6006                "    5) {\n"
6007                "}");
6008   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
6009                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa <=>\n"
6010                "    5) {\n"
6011                "}");
6012 
6013   FormatStyle OnePerLine = getLLVMStyle();
6014   OnePerLine.BinPackParameters = false;
6015   verifyFormat(
6016       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
6017       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
6018       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
6019       OnePerLine);
6020 
6021   verifyFormat("int i = someFunction(aaaaaaa, 0)\n"
6022                "                .aaa(aaaaaaaaaaaaa) *\n"
6023                "            aaaaaaa +\n"
6024                "        aaaaaaa;",
6025                getLLVMStyleWithColumns(40));
6026 }
6027 
6028 TEST_F(FormatTest, ExpressionIndentation) {
6029   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
6030                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
6031                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
6032                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
6033                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
6034                "                     bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
6035                "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
6036                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
6037                "                 ccccccccccccccccccccccccccccccccccccccccc;");
6038   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
6039                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
6040                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
6041                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
6042   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
6043                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
6044                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
6045                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
6046   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
6047                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
6048                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
6049                "        bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
6050   verifyFormat("if () {\n"
6051                "} else if (aaaaa && bbbbb > // break\n"
6052                "                        ccccc) {\n"
6053                "}");
6054   verifyFormat("if () {\n"
6055                "} else if constexpr (aaaaa && bbbbb > // break\n"
6056                "                                  ccccc) {\n"
6057                "}");
6058   verifyFormat("if () {\n"
6059                "} else if CONSTEXPR (aaaaa && bbbbb > // break\n"
6060                "                                  ccccc) {\n"
6061                "}");
6062   verifyFormat("if () {\n"
6063                "} else if (aaaaa &&\n"
6064                "           bbbbb > // break\n"
6065                "               ccccc &&\n"
6066                "           ddddd) {\n"
6067                "}");
6068 
6069   // Presence of a trailing comment used to change indentation of b.
6070   verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
6071                "       b;\n"
6072                "return aaaaaaaaaaaaaaaaaaa +\n"
6073                "       b; //",
6074                getLLVMStyleWithColumns(30));
6075 }
6076 
6077 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
6078   // Not sure what the best system is here. Like this, the LHS can be found
6079   // immediately above an operator (everything with the same or a higher
6080   // indent). The RHS is aligned right of the operator and so compasses
6081   // everything until something with the same indent as the operator is found.
6082   // FIXME: Is this a good system?
6083   FormatStyle Style = getLLVMStyle();
6084   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
6085   verifyFormat(
6086       "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       Style);
6096   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6097                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6098                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6099                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
6100                Style);
6101   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6102                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6103                "              * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6104                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
6105                Style);
6106   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6107                "    == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6108                "               * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6109                "           + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
6110                Style);
6111   verifyFormat("if () {\n"
6112                "} else if (aaaaa\n"
6113                "           && bbbbb // break\n"
6114                "                  > ccccc) {\n"
6115                "}",
6116                Style);
6117   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6118                "       && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
6119                Style);
6120   verifyFormat("return (a)\n"
6121                "       // comment\n"
6122                "       + b;",
6123                Style);
6124   verifyFormat(
6125       "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6126       "                 * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6127       "             + cc;",
6128       Style);
6129 
6130   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6131                "    = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
6132                Style);
6133 
6134   // Forced by comments.
6135   verifyFormat(
6136       "unsigned ContentSize =\n"
6137       "    sizeof(int16_t)   // DWARF ARange version number\n"
6138       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
6139       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
6140       "    + sizeof(int8_t); // Segment Size (in bytes)");
6141 
6142   verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
6143                "       == boost::fusion::at_c<1>(iiii).second;",
6144                Style);
6145 
6146   Style.ColumnLimit = 60;
6147   verifyFormat("zzzzzzzzzz\n"
6148                "    = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6149                "      >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
6150                Style);
6151 
6152   Style.ColumnLimit = 80;
6153   Style.IndentWidth = 4;
6154   Style.TabWidth = 4;
6155   Style.UseTab = FormatStyle::UT_Always;
6156   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
6157   Style.AlignOperands = FormatStyle::OAS_DontAlign;
6158   EXPECT_EQ("return someVeryVeryLongConditionThatBarelyFitsOnALine\n"
6159             "\t&& (someOtherLongishConditionPart1\n"
6160             "\t\t|| someOtherEvenLongerNestedConditionPart2);",
6161             format("return someVeryVeryLongConditionThatBarelyFitsOnALine && "
6162                    "(someOtherLongishConditionPart1 || "
6163                    "someOtherEvenLongerNestedConditionPart2);",
6164                    Style));
6165 }
6166 
6167 TEST_F(FormatTest, ExpressionIndentationStrictAlign) {
6168   FormatStyle Style = getLLVMStyle();
6169   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
6170   Style.AlignOperands = FormatStyle::OAS_AlignAfterOperator;
6171 
6172   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6173                "                   + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6174                "                   + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6175                "              == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6176                "                         * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6177                "                     + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6178                "          && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6179                "                     * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6180                "                 > ccccccccccccccccccccccccccccccccccccccccc;",
6181                Style);
6182   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6183                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6184                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6185                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
6186                Style);
6187   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6188                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6189                "              * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6190                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
6191                Style);
6192   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6193                "    == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6194                "               * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6195                "           + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
6196                Style);
6197   verifyFormat("if () {\n"
6198                "} else if (aaaaa\n"
6199                "           && bbbbb // break\n"
6200                "                  > ccccc) {\n"
6201                "}",
6202                Style);
6203   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6204                "    && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
6205                Style);
6206   verifyFormat("return (a)\n"
6207                "     // comment\n"
6208                "     + b;",
6209                Style);
6210   verifyFormat(
6211       "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6212       "               * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6213       "           + cc;",
6214       Style);
6215   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
6216                "     : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
6217                "                        : 3333333333333333;",
6218                Style);
6219   verifyFormat(
6220       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa    ? bbbbbbbbbbbbbbbbbb\n"
6221       "                           : ccccccccccccccc ? dddddddddddddddddd\n"
6222       "                                             : eeeeeeeeeeeeeeeeee)\n"
6223       "     : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
6224       "                        : 3333333333333333;",
6225       Style);
6226   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6227                "    = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
6228                Style);
6229 
6230   verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
6231                "    == boost::fusion::at_c<1>(iiii).second;",
6232                Style);
6233 
6234   Style.ColumnLimit = 60;
6235   verifyFormat("zzzzzzzzzzzzz\n"
6236                "    = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6237                "   >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
6238                Style);
6239 
6240   // Forced by comments.
6241   Style.ColumnLimit = 80;
6242   verifyFormat(
6243       "unsigned ContentSize\n"
6244       "    = sizeof(int16_t) // DWARF ARange version number\n"
6245       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
6246       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
6247       "    + sizeof(int8_t); // Segment Size (in bytes)",
6248       Style);
6249 
6250   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
6251   verifyFormat(
6252       "unsigned ContentSize =\n"
6253       "    sizeof(int16_t)   // DWARF ARange version number\n"
6254       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
6255       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
6256       "    + sizeof(int8_t); // Segment Size (in bytes)",
6257       Style);
6258 
6259   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
6260   verifyFormat(
6261       "unsigned ContentSize =\n"
6262       "    sizeof(int16_t)   // DWARF ARange version number\n"
6263       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
6264       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
6265       "    + sizeof(int8_t); // Segment Size (in bytes)",
6266       Style);
6267 }
6268 
6269 TEST_F(FormatTest, EnforcedOperatorWraps) {
6270   // Here we'd like to wrap after the || operators, but a comment is forcing an
6271   // earlier wrap.
6272   verifyFormat("bool x = aaaaa //\n"
6273                "         || bbbbb\n"
6274                "         //\n"
6275                "         || cccc;");
6276 }
6277 
6278 TEST_F(FormatTest, NoOperandAlignment) {
6279   FormatStyle Style = getLLVMStyle();
6280   Style.AlignOperands = FormatStyle::OAS_DontAlign;
6281   verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n"
6282                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
6283                "                   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
6284                Style);
6285   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
6286   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6287                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6288                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6289                "        == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6290                "                * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6291                "            + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6292                "    && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6293                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6294                "        > ccccccccccccccccccccccccccccccccccccccccc;",
6295                Style);
6296 
6297   verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6298                "        * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6299                "    + cc;",
6300                Style);
6301   verifyFormat("int a = aa\n"
6302                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6303                "        * cccccccccccccccccccccccccccccccccccc;\n",
6304                Style);
6305 
6306   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
6307   verifyFormat("return (a > b\n"
6308                "    // comment1\n"
6309                "    // comment2\n"
6310                "    || c);",
6311                Style);
6312 }
6313 
6314 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) {
6315   FormatStyle Style = getLLVMStyle();
6316   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
6317   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
6318                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6319                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
6320                Style);
6321 }
6322 
6323 TEST_F(FormatTest, AllowBinPackingInsideArguments) {
6324   FormatStyle Style = getLLVMStyleWithColumns(40);
6325   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
6326   Style.BinPackArguments = false;
6327   verifyFormat("void test() {\n"
6328                "  someFunction(\n"
6329                "      this + argument + is + quite\n"
6330                "      + long + so + it + gets + wrapped\n"
6331                "      + but + remains + bin - packed);\n"
6332                "}",
6333                Style);
6334   verifyFormat("void test() {\n"
6335                "  someFunction(arg1,\n"
6336                "               this + argument + is\n"
6337                "                   + quite + long + so\n"
6338                "                   + it + gets + wrapped\n"
6339                "                   + but + remains + bin\n"
6340                "                   - packed,\n"
6341                "               arg3);\n"
6342                "}",
6343                Style);
6344   verifyFormat("void test() {\n"
6345                "  someFunction(\n"
6346                "      arg1,\n"
6347                "      this + argument + has\n"
6348                "          + anotherFunc(nested,\n"
6349                "                        calls + whose\n"
6350                "                            + arguments\n"
6351                "                            + are + also\n"
6352                "                            + wrapped,\n"
6353                "                        in + addition)\n"
6354                "          + to + being + bin - packed,\n"
6355                "      arg3);\n"
6356                "}",
6357                Style);
6358 
6359   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
6360   verifyFormat("void test() {\n"
6361                "  someFunction(\n"
6362                "      arg1,\n"
6363                "      this + argument + has +\n"
6364                "          anotherFunc(nested,\n"
6365                "                      calls + whose +\n"
6366                "                          arguments +\n"
6367                "                          are + also +\n"
6368                "                          wrapped,\n"
6369                "                      in + addition) +\n"
6370                "          to + being + bin - packed,\n"
6371                "      arg3);\n"
6372                "}",
6373                Style);
6374 }
6375 
6376 TEST_F(FormatTest, ConstructorInitializers) {
6377   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
6378   verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
6379                getLLVMStyleWithColumns(45));
6380   verifyFormat("Constructor()\n"
6381                "    : Inttializer(FitsOnTheLine) {}",
6382                getLLVMStyleWithColumns(44));
6383   verifyFormat("Constructor()\n"
6384                "    : Inttializer(FitsOnTheLine) {}",
6385                getLLVMStyleWithColumns(43));
6386 
6387   verifyFormat("template <typename T>\n"
6388                "Constructor() : Initializer(FitsOnTheLine) {}",
6389                getLLVMStyleWithColumns(45));
6390 
6391   verifyFormat(
6392       "SomeClass::Constructor()\n"
6393       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
6394 
6395   verifyFormat(
6396       "SomeClass::Constructor()\n"
6397       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6398       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
6399   verifyFormat(
6400       "SomeClass::Constructor()\n"
6401       "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6402       "      aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
6403   verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6404                "            aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
6405                "    : aaaaaaaaaa(aaaaaa) {}");
6406 
6407   verifyFormat("Constructor()\n"
6408                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6409                "      aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6410                "                               aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6411                "      aaaaaaaaaaaaaaaaaaaaaaa() {}");
6412 
6413   verifyFormat("Constructor()\n"
6414                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6415                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
6416 
6417   verifyFormat("Constructor(int Parameter = 0)\n"
6418                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
6419                "      aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
6420   verifyFormat("Constructor()\n"
6421                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
6422                "}",
6423                getLLVMStyleWithColumns(60));
6424   verifyFormat("Constructor()\n"
6425                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6426                "          aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}");
6427 
6428   // Here a line could be saved by splitting the second initializer onto two
6429   // lines, but that is not desirable.
6430   verifyFormat("Constructor()\n"
6431                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
6432                "      aaaaaaaaaaa(aaaaaaaaaaa),\n"
6433                "      aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
6434 
6435   FormatStyle OnePerLine = getLLVMStyle();
6436   OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_Never;
6437   verifyFormat("MyClass::MyClass()\n"
6438                "    : a(a),\n"
6439                "      b(b),\n"
6440                "      c(c) {}",
6441                OnePerLine);
6442   verifyFormat("MyClass::MyClass()\n"
6443                "    : a(a), // comment\n"
6444                "      b(b),\n"
6445                "      c(c) {}",
6446                OnePerLine);
6447   verifyFormat("MyClass::MyClass(int a)\n"
6448                "    : b(a),      // comment\n"
6449                "      c(a + 1) { // lined up\n"
6450                "}",
6451                OnePerLine);
6452   verifyFormat("Constructor()\n"
6453                "    : a(b, b, b) {}",
6454                OnePerLine);
6455   OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6456   OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false;
6457   verifyFormat("SomeClass::Constructor()\n"
6458                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6459                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6460                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
6461                OnePerLine);
6462   verifyFormat("SomeClass::Constructor()\n"
6463                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
6464                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6465                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
6466                OnePerLine);
6467   verifyFormat("MyClass::MyClass(int var)\n"
6468                "    : some_var_(var),            // 4 space indent\n"
6469                "      some_other_var_(var + 1) { // lined up\n"
6470                "}",
6471                OnePerLine);
6472   verifyFormat("Constructor()\n"
6473                "    : aaaaa(aaaaaa),\n"
6474                "      aaaaa(aaaaaa),\n"
6475                "      aaaaa(aaaaaa),\n"
6476                "      aaaaa(aaaaaa),\n"
6477                "      aaaaa(aaaaaa) {}",
6478                OnePerLine);
6479   verifyFormat("Constructor()\n"
6480                "    : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
6481                "            aaaaaaaaaaaaaaaaaaaaaa) {}",
6482                OnePerLine);
6483   OnePerLine.BinPackParameters = false;
6484   verifyFormat(
6485       "Constructor()\n"
6486       "    : aaaaaaaaaaaaaaaaaaaaaaaa(\n"
6487       "          aaaaaaaaaaa().aaa(),\n"
6488       "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
6489       OnePerLine);
6490   OnePerLine.ColumnLimit = 60;
6491   verifyFormat("Constructor()\n"
6492                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
6493                "      bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
6494                OnePerLine);
6495 
6496   EXPECT_EQ("Constructor()\n"
6497             "    : // Comment forcing unwanted break.\n"
6498             "      aaaa(aaaa) {}",
6499             format("Constructor() :\n"
6500                    "    // Comment forcing unwanted break.\n"
6501                    "    aaaa(aaaa) {}"));
6502 }
6503 
6504 TEST_F(FormatTest, AllowAllConstructorInitializersOnNextLine) {
6505   FormatStyle Style = getLLVMStyleWithColumns(60);
6506   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
6507   Style.BinPackParameters = false;
6508 
6509   for (int i = 0; i < 4; ++i) {
6510     // Test all combinations of parameters that should not have an effect.
6511     Style.AllowAllParametersOfDeclarationOnNextLine = i & 1;
6512     Style.AllowAllArgumentsOnNextLine = i & 2;
6513 
6514     Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6515     Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
6516     verifyFormat("Constructor()\n"
6517                  "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6518                  Style);
6519     verifyFormat("Constructor() : a(a), b(b) {}", Style);
6520 
6521     Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
6522     verifyFormat("Constructor()\n"
6523                  "    : aaaaaaaaaaaaaaaaaaaa(a)\n"
6524                  "    , bbbbbbbbbbbbbbbbbbbbb(b) {}",
6525                  Style);
6526     verifyFormat("Constructor() : a(a), b(b) {}", Style);
6527 
6528     Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
6529     Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6530     verifyFormat("Constructor()\n"
6531                  "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6532                  Style);
6533 
6534     Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
6535     verifyFormat("Constructor()\n"
6536                  "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
6537                  "      bbbbbbbbbbbbbbbbbbbbb(b) {}",
6538                  Style);
6539 
6540     Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
6541     Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6542     verifyFormat("Constructor() :\n"
6543                  "    aaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6544                  Style);
6545 
6546     Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
6547     verifyFormat("Constructor() :\n"
6548                  "    aaaaaaaaaaaaaaaaaa(a),\n"
6549                  "    bbbbbbbbbbbbbbbbbbbbb(b) {}",
6550                  Style);
6551   }
6552 
6553   // Test interactions between AllowAllParametersOfDeclarationOnNextLine and
6554   // AllowAllConstructorInitializersOnNextLine in all
6555   // BreakConstructorInitializers modes
6556   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
6557   Style.AllowAllParametersOfDeclarationOnNextLine = true;
6558   verifyFormat("SomeClassWithALongName::Constructor(\n"
6559                "    int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n"
6560                "    : aaaaaaaaaaaaaaaaaaaa(a)\n"
6561                "    , bbbbbbbbbbbbbbbbbbbbb(b) {}",
6562                Style);
6563 
6564   Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6565   verifyFormat("SomeClassWithALongName::Constructor(\n"
6566                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6567                "    int bbbbbbbbbbbbb,\n"
6568                "    int cccccccccccccccc)\n"
6569                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6570                Style);
6571 
6572   Style.AllowAllParametersOfDeclarationOnNextLine = false;
6573   Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
6574   verifyFormat("SomeClassWithALongName::Constructor(\n"
6575                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6576                "    int bbbbbbbbbbbbb)\n"
6577                "    : aaaaaaaaaaaaaaaaaaaa(a)\n"
6578                "    , bbbbbbbbbbbbbbbbbbbbb(b) {}",
6579                Style);
6580 
6581   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
6582 
6583   Style.AllowAllParametersOfDeclarationOnNextLine = true;
6584   verifyFormat("SomeClassWithALongName::Constructor(\n"
6585                "    int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n"
6586                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
6587                "      bbbbbbbbbbbbbbbbbbbbb(b) {}",
6588                Style);
6589 
6590   Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6591   verifyFormat("SomeClassWithALongName::Constructor(\n"
6592                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6593                "    int bbbbbbbbbbbbb,\n"
6594                "    int cccccccccccccccc)\n"
6595                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6596                Style);
6597 
6598   Style.AllowAllParametersOfDeclarationOnNextLine = false;
6599   Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
6600   verifyFormat("SomeClassWithALongName::Constructor(\n"
6601                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6602                "    int bbbbbbbbbbbbb)\n"
6603                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
6604                "      bbbbbbbbbbbbbbbbbbbbb(b) {}",
6605                Style);
6606 
6607   Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
6608   Style.AllowAllParametersOfDeclarationOnNextLine = true;
6609   verifyFormat("SomeClassWithALongName::Constructor(\n"
6610                "    int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb) :\n"
6611                "    aaaaaaaaaaaaaaaaaaaa(a),\n"
6612                "    bbbbbbbbbbbbbbbbbbbbb(b) {}",
6613                Style);
6614 
6615   Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6616   verifyFormat("SomeClassWithALongName::Constructor(\n"
6617                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6618                "    int bbbbbbbbbbbbb,\n"
6619                "    int cccccccccccccccc) :\n"
6620                "    aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6621                Style);
6622 
6623   Style.AllowAllParametersOfDeclarationOnNextLine = false;
6624   Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
6625   verifyFormat("SomeClassWithALongName::Constructor(\n"
6626                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6627                "    int bbbbbbbbbbbbb) :\n"
6628                "    aaaaaaaaaaaaaaaaaaaa(a),\n"
6629                "    bbbbbbbbbbbbbbbbbbbbb(b) {}",
6630                Style);
6631 }
6632 
6633 TEST_F(FormatTest, AllowAllArgumentsOnNextLine) {
6634   FormatStyle Style = getLLVMStyleWithColumns(60);
6635   Style.BinPackArguments = false;
6636   for (int i = 0; i < 4; ++i) {
6637     // Test all combinations of parameters that should not have an effect.
6638     Style.AllowAllParametersOfDeclarationOnNextLine = i & 1;
6639     Style.PackConstructorInitializers =
6640         i & 2 ? FormatStyle::PCIS_BinPack : FormatStyle::PCIS_Never;
6641 
6642     Style.AllowAllArgumentsOnNextLine = true;
6643     verifyFormat("void foo() {\n"
6644                  "  FunctionCallWithReallyLongName(\n"
6645                  "      aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb);\n"
6646                  "}",
6647                  Style);
6648     Style.AllowAllArgumentsOnNextLine = false;
6649     verifyFormat("void foo() {\n"
6650                  "  FunctionCallWithReallyLongName(\n"
6651                  "      aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6652                  "      bbbbbbbbbbbb);\n"
6653                  "}",
6654                  Style);
6655 
6656     Style.AllowAllArgumentsOnNextLine = true;
6657     verifyFormat("void foo() {\n"
6658                  "  auto VariableWithReallyLongName = {\n"
6659                  "      aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb};\n"
6660                  "}",
6661                  Style);
6662     Style.AllowAllArgumentsOnNextLine = false;
6663     verifyFormat("void foo() {\n"
6664                  "  auto VariableWithReallyLongName = {\n"
6665                  "      aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6666                  "      bbbbbbbbbbbb};\n"
6667                  "}",
6668                  Style);
6669   }
6670 
6671   // This parameter should not affect declarations.
6672   Style.BinPackParameters = false;
6673   Style.AllowAllArgumentsOnNextLine = false;
6674   Style.AllowAllParametersOfDeclarationOnNextLine = true;
6675   verifyFormat("void FunctionCallWithReallyLongName(\n"
6676                "    int aaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbb);",
6677                Style);
6678   Style.AllowAllParametersOfDeclarationOnNextLine = false;
6679   verifyFormat("void FunctionCallWithReallyLongName(\n"
6680                "    int aaaaaaaaaaaaaaaaaaaaaaa,\n"
6681                "    int bbbbbbbbbbbb);",
6682                Style);
6683 }
6684 
6685 TEST_F(FormatTest, AllowAllArgumentsOnNextLineDontAlign) {
6686   // Check that AllowAllArgumentsOnNextLine is respected for both BAS_DontAlign
6687   // and BAS_Align.
6688   FormatStyle Style = getLLVMStyleWithColumns(35);
6689   StringRef Input = "functionCall(paramA, paramB, paramC);\n"
6690                     "void functionDecl(int A, int B, int C);";
6691   Style.AllowAllArgumentsOnNextLine = false;
6692   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
6693   EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n"
6694                       "    paramC);\n"
6695                       "void functionDecl(int A, int B,\n"
6696                       "    int C);"),
6697             format(Input, Style));
6698   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
6699   EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n"
6700                       "             paramC);\n"
6701                       "void functionDecl(int A, int B,\n"
6702                       "                  int C);"),
6703             format(Input, Style));
6704   // However, BAS_AlwaysBreak should take precedence over
6705   // AllowAllArgumentsOnNextLine.
6706   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
6707   EXPECT_EQ(StringRef("functionCall(\n"
6708                       "    paramA, paramB, paramC);\n"
6709                       "void functionDecl(\n"
6710                       "    int A, int B, int C);"),
6711             format(Input, Style));
6712 
6713   // When AllowAllArgumentsOnNextLine is set, we prefer breaking before the
6714   // first argument.
6715   Style.AllowAllArgumentsOnNextLine = true;
6716   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
6717   EXPECT_EQ(StringRef("functionCall(\n"
6718                       "    paramA, paramB, paramC);\n"
6719                       "void functionDecl(\n"
6720                       "    int A, int B, int C);"),
6721             format(Input, Style));
6722   // It wouldn't fit on one line with aligned parameters so this setting
6723   // doesn't change anything for BAS_Align.
6724   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
6725   EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n"
6726                       "             paramC);\n"
6727                       "void functionDecl(int A, int B,\n"
6728                       "                  int C);"),
6729             format(Input, Style));
6730   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
6731   EXPECT_EQ(StringRef("functionCall(\n"
6732                       "    paramA, paramB, paramC);\n"
6733                       "void functionDecl(\n"
6734                       "    int A, int B, int C);"),
6735             format(Input, Style));
6736 }
6737 
6738 TEST_F(FormatTest, BreakConstructorInitializersAfterColon) {
6739   FormatStyle Style = getLLVMStyle();
6740   Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
6741 
6742   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
6743   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}",
6744                getStyleWithColumns(Style, 45));
6745   verifyFormat("Constructor() :\n"
6746                "    Initializer(FitsOnTheLine) {}",
6747                getStyleWithColumns(Style, 44));
6748   verifyFormat("Constructor() :\n"
6749                "    Initializer(FitsOnTheLine) {}",
6750                getStyleWithColumns(Style, 43));
6751 
6752   verifyFormat("template <typename T>\n"
6753                "Constructor() : Initializer(FitsOnTheLine) {}",
6754                getStyleWithColumns(Style, 50));
6755   Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6756   verifyFormat(
6757       "SomeClass::Constructor() :\n"
6758       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
6759       Style);
6760 
6761   Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack;
6762   verifyFormat(
6763       "SomeClass::Constructor() :\n"
6764       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
6765       Style);
6766 
6767   verifyFormat(
6768       "SomeClass::Constructor() :\n"
6769       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6770       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
6771       Style);
6772   verifyFormat(
6773       "SomeClass::Constructor() :\n"
6774       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6775       "    aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
6776       Style);
6777   verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6778                "            aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
6779                "    aaaaaaaaaa(aaaaaa) {}",
6780                Style);
6781 
6782   verifyFormat("Constructor() :\n"
6783                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6784                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6785                "                             aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6786                "    aaaaaaaaaaaaaaaaaaaaaaa() {}",
6787                Style);
6788 
6789   verifyFormat("Constructor() :\n"
6790                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6791                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
6792                Style);
6793 
6794   verifyFormat("Constructor(int Parameter = 0) :\n"
6795                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
6796                "    aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}",
6797                Style);
6798   verifyFormat("Constructor() :\n"
6799                "    aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
6800                "}",
6801                getStyleWithColumns(Style, 60));
6802   verifyFormat("Constructor() :\n"
6803                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6804                "        aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}",
6805                Style);
6806 
6807   // Here a line could be saved by splitting the second initializer onto two
6808   // lines, but that is not desirable.
6809   verifyFormat("Constructor() :\n"
6810                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
6811                "    aaaaaaaaaaa(aaaaaaaaaaa),\n"
6812                "    aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
6813                Style);
6814 
6815   FormatStyle OnePerLine = Style;
6816   OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
6817   verifyFormat("SomeClass::Constructor() :\n"
6818                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6819                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6820                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
6821                OnePerLine);
6822   verifyFormat("SomeClass::Constructor() :\n"
6823                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
6824                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6825                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
6826                OnePerLine);
6827   verifyFormat("MyClass::MyClass(int var) :\n"
6828                "    some_var_(var),            // 4 space indent\n"
6829                "    some_other_var_(var + 1) { // lined up\n"
6830                "}",
6831                OnePerLine);
6832   verifyFormat("Constructor() :\n"
6833                "    aaaaa(aaaaaa),\n"
6834                "    aaaaa(aaaaaa),\n"
6835                "    aaaaa(aaaaaa),\n"
6836                "    aaaaa(aaaaaa),\n"
6837                "    aaaaa(aaaaaa) {}",
6838                OnePerLine);
6839   verifyFormat("Constructor() :\n"
6840                "    aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
6841                "          aaaaaaaaaaaaaaaaaaaaaa) {}",
6842                OnePerLine);
6843   OnePerLine.BinPackParameters = false;
6844   verifyFormat("Constructor() :\n"
6845                "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
6846                "        aaaaaaaaaaa().aaa(),\n"
6847                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
6848                OnePerLine);
6849   OnePerLine.ColumnLimit = 60;
6850   verifyFormat("Constructor() :\n"
6851                "    aaaaaaaaaaaaaaaaaaaa(a),\n"
6852                "    bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
6853                OnePerLine);
6854 
6855   EXPECT_EQ("Constructor() :\n"
6856             "    // Comment forcing unwanted break.\n"
6857             "    aaaa(aaaa) {}",
6858             format("Constructor() :\n"
6859                    "    // Comment forcing unwanted break.\n"
6860                    "    aaaa(aaaa) {}",
6861                    Style));
6862 
6863   Style.ColumnLimit = 0;
6864   verifyFormat("SomeClass::Constructor() :\n"
6865                "    a(a) {}",
6866                Style);
6867   verifyFormat("SomeClass::Constructor() noexcept :\n"
6868                "    a(a) {}",
6869                Style);
6870   verifyFormat("SomeClass::Constructor() :\n"
6871                "    a(a), b(b), c(c) {}",
6872                Style);
6873   verifyFormat("SomeClass::Constructor() :\n"
6874                "    a(a) {\n"
6875                "  foo();\n"
6876                "  bar();\n"
6877                "}",
6878                Style);
6879 
6880   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
6881   verifyFormat("SomeClass::Constructor() :\n"
6882                "    a(a), b(b), c(c) {\n"
6883                "}",
6884                Style);
6885   verifyFormat("SomeClass::Constructor() :\n"
6886                "    a(a) {\n"
6887                "}",
6888                Style);
6889 
6890   Style.ColumnLimit = 80;
6891   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
6892   Style.ConstructorInitializerIndentWidth = 2;
6893   verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}", Style);
6894   verifyFormat("SomeClass::Constructor() :\n"
6895                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6896                "  bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}",
6897                Style);
6898 
6899   // `ConstructorInitializerIndentWidth` actually applies to InheritanceList as
6900   // well
6901   Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
6902   verifyFormat(
6903       "class SomeClass\n"
6904       "  : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6905       "    public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
6906       Style);
6907   Style.BreakInheritanceList = FormatStyle::BILS_BeforeComma;
6908   verifyFormat(
6909       "class SomeClass\n"
6910       "  : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6911       "  , public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
6912       Style);
6913   Style.BreakInheritanceList = FormatStyle::BILS_AfterColon;
6914   verifyFormat(
6915       "class SomeClass :\n"
6916       "  public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6917       "  public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
6918       Style);
6919   Style.BreakInheritanceList = FormatStyle::BILS_AfterComma;
6920   verifyFormat(
6921       "class SomeClass\n"
6922       "  : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6923       "    public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
6924       Style);
6925 }
6926 
6927 #ifndef EXPENSIVE_CHECKS
6928 // Expensive checks enables libstdc++ checking which includes validating the
6929 // state of ranges used in std::priority_queue - this blows out the
6930 // runtime/scalability of the function and makes this test unacceptably slow.
6931 TEST_F(FormatTest, MemoizationTests) {
6932   // This breaks if the memoization lookup does not take \c Indent and
6933   // \c LastSpace into account.
6934   verifyFormat(
6935       "extern CFRunLoopTimerRef\n"
6936       "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
6937       "                     CFTimeInterval interval, CFOptionFlags flags,\n"
6938       "                     CFIndex order, CFRunLoopTimerCallBack callout,\n"
6939       "                     CFRunLoopTimerContext *context) {}");
6940 
6941   // Deep nesting somewhat works around our memoization.
6942   verifyFormat(
6943       "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
6944       "    aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
6945       "        aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
6946       "            aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
6947       "                aaaaa())))))))))))))))))))))))))))))))))))))));",
6948       getLLVMStyleWithColumns(65));
6949   verifyFormat(
6950       "aaaaa(\n"
6951       "    aaaaa,\n"
6952       "    aaaaa(\n"
6953       "        aaaaa,\n"
6954       "        aaaaa(\n"
6955       "            aaaaa,\n"
6956       "            aaaaa(\n"
6957       "                aaaaa,\n"
6958       "                aaaaa(\n"
6959       "                    aaaaa,\n"
6960       "                    aaaaa(\n"
6961       "                        aaaaa,\n"
6962       "                        aaaaa(\n"
6963       "                            aaaaa,\n"
6964       "                            aaaaa(\n"
6965       "                                aaaaa,\n"
6966       "                                aaaaa(\n"
6967       "                                    aaaaa,\n"
6968       "                                    aaaaa(\n"
6969       "                                        aaaaa,\n"
6970       "                                        aaaaa(\n"
6971       "                                            aaaaa,\n"
6972       "                                            aaaaa(\n"
6973       "                                                aaaaa,\n"
6974       "                                                aaaaa))))))))))));",
6975       getLLVMStyleWithColumns(65));
6976   verifyFormat(
6977       "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"
6978       "                                  a),\n"
6979       "                                a),\n"
6980       "                              a),\n"
6981       "                            a),\n"
6982       "                          a),\n"
6983       "                        a),\n"
6984       "                      a),\n"
6985       "                    a),\n"
6986       "                  a),\n"
6987       "                a),\n"
6988       "              a),\n"
6989       "            a),\n"
6990       "          a),\n"
6991       "        a),\n"
6992       "      a),\n"
6993       "    a),\n"
6994       "  a)",
6995       getLLVMStyleWithColumns(65));
6996 
6997   // This test takes VERY long when memoization is broken.
6998   FormatStyle OnePerLine = getLLVMStyle();
6999   OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
7000   OnePerLine.BinPackParameters = false;
7001   std::string input = "Constructor()\n"
7002                       "    : aaaa(a,\n";
7003   for (unsigned i = 0, e = 80; i != e; ++i) {
7004     input += "           a,\n";
7005   }
7006   input += "           a) {}";
7007   verifyFormat(input, OnePerLine);
7008 }
7009 #endif
7010 
7011 TEST_F(FormatTest, BreaksAsHighAsPossible) {
7012   verifyFormat(
7013       "void f() {\n"
7014       "  if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
7015       "      (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
7016       "    f();\n"
7017       "}");
7018   verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
7019                "    Intervals[i - 1].getRange().getLast()) {\n}");
7020 }
7021 
7022 TEST_F(FormatTest, BreaksFunctionDeclarations) {
7023   // Principially, we break function declarations in a certain order:
7024   // 1) break amongst arguments.
7025   verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
7026                "                              Cccccccccccccc cccccccccccccc);");
7027   verifyFormat("template <class TemplateIt>\n"
7028                "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
7029                "                            TemplateIt *stop) {}");
7030 
7031   // 2) break after return type.
7032   verifyFormat(
7033       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7034       "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);",
7035       getGoogleStyle());
7036 
7037   // 3) break after (.
7038   verifyFormat(
7039       "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
7040       "    Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);",
7041       getGoogleStyle());
7042 
7043   // 4) break before after nested name specifiers.
7044   verifyFormat(
7045       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7046       "SomeClasssssssssssssssssssssssssssssssssssssss::\n"
7047       "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);",
7048       getGoogleStyle());
7049 
7050   // However, there are exceptions, if a sufficient amount of lines can be
7051   // saved.
7052   // FIXME: The precise cut-offs wrt. the number of saved lines might need some
7053   // more adjusting.
7054   verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
7055                "                                  Cccccccccccccc cccccccccc,\n"
7056                "                                  Cccccccccccccc cccccccccc,\n"
7057                "                                  Cccccccccccccc cccccccccc,\n"
7058                "                                  Cccccccccccccc cccccccccc);");
7059   verifyFormat(
7060       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7061       "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
7062       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
7063       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);",
7064       getGoogleStyle());
7065   verifyFormat(
7066       "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
7067       "                                          Cccccccccccccc cccccccccc,\n"
7068       "                                          Cccccccccccccc cccccccccc,\n"
7069       "                                          Cccccccccccccc cccccccccc,\n"
7070       "                                          Cccccccccccccc cccccccccc,\n"
7071       "                                          Cccccccccccccc cccccccccc,\n"
7072       "                                          Cccccccccccccc cccccccccc);");
7073   verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
7074                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
7075                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
7076                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
7077                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
7078 
7079   // Break after multi-line parameters.
7080   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7081                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7082                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7083                "    bbbb bbbb);");
7084   verifyFormat("void SomeLoooooooooooongFunction(\n"
7085                "    std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
7086                "        aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7087                "    int bbbbbbbbbbbbb);");
7088 
7089   // Treat overloaded operators like other functions.
7090   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
7091                "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
7092   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
7093                "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
7094   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
7095                "operator<<(const SomeLooooooooooooooooooooooooogType &other);");
7096   verifyGoogleFormat(
7097       "SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
7098       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
7099   verifyGoogleFormat(
7100       "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
7101       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
7102   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7103                "    int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
7104   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
7105                "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
7106   verifyGoogleFormat(
7107       "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n"
7108       "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7109       "    bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}");
7110   verifyGoogleFormat("template <typename T>\n"
7111                      "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7112                      "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n"
7113                      "    aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);");
7114 
7115   FormatStyle Style = getLLVMStyle();
7116   Style.PointerAlignment = FormatStyle::PAS_Left;
7117   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7118                "    aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}",
7119                Style);
7120   verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
7121                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
7122                Style);
7123 }
7124 
7125 TEST_F(FormatTest, DontBreakBeforeQualifiedOperator) {
7126   // Regression test for https://bugs.llvm.org/show_bug.cgi?id=40516:
7127   // Prefer keeping `::` followed by `operator` together.
7128   EXPECT_EQ("const aaaa::bbbbbbb &\n"
7129             "ccccccccc::operator++() {\n"
7130             "  stuff();\n"
7131             "}",
7132             format("const aaaa::bbbbbbb\n"
7133                    "&ccccccccc::operator++() { stuff(); }",
7134                    getLLVMStyleWithColumns(40)));
7135 }
7136 
7137 TEST_F(FormatTest, TrailingReturnType) {
7138   verifyFormat("auto foo() -> int;\n");
7139   // correct trailing return type spacing
7140   verifyFormat("auto operator->() -> int;\n");
7141   verifyFormat("auto operator++(int) -> int;\n");
7142 
7143   verifyFormat("struct S {\n"
7144                "  auto bar() const -> int;\n"
7145                "};");
7146   verifyFormat("template <size_t Order, typename T>\n"
7147                "auto load_img(const std::string &filename)\n"
7148                "    -> alias::tensor<Order, T, mem::tag::cpu> {}");
7149   verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n"
7150                "    -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}");
7151   verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}");
7152   verifyFormat("template <typename T>\n"
7153                "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n"
7154                "    -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());");
7155 
7156   // Not trailing return types.
7157   verifyFormat("void f() { auto a = b->c(); }");
7158   verifyFormat("auto a = p->foo();");
7159   verifyFormat("int a = p->foo();");
7160   verifyFormat("auto lmbd = [] NOEXCEPT -> int { return 0; };");
7161 }
7162 
7163 TEST_F(FormatTest, DeductionGuides) {
7164   verifyFormat("template <class T> A(const T &, const T &) -> A<T &>;");
7165   verifyFormat("template <class T> explicit A(T &, T &&) -> A<T>;");
7166   verifyFormat("template <class... Ts> S(Ts...) -> S<Ts...>;");
7167   verifyFormat(
7168       "template <class... T>\n"
7169       "array(T &&...t) -> array<std::common_type_t<T...>, sizeof...(T)>;");
7170   verifyFormat("template <class T> A() -> A<decltype(p->foo<3>())>;");
7171   verifyFormat("template <class T> A() -> A<decltype(foo<traits<1>>)>;");
7172   verifyFormat("template <class T> A() -> A<sizeof(p->foo<1>)>;");
7173   verifyFormat("template <class T> A() -> A<(3 < 2)>;");
7174   verifyFormat("template <class T> A() -> A<((3) < (2))>;");
7175   verifyFormat("template <class T> x() -> x<1>;");
7176   verifyFormat("template <class T> explicit x(T &) -> x<1>;");
7177 
7178   // Ensure not deduction guides.
7179   verifyFormat("c()->f<int>();");
7180   verifyFormat("x()->foo<1>;");
7181   verifyFormat("x = p->foo<3>();");
7182   verifyFormat("x()->x<1>();");
7183   verifyFormat("x()->x<1>;");
7184 }
7185 
7186 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
7187   // Avoid breaking before trailing 'const' or other trailing annotations, if
7188   // they are not function-like.
7189   FormatStyle Style = getGoogleStyleWithColumns(47);
7190   verifyFormat("void someLongFunction(\n"
7191                "    int someLoooooooooooooongParameter) const {\n}",
7192                getLLVMStyleWithColumns(47));
7193   verifyFormat("LoooooongReturnType\n"
7194                "someLoooooooongFunction() const {}",
7195                getLLVMStyleWithColumns(47));
7196   verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
7197                "    const {}",
7198                Style);
7199   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
7200                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
7201   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
7202                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
7203   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
7204                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
7205   verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
7206                "                   aaaaaaaaaaa aaaaa) const override;");
7207   verifyGoogleFormat(
7208       "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7209       "    const override;");
7210 
7211   // Even if the first parameter has to be wrapped.
7212   verifyFormat("void someLongFunction(\n"
7213                "    int someLongParameter) const {}",
7214                getLLVMStyleWithColumns(46));
7215   verifyFormat("void someLongFunction(\n"
7216                "    int someLongParameter) const {}",
7217                Style);
7218   verifyFormat("void someLongFunction(\n"
7219                "    int someLongParameter) override {}",
7220                Style);
7221   verifyFormat("void someLongFunction(\n"
7222                "    int someLongParameter) OVERRIDE {}",
7223                Style);
7224   verifyFormat("void someLongFunction(\n"
7225                "    int someLongParameter) final {}",
7226                Style);
7227   verifyFormat("void someLongFunction(\n"
7228                "    int someLongParameter) FINAL {}",
7229                Style);
7230   verifyFormat("void someLongFunction(\n"
7231                "    int parameter) const override {}",
7232                Style);
7233 
7234   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
7235   verifyFormat("void someLongFunction(\n"
7236                "    int someLongParameter) const\n"
7237                "{\n"
7238                "}",
7239                Style);
7240 
7241   Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
7242   verifyFormat("void someLongFunction(\n"
7243                "    int someLongParameter) const\n"
7244                "  {\n"
7245                "  }",
7246                Style);
7247 
7248   // Unless these are unknown annotations.
7249   verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
7250                "                  aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7251                "    LONG_AND_UGLY_ANNOTATION;");
7252 
7253   // Breaking before function-like trailing annotations is fine to keep them
7254   // close to their arguments.
7255   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7256                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
7257   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
7258                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
7259   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
7260                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
7261   verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n"
7262                      "    AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);");
7263   verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});");
7264 
7265   verifyFormat(
7266       "void aaaaaaaaaaaaaaaaaa()\n"
7267       "    __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
7268       "                   aaaaaaaaaaaaaaaaaaaaaaaaa));");
7269   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7270                "    __attribute__((unused));");
7271   verifyGoogleFormat(
7272       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7273       "    GUARDED_BY(aaaaaaaaaaaa);");
7274   verifyGoogleFormat(
7275       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7276       "    GUARDED_BY(aaaaaaaaaaaa);");
7277   verifyGoogleFormat(
7278       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
7279       "    aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
7280   verifyGoogleFormat(
7281       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
7282       "    aaaaaaaaaaaaaaaaaaaaaaaaa;");
7283 }
7284 
7285 TEST_F(FormatTest, FunctionAnnotations) {
7286   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
7287                "int OldFunction(const string &parameter) {}");
7288   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
7289                "string OldFunction(const string &parameter) {}");
7290   verifyFormat("template <typename T>\n"
7291                "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
7292                "string OldFunction(const string &parameter) {}");
7293 
7294   // Not function annotations.
7295   verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7296                "                << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
7297   verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n"
7298                "       ThisIsATestWithAReallyReallyReallyReallyLongName) {}");
7299   verifyFormat("MACRO(abc).function() // wrap\n"
7300                "    << abc;");
7301   verifyFormat("MACRO(abc)->function() // wrap\n"
7302                "    << abc;");
7303   verifyFormat("MACRO(abc)::function() // wrap\n"
7304                "    << abc;");
7305 }
7306 
7307 TEST_F(FormatTest, BreaksDesireably) {
7308   verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
7309                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
7310                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
7311   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7312                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
7313                "}");
7314 
7315   verifyFormat(
7316       "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7317       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
7318 
7319   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7320                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7321                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
7322 
7323   verifyFormat(
7324       "aaaaaaaa(aaaaaaaaaaaaa,\n"
7325       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7326       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
7327       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7328       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
7329 
7330   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
7331                "    (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7332 
7333   verifyFormat(
7334       "void f() {\n"
7335       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
7336       "                                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
7337       "}");
7338   verifyFormat(
7339       "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7340       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
7341   verifyFormat(
7342       "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7343       "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
7344   verifyFormat(
7345       "aaaaaa(aaa,\n"
7346       "       new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7347       "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7348       "       aaaa);");
7349   verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7350                "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7351                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7352 
7353   // Indent consistently independent of call expression and unary operator.
7354   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
7355                "    dddddddddddddddddddddddddddddd));");
7356   verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
7357                "    dddddddddddddddddddddddddddddd));");
7358   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
7359                "    dddddddddddddddddddddddddddddd));");
7360 
7361   // This test case breaks on an incorrect memoization, i.e. an optimization not
7362   // taking into account the StopAt value.
7363   verifyFormat(
7364       "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
7365       "       aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
7366       "       aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
7367       "       (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7368 
7369   verifyFormat("{\n  {\n    {\n"
7370                "      Annotation.SpaceRequiredBefore =\n"
7371                "          Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
7372                "          Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
7373                "    }\n  }\n}");
7374 
7375   // Break on an outer level if there was a break on an inner level.
7376   EXPECT_EQ("f(g(h(a, // comment\n"
7377             "      b, c),\n"
7378             "    d, e),\n"
7379             "  x, y);",
7380             format("f(g(h(a, // comment\n"
7381                    "    b, c), d, e), x, y);"));
7382 
7383   // Prefer breaking similar line breaks.
7384   verifyFormat(
7385       "const int kTrackingOptions = NSTrackingMouseMoved |\n"
7386       "                             NSTrackingMouseEnteredAndExited |\n"
7387       "                             NSTrackingActiveAlways;");
7388 }
7389 
7390 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) {
7391   FormatStyle NoBinPacking = getGoogleStyle();
7392   NoBinPacking.BinPackParameters = false;
7393   NoBinPacking.BinPackArguments = true;
7394   verifyFormat("void f() {\n"
7395                "  f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n"
7396                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
7397                "}",
7398                NoBinPacking);
7399   verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n"
7400                "       int aaaaaaaaaaaaaaaaaaaa,\n"
7401                "       int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
7402                NoBinPacking);
7403 
7404   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
7405   verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7406                "                        vector<int> bbbbbbbbbbbbbbb);",
7407                NoBinPacking);
7408   // FIXME: This behavior difference is probably not wanted. However, currently
7409   // we cannot distinguish BreakBeforeParameter being set because of the wrapped
7410   // template arguments from BreakBeforeParameter being set because of the
7411   // one-per-line formatting.
7412   verifyFormat(
7413       "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n"
7414       "                                             aaaaaaaaaa> aaaaaaaaaa);",
7415       NoBinPacking);
7416   verifyFormat(
7417       "void fffffffffff(\n"
7418       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n"
7419       "        aaaaaaaaaa);");
7420 }
7421 
7422 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
7423   FormatStyle NoBinPacking = getGoogleStyle();
7424   NoBinPacking.BinPackParameters = false;
7425   NoBinPacking.BinPackArguments = false;
7426   verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
7427                "  aaaaaaaaaaaaaaaaaaaa,\n"
7428                "  aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
7429                NoBinPacking);
7430   verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
7431                "        aaaaaaaaaaaaa,\n"
7432                "        aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
7433                NoBinPacking);
7434   verifyFormat(
7435       "aaaaaaaa(aaaaaaaaaaaaa,\n"
7436       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7437       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
7438       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7439       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
7440       NoBinPacking);
7441   verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
7442                "    .aaaaaaaaaaaaaaaaaa();",
7443                NoBinPacking);
7444   verifyFormat("void f() {\n"
7445                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7446                "      aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n"
7447                "}",
7448                NoBinPacking);
7449 
7450   verifyFormat(
7451       "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7452       "             aaaaaaaaaaaa,\n"
7453       "             aaaaaaaaaaaa);",
7454       NoBinPacking);
7455   verifyFormat(
7456       "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
7457       "                               ddddddddddddddddddddddddddddd),\n"
7458       "             test);",
7459       NoBinPacking);
7460 
7461   verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
7462                "            aaaaaaaaaaaaaaaaaaaaaaa,\n"
7463                "            aaaaaaaaaaaaaaaaaaaaaaa>\n"
7464                "    aaaaaaaaaaaaaaaaaa;",
7465                NoBinPacking);
7466   verifyFormat("a(\"a\"\n"
7467                "  \"a\",\n"
7468                "  a);");
7469 
7470   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
7471   verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
7472                "                aaaaaaaaa,\n"
7473                "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7474                NoBinPacking);
7475   verifyFormat(
7476       "void f() {\n"
7477       "  aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
7478       "      .aaaaaaa();\n"
7479       "}",
7480       NoBinPacking);
7481   verifyFormat(
7482       "template <class SomeType, class SomeOtherType>\n"
7483       "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
7484       NoBinPacking);
7485 }
7486 
7487 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
7488   FormatStyle Style = getLLVMStyleWithColumns(15);
7489   Style.ExperimentalAutoDetectBinPacking = true;
7490   EXPECT_EQ("aaa(aaaa,\n"
7491             "    aaaa,\n"
7492             "    aaaa);\n"
7493             "aaa(aaaa,\n"
7494             "    aaaa,\n"
7495             "    aaaa);",
7496             format("aaa(aaaa,\n" // one-per-line
7497                    "  aaaa,\n"
7498                    "    aaaa  );\n"
7499                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
7500                    Style));
7501   EXPECT_EQ("aaa(aaaa, aaaa,\n"
7502             "    aaaa);\n"
7503             "aaa(aaaa, aaaa,\n"
7504             "    aaaa);",
7505             format("aaa(aaaa,  aaaa,\n" // bin-packed
7506                    "    aaaa  );\n"
7507                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
7508                    Style));
7509 }
7510 
7511 TEST_F(FormatTest, FormatsBuilderPattern) {
7512   verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n"
7513                "    .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
7514                "    .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
7515                "    .StartsWith(\".init\", ORDER_INIT)\n"
7516                "    .StartsWith(\".fini\", ORDER_FINI)\n"
7517                "    .StartsWith(\".hash\", ORDER_HASH)\n"
7518                "    .Default(ORDER_TEXT);\n");
7519 
7520   verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
7521                "       aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
7522   verifyFormat("aaaaaaa->aaaaaaa\n"
7523                "    ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7524                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7525                "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
7526   verifyFormat(
7527       "aaaaaaa->aaaaaaa\n"
7528       "    ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7529       "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
7530   verifyFormat(
7531       "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
7532       "    aaaaaaaaaaaaaa);");
7533   verifyFormat(
7534       "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
7535       "    aaaaaa->aaaaaaaaaaaa()\n"
7536       "        ->aaaaaaaaaaaaaaaa(\n"
7537       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7538       "        ->aaaaaaaaaaaaaaaaa();");
7539   verifyGoogleFormat(
7540       "void f() {\n"
7541       "  someo->Add((new util::filetools::Handler(dir))\n"
7542       "                 ->OnEvent1(NewPermanentCallback(\n"
7543       "                     this, &HandlerHolderClass::EventHandlerCBA))\n"
7544       "                 ->OnEvent2(NewPermanentCallback(\n"
7545       "                     this, &HandlerHolderClass::EventHandlerCBB))\n"
7546       "                 ->OnEvent3(NewPermanentCallback(\n"
7547       "                     this, &HandlerHolderClass::EventHandlerCBC))\n"
7548       "                 ->OnEvent5(NewPermanentCallback(\n"
7549       "                     this, &HandlerHolderClass::EventHandlerCBD))\n"
7550       "                 ->OnEvent6(NewPermanentCallback(\n"
7551       "                     this, &HandlerHolderClass::EventHandlerCBE)));\n"
7552       "}");
7553 
7554   verifyFormat(
7555       "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
7556   verifyFormat("aaaaaaaaaaaaaaa()\n"
7557                "    .aaaaaaaaaaaaaaa()\n"
7558                "    .aaaaaaaaaaaaaaa()\n"
7559                "    .aaaaaaaaaaaaaaa()\n"
7560                "    .aaaaaaaaaaaaaaa();");
7561   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
7562                "    .aaaaaaaaaaaaaaa()\n"
7563                "    .aaaaaaaaaaaaaaa()\n"
7564                "    .aaaaaaaaaaaaaaa();");
7565   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
7566                "    .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
7567                "    .aaaaaaaaaaaaaaa();");
7568   verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
7569                "    ->aaaaaaaaaaaaaae(0)\n"
7570                "    ->aaaaaaaaaaaaaaa();");
7571 
7572   // Don't linewrap after very short segments.
7573   verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7574                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7575                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
7576   verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7577                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7578                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
7579   verifyFormat("aaa()\n"
7580                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7581                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7582                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
7583 
7584   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
7585                "    .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7586                "    .has<bbbbbbbbbbbbbbbbbbbbb>();");
7587   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
7588                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
7589                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
7590 
7591   // Prefer not to break after empty parentheses.
7592   verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
7593                "    First->LastNewlineOffset);");
7594 
7595   // Prefer not to create "hanging" indents.
7596   verifyFormat(
7597       "return !soooooooooooooome_map\n"
7598       "            .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7599       "            .second;");
7600   verifyFormat(
7601       "return aaaaaaaaaaaaaaaa\n"
7602       "    .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n"
7603       "    .aaaa(aaaaaaaaaaaaaa);");
7604   // No hanging indent here.
7605   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n"
7606                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7607   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n"
7608                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7609   verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
7610                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7611                getLLVMStyleWithColumns(60));
7612   verifyFormat("aaaaaaaaaaaaaaaaaa\n"
7613                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
7614                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7615                getLLVMStyleWithColumns(59));
7616   verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7617                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7618                "    .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7619 
7620   // Dont break if only closing statements before member call
7621   verifyFormat("test() {\n"
7622                "  ([]() -> {\n"
7623                "    int b = 32;\n"
7624                "    return 3;\n"
7625                "  }).foo();\n"
7626                "}");
7627   verifyFormat("test() {\n"
7628                "  (\n"
7629                "      []() -> {\n"
7630                "        int b = 32;\n"
7631                "        return 3;\n"
7632                "      },\n"
7633                "      foo, bar)\n"
7634                "      .foo();\n"
7635                "}");
7636   verifyFormat("test() {\n"
7637                "  ([]() -> {\n"
7638                "    int b = 32;\n"
7639                "    return 3;\n"
7640                "  })\n"
7641                "      .foo()\n"
7642                "      .bar();\n"
7643                "}");
7644   verifyFormat("test() {\n"
7645                "  ([]() -> {\n"
7646                "    int b = 32;\n"
7647                "    return 3;\n"
7648                "  })\n"
7649                "      .foo(\"aaaaaaaaaaaaaaaaa\"\n"
7650                "           \"bbbb\");\n"
7651                "}",
7652                getLLVMStyleWithColumns(30));
7653 }
7654 
7655 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
7656   verifyFormat(
7657       "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
7658       "    bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
7659   verifyFormat(
7660       "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n"
7661       "    bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}");
7662 
7663   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
7664                "    ccccccccccccccccccccccccc) {\n}");
7665   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n"
7666                "    ccccccccccccccccccccccccc) {\n}");
7667 
7668   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
7669                "    ccccccccccccccccccccccccc) {\n}");
7670   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n"
7671                "    ccccccccccccccccccccccccc) {\n}");
7672 
7673   verifyFormat(
7674       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
7675       "    ccccccccccccccccccccccccc) {\n}");
7676   verifyFormat(
7677       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n"
7678       "    ccccccccccccccccccccccccc) {\n}");
7679 
7680   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
7681                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
7682                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
7683                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
7684   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n"
7685                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n"
7686                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n"
7687                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
7688 
7689   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
7690                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
7691                "    aaaaaaaaaaaaaaa != aa) {\n}");
7692   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n"
7693                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n"
7694                "    aaaaaaaaaaaaaaa != aa) {\n}");
7695 }
7696 
7697 TEST_F(FormatTest, BreaksAfterAssignments) {
7698   verifyFormat(
7699       "unsigned Cost =\n"
7700       "    TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
7701       "                        SI->getPointerAddressSpaceee());\n");
7702   verifyFormat(
7703       "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
7704       "    Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
7705 
7706   verifyFormat(
7707       "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
7708       "    aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
7709   verifyFormat("unsigned OriginalStartColumn =\n"
7710                "    SourceMgr.getSpellingColumnNumber(\n"
7711                "        Current.FormatTok.getStartOfNonWhitespace()) -\n"
7712                "    1;");
7713 }
7714 
7715 TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) {
7716   FormatStyle Style = getLLVMStyle();
7717   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
7718                "    bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;",
7719                Style);
7720 
7721   Style.PenaltyBreakAssignment = 20;
7722   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
7723                "                                 cccccccccccccccccccccccccc;",
7724                Style);
7725 }
7726 
7727 TEST_F(FormatTest, AlignsAfterAssignments) {
7728   verifyFormat(
7729       "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7730       "             aaaaaaaaaaaaaaaaaaaaaaaaa;");
7731   verifyFormat(
7732       "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7733       "          aaaaaaaaaaaaaaaaaaaaaaaaa;");
7734   verifyFormat(
7735       "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7736       "           aaaaaaaaaaaaaaaaaaaaaaaaa;");
7737   verifyFormat(
7738       "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7739       "              aaaaaaaaaaaaaaaaaaaaaaaaa);");
7740   verifyFormat(
7741       "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
7742       "                                            aaaaaaaaaaaaaaaaaaaaaaaa +\n"
7743       "                                            aaaaaaaaaaaaaaaaaaaaaaaa;");
7744 }
7745 
7746 TEST_F(FormatTest, AlignsAfterReturn) {
7747   verifyFormat(
7748       "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7749       "       aaaaaaaaaaaaaaaaaaaaaaaaa;");
7750   verifyFormat(
7751       "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7752       "        aaaaaaaaaaaaaaaaaaaaaaaaa);");
7753   verifyFormat(
7754       "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
7755       "       aaaaaaaaaaaaaaaaaaaaaa();");
7756   verifyFormat(
7757       "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
7758       "        aaaaaaaaaaaaaaaaaaaaaa());");
7759   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7760                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7761   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7762                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
7763                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
7764   verifyFormat("return\n"
7765                "    // true if code is one of a or b.\n"
7766                "    code == a || code == b;");
7767 }
7768 
7769 TEST_F(FormatTest, AlignsAfterOpenBracket) {
7770   verifyFormat(
7771       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
7772       "                                                aaaaaaaaa aaaaaaa) {}");
7773   verifyFormat(
7774       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
7775       "                                               aaaaaaaaaaa aaaaaaaaa);");
7776   verifyFormat(
7777       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
7778       "                                             aaaaaaaaaaaaaaaaaaaaa));");
7779   FormatStyle Style = getLLVMStyle();
7780   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
7781   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7782                "    aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}",
7783                Style);
7784   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
7785                "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);",
7786                Style);
7787   verifyFormat("SomeLongVariableName->someFunction(\n"
7788                "    foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));",
7789                Style);
7790   verifyFormat(
7791       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
7792       "    aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
7793       Style);
7794   verifyFormat(
7795       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
7796       "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7797       Style);
7798   verifyFormat(
7799       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
7800       "    aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
7801       Style);
7802 
7803   verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n"
7804                "    ccccccc(aaaaaaaaaaaaaaaaa,         //\n"
7805                "        b));",
7806                Style);
7807 
7808   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
7809   Style.BinPackArguments = false;
7810   Style.BinPackParameters = false;
7811   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7812                "    aaaaaaaaaaa aaaaaaaa,\n"
7813                "    aaaaaaaaa aaaaaaa,\n"
7814                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
7815                Style);
7816   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
7817                "    aaaaaaaaaaa aaaaaaaaa,\n"
7818                "    aaaaaaaaaaa aaaaaaaaa,\n"
7819                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7820                Style);
7821   verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n"
7822                "    aaaaaaaaaaaaaaa,\n"
7823                "    aaaaaaaaaaaaaaaaaaaaa,\n"
7824                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
7825                Style);
7826   verifyFormat(
7827       "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n"
7828       "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
7829       Style);
7830   verifyFormat(
7831       "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n"
7832       "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
7833       Style);
7834   verifyFormat(
7835       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
7836       "    aaaaaaaaaaaaaaaaaaaaa(\n"
7837       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n"
7838       "    aaaaaaaaaaaaaaaa);",
7839       Style);
7840   verifyFormat(
7841       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
7842       "    aaaaaaaaaaaaaaaaaaaaa(\n"
7843       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n"
7844       "    aaaaaaaaaaaaaaaa);",
7845       Style);
7846 }
7847 
7848 TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
7849   FormatStyle Style = getLLVMStyleWithColumns(40);
7850   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
7851                "          bbbbbbbbbbbbbbbbbbbbbb);",
7852                Style);
7853   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
7854   Style.AlignOperands = FormatStyle::OAS_DontAlign;
7855   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
7856                "          bbbbbbbbbbbbbbbbbbbbbb);",
7857                Style);
7858   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
7859   Style.AlignOperands = FormatStyle::OAS_Align;
7860   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
7861                "          bbbbbbbbbbbbbbbbbbbbbb);",
7862                Style);
7863   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
7864   Style.AlignOperands = FormatStyle::OAS_DontAlign;
7865   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
7866                "    bbbbbbbbbbbbbbbbbbbbbb);",
7867                Style);
7868 }
7869 
7870 TEST_F(FormatTest, BreaksConditionalExpressions) {
7871   verifyFormat(
7872       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7873       "                               ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7874       "                               : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7875   verifyFormat(
7876       "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
7877       "     aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7878       "                                : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7879   verifyFormat(
7880       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7881       "                                   : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7882   verifyFormat("aaaa(aaaaaaaaa, aaaaaaaaa,\n"
7883                "     aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7884                "             : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7885   verifyFormat(
7886       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
7887       "                                                    : aaaaaaaaaaaaa);");
7888   verifyFormat(
7889       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7890       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7891       "                                    : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7892       "                   aaaaaaaaaaaaa);");
7893   verifyFormat(
7894       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7895       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7896       "                   aaaaaaaaaaaaa);");
7897   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7898                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7899                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7900                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7901                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7902   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7903                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7904                "           ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7905                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7906                "           : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7907                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7908                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7909   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7910                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7911                "           ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7912                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7913                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7914   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7915                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7916                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
7917   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
7918                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7919                "        ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7920                "        : aaaaaaaaaaaaaaaa;");
7921   verifyFormat(
7922       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7923       "    ? aaaaaaaaaaaaaaa\n"
7924       "    : aaaaaaaaaaaaaaa;");
7925   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
7926                "          aaaaaaaaa\n"
7927                "      ? b\n"
7928                "      : c);");
7929   verifyFormat("return aaaa == bbbb\n"
7930                "           // comment\n"
7931                "           ? aaaa\n"
7932                "           : bbbb;");
7933   verifyFormat("unsigned Indent =\n"
7934                "    format(TheLine.First,\n"
7935                "           IndentForLevel[TheLine.Level] >= 0\n"
7936                "               ? IndentForLevel[TheLine.Level]\n"
7937                "               : TheLine * 2,\n"
7938                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
7939                getLLVMStyleWithColumns(60));
7940   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
7941                "                  ? aaaaaaaaaaaaaaa\n"
7942                "                  : bbbbbbbbbbbbbbb //\n"
7943                "                        ? ccccccccccccccc\n"
7944                "                        : ddddddddddddddd;");
7945   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
7946                "                  ? aaaaaaaaaaaaaaa\n"
7947                "                  : (bbbbbbbbbbbbbbb //\n"
7948                "                         ? ccccccccccccccc\n"
7949                "                         : ddddddddddddddd);");
7950   verifyFormat(
7951       "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7952       "                                      ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7953       "                                            aaaaaaaaaaaaaaaaaaaaa +\n"
7954       "                                            aaaaaaaaaaaaaaaaaaaaa\n"
7955       "                                      : aaaaaaaaaa;");
7956   verifyFormat(
7957       "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7958       "                                   : aaaaaaaaaaaaaaaaaaaaaa\n"
7959       "                      : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
7960 
7961   FormatStyle NoBinPacking = getLLVMStyle();
7962   NoBinPacking.BinPackArguments = false;
7963   verifyFormat(
7964       "void f() {\n"
7965       "  g(aaa,\n"
7966       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
7967       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7968       "        ? aaaaaaaaaaaaaaa\n"
7969       "        : aaaaaaaaaaaaaaa);\n"
7970       "}",
7971       NoBinPacking);
7972   verifyFormat(
7973       "void f() {\n"
7974       "  g(aaa,\n"
7975       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
7976       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7977       "        ?: aaaaaaaaaaaaaaa);\n"
7978       "}",
7979       NoBinPacking);
7980 
7981   verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n"
7982                "             // comment.\n"
7983                "             ccccccccccccccccccccccccccccccccccccccc\n"
7984                "                 ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7985                "                 : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);");
7986 
7987   // Assignments in conditional expressions. Apparently not uncommon :-(.
7988   verifyFormat("return a != b\n"
7989                "           // comment\n"
7990                "           ? a = b\n"
7991                "           : a = b;");
7992   verifyFormat("return a != b\n"
7993                "           // comment\n"
7994                "           ? a = a != b\n"
7995                "                     // comment\n"
7996                "                     ? a = b\n"
7997                "                     : a\n"
7998                "           : a;\n");
7999   verifyFormat("return a != b\n"
8000                "           // comment\n"
8001                "           ? a\n"
8002                "           : a = a != b\n"
8003                "                     // comment\n"
8004                "                     ? a = b\n"
8005                "                     : a;");
8006 
8007   // Chained conditionals
8008   FormatStyle Style = getLLVMStyleWithColumns(70);
8009   Style.AlignOperands = FormatStyle::OAS_Align;
8010   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
8011                "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
8012                "                        : 3333333333333333;",
8013                Style);
8014   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
8015                "       : bbbbbbbbbb     ? 2222222222222222\n"
8016                "                        : 3333333333333333;",
8017                Style);
8018   verifyFormat("return aaaaaaaaaa         ? 1111111111111111\n"
8019                "       : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
8020                "                          : 3333333333333333;",
8021                Style);
8022   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
8023                "       : bbbbbbbbbbbbbb ? 222222\n"
8024                "                        : 333333;",
8025                Style);
8026   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
8027                "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
8028                "       : cccccccccccccc ? 3333333333333333\n"
8029                "                        : 4444444444444444;",
8030                Style);
8031   verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc)\n"
8032                "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
8033                "                        : 3333333333333333;",
8034                Style);
8035   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
8036                "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
8037                "                        : (aaa ? bbb : ccc);",
8038                Style);
8039   verifyFormat(
8040       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
8041       "                                             : cccccccccccccccccc)\n"
8042       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
8043       "                        : 3333333333333333;",
8044       Style);
8045   verifyFormat(
8046       "return aaaaaaaaa        ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
8047       "                                             : cccccccccccccccccc)\n"
8048       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
8049       "                        : 3333333333333333;",
8050       Style);
8051   verifyFormat(
8052       "return aaaaaaaaa        ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
8053       "                                             : dddddddddddddddddd)\n"
8054       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
8055       "                        : 3333333333333333;",
8056       Style);
8057   verifyFormat(
8058       "return aaaaaaaaa        ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
8059       "                                             : dddddddddddddddddd)\n"
8060       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
8061       "                        : 3333333333333333;",
8062       Style);
8063   verifyFormat(
8064       "return aaaaaaaaa        ? 1111111111111111\n"
8065       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
8066       "                        : a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
8067       "                                             : dddddddddddddddddd)\n",
8068       Style);
8069   verifyFormat(
8070       "return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
8071       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
8072       "                        : (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
8073       "                                             : cccccccccccccccccc);",
8074       Style);
8075   verifyFormat(
8076       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
8077       "                           : ccccccccccccccc ? dddddddddddddddddd\n"
8078       "                                             : eeeeeeeeeeeeeeeeee)\n"
8079       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
8080       "                        : 3333333333333333;",
8081       Style);
8082   verifyFormat(
8083       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa    ? bbbbbbbbbbbbbbbbbb\n"
8084       "                           : ccccccccccccccc ? dddddddddddddddddd\n"
8085       "                                             : eeeeeeeeeeeeeeeeee)\n"
8086       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
8087       "                        : 3333333333333333;",
8088       Style);
8089   verifyFormat(
8090       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
8091       "                           : cccccccccccc    ? dddddddddddddddddd\n"
8092       "                                             : eeeeeeeeeeeeeeeeee)\n"
8093       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
8094       "                        : 3333333333333333;",
8095       Style);
8096   verifyFormat(
8097       "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
8098       "                                             : cccccccccccccccccc\n"
8099       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
8100       "                        : 3333333333333333;",
8101       Style);
8102   verifyFormat(
8103       "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
8104       "                          : cccccccccccccccc ? dddddddddddddddddd\n"
8105       "                                             : eeeeeeeeeeeeeeeeee\n"
8106       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
8107       "                        : 3333333333333333;",
8108       Style);
8109   verifyFormat("return aaaaaaaaaaaaaaaaaaaaa\n"
8110                "           ? (aaaaaaaaaaaaaaaaaa   ? bbbbbbbbbbbbbbbbbb\n"
8111                "              : cccccccccccccccccc ? dddddddddddddddddd\n"
8112                "                                   : eeeeeeeeeeeeeeeeee)\n"
8113                "       : bbbbbbbbbbbbbbbbbbb ? 2222222222222222\n"
8114                "                             : 3333333333333333;",
8115                Style);
8116   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaa\n"
8117                "           ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
8118                "             : cccccccccccccccc ? dddddddddddddddddd\n"
8119                "                                : eeeeeeeeeeeeeeeeee\n"
8120                "       : bbbbbbbbbbbbbbbbbbbbbbb ? 2222222222222222\n"
8121                "                                 : 3333333333333333;",
8122                Style);
8123 
8124   Style.AlignOperands = FormatStyle::OAS_DontAlign;
8125   Style.BreakBeforeTernaryOperators = false;
8126   // FIXME: Aligning the question marks is weird given DontAlign.
8127   // Consider disabling this alignment in this case. Also check whether this
8128   // will render the adjustment from https://reviews.llvm.org/D82199
8129   // unnecessary.
8130   verifyFormat("int x = aaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa :\n"
8131                "    bbbb                ? cccccccccccccccccc :\n"
8132                "                          ddddd;\n",
8133                Style);
8134 
8135   EXPECT_EQ(
8136       "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n"
8137       "    /*\n"
8138       "     */\n"
8139       "    function() {\n"
8140       "      try {\n"
8141       "        return JJJJJJJJJJJJJJ(\n"
8142       "            pppppppppppppppppppppppppppppppppppppppppppppppppp);\n"
8143       "      }\n"
8144       "    } :\n"
8145       "    function() {};",
8146       format(
8147           "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n"
8148           "     /*\n"
8149           "      */\n"
8150           "     function() {\n"
8151           "      try {\n"
8152           "        return JJJJJJJJJJJJJJ(\n"
8153           "            pppppppppppppppppppppppppppppppppppppppppppppppppp);\n"
8154           "      }\n"
8155           "    } :\n"
8156           "    function() {};",
8157           getGoogleStyle(FormatStyle::LK_JavaScript)));
8158 }
8159 
8160 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
8161   FormatStyle Style = getLLVMStyleWithColumns(70);
8162   Style.BreakBeforeTernaryOperators = false;
8163   verifyFormat(
8164       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
8165       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
8166       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
8167       Style);
8168   verifyFormat(
8169       "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
8170       "     aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
8171       "                                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
8172       Style);
8173   verifyFormat(
8174       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
8175       "                                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
8176       Style);
8177   verifyFormat("aaaa(aaaaaaaa, aaaaaaaaaa,\n"
8178                "     aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
8179                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
8180                Style);
8181   verifyFormat(
8182       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
8183       "                                                      aaaaaaaaaaaaa);",
8184       Style);
8185   verifyFormat(
8186       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8187       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
8188       "                                      aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8189       "                   aaaaaaaaaaaaa);",
8190       Style);
8191   verifyFormat(
8192       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8193       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8194       "                   aaaaaaaaaaaaa);",
8195       Style);
8196   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
8197                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8198                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
8199                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8200                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
8201                Style);
8202   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8203                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
8204                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8205                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
8206                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8207                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
8208                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
8209                Style);
8210   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8211                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n"
8212                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8213                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
8214                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
8215                Style);
8216   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
8217                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
8218                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
8219                Style);
8220   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
8221                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
8222                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
8223                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
8224                Style);
8225   verifyFormat(
8226       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
8227       "    aaaaaaaaaaaaaaa :\n"
8228       "    aaaaaaaaaaaaaaa;",
8229       Style);
8230   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
8231                "          aaaaaaaaa ?\n"
8232                "      b :\n"
8233                "      c);",
8234                Style);
8235   verifyFormat("unsigned Indent =\n"
8236                "    format(TheLine.First,\n"
8237                "           IndentForLevel[TheLine.Level] >= 0 ?\n"
8238                "               IndentForLevel[TheLine.Level] :\n"
8239                "               TheLine * 2,\n"
8240                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
8241                Style);
8242   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
8243                "                  aaaaaaaaaaaaaaa :\n"
8244                "                  bbbbbbbbbbbbbbb ? //\n"
8245                "                      ccccccccccccccc :\n"
8246                "                      ddddddddddddddd;",
8247                Style);
8248   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
8249                "                  aaaaaaaaaaaaaaa :\n"
8250                "                  (bbbbbbbbbbbbbbb ? //\n"
8251                "                       ccccccccccccccc :\n"
8252                "                       ddddddddddddddd);",
8253                Style);
8254   verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
8255                "            /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n"
8256                "            ccccccccccccccccccccccccccc;",
8257                Style);
8258   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
8259                "           aaaaa :\n"
8260                "           bbbbbbbbbbbbbbb + cccccccccccccccc;",
8261                Style);
8262 
8263   // Chained conditionals
8264   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
8265                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8266                "                          3333333333333333;",
8267                Style);
8268   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
8269                "       bbbbbbbbbb       ? 2222222222222222 :\n"
8270                "                          3333333333333333;",
8271                Style);
8272   verifyFormat("return aaaaaaaaaa       ? 1111111111111111 :\n"
8273                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8274                "                          3333333333333333;",
8275                Style);
8276   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
8277                "       bbbbbbbbbbbbbbbb ? 222222 :\n"
8278                "                          333333;",
8279                Style);
8280   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
8281                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8282                "       cccccccccccccccc ? 3333333333333333 :\n"
8283                "                          4444444444444444;",
8284                Style);
8285   verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc) :\n"
8286                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8287                "                          3333333333333333;",
8288                Style);
8289   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
8290                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8291                "                          (aaa ? bbb : ccc);",
8292                Style);
8293   verifyFormat(
8294       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8295       "                                               cccccccccccccccccc) :\n"
8296       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8297       "                          3333333333333333;",
8298       Style);
8299   verifyFormat(
8300       "return aaaaaaaaa        ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8301       "                                               cccccccccccccccccc) :\n"
8302       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8303       "                          3333333333333333;",
8304       Style);
8305   verifyFormat(
8306       "return aaaaaaaaa        ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8307       "                                               dddddddddddddddddd) :\n"
8308       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8309       "                          3333333333333333;",
8310       Style);
8311   verifyFormat(
8312       "return aaaaaaaaa        ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8313       "                                               dddddddddddddddddd) :\n"
8314       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8315       "                          3333333333333333;",
8316       Style);
8317   verifyFormat(
8318       "return aaaaaaaaa        ? 1111111111111111 :\n"
8319       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8320       "                          a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8321       "                                               dddddddddddddddddd)\n",
8322       Style);
8323   verifyFormat(
8324       "return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
8325       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8326       "                          (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8327       "                                               cccccccccccccccccc);",
8328       Style);
8329   verifyFormat(
8330       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8331       "                           ccccccccccccccccc ? dddddddddddddddddd :\n"
8332       "                                               eeeeeeeeeeeeeeeeee) :\n"
8333       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8334       "                          3333333333333333;",
8335       Style);
8336   verifyFormat(
8337       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8338       "                           ccccccccccccc     ? dddddddddddddddddd :\n"
8339       "                                               eeeeeeeeeeeeeeeeee) :\n"
8340       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8341       "                          3333333333333333;",
8342       Style);
8343   verifyFormat(
8344       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaa     ? bbbbbbbbbbbbbbbbbb :\n"
8345       "                           ccccccccccccccccc ? dddddddddddddddddd :\n"
8346       "                                               eeeeeeeeeeeeeeeeee) :\n"
8347       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8348       "                          3333333333333333;",
8349       Style);
8350   verifyFormat(
8351       "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8352       "                                               cccccccccccccccccc :\n"
8353       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8354       "                          3333333333333333;",
8355       Style);
8356   verifyFormat(
8357       "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8358       "                          cccccccccccccccccc ? dddddddddddddddddd :\n"
8359       "                                               eeeeeeeeeeeeeeeeee :\n"
8360       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8361       "                          3333333333333333;",
8362       Style);
8363   verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n"
8364                "           (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8365                "            cccccccccccccccccc ? dddddddddddddddddd :\n"
8366                "                                 eeeeeeeeeeeeeeeeee) :\n"
8367                "       bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8368                "                               3333333333333333;",
8369                Style);
8370   verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n"
8371                "           aaaaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8372                "           cccccccccccccccccccc ? dddddddddddddddddd :\n"
8373                "                                  eeeeeeeeeeeeeeeeee :\n"
8374                "       bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8375                "                               3333333333333333;",
8376                Style);
8377 }
8378 
8379 TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
8380   verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
8381                "     aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
8382   verifyFormat("bool a = true, b = false;");
8383 
8384   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
8385                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
8386                "     bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
8387                "         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
8388   verifyFormat(
8389       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
8390       "         bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
8391       "     d = e && f;");
8392   verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
8393                "          c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
8394   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
8395                "          *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
8396   verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
8397                "          ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
8398 
8399   FormatStyle Style = getGoogleStyle();
8400   Style.PointerAlignment = FormatStyle::PAS_Left;
8401   Style.DerivePointerAlignment = false;
8402   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8403                "    *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
8404                "    *b = bbbbbbbbbbbbbbbbbbb;",
8405                Style);
8406   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
8407                "          *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;",
8408                Style);
8409   verifyFormat("vector<int*> a, b;", Style);
8410   verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style);
8411   verifyFormat("/*comment*/ for (int *p, *q; p != q; p = p->next) {\n}", Style);
8412   verifyFormat("if (int *p, *q; p != q) {\n  p = p->next;\n}", Style);
8413   verifyFormat("/*comment*/ if (int *p, *q; p != q) {\n  p = p->next;\n}",
8414                Style);
8415   verifyFormat("switch (int *p, *q; p != q) {\n  default:\n    break;\n}",
8416                Style);
8417   verifyFormat(
8418       "/*comment*/ switch (int *p, *q; p != q) {\n  default:\n    break;\n}",
8419       Style);
8420 
8421   verifyFormat("if ([](int* p, int* q) {}()) {\n}", Style);
8422   verifyFormat("for ([](int* p, int* q) {}();;) {\n}", Style);
8423   verifyFormat("for (; [](int* p, int* q) {}();) {\n}", Style);
8424   verifyFormat("for (;; [](int* p, int* q) {}()) {\n}", Style);
8425   verifyFormat("switch ([](int* p, int* q) {}()) {\n  default:\n    break;\n}",
8426                Style);
8427 }
8428 
8429 TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
8430   verifyFormat("arr[foo ? bar : baz];");
8431   verifyFormat("f()[foo ? bar : baz];");
8432   verifyFormat("(a + b)[foo ? bar : baz];");
8433   verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
8434 }
8435 
8436 TEST_F(FormatTest, AlignsStringLiterals) {
8437   verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
8438                "                                      \"short literal\");");
8439   verifyFormat(
8440       "looooooooooooooooooooooooongFunction(\n"
8441       "    \"short literal\"\n"
8442       "    \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
8443   verifyFormat("someFunction(\"Always break between multi-line\"\n"
8444                "             \" string literals\",\n"
8445                "             and, other, parameters);");
8446   EXPECT_EQ("fun + \"1243\" /* comment */\n"
8447             "      \"5678\";",
8448             format("fun + \"1243\" /* comment */\n"
8449                    "    \"5678\";",
8450                    getLLVMStyleWithColumns(28)));
8451   EXPECT_EQ(
8452       "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
8453       "         \"aaaaaaaaaaaaaaaaaaaaa\"\n"
8454       "         \"aaaaaaaaaaaaaaaa\";",
8455       format("aaaaaa ="
8456              "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
8457              "aaaaaaaaaaaaaaaaaaaaa\" "
8458              "\"aaaaaaaaaaaaaaaa\";"));
8459   verifyFormat("a = a + \"a\"\n"
8460                "        \"a\"\n"
8461                "        \"a\";");
8462   verifyFormat("f(\"a\", \"b\"\n"
8463                "       \"c\");");
8464 
8465   verifyFormat(
8466       "#define LL_FORMAT \"ll\"\n"
8467       "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
8468       "       \"d, ddddddddd: %\" LL_FORMAT \"d\");");
8469 
8470   verifyFormat("#define A(X)          \\\n"
8471                "  \"aaaaa\" #X \"bbbbbb\" \\\n"
8472                "  \"ccccc\"",
8473                getLLVMStyleWithColumns(23));
8474   verifyFormat("#define A \"def\"\n"
8475                "f(\"abc\" A \"ghi\"\n"
8476                "  \"jkl\");");
8477 
8478   verifyFormat("f(L\"a\"\n"
8479                "  L\"b\");");
8480   verifyFormat("#define A(X)            \\\n"
8481                "  L\"aaaaa\" #X L\"bbbbbb\" \\\n"
8482                "  L\"ccccc\"",
8483                getLLVMStyleWithColumns(25));
8484 
8485   verifyFormat("f(@\"a\"\n"
8486                "  @\"b\");");
8487   verifyFormat("NSString s = @\"a\"\n"
8488                "             @\"b\"\n"
8489                "             @\"c\";");
8490   verifyFormat("NSString s = @\"a\"\n"
8491                "              \"b\"\n"
8492                "              \"c\";");
8493 }
8494 
8495 TEST_F(FormatTest, ReturnTypeBreakingStyle) {
8496   FormatStyle Style = getLLVMStyle();
8497   // No declarations or definitions should be moved to own line.
8498   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None;
8499   verifyFormat("class A {\n"
8500                "  int f() { return 1; }\n"
8501                "  int g();\n"
8502                "};\n"
8503                "int f() { return 1; }\n"
8504                "int g();\n",
8505                Style);
8506 
8507   // All declarations and definitions should have the return type moved to its
8508   // own line.
8509   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
8510   Style.TypenameMacros = {"LIST"};
8511   verifyFormat("SomeType\n"
8512                "funcdecl(LIST(uint64_t));",
8513                Style);
8514   verifyFormat("class E {\n"
8515                "  int\n"
8516                "  f() {\n"
8517                "    return 1;\n"
8518                "  }\n"
8519                "  int\n"
8520                "  g();\n"
8521                "};\n"
8522                "int\n"
8523                "f() {\n"
8524                "  return 1;\n"
8525                "}\n"
8526                "int\n"
8527                "g();\n",
8528                Style);
8529 
8530   // Top-level definitions, and no kinds of declarations should have the
8531   // return type moved to its own line.
8532   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions;
8533   verifyFormat("class B {\n"
8534                "  int f() { return 1; }\n"
8535                "  int g();\n"
8536                "};\n"
8537                "int\n"
8538                "f() {\n"
8539                "  return 1;\n"
8540                "}\n"
8541                "int g();\n",
8542                Style);
8543 
8544   // Top-level definitions and declarations should have the return type moved
8545   // to its own line.
8546   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel;
8547   verifyFormat("class C {\n"
8548                "  int f() { return 1; }\n"
8549                "  int g();\n"
8550                "};\n"
8551                "int\n"
8552                "f() {\n"
8553                "  return 1;\n"
8554                "}\n"
8555                "int\n"
8556                "g();\n",
8557                Style);
8558 
8559   // All definitions should have the return type moved to its own line, but no
8560   // kinds of declarations.
8561   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
8562   verifyFormat("class D {\n"
8563                "  int\n"
8564                "  f() {\n"
8565                "    return 1;\n"
8566                "  }\n"
8567                "  int g();\n"
8568                "};\n"
8569                "int\n"
8570                "f() {\n"
8571                "  return 1;\n"
8572                "}\n"
8573                "int g();\n",
8574                Style);
8575   verifyFormat("const char *\n"
8576                "f(void) {\n" // Break here.
8577                "  return \"\";\n"
8578                "}\n"
8579                "const char *bar(void);\n", // No break here.
8580                Style);
8581   verifyFormat("template <class T>\n"
8582                "T *\n"
8583                "f(T &c) {\n" // Break here.
8584                "  return NULL;\n"
8585                "}\n"
8586                "template <class T> T *f(T &c);\n", // No break here.
8587                Style);
8588   verifyFormat("class C {\n"
8589                "  int\n"
8590                "  operator+() {\n"
8591                "    return 1;\n"
8592                "  }\n"
8593                "  int\n"
8594                "  operator()() {\n"
8595                "    return 1;\n"
8596                "  }\n"
8597                "};\n",
8598                Style);
8599   verifyFormat("void\n"
8600                "A::operator()() {}\n"
8601                "void\n"
8602                "A::operator>>() {}\n"
8603                "void\n"
8604                "A::operator+() {}\n"
8605                "void\n"
8606                "A::operator*() {}\n"
8607                "void\n"
8608                "A::operator->() {}\n"
8609                "void\n"
8610                "A::operator void *() {}\n"
8611                "void\n"
8612                "A::operator void &() {}\n"
8613                "void\n"
8614                "A::operator void &&() {}\n"
8615                "void\n"
8616                "A::operator char *() {}\n"
8617                "void\n"
8618                "A::operator[]() {}\n"
8619                "void\n"
8620                "A::operator!() {}\n"
8621                "void\n"
8622                "A::operator**() {}\n"
8623                "void\n"
8624                "A::operator<Foo> *() {}\n"
8625                "void\n"
8626                "A::operator<Foo> **() {}\n"
8627                "void\n"
8628                "A::operator<Foo> &() {}\n"
8629                "void\n"
8630                "A::operator void **() {}\n",
8631                Style);
8632   verifyFormat("constexpr auto\n"
8633                "operator()() const -> reference {}\n"
8634                "constexpr auto\n"
8635                "operator>>() const -> reference {}\n"
8636                "constexpr auto\n"
8637                "operator+() const -> reference {}\n"
8638                "constexpr auto\n"
8639                "operator*() const -> reference {}\n"
8640                "constexpr auto\n"
8641                "operator->() const -> reference {}\n"
8642                "constexpr auto\n"
8643                "operator++() const -> reference {}\n"
8644                "constexpr auto\n"
8645                "operator void *() const -> reference {}\n"
8646                "constexpr auto\n"
8647                "operator void **() const -> reference {}\n"
8648                "constexpr auto\n"
8649                "operator void *() const -> reference {}\n"
8650                "constexpr auto\n"
8651                "operator void &() const -> reference {}\n"
8652                "constexpr auto\n"
8653                "operator void &&() const -> reference {}\n"
8654                "constexpr auto\n"
8655                "operator char *() const -> reference {}\n"
8656                "constexpr auto\n"
8657                "operator!() const -> reference {}\n"
8658                "constexpr auto\n"
8659                "operator[]() const -> reference {}\n",
8660                Style);
8661   verifyFormat("void *operator new(std::size_t s);", // No break here.
8662                Style);
8663   verifyFormat("void *\n"
8664                "operator new(std::size_t s) {}",
8665                Style);
8666   verifyFormat("void *\n"
8667                "operator delete[](void *ptr) {}",
8668                Style);
8669   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
8670   verifyFormat("const char *\n"
8671                "f(void)\n" // Break here.
8672                "{\n"
8673                "  return \"\";\n"
8674                "}\n"
8675                "const char *bar(void);\n", // No break here.
8676                Style);
8677   verifyFormat("template <class T>\n"
8678                "T *\n"     // Problem here: no line break
8679                "f(T &c)\n" // Break here.
8680                "{\n"
8681                "  return NULL;\n"
8682                "}\n"
8683                "template <class T> T *f(T &c);\n", // No break here.
8684                Style);
8685   verifyFormat("int\n"
8686                "foo(A<bool> a)\n"
8687                "{\n"
8688                "  return a;\n"
8689                "}\n",
8690                Style);
8691   verifyFormat("int\n"
8692                "foo(A<8> a)\n"
8693                "{\n"
8694                "  return a;\n"
8695                "}\n",
8696                Style);
8697   verifyFormat("int\n"
8698                "foo(A<B<bool>, 8> a)\n"
8699                "{\n"
8700                "  return a;\n"
8701                "}\n",
8702                Style);
8703   verifyFormat("int\n"
8704                "foo(A<B<8>, bool> a)\n"
8705                "{\n"
8706                "  return a;\n"
8707                "}\n",
8708                Style);
8709   verifyFormat("int\n"
8710                "foo(A<B<bool>, bool> a)\n"
8711                "{\n"
8712                "  return a;\n"
8713                "}\n",
8714                Style);
8715   verifyFormat("int\n"
8716                "foo(A<B<8>, 8> a)\n"
8717                "{\n"
8718                "  return a;\n"
8719                "}\n",
8720                Style);
8721 
8722   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
8723   Style.BraceWrapping.AfterFunction = true;
8724   verifyFormat("int f(i);\n" // No break here.
8725                "int\n"       // Break here.
8726                "f(i)\n"
8727                "{\n"
8728                "  return i + 1;\n"
8729                "}\n"
8730                "int\n" // Break here.
8731                "f(i)\n"
8732                "{\n"
8733                "  return i + 1;\n"
8734                "};",
8735                Style);
8736   verifyFormat("int f(a, b, c);\n" // No break here.
8737                "int\n"             // Break here.
8738                "f(a, b, c)\n"      // Break here.
8739                "short a, b;\n"
8740                "float c;\n"
8741                "{\n"
8742                "  return a + b < c;\n"
8743                "}\n"
8744                "int\n"        // Break here.
8745                "f(a, b, c)\n" // Break here.
8746                "short a, b;\n"
8747                "float c;\n"
8748                "{\n"
8749                "  return a + b < c;\n"
8750                "};",
8751                Style);
8752   verifyFormat("byte *\n" // Break here.
8753                "f(a)\n"   // Break here.
8754                "byte a[];\n"
8755                "{\n"
8756                "  return a;\n"
8757                "}",
8758                Style);
8759   verifyFormat("bool f(int a, int) override;\n"
8760                "Bar g(int a, Bar) final;\n"
8761                "Bar h(a, Bar) final;",
8762                Style);
8763   verifyFormat("int\n"
8764                "f(a)",
8765                Style);
8766   verifyFormat("bool\n"
8767                "f(size_t = 0, bool b = false)\n"
8768                "{\n"
8769                "  return !b;\n"
8770                "}",
8771                Style);
8772 
8773   // The return breaking style doesn't affect:
8774   // * function and object definitions with attribute-like macros
8775   verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n"
8776                "    ABSL_GUARDED_BY(mutex) = {};",
8777                getGoogleStyleWithColumns(40));
8778   verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n"
8779                "    ABSL_GUARDED_BY(mutex);  // comment",
8780                getGoogleStyleWithColumns(40));
8781   verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n"
8782                "    ABSL_GUARDED_BY(mutex1)\n"
8783                "        ABSL_GUARDED_BY(mutex2);",
8784                getGoogleStyleWithColumns(40));
8785   verifyFormat("Tttttt f(int a, int b)\n"
8786                "    ABSL_GUARDED_BY(mutex1)\n"
8787                "        ABSL_GUARDED_BY(mutex2);",
8788                getGoogleStyleWithColumns(40));
8789   // * typedefs
8790   verifyFormat("typedef ATTR(X) char x;", getGoogleStyle());
8791 
8792   Style = getGNUStyle();
8793 
8794   // Test for comments at the end of function declarations.
8795   verifyFormat("void\n"
8796                "foo (int a, /*abc*/ int b) // def\n"
8797                "{\n"
8798                "}\n",
8799                Style);
8800 
8801   verifyFormat("void\n"
8802                "foo (int a, /* abc */ int b) /* def */\n"
8803                "{\n"
8804                "}\n",
8805                Style);
8806 
8807   // Definitions that should not break after return type
8808   verifyFormat("void foo (int a, int b); // def\n", Style);
8809   verifyFormat("void foo (int a, int b); /* def */\n", Style);
8810   verifyFormat("void foo (int a, int b);\n", Style);
8811 }
8812 
8813 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
8814   FormatStyle NoBreak = getLLVMStyle();
8815   NoBreak.AlwaysBreakBeforeMultilineStrings = false;
8816   FormatStyle Break = getLLVMStyle();
8817   Break.AlwaysBreakBeforeMultilineStrings = true;
8818   verifyFormat("aaaa = \"bbbb\"\n"
8819                "       \"cccc\";",
8820                NoBreak);
8821   verifyFormat("aaaa =\n"
8822                "    \"bbbb\"\n"
8823                "    \"cccc\";",
8824                Break);
8825   verifyFormat("aaaa(\"bbbb\"\n"
8826                "     \"cccc\");",
8827                NoBreak);
8828   verifyFormat("aaaa(\n"
8829                "    \"bbbb\"\n"
8830                "    \"cccc\");",
8831                Break);
8832   verifyFormat("aaaa(qqq, \"bbbb\"\n"
8833                "          \"cccc\");",
8834                NoBreak);
8835   verifyFormat("aaaa(qqq,\n"
8836                "     \"bbbb\"\n"
8837                "     \"cccc\");",
8838                Break);
8839   verifyFormat("aaaa(qqq,\n"
8840                "     L\"bbbb\"\n"
8841                "     L\"cccc\");",
8842                Break);
8843   verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n"
8844                "                      \"bbbb\"));",
8845                Break);
8846   verifyFormat("string s = someFunction(\n"
8847                "    \"abc\"\n"
8848                "    \"abc\");",
8849                Break);
8850 
8851   // As we break before unary operators, breaking right after them is bad.
8852   verifyFormat("string foo = abc ? \"x\"\n"
8853                "                   \"blah blah blah blah blah blah\"\n"
8854                "                 : \"y\";",
8855                Break);
8856 
8857   // Don't break if there is no column gain.
8858   verifyFormat("f(\"aaaa\"\n"
8859                "  \"bbbb\");",
8860                Break);
8861 
8862   // Treat literals with escaped newlines like multi-line string literals.
8863   EXPECT_EQ("x = \"a\\\n"
8864             "b\\\n"
8865             "c\";",
8866             format("x = \"a\\\n"
8867                    "b\\\n"
8868                    "c\";",
8869                    NoBreak));
8870   EXPECT_EQ("xxxx =\n"
8871             "    \"a\\\n"
8872             "b\\\n"
8873             "c\";",
8874             format("xxxx = \"a\\\n"
8875                    "b\\\n"
8876                    "c\";",
8877                    Break));
8878 
8879   EXPECT_EQ("NSString *const kString =\n"
8880             "    @\"aaaa\"\n"
8881             "    @\"bbbb\";",
8882             format("NSString *const kString = @\"aaaa\"\n"
8883                    "@\"bbbb\";",
8884                    Break));
8885 
8886   Break.ColumnLimit = 0;
8887   verifyFormat("const char *hello = \"hello llvm\";", Break);
8888 }
8889 
8890 TEST_F(FormatTest, AlignsPipes) {
8891   verifyFormat(
8892       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8893       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8894       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8895   verifyFormat(
8896       "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
8897       "                     << aaaaaaaaaaaaaaaaaaaa;");
8898   verifyFormat(
8899       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8900       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8901   verifyFormat(
8902       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
8903       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8904   verifyFormat(
8905       "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
8906       "                \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
8907       "             << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
8908   verifyFormat(
8909       "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8910       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8911       "         << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8912   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8913                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8914                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8915                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
8916   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n"
8917                "             << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);");
8918   verifyFormat(
8919       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8920       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8921   verifyFormat(
8922       "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n"
8923       "                                       aaaaaaaaaaaaaaaaaaaaaaaaaa);");
8924 
8925   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
8926                "             << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
8927   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8928                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8929                "                    aaaaaaaaaaaaaaaaaaaaa)\n"
8930                "             << aaaaaaaaaaaaaaaaaaaaaaaaaa;");
8931   verifyFormat("LOG_IF(aaa == //\n"
8932                "       bbb)\n"
8933                "    << a << b;");
8934 
8935   // But sometimes, breaking before the first "<<" is desirable.
8936   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
8937                "    << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
8938   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
8939                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8940                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8941   verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
8942                "    << BEF << IsTemplate << Description << E->getType();");
8943   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
8944                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8945                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8946   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
8947                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8948                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8949                "    << aaa;");
8950 
8951   verifyFormat(
8952       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8953       "                    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
8954 
8955   // Incomplete string literal.
8956   EXPECT_EQ("llvm::errs() << \"\n"
8957             "             << a;",
8958             format("llvm::errs() << \"\n<<a;"));
8959 
8960   verifyFormat("void f() {\n"
8961                "  CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
8962                "      << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
8963                "}");
8964 
8965   // Handle 'endl'.
8966   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n"
8967                "             << bbbbbbbbbbbbbbbbbbbbbb << endl;");
8968   verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;");
8969 
8970   // Handle '\n'.
8971   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n"
8972                "             << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
8973   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n"
8974                "             << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';");
8975   verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n"
8976                "             << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";");
8977   verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
8978 }
8979 
8980 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) {
8981   verifyFormat("return out << \"somepacket = {\\n\"\n"
8982                "           << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
8983                "           << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
8984                "           << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
8985                "           << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
8986                "           << \"}\";");
8987 
8988   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
8989                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
8990                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
8991   verifyFormat(
8992       "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
8993       "             << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
8994       "             << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
8995       "             << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
8996       "             << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
8997   verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
8998                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
8999   verifyFormat(
9000       "void f() {\n"
9001       "  llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
9002       "               << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
9003       "}");
9004 
9005   // Breaking before the first "<<" is generally not desirable.
9006   verifyFormat(
9007       "llvm::errs()\n"
9008       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9009       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9010       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9011       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
9012       getLLVMStyleWithColumns(70));
9013   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
9014                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9015                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
9016                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9017                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
9018                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
9019                getLLVMStyleWithColumns(70));
9020 
9021   verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
9022                "           \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
9023                "           \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;");
9024   verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
9025                "                  \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
9026                "                  \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);");
9027   verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n"
9028                "           (aaaa + aaaa);",
9029                getLLVMStyleWithColumns(40));
9030   verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n"
9031                "                  (aaaaaaa + aaaaa));",
9032                getLLVMStyleWithColumns(40));
9033   verifyFormat(
9034       "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n"
9035       "                  SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n"
9036       "                  bbbbbbbbbbbbbbbbbbbbbbb);");
9037 }
9038 
9039 TEST_F(FormatTest, UnderstandsEquals) {
9040   verifyFormat(
9041       "aaaaaaaaaaaaaaaaa =\n"
9042       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
9043   verifyFormat(
9044       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
9045       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
9046   verifyFormat(
9047       "if (a) {\n"
9048       "  f();\n"
9049       "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
9050       "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
9051       "}");
9052 
9053   verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
9054                "        100000000 + 10000000) {\n}");
9055 }
9056 
9057 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
9058   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
9059                "    .looooooooooooooooooooooooooooooooooooooongFunction();");
9060 
9061   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
9062                "    ->looooooooooooooooooooooooooooooooooooooongFunction();");
9063 
9064   verifyFormat(
9065       "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
9066       "                                                          Parameter2);");
9067 
9068   verifyFormat(
9069       "ShortObject->shortFunction(\n"
9070       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
9071       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
9072 
9073   verifyFormat("loooooooooooooongFunction(\n"
9074                "    LoooooooooooooongObject->looooooooooooooooongFunction());");
9075 
9076   verifyFormat(
9077       "function(LoooooooooooooooooooooooooooooooooooongObject\n"
9078       "             ->loooooooooooooooooooooooooooooooooooooooongFunction());");
9079 
9080   verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
9081                "    .WillRepeatedly(Return(SomeValue));");
9082   verifyFormat("void f() {\n"
9083                "  EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
9084                "      .Times(2)\n"
9085                "      .WillRepeatedly(Return(SomeValue));\n"
9086                "}");
9087   verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
9088                "    ccccccccccccccccccccccc);");
9089   verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9090                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9091                "          .aaaaa(aaaaa),\n"
9092                "      aaaaaaaaaaaaaaaaaaaaa);");
9093   verifyFormat("void f() {\n"
9094                "  aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9095                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
9096                "}");
9097   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9098                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9099                "    .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9100                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9101                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
9102   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9103                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9104                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9105                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
9106                "}");
9107 
9108   // Here, it is not necessary to wrap at "." or "->".
9109   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
9110                "    aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
9111   verifyFormat(
9112       "aaaaaaaaaaa->aaaaaaaaa(\n"
9113       "    aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9114       "    aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
9115 
9116   verifyFormat(
9117       "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9118       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
9119   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
9120                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
9121   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
9122                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
9123 
9124   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9125                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9126                "    .a();");
9127 
9128   FormatStyle NoBinPacking = getLLVMStyle();
9129   NoBinPacking.BinPackParameters = false;
9130   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
9131                "    .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
9132                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
9133                "                         aaaaaaaaaaaaaaaaaaa,\n"
9134                "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9135                NoBinPacking);
9136 
9137   // If there is a subsequent call, change to hanging indentation.
9138   verifyFormat(
9139       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9140       "                         aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
9141       "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
9142   verifyFormat(
9143       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9144       "    aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
9145   verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9146                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9147                "                 .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
9148   verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9149                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9150                "               .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
9151 }
9152 
9153 TEST_F(FormatTest, WrapsTemplateDeclarations) {
9154   verifyFormat("template <typename T>\n"
9155                "virtual void loooooooooooongFunction(int Param1, int Param2);");
9156   verifyFormat("template <typename T>\n"
9157                "// T should be one of {A, B}.\n"
9158                "virtual void loooooooooooongFunction(int Param1, int Param2);");
9159   verifyFormat(
9160       "template <typename T>\n"
9161       "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
9162   verifyFormat("template <typename T>\n"
9163                "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
9164                "       int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
9165   verifyFormat(
9166       "template <typename T>\n"
9167       "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
9168       "                                      int Paaaaaaaaaaaaaaaaaaaaram2);");
9169   verifyFormat(
9170       "template <typename T>\n"
9171       "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
9172       "                    aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
9173       "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9174   verifyFormat("template <typename T>\n"
9175                "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9176                "    int aaaaaaaaaaaaaaaaaaaaaa);");
9177   verifyFormat(
9178       "template <typename T1, typename T2 = char, typename T3 = char,\n"
9179       "          typename T4 = char>\n"
9180       "void f();");
9181   verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
9182                "          template <typename> class cccccccccccccccccccccc,\n"
9183                "          typename ddddddddddddd>\n"
9184                "class C {};");
9185   verifyFormat(
9186       "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
9187       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9188 
9189   verifyFormat("void f() {\n"
9190                "  a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
9191                "      a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
9192                "}");
9193 
9194   verifyFormat("template <typename T> class C {};");
9195   verifyFormat("template <typename T> void f();");
9196   verifyFormat("template <typename T> void f() {}");
9197   verifyFormat(
9198       "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
9199       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9200       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
9201       "    new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
9202       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9203       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
9204       "        bbbbbbbbbbbbbbbbbbbbbbbb);",
9205       getLLVMStyleWithColumns(72));
9206   EXPECT_EQ("static_cast<A< //\n"
9207             "    B> *>(\n"
9208             "\n"
9209             ");",
9210             format("static_cast<A<//\n"
9211                    "    B>*>(\n"
9212                    "\n"
9213                    "    );"));
9214   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9215                "    const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
9216 
9217   FormatStyle AlwaysBreak = getLLVMStyle();
9218   AlwaysBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
9219   verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
9220   verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
9221   verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
9222   verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9223                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
9224                "    ccccccccccccccccccccccccccccccccccccccccccccccc);");
9225   verifyFormat("template <template <typename> class Fooooooo,\n"
9226                "          template <typename> class Baaaaaaar>\n"
9227                "struct C {};",
9228                AlwaysBreak);
9229   verifyFormat("template <typename T> // T can be A, B or C.\n"
9230                "struct C {};",
9231                AlwaysBreak);
9232   verifyFormat("template <enum E> class A {\n"
9233                "public:\n"
9234                "  E *f();\n"
9235                "};");
9236 
9237   FormatStyle NeverBreak = getLLVMStyle();
9238   NeverBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_No;
9239   verifyFormat("template <typename T> class C {};", NeverBreak);
9240   verifyFormat("template <typename T> void f();", NeverBreak);
9241   verifyFormat("template <typename T> void f() {}", NeverBreak);
9242   verifyFormat("template <typename T>\nvoid foo(aaaaaaaaaaaaaaaaaaaaaaaaaa "
9243                "bbbbbbbbbbbbbbbbbbbb) {}",
9244                NeverBreak);
9245   verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9246                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
9247                "    ccccccccccccccccccccccccccccccccccccccccccccccc);",
9248                NeverBreak);
9249   verifyFormat("template <template <typename> class Fooooooo,\n"
9250                "          template <typename> class Baaaaaaar>\n"
9251                "struct C {};",
9252                NeverBreak);
9253   verifyFormat("template <typename T> // T can be A, B or C.\n"
9254                "struct C {};",
9255                NeverBreak);
9256   verifyFormat("template <enum E> class A {\n"
9257                "public:\n"
9258                "  E *f();\n"
9259                "};",
9260                NeverBreak);
9261   NeverBreak.PenaltyBreakTemplateDeclaration = 100;
9262   verifyFormat("template <typename T> void\nfoo(aaaaaaaaaaaaaaaaaaaaaaaaaa "
9263                "bbbbbbbbbbbbbbbbbbbb) {}",
9264                NeverBreak);
9265 }
9266 
9267 TEST_F(FormatTest, WrapsTemplateDeclarationsWithComments) {
9268   FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp);
9269   Style.ColumnLimit = 60;
9270   EXPECT_EQ("// Baseline - no comments.\n"
9271             "template <\n"
9272             "    typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n"
9273             "void f() {}",
9274             format("// Baseline - no comments.\n"
9275                    "template <\n"
9276                    "    typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n"
9277                    "void f() {}",
9278                    Style));
9279 
9280   EXPECT_EQ("template <\n"
9281             "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value>  // trailing\n"
9282             "void f() {}",
9283             format("template <\n"
9284                    "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
9285                    "void f() {}",
9286                    Style));
9287 
9288   EXPECT_EQ(
9289       "template <\n"
9290       "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n"
9291       "void f() {}",
9292       format("template <typename aaaaaaaaaa<bbbbbbbbbbbb>::value>  /* line */\n"
9293              "void f() {}",
9294              Style));
9295 
9296   EXPECT_EQ(
9297       "template <\n"
9298       "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value>  // trailing\n"
9299       "                                               // multiline\n"
9300       "void f() {}",
9301       format("template <\n"
9302              "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
9303              "                                              // multiline\n"
9304              "void f() {}",
9305              Style));
9306 
9307   EXPECT_EQ(
9308       "template <typename aaaaaaaaaa<\n"
9309       "    bbbbbbbbbbbb>::value>  // trailing loooong\n"
9310       "void f() {}",
9311       format(
9312           "template <\n"
9313           "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing loooong\n"
9314           "void f() {}",
9315           Style));
9316 }
9317 
9318 TEST_F(FormatTest, WrapsTemplateParameters) {
9319   FormatStyle Style = getLLVMStyle();
9320   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
9321   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
9322   verifyFormat(
9323       "template <typename... a> struct q {};\n"
9324       "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
9325       "    aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
9326       "    y;",
9327       Style);
9328   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
9329   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
9330   verifyFormat(
9331       "template <typename... a> struct r {};\n"
9332       "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
9333       "    aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
9334       "    y;",
9335       Style);
9336   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
9337   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
9338   verifyFormat("template <typename... a> struct s {};\n"
9339                "extern s<\n"
9340                "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
9341                "aaaaaaaaaaaaaaaaaaaaaa,\n"
9342                "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
9343                "aaaaaaaaaaaaaaaaaaaaaa>\n"
9344                "    y;",
9345                Style);
9346   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
9347   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
9348   verifyFormat("template <typename... a> struct t {};\n"
9349                "extern t<\n"
9350                "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
9351                "aaaaaaaaaaaaaaaaaaaaaa,\n"
9352                "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
9353                "aaaaaaaaaaaaaaaaaaaaaa>\n"
9354                "    y;",
9355                Style);
9356 }
9357 
9358 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
9359   verifyFormat(
9360       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
9361       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
9362   verifyFormat(
9363       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
9364       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9365       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
9366 
9367   // FIXME: Should we have the extra indent after the second break?
9368   verifyFormat(
9369       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
9370       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
9371       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
9372 
9373   verifyFormat(
9374       "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
9375       "                    cccccccccccccccccccccccccccccccccccccccccccccc());");
9376 
9377   // Breaking at nested name specifiers is generally not desirable.
9378   verifyFormat(
9379       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9380       "    aaaaaaaaaaaaaaaaaaaaaaa);");
9381 
9382   verifyFormat("aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n"
9383                "                   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
9384                "                       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9385                "                   aaaaaaaaaaaaaaaaaaaaa);",
9386                getLLVMStyleWithColumns(74));
9387 
9388   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
9389                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9390                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
9391 }
9392 
9393 TEST_F(FormatTest, UnderstandsTemplateParameters) {
9394   verifyFormat("A<int> a;");
9395   verifyFormat("A<A<A<int>>> a;");
9396   verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
9397   verifyFormat("bool x = a < 1 || 2 > a;");
9398   verifyFormat("bool x = 5 < f<int>();");
9399   verifyFormat("bool x = f<int>() > 5;");
9400   verifyFormat("bool x = 5 < a<int>::x;");
9401   verifyFormat("bool x = a < 4 ? a > 2 : false;");
9402   verifyFormat("bool x = f() ? a < 2 : a > 2;");
9403 
9404   verifyGoogleFormat("A<A<int>> a;");
9405   verifyGoogleFormat("A<A<A<int>>> a;");
9406   verifyGoogleFormat("A<A<A<A<int>>>> a;");
9407   verifyGoogleFormat("A<A<int> > a;");
9408   verifyGoogleFormat("A<A<A<int> > > a;");
9409   verifyGoogleFormat("A<A<A<A<int> > > > a;");
9410   verifyGoogleFormat("A<::A<int>> a;");
9411   verifyGoogleFormat("A<::A> a;");
9412   verifyGoogleFormat("A< ::A> a;");
9413   verifyGoogleFormat("A< ::A<int> > a;");
9414   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
9415   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
9416   EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle()));
9417   EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle()));
9418   EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };",
9419             format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle()));
9420 
9421   verifyFormat("A<A<int>> a;", getChromiumStyle(FormatStyle::LK_Cpp));
9422 
9423   // template closer followed by a token that starts with > or =
9424   verifyFormat("bool b = a<1> > 1;");
9425   verifyFormat("bool b = a<1> >= 1;");
9426   verifyFormat("int i = a<1> >> 1;");
9427   FormatStyle Style = getLLVMStyle();
9428   Style.SpaceBeforeAssignmentOperators = false;
9429   verifyFormat("bool b= a<1> == 1;", Style);
9430   verifyFormat("a<int> = 1;", Style);
9431   verifyFormat("a<int> >>= 1;", Style);
9432 
9433   verifyFormat("test < a | b >> c;");
9434   verifyFormat("test<test<a | b>> c;");
9435   verifyFormat("test >> a >> b;");
9436   verifyFormat("test << a >> b;");
9437 
9438   verifyFormat("f<int>();");
9439   verifyFormat("template <typename T> void f() {}");
9440   verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
9441   verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : "
9442                "sizeof(char)>::type>;");
9443   verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};");
9444   verifyFormat("f(a.operator()<A>());");
9445   verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9446                "      .template operator()<A>());",
9447                getLLVMStyleWithColumns(35));
9448 
9449   // Not template parameters.
9450   verifyFormat("return a < b && c > d;");
9451   verifyFormat("void f() {\n"
9452                "  while (a < b && c > d) {\n"
9453                "  }\n"
9454                "}");
9455   verifyFormat("template <typename... Types>\n"
9456                "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
9457 
9458   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9459                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
9460                getLLVMStyleWithColumns(60));
9461   verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
9462   verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
9463   verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <");
9464   verifyFormat("some_templated_type<decltype([](int i) { return i; })>");
9465 }
9466 
9467 TEST_F(FormatTest, UnderstandsShiftOperators) {
9468   verifyFormat("if (i < x >> 1)");
9469   verifyFormat("while (i < x >> 1)");
9470   verifyFormat("for (unsigned i = 0; i < i; ++i, v = v >> 1)");
9471   verifyFormat("for (unsigned i = 0; i < x >> 1; ++i, v = v >> 1)");
9472   verifyFormat(
9473       "for (std::vector<int>::iterator i = 0; i < x >> 1; ++i, v = v >> 1)");
9474   verifyFormat("Foo.call<Bar<Function>>()");
9475   verifyFormat("if (Foo.call<Bar<Function>>() == 0)");
9476   verifyFormat("for (std::vector<std::pair<int>>::iterator i = 0; i < x >> 1; "
9477                "++i, v = v >> 1)");
9478   verifyFormat("if (w<u<v<x>>, 1>::t)");
9479 }
9480 
9481 TEST_F(FormatTest, BitshiftOperatorWidth) {
9482   EXPECT_EQ("int a = 1 << 2; /* foo\n"
9483             "                   bar */",
9484             format("int    a=1<<2;  /* foo\n"
9485                    "                   bar */"));
9486 
9487   EXPECT_EQ("int b = 256 >> 1; /* foo\n"
9488             "                     bar */",
9489             format("int  b  =256>>1 ;  /* foo\n"
9490                    "                      bar */"));
9491 }
9492 
9493 TEST_F(FormatTest, UnderstandsBinaryOperators) {
9494   verifyFormat("COMPARE(a, ==, b);");
9495   verifyFormat("auto s = sizeof...(Ts) - 1;");
9496 }
9497 
9498 TEST_F(FormatTest, UnderstandsPointersToMembers) {
9499   verifyFormat("int A::*x;");
9500   verifyFormat("int (S::*func)(void *);");
9501   verifyFormat("void f() { int (S::*func)(void *); }");
9502   verifyFormat("typedef bool *(Class::*Member)() const;");
9503   verifyFormat("void f() {\n"
9504                "  (a->*f)();\n"
9505                "  a->*x;\n"
9506                "  (a.*f)();\n"
9507                "  ((*a).*f)();\n"
9508                "  a.*x;\n"
9509                "}");
9510   verifyFormat("void f() {\n"
9511                "  (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
9512                "      aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
9513                "}");
9514   verifyFormat(
9515       "(aaaaaaaaaa->*bbbbbbb)(\n"
9516       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
9517   FormatStyle Style = getLLVMStyle();
9518   Style.PointerAlignment = FormatStyle::PAS_Left;
9519   verifyFormat("typedef bool* (Class::*Member)() const;", Style);
9520 }
9521 
9522 TEST_F(FormatTest, UnderstandsUnaryOperators) {
9523   verifyFormat("int a = -2;");
9524   verifyFormat("f(-1, -2, -3);");
9525   verifyFormat("a[-1] = 5;");
9526   verifyFormat("int a = 5 + -2;");
9527   verifyFormat("if (i == -1) {\n}");
9528   verifyFormat("if (i != -1) {\n}");
9529   verifyFormat("if (i > -1) {\n}");
9530   verifyFormat("if (i < -1) {\n}");
9531   verifyFormat("++(a->f());");
9532   verifyFormat("--(a->f());");
9533   verifyFormat("(a->f())++;");
9534   verifyFormat("a[42]++;");
9535   verifyFormat("if (!(a->f())) {\n}");
9536   verifyFormat("if (!+i) {\n}");
9537   verifyFormat("~&a;");
9538 
9539   verifyFormat("a-- > b;");
9540   verifyFormat("b ? -a : c;");
9541   verifyFormat("n * sizeof char16;");
9542   verifyFormat("n * alignof char16;", getGoogleStyle());
9543   verifyFormat("sizeof(char);");
9544   verifyFormat("alignof(char);", getGoogleStyle());
9545 
9546   verifyFormat("return -1;");
9547   verifyFormat("throw -1;");
9548   verifyFormat("switch (a) {\n"
9549                "case -1:\n"
9550                "  break;\n"
9551                "}");
9552   verifyFormat("#define X -1");
9553   verifyFormat("#define X -kConstant");
9554 
9555   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
9556   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
9557 
9558   verifyFormat("int a = /* confusing comment */ -1;");
9559   // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
9560   verifyFormat("int a = i /* confusing comment */++;");
9561 
9562   verifyFormat("co_yield -1;");
9563   verifyFormat("co_return -1;");
9564 
9565   // Check that * is not treated as a binary operator when we set
9566   // PointerAlignment as PAS_Left after a keyword and not a declaration.
9567   FormatStyle PASLeftStyle = getLLVMStyle();
9568   PASLeftStyle.PointerAlignment = FormatStyle::PAS_Left;
9569   verifyFormat("co_return *a;", PASLeftStyle);
9570   verifyFormat("co_await *a;", PASLeftStyle);
9571   verifyFormat("co_yield *a", PASLeftStyle);
9572   verifyFormat("return *a;", PASLeftStyle);
9573 }
9574 
9575 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) {
9576   verifyFormat("if (!aaaaaaaaaa( // break\n"
9577                "        aaaaa)) {\n"
9578                "}");
9579   verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
9580                "    aaaaa));");
9581   verifyFormat("*aaa = aaaaaaa( // break\n"
9582                "    bbbbbb);");
9583 }
9584 
9585 TEST_F(FormatTest, UnderstandsOverloadedOperators) {
9586   verifyFormat("bool operator<();");
9587   verifyFormat("bool operator>();");
9588   verifyFormat("bool operator=();");
9589   verifyFormat("bool operator==();");
9590   verifyFormat("bool operator!=();");
9591   verifyFormat("int operator+();");
9592   verifyFormat("int operator++();");
9593   verifyFormat("int operator++(int) volatile noexcept;");
9594   verifyFormat("bool operator,();");
9595   verifyFormat("bool operator();");
9596   verifyFormat("bool operator()();");
9597   verifyFormat("bool operator[]();");
9598   verifyFormat("operator bool();");
9599   verifyFormat("operator int();");
9600   verifyFormat("operator void *();");
9601   verifyFormat("operator SomeType<int>();");
9602   verifyFormat("operator SomeType<int, int>();");
9603   verifyFormat("operator SomeType<SomeType<int>>();");
9604   verifyFormat("operator< <>();");
9605   verifyFormat("operator<< <>();");
9606   verifyFormat("< <>");
9607 
9608   verifyFormat("void *operator new(std::size_t size);");
9609   verifyFormat("void *operator new[](std::size_t size);");
9610   verifyFormat("void operator delete(void *ptr);");
9611   verifyFormat("void operator delete[](void *ptr);");
9612   verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
9613                "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
9614   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n"
9615                "    aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;");
9616 
9617   verifyFormat(
9618       "ostream &operator<<(ostream &OutputStream,\n"
9619       "                    SomeReallyLongType WithSomeReallyLongValue);");
9620   verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
9621                "               const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
9622                "  return left.group < right.group;\n"
9623                "}");
9624   verifyFormat("SomeType &operator=(const SomeType &S);");
9625   verifyFormat("f.template operator()<int>();");
9626 
9627   verifyGoogleFormat("operator void*();");
9628   verifyGoogleFormat("operator SomeType<SomeType<int>>();");
9629   verifyGoogleFormat("operator ::A();");
9630 
9631   verifyFormat("using A::operator+;");
9632   verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n"
9633                "int i;");
9634 
9635   // Calling an operator as a member function.
9636   verifyFormat("void f() { a.operator*(); }");
9637   verifyFormat("void f() { a.operator*(b & b); }");
9638   verifyFormat("void f() { a->operator&(a * b); }");
9639   verifyFormat("void f() { NS::a.operator+(*b * *b); }");
9640   // TODO: Calling an operator as a non-member function is hard to distinguish.
9641   // https://llvm.org/PR50629
9642   // verifyFormat("void f() { operator*(a & a); }");
9643   // verifyFormat("void f() { operator&(a, b * b); }");
9644 
9645   verifyFormat("::operator delete(foo);");
9646   verifyFormat("::operator new(n * sizeof(foo));");
9647   verifyFormat("foo() { ::operator delete(foo); }");
9648   verifyFormat("foo() { ::operator new(n * sizeof(foo)); }");
9649 }
9650 
9651 TEST_F(FormatTest, UnderstandsFunctionRefQualification) {
9652   verifyFormat("void A::b() && {}");
9653   verifyFormat("void A::b() &&noexcept {}");
9654   verifyFormat("Deleted &operator=(const Deleted &) & = default;");
9655   verifyFormat("Deleted &operator=(const Deleted &) && = delete;");
9656   verifyFormat("Deleted &operator=(const Deleted &) &noexcept = default;");
9657   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;");
9658   verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;");
9659   verifyFormat("Deleted &operator=(const Deleted &) &;");
9660   verifyFormat("Deleted &operator=(const Deleted &) &&;");
9661   verifyFormat("SomeType MemberFunction(const Deleted &) &;");
9662   verifyFormat("SomeType MemberFunction(const Deleted &) &&;");
9663   verifyFormat("SomeType MemberFunction(const Deleted &) && {}");
9664   verifyFormat("SomeType MemberFunction(const Deleted &) && final {}");
9665   verifyFormat("SomeType MemberFunction(const Deleted &) && override {}");
9666   verifyFormat("SomeType MemberFunction(const Deleted &) &&noexcept {}");
9667   verifyFormat("void Fn(T const &) const &;");
9668   verifyFormat("void Fn(T const volatile &&) const volatile &&;");
9669   verifyFormat("void Fn(T const volatile &&) const volatile &&noexcept;");
9670   verifyFormat("template <typename T>\n"
9671                "void F(T) && = delete;",
9672                getGoogleStyle());
9673 
9674   FormatStyle AlignLeft = getLLVMStyle();
9675   AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
9676   verifyFormat("void A::b() && {}", AlignLeft);
9677   verifyFormat("void A::b() && noexcept {}", AlignLeft);
9678   verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft);
9679   verifyFormat("Deleted& operator=(const Deleted&) & noexcept = default;",
9680                AlignLeft);
9681   verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;",
9682                AlignLeft);
9683   verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft);
9684   verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft);
9685   verifyFormat("auto Function(T t) & -> void {}", AlignLeft);
9686   verifyFormat("auto Function(T... t) & -> void {}", AlignLeft);
9687   verifyFormat("auto Function(T) & -> void {}", AlignLeft);
9688   verifyFormat("auto Function(T) & -> void;", AlignLeft);
9689   verifyFormat("void Fn(T const&) const&;", AlignLeft);
9690   verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft);
9691   verifyFormat("void Fn(T const volatile&&) const volatile&& noexcept;",
9692                AlignLeft);
9693 
9694   FormatStyle AlignMiddle = getLLVMStyle();
9695   AlignMiddle.PointerAlignment = FormatStyle::PAS_Middle;
9696   verifyFormat("void A::b() && {}", AlignMiddle);
9697   verifyFormat("void A::b() && noexcept {}", AlignMiddle);
9698   verifyFormat("Deleted & operator=(const Deleted &) & = default;",
9699                AlignMiddle);
9700   verifyFormat("Deleted & operator=(const Deleted &) & noexcept = default;",
9701                AlignMiddle);
9702   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;",
9703                AlignMiddle);
9704   verifyFormat("Deleted & operator=(const Deleted &) &;", AlignMiddle);
9705   verifyFormat("SomeType MemberFunction(const Deleted &) &;", AlignMiddle);
9706   verifyFormat("auto Function(T t) & -> void {}", AlignMiddle);
9707   verifyFormat("auto Function(T... t) & -> void {}", AlignMiddle);
9708   verifyFormat("auto Function(T) & -> void {}", AlignMiddle);
9709   verifyFormat("auto Function(T) & -> void;", AlignMiddle);
9710   verifyFormat("void Fn(T const &) const &;", AlignMiddle);
9711   verifyFormat("void Fn(T const volatile &&) const volatile &&;", AlignMiddle);
9712   verifyFormat("void Fn(T const volatile &&) const volatile && noexcept;",
9713                AlignMiddle);
9714 
9715   FormatStyle Spaces = getLLVMStyle();
9716   Spaces.SpacesInCStyleCastParentheses = true;
9717   verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces);
9718   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces);
9719   verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces);
9720   verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces);
9721 
9722   Spaces.SpacesInCStyleCastParentheses = false;
9723   Spaces.SpacesInParentheses = true;
9724   verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces);
9725   verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;",
9726                Spaces);
9727   verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces);
9728   verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces);
9729 
9730   FormatStyle BreakTemplate = getLLVMStyle();
9731   BreakTemplate.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
9732 
9733   verifyFormat("struct f {\n"
9734                "  template <class T>\n"
9735                "  int &foo(const std::string &str) &noexcept {}\n"
9736                "};",
9737                BreakTemplate);
9738 
9739   verifyFormat("struct f {\n"
9740                "  template <class T>\n"
9741                "  int &foo(const std::string &str) &&noexcept {}\n"
9742                "};",
9743                BreakTemplate);
9744 
9745   verifyFormat("struct f {\n"
9746                "  template <class T>\n"
9747                "  int &foo(const std::string &str) const &noexcept {}\n"
9748                "};",
9749                BreakTemplate);
9750 
9751   verifyFormat("struct f {\n"
9752                "  template <class T>\n"
9753                "  int &foo(const std::string &str) const &noexcept {}\n"
9754                "};",
9755                BreakTemplate);
9756 
9757   verifyFormat("struct f {\n"
9758                "  template <class T>\n"
9759                "  auto foo(const std::string &str) &&noexcept -> int & {}\n"
9760                "};",
9761                BreakTemplate);
9762 
9763   FormatStyle AlignLeftBreakTemplate = getLLVMStyle();
9764   AlignLeftBreakTemplate.AlwaysBreakTemplateDeclarations =
9765       FormatStyle::BTDS_Yes;
9766   AlignLeftBreakTemplate.PointerAlignment = FormatStyle::PAS_Left;
9767 
9768   verifyFormat("struct f {\n"
9769                "  template <class T>\n"
9770                "  int& foo(const std::string& str) & noexcept {}\n"
9771                "};",
9772                AlignLeftBreakTemplate);
9773 
9774   verifyFormat("struct f {\n"
9775                "  template <class T>\n"
9776                "  int& foo(const std::string& str) && noexcept {}\n"
9777                "};",
9778                AlignLeftBreakTemplate);
9779 
9780   verifyFormat("struct f {\n"
9781                "  template <class T>\n"
9782                "  int& foo(const std::string& str) const& noexcept {}\n"
9783                "};",
9784                AlignLeftBreakTemplate);
9785 
9786   verifyFormat("struct f {\n"
9787                "  template <class T>\n"
9788                "  int& foo(const std::string& str) const&& noexcept {}\n"
9789                "};",
9790                AlignLeftBreakTemplate);
9791 
9792   verifyFormat("struct f {\n"
9793                "  template <class T>\n"
9794                "  auto foo(const std::string& str) && noexcept -> int& {}\n"
9795                "};",
9796                AlignLeftBreakTemplate);
9797 
9798   // The `&` in `Type&` should not be confused with a trailing `&` of
9799   // DEPRECATED(reason) member function.
9800   verifyFormat("struct f {\n"
9801                "  template <class T>\n"
9802                "  DEPRECATED(reason)\n"
9803                "  Type &foo(arguments) {}\n"
9804                "};",
9805                BreakTemplate);
9806 
9807   verifyFormat("struct f {\n"
9808                "  template <class T>\n"
9809                "  DEPRECATED(reason)\n"
9810                "  Type& foo(arguments) {}\n"
9811                "};",
9812                AlignLeftBreakTemplate);
9813 
9814   verifyFormat("void (*foopt)(int) = &func;");
9815 
9816   FormatStyle DerivePointerAlignment = getLLVMStyle();
9817   DerivePointerAlignment.DerivePointerAlignment = true;
9818   // There's always a space between the function and its trailing qualifiers.
9819   // This isn't evidence for PAS_Right (or for PAS_Left).
9820   std::string Prefix = "void a() &;\n"
9821                        "void b() &;\n";
9822   verifyFormat(Prefix + "int* x;", DerivePointerAlignment);
9823   verifyFormat(Prefix + "int *x;", DerivePointerAlignment);
9824   // Same if the function is an overloaded operator, and with &&.
9825   Prefix = "void operator()() &&;\n"
9826            "void operator()() &&;\n";
9827   verifyFormat(Prefix + "int* x;", DerivePointerAlignment);
9828   verifyFormat(Prefix + "int *x;", DerivePointerAlignment);
9829   // However a space between cv-qualifiers and ref-qualifiers *is* evidence.
9830   Prefix = "void a() const &;\n"
9831            "void b() const &;\n";
9832   EXPECT_EQ(Prefix + "int *x;",
9833             format(Prefix + "int* x;", DerivePointerAlignment));
9834 }
9835 
9836 TEST_F(FormatTest, UnderstandsNewAndDelete) {
9837   verifyFormat("void f() {\n"
9838                "  A *a = new A;\n"
9839                "  A *a = new (placement) A;\n"
9840                "  delete a;\n"
9841                "  delete (A *)a;\n"
9842                "}");
9843   verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
9844                "    typename aaaaaaaaaaaaaaaaaaaaaaaa();");
9845   verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
9846                "    new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
9847                "        typename aaaaaaaaaaaaaaaaaaaaaaaa();");
9848   verifyFormat("delete[] h->p;");
9849   verifyFormat("delete[] (void *)p;");
9850 
9851   verifyFormat("void operator delete(void *foo) ATTRIB;");
9852   verifyFormat("void operator new(void *foo) ATTRIB;");
9853   verifyFormat("void operator delete[](void *foo) ATTRIB;");
9854   verifyFormat("void operator delete(void *ptr) noexcept;");
9855 }
9856 
9857 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
9858   verifyFormat("int *f(int *a) {}");
9859   verifyFormat("int main(int argc, char **argv) {}");
9860   verifyFormat("Test::Test(int b) : a(b * b) {}");
9861   verifyIndependentOfContext("f(a, *a);");
9862   verifyFormat("void g() { f(*a); }");
9863   verifyIndependentOfContext("int a = b * 10;");
9864   verifyIndependentOfContext("int a = 10 * b;");
9865   verifyIndependentOfContext("int a = b * c;");
9866   verifyIndependentOfContext("int a += b * c;");
9867   verifyIndependentOfContext("int a -= b * c;");
9868   verifyIndependentOfContext("int a *= b * c;");
9869   verifyIndependentOfContext("int a /= b * c;");
9870   verifyIndependentOfContext("int a = *b;");
9871   verifyIndependentOfContext("int a = *b * c;");
9872   verifyIndependentOfContext("int a = b * *c;");
9873   verifyIndependentOfContext("int a = b * (10);");
9874   verifyIndependentOfContext("S << b * (10);");
9875   verifyIndependentOfContext("return 10 * b;");
9876   verifyIndependentOfContext("return *b * *c;");
9877   verifyIndependentOfContext("return a & ~b;");
9878   verifyIndependentOfContext("f(b ? *c : *d);");
9879   verifyIndependentOfContext("int a = b ? *c : *d;");
9880   verifyIndependentOfContext("*b = a;");
9881   verifyIndependentOfContext("a * ~b;");
9882   verifyIndependentOfContext("a * !b;");
9883   verifyIndependentOfContext("a * +b;");
9884   verifyIndependentOfContext("a * -b;");
9885   verifyIndependentOfContext("a * ++b;");
9886   verifyIndependentOfContext("a * --b;");
9887   verifyIndependentOfContext("a[4] * b;");
9888   verifyIndependentOfContext("a[a * a] = 1;");
9889   verifyIndependentOfContext("f() * b;");
9890   verifyIndependentOfContext("a * [self dostuff];");
9891   verifyIndependentOfContext("int x = a * (a + b);");
9892   verifyIndependentOfContext("(a *)(a + b);");
9893   verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
9894   verifyIndependentOfContext("int *pa = (int *)&a;");
9895   verifyIndependentOfContext("return sizeof(int **);");
9896   verifyIndependentOfContext("return sizeof(int ******);");
9897   verifyIndependentOfContext("return (int **&)a;");
9898   verifyIndependentOfContext("f((*PointerToArray)[10]);");
9899   verifyFormat("void f(Type (*parameter)[10]) {}");
9900   verifyFormat("void f(Type (&parameter)[10]) {}");
9901   verifyGoogleFormat("return sizeof(int**);");
9902   verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
9903   verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
9904   verifyFormat("auto a = [](int **&, int ***) {};");
9905   verifyFormat("auto PointerBinding = [](const char *S) {};");
9906   verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
9907   verifyFormat("[](const decltype(*a) &value) {}");
9908   verifyFormat("[](const typeof(*a) &value) {}");
9909   verifyFormat("[](const _Atomic(a *) &value) {}");
9910   verifyFormat("[](const __underlying_type(a) &value) {}");
9911   verifyFormat("decltype(a * b) F();");
9912   verifyFormat("typeof(a * b) F();");
9913   verifyFormat("#define MACRO() [](A *a) { return 1; }");
9914   verifyFormat("Constructor() : member([](A *a, B *b) {}) {}");
9915   verifyIndependentOfContext("typedef void (*f)(int *a);");
9916   verifyIndependentOfContext("int i{a * b};");
9917   verifyIndependentOfContext("aaa && aaa->f();");
9918   verifyIndependentOfContext("int x = ~*p;");
9919   verifyFormat("Constructor() : a(a), area(width * height) {}");
9920   verifyFormat("Constructor() : a(a), area(a, width * height) {}");
9921   verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}");
9922   verifyFormat("void f() { f(a, c * d); }");
9923   verifyFormat("void f() { f(new a(), c * d); }");
9924   verifyFormat("void f(const MyOverride &override);");
9925   verifyFormat("void f(const MyFinal &final);");
9926   verifyIndependentOfContext("bool a = f() && override.f();");
9927   verifyIndependentOfContext("bool a = f() && final.f();");
9928 
9929   verifyIndependentOfContext("InvalidRegions[*R] = 0;");
9930 
9931   verifyIndependentOfContext("A<int *> a;");
9932   verifyIndependentOfContext("A<int **> a;");
9933   verifyIndependentOfContext("A<int *, int *> a;");
9934   verifyIndependentOfContext("A<int *[]> a;");
9935   verifyIndependentOfContext(
9936       "const char *const p = reinterpret_cast<const char *const>(q);");
9937   verifyIndependentOfContext("A<int **, int **> a;");
9938   verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
9939   verifyFormat("for (char **a = b; *a; ++a) {\n}");
9940   verifyFormat("for (; a && b;) {\n}");
9941   verifyFormat("bool foo = true && [] { return false; }();");
9942 
9943   verifyFormat(
9944       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9945       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9946 
9947   verifyGoogleFormat("int const* a = &b;");
9948   verifyGoogleFormat("**outparam = 1;");
9949   verifyGoogleFormat("*outparam = a * b;");
9950   verifyGoogleFormat("int main(int argc, char** argv) {}");
9951   verifyGoogleFormat("A<int*> a;");
9952   verifyGoogleFormat("A<int**> a;");
9953   verifyGoogleFormat("A<int*, int*> a;");
9954   verifyGoogleFormat("A<int**, int**> a;");
9955   verifyGoogleFormat("f(b ? *c : *d);");
9956   verifyGoogleFormat("int a = b ? *c : *d;");
9957   verifyGoogleFormat("Type* t = **x;");
9958   verifyGoogleFormat("Type* t = *++*x;");
9959   verifyGoogleFormat("*++*x;");
9960   verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
9961   verifyGoogleFormat("Type* t = x++ * y;");
9962   verifyGoogleFormat(
9963       "const char* const p = reinterpret_cast<const char* const>(q);");
9964   verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
9965   verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
9966   verifyGoogleFormat("template <typename T>\n"
9967                      "void f(int i = 0, SomeType** temps = NULL);");
9968 
9969   FormatStyle Left = getLLVMStyle();
9970   Left.PointerAlignment = FormatStyle::PAS_Left;
9971   verifyFormat("x = *a(x) = *a(y);", Left);
9972   verifyFormat("for (;; *a = b) {\n}", Left);
9973   verifyFormat("return *this += 1;", Left);
9974   verifyFormat("throw *x;", Left);
9975   verifyFormat("delete *x;", Left);
9976   verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left);
9977   verifyFormat("[](const decltype(*a)* ptr) {}", Left);
9978   verifyFormat("[](const typeof(*a)* ptr) {}", Left);
9979   verifyFormat("[](const _Atomic(a*)* ptr) {}", Left);
9980   verifyFormat("[](const __underlying_type(a)* ptr) {}", Left);
9981   verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left);
9982   verifyFormat("auto x(A&&, B&&, C&&) -> D;", Left);
9983   verifyFormat("auto x = [](A&&, B&&, C&&) -> D {};", Left);
9984   verifyFormat("template <class T> X(T&&, T&&, T&&) -> X<T>;", Left);
9985 
9986   verifyIndependentOfContext("a = *(x + y);");
9987   verifyIndependentOfContext("a = &(x + y);");
9988   verifyIndependentOfContext("*(x + y).call();");
9989   verifyIndependentOfContext("&(x + y)->call();");
9990   verifyFormat("void f() { &(*I).first; }");
9991 
9992   verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
9993   verifyFormat("f(* /* confusing comment */ foo);");
9994   verifyFormat("void (* /*deleter*/)(const Slice &key, void *value)");
9995   verifyFormat("void foo(int * // this is the first paramters\n"
9996                "         ,\n"
9997                "         int second);");
9998   verifyFormat("double term = a * // first\n"
9999                "              b;");
10000   verifyFormat(
10001       "int *MyValues = {\n"
10002       "    *A, // Operator detection might be confused by the '{'\n"
10003       "    *BB // Operator detection might be confused by previous comment\n"
10004       "};");
10005 
10006   verifyIndependentOfContext("if (int *a = &b)");
10007   verifyIndependentOfContext("if (int &a = *b)");
10008   verifyIndependentOfContext("if (a & b[i])");
10009   verifyIndependentOfContext("if constexpr (a & b[i])");
10010   verifyIndependentOfContext("if CONSTEXPR (a & b[i])");
10011   verifyIndependentOfContext("if (a * (b * c))");
10012   verifyIndependentOfContext("if constexpr (a * (b * c))");
10013   verifyIndependentOfContext("if CONSTEXPR (a * (b * c))");
10014   verifyIndependentOfContext("if (a::b::c::d & b[i])");
10015   verifyIndependentOfContext("if (*b[i])");
10016   verifyIndependentOfContext("if (int *a = (&b))");
10017   verifyIndependentOfContext("while (int *a = &b)");
10018   verifyIndependentOfContext("while (a * (b * c))");
10019   verifyIndependentOfContext("size = sizeof *a;");
10020   verifyIndependentOfContext("if (a && (b = c))");
10021   verifyFormat("void f() {\n"
10022                "  for (const int &v : Values) {\n"
10023                "  }\n"
10024                "}");
10025   verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
10026   verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
10027   verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
10028 
10029   verifyFormat("#define A (!a * b)");
10030   verifyFormat("#define MACRO     \\\n"
10031                "  int *i = a * b; \\\n"
10032                "  void f(a *b);",
10033                getLLVMStyleWithColumns(19));
10034 
10035   verifyIndependentOfContext("A = new SomeType *[Length];");
10036   verifyIndependentOfContext("A = new SomeType *[Length]();");
10037   verifyIndependentOfContext("T **t = new T *;");
10038   verifyIndependentOfContext("T **t = new T *();");
10039   verifyGoogleFormat("A = new SomeType*[Length]();");
10040   verifyGoogleFormat("A = new SomeType*[Length];");
10041   verifyGoogleFormat("T** t = new T*;");
10042   verifyGoogleFormat("T** t = new T*();");
10043 
10044   verifyFormat("STATIC_ASSERT((a & b) == 0);");
10045   verifyFormat("STATIC_ASSERT(0 == (a & b));");
10046   verifyFormat("template <bool a, bool b> "
10047                "typename t::if<x && y>::type f() {}");
10048   verifyFormat("template <int *y> f() {}");
10049   verifyFormat("vector<int *> v;");
10050   verifyFormat("vector<int *const> v;");
10051   verifyFormat("vector<int *const **const *> v;");
10052   verifyFormat("vector<int *volatile> v;");
10053   verifyFormat("vector<a *_Nonnull> v;");
10054   verifyFormat("vector<a *_Nullable> v;");
10055   verifyFormat("vector<a *_Null_unspecified> v;");
10056   verifyFormat("vector<a *__ptr32> v;");
10057   verifyFormat("vector<a *__ptr64> v;");
10058   verifyFormat("vector<a *__capability> v;");
10059   FormatStyle TypeMacros = getLLVMStyle();
10060   TypeMacros.TypenameMacros = {"LIST"};
10061   verifyFormat("vector<LIST(uint64_t)> v;", TypeMacros);
10062   verifyFormat("vector<LIST(uint64_t) *> v;", TypeMacros);
10063   verifyFormat("vector<LIST(uint64_t) **> v;", TypeMacros);
10064   verifyFormat("vector<LIST(uint64_t) *attr> v;", TypeMacros);
10065   verifyFormat("vector<A(uint64_t) * attr> v;", TypeMacros); // multiplication
10066 
10067   FormatStyle CustomQualifier = getLLVMStyle();
10068   // Add identifiers that should not be parsed as a qualifier by default.
10069   CustomQualifier.AttributeMacros.push_back("__my_qualifier");
10070   CustomQualifier.AttributeMacros.push_back("_My_qualifier");
10071   CustomQualifier.AttributeMacros.push_back("my_other_qualifier");
10072   verifyFormat("vector<a * __my_qualifier> parse_as_multiply;");
10073   verifyFormat("vector<a *__my_qualifier> v;", CustomQualifier);
10074   verifyFormat("vector<a * _My_qualifier> parse_as_multiply;");
10075   verifyFormat("vector<a *_My_qualifier> v;", CustomQualifier);
10076   verifyFormat("vector<a * my_other_qualifier> parse_as_multiply;");
10077   verifyFormat("vector<a *my_other_qualifier> v;", CustomQualifier);
10078   verifyFormat("vector<a * _NotAQualifier> v;");
10079   verifyFormat("vector<a * __not_a_qualifier> v;");
10080   verifyFormat("vector<a * b> v;");
10081   verifyFormat("foo<b && false>();");
10082   verifyFormat("foo<b & 1>();");
10083   verifyFormat("decltype(*::std::declval<const T &>()) void F();");
10084   verifyFormat("typeof(*::std::declval<const T &>()) void F();");
10085   verifyFormat("_Atomic(*::std::declval<const T &>()) void F();");
10086   verifyFormat("__underlying_type(*::std::declval<const T &>()) void F();");
10087   verifyFormat(
10088       "template <class T, class = typename std::enable_if<\n"
10089       "                       std::is_integral<T>::value &&\n"
10090       "                       (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
10091       "void F();",
10092       getLLVMStyleWithColumns(70));
10093   verifyFormat("template <class T,\n"
10094                "          class = typename std::enable_if<\n"
10095                "              std::is_integral<T>::value &&\n"
10096                "              (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n"
10097                "          class U>\n"
10098                "void F();",
10099                getLLVMStyleWithColumns(70));
10100   verifyFormat(
10101       "template <class T,\n"
10102       "          class = typename ::std::enable_if<\n"
10103       "              ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
10104       "void F();",
10105       getGoogleStyleWithColumns(68));
10106 
10107   verifyIndependentOfContext("MACRO(int *i);");
10108   verifyIndependentOfContext("MACRO(auto *a);");
10109   verifyIndependentOfContext("MACRO(const A *a);");
10110   verifyIndependentOfContext("MACRO(_Atomic(A) *a);");
10111   verifyIndependentOfContext("MACRO(decltype(A) *a);");
10112   verifyIndependentOfContext("MACRO(typeof(A) *a);");
10113   verifyIndependentOfContext("MACRO(__underlying_type(A) *a);");
10114   verifyIndependentOfContext("MACRO(A *const a);");
10115   verifyIndependentOfContext("MACRO(A *restrict a);");
10116   verifyIndependentOfContext("MACRO(A *__restrict__ a);");
10117   verifyIndependentOfContext("MACRO(A *__restrict a);");
10118   verifyIndependentOfContext("MACRO(A *volatile a);");
10119   verifyIndependentOfContext("MACRO(A *__volatile a);");
10120   verifyIndependentOfContext("MACRO(A *__volatile__ a);");
10121   verifyIndependentOfContext("MACRO(A *_Nonnull a);");
10122   verifyIndependentOfContext("MACRO(A *_Nullable a);");
10123   verifyIndependentOfContext("MACRO(A *_Null_unspecified a);");
10124   verifyIndependentOfContext("MACRO(A *__attribute__((foo)) a);");
10125   verifyIndependentOfContext("MACRO(A *__attribute((foo)) a);");
10126   verifyIndependentOfContext("MACRO(A *[[clang::attr]] a);");
10127   verifyIndependentOfContext("MACRO(A *[[clang::attr(\"foo\")]] a);");
10128   verifyIndependentOfContext("MACRO(A *__ptr32 a);");
10129   verifyIndependentOfContext("MACRO(A *__ptr64 a);");
10130   verifyIndependentOfContext("MACRO(A *__capability);");
10131   verifyIndependentOfContext("MACRO(A &__capability);");
10132   verifyFormat("MACRO(A *__my_qualifier);");               // type declaration
10133   verifyFormat("void f() { MACRO(A * __my_qualifier); }"); // multiplication
10134   // If we add __my_qualifier to AttributeMacros it should always be parsed as
10135   // a type declaration:
10136   verifyFormat("MACRO(A *__my_qualifier);", CustomQualifier);
10137   verifyFormat("void f() { MACRO(A *__my_qualifier); }", CustomQualifier);
10138   // Also check that TypenameMacros prevents parsing it as multiplication:
10139   verifyIndependentOfContext("MACRO(LIST(uint64_t) * a);"); // multiplication
10140   verifyIndependentOfContext("MACRO(LIST(uint64_t) *a);", TypeMacros); // type
10141 
10142   verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
10143   verifyFormat("void f() { f(float{1}, a * a); }");
10144   verifyFormat("void f() { f(float(1), a * a); }");
10145 
10146   verifyFormat("f((void (*)(int))g);");
10147   verifyFormat("f((void (&)(int))g);");
10148   verifyFormat("f((void (^)(int))g);");
10149 
10150   // FIXME: Is there a way to make this work?
10151   // verifyIndependentOfContext("MACRO(A *a);");
10152   verifyFormat("MACRO(A &B);");
10153   verifyFormat("MACRO(A *B);");
10154   verifyFormat("void f() { MACRO(A * B); }");
10155   verifyFormat("void f() { MACRO(A & B); }");
10156 
10157   // This lambda was mis-formatted after D88956 (treating it as a binop):
10158   verifyFormat("auto x = [](const decltype(x) &ptr) {};");
10159   verifyFormat("auto x = [](const decltype(x) *ptr) {};");
10160   verifyFormat("#define lambda [](const decltype(x) &ptr) {}");
10161   verifyFormat("#define lambda [](const decltype(x) *ptr) {}");
10162 
10163   verifyFormat("DatumHandle const *operator->() const { return input_; }");
10164   verifyFormat("return options != nullptr && operator==(*options);");
10165 
10166   EXPECT_EQ("#define OP(x)                                    \\\n"
10167             "  ostream &operator<<(ostream &s, const A &a) {  \\\n"
10168             "    return s << a.DebugString();                 \\\n"
10169             "  }",
10170             format("#define OP(x) \\\n"
10171                    "  ostream &operator<<(ostream &s, const A &a) { \\\n"
10172                    "    return s << a.DebugString(); \\\n"
10173                    "  }",
10174                    getLLVMStyleWithColumns(50)));
10175 
10176   // FIXME: We cannot handle this case yet; we might be able to figure out that
10177   // foo<x> d > v; doesn't make sense.
10178   verifyFormat("foo<a<b && c> d> v;");
10179 
10180   FormatStyle PointerMiddle = getLLVMStyle();
10181   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
10182   verifyFormat("delete *x;", PointerMiddle);
10183   verifyFormat("int * x;", PointerMiddle);
10184   verifyFormat("int *[] x;", PointerMiddle);
10185   verifyFormat("template <int * y> f() {}", PointerMiddle);
10186   verifyFormat("int * f(int * a) {}", PointerMiddle);
10187   verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle);
10188   verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle);
10189   verifyFormat("A<int *> a;", PointerMiddle);
10190   verifyFormat("A<int **> a;", PointerMiddle);
10191   verifyFormat("A<int *, int *> a;", PointerMiddle);
10192   verifyFormat("A<int *[]> a;", PointerMiddle);
10193   verifyFormat("A = new SomeType *[Length]();", PointerMiddle);
10194   verifyFormat("A = new SomeType *[Length];", PointerMiddle);
10195   verifyFormat("T ** t = new T *;", PointerMiddle);
10196 
10197   // Member function reference qualifiers aren't binary operators.
10198   verifyFormat("string // break\n"
10199                "operator()() & {}");
10200   verifyFormat("string // break\n"
10201                "operator()() && {}");
10202   verifyGoogleFormat("template <typename T>\n"
10203                      "auto x() & -> int {}");
10204 
10205   // Should be binary operators when used as an argument expression (overloaded
10206   // operator invoked as a member function).
10207   verifyFormat("void f() { a.operator()(a * a); }");
10208   verifyFormat("void f() { a->operator()(a & a); }");
10209   verifyFormat("void f() { a.operator()(*a & *a); }");
10210   verifyFormat("void f() { a->operator()(*a * *a); }");
10211 
10212   verifyFormat("int operator()(T (&&)[N]) { return 1; }");
10213   verifyFormat("int operator()(T (&)[N]) { return 0; }");
10214 }
10215 
10216 TEST_F(FormatTest, UnderstandsAttributes) {
10217   verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
10218   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
10219                "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
10220   verifyFormat("__attribute__((nodebug)) ::qualified_type f();");
10221   FormatStyle AfterType = getLLVMStyle();
10222   AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
10223   verifyFormat("__attribute__((nodebug)) void\n"
10224                "foo() {}\n",
10225                AfterType);
10226   verifyFormat("__unused void\n"
10227                "foo() {}",
10228                AfterType);
10229 
10230   FormatStyle CustomAttrs = getLLVMStyle();
10231   CustomAttrs.AttributeMacros.push_back("__unused");
10232   CustomAttrs.AttributeMacros.push_back("__attr1");
10233   CustomAttrs.AttributeMacros.push_back("__attr2");
10234   CustomAttrs.AttributeMacros.push_back("no_underscore_attr");
10235   verifyFormat("vector<SomeType *__attribute((foo))> v;");
10236   verifyFormat("vector<SomeType *__attribute__((foo))> v;");
10237   verifyFormat("vector<SomeType * __not_attribute__((foo))> v;");
10238   // Check that it is parsed as a multiplication without AttributeMacros and
10239   // as a pointer qualifier when we add __attr1/__attr2 to AttributeMacros.
10240   verifyFormat("vector<SomeType * __attr1> v;");
10241   verifyFormat("vector<SomeType __attr1 *> v;");
10242   verifyFormat("vector<SomeType __attr1 *const> v;");
10243   verifyFormat("vector<SomeType __attr1 * __attr2> v;");
10244   verifyFormat("vector<SomeType *__attr1> v;", CustomAttrs);
10245   verifyFormat("vector<SomeType *__attr2> v;", CustomAttrs);
10246   verifyFormat("vector<SomeType *no_underscore_attr> v;", CustomAttrs);
10247   verifyFormat("vector<SomeType __attr1 *> v;", CustomAttrs);
10248   verifyFormat("vector<SomeType __attr1 *const> v;", CustomAttrs);
10249   verifyFormat("vector<SomeType __attr1 *__attr2> v;", CustomAttrs);
10250   verifyFormat("vector<SomeType __attr1 *no_underscore_attr> v;", CustomAttrs);
10251 
10252   // Check that these are not parsed as function declarations:
10253   CustomAttrs.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
10254   CustomAttrs.BreakBeforeBraces = FormatStyle::BS_Allman;
10255   verifyFormat("SomeType s(InitValue);", CustomAttrs);
10256   verifyFormat("SomeType s{InitValue};", CustomAttrs);
10257   verifyFormat("SomeType *__unused s(InitValue);", CustomAttrs);
10258   verifyFormat("SomeType *__unused s{InitValue};", CustomAttrs);
10259   verifyFormat("SomeType s __unused(InitValue);", CustomAttrs);
10260   verifyFormat("SomeType s __unused{InitValue};", CustomAttrs);
10261   verifyFormat("SomeType *__capability s(InitValue);", CustomAttrs);
10262   verifyFormat("SomeType *__capability s{InitValue};", CustomAttrs);
10263 }
10264 
10265 TEST_F(FormatTest, UnderstandsPointerQualifiersInCast) {
10266   // Check that qualifiers on pointers don't break parsing of casts.
10267   verifyFormat("x = (foo *const)*v;");
10268   verifyFormat("x = (foo *volatile)*v;");
10269   verifyFormat("x = (foo *restrict)*v;");
10270   verifyFormat("x = (foo *__attribute__((foo)))*v;");
10271   verifyFormat("x = (foo *_Nonnull)*v;");
10272   verifyFormat("x = (foo *_Nullable)*v;");
10273   verifyFormat("x = (foo *_Null_unspecified)*v;");
10274   verifyFormat("x = (foo *_Nonnull)*v;");
10275   verifyFormat("x = (foo *[[clang::attr]])*v;");
10276   verifyFormat("x = (foo *[[clang::attr(\"foo\")]])*v;");
10277   verifyFormat("x = (foo *__ptr32)*v;");
10278   verifyFormat("x = (foo *__ptr64)*v;");
10279   verifyFormat("x = (foo *__capability)*v;");
10280 
10281   // Check that we handle multiple trailing qualifiers and skip them all to
10282   // determine that the expression is a cast to a pointer type.
10283   FormatStyle LongPointerRight = getLLVMStyleWithColumns(999);
10284   FormatStyle LongPointerLeft = getLLVMStyleWithColumns(999);
10285   LongPointerLeft.PointerAlignment = FormatStyle::PAS_Left;
10286   StringRef AllQualifiers =
10287       "const volatile restrict __attribute__((foo)) _Nonnull _Null_unspecified "
10288       "_Nonnull [[clang::attr]] __ptr32 __ptr64 __capability";
10289   verifyFormat(("x = (foo *" + AllQualifiers + ")*v;").str(), LongPointerRight);
10290   verifyFormat(("x = (foo* " + AllQualifiers + ")*v;").str(), LongPointerLeft);
10291 
10292   // Also check that address-of is not parsed as a binary bitwise-and:
10293   verifyFormat("x = (foo *const)&v;");
10294   verifyFormat(("x = (foo *" + AllQualifiers + ")&v;").str(), LongPointerRight);
10295   verifyFormat(("x = (foo* " + AllQualifiers + ")&v;").str(), LongPointerLeft);
10296 
10297   // Check custom qualifiers:
10298   FormatStyle CustomQualifier = getLLVMStyleWithColumns(999);
10299   CustomQualifier.AttributeMacros.push_back("__my_qualifier");
10300   verifyFormat("x = (foo * __my_qualifier) * v;"); // not parsed as qualifier.
10301   verifyFormat("x = (foo *__my_qualifier)*v;", CustomQualifier);
10302   verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)*v;").str(),
10303                CustomQualifier);
10304   verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)&v;").str(),
10305                CustomQualifier);
10306 
10307   // Check that unknown identifiers result in binary operator parsing:
10308   verifyFormat("x = (foo * __unknown_qualifier) * v;");
10309   verifyFormat("x = (foo * __unknown_qualifier) & v;");
10310 }
10311 
10312 TEST_F(FormatTest, UnderstandsSquareAttributes) {
10313   verifyFormat("SomeType s [[unused]] (InitValue);");
10314   verifyFormat("SomeType s [[gnu::unused]] (InitValue);");
10315   verifyFormat("SomeType s [[using gnu: unused]] (InitValue);");
10316   verifyFormat("[[gsl::suppress(\"clang-tidy-check-name\")]] void f() {}");
10317   verifyFormat("void f() [[deprecated(\"so sorry\")]];");
10318   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10319                "    [[unused]] aaaaaaaaaaaaaaaaaaaaaaa(int i);");
10320   verifyFormat("[[nodiscard]] bool f() { return false; }");
10321   verifyFormat("class [[nodiscard]] f {\npublic:\n  f() {}\n}");
10322   verifyFormat("class [[deprecated(\"so sorry\")]] f {\npublic:\n  f() {}\n}");
10323   verifyFormat("class [[gnu::unused]] f {\npublic:\n  f() {}\n}");
10324   verifyFormat("[[nodiscard]] ::qualified_type f();");
10325 
10326   // Make sure we do not mistake attributes for array subscripts.
10327   verifyFormat("int a() {}\n"
10328                "[[unused]] int b() {}\n");
10329   verifyFormat("NSArray *arr;\n"
10330                "arr[[Foo() bar]];");
10331 
10332   // On the other hand, we still need to correctly find array subscripts.
10333   verifyFormat("int a = std::vector<int>{1, 2, 3}[0];");
10334 
10335   // Make sure that we do not mistake Objective-C method inside array literals
10336   // as attributes, even if those method names are also keywords.
10337   verifyFormat("@[ [foo bar] ];");
10338   verifyFormat("@[ [NSArray class] ];");
10339   verifyFormat("@[ [foo enum] ];");
10340 
10341   verifyFormat("template <typename T> [[nodiscard]] int a() { return 1; }");
10342 
10343   // Make sure we do not parse attributes as lambda introducers.
10344   FormatStyle MultiLineFunctions = getLLVMStyle();
10345   MultiLineFunctions.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
10346   verifyFormat("[[unused]] int b() {\n"
10347                "  return 42;\n"
10348                "}\n",
10349                MultiLineFunctions);
10350 }
10351 
10352 TEST_F(FormatTest, AttributeClass) {
10353   FormatStyle Style = getChromiumStyle(FormatStyle::LK_Cpp);
10354   verifyFormat("class S {\n"
10355                "  S(S&&) = default;\n"
10356                "};",
10357                Style);
10358   verifyFormat("class [[nodiscard]] S {\n"
10359                "  S(S&&) = default;\n"
10360                "};",
10361                Style);
10362   verifyFormat("class __attribute((maybeunused)) S {\n"
10363                "  S(S&&) = default;\n"
10364                "};",
10365                Style);
10366   verifyFormat("struct S {\n"
10367                "  S(S&&) = default;\n"
10368                "};",
10369                Style);
10370   verifyFormat("struct [[nodiscard]] S {\n"
10371                "  S(S&&) = default;\n"
10372                "};",
10373                Style);
10374 }
10375 
10376 TEST_F(FormatTest, AttributesAfterMacro) {
10377   FormatStyle Style = getLLVMStyle();
10378   verifyFormat("MACRO;\n"
10379                "__attribute__((maybe_unused)) int foo() {\n"
10380                "  //...\n"
10381                "}");
10382 
10383   verifyFormat("MACRO;\n"
10384                "[[nodiscard]] int foo() {\n"
10385                "  //...\n"
10386                "}");
10387 
10388   EXPECT_EQ("MACRO\n\n"
10389             "__attribute__((maybe_unused)) int foo() {\n"
10390             "  //...\n"
10391             "}",
10392             format("MACRO\n\n"
10393                    "__attribute__((maybe_unused)) int foo() {\n"
10394                    "  //...\n"
10395                    "}"));
10396 
10397   EXPECT_EQ("MACRO\n\n"
10398             "[[nodiscard]] int foo() {\n"
10399             "  //...\n"
10400             "}",
10401             format("MACRO\n\n"
10402                    "[[nodiscard]] int foo() {\n"
10403                    "  //...\n"
10404                    "}"));
10405 }
10406 
10407 TEST_F(FormatTest, AttributePenaltyBreaking) {
10408   FormatStyle Style = getLLVMStyle();
10409   verifyFormat("void ABCDEFGH::ABCDEFGHIJKLMN(\n"
10410                "    [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}",
10411                Style);
10412   verifyFormat("void ABCDEFGH::ABCDEFGHIJK(\n"
10413                "    [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}",
10414                Style);
10415   verifyFormat("void ABCDEFGH::ABCDEFGH([[maybe_unused]] const "
10416                "shared_ptr<ALongTypeName> &C d) {\n}",
10417                Style);
10418 }
10419 
10420 TEST_F(FormatTest, UnderstandsEllipsis) {
10421   FormatStyle Style = getLLVMStyle();
10422   verifyFormat("int printf(const char *fmt, ...);");
10423   verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
10424   verifyFormat("template <class... Ts> void Foo(Ts *...ts) {}");
10425 
10426   verifyFormat("template <int *...PP> a;", Style);
10427 
10428   Style.PointerAlignment = FormatStyle::PAS_Left;
10429   verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", Style);
10430 
10431   verifyFormat("template <int*... PP> a;", Style);
10432 
10433   Style.PointerAlignment = FormatStyle::PAS_Middle;
10434   verifyFormat("template <int *... PP> a;", Style);
10435 }
10436 
10437 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
10438   EXPECT_EQ("int *a;\n"
10439             "int *a;\n"
10440             "int *a;",
10441             format("int *a;\n"
10442                    "int* a;\n"
10443                    "int *a;",
10444                    getGoogleStyle()));
10445   EXPECT_EQ("int* a;\n"
10446             "int* a;\n"
10447             "int* a;",
10448             format("int* a;\n"
10449                    "int* a;\n"
10450                    "int *a;",
10451                    getGoogleStyle()));
10452   EXPECT_EQ("int *a;\n"
10453             "int *a;\n"
10454             "int *a;",
10455             format("int *a;\n"
10456                    "int * a;\n"
10457                    "int *  a;",
10458                    getGoogleStyle()));
10459   EXPECT_EQ("auto x = [] {\n"
10460             "  int *a;\n"
10461             "  int *a;\n"
10462             "  int *a;\n"
10463             "};",
10464             format("auto x=[]{int *a;\n"
10465                    "int * a;\n"
10466                    "int *  a;};",
10467                    getGoogleStyle()));
10468 }
10469 
10470 TEST_F(FormatTest, UnderstandsRvalueReferences) {
10471   verifyFormat("int f(int &&a) {}");
10472   verifyFormat("int f(int a, char &&b) {}");
10473   verifyFormat("void f() { int &&a = b; }");
10474   verifyGoogleFormat("int f(int a, char&& b) {}");
10475   verifyGoogleFormat("void f() { int&& a = b; }");
10476 
10477   verifyIndependentOfContext("A<int &&> a;");
10478   verifyIndependentOfContext("A<int &&, int &&> a;");
10479   verifyGoogleFormat("A<int&&> a;");
10480   verifyGoogleFormat("A<int&&, int&&> a;");
10481 
10482   // Not rvalue references:
10483   verifyFormat("template <bool B, bool C> class A {\n"
10484                "  static_assert(B && C, \"Something is wrong\");\n"
10485                "};");
10486   verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
10487   verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
10488   verifyFormat("#define A(a, b) (a && b)");
10489 }
10490 
10491 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
10492   verifyFormat("void f() {\n"
10493                "  x[aaaaaaaaa -\n"
10494                "    b] = 23;\n"
10495                "}",
10496                getLLVMStyleWithColumns(15));
10497 }
10498 
10499 TEST_F(FormatTest, FormatsCasts) {
10500   verifyFormat("Type *A = static_cast<Type *>(P);");
10501   verifyFormat("Type *A = (Type *)P;");
10502   verifyFormat("Type *A = (vector<Type *, int *>)P;");
10503   verifyFormat("int a = (int)(2.0f);");
10504   verifyFormat("int a = (int)2.0f;");
10505   verifyFormat("x[(int32)y];");
10506   verifyFormat("x = (int32)y;");
10507   verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
10508   verifyFormat("int a = (int)*b;");
10509   verifyFormat("int a = (int)2.0f;");
10510   verifyFormat("int a = (int)~0;");
10511   verifyFormat("int a = (int)++a;");
10512   verifyFormat("int a = (int)sizeof(int);");
10513   verifyFormat("int a = (int)+2;");
10514   verifyFormat("my_int a = (my_int)2.0f;");
10515   verifyFormat("my_int a = (my_int)sizeof(int);");
10516   verifyFormat("return (my_int)aaa;");
10517   verifyFormat("#define x ((int)-1)");
10518   verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
10519   verifyFormat("#define p(q) ((int *)&q)");
10520   verifyFormat("fn(a)(b) + 1;");
10521 
10522   verifyFormat("void f() { my_int a = (my_int)*b; }");
10523   verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
10524   verifyFormat("my_int a = (my_int)~0;");
10525   verifyFormat("my_int a = (my_int)++a;");
10526   verifyFormat("my_int a = (my_int)-2;");
10527   verifyFormat("my_int a = (my_int)1;");
10528   verifyFormat("my_int a = (my_int *)1;");
10529   verifyFormat("my_int a = (const my_int)-1;");
10530   verifyFormat("my_int a = (const my_int *)-1;");
10531   verifyFormat("my_int a = (my_int)(my_int)-1;");
10532   verifyFormat("my_int a = (ns::my_int)-2;");
10533   verifyFormat("case (my_int)ONE:");
10534   verifyFormat("auto x = (X)this;");
10535   // Casts in Obj-C style calls used to not be recognized as such.
10536   verifyFormat("int a = [(type*)[((type*)val) arg] arg];", getGoogleStyle());
10537 
10538   // FIXME: single value wrapped with paren will be treated as cast.
10539   verifyFormat("void f(int i = (kValue)*kMask) {}");
10540 
10541   verifyFormat("{ (void)F; }");
10542 
10543   // Don't break after a cast's
10544   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
10545                "    (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
10546                "                                   bbbbbbbbbbbbbbbbbbbbbb);");
10547 
10548   verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(x)");
10549   verifyFormat("#define CONF_BOOL(x) (bool *)(x)");
10550   verifyFormat("#define CONF_BOOL(x) (bool)(x)");
10551   verifyFormat("bool *y = (bool *)(void *)(x);");
10552   verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(int)(x)");
10553   verifyFormat("bool *y = (bool *)(void *)(int)(x);");
10554   verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(int)foo(x)");
10555   verifyFormat("bool *y = (bool *)(void *)(int)foo(x);");
10556 
10557   // These are not casts.
10558   verifyFormat("void f(int *) {}");
10559   verifyFormat("f(foo)->b;");
10560   verifyFormat("f(foo).b;");
10561   verifyFormat("f(foo)(b);");
10562   verifyFormat("f(foo)[b];");
10563   verifyFormat("[](foo) { return 4; }(bar);");
10564   verifyFormat("(*funptr)(foo)[4];");
10565   verifyFormat("funptrs[4](foo)[4];");
10566   verifyFormat("void f(int *);");
10567   verifyFormat("void f(int *) = 0;");
10568   verifyFormat("void f(SmallVector<int>) {}");
10569   verifyFormat("void f(SmallVector<int>);");
10570   verifyFormat("void f(SmallVector<int>) = 0;");
10571   verifyFormat("void f(int i = (kA * kB) & kMask) {}");
10572   verifyFormat("int a = sizeof(int) * b;");
10573   verifyFormat("int a = alignof(int) * b;", getGoogleStyle());
10574   verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
10575   verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
10576   verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
10577 
10578   // These are not casts, but at some point were confused with casts.
10579   verifyFormat("virtual void foo(int *) override;");
10580   verifyFormat("virtual void foo(char &) const;");
10581   verifyFormat("virtual void foo(int *a, char *) const;");
10582   verifyFormat("int a = sizeof(int *) + b;");
10583   verifyFormat("int a = alignof(int *) + b;", getGoogleStyle());
10584   verifyFormat("bool b = f(g<int>) && c;");
10585   verifyFormat("typedef void (*f)(int i) func;");
10586   verifyFormat("void operator++(int) noexcept;");
10587   verifyFormat("void operator++(int &) noexcept;");
10588   verifyFormat("void operator delete(void *, std::size_t, const std::nothrow_t "
10589                "&) noexcept;");
10590   verifyFormat(
10591       "void operator delete(std::size_t, const std::nothrow_t &) noexcept;");
10592   verifyFormat("void operator delete(const std::nothrow_t &) noexcept;");
10593   verifyFormat("void operator delete(std::nothrow_t &) noexcept;");
10594   verifyFormat("void operator delete(nothrow_t &) noexcept;");
10595   verifyFormat("void operator delete(foo &) noexcept;");
10596   verifyFormat("void operator delete(foo) noexcept;");
10597   verifyFormat("void operator delete(int) noexcept;");
10598   verifyFormat("void operator delete(int &) noexcept;");
10599   verifyFormat("void operator delete(int &) volatile noexcept;");
10600   verifyFormat("void operator delete(int &) const");
10601   verifyFormat("void operator delete(int &) = default");
10602   verifyFormat("void operator delete(int &) = delete");
10603   verifyFormat("void operator delete(int &) [[noreturn]]");
10604   verifyFormat("void operator delete(int &) throw();");
10605   verifyFormat("void operator delete(int &) throw(int);");
10606   verifyFormat("auto operator delete(int &) -> int;");
10607   verifyFormat("auto operator delete(int &) override");
10608   verifyFormat("auto operator delete(int &) final");
10609 
10610   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
10611                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
10612   // FIXME: The indentation here is not ideal.
10613   verifyFormat(
10614       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10615       "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
10616       "        [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
10617 }
10618 
10619 TEST_F(FormatTest, FormatsFunctionTypes) {
10620   verifyFormat("A<bool()> a;");
10621   verifyFormat("A<SomeType()> a;");
10622   verifyFormat("A<void (*)(int, std::string)> a;");
10623   verifyFormat("A<void *(int)>;");
10624   verifyFormat("void *(*a)(int *, SomeType *);");
10625   verifyFormat("int (*func)(void *);");
10626   verifyFormat("void f() { int (*func)(void *); }");
10627   verifyFormat("template <class CallbackClass>\n"
10628                "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
10629 
10630   verifyGoogleFormat("A<void*(int*, SomeType*)>;");
10631   verifyGoogleFormat("void* (*a)(int);");
10632   verifyGoogleFormat(
10633       "template <class CallbackClass>\n"
10634       "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
10635 
10636   // Other constructs can look somewhat like function types:
10637   verifyFormat("A<sizeof(*x)> a;");
10638   verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
10639   verifyFormat("some_var = function(*some_pointer_var)[0];");
10640   verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
10641   verifyFormat("int x = f(&h)();");
10642   verifyFormat("returnsFunction(&param1, &param2)(param);");
10643   verifyFormat("std::function<\n"
10644                "    LooooooooooongTemplatedType<\n"
10645                "        SomeType>*(\n"
10646                "        LooooooooooooooooongType type)>\n"
10647                "    function;",
10648                getGoogleStyleWithColumns(40));
10649 }
10650 
10651 TEST_F(FormatTest, FormatsPointersToArrayTypes) {
10652   verifyFormat("A (*foo_)[6];");
10653   verifyFormat("vector<int> (*foo_)[6];");
10654 }
10655 
10656 TEST_F(FormatTest, BreaksLongVariableDeclarations) {
10657   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
10658                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
10659   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
10660                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
10661   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
10662                "    *LoooooooooooooooooooooooooooooooooooooooongVariable;");
10663 
10664   // Different ways of ()-initializiation.
10665   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
10666                "    LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
10667   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
10668                "    LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
10669   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
10670                "    LoooooooooooooooooooooooooooooooooooooooongVariable({});");
10671   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
10672                "    LoooooooooooooooooooooooooooooooooooooongVariable([A a]);");
10673 
10674   // Lambdas should not confuse the variable declaration heuristic.
10675   verifyFormat("LooooooooooooooooongType\n"
10676                "    variable(nullptr, [](A *a) {});",
10677                getLLVMStyleWithColumns(40));
10678 }
10679 
10680 TEST_F(FormatTest, BreaksLongDeclarations) {
10681   verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
10682                "    AnotherNameForTheLongType;");
10683   verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
10684                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10685   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10686                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
10687   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n"
10688                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
10689   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10690                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10691   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
10692                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10693   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
10694                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10695   verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
10696                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10697   verifyFormat("typeof(LoooooooooooooooooooooooooooooooooooooooooongName)\n"
10698                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10699   verifyFormat("_Atomic(LooooooooooooooooooooooooooooooooooooooooongName)\n"
10700                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10701   verifyFormat("__underlying_type(LooooooooooooooooooooooooooooooongName)\n"
10702                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10703   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10704                "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);");
10705   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10706                "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}");
10707   FormatStyle Indented = getLLVMStyle();
10708   Indented.IndentWrappedFunctionNames = true;
10709   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10710                "    LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
10711                Indented);
10712   verifyFormat(
10713       "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10714       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
10715       Indented);
10716   verifyFormat(
10717       "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
10718       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
10719       Indented);
10720   verifyFormat(
10721       "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
10722       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
10723       Indented);
10724 
10725   // FIXME: Without the comment, this breaks after "(".
10726   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType  // break\n"
10727                "    (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();",
10728                getGoogleStyle());
10729 
10730   verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
10731                "                  int LoooooooooooooooooooongParam2) {}");
10732   verifyFormat(
10733       "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
10734       "                                   SourceLocation L, IdentifierIn *II,\n"
10735       "                                   Type *T) {}");
10736   verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
10737                "ReallyReaaallyLongFunctionName(\n"
10738                "    const std::string &SomeParameter,\n"
10739                "    const SomeType<string, SomeOtherTemplateParameter>\n"
10740                "        &ReallyReallyLongParameterName,\n"
10741                "    const SomeType<string, SomeOtherTemplateParameter>\n"
10742                "        &AnotherLongParameterName) {}");
10743   verifyFormat("template <typename A>\n"
10744                "SomeLoooooooooooooooooooooongType<\n"
10745                "    typename some_namespace::SomeOtherType<A>::Type>\n"
10746                "Function() {}");
10747 
10748   verifyGoogleFormat(
10749       "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
10750       "    aaaaaaaaaaaaaaaaaaaaaaa;");
10751   verifyGoogleFormat(
10752       "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
10753       "                                   SourceLocation L) {}");
10754   verifyGoogleFormat(
10755       "some_namespace::LongReturnType\n"
10756       "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
10757       "    int first_long_parameter, int second_parameter) {}");
10758 
10759   verifyGoogleFormat("template <typename T>\n"
10760                      "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
10761                      "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
10762   verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10763                      "                   int aaaaaaaaaaaaaaaaaaaaaaa);");
10764 
10765   verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
10766                "    const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10767                "        *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
10768   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10769                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
10770                "        aaaaaaaaaaaaaaaaaaaaaaaa);");
10771   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10772                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
10773                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n"
10774                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
10775 
10776   verifyFormat("template <typename T> // Templates on own line.\n"
10777                "static int            // Some comment.\n"
10778                "MyFunction(int a);",
10779                getLLVMStyle());
10780 }
10781 
10782 TEST_F(FormatTest, FormatsAccessModifiers) {
10783   FormatStyle Style = getLLVMStyle();
10784   EXPECT_EQ(Style.EmptyLineBeforeAccessModifier,
10785             FormatStyle::ELBAMS_LogicalBlock);
10786   verifyFormat("struct foo {\n"
10787                "private:\n"
10788                "  void f() {}\n"
10789                "\n"
10790                "private:\n"
10791                "  int i;\n"
10792                "\n"
10793                "protected:\n"
10794                "  int j;\n"
10795                "};\n",
10796                Style);
10797   verifyFormat("struct foo {\n"
10798                "private:\n"
10799                "  void f() {}\n"
10800                "\n"
10801                "private:\n"
10802                "  int i;\n"
10803                "\n"
10804                "protected:\n"
10805                "  int j;\n"
10806                "};\n",
10807                "struct foo {\n"
10808                "private:\n"
10809                "  void f() {}\n"
10810                "private:\n"
10811                "  int i;\n"
10812                "protected:\n"
10813                "  int j;\n"
10814                "};\n",
10815                Style);
10816   verifyFormat("struct foo { /* comment */\n"
10817                "private:\n"
10818                "  int i;\n"
10819                "  // comment\n"
10820                "private:\n"
10821                "  int j;\n"
10822                "};\n",
10823                Style);
10824   verifyFormat("struct foo {\n"
10825                "#ifdef FOO\n"
10826                "#endif\n"
10827                "private:\n"
10828                "  int i;\n"
10829                "#ifdef FOO\n"
10830                "private:\n"
10831                "#endif\n"
10832                "  int j;\n"
10833                "};\n",
10834                Style);
10835   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
10836   verifyFormat("struct foo {\n"
10837                "private:\n"
10838                "  void f() {}\n"
10839                "private:\n"
10840                "  int i;\n"
10841                "protected:\n"
10842                "  int j;\n"
10843                "};\n",
10844                Style);
10845   verifyFormat("struct foo {\n"
10846                "private:\n"
10847                "  void f() {}\n"
10848                "private:\n"
10849                "  int i;\n"
10850                "protected:\n"
10851                "  int j;\n"
10852                "};\n",
10853                "struct foo {\n"
10854                "\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                Style);
10865   verifyFormat("struct foo { /* comment */\n"
10866                "private:\n"
10867                "  int i;\n"
10868                "  // comment\n"
10869                "private:\n"
10870                "  int j;\n"
10871                "};\n",
10872                "struct foo { /* comment */\n"
10873                "\n"
10874                "private:\n"
10875                "  int i;\n"
10876                "  // comment\n"
10877                "\n"
10878                "private:\n"
10879                "  int j;\n"
10880                "};\n",
10881                Style);
10882   verifyFormat("struct foo {\n"
10883                "#ifdef FOO\n"
10884                "#endif\n"
10885                "private:\n"
10886                "  int i;\n"
10887                "#ifdef FOO\n"
10888                "private:\n"
10889                "#endif\n"
10890                "  int j;\n"
10891                "};\n",
10892                "struct foo {\n"
10893                "#ifdef FOO\n"
10894                "#endif\n"
10895                "\n"
10896                "private:\n"
10897                "  int i;\n"
10898                "#ifdef FOO\n"
10899                "\n"
10900                "private:\n"
10901                "#endif\n"
10902                "  int j;\n"
10903                "};\n",
10904                Style);
10905   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
10906   verifyFormat("struct foo {\n"
10907                "private:\n"
10908                "  void f() {}\n"
10909                "\n"
10910                "private:\n"
10911                "  int i;\n"
10912                "\n"
10913                "protected:\n"
10914                "  int j;\n"
10915                "};\n",
10916                Style);
10917   verifyFormat("struct foo {\n"
10918                "private:\n"
10919                "  void f() {}\n"
10920                "\n"
10921                "private:\n"
10922                "  int i;\n"
10923                "\n"
10924                "protected:\n"
10925                "  int j;\n"
10926                "};\n",
10927                "struct foo {\n"
10928                "private:\n"
10929                "  void f() {}\n"
10930                "private:\n"
10931                "  int i;\n"
10932                "protected:\n"
10933                "  int j;\n"
10934                "};\n",
10935                Style);
10936   verifyFormat("struct foo { /* comment */\n"
10937                "private:\n"
10938                "  int i;\n"
10939                "  // comment\n"
10940                "\n"
10941                "private:\n"
10942                "  int j;\n"
10943                "};\n",
10944                "struct foo { /* comment */\n"
10945                "private:\n"
10946                "  int i;\n"
10947                "  // comment\n"
10948                "\n"
10949                "private:\n"
10950                "  int j;\n"
10951                "};\n",
10952                Style);
10953   verifyFormat("struct foo {\n"
10954                "#ifdef FOO\n"
10955                "#endif\n"
10956                "\n"
10957                "private:\n"
10958                "  int i;\n"
10959                "#ifdef FOO\n"
10960                "\n"
10961                "private:\n"
10962                "#endif\n"
10963                "  int j;\n"
10964                "};\n",
10965                "struct foo {\n"
10966                "#ifdef FOO\n"
10967                "#endif\n"
10968                "private:\n"
10969                "  int i;\n"
10970                "#ifdef FOO\n"
10971                "private:\n"
10972                "#endif\n"
10973                "  int j;\n"
10974                "};\n",
10975                Style);
10976   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
10977   EXPECT_EQ("struct foo {\n"
10978             "\n"
10979             "private:\n"
10980             "  void f() {}\n"
10981             "\n"
10982             "private:\n"
10983             "  int i;\n"
10984             "\n"
10985             "protected:\n"
10986             "  int j;\n"
10987             "};\n",
10988             format("struct foo {\n"
10989                    "\n"
10990                    "private:\n"
10991                    "  void f() {}\n"
10992                    "\n"
10993                    "private:\n"
10994                    "  int i;\n"
10995                    "\n"
10996                    "protected:\n"
10997                    "  int j;\n"
10998                    "};\n",
10999                    Style));
11000   verifyFormat("struct foo {\n"
11001                "private:\n"
11002                "  void f() {}\n"
11003                "private:\n"
11004                "  int i;\n"
11005                "protected:\n"
11006                "  int j;\n"
11007                "};\n",
11008                Style);
11009   EXPECT_EQ("struct foo { /* comment */\n"
11010             "\n"
11011             "private:\n"
11012             "  int i;\n"
11013             "  // comment\n"
11014             "\n"
11015             "private:\n"
11016             "  int j;\n"
11017             "};\n",
11018             format("struct foo { /* comment */\n"
11019                    "\n"
11020                    "private:\n"
11021                    "  int i;\n"
11022                    "  // comment\n"
11023                    "\n"
11024                    "private:\n"
11025                    "  int j;\n"
11026                    "};\n",
11027                    Style));
11028   verifyFormat("struct foo { /* comment */\n"
11029                "private:\n"
11030                "  int i;\n"
11031                "  // comment\n"
11032                "private:\n"
11033                "  int j;\n"
11034                "};\n",
11035                Style);
11036   EXPECT_EQ("struct foo {\n"
11037             "#ifdef FOO\n"
11038             "#endif\n"
11039             "\n"
11040             "private:\n"
11041             "  int i;\n"
11042             "#ifdef FOO\n"
11043             "\n"
11044             "private:\n"
11045             "#endif\n"
11046             "  int j;\n"
11047             "};\n",
11048             format("struct foo {\n"
11049                    "#ifdef FOO\n"
11050                    "#endif\n"
11051                    "\n"
11052                    "private:\n"
11053                    "  int i;\n"
11054                    "#ifdef FOO\n"
11055                    "\n"
11056                    "private:\n"
11057                    "#endif\n"
11058                    "  int j;\n"
11059                    "};\n",
11060                    Style));
11061   verifyFormat("struct foo {\n"
11062                "#ifdef FOO\n"
11063                "#endif\n"
11064                "private:\n"
11065                "  int i;\n"
11066                "#ifdef FOO\n"
11067                "private:\n"
11068                "#endif\n"
11069                "  int j;\n"
11070                "};\n",
11071                Style);
11072 
11073   FormatStyle NoEmptyLines = getLLVMStyle();
11074   NoEmptyLines.MaxEmptyLinesToKeep = 0;
11075   verifyFormat("struct foo {\n"
11076                "private:\n"
11077                "  void f() {}\n"
11078                "\n"
11079                "private:\n"
11080                "  int i;\n"
11081                "\n"
11082                "public:\n"
11083                "protected:\n"
11084                "  int j;\n"
11085                "};\n",
11086                NoEmptyLines);
11087 
11088   NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
11089   verifyFormat("struct foo {\n"
11090                "private:\n"
11091                "  void f() {}\n"
11092                "private:\n"
11093                "  int i;\n"
11094                "public:\n"
11095                "protected:\n"
11096                "  int j;\n"
11097                "};\n",
11098                NoEmptyLines);
11099 
11100   NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
11101   verifyFormat("struct foo {\n"
11102                "private:\n"
11103                "  void f() {}\n"
11104                "\n"
11105                "private:\n"
11106                "  int i;\n"
11107                "\n"
11108                "public:\n"
11109                "\n"
11110                "protected:\n"
11111                "  int j;\n"
11112                "};\n",
11113                NoEmptyLines);
11114 }
11115 
11116 TEST_F(FormatTest, FormatsAfterAccessModifiers) {
11117 
11118   FormatStyle Style = getLLVMStyle();
11119   EXPECT_EQ(Style.EmptyLineAfterAccessModifier, FormatStyle::ELAAMS_Never);
11120   verifyFormat("struct foo {\n"
11121                "private:\n"
11122                "  void f() {}\n"
11123                "\n"
11124                "private:\n"
11125                "  int i;\n"
11126                "\n"
11127                "protected:\n"
11128                "  int j;\n"
11129                "};\n",
11130                Style);
11131 
11132   // Check if lines are removed.
11133   verifyFormat("struct foo {\n"
11134                "private:\n"
11135                "  void f() {}\n"
11136                "\n"
11137                "private:\n"
11138                "  int i;\n"
11139                "\n"
11140                "protected:\n"
11141                "  int j;\n"
11142                "};\n",
11143                "struct foo {\n"
11144                "private:\n"
11145                "\n"
11146                "  void f() {}\n"
11147                "\n"
11148                "private:\n"
11149                "\n"
11150                "  int i;\n"
11151                "\n"
11152                "protected:\n"
11153                "\n"
11154                "  int j;\n"
11155                "};\n",
11156                Style);
11157 
11158   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
11159   verifyFormat("struct foo {\n"
11160                "private:\n"
11161                "\n"
11162                "  void f() {}\n"
11163                "\n"
11164                "private:\n"
11165                "\n"
11166                "  int i;\n"
11167                "\n"
11168                "protected:\n"
11169                "\n"
11170                "  int j;\n"
11171                "};\n",
11172                Style);
11173 
11174   // Check if lines are added.
11175   verifyFormat("struct foo {\n"
11176                "private:\n"
11177                "\n"
11178                "  void f() {}\n"
11179                "\n"
11180                "private:\n"
11181                "\n"
11182                "  int i;\n"
11183                "\n"
11184                "protected:\n"
11185                "\n"
11186                "  int j;\n"
11187                "};\n",
11188                "struct foo {\n"
11189                "private:\n"
11190                "  void f() {}\n"
11191                "\n"
11192                "private:\n"
11193                "  int i;\n"
11194                "\n"
11195                "protected:\n"
11196                "  int j;\n"
11197                "};\n",
11198                Style);
11199 
11200   // Leave tests rely on the code layout, test::messUp can not be used.
11201   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
11202   Style.MaxEmptyLinesToKeep = 0u;
11203   verifyFormat("struct foo {\n"
11204                "private:\n"
11205                "  void f() {}\n"
11206                "\n"
11207                "private:\n"
11208                "  int i;\n"
11209                "\n"
11210                "protected:\n"
11211                "  int j;\n"
11212                "};\n",
11213                Style);
11214 
11215   // Check if MaxEmptyLinesToKeep is respected.
11216   EXPECT_EQ("struct foo {\n"
11217             "private:\n"
11218             "  void f() {}\n"
11219             "\n"
11220             "private:\n"
11221             "  int i;\n"
11222             "\n"
11223             "protected:\n"
11224             "  int j;\n"
11225             "};\n",
11226             format("struct foo {\n"
11227                    "private:\n"
11228                    "\n\n\n"
11229                    "  void f() {}\n"
11230                    "\n"
11231                    "private:\n"
11232                    "\n\n\n"
11233                    "  int i;\n"
11234                    "\n"
11235                    "protected:\n"
11236                    "\n\n\n"
11237                    "  int j;\n"
11238                    "};\n",
11239                    Style));
11240 
11241   Style.MaxEmptyLinesToKeep = 1u;
11242   EXPECT_EQ("struct foo {\n"
11243             "private:\n"
11244             "\n"
11245             "  void f() {}\n"
11246             "\n"
11247             "private:\n"
11248             "\n"
11249             "  int i;\n"
11250             "\n"
11251             "protected:\n"
11252             "\n"
11253             "  int j;\n"
11254             "};\n",
11255             format("struct foo {\n"
11256                    "private:\n"
11257                    "\n"
11258                    "  void f() {}\n"
11259                    "\n"
11260                    "private:\n"
11261                    "\n"
11262                    "  int i;\n"
11263                    "\n"
11264                    "protected:\n"
11265                    "\n"
11266                    "  int j;\n"
11267                    "};\n",
11268                    Style));
11269   // Check if no lines are kept.
11270   EXPECT_EQ("struct foo {\n"
11271             "private:\n"
11272             "  void f() {}\n"
11273             "\n"
11274             "private:\n"
11275             "  int i;\n"
11276             "\n"
11277             "protected:\n"
11278             "  int j;\n"
11279             "};\n",
11280             format("struct foo {\n"
11281                    "private:\n"
11282                    "  void f() {}\n"
11283                    "\n"
11284                    "private:\n"
11285                    "  int i;\n"
11286                    "\n"
11287                    "protected:\n"
11288                    "  int j;\n"
11289                    "};\n",
11290                    Style));
11291   // Check if MaxEmptyLinesToKeep is respected.
11292   EXPECT_EQ("struct foo {\n"
11293             "private:\n"
11294             "\n"
11295             "  void f() {}\n"
11296             "\n"
11297             "private:\n"
11298             "\n"
11299             "  int i;\n"
11300             "\n"
11301             "protected:\n"
11302             "\n"
11303             "  int j;\n"
11304             "};\n",
11305             format("struct foo {\n"
11306                    "private:\n"
11307                    "\n\n\n"
11308                    "  void f() {}\n"
11309                    "\n"
11310                    "private:\n"
11311                    "\n\n\n"
11312                    "  int i;\n"
11313                    "\n"
11314                    "protected:\n"
11315                    "\n\n\n"
11316                    "  int j;\n"
11317                    "};\n",
11318                    Style));
11319 
11320   Style.MaxEmptyLinesToKeep = 10u;
11321   EXPECT_EQ("struct foo {\n"
11322             "private:\n"
11323             "\n\n\n"
11324             "  void f() {}\n"
11325             "\n"
11326             "private:\n"
11327             "\n\n\n"
11328             "  int i;\n"
11329             "\n"
11330             "protected:\n"
11331             "\n\n\n"
11332             "  int j;\n"
11333             "};\n",
11334             format("struct foo {\n"
11335                    "private:\n"
11336                    "\n\n\n"
11337                    "  void f() {}\n"
11338                    "\n"
11339                    "private:\n"
11340                    "\n\n\n"
11341                    "  int i;\n"
11342                    "\n"
11343                    "protected:\n"
11344                    "\n\n\n"
11345                    "  int j;\n"
11346                    "};\n",
11347                    Style));
11348 
11349   // Test with comments.
11350   Style = getLLVMStyle();
11351   verifyFormat("struct foo {\n"
11352                "private:\n"
11353                "  // comment\n"
11354                "  void f() {}\n"
11355                "\n"
11356                "private: /* comment */\n"
11357                "  int i;\n"
11358                "};\n",
11359                Style);
11360   verifyFormat("struct foo {\n"
11361                "private:\n"
11362                "  // comment\n"
11363                "  void f() {}\n"
11364                "\n"
11365                "private: /* comment */\n"
11366                "  int i;\n"
11367                "};\n",
11368                "struct foo {\n"
11369                "private:\n"
11370                "\n"
11371                "  // comment\n"
11372                "  void f() {}\n"
11373                "\n"
11374                "private: /* comment */\n"
11375                "\n"
11376                "  int i;\n"
11377                "};\n",
11378                Style);
11379 
11380   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
11381   verifyFormat("struct foo {\n"
11382                "private:\n"
11383                "\n"
11384                "  // comment\n"
11385                "  void f() {}\n"
11386                "\n"
11387                "private: /* comment */\n"
11388                "\n"
11389                "  int i;\n"
11390                "};\n",
11391                "struct foo {\n"
11392                "private:\n"
11393                "  // comment\n"
11394                "  void f() {}\n"
11395                "\n"
11396                "private: /* comment */\n"
11397                "  int i;\n"
11398                "};\n",
11399                Style);
11400   verifyFormat("struct foo {\n"
11401                "private:\n"
11402                "\n"
11403                "  // comment\n"
11404                "  void f() {}\n"
11405                "\n"
11406                "private: /* comment */\n"
11407                "\n"
11408                "  int i;\n"
11409                "};\n",
11410                Style);
11411 
11412   // Test with preprocessor defines.
11413   Style = getLLVMStyle();
11414   verifyFormat("struct foo {\n"
11415                "private:\n"
11416                "#ifdef FOO\n"
11417                "#endif\n"
11418                "  void f() {}\n"
11419                "};\n",
11420                Style);
11421   verifyFormat("struct foo {\n"
11422                "private:\n"
11423                "#ifdef FOO\n"
11424                "#endif\n"
11425                "  void f() {}\n"
11426                "};\n",
11427                "struct foo {\n"
11428                "private:\n"
11429                "\n"
11430                "#ifdef FOO\n"
11431                "#endif\n"
11432                "  void f() {}\n"
11433                "};\n",
11434                Style);
11435 
11436   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
11437   verifyFormat("struct foo {\n"
11438                "private:\n"
11439                "\n"
11440                "#ifdef FOO\n"
11441                "#endif\n"
11442                "  void f() {}\n"
11443                "};\n",
11444                "struct foo {\n"
11445                "private:\n"
11446                "#ifdef FOO\n"
11447                "#endif\n"
11448                "  void f() {}\n"
11449                "};\n",
11450                Style);
11451   verifyFormat("struct foo {\n"
11452                "private:\n"
11453                "\n"
11454                "#ifdef FOO\n"
11455                "#endif\n"
11456                "  void f() {}\n"
11457                "};\n",
11458                Style);
11459 }
11460 
11461 TEST_F(FormatTest, FormatsAfterAndBeforeAccessModifiersInteraction) {
11462   // Combined tests of EmptyLineAfterAccessModifier and
11463   // EmptyLineBeforeAccessModifier.
11464   FormatStyle Style = getLLVMStyle();
11465   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
11466   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
11467   verifyFormat("struct foo {\n"
11468                "private:\n"
11469                "\n"
11470                "protected:\n"
11471                "};\n",
11472                Style);
11473 
11474   Style.MaxEmptyLinesToKeep = 10u;
11475   // Both remove all new lines.
11476   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
11477   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
11478   verifyFormat("struct foo {\n"
11479                "private:\n"
11480                "protected:\n"
11481                "};\n",
11482                "struct foo {\n"
11483                "private:\n"
11484                "\n\n\n"
11485                "protected:\n"
11486                "};\n",
11487                Style);
11488 
11489   // Leave tests rely on the code layout, test::messUp can not be used.
11490   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
11491   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
11492   Style.MaxEmptyLinesToKeep = 10u;
11493   EXPECT_EQ("struct foo {\n"
11494             "private:\n"
11495             "\n\n\n"
11496             "protected:\n"
11497             "};\n",
11498             format("struct foo {\n"
11499                    "private:\n"
11500                    "\n\n\n"
11501                    "protected:\n"
11502                    "};\n",
11503                    Style));
11504   Style.MaxEmptyLinesToKeep = 3u;
11505   EXPECT_EQ("struct foo {\n"
11506             "private:\n"
11507             "\n\n\n"
11508             "protected:\n"
11509             "};\n",
11510             format("struct foo {\n"
11511                    "private:\n"
11512                    "\n\n\n"
11513                    "protected:\n"
11514                    "};\n",
11515                    Style));
11516   Style.MaxEmptyLinesToKeep = 1u;
11517   EXPECT_EQ("struct foo {\n"
11518             "private:\n"
11519             "\n\n\n"
11520             "protected:\n"
11521             "};\n",
11522             format("struct foo {\n"
11523                    "private:\n"
11524                    "\n\n\n"
11525                    "protected:\n"
11526                    "};\n",
11527                    Style)); // Based on new lines in original document and not
11528                             // on the setting.
11529 
11530   Style.MaxEmptyLinesToKeep = 10u;
11531   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
11532   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
11533   // Newlines are kept if they are greater than zero,
11534   // test::messUp removes all new lines which changes the logic
11535   EXPECT_EQ("struct foo {\n"
11536             "private:\n"
11537             "\n\n\n"
11538             "protected:\n"
11539             "};\n",
11540             format("struct foo {\n"
11541                    "private:\n"
11542                    "\n\n\n"
11543                    "protected:\n"
11544                    "};\n",
11545                    Style));
11546 
11547   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
11548   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
11549   // test::messUp removes all new lines which changes the logic
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 
11562   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
11563   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
11564   EXPECT_EQ("struct foo {\n"
11565             "private:\n"
11566             "\n\n\n"
11567             "protected:\n"
11568             "};\n",
11569             format("struct foo {\n"
11570                    "private:\n"
11571                    "\n\n\n"
11572                    "protected:\n"
11573                    "};\n",
11574                    Style)); // test::messUp removes all new lines which changes
11575                             // the logic.
11576 
11577   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
11578   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
11579   verifyFormat("struct foo {\n"
11580                "private:\n"
11581                "protected:\n"
11582                "};\n",
11583                "struct foo {\n"
11584                "private:\n"
11585                "\n\n\n"
11586                "protected:\n"
11587                "};\n",
11588                Style);
11589 
11590   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
11591   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
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)); // test::messUp removes all new lines which changes
11603                             // the logic.
11604 
11605   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
11606   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
11607   verifyFormat("struct foo {\n"
11608                "private:\n"
11609                "protected:\n"
11610                "};\n",
11611                "struct foo {\n"
11612                "private:\n"
11613                "\n\n\n"
11614                "protected:\n"
11615                "};\n",
11616                Style);
11617 
11618   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
11619   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
11620   verifyFormat("struct foo {\n"
11621                "private:\n"
11622                "protected:\n"
11623                "};\n",
11624                "struct foo {\n"
11625                "private:\n"
11626                "\n\n\n"
11627                "protected:\n"
11628                "};\n",
11629                Style);
11630 
11631   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
11632   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
11633   verifyFormat("struct foo {\n"
11634                "private:\n"
11635                "protected:\n"
11636                "};\n",
11637                "struct foo {\n"
11638                "private:\n"
11639                "\n\n\n"
11640                "protected:\n"
11641                "};\n",
11642                Style);
11643 
11644   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
11645   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
11646   verifyFormat("struct foo {\n"
11647                "private:\n"
11648                "protected:\n"
11649                "};\n",
11650                "struct foo {\n"
11651                "private:\n"
11652                "\n\n\n"
11653                "protected:\n"
11654                "};\n",
11655                Style);
11656 }
11657 
11658 TEST_F(FormatTest, FormatsArrays) {
11659   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
11660                "                         [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
11661   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n"
11662                "                         [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;");
11663   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n"
11664                "    aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}");
11665   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
11666                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
11667   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
11668                "    [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
11669   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
11670                "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
11671                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
11672   verifyFormat(
11673       "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
11674       "             << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
11675       "                                  [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
11676   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n"
11677                "    .aaaaaaaaaaaaaaaaaaaaaa();");
11678 
11679   verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
11680                      "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
11681   verifyFormat(
11682       "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
11683       "                                  .aaaaaaa[0]\n"
11684       "                                  .aaaaaaaaaaaaaaaaaaaaaa();");
11685   verifyFormat("a[::b::c];");
11686 
11687   verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10));
11688 
11689   FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0);
11690   verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit);
11691 }
11692 
11693 TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
11694   verifyFormat("(a)->b();");
11695   verifyFormat("--a;");
11696 }
11697 
11698 TEST_F(FormatTest, HandlesIncludeDirectives) {
11699   verifyFormat("#include <string>\n"
11700                "#include <a/b/c.h>\n"
11701                "#include \"a/b/string\"\n"
11702                "#include \"string.h\"\n"
11703                "#include \"string.h\"\n"
11704                "#include <a-a>\n"
11705                "#include < path with space >\n"
11706                "#include_next <test.h>"
11707                "#include \"abc.h\" // this is included for ABC\n"
11708                "#include \"some long include\" // with a comment\n"
11709                "#include \"some very long include path\"\n"
11710                "#include <some/very/long/include/path>\n",
11711                getLLVMStyleWithColumns(35));
11712   EXPECT_EQ("#include \"a.h\"", format("#include  \"a.h\""));
11713   EXPECT_EQ("#include <a>", format("#include<a>"));
11714 
11715   verifyFormat("#import <string>");
11716   verifyFormat("#import <a/b/c.h>");
11717   verifyFormat("#import \"a/b/string\"");
11718   verifyFormat("#import \"string.h\"");
11719   verifyFormat("#import \"string.h\"");
11720   verifyFormat("#if __has_include(<strstream>)\n"
11721                "#include <strstream>\n"
11722                "#endif");
11723 
11724   verifyFormat("#define MY_IMPORT <a/b>");
11725 
11726   verifyFormat("#if __has_include(<a/b>)");
11727   verifyFormat("#if __has_include_next(<a/b>)");
11728   verifyFormat("#define F __has_include(<a/b>)");
11729   verifyFormat("#define F __has_include_next(<a/b>)");
11730 
11731   // Protocol buffer definition or missing "#".
11732   verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
11733                getLLVMStyleWithColumns(30));
11734 
11735   FormatStyle Style = getLLVMStyle();
11736   Style.AlwaysBreakBeforeMultilineStrings = true;
11737   Style.ColumnLimit = 0;
11738   verifyFormat("#import \"abc.h\"", Style);
11739 
11740   // But 'import' might also be a regular C++ namespace.
11741   verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
11742                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
11743 }
11744 
11745 //===----------------------------------------------------------------------===//
11746 // Error recovery tests.
11747 //===----------------------------------------------------------------------===//
11748 
11749 TEST_F(FormatTest, IncompleteParameterLists) {
11750   FormatStyle NoBinPacking = getLLVMStyle();
11751   NoBinPacking.BinPackParameters = false;
11752   verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
11753                "                        double *min_x,\n"
11754                "                        double *max_x,\n"
11755                "                        double *min_y,\n"
11756                "                        double *max_y,\n"
11757                "                        double *min_z,\n"
11758                "                        double *max_z, ) {}",
11759                NoBinPacking);
11760 }
11761 
11762 TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
11763   verifyFormat("void f() { return; }\n42");
11764   verifyFormat("void f() {\n"
11765                "  if (0)\n"
11766                "    return;\n"
11767                "}\n"
11768                "42");
11769   verifyFormat("void f() { return }\n42");
11770   verifyFormat("void f() {\n"
11771                "  if (0)\n"
11772                "    return\n"
11773                "}\n"
11774                "42");
11775 }
11776 
11777 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
11778   EXPECT_EQ("void f() { return }", format("void  f ( )  {  return  }"));
11779   EXPECT_EQ("void f() {\n"
11780             "  if (a)\n"
11781             "    return\n"
11782             "}",
11783             format("void  f  (  )  {  if  ( a )  return  }"));
11784   EXPECT_EQ("namespace N {\n"
11785             "void f()\n"
11786             "}",
11787             format("namespace  N  {  void f()  }"));
11788   EXPECT_EQ("namespace N {\n"
11789             "void f() {}\n"
11790             "void g()\n"
11791             "} // namespace N",
11792             format("namespace N  { void f( ) { } void g( ) }"));
11793 }
11794 
11795 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
11796   verifyFormat("int aaaaaaaa =\n"
11797                "    // Overlylongcomment\n"
11798                "    b;",
11799                getLLVMStyleWithColumns(20));
11800   verifyFormat("function(\n"
11801                "    ShortArgument,\n"
11802                "    LoooooooooooongArgument);\n",
11803                getLLVMStyleWithColumns(20));
11804 }
11805 
11806 TEST_F(FormatTest, IncorrectAccessSpecifier) {
11807   verifyFormat("public:");
11808   verifyFormat("class A {\n"
11809                "public\n"
11810                "  void f() {}\n"
11811                "};");
11812   verifyFormat("public\n"
11813                "int qwerty;");
11814   verifyFormat("public\n"
11815                "B {}");
11816   verifyFormat("public\n"
11817                "{}");
11818   verifyFormat("public\n"
11819                "B { int x; }");
11820 }
11821 
11822 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
11823   verifyFormat("{");
11824   verifyFormat("#})");
11825   verifyNoCrash("(/**/[:!] ?[).");
11826 }
11827 
11828 TEST_F(FormatTest, IncorrectUnbalancedBracesInMacrosWithUnicode) {
11829   // Found by oss-fuzz:
11830   // https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=8212
11831   FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp);
11832   Style.ColumnLimit = 60;
11833   verifyNoCrash(
11834       "\x23\x47\xff\x20\x28\xff\x3c\xff\x3f\xff\x20\x2f\x7b\x7a\xff\x20"
11835       "\xff\xff\xff\xca\xb5\xff\xff\xff\xff\x3a\x7b\x7d\xff\x20\xff\x20"
11836       "\xff\x74\xff\x20\x7d\x7d\xff\x7b\x3a\xff\x20\x71\xff\x20\xff\x0a",
11837       Style);
11838 }
11839 
11840 TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
11841   verifyFormat("do {\n}");
11842   verifyFormat("do {\n}\n"
11843                "f();");
11844   verifyFormat("do {\n}\n"
11845                "wheeee(fun);");
11846   verifyFormat("do {\n"
11847                "  f();\n"
11848                "}");
11849 }
11850 
11851 TEST_F(FormatTest, IncorrectCodeMissingParens) {
11852   verifyFormat("if {\n  foo;\n  foo();\n}");
11853   verifyFormat("switch {\n  foo;\n  foo();\n}");
11854   verifyIncompleteFormat("for {\n  foo;\n  foo();\n}");
11855   verifyFormat("while {\n  foo;\n  foo();\n}");
11856   verifyFormat("do {\n  foo;\n  foo();\n} while;");
11857 }
11858 
11859 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
11860   verifyIncompleteFormat("namespace {\n"
11861                          "class Foo { Foo (\n"
11862                          "};\n"
11863                          "} // namespace");
11864 }
11865 
11866 TEST_F(FormatTest, IncorrectCodeErrorDetection) {
11867   EXPECT_EQ("{\n  {}\n", format("{\n{\n}\n"));
11868   EXPECT_EQ("{\n  {}\n", format("{\n  {\n}\n"));
11869   EXPECT_EQ("{\n  {}\n", format("{\n  {\n  }\n"));
11870   EXPECT_EQ("{\n  {}\n}\n}\n", format("{\n  {\n    }\n  }\n}\n"));
11871 
11872   EXPECT_EQ("{\n"
11873             "  {\n"
11874             "    breakme(\n"
11875             "        qwe);\n"
11876             "  }\n",
11877             format("{\n"
11878                    "    {\n"
11879                    " breakme(qwe);\n"
11880                    "}\n",
11881                    getLLVMStyleWithColumns(10)));
11882 }
11883 
11884 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
11885   verifyFormat("int x = {\n"
11886                "    avariable,\n"
11887                "    b(alongervariable)};",
11888                getLLVMStyleWithColumns(25));
11889 }
11890 
11891 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
11892   verifyFormat("return (a)(b){1, 2, 3};");
11893 }
11894 
11895 TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
11896   verifyFormat("vector<int> x{1, 2, 3, 4};");
11897   verifyFormat("vector<int> x{\n"
11898                "    1,\n"
11899                "    2,\n"
11900                "    3,\n"
11901                "    4,\n"
11902                "};");
11903   verifyFormat("vector<T> x{{}, {}, {}, {}};");
11904   verifyFormat("f({1, 2});");
11905   verifyFormat("auto v = Foo{-1};");
11906   verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
11907   verifyFormat("Class::Class : member{1, 2, 3} {}");
11908   verifyFormat("new vector<int>{1, 2, 3};");
11909   verifyFormat("new int[3]{1, 2, 3};");
11910   verifyFormat("new int{1};");
11911   verifyFormat("return {arg1, arg2};");
11912   verifyFormat("return {arg1, SomeType{parameter}};");
11913   verifyFormat("int count = set<int>{f(), g(), h()}.size();");
11914   verifyFormat("new T{arg1, arg2};");
11915   verifyFormat("f(MyMap[{composite, key}]);");
11916   verifyFormat("class Class {\n"
11917                "  T member = {arg1, arg2};\n"
11918                "};");
11919   verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
11920   verifyFormat("const struct A a = {.a = 1, .b = 2};");
11921   verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
11922   verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
11923   verifyFormat("int a = std::is_integral<int>{} + 0;");
11924 
11925   verifyFormat("int foo(int i) { return fo1{}(i); }");
11926   verifyFormat("int foo(int i) { return fo1{}(i); }");
11927   verifyFormat("auto i = decltype(x){};");
11928   verifyFormat("auto i = typeof(x){};");
11929   verifyFormat("auto i = _Atomic(x){};");
11930   verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
11931   verifyFormat("Node n{1, Node{1000}, //\n"
11932                "       2};");
11933   verifyFormat("Aaaa aaaaaaa{\n"
11934                "    {\n"
11935                "        aaaa,\n"
11936                "    },\n"
11937                "};");
11938   verifyFormat("class C : public D {\n"
11939                "  SomeClass SC{2};\n"
11940                "};");
11941   verifyFormat("class C : public A {\n"
11942                "  class D : public B {\n"
11943                "    void f() { int i{2}; }\n"
11944                "  };\n"
11945                "};");
11946   verifyFormat("#define A {a, a},");
11947   // Don't confuse braced list initializers with compound statements.
11948   verifyFormat(
11949       "class A {\n"
11950       "  A() : a{} {}\n"
11951       "  A(int b) : b(b) {}\n"
11952       "  A(int a, int b) : a(a), bs{{bs...}} { f(); }\n"
11953       "  int a, b;\n"
11954       "  explicit Expr(const Scalar<Result> &x) : u{Constant<Result>{x}} {}\n"
11955       "  explicit Expr(Scalar<Result> &&x) : u{Constant<Result>{std::move(x)}} "
11956       "{}\n"
11957       "};");
11958 
11959   // Avoid breaking between equal sign and opening brace
11960   FormatStyle AvoidBreakingFirstArgument = getLLVMStyle();
11961   AvoidBreakingFirstArgument.PenaltyBreakBeforeFirstCallParameter = 200;
11962   verifyFormat("const std::unordered_map<std::string, int> MyHashTable =\n"
11963                "    {{\"aaaaaaaaaaaaaaaaaaaaa\", 0},\n"
11964                "     {\"bbbbbbbbbbbbbbbbbbbbb\", 1},\n"
11965                "     {\"ccccccccccccccccccccc\", 2}};",
11966                AvoidBreakingFirstArgument);
11967 
11968   // Binpacking only if there is no trailing comma
11969   verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n"
11970                "                      cccccccccc, dddddddddd};",
11971                getLLVMStyleWithColumns(50));
11972   verifyFormat("const Aaaaaa aaaaa = {\n"
11973                "    aaaaaaaaaaa,\n"
11974                "    bbbbbbbbbbb,\n"
11975                "    ccccccccccc,\n"
11976                "    ddddddddddd,\n"
11977                "};",
11978                getLLVMStyleWithColumns(50));
11979 
11980   // Cases where distinguising braced lists and blocks is hard.
11981   verifyFormat("vector<int> v{12} GUARDED_BY(mutex);");
11982   verifyFormat("void f() {\n"
11983                "  return; // comment\n"
11984                "}\n"
11985                "SomeType t;");
11986   verifyFormat("void f() {\n"
11987                "  if (a) {\n"
11988                "    f();\n"
11989                "  }\n"
11990                "}\n"
11991                "SomeType t;");
11992 
11993   // In combination with BinPackArguments = false.
11994   FormatStyle NoBinPacking = getLLVMStyle();
11995   NoBinPacking.BinPackArguments = false;
11996   verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
11997                "                      bbbbb,\n"
11998                "                      ccccc,\n"
11999                "                      ddddd,\n"
12000                "                      eeeee,\n"
12001                "                      ffffff,\n"
12002                "                      ggggg,\n"
12003                "                      hhhhhh,\n"
12004                "                      iiiiii,\n"
12005                "                      jjjjjj,\n"
12006                "                      kkkkkk};",
12007                NoBinPacking);
12008   verifyFormat("const Aaaaaa aaaaa = {\n"
12009                "    aaaaa,\n"
12010                "    bbbbb,\n"
12011                "    ccccc,\n"
12012                "    ddddd,\n"
12013                "    eeeee,\n"
12014                "    ffffff,\n"
12015                "    ggggg,\n"
12016                "    hhhhhh,\n"
12017                "    iiiiii,\n"
12018                "    jjjjjj,\n"
12019                "    kkkkkk,\n"
12020                "};",
12021                NoBinPacking);
12022   verifyFormat(
12023       "const Aaaaaa aaaaa = {\n"
12024       "    aaaaa,  bbbbb,  ccccc,  ddddd,  eeeee,  ffffff, ggggg, hhhhhh,\n"
12025       "    iiiiii, jjjjjj, kkkkkk, aaaaa,  bbbbb,  ccccc,  ddddd, eeeee,\n"
12026       "    ffffff, ggggg,  hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
12027       "};",
12028       NoBinPacking);
12029 
12030   NoBinPacking.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
12031   EXPECT_EQ("static uint8 CddDp83848Reg[] = {\n"
12032             "    CDDDP83848_BMCR_REGISTER,\n"
12033             "    CDDDP83848_BMSR_REGISTER,\n"
12034             "    CDDDP83848_RBR_REGISTER};",
12035             format("static uint8 CddDp83848Reg[] = {CDDDP83848_BMCR_REGISTER,\n"
12036                    "                                CDDDP83848_BMSR_REGISTER,\n"
12037                    "                                CDDDP83848_RBR_REGISTER};",
12038                    NoBinPacking));
12039 
12040   // FIXME: The alignment of these trailing comments might be bad. Then again,
12041   // this might be utterly useless in real code.
12042   verifyFormat("Constructor::Constructor()\n"
12043                "    : some_value{         //\n"
12044                "                 aaaaaaa, //\n"
12045                "                 bbbbbbb} {}");
12046 
12047   // In braced lists, the first comment is always assumed to belong to the
12048   // first element. Thus, it can be moved to the next or previous line as
12049   // appropriate.
12050   EXPECT_EQ("function({// First element:\n"
12051             "          1,\n"
12052             "          // Second element:\n"
12053             "          2});",
12054             format("function({\n"
12055                    "    // First element:\n"
12056                    "    1,\n"
12057                    "    // Second element:\n"
12058                    "    2});"));
12059   EXPECT_EQ("std::vector<int> MyNumbers{\n"
12060             "    // First element:\n"
12061             "    1,\n"
12062             "    // Second element:\n"
12063             "    2};",
12064             format("std::vector<int> MyNumbers{// First element:\n"
12065                    "                           1,\n"
12066                    "                           // Second element:\n"
12067                    "                           2};",
12068                    getLLVMStyleWithColumns(30)));
12069   // A trailing comma should still lead to an enforced line break and no
12070   // binpacking.
12071   EXPECT_EQ("vector<int> SomeVector = {\n"
12072             "    // aaa\n"
12073             "    1,\n"
12074             "    2,\n"
12075             "};",
12076             format("vector<int> SomeVector = { // aaa\n"
12077                    "    1, 2, };"));
12078 
12079   // C++11 brace initializer list l-braces should not be treated any differently
12080   // when breaking before lambda bodies is enabled
12081   FormatStyle BreakBeforeLambdaBody = getLLVMStyle();
12082   BreakBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
12083   BreakBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
12084   BreakBeforeLambdaBody.AlwaysBreakBeforeMultilineStrings = true;
12085   verifyFormat(
12086       "std::runtime_error{\n"
12087       "    \"Long string which will force a break onto the next line...\"};",
12088       BreakBeforeLambdaBody);
12089 
12090   FormatStyle ExtraSpaces = getLLVMStyle();
12091   ExtraSpaces.Cpp11BracedListStyle = false;
12092   ExtraSpaces.ColumnLimit = 75;
12093   verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
12094   verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
12095   verifyFormat("f({ 1, 2 });", ExtraSpaces);
12096   verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
12097   verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
12098   verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
12099   verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
12100   verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
12101   verifyFormat("return { arg1, arg2 };", ExtraSpaces);
12102   verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
12103   verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
12104   verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
12105   verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
12106   verifyFormat("class Class {\n"
12107                "  T member = { arg1, arg2 };\n"
12108                "};",
12109                ExtraSpaces);
12110   verifyFormat(
12111       "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
12112       "                                 aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
12113       "                  : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
12114       "                                 bbbbbbbbbbbbbbbbbbbb, bbbbb };",
12115       ExtraSpaces);
12116   verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
12117   verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
12118                ExtraSpaces);
12119   verifyFormat(
12120       "someFunction(OtherParam,\n"
12121       "             BracedList{ // comment 1 (Forcing interesting break)\n"
12122       "                         param1, param2,\n"
12123       "                         // comment 2\n"
12124       "                         param3, param4 });",
12125       ExtraSpaces);
12126   verifyFormat(
12127       "std::this_thread::sleep_for(\n"
12128       "    std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
12129       ExtraSpaces);
12130   verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n"
12131                "    aaaaaaa,\n"
12132                "    aaaaaaaaaa,\n"
12133                "    aaaaa,\n"
12134                "    aaaaaaaaaaaaaaa,\n"
12135                "    aaa,\n"
12136                "    aaaaaaaaaa,\n"
12137                "    a,\n"
12138                "    aaaaaaaaaaaaaaaaaaaaa,\n"
12139                "    aaaaaaaaaaaa,\n"
12140                "    aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
12141                "    aaaaaaa,\n"
12142                "    a};");
12143   verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
12144   verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces);
12145   verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces);
12146 
12147   // Avoid breaking between initializer/equal sign and opening brace
12148   ExtraSpaces.PenaltyBreakBeforeFirstCallParameter = 200;
12149   verifyFormat("const std::unordered_map<std::string, int> MyHashTable = {\n"
12150                "  { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n"
12151                "  { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n"
12152                "  { \"ccccccccccccccccccccc\", 2 }\n"
12153                "};",
12154                ExtraSpaces);
12155   verifyFormat("const std::unordered_map<std::string, int> MyHashTable{\n"
12156                "  { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n"
12157                "  { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n"
12158                "  { \"ccccccccccccccccccccc\", 2 }\n"
12159                "};",
12160                ExtraSpaces);
12161 
12162   FormatStyle SpaceBeforeBrace = getLLVMStyle();
12163   SpaceBeforeBrace.SpaceBeforeCpp11BracedList = true;
12164   verifyFormat("vector<int> x {1, 2, 3, 4};", SpaceBeforeBrace);
12165   verifyFormat("f({}, {{}, {}}, MyMap[{k, v}]);", SpaceBeforeBrace);
12166 
12167   FormatStyle SpaceBetweenBraces = getLLVMStyle();
12168   SpaceBetweenBraces.SpacesInAngles = FormatStyle::SIAS_Always;
12169   SpaceBetweenBraces.SpacesInParentheses = true;
12170   SpaceBetweenBraces.SpacesInSquareBrackets = true;
12171   verifyFormat("vector< int > x{ 1, 2, 3, 4 };", SpaceBetweenBraces);
12172   verifyFormat("f( {}, { {}, {} }, MyMap[ { k, v } ] );", SpaceBetweenBraces);
12173   verifyFormat("vector< int > x{ // comment 1\n"
12174                "                 1, 2, 3, 4 };",
12175                SpaceBetweenBraces);
12176   SpaceBetweenBraces.ColumnLimit = 20;
12177   EXPECT_EQ("vector< int > x{\n"
12178             "    1, 2, 3, 4 };",
12179             format("vector<int>x{1,2,3,4};", SpaceBetweenBraces));
12180   SpaceBetweenBraces.ColumnLimit = 24;
12181   EXPECT_EQ("vector< int > x{ 1, 2,\n"
12182             "                 3, 4 };",
12183             format("vector<int>x{1,2,3,4};", SpaceBetweenBraces));
12184   EXPECT_EQ("vector< int > x{\n"
12185             "    1,\n"
12186             "    2,\n"
12187             "    3,\n"
12188             "    4,\n"
12189             "};",
12190             format("vector<int>x{1,2,3,4,};", SpaceBetweenBraces));
12191   verifyFormat("vector< int > x{};", SpaceBetweenBraces);
12192   SpaceBetweenBraces.SpaceInEmptyParentheses = true;
12193   verifyFormat("vector< int > x{ };", SpaceBetweenBraces);
12194 }
12195 
12196 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
12197   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
12198                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
12199                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
12200                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
12201                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
12202                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
12203   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n"
12204                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
12205                "                 1, 22, 333, 4444, 55555, //\n"
12206                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
12207                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
12208   verifyFormat(
12209       "vector<int> x = {1,       22, 333, 4444, 55555, 666666, 7777777,\n"
12210       "                 1,       22, 333, 4444, 55555, 666666, 7777777,\n"
12211       "                 1,       22, 333, 4444, 55555, 666666, // comment\n"
12212       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
12213       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
12214       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
12215       "                 7777777};");
12216   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
12217                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
12218                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
12219   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
12220                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
12221                "    // Separating comment.\n"
12222                "    X86::R8, X86::R9, X86::R10, X86::R11, 0};");
12223   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
12224                "    // Leading comment\n"
12225                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
12226                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
12227   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
12228                "                 1, 1, 1, 1};",
12229                getLLVMStyleWithColumns(39));
12230   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
12231                "                 1, 1, 1, 1};",
12232                getLLVMStyleWithColumns(38));
12233   verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
12234                "    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
12235                getLLVMStyleWithColumns(43));
12236   verifyFormat(
12237       "static unsigned SomeValues[10][3] = {\n"
12238       "    {1, 4, 0},  {4, 9, 0},  {4, 5, 9},  {8, 5, 4}, {1, 8, 4},\n"
12239       "    {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};");
12240   verifyFormat("static auto fields = new vector<string>{\n"
12241                "    \"aaaaaaaaaaaaa\",\n"
12242                "    \"aaaaaaaaaaaaa\",\n"
12243                "    \"aaaaaaaaaaaa\",\n"
12244                "    \"aaaaaaaaaaaaaa\",\n"
12245                "    \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
12246                "    \"aaaaaaaaaaaa\",\n"
12247                "    \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
12248                "};");
12249   verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};");
12250   verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n"
12251                "                 2, bbbbbbbbbbbbbbbbbbbbbb,\n"
12252                "                 3, cccccccccccccccccccccc};",
12253                getLLVMStyleWithColumns(60));
12254 
12255   // Trailing commas.
12256   verifyFormat("vector<int> x = {\n"
12257                "    1, 1, 1, 1, 1, 1, 1, 1,\n"
12258                "};",
12259                getLLVMStyleWithColumns(39));
12260   verifyFormat("vector<int> x = {\n"
12261                "    1, 1, 1, 1, 1, 1, 1, 1, //\n"
12262                "};",
12263                getLLVMStyleWithColumns(39));
12264   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
12265                "                 1, 1, 1, 1,\n"
12266                "                 /**/ /**/};",
12267                getLLVMStyleWithColumns(39));
12268 
12269   // Trailing comment in the first line.
12270   verifyFormat("vector<int> iiiiiiiiiiiiiii = {                      //\n"
12271                "    1111111111, 2222222222, 33333333333, 4444444444, //\n"
12272                "    111111111,  222222222,  3333333333,  444444444,  //\n"
12273                "    11111111,   22222222,   333333333,   44444444};");
12274   // Trailing comment in the last line.
12275   verifyFormat("int aaaaa[] = {\n"
12276                "    1, 2, 3, // comment\n"
12277                "    4, 5, 6  // comment\n"
12278                "};");
12279 
12280   // With nested lists, we should either format one item per line or all nested
12281   // lists one on line.
12282   // FIXME: For some nested lists, we can do better.
12283   verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
12284                "        {aaaaaaaaaaaaaaaaaaa},\n"
12285                "        {aaaaaaaaaaaaaaaaaaaaa},\n"
12286                "        {aaaaaaaaaaaaaaaaa}};",
12287                getLLVMStyleWithColumns(60));
12288   verifyFormat(
12289       "SomeStruct my_struct_array = {\n"
12290       "    {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
12291       "     aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
12292       "    {aaa, aaa},\n"
12293       "    {aaa, aaa},\n"
12294       "    {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
12295       "    {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
12296       "     aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
12297 
12298   // No column layout should be used here.
12299   verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
12300                "                   bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
12301 
12302   verifyNoCrash("a<,");
12303 
12304   // No braced initializer here.
12305   verifyFormat("void f() {\n"
12306                "  struct Dummy {};\n"
12307                "  f(v);\n"
12308                "}");
12309 
12310   // Long lists should be formatted in columns even if they are nested.
12311   verifyFormat(
12312       "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n"
12313       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
12314       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
12315       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
12316       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
12317       "                          1, 22, 333, 4444, 55555, 666666, 7777777});");
12318 
12319   // Allow "single-column" layout even if that violates the column limit. There
12320   // isn't going to be a better way.
12321   verifyFormat("std::vector<int> a = {\n"
12322                "    aaaaaaaa,\n"
12323                "    aaaaaaaa,\n"
12324                "    aaaaaaaa,\n"
12325                "    aaaaaaaa,\n"
12326                "    aaaaaaaaaa,\n"
12327                "    aaaaaaaa,\n"
12328                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa};",
12329                getLLVMStyleWithColumns(30));
12330   verifyFormat("vector<int> aaaa = {\n"
12331                "    aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
12332                "    aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
12333                "    aaaaaa.aaaaaaa,\n"
12334                "    aaaaaa.aaaaaaa,\n"
12335                "    aaaaaa.aaaaaaa,\n"
12336                "    aaaaaa.aaaaaaa,\n"
12337                "};");
12338 
12339   // Don't create hanging lists.
12340   verifyFormat("someFunction(Param, {List1, List2,\n"
12341                "                     List3});",
12342                getLLVMStyleWithColumns(35));
12343   verifyFormat("someFunction(Param, Param,\n"
12344                "             {List1, List2,\n"
12345                "              List3});",
12346                getLLVMStyleWithColumns(35));
12347   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n"
12348                "                               aaaaaaaaaaaaaaaaaaaaaaa);");
12349 }
12350 
12351 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
12352   FormatStyle DoNotMerge = getLLVMStyle();
12353   DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
12354 
12355   verifyFormat("void f() { return 42; }");
12356   verifyFormat("void f() {\n"
12357                "  return 42;\n"
12358                "}",
12359                DoNotMerge);
12360   verifyFormat("void f() {\n"
12361                "  // Comment\n"
12362                "}");
12363   verifyFormat("{\n"
12364                "#error {\n"
12365                "  int a;\n"
12366                "}");
12367   verifyFormat("{\n"
12368                "  int a;\n"
12369                "#error {\n"
12370                "}");
12371   verifyFormat("void f() {} // comment");
12372   verifyFormat("void f() { int a; } // comment");
12373   verifyFormat("void f() {\n"
12374                "} // comment",
12375                DoNotMerge);
12376   verifyFormat("void f() {\n"
12377                "  int a;\n"
12378                "} // comment",
12379                DoNotMerge);
12380   verifyFormat("void f() {\n"
12381                "} // comment",
12382                getLLVMStyleWithColumns(15));
12383 
12384   verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
12385   verifyFormat("void f() {\n  return 42;\n}", getLLVMStyleWithColumns(22));
12386 
12387   verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
12388   verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
12389   verifyFormat("class C {\n"
12390                "  C()\n"
12391                "      : iiiiiiii(nullptr),\n"
12392                "        kkkkkkk(nullptr),\n"
12393                "        mmmmmmm(nullptr),\n"
12394                "        nnnnnnn(nullptr) {}\n"
12395                "};",
12396                getGoogleStyle());
12397 
12398   FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0);
12399   EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit));
12400   EXPECT_EQ("class C {\n"
12401             "  A() : b(0) {}\n"
12402             "};",
12403             format("class C{A():b(0){}};", NoColumnLimit));
12404   EXPECT_EQ("A()\n"
12405             "    : b(0) {\n"
12406             "}",
12407             format("A()\n:b(0)\n{\n}", NoColumnLimit));
12408 
12409   FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
12410   DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
12411       FormatStyle::SFS_None;
12412   EXPECT_EQ("A()\n"
12413             "    : b(0) {\n"
12414             "}",
12415             format("A():b(0){}", DoNotMergeNoColumnLimit));
12416   EXPECT_EQ("A()\n"
12417             "    : b(0) {\n"
12418             "}",
12419             format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit));
12420 
12421   verifyFormat("#define A          \\\n"
12422                "  void f() {       \\\n"
12423                "    int i;         \\\n"
12424                "  }",
12425                getLLVMStyleWithColumns(20));
12426   verifyFormat("#define A           \\\n"
12427                "  void f() { int i; }",
12428                getLLVMStyleWithColumns(21));
12429   verifyFormat("#define A            \\\n"
12430                "  void f() {         \\\n"
12431                "    int i;           \\\n"
12432                "  }                  \\\n"
12433                "  int j;",
12434                getLLVMStyleWithColumns(22));
12435   verifyFormat("#define A             \\\n"
12436                "  void f() { int i; } \\\n"
12437                "  int j;",
12438                getLLVMStyleWithColumns(23));
12439 }
12440 
12441 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) {
12442   FormatStyle MergeEmptyOnly = getLLVMStyle();
12443   MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
12444   verifyFormat("class C {\n"
12445                "  int f() {}\n"
12446                "};",
12447                MergeEmptyOnly);
12448   verifyFormat("class C {\n"
12449                "  int f() {\n"
12450                "    return 42;\n"
12451                "  }\n"
12452                "};",
12453                MergeEmptyOnly);
12454   verifyFormat("int f() {}", MergeEmptyOnly);
12455   verifyFormat("int f() {\n"
12456                "  return 42;\n"
12457                "}",
12458                MergeEmptyOnly);
12459 
12460   // Also verify behavior when BraceWrapping.AfterFunction = true
12461   MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
12462   MergeEmptyOnly.BraceWrapping.AfterFunction = true;
12463   verifyFormat("int f() {}", MergeEmptyOnly);
12464   verifyFormat("class C {\n"
12465                "  int f() {}\n"
12466                "};",
12467                MergeEmptyOnly);
12468 }
12469 
12470 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
12471   FormatStyle MergeInlineOnly = getLLVMStyle();
12472   MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
12473   verifyFormat("class C {\n"
12474                "  int f() { return 42; }\n"
12475                "};",
12476                MergeInlineOnly);
12477   verifyFormat("int f() {\n"
12478                "  return 42;\n"
12479                "}",
12480                MergeInlineOnly);
12481 
12482   // SFS_Inline implies SFS_Empty
12483   verifyFormat("class C {\n"
12484                "  int f() {}\n"
12485                "};",
12486                MergeInlineOnly);
12487   verifyFormat("int f() {}", MergeInlineOnly);
12488 
12489   // Also verify behavior when BraceWrapping.AfterFunction = true
12490   MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
12491   MergeInlineOnly.BraceWrapping.AfterFunction = true;
12492   verifyFormat("class C {\n"
12493                "  int f() { return 42; }\n"
12494                "};",
12495                MergeInlineOnly);
12496   verifyFormat("int f()\n"
12497                "{\n"
12498                "  return 42;\n"
12499                "}",
12500                MergeInlineOnly);
12501 
12502   // SFS_Inline implies SFS_Empty
12503   verifyFormat("int f() {}", MergeInlineOnly);
12504   verifyFormat("class C {\n"
12505                "  int f() {}\n"
12506                "};",
12507                MergeInlineOnly);
12508 
12509   MergeInlineOnly.BraceWrapping.AfterClass = true;
12510   MergeInlineOnly.BraceWrapping.AfterStruct = true;
12511   verifyFormat("class C\n"
12512                "{\n"
12513                "  int f() { return 42; }\n"
12514                "};",
12515                MergeInlineOnly);
12516   verifyFormat("struct C\n"
12517                "{\n"
12518                "  int f() { return 42; }\n"
12519                "};",
12520                MergeInlineOnly);
12521   verifyFormat("int f()\n"
12522                "{\n"
12523                "  return 42;\n"
12524                "}",
12525                MergeInlineOnly);
12526   verifyFormat("int f() {}", MergeInlineOnly);
12527   verifyFormat("class C\n"
12528                "{\n"
12529                "  int f() { return 42; }\n"
12530                "};",
12531                MergeInlineOnly);
12532   verifyFormat("struct C\n"
12533                "{\n"
12534                "  int f() { return 42; }\n"
12535                "};",
12536                MergeInlineOnly);
12537   verifyFormat("struct C\n"
12538                "// comment\n"
12539                "/* comment */\n"
12540                "// comment\n"
12541                "{\n"
12542                "  int f() { return 42; }\n"
12543                "};",
12544                MergeInlineOnly);
12545   verifyFormat("/* comment */ struct C\n"
12546                "{\n"
12547                "  int f() { return 42; }\n"
12548                "};",
12549                MergeInlineOnly);
12550 }
12551 
12552 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) {
12553   FormatStyle MergeInlineOnly = getLLVMStyle();
12554   MergeInlineOnly.AllowShortFunctionsOnASingleLine =
12555       FormatStyle::SFS_InlineOnly;
12556   verifyFormat("class C {\n"
12557                "  int f() { return 42; }\n"
12558                "};",
12559                MergeInlineOnly);
12560   verifyFormat("int f() {\n"
12561                "  return 42;\n"
12562                "}",
12563                MergeInlineOnly);
12564 
12565   // SFS_InlineOnly does not imply SFS_Empty
12566   verifyFormat("class C {\n"
12567                "  int f() {}\n"
12568                "};",
12569                MergeInlineOnly);
12570   verifyFormat("int f() {\n"
12571                "}",
12572                MergeInlineOnly);
12573 
12574   // Also verify behavior when BraceWrapping.AfterFunction = true
12575   MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
12576   MergeInlineOnly.BraceWrapping.AfterFunction = true;
12577   verifyFormat("class C {\n"
12578                "  int f() { return 42; }\n"
12579                "};",
12580                MergeInlineOnly);
12581   verifyFormat("int f()\n"
12582                "{\n"
12583                "  return 42;\n"
12584                "}",
12585                MergeInlineOnly);
12586 
12587   // SFS_InlineOnly does not imply SFS_Empty
12588   verifyFormat("int f()\n"
12589                "{\n"
12590                "}",
12591                MergeInlineOnly);
12592   verifyFormat("class C {\n"
12593                "  int f() {}\n"
12594                "};",
12595                MergeInlineOnly);
12596 }
12597 
12598 TEST_F(FormatTest, SplitEmptyFunction) {
12599   FormatStyle Style = getLLVMStyleWithColumns(40);
12600   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
12601   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12602   Style.BraceWrapping.AfterFunction = true;
12603   Style.BraceWrapping.SplitEmptyFunction = false;
12604 
12605   verifyFormat("int f()\n"
12606                "{}",
12607                Style);
12608   verifyFormat("int f()\n"
12609                "{\n"
12610                "  return 42;\n"
12611                "}",
12612                Style);
12613   verifyFormat("int f()\n"
12614                "{\n"
12615                "  // some comment\n"
12616                "}",
12617                Style);
12618 
12619   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
12620   verifyFormat("int f() {}", Style);
12621   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
12622                "{}",
12623                Style);
12624   verifyFormat("int f()\n"
12625                "{\n"
12626                "  return 0;\n"
12627                "}",
12628                Style);
12629 
12630   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
12631   verifyFormat("class Foo {\n"
12632                "  int f() {}\n"
12633                "};\n",
12634                Style);
12635   verifyFormat("class Foo {\n"
12636                "  int f() { return 0; }\n"
12637                "};\n",
12638                Style);
12639   verifyFormat("class Foo {\n"
12640                "  int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
12641                "  {}\n"
12642                "};\n",
12643                Style);
12644   verifyFormat("class Foo {\n"
12645                "  int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
12646                "  {\n"
12647                "    return 0;\n"
12648                "  }\n"
12649                "};\n",
12650                Style);
12651 
12652   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
12653   verifyFormat("int f() {}", Style);
12654   verifyFormat("int f() { return 0; }", Style);
12655   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
12656                "{}",
12657                Style);
12658   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
12659                "{\n"
12660                "  return 0;\n"
12661                "}",
12662                Style);
12663 }
12664 
12665 TEST_F(FormatTest, SplitEmptyFunctionButNotRecord) {
12666   FormatStyle Style = getLLVMStyleWithColumns(40);
12667   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
12668   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12669   Style.BraceWrapping.AfterFunction = true;
12670   Style.BraceWrapping.SplitEmptyFunction = true;
12671   Style.BraceWrapping.SplitEmptyRecord = false;
12672 
12673   verifyFormat("class C {};", Style);
12674   verifyFormat("struct C {};", Style);
12675   verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
12676                "       int bbbbbbbbbbbbbbbbbbbbbbbb)\n"
12677                "{\n"
12678                "}",
12679                Style);
12680   verifyFormat("class C {\n"
12681                "  C()\n"
12682                "      : aaaaaaaaaaaaaaaaaaaaaaaaaaaa(),\n"
12683                "        bbbbbbbbbbbbbbbbbbb()\n"
12684                "  {\n"
12685                "  }\n"
12686                "  void\n"
12687                "  m(int aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
12688                "    int bbbbbbbbbbbbbbbbbbbbbbbb)\n"
12689                "  {\n"
12690                "  }\n"
12691                "};",
12692                Style);
12693 }
12694 
12695 TEST_F(FormatTest, KeepShortFunctionAfterPPElse) {
12696   FormatStyle Style = getLLVMStyle();
12697   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
12698   verifyFormat("#ifdef A\n"
12699                "int f() {}\n"
12700                "#else\n"
12701                "int g() {}\n"
12702                "#endif",
12703                Style);
12704 }
12705 
12706 TEST_F(FormatTest, SplitEmptyClass) {
12707   FormatStyle Style = getLLVMStyle();
12708   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12709   Style.BraceWrapping.AfterClass = true;
12710   Style.BraceWrapping.SplitEmptyRecord = false;
12711 
12712   verifyFormat("class Foo\n"
12713                "{};",
12714                Style);
12715   verifyFormat("/* something */ class Foo\n"
12716                "{};",
12717                Style);
12718   verifyFormat("template <typename X> class Foo\n"
12719                "{};",
12720                Style);
12721   verifyFormat("class Foo\n"
12722                "{\n"
12723                "  Foo();\n"
12724                "};",
12725                Style);
12726   verifyFormat("typedef class Foo\n"
12727                "{\n"
12728                "} Foo_t;",
12729                Style);
12730 
12731   Style.BraceWrapping.SplitEmptyRecord = true;
12732   Style.BraceWrapping.AfterStruct = true;
12733   verifyFormat("class rep\n"
12734                "{\n"
12735                "};",
12736                Style);
12737   verifyFormat("struct rep\n"
12738                "{\n"
12739                "};",
12740                Style);
12741   verifyFormat("template <typename T> class rep\n"
12742                "{\n"
12743                "};",
12744                Style);
12745   verifyFormat("template <typename T> struct rep\n"
12746                "{\n"
12747                "};",
12748                Style);
12749   verifyFormat("class rep\n"
12750                "{\n"
12751                "  int x;\n"
12752                "};",
12753                Style);
12754   verifyFormat("struct rep\n"
12755                "{\n"
12756                "  int x;\n"
12757                "};",
12758                Style);
12759   verifyFormat("template <typename T> class rep\n"
12760                "{\n"
12761                "  int x;\n"
12762                "};",
12763                Style);
12764   verifyFormat("template <typename T> struct rep\n"
12765                "{\n"
12766                "  int x;\n"
12767                "};",
12768                Style);
12769   verifyFormat("template <typename T> class rep // Foo\n"
12770                "{\n"
12771                "  int x;\n"
12772                "};",
12773                Style);
12774   verifyFormat("template <typename T> struct rep // Bar\n"
12775                "{\n"
12776                "  int x;\n"
12777                "};",
12778                Style);
12779 
12780   verifyFormat("template <typename T> class rep<T>\n"
12781                "{\n"
12782                "  int x;\n"
12783                "};",
12784                Style);
12785 
12786   verifyFormat("template <typename T> class rep<std::complex<T>>\n"
12787                "{\n"
12788                "  int x;\n"
12789                "};",
12790                Style);
12791   verifyFormat("template <typename T> class rep<std::complex<T>>\n"
12792                "{\n"
12793                "};",
12794                Style);
12795 
12796   verifyFormat("#include \"stdint.h\"\n"
12797                "namespace rep {}",
12798                Style);
12799   verifyFormat("#include <stdint.h>\n"
12800                "namespace rep {}",
12801                Style);
12802   verifyFormat("#include <stdint.h>\n"
12803                "namespace rep {}",
12804                "#include <stdint.h>\n"
12805                "namespace rep {\n"
12806                "\n"
12807                "\n"
12808                "}",
12809                Style);
12810 }
12811 
12812 TEST_F(FormatTest, SplitEmptyStruct) {
12813   FormatStyle Style = getLLVMStyle();
12814   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12815   Style.BraceWrapping.AfterStruct = true;
12816   Style.BraceWrapping.SplitEmptyRecord = false;
12817 
12818   verifyFormat("struct Foo\n"
12819                "{};",
12820                Style);
12821   verifyFormat("/* something */ struct Foo\n"
12822                "{};",
12823                Style);
12824   verifyFormat("template <typename X> struct Foo\n"
12825                "{};",
12826                Style);
12827   verifyFormat("struct Foo\n"
12828                "{\n"
12829                "  Foo();\n"
12830                "};",
12831                Style);
12832   verifyFormat("typedef struct Foo\n"
12833                "{\n"
12834                "} Foo_t;",
12835                Style);
12836   // typedef struct Bar {} Bar_t;
12837 }
12838 
12839 TEST_F(FormatTest, SplitEmptyUnion) {
12840   FormatStyle Style = getLLVMStyle();
12841   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12842   Style.BraceWrapping.AfterUnion = true;
12843   Style.BraceWrapping.SplitEmptyRecord = false;
12844 
12845   verifyFormat("union Foo\n"
12846                "{};",
12847                Style);
12848   verifyFormat("/* something */ union Foo\n"
12849                "{};",
12850                Style);
12851   verifyFormat("union Foo\n"
12852                "{\n"
12853                "  A,\n"
12854                "};",
12855                Style);
12856   verifyFormat("typedef union Foo\n"
12857                "{\n"
12858                "} Foo_t;",
12859                Style);
12860 }
12861 
12862 TEST_F(FormatTest, SplitEmptyNamespace) {
12863   FormatStyle Style = getLLVMStyle();
12864   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12865   Style.BraceWrapping.AfterNamespace = true;
12866   Style.BraceWrapping.SplitEmptyNamespace = false;
12867 
12868   verifyFormat("namespace Foo\n"
12869                "{};",
12870                Style);
12871   verifyFormat("/* something */ namespace Foo\n"
12872                "{};",
12873                Style);
12874   verifyFormat("inline namespace Foo\n"
12875                "{};",
12876                Style);
12877   verifyFormat("/* something */ inline namespace Foo\n"
12878                "{};",
12879                Style);
12880   verifyFormat("export namespace Foo\n"
12881                "{};",
12882                Style);
12883   verifyFormat("namespace Foo\n"
12884                "{\n"
12885                "void Bar();\n"
12886                "};",
12887                Style);
12888 }
12889 
12890 TEST_F(FormatTest, NeverMergeShortRecords) {
12891   FormatStyle Style = getLLVMStyle();
12892 
12893   verifyFormat("class Foo {\n"
12894                "  Foo();\n"
12895                "};",
12896                Style);
12897   verifyFormat("typedef class Foo {\n"
12898                "  Foo();\n"
12899                "} Foo_t;",
12900                Style);
12901   verifyFormat("struct Foo {\n"
12902                "  Foo();\n"
12903                "};",
12904                Style);
12905   verifyFormat("typedef struct Foo {\n"
12906                "  Foo();\n"
12907                "} Foo_t;",
12908                Style);
12909   verifyFormat("union Foo {\n"
12910                "  A,\n"
12911                "};",
12912                Style);
12913   verifyFormat("typedef union Foo {\n"
12914                "  A,\n"
12915                "} Foo_t;",
12916                Style);
12917   verifyFormat("namespace Foo {\n"
12918                "void Bar();\n"
12919                "};",
12920                Style);
12921 
12922   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12923   Style.BraceWrapping.AfterClass = true;
12924   Style.BraceWrapping.AfterStruct = true;
12925   Style.BraceWrapping.AfterUnion = true;
12926   Style.BraceWrapping.AfterNamespace = true;
12927   verifyFormat("class Foo\n"
12928                "{\n"
12929                "  Foo();\n"
12930                "};",
12931                Style);
12932   verifyFormat("typedef class Foo\n"
12933                "{\n"
12934                "  Foo();\n"
12935                "} Foo_t;",
12936                Style);
12937   verifyFormat("struct Foo\n"
12938                "{\n"
12939                "  Foo();\n"
12940                "};",
12941                Style);
12942   verifyFormat("typedef struct Foo\n"
12943                "{\n"
12944                "  Foo();\n"
12945                "} Foo_t;",
12946                Style);
12947   verifyFormat("union Foo\n"
12948                "{\n"
12949                "  A,\n"
12950                "};",
12951                Style);
12952   verifyFormat("typedef union Foo\n"
12953                "{\n"
12954                "  A,\n"
12955                "} Foo_t;",
12956                Style);
12957   verifyFormat("namespace Foo\n"
12958                "{\n"
12959                "void Bar();\n"
12960                "};",
12961                Style);
12962 }
12963 
12964 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
12965   // Elaborate type variable declarations.
12966   verifyFormat("struct foo a = {bar};\nint n;");
12967   verifyFormat("class foo a = {bar};\nint n;");
12968   verifyFormat("union foo a = {bar};\nint n;");
12969 
12970   // Elaborate types inside function definitions.
12971   verifyFormat("struct foo f() {}\nint n;");
12972   verifyFormat("class foo f() {}\nint n;");
12973   verifyFormat("union foo f() {}\nint n;");
12974 
12975   // Templates.
12976   verifyFormat("template <class X> void f() {}\nint n;");
12977   verifyFormat("template <struct X> void f() {}\nint n;");
12978   verifyFormat("template <union X> void f() {}\nint n;");
12979 
12980   // Actual definitions...
12981   verifyFormat("struct {\n} n;");
12982   verifyFormat(
12983       "template <template <class T, class Y>, class Z> class X {\n} n;");
12984   verifyFormat("union Z {\n  int n;\n} x;");
12985   verifyFormat("class MACRO Z {\n} n;");
12986   verifyFormat("class MACRO(X) Z {\n} n;");
12987   verifyFormat("class __attribute__(X) Z {\n} n;");
12988   verifyFormat("class __declspec(X) Z {\n} n;");
12989   verifyFormat("class A##B##C {\n} n;");
12990   verifyFormat("class alignas(16) Z {\n} n;");
12991   verifyFormat("class MACRO(X) alignas(16) Z {\n} n;");
12992   verifyFormat("class MACROA MACRO(X) Z {\n} n;");
12993 
12994   // Redefinition from nested context:
12995   verifyFormat("class A::B::C {\n} n;");
12996 
12997   // Template definitions.
12998   verifyFormat(
12999       "template <typename F>\n"
13000       "Matcher(const Matcher<F> &Other,\n"
13001       "        typename enable_if_c<is_base_of<F, T>::value &&\n"
13002       "                             !is_same<F, T>::value>::type * = 0)\n"
13003       "    : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
13004 
13005   // FIXME: This is still incorrectly handled at the formatter side.
13006   verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
13007   verifyFormat("int i = SomeFunction(a<b, a> b);");
13008 
13009   // FIXME:
13010   // This now gets parsed incorrectly as class definition.
13011   // verifyFormat("class A<int> f() {\n}\nint n;");
13012 
13013   // Elaborate types where incorrectly parsing the structural element would
13014   // break the indent.
13015   verifyFormat("if (true)\n"
13016                "  class X x;\n"
13017                "else\n"
13018                "  f();\n");
13019 
13020   // This is simply incomplete. Formatting is not important, but must not crash.
13021   verifyFormat("class A:");
13022 }
13023 
13024 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
13025   EXPECT_EQ("#error Leave     all         white!!!!! space* alone!\n",
13026             format("#error Leave     all         white!!!!! space* alone!\n"));
13027   EXPECT_EQ(
13028       "#warning Leave     all         white!!!!! space* alone!\n",
13029       format("#warning Leave     all         white!!!!! space* alone!\n"));
13030   EXPECT_EQ("#error 1", format("  #  error   1"));
13031   EXPECT_EQ("#warning 1", format("  #  warning 1"));
13032 }
13033 
13034 TEST_F(FormatTest, FormatHashIfExpressions) {
13035   verifyFormat("#if AAAA && BBBB");
13036   verifyFormat("#if (AAAA && BBBB)");
13037   verifyFormat("#elif (AAAA && BBBB)");
13038   // FIXME: Come up with a better indentation for #elif.
13039   verifyFormat(
13040       "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) &&  \\\n"
13041       "    defined(BBBBBBBB)\n"
13042       "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) &&  \\\n"
13043       "    defined(BBBBBBBB)\n"
13044       "#endif",
13045       getLLVMStyleWithColumns(65));
13046 }
13047 
13048 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
13049   FormatStyle AllowsMergedIf = getGoogleStyle();
13050   AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
13051       FormatStyle::SIS_WithoutElse;
13052   verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
13053   verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
13054   verifyFormat("if (true)\n#error E\n  return 42;", AllowsMergedIf);
13055   EXPECT_EQ("if (true) return 42;",
13056             format("if (true)\nreturn 42;", AllowsMergedIf));
13057   FormatStyle ShortMergedIf = AllowsMergedIf;
13058   ShortMergedIf.ColumnLimit = 25;
13059   verifyFormat("#define A \\\n"
13060                "  if (true) return 42;",
13061                ShortMergedIf);
13062   verifyFormat("#define A \\\n"
13063                "  f();    \\\n"
13064                "  if (true)\n"
13065                "#define B",
13066                ShortMergedIf);
13067   verifyFormat("#define A \\\n"
13068                "  f();    \\\n"
13069                "  if (true)\n"
13070                "g();",
13071                ShortMergedIf);
13072   verifyFormat("{\n"
13073                "#ifdef A\n"
13074                "  // Comment\n"
13075                "  if (true) continue;\n"
13076                "#endif\n"
13077                "  // Comment\n"
13078                "  if (true) continue;\n"
13079                "}",
13080                ShortMergedIf);
13081   ShortMergedIf.ColumnLimit = 33;
13082   verifyFormat("#define A \\\n"
13083                "  if constexpr (true) return 42;",
13084                ShortMergedIf);
13085   verifyFormat("#define A \\\n"
13086                "  if CONSTEXPR (true) return 42;",
13087                ShortMergedIf);
13088   ShortMergedIf.ColumnLimit = 29;
13089   verifyFormat("#define A                   \\\n"
13090                "  if (aaaaaaaaaa) return 1; \\\n"
13091                "  return 2;",
13092                ShortMergedIf);
13093   ShortMergedIf.ColumnLimit = 28;
13094   verifyFormat("#define A         \\\n"
13095                "  if (aaaaaaaaaa) \\\n"
13096                "    return 1;     \\\n"
13097                "  return 2;",
13098                ShortMergedIf);
13099   verifyFormat("#define A                \\\n"
13100                "  if constexpr (aaaaaaa) \\\n"
13101                "    return 1;            \\\n"
13102                "  return 2;",
13103                ShortMergedIf);
13104   verifyFormat("#define A                \\\n"
13105                "  if CONSTEXPR (aaaaaaa) \\\n"
13106                "    return 1;            \\\n"
13107                "  return 2;",
13108                ShortMergedIf);
13109 }
13110 
13111 TEST_F(FormatTest, FormatStarDependingOnContext) {
13112   verifyFormat("void f(int *a);");
13113   verifyFormat("void f() { f(fint * b); }");
13114   verifyFormat("class A {\n  void f(int *a);\n};");
13115   verifyFormat("class A {\n  int *a;\n};");
13116   verifyFormat("namespace a {\n"
13117                "namespace b {\n"
13118                "class A {\n"
13119                "  void f() {}\n"
13120                "  int *a;\n"
13121                "};\n"
13122                "} // namespace b\n"
13123                "} // namespace a");
13124 }
13125 
13126 TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
13127   verifyFormat("while");
13128   verifyFormat("operator");
13129 }
13130 
13131 TEST_F(FormatTest, SkipsDeeplyNestedLines) {
13132   // This code would be painfully slow to format if we didn't skip it.
13133   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
13134                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
13135                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
13136                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
13137                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
13138                    "A(1, 1)\n"
13139                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x
13140                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
13141                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
13142                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
13143                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
13144                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
13145                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
13146                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
13147                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
13148                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n");
13149   // Deeply nested part is untouched, rest is formatted.
13150   EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n",
13151             format(std::string("int    i;\n") + Code + "int    j;\n",
13152                    getLLVMStyle(), SC_ExpectIncomplete));
13153 }
13154 
13155 //===----------------------------------------------------------------------===//
13156 // Objective-C tests.
13157 //===----------------------------------------------------------------------===//
13158 
13159 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
13160   verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
13161   EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
13162             format("-(NSUInteger)indexOfObject:(id)anObject;"));
13163   EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
13164   EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
13165   EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
13166             format("-(NSInteger)Method3:(id)anObject;"));
13167   EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
13168             format("-(NSInteger)Method4:(id)anObject;"));
13169   EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
13170             format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
13171   EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
13172             format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
13173   EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject "
13174             "forAllCells:(BOOL)flag;",
13175             format("- (void)sendAction:(SEL)aSelector to:(id)anObject "
13176                    "forAllCells:(BOOL)flag;"));
13177 
13178   // Very long objectiveC method declaration.
13179   verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
13180                "    (SoooooooooooooooooooooomeType *)bbbbbbbbbb;");
13181   verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
13182                "                    inRange:(NSRange)range\n"
13183                "                   outRange:(NSRange)out_range\n"
13184                "                  outRange1:(NSRange)out_range1\n"
13185                "                  outRange2:(NSRange)out_range2\n"
13186                "                  outRange3:(NSRange)out_range3\n"
13187                "                  outRange4:(NSRange)out_range4\n"
13188                "                  outRange5:(NSRange)out_range5\n"
13189                "                  outRange6:(NSRange)out_range6\n"
13190                "                  outRange7:(NSRange)out_range7\n"
13191                "                  outRange8:(NSRange)out_range8\n"
13192                "                  outRange9:(NSRange)out_range9;");
13193 
13194   // When the function name has to be wrapped.
13195   FormatStyle Style = getLLVMStyle();
13196   // ObjC ignores IndentWrappedFunctionNames when wrapping methods
13197   // and always indents instead.
13198   Style.IndentWrappedFunctionNames = false;
13199   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
13200                "    veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
13201                "               anotherName:(NSString)bbbbbbbbbbbbbb {\n"
13202                "}",
13203                Style);
13204   Style.IndentWrappedFunctionNames = true;
13205   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
13206                "    veryLooooooooooongName:(NSString)cccccccccccccc\n"
13207                "               anotherName:(NSString)dddddddddddddd {\n"
13208                "}",
13209                Style);
13210 
13211   verifyFormat("- (int)sum:(vector<int>)numbers;");
13212   verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
13213   // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
13214   // protocol lists (but not for template classes):
13215   // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
13216 
13217   verifyFormat("- (int (*)())foo:(int (*)())f;");
13218   verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
13219 
13220   // If there's no return type (very rare in practice!), LLVM and Google style
13221   // agree.
13222   verifyFormat("- foo;");
13223   verifyFormat("- foo:(int)f;");
13224   verifyGoogleFormat("- foo:(int)foo;");
13225 }
13226 
13227 TEST_F(FormatTest, BreaksStringLiterals) {
13228   EXPECT_EQ("\"some text \"\n"
13229             "\"other\";",
13230             format("\"some text other\";", getLLVMStyleWithColumns(12)));
13231   EXPECT_EQ("\"some text \"\n"
13232             "\"other\";",
13233             format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
13234   EXPECT_EQ(
13235       "#define A  \\\n"
13236       "  \"some \"  \\\n"
13237       "  \"text \"  \\\n"
13238       "  \"other\";",
13239       format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
13240   EXPECT_EQ(
13241       "#define A  \\\n"
13242       "  \"so \"    \\\n"
13243       "  \"text \"  \\\n"
13244       "  \"other\";",
13245       format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
13246 
13247   EXPECT_EQ("\"some text\"",
13248             format("\"some text\"", getLLVMStyleWithColumns(1)));
13249   EXPECT_EQ("\"some text\"",
13250             format("\"some text\"", getLLVMStyleWithColumns(11)));
13251   EXPECT_EQ("\"some \"\n"
13252             "\"text\"",
13253             format("\"some text\"", getLLVMStyleWithColumns(10)));
13254   EXPECT_EQ("\"some \"\n"
13255             "\"text\"",
13256             format("\"some text\"", getLLVMStyleWithColumns(7)));
13257   EXPECT_EQ("\"some\"\n"
13258             "\" tex\"\n"
13259             "\"t\"",
13260             format("\"some text\"", getLLVMStyleWithColumns(6)));
13261   EXPECT_EQ("\"some\"\n"
13262             "\" tex\"\n"
13263             "\" and\"",
13264             format("\"some tex and\"", getLLVMStyleWithColumns(6)));
13265   EXPECT_EQ("\"some\"\n"
13266             "\"/tex\"\n"
13267             "\"/and\"",
13268             format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
13269 
13270   EXPECT_EQ("variable =\n"
13271             "    \"long string \"\n"
13272             "    \"literal\";",
13273             format("variable = \"long string literal\";",
13274                    getLLVMStyleWithColumns(20)));
13275 
13276   EXPECT_EQ("variable = f(\n"
13277             "    \"long string \"\n"
13278             "    \"literal\",\n"
13279             "    short,\n"
13280             "    loooooooooooooooooooong);",
13281             format("variable = f(\"long string literal\", short, "
13282                    "loooooooooooooooooooong);",
13283                    getLLVMStyleWithColumns(20)));
13284 
13285   EXPECT_EQ(
13286       "f(g(\"long string \"\n"
13287       "    \"literal\"),\n"
13288       "  b);",
13289       format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20)));
13290   EXPECT_EQ("f(g(\"long string \"\n"
13291             "    \"literal\",\n"
13292             "    a),\n"
13293             "  b);",
13294             format("f(g(\"long string literal\", a), b);",
13295                    getLLVMStyleWithColumns(20)));
13296   EXPECT_EQ(
13297       "f(\"one two\".split(\n"
13298       "    variable));",
13299       format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
13300   EXPECT_EQ("f(\"one two three four five six \"\n"
13301             "  \"seven\".split(\n"
13302             "      really_looooong_variable));",
13303             format("f(\"one two three four five six seven\"."
13304                    "split(really_looooong_variable));",
13305                    getLLVMStyleWithColumns(33)));
13306 
13307   EXPECT_EQ("f(\"some \"\n"
13308             "  \"text\",\n"
13309             "  other);",
13310             format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
13311 
13312   // Only break as a last resort.
13313   verifyFormat(
13314       "aaaaaaaaaaaaaaaaaaaa(\n"
13315       "    aaaaaaaaaaaaaaaaaaaa,\n"
13316       "    aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
13317 
13318   EXPECT_EQ("\"splitmea\"\n"
13319             "\"trandomp\"\n"
13320             "\"oint\"",
13321             format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
13322 
13323   EXPECT_EQ("\"split/\"\n"
13324             "\"pathat/\"\n"
13325             "\"slashes\"",
13326             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
13327 
13328   EXPECT_EQ("\"split/\"\n"
13329             "\"pathat/\"\n"
13330             "\"slashes\"",
13331             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
13332   EXPECT_EQ("\"split at \"\n"
13333             "\"spaces/at/\"\n"
13334             "\"slashes.at.any$\"\n"
13335             "\"non-alphanumeric%\"\n"
13336             "\"1111111111characte\"\n"
13337             "\"rs\"",
13338             format("\"split at "
13339                    "spaces/at/"
13340                    "slashes.at."
13341                    "any$non-"
13342                    "alphanumeric%"
13343                    "1111111111characte"
13344                    "rs\"",
13345                    getLLVMStyleWithColumns(20)));
13346 
13347   // Verify that splitting the strings understands
13348   // Style::AlwaysBreakBeforeMultilineStrings.
13349   EXPECT_EQ("aaaaaaaaaaaa(\n"
13350             "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
13351             "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
13352             format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa "
13353                    "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
13354                    "aaaaaaaaaaaaaaaaaaaaaa\");",
13355                    getGoogleStyle()));
13356   EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
13357             "       \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
13358             format("return \"aaaaaaaaaaaaaaaaaaaaaa "
13359                    "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
13360                    "aaaaaaaaaaaaaaaaaaaaaa\";",
13361                    getGoogleStyle()));
13362   EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
13363             "                \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
13364             format("llvm::outs() << "
13365                    "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
13366                    "aaaaaaaaaaaaaaaaaaa\";"));
13367   EXPECT_EQ("ffff(\n"
13368             "    {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
13369             "     \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
13370             format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
13371                    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
13372                    getGoogleStyle()));
13373 
13374   FormatStyle Style = getLLVMStyleWithColumns(12);
13375   Style.BreakStringLiterals = false;
13376   EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style));
13377 
13378   FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
13379   AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
13380   EXPECT_EQ("#define A \\\n"
13381             "  \"some \" \\\n"
13382             "  \"text \" \\\n"
13383             "  \"other\";",
13384             format("#define A \"some text other\";", AlignLeft));
13385 }
13386 
13387 TEST_F(FormatTest, BreaksStringLiteralsAtColumnLimit) {
13388   EXPECT_EQ("C a = \"some more \"\n"
13389             "      \"text\";",
13390             format("C a = \"some more text\";", getLLVMStyleWithColumns(18)));
13391 }
13392 
13393 TEST_F(FormatTest, FullyRemoveEmptyLines) {
13394   FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80);
13395   NoEmptyLines.MaxEmptyLinesToKeep = 0;
13396   EXPECT_EQ("int i = a(b());",
13397             format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines));
13398 }
13399 
13400 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
13401   EXPECT_EQ(
13402       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
13403       "(\n"
13404       "    \"x\t\");",
13405       format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
13406              "aaaaaaa("
13407              "\"x\t\");"));
13408 }
13409 
13410 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
13411   EXPECT_EQ(
13412       "u8\"utf8 string \"\n"
13413       "u8\"literal\";",
13414       format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
13415   EXPECT_EQ(
13416       "u\"utf16 string \"\n"
13417       "u\"literal\";",
13418       format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
13419   EXPECT_EQ(
13420       "U\"utf32 string \"\n"
13421       "U\"literal\";",
13422       format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
13423   EXPECT_EQ("L\"wide string \"\n"
13424             "L\"literal\";",
13425             format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
13426   EXPECT_EQ("@\"NSString \"\n"
13427             "@\"literal\";",
13428             format("@\"NSString literal\";", getGoogleStyleWithColumns(19)));
13429   verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26));
13430 
13431   // This input makes clang-format try to split the incomplete unicode escape
13432   // sequence, which used to lead to a crasher.
13433   verifyNoCrash(
13434       "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
13435       getLLVMStyleWithColumns(60));
13436 }
13437 
13438 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) {
13439   FormatStyle Style = getGoogleStyleWithColumns(15);
13440   EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style));
13441   EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style));
13442   EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style));
13443   EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style));
13444   EXPECT_EQ("u8R\"x(raw literal)x\";",
13445             format("u8R\"x(raw literal)x\";", Style));
13446 }
13447 
13448 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
13449   FormatStyle Style = getLLVMStyleWithColumns(20);
13450   EXPECT_EQ(
13451       "_T(\"aaaaaaaaaaaaaa\")\n"
13452       "_T(\"aaaaaaaaaaaaaa\")\n"
13453       "_T(\"aaaaaaaaaaaa\")",
13454       format("  _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
13455   EXPECT_EQ("f(x,\n"
13456             "  _T(\"aaaaaaaaaaaa\")\n"
13457             "  _T(\"aaa\"),\n"
13458             "  z);",
13459             format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style));
13460 
13461   // FIXME: Handle embedded spaces in one iteration.
13462   //  EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
13463   //            "_T(\"aaaaaaaaaaaaa\")\n"
13464   //            "_T(\"aaaaaaaaaaaaa\")\n"
13465   //            "_T(\"a\")",
13466   //            format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
13467   //                   getLLVMStyleWithColumns(20)));
13468   EXPECT_EQ(
13469       "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
13470       format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style));
13471   EXPECT_EQ("f(\n"
13472             "#if !TEST\n"
13473             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
13474             "#endif\n"
13475             ");",
13476             format("f(\n"
13477                    "#if !TEST\n"
13478                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
13479                    "#endif\n"
13480                    ");"));
13481   EXPECT_EQ("f(\n"
13482             "\n"
13483             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));",
13484             format("f(\n"
13485                    "\n"
13486                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));"));
13487   // Regression test for accessing tokens past the end of a vector in the
13488   // TokenLexer.
13489   verifyNoCrash(R"(_T(
13490 "
13491 )
13492 )");
13493 }
13494 
13495 TEST_F(FormatTest, BreaksStringLiteralOperands) {
13496   // In a function call with two operands, the second can be broken with no line
13497   // break before it.
13498   EXPECT_EQ(
13499       "func(a, \"long long \"\n"
13500       "        \"long long\");",
13501       format("func(a, \"long long long long\");", getLLVMStyleWithColumns(24)));
13502   // In a function call with three operands, the second must be broken with a
13503   // line break before it.
13504   EXPECT_EQ("func(a,\n"
13505             "     \"long long long \"\n"
13506             "     \"long\",\n"
13507             "     c);",
13508             format("func(a, \"long long long long\", c);",
13509                    getLLVMStyleWithColumns(24)));
13510   // In a function call with three operands, the third must be broken with a
13511   // line break before it.
13512   EXPECT_EQ("func(a, b,\n"
13513             "     \"long long long \"\n"
13514             "     \"long\");",
13515             format("func(a, b, \"long long long long\");",
13516                    getLLVMStyleWithColumns(24)));
13517   // In a function call with three operands, both the second and the third must
13518   // be broken with a line break before them.
13519   EXPECT_EQ("func(a,\n"
13520             "     \"long long long \"\n"
13521             "     \"long\",\n"
13522             "     \"long long long \"\n"
13523             "     \"long\");",
13524             format("func(a, \"long long long long\", \"long long long long\");",
13525                    getLLVMStyleWithColumns(24)));
13526   // In a chain of << with two operands, the second can be broken with no line
13527   // break before it.
13528   EXPECT_EQ("a << \"line line \"\n"
13529             "     \"line\";",
13530             format("a << \"line line line\";", getLLVMStyleWithColumns(20)));
13531   // In a chain of << with three operands, the second can be broken with no line
13532   // break before it.
13533   EXPECT_EQ(
13534       "abcde << \"line \"\n"
13535       "         \"line line\"\n"
13536       "      << c;",
13537       format("abcde << \"line line line\" << c;", getLLVMStyleWithColumns(20)));
13538   // In a chain of << with three operands, the third must be broken with a line
13539   // break before it.
13540   EXPECT_EQ(
13541       "a << b\n"
13542       "  << \"line line \"\n"
13543       "     \"line\";",
13544       format("a << b << \"line line line\";", getLLVMStyleWithColumns(20)));
13545   // In a chain of << with three operands, the second can be broken with no line
13546   // break before it and the third must be broken with a line break before it.
13547   EXPECT_EQ("abcd << \"line line \"\n"
13548             "        \"line\"\n"
13549             "     << \"line line \"\n"
13550             "        \"line\";",
13551             format("abcd << \"line line line\" << \"line line line\";",
13552                    getLLVMStyleWithColumns(20)));
13553   // In a chain of binary operators with two operands, the second can be broken
13554   // with no line break before it.
13555   EXPECT_EQ(
13556       "abcd + \"line line \"\n"
13557       "       \"line line\";",
13558       format("abcd + \"line line line line\";", getLLVMStyleWithColumns(20)));
13559   // In a chain of binary operators with three operands, the second must be
13560   // broken with a line break before it.
13561   EXPECT_EQ("abcd +\n"
13562             "    \"line line \"\n"
13563             "    \"line line\" +\n"
13564             "    e;",
13565             format("abcd + \"line line line line\" + e;",
13566                    getLLVMStyleWithColumns(20)));
13567   // In a function call with two operands, with AlignAfterOpenBracket enabled,
13568   // the first must be broken with a line break before it.
13569   FormatStyle Style = getLLVMStyleWithColumns(25);
13570   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
13571   EXPECT_EQ("someFunction(\n"
13572             "    \"long long long \"\n"
13573             "    \"long\",\n"
13574             "    a);",
13575             format("someFunction(\"long long long long\", a);", Style));
13576 }
13577 
13578 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
13579   EXPECT_EQ(
13580       "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
13581       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
13582       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
13583       format("aaaaaaaaaaa  =  \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
13584              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
13585              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"));
13586 }
13587 
13588 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
13589   EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);",
13590             format("f(g(R\"x(raw literal)x\",   a), b);", getGoogleStyle()));
13591   EXPECT_EQ("fffffffffff(g(R\"x(\n"
13592             "multiline raw string literal xxxxxxxxxxxxxx\n"
13593             ")x\",\n"
13594             "              a),\n"
13595             "            b);",
13596             format("fffffffffff(g(R\"x(\n"
13597                    "multiline raw string literal xxxxxxxxxxxxxx\n"
13598                    ")x\", a), b);",
13599                    getGoogleStyleWithColumns(20)));
13600   EXPECT_EQ("fffffffffff(\n"
13601             "    g(R\"x(qqq\n"
13602             "multiline raw string literal xxxxxxxxxxxxxx\n"
13603             ")x\",\n"
13604             "      a),\n"
13605             "    b);",
13606             format("fffffffffff(g(R\"x(qqq\n"
13607                    "multiline raw string literal xxxxxxxxxxxxxx\n"
13608                    ")x\", a), b);",
13609                    getGoogleStyleWithColumns(20)));
13610 
13611   EXPECT_EQ("fffffffffff(R\"x(\n"
13612             "multiline raw string literal xxxxxxxxxxxxxx\n"
13613             ")x\");",
13614             format("fffffffffff(R\"x(\n"
13615                    "multiline raw string literal xxxxxxxxxxxxxx\n"
13616                    ")x\");",
13617                    getGoogleStyleWithColumns(20)));
13618   EXPECT_EQ("fffffffffff(R\"x(\n"
13619             "multiline raw string literal xxxxxxxxxxxxxx\n"
13620             ")x\" + bbbbbb);",
13621             format("fffffffffff(R\"x(\n"
13622                    "multiline raw string literal xxxxxxxxxxxxxx\n"
13623                    ")x\" +   bbbbbb);",
13624                    getGoogleStyleWithColumns(20)));
13625   EXPECT_EQ("fffffffffff(\n"
13626             "    R\"x(\n"
13627             "multiline raw string literal xxxxxxxxxxxxxx\n"
13628             ")x\" +\n"
13629             "    bbbbbb);",
13630             format("fffffffffff(\n"
13631                    " R\"x(\n"
13632                    "multiline raw string literal xxxxxxxxxxxxxx\n"
13633                    ")x\" + bbbbbb);",
13634                    getGoogleStyleWithColumns(20)));
13635   EXPECT_EQ("fffffffffff(R\"(single line raw string)\" + bbbbbb);",
13636             format("fffffffffff(\n"
13637                    " R\"(single line raw string)\" + bbbbbb);"));
13638 }
13639 
13640 TEST_F(FormatTest, SkipsUnknownStringLiterals) {
13641   verifyFormat("string a = \"unterminated;");
13642   EXPECT_EQ("function(\"unterminated,\n"
13643             "         OtherParameter);",
13644             format("function(  \"unterminated,\n"
13645                    "    OtherParameter);"));
13646 }
13647 
13648 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
13649   FormatStyle Style = getLLVMStyle();
13650   Style.Standard = FormatStyle::LS_Cpp03;
13651   EXPECT_EQ("#define x(_a) printf(\"foo\" _a);",
13652             format("#define x(_a) printf(\"foo\"_a);", Style));
13653 }
13654 
13655 TEST_F(FormatTest, CppLexVersion) {
13656   FormatStyle Style = getLLVMStyle();
13657   // Formatting of x * y differs if x is a type.
13658   verifyFormat("void foo() { MACRO(a * b); }", Style);
13659   verifyFormat("void foo() { MACRO(int *b); }", Style);
13660 
13661   // LLVM style uses latest lexer.
13662   verifyFormat("void foo() { MACRO(char8_t *b); }", Style);
13663   Style.Standard = FormatStyle::LS_Cpp17;
13664   // But in c++17, char8_t isn't a keyword.
13665   verifyFormat("void foo() { MACRO(char8_t * b); }", Style);
13666 }
13667 
13668 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); }
13669 
13670 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
13671   EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
13672             "             \"ddeeefff\");",
13673             format("someFunction(\"aaabbbcccdddeeefff\");",
13674                    getLLVMStyleWithColumns(25)));
13675   EXPECT_EQ("someFunction1234567890(\n"
13676             "    \"aaabbbcccdddeeefff\");",
13677             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
13678                    getLLVMStyleWithColumns(26)));
13679   EXPECT_EQ("someFunction1234567890(\n"
13680             "    \"aaabbbcccdddeeeff\"\n"
13681             "    \"f\");",
13682             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
13683                    getLLVMStyleWithColumns(25)));
13684   EXPECT_EQ("someFunction1234567890(\n"
13685             "    \"aaabbbcccdddeeeff\"\n"
13686             "    \"f\");",
13687             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
13688                    getLLVMStyleWithColumns(24)));
13689   EXPECT_EQ("someFunction(\n"
13690             "    \"aaabbbcc ddde \"\n"
13691             "    \"efff\");",
13692             format("someFunction(\"aaabbbcc ddde efff\");",
13693                    getLLVMStyleWithColumns(25)));
13694   EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
13695             "             \"ddeeefff\");",
13696             format("someFunction(\"aaabbbccc ddeeefff\");",
13697                    getLLVMStyleWithColumns(25)));
13698   EXPECT_EQ("someFunction1234567890(\n"
13699             "    \"aaabb \"\n"
13700             "    \"cccdddeeefff\");",
13701             format("someFunction1234567890(\"aaabb cccdddeeefff\");",
13702                    getLLVMStyleWithColumns(25)));
13703   EXPECT_EQ("#define A          \\\n"
13704             "  string s =       \\\n"
13705             "      \"123456789\"  \\\n"
13706             "      \"0\";         \\\n"
13707             "  int i;",
13708             format("#define A string s = \"1234567890\"; int i;",
13709                    getLLVMStyleWithColumns(20)));
13710   EXPECT_EQ("someFunction(\n"
13711             "    \"aaabbbcc \"\n"
13712             "    \"dddeeefff\");",
13713             format("someFunction(\"aaabbbcc dddeeefff\");",
13714                    getLLVMStyleWithColumns(25)));
13715 }
13716 
13717 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
13718   EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3)));
13719   EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2)));
13720   EXPECT_EQ("\"test\"\n"
13721             "\"\\n\"",
13722             format("\"test\\n\"", getLLVMStyleWithColumns(7)));
13723   EXPECT_EQ("\"tes\\\\\"\n"
13724             "\"n\"",
13725             format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
13726   EXPECT_EQ("\"\\\\\\\\\"\n"
13727             "\"\\n\"",
13728             format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
13729   EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
13730   EXPECT_EQ("\"\\uff01\"\n"
13731             "\"test\"",
13732             format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
13733   EXPECT_EQ("\"\\Uff01ff02\"",
13734             format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
13735   EXPECT_EQ("\"\\x000000000001\"\n"
13736             "\"next\"",
13737             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
13738   EXPECT_EQ("\"\\x000000000001next\"",
13739             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
13740   EXPECT_EQ("\"\\x000000000001\"",
13741             format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
13742   EXPECT_EQ("\"test\"\n"
13743             "\"\\000000\"\n"
13744             "\"000001\"",
13745             format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
13746   EXPECT_EQ("\"test\\000\"\n"
13747             "\"00000000\"\n"
13748             "\"1\"",
13749             format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
13750 }
13751 
13752 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
13753   verifyFormat("void f() {\n"
13754                "  return g() {}\n"
13755                "  void h() {}");
13756   verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
13757                "g();\n"
13758                "}");
13759 }
13760 
13761 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
13762   verifyFormat(
13763       "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
13764 }
13765 
13766 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
13767   verifyFormat("class X {\n"
13768                "  void f() {\n"
13769                "  }\n"
13770                "};",
13771                getLLVMStyleWithColumns(12));
13772 }
13773 
13774 TEST_F(FormatTest, ConfigurableIndentWidth) {
13775   FormatStyle EightIndent = getLLVMStyleWithColumns(18);
13776   EightIndent.IndentWidth = 8;
13777   EightIndent.ContinuationIndentWidth = 8;
13778   verifyFormat("void f() {\n"
13779                "        someFunction();\n"
13780                "        if (true) {\n"
13781                "                f();\n"
13782                "        }\n"
13783                "}",
13784                EightIndent);
13785   verifyFormat("class X {\n"
13786                "        void f() {\n"
13787                "        }\n"
13788                "};",
13789                EightIndent);
13790   verifyFormat("int x[] = {\n"
13791                "        call(),\n"
13792                "        call()};",
13793                EightIndent);
13794 }
13795 
13796 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
13797   verifyFormat("double\n"
13798                "f();",
13799                getLLVMStyleWithColumns(8));
13800 }
13801 
13802 TEST_F(FormatTest, ConfigurableUseOfTab) {
13803   FormatStyle Tab = getLLVMStyleWithColumns(42);
13804   Tab.IndentWidth = 8;
13805   Tab.UseTab = FormatStyle::UT_Always;
13806   Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left;
13807 
13808   EXPECT_EQ("if (aaaaaaaa && // q\n"
13809             "    bb)\t\t// w\n"
13810             "\t;",
13811             format("if (aaaaaaaa &&// q\n"
13812                    "bb)// w\n"
13813                    ";",
13814                    Tab));
13815   EXPECT_EQ("if (aaa && bbb) // w\n"
13816             "\t;",
13817             format("if(aaa&&bbb)// w\n"
13818                    ";",
13819                    Tab));
13820 
13821   verifyFormat("class X {\n"
13822                "\tvoid f() {\n"
13823                "\t\tsomeFunction(parameter1,\n"
13824                "\t\t\t     parameter2);\n"
13825                "\t}\n"
13826                "};",
13827                Tab);
13828   verifyFormat("#define A                        \\\n"
13829                "\tvoid f() {               \\\n"
13830                "\t\tsomeFunction(    \\\n"
13831                "\t\t    parameter1,  \\\n"
13832                "\t\t    parameter2); \\\n"
13833                "\t}",
13834                Tab);
13835   verifyFormat("int a;\t      // x\n"
13836                "int bbbbbbbb; // x\n",
13837                Tab);
13838 
13839   Tab.TabWidth = 4;
13840   Tab.IndentWidth = 8;
13841   verifyFormat("class TabWidth4Indent8 {\n"
13842                "\t\tvoid f() {\n"
13843                "\t\t\t\tsomeFunction(parameter1,\n"
13844                "\t\t\t\t\t\t\t parameter2);\n"
13845                "\t\t}\n"
13846                "};",
13847                Tab);
13848 
13849   Tab.TabWidth = 4;
13850   Tab.IndentWidth = 4;
13851   verifyFormat("class TabWidth4Indent4 {\n"
13852                "\tvoid f() {\n"
13853                "\t\tsomeFunction(parameter1,\n"
13854                "\t\t\t\t\t parameter2);\n"
13855                "\t}\n"
13856                "};",
13857                Tab);
13858 
13859   Tab.TabWidth = 8;
13860   Tab.IndentWidth = 4;
13861   verifyFormat("class TabWidth8Indent4 {\n"
13862                "    void f() {\n"
13863                "\tsomeFunction(parameter1,\n"
13864                "\t\t     parameter2);\n"
13865                "    }\n"
13866                "};",
13867                Tab);
13868 
13869   Tab.TabWidth = 8;
13870   Tab.IndentWidth = 8;
13871   EXPECT_EQ("/*\n"
13872             "\t      a\t\tcomment\n"
13873             "\t      in multiple lines\n"
13874             "       */",
13875             format("   /*\t \t \n"
13876                    " \t \t a\t\tcomment\t \t\n"
13877                    " \t \t in multiple lines\t\n"
13878                    " \t  */",
13879                    Tab));
13880 
13881   Tab.UseTab = FormatStyle::UT_ForIndentation;
13882   verifyFormat("{\n"
13883                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13884                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13885                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13886                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13887                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13888                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13889                "};",
13890                Tab);
13891   verifyFormat("enum AA {\n"
13892                "\ta1, // Force multiple lines\n"
13893                "\ta2,\n"
13894                "\ta3\n"
13895                "};",
13896                Tab);
13897   EXPECT_EQ("if (aaaaaaaa && // q\n"
13898             "    bb)         // w\n"
13899             "\t;",
13900             format("if (aaaaaaaa &&// q\n"
13901                    "bb)// w\n"
13902                    ";",
13903                    Tab));
13904   verifyFormat("class X {\n"
13905                "\tvoid f() {\n"
13906                "\t\tsomeFunction(parameter1,\n"
13907                "\t\t             parameter2);\n"
13908                "\t}\n"
13909                "};",
13910                Tab);
13911   verifyFormat("{\n"
13912                "\tQ(\n"
13913                "\t    {\n"
13914                "\t\t    int a;\n"
13915                "\t\t    someFunction(aaaaaaaa,\n"
13916                "\t\t                 bbbbbbb);\n"
13917                "\t    },\n"
13918                "\t    p);\n"
13919                "}",
13920                Tab);
13921   EXPECT_EQ("{\n"
13922             "\t/* aaaa\n"
13923             "\t   bbbb */\n"
13924             "}",
13925             format("{\n"
13926                    "/* aaaa\n"
13927                    "   bbbb */\n"
13928                    "}",
13929                    Tab));
13930   EXPECT_EQ("{\n"
13931             "\t/*\n"
13932             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13933             "\t  bbbbbbbbbbbbb\n"
13934             "\t*/\n"
13935             "}",
13936             format("{\n"
13937                    "/*\n"
13938                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13939                    "*/\n"
13940                    "}",
13941                    Tab));
13942   EXPECT_EQ("{\n"
13943             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13944             "\t// bbbbbbbbbbbbb\n"
13945             "}",
13946             format("{\n"
13947                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13948                    "}",
13949                    Tab));
13950   EXPECT_EQ("{\n"
13951             "\t/*\n"
13952             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13953             "\t  bbbbbbbbbbbbb\n"
13954             "\t*/\n"
13955             "}",
13956             format("{\n"
13957                    "\t/*\n"
13958                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13959                    "\t*/\n"
13960                    "}",
13961                    Tab));
13962   EXPECT_EQ("{\n"
13963             "\t/*\n"
13964             "\n"
13965             "\t*/\n"
13966             "}",
13967             format("{\n"
13968                    "\t/*\n"
13969                    "\n"
13970                    "\t*/\n"
13971                    "}",
13972                    Tab));
13973   EXPECT_EQ("{\n"
13974             "\t/*\n"
13975             " asdf\n"
13976             "\t*/\n"
13977             "}",
13978             format("{\n"
13979                    "\t/*\n"
13980                    " asdf\n"
13981                    "\t*/\n"
13982                    "}",
13983                    Tab));
13984 
13985   verifyFormat("void f() {\n"
13986                "\treturn true ? aaaaaaaaaaaaaaaaaa\n"
13987                "\t            : bbbbbbbbbbbbbbbbbb\n"
13988                "}",
13989                Tab);
13990   FormatStyle TabNoBreak = Tab;
13991   TabNoBreak.BreakBeforeTernaryOperators = false;
13992   verifyFormat("void f() {\n"
13993                "\treturn true ? aaaaaaaaaaaaaaaaaa :\n"
13994                "\t              bbbbbbbbbbbbbbbbbb\n"
13995                "}",
13996                TabNoBreak);
13997   verifyFormat("void f() {\n"
13998                "\treturn true ?\n"
13999                "\t           aaaaaaaaaaaaaaaaaaaa :\n"
14000                "\t           bbbbbbbbbbbbbbbbbbbb\n"
14001                "}",
14002                TabNoBreak);
14003 
14004   Tab.UseTab = FormatStyle::UT_Never;
14005   EXPECT_EQ("/*\n"
14006             "              a\t\tcomment\n"
14007             "              in multiple lines\n"
14008             "       */",
14009             format("   /*\t \t \n"
14010                    " \t \t a\t\tcomment\t \t\n"
14011                    " \t \t in multiple lines\t\n"
14012                    " \t  */",
14013                    Tab));
14014   EXPECT_EQ("/* some\n"
14015             "   comment */",
14016             format(" \t \t /* some\n"
14017                    " \t \t    comment */",
14018                    Tab));
14019   EXPECT_EQ("int a; /* some\n"
14020             "   comment */",
14021             format(" \t \t int a; /* some\n"
14022                    " \t \t    comment */",
14023                    Tab));
14024 
14025   EXPECT_EQ("int a; /* some\n"
14026             "comment */",
14027             format(" \t \t int\ta; /* some\n"
14028                    " \t \t    comment */",
14029                    Tab));
14030   EXPECT_EQ("f(\"\t\t\"); /* some\n"
14031             "    comment */",
14032             format(" \t \t f(\"\t\t\"); /* some\n"
14033                    " \t \t    comment */",
14034                    Tab));
14035   EXPECT_EQ("{\n"
14036             "        /*\n"
14037             "         * Comment\n"
14038             "         */\n"
14039             "        int i;\n"
14040             "}",
14041             format("{\n"
14042                    "\t/*\n"
14043                    "\t * Comment\n"
14044                    "\t */\n"
14045                    "\t int i;\n"
14046                    "}",
14047                    Tab));
14048 
14049   Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
14050   Tab.TabWidth = 8;
14051   Tab.IndentWidth = 8;
14052   EXPECT_EQ("if (aaaaaaaa && // q\n"
14053             "    bb)         // w\n"
14054             "\t;",
14055             format("if (aaaaaaaa &&// q\n"
14056                    "bb)// w\n"
14057                    ";",
14058                    Tab));
14059   EXPECT_EQ("if (aaa && bbb) // w\n"
14060             "\t;",
14061             format("if(aaa&&bbb)// w\n"
14062                    ";",
14063                    Tab));
14064   verifyFormat("class X {\n"
14065                "\tvoid f() {\n"
14066                "\t\tsomeFunction(parameter1,\n"
14067                "\t\t\t     parameter2);\n"
14068                "\t}\n"
14069                "};",
14070                Tab);
14071   verifyFormat("#define A                        \\\n"
14072                "\tvoid f() {               \\\n"
14073                "\t\tsomeFunction(    \\\n"
14074                "\t\t    parameter1,  \\\n"
14075                "\t\t    parameter2); \\\n"
14076                "\t}",
14077                Tab);
14078   Tab.TabWidth = 4;
14079   Tab.IndentWidth = 8;
14080   verifyFormat("class TabWidth4Indent8 {\n"
14081                "\t\tvoid f() {\n"
14082                "\t\t\t\tsomeFunction(parameter1,\n"
14083                "\t\t\t\t\t\t\t parameter2);\n"
14084                "\t\t}\n"
14085                "};",
14086                Tab);
14087   Tab.TabWidth = 4;
14088   Tab.IndentWidth = 4;
14089   verifyFormat("class TabWidth4Indent4 {\n"
14090                "\tvoid f() {\n"
14091                "\t\tsomeFunction(parameter1,\n"
14092                "\t\t\t\t\t parameter2);\n"
14093                "\t}\n"
14094                "};",
14095                Tab);
14096   Tab.TabWidth = 8;
14097   Tab.IndentWidth = 4;
14098   verifyFormat("class TabWidth8Indent4 {\n"
14099                "    void f() {\n"
14100                "\tsomeFunction(parameter1,\n"
14101                "\t\t     parameter2);\n"
14102                "    }\n"
14103                "};",
14104                Tab);
14105   Tab.TabWidth = 8;
14106   Tab.IndentWidth = 8;
14107   EXPECT_EQ("/*\n"
14108             "\t      a\t\tcomment\n"
14109             "\t      in multiple lines\n"
14110             "       */",
14111             format("   /*\t \t \n"
14112                    " \t \t a\t\tcomment\t \t\n"
14113                    " \t \t in multiple lines\t\n"
14114                    " \t  */",
14115                    Tab));
14116   verifyFormat("{\n"
14117                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
14118                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
14119                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
14120                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
14121                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
14122                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
14123                "};",
14124                Tab);
14125   verifyFormat("enum AA {\n"
14126                "\ta1, // Force multiple lines\n"
14127                "\ta2,\n"
14128                "\ta3\n"
14129                "};",
14130                Tab);
14131   EXPECT_EQ("if (aaaaaaaa && // q\n"
14132             "    bb)         // w\n"
14133             "\t;",
14134             format("if (aaaaaaaa &&// q\n"
14135                    "bb)// w\n"
14136                    ";",
14137                    Tab));
14138   verifyFormat("class X {\n"
14139                "\tvoid f() {\n"
14140                "\t\tsomeFunction(parameter1,\n"
14141                "\t\t\t     parameter2);\n"
14142                "\t}\n"
14143                "};",
14144                Tab);
14145   verifyFormat("{\n"
14146                "\tQ(\n"
14147                "\t    {\n"
14148                "\t\t    int a;\n"
14149                "\t\t    someFunction(aaaaaaaa,\n"
14150                "\t\t\t\t bbbbbbb);\n"
14151                "\t    },\n"
14152                "\t    p);\n"
14153                "}",
14154                Tab);
14155   EXPECT_EQ("{\n"
14156             "\t/* aaaa\n"
14157             "\t   bbbb */\n"
14158             "}",
14159             format("{\n"
14160                    "/* aaaa\n"
14161                    "   bbbb */\n"
14162                    "}",
14163                    Tab));
14164   EXPECT_EQ("{\n"
14165             "\t/*\n"
14166             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
14167             "\t  bbbbbbbbbbbbb\n"
14168             "\t*/\n"
14169             "}",
14170             format("{\n"
14171                    "/*\n"
14172                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
14173                    "*/\n"
14174                    "}",
14175                    Tab));
14176   EXPECT_EQ("{\n"
14177             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
14178             "\t// bbbbbbbbbbbbb\n"
14179             "}",
14180             format("{\n"
14181                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
14182                    "}",
14183                    Tab));
14184   EXPECT_EQ("{\n"
14185             "\t/*\n"
14186             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
14187             "\t  bbbbbbbbbbbbb\n"
14188             "\t*/\n"
14189             "}",
14190             format("{\n"
14191                    "\t/*\n"
14192                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
14193                    "\t*/\n"
14194                    "}",
14195                    Tab));
14196   EXPECT_EQ("{\n"
14197             "\t/*\n"
14198             "\n"
14199             "\t*/\n"
14200             "}",
14201             format("{\n"
14202                    "\t/*\n"
14203                    "\n"
14204                    "\t*/\n"
14205                    "}",
14206                    Tab));
14207   EXPECT_EQ("{\n"
14208             "\t/*\n"
14209             " asdf\n"
14210             "\t*/\n"
14211             "}",
14212             format("{\n"
14213                    "\t/*\n"
14214                    " asdf\n"
14215                    "\t*/\n"
14216                    "}",
14217                    Tab));
14218   EXPECT_EQ("/* some\n"
14219             "   comment */",
14220             format(" \t \t /* some\n"
14221                    " \t \t    comment */",
14222                    Tab));
14223   EXPECT_EQ("int a; /* some\n"
14224             "   comment */",
14225             format(" \t \t int a; /* some\n"
14226                    " \t \t    comment */",
14227                    Tab));
14228   EXPECT_EQ("int a; /* some\n"
14229             "comment */",
14230             format(" \t \t int\ta; /* some\n"
14231                    " \t \t    comment */",
14232                    Tab));
14233   EXPECT_EQ("f(\"\t\t\"); /* some\n"
14234             "    comment */",
14235             format(" \t \t f(\"\t\t\"); /* some\n"
14236                    " \t \t    comment */",
14237                    Tab));
14238   EXPECT_EQ("{\n"
14239             "\t/*\n"
14240             "\t * Comment\n"
14241             "\t */\n"
14242             "\tint i;\n"
14243             "}",
14244             format("{\n"
14245                    "\t/*\n"
14246                    "\t * Comment\n"
14247                    "\t */\n"
14248                    "\t int i;\n"
14249                    "}",
14250                    Tab));
14251   Tab.TabWidth = 2;
14252   Tab.IndentWidth = 2;
14253   EXPECT_EQ("{\n"
14254             "\t/* aaaa\n"
14255             "\t\t bbbb */\n"
14256             "}",
14257             format("{\n"
14258                    "/* aaaa\n"
14259                    "\t bbbb */\n"
14260                    "}",
14261                    Tab));
14262   EXPECT_EQ("{\n"
14263             "\t/*\n"
14264             "\t\taaaaaaaaaaaaaaaaaaaaaaaaaa\n"
14265             "\t\tbbbbbbbbbbbbb\n"
14266             "\t*/\n"
14267             "}",
14268             format("{\n"
14269                    "/*\n"
14270                    "\taaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
14271                    "*/\n"
14272                    "}",
14273                    Tab));
14274   Tab.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
14275   Tab.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
14276   Tab.TabWidth = 4;
14277   Tab.IndentWidth = 4;
14278   verifyFormat("class Assign {\n"
14279                "\tvoid f() {\n"
14280                "\t\tint         x      = 123;\n"
14281                "\t\tint         random = 4;\n"
14282                "\t\tstd::string alphabet =\n"
14283                "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
14284                "\t}\n"
14285                "};",
14286                Tab);
14287 
14288   Tab.UseTab = FormatStyle::UT_AlignWithSpaces;
14289   Tab.TabWidth = 8;
14290   Tab.IndentWidth = 8;
14291   EXPECT_EQ("if (aaaaaaaa && // q\n"
14292             "    bb)         // w\n"
14293             "\t;",
14294             format("if (aaaaaaaa &&// q\n"
14295                    "bb)// w\n"
14296                    ";",
14297                    Tab));
14298   EXPECT_EQ("if (aaa && bbb) // w\n"
14299             "\t;",
14300             format("if(aaa&&bbb)// w\n"
14301                    ";",
14302                    Tab));
14303   verifyFormat("class X {\n"
14304                "\tvoid f() {\n"
14305                "\t\tsomeFunction(parameter1,\n"
14306                "\t\t             parameter2);\n"
14307                "\t}\n"
14308                "};",
14309                Tab);
14310   verifyFormat("#define A                        \\\n"
14311                "\tvoid f() {               \\\n"
14312                "\t\tsomeFunction(    \\\n"
14313                "\t\t    parameter1,  \\\n"
14314                "\t\t    parameter2); \\\n"
14315                "\t}",
14316                Tab);
14317   Tab.TabWidth = 4;
14318   Tab.IndentWidth = 8;
14319   verifyFormat("class TabWidth4Indent8 {\n"
14320                "\t\tvoid f() {\n"
14321                "\t\t\t\tsomeFunction(parameter1,\n"
14322                "\t\t\t\t             parameter2);\n"
14323                "\t\t}\n"
14324                "};",
14325                Tab);
14326   Tab.TabWidth = 4;
14327   Tab.IndentWidth = 4;
14328   verifyFormat("class TabWidth4Indent4 {\n"
14329                "\tvoid f() {\n"
14330                "\t\tsomeFunction(parameter1,\n"
14331                "\t\t             parameter2);\n"
14332                "\t}\n"
14333                "};",
14334                Tab);
14335   Tab.TabWidth = 8;
14336   Tab.IndentWidth = 4;
14337   verifyFormat("class TabWidth8Indent4 {\n"
14338                "    void f() {\n"
14339                "\tsomeFunction(parameter1,\n"
14340                "\t             parameter2);\n"
14341                "    }\n"
14342                "};",
14343                Tab);
14344   Tab.TabWidth = 8;
14345   Tab.IndentWidth = 8;
14346   EXPECT_EQ("/*\n"
14347             "              a\t\tcomment\n"
14348             "              in multiple lines\n"
14349             "       */",
14350             format("   /*\t \t \n"
14351                    " \t \t a\t\tcomment\t \t\n"
14352                    " \t \t in multiple lines\t\n"
14353                    " \t  */",
14354                    Tab));
14355   verifyFormat("{\n"
14356                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
14357                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
14358                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
14359                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
14360                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
14361                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
14362                "};",
14363                Tab);
14364   verifyFormat("enum AA {\n"
14365                "\ta1, // Force multiple lines\n"
14366                "\ta2,\n"
14367                "\ta3\n"
14368                "};",
14369                Tab);
14370   EXPECT_EQ("if (aaaaaaaa && // q\n"
14371             "    bb)         // w\n"
14372             "\t;",
14373             format("if (aaaaaaaa &&// q\n"
14374                    "bb)// w\n"
14375                    ";",
14376                    Tab));
14377   verifyFormat("class X {\n"
14378                "\tvoid f() {\n"
14379                "\t\tsomeFunction(parameter1,\n"
14380                "\t\t             parameter2);\n"
14381                "\t}\n"
14382                "};",
14383                Tab);
14384   verifyFormat("{\n"
14385                "\tQ(\n"
14386                "\t    {\n"
14387                "\t\t    int a;\n"
14388                "\t\t    someFunction(aaaaaaaa,\n"
14389                "\t\t                 bbbbbbb);\n"
14390                "\t    },\n"
14391                "\t    p);\n"
14392                "}",
14393                Tab);
14394   EXPECT_EQ("{\n"
14395             "\t/* aaaa\n"
14396             "\t   bbbb */\n"
14397             "}",
14398             format("{\n"
14399                    "/* aaaa\n"
14400                    "   bbbb */\n"
14401                    "}",
14402                    Tab));
14403   EXPECT_EQ("{\n"
14404             "\t/*\n"
14405             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
14406             "\t  bbbbbbbbbbbbb\n"
14407             "\t*/\n"
14408             "}",
14409             format("{\n"
14410                    "/*\n"
14411                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
14412                    "*/\n"
14413                    "}",
14414                    Tab));
14415   EXPECT_EQ("{\n"
14416             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
14417             "\t// bbbbbbbbbbbbb\n"
14418             "}",
14419             format("{\n"
14420                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
14421                    "}",
14422                    Tab));
14423   EXPECT_EQ("{\n"
14424             "\t/*\n"
14425             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
14426             "\t  bbbbbbbbbbbbb\n"
14427             "\t*/\n"
14428             "}",
14429             format("{\n"
14430                    "\t/*\n"
14431                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
14432                    "\t*/\n"
14433                    "}",
14434                    Tab));
14435   EXPECT_EQ("{\n"
14436             "\t/*\n"
14437             "\n"
14438             "\t*/\n"
14439             "}",
14440             format("{\n"
14441                    "\t/*\n"
14442                    "\n"
14443                    "\t*/\n"
14444                    "}",
14445                    Tab));
14446   EXPECT_EQ("{\n"
14447             "\t/*\n"
14448             " asdf\n"
14449             "\t*/\n"
14450             "}",
14451             format("{\n"
14452                    "\t/*\n"
14453                    " asdf\n"
14454                    "\t*/\n"
14455                    "}",
14456                    Tab));
14457   EXPECT_EQ("/* some\n"
14458             "   comment */",
14459             format(" \t \t /* some\n"
14460                    " \t \t    comment */",
14461                    Tab));
14462   EXPECT_EQ("int a; /* some\n"
14463             "   comment */",
14464             format(" \t \t int a; /* some\n"
14465                    " \t \t    comment */",
14466                    Tab));
14467   EXPECT_EQ("int a; /* some\n"
14468             "comment */",
14469             format(" \t \t int\ta; /* some\n"
14470                    " \t \t    comment */",
14471                    Tab));
14472   EXPECT_EQ("f(\"\t\t\"); /* some\n"
14473             "    comment */",
14474             format(" \t \t f(\"\t\t\"); /* some\n"
14475                    " \t \t    comment */",
14476                    Tab));
14477   EXPECT_EQ("{\n"
14478             "\t/*\n"
14479             "\t * Comment\n"
14480             "\t */\n"
14481             "\tint i;\n"
14482             "}",
14483             format("{\n"
14484                    "\t/*\n"
14485                    "\t * Comment\n"
14486                    "\t */\n"
14487                    "\t int i;\n"
14488                    "}",
14489                    Tab));
14490   Tab.TabWidth = 2;
14491   Tab.IndentWidth = 2;
14492   EXPECT_EQ("{\n"
14493             "\t/* aaaa\n"
14494             "\t   bbbb */\n"
14495             "}",
14496             format("{\n"
14497                    "/* aaaa\n"
14498                    "   bbbb */\n"
14499                    "}",
14500                    Tab));
14501   EXPECT_EQ("{\n"
14502             "\t/*\n"
14503             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
14504             "\t  bbbbbbbbbbbbb\n"
14505             "\t*/\n"
14506             "}",
14507             format("{\n"
14508                    "/*\n"
14509                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
14510                    "*/\n"
14511                    "}",
14512                    Tab));
14513   Tab.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
14514   Tab.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
14515   Tab.TabWidth = 4;
14516   Tab.IndentWidth = 4;
14517   verifyFormat("class Assign {\n"
14518                "\tvoid f() {\n"
14519                "\t\tint         x      = 123;\n"
14520                "\t\tint         random = 4;\n"
14521                "\t\tstd::string alphabet =\n"
14522                "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
14523                "\t}\n"
14524                "};",
14525                Tab);
14526   Tab.AlignOperands = FormatStyle::OAS_Align;
14527   verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb +\n"
14528                "                 cccccccccccccccccccc;",
14529                Tab);
14530   // no alignment
14531   verifyFormat("int aaaaaaaaaa =\n"
14532                "\tbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
14533                Tab);
14534   verifyFormat("return aaaaaaaaaaaaaaaa ? 111111111111111\n"
14535                "       : bbbbbbbbbbbbbb ? 222222222222222\n"
14536                "                        : 333333333333333;",
14537                Tab);
14538   Tab.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
14539   Tab.AlignOperands = FormatStyle::OAS_AlignAfterOperator;
14540   verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb\n"
14541                "               + cccccccccccccccccccc;",
14542                Tab);
14543 }
14544 
14545 TEST_F(FormatTest, ZeroTabWidth) {
14546   FormatStyle Tab = getLLVMStyleWithColumns(42);
14547   Tab.IndentWidth = 8;
14548   Tab.UseTab = FormatStyle::UT_Never;
14549   Tab.TabWidth = 0;
14550   EXPECT_EQ("void a(){\n"
14551             "    // line starts with '\t'\n"
14552             "};",
14553             format("void a(){\n"
14554                    "\t// line starts with '\t'\n"
14555                    "};",
14556                    Tab));
14557 
14558   EXPECT_EQ("void a(){\n"
14559             "    // line starts with '\t'\n"
14560             "};",
14561             format("void a(){\n"
14562                    "\t\t// line starts with '\t'\n"
14563                    "};",
14564                    Tab));
14565 
14566   Tab.UseTab = FormatStyle::UT_ForIndentation;
14567   EXPECT_EQ("void a(){\n"
14568             "    // line starts with '\t'\n"
14569             "};",
14570             format("void a(){\n"
14571                    "\t// line starts with '\t'\n"
14572                    "};",
14573                    Tab));
14574 
14575   EXPECT_EQ("void a(){\n"
14576             "    // line starts with '\t'\n"
14577             "};",
14578             format("void a(){\n"
14579                    "\t\t// line starts with '\t'\n"
14580                    "};",
14581                    Tab));
14582 
14583   Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
14584   EXPECT_EQ("void a(){\n"
14585             "    // line starts with '\t'\n"
14586             "};",
14587             format("void a(){\n"
14588                    "\t// line starts with '\t'\n"
14589                    "};",
14590                    Tab));
14591 
14592   EXPECT_EQ("void a(){\n"
14593             "    // line starts with '\t'\n"
14594             "};",
14595             format("void a(){\n"
14596                    "\t\t// line starts with '\t'\n"
14597                    "};",
14598                    Tab));
14599 
14600   Tab.UseTab = FormatStyle::UT_AlignWithSpaces;
14601   EXPECT_EQ("void a(){\n"
14602             "    // line starts with '\t'\n"
14603             "};",
14604             format("void a(){\n"
14605                    "\t// line starts with '\t'\n"
14606                    "};",
14607                    Tab));
14608 
14609   EXPECT_EQ("void a(){\n"
14610             "    // line starts with '\t'\n"
14611             "};",
14612             format("void a(){\n"
14613                    "\t\t// line starts with '\t'\n"
14614                    "};",
14615                    Tab));
14616 
14617   Tab.UseTab = FormatStyle::UT_Always;
14618   EXPECT_EQ("void a(){\n"
14619             "// line starts with '\t'\n"
14620             "};",
14621             format("void a(){\n"
14622                    "\t// line starts with '\t'\n"
14623                    "};",
14624                    Tab));
14625 
14626   EXPECT_EQ("void a(){\n"
14627             "// line starts with '\t'\n"
14628             "};",
14629             format("void a(){\n"
14630                    "\t\t// line starts with '\t'\n"
14631                    "};",
14632                    Tab));
14633 }
14634 
14635 TEST_F(FormatTest, CalculatesOriginalColumn) {
14636   EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
14637             "q\"; /* some\n"
14638             "       comment */",
14639             format("  \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
14640                    "q\"; /* some\n"
14641                    "       comment */",
14642                    getLLVMStyle()));
14643   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
14644             "/* some\n"
14645             "   comment */",
14646             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
14647                    " /* some\n"
14648                    "    comment */",
14649                    getLLVMStyle()));
14650   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
14651             "qqq\n"
14652             "/* some\n"
14653             "   comment */",
14654             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
14655                    "qqq\n"
14656                    " /* some\n"
14657                    "    comment */",
14658                    getLLVMStyle()));
14659   EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
14660             "wwww; /* some\n"
14661             "         comment */",
14662             format("  inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
14663                    "wwww; /* some\n"
14664                    "         comment */",
14665                    getLLVMStyle()));
14666 }
14667 
14668 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
14669   FormatStyle NoSpace = getLLVMStyle();
14670   NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
14671 
14672   verifyFormat("while(true)\n"
14673                "  continue;",
14674                NoSpace);
14675   verifyFormat("for(;;)\n"
14676                "  continue;",
14677                NoSpace);
14678   verifyFormat("if(true)\n"
14679                "  f();\n"
14680                "else if(true)\n"
14681                "  f();",
14682                NoSpace);
14683   verifyFormat("do {\n"
14684                "  do_something();\n"
14685                "} while(something());",
14686                NoSpace);
14687   verifyFormat("switch(x) {\n"
14688                "default:\n"
14689                "  break;\n"
14690                "}",
14691                NoSpace);
14692   verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
14693   verifyFormat("size_t x = sizeof(x);", NoSpace);
14694   verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
14695   verifyFormat("auto f(int x) -> typeof(x);", NoSpace);
14696   verifyFormat("auto f(int x) -> _Atomic(x);", NoSpace);
14697   verifyFormat("auto f(int x) -> __underlying_type(x);", NoSpace);
14698   verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
14699   verifyFormat("alignas(128) char a[128];", NoSpace);
14700   verifyFormat("size_t x = alignof(MyType);", NoSpace);
14701   verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
14702   verifyFormat("int f() throw(Deprecated);", NoSpace);
14703   verifyFormat("typedef void (*cb)(int);", NoSpace);
14704   verifyFormat("T A::operator()();", NoSpace);
14705   verifyFormat("X A::operator++(T);", NoSpace);
14706   verifyFormat("auto lambda = []() { return 0; };", NoSpace);
14707 
14708   FormatStyle Space = getLLVMStyle();
14709   Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
14710 
14711   verifyFormat("int f ();", Space);
14712   verifyFormat("void f (int a, T b) {\n"
14713                "  while (true)\n"
14714                "    continue;\n"
14715                "}",
14716                Space);
14717   verifyFormat("if (true)\n"
14718                "  f ();\n"
14719                "else if (true)\n"
14720                "  f ();",
14721                Space);
14722   verifyFormat("do {\n"
14723                "  do_something ();\n"
14724                "} while (something ());",
14725                Space);
14726   verifyFormat("switch (x) {\n"
14727                "default:\n"
14728                "  break;\n"
14729                "}",
14730                Space);
14731   verifyFormat("A::A () : a (1) {}", Space);
14732   verifyFormat("void f () __attribute__ ((asdf));", Space);
14733   verifyFormat("*(&a + 1);\n"
14734                "&((&a)[1]);\n"
14735                "a[(b + c) * d];\n"
14736                "(((a + 1) * 2) + 3) * 4;",
14737                Space);
14738   verifyFormat("#define A(x) x", Space);
14739   verifyFormat("#define A (x) x", Space);
14740   verifyFormat("#if defined(x)\n"
14741                "#endif",
14742                Space);
14743   verifyFormat("auto i = std::make_unique<int> (5);", Space);
14744   verifyFormat("size_t x = sizeof (x);", Space);
14745   verifyFormat("auto f (int x) -> decltype (x);", Space);
14746   verifyFormat("auto f (int x) -> typeof (x);", Space);
14747   verifyFormat("auto f (int x) -> _Atomic (x);", Space);
14748   verifyFormat("auto f (int x) -> __underlying_type (x);", Space);
14749   verifyFormat("int f (T x) noexcept (x.create ());", Space);
14750   verifyFormat("alignas (128) char a[128];", Space);
14751   verifyFormat("size_t x = alignof (MyType);", Space);
14752   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
14753   verifyFormat("int f () throw (Deprecated);", Space);
14754   verifyFormat("typedef void (*cb) (int);", Space);
14755   // FIXME these tests regressed behaviour.
14756   // verifyFormat("T A::operator() ();", Space);
14757   // verifyFormat("X A::operator++ (T);", Space);
14758   verifyFormat("auto lambda = [] () { return 0; };", Space);
14759   verifyFormat("int x = int (y);", Space);
14760 
14761   FormatStyle SomeSpace = getLLVMStyle();
14762   SomeSpace.SpaceBeforeParens = FormatStyle::SBPO_NonEmptyParentheses;
14763 
14764   verifyFormat("[]() -> float {}", SomeSpace);
14765   verifyFormat("[] (auto foo) {}", SomeSpace);
14766   verifyFormat("[foo]() -> int {}", SomeSpace);
14767   verifyFormat("int f();", SomeSpace);
14768   verifyFormat("void f (int a, T b) {\n"
14769                "  while (true)\n"
14770                "    continue;\n"
14771                "}",
14772                SomeSpace);
14773   verifyFormat("if (true)\n"
14774                "  f();\n"
14775                "else if (true)\n"
14776                "  f();",
14777                SomeSpace);
14778   verifyFormat("do {\n"
14779                "  do_something();\n"
14780                "} while (something());",
14781                SomeSpace);
14782   verifyFormat("switch (x) {\n"
14783                "default:\n"
14784                "  break;\n"
14785                "}",
14786                SomeSpace);
14787   verifyFormat("A::A() : a (1) {}", SomeSpace);
14788   verifyFormat("void f() __attribute__ ((asdf));", SomeSpace);
14789   verifyFormat("*(&a + 1);\n"
14790                "&((&a)[1]);\n"
14791                "a[(b + c) * d];\n"
14792                "(((a + 1) * 2) + 3) * 4;",
14793                SomeSpace);
14794   verifyFormat("#define A(x) x", SomeSpace);
14795   verifyFormat("#define A (x) x", SomeSpace);
14796   verifyFormat("#if defined(x)\n"
14797                "#endif",
14798                SomeSpace);
14799   verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace);
14800   verifyFormat("size_t x = sizeof (x);", SomeSpace);
14801   verifyFormat("auto f (int x) -> decltype (x);", SomeSpace);
14802   verifyFormat("auto f (int x) -> typeof (x);", SomeSpace);
14803   verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace);
14804   verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace);
14805   verifyFormat("int f (T x) noexcept (x.create());", SomeSpace);
14806   verifyFormat("alignas (128) char a[128];", SomeSpace);
14807   verifyFormat("size_t x = alignof (MyType);", SomeSpace);
14808   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");",
14809                SomeSpace);
14810   verifyFormat("int f() throw (Deprecated);", SomeSpace);
14811   verifyFormat("typedef void (*cb) (int);", SomeSpace);
14812   verifyFormat("T A::operator()();", SomeSpace);
14813   // FIXME these tests regressed behaviour.
14814   // verifyFormat("X A::operator++ (T);", SomeSpace);
14815   verifyFormat("int x = int (y);", SomeSpace);
14816   verifyFormat("auto lambda = []() { return 0; };", SomeSpace);
14817 
14818   FormatStyle SpaceControlStatements = getLLVMStyle();
14819   SpaceControlStatements.SpaceBeforeParens = FormatStyle::SBPO_Custom;
14820   SpaceControlStatements.SpaceBeforeParensOptions.AfterControlStatements = true;
14821 
14822   verifyFormat("while (true)\n"
14823                "  continue;",
14824                SpaceControlStatements);
14825   verifyFormat("if (true)\n"
14826                "  f();\n"
14827                "else if (true)\n"
14828                "  f();",
14829                SpaceControlStatements);
14830   verifyFormat("for (;;) {\n"
14831                "  do_something();\n"
14832                "}",
14833                SpaceControlStatements);
14834   verifyFormat("do {\n"
14835                "  do_something();\n"
14836                "} while (something());",
14837                SpaceControlStatements);
14838   verifyFormat("switch (x) {\n"
14839                "default:\n"
14840                "  break;\n"
14841                "}",
14842                SpaceControlStatements);
14843 
14844   FormatStyle SpaceFuncDecl = getLLVMStyle();
14845   SpaceFuncDecl.SpaceBeforeParens = FormatStyle::SBPO_Custom;
14846   SpaceFuncDecl.SpaceBeforeParensOptions.AfterFunctionDeclarationName = true;
14847 
14848   verifyFormat("int f ();", SpaceFuncDecl);
14849   verifyFormat("void f(int a, T b) {}", SpaceFuncDecl);
14850   verifyFormat("A::A() : a(1) {}", SpaceFuncDecl);
14851   verifyFormat("void f () __attribute__((asdf));", SpaceFuncDecl);
14852   verifyFormat("#define A(x) x", SpaceFuncDecl);
14853   verifyFormat("#define A (x) x", SpaceFuncDecl);
14854   verifyFormat("#if defined(x)\n"
14855                "#endif",
14856                SpaceFuncDecl);
14857   verifyFormat("auto i = std::make_unique<int>(5);", SpaceFuncDecl);
14858   verifyFormat("size_t x = sizeof(x);", SpaceFuncDecl);
14859   verifyFormat("auto f (int x) -> decltype(x);", SpaceFuncDecl);
14860   verifyFormat("auto f (int x) -> typeof(x);", SpaceFuncDecl);
14861   verifyFormat("auto f (int x) -> _Atomic(x);", SpaceFuncDecl);
14862   verifyFormat("auto f (int x) -> __underlying_type(x);", SpaceFuncDecl);
14863   verifyFormat("int f (T x) noexcept(x.create());", SpaceFuncDecl);
14864   verifyFormat("alignas(128) char a[128];", SpaceFuncDecl);
14865   verifyFormat("size_t x = alignof(MyType);", SpaceFuncDecl);
14866   verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");",
14867                SpaceFuncDecl);
14868   verifyFormat("int f () throw(Deprecated);", SpaceFuncDecl);
14869   verifyFormat("typedef void (*cb)(int);", SpaceFuncDecl);
14870   // FIXME these tests regressed behaviour.
14871   // verifyFormat("T A::operator() ();", SpaceFuncDecl);
14872   // verifyFormat("X A::operator++ (T);", SpaceFuncDecl);
14873   verifyFormat("T A::operator()() {}", SpaceFuncDecl);
14874   verifyFormat("auto lambda = []() { return 0; };", SpaceFuncDecl);
14875   verifyFormat("int x = int(y);", SpaceFuncDecl);
14876   verifyFormat("M(std::size_t R, std::size_t C) : C(C), data(R) {}",
14877                SpaceFuncDecl);
14878 
14879   FormatStyle SpaceFuncDef = getLLVMStyle();
14880   SpaceFuncDef.SpaceBeforeParens = FormatStyle::SBPO_Custom;
14881   SpaceFuncDef.SpaceBeforeParensOptions.AfterFunctionDefinitionName = true;
14882 
14883   verifyFormat("int f();", SpaceFuncDef);
14884   verifyFormat("void f (int a, T b) {}", SpaceFuncDef);
14885   verifyFormat("A::A() : a(1) {}", SpaceFuncDef);
14886   verifyFormat("void f() __attribute__((asdf));", SpaceFuncDef);
14887   verifyFormat("#define A(x) x", SpaceFuncDef);
14888   verifyFormat("#define A (x) x", SpaceFuncDef);
14889   verifyFormat("#if defined(x)\n"
14890                "#endif",
14891                SpaceFuncDef);
14892   verifyFormat("auto i = std::make_unique<int>(5);", SpaceFuncDef);
14893   verifyFormat("size_t x = sizeof(x);", SpaceFuncDef);
14894   verifyFormat("auto f(int x) -> decltype(x);", SpaceFuncDef);
14895   verifyFormat("auto f(int x) -> typeof(x);", SpaceFuncDef);
14896   verifyFormat("auto f(int x) -> _Atomic(x);", SpaceFuncDef);
14897   verifyFormat("auto f(int x) -> __underlying_type(x);", SpaceFuncDef);
14898   verifyFormat("int f(T x) noexcept(x.create());", SpaceFuncDef);
14899   verifyFormat("alignas(128) char a[128];", SpaceFuncDef);
14900   verifyFormat("size_t x = alignof(MyType);", SpaceFuncDef);
14901   verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");",
14902                SpaceFuncDef);
14903   verifyFormat("int f() throw(Deprecated);", SpaceFuncDef);
14904   verifyFormat("typedef void (*cb)(int);", SpaceFuncDef);
14905   verifyFormat("T A::operator()();", SpaceFuncDef);
14906   verifyFormat("X A::operator++(T);", SpaceFuncDef);
14907   // verifyFormat("T A::operator() () {}", SpaceFuncDef);
14908   verifyFormat("auto lambda = [] () { return 0; };", SpaceFuncDef);
14909   verifyFormat("int x = int(y);", SpaceFuncDef);
14910   verifyFormat("M(std::size_t R, std::size_t C) : C(C), data(R) {}",
14911                SpaceFuncDef);
14912 
14913   FormatStyle SpaceIfMacros = getLLVMStyle();
14914   SpaceIfMacros.IfMacros.clear();
14915   SpaceIfMacros.IfMacros.push_back("MYIF");
14916   SpaceIfMacros.SpaceBeforeParens = FormatStyle::SBPO_Custom;
14917   SpaceIfMacros.SpaceBeforeParensOptions.AfterIfMacros = true;
14918   verifyFormat("MYIF (a)\n  return;", SpaceIfMacros);
14919   verifyFormat("MYIF (a)\n  return;\nelse MYIF (b)\n  return;", SpaceIfMacros);
14920   verifyFormat("MYIF (a)\n  return;\nelse\n  return;", SpaceIfMacros);
14921 
14922   FormatStyle SpaceForeachMacros = getLLVMStyle();
14923   EXPECT_EQ(SpaceForeachMacros.AllowShortBlocksOnASingleLine,
14924             FormatStyle::SBS_Never);
14925   EXPECT_EQ(SpaceForeachMacros.AllowShortLoopsOnASingleLine, false);
14926   SpaceForeachMacros.SpaceBeforeParens = FormatStyle::SBPO_Custom;
14927   SpaceForeachMacros.SpaceBeforeParensOptions.AfterForeachMacros = true;
14928   verifyFormat("for (;;) {\n"
14929                "}",
14930                SpaceForeachMacros);
14931   verifyFormat("foreach (Item *item, itemlist) {\n"
14932                "}",
14933                SpaceForeachMacros);
14934   verifyFormat("Q_FOREACH (Item *item, itemlist) {\n"
14935                "}",
14936                SpaceForeachMacros);
14937   verifyFormat("BOOST_FOREACH (Item *item, itemlist) {\n"
14938                "}",
14939                SpaceForeachMacros);
14940   verifyFormat("UNKNOWN_FOREACH(Item *item, itemlist) {}", SpaceForeachMacros);
14941 
14942   FormatStyle SomeSpace2 = getLLVMStyle();
14943   SomeSpace2.SpaceBeforeParens = FormatStyle::SBPO_Custom;
14944   SomeSpace2.SpaceBeforeParensOptions.BeforeNonEmptyParentheses = true;
14945   verifyFormat("[]() -> float {}", SomeSpace2);
14946   verifyFormat("[] (auto foo) {}", SomeSpace2);
14947   verifyFormat("[foo]() -> int {}", SomeSpace2);
14948   verifyFormat("int f();", SomeSpace2);
14949   verifyFormat("void f (int a, T b) {\n"
14950                "  while (true)\n"
14951                "    continue;\n"
14952                "}",
14953                SomeSpace2);
14954   verifyFormat("if (true)\n"
14955                "  f();\n"
14956                "else if (true)\n"
14957                "  f();",
14958                SomeSpace2);
14959   verifyFormat("do {\n"
14960                "  do_something();\n"
14961                "} while (something());",
14962                SomeSpace2);
14963   verifyFormat("switch (x) {\n"
14964                "default:\n"
14965                "  break;\n"
14966                "}",
14967                SomeSpace2);
14968   verifyFormat("A::A() : a (1) {}", SomeSpace2);
14969   verifyFormat("void f() __attribute__ ((asdf));", SomeSpace2);
14970   verifyFormat("*(&a + 1);\n"
14971                "&((&a)[1]);\n"
14972                "a[(b + c) * d];\n"
14973                "(((a + 1) * 2) + 3) * 4;",
14974                SomeSpace2);
14975   verifyFormat("#define A(x) x", SomeSpace2);
14976   verifyFormat("#define A (x) x", SomeSpace2);
14977   verifyFormat("#if defined(x)\n"
14978                "#endif",
14979                SomeSpace2);
14980   verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace2);
14981   verifyFormat("size_t x = sizeof (x);", SomeSpace2);
14982   verifyFormat("auto f (int x) -> decltype (x);", SomeSpace2);
14983   verifyFormat("auto f (int x) -> typeof (x);", SomeSpace2);
14984   verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace2);
14985   verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace2);
14986   verifyFormat("int f (T x) noexcept (x.create());", SomeSpace2);
14987   verifyFormat("alignas (128) char a[128];", SomeSpace2);
14988   verifyFormat("size_t x = alignof (MyType);", SomeSpace2);
14989   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");",
14990                SomeSpace2);
14991   verifyFormat("int f() throw (Deprecated);", SomeSpace2);
14992   verifyFormat("typedef void (*cb) (int);", SomeSpace2);
14993   verifyFormat("T A::operator()();", SomeSpace2);
14994   // verifyFormat("X A::operator++ (T);", SomeSpace2);
14995   verifyFormat("int x = int (y);", SomeSpace2);
14996   verifyFormat("auto lambda = []() { return 0; };", SomeSpace2);
14997 
14998   FormatStyle SpaceAfterOverloadedOperator = getLLVMStyle();
14999   SpaceAfterOverloadedOperator.SpaceBeforeParens = FormatStyle::SBPO_Custom;
15000   SpaceAfterOverloadedOperator.SpaceBeforeParensOptions
15001       .AfterOverloadedOperator = true;
15002 
15003   verifyFormat("auto operator++ () -> int;", SpaceAfterOverloadedOperator);
15004   verifyFormat("X A::operator++ ();", SpaceAfterOverloadedOperator);
15005   verifyFormat("some_object.operator++ ();", SpaceAfterOverloadedOperator);
15006   verifyFormat("auto func() -> int;", SpaceAfterOverloadedOperator);
15007 
15008   SpaceAfterOverloadedOperator.SpaceBeforeParensOptions
15009       .AfterOverloadedOperator = false;
15010 
15011   verifyFormat("auto operator++() -> int;", SpaceAfterOverloadedOperator);
15012   verifyFormat("X A::operator++();", SpaceAfterOverloadedOperator);
15013   verifyFormat("some_object.operator++();", SpaceAfterOverloadedOperator);
15014   verifyFormat("auto func() -> int;", SpaceAfterOverloadedOperator);
15015 }
15016 
15017 TEST_F(FormatTest, SpaceAfterLogicalNot) {
15018   FormatStyle Spaces = getLLVMStyle();
15019   Spaces.SpaceAfterLogicalNot = true;
15020 
15021   verifyFormat("bool x = ! y", Spaces);
15022   verifyFormat("if (! isFailure())", Spaces);
15023   verifyFormat("if (! (a && b))", Spaces);
15024   verifyFormat("\"Error!\"", Spaces);
15025   verifyFormat("! ! x", Spaces);
15026 }
15027 
15028 TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
15029   FormatStyle Spaces = getLLVMStyle();
15030 
15031   Spaces.SpacesInParentheses = true;
15032   verifyFormat("do_something( ::globalVar );", Spaces);
15033   verifyFormat("call( x, y, z );", Spaces);
15034   verifyFormat("call();", Spaces);
15035   verifyFormat("std::function<void( int, int )> callback;", Spaces);
15036   verifyFormat("void inFunction() { std::function<void( int, int )> fct; }",
15037                Spaces);
15038   verifyFormat("while ( (bool)1 )\n"
15039                "  continue;",
15040                Spaces);
15041   verifyFormat("for ( ;; )\n"
15042                "  continue;",
15043                Spaces);
15044   verifyFormat("if ( true )\n"
15045                "  f();\n"
15046                "else if ( true )\n"
15047                "  f();",
15048                Spaces);
15049   verifyFormat("do {\n"
15050                "  do_something( (int)i );\n"
15051                "} while ( something() );",
15052                Spaces);
15053   verifyFormat("switch ( x ) {\n"
15054                "default:\n"
15055                "  break;\n"
15056                "}",
15057                Spaces);
15058 
15059   Spaces.SpacesInParentheses = false;
15060   Spaces.SpacesInCStyleCastParentheses = true;
15061   verifyFormat("Type *A = ( Type * )P;", Spaces);
15062   verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
15063   verifyFormat("x = ( int32 )y;", Spaces);
15064   verifyFormat("int a = ( int )(2.0f);", Spaces);
15065   verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
15066   verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
15067   verifyFormat("#define x (( int )-1)", Spaces);
15068 
15069   // Run the first set of tests again with:
15070   Spaces.SpacesInParentheses = false;
15071   Spaces.SpaceInEmptyParentheses = true;
15072   Spaces.SpacesInCStyleCastParentheses = true;
15073   verifyFormat("call(x, y, z);", Spaces);
15074   verifyFormat("call( );", Spaces);
15075   verifyFormat("std::function<void(int, int)> callback;", Spaces);
15076   verifyFormat("while (( bool )1)\n"
15077                "  continue;",
15078                Spaces);
15079   verifyFormat("for (;;)\n"
15080                "  continue;",
15081                Spaces);
15082   verifyFormat("if (true)\n"
15083                "  f( );\n"
15084                "else if (true)\n"
15085                "  f( );",
15086                Spaces);
15087   verifyFormat("do {\n"
15088                "  do_something(( int )i);\n"
15089                "} while (something( ));",
15090                Spaces);
15091   verifyFormat("switch (x) {\n"
15092                "default:\n"
15093                "  break;\n"
15094                "}",
15095                Spaces);
15096 
15097   // Run the first set of tests again with:
15098   Spaces.SpaceAfterCStyleCast = true;
15099   verifyFormat("call(x, y, z);", Spaces);
15100   verifyFormat("call( );", Spaces);
15101   verifyFormat("std::function<void(int, int)> callback;", Spaces);
15102   verifyFormat("while (( bool ) 1)\n"
15103                "  continue;",
15104                Spaces);
15105   verifyFormat("for (;;)\n"
15106                "  continue;",
15107                Spaces);
15108   verifyFormat("if (true)\n"
15109                "  f( );\n"
15110                "else if (true)\n"
15111                "  f( );",
15112                Spaces);
15113   verifyFormat("do {\n"
15114                "  do_something(( int ) i);\n"
15115                "} while (something( ));",
15116                Spaces);
15117   verifyFormat("switch (x) {\n"
15118                "default:\n"
15119                "  break;\n"
15120                "}",
15121                Spaces);
15122   verifyFormat("#define CONF_BOOL(x) ( bool * ) ( void * ) (x)", Spaces);
15123   verifyFormat("#define CONF_BOOL(x) ( bool * ) (x)", Spaces);
15124   verifyFormat("#define CONF_BOOL(x) ( bool ) (x)", Spaces);
15125   verifyFormat("bool *y = ( bool * ) ( void * ) (x);", Spaces);
15126   verifyFormat("bool *y = ( bool * ) (x);", Spaces);
15127 
15128   // Run subset of tests again with:
15129   Spaces.SpacesInCStyleCastParentheses = false;
15130   Spaces.SpaceAfterCStyleCast = true;
15131   verifyFormat("while ((bool) 1)\n"
15132                "  continue;",
15133                Spaces);
15134   verifyFormat("do {\n"
15135                "  do_something((int) i);\n"
15136                "} while (something( ));",
15137                Spaces);
15138 
15139   verifyFormat("size_t idx = (size_t) (ptr - ((char *) file));", Spaces);
15140   verifyFormat("size_t idx = (size_t) a;", Spaces);
15141   verifyFormat("size_t idx = (size_t) (a - 1);", Spaces);
15142   verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces);
15143   verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces);
15144   verifyFormat("size_t idx = (*foo)(a - 1);", Spaces);
15145   verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces);
15146   verifyFormat("#define CONF_BOOL(x) (bool *) (void *) (x)", Spaces);
15147   verifyFormat("#define CONF_BOOL(x) (bool *) (void *) (int) (x)", Spaces);
15148   verifyFormat("bool *y = (bool *) (void *) (x);", Spaces);
15149   verifyFormat("bool *y = (bool *) (void *) (int) (x);", Spaces);
15150   verifyFormat("bool *y = (bool *) (void *) (int) foo(x);", Spaces);
15151   Spaces.ColumnLimit = 80;
15152   Spaces.IndentWidth = 4;
15153   Spaces.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
15154   verifyFormat("void foo( ) {\n"
15155                "    size_t foo = (*(function))(\n"
15156                "        Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, "
15157                "BarrrrrrrrrrrrLong,\n"
15158                "        FoooooooooLooooong);\n"
15159                "}",
15160                Spaces);
15161   Spaces.SpaceAfterCStyleCast = false;
15162   verifyFormat("size_t idx = (size_t)(ptr - ((char *)file));", Spaces);
15163   verifyFormat("size_t idx = (size_t)a;", Spaces);
15164   verifyFormat("size_t idx = (size_t)(a - 1);", Spaces);
15165   verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces);
15166   verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces);
15167   verifyFormat("size_t idx = (*foo)(a - 1);", Spaces);
15168   verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces);
15169 
15170   verifyFormat("void foo( ) {\n"
15171                "    size_t foo = (*(function))(\n"
15172                "        Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, "
15173                "BarrrrrrrrrrrrLong,\n"
15174                "        FoooooooooLooooong);\n"
15175                "}",
15176                Spaces);
15177 }
15178 
15179 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
15180   verifyFormat("int a[5];");
15181   verifyFormat("a[3] += 42;");
15182 
15183   FormatStyle Spaces = getLLVMStyle();
15184   Spaces.SpacesInSquareBrackets = true;
15185   // Not lambdas.
15186   verifyFormat("int a[ 5 ];", Spaces);
15187   verifyFormat("a[ 3 ] += 42;", Spaces);
15188   verifyFormat("constexpr char hello[]{\"hello\"};", Spaces);
15189   verifyFormat("double &operator[](int i) { return 0; }\n"
15190                "int i;",
15191                Spaces);
15192   verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces);
15193   verifyFormat("int i = a[ a ][ a ]->f();", Spaces);
15194   verifyFormat("int i = (*b)[ a ]->f();", Spaces);
15195   // Lambdas.
15196   verifyFormat("int c = []() -> int { return 2; }();\n", Spaces);
15197   verifyFormat("return [ i, args... ] {};", Spaces);
15198   verifyFormat("int foo = [ &bar ]() {};", Spaces);
15199   verifyFormat("int foo = [ = ]() {};", Spaces);
15200   verifyFormat("int foo = [ & ]() {};", Spaces);
15201   verifyFormat("int foo = [ =, &bar ]() {};", Spaces);
15202   verifyFormat("int foo = [ &bar, = ]() {};", Spaces);
15203 }
15204 
15205 TEST_F(FormatTest, ConfigurableSpaceBeforeBrackets) {
15206   FormatStyle NoSpaceStyle = getLLVMStyle();
15207   verifyFormat("int a[5];", NoSpaceStyle);
15208   verifyFormat("a[3] += 42;", NoSpaceStyle);
15209 
15210   verifyFormat("int a[1];", NoSpaceStyle);
15211   verifyFormat("int 1 [a];", NoSpaceStyle);
15212   verifyFormat("int a[1][2];", NoSpaceStyle);
15213   verifyFormat("a[7] = 5;", NoSpaceStyle);
15214   verifyFormat("int a = (f())[23];", NoSpaceStyle);
15215   verifyFormat("f([] {})", NoSpaceStyle);
15216 
15217   FormatStyle Space = getLLVMStyle();
15218   Space.SpaceBeforeSquareBrackets = true;
15219   verifyFormat("int c = []() -> int { return 2; }();\n", Space);
15220   verifyFormat("return [i, args...] {};", Space);
15221 
15222   verifyFormat("int a [5];", Space);
15223   verifyFormat("a [3] += 42;", Space);
15224   verifyFormat("constexpr char hello []{\"hello\"};", Space);
15225   verifyFormat("double &operator[](int i) { return 0; }\n"
15226                "int i;",
15227                Space);
15228   verifyFormat("std::unique_ptr<int []> foo() {}", Space);
15229   verifyFormat("int i = a [a][a]->f();", Space);
15230   verifyFormat("int i = (*b) [a]->f();", Space);
15231 
15232   verifyFormat("int a [1];", Space);
15233   verifyFormat("int 1 [a];", Space);
15234   verifyFormat("int a [1][2];", Space);
15235   verifyFormat("a [7] = 5;", Space);
15236   verifyFormat("int a = (f()) [23];", Space);
15237   verifyFormat("f([] {})", Space);
15238 }
15239 
15240 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
15241   verifyFormat("int a = 5;");
15242   verifyFormat("a += 42;");
15243   verifyFormat("a or_eq 8;");
15244 
15245   FormatStyle Spaces = getLLVMStyle();
15246   Spaces.SpaceBeforeAssignmentOperators = false;
15247   verifyFormat("int a= 5;", Spaces);
15248   verifyFormat("a+= 42;", Spaces);
15249   verifyFormat("a or_eq 8;", Spaces);
15250 }
15251 
15252 TEST_F(FormatTest, ConfigurableSpaceBeforeColon) {
15253   verifyFormat("class Foo : public Bar {};");
15254   verifyFormat("Foo::Foo() : foo(1) {}");
15255   verifyFormat("for (auto a : b) {\n}");
15256   verifyFormat("int x = a ? b : c;");
15257   verifyFormat("{\n"
15258                "label0:\n"
15259                "  int x = 0;\n"
15260                "}");
15261   verifyFormat("switch (x) {\n"
15262                "case 1:\n"
15263                "default:\n"
15264                "}");
15265   verifyFormat("switch (allBraces) {\n"
15266                "case 1: {\n"
15267                "  break;\n"
15268                "}\n"
15269                "case 2: {\n"
15270                "  [[fallthrough]];\n"
15271                "}\n"
15272                "default: {\n"
15273                "  break;\n"
15274                "}\n"
15275                "}");
15276 
15277   FormatStyle CtorInitializerStyle = getLLVMStyleWithColumns(30);
15278   CtorInitializerStyle.SpaceBeforeCtorInitializerColon = false;
15279   verifyFormat("class Foo : public Bar {};", CtorInitializerStyle);
15280   verifyFormat("Foo::Foo(): foo(1) {}", CtorInitializerStyle);
15281   verifyFormat("for (auto a : b) {\n}", CtorInitializerStyle);
15282   verifyFormat("int x = a ? b : c;", CtorInitializerStyle);
15283   verifyFormat("{\n"
15284                "label1:\n"
15285                "  int x = 0;\n"
15286                "}",
15287                CtorInitializerStyle);
15288   verifyFormat("switch (x) {\n"
15289                "case 1:\n"
15290                "default:\n"
15291                "}",
15292                CtorInitializerStyle);
15293   verifyFormat("switch (allBraces) {\n"
15294                "case 1: {\n"
15295                "  break;\n"
15296                "}\n"
15297                "case 2: {\n"
15298                "  [[fallthrough]];\n"
15299                "}\n"
15300                "default: {\n"
15301                "  break;\n"
15302                "}\n"
15303                "}",
15304                CtorInitializerStyle);
15305   CtorInitializerStyle.BreakConstructorInitializers =
15306       FormatStyle::BCIS_AfterColon;
15307   verifyFormat("Fooooooooooo::Fooooooooooo():\n"
15308                "    aaaaaaaaaaaaaaaa(1),\n"
15309                "    bbbbbbbbbbbbbbbb(2) {}",
15310                CtorInitializerStyle);
15311   CtorInitializerStyle.BreakConstructorInitializers =
15312       FormatStyle::BCIS_BeforeComma;
15313   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
15314                "    : aaaaaaaaaaaaaaaa(1)\n"
15315                "    , bbbbbbbbbbbbbbbb(2) {}",
15316                CtorInitializerStyle);
15317   CtorInitializerStyle.BreakConstructorInitializers =
15318       FormatStyle::BCIS_BeforeColon;
15319   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
15320                "    : aaaaaaaaaaaaaaaa(1),\n"
15321                "      bbbbbbbbbbbbbbbb(2) {}",
15322                CtorInitializerStyle);
15323   CtorInitializerStyle.ConstructorInitializerIndentWidth = 0;
15324   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
15325                ": aaaaaaaaaaaaaaaa(1),\n"
15326                "  bbbbbbbbbbbbbbbb(2) {}",
15327                CtorInitializerStyle);
15328 
15329   FormatStyle InheritanceStyle = getLLVMStyleWithColumns(30);
15330   InheritanceStyle.SpaceBeforeInheritanceColon = false;
15331   verifyFormat("class Foo: public Bar {};", InheritanceStyle);
15332   verifyFormat("Foo::Foo() : foo(1) {}", InheritanceStyle);
15333   verifyFormat("for (auto a : b) {\n}", InheritanceStyle);
15334   verifyFormat("int x = a ? b : c;", InheritanceStyle);
15335   verifyFormat("{\n"
15336                "label2:\n"
15337                "  int x = 0;\n"
15338                "}",
15339                InheritanceStyle);
15340   verifyFormat("switch (x) {\n"
15341                "case 1:\n"
15342                "default:\n"
15343                "}",
15344                InheritanceStyle);
15345   verifyFormat("switch (allBraces) {\n"
15346                "case 1: {\n"
15347                "  break;\n"
15348                "}\n"
15349                "case 2: {\n"
15350                "  [[fallthrough]];\n"
15351                "}\n"
15352                "default: {\n"
15353                "  break;\n"
15354                "}\n"
15355                "}",
15356                InheritanceStyle);
15357   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterComma;
15358   verifyFormat("class Foooooooooooooooooooooo\n"
15359                "    : public aaaaaaaaaaaaaaaaaa,\n"
15360                "      public bbbbbbbbbbbbbbbbbb {\n"
15361                "}",
15362                InheritanceStyle);
15363   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterColon;
15364   verifyFormat("class Foooooooooooooooooooooo:\n"
15365                "    public aaaaaaaaaaaaaaaaaa,\n"
15366                "    public bbbbbbbbbbbbbbbbbb {\n"
15367                "}",
15368                InheritanceStyle);
15369   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeComma;
15370   verifyFormat("class Foooooooooooooooooooooo\n"
15371                "    : public aaaaaaaaaaaaaaaaaa\n"
15372                "    , public bbbbbbbbbbbbbbbbbb {\n"
15373                "}",
15374                InheritanceStyle);
15375   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
15376   verifyFormat("class Foooooooooooooooooooooo\n"
15377                "    : public aaaaaaaaaaaaaaaaaa,\n"
15378                "      public bbbbbbbbbbbbbbbbbb {\n"
15379                "}",
15380                InheritanceStyle);
15381   InheritanceStyle.ConstructorInitializerIndentWidth = 0;
15382   verifyFormat("class Foooooooooooooooooooooo\n"
15383                ": public aaaaaaaaaaaaaaaaaa,\n"
15384                "  public bbbbbbbbbbbbbbbbbb {}",
15385                InheritanceStyle);
15386 
15387   FormatStyle ForLoopStyle = getLLVMStyle();
15388   ForLoopStyle.SpaceBeforeRangeBasedForLoopColon = false;
15389   verifyFormat("class Foo : public Bar {};", ForLoopStyle);
15390   verifyFormat("Foo::Foo() : foo(1) {}", ForLoopStyle);
15391   verifyFormat("for (auto a: b) {\n}", ForLoopStyle);
15392   verifyFormat("int x = a ? b : c;", ForLoopStyle);
15393   verifyFormat("{\n"
15394                "label2:\n"
15395                "  int x = 0;\n"
15396                "}",
15397                ForLoopStyle);
15398   verifyFormat("switch (x) {\n"
15399                "case 1:\n"
15400                "default:\n"
15401                "}",
15402                ForLoopStyle);
15403   verifyFormat("switch (allBraces) {\n"
15404                "case 1: {\n"
15405                "  break;\n"
15406                "}\n"
15407                "case 2: {\n"
15408                "  [[fallthrough]];\n"
15409                "}\n"
15410                "default: {\n"
15411                "  break;\n"
15412                "}\n"
15413                "}",
15414                ForLoopStyle);
15415 
15416   FormatStyle CaseStyle = getLLVMStyle();
15417   CaseStyle.SpaceBeforeCaseColon = true;
15418   verifyFormat("class Foo : public Bar {};", CaseStyle);
15419   verifyFormat("Foo::Foo() : foo(1) {}", CaseStyle);
15420   verifyFormat("for (auto a : b) {\n}", CaseStyle);
15421   verifyFormat("int x = a ? b : c;", CaseStyle);
15422   verifyFormat("switch (x) {\n"
15423                "case 1 :\n"
15424                "default :\n"
15425                "}",
15426                CaseStyle);
15427   verifyFormat("switch (allBraces) {\n"
15428                "case 1 : {\n"
15429                "  break;\n"
15430                "}\n"
15431                "case 2 : {\n"
15432                "  [[fallthrough]];\n"
15433                "}\n"
15434                "default : {\n"
15435                "  break;\n"
15436                "}\n"
15437                "}",
15438                CaseStyle);
15439 
15440   FormatStyle NoSpaceStyle = getLLVMStyle();
15441   EXPECT_EQ(NoSpaceStyle.SpaceBeforeCaseColon, false);
15442   NoSpaceStyle.SpaceBeforeCtorInitializerColon = false;
15443   NoSpaceStyle.SpaceBeforeInheritanceColon = false;
15444   NoSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false;
15445   verifyFormat("class Foo: public Bar {};", NoSpaceStyle);
15446   verifyFormat("Foo::Foo(): foo(1) {}", NoSpaceStyle);
15447   verifyFormat("for (auto a: b) {\n}", NoSpaceStyle);
15448   verifyFormat("int x = a ? b : c;", NoSpaceStyle);
15449   verifyFormat("{\n"
15450                "label3:\n"
15451                "  int x = 0;\n"
15452                "}",
15453                NoSpaceStyle);
15454   verifyFormat("switch (x) {\n"
15455                "case 1:\n"
15456                "default:\n"
15457                "}",
15458                NoSpaceStyle);
15459   verifyFormat("switch (allBraces) {\n"
15460                "case 1: {\n"
15461                "  break;\n"
15462                "}\n"
15463                "case 2: {\n"
15464                "  [[fallthrough]];\n"
15465                "}\n"
15466                "default: {\n"
15467                "  break;\n"
15468                "}\n"
15469                "}",
15470                NoSpaceStyle);
15471 
15472   FormatStyle InvertedSpaceStyle = getLLVMStyle();
15473   InvertedSpaceStyle.SpaceBeforeCaseColon = true;
15474   InvertedSpaceStyle.SpaceBeforeCtorInitializerColon = false;
15475   InvertedSpaceStyle.SpaceBeforeInheritanceColon = false;
15476   InvertedSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false;
15477   verifyFormat("class Foo: public Bar {};", InvertedSpaceStyle);
15478   verifyFormat("Foo::Foo(): foo(1) {}", InvertedSpaceStyle);
15479   verifyFormat("for (auto a: b) {\n}", InvertedSpaceStyle);
15480   verifyFormat("int x = a ? b : c;", InvertedSpaceStyle);
15481   verifyFormat("{\n"
15482                "label3:\n"
15483                "  int x = 0;\n"
15484                "}",
15485                InvertedSpaceStyle);
15486   verifyFormat("switch (x) {\n"
15487                "case 1 :\n"
15488                "case 2 : {\n"
15489                "  break;\n"
15490                "}\n"
15491                "default :\n"
15492                "  break;\n"
15493                "}",
15494                InvertedSpaceStyle);
15495   verifyFormat("switch (allBraces) {\n"
15496                "case 1 : {\n"
15497                "  break;\n"
15498                "}\n"
15499                "case 2 : {\n"
15500                "  [[fallthrough]];\n"
15501                "}\n"
15502                "default : {\n"
15503                "  break;\n"
15504                "}\n"
15505                "}",
15506                InvertedSpaceStyle);
15507 }
15508 
15509 TEST_F(FormatTest, ConfigurableSpaceAroundPointerQualifiers) {
15510   FormatStyle Style = getLLVMStyle();
15511 
15512   Style.PointerAlignment = FormatStyle::PAS_Left;
15513   Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default;
15514   verifyFormat("void* const* x = NULL;", Style);
15515 
15516 #define verifyQualifierSpaces(Code, Pointers, Qualifiers)                      \
15517   do {                                                                         \
15518     Style.PointerAlignment = FormatStyle::Pointers;                            \
15519     Style.SpaceAroundPointerQualifiers = FormatStyle::Qualifiers;              \
15520     verifyFormat(Code, Style);                                                 \
15521   } while (false)
15522 
15523   verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Default);
15524   verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_Default);
15525   verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Default);
15526 
15527   verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Before);
15528   verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Before);
15529   verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Before);
15530 
15531   verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_After);
15532   verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_After);
15533   verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_After);
15534 
15535   verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_Both);
15536   verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Both);
15537   verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Both);
15538 
15539   verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Default);
15540   verifyQualifierSpaces("Foo::operator void const *();", PAS_Right,
15541                         SAPQ_Default);
15542   verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
15543                         SAPQ_Default);
15544 
15545   verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Before);
15546   verifyQualifierSpaces("Foo::operator void const *();", PAS_Right,
15547                         SAPQ_Before);
15548   verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
15549                         SAPQ_Before);
15550 
15551   verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_After);
15552   verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_After);
15553   verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
15554                         SAPQ_After);
15555 
15556   verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_Both);
15557   verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_Both);
15558   verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, SAPQ_Both);
15559 
15560 #undef verifyQualifierSpaces
15561 
15562   FormatStyle Spaces = getLLVMStyle();
15563   Spaces.AttributeMacros.push_back("qualified");
15564   Spaces.PointerAlignment = FormatStyle::PAS_Right;
15565   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default;
15566   verifyFormat("SomeType *volatile *a = NULL;", Spaces);
15567   verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces);
15568   verifyFormat("std::vector<SomeType *const *> x;", Spaces);
15569   verifyFormat("std::vector<SomeType *qualified *> x;", Spaces);
15570   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
15571   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before;
15572   verifyFormat("SomeType * volatile *a = NULL;", Spaces);
15573   verifyFormat("SomeType * __attribute__((attr)) *a = NULL;", Spaces);
15574   verifyFormat("std::vector<SomeType * const *> x;", Spaces);
15575   verifyFormat("std::vector<SomeType * qualified *> x;", Spaces);
15576   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
15577 
15578   // Check that SAPQ_Before doesn't result in extra spaces for PAS_Left.
15579   Spaces.PointerAlignment = FormatStyle::PAS_Left;
15580   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before;
15581   verifyFormat("SomeType* volatile* a = NULL;", Spaces);
15582   verifyFormat("SomeType* __attribute__((attr))* a = NULL;", Spaces);
15583   verifyFormat("std::vector<SomeType* const*> x;", Spaces);
15584   verifyFormat("std::vector<SomeType* qualified*> x;", Spaces);
15585   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
15586   // However, setting it to SAPQ_After should add spaces after __attribute, etc.
15587   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After;
15588   verifyFormat("SomeType* volatile * a = NULL;", Spaces);
15589   verifyFormat("SomeType* __attribute__((attr)) * a = NULL;", Spaces);
15590   verifyFormat("std::vector<SomeType* const *> x;", Spaces);
15591   verifyFormat("std::vector<SomeType* qualified *> x;", Spaces);
15592   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
15593 
15594   // PAS_Middle should not have any noticeable changes even for SAPQ_Both
15595   Spaces.PointerAlignment = FormatStyle::PAS_Middle;
15596   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After;
15597   verifyFormat("SomeType * volatile * a = NULL;", Spaces);
15598   verifyFormat("SomeType * __attribute__((attr)) * a = NULL;", Spaces);
15599   verifyFormat("std::vector<SomeType * const *> x;", Spaces);
15600   verifyFormat("std::vector<SomeType * qualified *> x;", Spaces);
15601   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
15602 }
15603 
15604 TEST_F(FormatTest, AlignConsecutiveMacros) {
15605   FormatStyle Style = getLLVMStyle();
15606   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
15607   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
15608   Style.AlignConsecutiveMacros = FormatStyle::ACS_None;
15609 
15610   verifyFormat("#define a 3\n"
15611                "#define bbbb 4\n"
15612                "#define ccc (5)",
15613                Style);
15614 
15615   verifyFormat("#define f(x) (x * x)\n"
15616                "#define fff(x, y, z) (x * y + z)\n"
15617                "#define ffff(x, y) (x - y)",
15618                Style);
15619 
15620   verifyFormat("#define foo(x, y) (x + y)\n"
15621                "#define bar (5, 6)(2 + 2)",
15622                Style);
15623 
15624   verifyFormat("#define a 3\n"
15625                "#define bbbb 4\n"
15626                "#define ccc (5)\n"
15627                "#define f(x) (x * x)\n"
15628                "#define fff(x, y, z) (x * y + z)\n"
15629                "#define ffff(x, y) (x - y)",
15630                Style);
15631 
15632   Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
15633   verifyFormat("#define a    3\n"
15634                "#define bbbb 4\n"
15635                "#define ccc  (5)",
15636                Style);
15637 
15638   verifyFormat("#define f(x)         (x * x)\n"
15639                "#define fff(x, y, z) (x * y + z)\n"
15640                "#define ffff(x, y)   (x - y)",
15641                Style);
15642 
15643   verifyFormat("#define foo(x, y) (x + y)\n"
15644                "#define bar       (5, 6)(2 + 2)",
15645                Style);
15646 
15647   verifyFormat("#define a            3\n"
15648                "#define bbbb         4\n"
15649                "#define ccc          (5)\n"
15650                "#define f(x)         (x * x)\n"
15651                "#define fff(x, y, z) (x * y + z)\n"
15652                "#define ffff(x, y)   (x - y)",
15653                Style);
15654 
15655   verifyFormat("#define a         5\n"
15656                "#define foo(x, y) (x + y)\n"
15657                "#define CCC       (6)\n"
15658                "auto lambda = []() {\n"
15659                "  auto  ii = 0;\n"
15660                "  float j  = 0;\n"
15661                "  return 0;\n"
15662                "};\n"
15663                "int   i  = 0;\n"
15664                "float i2 = 0;\n"
15665                "auto  v  = type{\n"
15666                "    i = 1,   //\n"
15667                "    (i = 2), //\n"
15668                "    i = 3    //\n"
15669                "};",
15670                Style);
15671 
15672   Style.AlignConsecutiveMacros = FormatStyle::ACS_None;
15673   Style.ColumnLimit = 20;
15674 
15675   verifyFormat("#define a          \\\n"
15676                "  \"aabbbbbbbbbbbb\"\n"
15677                "#define D          \\\n"
15678                "  \"aabbbbbbbbbbbb\" \\\n"
15679                "  \"ccddeeeeeeeee\"\n"
15680                "#define B          \\\n"
15681                "  \"QQQQQQQQQQQQQ\"  \\\n"
15682                "  \"FFFFFFFFFFFFF\"  \\\n"
15683                "  \"LLLLLLLL\"\n",
15684                Style);
15685 
15686   Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
15687   verifyFormat("#define a          \\\n"
15688                "  \"aabbbbbbbbbbbb\"\n"
15689                "#define D          \\\n"
15690                "  \"aabbbbbbbbbbbb\" \\\n"
15691                "  \"ccddeeeeeeeee\"\n"
15692                "#define B          \\\n"
15693                "  \"QQQQQQQQQQQQQ\"  \\\n"
15694                "  \"FFFFFFFFFFFFF\"  \\\n"
15695                "  \"LLLLLLLL\"\n",
15696                Style);
15697 
15698   // Test across comments
15699   Style.MaxEmptyLinesToKeep = 10;
15700   Style.ReflowComments = false;
15701   Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossComments;
15702   EXPECT_EQ("#define a    3\n"
15703             "// line comment\n"
15704             "#define bbbb 4\n"
15705             "#define ccc  (5)",
15706             format("#define a 3\n"
15707                    "// line comment\n"
15708                    "#define bbbb 4\n"
15709                    "#define ccc (5)",
15710                    Style));
15711 
15712   EXPECT_EQ("#define a    3\n"
15713             "/* block comment */\n"
15714             "#define bbbb 4\n"
15715             "#define ccc  (5)",
15716             format("#define a  3\n"
15717                    "/* block comment */\n"
15718                    "#define bbbb 4\n"
15719                    "#define ccc (5)",
15720                    Style));
15721 
15722   EXPECT_EQ("#define a    3\n"
15723             "/* multi-line *\n"
15724             " * block comment */\n"
15725             "#define bbbb 4\n"
15726             "#define ccc  (5)",
15727             format("#define a 3\n"
15728                    "/* multi-line *\n"
15729                    " * block comment */\n"
15730                    "#define bbbb 4\n"
15731                    "#define ccc (5)",
15732                    Style));
15733 
15734   EXPECT_EQ("#define a    3\n"
15735             "// multi-line line comment\n"
15736             "//\n"
15737             "#define bbbb 4\n"
15738             "#define ccc  (5)",
15739             format("#define a  3\n"
15740                    "// multi-line line comment\n"
15741                    "//\n"
15742                    "#define bbbb 4\n"
15743                    "#define ccc (5)",
15744                    Style));
15745 
15746   EXPECT_EQ("#define a 3\n"
15747             "// empty lines still break.\n"
15748             "\n"
15749             "#define bbbb 4\n"
15750             "#define ccc  (5)",
15751             format("#define a     3\n"
15752                    "// empty lines still break.\n"
15753                    "\n"
15754                    "#define bbbb     4\n"
15755                    "#define ccc  (5)",
15756                    Style));
15757 
15758   // Test across empty lines
15759   Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossEmptyLines;
15760   EXPECT_EQ("#define a    3\n"
15761             "\n"
15762             "#define bbbb 4\n"
15763             "#define ccc  (5)",
15764             format("#define a 3\n"
15765                    "\n"
15766                    "#define bbbb 4\n"
15767                    "#define ccc (5)",
15768                    Style));
15769 
15770   EXPECT_EQ("#define a    3\n"
15771             "\n"
15772             "\n"
15773             "\n"
15774             "#define bbbb 4\n"
15775             "#define ccc  (5)",
15776             format("#define a        3\n"
15777                    "\n"
15778                    "\n"
15779                    "\n"
15780                    "#define bbbb 4\n"
15781                    "#define ccc (5)",
15782                    Style));
15783 
15784   EXPECT_EQ("#define a 3\n"
15785             "// comments should break alignment\n"
15786             "//\n"
15787             "#define bbbb 4\n"
15788             "#define ccc  (5)",
15789             format("#define a        3\n"
15790                    "// comments should break alignment\n"
15791                    "//\n"
15792                    "#define bbbb 4\n"
15793                    "#define ccc (5)",
15794                    Style));
15795 
15796   // Test across empty lines and comments
15797   Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossEmptyLinesAndComments;
15798   verifyFormat("#define a    3\n"
15799                "\n"
15800                "// line comment\n"
15801                "#define bbbb 4\n"
15802                "#define ccc  (5)",
15803                Style);
15804 
15805   EXPECT_EQ("#define a    3\n"
15806             "\n"
15807             "\n"
15808             "/* multi-line *\n"
15809             " * block comment */\n"
15810             "\n"
15811             "\n"
15812             "#define bbbb 4\n"
15813             "#define ccc  (5)",
15814             format("#define a 3\n"
15815                    "\n"
15816                    "\n"
15817                    "/* multi-line *\n"
15818                    " * block comment */\n"
15819                    "\n"
15820                    "\n"
15821                    "#define bbbb 4\n"
15822                    "#define ccc (5)",
15823                    Style));
15824 
15825   EXPECT_EQ("#define a    3\n"
15826             "\n"
15827             "\n"
15828             "/* multi-line *\n"
15829             " * block comment */\n"
15830             "\n"
15831             "\n"
15832             "#define bbbb 4\n"
15833             "#define ccc  (5)",
15834             format("#define a 3\n"
15835                    "\n"
15836                    "\n"
15837                    "/* multi-line *\n"
15838                    " * block comment */\n"
15839                    "\n"
15840                    "\n"
15841                    "#define bbbb 4\n"
15842                    "#define ccc       (5)",
15843                    Style));
15844 }
15845 
15846 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLines) {
15847   FormatStyle Alignment = getLLVMStyle();
15848   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
15849   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_AcrossEmptyLines;
15850 
15851   Alignment.MaxEmptyLinesToKeep = 10;
15852   /* Test alignment across empty lines */
15853   EXPECT_EQ("int a           = 5;\n"
15854             "\n"
15855             "int oneTwoThree = 123;",
15856             format("int a       = 5;\n"
15857                    "\n"
15858                    "int oneTwoThree= 123;",
15859                    Alignment));
15860   EXPECT_EQ("int a           = 5;\n"
15861             "int one         = 1;\n"
15862             "\n"
15863             "int oneTwoThree = 123;",
15864             format("int a = 5;\n"
15865                    "int one = 1;\n"
15866                    "\n"
15867                    "int oneTwoThree = 123;",
15868                    Alignment));
15869   EXPECT_EQ("int a           = 5;\n"
15870             "int one         = 1;\n"
15871             "\n"
15872             "int oneTwoThree = 123;\n"
15873             "int oneTwo      = 12;",
15874             format("int a = 5;\n"
15875                    "int one = 1;\n"
15876                    "\n"
15877                    "int oneTwoThree = 123;\n"
15878                    "int oneTwo = 12;",
15879                    Alignment));
15880 
15881   /* Test across comments */
15882   EXPECT_EQ("int a = 5;\n"
15883             "/* block comment */\n"
15884             "int oneTwoThree = 123;",
15885             format("int a = 5;\n"
15886                    "/* block comment */\n"
15887                    "int oneTwoThree=123;",
15888                    Alignment));
15889 
15890   EXPECT_EQ("int a = 5;\n"
15891             "// line comment\n"
15892             "int oneTwoThree = 123;",
15893             format("int a = 5;\n"
15894                    "// line comment\n"
15895                    "int oneTwoThree=123;",
15896                    Alignment));
15897 
15898   /* Test across comments and newlines */
15899   EXPECT_EQ("int a = 5;\n"
15900             "\n"
15901             "/* block comment */\n"
15902             "int oneTwoThree = 123;",
15903             format("int a = 5;\n"
15904                    "\n"
15905                    "/* block comment */\n"
15906                    "int oneTwoThree=123;",
15907                    Alignment));
15908 
15909   EXPECT_EQ("int a = 5;\n"
15910             "\n"
15911             "// line comment\n"
15912             "int oneTwoThree = 123;",
15913             format("int a = 5;\n"
15914                    "\n"
15915                    "// line comment\n"
15916                    "int oneTwoThree=123;",
15917                    Alignment));
15918 }
15919 
15920 TEST_F(FormatTest, AlignConsecutiveDeclarationsAcrossEmptyLinesAndComments) {
15921   FormatStyle Alignment = getLLVMStyle();
15922   Alignment.AlignConsecutiveDeclarations =
15923       FormatStyle::ACS_AcrossEmptyLinesAndComments;
15924   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
15925 
15926   Alignment.MaxEmptyLinesToKeep = 10;
15927   /* Test alignment across empty lines */
15928   EXPECT_EQ("int         a = 5;\n"
15929             "\n"
15930             "float const oneTwoThree = 123;",
15931             format("int a = 5;\n"
15932                    "\n"
15933                    "float const oneTwoThree = 123;",
15934                    Alignment));
15935   EXPECT_EQ("int         a = 5;\n"
15936             "float const one = 1;\n"
15937             "\n"
15938             "int         oneTwoThree = 123;",
15939             format("int a = 5;\n"
15940                    "float const one = 1;\n"
15941                    "\n"
15942                    "int oneTwoThree = 123;",
15943                    Alignment));
15944 
15945   /* Test across comments */
15946   EXPECT_EQ("float const a = 5;\n"
15947             "/* block comment */\n"
15948             "int         oneTwoThree = 123;",
15949             format("float const a = 5;\n"
15950                    "/* block comment */\n"
15951                    "int oneTwoThree=123;",
15952                    Alignment));
15953 
15954   EXPECT_EQ("float const a = 5;\n"
15955             "// line comment\n"
15956             "int         oneTwoThree = 123;",
15957             format("float const a = 5;\n"
15958                    "// line comment\n"
15959                    "int oneTwoThree=123;",
15960                    Alignment));
15961 
15962   /* Test across comments and newlines */
15963   EXPECT_EQ("float const a = 5;\n"
15964             "\n"
15965             "/* block comment */\n"
15966             "int         oneTwoThree = 123;",
15967             format("float const a = 5;\n"
15968                    "\n"
15969                    "/* block comment */\n"
15970                    "int         oneTwoThree=123;",
15971                    Alignment));
15972 
15973   EXPECT_EQ("float const a = 5;\n"
15974             "\n"
15975             "// line comment\n"
15976             "int         oneTwoThree = 123;",
15977             format("float const a = 5;\n"
15978                    "\n"
15979                    "// line comment\n"
15980                    "int oneTwoThree=123;",
15981                    Alignment));
15982 }
15983 
15984 TEST_F(FormatTest, AlignConsecutiveBitFieldsAcrossEmptyLinesAndComments) {
15985   FormatStyle Alignment = getLLVMStyle();
15986   Alignment.AlignConsecutiveBitFields =
15987       FormatStyle::ACS_AcrossEmptyLinesAndComments;
15988 
15989   Alignment.MaxEmptyLinesToKeep = 10;
15990   /* Test alignment across empty lines */
15991   EXPECT_EQ("int a            : 5;\n"
15992             "\n"
15993             "int longbitfield : 6;",
15994             format("int a : 5;\n"
15995                    "\n"
15996                    "int longbitfield : 6;",
15997                    Alignment));
15998   EXPECT_EQ("int a            : 5;\n"
15999             "int one          : 1;\n"
16000             "\n"
16001             "int longbitfield : 6;",
16002             format("int a : 5;\n"
16003                    "int one : 1;\n"
16004                    "\n"
16005                    "int longbitfield : 6;",
16006                    Alignment));
16007 
16008   /* Test across comments */
16009   EXPECT_EQ("int a            : 5;\n"
16010             "/* block comment */\n"
16011             "int longbitfield : 6;",
16012             format("int a : 5;\n"
16013                    "/* block comment */\n"
16014                    "int longbitfield : 6;",
16015                    Alignment));
16016   EXPECT_EQ("int a            : 5;\n"
16017             "int one          : 1;\n"
16018             "// line comment\n"
16019             "int longbitfield : 6;",
16020             format("int a : 5;\n"
16021                    "int one : 1;\n"
16022                    "// line comment\n"
16023                    "int longbitfield : 6;",
16024                    Alignment));
16025 
16026   /* Test across comments and newlines */
16027   EXPECT_EQ("int a            : 5;\n"
16028             "/* block comment */\n"
16029             "\n"
16030             "int longbitfield : 6;",
16031             format("int a : 5;\n"
16032                    "/* block comment */\n"
16033                    "\n"
16034                    "int longbitfield : 6;",
16035                    Alignment));
16036   EXPECT_EQ("int a            : 5;\n"
16037             "int one          : 1;\n"
16038             "\n"
16039             "// line comment\n"
16040             "\n"
16041             "int longbitfield : 6;",
16042             format("int a : 5;\n"
16043                    "int one : 1;\n"
16044                    "\n"
16045                    "// line comment \n"
16046                    "\n"
16047                    "int longbitfield : 6;",
16048                    Alignment));
16049 }
16050 
16051 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossComments) {
16052   FormatStyle Alignment = getLLVMStyle();
16053   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
16054   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_AcrossComments;
16055 
16056   Alignment.MaxEmptyLinesToKeep = 10;
16057   /* Test alignment across empty lines */
16058   EXPECT_EQ("int a = 5;\n"
16059             "\n"
16060             "int oneTwoThree = 123;",
16061             format("int a       = 5;\n"
16062                    "\n"
16063                    "int oneTwoThree= 123;",
16064                    Alignment));
16065   EXPECT_EQ("int a   = 5;\n"
16066             "int one = 1;\n"
16067             "\n"
16068             "int oneTwoThree = 123;",
16069             format("int a = 5;\n"
16070                    "int one = 1;\n"
16071                    "\n"
16072                    "int oneTwoThree = 123;",
16073                    Alignment));
16074 
16075   /* Test across comments */
16076   EXPECT_EQ("int a           = 5;\n"
16077             "/* block comment */\n"
16078             "int oneTwoThree = 123;",
16079             format("int a = 5;\n"
16080                    "/* block comment */\n"
16081                    "int oneTwoThree=123;",
16082                    Alignment));
16083 
16084   EXPECT_EQ("int a           = 5;\n"
16085             "// line comment\n"
16086             "int oneTwoThree = 123;",
16087             format("int a = 5;\n"
16088                    "// line comment\n"
16089                    "int oneTwoThree=123;",
16090                    Alignment));
16091 
16092   EXPECT_EQ("int a           = 5;\n"
16093             "/*\n"
16094             " * multi-line block comment\n"
16095             " */\n"
16096             "int oneTwoThree = 123;",
16097             format("int a = 5;\n"
16098                    "/*\n"
16099                    " * multi-line block comment\n"
16100                    " */\n"
16101                    "int oneTwoThree=123;",
16102                    Alignment));
16103 
16104   EXPECT_EQ("int a           = 5;\n"
16105             "//\n"
16106             "// multi-line line comment\n"
16107             "//\n"
16108             "int oneTwoThree = 123;",
16109             format("int a = 5;\n"
16110                    "//\n"
16111                    "// multi-line line comment\n"
16112                    "//\n"
16113                    "int oneTwoThree=123;",
16114                    Alignment));
16115 
16116   /* Test across comments and newlines */
16117   EXPECT_EQ("int a = 5;\n"
16118             "\n"
16119             "/* block comment */\n"
16120             "int oneTwoThree = 123;",
16121             format("int a = 5;\n"
16122                    "\n"
16123                    "/* block comment */\n"
16124                    "int oneTwoThree=123;",
16125                    Alignment));
16126 
16127   EXPECT_EQ("int a = 5;\n"
16128             "\n"
16129             "// line comment\n"
16130             "int oneTwoThree = 123;",
16131             format("int a = 5;\n"
16132                    "\n"
16133                    "// line comment\n"
16134                    "int oneTwoThree=123;",
16135                    Alignment));
16136 }
16137 
16138 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLinesAndComments) {
16139   FormatStyle Alignment = getLLVMStyle();
16140   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
16141   Alignment.AlignConsecutiveAssignments =
16142       FormatStyle::ACS_AcrossEmptyLinesAndComments;
16143   verifyFormat("int a           = 5;\n"
16144                "int oneTwoThree = 123;",
16145                Alignment);
16146   verifyFormat("int a           = method();\n"
16147                "int oneTwoThree = 133;",
16148                Alignment);
16149   verifyFormat("a &= 5;\n"
16150                "bcd *= 5;\n"
16151                "ghtyf += 5;\n"
16152                "dvfvdb -= 5;\n"
16153                "a /= 5;\n"
16154                "vdsvsv %= 5;\n"
16155                "sfdbddfbdfbb ^= 5;\n"
16156                "dvsdsv |= 5;\n"
16157                "int dsvvdvsdvvv = 123;",
16158                Alignment);
16159   verifyFormat("int i = 1, j = 10;\n"
16160                "something = 2000;",
16161                Alignment);
16162   verifyFormat("something = 2000;\n"
16163                "int i = 1, j = 10;\n",
16164                Alignment);
16165   verifyFormat("something = 2000;\n"
16166                "another   = 911;\n"
16167                "int i = 1, j = 10;\n"
16168                "oneMore = 1;\n"
16169                "i       = 2;",
16170                Alignment);
16171   verifyFormat("int a   = 5;\n"
16172                "int one = 1;\n"
16173                "method();\n"
16174                "int oneTwoThree = 123;\n"
16175                "int oneTwo      = 12;",
16176                Alignment);
16177   verifyFormat("int oneTwoThree = 123;\n"
16178                "int oneTwo      = 12;\n"
16179                "method();\n",
16180                Alignment);
16181   verifyFormat("int oneTwoThree = 123; // comment\n"
16182                "int oneTwo      = 12;  // comment",
16183                Alignment);
16184 
16185   // Bug 25167
16186   /* Uncomment when fixed
16187     verifyFormat("#if A\n"
16188                  "#else\n"
16189                  "int aaaaaaaa = 12;\n"
16190                  "#endif\n"
16191                  "#if B\n"
16192                  "#else\n"
16193                  "int a = 12;\n"
16194                  "#endif\n",
16195                  Alignment);
16196     verifyFormat("enum foo {\n"
16197                  "#if A\n"
16198                  "#else\n"
16199                  "  aaaaaaaa = 12;\n"
16200                  "#endif\n"
16201                  "#if B\n"
16202                  "#else\n"
16203                  "  a = 12;\n"
16204                  "#endif\n"
16205                  "};\n",
16206                  Alignment);
16207   */
16208 
16209   Alignment.MaxEmptyLinesToKeep = 10;
16210   /* Test alignment across empty lines */
16211   EXPECT_EQ("int a           = 5;\n"
16212             "\n"
16213             "int oneTwoThree = 123;",
16214             format("int a       = 5;\n"
16215                    "\n"
16216                    "int oneTwoThree= 123;",
16217                    Alignment));
16218   EXPECT_EQ("int a           = 5;\n"
16219             "int one         = 1;\n"
16220             "\n"
16221             "int oneTwoThree = 123;",
16222             format("int a = 5;\n"
16223                    "int one = 1;\n"
16224                    "\n"
16225                    "int oneTwoThree = 123;",
16226                    Alignment));
16227   EXPECT_EQ("int a           = 5;\n"
16228             "int one         = 1;\n"
16229             "\n"
16230             "int oneTwoThree = 123;\n"
16231             "int oneTwo      = 12;",
16232             format("int a = 5;\n"
16233                    "int one = 1;\n"
16234                    "\n"
16235                    "int oneTwoThree = 123;\n"
16236                    "int oneTwo = 12;",
16237                    Alignment));
16238 
16239   /* Test across comments */
16240   EXPECT_EQ("int a           = 5;\n"
16241             "/* block comment */\n"
16242             "int oneTwoThree = 123;",
16243             format("int a = 5;\n"
16244                    "/* block comment */\n"
16245                    "int oneTwoThree=123;",
16246                    Alignment));
16247 
16248   EXPECT_EQ("int a           = 5;\n"
16249             "// line comment\n"
16250             "int oneTwoThree = 123;",
16251             format("int a = 5;\n"
16252                    "// line comment\n"
16253                    "int oneTwoThree=123;",
16254                    Alignment));
16255 
16256   /* Test across comments and newlines */
16257   EXPECT_EQ("int a           = 5;\n"
16258             "\n"
16259             "/* block comment */\n"
16260             "int oneTwoThree = 123;",
16261             format("int a = 5;\n"
16262                    "\n"
16263                    "/* block comment */\n"
16264                    "int oneTwoThree=123;",
16265                    Alignment));
16266 
16267   EXPECT_EQ("int a           = 5;\n"
16268             "\n"
16269             "// line comment\n"
16270             "int oneTwoThree = 123;",
16271             format("int a = 5;\n"
16272                    "\n"
16273                    "// line comment\n"
16274                    "int oneTwoThree=123;",
16275                    Alignment));
16276 
16277   EXPECT_EQ("int a           = 5;\n"
16278             "//\n"
16279             "// multi-line line comment\n"
16280             "//\n"
16281             "int oneTwoThree = 123;",
16282             format("int a = 5;\n"
16283                    "//\n"
16284                    "// multi-line line comment\n"
16285                    "//\n"
16286                    "int oneTwoThree=123;",
16287                    Alignment));
16288 
16289   EXPECT_EQ("int a           = 5;\n"
16290             "/*\n"
16291             " *  multi-line block comment\n"
16292             " */\n"
16293             "int oneTwoThree = 123;",
16294             format("int a = 5;\n"
16295                    "/*\n"
16296                    " *  multi-line block comment\n"
16297                    " */\n"
16298                    "int oneTwoThree=123;",
16299                    Alignment));
16300 
16301   EXPECT_EQ("int a           = 5;\n"
16302             "\n"
16303             "/* block comment */\n"
16304             "\n"
16305             "\n"
16306             "\n"
16307             "int oneTwoThree = 123;",
16308             format("int a = 5;\n"
16309                    "\n"
16310                    "/* block comment */\n"
16311                    "\n"
16312                    "\n"
16313                    "\n"
16314                    "int oneTwoThree=123;",
16315                    Alignment));
16316 
16317   EXPECT_EQ("int a           = 5;\n"
16318             "\n"
16319             "// line comment\n"
16320             "\n"
16321             "\n"
16322             "\n"
16323             "int oneTwoThree = 123;",
16324             format("int a = 5;\n"
16325                    "\n"
16326                    "// line comment\n"
16327                    "\n"
16328                    "\n"
16329                    "\n"
16330                    "int oneTwoThree=123;",
16331                    Alignment));
16332 
16333   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
16334   verifyFormat("#define A \\\n"
16335                "  int aaaa       = 12; \\\n"
16336                "  int b          = 23; \\\n"
16337                "  int ccc        = 234; \\\n"
16338                "  int dddddddddd = 2345;",
16339                Alignment);
16340   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
16341   verifyFormat("#define A               \\\n"
16342                "  int aaaa       = 12;  \\\n"
16343                "  int b          = 23;  \\\n"
16344                "  int ccc        = 234; \\\n"
16345                "  int dddddddddd = 2345;",
16346                Alignment);
16347   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
16348   verifyFormat("#define A                                                      "
16349                "                \\\n"
16350                "  int aaaa       = 12;                                         "
16351                "                \\\n"
16352                "  int b          = 23;                                         "
16353                "                \\\n"
16354                "  int ccc        = 234;                                        "
16355                "                \\\n"
16356                "  int dddddddddd = 2345;",
16357                Alignment);
16358   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
16359                "k = 4, int l = 5,\n"
16360                "                  int m = 6) {\n"
16361                "  int j      = 10;\n"
16362                "  otherThing = 1;\n"
16363                "}",
16364                Alignment);
16365   verifyFormat("void SomeFunction(int parameter = 0) {\n"
16366                "  int i   = 1;\n"
16367                "  int j   = 2;\n"
16368                "  int big = 10000;\n"
16369                "}",
16370                Alignment);
16371   verifyFormat("class C {\n"
16372                "public:\n"
16373                "  int i            = 1;\n"
16374                "  virtual void f() = 0;\n"
16375                "};",
16376                Alignment);
16377   verifyFormat("int i = 1;\n"
16378                "if (SomeType t = getSomething()) {\n"
16379                "}\n"
16380                "int j   = 2;\n"
16381                "int big = 10000;",
16382                Alignment);
16383   verifyFormat("int j = 7;\n"
16384                "for (int k = 0; k < N; ++k) {\n"
16385                "}\n"
16386                "int j   = 2;\n"
16387                "int big = 10000;\n"
16388                "}",
16389                Alignment);
16390   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
16391   verifyFormat("int i = 1;\n"
16392                "LooooooooooongType loooooooooooooooooooooongVariable\n"
16393                "    = someLooooooooooooooooongFunction();\n"
16394                "int j = 2;",
16395                Alignment);
16396   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
16397   verifyFormat("int i = 1;\n"
16398                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
16399                "    someLooooooooooooooooongFunction();\n"
16400                "int j = 2;",
16401                Alignment);
16402 
16403   verifyFormat("auto lambda = []() {\n"
16404                "  auto i = 0;\n"
16405                "  return 0;\n"
16406                "};\n"
16407                "int i  = 0;\n"
16408                "auto v = type{\n"
16409                "    i = 1,   //\n"
16410                "    (i = 2), //\n"
16411                "    i = 3    //\n"
16412                "};",
16413                Alignment);
16414 
16415   verifyFormat(
16416       "int i      = 1;\n"
16417       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
16418       "                          loooooooooooooooooooooongParameterB);\n"
16419       "int j      = 2;",
16420       Alignment);
16421 
16422   verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
16423                "          typename B   = very_long_type_name_1,\n"
16424                "          typename T_2 = very_long_type_name_2>\n"
16425                "auto foo() {}\n",
16426                Alignment);
16427   verifyFormat("int a, b = 1;\n"
16428                "int c  = 2;\n"
16429                "int dd = 3;\n",
16430                Alignment);
16431   verifyFormat("int aa       = ((1 > 2) ? 3 : 4);\n"
16432                "float b[1][] = {{3.f}};\n",
16433                Alignment);
16434   verifyFormat("for (int i = 0; i < 1; i++)\n"
16435                "  int x = 1;\n",
16436                Alignment);
16437   verifyFormat("for (i = 0; i < 1; i++)\n"
16438                "  x = 1;\n"
16439                "y = 1;\n",
16440                Alignment);
16441 
16442   Alignment.ReflowComments = true;
16443   Alignment.ColumnLimit = 50;
16444   EXPECT_EQ("int x   = 0;\n"
16445             "int yy  = 1; /// specificlennospace\n"
16446             "int zzz = 2;\n",
16447             format("int x   = 0;\n"
16448                    "int yy  = 1; ///specificlennospace\n"
16449                    "int zzz = 2;\n",
16450                    Alignment));
16451 }
16452 
16453 TEST_F(FormatTest, AlignConsecutiveAssignments) {
16454   FormatStyle Alignment = getLLVMStyle();
16455   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
16456   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
16457   verifyFormat("int a = 5;\n"
16458                "int oneTwoThree = 123;",
16459                Alignment);
16460   verifyFormat("int a = 5;\n"
16461                "int oneTwoThree = 123;",
16462                Alignment);
16463 
16464   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16465   verifyFormat("int a           = 5;\n"
16466                "int oneTwoThree = 123;",
16467                Alignment);
16468   verifyFormat("int a           = method();\n"
16469                "int oneTwoThree = 133;",
16470                Alignment);
16471   verifyFormat("a &= 5;\n"
16472                "bcd *= 5;\n"
16473                "ghtyf += 5;\n"
16474                "dvfvdb -= 5;\n"
16475                "a /= 5;\n"
16476                "vdsvsv %= 5;\n"
16477                "sfdbddfbdfbb ^= 5;\n"
16478                "dvsdsv |= 5;\n"
16479                "int dsvvdvsdvvv = 123;",
16480                Alignment);
16481   verifyFormat("int i = 1, j = 10;\n"
16482                "something = 2000;",
16483                Alignment);
16484   verifyFormat("something = 2000;\n"
16485                "int i = 1, j = 10;\n",
16486                Alignment);
16487   verifyFormat("something = 2000;\n"
16488                "another   = 911;\n"
16489                "int i = 1, j = 10;\n"
16490                "oneMore = 1;\n"
16491                "i       = 2;",
16492                Alignment);
16493   verifyFormat("int a   = 5;\n"
16494                "int one = 1;\n"
16495                "method();\n"
16496                "int oneTwoThree = 123;\n"
16497                "int oneTwo      = 12;",
16498                Alignment);
16499   verifyFormat("int oneTwoThree = 123;\n"
16500                "int oneTwo      = 12;\n"
16501                "method();\n",
16502                Alignment);
16503   verifyFormat("int oneTwoThree = 123; // comment\n"
16504                "int oneTwo      = 12;  // comment",
16505                Alignment);
16506   verifyFormat("int f()         = default;\n"
16507                "int &operator() = default;\n"
16508                "int &operator=() {",
16509                Alignment);
16510   verifyFormat("int f()         = delete;\n"
16511                "int &operator() = delete;\n"
16512                "int &operator=() {",
16513                Alignment);
16514   verifyFormat("int f()         = default; // comment\n"
16515                "int &operator() = default; // comment\n"
16516                "int &operator=() {",
16517                Alignment);
16518   verifyFormat("int f()         = default;\n"
16519                "int &operator() = default;\n"
16520                "int &operator==() {",
16521                Alignment);
16522   verifyFormat("int f()         = default;\n"
16523                "int &operator() = default;\n"
16524                "int &operator<=() {",
16525                Alignment);
16526   verifyFormat("int f()         = default;\n"
16527                "int &operator() = default;\n"
16528                "int &operator!=() {",
16529                Alignment);
16530   verifyFormat("int f()         = default;\n"
16531                "int &operator() = default;\n"
16532                "int &operator=();",
16533                Alignment);
16534   verifyFormat("int f()         = delete;\n"
16535                "int &operator() = delete;\n"
16536                "int &operator=();",
16537                Alignment);
16538   verifyFormat("/* long long padding */ int f() = default;\n"
16539                "int &operator()                 = default;\n"
16540                "int &operator/**/ =();",
16541                Alignment);
16542   // https://llvm.org/PR33697
16543   FormatStyle AlignmentWithPenalty = getLLVMStyle();
16544   AlignmentWithPenalty.AlignConsecutiveAssignments =
16545       FormatStyle::ACS_Consecutive;
16546   AlignmentWithPenalty.PenaltyReturnTypeOnItsOwnLine = 5000;
16547   verifyFormat("class SSSSSSSSSSSSSSSSSSSSSSSSSSSS {\n"
16548                "  void f() = delete;\n"
16549                "  SSSSSSSSSSSSSSSSSSSSSSSSSSSS &operator=(\n"
16550                "      const SSSSSSSSSSSSSSSSSSSSSSSSSSSS &other) = delete;\n"
16551                "};\n",
16552                AlignmentWithPenalty);
16553 
16554   // Bug 25167
16555   /* Uncomment when fixed
16556     verifyFormat("#if A\n"
16557                  "#else\n"
16558                  "int aaaaaaaa = 12;\n"
16559                  "#endif\n"
16560                  "#if B\n"
16561                  "#else\n"
16562                  "int a = 12;\n"
16563                  "#endif\n",
16564                  Alignment);
16565     verifyFormat("enum foo {\n"
16566                  "#if A\n"
16567                  "#else\n"
16568                  "  aaaaaaaa = 12;\n"
16569                  "#endif\n"
16570                  "#if B\n"
16571                  "#else\n"
16572                  "  a = 12;\n"
16573                  "#endif\n"
16574                  "};\n",
16575                  Alignment);
16576   */
16577 
16578   EXPECT_EQ("int a = 5;\n"
16579             "\n"
16580             "int oneTwoThree = 123;",
16581             format("int a       = 5;\n"
16582                    "\n"
16583                    "int oneTwoThree= 123;",
16584                    Alignment));
16585   EXPECT_EQ("int a   = 5;\n"
16586             "int one = 1;\n"
16587             "\n"
16588             "int oneTwoThree = 123;",
16589             format("int a = 5;\n"
16590                    "int one = 1;\n"
16591                    "\n"
16592                    "int oneTwoThree = 123;",
16593                    Alignment));
16594   EXPECT_EQ("int a   = 5;\n"
16595             "int one = 1;\n"
16596             "\n"
16597             "int oneTwoThree = 123;\n"
16598             "int oneTwo      = 12;",
16599             format("int a = 5;\n"
16600                    "int one = 1;\n"
16601                    "\n"
16602                    "int oneTwoThree = 123;\n"
16603                    "int oneTwo = 12;",
16604                    Alignment));
16605   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
16606   verifyFormat("#define A \\\n"
16607                "  int aaaa       = 12; \\\n"
16608                "  int b          = 23; \\\n"
16609                "  int ccc        = 234; \\\n"
16610                "  int dddddddddd = 2345;",
16611                Alignment);
16612   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
16613   verifyFormat("#define A               \\\n"
16614                "  int aaaa       = 12;  \\\n"
16615                "  int b          = 23;  \\\n"
16616                "  int ccc        = 234; \\\n"
16617                "  int dddddddddd = 2345;",
16618                Alignment);
16619   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
16620   verifyFormat("#define A                                                      "
16621                "                \\\n"
16622                "  int aaaa       = 12;                                         "
16623                "                \\\n"
16624                "  int b          = 23;                                         "
16625                "                \\\n"
16626                "  int ccc        = 234;                                        "
16627                "                \\\n"
16628                "  int dddddddddd = 2345;",
16629                Alignment);
16630   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
16631                "k = 4, int l = 5,\n"
16632                "                  int m = 6) {\n"
16633                "  int j      = 10;\n"
16634                "  otherThing = 1;\n"
16635                "}",
16636                Alignment);
16637   verifyFormat("void SomeFunction(int parameter = 0) {\n"
16638                "  int i   = 1;\n"
16639                "  int j   = 2;\n"
16640                "  int big = 10000;\n"
16641                "}",
16642                Alignment);
16643   verifyFormat("class C {\n"
16644                "public:\n"
16645                "  int i            = 1;\n"
16646                "  virtual void f() = 0;\n"
16647                "};",
16648                Alignment);
16649   verifyFormat("int i = 1;\n"
16650                "if (SomeType t = getSomething()) {\n"
16651                "}\n"
16652                "int j   = 2;\n"
16653                "int big = 10000;",
16654                Alignment);
16655   verifyFormat("int j = 7;\n"
16656                "for (int k = 0; k < N; ++k) {\n"
16657                "}\n"
16658                "int j   = 2;\n"
16659                "int big = 10000;\n"
16660                "}",
16661                Alignment);
16662   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
16663   verifyFormat("int i = 1;\n"
16664                "LooooooooooongType loooooooooooooooooooooongVariable\n"
16665                "    = someLooooooooooooooooongFunction();\n"
16666                "int j = 2;",
16667                Alignment);
16668   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
16669   verifyFormat("int i = 1;\n"
16670                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
16671                "    someLooooooooooooooooongFunction();\n"
16672                "int j = 2;",
16673                Alignment);
16674 
16675   verifyFormat("auto lambda = []() {\n"
16676                "  auto i = 0;\n"
16677                "  return 0;\n"
16678                "};\n"
16679                "int i  = 0;\n"
16680                "auto v = type{\n"
16681                "    i = 1,   //\n"
16682                "    (i = 2), //\n"
16683                "    i = 3    //\n"
16684                "};",
16685                Alignment);
16686 
16687   verifyFormat(
16688       "int i      = 1;\n"
16689       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
16690       "                          loooooooooooooooooooooongParameterB);\n"
16691       "int j      = 2;",
16692       Alignment);
16693 
16694   verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
16695                "          typename B   = very_long_type_name_1,\n"
16696                "          typename T_2 = very_long_type_name_2>\n"
16697                "auto foo() {}\n",
16698                Alignment);
16699   verifyFormat("int a, b = 1;\n"
16700                "int c  = 2;\n"
16701                "int dd = 3;\n",
16702                Alignment);
16703   verifyFormat("int aa       = ((1 > 2) ? 3 : 4);\n"
16704                "float b[1][] = {{3.f}};\n",
16705                Alignment);
16706   verifyFormat("for (int i = 0; i < 1; i++)\n"
16707                "  int x = 1;\n",
16708                Alignment);
16709   verifyFormat("for (i = 0; i < 1; i++)\n"
16710                "  x = 1;\n"
16711                "y = 1;\n",
16712                Alignment);
16713 
16714   EXPECT_EQ(Alignment.ReflowComments, true);
16715   Alignment.ColumnLimit = 50;
16716   EXPECT_EQ("int x   = 0;\n"
16717             "int yy  = 1; /// specificlennospace\n"
16718             "int zzz = 2;\n",
16719             format("int x   = 0;\n"
16720                    "int yy  = 1; ///specificlennospace\n"
16721                    "int zzz = 2;\n",
16722                    Alignment));
16723 
16724   verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n"
16725                "auto b                     = [] {\n"
16726                "  f();\n"
16727                "  return;\n"
16728                "};",
16729                Alignment);
16730   verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n"
16731                "auto b                     = g([] {\n"
16732                "  f();\n"
16733                "  return;\n"
16734                "});",
16735                Alignment);
16736   verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n"
16737                "auto b                     = g(param, [] {\n"
16738                "  f();\n"
16739                "  return;\n"
16740                "});",
16741                Alignment);
16742   verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n"
16743                "auto b                     = [] {\n"
16744                "  if (condition) {\n"
16745                "    return;\n"
16746                "  }\n"
16747                "};",
16748                Alignment);
16749 
16750   verifyFormat("auto b = f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
16751                "           ccc ? aaaaa : bbbbb,\n"
16752                "           dddddddddddddddddddddddddd);",
16753                Alignment);
16754   // FIXME: https://llvm.org/PR53497
16755   // verifyFormat("auto aaaaaaaaaaaa = f();\n"
16756   //              "auto b            = f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
16757   //              "    ccc ? aaaaa : bbbbb,\n"
16758   //              "    dddddddddddddddddddddddddd);",
16759   //              Alignment);
16760 }
16761 
16762 TEST_F(FormatTest, AlignConsecutiveBitFields) {
16763   FormatStyle Alignment = getLLVMStyle();
16764   Alignment.AlignConsecutiveBitFields = FormatStyle::ACS_Consecutive;
16765   verifyFormat("int const a     : 5;\n"
16766                "int oneTwoThree : 23;",
16767                Alignment);
16768 
16769   // Initializers are allowed starting with c++2a
16770   verifyFormat("int const a     : 5 = 1;\n"
16771                "int oneTwoThree : 23 = 0;",
16772                Alignment);
16773 
16774   Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
16775   verifyFormat("int const a           : 5;\n"
16776                "int       oneTwoThree : 23;",
16777                Alignment);
16778 
16779   verifyFormat("int const a           : 5;  // comment\n"
16780                "int       oneTwoThree : 23; // comment",
16781                Alignment);
16782 
16783   verifyFormat("int const a           : 5 = 1;\n"
16784                "int       oneTwoThree : 23 = 0;",
16785                Alignment);
16786 
16787   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16788   verifyFormat("int const a           : 5  = 1;\n"
16789                "int       oneTwoThree : 23 = 0;",
16790                Alignment);
16791   verifyFormat("int const a           : 5  = {1};\n"
16792                "int       oneTwoThree : 23 = 0;",
16793                Alignment);
16794 
16795   Alignment.BitFieldColonSpacing = FormatStyle::BFCS_None;
16796   verifyFormat("int const a          :5;\n"
16797                "int       oneTwoThree:23;",
16798                Alignment);
16799 
16800   Alignment.BitFieldColonSpacing = FormatStyle::BFCS_Before;
16801   verifyFormat("int const a           :5;\n"
16802                "int       oneTwoThree :23;",
16803                Alignment);
16804 
16805   Alignment.BitFieldColonSpacing = FormatStyle::BFCS_After;
16806   verifyFormat("int const a          : 5;\n"
16807                "int       oneTwoThree: 23;",
16808                Alignment);
16809 
16810   // Known limitations: ':' is only recognized as a bitfield colon when
16811   // followed by a number.
16812   /*
16813   verifyFormat("int oneTwoThree : SOME_CONSTANT;\n"
16814                "int a           : 5;",
16815                Alignment);
16816   */
16817 }
16818 
16819 TEST_F(FormatTest, AlignConsecutiveDeclarations) {
16820   FormatStyle Alignment = getLLVMStyle();
16821   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
16822   Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_None;
16823   Alignment.PointerAlignment = FormatStyle::PAS_Right;
16824   verifyFormat("float const a = 5;\n"
16825                "int oneTwoThree = 123;",
16826                Alignment);
16827   verifyFormat("int a = 5;\n"
16828                "float const oneTwoThree = 123;",
16829                Alignment);
16830 
16831   Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
16832   verifyFormat("float const a = 5;\n"
16833                "int         oneTwoThree = 123;",
16834                Alignment);
16835   verifyFormat("int         a = method();\n"
16836                "float const oneTwoThree = 133;",
16837                Alignment);
16838   verifyFormat("int i = 1, j = 10;\n"
16839                "something = 2000;",
16840                Alignment);
16841   verifyFormat("something = 2000;\n"
16842                "int i = 1, j = 10;\n",
16843                Alignment);
16844   verifyFormat("float      something = 2000;\n"
16845                "double     another = 911;\n"
16846                "int        i = 1, j = 10;\n"
16847                "const int *oneMore = 1;\n"
16848                "unsigned   i = 2;",
16849                Alignment);
16850   verifyFormat("float a = 5;\n"
16851                "int   one = 1;\n"
16852                "method();\n"
16853                "const double       oneTwoThree = 123;\n"
16854                "const unsigned int oneTwo = 12;",
16855                Alignment);
16856   verifyFormat("int      oneTwoThree{0}; // comment\n"
16857                "unsigned oneTwo;         // comment",
16858                Alignment);
16859   verifyFormat("unsigned int       *a;\n"
16860                "int                *b;\n"
16861                "unsigned int Const *c;\n"
16862                "unsigned int const *d;\n"
16863                "unsigned int Const &e;\n"
16864                "unsigned int const &f;",
16865                Alignment);
16866   verifyFormat("Const unsigned int *c;\n"
16867                "const unsigned int *d;\n"
16868                "Const unsigned int &e;\n"
16869                "const unsigned int &f;\n"
16870                "const unsigned      g;\n"
16871                "Const unsigned      h;",
16872                Alignment);
16873   EXPECT_EQ("float const a = 5;\n"
16874             "\n"
16875             "int oneTwoThree = 123;",
16876             format("float const   a = 5;\n"
16877                    "\n"
16878                    "int           oneTwoThree= 123;",
16879                    Alignment));
16880   EXPECT_EQ("float a = 5;\n"
16881             "int   one = 1;\n"
16882             "\n"
16883             "unsigned oneTwoThree = 123;",
16884             format("float    a = 5;\n"
16885                    "int      one = 1;\n"
16886                    "\n"
16887                    "unsigned oneTwoThree = 123;",
16888                    Alignment));
16889   EXPECT_EQ("float a = 5;\n"
16890             "int   one = 1;\n"
16891             "\n"
16892             "unsigned oneTwoThree = 123;\n"
16893             "int      oneTwo = 12;",
16894             format("float    a = 5;\n"
16895                    "int one = 1;\n"
16896                    "\n"
16897                    "unsigned oneTwoThree = 123;\n"
16898                    "int oneTwo = 12;",
16899                    Alignment));
16900   // Function prototype alignment
16901   verifyFormat("int    a();\n"
16902                "double b();",
16903                Alignment);
16904   verifyFormat("int    a(int x);\n"
16905                "double b();",
16906                Alignment);
16907   unsigned OldColumnLimit = Alignment.ColumnLimit;
16908   // We need to set ColumnLimit to zero, in order to stress nested alignments,
16909   // otherwise the function parameters will be re-flowed onto a single line.
16910   Alignment.ColumnLimit = 0;
16911   EXPECT_EQ("int    a(int   x,\n"
16912             "         float y);\n"
16913             "double b(int    x,\n"
16914             "         double y);",
16915             format("int a(int x,\n"
16916                    " float y);\n"
16917                    "double b(int x,\n"
16918                    " double y);",
16919                    Alignment));
16920   // This ensures that function parameters of function declarations are
16921   // correctly indented when their owning functions are indented.
16922   // The failure case here is for 'double y' to not be indented enough.
16923   EXPECT_EQ("double a(int x);\n"
16924             "int    b(int    y,\n"
16925             "         double z);",
16926             format("double a(int x);\n"
16927                    "int b(int y,\n"
16928                    " double z);",
16929                    Alignment));
16930   // Set ColumnLimit low so that we induce wrapping immediately after
16931   // the function name and opening paren.
16932   Alignment.ColumnLimit = 13;
16933   verifyFormat("int function(\n"
16934                "    int  x,\n"
16935                "    bool y);",
16936                Alignment);
16937   Alignment.ColumnLimit = OldColumnLimit;
16938   // Ensure function pointers don't screw up recursive alignment
16939   verifyFormat("int    a(int x, void (*fp)(int y));\n"
16940                "double b();",
16941                Alignment);
16942   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16943   // Ensure recursive alignment is broken by function braces, so that the
16944   // "a = 1" does not align with subsequent assignments inside the function
16945   // body.
16946   verifyFormat("int func(int a = 1) {\n"
16947                "  int b  = 2;\n"
16948                "  int cc = 3;\n"
16949                "}",
16950                Alignment);
16951   verifyFormat("float      something = 2000;\n"
16952                "double     another   = 911;\n"
16953                "int        i = 1, j = 10;\n"
16954                "const int *oneMore = 1;\n"
16955                "unsigned   i       = 2;",
16956                Alignment);
16957   verifyFormat("int      oneTwoThree = {0}; // comment\n"
16958                "unsigned oneTwo      = 0;   // comment",
16959                Alignment);
16960   // Make sure that scope is correctly tracked, in the absence of braces
16961   verifyFormat("for (int i = 0; i < n; i++)\n"
16962                "  j = i;\n"
16963                "double x = 1;\n",
16964                Alignment);
16965   verifyFormat("if (int i = 0)\n"
16966                "  j = i;\n"
16967                "double x = 1;\n",
16968                Alignment);
16969   // Ensure operator[] and operator() are comprehended
16970   verifyFormat("struct test {\n"
16971                "  long long int foo();\n"
16972                "  int           operator[](int a);\n"
16973                "  double        bar();\n"
16974                "};\n",
16975                Alignment);
16976   verifyFormat("struct test {\n"
16977                "  long long int foo();\n"
16978                "  int           operator()(int a);\n"
16979                "  double        bar();\n"
16980                "};\n",
16981                Alignment);
16982   // http://llvm.org/PR52914
16983   verifyFormat("char *a[]     = {\"a\", // comment\n"
16984                "                 \"bb\"};\n"
16985                "int   bbbbbbb = 0;",
16986                Alignment);
16987 
16988   // PAS_Right
16989   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
16990             "  int const i   = 1;\n"
16991             "  int      *j   = 2;\n"
16992             "  int       big = 10000;\n"
16993             "\n"
16994             "  unsigned oneTwoThree = 123;\n"
16995             "  int      oneTwo      = 12;\n"
16996             "  method();\n"
16997             "  float k  = 2;\n"
16998             "  int   ll = 10000;\n"
16999             "}",
17000             format("void SomeFunction(int parameter= 0) {\n"
17001                    " int const  i= 1;\n"
17002                    "  int *j=2;\n"
17003                    " int big  =  10000;\n"
17004                    "\n"
17005                    "unsigned oneTwoThree  =123;\n"
17006                    "int oneTwo = 12;\n"
17007                    "  method();\n"
17008                    "float k= 2;\n"
17009                    "int ll=10000;\n"
17010                    "}",
17011                    Alignment));
17012   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
17013             "  int const i   = 1;\n"
17014             "  int     **j   = 2, ***k;\n"
17015             "  int      &k   = i;\n"
17016             "  int     &&l   = i + j;\n"
17017             "  int       big = 10000;\n"
17018             "\n"
17019             "  unsigned oneTwoThree = 123;\n"
17020             "  int      oneTwo      = 12;\n"
17021             "  method();\n"
17022             "  float k  = 2;\n"
17023             "  int   ll = 10000;\n"
17024             "}",
17025             format("void SomeFunction(int parameter= 0) {\n"
17026                    " int const  i= 1;\n"
17027                    "  int **j=2,***k;\n"
17028                    "int &k=i;\n"
17029                    "int &&l=i+j;\n"
17030                    " int big  =  10000;\n"
17031                    "\n"
17032                    "unsigned oneTwoThree  =123;\n"
17033                    "int oneTwo = 12;\n"
17034                    "  method();\n"
17035                    "float k= 2;\n"
17036                    "int ll=10000;\n"
17037                    "}",
17038                    Alignment));
17039   // variables are aligned at their name, pointers are at the right most
17040   // position
17041   verifyFormat("int   *a;\n"
17042                "int  **b;\n"
17043                "int ***c;\n"
17044                "int    foobar;\n",
17045                Alignment);
17046 
17047   // PAS_Left
17048   FormatStyle AlignmentLeft = Alignment;
17049   AlignmentLeft.PointerAlignment = FormatStyle::PAS_Left;
17050   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
17051             "  int const i   = 1;\n"
17052             "  int*      j   = 2;\n"
17053             "  int       big = 10000;\n"
17054             "\n"
17055             "  unsigned oneTwoThree = 123;\n"
17056             "  int      oneTwo      = 12;\n"
17057             "  method();\n"
17058             "  float k  = 2;\n"
17059             "  int   ll = 10000;\n"
17060             "}",
17061             format("void SomeFunction(int parameter= 0) {\n"
17062                    " int const  i= 1;\n"
17063                    "  int *j=2;\n"
17064                    " int big  =  10000;\n"
17065                    "\n"
17066                    "unsigned oneTwoThree  =123;\n"
17067                    "int oneTwo = 12;\n"
17068                    "  method();\n"
17069                    "float k= 2;\n"
17070                    "int ll=10000;\n"
17071                    "}",
17072                    AlignmentLeft));
17073   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
17074             "  int const i   = 1;\n"
17075             "  int**     j   = 2;\n"
17076             "  int&      k   = i;\n"
17077             "  int&&     l   = i + j;\n"
17078             "  int       big = 10000;\n"
17079             "\n"
17080             "  unsigned oneTwoThree = 123;\n"
17081             "  int      oneTwo      = 12;\n"
17082             "  method();\n"
17083             "  float k  = 2;\n"
17084             "  int   ll = 10000;\n"
17085             "}",
17086             format("void SomeFunction(int parameter= 0) {\n"
17087                    " int const  i= 1;\n"
17088                    "  int **j=2;\n"
17089                    "int &k=i;\n"
17090                    "int &&l=i+j;\n"
17091                    " int big  =  10000;\n"
17092                    "\n"
17093                    "unsigned oneTwoThree  =123;\n"
17094                    "int oneTwo = 12;\n"
17095                    "  method();\n"
17096                    "float k= 2;\n"
17097                    "int ll=10000;\n"
17098                    "}",
17099                    AlignmentLeft));
17100   // variables are aligned at their name, pointers are at the left most position
17101   verifyFormat("int*   a;\n"
17102                "int**  b;\n"
17103                "int*** c;\n"
17104                "int    foobar;\n",
17105                AlignmentLeft);
17106 
17107   // PAS_Middle
17108   FormatStyle AlignmentMiddle = Alignment;
17109   AlignmentMiddle.PointerAlignment = FormatStyle::PAS_Middle;
17110   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
17111             "  int const i   = 1;\n"
17112             "  int *     j   = 2;\n"
17113             "  int       big = 10000;\n"
17114             "\n"
17115             "  unsigned oneTwoThree = 123;\n"
17116             "  int      oneTwo      = 12;\n"
17117             "  method();\n"
17118             "  float k  = 2;\n"
17119             "  int   ll = 10000;\n"
17120             "}",
17121             format("void SomeFunction(int parameter= 0) {\n"
17122                    " int const  i= 1;\n"
17123                    "  int *j=2;\n"
17124                    " int big  =  10000;\n"
17125                    "\n"
17126                    "unsigned oneTwoThree  =123;\n"
17127                    "int oneTwo = 12;\n"
17128                    "  method();\n"
17129                    "float k= 2;\n"
17130                    "int ll=10000;\n"
17131                    "}",
17132                    AlignmentMiddle));
17133   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
17134             "  int const i   = 1;\n"
17135             "  int **    j   = 2, ***k;\n"
17136             "  int &     k   = i;\n"
17137             "  int &&    l   = i + j;\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             format("void SomeFunction(int parameter= 0) {\n"
17147                    " int const  i= 1;\n"
17148                    "  int **j=2,***k;\n"
17149                    "int &k=i;\n"
17150                    "int &&l=i+j;\n"
17151                    " int big  =  10000;\n"
17152                    "\n"
17153                    "unsigned oneTwoThree  =123;\n"
17154                    "int oneTwo = 12;\n"
17155                    "  method();\n"
17156                    "float k= 2;\n"
17157                    "int ll=10000;\n"
17158                    "}",
17159                    AlignmentMiddle));
17160   // variables are aligned at their name, pointers are in the middle
17161   verifyFormat("int *   a;\n"
17162                "int *   b;\n"
17163                "int *** c;\n"
17164                "int     foobar;\n",
17165                AlignmentMiddle);
17166 
17167   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
17168   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
17169   verifyFormat("#define A \\\n"
17170                "  int       aaaa = 12; \\\n"
17171                "  float     b = 23; \\\n"
17172                "  const int ccc = 234; \\\n"
17173                "  unsigned  dddddddddd = 2345;",
17174                Alignment);
17175   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
17176   verifyFormat("#define A              \\\n"
17177                "  int       aaaa = 12; \\\n"
17178                "  float     b = 23;    \\\n"
17179                "  const int ccc = 234; \\\n"
17180                "  unsigned  dddddddddd = 2345;",
17181                Alignment);
17182   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
17183   Alignment.ColumnLimit = 30;
17184   verifyFormat("#define A                    \\\n"
17185                "  int       aaaa = 12;       \\\n"
17186                "  float     b = 23;          \\\n"
17187                "  const int ccc = 234;       \\\n"
17188                "  int       dddddddddd = 2345;",
17189                Alignment);
17190   Alignment.ColumnLimit = 80;
17191   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
17192                "k = 4, int l = 5,\n"
17193                "                  int m = 6) {\n"
17194                "  const int j = 10;\n"
17195                "  otherThing = 1;\n"
17196                "}",
17197                Alignment);
17198   verifyFormat("void SomeFunction(int parameter = 0) {\n"
17199                "  int const i = 1;\n"
17200                "  int      *j = 2;\n"
17201                "  int       big = 10000;\n"
17202                "}",
17203                Alignment);
17204   verifyFormat("class C {\n"
17205                "public:\n"
17206                "  int          i = 1;\n"
17207                "  virtual void f() = 0;\n"
17208                "};",
17209                Alignment);
17210   verifyFormat("float i = 1;\n"
17211                "if (SomeType t = getSomething()) {\n"
17212                "}\n"
17213                "const unsigned j = 2;\n"
17214                "int            big = 10000;",
17215                Alignment);
17216   verifyFormat("float j = 7;\n"
17217                "for (int k = 0; k < N; ++k) {\n"
17218                "}\n"
17219                "unsigned j = 2;\n"
17220                "int      big = 10000;\n"
17221                "}",
17222                Alignment);
17223   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
17224   verifyFormat("float              i = 1;\n"
17225                "LooooooooooongType loooooooooooooooooooooongVariable\n"
17226                "    = someLooooooooooooooooongFunction();\n"
17227                "int j = 2;",
17228                Alignment);
17229   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
17230   verifyFormat("int                i = 1;\n"
17231                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
17232                "    someLooooooooooooooooongFunction();\n"
17233                "int j = 2;",
17234                Alignment);
17235 
17236   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
17237   verifyFormat("auto lambda = []() {\n"
17238                "  auto  ii = 0;\n"
17239                "  float j  = 0;\n"
17240                "  return 0;\n"
17241                "};\n"
17242                "int   i  = 0;\n"
17243                "float i2 = 0;\n"
17244                "auto  v  = type{\n"
17245                "    i = 1,   //\n"
17246                "    (i = 2), //\n"
17247                "    i = 3    //\n"
17248                "};",
17249                Alignment);
17250   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
17251 
17252   verifyFormat(
17253       "int      i = 1;\n"
17254       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
17255       "                          loooooooooooooooooooooongParameterB);\n"
17256       "int      j = 2;",
17257       Alignment);
17258 
17259   // Test interactions with ColumnLimit and AlignConsecutiveAssignments:
17260   // We expect declarations and assignments to align, as long as it doesn't
17261   // exceed the column limit, starting a new alignment sequence whenever it
17262   // happens.
17263   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
17264   Alignment.ColumnLimit = 30;
17265   verifyFormat("float    ii              = 1;\n"
17266                "unsigned j               = 2;\n"
17267                "int someVerylongVariable = 1;\n"
17268                "AnotherLongType  ll = 123456;\n"
17269                "VeryVeryLongType k  = 2;\n"
17270                "int              myvar = 1;",
17271                Alignment);
17272   Alignment.ColumnLimit = 80;
17273   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
17274 
17275   verifyFormat(
17276       "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n"
17277       "          typename LongType, typename B>\n"
17278       "auto foo() {}\n",
17279       Alignment);
17280   verifyFormat("float a, b = 1;\n"
17281                "int   c = 2;\n"
17282                "int   dd = 3;\n",
17283                Alignment);
17284   verifyFormat("int   aa = ((1 > 2) ? 3 : 4);\n"
17285                "float b[1][] = {{3.f}};\n",
17286                Alignment);
17287   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
17288   verifyFormat("float a, b = 1;\n"
17289                "int   c  = 2;\n"
17290                "int   dd = 3;\n",
17291                Alignment);
17292   verifyFormat("int   aa     = ((1 > 2) ? 3 : 4);\n"
17293                "float b[1][] = {{3.f}};\n",
17294                Alignment);
17295   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
17296 
17297   Alignment.ColumnLimit = 30;
17298   Alignment.BinPackParameters = false;
17299   verifyFormat("void foo(float     a,\n"
17300                "         float     b,\n"
17301                "         int       c,\n"
17302                "         uint32_t *d) {\n"
17303                "  int   *e = 0;\n"
17304                "  float  f = 0;\n"
17305                "  double g = 0;\n"
17306                "}\n"
17307                "void bar(ino_t     a,\n"
17308                "         int       b,\n"
17309                "         uint32_t *c,\n"
17310                "         bool      d) {}\n",
17311                Alignment);
17312   Alignment.BinPackParameters = true;
17313   Alignment.ColumnLimit = 80;
17314 
17315   // Bug 33507
17316   Alignment.PointerAlignment = FormatStyle::PAS_Middle;
17317   verifyFormat(
17318       "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n"
17319       "  static const Version verVs2017;\n"
17320       "  return true;\n"
17321       "});\n",
17322       Alignment);
17323   Alignment.PointerAlignment = FormatStyle::PAS_Right;
17324 
17325   // See llvm.org/PR35641
17326   Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
17327   verifyFormat("int func() { //\n"
17328                "  int      b;\n"
17329                "  unsigned c;\n"
17330                "}",
17331                Alignment);
17332 
17333   // See PR37175
17334   FormatStyle Style = getMozillaStyle();
17335   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
17336   EXPECT_EQ("DECOR1 /**/ int8_t /**/ DECOR2 /**/\n"
17337             "foo(int a);",
17338             format("DECOR1 /**/ int8_t /**/ DECOR2 /**/ foo (int a);", Style));
17339 
17340   Alignment.PointerAlignment = FormatStyle::PAS_Left;
17341   verifyFormat("unsigned int*       a;\n"
17342                "int*                b;\n"
17343                "unsigned int Const* c;\n"
17344                "unsigned int const* d;\n"
17345                "unsigned int Const& e;\n"
17346                "unsigned int const& f;",
17347                Alignment);
17348   verifyFormat("Const unsigned int* c;\n"
17349                "const unsigned int* d;\n"
17350                "Const unsigned int& e;\n"
17351                "const unsigned int& f;\n"
17352                "const unsigned      g;\n"
17353                "Const unsigned      h;",
17354                Alignment);
17355 
17356   Alignment.PointerAlignment = FormatStyle::PAS_Middle;
17357   verifyFormat("unsigned int *       a;\n"
17358                "int *                b;\n"
17359                "unsigned int Const * c;\n"
17360                "unsigned int const * d;\n"
17361                "unsigned int Const & e;\n"
17362                "unsigned int const & f;",
17363                Alignment);
17364   verifyFormat("Const unsigned int * c;\n"
17365                "const unsigned int * d;\n"
17366                "Const unsigned int & e;\n"
17367                "const unsigned int & f;\n"
17368                "const unsigned       g;\n"
17369                "Const unsigned       h;",
17370                Alignment);
17371 
17372   // See PR46529
17373   FormatStyle BracedAlign = getLLVMStyle();
17374   BracedAlign.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
17375   verifyFormat("const auto result{[]() {\n"
17376                "  const auto something = 1;\n"
17377                "  return 2;\n"
17378                "}};",
17379                BracedAlign);
17380   verifyFormat("int foo{[]() {\n"
17381                "  int bar{0};\n"
17382                "  return 0;\n"
17383                "}()};",
17384                BracedAlign);
17385   BracedAlign.Cpp11BracedListStyle = false;
17386   verifyFormat("const auto result{ []() {\n"
17387                "  const auto something = 1;\n"
17388                "  return 2;\n"
17389                "} };",
17390                BracedAlign);
17391   verifyFormat("int foo{ []() {\n"
17392                "  int bar{ 0 };\n"
17393                "  return 0;\n"
17394                "}() };",
17395                BracedAlign);
17396 }
17397 
17398 TEST_F(FormatTest, AlignWithLineBreaks) {
17399   auto Style = getLLVMStyleWithColumns(120);
17400 
17401   EXPECT_EQ(Style.AlignConsecutiveAssignments, FormatStyle::ACS_None);
17402   EXPECT_EQ(Style.AlignConsecutiveDeclarations, FormatStyle::ACS_None);
17403   verifyFormat("void foo() {\n"
17404                "  int myVar = 5;\n"
17405                "  double x = 3.14;\n"
17406                "  auto str = \"Hello \"\n"
17407                "             \"World\";\n"
17408                "  auto s = \"Hello \"\n"
17409                "           \"Again\";\n"
17410                "}",
17411                Style);
17412 
17413   // clang-format off
17414   verifyFormat("void foo() {\n"
17415                "  const int capacityBefore = Entries.capacity();\n"
17416                "  const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
17417                "                                            std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
17418                "  const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
17419                "                                          std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
17420                "}",
17421                Style);
17422   // clang-format on
17423 
17424   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
17425   verifyFormat("void foo() {\n"
17426                "  int myVar = 5;\n"
17427                "  double x  = 3.14;\n"
17428                "  auto str  = \"Hello \"\n"
17429                "              \"World\";\n"
17430                "  auto s    = \"Hello \"\n"
17431                "              \"Again\";\n"
17432                "}",
17433                Style);
17434 
17435   // clang-format off
17436   verifyFormat("void foo() {\n"
17437                "  const int capacityBefore = Entries.capacity();\n"
17438                "  const auto newEntry      = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
17439                "                                                 std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
17440                "  const X newEntry2        = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
17441                "                                                 std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
17442                "}",
17443                Style);
17444   // clang-format on
17445 
17446   Style.AlignConsecutiveAssignments = FormatStyle::ACS_None;
17447   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
17448   verifyFormat("void foo() {\n"
17449                "  int    myVar = 5;\n"
17450                "  double x = 3.14;\n"
17451                "  auto   str = \"Hello \"\n"
17452                "               \"World\";\n"
17453                "  auto   s = \"Hello \"\n"
17454                "             \"Again\";\n"
17455                "}",
17456                Style);
17457 
17458   // clang-format off
17459   verifyFormat("void foo() {\n"
17460                "  const int  capacityBefore = Entries.capacity();\n"
17461                "  const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
17462                "                                            std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
17463                "  const X    newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
17464                "                                             std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
17465                "}",
17466                Style);
17467   // clang-format on
17468 
17469   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
17470   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
17471 
17472   verifyFormat("void foo() {\n"
17473                "  int    myVar = 5;\n"
17474                "  double x     = 3.14;\n"
17475                "  auto   str   = \"Hello \"\n"
17476                "                 \"World\";\n"
17477                "  auto   s     = \"Hello \"\n"
17478                "                 \"Again\";\n"
17479                "}",
17480                Style);
17481 
17482   // clang-format off
17483   verifyFormat("void foo() {\n"
17484                "  const int  capacityBefore = Entries.capacity();\n"
17485                "  const auto newEntry       = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
17486                "                                                  std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
17487                "  const X    newEntry2      = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
17488                "                                                  std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
17489                "}",
17490                Style);
17491   // clang-format on
17492 
17493   Style = getLLVMStyleWithColumns(120);
17494   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
17495   Style.ContinuationIndentWidth = 4;
17496   Style.IndentWidth = 4;
17497 
17498   // clang-format off
17499   verifyFormat("void SomeFunc() {\n"
17500                "    newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n"
17501                "                                                        seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
17502                "    newWatcher.maxAge     = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n"
17503                "                                                        seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
17504                "    newWatcher.max        = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n"
17505                "                                                        seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
17506                "}",
17507                Style);
17508   // clang-format on
17509 
17510   Style.BinPackArguments = false;
17511 
17512   // clang-format off
17513   verifyFormat("void SomeFunc() {\n"
17514                "    newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(\n"
17515                "        FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
17516                "    newWatcher.maxAge     = ToLegacyTimestamp(GetMaxAge(\n"
17517                "        FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
17518                "    newWatcher.max        = ToLegacyTimestamp(GetMaxAge(\n"
17519                "        FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
17520                "}",
17521                Style);
17522   // clang-format on
17523 }
17524 
17525 TEST_F(FormatTest, AlignWithInitializerPeriods) {
17526   auto Style = getLLVMStyleWithColumns(60);
17527 
17528   verifyFormat("void foo1(void) {\n"
17529                "  BYTE p[1] = 1;\n"
17530                "  A B = {.one_foooooooooooooooo = 2,\n"
17531                "         .two_fooooooooooooo = 3,\n"
17532                "         .three_fooooooooooooo = 4};\n"
17533                "  BYTE payload = 2;\n"
17534                "}",
17535                Style);
17536 
17537   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
17538   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_None;
17539   verifyFormat("void foo2(void) {\n"
17540                "  BYTE p[1]    = 1;\n"
17541                "  A B          = {.one_foooooooooooooooo = 2,\n"
17542                "                  .two_fooooooooooooo    = 3,\n"
17543                "                  .three_fooooooooooooo  = 4};\n"
17544                "  BYTE payload = 2;\n"
17545                "}",
17546                Style);
17547 
17548   Style.AlignConsecutiveAssignments = FormatStyle::ACS_None;
17549   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
17550   verifyFormat("void foo3(void) {\n"
17551                "  BYTE p[1] = 1;\n"
17552                "  A    B = {.one_foooooooooooooooo = 2,\n"
17553                "            .two_fooooooooooooo = 3,\n"
17554                "            .three_fooooooooooooo = 4};\n"
17555                "  BYTE payload = 2;\n"
17556                "}",
17557                Style);
17558 
17559   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
17560   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
17561   verifyFormat("void foo4(void) {\n"
17562                "  BYTE p[1]    = 1;\n"
17563                "  A    B       = {.one_foooooooooooooooo = 2,\n"
17564                "                  .two_fooooooooooooo    = 3,\n"
17565                "                  .three_fooooooooooooo  = 4};\n"
17566                "  BYTE payload = 2;\n"
17567                "}",
17568                Style);
17569 }
17570 
17571 TEST_F(FormatTest, LinuxBraceBreaking) {
17572   FormatStyle LinuxBraceStyle = getLLVMStyle();
17573   LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
17574   verifyFormat("namespace a\n"
17575                "{\n"
17576                "class A\n"
17577                "{\n"
17578                "  void f()\n"
17579                "  {\n"
17580                "    if (true) {\n"
17581                "      a();\n"
17582                "      b();\n"
17583                "    } else {\n"
17584                "      a();\n"
17585                "    }\n"
17586                "  }\n"
17587                "  void g() { return; }\n"
17588                "};\n"
17589                "struct B {\n"
17590                "  int x;\n"
17591                "};\n"
17592                "} // namespace a\n",
17593                LinuxBraceStyle);
17594   verifyFormat("enum X {\n"
17595                "  Y = 0,\n"
17596                "}\n",
17597                LinuxBraceStyle);
17598   verifyFormat("struct S {\n"
17599                "  int Type;\n"
17600                "  union {\n"
17601                "    int x;\n"
17602                "    double y;\n"
17603                "  } Value;\n"
17604                "  class C\n"
17605                "  {\n"
17606                "    MyFavoriteType Value;\n"
17607                "  } Class;\n"
17608                "}\n",
17609                LinuxBraceStyle);
17610 }
17611 
17612 TEST_F(FormatTest, MozillaBraceBreaking) {
17613   FormatStyle MozillaBraceStyle = getLLVMStyle();
17614   MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
17615   MozillaBraceStyle.FixNamespaceComments = false;
17616   verifyFormat("namespace a {\n"
17617                "class A\n"
17618                "{\n"
17619                "  void f()\n"
17620                "  {\n"
17621                "    if (true) {\n"
17622                "      a();\n"
17623                "      b();\n"
17624                "    }\n"
17625                "  }\n"
17626                "  void g() { return; }\n"
17627                "};\n"
17628                "enum E\n"
17629                "{\n"
17630                "  A,\n"
17631                "  // foo\n"
17632                "  B,\n"
17633                "  C\n"
17634                "};\n"
17635                "struct B\n"
17636                "{\n"
17637                "  int x;\n"
17638                "};\n"
17639                "}\n",
17640                MozillaBraceStyle);
17641   verifyFormat("struct S\n"
17642                "{\n"
17643                "  int Type;\n"
17644                "  union\n"
17645                "  {\n"
17646                "    int x;\n"
17647                "    double y;\n"
17648                "  } Value;\n"
17649                "  class C\n"
17650                "  {\n"
17651                "    MyFavoriteType Value;\n"
17652                "  } Class;\n"
17653                "}\n",
17654                MozillaBraceStyle);
17655 }
17656 
17657 TEST_F(FormatTest, StroustrupBraceBreaking) {
17658   FormatStyle StroustrupBraceStyle = getLLVMStyle();
17659   StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
17660   verifyFormat("namespace a {\n"
17661                "class A {\n"
17662                "  void f()\n"
17663                "  {\n"
17664                "    if (true) {\n"
17665                "      a();\n"
17666                "      b();\n"
17667                "    }\n"
17668                "  }\n"
17669                "  void g() { return; }\n"
17670                "};\n"
17671                "struct B {\n"
17672                "  int x;\n"
17673                "};\n"
17674                "} // namespace a\n",
17675                StroustrupBraceStyle);
17676 
17677   verifyFormat("void foo()\n"
17678                "{\n"
17679                "  if (a) {\n"
17680                "    a();\n"
17681                "  }\n"
17682                "  else {\n"
17683                "    b();\n"
17684                "  }\n"
17685                "}\n",
17686                StroustrupBraceStyle);
17687 
17688   verifyFormat("#ifdef _DEBUG\n"
17689                "int foo(int i = 0)\n"
17690                "#else\n"
17691                "int foo(int i = 5)\n"
17692                "#endif\n"
17693                "{\n"
17694                "  return i;\n"
17695                "}",
17696                StroustrupBraceStyle);
17697 
17698   verifyFormat("void foo() {}\n"
17699                "void bar()\n"
17700                "#ifdef _DEBUG\n"
17701                "{\n"
17702                "  foo();\n"
17703                "}\n"
17704                "#else\n"
17705                "{\n"
17706                "}\n"
17707                "#endif",
17708                StroustrupBraceStyle);
17709 
17710   verifyFormat("void foobar() { int i = 5; }\n"
17711                "#ifdef _DEBUG\n"
17712                "void bar() {}\n"
17713                "#else\n"
17714                "void bar() { foobar(); }\n"
17715                "#endif",
17716                StroustrupBraceStyle);
17717 }
17718 
17719 TEST_F(FormatTest, AllmanBraceBreaking) {
17720   FormatStyle AllmanBraceStyle = getLLVMStyle();
17721   AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
17722 
17723   EXPECT_EQ("namespace a\n"
17724             "{\n"
17725             "void f();\n"
17726             "void g();\n"
17727             "} // namespace a\n",
17728             format("namespace a\n"
17729                    "{\n"
17730                    "void f();\n"
17731                    "void g();\n"
17732                    "}\n",
17733                    AllmanBraceStyle));
17734 
17735   verifyFormat("namespace a\n"
17736                "{\n"
17737                "class A\n"
17738                "{\n"
17739                "  void f()\n"
17740                "  {\n"
17741                "    if (true)\n"
17742                "    {\n"
17743                "      a();\n"
17744                "      b();\n"
17745                "    }\n"
17746                "  }\n"
17747                "  void g() { return; }\n"
17748                "};\n"
17749                "struct B\n"
17750                "{\n"
17751                "  int x;\n"
17752                "};\n"
17753                "union C\n"
17754                "{\n"
17755                "};\n"
17756                "} // namespace a",
17757                AllmanBraceStyle);
17758 
17759   verifyFormat("void f()\n"
17760                "{\n"
17761                "  if (true)\n"
17762                "  {\n"
17763                "    a();\n"
17764                "  }\n"
17765                "  else if (false)\n"
17766                "  {\n"
17767                "    b();\n"
17768                "  }\n"
17769                "  else\n"
17770                "  {\n"
17771                "    c();\n"
17772                "  }\n"
17773                "}\n",
17774                AllmanBraceStyle);
17775 
17776   verifyFormat("void f()\n"
17777                "{\n"
17778                "  for (int i = 0; i < 10; ++i)\n"
17779                "  {\n"
17780                "    a();\n"
17781                "  }\n"
17782                "  while (false)\n"
17783                "  {\n"
17784                "    b();\n"
17785                "  }\n"
17786                "  do\n"
17787                "  {\n"
17788                "    c();\n"
17789                "  } while (false)\n"
17790                "}\n",
17791                AllmanBraceStyle);
17792 
17793   verifyFormat("void f(int a)\n"
17794                "{\n"
17795                "  switch (a)\n"
17796                "  {\n"
17797                "  case 0:\n"
17798                "    break;\n"
17799                "  case 1:\n"
17800                "  {\n"
17801                "    break;\n"
17802                "  }\n"
17803                "  case 2:\n"
17804                "  {\n"
17805                "  }\n"
17806                "  break;\n"
17807                "  default:\n"
17808                "    break;\n"
17809                "  }\n"
17810                "}\n",
17811                AllmanBraceStyle);
17812 
17813   verifyFormat("enum X\n"
17814                "{\n"
17815                "  Y = 0,\n"
17816                "}\n",
17817                AllmanBraceStyle);
17818   verifyFormat("enum X\n"
17819                "{\n"
17820                "  Y = 0\n"
17821                "}\n",
17822                AllmanBraceStyle);
17823 
17824   verifyFormat("@interface BSApplicationController ()\n"
17825                "{\n"
17826                "@private\n"
17827                "  id _extraIvar;\n"
17828                "}\n"
17829                "@end\n",
17830                AllmanBraceStyle);
17831 
17832   verifyFormat("#ifdef _DEBUG\n"
17833                "int foo(int i = 0)\n"
17834                "#else\n"
17835                "int foo(int i = 5)\n"
17836                "#endif\n"
17837                "{\n"
17838                "  return i;\n"
17839                "}",
17840                AllmanBraceStyle);
17841 
17842   verifyFormat("void foo() {}\n"
17843                "void bar()\n"
17844                "#ifdef _DEBUG\n"
17845                "{\n"
17846                "  foo();\n"
17847                "}\n"
17848                "#else\n"
17849                "{\n"
17850                "}\n"
17851                "#endif",
17852                AllmanBraceStyle);
17853 
17854   verifyFormat("void foobar() { int i = 5; }\n"
17855                "#ifdef _DEBUG\n"
17856                "void bar() {}\n"
17857                "#else\n"
17858                "void bar() { foobar(); }\n"
17859                "#endif",
17860                AllmanBraceStyle);
17861 
17862   EXPECT_EQ(AllmanBraceStyle.AllowShortLambdasOnASingleLine,
17863             FormatStyle::SLS_All);
17864 
17865   verifyFormat("[](int i) { return i + 2; };\n"
17866                "[](int i, int j)\n"
17867                "{\n"
17868                "  auto x = i + j;\n"
17869                "  auto y = i * j;\n"
17870                "  return x ^ y;\n"
17871                "};\n"
17872                "void foo()\n"
17873                "{\n"
17874                "  auto shortLambda = [](int i) { return i + 2; };\n"
17875                "  auto longLambda = [](int i, int j)\n"
17876                "  {\n"
17877                "    auto x = i + j;\n"
17878                "    auto y = i * j;\n"
17879                "    return x ^ y;\n"
17880                "  };\n"
17881                "}",
17882                AllmanBraceStyle);
17883 
17884   AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
17885 
17886   verifyFormat("[](int i)\n"
17887                "{\n"
17888                "  return i + 2;\n"
17889                "};\n"
17890                "[](int i, int j)\n"
17891                "{\n"
17892                "  auto x = i + j;\n"
17893                "  auto y = i * j;\n"
17894                "  return x ^ y;\n"
17895                "};\n"
17896                "void foo()\n"
17897                "{\n"
17898                "  auto shortLambda = [](int i)\n"
17899                "  {\n"
17900                "    return i + 2;\n"
17901                "  };\n"
17902                "  auto longLambda = [](int i, int j)\n"
17903                "  {\n"
17904                "    auto x = i + j;\n"
17905                "    auto y = i * j;\n"
17906                "    return x ^ y;\n"
17907                "  };\n"
17908                "}",
17909                AllmanBraceStyle);
17910 
17911   // Reset
17912   AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_All;
17913 
17914   // This shouldn't affect ObjC blocks..
17915   verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
17916                "  // ...\n"
17917                "  int i;\n"
17918                "}];",
17919                AllmanBraceStyle);
17920   verifyFormat("void (^block)(void) = ^{\n"
17921                "  // ...\n"
17922                "  int i;\n"
17923                "};",
17924                AllmanBraceStyle);
17925   // .. or dict literals.
17926   verifyFormat("void f()\n"
17927                "{\n"
17928                "  // ...\n"
17929                "  [object someMethod:@{@\"a\" : @\"b\"}];\n"
17930                "}",
17931                AllmanBraceStyle);
17932   verifyFormat("void f()\n"
17933                "{\n"
17934                "  // ...\n"
17935                "  [object someMethod:@{a : @\"b\"}];\n"
17936                "}",
17937                AllmanBraceStyle);
17938   verifyFormat("int f()\n"
17939                "{ // comment\n"
17940                "  return 42;\n"
17941                "}",
17942                AllmanBraceStyle);
17943 
17944   AllmanBraceStyle.ColumnLimit = 19;
17945   verifyFormat("void f() { int i; }", AllmanBraceStyle);
17946   AllmanBraceStyle.ColumnLimit = 18;
17947   verifyFormat("void f()\n"
17948                "{\n"
17949                "  int i;\n"
17950                "}",
17951                AllmanBraceStyle);
17952   AllmanBraceStyle.ColumnLimit = 80;
17953 
17954   FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle;
17955   BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine =
17956       FormatStyle::SIS_WithoutElse;
17957   BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
17958   verifyFormat("void f(bool b)\n"
17959                "{\n"
17960                "  if (b)\n"
17961                "  {\n"
17962                "    return;\n"
17963                "  }\n"
17964                "}\n",
17965                BreakBeforeBraceShortIfs);
17966   verifyFormat("void f(bool b)\n"
17967                "{\n"
17968                "  if constexpr (b)\n"
17969                "  {\n"
17970                "    return;\n"
17971                "  }\n"
17972                "}\n",
17973                BreakBeforeBraceShortIfs);
17974   verifyFormat("void f(bool b)\n"
17975                "{\n"
17976                "  if CONSTEXPR (b)\n"
17977                "  {\n"
17978                "    return;\n"
17979                "  }\n"
17980                "}\n",
17981                BreakBeforeBraceShortIfs);
17982   verifyFormat("void f(bool b)\n"
17983                "{\n"
17984                "  if (b) return;\n"
17985                "}\n",
17986                BreakBeforeBraceShortIfs);
17987   verifyFormat("void f(bool b)\n"
17988                "{\n"
17989                "  if constexpr (b) return;\n"
17990                "}\n",
17991                BreakBeforeBraceShortIfs);
17992   verifyFormat("void f(bool b)\n"
17993                "{\n"
17994                "  if CONSTEXPR (b) return;\n"
17995                "}\n",
17996                BreakBeforeBraceShortIfs);
17997   verifyFormat("void f(bool b)\n"
17998                "{\n"
17999                "  while (b)\n"
18000                "  {\n"
18001                "    return;\n"
18002                "  }\n"
18003                "}\n",
18004                BreakBeforeBraceShortIfs);
18005 }
18006 
18007 TEST_F(FormatTest, WhitesmithsBraceBreaking) {
18008   FormatStyle WhitesmithsBraceStyle = getLLVMStyleWithColumns(0);
18009   WhitesmithsBraceStyle.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
18010 
18011   // Make a few changes to the style for testing purposes
18012   WhitesmithsBraceStyle.AllowShortFunctionsOnASingleLine =
18013       FormatStyle::SFS_Empty;
18014   WhitesmithsBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
18015 
18016   // FIXME: this test case can't decide whether there should be a blank line
18017   // after the ~D() line or not. It adds one if one doesn't exist in the test
18018   // and it removes the line if one exists.
18019   /*
18020   verifyFormat("class A;\n"
18021                "namespace B\n"
18022                "  {\n"
18023                "class C;\n"
18024                "// Comment\n"
18025                "class D\n"
18026                "  {\n"
18027                "public:\n"
18028                "  D();\n"
18029                "  ~D() {}\n"
18030                "private:\n"
18031                "  enum E\n"
18032                "    {\n"
18033                "    F\n"
18034                "    }\n"
18035                "  };\n"
18036                "  } // namespace B\n",
18037                WhitesmithsBraceStyle);
18038   */
18039 
18040   WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_None;
18041   verifyFormat("namespace a\n"
18042                "  {\n"
18043                "class A\n"
18044                "  {\n"
18045                "  void f()\n"
18046                "    {\n"
18047                "    if (true)\n"
18048                "      {\n"
18049                "      a();\n"
18050                "      b();\n"
18051                "      }\n"
18052                "    }\n"
18053                "  void g()\n"
18054                "    {\n"
18055                "    return;\n"
18056                "    }\n"
18057                "  };\n"
18058                "struct B\n"
18059                "  {\n"
18060                "  int x;\n"
18061                "  };\n"
18062                "  } // namespace a",
18063                WhitesmithsBraceStyle);
18064 
18065   verifyFormat("namespace a\n"
18066                "  {\n"
18067                "namespace b\n"
18068                "  {\n"
18069                "class A\n"
18070                "  {\n"
18071                "  void f()\n"
18072                "    {\n"
18073                "    if (true)\n"
18074                "      {\n"
18075                "      a();\n"
18076                "      b();\n"
18077                "      }\n"
18078                "    }\n"
18079                "  void g()\n"
18080                "    {\n"
18081                "    return;\n"
18082                "    }\n"
18083                "  };\n"
18084                "struct B\n"
18085                "  {\n"
18086                "  int x;\n"
18087                "  };\n"
18088                "  } // namespace b\n"
18089                "  } // namespace a",
18090                WhitesmithsBraceStyle);
18091 
18092   WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_Inner;
18093   verifyFormat("namespace a\n"
18094                "  {\n"
18095                "namespace b\n"
18096                "  {\n"
18097                "  class A\n"
18098                "    {\n"
18099                "    void f()\n"
18100                "      {\n"
18101                "      if (true)\n"
18102                "        {\n"
18103                "        a();\n"
18104                "        b();\n"
18105                "        }\n"
18106                "      }\n"
18107                "    void g()\n"
18108                "      {\n"
18109                "      return;\n"
18110                "      }\n"
18111                "    };\n"
18112                "  struct B\n"
18113                "    {\n"
18114                "    int x;\n"
18115                "    };\n"
18116                "  } // namespace b\n"
18117                "  } // namespace a",
18118                WhitesmithsBraceStyle);
18119 
18120   WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_All;
18121   verifyFormat("namespace a\n"
18122                "  {\n"
18123                "  namespace b\n"
18124                "    {\n"
18125                "    class A\n"
18126                "      {\n"
18127                "      void f()\n"
18128                "        {\n"
18129                "        if (true)\n"
18130                "          {\n"
18131                "          a();\n"
18132                "          b();\n"
18133                "          }\n"
18134                "        }\n"
18135                "      void g()\n"
18136                "        {\n"
18137                "        return;\n"
18138                "        }\n"
18139                "      };\n"
18140                "    struct B\n"
18141                "      {\n"
18142                "      int x;\n"
18143                "      };\n"
18144                "    } // namespace b\n"
18145                "  }   // namespace a",
18146                WhitesmithsBraceStyle);
18147 
18148   verifyFormat("void f()\n"
18149                "  {\n"
18150                "  if (true)\n"
18151                "    {\n"
18152                "    a();\n"
18153                "    }\n"
18154                "  else if (false)\n"
18155                "    {\n"
18156                "    b();\n"
18157                "    }\n"
18158                "  else\n"
18159                "    {\n"
18160                "    c();\n"
18161                "    }\n"
18162                "  }\n",
18163                WhitesmithsBraceStyle);
18164 
18165   verifyFormat("void f()\n"
18166                "  {\n"
18167                "  for (int i = 0; i < 10; ++i)\n"
18168                "    {\n"
18169                "    a();\n"
18170                "    }\n"
18171                "  while (false)\n"
18172                "    {\n"
18173                "    b();\n"
18174                "    }\n"
18175                "  do\n"
18176                "    {\n"
18177                "    c();\n"
18178                "    } while (false)\n"
18179                "  }\n",
18180                WhitesmithsBraceStyle);
18181 
18182   WhitesmithsBraceStyle.IndentCaseLabels = true;
18183   verifyFormat("void switchTest1(int a)\n"
18184                "  {\n"
18185                "  switch (a)\n"
18186                "    {\n"
18187                "    case 2:\n"
18188                "      {\n"
18189                "      }\n"
18190                "      break;\n"
18191                "    }\n"
18192                "  }\n",
18193                WhitesmithsBraceStyle);
18194 
18195   verifyFormat("void switchTest2(int a)\n"
18196                "  {\n"
18197                "  switch (a)\n"
18198                "    {\n"
18199                "    case 0:\n"
18200                "      break;\n"
18201                "    case 1:\n"
18202                "      {\n"
18203                "      break;\n"
18204                "      }\n"
18205                "    case 2:\n"
18206                "      {\n"
18207                "      }\n"
18208                "      break;\n"
18209                "    default:\n"
18210                "      break;\n"
18211                "    }\n"
18212                "  }\n",
18213                WhitesmithsBraceStyle);
18214 
18215   verifyFormat("void switchTest3(int a)\n"
18216                "  {\n"
18217                "  switch (a)\n"
18218                "    {\n"
18219                "    case 0:\n"
18220                "      {\n"
18221                "      foo(x);\n"
18222                "      }\n"
18223                "      break;\n"
18224                "    default:\n"
18225                "      {\n"
18226                "      foo(1);\n"
18227                "      }\n"
18228                "      break;\n"
18229                "    }\n"
18230                "  }\n",
18231                WhitesmithsBraceStyle);
18232 
18233   WhitesmithsBraceStyle.IndentCaseLabels = false;
18234 
18235   verifyFormat("void switchTest4(int a)\n"
18236                "  {\n"
18237                "  switch (a)\n"
18238                "    {\n"
18239                "  case 2:\n"
18240                "    {\n"
18241                "    }\n"
18242                "    break;\n"
18243                "    }\n"
18244                "  }\n",
18245                WhitesmithsBraceStyle);
18246 
18247   verifyFormat("void switchTest5(int a)\n"
18248                "  {\n"
18249                "  switch (a)\n"
18250                "    {\n"
18251                "  case 0:\n"
18252                "    break;\n"
18253                "  case 1:\n"
18254                "    {\n"
18255                "    foo();\n"
18256                "    break;\n"
18257                "    }\n"
18258                "  case 2:\n"
18259                "    {\n"
18260                "    }\n"
18261                "    break;\n"
18262                "  default:\n"
18263                "    break;\n"
18264                "    }\n"
18265                "  }\n",
18266                WhitesmithsBraceStyle);
18267 
18268   verifyFormat("void switchTest6(int a)\n"
18269                "  {\n"
18270                "  switch (a)\n"
18271                "    {\n"
18272                "  case 0:\n"
18273                "    {\n"
18274                "    foo(x);\n"
18275                "    }\n"
18276                "    break;\n"
18277                "  default:\n"
18278                "    {\n"
18279                "    foo(1);\n"
18280                "    }\n"
18281                "    break;\n"
18282                "    }\n"
18283                "  }\n",
18284                WhitesmithsBraceStyle);
18285 
18286   verifyFormat("enum X\n"
18287                "  {\n"
18288                "  Y = 0, // testing\n"
18289                "  }\n",
18290                WhitesmithsBraceStyle);
18291 
18292   verifyFormat("enum X\n"
18293                "  {\n"
18294                "  Y = 0\n"
18295                "  }\n",
18296                WhitesmithsBraceStyle);
18297   verifyFormat("enum X\n"
18298                "  {\n"
18299                "  Y = 0,\n"
18300                "  Z = 1\n"
18301                "  };\n",
18302                WhitesmithsBraceStyle);
18303 
18304   verifyFormat("@interface BSApplicationController ()\n"
18305                "  {\n"
18306                "@private\n"
18307                "  id _extraIvar;\n"
18308                "  }\n"
18309                "@end\n",
18310                WhitesmithsBraceStyle);
18311 
18312   verifyFormat("#ifdef _DEBUG\n"
18313                "int foo(int i = 0)\n"
18314                "#else\n"
18315                "int foo(int i = 5)\n"
18316                "#endif\n"
18317                "  {\n"
18318                "  return i;\n"
18319                "  }",
18320                WhitesmithsBraceStyle);
18321 
18322   verifyFormat("void foo() {}\n"
18323                "void bar()\n"
18324                "#ifdef _DEBUG\n"
18325                "  {\n"
18326                "  foo();\n"
18327                "  }\n"
18328                "#else\n"
18329                "  {\n"
18330                "  }\n"
18331                "#endif",
18332                WhitesmithsBraceStyle);
18333 
18334   verifyFormat("void foobar()\n"
18335                "  {\n"
18336                "  int i = 5;\n"
18337                "  }\n"
18338                "#ifdef _DEBUG\n"
18339                "void bar()\n"
18340                "  {\n"
18341                "  }\n"
18342                "#else\n"
18343                "void bar()\n"
18344                "  {\n"
18345                "  foobar();\n"
18346                "  }\n"
18347                "#endif",
18348                WhitesmithsBraceStyle);
18349 
18350   // This shouldn't affect ObjC blocks..
18351   verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
18352                "  // ...\n"
18353                "  int i;\n"
18354                "}];",
18355                WhitesmithsBraceStyle);
18356   verifyFormat("void (^block)(void) = ^{\n"
18357                "  // ...\n"
18358                "  int i;\n"
18359                "};",
18360                WhitesmithsBraceStyle);
18361   // .. or dict literals.
18362   verifyFormat("void f()\n"
18363                "  {\n"
18364                "  [object someMethod:@{@\"a\" : @\"b\"}];\n"
18365                "  }",
18366                WhitesmithsBraceStyle);
18367 
18368   verifyFormat("int f()\n"
18369                "  { // comment\n"
18370                "  return 42;\n"
18371                "  }",
18372                WhitesmithsBraceStyle);
18373 
18374   FormatStyle BreakBeforeBraceShortIfs = WhitesmithsBraceStyle;
18375   BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine =
18376       FormatStyle::SIS_OnlyFirstIf;
18377   BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
18378   verifyFormat("void f(bool b)\n"
18379                "  {\n"
18380                "  if (b)\n"
18381                "    {\n"
18382                "    return;\n"
18383                "    }\n"
18384                "  }\n",
18385                BreakBeforeBraceShortIfs);
18386   verifyFormat("void f(bool b)\n"
18387                "  {\n"
18388                "  if (b) return;\n"
18389                "  }\n",
18390                BreakBeforeBraceShortIfs);
18391   verifyFormat("void f(bool b)\n"
18392                "  {\n"
18393                "  while (b)\n"
18394                "    {\n"
18395                "    return;\n"
18396                "    }\n"
18397                "  }\n",
18398                BreakBeforeBraceShortIfs);
18399 }
18400 
18401 TEST_F(FormatTest, GNUBraceBreaking) {
18402   FormatStyle GNUBraceStyle = getLLVMStyle();
18403   GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
18404   verifyFormat("namespace a\n"
18405                "{\n"
18406                "class A\n"
18407                "{\n"
18408                "  void f()\n"
18409                "  {\n"
18410                "    int a;\n"
18411                "    {\n"
18412                "      int b;\n"
18413                "    }\n"
18414                "    if (true)\n"
18415                "      {\n"
18416                "        a();\n"
18417                "        b();\n"
18418                "      }\n"
18419                "  }\n"
18420                "  void g() { return; }\n"
18421                "}\n"
18422                "} // namespace a",
18423                GNUBraceStyle);
18424 
18425   verifyFormat("void f()\n"
18426                "{\n"
18427                "  if (true)\n"
18428                "    {\n"
18429                "      a();\n"
18430                "    }\n"
18431                "  else if (false)\n"
18432                "    {\n"
18433                "      b();\n"
18434                "    }\n"
18435                "  else\n"
18436                "    {\n"
18437                "      c();\n"
18438                "    }\n"
18439                "}\n",
18440                GNUBraceStyle);
18441 
18442   verifyFormat("void f()\n"
18443                "{\n"
18444                "  for (int i = 0; i < 10; ++i)\n"
18445                "    {\n"
18446                "      a();\n"
18447                "    }\n"
18448                "  while (false)\n"
18449                "    {\n"
18450                "      b();\n"
18451                "    }\n"
18452                "  do\n"
18453                "    {\n"
18454                "      c();\n"
18455                "    }\n"
18456                "  while (false);\n"
18457                "}\n",
18458                GNUBraceStyle);
18459 
18460   verifyFormat("void f(int a)\n"
18461                "{\n"
18462                "  switch (a)\n"
18463                "    {\n"
18464                "    case 0:\n"
18465                "      break;\n"
18466                "    case 1:\n"
18467                "      {\n"
18468                "        break;\n"
18469                "      }\n"
18470                "    case 2:\n"
18471                "      {\n"
18472                "      }\n"
18473                "      break;\n"
18474                "    default:\n"
18475                "      break;\n"
18476                "    }\n"
18477                "}\n",
18478                GNUBraceStyle);
18479 
18480   verifyFormat("enum X\n"
18481                "{\n"
18482                "  Y = 0,\n"
18483                "}\n",
18484                GNUBraceStyle);
18485 
18486   verifyFormat("@interface BSApplicationController ()\n"
18487                "{\n"
18488                "@private\n"
18489                "  id _extraIvar;\n"
18490                "}\n"
18491                "@end\n",
18492                GNUBraceStyle);
18493 
18494   verifyFormat("#ifdef _DEBUG\n"
18495                "int foo(int i = 0)\n"
18496                "#else\n"
18497                "int foo(int i = 5)\n"
18498                "#endif\n"
18499                "{\n"
18500                "  return i;\n"
18501                "}",
18502                GNUBraceStyle);
18503 
18504   verifyFormat("void foo() {}\n"
18505                "void bar()\n"
18506                "#ifdef _DEBUG\n"
18507                "{\n"
18508                "  foo();\n"
18509                "}\n"
18510                "#else\n"
18511                "{\n"
18512                "}\n"
18513                "#endif",
18514                GNUBraceStyle);
18515 
18516   verifyFormat("void foobar() { int i = 5; }\n"
18517                "#ifdef _DEBUG\n"
18518                "void bar() {}\n"
18519                "#else\n"
18520                "void bar() { foobar(); }\n"
18521                "#endif",
18522                GNUBraceStyle);
18523 }
18524 
18525 TEST_F(FormatTest, WebKitBraceBreaking) {
18526   FormatStyle WebKitBraceStyle = getLLVMStyle();
18527   WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit;
18528   WebKitBraceStyle.FixNamespaceComments = false;
18529   verifyFormat("namespace a {\n"
18530                "class A {\n"
18531                "  void f()\n"
18532                "  {\n"
18533                "    if (true) {\n"
18534                "      a();\n"
18535                "      b();\n"
18536                "    }\n"
18537                "  }\n"
18538                "  void g() { return; }\n"
18539                "};\n"
18540                "enum E {\n"
18541                "  A,\n"
18542                "  // foo\n"
18543                "  B,\n"
18544                "  C\n"
18545                "};\n"
18546                "struct B {\n"
18547                "  int x;\n"
18548                "};\n"
18549                "}\n",
18550                WebKitBraceStyle);
18551   verifyFormat("struct S {\n"
18552                "  int Type;\n"
18553                "  union {\n"
18554                "    int x;\n"
18555                "    double y;\n"
18556                "  } Value;\n"
18557                "  class C {\n"
18558                "    MyFavoriteType Value;\n"
18559                "  } Class;\n"
18560                "};\n",
18561                WebKitBraceStyle);
18562 }
18563 
18564 TEST_F(FormatTest, CatchExceptionReferenceBinding) {
18565   verifyFormat("void f() {\n"
18566                "  try {\n"
18567                "  } catch (const Exception &e) {\n"
18568                "  }\n"
18569                "}\n",
18570                getLLVMStyle());
18571 }
18572 
18573 TEST_F(FormatTest, CatchAlignArrayOfStructuresRightAlignment) {
18574   auto Style = getLLVMStyle();
18575   Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
18576   Style.AlignConsecutiveAssignments =
18577       FormatStyle::AlignConsecutiveStyle::ACS_Consecutive;
18578   Style.AlignConsecutiveDeclarations =
18579       FormatStyle::AlignConsecutiveStyle::ACS_Consecutive;
18580   verifyFormat("struct test demo[] = {\n"
18581                "    {56,    23, \"hello\"},\n"
18582                "    {-1, 93463, \"world\"},\n"
18583                "    { 7,     5,    \"!!\"}\n"
18584                "};\n",
18585                Style);
18586 
18587   verifyFormat("struct test demo[] = {\n"
18588                "    {56,    23, \"hello\"}, // first line\n"
18589                "    {-1, 93463, \"world\"}, // second line\n"
18590                "    { 7,     5,    \"!!\"}  // third line\n"
18591                "};\n",
18592                Style);
18593 
18594   verifyFormat("struct test demo[4] = {\n"
18595                "    { 56,    23, 21,       \"oh\"}, // first line\n"
18596                "    { -1, 93463, 22,       \"my\"}, // second line\n"
18597                "    {  7,     5,  1, \"goodness\"}  // third line\n"
18598                "    {234,     5,  1, \"gracious\"}  // fourth line\n"
18599                "};\n",
18600                Style);
18601 
18602   verifyFormat("struct test demo[3] = {\n"
18603                "    {56,    23, \"hello\"},\n"
18604                "    {-1, 93463, \"world\"},\n"
18605                "    { 7,     5,    \"!!\"}\n"
18606                "};\n",
18607                Style);
18608 
18609   verifyFormat("struct test demo[3] = {\n"
18610                "    {int{56},    23, \"hello\"},\n"
18611                "    {int{-1}, 93463, \"world\"},\n"
18612                "    { int{7},     5,    \"!!\"}\n"
18613                "};\n",
18614                Style);
18615 
18616   verifyFormat("struct test demo[] = {\n"
18617                "    {56,    23, \"hello\"},\n"
18618                "    {-1, 93463, \"world\"},\n"
18619                "    { 7,     5,    \"!!\"},\n"
18620                "};\n",
18621                Style);
18622 
18623   verifyFormat("test demo[] = {\n"
18624                "    {56,    23, \"hello\"},\n"
18625                "    {-1, 93463, \"world\"},\n"
18626                "    { 7,     5,    \"!!\"},\n"
18627                "};\n",
18628                Style);
18629 
18630   verifyFormat("demo = std::array<struct test, 3>{\n"
18631                "    test{56,    23, \"hello\"},\n"
18632                "    test{-1, 93463, \"world\"},\n"
18633                "    test{ 7,     5,    \"!!\"},\n"
18634                "};\n",
18635                Style);
18636 
18637   verifyFormat("test demo[] = {\n"
18638                "    {56,    23, \"hello\"},\n"
18639                "#if X\n"
18640                "    {-1, 93463, \"world\"},\n"
18641                "#endif\n"
18642                "    { 7,     5,    \"!!\"}\n"
18643                "};\n",
18644                Style);
18645 
18646   verifyFormat(
18647       "test demo[] = {\n"
18648       "    { 7,    23,\n"
18649       "     \"hello world i am a very long line that really, in any\"\n"
18650       "     \"just world, ought to be split over multiple lines\"},\n"
18651       "    {-1, 93463,                                  \"world\"},\n"
18652       "    {56,     5,                                     \"!!\"}\n"
18653       "};\n",
18654       Style);
18655 
18656   verifyFormat("return GradForUnaryCwise(g, {\n"
18657                "                                {{\"sign\"}, \"Sign\",  "
18658                "  {\"x\", \"dy\"}},\n"
18659                "                                {  {\"dx\"},  \"Mul\", {\"dy\""
18660                ", \"sign\"}},\n"
18661                "});\n",
18662                Style);
18663 
18664   Style.ColumnLimit = 0;
18665   EXPECT_EQ(
18666       "test demo[] = {\n"
18667       "    {56,    23, \"hello world i am a very long line that really, "
18668       "in any just world, ought to be split over multiple lines\"},\n"
18669       "    {-1, 93463,                                                  "
18670       "                                                 \"world\"},\n"
18671       "    { 7,     5,                                                  "
18672       "                                                    \"!!\"},\n"
18673       "};",
18674       format("test demo[] = {{56, 23, \"hello world i am a very long line "
18675              "that really, in any just world, ought to be split over multiple "
18676              "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
18677              Style));
18678 
18679   Style.ColumnLimit = 80;
18680   verifyFormat("test demo[] = {\n"
18681                "    {56,    23, /* a comment */ \"hello\"},\n"
18682                "    {-1, 93463,                 \"world\"},\n"
18683                "    { 7,     5,                    \"!!\"}\n"
18684                "};\n",
18685                Style);
18686 
18687   verifyFormat("test demo[] = {\n"
18688                "    {56,    23,                    \"hello\"},\n"
18689                "    {-1, 93463, \"world\" /* comment here */},\n"
18690                "    { 7,     5,                       \"!!\"}\n"
18691                "};\n",
18692                Style);
18693 
18694   verifyFormat("test demo[] = {\n"
18695                "    {56, /* a comment */ 23, \"hello\"},\n"
18696                "    {-1,              93463, \"world\"},\n"
18697                "    { 7,                  5,    \"!!\"}\n"
18698                "};\n",
18699                Style);
18700 
18701   Style.ColumnLimit = 20;
18702   EXPECT_EQ(
18703       "demo = std::array<\n"
18704       "    struct test, 3>{\n"
18705       "    test{\n"
18706       "         56,    23,\n"
18707       "         \"hello \"\n"
18708       "         \"world i \"\n"
18709       "         \"am a very \"\n"
18710       "         \"long line \"\n"
18711       "         \"that \"\n"
18712       "         \"really, \"\n"
18713       "         \"in any \"\n"
18714       "         \"just \"\n"
18715       "         \"world, \"\n"
18716       "         \"ought to \"\n"
18717       "         \"be split \"\n"
18718       "         \"over \"\n"
18719       "         \"multiple \"\n"
18720       "         \"lines\"},\n"
18721       "    test{-1, 93463,\n"
18722       "         \"world\"},\n"
18723       "    test{ 7,     5,\n"
18724       "         \"!!\"   },\n"
18725       "};",
18726       format("demo = std::array<struct test, 3>{test{56, 23, \"hello world "
18727              "i am a very long line that really, in any just world, ought "
18728              "to be split over multiple lines\"},test{-1, 93463, \"world\"},"
18729              "test{7, 5, \"!!\"},};",
18730              Style));
18731   // This caused a core dump by enabling Alignment in the LLVMStyle globally
18732   Style = getLLVMStyleWithColumns(50);
18733   Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
18734   verifyFormat("static A x = {\n"
18735                "    {{init1, init2, init3, init4},\n"
18736                "     {init1, init2, init3, init4}}\n"
18737                "};",
18738                Style);
18739   Style.ColumnLimit = 100;
18740   EXPECT_EQ(
18741       "test demo[] = {\n"
18742       "    {56,    23,\n"
18743       "     \"hello world i am a very long line that really, in any just world"
18744       ", ought to be split over \"\n"
18745       "     \"multiple lines\"  },\n"
18746       "    {-1, 93463, \"world\"},\n"
18747       "    { 7,     5,    \"!!\"},\n"
18748       "};",
18749       format("test demo[] = {{56, 23, \"hello world i am a very long line "
18750              "that really, in any just world, ought to be split over multiple "
18751              "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
18752              Style));
18753 
18754   Style = getLLVMStyleWithColumns(50);
18755   Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
18756   Style.AlignConsecutiveAssignments =
18757       FormatStyle::AlignConsecutiveStyle::ACS_Consecutive;
18758   Style.AlignConsecutiveDeclarations =
18759       FormatStyle::AlignConsecutiveStyle::ACS_Consecutive;
18760   verifyFormat("struct test demo[] = {\n"
18761                "    {56,    23, \"hello\"},\n"
18762                "    {-1, 93463, \"world\"},\n"
18763                "    { 7,     5,    \"!!\"}\n"
18764                "};\n"
18765                "static A x = {\n"
18766                "    {{init1, init2, init3, init4},\n"
18767                "     {init1, init2, init3, init4}}\n"
18768                "};",
18769                Style);
18770   Style.ColumnLimit = 100;
18771   Style.AlignConsecutiveAssignments =
18772       FormatStyle::AlignConsecutiveStyle::ACS_AcrossComments;
18773   Style.AlignConsecutiveDeclarations =
18774       FormatStyle::AlignConsecutiveStyle::ACS_AcrossComments;
18775   verifyFormat("struct test demo[] = {\n"
18776                "    {56,    23, \"hello\"},\n"
18777                "    {-1, 93463, \"world\"},\n"
18778                "    { 7,     5,    \"!!\"}\n"
18779                "};\n"
18780                "struct test demo[4] = {\n"
18781                "    { 56,    23, 21,       \"oh\"}, // first line\n"
18782                "    { -1, 93463, 22,       \"my\"}, // second line\n"
18783                "    {  7,     5,  1, \"goodness\"}  // third line\n"
18784                "    {234,     5,  1, \"gracious\"}  // fourth line\n"
18785                "};\n",
18786                Style);
18787   EXPECT_EQ(
18788       "test demo[] = {\n"
18789       "    {56,\n"
18790       "     \"hello world i am a very long line that really, in any just world"
18791       ", ought to be split over \"\n"
18792       "     \"multiple lines\",    23},\n"
18793       "    {-1,      \"world\", 93463},\n"
18794       "    { 7,         \"!!\",     5},\n"
18795       "};",
18796       format("test demo[] = {{56, \"hello world i am a very long line "
18797              "that really, in any just world, ought to be split over multiple "
18798              "lines\", 23},{-1, \"world\", 93463},{7, \"!!\", 5},};",
18799              Style));
18800 }
18801 
18802 TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) {
18803   auto Style = getLLVMStyle();
18804   Style.AlignArrayOfStructures = FormatStyle::AIAS_Left;
18805   /* FIXME: This case gets misformatted.
18806   verifyFormat("auto foo = Items{\n"
18807                "    Section{0, bar(), },\n"
18808                "    Section{1, boo()  }\n"
18809                "};\n",
18810                Style);
18811   */
18812   verifyFormat("auto foo = Items{\n"
18813                "    Section{\n"
18814                "            0, bar(),\n"
18815                "            }\n"
18816                "};\n",
18817                Style);
18818   verifyFormat("struct test demo[] = {\n"
18819                "    {56, 23,    \"hello\"},\n"
18820                "    {-1, 93463, \"world\"},\n"
18821                "    {7,  5,     \"!!\"   }\n"
18822                "};\n",
18823                Style);
18824   verifyFormat("struct test demo[] = {\n"
18825                "    {56, 23,    \"hello\"}, // first line\n"
18826                "    {-1, 93463, \"world\"}, // second line\n"
18827                "    {7,  5,     \"!!\"   }  // third line\n"
18828                "};\n",
18829                Style);
18830   verifyFormat("struct test demo[4] = {\n"
18831                "    {56,  23,    21, \"oh\"      }, // first line\n"
18832                "    {-1,  93463, 22, \"my\"      }, // second line\n"
18833                "    {7,   5,     1,  \"goodness\"}  // third line\n"
18834                "    {234, 5,     1,  \"gracious\"}  // fourth line\n"
18835                "};\n",
18836                Style);
18837   verifyFormat("struct test demo[3] = {\n"
18838                "    {56, 23,    \"hello\"},\n"
18839                "    {-1, 93463, \"world\"},\n"
18840                "    {7,  5,     \"!!\"   }\n"
18841                "};\n",
18842                Style);
18843 
18844   verifyFormat("struct test demo[3] = {\n"
18845                "    {int{56}, 23,    \"hello\"},\n"
18846                "    {int{-1}, 93463, \"world\"},\n"
18847                "    {int{7},  5,     \"!!\"   }\n"
18848                "};\n",
18849                Style);
18850   verifyFormat("struct test demo[] = {\n"
18851                "    {56, 23,    \"hello\"},\n"
18852                "    {-1, 93463, \"world\"},\n"
18853                "    {7,  5,     \"!!\"   },\n"
18854                "};\n",
18855                Style);
18856   verifyFormat("test demo[] = {\n"
18857                "    {56, 23,    \"hello\"},\n"
18858                "    {-1, 93463, \"world\"},\n"
18859                "    {7,  5,     \"!!\"   },\n"
18860                "};\n",
18861                Style);
18862   verifyFormat("demo = std::array<struct test, 3>{\n"
18863                "    test{56, 23,    \"hello\"},\n"
18864                "    test{-1, 93463, \"world\"},\n"
18865                "    test{7,  5,     \"!!\"   },\n"
18866                "};\n",
18867                Style);
18868   verifyFormat("test demo[] = {\n"
18869                "    {56, 23,    \"hello\"},\n"
18870                "#if X\n"
18871                "    {-1, 93463, \"world\"},\n"
18872                "#endif\n"
18873                "    {7,  5,     \"!!\"   }\n"
18874                "};\n",
18875                Style);
18876   verifyFormat(
18877       "test demo[] = {\n"
18878       "    {7,  23,\n"
18879       "     \"hello world i am a very long line that really, in any\"\n"
18880       "     \"just world, ought to be split over multiple lines\"},\n"
18881       "    {-1, 93463, \"world\"                                 },\n"
18882       "    {56, 5,     \"!!\"                                    }\n"
18883       "};\n",
18884       Style);
18885 
18886   verifyFormat("return GradForUnaryCwise(g, {\n"
18887                "                                {{\"sign\"}, \"Sign\", {\"x\", "
18888                "\"dy\"}   },\n"
18889                "                                {{\"dx\"},   \"Mul\",  "
18890                "{\"dy\", \"sign\"}},\n"
18891                "});\n",
18892                Style);
18893 
18894   Style.ColumnLimit = 0;
18895   EXPECT_EQ(
18896       "test demo[] = {\n"
18897       "    {56, 23,    \"hello world i am a very long line that really, in any "
18898       "just world, ought to be split over multiple lines\"},\n"
18899       "    {-1, 93463, \"world\"                                               "
18900       "                                                   },\n"
18901       "    {7,  5,     \"!!\"                                                  "
18902       "                                                   },\n"
18903       "};",
18904       format("test demo[] = {{56, 23, \"hello world i am a very long line "
18905              "that really, in any just world, ought to be split over multiple "
18906              "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
18907              Style));
18908 
18909   Style.ColumnLimit = 80;
18910   verifyFormat("test demo[] = {\n"
18911                "    {56, 23,    /* a comment */ \"hello\"},\n"
18912                "    {-1, 93463, \"world\"                },\n"
18913                "    {7,  5,     \"!!\"                   }\n"
18914                "};\n",
18915                Style);
18916 
18917   verifyFormat("test demo[] = {\n"
18918                "    {56, 23,    \"hello\"                   },\n"
18919                "    {-1, 93463, \"world\" /* comment here */},\n"
18920                "    {7,  5,     \"!!\"                      }\n"
18921                "};\n",
18922                Style);
18923 
18924   verifyFormat("test demo[] = {\n"
18925                "    {56, /* a comment */ 23, \"hello\"},\n"
18926                "    {-1, 93463,              \"world\"},\n"
18927                "    {7,  5,                  \"!!\"   }\n"
18928                "};\n",
18929                Style);
18930 
18931   Style.ColumnLimit = 20;
18932   EXPECT_EQ(
18933       "demo = std::array<\n"
18934       "    struct test, 3>{\n"
18935       "    test{\n"
18936       "         56, 23,\n"
18937       "         \"hello \"\n"
18938       "         \"world i \"\n"
18939       "         \"am a very \"\n"
18940       "         \"long line \"\n"
18941       "         \"that \"\n"
18942       "         \"really, \"\n"
18943       "         \"in any \"\n"
18944       "         \"just \"\n"
18945       "         \"world, \"\n"
18946       "         \"ought to \"\n"
18947       "         \"be split \"\n"
18948       "         \"over \"\n"
18949       "         \"multiple \"\n"
18950       "         \"lines\"},\n"
18951       "    test{-1, 93463,\n"
18952       "         \"world\"},\n"
18953       "    test{7,  5,\n"
18954       "         \"!!\"   },\n"
18955       "};",
18956       format("demo = std::array<struct test, 3>{test{56, 23, \"hello world "
18957              "i am a very long line that really, in any just world, ought "
18958              "to be split over multiple lines\"},test{-1, 93463, \"world\"},"
18959              "test{7, 5, \"!!\"},};",
18960              Style));
18961 
18962   // This caused a core dump by enabling Alignment in the LLVMStyle globally
18963   Style = getLLVMStyleWithColumns(50);
18964   Style.AlignArrayOfStructures = FormatStyle::AIAS_Left;
18965   verifyFormat("static A x = {\n"
18966                "    {{init1, init2, init3, init4},\n"
18967                "     {init1, init2, init3, init4}}\n"
18968                "};",
18969                Style);
18970   Style.ColumnLimit = 100;
18971   EXPECT_EQ(
18972       "test demo[] = {\n"
18973       "    {56, 23,\n"
18974       "     \"hello world i am a very long line that really, in any just world"
18975       ", ought to be split over \"\n"
18976       "     \"multiple lines\"  },\n"
18977       "    {-1, 93463, \"world\"},\n"
18978       "    {7,  5,     \"!!\"   },\n"
18979       "};",
18980       format("test demo[] = {{56, 23, \"hello world i am a very long line "
18981              "that really, in any just world, ought to be split over multiple "
18982              "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
18983              Style));
18984 }
18985 
18986 TEST_F(FormatTest, UnderstandsPragmas) {
18987   verifyFormat("#pragma omp reduction(| : var)");
18988   verifyFormat("#pragma omp reduction(+ : var)");
18989 
18990   EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string "
18991             "(including parentheses).",
18992             format("#pragma    mark   Any non-hyphenated or hyphenated string "
18993                    "(including parentheses)."));
18994 }
18995 
18996 TEST_F(FormatTest, UnderstandPragmaOption) {
18997   verifyFormat("#pragma option -C -A");
18998 
18999   EXPECT_EQ("#pragma option -C -A", format("#pragma    option   -C   -A"));
19000 }
19001 
19002 TEST_F(FormatTest, OptimizeBreakPenaltyVsExcess) {
19003   FormatStyle Style = getLLVMStyleWithColumns(20);
19004 
19005   // See PR41213
19006   EXPECT_EQ("/*\n"
19007             " *\t9012345\n"
19008             " * /8901\n"
19009             " */",
19010             format("/*\n"
19011                    " *\t9012345 /8901\n"
19012                    " */",
19013                    Style));
19014   EXPECT_EQ("/*\n"
19015             " *345678\n"
19016             " *\t/8901\n"
19017             " */",
19018             format("/*\n"
19019                    " *345678\t/8901\n"
19020                    " */",
19021                    Style));
19022 
19023   verifyFormat("int a; // the\n"
19024                "       // comment",
19025                Style);
19026   EXPECT_EQ("int a; /* first line\n"
19027             "        * second\n"
19028             "        * line third\n"
19029             "        * line\n"
19030             "        */",
19031             format("int a; /* first line\n"
19032                    "        * second\n"
19033                    "        * line third\n"
19034                    "        * line\n"
19035                    "        */",
19036                    Style));
19037   EXPECT_EQ("int a; // first line\n"
19038             "       // second\n"
19039             "       // line third\n"
19040             "       // line",
19041             format("int a; // first line\n"
19042                    "       // second line\n"
19043                    "       // third line",
19044                    Style));
19045 
19046   Style.PenaltyExcessCharacter = 90;
19047   verifyFormat("int a; // the comment", Style);
19048   EXPECT_EQ("int a; // the comment\n"
19049             "       // aaa",
19050             format("int a; // the comment aaa", Style));
19051   EXPECT_EQ("int a; /* first line\n"
19052             "        * second line\n"
19053             "        * third line\n"
19054             "        */",
19055             format("int a; /* first line\n"
19056                    "        * second line\n"
19057                    "        * third line\n"
19058                    "        */",
19059                    Style));
19060   EXPECT_EQ("int a; // first line\n"
19061             "       // second line\n"
19062             "       // third line",
19063             format("int a; // first line\n"
19064                    "       // second line\n"
19065                    "       // third line",
19066                    Style));
19067   // FIXME: Investigate why this is not getting the same layout as the test
19068   // above.
19069   EXPECT_EQ("int a; /* first line\n"
19070             "        * second line\n"
19071             "        * third line\n"
19072             "        */",
19073             format("int a; /* first line second line third line"
19074                    "\n*/",
19075                    Style));
19076 
19077   EXPECT_EQ("// foo bar baz bazfoo\n"
19078             "// foo bar foo bar\n",
19079             format("// foo bar baz bazfoo\n"
19080                    "// foo bar foo           bar\n",
19081                    Style));
19082   EXPECT_EQ("// foo bar baz bazfoo\n"
19083             "// foo bar foo bar\n",
19084             format("// foo bar baz      bazfoo\n"
19085                    "// foo            bar foo bar\n",
19086                    Style));
19087 
19088   // FIXME: Optimally, we'd keep bazfoo on the first line and reflow bar to the
19089   // next one.
19090   EXPECT_EQ("// foo bar baz bazfoo\n"
19091             "// bar foo bar\n",
19092             format("// foo bar baz      bazfoo bar\n"
19093                    "// foo            bar\n",
19094                    Style));
19095 
19096   EXPECT_EQ("// foo bar baz bazfoo\n"
19097             "// foo bar baz bazfoo\n"
19098             "// bar foo bar\n",
19099             format("// foo bar baz      bazfoo\n"
19100                    "// foo bar baz      bazfoo bar\n"
19101                    "// foo bar\n",
19102                    Style));
19103 
19104   EXPECT_EQ("// foo bar baz bazfoo\n"
19105             "// foo bar baz bazfoo\n"
19106             "// bar foo bar\n",
19107             format("// foo bar baz      bazfoo\n"
19108                    "// foo bar baz      bazfoo bar\n"
19109                    "// foo           bar\n",
19110                    Style));
19111 
19112   // Make sure we do not keep protruding characters if strict mode reflow is
19113   // cheaper than keeping protruding characters.
19114   Style.ColumnLimit = 21;
19115   EXPECT_EQ(
19116       "// foo foo foo foo\n"
19117       "// foo foo foo foo\n"
19118       "// foo foo foo foo\n",
19119       format("// foo foo foo foo foo foo foo foo foo foo foo foo\n", Style));
19120 
19121   EXPECT_EQ("int a = /* long block\n"
19122             "           comment */\n"
19123             "    42;",
19124             format("int a = /* long block comment */ 42;", Style));
19125 }
19126 
19127 TEST_F(FormatTest, BreakPenaltyAfterLParen) {
19128   FormatStyle Style = getLLVMStyle();
19129   Style.ColumnLimit = 8;
19130   Style.PenaltyExcessCharacter = 15;
19131   verifyFormat("int foo(\n"
19132                "    int aaaaaaaaaaaaaaaaaaaaaaaa);",
19133                Style);
19134   Style.PenaltyBreakOpenParenthesis = 200;
19135   EXPECT_EQ("int foo(int aaaaaaaaaaaaaaaaaaaaaaaa);",
19136             format("int foo(\n"
19137                    "    int aaaaaaaaaaaaaaaaaaaaaaaa);",
19138                    Style));
19139 }
19140 
19141 TEST_F(FormatTest, BreakPenaltyAfterCastLParen) {
19142   FormatStyle Style = getLLVMStyle();
19143   Style.ColumnLimit = 5;
19144   Style.PenaltyExcessCharacter = 150;
19145   verifyFormat("foo((\n"
19146                "    int)aaaaaaaaaaaaaaaaaaaaaaaa);",
19147 
19148                Style);
19149   Style.PenaltyBreakOpenParenthesis = 100000;
19150   EXPECT_EQ("foo((int)\n"
19151             "        aaaaaaaaaaaaaaaaaaaaaaaa);",
19152             format("foo((\n"
19153                    "int)aaaaaaaaaaaaaaaaaaaaaaaa);",
19154                    Style));
19155 }
19156 
19157 TEST_F(FormatTest, BreakPenaltyAfterForLoopLParen) {
19158   FormatStyle Style = getLLVMStyle();
19159   Style.ColumnLimit = 4;
19160   Style.PenaltyExcessCharacter = 100;
19161   verifyFormat("for (\n"
19162                "    int iiiiiiiiiiiiiiiii =\n"
19163                "        0;\n"
19164                "    iiiiiiiiiiiiiiiii <\n"
19165                "    2;\n"
19166                "    iiiiiiiiiiiiiiiii++) {\n"
19167                "}",
19168 
19169                Style);
19170   Style.PenaltyBreakOpenParenthesis = 1250;
19171   EXPECT_EQ("for (int iiiiiiiiiiiiiiiii =\n"
19172             "         0;\n"
19173             "     iiiiiiiiiiiiiiiii <\n"
19174             "     2;\n"
19175             "     iiiiiiiiiiiiiiiii++) {\n"
19176             "}",
19177             format("for (\n"
19178                    "    int iiiiiiiiiiiiiiiii =\n"
19179                    "        0;\n"
19180                    "    iiiiiiiiiiiiiiiii <\n"
19181                    "    2;\n"
19182                    "    iiiiiiiiiiiiiiiii++) {\n"
19183                    "}",
19184                    Style));
19185 }
19186 
19187 #define EXPECT_ALL_STYLES_EQUAL(Styles)                                        \
19188   for (size_t i = 1; i < Styles.size(); ++i)                                   \
19189   EXPECT_EQ(Styles[0], Styles[i])                                              \
19190       << "Style #" << i << " of " << Styles.size() << " differs from Style #0"
19191 
19192 TEST_F(FormatTest, GetsPredefinedStyleByName) {
19193   SmallVector<FormatStyle, 3> Styles;
19194   Styles.resize(3);
19195 
19196   Styles[0] = getLLVMStyle();
19197   EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1]));
19198   EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2]));
19199   EXPECT_ALL_STYLES_EQUAL(Styles);
19200 
19201   Styles[0] = getGoogleStyle();
19202   EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1]));
19203   EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2]));
19204   EXPECT_ALL_STYLES_EQUAL(Styles);
19205 
19206   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
19207   EXPECT_TRUE(
19208       getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1]));
19209   EXPECT_TRUE(
19210       getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2]));
19211   EXPECT_ALL_STYLES_EQUAL(Styles);
19212 
19213   Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp);
19214   EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1]));
19215   EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2]));
19216   EXPECT_ALL_STYLES_EQUAL(Styles);
19217 
19218   Styles[0] = getMozillaStyle();
19219   EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1]));
19220   EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2]));
19221   EXPECT_ALL_STYLES_EQUAL(Styles);
19222 
19223   Styles[0] = getWebKitStyle();
19224   EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1]));
19225   EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2]));
19226   EXPECT_ALL_STYLES_EQUAL(Styles);
19227 
19228   Styles[0] = getGNUStyle();
19229   EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1]));
19230   EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2]));
19231   EXPECT_ALL_STYLES_EQUAL(Styles);
19232 
19233   EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0]));
19234 }
19235 
19236 TEST_F(FormatTest, GetsCorrectBasedOnStyle) {
19237   SmallVector<FormatStyle, 8> Styles;
19238   Styles.resize(2);
19239 
19240   Styles[0] = getGoogleStyle();
19241   Styles[1] = getLLVMStyle();
19242   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
19243   EXPECT_ALL_STYLES_EQUAL(Styles);
19244 
19245   Styles.resize(5);
19246   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
19247   Styles[1] = getLLVMStyle();
19248   Styles[1].Language = FormatStyle::LK_JavaScript;
19249   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
19250 
19251   Styles[2] = getLLVMStyle();
19252   Styles[2].Language = FormatStyle::LK_JavaScript;
19253   EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n"
19254                                   "BasedOnStyle: Google",
19255                                   &Styles[2])
19256                    .value());
19257 
19258   Styles[3] = getLLVMStyle();
19259   Styles[3].Language = FormatStyle::LK_JavaScript;
19260   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n"
19261                                   "Language: JavaScript",
19262                                   &Styles[3])
19263                    .value());
19264 
19265   Styles[4] = getLLVMStyle();
19266   Styles[4].Language = FormatStyle::LK_JavaScript;
19267   EXPECT_EQ(0, parseConfiguration("---\n"
19268                                   "BasedOnStyle: LLVM\n"
19269                                   "IndentWidth: 123\n"
19270                                   "---\n"
19271                                   "BasedOnStyle: Google\n"
19272                                   "Language: JavaScript",
19273                                   &Styles[4])
19274                    .value());
19275   EXPECT_ALL_STYLES_EQUAL(Styles);
19276 }
19277 
19278 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME)                             \
19279   Style.FIELD = false;                                                         \
19280   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value());      \
19281   EXPECT_TRUE(Style.FIELD);                                                    \
19282   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value());     \
19283   EXPECT_FALSE(Style.FIELD);
19284 
19285 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD)
19286 
19287 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME)              \
19288   Style.STRUCT.FIELD = false;                                                  \
19289   EXPECT_EQ(0,                                                                 \
19290             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": true", &Style)   \
19291                 .value());                                                     \
19292   EXPECT_TRUE(Style.STRUCT.FIELD);                                             \
19293   EXPECT_EQ(0,                                                                 \
19294             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": false", &Style)  \
19295                 .value());                                                     \
19296   EXPECT_FALSE(Style.STRUCT.FIELD);
19297 
19298 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD)                                 \
19299   CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD)
19300 
19301 #define CHECK_PARSE(TEXT, FIELD, VALUE)                                        \
19302   EXPECT_NE(VALUE, Style.FIELD) << "Initial value already the same!";          \
19303   EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value());                      \
19304   EXPECT_EQ(VALUE, Style.FIELD) << "Unexpected value after parsing!"
19305 
19306 TEST_F(FormatTest, ParsesConfigurationBools) {
19307   FormatStyle Style = {};
19308   Style.Language = FormatStyle::LK_Cpp;
19309   CHECK_PARSE_BOOL(AlignTrailingComments);
19310   CHECK_PARSE_BOOL(AllowAllArgumentsOnNextLine);
19311   CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
19312   CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine);
19313   CHECK_PARSE_BOOL(AllowShortEnumsOnASingleLine);
19314   CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
19315   CHECK_PARSE_BOOL(BinPackArguments);
19316   CHECK_PARSE_BOOL(BinPackParameters);
19317   CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations);
19318   CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
19319   CHECK_PARSE_BOOL(BreakStringLiterals);
19320   CHECK_PARSE_BOOL(CompactNamespaces);
19321   CHECK_PARSE_BOOL(DeriveLineEnding);
19322   CHECK_PARSE_BOOL(DerivePointerAlignment);
19323   CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding");
19324   CHECK_PARSE_BOOL(DisableFormat);
19325   CHECK_PARSE_BOOL(IndentAccessModifiers);
19326   CHECK_PARSE_BOOL(IndentCaseLabels);
19327   CHECK_PARSE_BOOL(IndentCaseBlocks);
19328   CHECK_PARSE_BOOL(IndentGotoLabels);
19329   CHECK_PARSE_BOOL_FIELD(IndentRequiresClause, "IndentRequires");
19330   CHECK_PARSE_BOOL(IndentRequiresClause);
19331   CHECK_PARSE_BOOL(IndentWrappedFunctionNames);
19332   CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks);
19333   CHECK_PARSE_BOOL(ObjCSpaceAfterProperty);
19334   CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
19335   CHECK_PARSE_BOOL(Cpp11BracedListStyle);
19336   CHECK_PARSE_BOOL(ReflowComments);
19337   CHECK_PARSE_BOOL(RemoveBracesLLVM);
19338   CHECK_PARSE_BOOL(SortUsingDeclarations);
19339   CHECK_PARSE_BOOL(SpacesInParentheses);
19340   CHECK_PARSE_BOOL(SpacesInSquareBrackets);
19341   CHECK_PARSE_BOOL(SpacesInConditionalStatement);
19342   CHECK_PARSE_BOOL(SpaceInEmptyBlock);
19343   CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
19344   CHECK_PARSE_BOOL(SpacesInContainerLiterals);
19345   CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
19346   CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
19347   CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword);
19348   CHECK_PARSE_BOOL(SpaceAfterLogicalNot);
19349   CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
19350   CHECK_PARSE_BOOL(SpaceBeforeCaseColon);
19351   CHECK_PARSE_BOOL(SpaceBeforeCpp11BracedList);
19352   CHECK_PARSE_BOOL(SpaceBeforeCtorInitializerColon);
19353   CHECK_PARSE_BOOL(SpaceBeforeInheritanceColon);
19354   CHECK_PARSE_BOOL(SpaceBeforeRangeBasedForLoopColon);
19355   CHECK_PARSE_BOOL(SpaceBeforeSquareBrackets);
19356   CHECK_PARSE_BOOL(UseCRLF);
19357 
19358   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterCaseLabel);
19359   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass);
19360   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum);
19361   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction);
19362   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace);
19363   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration);
19364   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct);
19365   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion);
19366   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterExternBlock);
19367   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch);
19368   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse);
19369   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeLambdaBody);
19370   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeWhile);
19371   CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces);
19372   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyFunction);
19373   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyRecord);
19374   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyNamespace);
19375   CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterControlStatements);
19376   CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterForeachMacros);
19377   CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions,
19378                           AfterFunctionDeclarationName);
19379   CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions,
19380                           AfterFunctionDefinitionName);
19381   CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterIfMacros);
19382   CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterOverloadedOperator);
19383   CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, BeforeNonEmptyParentheses);
19384 }
19385 
19386 #undef CHECK_PARSE_BOOL
19387 
19388 TEST_F(FormatTest, ParsesConfiguration) {
19389   FormatStyle Style = {};
19390   Style.Language = FormatStyle::LK_Cpp;
19391   CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
19392   CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",
19393               ConstructorInitializerIndentWidth, 1234u);
19394   CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u);
19395   CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
19396   CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
19397   CHECK_PARSE("PenaltyBreakAssignment: 1234", PenaltyBreakAssignment, 1234u);
19398   CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234",
19399               PenaltyBreakBeforeFirstCallParameter, 1234u);
19400   CHECK_PARSE("PenaltyBreakTemplateDeclaration: 1234",
19401               PenaltyBreakTemplateDeclaration, 1234u);
19402   CHECK_PARSE("PenaltyBreakOpenParenthesis: 1234", PenaltyBreakOpenParenthesis,
19403               1234u);
19404   CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
19405   CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
19406               PenaltyReturnTypeOnItsOwnLine, 1234u);
19407   CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
19408               SpacesBeforeTrailingComments, 1234u);
19409   CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
19410   CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u);
19411   CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$");
19412 
19413   Style.QualifierAlignment = FormatStyle::QAS_Right;
19414   CHECK_PARSE("QualifierAlignment: Leave", QualifierAlignment,
19415               FormatStyle::QAS_Leave);
19416   CHECK_PARSE("QualifierAlignment: Right", QualifierAlignment,
19417               FormatStyle::QAS_Right);
19418   CHECK_PARSE("QualifierAlignment: Left", QualifierAlignment,
19419               FormatStyle::QAS_Left);
19420   CHECK_PARSE("QualifierAlignment: Custom", QualifierAlignment,
19421               FormatStyle::QAS_Custom);
19422 
19423   Style.QualifierOrder.clear();
19424   CHECK_PARSE("QualifierOrder: [ const, volatile, type ]", QualifierOrder,
19425               std::vector<std::string>({"const", "volatile", "type"}));
19426   Style.QualifierOrder.clear();
19427   CHECK_PARSE("QualifierOrder: [const, type]", QualifierOrder,
19428               std::vector<std::string>({"const", "type"}));
19429   Style.QualifierOrder.clear();
19430   CHECK_PARSE("QualifierOrder: [volatile, type]", QualifierOrder,
19431               std::vector<std::string>({"volatile", "type"}));
19432 
19433   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
19434   CHECK_PARSE("AlignConsecutiveAssignments: None", AlignConsecutiveAssignments,
19435               FormatStyle::ACS_None);
19436   CHECK_PARSE("AlignConsecutiveAssignments: Consecutive",
19437               AlignConsecutiveAssignments, FormatStyle::ACS_Consecutive);
19438   CHECK_PARSE("AlignConsecutiveAssignments: AcrossEmptyLines",
19439               AlignConsecutiveAssignments, FormatStyle::ACS_AcrossEmptyLines);
19440   CHECK_PARSE("AlignConsecutiveAssignments: AcrossEmptyLinesAndComments",
19441               AlignConsecutiveAssignments,
19442               FormatStyle::ACS_AcrossEmptyLinesAndComments);
19443   // For backwards compability, false / true should still parse
19444   CHECK_PARSE("AlignConsecutiveAssignments: false", AlignConsecutiveAssignments,
19445               FormatStyle::ACS_None);
19446   CHECK_PARSE("AlignConsecutiveAssignments: true", AlignConsecutiveAssignments,
19447               FormatStyle::ACS_Consecutive);
19448 
19449   Style.AlignConsecutiveBitFields = FormatStyle::ACS_Consecutive;
19450   CHECK_PARSE("AlignConsecutiveBitFields: None", AlignConsecutiveBitFields,
19451               FormatStyle::ACS_None);
19452   CHECK_PARSE("AlignConsecutiveBitFields: Consecutive",
19453               AlignConsecutiveBitFields, FormatStyle::ACS_Consecutive);
19454   CHECK_PARSE("AlignConsecutiveBitFields: AcrossEmptyLines",
19455               AlignConsecutiveBitFields, FormatStyle::ACS_AcrossEmptyLines);
19456   CHECK_PARSE("AlignConsecutiveBitFields: AcrossEmptyLinesAndComments",
19457               AlignConsecutiveBitFields,
19458               FormatStyle::ACS_AcrossEmptyLinesAndComments);
19459   // For backwards compability, false / true should still parse
19460   CHECK_PARSE("AlignConsecutiveBitFields: false", AlignConsecutiveBitFields,
19461               FormatStyle::ACS_None);
19462   CHECK_PARSE("AlignConsecutiveBitFields: true", AlignConsecutiveBitFields,
19463               FormatStyle::ACS_Consecutive);
19464 
19465   Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
19466   CHECK_PARSE("AlignConsecutiveMacros: None", AlignConsecutiveMacros,
19467               FormatStyle::ACS_None);
19468   CHECK_PARSE("AlignConsecutiveMacros: Consecutive", AlignConsecutiveMacros,
19469               FormatStyle::ACS_Consecutive);
19470   CHECK_PARSE("AlignConsecutiveMacros: AcrossEmptyLines",
19471               AlignConsecutiveMacros, FormatStyle::ACS_AcrossEmptyLines);
19472   CHECK_PARSE("AlignConsecutiveMacros: AcrossEmptyLinesAndComments",
19473               AlignConsecutiveMacros,
19474               FormatStyle::ACS_AcrossEmptyLinesAndComments);
19475   // For backwards compability, false / true should still parse
19476   CHECK_PARSE("AlignConsecutiveMacros: false", AlignConsecutiveMacros,
19477               FormatStyle::ACS_None);
19478   CHECK_PARSE("AlignConsecutiveMacros: true", AlignConsecutiveMacros,
19479               FormatStyle::ACS_Consecutive);
19480 
19481   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
19482   CHECK_PARSE("AlignConsecutiveDeclarations: None",
19483               AlignConsecutiveDeclarations, FormatStyle::ACS_None);
19484   CHECK_PARSE("AlignConsecutiveDeclarations: Consecutive",
19485               AlignConsecutiveDeclarations, FormatStyle::ACS_Consecutive);
19486   CHECK_PARSE("AlignConsecutiveDeclarations: AcrossEmptyLines",
19487               AlignConsecutiveDeclarations, FormatStyle::ACS_AcrossEmptyLines);
19488   CHECK_PARSE("AlignConsecutiveDeclarations: AcrossEmptyLinesAndComments",
19489               AlignConsecutiveDeclarations,
19490               FormatStyle::ACS_AcrossEmptyLinesAndComments);
19491   // For backwards compability, false / true should still parse
19492   CHECK_PARSE("AlignConsecutiveDeclarations: false",
19493               AlignConsecutiveDeclarations, FormatStyle::ACS_None);
19494   CHECK_PARSE("AlignConsecutiveDeclarations: true",
19495               AlignConsecutiveDeclarations, FormatStyle::ACS_Consecutive);
19496 
19497   Style.PointerAlignment = FormatStyle::PAS_Middle;
19498   CHECK_PARSE("PointerAlignment: Left", PointerAlignment,
19499               FormatStyle::PAS_Left);
19500   CHECK_PARSE("PointerAlignment: Right", PointerAlignment,
19501               FormatStyle::PAS_Right);
19502   CHECK_PARSE("PointerAlignment: Middle", PointerAlignment,
19503               FormatStyle::PAS_Middle);
19504   Style.ReferenceAlignment = FormatStyle::RAS_Middle;
19505   CHECK_PARSE("ReferenceAlignment: Pointer", ReferenceAlignment,
19506               FormatStyle::RAS_Pointer);
19507   CHECK_PARSE("ReferenceAlignment: Left", ReferenceAlignment,
19508               FormatStyle::RAS_Left);
19509   CHECK_PARSE("ReferenceAlignment: Right", ReferenceAlignment,
19510               FormatStyle::RAS_Right);
19511   CHECK_PARSE("ReferenceAlignment: Middle", ReferenceAlignment,
19512               FormatStyle::RAS_Middle);
19513   // For backward compatibility:
19514   CHECK_PARSE("PointerBindsToType: Left", PointerAlignment,
19515               FormatStyle::PAS_Left);
19516   CHECK_PARSE("PointerBindsToType: Right", PointerAlignment,
19517               FormatStyle::PAS_Right);
19518   CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment,
19519               FormatStyle::PAS_Middle);
19520 
19521   Style.Standard = FormatStyle::LS_Auto;
19522   CHECK_PARSE("Standard: c++03", Standard, FormatStyle::LS_Cpp03);
19523   CHECK_PARSE("Standard: c++11", Standard, FormatStyle::LS_Cpp11);
19524   CHECK_PARSE("Standard: c++14", Standard, FormatStyle::LS_Cpp14);
19525   CHECK_PARSE("Standard: c++17", Standard, FormatStyle::LS_Cpp17);
19526   CHECK_PARSE("Standard: c++20", Standard, FormatStyle::LS_Cpp20);
19527   CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
19528   CHECK_PARSE("Standard: Latest", Standard, FormatStyle::LS_Latest);
19529   // Legacy aliases:
19530   CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03);
19531   CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Latest);
19532   CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
19533   CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
19534 
19535   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
19536   CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment",
19537               BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment);
19538   CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators,
19539               FormatStyle::BOS_None);
19540   CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators,
19541               FormatStyle::BOS_All);
19542   // For backward compatibility:
19543   CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators,
19544               FormatStyle::BOS_None);
19545   CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators,
19546               FormatStyle::BOS_All);
19547 
19548   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
19549   CHECK_PARSE("BreakConstructorInitializers: BeforeComma",
19550               BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma);
19551   CHECK_PARSE("BreakConstructorInitializers: AfterColon",
19552               BreakConstructorInitializers, FormatStyle::BCIS_AfterColon);
19553   CHECK_PARSE("BreakConstructorInitializers: BeforeColon",
19554               BreakConstructorInitializers, FormatStyle::BCIS_BeforeColon);
19555   // For backward compatibility:
19556   CHECK_PARSE("BreakConstructorInitializersBeforeComma: true",
19557               BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma);
19558 
19559   Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
19560   CHECK_PARSE("BreakInheritanceList: AfterComma", BreakInheritanceList,
19561               FormatStyle::BILS_AfterComma);
19562   CHECK_PARSE("BreakInheritanceList: BeforeComma", BreakInheritanceList,
19563               FormatStyle::BILS_BeforeComma);
19564   CHECK_PARSE("BreakInheritanceList: AfterColon", BreakInheritanceList,
19565               FormatStyle::BILS_AfterColon);
19566   CHECK_PARSE("BreakInheritanceList: BeforeColon", BreakInheritanceList,
19567               FormatStyle::BILS_BeforeColon);
19568   // For backward compatibility:
19569   CHECK_PARSE("BreakBeforeInheritanceComma: true", BreakInheritanceList,
19570               FormatStyle::BILS_BeforeComma);
19571 
19572   Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack;
19573   CHECK_PARSE("PackConstructorInitializers: Never", PackConstructorInitializers,
19574               FormatStyle::PCIS_Never);
19575   CHECK_PARSE("PackConstructorInitializers: BinPack",
19576               PackConstructorInitializers, FormatStyle::PCIS_BinPack);
19577   CHECK_PARSE("PackConstructorInitializers: CurrentLine",
19578               PackConstructorInitializers, FormatStyle::PCIS_CurrentLine);
19579   CHECK_PARSE("PackConstructorInitializers: NextLine",
19580               PackConstructorInitializers, FormatStyle::PCIS_NextLine);
19581   // For backward compatibility:
19582   CHECK_PARSE("BasedOnStyle: Google\n"
19583               "ConstructorInitializerAllOnOneLineOrOnePerLine: true\n"
19584               "AllowAllConstructorInitializersOnNextLine: false",
19585               PackConstructorInitializers, FormatStyle::PCIS_CurrentLine);
19586   Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
19587   CHECK_PARSE("BasedOnStyle: Google\n"
19588               "ConstructorInitializerAllOnOneLineOrOnePerLine: false",
19589               PackConstructorInitializers, FormatStyle::PCIS_BinPack);
19590   CHECK_PARSE("ConstructorInitializerAllOnOneLineOrOnePerLine: true\n"
19591               "AllowAllConstructorInitializersOnNextLine: true",
19592               PackConstructorInitializers, FormatStyle::PCIS_NextLine);
19593   Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack;
19594   CHECK_PARSE("ConstructorInitializerAllOnOneLineOrOnePerLine: true\n"
19595               "AllowAllConstructorInitializersOnNextLine: false",
19596               PackConstructorInitializers, FormatStyle::PCIS_CurrentLine);
19597 
19598   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
19599   CHECK_PARSE("EmptyLineBeforeAccessModifier: Never",
19600               EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Never);
19601   CHECK_PARSE("EmptyLineBeforeAccessModifier: Leave",
19602               EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Leave);
19603   CHECK_PARSE("EmptyLineBeforeAccessModifier: LogicalBlock",
19604               EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_LogicalBlock);
19605   CHECK_PARSE("EmptyLineBeforeAccessModifier: Always",
19606               EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Always);
19607 
19608   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
19609   CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket,
19610               FormatStyle::BAS_Align);
19611   CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket,
19612               FormatStyle::BAS_DontAlign);
19613   CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket,
19614               FormatStyle::BAS_AlwaysBreak);
19615   CHECK_PARSE("AlignAfterOpenBracket: BlockIndent", AlignAfterOpenBracket,
19616               FormatStyle::BAS_BlockIndent);
19617   // For backward compatibility:
19618   CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket,
19619               FormatStyle::BAS_DontAlign);
19620   CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket,
19621               FormatStyle::BAS_Align);
19622 
19623   Style.AlignEscapedNewlines = FormatStyle::ENAS_Left;
19624   CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines,
19625               FormatStyle::ENAS_DontAlign);
19626   CHECK_PARSE("AlignEscapedNewlines: Left", AlignEscapedNewlines,
19627               FormatStyle::ENAS_Left);
19628   CHECK_PARSE("AlignEscapedNewlines: Right", AlignEscapedNewlines,
19629               FormatStyle::ENAS_Right);
19630   // For backward compatibility:
19631   CHECK_PARSE("AlignEscapedNewlinesLeft: true", AlignEscapedNewlines,
19632               FormatStyle::ENAS_Left);
19633   CHECK_PARSE("AlignEscapedNewlinesLeft: false", AlignEscapedNewlines,
19634               FormatStyle::ENAS_Right);
19635 
19636   Style.AlignOperands = FormatStyle::OAS_Align;
19637   CHECK_PARSE("AlignOperands: DontAlign", AlignOperands,
19638               FormatStyle::OAS_DontAlign);
19639   CHECK_PARSE("AlignOperands: Align", AlignOperands, FormatStyle::OAS_Align);
19640   CHECK_PARSE("AlignOperands: AlignAfterOperator", AlignOperands,
19641               FormatStyle::OAS_AlignAfterOperator);
19642   // For backward compatibility:
19643   CHECK_PARSE("AlignOperands: false", AlignOperands,
19644               FormatStyle::OAS_DontAlign);
19645   CHECK_PARSE("AlignOperands: true", AlignOperands, FormatStyle::OAS_Align);
19646 
19647   Style.UseTab = FormatStyle::UT_ForIndentation;
19648   CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
19649   CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
19650   CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
19651   CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab,
19652               FormatStyle::UT_ForContinuationAndIndentation);
19653   CHECK_PARSE("UseTab: AlignWithSpaces", UseTab,
19654               FormatStyle::UT_AlignWithSpaces);
19655   // For backward compatibility:
19656   CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never);
19657   CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always);
19658 
19659   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty;
19660   CHECK_PARSE("AllowShortBlocksOnASingleLine: Never",
19661               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never);
19662   CHECK_PARSE("AllowShortBlocksOnASingleLine: Empty",
19663               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Empty);
19664   CHECK_PARSE("AllowShortBlocksOnASingleLine: Always",
19665               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always);
19666   // For backward compatibility:
19667   CHECK_PARSE("AllowShortBlocksOnASingleLine: false",
19668               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never);
19669   CHECK_PARSE("AllowShortBlocksOnASingleLine: true",
19670               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always);
19671 
19672   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
19673   CHECK_PARSE("AllowShortFunctionsOnASingleLine: None",
19674               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
19675   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline",
19676               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline);
19677   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty",
19678               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty);
19679   CHECK_PARSE("AllowShortFunctionsOnASingleLine: All",
19680               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
19681   // For backward compatibility:
19682   CHECK_PARSE("AllowShortFunctionsOnASingleLine: false",
19683               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
19684   CHECK_PARSE("AllowShortFunctionsOnASingleLine: true",
19685               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
19686 
19687   Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Both;
19688   CHECK_PARSE("SpaceAroundPointerQualifiers: Default",
19689               SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Default);
19690   CHECK_PARSE("SpaceAroundPointerQualifiers: Before",
19691               SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Before);
19692   CHECK_PARSE("SpaceAroundPointerQualifiers: After",
19693               SpaceAroundPointerQualifiers, FormatStyle::SAPQ_After);
19694   CHECK_PARSE("SpaceAroundPointerQualifiers: Both",
19695               SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Both);
19696 
19697   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
19698   CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens,
19699               FormatStyle::SBPO_Never);
19700   CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens,
19701               FormatStyle::SBPO_Always);
19702   CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens,
19703               FormatStyle::SBPO_ControlStatements);
19704   CHECK_PARSE("SpaceBeforeParens: ControlStatementsExceptControlMacros",
19705               SpaceBeforeParens,
19706               FormatStyle::SBPO_ControlStatementsExceptControlMacros);
19707   CHECK_PARSE("SpaceBeforeParens: NonEmptyParentheses", SpaceBeforeParens,
19708               FormatStyle::SBPO_NonEmptyParentheses);
19709   CHECK_PARSE("SpaceBeforeParens: Custom", SpaceBeforeParens,
19710               FormatStyle::SBPO_Custom);
19711   // For backward compatibility:
19712   CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens,
19713               FormatStyle::SBPO_Never);
19714   CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens,
19715               FormatStyle::SBPO_ControlStatements);
19716   CHECK_PARSE("SpaceBeforeParens: ControlStatementsExceptForEachMacros",
19717               SpaceBeforeParens,
19718               FormatStyle::SBPO_ControlStatementsExceptControlMacros);
19719 
19720   Style.ColumnLimit = 123;
19721   FormatStyle BaseStyle = getLLVMStyle();
19722   CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
19723   CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
19724 
19725   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
19726   CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
19727               FormatStyle::BS_Attach);
19728   CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
19729               FormatStyle::BS_Linux);
19730   CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces,
19731               FormatStyle::BS_Mozilla);
19732   CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
19733               FormatStyle::BS_Stroustrup);
19734   CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces,
19735               FormatStyle::BS_Allman);
19736   CHECK_PARSE("BreakBeforeBraces: Whitesmiths", BreakBeforeBraces,
19737               FormatStyle::BS_Whitesmiths);
19738   CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU);
19739   CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces,
19740               FormatStyle::BS_WebKit);
19741   CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces,
19742               FormatStyle::BS_Custom);
19743 
19744   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never;
19745   CHECK_PARSE("BraceWrapping:\n"
19746               "  AfterControlStatement: MultiLine",
19747               BraceWrapping.AfterControlStatement,
19748               FormatStyle::BWACS_MultiLine);
19749   CHECK_PARSE("BraceWrapping:\n"
19750               "  AfterControlStatement: Always",
19751               BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always);
19752   CHECK_PARSE("BraceWrapping:\n"
19753               "  AfterControlStatement: Never",
19754               BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never);
19755   // For backward compatibility:
19756   CHECK_PARSE("BraceWrapping:\n"
19757               "  AfterControlStatement: true",
19758               BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always);
19759   CHECK_PARSE("BraceWrapping:\n"
19760               "  AfterControlStatement: false",
19761               BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never);
19762 
19763   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
19764   CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType,
19765               FormatStyle::RTBS_None);
19766   CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType,
19767               FormatStyle::RTBS_All);
19768   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel",
19769               AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel);
19770   CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions",
19771               AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions);
19772   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions",
19773               AlwaysBreakAfterReturnType,
19774               FormatStyle::RTBS_TopLevelDefinitions);
19775 
19776   Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
19777   CHECK_PARSE("AlwaysBreakTemplateDeclarations: No",
19778               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_No);
19779   CHECK_PARSE("AlwaysBreakTemplateDeclarations: MultiLine",
19780               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine);
19781   CHECK_PARSE("AlwaysBreakTemplateDeclarations: Yes",
19782               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes);
19783   CHECK_PARSE("AlwaysBreakTemplateDeclarations: false",
19784               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine);
19785   CHECK_PARSE("AlwaysBreakTemplateDeclarations: true",
19786               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes);
19787 
19788   Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
19789   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None",
19790               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None);
19791   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All",
19792               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All);
19793   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel",
19794               AlwaysBreakAfterDefinitionReturnType,
19795               FormatStyle::DRTBS_TopLevel);
19796 
19797   Style.NamespaceIndentation = FormatStyle::NI_All;
19798   CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
19799               FormatStyle::NI_None);
19800   CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation,
19801               FormatStyle::NI_Inner);
19802   CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
19803               FormatStyle::NI_All);
19804 
19805   Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_OnlyFirstIf;
19806   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Never",
19807               AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never);
19808   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: WithoutElse",
19809               AllowShortIfStatementsOnASingleLine,
19810               FormatStyle::SIS_WithoutElse);
19811   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: OnlyFirstIf",
19812               AllowShortIfStatementsOnASingleLine,
19813               FormatStyle::SIS_OnlyFirstIf);
19814   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: AllIfsAndElse",
19815               AllowShortIfStatementsOnASingleLine,
19816               FormatStyle::SIS_AllIfsAndElse);
19817   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Always",
19818               AllowShortIfStatementsOnASingleLine,
19819               FormatStyle::SIS_OnlyFirstIf);
19820   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: false",
19821               AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never);
19822   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: true",
19823               AllowShortIfStatementsOnASingleLine,
19824               FormatStyle::SIS_WithoutElse);
19825 
19826   Style.IndentExternBlock = FormatStyle::IEBS_NoIndent;
19827   CHECK_PARSE("IndentExternBlock: AfterExternBlock", IndentExternBlock,
19828               FormatStyle::IEBS_AfterExternBlock);
19829   CHECK_PARSE("IndentExternBlock: Indent", IndentExternBlock,
19830               FormatStyle::IEBS_Indent);
19831   CHECK_PARSE("IndentExternBlock: NoIndent", IndentExternBlock,
19832               FormatStyle::IEBS_NoIndent);
19833   CHECK_PARSE("IndentExternBlock: true", IndentExternBlock,
19834               FormatStyle::IEBS_Indent);
19835   CHECK_PARSE("IndentExternBlock: false", IndentExternBlock,
19836               FormatStyle::IEBS_NoIndent);
19837 
19838   Style.BitFieldColonSpacing = FormatStyle::BFCS_None;
19839   CHECK_PARSE("BitFieldColonSpacing: Both", BitFieldColonSpacing,
19840               FormatStyle::BFCS_Both);
19841   CHECK_PARSE("BitFieldColonSpacing: None", BitFieldColonSpacing,
19842               FormatStyle::BFCS_None);
19843   CHECK_PARSE("BitFieldColonSpacing: Before", BitFieldColonSpacing,
19844               FormatStyle::BFCS_Before);
19845   CHECK_PARSE("BitFieldColonSpacing: After", BitFieldColonSpacing,
19846               FormatStyle::BFCS_After);
19847 
19848   Style.SortJavaStaticImport = FormatStyle::SJSIO_Before;
19849   CHECK_PARSE("SortJavaStaticImport: After", SortJavaStaticImport,
19850               FormatStyle::SJSIO_After);
19851   CHECK_PARSE("SortJavaStaticImport: Before", SortJavaStaticImport,
19852               FormatStyle::SJSIO_Before);
19853 
19854   // FIXME: This is required because parsing a configuration simply overwrites
19855   // the first N elements of the list instead of resetting it.
19856   Style.ForEachMacros.clear();
19857   std::vector<std::string> BoostForeach;
19858   BoostForeach.push_back("BOOST_FOREACH");
19859   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach);
19860   std::vector<std::string> BoostAndQForeach;
19861   BoostAndQForeach.push_back("BOOST_FOREACH");
19862   BoostAndQForeach.push_back("Q_FOREACH");
19863   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
19864               BoostAndQForeach);
19865 
19866   Style.IfMacros.clear();
19867   std::vector<std::string> CustomIfs;
19868   CustomIfs.push_back("MYIF");
19869   CHECK_PARSE("IfMacros: [MYIF]", IfMacros, CustomIfs);
19870 
19871   Style.AttributeMacros.clear();
19872   CHECK_PARSE("BasedOnStyle: LLVM", AttributeMacros,
19873               std::vector<std::string>{"__capability"});
19874   CHECK_PARSE("AttributeMacros: [attr1, attr2]", AttributeMacros,
19875               std::vector<std::string>({"attr1", "attr2"}));
19876 
19877   Style.StatementAttributeLikeMacros.clear();
19878   CHECK_PARSE("StatementAttributeLikeMacros: [emit,Q_EMIT]",
19879               StatementAttributeLikeMacros,
19880               std::vector<std::string>({"emit", "Q_EMIT"}));
19881 
19882   Style.StatementMacros.clear();
19883   CHECK_PARSE("StatementMacros: [QUNUSED]", StatementMacros,
19884               std::vector<std::string>{"QUNUSED"});
19885   CHECK_PARSE("StatementMacros: [QUNUSED, QT_REQUIRE_VERSION]", StatementMacros,
19886               std::vector<std::string>({"QUNUSED", "QT_REQUIRE_VERSION"}));
19887 
19888   Style.NamespaceMacros.clear();
19889   CHECK_PARSE("NamespaceMacros: [TESTSUITE]", NamespaceMacros,
19890               std::vector<std::string>{"TESTSUITE"});
19891   CHECK_PARSE("NamespaceMacros: [TESTSUITE, SUITE]", NamespaceMacros,
19892               std::vector<std::string>({"TESTSUITE", "SUITE"}));
19893 
19894   Style.WhitespaceSensitiveMacros.clear();
19895   CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE]",
19896               WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"});
19897   CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE, ASSERT]",
19898               WhitespaceSensitiveMacros,
19899               std::vector<std::string>({"STRINGIZE", "ASSERT"}));
19900   Style.WhitespaceSensitiveMacros.clear();
19901   CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE']",
19902               WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"});
19903   CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE', 'ASSERT']",
19904               WhitespaceSensitiveMacros,
19905               std::vector<std::string>({"STRINGIZE", "ASSERT"}));
19906 
19907   Style.IncludeStyle.IncludeCategories.clear();
19908   std::vector<tooling::IncludeStyle::IncludeCategory> ExpectedCategories = {
19909       {"abc/.*", 2, 0, false}, {".*", 1, 0, true}};
19910   CHECK_PARSE("IncludeCategories:\n"
19911               "  - Regex: abc/.*\n"
19912               "    Priority: 2\n"
19913               "  - Regex: .*\n"
19914               "    Priority: 1\n"
19915               "    CaseSensitive: true\n",
19916               IncludeStyle.IncludeCategories, ExpectedCategories);
19917   CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeStyle.IncludeIsMainRegex,
19918               "abc$");
19919   CHECK_PARSE("IncludeIsMainSourceRegex: 'abc$'",
19920               IncludeStyle.IncludeIsMainSourceRegex, "abc$");
19921 
19922   Style.SortIncludes = FormatStyle::SI_Never;
19923   CHECK_PARSE("SortIncludes: true", SortIncludes,
19924               FormatStyle::SI_CaseSensitive);
19925   CHECK_PARSE("SortIncludes: false", SortIncludes, FormatStyle::SI_Never);
19926   CHECK_PARSE("SortIncludes: CaseInsensitive", SortIncludes,
19927               FormatStyle::SI_CaseInsensitive);
19928   CHECK_PARSE("SortIncludes: CaseSensitive", SortIncludes,
19929               FormatStyle::SI_CaseSensitive);
19930   CHECK_PARSE("SortIncludes: Never", SortIncludes, FormatStyle::SI_Never);
19931 
19932   Style.RawStringFormats.clear();
19933   std::vector<FormatStyle::RawStringFormat> ExpectedRawStringFormats = {
19934       {
19935           FormatStyle::LK_TextProto,
19936           {"pb", "proto"},
19937           {"PARSE_TEXT_PROTO"},
19938           /*CanonicalDelimiter=*/"",
19939           "llvm",
19940       },
19941       {
19942           FormatStyle::LK_Cpp,
19943           {"cc", "cpp"},
19944           {"C_CODEBLOCK", "CPPEVAL"},
19945           /*CanonicalDelimiter=*/"cc",
19946           /*BasedOnStyle=*/"",
19947       },
19948   };
19949 
19950   CHECK_PARSE("RawStringFormats:\n"
19951               "  - Language: TextProto\n"
19952               "    Delimiters:\n"
19953               "      - 'pb'\n"
19954               "      - 'proto'\n"
19955               "    EnclosingFunctions:\n"
19956               "      - 'PARSE_TEXT_PROTO'\n"
19957               "    BasedOnStyle: llvm\n"
19958               "  - Language: Cpp\n"
19959               "    Delimiters:\n"
19960               "      - 'cc'\n"
19961               "      - 'cpp'\n"
19962               "    EnclosingFunctions:\n"
19963               "      - 'C_CODEBLOCK'\n"
19964               "      - 'CPPEVAL'\n"
19965               "    CanonicalDelimiter: 'cc'",
19966               RawStringFormats, ExpectedRawStringFormats);
19967 
19968   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
19969               "  Minimum: 0\n"
19970               "  Maximum: 0",
19971               SpacesInLineCommentPrefix.Minimum, 0u);
19972   EXPECT_EQ(Style.SpacesInLineCommentPrefix.Maximum, 0u);
19973   Style.SpacesInLineCommentPrefix.Minimum = 1;
19974   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
19975               "  Minimum: 2",
19976               SpacesInLineCommentPrefix.Minimum, 0u);
19977   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
19978               "  Maximum: -1",
19979               SpacesInLineCommentPrefix.Maximum, -1u);
19980   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
19981               "  Minimum: 2",
19982               SpacesInLineCommentPrefix.Minimum, 2u);
19983   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
19984               "  Maximum: 1",
19985               SpacesInLineCommentPrefix.Maximum, 1u);
19986   EXPECT_EQ(Style.SpacesInLineCommentPrefix.Minimum, 1u);
19987 
19988   Style.SpacesInAngles = FormatStyle::SIAS_Always;
19989   CHECK_PARSE("SpacesInAngles: Never", SpacesInAngles, FormatStyle::SIAS_Never);
19990   CHECK_PARSE("SpacesInAngles: Always", SpacesInAngles,
19991               FormatStyle::SIAS_Always);
19992   CHECK_PARSE("SpacesInAngles: Leave", SpacesInAngles, FormatStyle::SIAS_Leave);
19993   // For backward compatibility:
19994   CHECK_PARSE("SpacesInAngles: false", SpacesInAngles, FormatStyle::SIAS_Never);
19995   CHECK_PARSE("SpacesInAngles: true", SpacesInAngles, FormatStyle::SIAS_Always);
19996 
19997   CHECK_PARSE("RequiresClausePosition: WithPreceding", RequiresClausePosition,
19998               FormatStyle::RCPS_WithPreceding);
19999   CHECK_PARSE("RequiresClausePosition: WithFollowing", RequiresClausePosition,
20000               FormatStyle::RCPS_WithFollowing);
20001   CHECK_PARSE("RequiresClausePosition: SingleLine", RequiresClausePosition,
20002               FormatStyle::RCPS_SingleLine);
20003   CHECK_PARSE("RequiresClausePosition: OwnLine", RequiresClausePosition,
20004               FormatStyle::RCPS_OwnLine);
20005 
20006   CHECK_PARSE("BreakBeforeConceptDeclarations: Never",
20007               BreakBeforeConceptDeclarations, FormatStyle::BBCDS_Never);
20008   CHECK_PARSE("BreakBeforeConceptDeclarations: Always",
20009               BreakBeforeConceptDeclarations, FormatStyle::BBCDS_Always);
20010   CHECK_PARSE("BreakBeforeConceptDeclarations: Allowed",
20011               BreakBeforeConceptDeclarations, FormatStyle::BBCDS_Allowed);
20012   // For backward compatibility:
20013   CHECK_PARSE("BreakBeforeConceptDeclarations: true",
20014               BreakBeforeConceptDeclarations, FormatStyle::BBCDS_Always);
20015   CHECK_PARSE("BreakBeforeConceptDeclarations: false",
20016               BreakBeforeConceptDeclarations, FormatStyle::BBCDS_Allowed);
20017 }
20018 
20019 TEST_F(FormatTest, ParsesConfigurationWithLanguages) {
20020   FormatStyle Style = {};
20021   Style.Language = FormatStyle::LK_Cpp;
20022   CHECK_PARSE("Language: Cpp\n"
20023               "IndentWidth: 12",
20024               IndentWidth, 12u);
20025   EXPECT_EQ(parseConfiguration("Language: JavaScript\n"
20026                                "IndentWidth: 34",
20027                                &Style),
20028             ParseError::Unsuitable);
20029   FormatStyle BinPackedTCS = {};
20030   BinPackedTCS.Language = FormatStyle::LK_JavaScript;
20031   EXPECT_EQ(parseConfiguration("BinPackArguments: true\n"
20032                                "InsertTrailingCommas: Wrapped",
20033                                &BinPackedTCS),
20034             ParseError::BinPackTrailingCommaConflict);
20035   EXPECT_EQ(12u, Style.IndentWidth);
20036   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
20037   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
20038 
20039   Style.Language = FormatStyle::LK_JavaScript;
20040   CHECK_PARSE("Language: JavaScript\n"
20041               "IndentWidth: 12",
20042               IndentWidth, 12u);
20043   CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u);
20044   EXPECT_EQ(parseConfiguration("Language: Cpp\n"
20045                                "IndentWidth: 34",
20046                                &Style),
20047             ParseError::Unsuitable);
20048   EXPECT_EQ(23u, Style.IndentWidth);
20049   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
20050   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
20051 
20052   CHECK_PARSE("BasedOnStyle: LLVM\n"
20053               "IndentWidth: 67",
20054               IndentWidth, 67u);
20055 
20056   CHECK_PARSE("---\n"
20057               "Language: JavaScript\n"
20058               "IndentWidth: 12\n"
20059               "---\n"
20060               "Language: Cpp\n"
20061               "IndentWidth: 34\n"
20062               "...\n",
20063               IndentWidth, 12u);
20064 
20065   Style.Language = FormatStyle::LK_Cpp;
20066   CHECK_PARSE("---\n"
20067               "Language: JavaScript\n"
20068               "IndentWidth: 12\n"
20069               "---\n"
20070               "Language: Cpp\n"
20071               "IndentWidth: 34\n"
20072               "...\n",
20073               IndentWidth, 34u);
20074   CHECK_PARSE("---\n"
20075               "IndentWidth: 78\n"
20076               "---\n"
20077               "Language: JavaScript\n"
20078               "IndentWidth: 56\n"
20079               "...\n",
20080               IndentWidth, 78u);
20081 
20082   Style.ColumnLimit = 123;
20083   Style.IndentWidth = 234;
20084   Style.BreakBeforeBraces = FormatStyle::BS_Linux;
20085   Style.TabWidth = 345;
20086   EXPECT_FALSE(parseConfiguration("---\n"
20087                                   "IndentWidth: 456\n"
20088                                   "BreakBeforeBraces: Allman\n"
20089                                   "---\n"
20090                                   "Language: JavaScript\n"
20091                                   "IndentWidth: 111\n"
20092                                   "TabWidth: 111\n"
20093                                   "---\n"
20094                                   "Language: Cpp\n"
20095                                   "BreakBeforeBraces: Stroustrup\n"
20096                                   "TabWidth: 789\n"
20097                                   "...\n",
20098                                   &Style));
20099   EXPECT_EQ(123u, Style.ColumnLimit);
20100   EXPECT_EQ(456u, Style.IndentWidth);
20101   EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces);
20102   EXPECT_EQ(789u, Style.TabWidth);
20103 
20104   EXPECT_EQ(parseConfiguration("---\n"
20105                                "Language: JavaScript\n"
20106                                "IndentWidth: 56\n"
20107                                "---\n"
20108                                "IndentWidth: 78\n"
20109                                "...\n",
20110                                &Style),
20111             ParseError::Error);
20112   EXPECT_EQ(parseConfiguration("---\n"
20113                                "Language: JavaScript\n"
20114                                "IndentWidth: 56\n"
20115                                "---\n"
20116                                "Language: JavaScript\n"
20117                                "IndentWidth: 78\n"
20118                                "...\n",
20119                                &Style),
20120             ParseError::Error);
20121 
20122   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
20123 }
20124 
20125 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) {
20126   FormatStyle Style = {};
20127   Style.Language = FormatStyle::LK_JavaScript;
20128   Style.BreakBeforeTernaryOperators = true;
20129   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value());
20130   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
20131 
20132   Style.BreakBeforeTernaryOperators = true;
20133   EXPECT_EQ(0, parseConfiguration("---\n"
20134                                   "BasedOnStyle: Google\n"
20135                                   "---\n"
20136                                   "Language: JavaScript\n"
20137                                   "IndentWidth: 76\n"
20138                                   "...\n",
20139                                   &Style)
20140                    .value());
20141   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
20142   EXPECT_EQ(76u, Style.IndentWidth);
20143   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
20144 }
20145 
20146 TEST_F(FormatTest, ConfigurationRoundTripTest) {
20147   FormatStyle Style = getLLVMStyle();
20148   std::string YAML = configurationAsText(Style);
20149   FormatStyle ParsedStyle = {};
20150   ParsedStyle.Language = FormatStyle::LK_Cpp;
20151   EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
20152   EXPECT_EQ(Style, ParsedStyle);
20153 }
20154 
20155 TEST_F(FormatTest, WorksFor8bitEncodings) {
20156   EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
20157             "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
20158             "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
20159             "\"\xef\xee\xf0\xf3...\"",
20160             format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
20161                    "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
20162                    "\xef\xee\xf0\xf3...\"",
20163                    getLLVMStyleWithColumns(12)));
20164 }
20165 
20166 TEST_F(FormatTest, HandlesUTF8BOM) {
20167   EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf"));
20168   EXPECT_EQ("\xef\xbb\xbf#include <iostream>",
20169             format("\xef\xbb\xbf#include <iostream>"));
20170   EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>",
20171             format("\xef\xbb\xbf\n#include <iostream>"));
20172 }
20173 
20174 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
20175 #if !defined(_MSC_VER)
20176 
20177 TEST_F(FormatTest, CountsUTF8CharactersProperly) {
20178   verifyFormat("\"Однажды в студёную зимнюю пору...\"",
20179                getLLVMStyleWithColumns(35));
20180   verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
20181                getLLVMStyleWithColumns(31));
20182   verifyFormat("// Однажды в студёную зимнюю пору...",
20183                getLLVMStyleWithColumns(36));
20184   verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32));
20185   verifyFormat("/* Однажды в студёную зимнюю пору... */",
20186                getLLVMStyleWithColumns(39));
20187   verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
20188                getLLVMStyleWithColumns(35));
20189 }
20190 
20191 TEST_F(FormatTest, SplitsUTF8Strings) {
20192   // Non-printable characters' width is currently considered to be the length in
20193   // bytes in UTF8. The characters can be displayed in very different manner
20194   // (zero-width, single width with a substitution glyph, expanded to their code
20195   // (e.g. "<8d>"), so there's no single correct way to handle them.
20196   EXPECT_EQ("\"aaaaÄ\"\n"
20197             "\"\xc2\x8d\";",
20198             format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
20199   EXPECT_EQ("\"aaaaaaaÄ\"\n"
20200             "\"\xc2\x8d\";",
20201             format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
20202   EXPECT_EQ("\"Однажды, в \"\n"
20203             "\"студёную \"\n"
20204             "\"зимнюю \"\n"
20205             "\"пору,\"",
20206             format("\"Однажды, в студёную зимнюю пору,\"",
20207                    getLLVMStyleWithColumns(13)));
20208   EXPECT_EQ(
20209       "\"一 二 三 \"\n"
20210       "\"四 五六 \"\n"
20211       "\"七 八 九 \"\n"
20212       "\"十\"",
20213       format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11)));
20214   EXPECT_EQ("\"一\t\"\n"
20215             "\"二 \t\"\n"
20216             "\"三 四 \"\n"
20217             "\"五\t\"\n"
20218             "\"六 \t\"\n"
20219             "\"七 \"\n"
20220             "\"八九十\tqq\"",
20221             format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
20222                    getLLVMStyleWithColumns(11)));
20223 
20224   // UTF8 character in an escape sequence.
20225   EXPECT_EQ("\"aaaaaa\"\n"
20226             "\"\\\xC2\x8D\"",
20227             format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10)));
20228 }
20229 
20230 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
20231   EXPECT_EQ("const char *sssss =\n"
20232             "    \"一二三四五六七八\\\n"
20233             " 九 十\";",
20234             format("const char *sssss = \"一二三四五六七八\\\n"
20235                    " 九 十\";",
20236                    getLLVMStyleWithColumns(30)));
20237 }
20238 
20239 TEST_F(FormatTest, SplitsUTF8LineComments) {
20240   EXPECT_EQ("// aaaaÄ\xc2\x8d",
20241             format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10)));
20242   EXPECT_EQ("// Я из лесу\n"
20243             "// вышел; был\n"
20244             "// сильный\n"
20245             "// мороз.",
20246             format("// Я из лесу вышел; был сильный мороз.",
20247                    getLLVMStyleWithColumns(13)));
20248   EXPECT_EQ("// 一二三\n"
20249             "// 四五六七\n"
20250             "// 八  九\n"
20251             "// 十",
20252             format("// 一二三 四五六七 八  九 十", getLLVMStyleWithColumns(9)));
20253 }
20254 
20255 TEST_F(FormatTest, SplitsUTF8BlockComments) {
20256   EXPECT_EQ("/* Гляжу,\n"
20257             " * поднимается\n"
20258             " * медленно в\n"
20259             " * гору\n"
20260             " * Лошадка,\n"
20261             " * везущая\n"
20262             " * хворосту\n"
20263             " * воз. */",
20264             format("/* Гляжу, поднимается медленно в гору\n"
20265                    " * Лошадка, везущая хворосту воз. */",
20266                    getLLVMStyleWithColumns(13)));
20267   EXPECT_EQ(
20268       "/* 一二三\n"
20269       " * 四五六七\n"
20270       " * 八  九\n"
20271       " * 十  */",
20272       format("/* 一二三 四五六七 八  九 十  */", getLLVMStyleWithColumns(9)));
20273   EXPECT_EQ("/* �������� ��������\n"
20274             " * ��������\n"
20275             " * ������-�� */",
20276             format("/* �������� �������� �������� ������-�� */", getLLVMStyleWithColumns(12)));
20277 }
20278 
20279 #endif // _MSC_VER
20280 
20281 TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
20282   FormatStyle Style = getLLVMStyle();
20283 
20284   Style.ConstructorInitializerIndentWidth = 4;
20285   verifyFormat(
20286       "SomeClass::Constructor()\n"
20287       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
20288       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
20289       Style);
20290 
20291   Style.ConstructorInitializerIndentWidth = 2;
20292   verifyFormat(
20293       "SomeClass::Constructor()\n"
20294       "  : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
20295       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
20296       Style);
20297 
20298   Style.ConstructorInitializerIndentWidth = 0;
20299   verifyFormat(
20300       "SomeClass::Constructor()\n"
20301       ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
20302       "  aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
20303       Style);
20304   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
20305   verifyFormat(
20306       "SomeLongTemplateVariableName<\n"
20307       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>",
20308       Style);
20309   verifyFormat("bool smaller = 1 < "
20310                "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
20311                "                       "
20312                "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
20313                Style);
20314 
20315   Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
20316   verifyFormat("SomeClass::Constructor() :\n"
20317                "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa),\n"
20318                "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa) {}",
20319                Style);
20320 }
20321 
20322 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {
20323   FormatStyle Style = getLLVMStyle();
20324   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
20325   Style.ConstructorInitializerIndentWidth = 4;
20326   verifyFormat("SomeClass::Constructor()\n"
20327                "    : a(a)\n"
20328                "    , b(b)\n"
20329                "    , c(c) {}",
20330                Style);
20331   verifyFormat("SomeClass::Constructor()\n"
20332                "    : a(a) {}",
20333                Style);
20334 
20335   Style.ColumnLimit = 0;
20336   verifyFormat("SomeClass::Constructor()\n"
20337                "    : a(a) {}",
20338                Style);
20339   verifyFormat("SomeClass::Constructor() noexcept\n"
20340                "    : a(a) {}",
20341                Style);
20342   verifyFormat("SomeClass::Constructor()\n"
20343                "    : a(a)\n"
20344                "    , b(b)\n"
20345                "    , c(c) {}",
20346                Style);
20347   verifyFormat("SomeClass::Constructor()\n"
20348                "    : a(a) {\n"
20349                "  foo();\n"
20350                "  bar();\n"
20351                "}",
20352                Style);
20353 
20354   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
20355   verifyFormat("SomeClass::Constructor()\n"
20356                "    : a(a)\n"
20357                "    , b(b)\n"
20358                "    , c(c) {\n}",
20359                Style);
20360   verifyFormat("SomeClass::Constructor()\n"
20361                "    : a(a) {\n}",
20362                Style);
20363 
20364   Style.ColumnLimit = 80;
20365   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
20366   Style.ConstructorInitializerIndentWidth = 2;
20367   verifyFormat("SomeClass::Constructor()\n"
20368                "  : a(a)\n"
20369                "  , b(b)\n"
20370                "  , c(c) {}",
20371                Style);
20372 
20373   Style.ConstructorInitializerIndentWidth = 0;
20374   verifyFormat("SomeClass::Constructor()\n"
20375                ": a(a)\n"
20376                ", b(b)\n"
20377                ", c(c) {}",
20378                Style);
20379 
20380   Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
20381   Style.ConstructorInitializerIndentWidth = 4;
20382   verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
20383   verifyFormat(
20384       "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n",
20385       Style);
20386   verifyFormat(
20387       "SomeClass::Constructor()\n"
20388       "    : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
20389       Style);
20390   Style.ConstructorInitializerIndentWidth = 4;
20391   Style.ColumnLimit = 60;
20392   verifyFormat("SomeClass::Constructor()\n"
20393                "    : aaaaaaaa(aaaaaaaa)\n"
20394                "    , aaaaaaaa(aaaaaaaa)\n"
20395                "    , aaaaaaaa(aaaaaaaa) {}",
20396                Style);
20397 }
20398 
20399 TEST_F(FormatTest, ConstructorInitializersWithPreprocessorDirective) {
20400   FormatStyle Style = getLLVMStyle();
20401   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
20402   Style.ConstructorInitializerIndentWidth = 4;
20403   verifyFormat("SomeClass::Constructor()\n"
20404                "    : a{a}\n"
20405                "    , b{b} {}",
20406                Style);
20407   verifyFormat("SomeClass::Constructor()\n"
20408                "    : a{a}\n"
20409                "#if CONDITION\n"
20410                "    , b{b}\n"
20411                "#endif\n"
20412                "{\n}",
20413                Style);
20414   Style.ConstructorInitializerIndentWidth = 2;
20415   verifyFormat("SomeClass::Constructor()\n"
20416                "#if CONDITION\n"
20417                "  : a{a}\n"
20418                "#endif\n"
20419                "  , b{b}\n"
20420                "  , c{c} {\n}",
20421                Style);
20422   Style.ConstructorInitializerIndentWidth = 0;
20423   verifyFormat("SomeClass::Constructor()\n"
20424                ": a{a}\n"
20425                "#ifdef CONDITION\n"
20426                ", b{b}\n"
20427                "#else\n"
20428                ", c{c}\n"
20429                "#endif\n"
20430                ", d{d} {\n}",
20431                Style);
20432   Style.ConstructorInitializerIndentWidth = 4;
20433   verifyFormat("SomeClass::Constructor()\n"
20434                "    : a{a}\n"
20435                "#if WINDOWS\n"
20436                "#if DEBUG\n"
20437                "    , b{0}\n"
20438                "#else\n"
20439                "    , b{1}\n"
20440                "#endif\n"
20441                "#else\n"
20442                "#if DEBUG\n"
20443                "    , b{2}\n"
20444                "#else\n"
20445                "    , b{3}\n"
20446                "#endif\n"
20447                "#endif\n"
20448                "{\n}",
20449                Style);
20450   verifyFormat("SomeClass::Constructor()\n"
20451                "    : a{a}\n"
20452                "#if WINDOWS\n"
20453                "    , b{0}\n"
20454                "#if DEBUG\n"
20455                "    , c{0}\n"
20456                "#else\n"
20457                "    , c{1}\n"
20458                "#endif\n"
20459                "#else\n"
20460                "#if DEBUG\n"
20461                "    , c{2}\n"
20462                "#else\n"
20463                "    , c{3}\n"
20464                "#endif\n"
20465                "    , b{1}\n"
20466                "#endif\n"
20467                "{\n}",
20468                Style);
20469 }
20470 
20471 TEST_F(FormatTest, Destructors) {
20472   verifyFormat("void F(int &i) { i.~int(); }");
20473   verifyFormat("void F(int &i) { i->~int(); }");
20474 }
20475 
20476 TEST_F(FormatTest, FormatsWithWebKitStyle) {
20477   FormatStyle Style = getWebKitStyle();
20478 
20479   // Don't indent in outer namespaces.
20480   verifyFormat("namespace outer {\n"
20481                "int i;\n"
20482                "namespace inner {\n"
20483                "    int i;\n"
20484                "} // namespace inner\n"
20485                "} // namespace outer\n"
20486                "namespace other_outer {\n"
20487                "int i;\n"
20488                "}",
20489                Style);
20490 
20491   // Don't indent case labels.
20492   verifyFormat("switch (variable) {\n"
20493                "case 1:\n"
20494                "case 2:\n"
20495                "    doSomething();\n"
20496                "    break;\n"
20497                "default:\n"
20498                "    ++variable;\n"
20499                "}",
20500                Style);
20501 
20502   // Wrap before binary operators.
20503   EXPECT_EQ("void f()\n"
20504             "{\n"
20505             "    if (aaaaaaaaaaaaaaaa\n"
20506             "        && bbbbbbbbbbbbbbbbbbbbbbbb\n"
20507             "        && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
20508             "        return;\n"
20509             "}",
20510             format("void f() {\n"
20511                    "if (aaaaaaaaaaaaaaaa\n"
20512                    "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
20513                    "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
20514                    "return;\n"
20515                    "}",
20516                    Style));
20517 
20518   // Allow functions on a single line.
20519   verifyFormat("void f() { return; }", Style);
20520 
20521   // Allow empty blocks on a single line and insert a space in empty blocks.
20522   EXPECT_EQ("void f() { }", format("void f() {}", Style));
20523   EXPECT_EQ("while (true) { }", format("while (true) {}", Style));
20524   // However, don't merge non-empty short loops.
20525   EXPECT_EQ("while (true) {\n"
20526             "    continue;\n"
20527             "}",
20528             format("while (true) { continue; }", Style));
20529 
20530   // Constructor initializers are formatted one per line with the "," on the
20531   // new line.
20532   verifyFormat("Constructor()\n"
20533                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
20534                "    , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
20535                "          aaaaaaaaaaaaaa)\n"
20536                "    , aaaaaaaaaaaaaaaaaaaaaaa()\n"
20537                "{\n"
20538                "}",
20539                Style);
20540   verifyFormat("SomeClass::Constructor()\n"
20541                "    : a(a)\n"
20542                "{\n"
20543                "}",
20544                Style);
20545   EXPECT_EQ("SomeClass::Constructor()\n"
20546             "    : a(a)\n"
20547             "{\n"
20548             "}",
20549             format("SomeClass::Constructor():a(a){}", Style));
20550   verifyFormat("SomeClass::Constructor()\n"
20551                "    : a(a)\n"
20552                "    , b(b)\n"
20553                "    , c(c)\n"
20554                "{\n"
20555                "}",
20556                Style);
20557   verifyFormat("SomeClass::Constructor()\n"
20558                "    : a(a)\n"
20559                "{\n"
20560                "    foo();\n"
20561                "    bar();\n"
20562                "}",
20563                Style);
20564 
20565   // Access specifiers should be aligned left.
20566   verifyFormat("class C {\n"
20567                "public:\n"
20568                "    int i;\n"
20569                "};",
20570                Style);
20571 
20572   // Do not align comments.
20573   verifyFormat("int a; // Do not\n"
20574                "double b; // align comments.",
20575                Style);
20576 
20577   // Do not align operands.
20578   EXPECT_EQ("ASSERT(aaaa\n"
20579             "    || bbbb);",
20580             format("ASSERT ( aaaa\n||bbbb);", Style));
20581 
20582   // Accept input's line breaks.
20583   EXPECT_EQ("if (aaaaaaaaaaaaaaa\n"
20584             "    || bbbbbbbbbbbbbbb) {\n"
20585             "    i++;\n"
20586             "}",
20587             format("if (aaaaaaaaaaaaaaa\n"
20588                    "|| bbbbbbbbbbbbbbb) { i++; }",
20589                    Style));
20590   EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
20591             "    i++;\n"
20592             "}",
20593             format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
20594 
20595   // Don't automatically break all macro definitions (llvm.org/PR17842).
20596   verifyFormat("#define aNumber 10", Style);
20597   // However, generally keep the line breaks that the user authored.
20598   EXPECT_EQ("#define aNumber \\\n"
20599             "    10",
20600             format("#define aNumber \\\n"
20601                    " 10",
20602                    Style));
20603 
20604   // Keep empty and one-element array literals on a single line.
20605   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
20606             "                                  copyItems:YES];",
20607             format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
20608                    "copyItems:YES];",
20609                    Style));
20610   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
20611             "                                  copyItems:YES];",
20612             format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
20613                    "             copyItems:YES];",
20614                    Style));
20615   // FIXME: This does not seem right, there should be more indentation before
20616   // the array literal's entries. Nested blocks have the same problem.
20617   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
20618             "    @\"a\",\n"
20619             "    @\"a\"\n"
20620             "]\n"
20621             "                                  copyItems:YES];",
20622             format("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
20623                    "     @\"a\",\n"
20624                    "     @\"a\"\n"
20625                    "     ]\n"
20626                    "       copyItems:YES];",
20627                    Style));
20628   EXPECT_EQ(
20629       "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
20630       "                                  copyItems:YES];",
20631       format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
20632              "   copyItems:YES];",
20633              Style));
20634 
20635   verifyFormat("[self.a b:c c:d];", Style);
20636   EXPECT_EQ("[self.a b:c\n"
20637             "        c:d];",
20638             format("[self.a b:c\n"
20639                    "c:d];",
20640                    Style));
20641 }
20642 
20643 TEST_F(FormatTest, FormatsLambdas) {
20644   verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n");
20645   verifyFormat(
20646       "int c = [b]() mutable noexcept { return [&b] { return b++; }(); }();\n");
20647   verifyFormat("int c = [&] { [=] { return b++; }(); }();\n");
20648   verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n");
20649   verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n");
20650   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n");
20651   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n");
20652   verifyFormat("auto c = [a = [b = 42] {}] {};\n");
20653   verifyFormat("auto c = [a = &i + 10, b = [] {}] {};\n");
20654   verifyFormat("int x = f(*+[] {});");
20655   verifyFormat("void f() {\n"
20656                "  other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
20657                "}\n");
20658   verifyFormat("void f() {\n"
20659                "  other(x.begin(), //\n"
20660                "        x.end(),   //\n"
20661                "        [&](int, int) { return 1; });\n"
20662                "}\n");
20663   verifyFormat("void f() {\n"
20664                "  other.other.other.other.other(\n"
20665                "      x.begin(), x.end(),\n"
20666                "      [something, rather](int, int, int, int, int, int, int) { "
20667                "return 1; });\n"
20668                "}\n");
20669   verifyFormat(
20670       "void f() {\n"
20671       "  other.other.other.other.other(\n"
20672       "      x.begin(), x.end(),\n"
20673       "      [something, rather](int, int, int, int, int, int, int) {\n"
20674       "        //\n"
20675       "      });\n"
20676       "}\n");
20677   verifyFormat("SomeFunction([]() { // A cool function...\n"
20678                "  return 43;\n"
20679                "});");
20680   EXPECT_EQ("SomeFunction([]() {\n"
20681             "#define A a\n"
20682             "  return 43;\n"
20683             "});",
20684             format("SomeFunction([](){\n"
20685                    "#define A a\n"
20686                    "return 43;\n"
20687                    "});"));
20688   verifyFormat("void f() {\n"
20689                "  SomeFunction([](decltype(x), A *a) {});\n"
20690                "  SomeFunction([](typeof(x), A *a) {});\n"
20691                "  SomeFunction([](_Atomic(x), A *a) {});\n"
20692                "  SomeFunction([](__underlying_type(x), A *a) {});\n"
20693                "}");
20694   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
20695                "    [](const aaaaaaaaaa &a) { return a; });");
20696   verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
20697                "  SomeOtherFunctioooooooooooooooooooooooooon();\n"
20698                "});");
20699   verifyFormat("Constructor()\n"
20700                "    : Field([] { // comment\n"
20701                "        int i;\n"
20702                "      }) {}");
20703   verifyFormat("auto my_lambda = [](const string &some_parameter) {\n"
20704                "  return some_parameter.size();\n"
20705                "};");
20706   verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n"
20707                "    [](const string &s) { return s; };");
20708   verifyFormat("int i = aaaaaa ? 1 //\n"
20709                "               : [] {\n"
20710                "                   return 2; //\n"
20711                "                 }();");
20712   verifyFormat("llvm::errs() << \"number of twos is \"\n"
20713                "             << std::count_if(v.begin(), v.end(), [](int x) {\n"
20714                "                  return x == 2; // force break\n"
20715                "                });");
20716   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
20717                "    [=](int iiiiiiiiiiii) {\n"
20718                "      return aaaaaaaaaaaaaaaaaaaaaaa !=\n"
20719                "             aaaaaaaaaaaaaaaaaaaaaaa;\n"
20720                "    });",
20721                getLLVMStyleWithColumns(60));
20722 
20723   verifyFormat("SomeFunction({[&] {\n"
20724                "                // comment\n"
20725                "              },\n"
20726                "              [&] {\n"
20727                "                // comment\n"
20728                "              }});");
20729   verifyFormat("SomeFunction({[&] {\n"
20730                "  // comment\n"
20731                "}});");
20732   verifyFormat(
20733       "virtual aaaaaaaaaaaaaaaa(\n"
20734       "    std::function<bool()> bbbbbbbbbbbb = [&]() { return true; },\n"
20735       "    aaaaa aaaaaaaaa);");
20736 
20737   // Lambdas with return types.
20738   verifyFormat("int c = []() -> int { return 2; }();\n");
20739   verifyFormat("int c = []() -> int * { return 2; }();\n");
20740   verifyFormat("int c = []() -> vector<int> { return {2}; }();\n");
20741   verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
20742   verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
20743   verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
20744   verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
20745   verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
20746   verifyFormat("[a, a]() -> a<1> {};");
20747   verifyFormat("[]() -> foo<5 + 2> { return {}; };");
20748   verifyFormat("[]() -> foo<5 - 2> { return {}; };");
20749   verifyFormat("[]() -> foo<5 / 2> { return {}; };");
20750   verifyFormat("[]() -> foo<5 * 2> { return {}; };");
20751   verifyFormat("[]() -> foo<5 % 2> { return {}; };");
20752   verifyFormat("[]() -> foo<5 << 2> { return {}; };");
20753   verifyFormat("[]() -> foo<!5> { return {}; };");
20754   verifyFormat("[]() -> foo<~5> { return {}; };");
20755   verifyFormat("[]() -> foo<5 | 2> { return {}; };");
20756   verifyFormat("[]() -> foo<5 || 2> { return {}; };");
20757   verifyFormat("[]() -> foo<5 & 2> { return {}; };");
20758   verifyFormat("[]() -> foo<5 && 2> { return {}; };");
20759   verifyFormat("[]() -> foo<5 == 2> { return {}; };");
20760   verifyFormat("[]() -> foo<5 != 2> { return {}; };");
20761   verifyFormat("[]() -> foo<5 >= 2> { return {}; };");
20762   verifyFormat("[]() -> foo<5 <= 2> { return {}; };");
20763   verifyFormat("[]() -> foo<5 < 2> { return {}; };");
20764   verifyFormat("[]() -> foo<2 ? 1 : 0> { return {}; };");
20765   verifyFormat("namespace bar {\n"
20766                "// broken:\n"
20767                "auto foo{[]() -> foo<5 + 2> { return {}; }};\n"
20768                "} // namespace bar");
20769   verifyFormat("namespace bar {\n"
20770                "// broken:\n"
20771                "auto foo{[]() -> foo<5 - 2> { return {}; }};\n"
20772                "} // namespace bar");
20773   verifyFormat("namespace bar {\n"
20774                "// broken:\n"
20775                "auto foo{[]() -> foo<5 / 2> { return {}; }};\n"
20776                "} // namespace bar");
20777   verifyFormat("namespace bar {\n"
20778                "// broken:\n"
20779                "auto foo{[]() -> foo<5 * 2> { return {}; }};\n"
20780                "} // namespace bar");
20781   verifyFormat("namespace bar {\n"
20782                "// broken:\n"
20783                "auto foo{[]() -> foo<5 % 2> { return {}; }};\n"
20784                "} // namespace bar");
20785   verifyFormat("namespace bar {\n"
20786                "// broken:\n"
20787                "auto foo{[]() -> foo<5 << 2> { return {}; }};\n"
20788                "} // namespace bar");
20789   verifyFormat("namespace bar {\n"
20790                "// broken:\n"
20791                "auto foo{[]() -> foo<!5> { return {}; }};\n"
20792                "} // namespace bar");
20793   verifyFormat("namespace bar {\n"
20794                "// broken:\n"
20795                "auto foo{[]() -> foo<~5> { return {}; }};\n"
20796                "} // namespace bar");
20797   verifyFormat("namespace bar {\n"
20798                "// broken:\n"
20799                "auto foo{[]() -> foo<5 | 2> { return {}; }};\n"
20800                "} // namespace bar");
20801   verifyFormat("namespace bar {\n"
20802                "// broken:\n"
20803                "auto foo{[]() -> foo<5 || 2> { return {}; }};\n"
20804                "} // namespace bar");
20805   verifyFormat("namespace bar {\n"
20806                "// broken:\n"
20807                "auto foo{[]() -> foo<5 & 2> { return {}; }};\n"
20808                "} // namespace bar");
20809   verifyFormat("namespace bar {\n"
20810                "// broken:\n"
20811                "auto foo{[]() -> foo<5 && 2> { return {}; }};\n"
20812                "} // namespace bar");
20813   verifyFormat("namespace bar {\n"
20814                "// broken:\n"
20815                "auto foo{[]() -> foo<5 == 2> { return {}; }};\n"
20816                "} // namespace bar");
20817   verifyFormat("namespace bar {\n"
20818                "// broken:\n"
20819                "auto foo{[]() -> foo<5 != 2> { return {}; }};\n"
20820                "} // namespace bar");
20821   verifyFormat("namespace bar {\n"
20822                "// broken:\n"
20823                "auto foo{[]() -> foo<5 >= 2> { return {}; }};\n"
20824                "} // namespace bar");
20825   verifyFormat("namespace bar {\n"
20826                "// broken:\n"
20827                "auto foo{[]() -> foo<5 <= 2> { return {}; }};\n"
20828                "} // namespace bar");
20829   verifyFormat("namespace bar {\n"
20830                "// broken:\n"
20831                "auto foo{[]() -> foo<5 < 2> { return {}; }};\n"
20832                "} // namespace bar");
20833   verifyFormat("namespace bar {\n"
20834                "// broken:\n"
20835                "auto foo{[]() -> foo<2 ? 1 : 0> { return {}; }};\n"
20836                "} // namespace bar");
20837   verifyFormat("[]() -> a<1> {};");
20838   verifyFormat("[]() -> a<1> { ; };");
20839   verifyFormat("[]() -> a<1> { ; }();");
20840   verifyFormat("[a, a]() -> a<true> {};");
20841   verifyFormat("[]() -> a<true> {};");
20842   verifyFormat("[]() -> a<true> { ; };");
20843   verifyFormat("[]() -> a<true> { ; }();");
20844   verifyFormat("[a, a]() -> a<false> {};");
20845   verifyFormat("[]() -> a<false> {};");
20846   verifyFormat("[]() -> a<false> { ; };");
20847   verifyFormat("[]() -> a<false> { ; }();");
20848   verifyFormat("auto foo{[]() -> foo<false> { ; }};");
20849   verifyFormat("namespace bar {\n"
20850                "auto foo{[]() -> foo<false> { ; }};\n"
20851                "} // namespace bar");
20852   verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
20853                "                   int j) -> int {\n"
20854                "  return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
20855                "};");
20856   verifyFormat(
20857       "aaaaaaaaaaaaaaaaaaaaaa(\n"
20858       "    [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n"
20859       "      return aaaaaaaaaaaaaaaaa;\n"
20860       "    });",
20861       getLLVMStyleWithColumns(70));
20862   verifyFormat("[]() //\n"
20863                "    -> int {\n"
20864                "  return 1; //\n"
20865                "};");
20866   verifyFormat("[]() -> Void<T...> {};");
20867   verifyFormat("[a, b]() -> Tuple<T...> { return {}; };");
20868   verifyFormat("SomeFunction({[]() -> int[] { return {}; }});");
20869   verifyFormat("SomeFunction({[]() -> int *[] { return {}; }});");
20870   verifyFormat("SomeFunction({[]() -> int (*)[] { return {}; }});");
20871   verifyFormat("SomeFunction({[]() -> ns::type<int (*)[]> { return {}; }});");
20872   verifyFormat("return int{[x = x]() { return x; }()};");
20873 
20874   // Lambdas with explicit template argument lists.
20875   verifyFormat(
20876       "auto L = []<template <typename> class T, class U>(T<U> &&a) {};\n");
20877   verifyFormat("auto L = []<class T>(T) {\n"
20878                "  {\n"
20879                "    f();\n"
20880                "    g();\n"
20881                "  }\n"
20882                "};\n");
20883   verifyFormat("auto L = []<class... T>(T...) {\n"
20884                "  {\n"
20885                "    f();\n"
20886                "    g();\n"
20887                "  }\n"
20888                "};\n");
20889   verifyFormat("auto L = []<typename... T>(T...) {\n"
20890                "  {\n"
20891                "    f();\n"
20892                "    g();\n"
20893                "  }\n"
20894                "};\n");
20895   verifyFormat("auto L = []<template <typename...> class T>(T...) {\n"
20896                "  {\n"
20897                "    f();\n"
20898                "    g();\n"
20899                "  }\n"
20900                "};\n");
20901   verifyFormat("auto L = []</*comment*/ class... T>(T...) {\n"
20902                "  {\n"
20903                "    f();\n"
20904                "    g();\n"
20905                "  }\n"
20906                "};\n");
20907 
20908   // Multiple lambdas in the same parentheses change indentation rules. These
20909   // lambdas are forced to start on new lines.
20910   verifyFormat("SomeFunction(\n"
20911                "    []() {\n"
20912                "      //\n"
20913                "    },\n"
20914                "    []() {\n"
20915                "      //\n"
20916                "    });");
20917 
20918   // A lambda passed as arg0 is always pushed to the next line.
20919   verifyFormat("SomeFunction(\n"
20920                "    [this] {\n"
20921                "      //\n"
20922                "    },\n"
20923                "    1);\n");
20924 
20925   // A multi-line lambda passed as arg1 forces arg0 to be pushed out, just like
20926   // the arg0 case above.
20927   auto Style = getGoogleStyle();
20928   Style.BinPackArguments = false;
20929   verifyFormat("SomeFunction(\n"
20930                "    a,\n"
20931                "    [this] {\n"
20932                "      //\n"
20933                "    },\n"
20934                "    b);\n",
20935                Style);
20936   verifyFormat("SomeFunction(\n"
20937                "    a,\n"
20938                "    [this] {\n"
20939                "      //\n"
20940                "    },\n"
20941                "    b);\n");
20942 
20943   // A lambda with a very long line forces arg0 to be pushed out irrespective of
20944   // the BinPackArguments value (as long as the code is wide enough).
20945   verifyFormat(
20946       "something->SomeFunction(\n"
20947       "    a,\n"
20948       "    [this] {\n"
20949       "      "
20950       "D0000000000000000000000000000000000000000000000000000000000001();\n"
20951       "    },\n"
20952       "    b);\n");
20953 
20954   // A multi-line lambda is pulled up as long as the introducer fits on the
20955   // previous line and there are no further args.
20956   verifyFormat("function(1, [this, that] {\n"
20957                "  //\n"
20958                "});\n");
20959   verifyFormat("function([this, that] {\n"
20960                "  //\n"
20961                "});\n");
20962   // FIXME: this format is not ideal and we should consider forcing the first
20963   // arg onto its own line.
20964   verifyFormat("function(a, b, c, //\n"
20965                "         d, [this, that] {\n"
20966                "           //\n"
20967                "         });\n");
20968 
20969   // Multiple lambdas are treated correctly even when there is a short arg0.
20970   verifyFormat("SomeFunction(\n"
20971                "    1,\n"
20972                "    [this] {\n"
20973                "      //\n"
20974                "    },\n"
20975                "    [this] {\n"
20976                "      //\n"
20977                "    },\n"
20978                "    1);\n");
20979 
20980   // More complex introducers.
20981   verifyFormat("return [i, args...] {};");
20982 
20983   // Not lambdas.
20984   verifyFormat("constexpr char hello[]{\"hello\"};");
20985   verifyFormat("double &operator[](int i) { return 0; }\n"
20986                "int i;");
20987   verifyFormat("std::unique_ptr<int[]> foo() {}");
20988   verifyFormat("int i = a[a][a]->f();");
20989   verifyFormat("int i = (*b)[a]->f();");
20990 
20991   // Other corner cases.
20992   verifyFormat("void f() {\n"
20993                "  bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
20994                "  );\n"
20995                "}");
20996 
20997   // Lambdas created through weird macros.
20998   verifyFormat("void f() {\n"
20999                "  MACRO((const AA &a) { return 1; });\n"
21000                "  MACRO((AA &a) { return 1; });\n"
21001                "}");
21002 
21003   verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
21004                "      doo_dah();\n"
21005                "      doo_dah();\n"
21006                "    })) {\n"
21007                "}");
21008   verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n"
21009                "                doo_dah();\n"
21010                "                doo_dah();\n"
21011                "              })) {\n"
21012                "}");
21013   verifyFormat("if CONSTEXPR (blah_blah(whatever, whatever, [] {\n"
21014                "                doo_dah();\n"
21015                "                doo_dah();\n"
21016                "              })) {\n"
21017                "}");
21018   verifyFormat("auto lambda = []() {\n"
21019                "  int a = 2\n"
21020                "#if A\n"
21021                "          + 2\n"
21022                "#endif\n"
21023                "      ;\n"
21024                "};");
21025 
21026   // Lambdas with complex multiline introducers.
21027   verifyFormat(
21028       "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
21029       "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n"
21030       "        -> ::std::unordered_set<\n"
21031       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
21032       "      //\n"
21033       "    });");
21034 
21035   FormatStyle DoNotMerge = getLLVMStyle();
21036   DoNotMerge.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
21037   verifyFormat("auto c = []() {\n"
21038                "  return b;\n"
21039                "};",
21040                "auto c = []() { return b; };", DoNotMerge);
21041   verifyFormat("auto c = []() {\n"
21042                "};",
21043                " auto c = []() {};", DoNotMerge);
21044 
21045   FormatStyle MergeEmptyOnly = getLLVMStyle();
21046   MergeEmptyOnly.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Empty;
21047   verifyFormat("auto c = []() {\n"
21048                "  return b;\n"
21049                "};",
21050                "auto c = []() {\n"
21051                "  return b;\n"
21052                " };",
21053                MergeEmptyOnly);
21054   verifyFormat("auto c = []() {};",
21055                "auto c = []() {\n"
21056                "};",
21057                MergeEmptyOnly);
21058 
21059   FormatStyle MergeInline = getLLVMStyle();
21060   MergeInline.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Inline;
21061   verifyFormat("auto c = []() {\n"
21062                "  return b;\n"
21063                "};",
21064                "auto c = []() { return b; };", MergeInline);
21065   verifyFormat("function([]() { return b; })", "function([]() { return b; })",
21066                MergeInline);
21067   verifyFormat("function([]() { return b; }, a)",
21068                "function([]() { return b; }, a)", MergeInline);
21069   verifyFormat("function(a, []() { return b; })",
21070                "function(a, []() { return b; })", MergeInline);
21071 
21072   // Check option "BraceWrapping.BeforeLambdaBody" and different state of
21073   // AllowShortLambdasOnASingleLine
21074   FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle();
21075   LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
21076   LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
21077   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
21078       FormatStyle::ShortLambdaStyle::SLS_None;
21079   verifyFormat("FctWithOneNestedLambdaInline_SLS_None(\n"
21080                "    []()\n"
21081                "    {\n"
21082                "      return 17;\n"
21083                "    });",
21084                LLVMWithBeforeLambdaBody);
21085   verifyFormat("FctWithOneNestedLambdaEmpty_SLS_None(\n"
21086                "    []()\n"
21087                "    {\n"
21088                "    });",
21089                LLVMWithBeforeLambdaBody);
21090   verifyFormat("auto fct_SLS_None = []()\n"
21091                "{\n"
21092                "  return 17;\n"
21093                "};",
21094                LLVMWithBeforeLambdaBody);
21095   verifyFormat("TwoNestedLambdas_SLS_None(\n"
21096                "    []()\n"
21097                "    {\n"
21098                "      return Call(\n"
21099                "          []()\n"
21100                "          {\n"
21101                "            return 17;\n"
21102                "          });\n"
21103                "    });",
21104                LLVMWithBeforeLambdaBody);
21105   verifyFormat("void Fct() {\n"
21106                "  return {[]()\n"
21107                "          {\n"
21108                "            return 17;\n"
21109                "          }};\n"
21110                "}",
21111                LLVMWithBeforeLambdaBody);
21112 
21113   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
21114       FormatStyle::ShortLambdaStyle::SLS_Empty;
21115   verifyFormat("FctWithOneNestedLambdaInline_SLS_Empty(\n"
21116                "    []()\n"
21117                "    {\n"
21118                "      return 17;\n"
21119                "    });",
21120                LLVMWithBeforeLambdaBody);
21121   verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Empty([]() {});",
21122                LLVMWithBeforeLambdaBody);
21123   verifyFormat("FctWithOneNestedLambdaEmptyInsideAVeryVeryVeryVeryVeryVeryVeryL"
21124                "ongFunctionName_SLS_Empty(\n"
21125                "    []() {});",
21126                LLVMWithBeforeLambdaBody);
21127   verifyFormat("FctWithMultipleParams_SLS_Empty(A, B,\n"
21128                "                                []()\n"
21129                "                                {\n"
21130                "                                  return 17;\n"
21131                "                                });",
21132                LLVMWithBeforeLambdaBody);
21133   verifyFormat("auto fct_SLS_Empty = []()\n"
21134                "{\n"
21135                "  return 17;\n"
21136                "};",
21137                LLVMWithBeforeLambdaBody);
21138   verifyFormat("TwoNestedLambdas_SLS_Empty(\n"
21139                "    []()\n"
21140                "    {\n"
21141                "      return Call([]() {});\n"
21142                "    });",
21143                LLVMWithBeforeLambdaBody);
21144   verifyFormat("TwoNestedLambdas_SLS_Empty(A,\n"
21145                "                           []()\n"
21146                "                           {\n"
21147                "                             return Call([]() {});\n"
21148                "                           });",
21149                LLVMWithBeforeLambdaBody);
21150   verifyFormat(
21151       "FctWithLongLineInLambda_SLS_Empty(\n"
21152       "    []()\n"
21153       "    {\n"
21154       "      return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
21155       "                               AndShouldNotBeConsiderAsInline,\n"
21156       "                               LambdaBodyMustBeBreak);\n"
21157       "    });",
21158       LLVMWithBeforeLambdaBody);
21159 
21160   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
21161       FormatStyle::ShortLambdaStyle::SLS_Inline;
21162   verifyFormat("FctWithOneNestedLambdaInline_SLS_Inline([]() { return 17; });",
21163                LLVMWithBeforeLambdaBody);
21164   verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Inline([]() {});",
21165                LLVMWithBeforeLambdaBody);
21166   verifyFormat("auto fct_SLS_Inline = []()\n"
21167                "{\n"
21168                "  return 17;\n"
21169                "};",
21170                LLVMWithBeforeLambdaBody);
21171   verifyFormat("TwoNestedLambdas_SLS_Inline([]() { return Call([]() { return "
21172                "17; }); });",
21173                LLVMWithBeforeLambdaBody);
21174   verifyFormat(
21175       "FctWithLongLineInLambda_SLS_Inline(\n"
21176       "    []()\n"
21177       "    {\n"
21178       "      return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
21179       "                               AndShouldNotBeConsiderAsInline,\n"
21180       "                               LambdaBodyMustBeBreak);\n"
21181       "    });",
21182       LLVMWithBeforeLambdaBody);
21183   verifyFormat("FctWithMultipleParams_SLS_Inline("
21184                "VeryLongParameterThatShouldAskToBeOnMultiLine,\n"
21185                "                                 []() { return 17; });",
21186                LLVMWithBeforeLambdaBody);
21187   verifyFormat(
21188       "FctWithMultipleParams_SLS_Inline(FirstParam, []() { return 17; });",
21189       LLVMWithBeforeLambdaBody);
21190 
21191   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
21192       FormatStyle::ShortLambdaStyle::SLS_All;
21193   verifyFormat("FctWithOneNestedLambdaInline_SLS_All([]() { return 17; });",
21194                LLVMWithBeforeLambdaBody);
21195   verifyFormat("FctWithOneNestedLambdaEmpty_SLS_All([]() {});",
21196                LLVMWithBeforeLambdaBody);
21197   verifyFormat("auto fct_SLS_All = []() { return 17; };",
21198                LLVMWithBeforeLambdaBody);
21199   verifyFormat("FctWithOneParam_SLS_All(\n"
21200                "    []()\n"
21201                "    {\n"
21202                "      // A cool function...\n"
21203                "      return 43;\n"
21204                "    });",
21205                LLVMWithBeforeLambdaBody);
21206   verifyFormat("FctWithMultipleParams_SLS_All("
21207                "VeryLongParameterThatShouldAskToBeOnMultiLine,\n"
21208                "                              []() { return 17; });",
21209                LLVMWithBeforeLambdaBody);
21210   verifyFormat("FctWithMultipleParams_SLS_All(A, []() { return 17; });",
21211                LLVMWithBeforeLambdaBody);
21212   verifyFormat("FctWithMultipleParams_SLS_All(A, B, []() { return 17; });",
21213                LLVMWithBeforeLambdaBody);
21214   verifyFormat(
21215       "FctWithLongLineInLambda_SLS_All(\n"
21216       "    []()\n"
21217       "    {\n"
21218       "      return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
21219       "                               AndShouldNotBeConsiderAsInline,\n"
21220       "                               LambdaBodyMustBeBreak);\n"
21221       "    });",
21222       LLVMWithBeforeLambdaBody);
21223   verifyFormat(
21224       "auto fct_SLS_All = []()\n"
21225       "{\n"
21226       "  return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
21227       "                           AndShouldNotBeConsiderAsInline,\n"
21228       "                           LambdaBodyMustBeBreak);\n"
21229       "};",
21230       LLVMWithBeforeLambdaBody);
21231   LLVMWithBeforeLambdaBody.BinPackParameters = false;
21232   verifyFormat("FctAllOnSameLine_SLS_All([]() { return S; }, Fst, Second);",
21233                LLVMWithBeforeLambdaBody);
21234   verifyFormat(
21235       "FctWithLongLineInLambda_SLS_All([]() { return SomeValueNotSoLong; },\n"
21236       "                                FirstParam,\n"
21237       "                                SecondParam,\n"
21238       "                                ThirdParam,\n"
21239       "                                FourthParam);",
21240       LLVMWithBeforeLambdaBody);
21241   verifyFormat("FctWithLongLineInLambda_SLS_All(\n"
21242                "    []() { return "
21243                "SomeValueVeryVeryVeryVeryVeryVeryVeryVeryVeryLong; },\n"
21244                "    FirstParam,\n"
21245                "    SecondParam,\n"
21246                "    ThirdParam,\n"
21247                "    FourthParam);",
21248                LLVMWithBeforeLambdaBody);
21249   verifyFormat(
21250       "FctWithLongLineInLambda_SLS_All(FirstParam,\n"
21251       "                                SecondParam,\n"
21252       "                                ThirdParam,\n"
21253       "                                FourthParam,\n"
21254       "                                []() { return SomeValueNotSoLong; });",
21255       LLVMWithBeforeLambdaBody);
21256   verifyFormat("FctWithLongLineInLambda_SLS_All(\n"
21257                "    []()\n"
21258                "    {\n"
21259                "      return "
21260                "HereAVeryLongLineThatWillBeFormattedOnMultipleLineAndShouldNotB"
21261                "eConsiderAsInline;\n"
21262                "    });",
21263                LLVMWithBeforeLambdaBody);
21264   verifyFormat(
21265       "FctWithLongLineInLambda_SLS_All(\n"
21266       "    []()\n"
21267       "    {\n"
21268       "      return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
21269       "                               AndShouldNotBeConsiderAsInline,\n"
21270       "                               LambdaBodyMustBeBreak);\n"
21271       "    });",
21272       LLVMWithBeforeLambdaBody);
21273   verifyFormat("FctWithTwoParams_SLS_All(\n"
21274                "    []()\n"
21275                "    {\n"
21276                "      // A cool function...\n"
21277                "      return 43;\n"
21278                "    },\n"
21279                "    87);",
21280                LLVMWithBeforeLambdaBody);
21281   verifyFormat("FctWithTwoParams_SLS_All([]() { return 43; }, 87);",
21282                LLVMWithBeforeLambdaBody);
21283   verifyFormat("FctWithOneNestedLambdas_SLS_All([]() { return 17; });",
21284                LLVMWithBeforeLambdaBody);
21285   verifyFormat(
21286       "TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; }); });",
21287       LLVMWithBeforeLambdaBody);
21288   verifyFormat("TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; "
21289                "}); }, x);",
21290                LLVMWithBeforeLambdaBody);
21291   verifyFormat("TwoNestedLambdas_SLS_All(\n"
21292                "    []()\n"
21293                "    {\n"
21294                "      // A cool function...\n"
21295                "      return Call([]() { return 17; });\n"
21296                "    });",
21297                LLVMWithBeforeLambdaBody);
21298   verifyFormat("TwoNestedLambdas_SLS_All(\n"
21299                "    []()\n"
21300                "    {\n"
21301                "      return Call(\n"
21302                "          []()\n"
21303                "          {\n"
21304                "            // A cool function...\n"
21305                "            return 17;\n"
21306                "          });\n"
21307                "    });",
21308                LLVMWithBeforeLambdaBody);
21309 
21310   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
21311       FormatStyle::ShortLambdaStyle::SLS_None;
21312 
21313   verifyFormat("auto select = [this]() -> const Library::Object *\n"
21314                "{\n"
21315                "  return MyAssignment::SelectFromList(this);\n"
21316                "};\n",
21317                LLVMWithBeforeLambdaBody);
21318 
21319   verifyFormat("auto select = [this]() -> const Library::Object &\n"
21320                "{\n"
21321                "  return MyAssignment::SelectFromList(this);\n"
21322                "};\n",
21323                LLVMWithBeforeLambdaBody);
21324 
21325   verifyFormat("auto select = [this]() -> std::unique_ptr<Object>\n"
21326                "{\n"
21327                "  return MyAssignment::SelectFromList(this);\n"
21328                "};\n",
21329                LLVMWithBeforeLambdaBody);
21330 
21331   verifyFormat("namespace test {\n"
21332                "class Test {\n"
21333                "public:\n"
21334                "  Test() = default;\n"
21335                "};\n"
21336                "} // namespace test",
21337                LLVMWithBeforeLambdaBody);
21338 
21339   // Lambdas with different indentation styles.
21340   Style = getLLVMStyleWithColumns(100);
21341   EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n"
21342             "  return promise.then(\n"
21343             "      [this, &someVariable, someObject = "
21344             "std::mv(s)](std::vector<int> evaluated) mutable {\n"
21345             "        return someObject.startAsyncAction().then(\n"
21346             "            [this, &someVariable](AsyncActionResult result) "
21347             "mutable { result.processMore(); });\n"
21348             "      });\n"
21349             "}\n",
21350             format("SomeResult doSomething(SomeObject promise) {\n"
21351                    "  return promise.then([this, &someVariable, someObject = "
21352                    "std::mv(s)](std::vector<int> evaluated) mutable {\n"
21353                    "    return someObject.startAsyncAction().then([this, "
21354                    "&someVariable](AsyncActionResult result) mutable {\n"
21355                    "      result.processMore();\n"
21356                    "    });\n"
21357                    "  });\n"
21358                    "}\n",
21359                    Style));
21360   Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope;
21361   verifyFormat("test() {\n"
21362                "  ([]() -> {\n"
21363                "    int b = 32;\n"
21364                "    return 3;\n"
21365                "  }).foo();\n"
21366                "}",
21367                Style);
21368   verifyFormat("test() {\n"
21369                "  []() -> {\n"
21370                "    int b = 32;\n"
21371                "    return 3;\n"
21372                "  }\n"
21373                "}",
21374                Style);
21375   verifyFormat("std::sort(v.begin(), v.end(),\n"
21376                "          [](const auto &someLongArgumentName, const auto "
21377                "&someOtherLongArgumentName) {\n"
21378                "  return someLongArgumentName.someMemberVariable < "
21379                "someOtherLongArgumentName.someMemberVariable;\n"
21380                "});",
21381                Style);
21382   verifyFormat("test() {\n"
21383                "  (\n"
21384                "      []() -> {\n"
21385                "        int b = 32;\n"
21386                "        return 3;\n"
21387                "      },\n"
21388                "      foo, bar)\n"
21389                "      .foo();\n"
21390                "}",
21391                Style);
21392   verifyFormat("test() {\n"
21393                "  ([]() -> {\n"
21394                "    int b = 32;\n"
21395                "    return 3;\n"
21396                "  })\n"
21397                "      .foo()\n"
21398                "      .bar();\n"
21399                "}",
21400                Style);
21401   EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n"
21402             "  return promise.then(\n"
21403             "      [this, &someVariable, someObject = "
21404             "std::mv(s)](std::vector<int> evaluated) mutable {\n"
21405             "    return someObject.startAsyncAction().then(\n"
21406             "        [this, &someVariable](AsyncActionResult result) mutable { "
21407             "result.processMore(); });\n"
21408             "  });\n"
21409             "}\n",
21410             format("SomeResult doSomething(SomeObject promise) {\n"
21411                    "  return promise.then([this, &someVariable, someObject = "
21412                    "std::mv(s)](std::vector<int> evaluated) mutable {\n"
21413                    "    return someObject.startAsyncAction().then([this, "
21414                    "&someVariable](AsyncActionResult result) mutable {\n"
21415                    "      result.processMore();\n"
21416                    "    });\n"
21417                    "  });\n"
21418                    "}\n",
21419                    Style));
21420   EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n"
21421             "  return promise.then([this, &someVariable] {\n"
21422             "    return someObject.startAsyncAction().then(\n"
21423             "        [this, &someVariable](AsyncActionResult result) mutable { "
21424             "result.processMore(); });\n"
21425             "  });\n"
21426             "}\n",
21427             format("SomeResult doSomething(SomeObject promise) {\n"
21428                    "  return promise.then([this, &someVariable] {\n"
21429                    "    return someObject.startAsyncAction().then([this, "
21430                    "&someVariable](AsyncActionResult result) mutable {\n"
21431                    "      result.processMore();\n"
21432                    "    });\n"
21433                    "  });\n"
21434                    "}\n",
21435                    Style));
21436   Style = getGoogleStyle();
21437   Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope;
21438   EXPECT_EQ("#define A                                       \\\n"
21439             "  [] {                                          \\\n"
21440             "    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(        \\\n"
21441             "        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
21442             "      }",
21443             format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
21444                    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
21445                    Style));
21446   // TODO: The current formatting has a minor issue that's not worth fixing
21447   // right now whereby the closing brace is indented relative to the signature
21448   // instead of being aligned. This only happens with macros.
21449 }
21450 
21451 TEST_F(FormatTest, LambdaWithLineComments) {
21452   FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle();
21453   LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
21454   LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
21455   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
21456       FormatStyle::ShortLambdaStyle::SLS_All;
21457 
21458   verifyFormat("auto k = []() { return; }", LLVMWithBeforeLambdaBody);
21459   verifyFormat("auto k = []() // comment\n"
21460                "{ return; }",
21461                LLVMWithBeforeLambdaBody);
21462   verifyFormat("auto k = []() /* comment */ { return; }",
21463                LLVMWithBeforeLambdaBody);
21464   verifyFormat("auto k = []() /* comment */ /* comment */ { return; }",
21465                LLVMWithBeforeLambdaBody);
21466   verifyFormat("auto k = []() // X\n"
21467                "{ return; }",
21468                LLVMWithBeforeLambdaBody);
21469   verifyFormat(
21470       "auto k = []() // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"
21471       "{ return; }",
21472       LLVMWithBeforeLambdaBody);
21473 }
21474 
21475 TEST_F(FormatTest, EmptyLinesInLambdas) {
21476   verifyFormat("auto lambda = []() {\n"
21477                "  x(); //\n"
21478                "};",
21479                "auto lambda = []() {\n"
21480                "\n"
21481                "  x(); //\n"
21482                "\n"
21483                "};");
21484 }
21485 
21486 TEST_F(FormatTest, FormatsBlocks) {
21487   FormatStyle ShortBlocks = getLLVMStyle();
21488   ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
21489   verifyFormat("int (^Block)(int, int);", ShortBlocks);
21490   verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
21491   verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
21492   verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks);
21493   verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks);
21494   verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks);
21495 
21496   verifyFormat("foo(^{ bar(); });", ShortBlocks);
21497   verifyFormat("foo(a, ^{ bar(); });", ShortBlocks);
21498   verifyFormat("{ void (^block)(Object *x); }", ShortBlocks);
21499 
21500   verifyFormat("[operation setCompletionBlock:^{\n"
21501                "  [self onOperationDone];\n"
21502                "}];");
21503   verifyFormat("int i = {[operation setCompletionBlock:^{\n"
21504                "  [self onOperationDone];\n"
21505                "}]};");
21506   verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
21507                "  f();\n"
21508                "}];");
21509   verifyFormat("int a = [operation block:^int(int *i) {\n"
21510                "  return 1;\n"
21511                "}];");
21512   verifyFormat("[myObject doSomethingWith:arg1\n"
21513                "                      aaa:^int(int *a) {\n"
21514                "                        return 1;\n"
21515                "                      }\n"
21516                "                      bbb:f(a * bbbbbbbb)];");
21517 
21518   verifyFormat("[operation setCompletionBlock:^{\n"
21519                "  [self.delegate newDataAvailable];\n"
21520                "}];",
21521                getLLVMStyleWithColumns(60));
21522   verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
21523                "  NSString *path = [self sessionFilePath];\n"
21524                "  if (path) {\n"
21525                "    // ...\n"
21526                "  }\n"
21527                "});");
21528   verifyFormat("[[SessionService sharedService]\n"
21529                "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
21530                "      if (window) {\n"
21531                "        [self windowDidLoad:window];\n"
21532                "      } else {\n"
21533                "        [self errorLoadingWindow];\n"
21534                "      }\n"
21535                "    }];");
21536   verifyFormat("void (^largeBlock)(void) = ^{\n"
21537                "  // ...\n"
21538                "};\n",
21539                getLLVMStyleWithColumns(40));
21540   verifyFormat("[[SessionService sharedService]\n"
21541                "    loadWindowWithCompletionBlock: //\n"
21542                "        ^(SessionWindow *window) {\n"
21543                "          if (window) {\n"
21544                "            [self windowDidLoad:window];\n"
21545                "          } else {\n"
21546                "            [self errorLoadingWindow];\n"
21547                "          }\n"
21548                "        }];",
21549                getLLVMStyleWithColumns(60));
21550   verifyFormat("[myObject doSomethingWith:arg1\n"
21551                "    firstBlock:^(Foo *a) {\n"
21552                "      // ...\n"
21553                "      int i;\n"
21554                "    }\n"
21555                "    secondBlock:^(Bar *b) {\n"
21556                "      // ...\n"
21557                "      int i;\n"
21558                "    }\n"
21559                "    thirdBlock:^Foo(Bar *b) {\n"
21560                "      // ...\n"
21561                "      int i;\n"
21562                "    }];");
21563   verifyFormat("[myObject doSomethingWith:arg1\n"
21564                "               firstBlock:-1\n"
21565                "              secondBlock:^(Bar *b) {\n"
21566                "                // ...\n"
21567                "                int i;\n"
21568                "              }];");
21569 
21570   verifyFormat("f(^{\n"
21571                "  @autoreleasepool {\n"
21572                "    if (a) {\n"
21573                "      g();\n"
21574                "    }\n"
21575                "  }\n"
21576                "});");
21577   verifyFormat("Block b = ^int *(A *a, B *b) {}");
21578   verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n"
21579                "};");
21580 
21581   FormatStyle FourIndent = getLLVMStyle();
21582   FourIndent.ObjCBlockIndentWidth = 4;
21583   verifyFormat("[operation setCompletionBlock:^{\n"
21584                "    [self onOperationDone];\n"
21585                "}];",
21586                FourIndent);
21587 }
21588 
21589 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) {
21590   FormatStyle ZeroColumn = getLLVMStyleWithColumns(0);
21591 
21592   verifyFormat("[[SessionService sharedService] "
21593                "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
21594                "  if (window) {\n"
21595                "    [self windowDidLoad:window];\n"
21596                "  } else {\n"
21597                "    [self errorLoadingWindow];\n"
21598                "  }\n"
21599                "}];",
21600                ZeroColumn);
21601   EXPECT_EQ("[[SessionService sharedService]\n"
21602             "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
21603             "      if (window) {\n"
21604             "        [self windowDidLoad:window];\n"
21605             "      } else {\n"
21606             "        [self errorLoadingWindow];\n"
21607             "      }\n"
21608             "    }];",
21609             format("[[SessionService sharedService]\n"
21610                    "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
21611                    "                if (window) {\n"
21612                    "    [self windowDidLoad:window];\n"
21613                    "  } else {\n"
21614                    "    [self errorLoadingWindow];\n"
21615                    "  }\n"
21616                    "}];",
21617                    ZeroColumn));
21618   verifyFormat("[myObject doSomethingWith:arg1\n"
21619                "    firstBlock:^(Foo *a) {\n"
21620                "      // ...\n"
21621                "      int i;\n"
21622                "    }\n"
21623                "    secondBlock:^(Bar *b) {\n"
21624                "      // ...\n"
21625                "      int i;\n"
21626                "    }\n"
21627                "    thirdBlock:^Foo(Bar *b) {\n"
21628                "      // ...\n"
21629                "      int i;\n"
21630                "    }];",
21631                ZeroColumn);
21632   verifyFormat("f(^{\n"
21633                "  @autoreleasepool {\n"
21634                "    if (a) {\n"
21635                "      g();\n"
21636                "    }\n"
21637                "  }\n"
21638                "});",
21639                ZeroColumn);
21640   verifyFormat("void (^largeBlock)(void) = ^{\n"
21641                "  // ...\n"
21642                "};",
21643                ZeroColumn);
21644 
21645   ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
21646   EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };",
21647             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
21648   ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
21649   EXPECT_EQ("void (^largeBlock)(void) = ^{\n"
21650             "  int i;\n"
21651             "};",
21652             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
21653 }
21654 
21655 TEST_F(FormatTest, SupportsCRLF) {
21656   EXPECT_EQ("int a;\r\n"
21657             "int b;\r\n"
21658             "int c;\r\n",
21659             format("int a;\r\n"
21660                    "  int b;\r\n"
21661                    "    int c;\r\n",
21662                    getLLVMStyle()));
21663   EXPECT_EQ("int a;\r\n"
21664             "int b;\r\n"
21665             "int c;\r\n",
21666             format("int a;\r\n"
21667                    "  int b;\n"
21668                    "    int c;\r\n",
21669                    getLLVMStyle()));
21670   EXPECT_EQ("int a;\n"
21671             "int b;\n"
21672             "int c;\n",
21673             format("int a;\r\n"
21674                    "  int b;\n"
21675                    "    int c;\n",
21676                    getLLVMStyle()));
21677   EXPECT_EQ("\"aaaaaaa \"\r\n"
21678             "\"bbbbbbb\";\r\n",
21679             format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
21680   EXPECT_EQ("#define A \\\r\n"
21681             "  b;      \\\r\n"
21682             "  c;      \\\r\n"
21683             "  d;\r\n",
21684             format("#define A \\\r\n"
21685                    "  b; \\\r\n"
21686                    "  c; d; \r\n",
21687                    getGoogleStyle()));
21688 
21689   EXPECT_EQ("/*\r\n"
21690             "multi line block comments\r\n"
21691             "should not introduce\r\n"
21692             "an extra carriage return\r\n"
21693             "*/\r\n",
21694             format("/*\r\n"
21695                    "multi line block comments\r\n"
21696                    "should not introduce\r\n"
21697                    "an extra carriage return\r\n"
21698                    "*/\r\n"));
21699   EXPECT_EQ("/*\r\n"
21700             "\r\n"
21701             "*/",
21702             format("/*\r\n"
21703                    "    \r\r\r\n"
21704                    "*/"));
21705 
21706   FormatStyle style = getLLVMStyle();
21707 
21708   style.DeriveLineEnding = true;
21709   style.UseCRLF = false;
21710   EXPECT_EQ("union FooBarBazQux {\n"
21711             "  int foo;\n"
21712             "  int bar;\n"
21713             "  int baz;\n"
21714             "};",
21715             format("union FooBarBazQux {\r\n"
21716                    "  int foo;\n"
21717                    "  int bar;\r\n"
21718                    "  int baz;\n"
21719                    "};",
21720                    style));
21721   style.UseCRLF = true;
21722   EXPECT_EQ("union FooBarBazQux {\r\n"
21723             "  int foo;\r\n"
21724             "  int bar;\r\n"
21725             "  int baz;\r\n"
21726             "};",
21727             format("union FooBarBazQux {\r\n"
21728                    "  int foo;\n"
21729                    "  int bar;\r\n"
21730                    "  int baz;\n"
21731                    "};",
21732                    style));
21733 
21734   style.DeriveLineEnding = false;
21735   style.UseCRLF = false;
21736   EXPECT_EQ("union FooBarBazQux {\n"
21737             "  int foo;\n"
21738             "  int bar;\n"
21739             "  int baz;\n"
21740             "  int qux;\n"
21741             "};",
21742             format("union FooBarBazQux {\r\n"
21743                    "  int foo;\n"
21744                    "  int bar;\r\n"
21745                    "  int baz;\n"
21746                    "  int qux;\r\n"
21747                    "};",
21748                    style));
21749   style.UseCRLF = true;
21750   EXPECT_EQ("union FooBarBazQux {\r\n"
21751             "  int foo;\r\n"
21752             "  int bar;\r\n"
21753             "  int baz;\r\n"
21754             "  int qux;\r\n"
21755             "};",
21756             format("union FooBarBazQux {\r\n"
21757                    "  int foo;\n"
21758                    "  int bar;\r\n"
21759                    "  int baz;\n"
21760                    "  int qux;\n"
21761                    "};",
21762                    style));
21763 
21764   style.DeriveLineEnding = true;
21765   style.UseCRLF = false;
21766   EXPECT_EQ("union FooBarBazQux {\r\n"
21767             "  int foo;\r\n"
21768             "  int bar;\r\n"
21769             "  int baz;\r\n"
21770             "  int qux;\r\n"
21771             "};",
21772             format("union FooBarBazQux {\r\n"
21773                    "  int foo;\n"
21774                    "  int bar;\r\n"
21775                    "  int baz;\n"
21776                    "  int qux;\r\n"
21777                    "};",
21778                    style));
21779   style.UseCRLF = true;
21780   EXPECT_EQ("union FooBarBazQux {\n"
21781             "  int foo;\n"
21782             "  int bar;\n"
21783             "  int baz;\n"
21784             "  int qux;\n"
21785             "};",
21786             format("union FooBarBazQux {\r\n"
21787                    "  int foo;\n"
21788                    "  int bar;\r\n"
21789                    "  int baz;\n"
21790                    "  int qux;\n"
21791                    "};",
21792                    style));
21793 }
21794 
21795 TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
21796   verifyFormat("MY_CLASS(C) {\n"
21797                "  int i;\n"
21798                "  int j;\n"
21799                "};");
21800 }
21801 
21802 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
21803   FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
21804   TwoIndent.ContinuationIndentWidth = 2;
21805 
21806   EXPECT_EQ("int i =\n"
21807             "  longFunction(\n"
21808             "    arg);",
21809             format("int i = longFunction(arg);", TwoIndent));
21810 
21811   FormatStyle SixIndent = getLLVMStyleWithColumns(20);
21812   SixIndent.ContinuationIndentWidth = 6;
21813 
21814   EXPECT_EQ("int i =\n"
21815             "      longFunction(\n"
21816             "            arg);",
21817             format("int i = longFunction(arg);", SixIndent));
21818 }
21819 
21820 TEST_F(FormatTest, WrappedClosingParenthesisIndent) {
21821   FormatStyle Style = getLLVMStyle();
21822   verifyFormat("int Foo::getter(\n"
21823                "    //\n"
21824                ") const {\n"
21825                "  return foo;\n"
21826                "}",
21827                Style);
21828   verifyFormat("void Foo::setter(\n"
21829                "    //\n"
21830                ") {\n"
21831                "  foo = 1;\n"
21832                "}",
21833                Style);
21834 }
21835 
21836 TEST_F(FormatTest, SpacesInAngles) {
21837   FormatStyle Spaces = getLLVMStyle();
21838   Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
21839 
21840   verifyFormat("vector< ::std::string > x1;", Spaces);
21841   verifyFormat("Foo< int, Bar > x2;", Spaces);
21842   verifyFormat("Foo< ::int, ::Bar > x3;", Spaces);
21843 
21844   verifyFormat("static_cast< int >(arg);", Spaces);
21845   verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
21846   verifyFormat("f< int, float >();", Spaces);
21847   verifyFormat("template <> g() {}", Spaces);
21848   verifyFormat("template < std::vector< int > > f() {}", Spaces);
21849   verifyFormat("std::function< void(int, int) > fct;", Spaces);
21850   verifyFormat("void inFunction() { std::function< void(int, int) > fct; }",
21851                Spaces);
21852 
21853   Spaces.Standard = FormatStyle::LS_Cpp03;
21854   Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
21855   verifyFormat("A< A< int > >();", Spaces);
21856 
21857   Spaces.SpacesInAngles = FormatStyle::SIAS_Never;
21858   verifyFormat("A<A<int> >();", Spaces);
21859 
21860   Spaces.SpacesInAngles = FormatStyle::SIAS_Leave;
21861   verifyFormat("vector< ::std::string> x4;", "vector<::std::string> x4;",
21862                Spaces);
21863   verifyFormat("vector< ::std::string > x4;", "vector<::std::string > x4;",
21864                Spaces);
21865 
21866   verifyFormat("A<A<int> >();", Spaces);
21867   verifyFormat("A<A<int> >();", "A<A<int>>();", Spaces);
21868   verifyFormat("A< A< int > >();", Spaces);
21869 
21870   Spaces.Standard = FormatStyle::LS_Cpp11;
21871   Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
21872   verifyFormat("A< A< int > >();", Spaces);
21873 
21874   Spaces.SpacesInAngles = FormatStyle::SIAS_Never;
21875   verifyFormat("vector<::std::string> x4;", Spaces);
21876   verifyFormat("vector<int> x5;", Spaces);
21877   verifyFormat("Foo<int, Bar> x6;", Spaces);
21878   verifyFormat("Foo<::int, ::Bar> x7;", Spaces);
21879 
21880   verifyFormat("A<A<int>>();", Spaces);
21881 
21882   Spaces.SpacesInAngles = FormatStyle::SIAS_Leave;
21883   verifyFormat("vector<::std::string> x4;", Spaces);
21884   verifyFormat("vector< ::std::string > x4;", Spaces);
21885   verifyFormat("vector<int> x5;", Spaces);
21886   verifyFormat("vector< int > x5;", Spaces);
21887   verifyFormat("Foo<int, Bar> x6;", Spaces);
21888   verifyFormat("Foo< int, Bar > x6;", Spaces);
21889   verifyFormat("Foo<::int, ::Bar> x7;", Spaces);
21890   verifyFormat("Foo< ::int, ::Bar > x7;", Spaces);
21891 
21892   verifyFormat("A<A<int>>();", Spaces);
21893   verifyFormat("A< A< int > >();", Spaces);
21894   verifyFormat("A<A<int > >();", Spaces);
21895   verifyFormat("A< A< int>>();", Spaces);
21896 
21897   Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
21898   verifyFormat("// clang-format off\n"
21899                "foo<<<1, 1>>>();\n"
21900                "// clang-format on\n",
21901                Spaces);
21902   verifyFormat("// clang-format off\n"
21903                "foo< < <1, 1> > >();\n"
21904                "// clang-format on\n",
21905                Spaces);
21906 }
21907 
21908 TEST_F(FormatTest, SpaceAfterTemplateKeyword) {
21909   FormatStyle Style = getLLVMStyle();
21910   Style.SpaceAfterTemplateKeyword = false;
21911   verifyFormat("template<int> void foo();", Style);
21912 }
21913 
21914 TEST_F(FormatTest, TripleAngleBrackets) {
21915   verifyFormat("f<<<1, 1>>>();");
21916   verifyFormat("f<<<1, 1, 1, s>>>();");
21917   verifyFormat("f<<<a, b, c, d>>>();");
21918   EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();"));
21919   verifyFormat("f<param><<<1, 1>>>();");
21920   verifyFormat("f<1><<<1, 1>>>();");
21921   EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();"));
21922   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
21923                "aaaaaaaaaaa<<<\n    1, 1>>>();");
21924   verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n"
21925                "    <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();");
21926 }
21927 
21928 TEST_F(FormatTest, MergeLessLessAtEnd) {
21929   verifyFormat("<<");
21930   EXPECT_EQ("< < <", format("\\\n<<<"));
21931   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
21932                "aaallvm::outs() <<");
21933   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
21934                "aaaallvm::outs()\n    <<");
21935 }
21936 
21937 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
21938   std::string code = "#if A\n"
21939                      "#if B\n"
21940                      "a.\n"
21941                      "#endif\n"
21942                      "    a = 1;\n"
21943                      "#else\n"
21944                      "#endif\n"
21945                      "#if C\n"
21946                      "#else\n"
21947                      "#endif\n";
21948   EXPECT_EQ(code, format(code));
21949 }
21950 
21951 TEST_F(FormatTest, HandleConflictMarkers) {
21952   // Git/SVN conflict markers.
21953   EXPECT_EQ("int a;\n"
21954             "void f() {\n"
21955             "  callme(some(parameter1,\n"
21956             "<<<<<<< text by the vcs\n"
21957             "              parameter2),\n"
21958             "||||||| text by the vcs\n"
21959             "              parameter2),\n"
21960             "         parameter3,\n"
21961             "======= text by the vcs\n"
21962             "              parameter2, parameter3),\n"
21963             ">>>>>>> text by the vcs\n"
21964             "         otherparameter);\n",
21965             format("int a;\n"
21966                    "void f() {\n"
21967                    "  callme(some(parameter1,\n"
21968                    "<<<<<<< text by the vcs\n"
21969                    "  parameter2),\n"
21970                    "||||||| text by the vcs\n"
21971                    "  parameter2),\n"
21972                    "  parameter3,\n"
21973                    "======= text by the vcs\n"
21974                    "  parameter2,\n"
21975                    "  parameter3),\n"
21976                    ">>>>>>> text by the vcs\n"
21977                    "  otherparameter);\n"));
21978 
21979   // Perforce markers.
21980   EXPECT_EQ("void f() {\n"
21981             "  function(\n"
21982             ">>>> text by the vcs\n"
21983             "      parameter,\n"
21984             "==== text by the vcs\n"
21985             "      parameter,\n"
21986             "==== text by the vcs\n"
21987             "      parameter,\n"
21988             "<<<< text by the vcs\n"
21989             "      parameter);\n",
21990             format("void f() {\n"
21991                    "  function(\n"
21992                    ">>>> text by the vcs\n"
21993                    "  parameter,\n"
21994                    "==== text by the vcs\n"
21995                    "  parameter,\n"
21996                    "==== text by the vcs\n"
21997                    "  parameter,\n"
21998                    "<<<< text by the vcs\n"
21999                    "  parameter);\n"));
22000 
22001   EXPECT_EQ("<<<<<<<\n"
22002             "|||||||\n"
22003             "=======\n"
22004             ">>>>>>>",
22005             format("<<<<<<<\n"
22006                    "|||||||\n"
22007                    "=======\n"
22008                    ">>>>>>>"));
22009 
22010   EXPECT_EQ("<<<<<<<\n"
22011             "|||||||\n"
22012             "int i;\n"
22013             "=======\n"
22014             ">>>>>>>",
22015             format("<<<<<<<\n"
22016                    "|||||||\n"
22017                    "int i;\n"
22018                    "=======\n"
22019                    ">>>>>>>"));
22020 
22021   // FIXME: Handle parsing of macros around conflict markers correctly:
22022   EXPECT_EQ("#define Macro \\\n"
22023             "<<<<<<<\n"
22024             "Something \\\n"
22025             "|||||||\n"
22026             "Else \\\n"
22027             "=======\n"
22028             "Other \\\n"
22029             ">>>>>>>\n"
22030             "    End int i;\n",
22031             format("#define Macro \\\n"
22032                    "<<<<<<<\n"
22033                    "  Something \\\n"
22034                    "|||||||\n"
22035                    "  Else \\\n"
22036                    "=======\n"
22037                    "  Other \\\n"
22038                    ">>>>>>>\n"
22039                    "  End\n"
22040                    "int i;\n"));
22041 
22042   verifyFormat(R"(====
22043 #ifdef A
22044 a
22045 #else
22046 b
22047 #endif
22048 )");
22049 }
22050 
22051 TEST_F(FormatTest, DisableRegions) {
22052   EXPECT_EQ("int i;\n"
22053             "// clang-format off\n"
22054             "  int j;\n"
22055             "// clang-format on\n"
22056             "int k;",
22057             format(" int  i;\n"
22058                    "   // clang-format off\n"
22059                    "  int j;\n"
22060                    " // clang-format on\n"
22061                    "   int   k;"));
22062   EXPECT_EQ("int i;\n"
22063             "/* clang-format off */\n"
22064             "  int j;\n"
22065             "/* clang-format on */\n"
22066             "int k;",
22067             format(" int  i;\n"
22068                    "   /* clang-format off */\n"
22069                    "  int j;\n"
22070                    " /* clang-format on */\n"
22071                    "   int   k;"));
22072 
22073   // Don't reflow comments within disabled regions.
22074   EXPECT_EQ("// clang-format off\n"
22075             "// long long long long long long line\n"
22076             "/* clang-format on */\n"
22077             "/* long long long\n"
22078             " * long long long\n"
22079             " * line */\n"
22080             "int i;\n"
22081             "/* clang-format off */\n"
22082             "/* long long long long long long line */\n",
22083             format("// clang-format off\n"
22084                    "// long long long long long long line\n"
22085                    "/* clang-format on */\n"
22086                    "/* long long long long long long line */\n"
22087                    "int i;\n"
22088                    "/* clang-format off */\n"
22089                    "/* long long long long long long line */\n",
22090                    getLLVMStyleWithColumns(20)));
22091 }
22092 
22093 TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
22094   format("? ) =");
22095   verifyNoCrash("#define a\\\n /**/}");
22096 }
22097 
22098 TEST_F(FormatTest, FormatsTableGenCode) {
22099   FormatStyle Style = getLLVMStyle();
22100   Style.Language = FormatStyle::LK_TableGen;
22101   verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style);
22102 }
22103 
22104 TEST_F(FormatTest, ArrayOfTemplates) {
22105   EXPECT_EQ("auto a = new unique_ptr<int>[10];",
22106             format("auto a = new unique_ptr<int > [ 10];"));
22107 
22108   FormatStyle Spaces = getLLVMStyle();
22109   Spaces.SpacesInSquareBrackets = true;
22110   EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];",
22111             format("auto a = new unique_ptr<int > [10];", Spaces));
22112 }
22113 
22114 TEST_F(FormatTest, ArrayAsTemplateType) {
22115   EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;",
22116             format("auto a = unique_ptr < Foo < Bar>[ 10]> ;"));
22117 
22118   FormatStyle Spaces = getLLVMStyle();
22119   Spaces.SpacesInSquareBrackets = true;
22120   EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;",
22121             format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces));
22122 }
22123 
22124 TEST_F(FormatTest, NoSpaceAfterSuper) { verifyFormat("__super::FooBar();"); }
22125 
22126 TEST(FormatStyle, GetStyleWithEmptyFileName) {
22127   llvm::vfs::InMemoryFileSystem FS;
22128   auto Style1 = getStyle("file", "", "Google", "", &FS);
22129   ASSERT_TRUE((bool)Style1);
22130   ASSERT_EQ(*Style1, getGoogleStyle());
22131 }
22132 
22133 TEST(FormatStyle, GetStyleOfFile) {
22134   llvm::vfs::InMemoryFileSystem FS;
22135   // Test 1: format file in the same directory.
22136   ASSERT_TRUE(
22137       FS.addFile("/a/.clang-format", 0,
22138                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM")));
22139   ASSERT_TRUE(
22140       FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
22141   auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS);
22142   ASSERT_TRUE((bool)Style1);
22143   ASSERT_EQ(*Style1, getLLVMStyle());
22144 
22145   // Test 2.1: fallback to default.
22146   ASSERT_TRUE(
22147       FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
22148   auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS);
22149   ASSERT_TRUE((bool)Style2);
22150   ASSERT_EQ(*Style2, getMozillaStyle());
22151 
22152   // Test 2.2: no format on 'none' fallback style.
22153   Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
22154   ASSERT_TRUE((bool)Style2);
22155   ASSERT_EQ(*Style2, getNoStyle());
22156 
22157   // Test 2.3: format if config is found with no based style while fallback is
22158   // 'none'.
22159   ASSERT_TRUE(FS.addFile("/b/.clang-format", 0,
22160                          llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2")));
22161   Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
22162   ASSERT_TRUE((bool)Style2);
22163   ASSERT_EQ(*Style2, getLLVMStyle());
22164 
22165   // Test 2.4: format if yaml with no based style, while fallback is 'none'.
22166   Style2 = getStyle("{}", "a.h", "none", "", &FS);
22167   ASSERT_TRUE((bool)Style2);
22168   ASSERT_EQ(*Style2, getLLVMStyle());
22169 
22170   // Test 3: format file in parent directory.
22171   ASSERT_TRUE(
22172       FS.addFile("/c/.clang-format", 0,
22173                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google")));
22174   ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0,
22175                          llvm::MemoryBuffer::getMemBuffer("int i;")));
22176   auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS);
22177   ASSERT_TRUE((bool)Style3);
22178   ASSERT_EQ(*Style3, getGoogleStyle());
22179 
22180   // Test 4: error on invalid fallback style
22181   auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS);
22182   ASSERT_FALSE((bool)Style4);
22183   llvm::consumeError(Style4.takeError());
22184 
22185   // Test 5: error on invalid yaml on command line
22186   auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS);
22187   ASSERT_FALSE((bool)Style5);
22188   llvm::consumeError(Style5.takeError());
22189 
22190   // Test 6: error on invalid style
22191   auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS);
22192   ASSERT_FALSE((bool)Style6);
22193   llvm::consumeError(Style6.takeError());
22194 
22195   // Test 7: found config file, error on parsing it
22196   ASSERT_TRUE(
22197       FS.addFile("/d/.clang-format", 0,
22198                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n"
22199                                                   "InvalidKey: InvalidValue")));
22200   ASSERT_TRUE(
22201       FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
22202   auto Style7a = getStyle("file", "/d/.clang-format", "LLVM", "", &FS);
22203   ASSERT_FALSE((bool)Style7a);
22204   llvm::consumeError(Style7a.takeError());
22205 
22206   auto Style7b = getStyle("file", "/d/.clang-format", "LLVM", "", &FS, true);
22207   ASSERT_TRUE((bool)Style7b);
22208 
22209   // Test 8: inferred per-language defaults apply.
22210   auto StyleTd = getStyle("file", "x.td", "llvm", "", &FS);
22211   ASSERT_TRUE((bool)StyleTd);
22212   ASSERT_EQ(*StyleTd, getLLVMStyle(FormatStyle::LK_TableGen));
22213 
22214   // Test 9.1.1: overwriting a file style, when no parent file exists with no
22215   // fallback style.
22216   ASSERT_TRUE(FS.addFile(
22217       "/e/sub/.clang-format", 0,
22218       llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: InheritParentConfig\n"
22219                                        "ColumnLimit: 20")));
22220   ASSERT_TRUE(FS.addFile("/e/sub/code.cpp", 0,
22221                          llvm::MemoryBuffer::getMemBuffer("int i;")));
22222   auto Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS);
22223   ASSERT_TRUE(static_cast<bool>(Style9));
22224   ASSERT_EQ(*Style9, [] {
22225     auto Style = getNoStyle();
22226     Style.ColumnLimit = 20;
22227     return Style;
22228   }());
22229 
22230   // Test 9.1.2: propagate more than one level with no parent file.
22231   ASSERT_TRUE(FS.addFile("/e/sub/sub/code.cpp", 0,
22232                          llvm::MemoryBuffer::getMemBuffer("int i;")));
22233   ASSERT_TRUE(FS.addFile("/e/sub/sub/.clang-format", 0,
22234                          llvm::MemoryBuffer::getMemBuffer(
22235                              "BasedOnStyle: InheritParentConfig\n"
22236                              "WhitespaceSensitiveMacros: ['FOO', 'BAR']")));
22237   std::vector<std::string> NonDefaultWhiteSpaceMacros{"FOO", "BAR"};
22238 
22239   ASSERT_NE(Style9->WhitespaceSensitiveMacros, NonDefaultWhiteSpaceMacros);
22240   Style9 = getStyle("file", "/e/sub/sub/code.cpp", "none", "", &FS);
22241   ASSERT_TRUE(static_cast<bool>(Style9));
22242   ASSERT_EQ(*Style9, [&NonDefaultWhiteSpaceMacros] {
22243     auto Style = getNoStyle();
22244     Style.ColumnLimit = 20;
22245     Style.WhitespaceSensitiveMacros = NonDefaultWhiteSpaceMacros;
22246     return Style;
22247   }());
22248 
22249   // Test 9.2: with LLVM fallback style
22250   Style9 = getStyle("file", "/e/sub/code.cpp", "LLVM", "", &FS);
22251   ASSERT_TRUE(static_cast<bool>(Style9));
22252   ASSERT_EQ(*Style9, [] {
22253     auto Style = getLLVMStyle();
22254     Style.ColumnLimit = 20;
22255     return Style;
22256   }());
22257 
22258   // Test 9.3: with a parent file
22259   ASSERT_TRUE(
22260       FS.addFile("/e/.clang-format", 0,
22261                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google\n"
22262                                                   "UseTab: Always")));
22263   Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS);
22264   ASSERT_TRUE(static_cast<bool>(Style9));
22265   ASSERT_EQ(*Style9, [] {
22266     auto Style = getGoogleStyle();
22267     Style.ColumnLimit = 20;
22268     Style.UseTab = FormatStyle::UT_Always;
22269     return Style;
22270   }());
22271 
22272   // Test 9.4: propagate more than one level with a parent file.
22273   const auto SubSubStyle = [&NonDefaultWhiteSpaceMacros] {
22274     auto Style = getGoogleStyle();
22275     Style.ColumnLimit = 20;
22276     Style.UseTab = FormatStyle::UT_Always;
22277     Style.WhitespaceSensitiveMacros = NonDefaultWhiteSpaceMacros;
22278     return Style;
22279   }();
22280 
22281   ASSERT_NE(Style9->WhitespaceSensitiveMacros, NonDefaultWhiteSpaceMacros);
22282   Style9 = getStyle("file", "/e/sub/sub/code.cpp", "none", "", &FS);
22283   ASSERT_TRUE(static_cast<bool>(Style9));
22284   ASSERT_EQ(*Style9, SubSubStyle);
22285 
22286   // Test 9.5: use InheritParentConfig as style name
22287   Style9 =
22288       getStyle("inheritparentconfig", "/e/sub/sub/code.cpp", "none", "", &FS);
22289   ASSERT_TRUE(static_cast<bool>(Style9));
22290   ASSERT_EQ(*Style9, SubSubStyle);
22291 
22292   // Test 9.6: use command line style with inheritance
22293   Style9 = getStyle("{BasedOnStyle: InheritParentConfig}", "/e/sub/code.cpp",
22294                     "none", "", &FS);
22295   ASSERT_TRUE(static_cast<bool>(Style9));
22296   ASSERT_EQ(*Style9, SubSubStyle);
22297 
22298   // Test 9.7: use command line style with inheritance and own config
22299   Style9 = getStyle("{BasedOnStyle: InheritParentConfig, "
22300                     "WhitespaceSensitiveMacros: ['FOO', 'BAR']}",
22301                     "/e/sub/code.cpp", "none", "", &FS);
22302   ASSERT_TRUE(static_cast<bool>(Style9));
22303   ASSERT_EQ(*Style9, SubSubStyle);
22304 
22305   // Test 9.8: use inheritance from a file without BasedOnStyle
22306   ASSERT_TRUE(FS.addFile("/e/withoutbase/.clang-format", 0,
22307                          llvm::MemoryBuffer::getMemBuffer("ColumnLimit: 123")));
22308   ASSERT_TRUE(
22309       FS.addFile("/e/withoutbase/sub/.clang-format", 0,
22310                  llvm::MemoryBuffer::getMemBuffer(
22311                      "BasedOnStyle: InheritParentConfig\nIndentWidth: 7")));
22312   // Make sure we do not use the fallback style
22313   Style9 = getStyle("file", "/e/withoutbase/code.cpp", "google", "", &FS);
22314   ASSERT_TRUE(static_cast<bool>(Style9));
22315   ASSERT_EQ(*Style9, [] {
22316     auto Style = getLLVMStyle();
22317     Style.ColumnLimit = 123;
22318     return Style;
22319   }());
22320 
22321   Style9 = getStyle("file", "/e/withoutbase/sub/code.cpp", "google", "", &FS);
22322   ASSERT_TRUE(static_cast<bool>(Style9));
22323   ASSERT_EQ(*Style9, [] {
22324     auto Style = getLLVMStyle();
22325     Style.ColumnLimit = 123;
22326     Style.IndentWidth = 7;
22327     return Style;
22328   }());
22329 
22330   // Test 9.9: use inheritance from a specific config file.
22331   Style9 = getStyle("file:/e/sub/sub/.clang-format", "/e/sub/sub/code.cpp",
22332                     "none", "", &FS);
22333   ASSERT_TRUE(static_cast<bool>(Style9));
22334   ASSERT_EQ(*Style9, SubSubStyle);
22335 }
22336 
22337 TEST(FormatStyle, GetStyleOfSpecificFile) {
22338   llvm::vfs::InMemoryFileSystem FS;
22339   // Specify absolute path to a format file in a parent directory.
22340   ASSERT_TRUE(
22341       FS.addFile("/e/.clang-format", 0,
22342                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM")));
22343   ASSERT_TRUE(
22344       FS.addFile("/e/explicit.clang-format", 0,
22345                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google")));
22346   ASSERT_TRUE(FS.addFile("/e/sub/sub/sub/test.cpp", 0,
22347                          llvm::MemoryBuffer::getMemBuffer("int i;")));
22348   auto Style = getStyle("file:/e/explicit.clang-format",
22349                         "/e/sub/sub/sub/test.cpp", "LLVM", "", &FS);
22350   ASSERT_TRUE(static_cast<bool>(Style));
22351   ASSERT_EQ(*Style, getGoogleStyle());
22352 
22353   // Specify relative path to a format file.
22354   ASSERT_TRUE(
22355       FS.addFile("../../e/explicit.clang-format", 0,
22356                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google")));
22357   Style = getStyle("file:../../e/explicit.clang-format",
22358                    "/e/sub/sub/sub/test.cpp", "LLVM", "", &FS);
22359   ASSERT_TRUE(static_cast<bool>(Style));
22360   ASSERT_EQ(*Style, getGoogleStyle());
22361 
22362   // Specify path to a format file that does not exist.
22363   Style = getStyle("file:/e/missing.clang-format", "/e/sub/sub/sub/test.cpp",
22364                    "LLVM", "", &FS);
22365   ASSERT_FALSE(static_cast<bool>(Style));
22366   llvm::consumeError(Style.takeError());
22367 
22368   // Specify path to a file on the filesystem.
22369   SmallString<128> FormatFilePath;
22370   std::error_code ECF = llvm::sys::fs::createTemporaryFile(
22371       "FormatFileTest", "tpl", FormatFilePath);
22372   EXPECT_FALSE((bool)ECF);
22373   llvm::raw_fd_ostream FormatFileTest(FormatFilePath, ECF);
22374   EXPECT_FALSE((bool)ECF);
22375   FormatFileTest << "BasedOnStyle: Google\n";
22376   FormatFileTest.close();
22377 
22378   SmallString<128> TestFilePath;
22379   std::error_code ECT =
22380       llvm::sys::fs::createTemporaryFile("CodeFileTest", "cc", TestFilePath);
22381   EXPECT_FALSE((bool)ECT);
22382   llvm::raw_fd_ostream CodeFileTest(TestFilePath, ECT);
22383   CodeFileTest << "int i;\n";
22384   CodeFileTest.close();
22385 
22386   std::string format_file_arg = std::string("file:") + FormatFilePath.c_str();
22387   Style = getStyle(format_file_arg, TestFilePath, "LLVM", "", nullptr);
22388 
22389   llvm::sys::fs::remove(FormatFilePath.c_str());
22390   llvm::sys::fs::remove(TestFilePath.c_str());
22391   ASSERT_TRUE(static_cast<bool>(Style));
22392   ASSERT_EQ(*Style, getGoogleStyle());
22393 }
22394 
22395 TEST_F(ReplacementTest, FormatCodeAfterReplacements) {
22396   // Column limit is 20.
22397   std::string Code = "Type *a =\n"
22398                      "    new Type();\n"
22399                      "g(iiiii, 0, jjjjj,\n"
22400                      "  0, kkkkk, 0, mm);\n"
22401                      "int  bad     = format   ;";
22402   std::string Expected = "auto a = new Type();\n"
22403                          "g(iiiii, nullptr,\n"
22404                          "  jjjjj, nullptr,\n"
22405                          "  kkkkk, nullptr,\n"
22406                          "  mm);\n"
22407                          "int  bad     = format   ;";
22408   FileID ID = Context.createInMemoryFile("format.cpp", Code);
22409   tooling::Replacements Replaces = toReplacements(
22410       {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6,
22411                             "auto "),
22412        tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1,
22413                             "nullptr"),
22414        tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1,
22415                             "nullptr"),
22416        tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1,
22417                             "nullptr")});
22418 
22419   FormatStyle Style = getLLVMStyle();
22420   Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility.
22421   auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
22422   EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
22423       << llvm::toString(FormattedReplaces.takeError()) << "\n";
22424   auto Result = applyAllReplacements(Code, *FormattedReplaces);
22425   EXPECT_TRUE(static_cast<bool>(Result));
22426   EXPECT_EQ(Expected, *Result);
22427 }
22428 
22429 TEST_F(ReplacementTest, SortIncludesAfterReplacement) {
22430   std::string Code = "#include \"a.h\"\n"
22431                      "#include \"c.h\"\n"
22432                      "\n"
22433                      "int main() {\n"
22434                      "  return 0;\n"
22435                      "}";
22436   std::string Expected = "#include \"a.h\"\n"
22437                          "#include \"b.h\"\n"
22438                          "#include \"c.h\"\n"
22439                          "\n"
22440                          "int main() {\n"
22441                          "  return 0;\n"
22442                          "}";
22443   FileID ID = Context.createInMemoryFile("fix.cpp", Code);
22444   tooling::Replacements Replaces = toReplacements(
22445       {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0,
22446                             "#include \"b.h\"\n")});
22447 
22448   FormatStyle Style = getLLVMStyle();
22449   Style.SortIncludes = FormatStyle::SI_CaseSensitive;
22450   auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
22451   EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
22452       << llvm::toString(FormattedReplaces.takeError()) << "\n";
22453   auto Result = applyAllReplacements(Code, *FormattedReplaces);
22454   EXPECT_TRUE(static_cast<bool>(Result));
22455   EXPECT_EQ(Expected, *Result);
22456 }
22457 
22458 TEST_F(FormatTest, FormatSortsUsingDeclarations) {
22459   EXPECT_EQ("using std::cin;\n"
22460             "using std::cout;",
22461             format("using std::cout;\n"
22462                    "using std::cin;",
22463                    getGoogleStyle()));
22464 }
22465 
22466 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) {
22467   FormatStyle Style = getLLVMStyle();
22468   Style.Standard = FormatStyle::LS_Cpp03;
22469   // cpp03 recognize this string as identifier u8 and literal character 'a'
22470   EXPECT_EQ("auto c = u8 'a';", format("auto c = u8'a';", Style));
22471 }
22472 
22473 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) {
22474   // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers
22475   // all modes, including C++11, C++14 and C++17
22476   EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';"));
22477 }
22478 
22479 TEST_F(FormatTest, DoNotFormatLikelyXml) {
22480   EXPECT_EQ("<!-- ;> -->", format("<!-- ;> -->", getGoogleStyle()));
22481   EXPECT_EQ(" <!-- >; -->", format(" <!-- >; -->", getGoogleStyle()));
22482 }
22483 
22484 TEST_F(FormatTest, StructuredBindings) {
22485   // Structured bindings is a C++17 feature.
22486   // all modes, including C++11, C++14 and C++17
22487   verifyFormat("auto [a, b] = f();");
22488   EXPECT_EQ("auto [a, b] = f();", format("auto[a, b] = f();"));
22489   EXPECT_EQ("const auto [a, b] = f();", format("const   auto[a, b] = f();"));
22490   EXPECT_EQ("auto const [a, b] = f();", format("auto  const[a, b] = f();"));
22491   EXPECT_EQ("auto const volatile [a, b] = f();",
22492             format("auto  const   volatile[a, b] = f();"));
22493   EXPECT_EQ("auto [a, b, c] = f();", format("auto   [  a  ,  b,c   ] = f();"));
22494   EXPECT_EQ("auto &[a, b, c] = f();",
22495             format("auto   &[  a  ,  b,c   ] = f();"));
22496   EXPECT_EQ("auto &&[a, b, c] = f();",
22497             format("auto   &&[  a  ,  b,c   ] = f();"));
22498   EXPECT_EQ("auto const &[a, b] = f();", format("auto  const&[a, b] = f();"));
22499   EXPECT_EQ("auto const volatile &&[a, b] = f();",
22500             format("auto  const  volatile  &&[a, b] = f();"));
22501   EXPECT_EQ("auto const &&[a, b] = f();",
22502             format("auto  const   &&  [a, b] = f();"));
22503   EXPECT_EQ("const auto &[a, b] = f();",
22504             format("const  auto  &  [a, b] = f();"));
22505   EXPECT_EQ("const auto volatile &&[a, b] = f();",
22506             format("const  auto   volatile  &&[a, b] = f();"));
22507   EXPECT_EQ("volatile const auto &&[a, b] = f();",
22508             format("volatile  const  auto   &&[a, b] = f();"));
22509   EXPECT_EQ("const auto &&[a, b] = f();",
22510             format("const  auto  &&  [a, b] = f();"));
22511 
22512   // Make sure we don't mistake structured bindings for lambdas.
22513   FormatStyle PointerMiddle = getLLVMStyle();
22514   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
22515   verifyFormat("auto [a1, b]{A * i};", getGoogleStyle());
22516   verifyFormat("auto [a2, b]{A * i};", getLLVMStyle());
22517   verifyFormat("auto [a3, b]{A * i};", PointerMiddle);
22518   verifyFormat("auto const [a1, b]{A * i};", getGoogleStyle());
22519   verifyFormat("auto const [a2, b]{A * i};", getLLVMStyle());
22520   verifyFormat("auto const [a3, b]{A * i};", PointerMiddle);
22521   verifyFormat("auto const& [a1, b]{A * i};", getGoogleStyle());
22522   verifyFormat("auto const &[a2, b]{A * i};", getLLVMStyle());
22523   verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle);
22524   verifyFormat("auto const&& [a1, b]{A * i};", getGoogleStyle());
22525   verifyFormat("auto const &&[a2, b]{A * i};", getLLVMStyle());
22526   verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle);
22527 
22528   EXPECT_EQ("for (const auto &&[a, b] : some_range) {\n}",
22529             format("for (const auto   &&   [a, b] : some_range) {\n}"));
22530   EXPECT_EQ("for (const auto &[a, b] : some_range) {\n}",
22531             format("for (const auto   &   [a, b] : some_range) {\n}"));
22532   EXPECT_EQ("for (const auto [a, b] : some_range) {\n}",
22533             format("for (const auto[a, b] : some_range) {\n}"));
22534   EXPECT_EQ("auto [x, y](expr);", format("auto[x,y]  (expr);"));
22535   EXPECT_EQ("auto &[x, y](expr);", format("auto  &  [x,y]  (expr);"));
22536   EXPECT_EQ("auto &&[x, y](expr);", format("auto  &&  [x,y]  (expr);"));
22537   EXPECT_EQ("auto const &[x, y](expr);",
22538             format("auto  const  &  [x,y]  (expr);"));
22539   EXPECT_EQ("auto const &&[x, y](expr);",
22540             format("auto  const  &&  [x,y]  (expr);"));
22541   EXPECT_EQ("auto [x, y]{expr};", format("auto[x,y]     {expr};"));
22542   EXPECT_EQ("auto const &[x, y]{expr};",
22543             format("auto  const  &  [x,y]  {expr};"));
22544   EXPECT_EQ("auto const &&[x, y]{expr};",
22545             format("auto  const  &&  [x,y]  {expr};"));
22546 
22547   FormatStyle Spaces = getLLVMStyle();
22548   Spaces.SpacesInSquareBrackets = true;
22549   verifyFormat("auto [ a, b ] = f();", Spaces);
22550   verifyFormat("auto &&[ a, b ] = f();", Spaces);
22551   verifyFormat("auto &[ a, b ] = f();", Spaces);
22552   verifyFormat("auto const &&[ a, b ] = f();", Spaces);
22553   verifyFormat("auto const &[ a, b ] = f();", Spaces);
22554 }
22555 
22556 TEST_F(FormatTest, FileAndCode) {
22557   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.cc", ""));
22558   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.m", ""));
22559   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.mm", ""));
22560   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", ""));
22561   EXPECT_EQ(FormatStyle::LK_ObjC,
22562             guessLanguage("foo.h", "@interface Foo\n@end\n"));
22563   EXPECT_EQ(
22564       FormatStyle::LK_ObjC,
22565       guessLanguage("foo.h", "#define TRY(x, y) @try { x; } @finally { y; }"));
22566   EXPECT_EQ(FormatStyle::LK_ObjC,
22567             guessLanguage("foo.h", "#define AVAIL(x) @available(x, *))"));
22568   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@class Foo;"));
22569   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo", ""));
22570   EXPECT_EQ(FormatStyle::LK_ObjC,
22571             guessLanguage("foo", "@interface Foo\n@end\n"));
22572   EXPECT_EQ(FormatStyle::LK_ObjC,
22573             guessLanguage("foo.h", "int DoStuff(CGRect rect);\n"));
22574   EXPECT_EQ(
22575       FormatStyle::LK_ObjC,
22576       guessLanguage("foo.h",
22577                     "#define MY_POINT_MAKE(x, y) CGPointMake((x), (y));\n"));
22578   EXPECT_EQ(
22579       FormatStyle::LK_Cpp,
22580       guessLanguage("foo.h", "#define FOO(...) auto bar = [] __VA_ARGS__;"));
22581 }
22582 
22583 TEST_F(FormatTest, GuessLanguageWithCpp11AttributeSpecifiers) {
22584   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[noreturn]];"));
22585   EXPECT_EQ(FormatStyle::LK_ObjC,
22586             guessLanguage("foo.h", "array[[calculator getIndex]];"));
22587   EXPECT_EQ(FormatStyle::LK_Cpp,
22588             guessLanguage("foo.h", "[[noreturn, deprecated(\"so sorry\")]];"));
22589   EXPECT_EQ(
22590       FormatStyle::LK_Cpp,
22591       guessLanguage("foo.h", "[[noreturn, deprecated(\"gone, sorry\")]];"));
22592   EXPECT_EQ(FormatStyle::LK_ObjC,
22593             guessLanguage("foo.h", "[[noreturn foo] bar];"));
22594   EXPECT_EQ(FormatStyle::LK_Cpp,
22595             guessLanguage("foo.h", "[[clang::fallthrough]];"));
22596   EXPECT_EQ(FormatStyle::LK_ObjC,
22597             guessLanguage("foo.h", "[[clang:fallthrough] foo];"));
22598   EXPECT_EQ(FormatStyle::LK_Cpp,
22599             guessLanguage("foo.h", "[[gsl::suppress(\"type\")]];"));
22600   EXPECT_EQ(FormatStyle::LK_Cpp,
22601             guessLanguage("foo.h", "[[using clang: fallthrough]];"));
22602   EXPECT_EQ(FormatStyle::LK_ObjC,
22603             guessLanguage("foo.h", "[[abusing clang:fallthrough] bar];"));
22604   EXPECT_EQ(FormatStyle::LK_Cpp,
22605             guessLanguage("foo.h", "[[using gsl: suppress(\"type\")]];"));
22606   EXPECT_EQ(
22607       FormatStyle::LK_Cpp,
22608       guessLanguage("foo.h", "for (auto &&[endpoint, stream] : streams_)"));
22609   EXPECT_EQ(
22610       FormatStyle::LK_Cpp,
22611       guessLanguage("foo.h",
22612                     "[[clang::callable_when(\"unconsumed\", \"unknown\")]]"));
22613   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[foo::bar, ...]]"));
22614 }
22615 
22616 TEST_F(FormatTest, GuessLanguageWithCaret) {
22617   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^);"));
22618   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^, Bar);"));
22619   EXPECT_EQ(FormatStyle::LK_ObjC,
22620             guessLanguage("foo.h", "int(^)(char, float);"));
22621   EXPECT_EQ(FormatStyle::LK_ObjC,
22622             guessLanguage("foo.h", "int(^foo)(char, float);"));
22623   EXPECT_EQ(FormatStyle::LK_ObjC,
22624             guessLanguage("foo.h", "int(^foo[10])(char, float);"));
22625   EXPECT_EQ(FormatStyle::LK_ObjC,
22626             guessLanguage("foo.h", "int(^foo[kNumEntries])(char, float);"));
22627   EXPECT_EQ(
22628       FormatStyle::LK_ObjC,
22629       guessLanguage("foo.h", "int(^foo[(kNumEntries + 10)])(char, float);"));
22630 }
22631 
22632 TEST_F(FormatTest, GuessLanguageWithPragmas) {
22633   EXPECT_EQ(FormatStyle::LK_Cpp,
22634             guessLanguage("foo.h", "__pragma(warning(disable:))"));
22635   EXPECT_EQ(FormatStyle::LK_Cpp,
22636             guessLanguage("foo.h", "#pragma(warning(disable:))"));
22637   EXPECT_EQ(FormatStyle::LK_Cpp,
22638             guessLanguage("foo.h", "_Pragma(warning(disable:))"));
22639 }
22640 
22641 TEST_F(FormatTest, FormatsInlineAsmSymbolicNames) {
22642   // ASM symbolic names are identifiers that must be surrounded by [] without
22643   // space in between:
22644   // https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#InputOperands
22645 
22646   // Example from https://bugs.llvm.org/show_bug.cgi?id=45108.
22647   verifyFormat(R"(//
22648 asm volatile("mrs %x[result], FPCR" : [result] "=r"(result));
22649 )");
22650 
22651   // A list of several ASM symbolic names.
22652   verifyFormat(R"(asm("mov %[e], %[d]" : [d] "=rm"(d), [e] "rm"(*e));)");
22653 
22654   // ASM symbolic names in inline ASM with inputs and outputs.
22655   verifyFormat(R"(//
22656 asm("cmoveq %1, %2, %[result]"
22657     : [result] "=r"(result)
22658     : "r"(test), "r"(new), "[result]"(old));
22659 )");
22660 
22661   // ASM symbolic names in inline ASM with no outputs.
22662   verifyFormat(R"(asm("mov %[e], %[d]" : : [d] "=rm"(d), [e] "rm"(*e));)");
22663 }
22664 
22665 TEST_F(FormatTest, GuessedLanguageWithInlineAsmClobbers) {
22666   EXPECT_EQ(FormatStyle::LK_Cpp,
22667             guessLanguage("foo.h", "void f() {\n"
22668                                    "  asm (\"mov %[e], %[d]\"\n"
22669                                    "     : [d] \"=rm\" (d)\n"
22670                                    "       [e] \"rm\" (*e));\n"
22671                                    "}"));
22672   EXPECT_EQ(FormatStyle::LK_Cpp,
22673             guessLanguage("foo.h", "void f() {\n"
22674                                    "  _asm (\"mov %[e], %[d]\"\n"
22675                                    "     : [d] \"=rm\" (d)\n"
22676                                    "       [e] \"rm\" (*e));\n"
22677                                    "}"));
22678   EXPECT_EQ(FormatStyle::LK_Cpp,
22679             guessLanguage("foo.h", "void f() {\n"
22680                                    "  __asm (\"mov %[e], %[d]\"\n"
22681                                    "     : [d] \"=rm\" (d)\n"
22682                                    "       [e] \"rm\" (*e));\n"
22683                                    "}"));
22684   EXPECT_EQ(FormatStyle::LK_Cpp,
22685             guessLanguage("foo.h", "void f() {\n"
22686                                    "  __asm__ (\"mov %[e], %[d]\"\n"
22687                                    "     : [d] \"=rm\" (d)\n"
22688                                    "       [e] \"rm\" (*e));\n"
22689                                    "}"));
22690   EXPECT_EQ(FormatStyle::LK_Cpp,
22691             guessLanguage("foo.h", "void f() {\n"
22692                                    "  asm (\"mov %[e], %[d]\"\n"
22693                                    "     : [d] \"=rm\" (d),\n"
22694                                    "       [e] \"rm\" (*e));\n"
22695                                    "}"));
22696   EXPECT_EQ(FormatStyle::LK_Cpp,
22697             guessLanguage("foo.h", "void f() {\n"
22698                                    "  asm volatile (\"mov %[e], %[d]\"\n"
22699                                    "     : [d] \"=rm\" (d)\n"
22700                                    "       [e] \"rm\" (*e));\n"
22701                                    "}"));
22702 }
22703 
22704 TEST_F(FormatTest, GuessLanguageWithChildLines) {
22705   EXPECT_EQ(FormatStyle::LK_Cpp,
22706             guessLanguage("foo.h", "#define FOO ({ std::string s; })"));
22707   EXPECT_EQ(FormatStyle::LK_ObjC,
22708             guessLanguage("foo.h", "#define FOO ({ NSString *s; })"));
22709   EXPECT_EQ(
22710       FormatStyle::LK_Cpp,
22711       guessLanguage("foo.h", "#define FOO ({ foo(); ({ std::string s; }) })"));
22712   EXPECT_EQ(
22713       FormatStyle::LK_ObjC,
22714       guessLanguage("foo.h", "#define FOO ({ foo(); ({ NSString *s; }) })"));
22715 }
22716 
22717 TEST_F(FormatTest, TypenameMacros) {
22718   std::vector<std::string> TypenameMacros = {"STACK_OF", "LIST", "TAILQ_ENTRY"};
22719 
22720   // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=30353
22721   FormatStyle Google = getGoogleStyleWithColumns(0);
22722   Google.TypenameMacros = TypenameMacros;
22723   verifyFormat("struct foo {\n"
22724                "  int bar;\n"
22725                "  TAILQ_ENTRY(a) bleh;\n"
22726                "};",
22727                Google);
22728 
22729   FormatStyle Macros = getLLVMStyle();
22730   Macros.TypenameMacros = TypenameMacros;
22731 
22732   verifyFormat("STACK_OF(int) a;", Macros);
22733   verifyFormat("STACK_OF(int) *a;", Macros);
22734   verifyFormat("STACK_OF(int const *) *a;", Macros);
22735   verifyFormat("STACK_OF(int *const) *a;", Macros);
22736   verifyFormat("STACK_OF(int, string) a;", Macros);
22737   verifyFormat("STACK_OF(LIST(int)) a;", Macros);
22738   verifyFormat("STACK_OF(LIST(int)) a, b;", Macros);
22739   verifyFormat("for (LIST(int) *a = NULL; a;) {\n}", Macros);
22740   verifyFormat("STACK_OF(int) f(LIST(int) *arg);", Macros);
22741   verifyFormat("vector<LIST(uint64_t) *attr> x;", Macros);
22742   verifyFormat("vector<LIST(uint64_t) *const> f(LIST(uint64_t) *arg);", Macros);
22743 
22744   Macros.PointerAlignment = FormatStyle::PAS_Left;
22745   verifyFormat("STACK_OF(int)* a;", Macros);
22746   verifyFormat("STACK_OF(int*)* a;", Macros);
22747   verifyFormat("x = (STACK_OF(uint64_t))*a;", Macros);
22748   verifyFormat("x = (STACK_OF(uint64_t))&a;", Macros);
22749   verifyFormat("vector<STACK_OF(uint64_t)* attr> x;", Macros);
22750 }
22751 
22752 TEST_F(FormatTest, AtomicQualifier) {
22753   // Check that we treate _Atomic as a type and not a function call
22754   FormatStyle Google = getGoogleStyleWithColumns(0);
22755   verifyFormat("struct foo {\n"
22756                "  int a1;\n"
22757                "  _Atomic(a) a2;\n"
22758                "  _Atomic(_Atomic(int) *const) a3;\n"
22759                "};",
22760                Google);
22761   verifyFormat("_Atomic(uint64_t) a;");
22762   verifyFormat("_Atomic(uint64_t) *a;");
22763   verifyFormat("_Atomic(uint64_t const *) *a;");
22764   verifyFormat("_Atomic(uint64_t *const) *a;");
22765   verifyFormat("_Atomic(const uint64_t *) *a;");
22766   verifyFormat("_Atomic(uint64_t) a;");
22767   verifyFormat("_Atomic(_Atomic(uint64_t)) a;");
22768   verifyFormat("_Atomic(_Atomic(uint64_t)) a, b;");
22769   verifyFormat("for (_Atomic(uint64_t) *a = NULL; a;) {\n}");
22770   verifyFormat("_Atomic(uint64_t) f(_Atomic(uint64_t) *arg);");
22771 
22772   verifyFormat("_Atomic(uint64_t) *s(InitValue);");
22773   verifyFormat("_Atomic(uint64_t) *s{InitValue};");
22774   FormatStyle Style = getLLVMStyle();
22775   Style.PointerAlignment = FormatStyle::PAS_Left;
22776   verifyFormat("_Atomic(uint64_t)* s(InitValue);", Style);
22777   verifyFormat("_Atomic(uint64_t)* s{InitValue};", Style);
22778   verifyFormat("_Atomic(int)* a;", Style);
22779   verifyFormat("_Atomic(int*)* a;", Style);
22780   verifyFormat("vector<_Atomic(uint64_t)* attr> x;", Style);
22781 
22782   Style.SpacesInCStyleCastParentheses = true;
22783   Style.SpacesInParentheses = false;
22784   verifyFormat("x = ( _Atomic(uint64_t) )*a;", Style);
22785   Style.SpacesInCStyleCastParentheses = false;
22786   Style.SpacesInParentheses = true;
22787   verifyFormat("x = (_Atomic( uint64_t ))*a;", Style);
22788   verifyFormat("x = (_Atomic( uint64_t ))&a;", Style);
22789 }
22790 
22791 TEST_F(FormatTest, AmbersandInLamda) {
22792   // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=41899
22793   FormatStyle AlignStyle = getLLVMStyle();
22794   AlignStyle.PointerAlignment = FormatStyle::PAS_Left;
22795   verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle);
22796   AlignStyle.PointerAlignment = FormatStyle::PAS_Right;
22797   verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle);
22798 }
22799 
22800 TEST_F(FormatTest, SpacesInConditionalStatement) {
22801   FormatStyle Spaces = getLLVMStyle();
22802   Spaces.IfMacros.clear();
22803   Spaces.IfMacros.push_back("MYIF");
22804   Spaces.SpacesInConditionalStatement = true;
22805   verifyFormat("for ( int i = 0; i; i++ )\n  continue;", Spaces);
22806   verifyFormat("if ( !a )\n  return;", Spaces);
22807   verifyFormat("if ( a )\n  return;", Spaces);
22808   verifyFormat("if constexpr ( a )\n  return;", Spaces);
22809   verifyFormat("MYIF ( a )\n  return;", Spaces);
22810   verifyFormat("MYIF ( a )\n  return;\nelse MYIF ( b )\n  return;", Spaces);
22811   verifyFormat("MYIF ( a )\n  return;\nelse\n  return;", Spaces);
22812   verifyFormat("switch ( a )\ncase 1:\n  return;", Spaces);
22813   verifyFormat("while ( a )\n  return;", Spaces);
22814   verifyFormat("while ( (a && b) )\n  return;", Spaces);
22815   verifyFormat("do {\n} while ( 1 != 0 );", Spaces);
22816   verifyFormat("try {\n} catch ( const std::exception & ) {\n}", Spaces);
22817   // Check that space on the left of "::" is inserted as expected at beginning
22818   // of condition.
22819   verifyFormat("while ( ::func() )\n  return;", Spaces);
22820 
22821   // Check impact of ControlStatementsExceptControlMacros is honored.
22822   Spaces.SpaceBeforeParens =
22823       FormatStyle::SBPO_ControlStatementsExceptControlMacros;
22824   verifyFormat("MYIF( a )\n  return;", Spaces);
22825   verifyFormat("MYIF( a )\n  return;\nelse MYIF( b )\n  return;", Spaces);
22826   verifyFormat("MYIF( a )\n  return;\nelse\n  return;", Spaces);
22827 }
22828 
22829 TEST_F(FormatTest, AlternativeOperators) {
22830   // Test case for ensuring alternate operators are not
22831   // combined with their right most neighbour.
22832   verifyFormat("int a and b;");
22833   verifyFormat("int a and_eq b;");
22834   verifyFormat("int a bitand b;");
22835   verifyFormat("int a bitor b;");
22836   verifyFormat("int a compl b;");
22837   verifyFormat("int a not b;");
22838   verifyFormat("int a not_eq b;");
22839   verifyFormat("int a or b;");
22840   verifyFormat("int a xor b;");
22841   verifyFormat("int a xor_eq b;");
22842   verifyFormat("return this not_eq bitand other;");
22843   verifyFormat("bool operator not_eq(const X bitand other)");
22844 
22845   verifyFormat("int a and 5;");
22846   verifyFormat("int a and_eq 5;");
22847   verifyFormat("int a bitand 5;");
22848   verifyFormat("int a bitor 5;");
22849   verifyFormat("int a compl 5;");
22850   verifyFormat("int a not 5;");
22851   verifyFormat("int a not_eq 5;");
22852   verifyFormat("int a or 5;");
22853   verifyFormat("int a xor 5;");
22854   verifyFormat("int a xor_eq 5;");
22855 
22856   verifyFormat("int a compl(5);");
22857   verifyFormat("int a not(5);");
22858 
22859   /* FIXME handle alternate tokens
22860    * https://en.cppreference.com/w/cpp/language/operator_alternative
22861   // alternative tokens
22862   verifyFormat("compl foo();");     //  ~foo();
22863   verifyFormat("foo() <%%>;");      // foo();
22864   verifyFormat("void foo() <%%>;"); // void foo(){}
22865   verifyFormat("int a <:1:>;");     // int a[1];[
22866   verifyFormat("%:define ABC abc"); // #define ABC abc
22867   verifyFormat("%:%:");             // ##
22868   */
22869 }
22870 
22871 TEST_F(FormatTest, STLWhileNotDefineChed) {
22872   verifyFormat("#if defined(while)\n"
22873                "#define while EMIT WARNING C4005\n"
22874                "#endif // while");
22875 }
22876 
22877 TEST_F(FormatTest, OperatorSpacing) {
22878   FormatStyle Style = getLLVMStyle();
22879   Style.PointerAlignment = FormatStyle::PAS_Right;
22880   verifyFormat("Foo::operator*();", Style);
22881   verifyFormat("Foo::operator void *();", Style);
22882   verifyFormat("Foo::operator void **();", Style);
22883   verifyFormat("Foo::operator void *&();", Style);
22884   verifyFormat("Foo::operator void *&&();", Style);
22885   verifyFormat("Foo::operator void const *();", Style);
22886   verifyFormat("Foo::operator void const **();", Style);
22887   verifyFormat("Foo::operator void const *&();", Style);
22888   verifyFormat("Foo::operator void const *&&();", Style);
22889   verifyFormat("Foo::operator()(void *);", Style);
22890   verifyFormat("Foo::operator*(void *);", Style);
22891   verifyFormat("Foo::operator*();", Style);
22892   verifyFormat("Foo::operator**();", Style);
22893   verifyFormat("Foo::operator&();", Style);
22894   verifyFormat("Foo::operator<int> *();", Style);
22895   verifyFormat("Foo::operator<Foo> *();", Style);
22896   verifyFormat("Foo::operator<int> **();", Style);
22897   verifyFormat("Foo::operator<Foo> **();", Style);
22898   verifyFormat("Foo::operator<int> &();", Style);
22899   verifyFormat("Foo::operator<Foo> &();", Style);
22900   verifyFormat("Foo::operator<int> &&();", Style);
22901   verifyFormat("Foo::operator<Foo> &&();", Style);
22902   verifyFormat("Foo::operator<int> *&();", Style);
22903   verifyFormat("Foo::operator<Foo> *&();", Style);
22904   verifyFormat("Foo::operator<int> *&&();", Style);
22905   verifyFormat("Foo::operator<Foo> *&&();", Style);
22906   verifyFormat("operator*(int (*)(), class Foo);", Style);
22907 
22908   verifyFormat("Foo::operator&();", Style);
22909   verifyFormat("Foo::operator void &();", Style);
22910   verifyFormat("Foo::operator void const &();", Style);
22911   verifyFormat("Foo::operator()(void &);", Style);
22912   verifyFormat("Foo::operator&(void &);", Style);
22913   verifyFormat("Foo::operator&();", Style);
22914   verifyFormat("operator&(int (&)(), class Foo);", Style);
22915   verifyFormat("operator&&(int (&)(), class Foo);", Style);
22916 
22917   verifyFormat("Foo::operator&&();", Style);
22918   verifyFormat("Foo::operator**();", Style);
22919   verifyFormat("Foo::operator void &&();", Style);
22920   verifyFormat("Foo::operator void const &&();", Style);
22921   verifyFormat("Foo::operator()(void &&);", Style);
22922   verifyFormat("Foo::operator&&(void &&);", Style);
22923   verifyFormat("Foo::operator&&();", Style);
22924   verifyFormat("operator&&(int (&&)(), class Foo);", Style);
22925   verifyFormat("operator const nsTArrayRight<E> &()", Style);
22926   verifyFormat("[[nodiscard]] operator const nsTArrayRight<E, Allocator> &()",
22927                Style);
22928   verifyFormat("operator void **()", Style);
22929   verifyFormat("operator const FooRight<Object> &()", Style);
22930   verifyFormat("operator const FooRight<Object> *()", Style);
22931   verifyFormat("operator const FooRight<Object> **()", Style);
22932   verifyFormat("operator const FooRight<Object> *&()", Style);
22933   verifyFormat("operator const FooRight<Object> *&&()", Style);
22934 
22935   Style.PointerAlignment = FormatStyle::PAS_Left;
22936   verifyFormat("Foo::operator*();", Style);
22937   verifyFormat("Foo::operator**();", Style);
22938   verifyFormat("Foo::operator void*();", Style);
22939   verifyFormat("Foo::operator void**();", Style);
22940   verifyFormat("Foo::operator void*&();", Style);
22941   verifyFormat("Foo::operator void*&&();", Style);
22942   verifyFormat("Foo::operator void const*();", Style);
22943   verifyFormat("Foo::operator void const**();", Style);
22944   verifyFormat("Foo::operator void const*&();", Style);
22945   verifyFormat("Foo::operator void const*&&();", Style);
22946   verifyFormat("Foo::operator/*comment*/ void*();", Style);
22947   verifyFormat("Foo::operator/*a*/ const /*b*/ void*();", Style);
22948   verifyFormat("Foo::operator/*a*/ volatile /*b*/ void*();", Style);
22949   verifyFormat("Foo::operator()(void*);", Style);
22950   verifyFormat("Foo::operator*(void*);", Style);
22951   verifyFormat("Foo::operator*();", Style);
22952   verifyFormat("Foo::operator<int>*();", Style);
22953   verifyFormat("Foo::operator<Foo>*();", Style);
22954   verifyFormat("Foo::operator<int>**();", Style);
22955   verifyFormat("Foo::operator<Foo>**();", Style);
22956   verifyFormat("Foo::operator<Foo>*&();", Style);
22957   verifyFormat("Foo::operator<int>&();", Style);
22958   verifyFormat("Foo::operator<Foo>&();", Style);
22959   verifyFormat("Foo::operator<int>&&();", Style);
22960   verifyFormat("Foo::operator<Foo>&&();", Style);
22961   verifyFormat("Foo::operator<int>*&();", Style);
22962   verifyFormat("Foo::operator<Foo>*&();", Style);
22963   verifyFormat("operator*(int (*)(), class Foo);", Style);
22964 
22965   verifyFormat("Foo::operator&();", Style);
22966   verifyFormat("Foo::operator void&();", Style);
22967   verifyFormat("Foo::operator void const&();", Style);
22968   verifyFormat("Foo::operator/*comment*/ void&();", Style);
22969   verifyFormat("Foo::operator/*a*/ const /*b*/ void&();", Style);
22970   verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&();", Style);
22971   verifyFormat("Foo::operator()(void&);", Style);
22972   verifyFormat("Foo::operator&(void&);", Style);
22973   verifyFormat("Foo::operator&();", Style);
22974   verifyFormat("operator&(int (&)(), class Foo);", Style);
22975   verifyFormat("operator&(int (&&)(), class Foo);", Style);
22976   verifyFormat("operator&&(int (&&)(), class Foo);", Style);
22977 
22978   verifyFormat("Foo::operator&&();", Style);
22979   verifyFormat("Foo::operator void&&();", Style);
22980   verifyFormat("Foo::operator void const&&();", Style);
22981   verifyFormat("Foo::operator/*comment*/ void&&();", Style);
22982   verifyFormat("Foo::operator/*a*/ const /*b*/ void&&();", Style);
22983   verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&&();", Style);
22984   verifyFormat("Foo::operator()(void&&);", Style);
22985   verifyFormat("Foo::operator&&(void&&);", Style);
22986   verifyFormat("Foo::operator&&();", Style);
22987   verifyFormat("operator&&(int (&&)(), class Foo);", Style);
22988   verifyFormat("operator const nsTArrayLeft<E>&()", Style);
22989   verifyFormat("[[nodiscard]] operator const nsTArrayLeft<E, Allocator>&()",
22990                Style);
22991   verifyFormat("operator void**()", Style);
22992   verifyFormat("operator const FooLeft<Object>&()", Style);
22993   verifyFormat("operator const FooLeft<Object>*()", Style);
22994   verifyFormat("operator const FooLeft<Object>**()", Style);
22995   verifyFormat("operator const FooLeft<Object>*&()", Style);
22996   verifyFormat("operator const FooLeft<Object>*&&()", Style);
22997 
22998   // PR45107
22999   verifyFormat("operator Vector<String>&();", Style);
23000   verifyFormat("operator const Vector<String>&();", Style);
23001   verifyFormat("operator foo::Bar*();", Style);
23002   verifyFormat("operator const Foo<X>::Bar<Y>*();", Style);
23003   verifyFormat("operator/*a*/ const /*b*/ Foo /*c*/<X> /*d*/ ::Bar<Y>*();",
23004                Style);
23005 
23006   Style.PointerAlignment = FormatStyle::PAS_Middle;
23007   verifyFormat("Foo::operator*();", Style);
23008   verifyFormat("Foo::operator void *();", Style);
23009   verifyFormat("Foo::operator()(void *);", Style);
23010   verifyFormat("Foo::operator*(void *);", Style);
23011   verifyFormat("Foo::operator*();", Style);
23012   verifyFormat("operator*(int (*)(), class Foo);", Style);
23013 
23014   verifyFormat("Foo::operator&();", Style);
23015   verifyFormat("Foo::operator void &();", Style);
23016   verifyFormat("Foo::operator void const &();", Style);
23017   verifyFormat("Foo::operator()(void &);", Style);
23018   verifyFormat("Foo::operator&(void &);", Style);
23019   verifyFormat("Foo::operator&();", Style);
23020   verifyFormat("operator&(int (&)(), class Foo);", Style);
23021 
23022   verifyFormat("Foo::operator&&();", Style);
23023   verifyFormat("Foo::operator void &&();", Style);
23024   verifyFormat("Foo::operator void const &&();", Style);
23025   verifyFormat("Foo::operator()(void &&);", Style);
23026   verifyFormat("Foo::operator&&(void &&);", Style);
23027   verifyFormat("Foo::operator&&();", Style);
23028   verifyFormat("operator&&(int (&&)(), class Foo);", Style);
23029 }
23030 
23031 TEST_F(FormatTest, OperatorPassedAsAFunctionPtr) {
23032   FormatStyle Style = getLLVMStyle();
23033   // PR46157
23034   verifyFormat("foo(operator+, -42);", Style);
23035   verifyFormat("foo(operator++, -42);", Style);
23036   verifyFormat("foo(operator--, -42);", Style);
23037   verifyFormat("foo(-42, operator--);", Style);
23038   verifyFormat("foo(-42, operator, );", Style);
23039   verifyFormat("foo(operator, , -42);", Style);
23040 }
23041 
23042 TEST_F(FormatTest, WhitespaceSensitiveMacros) {
23043   FormatStyle Style = getLLVMStyle();
23044   Style.WhitespaceSensitiveMacros.push_back("FOO");
23045 
23046   // Don't use the helpers here, since 'mess up' will change the whitespace
23047   // and these are all whitespace sensitive by definition
23048   EXPECT_EQ("FOO(String-ized&Messy+But(: :Still)=Intentional);",
23049             format("FOO(String-ized&Messy+But(: :Still)=Intentional);", Style));
23050   EXPECT_EQ(
23051       "FOO(String-ized&Messy+But\\(: :Still)=Intentional);",
23052       format("FOO(String-ized&Messy+But\\(: :Still)=Intentional);", Style));
23053   EXPECT_EQ("FOO(String-ized&Messy+But,: :Still=Intentional);",
23054             format("FOO(String-ized&Messy+But,: :Still=Intentional);", Style));
23055   EXPECT_EQ("FOO(String-ized&Messy+But,: :\n"
23056             "       Still=Intentional);",
23057             format("FOO(String-ized&Messy+But,: :\n"
23058                    "       Still=Intentional);",
23059                    Style));
23060   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
23061   EXPECT_EQ("FOO(String-ized=&Messy+But,: :\n"
23062             "       Still=Intentional);",
23063             format("FOO(String-ized=&Messy+But,: :\n"
23064                    "       Still=Intentional);",
23065                    Style));
23066 
23067   Style.ColumnLimit = 21;
23068   EXPECT_EQ("FOO(String-ized&Messy+But: :Still=Intentional);",
23069             format("FOO(String-ized&Messy+But: :Still=Intentional);", Style));
23070 }
23071 
23072 TEST_F(FormatTest, VeryLongNamespaceCommentSplit) {
23073   // These tests are not in NamespaceFixer because that doesn't
23074   // test its interaction with line wrapping
23075   FormatStyle Style = getLLVMStyleWithColumns(80);
23076   verifyFormat("namespace {\n"
23077                "int i;\n"
23078                "int j;\n"
23079                "} // namespace",
23080                Style);
23081 
23082   verifyFormat("namespace AAA {\n"
23083                "int i;\n"
23084                "int j;\n"
23085                "} // namespace AAA",
23086                Style);
23087 
23088   EXPECT_EQ("namespace Averyveryveryverylongnamespace {\n"
23089             "int i;\n"
23090             "int j;\n"
23091             "} // namespace Averyveryveryverylongnamespace",
23092             format("namespace Averyveryveryverylongnamespace {\n"
23093                    "int i;\n"
23094                    "int j;\n"
23095                    "}",
23096                    Style));
23097 
23098   EXPECT_EQ(
23099       "namespace "
23100       "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n"
23101       "    went::mad::now {\n"
23102       "int i;\n"
23103       "int j;\n"
23104       "} // namespace\n"
23105       "  // "
23106       "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::"
23107       "went::mad::now",
23108       format("namespace "
23109              "would::it::save::you::a::lot::of::time::if_::i::"
23110              "just::gave::up::and_::went::mad::now {\n"
23111              "int i;\n"
23112              "int j;\n"
23113              "}",
23114              Style));
23115 
23116   // This used to duplicate the comment again and again on subsequent runs
23117   EXPECT_EQ(
23118       "namespace "
23119       "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n"
23120       "    went::mad::now {\n"
23121       "int i;\n"
23122       "int j;\n"
23123       "} // namespace\n"
23124       "  // "
23125       "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::"
23126       "went::mad::now",
23127       format("namespace "
23128              "would::it::save::you::a::lot::of::time::if_::i::"
23129              "just::gave::up::and_::went::mad::now {\n"
23130              "int i;\n"
23131              "int j;\n"
23132              "} // namespace\n"
23133              "  // "
23134              "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::"
23135              "and_::went::mad::now",
23136              Style));
23137 }
23138 
23139 TEST_F(FormatTest, LikelyUnlikely) {
23140   FormatStyle Style = getLLVMStyle();
23141 
23142   verifyFormat("if (argc > 5) [[unlikely]] {\n"
23143                "  return 29;\n"
23144                "}",
23145                Style);
23146 
23147   verifyFormat("if (argc > 5) [[likely]] {\n"
23148                "  return 29;\n"
23149                "}",
23150                Style);
23151 
23152   verifyFormat("if (argc > 5) [[unlikely]] {\n"
23153                "  return 29;\n"
23154                "} else [[likely]] {\n"
23155                "  return 42;\n"
23156                "}\n",
23157                Style);
23158 
23159   verifyFormat("if (argc > 5) [[unlikely]] {\n"
23160                "  return 29;\n"
23161                "} else if (argc > 10) [[likely]] {\n"
23162                "  return 99;\n"
23163                "} else {\n"
23164                "  return 42;\n"
23165                "}\n",
23166                Style);
23167 
23168   verifyFormat("if (argc > 5) [[gnu::unused]] {\n"
23169                "  return 29;\n"
23170                "}",
23171                Style);
23172 
23173   verifyFormat("if (argc > 5) [[unlikely]]\n"
23174                "  return 29;\n",
23175                Style);
23176   verifyFormat("if (argc > 5) [[likely]]\n"
23177                "  return 29;\n",
23178                Style);
23179 
23180   Style.AttributeMacros.push_back("UNLIKELY");
23181   Style.AttributeMacros.push_back("LIKELY");
23182   verifyFormat("if (argc > 5) UNLIKELY\n"
23183                "  return 29;\n",
23184                Style);
23185 
23186   verifyFormat("if (argc > 5) UNLIKELY {\n"
23187                "  return 29;\n"
23188                "}",
23189                Style);
23190   verifyFormat("if (argc > 5) UNLIKELY {\n"
23191                "  return 29;\n"
23192                "} else [[likely]] {\n"
23193                "  return 42;\n"
23194                "}\n",
23195                Style);
23196   verifyFormat("if (argc > 5) UNLIKELY {\n"
23197                "  return 29;\n"
23198                "} else LIKELY {\n"
23199                "  return 42;\n"
23200                "}\n",
23201                Style);
23202   verifyFormat("if (argc > 5) [[unlikely]] {\n"
23203                "  return 29;\n"
23204                "} else LIKELY {\n"
23205                "  return 42;\n"
23206                "}\n",
23207                Style);
23208 }
23209 
23210 TEST_F(FormatTest, PenaltyIndentedWhitespace) {
23211   verifyFormat("Constructor()\n"
23212                "    : aaaaaa(aaaaaa), aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
23213                "                          aaaa(aaaaaaaaaaaaaaaaaa, "
23214                "aaaaaaaaaaaaaaaaaat))");
23215   verifyFormat("Constructor()\n"
23216                "    : aaaaaaaaaaaaa(aaaaaa), "
23217                "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)");
23218 
23219   FormatStyle StyleWithWhitespacePenalty = getLLVMStyle();
23220   StyleWithWhitespacePenalty.PenaltyIndentedWhitespace = 5;
23221   verifyFormat("Constructor()\n"
23222                "    : aaaaaa(aaaaaa),\n"
23223                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
23224                "          aaaa(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaat))",
23225                StyleWithWhitespacePenalty);
23226   verifyFormat("Constructor()\n"
23227                "    : aaaaaaaaaaaaa(aaaaaa), "
23228                "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)",
23229                StyleWithWhitespacePenalty);
23230 }
23231 
23232 TEST_F(FormatTest, LLVMDefaultStyle) {
23233   FormatStyle Style = getLLVMStyle();
23234   verifyFormat("extern \"C\" {\n"
23235                "int foo();\n"
23236                "}",
23237                Style);
23238 }
23239 TEST_F(FormatTest, GNUDefaultStyle) {
23240   FormatStyle Style = getGNUStyle();
23241   verifyFormat("extern \"C\"\n"
23242                "{\n"
23243                "  int foo ();\n"
23244                "}",
23245                Style);
23246 }
23247 TEST_F(FormatTest, MozillaDefaultStyle) {
23248   FormatStyle Style = getMozillaStyle();
23249   verifyFormat("extern \"C\"\n"
23250                "{\n"
23251                "  int foo();\n"
23252                "}",
23253                Style);
23254 }
23255 TEST_F(FormatTest, GoogleDefaultStyle) {
23256   FormatStyle Style = getGoogleStyle();
23257   verifyFormat("extern \"C\" {\n"
23258                "int foo();\n"
23259                "}",
23260                Style);
23261 }
23262 TEST_F(FormatTest, ChromiumDefaultStyle) {
23263   FormatStyle Style = getChromiumStyle(FormatStyle::LanguageKind::LK_Cpp);
23264   verifyFormat("extern \"C\" {\n"
23265                "int foo();\n"
23266                "}",
23267                Style);
23268 }
23269 TEST_F(FormatTest, MicrosoftDefaultStyle) {
23270   FormatStyle Style = getMicrosoftStyle(FormatStyle::LanguageKind::LK_Cpp);
23271   verifyFormat("extern \"C\"\n"
23272                "{\n"
23273                "    int foo();\n"
23274                "}",
23275                Style);
23276 }
23277 TEST_F(FormatTest, WebKitDefaultStyle) {
23278   FormatStyle Style = getWebKitStyle();
23279   verifyFormat("extern \"C\" {\n"
23280                "int foo();\n"
23281                "}",
23282                Style);
23283 }
23284 
23285 TEST_F(FormatTest, Concepts) {
23286   EXPECT_EQ(getLLVMStyle().BreakBeforeConceptDeclarations,
23287             FormatStyle::BBCDS_Always);
23288   verifyFormat("template <typename T>\n"
23289                "concept True = true;");
23290 
23291   verifyFormat("template <typename T>\n"
23292                "concept C = ((false || foo()) && C2<T>) ||\n"
23293                "            (std::trait<T>::value && Baz) || sizeof(T) >= 6;",
23294                getLLVMStyleWithColumns(60));
23295 
23296   verifyFormat("template <typename T>\n"
23297                "concept DelayedCheck = true && requires(T t) { t.bar(); } && "
23298                "sizeof(T) <= 8;");
23299 
23300   verifyFormat("template <typename T>\n"
23301                "concept DelayedCheck = true && requires(T t) {\n"
23302                "                                 t.bar();\n"
23303                "                                 t.baz();\n"
23304                "                               } && sizeof(T) <= 8;");
23305 
23306   verifyFormat("template <typename T>\n"
23307                "concept DelayedCheck = true && requires(T t) { // Comment\n"
23308                "                                 t.bar();\n"
23309                "                                 t.baz();\n"
23310                "                               } && sizeof(T) <= 8;");
23311 
23312   verifyFormat("template <typename T>\n"
23313                "concept DelayedCheck = false || requires(T t) { t.bar(); } && "
23314                "sizeof(T) <= 8;");
23315 
23316   verifyFormat("template <typename T>\n"
23317                "concept DelayedCheck = !!false || requires(T t) { t.bar(); } "
23318                "&& sizeof(T) <= 8;");
23319 
23320   verifyFormat(
23321       "template <typename T>\n"
23322       "concept DelayedCheck = static_cast<bool>(0) ||\n"
23323       "                       requires(T t) { t.bar(); } && sizeof(T) <= 8;");
23324 
23325   verifyFormat("template <typename T>\n"
23326                "concept DelayedCheck = bool(0) || requires(T t) { t.bar(); } "
23327                "&& sizeof(T) <= 8;");
23328 
23329   verifyFormat(
23330       "template <typename T>\n"
23331       "concept DelayedCheck = (bool)(0) ||\n"
23332       "                       requires(T t) { t.bar(); } && sizeof(T) <= 8;");
23333 
23334   verifyFormat("template <typename T>\n"
23335                "concept DelayedCheck = (bool)0 || requires(T t) { t.bar(); } "
23336                "&& sizeof(T) <= 8;");
23337 
23338   verifyFormat("template <typename T>\n"
23339                "concept Size = sizeof(T) >= 5 && requires(T t) { t.bar(); } && "
23340                "sizeof(T) <= 8;");
23341 
23342   verifyFormat("template <typename T>\n"
23343                "concept Size = 2 < 5 && 2 <= 5 && 8 >= 5 && 8 > 5 &&\n"
23344                "               requires(T t) {\n"
23345                "                 t.bar();\n"
23346                "                 t.baz();\n"
23347                "               } && sizeof(T) <= 8 && !(4 < 3);",
23348                getLLVMStyleWithColumns(60));
23349 
23350   verifyFormat("template <typename T>\n"
23351                "concept TrueOrNot = IsAlwaysTrue || IsNeverTrue;");
23352 
23353   verifyFormat("template <typename T>\n"
23354                "concept C = foo();");
23355 
23356   verifyFormat("template <typename T>\n"
23357                "concept C = foo(T());");
23358 
23359   verifyFormat("template <typename T>\n"
23360                "concept C = foo(T{});");
23361 
23362   verifyFormat("template <typename T>\n"
23363                "concept Size = V<sizeof(T)>::Value > 5;");
23364 
23365   verifyFormat("template <typename T>\n"
23366                "concept True = S<T>::Value;");
23367 
23368   verifyFormat(
23369       "template <typename T>\n"
23370       "concept C = []() { return true; }() && requires(T t) { t.bar(); } &&\n"
23371       "            sizeof(T) <= 8;");
23372 
23373   // FIXME: This is misformatted because the fake l paren starts at bool, not at
23374   // the lambda l square.
23375   verifyFormat("template <typename T>\n"
23376                "concept C = [] -> bool { return true; }() && requires(T t) { "
23377                "t.bar(); } &&\n"
23378                "                      sizeof(T) <= 8;");
23379 
23380   verifyFormat(
23381       "template <typename T>\n"
23382       "concept C = decltype([]() { return std::true_type{}; }())::value &&\n"
23383       "            requires(T t) { t.bar(); } && sizeof(T) <= 8;");
23384 
23385   verifyFormat("template <typename T>\n"
23386                "concept C = decltype([]() { return std::true_type{}; "
23387                "}())::value && requires(T t) { t.bar(); } && sizeof(T) <= 8;",
23388                getLLVMStyleWithColumns(120));
23389 
23390   verifyFormat("template <typename T>\n"
23391                "concept C = decltype([]() -> std::true_type { return {}; "
23392                "}())::value &&\n"
23393                "            requires(T t) { t.bar(); } && sizeof(T) <= 8;");
23394 
23395   verifyFormat("template <typename T>\n"
23396                "concept C = true;\n"
23397                "Foo Bar;");
23398 
23399   verifyFormat("template <typename T>\n"
23400                "concept Hashable = requires(T a) {\n"
23401                "                     { std::hash<T>{}(a) } -> "
23402                "std::convertible_to<std::size_t>;\n"
23403                "                   };");
23404 
23405   verifyFormat(
23406       "template <typename T>\n"
23407       "concept EqualityComparable = requires(T a, T b) {\n"
23408       "                               { a == b } -> std::same_as<bool>;\n"
23409       "                             };");
23410 
23411   verifyFormat(
23412       "template <typename T>\n"
23413       "concept EqualityComparable = requires(T a, T b) {\n"
23414       "                               { a == b } -> std::same_as<bool>;\n"
23415       "                               { a != b } -> std::same_as<bool>;\n"
23416       "                             };");
23417 
23418   verifyFormat("template <typename T>\n"
23419                "concept WeakEqualityComparable = requires(T a, T b) {\n"
23420                "                                   { a == b };\n"
23421                "                                   { a != b };\n"
23422                "                                 };");
23423 
23424   verifyFormat("template <typename T>\n"
23425                "concept HasSizeT = requires { typename T::size_t; };");
23426 
23427   verifyFormat("template <typename T>\n"
23428                "concept Semiregular =\n"
23429                "    DefaultConstructible<T> && CopyConstructible<T> && "
23430                "CopyAssignable<T> &&\n"
23431                "    requires(T a, std::size_t n) {\n"
23432                "      requires Same<T *, decltype(&a)>;\n"
23433                "      { a.~T() } noexcept;\n"
23434                "      requires Same<T *, decltype(new T)>;\n"
23435                "      requires Same<T *, decltype(new T[n])>;\n"
23436                "      { delete new T; };\n"
23437                "      { delete new T[n]; };\n"
23438                "    };");
23439 
23440   verifyFormat("template <typename T>\n"
23441                "concept Semiregular =\n"
23442                "    requires(T a, std::size_t n) {\n"
23443                "      requires Same<T *, decltype(&a)>;\n"
23444                "      { a.~T() } noexcept;\n"
23445                "      requires Same<T *, decltype(new T)>;\n"
23446                "      requires Same<T *, decltype(new T[n])>;\n"
23447                "      { delete new T; };\n"
23448                "      { delete new T[n]; };\n"
23449                "      { new T } -> std::same_as<T *>;\n"
23450                "    } && DefaultConstructible<T> && CopyConstructible<T> && "
23451                "CopyAssignable<T>;");
23452 
23453   verifyFormat(
23454       "template <typename T>\n"
23455       "concept Semiregular =\n"
23456       "    DefaultConstructible<T> && requires(T a, std::size_t n) {\n"
23457       "                                 requires Same<T *, decltype(&a)>;\n"
23458       "                                 { a.~T() } noexcept;\n"
23459       "                                 requires Same<T *, decltype(new T)>;\n"
23460       "                                 requires Same<T *, decltype(new "
23461       "T[n])>;\n"
23462       "                                 { delete new T; };\n"
23463       "                                 { delete new T[n]; };\n"
23464       "                               } && CopyConstructible<T> && "
23465       "CopyAssignable<T>;");
23466 
23467   verifyFormat("template <typename T>\n"
23468                "concept Two = requires(T t) {\n"
23469                "                { t.foo() } -> std::same_as<Bar>;\n"
23470                "              } && requires(T &&t) {\n"
23471                "                     { t.foo() } -> std::same_as<Bar &&>;\n"
23472                "                   };");
23473 
23474   verifyFormat(
23475       "template <typename T>\n"
23476       "concept C = requires(T x) {\n"
23477       "              { *x } -> std::convertible_to<typename T::inner>;\n"
23478       "              { x + 1 } noexcept -> std::same_as<int>;\n"
23479       "              { x * 1 } -> std::convertible_to<T>;\n"
23480       "            };");
23481 
23482   verifyFormat(
23483       "template <typename T, typename U = T>\n"
23484       "concept Swappable = requires(T &&t, U &&u) {\n"
23485       "                      swap(std::forward<T>(t), std::forward<U>(u));\n"
23486       "                      swap(std::forward<U>(u), std::forward<T>(t));\n"
23487       "                    };");
23488 
23489   verifyFormat("template <typename T, typename U>\n"
23490                "concept Common = requires(T &&t, U &&u) {\n"
23491                "                   typename CommonType<T, U>;\n"
23492                "                   { CommonType<T, U>(std::forward<T>(t)) };\n"
23493                "                 };");
23494 
23495   verifyFormat("template <typename T, typename U>\n"
23496                "concept Common = requires(T &&t, U &&u) {\n"
23497                "                   typename CommonType<T, U>;\n"
23498                "                   { CommonType<T, U>{std::forward<T>(t)} };\n"
23499                "                 };");
23500 
23501   verifyFormat(
23502       "template <typename T>\n"
23503       "concept C = requires(T t) {\n"
23504       "              requires Bar<T> && Foo<T>;\n"
23505       "              requires((trait<T> && Baz) || (T2<T> && Foo<T>));\n"
23506       "            };");
23507 
23508   verifyFormat("template <typename T>\n"
23509                "concept HasFoo = requires(T t) {\n"
23510                "                   { t.foo() };\n"
23511                "                   t.foo();\n"
23512                "                 };\n"
23513                "template <typename T>\n"
23514                "concept HasBar = requires(T t) {\n"
23515                "                   { t.bar() };\n"
23516                "                   t.bar();\n"
23517                "                 };");
23518 
23519   verifyFormat("template <typename T>\n"
23520                "concept Large = sizeof(T) > 10;");
23521 
23522   verifyFormat("template <typename T, typename U>\n"
23523                "concept FooableWith = requires(T t, U u) {\n"
23524                "                        typename T::foo_type;\n"
23525                "                        { t.foo(u) } -> typename T::foo_type;\n"
23526                "                        t++;\n"
23527                "                      };\n"
23528                "void doFoo(FooableWith<int> auto t) { t.foo(3); }");
23529 
23530   verifyFormat("template <typename T>\n"
23531                "concept Context = is_specialization_of_v<context, T>;");
23532 
23533   verifyFormat("template <typename T>\n"
23534                "concept Node = std::is_object_v<T>;");
23535 
23536   auto Style = getLLVMStyle();
23537   Style.BreakBeforeConceptDeclarations = FormatStyle::BBCDS_Allowed;
23538 
23539   verifyFormat(
23540       "template <typename T>\n"
23541       "concept C = requires(T t) {\n"
23542       "              requires Bar<T> && Foo<T>;\n"
23543       "              requires((trait<T> && Baz) || (T2<T> && Foo<T>));\n"
23544       "            };",
23545       Style);
23546 
23547   verifyFormat("template <typename T>\n"
23548                "concept HasFoo = requires(T t) {\n"
23549                "                   { t.foo() };\n"
23550                "                   t.foo();\n"
23551                "                 };\n"
23552                "template <typename T>\n"
23553                "concept HasBar = requires(T t) {\n"
23554                "                   { t.bar() };\n"
23555                "                   t.bar();\n"
23556                "                 };",
23557                Style);
23558 
23559   verifyFormat("template <typename T> concept True = true;", Style);
23560 
23561   verifyFormat("template <typename T>\n"
23562                "concept C = decltype([]() -> std::true_type { return {}; "
23563                "}())::value &&\n"
23564                "            requires(T t) { t.bar(); } && sizeof(T) <= 8;",
23565                Style);
23566 
23567   verifyFormat("template <typename T>\n"
23568                "concept Semiregular =\n"
23569                "    DefaultConstructible<T> && CopyConstructible<T> && "
23570                "CopyAssignable<T> &&\n"
23571                "    requires(T a, std::size_t n) {\n"
23572                "      requires Same<T *, decltype(&a)>;\n"
23573                "      { a.~T() } noexcept;\n"
23574                "      requires Same<T *, decltype(new T)>;\n"
23575                "      requires Same<T *, decltype(new T[n])>;\n"
23576                "      { delete new T; };\n"
23577                "      { delete new T[n]; };\n"
23578                "    };",
23579                Style);
23580 
23581   Style.BreakBeforeConceptDeclarations = FormatStyle::BBCDS_Never;
23582 
23583   verifyFormat("template <typename T> concept C =\n"
23584                "    requires(T t) {\n"
23585                "      requires Bar<T> && Foo<T>;\n"
23586                "      requires((trait<T> && Baz) || (T2<T> && Foo<T>));\n"
23587                "    };",
23588                Style);
23589 
23590   verifyFormat("template <typename T> concept HasFoo = requires(T t) {\n"
23591                "                                         { t.foo() };\n"
23592                "                                         t.foo();\n"
23593                "                                       };\n"
23594                "template <typename T> concept HasBar = requires(T t) {\n"
23595                "                                         { t.bar() };\n"
23596                "                                         t.bar();\n"
23597                "                                       };",
23598                Style);
23599 
23600   verifyFormat("template <typename T> concept True = true;", Style);
23601 
23602   verifyFormat(
23603       "template <typename T> concept C = decltype([]() -> std::true_type {\n"
23604       "                                    return {};\n"
23605       "                                  }())::value\n"
23606       "                                  && requires(T t) { t.bar(); } &&\n"
23607       "                                  sizeof(T) <= 8;",
23608       Style);
23609 
23610   verifyFormat("template <typename T> concept Semiregular =\n"
23611                "    DefaultConstructible<T> && CopyConstructible<T> && "
23612                "CopyAssignable<T> &&\n"
23613                "    requires(T a, std::size_t n) {\n"
23614                "      requires Same<T *, decltype(&a)>;\n"
23615                "      { a.~T() } noexcept;\n"
23616                "      requires Same<T *, decltype(new T)>;\n"
23617                "      requires Same<T *, decltype(new T[n])>;\n"
23618                "      { delete new T; };\n"
23619                "      { delete new T[n]; };\n"
23620                "    };",
23621                Style);
23622 
23623   // The following tests are invalid C++, we just want to make sure we don't
23624   // assert.
23625   verifyFormat("template <typename T>\n"
23626                "concept C = requires C2<T>;");
23627 
23628   verifyFormat("template <typename T>\n"
23629                "concept C = 5 + 4;");
23630 
23631   verifyFormat("template <typename T>\n"
23632                "concept C =\n"
23633                "class X;");
23634 
23635   verifyFormat("template <typename T>\n"
23636                "concept C = [] && true;");
23637 
23638   verifyFormat("template <typename T>\n"
23639                "concept C = [] && requires(T t) { typename T::size_type; };");
23640 }
23641 
23642 TEST_F(FormatTest, RequiresClauses) {
23643   auto Style = getLLVMStyle();
23644   EXPECT_EQ(Style.RequiresClausePosition, FormatStyle::RCPS_OwnLine);
23645   EXPECT_EQ(Style.IndentRequiresClause, true);
23646 
23647   verifyFormat("template <typename T>\n"
23648                "  requires(Foo<T> && std::trait<T>)\n"
23649                "struct Bar;",
23650                Style);
23651 
23652   verifyFormat("template <typename T>\n"
23653                "  requires(Foo<T> && std::trait<T>)\n"
23654                "class Bar {\n"
23655                "public:\n"
23656                "  Bar(T t);\n"
23657                "  bool baz();\n"
23658                "};",
23659                Style);
23660 
23661   verifyFormat(
23662       "template <typename T>\n"
23663       "  requires requires(T &&t) {\n"
23664       "             typename T::I;\n"
23665       "             requires(F<typename T::I> && std::trait<typename T::I>);\n"
23666       "           }\n"
23667       "Bar(T) -> Bar<typename T::I>;",
23668       Style);
23669 
23670   verifyFormat("template <typename T>\n"
23671                "  requires(Foo<T> && std::trait<T>)\n"
23672                "constexpr T MyGlobal;",
23673                Style);
23674 
23675   verifyFormat("template <typename T>\n"
23676                "  requires Foo<T> && requires(T t) {\n"
23677                "                       { t.baz() } -> std::same_as<bool>;\n"
23678                "                       requires std::same_as<T::Factor, int>;\n"
23679                "                     }\n"
23680                "inline int bar(T t) {\n"
23681                "  return t.baz() ? T::Factor : 5;\n"
23682                "}",
23683                Style);
23684 
23685   verifyFormat("template <typename T>\n"
23686                "inline int bar(T t)\n"
23687                "  requires Foo<T> && requires(T t) {\n"
23688                "                       { t.baz() } -> std::same_as<bool>;\n"
23689                "                       requires std::same_as<T::Factor, int>;\n"
23690                "                     }\n"
23691                "{\n"
23692                "  return t.baz() ? T::Factor : 5;\n"
23693                "}",
23694                Style);
23695 
23696   verifyFormat("template <typename T>\n"
23697                "  requires F<T>\n"
23698                "int bar(T t) {\n"
23699                "  return 5;\n"
23700                "}",
23701                Style);
23702 
23703   verifyFormat("template <typename T>\n"
23704                "int bar(T t)\n"
23705                "  requires F<T>\n"
23706                "{\n"
23707                "  return 5;\n"
23708                "}",
23709                Style);
23710 
23711   Style.IndentRequiresClause = false;
23712   verifyFormat("template <typename T>\n"
23713                "requires F<T>\n"
23714                "int bar(T t) {\n"
23715                "  return 5;\n"
23716                "}",
23717                Style);
23718 
23719   verifyFormat("template <typename T>\n"
23720                "int bar(T t)\n"
23721                "requires F<T>\n"
23722                "{\n"
23723                "  return 5;\n"
23724                "}",
23725                Style);
23726 
23727   Style.RequiresClausePosition = FormatStyle::RCPS_SingleLine;
23728   verifyFormat("template <typename T> requires Foo<T> struct Bar {};\n"
23729                "template <typename T> requires Foo<T> void bar() {}\n"
23730                "template <typename T> void bar() requires Foo<T> {}\n"
23731                "template <typename T> requires Foo<T> Bar(T) -> Bar<T>;",
23732                Style);
23733 
23734   auto ColumnStyle = Style;
23735   ColumnStyle.ColumnLimit = 40;
23736   verifyFormat("template <typename AAAAAAA>\n"
23737                "requires Foo<T> struct Bar {};\n"
23738                "template <typename AAAAAAA>\n"
23739                "requires Foo<T> void bar() {}\n"
23740                "template <typename AAAAAAA>\n"
23741                "void bar() requires Foo<T> {}\n"
23742                "template <typename AAAAAAA>\n"
23743                "requires Foo<T> Baz(T) -> Baz<T>;",
23744                ColumnStyle);
23745 
23746   verifyFormat("template <typename T>\n"
23747                "requires Foo<AAAAAAA> struct Bar {};\n"
23748                "template <typename T>\n"
23749                "requires Foo<AAAAAAA> void bar() {}\n"
23750                "template <typename T>\n"
23751                "void bar() requires Foo<AAAAAAA> {}\n"
23752                "template <typename T>\n"
23753                "requires Foo<AAAAAAA> Bar(T) -> Bar<T>;",
23754                ColumnStyle);
23755 
23756   verifyFormat("template <typename AAAAAAA>\n"
23757                "requires Foo<AAAAAAAAAAAAAAAA>\n"
23758                "struct Bar {};\n"
23759                "template <typename AAAAAAA>\n"
23760                "requires Foo<AAAAAAAAAAAAAAAA>\n"
23761                "void bar() {}\n"
23762                "template <typename AAAAAAA>\n"
23763                "void bar()\n"
23764                "    requires Foo<AAAAAAAAAAAAAAAA> {}\n"
23765                "template <typename AAAAAAA>\n"
23766                "requires Foo<AAAAAAAA> Bar(T) -> Bar<T>;\n"
23767                "template <typename AAAAAAA>\n"
23768                "requires Foo<AAAAAAAAAAAAAAAA>\n"
23769                "Bar(T) -> Bar<T>;",
23770                ColumnStyle);
23771 
23772   Style.RequiresClausePosition = FormatStyle::RCPS_WithFollowing;
23773   ColumnStyle.RequiresClausePosition = FormatStyle::RCPS_WithFollowing;
23774 
23775   verifyFormat("template <typename T>\n"
23776                "requires Foo<T> struct Bar {};\n"
23777                "template <typename T>\n"
23778                "requires Foo<T> void bar() {}\n"
23779                "template <typename T>\n"
23780                "void bar()\n"
23781                "requires Foo<T> {}\n"
23782                "template <typename T>\n"
23783                "requires Foo<T> Bar(T) -> Bar<T>;",
23784                Style);
23785 
23786   verifyFormat("template <typename AAAAAAA>\n"
23787                "requires Foo<AAAAAAAAAAAAAAAA>\n"
23788                "struct Bar {};\n"
23789                "template <typename AAAAAAA>\n"
23790                "requires Foo<AAAAAAAAAAAAAAAA>\n"
23791                "void bar() {}\n"
23792                "template <typename AAAAAAA>\n"
23793                "void bar()\n"
23794                "requires Foo<AAAAAAAAAAAAAAAA> {}\n"
23795                "template <typename AAAAAAA>\n"
23796                "requires Foo<AAAAAAAA> Bar(T) -> Bar<T>;\n"
23797                "template <typename AAAAAAA>\n"
23798                "requires Foo<AAAAAAAAAAAAAAAA>\n"
23799                "Bar(T) -> Bar<T>;",
23800                ColumnStyle);
23801 
23802   Style.IndentRequiresClause = true;
23803   ColumnStyle.IndentRequiresClause = true;
23804 
23805   verifyFormat("template <typename T>\n"
23806                "  requires Foo<T> struct Bar {};\n"
23807                "template <typename T>\n"
23808                "  requires Foo<T> void bar() {}\n"
23809                "template <typename T>\n"
23810                "void bar()\n"
23811                "  requires Foo<T> {}\n"
23812                "template <typename T>\n"
23813                "  requires Foo<T> Bar(T) -> Bar<T>;",
23814                Style);
23815 
23816   verifyFormat("template <typename AAAAAAA>\n"
23817                "  requires Foo<AAAAAAAAAAAAAAAA>\n"
23818                "struct Bar {};\n"
23819                "template <typename AAAAAAA>\n"
23820                "  requires Foo<AAAAAAAAAAAAAAAA>\n"
23821                "void bar() {}\n"
23822                "template <typename AAAAAAA>\n"
23823                "void bar()\n"
23824                "  requires Foo<AAAAAAAAAAAAAAAA> {}\n"
23825                "template <typename AAAAAAA>\n"
23826                "  requires Foo<AAAAAA> Bar(T) -> Bar<T>;\n"
23827                "template <typename AAAAAAA>\n"
23828                "  requires Foo<AAAAAAAAAAAAAAAA>\n"
23829                "Bar(T) -> Bar<T>;",
23830                ColumnStyle);
23831 
23832   Style.RequiresClausePosition = FormatStyle::RCPS_WithPreceding;
23833   ColumnStyle.RequiresClausePosition = FormatStyle::RCPS_WithPreceding;
23834 
23835   verifyFormat("template <typename T> requires Foo<T>\n"
23836                "struct Bar {};\n"
23837                "template <typename T> requires Foo<T>\n"
23838                "void bar() {}\n"
23839                "template <typename T>\n"
23840                "void bar() requires Foo<T>\n"
23841                "{}\n"
23842                "template <typename T> requires Foo<T>\n"
23843                "Bar(T) -> Bar<T>;",
23844                Style);
23845 
23846   verifyFormat("template <typename AAAAAAA>\n"
23847                "requires Foo<AAAAAAAAAAAAAAAA>\n"
23848                "struct Bar {};\n"
23849                "template <typename AAAAAAA>\n"
23850                "requires Foo<AAAAAAAAAAAAAAAA>\n"
23851                "void bar() {}\n"
23852                "template <typename AAAAAAA>\n"
23853                "void bar()\n"
23854                "    requires Foo<AAAAAAAAAAAAAAAA>\n"
23855                "{}\n"
23856                "template <typename AAAAAAA>\n"
23857                "requires Foo<AAAAAAAA>\n"
23858                "Bar(T) -> Bar<T>;\n"
23859                "template <typename AAAAAAA>\n"
23860                "requires Foo<AAAAAAAAAAAAAAAA>\n"
23861                "Bar(T) -> Bar<T>;",
23862                ColumnStyle);
23863 }
23864 
23865 TEST_F(FormatTest, StatementAttributeLikeMacros) {
23866   FormatStyle Style = getLLVMStyle();
23867   StringRef Source = "void Foo::slot() {\n"
23868                      "  unsigned char MyChar = 'x';\n"
23869                      "  emit signal(MyChar);\n"
23870                      "  Q_EMIT signal(MyChar);\n"
23871                      "}";
23872 
23873   EXPECT_EQ(Source, format(Source, Style));
23874 
23875   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
23876   EXPECT_EQ("void Foo::slot() {\n"
23877             "  unsigned char MyChar = 'x';\n"
23878             "  emit          signal(MyChar);\n"
23879             "  Q_EMIT signal(MyChar);\n"
23880             "}",
23881             format(Source, Style));
23882 
23883   Style.StatementAttributeLikeMacros.push_back("emit");
23884   EXPECT_EQ(Source, format(Source, Style));
23885 
23886   Style.StatementAttributeLikeMacros = {};
23887   EXPECT_EQ("void Foo::slot() {\n"
23888             "  unsigned char MyChar = 'x';\n"
23889             "  emit          signal(MyChar);\n"
23890             "  Q_EMIT        signal(MyChar);\n"
23891             "}",
23892             format(Source, Style));
23893 }
23894 
23895 TEST_F(FormatTest, IndentAccessModifiers) {
23896   FormatStyle Style = getLLVMStyle();
23897   Style.IndentAccessModifiers = true;
23898   // Members are *two* levels below the record;
23899   // Style.IndentWidth == 2, thus yielding a 4 spaces wide indentation.
23900   verifyFormat("class C {\n"
23901                "    int i;\n"
23902                "};\n",
23903                Style);
23904   verifyFormat("union C {\n"
23905                "    int i;\n"
23906                "    unsigned u;\n"
23907                "};\n",
23908                Style);
23909   // Access modifiers should be indented one level below the record.
23910   verifyFormat("class C {\n"
23911                "  public:\n"
23912                "    int i;\n"
23913                "};\n",
23914                Style);
23915   verifyFormat("struct S {\n"
23916                "  private:\n"
23917                "    class C {\n"
23918                "        int j;\n"
23919                "\n"
23920                "      public:\n"
23921                "        C();\n"
23922                "    };\n"
23923                "\n"
23924                "  public:\n"
23925                "    int i;\n"
23926                "};\n",
23927                Style);
23928   // Enumerations are not records and should be unaffected.
23929   Style.AllowShortEnumsOnASingleLine = false;
23930   verifyFormat("enum class E {\n"
23931                "  A,\n"
23932                "  B\n"
23933                "};\n",
23934                Style);
23935   // Test with a different indentation width;
23936   // also proves that the result is Style.AccessModifierOffset agnostic.
23937   Style.IndentWidth = 3;
23938   verifyFormat("class C {\n"
23939                "   public:\n"
23940                "      int i;\n"
23941                "};\n",
23942                Style);
23943 }
23944 
23945 TEST_F(FormatTest, LimitlessStringsAndComments) {
23946   auto Style = getLLVMStyleWithColumns(0);
23947   constexpr StringRef Code =
23948       "/**\n"
23949       " * This is a multiline comment with quite some long lines, at least for "
23950       "the LLVM Style.\n"
23951       " * We will redo this with strings and line comments. Just to  check if "
23952       "everything is working.\n"
23953       " */\n"
23954       "bool foo() {\n"
23955       "  /* Single line multi line comment. */\n"
23956       "  const std::string String = \"This is a multiline string with quite "
23957       "some long lines, at least for the LLVM Style.\"\n"
23958       "                             \"We already did it with multi line "
23959       "comments, and we will do it with line comments. Just to check if "
23960       "everything is working.\";\n"
23961       "  // This is a line comment (block) with quite some long lines, at "
23962       "least for the LLVM Style.\n"
23963       "  // We already did this with multi line comments and strings. Just to "
23964       "check if everything is working.\n"
23965       "  const std::string SmallString = \"Hello World\";\n"
23966       "  // Small line comment\n"
23967       "  return String.size() > SmallString.size();\n"
23968       "}";
23969   EXPECT_EQ(Code, format(Code, Style));
23970 }
23971 
23972 TEST_F(FormatTest, FormatDecayCopy) {
23973   // error cases from unit tests
23974   verifyFormat("foo(auto())");
23975   verifyFormat("foo(auto{})");
23976   verifyFormat("foo(auto({}))");
23977   verifyFormat("foo(auto{{}})");
23978 
23979   verifyFormat("foo(auto(1))");
23980   verifyFormat("foo(auto{1})");
23981   verifyFormat("foo(new auto(1))");
23982   verifyFormat("foo(new auto{1})");
23983   verifyFormat("decltype(auto(1)) x;");
23984   verifyFormat("decltype(auto{1}) x;");
23985   verifyFormat("auto(x);");
23986   verifyFormat("auto{x};");
23987   verifyFormat("new auto{x};");
23988   verifyFormat("auto{x} = y;");
23989   verifyFormat("auto(x) = y;"); // actually a declaration, but this is clearly
23990                                 // the user's own fault
23991   verifyFormat("integral auto(x) = y;"); // actually a declaration, but this is
23992                                          // clearly the user's own fault
23993   verifyFormat("auto(*p)() = f;");       // actually a declaration; TODO FIXME
23994 }
23995 
23996 TEST_F(FormatTest, Cpp20ModulesSupport) {
23997   FormatStyle Style = getLLVMStyle();
23998   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
23999   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
24000 
24001   verifyFormat("export import foo;", Style);
24002   verifyFormat("export import foo:bar;", Style);
24003   verifyFormat("export import foo.bar;", Style);
24004   verifyFormat("export import foo.bar:baz;", Style);
24005   verifyFormat("export import :bar;", Style);
24006   verifyFormat("export module foo:bar;", Style);
24007   verifyFormat("export module foo;", Style);
24008   verifyFormat("export module foo.bar;", Style);
24009   verifyFormat("export module foo.bar:baz;", Style);
24010   verifyFormat("export import <string_view>;", Style);
24011 
24012   verifyFormat("export type_name var;", Style);
24013   verifyFormat("template <class T> export using A = B<T>;", Style);
24014   verifyFormat("export using A = B;", Style);
24015   verifyFormat("export int func() {\n"
24016                "  foo();\n"
24017                "}",
24018                Style);
24019   verifyFormat("export struct {\n"
24020                "  int foo;\n"
24021                "};",
24022                Style);
24023   verifyFormat("export {\n"
24024                "  int foo;\n"
24025                "};",
24026                Style);
24027   verifyFormat("export export char const *hello() { return \"hello\"; }");
24028 
24029   verifyFormat("import bar;", Style);
24030   verifyFormat("import foo.bar;", Style);
24031   verifyFormat("import foo:bar;", Style);
24032   verifyFormat("import :bar;", Style);
24033   verifyFormat("import <ctime>;", Style);
24034   verifyFormat("import \"header\";", Style);
24035 
24036   verifyFormat("module foo;", Style);
24037   verifyFormat("module foo:bar;", Style);
24038   verifyFormat("module foo.bar;", Style);
24039   verifyFormat("module;", Style);
24040 
24041   verifyFormat("export namespace hi {\n"
24042                "const char *sayhi();\n"
24043                "}",
24044                Style);
24045 
24046   verifyFormat("module :private;", Style);
24047   verifyFormat("import <foo/bar.h>;", Style);
24048   verifyFormat("import foo...bar;", Style);
24049   verifyFormat("import ..........;", Style);
24050   verifyFormat("module foo:private;", Style);
24051   verifyFormat("import a", Style);
24052   verifyFormat("module a", Style);
24053   verifyFormat("export import a", Style);
24054   verifyFormat("export module a", Style);
24055 
24056   verifyFormat("import", Style);
24057   verifyFormat("module", Style);
24058   verifyFormat("export", Style);
24059 }
24060 
24061 TEST_F(FormatTest, CoroutineForCoawait) {
24062   FormatStyle Style = getLLVMStyle();
24063   verifyFormat("for co_await (auto x : range())\n  ;");
24064   verifyFormat("for (auto i : arr) {\n"
24065                "}",
24066                Style);
24067   verifyFormat("for co_await (auto i : arr) {\n"
24068                "}",
24069                Style);
24070   verifyFormat("for co_await (auto i : foo(T{})) {\n"
24071                "}",
24072                Style);
24073 }
24074 
24075 TEST_F(FormatTest, CoroutineCoAwait) {
24076   verifyFormat("int x = co_await foo();");
24077   verifyFormat("int x = (co_await foo());");
24078   verifyFormat("co_await (42);");
24079   verifyFormat("void operator co_await(int);");
24080   verifyFormat("void operator co_await(a);");
24081   verifyFormat("co_await a;");
24082   verifyFormat("co_await missing_await_resume{};");
24083   verifyFormat("co_await a; // comment");
24084   verifyFormat("void test0() { co_await a; }");
24085   verifyFormat("co_await co_await co_await foo();");
24086   verifyFormat("co_await foo().bar();");
24087   verifyFormat("co_await [this]() -> Task { co_return x; }");
24088   verifyFormat("co_await [this](int a, int b) -> Task { co_return co_await "
24089                "foo(); }(x, y);");
24090 
24091   FormatStyle Style = getLLVMStyleWithColumns(40);
24092   verifyFormat("co_await [this](int a, int b) -> Task {\n"
24093                "  co_return co_await foo();\n"
24094                "}(x, y);",
24095                Style);
24096   verifyFormat("co_await;");
24097 }
24098 
24099 TEST_F(FormatTest, CoroutineCoYield) {
24100   verifyFormat("int x = co_yield foo();");
24101   verifyFormat("int x = (co_yield foo());");
24102   verifyFormat("co_yield (42);");
24103   verifyFormat("co_yield {42};");
24104   verifyFormat("co_yield 42;");
24105   verifyFormat("co_yield n++;");
24106   verifyFormat("co_yield ++n;");
24107   verifyFormat("co_yield;");
24108 }
24109 
24110 TEST_F(FormatTest, CoroutineCoReturn) {
24111   verifyFormat("co_return (42);");
24112   verifyFormat("co_return;");
24113   verifyFormat("co_return {};");
24114   verifyFormat("co_return x;");
24115   verifyFormat("co_return co_await foo();");
24116   verifyFormat("co_return co_yield foo();");
24117 }
24118 
24119 TEST_F(FormatTest, EmptyShortBlock) {
24120   auto Style = getLLVMStyle();
24121   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty;
24122 
24123   verifyFormat("try {\n"
24124                "  doA();\n"
24125                "} catch (Exception &e) {\n"
24126                "  e.printStackTrace();\n"
24127                "}\n",
24128                Style);
24129 
24130   verifyFormat("try {\n"
24131                "  doA();\n"
24132                "} catch (Exception &e) {}\n",
24133                Style);
24134 }
24135 
24136 TEST_F(FormatTest, ShortTemplatedArgumentLists) {
24137   auto Style = getLLVMStyle();
24138 
24139   verifyFormat("template <> struct S : Template<int (*)[]> {};\n", Style);
24140   verifyFormat("template <> struct S : Template<int (*)[10]> {};\n", Style);
24141   verifyFormat("struct Y : X<[] { return 0; }> {};", Style);
24142   verifyFormat("struct Y<[] { return 0; }> {};", Style);
24143 
24144   verifyFormat("struct Z : X<decltype([] { return 0; }){}> {};", Style);
24145   verifyFormat("template <int N> struct Foo<char[N]> {};", Style);
24146 }
24147 
24148 TEST_F(FormatTest, RemoveBraces) {
24149   FormatStyle Style = getLLVMStyle();
24150   Style.RemoveBracesLLVM = true;
24151 
24152   // The following eight test cases are fully-braced versions of the examples at
24153   // "llvm.org/docs/CodingStandards.html#don-t-use-braces-on-simple-single-
24154   // statement-bodies-of-if-else-loop-statements".
24155 
24156   // 1. Omit the braces, since the body is simple and clearly associated with
24157   // the if.
24158   verifyFormat("if (isa<FunctionDecl>(D))\n"
24159                "  handleFunctionDecl(D);\n"
24160                "else if (isa<VarDecl>(D))\n"
24161                "  handleVarDecl(D);",
24162                "if (isa<FunctionDecl>(D)) {\n"
24163                "  handleFunctionDecl(D);\n"
24164                "} else if (isa<VarDecl>(D)) {\n"
24165                "  handleVarDecl(D);\n"
24166                "}",
24167                Style);
24168 
24169   // 2. Here we document the condition itself and not the body.
24170   verifyFormat("if (isa<VarDecl>(D)) {\n"
24171                "  // It is necessary that we explain the situation with this\n"
24172                "  // surprisingly long comment, so it would be unclear\n"
24173                "  // without the braces whether the following statement is in\n"
24174                "  // the scope of the `if`.\n"
24175                "  // Because the condition is documented, we can't really\n"
24176                "  // hoist this comment that applies to the body above the\n"
24177                "  // if.\n"
24178                "  handleOtherDecl(D);\n"
24179                "}",
24180                Style);
24181 
24182   // 3. Use braces on the outer `if` to avoid a potential dangling else
24183   // situation.
24184   verifyFormat("if (isa<VarDecl>(D)) {\n"
24185                "  for (auto *A : D.attrs())\n"
24186                "    if (shouldProcessAttr(A))\n"
24187                "      handleAttr(A);\n"
24188                "}",
24189                "if (isa<VarDecl>(D)) {\n"
24190                "  for (auto *A : D.attrs()) {\n"
24191                "    if (shouldProcessAttr(A)) {\n"
24192                "      handleAttr(A);\n"
24193                "    }\n"
24194                "  }\n"
24195                "}",
24196                Style);
24197 
24198   // 4. Use braces for the `if` block to keep it uniform with the else block.
24199   verifyFormat("if (isa<FunctionDecl>(D)) {\n"
24200                "  handleFunctionDecl(D);\n"
24201                "} else {\n"
24202                "  // In this else case, it is necessary that we explain the\n"
24203                "  // situation with this surprisingly long comment, so it\n"
24204                "  // would be unclear without the braces whether the\n"
24205                "  // following statement is in the scope of the `if`.\n"
24206                "  handleOtherDecl(D);\n"
24207                "}",
24208                Style);
24209 
24210   // 5. This should also omit braces.  The `for` loop contains only a single
24211   // statement, so it shouldn't have braces.  The `if` also only contains a
24212   // single simple statement (the for loop), so it also should omit braces.
24213   verifyFormat("if (isa<FunctionDecl>(D))\n"
24214                "  for (auto *A : D.attrs())\n"
24215                "    handleAttr(A);",
24216                "if (isa<FunctionDecl>(D)) {\n"
24217                "  for (auto *A : D.attrs()) {\n"
24218                "    handleAttr(A);\n"
24219                "  }\n"
24220                "}",
24221                Style);
24222 
24223   // 6. Use braces for the outer `if` since the nested `for` is braced.
24224   verifyFormat("if (isa<FunctionDecl>(D)) {\n"
24225                "  for (auto *A : D.attrs()) {\n"
24226                "    // In this for loop body, it is necessary that we explain\n"
24227                "    // the situation with this surprisingly long comment,\n"
24228                "    // forcing braces on the `for` block.\n"
24229                "    handleAttr(A);\n"
24230                "  }\n"
24231                "}",
24232                Style);
24233 
24234   // 7. Use braces on the outer block because there are more than two levels of
24235   // nesting.
24236   verifyFormat("if (isa<FunctionDecl>(D)) {\n"
24237                "  for (auto *A : D.attrs())\n"
24238                "    for (ssize_t i : llvm::seq<ssize_t>(count))\n"
24239                "      handleAttrOnDecl(D, A, i);\n"
24240                "}",
24241                "if (isa<FunctionDecl>(D)) {\n"
24242                "  for (auto *A : D.attrs()) {\n"
24243                "    for (ssize_t i : llvm::seq<ssize_t>(count)) {\n"
24244                "      handleAttrOnDecl(D, A, i);\n"
24245                "    }\n"
24246                "  }\n"
24247                "}",
24248                Style);
24249 
24250   // 8. Use braces on the outer block because of a nested `if`, otherwise the
24251   // compiler would warn: `add explicit braces to avoid dangling else`
24252   verifyFormat("if (auto *D = dyn_cast<FunctionDecl>(D)) {\n"
24253                "  if (shouldProcess(D))\n"
24254                "    handleVarDecl(D);\n"
24255                "  else\n"
24256                "    markAsIgnored(D);\n"
24257                "}",
24258                "if (auto *D = dyn_cast<FunctionDecl>(D)) {\n"
24259                "  if (shouldProcess(D)) {\n"
24260                "    handleVarDecl(D);\n"
24261                "  } else {\n"
24262                "    markAsIgnored(D);\n"
24263                "  }\n"
24264                "}",
24265                Style);
24266 
24267   verifyFormat("if (a)\n"
24268                "  b; // comment\n"
24269                "else if (c)\n"
24270                "  d; /* comment */\n"
24271                "else\n"
24272                "  e;",
24273                "if (a) {\n"
24274                "  b; // comment\n"
24275                "} else if (c) {\n"
24276                "  d; /* comment */\n"
24277                "} else {\n"
24278                "  e;\n"
24279                "}",
24280                Style);
24281 
24282   verifyFormat("if (a) {\n"
24283                "  b;\n"
24284                "  c;\n"
24285                "} else if (d) {\n"
24286                "  e;\n"
24287                "}",
24288                Style);
24289 
24290   verifyFormat("if (a) {\n"
24291                "#undef NDEBUG\n"
24292                "  b;\n"
24293                "} else {\n"
24294                "  c;\n"
24295                "}",
24296                Style);
24297 
24298   verifyFormat("if (a) {\n"
24299                "  // comment\n"
24300                "} else if (b) {\n"
24301                "  c;\n"
24302                "}",
24303                Style);
24304 
24305   verifyFormat("if (a) {\n"
24306                "  b;\n"
24307                "} else {\n"
24308                "  { c; }\n"
24309                "}",
24310                Style);
24311 
24312   verifyFormat("if (a) {\n"
24313                "  if (b) // comment\n"
24314                "    c;\n"
24315                "} else if (d) {\n"
24316                "  e;\n"
24317                "}",
24318                "if (a) {\n"
24319                "  if (b) { // comment\n"
24320                "    c;\n"
24321                "  }\n"
24322                "} else if (d) {\n"
24323                "  e;\n"
24324                "}",
24325                Style);
24326 
24327   verifyFormat("if (a) {\n"
24328                "  if (b) {\n"
24329                "    c;\n"
24330                "    // comment\n"
24331                "  } else if (d) {\n"
24332                "    e;\n"
24333                "  }\n"
24334                "}",
24335                Style);
24336 
24337   verifyFormat("if (a) {\n"
24338                "  if (b)\n"
24339                "    c;\n"
24340                "}",
24341                "if (a) {\n"
24342                "  if (b) {\n"
24343                "    c;\n"
24344                "  }\n"
24345                "}",
24346                Style);
24347 
24348   verifyFormat("if (a)\n"
24349                "  if (b)\n"
24350                "    c;\n"
24351                "  else\n"
24352                "    d;\n"
24353                "else\n"
24354                "  e;",
24355                "if (a) {\n"
24356                "  if (b) {\n"
24357                "    c;\n"
24358                "  } else {\n"
24359                "    d;\n"
24360                "  }\n"
24361                "} else {\n"
24362                "  e;\n"
24363                "}",
24364                Style);
24365 
24366   verifyFormat("if (a) {\n"
24367                "  // comment\n"
24368                "  if (b)\n"
24369                "    c;\n"
24370                "  else if (d)\n"
24371                "    e;\n"
24372                "} else {\n"
24373                "  g;\n"
24374                "}",
24375                "if (a) {\n"
24376                "  // comment\n"
24377                "  if (b) {\n"
24378                "    c;\n"
24379                "  } else if (d) {\n"
24380                "    e;\n"
24381                "  }\n"
24382                "} else {\n"
24383                "  g;\n"
24384                "}",
24385                Style);
24386 
24387   verifyFormat("if (a)\n"
24388                "  b;\n"
24389                "else if (c)\n"
24390                "  d;\n"
24391                "else\n"
24392                "  e;",
24393                "if (a) {\n"
24394                "  b;\n"
24395                "} else {\n"
24396                "  if (c) {\n"
24397                "    d;\n"
24398                "  } else {\n"
24399                "    e;\n"
24400                "  }\n"
24401                "}",
24402                Style);
24403 
24404   verifyFormat("if (a) {\n"
24405                "  if (b)\n"
24406                "    c;\n"
24407                "  else if (d)\n"
24408                "    e;\n"
24409                "} else {\n"
24410                "  g;\n"
24411                "}",
24412                "if (a) {\n"
24413                "  if (b)\n"
24414                "    c;\n"
24415                "  else {\n"
24416                "    if (d)\n"
24417                "      e;\n"
24418                "  }\n"
24419                "} else {\n"
24420                "  g;\n"
24421                "}",
24422                Style);
24423 
24424   verifyFormat("if (a)\n"
24425                "  b;\n"
24426                "else if (c)\n"
24427                "  while (d)\n"
24428                "    e;\n"
24429                "// comment",
24430                "if (a)\n"
24431                "{\n"
24432                "  b;\n"
24433                "} else if (c) {\n"
24434                "  while (d) {\n"
24435                "    e;\n"
24436                "  }\n"
24437                "}\n"
24438                "// comment",
24439                Style);
24440 
24441   verifyFormat("if (a) {\n"
24442                "  b;\n"
24443                "} else if (c) {\n"
24444                "  d;\n"
24445                "} else {\n"
24446                "  e;\n"
24447                "  g;\n"
24448                "}",
24449                Style);
24450 
24451   verifyFormat("if (a) {\n"
24452                "  b;\n"
24453                "} else if (c) {\n"
24454                "  d;\n"
24455                "} else {\n"
24456                "  e;\n"
24457                "} // comment",
24458                Style);
24459 
24460   verifyFormat("int abs = [](int i) {\n"
24461                "  if (i >= 0)\n"
24462                "    return i;\n"
24463                "  return -i;\n"
24464                "};",
24465                "int abs = [](int i) {\n"
24466                "  if (i >= 0) {\n"
24467                "    return i;\n"
24468                "  }\n"
24469                "  return -i;\n"
24470                "};",
24471                Style);
24472 
24473   // FIXME: See https://github.com/llvm/llvm-project/issues/53543.
24474 #if 0
24475   Style.ColumnLimit = 65;
24476 
24477   verifyFormat("if (condition) {\n"
24478                "  ff(Indices,\n"
24479                "     [&](unsigned LHSI, unsigned RHSI) { return true; });\n"
24480                "} else {\n"
24481                "  ff(Indices,\n"
24482                "     [&](unsigned LHSI, unsigned RHSI) { return true; });\n"
24483                "}",
24484                Style);
24485 
24486   Style.ColumnLimit = 20;
24487 
24488   verifyFormat("if (a) {\n"
24489                "  b = c + // 1 -\n"
24490                "      d;\n"
24491                "}",
24492                Style);
24493 
24494   verifyFormat("if (a) {\n"
24495                "  b = c >= 0 ? d\n"
24496                "             : e;\n"
24497                "}",
24498                "if (a) {\n"
24499                "  b = c >= 0 ? d : e;\n"
24500                "}",
24501                Style);
24502 #endif
24503 
24504   Style.ColumnLimit = 20;
24505 
24506   verifyFormat("if (a)\n"
24507                "  b = c > 0 ? d : e;",
24508                "if (a) {\n"
24509                "  b = c > 0 ? d : e;\n"
24510                "}",
24511                Style);
24512 
24513   Style.ColumnLimit = 0;
24514 
24515   verifyFormat("if (a)\n"
24516                "  b234567890223456789032345678904234567890 = "
24517                "c234567890223456789032345678904234567890;",
24518                "if (a) {\n"
24519                "  b234567890223456789032345678904234567890 = "
24520                "c234567890223456789032345678904234567890;\n"
24521                "}",
24522                Style);
24523 }
24524 
24525 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndent) {
24526   auto Style = getLLVMStyle();
24527 
24528   StringRef Short = "functionCall(paramA, paramB, paramC);\n"
24529                     "void functionDecl(int a, int b, int c);";
24530 
24531   StringRef Medium = "functionCall(paramA, paramB, paramC, paramD, paramE, "
24532                      "paramF, paramG, paramH, paramI);\n"
24533                      "void functionDecl(int argumentA, int argumentB, int "
24534                      "argumentC, int argumentD, int argumentE);";
24535 
24536   verifyFormat(Short, Style);
24537 
24538   StringRef NoBreak = "functionCall(paramA, paramB, paramC, paramD, paramE, "
24539                       "paramF, paramG, paramH,\n"
24540                       "             paramI);\n"
24541                       "void functionDecl(int argumentA, int argumentB, int "
24542                       "argumentC, int argumentD,\n"
24543                       "                  int argumentE);";
24544 
24545   verifyFormat(NoBreak, Medium, Style);
24546   verifyFormat(NoBreak,
24547                "functionCall(\n"
24548                "    paramA,\n"
24549                "    paramB,\n"
24550                "    paramC,\n"
24551                "    paramD,\n"
24552                "    paramE,\n"
24553                "    paramF,\n"
24554                "    paramG,\n"
24555                "    paramH,\n"
24556                "    paramI\n"
24557                ");\n"
24558                "void functionDecl(\n"
24559                "    int argumentA,\n"
24560                "    int argumentB,\n"
24561                "    int argumentC,\n"
24562                "    int argumentD,\n"
24563                "    int argumentE\n"
24564                ");",
24565                Style);
24566 
24567   verifyFormat("outerFunctionCall(nestedFunctionCall(argument1),\n"
24568                "                  nestedLongFunctionCall(argument1, "
24569                "argument2, argument3,\n"
24570                "                                         argument4, "
24571                "argument5));",
24572                Style);
24573 
24574   Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
24575 
24576   verifyFormat(Short, Style);
24577   verifyFormat(
24578       "functionCall(\n"
24579       "    paramA, paramB, paramC, paramD, paramE, paramF, paramG, paramH, "
24580       "paramI\n"
24581       ");\n"
24582       "void functionDecl(\n"
24583       "    int argumentA, int argumentB, int argumentC, int argumentD, int "
24584       "argumentE\n"
24585       ");",
24586       Medium, Style);
24587 
24588   Style.AllowAllArgumentsOnNextLine = false;
24589   Style.AllowAllParametersOfDeclarationOnNextLine = false;
24590 
24591   verifyFormat(Short, Style);
24592   verifyFormat(
24593       "functionCall(\n"
24594       "    paramA, paramB, paramC, paramD, paramE, paramF, paramG, paramH, "
24595       "paramI\n"
24596       ");\n"
24597       "void functionDecl(\n"
24598       "    int argumentA, int argumentB, int argumentC, int argumentD, int "
24599       "argumentE\n"
24600       ");",
24601       Medium, Style);
24602 
24603   Style.BinPackArguments = false;
24604   Style.BinPackParameters = false;
24605 
24606   verifyFormat(Short, Style);
24607 
24608   verifyFormat("functionCall(\n"
24609                "    paramA,\n"
24610                "    paramB,\n"
24611                "    paramC,\n"
24612                "    paramD,\n"
24613                "    paramE,\n"
24614                "    paramF,\n"
24615                "    paramG,\n"
24616                "    paramH,\n"
24617                "    paramI\n"
24618                ");\n"
24619                "void functionDecl(\n"
24620                "    int argumentA,\n"
24621                "    int argumentB,\n"
24622                "    int argumentC,\n"
24623                "    int argumentD,\n"
24624                "    int argumentE\n"
24625                ");",
24626                Medium, Style);
24627 
24628   verifyFormat("outerFunctionCall(\n"
24629                "    nestedFunctionCall(argument1),\n"
24630                "    nestedLongFunctionCall(\n"
24631                "        argument1,\n"
24632                "        argument2,\n"
24633                "        argument3,\n"
24634                "        argument4,\n"
24635                "        argument5\n"
24636                "    )\n"
24637                ");",
24638                Style);
24639 
24640   verifyFormat("int a = (int)b;", Style);
24641   verifyFormat("int a = (int)b;",
24642                "int a = (\n"
24643                "    int\n"
24644                ") b;",
24645                Style);
24646 
24647   verifyFormat("return (true);", Style);
24648   verifyFormat("return (true);",
24649                "return (\n"
24650                "    true\n"
24651                ");",
24652                Style);
24653 
24654   verifyFormat("void foo();", Style);
24655   verifyFormat("void foo();",
24656                "void foo(\n"
24657                ");",
24658                Style);
24659 
24660   verifyFormat("void foo() {}", Style);
24661   verifyFormat("void foo() {}",
24662                "void foo(\n"
24663                ") {\n"
24664                "}",
24665                Style);
24666 
24667   verifyFormat("auto string = std::string();", Style);
24668   verifyFormat("auto string = std::string();",
24669                "auto string = std::string(\n"
24670                ");",
24671                Style);
24672 
24673   verifyFormat("void (*functionPointer)() = nullptr;", Style);
24674   verifyFormat("void (*functionPointer)() = nullptr;",
24675                "void (\n"
24676                "    *functionPointer\n"
24677                ")\n"
24678                "(\n"
24679                ") = nullptr;",
24680                Style);
24681 }
24682 
24683 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentIfStatement) {
24684   auto Style = getLLVMStyle();
24685 
24686   verifyFormat("if (foo()) {\n"
24687                "  return;\n"
24688                "}",
24689                Style);
24690 
24691   verifyFormat("if (quitelongarg !=\n"
24692                "    (alsolongarg - 1)) { // ABC is a very longgggggggggggg "
24693                "comment\n"
24694                "  return;\n"
24695                "}",
24696                Style);
24697 
24698   Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
24699 
24700   verifyFormat("if (foo()) {\n"
24701                "  return;\n"
24702                "}",
24703                Style);
24704 
24705   verifyFormat("if (quitelongarg !=\n"
24706                "    (alsolongarg - 1)) { // ABC is a very longgggggggggggg "
24707                "comment\n"
24708                "  return;\n"
24709                "}",
24710                Style);
24711 }
24712 
24713 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentForStatement) {
24714   auto Style = getLLVMStyle();
24715 
24716   verifyFormat("for (int i = 0; i < 5; ++i) {\n"
24717                "  doSomething();\n"
24718                "}",
24719                Style);
24720 
24721   verifyFormat("for (int myReallyLongCountVariable = 0; "
24722                "myReallyLongCountVariable < count;\n"
24723                "     myReallyLongCountVariable++) {\n"
24724                "  doSomething();\n"
24725                "}",
24726                Style);
24727 
24728   Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
24729 
24730   verifyFormat("for (int i = 0; i < 5; ++i) {\n"
24731                "  doSomething();\n"
24732                "}",
24733                Style);
24734 
24735   verifyFormat("for (int myReallyLongCountVariable = 0; "
24736                "myReallyLongCountVariable < count;\n"
24737                "     myReallyLongCountVariable++) {\n"
24738                "  doSomething();\n"
24739                "}",
24740                Style);
24741 }
24742 
24743 TEST_F(FormatTest, UnderstandsDigraphs) {
24744   verifyFormat("int arr<:5:> = {};");
24745   verifyFormat("int arr[5] = <%%>;");
24746   verifyFormat("int arr<:::qualified_variable:> = {};");
24747   verifyFormat("int arr[::qualified_variable] = <%%>;");
24748   verifyFormat("%:include <header>");
24749   verifyFormat("%:define A x##y");
24750   verifyFormat("#define A x%:%:y");
24751 }
24752 
24753 } // namespace
24754 } // namespace format
24755 } // namespace clang
24756