1 //===- unittest/Format/FormatTest.cpp - Formatting unit tests -------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "clang/Format/Format.h"
10 
11 #include "../Tooling/ReplacementTest.h"
12 #include "FormatTestUtils.h"
13 
14 #include "llvm/Support/Debug.h"
15 #include "llvm/Support/MemoryBuffer.h"
16 #include "gtest/gtest.h"
17 
18 #define DEBUG_TYPE "format-test"
19 
20 using clang::tooling::ReplacementTest;
21 using clang::tooling::toReplacements;
22 using testing::ScopedTrace;
23 
24 namespace clang {
25 namespace format {
26 namespace {
27 
28 FormatStyle getGoogleStyle() { return getGoogleStyle(FormatStyle::LK_Cpp); }
29 
30 class FormatTest : public ::testing::Test {
31 protected:
32   enum StatusCheck { SC_ExpectComplete, SC_ExpectIncomplete, SC_DoNotCheck };
33 
34   std::string format(llvm::StringRef Code,
35                      const FormatStyle &Style = getLLVMStyle(),
36                      StatusCheck CheckComplete = SC_ExpectComplete) {
37     LLVM_DEBUG(llvm::errs() << "---\n");
38     LLVM_DEBUG(llvm::errs() << Code << "\n\n");
39     std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size()));
40     FormattingAttemptStatus Status;
41     tooling::Replacements Replaces =
42         reformat(Style, Code, Ranges, "<stdin>", &Status);
43     if (CheckComplete != SC_DoNotCheck) {
44       bool ExpectedCompleteFormat = CheckComplete == SC_ExpectComplete;
45       EXPECT_EQ(ExpectedCompleteFormat, Status.FormatComplete)
46           << Code << "\n\n";
47     }
48     ReplacementCount = Replaces.size();
49     auto Result = applyAllReplacements(Code, Replaces);
50     EXPECT_TRUE(static_cast<bool>(Result));
51     LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
52     return *Result;
53   }
54 
55   FormatStyle getStyleWithColumns(FormatStyle Style, unsigned ColumnLimit) {
56     Style.ColumnLimit = ColumnLimit;
57     return Style;
58   }
59 
60   FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) {
61     return getStyleWithColumns(getLLVMStyle(), ColumnLimit);
62   }
63 
64   FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) {
65     return getStyleWithColumns(getGoogleStyle(), ColumnLimit);
66   }
67 
68   void _verifyFormat(const char *File, int Line, llvm::StringRef Expected,
69                      llvm::StringRef Code,
70                      const FormatStyle &Style = getLLVMStyle()) {
71     ScopedTrace t(File, Line, ::testing::Message() << Code.str());
72     EXPECT_EQ(Expected.str(), format(Expected, Style))
73         << "Expected code is not stable";
74     EXPECT_EQ(Expected.str(), format(Code, Style));
75     if (Style.Language == FormatStyle::LK_Cpp) {
76       // Objective-C++ is a superset of C++, so everything checked for C++
77       // needs to be checked for Objective-C++ as well.
78       FormatStyle ObjCStyle = Style;
79       ObjCStyle.Language = FormatStyle::LK_ObjC;
80       EXPECT_EQ(Expected.str(), format(test::messUp(Code), ObjCStyle));
81     }
82   }
83 
84   void _verifyFormat(const char *File, int Line, llvm::StringRef Code,
85                      const FormatStyle &Style = getLLVMStyle()) {
86     _verifyFormat(File, Line, Code, test::messUp(Code), Style);
87   }
88 
89   void _verifyIncompleteFormat(const char *File, int Line, llvm::StringRef Code,
90                                const FormatStyle &Style = getLLVMStyle()) {
91     ScopedTrace t(File, Line, ::testing::Message() << Code.str());
92     EXPECT_EQ(Code.str(),
93               format(test::messUp(Code), Style, SC_ExpectIncomplete));
94   }
95 
96   void _verifyIndependentOfContext(const char *File, int Line,
97                                    llvm::StringRef Text,
98                                    const FormatStyle &Style = getLLVMStyle()) {
99     _verifyFormat(File, Line, Text, Style);
100     _verifyFormat(File, Line, llvm::Twine("void f() { " + Text + " }").str(),
101                   Style);
102   }
103 
104   /// \brief Verify that clang-format does not crash on the given input.
105   void verifyNoCrash(llvm::StringRef Code,
106                      const FormatStyle &Style = getLLVMStyle()) {
107     format(Code, Style, SC_DoNotCheck);
108   }
109 
110   int ReplacementCount;
111 };
112 
113 #define verifyIndependentOfContext(...)                                        \
114   _verifyIndependentOfContext(__FILE__, __LINE__, __VA_ARGS__)
115 #define verifyIncompleteFormat(...)                                            \
116   _verifyIncompleteFormat(__FILE__, __LINE__, __VA_ARGS__)
117 #define verifyFormat(...) _verifyFormat(__FILE__, __LINE__, __VA_ARGS__)
118 #define verifyGoogleFormat(Code) verifyFormat(Code, getGoogleStyle())
119 
120 TEST_F(FormatTest, MessUp) {
121   EXPECT_EQ("1 2 3", test::messUp("1 2 3"));
122   EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n"));
123   EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc"));
124   EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc"));
125   EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne"));
126 }
127 
128 TEST_F(FormatTest, DefaultLLVMStyleIsCpp) {
129   EXPECT_EQ(FormatStyle::LK_Cpp, getLLVMStyle().Language);
130 }
131 
132 TEST_F(FormatTest, LLVMStyleOverride) {
133   EXPECT_EQ(FormatStyle::LK_Proto,
134             getLLVMStyle(FormatStyle::LK_Proto).Language);
135 }
136 
137 //===----------------------------------------------------------------------===//
138 // Basic function tests.
139 //===----------------------------------------------------------------------===//
140 
141 TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) {
142   EXPECT_EQ(";", format(";"));
143 }
144 
145 TEST_F(FormatTest, FormatsGlobalStatementsAt0) {
146   EXPECT_EQ("int i;", format("  int i;"));
147   EXPECT_EQ("\nint i;", format(" \n\t \v \f  int i;"));
148   EXPECT_EQ("int i;\nint j;", format("    int i; int j;"));
149   EXPECT_EQ("int i;\nint j;", format("    int i;\n  int j;"));
150 }
151 
152 TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) {
153   EXPECT_EQ("int i;", format("int\ni;"));
154 }
155 
156 TEST_F(FormatTest, FormatsNestedBlockStatements) {
157   EXPECT_EQ("{\n  {\n    {}\n  }\n}", format("{{{}}}"));
158 }
159 
160 TEST_F(FormatTest, FormatsNestedCall) {
161   verifyFormat("Method(f1, f2(f3));");
162   verifyFormat("Method(f1(f2, f3()));");
163   verifyFormat("Method(f1(f2, (f3())));");
164 }
165 
166 TEST_F(FormatTest, NestedNameSpecifiers) {
167   verifyFormat("vector<::Type> v;");
168   verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())");
169   verifyFormat("static constexpr bool Bar = decltype(bar())::value;");
170   verifyFormat("static constexpr bool Bar = typeof(bar())::value;");
171   verifyFormat("static constexpr bool Bar = __underlying_type(bar())::value;");
172   verifyFormat("static constexpr bool Bar = _Atomic(bar())::value;");
173   verifyFormat("bool a = 2 < ::SomeFunction();");
174   verifyFormat("ALWAYS_INLINE ::std::string getName();");
175   verifyFormat("some::string getName();");
176 }
177 
178 TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) {
179   EXPECT_EQ("if (a) {\n"
180             "  f();\n"
181             "}",
182             format("if(a){f();}"));
183   EXPECT_EQ(4, ReplacementCount);
184   EXPECT_EQ("if (a) {\n"
185             "  f();\n"
186             "}",
187             format("if (a) {\n"
188                    "  f();\n"
189                    "}"));
190   EXPECT_EQ(0, ReplacementCount);
191   EXPECT_EQ("/*\r\n"
192             "\r\n"
193             "*/\r\n",
194             format("/*\r\n"
195                    "\r\n"
196                    "*/\r\n"));
197   EXPECT_EQ(0, ReplacementCount);
198 }
199 
200 TEST_F(FormatTest, RemovesEmptyLines) {
201   EXPECT_EQ("class C {\n"
202             "  int i;\n"
203             "};",
204             format("class C {\n"
205                    " int i;\n"
206                    "\n"
207                    "};"));
208 
209   // Don't remove empty lines at the start of namespaces or extern "C" blocks.
210   EXPECT_EQ("namespace N {\n"
211             "\n"
212             "int i;\n"
213             "}",
214             format("namespace N {\n"
215                    "\n"
216                    "int    i;\n"
217                    "}",
218                    getGoogleStyle()));
219   EXPECT_EQ("/* something */ namespace N {\n"
220             "\n"
221             "int i;\n"
222             "}",
223             format("/* something */ namespace N {\n"
224                    "\n"
225                    "int    i;\n"
226                    "}",
227                    getGoogleStyle()));
228   EXPECT_EQ("inline namespace N {\n"
229             "\n"
230             "int i;\n"
231             "}",
232             format("inline namespace N {\n"
233                    "\n"
234                    "int    i;\n"
235                    "}",
236                    getGoogleStyle()));
237   EXPECT_EQ("/* something */ inline namespace N {\n"
238             "\n"
239             "int i;\n"
240             "}",
241             format("/* something */ inline namespace N {\n"
242                    "\n"
243                    "int    i;\n"
244                    "}",
245                    getGoogleStyle()));
246   EXPECT_EQ("export namespace N {\n"
247             "\n"
248             "int i;\n"
249             "}",
250             format("export namespace N {\n"
251                    "\n"
252                    "int    i;\n"
253                    "}",
254                    getGoogleStyle()));
255   EXPECT_EQ("extern /**/ \"C\" /**/ {\n"
256             "\n"
257             "int i;\n"
258             "}",
259             format("extern /**/ \"C\" /**/ {\n"
260                    "\n"
261                    "int    i;\n"
262                    "}",
263                    getGoogleStyle()));
264 
265   auto CustomStyle = getLLVMStyle();
266   CustomStyle.BreakBeforeBraces = FormatStyle::BS_Custom;
267   CustomStyle.BraceWrapping.AfterNamespace = true;
268   CustomStyle.KeepEmptyLinesAtTheStartOfBlocks = false;
269   EXPECT_EQ("namespace N\n"
270             "{\n"
271             "\n"
272             "int i;\n"
273             "}",
274             format("namespace N\n"
275                    "{\n"
276                    "\n"
277                    "\n"
278                    "int    i;\n"
279                    "}",
280                    CustomStyle));
281   EXPECT_EQ("/* something */ namespace N\n"
282             "{\n"
283             "\n"
284             "int i;\n"
285             "}",
286             format("/* something */ namespace N {\n"
287                    "\n"
288                    "\n"
289                    "int    i;\n"
290                    "}",
291                    CustomStyle));
292   EXPECT_EQ("inline namespace N\n"
293             "{\n"
294             "\n"
295             "int i;\n"
296             "}",
297             format("inline namespace N\n"
298                    "{\n"
299                    "\n"
300                    "\n"
301                    "int    i;\n"
302                    "}",
303                    CustomStyle));
304   EXPECT_EQ("/* something */ inline namespace N\n"
305             "{\n"
306             "\n"
307             "int i;\n"
308             "}",
309             format("/* something */ inline namespace N\n"
310                    "{\n"
311                    "\n"
312                    "int    i;\n"
313                    "}",
314                    CustomStyle));
315   EXPECT_EQ("export namespace N\n"
316             "{\n"
317             "\n"
318             "int i;\n"
319             "}",
320             format("export namespace N\n"
321                    "{\n"
322                    "\n"
323                    "int    i;\n"
324                    "}",
325                    CustomStyle));
326   EXPECT_EQ("namespace a\n"
327             "{\n"
328             "namespace b\n"
329             "{\n"
330             "\n"
331             "class AA {};\n"
332             "\n"
333             "} // namespace b\n"
334             "} // namespace a\n",
335             format("namespace a\n"
336                    "{\n"
337                    "namespace b\n"
338                    "{\n"
339                    "\n"
340                    "\n"
341                    "class AA {};\n"
342                    "\n"
343                    "\n"
344                    "}\n"
345                    "}\n",
346                    CustomStyle));
347   EXPECT_EQ("namespace A /* comment */\n"
348             "{\n"
349             "class B {}\n"
350             "} // namespace A",
351             format("namespace A /* comment */ { class B {} }", CustomStyle));
352   EXPECT_EQ("namespace A\n"
353             "{ /* comment */\n"
354             "class B {}\n"
355             "} // namespace A",
356             format("namespace A {/* comment */ class B {} }", CustomStyle));
357   EXPECT_EQ("namespace A\n"
358             "{ /* comment */\n"
359             "\n"
360             "class B {}\n"
361             "\n"
362             ""
363             "} // namespace A",
364             format("namespace A { /* comment */\n"
365                    "\n"
366                    "\n"
367                    "class B {}\n"
368                    "\n"
369                    "\n"
370                    "}",
371                    CustomStyle));
372   EXPECT_EQ("namespace A /* comment */\n"
373             "{\n"
374             "\n"
375             "class B {}\n"
376             "\n"
377             "} // namespace A",
378             format("namespace A/* comment */ {\n"
379                    "\n"
380                    "\n"
381                    "class B {}\n"
382                    "\n"
383                    "\n"
384                    "}",
385                    CustomStyle));
386 
387   // ...but do keep inlining and removing empty lines for non-block extern "C"
388   // functions.
389   verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle());
390   EXPECT_EQ("extern \"C\" int f() {\n"
391             "  int i = 42;\n"
392             "  return i;\n"
393             "}",
394             format("extern \"C\" int f() {\n"
395                    "\n"
396                    "  int i = 42;\n"
397                    "  return i;\n"
398                    "}",
399                    getGoogleStyle()));
400 
401   // Remove empty lines at the beginning and end of blocks.
402   EXPECT_EQ("void f() {\n"
403             "\n"
404             "  if (a) {\n"
405             "\n"
406             "    f();\n"
407             "  }\n"
408             "}",
409             format("void f() {\n"
410                    "\n"
411                    "  if (a) {\n"
412                    "\n"
413                    "    f();\n"
414                    "\n"
415                    "  }\n"
416                    "\n"
417                    "}",
418                    getLLVMStyle()));
419   EXPECT_EQ("void f() {\n"
420             "  if (a) {\n"
421             "    f();\n"
422             "  }\n"
423             "}",
424             format("void f() {\n"
425                    "\n"
426                    "  if (a) {\n"
427                    "\n"
428                    "    f();\n"
429                    "\n"
430                    "  }\n"
431                    "\n"
432                    "}",
433                    getGoogleStyle()));
434 
435   // Don't remove empty lines in more complex control statements.
436   EXPECT_EQ("void f() {\n"
437             "  if (a) {\n"
438             "    f();\n"
439             "\n"
440             "  } else if (b) {\n"
441             "    f();\n"
442             "  }\n"
443             "}",
444             format("void f() {\n"
445                    "  if (a) {\n"
446                    "    f();\n"
447                    "\n"
448                    "  } else if (b) {\n"
449                    "    f();\n"
450                    "\n"
451                    "  }\n"
452                    "\n"
453                    "}"));
454 
455   // Don't remove empty lines before namespace endings.
456   FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle();
457   LLVMWithNoNamespaceFix.FixNamespaceComments = false;
458   EXPECT_EQ("namespace {\n"
459             "int i;\n"
460             "\n"
461             "}",
462             format("namespace {\n"
463                    "int i;\n"
464                    "\n"
465                    "}",
466                    LLVMWithNoNamespaceFix));
467   EXPECT_EQ("namespace {\n"
468             "int i;\n"
469             "}",
470             format("namespace {\n"
471                    "int i;\n"
472                    "}",
473                    LLVMWithNoNamespaceFix));
474   EXPECT_EQ("namespace {\n"
475             "int i;\n"
476             "\n"
477             "};",
478             format("namespace {\n"
479                    "int i;\n"
480                    "\n"
481                    "};",
482                    LLVMWithNoNamespaceFix));
483   EXPECT_EQ("namespace {\n"
484             "int i;\n"
485             "};",
486             format("namespace {\n"
487                    "int i;\n"
488                    "};",
489                    LLVMWithNoNamespaceFix));
490   EXPECT_EQ("namespace {\n"
491             "int i;\n"
492             "\n"
493             "}",
494             format("namespace {\n"
495                    "int i;\n"
496                    "\n"
497                    "}"));
498   EXPECT_EQ("namespace {\n"
499             "int i;\n"
500             "\n"
501             "} // namespace",
502             format("namespace {\n"
503                    "int i;\n"
504                    "\n"
505                    "}  // namespace"));
506 
507   FormatStyle Style = getLLVMStyle();
508   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
509   Style.MaxEmptyLinesToKeep = 2;
510   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
511   Style.BraceWrapping.AfterClass = true;
512   Style.BraceWrapping.AfterFunction = true;
513   Style.KeepEmptyLinesAtTheStartOfBlocks = false;
514 
515   EXPECT_EQ("class Foo\n"
516             "{\n"
517             "  Foo() {}\n"
518             "\n"
519             "  void funk() {}\n"
520             "};",
521             format("class Foo\n"
522                    "{\n"
523                    "  Foo()\n"
524                    "  {\n"
525                    "  }\n"
526                    "\n"
527                    "  void funk() {}\n"
528                    "};",
529                    Style));
530 }
531 
532 TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) {
533   verifyFormat("x = (a) and (b);");
534   verifyFormat("x = (a) or (b);");
535   verifyFormat("x = (a) bitand (b);");
536   verifyFormat("x = (a) bitor (b);");
537   verifyFormat("x = (a) not_eq (b);");
538   verifyFormat("x = (a) and_eq (b);");
539   verifyFormat("x = (a) or_eq (b);");
540   verifyFormat("x = (a) xor (b);");
541 }
542 
543 TEST_F(FormatTest, RecognizesUnaryOperatorKeywords) {
544   verifyFormat("x = compl(a);");
545   verifyFormat("x = not(a);");
546   verifyFormat("x = bitand(a);");
547   // Unary operator must not be merged with the next identifier
548   verifyFormat("x = compl a;");
549   verifyFormat("x = not a;");
550   verifyFormat("x = bitand a;");
551 }
552 
553 //===----------------------------------------------------------------------===//
554 // Tests for control statements.
555 //===----------------------------------------------------------------------===//
556 
557 TEST_F(FormatTest, FormatIfWithoutCompoundStatement) {
558   verifyFormat("if (true)\n  f();\ng();");
559   verifyFormat("if (a)\n  if (b)\n    if (c)\n      g();\nh();");
560   verifyFormat("if (a)\n  if (b) {\n    f();\n  }\ng();");
561   verifyFormat("if constexpr (true)\n"
562                "  f();\ng();");
563   verifyFormat("if CONSTEXPR (true)\n"
564                "  f();\ng();");
565   verifyFormat("if constexpr (a)\n"
566                "  if constexpr (b)\n"
567                "    if constexpr (c)\n"
568                "      g();\n"
569                "h();");
570   verifyFormat("if CONSTEXPR (a)\n"
571                "  if CONSTEXPR (b)\n"
572                "    if CONSTEXPR (c)\n"
573                "      g();\n"
574                "h();");
575   verifyFormat("if constexpr (a)\n"
576                "  if constexpr (b) {\n"
577                "    f();\n"
578                "  }\n"
579                "g();");
580   verifyFormat("if CONSTEXPR (a)\n"
581                "  if CONSTEXPR (b) {\n"
582                "    f();\n"
583                "  }\n"
584                "g();");
585 
586   verifyFormat("if (a)\n"
587                "  g();");
588   verifyFormat("if (a) {\n"
589                "  g()\n"
590                "};");
591   verifyFormat("if (a)\n"
592                "  g();\n"
593                "else\n"
594                "  g();");
595   verifyFormat("if (a) {\n"
596                "  g();\n"
597                "} else\n"
598                "  g();");
599   verifyFormat("if (a)\n"
600                "  g();\n"
601                "else {\n"
602                "  g();\n"
603                "}");
604   verifyFormat("if (a) {\n"
605                "  g();\n"
606                "} else {\n"
607                "  g();\n"
608                "}");
609   verifyFormat("if (a)\n"
610                "  g();\n"
611                "else if (b)\n"
612                "  g();\n"
613                "else\n"
614                "  g();");
615   verifyFormat("if (a) {\n"
616                "  g();\n"
617                "} else if (b)\n"
618                "  g();\n"
619                "else\n"
620                "  g();");
621   verifyFormat("if (a)\n"
622                "  g();\n"
623                "else if (b) {\n"
624                "  g();\n"
625                "} else\n"
626                "  g();");
627   verifyFormat("if (a)\n"
628                "  g();\n"
629                "else if (b)\n"
630                "  g();\n"
631                "else {\n"
632                "  g();\n"
633                "}");
634   verifyFormat("if (a)\n"
635                "  g();\n"
636                "else if (b) {\n"
637                "  g();\n"
638                "} else {\n"
639                "  g();\n"
640                "}");
641   verifyFormat("if (a) {\n"
642                "  g();\n"
643                "} else if (b) {\n"
644                "  g();\n"
645                "} else {\n"
646                "  g();\n"
647                "}");
648 
649   FormatStyle AllowsMergedIf = getLLVMStyle();
650   AllowsMergedIf.IfMacros.push_back("MYIF");
651   AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left;
652   AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
653       FormatStyle::SIS_WithoutElse;
654   verifyFormat("if (a)\n"
655                "  // comment\n"
656                "  f();",
657                AllowsMergedIf);
658   verifyFormat("{\n"
659                "  if (a)\n"
660                "  label:\n"
661                "    f();\n"
662                "}",
663                AllowsMergedIf);
664   verifyFormat("#define A \\\n"
665                "  if (a)  \\\n"
666                "  label:  \\\n"
667                "    f()",
668                AllowsMergedIf);
669   verifyFormat("if (a)\n"
670                "  ;",
671                AllowsMergedIf);
672   verifyFormat("if (a)\n"
673                "  if (b) return;",
674                AllowsMergedIf);
675 
676   verifyFormat("if (a) // Can't merge this\n"
677                "  f();\n",
678                AllowsMergedIf);
679   verifyFormat("if (a) /* still don't merge */\n"
680                "  f();",
681                AllowsMergedIf);
682   verifyFormat("if (a) { // Never merge this\n"
683                "  f();\n"
684                "}",
685                AllowsMergedIf);
686   verifyFormat("if (a) { /* Never merge this */\n"
687                "  f();\n"
688                "}",
689                AllowsMergedIf);
690   verifyFormat("MYIF (a)\n"
691                "  // comment\n"
692                "  f();",
693                AllowsMergedIf);
694   verifyFormat("{\n"
695                "  MYIF (a)\n"
696                "  label:\n"
697                "    f();\n"
698                "}",
699                AllowsMergedIf);
700   verifyFormat("#define A  \\\n"
701                "  MYIF (a) \\\n"
702                "  label:   \\\n"
703                "    f()",
704                AllowsMergedIf);
705   verifyFormat("MYIF (a)\n"
706                "  ;",
707                AllowsMergedIf);
708   verifyFormat("MYIF (a)\n"
709                "  MYIF (b) return;",
710                AllowsMergedIf);
711 
712   verifyFormat("MYIF (a) // Can't merge this\n"
713                "  f();\n",
714                AllowsMergedIf);
715   verifyFormat("MYIF (a) /* still don't merge */\n"
716                "  f();",
717                AllowsMergedIf);
718   verifyFormat("MYIF (a) { // Never merge this\n"
719                "  f();\n"
720                "}",
721                AllowsMergedIf);
722   verifyFormat("MYIF (a) { /* Never merge this */\n"
723                "  f();\n"
724                "}",
725                AllowsMergedIf);
726 
727   AllowsMergedIf.ColumnLimit = 14;
728   // Where line-lengths matter, a 2-letter synonym that maintains line length.
729   // Not IF to avoid any confusion that IF is somehow special.
730   AllowsMergedIf.IfMacros.push_back("FI");
731   verifyFormat("if (a) return;", AllowsMergedIf);
732   verifyFormat("if (aaaaaaaaa)\n"
733                "  return;",
734                AllowsMergedIf);
735   verifyFormat("FI (a) return;", AllowsMergedIf);
736   verifyFormat("FI (aaaaaaaaa)\n"
737                "  return;",
738                AllowsMergedIf);
739 
740   AllowsMergedIf.ColumnLimit = 13;
741   verifyFormat("if (a)\n  return;", AllowsMergedIf);
742   verifyFormat("FI (a)\n  return;", AllowsMergedIf);
743 
744   FormatStyle AllowsMergedIfElse = getLLVMStyle();
745   AllowsMergedIfElse.IfMacros.push_back("MYIF");
746   AllowsMergedIfElse.AllowShortIfStatementsOnASingleLine =
747       FormatStyle::SIS_AllIfsAndElse;
748   verifyFormat("if (a)\n"
749                "  // comment\n"
750                "  f();\n"
751                "else\n"
752                "  // comment\n"
753                "  f();",
754                AllowsMergedIfElse);
755   verifyFormat("{\n"
756                "  if (a)\n"
757                "  label:\n"
758                "    f();\n"
759                "  else\n"
760                "  label:\n"
761                "    f();\n"
762                "}",
763                AllowsMergedIfElse);
764   verifyFormat("if (a)\n"
765                "  ;\n"
766                "else\n"
767                "  ;",
768                AllowsMergedIfElse);
769   verifyFormat("if (a) {\n"
770                "} else {\n"
771                "}",
772                AllowsMergedIfElse);
773   verifyFormat("if (a) return;\n"
774                "else if (b) return;\n"
775                "else return;",
776                AllowsMergedIfElse);
777   verifyFormat("if (a) {\n"
778                "} else return;",
779                AllowsMergedIfElse);
780   verifyFormat("if (a) {\n"
781                "} else if (b) return;\n"
782                "else return;",
783                AllowsMergedIfElse);
784   verifyFormat("if (a) return;\n"
785                "else if (b) {\n"
786                "} else return;",
787                AllowsMergedIfElse);
788   verifyFormat("if (a)\n"
789                "  if (b) return;\n"
790                "  else return;",
791                AllowsMergedIfElse);
792   verifyFormat("if constexpr (a)\n"
793                "  if constexpr (b) return;\n"
794                "  else if constexpr (c) return;\n"
795                "  else return;",
796                AllowsMergedIfElse);
797   verifyFormat("MYIF (a)\n"
798                "  // comment\n"
799                "  f();\n"
800                "else\n"
801                "  // comment\n"
802                "  f();",
803                AllowsMergedIfElse);
804   verifyFormat("{\n"
805                "  MYIF (a)\n"
806                "  label:\n"
807                "    f();\n"
808                "  else\n"
809                "  label:\n"
810                "    f();\n"
811                "}",
812                AllowsMergedIfElse);
813   verifyFormat("MYIF (a)\n"
814                "  ;\n"
815                "else\n"
816                "  ;",
817                AllowsMergedIfElse);
818   verifyFormat("MYIF (a) {\n"
819                "} else {\n"
820                "}",
821                AllowsMergedIfElse);
822   verifyFormat("MYIF (a) return;\n"
823                "else MYIF (b) return;\n"
824                "else return;",
825                AllowsMergedIfElse);
826   verifyFormat("MYIF (a) {\n"
827                "} else return;",
828                AllowsMergedIfElse);
829   verifyFormat("MYIF (a) {\n"
830                "} else MYIF (b) return;\n"
831                "else return;",
832                AllowsMergedIfElse);
833   verifyFormat("MYIF (a) return;\n"
834                "else MYIF (b) {\n"
835                "} else return;",
836                AllowsMergedIfElse);
837   verifyFormat("MYIF (a)\n"
838                "  MYIF (b) return;\n"
839                "  else return;",
840                AllowsMergedIfElse);
841   verifyFormat("MYIF constexpr (a)\n"
842                "  MYIF constexpr (b) return;\n"
843                "  else MYIF constexpr (c) return;\n"
844                "  else return;",
845                AllowsMergedIfElse);
846 }
847 
848 TEST_F(FormatTest, FormatIfWithoutCompoundStatementButElseWith) {
849   FormatStyle AllowsMergedIf = getLLVMStyle();
850   AllowsMergedIf.IfMacros.push_back("MYIF");
851   AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left;
852   AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
853       FormatStyle::SIS_WithoutElse;
854   verifyFormat("if (a)\n"
855                "  f();\n"
856                "else {\n"
857                "  g();\n"
858                "}",
859                AllowsMergedIf);
860   verifyFormat("if (a)\n"
861                "  f();\n"
862                "else\n"
863                "  g();\n",
864                AllowsMergedIf);
865 
866   verifyFormat("if (a) g();", AllowsMergedIf);
867   verifyFormat("if (a) {\n"
868                "  g()\n"
869                "};",
870                AllowsMergedIf);
871   verifyFormat("if (a)\n"
872                "  g();\n"
873                "else\n"
874                "  g();",
875                AllowsMergedIf);
876   verifyFormat("if (a) {\n"
877                "  g();\n"
878                "} else\n"
879                "  g();",
880                AllowsMergedIf);
881   verifyFormat("if (a)\n"
882                "  g();\n"
883                "else {\n"
884                "  g();\n"
885                "}",
886                AllowsMergedIf);
887   verifyFormat("if (a) {\n"
888                "  g();\n"
889                "} else {\n"
890                "  g();\n"
891                "}",
892                AllowsMergedIf);
893   verifyFormat("if (a)\n"
894                "  g();\n"
895                "else if (b)\n"
896                "  g();\n"
897                "else\n"
898                "  g();",
899                AllowsMergedIf);
900   verifyFormat("if (a) {\n"
901                "  g();\n"
902                "} else if (b)\n"
903                "  g();\n"
904                "else\n"
905                "  g();",
906                AllowsMergedIf);
907   verifyFormat("if (a)\n"
908                "  g();\n"
909                "else if (b) {\n"
910                "  g();\n"
911                "} else\n"
912                "  g();",
913                AllowsMergedIf);
914   verifyFormat("if (a)\n"
915                "  g();\n"
916                "else if (b)\n"
917                "  g();\n"
918                "else {\n"
919                "  g();\n"
920                "}",
921                AllowsMergedIf);
922   verifyFormat("if (a)\n"
923                "  g();\n"
924                "else if (b) {\n"
925                "  g();\n"
926                "} else {\n"
927                "  g();\n"
928                "}",
929                AllowsMergedIf);
930   verifyFormat("if (a) {\n"
931                "  g();\n"
932                "} else if (b) {\n"
933                "  g();\n"
934                "} else {\n"
935                "  g();\n"
936                "}",
937                AllowsMergedIf);
938   verifyFormat("MYIF (a)\n"
939                "  f();\n"
940                "else {\n"
941                "  g();\n"
942                "}",
943                AllowsMergedIf);
944   verifyFormat("MYIF (a)\n"
945                "  f();\n"
946                "else\n"
947                "  g();\n",
948                AllowsMergedIf);
949 
950   verifyFormat("MYIF (a) g();", AllowsMergedIf);
951   verifyFormat("MYIF (a) {\n"
952                "  g()\n"
953                "};",
954                AllowsMergedIf);
955   verifyFormat("MYIF (a)\n"
956                "  g();\n"
957                "else\n"
958                "  g();",
959                AllowsMergedIf);
960   verifyFormat("MYIF (a) {\n"
961                "  g();\n"
962                "} else\n"
963                "  g();",
964                AllowsMergedIf);
965   verifyFormat("MYIF (a)\n"
966                "  g();\n"
967                "else {\n"
968                "  g();\n"
969                "}",
970                AllowsMergedIf);
971   verifyFormat("MYIF (a) {\n"
972                "  g();\n"
973                "} else {\n"
974                "  g();\n"
975                "}",
976                AllowsMergedIf);
977   verifyFormat("MYIF (a)\n"
978                "  g();\n"
979                "else MYIF (b)\n"
980                "  g();\n"
981                "else\n"
982                "  g();",
983                AllowsMergedIf);
984   verifyFormat("MYIF (a)\n"
985                "  g();\n"
986                "else if (b)\n"
987                "  g();\n"
988                "else\n"
989                "  g();",
990                AllowsMergedIf);
991   verifyFormat("MYIF (a) {\n"
992                "  g();\n"
993                "} else MYIF (b)\n"
994                "  g();\n"
995                "else\n"
996                "  g();",
997                AllowsMergedIf);
998   verifyFormat("MYIF (a) {\n"
999                "  g();\n"
1000                "} else if (b)\n"
1001                "  g();\n"
1002                "else\n"
1003                "  g();",
1004                AllowsMergedIf);
1005   verifyFormat("MYIF (a)\n"
1006                "  g();\n"
1007                "else MYIF (b) {\n"
1008                "  g();\n"
1009                "} else\n"
1010                "  g();",
1011                AllowsMergedIf);
1012   verifyFormat("MYIF (a)\n"
1013                "  g();\n"
1014                "else if (b) {\n"
1015                "  g();\n"
1016                "} else\n"
1017                "  g();",
1018                AllowsMergedIf);
1019   verifyFormat("MYIF (a)\n"
1020                "  g();\n"
1021                "else MYIF (b)\n"
1022                "  g();\n"
1023                "else {\n"
1024                "  g();\n"
1025                "}",
1026                AllowsMergedIf);
1027   verifyFormat("MYIF (a)\n"
1028                "  g();\n"
1029                "else if (b)\n"
1030                "  g();\n"
1031                "else {\n"
1032                "  g();\n"
1033                "}",
1034                AllowsMergedIf);
1035   verifyFormat("MYIF (a)\n"
1036                "  g();\n"
1037                "else MYIF (b) {\n"
1038                "  g();\n"
1039                "} else {\n"
1040                "  g();\n"
1041                "}",
1042                AllowsMergedIf);
1043   verifyFormat("MYIF (a)\n"
1044                "  g();\n"
1045                "else if (b) {\n"
1046                "  g();\n"
1047                "} else {\n"
1048                "  g();\n"
1049                "}",
1050                AllowsMergedIf);
1051   verifyFormat("MYIF (a) {\n"
1052                "  g();\n"
1053                "} else MYIF (b) {\n"
1054                "  g();\n"
1055                "} else {\n"
1056                "  g();\n"
1057                "}",
1058                AllowsMergedIf);
1059   verifyFormat("MYIF (a) {\n"
1060                "  g();\n"
1061                "} else if (b) {\n"
1062                "  g();\n"
1063                "} else {\n"
1064                "  g();\n"
1065                "}",
1066                AllowsMergedIf);
1067 
1068   AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
1069       FormatStyle::SIS_OnlyFirstIf;
1070 
1071   verifyFormat("if (a) f();\n"
1072                "else {\n"
1073                "  g();\n"
1074                "}",
1075                AllowsMergedIf);
1076   verifyFormat("if (a) f();\n"
1077                "else {\n"
1078                "  if (a) f();\n"
1079                "  else {\n"
1080                "    g();\n"
1081                "  }\n"
1082                "  g();\n"
1083                "}",
1084                AllowsMergedIf);
1085 
1086   verifyFormat("if (a) g();", AllowsMergedIf);
1087   verifyFormat("if (a) {\n"
1088                "  g()\n"
1089                "};",
1090                AllowsMergedIf);
1091   verifyFormat("if (a) g();\n"
1092                "else\n"
1093                "  g();",
1094                AllowsMergedIf);
1095   verifyFormat("if (a) {\n"
1096                "  g();\n"
1097                "} else\n"
1098                "  g();",
1099                AllowsMergedIf);
1100   verifyFormat("if (a) g();\n"
1101                "else {\n"
1102                "  g();\n"
1103                "}",
1104                AllowsMergedIf);
1105   verifyFormat("if (a) {\n"
1106                "  g();\n"
1107                "} else {\n"
1108                "  g();\n"
1109                "}",
1110                AllowsMergedIf);
1111   verifyFormat("if (a) g();\n"
1112                "else if (b)\n"
1113                "  g();\n"
1114                "else\n"
1115                "  g();",
1116                AllowsMergedIf);
1117   verifyFormat("if (a) {\n"
1118                "  g();\n"
1119                "} else if (b)\n"
1120                "  g();\n"
1121                "else\n"
1122                "  g();",
1123                AllowsMergedIf);
1124   verifyFormat("if (a) g();\n"
1125                "else if (b) {\n"
1126                "  g();\n"
1127                "} else\n"
1128                "  g();",
1129                AllowsMergedIf);
1130   verifyFormat("if (a) g();\n"
1131                "else if (b)\n"
1132                "  g();\n"
1133                "else {\n"
1134                "  g();\n"
1135                "}",
1136                AllowsMergedIf);
1137   verifyFormat("if (a) g();\n"
1138                "else if (b) {\n"
1139                "  g();\n"
1140                "} else {\n"
1141                "  g();\n"
1142                "}",
1143                AllowsMergedIf);
1144   verifyFormat("if (a) {\n"
1145                "  g();\n"
1146                "} else if (b) {\n"
1147                "  g();\n"
1148                "} else {\n"
1149                "  g();\n"
1150                "}",
1151                AllowsMergedIf);
1152   verifyFormat("MYIF (a) f();\n"
1153                "else {\n"
1154                "  g();\n"
1155                "}",
1156                AllowsMergedIf);
1157   verifyFormat("MYIF (a) f();\n"
1158                "else {\n"
1159                "  if (a) f();\n"
1160                "  else {\n"
1161                "    g();\n"
1162                "  }\n"
1163                "  g();\n"
1164                "}",
1165                AllowsMergedIf);
1166 
1167   verifyFormat("MYIF (a) g();", AllowsMergedIf);
1168   verifyFormat("MYIF (a) {\n"
1169                "  g()\n"
1170                "};",
1171                AllowsMergedIf);
1172   verifyFormat("MYIF (a) g();\n"
1173                "else\n"
1174                "  g();",
1175                AllowsMergedIf);
1176   verifyFormat("MYIF (a) {\n"
1177                "  g();\n"
1178                "} else\n"
1179                "  g();",
1180                AllowsMergedIf);
1181   verifyFormat("MYIF (a) g();\n"
1182                "else {\n"
1183                "  g();\n"
1184                "}",
1185                AllowsMergedIf);
1186   verifyFormat("MYIF (a) {\n"
1187                "  g();\n"
1188                "} else {\n"
1189                "  g();\n"
1190                "}",
1191                AllowsMergedIf);
1192   verifyFormat("MYIF (a) g();\n"
1193                "else MYIF (b)\n"
1194                "  g();\n"
1195                "else\n"
1196                "  g();",
1197                AllowsMergedIf);
1198   verifyFormat("MYIF (a) g();\n"
1199                "else if (b)\n"
1200                "  g();\n"
1201                "else\n"
1202                "  g();",
1203                AllowsMergedIf);
1204   verifyFormat("MYIF (a) {\n"
1205                "  g();\n"
1206                "} else MYIF (b)\n"
1207                "  g();\n"
1208                "else\n"
1209                "  g();",
1210                AllowsMergedIf);
1211   verifyFormat("MYIF (a) {\n"
1212                "  g();\n"
1213                "} else if (b)\n"
1214                "  g();\n"
1215                "else\n"
1216                "  g();",
1217                AllowsMergedIf);
1218   verifyFormat("MYIF (a) g();\n"
1219                "else MYIF (b) {\n"
1220                "  g();\n"
1221                "} else\n"
1222                "  g();",
1223                AllowsMergedIf);
1224   verifyFormat("MYIF (a) g();\n"
1225                "else if (b) {\n"
1226                "  g();\n"
1227                "} else\n"
1228                "  g();",
1229                AllowsMergedIf);
1230   verifyFormat("MYIF (a) g();\n"
1231                "else MYIF (b)\n"
1232                "  g();\n"
1233                "else {\n"
1234                "  g();\n"
1235                "}",
1236                AllowsMergedIf);
1237   verifyFormat("MYIF (a) g();\n"
1238                "else if (b)\n"
1239                "  g();\n"
1240                "else {\n"
1241                "  g();\n"
1242                "}",
1243                AllowsMergedIf);
1244   verifyFormat("MYIF (a) g();\n"
1245                "else MYIF (b) {\n"
1246                "  g();\n"
1247                "} else {\n"
1248                "  g();\n"
1249                "}",
1250                AllowsMergedIf);
1251   verifyFormat("MYIF (a) g();\n"
1252                "else if (b) {\n"
1253                "  g();\n"
1254                "} else {\n"
1255                "  g();\n"
1256                "}",
1257                AllowsMergedIf);
1258   verifyFormat("MYIF (a) {\n"
1259                "  g();\n"
1260                "} else MYIF (b) {\n"
1261                "  g();\n"
1262                "} else {\n"
1263                "  g();\n"
1264                "}",
1265                AllowsMergedIf);
1266   verifyFormat("MYIF (a) {\n"
1267                "  g();\n"
1268                "} else if (b) {\n"
1269                "  g();\n"
1270                "} else {\n"
1271                "  g();\n"
1272                "}",
1273                AllowsMergedIf);
1274 
1275   AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
1276       FormatStyle::SIS_AllIfsAndElse;
1277 
1278   verifyFormat("if (a) f();\n"
1279                "else {\n"
1280                "  g();\n"
1281                "}",
1282                AllowsMergedIf);
1283   verifyFormat("if (a) f();\n"
1284                "else {\n"
1285                "  if (a) f();\n"
1286                "  else {\n"
1287                "    g();\n"
1288                "  }\n"
1289                "  g();\n"
1290                "}",
1291                AllowsMergedIf);
1292 
1293   verifyFormat("if (a) g();", AllowsMergedIf);
1294   verifyFormat("if (a) {\n"
1295                "  g()\n"
1296                "};",
1297                AllowsMergedIf);
1298   verifyFormat("if (a) g();\n"
1299                "else g();",
1300                AllowsMergedIf);
1301   verifyFormat("if (a) {\n"
1302                "  g();\n"
1303                "} else g();",
1304                AllowsMergedIf);
1305   verifyFormat("if (a) g();\n"
1306                "else {\n"
1307                "  g();\n"
1308                "}",
1309                AllowsMergedIf);
1310   verifyFormat("if (a) {\n"
1311                "  g();\n"
1312                "} else {\n"
1313                "  g();\n"
1314                "}",
1315                AllowsMergedIf);
1316   verifyFormat("if (a) g();\n"
1317                "else if (b) g();\n"
1318                "else g();",
1319                AllowsMergedIf);
1320   verifyFormat("if (a) {\n"
1321                "  g();\n"
1322                "} else if (b) g();\n"
1323                "else g();",
1324                AllowsMergedIf);
1325   verifyFormat("if (a) g();\n"
1326                "else if (b) {\n"
1327                "  g();\n"
1328                "} else g();",
1329                AllowsMergedIf);
1330   verifyFormat("if (a) g();\n"
1331                "else if (b) g();\n"
1332                "else {\n"
1333                "  g();\n"
1334                "}",
1335                AllowsMergedIf);
1336   verifyFormat("if (a) g();\n"
1337                "else if (b) {\n"
1338                "  g();\n"
1339                "} else {\n"
1340                "  g();\n"
1341                "}",
1342                AllowsMergedIf);
1343   verifyFormat("if (a) {\n"
1344                "  g();\n"
1345                "} else if (b) {\n"
1346                "  g();\n"
1347                "} else {\n"
1348                "  g();\n"
1349                "}",
1350                AllowsMergedIf);
1351   verifyFormat("MYIF (a) f();\n"
1352                "else {\n"
1353                "  g();\n"
1354                "}",
1355                AllowsMergedIf);
1356   verifyFormat("MYIF (a) f();\n"
1357                "else {\n"
1358                "  if (a) f();\n"
1359                "  else {\n"
1360                "    g();\n"
1361                "  }\n"
1362                "  g();\n"
1363                "}",
1364                AllowsMergedIf);
1365 
1366   verifyFormat("MYIF (a) g();", AllowsMergedIf);
1367   verifyFormat("MYIF (a) {\n"
1368                "  g()\n"
1369                "};",
1370                AllowsMergedIf);
1371   verifyFormat("MYIF (a) g();\n"
1372                "else g();",
1373                AllowsMergedIf);
1374   verifyFormat("MYIF (a) {\n"
1375                "  g();\n"
1376                "} else g();",
1377                AllowsMergedIf);
1378   verifyFormat("MYIF (a) g();\n"
1379                "else {\n"
1380                "  g();\n"
1381                "}",
1382                AllowsMergedIf);
1383   verifyFormat("MYIF (a) {\n"
1384                "  g();\n"
1385                "} else {\n"
1386                "  g();\n"
1387                "}",
1388                AllowsMergedIf);
1389   verifyFormat("MYIF (a) g();\n"
1390                "else MYIF (b) g();\n"
1391                "else g();",
1392                AllowsMergedIf);
1393   verifyFormat("MYIF (a) g();\n"
1394                "else if (b) g();\n"
1395                "else g();",
1396                AllowsMergedIf);
1397   verifyFormat("MYIF (a) {\n"
1398                "  g();\n"
1399                "} else MYIF (b) g();\n"
1400                "else g();",
1401                AllowsMergedIf);
1402   verifyFormat("MYIF (a) {\n"
1403                "  g();\n"
1404                "} else if (b) g();\n"
1405                "else g();",
1406                AllowsMergedIf);
1407   verifyFormat("MYIF (a) g();\n"
1408                "else MYIF (b) {\n"
1409                "  g();\n"
1410                "} else g();",
1411                AllowsMergedIf);
1412   verifyFormat("MYIF (a) g();\n"
1413                "else if (b) {\n"
1414                "  g();\n"
1415                "} else g();",
1416                AllowsMergedIf);
1417   verifyFormat("MYIF (a) g();\n"
1418                "else MYIF (b) g();\n"
1419                "else {\n"
1420                "  g();\n"
1421                "}",
1422                AllowsMergedIf);
1423   verifyFormat("MYIF (a) g();\n"
1424                "else if (b) g();\n"
1425                "else {\n"
1426                "  g();\n"
1427                "}",
1428                AllowsMergedIf);
1429   verifyFormat("MYIF (a) g();\n"
1430                "else MYIF (b) {\n"
1431                "  g();\n"
1432                "} else {\n"
1433                "  g();\n"
1434                "}",
1435                AllowsMergedIf);
1436   verifyFormat("MYIF (a) g();\n"
1437                "else if (b) {\n"
1438                "  g();\n"
1439                "} else {\n"
1440                "  g();\n"
1441                "}",
1442                AllowsMergedIf);
1443   verifyFormat("MYIF (a) {\n"
1444                "  g();\n"
1445                "} else MYIF (b) {\n"
1446                "  g();\n"
1447                "} else {\n"
1448                "  g();\n"
1449                "}",
1450                AllowsMergedIf);
1451   verifyFormat("MYIF (a) {\n"
1452                "  g();\n"
1453                "} else if (b) {\n"
1454                "  g();\n"
1455                "} else {\n"
1456                "  g();\n"
1457                "}",
1458                AllowsMergedIf);
1459 }
1460 
1461 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
1462   FormatStyle AllowsMergedLoops = getLLVMStyle();
1463   AllowsMergedLoops.AllowShortLoopsOnASingleLine = true;
1464   verifyFormat("while (true) continue;", AllowsMergedLoops);
1465   verifyFormat("for (;;) continue;", AllowsMergedLoops);
1466   verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops);
1467   verifyFormat("BOOST_FOREACH (int &v, vec) v *= 2;", AllowsMergedLoops);
1468   verifyFormat("while (true)\n"
1469                "  ;",
1470                AllowsMergedLoops);
1471   verifyFormat("for (;;)\n"
1472                "  ;",
1473                AllowsMergedLoops);
1474   verifyFormat("for (;;)\n"
1475                "  for (;;) continue;",
1476                AllowsMergedLoops);
1477   verifyFormat("for (;;)\n"
1478                "  while (true) continue;",
1479                AllowsMergedLoops);
1480   verifyFormat("while (true)\n"
1481                "  for (;;) continue;",
1482                AllowsMergedLoops);
1483   verifyFormat("BOOST_FOREACH (int &v, vec)\n"
1484                "  for (;;) continue;",
1485                AllowsMergedLoops);
1486   verifyFormat("for (;;)\n"
1487                "  BOOST_FOREACH (int &v, vec) continue;",
1488                AllowsMergedLoops);
1489   verifyFormat("for (;;) // Can't merge this\n"
1490                "  continue;",
1491                AllowsMergedLoops);
1492   verifyFormat("for (;;) /* still don't merge */\n"
1493                "  continue;",
1494                AllowsMergedLoops);
1495   verifyFormat("do a++;\n"
1496                "while (true);",
1497                AllowsMergedLoops);
1498   verifyFormat("do /* Don't merge */\n"
1499                "  a++;\n"
1500                "while (true);",
1501                AllowsMergedLoops);
1502   verifyFormat("do // Don't merge\n"
1503                "  a++;\n"
1504                "while (true);",
1505                AllowsMergedLoops);
1506   verifyFormat("do\n"
1507                "  // Don't merge\n"
1508                "  a++;\n"
1509                "while (true);",
1510                AllowsMergedLoops);
1511   // Without braces labels are interpreted differently.
1512   verifyFormat("{\n"
1513                "  do\n"
1514                "  label:\n"
1515                "    a++;\n"
1516                "  while (true);\n"
1517                "}",
1518                AllowsMergedLoops);
1519 }
1520 
1521 TEST_F(FormatTest, FormatShortBracedStatements) {
1522   FormatStyle AllowSimpleBracedStatements = getLLVMStyle();
1523   AllowSimpleBracedStatements.IfMacros.push_back("MYIF");
1524   // Where line-lengths matter, a 2-letter synonym that maintains line length.
1525   // Not IF to avoid any confusion that IF is somehow special.
1526   AllowSimpleBracedStatements.IfMacros.push_back("FI");
1527   AllowSimpleBracedStatements.ColumnLimit = 40;
1528   AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine =
1529       FormatStyle::SBS_Always;
1530 
1531   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
1532       FormatStyle::SIS_WithoutElse;
1533   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
1534 
1535   AllowSimpleBracedStatements.BreakBeforeBraces = FormatStyle::BS_Custom;
1536   AllowSimpleBracedStatements.BraceWrapping.AfterFunction = true;
1537   AllowSimpleBracedStatements.BraceWrapping.SplitEmptyRecord = false;
1538 
1539   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
1540   verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements);
1541   verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements);
1542   verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements);
1543   verifyFormat("MYIF constexpr (true) {}", AllowSimpleBracedStatements);
1544   verifyFormat("MYIF CONSTEXPR (true) {}", AllowSimpleBracedStatements);
1545   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
1546   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
1547   verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
1548   verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements);
1549   verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements);
1550   verifyFormat("MYIF (true) { f(); }", AllowSimpleBracedStatements);
1551   verifyFormat("MYIF constexpr (true) { f(); }", AllowSimpleBracedStatements);
1552   verifyFormat("MYIF CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements);
1553   verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
1554   verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
1555   verifyFormat("if (true) { fffffffffffffffffffffff(); }",
1556                AllowSimpleBracedStatements);
1557   verifyFormat("if (true) {\n"
1558                "  ffffffffffffffffffffffff();\n"
1559                "}",
1560                AllowSimpleBracedStatements);
1561   verifyFormat("if (true) {\n"
1562                "  ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
1563                "}",
1564                AllowSimpleBracedStatements);
1565   verifyFormat("if (true) { //\n"
1566                "  f();\n"
1567                "}",
1568                AllowSimpleBracedStatements);
1569   verifyFormat("if (true) {\n"
1570                "  f();\n"
1571                "  f();\n"
1572                "}",
1573                AllowSimpleBracedStatements);
1574   verifyFormat("if (true) {\n"
1575                "  f();\n"
1576                "} else {\n"
1577                "  f();\n"
1578                "}",
1579                AllowSimpleBracedStatements);
1580   verifyFormat("FI (true) { fffffffffffffffffffffff(); }",
1581                AllowSimpleBracedStatements);
1582   verifyFormat("MYIF (true) {\n"
1583                "  ffffffffffffffffffffffff();\n"
1584                "}",
1585                AllowSimpleBracedStatements);
1586   verifyFormat("MYIF (true) {\n"
1587                "  ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
1588                "}",
1589                AllowSimpleBracedStatements);
1590   verifyFormat("MYIF (true) { //\n"
1591                "  f();\n"
1592                "}",
1593                AllowSimpleBracedStatements);
1594   verifyFormat("MYIF (true) {\n"
1595                "  f();\n"
1596                "  f();\n"
1597                "}",
1598                AllowSimpleBracedStatements);
1599   verifyFormat("MYIF (true) {\n"
1600                "  f();\n"
1601                "} else {\n"
1602                "  f();\n"
1603                "}",
1604                AllowSimpleBracedStatements);
1605 
1606   verifyFormat("struct A2 {\n"
1607                "  int X;\n"
1608                "};",
1609                AllowSimpleBracedStatements);
1610   verifyFormat("typedef struct A2 {\n"
1611                "  int X;\n"
1612                "} A2_t;",
1613                AllowSimpleBracedStatements);
1614   verifyFormat("template <int> struct A2 {\n"
1615                "  struct B {};\n"
1616                "};",
1617                AllowSimpleBracedStatements);
1618 
1619   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
1620       FormatStyle::SIS_Never;
1621   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
1622   verifyFormat("if (true) {\n"
1623                "  f();\n"
1624                "}",
1625                AllowSimpleBracedStatements);
1626   verifyFormat("if (true) {\n"
1627                "  f();\n"
1628                "} else {\n"
1629                "  f();\n"
1630                "}",
1631                AllowSimpleBracedStatements);
1632   verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements);
1633   verifyFormat("MYIF (true) {\n"
1634                "  f();\n"
1635                "}",
1636                AllowSimpleBracedStatements);
1637   verifyFormat("MYIF (true) {\n"
1638                "  f();\n"
1639                "} else {\n"
1640                "  f();\n"
1641                "}",
1642                AllowSimpleBracedStatements);
1643 
1644   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
1645   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
1646   verifyFormat("while (true) {\n"
1647                "  f();\n"
1648                "}",
1649                AllowSimpleBracedStatements);
1650   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
1651   verifyFormat("for (;;) {\n"
1652                "  f();\n"
1653                "}",
1654                AllowSimpleBracedStatements);
1655   verifyFormat("BOOST_FOREACH (int v, vec) {}", AllowSimpleBracedStatements);
1656   verifyFormat("BOOST_FOREACH (int v, vec) {\n"
1657                "  f();\n"
1658                "}",
1659                AllowSimpleBracedStatements);
1660 
1661   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
1662       FormatStyle::SIS_WithoutElse;
1663   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
1664   AllowSimpleBracedStatements.BraceWrapping.AfterControlStatement =
1665       FormatStyle::BWACS_Always;
1666 
1667   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
1668   verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements);
1669   verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements);
1670   verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements);
1671   verifyFormat("MYIF constexpr (true) {}", AllowSimpleBracedStatements);
1672   verifyFormat("MYIF CONSTEXPR (true) {}", AllowSimpleBracedStatements);
1673   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
1674   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
1675   verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
1676   verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements);
1677   verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements);
1678   verifyFormat("MYIF (true) { f(); }", AllowSimpleBracedStatements);
1679   verifyFormat("MYIF constexpr (true) { f(); }", AllowSimpleBracedStatements);
1680   verifyFormat("MYIF CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements);
1681   verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
1682   verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
1683   verifyFormat("if (true) { fffffffffffffffffffffff(); }",
1684                AllowSimpleBracedStatements);
1685   verifyFormat("if (true)\n"
1686                "{\n"
1687                "  ffffffffffffffffffffffff();\n"
1688                "}",
1689                AllowSimpleBracedStatements);
1690   verifyFormat("if (true)\n"
1691                "{\n"
1692                "  ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
1693                "}",
1694                AllowSimpleBracedStatements);
1695   verifyFormat("if (true)\n"
1696                "{ //\n"
1697                "  f();\n"
1698                "}",
1699                AllowSimpleBracedStatements);
1700   verifyFormat("if (true)\n"
1701                "{\n"
1702                "  f();\n"
1703                "  f();\n"
1704                "}",
1705                AllowSimpleBracedStatements);
1706   verifyFormat("if (true)\n"
1707                "{\n"
1708                "  f();\n"
1709                "} else\n"
1710                "{\n"
1711                "  f();\n"
1712                "}",
1713                AllowSimpleBracedStatements);
1714   verifyFormat("FI (true) { fffffffffffffffffffffff(); }",
1715                AllowSimpleBracedStatements);
1716   verifyFormat("MYIF (true)\n"
1717                "{\n"
1718                "  ffffffffffffffffffffffff();\n"
1719                "}",
1720                AllowSimpleBracedStatements);
1721   verifyFormat("MYIF (true)\n"
1722                "{\n"
1723                "  ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
1724                "}",
1725                AllowSimpleBracedStatements);
1726   verifyFormat("MYIF (true)\n"
1727                "{ //\n"
1728                "  f();\n"
1729                "}",
1730                AllowSimpleBracedStatements);
1731   verifyFormat("MYIF (true)\n"
1732                "{\n"
1733                "  f();\n"
1734                "  f();\n"
1735                "}",
1736                AllowSimpleBracedStatements);
1737   verifyFormat("MYIF (true)\n"
1738                "{\n"
1739                "  f();\n"
1740                "} else\n"
1741                "{\n"
1742                "  f();\n"
1743                "}",
1744                AllowSimpleBracedStatements);
1745 
1746   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
1747       FormatStyle::SIS_Never;
1748   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
1749   verifyFormat("if (true)\n"
1750                "{\n"
1751                "  f();\n"
1752                "}",
1753                AllowSimpleBracedStatements);
1754   verifyFormat("if (true)\n"
1755                "{\n"
1756                "  f();\n"
1757                "} else\n"
1758                "{\n"
1759                "  f();\n"
1760                "}",
1761                AllowSimpleBracedStatements);
1762   verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements);
1763   verifyFormat("MYIF (true)\n"
1764                "{\n"
1765                "  f();\n"
1766                "}",
1767                AllowSimpleBracedStatements);
1768   verifyFormat("MYIF (true)\n"
1769                "{\n"
1770                "  f();\n"
1771                "} else\n"
1772                "{\n"
1773                "  f();\n"
1774                "}",
1775                AllowSimpleBracedStatements);
1776 
1777   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
1778   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
1779   verifyFormat("while (true)\n"
1780                "{\n"
1781                "  f();\n"
1782                "}",
1783                AllowSimpleBracedStatements);
1784   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
1785   verifyFormat("for (;;)\n"
1786                "{\n"
1787                "  f();\n"
1788                "}",
1789                AllowSimpleBracedStatements);
1790   verifyFormat("BOOST_FOREACH (int v, vec) {}", AllowSimpleBracedStatements);
1791   verifyFormat("BOOST_FOREACH (int v, vec)\n"
1792                "{\n"
1793                "  f();\n"
1794                "}",
1795                AllowSimpleBracedStatements);
1796 }
1797 
1798 TEST_F(FormatTest, UnderstandsMacros) {
1799   verifyFormat("#define A (parentheses)");
1800   verifyFormat("#define true ((int)1)");
1801   verifyFormat("#define and(x)");
1802   verifyFormat("#define if(x) x");
1803   verifyFormat("#define return(x) (x)");
1804   verifyFormat("#define while(x) for (; x;)");
1805   verifyFormat("#define xor(x) (^(x))");
1806   verifyFormat("#define __except(x)");
1807   verifyFormat("#define __try(x)");
1808 
1809   FormatStyle Style = getLLVMStyle();
1810   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
1811   Style.BraceWrapping.AfterFunction = true;
1812   // Test that a macro definition never gets merged with the following
1813   // definition.
1814   // FIXME: The AAA macro definition probably should not be split into 3 lines.
1815   verifyFormat("#define AAA                                                    "
1816                "                \\\n"
1817                "  N                                                            "
1818                "                \\\n"
1819                "  {\n"
1820                "#define BBB }\n",
1821                Style);
1822   // verifyFormat("#define AAA N { //\n", Style);
1823 }
1824 
1825 TEST_F(FormatTest, ShortBlocksInMacrosDontMergeWithCodeAfterMacro) {
1826   FormatStyle Style = getLLVMStyleWithColumns(60);
1827   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
1828   Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse;
1829   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
1830   EXPECT_EQ("#define A                                                  \\\n"
1831             "  if (HANDLEwernufrnuLwrmviferuvnierv)                     \\\n"
1832             "  {                                                        \\\n"
1833             "    RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier;               \\\n"
1834             "  }\n"
1835             "X;",
1836             format("#define A \\\n"
1837                    "   if (HANDLEwernufrnuLwrmviferuvnierv) { \\\n"
1838                    "      RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n"
1839                    "   }\n"
1840                    "X;",
1841                    Style));
1842 }
1843 
1844 TEST_F(FormatTest, ParseIfElse) {
1845   verifyFormat("if (true)\n"
1846                "  if (true)\n"
1847                "    if (true)\n"
1848                "      f();\n"
1849                "    else\n"
1850                "      g();\n"
1851                "  else\n"
1852                "    h();\n"
1853                "else\n"
1854                "  i();");
1855   verifyFormat("if (true)\n"
1856                "  if (true)\n"
1857                "    if (true) {\n"
1858                "      if (true)\n"
1859                "        f();\n"
1860                "    } else {\n"
1861                "      g();\n"
1862                "    }\n"
1863                "  else\n"
1864                "    h();\n"
1865                "else {\n"
1866                "  i();\n"
1867                "}");
1868   verifyFormat("if (true)\n"
1869                "  if constexpr (true)\n"
1870                "    if (true) {\n"
1871                "      if constexpr (true)\n"
1872                "        f();\n"
1873                "    } else {\n"
1874                "      g();\n"
1875                "    }\n"
1876                "  else\n"
1877                "    h();\n"
1878                "else {\n"
1879                "  i();\n"
1880                "}");
1881   verifyFormat("if (true)\n"
1882                "  if CONSTEXPR (true)\n"
1883                "    if (true) {\n"
1884                "      if CONSTEXPR (true)\n"
1885                "        f();\n"
1886                "    } else {\n"
1887                "      g();\n"
1888                "    }\n"
1889                "  else\n"
1890                "    h();\n"
1891                "else {\n"
1892                "  i();\n"
1893                "}");
1894   verifyFormat("void f() {\n"
1895                "  if (a) {\n"
1896                "  } else {\n"
1897                "  }\n"
1898                "}");
1899 }
1900 
1901 TEST_F(FormatTest, ElseIf) {
1902   verifyFormat("if (a) {\n} else if (b) {\n}");
1903   verifyFormat("if (a)\n"
1904                "  f();\n"
1905                "else if (b)\n"
1906                "  g();\n"
1907                "else\n"
1908                "  h();");
1909   verifyFormat("if (a)\n"
1910                "  f();\n"
1911                "else // comment\n"
1912                "  if (b) {\n"
1913                "    g();\n"
1914                "    h();\n"
1915                "  }");
1916   verifyFormat("if constexpr (a)\n"
1917                "  f();\n"
1918                "else if constexpr (b)\n"
1919                "  g();\n"
1920                "else\n"
1921                "  h();");
1922   verifyFormat("if CONSTEXPR (a)\n"
1923                "  f();\n"
1924                "else if CONSTEXPR (b)\n"
1925                "  g();\n"
1926                "else\n"
1927                "  h();");
1928   verifyFormat("if (a) {\n"
1929                "  f();\n"
1930                "}\n"
1931                "// or else ..\n"
1932                "else {\n"
1933                "  g()\n"
1934                "}");
1935 
1936   verifyFormat("if (a) {\n"
1937                "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1938                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
1939                "}");
1940   verifyFormat("if (a) {\n"
1941                "} else if constexpr (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1942                "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
1943                "}");
1944   verifyFormat("if (a) {\n"
1945                "} else if CONSTEXPR (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1946                "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
1947                "}");
1948   verifyFormat("if (a) {\n"
1949                "} else if (\n"
1950                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
1951                "}",
1952                getLLVMStyleWithColumns(62));
1953   verifyFormat("if (a) {\n"
1954                "} else if constexpr (\n"
1955                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
1956                "}",
1957                getLLVMStyleWithColumns(62));
1958   verifyFormat("if (a) {\n"
1959                "} else if CONSTEXPR (\n"
1960                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
1961                "}",
1962                getLLVMStyleWithColumns(62));
1963 }
1964 
1965 TEST_F(FormatTest, SeparatePointerReferenceAlignment) {
1966   FormatStyle Style = getLLVMStyle();
1967   // Check first the default LLVM style
1968   // Style.PointerAlignment = FormatStyle::PAS_Right;
1969   // Style.ReferenceAlignment = FormatStyle::RAS_Pointer;
1970   verifyFormat("int *f1(int *a, int &b, int &&c);", Style);
1971   verifyFormat("int &f2(int &&c, int *a, int &b);", Style);
1972   verifyFormat("int &&f3(int &b, int &&c, int *a);", Style);
1973   verifyFormat("int *f1(int &a) const &;", Style);
1974   verifyFormat("int *f1(int &a) const & = 0;", Style);
1975   verifyFormat("int *a = f1();", Style);
1976   verifyFormat("int &b = f2();", Style);
1977   verifyFormat("int &&c = f3();", Style);
1978   verifyFormat("for (auto a = 0, b = 0; const auto &c : {1, 2, 3})", Style);
1979   verifyFormat("for (auto a = 0, b = 0; const int &c : {1, 2, 3})", Style);
1980   verifyFormat("for (auto a = 0, b = 0; const Foo &c : {1, 2, 3})", Style);
1981   verifyFormat("for (auto a = 0, b = 0; const Foo *c : {1, 2, 3})", Style);
1982   verifyFormat("for (int a = 0, b = 0; const auto &c : {1, 2, 3})", Style);
1983   verifyFormat("for (int a = 0, b = 0; const int &c : {1, 2, 3})", Style);
1984   verifyFormat("for (int a = 0, b = 0; const Foo &c : {1, 2, 3})", Style);
1985   verifyFormat("for (int a = 0, b++; const auto &c : {1, 2, 3})", Style);
1986   verifyFormat("for (int a = 0, b++; const int &c : {1, 2, 3})", Style);
1987   verifyFormat("for (int a = 0, b++; const Foo &c : {1, 2, 3})", Style);
1988   verifyFormat("for (auto x = 0; auto &c : {1, 2, 3})", Style);
1989   verifyFormat("for (auto x = 0; int &c : {1, 2, 3})", Style);
1990   verifyFormat("for (int x = 0; auto &c : {1, 2, 3})", Style);
1991   verifyFormat("for (int x = 0; int &c : {1, 2, 3})", Style);
1992   verifyFormat("for (f(); auto &c : {1, 2, 3})", Style);
1993   verifyFormat("for (f(); int &c : {1, 2, 3})", Style);
1994 
1995   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
1996   verifyFormat("Const unsigned int *c;\n"
1997                "const unsigned int *d;\n"
1998                "Const unsigned int &e;\n"
1999                "const unsigned int &f;\n"
2000                "const unsigned    &&g;\n"
2001                "Const unsigned      h;",
2002                Style);
2003 
2004   Style.PointerAlignment = FormatStyle::PAS_Left;
2005   Style.ReferenceAlignment = FormatStyle::RAS_Pointer;
2006   verifyFormat("int* f1(int* a, int& b, int&& c);", Style);
2007   verifyFormat("int& f2(int&& c, int* a, int& b);", Style);
2008   verifyFormat("int&& f3(int& b, int&& c, int* a);", Style);
2009   verifyFormat("int* f1(int& a) const& = 0;", Style);
2010   verifyFormat("int* a = f1();", Style);
2011   verifyFormat("int& b = f2();", Style);
2012   verifyFormat("int&& c = f3();", Style);
2013   verifyFormat("for (auto a = 0, b = 0; const auto& c : {1, 2, 3})", Style);
2014   verifyFormat("for (auto a = 0, b = 0; const int& c : {1, 2, 3})", Style);
2015   verifyFormat("for (auto a = 0, b = 0; const Foo& c : {1, 2, 3})", Style);
2016   verifyFormat("for (auto a = 0, b = 0; const Foo* c : {1, 2, 3})", Style);
2017   verifyFormat("for (int a = 0, b = 0; const auto& c : {1, 2, 3})", Style);
2018   verifyFormat("for (int a = 0, b = 0; const int& c : {1, 2, 3})", Style);
2019   verifyFormat("for (int a = 0, b = 0; const Foo& c : {1, 2, 3})", Style);
2020   verifyFormat("for (int a = 0, b = 0; const Foo* c : {1, 2, 3})", Style);
2021   verifyFormat("for (int a = 0, b++; const auto& c : {1, 2, 3})", Style);
2022   verifyFormat("for (int a = 0, b++; const int& c : {1, 2, 3})", Style);
2023   verifyFormat("for (int a = 0, b++; const Foo& c : {1, 2, 3})", Style);
2024   verifyFormat("for (int a = 0, b++; const Foo* c : {1, 2, 3})", Style);
2025   verifyFormat("for (auto x = 0; auto& c : {1, 2, 3})", Style);
2026   verifyFormat("for (auto x = 0; int& c : {1, 2, 3})", Style);
2027   verifyFormat("for (int x = 0; auto& c : {1, 2, 3})", Style);
2028   verifyFormat("for (int x = 0; int& c : {1, 2, 3})", Style);
2029   verifyFormat("for (f(); auto& c : {1, 2, 3})", Style);
2030   verifyFormat("for (f(); int& c : {1, 2, 3})", Style);
2031 
2032   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
2033   verifyFormat("Const unsigned int* c;\n"
2034                "const unsigned int* d;\n"
2035                "Const unsigned int& e;\n"
2036                "const unsigned int& f;\n"
2037                "const unsigned&&    g;\n"
2038                "Const unsigned      h;",
2039                Style);
2040 
2041   Style.PointerAlignment = FormatStyle::PAS_Right;
2042   Style.ReferenceAlignment = FormatStyle::RAS_Left;
2043   verifyFormat("int *f1(int *a, int& b, int&& c);", Style);
2044   verifyFormat("int& f2(int&& c, int *a, int& b);", Style);
2045   verifyFormat("int&& f3(int& b, int&& c, int *a);", Style);
2046   verifyFormat("int *a = f1();", Style);
2047   verifyFormat("int& b = f2();", Style);
2048   verifyFormat("int&& c = f3();", Style);
2049   verifyFormat("for (auto a = 0, b = 0; const Foo *c : {1, 2, 3})", Style);
2050   verifyFormat("for (int a = 0, b = 0; const Foo *c : {1, 2, 3})", Style);
2051   verifyFormat("for (int a = 0, b++; const Foo *c : {1, 2, 3})", Style);
2052 
2053   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
2054   verifyFormat("Const unsigned int *c;\n"
2055                "const unsigned int *d;\n"
2056                "Const unsigned int& e;\n"
2057                "const unsigned int& f;\n"
2058                "const unsigned      g;\n"
2059                "Const unsigned      h;",
2060                Style);
2061 
2062   Style.PointerAlignment = FormatStyle::PAS_Left;
2063   Style.ReferenceAlignment = FormatStyle::RAS_Middle;
2064   verifyFormat("int* f1(int* a, int & b, int && c);", Style);
2065   verifyFormat("int & f2(int && c, int* a, int & b);", Style);
2066   verifyFormat("int && f3(int & b, int && c, int* a);", Style);
2067   verifyFormat("int* a = f1();", Style);
2068   verifyFormat("int & b = f2();", Style);
2069   verifyFormat("int && c = f3();", Style);
2070   verifyFormat("for (auto a = 0, b = 0; const auto & c : {1, 2, 3})", Style);
2071   verifyFormat("for (auto a = 0, b = 0; const int & c : {1, 2, 3})", Style);
2072   verifyFormat("for (auto a = 0, b = 0; const Foo & c : {1, 2, 3})", Style);
2073   verifyFormat("for (auto a = 0, b = 0; const Foo* c : {1, 2, 3})", Style);
2074   verifyFormat("for (int a = 0, b++; const auto & c : {1, 2, 3})", Style);
2075   verifyFormat("for (int a = 0, b++; const int & c : {1, 2, 3})", Style);
2076   verifyFormat("for (int a = 0, b++; const Foo & c : {1, 2, 3})", Style);
2077   verifyFormat("for (int a = 0, b++; const Foo* c : {1, 2, 3})", Style);
2078   verifyFormat("for (auto x = 0; auto & c : {1, 2, 3})", Style);
2079   verifyFormat("for (auto x = 0; int & c : {1, 2, 3})", Style);
2080   verifyFormat("for (int x = 0; auto & c : {1, 2, 3})", Style);
2081   verifyFormat("for (int x = 0; int & c : {1, 2, 3})", Style);
2082   verifyFormat("for (f(); auto & c : {1, 2, 3})", Style);
2083   verifyFormat("for (f(); int & c : {1, 2, 3})", Style);
2084 
2085   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
2086   verifyFormat("Const unsigned int*  c;\n"
2087                "const unsigned int*  d;\n"
2088                "Const unsigned int & e;\n"
2089                "const unsigned int & f;\n"
2090                "const unsigned &&    g;\n"
2091                "Const unsigned       h;",
2092                Style);
2093 
2094   Style.PointerAlignment = FormatStyle::PAS_Middle;
2095   Style.ReferenceAlignment = FormatStyle::RAS_Right;
2096   verifyFormat("int * f1(int * a, int &b, int &&c);", Style);
2097   verifyFormat("int &f2(int &&c, int * a, int &b);", Style);
2098   verifyFormat("int &&f3(int &b, int &&c, int * a);", Style);
2099   verifyFormat("int * a = f1();", Style);
2100   verifyFormat("int &b = f2();", Style);
2101   verifyFormat("int &&c = f3();", Style);
2102   verifyFormat("for (auto a = 0, b = 0; const Foo * c : {1, 2, 3})", Style);
2103   verifyFormat("for (int a = 0, b = 0; const Foo * c : {1, 2, 3})", Style);
2104   verifyFormat("for (int a = 0, b++; const Foo * c : {1, 2, 3})", Style);
2105 
2106   // FIXME: we don't handle this yet, so output may be arbitrary until it's
2107   // specifically handled
2108   // verifyFormat("int Add2(BTree * &Root, char * szToAdd)", Style);
2109 }
2110 
2111 TEST_F(FormatTest, FormatsForLoop) {
2112   verifyFormat(
2113       "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
2114       "     ++VeryVeryLongLoopVariable)\n"
2115       "  ;");
2116   verifyFormat("for (;;)\n"
2117                "  f();");
2118   verifyFormat("for (;;) {\n}");
2119   verifyFormat("for (;;) {\n"
2120                "  f();\n"
2121                "}");
2122   verifyFormat("for (int i = 0; (i < 10); ++i) {\n}");
2123 
2124   verifyFormat(
2125       "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
2126       "                                          E = UnwrappedLines.end();\n"
2127       "     I != E; ++I) {\n}");
2128 
2129   verifyFormat(
2130       "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
2131       "     ++IIIII) {\n}");
2132   verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n"
2133                "         aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n"
2134                "     aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}");
2135   verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n"
2136                "         I = FD->getDeclsInPrototypeScope().begin(),\n"
2137                "         E = FD->getDeclsInPrototypeScope().end();\n"
2138                "     I != E; ++I) {\n}");
2139   verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n"
2140                "         I = Container.begin(),\n"
2141                "         E = Container.end();\n"
2142                "     I != E; ++I) {\n}",
2143                getLLVMStyleWithColumns(76));
2144 
2145   verifyFormat(
2146       "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
2147       "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n"
2148       "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2149       "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
2150       "     ++aaaaaaaaaaa) {\n}");
2151   verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
2152                "                bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n"
2153                "     ++i) {\n}");
2154   verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n"
2155                "     aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
2156                "}");
2157   verifyFormat("for (some_namespace::SomeIterator iter( // force break\n"
2158                "         aaaaaaaaaa);\n"
2159                "     iter; ++iter) {\n"
2160                "}");
2161   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2162                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
2163                "     aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n"
2164                "     ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {");
2165 
2166   // These should not be formatted as Objective-C for-in loops.
2167   verifyFormat("for (Foo *x = 0; x != in; x++) {\n}");
2168   verifyFormat("Foo *x;\nfor (x = 0; x != in; x++) {\n}");
2169   verifyFormat("Foo *x;\nfor (x in y) {\n}");
2170   verifyFormat(
2171       "for (const Foo<Bar> &baz = in.value(); !baz.at_end(); ++baz) {\n}");
2172 
2173   FormatStyle NoBinPacking = getLLVMStyle();
2174   NoBinPacking.BinPackParameters = false;
2175   verifyFormat("for (int aaaaaaaaaaa = 1;\n"
2176                "     aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n"
2177                "                                           aaaaaaaaaaaaaaaa,\n"
2178                "                                           aaaaaaaaaaaaaaaa,\n"
2179                "                                           aaaaaaaaaaaaaaaa);\n"
2180                "     aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
2181                "}",
2182                NoBinPacking);
2183   verifyFormat(
2184       "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
2185       "                                          E = UnwrappedLines.end();\n"
2186       "     I != E;\n"
2187       "     ++I) {\n}",
2188       NoBinPacking);
2189 
2190   FormatStyle AlignLeft = getLLVMStyle();
2191   AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
2192   verifyFormat("for (A* a = start; a < end; ++a, ++value) {\n}", AlignLeft);
2193 }
2194 
2195 TEST_F(FormatTest, RangeBasedForLoops) {
2196   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
2197                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
2198   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n"
2199                "     aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}");
2200   verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n"
2201                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
2202   verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n"
2203                "     aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}");
2204 }
2205 
2206 TEST_F(FormatTest, ForEachLoops) {
2207   FormatStyle Style = getLLVMStyle();
2208   EXPECT_EQ(Style.AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never);
2209   EXPECT_EQ(Style.AllowShortLoopsOnASingleLine, false);
2210   verifyFormat("void f() {\n"
2211                "  for (;;) {\n"
2212                "  }\n"
2213                "  foreach (Item *item, itemlist) {\n"
2214                "  }\n"
2215                "  Q_FOREACH (Item *item, itemlist) {\n"
2216                "  }\n"
2217                "  BOOST_FOREACH (Item *item, itemlist) {\n"
2218                "  }\n"
2219                "  UNKNOWN_FOREACH(Item * item, itemlist) {}\n"
2220                "}",
2221                Style);
2222   verifyFormat("void f() {\n"
2223                "  for (;;)\n"
2224                "    int j = 1;\n"
2225                "  Q_FOREACH (int v, vec)\n"
2226                "    v *= 2;\n"
2227                "  for (;;) {\n"
2228                "    int j = 1;\n"
2229                "  }\n"
2230                "  Q_FOREACH (int v, vec) {\n"
2231                "    v *= 2;\n"
2232                "  }\n"
2233                "}",
2234                Style);
2235 
2236   FormatStyle ShortBlocks = getLLVMStyle();
2237   ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
2238   EXPECT_EQ(ShortBlocks.AllowShortLoopsOnASingleLine, false);
2239   verifyFormat("void f() {\n"
2240                "  for (;;)\n"
2241                "    int j = 1;\n"
2242                "  Q_FOREACH (int &v, vec)\n"
2243                "    v *= 2;\n"
2244                "  for (;;) {\n"
2245                "    int j = 1;\n"
2246                "  }\n"
2247                "  Q_FOREACH (int &v, vec) {\n"
2248                "    int j = 1;\n"
2249                "  }\n"
2250                "}",
2251                ShortBlocks);
2252 
2253   FormatStyle ShortLoops = getLLVMStyle();
2254   ShortLoops.AllowShortLoopsOnASingleLine = true;
2255   EXPECT_EQ(ShortLoops.AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never);
2256   verifyFormat("void f() {\n"
2257                "  for (;;) int j = 1;\n"
2258                "  Q_FOREACH (int &v, vec) int j = 1;\n"
2259                "  for (;;) {\n"
2260                "    int j = 1;\n"
2261                "  }\n"
2262                "  Q_FOREACH (int &v, vec) {\n"
2263                "    int j = 1;\n"
2264                "  }\n"
2265                "}",
2266                ShortLoops);
2267 
2268   FormatStyle ShortBlocksAndLoops = getLLVMStyle();
2269   ShortBlocksAndLoops.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
2270   ShortBlocksAndLoops.AllowShortLoopsOnASingleLine = true;
2271   verifyFormat("void f() {\n"
2272                "  for (;;) int j = 1;\n"
2273                "  Q_FOREACH (int &v, vec) int j = 1;\n"
2274                "  for (;;) { int j = 1; }\n"
2275                "  Q_FOREACH (int &v, vec) { int j = 1; }\n"
2276                "}",
2277                ShortBlocksAndLoops);
2278 
2279   Style.SpaceBeforeParens =
2280       FormatStyle::SBPO_ControlStatementsExceptControlMacros;
2281   verifyFormat("void f() {\n"
2282                "  for (;;) {\n"
2283                "  }\n"
2284                "  foreach(Item *item, itemlist) {\n"
2285                "  }\n"
2286                "  Q_FOREACH(Item *item, itemlist) {\n"
2287                "  }\n"
2288                "  BOOST_FOREACH(Item *item, itemlist) {\n"
2289                "  }\n"
2290                "  UNKNOWN_FOREACH(Item * item, itemlist) {}\n"
2291                "}",
2292                Style);
2293 
2294   // As function-like macros.
2295   verifyFormat("#define foreach(x, y)\n"
2296                "#define Q_FOREACH(x, y)\n"
2297                "#define BOOST_FOREACH(x, y)\n"
2298                "#define UNKNOWN_FOREACH(x, y)\n");
2299 
2300   // Not as function-like macros.
2301   verifyFormat("#define foreach (x, y)\n"
2302                "#define Q_FOREACH (x, y)\n"
2303                "#define BOOST_FOREACH (x, y)\n"
2304                "#define UNKNOWN_FOREACH (x, y)\n");
2305 
2306   // handle microsoft non standard extension
2307   verifyFormat("for each (char c in x->MyStringProperty)");
2308 }
2309 
2310 TEST_F(FormatTest, FormatsWhileLoop) {
2311   verifyFormat("while (true) {\n}");
2312   verifyFormat("while (true)\n"
2313                "  f();");
2314   verifyFormat("while () {\n}");
2315   verifyFormat("while () {\n"
2316                "  f();\n"
2317                "}");
2318 }
2319 
2320 TEST_F(FormatTest, FormatsDoWhile) {
2321   verifyFormat("do {\n"
2322                "  do_something();\n"
2323                "} while (something());");
2324   verifyFormat("do\n"
2325                "  do_something();\n"
2326                "while (something());");
2327 }
2328 
2329 TEST_F(FormatTest, FormatsSwitchStatement) {
2330   verifyFormat("switch (x) {\n"
2331                "case 1:\n"
2332                "  f();\n"
2333                "  break;\n"
2334                "case kFoo:\n"
2335                "case ns::kBar:\n"
2336                "case kBaz:\n"
2337                "  break;\n"
2338                "default:\n"
2339                "  g();\n"
2340                "  break;\n"
2341                "}");
2342   verifyFormat("switch (x) {\n"
2343                "case 1: {\n"
2344                "  f();\n"
2345                "  break;\n"
2346                "}\n"
2347                "case 2: {\n"
2348                "  break;\n"
2349                "}\n"
2350                "}");
2351   verifyFormat("switch (x) {\n"
2352                "case 1: {\n"
2353                "  f();\n"
2354                "  {\n"
2355                "    g();\n"
2356                "    h();\n"
2357                "  }\n"
2358                "  break;\n"
2359                "}\n"
2360                "}");
2361   verifyFormat("switch (x) {\n"
2362                "case 1: {\n"
2363                "  f();\n"
2364                "  if (foo) {\n"
2365                "    g();\n"
2366                "    h();\n"
2367                "  }\n"
2368                "  break;\n"
2369                "}\n"
2370                "}");
2371   verifyFormat("switch (x) {\n"
2372                "case 1: {\n"
2373                "  f();\n"
2374                "  g();\n"
2375                "} break;\n"
2376                "}");
2377   verifyFormat("switch (test)\n"
2378                "  ;");
2379   verifyFormat("switch (x) {\n"
2380                "default: {\n"
2381                "  // Do nothing.\n"
2382                "}\n"
2383                "}");
2384   verifyFormat("switch (x) {\n"
2385                "// comment\n"
2386                "// if 1, do f()\n"
2387                "case 1:\n"
2388                "  f();\n"
2389                "}");
2390   verifyFormat("switch (x) {\n"
2391                "case 1:\n"
2392                "  // Do amazing stuff\n"
2393                "  {\n"
2394                "    f();\n"
2395                "    g();\n"
2396                "  }\n"
2397                "  break;\n"
2398                "}");
2399   verifyFormat("#define A          \\\n"
2400                "  switch (x) {     \\\n"
2401                "  case a:          \\\n"
2402                "    foo = b;       \\\n"
2403                "  }",
2404                getLLVMStyleWithColumns(20));
2405   verifyFormat("#define OPERATION_CASE(name)           \\\n"
2406                "  case OP_name:                        \\\n"
2407                "    return operations::Operation##name\n",
2408                getLLVMStyleWithColumns(40));
2409   verifyFormat("switch (x) {\n"
2410                "case 1:;\n"
2411                "default:;\n"
2412                "  int i;\n"
2413                "}");
2414 
2415   verifyGoogleFormat("switch (x) {\n"
2416                      "  case 1:\n"
2417                      "    f();\n"
2418                      "    break;\n"
2419                      "  case kFoo:\n"
2420                      "  case ns::kBar:\n"
2421                      "  case kBaz:\n"
2422                      "    break;\n"
2423                      "  default:\n"
2424                      "    g();\n"
2425                      "    break;\n"
2426                      "}");
2427   verifyGoogleFormat("switch (x) {\n"
2428                      "  case 1: {\n"
2429                      "    f();\n"
2430                      "    break;\n"
2431                      "  }\n"
2432                      "}");
2433   verifyGoogleFormat("switch (test)\n"
2434                      "  ;");
2435 
2436   verifyGoogleFormat("#define OPERATION_CASE(name) \\\n"
2437                      "  case OP_name:              \\\n"
2438                      "    return operations::Operation##name\n");
2439   verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n"
2440                      "  // Get the correction operation class.\n"
2441                      "  switch (OpCode) {\n"
2442                      "    CASE(Add);\n"
2443                      "    CASE(Subtract);\n"
2444                      "    default:\n"
2445                      "      return operations::Unknown;\n"
2446                      "  }\n"
2447                      "#undef OPERATION_CASE\n"
2448                      "}");
2449   verifyFormat("DEBUG({\n"
2450                "  switch (x) {\n"
2451                "  case A:\n"
2452                "    f();\n"
2453                "    break;\n"
2454                "    // fallthrough\n"
2455                "  case B:\n"
2456                "    g();\n"
2457                "    break;\n"
2458                "  }\n"
2459                "});");
2460   EXPECT_EQ("DEBUG({\n"
2461             "  switch (x) {\n"
2462             "  case A:\n"
2463             "    f();\n"
2464             "    break;\n"
2465             "  // On B:\n"
2466             "  case B:\n"
2467             "    g();\n"
2468             "    break;\n"
2469             "  }\n"
2470             "});",
2471             format("DEBUG({\n"
2472                    "  switch (x) {\n"
2473                    "  case A:\n"
2474                    "    f();\n"
2475                    "    break;\n"
2476                    "  // On B:\n"
2477                    "  case B:\n"
2478                    "    g();\n"
2479                    "    break;\n"
2480                    "  }\n"
2481                    "});",
2482                    getLLVMStyle()));
2483   EXPECT_EQ("switch (n) {\n"
2484             "case 0: {\n"
2485             "  return false;\n"
2486             "}\n"
2487             "default: {\n"
2488             "  return true;\n"
2489             "}\n"
2490             "}",
2491             format("switch (n)\n"
2492                    "{\n"
2493                    "case 0: {\n"
2494                    "  return false;\n"
2495                    "}\n"
2496                    "default: {\n"
2497                    "  return true;\n"
2498                    "}\n"
2499                    "}",
2500                    getLLVMStyle()));
2501   verifyFormat("switch (a) {\n"
2502                "case (b):\n"
2503                "  return;\n"
2504                "}");
2505 
2506   verifyFormat("switch (a) {\n"
2507                "case some_namespace::\n"
2508                "    some_constant:\n"
2509                "  return;\n"
2510                "}",
2511                getLLVMStyleWithColumns(34));
2512 
2513   FormatStyle Style = getLLVMStyle();
2514   Style.IndentCaseLabels = true;
2515   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
2516   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
2517   Style.BraceWrapping.AfterCaseLabel = true;
2518   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
2519   EXPECT_EQ("switch (n)\n"
2520             "{\n"
2521             "  case 0:\n"
2522             "  {\n"
2523             "    return false;\n"
2524             "  }\n"
2525             "  default:\n"
2526             "  {\n"
2527             "    return true;\n"
2528             "  }\n"
2529             "}",
2530             format("switch (n) {\n"
2531                    "  case 0: {\n"
2532                    "    return false;\n"
2533                    "  }\n"
2534                    "  default: {\n"
2535                    "    return true;\n"
2536                    "  }\n"
2537                    "}",
2538                    Style));
2539   Style.BraceWrapping.AfterCaseLabel = false;
2540   EXPECT_EQ("switch (n)\n"
2541             "{\n"
2542             "  case 0: {\n"
2543             "    return false;\n"
2544             "  }\n"
2545             "  default: {\n"
2546             "    return true;\n"
2547             "  }\n"
2548             "}",
2549             format("switch (n) {\n"
2550                    "  case 0:\n"
2551                    "  {\n"
2552                    "    return false;\n"
2553                    "  }\n"
2554                    "  default:\n"
2555                    "  {\n"
2556                    "    return true;\n"
2557                    "  }\n"
2558                    "}",
2559                    Style));
2560   Style.IndentCaseLabels = false;
2561   Style.IndentCaseBlocks = true;
2562   EXPECT_EQ("switch (n)\n"
2563             "{\n"
2564             "case 0:\n"
2565             "  {\n"
2566             "    return false;\n"
2567             "  }\n"
2568             "case 1:\n"
2569             "  break;\n"
2570             "default:\n"
2571             "  {\n"
2572             "    return true;\n"
2573             "  }\n"
2574             "}",
2575             format("switch (n) {\n"
2576                    "case 0: {\n"
2577                    "  return false;\n"
2578                    "}\n"
2579                    "case 1:\n"
2580                    "  break;\n"
2581                    "default: {\n"
2582                    "  return true;\n"
2583                    "}\n"
2584                    "}",
2585                    Style));
2586   Style.IndentCaseLabels = true;
2587   Style.IndentCaseBlocks = true;
2588   EXPECT_EQ("switch (n)\n"
2589             "{\n"
2590             "  case 0:\n"
2591             "    {\n"
2592             "      return false;\n"
2593             "    }\n"
2594             "  case 1:\n"
2595             "    break;\n"
2596             "  default:\n"
2597             "    {\n"
2598             "      return true;\n"
2599             "    }\n"
2600             "}",
2601             format("switch (n) {\n"
2602                    "case 0: {\n"
2603                    "  return false;\n"
2604                    "}\n"
2605                    "case 1:\n"
2606                    "  break;\n"
2607                    "default: {\n"
2608                    "  return true;\n"
2609                    "}\n"
2610                    "}",
2611                    Style));
2612 }
2613 
2614 TEST_F(FormatTest, CaseRanges) {
2615   verifyFormat("switch (x) {\n"
2616                "case 'A' ... 'Z':\n"
2617                "case 1 ... 5:\n"
2618                "case a ... b:\n"
2619                "  break;\n"
2620                "}");
2621 }
2622 
2623 TEST_F(FormatTest, ShortEnums) {
2624   FormatStyle Style = getLLVMStyle();
2625   Style.AllowShortEnumsOnASingleLine = true;
2626   verifyFormat("enum { A, B, C } ShortEnum1, ShortEnum2;", Style);
2627   verifyFormat("typedef enum { A, B, C } ShortEnum1, ShortEnum2;", Style);
2628   Style.AllowShortEnumsOnASingleLine = false;
2629   verifyFormat("enum {\n"
2630                "  A,\n"
2631                "  B,\n"
2632                "  C\n"
2633                "} ShortEnum1, ShortEnum2;",
2634                Style);
2635   verifyFormat("typedef enum {\n"
2636                "  A,\n"
2637                "  B,\n"
2638                "  C\n"
2639                "} ShortEnum1, ShortEnum2;",
2640                Style);
2641   verifyFormat("enum {\n"
2642                "  A,\n"
2643                "} ShortEnum1, ShortEnum2;",
2644                Style);
2645   verifyFormat("typedef enum {\n"
2646                "  A,\n"
2647                "} ShortEnum1, ShortEnum2;",
2648                Style);
2649   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
2650   Style.BraceWrapping.AfterEnum = true;
2651   verifyFormat("enum\n"
2652                "{\n"
2653                "  A,\n"
2654                "  B,\n"
2655                "  C\n"
2656                "} ShortEnum1, ShortEnum2;",
2657                Style);
2658   verifyFormat("typedef enum\n"
2659                "{\n"
2660                "  A,\n"
2661                "  B,\n"
2662                "  C\n"
2663                "} ShortEnum1, ShortEnum2;",
2664                Style);
2665 }
2666 
2667 TEST_F(FormatTest, ShortCaseLabels) {
2668   FormatStyle Style = getLLVMStyle();
2669   Style.AllowShortCaseLabelsOnASingleLine = true;
2670   verifyFormat("switch (a) {\n"
2671                "case 1: x = 1; break;\n"
2672                "case 2: return;\n"
2673                "case 3:\n"
2674                "case 4:\n"
2675                "case 5: return;\n"
2676                "case 6: // comment\n"
2677                "  return;\n"
2678                "case 7:\n"
2679                "  // comment\n"
2680                "  return;\n"
2681                "case 8:\n"
2682                "  x = 8; // comment\n"
2683                "  break;\n"
2684                "default: y = 1; break;\n"
2685                "}",
2686                Style);
2687   verifyFormat("switch (a) {\n"
2688                "case 0: return; // comment\n"
2689                "case 1: break;  // comment\n"
2690                "case 2: return;\n"
2691                "// comment\n"
2692                "case 3: return;\n"
2693                "// comment 1\n"
2694                "// comment 2\n"
2695                "// comment 3\n"
2696                "case 4: break; /* comment */\n"
2697                "case 5:\n"
2698                "  // comment\n"
2699                "  break;\n"
2700                "case 6: /* comment */ x = 1; break;\n"
2701                "case 7: x = /* comment */ 1; break;\n"
2702                "case 8:\n"
2703                "  x = 1; /* comment */\n"
2704                "  break;\n"
2705                "case 9:\n"
2706                "  break; // comment line 1\n"
2707                "         // comment line 2\n"
2708                "}",
2709                Style);
2710   EXPECT_EQ("switch (a) {\n"
2711             "case 1:\n"
2712             "  x = 8;\n"
2713             "  // fall through\n"
2714             "case 2: x = 8;\n"
2715             "// comment\n"
2716             "case 3:\n"
2717             "  return; /* comment line 1\n"
2718             "           * comment line 2 */\n"
2719             "case 4: i = 8;\n"
2720             "// something else\n"
2721             "#if FOO\n"
2722             "case 5: break;\n"
2723             "#endif\n"
2724             "}",
2725             format("switch (a) {\n"
2726                    "case 1: x = 8;\n"
2727                    "  // fall through\n"
2728                    "case 2:\n"
2729                    "  x = 8;\n"
2730                    "// comment\n"
2731                    "case 3:\n"
2732                    "  return; /* comment line 1\n"
2733                    "           * comment line 2 */\n"
2734                    "case 4:\n"
2735                    "  i = 8;\n"
2736                    "// something else\n"
2737                    "#if FOO\n"
2738                    "case 5: break;\n"
2739                    "#endif\n"
2740                    "}",
2741                    Style));
2742   EXPECT_EQ("switch (a) {\n"
2743             "case 0:\n"
2744             "  return; // long long long long long long long long long long "
2745             "long long comment\n"
2746             "          // line\n"
2747             "}",
2748             format("switch (a) {\n"
2749                    "case 0: return; // long long long long long long long long "
2750                    "long long long long comment line\n"
2751                    "}",
2752                    Style));
2753   EXPECT_EQ("switch (a) {\n"
2754             "case 0:\n"
2755             "  return; /* long long long long long long long long long long "
2756             "long long comment\n"
2757             "             line */\n"
2758             "}",
2759             format("switch (a) {\n"
2760                    "case 0: return; /* long long long long long long long long "
2761                    "long long long long comment line */\n"
2762                    "}",
2763                    Style));
2764   verifyFormat("switch (a) {\n"
2765                "#if FOO\n"
2766                "case 0: return 0;\n"
2767                "#endif\n"
2768                "}",
2769                Style);
2770   verifyFormat("switch (a) {\n"
2771                "case 1: {\n"
2772                "}\n"
2773                "case 2: {\n"
2774                "  return;\n"
2775                "}\n"
2776                "case 3: {\n"
2777                "  x = 1;\n"
2778                "  return;\n"
2779                "}\n"
2780                "case 4:\n"
2781                "  if (x)\n"
2782                "    return;\n"
2783                "}",
2784                Style);
2785   Style.ColumnLimit = 21;
2786   verifyFormat("switch (a) {\n"
2787                "case 1: x = 1; break;\n"
2788                "case 2: return;\n"
2789                "case 3:\n"
2790                "case 4:\n"
2791                "case 5: return;\n"
2792                "default:\n"
2793                "  y = 1;\n"
2794                "  break;\n"
2795                "}",
2796                Style);
2797   Style.ColumnLimit = 80;
2798   Style.AllowShortCaseLabelsOnASingleLine = false;
2799   Style.IndentCaseLabels = true;
2800   EXPECT_EQ("switch (n) {\n"
2801             "  default /*comments*/:\n"
2802             "    return true;\n"
2803             "  case 0:\n"
2804             "    return false;\n"
2805             "}",
2806             format("switch (n) {\n"
2807                    "default/*comments*/:\n"
2808                    "  return true;\n"
2809                    "case 0:\n"
2810                    "  return false;\n"
2811                    "}",
2812                    Style));
2813   Style.AllowShortCaseLabelsOnASingleLine = true;
2814   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
2815   Style.BraceWrapping.AfterCaseLabel = true;
2816   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
2817   EXPECT_EQ("switch (n)\n"
2818             "{\n"
2819             "  case 0:\n"
2820             "  {\n"
2821             "    return false;\n"
2822             "  }\n"
2823             "  default:\n"
2824             "  {\n"
2825             "    return true;\n"
2826             "  }\n"
2827             "}",
2828             format("switch (n) {\n"
2829                    "  case 0: {\n"
2830                    "    return false;\n"
2831                    "  }\n"
2832                    "  default:\n"
2833                    "  {\n"
2834                    "    return true;\n"
2835                    "  }\n"
2836                    "}",
2837                    Style));
2838 }
2839 
2840 TEST_F(FormatTest, FormatsLabels) {
2841   verifyFormat("void f() {\n"
2842                "  some_code();\n"
2843                "test_label:\n"
2844                "  some_other_code();\n"
2845                "  {\n"
2846                "    some_more_code();\n"
2847                "  another_label:\n"
2848                "    some_more_code();\n"
2849                "  }\n"
2850                "}");
2851   verifyFormat("{\n"
2852                "  some_code();\n"
2853                "test_label:\n"
2854                "  some_other_code();\n"
2855                "}");
2856   verifyFormat("{\n"
2857                "  some_code();\n"
2858                "test_label:;\n"
2859                "  int i = 0;\n"
2860                "}");
2861   FormatStyle Style = getLLVMStyle();
2862   Style.IndentGotoLabels = false;
2863   verifyFormat("void f() {\n"
2864                "  some_code();\n"
2865                "test_label:\n"
2866                "  some_other_code();\n"
2867                "  {\n"
2868                "    some_more_code();\n"
2869                "another_label:\n"
2870                "    some_more_code();\n"
2871                "  }\n"
2872                "}",
2873                Style);
2874   verifyFormat("{\n"
2875                "  some_code();\n"
2876                "test_label:\n"
2877                "  some_other_code();\n"
2878                "}",
2879                Style);
2880   verifyFormat("{\n"
2881                "  some_code();\n"
2882                "test_label:;\n"
2883                "  int i = 0;\n"
2884                "}");
2885 }
2886 
2887 TEST_F(FormatTest, MultiLineControlStatements) {
2888   FormatStyle Style = getLLVMStyleWithColumns(20);
2889   Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom;
2890   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine;
2891   // Short lines should keep opening brace on same line.
2892   EXPECT_EQ("if (foo) {\n"
2893             "  bar();\n"
2894             "}",
2895             format("if(foo){bar();}", Style));
2896   EXPECT_EQ("if (foo) {\n"
2897             "  bar();\n"
2898             "} else {\n"
2899             "  baz();\n"
2900             "}",
2901             format("if(foo){bar();}else{baz();}", Style));
2902   EXPECT_EQ("if (foo && bar) {\n"
2903             "  baz();\n"
2904             "}",
2905             format("if(foo&&bar){baz();}", Style));
2906   EXPECT_EQ("if (foo) {\n"
2907             "  bar();\n"
2908             "} else if (baz) {\n"
2909             "  quux();\n"
2910             "}",
2911             format("if(foo){bar();}else if(baz){quux();}", Style));
2912   EXPECT_EQ(
2913       "if (foo) {\n"
2914       "  bar();\n"
2915       "} else if (baz) {\n"
2916       "  quux();\n"
2917       "} else {\n"
2918       "  foobar();\n"
2919       "}",
2920       format("if(foo){bar();}else if(baz){quux();}else{foobar();}", Style));
2921   EXPECT_EQ("for (;;) {\n"
2922             "  foo();\n"
2923             "}",
2924             format("for(;;){foo();}"));
2925   EXPECT_EQ("while (1) {\n"
2926             "  foo();\n"
2927             "}",
2928             format("while(1){foo();}", Style));
2929   EXPECT_EQ("switch (foo) {\n"
2930             "case bar:\n"
2931             "  return;\n"
2932             "}",
2933             format("switch(foo){case bar:return;}", Style));
2934   EXPECT_EQ("try {\n"
2935             "  foo();\n"
2936             "} catch (...) {\n"
2937             "  bar();\n"
2938             "}",
2939             format("try{foo();}catch(...){bar();}", Style));
2940   EXPECT_EQ("do {\n"
2941             "  foo();\n"
2942             "} while (bar &&\n"
2943             "         baz);",
2944             format("do{foo();}while(bar&&baz);", Style));
2945   // Long lines should put opening brace on new line.
2946   EXPECT_EQ("if (foo && bar &&\n"
2947             "    baz)\n"
2948             "{\n"
2949             "  quux();\n"
2950             "}",
2951             format("if(foo&&bar&&baz){quux();}", Style));
2952   EXPECT_EQ("if (foo && bar &&\n"
2953             "    baz)\n"
2954             "{\n"
2955             "  quux();\n"
2956             "}",
2957             format("if (foo && bar &&\n"
2958                    "    baz) {\n"
2959                    "  quux();\n"
2960                    "}",
2961                    Style));
2962   EXPECT_EQ("if (foo) {\n"
2963             "  bar();\n"
2964             "} else if (baz ||\n"
2965             "           quux)\n"
2966             "{\n"
2967             "  foobar();\n"
2968             "}",
2969             format("if(foo){bar();}else if(baz||quux){foobar();}", Style));
2970   EXPECT_EQ(
2971       "if (foo) {\n"
2972       "  bar();\n"
2973       "} else if (baz ||\n"
2974       "           quux)\n"
2975       "{\n"
2976       "  foobar();\n"
2977       "} else {\n"
2978       "  barbaz();\n"
2979       "}",
2980       format("if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}",
2981              Style));
2982   EXPECT_EQ("for (int i = 0;\n"
2983             "     i < 10; ++i)\n"
2984             "{\n"
2985             "  foo();\n"
2986             "}",
2987             format("for(int i=0;i<10;++i){foo();}", Style));
2988   EXPECT_EQ("foreach (int i,\n"
2989             "         list)\n"
2990             "{\n"
2991             "  foo();\n"
2992             "}",
2993             format("foreach(int i, list){foo();}", Style));
2994   Style.ColumnLimit =
2995       40; // to concentrate at brace wrapping, not line wrap due to column limit
2996   EXPECT_EQ("foreach (int i, list) {\n"
2997             "  foo();\n"
2998             "}",
2999             format("foreach(int i, list){foo();}", Style));
3000   Style.ColumnLimit =
3001       20; // to concentrate at brace wrapping, not line wrap due to column limit
3002   EXPECT_EQ("while (foo || bar ||\n"
3003             "       baz)\n"
3004             "{\n"
3005             "  quux();\n"
3006             "}",
3007             format("while(foo||bar||baz){quux();}", Style));
3008   EXPECT_EQ("switch (\n"
3009             "    foo = barbaz)\n"
3010             "{\n"
3011             "case quux:\n"
3012             "  return;\n"
3013             "}",
3014             format("switch(foo=barbaz){case quux:return;}", Style));
3015   EXPECT_EQ("try {\n"
3016             "  foo();\n"
3017             "} catch (\n"
3018             "    Exception &bar)\n"
3019             "{\n"
3020             "  baz();\n"
3021             "}",
3022             format("try{foo();}catch(Exception&bar){baz();}", Style));
3023   Style.ColumnLimit =
3024       40; // to concentrate at brace wrapping, not line wrap due to column limit
3025   EXPECT_EQ("try {\n"
3026             "  foo();\n"
3027             "} catch (Exception &bar) {\n"
3028             "  baz();\n"
3029             "}",
3030             format("try{foo();}catch(Exception&bar){baz();}", Style));
3031   Style.ColumnLimit =
3032       20; // to concentrate at brace wrapping, not line wrap due to column limit
3033 
3034   Style.BraceWrapping.BeforeElse = true;
3035   EXPECT_EQ(
3036       "if (foo) {\n"
3037       "  bar();\n"
3038       "}\n"
3039       "else if (baz ||\n"
3040       "         quux)\n"
3041       "{\n"
3042       "  foobar();\n"
3043       "}\n"
3044       "else {\n"
3045       "  barbaz();\n"
3046       "}",
3047       format("if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}",
3048              Style));
3049 
3050   Style.BraceWrapping.BeforeCatch = true;
3051   EXPECT_EQ("try {\n"
3052             "  foo();\n"
3053             "}\n"
3054             "catch (...) {\n"
3055             "  baz();\n"
3056             "}",
3057             format("try{foo();}catch(...){baz();}", Style));
3058 
3059   Style.BraceWrapping.AfterFunction = true;
3060   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine;
3061   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
3062   Style.ColumnLimit = 80;
3063   verifyFormat("void shortfunction() { bar(); }", Style);
3064 
3065   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
3066   verifyFormat("void shortfunction()\n"
3067                "{\n"
3068                "  bar();\n"
3069                "}",
3070                Style);
3071 }
3072 
3073 TEST_F(FormatTest, BeforeWhile) {
3074   FormatStyle Style = getLLVMStyle();
3075   Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom;
3076 
3077   verifyFormat("do {\n"
3078                "  foo();\n"
3079                "} while (1);",
3080                Style);
3081   Style.BraceWrapping.BeforeWhile = true;
3082   verifyFormat("do {\n"
3083                "  foo();\n"
3084                "}\n"
3085                "while (1);",
3086                Style);
3087 }
3088 
3089 //===----------------------------------------------------------------------===//
3090 // Tests for classes, namespaces, etc.
3091 //===----------------------------------------------------------------------===//
3092 
3093 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
3094   verifyFormat("class A {};");
3095 }
3096 
3097 TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
3098   verifyFormat("class A {\n"
3099                "public:\n"
3100                "public: // comment\n"
3101                "protected:\n"
3102                "private:\n"
3103                "  void f() {}\n"
3104                "};");
3105   verifyFormat("export class A {\n"
3106                "public:\n"
3107                "public: // comment\n"
3108                "protected:\n"
3109                "private:\n"
3110                "  void f() {}\n"
3111                "};");
3112   verifyGoogleFormat("class A {\n"
3113                      " public:\n"
3114                      " protected:\n"
3115                      " private:\n"
3116                      "  void f() {}\n"
3117                      "};");
3118   verifyGoogleFormat("export class A {\n"
3119                      " public:\n"
3120                      " protected:\n"
3121                      " private:\n"
3122                      "  void f() {}\n"
3123                      "};");
3124   verifyFormat("class A {\n"
3125                "public slots:\n"
3126                "  void f1() {}\n"
3127                "public Q_SLOTS:\n"
3128                "  void f2() {}\n"
3129                "protected slots:\n"
3130                "  void f3() {}\n"
3131                "protected Q_SLOTS:\n"
3132                "  void f4() {}\n"
3133                "private slots:\n"
3134                "  void f5() {}\n"
3135                "private Q_SLOTS:\n"
3136                "  void f6() {}\n"
3137                "signals:\n"
3138                "  void g1();\n"
3139                "Q_SIGNALS:\n"
3140                "  void g2();\n"
3141                "};");
3142 
3143   // Don't interpret 'signals' the wrong way.
3144   verifyFormat("signals.set();");
3145   verifyFormat("for (Signals signals : f()) {\n}");
3146   verifyFormat("{\n"
3147                "  signals.set(); // This needs indentation.\n"
3148                "}");
3149   verifyFormat("void f() {\n"
3150                "label:\n"
3151                "  signals.baz();\n"
3152                "}");
3153   verifyFormat("private[1];");
3154   verifyFormat("testArray[public] = 1;");
3155   verifyFormat("public();");
3156   verifyFormat("myFunc(public);");
3157   verifyFormat("std::vector<int> testVec = {private};");
3158   verifyFormat("private.p = 1;");
3159   verifyFormat("void function(private...){};");
3160   verifyFormat("if (private && public)\n");
3161   verifyFormat("private &= true;");
3162   verifyFormat("int x = private * public;");
3163   verifyFormat("public *= private;");
3164   verifyFormat("int x = public + private;");
3165   verifyFormat("private++;");
3166   verifyFormat("++private;");
3167   verifyFormat("public += private;");
3168   verifyFormat("public = public - private;");
3169   verifyFormat("public->foo();");
3170   verifyFormat("private--;");
3171   verifyFormat("--private;");
3172   verifyFormat("public -= 1;");
3173   verifyFormat("if (!private && !public)\n");
3174   verifyFormat("public != private;");
3175   verifyFormat("int x = public / private;");
3176   verifyFormat("public /= 2;");
3177   verifyFormat("public = public % 2;");
3178   verifyFormat("public %= 2;");
3179   verifyFormat("if (public < private)\n");
3180   verifyFormat("public << private;");
3181   verifyFormat("public <<= private;");
3182   verifyFormat("if (public > private)\n");
3183   verifyFormat("public >> private;");
3184   verifyFormat("public >>= private;");
3185   verifyFormat("public ^ private;");
3186   verifyFormat("public ^= private;");
3187   verifyFormat("public | private;");
3188   verifyFormat("public |= private;");
3189   verifyFormat("auto x = private ? 1 : 2;");
3190   verifyFormat("if (public == private)\n");
3191   verifyFormat("void foo(public, private)");
3192   verifyFormat("public::foo();");
3193 }
3194 
3195 TEST_F(FormatTest, SeparatesLogicalBlocks) {
3196   EXPECT_EQ("class A {\n"
3197             "public:\n"
3198             "  void f();\n"
3199             "\n"
3200             "private:\n"
3201             "  void g() {}\n"
3202             "  // test\n"
3203             "protected:\n"
3204             "  int h;\n"
3205             "};",
3206             format("class A {\n"
3207                    "public:\n"
3208                    "void f();\n"
3209                    "private:\n"
3210                    "void g() {}\n"
3211                    "// test\n"
3212                    "protected:\n"
3213                    "int h;\n"
3214                    "};"));
3215   EXPECT_EQ("class A {\n"
3216             "protected:\n"
3217             "public:\n"
3218             "  void f();\n"
3219             "};",
3220             format("class A {\n"
3221                    "protected:\n"
3222                    "\n"
3223                    "public:\n"
3224                    "\n"
3225                    "  void f();\n"
3226                    "};"));
3227 
3228   // Even ensure proper spacing inside macros.
3229   EXPECT_EQ("#define B     \\\n"
3230             "  class A {   \\\n"
3231             "   protected: \\\n"
3232             "   public:    \\\n"
3233             "    void f(); \\\n"
3234             "  };",
3235             format("#define B     \\\n"
3236                    "  class A {   \\\n"
3237                    "   protected: \\\n"
3238                    "              \\\n"
3239                    "   public:    \\\n"
3240                    "              \\\n"
3241                    "    void f(); \\\n"
3242                    "  };",
3243                    getGoogleStyle()));
3244   // But don't remove empty lines after macros ending in access specifiers.
3245   EXPECT_EQ("#define A private:\n"
3246             "\n"
3247             "int i;",
3248             format("#define A         private:\n"
3249                    "\n"
3250                    "int              i;"));
3251 }
3252 
3253 TEST_F(FormatTest, FormatsClasses) {
3254   verifyFormat("class A : public B {};");
3255   verifyFormat("class A : public ::B {};");
3256 
3257   verifyFormat(
3258       "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3259       "                             public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
3260   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
3261                "    : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3262                "      public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
3263   verifyFormat(
3264       "class A : public B, public C, public D, public E, public F {};");
3265   verifyFormat("class AAAAAAAAAAAA : public B,\n"
3266                "                     public C,\n"
3267                "                     public D,\n"
3268                "                     public E,\n"
3269                "                     public F,\n"
3270                "                     public G {};");
3271 
3272   verifyFormat("class\n"
3273                "    ReallyReallyLongClassName {\n"
3274                "  int i;\n"
3275                "};",
3276                getLLVMStyleWithColumns(32));
3277   verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
3278                "                           aaaaaaaaaaaaaaaa> {};");
3279   verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n"
3280                "    : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n"
3281                "                                 aaaaaaaaaaaaaaaaaaaaaa> {};");
3282   verifyFormat("template <class R, class C>\n"
3283                "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n"
3284                "    : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};");
3285   verifyFormat("class ::A::B {};");
3286 }
3287 
3288 TEST_F(FormatTest, BreakInheritanceStyle) {
3289   FormatStyle StyleWithInheritanceBreakBeforeComma = getLLVMStyle();
3290   StyleWithInheritanceBreakBeforeComma.BreakInheritanceList =
3291       FormatStyle::BILS_BeforeComma;
3292   verifyFormat("class MyClass : public X {};",
3293                StyleWithInheritanceBreakBeforeComma);
3294   verifyFormat("class MyClass\n"
3295                "    : public X\n"
3296                "    , public Y {};",
3297                StyleWithInheritanceBreakBeforeComma);
3298   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA\n"
3299                "    : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n"
3300                "    , public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};",
3301                StyleWithInheritanceBreakBeforeComma);
3302   verifyFormat("struct aaaaaaaaaaaaa\n"
3303                "    : public aaaaaaaaaaaaaaaaaaa< // break\n"
3304                "          aaaaaaaaaaaaaaaa> {};",
3305                StyleWithInheritanceBreakBeforeComma);
3306 
3307   FormatStyle StyleWithInheritanceBreakAfterColon = getLLVMStyle();
3308   StyleWithInheritanceBreakAfterColon.BreakInheritanceList =
3309       FormatStyle::BILS_AfterColon;
3310   verifyFormat("class MyClass : public X {};",
3311                StyleWithInheritanceBreakAfterColon);
3312   verifyFormat("class MyClass : public X, public Y {};",
3313                StyleWithInheritanceBreakAfterColon);
3314   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA :\n"
3315                "    public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3316                "    public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};",
3317                StyleWithInheritanceBreakAfterColon);
3318   verifyFormat("struct aaaaaaaaaaaaa :\n"
3319                "    public aaaaaaaaaaaaaaaaaaa< // break\n"
3320                "        aaaaaaaaaaaaaaaa> {};",
3321                StyleWithInheritanceBreakAfterColon);
3322 
3323   FormatStyle StyleWithInheritanceBreakAfterComma = getLLVMStyle();
3324   StyleWithInheritanceBreakAfterComma.BreakInheritanceList =
3325       FormatStyle::BILS_AfterComma;
3326   verifyFormat("class MyClass : public X {};",
3327                StyleWithInheritanceBreakAfterComma);
3328   verifyFormat("class MyClass : public X,\n"
3329                "                public Y {};",
3330                StyleWithInheritanceBreakAfterComma);
3331   verifyFormat(
3332       "class AAAAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3333       "                               public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC "
3334       "{};",
3335       StyleWithInheritanceBreakAfterComma);
3336   verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
3337                "                           aaaaaaaaaaaaaaaa> {};",
3338                StyleWithInheritanceBreakAfterComma);
3339   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
3340                "    : public OnceBreak,\n"
3341                "      public AlwaysBreak,\n"
3342                "      EvenBasesFitInOneLine {};",
3343                StyleWithInheritanceBreakAfterComma);
3344 }
3345 
3346 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
3347   verifyFormat("class A {\n} a, b;");
3348   verifyFormat("struct A {\n} a, b;");
3349   verifyFormat("union A {\n} a;");
3350 }
3351 
3352 TEST_F(FormatTest, FormatsEnum) {
3353   verifyFormat("enum {\n"
3354                "  Zero,\n"
3355                "  One = 1,\n"
3356                "  Two = One + 1,\n"
3357                "  Three = (One + Two),\n"
3358                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
3359                "  Five = (One, Two, Three, Four, 5)\n"
3360                "};");
3361   verifyGoogleFormat("enum {\n"
3362                      "  Zero,\n"
3363                      "  One = 1,\n"
3364                      "  Two = One + 1,\n"
3365                      "  Three = (One + Two),\n"
3366                      "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
3367                      "  Five = (One, Two, Three, Four, 5)\n"
3368                      "};");
3369   verifyFormat("enum Enum {};");
3370   verifyFormat("enum {};");
3371   verifyFormat("enum X E {} d;");
3372   verifyFormat("enum __attribute__((...)) E {} d;");
3373   verifyFormat("enum __declspec__((...)) E {} d;");
3374   verifyFormat("enum {\n"
3375                "  Bar = Foo<int, int>::value\n"
3376                "};",
3377                getLLVMStyleWithColumns(30));
3378 
3379   verifyFormat("enum ShortEnum { A, B, C };");
3380   verifyGoogleFormat("enum ShortEnum { A, B, C };");
3381 
3382   EXPECT_EQ("enum KeepEmptyLines {\n"
3383             "  ONE,\n"
3384             "\n"
3385             "  TWO,\n"
3386             "\n"
3387             "  THREE\n"
3388             "}",
3389             format("enum KeepEmptyLines {\n"
3390                    "  ONE,\n"
3391                    "\n"
3392                    "  TWO,\n"
3393                    "\n"
3394                    "\n"
3395                    "  THREE\n"
3396                    "}"));
3397   verifyFormat("enum E { // comment\n"
3398                "  ONE,\n"
3399                "  TWO\n"
3400                "};\n"
3401                "int i;");
3402 
3403   FormatStyle EightIndent = getLLVMStyle();
3404   EightIndent.IndentWidth = 8;
3405   verifyFormat("enum {\n"
3406                "        VOID,\n"
3407                "        CHAR,\n"
3408                "        SHORT,\n"
3409                "        INT,\n"
3410                "        LONG,\n"
3411                "        SIGNED,\n"
3412                "        UNSIGNED,\n"
3413                "        BOOL,\n"
3414                "        FLOAT,\n"
3415                "        DOUBLE,\n"
3416                "        COMPLEX\n"
3417                "};",
3418                EightIndent);
3419 
3420   // Not enums.
3421   verifyFormat("enum X f() {\n"
3422                "  a();\n"
3423                "  return 42;\n"
3424                "}");
3425   verifyFormat("enum X Type::f() {\n"
3426                "  a();\n"
3427                "  return 42;\n"
3428                "}");
3429   verifyFormat("enum ::X f() {\n"
3430                "  a();\n"
3431                "  return 42;\n"
3432                "}");
3433   verifyFormat("enum ns::X f() {\n"
3434                "  a();\n"
3435                "  return 42;\n"
3436                "}");
3437 }
3438 
3439 TEST_F(FormatTest, FormatsEnumsWithErrors) {
3440   verifyFormat("enum Type {\n"
3441                "  One = 0; // These semicolons should be commas.\n"
3442                "  Two = 1;\n"
3443                "};");
3444   verifyFormat("namespace n {\n"
3445                "enum Type {\n"
3446                "  One,\n"
3447                "  Two, // missing };\n"
3448                "  int i;\n"
3449                "}\n"
3450                "void g() {}");
3451 }
3452 
3453 TEST_F(FormatTest, FormatsEnumStruct) {
3454   verifyFormat("enum struct {\n"
3455                "  Zero,\n"
3456                "  One = 1,\n"
3457                "  Two = One + 1,\n"
3458                "  Three = (One + Two),\n"
3459                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
3460                "  Five = (One, Two, Three, Four, 5)\n"
3461                "};");
3462   verifyFormat("enum struct Enum {};");
3463   verifyFormat("enum struct {};");
3464   verifyFormat("enum struct X E {} d;");
3465   verifyFormat("enum struct __attribute__((...)) E {} d;");
3466   verifyFormat("enum struct __declspec__((...)) E {} d;");
3467   verifyFormat("enum struct X f() {\n  a();\n  return 42;\n}");
3468 }
3469 
3470 TEST_F(FormatTest, FormatsEnumClass) {
3471   verifyFormat("enum class {\n"
3472                "  Zero,\n"
3473                "  One = 1,\n"
3474                "  Two = One + 1,\n"
3475                "  Three = (One + Two),\n"
3476                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
3477                "  Five = (One, Two, Three, Four, 5)\n"
3478                "};");
3479   verifyFormat("enum class Enum {};");
3480   verifyFormat("enum class {};");
3481   verifyFormat("enum class X E {} d;");
3482   verifyFormat("enum class __attribute__((...)) E {} d;");
3483   verifyFormat("enum class __declspec__((...)) E {} d;");
3484   verifyFormat("enum class X f() {\n  a();\n  return 42;\n}");
3485 }
3486 
3487 TEST_F(FormatTest, FormatsEnumTypes) {
3488   verifyFormat("enum X : int {\n"
3489                "  A, // Force multiple lines.\n"
3490                "  B\n"
3491                "};");
3492   verifyFormat("enum X : int { A, B };");
3493   verifyFormat("enum X : std::uint32_t { A, B };");
3494 }
3495 
3496 TEST_F(FormatTest, FormatsTypedefEnum) {
3497   FormatStyle Style = getLLVMStyleWithColumns(40);
3498   verifyFormat("typedef enum {} EmptyEnum;");
3499   verifyFormat("typedef enum { A, B, C } ShortEnum;");
3500   verifyFormat("typedef enum {\n"
3501                "  ZERO = 0,\n"
3502                "  ONE = 1,\n"
3503                "  TWO = 2,\n"
3504                "  THREE = 3\n"
3505                "} LongEnum;",
3506                Style);
3507   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
3508   Style.BraceWrapping.AfterEnum = true;
3509   verifyFormat("typedef enum {} EmptyEnum;");
3510   verifyFormat("typedef enum { A, B, C } ShortEnum;");
3511   verifyFormat("typedef enum\n"
3512                "{\n"
3513                "  ZERO = 0,\n"
3514                "  ONE = 1,\n"
3515                "  TWO = 2,\n"
3516                "  THREE = 3\n"
3517                "} LongEnum;",
3518                Style);
3519 }
3520 
3521 TEST_F(FormatTest, FormatsNSEnums) {
3522   verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }");
3523   verifyGoogleFormat(
3524       "typedef NS_CLOSED_ENUM(NSInteger, SomeName) { AAA, BBB }");
3525   verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n"
3526                      "  // Information about someDecentlyLongValue.\n"
3527                      "  someDecentlyLongValue,\n"
3528                      "  // Information about anotherDecentlyLongValue.\n"
3529                      "  anotherDecentlyLongValue,\n"
3530                      "  // Information about aThirdDecentlyLongValue.\n"
3531                      "  aThirdDecentlyLongValue\n"
3532                      "};");
3533   verifyGoogleFormat("typedef NS_CLOSED_ENUM(NSInteger, MyType) {\n"
3534                      "  // Information about someDecentlyLongValue.\n"
3535                      "  someDecentlyLongValue,\n"
3536                      "  // Information about anotherDecentlyLongValue.\n"
3537                      "  anotherDecentlyLongValue,\n"
3538                      "  // Information about aThirdDecentlyLongValue.\n"
3539                      "  aThirdDecentlyLongValue\n"
3540                      "};");
3541   verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n"
3542                      "  a = 1,\n"
3543                      "  b = 2,\n"
3544                      "  c = 3,\n"
3545                      "};");
3546   verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n"
3547                      "  a = 1,\n"
3548                      "  b = 2,\n"
3549                      "  c = 3,\n"
3550                      "};");
3551   verifyGoogleFormat("typedef CF_CLOSED_ENUM(NSInteger, MyType) {\n"
3552                      "  a = 1,\n"
3553                      "  b = 2,\n"
3554                      "  c = 3,\n"
3555                      "};");
3556   verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n"
3557                      "  a = 1,\n"
3558                      "  b = 2,\n"
3559                      "  c = 3,\n"
3560                      "};");
3561 }
3562 
3563 TEST_F(FormatTest, FormatsBitfields) {
3564   verifyFormat("struct Bitfields {\n"
3565                "  unsigned sClass : 8;\n"
3566                "  unsigned ValueKind : 2;\n"
3567                "};");
3568   verifyFormat("struct A {\n"
3569                "  int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n"
3570                "      bbbbbbbbbbbbbbbbbbbbbbbbb;\n"
3571                "};");
3572   verifyFormat("struct MyStruct {\n"
3573                "  uchar data;\n"
3574                "  uchar : 8;\n"
3575                "  uchar : 8;\n"
3576                "  uchar other;\n"
3577                "};");
3578   FormatStyle Style = getLLVMStyle();
3579   Style.BitFieldColonSpacing = FormatStyle::BFCS_None;
3580   verifyFormat("struct Bitfields {\n"
3581                "  unsigned sClass:8;\n"
3582                "  unsigned ValueKind:2;\n"
3583                "  uchar other;\n"
3584                "};",
3585                Style);
3586   verifyFormat("struct A {\n"
3587                "  int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:1,\n"
3588                "      bbbbbbbbbbbbbbbbbbbbbbbbb:2;\n"
3589                "};",
3590                Style);
3591   Style.BitFieldColonSpacing = FormatStyle::BFCS_Before;
3592   verifyFormat("struct Bitfields {\n"
3593                "  unsigned sClass :8;\n"
3594                "  unsigned ValueKind :2;\n"
3595                "  uchar other;\n"
3596                "};",
3597                Style);
3598   Style.BitFieldColonSpacing = FormatStyle::BFCS_After;
3599   verifyFormat("struct Bitfields {\n"
3600                "  unsigned sClass: 8;\n"
3601                "  unsigned ValueKind: 2;\n"
3602                "  uchar other;\n"
3603                "};",
3604                Style);
3605 }
3606 
3607 TEST_F(FormatTest, FormatsNamespaces) {
3608   FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle();
3609   LLVMWithNoNamespaceFix.FixNamespaceComments = false;
3610 
3611   verifyFormat("namespace some_namespace {\n"
3612                "class A {};\n"
3613                "void f() { f(); }\n"
3614                "}",
3615                LLVMWithNoNamespaceFix);
3616   verifyFormat("namespace N::inline D {\n"
3617                "class A {};\n"
3618                "void f() { f(); }\n"
3619                "}",
3620                LLVMWithNoNamespaceFix);
3621   verifyFormat("namespace N::inline D::E {\n"
3622                "class A {};\n"
3623                "void f() { f(); }\n"
3624                "}",
3625                LLVMWithNoNamespaceFix);
3626   verifyFormat("namespace [[deprecated(\"foo[bar\")]] some_namespace {\n"
3627                "class A {};\n"
3628                "void f() { f(); }\n"
3629                "}",
3630                LLVMWithNoNamespaceFix);
3631   verifyFormat("/* something */ namespace some_namespace {\n"
3632                "class A {};\n"
3633                "void f() { f(); }\n"
3634                "}",
3635                LLVMWithNoNamespaceFix);
3636   verifyFormat("namespace {\n"
3637                "class A {};\n"
3638                "void f() { f(); }\n"
3639                "}",
3640                LLVMWithNoNamespaceFix);
3641   verifyFormat("/* something */ namespace {\n"
3642                "class A {};\n"
3643                "void f() { f(); }\n"
3644                "}",
3645                LLVMWithNoNamespaceFix);
3646   verifyFormat("inline namespace X {\n"
3647                "class A {};\n"
3648                "void f() { f(); }\n"
3649                "}",
3650                LLVMWithNoNamespaceFix);
3651   verifyFormat("/* something */ inline namespace X {\n"
3652                "class A {};\n"
3653                "void f() { f(); }\n"
3654                "}",
3655                LLVMWithNoNamespaceFix);
3656   verifyFormat("export namespace X {\n"
3657                "class A {};\n"
3658                "void f() { f(); }\n"
3659                "}",
3660                LLVMWithNoNamespaceFix);
3661   verifyFormat("using namespace some_namespace;\n"
3662                "class A {};\n"
3663                "void f() { f(); }",
3664                LLVMWithNoNamespaceFix);
3665 
3666   // This code is more common than we thought; if we
3667   // layout this correctly the semicolon will go into
3668   // its own line, which is undesirable.
3669   verifyFormat("namespace {};", LLVMWithNoNamespaceFix);
3670   verifyFormat("namespace {\n"
3671                "class A {};\n"
3672                "};",
3673                LLVMWithNoNamespaceFix);
3674 
3675   verifyFormat("namespace {\n"
3676                "int SomeVariable = 0; // comment\n"
3677                "} // namespace",
3678                LLVMWithNoNamespaceFix);
3679   EXPECT_EQ("#ifndef HEADER_GUARD\n"
3680             "#define HEADER_GUARD\n"
3681             "namespace my_namespace {\n"
3682             "int i;\n"
3683             "} // my_namespace\n"
3684             "#endif // HEADER_GUARD",
3685             format("#ifndef HEADER_GUARD\n"
3686                    " #define HEADER_GUARD\n"
3687                    "   namespace my_namespace {\n"
3688                    "int i;\n"
3689                    "}    // my_namespace\n"
3690                    "#endif    // HEADER_GUARD",
3691                    LLVMWithNoNamespaceFix));
3692 
3693   EXPECT_EQ("namespace A::B {\n"
3694             "class C {};\n"
3695             "}",
3696             format("namespace A::B {\n"
3697                    "class C {};\n"
3698                    "}",
3699                    LLVMWithNoNamespaceFix));
3700 
3701   FormatStyle Style = getLLVMStyle();
3702   Style.NamespaceIndentation = FormatStyle::NI_All;
3703   EXPECT_EQ("namespace out {\n"
3704             "  int i;\n"
3705             "  namespace in {\n"
3706             "    int i;\n"
3707             "  } // namespace in\n"
3708             "} // namespace out",
3709             format("namespace out {\n"
3710                    "int i;\n"
3711                    "namespace in {\n"
3712                    "int i;\n"
3713                    "} // namespace in\n"
3714                    "} // namespace out",
3715                    Style));
3716 
3717   FormatStyle ShortInlineFunctions = getLLVMStyle();
3718   ShortInlineFunctions.NamespaceIndentation = FormatStyle::NI_All;
3719   ShortInlineFunctions.AllowShortFunctionsOnASingleLine =
3720       FormatStyle::SFS_Inline;
3721   verifyFormat("namespace {\n"
3722                "  void f() {\n"
3723                "    return;\n"
3724                "  }\n"
3725                "} // namespace\n",
3726                ShortInlineFunctions);
3727   verifyFormat("namespace {\n"
3728                "  int some_int;\n"
3729                "  void f() {\n"
3730                "    return;\n"
3731                "  }\n"
3732                "} // namespace\n",
3733                ShortInlineFunctions);
3734   verifyFormat("namespace interface {\n"
3735                "  void f() {\n"
3736                "    return;\n"
3737                "  }\n"
3738                "} // namespace interface\n",
3739                ShortInlineFunctions);
3740   verifyFormat("namespace {\n"
3741                "  class X {\n"
3742                "    void f() { return; }\n"
3743                "  };\n"
3744                "} // namespace\n",
3745                ShortInlineFunctions);
3746   verifyFormat("namespace {\n"
3747                "  struct X {\n"
3748                "    void f() { return; }\n"
3749                "  };\n"
3750                "} // namespace\n",
3751                ShortInlineFunctions);
3752   verifyFormat("namespace {\n"
3753                "  union X {\n"
3754                "    void f() { return; }\n"
3755                "  };\n"
3756                "} // namespace\n",
3757                ShortInlineFunctions);
3758   verifyFormat("extern \"C\" {\n"
3759                "void f() {\n"
3760                "  return;\n"
3761                "}\n"
3762                "} // namespace\n",
3763                ShortInlineFunctions);
3764   verifyFormat("namespace {\n"
3765                "  class X {\n"
3766                "    void f() { return; }\n"
3767                "  } x;\n"
3768                "} // namespace\n",
3769                ShortInlineFunctions);
3770   verifyFormat("namespace {\n"
3771                "  [[nodiscard]] class X {\n"
3772                "    void f() { return; }\n"
3773                "  };\n"
3774                "} // namespace\n",
3775                ShortInlineFunctions);
3776   verifyFormat("namespace {\n"
3777                "  static class X {\n"
3778                "    void f() { return; }\n"
3779                "  } x;\n"
3780                "} // namespace\n",
3781                ShortInlineFunctions);
3782   verifyFormat("namespace {\n"
3783                "  constexpr class X {\n"
3784                "    void f() { return; }\n"
3785                "  } x;\n"
3786                "} // namespace\n",
3787                ShortInlineFunctions);
3788 
3789   ShortInlineFunctions.IndentExternBlock = FormatStyle::IEBS_Indent;
3790   verifyFormat("extern \"C\" {\n"
3791                "  void f() {\n"
3792                "    return;\n"
3793                "  }\n"
3794                "} // namespace\n",
3795                ShortInlineFunctions);
3796 
3797   Style.NamespaceIndentation = FormatStyle::NI_Inner;
3798   EXPECT_EQ("namespace out {\n"
3799             "int i;\n"
3800             "namespace in {\n"
3801             "  int i;\n"
3802             "} // namespace in\n"
3803             "} // namespace out",
3804             format("namespace out {\n"
3805                    "int i;\n"
3806                    "namespace in {\n"
3807                    "int i;\n"
3808                    "} // namespace in\n"
3809                    "} // namespace out",
3810                    Style));
3811 
3812   Style.NamespaceIndentation = FormatStyle::NI_None;
3813   verifyFormat("template <class T>\n"
3814                "concept a_concept = X<>;\n"
3815                "namespace B {\n"
3816                "struct b_struct {};\n"
3817                "} // namespace B\n",
3818                Style);
3819   verifyFormat("template <int I> constexpr void foo requires(I == 42) {}\n"
3820                "namespace ns {\n"
3821                "void foo() {}\n"
3822                "} // namespace ns\n",
3823                Style);
3824 }
3825 
3826 TEST_F(FormatTest, NamespaceMacros) {
3827   FormatStyle Style = getLLVMStyle();
3828   Style.NamespaceMacros.push_back("TESTSUITE");
3829 
3830   verifyFormat("TESTSUITE(A) {\n"
3831                "int foo();\n"
3832                "} // TESTSUITE(A)",
3833                Style);
3834 
3835   verifyFormat("TESTSUITE(A, B) {\n"
3836                "int foo();\n"
3837                "} // TESTSUITE(A)",
3838                Style);
3839 
3840   // Properly indent according to NamespaceIndentation style
3841   Style.NamespaceIndentation = FormatStyle::NI_All;
3842   verifyFormat("TESTSUITE(A) {\n"
3843                "  int foo();\n"
3844                "} // TESTSUITE(A)",
3845                Style);
3846   verifyFormat("TESTSUITE(A) {\n"
3847                "  namespace B {\n"
3848                "    int foo();\n"
3849                "  } // namespace B\n"
3850                "} // TESTSUITE(A)",
3851                Style);
3852   verifyFormat("namespace A {\n"
3853                "  TESTSUITE(B) {\n"
3854                "    int foo();\n"
3855                "  } // TESTSUITE(B)\n"
3856                "} // namespace A",
3857                Style);
3858 
3859   Style.NamespaceIndentation = FormatStyle::NI_Inner;
3860   verifyFormat("TESTSUITE(A) {\n"
3861                "TESTSUITE(B) {\n"
3862                "  int foo();\n"
3863                "} // TESTSUITE(B)\n"
3864                "} // TESTSUITE(A)",
3865                Style);
3866   verifyFormat("TESTSUITE(A) {\n"
3867                "namespace B {\n"
3868                "  int foo();\n"
3869                "} // namespace B\n"
3870                "} // TESTSUITE(A)",
3871                Style);
3872   verifyFormat("namespace A {\n"
3873                "TESTSUITE(B) {\n"
3874                "  int foo();\n"
3875                "} // TESTSUITE(B)\n"
3876                "} // namespace A",
3877                Style);
3878 
3879   // Properly merge namespace-macros blocks in CompactNamespaces mode
3880   Style.NamespaceIndentation = FormatStyle::NI_None;
3881   Style.CompactNamespaces = true;
3882   verifyFormat("TESTSUITE(A) { TESTSUITE(B) {\n"
3883                "}} // TESTSUITE(A::B)",
3884                Style);
3885 
3886   EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n"
3887             "}} // TESTSUITE(out::in)",
3888             format("TESTSUITE(out) {\n"
3889                    "TESTSUITE(in) {\n"
3890                    "} // TESTSUITE(in)\n"
3891                    "} // TESTSUITE(out)",
3892                    Style));
3893 
3894   EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n"
3895             "}} // TESTSUITE(out::in)",
3896             format("TESTSUITE(out) {\n"
3897                    "TESTSUITE(in) {\n"
3898                    "} // TESTSUITE(in)\n"
3899                    "} // TESTSUITE(out)",
3900                    Style));
3901 
3902   // Do not merge different namespaces/macros
3903   EXPECT_EQ("namespace out {\n"
3904             "TESTSUITE(in) {\n"
3905             "} // TESTSUITE(in)\n"
3906             "} // namespace out",
3907             format("namespace out {\n"
3908                    "TESTSUITE(in) {\n"
3909                    "} // TESTSUITE(in)\n"
3910                    "} // namespace out",
3911                    Style));
3912   EXPECT_EQ("TESTSUITE(out) {\n"
3913             "namespace in {\n"
3914             "} // namespace in\n"
3915             "} // TESTSUITE(out)",
3916             format("TESTSUITE(out) {\n"
3917                    "namespace in {\n"
3918                    "} // namespace in\n"
3919                    "} // TESTSUITE(out)",
3920                    Style));
3921   Style.NamespaceMacros.push_back("FOOBAR");
3922   EXPECT_EQ("TESTSUITE(out) {\n"
3923             "FOOBAR(in) {\n"
3924             "} // FOOBAR(in)\n"
3925             "} // TESTSUITE(out)",
3926             format("TESTSUITE(out) {\n"
3927                    "FOOBAR(in) {\n"
3928                    "} // FOOBAR(in)\n"
3929                    "} // TESTSUITE(out)",
3930                    Style));
3931 }
3932 
3933 TEST_F(FormatTest, FormatsCompactNamespaces) {
3934   FormatStyle Style = getLLVMStyle();
3935   Style.CompactNamespaces = true;
3936   Style.NamespaceMacros.push_back("TESTSUITE");
3937 
3938   verifyFormat("namespace A { namespace B {\n"
3939                "}} // namespace A::B",
3940                Style);
3941 
3942   EXPECT_EQ("namespace out { namespace in {\n"
3943             "}} // namespace out::in",
3944             format("namespace out {\n"
3945                    "namespace in {\n"
3946                    "} // namespace in\n"
3947                    "} // namespace out",
3948                    Style));
3949 
3950   // Only namespaces which have both consecutive opening and end get compacted
3951   EXPECT_EQ("namespace out {\n"
3952             "namespace in1 {\n"
3953             "} // namespace in1\n"
3954             "namespace in2 {\n"
3955             "} // namespace in2\n"
3956             "} // namespace out",
3957             format("namespace out {\n"
3958                    "namespace in1 {\n"
3959                    "} // namespace in1\n"
3960                    "namespace in2 {\n"
3961                    "} // namespace in2\n"
3962                    "} // namespace out",
3963                    Style));
3964 
3965   EXPECT_EQ("namespace out {\n"
3966             "int i;\n"
3967             "namespace in {\n"
3968             "int j;\n"
3969             "} // namespace in\n"
3970             "int k;\n"
3971             "} // namespace out",
3972             format("namespace out { int i;\n"
3973                    "namespace in { int j; } // namespace in\n"
3974                    "int k; } // namespace out",
3975                    Style));
3976 
3977   EXPECT_EQ("namespace A { namespace B { namespace C {\n"
3978             "}}} // namespace A::B::C\n",
3979             format("namespace A { namespace B {\n"
3980                    "namespace C {\n"
3981                    "}} // namespace B::C\n"
3982                    "} // namespace A\n",
3983                    Style));
3984 
3985   Style.ColumnLimit = 40;
3986   EXPECT_EQ("namespace aaaaaaaaaa {\n"
3987             "namespace bbbbbbbbbb {\n"
3988             "}} // namespace aaaaaaaaaa::bbbbbbbbbb",
3989             format("namespace aaaaaaaaaa {\n"
3990                    "namespace bbbbbbbbbb {\n"
3991                    "} // namespace bbbbbbbbbb\n"
3992                    "} // namespace aaaaaaaaaa",
3993                    Style));
3994 
3995   EXPECT_EQ("namespace aaaaaa { namespace bbbbbb {\n"
3996             "namespace cccccc {\n"
3997             "}}} // namespace aaaaaa::bbbbbb::cccccc",
3998             format("namespace aaaaaa {\n"
3999                    "namespace bbbbbb {\n"
4000                    "namespace cccccc {\n"
4001                    "} // namespace cccccc\n"
4002                    "} // namespace bbbbbb\n"
4003                    "} // namespace aaaaaa",
4004                    Style));
4005   Style.ColumnLimit = 80;
4006 
4007   // Extra semicolon after 'inner' closing brace prevents merging
4008   EXPECT_EQ("namespace out { namespace in {\n"
4009             "}; } // namespace out::in",
4010             format("namespace out {\n"
4011                    "namespace in {\n"
4012                    "}; // namespace in\n"
4013                    "} // namespace out",
4014                    Style));
4015 
4016   // Extra semicolon after 'outer' closing brace is conserved
4017   EXPECT_EQ("namespace out { namespace in {\n"
4018             "}}; // namespace out::in",
4019             format("namespace out {\n"
4020                    "namespace in {\n"
4021                    "} // namespace in\n"
4022                    "}; // namespace out",
4023                    Style));
4024 
4025   Style.NamespaceIndentation = FormatStyle::NI_All;
4026   EXPECT_EQ("namespace out { namespace in {\n"
4027             "  int i;\n"
4028             "}} // namespace out::in",
4029             format("namespace out {\n"
4030                    "namespace in {\n"
4031                    "int i;\n"
4032                    "} // namespace in\n"
4033                    "} // namespace out",
4034                    Style));
4035   EXPECT_EQ("namespace out { namespace mid {\n"
4036             "  namespace in {\n"
4037             "    int j;\n"
4038             "  } // namespace in\n"
4039             "  int k;\n"
4040             "}} // namespace out::mid",
4041             format("namespace out { namespace mid {\n"
4042                    "namespace in { int j; } // namespace in\n"
4043                    "int k; }} // namespace out::mid",
4044                    Style));
4045 
4046   Style.NamespaceIndentation = FormatStyle::NI_Inner;
4047   EXPECT_EQ("namespace out { namespace in {\n"
4048             "  int i;\n"
4049             "}} // namespace out::in",
4050             format("namespace out {\n"
4051                    "namespace in {\n"
4052                    "int i;\n"
4053                    "} // namespace in\n"
4054                    "} // namespace out",
4055                    Style));
4056   EXPECT_EQ("namespace out { namespace mid { namespace in {\n"
4057             "  int i;\n"
4058             "}}} // namespace out::mid::in",
4059             format("namespace out {\n"
4060                    "namespace mid {\n"
4061                    "namespace in {\n"
4062                    "int i;\n"
4063                    "} // namespace in\n"
4064                    "} // namespace mid\n"
4065                    "} // namespace out",
4066                    Style));
4067 
4068   Style.CompactNamespaces = true;
4069   Style.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
4070   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4071   Style.BraceWrapping.BeforeLambdaBody = true;
4072   verifyFormat("namespace out { namespace in {\n"
4073                "}} // namespace out::in",
4074                Style);
4075   EXPECT_EQ("namespace out { namespace in {\n"
4076             "}} // namespace out::in",
4077             format("namespace out {\n"
4078                    "namespace in {\n"
4079                    "} // namespace in\n"
4080                    "} // namespace out",
4081                    Style));
4082 }
4083 
4084 TEST_F(FormatTest, FormatsExternC) {
4085   verifyFormat("extern \"C\" {\nint a;");
4086   verifyFormat("extern \"C\" {}");
4087   verifyFormat("extern \"C\" {\n"
4088                "int foo();\n"
4089                "}");
4090   verifyFormat("extern \"C\" int foo() {}");
4091   verifyFormat("extern \"C\" int foo();");
4092   verifyFormat("extern \"C\" int foo() {\n"
4093                "  int i = 42;\n"
4094                "  return i;\n"
4095                "}");
4096 
4097   FormatStyle Style = getLLVMStyle();
4098   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4099   Style.BraceWrapping.AfterFunction = true;
4100   verifyFormat("extern \"C\" int foo() {}", Style);
4101   verifyFormat("extern \"C\" int foo();", Style);
4102   verifyFormat("extern \"C\" int foo()\n"
4103                "{\n"
4104                "  int i = 42;\n"
4105                "  return i;\n"
4106                "}",
4107                Style);
4108 
4109   Style.BraceWrapping.AfterExternBlock = true;
4110   Style.BraceWrapping.SplitEmptyRecord = false;
4111   verifyFormat("extern \"C\"\n"
4112                "{}",
4113                Style);
4114   verifyFormat("extern \"C\"\n"
4115                "{\n"
4116                "  int foo();\n"
4117                "}",
4118                Style);
4119 }
4120 
4121 TEST_F(FormatTest, IndentExternBlockStyle) {
4122   FormatStyle Style = getLLVMStyle();
4123   Style.IndentWidth = 2;
4124 
4125   Style.IndentExternBlock = FormatStyle::IEBS_Indent;
4126   verifyFormat("extern \"C\" { /*9*/\n"
4127                "}",
4128                Style);
4129   verifyFormat("extern \"C\" {\n"
4130                "  int foo10();\n"
4131                "}",
4132                Style);
4133 
4134   Style.IndentExternBlock = FormatStyle::IEBS_NoIndent;
4135   verifyFormat("extern \"C\" { /*11*/\n"
4136                "}",
4137                Style);
4138   verifyFormat("extern \"C\" {\n"
4139                "int foo12();\n"
4140                "}",
4141                Style);
4142 
4143   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4144   Style.BraceWrapping.AfterExternBlock = true;
4145   Style.IndentExternBlock = FormatStyle::IEBS_Indent;
4146   verifyFormat("extern \"C\"\n"
4147                "{ /*13*/\n"
4148                "}",
4149                Style);
4150   verifyFormat("extern \"C\"\n{\n"
4151                "  int foo14();\n"
4152                "}",
4153                Style);
4154 
4155   Style.BraceWrapping.AfterExternBlock = false;
4156   Style.IndentExternBlock = FormatStyle::IEBS_NoIndent;
4157   verifyFormat("extern \"C\" { /*15*/\n"
4158                "}",
4159                Style);
4160   verifyFormat("extern \"C\" {\n"
4161                "int foo16();\n"
4162                "}",
4163                Style);
4164 
4165   Style.BraceWrapping.AfterExternBlock = true;
4166   verifyFormat("extern \"C\"\n"
4167                "{ /*13*/\n"
4168                "}",
4169                Style);
4170   verifyFormat("extern \"C\"\n"
4171                "{\n"
4172                "int foo14();\n"
4173                "}",
4174                Style);
4175 
4176   Style.IndentExternBlock = FormatStyle::IEBS_Indent;
4177   verifyFormat("extern \"C\"\n"
4178                "{ /*13*/\n"
4179                "}",
4180                Style);
4181   verifyFormat("extern \"C\"\n"
4182                "{\n"
4183                "  int foo14();\n"
4184                "}",
4185                Style);
4186 }
4187 
4188 TEST_F(FormatTest, FormatsInlineASM) {
4189   verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
4190   verifyFormat("asm(\"nop\" ::: \"memory\");");
4191   verifyFormat(
4192       "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
4193       "    \"cpuid\\n\\t\"\n"
4194       "    \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
4195       "    : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
4196       "    : \"a\"(value));");
4197   EXPECT_EQ(
4198       "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
4199       "  __asm {\n"
4200       "        mov     edx,[that] // vtable in edx\n"
4201       "        mov     eax,methodIndex\n"
4202       "        call    [edx][eax*4] // stdcall\n"
4203       "  }\n"
4204       "}",
4205       format("void NS_InvokeByIndex(void *that,   unsigned int methodIndex) {\n"
4206              "    __asm {\n"
4207              "        mov     edx,[that] // vtable in edx\n"
4208              "        mov     eax,methodIndex\n"
4209              "        call    [edx][eax*4] // stdcall\n"
4210              "    }\n"
4211              "}"));
4212   EXPECT_EQ("_asm {\n"
4213             "  xor eax, eax;\n"
4214             "  cpuid;\n"
4215             "}",
4216             format("_asm {\n"
4217                    "  xor eax, eax;\n"
4218                    "  cpuid;\n"
4219                    "}"));
4220   verifyFormat("void function() {\n"
4221                "  // comment\n"
4222                "  asm(\"\");\n"
4223                "}");
4224   EXPECT_EQ("__asm {\n"
4225             "}\n"
4226             "int i;",
4227             format("__asm   {\n"
4228                    "}\n"
4229                    "int   i;"));
4230 }
4231 
4232 TEST_F(FormatTest, FormatTryCatch) {
4233   verifyFormat("try {\n"
4234                "  throw a * b;\n"
4235                "} catch (int a) {\n"
4236                "  // Do nothing.\n"
4237                "} catch (...) {\n"
4238                "  exit(42);\n"
4239                "}");
4240 
4241   // Function-level try statements.
4242   verifyFormat("int f() try { return 4; } catch (...) {\n"
4243                "  return 5;\n"
4244                "}");
4245   verifyFormat("class A {\n"
4246                "  int a;\n"
4247                "  A() try : a(0) {\n"
4248                "  } catch (...) {\n"
4249                "    throw;\n"
4250                "  }\n"
4251                "};\n");
4252   verifyFormat("class A {\n"
4253                "  int a;\n"
4254                "  A() try : a(0), b{1} {\n"
4255                "  } catch (...) {\n"
4256                "    throw;\n"
4257                "  }\n"
4258                "};\n");
4259   verifyFormat("class A {\n"
4260                "  int a;\n"
4261                "  A() try : a(0), b{1}, c{2} {\n"
4262                "  } catch (...) {\n"
4263                "    throw;\n"
4264                "  }\n"
4265                "};\n");
4266   verifyFormat("class A {\n"
4267                "  int a;\n"
4268                "  A() try : a(0), b{1}, c{2} {\n"
4269                "    { // New scope.\n"
4270                "    }\n"
4271                "  } catch (...) {\n"
4272                "    throw;\n"
4273                "  }\n"
4274                "};\n");
4275 
4276   // Incomplete try-catch blocks.
4277   verifyIncompleteFormat("try {} catch (");
4278 }
4279 
4280 TEST_F(FormatTest, FormatTryAsAVariable) {
4281   verifyFormat("int try;");
4282   verifyFormat("int try, size;");
4283   verifyFormat("try = foo();");
4284   verifyFormat("if (try < size) {\n  return true;\n}");
4285 
4286   verifyFormat("int catch;");
4287   verifyFormat("int catch, size;");
4288   verifyFormat("catch = foo();");
4289   verifyFormat("if (catch < size) {\n  return true;\n}");
4290 
4291   FormatStyle Style = getLLVMStyle();
4292   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4293   Style.BraceWrapping.AfterFunction = true;
4294   Style.BraceWrapping.BeforeCatch = true;
4295   verifyFormat("try {\n"
4296                "  int bar = 1;\n"
4297                "}\n"
4298                "catch (...) {\n"
4299                "  int bar = 1;\n"
4300                "}",
4301                Style);
4302   verifyFormat("#if NO_EX\n"
4303                "try\n"
4304                "#endif\n"
4305                "{\n"
4306                "}\n"
4307                "#if NO_EX\n"
4308                "catch (...) {\n"
4309                "}",
4310                Style);
4311   verifyFormat("try /* abc */ {\n"
4312                "  int bar = 1;\n"
4313                "}\n"
4314                "catch (...) {\n"
4315                "  int bar = 1;\n"
4316                "}",
4317                Style);
4318   verifyFormat("try\n"
4319                "// abc\n"
4320                "{\n"
4321                "  int bar = 1;\n"
4322                "}\n"
4323                "catch (...) {\n"
4324                "  int bar = 1;\n"
4325                "}",
4326                Style);
4327 }
4328 
4329 TEST_F(FormatTest, FormatSEHTryCatch) {
4330   verifyFormat("__try {\n"
4331                "  int a = b * c;\n"
4332                "} __except (EXCEPTION_EXECUTE_HANDLER) {\n"
4333                "  // Do nothing.\n"
4334                "}");
4335 
4336   verifyFormat("__try {\n"
4337                "  int a = b * c;\n"
4338                "} __finally {\n"
4339                "  // Do nothing.\n"
4340                "}");
4341 
4342   verifyFormat("DEBUG({\n"
4343                "  __try {\n"
4344                "  } __finally {\n"
4345                "  }\n"
4346                "});\n");
4347 }
4348 
4349 TEST_F(FormatTest, IncompleteTryCatchBlocks) {
4350   verifyFormat("try {\n"
4351                "  f();\n"
4352                "} catch {\n"
4353                "  g();\n"
4354                "}");
4355   verifyFormat("try {\n"
4356                "  f();\n"
4357                "} catch (A a) MACRO(x) {\n"
4358                "  g();\n"
4359                "} catch (B b) MACRO(x) {\n"
4360                "  g();\n"
4361                "}");
4362 }
4363 
4364 TEST_F(FormatTest, FormatTryCatchBraceStyles) {
4365   FormatStyle Style = getLLVMStyle();
4366   for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla,
4367                           FormatStyle::BS_WebKit}) {
4368     Style.BreakBeforeBraces = BraceStyle;
4369     verifyFormat("try {\n"
4370                  "  // something\n"
4371                  "} catch (...) {\n"
4372                  "  // something\n"
4373                  "}",
4374                  Style);
4375   }
4376   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
4377   verifyFormat("try {\n"
4378                "  // something\n"
4379                "}\n"
4380                "catch (...) {\n"
4381                "  // something\n"
4382                "}",
4383                Style);
4384   verifyFormat("__try {\n"
4385                "  // something\n"
4386                "}\n"
4387                "__finally {\n"
4388                "  // something\n"
4389                "}",
4390                Style);
4391   verifyFormat("@try {\n"
4392                "  // something\n"
4393                "}\n"
4394                "@finally {\n"
4395                "  // something\n"
4396                "}",
4397                Style);
4398   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
4399   verifyFormat("try\n"
4400                "{\n"
4401                "  // something\n"
4402                "}\n"
4403                "catch (...)\n"
4404                "{\n"
4405                "  // something\n"
4406                "}",
4407                Style);
4408   Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
4409   verifyFormat("try\n"
4410                "  {\n"
4411                "  // something white\n"
4412                "  }\n"
4413                "catch (...)\n"
4414                "  {\n"
4415                "  // something white\n"
4416                "  }",
4417                Style);
4418   Style.BreakBeforeBraces = FormatStyle::BS_GNU;
4419   verifyFormat("try\n"
4420                "  {\n"
4421                "    // something\n"
4422                "  }\n"
4423                "catch (...)\n"
4424                "  {\n"
4425                "    // something\n"
4426                "  }",
4427                Style);
4428   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4429   Style.BraceWrapping.BeforeCatch = true;
4430   verifyFormat("try {\n"
4431                "  // something\n"
4432                "}\n"
4433                "catch (...) {\n"
4434                "  // something\n"
4435                "}",
4436                Style);
4437 }
4438 
4439 TEST_F(FormatTest, StaticInitializers) {
4440   verifyFormat("static SomeClass SC = {1, 'a'};");
4441 
4442   verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n"
4443                "    100000000, "
4444                "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};");
4445 
4446   // Here, everything other than the "}" would fit on a line.
4447   verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
4448                "    10000000000000000000000000};");
4449   EXPECT_EQ("S s = {a,\n"
4450             "\n"
4451             "       b};",
4452             format("S s = {\n"
4453                    "  a,\n"
4454                    "\n"
4455                    "  b\n"
4456                    "};"));
4457 
4458   // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
4459   // line. However, the formatting looks a bit off and this probably doesn't
4460   // happen often in practice.
4461   verifyFormat("static int Variable[1] = {\n"
4462                "    {1000000000000000000000000000000000000}};",
4463                getLLVMStyleWithColumns(40));
4464 }
4465 
4466 TEST_F(FormatTest, DesignatedInitializers) {
4467   verifyFormat("const struct A a = {.a = 1, .b = 2};");
4468   verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n"
4469                "                    .bbbbbbbbbb = 2,\n"
4470                "                    .cccccccccc = 3,\n"
4471                "                    .dddddddddd = 4,\n"
4472                "                    .eeeeeeeeee = 5};");
4473   verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
4474                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
4475                "    .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
4476                "    .ccccccccccccccccccccccccccc = 3,\n"
4477                "    .ddddddddddddddddddddddddddd = 4,\n"
4478                "    .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};");
4479 
4480   verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
4481 
4482   verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
4483   verifyFormat("const struct A a = {[1] = aaaaaaaaaa,\n"
4484                "                    [2] = bbbbbbbbbb,\n"
4485                "                    [3] = cccccccccc,\n"
4486                "                    [4] = dddddddddd,\n"
4487                "                    [5] = eeeeeeeeee};");
4488   verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
4489                "    [1] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4490                "    [2] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
4491                "    [3] = cccccccccccccccccccccccccccccccccccccc,\n"
4492                "    [4] = dddddddddddddddddddddddddddddddddddddd,\n"
4493                "    [5] = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee};");
4494 }
4495 
4496 TEST_F(FormatTest, NestedStaticInitializers) {
4497   verifyFormat("static A x = {{{}}};\n");
4498   verifyFormat("static A x = {{{init1, init2, init3, init4},\n"
4499                "               {init1, init2, init3, init4}}};",
4500                getLLVMStyleWithColumns(50));
4501 
4502   verifyFormat("somes Status::global_reps[3] = {\n"
4503                "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
4504                "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
4505                "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};",
4506                getLLVMStyleWithColumns(60));
4507   verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
4508                      "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
4509                      "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
4510                      "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
4511   verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n"
4512                "                  {rect.fRight - rect.fLeft, rect.fBottom - "
4513                "rect.fTop}};");
4514 
4515   verifyFormat(
4516       "SomeArrayOfSomeType a = {\n"
4517       "    {{1, 2, 3},\n"
4518       "     {1, 2, 3},\n"
4519       "     {111111111111111111111111111111, 222222222222222222222222222222,\n"
4520       "      333333333333333333333333333333},\n"
4521       "     {1, 2, 3},\n"
4522       "     {1, 2, 3}}};");
4523   verifyFormat(
4524       "SomeArrayOfSomeType a = {\n"
4525       "    {{1, 2, 3}},\n"
4526       "    {{1, 2, 3}},\n"
4527       "    {{111111111111111111111111111111, 222222222222222222222222222222,\n"
4528       "      333333333333333333333333333333}},\n"
4529       "    {{1, 2, 3}},\n"
4530       "    {{1, 2, 3}}};");
4531 
4532   verifyFormat("struct {\n"
4533                "  unsigned bit;\n"
4534                "  const char *const name;\n"
4535                "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n"
4536                "                 {kOsWin, \"Windows\"},\n"
4537                "                 {kOsLinux, \"Linux\"},\n"
4538                "                 {kOsCrOS, \"Chrome OS\"}};");
4539   verifyFormat("struct {\n"
4540                "  unsigned bit;\n"
4541                "  const char *const name;\n"
4542                "} kBitsToOs[] = {\n"
4543                "    {kOsMac, \"Mac\"},\n"
4544                "    {kOsWin, \"Windows\"},\n"
4545                "    {kOsLinux, \"Linux\"},\n"
4546                "    {kOsCrOS, \"Chrome OS\"},\n"
4547                "};");
4548 }
4549 
4550 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
4551   verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
4552                "                      \\\n"
4553                "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
4554 }
4555 
4556 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
4557   verifyFormat("virtual void write(ELFWriter *writerrr,\n"
4558                "                   OwningPtr<FileOutputBuffer> &buffer) = 0;");
4559 
4560   // Do break defaulted and deleted functions.
4561   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
4562                "    default;",
4563                getLLVMStyleWithColumns(40));
4564   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
4565                "    delete;",
4566                getLLVMStyleWithColumns(40));
4567 }
4568 
4569 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) {
4570   verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3",
4571                getLLVMStyleWithColumns(40));
4572   verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
4573                getLLVMStyleWithColumns(40));
4574   EXPECT_EQ("#define Q                              \\\n"
4575             "  \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\"    \\\n"
4576             "  \"aaaaaaaa.cpp\"",
4577             format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
4578                    getLLVMStyleWithColumns(40)));
4579 }
4580 
4581 TEST_F(FormatTest, UnderstandsLinePPDirective) {
4582   EXPECT_EQ("# 123 \"A string literal\"",
4583             format("   #     123    \"A string literal\""));
4584 }
4585 
4586 TEST_F(FormatTest, LayoutUnknownPPDirective) {
4587   EXPECT_EQ("#;", format("#;"));
4588   verifyFormat("#\n;\n;\n;");
4589 }
4590 
4591 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
4592   EXPECT_EQ("#line 42 \"test\"\n",
4593             format("#  \\\n  line  \\\n  42  \\\n  \"test\"\n"));
4594   EXPECT_EQ("#define A B\n", format("#  \\\n define  \\\n    A  \\\n       B\n",
4595                                     getLLVMStyleWithColumns(12)));
4596 }
4597 
4598 TEST_F(FormatTest, EndOfFileEndsPPDirective) {
4599   EXPECT_EQ("#line 42 \"test\"",
4600             format("#  \\\n  line  \\\n  42  \\\n  \"test\""));
4601   EXPECT_EQ("#define A B", format("#  \\\n define  \\\n    A  \\\n       B"));
4602 }
4603 
4604 TEST_F(FormatTest, DoesntRemoveUnknownTokens) {
4605   verifyFormat("#define A \\x20");
4606   verifyFormat("#define A \\ x20");
4607   EXPECT_EQ("#define A \\ x20", format("#define A \\   x20"));
4608   verifyFormat("#define A ''");
4609   verifyFormat("#define A ''qqq");
4610   verifyFormat("#define A `qqq");
4611   verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");");
4612   EXPECT_EQ("const char *c = STRINGIFY(\n"
4613             "\\na : b);",
4614             format("const char * c = STRINGIFY(\n"
4615                    "\\na : b);"));
4616 
4617   verifyFormat("a\r\\");
4618   verifyFormat("a\v\\");
4619   verifyFormat("a\f\\");
4620 }
4621 
4622 TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) {
4623   FormatStyle style = getChromiumStyle(FormatStyle::LK_Cpp);
4624   style.IndentWidth = 4;
4625   style.PPIndentWidth = 1;
4626 
4627   style.IndentPPDirectives = FormatStyle::PPDIS_None;
4628   verifyFormat("#ifdef __linux__\n"
4629                "void foo() {\n"
4630                "    int x = 0;\n"
4631                "}\n"
4632                "#define FOO\n"
4633                "#endif\n"
4634                "void bar() {\n"
4635                "    int y = 0;\n"
4636                "}\n",
4637                style);
4638 
4639   style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
4640   verifyFormat("#ifdef __linux__\n"
4641                "void foo() {\n"
4642                "    int x = 0;\n"
4643                "}\n"
4644                "# define FOO foo\n"
4645                "#endif\n"
4646                "void bar() {\n"
4647                "    int y = 0;\n"
4648                "}\n",
4649                style);
4650 
4651   style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash;
4652   verifyFormat("#ifdef __linux__\n"
4653                "void foo() {\n"
4654                "    int x = 0;\n"
4655                "}\n"
4656                " #define FOO foo\n"
4657                "#endif\n"
4658                "void bar() {\n"
4659                "    int y = 0;\n"
4660                "}\n",
4661                style);
4662 }
4663 
4664 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
4665   verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
4666   verifyFormat("#define A( \\\n    BB)", getLLVMStyleWithColumns(12));
4667   verifyFormat("#define A( \\\n    A, B)", getLLVMStyleWithColumns(12));
4668   // FIXME: We never break before the macro name.
4669   verifyFormat("#define AA( \\\n    B)", getLLVMStyleWithColumns(12));
4670 
4671   verifyFormat("#define A A\n#define A A");
4672   verifyFormat("#define A(X) A\n#define A A");
4673 
4674   verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
4675   verifyFormat("#define Something    \\\n  Other", getLLVMStyleWithColumns(22));
4676 }
4677 
4678 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
4679   EXPECT_EQ("// somecomment\n"
4680             "#include \"a.h\"\n"
4681             "#define A(  \\\n"
4682             "    A, B)\n"
4683             "#include \"b.h\"\n"
4684             "// somecomment\n",
4685             format("  // somecomment\n"
4686                    "  #include \"a.h\"\n"
4687                    "#define A(A,\\\n"
4688                    "    B)\n"
4689                    "    #include \"b.h\"\n"
4690                    " // somecomment\n",
4691                    getLLVMStyleWithColumns(13)));
4692 }
4693 
4694 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); }
4695 
4696 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
4697   EXPECT_EQ("#define A    \\\n"
4698             "  c;         \\\n"
4699             "  e;\n"
4700             "f;",
4701             format("#define A c; e;\n"
4702                    "f;",
4703                    getLLVMStyleWithColumns(14)));
4704 }
4705 
4706 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
4707 
4708 TEST_F(FormatTest, MacroDefinitionInsideStatement) {
4709   EXPECT_EQ("int x,\n"
4710             "#define A\n"
4711             "    y;",
4712             format("int x,\n#define A\ny;"));
4713 }
4714 
4715 TEST_F(FormatTest, HashInMacroDefinition) {
4716   EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle()));
4717   EXPECT_EQ("#define A(c) u#c", format("#define A(c) u#c", getLLVMStyle()));
4718   EXPECT_EQ("#define A(c) U#c", format("#define A(c) U#c", getLLVMStyle()));
4719   EXPECT_EQ("#define A(c) u8#c", format("#define A(c) u8#c", getLLVMStyle()));
4720   EXPECT_EQ("#define A(c) LR#c", format("#define A(c) LR#c", getLLVMStyle()));
4721   EXPECT_EQ("#define A(c) uR#c", format("#define A(c) uR#c", getLLVMStyle()));
4722   EXPECT_EQ("#define A(c) UR#c", format("#define A(c) UR#c", getLLVMStyle()));
4723   EXPECT_EQ("#define A(c) u8R#c", format("#define A(c) u8R#c", getLLVMStyle()));
4724   verifyFormat("#define A \\\n  b #c;", getLLVMStyleWithColumns(11));
4725   verifyFormat("#define A  \\\n"
4726                "  {        \\\n"
4727                "    f(#c); \\\n"
4728                "  }",
4729                getLLVMStyleWithColumns(11));
4730 
4731   verifyFormat("#define A(X)         \\\n"
4732                "  void function##X()",
4733                getLLVMStyleWithColumns(22));
4734 
4735   verifyFormat("#define A(a, b, c)   \\\n"
4736                "  void a##b##c()",
4737                getLLVMStyleWithColumns(22));
4738 
4739   verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
4740 }
4741 
4742 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
4743   EXPECT_EQ("#define A (x)", format("#define A (x)"));
4744   EXPECT_EQ("#define A(x)", format("#define A(x)"));
4745 
4746   FormatStyle Style = getLLVMStyle();
4747   Style.SpaceBeforeParens = FormatStyle::SBPO_Never;
4748   verifyFormat("#define true ((foo)1)", Style);
4749   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
4750   verifyFormat("#define false((foo)0)", Style);
4751 }
4752 
4753 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
4754   EXPECT_EQ("#define A b;", format("#define A \\\n"
4755                                    "          \\\n"
4756                                    "  b;",
4757                                    getLLVMStyleWithColumns(25)));
4758   EXPECT_EQ("#define A \\\n"
4759             "          \\\n"
4760             "  a;      \\\n"
4761             "  b;",
4762             format("#define A \\\n"
4763                    "          \\\n"
4764                    "  a;      \\\n"
4765                    "  b;",
4766                    getLLVMStyleWithColumns(11)));
4767   EXPECT_EQ("#define A \\\n"
4768             "  a;      \\\n"
4769             "          \\\n"
4770             "  b;",
4771             format("#define A \\\n"
4772                    "  a;      \\\n"
4773                    "          \\\n"
4774                    "  b;",
4775                    getLLVMStyleWithColumns(11)));
4776 }
4777 
4778 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
4779   verifyIncompleteFormat("#define A :");
4780   verifyFormat("#define SOMECASES  \\\n"
4781                "  case 1:          \\\n"
4782                "  case 2\n",
4783                getLLVMStyleWithColumns(20));
4784   verifyFormat("#define MACRO(a) \\\n"
4785                "  if (a)         \\\n"
4786                "    f();         \\\n"
4787                "  else           \\\n"
4788                "    g()",
4789                getLLVMStyleWithColumns(18));
4790   verifyFormat("#define A template <typename T>");
4791   verifyIncompleteFormat("#define STR(x) #x\n"
4792                          "f(STR(this_is_a_string_literal{));");
4793   verifyFormat("#pragma omp threadprivate( \\\n"
4794                "    y)), // expected-warning",
4795                getLLVMStyleWithColumns(28));
4796   verifyFormat("#d, = };");
4797   verifyFormat("#if \"a");
4798   verifyIncompleteFormat("({\n"
4799                          "#define b     \\\n"
4800                          "  }           \\\n"
4801                          "  a\n"
4802                          "a",
4803                          getLLVMStyleWithColumns(15));
4804   verifyFormat("#define A     \\\n"
4805                "  {           \\\n"
4806                "    {\n"
4807                "#define B     \\\n"
4808                "  }           \\\n"
4809                "  }",
4810                getLLVMStyleWithColumns(15));
4811   verifyNoCrash("#if a\na(\n#else\n#endif\n{a");
4812   verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}");
4813   verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};");
4814   verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() {      \n)}");
4815 }
4816 
4817 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
4818   verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
4819   EXPECT_EQ("class A : public QObject {\n"
4820             "  Q_OBJECT\n"
4821             "\n"
4822             "  A() {}\n"
4823             "};",
4824             format("class A  :  public QObject {\n"
4825                    "     Q_OBJECT\n"
4826                    "\n"
4827                    "  A() {\n}\n"
4828                    "}  ;"));
4829   EXPECT_EQ("MACRO\n"
4830             "/*static*/ int i;",
4831             format("MACRO\n"
4832                    " /*static*/ int   i;"));
4833   EXPECT_EQ("SOME_MACRO\n"
4834             "namespace {\n"
4835             "void f();\n"
4836             "} // namespace",
4837             format("SOME_MACRO\n"
4838                    "  namespace    {\n"
4839                    "void   f(  );\n"
4840                    "} // namespace"));
4841   // Only if the identifier contains at least 5 characters.
4842   EXPECT_EQ("HTTP f();", format("HTTP\nf();"));
4843   EXPECT_EQ("MACRO\nf();", format("MACRO\nf();"));
4844   // Only if everything is upper case.
4845   EXPECT_EQ("class A : public QObject {\n"
4846             "  Q_Object A() {}\n"
4847             "};",
4848             format("class A  :  public QObject {\n"
4849                    "     Q_Object\n"
4850                    "  A() {\n}\n"
4851                    "}  ;"));
4852 
4853   // Only if the next line can actually start an unwrapped line.
4854   EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;",
4855             format("SOME_WEIRD_LOG_MACRO\n"
4856                    "<< SomeThing;"));
4857 
4858   verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
4859                "(n, buffers))\n",
4860                getChromiumStyle(FormatStyle::LK_Cpp));
4861 
4862   // See PR41483
4863   EXPECT_EQ("/**/ FOO(a)\n"
4864             "FOO(b)",
4865             format("/**/ FOO(a)\n"
4866                    "FOO(b)"));
4867 }
4868 
4869 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
4870   EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
4871             "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
4872             "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
4873             "class X {};\n"
4874             "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
4875             "int *createScopDetectionPass() { return 0; }",
4876             format("  INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
4877                    "  INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
4878                    "  INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
4879                    "  class X {};\n"
4880                    "  INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
4881                    "  int *createScopDetectionPass() { return 0; }"));
4882   // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
4883   // braces, so that inner block is indented one level more.
4884   EXPECT_EQ("int q() {\n"
4885             "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
4886             "  IPC_MESSAGE_HANDLER(xxx, qqq)\n"
4887             "  IPC_END_MESSAGE_MAP()\n"
4888             "}",
4889             format("int q() {\n"
4890                    "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
4891                    "    IPC_MESSAGE_HANDLER(xxx, qqq)\n"
4892                    "  IPC_END_MESSAGE_MAP()\n"
4893                    "}"));
4894 
4895   // Same inside macros.
4896   EXPECT_EQ("#define LIST(L) \\\n"
4897             "  L(A)          \\\n"
4898             "  L(B)          \\\n"
4899             "  L(C)",
4900             format("#define LIST(L) \\\n"
4901                    "  L(A) \\\n"
4902                    "  L(B) \\\n"
4903                    "  L(C)",
4904                    getGoogleStyle()));
4905 
4906   // These must not be recognized as macros.
4907   EXPECT_EQ("int q() {\n"
4908             "  f(x);\n"
4909             "  f(x) {}\n"
4910             "  f(x)->g();\n"
4911             "  f(x)->*g();\n"
4912             "  f(x).g();\n"
4913             "  f(x) = x;\n"
4914             "  f(x) += x;\n"
4915             "  f(x) -= x;\n"
4916             "  f(x) *= x;\n"
4917             "  f(x) /= x;\n"
4918             "  f(x) %= x;\n"
4919             "  f(x) &= x;\n"
4920             "  f(x) |= x;\n"
4921             "  f(x) ^= x;\n"
4922             "  f(x) >>= x;\n"
4923             "  f(x) <<= x;\n"
4924             "  f(x)[y].z();\n"
4925             "  LOG(INFO) << x;\n"
4926             "  ifstream(x) >> x;\n"
4927             "}\n",
4928             format("int q() {\n"
4929                    "  f(x)\n;\n"
4930                    "  f(x)\n {}\n"
4931                    "  f(x)\n->g();\n"
4932                    "  f(x)\n->*g();\n"
4933                    "  f(x)\n.g();\n"
4934                    "  f(x)\n = x;\n"
4935                    "  f(x)\n += x;\n"
4936                    "  f(x)\n -= x;\n"
4937                    "  f(x)\n *= x;\n"
4938                    "  f(x)\n /= x;\n"
4939                    "  f(x)\n %= x;\n"
4940                    "  f(x)\n &= x;\n"
4941                    "  f(x)\n |= x;\n"
4942                    "  f(x)\n ^= x;\n"
4943                    "  f(x)\n >>= x;\n"
4944                    "  f(x)\n <<= x;\n"
4945                    "  f(x)\n[y].z();\n"
4946                    "  LOG(INFO)\n << x;\n"
4947                    "  ifstream(x)\n >> x;\n"
4948                    "}\n"));
4949   EXPECT_EQ("int q() {\n"
4950             "  F(x)\n"
4951             "  if (1) {\n"
4952             "  }\n"
4953             "  F(x)\n"
4954             "  while (1) {\n"
4955             "  }\n"
4956             "  F(x)\n"
4957             "  G(x);\n"
4958             "  F(x)\n"
4959             "  try {\n"
4960             "    Q();\n"
4961             "  } catch (...) {\n"
4962             "  }\n"
4963             "}\n",
4964             format("int q() {\n"
4965                    "F(x)\n"
4966                    "if (1) {}\n"
4967                    "F(x)\n"
4968                    "while (1) {}\n"
4969                    "F(x)\n"
4970                    "G(x);\n"
4971                    "F(x)\n"
4972                    "try { Q(); } catch (...) {}\n"
4973                    "}\n"));
4974   EXPECT_EQ("class A {\n"
4975             "  A() : t(0) {}\n"
4976             "  A(int i) noexcept() : {}\n"
4977             "  A(X x)\n" // FIXME: function-level try blocks are broken.
4978             "  try : t(0) {\n"
4979             "  } catch (...) {\n"
4980             "  }\n"
4981             "};",
4982             format("class A {\n"
4983                    "  A()\n : t(0) {}\n"
4984                    "  A(int i)\n noexcept() : {}\n"
4985                    "  A(X x)\n"
4986                    "  try : t(0) {} catch (...) {}\n"
4987                    "};"));
4988   FormatStyle Style = getLLVMStyle();
4989   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4990   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
4991   Style.BraceWrapping.AfterFunction = true;
4992   EXPECT_EQ("void f()\n"
4993             "try\n"
4994             "{\n"
4995             "}",
4996             format("void f() try {\n"
4997                    "}",
4998                    Style));
4999   EXPECT_EQ("class SomeClass {\n"
5000             "public:\n"
5001             "  SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
5002             "};",
5003             format("class SomeClass {\n"
5004                    "public:\n"
5005                    "  SomeClass()\n"
5006                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
5007                    "};"));
5008   EXPECT_EQ("class SomeClass {\n"
5009             "public:\n"
5010             "  SomeClass()\n"
5011             "      EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
5012             "};",
5013             format("class SomeClass {\n"
5014                    "public:\n"
5015                    "  SomeClass()\n"
5016                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
5017                    "};",
5018                    getLLVMStyleWithColumns(40)));
5019 
5020   verifyFormat("MACRO(>)");
5021 
5022   // Some macros contain an implicit semicolon.
5023   Style = getLLVMStyle();
5024   Style.StatementMacros.push_back("FOO");
5025   verifyFormat("FOO(a) int b = 0;");
5026   verifyFormat("FOO(a)\n"
5027                "int b = 0;",
5028                Style);
5029   verifyFormat("FOO(a);\n"
5030                "int b = 0;",
5031                Style);
5032   verifyFormat("FOO(argc, argv, \"4.0.2\")\n"
5033                "int b = 0;",
5034                Style);
5035   verifyFormat("FOO()\n"
5036                "int b = 0;",
5037                Style);
5038   verifyFormat("FOO\n"
5039                "int b = 0;",
5040                Style);
5041   verifyFormat("void f() {\n"
5042                "  FOO(a)\n"
5043                "  return a;\n"
5044                "}",
5045                Style);
5046   verifyFormat("FOO(a)\n"
5047                "FOO(b)",
5048                Style);
5049   verifyFormat("int a = 0;\n"
5050                "FOO(b)\n"
5051                "int c = 0;",
5052                Style);
5053   verifyFormat("int a = 0;\n"
5054                "int x = FOO(a)\n"
5055                "int b = 0;",
5056                Style);
5057   verifyFormat("void foo(int a) { FOO(a) }\n"
5058                "uint32_t bar() {}",
5059                Style);
5060 }
5061 
5062 TEST_F(FormatTest, FormatsMacrosWithZeroColumnWidth) {
5063   FormatStyle ZeroColumn = getLLVMStyleWithColumns(0);
5064 
5065   verifyFormat("#define A LOOOOOOOOOOOOOOOOOOONG() LOOOOOOOOOOOOOOOOOOONG()",
5066                ZeroColumn);
5067 }
5068 
5069 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
5070   verifyFormat("#define A \\\n"
5071                "  f({     \\\n"
5072                "    g();  \\\n"
5073                "  });",
5074                getLLVMStyleWithColumns(11));
5075 }
5076 
5077 TEST_F(FormatTest, IndentPreprocessorDirectives) {
5078   FormatStyle Style = getLLVMStyleWithColumns(40);
5079   Style.IndentPPDirectives = FormatStyle::PPDIS_None;
5080   verifyFormat("#ifdef _WIN32\n"
5081                "#define A 0\n"
5082                "#ifdef VAR2\n"
5083                "#define B 1\n"
5084                "#include <someheader.h>\n"
5085                "#define MACRO                          \\\n"
5086                "  some_very_long_func_aaaaaaaaaa();\n"
5087                "#endif\n"
5088                "#else\n"
5089                "#define A 1\n"
5090                "#endif",
5091                Style);
5092   Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
5093   verifyFormat("#ifdef _WIN32\n"
5094                "#  define A 0\n"
5095                "#  ifdef VAR2\n"
5096                "#    define B 1\n"
5097                "#    include <someheader.h>\n"
5098                "#    define MACRO                      \\\n"
5099                "      some_very_long_func_aaaaaaaaaa();\n"
5100                "#  endif\n"
5101                "#else\n"
5102                "#  define A 1\n"
5103                "#endif",
5104                Style);
5105   verifyFormat("#if A\n"
5106                "#  define MACRO                        \\\n"
5107                "    void a(int x) {                    \\\n"
5108                "      b();                             \\\n"
5109                "      c();                             \\\n"
5110                "      d();                             \\\n"
5111                "      e();                             \\\n"
5112                "      f();                             \\\n"
5113                "    }\n"
5114                "#endif",
5115                Style);
5116   // Comments before include guard.
5117   verifyFormat("// file comment\n"
5118                "// file comment\n"
5119                "#ifndef HEADER_H\n"
5120                "#define HEADER_H\n"
5121                "code();\n"
5122                "#endif",
5123                Style);
5124   // Test with include guards.
5125   verifyFormat("#ifndef HEADER_H\n"
5126                "#define HEADER_H\n"
5127                "code();\n"
5128                "#endif",
5129                Style);
5130   // Include guards must have a #define with the same variable immediately
5131   // after #ifndef.
5132   verifyFormat("#ifndef NOT_GUARD\n"
5133                "#  define FOO\n"
5134                "code();\n"
5135                "#endif",
5136                Style);
5137 
5138   // Include guards must cover the entire file.
5139   verifyFormat("code();\n"
5140                "code();\n"
5141                "#ifndef NOT_GUARD\n"
5142                "#  define NOT_GUARD\n"
5143                "code();\n"
5144                "#endif",
5145                Style);
5146   verifyFormat("#ifndef NOT_GUARD\n"
5147                "#  define NOT_GUARD\n"
5148                "code();\n"
5149                "#endif\n"
5150                "code();",
5151                Style);
5152   // Test with trailing blank lines.
5153   verifyFormat("#ifndef HEADER_H\n"
5154                "#define HEADER_H\n"
5155                "code();\n"
5156                "#endif\n",
5157                Style);
5158   // Include guards don't have #else.
5159   verifyFormat("#ifndef NOT_GUARD\n"
5160                "#  define NOT_GUARD\n"
5161                "code();\n"
5162                "#else\n"
5163                "#endif",
5164                Style);
5165   verifyFormat("#ifndef NOT_GUARD\n"
5166                "#  define NOT_GUARD\n"
5167                "code();\n"
5168                "#elif FOO\n"
5169                "#endif",
5170                Style);
5171   // Non-identifier #define after potential include guard.
5172   verifyFormat("#ifndef FOO\n"
5173                "#  define 1\n"
5174                "#endif\n",
5175                Style);
5176   // #if closes past last non-preprocessor line.
5177   verifyFormat("#ifndef FOO\n"
5178                "#define FOO\n"
5179                "#if 1\n"
5180                "int i;\n"
5181                "#  define A 0\n"
5182                "#endif\n"
5183                "#endif\n",
5184                Style);
5185   // Don't crash if there is an #elif directive without a condition.
5186   verifyFormat("#if 1\n"
5187                "int x;\n"
5188                "#elif\n"
5189                "int y;\n"
5190                "#else\n"
5191                "int z;\n"
5192                "#endif",
5193                Style);
5194   // FIXME: This doesn't handle the case where there's code between the
5195   // #ifndef and #define but all other conditions hold. This is because when
5196   // the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the
5197   // previous code line yet, so we can't detect it.
5198   EXPECT_EQ("#ifndef NOT_GUARD\n"
5199             "code();\n"
5200             "#define NOT_GUARD\n"
5201             "code();\n"
5202             "#endif",
5203             format("#ifndef NOT_GUARD\n"
5204                    "code();\n"
5205                    "#  define NOT_GUARD\n"
5206                    "code();\n"
5207                    "#endif",
5208                    Style));
5209   // FIXME: This doesn't handle cases where legitimate preprocessor lines may
5210   // be outside an include guard. Examples are #pragma once and
5211   // #pragma GCC diagnostic, or anything else that does not change the meaning
5212   // of the file if it's included multiple times.
5213   EXPECT_EQ("#ifdef WIN32\n"
5214             "#  pragma once\n"
5215             "#endif\n"
5216             "#ifndef HEADER_H\n"
5217             "#  define HEADER_H\n"
5218             "code();\n"
5219             "#endif",
5220             format("#ifdef WIN32\n"
5221                    "#  pragma once\n"
5222                    "#endif\n"
5223                    "#ifndef HEADER_H\n"
5224                    "#define HEADER_H\n"
5225                    "code();\n"
5226                    "#endif",
5227                    Style));
5228   // FIXME: This does not detect when there is a single non-preprocessor line
5229   // in front of an include-guard-like structure where other conditions hold
5230   // because ScopedLineState hides the line.
5231   EXPECT_EQ("code();\n"
5232             "#ifndef HEADER_H\n"
5233             "#define HEADER_H\n"
5234             "code();\n"
5235             "#endif",
5236             format("code();\n"
5237                    "#ifndef HEADER_H\n"
5238                    "#  define HEADER_H\n"
5239                    "code();\n"
5240                    "#endif",
5241                    Style));
5242   // Keep comments aligned with #, otherwise indent comments normally. These
5243   // tests cannot use verifyFormat because messUp manipulates leading
5244   // whitespace.
5245   {
5246     const char *Expected = ""
5247                            "void f() {\n"
5248                            "#if 1\n"
5249                            "// Preprocessor aligned.\n"
5250                            "#  define A 0\n"
5251                            "  // Code. Separated by blank line.\n"
5252                            "\n"
5253                            "#  define B 0\n"
5254                            "  // Code. Not aligned with #\n"
5255                            "#  define C 0\n"
5256                            "#endif";
5257     const char *ToFormat = ""
5258                            "void f() {\n"
5259                            "#if 1\n"
5260                            "// Preprocessor aligned.\n"
5261                            "#  define A 0\n"
5262                            "// Code. Separated by blank line.\n"
5263                            "\n"
5264                            "#  define B 0\n"
5265                            "   // Code. Not aligned with #\n"
5266                            "#  define C 0\n"
5267                            "#endif";
5268     EXPECT_EQ(Expected, format(ToFormat, Style));
5269     EXPECT_EQ(Expected, format(Expected, Style));
5270   }
5271   // Keep block quotes aligned.
5272   {
5273     const char *Expected = ""
5274                            "void f() {\n"
5275                            "#if 1\n"
5276                            "/* Preprocessor aligned. */\n"
5277                            "#  define A 0\n"
5278                            "  /* Code. Separated by blank line. */\n"
5279                            "\n"
5280                            "#  define B 0\n"
5281                            "  /* Code. Not aligned with # */\n"
5282                            "#  define C 0\n"
5283                            "#endif";
5284     const char *ToFormat = ""
5285                            "void f() {\n"
5286                            "#if 1\n"
5287                            "/* Preprocessor aligned. */\n"
5288                            "#  define A 0\n"
5289                            "/* Code. Separated by blank line. */\n"
5290                            "\n"
5291                            "#  define B 0\n"
5292                            "   /* Code. Not aligned with # */\n"
5293                            "#  define C 0\n"
5294                            "#endif";
5295     EXPECT_EQ(Expected, format(ToFormat, Style));
5296     EXPECT_EQ(Expected, format(Expected, Style));
5297   }
5298   // Keep comments aligned with un-indented directives.
5299   {
5300     const char *Expected = ""
5301                            "void f() {\n"
5302                            "// Preprocessor aligned.\n"
5303                            "#define A 0\n"
5304                            "  // Code. Separated by blank line.\n"
5305                            "\n"
5306                            "#define B 0\n"
5307                            "  // Code. Not aligned with #\n"
5308                            "#define C 0\n";
5309     const char *ToFormat = ""
5310                            "void f() {\n"
5311                            "// Preprocessor aligned.\n"
5312                            "#define A 0\n"
5313                            "// Code. Separated by blank line.\n"
5314                            "\n"
5315                            "#define B 0\n"
5316                            "   // Code. Not aligned with #\n"
5317                            "#define C 0\n";
5318     EXPECT_EQ(Expected, format(ToFormat, Style));
5319     EXPECT_EQ(Expected, format(Expected, Style));
5320   }
5321   // Test AfterHash with tabs.
5322   {
5323     FormatStyle Tabbed = Style;
5324     Tabbed.UseTab = FormatStyle::UT_Always;
5325     Tabbed.IndentWidth = 8;
5326     Tabbed.TabWidth = 8;
5327     verifyFormat("#ifdef _WIN32\n"
5328                  "#\tdefine A 0\n"
5329                  "#\tifdef VAR2\n"
5330                  "#\t\tdefine B 1\n"
5331                  "#\t\tinclude <someheader.h>\n"
5332                  "#\t\tdefine MACRO          \\\n"
5333                  "\t\t\tsome_very_long_func_aaaaaaaaaa();\n"
5334                  "#\tendif\n"
5335                  "#else\n"
5336                  "#\tdefine A 1\n"
5337                  "#endif",
5338                  Tabbed);
5339   }
5340 
5341   // Regression test: Multiline-macro inside include guards.
5342   verifyFormat("#ifndef HEADER_H\n"
5343                "#define HEADER_H\n"
5344                "#define A()        \\\n"
5345                "  int i;           \\\n"
5346                "  int j;\n"
5347                "#endif // HEADER_H",
5348                getLLVMStyleWithColumns(20));
5349 
5350   Style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash;
5351   // Basic before hash indent tests
5352   verifyFormat("#ifdef _WIN32\n"
5353                "  #define A 0\n"
5354                "  #ifdef VAR2\n"
5355                "    #define B 1\n"
5356                "    #include <someheader.h>\n"
5357                "    #define MACRO                      \\\n"
5358                "      some_very_long_func_aaaaaaaaaa();\n"
5359                "  #endif\n"
5360                "#else\n"
5361                "  #define A 1\n"
5362                "#endif",
5363                Style);
5364   verifyFormat("#if A\n"
5365                "  #define MACRO                        \\\n"
5366                "    void a(int x) {                    \\\n"
5367                "      b();                             \\\n"
5368                "      c();                             \\\n"
5369                "      d();                             \\\n"
5370                "      e();                             \\\n"
5371                "      f();                             \\\n"
5372                "    }\n"
5373                "#endif",
5374                Style);
5375   // Keep comments aligned with indented directives. These
5376   // tests cannot use verifyFormat because messUp manipulates leading
5377   // whitespace.
5378   {
5379     const char *Expected = "void f() {\n"
5380                            "// Aligned to preprocessor.\n"
5381                            "#if 1\n"
5382                            "  // Aligned to code.\n"
5383                            "  int a;\n"
5384                            "  #if 1\n"
5385                            "    // Aligned to preprocessor.\n"
5386                            "    #define A 0\n"
5387                            "  // Aligned to code.\n"
5388                            "  int b;\n"
5389                            "  #endif\n"
5390                            "#endif\n"
5391                            "}";
5392     const char *ToFormat = "void f() {\n"
5393                            "// Aligned to preprocessor.\n"
5394                            "#if 1\n"
5395                            "// Aligned to code.\n"
5396                            "int a;\n"
5397                            "#if 1\n"
5398                            "// Aligned to preprocessor.\n"
5399                            "#define A 0\n"
5400                            "// Aligned to code.\n"
5401                            "int b;\n"
5402                            "#endif\n"
5403                            "#endif\n"
5404                            "}";
5405     EXPECT_EQ(Expected, format(ToFormat, Style));
5406     EXPECT_EQ(Expected, format(Expected, Style));
5407   }
5408   {
5409     const char *Expected = "void f() {\n"
5410                            "/* Aligned to preprocessor. */\n"
5411                            "#if 1\n"
5412                            "  /* Aligned to code. */\n"
5413                            "  int a;\n"
5414                            "  #if 1\n"
5415                            "    /* Aligned to preprocessor. */\n"
5416                            "    #define A 0\n"
5417                            "  /* Aligned to code. */\n"
5418                            "  int b;\n"
5419                            "  #endif\n"
5420                            "#endif\n"
5421                            "}";
5422     const char *ToFormat = "void f() {\n"
5423                            "/* Aligned to preprocessor. */\n"
5424                            "#if 1\n"
5425                            "/* Aligned to code. */\n"
5426                            "int a;\n"
5427                            "#if 1\n"
5428                            "/* Aligned to preprocessor. */\n"
5429                            "#define A 0\n"
5430                            "/* Aligned to code. */\n"
5431                            "int b;\n"
5432                            "#endif\n"
5433                            "#endif\n"
5434                            "}";
5435     EXPECT_EQ(Expected, format(ToFormat, Style));
5436     EXPECT_EQ(Expected, format(Expected, Style));
5437   }
5438 
5439   // Test single comment before preprocessor
5440   verifyFormat("// Comment\n"
5441                "\n"
5442                "#if 1\n"
5443                "#endif",
5444                Style);
5445 }
5446 
5447 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
5448   verifyFormat("{\n  { a #c; }\n}");
5449 }
5450 
5451 TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
5452   EXPECT_EQ("#define A \\\n  {       \\\n    {\nint i;",
5453             format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
5454   EXPECT_EQ("#define A \\\n  }       \\\n  }\nint i;",
5455             format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
5456 }
5457 
5458 TEST_F(FormatTest, EscapedNewlines) {
5459   FormatStyle Narrow = getLLVMStyleWithColumns(11);
5460   EXPECT_EQ("#define A \\\n  int i;  \\\n  int j;",
5461             format("#define A \\\nint i;\\\n  int j;", Narrow));
5462   EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;"));
5463   EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
5464   EXPECT_EQ("/* \\  \\  \\\n */", format("\\\n/* \\  \\  \\\n */"));
5465   EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>"));
5466 
5467   FormatStyle AlignLeft = getLLVMStyle();
5468   AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
5469   EXPECT_EQ("#define MACRO(x) \\\n"
5470             "private:         \\\n"
5471             "  int x(int a);\n",
5472             format("#define MACRO(x) \\\n"
5473                    "private:         \\\n"
5474                    "  int x(int a);\n",
5475                    AlignLeft));
5476 
5477   // CRLF line endings
5478   EXPECT_EQ("#define A \\\r\n  int i;  \\\r\n  int j;",
5479             format("#define A \\\r\nint i;\\\r\n  int j;", Narrow));
5480   EXPECT_EQ("#define A\r\n\r\nint i;", format("#define A \\\r\n\r\n int i;"));
5481   EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
5482   EXPECT_EQ("/* \\  \\  \\\r\n */", format("\\\r\n/* \\  \\  \\\r\n */"));
5483   EXPECT_EQ("<a\r\n\\\\\r\n>", format("<a\r\n\\\\\r\n>"));
5484   EXPECT_EQ("#define MACRO(x) \\\r\n"
5485             "private:         \\\r\n"
5486             "  int x(int a);\r\n",
5487             format("#define MACRO(x) \\\r\n"
5488                    "private:         \\\r\n"
5489                    "  int x(int a);\r\n",
5490                    AlignLeft));
5491 
5492   FormatStyle DontAlign = getLLVMStyle();
5493   DontAlign.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
5494   DontAlign.MaxEmptyLinesToKeep = 3;
5495   // FIXME: can't use verifyFormat here because the newline before
5496   // "public:" is not inserted the first time it's reformatted
5497   EXPECT_EQ("#define A \\\n"
5498             "  class Foo { \\\n"
5499             "    void bar(); \\\n"
5500             "\\\n"
5501             "\\\n"
5502             "\\\n"
5503             "  public: \\\n"
5504             "    void baz(); \\\n"
5505             "  };",
5506             format("#define A \\\n"
5507                    "  class Foo { \\\n"
5508                    "    void bar(); \\\n"
5509                    "\\\n"
5510                    "\\\n"
5511                    "\\\n"
5512                    "  public: \\\n"
5513                    "    void baz(); \\\n"
5514                    "  };",
5515                    DontAlign));
5516 }
5517 
5518 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
5519   verifyFormat("#define A \\\n"
5520                "  int v(  \\\n"
5521                "      a); \\\n"
5522                "  int i;",
5523                getLLVMStyleWithColumns(11));
5524 }
5525 
5526 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
5527   EXPECT_EQ(
5528       "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
5529       "                      \\\n"
5530       "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
5531       "\n"
5532       "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
5533       "    aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
5534       format("  #define   ALooooooooooooooooooooooooooooooooooooooongMacro("
5535              "\\\n"
5536              "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
5537              "  \n"
5538              "   AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
5539              "  aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
5540 }
5541 
5542 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
5543   EXPECT_EQ("int\n"
5544             "#define A\n"
5545             "    a;",
5546             format("int\n#define A\na;"));
5547   verifyFormat("functionCallTo(\n"
5548                "    someOtherFunction(\n"
5549                "        withSomeParameters, whichInSequence,\n"
5550                "        areLongerThanALine(andAnotherCall,\n"
5551                "#define A B\n"
5552                "                           withMoreParamters,\n"
5553                "                           whichStronglyInfluenceTheLayout),\n"
5554                "        andMoreParameters),\n"
5555                "    trailing);",
5556                getLLVMStyleWithColumns(69));
5557   verifyFormat("Foo::Foo()\n"
5558                "#ifdef BAR\n"
5559                "    : baz(0)\n"
5560                "#endif\n"
5561                "{\n"
5562                "}");
5563   verifyFormat("void f() {\n"
5564                "  if (true)\n"
5565                "#ifdef A\n"
5566                "    f(42);\n"
5567                "  x();\n"
5568                "#else\n"
5569                "    g();\n"
5570                "  x();\n"
5571                "#endif\n"
5572                "}");
5573   verifyFormat("void f(param1, param2,\n"
5574                "       param3,\n"
5575                "#ifdef A\n"
5576                "       param4(param5,\n"
5577                "#ifdef A1\n"
5578                "              param6,\n"
5579                "#ifdef A2\n"
5580                "              param7),\n"
5581                "#else\n"
5582                "              param8),\n"
5583                "       param9,\n"
5584                "#endif\n"
5585                "       param10,\n"
5586                "#endif\n"
5587                "       param11)\n"
5588                "#else\n"
5589                "       param12)\n"
5590                "#endif\n"
5591                "{\n"
5592                "  x();\n"
5593                "}",
5594                getLLVMStyleWithColumns(28));
5595   verifyFormat("#if 1\n"
5596                "int i;");
5597   verifyFormat("#if 1\n"
5598                "#endif\n"
5599                "#if 1\n"
5600                "#else\n"
5601                "#endif\n");
5602   verifyFormat("DEBUG({\n"
5603                "  return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5604                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
5605                "});\n"
5606                "#if a\n"
5607                "#else\n"
5608                "#endif");
5609 
5610   verifyIncompleteFormat("void f(\n"
5611                          "#if A\n"
5612                          ");\n"
5613                          "#else\n"
5614                          "#endif");
5615 }
5616 
5617 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
5618   verifyFormat("#endif\n"
5619                "#if B");
5620 }
5621 
5622 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
5623   FormatStyle SingleLine = getLLVMStyle();
5624   SingleLine.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse;
5625   verifyFormat("#if 0\n"
5626                "#elif 1\n"
5627                "#endif\n"
5628                "void foo() {\n"
5629                "  if (test) foo2();\n"
5630                "}",
5631                SingleLine);
5632 }
5633 
5634 TEST_F(FormatTest, LayoutBlockInsideParens) {
5635   verifyFormat("functionCall({ int i; });");
5636   verifyFormat("functionCall({\n"
5637                "  int i;\n"
5638                "  int j;\n"
5639                "});");
5640   verifyFormat("functionCall(\n"
5641                "    {\n"
5642                "      int i;\n"
5643                "      int j;\n"
5644                "    },\n"
5645                "    aaaa, bbbb, cccc);");
5646   verifyFormat("functionA(functionB({\n"
5647                "            int i;\n"
5648                "            int j;\n"
5649                "          }),\n"
5650                "          aaaa, bbbb, cccc);");
5651   verifyFormat("functionCall(\n"
5652                "    {\n"
5653                "      int i;\n"
5654                "      int j;\n"
5655                "    },\n"
5656                "    aaaa, bbbb, // comment\n"
5657                "    cccc);");
5658   verifyFormat("functionA(functionB({\n"
5659                "            int i;\n"
5660                "            int j;\n"
5661                "          }),\n"
5662                "          aaaa, bbbb, // comment\n"
5663                "          cccc);");
5664   verifyFormat("functionCall(aaaa, bbbb, { int i; });");
5665   verifyFormat("functionCall(aaaa, bbbb, {\n"
5666                "  int i;\n"
5667                "  int j;\n"
5668                "});");
5669   verifyFormat(
5670       "Aaa(\n" // FIXME: There shouldn't be a linebreak here.
5671       "    {\n"
5672       "      int i; // break\n"
5673       "    },\n"
5674       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
5675       "                                     ccccccccccccccccc));");
5676   verifyFormat("DEBUG({\n"
5677                "  if (a)\n"
5678                "    f();\n"
5679                "});");
5680 }
5681 
5682 TEST_F(FormatTest, LayoutBlockInsideStatement) {
5683   EXPECT_EQ("SOME_MACRO { int i; }\n"
5684             "int i;",
5685             format("  SOME_MACRO  {int i;}  int i;"));
5686 }
5687 
5688 TEST_F(FormatTest, LayoutNestedBlocks) {
5689   verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
5690                "  struct s {\n"
5691                "    int i;\n"
5692                "  };\n"
5693                "  s kBitsToOs[] = {{10}};\n"
5694                "  for (int i = 0; i < 10; ++i)\n"
5695                "    return;\n"
5696                "}");
5697   verifyFormat("call(parameter, {\n"
5698                "  something();\n"
5699                "  // Comment using all columns.\n"
5700                "  somethingelse();\n"
5701                "});",
5702                getLLVMStyleWithColumns(40));
5703   verifyFormat("DEBUG( //\n"
5704                "    { f(); }, a);");
5705   verifyFormat("DEBUG( //\n"
5706                "    {\n"
5707                "      f(); //\n"
5708                "    },\n"
5709                "    a);");
5710 
5711   EXPECT_EQ("call(parameter, {\n"
5712             "  something();\n"
5713             "  // Comment too\n"
5714             "  // looooooooooong.\n"
5715             "  somethingElse();\n"
5716             "});",
5717             format("call(parameter, {\n"
5718                    "  something();\n"
5719                    "  // Comment too looooooooooong.\n"
5720                    "  somethingElse();\n"
5721                    "});",
5722                    getLLVMStyleWithColumns(29)));
5723   EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int   i; });"));
5724   EXPECT_EQ("DEBUG({ // comment\n"
5725             "  int i;\n"
5726             "});",
5727             format("DEBUG({ // comment\n"
5728                    "int  i;\n"
5729                    "});"));
5730   EXPECT_EQ("DEBUG({\n"
5731             "  int i;\n"
5732             "\n"
5733             "  // comment\n"
5734             "  int j;\n"
5735             "});",
5736             format("DEBUG({\n"
5737                    "  int  i;\n"
5738                    "\n"
5739                    "  // comment\n"
5740                    "  int  j;\n"
5741                    "});"));
5742 
5743   verifyFormat("DEBUG({\n"
5744                "  if (a)\n"
5745                "    return;\n"
5746                "});");
5747   verifyGoogleFormat("DEBUG({\n"
5748                      "  if (a) return;\n"
5749                      "});");
5750   FormatStyle Style = getGoogleStyle();
5751   Style.ColumnLimit = 45;
5752   verifyFormat("Debug(\n"
5753                "    aaaaa,\n"
5754                "    {\n"
5755                "      if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
5756                "    },\n"
5757                "    a);",
5758                Style);
5759 
5760   verifyFormat("SomeFunction({MACRO({ return output; }), b});");
5761 
5762   verifyNoCrash("^{v^{a}}");
5763 }
5764 
5765 TEST_F(FormatTest, FormatNestedBlocksInMacros) {
5766   EXPECT_EQ("#define MACRO()                     \\\n"
5767             "  Debug(aaa, /* force line break */ \\\n"
5768             "        {                           \\\n"
5769             "          int i;                    \\\n"
5770             "          int j;                    \\\n"
5771             "        })",
5772             format("#define   MACRO()   Debug(aaa,  /* force line break */ \\\n"
5773                    "          {  int   i;  int  j;   })",
5774                    getGoogleStyle()));
5775 
5776   EXPECT_EQ("#define A                                       \\\n"
5777             "  [] {                                          \\\n"
5778             "    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(        \\\n"
5779             "        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
5780             "  }",
5781             format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
5782                    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
5783                    getGoogleStyle()));
5784 }
5785 
5786 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
5787   EXPECT_EQ("{}", format("{}"));
5788   verifyFormat("enum E {};");
5789   verifyFormat("enum E {}");
5790   FormatStyle Style = getLLVMStyle();
5791   Style.SpaceInEmptyBlock = true;
5792   EXPECT_EQ("void f() { }", format("void f() {}", Style));
5793   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty;
5794   EXPECT_EQ("while (true) { }", format("while (true) {}", Style));
5795   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
5796   Style.BraceWrapping.BeforeElse = false;
5797   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
5798   verifyFormat("if (a)\n"
5799                "{\n"
5800                "} else if (b)\n"
5801                "{\n"
5802                "} else\n"
5803                "{ }",
5804                Style);
5805   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never;
5806   verifyFormat("if (a) {\n"
5807                "} else if (b) {\n"
5808                "} else {\n"
5809                "}",
5810                Style);
5811   Style.BraceWrapping.BeforeElse = true;
5812   verifyFormat("if (a) { }\n"
5813                "else if (b) { }\n"
5814                "else { }",
5815                Style);
5816 }
5817 
5818 TEST_F(FormatTest, FormatBeginBlockEndMacros) {
5819   FormatStyle Style = getLLVMStyle();
5820   Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$";
5821   Style.MacroBlockEnd = "^[A-Z_]+_END$";
5822   verifyFormat("FOO_BEGIN\n"
5823                "  FOO_ENTRY\n"
5824                "FOO_END",
5825                Style);
5826   verifyFormat("FOO_BEGIN\n"
5827                "  NESTED_FOO_BEGIN\n"
5828                "    NESTED_FOO_ENTRY\n"
5829                "  NESTED_FOO_END\n"
5830                "FOO_END",
5831                Style);
5832   verifyFormat("FOO_BEGIN(Foo, Bar)\n"
5833                "  int x;\n"
5834                "  x = 1;\n"
5835                "FOO_END(Baz)",
5836                Style);
5837 }
5838 
5839 //===----------------------------------------------------------------------===//
5840 // Line break tests.
5841 //===----------------------------------------------------------------------===//
5842 
5843 TEST_F(FormatTest, PreventConfusingIndents) {
5844   verifyFormat(
5845       "void f() {\n"
5846       "  SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
5847       "                         parameter, parameter, parameter)),\n"
5848       "                     SecondLongCall(parameter));\n"
5849       "}");
5850   verifyFormat(
5851       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5852       "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
5853       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
5854       "    aaaaaaaaaaaaaaaaaaaaaaaa);");
5855   verifyFormat(
5856       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5857       "    [aaaaaaaaaaaaaaaaaaaaaaaa\n"
5858       "         [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
5859       "         [aaaaaaaaaaaaaaaaaaaaaaaa]];");
5860   verifyFormat(
5861       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
5862       "    aaaaaaaaaaaaaaaaaaaaaaaa<\n"
5863       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
5864       "    aaaaaaaaaaaaaaaaaaaaaaaa>;");
5865   verifyFormat("int a = bbbb && ccc &&\n"
5866                "        fffff(\n"
5867                "#define A Just forcing a new line\n"
5868                "            ddd);");
5869 }
5870 
5871 TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
5872   verifyFormat(
5873       "bool aaaaaaa =\n"
5874       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
5875       "    bbbbbbbb();");
5876   verifyFormat(
5877       "bool aaaaaaa =\n"
5878       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n"
5879       "    bbbbbbbb();");
5880 
5881   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
5882                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
5883                "    ccccccccc == ddddddddddd;");
5884   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
5885                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n"
5886                "    ccccccccc == ddddddddddd;");
5887   verifyFormat(
5888       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
5889       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n"
5890       "    ccccccccc == ddddddddddd;");
5891 
5892   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
5893                "                 aaaaaa) &&\n"
5894                "         bbbbbb && cccccc;");
5895   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
5896                "                 aaaaaa) >>\n"
5897                "         bbbbbb;");
5898   verifyFormat("aa = Whitespaces.addUntouchableComment(\n"
5899                "    SourceMgr.getSpellingColumnNumber(\n"
5900                "        TheLine.Last->FormatTok.Tok.getLocation()) -\n"
5901                "    1);");
5902 
5903   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
5904                "     bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
5905                "    cccccc) {\n}");
5906   verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
5907                "               bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n"
5908                "              cccccc) {\n}");
5909   verifyFormat("if CONSTEXPR ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
5910                "               bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n"
5911                "              cccccc) {\n}");
5912   verifyFormat("b = a &&\n"
5913                "    // Comment\n"
5914                "    b.c && d;");
5915 
5916   // If the LHS of a comparison is not a binary expression itself, the
5917   // additional linebreak confuses many people.
5918   verifyFormat(
5919       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5920       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
5921       "}");
5922   verifyFormat(
5923       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5924       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
5925       "}");
5926   verifyFormat(
5927       "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
5928       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
5929       "}");
5930   verifyFormat(
5931       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5932       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) <=> 5) {\n"
5933       "}");
5934   // Even explicit parentheses stress the precedence enough to make the
5935   // additional break unnecessary.
5936   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5937                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
5938                "}");
5939   // This cases is borderline, but with the indentation it is still readable.
5940   verifyFormat(
5941       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5942       "        aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5943       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
5944       "}",
5945       getLLVMStyleWithColumns(75));
5946 
5947   // If the LHS is a binary expression, we should still use the additional break
5948   // as otherwise the formatting hides the operator precedence.
5949   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5950                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
5951                "    5) {\n"
5952                "}");
5953   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5954                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa <=>\n"
5955                "    5) {\n"
5956                "}");
5957 
5958   FormatStyle OnePerLine = getLLVMStyle();
5959   OnePerLine.BinPackParameters = false;
5960   verifyFormat(
5961       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
5962       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
5963       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
5964       OnePerLine);
5965 
5966   verifyFormat("int i = someFunction(aaaaaaa, 0)\n"
5967                "                .aaa(aaaaaaaaaaaaa) *\n"
5968                "            aaaaaaa +\n"
5969                "        aaaaaaa;",
5970                getLLVMStyleWithColumns(40));
5971 }
5972 
5973 TEST_F(FormatTest, ExpressionIndentation) {
5974   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5975                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5976                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
5977                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
5978                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
5979                "                     bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
5980                "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
5981                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
5982                "                 ccccccccccccccccccccccccccccccccccccccccc;");
5983   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
5984                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5985                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
5986                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
5987   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5988                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
5989                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
5990                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
5991   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
5992                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
5993                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5994                "        bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
5995   verifyFormat("if () {\n"
5996                "} else if (aaaaa && bbbbb > // break\n"
5997                "                        ccccc) {\n"
5998                "}");
5999   verifyFormat("if () {\n"
6000                "} else if constexpr (aaaaa && bbbbb > // break\n"
6001                "                                  ccccc) {\n"
6002                "}");
6003   verifyFormat("if () {\n"
6004                "} else if CONSTEXPR (aaaaa && bbbbb > // break\n"
6005                "                                  ccccc) {\n"
6006                "}");
6007   verifyFormat("if () {\n"
6008                "} else if (aaaaa &&\n"
6009                "           bbbbb > // break\n"
6010                "               ccccc &&\n"
6011                "           ddddd) {\n"
6012                "}");
6013 
6014   // Presence of a trailing comment used to change indentation of b.
6015   verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
6016                "       b;\n"
6017                "return aaaaaaaaaaaaaaaaaaa +\n"
6018                "       b; //",
6019                getLLVMStyleWithColumns(30));
6020 }
6021 
6022 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
6023   // Not sure what the best system is here. Like this, the LHS can be found
6024   // immediately above an operator (everything with the same or a higher
6025   // indent). The RHS is aligned right of the operator and so compasses
6026   // everything until something with the same indent as the operator is found.
6027   // FIXME: Is this a good system?
6028   FormatStyle Style = getLLVMStyle();
6029   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
6030   verifyFormat(
6031       "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6032       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6033       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6034       "                 == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6035       "                            * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6036       "                        + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6037       "             && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6038       "                        * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6039       "                    > ccccccccccccccccccccccccccccccccccccccccc;",
6040       Style);
6041   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6042                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6043                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6044                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
6045                Style);
6046   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6047                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6048                "              * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6049                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
6050                Style);
6051   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6052                "    == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6053                "               * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6054                "           + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
6055                Style);
6056   verifyFormat("if () {\n"
6057                "} else if (aaaaa\n"
6058                "           && bbbbb // break\n"
6059                "                  > ccccc) {\n"
6060                "}",
6061                Style);
6062   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6063                "       && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
6064                Style);
6065   verifyFormat("return (a)\n"
6066                "       // comment\n"
6067                "       + b;",
6068                Style);
6069   verifyFormat(
6070       "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6071       "                 * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6072       "             + cc;",
6073       Style);
6074 
6075   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6076                "    = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
6077                Style);
6078 
6079   // Forced by comments.
6080   verifyFormat(
6081       "unsigned ContentSize =\n"
6082       "    sizeof(int16_t)   // DWARF ARange version number\n"
6083       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
6084       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
6085       "    + sizeof(int8_t); // Segment Size (in bytes)");
6086 
6087   verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
6088                "       == boost::fusion::at_c<1>(iiii).second;",
6089                Style);
6090 
6091   Style.ColumnLimit = 60;
6092   verifyFormat("zzzzzzzzzz\n"
6093                "    = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6094                "      >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
6095                Style);
6096 
6097   Style.ColumnLimit = 80;
6098   Style.IndentWidth = 4;
6099   Style.TabWidth = 4;
6100   Style.UseTab = FormatStyle::UT_Always;
6101   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
6102   Style.AlignOperands = FormatStyle::OAS_DontAlign;
6103   EXPECT_EQ("return someVeryVeryLongConditionThatBarelyFitsOnALine\n"
6104             "\t&& (someOtherLongishConditionPart1\n"
6105             "\t\t|| someOtherEvenLongerNestedConditionPart2);",
6106             format("return someVeryVeryLongConditionThatBarelyFitsOnALine && "
6107                    "(someOtherLongishConditionPart1 || "
6108                    "someOtherEvenLongerNestedConditionPart2);",
6109                    Style));
6110 }
6111 
6112 TEST_F(FormatTest, ExpressionIndentationStrictAlign) {
6113   FormatStyle Style = getLLVMStyle();
6114   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
6115   Style.AlignOperands = FormatStyle::OAS_AlignAfterOperator;
6116 
6117   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6118                "                   + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6119                "                   + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6120                "              == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6121                "                         * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6122                "                     + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6123                "          && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6124                "                     * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6125                "                 > ccccccccccccccccccccccccccccccccccccccccc;",
6126                Style);
6127   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6128                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6129                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6130                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
6131                Style);
6132   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6133                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6134                "              * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6135                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
6136                Style);
6137   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6138                "    == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6139                "               * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6140                "           + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
6141                Style);
6142   verifyFormat("if () {\n"
6143                "} else if (aaaaa\n"
6144                "           && bbbbb // break\n"
6145                "                  > ccccc) {\n"
6146                "}",
6147                Style);
6148   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6149                "    && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
6150                Style);
6151   verifyFormat("return (a)\n"
6152                "     // comment\n"
6153                "     + b;",
6154                Style);
6155   verifyFormat(
6156       "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6157       "               * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6158       "           + cc;",
6159       Style);
6160   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
6161                "     : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
6162                "                        : 3333333333333333;",
6163                Style);
6164   verifyFormat(
6165       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa    ? bbbbbbbbbbbbbbbbbb\n"
6166       "                           : ccccccccccccccc ? dddddddddddddddddd\n"
6167       "                                             : eeeeeeeeeeeeeeeeee)\n"
6168       "     : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
6169       "                        : 3333333333333333;",
6170       Style);
6171   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6172                "    = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
6173                Style);
6174 
6175   verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
6176                "    == boost::fusion::at_c<1>(iiii).second;",
6177                Style);
6178 
6179   Style.ColumnLimit = 60;
6180   verifyFormat("zzzzzzzzzzzzz\n"
6181                "    = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6182                "   >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
6183                Style);
6184 
6185   // Forced by comments.
6186   Style.ColumnLimit = 80;
6187   verifyFormat(
6188       "unsigned ContentSize\n"
6189       "    = sizeof(int16_t) // DWARF ARange version number\n"
6190       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
6191       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
6192       "    + sizeof(int8_t); // Segment Size (in bytes)",
6193       Style);
6194 
6195   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
6196   verifyFormat(
6197       "unsigned ContentSize =\n"
6198       "    sizeof(int16_t)   // DWARF ARange version number\n"
6199       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
6200       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
6201       "    + sizeof(int8_t); // Segment Size (in bytes)",
6202       Style);
6203 
6204   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
6205   verifyFormat(
6206       "unsigned ContentSize =\n"
6207       "    sizeof(int16_t)   // DWARF ARange version number\n"
6208       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
6209       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
6210       "    + sizeof(int8_t); // Segment Size (in bytes)",
6211       Style);
6212 }
6213 
6214 TEST_F(FormatTest, EnforcedOperatorWraps) {
6215   // Here we'd like to wrap after the || operators, but a comment is forcing an
6216   // earlier wrap.
6217   verifyFormat("bool x = aaaaa //\n"
6218                "         || bbbbb\n"
6219                "         //\n"
6220                "         || cccc;");
6221 }
6222 
6223 TEST_F(FormatTest, NoOperandAlignment) {
6224   FormatStyle Style = getLLVMStyle();
6225   Style.AlignOperands = FormatStyle::OAS_DontAlign;
6226   verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n"
6227                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
6228                "                   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
6229                Style);
6230   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
6231   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6232                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6233                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6234                "        == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6235                "                * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6236                "            + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6237                "    && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6238                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6239                "        > ccccccccccccccccccccccccccccccccccccccccc;",
6240                Style);
6241 
6242   verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6243                "        * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6244                "    + cc;",
6245                Style);
6246   verifyFormat("int a = aa\n"
6247                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6248                "        * cccccccccccccccccccccccccccccccccccc;\n",
6249                Style);
6250 
6251   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
6252   verifyFormat("return (a > b\n"
6253                "    // comment1\n"
6254                "    // comment2\n"
6255                "    || c);",
6256                Style);
6257 }
6258 
6259 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) {
6260   FormatStyle Style = getLLVMStyle();
6261   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
6262   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
6263                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6264                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
6265                Style);
6266 }
6267 
6268 TEST_F(FormatTest, AllowBinPackingInsideArguments) {
6269   FormatStyle Style = getLLVMStyleWithColumns(40);
6270   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
6271   Style.BinPackArguments = false;
6272   verifyFormat("void test() {\n"
6273                "  someFunction(\n"
6274                "      this + argument + is + quite\n"
6275                "      + long + so + it + gets + wrapped\n"
6276                "      + but + remains + bin - packed);\n"
6277                "}",
6278                Style);
6279   verifyFormat("void test() {\n"
6280                "  someFunction(arg1,\n"
6281                "               this + argument + is\n"
6282                "                   + quite + long + so\n"
6283                "                   + it + gets + wrapped\n"
6284                "                   + but + remains + bin\n"
6285                "                   - packed,\n"
6286                "               arg3);\n"
6287                "}",
6288                Style);
6289   verifyFormat("void test() {\n"
6290                "  someFunction(\n"
6291                "      arg1,\n"
6292                "      this + argument + has\n"
6293                "          + anotherFunc(nested,\n"
6294                "                        calls + whose\n"
6295                "                            + arguments\n"
6296                "                            + are + also\n"
6297                "                            + wrapped,\n"
6298                "                        in + addition)\n"
6299                "          + to + being + bin - packed,\n"
6300                "      arg3);\n"
6301                "}",
6302                Style);
6303 
6304   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
6305   verifyFormat("void test() {\n"
6306                "  someFunction(\n"
6307                "      arg1,\n"
6308                "      this + argument + has +\n"
6309                "          anotherFunc(nested,\n"
6310                "                      calls + whose +\n"
6311                "                          arguments +\n"
6312                "                          are + also +\n"
6313                "                          wrapped,\n"
6314                "                      in + addition) +\n"
6315                "          to + being + bin - packed,\n"
6316                "      arg3);\n"
6317                "}",
6318                Style);
6319 }
6320 
6321 TEST_F(FormatTest, ConstructorInitializers) {
6322   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
6323   verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
6324                getLLVMStyleWithColumns(45));
6325   verifyFormat("Constructor()\n"
6326                "    : Inttializer(FitsOnTheLine) {}",
6327                getLLVMStyleWithColumns(44));
6328   verifyFormat("Constructor()\n"
6329                "    : Inttializer(FitsOnTheLine) {}",
6330                getLLVMStyleWithColumns(43));
6331 
6332   verifyFormat("template <typename T>\n"
6333                "Constructor() : Initializer(FitsOnTheLine) {}",
6334                getLLVMStyleWithColumns(45));
6335 
6336   verifyFormat(
6337       "SomeClass::Constructor()\n"
6338       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
6339 
6340   verifyFormat(
6341       "SomeClass::Constructor()\n"
6342       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6343       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
6344   verifyFormat(
6345       "SomeClass::Constructor()\n"
6346       "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6347       "      aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
6348   verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6349                "            aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
6350                "    : aaaaaaaaaa(aaaaaa) {}");
6351 
6352   verifyFormat("Constructor()\n"
6353                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6354                "      aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6355                "                               aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6356                "      aaaaaaaaaaaaaaaaaaaaaaa() {}");
6357 
6358   verifyFormat("Constructor()\n"
6359                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6360                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
6361 
6362   verifyFormat("Constructor(int Parameter = 0)\n"
6363                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
6364                "      aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
6365   verifyFormat("Constructor()\n"
6366                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
6367                "}",
6368                getLLVMStyleWithColumns(60));
6369   verifyFormat("Constructor()\n"
6370                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6371                "          aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}");
6372 
6373   // Here a line could be saved by splitting the second initializer onto two
6374   // lines, but that is not desirable.
6375   verifyFormat("Constructor()\n"
6376                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
6377                "      aaaaaaaaaaa(aaaaaaaaaaa),\n"
6378                "      aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
6379 
6380   FormatStyle OnePerLine = getLLVMStyle();
6381   OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_Never;
6382   verifyFormat("MyClass::MyClass()\n"
6383                "    : a(a),\n"
6384                "      b(b),\n"
6385                "      c(c) {}",
6386                OnePerLine);
6387   verifyFormat("MyClass::MyClass()\n"
6388                "    : a(a), // comment\n"
6389                "      b(b),\n"
6390                "      c(c) {}",
6391                OnePerLine);
6392   verifyFormat("MyClass::MyClass(int a)\n"
6393                "    : b(a),      // comment\n"
6394                "      c(a + 1) { // lined up\n"
6395                "}",
6396                OnePerLine);
6397   verifyFormat("Constructor()\n"
6398                "    : a(b, b, b) {}",
6399                OnePerLine);
6400   OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6401   OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false;
6402   verifyFormat("SomeClass::Constructor()\n"
6403                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6404                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6405                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
6406                OnePerLine);
6407   verifyFormat("SomeClass::Constructor()\n"
6408                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
6409                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6410                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
6411                OnePerLine);
6412   verifyFormat("MyClass::MyClass(int var)\n"
6413                "    : some_var_(var),            // 4 space indent\n"
6414                "      some_other_var_(var + 1) { // lined up\n"
6415                "}",
6416                OnePerLine);
6417   verifyFormat("Constructor()\n"
6418                "    : aaaaa(aaaaaa),\n"
6419                "      aaaaa(aaaaaa),\n"
6420                "      aaaaa(aaaaaa),\n"
6421                "      aaaaa(aaaaaa),\n"
6422                "      aaaaa(aaaaaa) {}",
6423                OnePerLine);
6424   verifyFormat("Constructor()\n"
6425                "    : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
6426                "            aaaaaaaaaaaaaaaaaaaaaa) {}",
6427                OnePerLine);
6428   OnePerLine.BinPackParameters = false;
6429   verifyFormat(
6430       "Constructor()\n"
6431       "    : aaaaaaaaaaaaaaaaaaaaaaaa(\n"
6432       "          aaaaaaaaaaa().aaa(),\n"
6433       "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
6434       OnePerLine);
6435   OnePerLine.ColumnLimit = 60;
6436   verifyFormat("Constructor()\n"
6437                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
6438                "      bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
6439                OnePerLine);
6440 
6441   EXPECT_EQ("Constructor()\n"
6442             "    : // Comment forcing unwanted break.\n"
6443             "      aaaa(aaaa) {}",
6444             format("Constructor() :\n"
6445                    "    // Comment forcing unwanted break.\n"
6446                    "    aaaa(aaaa) {}"));
6447 }
6448 
6449 TEST_F(FormatTest, AllowAllConstructorInitializersOnNextLine) {
6450   FormatStyle Style = getLLVMStyleWithColumns(60);
6451   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
6452   Style.BinPackParameters = false;
6453 
6454   for (int i = 0; i < 4; ++i) {
6455     // Test all combinations of parameters that should not have an effect.
6456     Style.AllowAllParametersOfDeclarationOnNextLine = i & 1;
6457     Style.AllowAllArgumentsOnNextLine = i & 2;
6458 
6459     Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6460     Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
6461     verifyFormat("Constructor()\n"
6462                  "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6463                  Style);
6464     verifyFormat("Constructor() : a(a), b(b) {}", Style);
6465 
6466     Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
6467     verifyFormat("Constructor()\n"
6468                  "    : aaaaaaaaaaaaaaaaaaaa(a)\n"
6469                  "    , bbbbbbbbbbbbbbbbbbbbb(b) {}",
6470                  Style);
6471     verifyFormat("Constructor() : a(a), b(b) {}", Style);
6472 
6473     Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
6474     Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6475     verifyFormat("Constructor()\n"
6476                  "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6477                  Style);
6478 
6479     Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
6480     verifyFormat("Constructor()\n"
6481                  "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
6482                  "      bbbbbbbbbbbbbbbbbbbbb(b) {}",
6483                  Style);
6484 
6485     Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
6486     Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6487     verifyFormat("Constructor() :\n"
6488                  "    aaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6489                  Style);
6490 
6491     Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
6492     verifyFormat("Constructor() :\n"
6493                  "    aaaaaaaaaaaaaaaaaa(a),\n"
6494                  "    bbbbbbbbbbbbbbbbbbbbb(b) {}",
6495                  Style);
6496   }
6497 
6498   // Test interactions between AllowAllParametersOfDeclarationOnNextLine and
6499   // AllowAllConstructorInitializersOnNextLine in all
6500   // BreakConstructorInitializers modes
6501   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
6502   Style.AllowAllParametersOfDeclarationOnNextLine = true;
6503   verifyFormat("SomeClassWithALongName::Constructor(\n"
6504                "    int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n"
6505                "    : aaaaaaaaaaaaaaaaaaaa(a)\n"
6506                "    , bbbbbbbbbbbbbbbbbbbbb(b) {}",
6507                Style);
6508 
6509   Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6510   verifyFormat("SomeClassWithALongName::Constructor(\n"
6511                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6512                "    int bbbbbbbbbbbbb,\n"
6513                "    int cccccccccccccccc)\n"
6514                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6515                Style);
6516 
6517   Style.AllowAllParametersOfDeclarationOnNextLine = false;
6518   Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
6519   verifyFormat("SomeClassWithALongName::Constructor(\n"
6520                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6521                "    int bbbbbbbbbbbbb)\n"
6522                "    : aaaaaaaaaaaaaaaaaaaa(a)\n"
6523                "    , bbbbbbbbbbbbbbbbbbbbb(b) {}",
6524                Style);
6525 
6526   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
6527 
6528   Style.AllowAllParametersOfDeclarationOnNextLine = true;
6529   verifyFormat("SomeClassWithALongName::Constructor(\n"
6530                "    int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n"
6531                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
6532                "      bbbbbbbbbbbbbbbbbbbbb(b) {}",
6533                Style);
6534 
6535   Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6536   verifyFormat("SomeClassWithALongName::Constructor(\n"
6537                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6538                "    int bbbbbbbbbbbbb,\n"
6539                "    int cccccccccccccccc)\n"
6540                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6541                Style);
6542 
6543   Style.AllowAllParametersOfDeclarationOnNextLine = false;
6544   Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
6545   verifyFormat("SomeClassWithALongName::Constructor(\n"
6546                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6547                "    int bbbbbbbbbbbbb)\n"
6548                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
6549                "      bbbbbbbbbbbbbbbbbbbbb(b) {}",
6550                Style);
6551 
6552   Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
6553   Style.AllowAllParametersOfDeclarationOnNextLine = true;
6554   verifyFormat("SomeClassWithALongName::Constructor(\n"
6555                "    int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb) :\n"
6556                "    aaaaaaaaaaaaaaaaaaaa(a),\n"
6557                "    bbbbbbbbbbbbbbbbbbbbb(b) {}",
6558                Style);
6559 
6560   Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6561   verifyFormat("SomeClassWithALongName::Constructor(\n"
6562                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6563                "    int bbbbbbbbbbbbb,\n"
6564                "    int cccccccccccccccc) :\n"
6565                "    aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6566                Style);
6567 
6568   Style.AllowAllParametersOfDeclarationOnNextLine = false;
6569   Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
6570   verifyFormat("SomeClassWithALongName::Constructor(\n"
6571                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6572                "    int bbbbbbbbbbbbb) :\n"
6573                "    aaaaaaaaaaaaaaaaaaaa(a),\n"
6574                "    bbbbbbbbbbbbbbbbbbbbb(b) {}",
6575                Style);
6576 }
6577 
6578 TEST_F(FormatTest, AllowAllArgumentsOnNextLine) {
6579   FormatStyle Style = getLLVMStyleWithColumns(60);
6580   Style.BinPackArguments = false;
6581   for (int i = 0; i < 4; ++i) {
6582     // Test all combinations of parameters that should not have an effect.
6583     Style.AllowAllParametersOfDeclarationOnNextLine = i & 1;
6584     Style.PackConstructorInitializers =
6585         i & 2 ? FormatStyle::PCIS_BinPack : FormatStyle::PCIS_Never;
6586 
6587     Style.AllowAllArgumentsOnNextLine = true;
6588     verifyFormat("void foo() {\n"
6589                  "  FunctionCallWithReallyLongName(\n"
6590                  "      aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb);\n"
6591                  "}",
6592                  Style);
6593     Style.AllowAllArgumentsOnNextLine = false;
6594     verifyFormat("void foo() {\n"
6595                  "  FunctionCallWithReallyLongName(\n"
6596                  "      aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6597                  "      bbbbbbbbbbbb);\n"
6598                  "}",
6599                  Style);
6600 
6601     Style.AllowAllArgumentsOnNextLine = true;
6602     verifyFormat("void foo() {\n"
6603                  "  auto VariableWithReallyLongName = {\n"
6604                  "      aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb};\n"
6605                  "}",
6606                  Style);
6607     Style.AllowAllArgumentsOnNextLine = false;
6608     verifyFormat("void foo() {\n"
6609                  "  auto VariableWithReallyLongName = {\n"
6610                  "      aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6611                  "      bbbbbbbbbbbb};\n"
6612                  "}",
6613                  Style);
6614   }
6615 
6616   // This parameter should not affect declarations.
6617   Style.BinPackParameters = false;
6618   Style.AllowAllArgumentsOnNextLine = false;
6619   Style.AllowAllParametersOfDeclarationOnNextLine = true;
6620   verifyFormat("void FunctionCallWithReallyLongName(\n"
6621                "    int aaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbb);",
6622                Style);
6623   Style.AllowAllParametersOfDeclarationOnNextLine = false;
6624   verifyFormat("void FunctionCallWithReallyLongName(\n"
6625                "    int aaaaaaaaaaaaaaaaaaaaaaa,\n"
6626                "    int bbbbbbbbbbbb);",
6627                Style);
6628 }
6629 
6630 TEST_F(FormatTest, AllowAllArgumentsOnNextLineDontAlign) {
6631   // Check that AllowAllArgumentsOnNextLine is respected for both BAS_DontAlign
6632   // and BAS_Align.
6633   FormatStyle Style = getLLVMStyleWithColumns(35);
6634   StringRef Input = "functionCall(paramA, paramB, paramC);\n"
6635                     "void functionDecl(int A, int B, int C);";
6636   Style.AllowAllArgumentsOnNextLine = false;
6637   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
6638   EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n"
6639                       "    paramC);\n"
6640                       "void functionDecl(int A, int B,\n"
6641                       "    int C);"),
6642             format(Input, Style));
6643   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
6644   EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n"
6645                       "             paramC);\n"
6646                       "void functionDecl(int A, int B,\n"
6647                       "                  int C);"),
6648             format(Input, Style));
6649   // However, BAS_AlwaysBreak should take precedence over
6650   // AllowAllArgumentsOnNextLine.
6651   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
6652   EXPECT_EQ(StringRef("functionCall(\n"
6653                       "    paramA, paramB, paramC);\n"
6654                       "void functionDecl(\n"
6655                       "    int A, int B, int C);"),
6656             format(Input, Style));
6657 
6658   // When AllowAllArgumentsOnNextLine is set, we prefer breaking before the
6659   // first argument.
6660   Style.AllowAllArgumentsOnNextLine = true;
6661   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
6662   EXPECT_EQ(StringRef("functionCall(\n"
6663                       "    paramA, paramB, paramC);\n"
6664                       "void functionDecl(\n"
6665                       "    int A, int B, int C);"),
6666             format(Input, Style));
6667   // It wouldn't fit on one line with aligned parameters so this setting
6668   // doesn't change anything for BAS_Align.
6669   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
6670   EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n"
6671                       "             paramC);\n"
6672                       "void functionDecl(int A, int B,\n"
6673                       "                  int C);"),
6674             format(Input, Style));
6675   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
6676   EXPECT_EQ(StringRef("functionCall(\n"
6677                       "    paramA, paramB, paramC);\n"
6678                       "void functionDecl(\n"
6679                       "    int A, int B, int C);"),
6680             format(Input, Style));
6681 }
6682 
6683 TEST_F(FormatTest, BreakConstructorInitializersAfterColon) {
6684   FormatStyle Style = getLLVMStyle();
6685   Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
6686 
6687   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
6688   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}",
6689                getStyleWithColumns(Style, 45));
6690   verifyFormat("Constructor() :\n"
6691                "    Initializer(FitsOnTheLine) {}",
6692                getStyleWithColumns(Style, 44));
6693   verifyFormat("Constructor() :\n"
6694                "    Initializer(FitsOnTheLine) {}",
6695                getStyleWithColumns(Style, 43));
6696 
6697   verifyFormat("template <typename T>\n"
6698                "Constructor() : Initializer(FitsOnTheLine) {}",
6699                getStyleWithColumns(Style, 50));
6700   Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6701   verifyFormat(
6702       "SomeClass::Constructor() :\n"
6703       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
6704       Style);
6705 
6706   Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack;
6707   verifyFormat(
6708       "SomeClass::Constructor() :\n"
6709       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
6710       Style);
6711 
6712   verifyFormat(
6713       "SomeClass::Constructor() :\n"
6714       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6715       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
6716       Style);
6717   verifyFormat(
6718       "SomeClass::Constructor() :\n"
6719       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6720       "    aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
6721       Style);
6722   verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6723                "            aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
6724                "    aaaaaaaaaa(aaaaaa) {}",
6725                Style);
6726 
6727   verifyFormat("Constructor() :\n"
6728                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6729                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6730                "                             aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6731                "    aaaaaaaaaaaaaaaaaaaaaaa() {}",
6732                Style);
6733 
6734   verifyFormat("Constructor() :\n"
6735                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6736                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
6737                Style);
6738 
6739   verifyFormat("Constructor(int Parameter = 0) :\n"
6740                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
6741                "    aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}",
6742                Style);
6743   verifyFormat("Constructor() :\n"
6744                "    aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
6745                "}",
6746                getStyleWithColumns(Style, 60));
6747   verifyFormat("Constructor() :\n"
6748                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6749                "        aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}",
6750                Style);
6751 
6752   // Here a line could be saved by splitting the second initializer onto two
6753   // lines, but that is not desirable.
6754   verifyFormat("Constructor() :\n"
6755                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
6756                "    aaaaaaaaaaa(aaaaaaaaaaa),\n"
6757                "    aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
6758                Style);
6759 
6760   FormatStyle OnePerLine = Style;
6761   OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
6762   verifyFormat("SomeClass::Constructor() :\n"
6763                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6764                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6765                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
6766                OnePerLine);
6767   verifyFormat("SomeClass::Constructor() :\n"
6768                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
6769                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6770                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
6771                OnePerLine);
6772   verifyFormat("MyClass::MyClass(int var) :\n"
6773                "    some_var_(var),            // 4 space indent\n"
6774                "    some_other_var_(var + 1) { // lined up\n"
6775                "}",
6776                OnePerLine);
6777   verifyFormat("Constructor() :\n"
6778                "    aaaaa(aaaaaa),\n"
6779                "    aaaaa(aaaaaa),\n"
6780                "    aaaaa(aaaaaa),\n"
6781                "    aaaaa(aaaaaa),\n"
6782                "    aaaaa(aaaaaa) {}",
6783                OnePerLine);
6784   verifyFormat("Constructor() :\n"
6785                "    aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
6786                "          aaaaaaaaaaaaaaaaaaaaaa) {}",
6787                OnePerLine);
6788   OnePerLine.BinPackParameters = false;
6789   verifyFormat("Constructor() :\n"
6790                "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
6791                "        aaaaaaaaaaa().aaa(),\n"
6792                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
6793                OnePerLine);
6794   OnePerLine.ColumnLimit = 60;
6795   verifyFormat("Constructor() :\n"
6796                "    aaaaaaaaaaaaaaaaaaaa(a),\n"
6797                "    bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
6798                OnePerLine);
6799 
6800   EXPECT_EQ("Constructor() :\n"
6801             "    // Comment forcing unwanted break.\n"
6802             "    aaaa(aaaa) {}",
6803             format("Constructor() :\n"
6804                    "    // Comment forcing unwanted break.\n"
6805                    "    aaaa(aaaa) {}",
6806                    Style));
6807 
6808   Style.ColumnLimit = 0;
6809   verifyFormat("SomeClass::Constructor() :\n"
6810                "    a(a) {}",
6811                Style);
6812   verifyFormat("SomeClass::Constructor() noexcept :\n"
6813                "    a(a) {}",
6814                Style);
6815   verifyFormat("SomeClass::Constructor() :\n"
6816                "    a(a), b(b), c(c) {}",
6817                Style);
6818   verifyFormat("SomeClass::Constructor() :\n"
6819                "    a(a) {\n"
6820                "  foo();\n"
6821                "  bar();\n"
6822                "}",
6823                Style);
6824 
6825   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
6826   verifyFormat("SomeClass::Constructor() :\n"
6827                "    a(a), b(b), c(c) {\n"
6828                "}",
6829                Style);
6830   verifyFormat("SomeClass::Constructor() :\n"
6831                "    a(a) {\n"
6832                "}",
6833                Style);
6834 
6835   Style.ColumnLimit = 80;
6836   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
6837   Style.ConstructorInitializerIndentWidth = 2;
6838   verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}", Style);
6839   verifyFormat("SomeClass::Constructor() :\n"
6840                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6841                "  bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}",
6842                Style);
6843 
6844   // `ConstructorInitializerIndentWidth` actually applies to InheritanceList as
6845   // well
6846   Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
6847   verifyFormat(
6848       "class SomeClass\n"
6849       "  : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6850       "    public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
6851       Style);
6852   Style.BreakInheritanceList = FormatStyle::BILS_BeforeComma;
6853   verifyFormat(
6854       "class SomeClass\n"
6855       "  : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6856       "  , public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
6857       Style);
6858   Style.BreakInheritanceList = FormatStyle::BILS_AfterColon;
6859   verifyFormat(
6860       "class SomeClass :\n"
6861       "  public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6862       "  public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
6863       Style);
6864   Style.BreakInheritanceList = FormatStyle::BILS_AfterComma;
6865   verifyFormat(
6866       "class SomeClass\n"
6867       "  : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6868       "    public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
6869       Style);
6870 }
6871 
6872 #ifndef EXPENSIVE_CHECKS
6873 // Expensive checks enables libstdc++ checking which includes validating the
6874 // state of ranges used in std::priority_queue - this blows out the
6875 // runtime/scalability of the function and makes this test unacceptably slow.
6876 TEST_F(FormatTest, MemoizationTests) {
6877   // This breaks if the memoization lookup does not take \c Indent and
6878   // \c LastSpace into account.
6879   verifyFormat(
6880       "extern CFRunLoopTimerRef\n"
6881       "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
6882       "                     CFTimeInterval interval, CFOptionFlags flags,\n"
6883       "                     CFIndex order, CFRunLoopTimerCallBack callout,\n"
6884       "                     CFRunLoopTimerContext *context) {}");
6885 
6886   // Deep nesting somewhat works around our memoization.
6887   verifyFormat(
6888       "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
6889       "    aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
6890       "        aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
6891       "            aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
6892       "                aaaaa())))))))))))))))))))))))))))))))))))))));",
6893       getLLVMStyleWithColumns(65));
6894   verifyFormat(
6895       "aaaaa(\n"
6896       "    aaaaa,\n"
6897       "    aaaaa(\n"
6898       "        aaaaa,\n"
6899       "        aaaaa(\n"
6900       "            aaaaa,\n"
6901       "            aaaaa(\n"
6902       "                aaaaa,\n"
6903       "                aaaaa(\n"
6904       "                    aaaaa,\n"
6905       "                    aaaaa(\n"
6906       "                        aaaaa,\n"
6907       "                        aaaaa(\n"
6908       "                            aaaaa,\n"
6909       "                            aaaaa(\n"
6910       "                                aaaaa,\n"
6911       "                                aaaaa(\n"
6912       "                                    aaaaa,\n"
6913       "                                    aaaaa(\n"
6914       "                                        aaaaa,\n"
6915       "                                        aaaaa(\n"
6916       "                                            aaaaa,\n"
6917       "                                            aaaaa(\n"
6918       "                                                aaaaa,\n"
6919       "                                                aaaaa))))))))))));",
6920       getLLVMStyleWithColumns(65));
6921   verifyFormat(
6922       "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"
6923       "                                  a),\n"
6924       "                                a),\n"
6925       "                              a),\n"
6926       "                            a),\n"
6927       "                          a),\n"
6928       "                        a),\n"
6929       "                      a),\n"
6930       "                    a),\n"
6931       "                  a),\n"
6932       "                a),\n"
6933       "              a),\n"
6934       "            a),\n"
6935       "          a),\n"
6936       "        a),\n"
6937       "      a),\n"
6938       "    a),\n"
6939       "  a)",
6940       getLLVMStyleWithColumns(65));
6941 
6942   // This test takes VERY long when memoization is broken.
6943   FormatStyle OnePerLine = getLLVMStyle();
6944   OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6945   OnePerLine.BinPackParameters = false;
6946   std::string input = "Constructor()\n"
6947                       "    : aaaa(a,\n";
6948   for (unsigned i = 0, e = 80; i != e; ++i) {
6949     input += "           a,\n";
6950   }
6951   input += "           a) {}";
6952   verifyFormat(input, OnePerLine);
6953 }
6954 #endif
6955 
6956 TEST_F(FormatTest, BreaksAsHighAsPossible) {
6957   verifyFormat(
6958       "void f() {\n"
6959       "  if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
6960       "      (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
6961       "    f();\n"
6962       "}");
6963   verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
6964                "    Intervals[i - 1].getRange().getLast()) {\n}");
6965 }
6966 
6967 TEST_F(FormatTest, BreaksFunctionDeclarations) {
6968   // Principially, we break function declarations in a certain order:
6969   // 1) break amongst arguments.
6970   verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
6971                "                              Cccccccccccccc cccccccccccccc);");
6972   verifyFormat("template <class TemplateIt>\n"
6973                "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
6974                "                            TemplateIt *stop) {}");
6975 
6976   // 2) break after return type.
6977   verifyFormat(
6978       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6979       "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);",
6980       getGoogleStyle());
6981 
6982   // 3) break after (.
6983   verifyFormat(
6984       "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
6985       "    Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);",
6986       getGoogleStyle());
6987 
6988   // 4) break before after nested name specifiers.
6989   verifyFormat(
6990       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6991       "SomeClasssssssssssssssssssssssssssssssssssssss::\n"
6992       "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);",
6993       getGoogleStyle());
6994 
6995   // However, there are exceptions, if a sufficient amount of lines can be
6996   // saved.
6997   // FIXME: The precise cut-offs wrt. the number of saved lines might need some
6998   // more adjusting.
6999   verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
7000                "                                  Cccccccccccccc cccccccccc,\n"
7001                "                                  Cccccccccccccc cccccccccc,\n"
7002                "                                  Cccccccccccccc cccccccccc,\n"
7003                "                                  Cccccccccccccc cccccccccc);");
7004   verifyFormat(
7005       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7006       "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
7007       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
7008       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);",
7009       getGoogleStyle());
7010   verifyFormat(
7011       "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
7012       "                                          Cccccccccccccc cccccccccc,\n"
7013       "                                          Cccccccccccccc cccccccccc,\n"
7014       "                                          Cccccccccccccc cccccccccc,\n"
7015       "                                          Cccccccccccccc cccccccccc,\n"
7016       "                                          Cccccccccccccc cccccccccc,\n"
7017       "                                          Cccccccccccccc cccccccccc);");
7018   verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
7019                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
7020                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
7021                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
7022                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
7023 
7024   // Break after multi-line parameters.
7025   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7026                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7027                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7028                "    bbbb bbbb);");
7029   verifyFormat("void SomeLoooooooooooongFunction(\n"
7030                "    std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
7031                "        aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7032                "    int bbbbbbbbbbbbb);");
7033 
7034   // Treat overloaded operators like other functions.
7035   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
7036                "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
7037   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
7038                "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
7039   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
7040                "operator<<(const SomeLooooooooooooooooooooooooogType &other);");
7041   verifyGoogleFormat(
7042       "SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
7043       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
7044   verifyGoogleFormat(
7045       "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
7046       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
7047   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7048                "    int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
7049   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
7050                "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
7051   verifyGoogleFormat(
7052       "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n"
7053       "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7054       "    bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}");
7055   verifyGoogleFormat("template <typename T>\n"
7056                      "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7057                      "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n"
7058                      "    aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);");
7059 
7060   FormatStyle Style = getLLVMStyle();
7061   Style.PointerAlignment = FormatStyle::PAS_Left;
7062   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7063                "    aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}",
7064                Style);
7065   verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
7066                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
7067                Style);
7068 }
7069 
7070 TEST_F(FormatTest, DontBreakBeforeQualifiedOperator) {
7071   // Regression test for https://bugs.llvm.org/show_bug.cgi?id=40516:
7072   // Prefer keeping `::` followed by `operator` together.
7073   EXPECT_EQ("const aaaa::bbbbbbb &\n"
7074             "ccccccccc::operator++() {\n"
7075             "  stuff();\n"
7076             "}",
7077             format("const aaaa::bbbbbbb\n"
7078                    "&ccccccccc::operator++() { stuff(); }",
7079                    getLLVMStyleWithColumns(40)));
7080 }
7081 
7082 TEST_F(FormatTest, TrailingReturnType) {
7083   verifyFormat("auto foo() -> int;\n");
7084   // correct trailing return type spacing
7085   verifyFormat("auto operator->() -> int;\n");
7086   verifyFormat("auto operator++(int) -> int;\n");
7087 
7088   verifyFormat("struct S {\n"
7089                "  auto bar() const -> int;\n"
7090                "};");
7091   verifyFormat("template <size_t Order, typename T>\n"
7092                "auto load_img(const std::string &filename)\n"
7093                "    -> alias::tensor<Order, T, mem::tag::cpu> {}");
7094   verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n"
7095                "    -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}");
7096   verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}");
7097   verifyFormat("template <typename T>\n"
7098                "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n"
7099                "    -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());");
7100 
7101   // Not trailing return types.
7102   verifyFormat("void f() { auto a = b->c(); }");
7103   verifyFormat("auto a = p->foo();");
7104   verifyFormat("int a = p->foo();");
7105   verifyFormat("auto lmbd = [] NOEXCEPT -> int { return 0; };");
7106 }
7107 
7108 TEST_F(FormatTest, DeductionGuides) {
7109   verifyFormat("template <class T> A(const T &, const T &) -> A<T &>;");
7110   verifyFormat("template <class T> explicit A(T &, T &&) -> A<T>;");
7111   verifyFormat("template <class... Ts> S(Ts...) -> S<Ts...>;");
7112   verifyFormat(
7113       "template <class... T>\n"
7114       "array(T &&...t) -> array<std::common_type_t<T...>, sizeof...(T)>;");
7115   verifyFormat("template <class T> A() -> A<decltype(p->foo<3>())>;");
7116   verifyFormat("template <class T> A() -> A<decltype(foo<traits<1>>)>;");
7117   verifyFormat("template <class T> A() -> A<sizeof(p->foo<1>)>;");
7118   verifyFormat("template <class T> A() -> A<(3 < 2)>;");
7119   verifyFormat("template <class T> A() -> A<((3) < (2))>;");
7120   verifyFormat("template <class T> x() -> x<1>;");
7121   verifyFormat("template <class T> explicit x(T &) -> x<1>;");
7122 
7123   // Ensure not deduction guides.
7124   verifyFormat("c()->f<int>();");
7125   verifyFormat("x()->foo<1>;");
7126   verifyFormat("x = p->foo<3>();");
7127   verifyFormat("x()->x<1>();");
7128   verifyFormat("x()->x<1>;");
7129 }
7130 
7131 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
7132   // Avoid breaking before trailing 'const' or other trailing annotations, if
7133   // they are not function-like.
7134   FormatStyle Style = getGoogleStyleWithColumns(47);
7135   verifyFormat("void someLongFunction(\n"
7136                "    int someLoooooooooooooongParameter) const {\n}",
7137                getLLVMStyleWithColumns(47));
7138   verifyFormat("LoooooongReturnType\n"
7139                "someLoooooooongFunction() const {}",
7140                getLLVMStyleWithColumns(47));
7141   verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
7142                "    const {}",
7143                Style);
7144   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
7145                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
7146   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
7147                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
7148   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
7149                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
7150   verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
7151                "                   aaaaaaaaaaa aaaaa) const override;");
7152   verifyGoogleFormat(
7153       "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7154       "    const override;");
7155 
7156   // Even if the first parameter has to be wrapped.
7157   verifyFormat("void someLongFunction(\n"
7158                "    int someLongParameter) const {}",
7159                getLLVMStyleWithColumns(46));
7160   verifyFormat("void someLongFunction(\n"
7161                "    int someLongParameter) const {}",
7162                Style);
7163   verifyFormat("void someLongFunction(\n"
7164                "    int someLongParameter) override {}",
7165                Style);
7166   verifyFormat("void someLongFunction(\n"
7167                "    int someLongParameter) OVERRIDE {}",
7168                Style);
7169   verifyFormat("void someLongFunction(\n"
7170                "    int someLongParameter) final {}",
7171                Style);
7172   verifyFormat("void someLongFunction(\n"
7173                "    int someLongParameter) FINAL {}",
7174                Style);
7175   verifyFormat("void someLongFunction(\n"
7176                "    int parameter) const override {}",
7177                Style);
7178 
7179   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
7180   verifyFormat("void someLongFunction(\n"
7181                "    int someLongParameter) const\n"
7182                "{\n"
7183                "}",
7184                Style);
7185 
7186   Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
7187   verifyFormat("void someLongFunction(\n"
7188                "    int someLongParameter) const\n"
7189                "  {\n"
7190                "  }",
7191                Style);
7192 
7193   // Unless these are unknown annotations.
7194   verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
7195                "                  aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7196                "    LONG_AND_UGLY_ANNOTATION;");
7197 
7198   // Breaking before function-like trailing annotations is fine to keep them
7199   // close to their arguments.
7200   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7201                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
7202   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
7203                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
7204   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
7205                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
7206   verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n"
7207                      "    AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);");
7208   verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});");
7209 
7210   verifyFormat(
7211       "void aaaaaaaaaaaaaaaaaa()\n"
7212       "    __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
7213       "                   aaaaaaaaaaaaaaaaaaaaaaaaa));");
7214   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7215                "    __attribute__((unused));");
7216   verifyGoogleFormat(
7217       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7218       "    GUARDED_BY(aaaaaaaaaaaa);");
7219   verifyGoogleFormat(
7220       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7221       "    GUARDED_BY(aaaaaaaaaaaa);");
7222   verifyGoogleFormat(
7223       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
7224       "    aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
7225   verifyGoogleFormat(
7226       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
7227       "    aaaaaaaaaaaaaaaaaaaaaaaaa;");
7228 }
7229 
7230 TEST_F(FormatTest, FunctionAnnotations) {
7231   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
7232                "int OldFunction(const string &parameter) {}");
7233   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
7234                "string OldFunction(const string &parameter) {}");
7235   verifyFormat("template <typename T>\n"
7236                "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
7237                "string OldFunction(const string &parameter) {}");
7238 
7239   // Not function annotations.
7240   verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7241                "                << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
7242   verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n"
7243                "       ThisIsATestWithAReallyReallyReallyReallyLongName) {}");
7244   verifyFormat("MACRO(abc).function() // wrap\n"
7245                "    << abc;");
7246   verifyFormat("MACRO(abc)->function() // wrap\n"
7247                "    << abc;");
7248   verifyFormat("MACRO(abc)::function() // wrap\n"
7249                "    << abc;");
7250 }
7251 
7252 TEST_F(FormatTest, BreaksDesireably) {
7253   verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
7254                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
7255                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
7256   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7257                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
7258                "}");
7259 
7260   verifyFormat(
7261       "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7262       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
7263 
7264   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7265                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7266                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
7267 
7268   verifyFormat(
7269       "aaaaaaaa(aaaaaaaaaaaaa,\n"
7270       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7271       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
7272       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7273       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
7274 
7275   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
7276                "    (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7277 
7278   verifyFormat(
7279       "void f() {\n"
7280       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
7281       "                                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
7282       "}");
7283   verifyFormat(
7284       "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7285       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
7286   verifyFormat(
7287       "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7288       "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
7289   verifyFormat(
7290       "aaaaaa(aaa,\n"
7291       "       new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7292       "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7293       "       aaaa);");
7294   verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7295                "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7296                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7297 
7298   // Indent consistently independent of call expression and unary operator.
7299   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
7300                "    dddddddddddddddddddddddddddddd));");
7301   verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
7302                "    dddddddddddddddddddddddddddddd));");
7303   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
7304                "    dddddddddddddddddddddddddddddd));");
7305 
7306   // This test case breaks on an incorrect memoization, i.e. an optimization not
7307   // taking into account the StopAt value.
7308   verifyFormat(
7309       "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
7310       "       aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
7311       "       aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
7312       "       (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7313 
7314   verifyFormat("{\n  {\n    {\n"
7315                "      Annotation.SpaceRequiredBefore =\n"
7316                "          Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
7317                "          Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
7318                "    }\n  }\n}");
7319 
7320   // Break on an outer level if there was a break on an inner level.
7321   EXPECT_EQ("f(g(h(a, // comment\n"
7322             "      b, c),\n"
7323             "    d, e),\n"
7324             "  x, y);",
7325             format("f(g(h(a, // comment\n"
7326                    "    b, c), d, e), x, y);"));
7327 
7328   // Prefer breaking similar line breaks.
7329   verifyFormat(
7330       "const int kTrackingOptions = NSTrackingMouseMoved |\n"
7331       "                             NSTrackingMouseEnteredAndExited |\n"
7332       "                             NSTrackingActiveAlways;");
7333 }
7334 
7335 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) {
7336   FormatStyle NoBinPacking = getGoogleStyle();
7337   NoBinPacking.BinPackParameters = false;
7338   NoBinPacking.BinPackArguments = true;
7339   verifyFormat("void f() {\n"
7340                "  f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n"
7341                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
7342                "}",
7343                NoBinPacking);
7344   verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n"
7345                "       int aaaaaaaaaaaaaaaaaaaa,\n"
7346                "       int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
7347                NoBinPacking);
7348 
7349   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
7350   verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7351                "                        vector<int> bbbbbbbbbbbbbbb);",
7352                NoBinPacking);
7353   // FIXME: This behavior difference is probably not wanted. However, currently
7354   // we cannot distinguish BreakBeforeParameter being set because of the wrapped
7355   // template arguments from BreakBeforeParameter being set because of the
7356   // one-per-line formatting.
7357   verifyFormat(
7358       "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n"
7359       "                                             aaaaaaaaaa> aaaaaaaaaa);",
7360       NoBinPacking);
7361   verifyFormat(
7362       "void fffffffffff(\n"
7363       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n"
7364       "        aaaaaaaaaa);");
7365 }
7366 
7367 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
7368   FormatStyle NoBinPacking = getGoogleStyle();
7369   NoBinPacking.BinPackParameters = false;
7370   NoBinPacking.BinPackArguments = false;
7371   verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
7372                "  aaaaaaaaaaaaaaaaaaaa,\n"
7373                "  aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
7374                NoBinPacking);
7375   verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
7376                "        aaaaaaaaaaaaa,\n"
7377                "        aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
7378                NoBinPacking);
7379   verifyFormat(
7380       "aaaaaaaa(aaaaaaaaaaaaa,\n"
7381       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7382       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
7383       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7384       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
7385       NoBinPacking);
7386   verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
7387                "    .aaaaaaaaaaaaaaaaaa();",
7388                NoBinPacking);
7389   verifyFormat("void f() {\n"
7390                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7391                "      aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n"
7392                "}",
7393                NoBinPacking);
7394 
7395   verifyFormat(
7396       "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7397       "             aaaaaaaaaaaa,\n"
7398       "             aaaaaaaaaaaa);",
7399       NoBinPacking);
7400   verifyFormat(
7401       "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
7402       "                               ddddddddddddddddddddddddddddd),\n"
7403       "             test);",
7404       NoBinPacking);
7405 
7406   verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
7407                "            aaaaaaaaaaaaaaaaaaaaaaa,\n"
7408                "            aaaaaaaaaaaaaaaaaaaaaaa>\n"
7409                "    aaaaaaaaaaaaaaaaaa;",
7410                NoBinPacking);
7411   verifyFormat("a(\"a\"\n"
7412                "  \"a\",\n"
7413                "  a);");
7414 
7415   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
7416   verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
7417                "                aaaaaaaaa,\n"
7418                "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7419                NoBinPacking);
7420   verifyFormat(
7421       "void f() {\n"
7422       "  aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
7423       "      .aaaaaaa();\n"
7424       "}",
7425       NoBinPacking);
7426   verifyFormat(
7427       "template <class SomeType, class SomeOtherType>\n"
7428       "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
7429       NoBinPacking);
7430 }
7431 
7432 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
7433   FormatStyle Style = getLLVMStyleWithColumns(15);
7434   Style.ExperimentalAutoDetectBinPacking = true;
7435   EXPECT_EQ("aaa(aaaa,\n"
7436             "    aaaa,\n"
7437             "    aaaa);\n"
7438             "aaa(aaaa,\n"
7439             "    aaaa,\n"
7440             "    aaaa);",
7441             format("aaa(aaaa,\n" // one-per-line
7442                    "  aaaa,\n"
7443                    "    aaaa  );\n"
7444                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
7445                    Style));
7446   EXPECT_EQ("aaa(aaaa, aaaa,\n"
7447             "    aaaa);\n"
7448             "aaa(aaaa, aaaa,\n"
7449             "    aaaa);",
7450             format("aaa(aaaa,  aaaa,\n" // bin-packed
7451                    "    aaaa  );\n"
7452                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
7453                    Style));
7454 }
7455 
7456 TEST_F(FormatTest, FormatsBuilderPattern) {
7457   verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n"
7458                "    .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
7459                "    .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
7460                "    .StartsWith(\".init\", ORDER_INIT)\n"
7461                "    .StartsWith(\".fini\", ORDER_FINI)\n"
7462                "    .StartsWith(\".hash\", ORDER_HASH)\n"
7463                "    .Default(ORDER_TEXT);\n");
7464 
7465   verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
7466                "       aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
7467   verifyFormat("aaaaaaa->aaaaaaa\n"
7468                "    ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7469                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7470                "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
7471   verifyFormat(
7472       "aaaaaaa->aaaaaaa\n"
7473       "    ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7474       "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
7475   verifyFormat(
7476       "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
7477       "    aaaaaaaaaaaaaa);");
7478   verifyFormat(
7479       "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
7480       "    aaaaaa->aaaaaaaaaaaa()\n"
7481       "        ->aaaaaaaaaaaaaaaa(\n"
7482       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7483       "        ->aaaaaaaaaaaaaaaaa();");
7484   verifyGoogleFormat(
7485       "void f() {\n"
7486       "  someo->Add((new util::filetools::Handler(dir))\n"
7487       "                 ->OnEvent1(NewPermanentCallback(\n"
7488       "                     this, &HandlerHolderClass::EventHandlerCBA))\n"
7489       "                 ->OnEvent2(NewPermanentCallback(\n"
7490       "                     this, &HandlerHolderClass::EventHandlerCBB))\n"
7491       "                 ->OnEvent3(NewPermanentCallback(\n"
7492       "                     this, &HandlerHolderClass::EventHandlerCBC))\n"
7493       "                 ->OnEvent5(NewPermanentCallback(\n"
7494       "                     this, &HandlerHolderClass::EventHandlerCBD))\n"
7495       "                 ->OnEvent6(NewPermanentCallback(\n"
7496       "                     this, &HandlerHolderClass::EventHandlerCBE)));\n"
7497       "}");
7498 
7499   verifyFormat(
7500       "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
7501   verifyFormat("aaaaaaaaaaaaaaa()\n"
7502                "    .aaaaaaaaaaaaaaa()\n"
7503                "    .aaaaaaaaaaaaaaa()\n"
7504                "    .aaaaaaaaaaaaaaa()\n"
7505                "    .aaaaaaaaaaaaaaa();");
7506   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
7507                "    .aaaaaaaaaaaaaaa()\n"
7508                "    .aaaaaaaaaaaaaaa()\n"
7509                "    .aaaaaaaaaaaaaaa();");
7510   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
7511                "    .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
7512                "    .aaaaaaaaaaaaaaa();");
7513   verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
7514                "    ->aaaaaaaaaaaaaae(0)\n"
7515                "    ->aaaaaaaaaaaaaaa();");
7516 
7517   // Don't linewrap after very short segments.
7518   verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7519                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7520                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
7521   verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7522                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7523                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
7524   verifyFormat("aaa()\n"
7525                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7526                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7527                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
7528 
7529   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
7530                "    .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7531                "    .has<bbbbbbbbbbbbbbbbbbbbb>();");
7532   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
7533                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
7534                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
7535 
7536   // Prefer not to break after empty parentheses.
7537   verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
7538                "    First->LastNewlineOffset);");
7539 
7540   // Prefer not to create "hanging" indents.
7541   verifyFormat(
7542       "return !soooooooooooooome_map\n"
7543       "            .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7544       "            .second;");
7545   verifyFormat(
7546       "return aaaaaaaaaaaaaaaa\n"
7547       "    .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n"
7548       "    .aaaa(aaaaaaaaaaaaaa);");
7549   // No hanging indent here.
7550   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n"
7551                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7552   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n"
7553                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7554   verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
7555                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7556                getLLVMStyleWithColumns(60));
7557   verifyFormat("aaaaaaaaaaaaaaaaaa\n"
7558                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
7559                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7560                getLLVMStyleWithColumns(59));
7561   verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7562                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7563                "    .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7564 
7565   // Dont break if only closing statements before member call
7566   verifyFormat("test() {\n"
7567                "  ([]() -> {\n"
7568                "    int b = 32;\n"
7569                "    return 3;\n"
7570                "  }).foo();\n"
7571                "}");
7572   verifyFormat("test() {\n"
7573                "  (\n"
7574                "      []() -> {\n"
7575                "        int b = 32;\n"
7576                "        return 3;\n"
7577                "      },\n"
7578                "      foo, bar)\n"
7579                "      .foo();\n"
7580                "}");
7581   verifyFormat("test() {\n"
7582                "  ([]() -> {\n"
7583                "    int b = 32;\n"
7584                "    return 3;\n"
7585                "  })\n"
7586                "      .foo()\n"
7587                "      .bar();\n"
7588                "}");
7589   verifyFormat("test() {\n"
7590                "  ([]() -> {\n"
7591                "    int b = 32;\n"
7592                "    return 3;\n"
7593                "  })\n"
7594                "      .foo(\"aaaaaaaaaaaaaaaaa\"\n"
7595                "           \"bbbb\");\n"
7596                "}",
7597                getLLVMStyleWithColumns(30));
7598 }
7599 
7600 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
7601   verifyFormat(
7602       "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
7603       "    bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
7604   verifyFormat(
7605       "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n"
7606       "    bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}");
7607 
7608   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
7609                "    ccccccccccccccccccccccccc) {\n}");
7610   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n"
7611                "    ccccccccccccccccccccccccc) {\n}");
7612 
7613   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
7614                "    ccccccccccccccccccccccccc) {\n}");
7615   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n"
7616                "    ccccccccccccccccccccccccc) {\n}");
7617 
7618   verifyFormat(
7619       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
7620       "    ccccccccccccccccccccccccc) {\n}");
7621   verifyFormat(
7622       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n"
7623       "    ccccccccccccccccccccccccc) {\n}");
7624 
7625   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
7626                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
7627                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
7628                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
7629   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n"
7630                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n"
7631                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n"
7632                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
7633 
7634   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
7635                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
7636                "    aaaaaaaaaaaaaaa != aa) {\n}");
7637   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n"
7638                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n"
7639                "    aaaaaaaaaaaaaaa != aa) {\n}");
7640 }
7641 
7642 TEST_F(FormatTest, BreaksAfterAssignments) {
7643   verifyFormat(
7644       "unsigned Cost =\n"
7645       "    TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
7646       "                        SI->getPointerAddressSpaceee());\n");
7647   verifyFormat(
7648       "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
7649       "    Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
7650 
7651   verifyFormat(
7652       "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
7653       "    aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
7654   verifyFormat("unsigned OriginalStartColumn =\n"
7655                "    SourceMgr.getSpellingColumnNumber(\n"
7656                "        Current.FormatTok.getStartOfNonWhitespace()) -\n"
7657                "    1;");
7658 }
7659 
7660 TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) {
7661   FormatStyle Style = getLLVMStyle();
7662   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
7663                "    bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;",
7664                Style);
7665 
7666   Style.PenaltyBreakAssignment = 20;
7667   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
7668                "                                 cccccccccccccccccccccccccc;",
7669                Style);
7670 }
7671 
7672 TEST_F(FormatTest, AlignsAfterAssignments) {
7673   verifyFormat(
7674       "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7675       "             aaaaaaaaaaaaaaaaaaaaaaaaa;");
7676   verifyFormat(
7677       "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7678       "          aaaaaaaaaaaaaaaaaaaaaaaaa;");
7679   verifyFormat(
7680       "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7681       "           aaaaaaaaaaaaaaaaaaaaaaaaa;");
7682   verifyFormat(
7683       "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7684       "              aaaaaaaaaaaaaaaaaaaaaaaaa);");
7685   verifyFormat(
7686       "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
7687       "                                            aaaaaaaaaaaaaaaaaaaaaaaa +\n"
7688       "                                            aaaaaaaaaaaaaaaaaaaaaaaa;");
7689 }
7690 
7691 TEST_F(FormatTest, AlignsAfterReturn) {
7692   verifyFormat(
7693       "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7694       "       aaaaaaaaaaaaaaaaaaaaaaaaa;");
7695   verifyFormat(
7696       "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7697       "        aaaaaaaaaaaaaaaaaaaaaaaaa);");
7698   verifyFormat(
7699       "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
7700       "       aaaaaaaaaaaaaaaaaaaaaa();");
7701   verifyFormat(
7702       "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
7703       "        aaaaaaaaaaaaaaaaaaaaaa());");
7704   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7705                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7706   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7707                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
7708                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
7709   verifyFormat("return\n"
7710                "    // true if code is one of a or b.\n"
7711                "    code == a || code == b;");
7712 }
7713 
7714 TEST_F(FormatTest, AlignsAfterOpenBracket) {
7715   verifyFormat(
7716       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
7717       "                                                aaaaaaaaa aaaaaaa) {}");
7718   verifyFormat(
7719       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
7720       "                                               aaaaaaaaaaa aaaaaaaaa);");
7721   verifyFormat(
7722       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
7723       "                                             aaaaaaaaaaaaaaaaaaaaa));");
7724   FormatStyle Style = getLLVMStyle();
7725   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
7726   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7727                "    aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}",
7728                Style);
7729   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
7730                "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);",
7731                Style);
7732   verifyFormat("SomeLongVariableName->someFunction(\n"
7733                "    foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));",
7734                Style);
7735   verifyFormat(
7736       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
7737       "    aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
7738       Style);
7739   verifyFormat(
7740       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
7741       "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7742       Style);
7743   verifyFormat(
7744       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
7745       "    aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
7746       Style);
7747 
7748   verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n"
7749                "    ccccccc(aaaaaaaaaaaaaaaaa,         //\n"
7750                "        b));",
7751                Style);
7752 
7753   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
7754   Style.BinPackArguments = false;
7755   Style.BinPackParameters = false;
7756   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7757                "    aaaaaaaaaaa aaaaaaaa,\n"
7758                "    aaaaaaaaa aaaaaaa,\n"
7759                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
7760                Style);
7761   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
7762                "    aaaaaaaaaaa aaaaaaaaa,\n"
7763                "    aaaaaaaaaaa aaaaaaaaa,\n"
7764                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7765                Style);
7766   verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n"
7767                "    aaaaaaaaaaaaaaa,\n"
7768                "    aaaaaaaaaaaaaaaaaaaaa,\n"
7769                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
7770                Style);
7771   verifyFormat(
7772       "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n"
7773       "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
7774       Style);
7775   verifyFormat(
7776       "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n"
7777       "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
7778       Style);
7779   verifyFormat(
7780       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
7781       "    aaaaaaaaaaaaaaaaaaaaa(\n"
7782       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n"
7783       "    aaaaaaaaaaaaaaaa);",
7784       Style);
7785   verifyFormat(
7786       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
7787       "    aaaaaaaaaaaaaaaaaaaaa(\n"
7788       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n"
7789       "    aaaaaaaaaaaaaaaa);",
7790       Style);
7791 }
7792 
7793 TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
7794   FormatStyle Style = getLLVMStyleWithColumns(40);
7795   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
7796                "          bbbbbbbbbbbbbbbbbbbbbb);",
7797                Style);
7798   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
7799   Style.AlignOperands = FormatStyle::OAS_DontAlign;
7800   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
7801                "          bbbbbbbbbbbbbbbbbbbbbb);",
7802                Style);
7803   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
7804   Style.AlignOperands = FormatStyle::OAS_Align;
7805   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
7806                "          bbbbbbbbbbbbbbbbbbbbbb);",
7807                Style);
7808   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
7809   Style.AlignOperands = FormatStyle::OAS_DontAlign;
7810   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
7811                "    bbbbbbbbbbbbbbbbbbbbbb);",
7812                Style);
7813 }
7814 
7815 TEST_F(FormatTest, BreaksConditionalExpressions) {
7816   verifyFormat(
7817       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7818       "                               ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7819       "                               : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7820   verifyFormat(
7821       "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
7822       "     aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7823       "                                : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7824   verifyFormat(
7825       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7826       "                                   : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7827   verifyFormat("aaaa(aaaaaaaaa, aaaaaaaaa,\n"
7828                "     aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7829                "             : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7830   verifyFormat(
7831       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
7832       "                                                    : aaaaaaaaaaaaa);");
7833   verifyFormat(
7834       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7835       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7836       "                                    : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7837       "                   aaaaaaaaaaaaa);");
7838   verifyFormat(
7839       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7840       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7841       "                   aaaaaaaaaaaaa);");
7842   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7843                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7844                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7845                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7846                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7847   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7848                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7849                "           ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7850                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7851                "           : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7852                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7853                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7854   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7855                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7856                "           ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7857                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7858                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7859   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7860                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7861                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
7862   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
7863                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7864                "        ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7865                "        : aaaaaaaaaaaaaaaa;");
7866   verifyFormat(
7867       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7868       "    ? aaaaaaaaaaaaaaa\n"
7869       "    : aaaaaaaaaaaaaaa;");
7870   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
7871                "          aaaaaaaaa\n"
7872                "      ? b\n"
7873                "      : c);");
7874   verifyFormat("return aaaa == bbbb\n"
7875                "           // comment\n"
7876                "           ? aaaa\n"
7877                "           : bbbb;");
7878   verifyFormat("unsigned Indent =\n"
7879                "    format(TheLine.First,\n"
7880                "           IndentForLevel[TheLine.Level] >= 0\n"
7881                "               ? IndentForLevel[TheLine.Level]\n"
7882                "               : TheLine * 2,\n"
7883                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
7884                getLLVMStyleWithColumns(60));
7885   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
7886                "                  ? aaaaaaaaaaaaaaa\n"
7887                "                  : bbbbbbbbbbbbbbb //\n"
7888                "                        ? ccccccccccccccc\n"
7889                "                        : ddddddddddddddd;");
7890   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
7891                "                  ? aaaaaaaaaaaaaaa\n"
7892                "                  : (bbbbbbbbbbbbbbb //\n"
7893                "                         ? ccccccccccccccc\n"
7894                "                         : ddddddddddddddd);");
7895   verifyFormat(
7896       "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7897       "                                      ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7898       "                                            aaaaaaaaaaaaaaaaaaaaa +\n"
7899       "                                            aaaaaaaaaaaaaaaaaaaaa\n"
7900       "                                      : aaaaaaaaaa;");
7901   verifyFormat(
7902       "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7903       "                                   : aaaaaaaaaaaaaaaaaaaaaa\n"
7904       "                      : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
7905 
7906   FormatStyle NoBinPacking = getLLVMStyle();
7907   NoBinPacking.BinPackArguments = false;
7908   verifyFormat(
7909       "void f() {\n"
7910       "  g(aaa,\n"
7911       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
7912       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7913       "        ? aaaaaaaaaaaaaaa\n"
7914       "        : aaaaaaaaaaaaaaa);\n"
7915       "}",
7916       NoBinPacking);
7917   verifyFormat(
7918       "void f() {\n"
7919       "  g(aaa,\n"
7920       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
7921       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7922       "        ?: aaaaaaaaaaaaaaa);\n"
7923       "}",
7924       NoBinPacking);
7925 
7926   verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n"
7927                "             // comment.\n"
7928                "             ccccccccccccccccccccccccccccccccccccccc\n"
7929                "                 ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7930                "                 : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);");
7931 
7932   // Assignments in conditional expressions. Apparently not uncommon :-(.
7933   verifyFormat("return a != b\n"
7934                "           // comment\n"
7935                "           ? a = b\n"
7936                "           : a = b;");
7937   verifyFormat("return a != b\n"
7938                "           // comment\n"
7939                "           ? a = a != b\n"
7940                "                     // comment\n"
7941                "                     ? a = b\n"
7942                "                     : a\n"
7943                "           : a;\n");
7944   verifyFormat("return a != b\n"
7945                "           // comment\n"
7946                "           ? a\n"
7947                "           : a = a != b\n"
7948                "                     // comment\n"
7949                "                     ? a = b\n"
7950                "                     : a;");
7951 
7952   // Chained conditionals
7953   FormatStyle Style = getLLVMStyleWithColumns(70);
7954   Style.AlignOperands = FormatStyle::OAS_Align;
7955   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7956                "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7957                "                        : 3333333333333333;",
7958                Style);
7959   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7960                "       : bbbbbbbbbb     ? 2222222222222222\n"
7961                "                        : 3333333333333333;",
7962                Style);
7963   verifyFormat("return aaaaaaaaaa         ? 1111111111111111\n"
7964                "       : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
7965                "                          : 3333333333333333;",
7966                Style);
7967   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7968                "       : bbbbbbbbbbbbbb ? 222222\n"
7969                "                        : 333333;",
7970                Style);
7971   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7972                "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7973                "       : cccccccccccccc ? 3333333333333333\n"
7974                "                        : 4444444444444444;",
7975                Style);
7976   verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc)\n"
7977                "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7978                "                        : 3333333333333333;",
7979                Style);
7980   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7981                "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7982                "                        : (aaa ? bbb : ccc);",
7983                Style);
7984   verifyFormat(
7985       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7986       "                                             : cccccccccccccccccc)\n"
7987       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7988       "                        : 3333333333333333;",
7989       Style);
7990   verifyFormat(
7991       "return aaaaaaaaa        ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7992       "                                             : cccccccccccccccccc)\n"
7993       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7994       "                        : 3333333333333333;",
7995       Style);
7996   verifyFormat(
7997       "return aaaaaaaaa        ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7998       "                                             : dddddddddddddddddd)\n"
7999       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
8000       "                        : 3333333333333333;",
8001       Style);
8002   verifyFormat(
8003       "return aaaaaaaaa        ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
8004       "                                             : dddddddddddddddddd)\n"
8005       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
8006       "                        : 3333333333333333;",
8007       Style);
8008   verifyFormat(
8009       "return aaaaaaaaa        ? 1111111111111111\n"
8010       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
8011       "                        : a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
8012       "                                             : dddddddddddddddddd)\n",
8013       Style);
8014   verifyFormat(
8015       "return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
8016       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
8017       "                        : (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
8018       "                                             : cccccccccccccccccc);",
8019       Style);
8020   verifyFormat(
8021       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
8022       "                           : ccccccccccccccc ? dddddddddddddddddd\n"
8023       "                                             : eeeeeeeeeeeeeeeeee)\n"
8024       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
8025       "                        : 3333333333333333;",
8026       Style);
8027   verifyFormat(
8028       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa    ? bbbbbbbbbbbbbbbbbb\n"
8029       "                           : ccccccccccccccc ? dddddddddddddddddd\n"
8030       "                                             : eeeeeeeeeeeeeeeeee)\n"
8031       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
8032       "                        : 3333333333333333;",
8033       Style);
8034   verifyFormat(
8035       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
8036       "                           : cccccccccccc    ? dddddddddddddddddd\n"
8037       "                                             : eeeeeeeeeeeeeeeeee)\n"
8038       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
8039       "                        : 3333333333333333;",
8040       Style);
8041   verifyFormat(
8042       "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
8043       "                                             : cccccccccccccccccc\n"
8044       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
8045       "                        : 3333333333333333;",
8046       Style);
8047   verifyFormat(
8048       "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
8049       "                          : cccccccccccccccc ? dddddddddddddddddd\n"
8050       "                                             : eeeeeeeeeeeeeeeeee\n"
8051       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
8052       "                        : 3333333333333333;",
8053       Style);
8054   verifyFormat("return aaaaaaaaaaaaaaaaaaaaa\n"
8055                "           ? (aaaaaaaaaaaaaaaaaa   ? bbbbbbbbbbbbbbbbbb\n"
8056                "              : cccccccccccccccccc ? dddddddddddddddddd\n"
8057                "                                   : eeeeeeeeeeeeeeeeee)\n"
8058                "       : bbbbbbbbbbbbbbbbbbb ? 2222222222222222\n"
8059                "                             : 3333333333333333;",
8060                Style);
8061   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaa\n"
8062                "           ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
8063                "             : cccccccccccccccc ? dddddddddddddddddd\n"
8064                "                                : eeeeeeeeeeeeeeeeee\n"
8065                "       : bbbbbbbbbbbbbbbbbbbbbbb ? 2222222222222222\n"
8066                "                                 : 3333333333333333;",
8067                Style);
8068 
8069   Style.AlignOperands = FormatStyle::OAS_DontAlign;
8070   Style.BreakBeforeTernaryOperators = false;
8071   // FIXME: Aligning the question marks is weird given DontAlign.
8072   // Consider disabling this alignment in this case. Also check whether this
8073   // will render the adjustment from https://reviews.llvm.org/D82199
8074   // unnecessary.
8075   verifyFormat("int x = aaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa :\n"
8076                "    bbbb                ? cccccccccccccccccc :\n"
8077                "                          ddddd;\n",
8078                Style);
8079 
8080   EXPECT_EQ(
8081       "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n"
8082       "    /*\n"
8083       "     */\n"
8084       "    function() {\n"
8085       "      try {\n"
8086       "        return JJJJJJJJJJJJJJ(\n"
8087       "            pppppppppppppppppppppppppppppppppppppppppppppppppp);\n"
8088       "      }\n"
8089       "    } :\n"
8090       "    function() {};",
8091       format(
8092           "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n"
8093           "     /*\n"
8094           "      */\n"
8095           "     function() {\n"
8096           "      try {\n"
8097           "        return JJJJJJJJJJJJJJ(\n"
8098           "            pppppppppppppppppppppppppppppppppppppppppppppppppp);\n"
8099           "      }\n"
8100           "    } :\n"
8101           "    function() {};",
8102           getGoogleStyle(FormatStyle::LK_JavaScript)));
8103 }
8104 
8105 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
8106   FormatStyle Style = getLLVMStyleWithColumns(70);
8107   Style.BreakBeforeTernaryOperators = false;
8108   verifyFormat(
8109       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
8110       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
8111       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
8112       Style);
8113   verifyFormat(
8114       "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
8115       "     aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
8116       "                                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
8117       Style);
8118   verifyFormat(
8119       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
8120       "                                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
8121       Style);
8122   verifyFormat("aaaa(aaaaaaaa, aaaaaaaaaa,\n"
8123                "     aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
8124                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
8125                Style);
8126   verifyFormat(
8127       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
8128       "                                                      aaaaaaaaaaaaa);",
8129       Style);
8130   verifyFormat(
8131       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8132       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
8133       "                                      aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8134       "                   aaaaaaaaaaaaa);",
8135       Style);
8136   verifyFormat(
8137       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8138       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8139       "                   aaaaaaaaaaaaa);",
8140       Style);
8141   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
8142                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8143                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
8144                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8145                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
8146                Style);
8147   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8148                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
8149                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8150                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
8151                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8152                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
8153                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
8154                Style);
8155   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8156                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n"
8157                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8158                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
8159                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
8160                Style);
8161   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
8162                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
8163                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
8164                Style);
8165   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
8166                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
8167                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
8168                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
8169                Style);
8170   verifyFormat(
8171       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
8172       "    aaaaaaaaaaaaaaa :\n"
8173       "    aaaaaaaaaaaaaaa;",
8174       Style);
8175   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
8176                "          aaaaaaaaa ?\n"
8177                "      b :\n"
8178                "      c);",
8179                Style);
8180   verifyFormat("unsigned Indent =\n"
8181                "    format(TheLine.First,\n"
8182                "           IndentForLevel[TheLine.Level] >= 0 ?\n"
8183                "               IndentForLevel[TheLine.Level] :\n"
8184                "               TheLine * 2,\n"
8185                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
8186                Style);
8187   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
8188                "                  aaaaaaaaaaaaaaa :\n"
8189                "                  bbbbbbbbbbbbbbb ? //\n"
8190                "                      ccccccccccccccc :\n"
8191                "                      ddddddddddddddd;",
8192                Style);
8193   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
8194                "                  aaaaaaaaaaaaaaa :\n"
8195                "                  (bbbbbbbbbbbbbbb ? //\n"
8196                "                       ccccccccccccccc :\n"
8197                "                       ddddddddddddddd);",
8198                Style);
8199   verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
8200                "            /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n"
8201                "            ccccccccccccccccccccccccccc;",
8202                Style);
8203   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
8204                "           aaaaa :\n"
8205                "           bbbbbbbbbbbbbbb + cccccccccccccccc;",
8206                Style);
8207 
8208   // Chained conditionals
8209   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
8210                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8211                "                          3333333333333333;",
8212                Style);
8213   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
8214                "       bbbbbbbbbb       ? 2222222222222222 :\n"
8215                "                          3333333333333333;",
8216                Style);
8217   verifyFormat("return aaaaaaaaaa       ? 1111111111111111 :\n"
8218                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8219                "                          3333333333333333;",
8220                Style);
8221   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
8222                "       bbbbbbbbbbbbbbbb ? 222222 :\n"
8223                "                          333333;",
8224                Style);
8225   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
8226                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8227                "       cccccccccccccccc ? 3333333333333333 :\n"
8228                "                          4444444444444444;",
8229                Style);
8230   verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc) :\n"
8231                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8232                "                          3333333333333333;",
8233                Style);
8234   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
8235                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8236                "                          (aaa ? bbb : ccc);",
8237                Style);
8238   verifyFormat(
8239       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8240       "                                               cccccccccccccccccc) :\n"
8241       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8242       "                          3333333333333333;",
8243       Style);
8244   verifyFormat(
8245       "return aaaaaaaaa        ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8246       "                                               cccccccccccccccccc) :\n"
8247       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8248       "                          3333333333333333;",
8249       Style);
8250   verifyFormat(
8251       "return aaaaaaaaa        ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8252       "                                               dddddddddddddddddd) :\n"
8253       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8254       "                          3333333333333333;",
8255       Style);
8256   verifyFormat(
8257       "return aaaaaaaaa        ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8258       "                                               dddddddddddddddddd) :\n"
8259       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8260       "                          3333333333333333;",
8261       Style);
8262   verifyFormat(
8263       "return aaaaaaaaa        ? 1111111111111111 :\n"
8264       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8265       "                          a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8266       "                                               dddddddddddddddddd)\n",
8267       Style);
8268   verifyFormat(
8269       "return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
8270       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8271       "                          (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8272       "                                               cccccccccccccccccc);",
8273       Style);
8274   verifyFormat(
8275       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8276       "                           ccccccccccccccccc ? dddddddddddddddddd :\n"
8277       "                                               eeeeeeeeeeeeeeeeee) :\n"
8278       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8279       "                          3333333333333333;",
8280       Style);
8281   verifyFormat(
8282       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8283       "                           ccccccccccccc     ? dddddddddddddddddd :\n"
8284       "                                               eeeeeeeeeeeeeeeeee) :\n"
8285       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8286       "                          3333333333333333;",
8287       Style);
8288   verifyFormat(
8289       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaa     ? bbbbbbbbbbbbbbbbbb :\n"
8290       "                           ccccccccccccccccc ? dddddddddddddddddd :\n"
8291       "                                               eeeeeeeeeeeeeeeeee) :\n"
8292       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8293       "                          3333333333333333;",
8294       Style);
8295   verifyFormat(
8296       "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8297       "                                               cccccccccccccccccc :\n"
8298       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8299       "                          3333333333333333;",
8300       Style);
8301   verifyFormat(
8302       "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8303       "                          cccccccccccccccccc ? dddddddddddddddddd :\n"
8304       "                                               eeeeeeeeeeeeeeeeee :\n"
8305       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8306       "                          3333333333333333;",
8307       Style);
8308   verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n"
8309                "           (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8310                "            cccccccccccccccccc ? dddddddddddddddddd :\n"
8311                "                                 eeeeeeeeeeeeeeeeee) :\n"
8312                "       bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8313                "                               3333333333333333;",
8314                Style);
8315   verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n"
8316                "           aaaaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8317                "           cccccccccccccccccccc ? dddddddddddddddddd :\n"
8318                "                                  eeeeeeeeeeeeeeeeee :\n"
8319                "       bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8320                "                               3333333333333333;",
8321                Style);
8322 }
8323 
8324 TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
8325   verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
8326                "     aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
8327   verifyFormat("bool a = true, b = false;");
8328 
8329   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
8330                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
8331                "     bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
8332                "         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
8333   verifyFormat(
8334       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
8335       "         bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
8336       "     d = e && f;");
8337   verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
8338                "          c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
8339   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
8340                "          *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
8341   verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
8342                "          ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
8343 
8344   FormatStyle Style = getGoogleStyle();
8345   Style.PointerAlignment = FormatStyle::PAS_Left;
8346   Style.DerivePointerAlignment = false;
8347   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8348                "    *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
8349                "    *b = bbbbbbbbbbbbbbbbbbb;",
8350                Style);
8351   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
8352                "          *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;",
8353                Style);
8354   verifyFormat("vector<int*> a, b;", Style);
8355   verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style);
8356 }
8357 
8358 TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
8359   verifyFormat("arr[foo ? bar : baz];");
8360   verifyFormat("f()[foo ? bar : baz];");
8361   verifyFormat("(a + b)[foo ? bar : baz];");
8362   verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
8363 }
8364 
8365 TEST_F(FormatTest, AlignsStringLiterals) {
8366   verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
8367                "                                      \"short literal\");");
8368   verifyFormat(
8369       "looooooooooooooooooooooooongFunction(\n"
8370       "    \"short literal\"\n"
8371       "    \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
8372   verifyFormat("someFunction(\"Always break between multi-line\"\n"
8373                "             \" string literals\",\n"
8374                "             and, other, parameters);");
8375   EXPECT_EQ("fun + \"1243\" /* comment */\n"
8376             "      \"5678\";",
8377             format("fun + \"1243\" /* comment */\n"
8378                    "    \"5678\";",
8379                    getLLVMStyleWithColumns(28)));
8380   EXPECT_EQ(
8381       "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
8382       "         \"aaaaaaaaaaaaaaaaaaaaa\"\n"
8383       "         \"aaaaaaaaaaaaaaaa\";",
8384       format("aaaaaa ="
8385              "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
8386              "aaaaaaaaaaaaaaaaaaaaa\" "
8387              "\"aaaaaaaaaaaaaaaa\";"));
8388   verifyFormat("a = a + \"a\"\n"
8389                "        \"a\"\n"
8390                "        \"a\";");
8391   verifyFormat("f(\"a\", \"b\"\n"
8392                "       \"c\");");
8393 
8394   verifyFormat(
8395       "#define LL_FORMAT \"ll\"\n"
8396       "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
8397       "       \"d, ddddddddd: %\" LL_FORMAT \"d\");");
8398 
8399   verifyFormat("#define A(X)          \\\n"
8400                "  \"aaaaa\" #X \"bbbbbb\" \\\n"
8401                "  \"ccccc\"",
8402                getLLVMStyleWithColumns(23));
8403   verifyFormat("#define A \"def\"\n"
8404                "f(\"abc\" A \"ghi\"\n"
8405                "  \"jkl\");");
8406 
8407   verifyFormat("f(L\"a\"\n"
8408                "  L\"b\");");
8409   verifyFormat("#define A(X)            \\\n"
8410                "  L\"aaaaa\" #X L\"bbbbbb\" \\\n"
8411                "  L\"ccccc\"",
8412                getLLVMStyleWithColumns(25));
8413 
8414   verifyFormat("f(@\"a\"\n"
8415                "  @\"b\");");
8416   verifyFormat("NSString s = @\"a\"\n"
8417                "             @\"b\"\n"
8418                "             @\"c\";");
8419   verifyFormat("NSString s = @\"a\"\n"
8420                "              \"b\"\n"
8421                "              \"c\";");
8422 }
8423 
8424 TEST_F(FormatTest, ReturnTypeBreakingStyle) {
8425   FormatStyle Style = getLLVMStyle();
8426   // No declarations or definitions should be moved to own line.
8427   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None;
8428   verifyFormat("class A {\n"
8429                "  int f() { return 1; }\n"
8430                "  int g();\n"
8431                "};\n"
8432                "int f() { return 1; }\n"
8433                "int g();\n",
8434                Style);
8435 
8436   // All declarations and definitions should have the return type moved to its
8437   // own line.
8438   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
8439   Style.TypenameMacros = {"LIST"};
8440   verifyFormat("SomeType\n"
8441                "funcdecl(LIST(uint64_t));",
8442                Style);
8443   verifyFormat("class E {\n"
8444                "  int\n"
8445                "  f() {\n"
8446                "    return 1;\n"
8447                "  }\n"
8448                "  int\n"
8449                "  g();\n"
8450                "};\n"
8451                "int\n"
8452                "f() {\n"
8453                "  return 1;\n"
8454                "}\n"
8455                "int\n"
8456                "g();\n",
8457                Style);
8458 
8459   // Top-level definitions, and no kinds of declarations should have the
8460   // return type moved to its own line.
8461   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions;
8462   verifyFormat("class B {\n"
8463                "  int f() { return 1; }\n"
8464                "  int g();\n"
8465                "};\n"
8466                "int\n"
8467                "f() {\n"
8468                "  return 1;\n"
8469                "}\n"
8470                "int g();\n",
8471                Style);
8472 
8473   // Top-level definitions and declarations should have the return type moved
8474   // to its own line.
8475   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel;
8476   verifyFormat("class C {\n"
8477                "  int f() { return 1; }\n"
8478                "  int g();\n"
8479                "};\n"
8480                "int\n"
8481                "f() {\n"
8482                "  return 1;\n"
8483                "}\n"
8484                "int\n"
8485                "g();\n",
8486                Style);
8487 
8488   // All definitions should have the return type moved to its own line, but no
8489   // kinds of declarations.
8490   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
8491   verifyFormat("class D {\n"
8492                "  int\n"
8493                "  f() {\n"
8494                "    return 1;\n"
8495                "  }\n"
8496                "  int g();\n"
8497                "};\n"
8498                "int\n"
8499                "f() {\n"
8500                "  return 1;\n"
8501                "}\n"
8502                "int g();\n",
8503                Style);
8504   verifyFormat("const char *\n"
8505                "f(void) {\n" // Break here.
8506                "  return \"\";\n"
8507                "}\n"
8508                "const char *bar(void);\n", // No break here.
8509                Style);
8510   verifyFormat("template <class T>\n"
8511                "T *\n"
8512                "f(T &c) {\n" // Break here.
8513                "  return NULL;\n"
8514                "}\n"
8515                "template <class T> T *f(T &c);\n", // No break here.
8516                Style);
8517   verifyFormat("class C {\n"
8518                "  int\n"
8519                "  operator+() {\n"
8520                "    return 1;\n"
8521                "  }\n"
8522                "  int\n"
8523                "  operator()() {\n"
8524                "    return 1;\n"
8525                "  }\n"
8526                "};\n",
8527                Style);
8528   verifyFormat("void\n"
8529                "A::operator()() {}\n"
8530                "void\n"
8531                "A::operator>>() {}\n"
8532                "void\n"
8533                "A::operator+() {}\n"
8534                "void\n"
8535                "A::operator*() {}\n"
8536                "void\n"
8537                "A::operator->() {}\n"
8538                "void\n"
8539                "A::operator void *() {}\n"
8540                "void\n"
8541                "A::operator void &() {}\n"
8542                "void\n"
8543                "A::operator void &&() {}\n"
8544                "void\n"
8545                "A::operator char *() {}\n"
8546                "void\n"
8547                "A::operator[]() {}\n"
8548                "void\n"
8549                "A::operator!() {}\n"
8550                "void\n"
8551                "A::operator**() {}\n"
8552                "void\n"
8553                "A::operator<Foo> *() {}\n"
8554                "void\n"
8555                "A::operator<Foo> **() {}\n"
8556                "void\n"
8557                "A::operator<Foo> &() {}\n"
8558                "void\n"
8559                "A::operator void **() {}\n",
8560                Style);
8561   verifyFormat("constexpr auto\n"
8562                "operator()() const -> reference {}\n"
8563                "constexpr auto\n"
8564                "operator>>() const -> reference {}\n"
8565                "constexpr auto\n"
8566                "operator+() const -> reference {}\n"
8567                "constexpr auto\n"
8568                "operator*() const -> reference {}\n"
8569                "constexpr auto\n"
8570                "operator->() const -> reference {}\n"
8571                "constexpr auto\n"
8572                "operator++() const -> reference {}\n"
8573                "constexpr auto\n"
8574                "operator void *() const -> reference {}\n"
8575                "constexpr auto\n"
8576                "operator void **() const -> reference {}\n"
8577                "constexpr auto\n"
8578                "operator void *() const -> reference {}\n"
8579                "constexpr auto\n"
8580                "operator void &() const -> reference {}\n"
8581                "constexpr auto\n"
8582                "operator void &&() const -> reference {}\n"
8583                "constexpr auto\n"
8584                "operator char *() const -> reference {}\n"
8585                "constexpr auto\n"
8586                "operator!() const -> reference {}\n"
8587                "constexpr auto\n"
8588                "operator[]() const -> reference {}\n",
8589                Style);
8590   verifyFormat("void *operator new(std::size_t s);", // No break here.
8591                Style);
8592   verifyFormat("void *\n"
8593                "operator new(std::size_t s) {}",
8594                Style);
8595   verifyFormat("void *\n"
8596                "operator delete[](void *ptr) {}",
8597                Style);
8598   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
8599   verifyFormat("const char *\n"
8600                "f(void)\n" // Break here.
8601                "{\n"
8602                "  return \"\";\n"
8603                "}\n"
8604                "const char *bar(void);\n", // No break here.
8605                Style);
8606   verifyFormat("template <class T>\n"
8607                "T *\n"     // Problem here: no line break
8608                "f(T &c)\n" // Break here.
8609                "{\n"
8610                "  return NULL;\n"
8611                "}\n"
8612                "template <class T> T *f(T &c);\n", // No break here.
8613                Style);
8614   verifyFormat("int\n"
8615                "foo(A<bool> a)\n"
8616                "{\n"
8617                "  return a;\n"
8618                "}\n",
8619                Style);
8620   verifyFormat("int\n"
8621                "foo(A<8> a)\n"
8622                "{\n"
8623                "  return a;\n"
8624                "}\n",
8625                Style);
8626   verifyFormat("int\n"
8627                "foo(A<B<bool>, 8> a)\n"
8628                "{\n"
8629                "  return a;\n"
8630                "}\n",
8631                Style);
8632   verifyFormat("int\n"
8633                "foo(A<B<8>, bool> a)\n"
8634                "{\n"
8635                "  return a;\n"
8636                "}\n",
8637                Style);
8638   verifyFormat("int\n"
8639                "foo(A<B<bool>, bool> a)\n"
8640                "{\n"
8641                "  return a;\n"
8642                "}\n",
8643                Style);
8644   verifyFormat("int\n"
8645                "foo(A<B<8>, 8> a)\n"
8646                "{\n"
8647                "  return a;\n"
8648                "}\n",
8649                Style);
8650 
8651   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
8652   Style.BraceWrapping.AfterFunction = true;
8653   verifyFormat("int f(i);\n" // No break here.
8654                "int\n"       // Break here.
8655                "f(i)\n"
8656                "{\n"
8657                "  return i + 1;\n"
8658                "}\n"
8659                "int\n" // Break here.
8660                "f(i)\n"
8661                "{\n"
8662                "  return i + 1;\n"
8663                "};",
8664                Style);
8665   verifyFormat("int f(a, b, c);\n" // No break here.
8666                "int\n"             // Break here.
8667                "f(a, b, c)\n"      // Break here.
8668                "short a, b;\n"
8669                "float c;\n"
8670                "{\n"
8671                "  return a + b < c;\n"
8672                "}\n"
8673                "int\n"        // Break here.
8674                "f(a, b, c)\n" // Break here.
8675                "short a, b;\n"
8676                "float c;\n"
8677                "{\n"
8678                "  return a + b < c;\n"
8679                "};",
8680                Style);
8681   verifyFormat("byte *\n" // Break here.
8682                "f(a)\n"   // Break here.
8683                "byte a[];\n"
8684                "{\n"
8685                "  return a;\n"
8686                "}",
8687                Style);
8688   verifyFormat("bool f(int a, int) override;\n"
8689                "Bar g(int a, Bar) final;\n"
8690                "Bar h(a, Bar) final;",
8691                Style);
8692   verifyFormat("int\n"
8693                "f(a)",
8694                Style);
8695   verifyFormat("bool\n"
8696                "f(size_t = 0, bool b = false)\n"
8697                "{\n"
8698                "  return !b;\n"
8699                "}",
8700                Style);
8701 
8702   // The return breaking style doesn't affect:
8703   // * function and object definitions with attribute-like macros
8704   verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n"
8705                "    ABSL_GUARDED_BY(mutex) = {};",
8706                getGoogleStyleWithColumns(40));
8707   verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n"
8708                "    ABSL_GUARDED_BY(mutex);  // comment",
8709                getGoogleStyleWithColumns(40));
8710   verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n"
8711                "    ABSL_GUARDED_BY(mutex1)\n"
8712                "        ABSL_GUARDED_BY(mutex2);",
8713                getGoogleStyleWithColumns(40));
8714   verifyFormat("Tttttt f(int a, int b)\n"
8715                "    ABSL_GUARDED_BY(mutex1)\n"
8716                "        ABSL_GUARDED_BY(mutex2);",
8717                getGoogleStyleWithColumns(40));
8718   // * typedefs
8719   verifyFormat("typedef ATTR(X) char x;", getGoogleStyle());
8720 
8721   Style = getGNUStyle();
8722 
8723   // Test for comments at the end of function declarations.
8724   verifyFormat("void\n"
8725                "foo (int a, /*abc*/ int b) // def\n"
8726                "{\n"
8727                "}\n",
8728                Style);
8729 
8730   verifyFormat("void\n"
8731                "foo (int a, /* abc */ int b) /* def */\n"
8732                "{\n"
8733                "}\n",
8734                Style);
8735 
8736   // Definitions that should not break after return type
8737   verifyFormat("void foo (int a, int b); // def\n", Style);
8738   verifyFormat("void foo (int a, int b); /* def */\n", Style);
8739   verifyFormat("void foo (int a, int b);\n", Style);
8740 }
8741 
8742 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
8743   FormatStyle NoBreak = getLLVMStyle();
8744   NoBreak.AlwaysBreakBeforeMultilineStrings = false;
8745   FormatStyle Break = getLLVMStyle();
8746   Break.AlwaysBreakBeforeMultilineStrings = true;
8747   verifyFormat("aaaa = \"bbbb\"\n"
8748                "       \"cccc\";",
8749                NoBreak);
8750   verifyFormat("aaaa =\n"
8751                "    \"bbbb\"\n"
8752                "    \"cccc\";",
8753                Break);
8754   verifyFormat("aaaa(\"bbbb\"\n"
8755                "     \"cccc\");",
8756                NoBreak);
8757   verifyFormat("aaaa(\n"
8758                "    \"bbbb\"\n"
8759                "    \"cccc\");",
8760                Break);
8761   verifyFormat("aaaa(qqq, \"bbbb\"\n"
8762                "          \"cccc\");",
8763                NoBreak);
8764   verifyFormat("aaaa(qqq,\n"
8765                "     \"bbbb\"\n"
8766                "     \"cccc\");",
8767                Break);
8768   verifyFormat("aaaa(qqq,\n"
8769                "     L\"bbbb\"\n"
8770                "     L\"cccc\");",
8771                Break);
8772   verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n"
8773                "                      \"bbbb\"));",
8774                Break);
8775   verifyFormat("string s = someFunction(\n"
8776                "    \"abc\"\n"
8777                "    \"abc\");",
8778                Break);
8779 
8780   // As we break before unary operators, breaking right after them is bad.
8781   verifyFormat("string foo = abc ? \"x\"\n"
8782                "                   \"blah blah blah blah blah blah\"\n"
8783                "                 : \"y\";",
8784                Break);
8785 
8786   // Don't break if there is no column gain.
8787   verifyFormat("f(\"aaaa\"\n"
8788                "  \"bbbb\");",
8789                Break);
8790 
8791   // Treat literals with escaped newlines like multi-line string literals.
8792   EXPECT_EQ("x = \"a\\\n"
8793             "b\\\n"
8794             "c\";",
8795             format("x = \"a\\\n"
8796                    "b\\\n"
8797                    "c\";",
8798                    NoBreak));
8799   EXPECT_EQ("xxxx =\n"
8800             "    \"a\\\n"
8801             "b\\\n"
8802             "c\";",
8803             format("xxxx = \"a\\\n"
8804                    "b\\\n"
8805                    "c\";",
8806                    Break));
8807 
8808   EXPECT_EQ("NSString *const kString =\n"
8809             "    @\"aaaa\"\n"
8810             "    @\"bbbb\";",
8811             format("NSString *const kString = @\"aaaa\"\n"
8812                    "@\"bbbb\";",
8813                    Break));
8814 
8815   Break.ColumnLimit = 0;
8816   verifyFormat("const char *hello = \"hello llvm\";", Break);
8817 }
8818 
8819 TEST_F(FormatTest, AlignsPipes) {
8820   verifyFormat(
8821       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8822       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8823       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8824   verifyFormat(
8825       "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
8826       "                     << aaaaaaaaaaaaaaaaaaaa;");
8827   verifyFormat(
8828       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8829       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8830   verifyFormat(
8831       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
8832       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8833   verifyFormat(
8834       "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
8835       "                \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
8836       "             << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
8837   verifyFormat(
8838       "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8839       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8840       "         << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8841   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8842                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8843                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8844                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
8845   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n"
8846                "             << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);");
8847   verifyFormat(
8848       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8849       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8850   verifyFormat(
8851       "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n"
8852       "                                       aaaaaaaaaaaaaaaaaaaaaaaaaa);");
8853 
8854   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
8855                "             << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
8856   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8857                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8858                "                    aaaaaaaaaaaaaaaaaaaaa)\n"
8859                "             << aaaaaaaaaaaaaaaaaaaaaaaaaa;");
8860   verifyFormat("LOG_IF(aaa == //\n"
8861                "       bbb)\n"
8862                "    << a << b;");
8863 
8864   // But sometimes, breaking before the first "<<" is desirable.
8865   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
8866                "    << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
8867   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
8868                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8869                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8870   verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
8871                "    << BEF << IsTemplate << Description << E->getType();");
8872   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
8873                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8874                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8875   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
8876                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8877                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8878                "    << aaa;");
8879 
8880   verifyFormat(
8881       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8882       "                    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
8883 
8884   // Incomplete string literal.
8885   EXPECT_EQ("llvm::errs() << \"\n"
8886             "             << a;",
8887             format("llvm::errs() << \"\n<<a;"));
8888 
8889   verifyFormat("void f() {\n"
8890                "  CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
8891                "      << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
8892                "}");
8893 
8894   // Handle 'endl'.
8895   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n"
8896                "             << bbbbbbbbbbbbbbbbbbbbbb << endl;");
8897   verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;");
8898 
8899   // Handle '\n'.
8900   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n"
8901                "             << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
8902   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n"
8903                "             << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';");
8904   verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n"
8905                "             << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";");
8906   verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
8907 }
8908 
8909 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) {
8910   verifyFormat("return out << \"somepacket = {\\n\"\n"
8911                "           << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
8912                "           << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
8913                "           << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
8914                "           << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
8915                "           << \"}\";");
8916 
8917   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
8918                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
8919                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
8920   verifyFormat(
8921       "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
8922       "             << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
8923       "             << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
8924       "             << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
8925       "             << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
8926   verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
8927                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
8928   verifyFormat(
8929       "void f() {\n"
8930       "  llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
8931       "               << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
8932       "}");
8933 
8934   // Breaking before the first "<<" is generally not desirable.
8935   verifyFormat(
8936       "llvm::errs()\n"
8937       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8938       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8939       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8940       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
8941       getLLVMStyleWithColumns(70));
8942   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
8943                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8944                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
8945                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8946                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
8947                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
8948                getLLVMStyleWithColumns(70));
8949 
8950   verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
8951                "           \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
8952                "           \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;");
8953   verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
8954                "                  \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
8955                "                  \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);");
8956   verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n"
8957                "           (aaaa + aaaa);",
8958                getLLVMStyleWithColumns(40));
8959   verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n"
8960                "                  (aaaaaaa + aaaaa));",
8961                getLLVMStyleWithColumns(40));
8962   verifyFormat(
8963       "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n"
8964       "                  SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n"
8965       "                  bbbbbbbbbbbbbbbbbbbbbbb);");
8966 }
8967 
8968 TEST_F(FormatTest, UnderstandsEquals) {
8969   verifyFormat(
8970       "aaaaaaaaaaaaaaaaa =\n"
8971       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8972   verifyFormat(
8973       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
8974       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
8975   verifyFormat(
8976       "if (a) {\n"
8977       "  f();\n"
8978       "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
8979       "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
8980       "}");
8981 
8982   verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
8983                "        100000000 + 10000000) {\n}");
8984 }
8985 
8986 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
8987   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
8988                "    .looooooooooooooooooooooooooooooooooooooongFunction();");
8989 
8990   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
8991                "    ->looooooooooooooooooooooooooooooooooooooongFunction();");
8992 
8993   verifyFormat(
8994       "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
8995       "                                                          Parameter2);");
8996 
8997   verifyFormat(
8998       "ShortObject->shortFunction(\n"
8999       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
9000       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
9001 
9002   verifyFormat("loooooooooooooongFunction(\n"
9003                "    LoooooooooooooongObject->looooooooooooooooongFunction());");
9004 
9005   verifyFormat(
9006       "function(LoooooooooooooooooooooooooooooooooooongObject\n"
9007       "             ->loooooooooooooooooooooooooooooooooooooooongFunction());");
9008 
9009   verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
9010                "    .WillRepeatedly(Return(SomeValue));");
9011   verifyFormat("void f() {\n"
9012                "  EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
9013                "      .Times(2)\n"
9014                "      .WillRepeatedly(Return(SomeValue));\n"
9015                "}");
9016   verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
9017                "    ccccccccccccccccccccccc);");
9018   verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9019                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9020                "          .aaaaa(aaaaa),\n"
9021                "      aaaaaaaaaaaaaaaaaaaaa);");
9022   verifyFormat("void f() {\n"
9023                "  aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9024                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
9025                "}");
9026   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9027                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9028                "    .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9029                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9030                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
9031   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9032                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9033                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9034                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
9035                "}");
9036 
9037   // Here, it is not necessary to wrap at "." or "->".
9038   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
9039                "    aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
9040   verifyFormat(
9041       "aaaaaaaaaaa->aaaaaaaaa(\n"
9042       "    aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9043       "    aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
9044 
9045   verifyFormat(
9046       "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9047       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
9048   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
9049                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
9050   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
9051                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
9052 
9053   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9054                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9055                "    .a();");
9056 
9057   FormatStyle NoBinPacking = getLLVMStyle();
9058   NoBinPacking.BinPackParameters = false;
9059   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
9060                "    .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
9061                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
9062                "                         aaaaaaaaaaaaaaaaaaa,\n"
9063                "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9064                NoBinPacking);
9065 
9066   // If there is a subsequent call, change to hanging indentation.
9067   verifyFormat(
9068       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9069       "                         aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
9070       "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
9071   verifyFormat(
9072       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9073       "    aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
9074   verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9075                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9076                "                 .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
9077   verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9078                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9079                "               .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
9080 }
9081 
9082 TEST_F(FormatTest, WrapsTemplateDeclarations) {
9083   verifyFormat("template <typename T>\n"
9084                "virtual void loooooooooooongFunction(int Param1, int Param2);");
9085   verifyFormat("template <typename T>\n"
9086                "// T should be one of {A, B}.\n"
9087                "virtual void loooooooooooongFunction(int Param1, int Param2);");
9088   verifyFormat(
9089       "template <typename T>\n"
9090       "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
9091   verifyFormat("template <typename T>\n"
9092                "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
9093                "       int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
9094   verifyFormat(
9095       "template <typename T>\n"
9096       "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
9097       "                                      int Paaaaaaaaaaaaaaaaaaaaram2);");
9098   verifyFormat(
9099       "template <typename T>\n"
9100       "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
9101       "                    aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
9102       "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9103   verifyFormat("template <typename T>\n"
9104                "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9105                "    int aaaaaaaaaaaaaaaaaaaaaa);");
9106   verifyFormat(
9107       "template <typename T1, typename T2 = char, typename T3 = char,\n"
9108       "          typename T4 = char>\n"
9109       "void f();");
9110   verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
9111                "          template <typename> class cccccccccccccccccccccc,\n"
9112                "          typename ddddddddddddd>\n"
9113                "class C {};");
9114   verifyFormat(
9115       "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
9116       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9117 
9118   verifyFormat("void f() {\n"
9119                "  a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
9120                "      a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
9121                "}");
9122 
9123   verifyFormat("template <typename T> class C {};");
9124   verifyFormat("template <typename T> void f();");
9125   verifyFormat("template <typename T> void f() {}");
9126   verifyFormat(
9127       "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
9128       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9129       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
9130       "    new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
9131       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9132       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
9133       "        bbbbbbbbbbbbbbbbbbbbbbbb);",
9134       getLLVMStyleWithColumns(72));
9135   EXPECT_EQ("static_cast<A< //\n"
9136             "    B> *>(\n"
9137             "\n"
9138             ");",
9139             format("static_cast<A<//\n"
9140                    "    B>*>(\n"
9141                    "\n"
9142                    "    );"));
9143   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9144                "    const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
9145 
9146   FormatStyle AlwaysBreak = getLLVMStyle();
9147   AlwaysBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
9148   verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
9149   verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
9150   verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
9151   verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9152                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
9153                "    ccccccccccccccccccccccccccccccccccccccccccccccc);");
9154   verifyFormat("template <template <typename> class Fooooooo,\n"
9155                "          template <typename> class Baaaaaaar>\n"
9156                "struct C {};",
9157                AlwaysBreak);
9158   verifyFormat("template <typename T> // T can be A, B or C.\n"
9159                "struct C {};",
9160                AlwaysBreak);
9161   verifyFormat("template <enum E> class A {\n"
9162                "public:\n"
9163                "  E *f();\n"
9164                "};");
9165 
9166   FormatStyle NeverBreak = getLLVMStyle();
9167   NeverBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_No;
9168   verifyFormat("template <typename T> class C {};", NeverBreak);
9169   verifyFormat("template <typename T> void f();", NeverBreak);
9170   verifyFormat("template <typename T> void f() {}", NeverBreak);
9171   verifyFormat("template <typename T>\nvoid foo(aaaaaaaaaaaaaaaaaaaaaaaaaa "
9172                "bbbbbbbbbbbbbbbbbbbb) {}",
9173                NeverBreak);
9174   verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9175                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
9176                "    ccccccccccccccccccccccccccccccccccccccccccccccc);",
9177                NeverBreak);
9178   verifyFormat("template <template <typename> class Fooooooo,\n"
9179                "          template <typename> class Baaaaaaar>\n"
9180                "struct C {};",
9181                NeverBreak);
9182   verifyFormat("template <typename T> // T can be A, B or C.\n"
9183                "struct C {};",
9184                NeverBreak);
9185   verifyFormat("template <enum E> class A {\n"
9186                "public:\n"
9187                "  E *f();\n"
9188                "};",
9189                NeverBreak);
9190   NeverBreak.PenaltyBreakTemplateDeclaration = 100;
9191   verifyFormat("template <typename T> void\nfoo(aaaaaaaaaaaaaaaaaaaaaaaaaa "
9192                "bbbbbbbbbbbbbbbbbbbb) {}",
9193                NeverBreak);
9194 }
9195 
9196 TEST_F(FormatTest, WrapsTemplateDeclarationsWithComments) {
9197   FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp);
9198   Style.ColumnLimit = 60;
9199   EXPECT_EQ("// Baseline - no comments.\n"
9200             "template <\n"
9201             "    typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n"
9202             "void f() {}",
9203             format("// Baseline - no comments.\n"
9204                    "template <\n"
9205                    "    typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n"
9206                    "void f() {}",
9207                    Style));
9208 
9209   EXPECT_EQ("template <\n"
9210             "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value>  // trailing\n"
9211             "void f() {}",
9212             format("template <\n"
9213                    "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
9214                    "void f() {}",
9215                    Style));
9216 
9217   EXPECT_EQ(
9218       "template <\n"
9219       "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n"
9220       "void f() {}",
9221       format("template <typename aaaaaaaaaa<bbbbbbbbbbbb>::value>  /* line */\n"
9222              "void f() {}",
9223              Style));
9224 
9225   EXPECT_EQ(
9226       "template <\n"
9227       "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value>  // trailing\n"
9228       "                                               // multiline\n"
9229       "void f() {}",
9230       format("template <\n"
9231              "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
9232              "                                              // multiline\n"
9233              "void f() {}",
9234              Style));
9235 
9236   EXPECT_EQ(
9237       "template <typename aaaaaaaaaa<\n"
9238       "    bbbbbbbbbbbb>::value>  // trailing loooong\n"
9239       "void f() {}",
9240       format(
9241           "template <\n"
9242           "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing loooong\n"
9243           "void f() {}",
9244           Style));
9245 }
9246 
9247 TEST_F(FormatTest, WrapsTemplateParameters) {
9248   FormatStyle Style = getLLVMStyle();
9249   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
9250   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
9251   verifyFormat(
9252       "template <typename... a> struct q {};\n"
9253       "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
9254       "    aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
9255       "    y;",
9256       Style);
9257   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
9258   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
9259   verifyFormat(
9260       "template <typename... a> struct r {};\n"
9261       "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
9262       "    aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
9263       "    y;",
9264       Style);
9265   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
9266   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
9267   verifyFormat("template <typename... a> struct s {};\n"
9268                "extern s<\n"
9269                "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
9270                "aaaaaaaaaaaaaaaaaaaaaa,\n"
9271                "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
9272                "aaaaaaaaaaaaaaaaaaaaaa>\n"
9273                "    y;",
9274                Style);
9275   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
9276   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
9277   verifyFormat("template <typename... a> struct t {};\n"
9278                "extern t<\n"
9279                "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
9280                "aaaaaaaaaaaaaaaaaaaaaa,\n"
9281                "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
9282                "aaaaaaaaaaaaaaaaaaaaaa>\n"
9283                "    y;",
9284                Style);
9285 }
9286 
9287 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
9288   verifyFormat(
9289       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
9290       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
9291   verifyFormat(
9292       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
9293       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9294       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
9295 
9296   // FIXME: Should we have the extra indent after the second break?
9297   verifyFormat(
9298       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
9299       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
9300       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
9301 
9302   verifyFormat(
9303       "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
9304       "                    cccccccccccccccccccccccccccccccccccccccccccccc());");
9305 
9306   // Breaking at nested name specifiers is generally not desirable.
9307   verifyFormat(
9308       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9309       "    aaaaaaaaaaaaaaaaaaaaaaa);");
9310 
9311   verifyFormat("aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n"
9312                "                   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
9313                "                       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9314                "                   aaaaaaaaaaaaaaaaaaaaa);",
9315                getLLVMStyleWithColumns(74));
9316 
9317   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
9318                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9319                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
9320 }
9321 
9322 TEST_F(FormatTest, UnderstandsTemplateParameters) {
9323   verifyFormat("A<int> a;");
9324   verifyFormat("A<A<A<int>>> a;");
9325   verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
9326   verifyFormat("bool x = a < 1 || 2 > a;");
9327   verifyFormat("bool x = 5 < f<int>();");
9328   verifyFormat("bool x = f<int>() > 5;");
9329   verifyFormat("bool x = 5 < a<int>::x;");
9330   verifyFormat("bool x = a < 4 ? a > 2 : false;");
9331   verifyFormat("bool x = f() ? a < 2 : a > 2;");
9332 
9333   verifyGoogleFormat("A<A<int>> a;");
9334   verifyGoogleFormat("A<A<A<int>>> a;");
9335   verifyGoogleFormat("A<A<A<A<int>>>> a;");
9336   verifyGoogleFormat("A<A<int> > a;");
9337   verifyGoogleFormat("A<A<A<int> > > a;");
9338   verifyGoogleFormat("A<A<A<A<int> > > > a;");
9339   verifyGoogleFormat("A<::A<int>> a;");
9340   verifyGoogleFormat("A<::A> a;");
9341   verifyGoogleFormat("A< ::A> a;");
9342   verifyGoogleFormat("A< ::A<int> > a;");
9343   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
9344   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
9345   EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle()));
9346   EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle()));
9347   EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };",
9348             format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle()));
9349 
9350   verifyFormat("A<A<int>> a;", getChromiumStyle(FormatStyle::LK_Cpp));
9351 
9352   // template closer followed by a token that starts with > or =
9353   verifyFormat("bool b = a<1> > 1;");
9354   verifyFormat("bool b = a<1> >= 1;");
9355   verifyFormat("int i = a<1> >> 1;");
9356   FormatStyle Style = getLLVMStyle();
9357   Style.SpaceBeforeAssignmentOperators = false;
9358   verifyFormat("bool b= a<1> == 1;", Style);
9359   verifyFormat("a<int> = 1;", Style);
9360   verifyFormat("a<int> >>= 1;", Style);
9361 
9362   verifyFormat("test < a | b >> c;");
9363   verifyFormat("test<test<a | b>> c;");
9364   verifyFormat("test >> a >> b;");
9365   verifyFormat("test << a >> b;");
9366 
9367   verifyFormat("f<int>();");
9368   verifyFormat("template <typename T> void f() {}");
9369   verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
9370   verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : "
9371                "sizeof(char)>::type>;");
9372   verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};");
9373   verifyFormat("f(a.operator()<A>());");
9374   verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9375                "      .template operator()<A>());",
9376                getLLVMStyleWithColumns(35));
9377 
9378   // Not template parameters.
9379   verifyFormat("return a < b && c > d;");
9380   verifyFormat("void f() {\n"
9381                "  while (a < b && c > d) {\n"
9382                "  }\n"
9383                "}");
9384   verifyFormat("template <typename... Types>\n"
9385                "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
9386 
9387   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9388                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
9389                getLLVMStyleWithColumns(60));
9390   verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
9391   verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
9392   verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <");
9393   verifyFormat("some_templated_type<decltype([](int i) { return i; })>");
9394 }
9395 
9396 TEST_F(FormatTest, UnderstandsShiftOperators) {
9397   verifyFormat("if (i < x >> 1)");
9398   verifyFormat("while (i < x >> 1)");
9399   verifyFormat("for (unsigned i = 0; i < i; ++i, v = v >> 1)");
9400   verifyFormat("for (unsigned i = 0; i < x >> 1; ++i, v = v >> 1)");
9401   verifyFormat(
9402       "for (std::vector<int>::iterator i = 0; i < x >> 1; ++i, v = v >> 1)");
9403   verifyFormat("Foo.call<Bar<Function>>()");
9404   verifyFormat("if (Foo.call<Bar<Function>>() == 0)");
9405   verifyFormat("for (std::vector<std::pair<int>>::iterator i = 0; i < x >> 1; "
9406                "++i, v = v >> 1)");
9407   verifyFormat("if (w<u<v<x>>, 1>::t)");
9408 }
9409 
9410 TEST_F(FormatTest, BitshiftOperatorWidth) {
9411   EXPECT_EQ("int a = 1 << 2; /* foo\n"
9412             "                   bar */",
9413             format("int    a=1<<2;  /* foo\n"
9414                    "                   bar */"));
9415 
9416   EXPECT_EQ("int b = 256 >> 1; /* foo\n"
9417             "                     bar */",
9418             format("int  b  =256>>1 ;  /* foo\n"
9419                    "                      bar */"));
9420 }
9421 
9422 TEST_F(FormatTest, UnderstandsBinaryOperators) {
9423   verifyFormat("COMPARE(a, ==, b);");
9424   verifyFormat("auto s = sizeof...(Ts) - 1;");
9425 }
9426 
9427 TEST_F(FormatTest, UnderstandsPointersToMembers) {
9428   verifyFormat("int A::*x;");
9429   verifyFormat("int (S::*func)(void *);");
9430   verifyFormat("void f() { int (S::*func)(void *); }");
9431   verifyFormat("typedef bool *(Class::*Member)() const;");
9432   verifyFormat("void f() {\n"
9433                "  (a->*f)();\n"
9434                "  a->*x;\n"
9435                "  (a.*f)();\n"
9436                "  ((*a).*f)();\n"
9437                "  a.*x;\n"
9438                "}");
9439   verifyFormat("void f() {\n"
9440                "  (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
9441                "      aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
9442                "}");
9443   verifyFormat(
9444       "(aaaaaaaaaa->*bbbbbbb)(\n"
9445       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
9446   FormatStyle Style = getLLVMStyle();
9447   Style.PointerAlignment = FormatStyle::PAS_Left;
9448   verifyFormat("typedef bool* (Class::*Member)() const;", Style);
9449 }
9450 
9451 TEST_F(FormatTest, UnderstandsUnaryOperators) {
9452   verifyFormat("int a = -2;");
9453   verifyFormat("f(-1, -2, -3);");
9454   verifyFormat("a[-1] = 5;");
9455   verifyFormat("int a = 5 + -2;");
9456   verifyFormat("if (i == -1) {\n}");
9457   verifyFormat("if (i != -1) {\n}");
9458   verifyFormat("if (i > -1) {\n}");
9459   verifyFormat("if (i < -1) {\n}");
9460   verifyFormat("++(a->f());");
9461   verifyFormat("--(a->f());");
9462   verifyFormat("(a->f())++;");
9463   verifyFormat("a[42]++;");
9464   verifyFormat("if (!(a->f())) {\n}");
9465   verifyFormat("if (!+i) {\n}");
9466   verifyFormat("~&a;");
9467 
9468   verifyFormat("a-- > b;");
9469   verifyFormat("b ? -a : c;");
9470   verifyFormat("n * sizeof char16;");
9471   verifyFormat("n * alignof char16;", getGoogleStyle());
9472   verifyFormat("sizeof(char);");
9473   verifyFormat("alignof(char);", getGoogleStyle());
9474 
9475   verifyFormat("return -1;");
9476   verifyFormat("throw -1;");
9477   verifyFormat("switch (a) {\n"
9478                "case -1:\n"
9479                "  break;\n"
9480                "}");
9481   verifyFormat("#define X -1");
9482   verifyFormat("#define X -kConstant");
9483 
9484   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
9485   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
9486 
9487   verifyFormat("int a = /* confusing comment */ -1;");
9488   // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
9489   verifyFormat("int a = i /* confusing comment */++;");
9490 
9491   verifyFormat("co_yield -1;");
9492   verifyFormat("co_return -1;");
9493 
9494   // Check that * is not treated as a binary operator when we set
9495   // PointerAlignment as PAS_Left after a keyword and not a declaration.
9496   FormatStyle PASLeftStyle = getLLVMStyle();
9497   PASLeftStyle.PointerAlignment = FormatStyle::PAS_Left;
9498   verifyFormat("co_return *a;", PASLeftStyle);
9499   verifyFormat("co_await *a;", PASLeftStyle);
9500   verifyFormat("co_yield *a", PASLeftStyle);
9501   verifyFormat("return *a;", PASLeftStyle);
9502 }
9503 
9504 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) {
9505   verifyFormat("if (!aaaaaaaaaa( // break\n"
9506                "        aaaaa)) {\n"
9507                "}");
9508   verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
9509                "    aaaaa));");
9510   verifyFormat("*aaa = aaaaaaa( // break\n"
9511                "    bbbbbb);");
9512 }
9513 
9514 TEST_F(FormatTest, UnderstandsOverloadedOperators) {
9515   verifyFormat("bool operator<();");
9516   verifyFormat("bool operator>();");
9517   verifyFormat("bool operator=();");
9518   verifyFormat("bool operator==();");
9519   verifyFormat("bool operator!=();");
9520   verifyFormat("int operator+();");
9521   verifyFormat("int operator++();");
9522   verifyFormat("int operator++(int) volatile noexcept;");
9523   verifyFormat("bool operator,();");
9524   verifyFormat("bool operator();");
9525   verifyFormat("bool operator()();");
9526   verifyFormat("bool operator[]();");
9527   verifyFormat("operator bool();");
9528   verifyFormat("operator int();");
9529   verifyFormat("operator void *();");
9530   verifyFormat("operator SomeType<int>();");
9531   verifyFormat("operator SomeType<int, int>();");
9532   verifyFormat("operator SomeType<SomeType<int>>();");
9533   verifyFormat("operator< <>();");
9534   verifyFormat("operator<< <>();");
9535   verifyFormat("< <>");
9536 
9537   verifyFormat("void *operator new(std::size_t size);");
9538   verifyFormat("void *operator new[](std::size_t size);");
9539   verifyFormat("void operator delete(void *ptr);");
9540   verifyFormat("void operator delete[](void *ptr);");
9541   verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
9542                "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
9543   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n"
9544                "    aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;");
9545 
9546   verifyFormat(
9547       "ostream &operator<<(ostream &OutputStream,\n"
9548       "                    SomeReallyLongType WithSomeReallyLongValue);");
9549   verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
9550                "               const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
9551                "  return left.group < right.group;\n"
9552                "}");
9553   verifyFormat("SomeType &operator=(const SomeType &S);");
9554   verifyFormat("f.template operator()<int>();");
9555 
9556   verifyGoogleFormat("operator void*();");
9557   verifyGoogleFormat("operator SomeType<SomeType<int>>();");
9558   verifyGoogleFormat("operator ::A();");
9559 
9560   verifyFormat("using A::operator+;");
9561   verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n"
9562                "int i;");
9563 
9564   // Calling an operator as a member function.
9565   verifyFormat("void f() { a.operator*(); }");
9566   verifyFormat("void f() { a.operator*(b & b); }");
9567   verifyFormat("void f() { a->operator&(a * b); }");
9568   verifyFormat("void f() { NS::a.operator+(*b * *b); }");
9569   // TODO: Calling an operator as a non-member function is hard to distinguish.
9570   // https://llvm.org/PR50629
9571   // verifyFormat("void f() { operator*(a & a); }");
9572   // verifyFormat("void f() { operator&(a, b * b); }");
9573 
9574   verifyFormat("::operator delete(foo);");
9575   verifyFormat("::operator new(n * sizeof(foo));");
9576   verifyFormat("foo() { ::operator delete(foo); }");
9577   verifyFormat("foo() { ::operator new(n * sizeof(foo)); }");
9578 }
9579 
9580 TEST_F(FormatTest, UnderstandsFunctionRefQualification) {
9581   verifyFormat("Deleted &operator=(const Deleted &) & = default;");
9582   verifyFormat("Deleted &operator=(const Deleted &) && = delete;");
9583   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;");
9584   verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;");
9585   verifyFormat("Deleted &operator=(const Deleted &) &;");
9586   verifyFormat("Deleted &operator=(const Deleted &) &&;");
9587   verifyFormat("SomeType MemberFunction(const Deleted &) &;");
9588   verifyFormat("SomeType MemberFunction(const Deleted &) &&;");
9589   verifyFormat("SomeType MemberFunction(const Deleted &) && {}");
9590   verifyFormat("SomeType MemberFunction(const Deleted &) && final {}");
9591   verifyFormat("SomeType MemberFunction(const Deleted &) && override {}");
9592   verifyFormat("void Fn(T const &) const &;");
9593   verifyFormat("void Fn(T const volatile &&) const volatile &&;");
9594   verifyFormat("template <typename T>\n"
9595                "void F(T) && = delete;",
9596                getGoogleStyle());
9597 
9598   FormatStyle AlignLeft = getLLVMStyle();
9599   AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
9600   verifyFormat("void A::b() && {}", AlignLeft);
9601   verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft);
9602   verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;",
9603                AlignLeft);
9604   verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft);
9605   verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft);
9606   verifyFormat("auto Function(T t) & -> void {}", AlignLeft);
9607   verifyFormat("auto Function(T... t) & -> void {}", AlignLeft);
9608   verifyFormat("auto Function(T) & -> void {}", AlignLeft);
9609   verifyFormat("auto Function(T) & -> void;", AlignLeft);
9610   verifyFormat("void Fn(T const&) const&;", AlignLeft);
9611   verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft);
9612 
9613   FormatStyle Spaces = getLLVMStyle();
9614   Spaces.SpacesInCStyleCastParentheses = true;
9615   verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces);
9616   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces);
9617   verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces);
9618   verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces);
9619 
9620   Spaces.SpacesInCStyleCastParentheses = false;
9621   Spaces.SpacesInParentheses = true;
9622   verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces);
9623   verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;",
9624                Spaces);
9625   verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces);
9626   verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces);
9627 
9628   FormatStyle BreakTemplate = getLLVMStyle();
9629   BreakTemplate.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
9630 
9631   verifyFormat("struct f {\n"
9632                "  template <class T>\n"
9633                "  int &foo(const std::string &str) &noexcept {}\n"
9634                "};",
9635                BreakTemplate);
9636 
9637   verifyFormat("struct f {\n"
9638                "  template <class T>\n"
9639                "  int &foo(const std::string &str) &&noexcept {}\n"
9640                "};",
9641                BreakTemplate);
9642 
9643   verifyFormat("struct f {\n"
9644                "  template <class T>\n"
9645                "  int &foo(const std::string &str) const &noexcept {}\n"
9646                "};",
9647                BreakTemplate);
9648 
9649   verifyFormat("struct f {\n"
9650                "  template <class T>\n"
9651                "  int &foo(const std::string &str) const &noexcept {}\n"
9652                "};",
9653                BreakTemplate);
9654 
9655   verifyFormat("struct f {\n"
9656                "  template <class T>\n"
9657                "  auto foo(const std::string &str) &&noexcept -> int & {}\n"
9658                "};",
9659                BreakTemplate);
9660 
9661   FormatStyle AlignLeftBreakTemplate = getLLVMStyle();
9662   AlignLeftBreakTemplate.AlwaysBreakTemplateDeclarations =
9663       FormatStyle::BTDS_Yes;
9664   AlignLeftBreakTemplate.PointerAlignment = FormatStyle::PAS_Left;
9665 
9666   verifyFormat("struct f {\n"
9667                "  template <class T>\n"
9668                "  int& foo(const std::string& str) & noexcept {}\n"
9669                "};",
9670                AlignLeftBreakTemplate);
9671 
9672   verifyFormat("struct f {\n"
9673                "  template <class T>\n"
9674                "  int& foo(const std::string& str) && noexcept {}\n"
9675                "};",
9676                AlignLeftBreakTemplate);
9677 
9678   verifyFormat("struct f {\n"
9679                "  template <class T>\n"
9680                "  int& foo(const std::string& str) const& noexcept {}\n"
9681                "};",
9682                AlignLeftBreakTemplate);
9683 
9684   verifyFormat("struct f {\n"
9685                "  template <class T>\n"
9686                "  int& foo(const std::string& str) const&& noexcept {}\n"
9687                "};",
9688                AlignLeftBreakTemplate);
9689 
9690   verifyFormat("struct f {\n"
9691                "  template <class T>\n"
9692                "  auto foo(const std::string& str) && noexcept -> int& {}\n"
9693                "};",
9694                AlignLeftBreakTemplate);
9695 
9696   // The `&` in `Type&` should not be confused with a trailing `&` of
9697   // DEPRECATED(reason) member function.
9698   verifyFormat("struct f {\n"
9699                "  template <class T>\n"
9700                "  DEPRECATED(reason)\n"
9701                "  Type &foo(arguments) {}\n"
9702                "};",
9703                BreakTemplate);
9704 
9705   verifyFormat("struct f {\n"
9706                "  template <class T>\n"
9707                "  DEPRECATED(reason)\n"
9708                "  Type& foo(arguments) {}\n"
9709                "};",
9710                AlignLeftBreakTemplate);
9711 
9712   verifyFormat("void (*foopt)(int) = &func;");
9713 
9714   FormatStyle DerivePointerAlignment = getLLVMStyle();
9715   DerivePointerAlignment.DerivePointerAlignment = true;
9716   // There's always a space between the function and its trailing qualifiers.
9717   // This isn't evidence for PAS_Right (or for PAS_Left).
9718   std::string Prefix = "void a() &;\n"
9719                        "void b() &;\n";
9720   verifyFormat(Prefix + "int* x;", DerivePointerAlignment);
9721   verifyFormat(Prefix + "int *x;", DerivePointerAlignment);
9722   // Same if the function is an overloaded operator, and with &&.
9723   Prefix = "void operator()() &&;\n"
9724            "void operator()() &&;\n";
9725   verifyFormat(Prefix + "int* x;", DerivePointerAlignment);
9726   verifyFormat(Prefix + "int *x;", DerivePointerAlignment);
9727   // However a space between cv-qualifiers and ref-qualifiers *is* evidence.
9728   Prefix = "void a() const &;\n"
9729            "void b() const &;\n";
9730   EXPECT_EQ(Prefix + "int *x;",
9731             format(Prefix + "int* x;", DerivePointerAlignment));
9732 }
9733 
9734 TEST_F(FormatTest, UnderstandsNewAndDelete) {
9735   verifyFormat("void f() {\n"
9736                "  A *a = new A;\n"
9737                "  A *a = new (placement) A;\n"
9738                "  delete a;\n"
9739                "  delete (A *)a;\n"
9740                "}");
9741   verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
9742                "    typename aaaaaaaaaaaaaaaaaaaaaaaa();");
9743   verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
9744                "    new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
9745                "        typename aaaaaaaaaaaaaaaaaaaaaaaa();");
9746   verifyFormat("delete[] h->p;");
9747 
9748   verifyFormat("void operator delete(void *foo) ATTRIB;");
9749   verifyFormat("void operator new(void *foo) ATTRIB;");
9750   verifyFormat("void operator delete[](void *foo) ATTRIB;");
9751   verifyFormat("void operator delete(void *ptr) noexcept;");
9752 }
9753 
9754 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
9755   verifyFormat("int *f(int *a) {}");
9756   verifyFormat("int main(int argc, char **argv) {}");
9757   verifyFormat("Test::Test(int b) : a(b * b) {}");
9758   verifyIndependentOfContext("f(a, *a);");
9759   verifyFormat("void g() { f(*a); }");
9760   verifyIndependentOfContext("int a = b * 10;");
9761   verifyIndependentOfContext("int a = 10 * b;");
9762   verifyIndependentOfContext("int a = b * c;");
9763   verifyIndependentOfContext("int a += b * c;");
9764   verifyIndependentOfContext("int a -= b * c;");
9765   verifyIndependentOfContext("int a *= b * c;");
9766   verifyIndependentOfContext("int a /= b * c;");
9767   verifyIndependentOfContext("int a = *b;");
9768   verifyIndependentOfContext("int a = *b * c;");
9769   verifyIndependentOfContext("int a = b * *c;");
9770   verifyIndependentOfContext("int a = b * (10);");
9771   verifyIndependentOfContext("S << b * (10);");
9772   verifyIndependentOfContext("return 10 * b;");
9773   verifyIndependentOfContext("return *b * *c;");
9774   verifyIndependentOfContext("return a & ~b;");
9775   verifyIndependentOfContext("f(b ? *c : *d);");
9776   verifyIndependentOfContext("int a = b ? *c : *d;");
9777   verifyIndependentOfContext("*b = a;");
9778   verifyIndependentOfContext("a * ~b;");
9779   verifyIndependentOfContext("a * !b;");
9780   verifyIndependentOfContext("a * +b;");
9781   verifyIndependentOfContext("a * -b;");
9782   verifyIndependentOfContext("a * ++b;");
9783   verifyIndependentOfContext("a * --b;");
9784   verifyIndependentOfContext("a[4] * b;");
9785   verifyIndependentOfContext("a[a * a] = 1;");
9786   verifyIndependentOfContext("f() * b;");
9787   verifyIndependentOfContext("a * [self dostuff];");
9788   verifyIndependentOfContext("int x = a * (a + b);");
9789   verifyIndependentOfContext("(a *)(a + b);");
9790   verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
9791   verifyIndependentOfContext("int *pa = (int *)&a;");
9792   verifyIndependentOfContext("return sizeof(int **);");
9793   verifyIndependentOfContext("return sizeof(int ******);");
9794   verifyIndependentOfContext("return (int **&)a;");
9795   verifyIndependentOfContext("f((*PointerToArray)[10]);");
9796   verifyFormat("void f(Type (*parameter)[10]) {}");
9797   verifyFormat("void f(Type (&parameter)[10]) {}");
9798   verifyGoogleFormat("return sizeof(int**);");
9799   verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
9800   verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
9801   verifyFormat("auto a = [](int **&, int ***) {};");
9802   verifyFormat("auto PointerBinding = [](const char *S) {};");
9803   verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
9804   verifyFormat("[](const decltype(*a) &value) {}");
9805   verifyFormat("[](const typeof(*a) &value) {}");
9806   verifyFormat("[](const _Atomic(a *) &value) {}");
9807   verifyFormat("[](const __underlying_type(a) &value) {}");
9808   verifyFormat("decltype(a * b) F();");
9809   verifyFormat("typeof(a * b) F();");
9810   verifyFormat("#define MACRO() [](A *a) { return 1; }");
9811   verifyFormat("Constructor() : member([](A *a, B *b) {}) {}");
9812   verifyIndependentOfContext("typedef void (*f)(int *a);");
9813   verifyIndependentOfContext("int i{a * b};");
9814   verifyIndependentOfContext("aaa && aaa->f();");
9815   verifyIndependentOfContext("int x = ~*p;");
9816   verifyFormat("Constructor() : a(a), area(width * height) {}");
9817   verifyFormat("Constructor() : a(a), area(a, width * height) {}");
9818   verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}");
9819   verifyFormat("void f() { f(a, c * d); }");
9820   verifyFormat("void f() { f(new a(), c * d); }");
9821   verifyFormat("void f(const MyOverride &override);");
9822   verifyFormat("void f(const MyFinal &final);");
9823   verifyIndependentOfContext("bool a = f() && override.f();");
9824   verifyIndependentOfContext("bool a = f() && final.f();");
9825 
9826   verifyIndependentOfContext("InvalidRegions[*R] = 0;");
9827 
9828   verifyIndependentOfContext("A<int *> a;");
9829   verifyIndependentOfContext("A<int **> a;");
9830   verifyIndependentOfContext("A<int *, int *> a;");
9831   verifyIndependentOfContext("A<int *[]> a;");
9832   verifyIndependentOfContext(
9833       "const char *const p = reinterpret_cast<const char *const>(q);");
9834   verifyIndependentOfContext("A<int **, int **> a;");
9835   verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
9836   verifyFormat("for (char **a = b; *a; ++a) {\n}");
9837   verifyFormat("for (; a && b;) {\n}");
9838   verifyFormat("bool foo = true && [] { return false; }();");
9839 
9840   verifyFormat(
9841       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9842       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9843 
9844   verifyGoogleFormat("int const* a = &b;");
9845   verifyGoogleFormat("**outparam = 1;");
9846   verifyGoogleFormat("*outparam = a * b;");
9847   verifyGoogleFormat("int main(int argc, char** argv) {}");
9848   verifyGoogleFormat("A<int*> a;");
9849   verifyGoogleFormat("A<int**> a;");
9850   verifyGoogleFormat("A<int*, int*> a;");
9851   verifyGoogleFormat("A<int**, int**> a;");
9852   verifyGoogleFormat("f(b ? *c : *d);");
9853   verifyGoogleFormat("int a = b ? *c : *d;");
9854   verifyGoogleFormat("Type* t = **x;");
9855   verifyGoogleFormat("Type* t = *++*x;");
9856   verifyGoogleFormat("*++*x;");
9857   verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
9858   verifyGoogleFormat("Type* t = x++ * y;");
9859   verifyGoogleFormat(
9860       "const char* const p = reinterpret_cast<const char* const>(q);");
9861   verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
9862   verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
9863   verifyGoogleFormat("template <typename T>\n"
9864                      "void f(int i = 0, SomeType** temps = NULL);");
9865 
9866   FormatStyle Left = getLLVMStyle();
9867   Left.PointerAlignment = FormatStyle::PAS_Left;
9868   verifyFormat("x = *a(x) = *a(y);", Left);
9869   verifyFormat("for (;; *a = b) {\n}", Left);
9870   verifyFormat("return *this += 1;", Left);
9871   verifyFormat("throw *x;", Left);
9872   verifyFormat("delete *x;", Left);
9873   verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left);
9874   verifyFormat("[](const decltype(*a)* ptr) {}", Left);
9875   verifyFormat("[](const typeof(*a)* ptr) {}", Left);
9876   verifyFormat("[](const _Atomic(a*)* ptr) {}", Left);
9877   verifyFormat("[](const __underlying_type(a)* ptr) {}", Left);
9878   verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left);
9879   verifyFormat("auto x(A&&, B&&, C&&) -> D;", Left);
9880   verifyFormat("auto x = [](A&&, B&&, C&&) -> D {};", Left);
9881   verifyFormat("template <class T> X(T&&, T&&, T&&) -> X<T>;", Left);
9882 
9883   verifyIndependentOfContext("a = *(x + y);");
9884   verifyIndependentOfContext("a = &(x + y);");
9885   verifyIndependentOfContext("*(x + y).call();");
9886   verifyIndependentOfContext("&(x + y)->call();");
9887   verifyFormat("void f() { &(*I).first; }");
9888 
9889   verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
9890   verifyFormat("f(* /* confusing comment */ foo);");
9891   verifyFormat("void (* /*deleter*/)(const Slice &key, void *value)");
9892   verifyFormat("void foo(int * // this is the first paramters\n"
9893                "         ,\n"
9894                "         int second);");
9895   verifyFormat("double term = a * // first\n"
9896                "              b;");
9897   verifyFormat(
9898       "int *MyValues = {\n"
9899       "    *A, // Operator detection might be confused by the '{'\n"
9900       "    *BB // Operator detection might be confused by previous comment\n"
9901       "};");
9902 
9903   verifyIndependentOfContext("if (int *a = &b)");
9904   verifyIndependentOfContext("if (int &a = *b)");
9905   verifyIndependentOfContext("if (a & b[i])");
9906   verifyIndependentOfContext("if constexpr (a & b[i])");
9907   verifyIndependentOfContext("if CONSTEXPR (a & b[i])");
9908   verifyIndependentOfContext("if (a * (b * c))");
9909   verifyIndependentOfContext("if constexpr (a * (b * c))");
9910   verifyIndependentOfContext("if CONSTEXPR (a * (b * c))");
9911   verifyIndependentOfContext("if (a::b::c::d & b[i])");
9912   verifyIndependentOfContext("if (*b[i])");
9913   verifyIndependentOfContext("if (int *a = (&b))");
9914   verifyIndependentOfContext("while (int *a = &b)");
9915   verifyIndependentOfContext("while (a * (b * c))");
9916   verifyIndependentOfContext("size = sizeof *a;");
9917   verifyIndependentOfContext("if (a && (b = c))");
9918   verifyFormat("void f() {\n"
9919                "  for (const int &v : Values) {\n"
9920                "  }\n"
9921                "}");
9922   verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
9923   verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
9924   verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
9925 
9926   verifyFormat("#define A (!a * b)");
9927   verifyFormat("#define MACRO     \\\n"
9928                "  int *i = a * b; \\\n"
9929                "  void f(a *b);",
9930                getLLVMStyleWithColumns(19));
9931 
9932   verifyIndependentOfContext("A = new SomeType *[Length];");
9933   verifyIndependentOfContext("A = new SomeType *[Length]();");
9934   verifyIndependentOfContext("T **t = new T *;");
9935   verifyIndependentOfContext("T **t = new T *();");
9936   verifyGoogleFormat("A = new SomeType*[Length]();");
9937   verifyGoogleFormat("A = new SomeType*[Length];");
9938   verifyGoogleFormat("T** t = new T*;");
9939   verifyGoogleFormat("T** t = new T*();");
9940 
9941   verifyFormat("STATIC_ASSERT((a & b) == 0);");
9942   verifyFormat("STATIC_ASSERT(0 == (a & b));");
9943   verifyFormat("template <bool a, bool b> "
9944                "typename t::if<x && y>::type f() {}");
9945   verifyFormat("template <int *y> f() {}");
9946   verifyFormat("vector<int *> v;");
9947   verifyFormat("vector<int *const> v;");
9948   verifyFormat("vector<int *const **const *> v;");
9949   verifyFormat("vector<int *volatile> v;");
9950   verifyFormat("vector<a *_Nonnull> v;");
9951   verifyFormat("vector<a *_Nullable> v;");
9952   verifyFormat("vector<a *_Null_unspecified> v;");
9953   verifyFormat("vector<a *__ptr32> v;");
9954   verifyFormat("vector<a *__ptr64> v;");
9955   verifyFormat("vector<a *__capability> v;");
9956   FormatStyle TypeMacros = getLLVMStyle();
9957   TypeMacros.TypenameMacros = {"LIST"};
9958   verifyFormat("vector<LIST(uint64_t)> v;", TypeMacros);
9959   verifyFormat("vector<LIST(uint64_t) *> v;", TypeMacros);
9960   verifyFormat("vector<LIST(uint64_t) **> v;", TypeMacros);
9961   verifyFormat("vector<LIST(uint64_t) *attr> v;", TypeMacros);
9962   verifyFormat("vector<A(uint64_t) * attr> v;", TypeMacros); // multiplication
9963 
9964   FormatStyle CustomQualifier = getLLVMStyle();
9965   // Add identifiers that should not be parsed as a qualifier by default.
9966   CustomQualifier.AttributeMacros.push_back("__my_qualifier");
9967   CustomQualifier.AttributeMacros.push_back("_My_qualifier");
9968   CustomQualifier.AttributeMacros.push_back("my_other_qualifier");
9969   verifyFormat("vector<a * __my_qualifier> parse_as_multiply;");
9970   verifyFormat("vector<a *__my_qualifier> v;", CustomQualifier);
9971   verifyFormat("vector<a * _My_qualifier> parse_as_multiply;");
9972   verifyFormat("vector<a *_My_qualifier> v;", CustomQualifier);
9973   verifyFormat("vector<a * my_other_qualifier> parse_as_multiply;");
9974   verifyFormat("vector<a *my_other_qualifier> v;", CustomQualifier);
9975   verifyFormat("vector<a * _NotAQualifier> v;");
9976   verifyFormat("vector<a * __not_a_qualifier> v;");
9977   verifyFormat("vector<a * b> v;");
9978   verifyFormat("foo<b && false>();");
9979   verifyFormat("foo<b & 1>();");
9980   verifyFormat("decltype(*::std::declval<const T &>()) void F();");
9981   verifyFormat("typeof(*::std::declval<const T &>()) void F();");
9982   verifyFormat("_Atomic(*::std::declval<const T &>()) void F();");
9983   verifyFormat("__underlying_type(*::std::declval<const T &>()) void F();");
9984   verifyFormat(
9985       "template <class T, class = typename std::enable_if<\n"
9986       "                       std::is_integral<T>::value &&\n"
9987       "                       (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
9988       "void F();",
9989       getLLVMStyleWithColumns(70));
9990   verifyFormat("template <class T,\n"
9991                "          class = typename std::enable_if<\n"
9992                "              std::is_integral<T>::value &&\n"
9993                "              (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n"
9994                "          class U>\n"
9995                "void F();",
9996                getLLVMStyleWithColumns(70));
9997   verifyFormat(
9998       "template <class T,\n"
9999       "          class = typename ::std::enable_if<\n"
10000       "              ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
10001       "void F();",
10002       getGoogleStyleWithColumns(68));
10003 
10004   verifyIndependentOfContext("MACRO(int *i);");
10005   verifyIndependentOfContext("MACRO(auto *a);");
10006   verifyIndependentOfContext("MACRO(const A *a);");
10007   verifyIndependentOfContext("MACRO(_Atomic(A) *a);");
10008   verifyIndependentOfContext("MACRO(decltype(A) *a);");
10009   verifyIndependentOfContext("MACRO(typeof(A) *a);");
10010   verifyIndependentOfContext("MACRO(__underlying_type(A) *a);");
10011   verifyIndependentOfContext("MACRO(A *const a);");
10012   verifyIndependentOfContext("MACRO(A *restrict a);");
10013   verifyIndependentOfContext("MACRO(A *__restrict__ a);");
10014   verifyIndependentOfContext("MACRO(A *__restrict a);");
10015   verifyIndependentOfContext("MACRO(A *volatile a);");
10016   verifyIndependentOfContext("MACRO(A *__volatile a);");
10017   verifyIndependentOfContext("MACRO(A *__volatile__ a);");
10018   verifyIndependentOfContext("MACRO(A *_Nonnull a);");
10019   verifyIndependentOfContext("MACRO(A *_Nullable a);");
10020   verifyIndependentOfContext("MACRO(A *_Null_unspecified a);");
10021   verifyIndependentOfContext("MACRO(A *__attribute__((foo)) a);");
10022   verifyIndependentOfContext("MACRO(A *__attribute((foo)) a);");
10023   verifyIndependentOfContext("MACRO(A *[[clang::attr]] a);");
10024   verifyIndependentOfContext("MACRO(A *[[clang::attr(\"foo\")]] a);");
10025   verifyIndependentOfContext("MACRO(A *__ptr32 a);");
10026   verifyIndependentOfContext("MACRO(A *__ptr64 a);");
10027   verifyIndependentOfContext("MACRO(A *__capability);");
10028   verifyIndependentOfContext("MACRO(A &__capability);");
10029   verifyFormat("MACRO(A *__my_qualifier);");               // type declaration
10030   verifyFormat("void f() { MACRO(A * __my_qualifier); }"); // multiplication
10031   // If we add __my_qualifier to AttributeMacros it should always be parsed as
10032   // a type declaration:
10033   verifyFormat("MACRO(A *__my_qualifier);", CustomQualifier);
10034   verifyFormat("void f() { MACRO(A *__my_qualifier); }", CustomQualifier);
10035   // Also check that TypenameMacros prevents parsing it as multiplication:
10036   verifyIndependentOfContext("MACRO(LIST(uint64_t) * a);"); // multiplication
10037   verifyIndependentOfContext("MACRO(LIST(uint64_t) *a);", TypeMacros); // type
10038 
10039   verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
10040   verifyFormat("void f() { f(float{1}, a * a); }");
10041   verifyFormat("void f() { f(float(1), a * a); }");
10042 
10043   verifyFormat("f((void (*)(int))g);");
10044   verifyFormat("f((void (&)(int))g);");
10045   verifyFormat("f((void (^)(int))g);");
10046 
10047   // FIXME: Is there a way to make this work?
10048   // verifyIndependentOfContext("MACRO(A *a);");
10049   verifyFormat("MACRO(A &B);");
10050   verifyFormat("MACRO(A *B);");
10051   verifyFormat("void f() { MACRO(A * B); }");
10052   verifyFormat("void f() { MACRO(A & B); }");
10053 
10054   // This lambda was mis-formatted after D88956 (treating it as a binop):
10055   verifyFormat("auto x = [](const decltype(x) &ptr) {};");
10056   verifyFormat("auto x = [](const decltype(x) *ptr) {};");
10057   verifyFormat("#define lambda [](const decltype(x) &ptr) {}");
10058   verifyFormat("#define lambda [](const decltype(x) *ptr) {}");
10059 
10060   verifyFormat("DatumHandle const *operator->() const { return input_; }");
10061   verifyFormat("return options != nullptr && operator==(*options);");
10062 
10063   EXPECT_EQ("#define OP(x)                                    \\\n"
10064             "  ostream &operator<<(ostream &s, const A &a) {  \\\n"
10065             "    return s << a.DebugString();                 \\\n"
10066             "  }",
10067             format("#define OP(x) \\\n"
10068                    "  ostream &operator<<(ostream &s, const A &a) { \\\n"
10069                    "    return s << a.DebugString(); \\\n"
10070                    "  }",
10071                    getLLVMStyleWithColumns(50)));
10072 
10073   // FIXME: We cannot handle this case yet; we might be able to figure out that
10074   // foo<x> d > v; doesn't make sense.
10075   verifyFormat("foo<a<b && c> d> v;");
10076 
10077   FormatStyle PointerMiddle = getLLVMStyle();
10078   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
10079   verifyFormat("delete *x;", PointerMiddle);
10080   verifyFormat("int * x;", PointerMiddle);
10081   verifyFormat("int *[] x;", PointerMiddle);
10082   verifyFormat("template <int * y> f() {}", PointerMiddle);
10083   verifyFormat("int * f(int * a) {}", PointerMiddle);
10084   verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle);
10085   verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle);
10086   verifyFormat("A<int *> a;", PointerMiddle);
10087   verifyFormat("A<int **> a;", PointerMiddle);
10088   verifyFormat("A<int *, int *> a;", PointerMiddle);
10089   verifyFormat("A<int *[]> a;", PointerMiddle);
10090   verifyFormat("A = new SomeType *[Length]();", PointerMiddle);
10091   verifyFormat("A = new SomeType *[Length];", PointerMiddle);
10092   verifyFormat("T ** t = new T *;", PointerMiddle);
10093 
10094   // Member function reference qualifiers aren't binary operators.
10095   verifyFormat("string // break\n"
10096                "operator()() & {}");
10097   verifyFormat("string // break\n"
10098                "operator()() && {}");
10099   verifyGoogleFormat("template <typename T>\n"
10100                      "auto x() & -> int {}");
10101 
10102   // Should be binary operators when used as an argument expression (overloaded
10103   // operator invoked as a member function).
10104   verifyFormat("void f() { a.operator()(a * a); }");
10105   verifyFormat("void f() { a->operator()(a & a); }");
10106   verifyFormat("void f() { a.operator()(*a & *a); }");
10107   verifyFormat("void f() { a->operator()(*a * *a); }");
10108 
10109   verifyFormat("int operator()(T (&&)[N]) { return 1; }");
10110   verifyFormat("int operator()(T (&)[N]) { return 0; }");
10111 }
10112 
10113 TEST_F(FormatTest, UnderstandsAttributes) {
10114   verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
10115   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
10116                "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
10117   verifyFormat("__attribute__((nodebug)) ::qualified_type f();");
10118   FormatStyle AfterType = getLLVMStyle();
10119   AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
10120   verifyFormat("__attribute__((nodebug)) void\n"
10121                "foo() {}\n",
10122                AfterType);
10123   verifyFormat("__unused void\n"
10124                "foo() {}",
10125                AfterType);
10126 
10127   FormatStyle CustomAttrs = getLLVMStyle();
10128   CustomAttrs.AttributeMacros.push_back("__unused");
10129   CustomAttrs.AttributeMacros.push_back("__attr1");
10130   CustomAttrs.AttributeMacros.push_back("__attr2");
10131   CustomAttrs.AttributeMacros.push_back("no_underscore_attr");
10132   verifyFormat("vector<SomeType *__attribute((foo))> v;");
10133   verifyFormat("vector<SomeType *__attribute__((foo))> v;");
10134   verifyFormat("vector<SomeType * __not_attribute__((foo))> v;");
10135   // Check that it is parsed as a multiplication without AttributeMacros and
10136   // as a pointer qualifier when we add __attr1/__attr2 to AttributeMacros.
10137   verifyFormat("vector<SomeType * __attr1> v;");
10138   verifyFormat("vector<SomeType __attr1 *> v;");
10139   verifyFormat("vector<SomeType __attr1 *const> v;");
10140   verifyFormat("vector<SomeType __attr1 * __attr2> v;");
10141   verifyFormat("vector<SomeType *__attr1> v;", CustomAttrs);
10142   verifyFormat("vector<SomeType *__attr2> v;", CustomAttrs);
10143   verifyFormat("vector<SomeType *no_underscore_attr> v;", CustomAttrs);
10144   verifyFormat("vector<SomeType __attr1 *> v;", CustomAttrs);
10145   verifyFormat("vector<SomeType __attr1 *const> v;", CustomAttrs);
10146   verifyFormat("vector<SomeType __attr1 *__attr2> v;", CustomAttrs);
10147   verifyFormat("vector<SomeType __attr1 *no_underscore_attr> v;", CustomAttrs);
10148 
10149   // Check that these are not parsed as function declarations:
10150   CustomAttrs.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
10151   CustomAttrs.BreakBeforeBraces = FormatStyle::BS_Allman;
10152   verifyFormat("SomeType s(InitValue);", CustomAttrs);
10153   verifyFormat("SomeType s{InitValue};", CustomAttrs);
10154   verifyFormat("SomeType *__unused s(InitValue);", CustomAttrs);
10155   verifyFormat("SomeType *__unused s{InitValue};", CustomAttrs);
10156   verifyFormat("SomeType s __unused(InitValue);", CustomAttrs);
10157   verifyFormat("SomeType s __unused{InitValue};", CustomAttrs);
10158   verifyFormat("SomeType *__capability s(InitValue);", CustomAttrs);
10159   verifyFormat("SomeType *__capability s{InitValue};", CustomAttrs);
10160 }
10161 
10162 TEST_F(FormatTest, UnderstandsPointerQualifiersInCast) {
10163   // Check that qualifiers on pointers don't break parsing of casts.
10164   verifyFormat("x = (foo *const)*v;");
10165   verifyFormat("x = (foo *volatile)*v;");
10166   verifyFormat("x = (foo *restrict)*v;");
10167   verifyFormat("x = (foo *__attribute__((foo)))*v;");
10168   verifyFormat("x = (foo *_Nonnull)*v;");
10169   verifyFormat("x = (foo *_Nullable)*v;");
10170   verifyFormat("x = (foo *_Null_unspecified)*v;");
10171   verifyFormat("x = (foo *_Nonnull)*v;");
10172   verifyFormat("x = (foo *[[clang::attr]])*v;");
10173   verifyFormat("x = (foo *[[clang::attr(\"foo\")]])*v;");
10174   verifyFormat("x = (foo *__ptr32)*v;");
10175   verifyFormat("x = (foo *__ptr64)*v;");
10176   verifyFormat("x = (foo *__capability)*v;");
10177 
10178   // Check that we handle multiple trailing qualifiers and skip them all to
10179   // determine that the expression is a cast to a pointer type.
10180   FormatStyle LongPointerRight = getLLVMStyleWithColumns(999);
10181   FormatStyle LongPointerLeft = getLLVMStyleWithColumns(999);
10182   LongPointerLeft.PointerAlignment = FormatStyle::PAS_Left;
10183   StringRef AllQualifiers =
10184       "const volatile restrict __attribute__((foo)) _Nonnull _Null_unspecified "
10185       "_Nonnull [[clang::attr]] __ptr32 __ptr64 __capability";
10186   verifyFormat(("x = (foo *" + AllQualifiers + ")*v;").str(), LongPointerRight);
10187   verifyFormat(("x = (foo* " + AllQualifiers + ")*v;").str(), LongPointerLeft);
10188 
10189   // Also check that address-of is not parsed as a binary bitwise-and:
10190   verifyFormat("x = (foo *const)&v;");
10191   verifyFormat(("x = (foo *" + AllQualifiers + ")&v;").str(), LongPointerRight);
10192   verifyFormat(("x = (foo* " + AllQualifiers + ")&v;").str(), LongPointerLeft);
10193 
10194   // Check custom qualifiers:
10195   FormatStyle CustomQualifier = getLLVMStyleWithColumns(999);
10196   CustomQualifier.AttributeMacros.push_back("__my_qualifier");
10197   verifyFormat("x = (foo * __my_qualifier) * v;"); // not parsed as qualifier.
10198   verifyFormat("x = (foo *__my_qualifier)*v;", CustomQualifier);
10199   verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)*v;").str(),
10200                CustomQualifier);
10201   verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)&v;").str(),
10202                CustomQualifier);
10203 
10204   // Check that unknown identifiers result in binary operator parsing:
10205   verifyFormat("x = (foo * __unknown_qualifier) * v;");
10206   verifyFormat("x = (foo * __unknown_qualifier) & v;");
10207 }
10208 
10209 TEST_F(FormatTest, UnderstandsSquareAttributes) {
10210   verifyFormat("SomeType s [[unused]] (InitValue);");
10211   verifyFormat("SomeType s [[gnu::unused]] (InitValue);");
10212   verifyFormat("SomeType s [[using gnu: unused]] (InitValue);");
10213   verifyFormat("[[gsl::suppress(\"clang-tidy-check-name\")]] void f() {}");
10214   verifyFormat("void f() [[deprecated(\"so sorry\")]];");
10215   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10216                "    [[unused]] aaaaaaaaaaaaaaaaaaaaaaa(int i);");
10217   verifyFormat("[[nodiscard]] bool f() { return false; }");
10218   verifyFormat("class [[nodiscard]] f {\npublic:\n  f() {}\n}");
10219   verifyFormat("class [[deprecated(\"so sorry\")]] f {\npublic:\n  f() {}\n}");
10220   verifyFormat("class [[gnu::unused]] f {\npublic:\n  f() {}\n}");
10221   verifyFormat("[[nodiscard]] ::qualified_type f();");
10222 
10223   // Make sure we do not mistake attributes for array subscripts.
10224   verifyFormat("int a() {}\n"
10225                "[[unused]] int b() {}\n");
10226   verifyFormat("NSArray *arr;\n"
10227                "arr[[Foo() bar]];");
10228 
10229   // On the other hand, we still need to correctly find array subscripts.
10230   verifyFormat("int a = std::vector<int>{1, 2, 3}[0];");
10231 
10232   // Make sure that we do not mistake Objective-C method inside array literals
10233   // as attributes, even if those method names are also keywords.
10234   verifyFormat("@[ [foo bar] ];");
10235   verifyFormat("@[ [NSArray class] ];");
10236   verifyFormat("@[ [foo enum] ];");
10237 
10238   verifyFormat("template <typename T> [[nodiscard]] int a() { return 1; }");
10239 
10240   // Make sure we do not parse attributes as lambda introducers.
10241   FormatStyle MultiLineFunctions = getLLVMStyle();
10242   MultiLineFunctions.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
10243   verifyFormat("[[unused]] int b() {\n"
10244                "  return 42;\n"
10245                "}\n",
10246                MultiLineFunctions);
10247 }
10248 
10249 TEST_F(FormatTest, AttributeClass) {
10250   FormatStyle Style = getChromiumStyle(FormatStyle::LK_Cpp);
10251   verifyFormat("class S {\n"
10252                "  S(S&&) = default;\n"
10253                "};",
10254                Style);
10255   verifyFormat("class [[nodiscard]] S {\n"
10256                "  S(S&&) = default;\n"
10257                "};",
10258                Style);
10259   verifyFormat("class __attribute((maybeunused)) S {\n"
10260                "  S(S&&) = default;\n"
10261                "};",
10262                Style);
10263   verifyFormat("struct S {\n"
10264                "  S(S&&) = default;\n"
10265                "};",
10266                Style);
10267   verifyFormat("struct [[nodiscard]] S {\n"
10268                "  S(S&&) = default;\n"
10269                "};",
10270                Style);
10271 }
10272 
10273 TEST_F(FormatTest, AttributesAfterMacro) {
10274   FormatStyle Style = getLLVMStyle();
10275   verifyFormat("MACRO;\n"
10276                "__attribute__((maybe_unused)) int foo() {\n"
10277                "  //...\n"
10278                "}");
10279 
10280   verifyFormat("MACRO;\n"
10281                "[[nodiscard]] int foo() {\n"
10282                "  //...\n"
10283                "}");
10284 
10285   EXPECT_EQ("MACRO\n\n"
10286             "__attribute__((maybe_unused)) int foo() {\n"
10287             "  //...\n"
10288             "}",
10289             format("MACRO\n\n"
10290                    "__attribute__((maybe_unused)) int foo() {\n"
10291                    "  //...\n"
10292                    "}"));
10293 
10294   EXPECT_EQ("MACRO\n\n"
10295             "[[nodiscard]] int foo() {\n"
10296             "  //...\n"
10297             "}",
10298             format("MACRO\n\n"
10299                    "[[nodiscard]] int foo() {\n"
10300                    "  //...\n"
10301                    "}"));
10302 }
10303 
10304 TEST_F(FormatTest, AttributePenaltyBreaking) {
10305   FormatStyle Style = getLLVMStyle();
10306   verifyFormat("void ABCDEFGH::ABCDEFGHIJKLMN(\n"
10307                "    [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}",
10308                Style);
10309   verifyFormat("void ABCDEFGH::ABCDEFGHIJK(\n"
10310                "    [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}",
10311                Style);
10312   verifyFormat("void ABCDEFGH::ABCDEFGH([[maybe_unused]] const "
10313                "shared_ptr<ALongTypeName> &C d) {\n}",
10314                Style);
10315 }
10316 
10317 TEST_F(FormatTest, UnderstandsEllipsis) {
10318   FormatStyle Style = getLLVMStyle();
10319   verifyFormat("int printf(const char *fmt, ...);");
10320   verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
10321   verifyFormat("template <class... Ts> void Foo(Ts *...ts) {}");
10322 
10323   verifyFormat("template <int *...PP> a;", Style);
10324 
10325   Style.PointerAlignment = FormatStyle::PAS_Left;
10326   verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", Style);
10327 
10328   verifyFormat("template <int*... PP> a;", Style);
10329 
10330   Style.PointerAlignment = FormatStyle::PAS_Middle;
10331   verifyFormat("template <int *... PP> a;", Style);
10332 }
10333 
10334 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
10335   EXPECT_EQ("int *a;\n"
10336             "int *a;\n"
10337             "int *a;",
10338             format("int *a;\n"
10339                    "int* a;\n"
10340                    "int *a;",
10341                    getGoogleStyle()));
10342   EXPECT_EQ("int* a;\n"
10343             "int* a;\n"
10344             "int* a;",
10345             format("int* a;\n"
10346                    "int* a;\n"
10347                    "int *a;",
10348                    getGoogleStyle()));
10349   EXPECT_EQ("int *a;\n"
10350             "int *a;\n"
10351             "int *a;",
10352             format("int *a;\n"
10353                    "int * a;\n"
10354                    "int *  a;",
10355                    getGoogleStyle()));
10356   EXPECT_EQ("auto x = [] {\n"
10357             "  int *a;\n"
10358             "  int *a;\n"
10359             "  int *a;\n"
10360             "};",
10361             format("auto x=[]{int *a;\n"
10362                    "int * a;\n"
10363                    "int *  a;};",
10364                    getGoogleStyle()));
10365 }
10366 
10367 TEST_F(FormatTest, UnderstandsRvalueReferences) {
10368   verifyFormat("int f(int &&a) {}");
10369   verifyFormat("int f(int a, char &&b) {}");
10370   verifyFormat("void f() { int &&a = b; }");
10371   verifyGoogleFormat("int f(int a, char&& b) {}");
10372   verifyGoogleFormat("void f() { int&& a = b; }");
10373 
10374   verifyIndependentOfContext("A<int &&> a;");
10375   verifyIndependentOfContext("A<int &&, int &&> a;");
10376   verifyGoogleFormat("A<int&&> a;");
10377   verifyGoogleFormat("A<int&&, int&&> a;");
10378 
10379   // Not rvalue references:
10380   verifyFormat("template <bool B, bool C> class A {\n"
10381                "  static_assert(B && C, \"Something is wrong\");\n"
10382                "};");
10383   verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
10384   verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
10385   verifyFormat("#define A(a, b) (a && b)");
10386 }
10387 
10388 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
10389   verifyFormat("void f() {\n"
10390                "  x[aaaaaaaaa -\n"
10391                "    b] = 23;\n"
10392                "}",
10393                getLLVMStyleWithColumns(15));
10394 }
10395 
10396 TEST_F(FormatTest, FormatsCasts) {
10397   verifyFormat("Type *A = static_cast<Type *>(P);");
10398   verifyFormat("Type *A = (Type *)P;");
10399   verifyFormat("Type *A = (vector<Type *, int *>)P;");
10400   verifyFormat("int a = (int)(2.0f);");
10401   verifyFormat("int a = (int)2.0f;");
10402   verifyFormat("x[(int32)y];");
10403   verifyFormat("x = (int32)y;");
10404   verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
10405   verifyFormat("int a = (int)*b;");
10406   verifyFormat("int a = (int)2.0f;");
10407   verifyFormat("int a = (int)~0;");
10408   verifyFormat("int a = (int)++a;");
10409   verifyFormat("int a = (int)sizeof(int);");
10410   verifyFormat("int a = (int)+2;");
10411   verifyFormat("my_int a = (my_int)2.0f;");
10412   verifyFormat("my_int a = (my_int)sizeof(int);");
10413   verifyFormat("return (my_int)aaa;");
10414   verifyFormat("#define x ((int)-1)");
10415   verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
10416   verifyFormat("#define p(q) ((int *)&q)");
10417   verifyFormat("fn(a)(b) + 1;");
10418 
10419   verifyFormat("void f() { my_int a = (my_int)*b; }");
10420   verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
10421   verifyFormat("my_int a = (my_int)~0;");
10422   verifyFormat("my_int a = (my_int)++a;");
10423   verifyFormat("my_int a = (my_int)-2;");
10424   verifyFormat("my_int a = (my_int)1;");
10425   verifyFormat("my_int a = (my_int *)1;");
10426   verifyFormat("my_int a = (const my_int)-1;");
10427   verifyFormat("my_int a = (const my_int *)-1;");
10428   verifyFormat("my_int a = (my_int)(my_int)-1;");
10429   verifyFormat("my_int a = (ns::my_int)-2;");
10430   verifyFormat("case (my_int)ONE:");
10431   verifyFormat("auto x = (X)this;");
10432   // Casts in Obj-C style calls used to not be recognized as such.
10433   verifyFormat("int a = [(type*)[((type*)val) arg] arg];", getGoogleStyle());
10434 
10435   // FIXME: single value wrapped with paren will be treated as cast.
10436   verifyFormat("void f(int i = (kValue)*kMask) {}");
10437 
10438   verifyFormat("{ (void)F; }");
10439 
10440   // Don't break after a cast's
10441   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
10442                "    (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
10443                "                                   bbbbbbbbbbbbbbbbbbbbbb);");
10444 
10445   verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(x)");
10446   verifyFormat("#define CONF_BOOL(x) (bool *)(x)");
10447   verifyFormat("#define CONF_BOOL(x) (bool)(x)");
10448   verifyFormat("bool *y = (bool *)(void *)(x);");
10449   verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(int)(x)");
10450   verifyFormat("bool *y = (bool *)(void *)(int)(x);");
10451   verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(int)foo(x)");
10452   verifyFormat("bool *y = (bool *)(void *)(int)foo(x);");
10453 
10454   // These are not casts.
10455   verifyFormat("void f(int *) {}");
10456   verifyFormat("f(foo)->b;");
10457   verifyFormat("f(foo).b;");
10458   verifyFormat("f(foo)(b);");
10459   verifyFormat("f(foo)[b];");
10460   verifyFormat("[](foo) { return 4; }(bar);");
10461   verifyFormat("(*funptr)(foo)[4];");
10462   verifyFormat("funptrs[4](foo)[4];");
10463   verifyFormat("void f(int *);");
10464   verifyFormat("void f(int *) = 0;");
10465   verifyFormat("void f(SmallVector<int>) {}");
10466   verifyFormat("void f(SmallVector<int>);");
10467   verifyFormat("void f(SmallVector<int>) = 0;");
10468   verifyFormat("void f(int i = (kA * kB) & kMask) {}");
10469   verifyFormat("int a = sizeof(int) * b;");
10470   verifyFormat("int a = alignof(int) * b;", getGoogleStyle());
10471   verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
10472   verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
10473   verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
10474 
10475   // These are not casts, but at some point were confused with casts.
10476   verifyFormat("virtual void foo(int *) override;");
10477   verifyFormat("virtual void foo(char &) const;");
10478   verifyFormat("virtual void foo(int *a, char *) const;");
10479   verifyFormat("int a = sizeof(int *) + b;");
10480   verifyFormat("int a = alignof(int *) + b;", getGoogleStyle());
10481   verifyFormat("bool b = f(g<int>) && c;");
10482   verifyFormat("typedef void (*f)(int i) func;");
10483   verifyFormat("void operator++(int) noexcept;");
10484   verifyFormat("void operator++(int &) noexcept;");
10485   verifyFormat("void operator delete(void *, std::size_t, const std::nothrow_t "
10486                "&) noexcept;");
10487   verifyFormat(
10488       "void operator delete(std::size_t, const std::nothrow_t &) noexcept;");
10489   verifyFormat("void operator delete(const std::nothrow_t &) noexcept;");
10490   verifyFormat("void operator delete(std::nothrow_t &) noexcept;");
10491   verifyFormat("void operator delete(nothrow_t &) noexcept;");
10492   verifyFormat("void operator delete(foo &) noexcept;");
10493   verifyFormat("void operator delete(foo) noexcept;");
10494   verifyFormat("void operator delete(int) noexcept;");
10495   verifyFormat("void operator delete(int &) noexcept;");
10496   verifyFormat("void operator delete(int &) volatile noexcept;");
10497   verifyFormat("void operator delete(int &) const");
10498   verifyFormat("void operator delete(int &) = default");
10499   verifyFormat("void operator delete(int &) = delete");
10500   verifyFormat("void operator delete(int &) [[noreturn]]");
10501   verifyFormat("void operator delete(int &) throw();");
10502   verifyFormat("void operator delete(int &) throw(int);");
10503   verifyFormat("auto operator delete(int &) -> int;");
10504   verifyFormat("auto operator delete(int &) override");
10505   verifyFormat("auto operator delete(int &) final");
10506 
10507   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
10508                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
10509   // FIXME: The indentation here is not ideal.
10510   verifyFormat(
10511       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10512       "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
10513       "        [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
10514 }
10515 
10516 TEST_F(FormatTest, FormatsFunctionTypes) {
10517   verifyFormat("A<bool()> a;");
10518   verifyFormat("A<SomeType()> a;");
10519   verifyFormat("A<void (*)(int, std::string)> a;");
10520   verifyFormat("A<void *(int)>;");
10521   verifyFormat("void *(*a)(int *, SomeType *);");
10522   verifyFormat("int (*func)(void *);");
10523   verifyFormat("void f() { int (*func)(void *); }");
10524   verifyFormat("template <class CallbackClass>\n"
10525                "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
10526 
10527   verifyGoogleFormat("A<void*(int*, SomeType*)>;");
10528   verifyGoogleFormat("void* (*a)(int);");
10529   verifyGoogleFormat(
10530       "template <class CallbackClass>\n"
10531       "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
10532 
10533   // Other constructs can look somewhat like function types:
10534   verifyFormat("A<sizeof(*x)> a;");
10535   verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
10536   verifyFormat("some_var = function(*some_pointer_var)[0];");
10537   verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
10538   verifyFormat("int x = f(&h)();");
10539   verifyFormat("returnsFunction(&param1, &param2)(param);");
10540   verifyFormat("std::function<\n"
10541                "    LooooooooooongTemplatedType<\n"
10542                "        SomeType>*(\n"
10543                "        LooooooooooooooooongType type)>\n"
10544                "    function;",
10545                getGoogleStyleWithColumns(40));
10546 }
10547 
10548 TEST_F(FormatTest, FormatsPointersToArrayTypes) {
10549   verifyFormat("A (*foo_)[6];");
10550   verifyFormat("vector<int> (*foo_)[6];");
10551 }
10552 
10553 TEST_F(FormatTest, BreaksLongVariableDeclarations) {
10554   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
10555                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
10556   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
10557                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
10558   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
10559                "    *LoooooooooooooooooooooooooooooooooooooooongVariable;");
10560 
10561   // Different ways of ()-initializiation.
10562   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
10563                "    LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
10564   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
10565                "    LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
10566   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
10567                "    LoooooooooooooooooooooooooooooooooooooooongVariable({});");
10568   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
10569                "    LoooooooooooooooooooooooooooooooooooooongVariable([A a]);");
10570 
10571   // Lambdas should not confuse the variable declaration heuristic.
10572   verifyFormat("LooooooooooooooooongType\n"
10573                "    variable(nullptr, [](A *a) {});",
10574                getLLVMStyleWithColumns(40));
10575 }
10576 
10577 TEST_F(FormatTest, BreaksLongDeclarations) {
10578   verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
10579                "    AnotherNameForTheLongType;");
10580   verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
10581                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10582   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10583                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
10584   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n"
10585                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
10586   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10587                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10588   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
10589                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10590   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
10591                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10592   verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
10593                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10594   verifyFormat("typeof(LoooooooooooooooooooooooooooooooooooooooooongName)\n"
10595                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10596   verifyFormat("_Atomic(LooooooooooooooooooooooooooooooooooooooooongName)\n"
10597                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10598   verifyFormat("__underlying_type(LooooooooooooooooooooooooooooooongName)\n"
10599                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10600   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10601                "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);");
10602   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10603                "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}");
10604   FormatStyle Indented = getLLVMStyle();
10605   Indented.IndentWrappedFunctionNames = true;
10606   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10607                "    LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
10608                Indented);
10609   verifyFormat(
10610       "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10611       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
10612       Indented);
10613   verifyFormat(
10614       "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
10615       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
10616       Indented);
10617   verifyFormat(
10618       "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
10619       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
10620       Indented);
10621 
10622   // FIXME: Without the comment, this breaks after "(".
10623   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType  // break\n"
10624                "    (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();",
10625                getGoogleStyle());
10626 
10627   verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
10628                "                  int LoooooooooooooooooooongParam2) {}");
10629   verifyFormat(
10630       "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
10631       "                                   SourceLocation L, IdentifierIn *II,\n"
10632       "                                   Type *T) {}");
10633   verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
10634                "ReallyReaaallyLongFunctionName(\n"
10635                "    const std::string &SomeParameter,\n"
10636                "    const SomeType<string, SomeOtherTemplateParameter>\n"
10637                "        &ReallyReallyLongParameterName,\n"
10638                "    const SomeType<string, SomeOtherTemplateParameter>\n"
10639                "        &AnotherLongParameterName) {}");
10640   verifyFormat("template <typename A>\n"
10641                "SomeLoooooooooooooooooooooongType<\n"
10642                "    typename some_namespace::SomeOtherType<A>::Type>\n"
10643                "Function() {}");
10644 
10645   verifyGoogleFormat(
10646       "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
10647       "    aaaaaaaaaaaaaaaaaaaaaaa;");
10648   verifyGoogleFormat(
10649       "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
10650       "                                   SourceLocation L) {}");
10651   verifyGoogleFormat(
10652       "some_namespace::LongReturnType\n"
10653       "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
10654       "    int first_long_parameter, int second_parameter) {}");
10655 
10656   verifyGoogleFormat("template <typename T>\n"
10657                      "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
10658                      "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
10659   verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10660                      "                   int aaaaaaaaaaaaaaaaaaaaaaa);");
10661 
10662   verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
10663                "    const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10664                "        *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
10665   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10666                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
10667                "        aaaaaaaaaaaaaaaaaaaaaaaa);");
10668   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10669                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
10670                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n"
10671                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
10672 
10673   verifyFormat("template <typename T> // Templates on own line.\n"
10674                "static int            // Some comment.\n"
10675                "MyFunction(int a);",
10676                getLLVMStyle());
10677 }
10678 
10679 TEST_F(FormatTest, FormatsAccessModifiers) {
10680   FormatStyle Style = getLLVMStyle();
10681   EXPECT_EQ(Style.EmptyLineBeforeAccessModifier,
10682             FormatStyle::ELBAMS_LogicalBlock);
10683   verifyFormat("struct foo {\n"
10684                "private:\n"
10685                "  void f() {}\n"
10686                "\n"
10687                "private:\n"
10688                "  int i;\n"
10689                "\n"
10690                "protected:\n"
10691                "  int j;\n"
10692                "};\n",
10693                Style);
10694   verifyFormat("struct foo {\n"
10695                "private:\n"
10696                "  void f() {}\n"
10697                "\n"
10698                "private:\n"
10699                "  int i;\n"
10700                "\n"
10701                "protected:\n"
10702                "  int j;\n"
10703                "};\n",
10704                "struct foo {\n"
10705                "private:\n"
10706                "  void f() {}\n"
10707                "private:\n"
10708                "  int i;\n"
10709                "protected:\n"
10710                "  int j;\n"
10711                "};\n",
10712                Style);
10713   verifyFormat("struct foo { /* comment */\n"
10714                "private:\n"
10715                "  int i;\n"
10716                "  // comment\n"
10717                "private:\n"
10718                "  int j;\n"
10719                "};\n",
10720                Style);
10721   verifyFormat("struct foo {\n"
10722                "#ifdef FOO\n"
10723                "#endif\n"
10724                "private:\n"
10725                "  int i;\n"
10726                "#ifdef FOO\n"
10727                "private:\n"
10728                "#endif\n"
10729                "  int j;\n"
10730                "};\n",
10731                Style);
10732   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
10733   verifyFormat("struct foo {\n"
10734                "private:\n"
10735                "  void f() {}\n"
10736                "private:\n"
10737                "  int i;\n"
10738                "protected:\n"
10739                "  int j;\n"
10740                "};\n",
10741                Style);
10742   verifyFormat("struct foo {\n"
10743                "private:\n"
10744                "  void f() {}\n"
10745                "private:\n"
10746                "  int i;\n"
10747                "protected:\n"
10748                "  int j;\n"
10749                "};\n",
10750                "struct foo {\n"
10751                "\n"
10752                "private:\n"
10753                "  void f() {}\n"
10754                "\n"
10755                "private:\n"
10756                "  int i;\n"
10757                "\n"
10758                "protected:\n"
10759                "  int j;\n"
10760                "};\n",
10761                Style);
10762   verifyFormat("struct foo { /* comment */\n"
10763                "private:\n"
10764                "  int i;\n"
10765                "  // comment\n"
10766                "private:\n"
10767                "  int j;\n"
10768                "};\n",
10769                "struct foo { /* comment */\n"
10770                "\n"
10771                "private:\n"
10772                "  int i;\n"
10773                "  // comment\n"
10774                "\n"
10775                "private:\n"
10776                "  int j;\n"
10777                "};\n",
10778                Style);
10779   verifyFormat("struct foo {\n"
10780                "#ifdef FOO\n"
10781                "#endif\n"
10782                "private:\n"
10783                "  int i;\n"
10784                "#ifdef FOO\n"
10785                "private:\n"
10786                "#endif\n"
10787                "  int j;\n"
10788                "};\n",
10789                "struct foo {\n"
10790                "#ifdef FOO\n"
10791                "#endif\n"
10792                "\n"
10793                "private:\n"
10794                "  int i;\n"
10795                "#ifdef FOO\n"
10796                "\n"
10797                "private:\n"
10798                "#endif\n"
10799                "  int j;\n"
10800                "};\n",
10801                Style);
10802   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
10803   verifyFormat("struct foo {\n"
10804                "private:\n"
10805                "  void f() {}\n"
10806                "\n"
10807                "private:\n"
10808                "  int i;\n"
10809                "\n"
10810                "protected:\n"
10811                "  int j;\n"
10812                "};\n",
10813                Style);
10814   verifyFormat("struct foo {\n"
10815                "private:\n"
10816                "  void f() {}\n"
10817                "\n"
10818                "private:\n"
10819                "  int i;\n"
10820                "\n"
10821                "protected:\n"
10822                "  int j;\n"
10823                "};\n",
10824                "struct foo {\n"
10825                "private:\n"
10826                "  void f() {}\n"
10827                "private:\n"
10828                "  int i;\n"
10829                "protected:\n"
10830                "  int j;\n"
10831                "};\n",
10832                Style);
10833   verifyFormat("struct foo { /* comment */\n"
10834                "private:\n"
10835                "  int i;\n"
10836                "  // comment\n"
10837                "\n"
10838                "private:\n"
10839                "  int j;\n"
10840                "};\n",
10841                "struct foo { /* comment */\n"
10842                "private:\n"
10843                "  int i;\n"
10844                "  // comment\n"
10845                "\n"
10846                "private:\n"
10847                "  int j;\n"
10848                "};\n",
10849                Style);
10850   verifyFormat("struct foo {\n"
10851                "#ifdef FOO\n"
10852                "#endif\n"
10853                "\n"
10854                "private:\n"
10855                "  int i;\n"
10856                "#ifdef FOO\n"
10857                "\n"
10858                "private:\n"
10859                "#endif\n"
10860                "  int j;\n"
10861                "};\n",
10862                "struct foo {\n"
10863                "#ifdef FOO\n"
10864                "#endif\n"
10865                "private:\n"
10866                "  int i;\n"
10867                "#ifdef FOO\n"
10868                "private:\n"
10869                "#endif\n"
10870                "  int j;\n"
10871                "};\n",
10872                Style);
10873   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
10874   EXPECT_EQ("struct foo {\n"
10875             "\n"
10876             "private:\n"
10877             "  void f() {}\n"
10878             "\n"
10879             "private:\n"
10880             "  int i;\n"
10881             "\n"
10882             "protected:\n"
10883             "  int j;\n"
10884             "};\n",
10885             format("struct foo {\n"
10886                    "\n"
10887                    "private:\n"
10888                    "  void f() {}\n"
10889                    "\n"
10890                    "private:\n"
10891                    "  int i;\n"
10892                    "\n"
10893                    "protected:\n"
10894                    "  int j;\n"
10895                    "};\n",
10896                    Style));
10897   verifyFormat("struct foo {\n"
10898                "private:\n"
10899                "  void f() {}\n"
10900                "private:\n"
10901                "  int i;\n"
10902                "protected:\n"
10903                "  int j;\n"
10904                "};\n",
10905                Style);
10906   EXPECT_EQ("struct foo { /* comment */\n"
10907             "\n"
10908             "private:\n"
10909             "  int i;\n"
10910             "  // comment\n"
10911             "\n"
10912             "private:\n"
10913             "  int j;\n"
10914             "};\n",
10915             format("struct foo { /* comment */\n"
10916                    "\n"
10917                    "private:\n"
10918                    "  int i;\n"
10919                    "  // comment\n"
10920                    "\n"
10921                    "private:\n"
10922                    "  int j;\n"
10923                    "};\n",
10924                    Style));
10925   verifyFormat("struct foo { /* comment */\n"
10926                "private:\n"
10927                "  int i;\n"
10928                "  // comment\n"
10929                "private:\n"
10930                "  int j;\n"
10931                "};\n",
10932                Style);
10933   EXPECT_EQ("struct foo {\n"
10934             "#ifdef FOO\n"
10935             "#endif\n"
10936             "\n"
10937             "private:\n"
10938             "  int i;\n"
10939             "#ifdef FOO\n"
10940             "\n"
10941             "private:\n"
10942             "#endif\n"
10943             "  int j;\n"
10944             "};\n",
10945             format("struct foo {\n"
10946                    "#ifdef FOO\n"
10947                    "#endif\n"
10948                    "\n"
10949                    "private:\n"
10950                    "  int i;\n"
10951                    "#ifdef FOO\n"
10952                    "\n"
10953                    "private:\n"
10954                    "#endif\n"
10955                    "  int j;\n"
10956                    "};\n",
10957                    Style));
10958   verifyFormat("struct foo {\n"
10959                "#ifdef FOO\n"
10960                "#endif\n"
10961                "private:\n"
10962                "  int i;\n"
10963                "#ifdef FOO\n"
10964                "private:\n"
10965                "#endif\n"
10966                "  int j;\n"
10967                "};\n",
10968                Style);
10969 
10970   FormatStyle NoEmptyLines = getLLVMStyle();
10971   NoEmptyLines.MaxEmptyLinesToKeep = 0;
10972   verifyFormat("struct foo {\n"
10973                "private:\n"
10974                "  void f() {}\n"
10975                "\n"
10976                "private:\n"
10977                "  int i;\n"
10978                "\n"
10979                "public:\n"
10980                "protected:\n"
10981                "  int j;\n"
10982                "};\n",
10983                NoEmptyLines);
10984 
10985   NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
10986   verifyFormat("struct foo {\n"
10987                "private:\n"
10988                "  void f() {}\n"
10989                "private:\n"
10990                "  int i;\n"
10991                "public:\n"
10992                "protected:\n"
10993                "  int j;\n"
10994                "};\n",
10995                NoEmptyLines);
10996 
10997   NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
10998   verifyFormat("struct foo {\n"
10999                "private:\n"
11000                "  void f() {}\n"
11001                "\n"
11002                "private:\n"
11003                "  int i;\n"
11004                "\n"
11005                "public:\n"
11006                "\n"
11007                "protected:\n"
11008                "  int j;\n"
11009                "};\n",
11010                NoEmptyLines);
11011 }
11012 
11013 TEST_F(FormatTest, FormatsAfterAccessModifiers) {
11014 
11015   FormatStyle Style = getLLVMStyle();
11016   EXPECT_EQ(Style.EmptyLineAfterAccessModifier, FormatStyle::ELAAMS_Never);
11017   verifyFormat("struct foo {\n"
11018                "private:\n"
11019                "  void f() {}\n"
11020                "\n"
11021                "private:\n"
11022                "  int i;\n"
11023                "\n"
11024                "protected:\n"
11025                "  int j;\n"
11026                "};\n",
11027                Style);
11028 
11029   // Check if lines are removed.
11030   verifyFormat("struct foo {\n"
11031                "private:\n"
11032                "  void f() {}\n"
11033                "\n"
11034                "private:\n"
11035                "  int i;\n"
11036                "\n"
11037                "protected:\n"
11038                "  int j;\n"
11039                "};\n",
11040                "struct foo {\n"
11041                "private:\n"
11042                "\n"
11043                "  void f() {}\n"
11044                "\n"
11045                "private:\n"
11046                "\n"
11047                "  int i;\n"
11048                "\n"
11049                "protected:\n"
11050                "\n"
11051                "  int j;\n"
11052                "};\n",
11053                Style);
11054 
11055   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
11056   verifyFormat("struct foo {\n"
11057                "private:\n"
11058                "\n"
11059                "  void f() {}\n"
11060                "\n"
11061                "private:\n"
11062                "\n"
11063                "  int i;\n"
11064                "\n"
11065                "protected:\n"
11066                "\n"
11067                "  int j;\n"
11068                "};\n",
11069                Style);
11070 
11071   // Check if lines are added.
11072   verifyFormat("struct foo {\n"
11073                "private:\n"
11074                "\n"
11075                "  void f() {}\n"
11076                "\n"
11077                "private:\n"
11078                "\n"
11079                "  int i;\n"
11080                "\n"
11081                "protected:\n"
11082                "\n"
11083                "  int j;\n"
11084                "};\n",
11085                "struct foo {\n"
11086                "private:\n"
11087                "  void f() {}\n"
11088                "\n"
11089                "private:\n"
11090                "  int i;\n"
11091                "\n"
11092                "protected:\n"
11093                "  int j;\n"
11094                "};\n",
11095                Style);
11096 
11097   // Leave tests rely on the code layout, test::messUp can not be used.
11098   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
11099   Style.MaxEmptyLinesToKeep = 0u;
11100   verifyFormat("struct foo {\n"
11101                "private:\n"
11102                "  void f() {}\n"
11103                "\n"
11104                "private:\n"
11105                "  int i;\n"
11106                "\n"
11107                "protected:\n"
11108                "  int j;\n"
11109                "};\n",
11110                Style);
11111 
11112   // Check if MaxEmptyLinesToKeep is respected.
11113   EXPECT_EQ("struct foo {\n"
11114             "private:\n"
11115             "  void f() {}\n"
11116             "\n"
11117             "private:\n"
11118             "  int i;\n"
11119             "\n"
11120             "protected:\n"
11121             "  int j;\n"
11122             "};\n",
11123             format("struct foo {\n"
11124                    "private:\n"
11125                    "\n\n\n"
11126                    "  void f() {}\n"
11127                    "\n"
11128                    "private:\n"
11129                    "\n\n\n"
11130                    "  int i;\n"
11131                    "\n"
11132                    "protected:\n"
11133                    "\n\n\n"
11134                    "  int j;\n"
11135                    "};\n",
11136                    Style));
11137 
11138   Style.MaxEmptyLinesToKeep = 1u;
11139   EXPECT_EQ("struct foo {\n"
11140             "private:\n"
11141             "\n"
11142             "  void f() {}\n"
11143             "\n"
11144             "private:\n"
11145             "\n"
11146             "  int i;\n"
11147             "\n"
11148             "protected:\n"
11149             "\n"
11150             "  int j;\n"
11151             "};\n",
11152             format("struct foo {\n"
11153                    "private:\n"
11154                    "\n"
11155                    "  void f() {}\n"
11156                    "\n"
11157                    "private:\n"
11158                    "\n"
11159                    "  int i;\n"
11160                    "\n"
11161                    "protected:\n"
11162                    "\n"
11163                    "  int j;\n"
11164                    "};\n",
11165                    Style));
11166   // Check if no lines are kept.
11167   EXPECT_EQ("struct foo {\n"
11168             "private:\n"
11169             "  void f() {}\n"
11170             "\n"
11171             "private:\n"
11172             "  int i;\n"
11173             "\n"
11174             "protected:\n"
11175             "  int j;\n"
11176             "};\n",
11177             format("struct foo {\n"
11178                    "private:\n"
11179                    "  void f() {}\n"
11180                    "\n"
11181                    "private:\n"
11182                    "  int i;\n"
11183                    "\n"
11184                    "protected:\n"
11185                    "  int j;\n"
11186                    "};\n",
11187                    Style));
11188   // Check if MaxEmptyLinesToKeep is respected.
11189   EXPECT_EQ("struct foo {\n"
11190             "private:\n"
11191             "\n"
11192             "  void f() {}\n"
11193             "\n"
11194             "private:\n"
11195             "\n"
11196             "  int i;\n"
11197             "\n"
11198             "protected:\n"
11199             "\n"
11200             "  int j;\n"
11201             "};\n",
11202             format("struct foo {\n"
11203                    "private:\n"
11204                    "\n\n\n"
11205                    "  void f() {}\n"
11206                    "\n"
11207                    "private:\n"
11208                    "\n\n\n"
11209                    "  int i;\n"
11210                    "\n"
11211                    "protected:\n"
11212                    "\n\n\n"
11213                    "  int j;\n"
11214                    "};\n",
11215                    Style));
11216 
11217   Style.MaxEmptyLinesToKeep = 10u;
11218   EXPECT_EQ("struct foo {\n"
11219             "private:\n"
11220             "\n\n\n"
11221             "  void f() {}\n"
11222             "\n"
11223             "private:\n"
11224             "\n\n\n"
11225             "  int i;\n"
11226             "\n"
11227             "protected:\n"
11228             "\n\n\n"
11229             "  int j;\n"
11230             "};\n",
11231             format("struct foo {\n"
11232                    "private:\n"
11233                    "\n\n\n"
11234                    "  void f() {}\n"
11235                    "\n"
11236                    "private:\n"
11237                    "\n\n\n"
11238                    "  int i;\n"
11239                    "\n"
11240                    "protected:\n"
11241                    "\n\n\n"
11242                    "  int j;\n"
11243                    "};\n",
11244                    Style));
11245 
11246   // Test with comments.
11247   Style = getLLVMStyle();
11248   verifyFormat("struct foo {\n"
11249                "private:\n"
11250                "  // comment\n"
11251                "  void f() {}\n"
11252                "\n"
11253                "private: /* comment */\n"
11254                "  int i;\n"
11255                "};\n",
11256                Style);
11257   verifyFormat("struct foo {\n"
11258                "private:\n"
11259                "  // comment\n"
11260                "  void f() {}\n"
11261                "\n"
11262                "private: /* comment */\n"
11263                "  int i;\n"
11264                "};\n",
11265                "struct foo {\n"
11266                "private:\n"
11267                "\n"
11268                "  // comment\n"
11269                "  void f() {}\n"
11270                "\n"
11271                "private: /* comment */\n"
11272                "\n"
11273                "  int i;\n"
11274                "};\n",
11275                Style);
11276 
11277   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
11278   verifyFormat("struct foo {\n"
11279                "private:\n"
11280                "\n"
11281                "  // comment\n"
11282                "  void f() {}\n"
11283                "\n"
11284                "private: /* comment */\n"
11285                "\n"
11286                "  int i;\n"
11287                "};\n",
11288                "struct foo {\n"
11289                "private:\n"
11290                "  // comment\n"
11291                "  void f() {}\n"
11292                "\n"
11293                "private: /* comment */\n"
11294                "  int i;\n"
11295                "};\n",
11296                Style);
11297   verifyFormat("struct foo {\n"
11298                "private:\n"
11299                "\n"
11300                "  // comment\n"
11301                "  void f() {}\n"
11302                "\n"
11303                "private: /* comment */\n"
11304                "\n"
11305                "  int i;\n"
11306                "};\n",
11307                Style);
11308 
11309   // Test with preprocessor defines.
11310   Style = getLLVMStyle();
11311   verifyFormat("struct foo {\n"
11312                "private:\n"
11313                "#ifdef FOO\n"
11314                "#endif\n"
11315                "  void f() {}\n"
11316                "};\n",
11317                Style);
11318   verifyFormat("struct foo {\n"
11319                "private:\n"
11320                "#ifdef FOO\n"
11321                "#endif\n"
11322                "  void f() {}\n"
11323                "};\n",
11324                "struct foo {\n"
11325                "private:\n"
11326                "\n"
11327                "#ifdef FOO\n"
11328                "#endif\n"
11329                "  void f() {}\n"
11330                "};\n",
11331                Style);
11332 
11333   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
11334   verifyFormat("struct foo {\n"
11335                "private:\n"
11336                "\n"
11337                "#ifdef FOO\n"
11338                "#endif\n"
11339                "  void f() {}\n"
11340                "};\n",
11341                "struct foo {\n"
11342                "private:\n"
11343                "#ifdef FOO\n"
11344                "#endif\n"
11345                "  void f() {}\n"
11346                "};\n",
11347                Style);
11348   verifyFormat("struct foo {\n"
11349                "private:\n"
11350                "\n"
11351                "#ifdef FOO\n"
11352                "#endif\n"
11353                "  void f() {}\n"
11354                "};\n",
11355                Style);
11356 }
11357 
11358 TEST_F(FormatTest, FormatsAfterAndBeforeAccessModifiersInteraction) {
11359   // Combined tests of EmptyLineAfterAccessModifier and
11360   // EmptyLineBeforeAccessModifier.
11361   FormatStyle Style = getLLVMStyle();
11362   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
11363   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
11364   verifyFormat("struct foo {\n"
11365                "private:\n"
11366                "\n"
11367                "protected:\n"
11368                "};\n",
11369                Style);
11370 
11371   Style.MaxEmptyLinesToKeep = 10u;
11372   // Both remove all new lines.
11373   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
11374   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
11375   verifyFormat("struct foo {\n"
11376                "private:\n"
11377                "protected:\n"
11378                "};\n",
11379                "struct foo {\n"
11380                "private:\n"
11381                "\n\n\n"
11382                "protected:\n"
11383                "};\n",
11384                Style);
11385 
11386   // Leave tests rely on the code layout, test::messUp can not be used.
11387   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
11388   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
11389   Style.MaxEmptyLinesToKeep = 10u;
11390   EXPECT_EQ("struct foo {\n"
11391             "private:\n"
11392             "\n\n\n"
11393             "protected:\n"
11394             "};\n",
11395             format("struct foo {\n"
11396                    "private:\n"
11397                    "\n\n\n"
11398                    "protected:\n"
11399                    "};\n",
11400                    Style));
11401   Style.MaxEmptyLinesToKeep = 3u;
11402   EXPECT_EQ("struct foo {\n"
11403             "private:\n"
11404             "\n\n\n"
11405             "protected:\n"
11406             "};\n",
11407             format("struct foo {\n"
11408                    "private:\n"
11409                    "\n\n\n"
11410                    "protected:\n"
11411                    "};\n",
11412                    Style));
11413   Style.MaxEmptyLinesToKeep = 1u;
11414   EXPECT_EQ("struct foo {\n"
11415             "private:\n"
11416             "\n\n\n"
11417             "protected:\n"
11418             "};\n",
11419             format("struct foo {\n"
11420                    "private:\n"
11421                    "\n\n\n"
11422                    "protected:\n"
11423                    "};\n",
11424                    Style)); // Based on new lines in original document and not
11425                             // on the setting.
11426 
11427   Style.MaxEmptyLinesToKeep = 10u;
11428   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
11429   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
11430   // Newlines are kept if they are greater than zero,
11431   // test::messUp removes all new lines which changes the logic
11432   EXPECT_EQ("struct foo {\n"
11433             "private:\n"
11434             "\n\n\n"
11435             "protected:\n"
11436             "};\n",
11437             format("struct foo {\n"
11438                    "private:\n"
11439                    "\n\n\n"
11440                    "protected:\n"
11441                    "};\n",
11442                    Style));
11443 
11444   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
11445   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
11446   // test::messUp removes all new lines which changes the logic
11447   EXPECT_EQ("struct foo {\n"
11448             "private:\n"
11449             "\n\n\n"
11450             "protected:\n"
11451             "};\n",
11452             format("struct foo {\n"
11453                    "private:\n"
11454                    "\n\n\n"
11455                    "protected:\n"
11456                    "};\n",
11457                    Style));
11458 
11459   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
11460   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
11461   EXPECT_EQ("struct foo {\n"
11462             "private:\n"
11463             "\n\n\n"
11464             "protected:\n"
11465             "};\n",
11466             format("struct foo {\n"
11467                    "private:\n"
11468                    "\n\n\n"
11469                    "protected:\n"
11470                    "};\n",
11471                    Style)); // test::messUp removes all new lines which changes
11472                             // the logic.
11473 
11474   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
11475   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
11476   verifyFormat("struct foo {\n"
11477                "private:\n"
11478                "protected:\n"
11479                "};\n",
11480                "struct foo {\n"
11481                "private:\n"
11482                "\n\n\n"
11483                "protected:\n"
11484                "};\n",
11485                Style);
11486 
11487   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
11488   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
11489   EXPECT_EQ("struct foo {\n"
11490             "private:\n"
11491             "\n\n\n"
11492             "protected:\n"
11493             "};\n",
11494             format("struct foo {\n"
11495                    "private:\n"
11496                    "\n\n\n"
11497                    "protected:\n"
11498                    "};\n",
11499                    Style)); // test::messUp removes all new lines which changes
11500                             // the logic.
11501 
11502   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
11503   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
11504   verifyFormat("struct foo {\n"
11505                "private:\n"
11506                "protected:\n"
11507                "};\n",
11508                "struct foo {\n"
11509                "private:\n"
11510                "\n\n\n"
11511                "protected:\n"
11512                "};\n",
11513                Style);
11514 
11515   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
11516   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
11517   verifyFormat("struct foo {\n"
11518                "private:\n"
11519                "protected:\n"
11520                "};\n",
11521                "struct foo {\n"
11522                "private:\n"
11523                "\n\n\n"
11524                "protected:\n"
11525                "};\n",
11526                Style);
11527 
11528   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
11529   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
11530   verifyFormat("struct foo {\n"
11531                "private:\n"
11532                "protected:\n"
11533                "};\n",
11534                "struct foo {\n"
11535                "private:\n"
11536                "\n\n\n"
11537                "protected:\n"
11538                "};\n",
11539                Style);
11540 
11541   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
11542   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
11543   verifyFormat("struct foo {\n"
11544                "private:\n"
11545                "protected:\n"
11546                "};\n",
11547                "struct foo {\n"
11548                "private:\n"
11549                "\n\n\n"
11550                "protected:\n"
11551                "};\n",
11552                Style);
11553 }
11554 
11555 TEST_F(FormatTest, FormatsArrays) {
11556   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
11557                "                         [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
11558   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n"
11559                "                         [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;");
11560   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n"
11561                "    aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}");
11562   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
11563                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
11564   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
11565                "    [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
11566   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
11567                "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
11568                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
11569   verifyFormat(
11570       "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
11571       "             << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
11572       "                                  [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
11573   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n"
11574                "    .aaaaaaaaaaaaaaaaaaaaaa();");
11575 
11576   verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
11577                      "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
11578   verifyFormat(
11579       "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
11580       "                                  .aaaaaaa[0]\n"
11581       "                                  .aaaaaaaaaaaaaaaaaaaaaa();");
11582   verifyFormat("a[::b::c];");
11583 
11584   verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10));
11585 
11586   FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0);
11587   verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit);
11588 }
11589 
11590 TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
11591   verifyFormat("(a)->b();");
11592   verifyFormat("--a;");
11593 }
11594 
11595 TEST_F(FormatTest, HandlesIncludeDirectives) {
11596   verifyFormat("#include <string>\n"
11597                "#include <a/b/c.h>\n"
11598                "#include \"a/b/string\"\n"
11599                "#include \"string.h\"\n"
11600                "#include \"string.h\"\n"
11601                "#include <a-a>\n"
11602                "#include < path with space >\n"
11603                "#include_next <test.h>"
11604                "#include \"abc.h\" // this is included for ABC\n"
11605                "#include \"some long include\" // with a comment\n"
11606                "#include \"some very long include path\"\n"
11607                "#include <some/very/long/include/path>\n",
11608                getLLVMStyleWithColumns(35));
11609   EXPECT_EQ("#include \"a.h\"", format("#include  \"a.h\""));
11610   EXPECT_EQ("#include <a>", format("#include<a>"));
11611 
11612   verifyFormat("#import <string>");
11613   verifyFormat("#import <a/b/c.h>");
11614   verifyFormat("#import \"a/b/string\"");
11615   verifyFormat("#import \"string.h\"");
11616   verifyFormat("#import \"string.h\"");
11617   verifyFormat("#if __has_include(<strstream>)\n"
11618                "#include <strstream>\n"
11619                "#endif");
11620 
11621   verifyFormat("#define MY_IMPORT <a/b>");
11622 
11623   verifyFormat("#if __has_include(<a/b>)");
11624   verifyFormat("#if __has_include_next(<a/b>)");
11625   verifyFormat("#define F __has_include(<a/b>)");
11626   verifyFormat("#define F __has_include_next(<a/b>)");
11627 
11628   // Protocol buffer definition or missing "#".
11629   verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
11630                getLLVMStyleWithColumns(30));
11631 
11632   FormatStyle Style = getLLVMStyle();
11633   Style.AlwaysBreakBeforeMultilineStrings = true;
11634   Style.ColumnLimit = 0;
11635   verifyFormat("#import \"abc.h\"", Style);
11636 
11637   // But 'import' might also be a regular C++ namespace.
11638   verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
11639                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
11640 }
11641 
11642 //===----------------------------------------------------------------------===//
11643 // Error recovery tests.
11644 //===----------------------------------------------------------------------===//
11645 
11646 TEST_F(FormatTest, IncompleteParameterLists) {
11647   FormatStyle NoBinPacking = getLLVMStyle();
11648   NoBinPacking.BinPackParameters = false;
11649   verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
11650                "                        double *min_x,\n"
11651                "                        double *max_x,\n"
11652                "                        double *min_y,\n"
11653                "                        double *max_y,\n"
11654                "                        double *min_z,\n"
11655                "                        double *max_z, ) {}",
11656                NoBinPacking);
11657 }
11658 
11659 TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
11660   verifyFormat("void f() { return; }\n42");
11661   verifyFormat("void f() {\n"
11662                "  if (0)\n"
11663                "    return;\n"
11664                "}\n"
11665                "42");
11666   verifyFormat("void f() { return }\n42");
11667   verifyFormat("void f() {\n"
11668                "  if (0)\n"
11669                "    return\n"
11670                "}\n"
11671                "42");
11672 }
11673 
11674 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
11675   EXPECT_EQ("void f() { return }", format("void  f ( )  {  return  }"));
11676   EXPECT_EQ("void f() {\n"
11677             "  if (a)\n"
11678             "    return\n"
11679             "}",
11680             format("void  f  (  )  {  if  ( a )  return  }"));
11681   EXPECT_EQ("namespace N {\n"
11682             "void f()\n"
11683             "}",
11684             format("namespace  N  {  void f()  }"));
11685   EXPECT_EQ("namespace N {\n"
11686             "void f() {}\n"
11687             "void g()\n"
11688             "} // namespace N",
11689             format("namespace N  { void f( ) { } void g( ) }"));
11690 }
11691 
11692 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
11693   verifyFormat("int aaaaaaaa =\n"
11694                "    // Overlylongcomment\n"
11695                "    b;",
11696                getLLVMStyleWithColumns(20));
11697   verifyFormat("function(\n"
11698                "    ShortArgument,\n"
11699                "    LoooooooooooongArgument);\n",
11700                getLLVMStyleWithColumns(20));
11701 }
11702 
11703 TEST_F(FormatTest, IncorrectAccessSpecifier) {
11704   verifyFormat("public:");
11705   verifyFormat("class A {\n"
11706                "public\n"
11707                "  void f() {}\n"
11708                "};");
11709   verifyFormat("public\n"
11710                "int qwerty;");
11711   verifyFormat("public\n"
11712                "B {}");
11713   verifyFormat("public\n"
11714                "{}");
11715   verifyFormat("public\n"
11716                "B { int x; }");
11717 }
11718 
11719 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
11720   verifyFormat("{");
11721   verifyFormat("#})");
11722   verifyNoCrash("(/**/[:!] ?[).");
11723 }
11724 
11725 TEST_F(FormatTest, IncorrectUnbalancedBracesInMacrosWithUnicode) {
11726   // Found by oss-fuzz:
11727   // https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=8212
11728   FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp);
11729   Style.ColumnLimit = 60;
11730   verifyNoCrash(
11731       "\x23\x47\xff\x20\x28\xff\x3c\xff\x3f\xff\x20\x2f\x7b\x7a\xff\x20"
11732       "\xff\xff\xff\xca\xb5\xff\xff\xff\xff\x3a\x7b\x7d\xff\x20\xff\x20"
11733       "\xff\x74\xff\x20\x7d\x7d\xff\x7b\x3a\xff\x20\x71\xff\x20\xff\x0a",
11734       Style);
11735 }
11736 
11737 TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
11738   verifyFormat("do {\n}");
11739   verifyFormat("do {\n}\n"
11740                "f();");
11741   verifyFormat("do {\n}\n"
11742                "wheeee(fun);");
11743   verifyFormat("do {\n"
11744                "  f();\n"
11745                "}");
11746 }
11747 
11748 TEST_F(FormatTest, IncorrectCodeMissingParens) {
11749   verifyFormat("if {\n  foo;\n  foo();\n}");
11750   verifyFormat("switch {\n  foo;\n  foo();\n}");
11751   verifyIncompleteFormat("for {\n  foo;\n  foo();\n}");
11752   verifyFormat("while {\n  foo;\n  foo();\n}");
11753   verifyFormat("do {\n  foo;\n  foo();\n} while;");
11754 }
11755 
11756 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
11757   verifyIncompleteFormat("namespace {\n"
11758                          "class Foo { Foo (\n"
11759                          "};\n"
11760                          "} // namespace");
11761 }
11762 
11763 TEST_F(FormatTest, IncorrectCodeErrorDetection) {
11764   EXPECT_EQ("{\n  {}\n", format("{\n{\n}\n"));
11765   EXPECT_EQ("{\n  {}\n", format("{\n  {\n}\n"));
11766   EXPECT_EQ("{\n  {}\n", format("{\n  {\n  }\n"));
11767   EXPECT_EQ("{\n  {}\n}\n}\n", format("{\n  {\n    }\n  }\n}\n"));
11768 
11769   EXPECT_EQ("{\n"
11770             "  {\n"
11771             "    breakme(\n"
11772             "        qwe);\n"
11773             "  }\n",
11774             format("{\n"
11775                    "    {\n"
11776                    " breakme(qwe);\n"
11777                    "}\n",
11778                    getLLVMStyleWithColumns(10)));
11779 }
11780 
11781 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
11782   verifyFormat("int x = {\n"
11783                "    avariable,\n"
11784                "    b(alongervariable)};",
11785                getLLVMStyleWithColumns(25));
11786 }
11787 
11788 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
11789   verifyFormat("return (a)(b){1, 2, 3};");
11790 }
11791 
11792 TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
11793   verifyFormat("vector<int> x{1, 2, 3, 4};");
11794   verifyFormat("vector<int> x{\n"
11795                "    1,\n"
11796                "    2,\n"
11797                "    3,\n"
11798                "    4,\n"
11799                "};");
11800   verifyFormat("vector<T> x{{}, {}, {}, {}};");
11801   verifyFormat("f({1, 2});");
11802   verifyFormat("auto v = Foo{-1};");
11803   verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
11804   verifyFormat("Class::Class : member{1, 2, 3} {}");
11805   verifyFormat("new vector<int>{1, 2, 3};");
11806   verifyFormat("new int[3]{1, 2, 3};");
11807   verifyFormat("new int{1};");
11808   verifyFormat("return {arg1, arg2};");
11809   verifyFormat("return {arg1, SomeType{parameter}};");
11810   verifyFormat("int count = set<int>{f(), g(), h()}.size();");
11811   verifyFormat("new T{arg1, arg2};");
11812   verifyFormat("f(MyMap[{composite, key}]);");
11813   verifyFormat("class Class {\n"
11814                "  T member = {arg1, arg2};\n"
11815                "};");
11816   verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
11817   verifyFormat("const struct A a = {.a = 1, .b = 2};");
11818   verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
11819   verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
11820   verifyFormat("int a = std::is_integral<int>{} + 0;");
11821 
11822   verifyFormat("int foo(int i) { return fo1{}(i); }");
11823   verifyFormat("int foo(int i) { return fo1{}(i); }");
11824   verifyFormat("auto i = decltype(x){};");
11825   verifyFormat("auto i = typeof(x){};");
11826   verifyFormat("auto i = _Atomic(x){};");
11827   verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
11828   verifyFormat("Node n{1, Node{1000}, //\n"
11829                "       2};");
11830   verifyFormat("Aaaa aaaaaaa{\n"
11831                "    {\n"
11832                "        aaaa,\n"
11833                "    },\n"
11834                "};");
11835   verifyFormat("class C : public D {\n"
11836                "  SomeClass SC{2};\n"
11837                "};");
11838   verifyFormat("class C : public A {\n"
11839                "  class D : public B {\n"
11840                "    void f() { int i{2}; }\n"
11841                "  };\n"
11842                "};");
11843   verifyFormat("#define A {a, a},");
11844   // Don't confuse braced list initializers with compound statements.
11845   verifyFormat(
11846       "class A {\n"
11847       "  A() : a{} {}\n"
11848       "  A(int b) : b(b) {}\n"
11849       "  A(int a, int b) : a(a), bs{{bs...}} { f(); }\n"
11850       "  int a, b;\n"
11851       "  explicit Expr(const Scalar<Result> &x) : u{Constant<Result>{x}} {}\n"
11852       "  explicit Expr(Scalar<Result> &&x) : u{Constant<Result>{std::move(x)}} "
11853       "{}\n"
11854       "};");
11855 
11856   // Avoid breaking between equal sign and opening brace
11857   FormatStyle AvoidBreakingFirstArgument = getLLVMStyle();
11858   AvoidBreakingFirstArgument.PenaltyBreakBeforeFirstCallParameter = 200;
11859   verifyFormat("const std::unordered_map<std::string, int> MyHashTable =\n"
11860                "    {{\"aaaaaaaaaaaaaaaaaaaaa\", 0},\n"
11861                "     {\"bbbbbbbbbbbbbbbbbbbbb\", 1},\n"
11862                "     {\"ccccccccccccccccccccc\", 2}};",
11863                AvoidBreakingFirstArgument);
11864 
11865   // Binpacking only if there is no trailing comma
11866   verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n"
11867                "                      cccccccccc, dddddddddd};",
11868                getLLVMStyleWithColumns(50));
11869   verifyFormat("const Aaaaaa aaaaa = {\n"
11870                "    aaaaaaaaaaa,\n"
11871                "    bbbbbbbbbbb,\n"
11872                "    ccccccccccc,\n"
11873                "    ddddddddddd,\n"
11874                "};",
11875                getLLVMStyleWithColumns(50));
11876 
11877   // Cases where distinguising braced lists and blocks is hard.
11878   verifyFormat("vector<int> v{12} GUARDED_BY(mutex);");
11879   verifyFormat("void f() {\n"
11880                "  return; // comment\n"
11881                "}\n"
11882                "SomeType t;");
11883   verifyFormat("void f() {\n"
11884                "  if (a) {\n"
11885                "    f();\n"
11886                "  }\n"
11887                "}\n"
11888                "SomeType t;");
11889 
11890   // In combination with BinPackArguments = false.
11891   FormatStyle NoBinPacking = getLLVMStyle();
11892   NoBinPacking.BinPackArguments = false;
11893   verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
11894                "                      bbbbb,\n"
11895                "                      ccccc,\n"
11896                "                      ddddd,\n"
11897                "                      eeeee,\n"
11898                "                      ffffff,\n"
11899                "                      ggggg,\n"
11900                "                      hhhhhh,\n"
11901                "                      iiiiii,\n"
11902                "                      jjjjjj,\n"
11903                "                      kkkkkk};",
11904                NoBinPacking);
11905   verifyFormat("const Aaaaaa aaaaa = {\n"
11906                "    aaaaa,\n"
11907                "    bbbbb,\n"
11908                "    ccccc,\n"
11909                "    ddddd,\n"
11910                "    eeeee,\n"
11911                "    ffffff,\n"
11912                "    ggggg,\n"
11913                "    hhhhhh,\n"
11914                "    iiiiii,\n"
11915                "    jjjjjj,\n"
11916                "    kkkkkk,\n"
11917                "};",
11918                NoBinPacking);
11919   verifyFormat(
11920       "const Aaaaaa aaaaa = {\n"
11921       "    aaaaa,  bbbbb,  ccccc,  ddddd,  eeeee,  ffffff, ggggg, hhhhhh,\n"
11922       "    iiiiii, jjjjjj, kkkkkk, aaaaa,  bbbbb,  ccccc,  ddddd, eeeee,\n"
11923       "    ffffff, ggggg,  hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
11924       "};",
11925       NoBinPacking);
11926 
11927   NoBinPacking.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
11928   EXPECT_EQ("static uint8 CddDp83848Reg[] = {\n"
11929             "    CDDDP83848_BMCR_REGISTER,\n"
11930             "    CDDDP83848_BMSR_REGISTER,\n"
11931             "    CDDDP83848_RBR_REGISTER};",
11932             format("static uint8 CddDp83848Reg[] = {CDDDP83848_BMCR_REGISTER,\n"
11933                    "                                CDDDP83848_BMSR_REGISTER,\n"
11934                    "                                CDDDP83848_RBR_REGISTER};",
11935                    NoBinPacking));
11936 
11937   // FIXME: The alignment of these trailing comments might be bad. Then again,
11938   // this might be utterly useless in real code.
11939   verifyFormat("Constructor::Constructor()\n"
11940                "    : some_value{         //\n"
11941                "                 aaaaaaa, //\n"
11942                "                 bbbbbbb} {}");
11943 
11944   // In braced lists, the first comment is always assumed to belong to the
11945   // first element. Thus, it can be moved to the next or previous line as
11946   // appropriate.
11947   EXPECT_EQ("function({// First element:\n"
11948             "          1,\n"
11949             "          // Second element:\n"
11950             "          2});",
11951             format("function({\n"
11952                    "    // First element:\n"
11953                    "    1,\n"
11954                    "    // Second element:\n"
11955                    "    2});"));
11956   EXPECT_EQ("std::vector<int> MyNumbers{\n"
11957             "    // First element:\n"
11958             "    1,\n"
11959             "    // Second element:\n"
11960             "    2};",
11961             format("std::vector<int> MyNumbers{// First element:\n"
11962                    "                           1,\n"
11963                    "                           // Second element:\n"
11964                    "                           2};",
11965                    getLLVMStyleWithColumns(30)));
11966   // A trailing comma should still lead to an enforced line break and no
11967   // binpacking.
11968   EXPECT_EQ("vector<int> SomeVector = {\n"
11969             "    // aaa\n"
11970             "    1,\n"
11971             "    2,\n"
11972             "};",
11973             format("vector<int> SomeVector = { // aaa\n"
11974                    "    1, 2, };"));
11975 
11976   // C++11 brace initializer list l-braces should not be treated any differently
11977   // when breaking before lambda bodies is enabled
11978   FormatStyle BreakBeforeLambdaBody = getLLVMStyle();
11979   BreakBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
11980   BreakBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
11981   BreakBeforeLambdaBody.AlwaysBreakBeforeMultilineStrings = true;
11982   verifyFormat(
11983       "std::runtime_error{\n"
11984       "    \"Long string which will force a break onto the next line...\"};",
11985       BreakBeforeLambdaBody);
11986 
11987   FormatStyle ExtraSpaces = getLLVMStyle();
11988   ExtraSpaces.Cpp11BracedListStyle = false;
11989   ExtraSpaces.ColumnLimit = 75;
11990   verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
11991   verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
11992   verifyFormat("f({ 1, 2 });", ExtraSpaces);
11993   verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
11994   verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
11995   verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
11996   verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
11997   verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
11998   verifyFormat("return { arg1, arg2 };", ExtraSpaces);
11999   verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
12000   verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
12001   verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
12002   verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
12003   verifyFormat("class Class {\n"
12004                "  T member = { arg1, arg2 };\n"
12005                "};",
12006                ExtraSpaces);
12007   verifyFormat(
12008       "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
12009       "                                 aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
12010       "                  : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
12011       "                                 bbbbbbbbbbbbbbbbbbbb, bbbbb };",
12012       ExtraSpaces);
12013   verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
12014   verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
12015                ExtraSpaces);
12016   verifyFormat(
12017       "someFunction(OtherParam,\n"
12018       "             BracedList{ // comment 1 (Forcing interesting break)\n"
12019       "                         param1, param2,\n"
12020       "                         // comment 2\n"
12021       "                         param3, param4 });",
12022       ExtraSpaces);
12023   verifyFormat(
12024       "std::this_thread::sleep_for(\n"
12025       "    std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
12026       ExtraSpaces);
12027   verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n"
12028                "    aaaaaaa,\n"
12029                "    aaaaaaaaaa,\n"
12030                "    aaaaa,\n"
12031                "    aaaaaaaaaaaaaaa,\n"
12032                "    aaa,\n"
12033                "    aaaaaaaaaa,\n"
12034                "    a,\n"
12035                "    aaaaaaaaaaaaaaaaaaaaa,\n"
12036                "    aaaaaaaaaaaa,\n"
12037                "    aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
12038                "    aaaaaaa,\n"
12039                "    a};");
12040   verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
12041   verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces);
12042   verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces);
12043 
12044   // Avoid breaking between initializer/equal sign and opening brace
12045   ExtraSpaces.PenaltyBreakBeforeFirstCallParameter = 200;
12046   verifyFormat("const std::unordered_map<std::string, int> MyHashTable = {\n"
12047                "  { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n"
12048                "  { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n"
12049                "  { \"ccccccccccccccccccccc\", 2 }\n"
12050                "};",
12051                ExtraSpaces);
12052   verifyFormat("const std::unordered_map<std::string, int> MyHashTable{\n"
12053                "  { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n"
12054                "  { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n"
12055                "  { \"ccccccccccccccccccccc\", 2 }\n"
12056                "};",
12057                ExtraSpaces);
12058 
12059   FormatStyle SpaceBeforeBrace = getLLVMStyle();
12060   SpaceBeforeBrace.SpaceBeforeCpp11BracedList = true;
12061   verifyFormat("vector<int> x {1, 2, 3, 4};", SpaceBeforeBrace);
12062   verifyFormat("f({}, {{}, {}}, MyMap[{k, v}]);", SpaceBeforeBrace);
12063 
12064   FormatStyle SpaceBetweenBraces = getLLVMStyle();
12065   SpaceBetweenBraces.SpacesInAngles = FormatStyle::SIAS_Always;
12066   SpaceBetweenBraces.SpacesInParentheses = true;
12067   SpaceBetweenBraces.SpacesInSquareBrackets = true;
12068   verifyFormat("vector< int > x{ 1, 2, 3, 4 };", SpaceBetweenBraces);
12069   verifyFormat("f( {}, { {}, {} }, MyMap[ { k, v } ] );", SpaceBetweenBraces);
12070   verifyFormat("vector< int > x{ // comment 1\n"
12071                "                 1, 2, 3, 4 };",
12072                SpaceBetweenBraces);
12073   SpaceBetweenBraces.ColumnLimit = 20;
12074   EXPECT_EQ("vector< int > x{\n"
12075             "    1, 2, 3, 4 };",
12076             format("vector<int>x{1,2,3,4};", SpaceBetweenBraces));
12077   SpaceBetweenBraces.ColumnLimit = 24;
12078   EXPECT_EQ("vector< int > x{ 1, 2,\n"
12079             "                 3, 4 };",
12080             format("vector<int>x{1,2,3,4};", SpaceBetweenBraces));
12081   EXPECT_EQ("vector< int > x{\n"
12082             "    1,\n"
12083             "    2,\n"
12084             "    3,\n"
12085             "    4,\n"
12086             "};",
12087             format("vector<int>x{1,2,3,4,};", SpaceBetweenBraces));
12088   verifyFormat("vector< int > x{};", SpaceBetweenBraces);
12089   SpaceBetweenBraces.SpaceInEmptyParentheses = true;
12090   verifyFormat("vector< int > x{ };", SpaceBetweenBraces);
12091 }
12092 
12093 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
12094   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
12095                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
12096                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
12097                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
12098                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
12099                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
12100   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n"
12101                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
12102                "                 1, 22, 333, 4444, 55555, //\n"
12103                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
12104                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
12105   verifyFormat(
12106       "vector<int> x = {1,       22, 333, 4444, 55555, 666666, 7777777,\n"
12107       "                 1,       22, 333, 4444, 55555, 666666, 7777777,\n"
12108       "                 1,       22, 333, 4444, 55555, 666666, // comment\n"
12109       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
12110       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
12111       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
12112       "                 7777777};");
12113   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
12114                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
12115                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
12116   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
12117                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
12118                "    // Separating comment.\n"
12119                "    X86::R8, X86::R9, X86::R10, X86::R11, 0};");
12120   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
12121                "    // Leading comment\n"
12122                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
12123                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
12124   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
12125                "                 1, 1, 1, 1};",
12126                getLLVMStyleWithColumns(39));
12127   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
12128                "                 1, 1, 1, 1};",
12129                getLLVMStyleWithColumns(38));
12130   verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
12131                "    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
12132                getLLVMStyleWithColumns(43));
12133   verifyFormat(
12134       "static unsigned SomeValues[10][3] = {\n"
12135       "    {1, 4, 0},  {4, 9, 0},  {4, 5, 9},  {8, 5, 4}, {1, 8, 4},\n"
12136       "    {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};");
12137   verifyFormat("static auto fields = new vector<string>{\n"
12138                "    \"aaaaaaaaaaaaa\",\n"
12139                "    \"aaaaaaaaaaaaa\",\n"
12140                "    \"aaaaaaaaaaaa\",\n"
12141                "    \"aaaaaaaaaaaaaa\",\n"
12142                "    \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
12143                "    \"aaaaaaaaaaaa\",\n"
12144                "    \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
12145                "};");
12146   verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};");
12147   verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n"
12148                "                 2, bbbbbbbbbbbbbbbbbbbbbb,\n"
12149                "                 3, cccccccccccccccccccccc};",
12150                getLLVMStyleWithColumns(60));
12151 
12152   // Trailing commas.
12153   verifyFormat("vector<int> x = {\n"
12154                "    1, 1, 1, 1, 1, 1, 1, 1,\n"
12155                "};",
12156                getLLVMStyleWithColumns(39));
12157   verifyFormat("vector<int> x = {\n"
12158                "    1, 1, 1, 1, 1, 1, 1, 1, //\n"
12159                "};",
12160                getLLVMStyleWithColumns(39));
12161   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
12162                "                 1, 1, 1, 1,\n"
12163                "                 /**/ /**/};",
12164                getLLVMStyleWithColumns(39));
12165 
12166   // Trailing comment in the first line.
12167   verifyFormat("vector<int> iiiiiiiiiiiiiii = {                      //\n"
12168                "    1111111111, 2222222222, 33333333333, 4444444444, //\n"
12169                "    111111111,  222222222,  3333333333,  444444444,  //\n"
12170                "    11111111,   22222222,   333333333,   44444444};");
12171   // Trailing comment in the last line.
12172   verifyFormat("int aaaaa[] = {\n"
12173                "    1, 2, 3, // comment\n"
12174                "    4, 5, 6  // comment\n"
12175                "};");
12176 
12177   // With nested lists, we should either format one item per line or all nested
12178   // lists one on line.
12179   // FIXME: For some nested lists, we can do better.
12180   verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
12181                "        {aaaaaaaaaaaaaaaaaaa},\n"
12182                "        {aaaaaaaaaaaaaaaaaaaaa},\n"
12183                "        {aaaaaaaaaaaaaaaaa}};",
12184                getLLVMStyleWithColumns(60));
12185   verifyFormat(
12186       "SomeStruct my_struct_array = {\n"
12187       "    {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
12188       "     aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
12189       "    {aaa, aaa},\n"
12190       "    {aaa, aaa},\n"
12191       "    {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
12192       "    {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
12193       "     aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
12194 
12195   // No column layout should be used here.
12196   verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
12197                "                   bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
12198 
12199   verifyNoCrash("a<,");
12200 
12201   // No braced initializer here.
12202   verifyFormat("void f() {\n"
12203                "  struct Dummy {};\n"
12204                "  f(v);\n"
12205                "}");
12206 
12207   // Long lists should be formatted in columns even if they are nested.
12208   verifyFormat(
12209       "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n"
12210       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
12211       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
12212       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
12213       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
12214       "                          1, 22, 333, 4444, 55555, 666666, 7777777});");
12215 
12216   // Allow "single-column" layout even if that violates the column limit. There
12217   // isn't going to be a better way.
12218   verifyFormat("std::vector<int> a = {\n"
12219                "    aaaaaaaa,\n"
12220                "    aaaaaaaa,\n"
12221                "    aaaaaaaa,\n"
12222                "    aaaaaaaa,\n"
12223                "    aaaaaaaaaa,\n"
12224                "    aaaaaaaa,\n"
12225                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa};",
12226                getLLVMStyleWithColumns(30));
12227   verifyFormat("vector<int> aaaa = {\n"
12228                "    aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
12229                "    aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
12230                "    aaaaaa.aaaaaaa,\n"
12231                "    aaaaaa.aaaaaaa,\n"
12232                "    aaaaaa.aaaaaaa,\n"
12233                "    aaaaaa.aaaaaaa,\n"
12234                "};");
12235 
12236   // Don't create hanging lists.
12237   verifyFormat("someFunction(Param, {List1, List2,\n"
12238                "                     List3});",
12239                getLLVMStyleWithColumns(35));
12240   verifyFormat("someFunction(Param, Param,\n"
12241                "             {List1, List2,\n"
12242                "              List3});",
12243                getLLVMStyleWithColumns(35));
12244   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n"
12245                "                               aaaaaaaaaaaaaaaaaaaaaaa);");
12246 }
12247 
12248 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
12249   FormatStyle DoNotMerge = getLLVMStyle();
12250   DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
12251 
12252   verifyFormat("void f() { return 42; }");
12253   verifyFormat("void f() {\n"
12254                "  return 42;\n"
12255                "}",
12256                DoNotMerge);
12257   verifyFormat("void f() {\n"
12258                "  // Comment\n"
12259                "}");
12260   verifyFormat("{\n"
12261                "#error {\n"
12262                "  int a;\n"
12263                "}");
12264   verifyFormat("{\n"
12265                "  int a;\n"
12266                "#error {\n"
12267                "}");
12268   verifyFormat("void f() {} // comment");
12269   verifyFormat("void f() { int a; } // comment");
12270   verifyFormat("void f() {\n"
12271                "} // comment",
12272                DoNotMerge);
12273   verifyFormat("void f() {\n"
12274                "  int a;\n"
12275                "} // comment",
12276                DoNotMerge);
12277   verifyFormat("void f() {\n"
12278                "} // comment",
12279                getLLVMStyleWithColumns(15));
12280 
12281   verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
12282   verifyFormat("void f() {\n  return 42;\n}", getLLVMStyleWithColumns(22));
12283 
12284   verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
12285   verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
12286   verifyFormat("class C {\n"
12287                "  C()\n"
12288                "      : iiiiiiii(nullptr),\n"
12289                "        kkkkkkk(nullptr),\n"
12290                "        mmmmmmm(nullptr),\n"
12291                "        nnnnnnn(nullptr) {}\n"
12292                "};",
12293                getGoogleStyle());
12294 
12295   FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0);
12296   EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit));
12297   EXPECT_EQ("class C {\n"
12298             "  A() : b(0) {}\n"
12299             "};",
12300             format("class C{A():b(0){}};", NoColumnLimit));
12301   EXPECT_EQ("A()\n"
12302             "    : b(0) {\n"
12303             "}",
12304             format("A()\n:b(0)\n{\n}", NoColumnLimit));
12305 
12306   FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
12307   DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
12308       FormatStyle::SFS_None;
12309   EXPECT_EQ("A()\n"
12310             "    : b(0) {\n"
12311             "}",
12312             format("A():b(0){}", DoNotMergeNoColumnLimit));
12313   EXPECT_EQ("A()\n"
12314             "    : b(0) {\n"
12315             "}",
12316             format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit));
12317 
12318   verifyFormat("#define A          \\\n"
12319                "  void f() {       \\\n"
12320                "    int i;         \\\n"
12321                "  }",
12322                getLLVMStyleWithColumns(20));
12323   verifyFormat("#define A           \\\n"
12324                "  void f() { int i; }",
12325                getLLVMStyleWithColumns(21));
12326   verifyFormat("#define A            \\\n"
12327                "  void f() {         \\\n"
12328                "    int i;           \\\n"
12329                "  }                  \\\n"
12330                "  int j;",
12331                getLLVMStyleWithColumns(22));
12332   verifyFormat("#define A             \\\n"
12333                "  void f() { int i; } \\\n"
12334                "  int j;",
12335                getLLVMStyleWithColumns(23));
12336 }
12337 
12338 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) {
12339   FormatStyle MergeEmptyOnly = getLLVMStyle();
12340   MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
12341   verifyFormat("class C {\n"
12342                "  int f() {}\n"
12343                "};",
12344                MergeEmptyOnly);
12345   verifyFormat("class C {\n"
12346                "  int f() {\n"
12347                "    return 42;\n"
12348                "  }\n"
12349                "};",
12350                MergeEmptyOnly);
12351   verifyFormat("int f() {}", MergeEmptyOnly);
12352   verifyFormat("int f() {\n"
12353                "  return 42;\n"
12354                "}",
12355                MergeEmptyOnly);
12356 
12357   // Also verify behavior when BraceWrapping.AfterFunction = true
12358   MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
12359   MergeEmptyOnly.BraceWrapping.AfterFunction = true;
12360   verifyFormat("int f() {}", MergeEmptyOnly);
12361   verifyFormat("class C {\n"
12362                "  int f() {}\n"
12363                "};",
12364                MergeEmptyOnly);
12365 }
12366 
12367 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
12368   FormatStyle MergeInlineOnly = getLLVMStyle();
12369   MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
12370   verifyFormat("class C {\n"
12371                "  int f() { return 42; }\n"
12372                "};",
12373                MergeInlineOnly);
12374   verifyFormat("int f() {\n"
12375                "  return 42;\n"
12376                "}",
12377                MergeInlineOnly);
12378 
12379   // SFS_Inline implies SFS_Empty
12380   verifyFormat("class C {\n"
12381                "  int f() {}\n"
12382                "};",
12383                MergeInlineOnly);
12384   verifyFormat("int f() {}", MergeInlineOnly);
12385 
12386   // Also verify behavior when BraceWrapping.AfterFunction = true
12387   MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
12388   MergeInlineOnly.BraceWrapping.AfterFunction = true;
12389   verifyFormat("class C {\n"
12390                "  int f() { return 42; }\n"
12391                "};",
12392                MergeInlineOnly);
12393   verifyFormat("int f()\n"
12394                "{\n"
12395                "  return 42;\n"
12396                "}",
12397                MergeInlineOnly);
12398 
12399   // SFS_Inline implies SFS_Empty
12400   verifyFormat("int f() {}", MergeInlineOnly);
12401   verifyFormat("class C {\n"
12402                "  int f() {}\n"
12403                "};",
12404                MergeInlineOnly);
12405 
12406   MergeInlineOnly.BraceWrapping.AfterClass = true;
12407   MergeInlineOnly.BraceWrapping.AfterStruct = true;
12408   verifyFormat("class C\n"
12409                "{\n"
12410                "  int f() { return 42; }\n"
12411                "};",
12412                MergeInlineOnly);
12413   verifyFormat("struct C\n"
12414                "{\n"
12415                "  int f() { return 42; }\n"
12416                "};",
12417                MergeInlineOnly);
12418   verifyFormat("int f()\n"
12419                "{\n"
12420                "  return 42;\n"
12421                "}",
12422                MergeInlineOnly);
12423   verifyFormat("int f() {}", MergeInlineOnly);
12424   verifyFormat("class C\n"
12425                "{\n"
12426                "  int f() { return 42; }\n"
12427                "};",
12428                MergeInlineOnly);
12429   verifyFormat("struct C\n"
12430                "{\n"
12431                "  int f() { return 42; }\n"
12432                "};",
12433                MergeInlineOnly);
12434   verifyFormat("struct C\n"
12435                "// comment\n"
12436                "/* comment */\n"
12437                "// comment\n"
12438                "{\n"
12439                "  int f() { return 42; }\n"
12440                "};",
12441                MergeInlineOnly);
12442   verifyFormat("/* comment */ struct C\n"
12443                "{\n"
12444                "  int f() { return 42; }\n"
12445                "};",
12446                MergeInlineOnly);
12447 }
12448 
12449 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) {
12450   FormatStyle MergeInlineOnly = getLLVMStyle();
12451   MergeInlineOnly.AllowShortFunctionsOnASingleLine =
12452       FormatStyle::SFS_InlineOnly;
12453   verifyFormat("class C {\n"
12454                "  int f() { return 42; }\n"
12455                "};",
12456                MergeInlineOnly);
12457   verifyFormat("int f() {\n"
12458                "  return 42;\n"
12459                "}",
12460                MergeInlineOnly);
12461 
12462   // SFS_InlineOnly does not imply SFS_Empty
12463   verifyFormat("class C {\n"
12464                "  int f() {}\n"
12465                "};",
12466                MergeInlineOnly);
12467   verifyFormat("int f() {\n"
12468                "}",
12469                MergeInlineOnly);
12470 
12471   // Also verify behavior when BraceWrapping.AfterFunction = true
12472   MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
12473   MergeInlineOnly.BraceWrapping.AfterFunction = true;
12474   verifyFormat("class C {\n"
12475                "  int f() { return 42; }\n"
12476                "};",
12477                MergeInlineOnly);
12478   verifyFormat("int f()\n"
12479                "{\n"
12480                "  return 42;\n"
12481                "}",
12482                MergeInlineOnly);
12483 
12484   // SFS_InlineOnly does not imply SFS_Empty
12485   verifyFormat("int f()\n"
12486                "{\n"
12487                "}",
12488                MergeInlineOnly);
12489   verifyFormat("class C {\n"
12490                "  int f() {}\n"
12491                "};",
12492                MergeInlineOnly);
12493 }
12494 
12495 TEST_F(FormatTest, SplitEmptyFunction) {
12496   FormatStyle Style = getLLVMStyleWithColumns(40);
12497   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
12498   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12499   Style.BraceWrapping.AfterFunction = true;
12500   Style.BraceWrapping.SplitEmptyFunction = false;
12501 
12502   verifyFormat("int f()\n"
12503                "{}",
12504                Style);
12505   verifyFormat("int f()\n"
12506                "{\n"
12507                "  return 42;\n"
12508                "}",
12509                Style);
12510   verifyFormat("int f()\n"
12511                "{\n"
12512                "  // some comment\n"
12513                "}",
12514                Style);
12515 
12516   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
12517   verifyFormat("int f() {}", Style);
12518   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
12519                "{}",
12520                Style);
12521   verifyFormat("int f()\n"
12522                "{\n"
12523                "  return 0;\n"
12524                "}",
12525                Style);
12526 
12527   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
12528   verifyFormat("class Foo {\n"
12529                "  int f() {}\n"
12530                "};\n",
12531                Style);
12532   verifyFormat("class Foo {\n"
12533                "  int f() { return 0; }\n"
12534                "};\n",
12535                Style);
12536   verifyFormat("class Foo {\n"
12537                "  int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
12538                "  {}\n"
12539                "};\n",
12540                Style);
12541   verifyFormat("class Foo {\n"
12542                "  int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
12543                "  {\n"
12544                "    return 0;\n"
12545                "  }\n"
12546                "};\n",
12547                Style);
12548 
12549   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
12550   verifyFormat("int f() {}", Style);
12551   verifyFormat("int f() { return 0; }", Style);
12552   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
12553                "{}",
12554                Style);
12555   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
12556                "{\n"
12557                "  return 0;\n"
12558                "}",
12559                Style);
12560 }
12561 
12562 TEST_F(FormatTest, SplitEmptyFunctionButNotRecord) {
12563   FormatStyle Style = getLLVMStyleWithColumns(40);
12564   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
12565   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12566   Style.BraceWrapping.AfterFunction = true;
12567   Style.BraceWrapping.SplitEmptyFunction = true;
12568   Style.BraceWrapping.SplitEmptyRecord = false;
12569 
12570   verifyFormat("class C {};", Style);
12571   verifyFormat("struct C {};", Style);
12572   verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
12573                "       int bbbbbbbbbbbbbbbbbbbbbbbb)\n"
12574                "{\n"
12575                "}",
12576                Style);
12577   verifyFormat("class C {\n"
12578                "  C()\n"
12579                "      : aaaaaaaaaaaaaaaaaaaaaaaaaaaa(),\n"
12580                "        bbbbbbbbbbbbbbbbbbb()\n"
12581                "  {\n"
12582                "  }\n"
12583                "  void\n"
12584                "  m(int aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
12585                "    int bbbbbbbbbbbbbbbbbbbbbbbb)\n"
12586                "  {\n"
12587                "  }\n"
12588                "};",
12589                Style);
12590 }
12591 
12592 TEST_F(FormatTest, KeepShortFunctionAfterPPElse) {
12593   FormatStyle Style = getLLVMStyle();
12594   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
12595   verifyFormat("#ifdef A\n"
12596                "int f() {}\n"
12597                "#else\n"
12598                "int g() {}\n"
12599                "#endif",
12600                Style);
12601 }
12602 
12603 TEST_F(FormatTest, SplitEmptyClass) {
12604   FormatStyle Style = getLLVMStyle();
12605   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12606   Style.BraceWrapping.AfterClass = true;
12607   Style.BraceWrapping.SplitEmptyRecord = false;
12608 
12609   verifyFormat("class Foo\n"
12610                "{};",
12611                Style);
12612   verifyFormat("/* something */ class Foo\n"
12613                "{};",
12614                Style);
12615   verifyFormat("template <typename X> class Foo\n"
12616                "{};",
12617                Style);
12618   verifyFormat("class Foo\n"
12619                "{\n"
12620                "  Foo();\n"
12621                "};",
12622                Style);
12623   verifyFormat("typedef class Foo\n"
12624                "{\n"
12625                "} Foo_t;",
12626                Style);
12627 
12628   Style.BraceWrapping.SplitEmptyRecord = true;
12629   Style.BraceWrapping.AfterStruct = true;
12630   verifyFormat("class rep\n"
12631                "{\n"
12632                "};",
12633                Style);
12634   verifyFormat("struct rep\n"
12635                "{\n"
12636                "};",
12637                Style);
12638   verifyFormat("template <typename T> class rep\n"
12639                "{\n"
12640                "};",
12641                Style);
12642   verifyFormat("template <typename T> struct rep\n"
12643                "{\n"
12644                "};",
12645                Style);
12646   verifyFormat("class rep\n"
12647                "{\n"
12648                "  int x;\n"
12649                "};",
12650                Style);
12651   verifyFormat("struct rep\n"
12652                "{\n"
12653                "  int x;\n"
12654                "};",
12655                Style);
12656   verifyFormat("template <typename T> class rep\n"
12657                "{\n"
12658                "  int x;\n"
12659                "};",
12660                Style);
12661   verifyFormat("template <typename T> struct rep\n"
12662                "{\n"
12663                "  int x;\n"
12664                "};",
12665                Style);
12666   verifyFormat("template <typename T> class rep // Foo\n"
12667                "{\n"
12668                "  int x;\n"
12669                "};",
12670                Style);
12671   verifyFormat("template <typename T> struct rep // Bar\n"
12672                "{\n"
12673                "  int x;\n"
12674                "};",
12675                Style);
12676 
12677   verifyFormat("template <typename T> class rep<T>\n"
12678                "{\n"
12679                "  int x;\n"
12680                "};",
12681                Style);
12682 
12683   verifyFormat("template <typename T> class rep<std::complex<T>>\n"
12684                "{\n"
12685                "  int x;\n"
12686                "};",
12687                Style);
12688   verifyFormat("template <typename T> class rep<std::complex<T>>\n"
12689                "{\n"
12690                "};",
12691                Style);
12692 
12693   verifyFormat("#include \"stdint.h\"\n"
12694                "namespace rep {}",
12695                Style);
12696   verifyFormat("#include <stdint.h>\n"
12697                "namespace rep {}",
12698                Style);
12699   verifyFormat("#include <stdint.h>\n"
12700                "namespace rep {}",
12701                "#include <stdint.h>\n"
12702                "namespace rep {\n"
12703                "\n"
12704                "\n"
12705                "}",
12706                Style);
12707 }
12708 
12709 TEST_F(FormatTest, SplitEmptyStruct) {
12710   FormatStyle Style = getLLVMStyle();
12711   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12712   Style.BraceWrapping.AfterStruct = true;
12713   Style.BraceWrapping.SplitEmptyRecord = false;
12714 
12715   verifyFormat("struct Foo\n"
12716                "{};",
12717                Style);
12718   verifyFormat("/* something */ struct Foo\n"
12719                "{};",
12720                Style);
12721   verifyFormat("template <typename X> struct Foo\n"
12722                "{};",
12723                Style);
12724   verifyFormat("struct Foo\n"
12725                "{\n"
12726                "  Foo();\n"
12727                "};",
12728                Style);
12729   verifyFormat("typedef struct Foo\n"
12730                "{\n"
12731                "} Foo_t;",
12732                Style);
12733   // typedef struct Bar {} Bar_t;
12734 }
12735 
12736 TEST_F(FormatTest, SplitEmptyUnion) {
12737   FormatStyle Style = getLLVMStyle();
12738   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12739   Style.BraceWrapping.AfterUnion = true;
12740   Style.BraceWrapping.SplitEmptyRecord = false;
12741 
12742   verifyFormat("union Foo\n"
12743                "{};",
12744                Style);
12745   verifyFormat("/* something */ union Foo\n"
12746                "{};",
12747                Style);
12748   verifyFormat("union Foo\n"
12749                "{\n"
12750                "  A,\n"
12751                "};",
12752                Style);
12753   verifyFormat("typedef union Foo\n"
12754                "{\n"
12755                "} Foo_t;",
12756                Style);
12757 }
12758 
12759 TEST_F(FormatTest, SplitEmptyNamespace) {
12760   FormatStyle Style = getLLVMStyle();
12761   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12762   Style.BraceWrapping.AfterNamespace = true;
12763   Style.BraceWrapping.SplitEmptyNamespace = false;
12764 
12765   verifyFormat("namespace Foo\n"
12766                "{};",
12767                Style);
12768   verifyFormat("/* something */ namespace Foo\n"
12769                "{};",
12770                Style);
12771   verifyFormat("inline namespace Foo\n"
12772                "{};",
12773                Style);
12774   verifyFormat("/* something */ inline namespace Foo\n"
12775                "{};",
12776                Style);
12777   verifyFormat("export namespace Foo\n"
12778                "{};",
12779                Style);
12780   verifyFormat("namespace Foo\n"
12781                "{\n"
12782                "void Bar();\n"
12783                "};",
12784                Style);
12785 }
12786 
12787 TEST_F(FormatTest, NeverMergeShortRecords) {
12788   FormatStyle Style = getLLVMStyle();
12789 
12790   verifyFormat("class Foo {\n"
12791                "  Foo();\n"
12792                "};",
12793                Style);
12794   verifyFormat("typedef class Foo {\n"
12795                "  Foo();\n"
12796                "} Foo_t;",
12797                Style);
12798   verifyFormat("struct Foo {\n"
12799                "  Foo();\n"
12800                "};",
12801                Style);
12802   verifyFormat("typedef struct Foo {\n"
12803                "  Foo();\n"
12804                "} Foo_t;",
12805                Style);
12806   verifyFormat("union Foo {\n"
12807                "  A,\n"
12808                "};",
12809                Style);
12810   verifyFormat("typedef union Foo {\n"
12811                "  A,\n"
12812                "} Foo_t;",
12813                Style);
12814   verifyFormat("namespace Foo {\n"
12815                "void Bar();\n"
12816                "};",
12817                Style);
12818 
12819   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12820   Style.BraceWrapping.AfterClass = true;
12821   Style.BraceWrapping.AfterStruct = true;
12822   Style.BraceWrapping.AfterUnion = true;
12823   Style.BraceWrapping.AfterNamespace = true;
12824   verifyFormat("class Foo\n"
12825                "{\n"
12826                "  Foo();\n"
12827                "};",
12828                Style);
12829   verifyFormat("typedef class Foo\n"
12830                "{\n"
12831                "  Foo();\n"
12832                "} Foo_t;",
12833                Style);
12834   verifyFormat("struct Foo\n"
12835                "{\n"
12836                "  Foo();\n"
12837                "};",
12838                Style);
12839   verifyFormat("typedef struct Foo\n"
12840                "{\n"
12841                "  Foo();\n"
12842                "} Foo_t;",
12843                Style);
12844   verifyFormat("union Foo\n"
12845                "{\n"
12846                "  A,\n"
12847                "};",
12848                Style);
12849   verifyFormat("typedef union Foo\n"
12850                "{\n"
12851                "  A,\n"
12852                "} Foo_t;",
12853                Style);
12854   verifyFormat("namespace Foo\n"
12855                "{\n"
12856                "void Bar();\n"
12857                "};",
12858                Style);
12859 }
12860 
12861 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
12862   // Elaborate type variable declarations.
12863   verifyFormat("struct foo a = {bar};\nint n;");
12864   verifyFormat("class foo a = {bar};\nint n;");
12865   verifyFormat("union foo a = {bar};\nint n;");
12866 
12867   // Elaborate types inside function definitions.
12868   verifyFormat("struct foo f() {}\nint n;");
12869   verifyFormat("class foo f() {}\nint n;");
12870   verifyFormat("union foo f() {}\nint n;");
12871 
12872   // Templates.
12873   verifyFormat("template <class X> void f() {}\nint n;");
12874   verifyFormat("template <struct X> void f() {}\nint n;");
12875   verifyFormat("template <union X> void f() {}\nint n;");
12876 
12877   // Actual definitions...
12878   verifyFormat("struct {\n} n;");
12879   verifyFormat(
12880       "template <template <class T, class Y>, class Z> class X {\n} n;");
12881   verifyFormat("union Z {\n  int n;\n} x;");
12882   verifyFormat("class MACRO Z {\n} n;");
12883   verifyFormat("class MACRO(X) Z {\n} n;");
12884   verifyFormat("class __attribute__(X) Z {\n} n;");
12885   verifyFormat("class __declspec(X) Z {\n} n;");
12886   verifyFormat("class A##B##C {\n} n;");
12887   verifyFormat("class alignas(16) Z {\n} n;");
12888   verifyFormat("class MACRO(X) alignas(16) Z {\n} n;");
12889   verifyFormat("class MACROA MACRO(X) Z {\n} n;");
12890 
12891   // Redefinition from nested context:
12892   verifyFormat("class A::B::C {\n} n;");
12893 
12894   // Template definitions.
12895   verifyFormat(
12896       "template <typename F>\n"
12897       "Matcher(const Matcher<F> &Other,\n"
12898       "        typename enable_if_c<is_base_of<F, T>::value &&\n"
12899       "                             !is_same<F, T>::value>::type * = 0)\n"
12900       "    : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
12901 
12902   // FIXME: This is still incorrectly handled at the formatter side.
12903   verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
12904   verifyFormat("int i = SomeFunction(a<b, a> b);");
12905 
12906   // FIXME:
12907   // This now gets parsed incorrectly as class definition.
12908   // verifyFormat("class A<int> f() {\n}\nint n;");
12909 
12910   // Elaborate types where incorrectly parsing the structural element would
12911   // break the indent.
12912   verifyFormat("if (true)\n"
12913                "  class X x;\n"
12914                "else\n"
12915                "  f();\n");
12916 
12917   // This is simply incomplete. Formatting is not important, but must not crash.
12918   verifyFormat("class A:");
12919 }
12920 
12921 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
12922   EXPECT_EQ("#error Leave     all         white!!!!! space* alone!\n",
12923             format("#error Leave     all         white!!!!! space* alone!\n"));
12924   EXPECT_EQ(
12925       "#warning Leave     all         white!!!!! space* alone!\n",
12926       format("#warning Leave     all         white!!!!! space* alone!\n"));
12927   EXPECT_EQ("#error 1", format("  #  error   1"));
12928   EXPECT_EQ("#warning 1", format("  #  warning 1"));
12929 }
12930 
12931 TEST_F(FormatTest, FormatHashIfExpressions) {
12932   verifyFormat("#if AAAA && BBBB");
12933   verifyFormat("#if (AAAA && BBBB)");
12934   verifyFormat("#elif (AAAA && BBBB)");
12935   // FIXME: Come up with a better indentation for #elif.
12936   verifyFormat(
12937       "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) &&  \\\n"
12938       "    defined(BBBBBBBB)\n"
12939       "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) &&  \\\n"
12940       "    defined(BBBBBBBB)\n"
12941       "#endif",
12942       getLLVMStyleWithColumns(65));
12943 }
12944 
12945 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
12946   FormatStyle AllowsMergedIf = getGoogleStyle();
12947   AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
12948       FormatStyle::SIS_WithoutElse;
12949   verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
12950   verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
12951   verifyFormat("if (true)\n#error E\n  return 42;", AllowsMergedIf);
12952   EXPECT_EQ("if (true) return 42;",
12953             format("if (true)\nreturn 42;", AllowsMergedIf));
12954   FormatStyle ShortMergedIf = AllowsMergedIf;
12955   ShortMergedIf.ColumnLimit = 25;
12956   verifyFormat("#define A \\\n"
12957                "  if (true) return 42;",
12958                ShortMergedIf);
12959   verifyFormat("#define A \\\n"
12960                "  f();    \\\n"
12961                "  if (true)\n"
12962                "#define B",
12963                ShortMergedIf);
12964   verifyFormat("#define A \\\n"
12965                "  f();    \\\n"
12966                "  if (true)\n"
12967                "g();",
12968                ShortMergedIf);
12969   verifyFormat("{\n"
12970                "#ifdef A\n"
12971                "  // Comment\n"
12972                "  if (true) continue;\n"
12973                "#endif\n"
12974                "  // Comment\n"
12975                "  if (true) continue;\n"
12976                "}",
12977                ShortMergedIf);
12978   ShortMergedIf.ColumnLimit = 33;
12979   verifyFormat("#define A \\\n"
12980                "  if constexpr (true) return 42;",
12981                ShortMergedIf);
12982   verifyFormat("#define A \\\n"
12983                "  if CONSTEXPR (true) return 42;",
12984                ShortMergedIf);
12985   ShortMergedIf.ColumnLimit = 29;
12986   verifyFormat("#define A                   \\\n"
12987                "  if (aaaaaaaaaa) return 1; \\\n"
12988                "  return 2;",
12989                ShortMergedIf);
12990   ShortMergedIf.ColumnLimit = 28;
12991   verifyFormat("#define A         \\\n"
12992                "  if (aaaaaaaaaa) \\\n"
12993                "    return 1;     \\\n"
12994                "  return 2;",
12995                ShortMergedIf);
12996   verifyFormat("#define A                \\\n"
12997                "  if constexpr (aaaaaaa) \\\n"
12998                "    return 1;            \\\n"
12999                "  return 2;",
13000                ShortMergedIf);
13001   verifyFormat("#define A                \\\n"
13002                "  if CONSTEXPR (aaaaaaa) \\\n"
13003                "    return 1;            \\\n"
13004                "  return 2;",
13005                ShortMergedIf);
13006 }
13007 
13008 TEST_F(FormatTest, FormatStarDependingOnContext) {
13009   verifyFormat("void f(int *a);");
13010   verifyFormat("void f() { f(fint * b); }");
13011   verifyFormat("class A {\n  void f(int *a);\n};");
13012   verifyFormat("class A {\n  int *a;\n};");
13013   verifyFormat("namespace a {\n"
13014                "namespace b {\n"
13015                "class A {\n"
13016                "  void f() {}\n"
13017                "  int *a;\n"
13018                "};\n"
13019                "} // namespace b\n"
13020                "} // namespace a");
13021 }
13022 
13023 TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
13024   verifyFormat("while");
13025   verifyFormat("operator");
13026 }
13027 
13028 TEST_F(FormatTest, SkipsDeeplyNestedLines) {
13029   // This code would be painfully slow to format if we didn't skip it.
13030   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
13031                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
13032                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
13033                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
13034                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
13035                    "A(1, 1)\n"
13036                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x
13037                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
13038                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
13039                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
13040                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
13041                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
13042                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
13043                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
13044                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
13045                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n");
13046   // Deeply nested part is untouched, rest is formatted.
13047   EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n",
13048             format(std::string("int    i;\n") + Code + "int    j;\n",
13049                    getLLVMStyle(), SC_ExpectIncomplete));
13050 }
13051 
13052 //===----------------------------------------------------------------------===//
13053 // Objective-C tests.
13054 //===----------------------------------------------------------------------===//
13055 
13056 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
13057   verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
13058   EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
13059             format("-(NSUInteger)indexOfObject:(id)anObject;"));
13060   EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
13061   EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
13062   EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
13063             format("-(NSInteger)Method3:(id)anObject;"));
13064   EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
13065             format("-(NSInteger)Method4:(id)anObject;"));
13066   EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
13067             format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
13068   EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
13069             format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
13070   EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject "
13071             "forAllCells:(BOOL)flag;",
13072             format("- (void)sendAction:(SEL)aSelector to:(id)anObject "
13073                    "forAllCells:(BOOL)flag;"));
13074 
13075   // Very long objectiveC method declaration.
13076   verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
13077                "    (SoooooooooooooooooooooomeType *)bbbbbbbbbb;");
13078   verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
13079                "                    inRange:(NSRange)range\n"
13080                "                   outRange:(NSRange)out_range\n"
13081                "                  outRange1:(NSRange)out_range1\n"
13082                "                  outRange2:(NSRange)out_range2\n"
13083                "                  outRange3:(NSRange)out_range3\n"
13084                "                  outRange4:(NSRange)out_range4\n"
13085                "                  outRange5:(NSRange)out_range5\n"
13086                "                  outRange6:(NSRange)out_range6\n"
13087                "                  outRange7:(NSRange)out_range7\n"
13088                "                  outRange8:(NSRange)out_range8\n"
13089                "                  outRange9:(NSRange)out_range9;");
13090 
13091   // When the function name has to be wrapped.
13092   FormatStyle Style = getLLVMStyle();
13093   // ObjC ignores IndentWrappedFunctionNames when wrapping methods
13094   // and always indents instead.
13095   Style.IndentWrappedFunctionNames = false;
13096   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
13097                "    veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
13098                "               anotherName:(NSString)bbbbbbbbbbbbbb {\n"
13099                "}",
13100                Style);
13101   Style.IndentWrappedFunctionNames = true;
13102   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
13103                "    veryLooooooooooongName:(NSString)cccccccccccccc\n"
13104                "               anotherName:(NSString)dddddddddddddd {\n"
13105                "}",
13106                Style);
13107 
13108   verifyFormat("- (int)sum:(vector<int>)numbers;");
13109   verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
13110   // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
13111   // protocol lists (but not for template classes):
13112   // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
13113 
13114   verifyFormat("- (int (*)())foo:(int (*)())f;");
13115   verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
13116 
13117   // If there's no return type (very rare in practice!), LLVM and Google style
13118   // agree.
13119   verifyFormat("- foo;");
13120   verifyFormat("- foo:(int)f;");
13121   verifyGoogleFormat("- foo:(int)foo;");
13122 }
13123 
13124 TEST_F(FormatTest, BreaksStringLiterals) {
13125   EXPECT_EQ("\"some text \"\n"
13126             "\"other\";",
13127             format("\"some text other\";", getLLVMStyleWithColumns(12)));
13128   EXPECT_EQ("\"some text \"\n"
13129             "\"other\";",
13130             format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
13131   EXPECT_EQ(
13132       "#define A  \\\n"
13133       "  \"some \"  \\\n"
13134       "  \"text \"  \\\n"
13135       "  \"other\";",
13136       format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
13137   EXPECT_EQ(
13138       "#define A  \\\n"
13139       "  \"so \"    \\\n"
13140       "  \"text \"  \\\n"
13141       "  \"other\";",
13142       format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
13143 
13144   EXPECT_EQ("\"some text\"",
13145             format("\"some text\"", getLLVMStyleWithColumns(1)));
13146   EXPECT_EQ("\"some text\"",
13147             format("\"some text\"", getLLVMStyleWithColumns(11)));
13148   EXPECT_EQ("\"some \"\n"
13149             "\"text\"",
13150             format("\"some text\"", getLLVMStyleWithColumns(10)));
13151   EXPECT_EQ("\"some \"\n"
13152             "\"text\"",
13153             format("\"some text\"", getLLVMStyleWithColumns(7)));
13154   EXPECT_EQ("\"some\"\n"
13155             "\" tex\"\n"
13156             "\"t\"",
13157             format("\"some text\"", getLLVMStyleWithColumns(6)));
13158   EXPECT_EQ("\"some\"\n"
13159             "\" tex\"\n"
13160             "\" and\"",
13161             format("\"some tex and\"", getLLVMStyleWithColumns(6)));
13162   EXPECT_EQ("\"some\"\n"
13163             "\"/tex\"\n"
13164             "\"/and\"",
13165             format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
13166 
13167   EXPECT_EQ("variable =\n"
13168             "    \"long string \"\n"
13169             "    \"literal\";",
13170             format("variable = \"long string literal\";",
13171                    getLLVMStyleWithColumns(20)));
13172 
13173   EXPECT_EQ("variable = f(\n"
13174             "    \"long string \"\n"
13175             "    \"literal\",\n"
13176             "    short,\n"
13177             "    loooooooooooooooooooong);",
13178             format("variable = f(\"long string literal\", short, "
13179                    "loooooooooooooooooooong);",
13180                    getLLVMStyleWithColumns(20)));
13181 
13182   EXPECT_EQ(
13183       "f(g(\"long string \"\n"
13184       "    \"literal\"),\n"
13185       "  b);",
13186       format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20)));
13187   EXPECT_EQ("f(g(\"long string \"\n"
13188             "    \"literal\",\n"
13189             "    a),\n"
13190             "  b);",
13191             format("f(g(\"long string literal\", a), b);",
13192                    getLLVMStyleWithColumns(20)));
13193   EXPECT_EQ(
13194       "f(\"one two\".split(\n"
13195       "    variable));",
13196       format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
13197   EXPECT_EQ("f(\"one two three four five six \"\n"
13198             "  \"seven\".split(\n"
13199             "      really_looooong_variable));",
13200             format("f(\"one two three four five six seven\"."
13201                    "split(really_looooong_variable));",
13202                    getLLVMStyleWithColumns(33)));
13203 
13204   EXPECT_EQ("f(\"some \"\n"
13205             "  \"text\",\n"
13206             "  other);",
13207             format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
13208 
13209   // Only break as a last resort.
13210   verifyFormat(
13211       "aaaaaaaaaaaaaaaaaaaa(\n"
13212       "    aaaaaaaaaaaaaaaaaaaa,\n"
13213       "    aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
13214 
13215   EXPECT_EQ("\"splitmea\"\n"
13216             "\"trandomp\"\n"
13217             "\"oint\"",
13218             format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
13219 
13220   EXPECT_EQ("\"split/\"\n"
13221             "\"pathat/\"\n"
13222             "\"slashes\"",
13223             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
13224 
13225   EXPECT_EQ("\"split/\"\n"
13226             "\"pathat/\"\n"
13227             "\"slashes\"",
13228             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
13229   EXPECT_EQ("\"split at \"\n"
13230             "\"spaces/at/\"\n"
13231             "\"slashes.at.any$\"\n"
13232             "\"non-alphanumeric%\"\n"
13233             "\"1111111111characte\"\n"
13234             "\"rs\"",
13235             format("\"split at "
13236                    "spaces/at/"
13237                    "slashes.at."
13238                    "any$non-"
13239                    "alphanumeric%"
13240                    "1111111111characte"
13241                    "rs\"",
13242                    getLLVMStyleWithColumns(20)));
13243 
13244   // Verify that splitting the strings understands
13245   // Style::AlwaysBreakBeforeMultilineStrings.
13246   EXPECT_EQ("aaaaaaaaaaaa(\n"
13247             "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
13248             "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
13249             format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa "
13250                    "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
13251                    "aaaaaaaaaaaaaaaaaaaaaa\");",
13252                    getGoogleStyle()));
13253   EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
13254             "       \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
13255             format("return \"aaaaaaaaaaaaaaaaaaaaaa "
13256                    "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
13257                    "aaaaaaaaaaaaaaaaaaaaaa\";",
13258                    getGoogleStyle()));
13259   EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
13260             "                \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
13261             format("llvm::outs() << "
13262                    "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
13263                    "aaaaaaaaaaaaaaaaaaa\";"));
13264   EXPECT_EQ("ffff(\n"
13265             "    {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
13266             "     \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
13267             format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
13268                    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
13269                    getGoogleStyle()));
13270 
13271   FormatStyle Style = getLLVMStyleWithColumns(12);
13272   Style.BreakStringLiterals = false;
13273   EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style));
13274 
13275   FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
13276   AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
13277   EXPECT_EQ("#define A \\\n"
13278             "  \"some \" \\\n"
13279             "  \"text \" \\\n"
13280             "  \"other\";",
13281             format("#define A \"some text other\";", AlignLeft));
13282 }
13283 
13284 TEST_F(FormatTest, BreaksStringLiteralsAtColumnLimit) {
13285   EXPECT_EQ("C a = \"some more \"\n"
13286             "      \"text\";",
13287             format("C a = \"some more text\";", getLLVMStyleWithColumns(18)));
13288 }
13289 
13290 TEST_F(FormatTest, FullyRemoveEmptyLines) {
13291   FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80);
13292   NoEmptyLines.MaxEmptyLinesToKeep = 0;
13293   EXPECT_EQ("int i = a(b());",
13294             format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines));
13295 }
13296 
13297 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
13298   EXPECT_EQ(
13299       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
13300       "(\n"
13301       "    \"x\t\");",
13302       format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
13303              "aaaaaaa("
13304              "\"x\t\");"));
13305 }
13306 
13307 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
13308   EXPECT_EQ(
13309       "u8\"utf8 string \"\n"
13310       "u8\"literal\";",
13311       format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
13312   EXPECT_EQ(
13313       "u\"utf16 string \"\n"
13314       "u\"literal\";",
13315       format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
13316   EXPECT_EQ(
13317       "U\"utf32 string \"\n"
13318       "U\"literal\";",
13319       format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
13320   EXPECT_EQ("L\"wide string \"\n"
13321             "L\"literal\";",
13322             format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
13323   EXPECT_EQ("@\"NSString \"\n"
13324             "@\"literal\";",
13325             format("@\"NSString literal\";", getGoogleStyleWithColumns(19)));
13326   verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26));
13327 
13328   // This input makes clang-format try to split the incomplete unicode escape
13329   // sequence, which used to lead to a crasher.
13330   verifyNoCrash(
13331       "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
13332       getLLVMStyleWithColumns(60));
13333 }
13334 
13335 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) {
13336   FormatStyle Style = getGoogleStyleWithColumns(15);
13337   EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style));
13338   EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style));
13339   EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style));
13340   EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style));
13341   EXPECT_EQ("u8R\"x(raw literal)x\";",
13342             format("u8R\"x(raw literal)x\";", Style));
13343 }
13344 
13345 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
13346   FormatStyle Style = getLLVMStyleWithColumns(20);
13347   EXPECT_EQ(
13348       "_T(\"aaaaaaaaaaaaaa\")\n"
13349       "_T(\"aaaaaaaaaaaaaa\")\n"
13350       "_T(\"aaaaaaaaaaaa\")",
13351       format("  _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
13352   EXPECT_EQ("f(x,\n"
13353             "  _T(\"aaaaaaaaaaaa\")\n"
13354             "  _T(\"aaa\"),\n"
13355             "  z);",
13356             format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style));
13357 
13358   // FIXME: Handle embedded spaces in one iteration.
13359   //  EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
13360   //            "_T(\"aaaaaaaaaaaaa\")\n"
13361   //            "_T(\"aaaaaaaaaaaaa\")\n"
13362   //            "_T(\"a\")",
13363   //            format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
13364   //                   getLLVMStyleWithColumns(20)));
13365   EXPECT_EQ(
13366       "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
13367       format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style));
13368   EXPECT_EQ("f(\n"
13369             "#if !TEST\n"
13370             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
13371             "#endif\n"
13372             ");",
13373             format("f(\n"
13374                    "#if !TEST\n"
13375                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
13376                    "#endif\n"
13377                    ");"));
13378   EXPECT_EQ("f(\n"
13379             "\n"
13380             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));",
13381             format("f(\n"
13382                    "\n"
13383                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));"));
13384   // Regression test for accessing tokens past the end of a vector in the
13385   // TokenLexer.
13386   verifyNoCrash(R"(_T(
13387 "
13388 )
13389 )");
13390 }
13391 
13392 TEST_F(FormatTest, BreaksStringLiteralOperands) {
13393   // In a function call with two operands, the second can be broken with no line
13394   // break before it.
13395   EXPECT_EQ(
13396       "func(a, \"long long \"\n"
13397       "        \"long long\");",
13398       format("func(a, \"long long long long\");", getLLVMStyleWithColumns(24)));
13399   // In a function call with three operands, the second must be broken with a
13400   // line break before it.
13401   EXPECT_EQ("func(a,\n"
13402             "     \"long long long \"\n"
13403             "     \"long\",\n"
13404             "     c);",
13405             format("func(a, \"long long long long\", c);",
13406                    getLLVMStyleWithColumns(24)));
13407   // In a function call with three operands, the third must be broken with a
13408   // line break before it.
13409   EXPECT_EQ("func(a, b,\n"
13410             "     \"long long long \"\n"
13411             "     \"long\");",
13412             format("func(a, b, \"long long long long\");",
13413                    getLLVMStyleWithColumns(24)));
13414   // In a function call with three operands, both the second and the third must
13415   // be broken with a line break before them.
13416   EXPECT_EQ("func(a,\n"
13417             "     \"long long long \"\n"
13418             "     \"long\",\n"
13419             "     \"long long long \"\n"
13420             "     \"long\");",
13421             format("func(a, \"long long long long\", \"long long long long\");",
13422                    getLLVMStyleWithColumns(24)));
13423   // In a chain of << with two operands, the second can be broken with no line
13424   // break before it.
13425   EXPECT_EQ("a << \"line line \"\n"
13426             "     \"line\";",
13427             format("a << \"line line line\";", getLLVMStyleWithColumns(20)));
13428   // In a chain of << with three operands, the second can be broken with no line
13429   // break before it.
13430   EXPECT_EQ(
13431       "abcde << \"line \"\n"
13432       "         \"line line\"\n"
13433       "      << c;",
13434       format("abcde << \"line line line\" << c;", getLLVMStyleWithColumns(20)));
13435   // In a chain of << with three operands, the third must be broken with a line
13436   // break before it.
13437   EXPECT_EQ(
13438       "a << b\n"
13439       "  << \"line line \"\n"
13440       "     \"line\";",
13441       format("a << b << \"line line line\";", getLLVMStyleWithColumns(20)));
13442   // In a chain of << with three operands, the second can be broken with no line
13443   // break before it and the third must be broken with a line break before it.
13444   EXPECT_EQ("abcd << \"line line \"\n"
13445             "        \"line\"\n"
13446             "     << \"line line \"\n"
13447             "        \"line\";",
13448             format("abcd << \"line line line\" << \"line line line\";",
13449                    getLLVMStyleWithColumns(20)));
13450   // In a chain of binary operators with two operands, the second can be broken
13451   // with no line break before it.
13452   EXPECT_EQ(
13453       "abcd + \"line line \"\n"
13454       "       \"line line\";",
13455       format("abcd + \"line line line line\";", getLLVMStyleWithColumns(20)));
13456   // In a chain of binary operators with three operands, the second must be
13457   // broken with a line break before it.
13458   EXPECT_EQ("abcd +\n"
13459             "    \"line line \"\n"
13460             "    \"line line\" +\n"
13461             "    e;",
13462             format("abcd + \"line line line line\" + e;",
13463                    getLLVMStyleWithColumns(20)));
13464   // In a function call with two operands, with AlignAfterOpenBracket enabled,
13465   // the first must be broken with a line break before it.
13466   FormatStyle Style = getLLVMStyleWithColumns(25);
13467   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
13468   EXPECT_EQ("someFunction(\n"
13469             "    \"long long long \"\n"
13470             "    \"long\",\n"
13471             "    a);",
13472             format("someFunction(\"long long long long\", a);", Style));
13473 }
13474 
13475 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
13476   EXPECT_EQ(
13477       "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
13478       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
13479       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
13480       format("aaaaaaaaaaa  =  \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
13481              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
13482              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"));
13483 }
13484 
13485 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
13486   EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);",
13487             format("f(g(R\"x(raw literal)x\",   a), b);", getGoogleStyle()));
13488   EXPECT_EQ("fffffffffff(g(R\"x(\n"
13489             "multiline raw string literal xxxxxxxxxxxxxx\n"
13490             ")x\",\n"
13491             "              a),\n"
13492             "            b);",
13493             format("fffffffffff(g(R\"x(\n"
13494                    "multiline raw string literal xxxxxxxxxxxxxx\n"
13495                    ")x\", a), b);",
13496                    getGoogleStyleWithColumns(20)));
13497   EXPECT_EQ("fffffffffff(\n"
13498             "    g(R\"x(qqq\n"
13499             "multiline raw string literal xxxxxxxxxxxxxx\n"
13500             ")x\",\n"
13501             "      a),\n"
13502             "    b);",
13503             format("fffffffffff(g(R\"x(qqq\n"
13504                    "multiline raw string literal xxxxxxxxxxxxxx\n"
13505                    ")x\", a), b);",
13506                    getGoogleStyleWithColumns(20)));
13507 
13508   EXPECT_EQ("fffffffffff(R\"x(\n"
13509             "multiline raw string literal xxxxxxxxxxxxxx\n"
13510             ")x\");",
13511             format("fffffffffff(R\"x(\n"
13512                    "multiline raw string literal xxxxxxxxxxxxxx\n"
13513                    ")x\");",
13514                    getGoogleStyleWithColumns(20)));
13515   EXPECT_EQ("fffffffffff(R\"x(\n"
13516             "multiline raw string literal xxxxxxxxxxxxxx\n"
13517             ")x\" + bbbbbb);",
13518             format("fffffffffff(R\"x(\n"
13519                    "multiline raw string literal xxxxxxxxxxxxxx\n"
13520                    ")x\" +   bbbbbb);",
13521                    getGoogleStyleWithColumns(20)));
13522   EXPECT_EQ("fffffffffff(\n"
13523             "    R\"x(\n"
13524             "multiline raw string literal xxxxxxxxxxxxxx\n"
13525             ")x\" +\n"
13526             "    bbbbbb);",
13527             format("fffffffffff(\n"
13528                    " R\"x(\n"
13529                    "multiline raw string literal xxxxxxxxxxxxxx\n"
13530                    ")x\" + bbbbbb);",
13531                    getGoogleStyleWithColumns(20)));
13532   EXPECT_EQ("fffffffffff(R\"(single line raw string)\" + bbbbbb);",
13533             format("fffffffffff(\n"
13534                    " R\"(single line raw string)\" + bbbbbb);"));
13535 }
13536 
13537 TEST_F(FormatTest, SkipsUnknownStringLiterals) {
13538   verifyFormat("string a = \"unterminated;");
13539   EXPECT_EQ("function(\"unterminated,\n"
13540             "         OtherParameter);",
13541             format("function(  \"unterminated,\n"
13542                    "    OtherParameter);"));
13543 }
13544 
13545 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
13546   FormatStyle Style = getLLVMStyle();
13547   Style.Standard = FormatStyle::LS_Cpp03;
13548   EXPECT_EQ("#define x(_a) printf(\"foo\" _a);",
13549             format("#define x(_a) printf(\"foo\"_a);", Style));
13550 }
13551 
13552 TEST_F(FormatTest, CppLexVersion) {
13553   FormatStyle Style = getLLVMStyle();
13554   // Formatting of x * y differs if x is a type.
13555   verifyFormat("void foo() { MACRO(a * b); }", Style);
13556   verifyFormat("void foo() { MACRO(int *b); }", Style);
13557 
13558   // LLVM style uses latest lexer.
13559   verifyFormat("void foo() { MACRO(char8_t *b); }", Style);
13560   Style.Standard = FormatStyle::LS_Cpp17;
13561   // But in c++17, char8_t isn't a keyword.
13562   verifyFormat("void foo() { MACRO(char8_t * b); }", Style);
13563 }
13564 
13565 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); }
13566 
13567 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
13568   EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
13569             "             \"ddeeefff\");",
13570             format("someFunction(\"aaabbbcccdddeeefff\");",
13571                    getLLVMStyleWithColumns(25)));
13572   EXPECT_EQ("someFunction1234567890(\n"
13573             "    \"aaabbbcccdddeeefff\");",
13574             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
13575                    getLLVMStyleWithColumns(26)));
13576   EXPECT_EQ("someFunction1234567890(\n"
13577             "    \"aaabbbcccdddeeeff\"\n"
13578             "    \"f\");",
13579             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
13580                    getLLVMStyleWithColumns(25)));
13581   EXPECT_EQ("someFunction1234567890(\n"
13582             "    \"aaabbbcccdddeeeff\"\n"
13583             "    \"f\");",
13584             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
13585                    getLLVMStyleWithColumns(24)));
13586   EXPECT_EQ("someFunction(\n"
13587             "    \"aaabbbcc ddde \"\n"
13588             "    \"efff\");",
13589             format("someFunction(\"aaabbbcc ddde efff\");",
13590                    getLLVMStyleWithColumns(25)));
13591   EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
13592             "             \"ddeeefff\");",
13593             format("someFunction(\"aaabbbccc ddeeefff\");",
13594                    getLLVMStyleWithColumns(25)));
13595   EXPECT_EQ("someFunction1234567890(\n"
13596             "    \"aaabb \"\n"
13597             "    \"cccdddeeefff\");",
13598             format("someFunction1234567890(\"aaabb cccdddeeefff\");",
13599                    getLLVMStyleWithColumns(25)));
13600   EXPECT_EQ("#define A          \\\n"
13601             "  string s =       \\\n"
13602             "      \"123456789\"  \\\n"
13603             "      \"0\";         \\\n"
13604             "  int i;",
13605             format("#define A string s = \"1234567890\"; int i;",
13606                    getLLVMStyleWithColumns(20)));
13607   EXPECT_EQ("someFunction(\n"
13608             "    \"aaabbbcc \"\n"
13609             "    \"dddeeefff\");",
13610             format("someFunction(\"aaabbbcc dddeeefff\");",
13611                    getLLVMStyleWithColumns(25)));
13612 }
13613 
13614 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
13615   EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3)));
13616   EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2)));
13617   EXPECT_EQ("\"test\"\n"
13618             "\"\\n\"",
13619             format("\"test\\n\"", getLLVMStyleWithColumns(7)));
13620   EXPECT_EQ("\"tes\\\\\"\n"
13621             "\"n\"",
13622             format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
13623   EXPECT_EQ("\"\\\\\\\\\"\n"
13624             "\"\\n\"",
13625             format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
13626   EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
13627   EXPECT_EQ("\"\\uff01\"\n"
13628             "\"test\"",
13629             format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
13630   EXPECT_EQ("\"\\Uff01ff02\"",
13631             format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
13632   EXPECT_EQ("\"\\x000000000001\"\n"
13633             "\"next\"",
13634             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
13635   EXPECT_EQ("\"\\x000000000001next\"",
13636             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
13637   EXPECT_EQ("\"\\x000000000001\"",
13638             format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
13639   EXPECT_EQ("\"test\"\n"
13640             "\"\\000000\"\n"
13641             "\"000001\"",
13642             format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
13643   EXPECT_EQ("\"test\\000\"\n"
13644             "\"00000000\"\n"
13645             "\"1\"",
13646             format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
13647 }
13648 
13649 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
13650   verifyFormat("void f() {\n"
13651                "  return g() {}\n"
13652                "  void h() {}");
13653   verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
13654                "g();\n"
13655                "}");
13656 }
13657 
13658 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
13659   verifyFormat(
13660       "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
13661 }
13662 
13663 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
13664   verifyFormat("class X {\n"
13665                "  void f() {\n"
13666                "  }\n"
13667                "};",
13668                getLLVMStyleWithColumns(12));
13669 }
13670 
13671 TEST_F(FormatTest, ConfigurableIndentWidth) {
13672   FormatStyle EightIndent = getLLVMStyleWithColumns(18);
13673   EightIndent.IndentWidth = 8;
13674   EightIndent.ContinuationIndentWidth = 8;
13675   verifyFormat("void f() {\n"
13676                "        someFunction();\n"
13677                "        if (true) {\n"
13678                "                f();\n"
13679                "        }\n"
13680                "}",
13681                EightIndent);
13682   verifyFormat("class X {\n"
13683                "        void f() {\n"
13684                "        }\n"
13685                "};",
13686                EightIndent);
13687   verifyFormat("int x[] = {\n"
13688                "        call(),\n"
13689                "        call()};",
13690                EightIndent);
13691 }
13692 
13693 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
13694   verifyFormat("double\n"
13695                "f();",
13696                getLLVMStyleWithColumns(8));
13697 }
13698 
13699 TEST_F(FormatTest, ConfigurableUseOfTab) {
13700   FormatStyle Tab = getLLVMStyleWithColumns(42);
13701   Tab.IndentWidth = 8;
13702   Tab.UseTab = FormatStyle::UT_Always;
13703   Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left;
13704 
13705   EXPECT_EQ("if (aaaaaaaa && // q\n"
13706             "    bb)\t\t// w\n"
13707             "\t;",
13708             format("if (aaaaaaaa &&// q\n"
13709                    "bb)// w\n"
13710                    ";",
13711                    Tab));
13712   EXPECT_EQ("if (aaa && bbb) // w\n"
13713             "\t;",
13714             format("if(aaa&&bbb)// w\n"
13715                    ";",
13716                    Tab));
13717 
13718   verifyFormat("class X {\n"
13719                "\tvoid f() {\n"
13720                "\t\tsomeFunction(parameter1,\n"
13721                "\t\t\t     parameter2);\n"
13722                "\t}\n"
13723                "};",
13724                Tab);
13725   verifyFormat("#define A                        \\\n"
13726                "\tvoid f() {               \\\n"
13727                "\t\tsomeFunction(    \\\n"
13728                "\t\t    parameter1,  \\\n"
13729                "\t\t    parameter2); \\\n"
13730                "\t}",
13731                Tab);
13732   verifyFormat("int a;\t      // x\n"
13733                "int bbbbbbbb; // x\n",
13734                Tab);
13735 
13736   Tab.TabWidth = 4;
13737   Tab.IndentWidth = 8;
13738   verifyFormat("class TabWidth4Indent8 {\n"
13739                "\t\tvoid f() {\n"
13740                "\t\t\t\tsomeFunction(parameter1,\n"
13741                "\t\t\t\t\t\t\t parameter2);\n"
13742                "\t\t}\n"
13743                "};",
13744                Tab);
13745 
13746   Tab.TabWidth = 4;
13747   Tab.IndentWidth = 4;
13748   verifyFormat("class TabWidth4Indent4 {\n"
13749                "\tvoid f() {\n"
13750                "\t\tsomeFunction(parameter1,\n"
13751                "\t\t\t\t\t parameter2);\n"
13752                "\t}\n"
13753                "};",
13754                Tab);
13755 
13756   Tab.TabWidth = 8;
13757   Tab.IndentWidth = 4;
13758   verifyFormat("class TabWidth8Indent4 {\n"
13759                "    void f() {\n"
13760                "\tsomeFunction(parameter1,\n"
13761                "\t\t     parameter2);\n"
13762                "    }\n"
13763                "};",
13764                Tab);
13765 
13766   Tab.TabWidth = 8;
13767   Tab.IndentWidth = 8;
13768   EXPECT_EQ("/*\n"
13769             "\t      a\t\tcomment\n"
13770             "\t      in multiple lines\n"
13771             "       */",
13772             format("   /*\t \t \n"
13773                    " \t \t a\t\tcomment\t \t\n"
13774                    " \t \t in multiple lines\t\n"
13775                    " \t  */",
13776                    Tab));
13777 
13778   Tab.UseTab = FormatStyle::UT_ForIndentation;
13779   verifyFormat("{\n"
13780                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13781                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13782                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13783                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13784                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13785                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13786                "};",
13787                Tab);
13788   verifyFormat("enum AA {\n"
13789                "\ta1, // Force multiple lines\n"
13790                "\ta2,\n"
13791                "\ta3\n"
13792                "};",
13793                Tab);
13794   EXPECT_EQ("if (aaaaaaaa && // q\n"
13795             "    bb)         // w\n"
13796             "\t;",
13797             format("if (aaaaaaaa &&// q\n"
13798                    "bb)// w\n"
13799                    ";",
13800                    Tab));
13801   verifyFormat("class X {\n"
13802                "\tvoid f() {\n"
13803                "\t\tsomeFunction(parameter1,\n"
13804                "\t\t             parameter2);\n"
13805                "\t}\n"
13806                "};",
13807                Tab);
13808   verifyFormat("{\n"
13809                "\tQ(\n"
13810                "\t    {\n"
13811                "\t\t    int a;\n"
13812                "\t\t    someFunction(aaaaaaaa,\n"
13813                "\t\t                 bbbbbbb);\n"
13814                "\t    },\n"
13815                "\t    p);\n"
13816                "}",
13817                Tab);
13818   EXPECT_EQ("{\n"
13819             "\t/* aaaa\n"
13820             "\t   bbbb */\n"
13821             "}",
13822             format("{\n"
13823                    "/* aaaa\n"
13824                    "   bbbb */\n"
13825                    "}",
13826                    Tab));
13827   EXPECT_EQ("{\n"
13828             "\t/*\n"
13829             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13830             "\t  bbbbbbbbbbbbb\n"
13831             "\t*/\n"
13832             "}",
13833             format("{\n"
13834                    "/*\n"
13835                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13836                    "*/\n"
13837                    "}",
13838                    Tab));
13839   EXPECT_EQ("{\n"
13840             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13841             "\t// bbbbbbbbbbbbb\n"
13842             "}",
13843             format("{\n"
13844                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13845                    "}",
13846                    Tab));
13847   EXPECT_EQ("{\n"
13848             "\t/*\n"
13849             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13850             "\t  bbbbbbbbbbbbb\n"
13851             "\t*/\n"
13852             "}",
13853             format("{\n"
13854                    "\t/*\n"
13855                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13856                    "\t*/\n"
13857                    "}",
13858                    Tab));
13859   EXPECT_EQ("{\n"
13860             "\t/*\n"
13861             "\n"
13862             "\t*/\n"
13863             "}",
13864             format("{\n"
13865                    "\t/*\n"
13866                    "\n"
13867                    "\t*/\n"
13868                    "}",
13869                    Tab));
13870   EXPECT_EQ("{\n"
13871             "\t/*\n"
13872             " asdf\n"
13873             "\t*/\n"
13874             "}",
13875             format("{\n"
13876                    "\t/*\n"
13877                    " asdf\n"
13878                    "\t*/\n"
13879                    "}",
13880                    Tab));
13881 
13882   verifyFormat("void f() {\n"
13883                "\treturn true ? aaaaaaaaaaaaaaaaaa\n"
13884                "\t            : bbbbbbbbbbbbbbbbbb\n"
13885                "}",
13886                Tab);
13887   FormatStyle TabNoBreak = Tab;
13888   TabNoBreak.BreakBeforeTernaryOperators = false;
13889   verifyFormat("void f() {\n"
13890                "\treturn true ? aaaaaaaaaaaaaaaaaa :\n"
13891                "\t              bbbbbbbbbbbbbbbbbb\n"
13892                "}",
13893                TabNoBreak);
13894   verifyFormat("void f() {\n"
13895                "\treturn true ?\n"
13896                "\t           aaaaaaaaaaaaaaaaaaaa :\n"
13897                "\t           bbbbbbbbbbbbbbbbbbbb\n"
13898                "}",
13899                TabNoBreak);
13900 
13901   Tab.UseTab = FormatStyle::UT_Never;
13902   EXPECT_EQ("/*\n"
13903             "              a\t\tcomment\n"
13904             "              in multiple lines\n"
13905             "       */",
13906             format("   /*\t \t \n"
13907                    " \t \t a\t\tcomment\t \t\n"
13908                    " \t \t in multiple lines\t\n"
13909                    " \t  */",
13910                    Tab));
13911   EXPECT_EQ("/* some\n"
13912             "   comment */",
13913             format(" \t \t /* some\n"
13914                    " \t \t    comment */",
13915                    Tab));
13916   EXPECT_EQ("int a; /* some\n"
13917             "   comment */",
13918             format(" \t \t int a; /* some\n"
13919                    " \t \t    comment */",
13920                    Tab));
13921 
13922   EXPECT_EQ("int a; /* some\n"
13923             "comment */",
13924             format(" \t \t int\ta; /* some\n"
13925                    " \t \t    comment */",
13926                    Tab));
13927   EXPECT_EQ("f(\"\t\t\"); /* some\n"
13928             "    comment */",
13929             format(" \t \t f(\"\t\t\"); /* some\n"
13930                    " \t \t    comment */",
13931                    Tab));
13932   EXPECT_EQ("{\n"
13933             "        /*\n"
13934             "         * Comment\n"
13935             "         */\n"
13936             "        int i;\n"
13937             "}",
13938             format("{\n"
13939                    "\t/*\n"
13940                    "\t * Comment\n"
13941                    "\t */\n"
13942                    "\t int i;\n"
13943                    "}",
13944                    Tab));
13945 
13946   Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
13947   Tab.TabWidth = 8;
13948   Tab.IndentWidth = 8;
13949   EXPECT_EQ("if (aaaaaaaa && // q\n"
13950             "    bb)         // w\n"
13951             "\t;",
13952             format("if (aaaaaaaa &&// q\n"
13953                    "bb)// w\n"
13954                    ";",
13955                    Tab));
13956   EXPECT_EQ("if (aaa && bbb) // w\n"
13957             "\t;",
13958             format("if(aaa&&bbb)// w\n"
13959                    ";",
13960                    Tab));
13961   verifyFormat("class X {\n"
13962                "\tvoid f() {\n"
13963                "\t\tsomeFunction(parameter1,\n"
13964                "\t\t\t     parameter2);\n"
13965                "\t}\n"
13966                "};",
13967                Tab);
13968   verifyFormat("#define A                        \\\n"
13969                "\tvoid f() {               \\\n"
13970                "\t\tsomeFunction(    \\\n"
13971                "\t\t    parameter1,  \\\n"
13972                "\t\t    parameter2); \\\n"
13973                "\t}",
13974                Tab);
13975   Tab.TabWidth = 4;
13976   Tab.IndentWidth = 8;
13977   verifyFormat("class TabWidth4Indent8 {\n"
13978                "\t\tvoid f() {\n"
13979                "\t\t\t\tsomeFunction(parameter1,\n"
13980                "\t\t\t\t\t\t\t parameter2);\n"
13981                "\t\t}\n"
13982                "};",
13983                Tab);
13984   Tab.TabWidth = 4;
13985   Tab.IndentWidth = 4;
13986   verifyFormat("class TabWidth4Indent4 {\n"
13987                "\tvoid f() {\n"
13988                "\t\tsomeFunction(parameter1,\n"
13989                "\t\t\t\t\t parameter2);\n"
13990                "\t}\n"
13991                "};",
13992                Tab);
13993   Tab.TabWidth = 8;
13994   Tab.IndentWidth = 4;
13995   verifyFormat("class TabWidth8Indent4 {\n"
13996                "    void f() {\n"
13997                "\tsomeFunction(parameter1,\n"
13998                "\t\t     parameter2);\n"
13999                "    }\n"
14000                "};",
14001                Tab);
14002   Tab.TabWidth = 8;
14003   Tab.IndentWidth = 8;
14004   EXPECT_EQ("/*\n"
14005             "\t      a\t\tcomment\n"
14006             "\t      in multiple lines\n"
14007             "       */",
14008             format("   /*\t \t \n"
14009                    " \t \t a\t\tcomment\t \t\n"
14010                    " \t \t in multiple lines\t\n"
14011                    " \t  */",
14012                    Tab));
14013   verifyFormat("{\n"
14014                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
14015                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
14016                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
14017                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
14018                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
14019                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
14020                "};",
14021                Tab);
14022   verifyFormat("enum AA {\n"
14023                "\ta1, // Force multiple lines\n"
14024                "\ta2,\n"
14025                "\ta3\n"
14026                "};",
14027                Tab);
14028   EXPECT_EQ("if (aaaaaaaa && // q\n"
14029             "    bb)         // w\n"
14030             "\t;",
14031             format("if (aaaaaaaa &&// q\n"
14032                    "bb)// w\n"
14033                    ";",
14034                    Tab));
14035   verifyFormat("class X {\n"
14036                "\tvoid f() {\n"
14037                "\t\tsomeFunction(parameter1,\n"
14038                "\t\t\t     parameter2);\n"
14039                "\t}\n"
14040                "};",
14041                Tab);
14042   verifyFormat("{\n"
14043                "\tQ(\n"
14044                "\t    {\n"
14045                "\t\t    int a;\n"
14046                "\t\t    someFunction(aaaaaaaa,\n"
14047                "\t\t\t\t bbbbbbb);\n"
14048                "\t    },\n"
14049                "\t    p);\n"
14050                "}",
14051                Tab);
14052   EXPECT_EQ("{\n"
14053             "\t/* aaaa\n"
14054             "\t   bbbb */\n"
14055             "}",
14056             format("{\n"
14057                    "/* aaaa\n"
14058                    "   bbbb */\n"
14059                    "}",
14060                    Tab));
14061   EXPECT_EQ("{\n"
14062             "\t/*\n"
14063             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
14064             "\t  bbbbbbbbbbbbb\n"
14065             "\t*/\n"
14066             "}",
14067             format("{\n"
14068                    "/*\n"
14069                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
14070                    "*/\n"
14071                    "}",
14072                    Tab));
14073   EXPECT_EQ("{\n"
14074             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
14075             "\t// bbbbbbbbbbbbb\n"
14076             "}",
14077             format("{\n"
14078                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
14079                    "}",
14080                    Tab));
14081   EXPECT_EQ("{\n"
14082             "\t/*\n"
14083             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
14084             "\t  bbbbbbbbbbbbb\n"
14085             "\t*/\n"
14086             "}",
14087             format("{\n"
14088                    "\t/*\n"
14089                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
14090                    "\t*/\n"
14091                    "}",
14092                    Tab));
14093   EXPECT_EQ("{\n"
14094             "\t/*\n"
14095             "\n"
14096             "\t*/\n"
14097             "}",
14098             format("{\n"
14099                    "\t/*\n"
14100                    "\n"
14101                    "\t*/\n"
14102                    "}",
14103                    Tab));
14104   EXPECT_EQ("{\n"
14105             "\t/*\n"
14106             " asdf\n"
14107             "\t*/\n"
14108             "}",
14109             format("{\n"
14110                    "\t/*\n"
14111                    " asdf\n"
14112                    "\t*/\n"
14113                    "}",
14114                    Tab));
14115   EXPECT_EQ("/* some\n"
14116             "   comment */",
14117             format(" \t \t /* some\n"
14118                    " \t \t    comment */",
14119                    Tab));
14120   EXPECT_EQ("int a; /* some\n"
14121             "   comment */",
14122             format(" \t \t int a; /* some\n"
14123                    " \t \t    comment */",
14124                    Tab));
14125   EXPECT_EQ("int a; /* some\n"
14126             "comment */",
14127             format(" \t \t int\ta; /* some\n"
14128                    " \t \t    comment */",
14129                    Tab));
14130   EXPECT_EQ("f(\"\t\t\"); /* some\n"
14131             "    comment */",
14132             format(" \t \t f(\"\t\t\"); /* some\n"
14133                    " \t \t    comment */",
14134                    Tab));
14135   EXPECT_EQ("{\n"
14136             "\t/*\n"
14137             "\t * Comment\n"
14138             "\t */\n"
14139             "\tint i;\n"
14140             "}",
14141             format("{\n"
14142                    "\t/*\n"
14143                    "\t * Comment\n"
14144                    "\t */\n"
14145                    "\t int i;\n"
14146                    "}",
14147                    Tab));
14148   Tab.TabWidth = 2;
14149   Tab.IndentWidth = 2;
14150   EXPECT_EQ("{\n"
14151             "\t/* aaaa\n"
14152             "\t\t bbbb */\n"
14153             "}",
14154             format("{\n"
14155                    "/* aaaa\n"
14156                    "\t bbbb */\n"
14157                    "}",
14158                    Tab));
14159   EXPECT_EQ("{\n"
14160             "\t/*\n"
14161             "\t\taaaaaaaaaaaaaaaaaaaaaaaaaa\n"
14162             "\t\tbbbbbbbbbbbbb\n"
14163             "\t*/\n"
14164             "}",
14165             format("{\n"
14166                    "/*\n"
14167                    "\taaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
14168                    "*/\n"
14169                    "}",
14170                    Tab));
14171   Tab.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
14172   Tab.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
14173   Tab.TabWidth = 4;
14174   Tab.IndentWidth = 4;
14175   verifyFormat("class Assign {\n"
14176                "\tvoid f() {\n"
14177                "\t\tint         x      = 123;\n"
14178                "\t\tint         random = 4;\n"
14179                "\t\tstd::string alphabet =\n"
14180                "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
14181                "\t}\n"
14182                "};",
14183                Tab);
14184 
14185   Tab.UseTab = FormatStyle::UT_AlignWithSpaces;
14186   Tab.TabWidth = 8;
14187   Tab.IndentWidth = 8;
14188   EXPECT_EQ("if (aaaaaaaa && // q\n"
14189             "    bb)         // w\n"
14190             "\t;",
14191             format("if (aaaaaaaa &&// q\n"
14192                    "bb)// w\n"
14193                    ";",
14194                    Tab));
14195   EXPECT_EQ("if (aaa && bbb) // w\n"
14196             "\t;",
14197             format("if(aaa&&bbb)// w\n"
14198                    ";",
14199                    Tab));
14200   verifyFormat("class X {\n"
14201                "\tvoid f() {\n"
14202                "\t\tsomeFunction(parameter1,\n"
14203                "\t\t             parameter2);\n"
14204                "\t}\n"
14205                "};",
14206                Tab);
14207   verifyFormat("#define A                        \\\n"
14208                "\tvoid f() {               \\\n"
14209                "\t\tsomeFunction(    \\\n"
14210                "\t\t    parameter1,  \\\n"
14211                "\t\t    parameter2); \\\n"
14212                "\t}",
14213                Tab);
14214   Tab.TabWidth = 4;
14215   Tab.IndentWidth = 8;
14216   verifyFormat("class TabWidth4Indent8 {\n"
14217                "\t\tvoid f() {\n"
14218                "\t\t\t\tsomeFunction(parameter1,\n"
14219                "\t\t\t\t             parameter2);\n"
14220                "\t\t}\n"
14221                "};",
14222                Tab);
14223   Tab.TabWidth = 4;
14224   Tab.IndentWidth = 4;
14225   verifyFormat("class TabWidth4Indent4 {\n"
14226                "\tvoid f() {\n"
14227                "\t\tsomeFunction(parameter1,\n"
14228                "\t\t             parameter2);\n"
14229                "\t}\n"
14230                "};",
14231                Tab);
14232   Tab.TabWidth = 8;
14233   Tab.IndentWidth = 4;
14234   verifyFormat("class TabWidth8Indent4 {\n"
14235                "    void f() {\n"
14236                "\tsomeFunction(parameter1,\n"
14237                "\t             parameter2);\n"
14238                "    }\n"
14239                "};",
14240                Tab);
14241   Tab.TabWidth = 8;
14242   Tab.IndentWidth = 8;
14243   EXPECT_EQ("/*\n"
14244             "              a\t\tcomment\n"
14245             "              in multiple lines\n"
14246             "       */",
14247             format("   /*\t \t \n"
14248                    " \t \t a\t\tcomment\t \t\n"
14249                    " \t \t in multiple lines\t\n"
14250                    " \t  */",
14251                    Tab));
14252   verifyFormat("{\n"
14253                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
14254                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
14255                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
14256                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
14257                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
14258                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
14259                "};",
14260                Tab);
14261   verifyFormat("enum AA {\n"
14262                "\ta1, // Force multiple lines\n"
14263                "\ta2,\n"
14264                "\ta3\n"
14265                "};",
14266                Tab);
14267   EXPECT_EQ("if (aaaaaaaa && // q\n"
14268             "    bb)         // w\n"
14269             "\t;",
14270             format("if (aaaaaaaa &&// q\n"
14271                    "bb)// w\n"
14272                    ";",
14273                    Tab));
14274   verifyFormat("class X {\n"
14275                "\tvoid f() {\n"
14276                "\t\tsomeFunction(parameter1,\n"
14277                "\t\t             parameter2);\n"
14278                "\t}\n"
14279                "};",
14280                Tab);
14281   verifyFormat("{\n"
14282                "\tQ(\n"
14283                "\t    {\n"
14284                "\t\t    int a;\n"
14285                "\t\t    someFunction(aaaaaaaa,\n"
14286                "\t\t                 bbbbbbb);\n"
14287                "\t    },\n"
14288                "\t    p);\n"
14289                "}",
14290                Tab);
14291   EXPECT_EQ("{\n"
14292             "\t/* aaaa\n"
14293             "\t   bbbb */\n"
14294             "}",
14295             format("{\n"
14296                    "/* aaaa\n"
14297                    "   bbbb */\n"
14298                    "}",
14299                    Tab));
14300   EXPECT_EQ("{\n"
14301             "\t/*\n"
14302             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
14303             "\t  bbbbbbbbbbbbb\n"
14304             "\t*/\n"
14305             "}",
14306             format("{\n"
14307                    "/*\n"
14308                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
14309                    "*/\n"
14310                    "}",
14311                    Tab));
14312   EXPECT_EQ("{\n"
14313             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
14314             "\t// bbbbbbbbbbbbb\n"
14315             "}",
14316             format("{\n"
14317                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
14318                    "}",
14319                    Tab));
14320   EXPECT_EQ("{\n"
14321             "\t/*\n"
14322             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
14323             "\t  bbbbbbbbbbbbb\n"
14324             "\t*/\n"
14325             "}",
14326             format("{\n"
14327                    "\t/*\n"
14328                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
14329                    "\t*/\n"
14330                    "}",
14331                    Tab));
14332   EXPECT_EQ("{\n"
14333             "\t/*\n"
14334             "\n"
14335             "\t*/\n"
14336             "}",
14337             format("{\n"
14338                    "\t/*\n"
14339                    "\n"
14340                    "\t*/\n"
14341                    "}",
14342                    Tab));
14343   EXPECT_EQ("{\n"
14344             "\t/*\n"
14345             " asdf\n"
14346             "\t*/\n"
14347             "}",
14348             format("{\n"
14349                    "\t/*\n"
14350                    " asdf\n"
14351                    "\t*/\n"
14352                    "}",
14353                    Tab));
14354   EXPECT_EQ("/* some\n"
14355             "   comment */",
14356             format(" \t \t /* some\n"
14357                    " \t \t    comment */",
14358                    Tab));
14359   EXPECT_EQ("int a; /* some\n"
14360             "   comment */",
14361             format(" \t \t int a; /* some\n"
14362                    " \t \t    comment */",
14363                    Tab));
14364   EXPECT_EQ("int a; /* some\n"
14365             "comment */",
14366             format(" \t \t int\ta; /* some\n"
14367                    " \t \t    comment */",
14368                    Tab));
14369   EXPECT_EQ("f(\"\t\t\"); /* some\n"
14370             "    comment */",
14371             format(" \t \t f(\"\t\t\"); /* some\n"
14372                    " \t \t    comment */",
14373                    Tab));
14374   EXPECT_EQ("{\n"
14375             "\t/*\n"
14376             "\t * Comment\n"
14377             "\t */\n"
14378             "\tint i;\n"
14379             "}",
14380             format("{\n"
14381                    "\t/*\n"
14382                    "\t * Comment\n"
14383                    "\t */\n"
14384                    "\t int i;\n"
14385                    "}",
14386                    Tab));
14387   Tab.TabWidth = 2;
14388   Tab.IndentWidth = 2;
14389   EXPECT_EQ("{\n"
14390             "\t/* aaaa\n"
14391             "\t   bbbb */\n"
14392             "}",
14393             format("{\n"
14394                    "/* aaaa\n"
14395                    "   bbbb */\n"
14396                    "}",
14397                    Tab));
14398   EXPECT_EQ("{\n"
14399             "\t/*\n"
14400             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
14401             "\t  bbbbbbbbbbbbb\n"
14402             "\t*/\n"
14403             "}",
14404             format("{\n"
14405                    "/*\n"
14406                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
14407                    "*/\n"
14408                    "}",
14409                    Tab));
14410   Tab.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
14411   Tab.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
14412   Tab.TabWidth = 4;
14413   Tab.IndentWidth = 4;
14414   verifyFormat("class Assign {\n"
14415                "\tvoid f() {\n"
14416                "\t\tint         x      = 123;\n"
14417                "\t\tint         random = 4;\n"
14418                "\t\tstd::string alphabet =\n"
14419                "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
14420                "\t}\n"
14421                "};",
14422                Tab);
14423   Tab.AlignOperands = FormatStyle::OAS_Align;
14424   verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb +\n"
14425                "                 cccccccccccccccccccc;",
14426                Tab);
14427   // no alignment
14428   verifyFormat("int aaaaaaaaaa =\n"
14429                "\tbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
14430                Tab);
14431   verifyFormat("return aaaaaaaaaaaaaaaa ? 111111111111111\n"
14432                "       : bbbbbbbbbbbbbb ? 222222222222222\n"
14433                "                        : 333333333333333;",
14434                Tab);
14435   Tab.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
14436   Tab.AlignOperands = FormatStyle::OAS_AlignAfterOperator;
14437   verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb\n"
14438                "               + cccccccccccccccccccc;",
14439                Tab);
14440 }
14441 
14442 TEST_F(FormatTest, ZeroTabWidth) {
14443   FormatStyle Tab = getLLVMStyleWithColumns(42);
14444   Tab.IndentWidth = 8;
14445   Tab.UseTab = FormatStyle::UT_Never;
14446   Tab.TabWidth = 0;
14447   EXPECT_EQ("void a(){\n"
14448             "    // line starts with '\t'\n"
14449             "};",
14450             format("void a(){\n"
14451                    "\t// line starts with '\t'\n"
14452                    "};",
14453                    Tab));
14454 
14455   EXPECT_EQ("void a(){\n"
14456             "    // line starts with '\t'\n"
14457             "};",
14458             format("void a(){\n"
14459                    "\t\t// line starts with '\t'\n"
14460                    "};",
14461                    Tab));
14462 
14463   Tab.UseTab = FormatStyle::UT_ForIndentation;
14464   EXPECT_EQ("void a(){\n"
14465             "    // line starts with '\t'\n"
14466             "};",
14467             format("void a(){\n"
14468                    "\t// line starts with '\t'\n"
14469                    "};",
14470                    Tab));
14471 
14472   EXPECT_EQ("void a(){\n"
14473             "    // line starts with '\t'\n"
14474             "};",
14475             format("void a(){\n"
14476                    "\t\t// line starts with '\t'\n"
14477                    "};",
14478                    Tab));
14479 
14480   Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
14481   EXPECT_EQ("void a(){\n"
14482             "    // line starts with '\t'\n"
14483             "};",
14484             format("void a(){\n"
14485                    "\t// line starts with '\t'\n"
14486                    "};",
14487                    Tab));
14488 
14489   EXPECT_EQ("void a(){\n"
14490             "    // line starts with '\t'\n"
14491             "};",
14492             format("void a(){\n"
14493                    "\t\t// line starts with '\t'\n"
14494                    "};",
14495                    Tab));
14496 
14497   Tab.UseTab = FormatStyle::UT_AlignWithSpaces;
14498   EXPECT_EQ("void a(){\n"
14499             "    // line starts with '\t'\n"
14500             "};",
14501             format("void a(){\n"
14502                    "\t// line starts with '\t'\n"
14503                    "};",
14504                    Tab));
14505 
14506   EXPECT_EQ("void a(){\n"
14507             "    // line starts with '\t'\n"
14508             "};",
14509             format("void a(){\n"
14510                    "\t\t// line starts with '\t'\n"
14511                    "};",
14512                    Tab));
14513 
14514   Tab.UseTab = FormatStyle::UT_Always;
14515   EXPECT_EQ("void a(){\n"
14516             "// line starts with '\t'\n"
14517             "};",
14518             format("void a(){\n"
14519                    "\t// line starts with '\t'\n"
14520                    "};",
14521                    Tab));
14522 
14523   EXPECT_EQ("void a(){\n"
14524             "// line starts with '\t'\n"
14525             "};",
14526             format("void a(){\n"
14527                    "\t\t// line starts with '\t'\n"
14528                    "};",
14529                    Tab));
14530 }
14531 
14532 TEST_F(FormatTest, CalculatesOriginalColumn) {
14533   EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
14534             "q\"; /* some\n"
14535             "       comment */",
14536             format("  \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
14537                    "q\"; /* some\n"
14538                    "       comment */",
14539                    getLLVMStyle()));
14540   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
14541             "/* some\n"
14542             "   comment */",
14543             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
14544                    " /* some\n"
14545                    "    comment */",
14546                    getLLVMStyle()));
14547   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
14548             "qqq\n"
14549             "/* some\n"
14550             "   comment */",
14551             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
14552                    "qqq\n"
14553                    " /* some\n"
14554                    "    comment */",
14555                    getLLVMStyle()));
14556   EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
14557             "wwww; /* some\n"
14558             "         comment */",
14559             format("  inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
14560                    "wwww; /* some\n"
14561                    "         comment */",
14562                    getLLVMStyle()));
14563 }
14564 
14565 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
14566   FormatStyle NoSpace = getLLVMStyle();
14567   NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
14568 
14569   verifyFormat("while(true)\n"
14570                "  continue;",
14571                NoSpace);
14572   verifyFormat("for(;;)\n"
14573                "  continue;",
14574                NoSpace);
14575   verifyFormat("if(true)\n"
14576                "  f();\n"
14577                "else if(true)\n"
14578                "  f();",
14579                NoSpace);
14580   verifyFormat("do {\n"
14581                "  do_something();\n"
14582                "} while(something());",
14583                NoSpace);
14584   verifyFormat("switch(x) {\n"
14585                "default:\n"
14586                "  break;\n"
14587                "}",
14588                NoSpace);
14589   verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
14590   verifyFormat("size_t x = sizeof(x);", NoSpace);
14591   verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
14592   verifyFormat("auto f(int x) -> typeof(x);", NoSpace);
14593   verifyFormat("auto f(int x) -> _Atomic(x);", NoSpace);
14594   verifyFormat("auto f(int x) -> __underlying_type(x);", NoSpace);
14595   verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
14596   verifyFormat("alignas(128) char a[128];", NoSpace);
14597   verifyFormat("size_t x = alignof(MyType);", NoSpace);
14598   verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
14599   verifyFormat("int f() throw(Deprecated);", NoSpace);
14600   verifyFormat("typedef void (*cb)(int);", NoSpace);
14601   verifyFormat("T A::operator()();", NoSpace);
14602   verifyFormat("X A::operator++(T);", NoSpace);
14603   verifyFormat("auto lambda = []() { return 0; };", NoSpace);
14604 
14605   FormatStyle Space = getLLVMStyle();
14606   Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
14607 
14608   verifyFormat("int f ();", Space);
14609   verifyFormat("void f (int a, T b) {\n"
14610                "  while (true)\n"
14611                "    continue;\n"
14612                "}",
14613                Space);
14614   verifyFormat("if (true)\n"
14615                "  f ();\n"
14616                "else if (true)\n"
14617                "  f ();",
14618                Space);
14619   verifyFormat("do {\n"
14620                "  do_something ();\n"
14621                "} while (something ());",
14622                Space);
14623   verifyFormat("switch (x) {\n"
14624                "default:\n"
14625                "  break;\n"
14626                "}",
14627                Space);
14628   verifyFormat("A::A () : a (1) {}", Space);
14629   verifyFormat("void f () __attribute__ ((asdf));", Space);
14630   verifyFormat("*(&a + 1);\n"
14631                "&((&a)[1]);\n"
14632                "a[(b + c) * d];\n"
14633                "(((a + 1) * 2) + 3) * 4;",
14634                Space);
14635   verifyFormat("#define A(x) x", Space);
14636   verifyFormat("#define A (x) x", Space);
14637   verifyFormat("#if defined(x)\n"
14638                "#endif",
14639                Space);
14640   verifyFormat("auto i = std::make_unique<int> (5);", Space);
14641   verifyFormat("size_t x = sizeof (x);", Space);
14642   verifyFormat("auto f (int x) -> decltype (x);", Space);
14643   verifyFormat("auto f (int x) -> typeof (x);", Space);
14644   verifyFormat("auto f (int x) -> _Atomic (x);", Space);
14645   verifyFormat("auto f (int x) -> __underlying_type (x);", Space);
14646   verifyFormat("int f (T x) noexcept (x.create ());", Space);
14647   verifyFormat("alignas (128) char a[128];", Space);
14648   verifyFormat("size_t x = alignof (MyType);", Space);
14649   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
14650   verifyFormat("int f () throw (Deprecated);", Space);
14651   verifyFormat("typedef void (*cb) (int);", Space);
14652   // FIXME these tests regressed behaviour.
14653   // verifyFormat("T A::operator() ();", Space);
14654   // verifyFormat("X A::operator++ (T);", Space);
14655   verifyFormat("auto lambda = [] () { return 0; };", Space);
14656   verifyFormat("int x = int (y);", Space);
14657 
14658   FormatStyle SomeSpace = getLLVMStyle();
14659   SomeSpace.SpaceBeforeParens = FormatStyle::SBPO_NonEmptyParentheses;
14660 
14661   verifyFormat("[]() -> float {}", SomeSpace);
14662   verifyFormat("[] (auto foo) {}", SomeSpace);
14663   verifyFormat("[foo]() -> int {}", SomeSpace);
14664   verifyFormat("int f();", SomeSpace);
14665   verifyFormat("void f (int a, T b) {\n"
14666                "  while (true)\n"
14667                "    continue;\n"
14668                "}",
14669                SomeSpace);
14670   verifyFormat("if (true)\n"
14671                "  f();\n"
14672                "else if (true)\n"
14673                "  f();",
14674                SomeSpace);
14675   verifyFormat("do {\n"
14676                "  do_something();\n"
14677                "} while (something());",
14678                SomeSpace);
14679   verifyFormat("switch (x) {\n"
14680                "default:\n"
14681                "  break;\n"
14682                "}",
14683                SomeSpace);
14684   verifyFormat("A::A() : a (1) {}", SomeSpace);
14685   verifyFormat("void f() __attribute__ ((asdf));", SomeSpace);
14686   verifyFormat("*(&a + 1);\n"
14687                "&((&a)[1]);\n"
14688                "a[(b + c) * d];\n"
14689                "(((a + 1) * 2) + 3) * 4;",
14690                SomeSpace);
14691   verifyFormat("#define A(x) x", SomeSpace);
14692   verifyFormat("#define A (x) x", SomeSpace);
14693   verifyFormat("#if defined(x)\n"
14694                "#endif",
14695                SomeSpace);
14696   verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace);
14697   verifyFormat("size_t x = sizeof (x);", SomeSpace);
14698   verifyFormat("auto f (int x) -> decltype (x);", SomeSpace);
14699   verifyFormat("auto f (int x) -> typeof (x);", SomeSpace);
14700   verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace);
14701   verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace);
14702   verifyFormat("int f (T x) noexcept (x.create());", SomeSpace);
14703   verifyFormat("alignas (128) char a[128];", SomeSpace);
14704   verifyFormat("size_t x = alignof (MyType);", SomeSpace);
14705   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");",
14706                SomeSpace);
14707   verifyFormat("int f() throw (Deprecated);", SomeSpace);
14708   verifyFormat("typedef void (*cb) (int);", SomeSpace);
14709   verifyFormat("T A::operator()();", SomeSpace);
14710   // FIXME these tests regressed behaviour.
14711   // verifyFormat("X A::operator++ (T);", SomeSpace);
14712   verifyFormat("int x = int (y);", SomeSpace);
14713   verifyFormat("auto lambda = []() { return 0; };", SomeSpace);
14714 
14715   FormatStyle SpaceControlStatements = getLLVMStyle();
14716   SpaceControlStatements.SpaceBeforeParens = FormatStyle::SBPO_Custom;
14717   SpaceControlStatements.SpaceBeforeParensOptions.AfterControlStatements = true;
14718 
14719   verifyFormat("while (true)\n"
14720                "  continue;",
14721                SpaceControlStatements);
14722   verifyFormat("if (true)\n"
14723                "  f();\n"
14724                "else if (true)\n"
14725                "  f();",
14726                SpaceControlStatements);
14727   verifyFormat("for (;;) {\n"
14728                "  do_something();\n"
14729                "}",
14730                SpaceControlStatements);
14731   verifyFormat("do {\n"
14732                "  do_something();\n"
14733                "} while (something());",
14734                SpaceControlStatements);
14735   verifyFormat("switch (x) {\n"
14736                "default:\n"
14737                "  break;\n"
14738                "}",
14739                SpaceControlStatements);
14740 
14741   FormatStyle SpaceFuncDecl = getLLVMStyle();
14742   SpaceFuncDecl.SpaceBeforeParens = FormatStyle::SBPO_Custom;
14743   SpaceFuncDecl.SpaceBeforeParensOptions.AfterFunctionDeclarationName = true;
14744 
14745   verifyFormat("int f ();", SpaceFuncDecl);
14746   verifyFormat("void f(int a, T b) {}", SpaceFuncDecl);
14747   verifyFormat("A::A() : a(1) {}", SpaceFuncDecl);
14748   verifyFormat("void f () __attribute__((asdf));", SpaceFuncDecl);
14749   verifyFormat("#define A(x) x", SpaceFuncDecl);
14750   verifyFormat("#define A (x) x", SpaceFuncDecl);
14751   verifyFormat("#if defined(x)\n"
14752                "#endif",
14753                SpaceFuncDecl);
14754   verifyFormat("auto i = std::make_unique<int>(5);", SpaceFuncDecl);
14755   verifyFormat("size_t x = sizeof(x);", SpaceFuncDecl);
14756   verifyFormat("auto f (int x) -> decltype(x);", SpaceFuncDecl);
14757   verifyFormat("auto f (int x) -> typeof(x);", SpaceFuncDecl);
14758   verifyFormat("auto f (int x) -> _Atomic(x);", SpaceFuncDecl);
14759   verifyFormat("auto f (int x) -> __underlying_type(x);", SpaceFuncDecl);
14760   verifyFormat("int f (T x) noexcept(x.create());", SpaceFuncDecl);
14761   verifyFormat("alignas(128) char a[128];", SpaceFuncDecl);
14762   verifyFormat("size_t x = alignof(MyType);", SpaceFuncDecl);
14763   verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");",
14764                SpaceFuncDecl);
14765   verifyFormat("int f () throw(Deprecated);", SpaceFuncDecl);
14766   verifyFormat("typedef void (*cb)(int);", SpaceFuncDecl);
14767   // FIXME these tests regressed behaviour.
14768   // verifyFormat("T A::operator() ();", SpaceFuncDecl);
14769   // verifyFormat("X A::operator++ (T);", SpaceFuncDecl);
14770   verifyFormat("T A::operator()() {}", SpaceFuncDecl);
14771   verifyFormat("auto lambda = []() { return 0; };", SpaceFuncDecl);
14772   verifyFormat("int x = int(y);", SpaceFuncDecl);
14773   verifyFormat("M(std::size_t R, std::size_t C) : C(C), data(R) {}",
14774                SpaceFuncDecl);
14775 
14776   FormatStyle SpaceFuncDef = getLLVMStyle();
14777   SpaceFuncDef.SpaceBeforeParens = FormatStyle::SBPO_Custom;
14778   SpaceFuncDef.SpaceBeforeParensOptions.AfterFunctionDefinitionName = true;
14779 
14780   verifyFormat("int f();", SpaceFuncDef);
14781   verifyFormat("void f (int a, T b) {}", SpaceFuncDef);
14782   verifyFormat("A::A() : a(1) {}", SpaceFuncDef);
14783   verifyFormat("void f() __attribute__((asdf));", SpaceFuncDef);
14784   verifyFormat("#define A(x) x", SpaceFuncDef);
14785   verifyFormat("#define A (x) x", SpaceFuncDef);
14786   verifyFormat("#if defined(x)\n"
14787                "#endif",
14788                SpaceFuncDef);
14789   verifyFormat("auto i = std::make_unique<int>(5);", SpaceFuncDef);
14790   verifyFormat("size_t x = sizeof(x);", SpaceFuncDef);
14791   verifyFormat("auto f(int x) -> decltype(x);", SpaceFuncDef);
14792   verifyFormat("auto f(int x) -> typeof(x);", SpaceFuncDef);
14793   verifyFormat("auto f(int x) -> _Atomic(x);", SpaceFuncDef);
14794   verifyFormat("auto f(int x) -> __underlying_type(x);", SpaceFuncDef);
14795   verifyFormat("int f(T x) noexcept(x.create());", SpaceFuncDef);
14796   verifyFormat("alignas(128) char a[128];", SpaceFuncDef);
14797   verifyFormat("size_t x = alignof(MyType);", SpaceFuncDef);
14798   verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");",
14799                SpaceFuncDef);
14800   verifyFormat("int f() throw(Deprecated);", SpaceFuncDef);
14801   verifyFormat("typedef void (*cb)(int);", SpaceFuncDef);
14802   verifyFormat("T A::operator()();", SpaceFuncDef);
14803   verifyFormat("X A::operator++(T);", SpaceFuncDef);
14804   // verifyFormat("T A::operator() () {}", SpaceFuncDef);
14805   verifyFormat("auto lambda = [] () { return 0; };", SpaceFuncDef);
14806   verifyFormat("int x = int(y);", SpaceFuncDef);
14807   verifyFormat("M(std::size_t R, std::size_t C) : C(C), data(R) {}",
14808                SpaceFuncDef);
14809 
14810   FormatStyle SpaceIfMacros = getLLVMStyle();
14811   SpaceIfMacros.IfMacros.clear();
14812   SpaceIfMacros.IfMacros.push_back("MYIF");
14813   SpaceIfMacros.SpaceBeforeParens = FormatStyle::SBPO_Custom;
14814   SpaceIfMacros.SpaceBeforeParensOptions.AfterIfMacros = true;
14815   verifyFormat("MYIF (a)\n  return;", SpaceIfMacros);
14816   verifyFormat("MYIF (a)\n  return;\nelse MYIF (b)\n  return;", SpaceIfMacros);
14817   verifyFormat("MYIF (a)\n  return;\nelse\n  return;", SpaceIfMacros);
14818 
14819   FormatStyle SpaceForeachMacros = getLLVMStyle();
14820   EXPECT_EQ(SpaceForeachMacros.AllowShortBlocksOnASingleLine,
14821             FormatStyle::SBS_Never);
14822   EXPECT_EQ(SpaceForeachMacros.AllowShortLoopsOnASingleLine, false);
14823   SpaceForeachMacros.SpaceBeforeParens = FormatStyle::SBPO_Custom;
14824   SpaceForeachMacros.SpaceBeforeParensOptions.AfterForeachMacros = true;
14825   verifyFormat("for (;;) {\n"
14826                "}",
14827                SpaceForeachMacros);
14828   verifyFormat("foreach (Item *item, itemlist) {\n"
14829                "}",
14830                SpaceForeachMacros);
14831   verifyFormat("Q_FOREACH (Item *item, itemlist) {\n"
14832                "}",
14833                SpaceForeachMacros);
14834   verifyFormat("BOOST_FOREACH (Item *item, itemlist) {\n"
14835                "}",
14836                SpaceForeachMacros);
14837   verifyFormat("UNKNOWN_FOREACH(Item *item, itemlist) {}", SpaceForeachMacros);
14838 
14839   FormatStyle SomeSpace2 = getLLVMStyle();
14840   SomeSpace2.SpaceBeforeParens = FormatStyle::SBPO_Custom;
14841   SomeSpace2.SpaceBeforeParensOptions.BeforeNonEmptyParentheses = true;
14842   verifyFormat("[]() -> float {}", SomeSpace2);
14843   verifyFormat("[] (auto foo) {}", SomeSpace2);
14844   verifyFormat("[foo]() -> int {}", SomeSpace2);
14845   verifyFormat("int f();", SomeSpace2);
14846   verifyFormat("void f (int a, T b) {\n"
14847                "  while (true)\n"
14848                "    continue;\n"
14849                "}",
14850                SomeSpace2);
14851   verifyFormat("if (true)\n"
14852                "  f();\n"
14853                "else if (true)\n"
14854                "  f();",
14855                SomeSpace2);
14856   verifyFormat("do {\n"
14857                "  do_something();\n"
14858                "} while (something());",
14859                SomeSpace2);
14860   verifyFormat("switch (x) {\n"
14861                "default:\n"
14862                "  break;\n"
14863                "}",
14864                SomeSpace2);
14865   verifyFormat("A::A() : a (1) {}", SomeSpace2);
14866   verifyFormat("void f() __attribute__ ((asdf));", SomeSpace2);
14867   verifyFormat("*(&a + 1);\n"
14868                "&((&a)[1]);\n"
14869                "a[(b + c) * d];\n"
14870                "(((a + 1) * 2) + 3) * 4;",
14871                SomeSpace2);
14872   verifyFormat("#define A(x) x", SomeSpace2);
14873   verifyFormat("#define A (x) x", SomeSpace2);
14874   verifyFormat("#if defined(x)\n"
14875                "#endif",
14876                SomeSpace2);
14877   verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace2);
14878   verifyFormat("size_t x = sizeof (x);", SomeSpace2);
14879   verifyFormat("auto f (int x) -> decltype (x);", SomeSpace2);
14880   verifyFormat("auto f (int x) -> typeof (x);", SomeSpace2);
14881   verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace2);
14882   verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace2);
14883   verifyFormat("int f (T x) noexcept (x.create());", SomeSpace2);
14884   verifyFormat("alignas (128) char a[128];", SomeSpace2);
14885   verifyFormat("size_t x = alignof (MyType);", SomeSpace2);
14886   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");",
14887                SomeSpace2);
14888   verifyFormat("int f() throw (Deprecated);", SomeSpace2);
14889   verifyFormat("typedef void (*cb) (int);", SomeSpace2);
14890   verifyFormat("T A::operator()();", SomeSpace2);
14891   // verifyFormat("X A::operator++ (T);", SomeSpace2);
14892   verifyFormat("int x = int (y);", SomeSpace2);
14893   verifyFormat("auto lambda = []() { return 0; };", SomeSpace2);
14894 
14895   FormatStyle SpaceAfterOverloadedOperator = getLLVMStyle();
14896   SpaceAfterOverloadedOperator.SpaceBeforeParens = FormatStyle::SBPO_Custom;
14897   SpaceAfterOverloadedOperator.SpaceBeforeParensOptions
14898       .AfterOverloadedOperator = true;
14899 
14900   verifyFormat("auto operator++ () -> int;", SpaceAfterOverloadedOperator);
14901   verifyFormat("X A::operator++ ();", SpaceAfterOverloadedOperator);
14902   verifyFormat("some_object.operator++ ();", SpaceAfterOverloadedOperator);
14903   verifyFormat("auto func() -> int;", SpaceAfterOverloadedOperator);
14904 
14905   SpaceAfterOverloadedOperator.SpaceBeforeParensOptions
14906       .AfterOverloadedOperator = false;
14907 
14908   verifyFormat("auto operator++() -> int;", SpaceAfterOverloadedOperator);
14909   verifyFormat("X A::operator++();", SpaceAfterOverloadedOperator);
14910   verifyFormat("some_object.operator++();", SpaceAfterOverloadedOperator);
14911   verifyFormat("auto func() -> int;", SpaceAfterOverloadedOperator);
14912 }
14913 
14914 TEST_F(FormatTest, SpaceAfterLogicalNot) {
14915   FormatStyle Spaces = getLLVMStyle();
14916   Spaces.SpaceAfterLogicalNot = true;
14917 
14918   verifyFormat("bool x = ! y", Spaces);
14919   verifyFormat("if (! isFailure())", Spaces);
14920   verifyFormat("if (! (a && b))", Spaces);
14921   verifyFormat("\"Error!\"", Spaces);
14922   verifyFormat("! ! x", Spaces);
14923 }
14924 
14925 TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
14926   FormatStyle Spaces = getLLVMStyle();
14927 
14928   Spaces.SpacesInParentheses = true;
14929   verifyFormat("do_something( ::globalVar );", Spaces);
14930   verifyFormat("call( x, y, z );", Spaces);
14931   verifyFormat("call();", Spaces);
14932   verifyFormat("std::function<void( int, int )> callback;", Spaces);
14933   verifyFormat("void inFunction() { std::function<void( int, int )> fct; }",
14934                Spaces);
14935   verifyFormat("while ( (bool)1 )\n"
14936                "  continue;",
14937                Spaces);
14938   verifyFormat("for ( ;; )\n"
14939                "  continue;",
14940                Spaces);
14941   verifyFormat("if ( true )\n"
14942                "  f();\n"
14943                "else if ( true )\n"
14944                "  f();",
14945                Spaces);
14946   verifyFormat("do {\n"
14947                "  do_something( (int)i );\n"
14948                "} while ( something() );",
14949                Spaces);
14950   verifyFormat("switch ( x ) {\n"
14951                "default:\n"
14952                "  break;\n"
14953                "}",
14954                Spaces);
14955 
14956   Spaces.SpacesInParentheses = false;
14957   Spaces.SpacesInCStyleCastParentheses = true;
14958   verifyFormat("Type *A = ( Type * )P;", Spaces);
14959   verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
14960   verifyFormat("x = ( int32 )y;", Spaces);
14961   verifyFormat("int a = ( int )(2.0f);", Spaces);
14962   verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
14963   verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
14964   verifyFormat("#define x (( int )-1)", Spaces);
14965 
14966   // Run the first set of tests again with:
14967   Spaces.SpacesInParentheses = false;
14968   Spaces.SpaceInEmptyParentheses = true;
14969   Spaces.SpacesInCStyleCastParentheses = true;
14970   verifyFormat("call(x, y, z);", Spaces);
14971   verifyFormat("call( );", Spaces);
14972   verifyFormat("std::function<void(int, int)> callback;", Spaces);
14973   verifyFormat("while (( bool )1)\n"
14974                "  continue;",
14975                Spaces);
14976   verifyFormat("for (;;)\n"
14977                "  continue;",
14978                Spaces);
14979   verifyFormat("if (true)\n"
14980                "  f( );\n"
14981                "else if (true)\n"
14982                "  f( );",
14983                Spaces);
14984   verifyFormat("do {\n"
14985                "  do_something(( int )i);\n"
14986                "} while (something( ));",
14987                Spaces);
14988   verifyFormat("switch (x) {\n"
14989                "default:\n"
14990                "  break;\n"
14991                "}",
14992                Spaces);
14993 
14994   // Run the first set of tests again with:
14995   Spaces.SpaceAfterCStyleCast = true;
14996   verifyFormat("call(x, y, z);", Spaces);
14997   verifyFormat("call( );", Spaces);
14998   verifyFormat("std::function<void(int, int)> callback;", Spaces);
14999   verifyFormat("while (( bool ) 1)\n"
15000                "  continue;",
15001                Spaces);
15002   verifyFormat("for (;;)\n"
15003                "  continue;",
15004                Spaces);
15005   verifyFormat("if (true)\n"
15006                "  f( );\n"
15007                "else if (true)\n"
15008                "  f( );",
15009                Spaces);
15010   verifyFormat("do {\n"
15011                "  do_something(( int ) i);\n"
15012                "} while (something( ));",
15013                Spaces);
15014   verifyFormat("switch (x) {\n"
15015                "default:\n"
15016                "  break;\n"
15017                "}",
15018                Spaces);
15019   verifyFormat("#define CONF_BOOL(x) ( bool * ) ( void * ) (x)", Spaces);
15020   verifyFormat("#define CONF_BOOL(x) ( bool * ) (x)", Spaces);
15021   verifyFormat("#define CONF_BOOL(x) ( bool ) (x)", Spaces);
15022   verifyFormat("bool *y = ( bool * ) ( void * ) (x);", Spaces);
15023   verifyFormat("bool *y = ( bool * ) (x);", Spaces);
15024 
15025   // Run subset of tests again with:
15026   Spaces.SpacesInCStyleCastParentheses = false;
15027   Spaces.SpaceAfterCStyleCast = true;
15028   verifyFormat("while ((bool) 1)\n"
15029                "  continue;",
15030                Spaces);
15031   verifyFormat("do {\n"
15032                "  do_something((int) i);\n"
15033                "} while (something( ));",
15034                Spaces);
15035 
15036   verifyFormat("size_t idx = (size_t) (ptr - ((char *) file));", Spaces);
15037   verifyFormat("size_t idx = (size_t) a;", Spaces);
15038   verifyFormat("size_t idx = (size_t) (a - 1);", Spaces);
15039   verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces);
15040   verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces);
15041   verifyFormat("size_t idx = (*foo)(a - 1);", Spaces);
15042   verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces);
15043   verifyFormat("#define CONF_BOOL(x) (bool *) (void *) (x)", Spaces);
15044   verifyFormat("#define CONF_BOOL(x) (bool *) (void *) (int) (x)", Spaces);
15045   verifyFormat("bool *y = (bool *) (void *) (x);", Spaces);
15046   verifyFormat("bool *y = (bool *) (void *) (int) (x);", Spaces);
15047   verifyFormat("bool *y = (bool *) (void *) (int) foo(x);", Spaces);
15048   Spaces.ColumnLimit = 80;
15049   Spaces.IndentWidth = 4;
15050   Spaces.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
15051   verifyFormat("void foo( ) {\n"
15052                "    size_t foo = (*(function))(\n"
15053                "        Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, "
15054                "BarrrrrrrrrrrrLong,\n"
15055                "        FoooooooooLooooong);\n"
15056                "}",
15057                Spaces);
15058   Spaces.SpaceAfterCStyleCast = false;
15059   verifyFormat("size_t idx = (size_t)(ptr - ((char *)file));", Spaces);
15060   verifyFormat("size_t idx = (size_t)a;", Spaces);
15061   verifyFormat("size_t idx = (size_t)(a - 1);", Spaces);
15062   verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces);
15063   verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces);
15064   verifyFormat("size_t idx = (*foo)(a - 1);", Spaces);
15065   verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces);
15066 
15067   verifyFormat("void foo( ) {\n"
15068                "    size_t foo = (*(function))(\n"
15069                "        Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, "
15070                "BarrrrrrrrrrrrLong,\n"
15071                "        FoooooooooLooooong);\n"
15072                "}",
15073                Spaces);
15074 }
15075 
15076 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
15077   verifyFormat("int a[5];");
15078   verifyFormat("a[3] += 42;");
15079 
15080   FormatStyle Spaces = getLLVMStyle();
15081   Spaces.SpacesInSquareBrackets = true;
15082   // Not lambdas.
15083   verifyFormat("int a[ 5 ];", Spaces);
15084   verifyFormat("a[ 3 ] += 42;", Spaces);
15085   verifyFormat("constexpr char hello[]{\"hello\"};", Spaces);
15086   verifyFormat("double &operator[](int i) { return 0; }\n"
15087                "int i;",
15088                Spaces);
15089   verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces);
15090   verifyFormat("int i = a[ a ][ a ]->f();", Spaces);
15091   verifyFormat("int i = (*b)[ a ]->f();", Spaces);
15092   // Lambdas.
15093   verifyFormat("int c = []() -> int { return 2; }();\n", Spaces);
15094   verifyFormat("return [ i, args... ] {};", Spaces);
15095   verifyFormat("int foo = [ &bar ]() {};", Spaces);
15096   verifyFormat("int foo = [ = ]() {};", Spaces);
15097   verifyFormat("int foo = [ & ]() {};", Spaces);
15098   verifyFormat("int foo = [ =, &bar ]() {};", Spaces);
15099   verifyFormat("int foo = [ &bar, = ]() {};", Spaces);
15100 }
15101 
15102 TEST_F(FormatTest, ConfigurableSpaceBeforeBrackets) {
15103   FormatStyle NoSpaceStyle = getLLVMStyle();
15104   verifyFormat("int a[5];", NoSpaceStyle);
15105   verifyFormat("a[3] += 42;", NoSpaceStyle);
15106 
15107   verifyFormat("int a[1];", NoSpaceStyle);
15108   verifyFormat("int 1 [a];", NoSpaceStyle);
15109   verifyFormat("int a[1][2];", NoSpaceStyle);
15110   verifyFormat("a[7] = 5;", NoSpaceStyle);
15111   verifyFormat("int a = (f())[23];", NoSpaceStyle);
15112   verifyFormat("f([] {})", NoSpaceStyle);
15113 
15114   FormatStyle Space = getLLVMStyle();
15115   Space.SpaceBeforeSquareBrackets = true;
15116   verifyFormat("int c = []() -> int { return 2; }();\n", Space);
15117   verifyFormat("return [i, args...] {};", Space);
15118 
15119   verifyFormat("int a [5];", Space);
15120   verifyFormat("a [3] += 42;", Space);
15121   verifyFormat("constexpr char hello []{\"hello\"};", Space);
15122   verifyFormat("double &operator[](int i) { return 0; }\n"
15123                "int i;",
15124                Space);
15125   verifyFormat("std::unique_ptr<int []> foo() {}", Space);
15126   verifyFormat("int i = a [a][a]->f();", Space);
15127   verifyFormat("int i = (*b) [a]->f();", Space);
15128 
15129   verifyFormat("int a [1];", Space);
15130   verifyFormat("int 1 [a];", Space);
15131   verifyFormat("int a [1][2];", Space);
15132   verifyFormat("a [7] = 5;", Space);
15133   verifyFormat("int a = (f()) [23];", Space);
15134   verifyFormat("f([] {})", Space);
15135 }
15136 
15137 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
15138   verifyFormat("int a = 5;");
15139   verifyFormat("a += 42;");
15140   verifyFormat("a or_eq 8;");
15141 
15142   FormatStyle Spaces = getLLVMStyle();
15143   Spaces.SpaceBeforeAssignmentOperators = false;
15144   verifyFormat("int a= 5;", Spaces);
15145   verifyFormat("a+= 42;", Spaces);
15146   verifyFormat("a or_eq 8;", Spaces);
15147 }
15148 
15149 TEST_F(FormatTest, ConfigurableSpaceBeforeColon) {
15150   verifyFormat("class Foo : public Bar {};");
15151   verifyFormat("Foo::Foo() : foo(1) {}");
15152   verifyFormat("for (auto a : b) {\n}");
15153   verifyFormat("int x = a ? b : c;");
15154   verifyFormat("{\n"
15155                "label0:\n"
15156                "  int x = 0;\n"
15157                "}");
15158   verifyFormat("switch (x) {\n"
15159                "case 1:\n"
15160                "default:\n"
15161                "}");
15162   verifyFormat("switch (allBraces) {\n"
15163                "case 1: {\n"
15164                "  break;\n"
15165                "}\n"
15166                "case 2: {\n"
15167                "  [[fallthrough]];\n"
15168                "}\n"
15169                "default: {\n"
15170                "  break;\n"
15171                "}\n"
15172                "}");
15173 
15174   FormatStyle CtorInitializerStyle = getLLVMStyleWithColumns(30);
15175   CtorInitializerStyle.SpaceBeforeCtorInitializerColon = false;
15176   verifyFormat("class Foo : public Bar {};", CtorInitializerStyle);
15177   verifyFormat("Foo::Foo(): foo(1) {}", CtorInitializerStyle);
15178   verifyFormat("for (auto a : b) {\n}", CtorInitializerStyle);
15179   verifyFormat("int x = a ? b : c;", CtorInitializerStyle);
15180   verifyFormat("{\n"
15181                "label1:\n"
15182                "  int x = 0;\n"
15183                "}",
15184                CtorInitializerStyle);
15185   verifyFormat("switch (x) {\n"
15186                "case 1:\n"
15187                "default:\n"
15188                "}",
15189                CtorInitializerStyle);
15190   verifyFormat("switch (allBraces) {\n"
15191                "case 1: {\n"
15192                "  break;\n"
15193                "}\n"
15194                "case 2: {\n"
15195                "  [[fallthrough]];\n"
15196                "}\n"
15197                "default: {\n"
15198                "  break;\n"
15199                "}\n"
15200                "}",
15201                CtorInitializerStyle);
15202   CtorInitializerStyle.BreakConstructorInitializers =
15203       FormatStyle::BCIS_AfterColon;
15204   verifyFormat("Fooooooooooo::Fooooooooooo():\n"
15205                "    aaaaaaaaaaaaaaaa(1),\n"
15206                "    bbbbbbbbbbbbbbbb(2) {}",
15207                CtorInitializerStyle);
15208   CtorInitializerStyle.BreakConstructorInitializers =
15209       FormatStyle::BCIS_BeforeComma;
15210   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
15211                "    : aaaaaaaaaaaaaaaa(1)\n"
15212                "    , bbbbbbbbbbbbbbbb(2) {}",
15213                CtorInitializerStyle);
15214   CtorInitializerStyle.BreakConstructorInitializers =
15215       FormatStyle::BCIS_BeforeColon;
15216   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
15217                "    : aaaaaaaaaaaaaaaa(1),\n"
15218                "      bbbbbbbbbbbbbbbb(2) {}",
15219                CtorInitializerStyle);
15220   CtorInitializerStyle.ConstructorInitializerIndentWidth = 0;
15221   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
15222                ": aaaaaaaaaaaaaaaa(1),\n"
15223                "  bbbbbbbbbbbbbbbb(2) {}",
15224                CtorInitializerStyle);
15225 
15226   FormatStyle InheritanceStyle = getLLVMStyleWithColumns(30);
15227   InheritanceStyle.SpaceBeforeInheritanceColon = false;
15228   verifyFormat("class Foo: public Bar {};", InheritanceStyle);
15229   verifyFormat("Foo::Foo() : foo(1) {}", InheritanceStyle);
15230   verifyFormat("for (auto a : b) {\n}", InheritanceStyle);
15231   verifyFormat("int x = a ? b : c;", InheritanceStyle);
15232   verifyFormat("{\n"
15233                "label2:\n"
15234                "  int x = 0;\n"
15235                "}",
15236                InheritanceStyle);
15237   verifyFormat("switch (x) {\n"
15238                "case 1:\n"
15239                "default:\n"
15240                "}",
15241                InheritanceStyle);
15242   verifyFormat("switch (allBraces) {\n"
15243                "case 1: {\n"
15244                "  break;\n"
15245                "}\n"
15246                "case 2: {\n"
15247                "  [[fallthrough]];\n"
15248                "}\n"
15249                "default: {\n"
15250                "  break;\n"
15251                "}\n"
15252                "}",
15253                InheritanceStyle);
15254   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterComma;
15255   verifyFormat("class Foooooooooooooooooooooo\n"
15256                "    : public aaaaaaaaaaaaaaaaaa,\n"
15257                "      public bbbbbbbbbbbbbbbbbb {\n"
15258                "}",
15259                InheritanceStyle);
15260   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterColon;
15261   verifyFormat("class Foooooooooooooooooooooo:\n"
15262                "    public aaaaaaaaaaaaaaaaaa,\n"
15263                "    public bbbbbbbbbbbbbbbbbb {\n"
15264                "}",
15265                InheritanceStyle);
15266   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeComma;
15267   verifyFormat("class Foooooooooooooooooooooo\n"
15268                "    : public aaaaaaaaaaaaaaaaaa\n"
15269                "    , public bbbbbbbbbbbbbbbbbb {\n"
15270                "}",
15271                InheritanceStyle);
15272   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
15273   verifyFormat("class Foooooooooooooooooooooo\n"
15274                "    : public aaaaaaaaaaaaaaaaaa,\n"
15275                "      public bbbbbbbbbbbbbbbbbb {\n"
15276                "}",
15277                InheritanceStyle);
15278   InheritanceStyle.ConstructorInitializerIndentWidth = 0;
15279   verifyFormat("class Foooooooooooooooooooooo\n"
15280                ": public aaaaaaaaaaaaaaaaaa,\n"
15281                "  public bbbbbbbbbbbbbbbbbb {}",
15282                InheritanceStyle);
15283 
15284   FormatStyle ForLoopStyle = getLLVMStyle();
15285   ForLoopStyle.SpaceBeforeRangeBasedForLoopColon = false;
15286   verifyFormat("class Foo : public Bar {};", ForLoopStyle);
15287   verifyFormat("Foo::Foo() : foo(1) {}", ForLoopStyle);
15288   verifyFormat("for (auto a: b) {\n}", ForLoopStyle);
15289   verifyFormat("int x = a ? b : c;", ForLoopStyle);
15290   verifyFormat("{\n"
15291                "label2:\n"
15292                "  int x = 0;\n"
15293                "}",
15294                ForLoopStyle);
15295   verifyFormat("switch (x) {\n"
15296                "case 1:\n"
15297                "default:\n"
15298                "}",
15299                ForLoopStyle);
15300   verifyFormat("switch (allBraces) {\n"
15301                "case 1: {\n"
15302                "  break;\n"
15303                "}\n"
15304                "case 2: {\n"
15305                "  [[fallthrough]];\n"
15306                "}\n"
15307                "default: {\n"
15308                "  break;\n"
15309                "}\n"
15310                "}",
15311                ForLoopStyle);
15312 
15313   FormatStyle CaseStyle = getLLVMStyle();
15314   CaseStyle.SpaceBeforeCaseColon = true;
15315   verifyFormat("class Foo : public Bar {};", CaseStyle);
15316   verifyFormat("Foo::Foo() : foo(1) {}", CaseStyle);
15317   verifyFormat("for (auto a : b) {\n}", CaseStyle);
15318   verifyFormat("int x = a ? b : c;", CaseStyle);
15319   verifyFormat("switch (x) {\n"
15320                "case 1 :\n"
15321                "default :\n"
15322                "}",
15323                CaseStyle);
15324   verifyFormat("switch (allBraces) {\n"
15325                "case 1 : {\n"
15326                "  break;\n"
15327                "}\n"
15328                "case 2 : {\n"
15329                "  [[fallthrough]];\n"
15330                "}\n"
15331                "default : {\n"
15332                "  break;\n"
15333                "}\n"
15334                "}",
15335                CaseStyle);
15336 
15337   FormatStyle NoSpaceStyle = getLLVMStyle();
15338   EXPECT_EQ(NoSpaceStyle.SpaceBeforeCaseColon, false);
15339   NoSpaceStyle.SpaceBeforeCtorInitializerColon = false;
15340   NoSpaceStyle.SpaceBeforeInheritanceColon = false;
15341   NoSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false;
15342   verifyFormat("class Foo: public Bar {};", NoSpaceStyle);
15343   verifyFormat("Foo::Foo(): foo(1) {}", NoSpaceStyle);
15344   verifyFormat("for (auto a: b) {\n}", NoSpaceStyle);
15345   verifyFormat("int x = a ? b : c;", NoSpaceStyle);
15346   verifyFormat("{\n"
15347                "label3:\n"
15348                "  int x = 0;\n"
15349                "}",
15350                NoSpaceStyle);
15351   verifyFormat("switch (x) {\n"
15352                "case 1:\n"
15353                "default:\n"
15354                "}",
15355                NoSpaceStyle);
15356   verifyFormat("switch (allBraces) {\n"
15357                "case 1: {\n"
15358                "  break;\n"
15359                "}\n"
15360                "case 2: {\n"
15361                "  [[fallthrough]];\n"
15362                "}\n"
15363                "default: {\n"
15364                "  break;\n"
15365                "}\n"
15366                "}",
15367                NoSpaceStyle);
15368 
15369   FormatStyle InvertedSpaceStyle = getLLVMStyle();
15370   InvertedSpaceStyle.SpaceBeforeCaseColon = true;
15371   InvertedSpaceStyle.SpaceBeforeCtorInitializerColon = false;
15372   InvertedSpaceStyle.SpaceBeforeInheritanceColon = false;
15373   InvertedSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false;
15374   verifyFormat("class Foo: public Bar {};", InvertedSpaceStyle);
15375   verifyFormat("Foo::Foo(): foo(1) {}", InvertedSpaceStyle);
15376   verifyFormat("for (auto a: b) {\n}", InvertedSpaceStyle);
15377   verifyFormat("int x = a ? b : c;", InvertedSpaceStyle);
15378   verifyFormat("{\n"
15379                "label3:\n"
15380                "  int x = 0;\n"
15381                "}",
15382                InvertedSpaceStyle);
15383   verifyFormat("switch (x) {\n"
15384                "case 1 :\n"
15385                "case 2 : {\n"
15386                "  break;\n"
15387                "}\n"
15388                "default :\n"
15389                "  break;\n"
15390                "}",
15391                InvertedSpaceStyle);
15392   verifyFormat("switch (allBraces) {\n"
15393                "case 1 : {\n"
15394                "  break;\n"
15395                "}\n"
15396                "case 2 : {\n"
15397                "  [[fallthrough]];\n"
15398                "}\n"
15399                "default : {\n"
15400                "  break;\n"
15401                "}\n"
15402                "}",
15403                InvertedSpaceStyle);
15404 }
15405 
15406 TEST_F(FormatTest, ConfigurableSpaceAroundPointerQualifiers) {
15407   FormatStyle Style = getLLVMStyle();
15408 
15409   Style.PointerAlignment = FormatStyle::PAS_Left;
15410   Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default;
15411   verifyFormat("void* const* x = NULL;", Style);
15412 
15413 #define verifyQualifierSpaces(Code, Pointers, Qualifiers)                      \
15414   do {                                                                         \
15415     Style.PointerAlignment = FormatStyle::Pointers;                            \
15416     Style.SpaceAroundPointerQualifiers = FormatStyle::Qualifiers;              \
15417     verifyFormat(Code, Style);                                                 \
15418   } while (false)
15419 
15420   verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Default);
15421   verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_Default);
15422   verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Default);
15423 
15424   verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Before);
15425   verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Before);
15426   verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Before);
15427 
15428   verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_After);
15429   verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_After);
15430   verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_After);
15431 
15432   verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_Both);
15433   verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Both);
15434   verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Both);
15435 
15436   verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Default);
15437   verifyQualifierSpaces("Foo::operator void const *();", PAS_Right,
15438                         SAPQ_Default);
15439   verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
15440                         SAPQ_Default);
15441 
15442   verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Before);
15443   verifyQualifierSpaces("Foo::operator void const *();", PAS_Right,
15444                         SAPQ_Before);
15445   verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
15446                         SAPQ_Before);
15447 
15448   verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_After);
15449   verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_After);
15450   verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
15451                         SAPQ_After);
15452 
15453   verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_Both);
15454   verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_Both);
15455   verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, SAPQ_Both);
15456 
15457 #undef verifyQualifierSpaces
15458 
15459   FormatStyle Spaces = getLLVMStyle();
15460   Spaces.AttributeMacros.push_back("qualified");
15461   Spaces.PointerAlignment = FormatStyle::PAS_Right;
15462   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default;
15463   verifyFormat("SomeType *volatile *a = NULL;", Spaces);
15464   verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces);
15465   verifyFormat("std::vector<SomeType *const *> x;", Spaces);
15466   verifyFormat("std::vector<SomeType *qualified *> x;", Spaces);
15467   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
15468   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before;
15469   verifyFormat("SomeType * volatile *a = NULL;", Spaces);
15470   verifyFormat("SomeType * __attribute__((attr)) *a = NULL;", Spaces);
15471   verifyFormat("std::vector<SomeType * const *> x;", Spaces);
15472   verifyFormat("std::vector<SomeType * qualified *> x;", Spaces);
15473   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
15474 
15475   // Check that SAPQ_Before doesn't result in extra spaces for PAS_Left.
15476   Spaces.PointerAlignment = FormatStyle::PAS_Left;
15477   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before;
15478   verifyFormat("SomeType* volatile* a = NULL;", Spaces);
15479   verifyFormat("SomeType* __attribute__((attr))* a = NULL;", Spaces);
15480   verifyFormat("std::vector<SomeType* const*> x;", Spaces);
15481   verifyFormat("std::vector<SomeType* qualified*> x;", Spaces);
15482   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
15483   // However, setting it to SAPQ_After should add spaces after __attribute, etc.
15484   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After;
15485   verifyFormat("SomeType* volatile * a = NULL;", Spaces);
15486   verifyFormat("SomeType* __attribute__((attr)) * a = NULL;", Spaces);
15487   verifyFormat("std::vector<SomeType* const *> x;", Spaces);
15488   verifyFormat("std::vector<SomeType* qualified *> x;", Spaces);
15489   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
15490 
15491   // PAS_Middle should not have any noticeable changes even for SAPQ_Both
15492   Spaces.PointerAlignment = FormatStyle::PAS_Middle;
15493   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After;
15494   verifyFormat("SomeType * volatile * a = NULL;", Spaces);
15495   verifyFormat("SomeType * __attribute__((attr)) * a = NULL;", Spaces);
15496   verifyFormat("std::vector<SomeType * const *> x;", Spaces);
15497   verifyFormat("std::vector<SomeType * qualified *> x;", Spaces);
15498   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
15499 }
15500 
15501 TEST_F(FormatTest, AlignConsecutiveMacros) {
15502   FormatStyle Style = getLLVMStyle();
15503   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
15504   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
15505   Style.AlignConsecutiveMacros = FormatStyle::ACS_None;
15506 
15507   verifyFormat("#define a 3\n"
15508                "#define bbbb 4\n"
15509                "#define ccc (5)",
15510                Style);
15511 
15512   verifyFormat("#define f(x) (x * x)\n"
15513                "#define fff(x, y, z) (x * y + z)\n"
15514                "#define ffff(x, y) (x - y)",
15515                Style);
15516 
15517   verifyFormat("#define foo(x, y) (x + y)\n"
15518                "#define bar (5, 6)(2 + 2)",
15519                Style);
15520 
15521   verifyFormat("#define a 3\n"
15522                "#define bbbb 4\n"
15523                "#define ccc (5)\n"
15524                "#define f(x) (x * x)\n"
15525                "#define fff(x, y, z) (x * y + z)\n"
15526                "#define ffff(x, y) (x - y)",
15527                Style);
15528 
15529   Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
15530   verifyFormat("#define a    3\n"
15531                "#define bbbb 4\n"
15532                "#define ccc  (5)",
15533                Style);
15534 
15535   verifyFormat("#define f(x)         (x * x)\n"
15536                "#define fff(x, y, z) (x * y + z)\n"
15537                "#define ffff(x, y)   (x - y)",
15538                Style);
15539 
15540   verifyFormat("#define foo(x, y) (x + y)\n"
15541                "#define bar       (5, 6)(2 + 2)",
15542                Style);
15543 
15544   verifyFormat("#define a            3\n"
15545                "#define bbbb         4\n"
15546                "#define ccc          (5)\n"
15547                "#define f(x)         (x * x)\n"
15548                "#define fff(x, y, z) (x * y + z)\n"
15549                "#define ffff(x, y)   (x - y)",
15550                Style);
15551 
15552   verifyFormat("#define a         5\n"
15553                "#define foo(x, y) (x + y)\n"
15554                "#define CCC       (6)\n"
15555                "auto lambda = []() {\n"
15556                "  auto  ii = 0;\n"
15557                "  float j  = 0;\n"
15558                "  return 0;\n"
15559                "};\n"
15560                "int   i  = 0;\n"
15561                "float i2 = 0;\n"
15562                "auto  v  = type{\n"
15563                "    i = 1,   //\n"
15564                "    (i = 2), //\n"
15565                "    i = 3    //\n"
15566                "};",
15567                Style);
15568 
15569   Style.AlignConsecutiveMacros = FormatStyle::ACS_None;
15570   Style.ColumnLimit = 20;
15571 
15572   verifyFormat("#define a          \\\n"
15573                "  \"aabbbbbbbbbbbb\"\n"
15574                "#define D          \\\n"
15575                "  \"aabbbbbbbbbbbb\" \\\n"
15576                "  \"ccddeeeeeeeee\"\n"
15577                "#define B          \\\n"
15578                "  \"QQQQQQQQQQQQQ\"  \\\n"
15579                "  \"FFFFFFFFFFFFF\"  \\\n"
15580                "  \"LLLLLLLL\"\n",
15581                Style);
15582 
15583   Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
15584   verifyFormat("#define a          \\\n"
15585                "  \"aabbbbbbbbbbbb\"\n"
15586                "#define D          \\\n"
15587                "  \"aabbbbbbbbbbbb\" \\\n"
15588                "  \"ccddeeeeeeeee\"\n"
15589                "#define B          \\\n"
15590                "  \"QQQQQQQQQQQQQ\"  \\\n"
15591                "  \"FFFFFFFFFFFFF\"  \\\n"
15592                "  \"LLLLLLLL\"\n",
15593                Style);
15594 
15595   // Test across comments
15596   Style.MaxEmptyLinesToKeep = 10;
15597   Style.ReflowComments = false;
15598   Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossComments;
15599   EXPECT_EQ("#define a    3\n"
15600             "// line comment\n"
15601             "#define bbbb 4\n"
15602             "#define ccc  (5)",
15603             format("#define a 3\n"
15604                    "// line comment\n"
15605                    "#define bbbb 4\n"
15606                    "#define ccc (5)",
15607                    Style));
15608 
15609   EXPECT_EQ("#define a    3\n"
15610             "/* block comment */\n"
15611             "#define bbbb 4\n"
15612             "#define ccc  (5)",
15613             format("#define a  3\n"
15614                    "/* block comment */\n"
15615                    "#define bbbb 4\n"
15616                    "#define ccc (5)",
15617                    Style));
15618 
15619   EXPECT_EQ("#define a    3\n"
15620             "/* multi-line *\n"
15621             " * block comment */\n"
15622             "#define bbbb 4\n"
15623             "#define ccc  (5)",
15624             format("#define a 3\n"
15625                    "/* multi-line *\n"
15626                    " * block comment */\n"
15627                    "#define bbbb 4\n"
15628                    "#define ccc (5)",
15629                    Style));
15630 
15631   EXPECT_EQ("#define a    3\n"
15632             "// multi-line line comment\n"
15633             "//\n"
15634             "#define bbbb 4\n"
15635             "#define ccc  (5)",
15636             format("#define a  3\n"
15637                    "// multi-line line comment\n"
15638                    "//\n"
15639                    "#define bbbb 4\n"
15640                    "#define ccc (5)",
15641                    Style));
15642 
15643   EXPECT_EQ("#define a 3\n"
15644             "// empty lines still break.\n"
15645             "\n"
15646             "#define bbbb 4\n"
15647             "#define ccc  (5)",
15648             format("#define a     3\n"
15649                    "// empty lines still break.\n"
15650                    "\n"
15651                    "#define bbbb     4\n"
15652                    "#define ccc  (5)",
15653                    Style));
15654 
15655   // Test across empty lines
15656   Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossEmptyLines;
15657   EXPECT_EQ("#define a    3\n"
15658             "\n"
15659             "#define bbbb 4\n"
15660             "#define ccc  (5)",
15661             format("#define a 3\n"
15662                    "\n"
15663                    "#define bbbb 4\n"
15664                    "#define ccc (5)",
15665                    Style));
15666 
15667   EXPECT_EQ("#define a    3\n"
15668             "\n"
15669             "\n"
15670             "\n"
15671             "#define bbbb 4\n"
15672             "#define ccc  (5)",
15673             format("#define a        3\n"
15674                    "\n"
15675                    "\n"
15676                    "\n"
15677                    "#define bbbb 4\n"
15678                    "#define ccc (5)",
15679                    Style));
15680 
15681   EXPECT_EQ("#define a 3\n"
15682             "// comments should break alignment\n"
15683             "//\n"
15684             "#define bbbb 4\n"
15685             "#define ccc  (5)",
15686             format("#define a        3\n"
15687                    "// comments should break alignment\n"
15688                    "//\n"
15689                    "#define bbbb 4\n"
15690                    "#define ccc (5)",
15691                    Style));
15692 
15693   // Test across empty lines and comments
15694   Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossEmptyLinesAndComments;
15695   verifyFormat("#define a    3\n"
15696                "\n"
15697                "// line comment\n"
15698                "#define bbbb 4\n"
15699                "#define ccc  (5)",
15700                Style);
15701 
15702   EXPECT_EQ("#define a    3\n"
15703             "\n"
15704             "\n"
15705             "/* multi-line *\n"
15706             " * block comment */\n"
15707             "\n"
15708             "\n"
15709             "#define bbbb 4\n"
15710             "#define ccc  (5)",
15711             format("#define a 3\n"
15712                    "\n"
15713                    "\n"
15714                    "/* multi-line *\n"
15715                    " * block comment */\n"
15716                    "\n"
15717                    "\n"
15718                    "#define bbbb 4\n"
15719                    "#define ccc (5)",
15720                    Style));
15721 
15722   EXPECT_EQ("#define a    3\n"
15723             "\n"
15724             "\n"
15725             "/* multi-line *\n"
15726             " * block comment */\n"
15727             "\n"
15728             "\n"
15729             "#define bbbb 4\n"
15730             "#define ccc  (5)",
15731             format("#define a 3\n"
15732                    "\n"
15733                    "\n"
15734                    "/* multi-line *\n"
15735                    " * block comment */\n"
15736                    "\n"
15737                    "\n"
15738                    "#define bbbb 4\n"
15739                    "#define ccc       (5)",
15740                    Style));
15741 }
15742 
15743 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLines) {
15744   FormatStyle Alignment = getLLVMStyle();
15745   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
15746   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_AcrossEmptyLines;
15747 
15748   Alignment.MaxEmptyLinesToKeep = 10;
15749   /* Test alignment across empty lines */
15750   EXPECT_EQ("int a           = 5;\n"
15751             "\n"
15752             "int oneTwoThree = 123;",
15753             format("int a       = 5;\n"
15754                    "\n"
15755                    "int oneTwoThree= 123;",
15756                    Alignment));
15757   EXPECT_EQ("int a           = 5;\n"
15758             "int one         = 1;\n"
15759             "\n"
15760             "int oneTwoThree = 123;",
15761             format("int a = 5;\n"
15762                    "int one = 1;\n"
15763                    "\n"
15764                    "int oneTwoThree = 123;",
15765                    Alignment));
15766   EXPECT_EQ("int a           = 5;\n"
15767             "int one         = 1;\n"
15768             "\n"
15769             "int oneTwoThree = 123;\n"
15770             "int oneTwo      = 12;",
15771             format("int a = 5;\n"
15772                    "int one = 1;\n"
15773                    "\n"
15774                    "int oneTwoThree = 123;\n"
15775                    "int oneTwo = 12;",
15776                    Alignment));
15777 
15778   /* Test across comments */
15779   EXPECT_EQ("int a = 5;\n"
15780             "/* block comment */\n"
15781             "int oneTwoThree = 123;",
15782             format("int a = 5;\n"
15783                    "/* block comment */\n"
15784                    "int oneTwoThree=123;",
15785                    Alignment));
15786 
15787   EXPECT_EQ("int a = 5;\n"
15788             "// line comment\n"
15789             "int oneTwoThree = 123;",
15790             format("int a = 5;\n"
15791                    "// line comment\n"
15792                    "int oneTwoThree=123;",
15793                    Alignment));
15794 
15795   /* Test across comments and newlines */
15796   EXPECT_EQ("int a = 5;\n"
15797             "\n"
15798             "/* block comment */\n"
15799             "int oneTwoThree = 123;",
15800             format("int a = 5;\n"
15801                    "\n"
15802                    "/* block comment */\n"
15803                    "int oneTwoThree=123;",
15804                    Alignment));
15805 
15806   EXPECT_EQ("int a = 5;\n"
15807             "\n"
15808             "// line comment\n"
15809             "int oneTwoThree = 123;",
15810             format("int a = 5;\n"
15811                    "\n"
15812                    "// line comment\n"
15813                    "int oneTwoThree=123;",
15814                    Alignment));
15815 }
15816 
15817 TEST_F(FormatTest, AlignConsecutiveDeclarationsAcrossEmptyLinesAndComments) {
15818   FormatStyle Alignment = getLLVMStyle();
15819   Alignment.AlignConsecutiveDeclarations =
15820       FormatStyle::ACS_AcrossEmptyLinesAndComments;
15821   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
15822 
15823   Alignment.MaxEmptyLinesToKeep = 10;
15824   /* Test alignment across empty lines */
15825   EXPECT_EQ("int         a = 5;\n"
15826             "\n"
15827             "float const oneTwoThree = 123;",
15828             format("int a = 5;\n"
15829                    "\n"
15830                    "float const oneTwoThree = 123;",
15831                    Alignment));
15832   EXPECT_EQ("int         a = 5;\n"
15833             "float const one = 1;\n"
15834             "\n"
15835             "int         oneTwoThree = 123;",
15836             format("int a = 5;\n"
15837                    "float const one = 1;\n"
15838                    "\n"
15839                    "int oneTwoThree = 123;",
15840                    Alignment));
15841 
15842   /* Test across comments */
15843   EXPECT_EQ("float const a = 5;\n"
15844             "/* block comment */\n"
15845             "int         oneTwoThree = 123;",
15846             format("float const a = 5;\n"
15847                    "/* block comment */\n"
15848                    "int oneTwoThree=123;",
15849                    Alignment));
15850 
15851   EXPECT_EQ("float const a = 5;\n"
15852             "// line comment\n"
15853             "int         oneTwoThree = 123;",
15854             format("float const a = 5;\n"
15855                    "// line comment\n"
15856                    "int oneTwoThree=123;",
15857                    Alignment));
15858 
15859   /* Test across comments and newlines */
15860   EXPECT_EQ("float const a = 5;\n"
15861             "\n"
15862             "/* block comment */\n"
15863             "int         oneTwoThree = 123;",
15864             format("float const a = 5;\n"
15865                    "\n"
15866                    "/* block comment */\n"
15867                    "int         oneTwoThree=123;",
15868                    Alignment));
15869 
15870   EXPECT_EQ("float const a = 5;\n"
15871             "\n"
15872             "// line comment\n"
15873             "int         oneTwoThree = 123;",
15874             format("float const a = 5;\n"
15875                    "\n"
15876                    "// line comment\n"
15877                    "int oneTwoThree=123;",
15878                    Alignment));
15879 }
15880 
15881 TEST_F(FormatTest, AlignConsecutiveBitFieldsAcrossEmptyLinesAndComments) {
15882   FormatStyle Alignment = getLLVMStyle();
15883   Alignment.AlignConsecutiveBitFields =
15884       FormatStyle::ACS_AcrossEmptyLinesAndComments;
15885 
15886   Alignment.MaxEmptyLinesToKeep = 10;
15887   /* Test alignment across empty lines */
15888   EXPECT_EQ("int a            : 5;\n"
15889             "\n"
15890             "int longbitfield : 6;",
15891             format("int a : 5;\n"
15892                    "\n"
15893                    "int longbitfield : 6;",
15894                    Alignment));
15895   EXPECT_EQ("int a            : 5;\n"
15896             "int one          : 1;\n"
15897             "\n"
15898             "int longbitfield : 6;",
15899             format("int a : 5;\n"
15900                    "int one : 1;\n"
15901                    "\n"
15902                    "int longbitfield : 6;",
15903                    Alignment));
15904 
15905   /* Test across comments */
15906   EXPECT_EQ("int a            : 5;\n"
15907             "/* block comment */\n"
15908             "int longbitfield : 6;",
15909             format("int a : 5;\n"
15910                    "/* block comment */\n"
15911                    "int longbitfield : 6;",
15912                    Alignment));
15913   EXPECT_EQ("int a            : 5;\n"
15914             "int one          : 1;\n"
15915             "// line comment\n"
15916             "int longbitfield : 6;",
15917             format("int a : 5;\n"
15918                    "int one : 1;\n"
15919                    "// line comment\n"
15920                    "int longbitfield : 6;",
15921                    Alignment));
15922 
15923   /* Test across comments and newlines */
15924   EXPECT_EQ("int a            : 5;\n"
15925             "/* block comment */\n"
15926             "\n"
15927             "int longbitfield : 6;",
15928             format("int a : 5;\n"
15929                    "/* block comment */\n"
15930                    "\n"
15931                    "int longbitfield : 6;",
15932                    Alignment));
15933   EXPECT_EQ("int a            : 5;\n"
15934             "int one          : 1;\n"
15935             "\n"
15936             "// line comment\n"
15937             "\n"
15938             "int longbitfield : 6;",
15939             format("int a : 5;\n"
15940                    "int one : 1;\n"
15941                    "\n"
15942                    "// line comment \n"
15943                    "\n"
15944                    "int longbitfield : 6;",
15945                    Alignment));
15946 }
15947 
15948 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossComments) {
15949   FormatStyle Alignment = getLLVMStyle();
15950   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
15951   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_AcrossComments;
15952 
15953   Alignment.MaxEmptyLinesToKeep = 10;
15954   /* Test alignment across empty lines */
15955   EXPECT_EQ("int a = 5;\n"
15956             "\n"
15957             "int oneTwoThree = 123;",
15958             format("int a       = 5;\n"
15959                    "\n"
15960                    "int oneTwoThree= 123;",
15961                    Alignment));
15962   EXPECT_EQ("int a   = 5;\n"
15963             "int one = 1;\n"
15964             "\n"
15965             "int oneTwoThree = 123;",
15966             format("int a = 5;\n"
15967                    "int one = 1;\n"
15968                    "\n"
15969                    "int oneTwoThree = 123;",
15970                    Alignment));
15971 
15972   /* Test across comments */
15973   EXPECT_EQ("int a           = 5;\n"
15974             "/* block comment */\n"
15975             "int oneTwoThree = 123;",
15976             format("int a = 5;\n"
15977                    "/* block comment */\n"
15978                    "int oneTwoThree=123;",
15979                    Alignment));
15980 
15981   EXPECT_EQ("int a           = 5;\n"
15982             "// line comment\n"
15983             "int oneTwoThree = 123;",
15984             format("int a = 5;\n"
15985                    "// line comment\n"
15986                    "int oneTwoThree=123;",
15987                    Alignment));
15988 
15989   EXPECT_EQ("int a           = 5;\n"
15990             "/*\n"
15991             " * multi-line block comment\n"
15992             " */\n"
15993             "int oneTwoThree = 123;",
15994             format("int a = 5;\n"
15995                    "/*\n"
15996                    " * multi-line block comment\n"
15997                    " */\n"
15998                    "int oneTwoThree=123;",
15999                    Alignment));
16000 
16001   EXPECT_EQ("int a           = 5;\n"
16002             "//\n"
16003             "// multi-line line comment\n"
16004             "//\n"
16005             "int oneTwoThree = 123;",
16006             format("int a = 5;\n"
16007                    "//\n"
16008                    "// multi-line line comment\n"
16009                    "//\n"
16010                    "int oneTwoThree=123;",
16011                    Alignment));
16012 
16013   /* Test across comments and newlines */
16014   EXPECT_EQ("int a = 5;\n"
16015             "\n"
16016             "/* block comment */\n"
16017             "int oneTwoThree = 123;",
16018             format("int a = 5;\n"
16019                    "\n"
16020                    "/* block comment */\n"
16021                    "int oneTwoThree=123;",
16022                    Alignment));
16023 
16024   EXPECT_EQ("int a = 5;\n"
16025             "\n"
16026             "// line comment\n"
16027             "int oneTwoThree = 123;",
16028             format("int a = 5;\n"
16029                    "\n"
16030                    "// line comment\n"
16031                    "int oneTwoThree=123;",
16032                    Alignment));
16033 }
16034 
16035 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLinesAndComments) {
16036   FormatStyle Alignment = getLLVMStyle();
16037   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
16038   Alignment.AlignConsecutiveAssignments =
16039       FormatStyle::ACS_AcrossEmptyLinesAndComments;
16040   verifyFormat("int a           = 5;\n"
16041                "int oneTwoThree = 123;",
16042                Alignment);
16043   verifyFormat("int a           = method();\n"
16044                "int oneTwoThree = 133;",
16045                Alignment);
16046   verifyFormat("a &= 5;\n"
16047                "bcd *= 5;\n"
16048                "ghtyf += 5;\n"
16049                "dvfvdb -= 5;\n"
16050                "a /= 5;\n"
16051                "vdsvsv %= 5;\n"
16052                "sfdbddfbdfbb ^= 5;\n"
16053                "dvsdsv |= 5;\n"
16054                "int dsvvdvsdvvv = 123;",
16055                Alignment);
16056   verifyFormat("int i = 1, j = 10;\n"
16057                "something = 2000;",
16058                Alignment);
16059   verifyFormat("something = 2000;\n"
16060                "int i = 1, j = 10;\n",
16061                Alignment);
16062   verifyFormat("something = 2000;\n"
16063                "another   = 911;\n"
16064                "int i = 1, j = 10;\n"
16065                "oneMore = 1;\n"
16066                "i       = 2;",
16067                Alignment);
16068   verifyFormat("int a   = 5;\n"
16069                "int one = 1;\n"
16070                "method();\n"
16071                "int oneTwoThree = 123;\n"
16072                "int oneTwo      = 12;",
16073                Alignment);
16074   verifyFormat("int oneTwoThree = 123;\n"
16075                "int oneTwo      = 12;\n"
16076                "method();\n",
16077                Alignment);
16078   verifyFormat("int oneTwoThree = 123; // comment\n"
16079                "int oneTwo      = 12;  // comment",
16080                Alignment);
16081 
16082   // Bug 25167
16083   /* Uncomment when fixed
16084     verifyFormat("#if A\n"
16085                  "#else\n"
16086                  "int aaaaaaaa = 12;\n"
16087                  "#endif\n"
16088                  "#if B\n"
16089                  "#else\n"
16090                  "int a = 12;\n"
16091                  "#endif\n",
16092                  Alignment);
16093     verifyFormat("enum foo {\n"
16094                  "#if A\n"
16095                  "#else\n"
16096                  "  aaaaaaaa = 12;\n"
16097                  "#endif\n"
16098                  "#if B\n"
16099                  "#else\n"
16100                  "  a = 12;\n"
16101                  "#endif\n"
16102                  "};\n",
16103                  Alignment);
16104   */
16105 
16106   Alignment.MaxEmptyLinesToKeep = 10;
16107   /* Test alignment across empty lines */
16108   EXPECT_EQ("int a           = 5;\n"
16109             "\n"
16110             "int oneTwoThree = 123;",
16111             format("int a       = 5;\n"
16112                    "\n"
16113                    "int oneTwoThree= 123;",
16114                    Alignment));
16115   EXPECT_EQ("int a           = 5;\n"
16116             "int one         = 1;\n"
16117             "\n"
16118             "int oneTwoThree = 123;",
16119             format("int a = 5;\n"
16120                    "int one = 1;\n"
16121                    "\n"
16122                    "int oneTwoThree = 123;",
16123                    Alignment));
16124   EXPECT_EQ("int a           = 5;\n"
16125             "int one         = 1;\n"
16126             "\n"
16127             "int oneTwoThree = 123;\n"
16128             "int oneTwo      = 12;",
16129             format("int a = 5;\n"
16130                    "int one = 1;\n"
16131                    "\n"
16132                    "int oneTwoThree = 123;\n"
16133                    "int oneTwo = 12;",
16134                    Alignment));
16135 
16136   /* Test across comments */
16137   EXPECT_EQ("int a           = 5;\n"
16138             "/* block comment */\n"
16139             "int oneTwoThree = 123;",
16140             format("int a = 5;\n"
16141                    "/* block comment */\n"
16142                    "int oneTwoThree=123;",
16143                    Alignment));
16144 
16145   EXPECT_EQ("int a           = 5;\n"
16146             "// line comment\n"
16147             "int oneTwoThree = 123;",
16148             format("int a = 5;\n"
16149                    "// line comment\n"
16150                    "int oneTwoThree=123;",
16151                    Alignment));
16152 
16153   /* Test across comments and newlines */
16154   EXPECT_EQ("int a           = 5;\n"
16155             "\n"
16156             "/* block comment */\n"
16157             "int oneTwoThree = 123;",
16158             format("int a = 5;\n"
16159                    "\n"
16160                    "/* block comment */\n"
16161                    "int oneTwoThree=123;",
16162                    Alignment));
16163 
16164   EXPECT_EQ("int a           = 5;\n"
16165             "\n"
16166             "// line comment\n"
16167             "int oneTwoThree = 123;",
16168             format("int a = 5;\n"
16169                    "\n"
16170                    "// line comment\n"
16171                    "int oneTwoThree=123;",
16172                    Alignment));
16173 
16174   EXPECT_EQ("int a           = 5;\n"
16175             "//\n"
16176             "// multi-line line comment\n"
16177             "//\n"
16178             "int oneTwoThree = 123;",
16179             format("int a = 5;\n"
16180                    "//\n"
16181                    "// multi-line line comment\n"
16182                    "//\n"
16183                    "int oneTwoThree=123;",
16184                    Alignment));
16185 
16186   EXPECT_EQ("int a           = 5;\n"
16187             "/*\n"
16188             " *  multi-line block comment\n"
16189             " */\n"
16190             "int oneTwoThree = 123;",
16191             format("int a = 5;\n"
16192                    "/*\n"
16193                    " *  multi-line block comment\n"
16194                    " */\n"
16195                    "int oneTwoThree=123;",
16196                    Alignment));
16197 
16198   EXPECT_EQ("int a           = 5;\n"
16199             "\n"
16200             "/* block comment */\n"
16201             "\n"
16202             "\n"
16203             "\n"
16204             "int oneTwoThree = 123;",
16205             format("int a = 5;\n"
16206                    "\n"
16207                    "/* block comment */\n"
16208                    "\n"
16209                    "\n"
16210                    "\n"
16211                    "int oneTwoThree=123;",
16212                    Alignment));
16213 
16214   EXPECT_EQ("int a           = 5;\n"
16215             "\n"
16216             "// line comment\n"
16217             "\n"
16218             "\n"
16219             "\n"
16220             "int oneTwoThree = 123;",
16221             format("int a = 5;\n"
16222                    "\n"
16223                    "// line comment\n"
16224                    "\n"
16225                    "\n"
16226                    "\n"
16227                    "int oneTwoThree=123;",
16228                    Alignment));
16229 
16230   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
16231   verifyFormat("#define A \\\n"
16232                "  int aaaa       = 12; \\\n"
16233                "  int b          = 23; \\\n"
16234                "  int ccc        = 234; \\\n"
16235                "  int dddddddddd = 2345;",
16236                Alignment);
16237   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
16238   verifyFormat("#define A               \\\n"
16239                "  int aaaa       = 12;  \\\n"
16240                "  int b          = 23;  \\\n"
16241                "  int ccc        = 234; \\\n"
16242                "  int dddddddddd = 2345;",
16243                Alignment);
16244   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
16245   verifyFormat("#define A                                                      "
16246                "                \\\n"
16247                "  int aaaa       = 12;                                         "
16248                "                \\\n"
16249                "  int b          = 23;                                         "
16250                "                \\\n"
16251                "  int ccc        = 234;                                        "
16252                "                \\\n"
16253                "  int dddddddddd = 2345;",
16254                Alignment);
16255   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
16256                "k = 4, int l = 5,\n"
16257                "                  int m = 6) {\n"
16258                "  int j      = 10;\n"
16259                "  otherThing = 1;\n"
16260                "}",
16261                Alignment);
16262   verifyFormat("void SomeFunction(int parameter = 0) {\n"
16263                "  int i   = 1;\n"
16264                "  int j   = 2;\n"
16265                "  int big = 10000;\n"
16266                "}",
16267                Alignment);
16268   verifyFormat("class C {\n"
16269                "public:\n"
16270                "  int i            = 1;\n"
16271                "  virtual void f() = 0;\n"
16272                "};",
16273                Alignment);
16274   verifyFormat("int i = 1;\n"
16275                "if (SomeType t = getSomething()) {\n"
16276                "}\n"
16277                "int j   = 2;\n"
16278                "int big = 10000;",
16279                Alignment);
16280   verifyFormat("int j = 7;\n"
16281                "for (int k = 0; k < N; ++k) {\n"
16282                "}\n"
16283                "int j   = 2;\n"
16284                "int big = 10000;\n"
16285                "}",
16286                Alignment);
16287   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
16288   verifyFormat("int i = 1;\n"
16289                "LooooooooooongType loooooooooooooooooooooongVariable\n"
16290                "    = someLooooooooooooooooongFunction();\n"
16291                "int j = 2;",
16292                Alignment);
16293   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
16294   verifyFormat("int i = 1;\n"
16295                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
16296                "    someLooooooooooooooooongFunction();\n"
16297                "int j = 2;",
16298                Alignment);
16299 
16300   verifyFormat("auto lambda = []() {\n"
16301                "  auto i = 0;\n"
16302                "  return 0;\n"
16303                "};\n"
16304                "int i  = 0;\n"
16305                "auto v = type{\n"
16306                "    i = 1,   //\n"
16307                "    (i = 2), //\n"
16308                "    i = 3    //\n"
16309                "};",
16310                Alignment);
16311 
16312   verifyFormat(
16313       "int i      = 1;\n"
16314       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
16315       "                          loooooooooooooooooooooongParameterB);\n"
16316       "int j      = 2;",
16317       Alignment);
16318 
16319   verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
16320                "          typename B   = very_long_type_name_1,\n"
16321                "          typename T_2 = very_long_type_name_2>\n"
16322                "auto foo() {}\n",
16323                Alignment);
16324   verifyFormat("int a, b = 1;\n"
16325                "int c  = 2;\n"
16326                "int dd = 3;\n",
16327                Alignment);
16328   verifyFormat("int aa       = ((1 > 2) ? 3 : 4);\n"
16329                "float b[1][] = {{3.f}};\n",
16330                Alignment);
16331   verifyFormat("for (int i = 0; i < 1; i++)\n"
16332                "  int x = 1;\n",
16333                Alignment);
16334   verifyFormat("for (i = 0; i < 1; i++)\n"
16335                "  x = 1;\n"
16336                "y = 1;\n",
16337                Alignment);
16338 
16339   Alignment.ReflowComments = true;
16340   Alignment.ColumnLimit = 50;
16341   EXPECT_EQ("int x   = 0;\n"
16342             "int yy  = 1; /// specificlennospace\n"
16343             "int zzz = 2;\n",
16344             format("int x   = 0;\n"
16345                    "int yy  = 1; ///specificlennospace\n"
16346                    "int zzz = 2;\n",
16347                    Alignment));
16348 }
16349 
16350 TEST_F(FormatTest, AlignConsecutiveAssignments) {
16351   FormatStyle Alignment = getLLVMStyle();
16352   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
16353   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
16354   verifyFormat("int a = 5;\n"
16355                "int oneTwoThree = 123;",
16356                Alignment);
16357   verifyFormat("int a = 5;\n"
16358                "int oneTwoThree = 123;",
16359                Alignment);
16360 
16361   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16362   verifyFormat("int a           = 5;\n"
16363                "int oneTwoThree = 123;",
16364                Alignment);
16365   verifyFormat("int a           = method();\n"
16366                "int oneTwoThree = 133;",
16367                Alignment);
16368   verifyFormat("a &= 5;\n"
16369                "bcd *= 5;\n"
16370                "ghtyf += 5;\n"
16371                "dvfvdb -= 5;\n"
16372                "a /= 5;\n"
16373                "vdsvsv %= 5;\n"
16374                "sfdbddfbdfbb ^= 5;\n"
16375                "dvsdsv |= 5;\n"
16376                "int dsvvdvsdvvv = 123;",
16377                Alignment);
16378   verifyFormat("int i = 1, j = 10;\n"
16379                "something = 2000;",
16380                Alignment);
16381   verifyFormat("something = 2000;\n"
16382                "int i = 1, j = 10;\n",
16383                Alignment);
16384   verifyFormat("something = 2000;\n"
16385                "another   = 911;\n"
16386                "int i = 1, j = 10;\n"
16387                "oneMore = 1;\n"
16388                "i       = 2;",
16389                Alignment);
16390   verifyFormat("int a   = 5;\n"
16391                "int one = 1;\n"
16392                "method();\n"
16393                "int oneTwoThree = 123;\n"
16394                "int oneTwo      = 12;",
16395                Alignment);
16396   verifyFormat("int oneTwoThree = 123;\n"
16397                "int oneTwo      = 12;\n"
16398                "method();\n",
16399                Alignment);
16400   verifyFormat("int oneTwoThree = 123; // comment\n"
16401                "int oneTwo      = 12;  // comment",
16402                Alignment);
16403   verifyFormat("int f()         = default;\n"
16404                "int &operator() = default;\n"
16405                "int &operator=() {",
16406                Alignment);
16407   verifyFormat("int f()         = delete;\n"
16408                "int &operator() = delete;\n"
16409                "int &operator=() {",
16410                Alignment);
16411   verifyFormat("int f()         = default; // comment\n"
16412                "int &operator() = default; // comment\n"
16413                "int &operator=() {",
16414                Alignment);
16415   verifyFormat("int f()         = default;\n"
16416                "int &operator() = default;\n"
16417                "int &operator==() {",
16418                Alignment);
16419   verifyFormat("int f()         = default;\n"
16420                "int &operator() = default;\n"
16421                "int &operator<=() {",
16422                Alignment);
16423   verifyFormat("int f()         = default;\n"
16424                "int &operator() = default;\n"
16425                "int &operator!=() {",
16426                Alignment);
16427   verifyFormat("int f()         = default;\n"
16428                "int &operator() = default;\n"
16429                "int &operator=();",
16430                Alignment);
16431   verifyFormat("int f()         = delete;\n"
16432                "int &operator() = delete;\n"
16433                "int &operator=();",
16434                Alignment);
16435   verifyFormat("/* long long padding */ int f() = default;\n"
16436                "int &operator()                 = default;\n"
16437                "int &operator/**/ =();",
16438                Alignment);
16439   // https://llvm.org/PR33697
16440   FormatStyle AlignmentWithPenalty = getLLVMStyle();
16441   AlignmentWithPenalty.AlignConsecutiveAssignments =
16442       FormatStyle::ACS_Consecutive;
16443   AlignmentWithPenalty.PenaltyReturnTypeOnItsOwnLine = 5000;
16444   verifyFormat("class SSSSSSSSSSSSSSSSSSSSSSSSSSSS {\n"
16445                "  void f() = delete;\n"
16446                "  SSSSSSSSSSSSSSSSSSSSSSSSSSSS &operator=(\n"
16447                "      const SSSSSSSSSSSSSSSSSSSSSSSSSSSS &other) = delete;\n"
16448                "};\n",
16449                AlignmentWithPenalty);
16450 
16451   // Bug 25167
16452   /* Uncomment when fixed
16453     verifyFormat("#if A\n"
16454                  "#else\n"
16455                  "int aaaaaaaa = 12;\n"
16456                  "#endif\n"
16457                  "#if B\n"
16458                  "#else\n"
16459                  "int a = 12;\n"
16460                  "#endif\n",
16461                  Alignment);
16462     verifyFormat("enum foo {\n"
16463                  "#if A\n"
16464                  "#else\n"
16465                  "  aaaaaaaa = 12;\n"
16466                  "#endif\n"
16467                  "#if B\n"
16468                  "#else\n"
16469                  "  a = 12;\n"
16470                  "#endif\n"
16471                  "};\n",
16472                  Alignment);
16473   */
16474 
16475   EXPECT_EQ("int a = 5;\n"
16476             "\n"
16477             "int oneTwoThree = 123;",
16478             format("int a       = 5;\n"
16479                    "\n"
16480                    "int oneTwoThree= 123;",
16481                    Alignment));
16482   EXPECT_EQ("int a   = 5;\n"
16483             "int one = 1;\n"
16484             "\n"
16485             "int oneTwoThree = 123;",
16486             format("int a = 5;\n"
16487                    "int one = 1;\n"
16488                    "\n"
16489                    "int oneTwoThree = 123;",
16490                    Alignment));
16491   EXPECT_EQ("int a   = 5;\n"
16492             "int one = 1;\n"
16493             "\n"
16494             "int oneTwoThree = 123;\n"
16495             "int oneTwo      = 12;",
16496             format("int a = 5;\n"
16497                    "int one = 1;\n"
16498                    "\n"
16499                    "int oneTwoThree = 123;\n"
16500                    "int oneTwo = 12;",
16501                    Alignment));
16502   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
16503   verifyFormat("#define A \\\n"
16504                "  int aaaa       = 12; \\\n"
16505                "  int b          = 23; \\\n"
16506                "  int ccc        = 234; \\\n"
16507                "  int dddddddddd = 2345;",
16508                Alignment);
16509   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
16510   verifyFormat("#define A               \\\n"
16511                "  int aaaa       = 12;  \\\n"
16512                "  int b          = 23;  \\\n"
16513                "  int ccc        = 234; \\\n"
16514                "  int dddddddddd = 2345;",
16515                Alignment);
16516   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
16517   verifyFormat("#define A                                                      "
16518                "                \\\n"
16519                "  int aaaa       = 12;                                         "
16520                "                \\\n"
16521                "  int b          = 23;                                         "
16522                "                \\\n"
16523                "  int ccc        = 234;                                        "
16524                "                \\\n"
16525                "  int dddddddddd = 2345;",
16526                Alignment);
16527   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
16528                "k = 4, int l = 5,\n"
16529                "                  int m = 6) {\n"
16530                "  int j      = 10;\n"
16531                "  otherThing = 1;\n"
16532                "}",
16533                Alignment);
16534   verifyFormat("void SomeFunction(int parameter = 0) {\n"
16535                "  int i   = 1;\n"
16536                "  int j   = 2;\n"
16537                "  int big = 10000;\n"
16538                "}",
16539                Alignment);
16540   verifyFormat("class C {\n"
16541                "public:\n"
16542                "  int i            = 1;\n"
16543                "  virtual void f() = 0;\n"
16544                "};",
16545                Alignment);
16546   verifyFormat("int i = 1;\n"
16547                "if (SomeType t = getSomething()) {\n"
16548                "}\n"
16549                "int j   = 2;\n"
16550                "int big = 10000;",
16551                Alignment);
16552   verifyFormat("int j = 7;\n"
16553                "for (int k = 0; k < N; ++k) {\n"
16554                "}\n"
16555                "int j   = 2;\n"
16556                "int big = 10000;\n"
16557                "}",
16558                Alignment);
16559   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
16560   verifyFormat("int i = 1;\n"
16561                "LooooooooooongType loooooooooooooooooooooongVariable\n"
16562                "    = someLooooooooooooooooongFunction();\n"
16563                "int j = 2;",
16564                Alignment);
16565   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
16566   verifyFormat("int i = 1;\n"
16567                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
16568                "    someLooooooooooooooooongFunction();\n"
16569                "int j = 2;",
16570                Alignment);
16571 
16572   verifyFormat("auto lambda = []() {\n"
16573                "  auto i = 0;\n"
16574                "  return 0;\n"
16575                "};\n"
16576                "int i  = 0;\n"
16577                "auto v = type{\n"
16578                "    i = 1,   //\n"
16579                "    (i = 2), //\n"
16580                "    i = 3    //\n"
16581                "};",
16582                Alignment);
16583 
16584   verifyFormat(
16585       "int i      = 1;\n"
16586       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
16587       "                          loooooooooooooooooooooongParameterB);\n"
16588       "int j      = 2;",
16589       Alignment);
16590 
16591   verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
16592                "          typename B   = very_long_type_name_1,\n"
16593                "          typename T_2 = very_long_type_name_2>\n"
16594                "auto foo() {}\n",
16595                Alignment);
16596   verifyFormat("int a, b = 1;\n"
16597                "int c  = 2;\n"
16598                "int dd = 3;\n",
16599                Alignment);
16600   verifyFormat("int aa       = ((1 > 2) ? 3 : 4);\n"
16601                "float b[1][] = {{3.f}};\n",
16602                Alignment);
16603   verifyFormat("for (int i = 0; i < 1; i++)\n"
16604                "  int x = 1;\n",
16605                Alignment);
16606   verifyFormat("for (i = 0; i < 1; i++)\n"
16607                "  x = 1;\n"
16608                "y = 1;\n",
16609                Alignment);
16610 
16611   EXPECT_EQ(Alignment.ReflowComments, true);
16612   Alignment.ColumnLimit = 50;
16613   EXPECT_EQ("int x   = 0;\n"
16614             "int yy  = 1; /// specificlennospace\n"
16615             "int zzz = 2;\n",
16616             format("int x   = 0;\n"
16617                    "int yy  = 1; ///specificlennospace\n"
16618                    "int zzz = 2;\n",
16619                    Alignment));
16620 
16621   verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n"
16622                "auto b                     = [] {\n"
16623                "  f();\n"
16624                "  return;\n"
16625                "};",
16626                Alignment);
16627   verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n"
16628                "auto b                     = g([] {\n"
16629                "  f();\n"
16630                "  return;\n"
16631                "});",
16632                Alignment);
16633   verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n"
16634                "auto b                     = g(param, [] {\n"
16635                "  f();\n"
16636                "  return;\n"
16637                "});",
16638                Alignment);
16639   verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n"
16640                "auto b                     = [] {\n"
16641                "  if (condition) {\n"
16642                "    return;\n"
16643                "  }\n"
16644                "};",
16645                Alignment);
16646 
16647   verifyFormat("auto b = f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
16648                "           ccc ? aaaaa : bbbbb,\n"
16649                "           dddddddddddddddddddddddddd);",
16650                Alignment);
16651   // FIXME: https://llvm.org/PR53497
16652   // verifyFormat("auto aaaaaaaaaaaa = f();\n"
16653   //              "auto b            = f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
16654   //              "    ccc ? aaaaa : bbbbb,\n"
16655   //              "    dddddddddddddddddddddddddd);",
16656   //              Alignment);
16657 }
16658 
16659 TEST_F(FormatTest, AlignConsecutiveBitFields) {
16660   FormatStyle Alignment = getLLVMStyle();
16661   Alignment.AlignConsecutiveBitFields = FormatStyle::ACS_Consecutive;
16662   verifyFormat("int const a     : 5;\n"
16663                "int oneTwoThree : 23;",
16664                Alignment);
16665 
16666   // Initializers are allowed starting with c++2a
16667   verifyFormat("int const a     : 5 = 1;\n"
16668                "int oneTwoThree : 23 = 0;",
16669                Alignment);
16670 
16671   Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
16672   verifyFormat("int const a           : 5;\n"
16673                "int       oneTwoThree : 23;",
16674                Alignment);
16675 
16676   verifyFormat("int const a           : 5;  // comment\n"
16677                "int       oneTwoThree : 23; // comment",
16678                Alignment);
16679 
16680   verifyFormat("int const a           : 5 = 1;\n"
16681                "int       oneTwoThree : 23 = 0;",
16682                Alignment);
16683 
16684   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16685   verifyFormat("int const a           : 5  = 1;\n"
16686                "int       oneTwoThree : 23 = 0;",
16687                Alignment);
16688   verifyFormat("int const a           : 5  = {1};\n"
16689                "int       oneTwoThree : 23 = 0;",
16690                Alignment);
16691 
16692   Alignment.BitFieldColonSpacing = FormatStyle::BFCS_None;
16693   verifyFormat("int const a          :5;\n"
16694                "int       oneTwoThree:23;",
16695                Alignment);
16696 
16697   Alignment.BitFieldColonSpacing = FormatStyle::BFCS_Before;
16698   verifyFormat("int const a           :5;\n"
16699                "int       oneTwoThree :23;",
16700                Alignment);
16701 
16702   Alignment.BitFieldColonSpacing = FormatStyle::BFCS_After;
16703   verifyFormat("int const a          : 5;\n"
16704                "int       oneTwoThree: 23;",
16705                Alignment);
16706 
16707   // Known limitations: ':' is only recognized as a bitfield colon when
16708   // followed by a number.
16709   /*
16710   verifyFormat("int oneTwoThree : SOME_CONSTANT;\n"
16711                "int a           : 5;",
16712                Alignment);
16713   */
16714 }
16715 
16716 TEST_F(FormatTest, AlignConsecutiveDeclarations) {
16717   FormatStyle Alignment = getLLVMStyle();
16718   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
16719   Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_None;
16720   Alignment.PointerAlignment = FormatStyle::PAS_Right;
16721   verifyFormat("float const a = 5;\n"
16722                "int oneTwoThree = 123;",
16723                Alignment);
16724   verifyFormat("int a = 5;\n"
16725                "float const oneTwoThree = 123;",
16726                Alignment);
16727 
16728   Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
16729   verifyFormat("float const a = 5;\n"
16730                "int         oneTwoThree = 123;",
16731                Alignment);
16732   verifyFormat("int         a = method();\n"
16733                "float const oneTwoThree = 133;",
16734                Alignment);
16735   verifyFormat("int i = 1, j = 10;\n"
16736                "something = 2000;",
16737                Alignment);
16738   verifyFormat("something = 2000;\n"
16739                "int i = 1, j = 10;\n",
16740                Alignment);
16741   verifyFormat("float      something = 2000;\n"
16742                "double     another = 911;\n"
16743                "int        i = 1, j = 10;\n"
16744                "const int *oneMore = 1;\n"
16745                "unsigned   i = 2;",
16746                Alignment);
16747   verifyFormat("float a = 5;\n"
16748                "int   one = 1;\n"
16749                "method();\n"
16750                "const double       oneTwoThree = 123;\n"
16751                "const unsigned int oneTwo = 12;",
16752                Alignment);
16753   verifyFormat("int      oneTwoThree{0}; // comment\n"
16754                "unsigned oneTwo;         // comment",
16755                Alignment);
16756   verifyFormat("unsigned int       *a;\n"
16757                "int                *b;\n"
16758                "unsigned int Const *c;\n"
16759                "unsigned int const *d;\n"
16760                "unsigned int Const &e;\n"
16761                "unsigned int const &f;",
16762                Alignment);
16763   verifyFormat("Const unsigned int *c;\n"
16764                "const unsigned int *d;\n"
16765                "Const unsigned int &e;\n"
16766                "const unsigned int &f;\n"
16767                "const unsigned      g;\n"
16768                "Const unsigned      h;",
16769                Alignment);
16770   EXPECT_EQ("float const a = 5;\n"
16771             "\n"
16772             "int oneTwoThree = 123;",
16773             format("float const   a = 5;\n"
16774                    "\n"
16775                    "int           oneTwoThree= 123;",
16776                    Alignment));
16777   EXPECT_EQ("float a = 5;\n"
16778             "int   one = 1;\n"
16779             "\n"
16780             "unsigned oneTwoThree = 123;",
16781             format("float    a = 5;\n"
16782                    "int      one = 1;\n"
16783                    "\n"
16784                    "unsigned oneTwoThree = 123;",
16785                    Alignment));
16786   EXPECT_EQ("float a = 5;\n"
16787             "int   one = 1;\n"
16788             "\n"
16789             "unsigned oneTwoThree = 123;\n"
16790             "int      oneTwo = 12;",
16791             format("float    a = 5;\n"
16792                    "int one = 1;\n"
16793                    "\n"
16794                    "unsigned oneTwoThree = 123;\n"
16795                    "int oneTwo = 12;",
16796                    Alignment));
16797   // Function prototype alignment
16798   verifyFormat("int    a();\n"
16799                "double b();",
16800                Alignment);
16801   verifyFormat("int    a(int x);\n"
16802                "double b();",
16803                Alignment);
16804   unsigned OldColumnLimit = Alignment.ColumnLimit;
16805   // We need to set ColumnLimit to zero, in order to stress nested alignments,
16806   // otherwise the function parameters will be re-flowed onto a single line.
16807   Alignment.ColumnLimit = 0;
16808   EXPECT_EQ("int    a(int   x,\n"
16809             "         float y);\n"
16810             "double b(int    x,\n"
16811             "         double y);",
16812             format("int a(int x,\n"
16813                    " float y);\n"
16814                    "double b(int x,\n"
16815                    " double y);",
16816                    Alignment));
16817   // This ensures that function parameters of function declarations are
16818   // correctly indented when their owning functions are indented.
16819   // The failure case here is for 'double y' to not be indented enough.
16820   EXPECT_EQ("double a(int x);\n"
16821             "int    b(int    y,\n"
16822             "         double z);",
16823             format("double a(int x);\n"
16824                    "int b(int y,\n"
16825                    " double z);",
16826                    Alignment));
16827   // Set ColumnLimit low so that we induce wrapping immediately after
16828   // the function name and opening paren.
16829   Alignment.ColumnLimit = 13;
16830   verifyFormat("int function(\n"
16831                "    int  x,\n"
16832                "    bool y);",
16833                Alignment);
16834   Alignment.ColumnLimit = OldColumnLimit;
16835   // Ensure function pointers don't screw up recursive alignment
16836   verifyFormat("int    a(int x, void (*fp)(int y));\n"
16837                "double b();",
16838                Alignment);
16839   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16840   // Ensure recursive alignment is broken by function braces, so that the
16841   // "a = 1" does not align with subsequent assignments inside the function
16842   // body.
16843   verifyFormat("int func(int a = 1) {\n"
16844                "  int b  = 2;\n"
16845                "  int cc = 3;\n"
16846                "}",
16847                Alignment);
16848   verifyFormat("float      something = 2000;\n"
16849                "double     another   = 911;\n"
16850                "int        i = 1, j = 10;\n"
16851                "const int *oneMore = 1;\n"
16852                "unsigned   i       = 2;",
16853                Alignment);
16854   verifyFormat("int      oneTwoThree = {0}; // comment\n"
16855                "unsigned oneTwo      = 0;   // comment",
16856                Alignment);
16857   // Make sure that scope is correctly tracked, in the absence of braces
16858   verifyFormat("for (int i = 0; i < n; i++)\n"
16859                "  j = i;\n"
16860                "double x = 1;\n",
16861                Alignment);
16862   verifyFormat("if (int i = 0)\n"
16863                "  j = i;\n"
16864                "double x = 1;\n",
16865                Alignment);
16866   // Ensure operator[] and operator() are comprehended
16867   verifyFormat("struct test {\n"
16868                "  long long int foo();\n"
16869                "  int           operator[](int a);\n"
16870                "  double        bar();\n"
16871                "};\n",
16872                Alignment);
16873   verifyFormat("struct test {\n"
16874                "  long long int foo();\n"
16875                "  int           operator()(int a);\n"
16876                "  double        bar();\n"
16877                "};\n",
16878                Alignment);
16879   // http://llvm.org/PR52914
16880   verifyFormat("char *a[]     = {\"a\", // comment\n"
16881                "                 \"bb\"};\n"
16882                "int   bbbbbbb = 0;",
16883                Alignment);
16884 
16885   // PAS_Right
16886   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
16887             "  int const i   = 1;\n"
16888             "  int      *j   = 2;\n"
16889             "  int       big = 10000;\n"
16890             "\n"
16891             "  unsigned oneTwoThree = 123;\n"
16892             "  int      oneTwo      = 12;\n"
16893             "  method();\n"
16894             "  float k  = 2;\n"
16895             "  int   ll = 10000;\n"
16896             "}",
16897             format("void SomeFunction(int parameter= 0) {\n"
16898                    " int const  i= 1;\n"
16899                    "  int *j=2;\n"
16900                    " int big  =  10000;\n"
16901                    "\n"
16902                    "unsigned oneTwoThree  =123;\n"
16903                    "int oneTwo = 12;\n"
16904                    "  method();\n"
16905                    "float k= 2;\n"
16906                    "int ll=10000;\n"
16907                    "}",
16908                    Alignment));
16909   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
16910             "  int const i   = 1;\n"
16911             "  int     **j   = 2, ***k;\n"
16912             "  int      &k   = i;\n"
16913             "  int     &&l   = i + j;\n"
16914             "  int       big = 10000;\n"
16915             "\n"
16916             "  unsigned oneTwoThree = 123;\n"
16917             "  int      oneTwo      = 12;\n"
16918             "  method();\n"
16919             "  float k  = 2;\n"
16920             "  int   ll = 10000;\n"
16921             "}",
16922             format("void SomeFunction(int parameter= 0) {\n"
16923                    " int const  i= 1;\n"
16924                    "  int **j=2,***k;\n"
16925                    "int &k=i;\n"
16926                    "int &&l=i+j;\n"
16927                    " int big  =  10000;\n"
16928                    "\n"
16929                    "unsigned oneTwoThree  =123;\n"
16930                    "int oneTwo = 12;\n"
16931                    "  method();\n"
16932                    "float k= 2;\n"
16933                    "int ll=10000;\n"
16934                    "}",
16935                    Alignment));
16936   // variables are aligned at their name, pointers are at the right most
16937   // position
16938   verifyFormat("int   *a;\n"
16939                "int  **b;\n"
16940                "int ***c;\n"
16941                "int    foobar;\n",
16942                Alignment);
16943 
16944   // PAS_Left
16945   FormatStyle AlignmentLeft = Alignment;
16946   AlignmentLeft.PointerAlignment = FormatStyle::PAS_Left;
16947   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
16948             "  int const i   = 1;\n"
16949             "  int*      j   = 2;\n"
16950             "  int       big = 10000;\n"
16951             "\n"
16952             "  unsigned oneTwoThree = 123;\n"
16953             "  int      oneTwo      = 12;\n"
16954             "  method();\n"
16955             "  float k  = 2;\n"
16956             "  int   ll = 10000;\n"
16957             "}",
16958             format("void SomeFunction(int parameter= 0) {\n"
16959                    " int const  i= 1;\n"
16960                    "  int *j=2;\n"
16961                    " int big  =  10000;\n"
16962                    "\n"
16963                    "unsigned oneTwoThree  =123;\n"
16964                    "int oneTwo = 12;\n"
16965                    "  method();\n"
16966                    "float k= 2;\n"
16967                    "int ll=10000;\n"
16968                    "}",
16969                    AlignmentLeft));
16970   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
16971             "  int const i   = 1;\n"
16972             "  int**     j   = 2;\n"
16973             "  int&      k   = i;\n"
16974             "  int&&     l   = i + j;\n"
16975             "  int       big = 10000;\n"
16976             "\n"
16977             "  unsigned oneTwoThree = 123;\n"
16978             "  int      oneTwo      = 12;\n"
16979             "  method();\n"
16980             "  float k  = 2;\n"
16981             "  int   ll = 10000;\n"
16982             "}",
16983             format("void SomeFunction(int parameter= 0) {\n"
16984                    " int const  i= 1;\n"
16985                    "  int **j=2;\n"
16986                    "int &k=i;\n"
16987                    "int &&l=i+j;\n"
16988                    " int big  =  10000;\n"
16989                    "\n"
16990                    "unsigned oneTwoThree  =123;\n"
16991                    "int oneTwo = 12;\n"
16992                    "  method();\n"
16993                    "float k= 2;\n"
16994                    "int ll=10000;\n"
16995                    "}",
16996                    AlignmentLeft));
16997   // variables are aligned at their name, pointers are at the left most position
16998   verifyFormat("int*   a;\n"
16999                "int**  b;\n"
17000                "int*** c;\n"
17001                "int    foobar;\n",
17002                AlignmentLeft);
17003 
17004   // PAS_Middle
17005   FormatStyle AlignmentMiddle = Alignment;
17006   AlignmentMiddle.PointerAlignment = FormatStyle::PAS_Middle;
17007   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
17008             "  int const i   = 1;\n"
17009             "  int *     j   = 2;\n"
17010             "  int       big = 10000;\n"
17011             "\n"
17012             "  unsigned oneTwoThree = 123;\n"
17013             "  int      oneTwo      = 12;\n"
17014             "  method();\n"
17015             "  float k  = 2;\n"
17016             "  int   ll = 10000;\n"
17017             "}",
17018             format("void SomeFunction(int parameter= 0) {\n"
17019                    " int const  i= 1;\n"
17020                    "  int *j=2;\n"
17021                    " int big  =  10000;\n"
17022                    "\n"
17023                    "unsigned oneTwoThree  =123;\n"
17024                    "int oneTwo = 12;\n"
17025                    "  method();\n"
17026                    "float k= 2;\n"
17027                    "int ll=10000;\n"
17028                    "}",
17029                    AlignmentMiddle));
17030   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
17031             "  int const i   = 1;\n"
17032             "  int **    j   = 2, ***k;\n"
17033             "  int &     k   = i;\n"
17034             "  int &&    l   = i + j;\n"
17035             "  int       big = 10000;\n"
17036             "\n"
17037             "  unsigned oneTwoThree = 123;\n"
17038             "  int      oneTwo      = 12;\n"
17039             "  method();\n"
17040             "  float k  = 2;\n"
17041             "  int   ll = 10000;\n"
17042             "}",
17043             format("void SomeFunction(int parameter= 0) {\n"
17044                    " int const  i= 1;\n"
17045                    "  int **j=2,***k;\n"
17046                    "int &k=i;\n"
17047                    "int &&l=i+j;\n"
17048                    " int big  =  10000;\n"
17049                    "\n"
17050                    "unsigned oneTwoThree  =123;\n"
17051                    "int oneTwo = 12;\n"
17052                    "  method();\n"
17053                    "float k= 2;\n"
17054                    "int ll=10000;\n"
17055                    "}",
17056                    AlignmentMiddle));
17057   // variables are aligned at their name, pointers are in the middle
17058   verifyFormat("int *   a;\n"
17059                "int *   b;\n"
17060                "int *** c;\n"
17061                "int     foobar;\n",
17062                AlignmentMiddle);
17063 
17064   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
17065   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
17066   verifyFormat("#define A \\\n"
17067                "  int       aaaa = 12; \\\n"
17068                "  float     b = 23; \\\n"
17069                "  const int ccc = 234; \\\n"
17070                "  unsigned  dddddddddd = 2345;",
17071                Alignment);
17072   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
17073   verifyFormat("#define A              \\\n"
17074                "  int       aaaa = 12; \\\n"
17075                "  float     b = 23;    \\\n"
17076                "  const int ccc = 234; \\\n"
17077                "  unsigned  dddddddddd = 2345;",
17078                Alignment);
17079   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
17080   Alignment.ColumnLimit = 30;
17081   verifyFormat("#define A                    \\\n"
17082                "  int       aaaa = 12;       \\\n"
17083                "  float     b = 23;          \\\n"
17084                "  const int ccc = 234;       \\\n"
17085                "  int       dddddddddd = 2345;",
17086                Alignment);
17087   Alignment.ColumnLimit = 80;
17088   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
17089                "k = 4, int l = 5,\n"
17090                "                  int m = 6) {\n"
17091                "  const int j = 10;\n"
17092                "  otherThing = 1;\n"
17093                "}",
17094                Alignment);
17095   verifyFormat("void SomeFunction(int parameter = 0) {\n"
17096                "  int const i = 1;\n"
17097                "  int      *j = 2;\n"
17098                "  int       big = 10000;\n"
17099                "}",
17100                Alignment);
17101   verifyFormat("class C {\n"
17102                "public:\n"
17103                "  int          i = 1;\n"
17104                "  virtual void f() = 0;\n"
17105                "};",
17106                Alignment);
17107   verifyFormat("float i = 1;\n"
17108                "if (SomeType t = getSomething()) {\n"
17109                "}\n"
17110                "const unsigned j = 2;\n"
17111                "int            big = 10000;",
17112                Alignment);
17113   verifyFormat("float j = 7;\n"
17114                "for (int k = 0; k < N; ++k) {\n"
17115                "}\n"
17116                "unsigned j = 2;\n"
17117                "int      big = 10000;\n"
17118                "}",
17119                Alignment);
17120   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
17121   verifyFormat("float              i = 1;\n"
17122                "LooooooooooongType loooooooooooooooooooooongVariable\n"
17123                "    = someLooooooooooooooooongFunction();\n"
17124                "int j = 2;",
17125                Alignment);
17126   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
17127   verifyFormat("int                i = 1;\n"
17128                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
17129                "    someLooooooooooooooooongFunction();\n"
17130                "int j = 2;",
17131                Alignment);
17132 
17133   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
17134   verifyFormat("auto lambda = []() {\n"
17135                "  auto  ii = 0;\n"
17136                "  float j  = 0;\n"
17137                "  return 0;\n"
17138                "};\n"
17139                "int   i  = 0;\n"
17140                "float i2 = 0;\n"
17141                "auto  v  = type{\n"
17142                "    i = 1,   //\n"
17143                "    (i = 2), //\n"
17144                "    i = 3    //\n"
17145                "};",
17146                Alignment);
17147   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
17148 
17149   verifyFormat(
17150       "int      i = 1;\n"
17151       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
17152       "                          loooooooooooooooooooooongParameterB);\n"
17153       "int      j = 2;",
17154       Alignment);
17155 
17156   // Test interactions with ColumnLimit and AlignConsecutiveAssignments:
17157   // We expect declarations and assignments to align, as long as it doesn't
17158   // exceed the column limit, starting a new alignment sequence whenever it
17159   // happens.
17160   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
17161   Alignment.ColumnLimit = 30;
17162   verifyFormat("float    ii              = 1;\n"
17163                "unsigned j               = 2;\n"
17164                "int someVerylongVariable = 1;\n"
17165                "AnotherLongType  ll = 123456;\n"
17166                "VeryVeryLongType k  = 2;\n"
17167                "int              myvar = 1;",
17168                Alignment);
17169   Alignment.ColumnLimit = 80;
17170   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
17171 
17172   verifyFormat(
17173       "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n"
17174       "          typename LongType, typename B>\n"
17175       "auto foo() {}\n",
17176       Alignment);
17177   verifyFormat("float a, b = 1;\n"
17178                "int   c = 2;\n"
17179                "int   dd = 3;\n",
17180                Alignment);
17181   verifyFormat("int   aa = ((1 > 2) ? 3 : 4);\n"
17182                "float b[1][] = {{3.f}};\n",
17183                Alignment);
17184   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
17185   verifyFormat("float a, b = 1;\n"
17186                "int   c  = 2;\n"
17187                "int   dd = 3;\n",
17188                Alignment);
17189   verifyFormat("int   aa     = ((1 > 2) ? 3 : 4);\n"
17190                "float b[1][] = {{3.f}};\n",
17191                Alignment);
17192   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
17193 
17194   Alignment.ColumnLimit = 30;
17195   Alignment.BinPackParameters = false;
17196   verifyFormat("void foo(float     a,\n"
17197                "         float     b,\n"
17198                "         int       c,\n"
17199                "         uint32_t *d) {\n"
17200                "  int   *e = 0;\n"
17201                "  float  f = 0;\n"
17202                "  double g = 0;\n"
17203                "}\n"
17204                "void bar(ino_t     a,\n"
17205                "         int       b,\n"
17206                "         uint32_t *c,\n"
17207                "         bool      d) {}\n",
17208                Alignment);
17209   Alignment.BinPackParameters = true;
17210   Alignment.ColumnLimit = 80;
17211 
17212   // Bug 33507
17213   Alignment.PointerAlignment = FormatStyle::PAS_Middle;
17214   verifyFormat(
17215       "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n"
17216       "  static const Version verVs2017;\n"
17217       "  return true;\n"
17218       "});\n",
17219       Alignment);
17220   Alignment.PointerAlignment = FormatStyle::PAS_Right;
17221 
17222   // See llvm.org/PR35641
17223   Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
17224   verifyFormat("int func() { //\n"
17225                "  int      b;\n"
17226                "  unsigned c;\n"
17227                "}",
17228                Alignment);
17229 
17230   // See PR37175
17231   FormatStyle Style = getMozillaStyle();
17232   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
17233   EXPECT_EQ("DECOR1 /**/ int8_t /**/ DECOR2 /**/\n"
17234             "foo(int a);",
17235             format("DECOR1 /**/ int8_t /**/ DECOR2 /**/ foo (int a);", Style));
17236 
17237   Alignment.PointerAlignment = FormatStyle::PAS_Left;
17238   verifyFormat("unsigned int*       a;\n"
17239                "int*                b;\n"
17240                "unsigned int Const* c;\n"
17241                "unsigned int const* d;\n"
17242                "unsigned int Const& e;\n"
17243                "unsigned int const& f;",
17244                Alignment);
17245   verifyFormat("Const unsigned int* c;\n"
17246                "const unsigned int* d;\n"
17247                "Const unsigned int& e;\n"
17248                "const unsigned int& f;\n"
17249                "const unsigned      g;\n"
17250                "Const unsigned      h;",
17251                Alignment);
17252 
17253   Alignment.PointerAlignment = FormatStyle::PAS_Middle;
17254   verifyFormat("unsigned int *       a;\n"
17255                "int *                b;\n"
17256                "unsigned int Const * c;\n"
17257                "unsigned int const * d;\n"
17258                "unsigned int Const & e;\n"
17259                "unsigned int const & f;",
17260                Alignment);
17261   verifyFormat("Const unsigned int * c;\n"
17262                "const unsigned int * d;\n"
17263                "Const unsigned int & e;\n"
17264                "const unsigned int & f;\n"
17265                "const unsigned       g;\n"
17266                "Const unsigned       h;",
17267                Alignment);
17268 }
17269 
17270 TEST_F(FormatTest, AlignWithLineBreaks) {
17271   auto Style = getLLVMStyleWithColumns(120);
17272 
17273   EXPECT_EQ(Style.AlignConsecutiveAssignments, FormatStyle::ACS_None);
17274   EXPECT_EQ(Style.AlignConsecutiveDeclarations, FormatStyle::ACS_None);
17275   verifyFormat("void foo() {\n"
17276                "  int myVar = 5;\n"
17277                "  double x = 3.14;\n"
17278                "  auto str = \"Hello \"\n"
17279                "             \"World\";\n"
17280                "  auto s = \"Hello \"\n"
17281                "           \"Again\";\n"
17282                "}",
17283                Style);
17284 
17285   // clang-format off
17286   verifyFormat("void foo() {\n"
17287                "  const int capacityBefore = Entries.capacity();\n"
17288                "  const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
17289                "                                            std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
17290                "  const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
17291                "                                          std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
17292                "}",
17293                Style);
17294   // clang-format on
17295 
17296   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
17297   verifyFormat("void foo() {\n"
17298                "  int myVar = 5;\n"
17299                "  double x  = 3.14;\n"
17300                "  auto str  = \"Hello \"\n"
17301                "              \"World\";\n"
17302                "  auto s    = \"Hello \"\n"
17303                "              \"Again\";\n"
17304                "}",
17305                Style);
17306 
17307   // clang-format off
17308   verifyFormat("void foo() {\n"
17309                "  const int capacityBefore = Entries.capacity();\n"
17310                "  const auto newEntry      = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
17311                "                                                 std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
17312                "  const X newEntry2        = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
17313                "                                                 std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
17314                "}",
17315                Style);
17316   // clang-format on
17317 
17318   Style.AlignConsecutiveAssignments = FormatStyle::ACS_None;
17319   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
17320   verifyFormat("void foo() {\n"
17321                "  int    myVar = 5;\n"
17322                "  double x = 3.14;\n"
17323                "  auto   str = \"Hello \"\n"
17324                "               \"World\";\n"
17325                "  auto   s = \"Hello \"\n"
17326                "             \"Again\";\n"
17327                "}",
17328                Style);
17329 
17330   // clang-format off
17331   verifyFormat("void foo() {\n"
17332                "  const int  capacityBefore = Entries.capacity();\n"
17333                "  const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
17334                "                                            std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
17335                "  const X    newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
17336                "                                             std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
17337                "}",
17338                Style);
17339   // clang-format on
17340 
17341   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
17342   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
17343 
17344   verifyFormat("void foo() {\n"
17345                "  int    myVar = 5;\n"
17346                "  double x     = 3.14;\n"
17347                "  auto   str   = \"Hello \"\n"
17348                "                 \"World\";\n"
17349                "  auto   s     = \"Hello \"\n"
17350                "                 \"Again\";\n"
17351                "}",
17352                Style);
17353 
17354   // clang-format off
17355   verifyFormat("void foo() {\n"
17356                "  const int  capacityBefore = Entries.capacity();\n"
17357                "  const auto newEntry       = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
17358                "                                                  std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
17359                "  const X    newEntry2      = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
17360                "                                                  std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
17361                "}",
17362                Style);
17363   // clang-format on
17364 
17365   Style = getLLVMStyleWithColumns(120);
17366   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
17367   Style.ContinuationIndentWidth = 4;
17368   Style.IndentWidth = 4;
17369 
17370   // clang-format off
17371   verifyFormat("void SomeFunc() {\n"
17372                "    newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n"
17373                "                                                        seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
17374                "    newWatcher.maxAge     = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n"
17375                "                                                        seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
17376                "    newWatcher.max        = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n"
17377                "                                                        seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
17378                "}",
17379                Style);
17380   // clang-format on
17381 
17382   Style.BinPackArguments = false;
17383 
17384   // clang-format off
17385   verifyFormat("void SomeFunc() {\n"
17386                "    newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(\n"
17387                "        FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
17388                "    newWatcher.maxAge     = ToLegacyTimestamp(GetMaxAge(\n"
17389                "        FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
17390                "    newWatcher.max        = ToLegacyTimestamp(GetMaxAge(\n"
17391                "        FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
17392                "}",
17393                Style);
17394   // clang-format on
17395 }
17396 
17397 TEST_F(FormatTest, AlignWithInitializerPeriods) {
17398   auto Style = getLLVMStyleWithColumns(60);
17399 
17400   verifyFormat("void foo1(void) {\n"
17401                "  BYTE p[1] = 1;\n"
17402                "  A B = {.one_foooooooooooooooo = 2,\n"
17403                "         .two_fooooooooooooo = 3,\n"
17404                "         .three_fooooooooooooo = 4};\n"
17405                "  BYTE payload = 2;\n"
17406                "}",
17407                Style);
17408 
17409   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
17410   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_None;
17411   verifyFormat("void foo2(void) {\n"
17412                "  BYTE p[1]    = 1;\n"
17413                "  A B          = {.one_foooooooooooooooo = 2,\n"
17414                "                  .two_fooooooooooooo    = 3,\n"
17415                "                  .three_fooooooooooooo  = 4};\n"
17416                "  BYTE payload = 2;\n"
17417                "}",
17418                Style);
17419 
17420   Style.AlignConsecutiveAssignments = FormatStyle::ACS_None;
17421   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
17422   verifyFormat("void foo3(void) {\n"
17423                "  BYTE p[1] = 1;\n"
17424                "  A    B = {.one_foooooooooooooooo = 2,\n"
17425                "            .two_fooooooooooooo = 3,\n"
17426                "            .three_fooooooooooooo = 4};\n"
17427                "  BYTE payload = 2;\n"
17428                "}",
17429                Style);
17430 
17431   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
17432   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
17433   verifyFormat("void foo4(void) {\n"
17434                "  BYTE p[1]    = 1;\n"
17435                "  A    B       = {.one_foooooooooooooooo = 2,\n"
17436                "                  .two_fooooooooooooo    = 3,\n"
17437                "                  .three_fooooooooooooo  = 4};\n"
17438                "  BYTE payload = 2;\n"
17439                "}",
17440                Style);
17441 }
17442 
17443 TEST_F(FormatTest, LinuxBraceBreaking) {
17444   FormatStyle LinuxBraceStyle = getLLVMStyle();
17445   LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
17446   verifyFormat("namespace a\n"
17447                "{\n"
17448                "class A\n"
17449                "{\n"
17450                "  void f()\n"
17451                "  {\n"
17452                "    if (true) {\n"
17453                "      a();\n"
17454                "      b();\n"
17455                "    } else {\n"
17456                "      a();\n"
17457                "    }\n"
17458                "  }\n"
17459                "  void g() { return; }\n"
17460                "};\n"
17461                "struct B {\n"
17462                "  int x;\n"
17463                "};\n"
17464                "} // namespace a\n",
17465                LinuxBraceStyle);
17466   verifyFormat("enum X {\n"
17467                "  Y = 0,\n"
17468                "}\n",
17469                LinuxBraceStyle);
17470   verifyFormat("struct S {\n"
17471                "  int Type;\n"
17472                "  union {\n"
17473                "    int x;\n"
17474                "    double y;\n"
17475                "  } Value;\n"
17476                "  class C\n"
17477                "  {\n"
17478                "    MyFavoriteType Value;\n"
17479                "  } Class;\n"
17480                "}\n",
17481                LinuxBraceStyle);
17482 }
17483 
17484 TEST_F(FormatTest, MozillaBraceBreaking) {
17485   FormatStyle MozillaBraceStyle = getLLVMStyle();
17486   MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
17487   MozillaBraceStyle.FixNamespaceComments = false;
17488   verifyFormat("namespace a {\n"
17489                "class A\n"
17490                "{\n"
17491                "  void f()\n"
17492                "  {\n"
17493                "    if (true) {\n"
17494                "      a();\n"
17495                "      b();\n"
17496                "    }\n"
17497                "  }\n"
17498                "  void g() { return; }\n"
17499                "};\n"
17500                "enum E\n"
17501                "{\n"
17502                "  A,\n"
17503                "  // foo\n"
17504                "  B,\n"
17505                "  C\n"
17506                "};\n"
17507                "struct B\n"
17508                "{\n"
17509                "  int x;\n"
17510                "};\n"
17511                "}\n",
17512                MozillaBraceStyle);
17513   verifyFormat("struct S\n"
17514                "{\n"
17515                "  int Type;\n"
17516                "  union\n"
17517                "  {\n"
17518                "    int x;\n"
17519                "    double y;\n"
17520                "  } Value;\n"
17521                "  class C\n"
17522                "  {\n"
17523                "    MyFavoriteType Value;\n"
17524                "  } Class;\n"
17525                "}\n",
17526                MozillaBraceStyle);
17527 }
17528 
17529 TEST_F(FormatTest, StroustrupBraceBreaking) {
17530   FormatStyle StroustrupBraceStyle = getLLVMStyle();
17531   StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
17532   verifyFormat("namespace a {\n"
17533                "class A {\n"
17534                "  void f()\n"
17535                "  {\n"
17536                "    if (true) {\n"
17537                "      a();\n"
17538                "      b();\n"
17539                "    }\n"
17540                "  }\n"
17541                "  void g() { return; }\n"
17542                "};\n"
17543                "struct B {\n"
17544                "  int x;\n"
17545                "};\n"
17546                "} // namespace a\n",
17547                StroustrupBraceStyle);
17548 
17549   verifyFormat("void foo()\n"
17550                "{\n"
17551                "  if (a) {\n"
17552                "    a();\n"
17553                "  }\n"
17554                "  else {\n"
17555                "    b();\n"
17556                "  }\n"
17557                "}\n",
17558                StroustrupBraceStyle);
17559 
17560   verifyFormat("#ifdef _DEBUG\n"
17561                "int foo(int i = 0)\n"
17562                "#else\n"
17563                "int foo(int i = 5)\n"
17564                "#endif\n"
17565                "{\n"
17566                "  return i;\n"
17567                "}",
17568                StroustrupBraceStyle);
17569 
17570   verifyFormat("void foo() {}\n"
17571                "void bar()\n"
17572                "#ifdef _DEBUG\n"
17573                "{\n"
17574                "  foo();\n"
17575                "}\n"
17576                "#else\n"
17577                "{\n"
17578                "}\n"
17579                "#endif",
17580                StroustrupBraceStyle);
17581 
17582   verifyFormat("void foobar() { int i = 5; }\n"
17583                "#ifdef _DEBUG\n"
17584                "void bar() {}\n"
17585                "#else\n"
17586                "void bar() { foobar(); }\n"
17587                "#endif",
17588                StroustrupBraceStyle);
17589 }
17590 
17591 TEST_F(FormatTest, AllmanBraceBreaking) {
17592   FormatStyle AllmanBraceStyle = getLLVMStyle();
17593   AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
17594 
17595   EXPECT_EQ("namespace a\n"
17596             "{\n"
17597             "void f();\n"
17598             "void g();\n"
17599             "} // namespace a\n",
17600             format("namespace a\n"
17601                    "{\n"
17602                    "void f();\n"
17603                    "void g();\n"
17604                    "}\n",
17605                    AllmanBraceStyle));
17606 
17607   verifyFormat("namespace a\n"
17608                "{\n"
17609                "class A\n"
17610                "{\n"
17611                "  void f()\n"
17612                "  {\n"
17613                "    if (true)\n"
17614                "    {\n"
17615                "      a();\n"
17616                "      b();\n"
17617                "    }\n"
17618                "  }\n"
17619                "  void g() { return; }\n"
17620                "};\n"
17621                "struct B\n"
17622                "{\n"
17623                "  int x;\n"
17624                "};\n"
17625                "union C\n"
17626                "{\n"
17627                "};\n"
17628                "} // namespace a",
17629                AllmanBraceStyle);
17630 
17631   verifyFormat("void f()\n"
17632                "{\n"
17633                "  if (true)\n"
17634                "  {\n"
17635                "    a();\n"
17636                "  }\n"
17637                "  else if (false)\n"
17638                "  {\n"
17639                "    b();\n"
17640                "  }\n"
17641                "  else\n"
17642                "  {\n"
17643                "    c();\n"
17644                "  }\n"
17645                "}\n",
17646                AllmanBraceStyle);
17647 
17648   verifyFormat("void f()\n"
17649                "{\n"
17650                "  for (int i = 0; i < 10; ++i)\n"
17651                "  {\n"
17652                "    a();\n"
17653                "  }\n"
17654                "  while (false)\n"
17655                "  {\n"
17656                "    b();\n"
17657                "  }\n"
17658                "  do\n"
17659                "  {\n"
17660                "    c();\n"
17661                "  } while (false)\n"
17662                "}\n",
17663                AllmanBraceStyle);
17664 
17665   verifyFormat("void f(int a)\n"
17666                "{\n"
17667                "  switch (a)\n"
17668                "  {\n"
17669                "  case 0:\n"
17670                "    break;\n"
17671                "  case 1:\n"
17672                "  {\n"
17673                "    break;\n"
17674                "  }\n"
17675                "  case 2:\n"
17676                "  {\n"
17677                "  }\n"
17678                "  break;\n"
17679                "  default:\n"
17680                "    break;\n"
17681                "  }\n"
17682                "}\n",
17683                AllmanBraceStyle);
17684 
17685   verifyFormat("enum X\n"
17686                "{\n"
17687                "  Y = 0,\n"
17688                "}\n",
17689                AllmanBraceStyle);
17690   verifyFormat("enum X\n"
17691                "{\n"
17692                "  Y = 0\n"
17693                "}\n",
17694                AllmanBraceStyle);
17695 
17696   verifyFormat("@interface BSApplicationController ()\n"
17697                "{\n"
17698                "@private\n"
17699                "  id _extraIvar;\n"
17700                "}\n"
17701                "@end\n",
17702                AllmanBraceStyle);
17703 
17704   verifyFormat("#ifdef _DEBUG\n"
17705                "int foo(int i = 0)\n"
17706                "#else\n"
17707                "int foo(int i = 5)\n"
17708                "#endif\n"
17709                "{\n"
17710                "  return i;\n"
17711                "}",
17712                AllmanBraceStyle);
17713 
17714   verifyFormat("void foo() {}\n"
17715                "void bar()\n"
17716                "#ifdef _DEBUG\n"
17717                "{\n"
17718                "  foo();\n"
17719                "}\n"
17720                "#else\n"
17721                "{\n"
17722                "}\n"
17723                "#endif",
17724                AllmanBraceStyle);
17725 
17726   verifyFormat("void foobar() { int i = 5; }\n"
17727                "#ifdef _DEBUG\n"
17728                "void bar() {}\n"
17729                "#else\n"
17730                "void bar() { foobar(); }\n"
17731                "#endif",
17732                AllmanBraceStyle);
17733 
17734   EXPECT_EQ(AllmanBraceStyle.AllowShortLambdasOnASingleLine,
17735             FormatStyle::SLS_All);
17736 
17737   verifyFormat("[](int i) { return i + 2; };\n"
17738                "[](int i, int j)\n"
17739                "{\n"
17740                "  auto x = i + j;\n"
17741                "  auto y = i * j;\n"
17742                "  return x ^ y;\n"
17743                "};\n"
17744                "void foo()\n"
17745                "{\n"
17746                "  auto shortLambda = [](int i) { return i + 2; };\n"
17747                "  auto longLambda = [](int i, int j)\n"
17748                "  {\n"
17749                "    auto x = i + j;\n"
17750                "    auto y = i * j;\n"
17751                "    return x ^ y;\n"
17752                "  };\n"
17753                "}",
17754                AllmanBraceStyle);
17755 
17756   AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
17757 
17758   verifyFormat("[](int i)\n"
17759                "{\n"
17760                "  return i + 2;\n"
17761                "};\n"
17762                "[](int i, int j)\n"
17763                "{\n"
17764                "  auto x = i + j;\n"
17765                "  auto y = i * j;\n"
17766                "  return x ^ y;\n"
17767                "};\n"
17768                "void foo()\n"
17769                "{\n"
17770                "  auto shortLambda = [](int i)\n"
17771                "  {\n"
17772                "    return i + 2;\n"
17773                "  };\n"
17774                "  auto longLambda = [](int i, int j)\n"
17775                "  {\n"
17776                "    auto x = i + j;\n"
17777                "    auto y = i * j;\n"
17778                "    return x ^ y;\n"
17779                "  };\n"
17780                "}",
17781                AllmanBraceStyle);
17782 
17783   // Reset
17784   AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_All;
17785 
17786   // This shouldn't affect ObjC blocks..
17787   verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
17788                "  // ...\n"
17789                "  int i;\n"
17790                "}];",
17791                AllmanBraceStyle);
17792   verifyFormat("void (^block)(void) = ^{\n"
17793                "  // ...\n"
17794                "  int i;\n"
17795                "};",
17796                AllmanBraceStyle);
17797   // .. or dict literals.
17798   verifyFormat("void f()\n"
17799                "{\n"
17800                "  // ...\n"
17801                "  [object someMethod:@{@\"a\" : @\"b\"}];\n"
17802                "}",
17803                AllmanBraceStyle);
17804   verifyFormat("void f()\n"
17805                "{\n"
17806                "  // ...\n"
17807                "  [object someMethod:@{a : @\"b\"}];\n"
17808                "}",
17809                AllmanBraceStyle);
17810   verifyFormat("int f()\n"
17811                "{ // comment\n"
17812                "  return 42;\n"
17813                "}",
17814                AllmanBraceStyle);
17815 
17816   AllmanBraceStyle.ColumnLimit = 19;
17817   verifyFormat("void f() { int i; }", AllmanBraceStyle);
17818   AllmanBraceStyle.ColumnLimit = 18;
17819   verifyFormat("void f()\n"
17820                "{\n"
17821                "  int i;\n"
17822                "}",
17823                AllmanBraceStyle);
17824   AllmanBraceStyle.ColumnLimit = 80;
17825 
17826   FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle;
17827   BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine =
17828       FormatStyle::SIS_WithoutElse;
17829   BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
17830   verifyFormat("void f(bool b)\n"
17831                "{\n"
17832                "  if (b)\n"
17833                "  {\n"
17834                "    return;\n"
17835                "  }\n"
17836                "}\n",
17837                BreakBeforeBraceShortIfs);
17838   verifyFormat("void f(bool b)\n"
17839                "{\n"
17840                "  if constexpr (b)\n"
17841                "  {\n"
17842                "    return;\n"
17843                "  }\n"
17844                "}\n",
17845                BreakBeforeBraceShortIfs);
17846   verifyFormat("void f(bool b)\n"
17847                "{\n"
17848                "  if CONSTEXPR (b)\n"
17849                "  {\n"
17850                "    return;\n"
17851                "  }\n"
17852                "}\n",
17853                BreakBeforeBraceShortIfs);
17854   verifyFormat("void f(bool b)\n"
17855                "{\n"
17856                "  if (b) return;\n"
17857                "}\n",
17858                BreakBeforeBraceShortIfs);
17859   verifyFormat("void f(bool b)\n"
17860                "{\n"
17861                "  if constexpr (b) return;\n"
17862                "}\n",
17863                BreakBeforeBraceShortIfs);
17864   verifyFormat("void f(bool b)\n"
17865                "{\n"
17866                "  if CONSTEXPR (b) return;\n"
17867                "}\n",
17868                BreakBeforeBraceShortIfs);
17869   verifyFormat("void f(bool b)\n"
17870                "{\n"
17871                "  while (b)\n"
17872                "  {\n"
17873                "    return;\n"
17874                "  }\n"
17875                "}\n",
17876                BreakBeforeBraceShortIfs);
17877 }
17878 
17879 TEST_F(FormatTest, WhitesmithsBraceBreaking) {
17880   FormatStyle WhitesmithsBraceStyle = getLLVMStyleWithColumns(0);
17881   WhitesmithsBraceStyle.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
17882 
17883   // Make a few changes to the style for testing purposes
17884   WhitesmithsBraceStyle.AllowShortFunctionsOnASingleLine =
17885       FormatStyle::SFS_Empty;
17886   WhitesmithsBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
17887 
17888   // FIXME: this test case can't decide whether there should be a blank line
17889   // after the ~D() line or not. It adds one if one doesn't exist in the test
17890   // and it removes the line if one exists.
17891   /*
17892   verifyFormat("class A;\n"
17893                "namespace B\n"
17894                "  {\n"
17895                "class C;\n"
17896                "// Comment\n"
17897                "class D\n"
17898                "  {\n"
17899                "public:\n"
17900                "  D();\n"
17901                "  ~D() {}\n"
17902                "private:\n"
17903                "  enum E\n"
17904                "    {\n"
17905                "    F\n"
17906                "    }\n"
17907                "  };\n"
17908                "  } // namespace B\n",
17909                WhitesmithsBraceStyle);
17910   */
17911 
17912   WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_None;
17913   verifyFormat("namespace a\n"
17914                "  {\n"
17915                "class A\n"
17916                "  {\n"
17917                "  void f()\n"
17918                "    {\n"
17919                "    if (true)\n"
17920                "      {\n"
17921                "      a();\n"
17922                "      b();\n"
17923                "      }\n"
17924                "    }\n"
17925                "  void g()\n"
17926                "    {\n"
17927                "    return;\n"
17928                "    }\n"
17929                "  };\n"
17930                "struct B\n"
17931                "  {\n"
17932                "  int x;\n"
17933                "  };\n"
17934                "  } // namespace a",
17935                WhitesmithsBraceStyle);
17936 
17937   verifyFormat("namespace a\n"
17938                "  {\n"
17939                "namespace b\n"
17940                "  {\n"
17941                "class A\n"
17942                "  {\n"
17943                "  void f()\n"
17944                "    {\n"
17945                "    if (true)\n"
17946                "      {\n"
17947                "      a();\n"
17948                "      b();\n"
17949                "      }\n"
17950                "    }\n"
17951                "  void g()\n"
17952                "    {\n"
17953                "    return;\n"
17954                "    }\n"
17955                "  };\n"
17956                "struct B\n"
17957                "  {\n"
17958                "  int x;\n"
17959                "  };\n"
17960                "  } // namespace b\n"
17961                "  } // namespace a",
17962                WhitesmithsBraceStyle);
17963 
17964   WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_Inner;
17965   verifyFormat("namespace a\n"
17966                "  {\n"
17967                "namespace b\n"
17968                "  {\n"
17969                "  class A\n"
17970                "    {\n"
17971                "    void f()\n"
17972                "      {\n"
17973                "      if (true)\n"
17974                "        {\n"
17975                "        a();\n"
17976                "        b();\n"
17977                "        }\n"
17978                "      }\n"
17979                "    void g()\n"
17980                "      {\n"
17981                "      return;\n"
17982                "      }\n"
17983                "    };\n"
17984                "  struct B\n"
17985                "    {\n"
17986                "    int x;\n"
17987                "    };\n"
17988                "  } // namespace b\n"
17989                "  } // namespace a",
17990                WhitesmithsBraceStyle);
17991 
17992   WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_All;
17993   verifyFormat("namespace a\n"
17994                "  {\n"
17995                "  namespace b\n"
17996                "    {\n"
17997                "    class A\n"
17998                "      {\n"
17999                "      void f()\n"
18000                "        {\n"
18001                "        if (true)\n"
18002                "          {\n"
18003                "          a();\n"
18004                "          b();\n"
18005                "          }\n"
18006                "        }\n"
18007                "      void g()\n"
18008                "        {\n"
18009                "        return;\n"
18010                "        }\n"
18011                "      };\n"
18012                "    struct B\n"
18013                "      {\n"
18014                "      int x;\n"
18015                "      };\n"
18016                "    } // namespace b\n"
18017                "  }   // namespace a",
18018                WhitesmithsBraceStyle);
18019 
18020   verifyFormat("void f()\n"
18021                "  {\n"
18022                "  if (true)\n"
18023                "    {\n"
18024                "    a();\n"
18025                "    }\n"
18026                "  else if (false)\n"
18027                "    {\n"
18028                "    b();\n"
18029                "    }\n"
18030                "  else\n"
18031                "    {\n"
18032                "    c();\n"
18033                "    }\n"
18034                "  }\n",
18035                WhitesmithsBraceStyle);
18036 
18037   verifyFormat("void f()\n"
18038                "  {\n"
18039                "  for (int i = 0; i < 10; ++i)\n"
18040                "    {\n"
18041                "    a();\n"
18042                "    }\n"
18043                "  while (false)\n"
18044                "    {\n"
18045                "    b();\n"
18046                "    }\n"
18047                "  do\n"
18048                "    {\n"
18049                "    c();\n"
18050                "    } while (false)\n"
18051                "  }\n",
18052                WhitesmithsBraceStyle);
18053 
18054   WhitesmithsBraceStyle.IndentCaseLabels = true;
18055   verifyFormat("void switchTest1(int a)\n"
18056                "  {\n"
18057                "  switch (a)\n"
18058                "    {\n"
18059                "    case 2:\n"
18060                "      {\n"
18061                "      }\n"
18062                "      break;\n"
18063                "    }\n"
18064                "  }\n",
18065                WhitesmithsBraceStyle);
18066 
18067   verifyFormat("void switchTest2(int a)\n"
18068                "  {\n"
18069                "  switch (a)\n"
18070                "    {\n"
18071                "    case 0:\n"
18072                "      break;\n"
18073                "    case 1:\n"
18074                "      {\n"
18075                "      break;\n"
18076                "      }\n"
18077                "    case 2:\n"
18078                "      {\n"
18079                "      }\n"
18080                "      break;\n"
18081                "    default:\n"
18082                "      break;\n"
18083                "    }\n"
18084                "  }\n",
18085                WhitesmithsBraceStyle);
18086 
18087   verifyFormat("void switchTest3(int a)\n"
18088                "  {\n"
18089                "  switch (a)\n"
18090                "    {\n"
18091                "    case 0:\n"
18092                "      {\n"
18093                "      foo(x);\n"
18094                "      }\n"
18095                "      break;\n"
18096                "    default:\n"
18097                "      {\n"
18098                "      foo(1);\n"
18099                "      }\n"
18100                "      break;\n"
18101                "    }\n"
18102                "  }\n",
18103                WhitesmithsBraceStyle);
18104 
18105   WhitesmithsBraceStyle.IndentCaseLabels = false;
18106 
18107   verifyFormat("void switchTest4(int a)\n"
18108                "  {\n"
18109                "  switch (a)\n"
18110                "    {\n"
18111                "  case 2:\n"
18112                "    {\n"
18113                "    }\n"
18114                "    break;\n"
18115                "    }\n"
18116                "  }\n",
18117                WhitesmithsBraceStyle);
18118 
18119   verifyFormat("void switchTest5(int a)\n"
18120                "  {\n"
18121                "  switch (a)\n"
18122                "    {\n"
18123                "  case 0:\n"
18124                "    break;\n"
18125                "  case 1:\n"
18126                "    {\n"
18127                "    foo();\n"
18128                "    break;\n"
18129                "    }\n"
18130                "  case 2:\n"
18131                "    {\n"
18132                "    }\n"
18133                "    break;\n"
18134                "  default:\n"
18135                "    break;\n"
18136                "    }\n"
18137                "  }\n",
18138                WhitesmithsBraceStyle);
18139 
18140   verifyFormat("void switchTest6(int a)\n"
18141                "  {\n"
18142                "  switch (a)\n"
18143                "    {\n"
18144                "  case 0:\n"
18145                "    {\n"
18146                "    foo(x);\n"
18147                "    }\n"
18148                "    break;\n"
18149                "  default:\n"
18150                "    {\n"
18151                "    foo(1);\n"
18152                "    }\n"
18153                "    break;\n"
18154                "    }\n"
18155                "  }\n",
18156                WhitesmithsBraceStyle);
18157 
18158   verifyFormat("enum X\n"
18159                "  {\n"
18160                "  Y = 0, // testing\n"
18161                "  }\n",
18162                WhitesmithsBraceStyle);
18163 
18164   verifyFormat("enum X\n"
18165                "  {\n"
18166                "  Y = 0\n"
18167                "  }\n",
18168                WhitesmithsBraceStyle);
18169   verifyFormat("enum X\n"
18170                "  {\n"
18171                "  Y = 0,\n"
18172                "  Z = 1\n"
18173                "  };\n",
18174                WhitesmithsBraceStyle);
18175 
18176   verifyFormat("@interface BSApplicationController ()\n"
18177                "  {\n"
18178                "@private\n"
18179                "  id _extraIvar;\n"
18180                "  }\n"
18181                "@end\n",
18182                WhitesmithsBraceStyle);
18183 
18184   verifyFormat("#ifdef _DEBUG\n"
18185                "int foo(int i = 0)\n"
18186                "#else\n"
18187                "int foo(int i = 5)\n"
18188                "#endif\n"
18189                "  {\n"
18190                "  return i;\n"
18191                "  }",
18192                WhitesmithsBraceStyle);
18193 
18194   verifyFormat("void foo() {}\n"
18195                "void bar()\n"
18196                "#ifdef _DEBUG\n"
18197                "  {\n"
18198                "  foo();\n"
18199                "  }\n"
18200                "#else\n"
18201                "  {\n"
18202                "  }\n"
18203                "#endif",
18204                WhitesmithsBraceStyle);
18205 
18206   verifyFormat("void foobar()\n"
18207                "  {\n"
18208                "  int i = 5;\n"
18209                "  }\n"
18210                "#ifdef _DEBUG\n"
18211                "void bar()\n"
18212                "  {\n"
18213                "  }\n"
18214                "#else\n"
18215                "void bar()\n"
18216                "  {\n"
18217                "  foobar();\n"
18218                "  }\n"
18219                "#endif",
18220                WhitesmithsBraceStyle);
18221 
18222   // This shouldn't affect ObjC blocks..
18223   verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
18224                "  // ...\n"
18225                "  int i;\n"
18226                "}];",
18227                WhitesmithsBraceStyle);
18228   verifyFormat("void (^block)(void) = ^{\n"
18229                "  // ...\n"
18230                "  int i;\n"
18231                "};",
18232                WhitesmithsBraceStyle);
18233   // .. or dict literals.
18234   verifyFormat("void f()\n"
18235                "  {\n"
18236                "  [object someMethod:@{@\"a\" : @\"b\"}];\n"
18237                "  }",
18238                WhitesmithsBraceStyle);
18239 
18240   verifyFormat("int f()\n"
18241                "  { // comment\n"
18242                "  return 42;\n"
18243                "  }",
18244                WhitesmithsBraceStyle);
18245 
18246   FormatStyle BreakBeforeBraceShortIfs = WhitesmithsBraceStyle;
18247   BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine =
18248       FormatStyle::SIS_OnlyFirstIf;
18249   BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
18250   verifyFormat("void f(bool b)\n"
18251                "  {\n"
18252                "  if (b)\n"
18253                "    {\n"
18254                "    return;\n"
18255                "    }\n"
18256                "  }\n",
18257                BreakBeforeBraceShortIfs);
18258   verifyFormat("void f(bool b)\n"
18259                "  {\n"
18260                "  if (b) return;\n"
18261                "  }\n",
18262                BreakBeforeBraceShortIfs);
18263   verifyFormat("void f(bool b)\n"
18264                "  {\n"
18265                "  while (b)\n"
18266                "    {\n"
18267                "    return;\n"
18268                "    }\n"
18269                "  }\n",
18270                BreakBeforeBraceShortIfs);
18271 }
18272 
18273 TEST_F(FormatTest, GNUBraceBreaking) {
18274   FormatStyle GNUBraceStyle = getLLVMStyle();
18275   GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
18276   verifyFormat("namespace a\n"
18277                "{\n"
18278                "class A\n"
18279                "{\n"
18280                "  void f()\n"
18281                "  {\n"
18282                "    int a;\n"
18283                "    {\n"
18284                "      int b;\n"
18285                "    }\n"
18286                "    if (true)\n"
18287                "      {\n"
18288                "        a();\n"
18289                "        b();\n"
18290                "      }\n"
18291                "  }\n"
18292                "  void g() { return; }\n"
18293                "}\n"
18294                "} // namespace a",
18295                GNUBraceStyle);
18296 
18297   verifyFormat("void f()\n"
18298                "{\n"
18299                "  if (true)\n"
18300                "    {\n"
18301                "      a();\n"
18302                "    }\n"
18303                "  else if (false)\n"
18304                "    {\n"
18305                "      b();\n"
18306                "    }\n"
18307                "  else\n"
18308                "    {\n"
18309                "      c();\n"
18310                "    }\n"
18311                "}\n",
18312                GNUBraceStyle);
18313 
18314   verifyFormat("void f()\n"
18315                "{\n"
18316                "  for (int i = 0; i < 10; ++i)\n"
18317                "    {\n"
18318                "      a();\n"
18319                "    }\n"
18320                "  while (false)\n"
18321                "    {\n"
18322                "      b();\n"
18323                "    }\n"
18324                "  do\n"
18325                "    {\n"
18326                "      c();\n"
18327                "    }\n"
18328                "  while (false);\n"
18329                "}\n",
18330                GNUBraceStyle);
18331 
18332   verifyFormat("void f(int a)\n"
18333                "{\n"
18334                "  switch (a)\n"
18335                "    {\n"
18336                "    case 0:\n"
18337                "      break;\n"
18338                "    case 1:\n"
18339                "      {\n"
18340                "        break;\n"
18341                "      }\n"
18342                "    case 2:\n"
18343                "      {\n"
18344                "      }\n"
18345                "      break;\n"
18346                "    default:\n"
18347                "      break;\n"
18348                "    }\n"
18349                "}\n",
18350                GNUBraceStyle);
18351 
18352   verifyFormat("enum X\n"
18353                "{\n"
18354                "  Y = 0,\n"
18355                "}\n",
18356                GNUBraceStyle);
18357 
18358   verifyFormat("@interface BSApplicationController ()\n"
18359                "{\n"
18360                "@private\n"
18361                "  id _extraIvar;\n"
18362                "}\n"
18363                "@end\n",
18364                GNUBraceStyle);
18365 
18366   verifyFormat("#ifdef _DEBUG\n"
18367                "int foo(int i = 0)\n"
18368                "#else\n"
18369                "int foo(int i = 5)\n"
18370                "#endif\n"
18371                "{\n"
18372                "  return i;\n"
18373                "}",
18374                GNUBraceStyle);
18375 
18376   verifyFormat("void foo() {}\n"
18377                "void bar()\n"
18378                "#ifdef _DEBUG\n"
18379                "{\n"
18380                "  foo();\n"
18381                "}\n"
18382                "#else\n"
18383                "{\n"
18384                "}\n"
18385                "#endif",
18386                GNUBraceStyle);
18387 
18388   verifyFormat("void foobar() { int i = 5; }\n"
18389                "#ifdef _DEBUG\n"
18390                "void bar() {}\n"
18391                "#else\n"
18392                "void bar() { foobar(); }\n"
18393                "#endif",
18394                GNUBraceStyle);
18395 }
18396 
18397 TEST_F(FormatTest, WebKitBraceBreaking) {
18398   FormatStyle WebKitBraceStyle = getLLVMStyle();
18399   WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit;
18400   WebKitBraceStyle.FixNamespaceComments = false;
18401   verifyFormat("namespace a {\n"
18402                "class A {\n"
18403                "  void f()\n"
18404                "  {\n"
18405                "    if (true) {\n"
18406                "      a();\n"
18407                "      b();\n"
18408                "    }\n"
18409                "  }\n"
18410                "  void g() { return; }\n"
18411                "};\n"
18412                "enum E {\n"
18413                "  A,\n"
18414                "  // foo\n"
18415                "  B,\n"
18416                "  C\n"
18417                "};\n"
18418                "struct B {\n"
18419                "  int x;\n"
18420                "};\n"
18421                "}\n",
18422                WebKitBraceStyle);
18423   verifyFormat("struct S {\n"
18424                "  int Type;\n"
18425                "  union {\n"
18426                "    int x;\n"
18427                "    double y;\n"
18428                "  } Value;\n"
18429                "  class C {\n"
18430                "    MyFavoriteType Value;\n"
18431                "  } Class;\n"
18432                "};\n",
18433                WebKitBraceStyle);
18434 }
18435 
18436 TEST_F(FormatTest, CatchExceptionReferenceBinding) {
18437   verifyFormat("void f() {\n"
18438                "  try {\n"
18439                "  } catch (const Exception &e) {\n"
18440                "  }\n"
18441                "}\n",
18442                getLLVMStyle());
18443 }
18444 
18445 TEST_F(FormatTest, CatchAlignArrayOfStructuresRightAlignment) {
18446   auto Style = getLLVMStyle();
18447   Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
18448   Style.AlignConsecutiveAssignments =
18449       FormatStyle::AlignConsecutiveStyle::ACS_Consecutive;
18450   Style.AlignConsecutiveDeclarations =
18451       FormatStyle::AlignConsecutiveStyle::ACS_Consecutive;
18452   verifyFormat("struct test demo[] = {\n"
18453                "    {56,    23, \"hello\"},\n"
18454                "    {-1, 93463, \"world\"},\n"
18455                "    { 7,     5,    \"!!\"}\n"
18456                "};\n",
18457                Style);
18458 
18459   verifyFormat("struct test demo[] = {\n"
18460                "    {56,    23, \"hello\"}, // first line\n"
18461                "    {-1, 93463, \"world\"}, // second line\n"
18462                "    { 7,     5,    \"!!\"}  // third line\n"
18463                "};\n",
18464                Style);
18465 
18466   verifyFormat("struct test demo[4] = {\n"
18467                "    { 56,    23, 21,       \"oh\"}, // first line\n"
18468                "    { -1, 93463, 22,       \"my\"}, // second line\n"
18469                "    {  7,     5,  1, \"goodness\"}  // third line\n"
18470                "    {234,     5,  1, \"gracious\"}  // fourth line\n"
18471                "};\n",
18472                Style);
18473 
18474   verifyFormat("struct test demo[3] = {\n"
18475                "    {56,    23, \"hello\"},\n"
18476                "    {-1, 93463, \"world\"},\n"
18477                "    { 7,     5,    \"!!\"}\n"
18478                "};\n",
18479                Style);
18480 
18481   verifyFormat("struct test demo[3] = {\n"
18482                "    {int{56},    23, \"hello\"},\n"
18483                "    {int{-1}, 93463, \"world\"},\n"
18484                "    { int{7},     5,    \"!!\"}\n"
18485                "};\n",
18486                Style);
18487 
18488   verifyFormat("struct test demo[] = {\n"
18489                "    {56,    23, \"hello\"},\n"
18490                "    {-1, 93463, \"world\"},\n"
18491                "    { 7,     5,    \"!!\"},\n"
18492                "};\n",
18493                Style);
18494 
18495   verifyFormat("test demo[] = {\n"
18496                "    {56,    23, \"hello\"},\n"
18497                "    {-1, 93463, \"world\"},\n"
18498                "    { 7,     5,    \"!!\"},\n"
18499                "};\n",
18500                Style);
18501 
18502   verifyFormat("demo = std::array<struct test, 3>{\n"
18503                "    test{56,    23, \"hello\"},\n"
18504                "    test{-1, 93463, \"world\"},\n"
18505                "    test{ 7,     5,    \"!!\"},\n"
18506                "};\n",
18507                Style);
18508 
18509   verifyFormat("test demo[] = {\n"
18510                "    {56,    23, \"hello\"},\n"
18511                "#if X\n"
18512                "    {-1, 93463, \"world\"},\n"
18513                "#endif\n"
18514                "    { 7,     5,    \"!!\"}\n"
18515                "};\n",
18516                Style);
18517 
18518   verifyFormat(
18519       "test demo[] = {\n"
18520       "    { 7,    23,\n"
18521       "     \"hello world i am a very long line that really, in any\"\n"
18522       "     \"just world, ought to be split over multiple lines\"},\n"
18523       "    {-1, 93463,                                  \"world\"},\n"
18524       "    {56,     5,                                     \"!!\"}\n"
18525       "};\n",
18526       Style);
18527 
18528   verifyFormat("return GradForUnaryCwise(g, {\n"
18529                "                                {{\"sign\"}, \"Sign\",  "
18530                "  {\"x\", \"dy\"}},\n"
18531                "                                {  {\"dx\"},  \"Mul\", {\"dy\""
18532                ", \"sign\"}},\n"
18533                "});\n",
18534                Style);
18535 
18536   Style.ColumnLimit = 0;
18537   EXPECT_EQ(
18538       "test demo[] = {\n"
18539       "    {56,    23, \"hello world i am a very long line that really, "
18540       "in any just world, ought to be split over multiple lines\"},\n"
18541       "    {-1, 93463,                                                  "
18542       "                                                 \"world\"},\n"
18543       "    { 7,     5,                                                  "
18544       "                                                    \"!!\"},\n"
18545       "};",
18546       format("test demo[] = {{56, 23, \"hello world i am a very long line "
18547              "that really, in any just world, ought to be split over multiple "
18548              "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
18549              Style));
18550 
18551   Style.ColumnLimit = 80;
18552   verifyFormat("test demo[] = {\n"
18553                "    {56,    23, /* a comment */ \"hello\"},\n"
18554                "    {-1, 93463,                 \"world\"},\n"
18555                "    { 7,     5,                    \"!!\"}\n"
18556                "};\n",
18557                Style);
18558 
18559   verifyFormat("test demo[] = {\n"
18560                "    {56,    23,                    \"hello\"},\n"
18561                "    {-1, 93463, \"world\" /* comment here */},\n"
18562                "    { 7,     5,                       \"!!\"}\n"
18563                "};\n",
18564                Style);
18565 
18566   verifyFormat("test demo[] = {\n"
18567                "    {56, /* a comment */ 23, \"hello\"},\n"
18568                "    {-1,              93463, \"world\"},\n"
18569                "    { 7,                  5,    \"!!\"}\n"
18570                "};\n",
18571                Style);
18572 
18573   Style.ColumnLimit = 20;
18574   EXPECT_EQ(
18575       "demo = std::array<\n"
18576       "    struct test, 3>{\n"
18577       "    test{\n"
18578       "         56,    23,\n"
18579       "         \"hello \"\n"
18580       "         \"world i \"\n"
18581       "         \"am a very \"\n"
18582       "         \"long line \"\n"
18583       "         \"that \"\n"
18584       "         \"really, \"\n"
18585       "         \"in any \"\n"
18586       "         \"just \"\n"
18587       "         \"world, \"\n"
18588       "         \"ought to \"\n"
18589       "         \"be split \"\n"
18590       "         \"over \"\n"
18591       "         \"multiple \"\n"
18592       "         \"lines\"},\n"
18593       "    test{-1, 93463,\n"
18594       "         \"world\"},\n"
18595       "    test{ 7,     5,\n"
18596       "         \"!!\"   },\n"
18597       "};",
18598       format("demo = std::array<struct test, 3>{test{56, 23, \"hello world "
18599              "i am a very long line that really, in any just world, ought "
18600              "to be split over multiple lines\"},test{-1, 93463, \"world\"},"
18601              "test{7, 5, \"!!\"},};",
18602              Style));
18603   // This caused a core dump by enabling Alignment in the LLVMStyle globally
18604   Style = getLLVMStyleWithColumns(50);
18605   Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
18606   verifyFormat("static A x = {\n"
18607                "    {{init1, init2, init3, init4},\n"
18608                "     {init1, init2, init3, init4}}\n"
18609                "};",
18610                Style);
18611   Style.ColumnLimit = 100;
18612   EXPECT_EQ(
18613       "test demo[] = {\n"
18614       "    {56,    23,\n"
18615       "     \"hello world i am a very long line that really, in any just world"
18616       ", ought to be split over \"\n"
18617       "     \"multiple lines\"  },\n"
18618       "    {-1, 93463, \"world\"},\n"
18619       "    { 7,     5,    \"!!\"},\n"
18620       "};",
18621       format("test demo[] = {{56, 23, \"hello world i am a very long line "
18622              "that really, in any just world, ought to be split over multiple "
18623              "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
18624              Style));
18625 
18626   Style = getLLVMStyleWithColumns(50);
18627   Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
18628   Style.AlignConsecutiveAssignments =
18629       FormatStyle::AlignConsecutiveStyle::ACS_Consecutive;
18630   Style.AlignConsecutiveDeclarations =
18631       FormatStyle::AlignConsecutiveStyle::ACS_Consecutive;
18632   verifyFormat("struct test demo[] = {\n"
18633                "    {56,    23, \"hello\"},\n"
18634                "    {-1, 93463, \"world\"},\n"
18635                "    { 7,     5,    \"!!\"}\n"
18636                "};\n"
18637                "static A x = {\n"
18638                "    {{init1, init2, init3, init4},\n"
18639                "     {init1, init2, init3, init4}}\n"
18640                "};",
18641                Style);
18642   Style.ColumnLimit = 100;
18643   Style.AlignConsecutiveAssignments =
18644       FormatStyle::AlignConsecutiveStyle::ACS_AcrossComments;
18645   Style.AlignConsecutiveDeclarations =
18646       FormatStyle::AlignConsecutiveStyle::ACS_AcrossComments;
18647   verifyFormat("struct test demo[] = {\n"
18648                "    {56,    23, \"hello\"},\n"
18649                "    {-1, 93463, \"world\"},\n"
18650                "    { 7,     5,    \"!!\"}\n"
18651                "};\n"
18652                "struct test demo[4] = {\n"
18653                "    { 56,    23, 21,       \"oh\"}, // first line\n"
18654                "    { -1, 93463, 22,       \"my\"}, // second line\n"
18655                "    {  7,     5,  1, \"goodness\"}  // third line\n"
18656                "    {234,     5,  1, \"gracious\"}  // fourth line\n"
18657                "};\n",
18658                Style);
18659   EXPECT_EQ(
18660       "test demo[] = {\n"
18661       "    {56,\n"
18662       "     \"hello world i am a very long line that really, in any just world"
18663       ", ought to be split over \"\n"
18664       "     \"multiple lines\",    23},\n"
18665       "    {-1,      \"world\", 93463},\n"
18666       "    { 7,         \"!!\",     5},\n"
18667       "};",
18668       format("test demo[] = {{56, \"hello world i am a very long line "
18669              "that really, in any just world, ought to be split over multiple "
18670              "lines\", 23},{-1, \"world\", 93463},{7, \"!!\", 5},};",
18671              Style));
18672 }
18673 
18674 TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) {
18675   auto Style = getLLVMStyle();
18676   Style.AlignArrayOfStructures = FormatStyle::AIAS_Left;
18677   /* FIXME: This case gets misformatted.
18678   verifyFormat("auto foo = Items{\n"
18679                "    Section{0, bar(), },\n"
18680                "    Section{1, boo()  }\n"
18681                "};\n",
18682                Style);
18683   */
18684   verifyFormat("auto foo = Items{\n"
18685                "    Section{\n"
18686                "            0, bar(),\n"
18687                "            }\n"
18688                "};\n",
18689                Style);
18690   verifyFormat("struct test demo[] = {\n"
18691                "    {56, 23,    \"hello\"},\n"
18692                "    {-1, 93463, \"world\"},\n"
18693                "    {7,  5,     \"!!\"   }\n"
18694                "};\n",
18695                Style);
18696   verifyFormat("struct test demo[] = {\n"
18697                "    {56, 23,    \"hello\"}, // first line\n"
18698                "    {-1, 93463, \"world\"}, // second line\n"
18699                "    {7,  5,     \"!!\"   }  // third line\n"
18700                "};\n",
18701                Style);
18702   verifyFormat("struct test demo[4] = {\n"
18703                "    {56,  23,    21, \"oh\"      }, // first line\n"
18704                "    {-1,  93463, 22, \"my\"      }, // second line\n"
18705                "    {7,   5,     1,  \"goodness\"}  // third line\n"
18706                "    {234, 5,     1,  \"gracious\"}  // fourth line\n"
18707                "};\n",
18708                Style);
18709   verifyFormat("struct test demo[3] = {\n"
18710                "    {56, 23,    \"hello\"},\n"
18711                "    {-1, 93463, \"world\"},\n"
18712                "    {7,  5,     \"!!\"   }\n"
18713                "};\n",
18714                Style);
18715 
18716   verifyFormat("struct test demo[3] = {\n"
18717                "    {int{56}, 23,    \"hello\"},\n"
18718                "    {int{-1}, 93463, \"world\"},\n"
18719                "    {int{7},  5,     \"!!\"   }\n"
18720                "};\n",
18721                Style);
18722   verifyFormat("struct test demo[] = {\n"
18723                "    {56, 23,    \"hello\"},\n"
18724                "    {-1, 93463, \"world\"},\n"
18725                "    {7,  5,     \"!!\"   },\n"
18726                "};\n",
18727                Style);
18728   verifyFormat("test demo[] = {\n"
18729                "    {56, 23,    \"hello\"},\n"
18730                "    {-1, 93463, \"world\"},\n"
18731                "    {7,  5,     \"!!\"   },\n"
18732                "};\n",
18733                Style);
18734   verifyFormat("demo = std::array<struct test, 3>{\n"
18735                "    test{56, 23,    \"hello\"},\n"
18736                "    test{-1, 93463, \"world\"},\n"
18737                "    test{7,  5,     \"!!\"   },\n"
18738                "};\n",
18739                Style);
18740   verifyFormat("test demo[] = {\n"
18741                "    {56, 23,    \"hello\"},\n"
18742                "#if X\n"
18743                "    {-1, 93463, \"world\"},\n"
18744                "#endif\n"
18745                "    {7,  5,     \"!!\"   }\n"
18746                "};\n",
18747                Style);
18748   verifyFormat(
18749       "test demo[] = {\n"
18750       "    {7,  23,\n"
18751       "     \"hello world i am a very long line that really, in any\"\n"
18752       "     \"just world, ought to be split over multiple lines\"},\n"
18753       "    {-1, 93463, \"world\"                                 },\n"
18754       "    {56, 5,     \"!!\"                                    }\n"
18755       "};\n",
18756       Style);
18757 
18758   verifyFormat("return GradForUnaryCwise(g, {\n"
18759                "                                {{\"sign\"}, \"Sign\", {\"x\", "
18760                "\"dy\"}   },\n"
18761                "                                {{\"dx\"},   \"Mul\",  "
18762                "{\"dy\", \"sign\"}},\n"
18763                "});\n",
18764                Style);
18765 
18766   Style.ColumnLimit = 0;
18767   EXPECT_EQ(
18768       "test demo[] = {\n"
18769       "    {56, 23,    \"hello world i am a very long line that really, in any "
18770       "just world, ought to be split over multiple lines\"},\n"
18771       "    {-1, 93463, \"world\"                                               "
18772       "                                                   },\n"
18773       "    {7,  5,     \"!!\"                                                  "
18774       "                                                   },\n"
18775       "};",
18776       format("test demo[] = {{56, 23, \"hello world i am a very long line "
18777              "that really, in any just world, ought to be split over multiple "
18778              "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
18779              Style));
18780 
18781   Style.ColumnLimit = 80;
18782   verifyFormat("test demo[] = {\n"
18783                "    {56, 23,    /* a comment */ \"hello\"},\n"
18784                "    {-1, 93463, \"world\"                },\n"
18785                "    {7,  5,     \"!!\"                   }\n"
18786                "};\n",
18787                Style);
18788 
18789   verifyFormat("test demo[] = {\n"
18790                "    {56, 23,    \"hello\"                   },\n"
18791                "    {-1, 93463, \"world\" /* comment here */},\n"
18792                "    {7,  5,     \"!!\"                      }\n"
18793                "};\n",
18794                Style);
18795 
18796   verifyFormat("test demo[] = {\n"
18797                "    {56, /* a comment */ 23, \"hello\"},\n"
18798                "    {-1, 93463,              \"world\"},\n"
18799                "    {7,  5,                  \"!!\"   }\n"
18800                "};\n",
18801                Style);
18802 
18803   Style.ColumnLimit = 20;
18804   EXPECT_EQ(
18805       "demo = std::array<\n"
18806       "    struct test, 3>{\n"
18807       "    test{\n"
18808       "         56, 23,\n"
18809       "         \"hello \"\n"
18810       "         \"world i \"\n"
18811       "         \"am a very \"\n"
18812       "         \"long line \"\n"
18813       "         \"that \"\n"
18814       "         \"really, \"\n"
18815       "         \"in any \"\n"
18816       "         \"just \"\n"
18817       "         \"world, \"\n"
18818       "         \"ought to \"\n"
18819       "         \"be split \"\n"
18820       "         \"over \"\n"
18821       "         \"multiple \"\n"
18822       "         \"lines\"},\n"
18823       "    test{-1, 93463,\n"
18824       "         \"world\"},\n"
18825       "    test{7,  5,\n"
18826       "         \"!!\"   },\n"
18827       "};",
18828       format("demo = std::array<struct test, 3>{test{56, 23, \"hello world "
18829              "i am a very long line that really, in any just world, ought "
18830              "to be split over multiple lines\"},test{-1, 93463, \"world\"},"
18831              "test{7, 5, \"!!\"},};",
18832              Style));
18833 
18834   // This caused a core dump by enabling Alignment in the LLVMStyle globally
18835   Style = getLLVMStyleWithColumns(50);
18836   Style.AlignArrayOfStructures = FormatStyle::AIAS_Left;
18837   verifyFormat("static A x = {\n"
18838                "    {{init1, init2, init3, init4},\n"
18839                "     {init1, init2, init3, init4}}\n"
18840                "};",
18841                Style);
18842   Style.ColumnLimit = 100;
18843   EXPECT_EQ(
18844       "test demo[] = {\n"
18845       "    {56, 23,\n"
18846       "     \"hello world i am a very long line that really, in any just world"
18847       ", ought to be split over \"\n"
18848       "     \"multiple lines\"  },\n"
18849       "    {-1, 93463, \"world\"},\n"
18850       "    {7,  5,     \"!!\"   },\n"
18851       "};",
18852       format("test demo[] = {{56, 23, \"hello world i am a very long line "
18853              "that really, in any just world, ought to be split over multiple "
18854              "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
18855              Style));
18856 }
18857 
18858 TEST_F(FormatTest, UnderstandsPragmas) {
18859   verifyFormat("#pragma omp reduction(| : var)");
18860   verifyFormat("#pragma omp reduction(+ : var)");
18861 
18862   EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string "
18863             "(including parentheses).",
18864             format("#pragma    mark   Any non-hyphenated or hyphenated string "
18865                    "(including parentheses)."));
18866 }
18867 
18868 TEST_F(FormatTest, UnderstandPragmaOption) {
18869   verifyFormat("#pragma option -C -A");
18870 
18871   EXPECT_EQ("#pragma option -C -A", format("#pragma    option   -C   -A"));
18872 }
18873 
18874 TEST_F(FormatTest, OptimizeBreakPenaltyVsExcess) {
18875   FormatStyle Style = getLLVMStyleWithColumns(20);
18876 
18877   // See PR41213
18878   EXPECT_EQ("/*\n"
18879             " *\t9012345\n"
18880             " * /8901\n"
18881             " */",
18882             format("/*\n"
18883                    " *\t9012345 /8901\n"
18884                    " */",
18885                    Style));
18886   EXPECT_EQ("/*\n"
18887             " *345678\n"
18888             " *\t/8901\n"
18889             " */",
18890             format("/*\n"
18891                    " *345678\t/8901\n"
18892                    " */",
18893                    Style));
18894 
18895   verifyFormat("int a; // the\n"
18896                "       // comment",
18897                Style);
18898   EXPECT_EQ("int a; /* first line\n"
18899             "        * second\n"
18900             "        * line third\n"
18901             "        * line\n"
18902             "        */",
18903             format("int a; /* first line\n"
18904                    "        * second\n"
18905                    "        * line third\n"
18906                    "        * line\n"
18907                    "        */",
18908                    Style));
18909   EXPECT_EQ("int a; // first line\n"
18910             "       // second\n"
18911             "       // line third\n"
18912             "       // line",
18913             format("int a; // first line\n"
18914                    "       // second line\n"
18915                    "       // third line",
18916                    Style));
18917 
18918   Style.PenaltyExcessCharacter = 90;
18919   verifyFormat("int a; // the comment", Style);
18920   EXPECT_EQ("int a; // the comment\n"
18921             "       // aaa",
18922             format("int a; // the comment aaa", Style));
18923   EXPECT_EQ("int a; /* first line\n"
18924             "        * second line\n"
18925             "        * third line\n"
18926             "        */",
18927             format("int a; /* first line\n"
18928                    "        * second line\n"
18929                    "        * third line\n"
18930                    "        */",
18931                    Style));
18932   EXPECT_EQ("int a; // first line\n"
18933             "       // second line\n"
18934             "       // third line",
18935             format("int a; // first line\n"
18936                    "       // second line\n"
18937                    "       // third line",
18938                    Style));
18939   // FIXME: Investigate why this is not getting the same layout as the test
18940   // above.
18941   EXPECT_EQ("int a; /* first line\n"
18942             "        * second line\n"
18943             "        * third line\n"
18944             "        */",
18945             format("int a; /* first line second line third line"
18946                    "\n*/",
18947                    Style));
18948 
18949   EXPECT_EQ("// foo bar baz bazfoo\n"
18950             "// foo bar foo bar\n",
18951             format("// foo bar baz bazfoo\n"
18952                    "// foo bar foo           bar\n",
18953                    Style));
18954   EXPECT_EQ("// foo bar baz bazfoo\n"
18955             "// foo bar foo bar\n",
18956             format("// foo bar baz      bazfoo\n"
18957                    "// foo            bar foo bar\n",
18958                    Style));
18959 
18960   // FIXME: Optimally, we'd keep bazfoo on the first line and reflow bar to the
18961   // next one.
18962   EXPECT_EQ("// foo bar baz bazfoo\n"
18963             "// bar foo bar\n",
18964             format("// foo bar baz      bazfoo bar\n"
18965                    "// foo            bar\n",
18966                    Style));
18967 
18968   EXPECT_EQ("// foo bar baz bazfoo\n"
18969             "// foo bar baz bazfoo\n"
18970             "// bar foo bar\n",
18971             format("// foo bar baz      bazfoo\n"
18972                    "// foo bar baz      bazfoo bar\n"
18973                    "// foo bar\n",
18974                    Style));
18975 
18976   EXPECT_EQ("// foo bar baz bazfoo\n"
18977             "// foo bar baz bazfoo\n"
18978             "// bar foo bar\n",
18979             format("// foo bar baz      bazfoo\n"
18980                    "// foo bar baz      bazfoo bar\n"
18981                    "// foo           bar\n",
18982                    Style));
18983 
18984   // Make sure we do not keep protruding characters if strict mode reflow is
18985   // cheaper than keeping protruding characters.
18986   Style.ColumnLimit = 21;
18987   EXPECT_EQ(
18988       "// foo foo foo foo\n"
18989       "// foo foo foo foo\n"
18990       "// foo foo foo foo\n",
18991       format("// foo foo foo foo foo foo foo foo foo foo foo foo\n", Style));
18992 
18993   EXPECT_EQ("int a = /* long block\n"
18994             "           comment */\n"
18995             "    42;",
18996             format("int a = /* long block comment */ 42;", Style));
18997 }
18998 
18999 TEST_F(FormatTest, BreakPenaltyAfterLParen) {
19000   FormatStyle Style = getLLVMStyle();
19001   Style.ColumnLimit = 8;
19002   Style.PenaltyExcessCharacter = 15;
19003   verifyFormat("int foo(\n"
19004                "    int aaaaaaaaaaaaaaaaaaaaaaaa);",
19005                Style);
19006   Style.PenaltyBreakOpenParenthesis = 200;
19007   EXPECT_EQ("int foo(int aaaaaaaaaaaaaaaaaaaaaaaa);",
19008             format("int foo(\n"
19009                    "    int aaaaaaaaaaaaaaaaaaaaaaaa);",
19010                    Style));
19011 }
19012 
19013 TEST_F(FormatTest, BreakPenaltyAfterCastLParen) {
19014   FormatStyle Style = getLLVMStyle();
19015   Style.ColumnLimit = 5;
19016   Style.PenaltyExcessCharacter = 150;
19017   verifyFormat("foo((\n"
19018                "    int)aaaaaaaaaaaaaaaaaaaaaaaa);",
19019 
19020                Style);
19021   Style.PenaltyBreakOpenParenthesis = 100000;
19022   EXPECT_EQ("foo((int)\n"
19023             "        aaaaaaaaaaaaaaaaaaaaaaaa);",
19024             format("foo((\n"
19025                    "int)aaaaaaaaaaaaaaaaaaaaaaaa);",
19026                    Style));
19027 }
19028 
19029 TEST_F(FormatTest, BreakPenaltyAfterForLoopLParen) {
19030   FormatStyle Style = getLLVMStyle();
19031   Style.ColumnLimit = 4;
19032   Style.PenaltyExcessCharacter = 100;
19033   verifyFormat("for (\n"
19034                "    int iiiiiiiiiiiiiiiii =\n"
19035                "        0;\n"
19036                "    iiiiiiiiiiiiiiiii <\n"
19037                "    2;\n"
19038                "    iiiiiiiiiiiiiiiii++) {\n"
19039                "}",
19040 
19041                Style);
19042   Style.PenaltyBreakOpenParenthesis = 1250;
19043   EXPECT_EQ("for (int iiiiiiiiiiiiiiiii =\n"
19044             "         0;\n"
19045             "     iiiiiiiiiiiiiiiii <\n"
19046             "     2;\n"
19047             "     iiiiiiiiiiiiiiiii++) {\n"
19048             "}",
19049             format("for (\n"
19050                    "    int iiiiiiiiiiiiiiiii =\n"
19051                    "        0;\n"
19052                    "    iiiiiiiiiiiiiiiii <\n"
19053                    "    2;\n"
19054                    "    iiiiiiiiiiiiiiiii++) {\n"
19055                    "}",
19056                    Style));
19057 }
19058 
19059 #define EXPECT_ALL_STYLES_EQUAL(Styles)                                        \
19060   for (size_t i = 1; i < Styles.size(); ++i)                                   \
19061   EXPECT_EQ(Styles[0], Styles[i])                                              \
19062       << "Style #" << i << " of " << Styles.size() << " differs from Style #0"
19063 
19064 TEST_F(FormatTest, GetsPredefinedStyleByName) {
19065   SmallVector<FormatStyle, 3> Styles;
19066   Styles.resize(3);
19067 
19068   Styles[0] = getLLVMStyle();
19069   EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1]));
19070   EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2]));
19071   EXPECT_ALL_STYLES_EQUAL(Styles);
19072 
19073   Styles[0] = getGoogleStyle();
19074   EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1]));
19075   EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2]));
19076   EXPECT_ALL_STYLES_EQUAL(Styles);
19077 
19078   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
19079   EXPECT_TRUE(
19080       getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1]));
19081   EXPECT_TRUE(
19082       getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2]));
19083   EXPECT_ALL_STYLES_EQUAL(Styles);
19084 
19085   Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp);
19086   EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1]));
19087   EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2]));
19088   EXPECT_ALL_STYLES_EQUAL(Styles);
19089 
19090   Styles[0] = getMozillaStyle();
19091   EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1]));
19092   EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2]));
19093   EXPECT_ALL_STYLES_EQUAL(Styles);
19094 
19095   Styles[0] = getWebKitStyle();
19096   EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1]));
19097   EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2]));
19098   EXPECT_ALL_STYLES_EQUAL(Styles);
19099 
19100   Styles[0] = getGNUStyle();
19101   EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1]));
19102   EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2]));
19103   EXPECT_ALL_STYLES_EQUAL(Styles);
19104 
19105   EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0]));
19106 }
19107 
19108 TEST_F(FormatTest, GetsCorrectBasedOnStyle) {
19109   SmallVector<FormatStyle, 8> Styles;
19110   Styles.resize(2);
19111 
19112   Styles[0] = getGoogleStyle();
19113   Styles[1] = getLLVMStyle();
19114   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
19115   EXPECT_ALL_STYLES_EQUAL(Styles);
19116 
19117   Styles.resize(5);
19118   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
19119   Styles[1] = getLLVMStyle();
19120   Styles[1].Language = FormatStyle::LK_JavaScript;
19121   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
19122 
19123   Styles[2] = getLLVMStyle();
19124   Styles[2].Language = FormatStyle::LK_JavaScript;
19125   EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n"
19126                                   "BasedOnStyle: Google",
19127                                   &Styles[2])
19128                    .value());
19129 
19130   Styles[3] = getLLVMStyle();
19131   Styles[3].Language = FormatStyle::LK_JavaScript;
19132   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n"
19133                                   "Language: JavaScript",
19134                                   &Styles[3])
19135                    .value());
19136 
19137   Styles[4] = getLLVMStyle();
19138   Styles[4].Language = FormatStyle::LK_JavaScript;
19139   EXPECT_EQ(0, parseConfiguration("---\n"
19140                                   "BasedOnStyle: LLVM\n"
19141                                   "IndentWidth: 123\n"
19142                                   "---\n"
19143                                   "BasedOnStyle: Google\n"
19144                                   "Language: JavaScript",
19145                                   &Styles[4])
19146                    .value());
19147   EXPECT_ALL_STYLES_EQUAL(Styles);
19148 }
19149 
19150 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME)                             \
19151   Style.FIELD = false;                                                         \
19152   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value());      \
19153   EXPECT_TRUE(Style.FIELD);                                                    \
19154   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value());     \
19155   EXPECT_FALSE(Style.FIELD);
19156 
19157 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD)
19158 
19159 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME)              \
19160   Style.STRUCT.FIELD = false;                                                  \
19161   EXPECT_EQ(0,                                                                 \
19162             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": true", &Style)   \
19163                 .value());                                                     \
19164   EXPECT_TRUE(Style.STRUCT.FIELD);                                             \
19165   EXPECT_EQ(0,                                                                 \
19166             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": false", &Style)  \
19167                 .value());                                                     \
19168   EXPECT_FALSE(Style.STRUCT.FIELD);
19169 
19170 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD)                                 \
19171   CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD)
19172 
19173 #define CHECK_PARSE(TEXT, FIELD, VALUE)                                        \
19174   EXPECT_NE(VALUE, Style.FIELD) << "Initial value already the same!";          \
19175   EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value());                      \
19176   EXPECT_EQ(VALUE, Style.FIELD) << "Unexpected value after parsing!"
19177 
19178 TEST_F(FormatTest, ParsesConfigurationBools) {
19179   FormatStyle Style = {};
19180   Style.Language = FormatStyle::LK_Cpp;
19181   CHECK_PARSE_BOOL(AlignTrailingComments);
19182   CHECK_PARSE_BOOL(AllowAllArgumentsOnNextLine);
19183   CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
19184   CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine);
19185   CHECK_PARSE_BOOL(AllowShortEnumsOnASingleLine);
19186   CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
19187   CHECK_PARSE_BOOL(BinPackArguments);
19188   CHECK_PARSE_BOOL(BinPackParameters);
19189   CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations);
19190   CHECK_PARSE_BOOL(BreakBeforeConceptDeclarations);
19191   CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
19192   CHECK_PARSE_BOOL(BreakStringLiterals);
19193   CHECK_PARSE_BOOL(CompactNamespaces);
19194   CHECK_PARSE_BOOL(DeriveLineEnding);
19195   CHECK_PARSE_BOOL(DerivePointerAlignment);
19196   CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding");
19197   CHECK_PARSE_BOOL(DisableFormat);
19198   CHECK_PARSE_BOOL(IndentAccessModifiers);
19199   CHECK_PARSE_BOOL(IndentCaseLabels);
19200   CHECK_PARSE_BOOL(IndentCaseBlocks);
19201   CHECK_PARSE_BOOL(IndentGotoLabels);
19202   CHECK_PARSE_BOOL(IndentRequires);
19203   CHECK_PARSE_BOOL(IndentWrappedFunctionNames);
19204   CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks);
19205   CHECK_PARSE_BOOL(ObjCSpaceAfterProperty);
19206   CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
19207   CHECK_PARSE_BOOL(Cpp11BracedListStyle);
19208   CHECK_PARSE_BOOL(ReflowComments);
19209   CHECK_PARSE_BOOL(RemoveBracesLLVM);
19210   CHECK_PARSE_BOOL(SortUsingDeclarations);
19211   CHECK_PARSE_BOOL(SpacesInParentheses);
19212   CHECK_PARSE_BOOL(SpacesInSquareBrackets);
19213   CHECK_PARSE_BOOL(SpacesInConditionalStatement);
19214   CHECK_PARSE_BOOL(SpaceInEmptyBlock);
19215   CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
19216   CHECK_PARSE_BOOL(SpacesInContainerLiterals);
19217   CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
19218   CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
19219   CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword);
19220   CHECK_PARSE_BOOL(SpaceAfterLogicalNot);
19221   CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
19222   CHECK_PARSE_BOOL(SpaceBeforeCaseColon);
19223   CHECK_PARSE_BOOL(SpaceBeforeCpp11BracedList);
19224   CHECK_PARSE_BOOL(SpaceBeforeCtorInitializerColon);
19225   CHECK_PARSE_BOOL(SpaceBeforeInheritanceColon);
19226   CHECK_PARSE_BOOL(SpaceBeforeRangeBasedForLoopColon);
19227   CHECK_PARSE_BOOL(SpaceBeforeSquareBrackets);
19228   CHECK_PARSE_BOOL(UseCRLF);
19229 
19230   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterCaseLabel);
19231   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass);
19232   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum);
19233   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction);
19234   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace);
19235   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration);
19236   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct);
19237   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion);
19238   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterExternBlock);
19239   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch);
19240   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse);
19241   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeLambdaBody);
19242   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeWhile);
19243   CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces);
19244   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyFunction);
19245   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyRecord);
19246   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyNamespace);
19247   CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterControlStatements);
19248   CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterForeachMacros);
19249   CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions,
19250                           AfterFunctionDeclarationName);
19251   CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions,
19252                           AfterFunctionDefinitionName);
19253   CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterIfMacros);
19254   CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterOverloadedOperator);
19255   CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, BeforeNonEmptyParentheses);
19256 }
19257 
19258 #undef CHECK_PARSE_BOOL
19259 
19260 TEST_F(FormatTest, ParsesConfiguration) {
19261   FormatStyle Style = {};
19262   Style.Language = FormatStyle::LK_Cpp;
19263   CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
19264   CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",
19265               ConstructorInitializerIndentWidth, 1234u);
19266   CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u);
19267   CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
19268   CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
19269   CHECK_PARSE("PenaltyBreakAssignment: 1234", PenaltyBreakAssignment, 1234u);
19270   CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234",
19271               PenaltyBreakBeforeFirstCallParameter, 1234u);
19272   CHECK_PARSE("PenaltyBreakTemplateDeclaration: 1234",
19273               PenaltyBreakTemplateDeclaration, 1234u);
19274   CHECK_PARSE("PenaltyBreakOpenParenthesis: 1234", PenaltyBreakOpenParenthesis,
19275               1234u);
19276   CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
19277   CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
19278               PenaltyReturnTypeOnItsOwnLine, 1234u);
19279   CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
19280               SpacesBeforeTrailingComments, 1234u);
19281   CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
19282   CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u);
19283   CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$");
19284 
19285   Style.QualifierAlignment = FormatStyle::QAS_Right;
19286   CHECK_PARSE("QualifierAlignment: Leave", QualifierAlignment,
19287               FormatStyle::QAS_Leave);
19288   CHECK_PARSE("QualifierAlignment: Right", QualifierAlignment,
19289               FormatStyle::QAS_Right);
19290   CHECK_PARSE("QualifierAlignment: Left", QualifierAlignment,
19291               FormatStyle::QAS_Left);
19292   CHECK_PARSE("QualifierAlignment: Custom", QualifierAlignment,
19293               FormatStyle::QAS_Custom);
19294 
19295   Style.QualifierOrder.clear();
19296   CHECK_PARSE("QualifierOrder: [ const, volatile, type ]", QualifierOrder,
19297               std::vector<std::string>({"const", "volatile", "type"}));
19298   Style.QualifierOrder.clear();
19299   CHECK_PARSE("QualifierOrder: [const, type]", QualifierOrder,
19300               std::vector<std::string>({"const", "type"}));
19301   Style.QualifierOrder.clear();
19302   CHECK_PARSE("QualifierOrder: [volatile, type]", QualifierOrder,
19303               std::vector<std::string>({"volatile", "type"}));
19304 
19305   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
19306   CHECK_PARSE("AlignConsecutiveAssignments: None", AlignConsecutiveAssignments,
19307               FormatStyle::ACS_None);
19308   CHECK_PARSE("AlignConsecutiveAssignments: Consecutive",
19309               AlignConsecutiveAssignments, FormatStyle::ACS_Consecutive);
19310   CHECK_PARSE("AlignConsecutiveAssignments: AcrossEmptyLines",
19311               AlignConsecutiveAssignments, FormatStyle::ACS_AcrossEmptyLines);
19312   CHECK_PARSE("AlignConsecutiveAssignments: AcrossEmptyLinesAndComments",
19313               AlignConsecutiveAssignments,
19314               FormatStyle::ACS_AcrossEmptyLinesAndComments);
19315   // For backwards compability, false / true should still parse
19316   CHECK_PARSE("AlignConsecutiveAssignments: false", AlignConsecutiveAssignments,
19317               FormatStyle::ACS_None);
19318   CHECK_PARSE("AlignConsecutiveAssignments: true", AlignConsecutiveAssignments,
19319               FormatStyle::ACS_Consecutive);
19320 
19321   Style.AlignConsecutiveBitFields = FormatStyle::ACS_Consecutive;
19322   CHECK_PARSE("AlignConsecutiveBitFields: None", AlignConsecutiveBitFields,
19323               FormatStyle::ACS_None);
19324   CHECK_PARSE("AlignConsecutiveBitFields: Consecutive",
19325               AlignConsecutiveBitFields, FormatStyle::ACS_Consecutive);
19326   CHECK_PARSE("AlignConsecutiveBitFields: AcrossEmptyLines",
19327               AlignConsecutiveBitFields, FormatStyle::ACS_AcrossEmptyLines);
19328   CHECK_PARSE("AlignConsecutiveBitFields: AcrossEmptyLinesAndComments",
19329               AlignConsecutiveBitFields,
19330               FormatStyle::ACS_AcrossEmptyLinesAndComments);
19331   // For backwards compability, false / true should still parse
19332   CHECK_PARSE("AlignConsecutiveBitFields: false", AlignConsecutiveBitFields,
19333               FormatStyle::ACS_None);
19334   CHECK_PARSE("AlignConsecutiveBitFields: true", AlignConsecutiveBitFields,
19335               FormatStyle::ACS_Consecutive);
19336 
19337   Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
19338   CHECK_PARSE("AlignConsecutiveMacros: None", AlignConsecutiveMacros,
19339               FormatStyle::ACS_None);
19340   CHECK_PARSE("AlignConsecutiveMacros: Consecutive", AlignConsecutiveMacros,
19341               FormatStyle::ACS_Consecutive);
19342   CHECK_PARSE("AlignConsecutiveMacros: AcrossEmptyLines",
19343               AlignConsecutiveMacros, FormatStyle::ACS_AcrossEmptyLines);
19344   CHECK_PARSE("AlignConsecutiveMacros: AcrossEmptyLinesAndComments",
19345               AlignConsecutiveMacros,
19346               FormatStyle::ACS_AcrossEmptyLinesAndComments);
19347   // For backwards compability, false / true should still parse
19348   CHECK_PARSE("AlignConsecutiveMacros: false", AlignConsecutiveMacros,
19349               FormatStyle::ACS_None);
19350   CHECK_PARSE("AlignConsecutiveMacros: true", AlignConsecutiveMacros,
19351               FormatStyle::ACS_Consecutive);
19352 
19353   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
19354   CHECK_PARSE("AlignConsecutiveDeclarations: None",
19355               AlignConsecutiveDeclarations, FormatStyle::ACS_None);
19356   CHECK_PARSE("AlignConsecutiveDeclarations: Consecutive",
19357               AlignConsecutiveDeclarations, FormatStyle::ACS_Consecutive);
19358   CHECK_PARSE("AlignConsecutiveDeclarations: AcrossEmptyLines",
19359               AlignConsecutiveDeclarations, FormatStyle::ACS_AcrossEmptyLines);
19360   CHECK_PARSE("AlignConsecutiveDeclarations: AcrossEmptyLinesAndComments",
19361               AlignConsecutiveDeclarations,
19362               FormatStyle::ACS_AcrossEmptyLinesAndComments);
19363   // For backwards compability, false / true should still parse
19364   CHECK_PARSE("AlignConsecutiveDeclarations: false",
19365               AlignConsecutiveDeclarations, FormatStyle::ACS_None);
19366   CHECK_PARSE("AlignConsecutiveDeclarations: true",
19367               AlignConsecutiveDeclarations, FormatStyle::ACS_Consecutive);
19368 
19369   Style.PointerAlignment = FormatStyle::PAS_Middle;
19370   CHECK_PARSE("PointerAlignment: Left", PointerAlignment,
19371               FormatStyle::PAS_Left);
19372   CHECK_PARSE("PointerAlignment: Right", PointerAlignment,
19373               FormatStyle::PAS_Right);
19374   CHECK_PARSE("PointerAlignment: Middle", PointerAlignment,
19375               FormatStyle::PAS_Middle);
19376   Style.ReferenceAlignment = FormatStyle::RAS_Middle;
19377   CHECK_PARSE("ReferenceAlignment: Pointer", ReferenceAlignment,
19378               FormatStyle::RAS_Pointer);
19379   CHECK_PARSE("ReferenceAlignment: Left", ReferenceAlignment,
19380               FormatStyle::RAS_Left);
19381   CHECK_PARSE("ReferenceAlignment: Right", ReferenceAlignment,
19382               FormatStyle::RAS_Right);
19383   CHECK_PARSE("ReferenceAlignment: Middle", ReferenceAlignment,
19384               FormatStyle::RAS_Middle);
19385   // For backward compatibility:
19386   CHECK_PARSE("PointerBindsToType: Left", PointerAlignment,
19387               FormatStyle::PAS_Left);
19388   CHECK_PARSE("PointerBindsToType: Right", PointerAlignment,
19389               FormatStyle::PAS_Right);
19390   CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment,
19391               FormatStyle::PAS_Middle);
19392 
19393   Style.Standard = FormatStyle::LS_Auto;
19394   CHECK_PARSE("Standard: c++03", Standard, FormatStyle::LS_Cpp03);
19395   CHECK_PARSE("Standard: c++11", Standard, FormatStyle::LS_Cpp11);
19396   CHECK_PARSE("Standard: c++14", Standard, FormatStyle::LS_Cpp14);
19397   CHECK_PARSE("Standard: c++17", Standard, FormatStyle::LS_Cpp17);
19398   CHECK_PARSE("Standard: c++20", Standard, FormatStyle::LS_Cpp20);
19399   CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
19400   CHECK_PARSE("Standard: Latest", Standard, FormatStyle::LS_Latest);
19401   // Legacy aliases:
19402   CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03);
19403   CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Latest);
19404   CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
19405   CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
19406 
19407   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
19408   CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment",
19409               BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment);
19410   CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators,
19411               FormatStyle::BOS_None);
19412   CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators,
19413               FormatStyle::BOS_All);
19414   // For backward compatibility:
19415   CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators,
19416               FormatStyle::BOS_None);
19417   CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators,
19418               FormatStyle::BOS_All);
19419 
19420   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
19421   CHECK_PARSE("BreakConstructorInitializers: BeforeComma",
19422               BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma);
19423   CHECK_PARSE("BreakConstructorInitializers: AfterColon",
19424               BreakConstructorInitializers, FormatStyle::BCIS_AfterColon);
19425   CHECK_PARSE("BreakConstructorInitializers: BeforeColon",
19426               BreakConstructorInitializers, FormatStyle::BCIS_BeforeColon);
19427   // For backward compatibility:
19428   CHECK_PARSE("BreakConstructorInitializersBeforeComma: true",
19429               BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma);
19430 
19431   Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
19432   CHECK_PARSE("BreakInheritanceList: AfterComma", BreakInheritanceList,
19433               FormatStyle::BILS_AfterComma);
19434   CHECK_PARSE("BreakInheritanceList: BeforeComma", BreakInheritanceList,
19435               FormatStyle::BILS_BeforeComma);
19436   CHECK_PARSE("BreakInheritanceList: AfterColon", BreakInheritanceList,
19437               FormatStyle::BILS_AfterColon);
19438   CHECK_PARSE("BreakInheritanceList: BeforeColon", BreakInheritanceList,
19439               FormatStyle::BILS_BeforeColon);
19440   // For backward compatibility:
19441   CHECK_PARSE("BreakBeforeInheritanceComma: true", BreakInheritanceList,
19442               FormatStyle::BILS_BeforeComma);
19443 
19444   Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack;
19445   CHECK_PARSE("PackConstructorInitializers: Never", PackConstructorInitializers,
19446               FormatStyle::PCIS_Never);
19447   CHECK_PARSE("PackConstructorInitializers: BinPack",
19448               PackConstructorInitializers, FormatStyle::PCIS_BinPack);
19449   CHECK_PARSE("PackConstructorInitializers: CurrentLine",
19450               PackConstructorInitializers, FormatStyle::PCIS_CurrentLine);
19451   CHECK_PARSE("PackConstructorInitializers: NextLine",
19452               PackConstructorInitializers, FormatStyle::PCIS_NextLine);
19453   // For backward compatibility:
19454   CHECK_PARSE("BasedOnStyle: Google\n"
19455               "ConstructorInitializerAllOnOneLineOrOnePerLine: true\n"
19456               "AllowAllConstructorInitializersOnNextLine: false",
19457               PackConstructorInitializers, FormatStyle::PCIS_CurrentLine);
19458   Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
19459   CHECK_PARSE("BasedOnStyle: Google\n"
19460               "ConstructorInitializerAllOnOneLineOrOnePerLine: false",
19461               PackConstructorInitializers, FormatStyle::PCIS_BinPack);
19462   CHECK_PARSE("ConstructorInitializerAllOnOneLineOrOnePerLine: true\n"
19463               "AllowAllConstructorInitializersOnNextLine: true",
19464               PackConstructorInitializers, FormatStyle::PCIS_NextLine);
19465   Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack;
19466   CHECK_PARSE("ConstructorInitializerAllOnOneLineOrOnePerLine: true\n"
19467               "AllowAllConstructorInitializersOnNextLine: false",
19468               PackConstructorInitializers, FormatStyle::PCIS_CurrentLine);
19469 
19470   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
19471   CHECK_PARSE("EmptyLineBeforeAccessModifier: Never",
19472               EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Never);
19473   CHECK_PARSE("EmptyLineBeforeAccessModifier: Leave",
19474               EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Leave);
19475   CHECK_PARSE("EmptyLineBeforeAccessModifier: LogicalBlock",
19476               EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_LogicalBlock);
19477   CHECK_PARSE("EmptyLineBeforeAccessModifier: Always",
19478               EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Always);
19479 
19480   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
19481   CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket,
19482               FormatStyle::BAS_Align);
19483   CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket,
19484               FormatStyle::BAS_DontAlign);
19485   CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket,
19486               FormatStyle::BAS_AlwaysBreak);
19487   CHECK_PARSE("AlignAfterOpenBracket: BlockIndent", AlignAfterOpenBracket,
19488               FormatStyle::BAS_BlockIndent);
19489   // For backward compatibility:
19490   CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket,
19491               FormatStyle::BAS_DontAlign);
19492   CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket,
19493               FormatStyle::BAS_Align);
19494 
19495   Style.AlignEscapedNewlines = FormatStyle::ENAS_Left;
19496   CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines,
19497               FormatStyle::ENAS_DontAlign);
19498   CHECK_PARSE("AlignEscapedNewlines: Left", AlignEscapedNewlines,
19499               FormatStyle::ENAS_Left);
19500   CHECK_PARSE("AlignEscapedNewlines: Right", AlignEscapedNewlines,
19501               FormatStyle::ENAS_Right);
19502   // For backward compatibility:
19503   CHECK_PARSE("AlignEscapedNewlinesLeft: true", AlignEscapedNewlines,
19504               FormatStyle::ENAS_Left);
19505   CHECK_PARSE("AlignEscapedNewlinesLeft: false", AlignEscapedNewlines,
19506               FormatStyle::ENAS_Right);
19507 
19508   Style.AlignOperands = FormatStyle::OAS_Align;
19509   CHECK_PARSE("AlignOperands: DontAlign", AlignOperands,
19510               FormatStyle::OAS_DontAlign);
19511   CHECK_PARSE("AlignOperands: Align", AlignOperands, FormatStyle::OAS_Align);
19512   CHECK_PARSE("AlignOperands: AlignAfterOperator", AlignOperands,
19513               FormatStyle::OAS_AlignAfterOperator);
19514   // For backward compatibility:
19515   CHECK_PARSE("AlignOperands: false", AlignOperands,
19516               FormatStyle::OAS_DontAlign);
19517   CHECK_PARSE("AlignOperands: true", AlignOperands, FormatStyle::OAS_Align);
19518 
19519   Style.UseTab = FormatStyle::UT_ForIndentation;
19520   CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
19521   CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
19522   CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
19523   CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab,
19524               FormatStyle::UT_ForContinuationAndIndentation);
19525   CHECK_PARSE("UseTab: AlignWithSpaces", UseTab,
19526               FormatStyle::UT_AlignWithSpaces);
19527   // For backward compatibility:
19528   CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never);
19529   CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always);
19530 
19531   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty;
19532   CHECK_PARSE("AllowShortBlocksOnASingleLine: Never",
19533               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never);
19534   CHECK_PARSE("AllowShortBlocksOnASingleLine: Empty",
19535               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Empty);
19536   CHECK_PARSE("AllowShortBlocksOnASingleLine: Always",
19537               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always);
19538   // For backward compatibility:
19539   CHECK_PARSE("AllowShortBlocksOnASingleLine: false",
19540               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never);
19541   CHECK_PARSE("AllowShortBlocksOnASingleLine: true",
19542               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always);
19543 
19544   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
19545   CHECK_PARSE("AllowShortFunctionsOnASingleLine: None",
19546               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
19547   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline",
19548               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline);
19549   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty",
19550               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty);
19551   CHECK_PARSE("AllowShortFunctionsOnASingleLine: All",
19552               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
19553   // For backward compatibility:
19554   CHECK_PARSE("AllowShortFunctionsOnASingleLine: false",
19555               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
19556   CHECK_PARSE("AllowShortFunctionsOnASingleLine: true",
19557               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
19558 
19559   Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Both;
19560   CHECK_PARSE("SpaceAroundPointerQualifiers: Default",
19561               SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Default);
19562   CHECK_PARSE("SpaceAroundPointerQualifiers: Before",
19563               SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Before);
19564   CHECK_PARSE("SpaceAroundPointerQualifiers: After",
19565               SpaceAroundPointerQualifiers, FormatStyle::SAPQ_After);
19566   CHECK_PARSE("SpaceAroundPointerQualifiers: Both",
19567               SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Both);
19568 
19569   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
19570   CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens,
19571               FormatStyle::SBPO_Never);
19572   CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens,
19573               FormatStyle::SBPO_Always);
19574   CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens,
19575               FormatStyle::SBPO_ControlStatements);
19576   CHECK_PARSE("SpaceBeforeParens: ControlStatementsExceptControlMacros",
19577               SpaceBeforeParens,
19578               FormatStyle::SBPO_ControlStatementsExceptControlMacros);
19579   CHECK_PARSE("SpaceBeforeParens: NonEmptyParentheses", SpaceBeforeParens,
19580               FormatStyle::SBPO_NonEmptyParentheses);
19581   CHECK_PARSE("SpaceBeforeParens: Custom", SpaceBeforeParens,
19582               FormatStyle::SBPO_Custom);
19583   // For backward compatibility:
19584   CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens,
19585               FormatStyle::SBPO_Never);
19586   CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens,
19587               FormatStyle::SBPO_ControlStatements);
19588   CHECK_PARSE("SpaceBeforeParens: ControlStatementsExceptForEachMacros",
19589               SpaceBeforeParens,
19590               FormatStyle::SBPO_ControlStatementsExceptControlMacros);
19591 
19592   Style.ColumnLimit = 123;
19593   FormatStyle BaseStyle = getLLVMStyle();
19594   CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
19595   CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
19596 
19597   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
19598   CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
19599               FormatStyle::BS_Attach);
19600   CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
19601               FormatStyle::BS_Linux);
19602   CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces,
19603               FormatStyle::BS_Mozilla);
19604   CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
19605               FormatStyle::BS_Stroustrup);
19606   CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces,
19607               FormatStyle::BS_Allman);
19608   CHECK_PARSE("BreakBeforeBraces: Whitesmiths", BreakBeforeBraces,
19609               FormatStyle::BS_Whitesmiths);
19610   CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU);
19611   CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces,
19612               FormatStyle::BS_WebKit);
19613   CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces,
19614               FormatStyle::BS_Custom);
19615 
19616   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never;
19617   CHECK_PARSE("BraceWrapping:\n"
19618               "  AfterControlStatement: MultiLine",
19619               BraceWrapping.AfterControlStatement,
19620               FormatStyle::BWACS_MultiLine);
19621   CHECK_PARSE("BraceWrapping:\n"
19622               "  AfterControlStatement: Always",
19623               BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always);
19624   CHECK_PARSE("BraceWrapping:\n"
19625               "  AfterControlStatement: Never",
19626               BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never);
19627   // For backward compatibility:
19628   CHECK_PARSE("BraceWrapping:\n"
19629               "  AfterControlStatement: true",
19630               BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always);
19631   CHECK_PARSE("BraceWrapping:\n"
19632               "  AfterControlStatement: false",
19633               BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never);
19634 
19635   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
19636   CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType,
19637               FormatStyle::RTBS_None);
19638   CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType,
19639               FormatStyle::RTBS_All);
19640   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel",
19641               AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel);
19642   CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions",
19643               AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions);
19644   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions",
19645               AlwaysBreakAfterReturnType,
19646               FormatStyle::RTBS_TopLevelDefinitions);
19647 
19648   Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
19649   CHECK_PARSE("AlwaysBreakTemplateDeclarations: No",
19650               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_No);
19651   CHECK_PARSE("AlwaysBreakTemplateDeclarations: MultiLine",
19652               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine);
19653   CHECK_PARSE("AlwaysBreakTemplateDeclarations: Yes",
19654               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes);
19655   CHECK_PARSE("AlwaysBreakTemplateDeclarations: false",
19656               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine);
19657   CHECK_PARSE("AlwaysBreakTemplateDeclarations: true",
19658               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes);
19659 
19660   Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
19661   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None",
19662               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None);
19663   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All",
19664               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All);
19665   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel",
19666               AlwaysBreakAfterDefinitionReturnType,
19667               FormatStyle::DRTBS_TopLevel);
19668 
19669   Style.NamespaceIndentation = FormatStyle::NI_All;
19670   CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
19671               FormatStyle::NI_None);
19672   CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation,
19673               FormatStyle::NI_Inner);
19674   CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
19675               FormatStyle::NI_All);
19676 
19677   Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_OnlyFirstIf;
19678   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Never",
19679               AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never);
19680   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: WithoutElse",
19681               AllowShortIfStatementsOnASingleLine,
19682               FormatStyle::SIS_WithoutElse);
19683   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: OnlyFirstIf",
19684               AllowShortIfStatementsOnASingleLine,
19685               FormatStyle::SIS_OnlyFirstIf);
19686   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: AllIfsAndElse",
19687               AllowShortIfStatementsOnASingleLine,
19688               FormatStyle::SIS_AllIfsAndElse);
19689   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Always",
19690               AllowShortIfStatementsOnASingleLine,
19691               FormatStyle::SIS_OnlyFirstIf);
19692   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: false",
19693               AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never);
19694   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: true",
19695               AllowShortIfStatementsOnASingleLine,
19696               FormatStyle::SIS_WithoutElse);
19697 
19698   Style.IndentExternBlock = FormatStyle::IEBS_NoIndent;
19699   CHECK_PARSE("IndentExternBlock: AfterExternBlock", IndentExternBlock,
19700               FormatStyle::IEBS_AfterExternBlock);
19701   CHECK_PARSE("IndentExternBlock: Indent", IndentExternBlock,
19702               FormatStyle::IEBS_Indent);
19703   CHECK_PARSE("IndentExternBlock: NoIndent", IndentExternBlock,
19704               FormatStyle::IEBS_NoIndent);
19705   CHECK_PARSE("IndentExternBlock: true", IndentExternBlock,
19706               FormatStyle::IEBS_Indent);
19707   CHECK_PARSE("IndentExternBlock: false", IndentExternBlock,
19708               FormatStyle::IEBS_NoIndent);
19709 
19710   Style.BitFieldColonSpacing = FormatStyle::BFCS_None;
19711   CHECK_PARSE("BitFieldColonSpacing: Both", BitFieldColonSpacing,
19712               FormatStyle::BFCS_Both);
19713   CHECK_PARSE("BitFieldColonSpacing: None", BitFieldColonSpacing,
19714               FormatStyle::BFCS_None);
19715   CHECK_PARSE("BitFieldColonSpacing: Before", BitFieldColonSpacing,
19716               FormatStyle::BFCS_Before);
19717   CHECK_PARSE("BitFieldColonSpacing: After", BitFieldColonSpacing,
19718               FormatStyle::BFCS_After);
19719 
19720   Style.SortJavaStaticImport = FormatStyle::SJSIO_Before;
19721   CHECK_PARSE("SortJavaStaticImport: After", SortJavaStaticImport,
19722               FormatStyle::SJSIO_After);
19723   CHECK_PARSE("SortJavaStaticImport: Before", SortJavaStaticImport,
19724               FormatStyle::SJSIO_Before);
19725 
19726   // FIXME: This is required because parsing a configuration simply overwrites
19727   // the first N elements of the list instead of resetting it.
19728   Style.ForEachMacros.clear();
19729   std::vector<std::string> BoostForeach;
19730   BoostForeach.push_back("BOOST_FOREACH");
19731   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach);
19732   std::vector<std::string> BoostAndQForeach;
19733   BoostAndQForeach.push_back("BOOST_FOREACH");
19734   BoostAndQForeach.push_back("Q_FOREACH");
19735   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
19736               BoostAndQForeach);
19737 
19738   Style.IfMacros.clear();
19739   std::vector<std::string> CustomIfs;
19740   CustomIfs.push_back("MYIF");
19741   CHECK_PARSE("IfMacros: [MYIF]", IfMacros, CustomIfs);
19742 
19743   Style.AttributeMacros.clear();
19744   CHECK_PARSE("BasedOnStyle: LLVM", AttributeMacros,
19745               std::vector<std::string>{"__capability"});
19746   CHECK_PARSE("AttributeMacros: [attr1, attr2]", AttributeMacros,
19747               std::vector<std::string>({"attr1", "attr2"}));
19748 
19749   Style.StatementAttributeLikeMacros.clear();
19750   CHECK_PARSE("StatementAttributeLikeMacros: [emit,Q_EMIT]",
19751               StatementAttributeLikeMacros,
19752               std::vector<std::string>({"emit", "Q_EMIT"}));
19753 
19754   Style.StatementMacros.clear();
19755   CHECK_PARSE("StatementMacros: [QUNUSED]", StatementMacros,
19756               std::vector<std::string>{"QUNUSED"});
19757   CHECK_PARSE("StatementMacros: [QUNUSED, QT_REQUIRE_VERSION]", StatementMacros,
19758               std::vector<std::string>({"QUNUSED", "QT_REQUIRE_VERSION"}));
19759 
19760   Style.NamespaceMacros.clear();
19761   CHECK_PARSE("NamespaceMacros: [TESTSUITE]", NamespaceMacros,
19762               std::vector<std::string>{"TESTSUITE"});
19763   CHECK_PARSE("NamespaceMacros: [TESTSUITE, SUITE]", NamespaceMacros,
19764               std::vector<std::string>({"TESTSUITE", "SUITE"}));
19765 
19766   Style.WhitespaceSensitiveMacros.clear();
19767   CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE]",
19768               WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"});
19769   CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE, ASSERT]",
19770               WhitespaceSensitiveMacros,
19771               std::vector<std::string>({"STRINGIZE", "ASSERT"}));
19772   Style.WhitespaceSensitiveMacros.clear();
19773   CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE']",
19774               WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"});
19775   CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE', 'ASSERT']",
19776               WhitespaceSensitiveMacros,
19777               std::vector<std::string>({"STRINGIZE", "ASSERT"}));
19778 
19779   Style.IncludeStyle.IncludeCategories.clear();
19780   std::vector<tooling::IncludeStyle::IncludeCategory> ExpectedCategories = {
19781       {"abc/.*", 2, 0, false}, {".*", 1, 0, true}};
19782   CHECK_PARSE("IncludeCategories:\n"
19783               "  - Regex: abc/.*\n"
19784               "    Priority: 2\n"
19785               "  - Regex: .*\n"
19786               "    Priority: 1\n"
19787               "    CaseSensitive: true\n",
19788               IncludeStyle.IncludeCategories, ExpectedCategories);
19789   CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeStyle.IncludeIsMainRegex,
19790               "abc$");
19791   CHECK_PARSE("IncludeIsMainSourceRegex: 'abc$'",
19792               IncludeStyle.IncludeIsMainSourceRegex, "abc$");
19793 
19794   Style.SortIncludes = FormatStyle::SI_Never;
19795   CHECK_PARSE("SortIncludes: true", SortIncludes,
19796               FormatStyle::SI_CaseSensitive);
19797   CHECK_PARSE("SortIncludes: false", SortIncludes, FormatStyle::SI_Never);
19798   CHECK_PARSE("SortIncludes: CaseInsensitive", SortIncludes,
19799               FormatStyle::SI_CaseInsensitive);
19800   CHECK_PARSE("SortIncludes: CaseSensitive", SortIncludes,
19801               FormatStyle::SI_CaseSensitive);
19802   CHECK_PARSE("SortIncludes: Never", SortIncludes, FormatStyle::SI_Never);
19803 
19804   Style.RawStringFormats.clear();
19805   std::vector<FormatStyle::RawStringFormat> ExpectedRawStringFormats = {
19806       {
19807           FormatStyle::LK_TextProto,
19808           {"pb", "proto"},
19809           {"PARSE_TEXT_PROTO"},
19810           /*CanonicalDelimiter=*/"",
19811           "llvm",
19812       },
19813       {
19814           FormatStyle::LK_Cpp,
19815           {"cc", "cpp"},
19816           {"C_CODEBLOCK", "CPPEVAL"},
19817           /*CanonicalDelimiter=*/"cc",
19818           /*BasedOnStyle=*/"",
19819       },
19820   };
19821 
19822   CHECK_PARSE("RawStringFormats:\n"
19823               "  - Language: TextProto\n"
19824               "    Delimiters:\n"
19825               "      - 'pb'\n"
19826               "      - 'proto'\n"
19827               "    EnclosingFunctions:\n"
19828               "      - 'PARSE_TEXT_PROTO'\n"
19829               "    BasedOnStyle: llvm\n"
19830               "  - Language: Cpp\n"
19831               "    Delimiters:\n"
19832               "      - 'cc'\n"
19833               "      - 'cpp'\n"
19834               "    EnclosingFunctions:\n"
19835               "      - 'C_CODEBLOCK'\n"
19836               "      - 'CPPEVAL'\n"
19837               "    CanonicalDelimiter: 'cc'",
19838               RawStringFormats, ExpectedRawStringFormats);
19839 
19840   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
19841               "  Minimum: 0\n"
19842               "  Maximum: 0",
19843               SpacesInLineCommentPrefix.Minimum, 0u);
19844   EXPECT_EQ(Style.SpacesInLineCommentPrefix.Maximum, 0u);
19845   Style.SpacesInLineCommentPrefix.Minimum = 1;
19846   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
19847               "  Minimum: 2",
19848               SpacesInLineCommentPrefix.Minimum, 0u);
19849   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
19850               "  Maximum: -1",
19851               SpacesInLineCommentPrefix.Maximum, -1u);
19852   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
19853               "  Minimum: 2",
19854               SpacesInLineCommentPrefix.Minimum, 2u);
19855   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
19856               "  Maximum: 1",
19857               SpacesInLineCommentPrefix.Maximum, 1u);
19858   EXPECT_EQ(Style.SpacesInLineCommentPrefix.Minimum, 1u);
19859 
19860   Style.SpacesInAngles = FormatStyle::SIAS_Always;
19861   CHECK_PARSE("SpacesInAngles: Never", SpacesInAngles, FormatStyle::SIAS_Never);
19862   CHECK_PARSE("SpacesInAngles: Always", SpacesInAngles,
19863               FormatStyle::SIAS_Always);
19864   CHECK_PARSE("SpacesInAngles: Leave", SpacesInAngles, FormatStyle::SIAS_Leave);
19865   // For backward compatibility:
19866   CHECK_PARSE("SpacesInAngles: false", SpacesInAngles, FormatStyle::SIAS_Never);
19867   CHECK_PARSE("SpacesInAngles: true", SpacesInAngles, FormatStyle::SIAS_Always);
19868 }
19869 
19870 TEST_F(FormatTest, ParsesConfigurationWithLanguages) {
19871   FormatStyle Style = {};
19872   Style.Language = FormatStyle::LK_Cpp;
19873   CHECK_PARSE("Language: Cpp\n"
19874               "IndentWidth: 12",
19875               IndentWidth, 12u);
19876   EXPECT_EQ(parseConfiguration("Language: JavaScript\n"
19877                                "IndentWidth: 34",
19878                                &Style),
19879             ParseError::Unsuitable);
19880   FormatStyle BinPackedTCS = {};
19881   BinPackedTCS.Language = FormatStyle::LK_JavaScript;
19882   EXPECT_EQ(parseConfiguration("BinPackArguments: true\n"
19883                                "InsertTrailingCommas: Wrapped",
19884                                &BinPackedTCS),
19885             ParseError::BinPackTrailingCommaConflict);
19886   EXPECT_EQ(12u, Style.IndentWidth);
19887   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
19888   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
19889 
19890   Style.Language = FormatStyle::LK_JavaScript;
19891   CHECK_PARSE("Language: JavaScript\n"
19892               "IndentWidth: 12",
19893               IndentWidth, 12u);
19894   CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u);
19895   EXPECT_EQ(parseConfiguration("Language: Cpp\n"
19896                                "IndentWidth: 34",
19897                                &Style),
19898             ParseError::Unsuitable);
19899   EXPECT_EQ(23u, Style.IndentWidth);
19900   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
19901   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
19902 
19903   CHECK_PARSE("BasedOnStyle: LLVM\n"
19904               "IndentWidth: 67",
19905               IndentWidth, 67u);
19906 
19907   CHECK_PARSE("---\n"
19908               "Language: JavaScript\n"
19909               "IndentWidth: 12\n"
19910               "---\n"
19911               "Language: Cpp\n"
19912               "IndentWidth: 34\n"
19913               "...\n",
19914               IndentWidth, 12u);
19915 
19916   Style.Language = FormatStyle::LK_Cpp;
19917   CHECK_PARSE("---\n"
19918               "Language: JavaScript\n"
19919               "IndentWidth: 12\n"
19920               "---\n"
19921               "Language: Cpp\n"
19922               "IndentWidth: 34\n"
19923               "...\n",
19924               IndentWidth, 34u);
19925   CHECK_PARSE("---\n"
19926               "IndentWidth: 78\n"
19927               "---\n"
19928               "Language: JavaScript\n"
19929               "IndentWidth: 56\n"
19930               "...\n",
19931               IndentWidth, 78u);
19932 
19933   Style.ColumnLimit = 123;
19934   Style.IndentWidth = 234;
19935   Style.BreakBeforeBraces = FormatStyle::BS_Linux;
19936   Style.TabWidth = 345;
19937   EXPECT_FALSE(parseConfiguration("---\n"
19938                                   "IndentWidth: 456\n"
19939                                   "BreakBeforeBraces: Allman\n"
19940                                   "---\n"
19941                                   "Language: JavaScript\n"
19942                                   "IndentWidth: 111\n"
19943                                   "TabWidth: 111\n"
19944                                   "---\n"
19945                                   "Language: Cpp\n"
19946                                   "BreakBeforeBraces: Stroustrup\n"
19947                                   "TabWidth: 789\n"
19948                                   "...\n",
19949                                   &Style));
19950   EXPECT_EQ(123u, Style.ColumnLimit);
19951   EXPECT_EQ(456u, Style.IndentWidth);
19952   EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces);
19953   EXPECT_EQ(789u, Style.TabWidth);
19954 
19955   EXPECT_EQ(parseConfiguration("---\n"
19956                                "Language: JavaScript\n"
19957                                "IndentWidth: 56\n"
19958                                "---\n"
19959                                "IndentWidth: 78\n"
19960                                "...\n",
19961                                &Style),
19962             ParseError::Error);
19963   EXPECT_EQ(parseConfiguration("---\n"
19964                                "Language: JavaScript\n"
19965                                "IndentWidth: 56\n"
19966                                "---\n"
19967                                "Language: JavaScript\n"
19968                                "IndentWidth: 78\n"
19969                                "...\n",
19970                                &Style),
19971             ParseError::Error);
19972 
19973   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
19974 }
19975 
19976 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) {
19977   FormatStyle Style = {};
19978   Style.Language = FormatStyle::LK_JavaScript;
19979   Style.BreakBeforeTernaryOperators = true;
19980   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value());
19981   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
19982 
19983   Style.BreakBeforeTernaryOperators = true;
19984   EXPECT_EQ(0, parseConfiguration("---\n"
19985                                   "BasedOnStyle: Google\n"
19986                                   "---\n"
19987                                   "Language: JavaScript\n"
19988                                   "IndentWidth: 76\n"
19989                                   "...\n",
19990                                   &Style)
19991                    .value());
19992   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
19993   EXPECT_EQ(76u, Style.IndentWidth);
19994   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
19995 }
19996 
19997 TEST_F(FormatTest, ConfigurationRoundTripTest) {
19998   FormatStyle Style = getLLVMStyle();
19999   std::string YAML = configurationAsText(Style);
20000   FormatStyle ParsedStyle = {};
20001   ParsedStyle.Language = FormatStyle::LK_Cpp;
20002   EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
20003   EXPECT_EQ(Style, ParsedStyle);
20004 }
20005 
20006 TEST_F(FormatTest, WorksFor8bitEncodings) {
20007   EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
20008             "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
20009             "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
20010             "\"\xef\xee\xf0\xf3...\"",
20011             format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
20012                    "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
20013                    "\xef\xee\xf0\xf3...\"",
20014                    getLLVMStyleWithColumns(12)));
20015 }
20016 
20017 TEST_F(FormatTest, HandlesUTF8BOM) {
20018   EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf"));
20019   EXPECT_EQ("\xef\xbb\xbf#include <iostream>",
20020             format("\xef\xbb\xbf#include <iostream>"));
20021   EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>",
20022             format("\xef\xbb\xbf\n#include <iostream>"));
20023 }
20024 
20025 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
20026 #if !defined(_MSC_VER)
20027 
20028 TEST_F(FormatTest, CountsUTF8CharactersProperly) {
20029   verifyFormat("\"Однажды в студёную зимнюю пору...\"",
20030                getLLVMStyleWithColumns(35));
20031   verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
20032                getLLVMStyleWithColumns(31));
20033   verifyFormat("// Однажды в студёную зимнюю пору...",
20034                getLLVMStyleWithColumns(36));
20035   verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32));
20036   verifyFormat("/* Однажды в студёную зимнюю пору... */",
20037                getLLVMStyleWithColumns(39));
20038   verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
20039                getLLVMStyleWithColumns(35));
20040 }
20041 
20042 TEST_F(FormatTest, SplitsUTF8Strings) {
20043   // Non-printable characters' width is currently considered to be the length in
20044   // bytes in UTF8. The characters can be displayed in very different manner
20045   // (zero-width, single width with a substitution glyph, expanded to their code
20046   // (e.g. "<8d>"), so there's no single correct way to handle them.
20047   EXPECT_EQ("\"aaaaÄ\"\n"
20048             "\"\xc2\x8d\";",
20049             format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
20050   EXPECT_EQ("\"aaaaaaaÄ\"\n"
20051             "\"\xc2\x8d\";",
20052             format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
20053   EXPECT_EQ("\"Однажды, в \"\n"
20054             "\"студёную \"\n"
20055             "\"зимнюю \"\n"
20056             "\"пору,\"",
20057             format("\"Однажды, в студёную зимнюю пору,\"",
20058                    getLLVMStyleWithColumns(13)));
20059   EXPECT_EQ(
20060       "\"一 二 三 \"\n"
20061       "\"四 五六 \"\n"
20062       "\"七 八 九 \"\n"
20063       "\"十\"",
20064       format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11)));
20065   EXPECT_EQ("\"一\t\"\n"
20066             "\"二 \t\"\n"
20067             "\"三 四 \"\n"
20068             "\"五\t\"\n"
20069             "\"六 \t\"\n"
20070             "\"七 \"\n"
20071             "\"八九十\tqq\"",
20072             format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
20073                    getLLVMStyleWithColumns(11)));
20074 
20075   // UTF8 character in an escape sequence.
20076   EXPECT_EQ("\"aaaaaa\"\n"
20077             "\"\\\xC2\x8D\"",
20078             format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10)));
20079 }
20080 
20081 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
20082   EXPECT_EQ("const char *sssss =\n"
20083             "    \"一二三四五六七八\\\n"
20084             " 九 十\";",
20085             format("const char *sssss = \"一二三四五六七八\\\n"
20086                    " 九 十\";",
20087                    getLLVMStyleWithColumns(30)));
20088 }
20089 
20090 TEST_F(FormatTest, SplitsUTF8LineComments) {
20091   EXPECT_EQ("// aaaaÄ\xc2\x8d",
20092             format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10)));
20093   EXPECT_EQ("// Я из лесу\n"
20094             "// вышел; был\n"
20095             "// сильный\n"
20096             "// мороз.",
20097             format("// Я из лесу вышел; был сильный мороз.",
20098                    getLLVMStyleWithColumns(13)));
20099   EXPECT_EQ("// 一二三\n"
20100             "// 四五六七\n"
20101             "// 八  九\n"
20102             "// 十",
20103             format("// 一二三 四五六七 八  九 十", getLLVMStyleWithColumns(9)));
20104 }
20105 
20106 TEST_F(FormatTest, SplitsUTF8BlockComments) {
20107   EXPECT_EQ("/* Гляжу,\n"
20108             " * поднимается\n"
20109             " * медленно в\n"
20110             " * гору\n"
20111             " * Лошадка,\n"
20112             " * везущая\n"
20113             " * хворосту\n"
20114             " * воз. */",
20115             format("/* Гляжу, поднимается медленно в гору\n"
20116                    " * Лошадка, везущая хворосту воз. */",
20117                    getLLVMStyleWithColumns(13)));
20118   EXPECT_EQ(
20119       "/* 一二三\n"
20120       " * 四五六七\n"
20121       " * 八  九\n"
20122       " * 十  */",
20123       format("/* 一二三 四五六七 八  九 十  */", getLLVMStyleWithColumns(9)));
20124   EXPECT_EQ("/* �������� ��������\n"
20125             " * ��������\n"
20126             " * ������-�� */",
20127             format("/* �������� �������� �������� ������-�� */", getLLVMStyleWithColumns(12)));
20128 }
20129 
20130 #endif // _MSC_VER
20131 
20132 TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
20133   FormatStyle Style = getLLVMStyle();
20134 
20135   Style.ConstructorInitializerIndentWidth = 4;
20136   verifyFormat(
20137       "SomeClass::Constructor()\n"
20138       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
20139       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
20140       Style);
20141 
20142   Style.ConstructorInitializerIndentWidth = 2;
20143   verifyFormat(
20144       "SomeClass::Constructor()\n"
20145       "  : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
20146       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
20147       Style);
20148 
20149   Style.ConstructorInitializerIndentWidth = 0;
20150   verifyFormat(
20151       "SomeClass::Constructor()\n"
20152       ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
20153       "  aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
20154       Style);
20155   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
20156   verifyFormat(
20157       "SomeLongTemplateVariableName<\n"
20158       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>",
20159       Style);
20160   verifyFormat("bool smaller = 1 < "
20161                "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
20162                "                       "
20163                "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
20164                Style);
20165 
20166   Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
20167   verifyFormat("SomeClass::Constructor() :\n"
20168                "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa),\n"
20169                "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa) {}",
20170                Style);
20171 }
20172 
20173 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {
20174   FormatStyle Style = getLLVMStyle();
20175   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
20176   Style.ConstructorInitializerIndentWidth = 4;
20177   verifyFormat("SomeClass::Constructor()\n"
20178                "    : a(a)\n"
20179                "    , b(b)\n"
20180                "    , c(c) {}",
20181                Style);
20182   verifyFormat("SomeClass::Constructor()\n"
20183                "    : a(a) {}",
20184                Style);
20185 
20186   Style.ColumnLimit = 0;
20187   verifyFormat("SomeClass::Constructor()\n"
20188                "    : a(a) {}",
20189                Style);
20190   verifyFormat("SomeClass::Constructor() noexcept\n"
20191                "    : a(a) {}",
20192                Style);
20193   verifyFormat("SomeClass::Constructor()\n"
20194                "    : a(a)\n"
20195                "    , b(b)\n"
20196                "    , c(c) {}",
20197                Style);
20198   verifyFormat("SomeClass::Constructor()\n"
20199                "    : a(a) {\n"
20200                "  foo();\n"
20201                "  bar();\n"
20202                "}",
20203                Style);
20204 
20205   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
20206   verifyFormat("SomeClass::Constructor()\n"
20207                "    : a(a)\n"
20208                "    , b(b)\n"
20209                "    , c(c) {\n}",
20210                Style);
20211   verifyFormat("SomeClass::Constructor()\n"
20212                "    : a(a) {\n}",
20213                Style);
20214 
20215   Style.ColumnLimit = 80;
20216   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
20217   Style.ConstructorInitializerIndentWidth = 2;
20218   verifyFormat("SomeClass::Constructor()\n"
20219                "  : a(a)\n"
20220                "  , b(b)\n"
20221                "  , c(c) {}",
20222                Style);
20223 
20224   Style.ConstructorInitializerIndentWidth = 0;
20225   verifyFormat("SomeClass::Constructor()\n"
20226                ": a(a)\n"
20227                ", b(b)\n"
20228                ", c(c) {}",
20229                Style);
20230 
20231   Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
20232   Style.ConstructorInitializerIndentWidth = 4;
20233   verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
20234   verifyFormat(
20235       "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n",
20236       Style);
20237   verifyFormat(
20238       "SomeClass::Constructor()\n"
20239       "    : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
20240       Style);
20241   Style.ConstructorInitializerIndentWidth = 4;
20242   Style.ColumnLimit = 60;
20243   verifyFormat("SomeClass::Constructor()\n"
20244                "    : aaaaaaaa(aaaaaaaa)\n"
20245                "    , aaaaaaaa(aaaaaaaa)\n"
20246                "    , aaaaaaaa(aaaaaaaa) {}",
20247                Style);
20248 }
20249 
20250 TEST_F(FormatTest, ConstructorInitializersWithPreprocessorDirective) {
20251   FormatStyle Style = getLLVMStyle();
20252   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
20253   Style.ConstructorInitializerIndentWidth = 4;
20254   verifyFormat("SomeClass::Constructor()\n"
20255                "    : a{a}\n"
20256                "    , b{b} {}",
20257                Style);
20258   verifyFormat("SomeClass::Constructor()\n"
20259                "    : a{a}\n"
20260                "#if CONDITION\n"
20261                "    , b{b}\n"
20262                "#endif\n"
20263                "{\n}",
20264                Style);
20265   Style.ConstructorInitializerIndentWidth = 2;
20266   verifyFormat("SomeClass::Constructor()\n"
20267                "#if CONDITION\n"
20268                "  : a{a}\n"
20269                "#endif\n"
20270                "  , b{b}\n"
20271                "  , c{c} {\n}",
20272                Style);
20273   Style.ConstructorInitializerIndentWidth = 0;
20274   verifyFormat("SomeClass::Constructor()\n"
20275                ": a{a}\n"
20276                "#ifdef CONDITION\n"
20277                ", b{b}\n"
20278                "#else\n"
20279                ", c{c}\n"
20280                "#endif\n"
20281                ", d{d} {\n}",
20282                Style);
20283   Style.ConstructorInitializerIndentWidth = 4;
20284   verifyFormat("SomeClass::Constructor()\n"
20285                "    : a{a}\n"
20286                "#if WINDOWS\n"
20287                "#if DEBUG\n"
20288                "    , b{0}\n"
20289                "#else\n"
20290                "    , b{1}\n"
20291                "#endif\n"
20292                "#else\n"
20293                "#if DEBUG\n"
20294                "    , b{2}\n"
20295                "#else\n"
20296                "    , b{3}\n"
20297                "#endif\n"
20298                "#endif\n"
20299                "{\n}",
20300                Style);
20301   verifyFormat("SomeClass::Constructor()\n"
20302                "    : a{a}\n"
20303                "#if WINDOWS\n"
20304                "    , b{0}\n"
20305                "#if DEBUG\n"
20306                "    , c{0}\n"
20307                "#else\n"
20308                "    , c{1}\n"
20309                "#endif\n"
20310                "#else\n"
20311                "#if DEBUG\n"
20312                "    , c{2}\n"
20313                "#else\n"
20314                "    , c{3}\n"
20315                "#endif\n"
20316                "    , b{1}\n"
20317                "#endif\n"
20318                "{\n}",
20319                Style);
20320 }
20321 
20322 TEST_F(FormatTest, Destructors) {
20323   verifyFormat("void F(int &i) { i.~int(); }");
20324   verifyFormat("void F(int &i) { i->~int(); }");
20325 }
20326 
20327 TEST_F(FormatTest, FormatsWithWebKitStyle) {
20328   FormatStyle Style = getWebKitStyle();
20329 
20330   // Don't indent in outer namespaces.
20331   verifyFormat("namespace outer {\n"
20332                "int i;\n"
20333                "namespace inner {\n"
20334                "    int i;\n"
20335                "} // namespace inner\n"
20336                "} // namespace outer\n"
20337                "namespace other_outer {\n"
20338                "int i;\n"
20339                "}",
20340                Style);
20341 
20342   // Don't indent case labels.
20343   verifyFormat("switch (variable) {\n"
20344                "case 1:\n"
20345                "case 2:\n"
20346                "    doSomething();\n"
20347                "    break;\n"
20348                "default:\n"
20349                "    ++variable;\n"
20350                "}",
20351                Style);
20352 
20353   // Wrap before binary operators.
20354   EXPECT_EQ("void f()\n"
20355             "{\n"
20356             "    if (aaaaaaaaaaaaaaaa\n"
20357             "        && bbbbbbbbbbbbbbbbbbbbbbbb\n"
20358             "        && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
20359             "        return;\n"
20360             "}",
20361             format("void f() {\n"
20362                    "if (aaaaaaaaaaaaaaaa\n"
20363                    "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
20364                    "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
20365                    "return;\n"
20366                    "}",
20367                    Style));
20368 
20369   // Allow functions on a single line.
20370   verifyFormat("void f() { return; }", Style);
20371 
20372   // Allow empty blocks on a single line and insert a space in empty blocks.
20373   EXPECT_EQ("void f() { }", format("void f() {}", Style));
20374   EXPECT_EQ("while (true) { }", format("while (true) {}", Style));
20375   // However, don't merge non-empty short loops.
20376   EXPECT_EQ("while (true) {\n"
20377             "    continue;\n"
20378             "}",
20379             format("while (true) { continue; }", Style));
20380 
20381   // Constructor initializers are formatted one per line with the "," on the
20382   // new line.
20383   verifyFormat("Constructor()\n"
20384                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
20385                "    , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
20386                "          aaaaaaaaaaaaaa)\n"
20387                "    , aaaaaaaaaaaaaaaaaaaaaaa()\n"
20388                "{\n"
20389                "}",
20390                Style);
20391   verifyFormat("SomeClass::Constructor()\n"
20392                "    : a(a)\n"
20393                "{\n"
20394                "}",
20395                Style);
20396   EXPECT_EQ("SomeClass::Constructor()\n"
20397             "    : a(a)\n"
20398             "{\n"
20399             "}",
20400             format("SomeClass::Constructor():a(a){}", Style));
20401   verifyFormat("SomeClass::Constructor()\n"
20402                "    : a(a)\n"
20403                "    , b(b)\n"
20404                "    , c(c)\n"
20405                "{\n"
20406                "}",
20407                Style);
20408   verifyFormat("SomeClass::Constructor()\n"
20409                "    : a(a)\n"
20410                "{\n"
20411                "    foo();\n"
20412                "    bar();\n"
20413                "}",
20414                Style);
20415 
20416   // Access specifiers should be aligned left.
20417   verifyFormat("class C {\n"
20418                "public:\n"
20419                "    int i;\n"
20420                "};",
20421                Style);
20422 
20423   // Do not align comments.
20424   verifyFormat("int a; // Do not\n"
20425                "double b; // align comments.",
20426                Style);
20427 
20428   // Do not align operands.
20429   EXPECT_EQ("ASSERT(aaaa\n"
20430             "    || bbbb);",
20431             format("ASSERT ( aaaa\n||bbbb);", Style));
20432 
20433   // Accept input's line breaks.
20434   EXPECT_EQ("if (aaaaaaaaaaaaaaa\n"
20435             "    || bbbbbbbbbbbbbbb) {\n"
20436             "    i++;\n"
20437             "}",
20438             format("if (aaaaaaaaaaaaaaa\n"
20439                    "|| bbbbbbbbbbbbbbb) { i++; }",
20440                    Style));
20441   EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
20442             "    i++;\n"
20443             "}",
20444             format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
20445 
20446   // Don't automatically break all macro definitions (llvm.org/PR17842).
20447   verifyFormat("#define aNumber 10", Style);
20448   // However, generally keep the line breaks that the user authored.
20449   EXPECT_EQ("#define aNumber \\\n"
20450             "    10",
20451             format("#define aNumber \\\n"
20452                    " 10",
20453                    Style));
20454 
20455   // Keep empty and one-element array literals on a single line.
20456   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
20457             "                                  copyItems:YES];",
20458             format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
20459                    "copyItems:YES];",
20460                    Style));
20461   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
20462             "                                  copyItems:YES];",
20463             format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
20464                    "             copyItems:YES];",
20465                    Style));
20466   // FIXME: This does not seem right, there should be more indentation before
20467   // the array literal's entries. Nested blocks have the same problem.
20468   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
20469             "    @\"a\",\n"
20470             "    @\"a\"\n"
20471             "]\n"
20472             "                                  copyItems:YES];",
20473             format("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
20474                    "     @\"a\",\n"
20475                    "     @\"a\"\n"
20476                    "     ]\n"
20477                    "       copyItems:YES];",
20478                    Style));
20479   EXPECT_EQ(
20480       "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
20481       "                                  copyItems:YES];",
20482       format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
20483              "   copyItems:YES];",
20484              Style));
20485 
20486   verifyFormat("[self.a b:c c:d];", Style);
20487   EXPECT_EQ("[self.a b:c\n"
20488             "        c:d];",
20489             format("[self.a b:c\n"
20490                    "c:d];",
20491                    Style));
20492 }
20493 
20494 TEST_F(FormatTest, FormatsLambdas) {
20495   verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n");
20496   verifyFormat(
20497       "int c = [b]() mutable noexcept { return [&b] { return b++; }(); }();\n");
20498   verifyFormat("int c = [&] { [=] { return b++; }(); }();\n");
20499   verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n");
20500   verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n");
20501   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n");
20502   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n");
20503   verifyFormat("auto c = [a = [b = 42] {}] {};\n");
20504   verifyFormat("auto c = [a = &i + 10, b = [] {}] {};\n");
20505   verifyFormat("int x = f(*+[] {});");
20506   verifyFormat("void f() {\n"
20507                "  other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
20508                "}\n");
20509   verifyFormat("void f() {\n"
20510                "  other(x.begin(), //\n"
20511                "        x.end(),   //\n"
20512                "        [&](int, int) { return 1; });\n"
20513                "}\n");
20514   verifyFormat("void f() {\n"
20515                "  other.other.other.other.other(\n"
20516                "      x.begin(), x.end(),\n"
20517                "      [something, rather](int, int, int, int, int, int, int) { "
20518                "return 1; });\n"
20519                "}\n");
20520   verifyFormat(
20521       "void f() {\n"
20522       "  other.other.other.other.other(\n"
20523       "      x.begin(), x.end(),\n"
20524       "      [something, rather](int, int, int, int, int, int, int) {\n"
20525       "        //\n"
20526       "      });\n"
20527       "}\n");
20528   verifyFormat("SomeFunction([]() { // A cool function...\n"
20529                "  return 43;\n"
20530                "});");
20531   EXPECT_EQ("SomeFunction([]() {\n"
20532             "#define A a\n"
20533             "  return 43;\n"
20534             "});",
20535             format("SomeFunction([](){\n"
20536                    "#define A a\n"
20537                    "return 43;\n"
20538                    "});"));
20539   verifyFormat("void f() {\n"
20540                "  SomeFunction([](decltype(x), A *a) {});\n"
20541                "  SomeFunction([](typeof(x), A *a) {});\n"
20542                "  SomeFunction([](_Atomic(x), A *a) {});\n"
20543                "  SomeFunction([](__underlying_type(x), A *a) {});\n"
20544                "}");
20545   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
20546                "    [](const aaaaaaaaaa &a) { return a; });");
20547   verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
20548                "  SomeOtherFunctioooooooooooooooooooooooooon();\n"
20549                "});");
20550   verifyFormat("Constructor()\n"
20551                "    : Field([] { // comment\n"
20552                "        int i;\n"
20553                "      }) {}");
20554   verifyFormat("auto my_lambda = [](const string &some_parameter) {\n"
20555                "  return some_parameter.size();\n"
20556                "};");
20557   verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n"
20558                "    [](const string &s) { return s; };");
20559   verifyFormat("int i = aaaaaa ? 1 //\n"
20560                "               : [] {\n"
20561                "                   return 2; //\n"
20562                "                 }();");
20563   verifyFormat("llvm::errs() << \"number of twos is \"\n"
20564                "             << std::count_if(v.begin(), v.end(), [](int x) {\n"
20565                "                  return x == 2; // force break\n"
20566                "                });");
20567   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
20568                "    [=](int iiiiiiiiiiii) {\n"
20569                "      return aaaaaaaaaaaaaaaaaaaaaaa !=\n"
20570                "             aaaaaaaaaaaaaaaaaaaaaaa;\n"
20571                "    });",
20572                getLLVMStyleWithColumns(60));
20573 
20574   verifyFormat("SomeFunction({[&] {\n"
20575                "                // comment\n"
20576                "              },\n"
20577                "              [&] {\n"
20578                "                // comment\n"
20579                "              }});");
20580   verifyFormat("SomeFunction({[&] {\n"
20581                "  // comment\n"
20582                "}});");
20583   verifyFormat(
20584       "virtual aaaaaaaaaaaaaaaa(\n"
20585       "    std::function<bool()> bbbbbbbbbbbb = [&]() { return true; },\n"
20586       "    aaaaa aaaaaaaaa);");
20587 
20588   // Lambdas with return types.
20589   verifyFormat("int c = []() -> int { return 2; }();\n");
20590   verifyFormat("int c = []() -> int * { return 2; }();\n");
20591   verifyFormat("int c = []() -> vector<int> { return {2}; }();\n");
20592   verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
20593   verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
20594   verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
20595   verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
20596   verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
20597   verifyFormat("[a, a]() -> a<1> {};");
20598   verifyFormat("[]() -> foo<5 + 2> { return {}; };");
20599   verifyFormat("[]() -> foo<5 - 2> { return {}; };");
20600   verifyFormat("[]() -> foo<5 / 2> { return {}; };");
20601   verifyFormat("[]() -> foo<5 * 2> { return {}; };");
20602   verifyFormat("[]() -> foo<5 % 2> { return {}; };");
20603   verifyFormat("[]() -> foo<5 << 2> { return {}; };");
20604   verifyFormat("[]() -> foo<!5> { return {}; };");
20605   verifyFormat("[]() -> foo<~5> { return {}; };");
20606   verifyFormat("[]() -> foo<5 | 2> { return {}; };");
20607   verifyFormat("[]() -> foo<5 || 2> { return {}; };");
20608   verifyFormat("[]() -> foo<5 & 2> { return {}; };");
20609   verifyFormat("[]() -> foo<5 && 2> { return {}; };");
20610   verifyFormat("[]() -> foo<5 == 2> { return {}; };");
20611   verifyFormat("[]() -> foo<5 != 2> { return {}; };");
20612   verifyFormat("[]() -> foo<5 >= 2> { return {}; };");
20613   verifyFormat("[]() -> foo<5 <= 2> { return {}; };");
20614   verifyFormat("[]() -> foo<5 < 2> { return {}; };");
20615   verifyFormat("[]() -> foo<2 ? 1 : 0> { return {}; };");
20616   verifyFormat("namespace bar {\n"
20617                "// broken:\n"
20618                "auto foo{[]() -> foo<5 + 2> { return {}; }};\n"
20619                "} // namespace bar");
20620   verifyFormat("namespace bar {\n"
20621                "// broken:\n"
20622                "auto foo{[]() -> foo<5 - 2> { return {}; }};\n"
20623                "} // namespace bar");
20624   verifyFormat("namespace bar {\n"
20625                "// broken:\n"
20626                "auto foo{[]() -> foo<5 / 2> { return {}; }};\n"
20627                "} // namespace bar");
20628   verifyFormat("namespace bar {\n"
20629                "// broken:\n"
20630                "auto foo{[]() -> foo<5 * 2> { return {}; }};\n"
20631                "} // namespace bar");
20632   verifyFormat("namespace bar {\n"
20633                "// broken:\n"
20634                "auto foo{[]() -> foo<5 % 2> { return {}; }};\n"
20635                "} // namespace bar");
20636   verifyFormat("namespace bar {\n"
20637                "// broken:\n"
20638                "auto foo{[]() -> foo<5 << 2> { return {}; }};\n"
20639                "} // namespace bar");
20640   verifyFormat("namespace bar {\n"
20641                "// broken:\n"
20642                "auto foo{[]() -> foo<!5> { return {}; }};\n"
20643                "} // namespace bar");
20644   verifyFormat("namespace bar {\n"
20645                "// broken:\n"
20646                "auto foo{[]() -> foo<~5> { return {}; }};\n"
20647                "} // namespace bar");
20648   verifyFormat("namespace bar {\n"
20649                "// broken:\n"
20650                "auto foo{[]() -> foo<5 | 2> { return {}; }};\n"
20651                "} // namespace bar");
20652   verifyFormat("namespace bar {\n"
20653                "// broken:\n"
20654                "auto foo{[]() -> foo<5 || 2> { return {}; }};\n"
20655                "} // namespace bar");
20656   verifyFormat("namespace bar {\n"
20657                "// broken:\n"
20658                "auto foo{[]() -> foo<5 & 2> { return {}; }};\n"
20659                "} // namespace bar");
20660   verifyFormat("namespace bar {\n"
20661                "// broken:\n"
20662                "auto foo{[]() -> foo<5 && 2> { return {}; }};\n"
20663                "} // namespace bar");
20664   verifyFormat("namespace bar {\n"
20665                "// broken:\n"
20666                "auto foo{[]() -> foo<5 == 2> { return {}; }};\n"
20667                "} // namespace bar");
20668   verifyFormat("namespace bar {\n"
20669                "// broken:\n"
20670                "auto foo{[]() -> foo<5 != 2> { return {}; }};\n"
20671                "} // namespace bar");
20672   verifyFormat("namespace bar {\n"
20673                "// broken:\n"
20674                "auto foo{[]() -> foo<5 >= 2> { return {}; }};\n"
20675                "} // namespace bar");
20676   verifyFormat("namespace bar {\n"
20677                "// broken:\n"
20678                "auto foo{[]() -> foo<5 <= 2> { return {}; }};\n"
20679                "} // namespace bar");
20680   verifyFormat("namespace bar {\n"
20681                "// broken:\n"
20682                "auto foo{[]() -> foo<5 < 2> { return {}; }};\n"
20683                "} // namespace bar");
20684   verifyFormat("namespace bar {\n"
20685                "// broken:\n"
20686                "auto foo{[]() -> foo<2 ? 1 : 0> { return {}; }};\n"
20687                "} // namespace bar");
20688   verifyFormat("[]() -> a<1> {};");
20689   verifyFormat("[]() -> a<1> { ; };");
20690   verifyFormat("[]() -> a<1> { ; }();");
20691   verifyFormat("[a, a]() -> a<true> {};");
20692   verifyFormat("[]() -> a<true> {};");
20693   verifyFormat("[]() -> a<true> { ; };");
20694   verifyFormat("[]() -> a<true> { ; }();");
20695   verifyFormat("[a, a]() -> a<false> {};");
20696   verifyFormat("[]() -> a<false> {};");
20697   verifyFormat("[]() -> a<false> { ; };");
20698   verifyFormat("[]() -> a<false> { ; }();");
20699   verifyFormat("auto foo{[]() -> foo<false> { ; }};");
20700   verifyFormat("namespace bar {\n"
20701                "auto foo{[]() -> foo<false> { ; }};\n"
20702                "} // namespace bar");
20703   verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
20704                "                   int j) -> int {\n"
20705                "  return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
20706                "};");
20707   verifyFormat(
20708       "aaaaaaaaaaaaaaaaaaaaaa(\n"
20709       "    [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n"
20710       "      return aaaaaaaaaaaaaaaaa;\n"
20711       "    });",
20712       getLLVMStyleWithColumns(70));
20713   verifyFormat("[]() //\n"
20714                "    -> int {\n"
20715                "  return 1; //\n"
20716                "};");
20717   verifyFormat("[]() -> Void<T...> {};");
20718   verifyFormat("[a, b]() -> Tuple<T...> { return {}; };");
20719   verifyFormat("SomeFunction({[]() -> int[] { return {}; }});");
20720   verifyFormat("SomeFunction({[]() -> int *[] { return {}; }});");
20721   verifyFormat("SomeFunction({[]() -> int (*)[] { return {}; }});");
20722   verifyFormat("SomeFunction({[]() -> ns::type<int (*)[]> { return {}; }});");
20723   verifyFormat("return int{[x = x]() { return x; }()};");
20724 
20725   // Lambdas with explicit template argument lists.
20726   verifyFormat(
20727       "auto L = []<template <typename> class T, class U>(T<U> &&a) {};\n");
20728   verifyFormat("auto L = []<class T>(T) {\n"
20729                "  {\n"
20730                "    f();\n"
20731                "    g();\n"
20732                "  }\n"
20733                "};\n");
20734   verifyFormat("auto L = []<class... T>(T...) {\n"
20735                "  {\n"
20736                "    f();\n"
20737                "    g();\n"
20738                "  }\n"
20739                "};\n");
20740   verifyFormat("auto L = []<typename... T>(T...) {\n"
20741                "  {\n"
20742                "    f();\n"
20743                "    g();\n"
20744                "  }\n"
20745                "};\n");
20746   verifyFormat("auto L = []<template <typename...> class T>(T...) {\n"
20747                "  {\n"
20748                "    f();\n"
20749                "    g();\n"
20750                "  }\n"
20751                "};\n");
20752   verifyFormat("auto L = []</*comment*/ class... T>(T...) {\n"
20753                "  {\n"
20754                "    f();\n"
20755                "    g();\n"
20756                "  }\n"
20757                "};\n");
20758 
20759   // Multiple lambdas in the same parentheses change indentation rules. These
20760   // lambdas are forced to start on new lines.
20761   verifyFormat("SomeFunction(\n"
20762                "    []() {\n"
20763                "      //\n"
20764                "    },\n"
20765                "    []() {\n"
20766                "      //\n"
20767                "    });");
20768 
20769   // A lambda passed as arg0 is always pushed to the next line.
20770   verifyFormat("SomeFunction(\n"
20771                "    [this] {\n"
20772                "      //\n"
20773                "    },\n"
20774                "    1);\n");
20775 
20776   // A multi-line lambda passed as arg1 forces arg0 to be pushed out, just like
20777   // the arg0 case above.
20778   auto Style = getGoogleStyle();
20779   Style.BinPackArguments = false;
20780   verifyFormat("SomeFunction(\n"
20781                "    a,\n"
20782                "    [this] {\n"
20783                "      //\n"
20784                "    },\n"
20785                "    b);\n",
20786                Style);
20787   verifyFormat("SomeFunction(\n"
20788                "    a,\n"
20789                "    [this] {\n"
20790                "      //\n"
20791                "    },\n"
20792                "    b);\n");
20793 
20794   // A lambda with a very long line forces arg0 to be pushed out irrespective of
20795   // the BinPackArguments value (as long as the code is wide enough).
20796   verifyFormat(
20797       "something->SomeFunction(\n"
20798       "    a,\n"
20799       "    [this] {\n"
20800       "      "
20801       "D0000000000000000000000000000000000000000000000000000000000001();\n"
20802       "    },\n"
20803       "    b);\n");
20804 
20805   // A multi-line lambda is pulled up as long as the introducer fits on the
20806   // previous line and there are no further args.
20807   verifyFormat("function(1, [this, that] {\n"
20808                "  //\n"
20809                "});\n");
20810   verifyFormat("function([this, that] {\n"
20811                "  //\n"
20812                "});\n");
20813   // FIXME: this format is not ideal and we should consider forcing the first
20814   // arg onto its own line.
20815   verifyFormat("function(a, b, c, //\n"
20816                "         d, [this, that] {\n"
20817                "           //\n"
20818                "         });\n");
20819 
20820   // Multiple lambdas are treated correctly even when there is a short arg0.
20821   verifyFormat("SomeFunction(\n"
20822                "    1,\n"
20823                "    [this] {\n"
20824                "      //\n"
20825                "    },\n"
20826                "    [this] {\n"
20827                "      //\n"
20828                "    },\n"
20829                "    1);\n");
20830 
20831   // More complex introducers.
20832   verifyFormat("return [i, args...] {};");
20833 
20834   // Not lambdas.
20835   verifyFormat("constexpr char hello[]{\"hello\"};");
20836   verifyFormat("double &operator[](int i) { return 0; }\n"
20837                "int i;");
20838   verifyFormat("std::unique_ptr<int[]> foo() {}");
20839   verifyFormat("int i = a[a][a]->f();");
20840   verifyFormat("int i = (*b)[a]->f();");
20841 
20842   // Other corner cases.
20843   verifyFormat("void f() {\n"
20844                "  bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
20845                "  );\n"
20846                "}");
20847 
20848   // Lambdas created through weird macros.
20849   verifyFormat("void f() {\n"
20850                "  MACRO((const AA &a) { return 1; });\n"
20851                "  MACRO((AA &a) { return 1; });\n"
20852                "}");
20853 
20854   verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
20855                "      doo_dah();\n"
20856                "      doo_dah();\n"
20857                "    })) {\n"
20858                "}");
20859   verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n"
20860                "                doo_dah();\n"
20861                "                doo_dah();\n"
20862                "              })) {\n"
20863                "}");
20864   verifyFormat("if CONSTEXPR (blah_blah(whatever, whatever, [] {\n"
20865                "                doo_dah();\n"
20866                "                doo_dah();\n"
20867                "              })) {\n"
20868                "}");
20869   verifyFormat("auto lambda = []() {\n"
20870                "  int a = 2\n"
20871                "#if A\n"
20872                "          + 2\n"
20873                "#endif\n"
20874                "      ;\n"
20875                "};");
20876 
20877   // Lambdas with complex multiline introducers.
20878   verifyFormat(
20879       "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
20880       "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n"
20881       "        -> ::std::unordered_set<\n"
20882       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
20883       "      //\n"
20884       "    });");
20885 
20886   FormatStyle DoNotMerge = getLLVMStyle();
20887   DoNotMerge.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
20888   verifyFormat("auto c = []() {\n"
20889                "  return b;\n"
20890                "};",
20891                "auto c = []() { return b; };", DoNotMerge);
20892   verifyFormat("auto c = []() {\n"
20893                "};",
20894                " auto c = []() {};", DoNotMerge);
20895 
20896   FormatStyle MergeEmptyOnly = getLLVMStyle();
20897   MergeEmptyOnly.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Empty;
20898   verifyFormat("auto c = []() {\n"
20899                "  return b;\n"
20900                "};",
20901                "auto c = []() {\n"
20902                "  return b;\n"
20903                " };",
20904                MergeEmptyOnly);
20905   verifyFormat("auto c = []() {};",
20906                "auto c = []() {\n"
20907                "};",
20908                MergeEmptyOnly);
20909 
20910   FormatStyle MergeInline = getLLVMStyle();
20911   MergeInline.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Inline;
20912   verifyFormat("auto c = []() {\n"
20913                "  return b;\n"
20914                "};",
20915                "auto c = []() { return b; };", MergeInline);
20916   verifyFormat("function([]() { return b; })", "function([]() { return b; })",
20917                MergeInline);
20918   verifyFormat("function([]() { return b; }, a)",
20919                "function([]() { return b; }, a)", MergeInline);
20920   verifyFormat("function(a, []() { return b; })",
20921                "function(a, []() { return b; })", MergeInline);
20922 
20923   // Check option "BraceWrapping.BeforeLambdaBody" and different state of
20924   // AllowShortLambdasOnASingleLine
20925   FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle();
20926   LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
20927   LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
20928   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
20929       FormatStyle::ShortLambdaStyle::SLS_None;
20930   verifyFormat("FctWithOneNestedLambdaInline_SLS_None(\n"
20931                "    []()\n"
20932                "    {\n"
20933                "      return 17;\n"
20934                "    });",
20935                LLVMWithBeforeLambdaBody);
20936   verifyFormat("FctWithOneNestedLambdaEmpty_SLS_None(\n"
20937                "    []()\n"
20938                "    {\n"
20939                "    });",
20940                LLVMWithBeforeLambdaBody);
20941   verifyFormat("auto fct_SLS_None = []()\n"
20942                "{\n"
20943                "  return 17;\n"
20944                "};",
20945                LLVMWithBeforeLambdaBody);
20946   verifyFormat("TwoNestedLambdas_SLS_None(\n"
20947                "    []()\n"
20948                "    {\n"
20949                "      return Call(\n"
20950                "          []()\n"
20951                "          {\n"
20952                "            return 17;\n"
20953                "          });\n"
20954                "    });",
20955                LLVMWithBeforeLambdaBody);
20956   verifyFormat("void Fct() {\n"
20957                "  return {[]()\n"
20958                "          {\n"
20959                "            return 17;\n"
20960                "          }};\n"
20961                "}",
20962                LLVMWithBeforeLambdaBody);
20963 
20964   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
20965       FormatStyle::ShortLambdaStyle::SLS_Empty;
20966   verifyFormat("FctWithOneNestedLambdaInline_SLS_Empty(\n"
20967                "    []()\n"
20968                "    {\n"
20969                "      return 17;\n"
20970                "    });",
20971                LLVMWithBeforeLambdaBody);
20972   verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Empty([]() {});",
20973                LLVMWithBeforeLambdaBody);
20974   verifyFormat("FctWithOneNestedLambdaEmptyInsideAVeryVeryVeryVeryVeryVeryVeryL"
20975                "ongFunctionName_SLS_Empty(\n"
20976                "    []() {});",
20977                LLVMWithBeforeLambdaBody);
20978   verifyFormat("FctWithMultipleParams_SLS_Empty(A, B,\n"
20979                "                                []()\n"
20980                "                                {\n"
20981                "                                  return 17;\n"
20982                "                                });",
20983                LLVMWithBeforeLambdaBody);
20984   verifyFormat("auto fct_SLS_Empty = []()\n"
20985                "{\n"
20986                "  return 17;\n"
20987                "};",
20988                LLVMWithBeforeLambdaBody);
20989   verifyFormat("TwoNestedLambdas_SLS_Empty(\n"
20990                "    []()\n"
20991                "    {\n"
20992                "      return Call([]() {});\n"
20993                "    });",
20994                LLVMWithBeforeLambdaBody);
20995   verifyFormat("TwoNestedLambdas_SLS_Empty(A,\n"
20996                "                           []()\n"
20997                "                           {\n"
20998                "                             return Call([]() {});\n"
20999                "                           });",
21000                LLVMWithBeforeLambdaBody);
21001   verifyFormat(
21002       "FctWithLongLineInLambda_SLS_Empty(\n"
21003       "    []()\n"
21004       "    {\n"
21005       "      return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
21006       "                               AndShouldNotBeConsiderAsInline,\n"
21007       "                               LambdaBodyMustBeBreak);\n"
21008       "    });",
21009       LLVMWithBeforeLambdaBody);
21010 
21011   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
21012       FormatStyle::ShortLambdaStyle::SLS_Inline;
21013   verifyFormat("FctWithOneNestedLambdaInline_SLS_Inline([]() { return 17; });",
21014                LLVMWithBeforeLambdaBody);
21015   verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Inline([]() {});",
21016                LLVMWithBeforeLambdaBody);
21017   verifyFormat("auto fct_SLS_Inline = []()\n"
21018                "{\n"
21019                "  return 17;\n"
21020                "};",
21021                LLVMWithBeforeLambdaBody);
21022   verifyFormat("TwoNestedLambdas_SLS_Inline([]() { return Call([]() { return "
21023                "17; }); });",
21024                LLVMWithBeforeLambdaBody);
21025   verifyFormat(
21026       "FctWithLongLineInLambda_SLS_Inline(\n"
21027       "    []()\n"
21028       "    {\n"
21029       "      return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
21030       "                               AndShouldNotBeConsiderAsInline,\n"
21031       "                               LambdaBodyMustBeBreak);\n"
21032       "    });",
21033       LLVMWithBeforeLambdaBody);
21034   verifyFormat("FctWithMultipleParams_SLS_Inline("
21035                "VeryLongParameterThatShouldAskToBeOnMultiLine,\n"
21036                "                                 []() { return 17; });",
21037                LLVMWithBeforeLambdaBody);
21038   verifyFormat(
21039       "FctWithMultipleParams_SLS_Inline(FirstParam, []() { return 17; });",
21040       LLVMWithBeforeLambdaBody);
21041 
21042   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
21043       FormatStyle::ShortLambdaStyle::SLS_All;
21044   verifyFormat("FctWithOneNestedLambdaInline_SLS_All([]() { return 17; });",
21045                LLVMWithBeforeLambdaBody);
21046   verifyFormat("FctWithOneNestedLambdaEmpty_SLS_All([]() {});",
21047                LLVMWithBeforeLambdaBody);
21048   verifyFormat("auto fct_SLS_All = []() { return 17; };",
21049                LLVMWithBeforeLambdaBody);
21050   verifyFormat("FctWithOneParam_SLS_All(\n"
21051                "    []()\n"
21052                "    {\n"
21053                "      // A cool function...\n"
21054                "      return 43;\n"
21055                "    });",
21056                LLVMWithBeforeLambdaBody);
21057   verifyFormat("FctWithMultipleParams_SLS_All("
21058                "VeryLongParameterThatShouldAskToBeOnMultiLine,\n"
21059                "                              []() { return 17; });",
21060                LLVMWithBeforeLambdaBody);
21061   verifyFormat("FctWithMultipleParams_SLS_All(A, []() { return 17; });",
21062                LLVMWithBeforeLambdaBody);
21063   verifyFormat("FctWithMultipleParams_SLS_All(A, B, []() { return 17; });",
21064                LLVMWithBeforeLambdaBody);
21065   verifyFormat(
21066       "FctWithLongLineInLambda_SLS_All(\n"
21067       "    []()\n"
21068       "    {\n"
21069       "      return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
21070       "                               AndShouldNotBeConsiderAsInline,\n"
21071       "                               LambdaBodyMustBeBreak);\n"
21072       "    });",
21073       LLVMWithBeforeLambdaBody);
21074   verifyFormat(
21075       "auto fct_SLS_All = []()\n"
21076       "{\n"
21077       "  return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
21078       "                           AndShouldNotBeConsiderAsInline,\n"
21079       "                           LambdaBodyMustBeBreak);\n"
21080       "};",
21081       LLVMWithBeforeLambdaBody);
21082   LLVMWithBeforeLambdaBody.BinPackParameters = false;
21083   verifyFormat("FctAllOnSameLine_SLS_All([]() { return S; }, Fst, Second);",
21084                LLVMWithBeforeLambdaBody);
21085   verifyFormat(
21086       "FctWithLongLineInLambda_SLS_All([]() { return SomeValueNotSoLong; },\n"
21087       "                                FirstParam,\n"
21088       "                                SecondParam,\n"
21089       "                                ThirdParam,\n"
21090       "                                FourthParam);",
21091       LLVMWithBeforeLambdaBody);
21092   verifyFormat("FctWithLongLineInLambda_SLS_All(\n"
21093                "    []() { return "
21094                "SomeValueVeryVeryVeryVeryVeryVeryVeryVeryVeryLong; },\n"
21095                "    FirstParam,\n"
21096                "    SecondParam,\n"
21097                "    ThirdParam,\n"
21098                "    FourthParam);",
21099                LLVMWithBeforeLambdaBody);
21100   verifyFormat(
21101       "FctWithLongLineInLambda_SLS_All(FirstParam,\n"
21102       "                                SecondParam,\n"
21103       "                                ThirdParam,\n"
21104       "                                FourthParam,\n"
21105       "                                []() { return SomeValueNotSoLong; });",
21106       LLVMWithBeforeLambdaBody);
21107   verifyFormat("FctWithLongLineInLambda_SLS_All(\n"
21108                "    []()\n"
21109                "    {\n"
21110                "      return "
21111                "HereAVeryLongLineThatWillBeFormattedOnMultipleLineAndShouldNotB"
21112                "eConsiderAsInline;\n"
21113                "    });",
21114                LLVMWithBeforeLambdaBody);
21115   verifyFormat(
21116       "FctWithLongLineInLambda_SLS_All(\n"
21117       "    []()\n"
21118       "    {\n"
21119       "      return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
21120       "                               AndShouldNotBeConsiderAsInline,\n"
21121       "                               LambdaBodyMustBeBreak);\n"
21122       "    });",
21123       LLVMWithBeforeLambdaBody);
21124   verifyFormat("FctWithTwoParams_SLS_All(\n"
21125                "    []()\n"
21126                "    {\n"
21127                "      // A cool function...\n"
21128                "      return 43;\n"
21129                "    },\n"
21130                "    87);",
21131                LLVMWithBeforeLambdaBody);
21132   verifyFormat("FctWithTwoParams_SLS_All([]() { return 43; }, 87);",
21133                LLVMWithBeforeLambdaBody);
21134   verifyFormat("FctWithOneNestedLambdas_SLS_All([]() { return 17; });",
21135                LLVMWithBeforeLambdaBody);
21136   verifyFormat(
21137       "TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; }); });",
21138       LLVMWithBeforeLambdaBody);
21139   verifyFormat("TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; "
21140                "}); }, x);",
21141                LLVMWithBeforeLambdaBody);
21142   verifyFormat("TwoNestedLambdas_SLS_All(\n"
21143                "    []()\n"
21144                "    {\n"
21145                "      // A cool function...\n"
21146                "      return Call([]() { return 17; });\n"
21147                "    });",
21148                LLVMWithBeforeLambdaBody);
21149   verifyFormat("TwoNestedLambdas_SLS_All(\n"
21150                "    []()\n"
21151                "    {\n"
21152                "      return Call(\n"
21153                "          []()\n"
21154                "          {\n"
21155                "            // A cool function...\n"
21156                "            return 17;\n"
21157                "          });\n"
21158                "    });",
21159                LLVMWithBeforeLambdaBody);
21160 
21161   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
21162       FormatStyle::ShortLambdaStyle::SLS_None;
21163 
21164   verifyFormat("auto select = [this]() -> const Library::Object *\n"
21165                "{\n"
21166                "  return MyAssignment::SelectFromList(this);\n"
21167                "};\n",
21168                LLVMWithBeforeLambdaBody);
21169 
21170   verifyFormat("auto select = [this]() -> const Library::Object &\n"
21171                "{\n"
21172                "  return MyAssignment::SelectFromList(this);\n"
21173                "};\n",
21174                LLVMWithBeforeLambdaBody);
21175 
21176   verifyFormat("auto select = [this]() -> std::unique_ptr<Object>\n"
21177                "{\n"
21178                "  return MyAssignment::SelectFromList(this);\n"
21179                "};\n",
21180                LLVMWithBeforeLambdaBody);
21181 
21182   verifyFormat("namespace test {\n"
21183                "class Test {\n"
21184                "public:\n"
21185                "  Test() = default;\n"
21186                "};\n"
21187                "} // namespace test",
21188                LLVMWithBeforeLambdaBody);
21189 
21190   // Lambdas with different indentation styles.
21191   Style = getLLVMStyleWithColumns(100);
21192   EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n"
21193             "  return promise.then(\n"
21194             "      [this, &someVariable, someObject = "
21195             "std::mv(s)](std::vector<int> evaluated) mutable {\n"
21196             "        return someObject.startAsyncAction().then(\n"
21197             "            [this, &someVariable](AsyncActionResult result) "
21198             "mutable { result.processMore(); });\n"
21199             "      });\n"
21200             "}\n",
21201             format("SomeResult doSomething(SomeObject promise) {\n"
21202                    "  return promise.then([this, &someVariable, someObject = "
21203                    "std::mv(s)](std::vector<int> evaluated) mutable {\n"
21204                    "    return someObject.startAsyncAction().then([this, "
21205                    "&someVariable](AsyncActionResult result) mutable {\n"
21206                    "      result.processMore();\n"
21207                    "    });\n"
21208                    "  });\n"
21209                    "}\n",
21210                    Style));
21211   Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope;
21212   verifyFormat("test() {\n"
21213                "  ([]() -> {\n"
21214                "    int b = 32;\n"
21215                "    return 3;\n"
21216                "  }).foo();\n"
21217                "}",
21218                Style);
21219   verifyFormat("test() {\n"
21220                "  []() -> {\n"
21221                "    int b = 32;\n"
21222                "    return 3;\n"
21223                "  }\n"
21224                "}",
21225                Style);
21226   verifyFormat("std::sort(v.begin(), v.end(),\n"
21227                "          [](const auto &someLongArgumentName, const auto "
21228                "&someOtherLongArgumentName) {\n"
21229                "  return someLongArgumentName.someMemberVariable < "
21230                "someOtherLongArgumentName.someMemberVariable;\n"
21231                "});",
21232                Style);
21233   verifyFormat("test() {\n"
21234                "  (\n"
21235                "      []() -> {\n"
21236                "        int b = 32;\n"
21237                "        return 3;\n"
21238                "      },\n"
21239                "      foo, bar)\n"
21240                "      .foo();\n"
21241                "}",
21242                Style);
21243   verifyFormat("test() {\n"
21244                "  ([]() -> {\n"
21245                "    int b = 32;\n"
21246                "    return 3;\n"
21247                "  })\n"
21248                "      .foo()\n"
21249                "      .bar();\n"
21250                "}",
21251                Style);
21252   EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n"
21253             "  return promise.then(\n"
21254             "      [this, &someVariable, someObject = "
21255             "std::mv(s)](std::vector<int> evaluated) mutable {\n"
21256             "    return someObject.startAsyncAction().then(\n"
21257             "        [this, &someVariable](AsyncActionResult result) mutable { "
21258             "result.processMore(); });\n"
21259             "  });\n"
21260             "}\n",
21261             format("SomeResult doSomething(SomeObject promise) {\n"
21262                    "  return promise.then([this, &someVariable, someObject = "
21263                    "std::mv(s)](std::vector<int> evaluated) mutable {\n"
21264                    "    return someObject.startAsyncAction().then([this, "
21265                    "&someVariable](AsyncActionResult result) mutable {\n"
21266                    "      result.processMore();\n"
21267                    "    });\n"
21268                    "  });\n"
21269                    "}\n",
21270                    Style));
21271   EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n"
21272             "  return promise.then([this, &someVariable] {\n"
21273             "    return someObject.startAsyncAction().then(\n"
21274             "        [this, &someVariable](AsyncActionResult result) mutable { "
21275             "result.processMore(); });\n"
21276             "  });\n"
21277             "}\n",
21278             format("SomeResult doSomething(SomeObject promise) {\n"
21279                    "  return promise.then([this, &someVariable] {\n"
21280                    "    return someObject.startAsyncAction().then([this, "
21281                    "&someVariable](AsyncActionResult result) mutable {\n"
21282                    "      result.processMore();\n"
21283                    "    });\n"
21284                    "  });\n"
21285                    "}\n",
21286                    Style));
21287   Style = getGoogleStyle();
21288   Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope;
21289   EXPECT_EQ("#define A                                       \\\n"
21290             "  [] {                                          \\\n"
21291             "    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(        \\\n"
21292             "        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
21293             "      }",
21294             format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
21295                    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
21296                    Style));
21297   // TODO: The current formatting has a minor issue that's not worth fixing
21298   // right now whereby the closing brace is indented relative to the signature
21299   // instead of being aligned. This only happens with macros.
21300 }
21301 
21302 TEST_F(FormatTest, LambdaWithLineComments) {
21303   FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle();
21304   LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
21305   LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
21306   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
21307       FormatStyle::ShortLambdaStyle::SLS_All;
21308 
21309   verifyFormat("auto k = []() { return; }", LLVMWithBeforeLambdaBody);
21310   verifyFormat("auto k = []() // comment\n"
21311                "{ return; }",
21312                LLVMWithBeforeLambdaBody);
21313   verifyFormat("auto k = []() /* comment */ { return; }",
21314                LLVMWithBeforeLambdaBody);
21315   verifyFormat("auto k = []() /* comment */ /* comment */ { return; }",
21316                LLVMWithBeforeLambdaBody);
21317   verifyFormat("auto k = []() // X\n"
21318                "{ return; }",
21319                LLVMWithBeforeLambdaBody);
21320   verifyFormat(
21321       "auto k = []() // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"
21322       "{ return; }",
21323       LLVMWithBeforeLambdaBody);
21324 }
21325 
21326 TEST_F(FormatTest, EmptyLinesInLambdas) {
21327   verifyFormat("auto lambda = []() {\n"
21328                "  x(); //\n"
21329                "};",
21330                "auto lambda = []() {\n"
21331                "\n"
21332                "  x(); //\n"
21333                "\n"
21334                "};");
21335 }
21336 
21337 TEST_F(FormatTest, FormatsBlocks) {
21338   FormatStyle ShortBlocks = getLLVMStyle();
21339   ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
21340   verifyFormat("int (^Block)(int, int);", ShortBlocks);
21341   verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
21342   verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
21343   verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks);
21344   verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks);
21345   verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks);
21346 
21347   verifyFormat("foo(^{ bar(); });", ShortBlocks);
21348   verifyFormat("foo(a, ^{ bar(); });", ShortBlocks);
21349   verifyFormat("{ void (^block)(Object *x); }", ShortBlocks);
21350 
21351   verifyFormat("[operation setCompletionBlock:^{\n"
21352                "  [self onOperationDone];\n"
21353                "}];");
21354   verifyFormat("int i = {[operation setCompletionBlock:^{\n"
21355                "  [self onOperationDone];\n"
21356                "}]};");
21357   verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
21358                "  f();\n"
21359                "}];");
21360   verifyFormat("int a = [operation block:^int(int *i) {\n"
21361                "  return 1;\n"
21362                "}];");
21363   verifyFormat("[myObject doSomethingWith:arg1\n"
21364                "                      aaa:^int(int *a) {\n"
21365                "                        return 1;\n"
21366                "                      }\n"
21367                "                      bbb:f(a * bbbbbbbb)];");
21368 
21369   verifyFormat("[operation setCompletionBlock:^{\n"
21370                "  [self.delegate newDataAvailable];\n"
21371                "}];",
21372                getLLVMStyleWithColumns(60));
21373   verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
21374                "  NSString *path = [self sessionFilePath];\n"
21375                "  if (path) {\n"
21376                "    // ...\n"
21377                "  }\n"
21378                "});");
21379   verifyFormat("[[SessionService sharedService]\n"
21380                "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
21381                "      if (window) {\n"
21382                "        [self windowDidLoad:window];\n"
21383                "      } else {\n"
21384                "        [self errorLoadingWindow];\n"
21385                "      }\n"
21386                "    }];");
21387   verifyFormat("void (^largeBlock)(void) = ^{\n"
21388                "  // ...\n"
21389                "};\n",
21390                getLLVMStyleWithColumns(40));
21391   verifyFormat("[[SessionService sharedService]\n"
21392                "    loadWindowWithCompletionBlock: //\n"
21393                "        ^(SessionWindow *window) {\n"
21394                "          if (window) {\n"
21395                "            [self windowDidLoad:window];\n"
21396                "          } else {\n"
21397                "            [self errorLoadingWindow];\n"
21398                "          }\n"
21399                "        }];",
21400                getLLVMStyleWithColumns(60));
21401   verifyFormat("[myObject doSomethingWith:arg1\n"
21402                "    firstBlock:^(Foo *a) {\n"
21403                "      // ...\n"
21404                "      int i;\n"
21405                "    }\n"
21406                "    secondBlock:^(Bar *b) {\n"
21407                "      // ...\n"
21408                "      int i;\n"
21409                "    }\n"
21410                "    thirdBlock:^Foo(Bar *b) {\n"
21411                "      // ...\n"
21412                "      int i;\n"
21413                "    }];");
21414   verifyFormat("[myObject doSomethingWith:arg1\n"
21415                "               firstBlock:-1\n"
21416                "              secondBlock:^(Bar *b) {\n"
21417                "                // ...\n"
21418                "                int i;\n"
21419                "              }];");
21420 
21421   verifyFormat("f(^{\n"
21422                "  @autoreleasepool {\n"
21423                "    if (a) {\n"
21424                "      g();\n"
21425                "    }\n"
21426                "  }\n"
21427                "});");
21428   verifyFormat("Block b = ^int *(A *a, B *b) {}");
21429   verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n"
21430                "};");
21431 
21432   FormatStyle FourIndent = getLLVMStyle();
21433   FourIndent.ObjCBlockIndentWidth = 4;
21434   verifyFormat("[operation setCompletionBlock:^{\n"
21435                "    [self onOperationDone];\n"
21436                "}];",
21437                FourIndent);
21438 }
21439 
21440 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) {
21441   FormatStyle ZeroColumn = getLLVMStyleWithColumns(0);
21442 
21443   verifyFormat("[[SessionService sharedService] "
21444                "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
21445                "  if (window) {\n"
21446                "    [self windowDidLoad:window];\n"
21447                "  } else {\n"
21448                "    [self errorLoadingWindow];\n"
21449                "  }\n"
21450                "}];",
21451                ZeroColumn);
21452   EXPECT_EQ("[[SessionService sharedService]\n"
21453             "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
21454             "      if (window) {\n"
21455             "        [self windowDidLoad:window];\n"
21456             "      } else {\n"
21457             "        [self errorLoadingWindow];\n"
21458             "      }\n"
21459             "    }];",
21460             format("[[SessionService sharedService]\n"
21461                    "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
21462                    "                if (window) {\n"
21463                    "    [self windowDidLoad:window];\n"
21464                    "  } else {\n"
21465                    "    [self errorLoadingWindow];\n"
21466                    "  }\n"
21467                    "}];",
21468                    ZeroColumn));
21469   verifyFormat("[myObject doSomethingWith:arg1\n"
21470                "    firstBlock:^(Foo *a) {\n"
21471                "      // ...\n"
21472                "      int i;\n"
21473                "    }\n"
21474                "    secondBlock:^(Bar *b) {\n"
21475                "      // ...\n"
21476                "      int i;\n"
21477                "    }\n"
21478                "    thirdBlock:^Foo(Bar *b) {\n"
21479                "      // ...\n"
21480                "      int i;\n"
21481                "    }];",
21482                ZeroColumn);
21483   verifyFormat("f(^{\n"
21484                "  @autoreleasepool {\n"
21485                "    if (a) {\n"
21486                "      g();\n"
21487                "    }\n"
21488                "  }\n"
21489                "});",
21490                ZeroColumn);
21491   verifyFormat("void (^largeBlock)(void) = ^{\n"
21492                "  // ...\n"
21493                "};",
21494                ZeroColumn);
21495 
21496   ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
21497   EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };",
21498             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
21499   ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
21500   EXPECT_EQ("void (^largeBlock)(void) = ^{\n"
21501             "  int i;\n"
21502             "};",
21503             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
21504 }
21505 
21506 TEST_F(FormatTest, SupportsCRLF) {
21507   EXPECT_EQ("int a;\r\n"
21508             "int b;\r\n"
21509             "int c;\r\n",
21510             format("int a;\r\n"
21511                    "  int b;\r\n"
21512                    "    int c;\r\n",
21513                    getLLVMStyle()));
21514   EXPECT_EQ("int a;\r\n"
21515             "int b;\r\n"
21516             "int c;\r\n",
21517             format("int a;\r\n"
21518                    "  int b;\n"
21519                    "    int c;\r\n",
21520                    getLLVMStyle()));
21521   EXPECT_EQ("int a;\n"
21522             "int b;\n"
21523             "int c;\n",
21524             format("int a;\r\n"
21525                    "  int b;\n"
21526                    "    int c;\n",
21527                    getLLVMStyle()));
21528   EXPECT_EQ("\"aaaaaaa \"\r\n"
21529             "\"bbbbbbb\";\r\n",
21530             format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
21531   EXPECT_EQ("#define A \\\r\n"
21532             "  b;      \\\r\n"
21533             "  c;      \\\r\n"
21534             "  d;\r\n",
21535             format("#define A \\\r\n"
21536                    "  b; \\\r\n"
21537                    "  c; d; \r\n",
21538                    getGoogleStyle()));
21539 
21540   EXPECT_EQ("/*\r\n"
21541             "multi line block comments\r\n"
21542             "should not introduce\r\n"
21543             "an extra carriage return\r\n"
21544             "*/\r\n",
21545             format("/*\r\n"
21546                    "multi line block comments\r\n"
21547                    "should not introduce\r\n"
21548                    "an extra carriage return\r\n"
21549                    "*/\r\n"));
21550   EXPECT_EQ("/*\r\n"
21551             "\r\n"
21552             "*/",
21553             format("/*\r\n"
21554                    "    \r\r\r\n"
21555                    "*/"));
21556 
21557   FormatStyle style = getLLVMStyle();
21558 
21559   style.DeriveLineEnding = true;
21560   style.UseCRLF = false;
21561   EXPECT_EQ("union FooBarBazQux {\n"
21562             "  int foo;\n"
21563             "  int bar;\n"
21564             "  int baz;\n"
21565             "};",
21566             format("union FooBarBazQux {\r\n"
21567                    "  int foo;\n"
21568                    "  int bar;\r\n"
21569                    "  int baz;\n"
21570                    "};",
21571                    style));
21572   style.UseCRLF = true;
21573   EXPECT_EQ("union FooBarBazQux {\r\n"
21574             "  int foo;\r\n"
21575             "  int bar;\r\n"
21576             "  int baz;\r\n"
21577             "};",
21578             format("union FooBarBazQux {\r\n"
21579                    "  int foo;\n"
21580                    "  int bar;\r\n"
21581                    "  int baz;\n"
21582                    "};",
21583                    style));
21584 
21585   style.DeriveLineEnding = false;
21586   style.UseCRLF = false;
21587   EXPECT_EQ("union FooBarBazQux {\n"
21588             "  int foo;\n"
21589             "  int bar;\n"
21590             "  int baz;\n"
21591             "  int qux;\n"
21592             "};",
21593             format("union FooBarBazQux {\r\n"
21594                    "  int foo;\n"
21595                    "  int bar;\r\n"
21596                    "  int baz;\n"
21597                    "  int qux;\r\n"
21598                    "};",
21599                    style));
21600   style.UseCRLF = true;
21601   EXPECT_EQ("union FooBarBazQux {\r\n"
21602             "  int foo;\r\n"
21603             "  int bar;\r\n"
21604             "  int baz;\r\n"
21605             "  int qux;\r\n"
21606             "};",
21607             format("union FooBarBazQux {\r\n"
21608                    "  int foo;\n"
21609                    "  int bar;\r\n"
21610                    "  int baz;\n"
21611                    "  int qux;\n"
21612                    "};",
21613                    style));
21614 
21615   style.DeriveLineEnding = true;
21616   style.UseCRLF = false;
21617   EXPECT_EQ("union FooBarBazQux {\r\n"
21618             "  int foo;\r\n"
21619             "  int bar;\r\n"
21620             "  int baz;\r\n"
21621             "  int qux;\r\n"
21622             "};",
21623             format("union FooBarBazQux {\r\n"
21624                    "  int foo;\n"
21625                    "  int bar;\r\n"
21626                    "  int baz;\n"
21627                    "  int qux;\r\n"
21628                    "};",
21629                    style));
21630   style.UseCRLF = true;
21631   EXPECT_EQ("union FooBarBazQux {\n"
21632             "  int foo;\n"
21633             "  int bar;\n"
21634             "  int baz;\n"
21635             "  int qux;\n"
21636             "};",
21637             format("union FooBarBazQux {\r\n"
21638                    "  int foo;\n"
21639                    "  int bar;\r\n"
21640                    "  int baz;\n"
21641                    "  int qux;\n"
21642                    "};",
21643                    style));
21644 }
21645 
21646 TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
21647   verifyFormat("MY_CLASS(C) {\n"
21648                "  int i;\n"
21649                "  int j;\n"
21650                "};");
21651 }
21652 
21653 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
21654   FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
21655   TwoIndent.ContinuationIndentWidth = 2;
21656 
21657   EXPECT_EQ("int i =\n"
21658             "  longFunction(\n"
21659             "    arg);",
21660             format("int i = longFunction(arg);", TwoIndent));
21661 
21662   FormatStyle SixIndent = getLLVMStyleWithColumns(20);
21663   SixIndent.ContinuationIndentWidth = 6;
21664 
21665   EXPECT_EQ("int i =\n"
21666             "      longFunction(\n"
21667             "            arg);",
21668             format("int i = longFunction(arg);", SixIndent));
21669 }
21670 
21671 TEST_F(FormatTest, WrappedClosingParenthesisIndent) {
21672   FormatStyle Style = getLLVMStyle();
21673   verifyFormat("int Foo::getter(\n"
21674                "    //\n"
21675                ") const {\n"
21676                "  return foo;\n"
21677                "}",
21678                Style);
21679   verifyFormat("void Foo::setter(\n"
21680                "    //\n"
21681                ") {\n"
21682                "  foo = 1;\n"
21683                "}",
21684                Style);
21685 }
21686 
21687 TEST_F(FormatTest, SpacesInAngles) {
21688   FormatStyle Spaces = getLLVMStyle();
21689   Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
21690 
21691   verifyFormat("vector< ::std::string > x1;", Spaces);
21692   verifyFormat("Foo< int, Bar > x2;", Spaces);
21693   verifyFormat("Foo< ::int, ::Bar > x3;", Spaces);
21694 
21695   verifyFormat("static_cast< int >(arg);", Spaces);
21696   verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
21697   verifyFormat("f< int, float >();", Spaces);
21698   verifyFormat("template <> g() {}", Spaces);
21699   verifyFormat("template < std::vector< int > > f() {}", Spaces);
21700   verifyFormat("std::function< void(int, int) > fct;", Spaces);
21701   verifyFormat("void inFunction() { std::function< void(int, int) > fct; }",
21702                Spaces);
21703 
21704   Spaces.Standard = FormatStyle::LS_Cpp03;
21705   Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
21706   verifyFormat("A< A< int > >();", Spaces);
21707 
21708   Spaces.SpacesInAngles = FormatStyle::SIAS_Never;
21709   verifyFormat("A<A<int> >();", Spaces);
21710 
21711   Spaces.SpacesInAngles = FormatStyle::SIAS_Leave;
21712   verifyFormat("vector< ::std::string> x4;", "vector<::std::string> x4;",
21713                Spaces);
21714   verifyFormat("vector< ::std::string > x4;", "vector<::std::string > x4;",
21715                Spaces);
21716 
21717   verifyFormat("A<A<int> >();", Spaces);
21718   verifyFormat("A<A<int> >();", "A<A<int>>();", Spaces);
21719   verifyFormat("A< A< int > >();", Spaces);
21720 
21721   Spaces.Standard = FormatStyle::LS_Cpp11;
21722   Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
21723   verifyFormat("A< A< int > >();", Spaces);
21724 
21725   Spaces.SpacesInAngles = FormatStyle::SIAS_Never;
21726   verifyFormat("vector<::std::string> x4;", Spaces);
21727   verifyFormat("vector<int> x5;", Spaces);
21728   verifyFormat("Foo<int, Bar> x6;", Spaces);
21729   verifyFormat("Foo<::int, ::Bar> x7;", Spaces);
21730 
21731   verifyFormat("A<A<int>>();", Spaces);
21732 
21733   Spaces.SpacesInAngles = FormatStyle::SIAS_Leave;
21734   verifyFormat("vector<::std::string> x4;", Spaces);
21735   verifyFormat("vector< ::std::string > x4;", Spaces);
21736   verifyFormat("vector<int> x5;", Spaces);
21737   verifyFormat("vector< int > x5;", Spaces);
21738   verifyFormat("Foo<int, Bar> x6;", Spaces);
21739   verifyFormat("Foo< int, Bar > x6;", Spaces);
21740   verifyFormat("Foo<::int, ::Bar> x7;", Spaces);
21741   verifyFormat("Foo< ::int, ::Bar > x7;", Spaces);
21742 
21743   verifyFormat("A<A<int>>();", Spaces);
21744   verifyFormat("A< A< int > >();", Spaces);
21745   verifyFormat("A<A<int > >();", Spaces);
21746   verifyFormat("A< A< int>>();", Spaces);
21747 
21748   Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
21749   verifyFormat("// clang-format off\n"
21750                "foo<<<1, 1>>>();\n"
21751                "// clang-format on\n",
21752                Spaces);
21753   verifyFormat("// clang-format off\n"
21754                "foo< < <1, 1> > >();\n"
21755                "// clang-format on\n",
21756                Spaces);
21757 }
21758 
21759 TEST_F(FormatTest, SpaceAfterTemplateKeyword) {
21760   FormatStyle Style = getLLVMStyle();
21761   Style.SpaceAfterTemplateKeyword = false;
21762   verifyFormat("template<int> void foo();", Style);
21763 }
21764 
21765 TEST_F(FormatTest, TripleAngleBrackets) {
21766   verifyFormat("f<<<1, 1>>>();");
21767   verifyFormat("f<<<1, 1, 1, s>>>();");
21768   verifyFormat("f<<<a, b, c, d>>>();");
21769   EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();"));
21770   verifyFormat("f<param><<<1, 1>>>();");
21771   verifyFormat("f<1><<<1, 1>>>();");
21772   EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();"));
21773   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
21774                "aaaaaaaaaaa<<<\n    1, 1>>>();");
21775   verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n"
21776                "    <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();");
21777 }
21778 
21779 TEST_F(FormatTest, MergeLessLessAtEnd) {
21780   verifyFormat("<<");
21781   EXPECT_EQ("< < <", format("\\\n<<<"));
21782   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
21783                "aaallvm::outs() <<");
21784   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
21785                "aaaallvm::outs()\n    <<");
21786 }
21787 
21788 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
21789   std::string code = "#if A\n"
21790                      "#if B\n"
21791                      "a.\n"
21792                      "#endif\n"
21793                      "    a = 1;\n"
21794                      "#else\n"
21795                      "#endif\n"
21796                      "#if C\n"
21797                      "#else\n"
21798                      "#endif\n";
21799   EXPECT_EQ(code, format(code));
21800 }
21801 
21802 TEST_F(FormatTest, HandleConflictMarkers) {
21803   // Git/SVN conflict markers.
21804   EXPECT_EQ("int a;\n"
21805             "void f() {\n"
21806             "  callme(some(parameter1,\n"
21807             "<<<<<<< text by the vcs\n"
21808             "              parameter2),\n"
21809             "||||||| text by the vcs\n"
21810             "              parameter2),\n"
21811             "         parameter3,\n"
21812             "======= text by the vcs\n"
21813             "              parameter2, parameter3),\n"
21814             ">>>>>>> text by the vcs\n"
21815             "         otherparameter);\n",
21816             format("int a;\n"
21817                    "void f() {\n"
21818                    "  callme(some(parameter1,\n"
21819                    "<<<<<<< text by the vcs\n"
21820                    "  parameter2),\n"
21821                    "||||||| text by the vcs\n"
21822                    "  parameter2),\n"
21823                    "  parameter3,\n"
21824                    "======= text by the vcs\n"
21825                    "  parameter2,\n"
21826                    "  parameter3),\n"
21827                    ">>>>>>> text by the vcs\n"
21828                    "  otherparameter);\n"));
21829 
21830   // Perforce markers.
21831   EXPECT_EQ("void f() {\n"
21832             "  function(\n"
21833             ">>>> text by the vcs\n"
21834             "      parameter,\n"
21835             "==== text by the vcs\n"
21836             "      parameter,\n"
21837             "==== text by the vcs\n"
21838             "      parameter,\n"
21839             "<<<< text by the vcs\n"
21840             "      parameter);\n",
21841             format("void f() {\n"
21842                    "  function(\n"
21843                    ">>>> text by the vcs\n"
21844                    "  parameter,\n"
21845                    "==== text by the vcs\n"
21846                    "  parameter,\n"
21847                    "==== text by the vcs\n"
21848                    "  parameter,\n"
21849                    "<<<< text by the vcs\n"
21850                    "  parameter);\n"));
21851 
21852   EXPECT_EQ("<<<<<<<\n"
21853             "|||||||\n"
21854             "=======\n"
21855             ">>>>>>>",
21856             format("<<<<<<<\n"
21857                    "|||||||\n"
21858                    "=======\n"
21859                    ">>>>>>>"));
21860 
21861   EXPECT_EQ("<<<<<<<\n"
21862             "|||||||\n"
21863             "int i;\n"
21864             "=======\n"
21865             ">>>>>>>",
21866             format("<<<<<<<\n"
21867                    "|||||||\n"
21868                    "int i;\n"
21869                    "=======\n"
21870                    ">>>>>>>"));
21871 
21872   // FIXME: Handle parsing of macros around conflict markers correctly:
21873   EXPECT_EQ("#define Macro \\\n"
21874             "<<<<<<<\n"
21875             "Something \\\n"
21876             "|||||||\n"
21877             "Else \\\n"
21878             "=======\n"
21879             "Other \\\n"
21880             ">>>>>>>\n"
21881             "    End int i;\n",
21882             format("#define Macro \\\n"
21883                    "<<<<<<<\n"
21884                    "  Something \\\n"
21885                    "|||||||\n"
21886                    "  Else \\\n"
21887                    "=======\n"
21888                    "  Other \\\n"
21889                    ">>>>>>>\n"
21890                    "  End\n"
21891                    "int i;\n"));
21892 
21893   verifyFormat(R"(====
21894 #ifdef A
21895 a
21896 #else
21897 b
21898 #endif
21899 )");
21900 }
21901 
21902 TEST_F(FormatTest, DisableRegions) {
21903   EXPECT_EQ("int i;\n"
21904             "// clang-format off\n"
21905             "  int j;\n"
21906             "// clang-format on\n"
21907             "int k;",
21908             format(" int  i;\n"
21909                    "   // clang-format off\n"
21910                    "  int j;\n"
21911                    " // clang-format on\n"
21912                    "   int   k;"));
21913   EXPECT_EQ("int i;\n"
21914             "/* clang-format off */\n"
21915             "  int j;\n"
21916             "/* clang-format on */\n"
21917             "int k;",
21918             format(" int  i;\n"
21919                    "   /* clang-format off */\n"
21920                    "  int j;\n"
21921                    " /* clang-format on */\n"
21922                    "   int   k;"));
21923 
21924   // Don't reflow comments within disabled regions.
21925   EXPECT_EQ("// clang-format off\n"
21926             "// long long long long long long line\n"
21927             "/* clang-format on */\n"
21928             "/* long long long\n"
21929             " * long long long\n"
21930             " * line */\n"
21931             "int i;\n"
21932             "/* clang-format off */\n"
21933             "/* long long long long long long line */\n",
21934             format("// clang-format off\n"
21935                    "// long long long long long long line\n"
21936                    "/* clang-format on */\n"
21937                    "/* long long long long long long line */\n"
21938                    "int i;\n"
21939                    "/* clang-format off */\n"
21940                    "/* long long long long long long line */\n",
21941                    getLLVMStyleWithColumns(20)));
21942 }
21943 
21944 TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
21945   format("? ) =");
21946   verifyNoCrash("#define a\\\n /**/}");
21947 }
21948 
21949 TEST_F(FormatTest, FormatsTableGenCode) {
21950   FormatStyle Style = getLLVMStyle();
21951   Style.Language = FormatStyle::LK_TableGen;
21952   verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style);
21953 }
21954 
21955 TEST_F(FormatTest, ArrayOfTemplates) {
21956   EXPECT_EQ("auto a = new unique_ptr<int>[10];",
21957             format("auto a = new unique_ptr<int > [ 10];"));
21958 
21959   FormatStyle Spaces = getLLVMStyle();
21960   Spaces.SpacesInSquareBrackets = true;
21961   EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];",
21962             format("auto a = new unique_ptr<int > [10];", Spaces));
21963 }
21964 
21965 TEST_F(FormatTest, ArrayAsTemplateType) {
21966   EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;",
21967             format("auto a = unique_ptr < Foo < Bar>[ 10]> ;"));
21968 
21969   FormatStyle Spaces = getLLVMStyle();
21970   Spaces.SpacesInSquareBrackets = true;
21971   EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;",
21972             format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces));
21973 }
21974 
21975 TEST_F(FormatTest, NoSpaceAfterSuper) { verifyFormat("__super::FooBar();"); }
21976 
21977 TEST(FormatStyle, GetStyleWithEmptyFileName) {
21978   llvm::vfs::InMemoryFileSystem FS;
21979   auto Style1 = getStyle("file", "", "Google", "", &FS);
21980   ASSERT_TRUE((bool)Style1);
21981   ASSERT_EQ(*Style1, getGoogleStyle());
21982 }
21983 
21984 TEST(FormatStyle, GetStyleOfFile) {
21985   llvm::vfs::InMemoryFileSystem FS;
21986   // Test 1: format file in the same directory.
21987   ASSERT_TRUE(
21988       FS.addFile("/a/.clang-format", 0,
21989                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM")));
21990   ASSERT_TRUE(
21991       FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
21992   auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS);
21993   ASSERT_TRUE((bool)Style1);
21994   ASSERT_EQ(*Style1, getLLVMStyle());
21995 
21996   // Test 2.1: fallback to default.
21997   ASSERT_TRUE(
21998       FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
21999   auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS);
22000   ASSERT_TRUE((bool)Style2);
22001   ASSERT_EQ(*Style2, getMozillaStyle());
22002 
22003   // Test 2.2: no format on 'none' fallback style.
22004   Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
22005   ASSERT_TRUE((bool)Style2);
22006   ASSERT_EQ(*Style2, getNoStyle());
22007 
22008   // Test 2.3: format if config is found with no based style while fallback is
22009   // 'none'.
22010   ASSERT_TRUE(FS.addFile("/b/.clang-format", 0,
22011                          llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2")));
22012   Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
22013   ASSERT_TRUE((bool)Style2);
22014   ASSERT_EQ(*Style2, getLLVMStyle());
22015 
22016   // Test 2.4: format if yaml with no based style, while fallback is 'none'.
22017   Style2 = getStyle("{}", "a.h", "none", "", &FS);
22018   ASSERT_TRUE((bool)Style2);
22019   ASSERT_EQ(*Style2, getLLVMStyle());
22020 
22021   // Test 3: format file in parent directory.
22022   ASSERT_TRUE(
22023       FS.addFile("/c/.clang-format", 0,
22024                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google")));
22025   ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0,
22026                          llvm::MemoryBuffer::getMemBuffer("int i;")));
22027   auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS);
22028   ASSERT_TRUE((bool)Style3);
22029   ASSERT_EQ(*Style3, getGoogleStyle());
22030 
22031   // Test 4: error on invalid fallback style
22032   auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS);
22033   ASSERT_FALSE((bool)Style4);
22034   llvm::consumeError(Style4.takeError());
22035 
22036   // Test 5: error on invalid yaml on command line
22037   auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS);
22038   ASSERT_FALSE((bool)Style5);
22039   llvm::consumeError(Style5.takeError());
22040 
22041   // Test 6: error on invalid style
22042   auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS);
22043   ASSERT_FALSE((bool)Style6);
22044   llvm::consumeError(Style6.takeError());
22045 
22046   // Test 7: found config file, error on parsing it
22047   ASSERT_TRUE(
22048       FS.addFile("/d/.clang-format", 0,
22049                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n"
22050                                                   "InvalidKey: InvalidValue")));
22051   ASSERT_TRUE(
22052       FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
22053   auto Style7a = getStyle("file", "/d/.clang-format", "LLVM", "", &FS);
22054   ASSERT_FALSE((bool)Style7a);
22055   llvm::consumeError(Style7a.takeError());
22056 
22057   auto Style7b = getStyle("file", "/d/.clang-format", "LLVM", "", &FS, true);
22058   ASSERT_TRUE((bool)Style7b);
22059 
22060   // Test 8: inferred per-language defaults apply.
22061   auto StyleTd = getStyle("file", "x.td", "llvm", "", &FS);
22062   ASSERT_TRUE((bool)StyleTd);
22063   ASSERT_EQ(*StyleTd, getLLVMStyle(FormatStyle::LK_TableGen));
22064 
22065   // Test 9.1.1: overwriting a file style, when no parent file exists with no
22066   // fallback style.
22067   ASSERT_TRUE(FS.addFile(
22068       "/e/sub/.clang-format", 0,
22069       llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: InheritParentConfig\n"
22070                                        "ColumnLimit: 20")));
22071   ASSERT_TRUE(FS.addFile("/e/sub/code.cpp", 0,
22072                          llvm::MemoryBuffer::getMemBuffer("int i;")));
22073   auto Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS);
22074   ASSERT_TRUE(static_cast<bool>(Style9));
22075   ASSERT_EQ(*Style9, [] {
22076     auto Style = getNoStyle();
22077     Style.ColumnLimit = 20;
22078     return Style;
22079   }());
22080 
22081   // Test 9.1.2: propagate more than one level with no parent file.
22082   ASSERT_TRUE(FS.addFile("/e/sub/sub/code.cpp", 0,
22083                          llvm::MemoryBuffer::getMemBuffer("int i;")));
22084   ASSERT_TRUE(FS.addFile("/e/sub/sub/.clang-format", 0,
22085                          llvm::MemoryBuffer::getMemBuffer(
22086                              "BasedOnStyle: InheritParentConfig\n"
22087                              "WhitespaceSensitiveMacros: ['FOO', 'BAR']")));
22088   std::vector<std::string> NonDefaultWhiteSpaceMacros{"FOO", "BAR"};
22089 
22090   ASSERT_NE(Style9->WhitespaceSensitiveMacros, NonDefaultWhiteSpaceMacros);
22091   Style9 = getStyle("file", "/e/sub/sub/code.cpp", "none", "", &FS);
22092   ASSERT_TRUE(static_cast<bool>(Style9));
22093   ASSERT_EQ(*Style9, [&NonDefaultWhiteSpaceMacros] {
22094     auto Style = getNoStyle();
22095     Style.ColumnLimit = 20;
22096     Style.WhitespaceSensitiveMacros = NonDefaultWhiteSpaceMacros;
22097     return Style;
22098   }());
22099 
22100   // Test 9.2: with LLVM fallback style
22101   Style9 = getStyle("file", "/e/sub/code.cpp", "LLVM", "", &FS);
22102   ASSERT_TRUE(static_cast<bool>(Style9));
22103   ASSERT_EQ(*Style9, [] {
22104     auto Style = getLLVMStyle();
22105     Style.ColumnLimit = 20;
22106     return Style;
22107   }());
22108 
22109   // Test 9.3: with a parent file
22110   ASSERT_TRUE(
22111       FS.addFile("/e/.clang-format", 0,
22112                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google\n"
22113                                                   "UseTab: Always")));
22114   Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS);
22115   ASSERT_TRUE(static_cast<bool>(Style9));
22116   ASSERT_EQ(*Style9, [] {
22117     auto Style = getGoogleStyle();
22118     Style.ColumnLimit = 20;
22119     Style.UseTab = FormatStyle::UT_Always;
22120     return Style;
22121   }());
22122 
22123   // Test 9.4: propagate more than one level with a parent file.
22124   const auto SubSubStyle = [&NonDefaultWhiteSpaceMacros] {
22125     auto Style = getGoogleStyle();
22126     Style.ColumnLimit = 20;
22127     Style.UseTab = FormatStyle::UT_Always;
22128     Style.WhitespaceSensitiveMacros = NonDefaultWhiteSpaceMacros;
22129     return Style;
22130   }();
22131 
22132   ASSERT_NE(Style9->WhitespaceSensitiveMacros, NonDefaultWhiteSpaceMacros);
22133   Style9 = getStyle("file", "/e/sub/sub/code.cpp", "none", "", &FS);
22134   ASSERT_TRUE(static_cast<bool>(Style9));
22135   ASSERT_EQ(*Style9, SubSubStyle);
22136 
22137   // Test 9.5: use InheritParentConfig as style name
22138   Style9 =
22139       getStyle("inheritparentconfig", "/e/sub/sub/code.cpp", "none", "", &FS);
22140   ASSERT_TRUE(static_cast<bool>(Style9));
22141   ASSERT_EQ(*Style9, SubSubStyle);
22142 
22143   // Test 9.6: use command line style with inheritance
22144   Style9 = getStyle("{BasedOnStyle: InheritParentConfig}", "/e/sub/code.cpp",
22145                     "none", "", &FS);
22146   ASSERT_TRUE(static_cast<bool>(Style9));
22147   ASSERT_EQ(*Style9, SubSubStyle);
22148 
22149   // Test 9.7: use command line style with inheritance and own config
22150   Style9 = getStyle("{BasedOnStyle: InheritParentConfig, "
22151                     "WhitespaceSensitiveMacros: ['FOO', 'BAR']}",
22152                     "/e/sub/code.cpp", "none", "", &FS);
22153   ASSERT_TRUE(static_cast<bool>(Style9));
22154   ASSERT_EQ(*Style9, SubSubStyle);
22155 
22156   // Test 9.8: use inheritance from a file without BasedOnStyle
22157   ASSERT_TRUE(FS.addFile("/e/withoutbase/.clang-format", 0,
22158                          llvm::MemoryBuffer::getMemBuffer("ColumnLimit: 123")));
22159   ASSERT_TRUE(
22160       FS.addFile("/e/withoutbase/sub/.clang-format", 0,
22161                  llvm::MemoryBuffer::getMemBuffer(
22162                      "BasedOnStyle: InheritParentConfig\nIndentWidth: 7")));
22163   // Make sure we do not use the fallback style
22164   Style9 = getStyle("file", "/e/withoutbase/code.cpp", "google", "", &FS);
22165   ASSERT_TRUE(static_cast<bool>(Style9));
22166   ASSERT_EQ(*Style9, [] {
22167     auto Style = getLLVMStyle();
22168     Style.ColumnLimit = 123;
22169     return Style;
22170   }());
22171 
22172   Style9 = getStyle("file", "/e/withoutbase/sub/code.cpp", "google", "", &FS);
22173   ASSERT_TRUE(static_cast<bool>(Style9));
22174   ASSERT_EQ(*Style9, [] {
22175     auto Style = getLLVMStyle();
22176     Style.ColumnLimit = 123;
22177     Style.IndentWidth = 7;
22178     return Style;
22179   }());
22180 
22181   // Test 9.9: use inheritance from a specific config file.
22182   Style9 = getStyle("file:/e/sub/sub/.clang-format", "/e/sub/sub/code.cpp",
22183                     "none", "", &FS);
22184   ASSERT_TRUE(static_cast<bool>(Style9));
22185   ASSERT_EQ(*Style9, SubSubStyle);
22186 }
22187 
22188 TEST(FormatStyle, GetStyleOfSpecificFile) {
22189   llvm::vfs::InMemoryFileSystem FS;
22190   // Specify absolute path to a format file in a parent directory.
22191   ASSERT_TRUE(
22192       FS.addFile("/e/.clang-format", 0,
22193                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM")));
22194   ASSERT_TRUE(
22195       FS.addFile("/e/explicit.clang-format", 0,
22196                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google")));
22197   ASSERT_TRUE(FS.addFile("/e/sub/sub/sub/test.cpp", 0,
22198                          llvm::MemoryBuffer::getMemBuffer("int i;")));
22199   auto Style = getStyle("file:/e/explicit.clang-format",
22200                         "/e/sub/sub/sub/test.cpp", "LLVM", "", &FS);
22201   ASSERT_TRUE(static_cast<bool>(Style));
22202   ASSERT_EQ(*Style, getGoogleStyle());
22203 
22204   // Specify relative path to a format file.
22205   ASSERT_TRUE(
22206       FS.addFile("../../e/explicit.clang-format", 0,
22207                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google")));
22208   Style = getStyle("file:../../e/explicit.clang-format",
22209                    "/e/sub/sub/sub/test.cpp", "LLVM", "", &FS);
22210   ASSERT_TRUE(static_cast<bool>(Style));
22211   ASSERT_EQ(*Style, getGoogleStyle());
22212 
22213   // Specify path to a format file that does not exist.
22214   Style = getStyle("file:/e/missing.clang-format", "/e/sub/sub/sub/test.cpp",
22215                    "LLVM", "", &FS);
22216   ASSERT_FALSE(static_cast<bool>(Style));
22217   llvm::consumeError(Style.takeError());
22218 
22219   // Specify path to a file on the filesystem.
22220   SmallString<128> FormatFilePath;
22221   std::error_code ECF = llvm::sys::fs::createTemporaryFile(
22222       "FormatFileTest", "tpl", FormatFilePath);
22223   EXPECT_FALSE((bool)ECF);
22224   llvm::raw_fd_ostream FormatFileTest(FormatFilePath, ECF);
22225   EXPECT_FALSE((bool)ECF);
22226   FormatFileTest << "BasedOnStyle: Google\n";
22227   FormatFileTest.close();
22228 
22229   SmallString<128> TestFilePath;
22230   std::error_code ECT =
22231       llvm::sys::fs::createTemporaryFile("CodeFileTest", "cc", TestFilePath);
22232   EXPECT_FALSE((bool)ECT);
22233   llvm::raw_fd_ostream CodeFileTest(TestFilePath, ECT);
22234   CodeFileTest << "int i;\n";
22235   CodeFileTest.close();
22236 
22237   std::string format_file_arg = std::string("file:") + FormatFilePath.c_str();
22238   Style = getStyle(format_file_arg, TestFilePath, "LLVM", "", nullptr);
22239 
22240   llvm::sys::fs::remove(FormatFilePath.c_str());
22241   llvm::sys::fs::remove(TestFilePath.c_str());
22242   ASSERT_TRUE(static_cast<bool>(Style));
22243   ASSERT_EQ(*Style, getGoogleStyle());
22244 }
22245 
22246 TEST_F(ReplacementTest, FormatCodeAfterReplacements) {
22247   // Column limit is 20.
22248   std::string Code = "Type *a =\n"
22249                      "    new Type();\n"
22250                      "g(iiiii, 0, jjjjj,\n"
22251                      "  0, kkkkk, 0, mm);\n"
22252                      "int  bad     = format   ;";
22253   std::string Expected = "auto a = new Type();\n"
22254                          "g(iiiii, nullptr,\n"
22255                          "  jjjjj, nullptr,\n"
22256                          "  kkkkk, nullptr,\n"
22257                          "  mm);\n"
22258                          "int  bad     = format   ;";
22259   FileID ID = Context.createInMemoryFile("format.cpp", Code);
22260   tooling::Replacements Replaces = toReplacements(
22261       {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6,
22262                             "auto "),
22263        tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1,
22264                             "nullptr"),
22265        tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1,
22266                             "nullptr"),
22267        tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1,
22268                             "nullptr")});
22269 
22270   FormatStyle Style = getLLVMStyle();
22271   Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility.
22272   auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
22273   EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
22274       << llvm::toString(FormattedReplaces.takeError()) << "\n";
22275   auto Result = applyAllReplacements(Code, *FormattedReplaces);
22276   EXPECT_TRUE(static_cast<bool>(Result));
22277   EXPECT_EQ(Expected, *Result);
22278 }
22279 
22280 TEST_F(ReplacementTest, SortIncludesAfterReplacement) {
22281   std::string Code = "#include \"a.h\"\n"
22282                      "#include \"c.h\"\n"
22283                      "\n"
22284                      "int main() {\n"
22285                      "  return 0;\n"
22286                      "}";
22287   std::string Expected = "#include \"a.h\"\n"
22288                          "#include \"b.h\"\n"
22289                          "#include \"c.h\"\n"
22290                          "\n"
22291                          "int main() {\n"
22292                          "  return 0;\n"
22293                          "}";
22294   FileID ID = Context.createInMemoryFile("fix.cpp", Code);
22295   tooling::Replacements Replaces = toReplacements(
22296       {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0,
22297                             "#include \"b.h\"\n")});
22298 
22299   FormatStyle Style = getLLVMStyle();
22300   Style.SortIncludes = FormatStyle::SI_CaseSensitive;
22301   auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
22302   EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
22303       << llvm::toString(FormattedReplaces.takeError()) << "\n";
22304   auto Result = applyAllReplacements(Code, *FormattedReplaces);
22305   EXPECT_TRUE(static_cast<bool>(Result));
22306   EXPECT_EQ(Expected, *Result);
22307 }
22308 
22309 TEST_F(FormatTest, FormatSortsUsingDeclarations) {
22310   EXPECT_EQ("using std::cin;\n"
22311             "using std::cout;",
22312             format("using std::cout;\n"
22313                    "using std::cin;",
22314                    getGoogleStyle()));
22315 }
22316 
22317 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) {
22318   FormatStyle Style = getLLVMStyle();
22319   Style.Standard = FormatStyle::LS_Cpp03;
22320   // cpp03 recognize this string as identifier u8 and literal character 'a'
22321   EXPECT_EQ("auto c = u8 'a';", format("auto c = u8'a';", Style));
22322 }
22323 
22324 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) {
22325   // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers
22326   // all modes, including C++11, C++14 and C++17
22327   EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';"));
22328 }
22329 
22330 TEST_F(FormatTest, DoNotFormatLikelyXml) {
22331   EXPECT_EQ("<!-- ;> -->", format("<!-- ;> -->", getGoogleStyle()));
22332   EXPECT_EQ(" <!-- >; -->", format(" <!-- >; -->", getGoogleStyle()));
22333 }
22334 
22335 TEST_F(FormatTest, StructuredBindings) {
22336   // Structured bindings is a C++17 feature.
22337   // all modes, including C++11, C++14 and C++17
22338   verifyFormat("auto [a, b] = f();");
22339   EXPECT_EQ("auto [a, b] = f();", format("auto[a, b] = f();"));
22340   EXPECT_EQ("const auto [a, b] = f();", format("const   auto[a, b] = f();"));
22341   EXPECT_EQ("auto const [a, b] = f();", format("auto  const[a, b] = f();"));
22342   EXPECT_EQ("auto const volatile [a, b] = f();",
22343             format("auto  const   volatile[a, b] = f();"));
22344   EXPECT_EQ("auto [a, b, c] = f();", format("auto   [  a  ,  b,c   ] = f();"));
22345   EXPECT_EQ("auto &[a, b, c] = f();",
22346             format("auto   &[  a  ,  b,c   ] = f();"));
22347   EXPECT_EQ("auto &&[a, b, c] = f();",
22348             format("auto   &&[  a  ,  b,c   ] = f();"));
22349   EXPECT_EQ("auto const &[a, b] = f();", format("auto  const&[a, b] = f();"));
22350   EXPECT_EQ("auto const volatile &&[a, b] = f();",
22351             format("auto  const  volatile  &&[a, b] = f();"));
22352   EXPECT_EQ("auto const &&[a, b] = f();",
22353             format("auto  const   &&  [a, b] = f();"));
22354   EXPECT_EQ("const auto &[a, b] = f();",
22355             format("const  auto  &  [a, b] = f();"));
22356   EXPECT_EQ("const auto volatile &&[a, b] = f();",
22357             format("const  auto   volatile  &&[a, b] = f();"));
22358   EXPECT_EQ("volatile const auto &&[a, b] = f();",
22359             format("volatile  const  auto   &&[a, b] = f();"));
22360   EXPECT_EQ("const auto &&[a, b] = f();",
22361             format("const  auto  &&  [a, b] = f();"));
22362 
22363   // Make sure we don't mistake structured bindings for lambdas.
22364   FormatStyle PointerMiddle = getLLVMStyle();
22365   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
22366   verifyFormat("auto [a1, b]{A * i};", getGoogleStyle());
22367   verifyFormat("auto [a2, b]{A * i};", getLLVMStyle());
22368   verifyFormat("auto [a3, b]{A * i};", PointerMiddle);
22369   verifyFormat("auto const [a1, b]{A * i};", getGoogleStyle());
22370   verifyFormat("auto const [a2, b]{A * i};", getLLVMStyle());
22371   verifyFormat("auto const [a3, b]{A * i};", PointerMiddle);
22372   verifyFormat("auto const& [a1, b]{A * i};", getGoogleStyle());
22373   verifyFormat("auto const &[a2, b]{A * i};", getLLVMStyle());
22374   verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle);
22375   verifyFormat("auto const&& [a1, b]{A * i};", getGoogleStyle());
22376   verifyFormat("auto const &&[a2, b]{A * i};", getLLVMStyle());
22377   verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle);
22378 
22379   EXPECT_EQ("for (const auto &&[a, b] : some_range) {\n}",
22380             format("for (const auto   &&   [a, b] : some_range) {\n}"));
22381   EXPECT_EQ("for (const auto &[a, b] : some_range) {\n}",
22382             format("for (const auto   &   [a, b] : some_range) {\n}"));
22383   EXPECT_EQ("for (const auto [a, b] : some_range) {\n}",
22384             format("for (const auto[a, b] : some_range) {\n}"));
22385   EXPECT_EQ("auto [x, y](expr);", format("auto[x,y]  (expr);"));
22386   EXPECT_EQ("auto &[x, y](expr);", format("auto  &  [x,y]  (expr);"));
22387   EXPECT_EQ("auto &&[x, y](expr);", format("auto  &&  [x,y]  (expr);"));
22388   EXPECT_EQ("auto const &[x, y](expr);",
22389             format("auto  const  &  [x,y]  (expr);"));
22390   EXPECT_EQ("auto const &&[x, y](expr);",
22391             format("auto  const  &&  [x,y]  (expr);"));
22392   EXPECT_EQ("auto [x, y]{expr};", format("auto[x,y]     {expr};"));
22393   EXPECT_EQ("auto const &[x, y]{expr};",
22394             format("auto  const  &  [x,y]  {expr};"));
22395   EXPECT_EQ("auto const &&[x, y]{expr};",
22396             format("auto  const  &&  [x,y]  {expr};"));
22397 
22398   FormatStyle Spaces = getLLVMStyle();
22399   Spaces.SpacesInSquareBrackets = true;
22400   verifyFormat("auto [ a, b ] = f();", Spaces);
22401   verifyFormat("auto &&[ a, b ] = f();", Spaces);
22402   verifyFormat("auto &[ a, b ] = f();", Spaces);
22403   verifyFormat("auto const &&[ a, b ] = f();", Spaces);
22404   verifyFormat("auto const &[ a, b ] = f();", Spaces);
22405 }
22406 
22407 TEST_F(FormatTest, FileAndCode) {
22408   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.cc", ""));
22409   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.m", ""));
22410   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.mm", ""));
22411   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", ""));
22412   EXPECT_EQ(FormatStyle::LK_ObjC,
22413             guessLanguage("foo.h", "@interface Foo\n@end\n"));
22414   EXPECT_EQ(
22415       FormatStyle::LK_ObjC,
22416       guessLanguage("foo.h", "#define TRY(x, y) @try { x; } @finally { y; }"));
22417   EXPECT_EQ(FormatStyle::LK_ObjC,
22418             guessLanguage("foo.h", "#define AVAIL(x) @available(x, *))"));
22419   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@class Foo;"));
22420   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo", ""));
22421   EXPECT_EQ(FormatStyle::LK_ObjC,
22422             guessLanguage("foo", "@interface Foo\n@end\n"));
22423   EXPECT_EQ(FormatStyle::LK_ObjC,
22424             guessLanguage("foo.h", "int DoStuff(CGRect rect);\n"));
22425   EXPECT_EQ(
22426       FormatStyle::LK_ObjC,
22427       guessLanguage("foo.h",
22428                     "#define MY_POINT_MAKE(x, y) CGPointMake((x), (y));\n"));
22429   EXPECT_EQ(
22430       FormatStyle::LK_Cpp,
22431       guessLanguage("foo.h", "#define FOO(...) auto bar = [] __VA_ARGS__;"));
22432 }
22433 
22434 TEST_F(FormatTest, GuessLanguageWithCpp11AttributeSpecifiers) {
22435   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[noreturn]];"));
22436   EXPECT_EQ(FormatStyle::LK_ObjC,
22437             guessLanguage("foo.h", "array[[calculator getIndex]];"));
22438   EXPECT_EQ(FormatStyle::LK_Cpp,
22439             guessLanguage("foo.h", "[[noreturn, deprecated(\"so sorry\")]];"));
22440   EXPECT_EQ(
22441       FormatStyle::LK_Cpp,
22442       guessLanguage("foo.h", "[[noreturn, deprecated(\"gone, sorry\")]];"));
22443   EXPECT_EQ(FormatStyle::LK_ObjC,
22444             guessLanguage("foo.h", "[[noreturn foo] bar];"));
22445   EXPECT_EQ(FormatStyle::LK_Cpp,
22446             guessLanguage("foo.h", "[[clang::fallthrough]];"));
22447   EXPECT_EQ(FormatStyle::LK_ObjC,
22448             guessLanguage("foo.h", "[[clang:fallthrough] foo];"));
22449   EXPECT_EQ(FormatStyle::LK_Cpp,
22450             guessLanguage("foo.h", "[[gsl::suppress(\"type\")]];"));
22451   EXPECT_EQ(FormatStyle::LK_Cpp,
22452             guessLanguage("foo.h", "[[using clang: fallthrough]];"));
22453   EXPECT_EQ(FormatStyle::LK_ObjC,
22454             guessLanguage("foo.h", "[[abusing clang:fallthrough] bar];"));
22455   EXPECT_EQ(FormatStyle::LK_Cpp,
22456             guessLanguage("foo.h", "[[using gsl: suppress(\"type\")]];"));
22457   EXPECT_EQ(
22458       FormatStyle::LK_Cpp,
22459       guessLanguage("foo.h", "for (auto &&[endpoint, stream] : streams_)"));
22460   EXPECT_EQ(
22461       FormatStyle::LK_Cpp,
22462       guessLanguage("foo.h",
22463                     "[[clang::callable_when(\"unconsumed\", \"unknown\")]]"));
22464   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[foo::bar, ...]]"));
22465 }
22466 
22467 TEST_F(FormatTest, GuessLanguageWithCaret) {
22468   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^);"));
22469   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^, Bar);"));
22470   EXPECT_EQ(FormatStyle::LK_ObjC,
22471             guessLanguage("foo.h", "int(^)(char, float);"));
22472   EXPECT_EQ(FormatStyle::LK_ObjC,
22473             guessLanguage("foo.h", "int(^foo)(char, float);"));
22474   EXPECT_EQ(FormatStyle::LK_ObjC,
22475             guessLanguage("foo.h", "int(^foo[10])(char, float);"));
22476   EXPECT_EQ(FormatStyle::LK_ObjC,
22477             guessLanguage("foo.h", "int(^foo[kNumEntries])(char, float);"));
22478   EXPECT_EQ(
22479       FormatStyle::LK_ObjC,
22480       guessLanguage("foo.h", "int(^foo[(kNumEntries + 10)])(char, float);"));
22481 }
22482 
22483 TEST_F(FormatTest, GuessLanguageWithPragmas) {
22484   EXPECT_EQ(FormatStyle::LK_Cpp,
22485             guessLanguage("foo.h", "__pragma(warning(disable:))"));
22486   EXPECT_EQ(FormatStyle::LK_Cpp,
22487             guessLanguage("foo.h", "#pragma(warning(disable:))"));
22488   EXPECT_EQ(FormatStyle::LK_Cpp,
22489             guessLanguage("foo.h", "_Pragma(warning(disable:))"));
22490 }
22491 
22492 TEST_F(FormatTest, FormatsInlineAsmSymbolicNames) {
22493   // ASM symbolic names are identifiers that must be surrounded by [] without
22494   // space in between:
22495   // https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#InputOperands
22496 
22497   // Example from https://bugs.llvm.org/show_bug.cgi?id=45108.
22498   verifyFormat(R"(//
22499 asm volatile("mrs %x[result], FPCR" : [result] "=r"(result));
22500 )");
22501 
22502   // A list of several ASM symbolic names.
22503   verifyFormat(R"(asm("mov %[e], %[d]" : [d] "=rm"(d), [e] "rm"(*e));)");
22504 
22505   // ASM symbolic names in inline ASM with inputs and outputs.
22506   verifyFormat(R"(//
22507 asm("cmoveq %1, %2, %[result]"
22508     : [result] "=r"(result)
22509     : "r"(test), "r"(new), "[result]"(old));
22510 )");
22511 
22512   // ASM symbolic names in inline ASM with no outputs.
22513   verifyFormat(R"(asm("mov %[e], %[d]" : : [d] "=rm"(d), [e] "rm"(*e));)");
22514 }
22515 
22516 TEST_F(FormatTest, GuessedLanguageWithInlineAsmClobbers) {
22517   EXPECT_EQ(FormatStyle::LK_Cpp,
22518             guessLanguage("foo.h", "void f() {\n"
22519                                    "  asm (\"mov %[e], %[d]\"\n"
22520                                    "     : [d] \"=rm\" (d)\n"
22521                                    "       [e] \"rm\" (*e));\n"
22522                                    "}"));
22523   EXPECT_EQ(FormatStyle::LK_Cpp,
22524             guessLanguage("foo.h", "void f() {\n"
22525                                    "  _asm (\"mov %[e], %[d]\"\n"
22526                                    "     : [d] \"=rm\" (d)\n"
22527                                    "       [e] \"rm\" (*e));\n"
22528                                    "}"));
22529   EXPECT_EQ(FormatStyle::LK_Cpp,
22530             guessLanguage("foo.h", "void f() {\n"
22531                                    "  __asm (\"mov %[e], %[d]\"\n"
22532                                    "     : [d] \"=rm\" (d)\n"
22533                                    "       [e] \"rm\" (*e));\n"
22534                                    "}"));
22535   EXPECT_EQ(FormatStyle::LK_Cpp,
22536             guessLanguage("foo.h", "void f() {\n"
22537                                    "  __asm__ (\"mov %[e], %[d]\"\n"
22538                                    "     : [d] \"=rm\" (d)\n"
22539                                    "       [e] \"rm\" (*e));\n"
22540                                    "}"));
22541   EXPECT_EQ(FormatStyle::LK_Cpp,
22542             guessLanguage("foo.h", "void f() {\n"
22543                                    "  asm (\"mov %[e], %[d]\"\n"
22544                                    "     : [d] \"=rm\" (d),\n"
22545                                    "       [e] \"rm\" (*e));\n"
22546                                    "}"));
22547   EXPECT_EQ(FormatStyle::LK_Cpp,
22548             guessLanguage("foo.h", "void f() {\n"
22549                                    "  asm volatile (\"mov %[e], %[d]\"\n"
22550                                    "     : [d] \"=rm\" (d)\n"
22551                                    "       [e] \"rm\" (*e));\n"
22552                                    "}"));
22553 }
22554 
22555 TEST_F(FormatTest, GuessLanguageWithChildLines) {
22556   EXPECT_EQ(FormatStyle::LK_Cpp,
22557             guessLanguage("foo.h", "#define FOO ({ std::string s; })"));
22558   EXPECT_EQ(FormatStyle::LK_ObjC,
22559             guessLanguage("foo.h", "#define FOO ({ NSString *s; })"));
22560   EXPECT_EQ(
22561       FormatStyle::LK_Cpp,
22562       guessLanguage("foo.h", "#define FOO ({ foo(); ({ std::string s; }) })"));
22563   EXPECT_EQ(
22564       FormatStyle::LK_ObjC,
22565       guessLanguage("foo.h", "#define FOO ({ foo(); ({ NSString *s; }) })"));
22566 }
22567 
22568 TEST_F(FormatTest, TypenameMacros) {
22569   std::vector<std::string> TypenameMacros = {"STACK_OF", "LIST", "TAILQ_ENTRY"};
22570 
22571   // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=30353
22572   FormatStyle Google = getGoogleStyleWithColumns(0);
22573   Google.TypenameMacros = TypenameMacros;
22574   verifyFormat("struct foo {\n"
22575                "  int bar;\n"
22576                "  TAILQ_ENTRY(a) bleh;\n"
22577                "};",
22578                Google);
22579 
22580   FormatStyle Macros = getLLVMStyle();
22581   Macros.TypenameMacros = TypenameMacros;
22582 
22583   verifyFormat("STACK_OF(int) a;", Macros);
22584   verifyFormat("STACK_OF(int) *a;", Macros);
22585   verifyFormat("STACK_OF(int const *) *a;", Macros);
22586   verifyFormat("STACK_OF(int *const) *a;", Macros);
22587   verifyFormat("STACK_OF(int, string) a;", Macros);
22588   verifyFormat("STACK_OF(LIST(int)) a;", Macros);
22589   verifyFormat("STACK_OF(LIST(int)) a, b;", Macros);
22590   verifyFormat("for (LIST(int) *a = NULL; a;) {\n}", Macros);
22591   verifyFormat("STACK_OF(int) f(LIST(int) *arg);", Macros);
22592   verifyFormat("vector<LIST(uint64_t) *attr> x;", Macros);
22593   verifyFormat("vector<LIST(uint64_t) *const> f(LIST(uint64_t) *arg);", Macros);
22594 
22595   Macros.PointerAlignment = FormatStyle::PAS_Left;
22596   verifyFormat("STACK_OF(int)* a;", Macros);
22597   verifyFormat("STACK_OF(int*)* a;", Macros);
22598   verifyFormat("x = (STACK_OF(uint64_t))*a;", Macros);
22599   verifyFormat("x = (STACK_OF(uint64_t))&a;", Macros);
22600   verifyFormat("vector<STACK_OF(uint64_t)* attr> x;", Macros);
22601 }
22602 
22603 TEST_F(FormatTest, AtomicQualifier) {
22604   // Check that we treate _Atomic as a type and not a function call
22605   FormatStyle Google = getGoogleStyleWithColumns(0);
22606   verifyFormat("struct foo {\n"
22607                "  int a1;\n"
22608                "  _Atomic(a) a2;\n"
22609                "  _Atomic(_Atomic(int) *const) a3;\n"
22610                "};",
22611                Google);
22612   verifyFormat("_Atomic(uint64_t) a;");
22613   verifyFormat("_Atomic(uint64_t) *a;");
22614   verifyFormat("_Atomic(uint64_t const *) *a;");
22615   verifyFormat("_Atomic(uint64_t *const) *a;");
22616   verifyFormat("_Atomic(const uint64_t *) *a;");
22617   verifyFormat("_Atomic(uint64_t) a;");
22618   verifyFormat("_Atomic(_Atomic(uint64_t)) a;");
22619   verifyFormat("_Atomic(_Atomic(uint64_t)) a, b;");
22620   verifyFormat("for (_Atomic(uint64_t) *a = NULL; a;) {\n}");
22621   verifyFormat("_Atomic(uint64_t) f(_Atomic(uint64_t) *arg);");
22622 
22623   verifyFormat("_Atomic(uint64_t) *s(InitValue);");
22624   verifyFormat("_Atomic(uint64_t) *s{InitValue};");
22625   FormatStyle Style = getLLVMStyle();
22626   Style.PointerAlignment = FormatStyle::PAS_Left;
22627   verifyFormat("_Atomic(uint64_t)* s(InitValue);", Style);
22628   verifyFormat("_Atomic(uint64_t)* s{InitValue};", Style);
22629   verifyFormat("_Atomic(int)* a;", Style);
22630   verifyFormat("_Atomic(int*)* a;", Style);
22631   verifyFormat("vector<_Atomic(uint64_t)* attr> x;", Style);
22632 
22633   Style.SpacesInCStyleCastParentheses = true;
22634   Style.SpacesInParentheses = false;
22635   verifyFormat("x = ( _Atomic(uint64_t) )*a;", Style);
22636   Style.SpacesInCStyleCastParentheses = false;
22637   Style.SpacesInParentheses = true;
22638   verifyFormat("x = (_Atomic( uint64_t ))*a;", Style);
22639   verifyFormat("x = (_Atomic( uint64_t ))&a;", Style);
22640 }
22641 
22642 TEST_F(FormatTest, AmbersandInLamda) {
22643   // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=41899
22644   FormatStyle AlignStyle = getLLVMStyle();
22645   AlignStyle.PointerAlignment = FormatStyle::PAS_Left;
22646   verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle);
22647   AlignStyle.PointerAlignment = FormatStyle::PAS_Right;
22648   verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle);
22649 }
22650 
22651 TEST_F(FormatTest, SpacesInConditionalStatement) {
22652   FormatStyle Spaces = getLLVMStyle();
22653   Spaces.IfMacros.clear();
22654   Spaces.IfMacros.push_back("MYIF");
22655   Spaces.SpacesInConditionalStatement = true;
22656   verifyFormat("for ( int i = 0; i; i++ )\n  continue;", Spaces);
22657   verifyFormat("if ( !a )\n  return;", Spaces);
22658   verifyFormat("if ( a )\n  return;", Spaces);
22659   verifyFormat("if constexpr ( a )\n  return;", Spaces);
22660   verifyFormat("MYIF ( a )\n  return;", Spaces);
22661   verifyFormat("MYIF ( a )\n  return;\nelse MYIF ( b )\n  return;", Spaces);
22662   verifyFormat("MYIF ( a )\n  return;\nelse\n  return;", Spaces);
22663   verifyFormat("switch ( a )\ncase 1:\n  return;", Spaces);
22664   verifyFormat("while ( a )\n  return;", Spaces);
22665   verifyFormat("while ( (a && b) )\n  return;", Spaces);
22666   verifyFormat("do {\n} while ( 1 != 0 );", Spaces);
22667   verifyFormat("try {\n} catch ( const std::exception & ) {\n}", Spaces);
22668   // Check that space on the left of "::" is inserted as expected at beginning
22669   // of condition.
22670   verifyFormat("while ( ::func() )\n  return;", Spaces);
22671 
22672   // Check impact of ControlStatementsExceptControlMacros is honored.
22673   Spaces.SpaceBeforeParens =
22674       FormatStyle::SBPO_ControlStatementsExceptControlMacros;
22675   verifyFormat("MYIF( a )\n  return;", Spaces);
22676   verifyFormat("MYIF( a )\n  return;\nelse MYIF( b )\n  return;", Spaces);
22677   verifyFormat("MYIF( a )\n  return;\nelse\n  return;", Spaces);
22678 }
22679 
22680 TEST_F(FormatTest, AlternativeOperators) {
22681   // Test case for ensuring alternate operators are not
22682   // combined with their right most neighbour.
22683   verifyFormat("int a and b;");
22684   verifyFormat("int a and_eq b;");
22685   verifyFormat("int a bitand b;");
22686   verifyFormat("int a bitor b;");
22687   verifyFormat("int a compl b;");
22688   verifyFormat("int a not b;");
22689   verifyFormat("int a not_eq b;");
22690   verifyFormat("int a or b;");
22691   verifyFormat("int a xor b;");
22692   verifyFormat("int a xor_eq b;");
22693   verifyFormat("return this not_eq bitand other;");
22694   verifyFormat("bool operator not_eq(const X bitand other)");
22695 
22696   verifyFormat("int a and 5;");
22697   verifyFormat("int a and_eq 5;");
22698   verifyFormat("int a bitand 5;");
22699   verifyFormat("int a bitor 5;");
22700   verifyFormat("int a compl 5;");
22701   verifyFormat("int a not 5;");
22702   verifyFormat("int a not_eq 5;");
22703   verifyFormat("int a or 5;");
22704   verifyFormat("int a xor 5;");
22705   verifyFormat("int a xor_eq 5;");
22706 
22707   verifyFormat("int a compl(5);");
22708   verifyFormat("int a not(5);");
22709 
22710   /* FIXME handle alternate tokens
22711    * https://en.cppreference.com/w/cpp/language/operator_alternative
22712   // alternative tokens
22713   verifyFormat("compl foo();");     //  ~foo();
22714   verifyFormat("foo() <%%>;");      // foo();
22715   verifyFormat("void foo() <%%>;"); // void foo(){}
22716   verifyFormat("int a <:1:>;");     // int a[1];[
22717   verifyFormat("%:define ABC abc"); // #define ABC abc
22718   verifyFormat("%:%:");             // ##
22719   */
22720 }
22721 
22722 TEST_F(FormatTest, STLWhileNotDefineChed) {
22723   verifyFormat("#if defined(while)\n"
22724                "#define while EMIT WARNING C4005\n"
22725                "#endif // while");
22726 }
22727 
22728 TEST_F(FormatTest, OperatorSpacing) {
22729   FormatStyle Style = getLLVMStyle();
22730   Style.PointerAlignment = FormatStyle::PAS_Right;
22731   verifyFormat("Foo::operator*();", Style);
22732   verifyFormat("Foo::operator void *();", Style);
22733   verifyFormat("Foo::operator void **();", Style);
22734   verifyFormat("Foo::operator void *&();", Style);
22735   verifyFormat("Foo::operator void *&&();", Style);
22736   verifyFormat("Foo::operator void const *();", Style);
22737   verifyFormat("Foo::operator void const **();", Style);
22738   verifyFormat("Foo::operator void const *&();", Style);
22739   verifyFormat("Foo::operator void const *&&();", Style);
22740   verifyFormat("Foo::operator()(void *);", Style);
22741   verifyFormat("Foo::operator*(void *);", Style);
22742   verifyFormat("Foo::operator*();", Style);
22743   verifyFormat("Foo::operator**();", Style);
22744   verifyFormat("Foo::operator&();", Style);
22745   verifyFormat("Foo::operator<int> *();", Style);
22746   verifyFormat("Foo::operator<Foo> *();", Style);
22747   verifyFormat("Foo::operator<int> **();", Style);
22748   verifyFormat("Foo::operator<Foo> **();", Style);
22749   verifyFormat("Foo::operator<int> &();", Style);
22750   verifyFormat("Foo::operator<Foo> &();", Style);
22751   verifyFormat("Foo::operator<int> &&();", Style);
22752   verifyFormat("Foo::operator<Foo> &&();", Style);
22753   verifyFormat("Foo::operator<int> *&();", Style);
22754   verifyFormat("Foo::operator<Foo> *&();", Style);
22755   verifyFormat("Foo::operator<int> *&&();", Style);
22756   verifyFormat("Foo::operator<Foo> *&&();", Style);
22757   verifyFormat("operator*(int (*)(), class Foo);", Style);
22758 
22759   verifyFormat("Foo::operator&();", Style);
22760   verifyFormat("Foo::operator void &();", Style);
22761   verifyFormat("Foo::operator void const &();", Style);
22762   verifyFormat("Foo::operator()(void &);", Style);
22763   verifyFormat("Foo::operator&(void &);", Style);
22764   verifyFormat("Foo::operator&();", Style);
22765   verifyFormat("operator&(int (&)(), class Foo);", Style);
22766   verifyFormat("operator&&(int (&)(), class Foo);", Style);
22767 
22768   verifyFormat("Foo::operator&&();", Style);
22769   verifyFormat("Foo::operator**();", Style);
22770   verifyFormat("Foo::operator void &&();", Style);
22771   verifyFormat("Foo::operator void const &&();", Style);
22772   verifyFormat("Foo::operator()(void &&);", Style);
22773   verifyFormat("Foo::operator&&(void &&);", Style);
22774   verifyFormat("Foo::operator&&();", Style);
22775   verifyFormat("operator&&(int (&&)(), class Foo);", Style);
22776   verifyFormat("operator const nsTArrayRight<E> &()", Style);
22777   verifyFormat("[[nodiscard]] operator const nsTArrayRight<E, Allocator> &()",
22778                Style);
22779   verifyFormat("operator void **()", Style);
22780   verifyFormat("operator const FooRight<Object> &()", Style);
22781   verifyFormat("operator const FooRight<Object> *()", Style);
22782   verifyFormat("operator const FooRight<Object> **()", Style);
22783   verifyFormat("operator const FooRight<Object> *&()", Style);
22784   verifyFormat("operator const FooRight<Object> *&&()", Style);
22785 
22786   Style.PointerAlignment = FormatStyle::PAS_Left;
22787   verifyFormat("Foo::operator*();", Style);
22788   verifyFormat("Foo::operator**();", Style);
22789   verifyFormat("Foo::operator void*();", Style);
22790   verifyFormat("Foo::operator void**();", Style);
22791   verifyFormat("Foo::operator void*&();", Style);
22792   verifyFormat("Foo::operator void*&&();", Style);
22793   verifyFormat("Foo::operator void const*();", Style);
22794   verifyFormat("Foo::operator void const**();", Style);
22795   verifyFormat("Foo::operator void const*&();", Style);
22796   verifyFormat("Foo::operator void const*&&();", Style);
22797   verifyFormat("Foo::operator/*comment*/ void*();", Style);
22798   verifyFormat("Foo::operator/*a*/ const /*b*/ void*();", Style);
22799   verifyFormat("Foo::operator/*a*/ volatile /*b*/ void*();", Style);
22800   verifyFormat("Foo::operator()(void*);", Style);
22801   verifyFormat("Foo::operator*(void*);", Style);
22802   verifyFormat("Foo::operator*();", Style);
22803   verifyFormat("Foo::operator<int>*();", Style);
22804   verifyFormat("Foo::operator<Foo>*();", Style);
22805   verifyFormat("Foo::operator<int>**();", Style);
22806   verifyFormat("Foo::operator<Foo>**();", Style);
22807   verifyFormat("Foo::operator<Foo>*&();", Style);
22808   verifyFormat("Foo::operator<int>&();", Style);
22809   verifyFormat("Foo::operator<Foo>&();", Style);
22810   verifyFormat("Foo::operator<int>&&();", Style);
22811   verifyFormat("Foo::operator<Foo>&&();", Style);
22812   verifyFormat("Foo::operator<int>*&();", Style);
22813   verifyFormat("Foo::operator<Foo>*&();", Style);
22814   verifyFormat("operator*(int (*)(), class Foo);", Style);
22815 
22816   verifyFormat("Foo::operator&();", Style);
22817   verifyFormat("Foo::operator void&();", Style);
22818   verifyFormat("Foo::operator void const&();", Style);
22819   verifyFormat("Foo::operator/*comment*/ void&();", Style);
22820   verifyFormat("Foo::operator/*a*/ const /*b*/ void&();", Style);
22821   verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&();", Style);
22822   verifyFormat("Foo::operator()(void&);", Style);
22823   verifyFormat("Foo::operator&(void&);", Style);
22824   verifyFormat("Foo::operator&();", Style);
22825   verifyFormat("operator&(int (&)(), class Foo);", Style);
22826   verifyFormat("operator&(int (&&)(), class Foo);", Style);
22827   verifyFormat("operator&&(int (&&)(), class Foo);", Style);
22828 
22829   verifyFormat("Foo::operator&&();", Style);
22830   verifyFormat("Foo::operator void&&();", Style);
22831   verifyFormat("Foo::operator void const&&();", Style);
22832   verifyFormat("Foo::operator/*comment*/ void&&();", Style);
22833   verifyFormat("Foo::operator/*a*/ const /*b*/ void&&();", Style);
22834   verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&&();", Style);
22835   verifyFormat("Foo::operator()(void&&);", Style);
22836   verifyFormat("Foo::operator&&(void&&);", Style);
22837   verifyFormat("Foo::operator&&();", Style);
22838   verifyFormat("operator&&(int (&&)(), class Foo);", Style);
22839   verifyFormat("operator const nsTArrayLeft<E>&()", Style);
22840   verifyFormat("[[nodiscard]] operator const nsTArrayLeft<E, Allocator>&()",
22841                Style);
22842   verifyFormat("operator void**()", Style);
22843   verifyFormat("operator const FooLeft<Object>&()", Style);
22844   verifyFormat("operator const FooLeft<Object>*()", Style);
22845   verifyFormat("operator const FooLeft<Object>**()", Style);
22846   verifyFormat("operator const FooLeft<Object>*&()", Style);
22847   verifyFormat("operator const FooLeft<Object>*&&()", Style);
22848 
22849   // PR45107
22850   verifyFormat("operator Vector<String>&();", Style);
22851   verifyFormat("operator const Vector<String>&();", Style);
22852   verifyFormat("operator foo::Bar*();", Style);
22853   verifyFormat("operator const Foo<X>::Bar<Y>*();", Style);
22854   verifyFormat("operator/*a*/ const /*b*/ Foo /*c*/<X> /*d*/ ::Bar<Y>*();",
22855                Style);
22856 
22857   Style.PointerAlignment = FormatStyle::PAS_Middle;
22858   verifyFormat("Foo::operator*();", Style);
22859   verifyFormat("Foo::operator void *();", Style);
22860   verifyFormat("Foo::operator()(void *);", Style);
22861   verifyFormat("Foo::operator*(void *);", Style);
22862   verifyFormat("Foo::operator*();", Style);
22863   verifyFormat("operator*(int (*)(), class Foo);", Style);
22864 
22865   verifyFormat("Foo::operator&();", Style);
22866   verifyFormat("Foo::operator void &();", Style);
22867   verifyFormat("Foo::operator void const &();", Style);
22868   verifyFormat("Foo::operator()(void &);", Style);
22869   verifyFormat("Foo::operator&(void &);", Style);
22870   verifyFormat("Foo::operator&();", Style);
22871   verifyFormat("operator&(int (&)(), class Foo);", Style);
22872 
22873   verifyFormat("Foo::operator&&();", Style);
22874   verifyFormat("Foo::operator void &&();", Style);
22875   verifyFormat("Foo::operator void const &&();", Style);
22876   verifyFormat("Foo::operator()(void &&);", Style);
22877   verifyFormat("Foo::operator&&(void &&);", Style);
22878   verifyFormat("Foo::operator&&();", Style);
22879   verifyFormat("operator&&(int (&&)(), class Foo);", Style);
22880 }
22881 
22882 TEST_F(FormatTest, OperatorPassedAsAFunctionPtr) {
22883   FormatStyle Style = getLLVMStyle();
22884   // PR46157
22885   verifyFormat("foo(operator+, -42);", Style);
22886   verifyFormat("foo(operator++, -42);", Style);
22887   verifyFormat("foo(operator--, -42);", Style);
22888   verifyFormat("foo(-42, operator--);", Style);
22889   verifyFormat("foo(-42, operator, );", Style);
22890   verifyFormat("foo(operator, , -42);", Style);
22891 }
22892 
22893 TEST_F(FormatTest, WhitespaceSensitiveMacros) {
22894   FormatStyle Style = getLLVMStyle();
22895   Style.WhitespaceSensitiveMacros.push_back("FOO");
22896 
22897   // Don't use the helpers here, since 'mess up' will change the whitespace
22898   // and these are all whitespace sensitive by definition
22899   EXPECT_EQ("FOO(String-ized&Messy+But(: :Still)=Intentional);",
22900             format("FOO(String-ized&Messy+But(: :Still)=Intentional);", Style));
22901   EXPECT_EQ(
22902       "FOO(String-ized&Messy+But\\(: :Still)=Intentional);",
22903       format("FOO(String-ized&Messy+But\\(: :Still)=Intentional);", Style));
22904   EXPECT_EQ("FOO(String-ized&Messy+But,: :Still=Intentional);",
22905             format("FOO(String-ized&Messy+But,: :Still=Intentional);", Style));
22906   EXPECT_EQ("FOO(String-ized&Messy+But,: :\n"
22907             "       Still=Intentional);",
22908             format("FOO(String-ized&Messy+But,: :\n"
22909                    "       Still=Intentional);",
22910                    Style));
22911   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
22912   EXPECT_EQ("FOO(String-ized=&Messy+But,: :\n"
22913             "       Still=Intentional);",
22914             format("FOO(String-ized=&Messy+But,: :\n"
22915                    "       Still=Intentional);",
22916                    Style));
22917 
22918   Style.ColumnLimit = 21;
22919   EXPECT_EQ("FOO(String-ized&Messy+But: :Still=Intentional);",
22920             format("FOO(String-ized&Messy+But: :Still=Intentional);", Style));
22921 }
22922 
22923 TEST_F(FormatTest, VeryLongNamespaceCommentSplit) {
22924   // These tests are not in NamespaceFixer because that doesn't
22925   // test its interaction with line wrapping
22926   FormatStyle Style = getLLVMStyleWithColumns(80);
22927   verifyFormat("namespace {\n"
22928                "int i;\n"
22929                "int j;\n"
22930                "} // namespace",
22931                Style);
22932 
22933   verifyFormat("namespace AAA {\n"
22934                "int i;\n"
22935                "int j;\n"
22936                "} // namespace AAA",
22937                Style);
22938 
22939   EXPECT_EQ("namespace Averyveryveryverylongnamespace {\n"
22940             "int i;\n"
22941             "int j;\n"
22942             "} // namespace Averyveryveryverylongnamespace",
22943             format("namespace Averyveryveryverylongnamespace {\n"
22944                    "int i;\n"
22945                    "int j;\n"
22946                    "}",
22947                    Style));
22948 
22949   EXPECT_EQ(
22950       "namespace "
22951       "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n"
22952       "    went::mad::now {\n"
22953       "int i;\n"
22954       "int j;\n"
22955       "} // namespace\n"
22956       "  // "
22957       "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::"
22958       "went::mad::now",
22959       format("namespace "
22960              "would::it::save::you::a::lot::of::time::if_::i::"
22961              "just::gave::up::and_::went::mad::now {\n"
22962              "int i;\n"
22963              "int j;\n"
22964              "}",
22965              Style));
22966 
22967   // This used to duplicate the comment again and again on subsequent runs
22968   EXPECT_EQ(
22969       "namespace "
22970       "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n"
22971       "    went::mad::now {\n"
22972       "int i;\n"
22973       "int j;\n"
22974       "} // namespace\n"
22975       "  // "
22976       "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::"
22977       "went::mad::now",
22978       format("namespace "
22979              "would::it::save::you::a::lot::of::time::if_::i::"
22980              "just::gave::up::and_::went::mad::now {\n"
22981              "int i;\n"
22982              "int j;\n"
22983              "} // namespace\n"
22984              "  // "
22985              "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::"
22986              "and_::went::mad::now",
22987              Style));
22988 }
22989 
22990 TEST_F(FormatTest, LikelyUnlikely) {
22991   FormatStyle Style = getLLVMStyle();
22992 
22993   verifyFormat("if (argc > 5) [[unlikely]] {\n"
22994                "  return 29;\n"
22995                "}",
22996                Style);
22997 
22998   verifyFormat("if (argc > 5) [[likely]] {\n"
22999                "  return 29;\n"
23000                "}",
23001                Style);
23002 
23003   verifyFormat("if (argc > 5) [[unlikely]] {\n"
23004                "  return 29;\n"
23005                "} else [[likely]] {\n"
23006                "  return 42;\n"
23007                "}\n",
23008                Style);
23009 
23010   verifyFormat("if (argc > 5) [[unlikely]] {\n"
23011                "  return 29;\n"
23012                "} else if (argc > 10) [[likely]] {\n"
23013                "  return 99;\n"
23014                "} else {\n"
23015                "  return 42;\n"
23016                "}\n",
23017                Style);
23018 
23019   verifyFormat("if (argc > 5) [[gnu::unused]] {\n"
23020                "  return 29;\n"
23021                "}",
23022                Style);
23023 
23024   verifyFormat("if (argc > 5) [[unlikely]]\n"
23025                "  return 29;\n",
23026                Style);
23027   verifyFormat("if (argc > 5) [[likely]]\n"
23028                "  return 29;\n",
23029                Style);
23030 
23031   Style.AttributeMacros.push_back("UNLIKELY");
23032   Style.AttributeMacros.push_back("LIKELY");
23033   verifyFormat("if (argc > 5) UNLIKELY\n"
23034                "  return 29;\n",
23035                Style);
23036 
23037   verifyFormat("if (argc > 5) UNLIKELY {\n"
23038                "  return 29;\n"
23039                "}",
23040                Style);
23041   verifyFormat("if (argc > 5) UNLIKELY {\n"
23042                "  return 29;\n"
23043                "} else [[likely]] {\n"
23044                "  return 42;\n"
23045                "}\n",
23046                Style);
23047   verifyFormat("if (argc > 5) UNLIKELY {\n"
23048                "  return 29;\n"
23049                "} else LIKELY {\n"
23050                "  return 42;\n"
23051                "}\n",
23052                Style);
23053   verifyFormat("if (argc > 5) [[unlikely]] {\n"
23054                "  return 29;\n"
23055                "} else LIKELY {\n"
23056                "  return 42;\n"
23057                "}\n",
23058                Style);
23059 }
23060 
23061 TEST_F(FormatTest, PenaltyIndentedWhitespace) {
23062   verifyFormat("Constructor()\n"
23063                "    : aaaaaa(aaaaaa), aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
23064                "                          aaaa(aaaaaaaaaaaaaaaaaa, "
23065                "aaaaaaaaaaaaaaaaaat))");
23066   verifyFormat("Constructor()\n"
23067                "    : aaaaaaaaaaaaa(aaaaaa), "
23068                "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)");
23069 
23070   FormatStyle StyleWithWhitespacePenalty = getLLVMStyle();
23071   StyleWithWhitespacePenalty.PenaltyIndentedWhitespace = 5;
23072   verifyFormat("Constructor()\n"
23073                "    : aaaaaa(aaaaaa),\n"
23074                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
23075                "          aaaa(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaat))",
23076                StyleWithWhitespacePenalty);
23077   verifyFormat("Constructor()\n"
23078                "    : aaaaaaaaaaaaa(aaaaaa), "
23079                "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)",
23080                StyleWithWhitespacePenalty);
23081 }
23082 
23083 TEST_F(FormatTest, LLVMDefaultStyle) {
23084   FormatStyle Style = getLLVMStyle();
23085   verifyFormat("extern \"C\" {\n"
23086                "int foo();\n"
23087                "}",
23088                Style);
23089 }
23090 TEST_F(FormatTest, GNUDefaultStyle) {
23091   FormatStyle Style = getGNUStyle();
23092   verifyFormat("extern \"C\"\n"
23093                "{\n"
23094                "  int foo ();\n"
23095                "}",
23096                Style);
23097 }
23098 TEST_F(FormatTest, MozillaDefaultStyle) {
23099   FormatStyle Style = getMozillaStyle();
23100   verifyFormat("extern \"C\"\n"
23101                "{\n"
23102                "  int foo();\n"
23103                "}",
23104                Style);
23105 }
23106 TEST_F(FormatTest, GoogleDefaultStyle) {
23107   FormatStyle Style = getGoogleStyle();
23108   verifyFormat("extern \"C\" {\n"
23109                "int foo();\n"
23110                "}",
23111                Style);
23112 }
23113 TEST_F(FormatTest, ChromiumDefaultStyle) {
23114   FormatStyle Style = getChromiumStyle(FormatStyle::LanguageKind::LK_Cpp);
23115   verifyFormat("extern \"C\" {\n"
23116                "int foo();\n"
23117                "}",
23118                Style);
23119 }
23120 TEST_F(FormatTest, MicrosoftDefaultStyle) {
23121   FormatStyle Style = getMicrosoftStyle(FormatStyle::LanguageKind::LK_Cpp);
23122   verifyFormat("extern \"C\"\n"
23123                "{\n"
23124                "    int foo();\n"
23125                "}",
23126                Style);
23127 }
23128 TEST_F(FormatTest, WebKitDefaultStyle) {
23129   FormatStyle Style = getWebKitStyle();
23130   verifyFormat("extern \"C\" {\n"
23131                "int foo();\n"
23132                "}",
23133                Style);
23134 }
23135 
23136 TEST_F(FormatTest, ConceptsAndRequires) {
23137   FormatStyle Style = getLLVMStyle();
23138   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
23139 
23140   verifyFormat("template <typename T>\n"
23141                "concept Hashable = requires(T a) {\n"
23142                "  { std::hash<T>{}(a) } -> std::convertible_to<std::size_t>;\n"
23143                "};",
23144                Style);
23145   verifyFormat("template <typename T>\n"
23146                "concept EqualityComparable = requires(T a, T b) {\n"
23147                "  { a == b } -> bool;\n"
23148                "};",
23149                Style);
23150   verifyFormat("template <typename T>\n"
23151                "concept EqualityComparable = requires(T a, T b) {\n"
23152                "  { a == b } -> bool;\n"
23153                "  { a != b } -> bool;\n"
23154                "};",
23155                Style);
23156   verifyFormat("template <typename T>\n"
23157                "concept EqualityComparable = requires(T a, T b) {\n"
23158                "  { a == b } -> bool;\n"
23159                "  { a != b } -> bool;\n"
23160                "};",
23161                Style);
23162 
23163   verifyFormat("template <typename It>\n"
23164                "requires Iterator<It>\n"
23165                "void sort(It begin, It end) {\n"
23166                "  //....\n"
23167                "}",
23168                Style);
23169 
23170   verifyFormat("template <typename T>\n"
23171                "concept Large = sizeof(T) > 10;",
23172                Style);
23173 
23174   verifyFormat("template <typename T, typename U>\n"
23175                "concept FooableWith = requires(T t, U u) {\n"
23176                "  typename T::foo_type;\n"
23177                "  { t.foo(u) } -> typename T::foo_type;\n"
23178                "  t++;\n"
23179                "};\n"
23180                "void doFoo(FooableWith<int> auto t) {\n"
23181                "  t.foo(3);\n"
23182                "}",
23183                Style);
23184   verifyFormat("template <typename T>\n"
23185                "concept Context = sizeof(T) == 1;",
23186                Style);
23187   verifyFormat("template <typename T>\n"
23188                "concept Context = is_specialization_of_v<context, T>;",
23189                Style);
23190   verifyFormat("template <typename T>\n"
23191                "concept Node = std::is_object_v<T>;",
23192                Style);
23193   verifyFormat("template <typename T>\n"
23194                "concept Tree = true;",
23195                Style);
23196 
23197   verifyFormat("template <typename T> int g(T i) requires Concept1<I> {\n"
23198                "  //...\n"
23199                "}",
23200                Style);
23201 
23202   verifyFormat(
23203       "template <typename T> int g(T i) requires Concept1<I> && Concept2<I> {\n"
23204       "  //...\n"
23205       "}",
23206       Style);
23207 
23208   verifyFormat(
23209       "template <typename T> int g(T i) requires Concept1<I> || Concept2<I> {\n"
23210       "  //...\n"
23211       "}",
23212       Style);
23213 
23214   verifyFormat("template <typename T>\n"
23215                "veryveryvery_long_return_type g(T i) requires Concept1<I> || "
23216                "Concept2<I> {\n"
23217                "  //...\n"
23218                "}",
23219                Style);
23220 
23221   verifyFormat("template <typename T>\n"
23222                "veryveryvery_long_return_type g(T i) requires Concept1<I> && "
23223                "Concept2<I> {\n"
23224                "  //...\n"
23225                "}",
23226                Style);
23227 
23228   verifyFormat(
23229       "template <typename T>\n"
23230       "veryveryvery_long_return_type g(T i) requires Concept1 && Concept2 {\n"
23231       "  //...\n"
23232       "}",
23233       Style);
23234 
23235   verifyFormat(
23236       "template <typename T>\n"
23237       "veryveryvery_long_return_type g(T i) requires Concept1 || Concept2 {\n"
23238       "  //...\n"
23239       "}",
23240       Style);
23241 
23242   verifyFormat("template <typename It>\n"
23243                "requires Foo<It>() && Bar<It> {\n"
23244                "  //....\n"
23245                "}",
23246                Style);
23247 
23248   verifyFormat("template <typename It>\n"
23249                "requires Foo<Bar<It>>() && Bar<Foo<It, It>> {\n"
23250                "  //....\n"
23251                "}",
23252                Style);
23253 
23254   verifyFormat("template <typename It>\n"
23255                "requires Foo<Bar<It, It>>() && Bar<Foo<It, It>> {\n"
23256                "  //....\n"
23257                "}",
23258                Style);
23259 
23260   verifyFormat(
23261       "template <typename It>\n"
23262       "requires Foo<Bar<It>, Baz<It>>() && Bar<Foo<It>, Baz<It, It>> {\n"
23263       "  //....\n"
23264       "}",
23265       Style);
23266 
23267   Style.IndentRequires = true;
23268   verifyFormat("template <typename It>\n"
23269                "  requires Iterator<It>\n"
23270                "void sort(It begin, It end) {\n"
23271                "  //....\n"
23272                "}",
23273                Style);
23274   verifyFormat("template <std::size index_>\n"
23275                "  requires(index_ < sizeof...(Children_))\n"
23276                "Tree auto &child() {\n"
23277                "  // ...\n"
23278                "}",
23279                Style);
23280 
23281   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
23282   verifyFormat("template <typename T>\n"
23283                "concept Hashable = requires (T a) {\n"
23284                "  { std::hash<T>{}(a) } -> std::convertible_to<std::size_t>;\n"
23285                "};",
23286                Style);
23287 
23288   verifyFormat("template <class T = void>\n"
23289                "  requires EqualityComparable<T> || Same<T, void>\n"
23290                "struct equal_to;",
23291                Style);
23292 
23293   verifyFormat("template <class T>\n"
23294                "  requires requires {\n"
23295                "    T{};\n"
23296                "    T (int);\n"
23297                "  }\n",
23298                Style);
23299 
23300   Style.ColumnLimit = 78;
23301   verifyFormat("template <typename T>\n"
23302                "concept Context = Traits<typename T::traits_type> and\n"
23303                "    Interface<typename T::interface_type> and\n"
23304                "    Request<typename T::request_type> and\n"
23305                "    Response<typename T::response_type> and\n"
23306                "    ContextExtension<typename T::extension_type> and\n"
23307                "    ::std::is_copy_constructable<T> and "
23308                "::std::is_move_constructable<T> and\n"
23309                "    requires (T c) {\n"
23310                "  { c.response; } -> Response;\n"
23311                "} and requires (T c) {\n"
23312                "  { c.request; } -> Request;\n"
23313                "}\n",
23314                Style);
23315 
23316   verifyFormat("template <typename T>\n"
23317                "concept Context = Traits<typename T::traits_type> or\n"
23318                "    Interface<typename T::interface_type> or\n"
23319                "    Request<typename T::request_type> or\n"
23320                "    Response<typename T::response_type> or\n"
23321                "    ContextExtension<typename T::extension_type> or\n"
23322                "    ::std::is_copy_constructable<T> or "
23323                "::std::is_move_constructable<T> or\n"
23324                "    requires (T c) {\n"
23325                "  { c.response; } -> Response;\n"
23326                "} or requires (T c) {\n"
23327                "  { c.request; } -> Request;\n"
23328                "}\n",
23329                Style);
23330 
23331   verifyFormat("template <typename T>\n"
23332                "concept Context = Traits<typename T::traits_type> &&\n"
23333                "    Interface<typename T::interface_type> &&\n"
23334                "    Request<typename T::request_type> &&\n"
23335                "    Response<typename T::response_type> &&\n"
23336                "    ContextExtension<typename T::extension_type> &&\n"
23337                "    ::std::is_copy_constructable<T> && "
23338                "::std::is_move_constructable<T> &&\n"
23339                "    requires (T c) {\n"
23340                "  { c.response; } -> Response;\n"
23341                "} && requires (T c) {\n"
23342                "  { c.request; } -> Request;\n"
23343                "}\n",
23344                Style);
23345 
23346   verifyFormat("template <typename T>\nconcept someConcept = Constraint1<T> && "
23347                "Constraint2<T>;");
23348 
23349   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
23350   Style.BraceWrapping.AfterFunction = true;
23351   Style.BraceWrapping.AfterClass = true;
23352   Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
23353   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
23354   verifyFormat("void Foo () requires (std::copyable<T>)\n"
23355                "{\n"
23356                "  return\n"
23357                "}\n",
23358                Style);
23359 
23360   verifyFormat("void Foo () requires std::copyable<T>\n"
23361                "{\n"
23362                "  return\n"
23363                "}\n",
23364                Style);
23365 
23366   verifyFormat("template <std::semiregular F, std::semiregular... Args>\n"
23367                "  requires (std::invocable<F, std::invoke_result_t<Args>...>)\n"
23368                "struct constant;",
23369                Style);
23370 
23371   verifyFormat("template <std::semiregular F, std::semiregular... Args>\n"
23372                "  requires std::invocable<F, std::invoke_result_t<Args>...>\n"
23373                "struct constant;",
23374                Style);
23375 
23376   verifyFormat("template <class T>\n"
23377                "class plane_with_very_very_very_long_name\n"
23378                "{\n"
23379                "  constexpr plane_with_very_very_very_long_name () requires "
23380                "std::copyable<T>\n"
23381                "      : plane_with_very_very_very_long_name (1)\n"
23382                "  {\n"
23383                "  }\n"
23384                "}\n",
23385                Style);
23386 
23387   verifyFormat("template <class T>\n"
23388                "class plane_with_long_name\n"
23389                "{\n"
23390                "  constexpr plane_with_long_name () requires std::copyable<T>\n"
23391                "      : plane_with_long_name (1)\n"
23392                "  {\n"
23393                "  }\n"
23394                "}\n",
23395                Style);
23396 
23397   Style.BreakBeforeConceptDeclarations = false;
23398   verifyFormat("template <typename T> concept Tree = true;", Style);
23399 
23400   Style.IndentRequires = false;
23401   verifyFormat("template <std::semiregular F, std::semiregular... Args>\n"
23402                "requires (std::invocable<F, std::invoke_result_t<Args>...>) "
23403                "struct constant;",
23404                Style);
23405 }
23406 
23407 TEST_F(FormatTest, StatementAttributeLikeMacros) {
23408   FormatStyle Style = getLLVMStyle();
23409   StringRef Source = "void Foo::slot() {\n"
23410                      "  unsigned char MyChar = 'x';\n"
23411                      "  emit signal(MyChar);\n"
23412                      "  Q_EMIT signal(MyChar);\n"
23413                      "}";
23414 
23415   EXPECT_EQ(Source, format(Source, Style));
23416 
23417   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
23418   EXPECT_EQ("void Foo::slot() {\n"
23419             "  unsigned char MyChar = 'x';\n"
23420             "  emit          signal(MyChar);\n"
23421             "  Q_EMIT signal(MyChar);\n"
23422             "}",
23423             format(Source, Style));
23424 
23425   Style.StatementAttributeLikeMacros.push_back("emit");
23426   EXPECT_EQ(Source, format(Source, Style));
23427 
23428   Style.StatementAttributeLikeMacros = {};
23429   EXPECT_EQ("void Foo::slot() {\n"
23430             "  unsigned char MyChar = 'x';\n"
23431             "  emit          signal(MyChar);\n"
23432             "  Q_EMIT        signal(MyChar);\n"
23433             "}",
23434             format(Source, Style));
23435 }
23436 
23437 TEST_F(FormatTest, IndentAccessModifiers) {
23438   FormatStyle Style = getLLVMStyle();
23439   Style.IndentAccessModifiers = true;
23440   // Members are *two* levels below the record;
23441   // Style.IndentWidth == 2, thus yielding a 4 spaces wide indentation.
23442   verifyFormat("class C {\n"
23443                "    int i;\n"
23444                "};\n",
23445                Style);
23446   verifyFormat("union C {\n"
23447                "    int i;\n"
23448                "    unsigned u;\n"
23449                "};\n",
23450                Style);
23451   // Access modifiers should be indented one level below the record.
23452   verifyFormat("class C {\n"
23453                "  public:\n"
23454                "    int i;\n"
23455                "};\n",
23456                Style);
23457   verifyFormat("struct S {\n"
23458                "  private:\n"
23459                "    class C {\n"
23460                "        int j;\n"
23461                "\n"
23462                "      public:\n"
23463                "        C();\n"
23464                "    };\n"
23465                "\n"
23466                "  public:\n"
23467                "    int i;\n"
23468                "};\n",
23469                Style);
23470   // Enumerations are not records and should be unaffected.
23471   Style.AllowShortEnumsOnASingleLine = false;
23472   verifyFormat("enum class E {\n"
23473                "  A,\n"
23474                "  B\n"
23475                "};\n",
23476                Style);
23477   // Test with a different indentation width;
23478   // also proves that the result is Style.AccessModifierOffset agnostic.
23479   Style.IndentWidth = 3;
23480   verifyFormat("class C {\n"
23481                "   public:\n"
23482                "      int i;\n"
23483                "};\n",
23484                Style);
23485 }
23486 
23487 TEST_F(FormatTest, LimitlessStringsAndComments) {
23488   auto Style = getLLVMStyleWithColumns(0);
23489   constexpr StringRef Code =
23490       "/**\n"
23491       " * This is a multiline comment with quite some long lines, at least for "
23492       "the LLVM Style.\n"
23493       " * We will redo this with strings and line comments. Just to  check if "
23494       "everything is working.\n"
23495       " */\n"
23496       "bool foo() {\n"
23497       "  /* Single line multi line comment. */\n"
23498       "  const std::string String = \"This is a multiline string with quite "
23499       "some long lines, at least for the LLVM Style.\"\n"
23500       "                             \"We already did it with multi line "
23501       "comments, and we will do it with line comments. Just to check if "
23502       "everything is working.\";\n"
23503       "  // This is a line comment (block) with quite some long lines, at "
23504       "least for the LLVM Style.\n"
23505       "  // We already did this with multi line comments and strings. Just to "
23506       "check if everything is working.\n"
23507       "  const std::string SmallString = \"Hello World\";\n"
23508       "  // Small line comment\n"
23509       "  return String.size() > SmallString.size();\n"
23510       "}";
23511   EXPECT_EQ(Code, format(Code, Style));
23512 }
23513 
23514 TEST_F(FormatTest, FormatDecayCopy) {
23515   // error cases from unit tests
23516   verifyFormat("foo(auto())");
23517   verifyFormat("foo(auto{})");
23518   verifyFormat("foo(auto({}))");
23519   verifyFormat("foo(auto{{}})");
23520 
23521   verifyFormat("foo(auto(1))");
23522   verifyFormat("foo(auto{1})");
23523   verifyFormat("foo(new auto(1))");
23524   verifyFormat("foo(new auto{1})");
23525   verifyFormat("decltype(auto(1)) x;");
23526   verifyFormat("decltype(auto{1}) x;");
23527   verifyFormat("auto(x);");
23528   verifyFormat("auto{x};");
23529   verifyFormat("new auto{x};");
23530   verifyFormat("auto{x} = y;");
23531   verifyFormat("auto(x) = y;"); // actually a declaration, but this is clearly
23532                                 // the user's own fault
23533   verifyFormat("integral auto(x) = y;"); // actually a declaration, but this is
23534                                          // clearly the user's own fault
23535   verifyFormat("auto(*p)() = f;");       // actually a declaration; TODO FIXME
23536 }
23537 
23538 TEST_F(FormatTest, Cpp20ModulesSupport) {
23539   FormatStyle Style = getLLVMStyle();
23540   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
23541   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
23542 
23543   verifyFormat("export import foo;", Style);
23544   verifyFormat("export import foo:bar;", Style);
23545   verifyFormat("export import foo.bar;", Style);
23546   verifyFormat("export import foo.bar:baz;", Style);
23547   verifyFormat("export import :bar;", Style);
23548   verifyFormat("export module foo:bar;", Style);
23549   verifyFormat("export module foo;", Style);
23550   verifyFormat("export module foo.bar;", Style);
23551   verifyFormat("export module foo.bar:baz;", Style);
23552   verifyFormat("export import <string_view>;", Style);
23553 
23554   verifyFormat("export type_name var;", Style);
23555   verifyFormat("template <class T> export using A = B<T>;", Style);
23556   verifyFormat("export using A = B;", Style);
23557   verifyFormat("export int func() {\n"
23558                "  foo();\n"
23559                "}",
23560                Style);
23561   verifyFormat("export struct {\n"
23562                "  int foo;\n"
23563                "};",
23564                Style);
23565   verifyFormat("export {\n"
23566                "  int foo;\n"
23567                "};",
23568                Style);
23569   verifyFormat("export export char const *hello() { return \"hello\"; }");
23570 
23571   verifyFormat("import bar;", Style);
23572   verifyFormat("import foo.bar;", Style);
23573   verifyFormat("import foo:bar;", Style);
23574   verifyFormat("import :bar;", Style);
23575   verifyFormat("import <ctime>;", Style);
23576   verifyFormat("import \"header\";", Style);
23577 
23578   verifyFormat("module foo;", Style);
23579   verifyFormat("module foo:bar;", Style);
23580   verifyFormat("module foo.bar;", Style);
23581   verifyFormat("module;", Style);
23582 
23583   verifyFormat("export namespace hi {\n"
23584                "const char *sayhi();\n"
23585                "}",
23586                Style);
23587 
23588   verifyFormat("module :private;", Style);
23589   verifyFormat("import <foo/bar.h>;", Style);
23590   verifyFormat("import foo...bar;", Style);
23591   verifyFormat("import ..........;", Style);
23592   verifyFormat("module foo:private;", Style);
23593   verifyFormat("import a", Style);
23594   verifyFormat("module a", Style);
23595   verifyFormat("export import a", Style);
23596   verifyFormat("export module a", Style);
23597 
23598   verifyFormat("import", Style);
23599   verifyFormat("module", Style);
23600   verifyFormat("export", Style);
23601 }
23602 
23603 TEST_F(FormatTest, CoroutineForCoawait) {
23604   FormatStyle Style = getLLVMStyle();
23605   verifyFormat("for co_await (auto x : range())\n  ;");
23606   verifyFormat("for (auto i : arr) {\n"
23607                "}",
23608                Style);
23609   verifyFormat("for co_await (auto i : arr) {\n"
23610                "}",
23611                Style);
23612   verifyFormat("for co_await (auto i : foo(T{})) {\n"
23613                "}",
23614                Style);
23615 }
23616 
23617 TEST_F(FormatTest, CoroutineCoAwait) {
23618   verifyFormat("int x = co_await foo();");
23619   verifyFormat("int x = (co_await foo());");
23620   verifyFormat("co_await (42);");
23621   verifyFormat("void operator co_await(int);");
23622   verifyFormat("void operator co_await(a);");
23623   verifyFormat("co_await a;");
23624   verifyFormat("co_await missing_await_resume{};");
23625   verifyFormat("co_await a; // comment");
23626   verifyFormat("void test0() { co_await a; }");
23627   verifyFormat("co_await co_await co_await foo();");
23628   verifyFormat("co_await foo().bar();");
23629   verifyFormat("co_await [this]() -> Task { co_return x; }");
23630   verifyFormat("co_await [this](int a, int b) -> Task { co_return co_await "
23631                "foo(); }(x, y);");
23632 
23633   FormatStyle Style = getLLVMStyleWithColumns(40);
23634   verifyFormat("co_await [this](int a, int b) -> Task {\n"
23635                "  co_return co_await foo();\n"
23636                "}(x, y);",
23637                Style);
23638   verifyFormat("co_await;");
23639 }
23640 
23641 TEST_F(FormatTest, CoroutineCoYield) {
23642   verifyFormat("int x = co_yield foo();");
23643   verifyFormat("int x = (co_yield foo());");
23644   verifyFormat("co_yield (42);");
23645   verifyFormat("co_yield {42};");
23646   verifyFormat("co_yield 42;");
23647   verifyFormat("co_yield n++;");
23648   verifyFormat("co_yield ++n;");
23649   verifyFormat("co_yield;");
23650 }
23651 
23652 TEST_F(FormatTest, CoroutineCoReturn) {
23653   verifyFormat("co_return (42);");
23654   verifyFormat("co_return;");
23655   verifyFormat("co_return {};");
23656   verifyFormat("co_return x;");
23657   verifyFormat("co_return co_await foo();");
23658   verifyFormat("co_return co_yield foo();");
23659 }
23660 
23661 TEST_F(FormatTest, EmptyShortBlock) {
23662   auto Style = getLLVMStyle();
23663   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty;
23664 
23665   verifyFormat("try {\n"
23666                "  doA();\n"
23667                "} catch (Exception &e) {\n"
23668                "  e.printStackTrace();\n"
23669                "}\n",
23670                Style);
23671 
23672   verifyFormat("try {\n"
23673                "  doA();\n"
23674                "} catch (Exception &e) {}\n",
23675                Style);
23676 }
23677 
23678 TEST_F(FormatTest, ShortTemplatedArgumentLists) {
23679   auto Style = getLLVMStyle();
23680 
23681   verifyFormat("template <> struct S : Template<int (*)[]> {};\n", Style);
23682   verifyFormat("template <> struct S : Template<int (*)[10]> {};\n", Style);
23683   verifyFormat("struct Y : X<[] { return 0; }> {};", Style);
23684   verifyFormat("struct Y<[] { return 0; }> {};", Style);
23685 
23686   verifyFormat("struct Z : X<decltype([] { return 0; }){}> {};", Style);
23687   verifyFormat("template <int N> struct Foo<char[N]> {};", Style);
23688 }
23689 
23690 TEST_F(FormatTest, RemoveBraces) {
23691   FormatStyle Style = getLLVMStyle();
23692   Style.RemoveBracesLLVM = true;
23693 
23694   // The following eight test cases are fully-braced versions of the examples at
23695   // "llvm.org/docs/CodingStandards.html#don-t-use-braces-on-simple-single-
23696   // statement-bodies-of-if-else-loop-statements".
23697 
23698   // 1. Omit the braces, since the body is simple and clearly associated with
23699   // the if.
23700   verifyFormat("if (isa<FunctionDecl>(D))\n"
23701                "  handleFunctionDecl(D);\n"
23702                "else if (isa<VarDecl>(D))\n"
23703                "  handleVarDecl(D);",
23704                "if (isa<FunctionDecl>(D)) {\n"
23705                "  handleFunctionDecl(D);\n"
23706                "} else if (isa<VarDecl>(D)) {\n"
23707                "  handleVarDecl(D);\n"
23708                "}",
23709                Style);
23710 
23711   // 2. Here we document the condition itself and not the body.
23712   verifyFormat("if (isa<VarDecl>(D)) {\n"
23713                "  // It is necessary that we explain the situation with this\n"
23714                "  // surprisingly long comment, so it would be unclear\n"
23715                "  // without the braces whether the following statement is in\n"
23716                "  // the scope of the `if`.\n"
23717                "  // Because the condition is documented, we can't really\n"
23718                "  // hoist this comment that applies to the body above the\n"
23719                "  // if.\n"
23720                "  handleOtherDecl(D);\n"
23721                "}",
23722                Style);
23723 
23724   // 3. Use braces on the outer `if` to avoid a potential dangling else
23725   // situation.
23726   verifyFormat("if (isa<VarDecl>(D)) {\n"
23727                "  for (auto *A : D.attrs())\n"
23728                "    if (shouldProcessAttr(A))\n"
23729                "      handleAttr(A);\n"
23730                "}",
23731                "if (isa<VarDecl>(D)) {\n"
23732                "  for (auto *A : D.attrs()) {\n"
23733                "    if (shouldProcessAttr(A)) {\n"
23734                "      handleAttr(A);\n"
23735                "    }\n"
23736                "  }\n"
23737                "}",
23738                Style);
23739 
23740   // 4. Use braces for the `if` block to keep it uniform with the else block.
23741   verifyFormat("if (isa<FunctionDecl>(D)) {\n"
23742                "  handleFunctionDecl(D);\n"
23743                "} else {\n"
23744                "  // In this else case, it is necessary that we explain the\n"
23745                "  // situation with this surprisingly long comment, so it\n"
23746                "  // would be unclear without the braces whether the\n"
23747                "  // following statement is in the scope of the `if`.\n"
23748                "  handleOtherDecl(D);\n"
23749                "}",
23750                Style);
23751 
23752   // 5. This should also omit braces.  The `for` loop contains only a single
23753   // statement, so it shouldn't have braces.  The `if` also only contains a
23754   // single simple statement (the for loop), so it also should omit braces.
23755   verifyFormat("if (isa<FunctionDecl>(D))\n"
23756                "  for (auto *A : D.attrs())\n"
23757                "    handleAttr(A);",
23758                "if (isa<FunctionDecl>(D)) {\n"
23759                "  for (auto *A : D.attrs()) {\n"
23760                "    handleAttr(A);\n"
23761                "  }\n"
23762                "}",
23763                Style);
23764 
23765   // 6. Use braces for the outer `if` since the nested `for` is braced.
23766   verifyFormat("if (isa<FunctionDecl>(D)) {\n"
23767                "  for (auto *A : D.attrs()) {\n"
23768                "    // In this for loop body, it is necessary that we explain\n"
23769                "    // the situation with this surprisingly long comment,\n"
23770                "    // forcing braces on the `for` block.\n"
23771                "    handleAttr(A);\n"
23772                "  }\n"
23773                "}",
23774                Style);
23775 
23776   // 7. Use braces on the outer block because there are more than two levels of
23777   // nesting.
23778   verifyFormat("if (isa<FunctionDecl>(D)) {\n"
23779                "  for (auto *A : D.attrs())\n"
23780                "    for (ssize_t i : llvm::seq<ssize_t>(count))\n"
23781                "      handleAttrOnDecl(D, A, i);\n"
23782                "}",
23783                "if (isa<FunctionDecl>(D)) {\n"
23784                "  for (auto *A : D.attrs()) {\n"
23785                "    for (ssize_t i : llvm::seq<ssize_t>(count)) {\n"
23786                "      handleAttrOnDecl(D, A, i);\n"
23787                "    }\n"
23788                "  }\n"
23789                "}",
23790                Style);
23791 
23792   // 8. Use braces on the outer block because of a nested `if`, otherwise the
23793   // compiler would warn: `add explicit braces to avoid dangling else`
23794   verifyFormat("if (auto *D = dyn_cast<FunctionDecl>(D)) {\n"
23795                "  if (shouldProcess(D))\n"
23796                "    handleVarDecl(D);\n"
23797                "  else\n"
23798                "    markAsIgnored(D);\n"
23799                "}",
23800                "if (auto *D = dyn_cast<FunctionDecl>(D)) {\n"
23801                "  if (shouldProcess(D)) {\n"
23802                "    handleVarDecl(D);\n"
23803                "  } else {\n"
23804                "    markAsIgnored(D);\n"
23805                "  }\n"
23806                "}",
23807                Style);
23808 
23809   verifyFormat("if (a)\n"
23810                "  b; // comment\n"
23811                "else if (c)\n"
23812                "  d; /* comment */\n"
23813                "else\n"
23814                "  e;",
23815                "if (a) {\n"
23816                "  b; // comment\n"
23817                "} else if (c) {\n"
23818                "  d; /* comment */\n"
23819                "} else {\n"
23820                "  e;\n"
23821                "}",
23822                Style);
23823 
23824   verifyFormat("if (a) {\n"
23825                "  b;\n"
23826                "  c;\n"
23827                "} else if (d) {\n"
23828                "  e;\n"
23829                "}",
23830                Style);
23831 
23832   verifyFormat("if (a) {\n"
23833                "#undef NDEBUG\n"
23834                "  b;\n"
23835                "} else {\n"
23836                "  c;\n"
23837                "}",
23838                Style);
23839 
23840   verifyFormat("if (a) {\n"
23841                "  // comment\n"
23842                "} else if (b) {\n"
23843                "  c;\n"
23844                "}",
23845                Style);
23846 
23847   verifyFormat("if (a) {\n"
23848                "  b;\n"
23849                "} else {\n"
23850                "  { c; }\n"
23851                "}",
23852                Style);
23853 
23854   verifyFormat("if (a) {\n"
23855                "  if (b) // comment\n"
23856                "    c;\n"
23857                "} else if (d) {\n"
23858                "  e;\n"
23859                "}",
23860                "if (a) {\n"
23861                "  if (b) { // comment\n"
23862                "    c;\n"
23863                "  }\n"
23864                "} else if (d) {\n"
23865                "  e;\n"
23866                "}",
23867                Style);
23868 
23869   verifyFormat("if (a) {\n"
23870                "  if (b) {\n"
23871                "    c;\n"
23872                "    // comment\n"
23873                "  } else if (d) {\n"
23874                "    e;\n"
23875                "  }\n"
23876                "}",
23877                Style);
23878 
23879   verifyFormat("if (a) {\n"
23880                "  if (b)\n"
23881                "    c;\n"
23882                "}",
23883                "if (a) {\n"
23884                "  if (b) {\n"
23885                "    c;\n"
23886                "  }\n"
23887                "}",
23888                Style);
23889 
23890   verifyFormat("if (a)\n"
23891                "  if (b)\n"
23892                "    c;\n"
23893                "  else\n"
23894                "    d;\n"
23895                "else\n"
23896                "  e;",
23897                "if (a) {\n"
23898                "  if (b) {\n"
23899                "    c;\n"
23900                "  } else {\n"
23901                "    d;\n"
23902                "  }\n"
23903                "} else {\n"
23904                "  e;\n"
23905                "}",
23906                Style);
23907 
23908   verifyFormat("if (a) {\n"
23909                "  // comment\n"
23910                "  if (b)\n"
23911                "    c;\n"
23912                "  else if (d)\n"
23913                "    e;\n"
23914                "} else {\n"
23915                "  g;\n"
23916                "}",
23917                "if (a) {\n"
23918                "  // comment\n"
23919                "  if (b) {\n"
23920                "    c;\n"
23921                "  } else if (d) {\n"
23922                "    e;\n"
23923                "  }\n"
23924                "} else {\n"
23925                "  g;\n"
23926                "}",
23927                Style);
23928 
23929   verifyFormat("if (a)\n"
23930                "  b;\n"
23931                "else if (c)\n"
23932                "  d;\n"
23933                "else\n"
23934                "  e;",
23935                "if (a) {\n"
23936                "  b;\n"
23937                "} else {\n"
23938                "  if (c) {\n"
23939                "    d;\n"
23940                "  } else {\n"
23941                "    e;\n"
23942                "  }\n"
23943                "}",
23944                Style);
23945 
23946   verifyFormat("if (a) {\n"
23947                "  if (b)\n"
23948                "    c;\n"
23949                "  else if (d)\n"
23950                "    e;\n"
23951                "} else {\n"
23952                "  g;\n"
23953                "}",
23954                "if (a) {\n"
23955                "  if (b)\n"
23956                "    c;\n"
23957                "  else {\n"
23958                "    if (d)\n"
23959                "      e;\n"
23960                "  }\n"
23961                "} else {\n"
23962                "  g;\n"
23963                "}",
23964                Style);
23965 
23966   verifyFormat("if (a)\n"
23967                "  b;\n"
23968                "else if (c)\n"
23969                "  while (d)\n"
23970                "    e;\n"
23971                "// comment",
23972                "if (a)\n"
23973                "{\n"
23974                "  b;\n"
23975                "} else if (c) {\n"
23976                "  while (d) {\n"
23977                "    e;\n"
23978                "  }\n"
23979                "}\n"
23980                "// comment",
23981                Style);
23982 
23983   verifyFormat("if (a) {\n"
23984                "  b;\n"
23985                "} else if (c) {\n"
23986                "  d;\n"
23987                "} else {\n"
23988                "  e;\n"
23989                "  g;\n"
23990                "}",
23991                Style);
23992 
23993   verifyFormat("if (a) {\n"
23994                "  b;\n"
23995                "} else if (c) {\n"
23996                "  d;\n"
23997                "} else {\n"
23998                "  e;\n"
23999                "} // comment",
24000                Style);
24001 
24002   verifyFormat("int abs = [](int i) {\n"
24003                "  if (i >= 0)\n"
24004                "    return i;\n"
24005                "  return -i;\n"
24006                "};",
24007                "int abs = [](int i) {\n"
24008                "  if (i >= 0) {\n"
24009                "    return i;\n"
24010                "  }\n"
24011                "  return -i;\n"
24012                "};",
24013                Style);
24014 
24015   // FIXME: See https://github.com/llvm/llvm-project/issues/53543.
24016 #if 0
24017   Style.ColumnLimit = 65;
24018 
24019   verifyFormat("if (condition) {\n"
24020                "  ff(Indices,\n"
24021                "     [&](unsigned LHSI, unsigned RHSI) { return true; });\n"
24022                "} else {\n"
24023                "  ff(Indices,\n"
24024                "     [&](unsigned LHSI, unsigned RHSI) { return true; });\n"
24025                "}",
24026                Style);
24027 
24028   Style.ColumnLimit = 20;
24029 
24030   verifyFormat("if (a) {\n"
24031                "  b = c + // 1 -\n"
24032                "      d;\n"
24033                "}",
24034                Style);
24035 
24036   verifyFormat("if (a) {\n"
24037                "  b = c >= 0 ? d\n"
24038                "             : e;\n"
24039                "}",
24040                "if (a) {\n"
24041                "  b = c >= 0 ? d : e;\n"
24042                "}",
24043                Style);
24044 #endif
24045 
24046   Style.ColumnLimit = 20;
24047 
24048   verifyFormat("if (a)\n"
24049                "  b = c > 0 ? d : e;",
24050                "if (a) {\n"
24051                "  b = c > 0 ? d : e;\n"
24052                "}",
24053                Style);
24054 
24055   Style.ColumnLimit = 0;
24056 
24057   verifyFormat("if (a)\n"
24058                "  b234567890223456789032345678904234567890 = "
24059                "c234567890223456789032345678904234567890;",
24060                "if (a) {\n"
24061                "  b234567890223456789032345678904234567890 = "
24062                "c234567890223456789032345678904234567890;\n"
24063                "}",
24064                Style);
24065 }
24066 
24067 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndent) {
24068   auto Style = getLLVMStyle();
24069 
24070   StringRef Short = "functionCall(paramA, paramB, paramC);\n"
24071                     "void functionDecl(int a, int b, int c);";
24072 
24073   StringRef Medium = "functionCall(paramA, paramB, paramC, paramD, paramE, "
24074                      "paramF, paramG, paramH, paramI);\n"
24075                      "void functionDecl(int argumentA, int argumentB, int "
24076                      "argumentC, int argumentD, int argumentE);";
24077 
24078   verifyFormat(Short, Style);
24079 
24080   StringRef NoBreak = "functionCall(paramA, paramB, paramC, paramD, paramE, "
24081                       "paramF, paramG, paramH,\n"
24082                       "             paramI);\n"
24083                       "void functionDecl(int argumentA, int argumentB, int "
24084                       "argumentC, int argumentD,\n"
24085                       "                  int argumentE);";
24086 
24087   verifyFormat(NoBreak, Medium, Style);
24088   verifyFormat(NoBreak,
24089                "functionCall(\n"
24090                "    paramA,\n"
24091                "    paramB,\n"
24092                "    paramC,\n"
24093                "    paramD,\n"
24094                "    paramE,\n"
24095                "    paramF,\n"
24096                "    paramG,\n"
24097                "    paramH,\n"
24098                "    paramI\n"
24099                ");\n"
24100                "void functionDecl(\n"
24101                "    int argumentA,\n"
24102                "    int argumentB,\n"
24103                "    int argumentC,\n"
24104                "    int argumentD,\n"
24105                "    int argumentE\n"
24106                ");",
24107                Style);
24108 
24109   verifyFormat("outerFunctionCall(nestedFunctionCall(argument1),\n"
24110                "                  nestedLongFunctionCall(argument1, "
24111                "argument2, argument3,\n"
24112                "                                         argument4, "
24113                "argument5));",
24114                Style);
24115 
24116   Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
24117 
24118   verifyFormat(Short, Style);
24119   verifyFormat(
24120       "functionCall(\n"
24121       "    paramA, paramB, paramC, paramD, paramE, paramF, paramG, paramH, "
24122       "paramI\n"
24123       ");\n"
24124       "void functionDecl(\n"
24125       "    int argumentA, int argumentB, int argumentC, int argumentD, int "
24126       "argumentE\n"
24127       ");",
24128       Medium, Style);
24129 
24130   Style.AllowAllArgumentsOnNextLine = false;
24131   Style.AllowAllParametersOfDeclarationOnNextLine = false;
24132 
24133   verifyFormat(Short, Style);
24134   verifyFormat(
24135       "functionCall(\n"
24136       "    paramA, paramB, paramC, paramD, paramE, paramF, paramG, paramH, "
24137       "paramI\n"
24138       ");\n"
24139       "void functionDecl(\n"
24140       "    int argumentA, int argumentB, int argumentC, int argumentD, int "
24141       "argumentE\n"
24142       ");",
24143       Medium, Style);
24144 
24145   Style.BinPackArguments = false;
24146   Style.BinPackParameters = false;
24147 
24148   verifyFormat(Short, Style);
24149 
24150   verifyFormat("functionCall(\n"
24151                "    paramA,\n"
24152                "    paramB,\n"
24153                "    paramC,\n"
24154                "    paramD,\n"
24155                "    paramE,\n"
24156                "    paramF,\n"
24157                "    paramG,\n"
24158                "    paramH,\n"
24159                "    paramI\n"
24160                ");\n"
24161                "void functionDecl(\n"
24162                "    int argumentA,\n"
24163                "    int argumentB,\n"
24164                "    int argumentC,\n"
24165                "    int argumentD,\n"
24166                "    int argumentE\n"
24167                ");",
24168                Medium, Style);
24169 
24170   verifyFormat("outerFunctionCall(\n"
24171                "    nestedFunctionCall(argument1),\n"
24172                "    nestedLongFunctionCall(\n"
24173                "        argument1,\n"
24174                "        argument2,\n"
24175                "        argument3,\n"
24176                "        argument4,\n"
24177                "        argument5\n"
24178                "    )\n"
24179                ");",
24180                Style);
24181 
24182   verifyFormat("int a = (int)b;", Style);
24183   verifyFormat("int a = (int)b;",
24184                "int a = (\n"
24185                "    int\n"
24186                ") b;",
24187                Style);
24188 
24189   verifyFormat("return (true);", Style);
24190   verifyFormat("return (true);",
24191                "return (\n"
24192                "    true\n"
24193                ");",
24194                Style);
24195 
24196   verifyFormat("void foo();", Style);
24197   verifyFormat("void foo();",
24198                "void foo(\n"
24199                ");",
24200                Style);
24201 
24202   verifyFormat("void foo() {}", Style);
24203   verifyFormat("void foo() {}",
24204                "void foo(\n"
24205                ") {\n"
24206                "}",
24207                Style);
24208 
24209   verifyFormat("auto string = std::string();", Style);
24210   verifyFormat("auto string = std::string();",
24211                "auto string = std::string(\n"
24212                ");",
24213                Style);
24214 
24215   verifyFormat("void (*functionPointer)() = nullptr;", Style);
24216   verifyFormat("void (*functionPointer)() = nullptr;",
24217                "void (\n"
24218                "    *functionPointer\n"
24219                ")\n"
24220                "(\n"
24221                ") = nullptr;",
24222                Style);
24223 }
24224 
24225 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentIfStatement) {
24226   auto Style = getLLVMStyle();
24227 
24228   verifyFormat("if (foo()) {\n"
24229                "  return;\n"
24230                "}",
24231                Style);
24232 
24233   verifyFormat("if (quitelongarg !=\n"
24234                "    (alsolongarg - 1)) { // ABC is a very longgggggggggggg "
24235                "comment\n"
24236                "  return;\n"
24237                "}",
24238                Style);
24239 
24240   Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
24241 
24242   verifyFormat("if (foo()) {\n"
24243                "  return;\n"
24244                "}",
24245                Style);
24246 
24247   verifyFormat("if (quitelongarg !=\n"
24248                "    (alsolongarg - 1)) { // ABC is a very longgggggggggggg "
24249                "comment\n"
24250                "  return;\n"
24251                "}",
24252                Style);
24253 }
24254 
24255 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentForStatement) {
24256   auto Style = getLLVMStyle();
24257 
24258   verifyFormat("for (int i = 0; i < 5; ++i) {\n"
24259                "  doSomething();\n"
24260                "}",
24261                Style);
24262 
24263   verifyFormat("for (int myReallyLongCountVariable = 0; "
24264                "myReallyLongCountVariable < count;\n"
24265                "     myReallyLongCountVariable++) {\n"
24266                "  doSomething();\n"
24267                "}",
24268                Style);
24269 
24270   Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
24271 
24272   verifyFormat("for (int i = 0; i < 5; ++i) {\n"
24273                "  doSomething();\n"
24274                "}",
24275                Style);
24276 
24277   verifyFormat("for (int myReallyLongCountVariable = 0; "
24278                "myReallyLongCountVariable < count;\n"
24279                "     myReallyLongCountVariable++) {\n"
24280                "  doSomething();\n"
24281                "}",
24282                Style);
24283 }
24284 
24285 TEST_F(FormatTest, UnderstandsDigraphs) {
24286   verifyFormat("int arr<:5:> = {};");
24287   verifyFormat("int arr[5] = <%%>;");
24288   verifyFormat("int arr<:::qualified_variable:> = {};");
24289   verifyFormat("int arr[::qualified_variable] = <%%>;");
24290   verifyFormat("%:include <header>");
24291   verifyFormat("%:define A x##y");
24292   verifyFormat("#define A x%:%:y");
24293 }
24294 
24295 } // namespace
24296 } // namespace format
24297 } // namespace clang
24298