1 //===- unittest/Format/FormatTest.cpp - Formatting unit tests -------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "clang/Format/Format.h"
10 
11 #include "../Tooling/ReplacementTest.h"
12 #include "FormatTestUtils.h"
13 
14 #include "llvm/Support/Debug.h"
15 #include "llvm/Support/MemoryBuffer.h"
16 #include "gtest/gtest.h"
17 
18 #define DEBUG_TYPE "format-test"
19 
20 using clang::tooling::ReplacementTest;
21 using clang::tooling::toReplacements;
22 using testing::ScopedTrace;
23 
24 namespace clang {
25 namespace format {
26 namespace {
27 
28 FormatStyle getGoogleStyle() { return getGoogleStyle(FormatStyle::LK_Cpp); }
29 
30 class FormatTest : public ::testing::Test {
31 protected:
32   enum StatusCheck { SC_ExpectComplete, SC_ExpectIncomplete, SC_DoNotCheck };
33 
34   std::string format(llvm::StringRef Code,
35                      const FormatStyle &Style = getLLVMStyle(),
36                      StatusCheck CheckComplete = SC_ExpectComplete) {
37     LLVM_DEBUG(llvm::errs() << "---\n");
38     LLVM_DEBUG(llvm::errs() << Code << "\n\n");
39     std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size()));
40     FormattingAttemptStatus Status;
41     tooling::Replacements Replaces =
42         reformat(Style, Code, Ranges, "<stdin>", &Status);
43     if (CheckComplete != SC_DoNotCheck) {
44       bool ExpectedCompleteFormat = CheckComplete == SC_ExpectComplete;
45       EXPECT_EQ(ExpectedCompleteFormat, Status.FormatComplete)
46           << Code << "\n\n";
47     }
48     ReplacementCount = Replaces.size();
49     auto Result = applyAllReplacements(Code, Replaces);
50     EXPECT_TRUE(static_cast<bool>(Result));
51     LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
52     return *Result;
53   }
54 
55   FormatStyle getStyleWithColumns(FormatStyle Style, unsigned ColumnLimit) {
56     Style.ColumnLimit = ColumnLimit;
57     return Style;
58   }
59 
60   FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) {
61     return getStyleWithColumns(getLLVMStyle(), ColumnLimit);
62   }
63 
64   FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) {
65     return getStyleWithColumns(getGoogleStyle(), ColumnLimit);
66   }
67 
68   void _verifyFormat(const char *File, int Line, llvm::StringRef Expected,
69                      llvm::StringRef Code,
70                      const FormatStyle &Style = getLLVMStyle()) {
71     ScopedTrace t(File, Line, ::testing::Message() << Code.str());
72     EXPECT_EQ(Expected.str(), format(Expected, Style))
73         << "Expected code is not stable";
74     EXPECT_EQ(Expected.str(), format(Code, Style));
75     if (Style.Language == FormatStyle::LK_Cpp) {
76       // Objective-C++ is a superset of C++, so everything checked for C++
77       // needs to be checked for Objective-C++ as well.
78       FormatStyle ObjCStyle = Style;
79       ObjCStyle.Language = FormatStyle::LK_ObjC;
80       EXPECT_EQ(Expected.str(), format(test::messUp(Code), ObjCStyle));
81     }
82   }
83 
84   void _verifyFormat(const char *File, int Line, llvm::StringRef Code,
85                      const FormatStyle &Style = getLLVMStyle()) {
86     _verifyFormat(File, Line, Code, test::messUp(Code), Style);
87   }
88 
89   void _verifyIncompleteFormat(const char *File, int Line, llvm::StringRef Code,
90                                const FormatStyle &Style = getLLVMStyle()) {
91     ScopedTrace t(File, Line, ::testing::Message() << Code.str());
92     EXPECT_EQ(Code.str(),
93               format(test::messUp(Code), Style, SC_ExpectIncomplete));
94   }
95 
96   void _verifyIndependentOfContext(const char *File, int Line,
97                                    llvm::StringRef Text,
98                                    const FormatStyle &Style = getLLVMStyle()) {
99     _verifyFormat(File, Line, Text, Style);
100     _verifyFormat(File, Line, llvm::Twine("void f() { " + Text + " }").str(),
101                   Style);
102   }
103 
104   /// \brief Verify that clang-format does not crash on the given input.
105   void verifyNoCrash(llvm::StringRef Code,
106                      const FormatStyle &Style = getLLVMStyle()) {
107     format(Code, Style, SC_DoNotCheck);
108   }
109 
110   int ReplacementCount;
111 };
112 
113 #define verifyIndependentOfContext(...)                                        \
114   _verifyIndependentOfContext(__FILE__, __LINE__, __VA_ARGS__)
115 #define verifyIncompleteFormat(...)                                            \
116   _verifyIncompleteFormat(__FILE__, __LINE__, __VA_ARGS__)
117 #define verifyFormat(...) _verifyFormat(__FILE__, __LINE__, __VA_ARGS__)
118 #define verifyGoogleFormat(Code) verifyFormat(Code, getGoogleStyle())
119 
120 TEST_F(FormatTest, MessUp) {
121   EXPECT_EQ("1 2 3", test::messUp("1 2 3"));
122   EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n"));
123   EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc"));
124   EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc"));
125   EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne"));
126 }
127 
128 TEST_F(FormatTest, DefaultLLVMStyleIsCpp) {
129   EXPECT_EQ(FormatStyle::LK_Cpp, getLLVMStyle().Language);
130 }
131 
132 TEST_F(FormatTest, LLVMStyleOverride) {
133   EXPECT_EQ(FormatStyle::LK_Proto,
134             getLLVMStyle(FormatStyle::LK_Proto).Language);
135 }
136 
137 //===----------------------------------------------------------------------===//
138 // Basic function tests.
139 //===----------------------------------------------------------------------===//
140 
141 TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) {
142   EXPECT_EQ(";", format(";"));
143 }
144 
145 TEST_F(FormatTest, FormatsGlobalStatementsAt0) {
146   EXPECT_EQ("int i;", format("  int i;"));
147   EXPECT_EQ("\nint i;", format(" \n\t \v \f  int i;"));
148   EXPECT_EQ("int i;\nint j;", format("    int i; int j;"));
149   EXPECT_EQ("int i;\nint j;", format("    int i;\n  int j;"));
150 }
151 
152 TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) {
153   EXPECT_EQ("int i;", format("int\ni;"));
154 }
155 
156 TEST_F(FormatTest, FormatsNestedBlockStatements) {
157   EXPECT_EQ("{\n  {\n    {}\n  }\n}", format("{{{}}}"));
158 }
159 
160 TEST_F(FormatTest, FormatsNestedCall) {
161   verifyFormat("Method(f1, f2(f3));");
162   verifyFormat("Method(f1(f2, f3()));");
163   verifyFormat("Method(f1(f2, (f3())));");
164 }
165 
166 TEST_F(FormatTest, NestedNameSpecifiers) {
167   verifyFormat("vector<::Type> v;");
168   verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())");
169   verifyFormat("static constexpr bool Bar = decltype(bar())::value;");
170   verifyFormat("static constexpr bool Bar = typeof(bar())::value;");
171   verifyFormat("static constexpr bool Bar = __underlying_type(bar())::value;");
172   verifyFormat("static constexpr bool Bar = _Atomic(bar())::value;");
173   verifyFormat("bool a = 2 < ::SomeFunction();");
174   verifyFormat("ALWAYS_INLINE ::std::string getName();");
175   verifyFormat("some::string getName();");
176 }
177 
178 TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) {
179   EXPECT_EQ("if (a) {\n"
180             "  f();\n"
181             "}",
182             format("if(a){f();}"));
183   EXPECT_EQ(4, ReplacementCount);
184   EXPECT_EQ("if (a) {\n"
185             "  f();\n"
186             "}",
187             format("if (a) {\n"
188                    "  f();\n"
189                    "}"));
190   EXPECT_EQ(0, ReplacementCount);
191   EXPECT_EQ("/*\r\n"
192             "\r\n"
193             "*/\r\n",
194             format("/*\r\n"
195                    "\r\n"
196                    "*/\r\n"));
197   EXPECT_EQ(0, ReplacementCount);
198 }
199 
200 TEST_F(FormatTest, RemovesEmptyLines) {
201   EXPECT_EQ("class C {\n"
202             "  int i;\n"
203             "};",
204             format("class C {\n"
205                    " int i;\n"
206                    "\n"
207                    "};"));
208 
209   // Don't remove empty lines at the start of namespaces or extern "C" blocks.
210   EXPECT_EQ("namespace N {\n"
211             "\n"
212             "int i;\n"
213             "}",
214             format("namespace N {\n"
215                    "\n"
216                    "int    i;\n"
217                    "}",
218                    getGoogleStyle()));
219   EXPECT_EQ("/* something */ namespace N {\n"
220             "\n"
221             "int i;\n"
222             "}",
223             format("/* something */ namespace N {\n"
224                    "\n"
225                    "int    i;\n"
226                    "}",
227                    getGoogleStyle()));
228   EXPECT_EQ("inline namespace N {\n"
229             "\n"
230             "int i;\n"
231             "}",
232             format("inline namespace N {\n"
233                    "\n"
234                    "int    i;\n"
235                    "}",
236                    getGoogleStyle()));
237   EXPECT_EQ("/* something */ inline namespace N {\n"
238             "\n"
239             "int i;\n"
240             "}",
241             format("/* something */ inline namespace N {\n"
242                    "\n"
243                    "int    i;\n"
244                    "}",
245                    getGoogleStyle()));
246   EXPECT_EQ("export namespace N {\n"
247             "\n"
248             "int i;\n"
249             "}",
250             format("export namespace N {\n"
251                    "\n"
252                    "int    i;\n"
253                    "}",
254                    getGoogleStyle()));
255   EXPECT_EQ("extern /**/ \"C\" /**/ {\n"
256             "\n"
257             "int i;\n"
258             "}",
259             format("extern /**/ \"C\" /**/ {\n"
260                    "\n"
261                    "int    i;\n"
262                    "}",
263                    getGoogleStyle()));
264 
265   auto CustomStyle = clang::format::getLLVMStyle();
266   CustomStyle.BreakBeforeBraces = clang::format::FormatStyle::BS_Custom;
267   CustomStyle.BraceWrapping.AfterNamespace = true;
268   CustomStyle.KeepEmptyLinesAtTheStartOfBlocks = false;
269   EXPECT_EQ("namespace N\n"
270             "{\n"
271             "\n"
272             "int i;\n"
273             "}",
274             format("namespace N\n"
275                    "{\n"
276                    "\n"
277                    "\n"
278                    "int    i;\n"
279                    "}",
280                    CustomStyle));
281   EXPECT_EQ("/* something */ namespace N\n"
282             "{\n"
283             "\n"
284             "int i;\n"
285             "}",
286             format("/* something */ namespace N {\n"
287                    "\n"
288                    "\n"
289                    "int    i;\n"
290                    "}",
291                    CustomStyle));
292   EXPECT_EQ("inline namespace N\n"
293             "{\n"
294             "\n"
295             "int i;\n"
296             "}",
297             format("inline namespace N\n"
298                    "{\n"
299                    "\n"
300                    "\n"
301                    "int    i;\n"
302                    "}",
303                    CustomStyle));
304   EXPECT_EQ("/* something */ inline namespace N\n"
305             "{\n"
306             "\n"
307             "int i;\n"
308             "}",
309             format("/* something */ inline namespace N\n"
310                    "{\n"
311                    "\n"
312                    "int    i;\n"
313                    "}",
314                    CustomStyle));
315   EXPECT_EQ("export namespace N\n"
316             "{\n"
317             "\n"
318             "int i;\n"
319             "}",
320             format("export namespace N\n"
321                    "{\n"
322                    "\n"
323                    "int    i;\n"
324                    "}",
325                    CustomStyle));
326   EXPECT_EQ("namespace a\n"
327             "{\n"
328             "namespace b\n"
329             "{\n"
330             "\n"
331             "class AA {};\n"
332             "\n"
333             "} // namespace b\n"
334             "} // namespace a\n",
335             format("namespace a\n"
336                    "{\n"
337                    "namespace b\n"
338                    "{\n"
339                    "\n"
340                    "\n"
341                    "class AA {};\n"
342                    "\n"
343                    "\n"
344                    "}\n"
345                    "}\n",
346                    CustomStyle));
347   EXPECT_EQ("namespace A /* comment */\n"
348             "{\n"
349             "class B {}\n"
350             "} // namespace A",
351             format("namespace A /* comment */ { class B {} }", CustomStyle));
352   EXPECT_EQ("namespace A\n"
353             "{ /* comment */\n"
354             "class B {}\n"
355             "} // namespace A",
356             format("namespace A {/* comment */ class B {} }", CustomStyle));
357   EXPECT_EQ("namespace A\n"
358             "{ /* comment */\n"
359             "\n"
360             "class B {}\n"
361             "\n"
362             ""
363             "} // namespace A",
364             format("namespace A { /* comment */\n"
365                    "\n"
366                    "\n"
367                    "class B {}\n"
368                    "\n"
369                    "\n"
370                    "}",
371                    CustomStyle));
372   EXPECT_EQ("namespace A /* comment */\n"
373             "{\n"
374             "\n"
375             "class B {}\n"
376             "\n"
377             "} // namespace A",
378             format("namespace A/* comment */ {\n"
379                    "\n"
380                    "\n"
381                    "class B {}\n"
382                    "\n"
383                    "\n"
384                    "}",
385                    CustomStyle));
386 
387   // ...but do keep inlining and removing empty lines for non-block extern "C"
388   // functions.
389   verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle());
390   EXPECT_EQ("extern \"C\" int f() {\n"
391             "  int i = 42;\n"
392             "  return i;\n"
393             "}",
394             format("extern \"C\" int f() {\n"
395                    "\n"
396                    "  int i = 42;\n"
397                    "  return i;\n"
398                    "}",
399                    getGoogleStyle()));
400 
401   // Remove empty lines at the beginning and end of blocks.
402   EXPECT_EQ("void f() {\n"
403             "\n"
404             "  if (a) {\n"
405             "\n"
406             "    f();\n"
407             "  }\n"
408             "}",
409             format("void f() {\n"
410                    "\n"
411                    "  if (a) {\n"
412                    "\n"
413                    "    f();\n"
414                    "\n"
415                    "  }\n"
416                    "\n"
417                    "}",
418                    getLLVMStyle()));
419   EXPECT_EQ("void f() {\n"
420             "  if (a) {\n"
421             "    f();\n"
422             "  }\n"
423             "}",
424             format("void f() {\n"
425                    "\n"
426                    "  if (a) {\n"
427                    "\n"
428                    "    f();\n"
429                    "\n"
430                    "  }\n"
431                    "\n"
432                    "}",
433                    getGoogleStyle()));
434 
435   // Don't remove empty lines in more complex control statements.
436   EXPECT_EQ("void f() {\n"
437             "  if (a) {\n"
438             "    f();\n"
439             "\n"
440             "  } else if (b) {\n"
441             "    f();\n"
442             "  }\n"
443             "}",
444             format("void f() {\n"
445                    "  if (a) {\n"
446                    "    f();\n"
447                    "\n"
448                    "  } else if (b) {\n"
449                    "    f();\n"
450                    "\n"
451                    "  }\n"
452                    "\n"
453                    "}"));
454 
455   // Don't remove empty lines before namespace endings.
456   FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle();
457   LLVMWithNoNamespaceFix.FixNamespaceComments = false;
458   EXPECT_EQ("namespace {\n"
459             "int i;\n"
460             "\n"
461             "}",
462             format("namespace {\n"
463                    "int i;\n"
464                    "\n"
465                    "}",
466                    LLVMWithNoNamespaceFix));
467   EXPECT_EQ("namespace {\n"
468             "int i;\n"
469             "}",
470             format("namespace {\n"
471                    "int i;\n"
472                    "}",
473                    LLVMWithNoNamespaceFix));
474   EXPECT_EQ("namespace {\n"
475             "int i;\n"
476             "\n"
477             "};",
478             format("namespace {\n"
479                    "int i;\n"
480                    "\n"
481                    "};",
482                    LLVMWithNoNamespaceFix));
483   EXPECT_EQ("namespace {\n"
484             "int i;\n"
485             "};",
486             format("namespace {\n"
487                    "int i;\n"
488                    "};",
489                    LLVMWithNoNamespaceFix));
490   EXPECT_EQ("namespace {\n"
491             "int i;\n"
492             "\n"
493             "}",
494             format("namespace {\n"
495                    "int i;\n"
496                    "\n"
497                    "}"));
498   EXPECT_EQ("namespace {\n"
499             "int i;\n"
500             "\n"
501             "} // namespace",
502             format("namespace {\n"
503                    "int i;\n"
504                    "\n"
505                    "}  // namespace"));
506 
507   FormatStyle Style = getLLVMStyle();
508   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
509   Style.MaxEmptyLinesToKeep = 2;
510   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
511   Style.BraceWrapping.AfterClass = true;
512   Style.BraceWrapping.AfterFunction = true;
513   Style.KeepEmptyLinesAtTheStartOfBlocks = false;
514 
515   EXPECT_EQ("class Foo\n"
516             "{\n"
517             "  Foo() {}\n"
518             "\n"
519             "  void funk() {}\n"
520             "};",
521             format("class Foo\n"
522                    "{\n"
523                    "  Foo()\n"
524                    "  {\n"
525                    "  }\n"
526                    "\n"
527                    "  void funk() {}\n"
528                    "};",
529                    Style));
530 }
531 
532 TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) {
533   verifyFormat("x = (a) and (b);");
534   verifyFormat("x = (a) or (b);");
535   verifyFormat("x = (a) bitand (b);");
536   verifyFormat("x = (a) bitor (b);");
537   verifyFormat("x = (a) not_eq (b);");
538   verifyFormat("x = (a) and_eq (b);");
539   verifyFormat("x = (a) or_eq (b);");
540   verifyFormat("x = (a) xor (b);");
541 }
542 
543 TEST_F(FormatTest, RecognizesUnaryOperatorKeywords) {
544   verifyFormat("x = compl(a);");
545   verifyFormat("x = not(a);");
546   verifyFormat("x = bitand(a);");
547   // Unary operator must not be merged with the next identifier
548   verifyFormat("x = compl a;");
549   verifyFormat("x = not a;");
550   verifyFormat("x = bitand a;");
551 }
552 
553 //===----------------------------------------------------------------------===//
554 // Tests for control statements.
555 //===----------------------------------------------------------------------===//
556 
557 TEST_F(FormatTest, FormatIfWithoutCompoundStatement) {
558   verifyFormat("if (true)\n  f();\ng();");
559   verifyFormat("if (a)\n  if (b)\n    if (c)\n      g();\nh();");
560   verifyFormat("if (a)\n  if (b) {\n    f();\n  }\ng();");
561   verifyFormat("if constexpr (true)\n"
562                "  f();\ng();");
563   verifyFormat("if CONSTEXPR (true)\n"
564                "  f();\ng();");
565   verifyFormat("if constexpr (a)\n"
566                "  if constexpr (b)\n"
567                "    if constexpr (c)\n"
568                "      g();\n"
569                "h();");
570   verifyFormat("if CONSTEXPR (a)\n"
571                "  if CONSTEXPR (b)\n"
572                "    if CONSTEXPR (c)\n"
573                "      g();\n"
574                "h();");
575   verifyFormat("if constexpr (a)\n"
576                "  if constexpr (b) {\n"
577                "    f();\n"
578                "  }\n"
579                "g();");
580   verifyFormat("if CONSTEXPR (a)\n"
581                "  if CONSTEXPR (b) {\n"
582                "    f();\n"
583                "  }\n"
584                "g();");
585 
586   verifyFormat("if (a)\n"
587                "  g();");
588   verifyFormat("if (a) {\n"
589                "  g()\n"
590                "};");
591   verifyFormat("if (a)\n"
592                "  g();\n"
593                "else\n"
594                "  g();");
595   verifyFormat("if (a) {\n"
596                "  g();\n"
597                "} else\n"
598                "  g();");
599   verifyFormat("if (a)\n"
600                "  g();\n"
601                "else {\n"
602                "  g();\n"
603                "}");
604   verifyFormat("if (a) {\n"
605                "  g();\n"
606                "} else {\n"
607                "  g();\n"
608                "}");
609   verifyFormat("if (a)\n"
610                "  g();\n"
611                "else if (b)\n"
612                "  g();\n"
613                "else\n"
614                "  g();");
615   verifyFormat("if (a) {\n"
616                "  g();\n"
617                "} else if (b)\n"
618                "  g();\n"
619                "else\n"
620                "  g();");
621   verifyFormat("if (a)\n"
622                "  g();\n"
623                "else if (b) {\n"
624                "  g();\n"
625                "} else\n"
626                "  g();");
627   verifyFormat("if (a)\n"
628                "  g();\n"
629                "else if (b)\n"
630                "  g();\n"
631                "else {\n"
632                "  g();\n"
633                "}");
634   verifyFormat("if (a)\n"
635                "  g();\n"
636                "else if (b) {\n"
637                "  g();\n"
638                "} else {\n"
639                "  g();\n"
640                "}");
641   verifyFormat("if (a) {\n"
642                "  g();\n"
643                "} else if (b) {\n"
644                "  g();\n"
645                "} else {\n"
646                "  g();\n"
647                "}");
648 
649   FormatStyle AllowsMergedIf = getLLVMStyle();
650   AllowsMergedIf.IfMacros.push_back("MYIF");
651   AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left;
652   AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
653       FormatStyle::SIS_WithoutElse;
654   verifyFormat("if (a)\n"
655                "  // comment\n"
656                "  f();",
657                AllowsMergedIf);
658   verifyFormat("{\n"
659                "  if (a)\n"
660                "  label:\n"
661                "    f();\n"
662                "}",
663                AllowsMergedIf);
664   verifyFormat("#define A \\\n"
665                "  if (a)  \\\n"
666                "  label:  \\\n"
667                "    f()",
668                AllowsMergedIf);
669   verifyFormat("if (a)\n"
670                "  ;",
671                AllowsMergedIf);
672   verifyFormat("if (a)\n"
673                "  if (b) return;",
674                AllowsMergedIf);
675 
676   verifyFormat("if (a) // Can't merge this\n"
677                "  f();\n",
678                AllowsMergedIf);
679   verifyFormat("if (a) /* still don't merge */\n"
680                "  f();",
681                AllowsMergedIf);
682   verifyFormat("if (a) { // Never merge this\n"
683                "  f();\n"
684                "}",
685                AllowsMergedIf);
686   verifyFormat("if (a) { /* Never merge this */\n"
687                "  f();\n"
688                "}",
689                AllowsMergedIf);
690   verifyFormat("MYIF (a)\n"
691                "  // comment\n"
692                "  f();",
693                AllowsMergedIf);
694   verifyFormat("{\n"
695                "  MYIF (a)\n"
696                "  label:\n"
697                "    f();\n"
698                "}",
699                AllowsMergedIf);
700   verifyFormat("#define A  \\\n"
701                "  MYIF (a) \\\n"
702                "  label:   \\\n"
703                "    f()",
704                AllowsMergedIf);
705   verifyFormat("MYIF (a)\n"
706                "  ;",
707                AllowsMergedIf);
708   verifyFormat("MYIF (a)\n"
709                "  MYIF (b) return;",
710                AllowsMergedIf);
711 
712   verifyFormat("MYIF (a) // Can't merge this\n"
713                "  f();\n",
714                AllowsMergedIf);
715   verifyFormat("MYIF (a) /* still don't merge */\n"
716                "  f();",
717                AllowsMergedIf);
718   verifyFormat("MYIF (a) { // Never merge this\n"
719                "  f();\n"
720                "}",
721                AllowsMergedIf);
722   verifyFormat("MYIF (a) { /* Never merge this */\n"
723                "  f();\n"
724                "}",
725                AllowsMergedIf);
726 
727   AllowsMergedIf.ColumnLimit = 14;
728   // Where line-lengths matter, a 2-letter synonym that maintains line length.
729   // Not IF to avoid any confusion that IF is somehow special.
730   AllowsMergedIf.IfMacros.push_back("FI");
731   verifyFormat("if (a) return;", AllowsMergedIf);
732   verifyFormat("if (aaaaaaaaa)\n"
733                "  return;",
734                AllowsMergedIf);
735   verifyFormat("FI (a) return;", AllowsMergedIf);
736   verifyFormat("FI (aaaaaaaaa)\n"
737                "  return;",
738                AllowsMergedIf);
739 
740   AllowsMergedIf.ColumnLimit = 13;
741   verifyFormat("if (a)\n  return;", AllowsMergedIf);
742   verifyFormat("FI (a)\n  return;", AllowsMergedIf);
743 
744   FormatStyle AllowsMergedIfElse = getLLVMStyle();
745   AllowsMergedIfElse.IfMacros.push_back("MYIF");
746   AllowsMergedIfElse.AllowShortIfStatementsOnASingleLine =
747       FormatStyle::SIS_AllIfsAndElse;
748   verifyFormat("if (a)\n"
749                "  // comment\n"
750                "  f();\n"
751                "else\n"
752                "  // comment\n"
753                "  f();",
754                AllowsMergedIfElse);
755   verifyFormat("{\n"
756                "  if (a)\n"
757                "  label:\n"
758                "    f();\n"
759                "  else\n"
760                "  label:\n"
761                "    f();\n"
762                "}",
763                AllowsMergedIfElse);
764   verifyFormat("if (a)\n"
765                "  ;\n"
766                "else\n"
767                "  ;",
768                AllowsMergedIfElse);
769   verifyFormat("if (a) {\n"
770                "} else {\n"
771                "}",
772                AllowsMergedIfElse);
773   verifyFormat("if (a) return;\n"
774                "else if (b) return;\n"
775                "else return;",
776                AllowsMergedIfElse);
777   verifyFormat("if (a) {\n"
778                "} else return;",
779                AllowsMergedIfElse);
780   verifyFormat("if (a) {\n"
781                "} else if (b) return;\n"
782                "else return;",
783                AllowsMergedIfElse);
784   verifyFormat("if (a) return;\n"
785                "else if (b) {\n"
786                "} else return;",
787                AllowsMergedIfElse);
788   verifyFormat("if (a)\n"
789                "  if (b) return;\n"
790                "  else return;",
791                AllowsMergedIfElse);
792   verifyFormat("if constexpr (a)\n"
793                "  if constexpr (b) return;\n"
794                "  else if constexpr (c) return;\n"
795                "  else return;",
796                AllowsMergedIfElse);
797   verifyFormat("MYIF (a)\n"
798                "  // comment\n"
799                "  f();\n"
800                "else\n"
801                "  // comment\n"
802                "  f();",
803                AllowsMergedIfElse);
804   verifyFormat("{\n"
805                "  MYIF (a)\n"
806                "  label:\n"
807                "    f();\n"
808                "  else\n"
809                "  label:\n"
810                "    f();\n"
811                "}",
812                AllowsMergedIfElse);
813   verifyFormat("MYIF (a)\n"
814                "  ;\n"
815                "else\n"
816                "  ;",
817                AllowsMergedIfElse);
818   verifyFormat("MYIF (a) {\n"
819                "} else {\n"
820                "}",
821                AllowsMergedIfElse);
822   verifyFormat("MYIF (a) return;\n"
823                "else MYIF (b) return;\n"
824                "else return;",
825                AllowsMergedIfElse);
826   verifyFormat("MYIF (a) {\n"
827                "} else return;",
828                AllowsMergedIfElse);
829   verifyFormat("MYIF (a) {\n"
830                "} else MYIF (b) return;\n"
831                "else return;",
832                AllowsMergedIfElse);
833   verifyFormat("MYIF (a) return;\n"
834                "else MYIF (b) {\n"
835                "} else return;",
836                AllowsMergedIfElse);
837   verifyFormat("MYIF (a)\n"
838                "  MYIF (b) return;\n"
839                "  else return;",
840                AllowsMergedIfElse);
841   verifyFormat("MYIF constexpr (a)\n"
842                "  MYIF constexpr (b) return;\n"
843                "  else MYIF constexpr (c) return;\n"
844                "  else return;",
845                AllowsMergedIfElse);
846 }
847 
848 TEST_F(FormatTest, FormatIfWithoutCompoundStatementButElseWith) {
849   FormatStyle AllowsMergedIf = getLLVMStyle();
850   AllowsMergedIf.IfMacros.push_back("MYIF");
851   AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left;
852   AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
853       FormatStyle::SIS_WithoutElse;
854   verifyFormat("if (a)\n"
855                "  f();\n"
856                "else {\n"
857                "  g();\n"
858                "}",
859                AllowsMergedIf);
860   verifyFormat("if (a)\n"
861                "  f();\n"
862                "else\n"
863                "  g();\n",
864                AllowsMergedIf);
865 
866   verifyFormat("if (a) g();", AllowsMergedIf);
867   verifyFormat("if (a) {\n"
868                "  g()\n"
869                "};",
870                AllowsMergedIf);
871   verifyFormat("if (a)\n"
872                "  g();\n"
873                "else\n"
874                "  g();",
875                AllowsMergedIf);
876   verifyFormat("if (a) {\n"
877                "  g();\n"
878                "} else\n"
879                "  g();",
880                AllowsMergedIf);
881   verifyFormat("if (a)\n"
882                "  g();\n"
883                "else {\n"
884                "  g();\n"
885                "}",
886                AllowsMergedIf);
887   verifyFormat("if (a) {\n"
888                "  g();\n"
889                "} else {\n"
890                "  g();\n"
891                "}",
892                AllowsMergedIf);
893   verifyFormat("if (a)\n"
894                "  g();\n"
895                "else if (b)\n"
896                "  g();\n"
897                "else\n"
898                "  g();",
899                AllowsMergedIf);
900   verifyFormat("if (a) {\n"
901                "  g();\n"
902                "} else if (b)\n"
903                "  g();\n"
904                "else\n"
905                "  g();",
906                AllowsMergedIf);
907   verifyFormat("if (a)\n"
908                "  g();\n"
909                "else if (b) {\n"
910                "  g();\n"
911                "} else\n"
912                "  g();",
913                AllowsMergedIf);
914   verifyFormat("if (a)\n"
915                "  g();\n"
916                "else if (b)\n"
917                "  g();\n"
918                "else {\n"
919                "  g();\n"
920                "}",
921                AllowsMergedIf);
922   verifyFormat("if (a)\n"
923                "  g();\n"
924                "else if (b) {\n"
925                "  g();\n"
926                "} else {\n"
927                "  g();\n"
928                "}",
929                AllowsMergedIf);
930   verifyFormat("if (a) {\n"
931                "  g();\n"
932                "} else if (b) {\n"
933                "  g();\n"
934                "} else {\n"
935                "  g();\n"
936                "}",
937                AllowsMergedIf);
938   verifyFormat("MYIF (a)\n"
939                "  f();\n"
940                "else {\n"
941                "  g();\n"
942                "}",
943                AllowsMergedIf);
944   verifyFormat("MYIF (a)\n"
945                "  f();\n"
946                "else\n"
947                "  g();\n",
948                AllowsMergedIf);
949 
950   verifyFormat("MYIF (a) g();", AllowsMergedIf);
951   verifyFormat("MYIF (a) {\n"
952                "  g()\n"
953                "};",
954                AllowsMergedIf);
955   verifyFormat("MYIF (a)\n"
956                "  g();\n"
957                "else\n"
958                "  g();",
959                AllowsMergedIf);
960   verifyFormat("MYIF (a) {\n"
961                "  g();\n"
962                "} else\n"
963                "  g();",
964                AllowsMergedIf);
965   verifyFormat("MYIF (a)\n"
966                "  g();\n"
967                "else {\n"
968                "  g();\n"
969                "}",
970                AllowsMergedIf);
971   verifyFormat("MYIF (a) {\n"
972                "  g();\n"
973                "} else {\n"
974                "  g();\n"
975                "}",
976                AllowsMergedIf);
977   verifyFormat("MYIF (a)\n"
978                "  g();\n"
979                "else MYIF (b)\n"
980                "  g();\n"
981                "else\n"
982                "  g();",
983                AllowsMergedIf);
984   verifyFormat("MYIF (a)\n"
985                "  g();\n"
986                "else if (b)\n"
987                "  g();\n"
988                "else\n"
989                "  g();",
990                AllowsMergedIf);
991   verifyFormat("MYIF (a) {\n"
992                "  g();\n"
993                "} else MYIF (b)\n"
994                "  g();\n"
995                "else\n"
996                "  g();",
997                AllowsMergedIf);
998   verifyFormat("MYIF (a) {\n"
999                "  g();\n"
1000                "} else if (b)\n"
1001                "  g();\n"
1002                "else\n"
1003                "  g();",
1004                AllowsMergedIf);
1005   verifyFormat("MYIF (a)\n"
1006                "  g();\n"
1007                "else MYIF (b) {\n"
1008                "  g();\n"
1009                "} else\n"
1010                "  g();",
1011                AllowsMergedIf);
1012   verifyFormat("MYIF (a)\n"
1013                "  g();\n"
1014                "else if (b) {\n"
1015                "  g();\n"
1016                "} else\n"
1017                "  g();",
1018                AllowsMergedIf);
1019   verifyFormat("MYIF (a)\n"
1020                "  g();\n"
1021                "else MYIF (b)\n"
1022                "  g();\n"
1023                "else {\n"
1024                "  g();\n"
1025                "}",
1026                AllowsMergedIf);
1027   verifyFormat("MYIF (a)\n"
1028                "  g();\n"
1029                "else if (b)\n"
1030                "  g();\n"
1031                "else {\n"
1032                "  g();\n"
1033                "}",
1034                AllowsMergedIf);
1035   verifyFormat("MYIF (a)\n"
1036                "  g();\n"
1037                "else MYIF (b) {\n"
1038                "  g();\n"
1039                "} else {\n"
1040                "  g();\n"
1041                "}",
1042                AllowsMergedIf);
1043   verifyFormat("MYIF (a)\n"
1044                "  g();\n"
1045                "else if (b) {\n"
1046                "  g();\n"
1047                "} else {\n"
1048                "  g();\n"
1049                "}",
1050                AllowsMergedIf);
1051   verifyFormat("MYIF (a) {\n"
1052                "  g();\n"
1053                "} else MYIF (b) {\n"
1054                "  g();\n"
1055                "} else {\n"
1056                "  g();\n"
1057                "}",
1058                AllowsMergedIf);
1059   verifyFormat("MYIF (a) {\n"
1060                "  g();\n"
1061                "} else if (b) {\n"
1062                "  g();\n"
1063                "} else {\n"
1064                "  g();\n"
1065                "}",
1066                AllowsMergedIf);
1067 
1068   AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
1069       FormatStyle::SIS_OnlyFirstIf;
1070 
1071   verifyFormat("if (a) f();\n"
1072                "else {\n"
1073                "  g();\n"
1074                "}",
1075                AllowsMergedIf);
1076   verifyFormat("if (a) f();\n"
1077                "else {\n"
1078                "  if (a) f();\n"
1079                "  else {\n"
1080                "    g();\n"
1081                "  }\n"
1082                "  g();\n"
1083                "}",
1084                AllowsMergedIf);
1085 
1086   verifyFormat("if (a) g();", AllowsMergedIf);
1087   verifyFormat("if (a) {\n"
1088                "  g()\n"
1089                "};",
1090                AllowsMergedIf);
1091   verifyFormat("if (a) g();\n"
1092                "else\n"
1093                "  g();",
1094                AllowsMergedIf);
1095   verifyFormat("if (a) {\n"
1096                "  g();\n"
1097                "} else\n"
1098                "  g();",
1099                AllowsMergedIf);
1100   verifyFormat("if (a) g();\n"
1101                "else {\n"
1102                "  g();\n"
1103                "}",
1104                AllowsMergedIf);
1105   verifyFormat("if (a) {\n"
1106                "  g();\n"
1107                "} else {\n"
1108                "  g();\n"
1109                "}",
1110                AllowsMergedIf);
1111   verifyFormat("if (a) g();\n"
1112                "else if (b)\n"
1113                "  g();\n"
1114                "else\n"
1115                "  g();",
1116                AllowsMergedIf);
1117   verifyFormat("if (a) {\n"
1118                "  g();\n"
1119                "} else if (b)\n"
1120                "  g();\n"
1121                "else\n"
1122                "  g();",
1123                AllowsMergedIf);
1124   verifyFormat("if (a) g();\n"
1125                "else if (b) {\n"
1126                "  g();\n"
1127                "} else\n"
1128                "  g();",
1129                AllowsMergedIf);
1130   verifyFormat("if (a) g();\n"
1131                "else if (b)\n"
1132                "  g();\n"
1133                "else {\n"
1134                "  g();\n"
1135                "}",
1136                AllowsMergedIf);
1137   verifyFormat("if (a) g();\n"
1138                "else if (b) {\n"
1139                "  g();\n"
1140                "} else {\n"
1141                "  g();\n"
1142                "}",
1143                AllowsMergedIf);
1144   verifyFormat("if (a) {\n"
1145                "  g();\n"
1146                "} else if (b) {\n"
1147                "  g();\n"
1148                "} else {\n"
1149                "  g();\n"
1150                "}",
1151                AllowsMergedIf);
1152   verifyFormat("MYIF (a) f();\n"
1153                "else {\n"
1154                "  g();\n"
1155                "}",
1156                AllowsMergedIf);
1157   verifyFormat("MYIF (a) f();\n"
1158                "else {\n"
1159                "  if (a) f();\n"
1160                "  else {\n"
1161                "    g();\n"
1162                "  }\n"
1163                "  g();\n"
1164                "}",
1165                AllowsMergedIf);
1166 
1167   verifyFormat("MYIF (a) g();", AllowsMergedIf);
1168   verifyFormat("MYIF (a) {\n"
1169                "  g()\n"
1170                "};",
1171                AllowsMergedIf);
1172   verifyFormat("MYIF (a) g();\n"
1173                "else\n"
1174                "  g();",
1175                AllowsMergedIf);
1176   verifyFormat("MYIF (a) {\n"
1177                "  g();\n"
1178                "} else\n"
1179                "  g();",
1180                AllowsMergedIf);
1181   verifyFormat("MYIF (a) g();\n"
1182                "else {\n"
1183                "  g();\n"
1184                "}",
1185                AllowsMergedIf);
1186   verifyFormat("MYIF (a) {\n"
1187                "  g();\n"
1188                "} else {\n"
1189                "  g();\n"
1190                "}",
1191                AllowsMergedIf);
1192   verifyFormat("MYIF (a) g();\n"
1193                "else MYIF (b)\n"
1194                "  g();\n"
1195                "else\n"
1196                "  g();",
1197                AllowsMergedIf);
1198   verifyFormat("MYIF (a) g();\n"
1199                "else if (b)\n"
1200                "  g();\n"
1201                "else\n"
1202                "  g();",
1203                AllowsMergedIf);
1204   verifyFormat("MYIF (a) {\n"
1205                "  g();\n"
1206                "} else MYIF (b)\n"
1207                "  g();\n"
1208                "else\n"
1209                "  g();",
1210                AllowsMergedIf);
1211   verifyFormat("MYIF (a) {\n"
1212                "  g();\n"
1213                "} else if (b)\n"
1214                "  g();\n"
1215                "else\n"
1216                "  g();",
1217                AllowsMergedIf);
1218   verifyFormat("MYIF (a) g();\n"
1219                "else MYIF (b) {\n"
1220                "  g();\n"
1221                "} else\n"
1222                "  g();",
1223                AllowsMergedIf);
1224   verifyFormat("MYIF (a) g();\n"
1225                "else if (b) {\n"
1226                "  g();\n"
1227                "} else\n"
1228                "  g();",
1229                AllowsMergedIf);
1230   verifyFormat("MYIF (a) g();\n"
1231                "else MYIF (b)\n"
1232                "  g();\n"
1233                "else {\n"
1234                "  g();\n"
1235                "}",
1236                AllowsMergedIf);
1237   verifyFormat("MYIF (a) g();\n"
1238                "else if (b)\n"
1239                "  g();\n"
1240                "else {\n"
1241                "  g();\n"
1242                "}",
1243                AllowsMergedIf);
1244   verifyFormat("MYIF (a) g();\n"
1245                "else MYIF (b) {\n"
1246                "  g();\n"
1247                "} else {\n"
1248                "  g();\n"
1249                "}",
1250                AllowsMergedIf);
1251   verifyFormat("MYIF (a) g();\n"
1252                "else if (b) {\n"
1253                "  g();\n"
1254                "} else {\n"
1255                "  g();\n"
1256                "}",
1257                AllowsMergedIf);
1258   verifyFormat("MYIF (a) {\n"
1259                "  g();\n"
1260                "} else MYIF (b) {\n"
1261                "  g();\n"
1262                "} else {\n"
1263                "  g();\n"
1264                "}",
1265                AllowsMergedIf);
1266   verifyFormat("MYIF (a) {\n"
1267                "  g();\n"
1268                "} else if (b) {\n"
1269                "  g();\n"
1270                "} else {\n"
1271                "  g();\n"
1272                "}",
1273                AllowsMergedIf);
1274 
1275   AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
1276       FormatStyle::SIS_AllIfsAndElse;
1277 
1278   verifyFormat("if (a) f();\n"
1279                "else {\n"
1280                "  g();\n"
1281                "}",
1282                AllowsMergedIf);
1283   verifyFormat("if (a) f();\n"
1284                "else {\n"
1285                "  if (a) f();\n"
1286                "  else {\n"
1287                "    g();\n"
1288                "  }\n"
1289                "  g();\n"
1290                "}",
1291                AllowsMergedIf);
1292 
1293   verifyFormat("if (a) g();", AllowsMergedIf);
1294   verifyFormat("if (a) {\n"
1295                "  g()\n"
1296                "};",
1297                AllowsMergedIf);
1298   verifyFormat("if (a) g();\n"
1299                "else g();",
1300                AllowsMergedIf);
1301   verifyFormat("if (a) {\n"
1302                "  g();\n"
1303                "} else g();",
1304                AllowsMergedIf);
1305   verifyFormat("if (a) g();\n"
1306                "else {\n"
1307                "  g();\n"
1308                "}",
1309                AllowsMergedIf);
1310   verifyFormat("if (a) {\n"
1311                "  g();\n"
1312                "} else {\n"
1313                "  g();\n"
1314                "}",
1315                AllowsMergedIf);
1316   verifyFormat("if (a) g();\n"
1317                "else if (b) g();\n"
1318                "else g();",
1319                AllowsMergedIf);
1320   verifyFormat("if (a) {\n"
1321                "  g();\n"
1322                "} else if (b) g();\n"
1323                "else g();",
1324                AllowsMergedIf);
1325   verifyFormat("if (a) g();\n"
1326                "else if (b) {\n"
1327                "  g();\n"
1328                "} else g();",
1329                AllowsMergedIf);
1330   verifyFormat("if (a) g();\n"
1331                "else if (b) g();\n"
1332                "else {\n"
1333                "  g();\n"
1334                "}",
1335                AllowsMergedIf);
1336   verifyFormat("if (a) g();\n"
1337                "else if (b) {\n"
1338                "  g();\n"
1339                "} else {\n"
1340                "  g();\n"
1341                "}",
1342                AllowsMergedIf);
1343   verifyFormat("if (a) {\n"
1344                "  g();\n"
1345                "} else if (b) {\n"
1346                "  g();\n"
1347                "} else {\n"
1348                "  g();\n"
1349                "}",
1350                AllowsMergedIf);
1351   verifyFormat("MYIF (a) f();\n"
1352                "else {\n"
1353                "  g();\n"
1354                "}",
1355                AllowsMergedIf);
1356   verifyFormat("MYIF (a) f();\n"
1357                "else {\n"
1358                "  if (a) f();\n"
1359                "  else {\n"
1360                "    g();\n"
1361                "  }\n"
1362                "  g();\n"
1363                "}",
1364                AllowsMergedIf);
1365 
1366   verifyFormat("MYIF (a) g();", AllowsMergedIf);
1367   verifyFormat("MYIF (a) {\n"
1368                "  g()\n"
1369                "};",
1370                AllowsMergedIf);
1371   verifyFormat("MYIF (a) g();\n"
1372                "else g();",
1373                AllowsMergedIf);
1374   verifyFormat("MYIF (a) {\n"
1375                "  g();\n"
1376                "} else g();",
1377                AllowsMergedIf);
1378   verifyFormat("MYIF (a) g();\n"
1379                "else {\n"
1380                "  g();\n"
1381                "}",
1382                AllowsMergedIf);
1383   verifyFormat("MYIF (a) {\n"
1384                "  g();\n"
1385                "} else {\n"
1386                "  g();\n"
1387                "}",
1388                AllowsMergedIf);
1389   verifyFormat("MYIF (a) g();\n"
1390                "else MYIF (b) g();\n"
1391                "else g();",
1392                AllowsMergedIf);
1393   verifyFormat("MYIF (a) g();\n"
1394                "else if (b) g();\n"
1395                "else g();",
1396                AllowsMergedIf);
1397   verifyFormat("MYIF (a) {\n"
1398                "  g();\n"
1399                "} else MYIF (b) g();\n"
1400                "else g();",
1401                AllowsMergedIf);
1402   verifyFormat("MYIF (a) {\n"
1403                "  g();\n"
1404                "} else if (b) g();\n"
1405                "else g();",
1406                AllowsMergedIf);
1407   verifyFormat("MYIF (a) g();\n"
1408                "else MYIF (b) {\n"
1409                "  g();\n"
1410                "} else g();",
1411                AllowsMergedIf);
1412   verifyFormat("MYIF (a) g();\n"
1413                "else if (b) {\n"
1414                "  g();\n"
1415                "} else g();",
1416                AllowsMergedIf);
1417   verifyFormat("MYIF (a) g();\n"
1418                "else MYIF (b) g();\n"
1419                "else {\n"
1420                "  g();\n"
1421                "}",
1422                AllowsMergedIf);
1423   verifyFormat("MYIF (a) g();\n"
1424                "else if (b) g();\n"
1425                "else {\n"
1426                "  g();\n"
1427                "}",
1428                AllowsMergedIf);
1429   verifyFormat("MYIF (a) g();\n"
1430                "else MYIF (b) {\n"
1431                "  g();\n"
1432                "} else {\n"
1433                "  g();\n"
1434                "}",
1435                AllowsMergedIf);
1436   verifyFormat("MYIF (a) g();\n"
1437                "else if (b) {\n"
1438                "  g();\n"
1439                "} else {\n"
1440                "  g();\n"
1441                "}",
1442                AllowsMergedIf);
1443   verifyFormat("MYIF (a) {\n"
1444                "  g();\n"
1445                "} else MYIF (b) {\n"
1446                "  g();\n"
1447                "} else {\n"
1448                "  g();\n"
1449                "}",
1450                AllowsMergedIf);
1451   verifyFormat("MYIF (a) {\n"
1452                "  g();\n"
1453                "} else if (b) {\n"
1454                "  g();\n"
1455                "} else {\n"
1456                "  g();\n"
1457                "}",
1458                AllowsMergedIf);
1459 }
1460 
1461 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
1462   FormatStyle AllowsMergedLoops = getLLVMStyle();
1463   AllowsMergedLoops.AllowShortLoopsOnASingleLine = true;
1464   verifyFormat("while (true) continue;", AllowsMergedLoops);
1465   verifyFormat("for (;;) continue;", AllowsMergedLoops);
1466   verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops);
1467   verifyFormat("while (true)\n"
1468                "  ;",
1469                AllowsMergedLoops);
1470   verifyFormat("for (;;)\n"
1471                "  ;",
1472                AllowsMergedLoops);
1473   verifyFormat("for (;;)\n"
1474                "  for (;;) continue;",
1475                AllowsMergedLoops);
1476   verifyFormat("for (;;) // Can't merge this\n"
1477                "  continue;",
1478                AllowsMergedLoops);
1479   verifyFormat("for (;;) /* still don't merge */\n"
1480                "  continue;",
1481                AllowsMergedLoops);
1482   verifyFormat("do a++;\n"
1483                "while (true);",
1484                AllowsMergedLoops);
1485   verifyFormat("do /* Don't merge */\n"
1486                "  a++;\n"
1487                "while (true);",
1488                AllowsMergedLoops);
1489   verifyFormat("do // Don't merge\n"
1490                "  a++;\n"
1491                "while (true);",
1492                AllowsMergedLoops);
1493   verifyFormat("do\n"
1494                "  // Don't merge\n"
1495                "  a++;\n"
1496                "while (true);",
1497                AllowsMergedLoops);
1498   // Without braces labels are interpreted differently.
1499   verifyFormat("{\n"
1500                "  do\n"
1501                "  label:\n"
1502                "    a++;\n"
1503                "  while (true);\n"
1504                "}",
1505                AllowsMergedLoops);
1506 }
1507 
1508 TEST_F(FormatTest, FormatShortBracedStatements) {
1509   FormatStyle AllowSimpleBracedStatements = getLLVMStyle();
1510   AllowSimpleBracedStatements.IfMacros.push_back("MYIF");
1511   // Where line-lengths matter, a 2-letter synonym that maintains line length.
1512   // Not IF to avoid any confusion that IF is somehow special.
1513   AllowSimpleBracedStatements.IfMacros.push_back("FI");
1514   AllowSimpleBracedStatements.ColumnLimit = 40;
1515   AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine =
1516       FormatStyle::SBS_Always;
1517 
1518   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
1519       FormatStyle::SIS_WithoutElse;
1520   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
1521 
1522   AllowSimpleBracedStatements.BreakBeforeBraces = FormatStyle::BS_Custom;
1523   AllowSimpleBracedStatements.BraceWrapping.AfterFunction = true;
1524   AllowSimpleBracedStatements.BraceWrapping.SplitEmptyRecord = false;
1525 
1526   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
1527   verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements);
1528   verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements);
1529   verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements);
1530   verifyFormat("MYIF constexpr (true) {}", AllowSimpleBracedStatements);
1531   verifyFormat("MYIF CONSTEXPR (true) {}", AllowSimpleBracedStatements);
1532   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
1533   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
1534   verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
1535   verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements);
1536   verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements);
1537   verifyFormat("MYIF (true) { f(); }", AllowSimpleBracedStatements);
1538   verifyFormat("MYIF constexpr (true) { f(); }", AllowSimpleBracedStatements);
1539   verifyFormat("MYIF CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements);
1540   verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
1541   verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
1542   verifyFormat("if (true) { fffffffffffffffffffffff(); }",
1543                AllowSimpleBracedStatements);
1544   verifyFormat("if (true) {\n"
1545                "  ffffffffffffffffffffffff();\n"
1546                "}",
1547                AllowSimpleBracedStatements);
1548   verifyFormat("if (true) {\n"
1549                "  ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
1550                "}",
1551                AllowSimpleBracedStatements);
1552   verifyFormat("if (true) { //\n"
1553                "  f();\n"
1554                "}",
1555                AllowSimpleBracedStatements);
1556   verifyFormat("if (true) {\n"
1557                "  f();\n"
1558                "  f();\n"
1559                "}",
1560                AllowSimpleBracedStatements);
1561   verifyFormat("if (true) {\n"
1562                "  f();\n"
1563                "} else {\n"
1564                "  f();\n"
1565                "}",
1566                AllowSimpleBracedStatements);
1567   verifyFormat("FI (true) { fffffffffffffffffffffff(); }",
1568                AllowSimpleBracedStatements);
1569   verifyFormat("MYIF (true) {\n"
1570                "  ffffffffffffffffffffffff();\n"
1571                "}",
1572                AllowSimpleBracedStatements);
1573   verifyFormat("MYIF (true) {\n"
1574                "  ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
1575                "}",
1576                AllowSimpleBracedStatements);
1577   verifyFormat("MYIF (true) { //\n"
1578                "  f();\n"
1579                "}",
1580                AllowSimpleBracedStatements);
1581   verifyFormat("MYIF (true) {\n"
1582                "  f();\n"
1583                "  f();\n"
1584                "}",
1585                AllowSimpleBracedStatements);
1586   verifyFormat("MYIF (true) {\n"
1587                "  f();\n"
1588                "} else {\n"
1589                "  f();\n"
1590                "}",
1591                AllowSimpleBracedStatements);
1592 
1593   verifyFormat("struct A2 {\n"
1594                "  int X;\n"
1595                "};",
1596                AllowSimpleBracedStatements);
1597   verifyFormat("typedef struct A2 {\n"
1598                "  int X;\n"
1599                "} A2_t;",
1600                AllowSimpleBracedStatements);
1601   verifyFormat("template <int> struct A2 {\n"
1602                "  struct B {};\n"
1603                "};",
1604                AllowSimpleBracedStatements);
1605 
1606   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
1607       FormatStyle::SIS_Never;
1608   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
1609   verifyFormat("if (true) {\n"
1610                "  f();\n"
1611                "}",
1612                AllowSimpleBracedStatements);
1613   verifyFormat("if (true) {\n"
1614                "  f();\n"
1615                "} else {\n"
1616                "  f();\n"
1617                "}",
1618                AllowSimpleBracedStatements);
1619   verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements);
1620   verifyFormat("MYIF (true) {\n"
1621                "  f();\n"
1622                "}",
1623                AllowSimpleBracedStatements);
1624   verifyFormat("MYIF (true) {\n"
1625                "  f();\n"
1626                "} else {\n"
1627                "  f();\n"
1628                "}",
1629                AllowSimpleBracedStatements);
1630 
1631   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
1632   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
1633   verifyFormat("while (true) {\n"
1634                "  f();\n"
1635                "}",
1636                AllowSimpleBracedStatements);
1637   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
1638   verifyFormat("for (;;) {\n"
1639                "  f();\n"
1640                "}",
1641                AllowSimpleBracedStatements);
1642 
1643   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
1644       FormatStyle::SIS_WithoutElse;
1645   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
1646   AllowSimpleBracedStatements.BraceWrapping.AfterControlStatement =
1647       FormatStyle::BWACS_Always;
1648 
1649   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
1650   verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements);
1651   verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements);
1652   verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements);
1653   verifyFormat("MYIF constexpr (true) {}", AllowSimpleBracedStatements);
1654   verifyFormat("MYIF CONSTEXPR (true) {}", AllowSimpleBracedStatements);
1655   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
1656   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
1657   verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
1658   verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements);
1659   verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements);
1660   verifyFormat("MYIF (true) { f(); }", AllowSimpleBracedStatements);
1661   verifyFormat("MYIF constexpr (true) { f(); }", AllowSimpleBracedStatements);
1662   verifyFormat("MYIF CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements);
1663   verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
1664   verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
1665   verifyFormat("if (true) { fffffffffffffffffffffff(); }",
1666                AllowSimpleBracedStatements);
1667   verifyFormat("if (true)\n"
1668                "{\n"
1669                "  ffffffffffffffffffffffff();\n"
1670                "}",
1671                AllowSimpleBracedStatements);
1672   verifyFormat("if (true)\n"
1673                "{\n"
1674                "  ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
1675                "}",
1676                AllowSimpleBracedStatements);
1677   verifyFormat("if (true)\n"
1678                "{ //\n"
1679                "  f();\n"
1680                "}",
1681                AllowSimpleBracedStatements);
1682   verifyFormat("if (true)\n"
1683                "{\n"
1684                "  f();\n"
1685                "  f();\n"
1686                "}",
1687                AllowSimpleBracedStatements);
1688   verifyFormat("if (true)\n"
1689                "{\n"
1690                "  f();\n"
1691                "} else\n"
1692                "{\n"
1693                "  f();\n"
1694                "}",
1695                AllowSimpleBracedStatements);
1696   verifyFormat("FI (true) { fffffffffffffffffffffff(); }",
1697                AllowSimpleBracedStatements);
1698   verifyFormat("MYIF (true)\n"
1699                "{\n"
1700                "  ffffffffffffffffffffffff();\n"
1701                "}",
1702                AllowSimpleBracedStatements);
1703   verifyFormat("MYIF (true)\n"
1704                "{\n"
1705                "  ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
1706                "}",
1707                AllowSimpleBracedStatements);
1708   verifyFormat("MYIF (true)\n"
1709                "{ //\n"
1710                "  f();\n"
1711                "}",
1712                AllowSimpleBracedStatements);
1713   verifyFormat("MYIF (true)\n"
1714                "{\n"
1715                "  f();\n"
1716                "  f();\n"
1717                "}",
1718                AllowSimpleBracedStatements);
1719   verifyFormat("MYIF (true)\n"
1720                "{\n"
1721                "  f();\n"
1722                "} else\n"
1723                "{\n"
1724                "  f();\n"
1725                "}",
1726                AllowSimpleBracedStatements);
1727 
1728   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
1729       FormatStyle::SIS_Never;
1730   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
1731   verifyFormat("if (true)\n"
1732                "{\n"
1733                "  f();\n"
1734                "}",
1735                AllowSimpleBracedStatements);
1736   verifyFormat("if (true)\n"
1737                "{\n"
1738                "  f();\n"
1739                "} else\n"
1740                "{\n"
1741                "  f();\n"
1742                "}",
1743                AllowSimpleBracedStatements);
1744   verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements);
1745   verifyFormat("MYIF (true)\n"
1746                "{\n"
1747                "  f();\n"
1748                "}",
1749                AllowSimpleBracedStatements);
1750   verifyFormat("MYIF (true)\n"
1751                "{\n"
1752                "  f();\n"
1753                "} else\n"
1754                "{\n"
1755                "  f();\n"
1756                "}",
1757                AllowSimpleBracedStatements);
1758 
1759   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
1760   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
1761   verifyFormat("while (true)\n"
1762                "{\n"
1763                "  f();\n"
1764                "}",
1765                AllowSimpleBracedStatements);
1766   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
1767   verifyFormat("for (;;)\n"
1768                "{\n"
1769                "  f();\n"
1770                "}",
1771                AllowSimpleBracedStatements);
1772 }
1773 
1774 TEST_F(FormatTest, ShortBlocksInMacrosDontMergeWithCodeAfterMacro) {
1775   FormatStyle Style = getLLVMStyleWithColumns(60);
1776   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
1777   Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse;
1778   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
1779   EXPECT_EQ("#define A                                                  \\\n"
1780             "  if (HANDLEwernufrnuLwrmviferuvnierv)                     \\\n"
1781             "  {                                                        \\\n"
1782             "    RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier;               \\\n"
1783             "  }\n"
1784             "X;",
1785             format("#define A \\\n"
1786                    "   if (HANDLEwernufrnuLwrmviferuvnierv) { \\\n"
1787                    "      RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n"
1788                    "   }\n"
1789                    "X;",
1790                    Style));
1791 }
1792 
1793 TEST_F(FormatTest, ParseIfElse) {
1794   verifyFormat("if (true)\n"
1795                "  if (true)\n"
1796                "    if (true)\n"
1797                "      f();\n"
1798                "    else\n"
1799                "      g();\n"
1800                "  else\n"
1801                "    h();\n"
1802                "else\n"
1803                "  i();");
1804   verifyFormat("if (true)\n"
1805                "  if (true)\n"
1806                "    if (true) {\n"
1807                "      if (true)\n"
1808                "        f();\n"
1809                "    } else {\n"
1810                "      g();\n"
1811                "    }\n"
1812                "  else\n"
1813                "    h();\n"
1814                "else {\n"
1815                "  i();\n"
1816                "}");
1817   verifyFormat("if (true)\n"
1818                "  if constexpr (true)\n"
1819                "    if (true) {\n"
1820                "      if constexpr (true)\n"
1821                "        f();\n"
1822                "    } else {\n"
1823                "      g();\n"
1824                "    }\n"
1825                "  else\n"
1826                "    h();\n"
1827                "else {\n"
1828                "  i();\n"
1829                "}");
1830   verifyFormat("if (true)\n"
1831                "  if CONSTEXPR (true)\n"
1832                "    if (true) {\n"
1833                "      if CONSTEXPR (true)\n"
1834                "        f();\n"
1835                "    } else {\n"
1836                "      g();\n"
1837                "    }\n"
1838                "  else\n"
1839                "    h();\n"
1840                "else {\n"
1841                "  i();\n"
1842                "}");
1843   verifyFormat("void f() {\n"
1844                "  if (a) {\n"
1845                "  } else {\n"
1846                "  }\n"
1847                "}");
1848 }
1849 
1850 TEST_F(FormatTest, ElseIf) {
1851   verifyFormat("if (a) {\n} else if (b) {\n}");
1852   verifyFormat("if (a)\n"
1853                "  f();\n"
1854                "else if (b)\n"
1855                "  g();\n"
1856                "else\n"
1857                "  h();");
1858   verifyFormat("if (a)\n"
1859                "  f();\n"
1860                "else // comment\n"
1861                "  if (b) {\n"
1862                "    g();\n"
1863                "    h();\n"
1864                "  }");
1865   verifyFormat("if constexpr (a)\n"
1866                "  f();\n"
1867                "else if constexpr (b)\n"
1868                "  g();\n"
1869                "else\n"
1870                "  h();");
1871   verifyFormat("if CONSTEXPR (a)\n"
1872                "  f();\n"
1873                "else if CONSTEXPR (b)\n"
1874                "  g();\n"
1875                "else\n"
1876                "  h();");
1877   verifyFormat("if (a) {\n"
1878                "  f();\n"
1879                "}\n"
1880                "// or else ..\n"
1881                "else {\n"
1882                "  g()\n"
1883                "}");
1884 
1885   verifyFormat("if (a) {\n"
1886                "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1887                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
1888                "}");
1889   verifyFormat("if (a) {\n"
1890                "} else if constexpr (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1891                "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
1892                "}");
1893   verifyFormat("if (a) {\n"
1894                "} else if CONSTEXPR (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1895                "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
1896                "}");
1897   verifyFormat("if (a) {\n"
1898                "} else if (\n"
1899                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
1900                "}",
1901                getLLVMStyleWithColumns(62));
1902   verifyFormat("if (a) {\n"
1903                "} else if constexpr (\n"
1904                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
1905                "}",
1906                getLLVMStyleWithColumns(62));
1907   verifyFormat("if (a) {\n"
1908                "} else if CONSTEXPR (\n"
1909                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
1910                "}",
1911                getLLVMStyleWithColumns(62));
1912 }
1913 
1914 TEST_F(FormatTest, SeparatePointerReferenceAlignment) {
1915   FormatStyle Style = getLLVMStyle();
1916   // Check first the default LLVM style
1917   // Style.PointerAlignment = FormatStyle::PAS_Right;
1918   // Style.ReferenceAlignment = FormatStyle::RAS_Pointer;
1919   verifyFormat("int *f1(int *a, int &b, int &&c);", Style);
1920   verifyFormat("int &f2(int &&c, int *a, int &b);", Style);
1921   verifyFormat("int &&f3(int &b, int &&c, int *a);", Style);
1922   verifyFormat("int *f1(int &a) const &;", Style);
1923   verifyFormat("int *f1(int &a) const & = 0;", Style);
1924   verifyFormat("int *a = f1();", Style);
1925   verifyFormat("int &b = f2();", Style);
1926   verifyFormat("int &&c = f3();", Style);
1927 
1928   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
1929   verifyFormat("Const unsigned int *c;\n"
1930                "const unsigned int *d;\n"
1931                "Const unsigned int &e;\n"
1932                "const unsigned int &f;\n"
1933                "const unsigned    &&g;\n"
1934                "Const unsigned      h;",
1935                Style);
1936 
1937   Style.PointerAlignment = FormatStyle::PAS_Left;
1938   Style.ReferenceAlignment = FormatStyle::RAS_Pointer;
1939   verifyFormat("int* f1(int* a, int& b, int&& c);", Style);
1940   verifyFormat("int& f2(int&& c, int* a, int& b);", Style);
1941   verifyFormat("int&& f3(int& b, int&& c, int* a);", Style);
1942   verifyFormat("int* f1(int& a) const& = 0;", Style);
1943   verifyFormat("int* a = f1();", Style);
1944   verifyFormat("int& b = f2();", Style);
1945   verifyFormat("int&& c = f3();", Style);
1946 
1947   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
1948   verifyFormat("Const unsigned int* c;\n"
1949                "const unsigned int* d;\n"
1950                "Const unsigned int& e;\n"
1951                "const unsigned int& f;\n"
1952                "const unsigned&&    g;\n"
1953                "Const unsigned      h;",
1954                Style);
1955 
1956   Style.PointerAlignment = FormatStyle::PAS_Right;
1957   Style.ReferenceAlignment = FormatStyle::RAS_Left;
1958   verifyFormat("int *f1(int *a, int& b, int&& c);", Style);
1959   verifyFormat("int& f2(int&& c, int *a, int& b);", Style);
1960   verifyFormat("int&& f3(int& b, int&& c, int *a);", Style);
1961   verifyFormat("int *a = f1();", Style);
1962   verifyFormat("int& b = f2();", Style);
1963   verifyFormat("int&& c = f3();", Style);
1964 
1965   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
1966   verifyFormat("Const unsigned int *c;\n"
1967                "const unsigned int *d;\n"
1968                "Const unsigned int& e;\n"
1969                "const unsigned int& f;\n"
1970                "const unsigned      g;\n"
1971                "Const unsigned      h;",
1972                Style);
1973 
1974   Style.PointerAlignment = FormatStyle::PAS_Left;
1975   Style.ReferenceAlignment = FormatStyle::RAS_Middle;
1976   verifyFormat("int* f1(int* a, int & b, int && c);", Style);
1977   verifyFormat("int & f2(int && c, int* a, int & b);", Style);
1978   verifyFormat("int && f3(int & b, int && c, int* a);", Style);
1979   verifyFormat("int* a = f1();", Style);
1980   verifyFormat("int & b = f2();", Style);
1981   verifyFormat("int && c = f3();", Style);
1982 
1983   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
1984   verifyFormat("Const unsigned int*  c;\n"
1985                "const unsigned int*  d;\n"
1986                "Const unsigned int & e;\n"
1987                "const unsigned int & f;\n"
1988                "const unsigned &&    g;\n"
1989                "Const unsigned       h;",
1990                Style);
1991 
1992   Style.PointerAlignment = FormatStyle::PAS_Middle;
1993   Style.ReferenceAlignment = FormatStyle::RAS_Right;
1994   verifyFormat("int * f1(int * a, int &b, int &&c);", Style);
1995   verifyFormat("int &f2(int &&c, int * a, int &b);", Style);
1996   verifyFormat("int &&f3(int &b, int &&c, int * a);", Style);
1997   verifyFormat("int * a = f1();", Style);
1998   verifyFormat("int &b = f2();", Style);
1999   verifyFormat("int &&c = f3();", Style);
2000 
2001   // FIXME: we don't handle this yet, so output may be arbitrary until it's
2002   // specifically handled
2003   // verifyFormat("int Add2(BTree * &Root, char * szToAdd)", Style);
2004 }
2005 
2006 TEST_F(FormatTest, FormatsForLoop) {
2007   verifyFormat(
2008       "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
2009       "     ++VeryVeryLongLoopVariable)\n"
2010       "  ;");
2011   verifyFormat("for (;;)\n"
2012                "  f();");
2013   verifyFormat("for (;;) {\n}");
2014   verifyFormat("for (;;) {\n"
2015                "  f();\n"
2016                "}");
2017   verifyFormat("for (int i = 0; (i < 10); ++i) {\n}");
2018 
2019   verifyFormat(
2020       "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
2021       "                                          E = UnwrappedLines.end();\n"
2022       "     I != E; ++I) {\n}");
2023 
2024   verifyFormat(
2025       "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
2026       "     ++IIIII) {\n}");
2027   verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n"
2028                "         aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n"
2029                "     aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}");
2030   verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n"
2031                "         I = FD->getDeclsInPrototypeScope().begin(),\n"
2032                "         E = FD->getDeclsInPrototypeScope().end();\n"
2033                "     I != E; ++I) {\n}");
2034   verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n"
2035                "         I = Container.begin(),\n"
2036                "         E = Container.end();\n"
2037                "     I != E; ++I) {\n}",
2038                getLLVMStyleWithColumns(76));
2039 
2040   verifyFormat(
2041       "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
2042       "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n"
2043       "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2044       "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
2045       "     ++aaaaaaaaaaa) {\n}");
2046   verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
2047                "                bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n"
2048                "     ++i) {\n}");
2049   verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n"
2050                "     aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
2051                "}");
2052   verifyFormat("for (some_namespace::SomeIterator iter( // force break\n"
2053                "         aaaaaaaaaa);\n"
2054                "     iter; ++iter) {\n"
2055                "}");
2056   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2057                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
2058                "     aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n"
2059                "     ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {");
2060 
2061   // These should not be formatted as Objective-C for-in loops.
2062   verifyFormat("for (Foo *x = 0; x != in; x++) {\n}");
2063   verifyFormat("Foo *x;\nfor (x = 0; x != in; x++) {\n}");
2064   verifyFormat("Foo *x;\nfor (x in y) {\n}");
2065   verifyFormat(
2066       "for (const Foo<Bar> &baz = in.value(); !baz.at_end(); ++baz) {\n}");
2067 
2068   FormatStyle NoBinPacking = getLLVMStyle();
2069   NoBinPacking.BinPackParameters = false;
2070   verifyFormat("for (int aaaaaaaaaaa = 1;\n"
2071                "     aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n"
2072                "                                           aaaaaaaaaaaaaaaa,\n"
2073                "                                           aaaaaaaaaaaaaaaa,\n"
2074                "                                           aaaaaaaaaaaaaaaa);\n"
2075                "     aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
2076                "}",
2077                NoBinPacking);
2078   verifyFormat(
2079       "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
2080       "                                          E = UnwrappedLines.end();\n"
2081       "     I != E;\n"
2082       "     ++I) {\n}",
2083       NoBinPacking);
2084 
2085   FormatStyle AlignLeft = getLLVMStyle();
2086   AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
2087   verifyFormat("for (A* a = start; a < end; ++a, ++value) {\n}", AlignLeft);
2088 }
2089 
2090 TEST_F(FormatTest, RangeBasedForLoops) {
2091   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
2092                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
2093   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n"
2094                "     aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}");
2095   verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n"
2096                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
2097   verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n"
2098                "     aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}");
2099 }
2100 
2101 TEST_F(FormatTest, ForEachLoops) {
2102   verifyFormat("void f() {\n"
2103                "  foreach (Item *item, itemlist) {}\n"
2104                "  Q_FOREACH (Item *item, itemlist) {}\n"
2105                "  BOOST_FOREACH (Item *item, itemlist) {}\n"
2106                "  UNKNOWN_FORACH(Item * item, itemlist) {}\n"
2107                "}");
2108 
2109   FormatStyle Style = getLLVMStyle();
2110   Style.SpaceBeforeParens =
2111       FormatStyle::SBPO_ControlStatementsExceptControlMacros;
2112   verifyFormat("void f() {\n"
2113                "  foreach(Item *item, itemlist) {}\n"
2114                "  Q_FOREACH(Item *item, itemlist) {}\n"
2115                "  BOOST_FOREACH(Item *item, itemlist) {}\n"
2116                "  UNKNOWN_FORACH(Item * item, itemlist) {}\n"
2117                "}",
2118                Style);
2119 
2120   // As function-like macros.
2121   verifyFormat("#define foreach(x, y)\n"
2122                "#define Q_FOREACH(x, y)\n"
2123                "#define BOOST_FOREACH(x, y)\n"
2124                "#define UNKNOWN_FOREACH(x, y)\n");
2125 
2126   // Not as function-like macros.
2127   verifyFormat("#define foreach (x, y)\n"
2128                "#define Q_FOREACH (x, y)\n"
2129                "#define BOOST_FOREACH (x, y)\n"
2130                "#define UNKNOWN_FOREACH (x, y)\n");
2131 
2132   // handle microsoft non standard extension
2133   verifyFormat("for each (char c in x->MyStringProperty)");
2134 }
2135 
2136 TEST_F(FormatTest, FormatsWhileLoop) {
2137   verifyFormat("while (true) {\n}");
2138   verifyFormat("while (true)\n"
2139                "  f();");
2140   verifyFormat("while () {\n}");
2141   verifyFormat("while () {\n"
2142                "  f();\n"
2143                "}");
2144 }
2145 
2146 TEST_F(FormatTest, FormatsDoWhile) {
2147   verifyFormat("do {\n"
2148                "  do_something();\n"
2149                "} while (something());");
2150   verifyFormat("do\n"
2151                "  do_something();\n"
2152                "while (something());");
2153 }
2154 
2155 TEST_F(FormatTest, FormatsSwitchStatement) {
2156   verifyFormat("switch (x) {\n"
2157                "case 1:\n"
2158                "  f();\n"
2159                "  break;\n"
2160                "case kFoo:\n"
2161                "case ns::kBar:\n"
2162                "case kBaz:\n"
2163                "  break;\n"
2164                "default:\n"
2165                "  g();\n"
2166                "  break;\n"
2167                "}");
2168   verifyFormat("switch (x) {\n"
2169                "case 1: {\n"
2170                "  f();\n"
2171                "  break;\n"
2172                "}\n"
2173                "case 2: {\n"
2174                "  break;\n"
2175                "}\n"
2176                "}");
2177   verifyFormat("switch (x) {\n"
2178                "case 1: {\n"
2179                "  f();\n"
2180                "  {\n"
2181                "    g();\n"
2182                "    h();\n"
2183                "  }\n"
2184                "  break;\n"
2185                "}\n"
2186                "}");
2187   verifyFormat("switch (x) {\n"
2188                "case 1: {\n"
2189                "  f();\n"
2190                "  if (foo) {\n"
2191                "    g();\n"
2192                "    h();\n"
2193                "  }\n"
2194                "  break;\n"
2195                "}\n"
2196                "}");
2197   verifyFormat("switch (x) {\n"
2198                "case 1: {\n"
2199                "  f();\n"
2200                "  g();\n"
2201                "} break;\n"
2202                "}");
2203   verifyFormat("switch (test)\n"
2204                "  ;");
2205   verifyFormat("switch (x) {\n"
2206                "default: {\n"
2207                "  // Do nothing.\n"
2208                "}\n"
2209                "}");
2210   verifyFormat("switch (x) {\n"
2211                "// comment\n"
2212                "// if 1, do f()\n"
2213                "case 1:\n"
2214                "  f();\n"
2215                "}");
2216   verifyFormat("switch (x) {\n"
2217                "case 1:\n"
2218                "  // Do amazing stuff\n"
2219                "  {\n"
2220                "    f();\n"
2221                "    g();\n"
2222                "  }\n"
2223                "  break;\n"
2224                "}");
2225   verifyFormat("#define A          \\\n"
2226                "  switch (x) {     \\\n"
2227                "  case a:          \\\n"
2228                "    foo = b;       \\\n"
2229                "  }",
2230                getLLVMStyleWithColumns(20));
2231   verifyFormat("#define OPERATION_CASE(name)           \\\n"
2232                "  case OP_name:                        \\\n"
2233                "    return operations::Operation##name\n",
2234                getLLVMStyleWithColumns(40));
2235   verifyFormat("switch (x) {\n"
2236                "case 1:;\n"
2237                "default:;\n"
2238                "  int i;\n"
2239                "}");
2240 
2241   verifyGoogleFormat("switch (x) {\n"
2242                      "  case 1:\n"
2243                      "    f();\n"
2244                      "    break;\n"
2245                      "  case kFoo:\n"
2246                      "  case ns::kBar:\n"
2247                      "  case kBaz:\n"
2248                      "    break;\n"
2249                      "  default:\n"
2250                      "    g();\n"
2251                      "    break;\n"
2252                      "}");
2253   verifyGoogleFormat("switch (x) {\n"
2254                      "  case 1: {\n"
2255                      "    f();\n"
2256                      "    break;\n"
2257                      "  }\n"
2258                      "}");
2259   verifyGoogleFormat("switch (test)\n"
2260                      "  ;");
2261 
2262   verifyGoogleFormat("#define OPERATION_CASE(name) \\\n"
2263                      "  case OP_name:              \\\n"
2264                      "    return operations::Operation##name\n");
2265   verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n"
2266                      "  // Get the correction operation class.\n"
2267                      "  switch (OpCode) {\n"
2268                      "    CASE(Add);\n"
2269                      "    CASE(Subtract);\n"
2270                      "    default:\n"
2271                      "      return operations::Unknown;\n"
2272                      "  }\n"
2273                      "#undef OPERATION_CASE\n"
2274                      "}");
2275   verifyFormat("DEBUG({\n"
2276                "  switch (x) {\n"
2277                "  case A:\n"
2278                "    f();\n"
2279                "    break;\n"
2280                "    // fallthrough\n"
2281                "  case B:\n"
2282                "    g();\n"
2283                "    break;\n"
2284                "  }\n"
2285                "});");
2286   EXPECT_EQ("DEBUG({\n"
2287             "  switch (x) {\n"
2288             "  case A:\n"
2289             "    f();\n"
2290             "    break;\n"
2291             "  // On B:\n"
2292             "  case B:\n"
2293             "    g();\n"
2294             "    break;\n"
2295             "  }\n"
2296             "});",
2297             format("DEBUG({\n"
2298                    "  switch (x) {\n"
2299                    "  case A:\n"
2300                    "    f();\n"
2301                    "    break;\n"
2302                    "  // On B:\n"
2303                    "  case B:\n"
2304                    "    g();\n"
2305                    "    break;\n"
2306                    "  }\n"
2307                    "});",
2308                    getLLVMStyle()));
2309   EXPECT_EQ("switch (n) {\n"
2310             "case 0: {\n"
2311             "  return false;\n"
2312             "}\n"
2313             "default: {\n"
2314             "  return true;\n"
2315             "}\n"
2316             "}",
2317             format("switch (n)\n"
2318                    "{\n"
2319                    "case 0: {\n"
2320                    "  return false;\n"
2321                    "}\n"
2322                    "default: {\n"
2323                    "  return true;\n"
2324                    "}\n"
2325                    "}",
2326                    getLLVMStyle()));
2327   verifyFormat("switch (a) {\n"
2328                "case (b):\n"
2329                "  return;\n"
2330                "}");
2331 
2332   verifyFormat("switch (a) {\n"
2333                "case some_namespace::\n"
2334                "    some_constant:\n"
2335                "  return;\n"
2336                "}",
2337                getLLVMStyleWithColumns(34));
2338 
2339   FormatStyle Style = getLLVMStyle();
2340   Style.IndentCaseLabels = true;
2341   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
2342   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
2343   Style.BraceWrapping.AfterCaseLabel = true;
2344   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
2345   EXPECT_EQ("switch (n)\n"
2346             "{\n"
2347             "  case 0:\n"
2348             "  {\n"
2349             "    return false;\n"
2350             "  }\n"
2351             "  default:\n"
2352             "  {\n"
2353             "    return true;\n"
2354             "  }\n"
2355             "}",
2356             format("switch (n) {\n"
2357                    "  case 0: {\n"
2358                    "    return false;\n"
2359                    "  }\n"
2360                    "  default: {\n"
2361                    "    return true;\n"
2362                    "  }\n"
2363                    "}",
2364                    Style));
2365   Style.BraceWrapping.AfterCaseLabel = false;
2366   EXPECT_EQ("switch (n)\n"
2367             "{\n"
2368             "  case 0: {\n"
2369             "    return false;\n"
2370             "  }\n"
2371             "  default: {\n"
2372             "    return true;\n"
2373             "  }\n"
2374             "}",
2375             format("switch (n) {\n"
2376                    "  case 0:\n"
2377                    "  {\n"
2378                    "    return false;\n"
2379                    "  }\n"
2380                    "  default:\n"
2381                    "  {\n"
2382                    "    return true;\n"
2383                    "  }\n"
2384                    "}",
2385                    Style));
2386   Style.IndentCaseLabels = false;
2387   Style.IndentCaseBlocks = true;
2388   EXPECT_EQ("switch (n)\n"
2389             "{\n"
2390             "case 0:\n"
2391             "  {\n"
2392             "    return false;\n"
2393             "  }\n"
2394             "case 1:\n"
2395             "  break;\n"
2396             "default:\n"
2397             "  {\n"
2398             "    return true;\n"
2399             "  }\n"
2400             "}",
2401             format("switch (n) {\n"
2402                    "case 0: {\n"
2403                    "  return false;\n"
2404                    "}\n"
2405                    "case 1:\n"
2406                    "  break;\n"
2407                    "default: {\n"
2408                    "  return true;\n"
2409                    "}\n"
2410                    "}",
2411                    Style));
2412   Style.IndentCaseLabels = true;
2413   Style.IndentCaseBlocks = true;
2414   EXPECT_EQ("switch (n)\n"
2415             "{\n"
2416             "  case 0:\n"
2417             "    {\n"
2418             "      return false;\n"
2419             "    }\n"
2420             "  case 1:\n"
2421             "    break;\n"
2422             "  default:\n"
2423             "    {\n"
2424             "      return true;\n"
2425             "    }\n"
2426             "}",
2427             format("switch (n) {\n"
2428                    "case 0: {\n"
2429                    "  return false;\n"
2430                    "}\n"
2431                    "case 1:\n"
2432                    "  break;\n"
2433                    "default: {\n"
2434                    "  return true;\n"
2435                    "}\n"
2436                    "}",
2437                    Style));
2438 }
2439 
2440 TEST_F(FormatTest, CaseRanges) {
2441   verifyFormat("switch (x) {\n"
2442                "case 'A' ... 'Z':\n"
2443                "case 1 ... 5:\n"
2444                "case a ... b:\n"
2445                "  break;\n"
2446                "}");
2447 }
2448 
2449 TEST_F(FormatTest, ShortEnums) {
2450   FormatStyle Style = getLLVMStyle();
2451   Style.AllowShortEnumsOnASingleLine = true;
2452   verifyFormat("enum { A, B, C } ShortEnum1, ShortEnum2;", Style);
2453   Style.AllowShortEnumsOnASingleLine = false;
2454   verifyFormat("enum {\n"
2455                "  A,\n"
2456                "  B,\n"
2457                "  C\n"
2458                "} ShortEnum1, ShortEnum2;",
2459                Style);
2460   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
2461   Style.BraceWrapping.AfterEnum = true;
2462   verifyFormat("enum\n"
2463                "{\n"
2464                "  A,\n"
2465                "  B,\n"
2466                "  C\n"
2467                "} ShortEnum1, ShortEnum2;",
2468                Style);
2469 }
2470 
2471 TEST_F(FormatTest, ShortCaseLabels) {
2472   FormatStyle Style = getLLVMStyle();
2473   Style.AllowShortCaseLabelsOnASingleLine = true;
2474   verifyFormat("switch (a) {\n"
2475                "case 1: x = 1; break;\n"
2476                "case 2: return;\n"
2477                "case 3:\n"
2478                "case 4:\n"
2479                "case 5: return;\n"
2480                "case 6: // comment\n"
2481                "  return;\n"
2482                "case 7:\n"
2483                "  // comment\n"
2484                "  return;\n"
2485                "case 8:\n"
2486                "  x = 8; // comment\n"
2487                "  break;\n"
2488                "default: y = 1; break;\n"
2489                "}",
2490                Style);
2491   verifyFormat("switch (a) {\n"
2492                "case 0: return; // comment\n"
2493                "case 1: break;  // comment\n"
2494                "case 2: return;\n"
2495                "// comment\n"
2496                "case 3: return;\n"
2497                "// comment 1\n"
2498                "// comment 2\n"
2499                "// comment 3\n"
2500                "case 4: break; /* comment */\n"
2501                "case 5:\n"
2502                "  // comment\n"
2503                "  break;\n"
2504                "case 6: /* comment */ x = 1; break;\n"
2505                "case 7: x = /* comment */ 1; break;\n"
2506                "case 8:\n"
2507                "  x = 1; /* comment */\n"
2508                "  break;\n"
2509                "case 9:\n"
2510                "  break; // comment line 1\n"
2511                "         // comment line 2\n"
2512                "}",
2513                Style);
2514   EXPECT_EQ("switch (a) {\n"
2515             "case 1:\n"
2516             "  x = 8;\n"
2517             "  // fall through\n"
2518             "case 2: x = 8;\n"
2519             "// comment\n"
2520             "case 3:\n"
2521             "  return; /* comment line 1\n"
2522             "           * comment line 2 */\n"
2523             "case 4: i = 8;\n"
2524             "// something else\n"
2525             "#if FOO\n"
2526             "case 5: break;\n"
2527             "#endif\n"
2528             "}",
2529             format("switch (a) {\n"
2530                    "case 1: x = 8;\n"
2531                    "  // fall through\n"
2532                    "case 2:\n"
2533                    "  x = 8;\n"
2534                    "// comment\n"
2535                    "case 3:\n"
2536                    "  return; /* comment line 1\n"
2537                    "           * comment line 2 */\n"
2538                    "case 4:\n"
2539                    "  i = 8;\n"
2540                    "// something else\n"
2541                    "#if FOO\n"
2542                    "case 5: break;\n"
2543                    "#endif\n"
2544                    "}",
2545                    Style));
2546   EXPECT_EQ("switch (a) {\n"
2547             "case 0:\n"
2548             "  return; // long long long long long long long long long long "
2549             "long long comment\n"
2550             "          // line\n"
2551             "}",
2552             format("switch (a) {\n"
2553                    "case 0: return; // long long long long long long long long "
2554                    "long long long long comment line\n"
2555                    "}",
2556                    Style));
2557   EXPECT_EQ("switch (a) {\n"
2558             "case 0:\n"
2559             "  return; /* long long long long long long long long long long "
2560             "long long comment\n"
2561             "             line */\n"
2562             "}",
2563             format("switch (a) {\n"
2564                    "case 0: return; /* long long long long long long long long "
2565                    "long long long long comment line */\n"
2566                    "}",
2567                    Style));
2568   verifyFormat("switch (a) {\n"
2569                "#if FOO\n"
2570                "case 0: return 0;\n"
2571                "#endif\n"
2572                "}",
2573                Style);
2574   verifyFormat("switch (a) {\n"
2575                "case 1: {\n"
2576                "}\n"
2577                "case 2: {\n"
2578                "  return;\n"
2579                "}\n"
2580                "case 3: {\n"
2581                "  x = 1;\n"
2582                "  return;\n"
2583                "}\n"
2584                "case 4:\n"
2585                "  if (x)\n"
2586                "    return;\n"
2587                "}",
2588                Style);
2589   Style.ColumnLimit = 21;
2590   verifyFormat("switch (a) {\n"
2591                "case 1: x = 1; break;\n"
2592                "case 2: return;\n"
2593                "case 3:\n"
2594                "case 4:\n"
2595                "case 5: return;\n"
2596                "default:\n"
2597                "  y = 1;\n"
2598                "  break;\n"
2599                "}",
2600                Style);
2601   Style.ColumnLimit = 80;
2602   Style.AllowShortCaseLabelsOnASingleLine = false;
2603   Style.IndentCaseLabels = true;
2604   EXPECT_EQ("switch (n) {\n"
2605             "  default /*comments*/:\n"
2606             "    return true;\n"
2607             "  case 0:\n"
2608             "    return false;\n"
2609             "}",
2610             format("switch (n) {\n"
2611                    "default/*comments*/:\n"
2612                    "  return true;\n"
2613                    "case 0:\n"
2614                    "  return false;\n"
2615                    "}",
2616                    Style));
2617   Style.AllowShortCaseLabelsOnASingleLine = true;
2618   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
2619   Style.BraceWrapping.AfterCaseLabel = true;
2620   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
2621   EXPECT_EQ("switch (n)\n"
2622             "{\n"
2623             "  case 0:\n"
2624             "  {\n"
2625             "    return false;\n"
2626             "  }\n"
2627             "  default:\n"
2628             "  {\n"
2629             "    return true;\n"
2630             "  }\n"
2631             "}",
2632             format("switch (n) {\n"
2633                    "  case 0: {\n"
2634                    "    return false;\n"
2635                    "  }\n"
2636                    "  default:\n"
2637                    "  {\n"
2638                    "    return true;\n"
2639                    "  }\n"
2640                    "}",
2641                    Style));
2642 }
2643 
2644 TEST_F(FormatTest, FormatsLabels) {
2645   verifyFormat("void f() {\n"
2646                "  some_code();\n"
2647                "test_label:\n"
2648                "  some_other_code();\n"
2649                "  {\n"
2650                "    some_more_code();\n"
2651                "  another_label:\n"
2652                "    some_more_code();\n"
2653                "  }\n"
2654                "}");
2655   verifyFormat("{\n"
2656                "  some_code();\n"
2657                "test_label:\n"
2658                "  some_other_code();\n"
2659                "}");
2660   verifyFormat("{\n"
2661                "  some_code();\n"
2662                "test_label:;\n"
2663                "  int i = 0;\n"
2664                "}");
2665   FormatStyle Style = getLLVMStyle();
2666   Style.IndentGotoLabels = false;
2667   verifyFormat("void f() {\n"
2668                "  some_code();\n"
2669                "test_label:\n"
2670                "  some_other_code();\n"
2671                "  {\n"
2672                "    some_more_code();\n"
2673                "another_label:\n"
2674                "    some_more_code();\n"
2675                "  }\n"
2676                "}",
2677                Style);
2678   verifyFormat("{\n"
2679                "  some_code();\n"
2680                "test_label:\n"
2681                "  some_other_code();\n"
2682                "}",
2683                Style);
2684   verifyFormat("{\n"
2685                "  some_code();\n"
2686                "test_label:;\n"
2687                "  int i = 0;\n"
2688                "}");
2689 }
2690 
2691 TEST_F(FormatTest, MultiLineControlStatements) {
2692   FormatStyle Style = getLLVMStyle();
2693   Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom;
2694   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine;
2695   Style.ColumnLimit = 20;
2696   // Short lines should keep opening brace on same line.
2697   EXPECT_EQ("if (foo) {\n"
2698             "  bar();\n"
2699             "}",
2700             format("if(foo){bar();}", Style));
2701   EXPECT_EQ("if (foo) {\n"
2702             "  bar();\n"
2703             "} else {\n"
2704             "  baz();\n"
2705             "}",
2706             format("if(foo){bar();}else{baz();}", Style));
2707   EXPECT_EQ("if (foo && bar) {\n"
2708             "  baz();\n"
2709             "}",
2710             format("if(foo&&bar){baz();}", Style));
2711   EXPECT_EQ("if (foo) {\n"
2712             "  bar();\n"
2713             "} else if (baz) {\n"
2714             "  quux();\n"
2715             "}",
2716             format("if(foo){bar();}else if(baz){quux();}", Style));
2717   EXPECT_EQ(
2718       "if (foo) {\n"
2719       "  bar();\n"
2720       "} else if (baz) {\n"
2721       "  quux();\n"
2722       "} else {\n"
2723       "  foobar();\n"
2724       "}",
2725       format("if(foo){bar();}else if(baz){quux();}else{foobar();}", Style));
2726   EXPECT_EQ("for (;;) {\n"
2727             "  foo();\n"
2728             "}",
2729             format("for(;;){foo();}"));
2730   EXPECT_EQ("while (1) {\n"
2731             "  foo();\n"
2732             "}",
2733             format("while(1){foo();}", Style));
2734   EXPECT_EQ("switch (foo) {\n"
2735             "case bar:\n"
2736             "  return;\n"
2737             "}",
2738             format("switch(foo){case bar:return;}", Style));
2739   EXPECT_EQ("try {\n"
2740             "  foo();\n"
2741             "} catch (...) {\n"
2742             "  bar();\n"
2743             "}",
2744             format("try{foo();}catch(...){bar();}", Style));
2745   EXPECT_EQ("do {\n"
2746             "  foo();\n"
2747             "} while (bar &&\n"
2748             "         baz);",
2749             format("do{foo();}while(bar&&baz);", Style));
2750   // Long lines should put opening brace on new line.
2751   EXPECT_EQ("if (foo && bar &&\n"
2752             "    baz)\n"
2753             "{\n"
2754             "  quux();\n"
2755             "}",
2756             format("if(foo&&bar&&baz){quux();}", Style));
2757   EXPECT_EQ("if (foo && bar &&\n"
2758             "    baz)\n"
2759             "{\n"
2760             "  quux();\n"
2761             "}",
2762             format("if (foo && bar &&\n"
2763                    "    baz) {\n"
2764                    "  quux();\n"
2765                    "}",
2766                    Style));
2767   EXPECT_EQ("if (foo) {\n"
2768             "  bar();\n"
2769             "} else if (baz ||\n"
2770             "           quux)\n"
2771             "{\n"
2772             "  foobar();\n"
2773             "}",
2774             format("if(foo){bar();}else if(baz||quux){foobar();}", Style));
2775   EXPECT_EQ(
2776       "if (foo) {\n"
2777       "  bar();\n"
2778       "} else if (baz ||\n"
2779       "           quux)\n"
2780       "{\n"
2781       "  foobar();\n"
2782       "} else {\n"
2783       "  barbaz();\n"
2784       "}",
2785       format("if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}",
2786              Style));
2787   EXPECT_EQ("for (int i = 0;\n"
2788             "     i < 10; ++i)\n"
2789             "{\n"
2790             "  foo();\n"
2791             "}",
2792             format("for(int i=0;i<10;++i){foo();}", Style));
2793   EXPECT_EQ("foreach (int i,\n"
2794             "         list)\n"
2795             "{\n"
2796             "  foo();\n"
2797             "}",
2798             format("foreach(int i, list){foo();}", Style));
2799   Style.ColumnLimit =
2800       40; // to concentrate at brace wrapping, not line wrap due to column limit
2801   EXPECT_EQ("foreach (int i, list) {\n"
2802             "  foo();\n"
2803             "}",
2804             format("foreach(int i, list){foo();}", Style));
2805   Style.ColumnLimit =
2806       20; // to concentrate at brace wrapping, not line wrap due to column limit
2807   EXPECT_EQ("while (foo || bar ||\n"
2808             "       baz)\n"
2809             "{\n"
2810             "  quux();\n"
2811             "}",
2812             format("while(foo||bar||baz){quux();}", Style));
2813   EXPECT_EQ("switch (\n"
2814             "    foo = barbaz)\n"
2815             "{\n"
2816             "case quux:\n"
2817             "  return;\n"
2818             "}",
2819             format("switch(foo=barbaz){case quux:return;}", Style));
2820   EXPECT_EQ("try {\n"
2821             "  foo();\n"
2822             "} catch (\n"
2823             "    Exception &bar)\n"
2824             "{\n"
2825             "  baz();\n"
2826             "}",
2827             format("try{foo();}catch(Exception&bar){baz();}", Style));
2828   Style.ColumnLimit =
2829       40; // to concentrate at brace wrapping, not line wrap due to column limit
2830   EXPECT_EQ("try {\n"
2831             "  foo();\n"
2832             "} catch (Exception &bar) {\n"
2833             "  baz();\n"
2834             "}",
2835             format("try{foo();}catch(Exception&bar){baz();}", Style));
2836   Style.ColumnLimit =
2837       20; // to concentrate at brace wrapping, not line wrap due to column limit
2838 
2839   Style.BraceWrapping.BeforeElse = true;
2840   EXPECT_EQ(
2841       "if (foo) {\n"
2842       "  bar();\n"
2843       "}\n"
2844       "else if (baz ||\n"
2845       "         quux)\n"
2846       "{\n"
2847       "  foobar();\n"
2848       "}\n"
2849       "else {\n"
2850       "  barbaz();\n"
2851       "}",
2852       format("if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}",
2853              Style));
2854 
2855   Style.BraceWrapping.BeforeCatch = true;
2856   EXPECT_EQ("try {\n"
2857             "  foo();\n"
2858             "}\n"
2859             "catch (...) {\n"
2860             "  baz();\n"
2861             "}",
2862             format("try{foo();}catch(...){baz();}", Style));
2863 
2864   Style.BraceWrapping.AfterFunction = true;
2865   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine;
2866   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
2867   Style.ColumnLimit = 80;
2868   verifyFormat("void shortfunction() { bar(); }", Style);
2869 
2870   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
2871   verifyFormat("void shortfunction()\n"
2872                "{\n"
2873                "  bar();\n"
2874                "}",
2875                Style);
2876 }
2877 
2878 TEST_F(FormatTest, BeforeWhile) {
2879   FormatStyle Style = getLLVMStyle();
2880   Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom;
2881 
2882   verifyFormat("do {\n"
2883                "  foo();\n"
2884                "} while (1);",
2885                Style);
2886   Style.BraceWrapping.BeforeWhile = true;
2887   verifyFormat("do {\n"
2888                "  foo();\n"
2889                "}\n"
2890                "while (1);",
2891                Style);
2892 }
2893 
2894 //===----------------------------------------------------------------------===//
2895 // Tests for classes, namespaces, etc.
2896 //===----------------------------------------------------------------------===//
2897 
2898 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
2899   verifyFormat("class A {};");
2900 }
2901 
2902 TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
2903   verifyFormat("class A {\n"
2904                "public:\n"
2905                "public: // comment\n"
2906                "protected:\n"
2907                "private:\n"
2908                "  void f() {}\n"
2909                "};");
2910   verifyFormat("export class A {\n"
2911                "public:\n"
2912                "public: // comment\n"
2913                "protected:\n"
2914                "private:\n"
2915                "  void f() {}\n"
2916                "};");
2917   verifyGoogleFormat("class A {\n"
2918                      " public:\n"
2919                      " protected:\n"
2920                      " private:\n"
2921                      "  void f() {}\n"
2922                      "};");
2923   verifyGoogleFormat("export class A {\n"
2924                      " public:\n"
2925                      " protected:\n"
2926                      " private:\n"
2927                      "  void f() {}\n"
2928                      "};");
2929   verifyFormat("class A {\n"
2930                "public slots:\n"
2931                "  void f1() {}\n"
2932                "public Q_SLOTS:\n"
2933                "  void f2() {}\n"
2934                "protected slots:\n"
2935                "  void f3() {}\n"
2936                "protected Q_SLOTS:\n"
2937                "  void f4() {}\n"
2938                "private slots:\n"
2939                "  void f5() {}\n"
2940                "private Q_SLOTS:\n"
2941                "  void f6() {}\n"
2942                "signals:\n"
2943                "  void g1();\n"
2944                "Q_SIGNALS:\n"
2945                "  void g2();\n"
2946                "};");
2947 
2948   // Don't interpret 'signals' the wrong way.
2949   verifyFormat("signals.set();");
2950   verifyFormat("for (Signals signals : f()) {\n}");
2951   verifyFormat("{\n"
2952                "  signals.set(); // This needs indentation.\n"
2953                "}");
2954   verifyFormat("void f() {\n"
2955                "label:\n"
2956                "  signals.baz();\n"
2957                "}");
2958 }
2959 
2960 TEST_F(FormatTest, SeparatesLogicalBlocks) {
2961   EXPECT_EQ("class A {\n"
2962             "public:\n"
2963             "  void f();\n"
2964             "\n"
2965             "private:\n"
2966             "  void g() {}\n"
2967             "  // test\n"
2968             "protected:\n"
2969             "  int h;\n"
2970             "};",
2971             format("class A {\n"
2972                    "public:\n"
2973                    "void f();\n"
2974                    "private:\n"
2975                    "void g() {}\n"
2976                    "// test\n"
2977                    "protected:\n"
2978                    "int h;\n"
2979                    "};"));
2980   EXPECT_EQ("class A {\n"
2981             "protected:\n"
2982             "public:\n"
2983             "  void f();\n"
2984             "};",
2985             format("class A {\n"
2986                    "protected:\n"
2987                    "\n"
2988                    "public:\n"
2989                    "\n"
2990                    "  void f();\n"
2991                    "};"));
2992 
2993   // Even ensure proper spacing inside macros.
2994   EXPECT_EQ("#define B     \\\n"
2995             "  class A {   \\\n"
2996             "   protected: \\\n"
2997             "   public:    \\\n"
2998             "    void f(); \\\n"
2999             "  };",
3000             format("#define B     \\\n"
3001                    "  class A {   \\\n"
3002                    "   protected: \\\n"
3003                    "              \\\n"
3004                    "   public:    \\\n"
3005                    "              \\\n"
3006                    "    void f(); \\\n"
3007                    "  };",
3008                    getGoogleStyle()));
3009   // But don't remove empty lines after macros ending in access specifiers.
3010   EXPECT_EQ("#define A private:\n"
3011             "\n"
3012             "int i;",
3013             format("#define A         private:\n"
3014                    "\n"
3015                    "int              i;"));
3016 }
3017 
3018 TEST_F(FormatTest, FormatsClasses) {
3019   verifyFormat("class A : public B {};");
3020   verifyFormat("class A : public ::B {};");
3021 
3022   verifyFormat(
3023       "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3024       "                             public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
3025   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
3026                "    : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3027                "      public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
3028   verifyFormat(
3029       "class A : public B, public C, public D, public E, public F {};");
3030   verifyFormat("class AAAAAAAAAAAA : public B,\n"
3031                "                     public C,\n"
3032                "                     public D,\n"
3033                "                     public E,\n"
3034                "                     public F,\n"
3035                "                     public G {};");
3036 
3037   verifyFormat("class\n"
3038                "    ReallyReallyLongClassName {\n"
3039                "  int i;\n"
3040                "};",
3041                getLLVMStyleWithColumns(32));
3042   verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
3043                "                           aaaaaaaaaaaaaaaa> {};");
3044   verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n"
3045                "    : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n"
3046                "                                 aaaaaaaaaaaaaaaaaaaaaa> {};");
3047   verifyFormat("template <class R, class C>\n"
3048                "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n"
3049                "    : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};");
3050   verifyFormat("class ::A::B {};");
3051 }
3052 
3053 TEST_F(FormatTest, BreakInheritanceStyle) {
3054   FormatStyle StyleWithInheritanceBreakBeforeComma = getLLVMStyle();
3055   StyleWithInheritanceBreakBeforeComma.BreakInheritanceList =
3056       FormatStyle::BILS_BeforeComma;
3057   verifyFormat("class MyClass : public X {};",
3058                StyleWithInheritanceBreakBeforeComma);
3059   verifyFormat("class MyClass\n"
3060                "    : public X\n"
3061                "    , public Y {};",
3062                StyleWithInheritanceBreakBeforeComma);
3063   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA\n"
3064                "    : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n"
3065                "    , public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};",
3066                StyleWithInheritanceBreakBeforeComma);
3067   verifyFormat("struct aaaaaaaaaaaaa\n"
3068                "    : public aaaaaaaaaaaaaaaaaaa< // break\n"
3069                "          aaaaaaaaaaaaaaaa> {};",
3070                StyleWithInheritanceBreakBeforeComma);
3071 
3072   FormatStyle StyleWithInheritanceBreakAfterColon = getLLVMStyle();
3073   StyleWithInheritanceBreakAfterColon.BreakInheritanceList =
3074       FormatStyle::BILS_AfterColon;
3075   verifyFormat("class MyClass : public X {};",
3076                StyleWithInheritanceBreakAfterColon);
3077   verifyFormat("class MyClass : public X, public Y {};",
3078                StyleWithInheritanceBreakAfterColon);
3079   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA :\n"
3080                "    public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3081                "    public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};",
3082                StyleWithInheritanceBreakAfterColon);
3083   verifyFormat("struct aaaaaaaaaaaaa :\n"
3084                "    public aaaaaaaaaaaaaaaaaaa< // break\n"
3085                "        aaaaaaaaaaaaaaaa> {};",
3086                StyleWithInheritanceBreakAfterColon);
3087 
3088   FormatStyle StyleWithInheritanceBreakAfterComma = getLLVMStyle();
3089   StyleWithInheritanceBreakAfterComma.BreakInheritanceList =
3090       FormatStyle::BILS_AfterComma;
3091   verifyFormat("class MyClass : public X {};",
3092                StyleWithInheritanceBreakAfterComma);
3093   verifyFormat("class MyClass : public X,\n"
3094                "                public Y {};",
3095                StyleWithInheritanceBreakAfterComma);
3096   verifyFormat(
3097       "class AAAAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3098       "                               public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC "
3099       "{};",
3100       StyleWithInheritanceBreakAfterComma);
3101   verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
3102                "                           aaaaaaaaaaaaaaaa> {};",
3103                StyleWithInheritanceBreakAfterComma);
3104   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
3105                "    : public OnceBreak,\n"
3106                "      public AlwaysBreak,\n"
3107                "      EvenBasesFitInOneLine {};",
3108                StyleWithInheritanceBreakAfterComma);
3109 }
3110 
3111 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
3112   verifyFormat("class A {\n} a, b;");
3113   verifyFormat("struct A {\n} a, b;");
3114   verifyFormat("union A {\n} a;");
3115 }
3116 
3117 TEST_F(FormatTest, FormatsEnum) {
3118   verifyFormat("enum {\n"
3119                "  Zero,\n"
3120                "  One = 1,\n"
3121                "  Two = One + 1,\n"
3122                "  Three = (One + Two),\n"
3123                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
3124                "  Five = (One, Two, Three, Four, 5)\n"
3125                "};");
3126   verifyGoogleFormat("enum {\n"
3127                      "  Zero,\n"
3128                      "  One = 1,\n"
3129                      "  Two = One + 1,\n"
3130                      "  Three = (One + Two),\n"
3131                      "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
3132                      "  Five = (One, Two, Three, Four, 5)\n"
3133                      "};");
3134   verifyFormat("enum Enum {};");
3135   verifyFormat("enum {};");
3136   verifyFormat("enum X E {} d;");
3137   verifyFormat("enum __attribute__((...)) E {} d;");
3138   verifyFormat("enum __declspec__((...)) E {} d;");
3139   verifyFormat("enum {\n"
3140                "  Bar = Foo<int, int>::value\n"
3141                "};",
3142                getLLVMStyleWithColumns(30));
3143 
3144   verifyFormat("enum ShortEnum { A, B, C };");
3145   verifyGoogleFormat("enum ShortEnum { A, B, C };");
3146 
3147   EXPECT_EQ("enum KeepEmptyLines {\n"
3148             "  ONE,\n"
3149             "\n"
3150             "  TWO,\n"
3151             "\n"
3152             "  THREE\n"
3153             "}",
3154             format("enum KeepEmptyLines {\n"
3155                    "  ONE,\n"
3156                    "\n"
3157                    "  TWO,\n"
3158                    "\n"
3159                    "\n"
3160                    "  THREE\n"
3161                    "}"));
3162   verifyFormat("enum E { // comment\n"
3163                "  ONE,\n"
3164                "  TWO\n"
3165                "};\n"
3166                "int i;");
3167 
3168   FormatStyle EightIndent = getLLVMStyle();
3169   EightIndent.IndentWidth = 8;
3170   verifyFormat("enum {\n"
3171                "        VOID,\n"
3172                "        CHAR,\n"
3173                "        SHORT,\n"
3174                "        INT,\n"
3175                "        LONG,\n"
3176                "        SIGNED,\n"
3177                "        UNSIGNED,\n"
3178                "        BOOL,\n"
3179                "        FLOAT,\n"
3180                "        DOUBLE,\n"
3181                "        COMPLEX\n"
3182                "};",
3183                EightIndent);
3184 
3185   // Not enums.
3186   verifyFormat("enum X f() {\n"
3187                "  a();\n"
3188                "  return 42;\n"
3189                "}");
3190   verifyFormat("enum X Type::f() {\n"
3191                "  a();\n"
3192                "  return 42;\n"
3193                "}");
3194   verifyFormat("enum ::X f() {\n"
3195                "  a();\n"
3196                "  return 42;\n"
3197                "}");
3198   verifyFormat("enum ns::X f() {\n"
3199                "  a();\n"
3200                "  return 42;\n"
3201                "}");
3202 }
3203 
3204 TEST_F(FormatTest, FormatsEnumsWithErrors) {
3205   verifyFormat("enum Type {\n"
3206                "  One = 0; // These semicolons should be commas.\n"
3207                "  Two = 1;\n"
3208                "};");
3209   verifyFormat("namespace n {\n"
3210                "enum Type {\n"
3211                "  One,\n"
3212                "  Two, // missing };\n"
3213                "  int i;\n"
3214                "}\n"
3215                "void g() {}");
3216 }
3217 
3218 TEST_F(FormatTest, FormatsEnumStruct) {
3219   verifyFormat("enum struct {\n"
3220                "  Zero,\n"
3221                "  One = 1,\n"
3222                "  Two = One + 1,\n"
3223                "  Three = (One + Two),\n"
3224                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
3225                "  Five = (One, Two, Three, Four, 5)\n"
3226                "};");
3227   verifyFormat("enum struct Enum {};");
3228   verifyFormat("enum struct {};");
3229   verifyFormat("enum struct X E {} d;");
3230   verifyFormat("enum struct __attribute__((...)) E {} d;");
3231   verifyFormat("enum struct __declspec__((...)) E {} d;");
3232   verifyFormat("enum struct X f() {\n  a();\n  return 42;\n}");
3233 }
3234 
3235 TEST_F(FormatTest, FormatsEnumClass) {
3236   verifyFormat("enum class {\n"
3237                "  Zero,\n"
3238                "  One = 1,\n"
3239                "  Two = One + 1,\n"
3240                "  Three = (One + Two),\n"
3241                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
3242                "  Five = (One, Two, Three, Four, 5)\n"
3243                "};");
3244   verifyFormat("enum class Enum {};");
3245   verifyFormat("enum class {};");
3246   verifyFormat("enum class X E {} d;");
3247   verifyFormat("enum class __attribute__((...)) E {} d;");
3248   verifyFormat("enum class __declspec__((...)) E {} d;");
3249   verifyFormat("enum class X f() {\n  a();\n  return 42;\n}");
3250 }
3251 
3252 TEST_F(FormatTest, FormatsEnumTypes) {
3253   verifyFormat("enum X : int {\n"
3254                "  A, // Force multiple lines.\n"
3255                "  B\n"
3256                "};");
3257   verifyFormat("enum X : int { A, B };");
3258   verifyFormat("enum X : std::uint32_t { A, B };");
3259 }
3260 
3261 TEST_F(FormatTest, FormatsTypedefEnum) {
3262   FormatStyle Style = getLLVMStyle();
3263   Style.ColumnLimit = 40;
3264   verifyFormat("typedef enum {} EmptyEnum;");
3265   verifyFormat("typedef enum { A, B, C } ShortEnum;");
3266   verifyFormat("typedef enum {\n"
3267                "  ZERO = 0,\n"
3268                "  ONE = 1,\n"
3269                "  TWO = 2,\n"
3270                "  THREE = 3\n"
3271                "} LongEnum;",
3272                Style);
3273   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
3274   Style.BraceWrapping.AfterEnum = true;
3275   verifyFormat("typedef enum {} EmptyEnum;");
3276   verifyFormat("typedef enum { A, B, C } ShortEnum;");
3277   verifyFormat("typedef enum\n"
3278                "{\n"
3279                "  ZERO = 0,\n"
3280                "  ONE = 1,\n"
3281                "  TWO = 2,\n"
3282                "  THREE = 3\n"
3283                "} LongEnum;",
3284                Style);
3285 }
3286 
3287 TEST_F(FormatTest, FormatsNSEnums) {
3288   verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }");
3289   verifyGoogleFormat(
3290       "typedef NS_CLOSED_ENUM(NSInteger, SomeName) { AAA, BBB }");
3291   verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n"
3292                      "  // Information about someDecentlyLongValue.\n"
3293                      "  someDecentlyLongValue,\n"
3294                      "  // Information about anotherDecentlyLongValue.\n"
3295                      "  anotherDecentlyLongValue,\n"
3296                      "  // Information about aThirdDecentlyLongValue.\n"
3297                      "  aThirdDecentlyLongValue\n"
3298                      "};");
3299   verifyGoogleFormat("typedef NS_CLOSED_ENUM(NSInteger, MyType) {\n"
3300                      "  // Information about someDecentlyLongValue.\n"
3301                      "  someDecentlyLongValue,\n"
3302                      "  // Information about anotherDecentlyLongValue.\n"
3303                      "  anotherDecentlyLongValue,\n"
3304                      "  // Information about aThirdDecentlyLongValue.\n"
3305                      "  aThirdDecentlyLongValue\n"
3306                      "};");
3307   verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n"
3308                      "  a = 1,\n"
3309                      "  b = 2,\n"
3310                      "  c = 3,\n"
3311                      "};");
3312   verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n"
3313                      "  a = 1,\n"
3314                      "  b = 2,\n"
3315                      "  c = 3,\n"
3316                      "};");
3317   verifyGoogleFormat("typedef CF_CLOSED_ENUM(NSInteger, MyType) {\n"
3318                      "  a = 1,\n"
3319                      "  b = 2,\n"
3320                      "  c = 3,\n"
3321                      "};");
3322   verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n"
3323                      "  a = 1,\n"
3324                      "  b = 2,\n"
3325                      "  c = 3,\n"
3326                      "};");
3327 }
3328 
3329 TEST_F(FormatTest, FormatsBitfields) {
3330   verifyFormat("struct Bitfields {\n"
3331                "  unsigned sClass : 8;\n"
3332                "  unsigned ValueKind : 2;\n"
3333                "};");
3334   verifyFormat("struct A {\n"
3335                "  int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n"
3336                "      bbbbbbbbbbbbbbbbbbbbbbbbb;\n"
3337                "};");
3338   verifyFormat("struct MyStruct {\n"
3339                "  uchar data;\n"
3340                "  uchar : 8;\n"
3341                "  uchar : 8;\n"
3342                "  uchar other;\n"
3343                "};");
3344   FormatStyle Style = getLLVMStyle();
3345   Style.BitFieldColonSpacing = FormatStyle::BFCS_None;
3346   verifyFormat("struct Bitfields {\n"
3347                "  unsigned sClass:8;\n"
3348                "  unsigned ValueKind:2;\n"
3349                "  uchar other;\n"
3350                "};",
3351                Style);
3352   verifyFormat("struct A {\n"
3353                "  int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:1,\n"
3354                "      bbbbbbbbbbbbbbbbbbbbbbbbb:2;\n"
3355                "};",
3356                Style);
3357   Style.BitFieldColonSpacing = FormatStyle::BFCS_Before;
3358   verifyFormat("struct Bitfields {\n"
3359                "  unsigned sClass :8;\n"
3360                "  unsigned ValueKind :2;\n"
3361                "  uchar other;\n"
3362                "};",
3363                Style);
3364   Style.BitFieldColonSpacing = FormatStyle::BFCS_After;
3365   verifyFormat("struct Bitfields {\n"
3366                "  unsigned sClass: 8;\n"
3367                "  unsigned ValueKind: 2;\n"
3368                "  uchar other;\n"
3369                "};",
3370                Style);
3371 }
3372 
3373 TEST_F(FormatTest, FormatsNamespaces) {
3374   FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle();
3375   LLVMWithNoNamespaceFix.FixNamespaceComments = false;
3376 
3377   verifyFormat("namespace some_namespace {\n"
3378                "class A {};\n"
3379                "void f() { f(); }\n"
3380                "}",
3381                LLVMWithNoNamespaceFix);
3382   verifyFormat("namespace N::inline D {\n"
3383                "class A {};\n"
3384                "void f() { f(); }\n"
3385                "}",
3386                LLVMWithNoNamespaceFix);
3387   verifyFormat("namespace N::inline D::E {\n"
3388                "class A {};\n"
3389                "void f() { f(); }\n"
3390                "}",
3391                LLVMWithNoNamespaceFix);
3392   verifyFormat("namespace [[deprecated(\"foo[bar\")]] some_namespace {\n"
3393                "class A {};\n"
3394                "void f() { f(); }\n"
3395                "}",
3396                LLVMWithNoNamespaceFix);
3397   verifyFormat("/* something */ namespace some_namespace {\n"
3398                "class A {};\n"
3399                "void f() { f(); }\n"
3400                "}",
3401                LLVMWithNoNamespaceFix);
3402   verifyFormat("namespace {\n"
3403                "class A {};\n"
3404                "void f() { f(); }\n"
3405                "}",
3406                LLVMWithNoNamespaceFix);
3407   verifyFormat("/* something */ namespace {\n"
3408                "class A {};\n"
3409                "void f() { f(); }\n"
3410                "}",
3411                LLVMWithNoNamespaceFix);
3412   verifyFormat("inline namespace X {\n"
3413                "class A {};\n"
3414                "void f() { f(); }\n"
3415                "}",
3416                LLVMWithNoNamespaceFix);
3417   verifyFormat("/* something */ inline namespace X {\n"
3418                "class A {};\n"
3419                "void f() { f(); }\n"
3420                "}",
3421                LLVMWithNoNamespaceFix);
3422   verifyFormat("export namespace X {\n"
3423                "class A {};\n"
3424                "void f() { f(); }\n"
3425                "}",
3426                LLVMWithNoNamespaceFix);
3427   verifyFormat("using namespace some_namespace;\n"
3428                "class A {};\n"
3429                "void f() { f(); }",
3430                LLVMWithNoNamespaceFix);
3431 
3432   // This code is more common than we thought; if we
3433   // layout this correctly the semicolon will go into
3434   // its own line, which is undesirable.
3435   verifyFormat("namespace {};", LLVMWithNoNamespaceFix);
3436   verifyFormat("namespace {\n"
3437                "class A {};\n"
3438                "};",
3439                LLVMWithNoNamespaceFix);
3440 
3441   verifyFormat("namespace {\n"
3442                "int SomeVariable = 0; // comment\n"
3443                "} // namespace",
3444                LLVMWithNoNamespaceFix);
3445   EXPECT_EQ("#ifndef HEADER_GUARD\n"
3446             "#define HEADER_GUARD\n"
3447             "namespace my_namespace {\n"
3448             "int i;\n"
3449             "} // my_namespace\n"
3450             "#endif // HEADER_GUARD",
3451             format("#ifndef HEADER_GUARD\n"
3452                    " #define HEADER_GUARD\n"
3453                    "   namespace my_namespace {\n"
3454                    "int i;\n"
3455                    "}    // my_namespace\n"
3456                    "#endif    // HEADER_GUARD",
3457                    LLVMWithNoNamespaceFix));
3458 
3459   EXPECT_EQ("namespace A::B {\n"
3460             "class C {};\n"
3461             "}",
3462             format("namespace A::B {\n"
3463                    "class C {};\n"
3464                    "}",
3465                    LLVMWithNoNamespaceFix));
3466 
3467   FormatStyle Style = getLLVMStyle();
3468   Style.NamespaceIndentation = FormatStyle::NI_All;
3469   EXPECT_EQ("namespace out {\n"
3470             "  int i;\n"
3471             "  namespace in {\n"
3472             "    int i;\n"
3473             "  } // namespace in\n"
3474             "} // namespace out",
3475             format("namespace out {\n"
3476                    "int i;\n"
3477                    "namespace in {\n"
3478                    "int i;\n"
3479                    "} // namespace in\n"
3480                    "} // namespace out",
3481                    Style));
3482 
3483   Style.NamespaceIndentation = FormatStyle::NI_Inner;
3484   EXPECT_EQ("namespace out {\n"
3485             "int i;\n"
3486             "namespace in {\n"
3487             "  int i;\n"
3488             "} // namespace in\n"
3489             "} // namespace out",
3490             format("namespace out {\n"
3491                    "int i;\n"
3492                    "namespace in {\n"
3493                    "int i;\n"
3494                    "} // namespace in\n"
3495                    "} // namespace out",
3496                    Style));
3497 }
3498 
3499 TEST_F(FormatTest, NamespaceMacros) {
3500   FormatStyle Style = getLLVMStyle();
3501   Style.NamespaceMacros.push_back("TESTSUITE");
3502 
3503   verifyFormat("TESTSUITE(A) {\n"
3504                "int foo();\n"
3505                "} // TESTSUITE(A)",
3506                Style);
3507 
3508   verifyFormat("TESTSUITE(A, B) {\n"
3509                "int foo();\n"
3510                "} // TESTSUITE(A)",
3511                Style);
3512 
3513   // Properly indent according to NamespaceIndentation style
3514   Style.NamespaceIndentation = FormatStyle::NI_All;
3515   verifyFormat("TESTSUITE(A) {\n"
3516                "  int foo();\n"
3517                "} // TESTSUITE(A)",
3518                Style);
3519   verifyFormat("TESTSUITE(A) {\n"
3520                "  namespace B {\n"
3521                "    int foo();\n"
3522                "  } // namespace B\n"
3523                "} // TESTSUITE(A)",
3524                Style);
3525   verifyFormat("namespace A {\n"
3526                "  TESTSUITE(B) {\n"
3527                "    int foo();\n"
3528                "  } // TESTSUITE(B)\n"
3529                "} // namespace A",
3530                Style);
3531 
3532   Style.NamespaceIndentation = FormatStyle::NI_Inner;
3533   verifyFormat("TESTSUITE(A) {\n"
3534                "TESTSUITE(B) {\n"
3535                "  int foo();\n"
3536                "} // TESTSUITE(B)\n"
3537                "} // TESTSUITE(A)",
3538                Style);
3539   verifyFormat("TESTSUITE(A) {\n"
3540                "namespace B {\n"
3541                "  int foo();\n"
3542                "} // namespace B\n"
3543                "} // TESTSUITE(A)",
3544                Style);
3545   verifyFormat("namespace A {\n"
3546                "TESTSUITE(B) {\n"
3547                "  int foo();\n"
3548                "} // TESTSUITE(B)\n"
3549                "} // namespace A",
3550                Style);
3551 
3552   // Properly merge namespace-macros blocks in CompactNamespaces mode
3553   Style.NamespaceIndentation = FormatStyle::NI_None;
3554   Style.CompactNamespaces = true;
3555   verifyFormat("TESTSUITE(A) { TESTSUITE(B) {\n"
3556                "}} // TESTSUITE(A::B)",
3557                Style);
3558 
3559   EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n"
3560             "}} // TESTSUITE(out::in)",
3561             format("TESTSUITE(out) {\n"
3562                    "TESTSUITE(in) {\n"
3563                    "} // TESTSUITE(in)\n"
3564                    "} // TESTSUITE(out)",
3565                    Style));
3566 
3567   EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n"
3568             "}} // TESTSUITE(out::in)",
3569             format("TESTSUITE(out) {\n"
3570                    "TESTSUITE(in) {\n"
3571                    "} // TESTSUITE(in)\n"
3572                    "} // TESTSUITE(out)",
3573                    Style));
3574 
3575   // Do not merge different namespaces/macros
3576   EXPECT_EQ("namespace out {\n"
3577             "TESTSUITE(in) {\n"
3578             "} // TESTSUITE(in)\n"
3579             "} // namespace out",
3580             format("namespace out {\n"
3581                    "TESTSUITE(in) {\n"
3582                    "} // TESTSUITE(in)\n"
3583                    "} // namespace out",
3584                    Style));
3585   EXPECT_EQ("TESTSUITE(out) {\n"
3586             "namespace in {\n"
3587             "} // namespace in\n"
3588             "} // TESTSUITE(out)",
3589             format("TESTSUITE(out) {\n"
3590                    "namespace in {\n"
3591                    "} // namespace in\n"
3592                    "} // TESTSUITE(out)",
3593                    Style));
3594   Style.NamespaceMacros.push_back("FOOBAR");
3595   EXPECT_EQ("TESTSUITE(out) {\n"
3596             "FOOBAR(in) {\n"
3597             "} // FOOBAR(in)\n"
3598             "} // TESTSUITE(out)",
3599             format("TESTSUITE(out) {\n"
3600                    "FOOBAR(in) {\n"
3601                    "} // FOOBAR(in)\n"
3602                    "} // TESTSUITE(out)",
3603                    Style));
3604 }
3605 
3606 TEST_F(FormatTest, FormatsCompactNamespaces) {
3607   FormatStyle Style = getLLVMStyle();
3608   Style.CompactNamespaces = true;
3609   Style.NamespaceMacros.push_back("TESTSUITE");
3610 
3611   verifyFormat("namespace A { namespace B {\n"
3612                "}} // namespace A::B",
3613                Style);
3614 
3615   EXPECT_EQ("namespace out { namespace in {\n"
3616             "}} // namespace out::in",
3617             format("namespace out {\n"
3618                    "namespace in {\n"
3619                    "} // namespace in\n"
3620                    "} // namespace out",
3621                    Style));
3622 
3623   // Only namespaces which have both consecutive opening and end get compacted
3624   EXPECT_EQ("namespace out {\n"
3625             "namespace in1 {\n"
3626             "} // namespace in1\n"
3627             "namespace in2 {\n"
3628             "} // namespace in2\n"
3629             "} // namespace out",
3630             format("namespace out {\n"
3631                    "namespace in1 {\n"
3632                    "} // namespace in1\n"
3633                    "namespace in2 {\n"
3634                    "} // namespace in2\n"
3635                    "} // namespace out",
3636                    Style));
3637 
3638   EXPECT_EQ("namespace out {\n"
3639             "int i;\n"
3640             "namespace in {\n"
3641             "int j;\n"
3642             "} // namespace in\n"
3643             "int k;\n"
3644             "} // namespace out",
3645             format("namespace out { int i;\n"
3646                    "namespace in { int j; } // namespace in\n"
3647                    "int k; } // namespace out",
3648                    Style));
3649 
3650   EXPECT_EQ("namespace A { namespace B { namespace C {\n"
3651             "}}} // namespace A::B::C\n",
3652             format("namespace A { namespace B {\n"
3653                    "namespace C {\n"
3654                    "}} // namespace B::C\n"
3655                    "} // namespace A\n",
3656                    Style));
3657 
3658   Style.ColumnLimit = 40;
3659   EXPECT_EQ("namespace aaaaaaaaaa {\n"
3660             "namespace bbbbbbbbbb {\n"
3661             "}} // namespace aaaaaaaaaa::bbbbbbbbbb",
3662             format("namespace aaaaaaaaaa {\n"
3663                    "namespace bbbbbbbbbb {\n"
3664                    "} // namespace bbbbbbbbbb\n"
3665                    "} // namespace aaaaaaaaaa",
3666                    Style));
3667 
3668   EXPECT_EQ("namespace aaaaaa { namespace bbbbbb {\n"
3669             "namespace cccccc {\n"
3670             "}}} // namespace aaaaaa::bbbbbb::cccccc",
3671             format("namespace aaaaaa {\n"
3672                    "namespace bbbbbb {\n"
3673                    "namespace cccccc {\n"
3674                    "} // namespace cccccc\n"
3675                    "} // namespace bbbbbb\n"
3676                    "} // namespace aaaaaa",
3677                    Style));
3678   Style.ColumnLimit = 80;
3679 
3680   // Extra semicolon after 'inner' closing brace prevents merging
3681   EXPECT_EQ("namespace out { namespace in {\n"
3682             "}; } // namespace out::in",
3683             format("namespace out {\n"
3684                    "namespace in {\n"
3685                    "}; // namespace in\n"
3686                    "} // namespace out",
3687                    Style));
3688 
3689   // Extra semicolon after 'outer' closing brace is conserved
3690   EXPECT_EQ("namespace out { namespace in {\n"
3691             "}}; // namespace out::in",
3692             format("namespace out {\n"
3693                    "namespace in {\n"
3694                    "} // namespace in\n"
3695                    "}; // namespace out",
3696                    Style));
3697 
3698   Style.NamespaceIndentation = FormatStyle::NI_All;
3699   EXPECT_EQ("namespace out { namespace in {\n"
3700             "  int i;\n"
3701             "}} // namespace out::in",
3702             format("namespace out {\n"
3703                    "namespace in {\n"
3704                    "int i;\n"
3705                    "} // namespace in\n"
3706                    "} // namespace out",
3707                    Style));
3708   EXPECT_EQ("namespace out { namespace mid {\n"
3709             "  namespace in {\n"
3710             "    int j;\n"
3711             "  } // namespace in\n"
3712             "  int k;\n"
3713             "}} // namespace out::mid",
3714             format("namespace out { namespace mid {\n"
3715                    "namespace in { int j; } // namespace in\n"
3716                    "int k; }} // namespace out::mid",
3717                    Style));
3718 
3719   Style.NamespaceIndentation = FormatStyle::NI_Inner;
3720   EXPECT_EQ("namespace out { namespace in {\n"
3721             "  int i;\n"
3722             "}} // namespace out::in",
3723             format("namespace out {\n"
3724                    "namespace in {\n"
3725                    "int i;\n"
3726                    "} // namespace in\n"
3727                    "} // namespace out",
3728                    Style));
3729   EXPECT_EQ("namespace out { namespace mid { namespace in {\n"
3730             "  int i;\n"
3731             "}}} // namespace out::mid::in",
3732             format("namespace out {\n"
3733                    "namespace mid {\n"
3734                    "namespace in {\n"
3735                    "int i;\n"
3736                    "} // namespace in\n"
3737                    "} // namespace mid\n"
3738                    "} // namespace out",
3739                    Style));
3740 }
3741 
3742 TEST_F(FormatTest, FormatsExternC) {
3743   verifyFormat("extern \"C\" {\nint a;");
3744   verifyFormat("extern \"C\" {}");
3745   verifyFormat("extern \"C\" {\n"
3746                "int foo();\n"
3747                "}");
3748   verifyFormat("extern \"C\" int foo() {}");
3749   verifyFormat("extern \"C\" int foo();");
3750   verifyFormat("extern \"C\" int foo() {\n"
3751                "  int i = 42;\n"
3752                "  return i;\n"
3753                "}");
3754 
3755   FormatStyle Style = getLLVMStyle();
3756   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
3757   Style.BraceWrapping.AfterFunction = true;
3758   verifyFormat("extern \"C\" int foo() {}", Style);
3759   verifyFormat("extern \"C\" int foo();", Style);
3760   verifyFormat("extern \"C\" int foo()\n"
3761                "{\n"
3762                "  int i = 42;\n"
3763                "  return i;\n"
3764                "}",
3765                Style);
3766 
3767   Style.BraceWrapping.AfterExternBlock = true;
3768   Style.BraceWrapping.SplitEmptyRecord = false;
3769   verifyFormat("extern \"C\"\n"
3770                "{}",
3771                Style);
3772   verifyFormat("extern \"C\"\n"
3773                "{\n"
3774                "  int foo();\n"
3775                "}",
3776                Style);
3777 }
3778 
3779 TEST_F(FormatTest, IndentExternBlockStyle) {
3780   FormatStyle Style = getLLVMStyle();
3781   Style.IndentWidth = 2;
3782 
3783   Style.IndentExternBlock = FormatStyle::IEBS_Indent;
3784   verifyFormat("extern \"C\" { /*9*/\n}", Style);
3785   verifyFormat("extern \"C\" {\n"
3786                "  int foo10();\n"
3787                "}",
3788                Style);
3789 
3790   Style.IndentExternBlock = FormatStyle::IEBS_NoIndent;
3791   verifyFormat("extern \"C\" { /*11*/\n}", Style);
3792   verifyFormat("extern \"C\" {\n"
3793                "int foo12();\n"
3794                "}",
3795                Style);
3796 
3797   Style.IndentExternBlock = FormatStyle::IEBS_AfterExternBlock;
3798   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
3799   Style.BraceWrapping.AfterExternBlock = true;
3800   verifyFormat("extern \"C\"\n{ /*13*/\n}", Style);
3801   verifyFormat("extern \"C\"\n{\n"
3802                "  int foo14();\n"
3803                "}",
3804                Style);
3805 
3806   Style.IndentExternBlock = FormatStyle::IEBS_AfterExternBlock;
3807   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
3808   Style.BraceWrapping.AfterExternBlock = false;
3809   verifyFormat("extern \"C\" { /*15*/\n}", Style);
3810   verifyFormat("extern \"C\" {\n"
3811                "int foo16();\n"
3812                "}",
3813                Style);
3814 }
3815 
3816 TEST_F(FormatTest, FormatsInlineASM) {
3817   verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
3818   verifyFormat("asm(\"nop\" ::: \"memory\");");
3819   verifyFormat(
3820       "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
3821       "    \"cpuid\\n\\t\"\n"
3822       "    \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
3823       "    : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
3824       "    : \"a\"(value));");
3825   EXPECT_EQ(
3826       "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
3827       "  __asm {\n"
3828       "        mov     edx,[that] // vtable in edx\n"
3829       "        mov     eax,methodIndex\n"
3830       "        call    [edx][eax*4] // stdcall\n"
3831       "  }\n"
3832       "}",
3833       format("void NS_InvokeByIndex(void *that,   unsigned int methodIndex) {\n"
3834              "    __asm {\n"
3835              "        mov     edx,[that] // vtable in edx\n"
3836              "        mov     eax,methodIndex\n"
3837              "        call    [edx][eax*4] // stdcall\n"
3838              "    }\n"
3839              "}"));
3840   EXPECT_EQ("_asm {\n"
3841             "  xor eax, eax;\n"
3842             "  cpuid;\n"
3843             "}",
3844             format("_asm {\n"
3845                    "  xor eax, eax;\n"
3846                    "  cpuid;\n"
3847                    "}"));
3848   verifyFormat("void function() {\n"
3849                "  // comment\n"
3850                "  asm(\"\");\n"
3851                "}");
3852   EXPECT_EQ("__asm {\n"
3853             "}\n"
3854             "int i;",
3855             format("__asm   {\n"
3856                    "}\n"
3857                    "int   i;"));
3858 }
3859 
3860 TEST_F(FormatTest, FormatTryCatch) {
3861   verifyFormat("try {\n"
3862                "  throw a * b;\n"
3863                "} catch (int a) {\n"
3864                "  // Do nothing.\n"
3865                "} catch (...) {\n"
3866                "  exit(42);\n"
3867                "}");
3868 
3869   // Function-level try statements.
3870   verifyFormat("int f() try { return 4; } catch (...) {\n"
3871                "  return 5;\n"
3872                "}");
3873   verifyFormat("class A {\n"
3874                "  int a;\n"
3875                "  A() try : a(0) {\n"
3876                "  } catch (...) {\n"
3877                "    throw;\n"
3878                "  }\n"
3879                "};\n");
3880   verifyFormat("class A {\n"
3881                "  int a;\n"
3882                "  A() try : a(0), b{1} {\n"
3883                "  } catch (...) {\n"
3884                "    throw;\n"
3885                "  }\n"
3886                "};\n");
3887   verifyFormat("class A {\n"
3888                "  int a;\n"
3889                "  A() try : a(0), b{1}, c{2} {\n"
3890                "  } catch (...) {\n"
3891                "    throw;\n"
3892                "  }\n"
3893                "};\n");
3894   verifyFormat("class A {\n"
3895                "  int a;\n"
3896                "  A() try : a(0), b{1}, c{2} {\n"
3897                "    { // New scope.\n"
3898                "    }\n"
3899                "  } catch (...) {\n"
3900                "    throw;\n"
3901                "  }\n"
3902                "};\n");
3903 
3904   // Incomplete try-catch blocks.
3905   verifyIncompleteFormat("try {} catch (");
3906 }
3907 
3908 TEST_F(FormatTest, FormatTryAsAVariable) {
3909   verifyFormat("int try;");
3910   verifyFormat("int try, size;");
3911   verifyFormat("try = foo();");
3912   verifyFormat("if (try < size) {\n  return true;\n}");
3913 
3914   verifyFormat("int catch;");
3915   verifyFormat("int catch, size;");
3916   verifyFormat("catch = foo();");
3917   verifyFormat("if (catch < size) {\n  return true;\n}");
3918 
3919   FormatStyle Style = getLLVMStyle();
3920   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
3921   Style.BraceWrapping.AfterFunction = true;
3922   Style.BraceWrapping.BeforeCatch = true;
3923   verifyFormat("try {\n"
3924                "  int bar = 1;\n"
3925                "}\n"
3926                "catch (...) {\n"
3927                "  int bar = 1;\n"
3928                "}",
3929                Style);
3930   verifyFormat("#if NO_EX\n"
3931                "try\n"
3932                "#endif\n"
3933                "{\n"
3934                "}\n"
3935                "#if NO_EX\n"
3936                "catch (...) {\n"
3937                "}",
3938                Style);
3939   verifyFormat("try /* abc */ {\n"
3940                "  int bar = 1;\n"
3941                "}\n"
3942                "catch (...) {\n"
3943                "  int bar = 1;\n"
3944                "}",
3945                Style);
3946   verifyFormat("try\n"
3947                "// abc\n"
3948                "{\n"
3949                "  int bar = 1;\n"
3950                "}\n"
3951                "catch (...) {\n"
3952                "  int bar = 1;\n"
3953                "}",
3954                Style);
3955 }
3956 
3957 TEST_F(FormatTest, FormatSEHTryCatch) {
3958   verifyFormat("__try {\n"
3959                "  int a = b * c;\n"
3960                "} __except (EXCEPTION_EXECUTE_HANDLER) {\n"
3961                "  // Do nothing.\n"
3962                "}");
3963 
3964   verifyFormat("__try {\n"
3965                "  int a = b * c;\n"
3966                "} __finally {\n"
3967                "  // Do nothing.\n"
3968                "}");
3969 
3970   verifyFormat("DEBUG({\n"
3971                "  __try {\n"
3972                "  } __finally {\n"
3973                "  }\n"
3974                "});\n");
3975 }
3976 
3977 TEST_F(FormatTest, IncompleteTryCatchBlocks) {
3978   verifyFormat("try {\n"
3979                "  f();\n"
3980                "} catch {\n"
3981                "  g();\n"
3982                "}");
3983   verifyFormat("try {\n"
3984                "  f();\n"
3985                "} catch (A a) MACRO(x) {\n"
3986                "  g();\n"
3987                "} catch (B b) MACRO(x) {\n"
3988                "  g();\n"
3989                "}");
3990 }
3991 
3992 TEST_F(FormatTest, FormatTryCatchBraceStyles) {
3993   FormatStyle Style = getLLVMStyle();
3994   for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla,
3995                           FormatStyle::BS_WebKit}) {
3996     Style.BreakBeforeBraces = BraceStyle;
3997     verifyFormat("try {\n"
3998                  "  // something\n"
3999                  "} catch (...) {\n"
4000                  "  // something\n"
4001                  "}",
4002                  Style);
4003   }
4004   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
4005   verifyFormat("try {\n"
4006                "  // something\n"
4007                "}\n"
4008                "catch (...) {\n"
4009                "  // something\n"
4010                "}",
4011                Style);
4012   verifyFormat("__try {\n"
4013                "  // something\n"
4014                "}\n"
4015                "__finally {\n"
4016                "  // something\n"
4017                "}",
4018                Style);
4019   verifyFormat("@try {\n"
4020                "  // something\n"
4021                "}\n"
4022                "@finally {\n"
4023                "  // something\n"
4024                "}",
4025                Style);
4026   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
4027   verifyFormat("try\n"
4028                "{\n"
4029                "  // something\n"
4030                "}\n"
4031                "catch (...)\n"
4032                "{\n"
4033                "  // something\n"
4034                "}",
4035                Style);
4036   Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
4037   verifyFormat("try\n"
4038                "  {\n"
4039                "  // something white\n"
4040                "  }\n"
4041                "catch (...)\n"
4042                "  {\n"
4043                "  // something white\n"
4044                "  }",
4045                Style);
4046   Style.BreakBeforeBraces = FormatStyle::BS_GNU;
4047   verifyFormat("try\n"
4048                "  {\n"
4049                "    // something\n"
4050                "  }\n"
4051                "catch (...)\n"
4052                "  {\n"
4053                "    // something\n"
4054                "  }",
4055                Style);
4056   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4057   Style.BraceWrapping.BeforeCatch = true;
4058   verifyFormat("try {\n"
4059                "  // something\n"
4060                "}\n"
4061                "catch (...) {\n"
4062                "  // something\n"
4063                "}",
4064                Style);
4065 }
4066 
4067 TEST_F(FormatTest, StaticInitializers) {
4068   verifyFormat("static SomeClass SC = {1, 'a'};");
4069 
4070   verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n"
4071                "    100000000, "
4072                "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};");
4073 
4074   // Here, everything other than the "}" would fit on a line.
4075   verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
4076                "    10000000000000000000000000};");
4077   EXPECT_EQ("S s = {a,\n"
4078             "\n"
4079             "       b};",
4080             format("S s = {\n"
4081                    "  a,\n"
4082                    "\n"
4083                    "  b\n"
4084                    "};"));
4085 
4086   // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
4087   // line. However, the formatting looks a bit off and this probably doesn't
4088   // happen often in practice.
4089   verifyFormat("static int Variable[1] = {\n"
4090                "    {1000000000000000000000000000000000000}};",
4091                getLLVMStyleWithColumns(40));
4092 }
4093 
4094 TEST_F(FormatTest, DesignatedInitializers) {
4095   verifyFormat("const struct A a = {.a = 1, .b = 2};");
4096   verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n"
4097                "                    .bbbbbbbbbb = 2,\n"
4098                "                    .cccccccccc = 3,\n"
4099                "                    .dddddddddd = 4,\n"
4100                "                    .eeeeeeeeee = 5};");
4101   verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
4102                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
4103                "    .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
4104                "    .ccccccccccccccccccccccccccc = 3,\n"
4105                "    .ddddddddddddddddddddddddddd = 4,\n"
4106                "    .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};");
4107 
4108   verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
4109 
4110   verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
4111   verifyFormat("const struct A a = {[1] = aaaaaaaaaa,\n"
4112                "                    [2] = bbbbbbbbbb,\n"
4113                "                    [3] = cccccccccc,\n"
4114                "                    [4] = dddddddddd,\n"
4115                "                    [5] = eeeeeeeeee};");
4116   verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
4117                "    [1] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4118                "    [2] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
4119                "    [3] = cccccccccccccccccccccccccccccccccccccc,\n"
4120                "    [4] = dddddddddddddddddddddddddddddddddddddd,\n"
4121                "    [5] = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee};");
4122 }
4123 
4124 TEST_F(FormatTest, NestedStaticInitializers) {
4125   verifyFormat("static A x = {{{}}};\n");
4126   verifyFormat("static A x = {{{init1, init2, init3, init4},\n"
4127                "               {init1, init2, init3, init4}}};",
4128                getLLVMStyleWithColumns(50));
4129 
4130   verifyFormat("somes Status::global_reps[3] = {\n"
4131                "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
4132                "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
4133                "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};",
4134                getLLVMStyleWithColumns(60));
4135   verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
4136                      "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
4137                      "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
4138                      "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
4139   verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n"
4140                "                  {rect.fRight - rect.fLeft, rect.fBottom - "
4141                "rect.fTop}};");
4142 
4143   verifyFormat(
4144       "SomeArrayOfSomeType a = {\n"
4145       "    {{1, 2, 3},\n"
4146       "     {1, 2, 3},\n"
4147       "     {111111111111111111111111111111, 222222222222222222222222222222,\n"
4148       "      333333333333333333333333333333},\n"
4149       "     {1, 2, 3},\n"
4150       "     {1, 2, 3}}};");
4151   verifyFormat(
4152       "SomeArrayOfSomeType a = {\n"
4153       "    {{1, 2, 3}},\n"
4154       "    {{1, 2, 3}},\n"
4155       "    {{111111111111111111111111111111, 222222222222222222222222222222,\n"
4156       "      333333333333333333333333333333}},\n"
4157       "    {{1, 2, 3}},\n"
4158       "    {{1, 2, 3}}};");
4159 
4160   verifyFormat("struct {\n"
4161                "  unsigned bit;\n"
4162                "  const char *const name;\n"
4163                "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n"
4164                "                 {kOsWin, \"Windows\"},\n"
4165                "                 {kOsLinux, \"Linux\"},\n"
4166                "                 {kOsCrOS, \"Chrome OS\"}};");
4167   verifyFormat("struct {\n"
4168                "  unsigned bit;\n"
4169                "  const char *const name;\n"
4170                "} kBitsToOs[] = {\n"
4171                "    {kOsMac, \"Mac\"},\n"
4172                "    {kOsWin, \"Windows\"},\n"
4173                "    {kOsLinux, \"Linux\"},\n"
4174                "    {kOsCrOS, \"Chrome OS\"},\n"
4175                "};");
4176 }
4177 
4178 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
4179   verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
4180                "                      \\\n"
4181                "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
4182 }
4183 
4184 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
4185   verifyFormat("virtual void write(ELFWriter *writerrr,\n"
4186                "                   OwningPtr<FileOutputBuffer> &buffer) = 0;");
4187 
4188   // Do break defaulted and deleted functions.
4189   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
4190                "    default;",
4191                getLLVMStyleWithColumns(40));
4192   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
4193                "    delete;",
4194                getLLVMStyleWithColumns(40));
4195 }
4196 
4197 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) {
4198   verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3",
4199                getLLVMStyleWithColumns(40));
4200   verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
4201                getLLVMStyleWithColumns(40));
4202   EXPECT_EQ("#define Q                              \\\n"
4203             "  \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\"    \\\n"
4204             "  \"aaaaaaaa.cpp\"",
4205             format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
4206                    getLLVMStyleWithColumns(40)));
4207 }
4208 
4209 TEST_F(FormatTest, UnderstandsLinePPDirective) {
4210   EXPECT_EQ("# 123 \"A string literal\"",
4211             format("   #     123    \"A string literal\""));
4212 }
4213 
4214 TEST_F(FormatTest, LayoutUnknownPPDirective) {
4215   EXPECT_EQ("#;", format("#;"));
4216   verifyFormat("#\n;\n;\n;");
4217 }
4218 
4219 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
4220   EXPECT_EQ("#line 42 \"test\"\n",
4221             format("#  \\\n  line  \\\n  42  \\\n  \"test\"\n"));
4222   EXPECT_EQ("#define A B\n", format("#  \\\n define  \\\n    A  \\\n       B\n",
4223                                     getLLVMStyleWithColumns(12)));
4224 }
4225 
4226 TEST_F(FormatTest, EndOfFileEndsPPDirective) {
4227   EXPECT_EQ("#line 42 \"test\"",
4228             format("#  \\\n  line  \\\n  42  \\\n  \"test\""));
4229   EXPECT_EQ("#define A B", format("#  \\\n define  \\\n    A  \\\n       B"));
4230 }
4231 
4232 TEST_F(FormatTest, DoesntRemoveUnknownTokens) {
4233   verifyFormat("#define A \\x20");
4234   verifyFormat("#define A \\ x20");
4235   EXPECT_EQ("#define A \\ x20", format("#define A \\   x20"));
4236   verifyFormat("#define A ''");
4237   verifyFormat("#define A ''qqq");
4238   verifyFormat("#define A `qqq");
4239   verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");");
4240   EXPECT_EQ("const char *c = STRINGIFY(\n"
4241             "\\na : b);",
4242             format("const char * c = STRINGIFY(\n"
4243                    "\\na : b);"));
4244 
4245   verifyFormat("a\r\\");
4246   verifyFormat("a\v\\");
4247   verifyFormat("a\f\\");
4248 }
4249 
4250 TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) {
4251   FormatStyle style = getChromiumStyle(FormatStyle::LK_Cpp);
4252   style.IndentWidth = 4;
4253   style.PPIndentWidth = 1;
4254 
4255   style.IndentPPDirectives = FormatStyle::PPDIS_None;
4256   verifyFormat("#ifdef __linux__\n"
4257                "void foo() {\n"
4258                "    int x = 0;\n"
4259                "}\n"
4260                "#define FOO\n"
4261                "#endif\n"
4262                "void bar() {\n"
4263                "    int y = 0;\n"
4264                "}\n",
4265                style);
4266 
4267   style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
4268   verifyFormat("#ifdef __linux__\n"
4269                "void foo() {\n"
4270                "    int x = 0;\n"
4271                "}\n"
4272                "# define FOO foo\n"
4273                "#endif\n"
4274                "void bar() {\n"
4275                "    int y = 0;\n"
4276                "}\n",
4277                style);
4278 
4279   style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash;
4280   verifyFormat("#ifdef __linux__\n"
4281                "void foo() {\n"
4282                "    int x = 0;\n"
4283                "}\n"
4284                " #define FOO foo\n"
4285                "#endif\n"
4286                "void bar() {\n"
4287                "    int y = 0;\n"
4288                "}\n",
4289                style);
4290 }
4291 
4292 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
4293   verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
4294   verifyFormat("#define A( \\\n    BB)", getLLVMStyleWithColumns(12));
4295   verifyFormat("#define A( \\\n    A, B)", getLLVMStyleWithColumns(12));
4296   // FIXME: We never break before the macro name.
4297   verifyFormat("#define AA( \\\n    B)", getLLVMStyleWithColumns(12));
4298 
4299   verifyFormat("#define A A\n#define A A");
4300   verifyFormat("#define A(X) A\n#define A A");
4301 
4302   verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
4303   verifyFormat("#define Something    \\\n  Other", getLLVMStyleWithColumns(22));
4304 }
4305 
4306 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
4307   EXPECT_EQ("// somecomment\n"
4308             "#include \"a.h\"\n"
4309             "#define A(  \\\n"
4310             "    A, B)\n"
4311             "#include \"b.h\"\n"
4312             "// somecomment\n",
4313             format("  // somecomment\n"
4314                    "  #include \"a.h\"\n"
4315                    "#define A(A,\\\n"
4316                    "    B)\n"
4317                    "    #include \"b.h\"\n"
4318                    " // somecomment\n",
4319                    getLLVMStyleWithColumns(13)));
4320 }
4321 
4322 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); }
4323 
4324 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
4325   EXPECT_EQ("#define A    \\\n"
4326             "  c;         \\\n"
4327             "  e;\n"
4328             "f;",
4329             format("#define A c; e;\n"
4330                    "f;",
4331                    getLLVMStyleWithColumns(14)));
4332 }
4333 
4334 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
4335 
4336 TEST_F(FormatTest, MacroDefinitionInsideStatement) {
4337   EXPECT_EQ("int x,\n"
4338             "#define A\n"
4339             "    y;",
4340             format("int x,\n#define A\ny;"));
4341 }
4342 
4343 TEST_F(FormatTest, HashInMacroDefinition) {
4344   EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle()));
4345   verifyFormat("#define A \\\n  b #c;", getLLVMStyleWithColumns(11));
4346   verifyFormat("#define A  \\\n"
4347                "  {        \\\n"
4348                "    f(#c); \\\n"
4349                "  }",
4350                getLLVMStyleWithColumns(11));
4351 
4352   verifyFormat("#define A(X)         \\\n"
4353                "  void function##X()",
4354                getLLVMStyleWithColumns(22));
4355 
4356   verifyFormat("#define A(a, b, c)   \\\n"
4357                "  void a##b##c()",
4358                getLLVMStyleWithColumns(22));
4359 
4360   verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
4361 }
4362 
4363 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
4364   EXPECT_EQ("#define A (x)", format("#define A (x)"));
4365   EXPECT_EQ("#define A(x)", format("#define A(x)"));
4366 
4367   FormatStyle Style = getLLVMStyle();
4368   Style.SpaceBeforeParens = FormatStyle::SBPO_Never;
4369   verifyFormat("#define true ((foo)1)", Style);
4370   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
4371   verifyFormat("#define false((foo)0)", Style);
4372 }
4373 
4374 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
4375   EXPECT_EQ("#define A b;", format("#define A \\\n"
4376                                    "          \\\n"
4377                                    "  b;",
4378                                    getLLVMStyleWithColumns(25)));
4379   EXPECT_EQ("#define A \\\n"
4380             "          \\\n"
4381             "  a;      \\\n"
4382             "  b;",
4383             format("#define A \\\n"
4384                    "          \\\n"
4385                    "  a;      \\\n"
4386                    "  b;",
4387                    getLLVMStyleWithColumns(11)));
4388   EXPECT_EQ("#define A \\\n"
4389             "  a;      \\\n"
4390             "          \\\n"
4391             "  b;",
4392             format("#define A \\\n"
4393                    "  a;      \\\n"
4394                    "          \\\n"
4395                    "  b;",
4396                    getLLVMStyleWithColumns(11)));
4397 }
4398 
4399 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
4400   verifyIncompleteFormat("#define A :");
4401   verifyFormat("#define SOMECASES  \\\n"
4402                "  case 1:          \\\n"
4403                "  case 2\n",
4404                getLLVMStyleWithColumns(20));
4405   verifyFormat("#define MACRO(a) \\\n"
4406                "  if (a)         \\\n"
4407                "    f();         \\\n"
4408                "  else           \\\n"
4409                "    g()",
4410                getLLVMStyleWithColumns(18));
4411   verifyFormat("#define A template <typename T>");
4412   verifyIncompleteFormat("#define STR(x) #x\n"
4413                          "f(STR(this_is_a_string_literal{));");
4414   verifyFormat("#pragma omp threadprivate( \\\n"
4415                "    y)), // expected-warning",
4416                getLLVMStyleWithColumns(28));
4417   verifyFormat("#d, = };");
4418   verifyFormat("#if \"a");
4419   verifyIncompleteFormat("({\n"
4420                          "#define b     \\\n"
4421                          "  }           \\\n"
4422                          "  a\n"
4423                          "a",
4424                          getLLVMStyleWithColumns(15));
4425   verifyFormat("#define A     \\\n"
4426                "  {           \\\n"
4427                "    {\n"
4428                "#define B     \\\n"
4429                "  }           \\\n"
4430                "  }",
4431                getLLVMStyleWithColumns(15));
4432   verifyNoCrash("#if a\na(\n#else\n#endif\n{a");
4433   verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}");
4434   verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};");
4435   verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() {      \n)}");
4436 }
4437 
4438 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
4439   verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
4440   EXPECT_EQ("class A : public QObject {\n"
4441             "  Q_OBJECT\n"
4442             "\n"
4443             "  A() {}\n"
4444             "};",
4445             format("class A  :  public QObject {\n"
4446                    "     Q_OBJECT\n"
4447                    "\n"
4448                    "  A() {\n}\n"
4449                    "}  ;"));
4450   EXPECT_EQ("MACRO\n"
4451             "/*static*/ int i;",
4452             format("MACRO\n"
4453                    " /*static*/ int   i;"));
4454   EXPECT_EQ("SOME_MACRO\n"
4455             "namespace {\n"
4456             "void f();\n"
4457             "} // namespace",
4458             format("SOME_MACRO\n"
4459                    "  namespace    {\n"
4460                    "void   f(  );\n"
4461                    "} // namespace"));
4462   // Only if the identifier contains at least 5 characters.
4463   EXPECT_EQ("HTTP f();", format("HTTP\nf();"));
4464   EXPECT_EQ("MACRO\nf();", format("MACRO\nf();"));
4465   // Only if everything is upper case.
4466   EXPECT_EQ("class A : public QObject {\n"
4467             "  Q_Object A() {}\n"
4468             "};",
4469             format("class A  :  public QObject {\n"
4470                    "     Q_Object\n"
4471                    "  A() {\n}\n"
4472                    "}  ;"));
4473 
4474   // Only if the next line can actually start an unwrapped line.
4475   EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;",
4476             format("SOME_WEIRD_LOG_MACRO\n"
4477                    "<< SomeThing;"));
4478 
4479   verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
4480                "(n, buffers))\n",
4481                getChromiumStyle(FormatStyle::LK_Cpp));
4482 
4483   // See PR41483
4484   EXPECT_EQ("/**/ FOO(a)\n"
4485             "FOO(b)",
4486             format("/**/ FOO(a)\n"
4487                    "FOO(b)"));
4488 }
4489 
4490 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
4491   EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
4492             "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
4493             "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
4494             "class X {};\n"
4495             "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
4496             "int *createScopDetectionPass() { return 0; }",
4497             format("  INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
4498                    "  INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
4499                    "  INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
4500                    "  class X {};\n"
4501                    "  INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
4502                    "  int *createScopDetectionPass() { return 0; }"));
4503   // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
4504   // braces, so that inner block is indented one level more.
4505   EXPECT_EQ("int q() {\n"
4506             "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
4507             "  IPC_MESSAGE_HANDLER(xxx, qqq)\n"
4508             "  IPC_END_MESSAGE_MAP()\n"
4509             "}",
4510             format("int q() {\n"
4511                    "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
4512                    "    IPC_MESSAGE_HANDLER(xxx, qqq)\n"
4513                    "  IPC_END_MESSAGE_MAP()\n"
4514                    "}"));
4515 
4516   // Same inside macros.
4517   EXPECT_EQ("#define LIST(L) \\\n"
4518             "  L(A)          \\\n"
4519             "  L(B)          \\\n"
4520             "  L(C)",
4521             format("#define LIST(L) \\\n"
4522                    "  L(A) \\\n"
4523                    "  L(B) \\\n"
4524                    "  L(C)",
4525                    getGoogleStyle()));
4526 
4527   // These must not be recognized as macros.
4528   EXPECT_EQ("int q() {\n"
4529             "  f(x);\n"
4530             "  f(x) {}\n"
4531             "  f(x)->g();\n"
4532             "  f(x)->*g();\n"
4533             "  f(x).g();\n"
4534             "  f(x) = x;\n"
4535             "  f(x) += x;\n"
4536             "  f(x) -= x;\n"
4537             "  f(x) *= x;\n"
4538             "  f(x) /= x;\n"
4539             "  f(x) %= x;\n"
4540             "  f(x) &= x;\n"
4541             "  f(x) |= x;\n"
4542             "  f(x) ^= x;\n"
4543             "  f(x) >>= x;\n"
4544             "  f(x) <<= x;\n"
4545             "  f(x)[y].z();\n"
4546             "  LOG(INFO) << x;\n"
4547             "  ifstream(x) >> x;\n"
4548             "}\n",
4549             format("int q() {\n"
4550                    "  f(x)\n;\n"
4551                    "  f(x)\n {}\n"
4552                    "  f(x)\n->g();\n"
4553                    "  f(x)\n->*g();\n"
4554                    "  f(x)\n.g();\n"
4555                    "  f(x)\n = x;\n"
4556                    "  f(x)\n += x;\n"
4557                    "  f(x)\n -= x;\n"
4558                    "  f(x)\n *= x;\n"
4559                    "  f(x)\n /= x;\n"
4560                    "  f(x)\n %= x;\n"
4561                    "  f(x)\n &= x;\n"
4562                    "  f(x)\n |= x;\n"
4563                    "  f(x)\n ^= x;\n"
4564                    "  f(x)\n >>= x;\n"
4565                    "  f(x)\n <<= x;\n"
4566                    "  f(x)\n[y].z();\n"
4567                    "  LOG(INFO)\n << x;\n"
4568                    "  ifstream(x)\n >> x;\n"
4569                    "}\n"));
4570   EXPECT_EQ("int q() {\n"
4571             "  F(x)\n"
4572             "  if (1) {\n"
4573             "  }\n"
4574             "  F(x)\n"
4575             "  while (1) {\n"
4576             "  }\n"
4577             "  F(x)\n"
4578             "  G(x);\n"
4579             "  F(x)\n"
4580             "  try {\n"
4581             "    Q();\n"
4582             "  } catch (...) {\n"
4583             "  }\n"
4584             "}\n",
4585             format("int q() {\n"
4586                    "F(x)\n"
4587                    "if (1) {}\n"
4588                    "F(x)\n"
4589                    "while (1) {}\n"
4590                    "F(x)\n"
4591                    "G(x);\n"
4592                    "F(x)\n"
4593                    "try { Q(); } catch (...) {}\n"
4594                    "}\n"));
4595   EXPECT_EQ("class A {\n"
4596             "  A() : t(0) {}\n"
4597             "  A(int i) noexcept() : {}\n"
4598             "  A(X x)\n" // FIXME: function-level try blocks are broken.
4599             "  try : t(0) {\n"
4600             "  } catch (...) {\n"
4601             "  }\n"
4602             "};",
4603             format("class A {\n"
4604                    "  A()\n : t(0) {}\n"
4605                    "  A(int i)\n noexcept() : {}\n"
4606                    "  A(X x)\n"
4607                    "  try : t(0) {} catch (...) {}\n"
4608                    "};"));
4609   FormatStyle Style = getLLVMStyle();
4610   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4611   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
4612   Style.BraceWrapping.AfterFunction = true;
4613   EXPECT_EQ("void f()\n"
4614             "try\n"
4615             "{\n"
4616             "}",
4617             format("void f() try {\n"
4618                    "}",
4619                    Style));
4620   EXPECT_EQ("class SomeClass {\n"
4621             "public:\n"
4622             "  SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
4623             "};",
4624             format("class SomeClass {\n"
4625                    "public:\n"
4626                    "  SomeClass()\n"
4627                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
4628                    "};"));
4629   EXPECT_EQ("class SomeClass {\n"
4630             "public:\n"
4631             "  SomeClass()\n"
4632             "      EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
4633             "};",
4634             format("class SomeClass {\n"
4635                    "public:\n"
4636                    "  SomeClass()\n"
4637                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
4638                    "};",
4639                    getLLVMStyleWithColumns(40)));
4640 
4641   verifyFormat("MACRO(>)");
4642 
4643   // Some macros contain an implicit semicolon.
4644   Style = getLLVMStyle();
4645   Style.StatementMacros.push_back("FOO");
4646   verifyFormat("FOO(a) int b = 0;");
4647   verifyFormat("FOO(a)\n"
4648                "int b = 0;",
4649                Style);
4650   verifyFormat("FOO(a);\n"
4651                "int b = 0;",
4652                Style);
4653   verifyFormat("FOO(argc, argv, \"4.0.2\")\n"
4654                "int b = 0;",
4655                Style);
4656   verifyFormat("FOO()\n"
4657                "int b = 0;",
4658                Style);
4659   verifyFormat("FOO\n"
4660                "int b = 0;",
4661                Style);
4662   verifyFormat("void f() {\n"
4663                "  FOO(a)\n"
4664                "  return a;\n"
4665                "}",
4666                Style);
4667   verifyFormat("FOO(a)\n"
4668                "FOO(b)",
4669                Style);
4670   verifyFormat("int a = 0;\n"
4671                "FOO(b)\n"
4672                "int c = 0;",
4673                Style);
4674   verifyFormat("int a = 0;\n"
4675                "int x = FOO(a)\n"
4676                "int b = 0;",
4677                Style);
4678   verifyFormat("void foo(int a) { FOO(a) }\n"
4679                "uint32_t bar() {}",
4680                Style);
4681 }
4682 
4683 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
4684   verifyFormat("#define A \\\n"
4685                "  f({     \\\n"
4686                "    g();  \\\n"
4687                "  });",
4688                getLLVMStyleWithColumns(11));
4689 }
4690 
4691 TEST_F(FormatTest, IndentPreprocessorDirectives) {
4692   FormatStyle Style = getLLVMStyle();
4693   Style.IndentPPDirectives = FormatStyle::PPDIS_None;
4694   Style.ColumnLimit = 40;
4695   verifyFormat("#ifdef _WIN32\n"
4696                "#define A 0\n"
4697                "#ifdef VAR2\n"
4698                "#define B 1\n"
4699                "#include <someheader.h>\n"
4700                "#define MACRO                          \\\n"
4701                "  some_very_long_func_aaaaaaaaaa();\n"
4702                "#endif\n"
4703                "#else\n"
4704                "#define A 1\n"
4705                "#endif",
4706                Style);
4707   Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
4708   verifyFormat("#ifdef _WIN32\n"
4709                "#  define A 0\n"
4710                "#  ifdef VAR2\n"
4711                "#    define B 1\n"
4712                "#    include <someheader.h>\n"
4713                "#    define MACRO                      \\\n"
4714                "      some_very_long_func_aaaaaaaaaa();\n"
4715                "#  endif\n"
4716                "#else\n"
4717                "#  define A 1\n"
4718                "#endif",
4719                Style);
4720   verifyFormat("#if A\n"
4721                "#  define MACRO                        \\\n"
4722                "    void a(int x) {                    \\\n"
4723                "      b();                             \\\n"
4724                "      c();                             \\\n"
4725                "      d();                             \\\n"
4726                "      e();                             \\\n"
4727                "      f();                             \\\n"
4728                "    }\n"
4729                "#endif",
4730                Style);
4731   // Comments before include guard.
4732   verifyFormat("// file comment\n"
4733                "// file comment\n"
4734                "#ifndef HEADER_H\n"
4735                "#define HEADER_H\n"
4736                "code();\n"
4737                "#endif",
4738                Style);
4739   // Test with include guards.
4740   verifyFormat("#ifndef HEADER_H\n"
4741                "#define HEADER_H\n"
4742                "code();\n"
4743                "#endif",
4744                Style);
4745   // Include guards must have a #define with the same variable immediately
4746   // after #ifndef.
4747   verifyFormat("#ifndef NOT_GUARD\n"
4748                "#  define FOO\n"
4749                "code();\n"
4750                "#endif",
4751                Style);
4752 
4753   // Include guards must cover the entire file.
4754   verifyFormat("code();\n"
4755                "code();\n"
4756                "#ifndef NOT_GUARD\n"
4757                "#  define NOT_GUARD\n"
4758                "code();\n"
4759                "#endif",
4760                Style);
4761   verifyFormat("#ifndef NOT_GUARD\n"
4762                "#  define NOT_GUARD\n"
4763                "code();\n"
4764                "#endif\n"
4765                "code();",
4766                Style);
4767   // Test with trailing blank lines.
4768   verifyFormat("#ifndef HEADER_H\n"
4769                "#define HEADER_H\n"
4770                "code();\n"
4771                "#endif\n",
4772                Style);
4773   // Include guards don't have #else.
4774   verifyFormat("#ifndef NOT_GUARD\n"
4775                "#  define NOT_GUARD\n"
4776                "code();\n"
4777                "#else\n"
4778                "#endif",
4779                Style);
4780   verifyFormat("#ifndef NOT_GUARD\n"
4781                "#  define NOT_GUARD\n"
4782                "code();\n"
4783                "#elif FOO\n"
4784                "#endif",
4785                Style);
4786   // Non-identifier #define after potential include guard.
4787   verifyFormat("#ifndef FOO\n"
4788                "#  define 1\n"
4789                "#endif\n",
4790                Style);
4791   // #if closes past last non-preprocessor line.
4792   verifyFormat("#ifndef FOO\n"
4793                "#define FOO\n"
4794                "#if 1\n"
4795                "int i;\n"
4796                "#  define A 0\n"
4797                "#endif\n"
4798                "#endif\n",
4799                Style);
4800   // Don't crash if there is an #elif directive without a condition.
4801   verifyFormat("#if 1\n"
4802                "int x;\n"
4803                "#elif\n"
4804                "int y;\n"
4805                "#else\n"
4806                "int z;\n"
4807                "#endif",
4808                Style);
4809   // FIXME: This doesn't handle the case where there's code between the
4810   // #ifndef and #define but all other conditions hold. This is because when
4811   // the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the
4812   // previous code line yet, so we can't detect it.
4813   EXPECT_EQ("#ifndef NOT_GUARD\n"
4814             "code();\n"
4815             "#define NOT_GUARD\n"
4816             "code();\n"
4817             "#endif",
4818             format("#ifndef NOT_GUARD\n"
4819                    "code();\n"
4820                    "#  define NOT_GUARD\n"
4821                    "code();\n"
4822                    "#endif",
4823                    Style));
4824   // FIXME: This doesn't handle cases where legitimate preprocessor lines may
4825   // be outside an include guard. Examples are #pragma once and
4826   // #pragma GCC diagnostic, or anything else that does not change the meaning
4827   // of the file if it's included multiple times.
4828   EXPECT_EQ("#ifdef WIN32\n"
4829             "#  pragma once\n"
4830             "#endif\n"
4831             "#ifndef HEADER_H\n"
4832             "#  define HEADER_H\n"
4833             "code();\n"
4834             "#endif",
4835             format("#ifdef WIN32\n"
4836                    "#  pragma once\n"
4837                    "#endif\n"
4838                    "#ifndef HEADER_H\n"
4839                    "#define HEADER_H\n"
4840                    "code();\n"
4841                    "#endif",
4842                    Style));
4843   // FIXME: This does not detect when there is a single non-preprocessor line
4844   // in front of an include-guard-like structure where other conditions hold
4845   // because ScopedLineState hides the line.
4846   EXPECT_EQ("code();\n"
4847             "#ifndef HEADER_H\n"
4848             "#define HEADER_H\n"
4849             "code();\n"
4850             "#endif",
4851             format("code();\n"
4852                    "#ifndef HEADER_H\n"
4853                    "#  define HEADER_H\n"
4854                    "code();\n"
4855                    "#endif",
4856                    Style));
4857   // Keep comments aligned with #, otherwise indent comments normally. These
4858   // tests cannot use verifyFormat because messUp manipulates leading
4859   // whitespace.
4860   {
4861     const char *Expected = ""
4862                            "void f() {\n"
4863                            "#if 1\n"
4864                            "// Preprocessor aligned.\n"
4865                            "#  define A 0\n"
4866                            "  // Code. Separated by blank line.\n"
4867                            "\n"
4868                            "#  define B 0\n"
4869                            "  // Code. Not aligned with #\n"
4870                            "#  define C 0\n"
4871                            "#endif";
4872     const char *ToFormat = ""
4873                            "void f() {\n"
4874                            "#if 1\n"
4875                            "// Preprocessor aligned.\n"
4876                            "#  define A 0\n"
4877                            "// Code. Separated by blank line.\n"
4878                            "\n"
4879                            "#  define B 0\n"
4880                            "   // Code. Not aligned with #\n"
4881                            "#  define C 0\n"
4882                            "#endif";
4883     EXPECT_EQ(Expected, format(ToFormat, Style));
4884     EXPECT_EQ(Expected, format(Expected, Style));
4885   }
4886   // Keep block quotes aligned.
4887   {
4888     const char *Expected = ""
4889                            "void f() {\n"
4890                            "#if 1\n"
4891                            "/* Preprocessor aligned. */\n"
4892                            "#  define A 0\n"
4893                            "  /* Code. Separated by blank line. */\n"
4894                            "\n"
4895                            "#  define B 0\n"
4896                            "  /* Code. Not aligned with # */\n"
4897                            "#  define C 0\n"
4898                            "#endif";
4899     const char *ToFormat = ""
4900                            "void f() {\n"
4901                            "#if 1\n"
4902                            "/* Preprocessor aligned. */\n"
4903                            "#  define A 0\n"
4904                            "/* Code. Separated by blank line. */\n"
4905                            "\n"
4906                            "#  define B 0\n"
4907                            "   /* Code. Not aligned with # */\n"
4908                            "#  define C 0\n"
4909                            "#endif";
4910     EXPECT_EQ(Expected, format(ToFormat, Style));
4911     EXPECT_EQ(Expected, format(Expected, Style));
4912   }
4913   // Keep comments aligned with un-indented directives.
4914   {
4915     const char *Expected = ""
4916                            "void f() {\n"
4917                            "// Preprocessor aligned.\n"
4918                            "#define A 0\n"
4919                            "  // Code. Separated by blank line.\n"
4920                            "\n"
4921                            "#define B 0\n"
4922                            "  // Code. Not aligned with #\n"
4923                            "#define C 0\n";
4924     const char *ToFormat = ""
4925                            "void f() {\n"
4926                            "// Preprocessor aligned.\n"
4927                            "#define A 0\n"
4928                            "// Code. Separated by blank line.\n"
4929                            "\n"
4930                            "#define B 0\n"
4931                            "   // Code. Not aligned with #\n"
4932                            "#define C 0\n";
4933     EXPECT_EQ(Expected, format(ToFormat, Style));
4934     EXPECT_EQ(Expected, format(Expected, Style));
4935   }
4936   // Test AfterHash with tabs.
4937   {
4938     FormatStyle Tabbed = Style;
4939     Tabbed.UseTab = FormatStyle::UT_Always;
4940     Tabbed.IndentWidth = 8;
4941     Tabbed.TabWidth = 8;
4942     verifyFormat("#ifdef _WIN32\n"
4943                  "#\tdefine A 0\n"
4944                  "#\tifdef VAR2\n"
4945                  "#\t\tdefine B 1\n"
4946                  "#\t\tinclude <someheader.h>\n"
4947                  "#\t\tdefine MACRO          \\\n"
4948                  "\t\t\tsome_very_long_func_aaaaaaaaaa();\n"
4949                  "#\tendif\n"
4950                  "#else\n"
4951                  "#\tdefine A 1\n"
4952                  "#endif",
4953                  Tabbed);
4954   }
4955 
4956   // Regression test: Multiline-macro inside include guards.
4957   verifyFormat("#ifndef HEADER_H\n"
4958                "#define HEADER_H\n"
4959                "#define A()        \\\n"
4960                "  int i;           \\\n"
4961                "  int j;\n"
4962                "#endif // HEADER_H",
4963                getLLVMStyleWithColumns(20));
4964 
4965   Style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash;
4966   // Basic before hash indent tests
4967   verifyFormat("#ifdef _WIN32\n"
4968                "  #define A 0\n"
4969                "  #ifdef VAR2\n"
4970                "    #define B 1\n"
4971                "    #include <someheader.h>\n"
4972                "    #define MACRO                      \\\n"
4973                "      some_very_long_func_aaaaaaaaaa();\n"
4974                "  #endif\n"
4975                "#else\n"
4976                "  #define A 1\n"
4977                "#endif",
4978                Style);
4979   verifyFormat("#if A\n"
4980                "  #define MACRO                        \\\n"
4981                "    void a(int x) {                    \\\n"
4982                "      b();                             \\\n"
4983                "      c();                             \\\n"
4984                "      d();                             \\\n"
4985                "      e();                             \\\n"
4986                "      f();                             \\\n"
4987                "    }\n"
4988                "#endif",
4989                Style);
4990   // Keep comments aligned with indented directives. These
4991   // tests cannot use verifyFormat because messUp manipulates leading
4992   // whitespace.
4993   {
4994     const char *Expected = "void f() {\n"
4995                            "// Aligned to preprocessor.\n"
4996                            "#if 1\n"
4997                            "  // Aligned to code.\n"
4998                            "  int a;\n"
4999                            "  #if 1\n"
5000                            "    // Aligned to preprocessor.\n"
5001                            "    #define A 0\n"
5002                            "  // Aligned to code.\n"
5003                            "  int b;\n"
5004                            "  #endif\n"
5005                            "#endif\n"
5006                            "}";
5007     const char *ToFormat = "void f() {\n"
5008                            "// Aligned to preprocessor.\n"
5009                            "#if 1\n"
5010                            "// Aligned to code.\n"
5011                            "int a;\n"
5012                            "#if 1\n"
5013                            "// Aligned to preprocessor.\n"
5014                            "#define A 0\n"
5015                            "// Aligned to code.\n"
5016                            "int b;\n"
5017                            "#endif\n"
5018                            "#endif\n"
5019                            "}";
5020     EXPECT_EQ(Expected, format(ToFormat, Style));
5021     EXPECT_EQ(Expected, format(Expected, Style));
5022   }
5023   {
5024     const char *Expected = "void f() {\n"
5025                            "/* Aligned to preprocessor. */\n"
5026                            "#if 1\n"
5027                            "  /* Aligned to code. */\n"
5028                            "  int a;\n"
5029                            "  #if 1\n"
5030                            "    /* Aligned to preprocessor. */\n"
5031                            "    #define A 0\n"
5032                            "  /* Aligned to code. */\n"
5033                            "  int b;\n"
5034                            "  #endif\n"
5035                            "#endif\n"
5036                            "}";
5037     const char *ToFormat = "void f() {\n"
5038                            "/* Aligned to preprocessor. */\n"
5039                            "#if 1\n"
5040                            "/* Aligned to code. */\n"
5041                            "int a;\n"
5042                            "#if 1\n"
5043                            "/* Aligned to preprocessor. */\n"
5044                            "#define A 0\n"
5045                            "/* Aligned to code. */\n"
5046                            "int b;\n"
5047                            "#endif\n"
5048                            "#endif\n"
5049                            "}";
5050     EXPECT_EQ(Expected, format(ToFormat, Style));
5051     EXPECT_EQ(Expected, format(Expected, Style));
5052   }
5053 
5054   // Test single comment before preprocessor
5055   verifyFormat("// Comment\n"
5056                "\n"
5057                "#if 1\n"
5058                "#endif",
5059                Style);
5060 }
5061 
5062 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
5063   verifyFormat("{\n  { a #c; }\n}");
5064 }
5065 
5066 TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
5067   EXPECT_EQ("#define A \\\n  {       \\\n    {\nint i;",
5068             format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
5069   EXPECT_EQ("#define A \\\n  }       \\\n  }\nint i;",
5070             format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
5071 }
5072 
5073 TEST_F(FormatTest, EscapedNewlines) {
5074   FormatStyle Narrow = getLLVMStyleWithColumns(11);
5075   EXPECT_EQ("#define A \\\n  int i;  \\\n  int j;",
5076             format("#define A \\\nint i;\\\n  int j;", Narrow));
5077   EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;"));
5078   EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
5079   EXPECT_EQ("/* \\  \\  \\\n */", format("\\\n/* \\  \\  \\\n */"));
5080   EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>"));
5081 
5082   FormatStyle AlignLeft = getLLVMStyle();
5083   AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
5084   EXPECT_EQ("#define MACRO(x) \\\n"
5085             "private:         \\\n"
5086             "  int x(int a);\n",
5087             format("#define MACRO(x) \\\n"
5088                    "private:         \\\n"
5089                    "  int x(int a);\n",
5090                    AlignLeft));
5091 
5092   // CRLF line endings
5093   EXPECT_EQ("#define A \\\r\n  int i;  \\\r\n  int j;",
5094             format("#define A \\\r\nint i;\\\r\n  int j;", Narrow));
5095   EXPECT_EQ("#define A\r\n\r\nint i;", format("#define A \\\r\n\r\n int i;"));
5096   EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
5097   EXPECT_EQ("/* \\  \\  \\\r\n */", format("\\\r\n/* \\  \\  \\\r\n */"));
5098   EXPECT_EQ("<a\r\n\\\\\r\n>", format("<a\r\n\\\\\r\n>"));
5099   EXPECT_EQ("#define MACRO(x) \\\r\n"
5100             "private:         \\\r\n"
5101             "  int x(int a);\r\n",
5102             format("#define MACRO(x) \\\r\n"
5103                    "private:         \\\r\n"
5104                    "  int x(int a);\r\n",
5105                    AlignLeft));
5106 
5107   FormatStyle DontAlign = getLLVMStyle();
5108   DontAlign.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
5109   DontAlign.MaxEmptyLinesToKeep = 3;
5110   // FIXME: can't use verifyFormat here because the newline before
5111   // "public:" is not inserted the first time it's reformatted
5112   EXPECT_EQ("#define A \\\n"
5113             "  class Foo { \\\n"
5114             "    void bar(); \\\n"
5115             "\\\n"
5116             "\\\n"
5117             "\\\n"
5118             "  public: \\\n"
5119             "    void baz(); \\\n"
5120             "  };",
5121             format("#define A \\\n"
5122                    "  class Foo { \\\n"
5123                    "    void bar(); \\\n"
5124                    "\\\n"
5125                    "\\\n"
5126                    "\\\n"
5127                    "  public: \\\n"
5128                    "    void baz(); \\\n"
5129                    "  };",
5130                    DontAlign));
5131 }
5132 
5133 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
5134   verifyFormat("#define A \\\n"
5135                "  int v(  \\\n"
5136                "      a); \\\n"
5137                "  int i;",
5138                getLLVMStyleWithColumns(11));
5139 }
5140 
5141 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
5142   EXPECT_EQ(
5143       "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
5144       "                      \\\n"
5145       "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
5146       "\n"
5147       "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
5148       "    aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
5149       format("  #define   ALooooooooooooooooooooooooooooooooooooooongMacro("
5150              "\\\n"
5151              "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
5152              "  \n"
5153              "   AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
5154              "  aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
5155 }
5156 
5157 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
5158   EXPECT_EQ("int\n"
5159             "#define A\n"
5160             "    a;",
5161             format("int\n#define A\na;"));
5162   verifyFormat("functionCallTo(\n"
5163                "    someOtherFunction(\n"
5164                "        withSomeParameters, whichInSequence,\n"
5165                "        areLongerThanALine(andAnotherCall,\n"
5166                "#define A B\n"
5167                "                           withMoreParamters,\n"
5168                "                           whichStronglyInfluenceTheLayout),\n"
5169                "        andMoreParameters),\n"
5170                "    trailing);",
5171                getLLVMStyleWithColumns(69));
5172   verifyFormat("Foo::Foo()\n"
5173                "#ifdef BAR\n"
5174                "    : baz(0)\n"
5175                "#endif\n"
5176                "{\n"
5177                "}");
5178   verifyFormat("void f() {\n"
5179                "  if (true)\n"
5180                "#ifdef A\n"
5181                "    f(42);\n"
5182                "  x();\n"
5183                "#else\n"
5184                "    g();\n"
5185                "  x();\n"
5186                "#endif\n"
5187                "}");
5188   verifyFormat("void f(param1, param2,\n"
5189                "       param3,\n"
5190                "#ifdef A\n"
5191                "       param4(param5,\n"
5192                "#ifdef A1\n"
5193                "              param6,\n"
5194                "#ifdef A2\n"
5195                "              param7),\n"
5196                "#else\n"
5197                "              param8),\n"
5198                "       param9,\n"
5199                "#endif\n"
5200                "       param10,\n"
5201                "#endif\n"
5202                "       param11)\n"
5203                "#else\n"
5204                "       param12)\n"
5205                "#endif\n"
5206                "{\n"
5207                "  x();\n"
5208                "}",
5209                getLLVMStyleWithColumns(28));
5210   verifyFormat("#if 1\n"
5211                "int i;");
5212   verifyFormat("#if 1\n"
5213                "#endif\n"
5214                "#if 1\n"
5215                "#else\n"
5216                "#endif\n");
5217   verifyFormat("DEBUG({\n"
5218                "  return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5219                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
5220                "});\n"
5221                "#if a\n"
5222                "#else\n"
5223                "#endif");
5224 
5225   verifyIncompleteFormat("void f(\n"
5226                          "#if A\n"
5227                          ");\n"
5228                          "#else\n"
5229                          "#endif");
5230 }
5231 
5232 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
5233   verifyFormat("#endif\n"
5234                "#if B");
5235 }
5236 
5237 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
5238   FormatStyle SingleLine = getLLVMStyle();
5239   SingleLine.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse;
5240   verifyFormat("#if 0\n"
5241                "#elif 1\n"
5242                "#endif\n"
5243                "void foo() {\n"
5244                "  if (test) foo2();\n"
5245                "}",
5246                SingleLine);
5247 }
5248 
5249 TEST_F(FormatTest, LayoutBlockInsideParens) {
5250   verifyFormat("functionCall({ int i; });");
5251   verifyFormat("functionCall({\n"
5252                "  int i;\n"
5253                "  int j;\n"
5254                "});");
5255   verifyFormat("functionCall(\n"
5256                "    {\n"
5257                "      int i;\n"
5258                "      int j;\n"
5259                "    },\n"
5260                "    aaaa, bbbb, cccc);");
5261   verifyFormat("functionA(functionB({\n"
5262                "            int i;\n"
5263                "            int j;\n"
5264                "          }),\n"
5265                "          aaaa, bbbb, cccc);");
5266   verifyFormat("functionCall(\n"
5267                "    {\n"
5268                "      int i;\n"
5269                "      int j;\n"
5270                "    },\n"
5271                "    aaaa, bbbb, // comment\n"
5272                "    cccc);");
5273   verifyFormat("functionA(functionB({\n"
5274                "            int i;\n"
5275                "            int j;\n"
5276                "          }),\n"
5277                "          aaaa, bbbb, // comment\n"
5278                "          cccc);");
5279   verifyFormat("functionCall(aaaa, bbbb, { int i; });");
5280   verifyFormat("functionCall(aaaa, bbbb, {\n"
5281                "  int i;\n"
5282                "  int j;\n"
5283                "});");
5284   verifyFormat(
5285       "Aaa(\n" // FIXME: There shouldn't be a linebreak here.
5286       "    {\n"
5287       "      int i; // break\n"
5288       "    },\n"
5289       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
5290       "                                     ccccccccccccccccc));");
5291   verifyFormat("DEBUG({\n"
5292                "  if (a)\n"
5293                "    f();\n"
5294                "});");
5295 }
5296 
5297 TEST_F(FormatTest, LayoutBlockInsideStatement) {
5298   EXPECT_EQ("SOME_MACRO { int i; }\n"
5299             "int i;",
5300             format("  SOME_MACRO  {int i;}  int i;"));
5301 }
5302 
5303 TEST_F(FormatTest, LayoutNestedBlocks) {
5304   verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
5305                "  struct s {\n"
5306                "    int i;\n"
5307                "  };\n"
5308                "  s kBitsToOs[] = {{10}};\n"
5309                "  for (int i = 0; i < 10; ++i)\n"
5310                "    return;\n"
5311                "}");
5312   verifyFormat("call(parameter, {\n"
5313                "  something();\n"
5314                "  // Comment using all columns.\n"
5315                "  somethingelse();\n"
5316                "});",
5317                getLLVMStyleWithColumns(40));
5318   verifyFormat("DEBUG( //\n"
5319                "    { f(); }, a);");
5320   verifyFormat("DEBUG( //\n"
5321                "    {\n"
5322                "      f(); //\n"
5323                "    },\n"
5324                "    a);");
5325 
5326   EXPECT_EQ("call(parameter, {\n"
5327             "  something();\n"
5328             "  // Comment too\n"
5329             "  // looooooooooong.\n"
5330             "  somethingElse();\n"
5331             "});",
5332             format("call(parameter, {\n"
5333                    "  something();\n"
5334                    "  // Comment too looooooooooong.\n"
5335                    "  somethingElse();\n"
5336                    "});",
5337                    getLLVMStyleWithColumns(29)));
5338   EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int   i; });"));
5339   EXPECT_EQ("DEBUG({ // comment\n"
5340             "  int i;\n"
5341             "});",
5342             format("DEBUG({ // comment\n"
5343                    "int  i;\n"
5344                    "});"));
5345   EXPECT_EQ("DEBUG({\n"
5346             "  int i;\n"
5347             "\n"
5348             "  // comment\n"
5349             "  int j;\n"
5350             "});",
5351             format("DEBUG({\n"
5352                    "  int  i;\n"
5353                    "\n"
5354                    "  // comment\n"
5355                    "  int  j;\n"
5356                    "});"));
5357 
5358   verifyFormat("DEBUG({\n"
5359                "  if (a)\n"
5360                "    return;\n"
5361                "});");
5362   verifyGoogleFormat("DEBUG({\n"
5363                      "  if (a) return;\n"
5364                      "});");
5365   FormatStyle Style = getGoogleStyle();
5366   Style.ColumnLimit = 45;
5367   verifyFormat("Debug(\n"
5368                "    aaaaa,\n"
5369                "    {\n"
5370                "      if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
5371                "    },\n"
5372                "    a);",
5373                Style);
5374 
5375   verifyFormat("SomeFunction({MACRO({ return output; }), b});");
5376 
5377   verifyNoCrash("^{v^{a}}");
5378 }
5379 
5380 TEST_F(FormatTest, FormatNestedBlocksInMacros) {
5381   EXPECT_EQ("#define MACRO()                     \\\n"
5382             "  Debug(aaa, /* force line break */ \\\n"
5383             "        {                           \\\n"
5384             "          int i;                    \\\n"
5385             "          int j;                    \\\n"
5386             "        })",
5387             format("#define   MACRO()   Debug(aaa,  /* force line break */ \\\n"
5388                    "          {  int   i;  int  j;   })",
5389                    getGoogleStyle()));
5390 
5391   EXPECT_EQ("#define A                                       \\\n"
5392             "  [] {                                          \\\n"
5393             "    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(        \\\n"
5394             "        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
5395             "  }",
5396             format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
5397                    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
5398                    getGoogleStyle()));
5399 }
5400 
5401 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
5402   EXPECT_EQ("{}", format("{}"));
5403   verifyFormat("enum E {};");
5404   verifyFormat("enum E {}");
5405   FormatStyle Style = getLLVMStyle();
5406   Style.SpaceInEmptyBlock = true;
5407   EXPECT_EQ("void f() { }", format("void f() {}", Style));
5408   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty;
5409   EXPECT_EQ("while (true) { }", format("while (true) {}", Style));
5410   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
5411   Style.BraceWrapping.BeforeElse = false;
5412   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
5413   verifyFormat("if (a)\n"
5414                "{\n"
5415                "} else if (b)\n"
5416                "{\n"
5417                "} else\n"
5418                "{ }",
5419                Style);
5420   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never;
5421   verifyFormat("if (a) {\n"
5422                "} else if (b) {\n"
5423                "} else {\n"
5424                "}",
5425                Style);
5426   Style.BraceWrapping.BeforeElse = true;
5427   verifyFormat("if (a) { }\n"
5428                "else if (b) { }\n"
5429                "else { }",
5430                Style);
5431 }
5432 
5433 TEST_F(FormatTest, FormatBeginBlockEndMacros) {
5434   FormatStyle Style = getLLVMStyle();
5435   Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$";
5436   Style.MacroBlockEnd = "^[A-Z_]+_END$";
5437   verifyFormat("FOO_BEGIN\n"
5438                "  FOO_ENTRY\n"
5439                "FOO_END",
5440                Style);
5441   verifyFormat("FOO_BEGIN\n"
5442                "  NESTED_FOO_BEGIN\n"
5443                "    NESTED_FOO_ENTRY\n"
5444                "  NESTED_FOO_END\n"
5445                "FOO_END",
5446                Style);
5447   verifyFormat("FOO_BEGIN(Foo, Bar)\n"
5448                "  int x;\n"
5449                "  x = 1;\n"
5450                "FOO_END(Baz)",
5451                Style);
5452 }
5453 
5454 //===----------------------------------------------------------------------===//
5455 // Line break tests.
5456 //===----------------------------------------------------------------------===//
5457 
5458 TEST_F(FormatTest, PreventConfusingIndents) {
5459   verifyFormat(
5460       "void f() {\n"
5461       "  SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
5462       "                         parameter, parameter, parameter)),\n"
5463       "                     SecondLongCall(parameter));\n"
5464       "}");
5465   verifyFormat(
5466       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5467       "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
5468       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
5469       "    aaaaaaaaaaaaaaaaaaaaaaaa);");
5470   verifyFormat(
5471       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5472       "    [aaaaaaaaaaaaaaaaaaaaaaaa\n"
5473       "         [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
5474       "         [aaaaaaaaaaaaaaaaaaaaaaaa]];");
5475   verifyFormat(
5476       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
5477       "    aaaaaaaaaaaaaaaaaaaaaaaa<\n"
5478       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
5479       "    aaaaaaaaaaaaaaaaaaaaaaaa>;");
5480   verifyFormat("int a = bbbb && ccc &&\n"
5481                "        fffff(\n"
5482                "#define A Just forcing a new line\n"
5483                "            ddd);");
5484 }
5485 
5486 TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
5487   verifyFormat(
5488       "bool aaaaaaa =\n"
5489       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
5490       "    bbbbbbbb();");
5491   verifyFormat(
5492       "bool aaaaaaa =\n"
5493       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n"
5494       "    bbbbbbbb();");
5495 
5496   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
5497                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
5498                "    ccccccccc == ddddddddddd;");
5499   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
5500                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n"
5501                "    ccccccccc == ddddddddddd;");
5502   verifyFormat(
5503       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
5504       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n"
5505       "    ccccccccc == ddddddddddd;");
5506 
5507   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
5508                "                 aaaaaa) &&\n"
5509                "         bbbbbb && cccccc;");
5510   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
5511                "                 aaaaaa) >>\n"
5512                "         bbbbbb;");
5513   verifyFormat("aa = Whitespaces.addUntouchableComment(\n"
5514                "    SourceMgr.getSpellingColumnNumber(\n"
5515                "        TheLine.Last->FormatTok.Tok.getLocation()) -\n"
5516                "    1);");
5517 
5518   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
5519                "     bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
5520                "    cccccc) {\n}");
5521   verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
5522                "               bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n"
5523                "              cccccc) {\n}");
5524   verifyFormat("if CONSTEXPR ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
5525                "               bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n"
5526                "              cccccc) {\n}");
5527   verifyFormat("b = a &&\n"
5528                "    // Comment\n"
5529                "    b.c && d;");
5530 
5531   // If the LHS of a comparison is not a binary expression itself, the
5532   // additional linebreak confuses many people.
5533   verifyFormat(
5534       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5535       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
5536       "}");
5537   verifyFormat(
5538       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5539       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
5540       "}");
5541   verifyFormat(
5542       "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
5543       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
5544       "}");
5545   verifyFormat(
5546       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5547       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) <=> 5) {\n"
5548       "}");
5549   // Even explicit parentheses stress the precedence enough to make the
5550   // additional break unnecessary.
5551   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5552                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
5553                "}");
5554   // This cases is borderline, but with the indentation it is still readable.
5555   verifyFormat(
5556       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5557       "        aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5558       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
5559       "}",
5560       getLLVMStyleWithColumns(75));
5561 
5562   // If the LHS is a binary expression, we should still use the additional break
5563   // as otherwise the formatting hides the operator precedence.
5564   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5565                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
5566                "    5) {\n"
5567                "}");
5568   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5569                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa <=>\n"
5570                "    5) {\n"
5571                "}");
5572 
5573   FormatStyle OnePerLine = getLLVMStyle();
5574   OnePerLine.BinPackParameters = false;
5575   verifyFormat(
5576       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
5577       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
5578       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
5579       OnePerLine);
5580 
5581   verifyFormat("int i = someFunction(aaaaaaa, 0)\n"
5582                "                .aaa(aaaaaaaaaaaaa) *\n"
5583                "            aaaaaaa +\n"
5584                "        aaaaaaa;",
5585                getLLVMStyleWithColumns(40));
5586 }
5587 
5588 TEST_F(FormatTest, ExpressionIndentation) {
5589   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5590                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5591                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
5592                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
5593                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
5594                "                     bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
5595                "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
5596                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
5597                "                 ccccccccccccccccccccccccccccccccccccccccc;");
5598   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
5599                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5600                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
5601                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
5602   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5603                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
5604                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
5605                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
5606   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
5607                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
5608                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5609                "        bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
5610   verifyFormat("if () {\n"
5611                "} else if (aaaaa && bbbbb > // break\n"
5612                "                        ccccc) {\n"
5613                "}");
5614   verifyFormat("if () {\n"
5615                "} else if constexpr (aaaaa && bbbbb > // break\n"
5616                "                                  ccccc) {\n"
5617                "}");
5618   verifyFormat("if () {\n"
5619                "} else if CONSTEXPR (aaaaa && bbbbb > // break\n"
5620                "                                  ccccc) {\n"
5621                "}");
5622   verifyFormat("if () {\n"
5623                "} else if (aaaaa &&\n"
5624                "           bbbbb > // break\n"
5625                "               ccccc &&\n"
5626                "           ddddd) {\n"
5627                "}");
5628 
5629   // Presence of a trailing comment used to change indentation of b.
5630   verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
5631                "       b;\n"
5632                "return aaaaaaaaaaaaaaaaaaa +\n"
5633                "       b; //",
5634                getLLVMStyleWithColumns(30));
5635 }
5636 
5637 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
5638   // Not sure what the best system is here. Like this, the LHS can be found
5639   // immediately above an operator (everything with the same or a higher
5640   // indent). The RHS is aligned right of the operator and so compasses
5641   // everything until something with the same indent as the operator is found.
5642   // FIXME: Is this a good system?
5643   FormatStyle Style = getLLVMStyle();
5644   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
5645   verifyFormat(
5646       "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5647       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5648       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5649       "                 == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5650       "                            * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5651       "                        + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5652       "             && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5653       "                        * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5654       "                    > ccccccccccccccccccccccccccccccccccccccccc;",
5655       Style);
5656   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5657                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5658                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5659                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
5660                Style);
5661   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5662                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5663                "              * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5664                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
5665                Style);
5666   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5667                "    == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5668                "               * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5669                "           + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
5670                Style);
5671   verifyFormat("if () {\n"
5672                "} else if (aaaaa\n"
5673                "           && bbbbb // break\n"
5674                "                  > ccccc) {\n"
5675                "}",
5676                Style);
5677   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5678                "       && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
5679                Style);
5680   verifyFormat("return (a)\n"
5681                "       // comment\n"
5682                "       + b;",
5683                Style);
5684   verifyFormat(
5685       "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5686       "                 * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5687       "             + cc;",
5688       Style);
5689 
5690   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5691                "    = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
5692                Style);
5693 
5694   // Forced by comments.
5695   verifyFormat(
5696       "unsigned ContentSize =\n"
5697       "    sizeof(int16_t)   // DWARF ARange version number\n"
5698       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
5699       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
5700       "    + sizeof(int8_t); // Segment Size (in bytes)");
5701 
5702   verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
5703                "       == boost::fusion::at_c<1>(iiii).second;",
5704                Style);
5705 
5706   Style.ColumnLimit = 60;
5707   verifyFormat("zzzzzzzzzz\n"
5708                "    = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5709                "      >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
5710                Style);
5711 
5712   Style.ColumnLimit = 80;
5713   Style.IndentWidth = 4;
5714   Style.TabWidth = 4;
5715   Style.UseTab = FormatStyle::UT_Always;
5716   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
5717   Style.AlignOperands = FormatStyle::OAS_DontAlign;
5718   EXPECT_EQ("return someVeryVeryLongConditionThatBarelyFitsOnALine\n"
5719             "\t&& (someOtherLongishConditionPart1\n"
5720             "\t\t|| someOtherEvenLongerNestedConditionPart2);",
5721             format("return someVeryVeryLongConditionThatBarelyFitsOnALine && "
5722                    "(someOtherLongishConditionPart1 || "
5723                    "someOtherEvenLongerNestedConditionPart2);",
5724                    Style));
5725 }
5726 
5727 TEST_F(FormatTest, ExpressionIndentationStrictAlign) {
5728   FormatStyle Style = getLLVMStyle();
5729   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
5730   Style.AlignOperands = FormatStyle::OAS_AlignAfterOperator;
5731 
5732   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5733                "                   + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5734                "                   + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5735                "              == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5736                "                         * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5737                "                     + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5738                "          && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5739                "                     * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5740                "                 > ccccccccccccccccccccccccccccccccccccccccc;",
5741                Style);
5742   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5743                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5744                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5745                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
5746                Style);
5747   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5748                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5749                "              * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5750                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
5751                Style);
5752   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5753                "    == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5754                "               * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5755                "           + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
5756                Style);
5757   verifyFormat("if () {\n"
5758                "} else if (aaaaa\n"
5759                "           && bbbbb // break\n"
5760                "                  > ccccc) {\n"
5761                "}",
5762                Style);
5763   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5764                "    && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
5765                Style);
5766   verifyFormat("return (a)\n"
5767                "     // comment\n"
5768                "     + b;",
5769                Style);
5770   verifyFormat(
5771       "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5772       "               * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5773       "           + cc;",
5774       Style);
5775   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
5776                "     : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
5777                "                        : 3333333333333333;",
5778                Style);
5779   verifyFormat(
5780       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa    ? bbbbbbbbbbbbbbbbbb\n"
5781       "                           : ccccccccccccccc ? dddddddddddddddddd\n"
5782       "                                             : eeeeeeeeeeeeeeeeee)\n"
5783       "     : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
5784       "                        : 3333333333333333;",
5785       Style);
5786   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5787                "    = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
5788                Style);
5789 
5790   verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
5791                "    == boost::fusion::at_c<1>(iiii).second;",
5792                Style);
5793 
5794   Style.ColumnLimit = 60;
5795   verifyFormat("zzzzzzzzzzzzz\n"
5796                "    = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5797                "   >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
5798                Style);
5799 
5800   // Forced by comments.
5801   Style.ColumnLimit = 80;
5802   verifyFormat(
5803       "unsigned ContentSize\n"
5804       "    = sizeof(int16_t) // DWARF ARange version number\n"
5805       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
5806       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
5807       "    + sizeof(int8_t); // Segment Size (in bytes)",
5808       Style);
5809 
5810   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
5811   verifyFormat(
5812       "unsigned ContentSize =\n"
5813       "    sizeof(int16_t)   // DWARF ARange version number\n"
5814       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
5815       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
5816       "    + sizeof(int8_t); // Segment Size (in bytes)",
5817       Style);
5818 
5819   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
5820   verifyFormat(
5821       "unsigned ContentSize =\n"
5822       "    sizeof(int16_t)   // DWARF ARange version number\n"
5823       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
5824       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
5825       "    + sizeof(int8_t); // Segment Size (in bytes)",
5826       Style);
5827 }
5828 
5829 TEST_F(FormatTest, EnforcedOperatorWraps) {
5830   // Here we'd like to wrap after the || operators, but a comment is forcing an
5831   // earlier wrap.
5832   verifyFormat("bool x = aaaaa //\n"
5833                "         || bbbbb\n"
5834                "         //\n"
5835                "         || cccc;");
5836 }
5837 
5838 TEST_F(FormatTest, NoOperandAlignment) {
5839   FormatStyle Style = getLLVMStyle();
5840   Style.AlignOperands = FormatStyle::OAS_DontAlign;
5841   verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n"
5842                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5843                "                   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
5844                Style);
5845   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
5846   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5847                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5848                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5849                "        == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5850                "                * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5851                "            + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5852                "    && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5853                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5854                "        > ccccccccccccccccccccccccccccccccccccccccc;",
5855                Style);
5856 
5857   verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5858                "        * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5859                "    + cc;",
5860                Style);
5861   verifyFormat("int a = aa\n"
5862                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5863                "        * cccccccccccccccccccccccccccccccccccc;\n",
5864                Style);
5865 
5866   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
5867   verifyFormat("return (a > b\n"
5868                "    // comment1\n"
5869                "    // comment2\n"
5870                "    || c);",
5871                Style);
5872 }
5873 
5874 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) {
5875   FormatStyle Style = getLLVMStyle();
5876   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
5877   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5878                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5879                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
5880                Style);
5881 }
5882 
5883 TEST_F(FormatTest, AllowBinPackingInsideArguments) {
5884   FormatStyle Style = getLLVMStyle();
5885   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
5886   Style.BinPackArguments = false;
5887   Style.ColumnLimit = 40;
5888   verifyFormat("void test() {\n"
5889                "  someFunction(\n"
5890                "      this + argument + is + quite\n"
5891                "      + long + so + it + gets + wrapped\n"
5892                "      + but + remains + bin - packed);\n"
5893                "}",
5894                Style);
5895   verifyFormat("void test() {\n"
5896                "  someFunction(arg1,\n"
5897                "               this + argument + is\n"
5898                "                   + quite + long + so\n"
5899                "                   + it + gets + wrapped\n"
5900                "                   + but + remains + bin\n"
5901                "                   - packed,\n"
5902                "               arg3);\n"
5903                "}",
5904                Style);
5905   verifyFormat("void test() {\n"
5906                "  someFunction(\n"
5907                "      arg1,\n"
5908                "      this + argument + has\n"
5909                "          + anotherFunc(nested,\n"
5910                "                        calls + whose\n"
5911                "                            + arguments\n"
5912                "                            + are + also\n"
5913                "                            + wrapped,\n"
5914                "                        in + addition)\n"
5915                "          + to + being + bin - packed,\n"
5916                "      arg3);\n"
5917                "}",
5918                Style);
5919 
5920   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
5921   verifyFormat("void test() {\n"
5922                "  someFunction(\n"
5923                "      arg1,\n"
5924                "      this + argument + has +\n"
5925                "          anotherFunc(nested,\n"
5926                "                      calls + whose +\n"
5927                "                          arguments +\n"
5928                "                          are + also +\n"
5929                "                          wrapped,\n"
5930                "                      in + addition) +\n"
5931                "          to + being + bin - packed,\n"
5932                "      arg3);\n"
5933                "}",
5934                Style);
5935 }
5936 
5937 TEST_F(FormatTest, ConstructorInitializers) {
5938   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
5939   verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
5940                getLLVMStyleWithColumns(45));
5941   verifyFormat("Constructor()\n"
5942                "    : Inttializer(FitsOnTheLine) {}",
5943                getLLVMStyleWithColumns(44));
5944   verifyFormat("Constructor()\n"
5945                "    : Inttializer(FitsOnTheLine) {}",
5946                getLLVMStyleWithColumns(43));
5947 
5948   verifyFormat("template <typename T>\n"
5949                "Constructor() : Initializer(FitsOnTheLine) {}",
5950                getLLVMStyleWithColumns(45));
5951 
5952   verifyFormat(
5953       "SomeClass::Constructor()\n"
5954       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
5955 
5956   verifyFormat(
5957       "SomeClass::Constructor()\n"
5958       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
5959       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
5960   verifyFormat(
5961       "SomeClass::Constructor()\n"
5962       "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
5963       "      aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
5964   verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5965                "            aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5966                "    : aaaaaaaaaa(aaaaaa) {}");
5967 
5968   verifyFormat("Constructor()\n"
5969                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
5970                "      aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5971                "                               aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
5972                "      aaaaaaaaaaaaaaaaaaaaaaa() {}");
5973 
5974   verifyFormat("Constructor()\n"
5975                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5976                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
5977 
5978   verifyFormat("Constructor(int Parameter = 0)\n"
5979                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
5980                "      aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
5981   verifyFormat("Constructor()\n"
5982                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
5983                "}",
5984                getLLVMStyleWithColumns(60));
5985   verifyFormat("Constructor()\n"
5986                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5987                "          aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}");
5988 
5989   // Here a line could be saved by splitting the second initializer onto two
5990   // lines, but that is not desirable.
5991   verifyFormat("Constructor()\n"
5992                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
5993                "      aaaaaaaaaaa(aaaaaaaaaaa),\n"
5994                "      aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
5995 
5996   FormatStyle OnePerLine = getLLVMStyle();
5997   OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_Never;
5998   verifyFormat("MyClass::MyClass()\n"
5999                "    : a(a),\n"
6000                "      b(b),\n"
6001                "      c(c) {}",
6002                OnePerLine);
6003   verifyFormat("MyClass::MyClass()\n"
6004                "    : a(a), // comment\n"
6005                "      b(b),\n"
6006                "      c(c) {}",
6007                OnePerLine);
6008   verifyFormat("MyClass::MyClass(int a)\n"
6009                "    : b(a),      // comment\n"
6010                "      c(a + 1) { // lined up\n"
6011                "}",
6012                OnePerLine);
6013   verifyFormat("Constructor()\n"
6014                "    : a(b, b, b) {}",
6015                OnePerLine);
6016   OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6017   OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false;
6018   verifyFormat("SomeClass::Constructor()\n"
6019                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6020                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6021                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
6022                OnePerLine);
6023   verifyFormat("SomeClass::Constructor()\n"
6024                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
6025                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6026                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
6027                OnePerLine);
6028   verifyFormat("MyClass::MyClass(int var)\n"
6029                "    : some_var_(var),            // 4 space indent\n"
6030                "      some_other_var_(var + 1) { // lined up\n"
6031                "}",
6032                OnePerLine);
6033   verifyFormat("Constructor()\n"
6034                "    : aaaaa(aaaaaa),\n"
6035                "      aaaaa(aaaaaa),\n"
6036                "      aaaaa(aaaaaa),\n"
6037                "      aaaaa(aaaaaa),\n"
6038                "      aaaaa(aaaaaa) {}",
6039                OnePerLine);
6040   verifyFormat("Constructor()\n"
6041                "    : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
6042                "            aaaaaaaaaaaaaaaaaaaaaa) {}",
6043                OnePerLine);
6044   OnePerLine.BinPackParameters = false;
6045   verifyFormat(
6046       "Constructor()\n"
6047       "    : aaaaaaaaaaaaaaaaaaaaaaaa(\n"
6048       "          aaaaaaaaaaa().aaa(),\n"
6049       "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
6050       OnePerLine);
6051   OnePerLine.ColumnLimit = 60;
6052   verifyFormat("Constructor()\n"
6053                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
6054                "      bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
6055                OnePerLine);
6056 
6057   EXPECT_EQ("Constructor()\n"
6058             "    : // Comment forcing unwanted break.\n"
6059             "      aaaa(aaaa) {}",
6060             format("Constructor() :\n"
6061                    "    // Comment forcing unwanted break.\n"
6062                    "    aaaa(aaaa) {}"));
6063 }
6064 
6065 TEST_F(FormatTest, AllowAllConstructorInitializersOnNextLine) {
6066   FormatStyle Style = getLLVMStyle();
6067   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
6068   Style.ColumnLimit = 60;
6069   Style.BinPackParameters = false;
6070 
6071   for (int i = 0; i < 4; ++i) {
6072     // Test all combinations of parameters that should not have an effect.
6073     Style.AllowAllParametersOfDeclarationOnNextLine = i & 1;
6074     Style.AllowAllArgumentsOnNextLine = i & 2;
6075 
6076     Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6077     Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
6078     verifyFormat("Constructor()\n"
6079                  "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6080                  Style);
6081     verifyFormat("Constructor() : a(a), b(b) {}", Style);
6082 
6083     Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
6084     verifyFormat("Constructor()\n"
6085                  "    : aaaaaaaaaaaaaaaaaaaa(a)\n"
6086                  "    , bbbbbbbbbbbbbbbbbbbbb(b) {}",
6087                  Style);
6088     verifyFormat("Constructor() : a(a), b(b) {}", Style);
6089 
6090     Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
6091     Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6092     verifyFormat("Constructor()\n"
6093                  "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6094                  Style);
6095 
6096     Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
6097     verifyFormat("Constructor()\n"
6098                  "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
6099                  "      bbbbbbbbbbbbbbbbbbbbb(b) {}",
6100                  Style);
6101 
6102     Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
6103     Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6104     verifyFormat("Constructor() :\n"
6105                  "    aaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6106                  Style);
6107 
6108     Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
6109     verifyFormat("Constructor() :\n"
6110                  "    aaaaaaaaaaaaaaaaaa(a),\n"
6111                  "    bbbbbbbbbbbbbbbbbbbbb(b) {}",
6112                  Style);
6113   }
6114 
6115   // Test interactions between AllowAllParametersOfDeclarationOnNextLine and
6116   // AllowAllConstructorInitializersOnNextLine in all
6117   // BreakConstructorInitializers modes
6118   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
6119   Style.AllowAllParametersOfDeclarationOnNextLine = true;
6120   verifyFormat("SomeClassWithALongName::Constructor(\n"
6121                "    int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n"
6122                "    : aaaaaaaaaaaaaaaaaaaa(a)\n"
6123                "    , bbbbbbbbbbbbbbbbbbbbb(b) {}",
6124                Style);
6125 
6126   Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6127   verifyFormat("SomeClassWithALongName::Constructor(\n"
6128                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6129                "    int bbbbbbbbbbbbb,\n"
6130                "    int cccccccccccccccc)\n"
6131                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6132                Style);
6133 
6134   Style.AllowAllParametersOfDeclarationOnNextLine = false;
6135   Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
6136   verifyFormat("SomeClassWithALongName::Constructor(\n"
6137                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6138                "    int bbbbbbbbbbbbb)\n"
6139                "    : aaaaaaaaaaaaaaaaaaaa(a)\n"
6140                "    , bbbbbbbbbbbbbbbbbbbbb(b) {}",
6141                Style);
6142 
6143   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
6144 
6145   Style.AllowAllParametersOfDeclarationOnNextLine = true;
6146   verifyFormat("SomeClassWithALongName::Constructor(\n"
6147                "    int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n"
6148                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
6149                "      bbbbbbbbbbbbbbbbbbbbb(b) {}",
6150                Style);
6151 
6152   Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6153   verifyFormat("SomeClassWithALongName::Constructor(\n"
6154                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6155                "    int bbbbbbbbbbbbb,\n"
6156                "    int cccccccccccccccc)\n"
6157                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6158                Style);
6159 
6160   Style.AllowAllParametersOfDeclarationOnNextLine = false;
6161   Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
6162   verifyFormat("SomeClassWithALongName::Constructor(\n"
6163                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6164                "    int bbbbbbbbbbbbb)\n"
6165                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
6166                "      bbbbbbbbbbbbbbbbbbbbb(b) {}",
6167                Style);
6168 
6169   Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
6170   Style.AllowAllParametersOfDeclarationOnNextLine = true;
6171   verifyFormat("SomeClassWithALongName::Constructor(\n"
6172                "    int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb) :\n"
6173                "    aaaaaaaaaaaaaaaaaaaa(a),\n"
6174                "    bbbbbbbbbbbbbbbbbbbbb(b) {}",
6175                Style);
6176 
6177   Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6178   verifyFormat("SomeClassWithALongName::Constructor(\n"
6179                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6180                "    int bbbbbbbbbbbbb,\n"
6181                "    int cccccccccccccccc) :\n"
6182                "    aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6183                Style);
6184 
6185   Style.AllowAllParametersOfDeclarationOnNextLine = false;
6186   Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
6187   verifyFormat("SomeClassWithALongName::Constructor(\n"
6188                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6189                "    int bbbbbbbbbbbbb) :\n"
6190                "    aaaaaaaaaaaaaaaaaaaa(a),\n"
6191                "    bbbbbbbbbbbbbbbbbbbbb(b) {}",
6192                Style);
6193 }
6194 
6195 TEST_F(FormatTest, AllowAllArgumentsOnNextLine) {
6196   FormatStyle Style = getLLVMStyle();
6197   Style.ColumnLimit = 60;
6198   Style.BinPackArguments = false;
6199   for (int i = 0; i < 4; ++i) {
6200     // Test all combinations of parameters that should not have an effect.
6201     Style.AllowAllParametersOfDeclarationOnNextLine = i & 1;
6202     Style.PackConstructorInitializers =
6203         i & 2 ? FormatStyle::PCIS_BinPack : FormatStyle::PCIS_Never;
6204 
6205     Style.AllowAllArgumentsOnNextLine = true;
6206     verifyFormat("void foo() {\n"
6207                  "  FunctionCallWithReallyLongName(\n"
6208                  "      aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb);\n"
6209                  "}",
6210                  Style);
6211     Style.AllowAllArgumentsOnNextLine = false;
6212     verifyFormat("void foo() {\n"
6213                  "  FunctionCallWithReallyLongName(\n"
6214                  "      aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6215                  "      bbbbbbbbbbbb);\n"
6216                  "}",
6217                  Style);
6218 
6219     Style.AllowAllArgumentsOnNextLine = true;
6220     verifyFormat("void foo() {\n"
6221                  "  auto VariableWithReallyLongName = {\n"
6222                  "      aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb};\n"
6223                  "}",
6224                  Style);
6225     Style.AllowAllArgumentsOnNextLine = false;
6226     verifyFormat("void foo() {\n"
6227                  "  auto VariableWithReallyLongName = {\n"
6228                  "      aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6229                  "      bbbbbbbbbbbb};\n"
6230                  "}",
6231                  Style);
6232   }
6233 
6234   // This parameter should not affect declarations.
6235   Style.BinPackParameters = false;
6236   Style.AllowAllArgumentsOnNextLine = false;
6237   Style.AllowAllParametersOfDeclarationOnNextLine = true;
6238   verifyFormat("void FunctionCallWithReallyLongName(\n"
6239                "    int aaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbb);",
6240                Style);
6241   Style.AllowAllParametersOfDeclarationOnNextLine = false;
6242   verifyFormat("void FunctionCallWithReallyLongName(\n"
6243                "    int aaaaaaaaaaaaaaaaaaaaaaa,\n"
6244                "    int bbbbbbbbbbbb);",
6245                Style);
6246 }
6247 
6248 TEST_F(FormatTest, AllowAllArgumentsOnNextLineDontAlign) {
6249   // Check that AllowAllArgumentsOnNextLine is respected for both BAS_DontAlign
6250   // and BAS_Align.
6251   auto Style = getLLVMStyle();
6252   Style.ColumnLimit = 35;
6253   StringRef Input = "functionCall(paramA, paramB, paramC);\n"
6254                     "void functionDecl(int A, int B, int C);";
6255   Style.AllowAllArgumentsOnNextLine = false;
6256   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
6257   EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n"
6258                       "    paramC);\n"
6259                       "void functionDecl(int A, int B,\n"
6260                       "    int C);"),
6261             format(Input, Style));
6262   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
6263   EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n"
6264                       "             paramC);\n"
6265                       "void functionDecl(int A, int B,\n"
6266                       "                  int C);"),
6267             format(Input, Style));
6268   // However, BAS_AlwaysBreak should take precedence over
6269   // AllowAllArgumentsOnNextLine.
6270   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
6271   EXPECT_EQ(StringRef("functionCall(\n"
6272                       "    paramA, paramB, paramC);\n"
6273                       "void functionDecl(\n"
6274                       "    int A, int B, int C);"),
6275             format(Input, Style));
6276 
6277   // When AllowAllArgumentsOnNextLine is set, we prefer breaking before the
6278   // first argument.
6279   Style.AllowAllArgumentsOnNextLine = true;
6280   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
6281   EXPECT_EQ(StringRef("functionCall(\n"
6282                       "    paramA, paramB, paramC);\n"
6283                       "void functionDecl(\n"
6284                       "    int A, int B, int C);"),
6285             format(Input, Style));
6286   // It wouldn't fit on one line with aligned parameters so this setting
6287   // doesn't change anything for BAS_Align.
6288   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
6289   EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n"
6290                       "             paramC);\n"
6291                       "void functionDecl(int A, int B,\n"
6292                       "                  int C);"),
6293             format(Input, Style));
6294   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
6295   EXPECT_EQ(StringRef("functionCall(\n"
6296                       "    paramA, paramB, paramC);\n"
6297                       "void functionDecl(\n"
6298                       "    int A, int B, int C);"),
6299             format(Input, Style));
6300 }
6301 
6302 TEST_F(FormatTest, BreakConstructorInitializersAfterColon) {
6303   FormatStyle Style = getLLVMStyle();
6304   Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
6305 
6306   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
6307   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}",
6308                getStyleWithColumns(Style, 45));
6309   verifyFormat("Constructor() :\n"
6310                "    Initializer(FitsOnTheLine) {}",
6311                getStyleWithColumns(Style, 44));
6312   verifyFormat("Constructor() :\n"
6313                "    Initializer(FitsOnTheLine) {}",
6314                getStyleWithColumns(Style, 43));
6315 
6316   verifyFormat("template <typename T>\n"
6317                "Constructor() : Initializer(FitsOnTheLine) {}",
6318                getStyleWithColumns(Style, 50));
6319   Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6320   verifyFormat(
6321       "SomeClass::Constructor() :\n"
6322       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
6323       Style);
6324 
6325   Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack;
6326   verifyFormat(
6327       "SomeClass::Constructor() :\n"
6328       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
6329       Style);
6330 
6331   verifyFormat(
6332       "SomeClass::Constructor() :\n"
6333       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6334       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
6335       Style);
6336   verifyFormat(
6337       "SomeClass::Constructor() :\n"
6338       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6339       "    aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
6340       Style);
6341   verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6342                "            aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
6343                "    aaaaaaaaaa(aaaaaa) {}",
6344                Style);
6345 
6346   verifyFormat("Constructor() :\n"
6347                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6348                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6349                "                             aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6350                "    aaaaaaaaaaaaaaaaaaaaaaa() {}",
6351                Style);
6352 
6353   verifyFormat("Constructor() :\n"
6354                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6355                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
6356                Style);
6357 
6358   verifyFormat("Constructor(int Parameter = 0) :\n"
6359                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
6360                "    aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}",
6361                Style);
6362   verifyFormat("Constructor() :\n"
6363                "    aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
6364                "}",
6365                getStyleWithColumns(Style, 60));
6366   verifyFormat("Constructor() :\n"
6367                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6368                "        aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}",
6369                Style);
6370 
6371   // Here a line could be saved by splitting the second initializer onto two
6372   // lines, but that is not desirable.
6373   verifyFormat("Constructor() :\n"
6374                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
6375                "    aaaaaaaaaaa(aaaaaaaaaaa),\n"
6376                "    aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
6377                Style);
6378 
6379   FormatStyle OnePerLine = Style;
6380   OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
6381   verifyFormat("SomeClass::Constructor() :\n"
6382                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6383                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6384                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
6385                OnePerLine);
6386   verifyFormat("SomeClass::Constructor() :\n"
6387                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
6388                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6389                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
6390                OnePerLine);
6391   verifyFormat("MyClass::MyClass(int var) :\n"
6392                "    some_var_(var),            // 4 space indent\n"
6393                "    some_other_var_(var + 1) { // lined up\n"
6394                "}",
6395                OnePerLine);
6396   verifyFormat("Constructor() :\n"
6397                "    aaaaa(aaaaaa),\n"
6398                "    aaaaa(aaaaaa),\n"
6399                "    aaaaa(aaaaaa),\n"
6400                "    aaaaa(aaaaaa),\n"
6401                "    aaaaa(aaaaaa) {}",
6402                OnePerLine);
6403   verifyFormat("Constructor() :\n"
6404                "    aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
6405                "          aaaaaaaaaaaaaaaaaaaaaa) {}",
6406                OnePerLine);
6407   OnePerLine.BinPackParameters = false;
6408   verifyFormat("Constructor() :\n"
6409                "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
6410                "        aaaaaaaaaaa().aaa(),\n"
6411                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
6412                OnePerLine);
6413   OnePerLine.ColumnLimit = 60;
6414   verifyFormat("Constructor() :\n"
6415                "    aaaaaaaaaaaaaaaaaaaa(a),\n"
6416                "    bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
6417                OnePerLine);
6418 
6419   EXPECT_EQ("Constructor() :\n"
6420             "    // Comment forcing unwanted break.\n"
6421             "    aaaa(aaaa) {}",
6422             format("Constructor() :\n"
6423                    "    // Comment forcing unwanted break.\n"
6424                    "    aaaa(aaaa) {}",
6425                    Style));
6426 
6427   Style.ColumnLimit = 0;
6428   verifyFormat("SomeClass::Constructor() :\n"
6429                "    a(a) {}",
6430                Style);
6431   verifyFormat("SomeClass::Constructor() noexcept :\n"
6432                "    a(a) {}",
6433                Style);
6434   verifyFormat("SomeClass::Constructor() :\n"
6435                "    a(a), b(b), c(c) {}",
6436                Style);
6437   verifyFormat("SomeClass::Constructor() :\n"
6438                "    a(a) {\n"
6439                "  foo();\n"
6440                "  bar();\n"
6441                "}",
6442                Style);
6443 
6444   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
6445   verifyFormat("SomeClass::Constructor() :\n"
6446                "    a(a), b(b), c(c) {\n"
6447                "}",
6448                Style);
6449   verifyFormat("SomeClass::Constructor() :\n"
6450                "    a(a) {\n"
6451                "}",
6452                Style);
6453 
6454   Style.ColumnLimit = 80;
6455   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
6456   Style.ConstructorInitializerIndentWidth = 2;
6457   verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}", Style);
6458   verifyFormat("SomeClass::Constructor() :\n"
6459                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6460                "  bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}",
6461                Style);
6462 
6463   // `ConstructorInitializerIndentWidth` actually applies to InheritanceList as
6464   // well
6465   Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
6466   verifyFormat(
6467       "class SomeClass\n"
6468       "  : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6469       "    public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
6470       Style);
6471   Style.BreakInheritanceList = FormatStyle::BILS_BeforeComma;
6472   verifyFormat(
6473       "class SomeClass\n"
6474       "  : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6475       "  , public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
6476       Style);
6477   Style.BreakInheritanceList = FormatStyle::BILS_AfterColon;
6478   verifyFormat(
6479       "class SomeClass :\n"
6480       "  public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6481       "  public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
6482       Style);
6483   Style.BreakInheritanceList = FormatStyle::BILS_AfterComma;
6484   verifyFormat(
6485       "class SomeClass\n"
6486       "  : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6487       "    public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
6488       Style);
6489 }
6490 
6491 #ifndef EXPENSIVE_CHECKS
6492 // Expensive checks enables libstdc++ checking which includes validating the
6493 // state of ranges used in std::priority_queue - this blows out the
6494 // runtime/scalability of the function and makes this test unacceptably slow.
6495 TEST_F(FormatTest, MemoizationTests) {
6496   // This breaks if the memoization lookup does not take \c Indent and
6497   // \c LastSpace into account.
6498   verifyFormat(
6499       "extern CFRunLoopTimerRef\n"
6500       "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
6501       "                     CFTimeInterval interval, CFOptionFlags flags,\n"
6502       "                     CFIndex order, CFRunLoopTimerCallBack callout,\n"
6503       "                     CFRunLoopTimerContext *context) {}");
6504 
6505   // Deep nesting somewhat works around our memoization.
6506   verifyFormat(
6507       "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
6508       "    aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
6509       "        aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
6510       "            aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
6511       "                aaaaa())))))))))))))))))))))))))))))))))))))));",
6512       getLLVMStyleWithColumns(65));
6513   verifyFormat(
6514       "aaaaa(\n"
6515       "    aaaaa,\n"
6516       "    aaaaa(\n"
6517       "        aaaaa,\n"
6518       "        aaaaa(\n"
6519       "            aaaaa,\n"
6520       "            aaaaa(\n"
6521       "                aaaaa,\n"
6522       "                aaaaa(\n"
6523       "                    aaaaa,\n"
6524       "                    aaaaa(\n"
6525       "                        aaaaa,\n"
6526       "                        aaaaa(\n"
6527       "                            aaaaa,\n"
6528       "                            aaaaa(\n"
6529       "                                aaaaa,\n"
6530       "                                aaaaa(\n"
6531       "                                    aaaaa,\n"
6532       "                                    aaaaa(\n"
6533       "                                        aaaaa,\n"
6534       "                                        aaaaa(\n"
6535       "                                            aaaaa,\n"
6536       "                                            aaaaa(\n"
6537       "                                                aaaaa,\n"
6538       "                                                aaaaa))))))))))));",
6539       getLLVMStyleWithColumns(65));
6540   verifyFormat(
6541       "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"
6542       "                                  a),\n"
6543       "                                a),\n"
6544       "                              a),\n"
6545       "                            a),\n"
6546       "                          a),\n"
6547       "                        a),\n"
6548       "                      a),\n"
6549       "                    a),\n"
6550       "                  a),\n"
6551       "                a),\n"
6552       "              a),\n"
6553       "            a),\n"
6554       "          a),\n"
6555       "        a),\n"
6556       "      a),\n"
6557       "    a),\n"
6558       "  a)",
6559       getLLVMStyleWithColumns(65));
6560 
6561   // This test takes VERY long when memoization is broken.
6562   FormatStyle OnePerLine = getLLVMStyle();
6563   OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6564   OnePerLine.BinPackParameters = false;
6565   std::string input = "Constructor()\n"
6566                       "    : aaaa(a,\n";
6567   for (unsigned i = 0, e = 80; i != e; ++i) {
6568     input += "           a,\n";
6569   }
6570   input += "           a) {}";
6571   verifyFormat(input, OnePerLine);
6572 }
6573 #endif
6574 
6575 TEST_F(FormatTest, BreaksAsHighAsPossible) {
6576   verifyFormat(
6577       "void f() {\n"
6578       "  if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
6579       "      (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
6580       "    f();\n"
6581       "}");
6582   verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
6583                "    Intervals[i - 1].getRange().getLast()) {\n}");
6584 }
6585 
6586 TEST_F(FormatTest, BreaksFunctionDeclarations) {
6587   // Principially, we break function declarations in a certain order:
6588   // 1) break amongst arguments.
6589   verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
6590                "                              Cccccccccccccc cccccccccccccc);");
6591   verifyFormat("template <class TemplateIt>\n"
6592                "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
6593                "                            TemplateIt *stop) {}");
6594 
6595   // 2) break after return type.
6596   verifyFormat(
6597       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6598       "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);",
6599       getGoogleStyle());
6600 
6601   // 3) break after (.
6602   verifyFormat(
6603       "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
6604       "    Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);",
6605       getGoogleStyle());
6606 
6607   // 4) break before after nested name specifiers.
6608   verifyFormat(
6609       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6610       "SomeClasssssssssssssssssssssssssssssssssssssss::\n"
6611       "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);",
6612       getGoogleStyle());
6613 
6614   // However, there are exceptions, if a sufficient amount of lines can be
6615   // saved.
6616   // FIXME: The precise cut-offs wrt. the number of saved lines might need some
6617   // more adjusting.
6618   verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
6619                "                                  Cccccccccccccc cccccccccc,\n"
6620                "                                  Cccccccccccccc cccccccccc,\n"
6621                "                                  Cccccccccccccc cccccccccc,\n"
6622                "                                  Cccccccccccccc cccccccccc);");
6623   verifyFormat(
6624       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6625       "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
6626       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
6627       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);",
6628       getGoogleStyle());
6629   verifyFormat(
6630       "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
6631       "                                          Cccccccccccccc cccccccccc,\n"
6632       "                                          Cccccccccccccc cccccccccc,\n"
6633       "                                          Cccccccccccccc cccccccccc,\n"
6634       "                                          Cccccccccccccc cccccccccc,\n"
6635       "                                          Cccccccccccccc cccccccccc,\n"
6636       "                                          Cccccccccccccc cccccccccc);");
6637   verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
6638                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
6639                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
6640                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
6641                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
6642 
6643   // Break after multi-line parameters.
6644   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6645                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6646                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6647                "    bbbb bbbb);");
6648   verifyFormat("void SomeLoooooooooooongFunction(\n"
6649                "    std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
6650                "        aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6651                "    int bbbbbbbbbbbbb);");
6652 
6653   // Treat overloaded operators like other functions.
6654   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
6655                "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
6656   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
6657                "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
6658   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
6659                "operator<<(const SomeLooooooooooooooooooooooooogType &other);");
6660   verifyGoogleFormat(
6661       "SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
6662       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
6663   verifyGoogleFormat(
6664       "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
6665       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
6666   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6667                "    int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
6668   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
6669                "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
6670   verifyGoogleFormat(
6671       "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n"
6672       "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6673       "    bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}");
6674   verifyGoogleFormat("template <typename T>\n"
6675                      "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6676                      "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n"
6677                      "    aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);");
6678 
6679   FormatStyle Style = getLLVMStyle();
6680   Style.PointerAlignment = FormatStyle::PAS_Left;
6681   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6682                "    aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}",
6683                Style);
6684   verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
6685                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
6686                Style);
6687 }
6688 
6689 TEST_F(FormatTest, DontBreakBeforeQualifiedOperator) {
6690   // Regression test for https://bugs.llvm.org/show_bug.cgi?id=40516:
6691   // Prefer keeping `::` followed by `operator` together.
6692   EXPECT_EQ("const aaaa::bbbbbbb &\n"
6693             "ccccccccc::operator++() {\n"
6694             "  stuff();\n"
6695             "}",
6696             format("const aaaa::bbbbbbb\n"
6697                    "&ccccccccc::operator++() { stuff(); }",
6698                    getLLVMStyleWithColumns(40)));
6699 }
6700 
6701 TEST_F(FormatTest, TrailingReturnType) {
6702   verifyFormat("auto foo() -> int;\n");
6703   // correct trailing return type spacing
6704   verifyFormat("auto operator->() -> int;\n");
6705   verifyFormat("auto operator++(int) -> int;\n");
6706 
6707   verifyFormat("struct S {\n"
6708                "  auto bar() const -> int;\n"
6709                "};");
6710   verifyFormat("template <size_t Order, typename T>\n"
6711                "auto load_img(const std::string &filename)\n"
6712                "    -> alias::tensor<Order, T, mem::tag::cpu> {}");
6713   verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n"
6714                "    -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}");
6715   verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}");
6716   verifyFormat("template <typename T>\n"
6717                "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n"
6718                "    -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());");
6719 
6720   // Not trailing return types.
6721   verifyFormat("void f() { auto a = b->c(); }");
6722 }
6723 
6724 TEST_F(FormatTest, DeductionGuides) {
6725   verifyFormat("template <class T> A(const T &, const T &) -> A<T &>;");
6726   verifyFormat("template <class T> explicit A(T &, T &&) -> A<T>;");
6727   verifyFormat("template <class... Ts> S(Ts...) -> S<Ts...>;");
6728   verifyFormat(
6729       "template <class... T>\n"
6730       "array(T &&...t) -> array<std::common_type_t<T...>, sizeof...(T)>;");
6731   verifyFormat("template <class T> A() -> A<decltype(p->foo<3>())>;");
6732   verifyFormat("template <class T> A() -> A<decltype(foo<traits<1>>)>;");
6733   verifyFormat("template <class T> A() -> A<sizeof(p->foo<1>)>;");
6734   verifyFormat("template <class T> A() -> A<(3 < 2)>;");
6735   verifyFormat("template <class T> A() -> A<((3) < (2))>;");
6736   verifyFormat("template <class T> x() -> x<1>;");
6737   verifyFormat("template <class T> explicit x(T &) -> x<1>;");
6738 
6739   // Ensure not deduction guides.
6740   verifyFormat("c()->f<int>();");
6741   verifyFormat("x()->foo<1>;");
6742   verifyFormat("x = p->foo<3>();");
6743   verifyFormat("x()->x<1>();");
6744   verifyFormat("x()->x<1>;");
6745 }
6746 
6747 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
6748   // Avoid breaking before trailing 'const' or other trailing annotations, if
6749   // they are not function-like.
6750   FormatStyle Style = getGoogleStyle();
6751   Style.ColumnLimit = 47;
6752   verifyFormat("void someLongFunction(\n"
6753                "    int someLoooooooooooooongParameter) const {\n}",
6754                getLLVMStyleWithColumns(47));
6755   verifyFormat("LoooooongReturnType\n"
6756                "someLoooooooongFunction() const {}",
6757                getLLVMStyleWithColumns(47));
6758   verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
6759                "    const {}",
6760                Style);
6761   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
6762                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
6763   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
6764                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
6765   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
6766                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
6767   verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
6768                "                   aaaaaaaaaaa aaaaa) const override;");
6769   verifyGoogleFormat(
6770       "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
6771       "    const override;");
6772 
6773   // Even if the first parameter has to be wrapped.
6774   verifyFormat("void someLongFunction(\n"
6775                "    int someLongParameter) const {}",
6776                getLLVMStyleWithColumns(46));
6777   verifyFormat("void someLongFunction(\n"
6778                "    int someLongParameter) const {}",
6779                Style);
6780   verifyFormat("void someLongFunction(\n"
6781                "    int someLongParameter) override {}",
6782                Style);
6783   verifyFormat("void someLongFunction(\n"
6784                "    int someLongParameter) OVERRIDE {}",
6785                Style);
6786   verifyFormat("void someLongFunction(\n"
6787                "    int someLongParameter) final {}",
6788                Style);
6789   verifyFormat("void someLongFunction(\n"
6790                "    int someLongParameter) FINAL {}",
6791                Style);
6792   verifyFormat("void someLongFunction(\n"
6793                "    int parameter) const override {}",
6794                Style);
6795 
6796   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
6797   verifyFormat("void someLongFunction(\n"
6798                "    int someLongParameter) const\n"
6799                "{\n"
6800                "}",
6801                Style);
6802 
6803   Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
6804   verifyFormat("void someLongFunction(\n"
6805                "    int someLongParameter) const\n"
6806                "  {\n"
6807                "  }",
6808                Style);
6809 
6810   // Unless these are unknown annotations.
6811   verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
6812                "                  aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
6813                "    LONG_AND_UGLY_ANNOTATION;");
6814 
6815   // Breaking before function-like trailing annotations is fine to keep them
6816   // close to their arguments.
6817   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
6818                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
6819   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
6820                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
6821   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
6822                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
6823   verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n"
6824                      "    AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);");
6825   verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});");
6826 
6827   verifyFormat(
6828       "void aaaaaaaaaaaaaaaaaa()\n"
6829       "    __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
6830       "                   aaaaaaaaaaaaaaaaaaaaaaaaa));");
6831   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6832                "    __attribute__((unused));");
6833   verifyGoogleFormat(
6834       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6835       "    GUARDED_BY(aaaaaaaaaaaa);");
6836   verifyGoogleFormat(
6837       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6838       "    GUARDED_BY(aaaaaaaaaaaa);");
6839   verifyGoogleFormat(
6840       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
6841       "    aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
6842   verifyGoogleFormat(
6843       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
6844       "    aaaaaaaaaaaaaaaaaaaaaaaaa;");
6845 }
6846 
6847 TEST_F(FormatTest, FunctionAnnotations) {
6848   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
6849                "int OldFunction(const string &parameter) {}");
6850   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
6851                "string OldFunction(const string &parameter) {}");
6852   verifyFormat("template <typename T>\n"
6853                "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
6854                "string OldFunction(const string &parameter) {}");
6855 
6856   // Not function annotations.
6857   verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6858                "                << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
6859   verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n"
6860                "       ThisIsATestWithAReallyReallyReallyReallyLongName) {}");
6861   verifyFormat("MACRO(abc).function() // wrap\n"
6862                "    << abc;");
6863   verifyFormat("MACRO(abc)->function() // wrap\n"
6864                "    << abc;");
6865   verifyFormat("MACRO(abc)::function() // wrap\n"
6866                "    << abc;");
6867 }
6868 
6869 TEST_F(FormatTest, BreaksDesireably) {
6870   verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
6871                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
6872                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
6873   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6874                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
6875                "}");
6876 
6877   verifyFormat(
6878       "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6879       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
6880 
6881   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6882                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6883                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
6884 
6885   verifyFormat(
6886       "aaaaaaaa(aaaaaaaaaaaaa,\n"
6887       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6888       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
6889       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6890       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
6891 
6892   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
6893                "    (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6894 
6895   verifyFormat(
6896       "void f() {\n"
6897       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
6898       "                                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
6899       "}");
6900   verifyFormat(
6901       "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6902       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
6903   verifyFormat(
6904       "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6905       "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
6906   verifyFormat(
6907       "aaaaaa(aaa,\n"
6908       "       new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6909       "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6910       "       aaaa);");
6911   verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
6912                "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6913                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6914 
6915   // Indent consistently independent of call expression and unary operator.
6916   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
6917                "    dddddddddddddddddddddddddddddd));");
6918   verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
6919                "    dddddddddddddddddddddddddddddd));");
6920   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
6921                "    dddddddddddddddddddddddddddddd));");
6922 
6923   // This test case breaks on an incorrect memoization, i.e. an optimization not
6924   // taking into account the StopAt value.
6925   verifyFormat(
6926       "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
6927       "       aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
6928       "       aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
6929       "       (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6930 
6931   verifyFormat("{\n  {\n    {\n"
6932                "      Annotation.SpaceRequiredBefore =\n"
6933                "          Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
6934                "          Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
6935                "    }\n  }\n}");
6936 
6937   // Break on an outer level if there was a break on an inner level.
6938   EXPECT_EQ("f(g(h(a, // comment\n"
6939             "      b, c),\n"
6940             "    d, e),\n"
6941             "  x, y);",
6942             format("f(g(h(a, // comment\n"
6943                    "    b, c), d, e), x, y);"));
6944 
6945   // Prefer breaking similar line breaks.
6946   verifyFormat(
6947       "const int kTrackingOptions = NSTrackingMouseMoved |\n"
6948       "                             NSTrackingMouseEnteredAndExited |\n"
6949       "                             NSTrackingActiveAlways;");
6950 }
6951 
6952 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) {
6953   FormatStyle NoBinPacking = getGoogleStyle();
6954   NoBinPacking.BinPackParameters = false;
6955   NoBinPacking.BinPackArguments = true;
6956   verifyFormat("void f() {\n"
6957                "  f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n"
6958                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
6959                "}",
6960                NoBinPacking);
6961   verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n"
6962                "       int aaaaaaaaaaaaaaaaaaaa,\n"
6963                "       int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
6964                NoBinPacking);
6965 
6966   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
6967   verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6968                "                        vector<int> bbbbbbbbbbbbbbb);",
6969                NoBinPacking);
6970   // FIXME: This behavior difference is probably not wanted. However, currently
6971   // we cannot distinguish BreakBeforeParameter being set because of the wrapped
6972   // template arguments from BreakBeforeParameter being set because of the
6973   // one-per-line formatting.
6974   verifyFormat(
6975       "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n"
6976       "                                             aaaaaaaaaa> aaaaaaaaaa);",
6977       NoBinPacking);
6978   verifyFormat(
6979       "void fffffffffff(\n"
6980       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n"
6981       "        aaaaaaaaaa);");
6982 }
6983 
6984 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
6985   FormatStyle NoBinPacking = getGoogleStyle();
6986   NoBinPacking.BinPackParameters = false;
6987   NoBinPacking.BinPackArguments = false;
6988   verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
6989                "  aaaaaaaaaaaaaaaaaaaa,\n"
6990                "  aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
6991                NoBinPacking);
6992   verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
6993                "        aaaaaaaaaaaaa,\n"
6994                "        aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
6995                NoBinPacking);
6996   verifyFormat(
6997       "aaaaaaaa(aaaaaaaaaaaaa,\n"
6998       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6999       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
7000       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7001       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
7002       NoBinPacking);
7003   verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
7004                "    .aaaaaaaaaaaaaaaaaa();",
7005                NoBinPacking);
7006   verifyFormat("void f() {\n"
7007                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7008                "      aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n"
7009                "}",
7010                NoBinPacking);
7011 
7012   verifyFormat(
7013       "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7014       "             aaaaaaaaaaaa,\n"
7015       "             aaaaaaaaaaaa);",
7016       NoBinPacking);
7017   verifyFormat(
7018       "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
7019       "                               ddddddddddddddddddddddddddddd),\n"
7020       "             test);",
7021       NoBinPacking);
7022 
7023   verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
7024                "            aaaaaaaaaaaaaaaaaaaaaaa,\n"
7025                "            aaaaaaaaaaaaaaaaaaaaaaa>\n"
7026                "    aaaaaaaaaaaaaaaaaa;",
7027                NoBinPacking);
7028   verifyFormat("a(\"a\"\n"
7029                "  \"a\",\n"
7030                "  a);");
7031 
7032   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
7033   verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
7034                "                aaaaaaaaa,\n"
7035                "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7036                NoBinPacking);
7037   verifyFormat(
7038       "void f() {\n"
7039       "  aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
7040       "      .aaaaaaa();\n"
7041       "}",
7042       NoBinPacking);
7043   verifyFormat(
7044       "template <class SomeType, class SomeOtherType>\n"
7045       "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
7046       NoBinPacking);
7047 }
7048 
7049 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
7050   FormatStyle Style = getLLVMStyleWithColumns(15);
7051   Style.ExperimentalAutoDetectBinPacking = true;
7052   EXPECT_EQ("aaa(aaaa,\n"
7053             "    aaaa,\n"
7054             "    aaaa);\n"
7055             "aaa(aaaa,\n"
7056             "    aaaa,\n"
7057             "    aaaa);",
7058             format("aaa(aaaa,\n" // one-per-line
7059                    "  aaaa,\n"
7060                    "    aaaa  );\n"
7061                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
7062                    Style));
7063   EXPECT_EQ("aaa(aaaa, aaaa,\n"
7064             "    aaaa);\n"
7065             "aaa(aaaa, aaaa,\n"
7066             "    aaaa);",
7067             format("aaa(aaaa,  aaaa,\n" // bin-packed
7068                    "    aaaa  );\n"
7069                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
7070                    Style));
7071 }
7072 
7073 TEST_F(FormatTest, FormatsBuilderPattern) {
7074   verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n"
7075                "    .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
7076                "    .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
7077                "    .StartsWith(\".init\", ORDER_INIT)\n"
7078                "    .StartsWith(\".fini\", ORDER_FINI)\n"
7079                "    .StartsWith(\".hash\", ORDER_HASH)\n"
7080                "    .Default(ORDER_TEXT);\n");
7081 
7082   verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
7083                "       aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
7084   verifyFormat("aaaaaaa->aaaaaaa\n"
7085                "    ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7086                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7087                "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
7088   verifyFormat(
7089       "aaaaaaa->aaaaaaa\n"
7090       "    ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7091       "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
7092   verifyFormat(
7093       "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
7094       "    aaaaaaaaaaaaaa);");
7095   verifyFormat(
7096       "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
7097       "    aaaaaa->aaaaaaaaaaaa()\n"
7098       "        ->aaaaaaaaaaaaaaaa(\n"
7099       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7100       "        ->aaaaaaaaaaaaaaaaa();");
7101   verifyGoogleFormat(
7102       "void f() {\n"
7103       "  someo->Add((new util::filetools::Handler(dir))\n"
7104       "                 ->OnEvent1(NewPermanentCallback(\n"
7105       "                     this, &HandlerHolderClass::EventHandlerCBA))\n"
7106       "                 ->OnEvent2(NewPermanentCallback(\n"
7107       "                     this, &HandlerHolderClass::EventHandlerCBB))\n"
7108       "                 ->OnEvent3(NewPermanentCallback(\n"
7109       "                     this, &HandlerHolderClass::EventHandlerCBC))\n"
7110       "                 ->OnEvent5(NewPermanentCallback(\n"
7111       "                     this, &HandlerHolderClass::EventHandlerCBD))\n"
7112       "                 ->OnEvent6(NewPermanentCallback(\n"
7113       "                     this, &HandlerHolderClass::EventHandlerCBE)));\n"
7114       "}");
7115 
7116   verifyFormat(
7117       "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
7118   verifyFormat("aaaaaaaaaaaaaaa()\n"
7119                "    .aaaaaaaaaaaaaaa()\n"
7120                "    .aaaaaaaaaaaaaaa()\n"
7121                "    .aaaaaaaaaaaaaaa()\n"
7122                "    .aaaaaaaaaaaaaaa();");
7123   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
7124                "    .aaaaaaaaaaaaaaa()\n"
7125                "    .aaaaaaaaaaaaaaa()\n"
7126                "    .aaaaaaaaaaaaaaa();");
7127   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
7128                "    .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
7129                "    .aaaaaaaaaaaaaaa();");
7130   verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
7131                "    ->aaaaaaaaaaaaaae(0)\n"
7132                "    ->aaaaaaaaaaaaaaa();");
7133 
7134   // Don't linewrap after very short segments.
7135   verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7136                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7137                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
7138   verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7139                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7140                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
7141   verifyFormat("aaa()\n"
7142                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7143                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7144                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
7145 
7146   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
7147                "    .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7148                "    .has<bbbbbbbbbbbbbbbbbbbbb>();");
7149   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
7150                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
7151                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
7152 
7153   // Prefer not to break after empty parentheses.
7154   verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
7155                "    First->LastNewlineOffset);");
7156 
7157   // Prefer not to create "hanging" indents.
7158   verifyFormat(
7159       "return !soooooooooooooome_map\n"
7160       "            .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7161       "            .second;");
7162   verifyFormat(
7163       "return aaaaaaaaaaaaaaaa\n"
7164       "    .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n"
7165       "    .aaaa(aaaaaaaaaaaaaa);");
7166   // No hanging indent here.
7167   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n"
7168                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7169   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n"
7170                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7171   verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
7172                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7173                getLLVMStyleWithColumns(60));
7174   verifyFormat("aaaaaaaaaaaaaaaaaa\n"
7175                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
7176                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7177                getLLVMStyleWithColumns(59));
7178   verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7179                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7180                "    .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7181 
7182   // Dont break if only closing statements before member call
7183   verifyFormat("test() {\n"
7184                "  ([]() -> {\n"
7185                "    int b = 32;\n"
7186                "    return 3;\n"
7187                "  }).foo();\n"
7188                "}");
7189   verifyFormat("test() {\n"
7190                "  (\n"
7191                "      []() -> {\n"
7192                "        int b = 32;\n"
7193                "        return 3;\n"
7194                "      },\n"
7195                "      foo, bar)\n"
7196                "      .foo();\n"
7197                "}");
7198   verifyFormat("test() {\n"
7199                "  ([]() -> {\n"
7200                "    int b = 32;\n"
7201                "    return 3;\n"
7202                "  })\n"
7203                "      .foo()\n"
7204                "      .bar();\n"
7205                "}");
7206   verifyFormat("test() {\n"
7207                "  ([]() -> {\n"
7208                "    int b = 32;\n"
7209                "    return 3;\n"
7210                "  })\n"
7211                "      .foo(\"aaaaaaaaaaaaaaaaa\"\n"
7212                "           \"bbbb\");\n"
7213                "}",
7214                getLLVMStyleWithColumns(30));
7215 }
7216 
7217 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
7218   verifyFormat(
7219       "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
7220       "    bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
7221   verifyFormat(
7222       "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n"
7223       "    bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}");
7224 
7225   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
7226                "    ccccccccccccccccccccccccc) {\n}");
7227   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n"
7228                "    ccccccccccccccccccccccccc) {\n}");
7229 
7230   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
7231                "    ccccccccccccccccccccccccc) {\n}");
7232   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n"
7233                "    ccccccccccccccccccccccccc) {\n}");
7234 
7235   verifyFormat(
7236       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
7237       "    ccccccccccccccccccccccccc) {\n}");
7238   verifyFormat(
7239       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n"
7240       "    ccccccccccccccccccccccccc) {\n}");
7241 
7242   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
7243                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
7244                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
7245                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
7246   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n"
7247                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n"
7248                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n"
7249                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
7250 
7251   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
7252                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
7253                "    aaaaaaaaaaaaaaa != aa) {\n}");
7254   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n"
7255                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n"
7256                "    aaaaaaaaaaaaaaa != aa) {\n}");
7257 }
7258 
7259 TEST_F(FormatTest, BreaksAfterAssignments) {
7260   verifyFormat(
7261       "unsigned Cost =\n"
7262       "    TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
7263       "                        SI->getPointerAddressSpaceee());\n");
7264   verifyFormat(
7265       "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
7266       "    Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
7267 
7268   verifyFormat(
7269       "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
7270       "    aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
7271   verifyFormat("unsigned OriginalStartColumn =\n"
7272                "    SourceMgr.getSpellingColumnNumber(\n"
7273                "        Current.FormatTok.getStartOfNonWhitespace()) -\n"
7274                "    1;");
7275 }
7276 
7277 TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) {
7278   FormatStyle Style = getLLVMStyle();
7279   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
7280                "    bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;",
7281                Style);
7282 
7283   Style.PenaltyBreakAssignment = 20;
7284   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
7285                "                                 cccccccccccccccccccccccccc;",
7286                Style);
7287 }
7288 
7289 TEST_F(FormatTest, AlignsAfterAssignments) {
7290   verifyFormat(
7291       "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7292       "             aaaaaaaaaaaaaaaaaaaaaaaaa;");
7293   verifyFormat(
7294       "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7295       "          aaaaaaaaaaaaaaaaaaaaaaaaa;");
7296   verifyFormat(
7297       "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7298       "           aaaaaaaaaaaaaaaaaaaaaaaaa;");
7299   verifyFormat(
7300       "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7301       "              aaaaaaaaaaaaaaaaaaaaaaaaa);");
7302   verifyFormat(
7303       "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
7304       "                                            aaaaaaaaaaaaaaaaaaaaaaaa +\n"
7305       "                                            aaaaaaaaaaaaaaaaaaaaaaaa;");
7306 }
7307 
7308 TEST_F(FormatTest, AlignsAfterReturn) {
7309   verifyFormat(
7310       "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7311       "       aaaaaaaaaaaaaaaaaaaaaaaaa;");
7312   verifyFormat(
7313       "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7314       "        aaaaaaaaaaaaaaaaaaaaaaaaa);");
7315   verifyFormat(
7316       "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
7317       "       aaaaaaaaaaaaaaaaaaaaaa();");
7318   verifyFormat(
7319       "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
7320       "        aaaaaaaaaaaaaaaaaaaaaa());");
7321   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7322                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7323   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7324                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
7325                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
7326   verifyFormat("return\n"
7327                "    // true if code is one of a or b.\n"
7328                "    code == a || code == b;");
7329 }
7330 
7331 TEST_F(FormatTest, AlignsAfterOpenBracket) {
7332   verifyFormat(
7333       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
7334       "                                                aaaaaaaaa aaaaaaa) {}");
7335   verifyFormat(
7336       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
7337       "                                               aaaaaaaaaaa aaaaaaaaa);");
7338   verifyFormat(
7339       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
7340       "                                             aaaaaaaaaaaaaaaaaaaaa));");
7341   FormatStyle Style = getLLVMStyle();
7342   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
7343   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7344                "    aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}",
7345                Style);
7346   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
7347                "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);",
7348                Style);
7349   verifyFormat("SomeLongVariableName->someFunction(\n"
7350                "    foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));",
7351                Style);
7352   verifyFormat(
7353       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
7354       "    aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
7355       Style);
7356   verifyFormat(
7357       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
7358       "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7359       Style);
7360   verifyFormat(
7361       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
7362       "    aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
7363       Style);
7364 
7365   verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n"
7366                "    ccccccc(aaaaaaaaaaaaaaaaa,         //\n"
7367                "        b));",
7368                Style);
7369 
7370   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
7371   Style.BinPackArguments = false;
7372   Style.BinPackParameters = false;
7373   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7374                "    aaaaaaaaaaa aaaaaaaa,\n"
7375                "    aaaaaaaaa aaaaaaa,\n"
7376                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
7377                Style);
7378   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
7379                "    aaaaaaaaaaa aaaaaaaaa,\n"
7380                "    aaaaaaaaaaa aaaaaaaaa,\n"
7381                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7382                Style);
7383   verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n"
7384                "    aaaaaaaaaaaaaaa,\n"
7385                "    aaaaaaaaaaaaaaaaaaaaa,\n"
7386                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
7387                Style);
7388   verifyFormat(
7389       "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n"
7390       "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
7391       Style);
7392   verifyFormat(
7393       "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n"
7394       "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
7395       Style);
7396   verifyFormat(
7397       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
7398       "    aaaaaaaaaaaaaaaaaaaaa(\n"
7399       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n"
7400       "    aaaaaaaaaaaaaaaa);",
7401       Style);
7402   verifyFormat(
7403       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
7404       "    aaaaaaaaaaaaaaaaaaaaa(\n"
7405       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n"
7406       "    aaaaaaaaaaaaaaaa);",
7407       Style);
7408 }
7409 
7410 TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
7411   FormatStyle Style = getLLVMStyleWithColumns(40);
7412   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
7413                "          bbbbbbbbbbbbbbbbbbbbbb);",
7414                Style);
7415   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
7416   Style.AlignOperands = FormatStyle::OAS_DontAlign;
7417   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
7418                "          bbbbbbbbbbbbbbbbbbbbbb);",
7419                Style);
7420   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
7421   Style.AlignOperands = FormatStyle::OAS_Align;
7422   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
7423                "          bbbbbbbbbbbbbbbbbbbbbb);",
7424                Style);
7425   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
7426   Style.AlignOperands = FormatStyle::OAS_DontAlign;
7427   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
7428                "    bbbbbbbbbbbbbbbbbbbbbb);",
7429                Style);
7430 }
7431 
7432 TEST_F(FormatTest, BreaksConditionalExpressions) {
7433   verifyFormat(
7434       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7435       "                               ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7436       "                               : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7437   verifyFormat(
7438       "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
7439       "     aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7440       "                                : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7441   verifyFormat(
7442       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7443       "                                   : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7444   verifyFormat("aaaa(aaaaaaaaa, aaaaaaaaa,\n"
7445                "     aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7446                "             : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7447   verifyFormat(
7448       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
7449       "                                                    : aaaaaaaaaaaaa);");
7450   verifyFormat(
7451       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7452       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7453       "                                    : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7454       "                   aaaaaaaaaaaaa);");
7455   verifyFormat(
7456       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7457       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7458       "                   aaaaaaaaaaaaa);");
7459   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7460                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7461                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7462                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7463                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7464   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7465                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7466                "           ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7467                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7468                "           : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7469                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7470                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7471   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7472                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7473                "           ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7474                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7475                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7476   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7477                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7478                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
7479   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
7480                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7481                "        ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7482                "        : aaaaaaaaaaaaaaaa;");
7483   verifyFormat(
7484       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7485       "    ? aaaaaaaaaaaaaaa\n"
7486       "    : aaaaaaaaaaaaaaa;");
7487   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
7488                "          aaaaaaaaa\n"
7489                "      ? b\n"
7490                "      : c);");
7491   verifyFormat("return aaaa == bbbb\n"
7492                "           // comment\n"
7493                "           ? aaaa\n"
7494                "           : bbbb;");
7495   verifyFormat("unsigned Indent =\n"
7496                "    format(TheLine.First,\n"
7497                "           IndentForLevel[TheLine.Level] >= 0\n"
7498                "               ? IndentForLevel[TheLine.Level]\n"
7499                "               : TheLine * 2,\n"
7500                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
7501                getLLVMStyleWithColumns(60));
7502   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
7503                "                  ? aaaaaaaaaaaaaaa\n"
7504                "                  : bbbbbbbbbbbbbbb //\n"
7505                "                        ? ccccccccccccccc\n"
7506                "                        : ddddddddddddddd;");
7507   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
7508                "                  ? aaaaaaaaaaaaaaa\n"
7509                "                  : (bbbbbbbbbbbbbbb //\n"
7510                "                         ? ccccccccccccccc\n"
7511                "                         : ddddddddddddddd);");
7512   verifyFormat(
7513       "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7514       "                                      ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7515       "                                            aaaaaaaaaaaaaaaaaaaaa +\n"
7516       "                                            aaaaaaaaaaaaaaaaaaaaa\n"
7517       "                                      : aaaaaaaaaa;");
7518   verifyFormat(
7519       "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7520       "                                   : aaaaaaaaaaaaaaaaaaaaaa\n"
7521       "                      : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
7522 
7523   FormatStyle NoBinPacking = getLLVMStyle();
7524   NoBinPacking.BinPackArguments = false;
7525   verifyFormat(
7526       "void f() {\n"
7527       "  g(aaa,\n"
7528       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
7529       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7530       "        ? aaaaaaaaaaaaaaa\n"
7531       "        : aaaaaaaaaaaaaaa);\n"
7532       "}",
7533       NoBinPacking);
7534   verifyFormat(
7535       "void f() {\n"
7536       "  g(aaa,\n"
7537       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
7538       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7539       "        ?: aaaaaaaaaaaaaaa);\n"
7540       "}",
7541       NoBinPacking);
7542 
7543   verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n"
7544                "             // comment.\n"
7545                "             ccccccccccccccccccccccccccccccccccccccc\n"
7546                "                 ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7547                "                 : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);");
7548 
7549   // Assignments in conditional expressions. Apparently not uncommon :-(.
7550   verifyFormat("return a != b\n"
7551                "           // comment\n"
7552                "           ? a = b\n"
7553                "           : a = b;");
7554   verifyFormat("return a != b\n"
7555                "           // comment\n"
7556                "           ? a = a != b\n"
7557                "                     // comment\n"
7558                "                     ? a = b\n"
7559                "                     : a\n"
7560                "           : a;\n");
7561   verifyFormat("return a != b\n"
7562                "           // comment\n"
7563                "           ? a\n"
7564                "           : a = a != b\n"
7565                "                     // comment\n"
7566                "                     ? a = b\n"
7567                "                     : a;");
7568 
7569   // Chained conditionals
7570   FormatStyle Style = getLLVMStyle();
7571   Style.ColumnLimit = 70;
7572   Style.AlignOperands = FormatStyle::OAS_Align;
7573   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7574                "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7575                "                        : 3333333333333333;",
7576                Style);
7577   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7578                "       : bbbbbbbbbb     ? 2222222222222222\n"
7579                "                        : 3333333333333333;",
7580                Style);
7581   verifyFormat("return aaaaaaaaaa         ? 1111111111111111\n"
7582                "       : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
7583                "                          : 3333333333333333;",
7584                Style);
7585   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7586                "       : bbbbbbbbbbbbbb ? 222222\n"
7587                "                        : 333333;",
7588                Style);
7589   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7590                "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7591                "       : cccccccccccccc ? 3333333333333333\n"
7592                "                        : 4444444444444444;",
7593                Style);
7594   verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc)\n"
7595                "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7596                "                        : 3333333333333333;",
7597                Style);
7598   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7599                "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7600                "                        : (aaa ? bbb : ccc);",
7601                Style);
7602   verifyFormat(
7603       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7604       "                                             : cccccccccccccccccc)\n"
7605       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7606       "                        : 3333333333333333;",
7607       Style);
7608   verifyFormat(
7609       "return aaaaaaaaa        ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7610       "                                             : cccccccccccccccccc)\n"
7611       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7612       "                        : 3333333333333333;",
7613       Style);
7614   verifyFormat(
7615       "return aaaaaaaaa        ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7616       "                                             : dddddddddddddddddd)\n"
7617       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7618       "                        : 3333333333333333;",
7619       Style);
7620   verifyFormat(
7621       "return aaaaaaaaa        ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7622       "                                             : dddddddddddddddddd)\n"
7623       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7624       "                        : 3333333333333333;",
7625       Style);
7626   verifyFormat(
7627       "return aaaaaaaaa        ? 1111111111111111\n"
7628       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7629       "                        : a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7630       "                                             : dddddddddddddddddd)\n",
7631       Style);
7632   verifyFormat(
7633       "return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7634       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7635       "                        : (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7636       "                                             : cccccccccccccccccc);",
7637       Style);
7638   verifyFormat(
7639       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7640       "                           : ccccccccccccccc ? dddddddddddddddddd\n"
7641       "                                             : eeeeeeeeeeeeeeeeee)\n"
7642       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7643       "                        : 3333333333333333;",
7644       Style);
7645   verifyFormat(
7646       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa    ? bbbbbbbbbbbbbbbbbb\n"
7647       "                           : ccccccccccccccc ? dddddddddddddddddd\n"
7648       "                                             : eeeeeeeeeeeeeeeeee)\n"
7649       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7650       "                        : 3333333333333333;",
7651       Style);
7652   verifyFormat(
7653       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7654       "                           : cccccccccccc    ? dddddddddddddddddd\n"
7655       "                                             : eeeeeeeeeeeeeeeeee)\n"
7656       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7657       "                        : 3333333333333333;",
7658       Style);
7659   verifyFormat(
7660       "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7661       "                                             : cccccccccccccccccc\n"
7662       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7663       "                        : 3333333333333333;",
7664       Style);
7665   verifyFormat(
7666       "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7667       "                          : cccccccccccccccc ? dddddddddddddddddd\n"
7668       "                                             : eeeeeeeeeeeeeeeeee\n"
7669       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7670       "                        : 3333333333333333;",
7671       Style);
7672   verifyFormat("return aaaaaaaaaaaaaaaaaaaaa\n"
7673                "           ? (aaaaaaaaaaaaaaaaaa   ? bbbbbbbbbbbbbbbbbb\n"
7674                "              : cccccccccccccccccc ? dddddddddddddddddd\n"
7675                "                                   : eeeeeeeeeeeeeeeeee)\n"
7676                "       : bbbbbbbbbbbbbbbbbbb ? 2222222222222222\n"
7677                "                             : 3333333333333333;",
7678                Style);
7679   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaa\n"
7680                "           ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7681                "             : cccccccccccccccc ? dddddddddddddddddd\n"
7682                "                                : eeeeeeeeeeeeeeeeee\n"
7683                "       : bbbbbbbbbbbbbbbbbbbbbbb ? 2222222222222222\n"
7684                "                                 : 3333333333333333;",
7685                Style);
7686 
7687   Style.AlignOperands = FormatStyle::OAS_DontAlign;
7688   Style.BreakBeforeTernaryOperators = false;
7689   // FIXME: Aligning the question marks is weird given DontAlign.
7690   // Consider disabling this alignment in this case. Also check whether this
7691   // will render the adjustment from https://reviews.llvm.org/D82199
7692   // unnecessary.
7693   verifyFormat("int x = aaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa :\n"
7694                "    bbbb                ? cccccccccccccccccc :\n"
7695                "                          ddddd;\n",
7696                Style);
7697 
7698   EXPECT_EQ(
7699       "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n"
7700       "    /*\n"
7701       "     */\n"
7702       "    function() {\n"
7703       "      try {\n"
7704       "        return JJJJJJJJJJJJJJ(\n"
7705       "            pppppppppppppppppppppppppppppppppppppppppppppppppp);\n"
7706       "      }\n"
7707       "    } :\n"
7708       "    function() {};",
7709       format(
7710           "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n"
7711           "     /*\n"
7712           "      */\n"
7713           "     function() {\n"
7714           "      try {\n"
7715           "        return JJJJJJJJJJJJJJ(\n"
7716           "            pppppppppppppppppppppppppppppppppppppppppppppppppp);\n"
7717           "      }\n"
7718           "    } :\n"
7719           "    function() {};",
7720           getGoogleStyle(FormatStyle::LK_JavaScript)));
7721 }
7722 
7723 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
7724   FormatStyle Style = getLLVMStyle();
7725   Style.BreakBeforeTernaryOperators = false;
7726   Style.ColumnLimit = 70;
7727   verifyFormat(
7728       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
7729       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
7730       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7731       Style);
7732   verifyFormat(
7733       "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
7734       "     aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
7735       "                                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7736       Style);
7737   verifyFormat(
7738       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
7739       "                                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7740       Style);
7741   verifyFormat("aaaa(aaaaaaaa, aaaaaaaaaa,\n"
7742                "     aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
7743                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7744                Style);
7745   verifyFormat(
7746       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
7747       "                                                      aaaaaaaaaaaaa);",
7748       Style);
7749   verifyFormat(
7750       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7751       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
7752       "                                      aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7753       "                   aaaaaaaaaaaaa);",
7754       Style);
7755   verifyFormat(
7756       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7757       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7758       "                   aaaaaaaaaaaaa);",
7759       Style);
7760   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
7761                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7762                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
7763                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7764                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7765                Style);
7766   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7767                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
7768                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7769                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
7770                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7771                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7772                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7773                Style);
7774   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7775                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n"
7776                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7777                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7778                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7779                Style);
7780   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
7781                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
7782                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
7783                Style);
7784   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
7785                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
7786                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
7787                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
7788                Style);
7789   verifyFormat(
7790       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
7791       "    aaaaaaaaaaaaaaa :\n"
7792       "    aaaaaaaaaaaaaaa;",
7793       Style);
7794   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
7795                "          aaaaaaaaa ?\n"
7796                "      b :\n"
7797                "      c);",
7798                Style);
7799   verifyFormat("unsigned Indent =\n"
7800                "    format(TheLine.First,\n"
7801                "           IndentForLevel[TheLine.Level] >= 0 ?\n"
7802                "               IndentForLevel[TheLine.Level] :\n"
7803                "               TheLine * 2,\n"
7804                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
7805                Style);
7806   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
7807                "                  aaaaaaaaaaaaaaa :\n"
7808                "                  bbbbbbbbbbbbbbb ? //\n"
7809                "                      ccccccccccccccc :\n"
7810                "                      ddddddddddddddd;",
7811                Style);
7812   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
7813                "                  aaaaaaaaaaaaaaa :\n"
7814                "                  (bbbbbbbbbbbbbbb ? //\n"
7815                "                       ccccccccccccccc :\n"
7816                "                       ddddddddddddddd);",
7817                Style);
7818   verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
7819                "            /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n"
7820                "            ccccccccccccccccccccccccccc;",
7821                Style);
7822   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
7823                "           aaaaa :\n"
7824                "           bbbbbbbbbbbbbbb + cccccccccccccccc;",
7825                Style);
7826 
7827   // Chained conditionals
7828   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
7829                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7830                "                          3333333333333333;",
7831                Style);
7832   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
7833                "       bbbbbbbbbb       ? 2222222222222222 :\n"
7834                "                          3333333333333333;",
7835                Style);
7836   verifyFormat("return aaaaaaaaaa       ? 1111111111111111 :\n"
7837                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7838                "                          3333333333333333;",
7839                Style);
7840   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
7841                "       bbbbbbbbbbbbbbbb ? 222222 :\n"
7842                "                          333333;",
7843                Style);
7844   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
7845                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7846                "       cccccccccccccccc ? 3333333333333333 :\n"
7847                "                          4444444444444444;",
7848                Style);
7849   verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc) :\n"
7850                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7851                "                          3333333333333333;",
7852                Style);
7853   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
7854                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7855                "                          (aaa ? bbb : ccc);",
7856                Style);
7857   verifyFormat(
7858       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7859       "                                               cccccccccccccccccc) :\n"
7860       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7861       "                          3333333333333333;",
7862       Style);
7863   verifyFormat(
7864       "return aaaaaaaaa        ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7865       "                                               cccccccccccccccccc) :\n"
7866       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7867       "                          3333333333333333;",
7868       Style);
7869   verifyFormat(
7870       "return aaaaaaaaa        ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7871       "                                               dddddddddddddddddd) :\n"
7872       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7873       "                          3333333333333333;",
7874       Style);
7875   verifyFormat(
7876       "return aaaaaaaaa        ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7877       "                                               dddddddddddddddddd) :\n"
7878       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7879       "                          3333333333333333;",
7880       Style);
7881   verifyFormat(
7882       "return aaaaaaaaa        ? 1111111111111111 :\n"
7883       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7884       "                          a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7885       "                                               dddddddddddddddddd)\n",
7886       Style);
7887   verifyFormat(
7888       "return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
7889       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7890       "                          (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7891       "                                               cccccccccccccccccc);",
7892       Style);
7893   verifyFormat(
7894       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7895       "                           ccccccccccccccccc ? dddddddddddddddddd :\n"
7896       "                                               eeeeeeeeeeeeeeeeee) :\n"
7897       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7898       "                          3333333333333333;",
7899       Style);
7900   verifyFormat(
7901       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7902       "                           ccccccccccccc     ? dddddddddddddddddd :\n"
7903       "                                               eeeeeeeeeeeeeeeeee) :\n"
7904       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7905       "                          3333333333333333;",
7906       Style);
7907   verifyFormat(
7908       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaa     ? bbbbbbbbbbbbbbbbbb :\n"
7909       "                           ccccccccccccccccc ? dddddddddddddddddd :\n"
7910       "                                               eeeeeeeeeeeeeeeeee) :\n"
7911       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7912       "                          3333333333333333;",
7913       Style);
7914   verifyFormat(
7915       "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7916       "                                               cccccccccccccccccc :\n"
7917       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7918       "                          3333333333333333;",
7919       Style);
7920   verifyFormat(
7921       "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7922       "                          cccccccccccccccccc ? dddddddddddddddddd :\n"
7923       "                                               eeeeeeeeeeeeeeeeee :\n"
7924       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7925       "                          3333333333333333;",
7926       Style);
7927   verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n"
7928                "           (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7929                "            cccccccccccccccccc ? dddddddddddddddddd :\n"
7930                "                                 eeeeeeeeeeeeeeeeee) :\n"
7931                "       bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7932                "                               3333333333333333;",
7933                Style);
7934   verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n"
7935                "           aaaaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7936                "           cccccccccccccccccccc ? dddddddddddddddddd :\n"
7937                "                                  eeeeeeeeeeeeeeeeee :\n"
7938                "       bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7939                "                               3333333333333333;",
7940                Style);
7941 }
7942 
7943 TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
7944   verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
7945                "     aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
7946   verifyFormat("bool a = true, b = false;");
7947 
7948   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
7949                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
7950                "     bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
7951                "         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
7952   verifyFormat(
7953       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
7954       "         bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
7955       "     d = e && f;");
7956   verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
7957                "          c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
7958   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
7959                "          *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
7960   verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
7961                "          ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
7962 
7963   FormatStyle Style = getGoogleStyle();
7964   Style.PointerAlignment = FormatStyle::PAS_Left;
7965   Style.DerivePointerAlignment = false;
7966   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7967                "    *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
7968                "    *b = bbbbbbbbbbbbbbbbbbb;",
7969                Style);
7970   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
7971                "          *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;",
7972                Style);
7973   verifyFormat("vector<int*> a, b;", Style);
7974   verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style);
7975 }
7976 
7977 TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
7978   verifyFormat("arr[foo ? bar : baz];");
7979   verifyFormat("f()[foo ? bar : baz];");
7980   verifyFormat("(a + b)[foo ? bar : baz];");
7981   verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
7982 }
7983 
7984 TEST_F(FormatTest, AlignsStringLiterals) {
7985   verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
7986                "                                      \"short literal\");");
7987   verifyFormat(
7988       "looooooooooooooooooooooooongFunction(\n"
7989       "    \"short literal\"\n"
7990       "    \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
7991   verifyFormat("someFunction(\"Always break between multi-line\"\n"
7992                "             \" string literals\",\n"
7993                "             and, other, parameters);");
7994   EXPECT_EQ("fun + \"1243\" /* comment */\n"
7995             "      \"5678\";",
7996             format("fun + \"1243\" /* comment */\n"
7997                    "    \"5678\";",
7998                    getLLVMStyleWithColumns(28)));
7999   EXPECT_EQ(
8000       "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
8001       "         \"aaaaaaaaaaaaaaaaaaaaa\"\n"
8002       "         \"aaaaaaaaaaaaaaaa\";",
8003       format("aaaaaa ="
8004              "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
8005              "aaaaaaaaaaaaaaaaaaaaa\" "
8006              "\"aaaaaaaaaaaaaaaa\";"));
8007   verifyFormat("a = a + \"a\"\n"
8008                "        \"a\"\n"
8009                "        \"a\";");
8010   verifyFormat("f(\"a\", \"b\"\n"
8011                "       \"c\");");
8012 
8013   verifyFormat(
8014       "#define LL_FORMAT \"ll\"\n"
8015       "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
8016       "       \"d, ddddddddd: %\" LL_FORMAT \"d\");");
8017 
8018   verifyFormat("#define A(X)          \\\n"
8019                "  \"aaaaa\" #X \"bbbbbb\" \\\n"
8020                "  \"ccccc\"",
8021                getLLVMStyleWithColumns(23));
8022   verifyFormat("#define A \"def\"\n"
8023                "f(\"abc\" A \"ghi\"\n"
8024                "  \"jkl\");");
8025 
8026   verifyFormat("f(L\"a\"\n"
8027                "  L\"b\");");
8028   verifyFormat("#define A(X)            \\\n"
8029                "  L\"aaaaa\" #X L\"bbbbbb\" \\\n"
8030                "  L\"ccccc\"",
8031                getLLVMStyleWithColumns(25));
8032 
8033   verifyFormat("f(@\"a\"\n"
8034                "  @\"b\");");
8035   verifyFormat("NSString s = @\"a\"\n"
8036                "             @\"b\"\n"
8037                "             @\"c\";");
8038   verifyFormat("NSString s = @\"a\"\n"
8039                "              \"b\"\n"
8040                "              \"c\";");
8041 }
8042 
8043 TEST_F(FormatTest, ReturnTypeBreakingStyle) {
8044   FormatStyle Style = getLLVMStyle();
8045   // No declarations or definitions should be moved to own line.
8046   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None;
8047   verifyFormat("class A {\n"
8048                "  int f() { return 1; }\n"
8049                "  int g();\n"
8050                "};\n"
8051                "int f() { return 1; }\n"
8052                "int g();\n",
8053                Style);
8054 
8055   // All declarations and definitions should have the return type moved to its
8056   // own line.
8057   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
8058   Style.TypenameMacros = {"LIST"};
8059   verifyFormat("SomeType\n"
8060                "funcdecl(LIST(uint64_t));",
8061                Style);
8062   verifyFormat("class E {\n"
8063                "  int\n"
8064                "  f() {\n"
8065                "    return 1;\n"
8066                "  }\n"
8067                "  int\n"
8068                "  g();\n"
8069                "};\n"
8070                "int\n"
8071                "f() {\n"
8072                "  return 1;\n"
8073                "}\n"
8074                "int\n"
8075                "g();\n",
8076                Style);
8077 
8078   // Top-level definitions, and no kinds of declarations should have the
8079   // return type moved to its own line.
8080   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions;
8081   verifyFormat("class B {\n"
8082                "  int f() { return 1; }\n"
8083                "  int g();\n"
8084                "};\n"
8085                "int\n"
8086                "f() {\n"
8087                "  return 1;\n"
8088                "}\n"
8089                "int g();\n",
8090                Style);
8091 
8092   // Top-level definitions and declarations should have the return type moved
8093   // to its own line.
8094   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel;
8095   verifyFormat("class C {\n"
8096                "  int f() { return 1; }\n"
8097                "  int g();\n"
8098                "};\n"
8099                "int\n"
8100                "f() {\n"
8101                "  return 1;\n"
8102                "}\n"
8103                "int\n"
8104                "g();\n",
8105                Style);
8106 
8107   // All definitions should have the return type moved to its own line, but no
8108   // kinds of declarations.
8109   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
8110   verifyFormat("class D {\n"
8111                "  int\n"
8112                "  f() {\n"
8113                "    return 1;\n"
8114                "  }\n"
8115                "  int g();\n"
8116                "};\n"
8117                "int\n"
8118                "f() {\n"
8119                "  return 1;\n"
8120                "}\n"
8121                "int g();\n",
8122                Style);
8123   verifyFormat("const char *\n"
8124                "f(void) {\n" // Break here.
8125                "  return \"\";\n"
8126                "}\n"
8127                "const char *bar(void);\n", // No break here.
8128                Style);
8129   verifyFormat("template <class T>\n"
8130                "T *\n"
8131                "f(T &c) {\n" // Break here.
8132                "  return NULL;\n"
8133                "}\n"
8134                "template <class T> T *f(T &c);\n", // No break here.
8135                Style);
8136   verifyFormat("class C {\n"
8137                "  int\n"
8138                "  operator+() {\n"
8139                "    return 1;\n"
8140                "  }\n"
8141                "  int\n"
8142                "  operator()() {\n"
8143                "    return 1;\n"
8144                "  }\n"
8145                "};\n",
8146                Style);
8147   verifyFormat("void\n"
8148                "A::operator()() {}\n"
8149                "void\n"
8150                "A::operator>>() {}\n"
8151                "void\n"
8152                "A::operator+() {}\n"
8153                "void\n"
8154                "A::operator*() {}\n"
8155                "void\n"
8156                "A::operator->() {}\n"
8157                "void\n"
8158                "A::operator void *() {}\n"
8159                "void\n"
8160                "A::operator void &() {}\n"
8161                "void\n"
8162                "A::operator void &&() {}\n"
8163                "void\n"
8164                "A::operator char *() {}\n"
8165                "void\n"
8166                "A::operator[]() {}\n"
8167                "void\n"
8168                "A::operator!() {}\n"
8169                "void\n"
8170                "A::operator**() {}\n"
8171                "void\n"
8172                "A::operator<Foo> *() {}\n"
8173                "void\n"
8174                "A::operator<Foo> **() {}\n"
8175                "void\n"
8176                "A::operator<Foo> &() {}\n"
8177                "void\n"
8178                "A::operator void **() {}\n",
8179                Style);
8180   verifyFormat("constexpr auto\n"
8181                "operator()() const -> reference {}\n"
8182                "constexpr auto\n"
8183                "operator>>() const -> reference {}\n"
8184                "constexpr auto\n"
8185                "operator+() const -> reference {}\n"
8186                "constexpr auto\n"
8187                "operator*() const -> reference {}\n"
8188                "constexpr auto\n"
8189                "operator->() const -> reference {}\n"
8190                "constexpr auto\n"
8191                "operator++() const -> reference {}\n"
8192                "constexpr auto\n"
8193                "operator void *() const -> reference {}\n"
8194                "constexpr auto\n"
8195                "operator void **() const -> reference {}\n"
8196                "constexpr auto\n"
8197                "operator void *() const -> reference {}\n"
8198                "constexpr auto\n"
8199                "operator void &() const -> reference {}\n"
8200                "constexpr auto\n"
8201                "operator void &&() const -> reference {}\n"
8202                "constexpr auto\n"
8203                "operator char *() const -> reference {}\n"
8204                "constexpr auto\n"
8205                "operator!() const -> reference {}\n"
8206                "constexpr auto\n"
8207                "operator[]() const -> reference {}\n",
8208                Style);
8209   verifyFormat("void *operator new(std::size_t s);", // No break here.
8210                Style);
8211   verifyFormat("void *\n"
8212                "operator new(std::size_t s) {}",
8213                Style);
8214   verifyFormat("void *\n"
8215                "operator delete[](void *ptr) {}",
8216                Style);
8217   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
8218   verifyFormat("const char *\n"
8219                "f(void)\n" // Break here.
8220                "{\n"
8221                "  return \"\";\n"
8222                "}\n"
8223                "const char *bar(void);\n", // No break here.
8224                Style);
8225   verifyFormat("template <class T>\n"
8226                "T *\n"     // Problem here: no line break
8227                "f(T &c)\n" // Break here.
8228                "{\n"
8229                "  return NULL;\n"
8230                "}\n"
8231                "template <class T> T *f(T &c);\n", // No break here.
8232                Style);
8233   verifyFormat("int\n"
8234                "foo(A<bool> a)\n"
8235                "{\n"
8236                "  return a;\n"
8237                "}\n",
8238                Style);
8239   verifyFormat("int\n"
8240                "foo(A<8> a)\n"
8241                "{\n"
8242                "  return a;\n"
8243                "}\n",
8244                Style);
8245   verifyFormat("int\n"
8246                "foo(A<B<bool>, 8> a)\n"
8247                "{\n"
8248                "  return a;\n"
8249                "}\n",
8250                Style);
8251   verifyFormat("int\n"
8252                "foo(A<B<8>, bool> a)\n"
8253                "{\n"
8254                "  return a;\n"
8255                "}\n",
8256                Style);
8257   verifyFormat("int\n"
8258                "foo(A<B<bool>, bool> a)\n"
8259                "{\n"
8260                "  return a;\n"
8261                "}\n",
8262                Style);
8263   verifyFormat("int\n"
8264                "foo(A<B<8>, 8> a)\n"
8265                "{\n"
8266                "  return a;\n"
8267                "}\n",
8268                Style);
8269 
8270   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
8271   Style.BraceWrapping.AfterFunction = true;
8272   verifyFormat("int f(i);\n" // No break here.
8273                "int\n"       // Break here.
8274                "f(i)\n"
8275                "{\n"
8276                "  return i + 1;\n"
8277                "}\n"
8278                "int\n" // Break here.
8279                "f(i)\n"
8280                "{\n"
8281                "  return i + 1;\n"
8282                "};",
8283                Style);
8284   verifyFormat("int f(a, b, c);\n" // No break here.
8285                "int\n"             // Break here.
8286                "f(a, b, c)\n"      // Break here.
8287                "short a, b;\n"
8288                "float c;\n"
8289                "{\n"
8290                "  return a + b < c;\n"
8291                "}\n"
8292                "int\n"        // Break here.
8293                "f(a, b, c)\n" // Break here.
8294                "short a, b;\n"
8295                "float c;\n"
8296                "{\n"
8297                "  return a + b < c;\n"
8298                "};",
8299                Style);
8300   verifyFormat("byte *\n" // Break here.
8301                "f(a)\n"   // Break here.
8302                "byte a[];\n"
8303                "{\n"
8304                "  return a;\n"
8305                "}",
8306                Style);
8307   verifyFormat("bool f(int a, int) override;\n"
8308                "Bar g(int a, Bar) final;\n"
8309                "Bar h(a, Bar) final;",
8310                Style);
8311   verifyFormat("int\n"
8312                "f(a)",
8313                Style);
8314   verifyFormat("bool\n"
8315                "f(size_t = 0, bool b = false)\n"
8316                "{\n"
8317                "  return !b;\n"
8318                "}",
8319                Style);
8320 
8321   // The return breaking style doesn't affect:
8322   // * function and object definitions with attribute-like macros
8323   verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n"
8324                "    ABSL_GUARDED_BY(mutex) = {};",
8325                getGoogleStyleWithColumns(40));
8326   verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n"
8327                "    ABSL_GUARDED_BY(mutex);  // comment",
8328                getGoogleStyleWithColumns(40));
8329   verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n"
8330                "    ABSL_GUARDED_BY(mutex1)\n"
8331                "        ABSL_GUARDED_BY(mutex2);",
8332                getGoogleStyleWithColumns(40));
8333   verifyFormat("Tttttt f(int a, int b)\n"
8334                "    ABSL_GUARDED_BY(mutex1)\n"
8335                "        ABSL_GUARDED_BY(mutex2);",
8336                getGoogleStyleWithColumns(40));
8337   // * typedefs
8338   verifyFormat("typedef ATTR(X) char x;", getGoogleStyle());
8339 
8340   Style = getGNUStyle();
8341 
8342   // Test for comments at the end of function declarations.
8343   verifyFormat("void\n"
8344                "foo (int a, /*abc*/ int b) // def\n"
8345                "{\n"
8346                "}\n",
8347                Style);
8348 
8349   verifyFormat("void\n"
8350                "foo (int a, /* abc */ int b) /* def */\n"
8351                "{\n"
8352                "}\n",
8353                Style);
8354 
8355   // Definitions that should not break after return type
8356   verifyFormat("void foo (int a, int b); // def\n", Style);
8357   verifyFormat("void foo (int a, int b); /* def */\n", Style);
8358   verifyFormat("void foo (int a, int b);\n", Style);
8359 }
8360 
8361 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
8362   FormatStyle NoBreak = getLLVMStyle();
8363   NoBreak.AlwaysBreakBeforeMultilineStrings = false;
8364   FormatStyle Break = getLLVMStyle();
8365   Break.AlwaysBreakBeforeMultilineStrings = true;
8366   verifyFormat("aaaa = \"bbbb\"\n"
8367                "       \"cccc\";",
8368                NoBreak);
8369   verifyFormat("aaaa =\n"
8370                "    \"bbbb\"\n"
8371                "    \"cccc\";",
8372                Break);
8373   verifyFormat("aaaa(\"bbbb\"\n"
8374                "     \"cccc\");",
8375                NoBreak);
8376   verifyFormat("aaaa(\n"
8377                "    \"bbbb\"\n"
8378                "    \"cccc\");",
8379                Break);
8380   verifyFormat("aaaa(qqq, \"bbbb\"\n"
8381                "          \"cccc\");",
8382                NoBreak);
8383   verifyFormat("aaaa(qqq,\n"
8384                "     \"bbbb\"\n"
8385                "     \"cccc\");",
8386                Break);
8387   verifyFormat("aaaa(qqq,\n"
8388                "     L\"bbbb\"\n"
8389                "     L\"cccc\");",
8390                Break);
8391   verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n"
8392                "                      \"bbbb\"));",
8393                Break);
8394   verifyFormat("string s = someFunction(\n"
8395                "    \"abc\"\n"
8396                "    \"abc\");",
8397                Break);
8398 
8399   // As we break before unary operators, breaking right after them is bad.
8400   verifyFormat("string foo = abc ? \"x\"\n"
8401                "                   \"blah blah blah blah blah blah\"\n"
8402                "                 : \"y\";",
8403                Break);
8404 
8405   // Don't break if there is no column gain.
8406   verifyFormat("f(\"aaaa\"\n"
8407                "  \"bbbb\");",
8408                Break);
8409 
8410   // Treat literals with escaped newlines like multi-line string literals.
8411   EXPECT_EQ("x = \"a\\\n"
8412             "b\\\n"
8413             "c\";",
8414             format("x = \"a\\\n"
8415                    "b\\\n"
8416                    "c\";",
8417                    NoBreak));
8418   EXPECT_EQ("xxxx =\n"
8419             "    \"a\\\n"
8420             "b\\\n"
8421             "c\";",
8422             format("xxxx = \"a\\\n"
8423                    "b\\\n"
8424                    "c\";",
8425                    Break));
8426 
8427   EXPECT_EQ("NSString *const kString =\n"
8428             "    @\"aaaa\"\n"
8429             "    @\"bbbb\";",
8430             format("NSString *const kString = @\"aaaa\"\n"
8431                    "@\"bbbb\";",
8432                    Break));
8433 
8434   Break.ColumnLimit = 0;
8435   verifyFormat("const char *hello = \"hello llvm\";", Break);
8436 }
8437 
8438 TEST_F(FormatTest, AlignsPipes) {
8439   verifyFormat(
8440       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8441       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8442       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8443   verifyFormat(
8444       "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
8445       "                     << aaaaaaaaaaaaaaaaaaaa;");
8446   verifyFormat(
8447       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8448       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8449   verifyFormat(
8450       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
8451       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8452   verifyFormat(
8453       "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
8454       "                \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
8455       "             << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
8456   verifyFormat(
8457       "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8458       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8459       "         << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8460   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8461                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8462                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8463                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
8464   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n"
8465                "             << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);");
8466   verifyFormat(
8467       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8468       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8469   verifyFormat(
8470       "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n"
8471       "                                       aaaaaaaaaaaaaaaaaaaaaaaaaa);");
8472 
8473   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
8474                "             << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
8475   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8476                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8477                "                    aaaaaaaaaaaaaaaaaaaaa)\n"
8478                "             << aaaaaaaaaaaaaaaaaaaaaaaaaa;");
8479   verifyFormat("LOG_IF(aaa == //\n"
8480                "       bbb)\n"
8481                "    << a << b;");
8482 
8483   // But sometimes, breaking before the first "<<" is desirable.
8484   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
8485                "    << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
8486   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
8487                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8488                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8489   verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
8490                "    << BEF << IsTemplate << Description << E->getType();");
8491   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
8492                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8493                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8494   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
8495                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8496                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8497                "    << aaa;");
8498 
8499   verifyFormat(
8500       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8501       "                    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
8502 
8503   // Incomplete string literal.
8504   EXPECT_EQ("llvm::errs() << \"\n"
8505             "             << a;",
8506             format("llvm::errs() << \"\n<<a;"));
8507 
8508   verifyFormat("void f() {\n"
8509                "  CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
8510                "      << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
8511                "}");
8512 
8513   // Handle 'endl'.
8514   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n"
8515                "             << bbbbbbbbbbbbbbbbbbbbbb << endl;");
8516   verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;");
8517 
8518   // Handle '\n'.
8519   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n"
8520                "             << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
8521   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n"
8522                "             << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';");
8523   verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n"
8524                "             << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";");
8525   verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
8526 }
8527 
8528 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) {
8529   verifyFormat("return out << \"somepacket = {\\n\"\n"
8530                "           << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
8531                "           << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
8532                "           << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
8533                "           << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
8534                "           << \"}\";");
8535 
8536   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
8537                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
8538                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
8539   verifyFormat(
8540       "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
8541       "             << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
8542       "             << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
8543       "             << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
8544       "             << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
8545   verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
8546                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
8547   verifyFormat(
8548       "void f() {\n"
8549       "  llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
8550       "               << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
8551       "}");
8552 
8553   // Breaking before the first "<<" is generally not desirable.
8554   verifyFormat(
8555       "llvm::errs()\n"
8556       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8557       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8558       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8559       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
8560       getLLVMStyleWithColumns(70));
8561   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
8562                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8563                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
8564                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8565                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
8566                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
8567                getLLVMStyleWithColumns(70));
8568 
8569   verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
8570                "           \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
8571                "           \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;");
8572   verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
8573                "                  \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
8574                "                  \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);");
8575   verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n"
8576                "           (aaaa + aaaa);",
8577                getLLVMStyleWithColumns(40));
8578   verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n"
8579                "                  (aaaaaaa + aaaaa));",
8580                getLLVMStyleWithColumns(40));
8581   verifyFormat(
8582       "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n"
8583       "                  SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n"
8584       "                  bbbbbbbbbbbbbbbbbbbbbbb);");
8585 }
8586 
8587 TEST_F(FormatTest, UnderstandsEquals) {
8588   verifyFormat(
8589       "aaaaaaaaaaaaaaaaa =\n"
8590       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8591   verifyFormat(
8592       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
8593       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
8594   verifyFormat(
8595       "if (a) {\n"
8596       "  f();\n"
8597       "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
8598       "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
8599       "}");
8600 
8601   verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
8602                "        100000000 + 10000000) {\n}");
8603 }
8604 
8605 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
8606   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
8607                "    .looooooooooooooooooooooooooooooooooooooongFunction();");
8608 
8609   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
8610                "    ->looooooooooooooooooooooooooooooooooooooongFunction();");
8611 
8612   verifyFormat(
8613       "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
8614       "                                                          Parameter2);");
8615 
8616   verifyFormat(
8617       "ShortObject->shortFunction(\n"
8618       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
8619       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
8620 
8621   verifyFormat("loooooooooooooongFunction(\n"
8622                "    LoooooooooooooongObject->looooooooooooooooongFunction());");
8623 
8624   verifyFormat(
8625       "function(LoooooooooooooooooooooooooooooooooooongObject\n"
8626       "             ->loooooooooooooooooooooooooooooooooooooooongFunction());");
8627 
8628   verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
8629                "    .WillRepeatedly(Return(SomeValue));");
8630   verifyFormat("void f() {\n"
8631                "  EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
8632                "      .Times(2)\n"
8633                "      .WillRepeatedly(Return(SomeValue));\n"
8634                "}");
8635   verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
8636                "    ccccccccccccccccccccccc);");
8637   verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8638                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8639                "          .aaaaa(aaaaa),\n"
8640                "      aaaaaaaaaaaaaaaaaaaaa);");
8641   verifyFormat("void f() {\n"
8642                "  aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8643                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
8644                "}");
8645   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8646                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8647                "    .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8648                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8649                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
8650   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8651                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8652                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8653                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
8654                "}");
8655 
8656   // Here, it is not necessary to wrap at "." or "->".
8657   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
8658                "    aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
8659   verifyFormat(
8660       "aaaaaaaaaaa->aaaaaaaaa(\n"
8661       "    aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8662       "    aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
8663 
8664   verifyFormat(
8665       "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8666       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
8667   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
8668                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
8669   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
8670                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
8671 
8672   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8673                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8674                "    .a();");
8675 
8676   FormatStyle NoBinPacking = getLLVMStyle();
8677   NoBinPacking.BinPackParameters = false;
8678   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
8679                "    .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
8680                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
8681                "                         aaaaaaaaaaaaaaaaaaa,\n"
8682                "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
8683                NoBinPacking);
8684 
8685   // If there is a subsequent call, change to hanging indentation.
8686   verifyFormat(
8687       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8688       "                         aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
8689       "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
8690   verifyFormat(
8691       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8692       "    aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
8693   verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8694                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8695                "                 .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
8696   verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8697                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8698                "               .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
8699 }
8700 
8701 TEST_F(FormatTest, WrapsTemplateDeclarations) {
8702   verifyFormat("template <typename T>\n"
8703                "virtual void loooooooooooongFunction(int Param1, int Param2);");
8704   verifyFormat("template <typename T>\n"
8705                "// T should be one of {A, B}.\n"
8706                "virtual void loooooooooooongFunction(int Param1, int Param2);");
8707   verifyFormat(
8708       "template <typename T>\n"
8709       "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
8710   verifyFormat("template <typename T>\n"
8711                "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
8712                "       int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
8713   verifyFormat(
8714       "template <typename T>\n"
8715       "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
8716       "                                      int Paaaaaaaaaaaaaaaaaaaaram2);");
8717   verifyFormat(
8718       "template <typename T>\n"
8719       "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
8720       "                    aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
8721       "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8722   verifyFormat("template <typename T>\n"
8723                "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8724                "    int aaaaaaaaaaaaaaaaaaaaaa);");
8725   verifyFormat(
8726       "template <typename T1, typename T2 = char, typename T3 = char,\n"
8727       "          typename T4 = char>\n"
8728       "void f();");
8729   verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
8730                "          template <typename> class cccccccccccccccccccccc,\n"
8731                "          typename ddddddddddddd>\n"
8732                "class C {};");
8733   verifyFormat(
8734       "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
8735       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8736 
8737   verifyFormat("void f() {\n"
8738                "  a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
8739                "      a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
8740                "}");
8741 
8742   verifyFormat("template <typename T> class C {};");
8743   verifyFormat("template <typename T> void f();");
8744   verifyFormat("template <typename T> void f() {}");
8745   verifyFormat(
8746       "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
8747       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8748       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
8749       "    new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
8750       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8751       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
8752       "        bbbbbbbbbbbbbbbbbbbbbbbb);",
8753       getLLVMStyleWithColumns(72));
8754   EXPECT_EQ("static_cast<A< //\n"
8755             "    B> *>(\n"
8756             "\n"
8757             ");",
8758             format("static_cast<A<//\n"
8759                    "    B>*>(\n"
8760                    "\n"
8761                    "    );"));
8762   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8763                "    const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
8764 
8765   FormatStyle AlwaysBreak = getLLVMStyle();
8766   AlwaysBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
8767   verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
8768   verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
8769   verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
8770   verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8771                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
8772                "    ccccccccccccccccccccccccccccccccccccccccccccccc);");
8773   verifyFormat("template <template <typename> class Fooooooo,\n"
8774                "          template <typename> class Baaaaaaar>\n"
8775                "struct C {};",
8776                AlwaysBreak);
8777   verifyFormat("template <typename T> // T can be A, B or C.\n"
8778                "struct C {};",
8779                AlwaysBreak);
8780   verifyFormat("template <enum E> class A {\n"
8781                "public:\n"
8782                "  E *f();\n"
8783                "};");
8784 
8785   FormatStyle NeverBreak = getLLVMStyle();
8786   NeverBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_No;
8787   verifyFormat("template <typename T> class C {};", NeverBreak);
8788   verifyFormat("template <typename T> void f();", NeverBreak);
8789   verifyFormat("template <typename T> void f() {}", NeverBreak);
8790   verifyFormat("template <typename T>\nvoid foo(aaaaaaaaaaaaaaaaaaaaaaaaaa "
8791                "bbbbbbbbbbbbbbbbbbbb) {}",
8792                NeverBreak);
8793   verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8794                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
8795                "    ccccccccccccccccccccccccccccccccccccccccccccccc);",
8796                NeverBreak);
8797   verifyFormat("template <template <typename> class Fooooooo,\n"
8798                "          template <typename> class Baaaaaaar>\n"
8799                "struct C {};",
8800                NeverBreak);
8801   verifyFormat("template <typename T> // T can be A, B or C.\n"
8802                "struct C {};",
8803                NeverBreak);
8804   verifyFormat("template <enum E> class A {\n"
8805                "public:\n"
8806                "  E *f();\n"
8807                "};",
8808                NeverBreak);
8809   NeverBreak.PenaltyBreakTemplateDeclaration = 100;
8810   verifyFormat("template <typename T> void\nfoo(aaaaaaaaaaaaaaaaaaaaaaaaaa "
8811                "bbbbbbbbbbbbbbbbbbbb) {}",
8812                NeverBreak);
8813 }
8814 
8815 TEST_F(FormatTest, WrapsTemplateDeclarationsWithComments) {
8816   FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp);
8817   Style.ColumnLimit = 60;
8818   EXPECT_EQ("// Baseline - no comments.\n"
8819             "template <\n"
8820             "    typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n"
8821             "void f() {}",
8822             format("// Baseline - no comments.\n"
8823                    "template <\n"
8824                    "    typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n"
8825                    "void f() {}",
8826                    Style));
8827 
8828   EXPECT_EQ("template <\n"
8829             "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value>  // trailing\n"
8830             "void f() {}",
8831             format("template <\n"
8832                    "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
8833                    "void f() {}",
8834                    Style));
8835 
8836   EXPECT_EQ(
8837       "template <\n"
8838       "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n"
8839       "void f() {}",
8840       format("template <typename aaaaaaaaaa<bbbbbbbbbbbb>::value>  /* line */\n"
8841              "void f() {}",
8842              Style));
8843 
8844   EXPECT_EQ(
8845       "template <\n"
8846       "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value>  // trailing\n"
8847       "                                               // multiline\n"
8848       "void f() {}",
8849       format("template <\n"
8850              "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
8851              "                                              // multiline\n"
8852              "void f() {}",
8853              Style));
8854 
8855   EXPECT_EQ(
8856       "template <typename aaaaaaaaaa<\n"
8857       "    bbbbbbbbbbbb>::value>  // trailing loooong\n"
8858       "void f() {}",
8859       format(
8860           "template <\n"
8861           "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing loooong\n"
8862           "void f() {}",
8863           Style));
8864 }
8865 
8866 TEST_F(FormatTest, WrapsTemplateParameters) {
8867   FormatStyle Style = getLLVMStyle();
8868   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
8869   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
8870   verifyFormat(
8871       "template <typename... a> struct q {};\n"
8872       "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
8873       "    aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
8874       "    y;",
8875       Style);
8876   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
8877   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
8878   verifyFormat(
8879       "template <typename... a> struct r {};\n"
8880       "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
8881       "    aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
8882       "    y;",
8883       Style);
8884   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
8885   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
8886   verifyFormat("template <typename... a> struct s {};\n"
8887                "extern s<\n"
8888                "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
8889                "aaaaaaaaaaaaaaaaaaaaaa,\n"
8890                "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
8891                "aaaaaaaaaaaaaaaaaaaaaa>\n"
8892                "    y;",
8893                Style);
8894   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
8895   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
8896   verifyFormat("template <typename... a> struct t {};\n"
8897                "extern t<\n"
8898                "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
8899                "aaaaaaaaaaaaaaaaaaaaaa,\n"
8900                "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
8901                "aaaaaaaaaaaaaaaaaaaaaa>\n"
8902                "    y;",
8903                Style);
8904 }
8905 
8906 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
8907   verifyFormat(
8908       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
8909       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
8910   verifyFormat(
8911       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
8912       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8913       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
8914 
8915   // FIXME: Should we have the extra indent after the second break?
8916   verifyFormat(
8917       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
8918       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
8919       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
8920 
8921   verifyFormat(
8922       "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
8923       "                    cccccccccccccccccccccccccccccccccccccccccccccc());");
8924 
8925   // Breaking at nested name specifiers is generally not desirable.
8926   verifyFormat(
8927       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8928       "    aaaaaaaaaaaaaaaaaaaaaaa);");
8929 
8930   verifyFormat("aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n"
8931                "                   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
8932                "                       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8933                "                   aaaaaaaaaaaaaaaaaaaaa);",
8934                getLLVMStyleWithColumns(74));
8935 
8936   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
8937                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8938                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
8939 }
8940 
8941 TEST_F(FormatTest, UnderstandsTemplateParameters) {
8942   verifyFormat("A<int> a;");
8943   verifyFormat("A<A<A<int>>> a;");
8944   verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
8945   verifyFormat("bool x = a < 1 || 2 > a;");
8946   verifyFormat("bool x = 5 < f<int>();");
8947   verifyFormat("bool x = f<int>() > 5;");
8948   verifyFormat("bool x = 5 < a<int>::x;");
8949   verifyFormat("bool x = a < 4 ? a > 2 : false;");
8950   verifyFormat("bool x = f() ? a < 2 : a > 2;");
8951 
8952   verifyGoogleFormat("A<A<int>> a;");
8953   verifyGoogleFormat("A<A<A<int>>> a;");
8954   verifyGoogleFormat("A<A<A<A<int>>>> a;");
8955   verifyGoogleFormat("A<A<int> > a;");
8956   verifyGoogleFormat("A<A<A<int> > > a;");
8957   verifyGoogleFormat("A<A<A<A<int> > > > a;");
8958   verifyGoogleFormat("A<::A<int>> a;");
8959   verifyGoogleFormat("A<::A> a;");
8960   verifyGoogleFormat("A< ::A> a;");
8961   verifyGoogleFormat("A< ::A<int> > a;");
8962   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
8963   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
8964   EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle()));
8965   EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle()));
8966   EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };",
8967             format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle()));
8968 
8969   verifyFormat("A<A<int>> a;", getChromiumStyle(FormatStyle::LK_Cpp));
8970 
8971   // template closer followed by a token that starts with > or =
8972   verifyFormat("bool b = a<1> > 1;");
8973   verifyFormat("bool b = a<1> >= 1;");
8974   verifyFormat("int i = a<1> >> 1;");
8975   FormatStyle Style = getLLVMStyle();
8976   Style.SpaceBeforeAssignmentOperators = false;
8977   verifyFormat("bool b= a<1> == 1;", Style);
8978   verifyFormat("a<int> = 1;", Style);
8979   verifyFormat("a<int> >>= 1;", Style);
8980 
8981   verifyFormat("test < a | b >> c;");
8982   verifyFormat("test<test<a | b>> c;");
8983   verifyFormat("test >> a >> b;");
8984   verifyFormat("test << a >> b;");
8985 
8986   verifyFormat("f<int>();");
8987   verifyFormat("template <typename T> void f() {}");
8988   verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
8989   verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : "
8990                "sizeof(char)>::type>;");
8991   verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};");
8992   verifyFormat("f(a.operator()<A>());");
8993   verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8994                "      .template operator()<A>());",
8995                getLLVMStyleWithColumns(35));
8996 
8997   // Not template parameters.
8998   verifyFormat("return a < b && c > d;");
8999   verifyFormat("void f() {\n"
9000                "  while (a < b && c > d) {\n"
9001                "  }\n"
9002                "}");
9003   verifyFormat("template <typename... Types>\n"
9004                "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
9005 
9006   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9007                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
9008                getLLVMStyleWithColumns(60));
9009   verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
9010   verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
9011   verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <");
9012   verifyFormat("some_templated_type<decltype([](int i) { return i; })>");
9013 }
9014 
9015 TEST_F(FormatTest, UnderstandsShiftOperators) {
9016   verifyFormat("if (i < x >> 1)");
9017   verifyFormat("while (i < x >> 1)");
9018   verifyFormat("for (unsigned i = 0; i < i; ++i, v = v >> 1)");
9019   verifyFormat("for (unsigned i = 0; i < x >> 1; ++i, v = v >> 1)");
9020   verifyFormat(
9021       "for (std::vector<int>::iterator i = 0; i < x >> 1; ++i, v = v >> 1)");
9022   verifyFormat("Foo.call<Bar<Function>>()");
9023   verifyFormat("if (Foo.call<Bar<Function>>() == 0)");
9024   verifyFormat("for (std::vector<std::pair<int>>::iterator i = 0; i < x >> 1; "
9025                "++i, v = v >> 1)");
9026   verifyFormat("if (w<u<v<x>>, 1>::t)");
9027 }
9028 
9029 TEST_F(FormatTest, BitshiftOperatorWidth) {
9030   EXPECT_EQ("int a = 1 << 2; /* foo\n"
9031             "                   bar */",
9032             format("int    a=1<<2;  /* foo\n"
9033                    "                   bar */"));
9034 
9035   EXPECT_EQ("int b = 256 >> 1; /* foo\n"
9036             "                     bar */",
9037             format("int  b  =256>>1 ;  /* foo\n"
9038                    "                      bar */"));
9039 }
9040 
9041 TEST_F(FormatTest, UnderstandsBinaryOperators) {
9042   verifyFormat("COMPARE(a, ==, b);");
9043   verifyFormat("auto s = sizeof...(Ts) - 1;");
9044 }
9045 
9046 TEST_F(FormatTest, UnderstandsPointersToMembers) {
9047   verifyFormat("int A::*x;");
9048   verifyFormat("int (S::*func)(void *);");
9049   verifyFormat("void f() { int (S::*func)(void *); }");
9050   verifyFormat("typedef bool *(Class::*Member)() const;");
9051   verifyFormat("void f() {\n"
9052                "  (a->*f)();\n"
9053                "  a->*x;\n"
9054                "  (a.*f)();\n"
9055                "  ((*a).*f)();\n"
9056                "  a.*x;\n"
9057                "}");
9058   verifyFormat("void f() {\n"
9059                "  (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
9060                "      aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
9061                "}");
9062   verifyFormat(
9063       "(aaaaaaaaaa->*bbbbbbb)(\n"
9064       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
9065   FormatStyle Style = getLLVMStyle();
9066   Style.PointerAlignment = FormatStyle::PAS_Left;
9067   verifyFormat("typedef bool* (Class::*Member)() const;", Style);
9068 }
9069 
9070 TEST_F(FormatTest, UnderstandsUnaryOperators) {
9071   verifyFormat("int a = -2;");
9072   verifyFormat("f(-1, -2, -3);");
9073   verifyFormat("a[-1] = 5;");
9074   verifyFormat("int a = 5 + -2;");
9075   verifyFormat("if (i == -1) {\n}");
9076   verifyFormat("if (i != -1) {\n}");
9077   verifyFormat("if (i > -1) {\n}");
9078   verifyFormat("if (i < -1) {\n}");
9079   verifyFormat("++(a->f());");
9080   verifyFormat("--(a->f());");
9081   verifyFormat("(a->f())++;");
9082   verifyFormat("a[42]++;");
9083   verifyFormat("if (!(a->f())) {\n}");
9084   verifyFormat("if (!+i) {\n}");
9085   verifyFormat("~&a;");
9086 
9087   verifyFormat("a-- > b;");
9088   verifyFormat("b ? -a : c;");
9089   verifyFormat("n * sizeof char16;");
9090   verifyFormat("n * alignof char16;", getGoogleStyle());
9091   verifyFormat("sizeof(char);");
9092   verifyFormat("alignof(char);", getGoogleStyle());
9093 
9094   verifyFormat("return -1;");
9095   verifyFormat("throw -1;");
9096   verifyFormat("switch (a) {\n"
9097                "case -1:\n"
9098                "  break;\n"
9099                "}");
9100   verifyFormat("#define X -1");
9101   verifyFormat("#define X -kConstant");
9102 
9103   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
9104   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
9105 
9106   verifyFormat("int a = /* confusing comment */ -1;");
9107   // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
9108   verifyFormat("int a = i /* confusing comment */++;");
9109 
9110   verifyFormat("co_yield -1;");
9111   verifyFormat("co_return -1;");
9112 
9113   // Check that * is not treated as a binary operator when we set
9114   // PointerAlignment as PAS_Left after a keyword and not a declaration.
9115   FormatStyle PASLeftStyle = getLLVMStyle();
9116   PASLeftStyle.PointerAlignment = FormatStyle::PAS_Left;
9117   verifyFormat("co_return *a;", PASLeftStyle);
9118   verifyFormat("co_await *a;", PASLeftStyle);
9119   verifyFormat("co_yield *a", PASLeftStyle);
9120   verifyFormat("return *a;", PASLeftStyle);
9121 }
9122 
9123 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) {
9124   verifyFormat("if (!aaaaaaaaaa( // break\n"
9125                "        aaaaa)) {\n"
9126                "}");
9127   verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
9128                "    aaaaa));");
9129   verifyFormat("*aaa = aaaaaaa( // break\n"
9130                "    bbbbbb);");
9131 }
9132 
9133 TEST_F(FormatTest, UnderstandsOverloadedOperators) {
9134   verifyFormat("bool operator<();");
9135   verifyFormat("bool operator>();");
9136   verifyFormat("bool operator=();");
9137   verifyFormat("bool operator==();");
9138   verifyFormat("bool operator!=();");
9139   verifyFormat("int operator+();");
9140   verifyFormat("int operator++();");
9141   verifyFormat("int operator++(int) volatile noexcept;");
9142   verifyFormat("bool operator,();");
9143   verifyFormat("bool operator();");
9144   verifyFormat("bool operator()();");
9145   verifyFormat("bool operator[]();");
9146   verifyFormat("operator bool();");
9147   verifyFormat("operator int();");
9148   verifyFormat("operator void *();");
9149   verifyFormat("operator SomeType<int>();");
9150   verifyFormat("operator SomeType<int, int>();");
9151   verifyFormat("operator SomeType<SomeType<int>>();");
9152   verifyFormat("void *operator new(std::size_t size);");
9153   verifyFormat("void *operator new[](std::size_t size);");
9154   verifyFormat("void operator delete(void *ptr);");
9155   verifyFormat("void operator delete[](void *ptr);");
9156   verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
9157                "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
9158   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n"
9159                "    aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;");
9160 
9161   verifyFormat(
9162       "ostream &operator<<(ostream &OutputStream,\n"
9163       "                    SomeReallyLongType WithSomeReallyLongValue);");
9164   verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
9165                "               const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
9166                "  return left.group < right.group;\n"
9167                "}");
9168   verifyFormat("SomeType &operator=(const SomeType &S);");
9169   verifyFormat("f.template operator()<int>();");
9170 
9171   verifyGoogleFormat("operator void*();");
9172   verifyGoogleFormat("operator SomeType<SomeType<int>>();");
9173   verifyGoogleFormat("operator ::A();");
9174 
9175   verifyFormat("using A::operator+;");
9176   verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n"
9177                "int i;");
9178 
9179   // Calling an operator as a member function.
9180   verifyFormat("void f() { a.operator*(); }");
9181   verifyFormat("void f() { a.operator*(b & b); }");
9182   verifyFormat("void f() { a->operator&(a * b); }");
9183   verifyFormat("void f() { NS::a.operator+(*b * *b); }");
9184   // TODO: Calling an operator as a non-member function is hard to distinguish.
9185   // https://llvm.org/PR50629
9186   // verifyFormat("void f() { operator*(a & a); }");
9187   // verifyFormat("void f() { operator&(a, b * b); }");
9188 
9189   verifyFormat("::operator delete(foo);");
9190   verifyFormat("::operator new(n * sizeof(foo));");
9191   verifyFormat("foo() { ::operator delete(foo); }");
9192   verifyFormat("foo() { ::operator new(n * sizeof(foo)); }");
9193 }
9194 
9195 TEST_F(FormatTest, UnderstandsFunctionRefQualification) {
9196   verifyFormat("Deleted &operator=(const Deleted &) & = default;");
9197   verifyFormat("Deleted &operator=(const Deleted &) && = delete;");
9198   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;");
9199   verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;");
9200   verifyFormat("Deleted &operator=(const Deleted &) &;");
9201   verifyFormat("Deleted &operator=(const Deleted &) &&;");
9202   verifyFormat("SomeType MemberFunction(const Deleted &) &;");
9203   verifyFormat("SomeType MemberFunction(const Deleted &) &&;");
9204   verifyFormat("SomeType MemberFunction(const Deleted &) && {}");
9205   verifyFormat("SomeType MemberFunction(const Deleted &) && final {}");
9206   verifyFormat("SomeType MemberFunction(const Deleted &) && override {}");
9207   verifyFormat("void Fn(T const &) const &;");
9208   verifyFormat("void Fn(T const volatile &&) const volatile &&;");
9209   verifyFormat("template <typename T>\n"
9210                "void F(T) && = delete;",
9211                getGoogleStyle());
9212 
9213   FormatStyle AlignLeft = getLLVMStyle();
9214   AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
9215   verifyFormat("void A::b() && {}", AlignLeft);
9216   verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft);
9217   verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;",
9218                AlignLeft);
9219   verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft);
9220   verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft);
9221   verifyFormat("auto Function(T t) & -> void {}", AlignLeft);
9222   verifyFormat("auto Function(T... t) & -> void {}", AlignLeft);
9223   verifyFormat("auto Function(T) & -> void {}", AlignLeft);
9224   verifyFormat("auto Function(T) & -> void;", AlignLeft);
9225   verifyFormat("void Fn(T const&) const&;", AlignLeft);
9226   verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft);
9227 
9228   FormatStyle Spaces = getLLVMStyle();
9229   Spaces.SpacesInCStyleCastParentheses = true;
9230   verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces);
9231   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces);
9232   verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces);
9233   verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces);
9234 
9235   Spaces.SpacesInCStyleCastParentheses = false;
9236   Spaces.SpacesInParentheses = true;
9237   verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces);
9238   verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;",
9239                Spaces);
9240   verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces);
9241   verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces);
9242 
9243   FormatStyle BreakTemplate = getLLVMStyle();
9244   BreakTemplate.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
9245 
9246   verifyFormat("struct f {\n"
9247                "  template <class T>\n"
9248                "  int &foo(const std::string &str) &noexcept {}\n"
9249                "};",
9250                BreakTemplate);
9251 
9252   verifyFormat("struct f {\n"
9253                "  template <class T>\n"
9254                "  int &foo(const std::string &str) &&noexcept {}\n"
9255                "};",
9256                BreakTemplate);
9257 
9258   verifyFormat("struct f {\n"
9259                "  template <class T>\n"
9260                "  int &foo(const std::string &str) const &noexcept {}\n"
9261                "};",
9262                BreakTemplate);
9263 
9264   verifyFormat("struct f {\n"
9265                "  template <class T>\n"
9266                "  int &foo(const std::string &str) const &noexcept {}\n"
9267                "};",
9268                BreakTemplate);
9269 
9270   verifyFormat("struct f {\n"
9271                "  template <class T>\n"
9272                "  auto foo(const std::string &str) &&noexcept -> int & {}\n"
9273                "};",
9274                BreakTemplate);
9275 
9276   FormatStyle AlignLeftBreakTemplate = getLLVMStyle();
9277   AlignLeftBreakTemplate.AlwaysBreakTemplateDeclarations =
9278       FormatStyle::BTDS_Yes;
9279   AlignLeftBreakTemplate.PointerAlignment = FormatStyle::PAS_Left;
9280 
9281   verifyFormat("struct f {\n"
9282                "  template <class T>\n"
9283                "  int& foo(const std::string& str) & noexcept {}\n"
9284                "};",
9285                AlignLeftBreakTemplate);
9286 
9287   verifyFormat("struct f {\n"
9288                "  template <class T>\n"
9289                "  int& foo(const std::string& str) && noexcept {}\n"
9290                "};",
9291                AlignLeftBreakTemplate);
9292 
9293   verifyFormat("struct f {\n"
9294                "  template <class T>\n"
9295                "  int& foo(const std::string& str) const& noexcept {}\n"
9296                "};",
9297                AlignLeftBreakTemplate);
9298 
9299   verifyFormat("struct f {\n"
9300                "  template <class T>\n"
9301                "  int& foo(const std::string& str) const&& noexcept {}\n"
9302                "};",
9303                AlignLeftBreakTemplate);
9304 
9305   verifyFormat("struct f {\n"
9306                "  template <class T>\n"
9307                "  auto foo(const std::string& str) && noexcept -> int& {}\n"
9308                "};",
9309                AlignLeftBreakTemplate);
9310 
9311   // The `&` in `Type&` should not be confused with a trailing `&` of
9312   // DEPRECATED(reason) member function.
9313   verifyFormat("struct f {\n"
9314                "  template <class T>\n"
9315                "  DEPRECATED(reason)\n"
9316                "  Type &foo(arguments) {}\n"
9317                "};",
9318                BreakTemplate);
9319 
9320   verifyFormat("struct f {\n"
9321                "  template <class T>\n"
9322                "  DEPRECATED(reason)\n"
9323                "  Type& foo(arguments) {}\n"
9324                "};",
9325                AlignLeftBreakTemplate);
9326 
9327   verifyFormat("void (*foopt)(int) = &func;");
9328 }
9329 
9330 TEST_F(FormatTest, UnderstandsNewAndDelete) {
9331   verifyFormat("void f() {\n"
9332                "  A *a = new A;\n"
9333                "  A *a = new (placement) A;\n"
9334                "  delete a;\n"
9335                "  delete (A *)a;\n"
9336                "}");
9337   verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
9338                "    typename aaaaaaaaaaaaaaaaaaaaaaaa();");
9339   verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
9340                "    new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
9341                "        typename aaaaaaaaaaaaaaaaaaaaaaaa();");
9342   verifyFormat("delete[] h->p;");
9343 }
9344 
9345 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
9346   verifyFormat("int *f(int *a) {}");
9347   verifyFormat("int main(int argc, char **argv) {}");
9348   verifyFormat("Test::Test(int b) : a(b * b) {}");
9349   verifyIndependentOfContext("f(a, *a);");
9350   verifyFormat("void g() { f(*a); }");
9351   verifyIndependentOfContext("int a = b * 10;");
9352   verifyIndependentOfContext("int a = 10 * b;");
9353   verifyIndependentOfContext("int a = b * c;");
9354   verifyIndependentOfContext("int a += b * c;");
9355   verifyIndependentOfContext("int a -= b * c;");
9356   verifyIndependentOfContext("int a *= b * c;");
9357   verifyIndependentOfContext("int a /= b * c;");
9358   verifyIndependentOfContext("int a = *b;");
9359   verifyIndependentOfContext("int a = *b * c;");
9360   verifyIndependentOfContext("int a = b * *c;");
9361   verifyIndependentOfContext("int a = b * (10);");
9362   verifyIndependentOfContext("S << b * (10);");
9363   verifyIndependentOfContext("return 10 * b;");
9364   verifyIndependentOfContext("return *b * *c;");
9365   verifyIndependentOfContext("return a & ~b;");
9366   verifyIndependentOfContext("f(b ? *c : *d);");
9367   verifyIndependentOfContext("int a = b ? *c : *d;");
9368   verifyIndependentOfContext("*b = a;");
9369   verifyIndependentOfContext("a * ~b;");
9370   verifyIndependentOfContext("a * !b;");
9371   verifyIndependentOfContext("a * +b;");
9372   verifyIndependentOfContext("a * -b;");
9373   verifyIndependentOfContext("a * ++b;");
9374   verifyIndependentOfContext("a * --b;");
9375   verifyIndependentOfContext("a[4] * b;");
9376   verifyIndependentOfContext("a[a * a] = 1;");
9377   verifyIndependentOfContext("f() * b;");
9378   verifyIndependentOfContext("a * [self dostuff];");
9379   verifyIndependentOfContext("int x = a * (a + b);");
9380   verifyIndependentOfContext("(a *)(a + b);");
9381   verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
9382   verifyIndependentOfContext("int *pa = (int *)&a;");
9383   verifyIndependentOfContext("return sizeof(int **);");
9384   verifyIndependentOfContext("return sizeof(int ******);");
9385   verifyIndependentOfContext("return (int **&)a;");
9386   verifyIndependentOfContext("f((*PointerToArray)[10]);");
9387   verifyFormat("void f(Type (*parameter)[10]) {}");
9388   verifyFormat("void f(Type (&parameter)[10]) {}");
9389   verifyGoogleFormat("return sizeof(int**);");
9390   verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
9391   verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
9392   verifyFormat("auto a = [](int **&, int ***) {};");
9393   verifyFormat("auto PointerBinding = [](const char *S) {};");
9394   verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
9395   verifyFormat("[](const decltype(*a) &value) {}");
9396   verifyFormat("[](const typeof(*a) &value) {}");
9397   verifyFormat("[](const _Atomic(a *) &value) {}");
9398   verifyFormat("[](const __underlying_type(a) &value) {}");
9399   verifyFormat("decltype(a * b) F();");
9400   verifyFormat("typeof(a * b) F();");
9401   verifyFormat("#define MACRO() [](A *a) { return 1; }");
9402   verifyFormat("Constructor() : member([](A *a, B *b) {}) {}");
9403   verifyIndependentOfContext("typedef void (*f)(int *a);");
9404   verifyIndependentOfContext("int i{a * b};");
9405   verifyIndependentOfContext("aaa && aaa->f();");
9406   verifyIndependentOfContext("int x = ~*p;");
9407   verifyFormat("Constructor() : a(a), area(width * height) {}");
9408   verifyFormat("Constructor() : a(a), area(a, width * height) {}");
9409   verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}");
9410   verifyFormat("void f() { f(a, c * d); }");
9411   verifyFormat("void f() { f(new a(), c * d); }");
9412   verifyFormat("void f(const MyOverride &override);");
9413   verifyFormat("void f(const MyFinal &final);");
9414   verifyIndependentOfContext("bool a = f() && override.f();");
9415   verifyIndependentOfContext("bool a = f() && final.f();");
9416 
9417   verifyIndependentOfContext("InvalidRegions[*R] = 0;");
9418 
9419   verifyIndependentOfContext("A<int *> a;");
9420   verifyIndependentOfContext("A<int **> a;");
9421   verifyIndependentOfContext("A<int *, int *> a;");
9422   verifyIndependentOfContext("A<int *[]> a;");
9423   verifyIndependentOfContext(
9424       "const char *const p = reinterpret_cast<const char *const>(q);");
9425   verifyIndependentOfContext("A<int **, int **> a;");
9426   verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
9427   verifyFormat("for (char **a = b; *a; ++a) {\n}");
9428   verifyFormat("for (; a && b;) {\n}");
9429   verifyFormat("bool foo = true && [] { return false; }();");
9430 
9431   verifyFormat(
9432       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9433       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9434 
9435   verifyGoogleFormat("int const* a = &b;");
9436   verifyGoogleFormat("**outparam = 1;");
9437   verifyGoogleFormat("*outparam = a * b;");
9438   verifyGoogleFormat("int main(int argc, char** argv) {}");
9439   verifyGoogleFormat("A<int*> a;");
9440   verifyGoogleFormat("A<int**> a;");
9441   verifyGoogleFormat("A<int*, int*> a;");
9442   verifyGoogleFormat("A<int**, int**> a;");
9443   verifyGoogleFormat("f(b ? *c : *d);");
9444   verifyGoogleFormat("int a = b ? *c : *d;");
9445   verifyGoogleFormat("Type* t = **x;");
9446   verifyGoogleFormat("Type* t = *++*x;");
9447   verifyGoogleFormat("*++*x;");
9448   verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
9449   verifyGoogleFormat("Type* t = x++ * y;");
9450   verifyGoogleFormat(
9451       "const char* const p = reinterpret_cast<const char* const>(q);");
9452   verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
9453   verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
9454   verifyGoogleFormat("template <typename T>\n"
9455                      "void f(int i = 0, SomeType** temps = NULL);");
9456 
9457   FormatStyle Left = getLLVMStyle();
9458   Left.PointerAlignment = FormatStyle::PAS_Left;
9459   verifyFormat("x = *a(x) = *a(y);", Left);
9460   verifyFormat("for (;; *a = b) {\n}", Left);
9461   verifyFormat("return *this += 1;", Left);
9462   verifyFormat("throw *x;", Left);
9463   verifyFormat("delete *x;", Left);
9464   verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left);
9465   verifyFormat("[](const decltype(*a)* ptr) {}", Left);
9466   verifyFormat("[](const typeof(*a)* ptr) {}", Left);
9467   verifyFormat("[](const _Atomic(a*)* ptr) {}", Left);
9468   verifyFormat("[](const __underlying_type(a)* ptr) {}", Left);
9469   verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left);
9470   verifyFormat("auto x(A&&, B&&, C&&) -> D;", Left);
9471   verifyFormat("auto x = [](A&&, B&&, C&&) -> D {};", Left);
9472   verifyFormat("template <class T> X(T&&, T&&, T&&) -> X<T>;", Left);
9473 
9474   verifyIndependentOfContext("a = *(x + y);");
9475   verifyIndependentOfContext("a = &(x + y);");
9476   verifyIndependentOfContext("*(x + y).call();");
9477   verifyIndependentOfContext("&(x + y)->call();");
9478   verifyFormat("void f() { &(*I).first; }");
9479 
9480   verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
9481   verifyFormat("f(* /* confusing comment */ foo);");
9482   verifyFormat("void (* /*deleter*/)(const Slice &key, void *value)");
9483   verifyFormat("void foo(int * // this is the first paramters\n"
9484                "         ,\n"
9485                "         int second);");
9486   verifyFormat("double term = a * // first\n"
9487                "              b;");
9488   verifyFormat(
9489       "int *MyValues = {\n"
9490       "    *A, // Operator detection might be confused by the '{'\n"
9491       "    *BB // Operator detection might be confused by previous comment\n"
9492       "};");
9493 
9494   verifyIndependentOfContext("if (int *a = &b)");
9495   verifyIndependentOfContext("if (int &a = *b)");
9496   verifyIndependentOfContext("if (a & b[i])");
9497   verifyIndependentOfContext("if constexpr (a & b[i])");
9498   verifyIndependentOfContext("if CONSTEXPR (a & b[i])");
9499   verifyIndependentOfContext("if (a * (b * c))");
9500   verifyIndependentOfContext("if constexpr (a * (b * c))");
9501   verifyIndependentOfContext("if CONSTEXPR (a * (b * c))");
9502   verifyIndependentOfContext("if (a::b::c::d & b[i])");
9503   verifyIndependentOfContext("if (*b[i])");
9504   verifyIndependentOfContext("if (int *a = (&b))");
9505   verifyIndependentOfContext("while (int *a = &b)");
9506   verifyIndependentOfContext("while (a * (b * c))");
9507   verifyIndependentOfContext("size = sizeof *a;");
9508   verifyIndependentOfContext("if (a && (b = c))");
9509   verifyFormat("void f() {\n"
9510                "  for (const int &v : Values) {\n"
9511                "  }\n"
9512                "}");
9513   verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
9514   verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
9515   verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
9516 
9517   verifyFormat("#define A (!a * b)");
9518   verifyFormat("#define MACRO     \\\n"
9519                "  int *i = a * b; \\\n"
9520                "  void f(a *b);",
9521                getLLVMStyleWithColumns(19));
9522 
9523   verifyIndependentOfContext("A = new SomeType *[Length];");
9524   verifyIndependentOfContext("A = new SomeType *[Length]();");
9525   verifyIndependentOfContext("T **t = new T *;");
9526   verifyIndependentOfContext("T **t = new T *();");
9527   verifyGoogleFormat("A = new SomeType*[Length]();");
9528   verifyGoogleFormat("A = new SomeType*[Length];");
9529   verifyGoogleFormat("T** t = new T*;");
9530   verifyGoogleFormat("T** t = new T*();");
9531 
9532   verifyFormat("STATIC_ASSERT((a & b) == 0);");
9533   verifyFormat("STATIC_ASSERT(0 == (a & b));");
9534   verifyFormat("template <bool a, bool b> "
9535                "typename t::if<x && y>::type f() {}");
9536   verifyFormat("template <int *y> f() {}");
9537   verifyFormat("vector<int *> v;");
9538   verifyFormat("vector<int *const> v;");
9539   verifyFormat("vector<int *const **const *> v;");
9540   verifyFormat("vector<int *volatile> v;");
9541   verifyFormat("vector<a *_Nonnull> v;");
9542   verifyFormat("vector<a *_Nullable> v;");
9543   verifyFormat("vector<a *_Null_unspecified> v;");
9544   verifyFormat("vector<a *__ptr32> v;");
9545   verifyFormat("vector<a *__ptr64> v;");
9546   verifyFormat("vector<a *__capability> v;");
9547   FormatStyle TypeMacros = getLLVMStyle();
9548   TypeMacros.TypenameMacros = {"LIST"};
9549   verifyFormat("vector<LIST(uint64_t)> v;", TypeMacros);
9550   verifyFormat("vector<LIST(uint64_t) *> v;", TypeMacros);
9551   verifyFormat("vector<LIST(uint64_t) **> v;", TypeMacros);
9552   verifyFormat("vector<LIST(uint64_t) *attr> v;", TypeMacros);
9553   verifyFormat("vector<A(uint64_t) * attr> v;", TypeMacros); // multiplication
9554 
9555   FormatStyle CustomQualifier = getLLVMStyle();
9556   // Add identifiers that should not be parsed as a qualifier by default.
9557   CustomQualifier.AttributeMacros.push_back("__my_qualifier");
9558   CustomQualifier.AttributeMacros.push_back("_My_qualifier");
9559   CustomQualifier.AttributeMacros.push_back("my_other_qualifier");
9560   verifyFormat("vector<a * __my_qualifier> parse_as_multiply;");
9561   verifyFormat("vector<a *__my_qualifier> v;", CustomQualifier);
9562   verifyFormat("vector<a * _My_qualifier> parse_as_multiply;");
9563   verifyFormat("vector<a *_My_qualifier> v;", CustomQualifier);
9564   verifyFormat("vector<a * my_other_qualifier> parse_as_multiply;");
9565   verifyFormat("vector<a *my_other_qualifier> v;", CustomQualifier);
9566   verifyFormat("vector<a * _NotAQualifier> v;");
9567   verifyFormat("vector<a * __not_a_qualifier> v;");
9568   verifyFormat("vector<a * b> v;");
9569   verifyFormat("foo<b && false>();");
9570   verifyFormat("foo<b & 1>();");
9571   verifyFormat("decltype(*::std::declval<const T &>()) void F();");
9572   verifyFormat("typeof(*::std::declval<const T &>()) void F();");
9573   verifyFormat("_Atomic(*::std::declval<const T &>()) void F();");
9574   verifyFormat("__underlying_type(*::std::declval<const T &>()) void F();");
9575   verifyFormat(
9576       "template <class T, class = typename std::enable_if<\n"
9577       "                       std::is_integral<T>::value &&\n"
9578       "                       (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
9579       "void F();",
9580       getLLVMStyleWithColumns(70));
9581   verifyFormat("template <class T,\n"
9582                "          class = typename std::enable_if<\n"
9583                "              std::is_integral<T>::value &&\n"
9584                "              (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n"
9585                "          class U>\n"
9586                "void F();",
9587                getLLVMStyleWithColumns(70));
9588   verifyFormat(
9589       "template <class T,\n"
9590       "          class = typename ::std::enable_if<\n"
9591       "              ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
9592       "void F();",
9593       getGoogleStyleWithColumns(68));
9594 
9595   verifyIndependentOfContext("MACRO(int *i);");
9596   verifyIndependentOfContext("MACRO(auto *a);");
9597   verifyIndependentOfContext("MACRO(const A *a);");
9598   verifyIndependentOfContext("MACRO(_Atomic(A) *a);");
9599   verifyIndependentOfContext("MACRO(decltype(A) *a);");
9600   verifyIndependentOfContext("MACRO(typeof(A) *a);");
9601   verifyIndependentOfContext("MACRO(__underlying_type(A) *a);");
9602   verifyIndependentOfContext("MACRO(A *const a);");
9603   verifyIndependentOfContext("MACRO(A *restrict a);");
9604   verifyIndependentOfContext("MACRO(A *__restrict__ a);");
9605   verifyIndependentOfContext("MACRO(A *__restrict a);");
9606   verifyIndependentOfContext("MACRO(A *volatile a);");
9607   verifyIndependentOfContext("MACRO(A *__volatile a);");
9608   verifyIndependentOfContext("MACRO(A *__volatile__ a);");
9609   verifyIndependentOfContext("MACRO(A *_Nonnull a);");
9610   verifyIndependentOfContext("MACRO(A *_Nullable a);");
9611   verifyIndependentOfContext("MACRO(A *_Null_unspecified a);");
9612   verifyIndependentOfContext("MACRO(A *__attribute__((foo)) a);");
9613   verifyIndependentOfContext("MACRO(A *__attribute((foo)) a);");
9614   verifyIndependentOfContext("MACRO(A *[[clang::attr]] a);");
9615   verifyIndependentOfContext("MACRO(A *[[clang::attr(\"foo\")]] a);");
9616   verifyIndependentOfContext("MACRO(A *__ptr32 a);");
9617   verifyIndependentOfContext("MACRO(A *__ptr64 a);");
9618   verifyIndependentOfContext("MACRO(A *__capability);");
9619   verifyIndependentOfContext("MACRO(A &__capability);");
9620   verifyFormat("MACRO(A *__my_qualifier);");               // type declaration
9621   verifyFormat("void f() { MACRO(A * __my_qualifier); }"); // multiplication
9622   // If we add __my_qualifier to AttributeMacros it should always be parsed as
9623   // a type declaration:
9624   verifyFormat("MACRO(A *__my_qualifier);", CustomQualifier);
9625   verifyFormat("void f() { MACRO(A *__my_qualifier); }", CustomQualifier);
9626   // Also check that TypenameMacros prevents parsing it as multiplication:
9627   verifyIndependentOfContext("MACRO(LIST(uint64_t) * a);"); // multiplication
9628   verifyIndependentOfContext("MACRO(LIST(uint64_t) *a);", TypeMacros); // type
9629 
9630   verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
9631   verifyFormat("void f() { f(float{1}, a * a); }");
9632   verifyFormat("void f() { f(float(1), a * a); }");
9633 
9634   verifyFormat("f((void (*)(int))g);");
9635   verifyFormat("f((void (&)(int))g);");
9636   verifyFormat("f((void (^)(int))g);");
9637 
9638   // FIXME: Is there a way to make this work?
9639   // verifyIndependentOfContext("MACRO(A *a);");
9640   verifyFormat("MACRO(A &B);");
9641   verifyFormat("MACRO(A *B);");
9642   verifyFormat("void f() { MACRO(A * B); }");
9643   verifyFormat("void f() { MACRO(A & B); }");
9644 
9645   // This lambda was mis-formatted after D88956 (treating it as a binop):
9646   verifyFormat("auto x = [](const decltype(x) &ptr) {};");
9647   verifyFormat("auto x = [](const decltype(x) *ptr) {};");
9648   verifyFormat("#define lambda [](const decltype(x) &ptr) {}");
9649   verifyFormat("#define lambda [](const decltype(x) *ptr) {}");
9650 
9651   verifyFormat("DatumHandle const *operator->() const { return input_; }");
9652   verifyFormat("return options != nullptr && operator==(*options);");
9653 
9654   EXPECT_EQ("#define OP(x)                                    \\\n"
9655             "  ostream &operator<<(ostream &s, const A &a) {  \\\n"
9656             "    return s << a.DebugString();                 \\\n"
9657             "  }",
9658             format("#define OP(x) \\\n"
9659                    "  ostream &operator<<(ostream &s, const A &a) { \\\n"
9660                    "    return s << a.DebugString(); \\\n"
9661                    "  }",
9662                    getLLVMStyleWithColumns(50)));
9663 
9664   // FIXME: We cannot handle this case yet; we might be able to figure out that
9665   // foo<x> d > v; doesn't make sense.
9666   verifyFormat("foo<a<b && c> d> v;");
9667 
9668   FormatStyle PointerMiddle = getLLVMStyle();
9669   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
9670   verifyFormat("delete *x;", PointerMiddle);
9671   verifyFormat("int * x;", PointerMiddle);
9672   verifyFormat("int *[] x;", PointerMiddle);
9673   verifyFormat("template <int * y> f() {}", PointerMiddle);
9674   verifyFormat("int * f(int * a) {}", PointerMiddle);
9675   verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle);
9676   verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle);
9677   verifyFormat("A<int *> a;", PointerMiddle);
9678   verifyFormat("A<int **> a;", PointerMiddle);
9679   verifyFormat("A<int *, int *> a;", PointerMiddle);
9680   verifyFormat("A<int *[]> a;", PointerMiddle);
9681   verifyFormat("A = new SomeType *[Length]();", PointerMiddle);
9682   verifyFormat("A = new SomeType *[Length];", PointerMiddle);
9683   verifyFormat("T ** t = new T *;", PointerMiddle);
9684 
9685   // Member function reference qualifiers aren't binary operators.
9686   verifyFormat("string // break\n"
9687                "operator()() & {}");
9688   verifyFormat("string // break\n"
9689                "operator()() && {}");
9690   verifyGoogleFormat("template <typename T>\n"
9691                      "auto x() & -> int {}");
9692 
9693   // Should be binary operators when used as an argument expression (overloaded
9694   // operator invoked as a member function).
9695   verifyFormat("void f() { a.operator()(a * a); }");
9696   verifyFormat("void f() { a->operator()(a & a); }");
9697   verifyFormat("void f() { a.operator()(*a & *a); }");
9698   verifyFormat("void f() { a->operator()(*a * *a); }");
9699 
9700   verifyFormat("int operator()(T (&&)[N]) { return 1; }");
9701   verifyFormat("int operator()(T (&)[N]) { return 0; }");
9702 }
9703 
9704 TEST_F(FormatTest, UnderstandsAttributes) {
9705   verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
9706   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
9707                "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
9708   FormatStyle AfterType = getLLVMStyle();
9709   AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
9710   verifyFormat("__attribute__((nodebug)) void\n"
9711                "foo() {}\n",
9712                AfterType);
9713   verifyFormat("__unused void\n"
9714                "foo() {}",
9715                AfterType);
9716 
9717   FormatStyle CustomAttrs = getLLVMStyle();
9718   CustomAttrs.AttributeMacros.push_back("__unused");
9719   CustomAttrs.AttributeMacros.push_back("__attr1");
9720   CustomAttrs.AttributeMacros.push_back("__attr2");
9721   CustomAttrs.AttributeMacros.push_back("no_underscore_attr");
9722   verifyFormat("vector<SomeType *__attribute((foo))> v;");
9723   verifyFormat("vector<SomeType *__attribute__((foo))> v;");
9724   verifyFormat("vector<SomeType * __not_attribute__((foo))> v;");
9725   // Check that it is parsed as a multiplication without AttributeMacros and
9726   // as a pointer qualifier when we add __attr1/__attr2 to AttributeMacros.
9727   verifyFormat("vector<SomeType * __attr1> v;");
9728   verifyFormat("vector<SomeType __attr1 *> v;");
9729   verifyFormat("vector<SomeType __attr1 *const> v;");
9730   verifyFormat("vector<SomeType __attr1 * __attr2> v;");
9731   verifyFormat("vector<SomeType *__attr1> v;", CustomAttrs);
9732   verifyFormat("vector<SomeType *__attr2> v;", CustomAttrs);
9733   verifyFormat("vector<SomeType *no_underscore_attr> v;", CustomAttrs);
9734   verifyFormat("vector<SomeType __attr1 *> v;", CustomAttrs);
9735   verifyFormat("vector<SomeType __attr1 *const> v;", CustomAttrs);
9736   verifyFormat("vector<SomeType __attr1 *__attr2> v;", CustomAttrs);
9737   verifyFormat("vector<SomeType __attr1 *no_underscore_attr> v;", CustomAttrs);
9738 
9739   // Check that these are not parsed as function declarations:
9740   CustomAttrs.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
9741   CustomAttrs.BreakBeforeBraces = FormatStyle::BS_Allman;
9742   verifyFormat("SomeType s(InitValue);", CustomAttrs);
9743   verifyFormat("SomeType s{InitValue};", CustomAttrs);
9744   verifyFormat("SomeType *__unused s(InitValue);", CustomAttrs);
9745   verifyFormat("SomeType *__unused s{InitValue};", CustomAttrs);
9746   verifyFormat("SomeType s __unused(InitValue);", CustomAttrs);
9747   verifyFormat("SomeType s __unused{InitValue};", CustomAttrs);
9748   verifyFormat("SomeType *__capability s(InitValue);", CustomAttrs);
9749   verifyFormat("SomeType *__capability s{InitValue};", CustomAttrs);
9750 }
9751 
9752 TEST_F(FormatTest, UnderstandsPointerQualifiersInCast) {
9753   // Check that qualifiers on pointers don't break parsing of casts.
9754   verifyFormat("x = (foo *const)*v;");
9755   verifyFormat("x = (foo *volatile)*v;");
9756   verifyFormat("x = (foo *restrict)*v;");
9757   verifyFormat("x = (foo *__attribute__((foo)))*v;");
9758   verifyFormat("x = (foo *_Nonnull)*v;");
9759   verifyFormat("x = (foo *_Nullable)*v;");
9760   verifyFormat("x = (foo *_Null_unspecified)*v;");
9761   verifyFormat("x = (foo *_Nonnull)*v;");
9762   verifyFormat("x = (foo *[[clang::attr]])*v;");
9763   verifyFormat("x = (foo *[[clang::attr(\"foo\")]])*v;");
9764   verifyFormat("x = (foo *__ptr32)*v;");
9765   verifyFormat("x = (foo *__ptr64)*v;");
9766   verifyFormat("x = (foo *__capability)*v;");
9767 
9768   // Check that we handle multiple trailing qualifiers and skip them all to
9769   // determine that the expression is a cast to a pointer type.
9770   FormatStyle LongPointerRight = getLLVMStyleWithColumns(999);
9771   FormatStyle LongPointerLeft = getLLVMStyleWithColumns(999);
9772   LongPointerLeft.PointerAlignment = FormatStyle::PAS_Left;
9773   StringRef AllQualifiers =
9774       "const volatile restrict __attribute__((foo)) _Nonnull _Null_unspecified "
9775       "_Nonnull [[clang::attr]] __ptr32 __ptr64 __capability";
9776   verifyFormat(("x = (foo *" + AllQualifiers + ")*v;").str(), LongPointerRight);
9777   verifyFormat(("x = (foo* " + AllQualifiers + ")*v;").str(), LongPointerLeft);
9778 
9779   // Also check that address-of is not parsed as a binary bitwise-and:
9780   verifyFormat("x = (foo *const)&v;");
9781   verifyFormat(("x = (foo *" + AllQualifiers + ")&v;").str(), LongPointerRight);
9782   verifyFormat(("x = (foo* " + AllQualifiers + ")&v;").str(), LongPointerLeft);
9783 
9784   // Check custom qualifiers:
9785   FormatStyle CustomQualifier = getLLVMStyleWithColumns(999);
9786   CustomQualifier.AttributeMacros.push_back("__my_qualifier");
9787   verifyFormat("x = (foo * __my_qualifier) * v;"); // not parsed as qualifier.
9788   verifyFormat("x = (foo *__my_qualifier)*v;", CustomQualifier);
9789   verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)*v;").str(),
9790                CustomQualifier);
9791   verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)&v;").str(),
9792                CustomQualifier);
9793 
9794   // Check that unknown identifiers result in binary operator parsing:
9795   verifyFormat("x = (foo * __unknown_qualifier) * v;");
9796   verifyFormat("x = (foo * __unknown_qualifier) & v;");
9797 }
9798 
9799 TEST_F(FormatTest, UnderstandsSquareAttributes) {
9800   verifyFormat("SomeType s [[unused]] (InitValue);");
9801   verifyFormat("SomeType s [[gnu::unused]] (InitValue);");
9802   verifyFormat("SomeType s [[using gnu: unused]] (InitValue);");
9803   verifyFormat("[[gsl::suppress(\"clang-tidy-check-name\")]] void f() {}");
9804   verifyFormat("void f() [[deprecated(\"so sorry\")]];");
9805   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9806                "    [[unused]] aaaaaaaaaaaaaaaaaaaaaaa(int i);");
9807   verifyFormat("[[nodiscard]] bool f() { return false; }");
9808   verifyFormat("class [[nodiscard]] f {\npublic:\n  f() {}\n}");
9809   verifyFormat("class [[deprecated(\"so sorry\")]] f {\npublic:\n  f() {}\n}");
9810   verifyFormat("class [[gnu::unused]] f {\npublic:\n  f() {}\n}");
9811 
9812   // Make sure we do not mistake attributes for array subscripts.
9813   verifyFormat("int a() {}\n"
9814                "[[unused]] int b() {}\n");
9815   verifyFormat("NSArray *arr;\n"
9816                "arr[[Foo() bar]];");
9817 
9818   // On the other hand, we still need to correctly find array subscripts.
9819   verifyFormat("int a = std::vector<int>{1, 2, 3}[0];");
9820 
9821   // Make sure that we do not mistake Objective-C method inside array literals
9822   // as attributes, even if those method names are also keywords.
9823   verifyFormat("@[ [foo bar] ];");
9824   verifyFormat("@[ [NSArray class] ];");
9825   verifyFormat("@[ [foo enum] ];");
9826 
9827   verifyFormat("template <typename T> [[nodiscard]] int a() { return 1; }");
9828 
9829   // Make sure we do not parse attributes as lambda introducers.
9830   FormatStyle MultiLineFunctions = getLLVMStyle();
9831   MultiLineFunctions.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
9832   verifyFormat("[[unused]] int b() {\n"
9833                "  return 42;\n"
9834                "}\n",
9835                MultiLineFunctions);
9836 }
9837 
9838 TEST_F(FormatTest, AttributeClass) {
9839   FormatStyle Style = getChromiumStyle(FormatStyle::LK_Cpp);
9840   verifyFormat("class S {\n"
9841                "  S(S&&) = default;\n"
9842                "};",
9843                Style);
9844   verifyFormat("class [[nodiscard]] S {\n"
9845                "  S(S&&) = default;\n"
9846                "};",
9847                Style);
9848   verifyFormat("class __attribute((maybeunused)) S {\n"
9849                "  S(S&&) = default;\n"
9850                "};",
9851                Style);
9852   verifyFormat("struct S {\n"
9853                "  S(S&&) = default;\n"
9854                "};",
9855                Style);
9856   verifyFormat("struct [[nodiscard]] S {\n"
9857                "  S(S&&) = default;\n"
9858                "};",
9859                Style);
9860 }
9861 
9862 TEST_F(FormatTest, AttributesAfterMacro) {
9863   FormatStyle Style = getLLVMStyle();
9864   verifyFormat("MACRO;\n"
9865                "__attribute__((maybe_unused)) int foo() {\n"
9866                "  //...\n"
9867                "}");
9868 
9869   verifyFormat("MACRO;\n"
9870                "[[nodiscard]] int foo() {\n"
9871                "  //...\n"
9872                "}");
9873 
9874   EXPECT_EQ("MACRO\n\n"
9875             "__attribute__((maybe_unused)) int foo() {\n"
9876             "  //...\n"
9877             "}",
9878             format("MACRO\n\n"
9879                    "__attribute__((maybe_unused)) int foo() {\n"
9880                    "  //...\n"
9881                    "}"));
9882 
9883   EXPECT_EQ("MACRO\n\n"
9884             "[[nodiscard]] int foo() {\n"
9885             "  //...\n"
9886             "}",
9887             format("MACRO\n\n"
9888                    "[[nodiscard]] int foo() {\n"
9889                    "  //...\n"
9890                    "}"));
9891 }
9892 
9893 TEST_F(FormatTest, AttributePenaltyBreaking) {
9894   FormatStyle Style = getLLVMStyle();
9895   verifyFormat("void ABCDEFGH::ABCDEFGHIJKLMN(\n"
9896                "    [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}",
9897                Style);
9898   verifyFormat("void ABCDEFGH::ABCDEFGHIJK(\n"
9899                "    [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}",
9900                Style);
9901   verifyFormat("void ABCDEFGH::ABCDEFGH([[maybe_unused]] const "
9902                "shared_ptr<ALongTypeName> &C d) {\n}",
9903                Style);
9904 }
9905 
9906 TEST_F(FormatTest, UnderstandsEllipsis) {
9907   FormatStyle Style = getLLVMStyle();
9908   verifyFormat("int printf(const char *fmt, ...);");
9909   verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
9910   verifyFormat("template <class... Ts> void Foo(Ts *...ts) {}");
9911 
9912   verifyFormat("template <int *...PP> a;", Style);
9913 
9914   Style.PointerAlignment = FormatStyle::PAS_Left;
9915   verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", Style);
9916 
9917   verifyFormat("template <int*... PP> a;", Style);
9918 
9919   Style.PointerAlignment = FormatStyle::PAS_Middle;
9920   verifyFormat("template <int *... PP> a;", Style);
9921 }
9922 
9923 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
9924   EXPECT_EQ("int *a;\n"
9925             "int *a;\n"
9926             "int *a;",
9927             format("int *a;\n"
9928                    "int* a;\n"
9929                    "int *a;",
9930                    getGoogleStyle()));
9931   EXPECT_EQ("int* a;\n"
9932             "int* a;\n"
9933             "int* a;",
9934             format("int* a;\n"
9935                    "int* a;\n"
9936                    "int *a;",
9937                    getGoogleStyle()));
9938   EXPECT_EQ("int *a;\n"
9939             "int *a;\n"
9940             "int *a;",
9941             format("int *a;\n"
9942                    "int * a;\n"
9943                    "int *  a;",
9944                    getGoogleStyle()));
9945   EXPECT_EQ("auto x = [] {\n"
9946             "  int *a;\n"
9947             "  int *a;\n"
9948             "  int *a;\n"
9949             "};",
9950             format("auto x=[]{int *a;\n"
9951                    "int * a;\n"
9952                    "int *  a;};",
9953                    getGoogleStyle()));
9954 }
9955 
9956 TEST_F(FormatTest, UnderstandsRvalueReferences) {
9957   verifyFormat("int f(int &&a) {}");
9958   verifyFormat("int f(int a, char &&b) {}");
9959   verifyFormat("void f() { int &&a = b; }");
9960   verifyGoogleFormat("int f(int a, char&& b) {}");
9961   verifyGoogleFormat("void f() { int&& a = b; }");
9962 
9963   verifyIndependentOfContext("A<int &&> a;");
9964   verifyIndependentOfContext("A<int &&, int &&> a;");
9965   verifyGoogleFormat("A<int&&> a;");
9966   verifyGoogleFormat("A<int&&, int&&> a;");
9967 
9968   // Not rvalue references:
9969   verifyFormat("template <bool B, bool C> class A {\n"
9970                "  static_assert(B && C, \"Something is wrong\");\n"
9971                "};");
9972   verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
9973   verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
9974   verifyFormat("#define A(a, b) (a && b)");
9975 }
9976 
9977 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
9978   verifyFormat("void f() {\n"
9979                "  x[aaaaaaaaa -\n"
9980                "    b] = 23;\n"
9981                "}",
9982                getLLVMStyleWithColumns(15));
9983 }
9984 
9985 TEST_F(FormatTest, FormatsCasts) {
9986   verifyFormat("Type *A = static_cast<Type *>(P);");
9987   verifyFormat("Type *A = (Type *)P;");
9988   verifyFormat("Type *A = (vector<Type *, int *>)P;");
9989   verifyFormat("int a = (int)(2.0f);");
9990   verifyFormat("int a = (int)2.0f;");
9991   verifyFormat("x[(int32)y];");
9992   verifyFormat("x = (int32)y;");
9993   verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
9994   verifyFormat("int a = (int)*b;");
9995   verifyFormat("int a = (int)2.0f;");
9996   verifyFormat("int a = (int)~0;");
9997   verifyFormat("int a = (int)++a;");
9998   verifyFormat("int a = (int)sizeof(int);");
9999   verifyFormat("int a = (int)+2;");
10000   verifyFormat("my_int a = (my_int)2.0f;");
10001   verifyFormat("my_int a = (my_int)sizeof(int);");
10002   verifyFormat("return (my_int)aaa;");
10003   verifyFormat("#define x ((int)-1)");
10004   verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
10005   verifyFormat("#define p(q) ((int *)&q)");
10006   verifyFormat("fn(a)(b) + 1;");
10007 
10008   verifyFormat("void f() { my_int a = (my_int)*b; }");
10009   verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
10010   verifyFormat("my_int a = (my_int)~0;");
10011   verifyFormat("my_int a = (my_int)++a;");
10012   verifyFormat("my_int a = (my_int)-2;");
10013   verifyFormat("my_int a = (my_int)1;");
10014   verifyFormat("my_int a = (my_int *)1;");
10015   verifyFormat("my_int a = (const my_int)-1;");
10016   verifyFormat("my_int a = (const my_int *)-1;");
10017   verifyFormat("my_int a = (my_int)(my_int)-1;");
10018   verifyFormat("my_int a = (ns::my_int)-2;");
10019   verifyFormat("case (my_int)ONE:");
10020   verifyFormat("auto x = (X)this;");
10021   // Casts in Obj-C style calls used to not be recognized as such.
10022   verifyFormat("int a = [(type*)[((type*)val) arg] arg];", getGoogleStyle());
10023 
10024   // FIXME: single value wrapped with paren will be treated as cast.
10025   verifyFormat("void f(int i = (kValue)*kMask) {}");
10026 
10027   verifyFormat("{ (void)F; }");
10028 
10029   // Don't break after a cast's
10030   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
10031                "    (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
10032                "                                   bbbbbbbbbbbbbbbbbbbbbb);");
10033 
10034   // These are not casts.
10035   verifyFormat("void f(int *) {}");
10036   verifyFormat("f(foo)->b;");
10037   verifyFormat("f(foo).b;");
10038   verifyFormat("f(foo)(b);");
10039   verifyFormat("f(foo)[b];");
10040   verifyFormat("[](foo) { return 4; }(bar);");
10041   verifyFormat("(*funptr)(foo)[4];");
10042   verifyFormat("funptrs[4](foo)[4];");
10043   verifyFormat("void f(int *);");
10044   verifyFormat("void f(int *) = 0;");
10045   verifyFormat("void f(SmallVector<int>) {}");
10046   verifyFormat("void f(SmallVector<int>);");
10047   verifyFormat("void f(SmallVector<int>) = 0;");
10048   verifyFormat("void f(int i = (kA * kB) & kMask) {}");
10049   verifyFormat("int a = sizeof(int) * b;");
10050   verifyFormat("int a = alignof(int) * b;", getGoogleStyle());
10051   verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
10052   verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
10053   verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
10054 
10055   // These are not casts, but at some point were confused with casts.
10056   verifyFormat("virtual void foo(int *) override;");
10057   verifyFormat("virtual void foo(char &) const;");
10058   verifyFormat("virtual void foo(int *a, char *) const;");
10059   verifyFormat("int a = sizeof(int *) + b;");
10060   verifyFormat("int a = alignof(int *) + b;", getGoogleStyle());
10061   verifyFormat("bool b = f(g<int>) && c;");
10062   verifyFormat("typedef void (*f)(int i) func;");
10063   verifyFormat("void operator++(int) noexcept;");
10064   verifyFormat("void operator++(int &) noexcept;");
10065   verifyFormat("void operator delete(void *, std::size_t, const std::nothrow_t "
10066                "&) noexcept;");
10067   verifyFormat(
10068       "void operator delete(std::size_t, const std::nothrow_t &) noexcept;");
10069   verifyFormat("void operator delete(const std::nothrow_t &) noexcept;");
10070   verifyFormat("void operator delete(std::nothrow_t &) noexcept;");
10071   verifyFormat("void operator delete(nothrow_t &) noexcept;");
10072   verifyFormat("void operator delete(foo &) noexcept;");
10073   verifyFormat("void operator delete(foo) noexcept;");
10074   verifyFormat("void operator delete(int) noexcept;");
10075   verifyFormat("void operator delete(int &) noexcept;");
10076   verifyFormat("void operator delete(int &) volatile noexcept;");
10077   verifyFormat("void operator delete(int &) const");
10078   verifyFormat("void operator delete(int &) = default");
10079   verifyFormat("void operator delete(int &) = delete");
10080   verifyFormat("void operator delete(int &) [[noreturn]]");
10081   verifyFormat("void operator delete(int &) throw();");
10082   verifyFormat("void operator delete(int &) throw(int);");
10083   verifyFormat("auto operator delete(int &) -> int;");
10084   verifyFormat("auto operator delete(int &) override");
10085   verifyFormat("auto operator delete(int &) final");
10086 
10087   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
10088                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
10089   // FIXME: The indentation here is not ideal.
10090   verifyFormat(
10091       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10092       "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
10093       "        [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
10094 }
10095 
10096 TEST_F(FormatTest, FormatsFunctionTypes) {
10097   verifyFormat("A<bool()> a;");
10098   verifyFormat("A<SomeType()> a;");
10099   verifyFormat("A<void (*)(int, std::string)> a;");
10100   verifyFormat("A<void *(int)>;");
10101   verifyFormat("void *(*a)(int *, SomeType *);");
10102   verifyFormat("int (*func)(void *);");
10103   verifyFormat("void f() { int (*func)(void *); }");
10104   verifyFormat("template <class CallbackClass>\n"
10105                "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
10106 
10107   verifyGoogleFormat("A<void*(int*, SomeType*)>;");
10108   verifyGoogleFormat("void* (*a)(int);");
10109   verifyGoogleFormat(
10110       "template <class CallbackClass>\n"
10111       "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
10112 
10113   // Other constructs can look somewhat like function types:
10114   verifyFormat("A<sizeof(*x)> a;");
10115   verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
10116   verifyFormat("some_var = function(*some_pointer_var)[0];");
10117   verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
10118   verifyFormat("int x = f(&h)();");
10119   verifyFormat("returnsFunction(&param1, &param2)(param);");
10120   verifyFormat("std::function<\n"
10121                "    LooooooooooongTemplatedType<\n"
10122                "        SomeType>*(\n"
10123                "        LooooooooooooooooongType type)>\n"
10124                "    function;",
10125                getGoogleStyleWithColumns(40));
10126 }
10127 
10128 TEST_F(FormatTest, FormatsPointersToArrayTypes) {
10129   verifyFormat("A (*foo_)[6];");
10130   verifyFormat("vector<int> (*foo_)[6];");
10131 }
10132 
10133 TEST_F(FormatTest, BreaksLongVariableDeclarations) {
10134   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
10135                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
10136   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
10137                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
10138   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
10139                "    *LoooooooooooooooooooooooooooooooooooooooongVariable;");
10140 
10141   // Different ways of ()-initializiation.
10142   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
10143                "    LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
10144   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
10145                "    LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
10146   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
10147                "    LoooooooooooooooooooooooooooooooooooooooongVariable({});");
10148   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
10149                "    LoooooooooooooooooooooooooooooooooooooongVariable([A a]);");
10150 
10151   // Lambdas should not confuse the variable declaration heuristic.
10152   verifyFormat("LooooooooooooooooongType\n"
10153                "    variable(nullptr, [](A *a) {});",
10154                getLLVMStyleWithColumns(40));
10155 }
10156 
10157 TEST_F(FormatTest, BreaksLongDeclarations) {
10158   verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
10159                "    AnotherNameForTheLongType;");
10160   verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
10161                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10162   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10163                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
10164   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n"
10165                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
10166   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10167                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10168   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
10169                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10170   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
10171                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10172   verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
10173                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10174   verifyFormat("typeof(LoooooooooooooooooooooooooooooooooooooooooongName)\n"
10175                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10176   verifyFormat("_Atomic(LooooooooooooooooooooooooooooooooooooooooongName)\n"
10177                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10178   verifyFormat("__underlying_type(LooooooooooooooooooooooooooooooongName)\n"
10179                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10180   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10181                "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);");
10182   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10183                "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}");
10184   FormatStyle Indented = getLLVMStyle();
10185   Indented.IndentWrappedFunctionNames = true;
10186   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10187                "    LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
10188                Indented);
10189   verifyFormat(
10190       "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10191       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
10192       Indented);
10193   verifyFormat(
10194       "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
10195       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
10196       Indented);
10197   verifyFormat(
10198       "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
10199       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
10200       Indented);
10201 
10202   // FIXME: Without the comment, this breaks after "(".
10203   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType  // break\n"
10204                "    (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();",
10205                getGoogleStyle());
10206 
10207   verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
10208                "                  int LoooooooooooooooooooongParam2) {}");
10209   verifyFormat(
10210       "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
10211       "                                   SourceLocation L, IdentifierIn *II,\n"
10212       "                                   Type *T) {}");
10213   verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
10214                "ReallyReaaallyLongFunctionName(\n"
10215                "    const std::string &SomeParameter,\n"
10216                "    const SomeType<string, SomeOtherTemplateParameter>\n"
10217                "        &ReallyReallyLongParameterName,\n"
10218                "    const SomeType<string, SomeOtherTemplateParameter>\n"
10219                "        &AnotherLongParameterName) {}");
10220   verifyFormat("template <typename A>\n"
10221                "SomeLoooooooooooooooooooooongType<\n"
10222                "    typename some_namespace::SomeOtherType<A>::Type>\n"
10223                "Function() {}");
10224 
10225   verifyGoogleFormat(
10226       "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
10227       "    aaaaaaaaaaaaaaaaaaaaaaa;");
10228   verifyGoogleFormat(
10229       "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
10230       "                                   SourceLocation L) {}");
10231   verifyGoogleFormat(
10232       "some_namespace::LongReturnType\n"
10233       "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
10234       "    int first_long_parameter, int second_parameter) {}");
10235 
10236   verifyGoogleFormat("template <typename T>\n"
10237                      "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
10238                      "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
10239   verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10240                      "                   int aaaaaaaaaaaaaaaaaaaaaaa);");
10241 
10242   verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
10243                "    const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10244                "        *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
10245   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10246                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
10247                "        aaaaaaaaaaaaaaaaaaaaaaaa);");
10248   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10249                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
10250                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n"
10251                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
10252 
10253   verifyFormat("template <typename T> // Templates on own line.\n"
10254                "static int            // Some comment.\n"
10255                "MyFunction(int a);",
10256                getLLVMStyle());
10257 }
10258 
10259 TEST_F(FormatTest, FormatsAccessModifiers) {
10260   FormatStyle Style = getLLVMStyle();
10261   EXPECT_EQ(Style.EmptyLineBeforeAccessModifier,
10262             FormatStyle::ELBAMS_LogicalBlock);
10263   verifyFormat("struct foo {\n"
10264                "private:\n"
10265                "  void f() {}\n"
10266                "\n"
10267                "private:\n"
10268                "  int i;\n"
10269                "\n"
10270                "protected:\n"
10271                "  int j;\n"
10272                "};\n",
10273                Style);
10274   verifyFormat("struct foo {\n"
10275                "private:\n"
10276                "  void f() {}\n"
10277                "\n"
10278                "private:\n"
10279                "  int i;\n"
10280                "\n"
10281                "protected:\n"
10282                "  int j;\n"
10283                "};\n",
10284                "struct foo {\n"
10285                "private:\n"
10286                "  void f() {}\n"
10287                "private:\n"
10288                "  int i;\n"
10289                "protected:\n"
10290                "  int j;\n"
10291                "};\n",
10292                Style);
10293   verifyFormat("struct foo { /* comment */\n"
10294                "private:\n"
10295                "  int i;\n"
10296                "  // comment\n"
10297                "private:\n"
10298                "  int j;\n"
10299                "};\n",
10300                Style);
10301   verifyFormat("struct foo {\n"
10302                "#ifdef FOO\n"
10303                "#endif\n"
10304                "private:\n"
10305                "  int i;\n"
10306                "#ifdef FOO\n"
10307                "private:\n"
10308                "#endif\n"
10309                "  int j;\n"
10310                "};\n",
10311                Style);
10312   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
10313   verifyFormat("struct foo {\n"
10314                "private:\n"
10315                "  void f() {}\n"
10316                "private:\n"
10317                "  int i;\n"
10318                "protected:\n"
10319                "  int j;\n"
10320                "};\n",
10321                Style);
10322   verifyFormat("struct foo {\n"
10323                "private:\n"
10324                "  void f() {}\n"
10325                "private:\n"
10326                "  int i;\n"
10327                "protected:\n"
10328                "  int j;\n"
10329                "};\n",
10330                "struct foo {\n"
10331                "\n"
10332                "private:\n"
10333                "  void f() {}\n"
10334                "\n"
10335                "private:\n"
10336                "  int i;\n"
10337                "\n"
10338                "protected:\n"
10339                "  int j;\n"
10340                "};\n",
10341                Style);
10342   verifyFormat("struct foo { /* comment */\n"
10343                "private:\n"
10344                "  int i;\n"
10345                "  // comment\n"
10346                "private:\n"
10347                "  int j;\n"
10348                "};\n",
10349                "struct foo { /* comment */\n"
10350                "\n"
10351                "private:\n"
10352                "  int i;\n"
10353                "  // comment\n"
10354                "\n"
10355                "private:\n"
10356                "  int j;\n"
10357                "};\n",
10358                Style);
10359   verifyFormat("struct foo {\n"
10360                "#ifdef FOO\n"
10361                "#endif\n"
10362                "private:\n"
10363                "  int i;\n"
10364                "#ifdef FOO\n"
10365                "private:\n"
10366                "#endif\n"
10367                "  int j;\n"
10368                "};\n",
10369                "struct foo {\n"
10370                "#ifdef FOO\n"
10371                "#endif\n"
10372                "\n"
10373                "private:\n"
10374                "  int i;\n"
10375                "#ifdef FOO\n"
10376                "\n"
10377                "private:\n"
10378                "#endif\n"
10379                "  int j;\n"
10380                "};\n",
10381                Style);
10382   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
10383   verifyFormat("struct foo {\n"
10384                "private:\n"
10385                "  void f() {}\n"
10386                "\n"
10387                "private:\n"
10388                "  int i;\n"
10389                "\n"
10390                "protected:\n"
10391                "  int j;\n"
10392                "};\n",
10393                Style);
10394   verifyFormat("struct foo {\n"
10395                "private:\n"
10396                "  void f() {}\n"
10397                "\n"
10398                "private:\n"
10399                "  int i;\n"
10400                "\n"
10401                "protected:\n"
10402                "  int j;\n"
10403                "};\n",
10404                "struct foo {\n"
10405                "private:\n"
10406                "  void f() {}\n"
10407                "private:\n"
10408                "  int i;\n"
10409                "protected:\n"
10410                "  int j;\n"
10411                "};\n",
10412                Style);
10413   verifyFormat("struct foo { /* comment */\n"
10414                "private:\n"
10415                "  int i;\n"
10416                "  // comment\n"
10417                "\n"
10418                "private:\n"
10419                "  int j;\n"
10420                "};\n",
10421                "struct foo { /* comment */\n"
10422                "private:\n"
10423                "  int i;\n"
10424                "  // comment\n"
10425                "\n"
10426                "private:\n"
10427                "  int j;\n"
10428                "};\n",
10429                Style);
10430   verifyFormat("struct foo {\n"
10431                "#ifdef FOO\n"
10432                "#endif\n"
10433                "\n"
10434                "private:\n"
10435                "  int i;\n"
10436                "#ifdef FOO\n"
10437                "\n"
10438                "private:\n"
10439                "#endif\n"
10440                "  int j;\n"
10441                "};\n",
10442                "struct foo {\n"
10443                "#ifdef FOO\n"
10444                "#endif\n"
10445                "private:\n"
10446                "  int i;\n"
10447                "#ifdef FOO\n"
10448                "private:\n"
10449                "#endif\n"
10450                "  int j;\n"
10451                "};\n",
10452                Style);
10453   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
10454   EXPECT_EQ("struct foo {\n"
10455             "\n"
10456             "private:\n"
10457             "  void f() {}\n"
10458             "\n"
10459             "private:\n"
10460             "  int i;\n"
10461             "\n"
10462             "protected:\n"
10463             "  int j;\n"
10464             "};\n",
10465             format("struct foo {\n"
10466                    "\n"
10467                    "private:\n"
10468                    "  void f() {}\n"
10469                    "\n"
10470                    "private:\n"
10471                    "  int i;\n"
10472                    "\n"
10473                    "protected:\n"
10474                    "  int j;\n"
10475                    "};\n",
10476                    Style));
10477   verifyFormat("struct foo {\n"
10478                "private:\n"
10479                "  void f() {}\n"
10480                "private:\n"
10481                "  int i;\n"
10482                "protected:\n"
10483                "  int j;\n"
10484                "};\n",
10485                Style);
10486   EXPECT_EQ("struct foo { /* comment */\n"
10487             "\n"
10488             "private:\n"
10489             "  int i;\n"
10490             "  // comment\n"
10491             "\n"
10492             "private:\n"
10493             "  int j;\n"
10494             "};\n",
10495             format("struct foo { /* comment */\n"
10496                    "\n"
10497                    "private:\n"
10498                    "  int i;\n"
10499                    "  // comment\n"
10500                    "\n"
10501                    "private:\n"
10502                    "  int j;\n"
10503                    "};\n",
10504                    Style));
10505   verifyFormat("struct foo { /* comment */\n"
10506                "private:\n"
10507                "  int i;\n"
10508                "  // comment\n"
10509                "private:\n"
10510                "  int j;\n"
10511                "};\n",
10512                Style);
10513   EXPECT_EQ("struct foo {\n"
10514             "#ifdef FOO\n"
10515             "#endif\n"
10516             "\n"
10517             "private:\n"
10518             "  int i;\n"
10519             "#ifdef FOO\n"
10520             "\n"
10521             "private:\n"
10522             "#endif\n"
10523             "  int j;\n"
10524             "};\n",
10525             format("struct foo {\n"
10526                    "#ifdef FOO\n"
10527                    "#endif\n"
10528                    "\n"
10529                    "private:\n"
10530                    "  int i;\n"
10531                    "#ifdef FOO\n"
10532                    "\n"
10533                    "private:\n"
10534                    "#endif\n"
10535                    "  int j;\n"
10536                    "};\n",
10537                    Style));
10538   verifyFormat("struct foo {\n"
10539                "#ifdef FOO\n"
10540                "#endif\n"
10541                "private:\n"
10542                "  int i;\n"
10543                "#ifdef FOO\n"
10544                "private:\n"
10545                "#endif\n"
10546                "  int j;\n"
10547                "};\n",
10548                Style);
10549 
10550   FormatStyle NoEmptyLines = getLLVMStyle();
10551   NoEmptyLines.MaxEmptyLinesToKeep = 0;
10552   verifyFormat("struct foo {\n"
10553                "private:\n"
10554                "  void f() {}\n"
10555                "\n"
10556                "private:\n"
10557                "  int i;\n"
10558                "\n"
10559                "public:\n"
10560                "protected:\n"
10561                "  int j;\n"
10562                "};\n",
10563                NoEmptyLines);
10564 
10565   NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
10566   verifyFormat("struct foo {\n"
10567                "private:\n"
10568                "  void f() {}\n"
10569                "private:\n"
10570                "  int i;\n"
10571                "public:\n"
10572                "protected:\n"
10573                "  int j;\n"
10574                "};\n",
10575                NoEmptyLines);
10576 
10577   NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
10578   verifyFormat("struct foo {\n"
10579                "private:\n"
10580                "  void f() {}\n"
10581                "\n"
10582                "private:\n"
10583                "  int i;\n"
10584                "\n"
10585                "public:\n"
10586                "\n"
10587                "protected:\n"
10588                "  int j;\n"
10589                "};\n",
10590                NoEmptyLines);
10591 }
10592 
10593 TEST_F(FormatTest, FormatsAfterAccessModifiers) {
10594 
10595   FormatStyle Style = getLLVMStyle();
10596   EXPECT_EQ(Style.EmptyLineAfterAccessModifier, FormatStyle::ELAAMS_Never);
10597   verifyFormat("struct foo {\n"
10598                "private:\n"
10599                "  void f() {}\n"
10600                "\n"
10601                "private:\n"
10602                "  int i;\n"
10603                "\n"
10604                "protected:\n"
10605                "  int j;\n"
10606                "};\n",
10607                Style);
10608 
10609   // Check if lines are removed.
10610   verifyFormat("struct foo {\n"
10611                "private:\n"
10612                "  void f() {}\n"
10613                "\n"
10614                "private:\n"
10615                "  int i;\n"
10616                "\n"
10617                "protected:\n"
10618                "  int j;\n"
10619                "};\n",
10620                "struct foo {\n"
10621                "private:\n"
10622                "\n"
10623                "  void f() {}\n"
10624                "\n"
10625                "private:\n"
10626                "\n"
10627                "  int i;\n"
10628                "\n"
10629                "protected:\n"
10630                "\n"
10631                "  int j;\n"
10632                "};\n",
10633                Style);
10634 
10635   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
10636   verifyFormat("struct foo {\n"
10637                "private:\n"
10638                "\n"
10639                "  void f() {}\n"
10640                "\n"
10641                "private:\n"
10642                "\n"
10643                "  int i;\n"
10644                "\n"
10645                "protected:\n"
10646                "\n"
10647                "  int j;\n"
10648                "};\n",
10649                Style);
10650 
10651   // Check if lines are added.
10652   verifyFormat("struct foo {\n"
10653                "private:\n"
10654                "\n"
10655                "  void f() {}\n"
10656                "\n"
10657                "private:\n"
10658                "\n"
10659                "  int i;\n"
10660                "\n"
10661                "protected:\n"
10662                "\n"
10663                "  int j;\n"
10664                "};\n",
10665                "struct foo {\n"
10666                "private:\n"
10667                "  void f() {}\n"
10668                "\n"
10669                "private:\n"
10670                "  int i;\n"
10671                "\n"
10672                "protected:\n"
10673                "  int j;\n"
10674                "};\n",
10675                Style);
10676 
10677   // Leave tests rely on the code layout, test::messUp can not be used.
10678   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
10679   Style.MaxEmptyLinesToKeep = 0u;
10680   verifyFormat("struct foo {\n"
10681                "private:\n"
10682                "  void f() {}\n"
10683                "\n"
10684                "private:\n"
10685                "  int i;\n"
10686                "\n"
10687                "protected:\n"
10688                "  int j;\n"
10689                "};\n",
10690                Style);
10691 
10692   // Check if MaxEmptyLinesToKeep is respected.
10693   EXPECT_EQ("struct foo {\n"
10694             "private:\n"
10695             "  void f() {}\n"
10696             "\n"
10697             "private:\n"
10698             "  int i;\n"
10699             "\n"
10700             "protected:\n"
10701             "  int j;\n"
10702             "};\n",
10703             format("struct foo {\n"
10704                    "private:\n"
10705                    "\n\n\n"
10706                    "  void f() {}\n"
10707                    "\n"
10708                    "private:\n"
10709                    "\n\n\n"
10710                    "  int i;\n"
10711                    "\n"
10712                    "protected:\n"
10713                    "\n\n\n"
10714                    "  int j;\n"
10715                    "};\n",
10716                    Style));
10717 
10718   Style.MaxEmptyLinesToKeep = 1u;
10719   EXPECT_EQ("struct foo {\n"
10720             "private:\n"
10721             "\n"
10722             "  void f() {}\n"
10723             "\n"
10724             "private:\n"
10725             "\n"
10726             "  int i;\n"
10727             "\n"
10728             "protected:\n"
10729             "\n"
10730             "  int j;\n"
10731             "};\n",
10732             format("struct foo {\n"
10733                    "private:\n"
10734                    "\n"
10735                    "  void f() {}\n"
10736                    "\n"
10737                    "private:\n"
10738                    "\n"
10739                    "  int i;\n"
10740                    "\n"
10741                    "protected:\n"
10742                    "\n"
10743                    "  int j;\n"
10744                    "};\n",
10745                    Style));
10746   // Check if no lines are kept.
10747   EXPECT_EQ("struct foo {\n"
10748             "private:\n"
10749             "  void f() {}\n"
10750             "\n"
10751             "private:\n"
10752             "  int i;\n"
10753             "\n"
10754             "protected:\n"
10755             "  int j;\n"
10756             "};\n",
10757             format("struct foo {\n"
10758                    "private:\n"
10759                    "  void f() {}\n"
10760                    "\n"
10761                    "private:\n"
10762                    "  int i;\n"
10763                    "\n"
10764                    "protected:\n"
10765                    "  int j;\n"
10766                    "};\n",
10767                    Style));
10768   // Check if MaxEmptyLinesToKeep is respected.
10769   EXPECT_EQ("struct foo {\n"
10770             "private:\n"
10771             "\n"
10772             "  void f() {}\n"
10773             "\n"
10774             "private:\n"
10775             "\n"
10776             "  int i;\n"
10777             "\n"
10778             "protected:\n"
10779             "\n"
10780             "  int j;\n"
10781             "};\n",
10782             format("struct foo {\n"
10783                    "private:\n"
10784                    "\n\n\n"
10785                    "  void f() {}\n"
10786                    "\n"
10787                    "private:\n"
10788                    "\n\n\n"
10789                    "  int i;\n"
10790                    "\n"
10791                    "protected:\n"
10792                    "\n\n\n"
10793                    "  int j;\n"
10794                    "};\n",
10795                    Style));
10796 
10797   Style.MaxEmptyLinesToKeep = 10u;
10798   EXPECT_EQ("struct foo {\n"
10799             "private:\n"
10800             "\n\n\n"
10801             "  void f() {}\n"
10802             "\n"
10803             "private:\n"
10804             "\n\n\n"
10805             "  int i;\n"
10806             "\n"
10807             "protected:\n"
10808             "\n\n\n"
10809             "  int j;\n"
10810             "};\n",
10811             format("struct foo {\n"
10812                    "private:\n"
10813                    "\n\n\n"
10814                    "  void f() {}\n"
10815                    "\n"
10816                    "private:\n"
10817                    "\n\n\n"
10818                    "  int i;\n"
10819                    "\n"
10820                    "protected:\n"
10821                    "\n\n\n"
10822                    "  int j;\n"
10823                    "};\n",
10824                    Style));
10825 
10826   // Test with comments.
10827   Style = getLLVMStyle();
10828   verifyFormat("struct foo {\n"
10829                "private:\n"
10830                "  // comment\n"
10831                "  void f() {}\n"
10832                "\n"
10833                "private: /* comment */\n"
10834                "  int i;\n"
10835                "};\n",
10836                Style);
10837   verifyFormat("struct foo {\n"
10838                "private:\n"
10839                "  // comment\n"
10840                "  void f() {}\n"
10841                "\n"
10842                "private: /* comment */\n"
10843                "  int i;\n"
10844                "};\n",
10845                "struct foo {\n"
10846                "private:\n"
10847                "\n"
10848                "  // comment\n"
10849                "  void f() {}\n"
10850                "\n"
10851                "private: /* comment */\n"
10852                "\n"
10853                "  int i;\n"
10854                "};\n",
10855                Style);
10856 
10857   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
10858   verifyFormat("struct foo {\n"
10859                "private:\n"
10860                "\n"
10861                "  // comment\n"
10862                "  void f() {}\n"
10863                "\n"
10864                "private: /* comment */\n"
10865                "\n"
10866                "  int i;\n"
10867                "};\n",
10868                "struct foo {\n"
10869                "private:\n"
10870                "  // comment\n"
10871                "  void f() {}\n"
10872                "\n"
10873                "private: /* comment */\n"
10874                "  int i;\n"
10875                "};\n",
10876                Style);
10877   verifyFormat("struct foo {\n"
10878                "private:\n"
10879                "\n"
10880                "  // comment\n"
10881                "  void f() {}\n"
10882                "\n"
10883                "private: /* comment */\n"
10884                "\n"
10885                "  int i;\n"
10886                "};\n",
10887                Style);
10888 
10889   // Test with preprocessor defines.
10890   Style = getLLVMStyle();
10891   verifyFormat("struct foo {\n"
10892                "private:\n"
10893                "#ifdef FOO\n"
10894                "#endif\n"
10895                "  void f() {}\n"
10896                "};\n",
10897                Style);
10898   verifyFormat("struct foo {\n"
10899                "private:\n"
10900                "#ifdef FOO\n"
10901                "#endif\n"
10902                "  void f() {}\n"
10903                "};\n",
10904                "struct foo {\n"
10905                "private:\n"
10906                "\n"
10907                "#ifdef FOO\n"
10908                "#endif\n"
10909                "  void f() {}\n"
10910                "};\n",
10911                Style);
10912 
10913   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
10914   verifyFormat("struct foo {\n"
10915                "private:\n"
10916                "\n"
10917                "#ifdef FOO\n"
10918                "#endif\n"
10919                "  void f() {}\n"
10920                "};\n",
10921                "struct foo {\n"
10922                "private:\n"
10923                "#ifdef FOO\n"
10924                "#endif\n"
10925                "  void f() {}\n"
10926                "};\n",
10927                Style);
10928   verifyFormat("struct foo {\n"
10929                "private:\n"
10930                "\n"
10931                "#ifdef FOO\n"
10932                "#endif\n"
10933                "  void f() {}\n"
10934                "};\n",
10935                Style);
10936 }
10937 
10938 TEST_F(FormatTest, FormatsAfterAndBeforeAccessModifiersInteraction) {
10939   // Combined tests of EmptyLineAfterAccessModifier and
10940   // EmptyLineBeforeAccessModifier.
10941   FormatStyle Style = getLLVMStyle();
10942   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
10943   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
10944   verifyFormat("struct foo {\n"
10945                "private:\n"
10946                "\n"
10947                "protected:\n"
10948                "};\n",
10949                Style);
10950 
10951   Style.MaxEmptyLinesToKeep = 10u;
10952   // Both remove all new lines.
10953   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
10954   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
10955   verifyFormat("struct foo {\n"
10956                "private:\n"
10957                "protected:\n"
10958                "};\n",
10959                "struct foo {\n"
10960                "private:\n"
10961                "\n\n\n"
10962                "protected:\n"
10963                "};\n",
10964                Style);
10965 
10966   // Leave tests rely on the code layout, test::messUp can not be used.
10967   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
10968   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
10969   Style.MaxEmptyLinesToKeep = 10u;
10970   EXPECT_EQ("struct foo {\n"
10971             "private:\n"
10972             "\n\n\n"
10973             "protected:\n"
10974             "};\n",
10975             format("struct foo {\n"
10976                    "private:\n"
10977                    "\n\n\n"
10978                    "protected:\n"
10979                    "};\n",
10980                    Style));
10981   Style.MaxEmptyLinesToKeep = 3u;
10982   EXPECT_EQ("struct foo {\n"
10983             "private:\n"
10984             "\n\n\n"
10985             "protected:\n"
10986             "};\n",
10987             format("struct foo {\n"
10988                    "private:\n"
10989                    "\n\n\n"
10990                    "protected:\n"
10991                    "};\n",
10992                    Style));
10993   Style.MaxEmptyLinesToKeep = 1u;
10994   EXPECT_EQ("struct foo {\n"
10995             "private:\n"
10996             "\n\n\n"
10997             "protected:\n"
10998             "};\n",
10999             format("struct foo {\n"
11000                    "private:\n"
11001                    "\n\n\n"
11002                    "protected:\n"
11003                    "};\n",
11004                    Style)); // Based on new lines in original document and not
11005                             // on the setting.
11006 
11007   Style.MaxEmptyLinesToKeep = 10u;
11008   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
11009   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
11010   // Newlines are kept if they are greater than zero,
11011   // test::messUp removes all new lines which changes the logic
11012   EXPECT_EQ("struct foo {\n"
11013             "private:\n"
11014             "\n\n\n"
11015             "protected:\n"
11016             "};\n",
11017             format("struct foo {\n"
11018                    "private:\n"
11019                    "\n\n\n"
11020                    "protected:\n"
11021                    "};\n",
11022                    Style));
11023 
11024   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
11025   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
11026   // test::messUp removes all new lines which changes the logic
11027   EXPECT_EQ("struct foo {\n"
11028             "private:\n"
11029             "\n\n\n"
11030             "protected:\n"
11031             "};\n",
11032             format("struct foo {\n"
11033                    "private:\n"
11034                    "\n\n\n"
11035                    "protected:\n"
11036                    "};\n",
11037                    Style));
11038 
11039   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
11040   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
11041   EXPECT_EQ("struct foo {\n"
11042             "private:\n"
11043             "\n\n\n"
11044             "protected:\n"
11045             "};\n",
11046             format("struct foo {\n"
11047                    "private:\n"
11048                    "\n\n\n"
11049                    "protected:\n"
11050                    "};\n",
11051                    Style)); // test::messUp removes all new lines which changes
11052                             // the logic.
11053 
11054   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
11055   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
11056   verifyFormat("struct foo {\n"
11057                "private:\n"
11058                "protected:\n"
11059                "};\n",
11060                "struct foo {\n"
11061                "private:\n"
11062                "\n\n\n"
11063                "protected:\n"
11064                "};\n",
11065                Style);
11066 
11067   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
11068   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
11069   EXPECT_EQ("struct foo {\n"
11070             "private:\n"
11071             "\n\n\n"
11072             "protected:\n"
11073             "};\n",
11074             format("struct foo {\n"
11075                    "private:\n"
11076                    "\n\n\n"
11077                    "protected:\n"
11078                    "};\n",
11079                    Style)); // test::messUp removes all new lines which changes
11080                             // the logic.
11081 
11082   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
11083   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
11084   verifyFormat("struct foo {\n"
11085                "private:\n"
11086                "protected:\n"
11087                "};\n",
11088                "struct foo {\n"
11089                "private:\n"
11090                "\n\n\n"
11091                "protected:\n"
11092                "};\n",
11093                Style);
11094 
11095   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
11096   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
11097   verifyFormat("struct foo {\n"
11098                "private:\n"
11099                "protected:\n"
11100                "};\n",
11101                "struct foo {\n"
11102                "private:\n"
11103                "\n\n\n"
11104                "protected:\n"
11105                "};\n",
11106                Style);
11107 
11108   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
11109   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
11110   verifyFormat("struct foo {\n"
11111                "private:\n"
11112                "protected:\n"
11113                "};\n",
11114                "struct foo {\n"
11115                "private:\n"
11116                "\n\n\n"
11117                "protected:\n"
11118                "};\n",
11119                Style);
11120 
11121   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
11122   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
11123   verifyFormat("struct foo {\n"
11124                "private:\n"
11125                "protected:\n"
11126                "};\n",
11127                "struct foo {\n"
11128                "private:\n"
11129                "\n\n\n"
11130                "protected:\n"
11131                "};\n",
11132                Style);
11133 }
11134 
11135 TEST_F(FormatTest, FormatsArrays) {
11136   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
11137                "                         [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
11138   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n"
11139                "                         [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;");
11140   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n"
11141                "    aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}");
11142   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
11143                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
11144   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
11145                "    [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
11146   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
11147                "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
11148                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
11149   verifyFormat(
11150       "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
11151       "             << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
11152       "                                  [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
11153   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n"
11154                "    .aaaaaaaaaaaaaaaaaaaaaa();");
11155 
11156   verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
11157                      "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
11158   verifyFormat(
11159       "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
11160       "                                  .aaaaaaa[0]\n"
11161       "                                  .aaaaaaaaaaaaaaaaaaaaaa();");
11162   verifyFormat("a[::b::c];");
11163 
11164   verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10));
11165 
11166   FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0);
11167   verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit);
11168 }
11169 
11170 TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
11171   verifyFormat("(a)->b();");
11172   verifyFormat("--a;");
11173 }
11174 
11175 TEST_F(FormatTest, HandlesIncludeDirectives) {
11176   verifyFormat("#include <string>\n"
11177                "#include <a/b/c.h>\n"
11178                "#include \"a/b/string\"\n"
11179                "#include \"string.h\"\n"
11180                "#include \"string.h\"\n"
11181                "#include <a-a>\n"
11182                "#include < path with space >\n"
11183                "#include_next <test.h>"
11184                "#include \"abc.h\" // this is included for ABC\n"
11185                "#include \"some long include\" // with a comment\n"
11186                "#include \"some very long include path\"\n"
11187                "#include <some/very/long/include/path>\n",
11188                getLLVMStyleWithColumns(35));
11189   EXPECT_EQ("#include \"a.h\"", format("#include  \"a.h\""));
11190   EXPECT_EQ("#include <a>", format("#include<a>"));
11191 
11192   verifyFormat("#import <string>");
11193   verifyFormat("#import <a/b/c.h>");
11194   verifyFormat("#import \"a/b/string\"");
11195   verifyFormat("#import \"string.h\"");
11196   verifyFormat("#import \"string.h\"");
11197   verifyFormat("#if __has_include(<strstream>)\n"
11198                "#include <strstream>\n"
11199                "#endif");
11200 
11201   verifyFormat("#define MY_IMPORT <a/b>");
11202 
11203   verifyFormat("#if __has_include(<a/b>)");
11204   verifyFormat("#if __has_include_next(<a/b>)");
11205   verifyFormat("#define F __has_include(<a/b>)");
11206   verifyFormat("#define F __has_include_next(<a/b>)");
11207 
11208   // Protocol buffer definition or missing "#".
11209   verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
11210                getLLVMStyleWithColumns(30));
11211 
11212   FormatStyle Style = getLLVMStyle();
11213   Style.AlwaysBreakBeforeMultilineStrings = true;
11214   Style.ColumnLimit = 0;
11215   verifyFormat("#import \"abc.h\"", Style);
11216 
11217   // But 'import' might also be a regular C++ namespace.
11218   verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
11219                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
11220 }
11221 
11222 //===----------------------------------------------------------------------===//
11223 // Error recovery tests.
11224 //===----------------------------------------------------------------------===//
11225 
11226 TEST_F(FormatTest, IncompleteParameterLists) {
11227   FormatStyle NoBinPacking = getLLVMStyle();
11228   NoBinPacking.BinPackParameters = false;
11229   verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
11230                "                        double *min_x,\n"
11231                "                        double *max_x,\n"
11232                "                        double *min_y,\n"
11233                "                        double *max_y,\n"
11234                "                        double *min_z,\n"
11235                "                        double *max_z, ) {}",
11236                NoBinPacking);
11237 }
11238 
11239 TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
11240   verifyFormat("void f() { return; }\n42");
11241   verifyFormat("void f() {\n"
11242                "  if (0)\n"
11243                "    return;\n"
11244                "}\n"
11245                "42");
11246   verifyFormat("void f() { return }\n42");
11247   verifyFormat("void f() {\n"
11248                "  if (0)\n"
11249                "    return\n"
11250                "}\n"
11251                "42");
11252 }
11253 
11254 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
11255   EXPECT_EQ("void f() { return }", format("void  f ( )  {  return  }"));
11256   EXPECT_EQ("void f() {\n"
11257             "  if (a)\n"
11258             "    return\n"
11259             "}",
11260             format("void  f  (  )  {  if  ( a )  return  }"));
11261   EXPECT_EQ("namespace N {\n"
11262             "void f()\n"
11263             "}",
11264             format("namespace  N  {  void f()  }"));
11265   EXPECT_EQ("namespace N {\n"
11266             "void f() {}\n"
11267             "void g()\n"
11268             "} // namespace N",
11269             format("namespace N  { void f( ) { } void g( ) }"));
11270 }
11271 
11272 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
11273   verifyFormat("int aaaaaaaa =\n"
11274                "    // Overlylongcomment\n"
11275                "    b;",
11276                getLLVMStyleWithColumns(20));
11277   verifyFormat("function(\n"
11278                "    ShortArgument,\n"
11279                "    LoooooooooooongArgument);\n",
11280                getLLVMStyleWithColumns(20));
11281 }
11282 
11283 TEST_F(FormatTest, IncorrectAccessSpecifier) {
11284   verifyFormat("public:");
11285   verifyFormat("class A {\n"
11286                "public\n"
11287                "  void f() {}\n"
11288                "};");
11289   verifyFormat("public\n"
11290                "int qwerty;");
11291   verifyFormat("public\n"
11292                "B {}");
11293   verifyFormat("public\n"
11294                "{}");
11295   verifyFormat("public\n"
11296                "B { int x; }");
11297 }
11298 
11299 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
11300   verifyFormat("{");
11301   verifyFormat("#})");
11302   verifyNoCrash("(/**/[:!] ?[).");
11303 }
11304 
11305 TEST_F(FormatTest, IncorrectUnbalancedBracesInMacrosWithUnicode) {
11306   // Found by oss-fuzz:
11307   // https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=8212
11308   FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp);
11309   Style.ColumnLimit = 60;
11310   verifyNoCrash(
11311       "\x23\x47\xff\x20\x28\xff\x3c\xff\x3f\xff\x20\x2f\x7b\x7a\xff\x20"
11312       "\xff\xff\xff\xca\xb5\xff\xff\xff\xff\x3a\x7b\x7d\xff\x20\xff\x20"
11313       "\xff\x74\xff\x20\x7d\x7d\xff\x7b\x3a\xff\x20\x71\xff\x20\xff\x0a",
11314       Style);
11315 }
11316 
11317 TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
11318   verifyFormat("do {\n}");
11319   verifyFormat("do {\n}\n"
11320                "f();");
11321   verifyFormat("do {\n}\n"
11322                "wheeee(fun);");
11323   verifyFormat("do {\n"
11324                "  f();\n"
11325                "}");
11326 }
11327 
11328 TEST_F(FormatTest, IncorrectCodeMissingParens) {
11329   verifyFormat("if {\n  foo;\n  foo();\n}");
11330   verifyFormat("switch {\n  foo;\n  foo();\n}");
11331   verifyIncompleteFormat("for {\n  foo;\n  foo();\n}");
11332   verifyFormat("while {\n  foo;\n  foo();\n}");
11333   verifyFormat("do {\n  foo;\n  foo();\n} while;");
11334 }
11335 
11336 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
11337   verifyIncompleteFormat("namespace {\n"
11338                          "class Foo { Foo (\n"
11339                          "};\n"
11340                          "} // namespace");
11341 }
11342 
11343 TEST_F(FormatTest, IncorrectCodeErrorDetection) {
11344   EXPECT_EQ("{\n  {}\n", format("{\n{\n}\n"));
11345   EXPECT_EQ("{\n  {}\n", format("{\n  {\n}\n"));
11346   EXPECT_EQ("{\n  {}\n", format("{\n  {\n  }\n"));
11347   EXPECT_EQ("{\n  {}\n}\n}\n", format("{\n  {\n    }\n  }\n}\n"));
11348 
11349   EXPECT_EQ("{\n"
11350             "  {\n"
11351             "    breakme(\n"
11352             "        qwe);\n"
11353             "  }\n",
11354             format("{\n"
11355                    "    {\n"
11356                    " breakme(qwe);\n"
11357                    "}\n",
11358                    getLLVMStyleWithColumns(10)));
11359 }
11360 
11361 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
11362   verifyFormat("int x = {\n"
11363                "    avariable,\n"
11364                "    b(alongervariable)};",
11365                getLLVMStyleWithColumns(25));
11366 }
11367 
11368 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
11369   verifyFormat("return (a)(b){1, 2, 3};");
11370 }
11371 
11372 TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
11373   verifyFormat("vector<int> x{1, 2, 3, 4};");
11374   verifyFormat("vector<int> x{\n"
11375                "    1,\n"
11376                "    2,\n"
11377                "    3,\n"
11378                "    4,\n"
11379                "};");
11380   verifyFormat("vector<T> x{{}, {}, {}, {}};");
11381   verifyFormat("f({1, 2});");
11382   verifyFormat("auto v = Foo{-1};");
11383   verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
11384   verifyFormat("Class::Class : member{1, 2, 3} {}");
11385   verifyFormat("new vector<int>{1, 2, 3};");
11386   verifyFormat("new int[3]{1, 2, 3};");
11387   verifyFormat("new int{1};");
11388   verifyFormat("return {arg1, arg2};");
11389   verifyFormat("return {arg1, SomeType{parameter}};");
11390   verifyFormat("int count = set<int>{f(), g(), h()}.size();");
11391   verifyFormat("new T{arg1, arg2};");
11392   verifyFormat("f(MyMap[{composite, key}]);");
11393   verifyFormat("class Class {\n"
11394                "  T member = {arg1, arg2};\n"
11395                "};");
11396   verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
11397   verifyFormat("const struct A a = {.a = 1, .b = 2};");
11398   verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
11399   verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
11400   verifyFormat("int a = std::is_integral<int>{} + 0;");
11401 
11402   verifyFormat("int foo(int i) { return fo1{}(i); }");
11403   verifyFormat("int foo(int i) { return fo1{}(i); }");
11404   verifyFormat("auto i = decltype(x){};");
11405   verifyFormat("auto i = typeof(x){};");
11406   verifyFormat("auto i = _Atomic(x){};");
11407   verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
11408   verifyFormat("Node n{1, Node{1000}, //\n"
11409                "       2};");
11410   verifyFormat("Aaaa aaaaaaa{\n"
11411                "    {\n"
11412                "        aaaa,\n"
11413                "    },\n"
11414                "};");
11415   verifyFormat("class C : public D {\n"
11416                "  SomeClass SC{2};\n"
11417                "};");
11418   verifyFormat("class C : public A {\n"
11419                "  class D : public B {\n"
11420                "    void f() { int i{2}; }\n"
11421                "  };\n"
11422                "};");
11423   verifyFormat("#define A {a, a},");
11424 
11425   // Avoid breaking between equal sign and opening brace
11426   FormatStyle AvoidBreakingFirstArgument = getLLVMStyle();
11427   AvoidBreakingFirstArgument.PenaltyBreakBeforeFirstCallParameter = 200;
11428   verifyFormat("const std::unordered_map<std::string, int> MyHashTable =\n"
11429                "    {{\"aaaaaaaaaaaaaaaaaaaaa\", 0},\n"
11430                "     {\"bbbbbbbbbbbbbbbbbbbbb\", 1},\n"
11431                "     {\"ccccccccccccccccccccc\", 2}};",
11432                AvoidBreakingFirstArgument);
11433 
11434   // Binpacking only if there is no trailing comma
11435   verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n"
11436                "                      cccccccccc, dddddddddd};",
11437                getLLVMStyleWithColumns(50));
11438   verifyFormat("const Aaaaaa aaaaa = {\n"
11439                "    aaaaaaaaaaa,\n"
11440                "    bbbbbbbbbbb,\n"
11441                "    ccccccccccc,\n"
11442                "    ddddddddddd,\n"
11443                "};",
11444                getLLVMStyleWithColumns(50));
11445 
11446   // Cases where distinguising braced lists and blocks is hard.
11447   verifyFormat("vector<int> v{12} GUARDED_BY(mutex);");
11448   verifyFormat("void f() {\n"
11449                "  return; // comment\n"
11450                "}\n"
11451                "SomeType t;");
11452   verifyFormat("void f() {\n"
11453                "  if (a) {\n"
11454                "    f();\n"
11455                "  }\n"
11456                "}\n"
11457                "SomeType t;");
11458 
11459   // In combination with BinPackArguments = false.
11460   FormatStyle NoBinPacking = getLLVMStyle();
11461   NoBinPacking.BinPackArguments = false;
11462   verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
11463                "                      bbbbb,\n"
11464                "                      ccccc,\n"
11465                "                      ddddd,\n"
11466                "                      eeeee,\n"
11467                "                      ffffff,\n"
11468                "                      ggggg,\n"
11469                "                      hhhhhh,\n"
11470                "                      iiiiii,\n"
11471                "                      jjjjjj,\n"
11472                "                      kkkkkk};",
11473                NoBinPacking);
11474   verifyFormat("const Aaaaaa aaaaa = {\n"
11475                "    aaaaa,\n"
11476                "    bbbbb,\n"
11477                "    ccccc,\n"
11478                "    ddddd,\n"
11479                "    eeeee,\n"
11480                "    ffffff,\n"
11481                "    ggggg,\n"
11482                "    hhhhhh,\n"
11483                "    iiiiii,\n"
11484                "    jjjjjj,\n"
11485                "    kkkkkk,\n"
11486                "};",
11487                NoBinPacking);
11488   verifyFormat(
11489       "const Aaaaaa aaaaa = {\n"
11490       "    aaaaa,  bbbbb,  ccccc,  ddddd,  eeeee,  ffffff, ggggg, hhhhhh,\n"
11491       "    iiiiii, jjjjjj, kkkkkk, aaaaa,  bbbbb,  ccccc,  ddddd, eeeee,\n"
11492       "    ffffff, ggggg,  hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
11493       "};",
11494       NoBinPacking);
11495 
11496   NoBinPacking.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
11497   EXPECT_EQ("static uint8 CddDp83848Reg[] = {\n"
11498             "    CDDDP83848_BMCR_REGISTER,\n"
11499             "    CDDDP83848_BMSR_REGISTER,\n"
11500             "    CDDDP83848_RBR_REGISTER};",
11501             format("static uint8 CddDp83848Reg[] = {CDDDP83848_BMCR_REGISTER,\n"
11502                    "                                CDDDP83848_BMSR_REGISTER,\n"
11503                    "                                CDDDP83848_RBR_REGISTER};",
11504                    NoBinPacking));
11505 
11506   // FIXME: The alignment of these trailing comments might be bad. Then again,
11507   // this might be utterly useless in real code.
11508   verifyFormat("Constructor::Constructor()\n"
11509                "    : some_value{         //\n"
11510                "                 aaaaaaa, //\n"
11511                "                 bbbbbbb} {}");
11512 
11513   // In braced lists, the first comment is always assumed to belong to the
11514   // first element. Thus, it can be moved to the next or previous line as
11515   // appropriate.
11516   EXPECT_EQ("function({// First element:\n"
11517             "          1,\n"
11518             "          // Second element:\n"
11519             "          2});",
11520             format("function({\n"
11521                    "    // First element:\n"
11522                    "    1,\n"
11523                    "    // Second element:\n"
11524                    "    2});"));
11525   EXPECT_EQ("std::vector<int> MyNumbers{\n"
11526             "    // First element:\n"
11527             "    1,\n"
11528             "    // Second element:\n"
11529             "    2};",
11530             format("std::vector<int> MyNumbers{// First element:\n"
11531                    "                           1,\n"
11532                    "                           // Second element:\n"
11533                    "                           2};",
11534                    getLLVMStyleWithColumns(30)));
11535   // A trailing comma should still lead to an enforced line break and no
11536   // binpacking.
11537   EXPECT_EQ("vector<int> SomeVector = {\n"
11538             "    // aaa\n"
11539             "    1,\n"
11540             "    2,\n"
11541             "};",
11542             format("vector<int> SomeVector = { // aaa\n"
11543                    "    1, 2, };"));
11544 
11545   // C++11 brace initializer list l-braces should not be treated any differently
11546   // when breaking before lambda bodies is enabled
11547   FormatStyle BreakBeforeLambdaBody = getLLVMStyle();
11548   BreakBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
11549   BreakBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
11550   BreakBeforeLambdaBody.AlwaysBreakBeforeMultilineStrings = true;
11551   verifyFormat(
11552       "std::runtime_error{\n"
11553       "    \"Long string which will force a break onto the next line...\"};",
11554       BreakBeforeLambdaBody);
11555 
11556   FormatStyle ExtraSpaces = getLLVMStyle();
11557   ExtraSpaces.Cpp11BracedListStyle = false;
11558   ExtraSpaces.ColumnLimit = 75;
11559   verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
11560   verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
11561   verifyFormat("f({ 1, 2 });", ExtraSpaces);
11562   verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
11563   verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
11564   verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
11565   verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
11566   verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
11567   verifyFormat("return { arg1, arg2 };", ExtraSpaces);
11568   verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
11569   verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
11570   verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
11571   verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
11572   verifyFormat("class Class {\n"
11573                "  T member = { arg1, arg2 };\n"
11574                "};",
11575                ExtraSpaces);
11576   verifyFormat(
11577       "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
11578       "                                 aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
11579       "                  : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
11580       "                                 bbbbbbbbbbbbbbbbbbbb, bbbbb };",
11581       ExtraSpaces);
11582   verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
11583   verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
11584                ExtraSpaces);
11585   verifyFormat(
11586       "someFunction(OtherParam,\n"
11587       "             BracedList{ // comment 1 (Forcing interesting break)\n"
11588       "                         param1, param2,\n"
11589       "                         // comment 2\n"
11590       "                         param3, param4 });",
11591       ExtraSpaces);
11592   verifyFormat(
11593       "std::this_thread::sleep_for(\n"
11594       "    std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
11595       ExtraSpaces);
11596   verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n"
11597                "    aaaaaaa,\n"
11598                "    aaaaaaaaaa,\n"
11599                "    aaaaa,\n"
11600                "    aaaaaaaaaaaaaaa,\n"
11601                "    aaa,\n"
11602                "    aaaaaaaaaa,\n"
11603                "    a,\n"
11604                "    aaaaaaaaaaaaaaaaaaaaa,\n"
11605                "    aaaaaaaaaaaa,\n"
11606                "    aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
11607                "    aaaaaaa,\n"
11608                "    a};");
11609   verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
11610   verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces);
11611   verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces);
11612 
11613   // Avoid breaking between initializer/equal sign and opening brace
11614   ExtraSpaces.PenaltyBreakBeforeFirstCallParameter = 200;
11615   verifyFormat("const std::unordered_map<std::string, int> MyHashTable = {\n"
11616                "  { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n"
11617                "  { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n"
11618                "  { \"ccccccccccccccccccccc\", 2 }\n"
11619                "};",
11620                ExtraSpaces);
11621   verifyFormat("const std::unordered_map<std::string, int> MyHashTable{\n"
11622                "  { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n"
11623                "  { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n"
11624                "  { \"ccccccccccccccccccccc\", 2 }\n"
11625                "};",
11626                ExtraSpaces);
11627 
11628   FormatStyle SpaceBeforeBrace = getLLVMStyle();
11629   SpaceBeforeBrace.SpaceBeforeCpp11BracedList = true;
11630   verifyFormat("vector<int> x {1, 2, 3, 4};", SpaceBeforeBrace);
11631   verifyFormat("f({}, {{}, {}}, MyMap[{k, v}]);", SpaceBeforeBrace);
11632 
11633   FormatStyle SpaceBetweenBraces = getLLVMStyle();
11634   SpaceBetweenBraces.SpacesInAngles = FormatStyle::SIAS_Always;
11635   SpaceBetweenBraces.SpacesInParentheses = true;
11636   SpaceBetweenBraces.SpacesInSquareBrackets = true;
11637   verifyFormat("vector< int > x{ 1, 2, 3, 4 };", SpaceBetweenBraces);
11638   verifyFormat("f( {}, { {}, {} }, MyMap[ { k, v } ] );", SpaceBetweenBraces);
11639   verifyFormat("vector< int > x{ // comment 1\n"
11640                "                 1, 2, 3, 4 };",
11641                SpaceBetweenBraces);
11642   SpaceBetweenBraces.ColumnLimit = 20;
11643   EXPECT_EQ("vector< int > x{\n"
11644             "    1, 2, 3, 4 };",
11645             format("vector<int>x{1,2,3,4};", SpaceBetweenBraces));
11646   SpaceBetweenBraces.ColumnLimit = 24;
11647   EXPECT_EQ("vector< int > x{ 1, 2,\n"
11648             "                 3, 4 };",
11649             format("vector<int>x{1,2,3,4};", SpaceBetweenBraces));
11650   EXPECT_EQ("vector< int > x{\n"
11651             "    1,\n"
11652             "    2,\n"
11653             "    3,\n"
11654             "    4,\n"
11655             "};",
11656             format("vector<int>x{1,2,3,4,};", SpaceBetweenBraces));
11657   verifyFormat("vector< int > x{};", SpaceBetweenBraces);
11658   SpaceBetweenBraces.SpaceInEmptyParentheses = true;
11659   verifyFormat("vector< int > x{ };", SpaceBetweenBraces);
11660 }
11661 
11662 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
11663   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11664                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11665                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11666                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11667                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11668                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
11669   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n"
11670                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11671                "                 1, 22, 333, 4444, 55555, //\n"
11672                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11673                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
11674   verifyFormat(
11675       "vector<int> x = {1,       22, 333, 4444, 55555, 666666, 7777777,\n"
11676       "                 1,       22, 333, 4444, 55555, 666666, 7777777,\n"
11677       "                 1,       22, 333, 4444, 55555, 666666, // comment\n"
11678       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
11679       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
11680       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
11681       "                 7777777};");
11682   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
11683                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
11684                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
11685   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
11686                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
11687                "    // Separating comment.\n"
11688                "    X86::R8, X86::R9, X86::R10, X86::R11, 0};");
11689   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
11690                "    // Leading comment\n"
11691                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
11692                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
11693   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
11694                "                 1, 1, 1, 1};",
11695                getLLVMStyleWithColumns(39));
11696   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
11697                "                 1, 1, 1, 1};",
11698                getLLVMStyleWithColumns(38));
11699   verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
11700                "    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
11701                getLLVMStyleWithColumns(43));
11702   verifyFormat(
11703       "static unsigned SomeValues[10][3] = {\n"
11704       "    {1, 4, 0},  {4, 9, 0},  {4, 5, 9},  {8, 5, 4}, {1, 8, 4},\n"
11705       "    {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};");
11706   verifyFormat("static auto fields = new vector<string>{\n"
11707                "    \"aaaaaaaaaaaaa\",\n"
11708                "    \"aaaaaaaaaaaaa\",\n"
11709                "    \"aaaaaaaaaaaa\",\n"
11710                "    \"aaaaaaaaaaaaaa\",\n"
11711                "    \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
11712                "    \"aaaaaaaaaaaa\",\n"
11713                "    \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
11714                "};");
11715   verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};");
11716   verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n"
11717                "                 2, bbbbbbbbbbbbbbbbbbbbbb,\n"
11718                "                 3, cccccccccccccccccccccc};",
11719                getLLVMStyleWithColumns(60));
11720 
11721   // Trailing commas.
11722   verifyFormat("vector<int> x = {\n"
11723                "    1, 1, 1, 1, 1, 1, 1, 1,\n"
11724                "};",
11725                getLLVMStyleWithColumns(39));
11726   verifyFormat("vector<int> x = {\n"
11727                "    1, 1, 1, 1, 1, 1, 1, 1, //\n"
11728                "};",
11729                getLLVMStyleWithColumns(39));
11730   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
11731                "                 1, 1, 1, 1,\n"
11732                "                 /**/ /**/};",
11733                getLLVMStyleWithColumns(39));
11734 
11735   // Trailing comment in the first line.
11736   verifyFormat("vector<int> iiiiiiiiiiiiiii = {                      //\n"
11737                "    1111111111, 2222222222, 33333333333, 4444444444, //\n"
11738                "    111111111,  222222222,  3333333333,  444444444,  //\n"
11739                "    11111111,   22222222,   333333333,   44444444};");
11740   // Trailing comment in the last line.
11741   verifyFormat("int aaaaa[] = {\n"
11742                "    1, 2, 3, // comment\n"
11743                "    4, 5, 6  // comment\n"
11744                "};");
11745 
11746   // With nested lists, we should either format one item per line or all nested
11747   // lists one on line.
11748   // FIXME: For some nested lists, we can do better.
11749   verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
11750                "        {aaaaaaaaaaaaaaaaaaa},\n"
11751                "        {aaaaaaaaaaaaaaaaaaaaa},\n"
11752                "        {aaaaaaaaaaaaaaaaa}};",
11753                getLLVMStyleWithColumns(60));
11754   verifyFormat(
11755       "SomeStruct my_struct_array = {\n"
11756       "    {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
11757       "     aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
11758       "    {aaa, aaa},\n"
11759       "    {aaa, aaa},\n"
11760       "    {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
11761       "    {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
11762       "     aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
11763 
11764   // No column layout should be used here.
11765   verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
11766                "                   bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
11767 
11768   verifyNoCrash("a<,");
11769 
11770   // No braced initializer here.
11771   verifyFormat("void f() {\n"
11772                "  struct Dummy {};\n"
11773                "  f(v);\n"
11774                "}");
11775 
11776   // Long lists should be formatted in columns even if they are nested.
11777   verifyFormat(
11778       "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11779       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11780       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11781       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11782       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11783       "                          1, 22, 333, 4444, 55555, 666666, 7777777});");
11784 
11785   // Allow "single-column" layout even if that violates the column limit. There
11786   // isn't going to be a better way.
11787   verifyFormat("std::vector<int> a = {\n"
11788                "    aaaaaaaa,\n"
11789                "    aaaaaaaa,\n"
11790                "    aaaaaaaa,\n"
11791                "    aaaaaaaa,\n"
11792                "    aaaaaaaaaa,\n"
11793                "    aaaaaaaa,\n"
11794                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa};",
11795                getLLVMStyleWithColumns(30));
11796   verifyFormat("vector<int> aaaa = {\n"
11797                "    aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
11798                "    aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
11799                "    aaaaaa.aaaaaaa,\n"
11800                "    aaaaaa.aaaaaaa,\n"
11801                "    aaaaaa.aaaaaaa,\n"
11802                "    aaaaaa.aaaaaaa,\n"
11803                "};");
11804 
11805   // Don't create hanging lists.
11806   verifyFormat("someFunction(Param, {List1, List2,\n"
11807                "                     List3});",
11808                getLLVMStyleWithColumns(35));
11809   verifyFormat("someFunction(Param, Param,\n"
11810                "             {List1, List2,\n"
11811                "              List3});",
11812                getLLVMStyleWithColumns(35));
11813   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n"
11814                "                               aaaaaaaaaaaaaaaaaaaaaaa);");
11815 }
11816 
11817 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
11818   FormatStyle DoNotMerge = getLLVMStyle();
11819   DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
11820 
11821   verifyFormat("void f() { return 42; }");
11822   verifyFormat("void f() {\n"
11823                "  return 42;\n"
11824                "}",
11825                DoNotMerge);
11826   verifyFormat("void f() {\n"
11827                "  // Comment\n"
11828                "}");
11829   verifyFormat("{\n"
11830                "#error {\n"
11831                "  int a;\n"
11832                "}");
11833   verifyFormat("{\n"
11834                "  int a;\n"
11835                "#error {\n"
11836                "}");
11837   verifyFormat("void f() {} // comment");
11838   verifyFormat("void f() { int a; } // comment");
11839   verifyFormat("void f() {\n"
11840                "} // comment",
11841                DoNotMerge);
11842   verifyFormat("void f() {\n"
11843                "  int a;\n"
11844                "} // comment",
11845                DoNotMerge);
11846   verifyFormat("void f() {\n"
11847                "} // comment",
11848                getLLVMStyleWithColumns(15));
11849 
11850   verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
11851   verifyFormat("void f() {\n  return 42;\n}", getLLVMStyleWithColumns(22));
11852 
11853   verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
11854   verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
11855   verifyFormat("class C {\n"
11856                "  C()\n"
11857                "      : iiiiiiii(nullptr),\n"
11858                "        kkkkkkk(nullptr),\n"
11859                "        mmmmmmm(nullptr),\n"
11860                "        nnnnnnn(nullptr) {}\n"
11861                "};",
11862                getGoogleStyle());
11863 
11864   FormatStyle NoColumnLimit = getLLVMStyle();
11865   NoColumnLimit.ColumnLimit = 0;
11866   EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit));
11867   EXPECT_EQ("class C {\n"
11868             "  A() : b(0) {}\n"
11869             "};",
11870             format("class C{A():b(0){}};", NoColumnLimit));
11871   EXPECT_EQ("A()\n"
11872             "    : b(0) {\n"
11873             "}",
11874             format("A()\n:b(0)\n{\n}", NoColumnLimit));
11875 
11876   FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
11877   DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
11878       FormatStyle::SFS_None;
11879   EXPECT_EQ("A()\n"
11880             "    : b(0) {\n"
11881             "}",
11882             format("A():b(0){}", DoNotMergeNoColumnLimit));
11883   EXPECT_EQ("A()\n"
11884             "    : b(0) {\n"
11885             "}",
11886             format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit));
11887 
11888   verifyFormat("#define A          \\\n"
11889                "  void f() {       \\\n"
11890                "    int i;         \\\n"
11891                "  }",
11892                getLLVMStyleWithColumns(20));
11893   verifyFormat("#define A           \\\n"
11894                "  void f() { int i; }",
11895                getLLVMStyleWithColumns(21));
11896   verifyFormat("#define A            \\\n"
11897                "  void f() {         \\\n"
11898                "    int i;           \\\n"
11899                "  }                  \\\n"
11900                "  int j;",
11901                getLLVMStyleWithColumns(22));
11902   verifyFormat("#define A             \\\n"
11903                "  void f() { int i; } \\\n"
11904                "  int j;",
11905                getLLVMStyleWithColumns(23));
11906 }
11907 
11908 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) {
11909   FormatStyle MergeEmptyOnly = getLLVMStyle();
11910   MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
11911   verifyFormat("class C {\n"
11912                "  int f() {}\n"
11913                "};",
11914                MergeEmptyOnly);
11915   verifyFormat("class C {\n"
11916                "  int f() {\n"
11917                "    return 42;\n"
11918                "  }\n"
11919                "};",
11920                MergeEmptyOnly);
11921   verifyFormat("int f() {}", MergeEmptyOnly);
11922   verifyFormat("int f() {\n"
11923                "  return 42;\n"
11924                "}",
11925                MergeEmptyOnly);
11926 
11927   // Also verify behavior when BraceWrapping.AfterFunction = true
11928   MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
11929   MergeEmptyOnly.BraceWrapping.AfterFunction = true;
11930   verifyFormat("int f() {}", MergeEmptyOnly);
11931   verifyFormat("class C {\n"
11932                "  int f() {}\n"
11933                "};",
11934                MergeEmptyOnly);
11935 }
11936 
11937 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
11938   FormatStyle MergeInlineOnly = getLLVMStyle();
11939   MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
11940   verifyFormat("class C {\n"
11941                "  int f() { return 42; }\n"
11942                "};",
11943                MergeInlineOnly);
11944   verifyFormat("int f() {\n"
11945                "  return 42;\n"
11946                "}",
11947                MergeInlineOnly);
11948 
11949   // SFS_Inline implies SFS_Empty
11950   verifyFormat("class C {\n"
11951                "  int f() {}\n"
11952                "};",
11953                MergeInlineOnly);
11954   verifyFormat("int f() {}", MergeInlineOnly);
11955 
11956   // Also verify behavior when BraceWrapping.AfterFunction = true
11957   MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
11958   MergeInlineOnly.BraceWrapping.AfterFunction = true;
11959   verifyFormat("class C {\n"
11960                "  int f() { return 42; }\n"
11961                "};",
11962                MergeInlineOnly);
11963   verifyFormat("int f()\n"
11964                "{\n"
11965                "  return 42;\n"
11966                "}",
11967                MergeInlineOnly);
11968 
11969   // SFS_Inline implies SFS_Empty
11970   verifyFormat("int f() {}", MergeInlineOnly);
11971   verifyFormat("class C {\n"
11972                "  int f() {}\n"
11973                "};",
11974                MergeInlineOnly);
11975 }
11976 
11977 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) {
11978   FormatStyle MergeInlineOnly = getLLVMStyle();
11979   MergeInlineOnly.AllowShortFunctionsOnASingleLine =
11980       FormatStyle::SFS_InlineOnly;
11981   verifyFormat("class C {\n"
11982                "  int f() { return 42; }\n"
11983                "};",
11984                MergeInlineOnly);
11985   verifyFormat("int f() {\n"
11986                "  return 42;\n"
11987                "}",
11988                MergeInlineOnly);
11989 
11990   // SFS_InlineOnly does not imply SFS_Empty
11991   verifyFormat("class C {\n"
11992                "  int f() {}\n"
11993                "};",
11994                MergeInlineOnly);
11995   verifyFormat("int f() {\n"
11996                "}",
11997                MergeInlineOnly);
11998 
11999   // Also verify behavior when BraceWrapping.AfterFunction = true
12000   MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
12001   MergeInlineOnly.BraceWrapping.AfterFunction = true;
12002   verifyFormat("class C {\n"
12003                "  int f() { return 42; }\n"
12004                "};",
12005                MergeInlineOnly);
12006   verifyFormat("int f()\n"
12007                "{\n"
12008                "  return 42;\n"
12009                "}",
12010                MergeInlineOnly);
12011 
12012   // SFS_InlineOnly does not imply SFS_Empty
12013   verifyFormat("int f()\n"
12014                "{\n"
12015                "}",
12016                MergeInlineOnly);
12017   verifyFormat("class C {\n"
12018                "  int f() {}\n"
12019                "};",
12020                MergeInlineOnly);
12021 }
12022 
12023 TEST_F(FormatTest, SplitEmptyFunction) {
12024   FormatStyle Style = getLLVMStyle();
12025   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
12026   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12027   Style.BraceWrapping.AfterFunction = true;
12028   Style.BraceWrapping.SplitEmptyFunction = false;
12029   Style.ColumnLimit = 40;
12030 
12031   verifyFormat("int f()\n"
12032                "{}",
12033                Style);
12034   verifyFormat("int f()\n"
12035                "{\n"
12036                "  return 42;\n"
12037                "}",
12038                Style);
12039   verifyFormat("int f()\n"
12040                "{\n"
12041                "  // some comment\n"
12042                "}",
12043                Style);
12044 
12045   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
12046   verifyFormat("int f() {}", Style);
12047   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
12048                "{}",
12049                Style);
12050   verifyFormat("int f()\n"
12051                "{\n"
12052                "  return 0;\n"
12053                "}",
12054                Style);
12055 
12056   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
12057   verifyFormat("class Foo {\n"
12058                "  int f() {}\n"
12059                "};\n",
12060                Style);
12061   verifyFormat("class Foo {\n"
12062                "  int f() { return 0; }\n"
12063                "};\n",
12064                Style);
12065   verifyFormat("class Foo {\n"
12066                "  int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
12067                "  {}\n"
12068                "};\n",
12069                Style);
12070   verifyFormat("class Foo {\n"
12071                "  int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
12072                "  {\n"
12073                "    return 0;\n"
12074                "  }\n"
12075                "};\n",
12076                Style);
12077 
12078   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
12079   verifyFormat("int f() {}", Style);
12080   verifyFormat("int f() { return 0; }", Style);
12081   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
12082                "{}",
12083                Style);
12084   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
12085                "{\n"
12086                "  return 0;\n"
12087                "}",
12088                Style);
12089 }
12090 TEST_F(FormatTest, KeepShortFunctionAfterPPElse) {
12091   FormatStyle Style = getLLVMStyle();
12092   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
12093   verifyFormat("#ifdef A\n"
12094                "int f() {}\n"
12095                "#else\n"
12096                "int g() {}\n"
12097                "#endif",
12098                Style);
12099 }
12100 
12101 TEST_F(FormatTest, SplitEmptyClass) {
12102   FormatStyle Style = getLLVMStyle();
12103   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12104   Style.BraceWrapping.AfterClass = true;
12105   Style.BraceWrapping.SplitEmptyRecord = false;
12106 
12107   verifyFormat("class Foo\n"
12108                "{};",
12109                Style);
12110   verifyFormat("/* something */ class Foo\n"
12111                "{};",
12112                Style);
12113   verifyFormat("template <typename X> class Foo\n"
12114                "{};",
12115                Style);
12116   verifyFormat("class Foo\n"
12117                "{\n"
12118                "  Foo();\n"
12119                "};",
12120                Style);
12121   verifyFormat("typedef class Foo\n"
12122                "{\n"
12123                "} Foo_t;",
12124                Style);
12125 
12126   Style.BraceWrapping.SplitEmptyRecord = true;
12127   Style.BraceWrapping.AfterStruct = true;
12128   verifyFormat("class rep\n"
12129                "{\n"
12130                "};",
12131                Style);
12132   verifyFormat("struct rep\n"
12133                "{\n"
12134                "};",
12135                Style);
12136   verifyFormat("template <typename T> class rep\n"
12137                "{\n"
12138                "};",
12139                Style);
12140   verifyFormat("template <typename T> struct rep\n"
12141                "{\n"
12142                "};",
12143                Style);
12144   verifyFormat("class rep\n"
12145                "{\n"
12146                "  int x;\n"
12147                "};",
12148                Style);
12149   verifyFormat("struct rep\n"
12150                "{\n"
12151                "  int x;\n"
12152                "};",
12153                Style);
12154   verifyFormat("template <typename T> class rep\n"
12155                "{\n"
12156                "  int x;\n"
12157                "};",
12158                Style);
12159   verifyFormat("template <typename T> struct rep\n"
12160                "{\n"
12161                "  int x;\n"
12162                "};",
12163                Style);
12164   verifyFormat("template <typename T> class rep // Foo\n"
12165                "{\n"
12166                "  int x;\n"
12167                "};",
12168                Style);
12169   verifyFormat("template <typename T> struct rep // Bar\n"
12170                "{\n"
12171                "  int x;\n"
12172                "};",
12173                Style);
12174 
12175   verifyFormat("template <typename T> class rep<T>\n"
12176                "{\n"
12177                "  int x;\n"
12178                "};",
12179                Style);
12180 
12181   verifyFormat("template <typename T> class rep<std::complex<T>>\n"
12182                "{\n"
12183                "  int x;\n"
12184                "};",
12185                Style);
12186   verifyFormat("template <typename T> class rep<std::complex<T>>\n"
12187                "{\n"
12188                "};",
12189                Style);
12190 
12191   verifyFormat("#include \"stdint.h\"\n"
12192                "namespace rep {}",
12193                Style);
12194   verifyFormat("#include <stdint.h>\n"
12195                "namespace rep {}",
12196                Style);
12197   verifyFormat("#include <stdint.h>\n"
12198                "namespace rep {}",
12199                "#include <stdint.h>\n"
12200                "namespace rep {\n"
12201                "\n"
12202                "\n"
12203                "}",
12204                Style);
12205 }
12206 
12207 TEST_F(FormatTest, SplitEmptyStruct) {
12208   FormatStyle Style = getLLVMStyle();
12209   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12210   Style.BraceWrapping.AfterStruct = true;
12211   Style.BraceWrapping.SplitEmptyRecord = false;
12212 
12213   verifyFormat("struct Foo\n"
12214                "{};",
12215                Style);
12216   verifyFormat("/* something */ struct Foo\n"
12217                "{};",
12218                Style);
12219   verifyFormat("template <typename X> struct Foo\n"
12220                "{};",
12221                Style);
12222   verifyFormat("struct Foo\n"
12223                "{\n"
12224                "  Foo();\n"
12225                "};",
12226                Style);
12227   verifyFormat("typedef struct Foo\n"
12228                "{\n"
12229                "} Foo_t;",
12230                Style);
12231   // typedef struct Bar {} Bar_t;
12232 }
12233 
12234 TEST_F(FormatTest, SplitEmptyUnion) {
12235   FormatStyle Style = getLLVMStyle();
12236   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12237   Style.BraceWrapping.AfterUnion = true;
12238   Style.BraceWrapping.SplitEmptyRecord = false;
12239 
12240   verifyFormat("union Foo\n"
12241                "{};",
12242                Style);
12243   verifyFormat("/* something */ union Foo\n"
12244                "{};",
12245                Style);
12246   verifyFormat("union Foo\n"
12247                "{\n"
12248                "  A,\n"
12249                "};",
12250                Style);
12251   verifyFormat("typedef union Foo\n"
12252                "{\n"
12253                "} Foo_t;",
12254                Style);
12255 }
12256 
12257 TEST_F(FormatTest, SplitEmptyNamespace) {
12258   FormatStyle Style = getLLVMStyle();
12259   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12260   Style.BraceWrapping.AfterNamespace = true;
12261   Style.BraceWrapping.SplitEmptyNamespace = false;
12262 
12263   verifyFormat("namespace Foo\n"
12264                "{};",
12265                Style);
12266   verifyFormat("/* something */ namespace Foo\n"
12267                "{};",
12268                Style);
12269   verifyFormat("inline namespace Foo\n"
12270                "{};",
12271                Style);
12272   verifyFormat("/* something */ inline namespace Foo\n"
12273                "{};",
12274                Style);
12275   verifyFormat("export namespace Foo\n"
12276                "{};",
12277                Style);
12278   verifyFormat("namespace Foo\n"
12279                "{\n"
12280                "void Bar();\n"
12281                "};",
12282                Style);
12283 }
12284 
12285 TEST_F(FormatTest, NeverMergeShortRecords) {
12286   FormatStyle Style = getLLVMStyle();
12287 
12288   verifyFormat("class Foo {\n"
12289                "  Foo();\n"
12290                "};",
12291                Style);
12292   verifyFormat("typedef class Foo {\n"
12293                "  Foo();\n"
12294                "} Foo_t;",
12295                Style);
12296   verifyFormat("struct Foo {\n"
12297                "  Foo();\n"
12298                "};",
12299                Style);
12300   verifyFormat("typedef struct Foo {\n"
12301                "  Foo();\n"
12302                "} Foo_t;",
12303                Style);
12304   verifyFormat("union Foo {\n"
12305                "  A,\n"
12306                "};",
12307                Style);
12308   verifyFormat("typedef union Foo {\n"
12309                "  A,\n"
12310                "} Foo_t;",
12311                Style);
12312   verifyFormat("namespace Foo {\n"
12313                "void Bar();\n"
12314                "};",
12315                Style);
12316 
12317   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12318   Style.BraceWrapping.AfterClass = true;
12319   Style.BraceWrapping.AfterStruct = true;
12320   Style.BraceWrapping.AfterUnion = true;
12321   Style.BraceWrapping.AfterNamespace = true;
12322   verifyFormat("class Foo\n"
12323                "{\n"
12324                "  Foo();\n"
12325                "};",
12326                Style);
12327   verifyFormat("typedef class Foo\n"
12328                "{\n"
12329                "  Foo();\n"
12330                "} Foo_t;",
12331                Style);
12332   verifyFormat("struct Foo\n"
12333                "{\n"
12334                "  Foo();\n"
12335                "};",
12336                Style);
12337   verifyFormat("typedef struct Foo\n"
12338                "{\n"
12339                "  Foo();\n"
12340                "} Foo_t;",
12341                Style);
12342   verifyFormat("union Foo\n"
12343                "{\n"
12344                "  A,\n"
12345                "};",
12346                Style);
12347   verifyFormat("typedef union Foo\n"
12348                "{\n"
12349                "  A,\n"
12350                "} Foo_t;",
12351                Style);
12352   verifyFormat("namespace Foo\n"
12353                "{\n"
12354                "void Bar();\n"
12355                "};",
12356                Style);
12357 }
12358 
12359 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
12360   // Elaborate type variable declarations.
12361   verifyFormat("struct foo a = {bar};\nint n;");
12362   verifyFormat("class foo a = {bar};\nint n;");
12363   verifyFormat("union foo a = {bar};\nint n;");
12364 
12365   // Elaborate types inside function definitions.
12366   verifyFormat("struct foo f() {}\nint n;");
12367   verifyFormat("class foo f() {}\nint n;");
12368   verifyFormat("union foo f() {}\nint n;");
12369 
12370   // Templates.
12371   verifyFormat("template <class X> void f() {}\nint n;");
12372   verifyFormat("template <struct X> void f() {}\nint n;");
12373   verifyFormat("template <union X> void f() {}\nint n;");
12374 
12375   // Actual definitions...
12376   verifyFormat("struct {\n} n;");
12377   verifyFormat(
12378       "template <template <class T, class Y>, class Z> class X {\n} n;");
12379   verifyFormat("union Z {\n  int n;\n} x;");
12380   verifyFormat("class MACRO Z {\n} n;");
12381   verifyFormat("class MACRO(X) Z {\n} n;");
12382   verifyFormat("class __attribute__(X) Z {\n} n;");
12383   verifyFormat("class __declspec(X) Z {\n} n;");
12384   verifyFormat("class A##B##C {\n} n;");
12385   verifyFormat("class alignas(16) Z {\n} n;");
12386   verifyFormat("class MACRO(X) alignas(16) Z {\n} n;");
12387   verifyFormat("class MACROA MACRO(X) Z {\n} n;");
12388 
12389   // Redefinition from nested context:
12390   verifyFormat("class A::B::C {\n} n;");
12391 
12392   // Template definitions.
12393   verifyFormat(
12394       "template <typename F>\n"
12395       "Matcher(const Matcher<F> &Other,\n"
12396       "        typename enable_if_c<is_base_of<F, T>::value &&\n"
12397       "                             !is_same<F, T>::value>::type * = 0)\n"
12398       "    : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
12399 
12400   // FIXME: This is still incorrectly handled at the formatter side.
12401   verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
12402   verifyFormat("int i = SomeFunction(a<b, a> b);");
12403 
12404   // FIXME:
12405   // This now gets parsed incorrectly as class definition.
12406   // verifyFormat("class A<int> f() {\n}\nint n;");
12407 
12408   // Elaborate types where incorrectly parsing the structural element would
12409   // break the indent.
12410   verifyFormat("if (true)\n"
12411                "  class X x;\n"
12412                "else\n"
12413                "  f();\n");
12414 
12415   // This is simply incomplete. Formatting is not important, but must not crash.
12416   verifyFormat("class A:");
12417 }
12418 
12419 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
12420   EXPECT_EQ("#error Leave     all         white!!!!! space* alone!\n",
12421             format("#error Leave     all         white!!!!! space* alone!\n"));
12422   EXPECT_EQ(
12423       "#warning Leave     all         white!!!!! space* alone!\n",
12424       format("#warning Leave     all         white!!!!! space* alone!\n"));
12425   EXPECT_EQ("#error 1", format("  #  error   1"));
12426   EXPECT_EQ("#warning 1", format("  #  warning 1"));
12427 }
12428 
12429 TEST_F(FormatTest, FormatHashIfExpressions) {
12430   verifyFormat("#if AAAA && BBBB");
12431   verifyFormat("#if (AAAA && BBBB)");
12432   verifyFormat("#elif (AAAA && BBBB)");
12433   // FIXME: Come up with a better indentation for #elif.
12434   verifyFormat(
12435       "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) &&  \\\n"
12436       "    defined(BBBBBBBB)\n"
12437       "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) &&  \\\n"
12438       "    defined(BBBBBBBB)\n"
12439       "#endif",
12440       getLLVMStyleWithColumns(65));
12441 }
12442 
12443 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
12444   FormatStyle AllowsMergedIf = getGoogleStyle();
12445   AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
12446       FormatStyle::SIS_WithoutElse;
12447   verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
12448   verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
12449   verifyFormat("if (true)\n#error E\n  return 42;", AllowsMergedIf);
12450   EXPECT_EQ("if (true) return 42;",
12451             format("if (true)\nreturn 42;", AllowsMergedIf));
12452   FormatStyle ShortMergedIf = AllowsMergedIf;
12453   ShortMergedIf.ColumnLimit = 25;
12454   verifyFormat("#define A \\\n"
12455                "  if (true) return 42;",
12456                ShortMergedIf);
12457   verifyFormat("#define A \\\n"
12458                "  f();    \\\n"
12459                "  if (true)\n"
12460                "#define B",
12461                ShortMergedIf);
12462   verifyFormat("#define A \\\n"
12463                "  f();    \\\n"
12464                "  if (true)\n"
12465                "g();",
12466                ShortMergedIf);
12467   verifyFormat("{\n"
12468                "#ifdef A\n"
12469                "  // Comment\n"
12470                "  if (true) continue;\n"
12471                "#endif\n"
12472                "  // Comment\n"
12473                "  if (true) continue;\n"
12474                "}",
12475                ShortMergedIf);
12476   ShortMergedIf.ColumnLimit = 33;
12477   verifyFormat("#define A \\\n"
12478                "  if constexpr (true) return 42;",
12479                ShortMergedIf);
12480   verifyFormat("#define A \\\n"
12481                "  if CONSTEXPR (true) return 42;",
12482                ShortMergedIf);
12483   ShortMergedIf.ColumnLimit = 29;
12484   verifyFormat("#define A                   \\\n"
12485                "  if (aaaaaaaaaa) return 1; \\\n"
12486                "  return 2;",
12487                ShortMergedIf);
12488   ShortMergedIf.ColumnLimit = 28;
12489   verifyFormat("#define A         \\\n"
12490                "  if (aaaaaaaaaa) \\\n"
12491                "    return 1;     \\\n"
12492                "  return 2;",
12493                ShortMergedIf);
12494   verifyFormat("#define A                \\\n"
12495                "  if constexpr (aaaaaaa) \\\n"
12496                "    return 1;            \\\n"
12497                "  return 2;",
12498                ShortMergedIf);
12499   verifyFormat("#define A                \\\n"
12500                "  if CONSTEXPR (aaaaaaa) \\\n"
12501                "    return 1;            \\\n"
12502                "  return 2;",
12503                ShortMergedIf);
12504 }
12505 
12506 TEST_F(FormatTest, FormatStarDependingOnContext) {
12507   verifyFormat("void f(int *a);");
12508   verifyFormat("void f() { f(fint * b); }");
12509   verifyFormat("class A {\n  void f(int *a);\n};");
12510   verifyFormat("class A {\n  int *a;\n};");
12511   verifyFormat("namespace a {\n"
12512                "namespace b {\n"
12513                "class A {\n"
12514                "  void f() {}\n"
12515                "  int *a;\n"
12516                "};\n"
12517                "} // namespace b\n"
12518                "} // namespace a");
12519 }
12520 
12521 TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
12522   verifyFormat("while");
12523   verifyFormat("operator");
12524 }
12525 
12526 TEST_F(FormatTest, SkipsDeeplyNestedLines) {
12527   // This code would be painfully slow to format if we didn't skip it.
12528   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
12529                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
12530                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
12531                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
12532                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
12533                    "A(1, 1)\n"
12534                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x
12535                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12536                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12537                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12538                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12539                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12540                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12541                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12542                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12543                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n");
12544   // Deeply nested part is untouched, rest is formatted.
12545   EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n",
12546             format(std::string("int    i;\n") + Code + "int    j;\n",
12547                    getLLVMStyle(), SC_ExpectIncomplete));
12548 }
12549 
12550 //===----------------------------------------------------------------------===//
12551 // Objective-C tests.
12552 //===----------------------------------------------------------------------===//
12553 
12554 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
12555   verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
12556   EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
12557             format("-(NSUInteger)indexOfObject:(id)anObject;"));
12558   EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
12559   EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
12560   EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
12561             format("-(NSInteger)Method3:(id)anObject;"));
12562   EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
12563             format("-(NSInteger)Method4:(id)anObject;"));
12564   EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
12565             format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
12566   EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
12567             format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
12568   EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject "
12569             "forAllCells:(BOOL)flag;",
12570             format("- (void)sendAction:(SEL)aSelector to:(id)anObject "
12571                    "forAllCells:(BOOL)flag;"));
12572 
12573   // Very long objectiveC method declaration.
12574   verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
12575                "    (SoooooooooooooooooooooomeType *)bbbbbbbbbb;");
12576   verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
12577                "                    inRange:(NSRange)range\n"
12578                "                   outRange:(NSRange)out_range\n"
12579                "                  outRange1:(NSRange)out_range1\n"
12580                "                  outRange2:(NSRange)out_range2\n"
12581                "                  outRange3:(NSRange)out_range3\n"
12582                "                  outRange4:(NSRange)out_range4\n"
12583                "                  outRange5:(NSRange)out_range5\n"
12584                "                  outRange6:(NSRange)out_range6\n"
12585                "                  outRange7:(NSRange)out_range7\n"
12586                "                  outRange8:(NSRange)out_range8\n"
12587                "                  outRange9:(NSRange)out_range9;");
12588 
12589   // When the function name has to be wrapped.
12590   FormatStyle Style = getLLVMStyle();
12591   // ObjC ignores IndentWrappedFunctionNames when wrapping methods
12592   // and always indents instead.
12593   Style.IndentWrappedFunctionNames = false;
12594   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
12595                "    veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
12596                "               anotherName:(NSString)bbbbbbbbbbbbbb {\n"
12597                "}",
12598                Style);
12599   Style.IndentWrappedFunctionNames = true;
12600   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
12601                "    veryLooooooooooongName:(NSString)cccccccccccccc\n"
12602                "               anotherName:(NSString)dddddddddddddd {\n"
12603                "}",
12604                Style);
12605 
12606   verifyFormat("- (int)sum:(vector<int>)numbers;");
12607   verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
12608   // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
12609   // protocol lists (but not for template classes):
12610   // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
12611 
12612   verifyFormat("- (int (*)())foo:(int (*)())f;");
12613   verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
12614 
12615   // If there's no return type (very rare in practice!), LLVM and Google style
12616   // agree.
12617   verifyFormat("- foo;");
12618   verifyFormat("- foo:(int)f;");
12619   verifyGoogleFormat("- foo:(int)foo;");
12620 }
12621 
12622 TEST_F(FormatTest, BreaksStringLiterals) {
12623   EXPECT_EQ("\"some text \"\n"
12624             "\"other\";",
12625             format("\"some text other\";", getLLVMStyleWithColumns(12)));
12626   EXPECT_EQ("\"some text \"\n"
12627             "\"other\";",
12628             format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
12629   EXPECT_EQ(
12630       "#define A  \\\n"
12631       "  \"some \"  \\\n"
12632       "  \"text \"  \\\n"
12633       "  \"other\";",
12634       format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
12635   EXPECT_EQ(
12636       "#define A  \\\n"
12637       "  \"so \"    \\\n"
12638       "  \"text \"  \\\n"
12639       "  \"other\";",
12640       format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
12641 
12642   EXPECT_EQ("\"some text\"",
12643             format("\"some text\"", getLLVMStyleWithColumns(1)));
12644   EXPECT_EQ("\"some text\"",
12645             format("\"some text\"", getLLVMStyleWithColumns(11)));
12646   EXPECT_EQ("\"some \"\n"
12647             "\"text\"",
12648             format("\"some text\"", getLLVMStyleWithColumns(10)));
12649   EXPECT_EQ("\"some \"\n"
12650             "\"text\"",
12651             format("\"some text\"", getLLVMStyleWithColumns(7)));
12652   EXPECT_EQ("\"some\"\n"
12653             "\" tex\"\n"
12654             "\"t\"",
12655             format("\"some text\"", getLLVMStyleWithColumns(6)));
12656   EXPECT_EQ("\"some\"\n"
12657             "\" tex\"\n"
12658             "\" and\"",
12659             format("\"some tex and\"", getLLVMStyleWithColumns(6)));
12660   EXPECT_EQ("\"some\"\n"
12661             "\"/tex\"\n"
12662             "\"/and\"",
12663             format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
12664 
12665   EXPECT_EQ("variable =\n"
12666             "    \"long string \"\n"
12667             "    \"literal\";",
12668             format("variable = \"long string literal\";",
12669                    getLLVMStyleWithColumns(20)));
12670 
12671   EXPECT_EQ("variable = f(\n"
12672             "    \"long string \"\n"
12673             "    \"literal\",\n"
12674             "    short,\n"
12675             "    loooooooooooooooooooong);",
12676             format("variable = f(\"long string literal\", short, "
12677                    "loooooooooooooooooooong);",
12678                    getLLVMStyleWithColumns(20)));
12679 
12680   EXPECT_EQ(
12681       "f(g(\"long string \"\n"
12682       "    \"literal\"),\n"
12683       "  b);",
12684       format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20)));
12685   EXPECT_EQ("f(g(\"long string \"\n"
12686             "    \"literal\",\n"
12687             "    a),\n"
12688             "  b);",
12689             format("f(g(\"long string literal\", a), b);",
12690                    getLLVMStyleWithColumns(20)));
12691   EXPECT_EQ(
12692       "f(\"one two\".split(\n"
12693       "    variable));",
12694       format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
12695   EXPECT_EQ("f(\"one two three four five six \"\n"
12696             "  \"seven\".split(\n"
12697             "      really_looooong_variable));",
12698             format("f(\"one two three four five six seven\"."
12699                    "split(really_looooong_variable));",
12700                    getLLVMStyleWithColumns(33)));
12701 
12702   EXPECT_EQ("f(\"some \"\n"
12703             "  \"text\",\n"
12704             "  other);",
12705             format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
12706 
12707   // Only break as a last resort.
12708   verifyFormat(
12709       "aaaaaaaaaaaaaaaaaaaa(\n"
12710       "    aaaaaaaaaaaaaaaaaaaa,\n"
12711       "    aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
12712 
12713   EXPECT_EQ("\"splitmea\"\n"
12714             "\"trandomp\"\n"
12715             "\"oint\"",
12716             format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
12717 
12718   EXPECT_EQ("\"split/\"\n"
12719             "\"pathat/\"\n"
12720             "\"slashes\"",
12721             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
12722 
12723   EXPECT_EQ("\"split/\"\n"
12724             "\"pathat/\"\n"
12725             "\"slashes\"",
12726             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
12727   EXPECT_EQ("\"split at \"\n"
12728             "\"spaces/at/\"\n"
12729             "\"slashes.at.any$\"\n"
12730             "\"non-alphanumeric%\"\n"
12731             "\"1111111111characte\"\n"
12732             "\"rs\"",
12733             format("\"split at "
12734                    "spaces/at/"
12735                    "slashes.at."
12736                    "any$non-"
12737                    "alphanumeric%"
12738                    "1111111111characte"
12739                    "rs\"",
12740                    getLLVMStyleWithColumns(20)));
12741 
12742   // Verify that splitting the strings understands
12743   // Style::AlwaysBreakBeforeMultilineStrings.
12744   EXPECT_EQ("aaaaaaaaaaaa(\n"
12745             "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
12746             "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
12747             format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa "
12748                    "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
12749                    "aaaaaaaaaaaaaaaaaaaaaa\");",
12750                    getGoogleStyle()));
12751   EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
12752             "       \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
12753             format("return \"aaaaaaaaaaaaaaaaaaaaaa "
12754                    "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
12755                    "aaaaaaaaaaaaaaaaaaaaaa\";",
12756                    getGoogleStyle()));
12757   EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
12758             "                \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
12759             format("llvm::outs() << "
12760                    "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
12761                    "aaaaaaaaaaaaaaaaaaa\";"));
12762   EXPECT_EQ("ffff(\n"
12763             "    {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
12764             "     \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
12765             format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
12766                    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
12767                    getGoogleStyle()));
12768 
12769   FormatStyle Style = getLLVMStyleWithColumns(12);
12770   Style.BreakStringLiterals = false;
12771   EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style));
12772 
12773   FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
12774   AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
12775   EXPECT_EQ("#define A \\\n"
12776             "  \"some \" \\\n"
12777             "  \"text \" \\\n"
12778             "  \"other\";",
12779             format("#define A \"some text other\";", AlignLeft));
12780 }
12781 
12782 TEST_F(FormatTest, BreaksStringLiteralsAtColumnLimit) {
12783   EXPECT_EQ("C a = \"some more \"\n"
12784             "      \"text\";",
12785             format("C a = \"some more text\";", getLLVMStyleWithColumns(18)));
12786 }
12787 
12788 TEST_F(FormatTest, FullyRemoveEmptyLines) {
12789   FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80);
12790   NoEmptyLines.MaxEmptyLinesToKeep = 0;
12791   EXPECT_EQ("int i = a(b());",
12792             format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines));
12793 }
12794 
12795 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
12796   EXPECT_EQ(
12797       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
12798       "(\n"
12799       "    \"x\t\");",
12800       format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
12801              "aaaaaaa("
12802              "\"x\t\");"));
12803 }
12804 
12805 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
12806   EXPECT_EQ(
12807       "u8\"utf8 string \"\n"
12808       "u8\"literal\";",
12809       format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
12810   EXPECT_EQ(
12811       "u\"utf16 string \"\n"
12812       "u\"literal\";",
12813       format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
12814   EXPECT_EQ(
12815       "U\"utf32 string \"\n"
12816       "U\"literal\";",
12817       format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
12818   EXPECT_EQ("L\"wide string \"\n"
12819             "L\"literal\";",
12820             format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
12821   EXPECT_EQ("@\"NSString \"\n"
12822             "@\"literal\";",
12823             format("@\"NSString literal\";", getGoogleStyleWithColumns(19)));
12824   verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26));
12825 
12826   // This input makes clang-format try to split the incomplete unicode escape
12827   // sequence, which used to lead to a crasher.
12828   verifyNoCrash(
12829       "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
12830       getLLVMStyleWithColumns(60));
12831 }
12832 
12833 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) {
12834   FormatStyle Style = getGoogleStyleWithColumns(15);
12835   EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style));
12836   EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style));
12837   EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style));
12838   EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style));
12839   EXPECT_EQ("u8R\"x(raw literal)x\";",
12840             format("u8R\"x(raw literal)x\";", Style));
12841 }
12842 
12843 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
12844   FormatStyle Style = getLLVMStyleWithColumns(20);
12845   EXPECT_EQ(
12846       "_T(\"aaaaaaaaaaaaaa\")\n"
12847       "_T(\"aaaaaaaaaaaaaa\")\n"
12848       "_T(\"aaaaaaaaaaaa\")",
12849       format("  _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
12850   EXPECT_EQ("f(x,\n"
12851             "  _T(\"aaaaaaaaaaaa\")\n"
12852             "  _T(\"aaa\"),\n"
12853             "  z);",
12854             format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style));
12855 
12856   // FIXME: Handle embedded spaces in one iteration.
12857   //  EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
12858   //            "_T(\"aaaaaaaaaaaaa\")\n"
12859   //            "_T(\"aaaaaaaaaaaaa\")\n"
12860   //            "_T(\"a\")",
12861   //            format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
12862   //                   getLLVMStyleWithColumns(20)));
12863   EXPECT_EQ(
12864       "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
12865       format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style));
12866   EXPECT_EQ("f(\n"
12867             "#if !TEST\n"
12868             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
12869             "#endif\n"
12870             ");",
12871             format("f(\n"
12872                    "#if !TEST\n"
12873                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
12874                    "#endif\n"
12875                    ");"));
12876   EXPECT_EQ("f(\n"
12877             "\n"
12878             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));",
12879             format("f(\n"
12880                    "\n"
12881                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));"));
12882   // Regression test for accessing tokens past the end of a vector in the
12883   // TokenLexer.
12884   verifyNoCrash(R"(_T(
12885 "
12886 )
12887 )");
12888 }
12889 
12890 TEST_F(FormatTest, BreaksStringLiteralOperands) {
12891   // In a function call with two operands, the second can be broken with no line
12892   // break before it.
12893   EXPECT_EQ(
12894       "func(a, \"long long \"\n"
12895       "        \"long long\");",
12896       format("func(a, \"long long long long\");", getLLVMStyleWithColumns(24)));
12897   // In a function call with three operands, the second must be broken with a
12898   // line break before it.
12899   EXPECT_EQ("func(a,\n"
12900             "     \"long long long \"\n"
12901             "     \"long\",\n"
12902             "     c);",
12903             format("func(a, \"long long long long\", c);",
12904                    getLLVMStyleWithColumns(24)));
12905   // In a function call with three operands, the third must be broken with a
12906   // line break before it.
12907   EXPECT_EQ("func(a, b,\n"
12908             "     \"long long long \"\n"
12909             "     \"long\");",
12910             format("func(a, b, \"long long long long\");",
12911                    getLLVMStyleWithColumns(24)));
12912   // In a function call with three operands, both the second and the third must
12913   // be broken with a line break before them.
12914   EXPECT_EQ("func(a,\n"
12915             "     \"long long long \"\n"
12916             "     \"long\",\n"
12917             "     \"long long long \"\n"
12918             "     \"long\");",
12919             format("func(a, \"long long long long\", \"long long long long\");",
12920                    getLLVMStyleWithColumns(24)));
12921   // In a chain of << with two operands, the second can be broken with no line
12922   // break before it.
12923   EXPECT_EQ("a << \"line line \"\n"
12924             "     \"line\";",
12925             format("a << \"line line line\";", getLLVMStyleWithColumns(20)));
12926   // In a chain of << with three operands, the second can be broken with no line
12927   // break before it.
12928   EXPECT_EQ(
12929       "abcde << \"line \"\n"
12930       "         \"line line\"\n"
12931       "      << c;",
12932       format("abcde << \"line line line\" << c;", getLLVMStyleWithColumns(20)));
12933   // In a chain of << with three operands, the third must be broken with a line
12934   // break before it.
12935   EXPECT_EQ(
12936       "a << b\n"
12937       "  << \"line line \"\n"
12938       "     \"line\";",
12939       format("a << b << \"line line line\";", getLLVMStyleWithColumns(20)));
12940   // In a chain of << with three operands, the second can be broken with no line
12941   // break before it and the third must be broken with a line break before it.
12942   EXPECT_EQ("abcd << \"line line \"\n"
12943             "        \"line\"\n"
12944             "     << \"line line \"\n"
12945             "        \"line\";",
12946             format("abcd << \"line line line\" << \"line line line\";",
12947                    getLLVMStyleWithColumns(20)));
12948   // In a chain of binary operators with two operands, the second can be broken
12949   // with no line break before it.
12950   EXPECT_EQ(
12951       "abcd + \"line line \"\n"
12952       "       \"line line\";",
12953       format("abcd + \"line line line line\";", getLLVMStyleWithColumns(20)));
12954   // In a chain of binary operators with three operands, the second must be
12955   // broken with a line break before it.
12956   EXPECT_EQ("abcd +\n"
12957             "    \"line line \"\n"
12958             "    \"line line\" +\n"
12959             "    e;",
12960             format("abcd + \"line line line line\" + e;",
12961                    getLLVMStyleWithColumns(20)));
12962   // In a function call with two operands, with AlignAfterOpenBracket enabled,
12963   // the first must be broken with a line break before it.
12964   FormatStyle Style = getLLVMStyleWithColumns(25);
12965   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
12966   EXPECT_EQ("someFunction(\n"
12967             "    \"long long long \"\n"
12968             "    \"long\",\n"
12969             "    a);",
12970             format("someFunction(\"long long long long\", a);", Style));
12971 }
12972 
12973 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
12974   EXPECT_EQ(
12975       "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
12976       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
12977       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
12978       format("aaaaaaaaaaa  =  \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
12979              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
12980              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"));
12981 }
12982 
12983 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
12984   EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);",
12985             format("f(g(R\"x(raw literal)x\",   a), b);", getGoogleStyle()));
12986   EXPECT_EQ("fffffffffff(g(R\"x(\n"
12987             "multiline raw string literal xxxxxxxxxxxxxx\n"
12988             ")x\",\n"
12989             "              a),\n"
12990             "            b);",
12991             format("fffffffffff(g(R\"x(\n"
12992                    "multiline raw string literal xxxxxxxxxxxxxx\n"
12993                    ")x\", a), b);",
12994                    getGoogleStyleWithColumns(20)));
12995   EXPECT_EQ("fffffffffff(\n"
12996             "    g(R\"x(qqq\n"
12997             "multiline raw string literal xxxxxxxxxxxxxx\n"
12998             ")x\",\n"
12999             "      a),\n"
13000             "    b);",
13001             format("fffffffffff(g(R\"x(qqq\n"
13002                    "multiline raw string literal xxxxxxxxxxxxxx\n"
13003                    ")x\", a), b);",
13004                    getGoogleStyleWithColumns(20)));
13005 
13006   EXPECT_EQ("fffffffffff(R\"x(\n"
13007             "multiline raw string literal xxxxxxxxxxxxxx\n"
13008             ")x\");",
13009             format("fffffffffff(R\"x(\n"
13010                    "multiline raw string literal xxxxxxxxxxxxxx\n"
13011                    ")x\");",
13012                    getGoogleStyleWithColumns(20)));
13013   EXPECT_EQ("fffffffffff(R\"x(\n"
13014             "multiline raw string literal xxxxxxxxxxxxxx\n"
13015             ")x\" + bbbbbb);",
13016             format("fffffffffff(R\"x(\n"
13017                    "multiline raw string literal xxxxxxxxxxxxxx\n"
13018                    ")x\" +   bbbbbb);",
13019                    getGoogleStyleWithColumns(20)));
13020   EXPECT_EQ("fffffffffff(\n"
13021             "    R\"x(\n"
13022             "multiline raw string literal xxxxxxxxxxxxxx\n"
13023             ")x\" +\n"
13024             "    bbbbbb);",
13025             format("fffffffffff(\n"
13026                    " R\"x(\n"
13027                    "multiline raw string literal xxxxxxxxxxxxxx\n"
13028                    ")x\" + bbbbbb);",
13029                    getGoogleStyleWithColumns(20)));
13030   EXPECT_EQ("fffffffffff(R\"(single line raw string)\" + bbbbbb);",
13031             format("fffffffffff(\n"
13032                    " R\"(single line raw string)\" + bbbbbb);"));
13033 }
13034 
13035 TEST_F(FormatTest, SkipsUnknownStringLiterals) {
13036   verifyFormat("string a = \"unterminated;");
13037   EXPECT_EQ("function(\"unterminated,\n"
13038             "         OtherParameter);",
13039             format("function(  \"unterminated,\n"
13040                    "    OtherParameter);"));
13041 }
13042 
13043 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
13044   FormatStyle Style = getLLVMStyle();
13045   Style.Standard = FormatStyle::LS_Cpp03;
13046   EXPECT_EQ("#define x(_a) printf(\"foo\" _a);",
13047             format("#define x(_a) printf(\"foo\"_a);", Style));
13048 }
13049 
13050 TEST_F(FormatTest, CppLexVersion) {
13051   FormatStyle Style = getLLVMStyle();
13052   // Formatting of x * y differs if x is a type.
13053   verifyFormat("void foo() { MACRO(a * b); }", Style);
13054   verifyFormat("void foo() { MACRO(int *b); }", Style);
13055 
13056   // LLVM style uses latest lexer.
13057   verifyFormat("void foo() { MACRO(char8_t *b); }", Style);
13058   Style.Standard = FormatStyle::LS_Cpp17;
13059   // But in c++17, char8_t isn't a keyword.
13060   verifyFormat("void foo() { MACRO(char8_t * b); }", Style);
13061 }
13062 
13063 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); }
13064 
13065 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
13066   EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
13067             "             \"ddeeefff\");",
13068             format("someFunction(\"aaabbbcccdddeeefff\");",
13069                    getLLVMStyleWithColumns(25)));
13070   EXPECT_EQ("someFunction1234567890(\n"
13071             "    \"aaabbbcccdddeeefff\");",
13072             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
13073                    getLLVMStyleWithColumns(26)));
13074   EXPECT_EQ("someFunction1234567890(\n"
13075             "    \"aaabbbcccdddeeeff\"\n"
13076             "    \"f\");",
13077             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
13078                    getLLVMStyleWithColumns(25)));
13079   EXPECT_EQ("someFunction1234567890(\n"
13080             "    \"aaabbbcccdddeeeff\"\n"
13081             "    \"f\");",
13082             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
13083                    getLLVMStyleWithColumns(24)));
13084   EXPECT_EQ("someFunction(\n"
13085             "    \"aaabbbcc ddde \"\n"
13086             "    \"efff\");",
13087             format("someFunction(\"aaabbbcc ddde efff\");",
13088                    getLLVMStyleWithColumns(25)));
13089   EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
13090             "             \"ddeeefff\");",
13091             format("someFunction(\"aaabbbccc ddeeefff\");",
13092                    getLLVMStyleWithColumns(25)));
13093   EXPECT_EQ("someFunction1234567890(\n"
13094             "    \"aaabb \"\n"
13095             "    \"cccdddeeefff\");",
13096             format("someFunction1234567890(\"aaabb cccdddeeefff\");",
13097                    getLLVMStyleWithColumns(25)));
13098   EXPECT_EQ("#define A          \\\n"
13099             "  string s =       \\\n"
13100             "      \"123456789\"  \\\n"
13101             "      \"0\";         \\\n"
13102             "  int i;",
13103             format("#define A string s = \"1234567890\"; int i;",
13104                    getLLVMStyleWithColumns(20)));
13105   EXPECT_EQ("someFunction(\n"
13106             "    \"aaabbbcc \"\n"
13107             "    \"dddeeefff\");",
13108             format("someFunction(\"aaabbbcc dddeeefff\");",
13109                    getLLVMStyleWithColumns(25)));
13110 }
13111 
13112 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
13113   EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3)));
13114   EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2)));
13115   EXPECT_EQ("\"test\"\n"
13116             "\"\\n\"",
13117             format("\"test\\n\"", getLLVMStyleWithColumns(7)));
13118   EXPECT_EQ("\"tes\\\\\"\n"
13119             "\"n\"",
13120             format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
13121   EXPECT_EQ("\"\\\\\\\\\"\n"
13122             "\"\\n\"",
13123             format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
13124   EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
13125   EXPECT_EQ("\"\\uff01\"\n"
13126             "\"test\"",
13127             format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
13128   EXPECT_EQ("\"\\Uff01ff02\"",
13129             format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
13130   EXPECT_EQ("\"\\x000000000001\"\n"
13131             "\"next\"",
13132             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
13133   EXPECT_EQ("\"\\x000000000001next\"",
13134             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
13135   EXPECT_EQ("\"\\x000000000001\"",
13136             format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
13137   EXPECT_EQ("\"test\"\n"
13138             "\"\\000000\"\n"
13139             "\"000001\"",
13140             format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
13141   EXPECT_EQ("\"test\\000\"\n"
13142             "\"00000000\"\n"
13143             "\"1\"",
13144             format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
13145 }
13146 
13147 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
13148   verifyFormat("void f() {\n"
13149                "  return g() {}\n"
13150                "  void h() {}");
13151   verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
13152                "g();\n"
13153                "}");
13154 }
13155 
13156 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
13157   verifyFormat(
13158       "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
13159 }
13160 
13161 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
13162   verifyFormat("class X {\n"
13163                "  void f() {\n"
13164                "  }\n"
13165                "};",
13166                getLLVMStyleWithColumns(12));
13167 }
13168 
13169 TEST_F(FormatTest, ConfigurableIndentWidth) {
13170   FormatStyle EightIndent = getLLVMStyleWithColumns(18);
13171   EightIndent.IndentWidth = 8;
13172   EightIndent.ContinuationIndentWidth = 8;
13173   verifyFormat("void f() {\n"
13174                "        someFunction();\n"
13175                "        if (true) {\n"
13176                "                f();\n"
13177                "        }\n"
13178                "}",
13179                EightIndent);
13180   verifyFormat("class X {\n"
13181                "        void f() {\n"
13182                "        }\n"
13183                "};",
13184                EightIndent);
13185   verifyFormat("int x[] = {\n"
13186                "        call(),\n"
13187                "        call()};",
13188                EightIndent);
13189 }
13190 
13191 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
13192   verifyFormat("double\n"
13193                "f();",
13194                getLLVMStyleWithColumns(8));
13195 }
13196 
13197 TEST_F(FormatTest, ConfigurableUseOfTab) {
13198   FormatStyle Tab = getLLVMStyleWithColumns(42);
13199   Tab.IndentWidth = 8;
13200   Tab.UseTab = FormatStyle::UT_Always;
13201   Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left;
13202 
13203   EXPECT_EQ("if (aaaaaaaa && // q\n"
13204             "    bb)\t\t// w\n"
13205             "\t;",
13206             format("if (aaaaaaaa &&// q\n"
13207                    "bb)// w\n"
13208                    ";",
13209                    Tab));
13210   EXPECT_EQ("if (aaa && bbb) // w\n"
13211             "\t;",
13212             format("if(aaa&&bbb)// w\n"
13213                    ";",
13214                    Tab));
13215 
13216   verifyFormat("class X {\n"
13217                "\tvoid f() {\n"
13218                "\t\tsomeFunction(parameter1,\n"
13219                "\t\t\t     parameter2);\n"
13220                "\t}\n"
13221                "};",
13222                Tab);
13223   verifyFormat("#define A                        \\\n"
13224                "\tvoid f() {               \\\n"
13225                "\t\tsomeFunction(    \\\n"
13226                "\t\t    parameter1,  \\\n"
13227                "\t\t    parameter2); \\\n"
13228                "\t}",
13229                Tab);
13230   verifyFormat("int a;\t      // x\n"
13231                "int bbbbbbbb; // x\n",
13232                Tab);
13233 
13234   Tab.TabWidth = 4;
13235   Tab.IndentWidth = 8;
13236   verifyFormat("class TabWidth4Indent8 {\n"
13237                "\t\tvoid f() {\n"
13238                "\t\t\t\tsomeFunction(parameter1,\n"
13239                "\t\t\t\t\t\t\t parameter2);\n"
13240                "\t\t}\n"
13241                "};",
13242                Tab);
13243 
13244   Tab.TabWidth = 4;
13245   Tab.IndentWidth = 4;
13246   verifyFormat("class TabWidth4Indent4 {\n"
13247                "\tvoid f() {\n"
13248                "\t\tsomeFunction(parameter1,\n"
13249                "\t\t\t\t\t parameter2);\n"
13250                "\t}\n"
13251                "};",
13252                Tab);
13253 
13254   Tab.TabWidth = 8;
13255   Tab.IndentWidth = 4;
13256   verifyFormat("class TabWidth8Indent4 {\n"
13257                "    void f() {\n"
13258                "\tsomeFunction(parameter1,\n"
13259                "\t\t     parameter2);\n"
13260                "    }\n"
13261                "};",
13262                Tab);
13263 
13264   Tab.TabWidth = 8;
13265   Tab.IndentWidth = 8;
13266   EXPECT_EQ("/*\n"
13267             "\t      a\t\tcomment\n"
13268             "\t      in multiple lines\n"
13269             "       */",
13270             format("   /*\t \t \n"
13271                    " \t \t a\t\tcomment\t \t\n"
13272                    " \t \t in multiple lines\t\n"
13273                    " \t  */",
13274                    Tab));
13275 
13276   Tab.UseTab = FormatStyle::UT_ForIndentation;
13277   verifyFormat("{\n"
13278                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13279                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13280                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13281                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13282                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13283                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13284                "};",
13285                Tab);
13286   verifyFormat("enum AA {\n"
13287                "\ta1, // Force multiple lines\n"
13288                "\ta2,\n"
13289                "\ta3\n"
13290                "};",
13291                Tab);
13292   EXPECT_EQ("if (aaaaaaaa && // q\n"
13293             "    bb)         // w\n"
13294             "\t;",
13295             format("if (aaaaaaaa &&// q\n"
13296                    "bb)// w\n"
13297                    ";",
13298                    Tab));
13299   verifyFormat("class X {\n"
13300                "\tvoid f() {\n"
13301                "\t\tsomeFunction(parameter1,\n"
13302                "\t\t             parameter2);\n"
13303                "\t}\n"
13304                "};",
13305                Tab);
13306   verifyFormat("{\n"
13307                "\tQ(\n"
13308                "\t    {\n"
13309                "\t\t    int a;\n"
13310                "\t\t    someFunction(aaaaaaaa,\n"
13311                "\t\t                 bbbbbbb);\n"
13312                "\t    },\n"
13313                "\t    p);\n"
13314                "}",
13315                Tab);
13316   EXPECT_EQ("{\n"
13317             "\t/* aaaa\n"
13318             "\t   bbbb */\n"
13319             "}",
13320             format("{\n"
13321                    "/* aaaa\n"
13322                    "   bbbb */\n"
13323                    "}",
13324                    Tab));
13325   EXPECT_EQ("{\n"
13326             "\t/*\n"
13327             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13328             "\t  bbbbbbbbbbbbb\n"
13329             "\t*/\n"
13330             "}",
13331             format("{\n"
13332                    "/*\n"
13333                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13334                    "*/\n"
13335                    "}",
13336                    Tab));
13337   EXPECT_EQ("{\n"
13338             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13339             "\t// bbbbbbbbbbbbb\n"
13340             "}",
13341             format("{\n"
13342                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13343                    "}",
13344                    Tab));
13345   EXPECT_EQ("{\n"
13346             "\t/*\n"
13347             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13348             "\t  bbbbbbbbbbbbb\n"
13349             "\t*/\n"
13350             "}",
13351             format("{\n"
13352                    "\t/*\n"
13353                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13354                    "\t*/\n"
13355                    "}",
13356                    Tab));
13357   EXPECT_EQ("{\n"
13358             "\t/*\n"
13359             "\n"
13360             "\t*/\n"
13361             "}",
13362             format("{\n"
13363                    "\t/*\n"
13364                    "\n"
13365                    "\t*/\n"
13366                    "}",
13367                    Tab));
13368   EXPECT_EQ("{\n"
13369             "\t/*\n"
13370             " asdf\n"
13371             "\t*/\n"
13372             "}",
13373             format("{\n"
13374                    "\t/*\n"
13375                    " asdf\n"
13376                    "\t*/\n"
13377                    "}",
13378                    Tab));
13379 
13380   Tab.UseTab = FormatStyle::UT_Never;
13381   EXPECT_EQ("/*\n"
13382             "              a\t\tcomment\n"
13383             "              in multiple lines\n"
13384             "       */",
13385             format("   /*\t \t \n"
13386                    " \t \t a\t\tcomment\t \t\n"
13387                    " \t \t in multiple lines\t\n"
13388                    " \t  */",
13389                    Tab));
13390   EXPECT_EQ("/* some\n"
13391             "   comment */",
13392             format(" \t \t /* some\n"
13393                    " \t \t    comment */",
13394                    Tab));
13395   EXPECT_EQ("int a; /* some\n"
13396             "   comment */",
13397             format(" \t \t int a; /* some\n"
13398                    " \t \t    comment */",
13399                    Tab));
13400 
13401   EXPECT_EQ("int a; /* some\n"
13402             "comment */",
13403             format(" \t \t int\ta; /* some\n"
13404                    " \t \t    comment */",
13405                    Tab));
13406   EXPECT_EQ("f(\"\t\t\"); /* some\n"
13407             "    comment */",
13408             format(" \t \t f(\"\t\t\"); /* some\n"
13409                    " \t \t    comment */",
13410                    Tab));
13411   EXPECT_EQ("{\n"
13412             "        /*\n"
13413             "         * Comment\n"
13414             "         */\n"
13415             "        int i;\n"
13416             "}",
13417             format("{\n"
13418                    "\t/*\n"
13419                    "\t * Comment\n"
13420                    "\t */\n"
13421                    "\t int i;\n"
13422                    "}",
13423                    Tab));
13424 
13425   Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
13426   Tab.TabWidth = 8;
13427   Tab.IndentWidth = 8;
13428   EXPECT_EQ("if (aaaaaaaa && // q\n"
13429             "    bb)         // w\n"
13430             "\t;",
13431             format("if (aaaaaaaa &&// q\n"
13432                    "bb)// w\n"
13433                    ";",
13434                    Tab));
13435   EXPECT_EQ("if (aaa && bbb) // w\n"
13436             "\t;",
13437             format("if(aaa&&bbb)// w\n"
13438                    ";",
13439                    Tab));
13440   verifyFormat("class X {\n"
13441                "\tvoid f() {\n"
13442                "\t\tsomeFunction(parameter1,\n"
13443                "\t\t\t     parameter2);\n"
13444                "\t}\n"
13445                "};",
13446                Tab);
13447   verifyFormat("#define A                        \\\n"
13448                "\tvoid f() {               \\\n"
13449                "\t\tsomeFunction(    \\\n"
13450                "\t\t    parameter1,  \\\n"
13451                "\t\t    parameter2); \\\n"
13452                "\t}",
13453                Tab);
13454   Tab.TabWidth = 4;
13455   Tab.IndentWidth = 8;
13456   verifyFormat("class TabWidth4Indent8 {\n"
13457                "\t\tvoid f() {\n"
13458                "\t\t\t\tsomeFunction(parameter1,\n"
13459                "\t\t\t\t\t\t\t parameter2);\n"
13460                "\t\t}\n"
13461                "};",
13462                Tab);
13463   Tab.TabWidth = 4;
13464   Tab.IndentWidth = 4;
13465   verifyFormat("class TabWidth4Indent4 {\n"
13466                "\tvoid f() {\n"
13467                "\t\tsomeFunction(parameter1,\n"
13468                "\t\t\t\t\t parameter2);\n"
13469                "\t}\n"
13470                "};",
13471                Tab);
13472   Tab.TabWidth = 8;
13473   Tab.IndentWidth = 4;
13474   verifyFormat("class TabWidth8Indent4 {\n"
13475                "    void f() {\n"
13476                "\tsomeFunction(parameter1,\n"
13477                "\t\t     parameter2);\n"
13478                "    }\n"
13479                "};",
13480                Tab);
13481   Tab.TabWidth = 8;
13482   Tab.IndentWidth = 8;
13483   EXPECT_EQ("/*\n"
13484             "\t      a\t\tcomment\n"
13485             "\t      in multiple lines\n"
13486             "       */",
13487             format("   /*\t \t \n"
13488                    " \t \t a\t\tcomment\t \t\n"
13489                    " \t \t in multiple lines\t\n"
13490                    " \t  */",
13491                    Tab));
13492   verifyFormat("{\n"
13493                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13494                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13495                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13496                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13497                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13498                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13499                "};",
13500                Tab);
13501   verifyFormat("enum AA {\n"
13502                "\ta1, // Force multiple lines\n"
13503                "\ta2,\n"
13504                "\ta3\n"
13505                "};",
13506                Tab);
13507   EXPECT_EQ("if (aaaaaaaa && // q\n"
13508             "    bb)         // w\n"
13509             "\t;",
13510             format("if (aaaaaaaa &&// q\n"
13511                    "bb)// w\n"
13512                    ";",
13513                    Tab));
13514   verifyFormat("class X {\n"
13515                "\tvoid f() {\n"
13516                "\t\tsomeFunction(parameter1,\n"
13517                "\t\t\t     parameter2);\n"
13518                "\t}\n"
13519                "};",
13520                Tab);
13521   verifyFormat("{\n"
13522                "\tQ(\n"
13523                "\t    {\n"
13524                "\t\t    int a;\n"
13525                "\t\t    someFunction(aaaaaaaa,\n"
13526                "\t\t\t\t bbbbbbb);\n"
13527                "\t    },\n"
13528                "\t    p);\n"
13529                "}",
13530                Tab);
13531   EXPECT_EQ("{\n"
13532             "\t/* aaaa\n"
13533             "\t   bbbb */\n"
13534             "}",
13535             format("{\n"
13536                    "/* aaaa\n"
13537                    "   bbbb */\n"
13538                    "}",
13539                    Tab));
13540   EXPECT_EQ("{\n"
13541             "\t/*\n"
13542             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13543             "\t  bbbbbbbbbbbbb\n"
13544             "\t*/\n"
13545             "}",
13546             format("{\n"
13547                    "/*\n"
13548                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13549                    "*/\n"
13550                    "}",
13551                    Tab));
13552   EXPECT_EQ("{\n"
13553             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13554             "\t// bbbbbbbbbbbbb\n"
13555             "}",
13556             format("{\n"
13557                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13558                    "}",
13559                    Tab));
13560   EXPECT_EQ("{\n"
13561             "\t/*\n"
13562             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13563             "\t  bbbbbbbbbbbbb\n"
13564             "\t*/\n"
13565             "}",
13566             format("{\n"
13567                    "\t/*\n"
13568                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13569                    "\t*/\n"
13570                    "}",
13571                    Tab));
13572   EXPECT_EQ("{\n"
13573             "\t/*\n"
13574             "\n"
13575             "\t*/\n"
13576             "}",
13577             format("{\n"
13578                    "\t/*\n"
13579                    "\n"
13580                    "\t*/\n"
13581                    "}",
13582                    Tab));
13583   EXPECT_EQ("{\n"
13584             "\t/*\n"
13585             " asdf\n"
13586             "\t*/\n"
13587             "}",
13588             format("{\n"
13589                    "\t/*\n"
13590                    " asdf\n"
13591                    "\t*/\n"
13592                    "}",
13593                    Tab));
13594   EXPECT_EQ("/* some\n"
13595             "   comment */",
13596             format(" \t \t /* some\n"
13597                    " \t \t    comment */",
13598                    Tab));
13599   EXPECT_EQ("int a; /* some\n"
13600             "   comment */",
13601             format(" \t \t int a; /* some\n"
13602                    " \t \t    comment */",
13603                    Tab));
13604   EXPECT_EQ("int a; /* some\n"
13605             "comment */",
13606             format(" \t \t int\ta; /* some\n"
13607                    " \t \t    comment */",
13608                    Tab));
13609   EXPECT_EQ("f(\"\t\t\"); /* some\n"
13610             "    comment */",
13611             format(" \t \t f(\"\t\t\"); /* some\n"
13612                    " \t \t    comment */",
13613                    Tab));
13614   EXPECT_EQ("{\n"
13615             "\t/*\n"
13616             "\t * Comment\n"
13617             "\t */\n"
13618             "\tint i;\n"
13619             "}",
13620             format("{\n"
13621                    "\t/*\n"
13622                    "\t * Comment\n"
13623                    "\t */\n"
13624                    "\t int i;\n"
13625                    "}",
13626                    Tab));
13627   Tab.TabWidth = 2;
13628   Tab.IndentWidth = 2;
13629   EXPECT_EQ("{\n"
13630             "\t/* aaaa\n"
13631             "\t\t bbbb */\n"
13632             "}",
13633             format("{\n"
13634                    "/* aaaa\n"
13635                    "\t bbbb */\n"
13636                    "}",
13637                    Tab));
13638   EXPECT_EQ("{\n"
13639             "\t/*\n"
13640             "\t\taaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13641             "\t\tbbbbbbbbbbbbb\n"
13642             "\t*/\n"
13643             "}",
13644             format("{\n"
13645                    "/*\n"
13646                    "\taaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13647                    "*/\n"
13648                    "}",
13649                    Tab));
13650   Tab.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
13651   Tab.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
13652   Tab.TabWidth = 4;
13653   Tab.IndentWidth = 4;
13654   verifyFormat("class Assign {\n"
13655                "\tvoid f() {\n"
13656                "\t\tint         x      = 123;\n"
13657                "\t\tint         random = 4;\n"
13658                "\t\tstd::string alphabet =\n"
13659                "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
13660                "\t}\n"
13661                "};",
13662                Tab);
13663 
13664   Tab.UseTab = FormatStyle::UT_AlignWithSpaces;
13665   Tab.TabWidth = 8;
13666   Tab.IndentWidth = 8;
13667   EXPECT_EQ("if (aaaaaaaa && // q\n"
13668             "    bb)         // w\n"
13669             "\t;",
13670             format("if (aaaaaaaa &&// q\n"
13671                    "bb)// w\n"
13672                    ";",
13673                    Tab));
13674   EXPECT_EQ("if (aaa && bbb) // w\n"
13675             "\t;",
13676             format("if(aaa&&bbb)// w\n"
13677                    ";",
13678                    Tab));
13679   verifyFormat("class X {\n"
13680                "\tvoid f() {\n"
13681                "\t\tsomeFunction(parameter1,\n"
13682                "\t\t             parameter2);\n"
13683                "\t}\n"
13684                "};",
13685                Tab);
13686   verifyFormat("#define A                        \\\n"
13687                "\tvoid f() {               \\\n"
13688                "\t\tsomeFunction(    \\\n"
13689                "\t\t    parameter1,  \\\n"
13690                "\t\t    parameter2); \\\n"
13691                "\t}",
13692                Tab);
13693   Tab.TabWidth = 4;
13694   Tab.IndentWidth = 8;
13695   verifyFormat("class TabWidth4Indent8 {\n"
13696                "\t\tvoid f() {\n"
13697                "\t\t\t\tsomeFunction(parameter1,\n"
13698                "\t\t\t\t             parameter2);\n"
13699                "\t\t}\n"
13700                "};",
13701                Tab);
13702   Tab.TabWidth = 4;
13703   Tab.IndentWidth = 4;
13704   verifyFormat("class TabWidth4Indent4 {\n"
13705                "\tvoid f() {\n"
13706                "\t\tsomeFunction(parameter1,\n"
13707                "\t\t             parameter2);\n"
13708                "\t}\n"
13709                "};",
13710                Tab);
13711   Tab.TabWidth = 8;
13712   Tab.IndentWidth = 4;
13713   verifyFormat("class TabWidth8Indent4 {\n"
13714                "    void f() {\n"
13715                "\tsomeFunction(parameter1,\n"
13716                "\t             parameter2);\n"
13717                "    }\n"
13718                "};",
13719                Tab);
13720   Tab.TabWidth = 8;
13721   Tab.IndentWidth = 8;
13722   EXPECT_EQ("/*\n"
13723             "              a\t\tcomment\n"
13724             "              in multiple lines\n"
13725             "       */",
13726             format("   /*\t \t \n"
13727                    " \t \t a\t\tcomment\t \t\n"
13728                    " \t \t in multiple lines\t\n"
13729                    " \t  */",
13730                    Tab));
13731   verifyFormat("{\n"
13732                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13733                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13734                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13735                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13736                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13737                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13738                "};",
13739                Tab);
13740   verifyFormat("enum AA {\n"
13741                "\ta1, // Force multiple lines\n"
13742                "\ta2,\n"
13743                "\ta3\n"
13744                "};",
13745                Tab);
13746   EXPECT_EQ("if (aaaaaaaa && // q\n"
13747             "    bb)         // w\n"
13748             "\t;",
13749             format("if (aaaaaaaa &&// q\n"
13750                    "bb)// w\n"
13751                    ";",
13752                    Tab));
13753   verifyFormat("class X {\n"
13754                "\tvoid f() {\n"
13755                "\t\tsomeFunction(parameter1,\n"
13756                "\t\t             parameter2);\n"
13757                "\t}\n"
13758                "};",
13759                Tab);
13760   verifyFormat("{\n"
13761                "\tQ(\n"
13762                "\t    {\n"
13763                "\t\t    int a;\n"
13764                "\t\t    someFunction(aaaaaaaa,\n"
13765                "\t\t                 bbbbbbb);\n"
13766                "\t    },\n"
13767                "\t    p);\n"
13768                "}",
13769                Tab);
13770   EXPECT_EQ("{\n"
13771             "\t/* aaaa\n"
13772             "\t   bbbb */\n"
13773             "}",
13774             format("{\n"
13775                    "/* aaaa\n"
13776                    "   bbbb */\n"
13777                    "}",
13778                    Tab));
13779   EXPECT_EQ("{\n"
13780             "\t/*\n"
13781             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13782             "\t  bbbbbbbbbbbbb\n"
13783             "\t*/\n"
13784             "}",
13785             format("{\n"
13786                    "/*\n"
13787                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13788                    "*/\n"
13789                    "}",
13790                    Tab));
13791   EXPECT_EQ("{\n"
13792             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13793             "\t// bbbbbbbbbbbbb\n"
13794             "}",
13795             format("{\n"
13796                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13797                    "}",
13798                    Tab));
13799   EXPECT_EQ("{\n"
13800             "\t/*\n"
13801             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13802             "\t  bbbbbbbbbbbbb\n"
13803             "\t*/\n"
13804             "}",
13805             format("{\n"
13806                    "\t/*\n"
13807                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13808                    "\t*/\n"
13809                    "}",
13810                    Tab));
13811   EXPECT_EQ("{\n"
13812             "\t/*\n"
13813             "\n"
13814             "\t*/\n"
13815             "}",
13816             format("{\n"
13817                    "\t/*\n"
13818                    "\n"
13819                    "\t*/\n"
13820                    "}",
13821                    Tab));
13822   EXPECT_EQ("{\n"
13823             "\t/*\n"
13824             " asdf\n"
13825             "\t*/\n"
13826             "}",
13827             format("{\n"
13828                    "\t/*\n"
13829                    " asdf\n"
13830                    "\t*/\n"
13831                    "}",
13832                    Tab));
13833   EXPECT_EQ("/* some\n"
13834             "   comment */",
13835             format(" \t \t /* some\n"
13836                    " \t \t    comment */",
13837                    Tab));
13838   EXPECT_EQ("int a; /* some\n"
13839             "   comment */",
13840             format(" \t \t int a; /* some\n"
13841                    " \t \t    comment */",
13842                    Tab));
13843   EXPECT_EQ("int a; /* some\n"
13844             "comment */",
13845             format(" \t \t int\ta; /* some\n"
13846                    " \t \t    comment */",
13847                    Tab));
13848   EXPECT_EQ("f(\"\t\t\"); /* some\n"
13849             "    comment */",
13850             format(" \t \t f(\"\t\t\"); /* some\n"
13851                    " \t \t    comment */",
13852                    Tab));
13853   EXPECT_EQ("{\n"
13854             "\t/*\n"
13855             "\t * Comment\n"
13856             "\t */\n"
13857             "\tint i;\n"
13858             "}",
13859             format("{\n"
13860                    "\t/*\n"
13861                    "\t * Comment\n"
13862                    "\t */\n"
13863                    "\t int i;\n"
13864                    "}",
13865                    Tab));
13866   Tab.TabWidth = 2;
13867   Tab.IndentWidth = 2;
13868   EXPECT_EQ("{\n"
13869             "\t/* aaaa\n"
13870             "\t   bbbb */\n"
13871             "}",
13872             format("{\n"
13873                    "/* aaaa\n"
13874                    "   bbbb */\n"
13875                    "}",
13876                    Tab));
13877   EXPECT_EQ("{\n"
13878             "\t/*\n"
13879             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13880             "\t  bbbbbbbbbbbbb\n"
13881             "\t*/\n"
13882             "}",
13883             format("{\n"
13884                    "/*\n"
13885                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13886                    "*/\n"
13887                    "}",
13888                    Tab));
13889   Tab.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
13890   Tab.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
13891   Tab.TabWidth = 4;
13892   Tab.IndentWidth = 4;
13893   verifyFormat("class Assign {\n"
13894                "\tvoid f() {\n"
13895                "\t\tint         x      = 123;\n"
13896                "\t\tint         random = 4;\n"
13897                "\t\tstd::string alphabet =\n"
13898                "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
13899                "\t}\n"
13900                "};",
13901                Tab);
13902   Tab.AlignOperands = FormatStyle::OAS_Align;
13903   verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb +\n"
13904                "                 cccccccccccccccccccc;",
13905                Tab);
13906   // no alignment
13907   verifyFormat("int aaaaaaaaaa =\n"
13908                "\tbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
13909                Tab);
13910   verifyFormat("return aaaaaaaaaaaaaaaa ? 111111111111111\n"
13911                "       : bbbbbbbbbbbbbb ? 222222222222222\n"
13912                "                        : 333333333333333;",
13913                Tab);
13914   Tab.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
13915   Tab.AlignOperands = FormatStyle::OAS_AlignAfterOperator;
13916   verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb\n"
13917                "               + cccccccccccccccccccc;",
13918                Tab);
13919 }
13920 
13921 TEST_F(FormatTest, ZeroTabWidth) {
13922   FormatStyle Tab = getLLVMStyleWithColumns(42);
13923   Tab.IndentWidth = 8;
13924   Tab.UseTab = FormatStyle::UT_Never;
13925   Tab.TabWidth = 0;
13926   EXPECT_EQ("void a(){\n"
13927             "    // line starts with '\t'\n"
13928             "};",
13929             format("void a(){\n"
13930                    "\t// line starts with '\t'\n"
13931                    "};",
13932                    Tab));
13933 
13934   EXPECT_EQ("void a(){\n"
13935             "    // line starts with '\t'\n"
13936             "};",
13937             format("void a(){\n"
13938                    "\t\t// line starts with '\t'\n"
13939                    "};",
13940                    Tab));
13941 
13942   Tab.UseTab = FormatStyle::UT_ForIndentation;
13943   EXPECT_EQ("void a(){\n"
13944             "    // line starts with '\t'\n"
13945             "};",
13946             format("void a(){\n"
13947                    "\t// line starts with '\t'\n"
13948                    "};",
13949                    Tab));
13950 
13951   EXPECT_EQ("void a(){\n"
13952             "    // line starts with '\t'\n"
13953             "};",
13954             format("void a(){\n"
13955                    "\t\t// line starts with '\t'\n"
13956                    "};",
13957                    Tab));
13958 
13959   Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
13960   EXPECT_EQ("void a(){\n"
13961             "    // line starts with '\t'\n"
13962             "};",
13963             format("void a(){\n"
13964                    "\t// line starts with '\t'\n"
13965                    "};",
13966                    Tab));
13967 
13968   EXPECT_EQ("void a(){\n"
13969             "    // line starts with '\t'\n"
13970             "};",
13971             format("void a(){\n"
13972                    "\t\t// line starts with '\t'\n"
13973                    "};",
13974                    Tab));
13975 
13976   Tab.UseTab = FormatStyle::UT_AlignWithSpaces;
13977   EXPECT_EQ("void a(){\n"
13978             "    // line starts with '\t'\n"
13979             "};",
13980             format("void a(){\n"
13981                    "\t// line starts with '\t'\n"
13982                    "};",
13983                    Tab));
13984 
13985   EXPECT_EQ("void a(){\n"
13986             "    // line starts with '\t'\n"
13987             "};",
13988             format("void a(){\n"
13989                    "\t\t// line starts with '\t'\n"
13990                    "};",
13991                    Tab));
13992 
13993   Tab.UseTab = FormatStyle::UT_Always;
13994   EXPECT_EQ("void a(){\n"
13995             "// line starts with '\t'\n"
13996             "};",
13997             format("void a(){\n"
13998                    "\t// line starts with '\t'\n"
13999                    "};",
14000                    Tab));
14001 
14002   EXPECT_EQ("void a(){\n"
14003             "// line starts with '\t'\n"
14004             "};",
14005             format("void a(){\n"
14006                    "\t\t// line starts with '\t'\n"
14007                    "};",
14008                    Tab));
14009 }
14010 
14011 TEST_F(FormatTest, CalculatesOriginalColumn) {
14012   EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
14013             "q\"; /* some\n"
14014             "       comment */",
14015             format("  \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
14016                    "q\"; /* some\n"
14017                    "       comment */",
14018                    getLLVMStyle()));
14019   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
14020             "/* some\n"
14021             "   comment */",
14022             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
14023                    " /* some\n"
14024                    "    comment */",
14025                    getLLVMStyle()));
14026   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
14027             "qqq\n"
14028             "/* some\n"
14029             "   comment */",
14030             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
14031                    "qqq\n"
14032                    " /* some\n"
14033                    "    comment */",
14034                    getLLVMStyle()));
14035   EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
14036             "wwww; /* some\n"
14037             "         comment */",
14038             format("  inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
14039                    "wwww; /* some\n"
14040                    "         comment */",
14041                    getLLVMStyle()));
14042 }
14043 
14044 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
14045   FormatStyle NoSpace = getLLVMStyle();
14046   NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
14047 
14048   verifyFormat("while(true)\n"
14049                "  continue;",
14050                NoSpace);
14051   verifyFormat("for(;;)\n"
14052                "  continue;",
14053                NoSpace);
14054   verifyFormat("if(true)\n"
14055                "  f();\n"
14056                "else if(true)\n"
14057                "  f();",
14058                NoSpace);
14059   verifyFormat("do {\n"
14060                "  do_something();\n"
14061                "} while(something());",
14062                NoSpace);
14063   verifyFormat("switch(x) {\n"
14064                "default:\n"
14065                "  break;\n"
14066                "}",
14067                NoSpace);
14068   verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
14069   verifyFormat("size_t x = sizeof(x);", NoSpace);
14070   verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
14071   verifyFormat("auto f(int x) -> typeof(x);", NoSpace);
14072   verifyFormat("auto f(int x) -> _Atomic(x);", NoSpace);
14073   verifyFormat("auto f(int x) -> __underlying_type(x);", NoSpace);
14074   verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
14075   verifyFormat("alignas(128) char a[128];", NoSpace);
14076   verifyFormat("size_t x = alignof(MyType);", NoSpace);
14077   verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
14078   verifyFormat("int f() throw(Deprecated);", NoSpace);
14079   verifyFormat("typedef void (*cb)(int);", NoSpace);
14080   verifyFormat("T A::operator()();", NoSpace);
14081   verifyFormat("X A::operator++(T);", NoSpace);
14082   verifyFormat("auto lambda = []() { return 0; };", NoSpace);
14083 
14084   FormatStyle Space = getLLVMStyle();
14085   Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
14086 
14087   verifyFormat("int f ();", Space);
14088   verifyFormat("void f (int a, T b) {\n"
14089                "  while (true)\n"
14090                "    continue;\n"
14091                "}",
14092                Space);
14093   verifyFormat("if (true)\n"
14094                "  f ();\n"
14095                "else if (true)\n"
14096                "  f ();",
14097                Space);
14098   verifyFormat("do {\n"
14099                "  do_something ();\n"
14100                "} while (something ());",
14101                Space);
14102   verifyFormat("switch (x) {\n"
14103                "default:\n"
14104                "  break;\n"
14105                "}",
14106                Space);
14107   verifyFormat("A::A () : a (1) {}", Space);
14108   verifyFormat("void f () __attribute__ ((asdf));", Space);
14109   verifyFormat("*(&a + 1);\n"
14110                "&((&a)[1]);\n"
14111                "a[(b + c) * d];\n"
14112                "(((a + 1) * 2) + 3) * 4;",
14113                Space);
14114   verifyFormat("#define A(x) x", Space);
14115   verifyFormat("#define A (x) x", Space);
14116   verifyFormat("#if defined(x)\n"
14117                "#endif",
14118                Space);
14119   verifyFormat("auto i = std::make_unique<int> (5);", Space);
14120   verifyFormat("size_t x = sizeof (x);", Space);
14121   verifyFormat("auto f (int x) -> decltype (x);", Space);
14122   verifyFormat("auto f (int x) -> typeof (x);", Space);
14123   verifyFormat("auto f (int x) -> _Atomic (x);", Space);
14124   verifyFormat("auto f (int x) -> __underlying_type (x);", Space);
14125   verifyFormat("int f (T x) noexcept (x.create ());", Space);
14126   verifyFormat("alignas (128) char a[128];", Space);
14127   verifyFormat("size_t x = alignof (MyType);", Space);
14128   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
14129   verifyFormat("int f () throw (Deprecated);", Space);
14130   verifyFormat("typedef void (*cb) (int);", Space);
14131   // FIXME these tests regressed behaviour.
14132   // verifyFormat("T A::operator() ();", Space);
14133   // verifyFormat("X A::operator++ (T);", Space);
14134   verifyFormat("auto lambda = [] () { return 0; };", Space);
14135   verifyFormat("int x = int (y);", Space);
14136 
14137   FormatStyle SomeSpace = getLLVMStyle();
14138   SomeSpace.SpaceBeforeParens = FormatStyle::SBPO_NonEmptyParentheses;
14139 
14140   verifyFormat("[]() -> float {}", SomeSpace);
14141   verifyFormat("[] (auto foo) {}", SomeSpace);
14142   verifyFormat("[foo]() -> int {}", SomeSpace);
14143   verifyFormat("int f();", SomeSpace);
14144   verifyFormat("void f (int a, T b) {\n"
14145                "  while (true)\n"
14146                "    continue;\n"
14147                "}",
14148                SomeSpace);
14149   verifyFormat("if (true)\n"
14150                "  f();\n"
14151                "else if (true)\n"
14152                "  f();",
14153                SomeSpace);
14154   verifyFormat("do {\n"
14155                "  do_something();\n"
14156                "} while (something());",
14157                SomeSpace);
14158   verifyFormat("switch (x) {\n"
14159                "default:\n"
14160                "  break;\n"
14161                "}",
14162                SomeSpace);
14163   verifyFormat("A::A() : a (1) {}", SomeSpace);
14164   verifyFormat("void f() __attribute__ ((asdf));", SomeSpace);
14165   verifyFormat("*(&a + 1);\n"
14166                "&((&a)[1]);\n"
14167                "a[(b + c) * d];\n"
14168                "(((a + 1) * 2) + 3) * 4;",
14169                SomeSpace);
14170   verifyFormat("#define A(x) x", SomeSpace);
14171   verifyFormat("#define A (x) x", SomeSpace);
14172   verifyFormat("#if defined(x)\n"
14173                "#endif",
14174                SomeSpace);
14175   verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace);
14176   verifyFormat("size_t x = sizeof (x);", SomeSpace);
14177   verifyFormat("auto f (int x) -> decltype (x);", SomeSpace);
14178   verifyFormat("auto f (int x) -> typeof (x);", SomeSpace);
14179   verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace);
14180   verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace);
14181   verifyFormat("int f (T x) noexcept (x.create());", SomeSpace);
14182   verifyFormat("alignas (128) char a[128];", SomeSpace);
14183   verifyFormat("size_t x = alignof (MyType);", SomeSpace);
14184   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");",
14185                SomeSpace);
14186   verifyFormat("int f() throw (Deprecated);", SomeSpace);
14187   verifyFormat("typedef void (*cb) (int);", SomeSpace);
14188   verifyFormat("T A::operator()();", SomeSpace);
14189   // FIXME these tests regressed behaviour.
14190   // verifyFormat("X A::operator++ (T);", SomeSpace);
14191   verifyFormat("int x = int (y);", SomeSpace);
14192   verifyFormat("auto lambda = []() { return 0; };", SomeSpace);
14193 
14194   FormatStyle SpaceControlStatements = getLLVMStyle();
14195   SpaceControlStatements.SpaceBeforeParens = FormatStyle::SBPO_Custom;
14196   SpaceControlStatements.SpaceBeforeParensOptions.AfterControlStatements = true;
14197 
14198   verifyFormat("while (true)\n"
14199                "  continue;",
14200                SpaceControlStatements);
14201   verifyFormat("if (true)\n"
14202                "  f();\n"
14203                "else if (true)\n"
14204                "  f();",
14205                SpaceControlStatements);
14206   verifyFormat("for (;;) {\n"
14207                "  do_something();\n"
14208                "}",
14209                SpaceControlStatements);
14210   verifyFormat("do {\n"
14211                "  do_something();\n"
14212                "} while (something());",
14213                SpaceControlStatements);
14214   verifyFormat("switch (x) {\n"
14215                "default:\n"
14216                "  break;\n"
14217                "}",
14218                SpaceControlStatements);
14219 
14220   FormatStyle SpaceFuncDecl = getLLVMStyle();
14221   SpaceFuncDecl.SpaceBeforeParens = FormatStyle::SBPO_Custom;
14222   SpaceFuncDecl.SpaceBeforeParensOptions.AfterFunctionDeclarationName = true;
14223 
14224   verifyFormat("int f ();", SpaceFuncDecl);
14225   verifyFormat("void f(int a, T b) {}", SpaceFuncDecl);
14226   verifyFormat("A::A() : a(1) {}", SpaceFuncDecl);
14227   verifyFormat("void f () __attribute__((asdf));", SpaceFuncDecl);
14228   verifyFormat("#define A(x) x", SpaceFuncDecl);
14229   verifyFormat("#define A (x) x", SpaceFuncDecl);
14230   verifyFormat("#if defined(x)\n"
14231                "#endif",
14232                SpaceFuncDecl);
14233   verifyFormat("auto i = std::make_unique<int>(5);", SpaceFuncDecl);
14234   verifyFormat("size_t x = sizeof(x);", SpaceFuncDecl);
14235   verifyFormat("auto f (int x) -> decltype(x);", SpaceFuncDecl);
14236   verifyFormat("auto f (int x) -> typeof(x);", SpaceFuncDecl);
14237   verifyFormat("auto f (int x) -> _Atomic(x);", SpaceFuncDecl);
14238   verifyFormat("auto f (int x) -> __underlying_type(x);", SpaceFuncDecl);
14239   verifyFormat("int f (T x) noexcept(x.create());", SpaceFuncDecl);
14240   verifyFormat("alignas(128) char a[128];", SpaceFuncDecl);
14241   verifyFormat("size_t x = alignof(MyType);", SpaceFuncDecl);
14242   verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");",
14243                SpaceFuncDecl);
14244   verifyFormat("int f () throw(Deprecated);", SpaceFuncDecl);
14245   verifyFormat("typedef void (*cb)(int);", SpaceFuncDecl);
14246   // FIXME these tests regressed behaviour.
14247   // verifyFormat("T A::operator() ();", SpaceFuncDecl);
14248   // verifyFormat("X A::operator++ (T);", SpaceFuncDecl);
14249   verifyFormat("T A::operator()() {}", SpaceFuncDecl);
14250   verifyFormat("auto lambda = []() { return 0; };", SpaceFuncDecl);
14251   verifyFormat("int x = int(y);", SpaceFuncDecl);
14252   verifyFormat("M(std::size_t R, std::size_t C) : C(C), data(R) {}",
14253                SpaceFuncDecl);
14254 
14255   FormatStyle SpaceFuncDef = getLLVMStyle();
14256   SpaceFuncDef.SpaceBeforeParens = FormatStyle::SBPO_Custom;
14257   SpaceFuncDef.SpaceBeforeParensOptions.AfterFunctionDefinitionName = true;
14258 
14259   verifyFormat("int f();", SpaceFuncDef);
14260   verifyFormat("void f (int a, T b) {}", SpaceFuncDef);
14261   verifyFormat("A::A() : a(1) {}", SpaceFuncDef);
14262   verifyFormat("void f() __attribute__((asdf));", SpaceFuncDef);
14263   verifyFormat("#define A(x) x", SpaceFuncDef);
14264   verifyFormat("#define A (x) x", SpaceFuncDef);
14265   verifyFormat("#if defined(x)\n"
14266                "#endif",
14267                SpaceFuncDef);
14268   verifyFormat("auto i = std::make_unique<int>(5);", SpaceFuncDef);
14269   verifyFormat("size_t x = sizeof(x);", SpaceFuncDef);
14270   verifyFormat("auto f(int x) -> decltype(x);", SpaceFuncDef);
14271   verifyFormat("auto f(int x) -> typeof(x);", SpaceFuncDef);
14272   verifyFormat("auto f(int x) -> _Atomic(x);", SpaceFuncDef);
14273   verifyFormat("auto f(int x) -> __underlying_type(x);", SpaceFuncDef);
14274   verifyFormat("int f(T x) noexcept(x.create());", SpaceFuncDef);
14275   verifyFormat("alignas(128) char a[128];", SpaceFuncDef);
14276   verifyFormat("size_t x = alignof(MyType);", SpaceFuncDef);
14277   verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");",
14278                SpaceFuncDef);
14279   verifyFormat("int f() throw(Deprecated);", SpaceFuncDef);
14280   verifyFormat("typedef void (*cb)(int);", SpaceFuncDef);
14281   verifyFormat("T A::operator()();", SpaceFuncDef);
14282   verifyFormat("X A::operator++(T);", SpaceFuncDef);
14283   // verifyFormat("T A::operator() () {}", SpaceFuncDef);
14284   verifyFormat("auto lambda = [] () { return 0; };", SpaceFuncDef);
14285   verifyFormat("int x = int(y);", SpaceFuncDef);
14286   verifyFormat("M(std::size_t R, std::size_t C) : C(C), data(R) {}",
14287                SpaceFuncDef);
14288 
14289   FormatStyle SpaceIfMacros = getLLVMStyle();
14290   SpaceIfMacros.IfMacros.clear();
14291   SpaceIfMacros.IfMacros.push_back("MYIF");
14292   SpaceIfMacros.SpaceBeforeParens = FormatStyle::SBPO_Custom;
14293   SpaceIfMacros.SpaceBeforeParensOptions.AfterIfMacros = true;
14294   verifyFormat("MYIF (a)\n  return;", SpaceIfMacros);
14295   verifyFormat("MYIF (a)\n  return;\nelse MYIF (b)\n  return;", SpaceIfMacros);
14296   verifyFormat("MYIF (a)\n  return;\nelse\n  return;", SpaceIfMacros);
14297 
14298   FormatStyle SpaceForeachMacros = getLLVMStyle();
14299   SpaceForeachMacros.SpaceBeforeParens = FormatStyle::SBPO_Custom;
14300   SpaceForeachMacros.SpaceBeforeParensOptions.AfterForeachMacros = true;
14301   verifyFormat("foreach (Item *item, itemlist) {}", SpaceForeachMacros);
14302   verifyFormat("Q_FOREACH (Item *item, itemlist) {}", SpaceForeachMacros);
14303   verifyFormat("BOOST_FOREACH (Item *item, itemlist) {}", SpaceForeachMacros);
14304   verifyFormat("UNKNOWN_FOREACH(Item *item, itemlist) {}", SpaceForeachMacros);
14305 
14306   FormatStyle SomeSpace2 = getLLVMStyle();
14307   SomeSpace2.SpaceBeforeParens = FormatStyle::SBPO_Custom;
14308   SomeSpace2.SpaceBeforeParensOptions.BeforeNonEmptyParentheses = true;
14309   verifyFormat("[]() -> float {}", SomeSpace2);
14310   verifyFormat("[] (auto foo) {}", SomeSpace2);
14311   verifyFormat("[foo]() -> int {}", SomeSpace2);
14312   verifyFormat("int f();", SomeSpace2);
14313   verifyFormat("void f (int a, T b) {\n"
14314                "  while (true)\n"
14315                "    continue;\n"
14316                "}",
14317                SomeSpace2);
14318   verifyFormat("if (true)\n"
14319                "  f();\n"
14320                "else if (true)\n"
14321                "  f();",
14322                SomeSpace2);
14323   verifyFormat("do {\n"
14324                "  do_something();\n"
14325                "} while (something());",
14326                SomeSpace2);
14327   verifyFormat("switch (x) {\n"
14328                "default:\n"
14329                "  break;\n"
14330                "}",
14331                SomeSpace2);
14332   verifyFormat("A::A() : a (1) {}", SomeSpace2);
14333   verifyFormat("void f() __attribute__ ((asdf));", SomeSpace2);
14334   verifyFormat("*(&a + 1);\n"
14335                "&((&a)[1]);\n"
14336                "a[(b + c) * d];\n"
14337                "(((a + 1) * 2) + 3) * 4;",
14338                SomeSpace2);
14339   verifyFormat("#define A(x) x", SomeSpace2);
14340   verifyFormat("#define A (x) x", SomeSpace2);
14341   verifyFormat("#if defined(x)\n"
14342                "#endif",
14343                SomeSpace2);
14344   verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace2);
14345   verifyFormat("size_t x = sizeof (x);", SomeSpace2);
14346   verifyFormat("auto f (int x) -> decltype (x);", SomeSpace2);
14347   verifyFormat("auto f (int x) -> typeof (x);", SomeSpace2);
14348   verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace2);
14349   verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace2);
14350   verifyFormat("int f (T x) noexcept (x.create());", SomeSpace2);
14351   verifyFormat("alignas (128) char a[128];", SomeSpace2);
14352   verifyFormat("size_t x = alignof (MyType);", SomeSpace2);
14353   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");",
14354                SomeSpace2);
14355   verifyFormat("int f() throw (Deprecated);", SomeSpace2);
14356   verifyFormat("typedef void (*cb) (int);", SomeSpace2);
14357   verifyFormat("T A::operator()();", SomeSpace2);
14358   // verifyFormat("X A::operator++ (T);", SomeSpace2);
14359   verifyFormat("int x = int (y);", SomeSpace2);
14360   verifyFormat("auto lambda = []() { return 0; };", SomeSpace2);
14361 }
14362 
14363 TEST_F(FormatTest, SpaceAfterLogicalNot) {
14364   FormatStyle Spaces = getLLVMStyle();
14365   Spaces.SpaceAfterLogicalNot = true;
14366 
14367   verifyFormat("bool x = ! y", Spaces);
14368   verifyFormat("if (! isFailure())", Spaces);
14369   verifyFormat("if (! (a && b))", Spaces);
14370   verifyFormat("\"Error!\"", Spaces);
14371   verifyFormat("! ! x", Spaces);
14372 }
14373 
14374 TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
14375   FormatStyle Spaces = getLLVMStyle();
14376 
14377   Spaces.SpacesInParentheses = true;
14378   verifyFormat("do_something( ::globalVar );", Spaces);
14379   verifyFormat("call( x, y, z );", Spaces);
14380   verifyFormat("call();", Spaces);
14381   verifyFormat("std::function<void( int, int )> callback;", Spaces);
14382   verifyFormat("void inFunction() { std::function<void( int, int )> fct; }",
14383                Spaces);
14384   verifyFormat("while ( (bool)1 )\n"
14385                "  continue;",
14386                Spaces);
14387   verifyFormat("for ( ;; )\n"
14388                "  continue;",
14389                Spaces);
14390   verifyFormat("if ( true )\n"
14391                "  f();\n"
14392                "else if ( true )\n"
14393                "  f();",
14394                Spaces);
14395   verifyFormat("do {\n"
14396                "  do_something( (int)i );\n"
14397                "} while ( something() );",
14398                Spaces);
14399   verifyFormat("switch ( x ) {\n"
14400                "default:\n"
14401                "  break;\n"
14402                "}",
14403                Spaces);
14404 
14405   Spaces.SpacesInParentheses = false;
14406   Spaces.SpacesInCStyleCastParentheses = true;
14407   verifyFormat("Type *A = ( Type * )P;", Spaces);
14408   verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
14409   verifyFormat("x = ( int32 )y;", Spaces);
14410   verifyFormat("int a = ( int )(2.0f);", Spaces);
14411   verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
14412   verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
14413   verifyFormat("#define x (( int )-1)", Spaces);
14414 
14415   // Run the first set of tests again with:
14416   Spaces.SpacesInParentheses = false;
14417   Spaces.SpaceInEmptyParentheses = true;
14418   Spaces.SpacesInCStyleCastParentheses = true;
14419   verifyFormat("call(x, y, z);", Spaces);
14420   verifyFormat("call( );", Spaces);
14421   verifyFormat("std::function<void(int, int)> callback;", Spaces);
14422   verifyFormat("while (( bool )1)\n"
14423                "  continue;",
14424                Spaces);
14425   verifyFormat("for (;;)\n"
14426                "  continue;",
14427                Spaces);
14428   verifyFormat("if (true)\n"
14429                "  f( );\n"
14430                "else if (true)\n"
14431                "  f( );",
14432                Spaces);
14433   verifyFormat("do {\n"
14434                "  do_something(( int )i);\n"
14435                "} while (something( ));",
14436                Spaces);
14437   verifyFormat("switch (x) {\n"
14438                "default:\n"
14439                "  break;\n"
14440                "}",
14441                Spaces);
14442 
14443   // Run the first set of tests again with:
14444   Spaces.SpaceAfterCStyleCast = true;
14445   verifyFormat("call(x, y, z);", Spaces);
14446   verifyFormat("call( );", Spaces);
14447   verifyFormat("std::function<void(int, int)> callback;", Spaces);
14448   verifyFormat("while (( bool ) 1)\n"
14449                "  continue;",
14450                Spaces);
14451   verifyFormat("for (;;)\n"
14452                "  continue;",
14453                Spaces);
14454   verifyFormat("if (true)\n"
14455                "  f( );\n"
14456                "else if (true)\n"
14457                "  f( );",
14458                Spaces);
14459   verifyFormat("do {\n"
14460                "  do_something(( int ) i);\n"
14461                "} while (something( ));",
14462                Spaces);
14463   verifyFormat("switch (x) {\n"
14464                "default:\n"
14465                "  break;\n"
14466                "}",
14467                Spaces);
14468 
14469   // Run subset of tests again with:
14470   Spaces.SpacesInCStyleCastParentheses = false;
14471   Spaces.SpaceAfterCStyleCast = true;
14472   verifyFormat("while ((bool) 1)\n"
14473                "  continue;",
14474                Spaces);
14475   verifyFormat("do {\n"
14476                "  do_something((int) i);\n"
14477                "} while (something( ));",
14478                Spaces);
14479 
14480   verifyFormat("size_t idx = (size_t) (ptr - ((char *) file));", Spaces);
14481   verifyFormat("size_t idx = (size_t) a;", Spaces);
14482   verifyFormat("size_t idx = (size_t) (a - 1);", Spaces);
14483   verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces);
14484   verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces);
14485   verifyFormat("size_t idx = (*foo)(a - 1);", Spaces);
14486   verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces);
14487   Spaces.ColumnLimit = 80;
14488   Spaces.IndentWidth = 4;
14489   Spaces.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
14490   verifyFormat("void foo( ) {\n"
14491                "    size_t foo = (*(function))(\n"
14492                "        Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, "
14493                "BarrrrrrrrrrrrLong,\n"
14494                "        FoooooooooLooooong);\n"
14495                "}",
14496                Spaces);
14497   Spaces.SpaceAfterCStyleCast = false;
14498   verifyFormat("size_t idx = (size_t)(ptr - ((char *)file));", Spaces);
14499   verifyFormat("size_t idx = (size_t)a;", Spaces);
14500   verifyFormat("size_t idx = (size_t)(a - 1);", Spaces);
14501   verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces);
14502   verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces);
14503   verifyFormat("size_t idx = (*foo)(a - 1);", Spaces);
14504   verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces);
14505 
14506   verifyFormat("void foo( ) {\n"
14507                "    size_t foo = (*(function))(\n"
14508                "        Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, "
14509                "BarrrrrrrrrrrrLong,\n"
14510                "        FoooooooooLooooong);\n"
14511                "}",
14512                Spaces);
14513 }
14514 
14515 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
14516   verifyFormat("int a[5];");
14517   verifyFormat("a[3] += 42;");
14518 
14519   FormatStyle Spaces = getLLVMStyle();
14520   Spaces.SpacesInSquareBrackets = true;
14521   // Not lambdas.
14522   verifyFormat("int a[ 5 ];", Spaces);
14523   verifyFormat("a[ 3 ] += 42;", Spaces);
14524   verifyFormat("constexpr char hello[]{\"hello\"};", Spaces);
14525   verifyFormat("double &operator[](int i) { return 0; }\n"
14526                "int i;",
14527                Spaces);
14528   verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces);
14529   verifyFormat("int i = a[ a ][ a ]->f();", Spaces);
14530   verifyFormat("int i = (*b)[ a ]->f();", Spaces);
14531   // Lambdas.
14532   verifyFormat("int c = []() -> int { return 2; }();\n", Spaces);
14533   verifyFormat("return [ i, args... ] {};", Spaces);
14534   verifyFormat("int foo = [ &bar ]() {};", Spaces);
14535   verifyFormat("int foo = [ = ]() {};", Spaces);
14536   verifyFormat("int foo = [ & ]() {};", Spaces);
14537   verifyFormat("int foo = [ =, &bar ]() {};", Spaces);
14538   verifyFormat("int foo = [ &bar, = ]() {};", Spaces);
14539 }
14540 
14541 TEST_F(FormatTest, ConfigurableSpaceBeforeBrackets) {
14542   FormatStyle NoSpaceStyle = getLLVMStyle();
14543   verifyFormat("int a[5];", NoSpaceStyle);
14544   verifyFormat("a[3] += 42;", NoSpaceStyle);
14545 
14546   verifyFormat("int a[1];", NoSpaceStyle);
14547   verifyFormat("int 1 [a];", NoSpaceStyle);
14548   verifyFormat("int a[1][2];", NoSpaceStyle);
14549   verifyFormat("a[7] = 5;", NoSpaceStyle);
14550   verifyFormat("int a = (f())[23];", NoSpaceStyle);
14551   verifyFormat("f([] {})", NoSpaceStyle);
14552 
14553   FormatStyle Space = getLLVMStyle();
14554   Space.SpaceBeforeSquareBrackets = true;
14555   verifyFormat("int c = []() -> int { return 2; }();\n", Space);
14556   verifyFormat("return [i, args...] {};", Space);
14557 
14558   verifyFormat("int a [5];", Space);
14559   verifyFormat("a [3] += 42;", Space);
14560   verifyFormat("constexpr char hello []{\"hello\"};", Space);
14561   verifyFormat("double &operator[](int i) { return 0; }\n"
14562                "int i;",
14563                Space);
14564   verifyFormat("std::unique_ptr<int []> foo() {}", Space);
14565   verifyFormat("int i = a [a][a]->f();", Space);
14566   verifyFormat("int i = (*b) [a]->f();", Space);
14567 
14568   verifyFormat("int a [1];", Space);
14569   verifyFormat("int 1 [a];", Space);
14570   verifyFormat("int a [1][2];", Space);
14571   verifyFormat("a [7] = 5;", Space);
14572   verifyFormat("int a = (f()) [23];", Space);
14573   verifyFormat("f([] {})", Space);
14574 }
14575 
14576 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
14577   verifyFormat("int a = 5;");
14578   verifyFormat("a += 42;");
14579   verifyFormat("a or_eq 8;");
14580 
14581   FormatStyle Spaces = getLLVMStyle();
14582   Spaces.SpaceBeforeAssignmentOperators = false;
14583   verifyFormat("int a= 5;", Spaces);
14584   verifyFormat("a+= 42;", Spaces);
14585   verifyFormat("a or_eq 8;", Spaces);
14586 }
14587 
14588 TEST_F(FormatTest, ConfigurableSpaceBeforeColon) {
14589   verifyFormat("class Foo : public Bar {};");
14590   verifyFormat("Foo::Foo() : foo(1) {}");
14591   verifyFormat("for (auto a : b) {\n}");
14592   verifyFormat("int x = a ? b : c;");
14593   verifyFormat("{\n"
14594                "label0:\n"
14595                "  int x = 0;\n"
14596                "}");
14597   verifyFormat("switch (x) {\n"
14598                "case 1:\n"
14599                "default:\n"
14600                "}");
14601   verifyFormat("switch (allBraces) {\n"
14602                "case 1: {\n"
14603                "  break;\n"
14604                "}\n"
14605                "case 2: {\n"
14606                "  [[fallthrough]];\n"
14607                "}\n"
14608                "default: {\n"
14609                "  break;\n"
14610                "}\n"
14611                "}");
14612 
14613   FormatStyle CtorInitializerStyle = getLLVMStyleWithColumns(30);
14614   CtorInitializerStyle.SpaceBeforeCtorInitializerColon = false;
14615   verifyFormat("class Foo : public Bar {};", CtorInitializerStyle);
14616   verifyFormat("Foo::Foo(): foo(1) {}", CtorInitializerStyle);
14617   verifyFormat("for (auto a : b) {\n}", CtorInitializerStyle);
14618   verifyFormat("int x = a ? b : c;", CtorInitializerStyle);
14619   verifyFormat("{\n"
14620                "label1:\n"
14621                "  int x = 0;\n"
14622                "}",
14623                CtorInitializerStyle);
14624   verifyFormat("switch (x) {\n"
14625                "case 1:\n"
14626                "default:\n"
14627                "}",
14628                CtorInitializerStyle);
14629   verifyFormat("switch (allBraces) {\n"
14630                "case 1: {\n"
14631                "  break;\n"
14632                "}\n"
14633                "case 2: {\n"
14634                "  [[fallthrough]];\n"
14635                "}\n"
14636                "default: {\n"
14637                "  break;\n"
14638                "}\n"
14639                "}",
14640                CtorInitializerStyle);
14641   CtorInitializerStyle.BreakConstructorInitializers =
14642       FormatStyle::BCIS_AfterColon;
14643   verifyFormat("Fooooooooooo::Fooooooooooo():\n"
14644                "    aaaaaaaaaaaaaaaa(1),\n"
14645                "    bbbbbbbbbbbbbbbb(2) {}",
14646                CtorInitializerStyle);
14647   CtorInitializerStyle.BreakConstructorInitializers =
14648       FormatStyle::BCIS_BeforeComma;
14649   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
14650                "    : aaaaaaaaaaaaaaaa(1)\n"
14651                "    , bbbbbbbbbbbbbbbb(2) {}",
14652                CtorInitializerStyle);
14653   CtorInitializerStyle.BreakConstructorInitializers =
14654       FormatStyle::BCIS_BeforeColon;
14655   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
14656                "    : aaaaaaaaaaaaaaaa(1),\n"
14657                "      bbbbbbbbbbbbbbbb(2) {}",
14658                CtorInitializerStyle);
14659   CtorInitializerStyle.ConstructorInitializerIndentWidth = 0;
14660   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
14661                ": aaaaaaaaaaaaaaaa(1),\n"
14662                "  bbbbbbbbbbbbbbbb(2) {}",
14663                CtorInitializerStyle);
14664 
14665   FormatStyle InheritanceStyle = getLLVMStyleWithColumns(30);
14666   InheritanceStyle.SpaceBeforeInheritanceColon = false;
14667   verifyFormat("class Foo: public Bar {};", InheritanceStyle);
14668   verifyFormat("Foo::Foo() : foo(1) {}", InheritanceStyle);
14669   verifyFormat("for (auto a : b) {\n}", InheritanceStyle);
14670   verifyFormat("int x = a ? b : c;", InheritanceStyle);
14671   verifyFormat("{\n"
14672                "label2:\n"
14673                "  int x = 0;\n"
14674                "}",
14675                InheritanceStyle);
14676   verifyFormat("switch (x) {\n"
14677                "case 1:\n"
14678                "default:\n"
14679                "}",
14680                InheritanceStyle);
14681   verifyFormat("switch (allBraces) {\n"
14682                "case 1: {\n"
14683                "  break;\n"
14684                "}\n"
14685                "case 2: {\n"
14686                "  [[fallthrough]];\n"
14687                "}\n"
14688                "default: {\n"
14689                "  break;\n"
14690                "}\n"
14691                "}",
14692                InheritanceStyle);
14693   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterComma;
14694   verifyFormat("class Foooooooooooooooooooooo\n"
14695                "    : public aaaaaaaaaaaaaaaaaa,\n"
14696                "      public bbbbbbbbbbbbbbbbbb {\n"
14697                "}",
14698                InheritanceStyle);
14699   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterColon;
14700   verifyFormat("class Foooooooooooooooooooooo:\n"
14701                "    public aaaaaaaaaaaaaaaaaa,\n"
14702                "    public bbbbbbbbbbbbbbbbbb {\n"
14703                "}",
14704                InheritanceStyle);
14705   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeComma;
14706   verifyFormat("class Foooooooooooooooooooooo\n"
14707                "    : public aaaaaaaaaaaaaaaaaa\n"
14708                "    , public bbbbbbbbbbbbbbbbbb {\n"
14709                "}",
14710                InheritanceStyle);
14711   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
14712   verifyFormat("class Foooooooooooooooooooooo\n"
14713                "    : public aaaaaaaaaaaaaaaaaa,\n"
14714                "      public bbbbbbbbbbbbbbbbbb {\n"
14715                "}",
14716                InheritanceStyle);
14717   InheritanceStyle.ConstructorInitializerIndentWidth = 0;
14718   verifyFormat("class Foooooooooooooooooooooo\n"
14719                ": public aaaaaaaaaaaaaaaaaa,\n"
14720                "  public bbbbbbbbbbbbbbbbbb {}",
14721                InheritanceStyle);
14722 
14723   FormatStyle ForLoopStyle = getLLVMStyle();
14724   ForLoopStyle.SpaceBeforeRangeBasedForLoopColon = false;
14725   verifyFormat("class Foo : public Bar {};", ForLoopStyle);
14726   verifyFormat("Foo::Foo() : foo(1) {}", ForLoopStyle);
14727   verifyFormat("for (auto a: b) {\n}", ForLoopStyle);
14728   verifyFormat("int x = a ? b : c;", ForLoopStyle);
14729   verifyFormat("{\n"
14730                "label2:\n"
14731                "  int x = 0;\n"
14732                "}",
14733                ForLoopStyle);
14734   verifyFormat("switch (x) {\n"
14735                "case 1:\n"
14736                "default:\n"
14737                "}",
14738                ForLoopStyle);
14739   verifyFormat("switch (allBraces) {\n"
14740                "case 1: {\n"
14741                "  break;\n"
14742                "}\n"
14743                "case 2: {\n"
14744                "  [[fallthrough]];\n"
14745                "}\n"
14746                "default: {\n"
14747                "  break;\n"
14748                "}\n"
14749                "}",
14750                ForLoopStyle);
14751 
14752   FormatStyle CaseStyle = getLLVMStyle();
14753   CaseStyle.SpaceBeforeCaseColon = true;
14754   verifyFormat("class Foo : public Bar {};", CaseStyle);
14755   verifyFormat("Foo::Foo() : foo(1) {}", CaseStyle);
14756   verifyFormat("for (auto a : b) {\n}", CaseStyle);
14757   verifyFormat("int x = a ? b : c;", CaseStyle);
14758   verifyFormat("switch (x) {\n"
14759                "case 1 :\n"
14760                "default :\n"
14761                "}",
14762                CaseStyle);
14763   verifyFormat("switch (allBraces) {\n"
14764                "case 1 : {\n"
14765                "  break;\n"
14766                "}\n"
14767                "case 2 : {\n"
14768                "  [[fallthrough]];\n"
14769                "}\n"
14770                "default : {\n"
14771                "  break;\n"
14772                "}\n"
14773                "}",
14774                CaseStyle);
14775 
14776   FormatStyle NoSpaceStyle = getLLVMStyle();
14777   EXPECT_EQ(NoSpaceStyle.SpaceBeforeCaseColon, false);
14778   NoSpaceStyle.SpaceBeforeCtorInitializerColon = false;
14779   NoSpaceStyle.SpaceBeforeInheritanceColon = false;
14780   NoSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false;
14781   verifyFormat("class Foo: public Bar {};", NoSpaceStyle);
14782   verifyFormat("Foo::Foo(): foo(1) {}", NoSpaceStyle);
14783   verifyFormat("for (auto a: b) {\n}", NoSpaceStyle);
14784   verifyFormat("int x = a ? b : c;", NoSpaceStyle);
14785   verifyFormat("{\n"
14786                "label3:\n"
14787                "  int x = 0;\n"
14788                "}",
14789                NoSpaceStyle);
14790   verifyFormat("switch (x) {\n"
14791                "case 1:\n"
14792                "default:\n"
14793                "}",
14794                NoSpaceStyle);
14795   verifyFormat("switch (allBraces) {\n"
14796                "case 1: {\n"
14797                "  break;\n"
14798                "}\n"
14799                "case 2: {\n"
14800                "  [[fallthrough]];\n"
14801                "}\n"
14802                "default: {\n"
14803                "  break;\n"
14804                "}\n"
14805                "}",
14806                NoSpaceStyle);
14807 
14808   FormatStyle InvertedSpaceStyle = getLLVMStyle();
14809   InvertedSpaceStyle.SpaceBeforeCaseColon = true;
14810   InvertedSpaceStyle.SpaceBeforeCtorInitializerColon = false;
14811   InvertedSpaceStyle.SpaceBeforeInheritanceColon = false;
14812   InvertedSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false;
14813   verifyFormat("class Foo: public Bar {};", InvertedSpaceStyle);
14814   verifyFormat("Foo::Foo(): foo(1) {}", InvertedSpaceStyle);
14815   verifyFormat("for (auto a: b) {\n}", InvertedSpaceStyle);
14816   verifyFormat("int x = a ? b : c;", InvertedSpaceStyle);
14817   verifyFormat("{\n"
14818                "label3:\n"
14819                "  int x = 0;\n"
14820                "}",
14821                InvertedSpaceStyle);
14822   verifyFormat("switch (x) {\n"
14823                "case 1 :\n"
14824                "case 2 : {\n"
14825                "  break;\n"
14826                "}\n"
14827                "default :\n"
14828                "  break;\n"
14829                "}",
14830                InvertedSpaceStyle);
14831   verifyFormat("switch (allBraces) {\n"
14832                "case 1 : {\n"
14833                "  break;\n"
14834                "}\n"
14835                "case 2 : {\n"
14836                "  [[fallthrough]];\n"
14837                "}\n"
14838                "default : {\n"
14839                "  break;\n"
14840                "}\n"
14841                "}",
14842                InvertedSpaceStyle);
14843 }
14844 
14845 TEST_F(FormatTest, ConfigurableSpaceAroundPointerQualifiers) {
14846   FormatStyle Style = getLLVMStyle();
14847 
14848   Style.PointerAlignment = FormatStyle::PAS_Left;
14849   Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default;
14850   verifyFormat("void* const* x = NULL;", Style);
14851 
14852 #define verifyQualifierSpaces(Code, Pointers, Qualifiers)                      \
14853   do {                                                                         \
14854     Style.PointerAlignment = FormatStyle::Pointers;                            \
14855     Style.SpaceAroundPointerQualifiers = FormatStyle::Qualifiers;              \
14856     verifyFormat(Code, Style);                                                 \
14857   } while (false)
14858 
14859   verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Default);
14860   verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_Default);
14861   verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Default);
14862 
14863   verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Before);
14864   verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Before);
14865   verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Before);
14866 
14867   verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_After);
14868   verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_After);
14869   verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_After);
14870 
14871   verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_Both);
14872   verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Both);
14873   verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Both);
14874 
14875   verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Default);
14876   verifyQualifierSpaces("Foo::operator void const *();", PAS_Right,
14877                         SAPQ_Default);
14878   verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
14879                         SAPQ_Default);
14880 
14881   verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Before);
14882   verifyQualifierSpaces("Foo::operator void const *();", PAS_Right,
14883                         SAPQ_Before);
14884   verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
14885                         SAPQ_Before);
14886 
14887   verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_After);
14888   verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_After);
14889   verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
14890                         SAPQ_After);
14891 
14892   verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_Both);
14893   verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_Both);
14894   verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, SAPQ_Both);
14895 
14896 #undef verifyQualifierSpaces
14897 
14898   FormatStyle Spaces = getLLVMStyle();
14899   Spaces.AttributeMacros.push_back("qualified");
14900   Spaces.PointerAlignment = FormatStyle::PAS_Right;
14901   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default;
14902   verifyFormat("SomeType *volatile *a = NULL;", Spaces);
14903   verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces);
14904   verifyFormat("std::vector<SomeType *const *> x;", Spaces);
14905   verifyFormat("std::vector<SomeType *qualified *> x;", Spaces);
14906   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
14907   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before;
14908   verifyFormat("SomeType * volatile *a = NULL;", Spaces);
14909   verifyFormat("SomeType * __attribute__((attr)) *a = NULL;", Spaces);
14910   verifyFormat("std::vector<SomeType * const *> x;", Spaces);
14911   verifyFormat("std::vector<SomeType * qualified *> x;", Spaces);
14912   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
14913 
14914   // Check that SAPQ_Before doesn't result in extra spaces for PAS_Left.
14915   Spaces.PointerAlignment = FormatStyle::PAS_Left;
14916   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before;
14917   verifyFormat("SomeType* volatile* a = NULL;", Spaces);
14918   verifyFormat("SomeType* __attribute__((attr))* a = NULL;", Spaces);
14919   verifyFormat("std::vector<SomeType* const*> x;", Spaces);
14920   verifyFormat("std::vector<SomeType* qualified*> x;", Spaces);
14921   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
14922   // However, setting it to SAPQ_After should add spaces after __attribute, etc.
14923   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After;
14924   verifyFormat("SomeType* volatile * a = NULL;", Spaces);
14925   verifyFormat("SomeType* __attribute__((attr)) * a = NULL;", Spaces);
14926   verifyFormat("std::vector<SomeType* const *> x;", Spaces);
14927   verifyFormat("std::vector<SomeType* qualified *> x;", Spaces);
14928   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
14929 
14930   // PAS_Middle should not have any noticeable changes even for SAPQ_Both
14931   Spaces.PointerAlignment = FormatStyle::PAS_Middle;
14932   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After;
14933   verifyFormat("SomeType * volatile * a = NULL;", Spaces);
14934   verifyFormat("SomeType * __attribute__((attr)) * a = NULL;", Spaces);
14935   verifyFormat("std::vector<SomeType * const *> x;", Spaces);
14936   verifyFormat("std::vector<SomeType * qualified *> x;", Spaces);
14937   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
14938 }
14939 
14940 TEST_F(FormatTest, AlignConsecutiveMacros) {
14941   FormatStyle Style = getLLVMStyle();
14942   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
14943   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
14944   Style.AlignConsecutiveMacros = FormatStyle::ACS_None;
14945 
14946   verifyFormat("#define a 3\n"
14947                "#define bbbb 4\n"
14948                "#define ccc (5)",
14949                Style);
14950 
14951   verifyFormat("#define f(x) (x * x)\n"
14952                "#define fff(x, y, z) (x * y + z)\n"
14953                "#define ffff(x, y) (x - y)",
14954                Style);
14955 
14956   verifyFormat("#define foo(x, y) (x + y)\n"
14957                "#define bar (5, 6)(2 + 2)",
14958                Style);
14959 
14960   verifyFormat("#define a 3\n"
14961                "#define bbbb 4\n"
14962                "#define ccc (5)\n"
14963                "#define f(x) (x * x)\n"
14964                "#define fff(x, y, z) (x * y + z)\n"
14965                "#define ffff(x, y) (x - y)",
14966                Style);
14967 
14968   Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
14969   verifyFormat("#define a    3\n"
14970                "#define bbbb 4\n"
14971                "#define ccc  (5)",
14972                Style);
14973 
14974   verifyFormat("#define f(x)         (x * x)\n"
14975                "#define fff(x, y, z) (x * y + z)\n"
14976                "#define ffff(x, y)   (x - y)",
14977                Style);
14978 
14979   verifyFormat("#define foo(x, y) (x + y)\n"
14980                "#define bar       (5, 6)(2 + 2)",
14981                Style);
14982 
14983   verifyFormat("#define a            3\n"
14984                "#define bbbb         4\n"
14985                "#define ccc          (5)\n"
14986                "#define f(x)         (x * x)\n"
14987                "#define fff(x, y, z) (x * y + z)\n"
14988                "#define ffff(x, y)   (x - y)",
14989                Style);
14990 
14991   verifyFormat("#define a         5\n"
14992                "#define foo(x, y) (x + y)\n"
14993                "#define CCC       (6)\n"
14994                "auto lambda = []() {\n"
14995                "  auto  ii = 0;\n"
14996                "  float j  = 0;\n"
14997                "  return 0;\n"
14998                "};\n"
14999                "int   i  = 0;\n"
15000                "float i2 = 0;\n"
15001                "auto  v  = type{\n"
15002                "    i = 1,   //\n"
15003                "    (i = 2), //\n"
15004                "    i = 3    //\n"
15005                "};",
15006                Style);
15007 
15008   Style.AlignConsecutiveMacros = FormatStyle::ACS_None;
15009   Style.ColumnLimit = 20;
15010 
15011   verifyFormat("#define a          \\\n"
15012                "  \"aabbbbbbbbbbbb\"\n"
15013                "#define D          \\\n"
15014                "  \"aabbbbbbbbbbbb\" \\\n"
15015                "  \"ccddeeeeeeeee\"\n"
15016                "#define B          \\\n"
15017                "  \"QQQQQQQQQQQQQ\"  \\\n"
15018                "  \"FFFFFFFFFFFFF\"  \\\n"
15019                "  \"LLLLLLLL\"\n",
15020                Style);
15021 
15022   Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
15023   verifyFormat("#define a          \\\n"
15024                "  \"aabbbbbbbbbbbb\"\n"
15025                "#define D          \\\n"
15026                "  \"aabbbbbbbbbbbb\" \\\n"
15027                "  \"ccddeeeeeeeee\"\n"
15028                "#define B          \\\n"
15029                "  \"QQQQQQQQQQQQQ\"  \\\n"
15030                "  \"FFFFFFFFFFFFF\"  \\\n"
15031                "  \"LLLLLLLL\"\n",
15032                Style);
15033 
15034   // Test across comments
15035   Style.MaxEmptyLinesToKeep = 10;
15036   Style.ReflowComments = false;
15037   Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossComments;
15038   EXPECT_EQ("#define a    3\n"
15039             "// line comment\n"
15040             "#define bbbb 4\n"
15041             "#define ccc  (5)",
15042             format("#define a 3\n"
15043                    "// line comment\n"
15044                    "#define bbbb 4\n"
15045                    "#define ccc (5)",
15046                    Style));
15047 
15048   EXPECT_EQ("#define a    3\n"
15049             "/* block comment */\n"
15050             "#define bbbb 4\n"
15051             "#define ccc  (5)",
15052             format("#define a  3\n"
15053                    "/* block comment */\n"
15054                    "#define bbbb 4\n"
15055                    "#define ccc (5)",
15056                    Style));
15057 
15058   EXPECT_EQ("#define a    3\n"
15059             "/* multi-line *\n"
15060             " * block comment */\n"
15061             "#define bbbb 4\n"
15062             "#define ccc  (5)",
15063             format("#define a 3\n"
15064                    "/* multi-line *\n"
15065                    " * block comment */\n"
15066                    "#define bbbb 4\n"
15067                    "#define ccc (5)",
15068                    Style));
15069 
15070   EXPECT_EQ("#define a    3\n"
15071             "// multi-line line comment\n"
15072             "//\n"
15073             "#define bbbb 4\n"
15074             "#define ccc  (5)",
15075             format("#define a  3\n"
15076                    "// multi-line line comment\n"
15077                    "//\n"
15078                    "#define bbbb 4\n"
15079                    "#define ccc (5)",
15080                    Style));
15081 
15082   EXPECT_EQ("#define a 3\n"
15083             "// empty lines still break.\n"
15084             "\n"
15085             "#define bbbb 4\n"
15086             "#define ccc  (5)",
15087             format("#define a     3\n"
15088                    "// empty lines still break.\n"
15089                    "\n"
15090                    "#define bbbb     4\n"
15091                    "#define ccc  (5)",
15092                    Style));
15093 
15094   // Test across empty lines
15095   Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossEmptyLines;
15096   EXPECT_EQ("#define a    3\n"
15097             "\n"
15098             "#define bbbb 4\n"
15099             "#define ccc  (5)",
15100             format("#define a 3\n"
15101                    "\n"
15102                    "#define bbbb 4\n"
15103                    "#define ccc (5)",
15104                    Style));
15105 
15106   EXPECT_EQ("#define a    3\n"
15107             "\n"
15108             "\n"
15109             "\n"
15110             "#define bbbb 4\n"
15111             "#define ccc  (5)",
15112             format("#define a        3\n"
15113                    "\n"
15114                    "\n"
15115                    "\n"
15116                    "#define bbbb 4\n"
15117                    "#define ccc (5)",
15118                    Style));
15119 
15120   EXPECT_EQ("#define a 3\n"
15121             "// comments should break alignment\n"
15122             "//\n"
15123             "#define bbbb 4\n"
15124             "#define ccc  (5)",
15125             format("#define a        3\n"
15126                    "// comments should break alignment\n"
15127                    "//\n"
15128                    "#define bbbb 4\n"
15129                    "#define ccc (5)",
15130                    Style));
15131 
15132   // Test across empty lines and comments
15133   Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossEmptyLinesAndComments;
15134   verifyFormat("#define a    3\n"
15135                "\n"
15136                "// line comment\n"
15137                "#define bbbb 4\n"
15138                "#define ccc  (5)",
15139                Style);
15140 
15141   EXPECT_EQ("#define a    3\n"
15142             "\n"
15143             "\n"
15144             "/* multi-line *\n"
15145             " * block comment */\n"
15146             "\n"
15147             "\n"
15148             "#define bbbb 4\n"
15149             "#define ccc  (5)",
15150             format("#define a 3\n"
15151                    "\n"
15152                    "\n"
15153                    "/* multi-line *\n"
15154                    " * block comment */\n"
15155                    "\n"
15156                    "\n"
15157                    "#define bbbb 4\n"
15158                    "#define ccc (5)",
15159                    Style));
15160 
15161   EXPECT_EQ("#define a    3\n"
15162             "\n"
15163             "\n"
15164             "/* multi-line *\n"
15165             " * block comment */\n"
15166             "\n"
15167             "\n"
15168             "#define bbbb 4\n"
15169             "#define ccc  (5)",
15170             format("#define a 3\n"
15171                    "\n"
15172                    "\n"
15173                    "/* multi-line *\n"
15174                    " * block comment */\n"
15175                    "\n"
15176                    "\n"
15177                    "#define bbbb 4\n"
15178                    "#define ccc       (5)",
15179                    Style));
15180 }
15181 
15182 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLines) {
15183   FormatStyle Alignment = getLLVMStyle();
15184   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
15185   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_AcrossEmptyLines;
15186 
15187   Alignment.MaxEmptyLinesToKeep = 10;
15188   /* Test alignment across empty lines */
15189   EXPECT_EQ("int a           = 5;\n"
15190             "\n"
15191             "int oneTwoThree = 123;",
15192             format("int a       = 5;\n"
15193                    "\n"
15194                    "int oneTwoThree= 123;",
15195                    Alignment));
15196   EXPECT_EQ("int a           = 5;\n"
15197             "int one         = 1;\n"
15198             "\n"
15199             "int oneTwoThree = 123;",
15200             format("int a = 5;\n"
15201                    "int one = 1;\n"
15202                    "\n"
15203                    "int oneTwoThree = 123;",
15204                    Alignment));
15205   EXPECT_EQ("int a           = 5;\n"
15206             "int one         = 1;\n"
15207             "\n"
15208             "int oneTwoThree = 123;\n"
15209             "int oneTwo      = 12;",
15210             format("int a = 5;\n"
15211                    "int one = 1;\n"
15212                    "\n"
15213                    "int oneTwoThree = 123;\n"
15214                    "int oneTwo = 12;",
15215                    Alignment));
15216 
15217   /* Test across comments */
15218   EXPECT_EQ("int a = 5;\n"
15219             "/* block comment */\n"
15220             "int oneTwoThree = 123;",
15221             format("int a = 5;\n"
15222                    "/* block comment */\n"
15223                    "int oneTwoThree=123;",
15224                    Alignment));
15225 
15226   EXPECT_EQ("int a = 5;\n"
15227             "// line comment\n"
15228             "int oneTwoThree = 123;",
15229             format("int a = 5;\n"
15230                    "// line comment\n"
15231                    "int oneTwoThree=123;",
15232                    Alignment));
15233 
15234   /* Test across comments and newlines */
15235   EXPECT_EQ("int a = 5;\n"
15236             "\n"
15237             "/* block comment */\n"
15238             "int oneTwoThree = 123;",
15239             format("int a = 5;\n"
15240                    "\n"
15241                    "/* block comment */\n"
15242                    "int oneTwoThree=123;",
15243                    Alignment));
15244 
15245   EXPECT_EQ("int a = 5;\n"
15246             "\n"
15247             "// line comment\n"
15248             "int oneTwoThree = 123;",
15249             format("int a = 5;\n"
15250                    "\n"
15251                    "// line comment\n"
15252                    "int oneTwoThree=123;",
15253                    Alignment));
15254 }
15255 
15256 TEST_F(FormatTest, AlignConsecutiveDeclarationsAcrossEmptyLinesAndComments) {
15257   FormatStyle Alignment = getLLVMStyle();
15258   Alignment.AlignConsecutiveDeclarations =
15259       FormatStyle::ACS_AcrossEmptyLinesAndComments;
15260   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
15261 
15262   Alignment.MaxEmptyLinesToKeep = 10;
15263   /* Test alignment across empty lines */
15264   EXPECT_EQ("int         a = 5;\n"
15265             "\n"
15266             "float const oneTwoThree = 123;",
15267             format("int a = 5;\n"
15268                    "\n"
15269                    "float const oneTwoThree = 123;",
15270                    Alignment));
15271   EXPECT_EQ("int         a = 5;\n"
15272             "float const one = 1;\n"
15273             "\n"
15274             "int         oneTwoThree = 123;",
15275             format("int a = 5;\n"
15276                    "float const one = 1;\n"
15277                    "\n"
15278                    "int oneTwoThree = 123;",
15279                    Alignment));
15280 
15281   /* Test across comments */
15282   EXPECT_EQ("float const a = 5;\n"
15283             "/* block comment */\n"
15284             "int         oneTwoThree = 123;",
15285             format("float const a = 5;\n"
15286                    "/* block comment */\n"
15287                    "int oneTwoThree=123;",
15288                    Alignment));
15289 
15290   EXPECT_EQ("float const a = 5;\n"
15291             "// line comment\n"
15292             "int         oneTwoThree = 123;",
15293             format("float const a = 5;\n"
15294                    "// line comment\n"
15295                    "int oneTwoThree=123;",
15296                    Alignment));
15297 
15298   /* Test across comments and newlines */
15299   EXPECT_EQ("float const a = 5;\n"
15300             "\n"
15301             "/* block comment */\n"
15302             "int         oneTwoThree = 123;",
15303             format("float const a = 5;\n"
15304                    "\n"
15305                    "/* block comment */\n"
15306                    "int         oneTwoThree=123;",
15307                    Alignment));
15308 
15309   EXPECT_EQ("float const a = 5;\n"
15310             "\n"
15311             "// line comment\n"
15312             "int         oneTwoThree = 123;",
15313             format("float const a = 5;\n"
15314                    "\n"
15315                    "// line comment\n"
15316                    "int oneTwoThree=123;",
15317                    Alignment));
15318 }
15319 
15320 TEST_F(FormatTest, AlignConsecutiveBitFieldsAcrossEmptyLinesAndComments) {
15321   FormatStyle Alignment = getLLVMStyle();
15322   Alignment.AlignConsecutiveBitFields =
15323       FormatStyle::ACS_AcrossEmptyLinesAndComments;
15324 
15325   Alignment.MaxEmptyLinesToKeep = 10;
15326   /* Test alignment across empty lines */
15327   EXPECT_EQ("int a            : 5;\n"
15328             "\n"
15329             "int longbitfield : 6;",
15330             format("int a : 5;\n"
15331                    "\n"
15332                    "int longbitfield : 6;",
15333                    Alignment));
15334   EXPECT_EQ("int a            : 5;\n"
15335             "int one          : 1;\n"
15336             "\n"
15337             "int longbitfield : 6;",
15338             format("int a : 5;\n"
15339                    "int one : 1;\n"
15340                    "\n"
15341                    "int longbitfield : 6;",
15342                    Alignment));
15343 
15344   /* Test across comments */
15345   EXPECT_EQ("int a            : 5;\n"
15346             "/* block comment */\n"
15347             "int longbitfield : 6;",
15348             format("int a : 5;\n"
15349                    "/* block comment */\n"
15350                    "int longbitfield : 6;",
15351                    Alignment));
15352   EXPECT_EQ("int a            : 5;\n"
15353             "int one          : 1;\n"
15354             "// line comment\n"
15355             "int longbitfield : 6;",
15356             format("int a : 5;\n"
15357                    "int one : 1;\n"
15358                    "// line comment\n"
15359                    "int longbitfield : 6;",
15360                    Alignment));
15361 
15362   /* Test across comments and newlines */
15363   EXPECT_EQ("int a            : 5;\n"
15364             "/* block comment */\n"
15365             "\n"
15366             "int longbitfield : 6;",
15367             format("int a : 5;\n"
15368                    "/* block comment */\n"
15369                    "\n"
15370                    "int longbitfield : 6;",
15371                    Alignment));
15372   EXPECT_EQ("int a            : 5;\n"
15373             "int one          : 1;\n"
15374             "\n"
15375             "// line comment\n"
15376             "\n"
15377             "int longbitfield : 6;",
15378             format("int a : 5;\n"
15379                    "int one : 1;\n"
15380                    "\n"
15381                    "// line comment \n"
15382                    "\n"
15383                    "int longbitfield : 6;",
15384                    Alignment));
15385 }
15386 
15387 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossComments) {
15388   FormatStyle Alignment = getLLVMStyle();
15389   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
15390   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_AcrossComments;
15391 
15392   Alignment.MaxEmptyLinesToKeep = 10;
15393   /* Test alignment across empty lines */
15394   EXPECT_EQ("int a = 5;\n"
15395             "\n"
15396             "int oneTwoThree = 123;",
15397             format("int a       = 5;\n"
15398                    "\n"
15399                    "int oneTwoThree= 123;",
15400                    Alignment));
15401   EXPECT_EQ("int a   = 5;\n"
15402             "int one = 1;\n"
15403             "\n"
15404             "int oneTwoThree = 123;",
15405             format("int a = 5;\n"
15406                    "int one = 1;\n"
15407                    "\n"
15408                    "int oneTwoThree = 123;",
15409                    Alignment));
15410 
15411   /* Test across comments */
15412   EXPECT_EQ("int a           = 5;\n"
15413             "/* block comment */\n"
15414             "int oneTwoThree = 123;",
15415             format("int a = 5;\n"
15416                    "/* block comment */\n"
15417                    "int oneTwoThree=123;",
15418                    Alignment));
15419 
15420   EXPECT_EQ("int a           = 5;\n"
15421             "// line comment\n"
15422             "int oneTwoThree = 123;",
15423             format("int a = 5;\n"
15424                    "// line comment\n"
15425                    "int oneTwoThree=123;",
15426                    Alignment));
15427 
15428   EXPECT_EQ("int a           = 5;\n"
15429             "/*\n"
15430             " * multi-line block comment\n"
15431             " */\n"
15432             "int oneTwoThree = 123;",
15433             format("int a = 5;\n"
15434                    "/*\n"
15435                    " * multi-line block comment\n"
15436                    " */\n"
15437                    "int oneTwoThree=123;",
15438                    Alignment));
15439 
15440   EXPECT_EQ("int a           = 5;\n"
15441             "//\n"
15442             "// multi-line line comment\n"
15443             "//\n"
15444             "int oneTwoThree = 123;",
15445             format("int a = 5;\n"
15446                    "//\n"
15447                    "// multi-line line comment\n"
15448                    "//\n"
15449                    "int oneTwoThree=123;",
15450                    Alignment));
15451 
15452   /* Test across comments and newlines */
15453   EXPECT_EQ("int a = 5;\n"
15454             "\n"
15455             "/* block comment */\n"
15456             "int oneTwoThree = 123;",
15457             format("int a = 5;\n"
15458                    "\n"
15459                    "/* block comment */\n"
15460                    "int oneTwoThree=123;",
15461                    Alignment));
15462 
15463   EXPECT_EQ("int a = 5;\n"
15464             "\n"
15465             "// line comment\n"
15466             "int oneTwoThree = 123;",
15467             format("int a = 5;\n"
15468                    "\n"
15469                    "// line comment\n"
15470                    "int oneTwoThree=123;",
15471                    Alignment));
15472 }
15473 
15474 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLinesAndComments) {
15475   FormatStyle Alignment = getLLVMStyle();
15476   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
15477   Alignment.AlignConsecutiveAssignments =
15478       FormatStyle::ACS_AcrossEmptyLinesAndComments;
15479   verifyFormat("int a           = 5;\n"
15480                "int oneTwoThree = 123;",
15481                Alignment);
15482   verifyFormat("int a           = method();\n"
15483                "int oneTwoThree = 133;",
15484                Alignment);
15485   verifyFormat("a &= 5;\n"
15486                "bcd *= 5;\n"
15487                "ghtyf += 5;\n"
15488                "dvfvdb -= 5;\n"
15489                "a /= 5;\n"
15490                "vdsvsv %= 5;\n"
15491                "sfdbddfbdfbb ^= 5;\n"
15492                "dvsdsv |= 5;\n"
15493                "int dsvvdvsdvvv = 123;",
15494                Alignment);
15495   verifyFormat("int i = 1, j = 10;\n"
15496                "something = 2000;",
15497                Alignment);
15498   verifyFormat("something = 2000;\n"
15499                "int i = 1, j = 10;\n",
15500                Alignment);
15501   verifyFormat("something = 2000;\n"
15502                "another   = 911;\n"
15503                "int i = 1, j = 10;\n"
15504                "oneMore = 1;\n"
15505                "i       = 2;",
15506                Alignment);
15507   verifyFormat("int a   = 5;\n"
15508                "int one = 1;\n"
15509                "method();\n"
15510                "int oneTwoThree = 123;\n"
15511                "int oneTwo      = 12;",
15512                Alignment);
15513   verifyFormat("int oneTwoThree = 123;\n"
15514                "int oneTwo      = 12;\n"
15515                "method();\n",
15516                Alignment);
15517   verifyFormat("int oneTwoThree = 123; // comment\n"
15518                "int oneTwo      = 12;  // comment",
15519                Alignment);
15520 
15521   // Bug 25167
15522   /* Uncomment when fixed
15523     verifyFormat("#if A\n"
15524                  "#else\n"
15525                  "int aaaaaaaa = 12;\n"
15526                  "#endif\n"
15527                  "#if B\n"
15528                  "#else\n"
15529                  "int a = 12;\n"
15530                  "#endif\n",
15531                  Alignment);
15532     verifyFormat("enum foo {\n"
15533                  "#if A\n"
15534                  "#else\n"
15535                  "  aaaaaaaa = 12;\n"
15536                  "#endif\n"
15537                  "#if B\n"
15538                  "#else\n"
15539                  "  a = 12;\n"
15540                  "#endif\n"
15541                  "};\n",
15542                  Alignment);
15543   */
15544 
15545   Alignment.MaxEmptyLinesToKeep = 10;
15546   /* Test alignment across empty lines */
15547   EXPECT_EQ("int a           = 5;\n"
15548             "\n"
15549             "int oneTwoThree = 123;",
15550             format("int a       = 5;\n"
15551                    "\n"
15552                    "int oneTwoThree= 123;",
15553                    Alignment));
15554   EXPECT_EQ("int a           = 5;\n"
15555             "int one         = 1;\n"
15556             "\n"
15557             "int oneTwoThree = 123;",
15558             format("int a = 5;\n"
15559                    "int one = 1;\n"
15560                    "\n"
15561                    "int oneTwoThree = 123;",
15562                    Alignment));
15563   EXPECT_EQ("int a           = 5;\n"
15564             "int one         = 1;\n"
15565             "\n"
15566             "int oneTwoThree = 123;\n"
15567             "int oneTwo      = 12;",
15568             format("int a = 5;\n"
15569                    "int one = 1;\n"
15570                    "\n"
15571                    "int oneTwoThree = 123;\n"
15572                    "int oneTwo = 12;",
15573                    Alignment));
15574 
15575   /* Test across comments */
15576   EXPECT_EQ("int a           = 5;\n"
15577             "/* block comment */\n"
15578             "int oneTwoThree = 123;",
15579             format("int a = 5;\n"
15580                    "/* block comment */\n"
15581                    "int oneTwoThree=123;",
15582                    Alignment));
15583 
15584   EXPECT_EQ("int a           = 5;\n"
15585             "// line comment\n"
15586             "int oneTwoThree = 123;",
15587             format("int a = 5;\n"
15588                    "// line comment\n"
15589                    "int oneTwoThree=123;",
15590                    Alignment));
15591 
15592   /* Test across comments and newlines */
15593   EXPECT_EQ("int a           = 5;\n"
15594             "\n"
15595             "/* block comment */\n"
15596             "int oneTwoThree = 123;",
15597             format("int a = 5;\n"
15598                    "\n"
15599                    "/* block comment */\n"
15600                    "int oneTwoThree=123;",
15601                    Alignment));
15602 
15603   EXPECT_EQ("int a           = 5;\n"
15604             "\n"
15605             "// line comment\n"
15606             "int oneTwoThree = 123;",
15607             format("int a = 5;\n"
15608                    "\n"
15609                    "// line comment\n"
15610                    "int oneTwoThree=123;",
15611                    Alignment));
15612 
15613   EXPECT_EQ("int a           = 5;\n"
15614             "//\n"
15615             "// multi-line line comment\n"
15616             "//\n"
15617             "int oneTwoThree = 123;",
15618             format("int a = 5;\n"
15619                    "//\n"
15620                    "// multi-line line comment\n"
15621                    "//\n"
15622                    "int oneTwoThree=123;",
15623                    Alignment));
15624 
15625   EXPECT_EQ("int a           = 5;\n"
15626             "/*\n"
15627             " *  multi-line block comment\n"
15628             " */\n"
15629             "int oneTwoThree = 123;",
15630             format("int a = 5;\n"
15631                    "/*\n"
15632                    " *  multi-line block comment\n"
15633                    " */\n"
15634                    "int oneTwoThree=123;",
15635                    Alignment));
15636 
15637   EXPECT_EQ("int a           = 5;\n"
15638             "\n"
15639             "/* block comment */\n"
15640             "\n"
15641             "\n"
15642             "\n"
15643             "int oneTwoThree = 123;",
15644             format("int a = 5;\n"
15645                    "\n"
15646                    "/* block comment */\n"
15647                    "\n"
15648                    "\n"
15649                    "\n"
15650                    "int oneTwoThree=123;",
15651                    Alignment));
15652 
15653   EXPECT_EQ("int a           = 5;\n"
15654             "\n"
15655             "// line comment\n"
15656             "\n"
15657             "\n"
15658             "\n"
15659             "int oneTwoThree = 123;",
15660             format("int a = 5;\n"
15661                    "\n"
15662                    "// line comment\n"
15663                    "\n"
15664                    "\n"
15665                    "\n"
15666                    "int oneTwoThree=123;",
15667                    Alignment));
15668 
15669   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
15670   verifyFormat("#define A \\\n"
15671                "  int aaaa       = 12; \\\n"
15672                "  int b          = 23; \\\n"
15673                "  int ccc        = 234; \\\n"
15674                "  int dddddddddd = 2345;",
15675                Alignment);
15676   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
15677   verifyFormat("#define A               \\\n"
15678                "  int aaaa       = 12;  \\\n"
15679                "  int b          = 23;  \\\n"
15680                "  int ccc        = 234; \\\n"
15681                "  int dddddddddd = 2345;",
15682                Alignment);
15683   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
15684   verifyFormat("#define A                                                      "
15685                "                \\\n"
15686                "  int aaaa       = 12;                                         "
15687                "                \\\n"
15688                "  int b          = 23;                                         "
15689                "                \\\n"
15690                "  int ccc        = 234;                                        "
15691                "                \\\n"
15692                "  int dddddddddd = 2345;",
15693                Alignment);
15694   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
15695                "k = 4, int l = 5,\n"
15696                "                  int m = 6) {\n"
15697                "  int j      = 10;\n"
15698                "  otherThing = 1;\n"
15699                "}",
15700                Alignment);
15701   verifyFormat("void SomeFunction(int parameter = 0) {\n"
15702                "  int i   = 1;\n"
15703                "  int j   = 2;\n"
15704                "  int big = 10000;\n"
15705                "}",
15706                Alignment);
15707   verifyFormat("class C {\n"
15708                "public:\n"
15709                "  int i            = 1;\n"
15710                "  virtual void f() = 0;\n"
15711                "};",
15712                Alignment);
15713   verifyFormat("int i = 1;\n"
15714                "if (SomeType t = getSomething()) {\n"
15715                "}\n"
15716                "int j   = 2;\n"
15717                "int big = 10000;",
15718                Alignment);
15719   verifyFormat("int j = 7;\n"
15720                "for (int k = 0; k < N; ++k) {\n"
15721                "}\n"
15722                "int j   = 2;\n"
15723                "int big = 10000;\n"
15724                "}",
15725                Alignment);
15726   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
15727   verifyFormat("int i = 1;\n"
15728                "LooooooooooongType loooooooooooooooooooooongVariable\n"
15729                "    = someLooooooooooooooooongFunction();\n"
15730                "int j = 2;",
15731                Alignment);
15732   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
15733   verifyFormat("int i = 1;\n"
15734                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
15735                "    someLooooooooooooooooongFunction();\n"
15736                "int j = 2;",
15737                Alignment);
15738 
15739   verifyFormat("auto lambda = []() {\n"
15740                "  auto i = 0;\n"
15741                "  return 0;\n"
15742                "};\n"
15743                "int i  = 0;\n"
15744                "auto v = type{\n"
15745                "    i = 1,   //\n"
15746                "    (i = 2), //\n"
15747                "    i = 3    //\n"
15748                "};",
15749                Alignment);
15750 
15751   verifyFormat(
15752       "int i      = 1;\n"
15753       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
15754       "                          loooooooooooooooooooooongParameterB);\n"
15755       "int j      = 2;",
15756       Alignment);
15757 
15758   verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
15759                "          typename B   = very_long_type_name_1,\n"
15760                "          typename T_2 = very_long_type_name_2>\n"
15761                "auto foo() {}\n",
15762                Alignment);
15763   verifyFormat("int a, b = 1;\n"
15764                "int c  = 2;\n"
15765                "int dd = 3;\n",
15766                Alignment);
15767   verifyFormat("int aa       = ((1 > 2) ? 3 : 4);\n"
15768                "float b[1][] = {{3.f}};\n",
15769                Alignment);
15770   verifyFormat("for (int i = 0; i < 1; i++)\n"
15771                "  int x = 1;\n",
15772                Alignment);
15773   verifyFormat("for (i = 0; i < 1; i++)\n"
15774                "  x = 1;\n"
15775                "y = 1;\n",
15776                Alignment);
15777 
15778   Alignment.ReflowComments = true;
15779   Alignment.ColumnLimit = 50;
15780   EXPECT_EQ("int x   = 0;\n"
15781             "int yy  = 1; /// specificlennospace\n"
15782             "int zzz = 2;\n",
15783             format("int x   = 0;\n"
15784                    "int yy  = 1; ///specificlennospace\n"
15785                    "int zzz = 2;\n",
15786                    Alignment));
15787 }
15788 
15789 TEST_F(FormatTest, AlignConsecutiveAssignments) {
15790   FormatStyle Alignment = getLLVMStyle();
15791   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
15792   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
15793   verifyFormat("int a = 5;\n"
15794                "int oneTwoThree = 123;",
15795                Alignment);
15796   verifyFormat("int a = 5;\n"
15797                "int oneTwoThree = 123;",
15798                Alignment);
15799 
15800   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
15801   verifyFormat("int a           = 5;\n"
15802                "int oneTwoThree = 123;",
15803                Alignment);
15804   verifyFormat("int a           = method();\n"
15805                "int oneTwoThree = 133;",
15806                Alignment);
15807   verifyFormat("a &= 5;\n"
15808                "bcd *= 5;\n"
15809                "ghtyf += 5;\n"
15810                "dvfvdb -= 5;\n"
15811                "a /= 5;\n"
15812                "vdsvsv %= 5;\n"
15813                "sfdbddfbdfbb ^= 5;\n"
15814                "dvsdsv |= 5;\n"
15815                "int dsvvdvsdvvv = 123;",
15816                Alignment);
15817   verifyFormat("int i = 1, j = 10;\n"
15818                "something = 2000;",
15819                Alignment);
15820   verifyFormat("something = 2000;\n"
15821                "int i = 1, j = 10;\n",
15822                Alignment);
15823   verifyFormat("something = 2000;\n"
15824                "another   = 911;\n"
15825                "int i = 1, j = 10;\n"
15826                "oneMore = 1;\n"
15827                "i       = 2;",
15828                Alignment);
15829   verifyFormat("int a   = 5;\n"
15830                "int one = 1;\n"
15831                "method();\n"
15832                "int oneTwoThree = 123;\n"
15833                "int oneTwo      = 12;",
15834                Alignment);
15835   verifyFormat("int oneTwoThree = 123;\n"
15836                "int oneTwo      = 12;\n"
15837                "method();\n",
15838                Alignment);
15839   verifyFormat("int oneTwoThree = 123; // comment\n"
15840                "int oneTwo      = 12;  // comment",
15841                Alignment);
15842 
15843   // Bug 25167
15844   /* Uncomment when fixed
15845     verifyFormat("#if A\n"
15846                  "#else\n"
15847                  "int aaaaaaaa = 12;\n"
15848                  "#endif\n"
15849                  "#if B\n"
15850                  "#else\n"
15851                  "int a = 12;\n"
15852                  "#endif\n",
15853                  Alignment);
15854     verifyFormat("enum foo {\n"
15855                  "#if A\n"
15856                  "#else\n"
15857                  "  aaaaaaaa = 12;\n"
15858                  "#endif\n"
15859                  "#if B\n"
15860                  "#else\n"
15861                  "  a = 12;\n"
15862                  "#endif\n"
15863                  "};\n",
15864                  Alignment);
15865   */
15866 
15867   EXPECT_EQ("int a = 5;\n"
15868             "\n"
15869             "int oneTwoThree = 123;",
15870             format("int a       = 5;\n"
15871                    "\n"
15872                    "int oneTwoThree= 123;",
15873                    Alignment));
15874   EXPECT_EQ("int a   = 5;\n"
15875             "int one = 1;\n"
15876             "\n"
15877             "int oneTwoThree = 123;",
15878             format("int a = 5;\n"
15879                    "int one = 1;\n"
15880                    "\n"
15881                    "int oneTwoThree = 123;",
15882                    Alignment));
15883   EXPECT_EQ("int a   = 5;\n"
15884             "int one = 1;\n"
15885             "\n"
15886             "int oneTwoThree = 123;\n"
15887             "int oneTwo      = 12;",
15888             format("int a = 5;\n"
15889                    "int one = 1;\n"
15890                    "\n"
15891                    "int oneTwoThree = 123;\n"
15892                    "int oneTwo = 12;",
15893                    Alignment));
15894   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
15895   verifyFormat("#define A \\\n"
15896                "  int aaaa       = 12; \\\n"
15897                "  int b          = 23; \\\n"
15898                "  int ccc        = 234; \\\n"
15899                "  int dddddddddd = 2345;",
15900                Alignment);
15901   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
15902   verifyFormat("#define A               \\\n"
15903                "  int aaaa       = 12;  \\\n"
15904                "  int b          = 23;  \\\n"
15905                "  int ccc        = 234; \\\n"
15906                "  int dddddddddd = 2345;",
15907                Alignment);
15908   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
15909   verifyFormat("#define A                                                      "
15910                "                \\\n"
15911                "  int aaaa       = 12;                                         "
15912                "                \\\n"
15913                "  int b          = 23;                                         "
15914                "                \\\n"
15915                "  int ccc        = 234;                                        "
15916                "                \\\n"
15917                "  int dddddddddd = 2345;",
15918                Alignment);
15919   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
15920                "k = 4, int l = 5,\n"
15921                "                  int m = 6) {\n"
15922                "  int j      = 10;\n"
15923                "  otherThing = 1;\n"
15924                "}",
15925                Alignment);
15926   verifyFormat("void SomeFunction(int parameter = 0) {\n"
15927                "  int i   = 1;\n"
15928                "  int j   = 2;\n"
15929                "  int big = 10000;\n"
15930                "}",
15931                Alignment);
15932   verifyFormat("class C {\n"
15933                "public:\n"
15934                "  int i            = 1;\n"
15935                "  virtual void f() = 0;\n"
15936                "};",
15937                Alignment);
15938   verifyFormat("int i = 1;\n"
15939                "if (SomeType t = getSomething()) {\n"
15940                "}\n"
15941                "int j   = 2;\n"
15942                "int big = 10000;",
15943                Alignment);
15944   verifyFormat("int j = 7;\n"
15945                "for (int k = 0; k < N; ++k) {\n"
15946                "}\n"
15947                "int j   = 2;\n"
15948                "int big = 10000;\n"
15949                "}",
15950                Alignment);
15951   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
15952   verifyFormat("int i = 1;\n"
15953                "LooooooooooongType loooooooooooooooooooooongVariable\n"
15954                "    = someLooooooooooooooooongFunction();\n"
15955                "int j = 2;",
15956                Alignment);
15957   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
15958   verifyFormat("int i = 1;\n"
15959                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
15960                "    someLooooooooooooooooongFunction();\n"
15961                "int j = 2;",
15962                Alignment);
15963 
15964   verifyFormat("auto lambda = []() {\n"
15965                "  auto i = 0;\n"
15966                "  return 0;\n"
15967                "};\n"
15968                "int i  = 0;\n"
15969                "auto v = type{\n"
15970                "    i = 1,   //\n"
15971                "    (i = 2), //\n"
15972                "    i = 3    //\n"
15973                "};",
15974                Alignment);
15975 
15976   verifyFormat(
15977       "int i      = 1;\n"
15978       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
15979       "                          loooooooooooooooooooooongParameterB);\n"
15980       "int j      = 2;",
15981       Alignment);
15982 
15983   verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
15984                "          typename B   = very_long_type_name_1,\n"
15985                "          typename T_2 = very_long_type_name_2>\n"
15986                "auto foo() {}\n",
15987                Alignment);
15988   verifyFormat("int a, b = 1;\n"
15989                "int c  = 2;\n"
15990                "int dd = 3;\n",
15991                Alignment);
15992   verifyFormat("int aa       = ((1 > 2) ? 3 : 4);\n"
15993                "float b[1][] = {{3.f}};\n",
15994                Alignment);
15995   verifyFormat("for (int i = 0; i < 1; i++)\n"
15996                "  int x = 1;\n",
15997                Alignment);
15998   verifyFormat("for (i = 0; i < 1; i++)\n"
15999                "  x = 1;\n"
16000                "y = 1;\n",
16001                Alignment);
16002 
16003   Alignment.ReflowComments = true;
16004   Alignment.ColumnLimit = 50;
16005   EXPECT_EQ("int x   = 0;\n"
16006             "int yy  = 1; /// specificlennospace\n"
16007             "int zzz = 2;\n",
16008             format("int x   = 0;\n"
16009                    "int yy  = 1; ///specificlennospace\n"
16010                    "int zzz = 2;\n",
16011                    Alignment));
16012 }
16013 
16014 TEST_F(FormatTest, AlignConsecutiveBitFields) {
16015   FormatStyle Alignment = getLLVMStyle();
16016   Alignment.AlignConsecutiveBitFields = FormatStyle::ACS_Consecutive;
16017   verifyFormat("int const a     : 5;\n"
16018                "int oneTwoThree : 23;",
16019                Alignment);
16020 
16021   // Initializers are allowed starting with c++2a
16022   verifyFormat("int const a     : 5 = 1;\n"
16023                "int oneTwoThree : 23 = 0;",
16024                Alignment);
16025 
16026   Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
16027   verifyFormat("int const a           : 5;\n"
16028                "int       oneTwoThree : 23;",
16029                Alignment);
16030 
16031   verifyFormat("int const a           : 5;  // comment\n"
16032                "int       oneTwoThree : 23; // comment",
16033                Alignment);
16034 
16035   verifyFormat("int const a           : 5 = 1;\n"
16036                "int       oneTwoThree : 23 = 0;",
16037                Alignment);
16038 
16039   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16040   verifyFormat("int const a           : 5  = 1;\n"
16041                "int       oneTwoThree : 23 = 0;",
16042                Alignment);
16043   verifyFormat("int const a           : 5  = {1};\n"
16044                "int       oneTwoThree : 23 = 0;",
16045                Alignment);
16046 
16047   Alignment.BitFieldColonSpacing = FormatStyle::BFCS_None;
16048   verifyFormat("int const a          :5;\n"
16049                "int       oneTwoThree:23;",
16050                Alignment);
16051 
16052   Alignment.BitFieldColonSpacing = FormatStyle::BFCS_Before;
16053   verifyFormat("int const a           :5;\n"
16054                "int       oneTwoThree :23;",
16055                Alignment);
16056 
16057   Alignment.BitFieldColonSpacing = FormatStyle::BFCS_After;
16058   verifyFormat("int const a          : 5;\n"
16059                "int       oneTwoThree: 23;",
16060                Alignment);
16061 
16062   // Known limitations: ':' is only recognized as a bitfield colon when
16063   // followed by a number.
16064   /*
16065   verifyFormat("int oneTwoThree : SOME_CONSTANT;\n"
16066                "int a           : 5;",
16067                Alignment);
16068   */
16069 }
16070 
16071 TEST_F(FormatTest, AlignConsecutiveDeclarations) {
16072   FormatStyle Alignment = getLLVMStyle();
16073   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
16074   Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_None;
16075   Alignment.PointerAlignment = FormatStyle::PAS_Right;
16076   verifyFormat("float const a = 5;\n"
16077                "int oneTwoThree = 123;",
16078                Alignment);
16079   verifyFormat("int a = 5;\n"
16080                "float const oneTwoThree = 123;",
16081                Alignment);
16082 
16083   Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
16084   verifyFormat("float const a = 5;\n"
16085                "int         oneTwoThree = 123;",
16086                Alignment);
16087   verifyFormat("int         a = method();\n"
16088                "float const oneTwoThree = 133;",
16089                Alignment);
16090   verifyFormat("int i = 1, j = 10;\n"
16091                "something = 2000;",
16092                Alignment);
16093   verifyFormat("something = 2000;\n"
16094                "int i = 1, j = 10;\n",
16095                Alignment);
16096   verifyFormat("float      something = 2000;\n"
16097                "double     another = 911;\n"
16098                "int        i = 1, j = 10;\n"
16099                "const int *oneMore = 1;\n"
16100                "unsigned   i = 2;",
16101                Alignment);
16102   verifyFormat("float a = 5;\n"
16103                "int   one = 1;\n"
16104                "method();\n"
16105                "const double       oneTwoThree = 123;\n"
16106                "const unsigned int oneTwo = 12;",
16107                Alignment);
16108   verifyFormat("int      oneTwoThree{0}; // comment\n"
16109                "unsigned oneTwo;         // comment",
16110                Alignment);
16111   verifyFormat("unsigned int       *a;\n"
16112                "int                *b;\n"
16113                "unsigned int Const *c;\n"
16114                "unsigned int const *d;\n"
16115                "unsigned int Const &e;\n"
16116                "unsigned int const &f;",
16117                Alignment);
16118   verifyFormat("Const unsigned int *c;\n"
16119                "const unsigned int *d;\n"
16120                "Const unsigned int &e;\n"
16121                "const unsigned int &f;\n"
16122                "const unsigned      g;\n"
16123                "Const unsigned      h;",
16124                Alignment);
16125   EXPECT_EQ("float const a = 5;\n"
16126             "\n"
16127             "int oneTwoThree = 123;",
16128             format("float const   a = 5;\n"
16129                    "\n"
16130                    "int           oneTwoThree= 123;",
16131                    Alignment));
16132   EXPECT_EQ("float a = 5;\n"
16133             "int   one = 1;\n"
16134             "\n"
16135             "unsigned oneTwoThree = 123;",
16136             format("float    a = 5;\n"
16137                    "int      one = 1;\n"
16138                    "\n"
16139                    "unsigned oneTwoThree = 123;",
16140                    Alignment));
16141   EXPECT_EQ("float a = 5;\n"
16142             "int   one = 1;\n"
16143             "\n"
16144             "unsigned oneTwoThree = 123;\n"
16145             "int      oneTwo = 12;",
16146             format("float    a = 5;\n"
16147                    "int one = 1;\n"
16148                    "\n"
16149                    "unsigned oneTwoThree = 123;\n"
16150                    "int oneTwo = 12;",
16151                    Alignment));
16152   // Function prototype alignment
16153   verifyFormat("int    a();\n"
16154                "double b();",
16155                Alignment);
16156   verifyFormat("int    a(int x);\n"
16157                "double b();",
16158                Alignment);
16159   unsigned OldColumnLimit = Alignment.ColumnLimit;
16160   // We need to set ColumnLimit to zero, in order to stress nested alignments,
16161   // otherwise the function parameters will be re-flowed onto a single line.
16162   Alignment.ColumnLimit = 0;
16163   EXPECT_EQ("int    a(int   x,\n"
16164             "         float y);\n"
16165             "double b(int    x,\n"
16166             "         double y);",
16167             format("int a(int x,\n"
16168                    " float y);\n"
16169                    "double b(int x,\n"
16170                    " double y);",
16171                    Alignment));
16172   // This ensures that function parameters of function declarations are
16173   // correctly indented when their owning functions are indented.
16174   // The failure case here is for 'double y' to not be indented enough.
16175   EXPECT_EQ("double a(int x);\n"
16176             "int    b(int    y,\n"
16177             "         double z);",
16178             format("double a(int x);\n"
16179                    "int b(int y,\n"
16180                    " double z);",
16181                    Alignment));
16182   // Set ColumnLimit low so that we induce wrapping immediately after
16183   // the function name and opening paren.
16184   Alignment.ColumnLimit = 13;
16185   verifyFormat("int function(\n"
16186                "    int  x,\n"
16187                "    bool y);",
16188                Alignment);
16189   Alignment.ColumnLimit = OldColumnLimit;
16190   // Ensure function pointers don't screw up recursive alignment
16191   verifyFormat("int    a(int x, void (*fp)(int y));\n"
16192                "double b();",
16193                Alignment);
16194   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16195   // Ensure recursive alignment is broken by function braces, so that the
16196   // "a = 1" does not align with subsequent assignments inside the function
16197   // body.
16198   verifyFormat("int func(int a = 1) {\n"
16199                "  int b  = 2;\n"
16200                "  int cc = 3;\n"
16201                "}",
16202                Alignment);
16203   verifyFormat("float      something = 2000;\n"
16204                "double     another   = 911;\n"
16205                "int        i = 1, j = 10;\n"
16206                "const int *oneMore = 1;\n"
16207                "unsigned   i       = 2;",
16208                Alignment);
16209   verifyFormat("int      oneTwoThree = {0}; // comment\n"
16210                "unsigned oneTwo      = 0;   // comment",
16211                Alignment);
16212   // Make sure that scope is correctly tracked, in the absence of braces
16213   verifyFormat("for (int i = 0; i < n; i++)\n"
16214                "  j = i;\n"
16215                "double x = 1;\n",
16216                Alignment);
16217   verifyFormat("if (int i = 0)\n"
16218                "  j = i;\n"
16219                "double x = 1;\n",
16220                Alignment);
16221   // Ensure operator[] and operator() are comprehended
16222   verifyFormat("struct test {\n"
16223                "  long long int foo();\n"
16224                "  int           operator[](int a);\n"
16225                "  double        bar();\n"
16226                "};\n",
16227                Alignment);
16228   verifyFormat("struct test {\n"
16229                "  long long int foo();\n"
16230                "  int           operator()(int a);\n"
16231                "  double        bar();\n"
16232                "};\n",
16233                Alignment);
16234 
16235   // PAS_Right
16236   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
16237             "  int const i   = 1;\n"
16238             "  int      *j   = 2;\n"
16239             "  int       big = 10000;\n"
16240             "\n"
16241             "  unsigned oneTwoThree = 123;\n"
16242             "  int      oneTwo      = 12;\n"
16243             "  method();\n"
16244             "  float k  = 2;\n"
16245             "  int   ll = 10000;\n"
16246             "}",
16247             format("void SomeFunction(int parameter= 0) {\n"
16248                    " int const  i= 1;\n"
16249                    "  int *j=2;\n"
16250                    " int big  =  10000;\n"
16251                    "\n"
16252                    "unsigned oneTwoThree  =123;\n"
16253                    "int oneTwo = 12;\n"
16254                    "  method();\n"
16255                    "float k= 2;\n"
16256                    "int ll=10000;\n"
16257                    "}",
16258                    Alignment));
16259   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
16260             "  int const i   = 1;\n"
16261             "  int     **j   = 2, ***k;\n"
16262             "  int      &k   = i;\n"
16263             "  int     &&l   = i + j;\n"
16264             "  int       big = 10000;\n"
16265             "\n"
16266             "  unsigned oneTwoThree = 123;\n"
16267             "  int      oneTwo      = 12;\n"
16268             "  method();\n"
16269             "  float k  = 2;\n"
16270             "  int   ll = 10000;\n"
16271             "}",
16272             format("void SomeFunction(int parameter= 0) {\n"
16273                    " int const  i= 1;\n"
16274                    "  int **j=2,***k;\n"
16275                    "int &k=i;\n"
16276                    "int &&l=i+j;\n"
16277                    " int big  =  10000;\n"
16278                    "\n"
16279                    "unsigned oneTwoThree  =123;\n"
16280                    "int oneTwo = 12;\n"
16281                    "  method();\n"
16282                    "float k= 2;\n"
16283                    "int ll=10000;\n"
16284                    "}",
16285                    Alignment));
16286   // variables are aligned at their name, pointers are at the right most
16287   // position
16288   verifyFormat("int   *a;\n"
16289                "int  **b;\n"
16290                "int ***c;\n"
16291                "int    foobar;\n",
16292                Alignment);
16293 
16294   // PAS_Left
16295   FormatStyle AlignmentLeft = Alignment;
16296   AlignmentLeft.PointerAlignment = FormatStyle::PAS_Left;
16297   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
16298             "  int const i   = 1;\n"
16299             "  int*      j   = 2;\n"
16300             "  int       big = 10000;\n"
16301             "\n"
16302             "  unsigned oneTwoThree = 123;\n"
16303             "  int      oneTwo      = 12;\n"
16304             "  method();\n"
16305             "  float k  = 2;\n"
16306             "  int   ll = 10000;\n"
16307             "}",
16308             format("void SomeFunction(int parameter= 0) {\n"
16309                    " int const  i= 1;\n"
16310                    "  int *j=2;\n"
16311                    " int big  =  10000;\n"
16312                    "\n"
16313                    "unsigned oneTwoThree  =123;\n"
16314                    "int oneTwo = 12;\n"
16315                    "  method();\n"
16316                    "float k= 2;\n"
16317                    "int ll=10000;\n"
16318                    "}",
16319                    AlignmentLeft));
16320   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
16321             "  int const i   = 1;\n"
16322             "  int**     j   = 2;\n"
16323             "  int&      k   = i;\n"
16324             "  int&&     l   = i + j;\n"
16325             "  int       big = 10000;\n"
16326             "\n"
16327             "  unsigned oneTwoThree = 123;\n"
16328             "  int      oneTwo      = 12;\n"
16329             "  method();\n"
16330             "  float k  = 2;\n"
16331             "  int   ll = 10000;\n"
16332             "}",
16333             format("void SomeFunction(int parameter= 0) {\n"
16334                    " int const  i= 1;\n"
16335                    "  int **j=2;\n"
16336                    "int &k=i;\n"
16337                    "int &&l=i+j;\n"
16338                    " int big  =  10000;\n"
16339                    "\n"
16340                    "unsigned oneTwoThree  =123;\n"
16341                    "int oneTwo = 12;\n"
16342                    "  method();\n"
16343                    "float k= 2;\n"
16344                    "int ll=10000;\n"
16345                    "}",
16346                    AlignmentLeft));
16347   // variables are aligned at their name, pointers are at the left most position
16348   verifyFormat("int*   a;\n"
16349                "int**  b;\n"
16350                "int*** c;\n"
16351                "int    foobar;\n",
16352                AlignmentLeft);
16353 
16354   // PAS_Middle
16355   FormatStyle AlignmentMiddle = Alignment;
16356   AlignmentMiddle.PointerAlignment = FormatStyle::PAS_Middle;
16357   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
16358             "  int const i   = 1;\n"
16359             "  int *     j   = 2;\n"
16360             "  int       big = 10000;\n"
16361             "\n"
16362             "  unsigned oneTwoThree = 123;\n"
16363             "  int      oneTwo      = 12;\n"
16364             "  method();\n"
16365             "  float k  = 2;\n"
16366             "  int   ll = 10000;\n"
16367             "}",
16368             format("void SomeFunction(int parameter= 0) {\n"
16369                    " int const  i= 1;\n"
16370                    "  int *j=2;\n"
16371                    " int big  =  10000;\n"
16372                    "\n"
16373                    "unsigned oneTwoThree  =123;\n"
16374                    "int oneTwo = 12;\n"
16375                    "  method();\n"
16376                    "float k= 2;\n"
16377                    "int ll=10000;\n"
16378                    "}",
16379                    AlignmentMiddle));
16380   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
16381             "  int const i   = 1;\n"
16382             "  int **    j   = 2, ***k;\n"
16383             "  int &     k   = i;\n"
16384             "  int &&    l   = i + j;\n"
16385             "  int       big = 10000;\n"
16386             "\n"
16387             "  unsigned oneTwoThree = 123;\n"
16388             "  int      oneTwo      = 12;\n"
16389             "  method();\n"
16390             "  float k  = 2;\n"
16391             "  int   ll = 10000;\n"
16392             "}",
16393             format("void SomeFunction(int parameter= 0) {\n"
16394                    " int const  i= 1;\n"
16395                    "  int **j=2,***k;\n"
16396                    "int &k=i;\n"
16397                    "int &&l=i+j;\n"
16398                    " int big  =  10000;\n"
16399                    "\n"
16400                    "unsigned oneTwoThree  =123;\n"
16401                    "int oneTwo = 12;\n"
16402                    "  method();\n"
16403                    "float k= 2;\n"
16404                    "int ll=10000;\n"
16405                    "}",
16406                    AlignmentMiddle));
16407   // variables are aligned at their name, pointers are in the middle
16408   verifyFormat("int *   a;\n"
16409                "int *   b;\n"
16410                "int *** c;\n"
16411                "int     foobar;\n",
16412                AlignmentMiddle);
16413 
16414   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
16415   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
16416   verifyFormat("#define A \\\n"
16417                "  int       aaaa = 12; \\\n"
16418                "  float     b = 23; \\\n"
16419                "  const int ccc = 234; \\\n"
16420                "  unsigned  dddddddddd = 2345;",
16421                Alignment);
16422   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
16423   verifyFormat("#define A              \\\n"
16424                "  int       aaaa = 12; \\\n"
16425                "  float     b = 23;    \\\n"
16426                "  const int ccc = 234; \\\n"
16427                "  unsigned  dddddddddd = 2345;",
16428                Alignment);
16429   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
16430   Alignment.ColumnLimit = 30;
16431   verifyFormat("#define A                    \\\n"
16432                "  int       aaaa = 12;       \\\n"
16433                "  float     b = 23;          \\\n"
16434                "  const int ccc = 234;       \\\n"
16435                "  int       dddddddddd = 2345;",
16436                Alignment);
16437   Alignment.ColumnLimit = 80;
16438   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
16439                "k = 4, int l = 5,\n"
16440                "                  int m = 6) {\n"
16441                "  const int j = 10;\n"
16442                "  otherThing = 1;\n"
16443                "}",
16444                Alignment);
16445   verifyFormat("void SomeFunction(int parameter = 0) {\n"
16446                "  int const i = 1;\n"
16447                "  int      *j = 2;\n"
16448                "  int       big = 10000;\n"
16449                "}",
16450                Alignment);
16451   verifyFormat("class C {\n"
16452                "public:\n"
16453                "  int          i = 1;\n"
16454                "  virtual void f() = 0;\n"
16455                "};",
16456                Alignment);
16457   verifyFormat("float i = 1;\n"
16458                "if (SomeType t = getSomething()) {\n"
16459                "}\n"
16460                "const unsigned j = 2;\n"
16461                "int            big = 10000;",
16462                Alignment);
16463   verifyFormat("float j = 7;\n"
16464                "for (int k = 0; k < N; ++k) {\n"
16465                "}\n"
16466                "unsigned j = 2;\n"
16467                "int      big = 10000;\n"
16468                "}",
16469                Alignment);
16470   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
16471   verifyFormat("float              i = 1;\n"
16472                "LooooooooooongType loooooooooooooooooooooongVariable\n"
16473                "    = someLooooooooooooooooongFunction();\n"
16474                "int j = 2;",
16475                Alignment);
16476   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
16477   verifyFormat("int                i = 1;\n"
16478                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
16479                "    someLooooooooooooooooongFunction();\n"
16480                "int j = 2;",
16481                Alignment);
16482 
16483   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16484   verifyFormat("auto lambda = []() {\n"
16485                "  auto  ii = 0;\n"
16486                "  float j  = 0;\n"
16487                "  return 0;\n"
16488                "};\n"
16489                "int   i  = 0;\n"
16490                "float i2 = 0;\n"
16491                "auto  v  = type{\n"
16492                "    i = 1,   //\n"
16493                "    (i = 2), //\n"
16494                "    i = 3    //\n"
16495                "};",
16496                Alignment);
16497   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
16498 
16499   verifyFormat(
16500       "int      i = 1;\n"
16501       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
16502       "                          loooooooooooooooooooooongParameterB);\n"
16503       "int      j = 2;",
16504       Alignment);
16505 
16506   // Test interactions with ColumnLimit and AlignConsecutiveAssignments:
16507   // We expect declarations and assignments to align, as long as it doesn't
16508   // exceed the column limit, starting a new alignment sequence whenever it
16509   // happens.
16510   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16511   Alignment.ColumnLimit = 30;
16512   verifyFormat("float    ii              = 1;\n"
16513                "unsigned j               = 2;\n"
16514                "int someVerylongVariable = 1;\n"
16515                "AnotherLongType  ll = 123456;\n"
16516                "VeryVeryLongType k  = 2;\n"
16517                "int              myvar = 1;",
16518                Alignment);
16519   Alignment.ColumnLimit = 80;
16520   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
16521 
16522   verifyFormat(
16523       "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n"
16524       "          typename LongType, typename B>\n"
16525       "auto foo() {}\n",
16526       Alignment);
16527   verifyFormat("float a, b = 1;\n"
16528                "int   c = 2;\n"
16529                "int   dd = 3;\n",
16530                Alignment);
16531   verifyFormat("int   aa = ((1 > 2) ? 3 : 4);\n"
16532                "float b[1][] = {{3.f}};\n",
16533                Alignment);
16534   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16535   verifyFormat("float a, b = 1;\n"
16536                "int   c  = 2;\n"
16537                "int   dd = 3;\n",
16538                Alignment);
16539   verifyFormat("int   aa     = ((1 > 2) ? 3 : 4);\n"
16540                "float b[1][] = {{3.f}};\n",
16541                Alignment);
16542   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
16543 
16544   Alignment.ColumnLimit = 30;
16545   Alignment.BinPackParameters = false;
16546   verifyFormat("void foo(float     a,\n"
16547                "         float     b,\n"
16548                "         int       c,\n"
16549                "         uint32_t *d) {\n"
16550                "  int   *e = 0;\n"
16551                "  float  f = 0;\n"
16552                "  double g = 0;\n"
16553                "}\n"
16554                "void bar(ino_t     a,\n"
16555                "         int       b,\n"
16556                "         uint32_t *c,\n"
16557                "         bool      d) {}\n",
16558                Alignment);
16559   Alignment.BinPackParameters = true;
16560   Alignment.ColumnLimit = 80;
16561 
16562   // Bug 33507
16563   Alignment.PointerAlignment = FormatStyle::PAS_Middle;
16564   verifyFormat(
16565       "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n"
16566       "  static const Version verVs2017;\n"
16567       "  return true;\n"
16568       "});\n",
16569       Alignment);
16570   Alignment.PointerAlignment = FormatStyle::PAS_Right;
16571 
16572   // See llvm.org/PR35641
16573   Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
16574   verifyFormat("int func() { //\n"
16575                "  int      b;\n"
16576                "  unsigned c;\n"
16577                "}",
16578                Alignment);
16579 
16580   // See PR37175
16581   FormatStyle Style = getMozillaStyle();
16582   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
16583   EXPECT_EQ("DECOR1 /**/ int8_t /**/ DECOR2 /**/\n"
16584             "foo(int a);",
16585             format("DECOR1 /**/ int8_t /**/ DECOR2 /**/ foo (int a);", Style));
16586 
16587   Alignment.PointerAlignment = FormatStyle::PAS_Left;
16588   verifyFormat("unsigned int*       a;\n"
16589                "int*                b;\n"
16590                "unsigned int Const* c;\n"
16591                "unsigned int const* d;\n"
16592                "unsigned int Const& e;\n"
16593                "unsigned int const& f;",
16594                Alignment);
16595   verifyFormat("Const unsigned int* c;\n"
16596                "const unsigned int* d;\n"
16597                "Const unsigned int& e;\n"
16598                "const unsigned int& f;\n"
16599                "const unsigned      g;\n"
16600                "Const unsigned      h;",
16601                Alignment);
16602 
16603   Alignment.PointerAlignment = FormatStyle::PAS_Middle;
16604   verifyFormat("unsigned int *       a;\n"
16605                "int *                b;\n"
16606                "unsigned int Const * c;\n"
16607                "unsigned int const * d;\n"
16608                "unsigned int Const & e;\n"
16609                "unsigned int const & f;",
16610                Alignment);
16611   verifyFormat("Const unsigned int * c;\n"
16612                "const unsigned int * d;\n"
16613                "Const unsigned int & e;\n"
16614                "const unsigned int & f;\n"
16615                "const unsigned       g;\n"
16616                "Const unsigned       h;",
16617                Alignment);
16618 }
16619 
16620 TEST_F(FormatTest, AlignWithLineBreaks) {
16621   auto Style = getLLVMStyleWithColumns(120);
16622 
16623   EXPECT_EQ(Style.AlignConsecutiveAssignments, FormatStyle::ACS_None);
16624   EXPECT_EQ(Style.AlignConsecutiveDeclarations, FormatStyle::ACS_None);
16625   verifyFormat("void foo() {\n"
16626                "  int myVar = 5;\n"
16627                "  double x = 3.14;\n"
16628                "  auto str = \"Hello \"\n"
16629                "             \"World\";\n"
16630                "  auto s = \"Hello \"\n"
16631                "           \"Again\";\n"
16632                "}",
16633                Style);
16634 
16635   // clang-format off
16636   verifyFormat("void foo() {\n"
16637                "  const int capacityBefore = Entries.capacity();\n"
16638                "  const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
16639                "                                            std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
16640                "  const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
16641                "                                          std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
16642                "}",
16643                Style);
16644   // clang-format on
16645 
16646   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16647   verifyFormat("void foo() {\n"
16648                "  int myVar = 5;\n"
16649                "  double x  = 3.14;\n"
16650                "  auto str  = \"Hello \"\n"
16651                "              \"World\";\n"
16652                "  auto s    = \"Hello \"\n"
16653                "              \"Again\";\n"
16654                "}",
16655                Style);
16656 
16657   // clang-format off
16658   verifyFormat("void foo() {\n"
16659                "  const int capacityBefore = Entries.capacity();\n"
16660                "  const auto newEntry      = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
16661                "                                                 std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
16662                "  const X newEntry2        = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
16663                "                                                 std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
16664                "}",
16665                Style);
16666   // clang-format on
16667 
16668   Style.AlignConsecutiveAssignments = FormatStyle::ACS_None;
16669   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
16670   verifyFormat("void foo() {\n"
16671                "  int    myVar = 5;\n"
16672                "  double x = 3.14;\n"
16673                "  auto   str = \"Hello \"\n"
16674                "               \"World\";\n"
16675                "  auto   s = \"Hello \"\n"
16676                "             \"Again\";\n"
16677                "}",
16678                Style);
16679 
16680   // clang-format off
16681   verifyFormat("void foo() {\n"
16682                "  const int  capacityBefore = Entries.capacity();\n"
16683                "  const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
16684                "                                            std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
16685                "  const X    newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
16686                "                                             std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
16687                "}",
16688                Style);
16689   // clang-format on
16690 
16691   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16692   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
16693 
16694   verifyFormat("void foo() {\n"
16695                "  int    myVar = 5;\n"
16696                "  double x     = 3.14;\n"
16697                "  auto   str   = \"Hello \"\n"
16698                "                 \"World\";\n"
16699                "  auto   s     = \"Hello \"\n"
16700                "                 \"Again\";\n"
16701                "}",
16702                Style);
16703 
16704   // clang-format off
16705   verifyFormat("void foo() {\n"
16706                "  const int  capacityBefore = Entries.capacity();\n"
16707                "  const auto newEntry       = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
16708                "                                                  std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
16709                "  const X    newEntry2      = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
16710                "                                                  std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
16711                "}",
16712                Style);
16713   // clang-format on
16714 
16715   Style = getLLVMStyleWithColumns(120);
16716   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16717   Style.ContinuationIndentWidth = 4;
16718   Style.IndentWidth = 4;
16719 
16720   // clang-format off
16721   verifyFormat("void SomeFunc() {\n"
16722                "    newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n"
16723                "                                                        seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
16724                "    newWatcher.maxAge     = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n"
16725                "                                                        seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
16726                "    newWatcher.max        = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n"
16727                "                                                        seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
16728                "}",
16729                Style);
16730   // clang-format on
16731 
16732   Style.BinPackArguments = false;
16733 
16734   // clang-format off
16735   verifyFormat("void SomeFunc() {\n"
16736                "    newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(\n"
16737                "        FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
16738                "    newWatcher.maxAge     = ToLegacyTimestamp(GetMaxAge(\n"
16739                "        FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
16740                "    newWatcher.max        = ToLegacyTimestamp(GetMaxAge(\n"
16741                "        FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
16742                "}",
16743                Style);
16744   // clang-format on
16745 }
16746 
16747 TEST_F(FormatTest, AlignWithInitializerPeriods) {
16748   auto Style = getLLVMStyleWithColumns(60);
16749 
16750   verifyFormat("void foo1(void) {\n"
16751                "  BYTE p[1] = 1;\n"
16752                "  A B = {.one_foooooooooooooooo = 2,\n"
16753                "         .two_fooooooooooooo = 3,\n"
16754                "         .three_fooooooooooooo = 4};\n"
16755                "  BYTE payload = 2;\n"
16756                "}",
16757                Style);
16758 
16759   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16760   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_None;
16761   verifyFormat("void foo2(void) {\n"
16762                "  BYTE p[1]    = 1;\n"
16763                "  A B          = {.one_foooooooooooooooo = 2,\n"
16764                "                  .two_fooooooooooooo    = 3,\n"
16765                "                  .three_fooooooooooooo  = 4};\n"
16766                "  BYTE payload = 2;\n"
16767                "}",
16768                Style);
16769 
16770   Style.AlignConsecutiveAssignments = FormatStyle::ACS_None;
16771   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
16772   verifyFormat("void foo3(void) {\n"
16773                "  BYTE p[1] = 1;\n"
16774                "  A    B = {.one_foooooooooooooooo = 2,\n"
16775                "            .two_fooooooooooooo = 3,\n"
16776                "            .three_fooooooooooooo = 4};\n"
16777                "  BYTE payload = 2;\n"
16778                "}",
16779                Style);
16780 
16781   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16782   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
16783   verifyFormat("void foo4(void) {\n"
16784                "  BYTE p[1]    = 1;\n"
16785                "  A    B       = {.one_foooooooooooooooo = 2,\n"
16786                "                  .two_fooooooooooooo    = 3,\n"
16787                "                  .three_fooooooooooooo  = 4};\n"
16788                "  BYTE payload = 2;\n"
16789                "}",
16790                Style);
16791 }
16792 
16793 TEST_F(FormatTest, LinuxBraceBreaking) {
16794   FormatStyle LinuxBraceStyle = getLLVMStyle();
16795   LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
16796   verifyFormat("namespace a\n"
16797                "{\n"
16798                "class A\n"
16799                "{\n"
16800                "  void f()\n"
16801                "  {\n"
16802                "    if (true) {\n"
16803                "      a();\n"
16804                "      b();\n"
16805                "    } else {\n"
16806                "      a();\n"
16807                "    }\n"
16808                "  }\n"
16809                "  void g() { return; }\n"
16810                "};\n"
16811                "struct B {\n"
16812                "  int x;\n"
16813                "};\n"
16814                "} // namespace a\n",
16815                LinuxBraceStyle);
16816   verifyFormat("enum X {\n"
16817                "  Y = 0,\n"
16818                "}\n",
16819                LinuxBraceStyle);
16820   verifyFormat("struct S {\n"
16821                "  int Type;\n"
16822                "  union {\n"
16823                "    int x;\n"
16824                "    double y;\n"
16825                "  } Value;\n"
16826                "  class C\n"
16827                "  {\n"
16828                "    MyFavoriteType Value;\n"
16829                "  } Class;\n"
16830                "}\n",
16831                LinuxBraceStyle);
16832 }
16833 
16834 TEST_F(FormatTest, MozillaBraceBreaking) {
16835   FormatStyle MozillaBraceStyle = getLLVMStyle();
16836   MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
16837   MozillaBraceStyle.FixNamespaceComments = false;
16838   verifyFormat("namespace a {\n"
16839                "class A\n"
16840                "{\n"
16841                "  void f()\n"
16842                "  {\n"
16843                "    if (true) {\n"
16844                "      a();\n"
16845                "      b();\n"
16846                "    }\n"
16847                "  }\n"
16848                "  void g() { return; }\n"
16849                "};\n"
16850                "enum E\n"
16851                "{\n"
16852                "  A,\n"
16853                "  // foo\n"
16854                "  B,\n"
16855                "  C\n"
16856                "};\n"
16857                "struct B\n"
16858                "{\n"
16859                "  int x;\n"
16860                "};\n"
16861                "}\n",
16862                MozillaBraceStyle);
16863   verifyFormat("struct S\n"
16864                "{\n"
16865                "  int Type;\n"
16866                "  union\n"
16867                "  {\n"
16868                "    int x;\n"
16869                "    double y;\n"
16870                "  } Value;\n"
16871                "  class C\n"
16872                "  {\n"
16873                "    MyFavoriteType Value;\n"
16874                "  } Class;\n"
16875                "}\n",
16876                MozillaBraceStyle);
16877 }
16878 
16879 TEST_F(FormatTest, StroustrupBraceBreaking) {
16880   FormatStyle StroustrupBraceStyle = getLLVMStyle();
16881   StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
16882   verifyFormat("namespace a {\n"
16883                "class A {\n"
16884                "  void f()\n"
16885                "  {\n"
16886                "    if (true) {\n"
16887                "      a();\n"
16888                "      b();\n"
16889                "    }\n"
16890                "  }\n"
16891                "  void g() { return; }\n"
16892                "};\n"
16893                "struct B {\n"
16894                "  int x;\n"
16895                "};\n"
16896                "} // namespace a\n",
16897                StroustrupBraceStyle);
16898 
16899   verifyFormat("void foo()\n"
16900                "{\n"
16901                "  if (a) {\n"
16902                "    a();\n"
16903                "  }\n"
16904                "  else {\n"
16905                "    b();\n"
16906                "  }\n"
16907                "}\n",
16908                StroustrupBraceStyle);
16909 
16910   verifyFormat("#ifdef _DEBUG\n"
16911                "int foo(int i = 0)\n"
16912                "#else\n"
16913                "int foo(int i = 5)\n"
16914                "#endif\n"
16915                "{\n"
16916                "  return i;\n"
16917                "}",
16918                StroustrupBraceStyle);
16919 
16920   verifyFormat("void foo() {}\n"
16921                "void bar()\n"
16922                "#ifdef _DEBUG\n"
16923                "{\n"
16924                "  foo();\n"
16925                "}\n"
16926                "#else\n"
16927                "{\n"
16928                "}\n"
16929                "#endif",
16930                StroustrupBraceStyle);
16931 
16932   verifyFormat("void foobar() { int i = 5; }\n"
16933                "#ifdef _DEBUG\n"
16934                "void bar() {}\n"
16935                "#else\n"
16936                "void bar() { foobar(); }\n"
16937                "#endif",
16938                StroustrupBraceStyle);
16939 }
16940 
16941 TEST_F(FormatTest, AllmanBraceBreaking) {
16942   FormatStyle AllmanBraceStyle = getLLVMStyle();
16943   AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
16944 
16945   EXPECT_EQ("namespace a\n"
16946             "{\n"
16947             "void f();\n"
16948             "void g();\n"
16949             "} // namespace a\n",
16950             format("namespace a\n"
16951                    "{\n"
16952                    "void f();\n"
16953                    "void g();\n"
16954                    "}\n",
16955                    AllmanBraceStyle));
16956 
16957   verifyFormat("namespace a\n"
16958                "{\n"
16959                "class A\n"
16960                "{\n"
16961                "  void f()\n"
16962                "  {\n"
16963                "    if (true)\n"
16964                "    {\n"
16965                "      a();\n"
16966                "      b();\n"
16967                "    }\n"
16968                "  }\n"
16969                "  void g() { return; }\n"
16970                "};\n"
16971                "struct B\n"
16972                "{\n"
16973                "  int x;\n"
16974                "};\n"
16975                "union C\n"
16976                "{\n"
16977                "};\n"
16978                "} // namespace a",
16979                AllmanBraceStyle);
16980 
16981   verifyFormat("void f()\n"
16982                "{\n"
16983                "  if (true)\n"
16984                "  {\n"
16985                "    a();\n"
16986                "  }\n"
16987                "  else if (false)\n"
16988                "  {\n"
16989                "    b();\n"
16990                "  }\n"
16991                "  else\n"
16992                "  {\n"
16993                "    c();\n"
16994                "  }\n"
16995                "}\n",
16996                AllmanBraceStyle);
16997 
16998   verifyFormat("void f()\n"
16999                "{\n"
17000                "  for (int i = 0; i < 10; ++i)\n"
17001                "  {\n"
17002                "    a();\n"
17003                "  }\n"
17004                "  while (false)\n"
17005                "  {\n"
17006                "    b();\n"
17007                "  }\n"
17008                "  do\n"
17009                "  {\n"
17010                "    c();\n"
17011                "  } while (false)\n"
17012                "}\n",
17013                AllmanBraceStyle);
17014 
17015   verifyFormat("void f(int a)\n"
17016                "{\n"
17017                "  switch (a)\n"
17018                "  {\n"
17019                "  case 0:\n"
17020                "    break;\n"
17021                "  case 1:\n"
17022                "  {\n"
17023                "    break;\n"
17024                "  }\n"
17025                "  case 2:\n"
17026                "  {\n"
17027                "  }\n"
17028                "  break;\n"
17029                "  default:\n"
17030                "    break;\n"
17031                "  }\n"
17032                "}\n",
17033                AllmanBraceStyle);
17034 
17035   verifyFormat("enum X\n"
17036                "{\n"
17037                "  Y = 0,\n"
17038                "}\n",
17039                AllmanBraceStyle);
17040   verifyFormat("enum X\n"
17041                "{\n"
17042                "  Y = 0\n"
17043                "}\n",
17044                AllmanBraceStyle);
17045 
17046   verifyFormat("@interface BSApplicationController ()\n"
17047                "{\n"
17048                "@private\n"
17049                "  id _extraIvar;\n"
17050                "}\n"
17051                "@end\n",
17052                AllmanBraceStyle);
17053 
17054   verifyFormat("#ifdef _DEBUG\n"
17055                "int foo(int i = 0)\n"
17056                "#else\n"
17057                "int foo(int i = 5)\n"
17058                "#endif\n"
17059                "{\n"
17060                "  return i;\n"
17061                "}",
17062                AllmanBraceStyle);
17063 
17064   verifyFormat("void foo() {}\n"
17065                "void bar()\n"
17066                "#ifdef _DEBUG\n"
17067                "{\n"
17068                "  foo();\n"
17069                "}\n"
17070                "#else\n"
17071                "{\n"
17072                "}\n"
17073                "#endif",
17074                AllmanBraceStyle);
17075 
17076   verifyFormat("void foobar() { int i = 5; }\n"
17077                "#ifdef _DEBUG\n"
17078                "void bar() {}\n"
17079                "#else\n"
17080                "void bar() { foobar(); }\n"
17081                "#endif",
17082                AllmanBraceStyle);
17083 
17084   EXPECT_EQ(AllmanBraceStyle.AllowShortLambdasOnASingleLine,
17085             FormatStyle::SLS_All);
17086 
17087   verifyFormat("[](int i) { return i + 2; };\n"
17088                "[](int i, int j)\n"
17089                "{\n"
17090                "  auto x = i + j;\n"
17091                "  auto y = i * j;\n"
17092                "  return x ^ y;\n"
17093                "};\n"
17094                "void foo()\n"
17095                "{\n"
17096                "  auto shortLambda = [](int i) { return i + 2; };\n"
17097                "  auto longLambda = [](int i, int j)\n"
17098                "  {\n"
17099                "    auto x = i + j;\n"
17100                "    auto y = i * j;\n"
17101                "    return x ^ y;\n"
17102                "  };\n"
17103                "}",
17104                AllmanBraceStyle);
17105 
17106   AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
17107 
17108   verifyFormat("[](int i)\n"
17109                "{\n"
17110                "  return i + 2;\n"
17111                "};\n"
17112                "[](int i, int j)\n"
17113                "{\n"
17114                "  auto x = i + j;\n"
17115                "  auto y = i * j;\n"
17116                "  return x ^ y;\n"
17117                "};\n"
17118                "void foo()\n"
17119                "{\n"
17120                "  auto shortLambda = [](int i)\n"
17121                "  {\n"
17122                "    return i + 2;\n"
17123                "  };\n"
17124                "  auto longLambda = [](int i, int j)\n"
17125                "  {\n"
17126                "    auto x = i + j;\n"
17127                "    auto y = i * j;\n"
17128                "    return x ^ y;\n"
17129                "  };\n"
17130                "}",
17131                AllmanBraceStyle);
17132 
17133   // Reset
17134   AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_All;
17135 
17136   // This shouldn't affect ObjC blocks..
17137   verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
17138                "  // ...\n"
17139                "  int i;\n"
17140                "}];",
17141                AllmanBraceStyle);
17142   verifyFormat("void (^block)(void) = ^{\n"
17143                "  // ...\n"
17144                "  int i;\n"
17145                "};",
17146                AllmanBraceStyle);
17147   // .. or dict literals.
17148   verifyFormat("void f()\n"
17149                "{\n"
17150                "  // ...\n"
17151                "  [object someMethod:@{@\"a\" : @\"b\"}];\n"
17152                "}",
17153                AllmanBraceStyle);
17154   verifyFormat("void f()\n"
17155                "{\n"
17156                "  // ...\n"
17157                "  [object someMethod:@{a : @\"b\"}];\n"
17158                "}",
17159                AllmanBraceStyle);
17160   verifyFormat("int f()\n"
17161                "{ // comment\n"
17162                "  return 42;\n"
17163                "}",
17164                AllmanBraceStyle);
17165 
17166   AllmanBraceStyle.ColumnLimit = 19;
17167   verifyFormat("void f() { int i; }", AllmanBraceStyle);
17168   AllmanBraceStyle.ColumnLimit = 18;
17169   verifyFormat("void f()\n"
17170                "{\n"
17171                "  int i;\n"
17172                "}",
17173                AllmanBraceStyle);
17174   AllmanBraceStyle.ColumnLimit = 80;
17175 
17176   FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle;
17177   BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine =
17178       FormatStyle::SIS_WithoutElse;
17179   BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
17180   verifyFormat("void f(bool b)\n"
17181                "{\n"
17182                "  if (b)\n"
17183                "  {\n"
17184                "    return;\n"
17185                "  }\n"
17186                "}\n",
17187                BreakBeforeBraceShortIfs);
17188   verifyFormat("void f(bool b)\n"
17189                "{\n"
17190                "  if constexpr (b)\n"
17191                "  {\n"
17192                "    return;\n"
17193                "  }\n"
17194                "}\n",
17195                BreakBeforeBraceShortIfs);
17196   verifyFormat("void f(bool b)\n"
17197                "{\n"
17198                "  if CONSTEXPR (b)\n"
17199                "  {\n"
17200                "    return;\n"
17201                "  }\n"
17202                "}\n",
17203                BreakBeforeBraceShortIfs);
17204   verifyFormat("void f(bool b)\n"
17205                "{\n"
17206                "  if (b) return;\n"
17207                "}\n",
17208                BreakBeforeBraceShortIfs);
17209   verifyFormat("void f(bool b)\n"
17210                "{\n"
17211                "  if constexpr (b) return;\n"
17212                "}\n",
17213                BreakBeforeBraceShortIfs);
17214   verifyFormat("void f(bool b)\n"
17215                "{\n"
17216                "  if CONSTEXPR (b) return;\n"
17217                "}\n",
17218                BreakBeforeBraceShortIfs);
17219   verifyFormat("void f(bool b)\n"
17220                "{\n"
17221                "  while (b)\n"
17222                "  {\n"
17223                "    return;\n"
17224                "  }\n"
17225                "}\n",
17226                BreakBeforeBraceShortIfs);
17227 }
17228 
17229 TEST_F(FormatTest, WhitesmithsBraceBreaking) {
17230   FormatStyle WhitesmithsBraceStyle = getLLVMStyle();
17231   WhitesmithsBraceStyle.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
17232 
17233   // Make a few changes to the style for testing purposes
17234   WhitesmithsBraceStyle.AllowShortFunctionsOnASingleLine =
17235       FormatStyle::SFS_Empty;
17236   WhitesmithsBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
17237   WhitesmithsBraceStyle.ColumnLimit = 0;
17238 
17239   // FIXME: this test case can't decide whether there should be a blank line
17240   // after the ~D() line or not. It adds one if one doesn't exist in the test
17241   // and it removes the line if one exists.
17242   /*
17243   verifyFormat("class A;\n"
17244                "namespace B\n"
17245                "  {\n"
17246                "class C;\n"
17247                "// Comment\n"
17248                "class D\n"
17249                "  {\n"
17250                "public:\n"
17251                "  D();\n"
17252                "  ~D() {}\n"
17253                "private:\n"
17254                "  enum E\n"
17255                "    {\n"
17256                "    F\n"
17257                "    }\n"
17258                "  };\n"
17259                "  } // namespace B\n",
17260                WhitesmithsBraceStyle);
17261   */
17262 
17263   WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_None;
17264   verifyFormat("namespace a\n"
17265                "  {\n"
17266                "class A\n"
17267                "  {\n"
17268                "  void f()\n"
17269                "    {\n"
17270                "    if (true)\n"
17271                "      {\n"
17272                "      a();\n"
17273                "      b();\n"
17274                "      }\n"
17275                "    }\n"
17276                "  void g()\n"
17277                "    {\n"
17278                "    return;\n"
17279                "    }\n"
17280                "  };\n"
17281                "struct B\n"
17282                "  {\n"
17283                "  int x;\n"
17284                "  };\n"
17285                "  } // namespace a",
17286                WhitesmithsBraceStyle);
17287 
17288   verifyFormat("namespace a\n"
17289                "  {\n"
17290                "namespace b\n"
17291                "  {\n"
17292                "class A\n"
17293                "  {\n"
17294                "  void f()\n"
17295                "    {\n"
17296                "    if (true)\n"
17297                "      {\n"
17298                "      a();\n"
17299                "      b();\n"
17300                "      }\n"
17301                "    }\n"
17302                "  void g()\n"
17303                "    {\n"
17304                "    return;\n"
17305                "    }\n"
17306                "  };\n"
17307                "struct B\n"
17308                "  {\n"
17309                "  int x;\n"
17310                "  };\n"
17311                "  } // namespace b\n"
17312                "  } // namespace a",
17313                WhitesmithsBraceStyle);
17314 
17315   WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_Inner;
17316   verifyFormat("namespace a\n"
17317                "  {\n"
17318                "namespace b\n"
17319                "  {\n"
17320                "  class A\n"
17321                "    {\n"
17322                "    void f()\n"
17323                "      {\n"
17324                "      if (true)\n"
17325                "        {\n"
17326                "        a();\n"
17327                "        b();\n"
17328                "        }\n"
17329                "      }\n"
17330                "    void g()\n"
17331                "      {\n"
17332                "      return;\n"
17333                "      }\n"
17334                "    };\n"
17335                "  struct B\n"
17336                "    {\n"
17337                "    int x;\n"
17338                "    };\n"
17339                "  } // namespace b\n"
17340                "  } // namespace a",
17341                WhitesmithsBraceStyle);
17342 
17343   WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_All;
17344   verifyFormat("namespace a\n"
17345                "  {\n"
17346                "  namespace b\n"
17347                "    {\n"
17348                "    class A\n"
17349                "      {\n"
17350                "      void f()\n"
17351                "        {\n"
17352                "        if (true)\n"
17353                "          {\n"
17354                "          a();\n"
17355                "          b();\n"
17356                "          }\n"
17357                "        }\n"
17358                "      void g()\n"
17359                "        {\n"
17360                "        return;\n"
17361                "        }\n"
17362                "      };\n"
17363                "    struct B\n"
17364                "      {\n"
17365                "      int x;\n"
17366                "      };\n"
17367                "    } // namespace b\n"
17368                "  }   // namespace a",
17369                WhitesmithsBraceStyle);
17370 
17371   verifyFormat("void f()\n"
17372                "  {\n"
17373                "  if (true)\n"
17374                "    {\n"
17375                "    a();\n"
17376                "    }\n"
17377                "  else if (false)\n"
17378                "    {\n"
17379                "    b();\n"
17380                "    }\n"
17381                "  else\n"
17382                "    {\n"
17383                "    c();\n"
17384                "    }\n"
17385                "  }\n",
17386                WhitesmithsBraceStyle);
17387 
17388   verifyFormat("void f()\n"
17389                "  {\n"
17390                "  for (int i = 0; i < 10; ++i)\n"
17391                "    {\n"
17392                "    a();\n"
17393                "    }\n"
17394                "  while (false)\n"
17395                "    {\n"
17396                "    b();\n"
17397                "    }\n"
17398                "  do\n"
17399                "    {\n"
17400                "    c();\n"
17401                "    } while (false)\n"
17402                "  }\n",
17403                WhitesmithsBraceStyle);
17404 
17405   WhitesmithsBraceStyle.IndentCaseLabels = true;
17406   verifyFormat("void switchTest1(int a)\n"
17407                "  {\n"
17408                "  switch (a)\n"
17409                "    {\n"
17410                "    case 2:\n"
17411                "      {\n"
17412                "      }\n"
17413                "      break;\n"
17414                "    }\n"
17415                "  }\n",
17416                WhitesmithsBraceStyle);
17417 
17418   verifyFormat("void switchTest2(int a)\n"
17419                "  {\n"
17420                "  switch (a)\n"
17421                "    {\n"
17422                "    case 0:\n"
17423                "      break;\n"
17424                "    case 1:\n"
17425                "      {\n"
17426                "      break;\n"
17427                "      }\n"
17428                "    case 2:\n"
17429                "      {\n"
17430                "      }\n"
17431                "      break;\n"
17432                "    default:\n"
17433                "      break;\n"
17434                "    }\n"
17435                "  }\n",
17436                WhitesmithsBraceStyle);
17437 
17438   verifyFormat("void switchTest3(int a)\n"
17439                "  {\n"
17440                "  switch (a)\n"
17441                "    {\n"
17442                "    case 0:\n"
17443                "      {\n"
17444                "      foo(x);\n"
17445                "      }\n"
17446                "      break;\n"
17447                "    default:\n"
17448                "      {\n"
17449                "      foo(1);\n"
17450                "      }\n"
17451                "      break;\n"
17452                "    }\n"
17453                "  }\n",
17454                WhitesmithsBraceStyle);
17455 
17456   WhitesmithsBraceStyle.IndentCaseLabels = false;
17457 
17458   verifyFormat("void switchTest4(int a)\n"
17459                "  {\n"
17460                "  switch (a)\n"
17461                "    {\n"
17462                "  case 2:\n"
17463                "    {\n"
17464                "    }\n"
17465                "    break;\n"
17466                "    }\n"
17467                "  }\n",
17468                WhitesmithsBraceStyle);
17469 
17470   verifyFormat("void switchTest5(int a)\n"
17471                "  {\n"
17472                "  switch (a)\n"
17473                "    {\n"
17474                "  case 0:\n"
17475                "    break;\n"
17476                "  case 1:\n"
17477                "    {\n"
17478                "    foo();\n"
17479                "    break;\n"
17480                "    }\n"
17481                "  case 2:\n"
17482                "    {\n"
17483                "    }\n"
17484                "    break;\n"
17485                "  default:\n"
17486                "    break;\n"
17487                "    }\n"
17488                "  }\n",
17489                WhitesmithsBraceStyle);
17490 
17491   verifyFormat("void switchTest6(int a)\n"
17492                "  {\n"
17493                "  switch (a)\n"
17494                "    {\n"
17495                "  case 0:\n"
17496                "    {\n"
17497                "    foo(x);\n"
17498                "    }\n"
17499                "    break;\n"
17500                "  default:\n"
17501                "    {\n"
17502                "    foo(1);\n"
17503                "    }\n"
17504                "    break;\n"
17505                "    }\n"
17506                "  }\n",
17507                WhitesmithsBraceStyle);
17508 
17509   verifyFormat("enum X\n"
17510                "  {\n"
17511                "  Y = 0, // testing\n"
17512                "  }\n",
17513                WhitesmithsBraceStyle);
17514 
17515   verifyFormat("enum X\n"
17516                "  {\n"
17517                "  Y = 0\n"
17518                "  }\n",
17519                WhitesmithsBraceStyle);
17520   verifyFormat("enum X\n"
17521                "  {\n"
17522                "  Y = 0,\n"
17523                "  Z = 1\n"
17524                "  };\n",
17525                WhitesmithsBraceStyle);
17526 
17527   verifyFormat("@interface BSApplicationController ()\n"
17528                "  {\n"
17529                "@private\n"
17530                "  id _extraIvar;\n"
17531                "  }\n"
17532                "@end\n",
17533                WhitesmithsBraceStyle);
17534 
17535   verifyFormat("#ifdef _DEBUG\n"
17536                "int foo(int i = 0)\n"
17537                "#else\n"
17538                "int foo(int i = 5)\n"
17539                "#endif\n"
17540                "  {\n"
17541                "  return i;\n"
17542                "  }",
17543                WhitesmithsBraceStyle);
17544 
17545   verifyFormat("void foo() {}\n"
17546                "void bar()\n"
17547                "#ifdef _DEBUG\n"
17548                "  {\n"
17549                "  foo();\n"
17550                "  }\n"
17551                "#else\n"
17552                "  {\n"
17553                "  }\n"
17554                "#endif",
17555                WhitesmithsBraceStyle);
17556 
17557   verifyFormat("void foobar()\n"
17558                "  {\n"
17559                "  int i = 5;\n"
17560                "  }\n"
17561                "#ifdef _DEBUG\n"
17562                "void bar()\n"
17563                "  {\n"
17564                "  }\n"
17565                "#else\n"
17566                "void bar()\n"
17567                "  {\n"
17568                "  foobar();\n"
17569                "  }\n"
17570                "#endif",
17571                WhitesmithsBraceStyle);
17572 
17573   // This shouldn't affect ObjC blocks..
17574   verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
17575                "  // ...\n"
17576                "  int i;\n"
17577                "}];",
17578                WhitesmithsBraceStyle);
17579   verifyFormat("void (^block)(void) = ^{\n"
17580                "  // ...\n"
17581                "  int i;\n"
17582                "};",
17583                WhitesmithsBraceStyle);
17584   // .. or dict literals.
17585   verifyFormat("void f()\n"
17586                "  {\n"
17587                "  [object someMethod:@{@\"a\" : @\"b\"}];\n"
17588                "  }",
17589                WhitesmithsBraceStyle);
17590 
17591   verifyFormat("int f()\n"
17592                "  { // comment\n"
17593                "  return 42;\n"
17594                "  }",
17595                WhitesmithsBraceStyle);
17596 
17597   FormatStyle BreakBeforeBraceShortIfs = WhitesmithsBraceStyle;
17598   BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine =
17599       FormatStyle::SIS_OnlyFirstIf;
17600   BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
17601   verifyFormat("void f(bool b)\n"
17602                "  {\n"
17603                "  if (b)\n"
17604                "    {\n"
17605                "    return;\n"
17606                "    }\n"
17607                "  }\n",
17608                BreakBeforeBraceShortIfs);
17609   verifyFormat("void f(bool b)\n"
17610                "  {\n"
17611                "  if (b) return;\n"
17612                "  }\n",
17613                BreakBeforeBraceShortIfs);
17614   verifyFormat("void f(bool b)\n"
17615                "  {\n"
17616                "  while (b)\n"
17617                "    {\n"
17618                "    return;\n"
17619                "    }\n"
17620                "  }\n",
17621                BreakBeforeBraceShortIfs);
17622 }
17623 
17624 TEST_F(FormatTest, GNUBraceBreaking) {
17625   FormatStyle GNUBraceStyle = getLLVMStyle();
17626   GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
17627   verifyFormat("namespace a\n"
17628                "{\n"
17629                "class A\n"
17630                "{\n"
17631                "  void f()\n"
17632                "  {\n"
17633                "    int a;\n"
17634                "    {\n"
17635                "      int b;\n"
17636                "    }\n"
17637                "    if (true)\n"
17638                "      {\n"
17639                "        a();\n"
17640                "        b();\n"
17641                "      }\n"
17642                "  }\n"
17643                "  void g() { return; }\n"
17644                "}\n"
17645                "} // namespace a",
17646                GNUBraceStyle);
17647 
17648   verifyFormat("void f()\n"
17649                "{\n"
17650                "  if (true)\n"
17651                "    {\n"
17652                "      a();\n"
17653                "    }\n"
17654                "  else if (false)\n"
17655                "    {\n"
17656                "      b();\n"
17657                "    }\n"
17658                "  else\n"
17659                "    {\n"
17660                "      c();\n"
17661                "    }\n"
17662                "}\n",
17663                GNUBraceStyle);
17664 
17665   verifyFormat("void f()\n"
17666                "{\n"
17667                "  for (int i = 0; i < 10; ++i)\n"
17668                "    {\n"
17669                "      a();\n"
17670                "    }\n"
17671                "  while (false)\n"
17672                "    {\n"
17673                "      b();\n"
17674                "    }\n"
17675                "  do\n"
17676                "    {\n"
17677                "      c();\n"
17678                "    }\n"
17679                "  while (false);\n"
17680                "}\n",
17681                GNUBraceStyle);
17682 
17683   verifyFormat("void f(int a)\n"
17684                "{\n"
17685                "  switch (a)\n"
17686                "    {\n"
17687                "    case 0:\n"
17688                "      break;\n"
17689                "    case 1:\n"
17690                "      {\n"
17691                "        break;\n"
17692                "      }\n"
17693                "    case 2:\n"
17694                "      {\n"
17695                "      }\n"
17696                "      break;\n"
17697                "    default:\n"
17698                "      break;\n"
17699                "    }\n"
17700                "}\n",
17701                GNUBraceStyle);
17702 
17703   verifyFormat("enum X\n"
17704                "{\n"
17705                "  Y = 0,\n"
17706                "}\n",
17707                GNUBraceStyle);
17708 
17709   verifyFormat("@interface BSApplicationController ()\n"
17710                "{\n"
17711                "@private\n"
17712                "  id _extraIvar;\n"
17713                "}\n"
17714                "@end\n",
17715                GNUBraceStyle);
17716 
17717   verifyFormat("#ifdef _DEBUG\n"
17718                "int foo(int i = 0)\n"
17719                "#else\n"
17720                "int foo(int i = 5)\n"
17721                "#endif\n"
17722                "{\n"
17723                "  return i;\n"
17724                "}",
17725                GNUBraceStyle);
17726 
17727   verifyFormat("void foo() {}\n"
17728                "void bar()\n"
17729                "#ifdef _DEBUG\n"
17730                "{\n"
17731                "  foo();\n"
17732                "}\n"
17733                "#else\n"
17734                "{\n"
17735                "}\n"
17736                "#endif",
17737                GNUBraceStyle);
17738 
17739   verifyFormat("void foobar() { int i = 5; }\n"
17740                "#ifdef _DEBUG\n"
17741                "void bar() {}\n"
17742                "#else\n"
17743                "void bar() { foobar(); }\n"
17744                "#endif",
17745                GNUBraceStyle);
17746 }
17747 
17748 TEST_F(FormatTest, WebKitBraceBreaking) {
17749   FormatStyle WebKitBraceStyle = getLLVMStyle();
17750   WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit;
17751   WebKitBraceStyle.FixNamespaceComments = false;
17752   verifyFormat("namespace a {\n"
17753                "class A {\n"
17754                "  void f()\n"
17755                "  {\n"
17756                "    if (true) {\n"
17757                "      a();\n"
17758                "      b();\n"
17759                "    }\n"
17760                "  }\n"
17761                "  void g() { return; }\n"
17762                "};\n"
17763                "enum E {\n"
17764                "  A,\n"
17765                "  // foo\n"
17766                "  B,\n"
17767                "  C\n"
17768                "};\n"
17769                "struct B {\n"
17770                "  int x;\n"
17771                "};\n"
17772                "}\n",
17773                WebKitBraceStyle);
17774   verifyFormat("struct S {\n"
17775                "  int Type;\n"
17776                "  union {\n"
17777                "    int x;\n"
17778                "    double y;\n"
17779                "  } Value;\n"
17780                "  class C {\n"
17781                "    MyFavoriteType Value;\n"
17782                "  } Class;\n"
17783                "};\n",
17784                WebKitBraceStyle);
17785 }
17786 
17787 TEST_F(FormatTest, CatchExceptionReferenceBinding) {
17788   verifyFormat("void f() {\n"
17789                "  try {\n"
17790                "  } catch (const Exception &e) {\n"
17791                "  }\n"
17792                "}\n",
17793                getLLVMStyle());
17794 }
17795 
17796 TEST_F(FormatTest, CatchAlignArrayOfStructuresRightAlignment) {
17797   auto Style = getLLVMStyle();
17798   Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
17799   Style.AlignConsecutiveAssignments =
17800       FormatStyle::AlignConsecutiveStyle::ACS_Consecutive;
17801   Style.AlignConsecutiveDeclarations =
17802       FormatStyle::AlignConsecutiveStyle::ACS_Consecutive;
17803   verifyFormat("struct test demo[] = {\n"
17804                "    {56,    23, \"hello\"},\n"
17805                "    {-1, 93463, \"world\"},\n"
17806                "    { 7,     5,    \"!!\"}\n"
17807                "};\n",
17808                Style);
17809 
17810   verifyFormat("struct test demo[] = {\n"
17811                "    {56,    23, \"hello\"}, // first line\n"
17812                "    {-1, 93463, \"world\"}, // second line\n"
17813                "    { 7,     5,    \"!!\"}  // third line\n"
17814                "};\n",
17815                Style);
17816 
17817   verifyFormat("struct test demo[4] = {\n"
17818                "    { 56,    23, 21,       \"oh\"}, // first line\n"
17819                "    { -1, 93463, 22,       \"my\"}, // second line\n"
17820                "    {  7,     5,  1, \"goodness\"}  // third line\n"
17821                "    {234,     5,  1, \"gracious\"}  // fourth line\n"
17822                "};\n",
17823                Style);
17824 
17825   verifyFormat("struct test demo[3] = {\n"
17826                "    {56,    23, \"hello\"},\n"
17827                "    {-1, 93463, \"world\"},\n"
17828                "    { 7,     5,    \"!!\"}\n"
17829                "};\n",
17830                Style);
17831 
17832   verifyFormat("struct test demo[3] = {\n"
17833                "    {int{56},    23, \"hello\"},\n"
17834                "    {int{-1}, 93463, \"world\"},\n"
17835                "    { int{7},     5,    \"!!\"}\n"
17836                "};\n",
17837                Style);
17838 
17839   verifyFormat("struct test demo[] = {\n"
17840                "    {56,    23, \"hello\"},\n"
17841                "    {-1, 93463, \"world\"},\n"
17842                "    { 7,     5,    \"!!\"},\n"
17843                "};\n",
17844                Style);
17845 
17846   verifyFormat("test demo[] = {\n"
17847                "    {56,    23, \"hello\"},\n"
17848                "    {-1, 93463, \"world\"},\n"
17849                "    { 7,     5,    \"!!\"},\n"
17850                "};\n",
17851                Style);
17852 
17853   verifyFormat("demo = std::array<struct test, 3>{\n"
17854                "    test{56,    23, \"hello\"},\n"
17855                "    test{-1, 93463, \"world\"},\n"
17856                "    test{ 7,     5,    \"!!\"},\n"
17857                "};\n",
17858                Style);
17859 
17860   verifyFormat("test demo[] = {\n"
17861                "    {56,    23, \"hello\"},\n"
17862                "#if X\n"
17863                "    {-1, 93463, \"world\"},\n"
17864                "#endif\n"
17865                "    { 7,     5,    \"!!\"}\n"
17866                "};\n",
17867                Style);
17868 
17869   verifyFormat(
17870       "test demo[] = {\n"
17871       "    { 7,    23,\n"
17872       "     \"hello world i am a very long line that really, in any\"\n"
17873       "     \"just world, ought to be split over multiple lines\"},\n"
17874       "    {-1, 93463,                                  \"world\"},\n"
17875       "    {56,     5,                                     \"!!\"}\n"
17876       "};\n",
17877       Style);
17878 
17879   verifyFormat("return GradForUnaryCwise(g, {\n"
17880                "                                {{\"sign\"}, \"Sign\",  "
17881                "  {\"x\", \"dy\"}},\n"
17882                "                                {  {\"dx\"},  \"Mul\", {\"dy\""
17883                ", \"sign\"}},\n"
17884                "});\n",
17885                Style);
17886 
17887   Style.ColumnLimit = 0;
17888   EXPECT_EQ(
17889       "test demo[] = {\n"
17890       "    {56,    23, \"hello world i am a very long line that really, "
17891       "in any just world, ought to be split over multiple lines\"},\n"
17892       "    {-1, 93463,                                                  "
17893       "                                                 \"world\"},\n"
17894       "    { 7,     5,                                                  "
17895       "                                                    \"!!\"},\n"
17896       "};",
17897       format("test demo[] = {{56, 23, \"hello world i am a very long line "
17898              "that really, in any just world, ought to be split over multiple "
17899              "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
17900              Style));
17901 
17902   Style.ColumnLimit = 80;
17903   verifyFormat("test demo[] = {\n"
17904                "    {56,    23, /* a comment */ \"hello\"},\n"
17905                "    {-1, 93463,                 \"world\"},\n"
17906                "    { 7,     5,                    \"!!\"}\n"
17907                "};\n",
17908                Style);
17909 
17910   verifyFormat("test demo[] = {\n"
17911                "    {56,    23,                    \"hello\"},\n"
17912                "    {-1, 93463, \"world\" /* comment here */},\n"
17913                "    { 7,     5,                       \"!!\"}\n"
17914                "};\n",
17915                Style);
17916 
17917   verifyFormat("test demo[] = {\n"
17918                "    {56, /* a comment */ 23, \"hello\"},\n"
17919                "    {-1,              93463, \"world\"},\n"
17920                "    { 7,                  5,    \"!!\"}\n"
17921                "};\n",
17922                Style);
17923 
17924   Style.ColumnLimit = 20;
17925   EXPECT_EQ(
17926       "demo = std::array<\n"
17927       "    struct test, 3>{\n"
17928       "    test{\n"
17929       "         56,    23,\n"
17930       "         \"hello \"\n"
17931       "         \"world i \"\n"
17932       "         \"am a very \"\n"
17933       "         \"long line \"\n"
17934       "         \"that \"\n"
17935       "         \"really, \"\n"
17936       "         \"in any \"\n"
17937       "         \"just \"\n"
17938       "         \"world, \"\n"
17939       "         \"ought to \"\n"
17940       "         \"be split \"\n"
17941       "         \"over \"\n"
17942       "         \"multiple \"\n"
17943       "         \"lines\"},\n"
17944       "    test{-1, 93463,\n"
17945       "         \"world\"},\n"
17946       "    test{ 7,     5,\n"
17947       "         \"!!\"   },\n"
17948       "};",
17949       format("demo = std::array<struct test, 3>{test{56, 23, \"hello world "
17950              "i am a very long line that really, in any just world, ought "
17951              "to be split over multiple lines\"},test{-1, 93463, \"world\"},"
17952              "test{7, 5, \"!!\"},};",
17953              Style));
17954   // This caused a core dump by enabling Alignment in the LLVMStyle globally
17955   Style = getLLVMStyleWithColumns(50);
17956   Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
17957   verifyFormat("static A x = {\n"
17958                "    {{init1, init2, init3, init4},\n"
17959                "     {init1, init2, init3, init4}}\n"
17960                "};",
17961                Style);
17962   Style.ColumnLimit = 100;
17963   EXPECT_EQ(
17964       "test demo[] = {\n"
17965       "    {56,    23,\n"
17966       "     \"hello world i am a very long line that really, in any just world"
17967       ", ought to be split over \"\n"
17968       "     \"multiple lines\"  },\n"
17969       "    {-1, 93463, \"world\"},\n"
17970       "    { 7,     5,    \"!!\"},\n"
17971       "};",
17972       format("test demo[] = {{56, 23, \"hello world i am a very long line "
17973              "that really, in any just world, ought to be split over multiple "
17974              "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
17975              Style));
17976 
17977   Style = getLLVMStyleWithColumns(50);
17978   Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
17979   Style.AlignConsecutiveAssignments =
17980       FormatStyle::AlignConsecutiveStyle::ACS_Consecutive;
17981   Style.AlignConsecutiveDeclarations =
17982       FormatStyle::AlignConsecutiveStyle::ACS_Consecutive;
17983   verifyFormat("struct test demo[] = {\n"
17984                "    {56,    23, \"hello\"},\n"
17985                "    {-1, 93463, \"world\"},\n"
17986                "    { 7,     5,    \"!!\"}\n"
17987                "};\n"
17988                "static A x = {\n"
17989                "    {{init1, init2, init3, init4},\n"
17990                "     {init1, init2, init3, init4}}\n"
17991                "};",
17992                Style);
17993   Style.ColumnLimit = 100;
17994   Style.AlignConsecutiveAssignments =
17995       FormatStyle::AlignConsecutiveStyle::ACS_AcrossComments;
17996   Style.AlignConsecutiveDeclarations =
17997       FormatStyle::AlignConsecutiveStyle::ACS_AcrossComments;
17998   verifyFormat("struct test demo[] = {\n"
17999                "    {56,    23, \"hello\"},\n"
18000                "    {-1, 93463, \"world\"},\n"
18001                "    { 7,     5,    \"!!\"}\n"
18002                "};\n"
18003                "struct test demo[4] = {\n"
18004                "    { 56,    23, 21,       \"oh\"}, // first line\n"
18005                "    { -1, 93463, 22,       \"my\"}, // second line\n"
18006                "    {  7,     5,  1, \"goodness\"}  // third line\n"
18007                "    {234,     5,  1, \"gracious\"}  // fourth line\n"
18008                "};\n",
18009                Style);
18010   EXPECT_EQ(
18011       "test demo[] = {\n"
18012       "    {56,\n"
18013       "     \"hello world i am a very long line that really, in any just world"
18014       ", ought to be split over \"\n"
18015       "     \"multiple lines\",    23},\n"
18016       "    {-1,      \"world\", 93463},\n"
18017       "    { 7,         \"!!\",     5},\n"
18018       "};",
18019       format("test demo[] = {{56, \"hello world i am a very long line "
18020              "that really, in any just world, ought to be split over multiple "
18021              "lines\", 23},{-1, \"world\", 93463},{7, \"!!\", 5},};",
18022              Style));
18023 }
18024 
18025 TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) {
18026   auto Style = getLLVMStyle();
18027   Style.AlignArrayOfStructures = FormatStyle::AIAS_Left;
18028   /* FIXME: This case gets misformatted.
18029   verifyFormat("auto foo = Items{\n"
18030                "    Section{0, bar(), },\n"
18031                "    Section{1, boo()  }\n"
18032                "};\n",
18033                Style);
18034   */
18035   verifyFormat("auto foo = Items{\n"
18036                "    Section{\n"
18037                "            0, bar(),\n"
18038                "            }\n"
18039                "};\n",
18040                Style);
18041   verifyFormat("struct test demo[] = {\n"
18042                "    {56, 23,    \"hello\"},\n"
18043                "    {-1, 93463, \"world\"},\n"
18044                "    {7,  5,     \"!!\"   }\n"
18045                "};\n",
18046                Style);
18047   verifyFormat("struct test demo[] = {\n"
18048                "    {56, 23,    \"hello\"}, // first line\n"
18049                "    {-1, 93463, \"world\"}, // second line\n"
18050                "    {7,  5,     \"!!\"   }  // third line\n"
18051                "};\n",
18052                Style);
18053   verifyFormat("struct test demo[4] = {\n"
18054                "    {56,  23,    21, \"oh\"      }, // first line\n"
18055                "    {-1,  93463, 22, \"my\"      }, // second line\n"
18056                "    {7,   5,     1,  \"goodness\"}  // third line\n"
18057                "    {234, 5,     1,  \"gracious\"}  // fourth line\n"
18058                "};\n",
18059                Style);
18060   verifyFormat("struct test demo[3] = {\n"
18061                "    {56, 23,    \"hello\"},\n"
18062                "    {-1, 93463, \"world\"},\n"
18063                "    {7,  5,     \"!!\"   }\n"
18064                "};\n",
18065                Style);
18066 
18067   verifyFormat("struct test demo[3] = {\n"
18068                "    {int{56}, 23,    \"hello\"},\n"
18069                "    {int{-1}, 93463, \"world\"},\n"
18070                "    {int{7},  5,     \"!!\"   }\n"
18071                "};\n",
18072                Style);
18073   verifyFormat("struct test demo[] = {\n"
18074                "    {56, 23,    \"hello\"},\n"
18075                "    {-1, 93463, \"world\"},\n"
18076                "    {7,  5,     \"!!\"   },\n"
18077                "};\n",
18078                Style);
18079   verifyFormat("test demo[] = {\n"
18080                "    {56, 23,    \"hello\"},\n"
18081                "    {-1, 93463, \"world\"},\n"
18082                "    {7,  5,     \"!!\"   },\n"
18083                "};\n",
18084                Style);
18085   verifyFormat("demo = std::array<struct test, 3>{\n"
18086                "    test{56, 23,    \"hello\"},\n"
18087                "    test{-1, 93463, \"world\"},\n"
18088                "    test{7,  5,     \"!!\"   },\n"
18089                "};\n",
18090                Style);
18091   verifyFormat("test demo[] = {\n"
18092                "    {56, 23,    \"hello\"},\n"
18093                "#if X\n"
18094                "    {-1, 93463, \"world\"},\n"
18095                "#endif\n"
18096                "    {7,  5,     \"!!\"   }\n"
18097                "};\n",
18098                Style);
18099   verifyFormat(
18100       "test demo[] = {\n"
18101       "    {7,  23,\n"
18102       "     \"hello world i am a very long line that really, in any\"\n"
18103       "     \"just world, ought to be split over multiple lines\"},\n"
18104       "    {-1, 93463, \"world\"                                 },\n"
18105       "    {56, 5,     \"!!\"                                    }\n"
18106       "};\n",
18107       Style);
18108 
18109   verifyFormat("return GradForUnaryCwise(g, {\n"
18110                "                                {{\"sign\"}, \"Sign\", {\"x\", "
18111                "\"dy\"}   },\n"
18112                "                                {{\"dx\"},   \"Mul\",  "
18113                "{\"dy\", \"sign\"}},\n"
18114                "});\n",
18115                Style);
18116 
18117   Style.ColumnLimit = 0;
18118   EXPECT_EQ(
18119       "test demo[] = {\n"
18120       "    {56, 23,    \"hello world i am a very long line that really, in any "
18121       "just world, ought to be split over multiple lines\"},\n"
18122       "    {-1, 93463, \"world\"                                               "
18123       "                                                   },\n"
18124       "    {7,  5,     \"!!\"                                                  "
18125       "                                                   },\n"
18126       "};",
18127       format("test demo[] = {{56, 23, \"hello world i am a very long line "
18128              "that really, in any just world, ought to be split over multiple "
18129              "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
18130              Style));
18131 
18132   Style.ColumnLimit = 80;
18133   verifyFormat("test demo[] = {\n"
18134                "    {56, 23,    /* a comment */ \"hello\"},\n"
18135                "    {-1, 93463, \"world\"                },\n"
18136                "    {7,  5,     \"!!\"                   }\n"
18137                "};\n",
18138                Style);
18139 
18140   verifyFormat("test demo[] = {\n"
18141                "    {56, 23,    \"hello\"                   },\n"
18142                "    {-1, 93463, \"world\" /* comment here */},\n"
18143                "    {7,  5,     \"!!\"                      }\n"
18144                "};\n",
18145                Style);
18146 
18147   verifyFormat("test demo[] = {\n"
18148                "    {56, /* a comment */ 23, \"hello\"},\n"
18149                "    {-1, 93463,              \"world\"},\n"
18150                "    {7,  5,                  \"!!\"   }\n"
18151                "};\n",
18152                Style);
18153 
18154   Style.ColumnLimit = 20;
18155   EXPECT_EQ(
18156       "demo = std::array<\n"
18157       "    struct test, 3>{\n"
18158       "    test{\n"
18159       "         56, 23,\n"
18160       "         \"hello \"\n"
18161       "         \"world i \"\n"
18162       "         \"am a very \"\n"
18163       "         \"long line \"\n"
18164       "         \"that \"\n"
18165       "         \"really, \"\n"
18166       "         \"in any \"\n"
18167       "         \"just \"\n"
18168       "         \"world, \"\n"
18169       "         \"ought to \"\n"
18170       "         \"be split \"\n"
18171       "         \"over \"\n"
18172       "         \"multiple \"\n"
18173       "         \"lines\"},\n"
18174       "    test{-1, 93463,\n"
18175       "         \"world\"},\n"
18176       "    test{7,  5,\n"
18177       "         \"!!\"   },\n"
18178       "};",
18179       format("demo = std::array<struct test, 3>{test{56, 23, \"hello world "
18180              "i am a very long line that really, in any just world, ought "
18181              "to be split over multiple lines\"},test{-1, 93463, \"world\"},"
18182              "test{7, 5, \"!!\"},};",
18183              Style));
18184 
18185   // This caused a core dump by enabling Alignment in the LLVMStyle globally
18186   Style = getLLVMStyleWithColumns(50);
18187   Style.AlignArrayOfStructures = FormatStyle::AIAS_Left;
18188   verifyFormat("static A x = {\n"
18189                "    {{init1, init2, init3, init4},\n"
18190                "     {init1, init2, init3, init4}}\n"
18191                "};",
18192                Style);
18193   Style.ColumnLimit = 100;
18194   EXPECT_EQ(
18195       "test demo[] = {\n"
18196       "    {56, 23,\n"
18197       "     \"hello world i am a very long line that really, in any just world"
18198       ", ought to be split over \"\n"
18199       "     \"multiple lines\"  },\n"
18200       "    {-1, 93463, \"world\"},\n"
18201       "    {7,  5,     \"!!\"   },\n"
18202       "};",
18203       format("test demo[] = {{56, 23, \"hello world i am a very long line "
18204              "that really, in any just world, ought to be split over multiple "
18205              "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
18206              Style));
18207 }
18208 
18209 TEST_F(FormatTest, UnderstandsPragmas) {
18210   verifyFormat("#pragma omp reduction(| : var)");
18211   verifyFormat("#pragma omp reduction(+ : var)");
18212 
18213   EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string "
18214             "(including parentheses).",
18215             format("#pragma    mark   Any non-hyphenated or hyphenated string "
18216                    "(including parentheses)."));
18217 }
18218 
18219 TEST_F(FormatTest, UnderstandPragmaOption) {
18220   verifyFormat("#pragma option -C -A");
18221 
18222   EXPECT_EQ("#pragma option -C -A", format("#pragma    option   -C   -A"));
18223 }
18224 
18225 TEST_F(FormatTest, OptimizeBreakPenaltyVsExcess) {
18226   FormatStyle Style = getLLVMStyle();
18227   Style.ColumnLimit = 20;
18228 
18229   // See PR41213
18230   EXPECT_EQ("/*\n"
18231             " *\t9012345\n"
18232             " * /8901\n"
18233             " */",
18234             format("/*\n"
18235                    " *\t9012345 /8901\n"
18236                    " */",
18237                    Style));
18238   EXPECT_EQ("/*\n"
18239             " *345678\n"
18240             " *\t/8901\n"
18241             " */",
18242             format("/*\n"
18243                    " *345678\t/8901\n"
18244                    " */",
18245                    Style));
18246 
18247   verifyFormat("int a; // the\n"
18248                "       // comment",
18249                Style);
18250   EXPECT_EQ("int a; /* first line\n"
18251             "        * second\n"
18252             "        * line third\n"
18253             "        * line\n"
18254             "        */",
18255             format("int a; /* first line\n"
18256                    "        * second\n"
18257                    "        * line third\n"
18258                    "        * line\n"
18259                    "        */",
18260                    Style));
18261   EXPECT_EQ("int a; // first line\n"
18262             "       // second\n"
18263             "       // line third\n"
18264             "       // line",
18265             format("int a; // first line\n"
18266                    "       // second line\n"
18267                    "       // third line",
18268                    Style));
18269 
18270   Style.PenaltyExcessCharacter = 90;
18271   verifyFormat("int a; // the comment", Style);
18272   EXPECT_EQ("int a; // the comment\n"
18273             "       // aaa",
18274             format("int a; // the comment aaa", Style));
18275   EXPECT_EQ("int a; /* first line\n"
18276             "        * second line\n"
18277             "        * third line\n"
18278             "        */",
18279             format("int a; /* first line\n"
18280                    "        * second line\n"
18281                    "        * third line\n"
18282                    "        */",
18283                    Style));
18284   EXPECT_EQ("int a; // first line\n"
18285             "       // second line\n"
18286             "       // third line",
18287             format("int a; // first line\n"
18288                    "       // second line\n"
18289                    "       // third line",
18290                    Style));
18291   // FIXME: Investigate why this is not getting the same layout as the test
18292   // above.
18293   EXPECT_EQ("int a; /* first line\n"
18294             "        * second line\n"
18295             "        * third line\n"
18296             "        */",
18297             format("int a; /* first line second line third line"
18298                    "\n*/",
18299                    Style));
18300 
18301   EXPECT_EQ("// foo bar baz bazfoo\n"
18302             "// foo bar foo bar\n",
18303             format("// foo bar baz bazfoo\n"
18304                    "// foo bar foo           bar\n",
18305                    Style));
18306   EXPECT_EQ("// foo bar baz bazfoo\n"
18307             "// foo bar foo bar\n",
18308             format("// foo bar baz      bazfoo\n"
18309                    "// foo            bar foo bar\n",
18310                    Style));
18311 
18312   // FIXME: Optimally, we'd keep bazfoo on the first line and reflow bar to the
18313   // next one.
18314   EXPECT_EQ("// foo bar baz bazfoo\n"
18315             "// bar foo bar\n",
18316             format("// foo bar baz      bazfoo bar\n"
18317                    "// foo            bar\n",
18318                    Style));
18319 
18320   EXPECT_EQ("// foo bar baz bazfoo\n"
18321             "// foo bar baz bazfoo\n"
18322             "// bar foo bar\n",
18323             format("// foo bar baz      bazfoo\n"
18324                    "// foo bar baz      bazfoo bar\n"
18325                    "// foo bar\n",
18326                    Style));
18327 
18328   EXPECT_EQ("// foo bar baz bazfoo\n"
18329             "// foo bar baz bazfoo\n"
18330             "// bar foo bar\n",
18331             format("// foo bar baz      bazfoo\n"
18332                    "// foo bar baz      bazfoo bar\n"
18333                    "// foo           bar\n",
18334                    Style));
18335 
18336   // Make sure we do not keep protruding characters if strict mode reflow is
18337   // cheaper than keeping protruding characters.
18338   Style.ColumnLimit = 21;
18339   EXPECT_EQ(
18340       "// foo foo foo foo\n"
18341       "// foo foo foo foo\n"
18342       "// foo foo foo foo\n",
18343       format("// foo foo foo foo foo foo foo foo foo foo foo foo\n", Style));
18344 
18345   EXPECT_EQ("int a = /* long block\n"
18346             "           comment */\n"
18347             "    42;",
18348             format("int a = /* long block comment */ 42;", Style));
18349 }
18350 
18351 #define EXPECT_ALL_STYLES_EQUAL(Styles)                                        \
18352   for (size_t i = 1; i < Styles.size(); ++i)                                   \
18353   EXPECT_EQ(Styles[0], Styles[i])                                              \
18354       << "Style #" << i << " of " << Styles.size() << " differs from Style #0"
18355 
18356 TEST_F(FormatTest, GetsPredefinedStyleByName) {
18357   SmallVector<FormatStyle, 3> Styles;
18358   Styles.resize(3);
18359 
18360   Styles[0] = getLLVMStyle();
18361   EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1]));
18362   EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2]));
18363   EXPECT_ALL_STYLES_EQUAL(Styles);
18364 
18365   Styles[0] = getGoogleStyle();
18366   EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1]));
18367   EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2]));
18368   EXPECT_ALL_STYLES_EQUAL(Styles);
18369 
18370   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
18371   EXPECT_TRUE(
18372       getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1]));
18373   EXPECT_TRUE(
18374       getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2]));
18375   EXPECT_ALL_STYLES_EQUAL(Styles);
18376 
18377   Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp);
18378   EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1]));
18379   EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2]));
18380   EXPECT_ALL_STYLES_EQUAL(Styles);
18381 
18382   Styles[0] = getMozillaStyle();
18383   EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1]));
18384   EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2]));
18385   EXPECT_ALL_STYLES_EQUAL(Styles);
18386 
18387   Styles[0] = getWebKitStyle();
18388   EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1]));
18389   EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2]));
18390   EXPECT_ALL_STYLES_EQUAL(Styles);
18391 
18392   Styles[0] = getGNUStyle();
18393   EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1]));
18394   EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2]));
18395   EXPECT_ALL_STYLES_EQUAL(Styles);
18396 
18397   EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0]));
18398 }
18399 
18400 TEST_F(FormatTest, GetsCorrectBasedOnStyle) {
18401   SmallVector<FormatStyle, 8> Styles;
18402   Styles.resize(2);
18403 
18404   Styles[0] = getGoogleStyle();
18405   Styles[1] = getLLVMStyle();
18406   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
18407   EXPECT_ALL_STYLES_EQUAL(Styles);
18408 
18409   Styles.resize(5);
18410   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
18411   Styles[1] = getLLVMStyle();
18412   Styles[1].Language = FormatStyle::LK_JavaScript;
18413   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
18414 
18415   Styles[2] = getLLVMStyle();
18416   Styles[2].Language = FormatStyle::LK_JavaScript;
18417   EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n"
18418                                   "BasedOnStyle: Google",
18419                                   &Styles[2])
18420                    .value());
18421 
18422   Styles[3] = getLLVMStyle();
18423   Styles[3].Language = FormatStyle::LK_JavaScript;
18424   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n"
18425                                   "Language: JavaScript",
18426                                   &Styles[3])
18427                    .value());
18428 
18429   Styles[4] = getLLVMStyle();
18430   Styles[4].Language = FormatStyle::LK_JavaScript;
18431   EXPECT_EQ(0, parseConfiguration("---\n"
18432                                   "BasedOnStyle: LLVM\n"
18433                                   "IndentWidth: 123\n"
18434                                   "---\n"
18435                                   "BasedOnStyle: Google\n"
18436                                   "Language: JavaScript",
18437                                   &Styles[4])
18438                    .value());
18439   EXPECT_ALL_STYLES_EQUAL(Styles);
18440 }
18441 
18442 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME)                             \
18443   Style.FIELD = false;                                                         \
18444   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value());      \
18445   EXPECT_TRUE(Style.FIELD);                                                    \
18446   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value());     \
18447   EXPECT_FALSE(Style.FIELD);
18448 
18449 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD)
18450 
18451 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME)              \
18452   Style.STRUCT.FIELD = false;                                                  \
18453   EXPECT_EQ(0,                                                                 \
18454             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": true", &Style)   \
18455                 .value());                                                     \
18456   EXPECT_TRUE(Style.STRUCT.FIELD);                                             \
18457   EXPECT_EQ(0,                                                                 \
18458             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": false", &Style)  \
18459                 .value());                                                     \
18460   EXPECT_FALSE(Style.STRUCT.FIELD);
18461 
18462 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD)                                 \
18463   CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD)
18464 
18465 #define CHECK_PARSE(TEXT, FIELD, VALUE)                                        \
18466   EXPECT_NE(VALUE, Style.FIELD) << "Initial value already the same!";          \
18467   EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value());                      \
18468   EXPECT_EQ(VALUE, Style.FIELD) << "Unexpected value after parsing!"
18469 
18470 TEST_F(FormatTest, ParsesConfigurationBools) {
18471   FormatStyle Style = {};
18472   Style.Language = FormatStyle::LK_Cpp;
18473   CHECK_PARSE_BOOL(AlignTrailingComments);
18474   CHECK_PARSE_BOOL(AllowAllArgumentsOnNextLine);
18475   CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
18476   CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine);
18477   CHECK_PARSE_BOOL(AllowShortEnumsOnASingleLine);
18478   CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
18479   CHECK_PARSE_BOOL(BinPackArguments);
18480   CHECK_PARSE_BOOL(BinPackParameters);
18481   CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations);
18482   CHECK_PARSE_BOOL(BreakBeforeConceptDeclarations);
18483   CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
18484   CHECK_PARSE_BOOL(BreakStringLiterals);
18485   CHECK_PARSE_BOOL(CompactNamespaces);
18486   CHECK_PARSE_BOOL(DeriveLineEnding);
18487   CHECK_PARSE_BOOL(DerivePointerAlignment);
18488   CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding");
18489   CHECK_PARSE_BOOL(DisableFormat);
18490   CHECK_PARSE_BOOL(IndentAccessModifiers);
18491   CHECK_PARSE_BOOL(IndentCaseLabels);
18492   CHECK_PARSE_BOOL(IndentCaseBlocks);
18493   CHECK_PARSE_BOOL(IndentGotoLabels);
18494   CHECK_PARSE_BOOL(IndentRequires);
18495   CHECK_PARSE_BOOL(IndentWrappedFunctionNames);
18496   CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks);
18497   CHECK_PARSE_BOOL(ObjCSpaceAfterProperty);
18498   CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
18499   CHECK_PARSE_BOOL(Cpp11BracedListStyle);
18500   CHECK_PARSE_BOOL(ReflowComments);
18501   CHECK_PARSE_BOOL(SortUsingDeclarations);
18502   CHECK_PARSE_BOOL(SpacesInParentheses);
18503   CHECK_PARSE_BOOL(SpacesInSquareBrackets);
18504   CHECK_PARSE_BOOL(SpacesInConditionalStatement);
18505   CHECK_PARSE_BOOL(SpaceInEmptyBlock);
18506   CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
18507   CHECK_PARSE_BOOL(SpacesInContainerLiterals);
18508   CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
18509   CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
18510   CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword);
18511   CHECK_PARSE_BOOL(SpaceAfterLogicalNot);
18512   CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
18513   CHECK_PARSE_BOOL(SpaceBeforeCaseColon);
18514   CHECK_PARSE_BOOL(SpaceBeforeCpp11BracedList);
18515   CHECK_PARSE_BOOL(SpaceBeforeCtorInitializerColon);
18516   CHECK_PARSE_BOOL(SpaceBeforeInheritanceColon);
18517   CHECK_PARSE_BOOL(SpaceBeforeRangeBasedForLoopColon);
18518   CHECK_PARSE_BOOL(SpaceBeforeSquareBrackets);
18519   CHECK_PARSE_BOOL(UseCRLF);
18520 
18521   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterCaseLabel);
18522   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass);
18523   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum);
18524   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction);
18525   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace);
18526   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration);
18527   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct);
18528   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion);
18529   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterExternBlock);
18530   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch);
18531   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse);
18532   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeLambdaBody);
18533   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeWhile);
18534   CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces);
18535   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyFunction);
18536   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyRecord);
18537   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyNamespace);
18538 }
18539 
18540 #undef CHECK_PARSE_BOOL
18541 
18542 TEST_F(FormatTest, ParsesConfiguration) {
18543   FormatStyle Style = {};
18544   Style.Language = FormatStyle::LK_Cpp;
18545   CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
18546   CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",
18547               ConstructorInitializerIndentWidth, 1234u);
18548   CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u);
18549   CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
18550   CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
18551   CHECK_PARSE("PenaltyBreakAssignment: 1234", PenaltyBreakAssignment, 1234u);
18552   CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234",
18553               PenaltyBreakBeforeFirstCallParameter, 1234u);
18554   CHECK_PARSE("PenaltyBreakTemplateDeclaration: 1234",
18555               PenaltyBreakTemplateDeclaration, 1234u);
18556   CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
18557   CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
18558               PenaltyReturnTypeOnItsOwnLine, 1234u);
18559   CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
18560               SpacesBeforeTrailingComments, 1234u);
18561   CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
18562   CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u);
18563   CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$");
18564 
18565   Style.QualifierAlignment = FormatStyle::QAS_Right;
18566   CHECK_PARSE("QualifierAlignment: Leave", QualifierAlignment,
18567               FormatStyle::QAS_Leave);
18568   CHECK_PARSE("QualifierAlignment: Right", QualifierAlignment,
18569               FormatStyle::QAS_Right);
18570   CHECK_PARSE("QualifierAlignment: Left", QualifierAlignment,
18571               FormatStyle::QAS_Left);
18572   CHECK_PARSE("QualifierAlignment: Custom", QualifierAlignment,
18573               FormatStyle::QAS_Custom);
18574 
18575   Style.QualifierOrder.clear();
18576   CHECK_PARSE("QualifierOrder: [ const, volatile, type ]", QualifierOrder,
18577               std::vector<std::string>({"const", "volatile", "type"}));
18578   Style.QualifierOrder.clear();
18579   CHECK_PARSE("QualifierOrder: [const, type]", QualifierOrder,
18580               std::vector<std::string>({"const", "type"}));
18581   Style.QualifierOrder.clear();
18582   CHECK_PARSE("QualifierOrder: [volatile, type]", QualifierOrder,
18583               std::vector<std::string>({"volatile", "type"}));
18584 
18585   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
18586   CHECK_PARSE("AlignConsecutiveAssignments: None", AlignConsecutiveAssignments,
18587               FormatStyle::ACS_None);
18588   CHECK_PARSE("AlignConsecutiveAssignments: Consecutive",
18589               AlignConsecutiveAssignments, FormatStyle::ACS_Consecutive);
18590   CHECK_PARSE("AlignConsecutiveAssignments: AcrossEmptyLines",
18591               AlignConsecutiveAssignments, FormatStyle::ACS_AcrossEmptyLines);
18592   CHECK_PARSE("AlignConsecutiveAssignments: AcrossEmptyLinesAndComments",
18593               AlignConsecutiveAssignments,
18594               FormatStyle::ACS_AcrossEmptyLinesAndComments);
18595   // For backwards compability, false / true should still parse
18596   CHECK_PARSE("AlignConsecutiveAssignments: false", AlignConsecutiveAssignments,
18597               FormatStyle::ACS_None);
18598   CHECK_PARSE("AlignConsecutiveAssignments: true", AlignConsecutiveAssignments,
18599               FormatStyle::ACS_Consecutive);
18600 
18601   Style.AlignConsecutiveBitFields = FormatStyle::ACS_Consecutive;
18602   CHECK_PARSE("AlignConsecutiveBitFields: None", AlignConsecutiveBitFields,
18603               FormatStyle::ACS_None);
18604   CHECK_PARSE("AlignConsecutiveBitFields: Consecutive",
18605               AlignConsecutiveBitFields, FormatStyle::ACS_Consecutive);
18606   CHECK_PARSE("AlignConsecutiveBitFields: AcrossEmptyLines",
18607               AlignConsecutiveBitFields, FormatStyle::ACS_AcrossEmptyLines);
18608   CHECK_PARSE("AlignConsecutiveBitFields: AcrossEmptyLinesAndComments",
18609               AlignConsecutiveBitFields,
18610               FormatStyle::ACS_AcrossEmptyLinesAndComments);
18611   // For backwards compability, false / true should still parse
18612   CHECK_PARSE("AlignConsecutiveBitFields: false", AlignConsecutiveBitFields,
18613               FormatStyle::ACS_None);
18614   CHECK_PARSE("AlignConsecutiveBitFields: true", AlignConsecutiveBitFields,
18615               FormatStyle::ACS_Consecutive);
18616 
18617   Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
18618   CHECK_PARSE("AlignConsecutiveMacros: None", AlignConsecutiveMacros,
18619               FormatStyle::ACS_None);
18620   CHECK_PARSE("AlignConsecutiveMacros: Consecutive", AlignConsecutiveMacros,
18621               FormatStyle::ACS_Consecutive);
18622   CHECK_PARSE("AlignConsecutiveMacros: AcrossEmptyLines",
18623               AlignConsecutiveMacros, FormatStyle::ACS_AcrossEmptyLines);
18624   CHECK_PARSE("AlignConsecutiveMacros: AcrossEmptyLinesAndComments",
18625               AlignConsecutiveMacros,
18626               FormatStyle::ACS_AcrossEmptyLinesAndComments);
18627   // For backwards compability, false / true should still parse
18628   CHECK_PARSE("AlignConsecutiveMacros: false", AlignConsecutiveMacros,
18629               FormatStyle::ACS_None);
18630   CHECK_PARSE("AlignConsecutiveMacros: true", AlignConsecutiveMacros,
18631               FormatStyle::ACS_Consecutive);
18632 
18633   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
18634   CHECK_PARSE("AlignConsecutiveDeclarations: None",
18635               AlignConsecutiveDeclarations, FormatStyle::ACS_None);
18636   CHECK_PARSE("AlignConsecutiveDeclarations: Consecutive",
18637               AlignConsecutiveDeclarations, FormatStyle::ACS_Consecutive);
18638   CHECK_PARSE("AlignConsecutiveDeclarations: AcrossEmptyLines",
18639               AlignConsecutiveDeclarations, FormatStyle::ACS_AcrossEmptyLines);
18640   CHECK_PARSE("AlignConsecutiveDeclarations: AcrossEmptyLinesAndComments",
18641               AlignConsecutiveDeclarations,
18642               FormatStyle::ACS_AcrossEmptyLinesAndComments);
18643   // For backwards compability, false / true should still parse
18644   CHECK_PARSE("AlignConsecutiveDeclarations: false",
18645               AlignConsecutiveDeclarations, FormatStyle::ACS_None);
18646   CHECK_PARSE("AlignConsecutiveDeclarations: true",
18647               AlignConsecutiveDeclarations, FormatStyle::ACS_Consecutive);
18648 
18649   Style.PointerAlignment = FormatStyle::PAS_Middle;
18650   CHECK_PARSE("PointerAlignment: Left", PointerAlignment,
18651               FormatStyle::PAS_Left);
18652   CHECK_PARSE("PointerAlignment: Right", PointerAlignment,
18653               FormatStyle::PAS_Right);
18654   CHECK_PARSE("PointerAlignment: Middle", PointerAlignment,
18655               FormatStyle::PAS_Middle);
18656   Style.ReferenceAlignment = FormatStyle::RAS_Middle;
18657   CHECK_PARSE("ReferenceAlignment: Pointer", ReferenceAlignment,
18658               FormatStyle::RAS_Pointer);
18659   CHECK_PARSE("ReferenceAlignment: Left", ReferenceAlignment,
18660               FormatStyle::RAS_Left);
18661   CHECK_PARSE("ReferenceAlignment: Right", ReferenceAlignment,
18662               FormatStyle::RAS_Right);
18663   CHECK_PARSE("ReferenceAlignment: Middle", ReferenceAlignment,
18664               FormatStyle::RAS_Middle);
18665   // For backward compatibility:
18666   CHECK_PARSE("PointerBindsToType: Left", PointerAlignment,
18667               FormatStyle::PAS_Left);
18668   CHECK_PARSE("PointerBindsToType: Right", PointerAlignment,
18669               FormatStyle::PAS_Right);
18670   CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment,
18671               FormatStyle::PAS_Middle);
18672 
18673   Style.Standard = FormatStyle::LS_Auto;
18674   CHECK_PARSE("Standard: c++03", Standard, FormatStyle::LS_Cpp03);
18675   CHECK_PARSE("Standard: c++11", Standard, FormatStyle::LS_Cpp11);
18676   CHECK_PARSE("Standard: c++14", Standard, FormatStyle::LS_Cpp14);
18677   CHECK_PARSE("Standard: c++17", Standard, FormatStyle::LS_Cpp17);
18678   CHECK_PARSE("Standard: c++20", Standard, FormatStyle::LS_Cpp20);
18679   CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
18680   CHECK_PARSE("Standard: Latest", Standard, FormatStyle::LS_Latest);
18681   // Legacy aliases:
18682   CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03);
18683   CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Latest);
18684   CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
18685   CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
18686 
18687   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
18688   CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment",
18689               BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment);
18690   CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators,
18691               FormatStyle::BOS_None);
18692   CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators,
18693               FormatStyle::BOS_All);
18694   // For backward compatibility:
18695   CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators,
18696               FormatStyle::BOS_None);
18697   CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators,
18698               FormatStyle::BOS_All);
18699 
18700   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
18701   CHECK_PARSE("BreakConstructorInitializers: BeforeComma",
18702               BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma);
18703   CHECK_PARSE("BreakConstructorInitializers: AfterColon",
18704               BreakConstructorInitializers, FormatStyle::BCIS_AfterColon);
18705   CHECK_PARSE("BreakConstructorInitializers: BeforeColon",
18706               BreakConstructorInitializers, FormatStyle::BCIS_BeforeColon);
18707   // For backward compatibility:
18708   CHECK_PARSE("BreakConstructorInitializersBeforeComma: true",
18709               BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma);
18710 
18711   Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
18712   CHECK_PARSE("BreakInheritanceList: AfterComma", BreakInheritanceList,
18713               FormatStyle::BILS_AfterComma);
18714   CHECK_PARSE("BreakInheritanceList: BeforeComma", BreakInheritanceList,
18715               FormatStyle::BILS_BeforeComma);
18716   CHECK_PARSE("BreakInheritanceList: AfterColon", BreakInheritanceList,
18717               FormatStyle::BILS_AfterColon);
18718   CHECK_PARSE("BreakInheritanceList: BeforeColon", BreakInheritanceList,
18719               FormatStyle::BILS_BeforeColon);
18720   // For backward compatibility:
18721   CHECK_PARSE("BreakBeforeInheritanceComma: true", BreakInheritanceList,
18722               FormatStyle::BILS_BeforeComma);
18723 
18724   Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack;
18725   CHECK_PARSE("PackConstructorInitializers: Never", PackConstructorInitializers,
18726               FormatStyle::PCIS_Never);
18727   CHECK_PARSE("PackConstructorInitializers: BinPack",
18728               PackConstructorInitializers, FormatStyle::PCIS_BinPack);
18729   CHECK_PARSE("PackConstructorInitializers: CurrentLine",
18730               PackConstructorInitializers, FormatStyle::PCIS_CurrentLine);
18731   CHECK_PARSE("PackConstructorInitializers: NextLine",
18732               PackConstructorInitializers, FormatStyle::PCIS_NextLine);
18733   // For backward compatibility:
18734   CHECK_PARSE("BasedOnStyle: Google\n"
18735               "ConstructorInitializerAllOnOneLineOrOnePerLine: true\n"
18736               "AllowAllConstructorInitializersOnNextLine: false",
18737               PackConstructorInitializers, FormatStyle::PCIS_CurrentLine);
18738   Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
18739   CHECK_PARSE("BasedOnStyle: Google\n"
18740               "ConstructorInitializerAllOnOneLineOrOnePerLine: false",
18741               PackConstructorInitializers, FormatStyle::PCIS_BinPack);
18742   CHECK_PARSE("ConstructorInitializerAllOnOneLineOrOnePerLine: true\n"
18743               "AllowAllConstructorInitializersOnNextLine: true",
18744               PackConstructorInitializers, FormatStyle::PCIS_NextLine);
18745   Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack;
18746   CHECK_PARSE("ConstructorInitializerAllOnOneLineOrOnePerLine: true\n"
18747               "AllowAllConstructorInitializersOnNextLine: false",
18748               PackConstructorInitializers, FormatStyle::PCIS_CurrentLine);
18749 
18750   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
18751   CHECK_PARSE("EmptyLineBeforeAccessModifier: Never",
18752               EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Never);
18753   CHECK_PARSE("EmptyLineBeforeAccessModifier: Leave",
18754               EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Leave);
18755   CHECK_PARSE("EmptyLineBeforeAccessModifier: LogicalBlock",
18756               EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_LogicalBlock);
18757   CHECK_PARSE("EmptyLineBeforeAccessModifier: Always",
18758               EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Always);
18759 
18760   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
18761   CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket,
18762               FormatStyle::BAS_Align);
18763   CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket,
18764               FormatStyle::BAS_DontAlign);
18765   CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket,
18766               FormatStyle::BAS_AlwaysBreak);
18767   // For backward compatibility:
18768   CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket,
18769               FormatStyle::BAS_DontAlign);
18770   CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket,
18771               FormatStyle::BAS_Align);
18772 
18773   Style.AlignEscapedNewlines = FormatStyle::ENAS_Left;
18774   CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines,
18775               FormatStyle::ENAS_DontAlign);
18776   CHECK_PARSE("AlignEscapedNewlines: Left", AlignEscapedNewlines,
18777               FormatStyle::ENAS_Left);
18778   CHECK_PARSE("AlignEscapedNewlines: Right", AlignEscapedNewlines,
18779               FormatStyle::ENAS_Right);
18780   // For backward compatibility:
18781   CHECK_PARSE("AlignEscapedNewlinesLeft: true", AlignEscapedNewlines,
18782               FormatStyle::ENAS_Left);
18783   CHECK_PARSE("AlignEscapedNewlinesLeft: false", AlignEscapedNewlines,
18784               FormatStyle::ENAS_Right);
18785 
18786   Style.AlignOperands = FormatStyle::OAS_Align;
18787   CHECK_PARSE("AlignOperands: DontAlign", AlignOperands,
18788               FormatStyle::OAS_DontAlign);
18789   CHECK_PARSE("AlignOperands: Align", AlignOperands, FormatStyle::OAS_Align);
18790   CHECK_PARSE("AlignOperands: AlignAfterOperator", AlignOperands,
18791               FormatStyle::OAS_AlignAfterOperator);
18792   // For backward compatibility:
18793   CHECK_PARSE("AlignOperands: false", AlignOperands,
18794               FormatStyle::OAS_DontAlign);
18795   CHECK_PARSE("AlignOperands: true", AlignOperands, FormatStyle::OAS_Align);
18796 
18797   Style.UseTab = FormatStyle::UT_ForIndentation;
18798   CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
18799   CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
18800   CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
18801   CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab,
18802               FormatStyle::UT_ForContinuationAndIndentation);
18803   CHECK_PARSE("UseTab: AlignWithSpaces", UseTab,
18804               FormatStyle::UT_AlignWithSpaces);
18805   // For backward compatibility:
18806   CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never);
18807   CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always);
18808 
18809   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty;
18810   CHECK_PARSE("AllowShortBlocksOnASingleLine: Never",
18811               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never);
18812   CHECK_PARSE("AllowShortBlocksOnASingleLine: Empty",
18813               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Empty);
18814   CHECK_PARSE("AllowShortBlocksOnASingleLine: Always",
18815               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always);
18816   // For backward compatibility:
18817   CHECK_PARSE("AllowShortBlocksOnASingleLine: false",
18818               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never);
18819   CHECK_PARSE("AllowShortBlocksOnASingleLine: true",
18820               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always);
18821 
18822   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
18823   CHECK_PARSE("AllowShortFunctionsOnASingleLine: None",
18824               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
18825   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline",
18826               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline);
18827   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty",
18828               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty);
18829   CHECK_PARSE("AllowShortFunctionsOnASingleLine: All",
18830               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
18831   // For backward compatibility:
18832   CHECK_PARSE("AllowShortFunctionsOnASingleLine: false",
18833               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
18834   CHECK_PARSE("AllowShortFunctionsOnASingleLine: true",
18835               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
18836 
18837   Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Both;
18838   CHECK_PARSE("SpaceAroundPointerQualifiers: Default",
18839               SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Default);
18840   CHECK_PARSE("SpaceAroundPointerQualifiers: Before",
18841               SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Before);
18842   CHECK_PARSE("SpaceAroundPointerQualifiers: After",
18843               SpaceAroundPointerQualifiers, FormatStyle::SAPQ_After);
18844   CHECK_PARSE("SpaceAroundPointerQualifiers: Both",
18845               SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Both);
18846 
18847   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
18848   CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens,
18849               FormatStyle::SBPO_Never);
18850   CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens,
18851               FormatStyle::SBPO_Always);
18852   CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens,
18853               FormatStyle::SBPO_ControlStatements);
18854   CHECK_PARSE("SpaceBeforeParens: ControlStatementsExceptControlMacros",
18855               SpaceBeforeParens,
18856               FormatStyle::SBPO_ControlStatementsExceptControlMacros);
18857   CHECK_PARSE("SpaceBeforeParens: NonEmptyParentheses", SpaceBeforeParens,
18858               FormatStyle::SBPO_NonEmptyParentheses);
18859   CHECK_PARSE("SpaceBeforeParens: Custom", SpaceBeforeParens,
18860               FormatStyle::SBPO_Custom);
18861   // For backward compatibility:
18862   CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens,
18863               FormatStyle::SBPO_Never);
18864   CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens,
18865               FormatStyle::SBPO_ControlStatements);
18866   CHECK_PARSE("SpaceBeforeParens: ControlStatementsExceptForEachMacros",
18867               SpaceBeforeParens,
18868               FormatStyle::SBPO_ControlStatementsExceptControlMacros);
18869 
18870   Style.ColumnLimit = 123;
18871   FormatStyle BaseStyle = getLLVMStyle();
18872   CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
18873   CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
18874 
18875   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
18876   CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
18877               FormatStyle::BS_Attach);
18878   CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
18879               FormatStyle::BS_Linux);
18880   CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces,
18881               FormatStyle::BS_Mozilla);
18882   CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
18883               FormatStyle::BS_Stroustrup);
18884   CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces,
18885               FormatStyle::BS_Allman);
18886   CHECK_PARSE("BreakBeforeBraces: Whitesmiths", BreakBeforeBraces,
18887               FormatStyle::BS_Whitesmiths);
18888   CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU);
18889   CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces,
18890               FormatStyle::BS_WebKit);
18891   CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces,
18892               FormatStyle::BS_Custom);
18893 
18894   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never;
18895   CHECK_PARSE("BraceWrapping:\n"
18896               "  AfterControlStatement: MultiLine",
18897               BraceWrapping.AfterControlStatement,
18898               FormatStyle::BWACS_MultiLine);
18899   CHECK_PARSE("BraceWrapping:\n"
18900               "  AfterControlStatement: Always",
18901               BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always);
18902   CHECK_PARSE("BraceWrapping:\n"
18903               "  AfterControlStatement: Never",
18904               BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never);
18905   // For backward compatibility:
18906   CHECK_PARSE("BraceWrapping:\n"
18907               "  AfterControlStatement: true",
18908               BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always);
18909   CHECK_PARSE("BraceWrapping:\n"
18910               "  AfterControlStatement: false",
18911               BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never);
18912 
18913   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
18914   CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType,
18915               FormatStyle::RTBS_None);
18916   CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType,
18917               FormatStyle::RTBS_All);
18918   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel",
18919               AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel);
18920   CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions",
18921               AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions);
18922   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions",
18923               AlwaysBreakAfterReturnType,
18924               FormatStyle::RTBS_TopLevelDefinitions);
18925 
18926   Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
18927   CHECK_PARSE("AlwaysBreakTemplateDeclarations: No",
18928               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_No);
18929   CHECK_PARSE("AlwaysBreakTemplateDeclarations: MultiLine",
18930               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine);
18931   CHECK_PARSE("AlwaysBreakTemplateDeclarations: Yes",
18932               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes);
18933   CHECK_PARSE("AlwaysBreakTemplateDeclarations: false",
18934               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine);
18935   CHECK_PARSE("AlwaysBreakTemplateDeclarations: true",
18936               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes);
18937 
18938   Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
18939   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None",
18940               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None);
18941   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All",
18942               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All);
18943   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel",
18944               AlwaysBreakAfterDefinitionReturnType,
18945               FormatStyle::DRTBS_TopLevel);
18946 
18947   Style.NamespaceIndentation = FormatStyle::NI_All;
18948   CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
18949               FormatStyle::NI_None);
18950   CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation,
18951               FormatStyle::NI_Inner);
18952   CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
18953               FormatStyle::NI_All);
18954 
18955   Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_OnlyFirstIf;
18956   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Never",
18957               AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never);
18958   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: WithoutElse",
18959               AllowShortIfStatementsOnASingleLine,
18960               FormatStyle::SIS_WithoutElse);
18961   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: OnlyFirstIf",
18962               AllowShortIfStatementsOnASingleLine,
18963               FormatStyle::SIS_OnlyFirstIf);
18964   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: AllIfsAndElse",
18965               AllowShortIfStatementsOnASingleLine,
18966               FormatStyle::SIS_AllIfsAndElse);
18967   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Always",
18968               AllowShortIfStatementsOnASingleLine,
18969               FormatStyle::SIS_OnlyFirstIf);
18970   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: false",
18971               AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never);
18972   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: true",
18973               AllowShortIfStatementsOnASingleLine,
18974               FormatStyle::SIS_WithoutElse);
18975 
18976   Style.IndentExternBlock = FormatStyle::IEBS_NoIndent;
18977   CHECK_PARSE("IndentExternBlock: AfterExternBlock", IndentExternBlock,
18978               FormatStyle::IEBS_AfterExternBlock);
18979   CHECK_PARSE("IndentExternBlock: Indent", IndentExternBlock,
18980               FormatStyle::IEBS_Indent);
18981   CHECK_PARSE("IndentExternBlock: NoIndent", IndentExternBlock,
18982               FormatStyle::IEBS_NoIndent);
18983   CHECK_PARSE("IndentExternBlock: true", IndentExternBlock,
18984               FormatStyle::IEBS_Indent);
18985   CHECK_PARSE("IndentExternBlock: false", IndentExternBlock,
18986               FormatStyle::IEBS_NoIndent);
18987 
18988   Style.BitFieldColonSpacing = FormatStyle::BFCS_None;
18989   CHECK_PARSE("BitFieldColonSpacing: Both", BitFieldColonSpacing,
18990               FormatStyle::BFCS_Both);
18991   CHECK_PARSE("BitFieldColonSpacing: None", BitFieldColonSpacing,
18992               FormatStyle::BFCS_None);
18993   CHECK_PARSE("BitFieldColonSpacing: Before", BitFieldColonSpacing,
18994               FormatStyle::BFCS_Before);
18995   CHECK_PARSE("BitFieldColonSpacing: After", BitFieldColonSpacing,
18996               FormatStyle::BFCS_After);
18997 
18998   Style.SortJavaStaticImport = FormatStyle::SJSIO_Before;
18999   CHECK_PARSE("SortJavaStaticImport: After", SortJavaStaticImport,
19000               FormatStyle::SJSIO_After);
19001   CHECK_PARSE("SortJavaStaticImport: Before", SortJavaStaticImport,
19002               FormatStyle::SJSIO_Before);
19003 
19004   // FIXME: This is required because parsing a configuration simply overwrites
19005   // the first N elements of the list instead of resetting it.
19006   Style.ForEachMacros.clear();
19007   std::vector<std::string> BoostForeach;
19008   BoostForeach.push_back("BOOST_FOREACH");
19009   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach);
19010   std::vector<std::string> BoostAndQForeach;
19011   BoostAndQForeach.push_back("BOOST_FOREACH");
19012   BoostAndQForeach.push_back("Q_FOREACH");
19013   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
19014               BoostAndQForeach);
19015 
19016   Style.IfMacros.clear();
19017   std::vector<std::string> CustomIfs;
19018   CustomIfs.push_back("MYIF");
19019   CHECK_PARSE("IfMacros: [MYIF]", IfMacros, CustomIfs);
19020 
19021   Style.AttributeMacros.clear();
19022   CHECK_PARSE("BasedOnStyle: LLVM", AttributeMacros,
19023               std::vector<std::string>{"__capability"});
19024   CHECK_PARSE("AttributeMacros: [attr1, attr2]", AttributeMacros,
19025               std::vector<std::string>({"attr1", "attr2"}));
19026 
19027   Style.StatementAttributeLikeMacros.clear();
19028   CHECK_PARSE("StatementAttributeLikeMacros: [emit,Q_EMIT]",
19029               StatementAttributeLikeMacros,
19030               std::vector<std::string>({"emit", "Q_EMIT"}));
19031 
19032   Style.StatementMacros.clear();
19033   CHECK_PARSE("StatementMacros: [QUNUSED]", StatementMacros,
19034               std::vector<std::string>{"QUNUSED"});
19035   CHECK_PARSE("StatementMacros: [QUNUSED, QT_REQUIRE_VERSION]", StatementMacros,
19036               std::vector<std::string>({"QUNUSED", "QT_REQUIRE_VERSION"}));
19037 
19038   Style.NamespaceMacros.clear();
19039   CHECK_PARSE("NamespaceMacros: [TESTSUITE]", NamespaceMacros,
19040               std::vector<std::string>{"TESTSUITE"});
19041   CHECK_PARSE("NamespaceMacros: [TESTSUITE, SUITE]", NamespaceMacros,
19042               std::vector<std::string>({"TESTSUITE", "SUITE"}));
19043 
19044   Style.WhitespaceSensitiveMacros.clear();
19045   CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE]",
19046               WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"});
19047   CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE, ASSERT]",
19048               WhitespaceSensitiveMacros,
19049               std::vector<std::string>({"STRINGIZE", "ASSERT"}));
19050   Style.WhitespaceSensitiveMacros.clear();
19051   CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE']",
19052               WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"});
19053   CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE', 'ASSERT']",
19054               WhitespaceSensitiveMacros,
19055               std::vector<std::string>({"STRINGIZE", "ASSERT"}));
19056 
19057   Style.IncludeStyle.IncludeCategories.clear();
19058   std::vector<tooling::IncludeStyle::IncludeCategory> ExpectedCategories = {
19059       {"abc/.*", 2, 0, false}, {".*", 1, 0, true}};
19060   CHECK_PARSE("IncludeCategories:\n"
19061               "  - Regex: abc/.*\n"
19062               "    Priority: 2\n"
19063               "  - Regex: .*\n"
19064               "    Priority: 1\n"
19065               "    CaseSensitive: true\n",
19066               IncludeStyle.IncludeCategories, ExpectedCategories);
19067   CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeStyle.IncludeIsMainRegex,
19068               "abc$");
19069   CHECK_PARSE("IncludeIsMainSourceRegex: 'abc$'",
19070               IncludeStyle.IncludeIsMainSourceRegex, "abc$");
19071 
19072   Style.SortIncludes = FormatStyle::SI_Never;
19073   CHECK_PARSE("SortIncludes: true", SortIncludes,
19074               FormatStyle::SI_CaseSensitive);
19075   CHECK_PARSE("SortIncludes: false", SortIncludes, FormatStyle::SI_Never);
19076   CHECK_PARSE("SortIncludes: CaseInsensitive", SortIncludes,
19077               FormatStyle::SI_CaseInsensitive);
19078   CHECK_PARSE("SortIncludes: CaseSensitive", SortIncludes,
19079               FormatStyle::SI_CaseSensitive);
19080   CHECK_PARSE("SortIncludes: Never", SortIncludes, FormatStyle::SI_Never);
19081 
19082   Style.RawStringFormats.clear();
19083   std::vector<FormatStyle::RawStringFormat> ExpectedRawStringFormats = {
19084       {
19085           FormatStyle::LK_TextProto,
19086           {"pb", "proto"},
19087           {"PARSE_TEXT_PROTO"},
19088           /*CanonicalDelimiter=*/"",
19089           "llvm",
19090       },
19091       {
19092           FormatStyle::LK_Cpp,
19093           {"cc", "cpp"},
19094           {"C_CODEBLOCK", "CPPEVAL"},
19095           /*CanonicalDelimiter=*/"cc",
19096           /*BasedOnStyle=*/"",
19097       },
19098   };
19099 
19100   CHECK_PARSE("RawStringFormats:\n"
19101               "  - Language: TextProto\n"
19102               "    Delimiters:\n"
19103               "      - 'pb'\n"
19104               "      - 'proto'\n"
19105               "    EnclosingFunctions:\n"
19106               "      - 'PARSE_TEXT_PROTO'\n"
19107               "    BasedOnStyle: llvm\n"
19108               "  - Language: Cpp\n"
19109               "    Delimiters:\n"
19110               "      - 'cc'\n"
19111               "      - 'cpp'\n"
19112               "    EnclosingFunctions:\n"
19113               "      - 'C_CODEBLOCK'\n"
19114               "      - 'CPPEVAL'\n"
19115               "    CanonicalDelimiter: 'cc'",
19116               RawStringFormats, ExpectedRawStringFormats);
19117 
19118   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
19119               "  Minimum: 0\n"
19120               "  Maximum: 0",
19121               SpacesInLineCommentPrefix.Minimum, 0u);
19122   EXPECT_EQ(Style.SpacesInLineCommentPrefix.Maximum, 0u);
19123   Style.SpacesInLineCommentPrefix.Minimum = 1;
19124   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
19125               "  Minimum: 2",
19126               SpacesInLineCommentPrefix.Minimum, 0u);
19127   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
19128               "  Maximum: -1",
19129               SpacesInLineCommentPrefix.Maximum, -1u);
19130   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
19131               "  Minimum: 2",
19132               SpacesInLineCommentPrefix.Minimum, 2u);
19133   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
19134               "  Maximum: 1",
19135               SpacesInLineCommentPrefix.Maximum, 1u);
19136   EXPECT_EQ(Style.SpacesInLineCommentPrefix.Minimum, 1u);
19137 
19138   Style.SpacesInAngles = FormatStyle::SIAS_Always;
19139   CHECK_PARSE("SpacesInAngles: Never", SpacesInAngles, FormatStyle::SIAS_Never);
19140   CHECK_PARSE("SpacesInAngles: Always", SpacesInAngles,
19141               FormatStyle::SIAS_Always);
19142   CHECK_PARSE("SpacesInAngles: Leave", SpacesInAngles, FormatStyle::SIAS_Leave);
19143   // For backward compatibility:
19144   CHECK_PARSE("SpacesInAngles: false", SpacesInAngles, FormatStyle::SIAS_Never);
19145   CHECK_PARSE("SpacesInAngles: true", SpacesInAngles, FormatStyle::SIAS_Always);
19146 }
19147 
19148 TEST_F(FormatTest, ParsesConfigurationWithLanguages) {
19149   FormatStyle Style = {};
19150   Style.Language = FormatStyle::LK_Cpp;
19151   CHECK_PARSE("Language: Cpp\n"
19152               "IndentWidth: 12",
19153               IndentWidth, 12u);
19154   EXPECT_EQ(parseConfiguration("Language: JavaScript\n"
19155                                "IndentWidth: 34",
19156                                &Style),
19157             ParseError::Unsuitable);
19158   FormatStyle BinPackedTCS = {};
19159   BinPackedTCS.Language = FormatStyle::LK_JavaScript;
19160   EXPECT_EQ(parseConfiguration("BinPackArguments: true\n"
19161                                "InsertTrailingCommas: Wrapped",
19162                                &BinPackedTCS),
19163             ParseError::BinPackTrailingCommaConflict);
19164   EXPECT_EQ(12u, Style.IndentWidth);
19165   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
19166   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
19167 
19168   Style.Language = FormatStyle::LK_JavaScript;
19169   CHECK_PARSE("Language: JavaScript\n"
19170               "IndentWidth: 12",
19171               IndentWidth, 12u);
19172   CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u);
19173   EXPECT_EQ(parseConfiguration("Language: Cpp\n"
19174                                "IndentWidth: 34",
19175                                &Style),
19176             ParseError::Unsuitable);
19177   EXPECT_EQ(23u, Style.IndentWidth);
19178   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
19179   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
19180 
19181   CHECK_PARSE("BasedOnStyle: LLVM\n"
19182               "IndentWidth: 67",
19183               IndentWidth, 67u);
19184 
19185   CHECK_PARSE("---\n"
19186               "Language: JavaScript\n"
19187               "IndentWidth: 12\n"
19188               "---\n"
19189               "Language: Cpp\n"
19190               "IndentWidth: 34\n"
19191               "...\n",
19192               IndentWidth, 12u);
19193 
19194   Style.Language = FormatStyle::LK_Cpp;
19195   CHECK_PARSE("---\n"
19196               "Language: JavaScript\n"
19197               "IndentWidth: 12\n"
19198               "---\n"
19199               "Language: Cpp\n"
19200               "IndentWidth: 34\n"
19201               "...\n",
19202               IndentWidth, 34u);
19203   CHECK_PARSE("---\n"
19204               "IndentWidth: 78\n"
19205               "---\n"
19206               "Language: JavaScript\n"
19207               "IndentWidth: 56\n"
19208               "...\n",
19209               IndentWidth, 78u);
19210 
19211   Style.ColumnLimit = 123;
19212   Style.IndentWidth = 234;
19213   Style.BreakBeforeBraces = FormatStyle::BS_Linux;
19214   Style.TabWidth = 345;
19215   EXPECT_FALSE(parseConfiguration("---\n"
19216                                   "IndentWidth: 456\n"
19217                                   "BreakBeforeBraces: Allman\n"
19218                                   "---\n"
19219                                   "Language: JavaScript\n"
19220                                   "IndentWidth: 111\n"
19221                                   "TabWidth: 111\n"
19222                                   "---\n"
19223                                   "Language: Cpp\n"
19224                                   "BreakBeforeBraces: Stroustrup\n"
19225                                   "TabWidth: 789\n"
19226                                   "...\n",
19227                                   &Style));
19228   EXPECT_EQ(123u, Style.ColumnLimit);
19229   EXPECT_EQ(456u, Style.IndentWidth);
19230   EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces);
19231   EXPECT_EQ(789u, Style.TabWidth);
19232 
19233   EXPECT_EQ(parseConfiguration("---\n"
19234                                "Language: JavaScript\n"
19235                                "IndentWidth: 56\n"
19236                                "---\n"
19237                                "IndentWidth: 78\n"
19238                                "...\n",
19239                                &Style),
19240             ParseError::Error);
19241   EXPECT_EQ(parseConfiguration("---\n"
19242                                "Language: JavaScript\n"
19243                                "IndentWidth: 56\n"
19244                                "---\n"
19245                                "Language: JavaScript\n"
19246                                "IndentWidth: 78\n"
19247                                "...\n",
19248                                &Style),
19249             ParseError::Error);
19250 
19251   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
19252 }
19253 
19254 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) {
19255   FormatStyle Style = {};
19256   Style.Language = FormatStyle::LK_JavaScript;
19257   Style.BreakBeforeTernaryOperators = true;
19258   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value());
19259   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
19260 
19261   Style.BreakBeforeTernaryOperators = true;
19262   EXPECT_EQ(0, parseConfiguration("---\n"
19263                                   "BasedOnStyle: Google\n"
19264                                   "---\n"
19265                                   "Language: JavaScript\n"
19266                                   "IndentWidth: 76\n"
19267                                   "...\n",
19268                                   &Style)
19269                    .value());
19270   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
19271   EXPECT_EQ(76u, Style.IndentWidth);
19272   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
19273 }
19274 
19275 TEST_F(FormatTest, ConfigurationRoundTripTest) {
19276   FormatStyle Style = getLLVMStyle();
19277   std::string YAML = configurationAsText(Style);
19278   FormatStyle ParsedStyle = {};
19279   ParsedStyle.Language = FormatStyle::LK_Cpp;
19280   EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
19281   EXPECT_EQ(Style, ParsedStyle);
19282 }
19283 
19284 TEST_F(FormatTest, WorksFor8bitEncodings) {
19285   EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
19286             "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
19287             "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
19288             "\"\xef\xee\xf0\xf3...\"",
19289             format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
19290                    "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
19291                    "\xef\xee\xf0\xf3...\"",
19292                    getLLVMStyleWithColumns(12)));
19293 }
19294 
19295 TEST_F(FormatTest, HandlesUTF8BOM) {
19296   EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf"));
19297   EXPECT_EQ("\xef\xbb\xbf#include <iostream>",
19298             format("\xef\xbb\xbf#include <iostream>"));
19299   EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>",
19300             format("\xef\xbb\xbf\n#include <iostream>"));
19301 }
19302 
19303 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
19304 #if !defined(_MSC_VER)
19305 
19306 TEST_F(FormatTest, CountsUTF8CharactersProperly) {
19307   verifyFormat("\"Однажды в студёную зимнюю пору...\"",
19308                getLLVMStyleWithColumns(35));
19309   verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
19310                getLLVMStyleWithColumns(31));
19311   verifyFormat("// Однажды в студёную зимнюю пору...",
19312                getLLVMStyleWithColumns(36));
19313   verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32));
19314   verifyFormat("/* Однажды в студёную зимнюю пору... */",
19315                getLLVMStyleWithColumns(39));
19316   verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
19317                getLLVMStyleWithColumns(35));
19318 }
19319 
19320 TEST_F(FormatTest, SplitsUTF8Strings) {
19321   // Non-printable characters' width is currently considered to be the length in
19322   // bytes in UTF8. The characters can be displayed in very different manner
19323   // (zero-width, single width with a substitution glyph, expanded to their code
19324   // (e.g. "<8d>"), so there's no single correct way to handle them.
19325   EXPECT_EQ("\"aaaaÄ\"\n"
19326             "\"\xc2\x8d\";",
19327             format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
19328   EXPECT_EQ("\"aaaaaaaÄ\"\n"
19329             "\"\xc2\x8d\";",
19330             format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
19331   EXPECT_EQ("\"Однажды, в \"\n"
19332             "\"студёную \"\n"
19333             "\"зимнюю \"\n"
19334             "\"пору,\"",
19335             format("\"Однажды, в студёную зимнюю пору,\"",
19336                    getLLVMStyleWithColumns(13)));
19337   EXPECT_EQ(
19338       "\"一 二 三 \"\n"
19339       "\"四 五六 \"\n"
19340       "\"七 八 九 \"\n"
19341       "\"十\"",
19342       format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11)));
19343   EXPECT_EQ("\"一\t\"\n"
19344             "\"二 \t\"\n"
19345             "\"三 四 \"\n"
19346             "\"五\t\"\n"
19347             "\"六 \t\"\n"
19348             "\"七 \"\n"
19349             "\"八九十\tqq\"",
19350             format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
19351                    getLLVMStyleWithColumns(11)));
19352 
19353   // UTF8 character in an escape sequence.
19354   EXPECT_EQ("\"aaaaaa\"\n"
19355             "\"\\\xC2\x8D\"",
19356             format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10)));
19357 }
19358 
19359 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
19360   EXPECT_EQ("const char *sssss =\n"
19361             "    \"一二三四五六七八\\\n"
19362             " 九 十\";",
19363             format("const char *sssss = \"一二三四五六七八\\\n"
19364                    " 九 十\";",
19365                    getLLVMStyleWithColumns(30)));
19366 }
19367 
19368 TEST_F(FormatTest, SplitsUTF8LineComments) {
19369   EXPECT_EQ("// aaaaÄ\xc2\x8d",
19370             format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10)));
19371   EXPECT_EQ("// Я из лесу\n"
19372             "// вышел; был\n"
19373             "// сильный\n"
19374             "// мороз.",
19375             format("// Я из лесу вышел; был сильный мороз.",
19376                    getLLVMStyleWithColumns(13)));
19377   EXPECT_EQ("// 一二三\n"
19378             "// 四五六七\n"
19379             "// 八  九\n"
19380             "// 十",
19381             format("// 一二三 四五六七 八  九 十", getLLVMStyleWithColumns(9)));
19382 }
19383 
19384 TEST_F(FormatTest, SplitsUTF8BlockComments) {
19385   EXPECT_EQ("/* Гляжу,\n"
19386             " * поднимается\n"
19387             " * медленно в\n"
19388             " * гору\n"
19389             " * Лошадка,\n"
19390             " * везущая\n"
19391             " * хворосту\n"
19392             " * воз. */",
19393             format("/* Гляжу, поднимается медленно в гору\n"
19394                    " * Лошадка, везущая хворосту воз. */",
19395                    getLLVMStyleWithColumns(13)));
19396   EXPECT_EQ(
19397       "/* 一二三\n"
19398       " * 四五六七\n"
19399       " * 八  九\n"
19400       " * 十  */",
19401       format("/* 一二三 四五六七 八  九 十  */", getLLVMStyleWithColumns(9)));
19402   EXPECT_EQ("/* �������� ��������\n"
19403             " * ��������\n"
19404             " * ������-�� */",
19405             format("/* �������� �������� �������� ������-�� */", getLLVMStyleWithColumns(12)));
19406 }
19407 
19408 #endif // _MSC_VER
19409 
19410 TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
19411   FormatStyle Style = getLLVMStyle();
19412 
19413   Style.ConstructorInitializerIndentWidth = 4;
19414   verifyFormat(
19415       "SomeClass::Constructor()\n"
19416       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
19417       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
19418       Style);
19419 
19420   Style.ConstructorInitializerIndentWidth = 2;
19421   verifyFormat(
19422       "SomeClass::Constructor()\n"
19423       "  : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
19424       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
19425       Style);
19426 
19427   Style.ConstructorInitializerIndentWidth = 0;
19428   verifyFormat(
19429       "SomeClass::Constructor()\n"
19430       ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
19431       "  aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
19432       Style);
19433   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
19434   verifyFormat(
19435       "SomeLongTemplateVariableName<\n"
19436       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>",
19437       Style);
19438   verifyFormat("bool smaller = 1 < "
19439                "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
19440                "                       "
19441                "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
19442                Style);
19443 
19444   Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
19445   verifyFormat("SomeClass::Constructor() :\n"
19446                "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa),\n"
19447                "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa) {}",
19448                Style);
19449 }
19450 
19451 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {
19452   FormatStyle Style = getLLVMStyle();
19453   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
19454   Style.ConstructorInitializerIndentWidth = 4;
19455   verifyFormat("SomeClass::Constructor()\n"
19456                "    : a(a)\n"
19457                "    , b(b)\n"
19458                "    , c(c) {}",
19459                Style);
19460   verifyFormat("SomeClass::Constructor()\n"
19461                "    : a(a) {}",
19462                Style);
19463 
19464   Style.ColumnLimit = 0;
19465   verifyFormat("SomeClass::Constructor()\n"
19466                "    : a(a) {}",
19467                Style);
19468   verifyFormat("SomeClass::Constructor() noexcept\n"
19469                "    : a(a) {}",
19470                Style);
19471   verifyFormat("SomeClass::Constructor()\n"
19472                "    : a(a)\n"
19473                "    , b(b)\n"
19474                "    , c(c) {}",
19475                Style);
19476   verifyFormat("SomeClass::Constructor()\n"
19477                "    : a(a) {\n"
19478                "  foo();\n"
19479                "  bar();\n"
19480                "}",
19481                Style);
19482 
19483   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
19484   verifyFormat("SomeClass::Constructor()\n"
19485                "    : a(a)\n"
19486                "    , b(b)\n"
19487                "    , c(c) {\n}",
19488                Style);
19489   verifyFormat("SomeClass::Constructor()\n"
19490                "    : a(a) {\n}",
19491                Style);
19492 
19493   Style.ColumnLimit = 80;
19494   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
19495   Style.ConstructorInitializerIndentWidth = 2;
19496   verifyFormat("SomeClass::Constructor()\n"
19497                "  : a(a)\n"
19498                "  , b(b)\n"
19499                "  , c(c) {}",
19500                Style);
19501 
19502   Style.ConstructorInitializerIndentWidth = 0;
19503   verifyFormat("SomeClass::Constructor()\n"
19504                ": a(a)\n"
19505                ", b(b)\n"
19506                ", c(c) {}",
19507                Style);
19508 
19509   Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
19510   Style.ConstructorInitializerIndentWidth = 4;
19511   verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
19512   verifyFormat(
19513       "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n",
19514       Style);
19515   verifyFormat(
19516       "SomeClass::Constructor()\n"
19517       "    : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
19518       Style);
19519   Style.ConstructorInitializerIndentWidth = 4;
19520   Style.ColumnLimit = 60;
19521   verifyFormat("SomeClass::Constructor()\n"
19522                "    : aaaaaaaa(aaaaaaaa)\n"
19523                "    , aaaaaaaa(aaaaaaaa)\n"
19524                "    , aaaaaaaa(aaaaaaaa) {}",
19525                Style);
19526 }
19527 
19528 TEST_F(FormatTest, ConstructorInitializersWithPreprocessorDirective) {
19529   FormatStyle Style = getLLVMStyle();
19530   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
19531   Style.ConstructorInitializerIndentWidth = 4;
19532   verifyFormat("SomeClass::Constructor()\n"
19533                "    : a{a}\n"
19534                "    , b{b} {}",
19535                Style);
19536   verifyFormat("SomeClass::Constructor()\n"
19537                "    : a{a}\n"
19538                "#if CONDITION\n"
19539                "    , b{b}\n"
19540                "#endif\n"
19541                "{\n}",
19542                Style);
19543   Style.ConstructorInitializerIndentWidth = 2;
19544   verifyFormat("SomeClass::Constructor()\n"
19545                "#if CONDITION\n"
19546                "  : a{a}\n"
19547                "#endif\n"
19548                "  , b{b}\n"
19549                "  , c{c} {\n}",
19550                Style);
19551   Style.ConstructorInitializerIndentWidth = 0;
19552   verifyFormat("SomeClass::Constructor()\n"
19553                ": a{a}\n"
19554                "#ifdef CONDITION\n"
19555                ", b{b}\n"
19556                "#else\n"
19557                ", c{c}\n"
19558                "#endif\n"
19559                ", d{d} {\n}",
19560                Style);
19561   Style.ConstructorInitializerIndentWidth = 4;
19562   verifyFormat("SomeClass::Constructor()\n"
19563                "    : a{a}\n"
19564                "#if WINDOWS\n"
19565                "#if DEBUG\n"
19566                "    , b{0}\n"
19567                "#else\n"
19568                "    , b{1}\n"
19569                "#endif\n"
19570                "#else\n"
19571                "#if DEBUG\n"
19572                "    , b{2}\n"
19573                "#else\n"
19574                "    , b{3}\n"
19575                "#endif\n"
19576                "#endif\n"
19577                "{\n}",
19578                Style);
19579   verifyFormat("SomeClass::Constructor()\n"
19580                "    : a{a}\n"
19581                "#if WINDOWS\n"
19582                "    , b{0}\n"
19583                "#if DEBUG\n"
19584                "    , c{0}\n"
19585                "#else\n"
19586                "    , c{1}\n"
19587                "#endif\n"
19588                "#else\n"
19589                "#if DEBUG\n"
19590                "    , c{2}\n"
19591                "#else\n"
19592                "    , c{3}\n"
19593                "#endif\n"
19594                "    , b{1}\n"
19595                "#endif\n"
19596                "{\n}",
19597                Style);
19598 }
19599 
19600 TEST_F(FormatTest, Destructors) {
19601   verifyFormat("void F(int &i) { i.~int(); }");
19602   verifyFormat("void F(int &i) { i->~int(); }");
19603 }
19604 
19605 TEST_F(FormatTest, FormatsWithWebKitStyle) {
19606   FormatStyle Style = getWebKitStyle();
19607 
19608   // Don't indent in outer namespaces.
19609   verifyFormat("namespace outer {\n"
19610                "int i;\n"
19611                "namespace inner {\n"
19612                "    int i;\n"
19613                "} // namespace inner\n"
19614                "} // namespace outer\n"
19615                "namespace other_outer {\n"
19616                "int i;\n"
19617                "}",
19618                Style);
19619 
19620   // Don't indent case labels.
19621   verifyFormat("switch (variable) {\n"
19622                "case 1:\n"
19623                "case 2:\n"
19624                "    doSomething();\n"
19625                "    break;\n"
19626                "default:\n"
19627                "    ++variable;\n"
19628                "}",
19629                Style);
19630 
19631   // Wrap before binary operators.
19632   EXPECT_EQ("void f()\n"
19633             "{\n"
19634             "    if (aaaaaaaaaaaaaaaa\n"
19635             "        && bbbbbbbbbbbbbbbbbbbbbbbb\n"
19636             "        && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
19637             "        return;\n"
19638             "}",
19639             format("void f() {\n"
19640                    "if (aaaaaaaaaaaaaaaa\n"
19641                    "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
19642                    "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
19643                    "return;\n"
19644                    "}",
19645                    Style));
19646 
19647   // Allow functions on a single line.
19648   verifyFormat("void f() { return; }", Style);
19649 
19650   // Allow empty blocks on a single line and insert a space in empty blocks.
19651   EXPECT_EQ("void f() { }", format("void f() {}", Style));
19652   EXPECT_EQ("while (true) { }", format("while (true) {}", Style));
19653   // However, don't merge non-empty short loops.
19654   EXPECT_EQ("while (true) {\n"
19655             "    continue;\n"
19656             "}",
19657             format("while (true) { continue; }", Style));
19658 
19659   // Constructor initializers are formatted one per line with the "," on the
19660   // new line.
19661   verifyFormat("Constructor()\n"
19662                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
19663                "    , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
19664                "          aaaaaaaaaaaaaa)\n"
19665                "    , aaaaaaaaaaaaaaaaaaaaaaa()\n"
19666                "{\n"
19667                "}",
19668                Style);
19669   verifyFormat("SomeClass::Constructor()\n"
19670                "    : a(a)\n"
19671                "{\n"
19672                "}",
19673                Style);
19674   EXPECT_EQ("SomeClass::Constructor()\n"
19675             "    : a(a)\n"
19676             "{\n"
19677             "}",
19678             format("SomeClass::Constructor():a(a){}", Style));
19679   verifyFormat("SomeClass::Constructor()\n"
19680                "    : a(a)\n"
19681                "    , b(b)\n"
19682                "    , c(c)\n"
19683                "{\n"
19684                "}",
19685                Style);
19686   verifyFormat("SomeClass::Constructor()\n"
19687                "    : a(a)\n"
19688                "{\n"
19689                "    foo();\n"
19690                "    bar();\n"
19691                "}",
19692                Style);
19693 
19694   // Access specifiers should be aligned left.
19695   verifyFormat("class C {\n"
19696                "public:\n"
19697                "    int i;\n"
19698                "};",
19699                Style);
19700 
19701   // Do not align comments.
19702   verifyFormat("int a; // Do not\n"
19703                "double b; // align comments.",
19704                Style);
19705 
19706   // Do not align operands.
19707   EXPECT_EQ("ASSERT(aaaa\n"
19708             "    || bbbb);",
19709             format("ASSERT ( aaaa\n||bbbb);", Style));
19710 
19711   // Accept input's line breaks.
19712   EXPECT_EQ("if (aaaaaaaaaaaaaaa\n"
19713             "    || bbbbbbbbbbbbbbb) {\n"
19714             "    i++;\n"
19715             "}",
19716             format("if (aaaaaaaaaaaaaaa\n"
19717                    "|| bbbbbbbbbbbbbbb) { i++; }",
19718                    Style));
19719   EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
19720             "    i++;\n"
19721             "}",
19722             format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
19723 
19724   // Don't automatically break all macro definitions (llvm.org/PR17842).
19725   verifyFormat("#define aNumber 10", Style);
19726   // However, generally keep the line breaks that the user authored.
19727   EXPECT_EQ("#define aNumber \\\n"
19728             "    10",
19729             format("#define aNumber \\\n"
19730                    " 10",
19731                    Style));
19732 
19733   // Keep empty and one-element array literals on a single line.
19734   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
19735             "                                  copyItems:YES];",
19736             format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
19737                    "copyItems:YES];",
19738                    Style));
19739   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
19740             "                                  copyItems:YES];",
19741             format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
19742                    "             copyItems:YES];",
19743                    Style));
19744   // FIXME: This does not seem right, there should be more indentation before
19745   // the array literal's entries. Nested blocks have the same problem.
19746   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
19747             "    @\"a\",\n"
19748             "    @\"a\"\n"
19749             "]\n"
19750             "                                  copyItems:YES];",
19751             format("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
19752                    "     @\"a\",\n"
19753                    "     @\"a\"\n"
19754                    "     ]\n"
19755                    "       copyItems:YES];",
19756                    Style));
19757   EXPECT_EQ(
19758       "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
19759       "                                  copyItems:YES];",
19760       format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
19761              "   copyItems:YES];",
19762              Style));
19763 
19764   verifyFormat("[self.a b:c c:d];", Style);
19765   EXPECT_EQ("[self.a b:c\n"
19766             "        c:d];",
19767             format("[self.a b:c\n"
19768                    "c:d];",
19769                    Style));
19770 }
19771 
19772 TEST_F(FormatTest, FormatsLambdas) {
19773   verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n");
19774   verifyFormat(
19775       "int c = [b]() mutable noexcept { return [&b] { return b++; }(); }();\n");
19776   verifyFormat("int c = [&] { [=] { return b++; }(); }();\n");
19777   verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n");
19778   verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n");
19779   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n");
19780   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n");
19781   verifyFormat("auto c = [a = [b = 42] {}] {};\n");
19782   verifyFormat("auto c = [a = &i + 10, b = [] {}] {};\n");
19783   verifyFormat("int x = f(*+[] {});");
19784   verifyFormat("void f() {\n"
19785                "  other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
19786                "}\n");
19787   verifyFormat("void f() {\n"
19788                "  other(x.begin(), //\n"
19789                "        x.end(),   //\n"
19790                "        [&](int, int) { return 1; });\n"
19791                "}\n");
19792   verifyFormat("void f() {\n"
19793                "  other.other.other.other.other(\n"
19794                "      x.begin(), x.end(),\n"
19795                "      [something, rather](int, int, int, int, int, int, int) { "
19796                "return 1; });\n"
19797                "}\n");
19798   verifyFormat(
19799       "void f() {\n"
19800       "  other.other.other.other.other(\n"
19801       "      x.begin(), x.end(),\n"
19802       "      [something, rather](int, int, int, int, int, int, int) {\n"
19803       "        //\n"
19804       "      });\n"
19805       "}\n");
19806   verifyFormat("SomeFunction([]() { // A cool function...\n"
19807                "  return 43;\n"
19808                "});");
19809   EXPECT_EQ("SomeFunction([]() {\n"
19810             "#define A a\n"
19811             "  return 43;\n"
19812             "});",
19813             format("SomeFunction([](){\n"
19814                    "#define A a\n"
19815                    "return 43;\n"
19816                    "});"));
19817   verifyFormat("void f() {\n"
19818                "  SomeFunction([](decltype(x), A *a) {});\n"
19819                "  SomeFunction([](typeof(x), A *a) {});\n"
19820                "  SomeFunction([](_Atomic(x), A *a) {});\n"
19821                "  SomeFunction([](__underlying_type(x), A *a) {});\n"
19822                "}");
19823   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
19824                "    [](const aaaaaaaaaa &a) { return a; });");
19825   verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
19826                "  SomeOtherFunctioooooooooooooooooooooooooon();\n"
19827                "});");
19828   verifyFormat("Constructor()\n"
19829                "    : Field([] { // comment\n"
19830                "        int i;\n"
19831                "      }) {}");
19832   verifyFormat("auto my_lambda = [](const string &some_parameter) {\n"
19833                "  return some_parameter.size();\n"
19834                "};");
19835   verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n"
19836                "    [](const string &s) { return s; };");
19837   verifyFormat("int i = aaaaaa ? 1 //\n"
19838                "               : [] {\n"
19839                "                   return 2; //\n"
19840                "                 }();");
19841   verifyFormat("llvm::errs() << \"number of twos is \"\n"
19842                "             << std::count_if(v.begin(), v.end(), [](int x) {\n"
19843                "                  return x == 2; // force break\n"
19844                "                });");
19845   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
19846                "    [=](int iiiiiiiiiiii) {\n"
19847                "      return aaaaaaaaaaaaaaaaaaaaaaa !=\n"
19848                "             aaaaaaaaaaaaaaaaaaaaaaa;\n"
19849                "    });",
19850                getLLVMStyleWithColumns(60));
19851 
19852   verifyFormat("SomeFunction({[&] {\n"
19853                "                // comment\n"
19854                "              },\n"
19855                "              [&] {\n"
19856                "                // comment\n"
19857                "              }});");
19858   verifyFormat("SomeFunction({[&] {\n"
19859                "  // comment\n"
19860                "}});");
19861   verifyFormat(
19862       "virtual aaaaaaaaaaaaaaaa(\n"
19863       "    std::function<bool()> bbbbbbbbbbbb = [&]() { return true; },\n"
19864       "    aaaaa aaaaaaaaa);");
19865 
19866   // Lambdas with return types.
19867   verifyFormat("int c = []() -> int { return 2; }();\n");
19868   verifyFormat("int c = []() -> int * { return 2; }();\n");
19869   verifyFormat("int c = []() -> vector<int> { return {2}; }();\n");
19870   verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
19871   verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
19872   verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
19873   verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
19874   verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
19875   verifyFormat("[a, a]() -> a<1> {};");
19876   verifyFormat("[]() -> foo<5 + 2> { return {}; };");
19877   verifyFormat("[]() -> foo<5 - 2> { return {}; };");
19878   verifyFormat("[]() -> foo<5 / 2> { return {}; };");
19879   verifyFormat("[]() -> foo<5 * 2> { return {}; };");
19880   verifyFormat("[]() -> foo<5 % 2> { return {}; };");
19881   verifyFormat("[]() -> foo<5 << 2> { return {}; };");
19882   verifyFormat("[]() -> foo<!5> { return {}; };");
19883   verifyFormat("[]() -> foo<~5> { return {}; };");
19884   verifyFormat("[]() -> foo<5 | 2> { return {}; };");
19885   verifyFormat("[]() -> foo<5 || 2> { return {}; };");
19886   verifyFormat("[]() -> foo<5 & 2> { return {}; };");
19887   verifyFormat("[]() -> foo<5 && 2> { return {}; };");
19888   verifyFormat("[]() -> foo<5 == 2> { return {}; };");
19889   verifyFormat("[]() -> foo<5 != 2> { return {}; };");
19890   verifyFormat("[]() -> foo<5 >= 2> { return {}; };");
19891   verifyFormat("[]() -> foo<5 <= 2> { return {}; };");
19892   verifyFormat("[]() -> foo<5 < 2> { return {}; };");
19893   verifyFormat("[]() -> foo<2 ? 1 : 0> { return {}; };");
19894   verifyFormat("namespace bar {\n"
19895                "// broken:\n"
19896                "auto foo{[]() -> foo<5 + 2> { return {}; }};\n"
19897                "} // namespace bar");
19898   verifyFormat("namespace bar {\n"
19899                "// broken:\n"
19900                "auto foo{[]() -> foo<5 - 2> { return {}; }};\n"
19901                "} // namespace bar");
19902   verifyFormat("namespace bar {\n"
19903                "// broken:\n"
19904                "auto foo{[]() -> foo<5 / 2> { return {}; }};\n"
19905                "} // namespace bar");
19906   verifyFormat("namespace bar {\n"
19907                "// broken:\n"
19908                "auto foo{[]() -> foo<5 * 2> { return {}; }};\n"
19909                "} // namespace bar");
19910   verifyFormat("namespace bar {\n"
19911                "// broken:\n"
19912                "auto foo{[]() -> foo<5 % 2> { return {}; }};\n"
19913                "} // namespace bar");
19914   verifyFormat("namespace bar {\n"
19915                "// broken:\n"
19916                "auto foo{[]() -> foo<5 << 2> { return {}; }};\n"
19917                "} // namespace bar");
19918   verifyFormat("namespace bar {\n"
19919                "// broken:\n"
19920                "auto foo{[]() -> foo<!5> { return {}; }};\n"
19921                "} // namespace bar");
19922   verifyFormat("namespace bar {\n"
19923                "// broken:\n"
19924                "auto foo{[]() -> foo<~5> { return {}; }};\n"
19925                "} // namespace bar");
19926   verifyFormat("namespace bar {\n"
19927                "// broken:\n"
19928                "auto foo{[]() -> foo<5 | 2> { return {}; }};\n"
19929                "} // namespace bar");
19930   verifyFormat("namespace bar {\n"
19931                "// broken:\n"
19932                "auto foo{[]() -> foo<5 || 2> { return {}; }};\n"
19933                "} // namespace bar");
19934   verifyFormat("namespace bar {\n"
19935                "// broken:\n"
19936                "auto foo{[]() -> foo<5 & 2> { return {}; }};\n"
19937                "} // namespace bar");
19938   verifyFormat("namespace bar {\n"
19939                "// broken:\n"
19940                "auto foo{[]() -> foo<5 && 2> { return {}; }};\n"
19941                "} // namespace bar");
19942   verifyFormat("namespace bar {\n"
19943                "// broken:\n"
19944                "auto foo{[]() -> foo<5 == 2> { return {}; }};\n"
19945                "} // namespace bar");
19946   verifyFormat("namespace bar {\n"
19947                "// broken:\n"
19948                "auto foo{[]() -> foo<5 != 2> { return {}; }};\n"
19949                "} // namespace bar");
19950   verifyFormat("namespace bar {\n"
19951                "// broken:\n"
19952                "auto foo{[]() -> foo<5 >= 2> { return {}; }};\n"
19953                "} // namespace bar");
19954   verifyFormat("namespace bar {\n"
19955                "// broken:\n"
19956                "auto foo{[]() -> foo<5 <= 2> { return {}; }};\n"
19957                "} // namespace bar");
19958   verifyFormat("namespace bar {\n"
19959                "// broken:\n"
19960                "auto foo{[]() -> foo<5 < 2> { return {}; }};\n"
19961                "} // namespace bar");
19962   verifyFormat("namespace bar {\n"
19963                "// broken:\n"
19964                "auto foo{[]() -> foo<2 ? 1 : 0> { return {}; }};\n"
19965                "} // namespace bar");
19966   verifyFormat("[]() -> a<1> {};");
19967   verifyFormat("[]() -> a<1> { ; };");
19968   verifyFormat("[]() -> a<1> { ; }();");
19969   verifyFormat("[a, a]() -> a<true> {};");
19970   verifyFormat("[]() -> a<true> {};");
19971   verifyFormat("[]() -> a<true> { ; };");
19972   verifyFormat("[]() -> a<true> { ; }();");
19973   verifyFormat("[a, a]() -> a<false> {};");
19974   verifyFormat("[]() -> a<false> {};");
19975   verifyFormat("[]() -> a<false> { ; };");
19976   verifyFormat("[]() -> a<false> { ; }();");
19977   verifyFormat("auto foo{[]() -> foo<false> { ; }};");
19978   verifyFormat("namespace bar {\n"
19979                "auto foo{[]() -> foo<false> { ; }};\n"
19980                "} // namespace bar");
19981   verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
19982                "                   int j) -> int {\n"
19983                "  return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
19984                "};");
19985   verifyFormat(
19986       "aaaaaaaaaaaaaaaaaaaaaa(\n"
19987       "    [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n"
19988       "      return aaaaaaaaaaaaaaaaa;\n"
19989       "    });",
19990       getLLVMStyleWithColumns(70));
19991   verifyFormat("[]() //\n"
19992                "    -> int {\n"
19993                "  return 1; //\n"
19994                "};");
19995   verifyFormat("[]() -> Void<T...> {};");
19996   verifyFormat("[a, b]() -> Tuple<T...> { return {}; };");
19997 
19998   // Lambdas with explicit template argument lists.
19999   verifyFormat(
20000       "auto L = []<template <typename> class T, class U>(T<U> &&a) {};\n");
20001 
20002   // Multiple lambdas in the same parentheses change indentation rules. These
20003   // lambdas are forced to start on new lines.
20004   verifyFormat("SomeFunction(\n"
20005                "    []() {\n"
20006                "      //\n"
20007                "    },\n"
20008                "    []() {\n"
20009                "      //\n"
20010                "    });");
20011 
20012   // A lambda passed as arg0 is always pushed to the next line.
20013   verifyFormat("SomeFunction(\n"
20014                "    [this] {\n"
20015                "      //\n"
20016                "    },\n"
20017                "    1);\n");
20018 
20019   // A multi-line lambda passed as arg1 forces arg0 to be pushed out, just like
20020   // the arg0 case above.
20021   auto Style = getGoogleStyle();
20022   Style.BinPackArguments = false;
20023   verifyFormat("SomeFunction(\n"
20024                "    a,\n"
20025                "    [this] {\n"
20026                "      //\n"
20027                "    },\n"
20028                "    b);\n",
20029                Style);
20030   verifyFormat("SomeFunction(\n"
20031                "    a,\n"
20032                "    [this] {\n"
20033                "      //\n"
20034                "    },\n"
20035                "    b);\n");
20036 
20037   // A lambda with a very long line forces arg0 to be pushed out irrespective of
20038   // the BinPackArguments value (as long as the code is wide enough).
20039   verifyFormat(
20040       "something->SomeFunction(\n"
20041       "    a,\n"
20042       "    [this] {\n"
20043       "      "
20044       "D0000000000000000000000000000000000000000000000000000000000001();\n"
20045       "    },\n"
20046       "    b);\n");
20047 
20048   // A multi-line lambda is pulled up as long as the introducer fits on the
20049   // previous line and there are no further args.
20050   verifyFormat("function(1, [this, that] {\n"
20051                "  //\n"
20052                "});\n");
20053   verifyFormat("function([this, that] {\n"
20054                "  //\n"
20055                "});\n");
20056   // FIXME: this format is not ideal and we should consider forcing the first
20057   // arg onto its own line.
20058   verifyFormat("function(a, b, c, //\n"
20059                "         d, [this, that] {\n"
20060                "           //\n"
20061                "         });\n");
20062 
20063   // Multiple lambdas are treated correctly even when there is a short arg0.
20064   verifyFormat("SomeFunction(\n"
20065                "    1,\n"
20066                "    [this] {\n"
20067                "      //\n"
20068                "    },\n"
20069                "    [this] {\n"
20070                "      //\n"
20071                "    },\n"
20072                "    1);\n");
20073 
20074   // More complex introducers.
20075   verifyFormat("return [i, args...] {};");
20076 
20077   // Not lambdas.
20078   verifyFormat("constexpr char hello[]{\"hello\"};");
20079   verifyFormat("double &operator[](int i) { return 0; }\n"
20080                "int i;");
20081   verifyFormat("std::unique_ptr<int[]> foo() {}");
20082   verifyFormat("int i = a[a][a]->f();");
20083   verifyFormat("int i = (*b)[a]->f();");
20084 
20085   // Other corner cases.
20086   verifyFormat("void f() {\n"
20087                "  bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
20088                "  );\n"
20089                "}");
20090 
20091   // Lambdas created through weird macros.
20092   verifyFormat("void f() {\n"
20093                "  MACRO((const AA &a) { return 1; });\n"
20094                "  MACRO((AA &a) { return 1; });\n"
20095                "}");
20096 
20097   verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
20098                "      doo_dah();\n"
20099                "      doo_dah();\n"
20100                "    })) {\n"
20101                "}");
20102   verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n"
20103                "                doo_dah();\n"
20104                "                doo_dah();\n"
20105                "              })) {\n"
20106                "}");
20107   verifyFormat("if CONSTEXPR (blah_blah(whatever, whatever, [] {\n"
20108                "                doo_dah();\n"
20109                "                doo_dah();\n"
20110                "              })) {\n"
20111                "}");
20112   verifyFormat("auto lambda = []() {\n"
20113                "  int a = 2\n"
20114                "#if A\n"
20115                "          + 2\n"
20116                "#endif\n"
20117                "      ;\n"
20118                "};");
20119 
20120   // Lambdas with complex multiline introducers.
20121   verifyFormat(
20122       "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
20123       "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n"
20124       "        -> ::std::unordered_set<\n"
20125       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
20126       "      //\n"
20127       "    });");
20128 
20129   FormatStyle DoNotMerge = getLLVMStyle();
20130   DoNotMerge.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
20131   verifyFormat("auto c = []() {\n"
20132                "  return b;\n"
20133                "};",
20134                "auto c = []() { return b; };", DoNotMerge);
20135   verifyFormat("auto c = []() {\n"
20136                "};",
20137                " auto c = []() {};", DoNotMerge);
20138 
20139   FormatStyle MergeEmptyOnly = getLLVMStyle();
20140   MergeEmptyOnly.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Empty;
20141   verifyFormat("auto c = []() {\n"
20142                "  return b;\n"
20143                "};",
20144                "auto c = []() {\n"
20145                "  return b;\n"
20146                " };",
20147                MergeEmptyOnly);
20148   verifyFormat("auto c = []() {};",
20149                "auto c = []() {\n"
20150                "};",
20151                MergeEmptyOnly);
20152 
20153   FormatStyle MergeInline = getLLVMStyle();
20154   MergeInline.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Inline;
20155   verifyFormat("auto c = []() {\n"
20156                "  return b;\n"
20157                "};",
20158                "auto c = []() { return b; };", MergeInline);
20159   verifyFormat("function([]() { return b; })", "function([]() { return b; })",
20160                MergeInline);
20161   verifyFormat("function([]() { return b; }, a)",
20162                "function([]() { return b; }, a)", MergeInline);
20163   verifyFormat("function(a, []() { return b; })",
20164                "function(a, []() { return b; })", MergeInline);
20165 
20166   // Check option "BraceWrapping.BeforeLambdaBody" and different state of
20167   // AllowShortLambdasOnASingleLine
20168   FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle();
20169   LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
20170   LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
20171   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
20172       FormatStyle::ShortLambdaStyle::SLS_None;
20173   verifyFormat("FctWithOneNestedLambdaInline_SLS_None(\n"
20174                "    []()\n"
20175                "    {\n"
20176                "      return 17;\n"
20177                "    });",
20178                LLVMWithBeforeLambdaBody);
20179   verifyFormat("FctWithOneNestedLambdaEmpty_SLS_None(\n"
20180                "    []()\n"
20181                "    {\n"
20182                "    });",
20183                LLVMWithBeforeLambdaBody);
20184   verifyFormat("auto fct_SLS_None = []()\n"
20185                "{\n"
20186                "  return 17;\n"
20187                "};",
20188                LLVMWithBeforeLambdaBody);
20189   verifyFormat("TwoNestedLambdas_SLS_None(\n"
20190                "    []()\n"
20191                "    {\n"
20192                "      return Call(\n"
20193                "          []()\n"
20194                "          {\n"
20195                "            return 17;\n"
20196                "          });\n"
20197                "    });",
20198                LLVMWithBeforeLambdaBody);
20199   verifyFormat("void Fct() {\n"
20200                "  return {[]()\n"
20201                "          {\n"
20202                "            return 17;\n"
20203                "          }};\n"
20204                "}",
20205                LLVMWithBeforeLambdaBody);
20206 
20207   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
20208       FormatStyle::ShortLambdaStyle::SLS_Empty;
20209   verifyFormat("FctWithOneNestedLambdaInline_SLS_Empty(\n"
20210                "    []()\n"
20211                "    {\n"
20212                "      return 17;\n"
20213                "    });",
20214                LLVMWithBeforeLambdaBody);
20215   verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Empty([]() {});",
20216                LLVMWithBeforeLambdaBody);
20217   verifyFormat("FctWithOneNestedLambdaEmptyInsideAVeryVeryVeryVeryVeryVeryVeryL"
20218                "ongFunctionName_SLS_Empty(\n"
20219                "    []() {});",
20220                LLVMWithBeforeLambdaBody);
20221   verifyFormat("FctWithMultipleParams_SLS_Empty(A, B,\n"
20222                "                                []()\n"
20223                "                                {\n"
20224                "                                  return 17;\n"
20225                "                                });",
20226                LLVMWithBeforeLambdaBody);
20227   verifyFormat("auto fct_SLS_Empty = []()\n"
20228                "{\n"
20229                "  return 17;\n"
20230                "};",
20231                LLVMWithBeforeLambdaBody);
20232   verifyFormat("TwoNestedLambdas_SLS_Empty(\n"
20233                "    []()\n"
20234                "    {\n"
20235                "      return Call([]() {});\n"
20236                "    });",
20237                LLVMWithBeforeLambdaBody);
20238   verifyFormat("TwoNestedLambdas_SLS_Empty(A,\n"
20239                "                           []()\n"
20240                "                           {\n"
20241                "                             return Call([]() {});\n"
20242                "                           });",
20243                LLVMWithBeforeLambdaBody);
20244   verifyFormat(
20245       "FctWithLongLineInLambda_SLS_Empty(\n"
20246       "    []()\n"
20247       "    {\n"
20248       "      return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
20249       "                               AndShouldNotBeConsiderAsInline,\n"
20250       "                               LambdaBodyMustBeBreak);\n"
20251       "    });",
20252       LLVMWithBeforeLambdaBody);
20253 
20254   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
20255       FormatStyle::ShortLambdaStyle::SLS_Inline;
20256   verifyFormat("FctWithOneNestedLambdaInline_SLS_Inline([]() { return 17; });",
20257                LLVMWithBeforeLambdaBody);
20258   verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Inline([]() {});",
20259                LLVMWithBeforeLambdaBody);
20260   verifyFormat("auto fct_SLS_Inline = []()\n"
20261                "{\n"
20262                "  return 17;\n"
20263                "};",
20264                LLVMWithBeforeLambdaBody);
20265   verifyFormat("TwoNestedLambdas_SLS_Inline([]() { return Call([]() { return "
20266                "17; }); });",
20267                LLVMWithBeforeLambdaBody);
20268   verifyFormat(
20269       "FctWithLongLineInLambda_SLS_Inline(\n"
20270       "    []()\n"
20271       "    {\n"
20272       "      return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
20273       "                               AndShouldNotBeConsiderAsInline,\n"
20274       "                               LambdaBodyMustBeBreak);\n"
20275       "    });",
20276       LLVMWithBeforeLambdaBody);
20277   verifyFormat("FctWithMultipleParams_SLS_Inline("
20278                "VeryLongParameterThatShouldAskToBeOnMultiLine,\n"
20279                "                                 []() { return 17; });",
20280                LLVMWithBeforeLambdaBody);
20281   verifyFormat(
20282       "FctWithMultipleParams_SLS_Inline(FirstParam, []() { return 17; });",
20283       LLVMWithBeforeLambdaBody);
20284 
20285   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
20286       FormatStyle::ShortLambdaStyle::SLS_All;
20287   verifyFormat("FctWithOneNestedLambdaInline_SLS_All([]() { return 17; });",
20288                LLVMWithBeforeLambdaBody);
20289   verifyFormat("FctWithOneNestedLambdaEmpty_SLS_All([]() {});",
20290                LLVMWithBeforeLambdaBody);
20291   verifyFormat("auto fct_SLS_All = []() { return 17; };",
20292                LLVMWithBeforeLambdaBody);
20293   verifyFormat("FctWithOneParam_SLS_All(\n"
20294                "    []()\n"
20295                "    {\n"
20296                "      // A cool function...\n"
20297                "      return 43;\n"
20298                "    });",
20299                LLVMWithBeforeLambdaBody);
20300   verifyFormat("FctWithMultipleParams_SLS_All("
20301                "VeryLongParameterThatShouldAskToBeOnMultiLine,\n"
20302                "                              []() { return 17; });",
20303                LLVMWithBeforeLambdaBody);
20304   verifyFormat("FctWithMultipleParams_SLS_All(A, []() { return 17; });",
20305                LLVMWithBeforeLambdaBody);
20306   verifyFormat("FctWithMultipleParams_SLS_All(A, B, []() { return 17; });",
20307                LLVMWithBeforeLambdaBody);
20308   verifyFormat(
20309       "FctWithLongLineInLambda_SLS_All(\n"
20310       "    []()\n"
20311       "    {\n"
20312       "      return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
20313       "                               AndShouldNotBeConsiderAsInline,\n"
20314       "                               LambdaBodyMustBeBreak);\n"
20315       "    });",
20316       LLVMWithBeforeLambdaBody);
20317   verifyFormat(
20318       "auto fct_SLS_All = []()\n"
20319       "{\n"
20320       "  return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
20321       "                           AndShouldNotBeConsiderAsInline,\n"
20322       "                           LambdaBodyMustBeBreak);\n"
20323       "};",
20324       LLVMWithBeforeLambdaBody);
20325   LLVMWithBeforeLambdaBody.BinPackParameters = false;
20326   verifyFormat("FctAllOnSameLine_SLS_All([]() { return S; }, Fst, Second);",
20327                LLVMWithBeforeLambdaBody);
20328   verifyFormat(
20329       "FctWithLongLineInLambda_SLS_All([]() { return SomeValueNotSoLong; },\n"
20330       "                                FirstParam,\n"
20331       "                                SecondParam,\n"
20332       "                                ThirdParam,\n"
20333       "                                FourthParam);",
20334       LLVMWithBeforeLambdaBody);
20335   verifyFormat("FctWithLongLineInLambda_SLS_All(\n"
20336                "    []() { return "
20337                "SomeValueVeryVeryVeryVeryVeryVeryVeryVeryVeryLong; },\n"
20338                "    FirstParam,\n"
20339                "    SecondParam,\n"
20340                "    ThirdParam,\n"
20341                "    FourthParam);",
20342                LLVMWithBeforeLambdaBody);
20343   verifyFormat(
20344       "FctWithLongLineInLambda_SLS_All(FirstParam,\n"
20345       "                                SecondParam,\n"
20346       "                                ThirdParam,\n"
20347       "                                FourthParam,\n"
20348       "                                []() { return SomeValueNotSoLong; });",
20349       LLVMWithBeforeLambdaBody);
20350   verifyFormat("FctWithLongLineInLambda_SLS_All(\n"
20351                "    []()\n"
20352                "    {\n"
20353                "      return "
20354                "HereAVeryLongLineThatWillBeFormattedOnMultipleLineAndShouldNotB"
20355                "eConsiderAsInline;\n"
20356                "    });",
20357                LLVMWithBeforeLambdaBody);
20358   verifyFormat(
20359       "FctWithLongLineInLambda_SLS_All(\n"
20360       "    []()\n"
20361       "    {\n"
20362       "      return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
20363       "                               AndShouldNotBeConsiderAsInline,\n"
20364       "                               LambdaBodyMustBeBreak);\n"
20365       "    });",
20366       LLVMWithBeforeLambdaBody);
20367   verifyFormat("FctWithTwoParams_SLS_All(\n"
20368                "    []()\n"
20369                "    {\n"
20370                "      // A cool function...\n"
20371                "      return 43;\n"
20372                "    },\n"
20373                "    87);",
20374                LLVMWithBeforeLambdaBody);
20375   verifyFormat("FctWithTwoParams_SLS_All([]() { return 43; }, 87);",
20376                LLVMWithBeforeLambdaBody);
20377   verifyFormat("FctWithOneNestedLambdas_SLS_All([]() { return 17; });",
20378                LLVMWithBeforeLambdaBody);
20379   verifyFormat(
20380       "TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; }); });",
20381       LLVMWithBeforeLambdaBody);
20382   verifyFormat("TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; "
20383                "}); }, x);",
20384                LLVMWithBeforeLambdaBody);
20385   verifyFormat("TwoNestedLambdas_SLS_All(\n"
20386                "    []()\n"
20387                "    {\n"
20388                "      // A cool function...\n"
20389                "      return Call([]() { return 17; });\n"
20390                "    });",
20391                LLVMWithBeforeLambdaBody);
20392   verifyFormat("TwoNestedLambdas_SLS_All(\n"
20393                "    []()\n"
20394                "    {\n"
20395                "      return Call(\n"
20396                "          []()\n"
20397                "          {\n"
20398                "            // A cool function...\n"
20399                "            return 17;\n"
20400                "          });\n"
20401                "    });",
20402                LLVMWithBeforeLambdaBody);
20403 
20404   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
20405       FormatStyle::ShortLambdaStyle::SLS_None;
20406 
20407   verifyFormat("auto select = [this]() -> const Library::Object *\n"
20408                "{\n"
20409                "  return MyAssignment::SelectFromList(this);\n"
20410                "};\n",
20411                LLVMWithBeforeLambdaBody);
20412 
20413   verifyFormat("auto select = [this]() -> const Library::Object &\n"
20414                "{\n"
20415                "  return MyAssignment::SelectFromList(this);\n"
20416                "};\n",
20417                LLVMWithBeforeLambdaBody);
20418 
20419   verifyFormat("auto select = [this]() -> std::unique_ptr<Object>\n"
20420                "{\n"
20421                "  return MyAssignment::SelectFromList(this);\n"
20422                "};\n",
20423                LLVMWithBeforeLambdaBody);
20424 
20425   verifyFormat("namespace test {\n"
20426                "class Test {\n"
20427                "public:\n"
20428                "  Test() = default;\n"
20429                "};\n"
20430                "} // namespace test",
20431                LLVMWithBeforeLambdaBody);
20432 
20433   // Lambdas with different indentation styles.
20434   Style = getLLVMStyleWithColumns(100);
20435   EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n"
20436             "  return promise.then(\n"
20437             "      [this, &someVariable, someObject = "
20438             "std::mv(s)](std::vector<int> evaluated) mutable {\n"
20439             "        return someObject.startAsyncAction().then(\n"
20440             "            [this, &someVariable](AsyncActionResult result) "
20441             "mutable { result.processMore(); });\n"
20442             "      });\n"
20443             "}\n",
20444             format("SomeResult doSomething(SomeObject promise) {\n"
20445                    "  return promise.then([this, &someVariable, someObject = "
20446                    "std::mv(s)](std::vector<int> evaluated) mutable {\n"
20447                    "    return someObject.startAsyncAction().then([this, "
20448                    "&someVariable](AsyncActionResult result) mutable {\n"
20449                    "      result.processMore();\n"
20450                    "    });\n"
20451                    "  });\n"
20452                    "}\n",
20453                    Style));
20454   Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope;
20455   verifyFormat("test() {\n"
20456                "  ([]() -> {\n"
20457                "    int b = 32;\n"
20458                "    return 3;\n"
20459                "  }).foo();\n"
20460                "}",
20461                Style);
20462   verifyFormat("test() {\n"
20463                "  []() -> {\n"
20464                "    int b = 32;\n"
20465                "    return 3;\n"
20466                "  }\n"
20467                "}",
20468                Style);
20469   verifyFormat("std::sort(v.begin(), v.end(),\n"
20470                "          [](const auto &someLongArgumentName, const auto "
20471                "&someOtherLongArgumentName) {\n"
20472                "  return someLongArgumentName.someMemberVariable < "
20473                "someOtherLongArgumentName.someMemberVariable;\n"
20474                "});",
20475                Style);
20476   verifyFormat("test() {\n"
20477                "  (\n"
20478                "      []() -> {\n"
20479                "        int b = 32;\n"
20480                "        return 3;\n"
20481                "      },\n"
20482                "      foo, bar)\n"
20483                "      .foo();\n"
20484                "}",
20485                Style);
20486   verifyFormat("test() {\n"
20487                "  ([]() -> {\n"
20488                "    int b = 32;\n"
20489                "    return 3;\n"
20490                "  })\n"
20491                "      .foo()\n"
20492                "      .bar();\n"
20493                "}",
20494                Style);
20495   EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n"
20496             "  return promise.then(\n"
20497             "      [this, &someVariable, someObject = "
20498             "std::mv(s)](std::vector<int> evaluated) mutable {\n"
20499             "    return someObject.startAsyncAction().then(\n"
20500             "        [this, &someVariable](AsyncActionResult result) mutable { "
20501             "result.processMore(); });\n"
20502             "  });\n"
20503             "}\n",
20504             format("SomeResult doSomething(SomeObject promise) {\n"
20505                    "  return promise.then([this, &someVariable, someObject = "
20506                    "std::mv(s)](std::vector<int> evaluated) mutable {\n"
20507                    "    return someObject.startAsyncAction().then([this, "
20508                    "&someVariable](AsyncActionResult result) mutable {\n"
20509                    "      result.processMore();\n"
20510                    "    });\n"
20511                    "  });\n"
20512                    "}\n",
20513                    Style));
20514   EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n"
20515             "  return promise.then([this, &someVariable] {\n"
20516             "    return someObject.startAsyncAction().then(\n"
20517             "        [this, &someVariable](AsyncActionResult result) mutable { "
20518             "result.processMore(); });\n"
20519             "  });\n"
20520             "}\n",
20521             format("SomeResult doSomething(SomeObject promise) {\n"
20522                    "  return promise.then([this, &someVariable] {\n"
20523                    "    return someObject.startAsyncAction().then([this, "
20524                    "&someVariable](AsyncActionResult result) mutable {\n"
20525                    "      result.processMore();\n"
20526                    "    });\n"
20527                    "  });\n"
20528                    "}\n",
20529                    Style));
20530   Style = getGoogleStyle();
20531   Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope;
20532   EXPECT_EQ("#define A                                       \\\n"
20533             "  [] {                                          \\\n"
20534             "    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(        \\\n"
20535             "        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
20536             "      }",
20537             format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
20538                    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
20539                    Style));
20540   // TODO: The current formatting has a minor issue that's not worth fixing
20541   // right now whereby the closing brace is indented relative to the signature
20542   // instead of being aligned. This only happens with macros.
20543 }
20544 
20545 TEST_F(FormatTest, LambdaWithLineComments) {
20546   FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle();
20547   LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
20548   LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
20549   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
20550       FormatStyle::ShortLambdaStyle::SLS_All;
20551 
20552   verifyFormat("auto k = []() { return; }", LLVMWithBeforeLambdaBody);
20553   verifyFormat("auto k = []() // comment\n"
20554                "{ return; }",
20555                LLVMWithBeforeLambdaBody);
20556   verifyFormat("auto k = []() /* comment */ { return; }",
20557                LLVMWithBeforeLambdaBody);
20558   verifyFormat("auto k = []() /* comment */ /* comment */ { return; }",
20559                LLVMWithBeforeLambdaBody);
20560   verifyFormat("auto k = []() // X\n"
20561                "{ return; }",
20562                LLVMWithBeforeLambdaBody);
20563   verifyFormat(
20564       "auto k = []() // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"
20565       "{ return; }",
20566       LLVMWithBeforeLambdaBody);
20567 }
20568 
20569 TEST_F(FormatTest, EmptyLinesInLambdas) {
20570   verifyFormat("auto lambda = []() {\n"
20571                "  x(); //\n"
20572                "};",
20573                "auto lambda = []() {\n"
20574                "\n"
20575                "  x(); //\n"
20576                "\n"
20577                "};");
20578 }
20579 
20580 TEST_F(FormatTest, FormatsBlocks) {
20581   FormatStyle ShortBlocks = getLLVMStyle();
20582   ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
20583   verifyFormat("int (^Block)(int, int);", ShortBlocks);
20584   verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
20585   verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
20586   verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks);
20587   verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks);
20588   verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks);
20589 
20590   verifyFormat("foo(^{ bar(); });", ShortBlocks);
20591   verifyFormat("foo(a, ^{ bar(); });", ShortBlocks);
20592   verifyFormat("{ void (^block)(Object *x); }", ShortBlocks);
20593 
20594   verifyFormat("[operation setCompletionBlock:^{\n"
20595                "  [self onOperationDone];\n"
20596                "}];");
20597   verifyFormat("int i = {[operation setCompletionBlock:^{\n"
20598                "  [self onOperationDone];\n"
20599                "}]};");
20600   verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
20601                "  f();\n"
20602                "}];");
20603   verifyFormat("int a = [operation block:^int(int *i) {\n"
20604                "  return 1;\n"
20605                "}];");
20606   verifyFormat("[myObject doSomethingWith:arg1\n"
20607                "                      aaa:^int(int *a) {\n"
20608                "                        return 1;\n"
20609                "                      }\n"
20610                "                      bbb:f(a * bbbbbbbb)];");
20611 
20612   verifyFormat("[operation setCompletionBlock:^{\n"
20613                "  [self.delegate newDataAvailable];\n"
20614                "}];",
20615                getLLVMStyleWithColumns(60));
20616   verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
20617                "  NSString *path = [self sessionFilePath];\n"
20618                "  if (path) {\n"
20619                "    // ...\n"
20620                "  }\n"
20621                "});");
20622   verifyFormat("[[SessionService sharedService]\n"
20623                "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
20624                "      if (window) {\n"
20625                "        [self windowDidLoad:window];\n"
20626                "      } else {\n"
20627                "        [self errorLoadingWindow];\n"
20628                "      }\n"
20629                "    }];");
20630   verifyFormat("void (^largeBlock)(void) = ^{\n"
20631                "  // ...\n"
20632                "};\n",
20633                getLLVMStyleWithColumns(40));
20634   verifyFormat("[[SessionService sharedService]\n"
20635                "    loadWindowWithCompletionBlock: //\n"
20636                "        ^(SessionWindow *window) {\n"
20637                "          if (window) {\n"
20638                "            [self windowDidLoad:window];\n"
20639                "          } else {\n"
20640                "            [self errorLoadingWindow];\n"
20641                "          }\n"
20642                "        }];",
20643                getLLVMStyleWithColumns(60));
20644   verifyFormat("[myObject doSomethingWith:arg1\n"
20645                "    firstBlock:^(Foo *a) {\n"
20646                "      // ...\n"
20647                "      int i;\n"
20648                "    }\n"
20649                "    secondBlock:^(Bar *b) {\n"
20650                "      // ...\n"
20651                "      int i;\n"
20652                "    }\n"
20653                "    thirdBlock:^Foo(Bar *b) {\n"
20654                "      // ...\n"
20655                "      int i;\n"
20656                "    }];");
20657   verifyFormat("[myObject doSomethingWith:arg1\n"
20658                "               firstBlock:-1\n"
20659                "              secondBlock:^(Bar *b) {\n"
20660                "                // ...\n"
20661                "                int i;\n"
20662                "              }];");
20663 
20664   verifyFormat("f(^{\n"
20665                "  @autoreleasepool {\n"
20666                "    if (a) {\n"
20667                "      g();\n"
20668                "    }\n"
20669                "  }\n"
20670                "});");
20671   verifyFormat("Block b = ^int *(A *a, B *b) {}");
20672   verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n"
20673                "};");
20674 
20675   FormatStyle FourIndent = getLLVMStyle();
20676   FourIndent.ObjCBlockIndentWidth = 4;
20677   verifyFormat("[operation setCompletionBlock:^{\n"
20678                "    [self onOperationDone];\n"
20679                "}];",
20680                FourIndent);
20681 }
20682 
20683 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) {
20684   FormatStyle ZeroColumn = getLLVMStyle();
20685   ZeroColumn.ColumnLimit = 0;
20686 
20687   verifyFormat("[[SessionService sharedService] "
20688                "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
20689                "  if (window) {\n"
20690                "    [self windowDidLoad:window];\n"
20691                "  } else {\n"
20692                "    [self errorLoadingWindow];\n"
20693                "  }\n"
20694                "}];",
20695                ZeroColumn);
20696   EXPECT_EQ("[[SessionService sharedService]\n"
20697             "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
20698             "      if (window) {\n"
20699             "        [self windowDidLoad:window];\n"
20700             "      } else {\n"
20701             "        [self errorLoadingWindow];\n"
20702             "      }\n"
20703             "    }];",
20704             format("[[SessionService sharedService]\n"
20705                    "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
20706                    "                if (window) {\n"
20707                    "    [self windowDidLoad:window];\n"
20708                    "  } else {\n"
20709                    "    [self errorLoadingWindow];\n"
20710                    "  }\n"
20711                    "}];",
20712                    ZeroColumn));
20713   verifyFormat("[myObject doSomethingWith:arg1\n"
20714                "    firstBlock:^(Foo *a) {\n"
20715                "      // ...\n"
20716                "      int i;\n"
20717                "    }\n"
20718                "    secondBlock:^(Bar *b) {\n"
20719                "      // ...\n"
20720                "      int i;\n"
20721                "    }\n"
20722                "    thirdBlock:^Foo(Bar *b) {\n"
20723                "      // ...\n"
20724                "      int i;\n"
20725                "    }];",
20726                ZeroColumn);
20727   verifyFormat("f(^{\n"
20728                "  @autoreleasepool {\n"
20729                "    if (a) {\n"
20730                "      g();\n"
20731                "    }\n"
20732                "  }\n"
20733                "});",
20734                ZeroColumn);
20735   verifyFormat("void (^largeBlock)(void) = ^{\n"
20736                "  // ...\n"
20737                "};",
20738                ZeroColumn);
20739 
20740   ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
20741   EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };",
20742             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
20743   ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
20744   EXPECT_EQ("void (^largeBlock)(void) = ^{\n"
20745             "  int i;\n"
20746             "};",
20747             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
20748 }
20749 
20750 TEST_F(FormatTest, SupportsCRLF) {
20751   EXPECT_EQ("int a;\r\n"
20752             "int b;\r\n"
20753             "int c;\r\n",
20754             format("int a;\r\n"
20755                    "  int b;\r\n"
20756                    "    int c;\r\n",
20757                    getLLVMStyle()));
20758   EXPECT_EQ("int a;\r\n"
20759             "int b;\r\n"
20760             "int c;\r\n",
20761             format("int a;\r\n"
20762                    "  int b;\n"
20763                    "    int c;\r\n",
20764                    getLLVMStyle()));
20765   EXPECT_EQ("int a;\n"
20766             "int b;\n"
20767             "int c;\n",
20768             format("int a;\r\n"
20769                    "  int b;\n"
20770                    "    int c;\n",
20771                    getLLVMStyle()));
20772   EXPECT_EQ("\"aaaaaaa \"\r\n"
20773             "\"bbbbbbb\";\r\n",
20774             format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
20775   EXPECT_EQ("#define A \\\r\n"
20776             "  b;      \\\r\n"
20777             "  c;      \\\r\n"
20778             "  d;\r\n",
20779             format("#define A \\\r\n"
20780                    "  b; \\\r\n"
20781                    "  c; d; \r\n",
20782                    getGoogleStyle()));
20783 
20784   EXPECT_EQ("/*\r\n"
20785             "multi line block comments\r\n"
20786             "should not introduce\r\n"
20787             "an extra carriage return\r\n"
20788             "*/\r\n",
20789             format("/*\r\n"
20790                    "multi line block comments\r\n"
20791                    "should not introduce\r\n"
20792                    "an extra carriage return\r\n"
20793                    "*/\r\n"));
20794   EXPECT_EQ("/*\r\n"
20795             "\r\n"
20796             "*/",
20797             format("/*\r\n"
20798                    "    \r\r\r\n"
20799                    "*/"));
20800 
20801   FormatStyle style = getLLVMStyle();
20802 
20803   style.DeriveLineEnding = true;
20804   style.UseCRLF = false;
20805   EXPECT_EQ("union FooBarBazQux {\n"
20806             "  int foo;\n"
20807             "  int bar;\n"
20808             "  int baz;\n"
20809             "};",
20810             format("union FooBarBazQux {\r\n"
20811                    "  int foo;\n"
20812                    "  int bar;\r\n"
20813                    "  int baz;\n"
20814                    "};",
20815                    style));
20816   style.UseCRLF = true;
20817   EXPECT_EQ("union FooBarBazQux {\r\n"
20818             "  int foo;\r\n"
20819             "  int bar;\r\n"
20820             "  int baz;\r\n"
20821             "};",
20822             format("union FooBarBazQux {\r\n"
20823                    "  int foo;\n"
20824                    "  int bar;\r\n"
20825                    "  int baz;\n"
20826                    "};",
20827                    style));
20828 
20829   style.DeriveLineEnding = false;
20830   style.UseCRLF = false;
20831   EXPECT_EQ("union FooBarBazQux {\n"
20832             "  int foo;\n"
20833             "  int bar;\n"
20834             "  int baz;\n"
20835             "  int qux;\n"
20836             "};",
20837             format("union FooBarBazQux {\r\n"
20838                    "  int foo;\n"
20839                    "  int bar;\r\n"
20840                    "  int baz;\n"
20841                    "  int qux;\r\n"
20842                    "};",
20843                    style));
20844   style.UseCRLF = true;
20845   EXPECT_EQ("union FooBarBazQux {\r\n"
20846             "  int foo;\r\n"
20847             "  int bar;\r\n"
20848             "  int baz;\r\n"
20849             "  int qux;\r\n"
20850             "};",
20851             format("union FooBarBazQux {\r\n"
20852                    "  int foo;\n"
20853                    "  int bar;\r\n"
20854                    "  int baz;\n"
20855                    "  int qux;\n"
20856                    "};",
20857                    style));
20858 
20859   style.DeriveLineEnding = true;
20860   style.UseCRLF = false;
20861   EXPECT_EQ("union FooBarBazQux {\r\n"
20862             "  int foo;\r\n"
20863             "  int bar;\r\n"
20864             "  int baz;\r\n"
20865             "  int qux;\r\n"
20866             "};",
20867             format("union FooBarBazQux {\r\n"
20868                    "  int foo;\n"
20869                    "  int bar;\r\n"
20870                    "  int baz;\n"
20871                    "  int qux;\r\n"
20872                    "};",
20873                    style));
20874   style.UseCRLF = true;
20875   EXPECT_EQ("union FooBarBazQux {\n"
20876             "  int foo;\n"
20877             "  int bar;\n"
20878             "  int baz;\n"
20879             "  int qux;\n"
20880             "};",
20881             format("union FooBarBazQux {\r\n"
20882                    "  int foo;\n"
20883                    "  int bar;\r\n"
20884                    "  int baz;\n"
20885                    "  int qux;\n"
20886                    "};",
20887                    style));
20888 }
20889 
20890 TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
20891   verifyFormat("MY_CLASS(C) {\n"
20892                "  int i;\n"
20893                "  int j;\n"
20894                "};");
20895 }
20896 
20897 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
20898   FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
20899   TwoIndent.ContinuationIndentWidth = 2;
20900 
20901   EXPECT_EQ("int i =\n"
20902             "  longFunction(\n"
20903             "    arg);",
20904             format("int i = longFunction(arg);", TwoIndent));
20905 
20906   FormatStyle SixIndent = getLLVMStyleWithColumns(20);
20907   SixIndent.ContinuationIndentWidth = 6;
20908 
20909   EXPECT_EQ("int i =\n"
20910             "      longFunction(\n"
20911             "            arg);",
20912             format("int i = longFunction(arg);", SixIndent));
20913 }
20914 
20915 TEST_F(FormatTest, WrappedClosingParenthesisIndent) {
20916   FormatStyle Style = getLLVMStyle();
20917   verifyFormat("int Foo::getter(\n"
20918                "    //\n"
20919                ") const {\n"
20920                "  return foo;\n"
20921                "}",
20922                Style);
20923   verifyFormat("void Foo::setter(\n"
20924                "    //\n"
20925                ") {\n"
20926                "  foo = 1;\n"
20927                "}",
20928                Style);
20929 }
20930 
20931 TEST_F(FormatTest, SpacesInAngles) {
20932   FormatStyle Spaces = getLLVMStyle();
20933   Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
20934 
20935   verifyFormat("vector< ::std::string > x1;", Spaces);
20936   verifyFormat("Foo< int, Bar > x2;", Spaces);
20937   verifyFormat("Foo< ::int, ::Bar > x3;", Spaces);
20938 
20939   verifyFormat("static_cast< int >(arg);", Spaces);
20940   verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
20941   verifyFormat("f< int, float >();", Spaces);
20942   verifyFormat("template <> g() {}", Spaces);
20943   verifyFormat("template < std::vector< int > > f() {}", Spaces);
20944   verifyFormat("std::function< void(int, int) > fct;", Spaces);
20945   verifyFormat("void inFunction() { std::function< void(int, int) > fct; }",
20946                Spaces);
20947 
20948   Spaces.Standard = FormatStyle::LS_Cpp03;
20949   Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
20950   verifyFormat("A< A< int > >();", Spaces);
20951 
20952   Spaces.SpacesInAngles = FormatStyle::SIAS_Never;
20953   verifyFormat("A<A<int> >();", Spaces);
20954 
20955   Spaces.SpacesInAngles = FormatStyle::SIAS_Leave;
20956   verifyFormat("vector< ::std::string> x4;", "vector<::std::string> x4;",
20957                Spaces);
20958   verifyFormat("vector< ::std::string > x4;", "vector<::std::string > x4;",
20959                Spaces);
20960 
20961   verifyFormat("A<A<int> >();", Spaces);
20962   verifyFormat("A<A<int> >();", "A<A<int>>();", Spaces);
20963   verifyFormat("A< A< int > >();", Spaces);
20964 
20965   Spaces.Standard = FormatStyle::LS_Cpp11;
20966   Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
20967   verifyFormat("A< A< int > >();", Spaces);
20968 
20969   Spaces.SpacesInAngles = FormatStyle::SIAS_Never;
20970   verifyFormat("vector<::std::string> x4;", Spaces);
20971   verifyFormat("vector<int> x5;", Spaces);
20972   verifyFormat("Foo<int, Bar> x6;", Spaces);
20973   verifyFormat("Foo<::int, ::Bar> x7;", Spaces);
20974 
20975   verifyFormat("A<A<int>>();", Spaces);
20976 
20977   Spaces.SpacesInAngles = FormatStyle::SIAS_Leave;
20978   verifyFormat("vector<::std::string> x4;", Spaces);
20979   verifyFormat("vector< ::std::string > x4;", Spaces);
20980   verifyFormat("vector<int> x5;", Spaces);
20981   verifyFormat("vector< int > x5;", Spaces);
20982   verifyFormat("Foo<int, Bar> x6;", Spaces);
20983   verifyFormat("Foo< int, Bar > x6;", Spaces);
20984   verifyFormat("Foo<::int, ::Bar> x7;", Spaces);
20985   verifyFormat("Foo< ::int, ::Bar > x7;", Spaces);
20986 
20987   verifyFormat("A<A<int>>();", Spaces);
20988   verifyFormat("A< A< int > >();", Spaces);
20989   verifyFormat("A<A<int > >();", Spaces);
20990   verifyFormat("A< A< int>>();", Spaces);
20991 }
20992 
20993 TEST_F(FormatTest, SpaceAfterTemplateKeyword) {
20994   FormatStyle Style = getLLVMStyle();
20995   Style.SpaceAfterTemplateKeyword = false;
20996   verifyFormat("template<int> void foo();", Style);
20997 }
20998 
20999 TEST_F(FormatTest, TripleAngleBrackets) {
21000   verifyFormat("f<<<1, 1>>>();");
21001   verifyFormat("f<<<1, 1, 1, s>>>();");
21002   verifyFormat("f<<<a, b, c, d>>>();");
21003   EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();"));
21004   verifyFormat("f<param><<<1, 1>>>();");
21005   verifyFormat("f<1><<<1, 1>>>();");
21006   EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();"));
21007   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
21008                "aaaaaaaaaaa<<<\n    1, 1>>>();");
21009   verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n"
21010                "    <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();");
21011 }
21012 
21013 TEST_F(FormatTest, MergeLessLessAtEnd) {
21014   verifyFormat("<<");
21015   EXPECT_EQ("< < <", format("\\\n<<<"));
21016   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
21017                "aaallvm::outs() <<");
21018   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
21019                "aaaallvm::outs()\n    <<");
21020 }
21021 
21022 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
21023   std::string code = "#if A\n"
21024                      "#if B\n"
21025                      "a.\n"
21026                      "#endif\n"
21027                      "    a = 1;\n"
21028                      "#else\n"
21029                      "#endif\n"
21030                      "#if C\n"
21031                      "#else\n"
21032                      "#endif\n";
21033   EXPECT_EQ(code, format(code));
21034 }
21035 
21036 TEST_F(FormatTest, HandleConflictMarkers) {
21037   // Git/SVN conflict markers.
21038   EXPECT_EQ("int a;\n"
21039             "void f() {\n"
21040             "  callme(some(parameter1,\n"
21041             "<<<<<<< text by the vcs\n"
21042             "              parameter2),\n"
21043             "||||||| text by the vcs\n"
21044             "              parameter2),\n"
21045             "         parameter3,\n"
21046             "======= text by the vcs\n"
21047             "              parameter2, parameter3),\n"
21048             ">>>>>>> text by the vcs\n"
21049             "         otherparameter);\n",
21050             format("int a;\n"
21051                    "void f() {\n"
21052                    "  callme(some(parameter1,\n"
21053                    "<<<<<<< text by the vcs\n"
21054                    "  parameter2),\n"
21055                    "||||||| text by the vcs\n"
21056                    "  parameter2),\n"
21057                    "  parameter3,\n"
21058                    "======= text by the vcs\n"
21059                    "  parameter2,\n"
21060                    "  parameter3),\n"
21061                    ">>>>>>> text by the vcs\n"
21062                    "  otherparameter);\n"));
21063 
21064   // Perforce markers.
21065   EXPECT_EQ("void f() {\n"
21066             "  function(\n"
21067             ">>>> text by the vcs\n"
21068             "      parameter,\n"
21069             "==== text by the vcs\n"
21070             "      parameter,\n"
21071             "==== text by the vcs\n"
21072             "      parameter,\n"
21073             "<<<< text by the vcs\n"
21074             "      parameter);\n",
21075             format("void f() {\n"
21076                    "  function(\n"
21077                    ">>>> text by the vcs\n"
21078                    "  parameter,\n"
21079                    "==== text by the vcs\n"
21080                    "  parameter,\n"
21081                    "==== text by the vcs\n"
21082                    "  parameter,\n"
21083                    "<<<< text by the vcs\n"
21084                    "  parameter);\n"));
21085 
21086   EXPECT_EQ("<<<<<<<\n"
21087             "|||||||\n"
21088             "=======\n"
21089             ">>>>>>>",
21090             format("<<<<<<<\n"
21091                    "|||||||\n"
21092                    "=======\n"
21093                    ">>>>>>>"));
21094 
21095   EXPECT_EQ("<<<<<<<\n"
21096             "|||||||\n"
21097             "int i;\n"
21098             "=======\n"
21099             ">>>>>>>",
21100             format("<<<<<<<\n"
21101                    "|||||||\n"
21102                    "int i;\n"
21103                    "=======\n"
21104                    ">>>>>>>"));
21105 
21106   // FIXME: Handle parsing of macros around conflict markers correctly:
21107   EXPECT_EQ("#define Macro \\\n"
21108             "<<<<<<<\n"
21109             "Something \\\n"
21110             "|||||||\n"
21111             "Else \\\n"
21112             "=======\n"
21113             "Other \\\n"
21114             ">>>>>>>\n"
21115             "    End int i;\n",
21116             format("#define Macro \\\n"
21117                    "<<<<<<<\n"
21118                    "  Something \\\n"
21119                    "|||||||\n"
21120                    "  Else \\\n"
21121                    "=======\n"
21122                    "  Other \\\n"
21123                    ">>>>>>>\n"
21124                    "  End\n"
21125                    "int i;\n"));
21126 
21127   verifyFormat(R"(====
21128 #ifdef A
21129 a
21130 #else
21131 b
21132 #endif
21133 )");
21134 }
21135 
21136 TEST_F(FormatTest, DisableRegions) {
21137   EXPECT_EQ("int i;\n"
21138             "// clang-format off\n"
21139             "  int j;\n"
21140             "// clang-format on\n"
21141             "int k;",
21142             format(" int  i;\n"
21143                    "   // clang-format off\n"
21144                    "  int j;\n"
21145                    " // clang-format on\n"
21146                    "   int   k;"));
21147   EXPECT_EQ("int i;\n"
21148             "/* clang-format off */\n"
21149             "  int j;\n"
21150             "/* clang-format on */\n"
21151             "int k;",
21152             format(" int  i;\n"
21153                    "   /* clang-format off */\n"
21154                    "  int j;\n"
21155                    " /* clang-format on */\n"
21156                    "   int   k;"));
21157 
21158   // Don't reflow comments within disabled regions.
21159   EXPECT_EQ("// clang-format off\n"
21160             "// long long long long long long line\n"
21161             "/* clang-format on */\n"
21162             "/* long long long\n"
21163             " * long long long\n"
21164             " * line */\n"
21165             "int i;\n"
21166             "/* clang-format off */\n"
21167             "/* long long long long long long line */\n",
21168             format("// clang-format off\n"
21169                    "// long long long long long long line\n"
21170                    "/* clang-format on */\n"
21171                    "/* long long long long long long line */\n"
21172                    "int i;\n"
21173                    "/* clang-format off */\n"
21174                    "/* long long long long long long line */\n",
21175                    getLLVMStyleWithColumns(20)));
21176 }
21177 
21178 TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
21179   format("? ) =");
21180   verifyNoCrash("#define a\\\n /**/}");
21181 }
21182 
21183 TEST_F(FormatTest, FormatsTableGenCode) {
21184   FormatStyle Style = getLLVMStyle();
21185   Style.Language = FormatStyle::LK_TableGen;
21186   verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style);
21187 }
21188 
21189 TEST_F(FormatTest, ArrayOfTemplates) {
21190   EXPECT_EQ("auto a = new unique_ptr<int>[10];",
21191             format("auto a = new unique_ptr<int > [ 10];"));
21192 
21193   FormatStyle Spaces = getLLVMStyle();
21194   Spaces.SpacesInSquareBrackets = true;
21195   EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];",
21196             format("auto a = new unique_ptr<int > [10];", Spaces));
21197 }
21198 
21199 TEST_F(FormatTest, ArrayAsTemplateType) {
21200   EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;",
21201             format("auto a = unique_ptr < Foo < Bar>[ 10]> ;"));
21202 
21203   FormatStyle Spaces = getLLVMStyle();
21204   Spaces.SpacesInSquareBrackets = true;
21205   EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;",
21206             format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces));
21207 }
21208 
21209 TEST_F(FormatTest, NoSpaceAfterSuper) { verifyFormat("__super::FooBar();"); }
21210 
21211 TEST(FormatStyle, GetStyleWithEmptyFileName) {
21212   llvm::vfs::InMemoryFileSystem FS;
21213   auto Style1 = getStyle("file", "", "Google", "", &FS);
21214   ASSERT_TRUE((bool)Style1);
21215   ASSERT_EQ(*Style1, getGoogleStyle());
21216 }
21217 
21218 TEST(FormatStyle, GetStyleOfFile) {
21219   llvm::vfs::InMemoryFileSystem FS;
21220   // Test 1: format file in the same directory.
21221   ASSERT_TRUE(
21222       FS.addFile("/a/.clang-format", 0,
21223                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM")));
21224   ASSERT_TRUE(
21225       FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
21226   auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS);
21227   ASSERT_TRUE((bool)Style1);
21228   ASSERT_EQ(*Style1, getLLVMStyle());
21229 
21230   // Test 2.1: fallback to default.
21231   ASSERT_TRUE(
21232       FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
21233   auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS);
21234   ASSERT_TRUE((bool)Style2);
21235   ASSERT_EQ(*Style2, getMozillaStyle());
21236 
21237   // Test 2.2: no format on 'none' fallback style.
21238   Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
21239   ASSERT_TRUE((bool)Style2);
21240   ASSERT_EQ(*Style2, getNoStyle());
21241 
21242   // Test 2.3: format if config is found with no based style while fallback is
21243   // 'none'.
21244   ASSERT_TRUE(FS.addFile("/b/.clang-format", 0,
21245                          llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2")));
21246   Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
21247   ASSERT_TRUE((bool)Style2);
21248   ASSERT_EQ(*Style2, getLLVMStyle());
21249 
21250   // Test 2.4: format if yaml with no based style, while fallback is 'none'.
21251   Style2 = getStyle("{}", "a.h", "none", "", &FS);
21252   ASSERT_TRUE((bool)Style2);
21253   ASSERT_EQ(*Style2, getLLVMStyle());
21254 
21255   // Test 3: format file in parent directory.
21256   ASSERT_TRUE(
21257       FS.addFile("/c/.clang-format", 0,
21258                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google")));
21259   ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0,
21260                          llvm::MemoryBuffer::getMemBuffer("int i;")));
21261   auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS);
21262   ASSERT_TRUE((bool)Style3);
21263   ASSERT_EQ(*Style3, getGoogleStyle());
21264 
21265   // Test 4: error on invalid fallback style
21266   auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS);
21267   ASSERT_FALSE((bool)Style4);
21268   llvm::consumeError(Style4.takeError());
21269 
21270   // Test 5: error on invalid yaml on command line
21271   auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS);
21272   ASSERT_FALSE((bool)Style5);
21273   llvm::consumeError(Style5.takeError());
21274 
21275   // Test 6: error on invalid style
21276   auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS);
21277   ASSERT_FALSE((bool)Style6);
21278   llvm::consumeError(Style6.takeError());
21279 
21280   // Test 7: found config file, error on parsing it
21281   ASSERT_TRUE(
21282       FS.addFile("/d/.clang-format", 0,
21283                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n"
21284                                                   "InvalidKey: InvalidValue")));
21285   ASSERT_TRUE(
21286       FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
21287   auto Style7a = getStyle("file", "/d/.clang-format", "LLVM", "", &FS);
21288   ASSERT_FALSE((bool)Style7a);
21289   llvm::consumeError(Style7a.takeError());
21290 
21291   auto Style7b = getStyle("file", "/d/.clang-format", "LLVM", "", &FS, true);
21292   ASSERT_TRUE((bool)Style7b);
21293 
21294   // Test 8: inferred per-language defaults apply.
21295   auto StyleTd = getStyle("file", "x.td", "llvm", "", &FS);
21296   ASSERT_TRUE((bool)StyleTd);
21297   ASSERT_EQ(*StyleTd, getLLVMStyle(FormatStyle::LK_TableGen));
21298 
21299   // Test 9.1: overwriting a file style, when parent no file exists with no
21300   // fallback style
21301   ASSERT_TRUE(FS.addFile(
21302       "/e/sub/.clang-format", 0,
21303       llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: InheritParentConfig\n"
21304                                        "ColumnLimit: 20")));
21305   ASSERT_TRUE(FS.addFile("/e/sub/code.cpp", 0,
21306                          llvm::MemoryBuffer::getMemBuffer("int i;")));
21307   auto Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS);
21308   ASSERT_TRUE(static_cast<bool>(Style9));
21309   ASSERT_EQ(*Style9, [] {
21310     auto Style = getNoStyle();
21311     Style.ColumnLimit = 20;
21312     return Style;
21313   }());
21314 
21315   // Test 9.2: with LLVM fallback style
21316   Style9 = getStyle("file", "/e/sub/code.cpp", "LLVM", "", &FS);
21317   ASSERT_TRUE(static_cast<bool>(Style9));
21318   ASSERT_EQ(*Style9, [] {
21319     auto Style = getLLVMStyle();
21320     Style.ColumnLimit = 20;
21321     return Style;
21322   }());
21323 
21324   // Test 9.3: with a parent file
21325   ASSERT_TRUE(
21326       FS.addFile("/e/.clang-format", 0,
21327                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google\n"
21328                                                   "UseTab: Always")));
21329   Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS);
21330   ASSERT_TRUE(static_cast<bool>(Style9));
21331   ASSERT_EQ(*Style9, [] {
21332     auto Style = getGoogleStyle();
21333     Style.ColumnLimit = 20;
21334     Style.UseTab = FormatStyle::UT_Always;
21335     return Style;
21336   }());
21337 
21338   // Test 9.4: propagate more than one level
21339   ASSERT_TRUE(FS.addFile("/e/sub/sub/code.cpp", 0,
21340                          llvm::MemoryBuffer::getMemBuffer("int i;")));
21341   ASSERT_TRUE(FS.addFile("/e/sub/sub/.clang-format", 0,
21342                          llvm::MemoryBuffer::getMemBuffer(
21343                              "BasedOnStyle: InheritParentConfig\n"
21344                              "WhitespaceSensitiveMacros: ['FOO', 'BAR']")));
21345   std::vector<std::string> NonDefaultWhiteSpaceMacros{"FOO", "BAR"};
21346 
21347   const auto SubSubStyle = [&NonDefaultWhiteSpaceMacros] {
21348     auto Style = getGoogleStyle();
21349     Style.ColumnLimit = 20;
21350     Style.UseTab = FormatStyle::UT_Always;
21351     Style.WhitespaceSensitiveMacros = NonDefaultWhiteSpaceMacros;
21352     return Style;
21353   }();
21354 
21355   ASSERT_NE(Style9->WhitespaceSensitiveMacros, NonDefaultWhiteSpaceMacros);
21356   Style9 = getStyle("file", "/e/sub/sub/code.cpp", "none", "", &FS);
21357   ASSERT_TRUE(static_cast<bool>(Style9));
21358   ASSERT_EQ(*Style9, SubSubStyle);
21359 
21360   // Test 9.5: use InheritParentConfig as style name
21361   Style9 =
21362       getStyle("inheritparentconfig", "/e/sub/sub/code.cpp", "none", "", &FS);
21363   ASSERT_TRUE(static_cast<bool>(Style9));
21364   ASSERT_EQ(*Style9, SubSubStyle);
21365 
21366   // Test 9.6: use command line style with inheritance
21367   Style9 = getStyle("{BasedOnStyle: InheritParentConfig}", "/e/sub/code.cpp",
21368                     "none", "", &FS);
21369   ASSERT_TRUE(static_cast<bool>(Style9));
21370   ASSERT_EQ(*Style9, SubSubStyle);
21371 
21372   // Test 9.7: use command line style with inheritance and own config
21373   Style9 = getStyle("{BasedOnStyle: InheritParentConfig, "
21374                     "WhitespaceSensitiveMacros: ['FOO', 'BAR']}",
21375                     "/e/sub/code.cpp", "none", "", &FS);
21376   ASSERT_TRUE(static_cast<bool>(Style9));
21377   ASSERT_EQ(*Style9, SubSubStyle);
21378 
21379   // Test 9.8: use inheritance from a file without BasedOnStyle
21380   ASSERT_TRUE(FS.addFile("/e/withoutbase/.clang-format", 0,
21381                          llvm::MemoryBuffer::getMemBuffer("ColumnLimit: 123")));
21382   ASSERT_TRUE(
21383       FS.addFile("/e/withoutbase/sub/.clang-format", 0,
21384                  llvm::MemoryBuffer::getMemBuffer(
21385                      "BasedOnStyle: InheritParentConfig\nIndentWidth: 7")));
21386   // Make sure we do not use the fallback style
21387   Style9 = getStyle("file", "/e/withoutbase/code.cpp", "google", "", &FS);
21388   ASSERT_TRUE(static_cast<bool>(Style9));
21389   ASSERT_EQ(*Style9, [] {
21390     auto Style = getLLVMStyle();
21391     Style.ColumnLimit = 123;
21392     return Style;
21393   }());
21394 
21395   Style9 = getStyle("file", "/e/withoutbase/sub/code.cpp", "google", "", &FS);
21396   ASSERT_TRUE(static_cast<bool>(Style9));
21397   ASSERT_EQ(*Style9, [] {
21398     auto Style = getLLVMStyle();
21399     Style.ColumnLimit = 123;
21400     Style.IndentWidth = 7;
21401     return Style;
21402   }());
21403 }
21404 
21405 TEST_F(ReplacementTest, FormatCodeAfterReplacements) {
21406   // Column limit is 20.
21407   std::string Code = "Type *a =\n"
21408                      "    new Type();\n"
21409                      "g(iiiii, 0, jjjjj,\n"
21410                      "  0, kkkkk, 0, mm);\n"
21411                      "int  bad     = format   ;";
21412   std::string Expected = "auto a = new Type();\n"
21413                          "g(iiiii, nullptr,\n"
21414                          "  jjjjj, nullptr,\n"
21415                          "  kkkkk, nullptr,\n"
21416                          "  mm);\n"
21417                          "int  bad     = format   ;";
21418   FileID ID = Context.createInMemoryFile("format.cpp", Code);
21419   tooling::Replacements Replaces = toReplacements(
21420       {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6,
21421                             "auto "),
21422        tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1,
21423                             "nullptr"),
21424        tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1,
21425                             "nullptr"),
21426        tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1,
21427                             "nullptr")});
21428 
21429   format::FormatStyle Style = format::getLLVMStyle();
21430   Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility.
21431   auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
21432   EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
21433       << llvm::toString(FormattedReplaces.takeError()) << "\n";
21434   auto Result = applyAllReplacements(Code, *FormattedReplaces);
21435   EXPECT_TRUE(static_cast<bool>(Result));
21436   EXPECT_EQ(Expected, *Result);
21437 }
21438 
21439 TEST_F(ReplacementTest, SortIncludesAfterReplacement) {
21440   std::string Code = "#include \"a.h\"\n"
21441                      "#include \"c.h\"\n"
21442                      "\n"
21443                      "int main() {\n"
21444                      "  return 0;\n"
21445                      "}";
21446   std::string Expected = "#include \"a.h\"\n"
21447                          "#include \"b.h\"\n"
21448                          "#include \"c.h\"\n"
21449                          "\n"
21450                          "int main() {\n"
21451                          "  return 0;\n"
21452                          "}";
21453   FileID ID = Context.createInMemoryFile("fix.cpp", Code);
21454   tooling::Replacements Replaces = toReplacements(
21455       {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0,
21456                             "#include \"b.h\"\n")});
21457 
21458   format::FormatStyle Style = format::getLLVMStyle();
21459   Style.SortIncludes = FormatStyle::SI_CaseSensitive;
21460   auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
21461   EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
21462       << llvm::toString(FormattedReplaces.takeError()) << "\n";
21463   auto Result = applyAllReplacements(Code, *FormattedReplaces);
21464   EXPECT_TRUE(static_cast<bool>(Result));
21465   EXPECT_EQ(Expected, *Result);
21466 }
21467 
21468 TEST_F(FormatTest, FormatSortsUsingDeclarations) {
21469   EXPECT_EQ("using std::cin;\n"
21470             "using std::cout;",
21471             format("using std::cout;\n"
21472                    "using std::cin;",
21473                    getGoogleStyle()));
21474 }
21475 
21476 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) {
21477   format::FormatStyle Style = format::getLLVMStyle();
21478   Style.Standard = FormatStyle::LS_Cpp03;
21479   // cpp03 recognize this string as identifier u8 and literal character 'a'
21480   EXPECT_EQ("auto c = u8 'a';", format("auto c = u8'a';", Style));
21481 }
21482 
21483 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) {
21484   // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers
21485   // all modes, including C++11, C++14 and C++17
21486   EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';"));
21487 }
21488 
21489 TEST_F(FormatTest, DoNotFormatLikelyXml) {
21490   EXPECT_EQ("<!-- ;> -->", format("<!-- ;> -->", getGoogleStyle()));
21491   EXPECT_EQ(" <!-- >; -->", format(" <!-- >; -->", getGoogleStyle()));
21492 }
21493 
21494 TEST_F(FormatTest, StructuredBindings) {
21495   // Structured bindings is a C++17 feature.
21496   // all modes, including C++11, C++14 and C++17
21497   verifyFormat("auto [a, b] = f();");
21498   EXPECT_EQ("auto [a, b] = f();", format("auto[a, b] = f();"));
21499   EXPECT_EQ("const auto [a, b] = f();", format("const   auto[a, b] = f();"));
21500   EXPECT_EQ("auto const [a, b] = f();", format("auto  const[a, b] = f();"));
21501   EXPECT_EQ("auto const volatile [a, b] = f();",
21502             format("auto  const   volatile[a, b] = f();"));
21503   EXPECT_EQ("auto [a, b, c] = f();", format("auto   [  a  ,  b,c   ] = f();"));
21504   EXPECT_EQ("auto &[a, b, c] = f();",
21505             format("auto   &[  a  ,  b,c   ] = f();"));
21506   EXPECT_EQ("auto &&[a, b, c] = f();",
21507             format("auto   &&[  a  ,  b,c   ] = f();"));
21508   EXPECT_EQ("auto const &[a, b] = f();", format("auto  const&[a, b] = f();"));
21509   EXPECT_EQ("auto const volatile &&[a, b] = f();",
21510             format("auto  const  volatile  &&[a, b] = f();"));
21511   EXPECT_EQ("auto const &&[a, b] = f();",
21512             format("auto  const   &&  [a, b] = f();"));
21513   EXPECT_EQ("const auto &[a, b] = f();",
21514             format("const  auto  &  [a, b] = f();"));
21515   EXPECT_EQ("const auto volatile &&[a, b] = f();",
21516             format("const  auto   volatile  &&[a, b] = f();"));
21517   EXPECT_EQ("volatile const auto &&[a, b] = f();",
21518             format("volatile  const  auto   &&[a, b] = f();"));
21519   EXPECT_EQ("const auto &&[a, b] = f();",
21520             format("const  auto  &&  [a, b] = f();"));
21521 
21522   // Make sure we don't mistake structured bindings for lambdas.
21523   FormatStyle PointerMiddle = getLLVMStyle();
21524   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
21525   verifyFormat("auto [a1, b]{A * i};", getGoogleStyle());
21526   verifyFormat("auto [a2, b]{A * i};", getLLVMStyle());
21527   verifyFormat("auto [a3, b]{A * i};", PointerMiddle);
21528   verifyFormat("auto const [a1, b]{A * i};", getGoogleStyle());
21529   verifyFormat("auto const [a2, b]{A * i};", getLLVMStyle());
21530   verifyFormat("auto const [a3, b]{A * i};", PointerMiddle);
21531   verifyFormat("auto const& [a1, b]{A * i};", getGoogleStyle());
21532   verifyFormat("auto const &[a2, b]{A * i};", getLLVMStyle());
21533   verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle);
21534   verifyFormat("auto const&& [a1, b]{A * i};", getGoogleStyle());
21535   verifyFormat("auto const &&[a2, b]{A * i};", getLLVMStyle());
21536   verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle);
21537 
21538   EXPECT_EQ("for (const auto &&[a, b] : some_range) {\n}",
21539             format("for (const auto   &&   [a, b] : some_range) {\n}"));
21540   EXPECT_EQ("for (const auto &[a, b] : some_range) {\n}",
21541             format("for (const auto   &   [a, b] : some_range) {\n}"));
21542   EXPECT_EQ("for (const auto [a, b] : some_range) {\n}",
21543             format("for (const auto[a, b] : some_range) {\n}"));
21544   EXPECT_EQ("auto [x, y](expr);", format("auto[x,y]  (expr);"));
21545   EXPECT_EQ("auto &[x, y](expr);", format("auto  &  [x,y]  (expr);"));
21546   EXPECT_EQ("auto &&[x, y](expr);", format("auto  &&  [x,y]  (expr);"));
21547   EXPECT_EQ("auto const &[x, y](expr);",
21548             format("auto  const  &  [x,y]  (expr);"));
21549   EXPECT_EQ("auto const &&[x, y](expr);",
21550             format("auto  const  &&  [x,y]  (expr);"));
21551   EXPECT_EQ("auto [x, y]{expr};", format("auto[x,y]     {expr};"));
21552   EXPECT_EQ("auto const &[x, y]{expr};",
21553             format("auto  const  &  [x,y]  {expr};"));
21554   EXPECT_EQ("auto const &&[x, y]{expr};",
21555             format("auto  const  &&  [x,y]  {expr};"));
21556 
21557   format::FormatStyle Spaces = format::getLLVMStyle();
21558   Spaces.SpacesInSquareBrackets = true;
21559   verifyFormat("auto [ a, b ] = f();", Spaces);
21560   verifyFormat("auto &&[ a, b ] = f();", Spaces);
21561   verifyFormat("auto &[ a, b ] = f();", Spaces);
21562   verifyFormat("auto const &&[ a, b ] = f();", Spaces);
21563   verifyFormat("auto const &[ a, b ] = f();", Spaces);
21564 }
21565 
21566 TEST_F(FormatTest, FileAndCode) {
21567   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.cc", ""));
21568   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.m", ""));
21569   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.mm", ""));
21570   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", ""));
21571   EXPECT_EQ(FormatStyle::LK_ObjC,
21572             guessLanguage("foo.h", "@interface Foo\n@end\n"));
21573   EXPECT_EQ(
21574       FormatStyle::LK_ObjC,
21575       guessLanguage("foo.h", "#define TRY(x, y) @try { x; } @finally { y; }"));
21576   EXPECT_EQ(FormatStyle::LK_ObjC,
21577             guessLanguage("foo.h", "#define AVAIL(x) @available(x, *))"));
21578   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@class Foo;"));
21579   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo", ""));
21580   EXPECT_EQ(FormatStyle::LK_ObjC,
21581             guessLanguage("foo", "@interface Foo\n@end\n"));
21582   EXPECT_EQ(FormatStyle::LK_ObjC,
21583             guessLanguage("foo.h", "int DoStuff(CGRect rect);\n"));
21584   EXPECT_EQ(
21585       FormatStyle::LK_ObjC,
21586       guessLanguage("foo.h",
21587                     "#define MY_POINT_MAKE(x, y) CGPointMake((x), (y));\n"));
21588   EXPECT_EQ(
21589       FormatStyle::LK_Cpp,
21590       guessLanguage("foo.h", "#define FOO(...) auto bar = [] __VA_ARGS__;"));
21591 }
21592 
21593 TEST_F(FormatTest, GuessLanguageWithCpp11AttributeSpecifiers) {
21594   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[noreturn]];"));
21595   EXPECT_EQ(FormatStyle::LK_ObjC,
21596             guessLanguage("foo.h", "array[[calculator getIndex]];"));
21597   EXPECT_EQ(FormatStyle::LK_Cpp,
21598             guessLanguage("foo.h", "[[noreturn, deprecated(\"so sorry\")]];"));
21599   EXPECT_EQ(
21600       FormatStyle::LK_Cpp,
21601       guessLanguage("foo.h", "[[noreturn, deprecated(\"gone, sorry\")]];"));
21602   EXPECT_EQ(FormatStyle::LK_ObjC,
21603             guessLanguage("foo.h", "[[noreturn foo] bar];"));
21604   EXPECT_EQ(FormatStyle::LK_Cpp,
21605             guessLanguage("foo.h", "[[clang::fallthrough]];"));
21606   EXPECT_EQ(FormatStyle::LK_ObjC,
21607             guessLanguage("foo.h", "[[clang:fallthrough] foo];"));
21608   EXPECT_EQ(FormatStyle::LK_Cpp,
21609             guessLanguage("foo.h", "[[gsl::suppress(\"type\")]];"));
21610   EXPECT_EQ(FormatStyle::LK_Cpp,
21611             guessLanguage("foo.h", "[[using clang: fallthrough]];"));
21612   EXPECT_EQ(FormatStyle::LK_ObjC,
21613             guessLanguage("foo.h", "[[abusing clang:fallthrough] bar];"));
21614   EXPECT_EQ(FormatStyle::LK_Cpp,
21615             guessLanguage("foo.h", "[[using gsl: suppress(\"type\")]];"));
21616   EXPECT_EQ(
21617       FormatStyle::LK_Cpp,
21618       guessLanguage("foo.h", "for (auto &&[endpoint, stream] : streams_)"));
21619   EXPECT_EQ(
21620       FormatStyle::LK_Cpp,
21621       guessLanguage("foo.h",
21622                     "[[clang::callable_when(\"unconsumed\", \"unknown\")]]"));
21623   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[foo::bar, ...]]"));
21624 }
21625 
21626 TEST_F(FormatTest, GuessLanguageWithCaret) {
21627   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^);"));
21628   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^, Bar);"));
21629   EXPECT_EQ(FormatStyle::LK_ObjC,
21630             guessLanguage("foo.h", "int(^)(char, float);"));
21631   EXPECT_EQ(FormatStyle::LK_ObjC,
21632             guessLanguage("foo.h", "int(^foo)(char, float);"));
21633   EXPECT_EQ(FormatStyle::LK_ObjC,
21634             guessLanguage("foo.h", "int(^foo[10])(char, float);"));
21635   EXPECT_EQ(FormatStyle::LK_ObjC,
21636             guessLanguage("foo.h", "int(^foo[kNumEntries])(char, float);"));
21637   EXPECT_EQ(
21638       FormatStyle::LK_ObjC,
21639       guessLanguage("foo.h", "int(^foo[(kNumEntries + 10)])(char, float);"));
21640 }
21641 
21642 TEST_F(FormatTest, GuessLanguageWithPragmas) {
21643   EXPECT_EQ(FormatStyle::LK_Cpp,
21644             guessLanguage("foo.h", "__pragma(warning(disable:))"));
21645   EXPECT_EQ(FormatStyle::LK_Cpp,
21646             guessLanguage("foo.h", "#pragma(warning(disable:))"));
21647   EXPECT_EQ(FormatStyle::LK_Cpp,
21648             guessLanguage("foo.h", "_Pragma(warning(disable:))"));
21649 }
21650 
21651 TEST_F(FormatTest, FormatsInlineAsmSymbolicNames) {
21652   // ASM symbolic names are identifiers that must be surrounded by [] without
21653   // space in between:
21654   // https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#InputOperands
21655 
21656   // Example from https://bugs.llvm.org/show_bug.cgi?id=45108.
21657   verifyFormat(R"(//
21658 asm volatile("mrs %x[result], FPCR" : [result] "=r"(result));
21659 )");
21660 
21661   // A list of several ASM symbolic names.
21662   verifyFormat(R"(asm("mov %[e], %[d]" : [d] "=rm"(d), [e] "rm"(*e));)");
21663 
21664   // ASM symbolic names in inline ASM with inputs and outputs.
21665   verifyFormat(R"(//
21666 asm("cmoveq %1, %2, %[result]"
21667     : [result] "=r"(result)
21668     : "r"(test), "r"(new), "[result]"(old));
21669 )");
21670 
21671   // ASM symbolic names in inline ASM with no outputs.
21672   verifyFormat(R"(asm("mov %[e], %[d]" : : [d] "=rm"(d), [e] "rm"(*e));)");
21673 }
21674 
21675 TEST_F(FormatTest, GuessedLanguageWithInlineAsmClobbers) {
21676   EXPECT_EQ(FormatStyle::LK_Cpp,
21677             guessLanguage("foo.h", "void f() {\n"
21678                                    "  asm (\"mov %[e], %[d]\"\n"
21679                                    "     : [d] \"=rm\" (d)\n"
21680                                    "       [e] \"rm\" (*e));\n"
21681                                    "}"));
21682   EXPECT_EQ(FormatStyle::LK_Cpp,
21683             guessLanguage("foo.h", "void f() {\n"
21684                                    "  _asm (\"mov %[e], %[d]\"\n"
21685                                    "     : [d] \"=rm\" (d)\n"
21686                                    "       [e] \"rm\" (*e));\n"
21687                                    "}"));
21688   EXPECT_EQ(FormatStyle::LK_Cpp,
21689             guessLanguage("foo.h", "void f() {\n"
21690                                    "  __asm (\"mov %[e], %[d]\"\n"
21691                                    "     : [d] \"=rm\" (d)\n"
21692                                    "       [e] \"rm\" (*e));\n"
21693                                    "}"));
21694   EXPECT_EQ(FormatStyle::LK_Cpp,
21695             guessLanguage("foo.h", "void f() {\n"
21696                                    "  __asm__ (\"mov %[e], %[d]\"\n"
21697                                    "     : [d] \"=rm\" (d)\n"
21698                                    "       [e] \"rm\" (*e));\n"
21699                                    "}"));
21700   EXPECT_EQ(FormatStyle::LK_Cpp,
21701             guessLanguage("foo.h", "void f() {\n"
21702                                    "  asm (\"mov %[e], %[d]\"\n"
21703                                    "     : [d] \"=rm\" (d),\n"
21704                                    "       [e] \"rm\" (*e));\n"
21705                                    "}"));
21706   EXPECT_EQ(FormatStyle::LK_Cpp,
21707             guessLanguage("foo.h", "void f() {\n"
21708                                    "  asm volatile (\"mov %[e], %[d]\"\n"
21709                                    "     : [d] \"=rm\" (d)\n"
21710                                    "       [e] \"rm\" (*e));\n"
21711                                    "}"));
21712 }
21713 
21714 TEST_F(FormatTest, GuessLanguageWithChildLines) {
21715   EXPECT_EQ(FormatStyle::LK_Cpp,
21716             guessLanguage("foo.h", "#define FOO ({ std::string s; })"));
21717   EXPECT_EQ(FormatStyle::LK_ObjC,
21718             guessLanguage("foo.h", "#define FOO ({ NSString *s; })"));
21719   EXPECT_EQ(
21720       FormatStyle::LK_Cpp,
21721       guessLanguage("foo.h", "#define FOO ({ foo(); ({ std::string s; }) })"));
21722   EXPECT_EQ(
21723       FormatStyle::LK_ObjC,
21724       guessLanguage("foo.h", "#define FOO ({ foo(); ({ NSString *s; }) })"));
21725 }
21726 
21727 TEST_F(FormatTest, TypenameMacros) {
21728   std::vector<std::string> TypenameMacros = {"STACK_OF", "LIST", "TAILQ_ENTRY"};
21729 
21730   // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=30353
21731   FormatStyle Google = getGoogleStyleWithColumns(0);
21732   Google.TypenameMacros = TypenameMacros;
21733   verifyFormat("struct foo {\n"
21734                "  int bar;\n"
21735                "  TAILQ_ENTRY(a) bleh;\n"
21736                "};",
21737                Google);
21738 
21739   FormatStyle Macros = getLLVMStyle();
21740   Macros.TypenameMacros = TypenameMacros;
21741 
21742   verifyFormat("STACK_OF(int) a;", Macros);
21743   verifyFormat("STACK_OF(int) *a;", Macros);
21744   verifyFormat("STACK_OF(int const *) *a;", Macros);
21745   verifyFormat("STACK_OF(int *const) *a;", Macros);
21746   verifyFormat("STACK_OF(int, string) a;", Macros);
21747   verifyFormat("STACK_OF(LIST(int)) a;", Macros);
21748   verifyFormat("STACK_OF(LIST(int)) a, b;", Macros);
21749   verifyFormat("for (LIST(int) *a = NULL; a;) {\n}", Macros);
21750   verifyFormat("STACK_OF(int) f(LIST(int) *arg);", Macros);
21751   verifyFormat("vector<LIST(uint64_t) *attr> x;", Macros);
21752   verifyFormat("vector<LIST(uint64_t) *const> f(LIST(uint64_t) *arg);", Macros);
21753 
21754   Macros.PointerAlignment = FormatStyle::PAS_Left;
21755   verifyFormat("STACK_OF(int)* a;", Macros);
21756   verifyFormat("STACK_OF(int*)* a;", Macros);
21757   verifyFormat("x = (STACK_OF(uint64_t))*a;", Macros);
21758   verifyFormat("x = (STACK_OF(uint64_t))&a;", Macros);
21759   verifyFormat("vector<STACK_OF(uint64_t)* attr> x;", Macros);
21760 }
21761 
21762 TEST_F(FormatTest, AtomicQualifier) {
21763   // Check that we treate _Atomic as a type and not a function call
21764   FormatStyle Google = getGoogleStyleWithColumns(0);
21765   verifyFormat("struct foo {\n"
21766                "  int a1;\n"
21767                "  _Atomic(a) a2;\n"
21768                "  _Atomic(_Atomic(int) *const) a3;\n"
21769                "};",
21770                Google);
21771   verifyFormat("_Atomic(uint64_t) a;");
21772   verifyFormat("_Atomic(uint64_t) *a;");
21773   verifyFormat("_Atomic(uint64_t const *) *a;");
21774   verifyFormat("_Atomic(uint64_t *const) *a;");
21775   verifyFormat("_Atomic(const uint64_t *) *a;");
21776   verifyFormat("_Atomic(uint64_t) a;");
21777   verifyFormat("_Atomic(_Atomic(uint64_t)) a;");
21778   verifyFormat("_Atomic(_Atomic(uint64_t)) a, b;");
21779   verifyFormat("for (_Atomic(uint64_t) *a = NULL; a;) {\n}");
21780   verifyFormat("_Atomic(uint64_t) f(_Atomic(uint64_t) *arg);");
21781 
21782   verifyFormat("_Atomic(uint64_t) *s(InitValue);");
21783   verifyFormat("_Atomic(uint64_t) *s{InitValue};");
21784   FormatStyle Style = getLLVMStyle();
21785   Style.PointerAlignment = FormatStyle::PAS_Left;
21786   verifyFormat("_Atomic(uint64_t)* s(InitValue);", Style);
21787   verifyFormat("_Atomic(uint64_t)* s{InitValue};", Style);
21788   verifyFormat("_Atomic(int)* a;", Style);
21789   verifyFormat("_Atomic(int*)* a;", Style);
21790   verifyFormat("vector<_Atomic(uint64_t)* attr> x;", Style);
21791 
21792   Style.SpacesInCStyleCastParentheses = true;
21793   Style.SpacesInParentheses = false;
21794   verifyFormat("x = ( _Atomic(uint64_t) )*a;", Style);
21795   Style.SpacesInCStyleCastParentheses = false;
21796   Style.SpacesInParentheses = true;
21797   verifyFormat("x = (_Atomic( uint64_t ))*a;", Style);
21798   verifyFormat("x = (_Atomic( uint64_t ))&a;", Style);
21799 }
21800 
21801 TEST_F(FormatTest, AmbersandInLamda) {
21802   // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=41899
21803   FormatStyle AlignStyle = getLLVMStyle();
21804   AlignStyle.PointerAlignment = FormatStyle::PAS_Left;
21805   verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle);
21806   AlignStyle.PointerAlignment = FormatStyle::PAS_Right;
21807   verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle);
21808 }
21809 
21810 TEST_F(FormatTest, SpacesInConditionalStatement) {
21811   FormatStyle Spaces = getLLVMStyle();
21812   Spaces.IfMacros.clear();
21813   Spaces.IfMacros.push_back("MYIF");
21814   Spaces.SpacesInConditionalStatement = true;
21815   verifyFormat("for ( int i = 0; i; i++ )\n  continue;", Spaces);
21816   verifyFormat("if ( !a )\n  return;", Spaces);
21817   verifyFormat("if ( a )\n  return;", Spaces);
21818   verifyFormat("if constexpr ( a )\n  return;", Spaces);
21819   verifyFormat("MYIF ( a )\n  return;", Spaces);
21820   verifyFormat("MYIF ( a )\n  return;\nelse MYIF ( b )\n  return;", Spaces);
21821   verifyFormat("MYIF ( a )\n  return;\nelse\n  return;", Spaces);
21822   verifyFormat("switch ( a )\ncase 1:\n  return;", Spaces);
21823   verifyFormat("while ( a )\n  return;", Spaces);
21824   verifyFormat("while ( (a && b) )\n  return;", Spaces);
21825   verifyFormat("do {\n} while ( 1 != 0 );", Spaces);
21826   verifyFormat("try {\n} catch ( const std::exception & ) {\n}", Spaces);
21827   // Check that space on the left of "::" is inserted as expected at beginning
21828   // of condition.
21829   verifyFormat("while ( ::func() )\n  return;", Spaces);
21830 
21831   // Check impact of ControlStatementsExceptControlMacros is honored.
21832   Spaces.SpaceBeforeParens =
21833       FormatStyle::SBPO_ControlStatementsExceptControlMacros;
21834   verifyFormat("MYIF( a )\n  return;", Spaces);
21835   verifyFormat("MYIF( a )\n  return;\nelse MYIF( b )\n  return;", Spaces);
21836   verifyFormat("MYIF( a )\n  return;\nelse\n  return;", Spaces);
21837 }
21838 
21839 TEST_F(FormatTest, AlternativeOperators) {
21840   // Test case for ensuring alternate operators are not
21841   // combined with their right most neighbour.
21842   verifyFormat("int a and b;");
21843   verifyFormat("int a and_eq b;");
21844   verifyFormat("int a bitand b;");
21845   verifyFormat("int a bitor b;");
21846   verifyFormat("int a compl b;");
21847   verifyFormat("int a not b;");
21848   verifyFormat("int a not_eq b;");
21849   verifyFormat("int a or b;");
21850   verifyFormat("int a xor b;");
21851   verifyFormat("int a xor_eq b;");
21852   verifyFormat("return this not_eq bitand other;");
21853   verifyFormat("bool operator not_eq(const X bitand other)");
21854 
21855   verifyFormat("int a and 5;");
21856   verifyFormat("int a and_eq 5;");
21857   verifyFormat("int a bitand 5;");
21858   verifyFormat("int a bitor 5;");
21859   verifyFormat("int a compl 5;");
21860   verifyFormat("int a not 5;");
21861   verifyFormat("int a not_eq 5;");
21862   verifyFormat("int a or 5;");
21863   verifyFormat("int a xor 5;");
21864   verifyFormat("int a xor_eq 5;");
21865 
21866   verifyFormat("int a compl(5);");
21867   verifyFormat("int a not(5);");
21868 
21869   /* FIXME handle alternate tokens
21870    * https://en.cppreference.com/w/cpp/language/operator_alternative
21871   // alternative tokens
21872   verifyFormat("compl foo();");     //  ~foo();
21873   verifyFormat("foo() <%%>;");      // foo();
21874   verifyFormat("void foo() <%%>;"); // void foo(){}
21875   verifyFormat("int a <:1:>;");     // int a[1];[
21876   verifyFormat("%:define ABC abc"); // #define ABC abc
21877   verifyFormat("%:%:");             // ##
21878   */
21879 }
21880 
21881 TEST_F(FormatTest, STLWhileNotDefineChed) {
21882   verifyFormat("#if defined(while)\n"
21883                "#define while EMIT WARNING C4005\n"
21884                "#endif // while");
21885 }
21886 
21887 TEST_F(FormatTest, OperatorSpacing) {
21888   FormatStyle Style = getLLVMStyle();
21889   Style.PointerAlignment = FormatStyle::PAS_Right;
21890   verifyFormat("Foo::operator*();", Style);
21891   verifyFormat("Foo::operator void *();", Style);
21892   verifyFormat("Foo::operator void **();", Style);
21893   verifyFormat("Foo::operator void *&();", Style);
21894   verifyFormat("Foo::operator void *&&();", Style);
21895   verifyFormat("Foo::operator void const *();", Style);
21896   verifyFormat("Foo::operator void const **();", Style);
21897   verifyFormat("Foo::operator void const *&();", Style);
21898   verifyFormat("Foo::operator void const *&&();", Style);
21899   verifyFormat("Foo::operator()(void *);", Style);
21900   verifyFormat("Foo::operator*(void *);", Style);
21901   verifyFormat("Foo::operator*();", Style);
21902   verifyFormat("Foo::operator**();", Style);
21903   verifyFormat("Foo::operator&();", Style);
21904   verifyFormat("Foo::operator<int> *();", Style);
21905   verifyFormat("Foo::operator<Foo> *();", Style);
21906   verifyFormat("Foo::operator<int> **();", Style);
21907   verifyFormat("Foo::operator<Foo> **();", Style);
21908   verifyFormat("Foo::operator<int> &();", Style);
21909   verifyFormat("Foo::operator<Foo> &();", Style);
21910   verifyFormat("Foo::operator<int> &&();", Style);
21911   verifyFormat("Foo::operator<Foo> &&();", Style);
21912   verifyFormat("Foo::operator<int> *&();", Style);
21913   verifyFormat("Foo::operator<Foo> *&();", Style);
21914   verifyFormat("Foo::operator<int> *&&();", Style);
21915   verifyFormat("Foo::operator<Foo> *&&();", Style);
21916   verifyFormat("operator*(int (*)(), class Foo);", Style);
21917 
21918   verifyFormat("Foo::operator&();", Style);
21919   verifyFormat("Foo::operator void &();", Style);
21920   verifyFormat("Foo::operator void const &();", Style);
21921   verifyFormat("Foo::operator()(void &);", Style);
21922   verifyFormat("Foo::operator&(void &);", Style);
21923   verifyFormat("Foo::operator&();", Style);
21924   verifyFormat("operator&(int (&)(), class Foo);", Style);
21925   verifyFormat("operator&&(int (&)(), class Foo);", Style);
21926 
21927   verifyFormat("Foo::operator&&();", Style);
21928   verifyFormat("Foo::operator**();", Style);
21929   verifyFormat("Foo::operator void &&();", Style);
21930   verifyFormat("Foo::operator void const &&();", Style);
21931   verifyFormat("Foo::operator()(void &&);", Style);
21932   verifyFormat("Foo::operator&&(void &&);", Style);
21933   verifyFormat("Foo::operator&&();", Style);
21934   verifyFormat("operator&&(int (&&)(), class Foo);", Style);
21935   verifyFormat("operator const nsTArrayRight<E> &()", Style);
21936   verifyFormat("[[nodiscard]] operator const nsTArrayRight<E, Allocator> &()",
21937                Style);
21938   verifyFormat("operator void **()", Style);
21939   verifyFormat("operator const FooRight<Object> &()", Style);
21940   verifyFormat("operator const FooRight<Object> *()", Style);
21941   verifyFormat("operator const FooRight<Object> **()", Style);
21942   verifyFormat("operator const FooRight<Object> *&()", Style);
21943   verifyFormat("operator const FooRight<Object> *&&()", Style);
21944 
21945   Style.PointerAlignment = FormatStyle::PAS_Left;
21946   verifyFormat("Foo::operator*();", Style);
21947   verifyFormat("Foo::operator**();", Style);
21948   verifyFormat("Foo::operator void*();", Style);
21949   verifyFormat("Foo::operator void**();", Style);
21950   verifyFormat("Foo::operator void*&();", Style);
21951   verifyFormat("Foo::operator void*&&();", Style);
21952   verifyFormat("Foo::operator void const*();", Style);
21953   verifyFormat("Foo::operator void const**();", Style);
21954   verifyFormat("Foo::operator void const*&();", Style);
21955   verifyFormat("Foo::operator void const*&&();", Style);
21956   verifyFormat("Foo::operator/*comment*/ void*();", Style);
21957   verifyFormat("Foo::operator/*a*/ const /*b*/ void*();", Style);
21958   verifyFormat("Foo::operator/*a*/ volatile /*b*/ void*();", Style);
21959   verifyFormat("Foo::operator()(void*);", Style);
21960   verifyFormat("Foo::operator*(void*);", Style);
21961   verifyFormat("Foo::operator*();", Style);
21962   verifyFormat("Foo::operator<int>*();", Style);
21963   verifyFormat("Foo::operator<Foo>*();", Style);
21964   verifyFormat("Foo::operator<int>**();", Style);
21965   verifyFormat("Foo::operator<Foo>**();", Style);
21966   verifyFormat("Foo::operator<Foo>*&();", Style);
21967   verifyFormat("Foo::operator<int>&();", Style);
21968   verifyFormat("Foo::operator<Foo>&();", Style);
21969   verifyFormat("Foo::operator<int>&&();", Style);
21970   verifyFormat("Foo::operator<Foo>&&();", Style);
21971   verifyFormat("Foo::operator<int>*&();", Style);
21972   verifyFormat("Foo::operator<Foo>*&();", Style);
21973   verifyFormat("operator*(int (*)(), class Foo);", Style);
21974 
21975   verifyFormat("Foo::operator&();", Style);
21976   verifyFormat("Foo::operator void&();", Style);
21977   verifyFormat("Foo::operator void const&();", Style);
21978   verifyFormat("Foo::operator/*comment*/ void&();", Style);
21979   verifyFormat("Foo::operator/*a*/ const /*b*/ void&();", Style);
21980   verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&();", Style);
21981   verifyFormat("Foo::operator()(void&);", Style);
21982   verifyFormat("Foo::operator&(void&);", Style);
21983   verifyFormat("Foo::operator&();", Style);
21984   verifyFormat("operator&(int (&)(), class Foo);", Style);
21985   verifyFormat("operator&(int (&&)(), class Foo);", Style);
21986   verifyFormat("operator&&(int (&&)(), class Foo);", Style);
21987 
21988   verifyFormat("Foo::operator&&();", Style);
21989   verifyFormat("Foo::operator void&&();", Style);
21990   verifyFormat("Foo::operator void const&&();", Style);
21991   verifyFormat("Foo::operator/*comment*/ void&&();", Style);
21992   verifyFormat("Foo::operator/*a*/ const /*b*/ void&&();", Style);
21993   verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&&();", Style);
21994   verifyFormat("Foo::operator()(void&&);", Style);
21995   verifyFormat("Foo::operator&&(void&&);", Style);
21996   verifyFormat("Foo::operator&&();", Style);
21997   verifyFormat("operator&&(int (&&)(), class Foo);", Style);
21998   verifyFormat("operator const nsTArrayLeft<E>&()", Style);
21999   verifyFormat("[[nodiscard]] operator const nsTArrayLeft<E, Allocator>&()",
22000                Style);
22001   verifyFormat("operator void**()", Style);
22002   verifyFormat("operator const FooLeft<Object>&()", Style);
22003   verifyFormat("operator const FooLeft<Object>*()", Style);
22004   verifyFormat("operator const FooLeft<Object>**()", Style);
22005   verifyFormat("operator const FooLeft<Object>*&()", Style);
22006   verifyFormat("operator const FooLeft<Object>*&&()", Style);
22007 
22008   // PR45107
22009   verifyFormat("operator Vector<String>&();", Style);
22010   verifyFormat("operator const Vector<String>&();", Style);
22011   verifyFormat("operator foo::Bar*();", Style);
22012   verifyFormat("operator const Foo<X>::Bar<Y>*();", Style);
22013   verifyFormat("operator/*a*/ const /*b*/ Foo /*c*/<X> /*d*/ ::Bar<Y>*();",
22014                Style);
22015 
22016   Style.PointerAlignment = FormatStyle::PAS_Middle;
22017   verifyFormat("Foo::operator*();", Style);
22018   verifyFormat("Foo::operator void *();", Style);
22019   verifyFormat("Foo::operator()(void *);", Style);
22020   verifyFormat("Foo::operator*(void *);", Style);
22021   verifyFormat("Foo::operator*();", Style);
22022   verifyFormat("operator*(int (*)(), class Foo);", Style);
22023 
22024   verifyFormat("Foo::operator&();", Style);
22025   verifyFormat("Foo::operator void &();", Style);
22026   verifyFormat("Foo::operator void const &();", Style);
22027   verifyFormat("Foo::operator()(void &);", Style);
22028   verifyFormat("Foo::operator&(void &);", Style);
22029   verifyFormat("Foo::operator&();", Style);
22030   verifyFormat("operator&(int (&)(), class Foo);", Style);
22031 
22032   verifyFormat("Foo::operator&&();", Style);
22033   verifyFormat("Foo::operator void &&();", Style);
22034   verifyFormat("Foo::operator void const &&();", Style);
22035   verifyFormat("Foo::operator()(void &&);", Style);
22036   verifyFormat("Foo::operator&&(void &&);", Style);
22037   verifyFormat("Foo::operator&&();", Style);
22038   verifyFormat("operator&&(int (&&)(), class Foo);", Style);
22039 }
22040 
22041 TEST_F(FormatTest, OperatorPassedAsAFunctionPtr) {
22042   FormatStyle Style = getLLVMStyle();
22043   // PR46157
22044   verifyFormat("foo(operator+, -42);", Style);
22045   verifyFormat("foo(operator++, -42);", Style);
22046   verifyFormat("foo(operator--, -42);", Style);
22047   verifyFormat("foo(-42, operator--);", Style);
22048   verifyFormat("foo(-42, operator, );", Style);
22049   verifyFormat("foo(operator, , -42);", Style);
22050 }
22051 
22052 TEST_F(FormatTest, WhitespaceSensitiveMacros) {
22053   FormatStyle Style = getLLVMStyle();
22054   Style.WhitespaceSensitiveMacros.push_back("FOO");
22055 
22056   // Don't use the helpers here, since 'mess up' will change the whitespace
22057   // and these are all whitespace sensitive by definition
22058   EXPECT_EQ("FOO(String-ized&Messy+But(: :Still)=Intentional);",
22059             format("FOO(String-ized&Messy+But(: :Still)=Intentional);", Style));
22060   EXPECT_EQ(
22061       "FOO(String-ized&Messy+But\\(: :Still)=Intentional);",
22062       format("FOO(String-ized&Messy+But\\(: :Still)=Intentional);", Style));
22063   EXPECT_EQ("FOO(String-ized&Messy+But,: :Still=Intentional);",
22064             format("FOO(String-ized&Messy+But,: :Still=Intentional);", Style));
22065   EXPECT_EQ("FOO(String-ized&Messy+But,: :\n"
22066             "       Still=Intentional);",
22067             format("FOO(String-ized&Messy+But,: :\n"
22068                    "       Still=Intentional);",
22069                    Style));
22070   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
22071   EXPECT_EQ("FOO(String-ized=&Messy+But,: :\n"
22072             "       Still=Intentional);",
22073             format("FOO(String-ized=&Messy+But,: :\n"
22074                    "       Still=Intentional);",
22075                    Style));
22076 
22077   Style.ColumnLimit = 21;
22078   EXPECT_EQ("FOO(String-ized&Messy+But: :Still=Intentional);",
22079             format("FOO(String-ized&Messy+But: :Still=Intentional);", Style));
22080 }
22081 
22082 TEST_F(FormatTest, VeryLongNamespaceCommentSplit) {
22083   // These tests are not in NamespaceFixer because that doesn't
22084   // test its interaction with line wrapping
22085   FormatStyle Style = getLLVMStyle();
22086   Style.ColumnLimit = 80;
22087   verifyFormat("namespace {\n"
22088                "int i;\n"
22089                "int j;\n"
22090                "} // namespace",
22091                Style);
22092 
22093   verifyFormat("namespace AAA {\n"
22094                "int i;\n"
22095                "int j;\n"
22096                "} // namespace AAA",
22097                Style);
22098 
22099   EXPECT_EQ("namespace Averyveryveryverylongnamespace {\n"
22100             "int i;\n"
22101             "int j;\n"
22102             "} // namespace Averyveryveryverylongnamespace",
22103             format("namespace Averyveryveryverylongnamespace {\n"
22104                    "int i;\n"
22105                    "int j;\n"
22106                    "}",
22107                    Style));
22108 
22109   EXPECT_EQ(
22110       "namespace "
22111       "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n"
22112       "    went::mad::now {\n"
22113       "int i;\n"
22114       "int j;\n"
22115       "} // namespace\n"
22116       "  // "
22117       "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::"
22118       "went::mad::now",
22119       format("namespace "
22120              "would::it::save::you::a::lot::of::time::if_::i::"
22121              "just::gave::up::and_::went::mad::now {\n"
22122              "int i;\n"
22123              "int j;\n"
22124              "}",
22125              Style));
22126 
22127   // This used to duplicate the comment again and again on subsequent runs
22128   EXPECT_EQ(
22129       "namespace "
22130       "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n"
22131       "    went::mad::now {\n"
22132       "int i;\n"
22133       "int j;\n"
22134       "} // namespace\n"
22135       "  // "
22136       "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::"
22137       "went::mad::now",
22138       format("namespace "
22139              "would::it::save::you::a::lot::of::time::if_::i::"
22140              "just::gave::up::and_::went::mad::now {\n"
22141              "int i;\n"
22142              "int j;\n"
22143              "} // namespace\n"
22144              "  // "
22145              "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::"
22146              "and_::went::mad::now",
22147              Style));
22148 }
22149 
22150 TEST_F(FormatTest, LikelyUnlikely) {
22151   FormatStyle Style = getLLVMStyle();
22152 
22153   verifyFormat("if (argc > 5) [[unlikely]] {\n"
22154                "  return 29;\n"
22155                "}",
22156                Style);
22157 
22158   verifyFormat("if (argc > 5) [[likely]] {\n"
22159                "  return 29;\n"
22160                "}",
22161                Style);
22162 
22163   verifyFormat("if (argc > 5) [[unlikely]] {\n"
22164                "  return 29;\n"
22165                "} else [[likely]] {\n"
22166                "  return 42;\n"
22167                "}\n",
22168                Style);
22169 
22170   verifyFormat("if (argc > 5) [[unlikely]] {\n"
22171                "  return 29;\n"
22172                "} else if (argc > 10) [[likely]] {\n"
22173                "  return 99;\n"
22174                "} else {\n"
22175                "  return 42;\n"
22176                "}\n",
22177                Style);
22178 
22179   verifyFormat("if (argc > 5) [[gnu::unused]] {\n"
22180                "  return 29;\n"
22181                "}",
22182                Style);
22183 }
22184 
22185 TEST_F(FormatTest, PenaltyIndentedWhitespace) {
22186   verifyFormat("Constructor()\n"
22187                "    : aaaaaa(aaaaaa), aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
22188                "                          aaaa(aaaaaaaaaaaaaaaaaa, "
22189                "aaaaaaaaaaaaaaaaaat))");
22190   verifyFormat("Constructor()\n"
22191                "    : aaaaaaaaaaaaa(aaaaaa), "
22192                "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)");
22193 
22194   FormatStyle StyleWithWhitespacePenalty = getLLVMStyle();
22195   StyleWithWhitespacePenalty.PenaltyIndentedWhitespace = 5;
22196   verifyFormat("Constructor()\n"
22197                "    : aaaaaa(aaaaaa),\n"
22198                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
22199                "          aaaa(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaat))",
22200                StyleWithWhitespacePenalty);
22201   verifyFormat("Constructor()\n"
22202                "    : aaaaaaaaaaaaa(aaaaaa), "
22203                "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)",
22204                StyleWithWhitespacePenalty);
22205 }
22206 
22207 TEST_F(FormatTest, LLVMDefaultStyle) {
22208   FormatStyle Style = getLLVMStyle();
22209   verifyFormat("extern \"C\" {\n"
22210                "int foo();\n"
22211                "}",
22212                Style);
22213 }
22214 TEST_F(FormatTest, GNUDefaultStyle) {
22215   FormatStyle Style = getGNUStyle();
22216   verifyFormat("extern \"C\"\n"
22217                "{\n"
22218                "  int foo ();\n"
22219                "}",
22220                Style);
22221 }
22222 TEST_F(FormatTest, MozillaDefaultStyle) {
22223   FormatStyle Style = getMozillaStyle();
22224   verifyFormat("extern \"C\"\n"
22225                "{\n"
22226                "  int foo();\n"
22227                "}",
22228                Style);
22229 }
22230 TEST_F(FormatTest, GoogleDefaultStyle) {
22231   FormatStyle Style = getGoogleStyle();
22232   verifyFormat("extern \"C\" {\n"
22233                "int foo();\n"
22234                "}",
22235                Style);
22236 }
22237 TEST_F(FormatTest, ChromiumDefaultStyle) {
22238   FormatStyle Style = getChromiumStyle(FormatStyle::LanguageKind::LK_Cpp);
22239   verifyFormat("extern \"C\" {\n"
22240                "int foo();\n"
22241                "}",
22242                Style);
22243 }
22244 TEST_F(FormatTest, MicrosoftDefaultStyle) {
22245   FormatStyle Style = getMicrosoftStyle(FormatStyle::LanguageKind::LK_Cpp);
22246   verifyFormat("extern \"C\"\n"
22247                "{\n"
22248                "    int foo();\n"
22249                "}",
22250                Style);
22251 }
22252 TEST_F(FormatTest, WebKitDefaultStyle) {
22253   FormatStyle Style = getWebKitStyle();
22254   verifyFormat("extern \"C\" {\n"
22255                "int foo();\n"
22256                "}",
22257                Style);
22258 }
22259 
22260 TEST_F(FormatTest, ConceptsAndRequires) {
22261   FormatStyle Style = getLLVMStyle();
22262   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
22263 
22264   verifyFormat("template <typename T>\n"
22265                "concept Hashable = requires(T a) {\n"
22266                "  { std::hash<T>{}(a) } -> std::convertible_to<std::size_t>;\n"
22267                "};",
22268                Style);
22269   verifyFormat("template <typename T>\n"
22270                "concept EqualityComparable = requires(T a, T b) {\n"
22271                "  { a == b } -> bool;\n"
22272                "};",
22273                Style);
22274   verifyFormat("template <typename T>\n"
22275                "concept EqualityComparable = requires(T a, T b) {\n"
22276                "  { a == b } -> bool;\n"
22277                "  { a != b } -> bool;\n"
22278                "};",
22279                Style);
22280   verifyFormat("template <typename T>\n"
22281                "concept EqualityComparable = requires(T a, T b) {\n"
22282                "  { a == b } -> bool;\n"
22283                "  { a != b } -> bool;\n"
22284                "};",
22285                Style);
22286 
22287   verifyFormat("template <typename It>\n"
22288                "requires Iterator<It>\n"
22289                "void sort(It begin, It end) {\n"
22290                "  //....\n"
22291                "}",
22292                Style);
22293 
22294   verifyFormat("template <typename T>\n"
22295                "concept Large = sizeof(T) > 10;",
22296                Style);
22297 
22298   verifyFormat("template <typename T, typename U>\n"
22299                "concept FooableWith = requires(T t, U u) {\n"
22300                "  typename T::foo_type;\n"
22301                "  { t.foo(u) } -> typename T::foo_type;\n"
22302                "  t++;\n"
22303                "};\n"
22304                "void doFoo(FooableWith<int> auto t) {\n"
22305                "  t.foo(3);\n"
22306                "}",
22307                Style);
22308   verifyFormat("template <typename T>\n"
22309                "concept Context = sizeof(T) == 1;",
22310                Style);
22311   verifyFormat("template <typename T>\n"
22312                "concept Context = is_specialization_of_v<context, T>;",
22313                Style);
22314   verifyFormat("template <typename T>\n"
22315                "concept Node = std::is_object_v<T>;",
22316                Style);
22317   verifyFormat("template <typename T>\n"
22318                "concept Tree = true;",
22319                Style);
22320 
22321   verifyFormat("template <typename T> int g(T i) requires Concept1<I> {\n"
22322                "  //...\n"
22323                "}",
22324                Style);
22325 
22326   verifyFormat(
22327       "template <typename T> int g(T i) requires Concept1<I> && Concept2<I> {\n"
22328       "  //...\n"
22329       "}",
22330       Style);
22331 
22332   verifyFormat(
22333       "template <typename T> int g(T i) requires Concept1<I> || Concept2<I> {\n"
22334       "  //...\n"
22335       "}",
22336       Style);
22337 
22338   verifyFormat("template <typename T>\n"
22339                "veryveryvery_long_return_type g(T i) requires Concept1<I> || "
22340                "Concept2<I> {\n"
22341                "  //...\n"
22342                "}",
22343                Style);
22344 
22345   verifyFormat("template <typename T>\n"
22346                "veryveryvery_long_return_type g(T i) requires Concept1<I> && "
22347                "Concept2<I> {\n"
22348                "  //...\n"
22349                "}",
22350                Style);
22351 
22352   verifyFormat(
22353       "template <typename T>\n"
22354       "veryveryvery_long_return_type g(T i) requires Concept1 && Concept2 {\n"
22355       "  //...\n"
22356       "}",
22357       Style);
22358 
22359   verifyFormat(
22360       "template <typename T>\n"
22361       "veryveryvery_long_return_type g(T i) requires Concept1 || Concept2 {\n"
22362       "  //...\n"
22363       "}",
22364       Style);
22365 
22366   verifyFormat("template <typename It>\n"
22367                "requires Foo<It>() && Bar<It> {\n"
22368                "  //....\n"
22369                "}",
22370                Style);
22371 
22372   verifyFormat("template <typename It>\n"
22373                "requires Foo<Bar<It>>() && Bar<Foo<It, It>> {\n"
22374                "  //....\n"
22375                "}",
22376                Style);
22377 
22378   verifyFormat("template <typename It>\n"
22379                "requires Foo<Bar<It, It>>() && Bar<Foo<It, It>> {\n"
22380                "  //....\n"
22381                "}",
22382                Style);
22383 
22384   verifyFormat(
22385       "template <typename It>\n"
22386       "requires Foo<Bar<It>, Baz<It>>() && Bar<Foo<It>, Baz<It, It>> {\n"
22387       "  //....\n"
22388       "}",
22389       Style);
22390 
22391   Style.IndentRequires = true;
22392   verifyFormat("template <typename It>\n"
22393                "  requires Iterator<It>\n"
22394                "void sort(It begin, It end) {\n"
22395                "  //....\n"
22396                "}",
22397                Style);
22398   verifyFormat("template <std::size index_>\n"
22399                "  requires(index_ < sizeof...(Children_))\n"
22400                "Tree auto &child() {\n"
22401                "  // ...\n"
22402                "}",
22403                Style);
22404 
22405   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
22406   verifyFormat("template <typename T>\n"
22407                "concept Hashable = requires (T a) {\n"
22408                "  { std::hash<T>{}(a) } -> std::convertible_to<std::size_t>;\n"
22409                "};",
22410                Style);
22411 
22412   verifyFormat("template <class T = void>\n"
22413                "  requires EqualityComparable<T> || Same<T, void>\n"
22414                "struct equal_to;",
22415                Style);
22416 
22417   verifyFormat("template <class T>\n"
22418                "  requires requires {\n"
22419                "    T{};\n"
22420                "    T (int);\n"
22421                "  }\n",
22422                Style);
22423 
22424   Style.ColumnLimit = 78;
22425   verifyFormat("template <typename T>\n"
22426                "concept Context = Traits<typename T::traits_type> and\n"
22427                "    Interface<typename T::interface_type> and\n"
22428                "    Request<typename T::request_type> and\n"
22429                "    Response<typename T::response_type> and\n"
22430                "    ContextExtension<typename T::extension_type> and\n"
22431                "    ::std::is_copy_constructable<T> and "
22432                "::std::is_move_constructable<T> and\n"
22433                "    requires (T c) {\n"
22434                "  { c.response; } -> Response;\n"
22435                "} and requires (T c) {\n"
22436                "  { c.request; } -> Request;\n"
22437                "}\n",
22438                Style);
22439 
22440   verifyFormat("template <typename T>\n"
22441                "concept Context = Traits<typename T::traits_type> or\n"
22442                "    Interface<typename T::interface_type> or\n"
22443                "    Request<typename T::request_type> or\n"
22444                "    Response<typename T::response_type> or\n"
22445                "    ContextExtension<typename T::extension_type> or\n"
22446                "    ::std::is_copy_constructable<T> or "
22447                "::std::is_move_constructable<T> or\n"
22448                "    requires (T c) {\n"
22449                "  { c.response; } -> Response;\n"
22450                "} or requires (T c) {\n"
22451                "  { c.request; } -> Request;\n"
22452                "}\n",
22453                Style);
22454 
22455   verifyFormat("template <typename T>\n"
22456                "concept Context = Traits<typename T::traits_type> &&\n"
22457                "    Interface<typename T::interface_type> &&\n"
22458                "    Request<typename T::request_type> &&\n"
22459                "    Response<typename T::response_type> &&\n"
22460                "    ContextExtension<typename T::extension_type> &&\n"
22461                "    ::std::is_copy_constructable<T> && "
22462                "::std::is_move_constructable<T> &&\n"
22463                "    requires (T c) {\n"
22464                "  { c.response; } -> Response;\n"
22465                "} && requires (T c) {\n"
22466                "  { c.request; } -> Request;\n"
22467                "}\n",
22468                Style);
22469 
22470   verifyFormat("template <typename T>\nconcept someConcept = Constraint1<T> && "
22471                "Constraint2<T>;");
22472 
22473   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
22474   Style.BraceWrapping.AfterFunction = true;
22475   Style.BraceWrapping.AfterClass = true;
22476   Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
22477   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
22478   verifyFormat("void Foo () requires (std::copyable<T>)\n"
22479                "{\n"
22480                "  return\n"
22481                "}\n",
22482                Style);
22483 
22484   verifyFormat("void Foo () requires std::copyable<T>\n"
22485                "{\n"
22486                "  return\n"
22487                "}\n",
22488                Style);
22489 
22490   verifyFormat("template <std::semiregular F, std::semiregular... Args>\n"
22491                "  requires (std::invocable<F, std::invoke_result_t<Args>...>)\n"
22492                "struct constant;",
22493                Style);
22494 
22495   verifyFormat("template <std::semiregular F, std::semiregular... Args>\n"
22496                "  requires std::invocable<F, std::invoke_result_t<Args>...>\n"
22497                "struct constant;",
22498                Style);
22499 
22500   verifyFormat("template <class T>\n"
22501                "class plane_with_very_very_very_long_name\n"
22502                "{\n"
22503                "  constexpr plane_with_very_very_very_long_name () requires "
22504                "std::copyable<T>\n"
22505                "      : plane_with_very_very_very_long_name (1)\n"
22506                "  {\n"
22507                "  }\n"
22508                "}\n",
22509                Style);
22510 
22511   verifyFormat("template <class T>\n"
22512                "class plane_with_long_name\n"
22513                "{\n"
22514                "  constexpr plane_with_long_name () requires std::copyable<T>\n"
22515                "      : plane_with_long_name (1)\n"
22516                "  {\n"
22517                "  }\n"
22518                "}\n",
22519                Style);
22520 
22521   Style.BreakBeforeConceptDeclarations = false;
22522   verifyFormat("template <typename T> concept Tree = true;", Style);
22523 
22524   Style.IndentRequires = false;
22525   verifyFormat("template <std::semiregular F, std::semiregular... Args>\n"
22526                "requires (std::invocable<F, std::invoke_result_t<Args>...>) "
22527                "struct constant;",
22528                Style);
22529 }
22530 
22531 TEST_F(FormatTest, StatementAttributeLikeMacros) {
22532   FormatStyle Style = getLLVMStyle();
22533   StringRef Source = "void Foo::slot() {\n"
22534                      "  unsigned char MyChar = 'x';\n"
22535                      "  emit signal(MyChar);\n"
22536                      "  Q_EMIT signal(MyChar);\n"
22537                      "}";
22538 
22539   EXPECT_EQ(Source, format(Source, Style));
22540 
22541   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
22542   EXPECT_EQ("void Foo::slot() {\n"
22543             "  unsigned char MyChar = 'x';\n"
22544             "  emit          signal(MyChar);\n"
22545             "  Q_EMIT signal(MyChar);\n"
22546             "}",
22547             format(Source, Style));
22548 
22549   Style.StatementAttributeLikeMacros.push_back("emit");
22550   EXPECT_EQ(Source, format(Source, Style));
22551 
22552   Style.StatementAttributeLikeMacros = {};
22553   EXPECT_EQ("void Foo::slot() {\n"
22554             "  unsigned char MyChar = 'x';\n"
22555             "  emit          signal(MyChar);\n"
22556             "  Q_EMIT        signal(MyChar);\n"
22557             "}",
22558             format(Source, Style));
22559 }
22560 
22561 TEST_F(FormatTest, IndentAccessModifiers) {
22562   FormatStyle Style = getLLVMStyle();
22563   Style.IndentAccessModifiers = true;
22564   // Members are *two* levels below the record;
22565   // Style.IndentWidth == 2, thus yielding a 4 spaces wide indentation.
22566   verifyFormat("class C {\n"
22567                "    int i;\n"
22568                "};\n",
22569                Style);
22570   verifyFormat("union C {\n"
22571                "    int i;\n"
22572                "    unsigned u;\n"
22573                "};\n",
22574                Style);
22575   // Access modifiers should be indented one level below the record.
22576   verifyFormat("class C {\n"
22577                "  public:\n"
22578                "    int i;\n"
22579                "};\n",
22580                Style);
22581   verifyFormat("struct S {\n"
22582                "  private:\n"
22583                "    class C {\n"
22584                "        int j;\n"
22585                "\n"
22586                "      public:\n"
22587                "        C();\n"
22588                "    };\n"
22589                "\n"
22590                "  public:\n"
22591                "    int i;\n"
22592                "};\n",
22593                Style);
22594   // Enumerations are not records and should be unaffected.
22595   Style.AllowShortEnumsOnASingleLine = false;
22596   verifyFormat("enum class E {\n"
22597                "  A,\n"
22598                "  B\n"
22599                "};\n",
22600                Style);
22601   // Test with a different indentation width;
22602   // also proves that the result is Style.AccessModifierOffset agnostic.
22603   Style.IndentWidth = 3;
22604   verifyFormat("class C {\n"
22605                "   public:\n"
22606                "      int i;\n"
22607                "};\n",
22608                Style);
22609 }
22610 
22611 TEST_F(FormatTest, LimitlessStringsAndComments) {
22612   auto Style = getLLVMStyleWithColumns(0);
22613   constexpr StringRef Code =
22614       "/**\n"
22615       " * This is a multiline comment with quite some long lines, at least for "
22616       "the LLVM Style.\n"
22617       " * We will redo this with strings and line comments. Just to  check if "
22618       "everything is working.\n"
22619       " */\n"
22620       "bool foo() {\n"
22621       "  /* Single line multi line comment. */\n"
22622       "  const std::string String = \"This is a multiline string with quite "
22623       "some long lines, at least for the LLVM Style.\"\n"
22624       "                             \"We already did it with multi line "
22625       "comments, and we will do it with line comments. Just to check if "
22626       "everything is working.\";\n"
22627       "  // This is a line comment (block) with quite some long lines, at "
22628       "least for the LLVM Style.\n"
22629       "  // We already did this with multi line comments and strings. Just to "
22630       "check if everything is working.\n"
22631       "  const std::string SmallString = \"Hello World\";\n"
22632       "  // Small line comment\n"
22633       "  return String.size() > SmallString.size();\n"
22634       "}";
22635   EXPECT_EQ(Code, format(Code, Style));
22636 }
22637 
22638 TEST_F(FormatTest, FormatDecayCopy) {
22639   // error cases from unit tests
22640   verifyFormat("foo(auto())");
22641   verifyFormat("foo(auto{})");
22642   verifyFormat("foo(auto({}))");
22643   verifyFormat("foo(auto{{}})");
22644 
22645   verifyFormat("foo(auto(1))");
22646   verifyFormat("foo(auto{1})");
22647   verifyFormat("foo(new auto(1))");
22648   verifyFormat("foo(new auto{1})");
22649   verifyFormat("decltype(auto(1)) x;");
22650   verifyFormat("decltype(auto{1}) x;");
22651   verifyFormat("auto(x);");
22652   verifyFormat("auto{x};");
22653   verifyFormat("new auto{x};");
22654   verifyFormat("auto{x} = y;");
22655   verifyFormat("auto(x) = y;"); // actually a declaration, but this is clearly
22656                                 // the user's own fault
22657   verifyFormat("integral auto(x) = y;"); // actually a declaration, but this is
22658                                          // clearly the user's own fault
22659   verifyFormat("auto(*p)() = f;");       // actually a declaration; TODO FIXME
22660 }
22661 
22662 TEST_F(FormatTest, Cpp20ModulesSupport) {
22663   FormatStyle Style = getLLVMStyle();
22664   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
22665   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
22666 
22667   verifyFormat("export import foo;", Style);
22668   verifyFormat("export import foo:bar;", Style);
22669   verifyFormat("export import foo.bar;", Style);
22670   verifyFormat("export import foo.bar:baz;", Style);
22671   verifyFormat("export import :bar;", Style);
22672   verifyFormat("export module foo:bar;", Style);
22673   verifyFormat("export module foo;", Style);
22674   verifyFormat("export module foo.bar;", Style);
22675   verifyFormat("export module foo.bar:baz;", Style);
22676   verifyFormat("export import <string_view>;", Style);
22677 
22678   verifyFormat("export type_name var;", Style);
22679   verifyFormat("template <class T> export using A = B<T>;", Style);
22680   verifyFormat("export using A = B;", Style);
22681   verifyFormat("export int func() {\n"
22682                "  foo();\n"
22683                "}",
22684                Style);
22685   verifyFormat("export struct {\n"
22686                "  int foo;\n"
22687                "};",
22688                Style);
22689   verifyFormat("export {\n"
22690                "  int foo;\n"
22691                "};",
22692                Style);
22693   verifyFormat("export export char const *hello() { return \"hello\"; }");
22694 
22695   verifyFormat("import bar;", Style);
22696   verifyFormat("import foo.bar;", Style);
22697   verifyFormat("import foo:bar;", Style);
22698   verifyFormat("import :bar;", Style);
22699   verifyFormat("import <ctime>;", Style);
22700   verifyFormat("import \"header\";", Style);
22701 
22702   verifyFormat("module foo;", Style);
22703   verifyFormat("module foo:bar;", Style);
22704   verifyFormat("module foo.bar;", Style);
22705   verifyFormat("module;", Style);
22706 
22707   verifyFormat("export namespace hi {\n"
22708                "const char *sayhi();\n"
22709                "}",
22710                Style);
22711 
22712   verifyFormat("module :private;", Style);
22713   verifyFormat("import <foo/bar.h>;", Style);
22714   verifyFormat("import foo...bar;", Style);
22715   verifyFormat("import ..........;", Style);
22716   verifyFormat("module foo:private;", Style);
22717   verifyFormat("import a", Style);
22718   verifyFormat("module a", Style);
22719   verifyFormat("export import a", Style);
22720   verifyFormat("export module a", Style);
22721 
22722   verifyFormat("import", Style);
22723   verifyFormat("module", Style);
22724   verifyFormat("export", Style);
22725 }
22726 
22727 } // namespace
22728 } // namespace format
22729 } // namespace clang
22730