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   verifyFormat("void foo() {\n"
11777                "  { // asdf\n"
11778                "    { int a; }\n"
11779                "  }\n"
11780                "  {\n"
11781                "    { int b; }\n"
11782                "  }\n"
11783                "}");
11784   verifyFormat("namespace n {\n"
11785                "void foo() {\n"
11786                "  {\n"
11787                "    {\n"
11788                "      statement();\n"
11789                "      if (false) {\n"
11790                "      }\n"
11791                "    }\n"
11792                "  }\n"
11793                "  {}\n"
11794                "}\n"
11795                "} // namespace n");
11796 
11797   // Long lists should be formatted in columns even if they are nested.
11798   verifyFormat(
11799       "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11800       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11801       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11802       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11803       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
11804       "                          1, 22, 333, 4444, 55555, 666666, 7777777});");
11805 
11806   // Allow "single-column" layout even if that violates the column limit. There
11807   // isn't going to be a better way.
11808   verifyFormat("std::vector<int> a = {\n"
11809                "    aaaaaaaa,\n"
11810                "    aaaaaaaa,\n"
11811                "    aaaaaaaa,\n"
11812                "    aaaaaaaa,\n"
11813                "    aaaaaaaaaa,\n"
11814                "    aaaaaaaa,\n"
11815                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa};",
11816                getLLVMStyleWithColumns(30));
11817   verifyFormat("vector<int> aaaa = {\n"
11818                "    aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
11819                "    aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
11820                "    aaaaaa.aaaaaaa,\n"
11821                "    aaaaaa.aaaaaaa,\n"
11822                "    aaaaaa.aaaaaaa,\n"
11823                "    aaaaaa.aaaaaaa,\n"
11824                "};");
11825 
11826   // Don't create hanging lists.
11827   verifyFormat("someFunction(Param, {List1, List2,\n"
11828                "                     List3});",
11829                getLLVMStyleWithColumns(35));
11830   verifyFormat("someFunction(Param, Param,\n"
11831                "             {List1, List2,\n"
11832                "              List3});",
11833                getLLVMStyleWithColumns(35));
11834   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n"
11835                "                               aaaaaaaaaaaaaaaaaaaaaaa);");
11836 }
11837 
11838 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
11839   FormatStyle DoNotMerge = getLLVMStyle();
11840   DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
11841 
11842   verifyFormat("void f() { return 42; }");
11843   verifyFormat("void f() {\n"
11844                "  return 42;\n"
11845                "}",
11846                DoNotMerge);
11847   verifyFormat("void f() {\n"
11848                "  // Comment\n"
11849                "}");
11850   verifyFormat("{\n"
11851                "#error {\n"
11852                "  int a;\n"
11853                "}");
11854   verifyFormat("{\n"
11855                "  int a;\n"
11856                "#error {\n"
11857                "}");
11858   verifyFormat("void f() {} // comment");
11859   verifyFormat("void f() { int a; } // comment");
11860   verifyFormat("void f() {\n"
11861                "} // comment",
11862                DoNotMerge);
11863   verifyFormat("void f() {\n"
11864                "  int a;\n"
11865                "} // comment",
11866                DoNotMerge);
11867   verifyFormat("void f() {\n"
11868                "} // comment",
11869                getLLVMStyleWithColumns(15));
11870 
11871   verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
11872   verifyFormat("void f() {\n  return 42;\n}", getLLVMStyleWithColumns(22));
11873 
11874   verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
11875   verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
11876   verifyFormat("class C {\n"
11877                "  C()\n"
11878                "      : iiiiiiii(nullptr),\n"
11879                "        kkkkkkk(nullptr),\n"
11880                "        mmmmmmm(nullptr),\n"
11881                "        nnnnnnn(nullptr) {}\n"
11882                "};",
11883                getGoogleStyle());
11884 
11885   FormatStyle NoColumnLimit = getLLVMStyle();
11886   NoColumnLimit.ColumnLimit = 0;
11887   EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit));
11888   EXPECT_EQ("class C {\n"
11889             "  A() : b(0) {}\n"
11890             "};",
11891             format("class C{A():b(0){}};", NoColumnLimit));
11892   EXPECT_EQ("A()\n"
11893             "    : b(0) {\n"
11894             "}",
11895             format("A()\n:b(0)\n{\n}", NoColumnLimit));
11896 
11897   FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
11898   DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
11899       FormatStyle::SFS_None;
11900   EXPECT_EQ("A()\n"
11901             "    : b(0) {\n"
11902             "}",
11903             format("A():b(0){}", DoNotMergeNoColumnLimit));
11904   EXPECT_EQ("A()\n"
11905             "    : b(0) {\n"
11906             "}",
11907             format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit));
11908 
11909   verifyFormat("#define A          \\\n"
11910                "  void f() {       \\\n"
11911                "    int i;         \\\n"
11912                "  }",
11913                getLLVMStyleWithColumns(20));
11914   verifyFormat("#define A           \\\n"
11915                "  void f() { int i; }",
11916                getLLVMStyleWithColumns(21));
11917   verifyFormat("#define A            \\\n"
11918                "  void f() {         \\\n"
11919                "    int i;           \\\n"
11920                "  }                  \\\n"
11921                "  int j;",
11922                getLLVMStyleWithColumns(22));
11923   verifyFormat("#define A             \\\n"
11924                "  void f() { int i; } \\\n"
11925                "  int j;",
11926                getLLVMStyleWithColumns(23));
11927 }
11928 
11929 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) {
11930   FormatStyle MergeEmptyOnly = getLLVMStyle();
11931   MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
11932   verifyFormat("class C {\n"
11933                "  int f() {}\n"
11934                "};",
11935                MergeEmptyOnly);
11936   verifyFormat("class C {\n"
11937                "  int f() {\n"
11938                "    return 42;\n"
11939                "  }\n"
11940                "};",
11941                MergeEmptyOnly);
11942   verifyFormat("int f() {}", MergeEmptyOnly);
11943   verifyFormat("int f() {\n"
11944                "  return 42;\n"
11945                "}",
11946                MergeEmptyOnly);
11947 
11948   // Also verify behavior when BraceWrapping.AfterFunction = true
11949   MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
11950   MergeEmptyOnly.BraceWrapping.AfterFunction = true;
11951   verifyFormat("int f() {}", MergeEmptyOnly);
11952   verifyFormat("class C {\n"
11953                "  int f() {}\n"
11954                "};",
11955                MergeEmptyOnly);
11956 }
11957 
11958 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
11959   FormatStyle MergeInlineOnly = getLLVMStyle();
11960   MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
11961   verifyFormat("class C {\n"
11962                "  int f() { return 42; }\n"
11963                "};",
11964                MergeInlineOnly);
11965   verifyFormat("int f() {\n"
11966                "  return 42;\n"
11967                "}",
11968                MergeInlineOnly);
11969 
11970   // SFS_Inline implies SFS_Empty
11971   verifyFormat("class C {\n"
11972                "  int f() {}\n"
11973                "};",
11974                MergeInlineOnly);
11975   verifyFormat("int f() {}", MergeInlineOnly);
11976 
11977   // Also verify behavior when BraceWrapping.AfterFunction = true
11978   MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
11979   MergeInlineOnly.BraceWrapping.AfterFunction = true;
11980   verifyFormat("class C {\n"
11981                "  int f() { return 42; }\n"
11982                "};",
11983                MergeInlineOnly);
11984   verifyFormat("int f()\n"
11985                "{\n"
11986                "  return 42;\n"
11987                "}",
11988                MergeInlineOnly);
11989 
11990   // SFS_Inline implies SFS_Empty
11991   verifyFormat("int f() {}", MergeInlineOnly);
11992   verifyFormat("class C {\n"
11993                "  int f() {}\n"
11994                "};",
11995                MergeInlineOnly);
11996 }
11997 
11998 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) {
11999   FormatStyle MergeInlineOnly = getLLVMStyle();
12000   MergeInlineOnly.AllowShortFunctionsOnASingleLine =
12001       FormatStyle::SFS_InlineOnly;
12002   verifyFormat("class C {\n"
12003                "  int f() { return 42; }\n"
12004                "};",
12005                MergeInlineOnly);
12006   verifyFormat("int f() {\n"
12007                "  return 42;\n"
12008                "}",
12009                MergeInlineOnly);
12010 
12011   // SFS_InlineOnly does not imply SFS_Empty
12012   verifyFormat("class C {\n"
12013                "  int f() {}\n"
12014                "};",
12015                MergeInlineOnly);
12016   verifyFormat("int f() {\n"
12017                "}",
12018                MergeInlineOnly);
12019 
12020   // Also verify behavior when BraceWrapping.AfterFunction = true
12021   MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
12022   MergeInlineOnly.BraceWrapping.AfterFunction = true;
12023   verifyFormat("class C {\n"
12024                "  int f() { return 42; }\n"
12025                "};",
12026                MergeInlineOnly);
12027   verifyFormat("int f()\n"
12028                "{\n"
12029                "  return 42;\n"
12030                "}",
12031                MergeInlineOnly);
12032 
12033   // SFS_InlineOnly does not imply SFS_Empty
12034   verifyFormat("int f()\n"
12035                "{\n"
12036                "}",
12037                MergeInlineOnly);
12038   verifyFormat("class C {\n"
12039                "  int f() {}\n"
12040                "};",
12041                MergeInlineOnly);
12042 }
12043 
12044 TEST_F(FormatTest, SplitEmptyFunction) {
12045   FormatStyle Style = getLLVMStyle();
12046   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
12047   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12048   Style.BraceWrapping.AfterFunction = true;
12049   Style.BraceWrapping.SplitEmptyFunction = false;
12050   Style.ColumnLimit = 40;
12051 
12052   verifyFormat("int f()\n"
12053                "{}",
12054                Style);
12055   verifyFormat("int f()\n"
12056                "{\n"
12057                "  return 42;\n"
12058                "}",
12059                Style);
12060   verifyFormat("int f()\n"
12061                "{\n"
12062                "  // some comment\n"
12063                "}",
12064                Style);
12065 
12066   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
12067   verifyFormat("int f() {}", Style);
12068   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
12069                "{}",
12070                Style);
12071   verifyFormat("int f()\n"
12072                "{\n"
12073                "  return 0;\n"
12074                "}",
12075                Style);
12076 
12077   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
12078   verifyFormat("class Foo {\n"
12079                "  int f() {}\n"
12080                "};\n",
12081                Style);
12082   verifyFormat("class Foo {\n"
12083                "  int f() { return 0; }\n"
12084                "};\n",
12085                Style);
12086   verifyFormat("class Foo {\n"
12087                "  int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
12088                "  {}\n"
12089                "};\n",
12090                Style);
12091   verifyFormat("class Foo {\n"
12092                "  int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
12093                "  {\n"
12094                "    return 0;\n"
12095                "  }\n"
12096                "};\n",
12097                Style);
12098 
12099   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
12100   verifyFormat("int f() {}", Style);
12101   verifyFormat("int f() { return 0; }", Style);
12102   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
12103                "{}",
12104                Style);
12105   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
12106                "{\n"
12107                "  return 0;\n"
12108                "}",
12109                Style);
12110 }
12111 TEST_F(FormatTest, KeepShortFunctionAfterPPElse) {
12112   FormatStyle Style = getLLVMStyle();
12113   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
12114   verifyFormat("#ifdef A\n"
12115                "int f() {}\n"
12116                "#else\n"
12117                "int g() {}\n"
12118                "#endif",
12119                Style);
12120 }
12121 
12122 TEST_F(FormatTest, SplitEmptyClass) {
12123   FormatStyle Style = getLLVMStyle();
12124   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12125   Style.BraceWrapping.AfterClass = true;
12126   Style.BraceWrapping.SplitEmptyRecord = false;
12127 
12128   verifyFormat("class Foo\n"
12129                "{};",
12130                Style);
12131   verifyFormat("/* something */ class Foo\n"
12132                "{};",
12133                Style);
12134   verifyFormat("template <typename X> class Foo\n"
12135                "{};",
12136                Style);
12137   verifyFormat("class Foo\n"
12138                "{\n"
12139                "  Foo();\n"
12140                "};",
12141                Style);
12142   verifyFormat("typedef class Foo\n"
12143                "{\n"
12144                "} Foo_t;",
12145                Style);
12146 
12147   Style.BraceWrapping.SplitEmptyRecord = true;
12148   Style.BraceWrapping.AfterStruct = true;
12149   verifyFormat("class rep\n"
12150                "{\n"
12151                "};",
12152                Style);
12153   verifyFormat("struct rep\n"
12154                "{\n"
12155                "};",
12156                Style);
12157   verifyFormat("template <typename T> class rep\n"
12158                "{\n"
12159                "};",
12160                Style);
12161   verifyFormat("template <typename T> struct rep\n"
12162                "{\n"
12163                "};",
12164                Style);
12165   verifyFormat("class rep\n"
12166                "{\n"
12167                "  int x;\n"
12168                "};",
12169                Style);
12170   verifyFormat("struct rep\n"
12171                "{\n"
12172                "  int x;\n"
12173                "};",
12174                Style);
12175   verifyFormat("template <typename T> class rep\n"
12176                "{\n"
12177                "  int x;\n"
12178                "};",
12179                Style);
12180   verifyFormat("template <typename T> struct rep\n"
12181                "{\n"
12182                "  int x;\n"
12183                "};",
12184                Style);
12185   verifyFormat("template <typename T> class rep // Foo\n"
12186                "{\n"
12187                "  int x;\n"
12188                "};",
12189                Style);
12190   verifyFormat("template <typename T> struct rep // Bar\n"
12191                "{\n"
12192                "  int x;\n"
12193                "};",
12194                Style);
12195 
12196   verifyFormat("template <typename T> class rep<T>\n"
12197                "{\n"
12198                "  int x;\n"
12199                "};",
12200                Style);
12201 
12202   verifyFormat("template <typename T> class rep<std::complex<T>>\n"
12203                "{\n"
12204                "  int x;\n"
12205                "};",
12206                Style);
12207   verifyFormat("template <typename T> class rep<std::complex<T>>\n"
12208                "{\n"
12209                "};",
12210                Style);
12211 
12212   verifyFormat("#include \"stdint.h\"\n"
12213                "namespace rep {}",
12214                Style);
12215   verifyFormat("#include <stdint.h>\n"
12216                "namespace rep {}",
12217                Style);
12218   verifyFormat("#include <stdint.h>\n"
12219                "namespace rep {}",
12220                "#include <stdint.h>\n"
12221                "namespace rep {\n"
12222                "\n"
12223                "\n"
12224                "}",
12225                Style);
12226 }
12227 
12228 TEST_F(FormatTest, SplitEmptyStruct) {
12229   FormatStyle Style = getLLVMStyle();
12230   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12231   Style.BraceWrapping.AfterStruct = true;
12232   Style.BraceWrapping.SplitEmptyRecord = false;
12233 
12234   verifyFormat("struct Foo\n"
12235                "{};",
12236                Style);
12237   verifyFormat("/* something */ struct Foo\n"
12238                "{};",
12239                Style);
12240   verifyFormat("template <typename X> struct Foo\n"
12241                "{};",
12242                Style);
12243   verifyFormat("struct Foo\n"
12244                "{\n"
12245                "  Foo();\n"
12246                "};",
12247                Style);
12248   verifyFormat("typedef struct Foo\n"
12249                "{\n"
12250                "} Foo_t;",
12251                Style);
12252   // typedef struct Bar {} Bar_t;
12253 }
12254 
12255 TEST_F(FormatTest, SplitEmptyUnion) {
12256   FormatStyle Style = getLLVMStyle();
12257   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12258   Style.BraceWrapping.AfterUnion = true;
12259   Style.BraceWrapping.SplitEmptyRecord = false;
12260 
12261   verifyFormat("union Foo\n"
12262                "{};",
12263                Style);
12264   verifyFormat("/* something */ union Foo\n"
12265                "{};",
12266                Style);
12267   verifyFormat("union Foo\n"
12268                "{\n"
12269                "  A,\n"
12270                "};",
12271                Style);
12272   verifyFormat("typedef union Foo\n"
12273                "{\n"
12274                "} Foo_t;",
12275                Style);
12276 }
12277 
12278 TEST_F(FormatTest, SplitEmptyNamespace) {
12279   FormatStyle Style = getLLVMStyle();
12280   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12281   Style.BraceWrapping.AfterNamespace = true;
12282   Style.BraceWrapping.SplitEmptyNamespace = false;
12283 
12284   verifyFormat("namespace Foo\n"
12285                "{};",
12286                Style);
12287   verifyFormat("/* something */ namespace Foo\n"
12288                "{};",
12289                Style);
12290   verifyFormat("inline namespace Foo\n"
12291                "{};",
12292                Style);
12293   verifyFormat("/* something */ inline namespace Foo\n"
12294                "{};",
12295                Style);
12296   verifyFormat("export namespace Foo\n"
12297                "{};",
12298                Style);
12299   verifyFormat("namespace Foo\n"
12300                "{\n"
12301                "void Bar();\n"
12302                "};",
12303                Style);
12304 }
12305 
12306 TEST_F(FormatTest, NeverMergeShortRecords) {
12307   FormatStyle Style = getLLVMStyle();
12308 
12309   verifyFormat("class Foo {\n"
12310                "  Foo();\n"
12311                "};",
12312                Style);
12313   verifyFormat("typedef class Foo {\n"
12314                "  Foo();\n"
12315                "} Foo_t;",
12316                Style);
12317   verifyFormat("struct Foo {\n"
12318                "  Foo();\n"
12319                "};",
12320                Style);
12321   verifyFormat("typedef struct Foo {\n"
12322                "  Foo();\n"
12323                "} Foo_t;",
12324                Style);
12325   verifyFormat("union Foo {\n"
12326                "  A,\n"
12327                "};",
12328                Style);
12329   verifyFormat("typedef union Foo {\n"
12330                "  A,\n"
12331                "} Foo_t;",
12332                Style);
12333   verifyFormat("namespace Foo {\n"
12334                "void Bar();\n"
12335                "};",
12336                Style);
12337 
12338   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12339   Style.BraceWrapping.AfterClass = true;
12340   Style.BraceWrapping.AfterStruct = true;
12341   Style.BraceWrapping.AfterUnion = true;
12342   Style.BraceWrapping.AfterNamespace = true;
12343   verifyFormat("class Foo\n"
12344                "{\n"
12345                "  Foo();\n"
12346                "};",
12347                Style);
12348   verifyFormat("typedef class Foo\n"
12349                "{\n"
12350                "  Foo();\n"
12351                "} Foo_t;",
12352                Style);
12353   verifyFormat("struct Foo\n"
12354                "{\n"
12355                "  Foo();\n"
12356                "};",
12357                Style);
12358   verifyFormat("typedef struct Foo\n"
12359                "{\n"
12360                "  Foo();\n"
12361                "} Foo_t;",
12362                Style);
12363   verifyFormat("union Foo\n"
12364                "{\n"
12365                "  A,\n"
12366                "};",
12367                Style);
12368   verifyFormat("typedef union Foo\n"
12369                "{\n"
12370                "  A,\n"
12371                "} Foo_t;",
12372                Style);
12373   verifyFormat("namespace Foo\n"
12374                "{\n"
12375                "void Bar();\n"
12376                "};",
12377                Style);
12378 }
12379 
12380 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
12381   // Elaborate type variable declarations.
12382   verifyFormat("struct foo a = {bar};\nint n;");
12383   verifyFormat("class foo a = {bar};\nint n;");
12384   verifyFormat("union foo a = {bar};\nint n;");
12385 
12386   // Elaborate types inside function definitions.
12387   verifyFormat("struct foo f() {}\nint n;");
12388   verifyFormat("class foo f() {}\nint n;");
12389   verifyFormat("union foo f() {}\nint n;");
12390 
12391   // Templates.
12392   verifyFormat("template <class X> void f() {}\nint n;");
12393   verifyFormat("template <struct X> void f() {}\nint n;");
12394   verifyFormat("template <union X> void f() {}\nint n;");
12395 
12396   // Actual definitions...
12397   verifyFormat("struct {\n} n;");
12398   verifyFormat(
12399       "template <template <class T, class Y>, class Z> class X {\n} n;");
12400   verifyFormat("union Z {\n  int n;\n} x;");
12401   verifyFormat("class MACRO Z {\n} n;");
12402   verifyFormat("class MACRO(X) Z {\n} n;");
12403   verifyFormat("class __attribute__(X) Z {\n} n;");
12404   verifyFormat("class __declspec(X) Z {\n} n;");
12405   verifyFormat("class A##B##C {\n} n;");
12406   verifyFormat("class alignas(16) Z {\n} n;");
12407   verifyFormat("class MACRO(X) alignas(16) Z {\n} n;");
12408   verifyFormat("class MACROA MACRO(X) Z {\n} n;");
12409 
12410   // Redefinition from nested context:
12411   verifyFormat("class A::B::C {\n} n;");
12412 
12413   // Template definitions.
12414   verifyFormat(
12415       "template <typename F>\n"
12416       "Matcher(const Matcher<F> &Other,\n"
12417       "        typename enable_if_c<is_base_of<F, T>::value &&\n"
12418       "                             !is_same<F, T>::value>::type * = 0)\n"
12419       "    : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
12420 
12421   // FIXME: This is still incorrectly handled at the formatter side.
12422   verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
12423   verifyFormat("int i = SomeFunction(a<b, a> b);");
12424 
12425   // FIXME:
12426   // This now gets parsed incorrectly as class definition.
12427   // verifyFormat("class A<int> f() {\n}\nint n;");
12428 
12429   // Elaborate types where incorrectly parsing the structural element would
12430   // break the indent.
12431   verifyFormat("if (true)\n"
12432                "  class X x;\n"
12433                "else\n"
12434                "  f();\n");
12435 
12436   // This is simply incomplete. Formatting is not important, but must not crash.
12437   verifyFormat("class A:");
12438 }
12439 
12440 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
12441   EXPECT_EQ("#error Leave     all         white!!!!! space* alone!\n",
12442             format("#error Leave     all         white!!!!! space* alone!\n"));
12443   EXPECT_EQ(
12444       "#warning Leave     all         white!!!!! space* alone!\n",
12445       format("#warning Leave     all         white!!!!! space* alone!\n"));
12446   EXPECT_EQ("#error 1", format("  #  error   1"));
12447   EXPECT_EQ("#warning 1", format("  #  warning 1"));
12448 }
12449 
12450 TEST_F(FormatTest, FormatHashIfExpressions) {
12451   verifyFormat("#if AAAA && BBBB");
12452   verifyFormat("#if (AAAA && BBBB)");
12453   verifyFormat("#elif (AAAA && BBBB)");
12454   // FIXME: Come up with a better indentation for #elif.
12455   verifyFormat(
12456       "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) &&  \\\n"
12457       "    defined(BBBBBBBB)\n"
12458       "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) &&  \\\n"
12459       "    defined(BBBBBBBB)\n"
12460       "#endif",
12461       getLLVMStyleWithColumns(65));
12462 }
12463 
12464 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
12465   FormatStyle AllowsMergedIf = getGoogleStyle();
12466   AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
12467       FormatStyle::SIS_WithoutElse;
12468   verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
12469   verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
12470   verifyFormat("if (true)\n#error E\n  return 42;", AllowsMergedIf);
12471   EXPECT_EQ("if (true) return 42;",
12472             format("if (true)\nreturn 42;", AllowsMergedIf));
12473   FormatStyle ShortMergedIf = AllowsMergedIf;
12474   ShortMergedIf.ColumnLimit = 25;
12475   verifyFormat("#define A \\\n"
12476                "  if (true) return 42;",
12477                ShortMergedIf);
12478   verifyFormat("#define A \\\n"
12479                "  f();    \\\n"
12480                "  if (true)\n"
12481                "#define B",
12482                ShortMergedIf);
12483   verifyFormat("#define A \\\n"
12484                "  f();    \\\n"
12485                "  if (true)\n"
12486                "g();",
12487                ShortMergedIf);
12488   verifyFormat("{\n"
12489                "#ifdef A\n"
12490                "  // Comment\n"
12491                "  if (true) continue;\n"
12492                "#endif\n"
12493                "  // Comment\n"
12494                "  if (true) continue;\n"
12495                "}",
12496                ShortMergedIf);
12497   ShortMergedIf.ColumnLimit = 33;
12498   verifyFormat("#define A \\\n"
12499                "  if constexpr (true) return 42;",
12500                ShortMergedIf);
12501   verifyFormat("#define A \\\n"
12502                "  if CONSTEXPR (true) return 42;",
12503                ShortMergedIf);
12504   ShortMergedIf.ColumnLimit = 29;
12505   verifyFormat("#define A                   \\\n"
12506                "  if (aaaaaaaaaa) return 1; \\\n"
12507                "  return 2;",
12508                ShortMergedIf);
12509   ShortMergedIf.ColumnLimit = 28;
12510   verifyFormat("#define A         \\\n"
12511                "  if (aaaaaaaaaa) \\\n"
12512                "    return 1;     \\\n"
12513                "  return 2;",
12514                ShortMergedIf);
12515   verifyFormat("#define A                \\\n"
12516                "  if constexpr (aaaaaaa) \\\n"
12517                "    return 1;            \\\n"
12518                "  return 2;",
12519                ShortMergedIf);
12520   verifyFormat("#define A                \\\n"
12521                "  if CONSTEXPR (aaaaaaa) \\\n"
12522                "    return 1;            \\\n"
12523                "  return 2;",
12524                ShortMergedIf);
12525 }
12526 
12527 TEST_F(FormatTest, FormatStarDependingOnContext) {
12528   verifyFormat("void f(int *a);");
12529   verifyFormat("void f() { f(fint * b); }");
12530   verifyFormat("class A {\n  void f(int *a);\n};");
12531   verifyFormat("class A {\n  int *a;\n};");
12532   verifyFormat("namespace a {\n"
12533                "namespace b {\n"
12534                "class A {\n"
12535                "  void f() {}\n"
12536                "  int *a;\n"
12537                "};\n"
12538                "} // namespace b\n"
12539                "} // namespace a");
12540 }
12541 
12542 TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
12543   verifyFormat("while");
12544   verifyFormat("operator");
12545 }
12546 
12547 TEST_F(FormatTest, SkipsDeeplyNestedLines) {
12548   // This code would be painfully slow to format if we didn't skip it.
12549   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
12550                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
12551                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
12552                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
12553                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
12554                    "A(1, 1)\n"
12555                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x
12556                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12557                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12558                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12559                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12560                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12561                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12562                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12563                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12564                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n");
12565   // Deeply nested part is untouched, rest is formatted.
12566   EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n",
12567             format(std::string("int    i;\n") + Code + "int    j;\n",
12568                    getLLVMStyle(), SC_ExpectIncomplete));
12569 }
12570 
12571 //===----------------------------------------------------------------------===//
12572 // Objective-C tests.
12573 //===----------------------------------------------------------------------===//
12574 
12575 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
12576   verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
12577   EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
12578             format("-(NSUInteger)indexOfObject:(id)anObject;"));
12579   EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
12580   EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
12581   EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
12582             format("-(NSInteger)Method3:(id)anObject;"));
12583   EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
12584             format("-(NSInteger)Method4:(id)anObject;"));
12585   EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
12586             format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
12587   EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
12588             format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
12589   EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject "
12590             "forAllCells:(BOOL)flag;",
12591             format("- (void)sendAction:(SEL)aSelector to:(id)anObject "
12592                    "forAllCells:(BOOL)flag;"));
12593 
12594   // Very long objectiveC method declaration.
12595   verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
12596                "    (SoooooooooooooooooooooomeType *)bbbbbbbbbb;");
12597   verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
12598                "                    inRange:(NSRange)range\n"
12599                "                   outRange:(NSRange)out_range\n"
12600                "                  outRange1:(NSRange)out_range1\n"
12601                "                  outRange2:(NSRange)out_range2\n"
12602                "                  outRange3:(NSRange)out_range3\n"
12603                "                  outRange4:(NSRange)out_range4\n"
12604                "                  outRange5:(NSRange)out_range5\n"
12605                "                  outRange6:(NSRange)out_range6\n"
12606                "                  outRange7:(NSRange)out_range7\n"
12607                "                  outRange8:(NSRange)out_range8\n"
12608                "                  outRange9:(NSRange)out_range9;");
12609 
12610   // When the function name has to be wrapped.
12611   FormatStyle Style = getLLVMStyle();
12612   // ObjC ignores IndentWrappedFunctionNames when wrapping methods
12613   // and always indents instead.
12614   Style.IndentWrappedFunctionNames = false;
12615   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
12616                "    veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
12617                "               anotherName:(NSString)bbbbbbbbbbbbbb {\n"
12618                "}",
12619                Style);
12620   Style.IndentWrappedFunctionNames = true;
12621   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
12622                "    veryLooooooooooongName:(NSString)cccccccccccccc\n"
12623                "               anotherName:(NSString)dddddddddddddd {\n"
12624                "}",
12625                Style);
12626 
12627   verifyFormat("- (int)sum:(vector<int>)numbers;");
12628   verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
12629   // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
12630   // protocol lists (but not for template classes):
12631   // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
12632 
12633   verifyFormat("- (int (*)())foo:(int (*)())f;");
12634   verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
12635 
12636   // If there's no return type (very rare in practice!), LLVM and Google style
12637   // agree.
12638   verifyFormat("- foo;");
12639   verifyFormat("- foo:(int)f;");
12640   verifyGoogleFormat("- foo:(int)foo;");
12641 }
12642 
12643 TEST_F(FormatTest, BreaksStringLiterals) {
12644   EXPECT_EQ("\"some text \"\n"
12645             "\"other\";",
12646             format("\"some text other\";", getLLVMStyleWithColumns(12)));
12647   EXPECT_EQ("\"some text \"\n"
12648             "\"other\";",
12649             format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
12650   EXPECT_EQ(
12651       "#define A  \\\n"
12652       "  \"some \"  \\\n"
12653       "  \"text \"  \\\n"
12654       "  \"other\";",
12655       format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
12656   EXPECT_EQ(
12657       "#define A  \\\n"
12658       "  \"so \"    \\\n"
12659       "  \"text \"  \\\n"
12660       "  \"other\";",
12661       format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
12662 
12663   EXPECT_EQ("\"some text\"",
12664             format("\"some text\"", getLLVMStyleWithColumns(1)));
12665   EXPECT_EQ("\"some text\"",
12666             format("\"some text\"", getLLVMStyleWithColumns(11)));
12667   EXPECT_EQ("\"some \"\n"
12668             "\"text\"",
12669             format("\"some text\"", getLLVMStyleWithColumns(10)));
12670   EXPECT_EQ("\"some \"\n"
12671             "\"text\"",
12672             format("\"some text\"", getLLVMStyleWithColumns(7)));
12673   EXPECT_EQ("\"some\"\n"
12674             "\" tex\"\n"
12675             "\"t\"",
12676             format("\"some text\"", getLLVMStyleWithColumns(6)));
12677   EXPECT_EQ("\"some\"\n"
12678             "\" tex\"\n"
12679             "\" and\"",
12680             format("\"some tex and\"", getLLVMStyleWithColumns(6)));
12681   EXPECT_EQ("\"some\"\n"
12682             "\"/tex\"\n"
12683             "\"/and\"",
12684             format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
12685 
12686   EXPECT_EQ("variable =\n"
12687             "    \"long string \"\n"
12688             "    \"literal\";",
12689             format("variable = \"long string literal\";",
12690                    getLLVMStyleWithColumns(20)));
12691 
12692   EXPECT_EQ("variable = f(\n"
12693             "    \"long string \"\n"
12694             "    \"literal\",\n"
12695             "    short,\n"
12696             "    loooooooooooooooooooong);",
12697             format("variable = f(\"long string literal\", short, "
12698                    "loooooooooooooooooooong);",
12699                    getLLVMStyleWithColumns(20)));
12700 
12701   EXPECT_EQ(
12702       "f(g(\"long string \"\n"
12703       "    \"literal\"),\n"
12704       "  b);",
12705       format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20)));
12706   EXPECT_EQ("f(g(\"long string \"\n"
12707             "    \"literal\",\n"
12708             "    a),\n"
12709             "  b);",
12710             format("f(g(\"long string literal\", a), b);",
12711                    getLLVMStyleWithColumns(20)));
12712   EXPECT_EQ(
12713       "f(\"one two\".split(\n"
12714       "    variable));",
12715       format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
12716   EXPECT_EQ("f(\"one two three four five six \"\n"
12717             "  \"seven\".split(\n"
12718             "      really_looooong_variable));",
12719             format("f(\"one two three four five six seven\"."
12720                    "split(really_looooong_variable));",
12721                    getLLVMStyleWithColumns(33)));
12722 
12723   EXPECT_EQ("f(\"some \"\n"
12724             "  \"text\",\n"
12725             "  other);",
12726             format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
12727 
12728   // Only break as a last resort.
12729   verifyFormat(
12730       "aaaaaaaaaaaaaaaaaaaa(\n"
12731       "    aaaaaaaaaaaaaaaaaaaa,\n"
12732       "    aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
12733 
12734   EXPECT_EQ("\"splitmea\"\n"
12735             "\"trandomp\"\n"
12736             "\"oint\"",
12737             format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
12738 
12739   EXPECT_EQ("\"split/\"\n"
12740             "\"pathat/\"\n"
12741             "\"slashes\"",
12742             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
12743 
12744   EXPECT_EQ("\"split/\"\n"
12745             "\"pathat/\"\n"
12746             "\"slashes\"",
12747             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
12748   EXPECT_EQ("\"split at \"\n"
12749             "\"spaces/at/\"\n"
12750             "\"slashes.at.any$\"\n"
12751             "\"non-alphanumeric%\"\n"
12752             "\"1111111111characte\"\n"
12753             "\"rs\"",
12754             format("\"split at "
12755                    "spaces/at/"
12756                    "slashes.at."
12757                    "any$non-"
12758                    "alphanumeric%"
12759                    "1111111111characte"
12760                    "rs\"",
12761                    getLLVMStyleWithColumns(20)));
12762 
12763   // Verify that splitting the strings understands
12764   // Style::AlwaysBreakBeforeMultilineStrings.
12765   EXPECT_EQ("aaaaaaaaaaaa(\n"
12766             "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
12767             "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
12768             format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa "
12769                    "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
12770                    "aaaaaaaaaaaaaaaaaaaaaa\");",
12771                    getGoogleStyle()));
12772   EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
12773             "       \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
12774             format("return \"aaaaaaaaaaaaaaaaaaaaaa "
12775                    "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
12776                    "aaaaaaaaaaaaaaaaaaaaaa\";",
12777                    getGoogleStyle()));
12778   EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
12779             "                \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
12780             format("llvm::outs() << "
12781                    "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
12782                    "aaaaaaaaaaaaaaaaaaa\";"));
12783   EXPECT_EQ("ffff(\n"
12784             "    {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
12785             "     \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
12786             format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
12787                    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
12788                    getGoogleStyle()));
12789 
12790   FormatStyle Style = getLLVMStyleWithColumns(12);
12791   Style.BreakStringLiterals = false;
12792   EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style));
12793 
12794   FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
12795   AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
12796   EXPECT_EQ("#define A \\\n"
12797             "  \"some \" \\\n"
12798             "  \"text \" \\\n"
12799             "  \"other\";",
12800             format("#define A \"some text other\";", AlignLeft));
12801 }
12802 
12803 TEST_F(FormatTest, BreaksStringLiteralsAtColumnLimit) {
12804   EXPECT_EQ("C a = \"some more \"\n"
12805             "      \"text\";",
12806             format("C a = \"some more text\";", getLLVMStyleWithColumns(18)));
12807 }
12808 
12809 TEST_F(FormatTest, FullyRemoveEmptyLines) {
12810   FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80);
12811   NoEmptyLines.MaxEmptyLinesToKeep = 0;
12812   EXPECT_EQ("int i = a(b());",
12813             format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines));
12814 }
12815 
12816 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
12817   EXPECT_EQ(
12818       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
12819       "(\n"
12820       "    \"x\t\");",
12821       format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
12822              "aaaaaaa("
12823              "\"x\t\");"));
12824 }
12825 
12826 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
12827   EXPECT_EQ(
12828       "u8\"utf8 string \"\n"
12829       "u8\"literal\";",
12830       format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
12831   EXPECT_EQ(
12832       "u\"utf16 string \"\n"
12833       "u\"literal\";",
12834       format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
12835   EXPECT_EQ(
12836       "U\"utf32 string \"\n"
12837       "U\"literal\";",
12838       format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
12839   EXPECT_EQ("L\"wide string \"\n"
12840             "L\"literal\";",
12841             format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
12842   EXPECT_EQ("@\"NSString \"\n"
12843             "@\"literal\";",
12844             format("@\"NSString literal\";", getGoogleStyleWithColumns(19)));
12845   verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26));
12846 
12847   // This input makes clang-format try to split the incomplete unicode escape
12848   // sequence, which used to lead to a crasher.
12849   verifyNoCrash(
12850       "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
12851       getLLVMStyleWithColumns(60));
12852 }
12853 
12854 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) {
12855   FormatStyle Style = getGoogleStyleWithColumns(15);
12856   EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style));
12857   EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style));
12858   EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style));
12859   EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style));
12860   EXPECT_EQ("u8R\"x(raw literal)x\";",
12861             format("u8R\"x(raw literal)x\";", Style));
12862 }
12863 
12864 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
12865   FormatStyle Style = getLLVMStyleWithColumns(20);
12866   EXPECT_EQ(
12867       "_T(\"aaaaaaaaaaaaaa\")\n"
12868       "_T(\"aaaaaaaaaaaaaa\")\n"
12869       "_T(\"aaaaaaaaaaaa\")",
12870       format("  _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
12871   EXPECT_EQ("f(x,\n"
12872             "  _T(\"aaaaaaaaaaaa\")\n"
12873             "  _T(\"aaa\"),\n"
12874             "  z);",
12875             format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style));
12876 
12877   // FIXME: Handle embedded spaces in one iteration.
12878   //  EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
12879   //            "_T(\"aaaaaaaaaaaaa\")\n"
12880   //            "_T(\"aaaaaaaaaaaaa\")\n"
12881   //            "_T(\"a\")",
12882   //            format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
12883   //                   getLLVMStyleWithColumns(20)));
12884   EXPECT_EQ(
12885       "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
12886       format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style));
12887   EXPECT_EQ("f(\n"
12888             "#if !TEST\n"
12889             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
12890             "#endif\n"
12891             ");",
12892             format("f(\n"
12893                    "#if !TEST\n"
12894                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
12895                    "#endif\n"
12896                    ");"));
12897   EXPECT_EQ("f(\n"
12898             "\n"
12899             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));",
12900             format("f(\n"
12901                    "\n"
12902                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));"));
12903   // Regression test for accessing tokens past the end of a vector in the
12904   // TokenLexer.
12905   verifyNoCrash(R"(_T(
12906 "
12907 )
12908 )");
12909 }
12910 
12911 TEST_F(FormatTest, BreaksStringLiteralOperands) {
12912   // In a function call with two operands, the second can be broken with no line
12913   // break before it.
12914   EXPECT_EQ(
12915       "func(a, \"long long \"\n"
12916       "        \"long long\");",
12917       format("func(a, \"long long long long\");", getLLVMStyleWithColumns(24)));
12918   // In a function call with three operands, the second must be broken with a
12919   // line break before it.
12920   EXPECT_EQ("func(a,\n"
12921             "     \"long long long \"\n"
12922             "     \"long\",\n"
12923             "     c);",
12924             format("func(a, \"long long long long\", c);",
12925                    getLLVMStyleWithColumns(24)));
12926   // In a function call with three operands, the third must be broken with a
12927   // line break before it.
12928   EXPECT_EQ("func(a, b,\n"
12929             "     \"long long long \"\n"
12930             "     \"long\");",
12931             format("func(a, b, \"long long long long\");",
12932                    getLLVMStyleWithColumns(24)));
12933   // In a function call with three operands, both the second and the third must
12934   // be broken with a line break before them.
12935   EXPECT_EQ("func(a,\n"
12936             "     \"long long long \"\n"
12937             "     \"long\",\n"
12938             "     \"long long long \"\n"
12939             "     \"long\");",
12940             format("func(a, \"long long long long\", \"long long long long\");",
12941                    getLLVMStyleWithColumns(24)));
12942   // In a chain of << with two operands, the second can be broken with no line
12943   // break before it.
12944   EXPECT_EQ("a << \"line line \"\n"
12945             "     \"line\";",
12946             format("a << \"line line line\";", getLLVMStyleWithColumns(20)));
12947   // In a chain of << with three operands, the second can be broken with no line
12948   // break before it.
12949   EXPECT_EQ(
12950       "abcde << \"line \"\n"
12951       "         \"line line\"\n"
12952       "      << c;",
12953       format("abcde << \"line line line\" << c;", getLLVMStyleWithColumns(20)));
12954   // In a chain of << with three operands, the third must be broken with a line
12955   // break before it.
12956   EXPECT_EQ(
12957       "a << b\n"
12958       "  << \"line line \"\n"
12959       "     \"line\";",
12960       format("a << b << \"line line line\";", getLLVMStyleWithColumns(20)));
12961   // In a chain of << with three operands, the second can be broken with no line
12962   // break before it and the third must be broken with a line break before it.
12963   EXPECT_EQ("abcd << \"line line \"\n"
12964             "        \"line\"\n"
12965             "     << \"line line \"\n"
12966             "        \"line\";",
12967             format("abcd << \"line line line\" << \"line line line\";",
12968                    getLLVMStyleWithColumns(20)));
12969   // In a chain of binary operators with two operands, the second can be broken
12970   // with no line break before it.
12971   EXPECT_EQ(
12972       "abcd + \"line line \"\n"
12973       "       \"line line\";",
12974       format("abcd + \"line line line line\";", getLLVMStyleWithColumns(20)));
12975   // In a chain of binary operators with three operands, the second must be
12976   // broken with a line break before it.
12977   EXPECT_EQ("abcd +\n"
12978             "    \"line line \"\n"
12979             "    \"line line\" +\n"
12980             "    e;",
12981             format("abcd + \"line line line line\" + e;",
12982                    getLLVMStyleWithColumns(20)));
12983   // In a function call with two operands, with AlignAfterOpenBracket enabled,
12984   // the first must be broken with a line break before it.
12985   FormatStyle Style = getLLVMStyleWithColumns(25);
12986   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
12987   EXPECT_EQ("someFunction(\n"
12988             "    \"long long long \"\n"
12989             "    \"long\",\n"
12990             "    a);",
12991             format("someFunction(\"long long long long\", a);", Style));
12992 }
12993 
12994 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
12995   EXPECT_EQ(
12996       "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
12997       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
12998       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
12999       format("aaaaaaaaaaa  =  \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
13000              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
13001              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"));
13002 }
13003 
13004 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
13005   EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);",
13006             format("f(g(R\"x(raw literal)x\",   a), b);", getGoogleStyle()));
13007   EXPECT_EQ("fffffffffff(g(R\"x(\n"
13008             "multiline raw string literal xxxxxxxxxxxxxx\n"
13009             ")x\",\n"
13010             "              a),\n"
13011             "            b);",
13012             format("fffffffffff(g(R\"x(\n"
13013                    "multiline raw string literal xxxxxxxxxxxxxx\n"
13014                    ")x\", a), b);",
13015                    getGoogleStyleWithColumns(20)));
13016   EXPECT_EQ("fffffffffff(\n"
13017             "    g(R\"x(qqq\n"
13018             "multiline raw string literal xxxxxxxxxxxxxx\n"
13019             ")x\",\n"
13020             "      a),\n"
13021             "    b);",
13022             format("fffffffffff(g(R\"x(qqq\n"
13023                    "multiline raw string literal xxxxxxxxxxxxxx\n"
13024                    ")x\", a), b);",
13025                    getGoogleStyleWithColumns(20)));
13026 
13027   EXPECT_EQ("fffffffffff(R\"x(\n"
13028             "multiline raw string literal xxxxxxxxxxxxxx\n"
13029             ")x\");",
13030             format("fffffffffff(R\"x(\n"
13031                    "multiline raw string literal xxxxxxxxxxxxxx\n"
13032                    ")x\");",
13033                    getGoogleStyleWithColumns(20)));
13034   EXPECT_EQ("fffffffffff(R\"x(\n"
13035             "multiline raw string literal xxxxxxxxxxxxxx\n"
13036             ")x\" + bbbbbb);",
13037             format("fffffffffff(R\"x(\n"
13038                    "multiline raw string literal xxxxxxxxxxxxxx\n"
13039                    ")x\" +   bbbbbb);",
13040                    getGoogleStyleWithColumns(20)));
13041   EXPECT_EQ("fffffffffff(\n"
13042             "    R\"x(\n"
13043             "multiline raw string literal xxxxxxxxxxxxxx\n"
13044             ")x\" +\n"
13045             "    bbbbbb);",
13046             format("fffffffffff(\n"
13047                    " R\"x(\n"
13048                    "multiline raw string literal xxxxxxxxxxxxxx\n"
13049                    ")x\" + bbbbbb);",
13050                    getGoogleStyleWithColumns(20)));
13051   EXPECT_EQ("fffffffffff(R\"(single line raw string)\" + bbbbbb);",
13052             format("fffffffffff(\n"
13053                    " R\"(single line raw string)\" + bbbbbb);"));
13054 }
13055 
13056 TEST_F(FormatTest, SkipsUnknownStringLiterals) {
13057   verifyFormat("string a = \"unterminated;");
13058   EXPECT_EQ("function(\"unterminated,\n"
13059             "         OtherParameter);",
13060             format("function(  \"unterminated,\n"
13061                    "    OtherParameter);"));
13062 }
13063 
13064 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
13065   FormatStyle Style = getLLVMStyle();
13066   Style.Standard = FormatStyle::LS_Cpp03;
13067   EXPECT_EQ("#define x(_a) printf(\"foo\" _a);",
13068             format("#define x(_a) printf(\"foo\"_a);", Style));
13069 }
13070 
13071 TEST_F(FormatTest, CppLexVersion) {
13072   FormatStyle Style = getLLVMStyle();
13073   // Formatting of x * y differs if x is a type.
13074   verifyFormat("void foo() { MACRO(a * b); }", Style);
13075   verifyFormat("void foo() { MACRO(int *b); }", Style);
13076 
13077   // LLVM style uses latest lexer.
13078   verifyFormat("void foo() { MACRO(char8_t *b); }", Style);
13079   Style.Standard = FormatStyle::LS_Cpp17;
13080   // But in c++17, char8_t isn't a keyword.
13081   verifyFormat("void foo() { MACRO(char8_t * b); }", Style);
13082 }
13083 
13084 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); }
13085 
13086 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
13087   EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
13088             "             \"ddeeefff\");",
13089             format("someFunction(\"aaabbbcccdddeeefff\");",
13090                    getLLVMStyleWithColumns(25)));
13091   EXPECT_EQ("someFunction1234567890(\n"
13092             "    \"aaabbbcccdddeeefff\");",
13093             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
13094                    getLLVMStyleWithColumns(26)));
13095   EXPECT_EQ("someFunction1234567890(\n"
13096             "    \"aaabbbcccdddeeeff\"\n"
13097             "    \"f\");",
13098             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
13099                    getLLVMStyleWithColumns(25)));
13100   EXPECT_EQ("someFunction1234567890(\n"
13101             "    \"aaabbbcccdddeeeff\"\n"
13102             "    \"f\");",
13103             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
13104                    getLLVMStyleWithColumns(24)));
13105   EXPECT_EQ("someFunction(\n"
13106             "    \"aaabbbcc ddde \"\n"
13107             "    \"efff\");",
13108             format("someFunction(\"aaabbbcc ddde efff\");",
13109                    getLLVMStyleWithColumns(25)));
13110   EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
13111             "             \"ddeeefff\");",
13112             format("someFunction(\"aaabbbccc ddeeefff\");",
13113                    getLLVMStyleWithColumns(25)));
13114   EXPECT_EQ("someFunction1234567890(\n"
13115             "    \"aaabb \"\n"
13116             "    \"cccdddeeefff\");",
13117             format("someFunction1234567890(\"aaabb cccdddeeefff\");",
13118                    getLLVMStyleWithColumns(25)));
13119   EXPECT_EQ("#define A          \\\n"
13120             "  string s =       \\\n"
13121             "      \"123456789\"  \\\n"
13122             "      \"0\";         \\\n"
13123             "  int i;",
13124             format("#define A string s = \"1234567890\"; int i;",
13125                    getLLVMStyleWithColumns(20)));
13126   EXPECT_EQ("someFunction(\n"
13127             "    \"aaabbbcc \"\n"
13128             "    \"dddeeefff\");",
13129             format("someFunction(\"aaabbbcc dddeeefff\");",
13130                    getLLVMStyleWithColumns(25)));
13131 }
13132 
13133 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
13134   EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3)));
13135   EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2)));
13136   EXPECT_EQ("\"test\"\n"
13137             "\"\\n\"",
13138             format("\"test\\n\"", getLLVMStyleWithColumns(7)));
13139   EXPECT_EQ("\"tes\\\\\"\n"
13140             "\"n\"",
13141             format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
13142   EXPECT_EQ("\"\\\\\\\\\"\n"
13143             "\"\\n\"",
13144             format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
13145   EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
13146   EXPECT_EQ("\"\\uff01\"\n"
13147             "\"test\"",
13148             format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
13149   EXPECT_EQ("\"\\Uff01ff02\"",
13150             format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
13151   EXPECT_EQ("\"\\x000000000001\"\n"
13152             "\"next\"",
13153             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
13154   EXPECT_EQ("\"\\x000000000001next\"",
13155             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
13156   EXPECT_EQ("\"\\x000000000001\"",
13157             format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
13158   EXPECT_EQ("\"test\"\n"
13159             "\"\\000000\"\n"
13160             "\"000001\"",
13161             format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
13162   EXPECT_EQ("\"test\\000\"\n"
13163             "\"00000000\"\n"
13164             "\"1\"",
13165             format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
13166 }
13167 
13168 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
13169   verifyFormat("void f() {\n"
13170                "  return g() {}\n"
13171                "  void h() {}");
13172   verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
13173                "g();\n"
13174                "}");
13175 }
13176 
13177 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
13178   verifyFormat(
13179       "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
13180 }
13181 
13182 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
13183   verifyFormat("class X {\n"
13184                "  void f() {\n"
13185                "  }\n"
13186                "};",
13187                getLLVMStyleWithColumns(12));
13188 }
13189 
13190 TEST_F(FormatTest, ConfigurableIndentWidth) {
13191   FormatStyle EightIndent = getLLVMStyleWithColumns(18);
13192   EightIndent.IndentWidth = 8;
13193   EightIndent.ContinuationIndentWidth = 8;
13194   verifyFormat("void f() {\n"
13195                "        someFunction();\n"
13196                "        if (true) {\n"
13197                "                f();\n"
13198                "        }\n"
13199                "}",
13200                EightIndent);
13201   verifyFormat("class X {\n"
13202                "        void f() {\n"
13203                "        }\n"
13204                "};",
13205                EightIndent);
13206   verifyFormat("int x[] = {\n"
13207                "        call(),\n"
13208                "        call()};",
13209                EightIndent);
13210 }
13211 
13212 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
13213   verifyFormat("double\n"
13214                "f();",
13215                getLLVMStyleWithColumns(8));
13216 }
13217 
13218 TEST_F(FormatTest, ConfigurableUseOfTab) {
13219   FormatStyle Tab = getLLVMStyleWithColumns(42);
13220   Tab.IndentWidth = 8;
13221   Tab.UseTab = FormatStyle::UT_Always;
13222   Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left;
13223 
13224   EXPECT_EQ("if (aaaaaaaa && // q\n"
13225             "    bb)\t\t// w\n"
13226             "\t;",
13227             format("if (aaaaaaaa &&// q\n"
13228                    "bb)// w\n"
13229                    ";",
13230                    Tab));
13231   EXPECT_EQ("if (aaa && bbb) // w\n"
13232             "\t;",
13233             format("if(aaa&&bbb)// w\n"
13234                    ";",
13235                    Tab));
13236 
13237   verifyFormat("class X {\n"
13238                "\tvoid f() {\n"
13239                "\t\tsomeFunction(parameter1,\n"
13240                "\t\t\t     parameter2);\n"
13241                "\t}\n"
13242                "};",
13243                Tab);
13244   verifyFormat("#define A                        \\\n"
13245                "\tvoid f() {               \\\n"
13246                "\t\tsomeFunction(    \\\n"
13247                "\t\t    parameter1,  \\\n"
13248                "\t\t    parameter2); \\\n"
13249                "\t}",
13250                Tab);
13251   verifyFormat("int a;\t      // x\n"
13252                "int bbbbbbbb; // x\n",
13253                Tab);
13254 
13255   Tab.TabWidth = 4;
13256   Tab.IndentWidth = 8;
13257   verifyFormat("class TabWidth4Indent8 {\n"
13258                "\t\tvoid f() {\n"
13259                "\t\t\t\tsomeFunction(parameter1,\n"
13260                "\t\t\t\t\t\t\t parameter2);\n"
13261                "\t\t}\n"
13262                "};",
13263                Tab);
13264 
13265   Tab.TabWidth = 4;
13266   Tab.IndentWidth = 4;
13267   verifyFormat("class TabWidth4Indent4 {\n"
13268                "\tvoid f() {\n"
13269                "\t\tsomeFunction(parameter1,\n"
13270                "\t\t\t\t\t parameter2);\n"
13271                "\t}\n"
13272                "};",
13273                Tab);
13274 
13275   Tab.TabWidth = 8;
13276   Tab.IndentWidth = 4;
13277   verifyFormat("class TabWidth8Indent4 {\n"
13278                "    void f() {\n"
13279                "\tsomeFunction(parameter1,\n"
13280                "\t\t     parameter2);\n"
13281                "    }\n"
13282                "};",
13283                Tab);
13284 
13285   Tab.TabWidth = 8;
13286   Tab.IndentWidth = 8;
13287   EXPECT_EQ("/*\n"
13288             "\t      a\t\tcomment\n"
13289             "\t      in multiple lines\n"
13290             "       */",
13291             format("   /*\t \t \n"
13292                    " \t \t a\t\tcomment\t \t\n"
13293                    " \t \t in multiple lines\t\n"
13294                    " \t  */",
13295                    Tab));
13296 
13297   Tab.UseTab = FormatStyle::UT_ForIndentation;
13298   verifyFormat("{\n"
13299                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13300                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13301                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13302                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13303                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13304                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13305                "};",
13306                Tab);
13307   verifyFormat("enum AA {\n"
13308                "\ta1, // Force multiple lines\n"
13309                "\ta2,\n"
13310                "\ta3\n"
13311                "};",
13312                Tab);
13313   EXPECT_EQ("if (aaaaaaaa && // q\n"
13314             "    bb)         // w\n"
13315             "\t;",
13316             format("if (aaaaaaaa &&// q\n"
13317                    "bb)// w\n"
13318                    ";",
13319                    Tab));
13320   verifyFormat("class X {\n"
13321                "\tvoid f() {\n"
13322                "\t\tsomeFunction(parameter1,\n"
13323                "\t\t             parameter2);\n"
13324                "\t}\n"
13325                "};",
13326                Tab);
13327   verifyFormat("{\n"
13328                "\tQ(\n"
13329                "\t    {\n"
13330                "\t\t    int a;\n"
13331                "\t\t    someFunction(aaaaaaaa,\n"
13332                "\t\t                 bbbbbbb);\n"
13333                "\t    },\n"
13334                "\t    p);\n"
13335                "}",
13336                Tab);
13337   EXPECT_EQ("{\n"
13338             "\t/* aaaa\n"
13339             "\t   bbbb */\n"
13340             "}",
13341             format("{\n"
13342                    "/* aaaa\n"
13343                    "   bbbb */\n"
13344                    "}",
13345                    Tab));
13346   EXPECT_EQ("{\n"
13347             "\t/*\n"
13348             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13349             "\t  bbbbbbbbbbbbb\n"
13350             "\t*/\n"
13351             "}",
13352             format("{\n"
13353                    "/*\n"
13354                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13355                    "*/\n"
13356                    "}",
13357                    Tab));
13358   EXPECT_EQ("{\n"
13359             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13360             "\t// bbbbbbbbbbbbb\n"
13361             "}",
13362             format("{\n"
13363                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13364                    "}",
13365                    Tab));
13366   EXPECT_EQ("{\n"
13367             "\t/*\n"
13368             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13369             "\t  bbbbbbbbbbbbb\n"
13370             "\t*/\n"
13371             "}",
13372             format("{\n"
13373                    "\t/*\n"
13374                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13375                    "\t*/\n"
13376                    "}",
13377                    Tab));
13378   EXPECT_EQ("{\n"
13379             "\t/*\n"
13380             "\n"
13381             "\t*/\n"
13382             "}",
13383             format("{\n"
13384                    "\t/*\n"
13385                    "\n"
13386                    "\t*/\n"
13387                    "}",
13388                    Tab));
13389   EXPECT_EQ("{\n"
13390             "\t/*\n"
13391             " asdf\n"
13392             "\t*/\n"
13393             "}",
13394             format("{\n"
13395                    "\t/*\n"
13396                    " asdf\n"
13397                    "\t*/\n"
13398                    "}",
13399                    Tab));
13400 
13401   Tab.UseTab = FormatStyle::UT_Never;
13402   EXPECT_EQ("/*\n"
13403             "              a\t\tcomment\n"
13404             "              in multiple lines\n"
13405             "       */",
13406             format("   /*\t \t \n"
13407                    " \t \t a\t\tcomment\t \t\n"
13408                    " \t \t in multiple lines\t\n"
13409                    " \t  */",
13410                    Tab));
13411   EXPECT_EQ("/* some\n"
13412             "   comment */",
13413             format(" \t \t /* some\n"
13414                    " \t \t    comment */",
13415                    Tab));
13416   EXPECT_EQ("int a; /* some\n"
13417             "   comment */",
13418             format(" \t \t int a; /* some\n"
13419                    " \t \t    comment */",
13420                    Tab));
13421 
13422   EXPECT_EQ("int a; /* some\n"
13423             "comment */",
13424             format(" \t \t int\ta; /* some\n"
13425                    " \t \t    comment */",
13426                    Tab));
13427   EXPECT_EQ("f(\"\t\t\"); /* some\n"
13428             "    comment */",
13429             format(" \t \t f(\"\t\t\"); /* some\n"
13430                    " \t \t    comment */",
13431                    Tab));
13432   EXPECT_EQ("{\n"
13433             "        /*\n"
13434             "         * Comment\n"
13435             "         */\n"
13436             "        int i;\n"
13437             "}",
13438             format("{\n"
13439                    "\t/*\n"
13440                    "\t * Comment\n"
13441                    "\t */\n"
13442                    "\t int i;\n"
13443                    "}",
13444                    Tab));
13445 
13446   Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
13447   Tab.TabWidth = 8;
13448   Tab.IndentWidth = 8;
13449   EXPECT_EQ("if (aaaaaaaa && // q\n"
13450             "    bb)         // w\n"
13451             "\t;",
13452             format("if (aaaaaaaa &&// q\n"
13453                    "bb)// w\n"
13454                    ";",
13455                    Tab));
13456   EXPECT_EQ("if (aaa && bbb) // w\n"
13457             "\t;",
13458             format("if(aaa&&bbb)// w\n"
13459                    ";",
13460                    Tab));
13461   verifyFormat("class X {\n"
13462                "\tvoid f() {\n"
13463                "\t\tsomeFunction(parameter1,\n"
13464                "\t\t\t     parameter2);\n"
13465                "\t}\n"
13466                "};",
13467                Tab);
13468   verifyFormat("#define A                        \\\n"
13469                "\tvoid f() {               \\\n"
13470                "\t\tsomeFunction(    \\\n"
13471                "\t\t    parameter1,  \\\n"
13472                "\t\t    parameter2); \\\n"
13473                "\t}",
13474                Tab);
13475   Tab.TabWidth = 4;
13476   Tab.IndentWidth = 8;
13477   verifyFormat("class TabWidth4Indent8 {\n"
13478                "\t\tvoid f() {\n"
13479                "\t\t\t\tsomeFunction(parameter1,\n"
13480                "\t\t\t\t\t\t\t parameter2);\n"
13481                "\t\t}\n"
13482                "};",
13483                Tab);
13484   Tab.TabWidth = 4;
13485   Tab.IndentWidth = 4;
13486   verifyFormat("class TabWidth4Indent4 {\n"
13487                "\tvoid f() {\n"
13488                "\t\tsomeFunction(parameter1,\n"
13489                "\t\t\t\t\t parameter2);\n"
13490                "\t}\n"
13491                "};",
13492                Tab);
13493   Tab.TabWidth = 8;
13494   Tab.IndentWidth = 4;
13495   verifyFormat("class TabWidth8Indent4 {\n"
13496                "    void f() {\n"
13497                "\tsomeFunction(parameter1,\n"
13498                "\t\t     parameter2);\n"
13499                "    }\n"
13500                "};",
13501                Tab);
13502   Tab.TabWidth = 8;
13503   Tab.IndentWidth = 8;
13504   EXPECT_EQ("/*\n"
13505             "\t      a\t\tcomment\n"
13506             "\t      in multiple lines\n"
13507             "       */",
13508             format("   /*\t \t \n"
13509                    " \t \t a\t\tcomment\t \t\n"
13510                    " \t \t in multiple lines\t\n"
13511                    " \t  */",
13512                    Tab));
13513   verifyFormat("{\n"
13514                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13515                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13516                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13517                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13518                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13519                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13520                "};",
13521                Tab);
13522   verifyFormat("enum AA {\n"
13523                "\ta1, // Force multiple lines\n"
13524                "\ta2,\n"
13525                "\ta3\n"
13526                "};",
13527                Tab);
13528   EXPECT_EQ("if (aaaaaaaa && // q\n"
13529             "    bb)         // w\n"
13530             "\t;",
13531             format("if (aaaaaaaa &&// q\n"
13532                    "bb)// w\n"
13533                    ";",
13534                    Tab));
13535   verifyFormat("class X {\n"
13536                "\tvoid f() {\n"
13537                "\t\tsomeFunction(parameter1,\n"
13538                "\t\t\t     parameter2);\n"
13539                "\t}\n"
13540                "};",
13541                Tab);
13542   verifyFormat("{\n"
13543                "\tQ(\n"
13544                "\t    {\n"
13545                "\t\t    int a;\n"
13546                "\t\t    someFunction(aaaaaaaa,\n"
13547                "\t\t\t\t bbbbbbb);\n"
13548                "\t    },\n"
13549                "\t    p);\n"
13550                "}",
13551                Tab);
13552   EXPECT_EQ("{\n"
13553             "\t/* aaaa\n"
13554             "\t   bbbb */\n"
13555             "}",
13556             format("{\n"
13557                    "/* aaaa\n"
13558                    "   bbbb */\n"
13559                    "}",
13560                    Tab));
13561   EXPECT_EQ("{\n"
13562             "\t/*\n"
13563             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13564             "\t  bbbbbbbbbbbbb\n"
13565             "\t*/\n"
13566             "}",
13567             format("{\n"
13568                    "/*\n"
13569                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13570                    "*/\n"
13571                    "}",
13572                    Tab));
13573   EXPECT_EQ("{\n"
13574             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13575             "\t// bbbbbbbbbbbbb\n"
13576             "}",
13577             format("{\n"
13578                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13579                    "}",
13580                    Tab));
13581   EXPECT_EQ("{\n"
13582             "\t/*\n"
13583             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13584             "\t  bbbbbbbbbbbbb\n"
13585             "\t*/\n"
13586             "}",
13587             format("{\n"
13588                    "\t/*\n"
13589                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13590                    "\t*/\n"
13591                    "}",
13592                    Tab));
13593   EXPECT_EQ("{\n"
13594             "\t/*\n"
13595             "\n"
13596             "\t*/\n"
13597             "}",
13598             format("{\n"
13599                    "\t/*\n"
13600                    "\n"
13601                    "\t*/\n"
13602                    "}",
13603                    Tab));
13604   EXPECT_EQ("{\n"
13605             "\t/*\n"
13606             " asdf\n"
13607             "\t*/\n"
13608             "}",
13609             format("{\n"
13610                    "\t/*\n"
13611                    " asdf\n"
13612                    "\t*/\n"
13613                    "}",
13614                    Tab));
13615   EXPECT_EQ("/* some\n"
13616             "   comment */",
13617             format(" \t \t /* some\n"
13618                    " \t \t    comment */",
13619                    Tab));
13620   EXPECT_EQ("int a; /* some\n"
13621             "   comment */",
13622             format(" \t \t int a; /* some\n"
13623                    " \t \t    comment */",
13624                    Tab));
13625   EXPECT_EQ("int a; /* some\n"
13626             "comment */",
13627             format(" \t \t int\ta; /* some\n"
13628                    " \t \t    comment */",
13629                    Tab));
13630   EXPECT_EQ("f(\"\t\t\"); /* some\n"
13631             "    comment */",
13632             format(" \t \t f(\"\t\t\"); /* some\n"
13633                    " \t \t    comment */",
13634                    Tab));
13635   EXPECT_EQ("{\n"
13636             "\t/*\n"
13637             "\t * Comment\n"
13638             "\t */\n"
13639             "\tint i;\n"
13640             "}",
13641             format("{\n"
13642                    "\t/*\n"
13643                    "\t * Comment\n"
13644                    "\t */\n"
13645                    "\t int i;\n"
13646                    "}",
13647                    Tab));
13648   Tab.TabWidth = 2;
13649   Tab.IndentWidth = 2;
13650   EXPECT_EQ("{\n"
13651             "\t/* aaaa\n"
13652             "\t\t bbbb */\n"
13653             "}",
13654             format("{\n"
13655                    "/* aaaa\n"
13656                    "\t bbbb */\n"
13657                    "}",
13658                    Tab));
13659   EXPECT_EQ("{\n"
13660             "\t/*\n"
13661             "\t\taaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13662             "\t\tbbbbbbbbbbbbb\n"
13663             "\t*/\n"
13664             "}",
13665             format("{\n"
13666                    "/*\n"
13667                    "\taaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13668                    "*/\n"
13669                    "}",
13670                    Tab));
13671   Tab.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
13672   Tab.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
13673   Tab.TabWidth = 4;
13674   Tab.IndentWidth = 4;
13675   verifyFormat("class Assign {\n"
13676                "\tvoid f() {\n"
13677                "\t\tint         x      = 123;\n"
13678                "\t\tint         random = 4;\n"
13679                "\t\tstd::string alphabet =\n"
13680                "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
13681                "\t}\n"
13682                "};",
13683                Tab);
13684 
13685   Tab.UseTab = FormatStyle::UT_AlignWithSpaces;
13686   Tab.TabWidth = 8;
13687   Tab.IndentWidth = 8;
13688   EXPECT_EQ("if (aaaaaaaa && // q\n"
13689             "    bb)         // w\n"
13690             "\t;",
13691             format("if (aaaaaaaa &&// q\n"
13692                    "bb)// w\n"
13693                    ";",
13694                    Tab));
13695   EXPECT_EQ("if (aaa && bbb) // w\n"
13696             "\t;",
13697             format("if(aaa&&bbb)// w\n"
13698                    ";",
13699                    Tab));
13700   verifyFormat("class X {\n"
13701                "\tvoid f() {\n"
13702                "\t\tsomeFunction(parameter1,\n"
13703                "\t\t             parameter2);\n"
13704                "\t}\n"
13705                "};",
13706                Tab);
13707   verifyFormat("#define A                        \\\n"
13708                "\tvoid f() {               \\\n"
13709                "\t\tsomeFunction(    \\\n"
13710                "\t\t    parameter1,  \\\n"
13711                "\t\t    parameter2); \\\n"
13712                "\t}",
13713                Tab);
13714   Tab.TabWidth = 4;
13715   Tab.IndentWidth = 8;
13716   verifyFormat("class TabWidth4Indent8 {\n"
13717                "\t\tvoid f() {\n"
13718                "\t\t\t\tsomeFunction(parameter1,\n"
13719                "\t\t\t\t             parameter2);\n"
13720                "\t\t}\n"
13721                "};",
13722                Tab);
13723   Tab.TabWidth = 4;
13724   Tab.IndentWidth = 4;
13725   verifyFormat("class TabWidth4Indent4 {\n"
13726                "\tvoid f() {\n"
13727                "\t\tsomeFunction(parameter1,\n"
13728                "\t\t             parameter2);\n"
13729                "\t}\n"
13730                "};",
13731                Tab);
13732   Tab.TabWidth = 8;
13733   Tab.IndentWidth = 4;
13734   verifyFormat("class TabWidth8Indent4 {\n"
13735                "    void f() {\n"
13736                "\tsomeFunction(parameter1,\n"
13737                "\t             parameter2);\n"
13738                "    }\n"
13739                "};",
13740                Tab);
13741   Tab.TabWidth = 8;
13742   Tab.IndentWidth = 8;
13743   EXPECT_EQ("/*\n"
13744             "              a\t\tcomment\n"
13745             "              in multiple lines\n"
13746             "       */",
13747             format("   /*\t \t \n"
13748                    " \t \t a\t\tcomment\t \t\n"
13749                    " \t \t in multiple lines\t\n"
13750                    " \t  */",
13751                    Tab));
13752   verifyFormat("{\n"
13753                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13754                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13755                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13756                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13757                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13758                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13759                "};",
13760                Tab);
13761   verifyFormat("enum AA {\n"
13762                "\ta1, // Force multiple lines\n"
13763                "\ta2,\n"
13764                "\ta3\n"
13765                "};",
13766                Tab);
13767   EXPECT_EQ("if (aaaaaaaa && // q\n"
13768             "    bb)         // w\n"
13769             "\t;",
13770             format("if (aaaaaaaa &&// q\n"
13771                    "bb)// w\n"
13772                    ";",
13773                    Tab));
13774   verifyFormat("class X {\n"
13775                "\tvoid f() {\n"
13776                "\t\tsomeFunction(parameter1,\n"
13777                "\t\t             parameter2);\n"
13778                "\t}\n"
13779                "};",
13780                Tab);
13781   verifyFormat("{\n"
13782                "\tQ(\n"
13783                "\t    {\n"
13784                "\t\t    int a;\n"
13785                "\t\t    someFunction(aaaaaaaa,\n"
13786                "\t\t                 bbbbbbb);\n"
13787                "\t    },\n"
13788                "\t    p);\n"
13789                "}",
13790                Tab);
13791   EXPECT_EQ("{\n"
13792             "\t/* aaaa\n"
13793             "\t   bbbb */\n"
13794             "}",
13795             format("{\n"
13796                    "/* aaaa\n"
13797                    "   bbbb */\n"
13798                    "}",
13799                    Tab));
13800   EXPECT_EQ("{\n"
13801             "\t/*\n"
13802             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13803             "\t  bbbbbbbbbbbbb\n"
13804             "\t*/\n"
13805             "}",
13806             format("{\n"
13807                    "/*\n"
13808                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13809                    "*/\n"
13810                    "}",
13811                    Tab));
13812   EXPECT_EQ("{\n"
13813             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13814             "\t// bbbbbbbbbbbbb\n"
13815             "}",
13816             format("{\n"
13817                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13818                    "}",
13819                    Tab));
13820   EXPECT_EQ("{\n"
13821             "\t/*\n"
13822             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13823             "\t  bbbbbbbbbbbbb\n"
13824             "\t*/\n"
13825             "}",
13826             format("{\n"
13827                    "\t/*\n"
13828                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13829                    "\t*/\n"
13830                    "}",
13831                    Tab));
13832   EXPECT_EQ("{\n"
13833             "\t/*\n"
13834             "\n"
13835             "\t*/\n"
13836             "}",
13837             format("{\n"
13838                    "\t/*\n"
13839                    "\n"
13840                    "\t*/\n"
13841                    "}",
13842                    Tab));
13843   EXPECT_EQ("{\n"
13844             "\t/*\n"
13845             " asdf\n"
13846             "\t*/\n"
13847             "}",
13848             format("{\n"
13849                    "\t/*\n"
13850                    " asdf\n"
13851                    "\t*/\n"
13852                    "}",
13853                    Tab));
13854   EXPECT_EQ("/* some\n"
13855             "   comment */",
13856             format(" \t \t /* some\n"
13857                    " \t \t    comment */",
13858                    Tab));
13859   EXPECT_EQ("int a; /* some\n"
13860             "   comment */",
13861             format(" \t \t int a; /* some\n"
13862                    " \t \t    comment */",
13863                    Tab));
13864   EXPECT_EQ("int a; /* some\n"
13865             "comment */",
13866             format(" \t \t int\ta; /* some\n"
13867                    " \t \t    comment */",
13868                    Tab));
13869   EXPECT_EQ("f(\"\t\t\"); /* some\n"
13870             "    comment */",
13871             format(" \t \t f(\"\t\t\"); /* some\n"
13872                    " \t \t    comment */",
13873                    Tab));
13874   EXPECT_EQ("{\n"
13875             "\t/*\n"
13876             "\t * Comment\n"
13877             "\t */\n"
13878             "\tint i;\n"
13879             "}",
13880             format("{\n"
13881                    "\t/*\n"
13882                    "\t * Comment\n"
13883                    "\t */\n"
13884                    "\t int i;\n"
13885                    "}",
13886                    Tab));
13887   Tab.TabWidth = 2;
13888   Tab.IndentWidth = 2;
13889   EXPECT_EQ("{\n"
13890             "\t/* aaaa\n"
13891             "\t   bbbb */\n"
13892             "}",
13893             format("{\n"
13894                    "/* aaaa\n"
13895                    "   bbbb */\n"
13896                    "}",
13897                    Tab));
13898   EXPECT_EQ("{\n"
13899             "\t/*\n"
13900             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13901             "\t  bbbbbbbbbbbbb\n"
13902             "\t*/\n"
13903             "}",
13904             format("{\n"
13905                    "/*\n"
13906                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13907                    "*/\n"
13908                    "}",
13909                    Tab));
13910   Tab.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
13911   Tab.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
13912   Tab.TabWidth = 4;
13913   Tab.IndentWidth = 4;
13914   verifyFormat("class Assign {\n"
13915                "\tvoid f() {\n"
13916                "\t\tint         x      = 123;\n"
13917                "\t\tint         random = 4;\n"
13918                "\t\tstd::string alphabet =\n"
13919                "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
13920                "\t}\n"
13921                "};",
13922                Tab);
13923   Tab.AlignOperands = FormatStyle::OAS_Align;
13924   verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb +\n"
13925                "                 cccccccccccccccccccc;",
13926                Tab);
13927   // no alignment
13928   verifyFormat("int aaaaaaaaaa =\n"
13929                "\tbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
13930                Tab);
13931   verifyFormat("return aaaaaaaaaaaaaaaa ? 111111111111111\n"
13932                "       : bbbbbbbbbbbbbb ? 222222222222222\n"
13933                "                        : 333333333333333;",
13934                Tab);
13935   Tab.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
13936   Tab.AlignOperands = FormatStyle::OAS_AlignAfterOperator;
13937   verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb\n"
13938                "               + cccccccccccccccccccc;",
13939                Tab);
13940 }
13941 
13942 TEST_F(FormatTest, ZeroTabWidth) {
13943   FormatStyle Tab = getLLVMStyleWithColumns(42);
13944   Tab.IndentWidth = 8;
13945   Tab.UseTab = FormatStyle::UT_Never;
13946   Tab.TabWidth = 0;
13947   EXPECT_EQ("void a(){\n"
13948             "    // line starts with '\t'\n"
13949             "};",
13950             format("void a(){\n"
13951                    "\t// line starts with '\t'\n"
13952                    "};",
13953                    Tab));
13954 
13955   EXPECT_EQ("void a(){\n"
13956             "    // line starts with '\t'\n"
13957             "};",
13958             format("void a(){\n"
13959                    "\t\t// line starts with '\t'\n"
13960                    "};",
13961                    Tab));
13962 
13963   Tab.UseTab = FormatStyle::UT_ForIndentation;
13964   EXPECT_EQ("void a(){\n"
13965             "    // line starts with '\t'\n"
13966             "};",
13967             format("void a(){\n"
13968                    "\t// line starts with '\t'\n"
13969                    "};",
13970                    Tab));
13971 
13972   EXPECT_EQ("void a(){\n"
13973             "    // line starts with '\t'\n"
13974             "};",
13975             format("void a(){\n"
13976                    "\t\t// line starts with '\t'\n"
13977                    "};",
13978                    Tab));
13979 
13980   Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
13981   EXPECT_EQ("void a(){\n"
13982             "    // line starts with '\t'\n"
13983             "};",
13984             format("void a(){\n"
13985                    "\t// line starts with '\t'\n"
13986                    "};",
13987                    Tab));
13988 
13989   EXPECT_EQ("void a(){\n"
13990             "    // line starts with '\t'\n"
13991             "};",
13992             format("void a(){\n"
13993                    "\t\t// line starts with '\t'\n"
13994                    "};",
13995                    Tab));
13996 
13997   Tab.UseTab = FormatStyle::UT_AlignWithSpaces;
13998   EXPECT_EQ("void a(){\n"
13999             "    // line starts with '\t'\n"
14000             "};",
14001             format("void a(){\n"
14002                    "\t// line starts with '\t'\n"
14003                    "};",
14004                    Tab));
14005 
14006   EXPECT_EQ("void a(){\n"
14007             "    // line starts with '\t'\n"
14008             "};",
14009             format("void a(){\n"
14010                    "\t\t// line starts with '\t'\n"
14011                    "};",
14012                    Tab));
14013 
14014   Tab.UseTab = FormatStyle::UT_Always;
14015   EXPECT_EQ("void a(){\n"
14016             "// line starts with '\t'\n"
14017             "};",
14018             format("void a(){\n"
14019                    "\t// line starts with '\t'\n"
14020                    "};",
14021                    Tab));
14022 
14023   EXPECT_EQ("void a(){\n"
14024             "// line starts with '\t'\n"
14025             "};",
14026             format("void a(){\n"
14027                    "\t\t// line starts with '\t'\n"
14028                    "};",
14029                    Tab));
14030 }
14031 
14032 TEST_F(FormatTest, CalculatesOriginalColumn) {
14033   EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
14034             "q\"; /* some\n"
14035             "       comment */",
14036             format("  \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
14037                    "q\"; /* some\n"
14038                    "       comment */",
14039                    getLLVMStyle()));
14040   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
14041             "/* some\n"
14042             "   comment */",
14043             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
14044                    " /* some\n"
14045                    "    comment */",
14046                    getLLVMStyle()));
14047   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
14048             "qqq\n"
14049             "/* some\n"
14050             "   comment */",
14051             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
14052                    "qqq\n"
14053                    " /* some\n"
14054                    "    comment */",
14055                    getLLVMStyle()));
14056   EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
14057             "wwww; /* some\n"
14058             "         comment */",
14059             format("  inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
14060                    "wwww; /* some\n"
14061                    "         comment */",
14062                    getLLVMStyle()));
14063 }
14064 
14065 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
14066   FormatStyle NoSpace = getLLVMStyle();
14067   NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
14068 
14069   verifyFormat("while(true)\n"
14070                "  continue;",
14071                NoSpace);
14072   verifyFormat("for(;;)\n"
14073                "  continue;",
14074                NoSpace);
14075   verifyFormat("if(true)\n"
14076                "  f();\n"
14077                "else if(true)\n"
14078                "  f();",
14079                NoSpace);
14080   verifyFormat("do {\n"
14081                "  do_something();\n"
14082                "} while(something());",
14083                NoSpace);
14084   verifyFormat("switch(x) {\n"
14085                "default:\n"
14086                "  break;\n"
14087                "}",
14088                NoSpace);
14089   verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
14090   verifyFormat("size_t x = sizeof(x);", NoSpace);
14091   verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
14092   verifyFormat("auto f(int x) -> typeof(x);", NoSpace);
14093   verifyFormat("auto f(int x) -> _Atomic(x);", NoSpace);
14094   verifyFormat("auto f(int x) -> __underlying_type(x);", NoSpace);
14095   verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
14096   verifyFormat("alignas(128) char a[128];", NoSpace);
14097   verifyFormat("size_t x = alignof(MyType);", NoSpace);
14098   verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
14099   verifyFormat("int f() throw(Deprecated);", NoSpace);
14100   verifyFormat("typedef void (*cb)(int);", NoSpace);
14101   verifyFormat("T A::operator()();", NoSpace);
14102   verifyFormat("X A::operator++(T);", NoSpace);
14103   verifyFormat("auto lambda = []() { return 0; };", NoSpace);
14104 
14105   FormatStyle Space = getLLVMStyle();
14106   Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
14107 
14108   verifyFormat("int f ();", Space);
14109   verifyFormat("void f (int a, T b) {\n"
14110                "  while (true)\n"
14111                "    continue;\n"
14112                "}",
14113                Space);
14114   verifyFormat("if (true)\n"
14115                "  f ();\n"
14116                "else if (true)\n"
14117                "  f ();",
14118                Space);
14119   verifyFormat("do {\n"
14120                "  do_something ();\n"
14121                "} while (something ());",
14122                Space);
14123   verifyFormat("switch (x) {\n"
14124                "default:\n"
14125                "  break;\n"
14126                "}",
14127                Space);
14128   verifyFormat("A::A () : a (1) {}", Space);
14129   verifyFormat("void f () __attribute__ ((asdf));", Space);
14130   verifyFormat("*(&a + 1);\n"
14131                "&((&a)[1]);\n"
14132                "a[(b + c) * d];\n"
14133                "(((a + 1) * 2) + 3) * 4;",
14134                Space);
14135   verifyFormat("#define A(x) x", Space);
14136   verifyFormat("#define A (x) x", Space);
14137   verifyFormat("#if defined(x)\n"
14138                "#endif",
14139                Space);
14140   verifyFormat("auto i = std::make_unique<int> (5);", Space);
14141   verifyFormat("size_t x = sizeof (x);", Space);
14142   verifyFormat("auto f (int x) -> decltype (x);", Space);
14143   verifyFormat("auto f (int x) -> typeof (x);", Space);
14144   verifyFormat("auto f (int x) -> _Atomic (x);", Space);
14145   verifyFormat("auto f (int x) -> __underlying_type (x);", Space);
14146   verifyFormat("int f (T x) noexcept (x.create ());", Space);
14147   verifyFormat("alignas (128) char a[128];", Space);
14148   verifyFormat("size_t x = alignof (MyType);", Space);
14149   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
14150   verifyFormat("int f () throw (Deprecated);", Space);
14151   verifyFormat("typedef void (*cb) (int);", Space);
14152   // FIXME these tests regressed behaviour.
14153   // verifyFormat("T A::operator() ();", Space);
14154   // verifyFormat("X A::operator++ (T);", Space);
14155   verifyFormat("auto lambda = [] () { return 0; };", Space);
14156   verifyFormat("int x = int (y);", Space);
14157 
14158   FormatStyle SomeSpace = getLLVMStyle();
14159   SomeSpace.SpaceBeforeParens = FormatStyle::SBPO_NonEmptyParentheses;
14160 
14161   verifyFormat("[]() -> float {}", SomeSpace);
14162   verifyFormat("[] (auto foo) {}", SomeSpace);
14163   verifyFormat("[foo]() -> int {}", SomeSpace);
14164   verifyFormat("int f();", SomeSpace);
14165   verifyFormat("void f (int a, T b) {\n"
14166                "  while (true)\n"
14167                "    continue;\n"
14168                "}",
14169                SomeSpace);
14170   verifyFormat("if (true)\n"
14171                "  f();\n"
14172                "else if (true)\n"
14173                "  f();",
14174                SomeSpace);
14175   verifyFormat("do {\n"
14176                "  do_something();\n"
14177                "} while (something());",
14178                SomeSpace);
14179   verifyFormat("switch (x) {\n"
14180                "default:\n"
14181                "  break;\n"
14182                "}",
14183                SomeSpace);
14184   verifyFormat("A::A() : a (1) {}", SomeSpace);
14185   verifyFormat("void f() __attribute__ ((asdf));", SomeSpace);
14186   verifyFormat("*(&a + 1);\n"
14187                "&((&a)[1]);\n"
14188                "a[(b + c) * d];\n"
14189                "(((a + 1) * 2) + 3) * 4;",
14190                SomeSpace);
14191   verifyFormat("#define A(x) x", SomeSpace);
14192   verifyFormat("#define A (x) x", SomeSpace);
14193   verifyFormat("#if defined(x)\n"
14194                "#endif",
14195                SomeSpace);
14196   verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace);
14197   verifyFormat("size_t x = sizeof (x);", SomeSpace);
14198   verifyFormat("auto f (int x) -> decltype (x);", SomeSpace);
14199   verifyFormat("auto f (int x) -> typeof (x);", SomeSpace);
14200   verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace);
14201   verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace);
14202   verifyFormat("int f (T x) noexcept (x.create());", SomeSpace);
14203   verifyFormat("alignas (128) char a[128];", SomeSpace);
14204   verifyFormat("size_t x = alignof (MyType);", SomeSpace);
14205   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");",
14206                SomeSpace);
14207   verifyFormat("int f() throw (Deprecated);", SomeSpace);
14208   verifyFormat("typedef void (*cb) (int);", SomeSpace);
14209   verifyFormat("T A::operator()();", SomeSpace);
14210   // FIXME these tests regressed behaviour.
14211   // verifyFormat("X A::operator++ (T);", SomeSpace);
14212   verifyFormat("int x = int (y);", SomeSpace);
14213   verifyFormat("auto lambda = []() { return 0; };", SomeSpace);
14214 
14215   FormatStyle SpaceControlStatements = getLLVMStyle();
14216   SpaceControlStatements.SpaceBeforeParens = FormatStyle::SBPO_Custom;
14217   SpaceControlStatements.SpaceBeforeParensOptions.AfterControlStatements = true;
14218 
14219   verifyFormat("while (true)\n"
14220                "  continue;",
14221                SpaceControlStatements);
14222   verifyFormat("if (true)\n"
14223                "  f();\n"
14224                "else if (true)\n"
14225                "  f();",
14226                SpaceControlStatements);
14227   verifyFormat("for (;;) {\n"
14228                "  do_something();\n"
14229                "}",
14230                SpaceControlStatements);
14231   verifyFormat("do {\n"
14232                "  do_something();\n"
14233                "} while (something());",
14234                SpaceControlStatements);
14235   verifyFormat("switch (x) {\n"
14236                "default:\n"
14237                "  break;\n"
14238                "}",
14239                SpaceControlStatements);
14240 
14241   FormatStyle SpaceFuncDecl = getLLVMStyle();
14242   SpaceFuncDecl.SpaceBeforeParens = FormatStyle::SBPO_Custom;
14243   SpaceFuncDecl.SpaceBeforeParensOptions.AfterFunctionDeclarationName = true;
14244 
14245   verifyFormat("int f ();", SpaceFuncDecl);
14246   verifyFormat("void f(int a, T b) {}", SpaceFuncDecl);
14247   verifyFormat("A::A() : a(1) {}", SpaceFuncDecl);
14248   verifyFormat("void f () __attribute__((asdf));", SpaceFuncDecl);
14249   verifyFormat("#define A(x) x", SpaceFuncDecl);
14250   verifyFormat("#define A (x) x", SpaceFuncDecl);
14251   verifyFormat("#if defined(x)\n"
14252                "#endif",
14253                SpaceFuncDecl);
14254   verifyFormat("auto i = std::make_unique<int>(5);", SpaceFuncDecl);
14255   verifyFormat("size_t x = sizeof(x);", SpaceFuncDecl);
14256   verifyFormat("auto f (int x) -> decltype(x);", SpaceFuncDecl);
14257   verifyFormat("auto f (int x) -> typeof(x);", SpaceFuncDecl);
14258   verifyFormat("auto f (int x) -> _Atomic(x);", SpaceFuncDecl);
14259   verifyFormat("auto f (int x) -> __underlying_type(x);", SpaceFuncDecl);
14260   verifyFormat("int f (T x) noexcept(x.create());", SpaceFuncDecl);
14261   verifyFormat("alignas(128) char a[128];", SpaceFuncDecl);
14262   verifyFormat("size_t x = alignof(MyType);", SpaceFuncDecl);
14263   verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");",
14264                SpaceFuncDecl);
14265   verifyFormat("int f () throw(Deprecated);", SpaceFuncDecl);
14266   verifyFormat("typedef void (*cb)(int);", SpaceFuncDecl);
14267   // FIXME these tests regressed behaviour.
14268   // verifyFormat("T A::operator() ();", SpaceFuncDecl);
14269   // verifyFormat("X A::operator++ (T);", SpaceFuncDecl);
14270   verifyFormat("T A::operator()() {}", SpaceFuncDecl);
14271   verifyFormat("auto lambda = []() { return 0; };", SpaceFuncDecl);
14272   verifyFormat("int x = int(y);", SpaceFuncDecl);
14273   verifyFormat("M(std::size_t R, std::size_t C) : C(C), data(R) {}",
14274                SpaceFuncDecl);
14275 
14276   FormatStyle SpaceFuncDef = getLLVMStyle();
14277   SpaceFuncDef.SpaceBeforeParens = FormatStyle::SBPO_Custom;
14278   SpaceFuncDef.SpaceBeforeParensOptions.AfterFunctionDefinitionName = true;
14279 
14280   verifyFormat("int f();", SpaceFuncDef);
14281   verifyFormat("void f (int a, T b) {}", SpaceFuncDef);
14282   verifyFormat("A::A() : a(1) {}", SpaceFuncDef);
14283   verifyFormat("void f() __attribute__((asdf));", SpaceFuncDef);
14284   verifyFormat("#define A(x) x", SpaceFuncDef);
14285   verifyFormat("#define A (x) x", SpaceFuncDef);
14286   verifyFormat("#if defined(x)\n"
14287                "#endif",
14288                SpaceFuncDef);
14289   verifyFormat("auto i = std::make_unique<int>(5);", SpaceFuncDef);
14290   verifyFormat("size_t x = sizeof(x);", SpaceFuncDef);
14291   verifyFormat("auto f(int x) -> decltype(x);", SpaceFuncDef);
14292   verifyFormat("auto f(int x) -> typeof(x);", SpaceFuncDef);
14293   verifyFormat("auto f(int x) -> _Atomic(x);", SpaceFuncDef);
14294   verifyFormat("auto f(int x) -> __underlying_type(x);", SpaceFuncDef);
14295   verifyFormat("int f(T x) noexcept(x.create());", SpaceFuncDef);
14296   verifyFormat("alignas(128) char a[128];", SpaceFuncDef);
14297   verifyFormat("size_t x = alignof(MyType);", SpaceFuncDef);
14298   verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");",
14299                SpaceFuncDef);
14300   verifyFormat("int f() throw(Deprecated);", SpaceFuncDef);
14301   verifyFormat("typedef void (*cb)(int);", SpaceFuncDef);
14302   verifyFormat("T A::operator()();", SpaceFuncDef);
14303   verifyFormat("X A::operator++(T);", SpaceFuncDef);
14304   // verifyFormat("T A::operator() () {}", SpaceFuncDef);
14305   verifyFormat("auto lambda = [] () { return 0; };", SpaceFuncDef);
14306   verifyFormat("int x = int(y);", SpaceFuncDef);
14307   verifyFormat("M(std::size_t R, std::size_t C) : C(C), data(R) {}",
14308                SpaceFuncDef);
14309 
14310   FormatStyle SpaceIfMacros = getLLVMStyle();
14311   SpaceIfMacros.IfMacros.clear();
14312   SpaceIfMacros.IfMacros.push_back("MYIF");
14313   SpaceIfMacros.SpaceBeforeParens = FormatStyle::SBPO_Custom;
14314   SpaceIfMacros.SpaceBeforeParensOptions.AfterIfMacros = true;
14315   verifyFormat("MYIF (a)\n  return;", SpaceIfMacros);
14316   verifyFormat("MYIF (a)\n  return;\nelse MYIF (b)\n  return;", SpaceIfMacros);
14317   verifyFormat("MYIF (a)\n  return;\nelse\n  return;", SpaceIfMacros);
14318 
14319   FormatStyle SpaceForeachMacros = getLLVMStyle();
14320   SpaceForeachMacros.SpaceBeforeParens = FormatStyle::SBPO_Custom;
14321   SpaceForeachMacros.SpaceBeforeParensOptions.AfterForeachMacros = true;
14322   verifyFormat("foreach (Item *item, itemlist) {}", SpaceForeachMacros);
14323   verifyFormat("Q_FOREACH (Item *item, itemlist) {}", SpaceForeachMacros);
14324   verifyFormat("BOOST_FOREACH (Item *item, itemlist) {}", SpaceForeachMacros);
14325   verifyFormat("UNKNOWN_FOREACH(Item *item, itemlist) {}", SpaceForeachMacros);
14326 
14327   FormatStyle SomeSpace2 = getLLVMStyle();
14328   SomeSpace2.SpaceBeforeParens = FormatStyle::SBPO_Custom;
14329   SomeSpace2.SpaceBeforeParensOptions.BeforeNonEmptyParentheses = true;
14330   verifyFormat("[]() -> float {}", SomeSpace2);
14331   verifyFormat("[] (auto foo) {}", SomeSpace2);
14332   verifyFormat("[foo]() -> int {}", SomeSpace2);
14333   verifyFormat("int f();", SomeSpace2);
14334   verifyFormat("void f (int a, T b) {\n"
14335                "  while (true)\n"
14336                "    continue;\n"
14337                "}",
14338                SomeSpace2);
14339   verifyFormat("if (true)\n"
14340                "  f();\n"
14341                "else if (true)\n"
14342                "  f();",
14343                SomeSpace2);
14344   verifyFormat("do {\n"
14345                "  do_something();\n"
14346                "} while (something());",
14347                SomeSpace2);
14348   verifyFormat("switch (x) {\n"
14349                "default:\n"
14350                "  break;\n"
14351                "}",
14352                SomeSpace2);
14353   verifyFormat("A::A() : a (1) {}", SomeSpace2);
14354   verifyFormat("void f() __attribute__ ((asdf));", SomeSpace2);
14355   verifyFormat("*(&a + 1);\n"
14356                "&((&a)[1]);\n"
14357                "a[(b + c) * d];\n"
14358                "(((a + 1) * 2) + 3) * 4;",
14359                SomeSpace2);
14360   verifyFormat("#define A(x) x", SomeSpace2);
14361   verifyFormat("#define A (x) x", SomeSpace2);
14362   verifyFormat("#if defined(x)\n"
14363                "#endif",
14364                SomeSpace2);
14365   verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace2);
14366   verifyFormat("size_t x = sizeof (x);", SomeSpace2);
14367   verifyFormat("auto f (int x) -> decltype (x);", SomeSpace2);
14368   verifyFormat("auto f (int x) -> typeof (x);", SomeSpace2);
14369   verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace2);
14370   verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace2);
14371   verifyFormat("int f (T x) noexcept (x.create());", SomeSpace2);
14372   verifyFormat("alignas (128) char a[128];", SomeSpace2);
14373   verifyFormat("size_t x = alignof (MyType);", SomeSpace2);
14374   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");",
14375                SomeSpace2);
14376   verifyFormat("int f() throw (Deprecated);", SomeSpace2);
14377   verifyFormat("typedef void (*cb) (int);", SomeSpace2);
14378   verifyFormat("T A::operator()();", SomeSpace2);
14379   // verifyFormat("X A::operator++ (T);", SomeSpace2);
14380   verifyFormat("int x = int (y);", SomeSpace2);
14381   verifyFormat("auto lambda = []() { return 0; };", SomeSpace2);
14382 }
14383 
14384 TEST_F(FormatTest, SpaceAfterLogicalNot) {
14385   FormatStyle Spaces = getLLVMStyle();
14386   Spaces.SpaceAfterLogicalNot = true;
14387 
14388   verifyFormat("bool x = ! y", Spaces);
14389   verifyFormat("if (! isFailure())", Spaces);
14390   verifyFormat("if (! (a && b))", Spaces);
14391   verifyFormat("\"Error!\"", Spaces);
14392   verifyFormat("! ! x", Spaces);
14393 }
14394 
14395 TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
14396   FormatStyle Spaces = getLLVMStyle();
14397 
14398   Spaces.SpacesInParentheses = true;
14399   verifyFormat("do_something( ::globalVar );", Spaces);
14400   verifyFormat("call( x, y, z );", Spaces);
14401   verifyFormat("call();", Spaces);
14402   verifyFormat("std::function<void( int, int )> callback;", Spaces);
14403   verifyFormat("void inFunction() { std::function<void( int, int )> fct; }",
14404                Spaces);
14405   verifyFormat("while ( (bool)1 )\n"
14406                "  continue;",
14407                Spaces);
14408   verifyFormat("for ( ;; )\n"
14409                "  continue;",
14410                Spaces);
14411   verifyFormat("if ( true )\n"
14412                "  f();\n"
14413                "else if ( true )\n"
14414                "  f();",
14415                Spaces);
14416   verifyFormat("do {\n"
14417                "  do_something( (int)i );\n"
14418                "} while ( something() );",
14419                Spaces);
14420   verifyFormat("switch ( x ) {\n"
14421                "default:\n"
14422                "  break;\n"
14423                "}",
14424                Spaces);
14425 
14426   Spaces.SpacesInParentheses = false;
14427   Spaces.SpacesInCStyleCastParentheses = true;
14428   verifyFormat("Type *A = ( Type * )P;", Spaces);
14429   verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
14430   verifyFormat("x = ( int32 )y;", Spaces);
14431   verifyFormat("int a = ( int )(2.0f);", Spaces);
14432   verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
14433   verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
14434   verifyFormat("#define x (( int )-1)", Spaces);
14435 
14436   // Run the first set of tests again with:
14437   Spaces.SpacesInParentheses = false;
14438   Spaces.SpaceInEmptyParentheses = true;
14439   Spaces.SpacesInCStyleCastParentheses = true;
14440   verifyFormat("call(x, y, z);", Spaces);
14441   verifyFormat("call( );", Spaces);
14442   verifyFormat("std::function<void(int, int)> callback;", Spaces);
14443   verifyFormat("while (( bool )1)\n"
14444                "  continue;",
14445                Spaces);
14446   verifyFormat("for (;;)\n"
14447                "  continue;",
14448                Spaces);
14449   verifyFormat("if (true)\n"
14450                "  f( );\n"
14451                "else if (true)\n"
14452                "  f( );",
14453                Spaces);
14454   verifyFormat("do {\n"
14455                "  do_something(( int )i);\n"
14456                "} while (something( ));",
14457                Spaces);
14458   verifyFormat("switch (x) {\n"
14459                "default:\n"
14460                "  break;\n"
14461                "}",
14462                Spaces);
14463 
14464   // Run the first set of tests again with:
14465   Spaces.SpaceAfterCStyleCast = true;
14466   verifyFormat("call(x, y, z);", Spaces);
14467   verifyFormat("call( );", Spaces);
14468   verifyFormat("std::function<void(int, int)> callback;", Spaces);
14469   verifyFormat("while (( bool ) 1)\n"
14470                "  continue;",
14471                Spaces);
14472   verifyFormat("for (;;)\n"
14473                "  continue;",
14474                Spaces);
14475   verifyFormat("if (true)\n"
14476                "  f( );\n"
14477                "else if (true)\n"
14478                "  f( );",
14479                Spaces);
14480   verifyFormat("do {\n"
14481                "  do_something(( int ) i);\n"
14482                "} while (something( ));",
14483                Spaces);
14484   verifyFormat("switch (x) {\n"
14485                "default:\n"
14486                "  break;\n"
14487                "}",
14488                Spaces);
14489 
14490   // Run subset of tests again with:
14491   Spaces.SpacesInCStyleCastParentheses = false;
14492   Spaces.SpaceAfterCStyleCast = true;
14493   verifyFormat("while ((bool) 1)\n"
14494                "  continue;",
14495                Spaces);
14496   verifyFormat("do {\n"
14497                "  do_something((int) i);\n"
14498                "} while (something( ));",
14499                Spaces);
14500 
14501   verifyFormat("size_t idx = (size_t) (ptr - ((char *) file));", Spaces);
14502   verifyFormat("size_t idx = (size_t) a;", Spaces);
14503   verifyFormat("size_t idx = (size_t) (a - 1);", Spaces);
14504   verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces);
14505   verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces);
14506   verifyFormat("size_t idx = (*foo)(a - 1);", Spaces);
14507   verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces);
14508   Spaces.ColumnLimit = 80;
14509   Spaces.IndentWidth = 4;
14510   Spaces.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
14511   verifyFormat("void foo( ) {\n"
14512                "    size_t foo = (*(function))(\n"
14513                "        Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, "
14514                "BarrrrrrrrrrrrLong,\n"
14515                "        FoooooooooLooooong);\n"
14516                "}",
14517                Spaces);
14518   Spaces.SpaceAfterCStyleCast = false;
14519   verifyFormat("size_t idx = (size_t)(ptr - ((char *)file));", Spaces);
14520   verifyFormat("size_t idx = (size_t)a;", Spaces);
14521   verifyFormat("size_t idx = (size_t)(a - 1);", Spaces);
14522   verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces);
14523   verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces);
14524   verifyFormat("size_t idx = (*foo)(a - 1);", Spaces);
14525   verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces);
14526 
14527   verifyFormat("void foo( ) {\n"
14528                "    size_t foo = (*(function))(\n"
14529                "        Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, "
14530                "BarrrrrrrrrrrrLong,\n"
14531                "        FoooooooooLooooong);\n"
14532                "}",
14533                Spaces);
14534 }
14535 
14536 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
14537   verifyFormat("int a[5];");
14538   verifyFormat("a[3] += 42;");
14539 
14540   FormatStyle Spaces = getLLVMStyle();
14541   Spaces.SpacesInSquareBrackets = true;
14542   // Not lambdas.
14543   verifyFormat("int a[ 5 ];", Spaces);
14544   verifyFormat("a[ 3 ] += 42;", Spaces);
14545   verifyFormat("constexpr char hello[]{\"hello\"};", Spaces);
14546   verifyFormat("double &operator[](int i) { return 0; }\n"
14547                "int i;",
14548                Spaces);
14549   verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces);
14550   verifyFormat("int i = a[ a ][ a ]->f();", Spaces);
14551   verifyFormat("int i = (*b)[ a ]->f();", Spaces);
14552   // Lambdas.
14553   verifyFormat("int c = []() -> int { return 2; }();\n", Spaces);
14554   verifyFormat("return [ i, args... ] {};", Spaces);
14555   verifyFormat("int foo = [ &bar ]() {};", Spaces);
14556   verifyFormat("int foo = [ = ]() {};", Spaces);
14557   verifyFormat("int foo = [ & ]() {};", Spaces);
14558   verifyFormat("int foo = [ =, &bar ]() {};", Spaces);
14559   verifyFormat("int foo = [ &bar, = ]() {};", Spaces);
14560 }
14561 
14562 TEST_F(FormatTest, ConfigurableSpaceBeforeBrackets) {
14563   FormatStyle NoSpaceStyle = getLLVMStyle();
14564   verifyFormat("int a[5];", NoSpaceStyle);
14565   verifyFormat("a[3] += 42;", NoSpaceStyle);
14566 
14567   verifyFormat("int a[1];", NoSpaceStyle);
14568   verifyFormat("int 1 [a];", NoSpaceStyle);
14569   verifyFormat("int a[1][2];", NoSpaceStyle);
14570   verifyFormat("a[7] = 5;", NoSpaceStyle);
14571   verifyFormat("int a = (f())[23];", NoSpaceStyle);
14572   verifyFormat("f([] {})", NoSpaceStyle);
14573 
14574   FormatStyle Space = getLLVMStyle();
14575   Space.SpaceBeforeSquareBrackets = true;
14576   verifyFormat("int c = []() -> int { return 2; }();\n", Space);
14577   verifyFormat("return [i, args...] {};", Space);
14578 
14579   verifyFormat("int a [5];", Space);
14580   verifyFormat("a [3] += 42;", Space);
14581   verifyFormat("constexpr char hello []{\"hello\"};", Space);
14582   verifyFormat("double &operator[](int i) { return 0; }\n"
14583                "int i;",
14584                Space);
14585   verifyFormat("std::unique_ptr<int []> foo() {}", Space);
14586   verifyFormat("int i = a [a][a]->f();", Space);
14587   verifyFormat("int i = (*b) [a]->f();", Space);
14588 
14589   verifyFormat("int a [1];", Space);
14590   verifyFormat("int 1 [a];", Space);
14591   verifyFormat("int a [1][2];", Space);
14592   verifyFormat("a [7] = 5;", Space);
14593   verifyFormat("int a = (f()) [23];", Space);
14594   verifyFormat("f([] {})", Space);
14595 }
14596 
14597 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
14598   verifyFormat("int a = 5;");
14599   verifyFormat("a += 42;");
14600   verifyFormat("a or_eq 8;");
14601 
14602   FormatStyle Spaces = getLLVMStyle();
14603   Spaces.SpaceBeforeAssignmentOperators = false;
14604   verifyFormat("int a= 5;", Spaces);
14605   verifyFormat("a+= 42;", Spaces);
14606   verifyFormat("a or_eq 8;", Spaces);
14607 }
14608 
14609 TEST_F(FormatTest, ConfigurableSpaceBeforeColon) {
14610   verifyFormat("class Foo : public Bar {};");
14611   verifyFormat("Foo::Foo() : foo(1) {}");
14612   verifyFormat("for (auto a : b) {\n}");
14613   verifyFormat("int x = a ? b : c;");
14614   verifyFormat("{\n"
14615                "label0:\n"
14616                "  int x = 0;\n"
14617                "}");
14618   verifyFormat("switch (x) {\n"
14619                "case 1:\n"
14620                "default:\n"
14621                "}");
14622   verifyFormat("switch (allBraces) {\n"
14623                "case 1: {\n"
14624                "  break;\n"
14625                "}\n"
14626                "case 2: {\n"
14627                "  [[fallthrough]];\n"
14628                "}\n"
14629                "default: {\n"
14630                "  break;\n"
14631                "}\n"
14632                "}");
14633 
14634   FormatStyle CtorInitializerStyle = getLLVMStyleWithColumns(30);
14635   CtorInitializerStyle.SpaceBeforeCtorInitializerColon = false;
14636   verifyFormat("class Foo : public Bar {};", CtorInitializerStyle);
14637   verifyFormat("Foo::Foo(): foo(1) {}", CtorInitializerStyle);
14638   verifyFormat("for (auto a : b) {\n}", CtorInitializerStyle);
14639   verifyFormat("int x = a ? b : c;", CtorInitializerStyle);
14640   verifyFormat("{\n"
14641                "label1:\n"
14642                "  int x = 0;\n"
14643                "}",
14644                CtorInitializerStyle);
14645   verifyFormat("switch (x) {\n"
14646                "case 1:\n"
14647                "default:\n"
14648                "}",
14649                CtorInitializerStyle);
14650   verifyFormat("switch (allBraces) {\n"
14651                "case 1: {\n"
14652                "  break;\n"
14653                "}\n"
14654                "case 2: {\n"
14655                "  [[fallthrough]];\n"
14656                "}\n"
14657                "default: {\n"
14658                "  break;\n"
14659                "}\n"
14660                "}",
14661                CtorInitializerStyle);
14662   CtorInitializerStyle.BreakConstructorInitializers =
14663       FormatStyle::BCIS_AfterColon;
14664   verifyFormat("Fooooooooooo::Fooooooooooo():\n"
14665                "    aaaaaaaaaaaaaaaa(1),\n"
14666                "    bbbbbbbbbbbbbbbb(2) {}",
14667                CtorInitializerStyle);
14668   CtorInitializerStyle.BreakConstructorInitializers =
14669       FormatStyle::BCIS_BeforeComma;
14670   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
14671                "    : aaaaaaaaaaaaaaaa(1)\n"
14672                "    , bbbbbbbbbbbbbbbb(2) {}",
14673                CtorInitializerStyle);
14674   CtorInitializerStyle.BreakConstructorInitializers =
14675       FormatStyle::BCIS_BeforeColon;
14676   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
14677                "    : aaaaaaaaaaaaaaaa(1),\n"
14678                "      bbbbbbbbbbbbbbbb(2) {}",
14679                CtorInitializerStyle);
14680   CtorInitializerStyle.ConstructorInitializerIndentWidth = 0;
14681   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
14682                ": aaaaaaaaaaaaaaaa(1),\n"
14683                "  bbbbbbbbbbbbbbbb(2) {}",
14684                CtorInitializerStyle);
14685 
14686   FormatStyle InheritanceStyle = getLLVMStyleWithColumns(30);
14687   InheritanceStyle.SpaceBeforeInheritanceColon = false;
14688   verifyFormat("class Foo: public Bar {};", InheritanceStyle);
14689   verifyFormat("Foo::Foo() : foo(1) {}", InheritanceStyle);
14690   verifyFormat("for (auto a : b) {\n}", InheritanceStyle);
14691   verifyFormat("int x = a ? b : c;", InheritanceStyle);
14692   verifyFormat("{\n"
14693                "label2:\n"
14694                "  int x = 0;\n"
14695                "}",
14696                InheritanceStyle);
14697   verifyFormat("switch (x) {\n"
14698                "case 1:\n"
14699                "default:\n"
14700                "}",
14701                InheritanceStyle);
14702   verifyFormat("switch (allBraces) {\n"
14703                "case 1: {\n"
14704                "  break;\n"
14705                "}\n"
14706                "case 2: {\n"
14707                "  [[fallthrough]];\n"
14708                "}\n"
14709                "default: {\n"
14710                "  break;\n"
14711                "}\n"
14712                "}",
14713                InheritanceStyle);
14714   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterComma;
14715   verifyFormat("class Foooooooooooooooooooooo\n"
14716                "    : public aaaaaaaaaaaaaaaaaa,\n"
14717                "      public bbbbbbbbbbbbbbbbbb {\n"
14718                "}",
14719                InheritanceStyle);
14720   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterColon;
14721   verifyFormat("class Foooooooooooooooooooooo:\n"
14722                "    public aaaaaaaaaaaaaaaaaa,\n"
14723                "    public bbbbbbbbbbbbbbbbbb {\n"
14724                "}",
14725                InheritanceStyle);
14726   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeComma;
14727   verifyFormat("class Foooooooooooooooooooooo\n"
14728                "    : public aaaaaaaaaaaaaaaaaa\n"
14729                "    , public bbbbbbbbbbbbbbbbbb {\n"
14730                "}",
14731                InheritanceStyle);
14732   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
14733   verifyFormat("class Foooooooooooooooooooooo\n"
14734                "    : public aaaaaaaaaaaaaaaaaa,\n"
14735                "      public bbbbbbbbbbbbbbbbbb {\n"
14736                "}",
14737                InheritanceStyle);
14738   InheritanceStyle.ConstructorInitializerIndentWidth = 0;
14739   verifyFormat("class Foooooooooooooooooooooo\n"
14740                ": public aaaaaaaaaaaaaaaaaa,\n"
14741                "  public bbbbbbbbbbbbbbbbbb {}",
14742                InheritanceStyle);
14743 
14744   FormatStyle ForLoopStyle = getLLVMStyle();
14745   ForLoopStyle.SpaceBeforeRangeBasedForLoopColon = false;
14746   verifyFormat("class Foo : public Bar {};", ForLoopStyle);
14747   verifyFormat("Foo::Foo() : foo(1) {}", ForLoopStyle);
14748   verifyFormat("for (auto a: b) {\n}", ForLoopStyle);
14749   verifyFormat("int x = a ? b : c;", ForLoopStyle);
14750   verifyFormat("{\n"
14751                "label2:\n"
14752                "  int x = 0;\n"
14753                "}",
14754                ForLoopStyle);
14755   verifyFormat("switch (x) {\n"
14756                "case 1:\n"
14757                "default:\n"
14758                "}",
14759                ForLoopStyle);
14760   verifyFormat("switch (allBraces) {\n"
14761                "case 1: {\n"
14762                "  break;\n"
14763                "}\n"
14764                "case 2: {\n"
14765                "  [[fallthrough]];\n"
14766                "}\n"
14767                "default: {\n"
14768                "  break;\n"
14769                "}\n"
14770                "}",
14771                ForLoopStyle);
14772 
14773   FormatStyle CaseStyle = getLLVMStyle();
14774   CaseStyle.SpaceBeforeCaseColon = true;
14775   verifyFormat("class Foo : public Bar {};", CaseStyle);
14776   verifyFormat("Foo::Foo() : foo(1) {}", CaseStyle);
14777   verifyFormat("for (auto a : b) {\n}", CaseStyle);
14778   verifyFormat("int x = a ? b : c;", CaseStyle);
14779   verifyFormat("switch (x) {\n"
14780                "case 1 :\n"
14781                "default :\n"
14782                "}",
14783                CaseStyle);
14784   verifyFormat("switch (allBraces) {\n"
14785                "case 1 : {\n"
14786                "  break;\n"
14787                "}\n"
14788                "case 2 : {\n"
14789                "  [[fallthrough]];\n"
14790                "}\n"
14791                "default : {\n"
14792                "  break;\n"
14793                "}\n"
14794                "}",
14795                CaseStyle);
14796 
14797   FormatStyle NoSpaceStyle = getLLVMStyle();
14798   EXPECT_EQ(NoSpaceStyle.SpaceBeforeCaseColon, false);
14799   NoSpaceStyle.SpaceBeforeCtorInitializerColon = false;
14800   NoSpaceStyle.SpaceBeforeInheritanceColon = false;
14801   NoSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false;
14802   verifyFormat("class Foo: public Bar {};", NoSpaceStyle);
14803   verifyFormat("Foo::Foo(): foo(1) {}", NoSpaceStyle);
14804   verifyFormat("for (auto a: b) {\n}", NoSpaceStyle);
14805   verifyFormat("int x = a ? b : c;", NoSpaceStyle);
14806   verifyFormat("{\n"
14807                "label3:\n"
14808                "  int x = 0;\n"
14809                "}",
14810                NoSpaceStyle);
14811   verifyFormat("switch (x) {\n"
14812                "case 1:\n"
14813                "default:\n"
14814                "}",
14815                NoSpaceStyle);
14816   verifyFormat("switch (allBraces) {\n"
14817                "case 1: {\n"
14818                "  break;\n"
14819                "}\n"
14820                "case 2: {\n"
14821                "  [[fallthrough]];\n"
14822                "}\n"
14823                "default: {\n"
14824                "  break;\n"
14825                "}\n"
14826                "}",
14827                NoSpaceStyle);
14828 
14829   FormatStyle InvertedSpaceStyle = getLLVMStyle();
14830   InvertedSpaceStyle.SpaceBeforeCaseColon = true;
14831   InvertedSpaceStyle.SpaceBeforeCtorInitializerColon = false;
14832   InvertedSpaceStyle.SpaceBeforeInheritanceColon = false;
14833   InvertedSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false;
14834   verifyFormat("class Foo: public Bar {};", InvertedSpaceStyle);
14835   verifyFormat("Foo::Foo(): foo(1) {}", InvertedSpaceStyle);
14836   verifyFormat("for (auto a: b) {\n}", InvertedSpaceStyle);
14837   verifyFormat("int x = a ? b : c;", InvertedSpaceStyle);
14838   verifyFormat("{\n"
14839                "label3:\n"
14840                "  int x = 0;\n"
14841                "}",
14842                InvertedSpaceStyle);
14843   verifyFormat("switch (x) {\n"
14844                "case 1 :\n"
14845                "case 2 : {\n"
14846                "  break;\n"
14847                "}\n"
14848                "default :\n"
14849                "  break;\n"
14850                "}",
14851                InvertedSpaceStyle);
14852   verifyFormat("switch (allBraces) {\n"
14853                "case 1 : {\n"
14854                "  break;\n"
14855                "}\n"
14856                "case 2 : {\n"
14857                "  [[fallthrough]];\n"
14858                "}\n"
14859                "default : {\n"
14860                "  break;\n"
14861                "}\n"
14862                "}",
14863                InvertedSpaceStyle);
14864 }
14865 
14866 TEST_F(FormatTest, ConfigurableSpaceAroundPointerQualifiers) {
14867   FormatStyle Style = getLLVMStyle();
14868 
14869   Style.PointerAlignment = FormatStyle::PAS_Left;
14870   Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default;
14871   verifyFormat("void* const* x = NULL;", Style);
14872 
14873 #define verifyQualifierSpaces(Code, Pointers, Qualifiers)                      \
14874   do {                                                                         \
14875     Style.PointerAlignment = FormatStyle::Pointers;                            \
14876     Style.SpaceAroundPointerQualifiers = FormatStyle::Qualifiers;              \
14877     verifyFormat(Code, Style);                                                 \
14878   } while (false)
14879 
14880   verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Default);
14881   verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_Default);
14882   verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Default);
14883 
14884   verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Before);
14885   verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Before);
14886   verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Before);
14887 
14888   verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_After);
14889   verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_After);
14890   verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_After);
14891 
14892   verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_Both);
14893   verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Both);
14894   verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Both);
14895 
14896   verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Default);
14897   verifyQualifierSpaces("Foo::operator void const *();", PAS_Right,
14898                         SAPQ_Default);
14899   verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
14900                         SAPQ_Default);
14901 
14902   verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Before);
14903   verifyQualifierSpaces("Foo::operator void const *();", PAS_Right,
14904                         SAPQ_Before);
14905   verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
14906                         SAPQ_Before);
14907 
14908   verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_After);
14909   verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_After);
14910   verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
14911                         SAPQ_After);
14912 
14913   verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_Both);
14914   verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_Both);
14915   verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, SAPQ_Both);
14916 
14917 #undef verifyQualifierSpaces
14918 
14919   FormatStyle Spaces = getLLVMStyle();
14920   Spaces.AttributeMacros.push_back("qualified");
14921   Spaces.PointerAlignment = FormatStyle::PAS_Right;
14922   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default;
14923   verifyFormat("SomeType *volatile *a = NULL;", Spaces);
14924   verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces);
14925   verifyFormat("std::vector<SomeType *const *> x;", Spaces);
14926   verifyFormat("std::vector<SomeType *qualified *> x;", Spaces);
14927   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
14928   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before;
14929   verifyFormat("SomeType * volatile *a = NULL;", Spaces);
14930   verifyFormat("SomeType * __attribute__((attr)) *a = NULL;", Spaces);
14931   verifyFormat("std::vector<SomeType * const *> x;", Spaces);
14932   verifyFormat("std::vector<SomeType * qualified *> x;", Spaces);
14933   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
14934 
14935   // Check that SAPQ_Before doesn't result in extra spaces for PAS_Left.
14936   Spaces.PointerAlignment = FormatStyle::PAS_Left;
14937   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before;
14938   verifyFormat("SomeType* volatile* a = NULL;", Spaces);
14939   verifyFormat("SomeType* __attribute__((attr))* a = NULL;", Spaces);
14940   verifyFormat("std::vector<SomeType* const*> x;", Spaces);
14941   verifyFormat("std::vector<SomeType* qualified*> x;", Spaces);
14942   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
14943   // However, setting it to SAPQ_After should add spaces after __attribute, etc.
14944   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After;
14945   verifyFormat("SomeType* volatile * a = NULL;", Spaces);
14946   verifyFormat("SomeType* __attribute__((attr)) * a = NULL;", Spaces);
14947   verifyFormat("std::vector<SomeType* const *> x;", Spaces);
14948   verifyFormat("std::vector<SomeType* qualified *> x;", Spaces);
14949   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
14950 
14951   // PAS_Middle should not have any noticeable changes even for SAPQ_Both
14952   Spaces.PointerAlignment = FormatStyle::PAS_Middle;
14953   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After;
14954   verifyFormat("SomeType * volatile * a = NULL;", Spaces);
14955   verifyFormat("SomeType * __attribute__((attr)) * a = NULL;", Spaces);
14956   verifyFormat("std::vector<SomeType * const *> x;", Spaces);
14957   verifyFormat("std::vector<SomeType * qualified *> x;", Spaces);
14958   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
14959 }
14960 
14961 TEST_F(FormatTest, AlignConsecutiveMacros) {
14962   FormatStyle Style = getLLVMStyle();
14963   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
14964   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
14965   Style.AlignConsecutiveMacros = FormatStyle::ACS_None;
14966 
14967   verifyFormat("#define a 3\n"
14968                "#define bbbb 4\n"
14969                "#define ccc (5)",
14970                Style);
14971 
14972   verifyFormat("#define f(x) (x * x)\n"
14973                "#define fff(x, y, z) (x * y + z)\n"
14974                "#define ffff(x, y) (x - y)",
14975                Style);
14976 
14977   verifyFormat("#define foo(x, y) (x + y)\n"
14978                "#define bar (5, 6)(2 + 2)",
14979                Style);
14980 
14981   verifyFormat("#define a 3\n"
14982                "#define bbbb 4\n"
14983                "#define ccc (5)\n"
14984                "#define f(x) (x * x)\n"
14985                "#define fff(x, y, z) (x * y + z)\n"
14986                "#define ffff(x, y) (x - y)",
14987                Style);
14988 
14989   Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
14990   verifyFormat("#define a    3\n"
14991                "#define bbbb 4\n"
14992                "#define ccc  (5)",
14993                Style);
14994 
14995   verifyFormat("#define f(x)         (x * x)\n"
14996                "#define fff(x, y, z) (x * y + z)\n"
14997                "#define ffff(x, y)   (x - y)",
14998                Style);
14999 
15000   verifyFormat("#define foo(x, y) (x + y)\n"
15001                "#define bar       (5, 6)(2 + 2)",
15002                Style);
15003 
15004   verifyFormat("#define a            3\n"
15005                "#define bbbb         4\n"
15006                "#define ccc          (5)\n"
15007                "#define f(x)         (x * x)\n"
15008                "#define fff(x, y, z) (x * y + z)\n"
15009                "#define ffff(x, y)   (x - y)",
15010                Style);
15011 
15012   verifyFormat("#define a         5\n"
15013                "#define foo(x, y) (x + y)\n"
15014                "#define CCC       (6)\n"
15015                "auto lambda = []() {\n"
15016                "  auto  ii = 0;\n"
15017                "  float j  = 0;\n"
15018                "  return 0;\n"
15019                "};\n"
15020                "int   i  = 0;\n"
15021                "float i2 = 0;\n"
15022                "auto  v  = type{\n"
15023                "    i = 1,   //\n"
15024                "    (i = 2), //\n"
15025                "    i = 3    //\n"
15026                "};",
15027                Style);
15028 
15029   Style.AlignConsecutiveMacros = FormatStyle::ACS_None;
15030   Style.ColumnLimit = 20;
15031 
15032   verifyFormat("#define a          \\\n"
15033                "  \"aabbbbbbbbbbbb\"\n"
15034                "#define D          \\\n"
15035                "  \"aabbbbbbbbbbbb\" \\\n"
15036                "  \"ccddeeeeeeeee\"\n"
15037                "#define B          \\\n"
15038                "  \"QQQQQQQQQQQQQ\"  \\\n"
15039                "  \"FFFFFFFFFFFFF\"  \\\n"
15040                "  \"LLLLLLLL\"\n",
15041                Style);
15042 
15043   Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
15044   verifyFormat("#define a          \\\n"
15045                "  \"aabbbbbbbbbbbb\"\n"
15046                "#define D          \\\n"
15047                "  \"aabbbbbbbbbbbb\" \\\n"
15048                "  \"ccddeeeeeeeee\"\n"
15049                "#define B          \\\n"
15050                "  \"QQQQQQQQQQQQQ\"  \\\n"
15051                "  \"FFFFFFFFFFFFF\"  \\\n"
15052                "  \"LLLLLLLL\"\n",
15053                Style);
15054 
15055   // Test across comments
15056   Style.MaxEmptyLinesToKeep = 10;
15057   Style.ReflowComments = false;
15058   Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossComments;
15059   EXPECT_EQ("#define a    3\n"
15060             "// line comment\n"
15061             "#define bbbb 4\n"
15062             "#define ccc  (5)",
15063             format("#define a 3\n"
15064                    "// line comment\n"
15065                    "#define bbbb 4\n"
15066                    "#define ccc (5)",
15067                    Style));
15068 
15069   EXPECT_EQ("#define a    3\n"
15070             "/* block comment */\n"
15071             "#define bbbb 4\n"
15072             "#define ccc  (5)",
15073             format("#define a  3\n"
15074                    "/* block comment */\n"
15075                    "#define bbbb 4\n"
15076                    "#define ccc (5)",
15077                    Style));
15078 
15079   EXPECT_EQ("#define a    3\n"
15080             "/* multi-line *\n"
15081             " * block comment */\n"
15082             "#define bbbb 4\n"
15083             "#define ccc  (5)",
15084             format("#define a 3\n"
15085                    "/* multi-line *\n"
15086                    " * block comment */\n"
15087                    "#define bbbb 4\n"
15088                    "#define ccc (5)",
15089                    Style));
15090 
15091   EXPECT_EQ("#define a    3\n"
15092             "// multi-line line comment\n"
15093             "//\n"
15094             "#define bbbb 4\n"
15095             "#define ccc  (5)",
15096             format("#define a  3\n"
15097                    "// multi-line line comment\n"
15098                    "//\n"
15099                    "#define bbbb 4\n"
15100                    "#define ccc (5)",
15101                    Style));
15102 
15103   EXPECT_EQ("#define a 3\n"
15104             "// empty lines still break.\n"
15105             "\n"
15106             "#define bbbb 4\n"
15107             "#define ccc  (5)",
15108             format("#define a     3\n"
15109                    "// empty lines still break.\n"
15110                    "\n"
15111                    "#define bbbb     4\n"
15112                    "#define ccc  (5)",
15113                    Style));
15114 
15115   // Test across empty lines
15116   Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossEmptyLines;
15117   EXPECT_EQ("#define a    3\n"
15118             "\n"
15119             "#define bbbb 4\n"
15120             "#define ccc  (5)",
15121             format("#define a 3\n"
15122                    "\n"
15123                    "#define bbbb 4\n"
15124                    "#define ccc (5)",
15125                    Style));
15126 
15127   EXPECT_EQ("#define a    3\n"
15128             "\n"
15129             "\n"
15130             "\n"
15131             "#define bbbb 4\n"
15132             "#define ccc  (5)",
15133             format("#define a        3\n"
15134                    "\n"
15135                    "\n"
15136                    "\n"
15137                    "#define bbbb 4\n"
15138                    "#define ccc (5)",
15139                    Style));
15140 
15141   EXPECT_EQ("#define a 3\n"
15142             "// comments should break alignment\n"
15143             "//\n"
15144             "#define bbbb 4\n"
15145             "#define ccc  (5)",
15146             format("#define a        3\n"
15147                    "// comments should break alignment\n"
15148                    "//\n"
15149                    "#define bbbb 4\n"
15150                    "#define ccc (5)",
15151                    Style));
15152 
15153   // Test across empty lines and comments
15154   Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossEmptyLinesAndComments;
15155   verifyFormat("#define a    3\n"
15156                "\n"
15157                "// line comment\n"
15158                "#define bbbb 4\n"
15159                "#define ccc  (5)",
15160                Style);
15161 
15162   EXPECT_EQ("#define a    3\n"
15163             "\n"
15164             "\n"
15165             "/* multi-line *\n"
15166             " * block comment */\n"
15167             "\n"
15168             "\n"
15169             "#define bbbb 4\n"
15170             "#define ccc  (5)",
15171             format("#define a 3\n"
15172                    "\n"
15173                    "\n"
15174                    "/* multi-line *\n"
15175                    " * block comment */\n"
15176                    "\n"
15177                    "\n"
15178                    "#define bbbb 4\n"
15179                    "#define ccc (5)",
15180                    Style));
15181 
15182   EXPECT_EQ("#define a    3\n"
15183             "\n"
15184             "\n"
15185             "/* multi-line *\n"
15186             " * block comment */\n"
15187             "\n"
15188             "\n"
15189             "#define bbbb 4\n"
15190             "#define ccc  (5)",
15191             format("#define a 3\n"
15192                    "\n"
15193                    "\n"
15194                    "/* multi-line *\n"
15195                    " * block comment */\n"
15196                    "\n"
15197                    "\n"
15198                    "#define bbbb 4\n"
15199                    "#define ccc       (5)",
15200                    Style));
15201 }
15202 
15203 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLines) {
15204   FormatStyle Alignment = getLLVMStyle();
15205   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
15206   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_AcrossEmptyLines;
15207 
15208   Alignment.MaxEmptyLinesToKeep = 10;
15209   /* Test alignment across empty lines */
15210   EXPECT_EQ("int a           = 5;\n"
15211             "\n"
15212             "int oneTwoThree = 123;",
15213             format("int a       = 5;\n"
15214                    "\n"
15215                    "int oneTwoThree= 123;",
15216                    Alignment));
15217   EXPECT_EQ("int a           = 5;\n"
15218             "int one         = 1;\n"
15219             "\n"
15220             "int oneTwoThree = 123;",
15221             format("int a = 5;\n"
15222                    "int one = 1;\n"
15223                    "\n"
15224                    "int oneTwoThree = 123;",
15225                    Alignment));
15226   EXPECT_EQ("int a           = 5;\n"
15227             "int one         = 1;\n"
15228             "\n"
15229             "int oneTwoThree = 123;\n"
15230             "int oneTwo      = 12;",
15231             format("int a = 5;\n"
15232                    "int one = 1;\n"
15233                    "\n"
15234                    "int oneTwoThree = 123;\n"
15235                    "int oneTwo = 12;",
15236                    Alignment));
15237 
15238   /* Test across comments */
15239   EXPECT_EQ("int a = 5;\n"
15240             "/* block comment */\n"
15241             "int oneTwoThree = 123;",
15242             format("int a = 5;\n"
15243                    "/* block comment */\n"
15244                    "int oneTwoThree=123;",
15245                    Alignment));
15246 
15247   EXPECT_EQ("int a = 5;\n"
15248             "// line comment\n"
15249             "int oneTwoThree = 123;",
15250             format("int a = 5;\n"
15251                    "// line comment\n"
15252                    "int oneTwoThree=123;",
15253                    Alignment));
15254 
15255   /* Test across comments and newlines */
15256   EXPECT_EQ("int a = 5;\n"
15257             "\n"
15258             "/* block comment */\n"
15259             "int oneTwoThree = 123;",
15260             format("int a = 5;\n"
15261                    "\n"
15262                    "/* block comment */\n"
15263                    "int oneTwoThree=123;",
15264                    Alignment));
15265 
15266   EXPECT_EQ("int a = 5;\n"
15267             "\n"
15268             "// line comment\n"
15269             "int oneTwoThree = 123;",
15270             format("int a = 5;\n"
15271                    "\n"
15272                    "// line comment\n"
15273                    "int oneTwoThree=123;",
15274                    Alignment));
15275 }
15276 
15277 TEST_F(FormatTest, AlignConsecutiveDeclarationsAcrossEmptyLinesAndComments) {
15278   FormatStyle Alignment = getLLVMStyle();
15279   Alignment.AlignConsecutiveDeclarations =
15280       FormatStyle::ACS_AcrossEmptyLinesAndComments;
15281   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
15282 
15283   Alignment.MaxEmptyLinesToKeep = 10;
15284   /* Test alignment across empty lines */
15285   EXPECT_EQ("int         a = 5;\n"
15286             "\n"
15287             "float const oneTwoThree = 123;",
15288             format("int a = 5;\n"
15289                    "\n"
15290                    "float const oneTwoThree = 123;",
15291                    Alignment));
15292   EXPECT_EQ("int         a = 5;\n"
15293             "float const one = 1;\n"
15294             "\n"
15295             "int         oneTwoThree = 123;",
15296             format("int a = 5;\n"
15297                    "float const one = 1;\n"
15298                    "\n"
15299                    "int oneTwoThree = 123;",
15300                    Alignment));
15301 
15302   /* Test across comments */
15303   EXPECT_EQ("float const a = 5;\n"
15304             "/* block comment */\n"
15305             "int         oneTwoThree = 123;",
15306             format("float const a = 5;\n"
15307                    "/* block comment */\n"
15308                    "int oneTwoThree=123;",
15309                    Alignment));
15310 
15311   EXPECT_EQ("float const a = 5;\n"
15312             "// line comment\n"
15313             "int         oneTwoThree = 123;",
15314             format("float const a = 5;\n"
15315                    "// line comment\n"
15316                    "int oneTwoThree=123;",
15317                    Alignment));
15318 
15319   /* Test across comments and newlines */
15320   EXPECT_EQ("float const a = 5;\n"
15321             "\n"
15322             "/* block comment */\n"
15323             "int         oneTwoThree = 123;",
15324             format("float const a = 5;\n"
15325                    "\n"
15326                    "/* block comment */\n"
15327                    "int         oneTwoThree=123;",
15328                    Alignment));
15329 
15330   EXPECT_EQ("float const a = 5;\n"
15331             "\n"
15332             "// line comment\n"
15333             "int         oneTwoThree = 123;",
15334             format("float const a = 5;\n"
15335                    "\n"
15336                    "// line comment\n"
15337                    "int oneTwoThree=123;",
15338                    Alignment));
15339 }
15340 
15341 TEST_F(FormatTest, AlignConsecutiveBitFieldsAcrossEmptyLinesAndComments) {
15342   FormatStyle Alignment = getLLVMStyle();
15343   Alignment.AlignConsecutiveBitFields =
15344       FormatStyle::ACS_AcrossEmptyLinesAndComments;
15345 
15346   Alignment.MaxEmptyLinesToKeep = 10;
15347   /* Test alignment across empty lines */
15348   EXPECT_EQ("int a            : 5;\n"
15349             "\n"
15350             "int longbitfield : 6;",
15351             format("int a : 5;\n"
15352                    "\n"
15353                    "int longbitfield : 6;",
15354                    Alignment));
15355   EXPECT_EQ("int a            : 5;\n"
15356             "int one          : 1;\n"
15357             "\n"
15358             "int longbitfield : 6;",
15359             format("int a : 5;\n"
15360                    "int one : 1;\n"
15361                    "\n"
15362                    "int longbitfield : 6;",
15363                    Alignment));
15364 
15365   /* Test across comments */
15366   EXPECT_EQ("int a            : 5;\n"
15367             "/* block comment */\n"
15368             "int longbitfield : 6;",
15369             format("int a : 5;\n"
15370                    "/* block comment */\n"
15371                    "int longbitfield : 6;",
15372                    Alignment));
15373   EXPECT_EQ("int a            : 5;\n"
15374             "int one          : 1;\n"
15375             "// line comment\n"
15376             "int longbitfield : 6;",
15377             format("int a : 5;\n"
15378                    "int one : 1;\n"
15379                    "// line comment\n"
15380                    "int longbitfield : 6;",
15381                    Alignment));
15382 
15383   /* Test across comments and newlines */
15384   EXPECT_EQ("int a            : 5;\n"
15385             "/* block comment */\n"
15386             "\n"
15387             "int longbitfield : 6;",
15388             format("int a : 5;\n"
15389                    "/* block comment */\n"
15390                    "\n"
15391                    "int longbitfield : 6;",
15392                    Alignment));
15393   EXPECT_EQ("int a            : 5;\n"
15394             "int one          : 1;\n"
15395             "\n"
15396             "// line comment\n"
15397             "\n"
15398             "int longbitfield : 6;",
15399             format("int a : 5;\n"
15400                    "int one : 1;\n"
15401                    "\n"
15402                    "// line comment \n"
15403                    "\n"
15404                    "int longbitfield : 6;",
15405                    Alignment));
15406 }
15407 
15408 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossComments) {
15409   FormatStyle Alignment = getLLVMStyle();
15410   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
15411   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_AcrossComments;
15412 
15413   Alignment.MaxEmptyLinesToKeep = 10;
15414   /* Test alignment across empty lines */
15415   EXPECT_EQ("int a = 5;\n"
15416             "\n"
15417             "int oneTwoThree = 123;",
15418             format("int a       = 5;\n"
15419                    "\n"
15420                    "int oneTwoThree= 123;",
15421                    Alignment));
15422   EXPECT_EQ("int a   = 5;\n"
15423             "int one = 1;\n"
15424             "\n"
15425             "int oneTwoThree = 123;",
15426             format("int a = 5;\n"
15427                    "int one = 1;\n"
15428                    "\n"
15429                    "int oneTwoThree = 123;",
15430                    Alignment));
15431 
15432   /* Test across comments */
15433   EXPECT_EQ("int a           = 5;\n"
15434             "/* block comment */\n"
15435             "int oneTwoThree = 123;",
15436             format("int a = 5;\n"
15437                    "/* block comment */\n"
15438                    "int oneTwoThree=123;",
15439                    Alignment));
15440 
15441   EXPECT_EQ("int a           = 5;\n"
15442             "// line comment\n"
15443             "int oneTwoThree = 123;",
15444             format("int a = 5;\n"
15445                    "// line comment\n"
15446                    "int oneTwoThree=123;",
15447                    Alignment));
15448 
15449   EXPECT_EQ("int a           = 5;\n"
15450             "/*\n"
15451             " * multi-line block comment\n"
15452             " */\n"
15453             "int oneTwoThree = 123;",
15454             format("int a = 5;\n"
15455                    "/*\n"
15456                    " * multi-line block comment\n"
15457                    " */\n"
15458                    "int oneTwoThree=123;",
15459                    Alignment));
15460 
15461   EXPECT_EQ("int a           = 5;\n"
15462             "//\n"
15463             "// multi-line line comment\n"
15464             "//\n"
15465             "int oneTwoThree = 123;",
15466             format("int a = 5;\n"
15467                    "//\n"
15468                    "// multi-line line comment\n"
15469                    "//\n"
15470                    "int oneTwoThree=123;",
15471                    Alignment));
15472 
15473   /* Test across comments and newlines */
15474   EXPECT_EQ("int a = 5;\n"
15475             "\n"
15476             "/* block comment */\n"
15477             "int oneTwoThree = 123;",
15478             format("int a = 5;\n"
15479                    "\n"
15480                    "/* block comment */\n"
15481                    "int oneTwoThree=123;",
15482                    Alignment));
15483 
15484   EXPECT_EQ("int a = 5;\n"
15485             "\n"
15486             "// line comment\n"
15487             "int oneTwoThree = 123;",
15488             format("int a = 5;\n"
15489                    "\n"
15490                    "// line comment\n"
15491                    "int oneTwoThree=123;",
15492                    Alignment));
15493 }
15494 
15495 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLinesAndComments) {
15496   FormatStyle Alignment = getLLVMStyle();
15497   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
15498   Alignment.AlignConsecutiveAssignments =
15499       FormatStyle::ACS_AcrossEmptyLinesAndComments;
15500   verifyFormat("int a           = 5;\n"
15501                "int oneTwoThree = 123;",
15502                Alignment);
15503   verifyFormat("int a           = method();\n"
15504                "int oneTwoThree = 133;",
15505                Alignment);
15506   verifyFormat("a &= 5;\n"
15507                "bcd *= 5;\n"
15508                "ghtyf += 5;\n"
15509                "dvfvdb -= 5;\n"
15510                "a /= 5;\n"
15511                "vdsvsv %= 5;\n"
15512                "sfdbddfbdfbb ^= 5;\n"
15513                "dvsdsv |= 5;\n"
15514                "int dsvvdvsdvvv = 123;",
15515                Alignment);
15516   verifyFormat("int i = 1, j = 10;\n"
15517                "something = 2000;",
15518                Alignment);
15519   verifyFormat("something = 2000;\n"
15520                "int i = 1, j = 10;\n",
15521                Alignment);
15522   verifyFormat("something = 2000;\n"
15523                "another   = 911;\n"
15524                "int i = 1, j = 10;\n"
15525                "oneMore = 1;\n"
15526                "i       = 2;",
15527                Alignment);
15528   verifyFormat("int a   = 5;\n"
15529                "int one = 1;\n"
15530                "method();\n"
15531                "int oneTwoThree = 123;\n"
15532                "int oneTwo      = 12;",
15533                Alignment);
15534   verifyFormat("int oneTwoThree = 123;\n"
15535                "int oneTwo      = 12;\n"
15536                "method();\n",
15537                Alignment);
15538   verifyFormat("int oneTwoThree = 123; // comment\n"
15539                "int oneTwo      = 12;  // comment",
15540                Alignment);
15541 
15542   // Bug 25167
15543   /* Uncomment when fixed
15544     verifyFormat("#if A\n"
15545                  "#else\n"
15546                  "int aaaaaaaa = 12;\n"
15547                  "#endif\n"
15548                  "#if B\n"
15549                  "#else\n"
15550                  "int a = 12;\n"
15551                  "#endif\n",
15552                  Alignment);
15553     verifyFormat("enum foo {\n"
15554                  "#if A\n"
15555                  "#else\n"
15556                  "  aaaaaaaa = 12;\n"
15557                  "#endif\n"
15558                  "#if B\n"
15559                  "#else\n"
15560                  "  a = 12;\n"
15561                  "#endif\n"
15562                  "};\n",
15563                  Alignment);
15564   */
15565 
15566   Alignment.MaxEmptyLinesToKeep = 10;
15567   /* Test alignment across empty lines */
15568   EXPECT_EQ("int a           = 5;\n"
15569             "\n"
15570             "int oneTwoThree = 123;",
15571             format("int a       = 5;\n"
15572                    "\n"
15573                    "int oneTwoThree= 123;",
15574                    Alignment));
15575   EXPECT_EQ("int a           = 5;\n"
15576             "int one         = 1;\n"
15577             "\n"
15578             "int oneTwoThree = 123;",
15579             format("int a = 5;\n"
15580                    "int one = 1;\n"
15581                    "\n"
15582                    "int oneTwoThree = 123;",
15583                    Alignment));
15584   EXPECT_EQ("int a           = 5;\n"
15585             "int one         = 1;\n"
15586             "\n"
15587             "int oneTwoThree = 123;\n"
15588             "int oneTwo      = 12;",
15589             format("int a = 5;\n"
15590                    "int one = 1;\n"
15591                    "\n"
15592                    "int oneTwoThree = 123;\n"
15593                    "int oneTwo = 12;",
15594                    Alignment));
15595 
15596   /* Test across comments */
15597   EXPECT_EQ("int a           = 5;\n"
15598             "/* block comment */\n"
15599             "int oneTwoThree = 123;",
15600             format("int a = 5;\n"
15601                    "/* block comment */\n"
15602                    "int oneTwoThree=123;",
15603                    Alignment));
15604 
15605   EXPECT_EQ("int a           = 5;\n"
15606             "// line comment\n"
15607             "int oneTwoThree = 123;",
15608             format("int a = 5;\n"
15609                    "// line comment\n"
15610                    "int oneTwoThree=123;",
15611                    Alignment));
15612 
15613   /* Test across comments and newlines */
15614   EXPECT_EQ("int a           = 5;\n"
15615             "\n"
15616             "/* block comment */\n"
15617             "int oneTwoThree = 123;",
15618             format("int a = 5;\n"
15619                    "\n"
15620                    "/* block comment */\n"
15621                    "int oneTwoThree=123;",
15622                    Alignment));
15623 
15624   EXPECT_EQ("int a           = 5;\n"
15625             "\n"
15626             "// line comment\n"
15627             "int oneTwoThree = 123;",
15628             format("int a = 5;\n"
15629                    "\n"
15630                    "// line comment\n"
15631                    "int oneTwoThree=123;",
15632                    Alignment));
15633 
15634   EXPECT_EQ("int a           = 5;\n"
15635             "//\n"
15636             "// multi-line line comment\n"
15637             "//\n"
15638             "int oneTwoThree = 123;",
15639             format("int a = 5;\n"
15640                    "//\n"
15641                    "// multi-line line comment\n"
15642                    "//\n"
15643                    "int oneTwoThree=123;",
15644                    Alignment));
15645 
15646   EXPECT_EQ("int a           = 5;\n"
15647             "/*\n"
15648             " *  multi-line block comment\n"
15649             " */\n"
15650             "int oneTwoThree = 123;",
15651             format("int a = 5;\n"
15652                    "/*\n"
15653                    " *  multi-line block comment\n"
15654                    " */\n"
15655                    "int oneTwoThree=123;",
15656                    Alignment));
15657 
15658   EXPECT_EQ("int a           = 5;\n"
15659             "\n"
15660             "/* block comment */\n"
15661             "\n"
15662             "\n"
15663             "\n"
15664             "int oneTwoThree = 123;",
15665             format("int a = 5;\n"
15666                    "\n"
15667                    "/* block comment */\n"
15668                    "\n"
15669                    "\n"
15670                    "\n"
15671                    "int oneTwoThree=123;",
15672                    Alignment));
15673 
15674   EXPECT_EQ("int a           = 5;\n"
15675             "\n"
15676             "// line comment\n"
15677             "\n"
15678             "\n"
15679             "\n"
15680             "int oneTwoThree = 123;",
15681             format("int a = 5;\n"
15682                    "\n"
15683                    "// line comment\n"
15684                    "\n"
15685                    "\n"
15686                    "\n"
15687                    "int oneTwoThree=123;",
15688                    Alignment));
15689 
15690   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
15691   verifyFormat("#define A \\\n"
15692                "  int aaaa       = 12; \\\n"
15693                "  int b          = 23; \\\n"
15694                "  int ccc        = 234; \\\n"
15695                "  int dddddddddd = 2345;",
15696                Alignment);
15697   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
15698   verifyFormat("#define A               \\\n"
15699                "  int aaaa       = 12;  \\\n"
15700                "  int b          = 23;  \\\n"
15701                "  int ccc        = 234; \\\n"
15702                "  int dddddddddd = 2345;",
15703                Alignment);
15704   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
15705   verifyFormat("#define A                                                      "
15706                "                \\\n"
15707                "  int aaaa       = 12;                                         "
15708                "                \\\n"
15709                "  int b          = 23;                                         "
15710                "                \\\n"
15711                "  int ccc        = 234;                                        "
15712                "                \\\n"
15713                "  int dddddddddd = 2345;",
15714                Alignment);
15715   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
15716                "k = 4, int l = 5,\n"
15717                "                  int m = 6) {\n"
15718                "  int j      = 10;\n"
15719                "  otherThing = 1;\n"
15720                "}",
15721                Alignment);
15722   verifyFormat("void SomeFunction(int parameter = 0) {\n"
15723                "  int i   = 1;\n"
15724                "  int j   = 2;\n"
15725                "  int big = 10000;\n"
15726                "}",
15727                Alignment);
15728   verifyFormat("class C {\n"
15729                "public:\n"
15730                "  int i            = 1;\n"
15731                "  virtual void f() = 0;\n"
15732                "};",
15733                Alignment);
15734   verifyFormat("int i = 1;\n"
15735                "if (SomeType t = getSomething()) {\n"
15736                "}\n"
15737                "int j   = 2;\n"
15738                "int big = 10000;",
15739                Alignment);
15740   verifyFormat("int j = 7;\n"
15741                "for (int k = 0; k < N; ++k) {\n"
15742                "}\n"
15743                "int j   = 2;\n"
15744                "int big = 10000;\n"
15745                "}",
15746                Alignment);
15747   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
15748   verifyFormat("int i = 1;\n"
15749                "LooooooooooongType loooooooooooooooooooooongVariable\n"
15750                "    = someLooooooooooooooooongFunction();\n"
15751                "int j = 2;",
15752                Alignment);
15753   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
15754   verifyFormat("int i = 1;\n"
15755                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
15756                "    someLooooooooooooooooongFunction();\n"
15757                "int j = 2;",
15758                Alignment);
15759 
15760   verifyFormat("auto lambda = []() {\n"
15761                "  auto i = 0;\n"
15762                "  return 0;\n"
15763                "};\n"
15764                "int i  = 0;\n"
15765                "auto v = type{\n"
15766                "    i = 1,   //\n"
15767                "    (i = 2), //\n"
15768                "    i = 3    //\n"
15769                "};",
15770                Alignment);
15771 
15772   verifyFormat(
15773       "int i      = 1;\n"
15774       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
15775       "                          loooooooooooooooooooooongParameterB);\n"
15776       "int j      = 2;",
15777       Alignment);
15778 
15779   verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
15780                "          typename B   = very_long_type_name_1,\n"
15781                "          typename T_2 = very_long_type_name_2>\n"
15782                "auto foo() {}\n",
15783                Alignment);
15784   verifyFormat("int a, b = 1;\n"
15785                "int c  = 2;\n"
15786                "int dd = 3;\n",
15787                Alignment);
15788   verifyFormat("int aa       = ((1 > 2) ? 3 : 4);\n"
15789                "float b[1][] = {{3.f}};\n",
15790                Alignment);
15791   verifyFormat("for (int i = 0; i < 1; i++)\n"
15792                "  int x = 1;\n",
15793                Alignment);
15794   verifyFormat("for (i = 0; i < 1; i++)\n"
15795                "  x = 1;\n"
15796                "y = 1;\n",
15797                Alignment);
15798 
15799   Alignment.ReflowComments = true;
15800   Alignment.ColumnLimit = 50;
15801   EXPECT_EQ("int x   = 0;\n"
15802             "int yy  = 1; /// specificlennospace\n"
15803             "int zzz = 2;\n",
15804             format("int x   = 0;\n"
15805                    "int yy  = 1; ///specificlennospace\n"
15806                    "int zzz = 2;\n",
15807                    Alignment));
15808 }
15809 
15810 TEST_F(FormatTest, AlignConsecutiveAssignments) {
15811   FormatStyle Alignment = getLLVMStyle();
15812   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
15813   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
15814   verifyFormat("int a = 5;\n"
15815                "int oneTwoThree = 123;",
15816                Alignment);
15817   verifyFormat("int a = 5;\n"
15818                "int oneTwoThree = 123;",
15819                Alignment);
15820 
15821   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
15822   verifyFormat("int a           = 5;\n"
15823                "int oneTwoThree = 123;",
15824                Alignment);
15825   verifyFormat("int a           = method();\n"
15826                "int oneTwoThree = 133;",
15827                Alignment);
15828   verifyFormat("a &= 5;\n"
15829                "bcd *= 5;\n"
15830                "ghtyf += 5;\n"
15831                "dvfvdb -= 5;\n"
15832                "a /= 5;\n"
15833                "vdsvsv %= 5;\n"
15834                "sfdbddfbdfbb ^= 5;\n"
15835                "dvsdsv |= 5;\n"
15836                "int dsvvdvsdvvv = 123;",
15837                Alignment);
15838   verifyFormat("int i = 1, j = 10;\n"
15839                "something = 2000;",
15840                Alignment);
15841   verifyFormat("something = 2000;\n"
15842                "int i = 1, j = 10;\n",
15843                Alignment);
15844   verifyFormat("something = 2000;\n"
15845                "another   = 911;\n"
15846                "int i = 1, j = 10;\n"
15847                "oneMore = 1;\n"
15848                "i       = 2;",
15849                Alignment);
15850   verifyFormat("int a   = 5;\n"
15851                "int one = 1;\n"
15852                "method();\n"
15853                "int oneTwoThree = 123;\n"
15854                "int oneTwo      = 12;",
15855                Alignment);
15856   verifyFormat("int oneTwoThree = 123;\n"
15857                "int oneTwo      = 12;\n"
15858                "method();\n",
15859                Alignment);
15860   verifyFormat("int oneTwoThree = 123; // comment\n"
15861                "int oneTwo      = 12;  // comment",
15862                Alignment);
15863 
15864   // Bug 25167
15865   /* Uncomment when fixed
15866     verifyFormat("#if A\n"
15867                  "#else\n"
15868                  "int aaaaaaaa = 12;\n"
15869                  "#endif\n"
15870                  "#if B\n"
15871                  "#else\n"
15872                  "int a = 12;\n"
15873                  "#endif\n",
15874                  Alignment);
15875     verifyFormat("enum foo {\n"
15876                  "#if A\n"
15877                  "#else\n"
15878                  "  aaaaaaaa = 12;\n"
15879                  "#endif\n"
15880                  "#if B\n"
15881                  "#else\n"
15882                  "  a = 12;\n"
15883                  "#endif\n"
15884                  "};\n",
15885                  Alignment);
15886   */
15887 
15888   EXPECT_EQ("int a = 5;\n"
15889             "\n"
15890             "int oneTwoThree = 123;",
15891             format("int a       = 5;\n"
15892                    "\n"
15893                    "int oneTwoThree= 123;",
15894                    Alignment));
15895   EXPECT_EQ("int a   = 5;\n"
15896             "int one = 1;\n"
15897             "\n"
15898             "int oneTwoThree = 123;",
15899             format("int a = 5;\n"
15900                    "int one = 1;\n"
15901                    "\n"
15902                    "int oneTwoThree = 123;",
15903                    Alignment));
15904   EXPECT_EQ("int a   = 5;\n"
15905             "int one = 1;\n"
15906             "\n"
15907             "int oneTwoThree = 123;\n"
15908             "int oneTwo      = 12;",
15909             format("int a = 5;\n"
15910                    "int one = 1;\n"
15911                    "\n"
15912                    "int oneTwoThree = 123;\n"
15913                    "int oneTwo = 12;",
15914                    Alignment));
15915   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
15916   verifyFormat("#define A \\\n"
15917                "  int aaaa       = 12; \\\n"
15918                "  int b          = 23; \\\n"
15919                "  int ccc        = 234; \\\n"
15920                "  int dddddddddd = 2345;",
15921                Alignment);
15922   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
15923   verifyFormat("#define A               \\\n"
15924                "  int aaaa       = 12;  \\\n"
15925                "  int b          = 23;  \\\n"
15926                "  int ccc        = 234; \\\n"
15927                "  int dddddddddd = 2345;",
15928                Alignment);
15929   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
15930   verifyFormat("#define A                                                      "
15931                "                \\\n"
15932                "  int aaaa       = 12;                                         "
15933                "                \\\n"
15934                "  int b          = 23;                                         "
15935                "                \\\n"
15936                "  int ccc        = 234;                                        "
15937                "                \\\n"
15938                "  int dddddddddd = 2345;",
15939                Alignment);
15940   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
15941                "k = 4, int l = 5,\n"
15942                "                  int m = 6) {\n"
15943                "  int j      = 10;\n"
15944                "  otherThing = 1;\n"
15945                "}",
15946                Alignment);
15947   verifyFormat("void SomeFunction(int parameter = 0) {\n"
15948                "  int i   = 1;\n"
15949                "  int j   = 2;\n"
15950                "  int big = 10000;\n"
15951                "}",
15952                Alignment);
15953   verifyFormat("class C {\n"
15954                "public:\n"
15955                "  int i            = 1;\n"
15956                "  virtual void f() = 0;\n"
15957                "};",
15958                Alignment);
15959   verifyFormat("int i = 1;\n"
15960                "if (SomeType t = getSomething()) {\n"
15961                "}\n"
15962                "int j   = 2;\n"
15963                "int big = 10000;",
15964                Alignment);
15965   verifyFormat("int j = 7;\n"
15966                "for (int k = 0; k < N; ++k) {\n"
15967                "}\n"
15968                "int j   = 2;\n"
15969                "int big = 10000;\n"
15970                "}",
15971                Alignment);
15972   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
15973   verifyFormat("int i = 1;\n"
15974                "LooooooooooongType loooooooooooooooooooooongVariable\n"
15975                "    = someLooooooooooooooooongFunction();\n"
15976                "int j = 2;",
15977                Alignment);
15978   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
15979   verifyFormat("int i = 1;\n"
15980                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
15981                "    someLooooooooooooooooongFunction();\n"
15982                "int j = 2;",
15983                Alignment);
15984 
15985   verifyFormat("auto lambda = []() {\n"
15986                "  auto i = 0;\n"
15987                "  return 0;\n"
15988                "};\n"
15989                "int i  = 0;\n"
15990                "auto v = type{\n"
15991                "    i = 1,   //\n"
15992                "    (i = 2), //\n"
15993                "    i = 3    //\n"
15994                "};",
15995                Alignment);
15996 
15997   verifyFormat(
15998       "int i      = 1;\n"
15999       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
16000       "                          loooooooooooooooooooooongParameterB);\n"
16001       "int j      = 2;",
16002       Alignment);
16003 
16004   verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
16005                "          typename B   = very_long_type_name_1,\n"
16006                "          typename T_2 = very_long_type_name_2>\n"
16007                "auto foo() {}\n",
16008                Alignment);
16009   verifyFormat("int a, b = 1;\n"
16010                "int c  = 2;\n"
16011                "int dd = 3;\n",
16012                Alignment);
16013   verifyFormat("int aa       = ((1 > 2) ? 3 : 4);\n"
16014                "float b[1][] = {{3.f}};\n",
16015                Alignment);
16016   verifyFormat("for (int i = 0; i < 1; i++)\n"
16017                "  int x = 1;\n",
16018                Alignment);
16019   verifyFormat("for (i = 0; i < 1; i++)\n"
16020                "  x = 1;\n"
16021                "y = 1;\n",
16022                Alignment);
16023 
16024   Alignment.ReflowComments = true;
16025   Alignment.ColumnLimit = 50;
16026   EXPECT_EQ("int x   = 0;\n"
16027             "int yy  = 1; /// specificlennospace\n"
16028             "int zzz = 2;\n",
16029             format("int x   = 0;\n"
16030                    "int yy  = 1; ///specificlennospace\n"
16031                    "int zzz = 2;\n",
16032                    Alignment));
16033 }
16034 
16035 TEST_F(FormatTest, AlignConsecutiveBitFields) {
16036   FormatStyle Alignment = getLLVMStyle();
16037   Alignment.AlignConsecutiveBitFields = FormatStyle::ACS_Consecutive;
16038   verifyFormat("int const a     : 5;\n"
16039                "int oneTwoThree : 23;",
16040                Alignment);
16041 
16042   // Initializers are allowed starting with c++2a
16043   verifyFormat("int const a     : 5 = 1;\n"
16044                "int oneTwoThree : 23 = 0;",
16045                Alignment);
16046 
16047   Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
16048   verifyFormat("int const a           : 5;\n"
16049                "int       oneTwoThree : 23;",
16050                Alignment);
16051 
16052   verifyFormat("int const a           : 5;  // comment\n"
16053                "int       oneTwoThree : 23; // comment",
16054                Alignment);
16055 
16056   verifyFormat("int const a           : 5 = 1;\n"
16057                "int       oneTwoThree : 23 = 0;",
16058                Alignment);
16059 
16060   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16061   verifyFormat("int const a           : 5  = 1;\n"
16062                "int       oneTwoThree : 23 = 0;",
16063                Alignment);
16064   verifyFormat("int const a           : 5  = {1};\n"
16065                "int       oneTwoThree : 23 = 0;",
16066                Alignment);
16067 
16068   Alignment.BitFieldColonSpacing = FormatStyle::BFCS_None;
16069   verifyFormat("int const a          :5;\n"
16070                "int       oneTwoThree:23;",
16071                Alignment);
16072 
16073   Alignment.BitFieldColonSpacing = FormatStyle::BFCS_Before;
16074   verifyFormat("int const a           :5;\n"
16075                "int       oneTwoThree :23;",
16076                Alignment);
16077 
16078   Alignment.BitFieldColonSpacing = FormatStyle::BFCS_After;
16079   verifyFormat("int const a          : 5;\n"
16080                "int       oneTwoThree: 23;",
16081                Alignment);
16082 
16083   // Known limitations: ':' is only recognized as a bitfield colon when
16084   // followed by a number.
16085   /*
16086   verifyFormat("int oneTwoThree : SOME_CONSTANT;\n"
16087                "int a           : 5;",
16088                Alignment);
16089   */
16090 }
16091 
16092 TEST_F(FormatTest, AlignConsecutiveDeclarations) {
16093   FormatStyle Alignment = getLLVMStyle();
16094   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
16095   Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_None;
16096   Alignment.PointerAlignment = FormatStyle::PAS_Right;
16097   verifyFormat("float const a = 5;\n"
16098                "int oneTwoThree = 123;",
16099                Alignment);
16100   verifyFormat("int a = 5;\n"
16101                "float const oneTwoThree = 123;",
16102                Alignment);
16103 
16104   Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
16105   verifyFormat("float const a = 5;\n"
16106                "int         oneTwoThree = 123;",
16107                Alignment);
16108   verifyFormat("int         a = method();\n"
16109                "float const oneTwoThree = 133;",
16110                Alignment);
16111   verifyFormat("int i = 1, j = 10;\n"
16112                "something = 2000;",
16113                Alignment);
16114   verifyFormat("something = 2000;\n"
16115                "int i = 1, j = 10;\n",
16116                Alignment);
16117   verifyFormat("float      something = 2000;\n"
16118                "double     another = 911;\n"
16119                "int        i = 1, j = 10;\n"
16120                "const int *oneMore = 1;\n"
16121                "unsigned   i = 2;",
16122                Alignment);
16123   verifyFormat("float a = 5;\n"
16124                "int   one = 1;\n"
16125                "method();\n"
16126                "const double       oneTwoThree = 123;\n"
16127                "const unsigned int oneTwo = 12;",
16128                Alignment);
16129   verifyFormat("int      oneTwoThree{0}; // comment\n"
16130                "unsigned oneTwo;         // comment",
16131                Alignment);
16132   verifyFormat("unsigned int       *a;\n"
16133                "int                *b;\n"
16134                "unsigned int Const *c;\n"
16135                "unsigned int const *d;\n"
16136                "unsigned int Const &e;\n"
16137                "unsigned int const &f;",
16138                Alignment);
16139   verifyFormat("Const unsigned int *c;\n"
16140                "const unsigned int *d;\n"
16141                "Const unsigned int &e;\n"
16142                "const unsigned int &f;\n"
16143                "const unsigned      g;\n"
16144                "Const unsigned      h;",
16145                Alignment);
16146   EXPECT_EQ("float const a = 5;\n"
16147             "\n"
16148             "int oneTwoThree = 123;",
16149             format("float const   a = 5;\n"
16150                    "\n"
16151                    "int           oneTwoThree= 123;",
16152                    Alignment));
16153   EXPECT_EQ("float a = 5;\n"
16154             "int   one = 1;\n"
16155             "\n"
16156             "unsigned oneTwoThree = 123;",
16157             format("float    a = 5;\n"
16158                    "int      one = 1;\n"
16159                    "\n"
16160                    "unsigned oneTwoThree = 123;",
16161                    Alignment));
16162   EXPECT_EQ("float a = 5;\n"
16163             "int   one = 1;\n"
16164             "\n"
16165             "unsigned oneTwoThree = 123;\n"
16166             "int      oneTwo = 12;",
16167             format("float    a = 5;\n"
16168                    "int one = 1;\n"
16169                    "\n"
16170                    "unsigned oneTwoThree = 123;\n"
16171                    "int oneTwo = 12;",
16172                    Alignment));
16173   // Function prototype alignment
16174   verifyFormat("int    a();\n"
16175                "double b();",
16176                Alignment);
16177   verifyFormat("int    a(int x);\n"
16178                "double b();",
16179                Alignment);
16180   unsigned OldColumnLimit = Alignment.ColumnLimit;
16181   // We need to set ColumnLimit to zero, in order to stress nested alignments,
16182   // otherwise the function parameters will be re-flowed onto a single line.
16183   Alignment.ColumnLimit = 0;
16184   EXPECT_EQ("int    a(int   x,\n"
16185             "         float y);\n"
16186             "double b(int    x,\n"
16187             "         double y);",
16188             format("int a(int x,\n"
16189                    " float y);\n"
16190                    "double b(int x,\n"
16191                    " double y);",
16192                    Alignment));
16193   // This ensures that function parameters of function declarations are
16194   // correctly indented when their owning functions are indented.
16195   // The failure case here is for 'double y' to not be indented enough.
16196   EXPECT_EQ("double a(int x);\n"
16197             "int    b(int    y,\n"
16198             "         double z);",
16199             format("double a(int x);\n"
16200                    "int b(int y,\n"
16201                    " double z);",
16202                    Alignment));
16203   // Set ColumnLimit low so that we induce wrapping immediately after
16204   // the function name and opening paren.
16205   Alignment.ColumnLimit = 13;
16206   verifyFormat("int function(\n"
16207                "    int  x,\n"
16208                "    bool y);",
16209                Alignment);
16210   Alignment.ColumnLimit = OldColumnLimit;
16211   // Ensure function pointers don't screw up recursive alignment
16212   verifyFormat("int    a(int x, void (*fp)(int y));\n"
16213                "double b();",
16214                Alignment);
16215   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16216   // Ensure recursive alignment is broken by function braces, so that the
16217   // "a = 1" does not align with subsequent assignments inside the function
16218   // body.
16219   verifyFormat("int func(int a = 1) {\n"
16220                "  int b  = 2;\n"
16221                "  int cc = 3;\n"
16222                "}",
16223                Alignment);
16224   verifyFormat("float      something = 2000;\n"
16225                "double     another   = 911;\n"
16226                "int        i = 1, j = 10;\n"
16227                "const int *oneMore = 1;\n"
16228                "unsigned   i       = 2;",
16229                Alignment);
16230   verifyFormat("int      oneTwoThree = {0}; // comment\n"
16231                "unsigned oneTwo      = 0;   // comment",
16232                Alignment);
16233   // Make sure that scope is correctly tracked, in the absence of braces
16234   verifyFormat("for (int i = 0; i < n; i++)\n"
16235                "  j = i;\n"
16236                "double x = 1;\n",
16237                Alignment);
16238   verifyFormat("if (int i = 0)\n"
16239                "  j = i;\n"
16240                "double x = 1;\n",
16241                Alignment);
16242   // Ensure operator[] and operator() are comprehended
16243   verifyFormat("struct test {\n"
16244                "  long long int foo();\n"
16245                "  int           operator[](int a);\n"
16246                "  double        bar();\n"
16247                "};\n",
16248                Alignment);
16249   verifyFormat("struct test {\n"
16250                "  long long int foo();\n"
16251                "  int           operator()(int a);\n"
16252                "  double        bar();\n"
16253                "};\n",
16254                Alignment);
16255 
16256   // PAS_Right
16257   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
16258             "  int const i   = 1;\n"
16259             "  int      *j   = 2;\n"
16260             "  int       big = 10000;\n"
16261             "\n"
16262             "  unsigned oneTwoThree = 123;\n"
16263             "  int      oneTwo      = 12;\n"
16264             "  method();\n"
16265             "  float k  = 2;\n"
16266             "  int   ll = 10000;\n"
16267             "}",
16268             format("void SomeFunction(int parameter= 0) {\n"
16269                    " int const  i= 1;\n"
16270                    "  int *j=2;\n"
16271                    " int big  =  10000;\n"
16272                    "\n"
16273                    "unsigned oneTwoThree  =123;\n"
16274                    "int oneTwo = 12;\n"
16275                    "  method();\n"
16276                    "float k= 2;\n"
16277                    "int ll=10000;\n"
16278                    "}",
16279                    Alignment));
16280   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
16281             "  int const i   = 1;\n"
16282             "  int     **j   = 2, ***k;\n"
16283             "  int      &k   = i;\n"
16284             "  int     &&l   = i + j;\n"
16285             "  int       big = 10000;\n"
16286             "\n"
16287             "  unsigned oneTwoThree = 123;\n"
16288             "  int      oneTwo      = 12;\n"
16289             "  method();\n"
16290             "  float k  = 2;\n"
16291             "  int   ll = 10000;\n"
16292             "}",
16293             format("void SomeFunction(int parameter= 0) {\n"
16294                    " int const  i= 1;\n"
16295                    "  int **j=2,***k;\n"
16296                    "int &k=i;\n"
16297                    "int &&l=i+j;\n"
16298                    " int big  =  10000;\n"
16299                    "\n"
16300                    "unsigned oneTwoThree  =123;\n"
16301                    "int oneTwo = 12;\n"
16302                    "  method();\n"
16303                    "float k= 2;\n"
16304                    "int ll=10000;\n"
16305                    "}",
16306                    Alignment));
16307   // variables are aligned at their name, pointers are at the right most
16308   // position
16309   verifyFormat("int   *a;\n"
16310                "int  **b;\n"
16311                "int ***c;\n"
16312                "int    foobar;\n",
16313                Alignment);
16314 
16315   // PAS_Left
16316   FormatStyle AlignmentLeft = Alignment;
16317   AlignmentLeft.PointerAlignment = FormatStyle::PAS_Left;
16318   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
16319             "  int const i   = 1;\n"
16320             "  int*      j   = 2;\n"
16321             "  int       big = 10000;\n"
16322             "\n"
16323             "  unsigned oneTwoThree = 123;\n"
16324             "  int      oneTwo      = 12;\n"
16325             "  method();\n"
16326             "  float k  = 2;\n"
16327             "  int   ll = 10000;\n"
16328             "}",
16329             format("void SomeFunction(int parameter= 0) {\n"
16330                    " int const  i= 1;\n"
16331                    "  int *j=2;\n"
16332                    " int big  =  10000;\n"
16333                    "\n"
16334                    "unsigned oneTwoThree  =123;\n"
16335                    "int oneTwo = 12;\n"
16336                    "  method();\n"
16337                    "float k= 2;\n"
16338                    "int ll=10000;\n"
16339                    "}",
16340                    AlignmentLeft));
16341   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
16342             "  int const i   = 1;\n"
16343             "  int**     j   = 2;\n"
16344             "  int&      k   = i;\n"
16345             "  int&&     l   = i + j;\n"
16346             "  int       big = 10000;\n"
16347             "\n"
16348             "  unsigned oneTwoThree = 123;\n"
16349             "  int      oneTwo      = 12;\n"
16350             "  method();\n"
16351             "  float k  = 2;\n"
16352             "  int   ll = 10000;\n"
16353             "}",
16354             format("void SomeFunction(int parameter= 0) {\n"
16355                    " int const  i= 1;\n"
16356                    "  int **j=2;\n"
16357                    "int &k=i;\n"
16358                    "int &&l=i+j;\n"
16359                    " int big  =  10000;\n"
16360                    "\n"
16361                    "unsigned oneTwoThree  =123;\n"
16362                    "int oneTwo = 12;\n"
16363                    "  method();\n"
16364                    "float k= 2;\n"
16365                    "int ll=10000;\n"
16366                    "}",
16367                    AlignmentLeft));
16368   // variables are aligned at their name, pointers are at the left most position
16369   verifyFormat("int*   a;\n"
16370                "int**  b;\n"
16371                "int*** c;\n"
16372                "int    foobar;\n",
16373                AlignmentLeft);
16374 
16375   // PAS_Middle
16376   FormatStyle AlignmentMiddle = Alignment;
16377   AlignmentMiddle.PointerAlignment = FormatStyle::PAS_Middle;
16378   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
16379             "  int const i   = 1;\n"
16380             "  int *     j   = 2;\n"
16381             "  int       big = 10000;\n"
16382             "\n"
16383             "  unsigned oneTwoThree = 123;\n"
16384             "  int      oneTwo      = 12;\n"
16385             "  method();\n"
16386             "  float k  = 2;\n"
16387             "  int   ll = 10000;\n"
16388             "}",
16389             format("void SomeFunction(int parameter= 0) {\n"
16390                    " int const  i= 1;\n"
16391                    "  int *j=2;\n"
16392                    " int big  =  10000;\n"
16393                    "\n"
16394                    "unsigned oneTwoThree  =123;\n"
16395                    "int oneTwo = 12;\n"
16396                    "  method();\n"
16397                    "float k= 2;\n"
16398                    "int ll=10000;\n"
16399                    "}",
16400                    AlignmentMiddle));
16401   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
16402             "  int const i   = 1;\n"
16403             "  int **    j   = 2, ***k;\n"
16404             "  int &     k   = i;\n"
16405             "  int &&    l   = i + j;\n"
16406             "  int       big = 10000;\n"
16407             "\n"
16408             "  unsigned oneTwoThree = 123;\n"
16409             "  int      oneTwo      = 12;\n"
16410             "  method();\n"
16411             "  float k  = 2;\n"
16412             "  int   ll = 10000;\n"
16413             "}",
16414             format("void SomeFunction(int parameter= 0) {\n"
16415                    " int const  i= 1;\n"
16416                    "  int **j=2,***k;\n"
16417                    "int &k=i;\n"
16418                    "int &&l=i+j;\n"
16419                    " int big  =  10000;\n"
16420                    "\n"
16421                    "unsigned oneTwoThree  =123;\n"
16422                    "int oneTwo = 12;\n"
16423                    "  method();\n"
16424                    "float k= 2;\n"
16425                    "int ll=10000;\n"
16426                    "}",
16427                    AlignmentMiddle));
16428   // variables are aligned at their name, pointers are in the middle
16429   verifyFormat("int *   a;\n"
16430                "int *   b;\n"
16431                "int *** c;\n"
16432                "int     foobar;\n",
16433                AlignmentMiddle);
16434 
16435   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
16436   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
16437   verifyFormat("#define A \\\n"
16438                "  int       aaaa = 12; \\\n"
16439                "  float     b = 23; \\\n"
16440                "  const int ccc = 234; \\\n"
16441                "  unsigned  dddddddddd = 2345;",
16442                Alignment);
16443   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
16444   verifyFormat("#define A              \\\n"
16445                "  int       aaaa = 12; \\\n"
16446                "  float     b = 23;    \\\n"
16447                "  const int ccc = 234; \\\n"
16448                "  unsigned  dddddddddd = 2345;",
16449                Alignment);
16450   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
16451   Alignment.ColumnLimit = 30;
16452   verifyFormat("#define A                    \\\n"
16453                "  int       aaaa = 12;       \\\n"
16454                "  float     b = 23;          \\\n"
16455                "  const int ccc = 234;       \\\n"
16456                "  int       dddddddddd = 2345;",
16457                Alignment);
16458   Alignment.ColumnLimit = 80;
16459   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
16460                "k = 4, int l = 5,\n"
16461                "                  int m = 6) {\n"
16462                "  const int j = 10;\n"
16463                "  otherThing = 1;\n"
16464                "}",
16465                Alignment);
16466   verifyFormat("void SomeFunction(int parameter = 0) {\n"
16467                "  int const i = 1;\n"
16468                "  int      *j = 2;\n"
16469                "  int       big = 10000;\n"
16470                "}",
16471                Alignment);
16472   verifyFormat("class C {\n"
16473                "public:\n"
16474                "  int          i = 1;\n"
16475                "  virtual void f() = 0;\n"
16476                "};",
16477                Alignment);
16478   verifyFormat("float i = 1;\n"
16479                "if (SomeType t = getSomething()) {\n"
16480                "}\n"
16481                "const unsigned j = 2;\n"
16482                "int            big = 10000;",
16483                Alignment);
16484   verifyFormat("float j = 7;\n"
16485                "for (int k = 0; k < N; ++k) {\n"
16486                "}\n"
16487                "unsigned j = 2;\n"
16488                "int      big = 10000;\n"
16489                "}",
16490                Alignment);
16491   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
16492   verifyFormat("float              i = 1;\n"
16493                "LooooooooooongType loooooooooooooooooooooongVariable\n"
16494                "    = someLooooooooooooooooongFunction();\n"
16495                "int j = 2;",
16496                Alignment);
16497   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
16498   verifyFormat("int                i = 1;\n"
16499                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
16500                "    someLooooooooooooooooongFunction();\n"
16501                "int j = 2;",
16502                Alignment);
16503 
16504   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16505   verifyFormat("auto lambda = []() {\n"
16506                "  auto  ii = 0;\n"
16507                "  float j  = 0;\n"
16508                "  return 0;\n"
16509                "};\n"
16510                "int   i  = 0;\n"
16511                "float i2 = 0;\n"
16512                "auto  v  = type{\n"
16513                "    i = 1,   //\n"
16514                "    (i = 2), //\n"
16515                "    i = 3    //\n"
16516                "};",
16517                Alignment);
16518   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
16519 
16520   verifyFormat(
16521       "int      i = 1;\n"
16522       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
16523       "                          loooooooooooooooooooooongParameterB);\n"
16524       "int      j = 2;",
16525       Alignment);
16526 
16527   // Test interactions with ColumnLimit and AlignConsecutiveAssignments:
16528   // We expect declarations and assignments to align, as long as it doesn't
16529   // exceed the column limit, starting a new alignment sequence whenever it
16530   // happens.
16531   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16532   Alignment.ColumnLimit = 30;
16533   verifyFormat("float    ii              = 1;\n"
16534                "unsigned j               = 2;\n"
16535                "int someVerylongVariable = 1;\n"
16536                "AnotherLongType  ll = 123456;\n"
16537                "VeryVeryLongType k  = 2;\n"
16538                "int              myvar = 1;",
16539                Alignment);
16540   Alignment.ColumnLimit = 80;
16541   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
16542 
16543   verifyFormat(
16544       "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n"
16545       "          typename LongType, typename B>\n"
16546       "auto foo() {}\n",
16547       Alignment);
16548   verifyFormat("float a, b = 1;\n"
16549                "int   c = 2;\n"
16550                "int   dd = 3;\n",
16551                Alignment);
16552   verifyFormat("int   aa = ((1 > 2) ? 3 : 4);\n"
16553                "float b[1][] = {{3.f}};\n",
16554                Alignment);
16555   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16556   verifyFormat("float a, b = 1;\n"
16557                "int   c  = 2;\n"
16558                "int   dd = 3;\n",
16559                Alignment);
16560   verifyFormat("int   aa     = ((1 > 2) ? 3 : 4);\n"
16561                "float b[1][] = {{3.f}};\n",
16562                Alignment);
16563   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
16564 
16565   Alignment.ColumnLimit = 30;
16566   Alignment.BinPackParameters = false;
16567   verifyFormat("void foo(float     a,\n"
16568                "         float     b,\n"
16569                "         int       c,\n"
16570                "         uint32_t *d) {\n"
16571                "  int   *e = 0;\n"
16572                "  float  f = 0;\n"
16573                "  double g = 0;\n"
16574                "}\n"
16575                "void bar(ino_t     a,\n"
16576                "         int       b,\n"
16577                "         uint32_t *c,\n"
16578                "         bool      d) {}\n",
16579                Alignment);
16580   Alignment.BinPackParameters = true;
16581   Alignment.ColumnLimit = 80;
16582 
16583   // Bug 33507
16584   Alignment.PointerAlignment = FormatStyle::PAS_Middle;
16585   verifyFormat(
16586       "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n"
16587       "  static const Version verVs2017;\n"
16588       "  return true;\n"
16589       "});\n",
16590       Alignment);
16591   Alignment.PointerAlignment = FormatStyle::PAS_Right;
16592 
16593   // See llvm.org/PR35641
16594   Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
16595   verifyFormat("int func() { //\n"
16596                "  int      b;\n"
16597                "  unsigned c;\n"
16598                "}",
16599                Alignment);
16600 
16601   // See PR37175
16602   FormatStyle Style = getMozillaStyle();
16603   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
16604   EXPECT_EQ("DECOR1 /**/ int8_t /**/ DECOR2 /**/\n"
16605             "foo(int a);",
16606             format("DECOR1 /**/ int8_t /**/ DECOR2 /**/ foo (int a);", Style));
16607 
16608   Alignment.PointerAlignment = FormatStyle::PAS_Left;
16609   verifyFormat("unsigned int*       a;\n"
16610                "int*                b;\n"
16611                "unsigned int Const* c;\n"
16612                "unsigned int const* d;\n"
16613                "unsigned int Const& e;\n"
16614                "unsigned int const& f;",
16615                Alignment);
16616   verifyFormat("Const unsigned int* c;\n"
16617                "const unsigned int* d;\n"
16618                "Const unsigned int& e;\n"
16619                "const unsigned int& f;\n"
16620                "const unsigned      g;\n"
16621                "Const unsigned      h;",
16622                Alignment);
16623 
16624   Alignment.PointerAlignment = FormatStyle::PAS_Middle;
16625   verifyFormat("unsigned int *       a;\n"
16626                "int *                b;\n"
16627                "unsigned int Const * c;\n"
16628                "unsigned int const * d;\n"
16629                "unsigned int Const & e;\n"
16630                "unsigned int const & f;",
16631                Alignment);
16632   verifyFormat("Const unsigned int * c;\n"
16633                "const unsigned int * d;\n"
16634                "Const unsigned int & e;\n"
16635                "const unsigned int & f;\n"
16636                "const unsigned       g;\n"
16637                "Const unsigned       h;",
16638                Alignment);
16639 }
16640 
16641 TEST_F(FormatTest, AlignWithLineBreaks) {
16642   auto Style = getLLVMStyleWithColumns(120);
16643 
16644   EXPECT_EQ(Style.AlignConsecutiveAssignments, FormatStyle::ACS_None);
16645   EXPECT_EQ(Style.AlignConsecutiveDeclarations, FormatStyle::ACS_None);
16646   verifyFormat("void foo() {\n"
16647                "  int myVar = 5;\n"
16648                "  double x = 3.14;\n"
16649                "  auto str = \"Hello \"\n"
16650                "             \"World\";\n"
16651                "  auto s = \"Hello \"\n"
16652                "           \"Again\";\n"
16653                "}",
16654                Style);
16655 
16656   // clang-format off
16657   verifyFormat("void foo() {\n"
16658                "  const int capacityBefore = Entries.capacity();\n"
16659                "  const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
16660                "                                            std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
16661                "  const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
16662                "                                          std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
16663                "}",
16664                Style);
16665   // clang-format on
16666 
16667   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16668   verifyFormat("void foo() {\n"
16669                "  int myVar = 5;\n"
16670                "  double x  = 3.14;\n"
16671                "  auto str  = \"Hello \"\n"
16672                "              \"World\";\n"
16673                "  auto s    = \"Hello \"\n"
16674                "              \"Again\";\n"
16675                "}",
16676                Style);
16677 
16678   // clang-format off
16679   verifyFormat("void foo() {\n"
16680                "  const int capacityBefore = Entries.capacity();\n"
16681                "  const auto newEntry      = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
16682                "                                                 std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
16683                "  const X newEntry2        = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
16684                "                                                 std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
16685                "}",
16686                Style);
16687   // clang-format on
16688 
16689   Style.AlignConsecutiveAssignments = FormatStyle::ACS_None;
16690   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
16691   verifyFormat("void foo() {\n"
16692                "  int    myVar = 5;\n"
16693                "  double x = 3.14;\n"
16694                "  auto   str = \"Hello \"\n"
16695                "               \"World\";\n"
16696                "  auto   s = \"Hello \"\n"
16697                "             \"Again\";\n"
16698                "}",
16699                Style);
16700 
16701   // clang-format off
16702   verifyFormat("void foo() {\n"
16703                "  const int  capacityBefore = Entries.capacity();\n"
16704                "  const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
16705                "                                            std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
16706                "  const X    newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
16707                "                                             std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
16708                "}",
16709                Style);
16710   // clang-format on
16711 
16712   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16713   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
16714 
16715   verifyFormat("void foo() {\n"
16716                "  int    myVar = 5;\n"
16717                "  double x     = 3.14;\n"
16718                "  auto   str   = \"Hello \"\n"
16719                "                 \"World\";\n"
16720                "  auto   s     = \"Hello \"\n"
16721                "                 \"Again\";\n"
16722                "}",
16723                Style);
16724 
16725   // clang-format off
16726   verifyFormat("void foo() {\n"
16727                "  const int  capacityBefore = Entries.capacity();\n"
16728                "  const auto newEntry       = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
16729                "                                                  std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
16730                "  const X    newEntry2      = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
16731                "                                                  std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
16732                "}",
16733                Style);
16734   // clang-format on
16735 
16736   Style = getLLVMStyleWithColumns(120);
16737   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16738   Style.ContinuationIndentWidth = 4;
16739   Style.IndentWidth = 4;
16740 
16741   // clang-format off
16742   verifyFormat("void SomeFunc() {\n"
16743                "    newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n"
16744                "                                                        seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
16745                "    newWatcher.maxAge     = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n"
16746                "                                                        seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
16747                "    newWatcher.max        = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n"
16748                "                                                        seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
16749                "}",
16750                Style);
16751   // clang-format on
16752 
16753   Style.BinPackArguments = false;
16754 
16755   // clang-format off
16756   verifyFormat("void SomeFunc() {\n"
16757                "    newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(\n"
16758                "        FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
16759                "    newWatcher.maxAge     = ToLegacyTimestamp(GetMaxAge(\n"
16760                "        FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
16761                "    newWatcher.max        = ToLegacyTimestamp(GetMaxAge(\n"
16762                "        FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
16763                "}",
16764                Style);
16765   // clang-format on
16766 }
16767 
16768 TEST_F(FormatTest, AlignWithInitializerPeriods) {
16769   auto Style = getLLVMStyleWithColumns(60);
16770 
16771   verifyFormat("void foo1(void) {\n"
16772                "  BYTE p[1] = 1;\n"
16773                "  A B = {.one_foooooooooooooooo = 2,\n"
16774                "         .two_fooooooooooooo = 3,\n"
16775                "         .three_fooooooooooooo = 4};\n"
16776                "  BYTE payload = 2;\n"
16777                "}",
16778                Style);
16779 
16780   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16781   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_None;
16782   verifyFormat("void foo2(void) {\n"
16783                "  BYTE p[1]    = 1;\n"
16784                "  A B          = {.one_foooooooooooooooo = 2,\n"
16785                "                  .two_fooooooooooooo    = 3,\n"
16786                "                  .three_fooooooooooooo  = 4};\n"
16787                "  BYTE payload = 2;\n"
16788                "}",
16789                Style);
16790 
16791   Style.AlignConsecutiveAssignments = FormatStyle::ACS_None;
16792   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
16793   verifyFormat("void foo3(void) {\n"
16794                "  BYTE p[1] = 1;\n"
16795                "  A    B = {.one_foooooooooooooooo = 2,\n"
16796                "            .two_fooooooooooooo = 3,\n"
16797                "            .three_fooooooooooooo = 4};\n"
16798                "  BYTE payload = 2;\n"
16799                "}",
16800                Style);
16801 
16802   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16803   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
16804   verifyFormat("void foo4(void) {\n"
16805                "  BYTE p[1]    = 1;\n"
16806                "  A    B       = {.one_foooooooooooooooo = 2,\n"
16807                "                  .two_fooooooooooooo    = 3,\n"
16808                "                  .three_fooooooooooooo  = 4};\n"
16809                "  BYTE payload = 2;\n"
16810                "}",
16811                Style);
16812 }
16813 
16814 TEST_F(FormatTest, LinuxBraceBreaking) {
16815   FormatStyle LinuxBraceStyle = getLLVMStyle();
16816   LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
16817   verifyFormat("namespace a\n"
16818                "{\n"
16819                "class A\n"
16820                "{\n"
16821                "  void f()\n"
16822                "  {\n"
16823                "    if (true) {\n"
16824                "      a();\n"
16825                "      b();\n"
16826                "    } else {\n"
16827                "      a();\n"
16828                "    }\n"
16829                "  }\n"
16830                "  void g() { return; }\n"
16831                "};\n"
16832                "struct B {\n"
16833                "  int x;\n"
16834                "};\n"
16835                "} // namespace a\n",
16836                LinuxBraceStyle);
16837   verifyFormat("enum X {\n"
16838                "  Y = 0,\n"
16839                "}\n",
16840                LinuxBraceStyle);
16841   verifyFormat("struct S {\n"
16842                "  int Type;\n"
16843                "  union {\n"
16844                "    int x;\n"
16845                "    double y;\n"
16846                "  } Value;\n"
16847                "  class C\n"
16848                "  {\n"
16849                "    MyFavoriteType Value;\n"
16850                "  } Class;\n"
16851                "}\n",
16852                LinuxBraceStyle);
16853 }
16854 
16855 TEST_F(FormatTest, MozillaBraceBreaking) {
16856   FormatStyle MozillaBraceStyle = getLLVMStyle();
16857   MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
16858   MozillaBraceStyle.FixNamespaceComments = false;
16859   verifyFormat("namespace a {\n"
16860                "class A\n"
16861                "{\n"
16862                "  void f()\n"
16863                "  {\n"
16864                "    if (true) {\n"
16865                "      a();\n"
16866                "      b();\n"
16867                "    }\n"
16868                "  }\n"
16869                "  void g() { return; }\n"
16870                "};\n"
16871                "enum E\n"
16872                "{\n"
16873                "  A,\n"
16874                "  // foo\n"
16875                "  B,\n"
16876                "  C\n"
16877                "};\n"
16878                "struct B\n"
16879                "{\n"
16880                "  int x;\n"
16881                "};\n"
16882                "}\n",
16883                MozillaBraceStyle);
16884   verifyFormat("struct S\n"
16885                "{\n"
16886                "  int Type;\n"
16887                "  union\n"
16888                "  {\n"
16889                "    int x;\n"
16890                "    double y;\n"
16891                "  } Value;\n"
16892                "  class C\n"
16893                "  {\n"
16894                "    MyFavoriteType Value;\n"
16895                "  } Class;\n"
16896                "}\n",
16897                MozillaBraceStyle);
16898 }
16899 
16900 TEST_F(FormatTest, StroustrupBraceBreaking) {
16901   FormatStyle StroustrupBraceStyle = getLLVMStyle();
16902   StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
16903   verifyFormat("namespace a {\n"
16904                "class A {\n"
16905                "  void f()\n"
16906                "  {\n"
16907                "    if (true) {\n"
16908                "      a();\n"
16909                "      b();\n"
16910                "    }\n"
16911                "  }\n"
16912                "  void g() { return; }\n"
16913                "};\n"
16914                "struct B {\n"
16915                "  int x;\n"
16916                "};\n"
16917                "} // namespace a\n",
16918                StroustrupBraceStyle);
16919 
16920   verifyFormat("void foo()\n"
16921                "{\n"
16922                "  if (a) {\n"
16923                "    a();\n"
16924                "  }\n"
16925                "  else {\n"
16926                "    b();\n"
16927                "  }\n"
16928                "}\n",
16929                StroustrupBraceStyle);
16930 
16931   verifyFormat("#ifdef _DEBUG\n"
16932                "int foo(int i = 0)\n"
16933                "#else\n"
16934                "int foo(int i = 5)\n"
16935                "#endif\n"
16936                "{\n"
16937                "  return i;\n"
16938                "}",
16939                StroustrupBraceStyle);
16940 
16941   verifyFormat("void foo() {}\n"
16942                "void bar()\n"
16943                "#ifdef _DEBUG\n"
16944                "{\n"
16945                "  foo();\n"
16946                "}\n"
16947                "#else\n"
16948                "{\n"
16949                "}\n"
16950                "#endif",
16951                StroustrupBraceStyle);
16952 
16953   verifyFormat("void foobar() { int i = 5; }\n"
16954                "#ifdef _DEBUG\n"
16955                "void bar() {}\n"
16956                "#else\n"
16957                "void bar() { foobar(); }\n"
16958                "#endif",
16959                StroustrupBraceStyle);
16960 }
16961 
16962 TEST_F(FormatTest, AllmanBraceBreaking) {
16963   FormatStyle AllmanBraceStyle = getLLVMStyle();
16964   AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
16965 
16966   EXPECT_EQ("namespace a\n"
16967             "{\n"
16968             "void f();\n"
16969             "void g();\n"
16970             "} // namespace a\n",
16971             format("namespace a\n"
16972                    "{\n"
16973                    "void f();\n"
16974                    "void g();\n"
16975                    "}\n",
16976                    AllmanBraceStyle));
16977 
16978   verifyFormat("namespace a\n"
16979                "{\n"
16980                "class A\n"
16981                "{\n"
16982                "  void f()\n"
16983                "  {\n"
16984                "    if (true)\n"
16985                "    {\n"
16986                "      a();\n"
16987                "      b();\n"
16988                "    }\n"
16989                "  }\n"
16990                "  void g() { return; }\n"
16991                "};\n"
16992                "struct B\n"
16993                "{\n"
16994                "  int x;\n"
16995                "};\n"
16996                "union C\n"
16997                "{\n"
16998                "};\n"
16999                "} // namespace a",
17000                AllmanBraceStyle);
17001 
17002   verifyFormat("void f()\n"
17003                "{\n"
17004                "  if (true)\n"
17005                "  {\n"
17006                "    a();\n"
17007                "  }\n"
17008                "  else if (false)\n"
17009                "  {\n"
17010                "    b();\n"
17011                "  }\n"
17012                "  else\n"
17013                "  {\n"
17014                "    c();\n"
17015                "  }\n"
17016                "}\n",
17017                AllmanBraceStyle);
17018 
17019   verifyFormat("void f()\n"
17020                "{\n"
17021                "  for (int i = 0; i < 10; ++i)\n"
17022                "  {\n"
17023                "    a();\n"
17024                "  }\n"
17025                "  while (false)\n"
17026                "  {\n"
17027                "    b();\n"
17028                "  }\n"
17029                "  do\n"
17030                "  {\n"
17031                "    c();\n"
17032                "  } while (false)\n"
17033                "}\n",
17034                AllmanBraceStyle);
17035 
17036   verifyFormat("void f(int a)\n"
17037                "{\n"
17038                "  switch (a)\n"
17039                "  {\n"
17040                "  case 0:\n"
17041                "    break;\n"
17042                "  case 1:\n"
17043                "  {\n"
17044                "    break;\n"
17045                "  }\n"
17046                "  case 2:\n"
17047                "  {\n"
17048                "  }\n"
17049                "  break;\n"
17050                "  default:\n"
17051                "    break;\n"
17052                "  }\n"
17053                "}\n",
17054                AllmanBraceStyle);
17055 
17056   verifyFormat("enum X\n"
17057                "{\n"
17058                "  Y = 0,\n"
17059                "}\n",
17060                AllmanBraceStyle);
17061   verifyFormat("enum X\n"
17062                "{\n"
17063                "  Y = 0\n"
17064                "}\n",
17065                AllmanBraceStyle);
17066 
17067   verifyFormat("@interface BSApplicationController ()\n"
17068                "{\n"
17069                "@private\n"
17070                "  id _extraIvar;\n"
17071                "}\n"
17072                "@end\n",
17073                AllmanBraceStyle);
17074 
17075   verifyFormat("#ifdef _DEBUG\n"
17076                "int foo(int i = 0)\n"
17077                "#else\n"
17078                "int foo(int i = 5)\n"
17079                "#endif\n"
17080                "{\n"
17081                "  return i;\n"
17082                "}",
17083                AllmanBraceStyle);
17084 
17085   verifyFormat("void foo() {}\n"
17086                "void bar()\n"
17087                "#ifdef _DEBUG\n"
17088                "{\n"
17089                "  foo();\n"
17090                "}\n"
17091                "#else\n"
17092                "{\n"
17093                "}\n"
17094                "#endif",
17095                AllmanBraceStyle);
17096 
17097   verifyFormat("void foobar() { int i = 5; }\n"
17098                "#ifdef _DEBUG\n"
17099                "void bar() {}\n"
17100                "#else\n"
17101                "void bar() { foobar(); }\n"
17102                "#endif",
17103                AllmanBraceStyle);
17104 
17105   EXPECT_EQ(AllmanBraceStyle.AllowShortLambdasOnASingleLine,
17106             FormatStyle::SLS_All);
17107 
17108   verifyFormat("[](int i) { return i + 2; };\n"
17109                "[](int i, int j)\n"
17110                "{\n"
17111                "  auto x = i + j;\n"
17112                "  auto y = i * j;\n"
17113                "  return x ^ y;\n"
17114                "};\n"
17115                "void foo()\n"
17116                "{\n"
17117                "  auto shortLambda = [](int i) { return i + 2; };\n"
17118                "  auto longLambda = [](int i, int j)\n"
17119                "  {\n"
17120                "    auto x = i + j;\n"
17121                "    auto y = i * j;\n"
17122                "    return x ^ y;\n"
17123                "  };\n"
17124                "}",
17125                AllmanBraceStyle);
17126 
17127   AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
17128 
17129   verifyFormat("[](int i)\n"
17130                "{\n"
17131                "  return i + 2;\n"
17132                "};\n"
17133                "[](int i, int j)\n"
17134                "{\n"
17135                "  auto x = i + j;\n"
17136                "  auto y = i * j;\n"
17137                "  return x ^ y;\n"
17138                "};\n"
17139                "void foo()\n"
17140                "{\n"
17141                "  auto shortLambda = [](int i)\n"
17142                "  {\n"
17143                "    return i + 2;\n"
17144                "  };\n"
17145                "  auto longLambda = [](int i, int j)\n"
17146                "  {\n"
17147                "    auto x = i + j;\n"
17148                "    auto y = i * j;\n"
17149                "    return x ^ y;\n"
17150                "  };\n"
17151                "}",
17152                AllmanBraceStyle);
17153 
17154   // Reset
17155   AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_All;
17156 
17157   // This shouldn't affect ObjC blocks..
17158   verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
17159                "  // ...\n"
17160                "  int i;\n"
17161                "}];",
17162                AllmanBraceStyle);
17163   verifyFormat("void (^block)(void) = ^{\n"
17164                "  // ...\n"
17165                "  int i;\n"
17166                "};",
17167                AllmanBraceStyle);
17168   // .. or dict literals.
17169   verifyFormat("void f()\n"
17170                "{\n"
17171                "  // ...\n"
17172                "  [object someMethod:@{@\"a\" : @\"b\"}];\n"
17173                "}",
17174                AllmanBraceStyle);
17175   verifyFormat("void f()\n"
17176                "{\n"
17177                "  // ...\n"
17178                "  [object someMethod:@{a : @\"b\"}];\n"
17179                "}",
17180                AllmanBraceStyle);
17181   verifyFormat("int f()\n"
17182                "{ // comment\n"
17183                "  return 42;\n"
17184                "}",
17185                AllmanBraceStyle);
17186 
17187   AllmanBraceStyle.ColumnLimit = 19;
17188   verifyFormat("void f() { int i; }", AllmanBraceStyle);
17189   AllmanBraceStyle.ColumnLimit = 18;
17190   verifyFormat("void f()\n"
17191                "{\n"
17192                "  int i;\n"
17193                "}",
17194                AllmanBraceStyle);
17195   AllmanBraceStyle.ColumnLimit = 80;
17196 
17197   FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle;
17198   BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine =
17199       FormatStyle::SIS_WithoutElse;
17200   BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
17201   verifyFormat("void f(bool b)\n"
17202                "{\n"
17203                "  if (b)\n"
17204                "  {\n"
17205                "    return;\n"
17206                "  }\n"
17207                "}\n",
17208                BreakBeforeBraceShortIfs);
17209   verifyFormat("void f(bool b)\n"
17210                "{\n"
17211                "  if constexpr (b)\n"
17212                "  {\n"
17213                "    return;\n"
17214                "  }\n"
17215                "}\n",
17216                BreakBeforeBraceShortIfs);
17217   verifyFormat("void f(bool b)\n"
17218                "{\n"
17219                "  if CONSTEXPR (b)\n"
17220                "  {\n"
17221                "    return;\n"
17222                "  }\n"
17223                "}\n",
17224                BreakBeforeBraceShortIfs);
17225   verifyFormat("void f(bool b)\n"
17226                "{\n"
17227                "  if (b) return;\n"
17228                "}\n",
17229                BreakBeforeBraceShortIfs);
17230   verifyFormat("void f(bool b)\n"
17231                "{\n"
17232                "  if constexpr (b) return;\n"
17233                "}\n",
17234                BreakBeforeBraceShortIfs);
17235   verifyFormat("void f(bool b)\n"
17236                "{\n"
17237                "  if CONSTEXPR (b) return;\n"
17238                "}\n",
17239                BreakBeforeBraceShortIfs);
17240   verifyFormat("void f(bool b)\n"
17241                "{\n"
17242                "  while (b)\n"
17243                "  {\n"
17244                "    return;\n"
17245                "  }\n"
17246                "}\n",
17247                BreakBeforeBraceShortIfs);
17248 }
17249 
17250 TEST_F(FormatTest, WhitesmithsBraceBreaking) {
17251   FormatStyle WhitesmithsBraceStyle = getLLVMStyle();
17252   WhitesmithsBraceStyle.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
17253 
17254   // Make a few changes to the style for testing purposes
17255   WhitesmithsBraceStyle.AllowShortFunctionsOnASingleLine =
17256       FormatStyle::SFS_Empty;
17257   WhitesmithsBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
17258   WhitesmithsBraceStyle.ColumnLimit = 0;
17259 
17260   // FIXME: this test case can't decide whether there should be a blank line
17261   // after the ~D() line or not. It adds one if one doesn't exist in the test
17262   // and it removes the line if one exists.
17263   /*
17264   verifyFormat("class A;\n"
17265                "namespace B\n"
17266                "  {\n"
17267                "class C;\n"
17268                "// Comment\n"
17269                "class D\n"
17270                "  {\n"
17271                "public:\n"
17272                "  D();\n"
17273                "  ~D() {}\n"
17274                "private:\n"
17275                "  enum E\n"
17276                "    {\n"
17277                "    F\n"
17278                "    }\n"
17279                "  };\n"
17280                "  } // namespace B\n",
17281                WhitesmithsBraceStyle);
17282   */
17283 
17284   WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_None;
17285   verifyFormat("namespace a\n"
17286                "  {\n"
17287                "class A\n"
17288                "  {\n"
17289                "  void f()\n"
17290                "    {\n"
17291                "    if (true)\n"
17292                "      {\n"
17293                "      a();\n"
17294                "      b();\n"
17295                "      }\n"
17296                "    }\n"
17297                "  void g()\n"
17298                "    {\n"
17299                "    return;\n"
17300                "    }\n"
17301                "  };\n"
17302                "struct B\n"
17303                "  {\n"
17304                "  int x;\n"
17305                "  };\n"
17306                "  } // namespace a",
17307                WhitesmithsBraceStyle);
17308 
17309   verifyFormat("namespace a\n"
17310                "  {\n"
17311                "namespace b\n"
17312                "  {\n"
17313                "class A\n"
17314                "  {\n"
17315                "  void f()\n"
17316                "    {\n"
17317                "    if (true)\n"
17318                "      {\n"
17319                "      a();\n"
17320                "      b();\n"
17321                "      }\n"
17322                "    }\n"
17323                "  void g()\n"
17324                "    {\n"
17325                "    return;\n"
17326                "    }\n"
17327                "  };\n"
17328                "struct B\n"
17329                "  {\n"
17330                "  int x;\n"
17331                "  };\n"
17332                "  } // namespace b\n"
17333                "  } // namespace a",
17334                WhitesmithsBraceStyle);
17335 
17336   WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_Inner;
17337   verifyFormat("namespace a\n"
17338                "  {\n"
17339                "namespace b\n"
17340                "  {\n"
17341                "  class A\n"
17342                "    {\n"
17343                "    void f()\n"
17344                "      {\n"
17345                "      if (true)\n"
17346                "        {\n"
17347                "        a();\n"
17348                "        b();\n"
17349                "        }\n"
17350                "      }\n"
17351                "    void g()\n"
17352                "      {\n"
17353                "      return;\n"
17354                "      }\n"
17355                "    };\n"
17356                "  struct B\n"
17357                "    {\n"
17358                "    int x;\n"
17359                "    };\n"
17360                "  } // namespace b\n"
17361                "  } // namespace a",
17362                WhitesmithsBraceStyle);
17363 
17364   WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_All;
17365   verifyFormat("namespace a\n"
17366                "  {\n"
17367                "  namespace b\n"
17368                "    {\n"
17369                "    class A\n"
17370                "      {\n"
17371                "      void f()\n"
17372                "        {\n"
17373                "        if (true)\n"
17374                "          {\n"
17375                "          a();\n"
17376                "          b();\n"
17377                "          }\n"
17378                "        }\n"
17379                "      void g()\n"
17380                "        {\n"
17381                "        return;\n"
17382                "        }\n"
17383                "      };\n"
17384                "    struct B\n"
17385                "      {\n"
17386                "      int x;\n"
17387                "      };\n"
17388                "    } // namespace b\n"
17389                "  }   // namespace a",
17390                WhitesmithsBraceStyle);
17391 
17392   verifyFormat("void f()\n"
17393                "  {\n"
17394                "  if (true)\n"
17395                "    {\n"
17396                "    a();\n"
17397                "    }\n"
17398                "  else if (false)\n"
17399                "    {\n"
17400                "    b();\n"
17401                "    }\n"
17402                "  else\n"
17403                "    {\n"
17404                "    c();\n"
17405                "    }\n"
17406                "  }\n",
17407                WhitesmithsBraceStyle);
17408 
17409   verifyFormat("void f()\n"
17410                "  {\n"
17411                "  for (int i = 0; i < 10; ++i)\n"
17412                "    {\n"
17413                "    a();\n"
17414                "    }\n"
17415                "  while (false)\n"
17416                "    {\n"
17417                "    b();\n"
17418                "    }\n"
17419                "  do\n"
17420                "    {\n"
17421                "    c();\n"
17422                "    } while (false)\n"
17423                "  }\n",
17424                WhitesmithsBraceStyle);
17425 
17426   WhitesmithsBraceStyle.IndentCaseLabels = true;
17427   verifyFormat("void switchTest1(int a)\n"
17428                "  {\n"
17429                "  switch (a)\n"
17430                "    {\n"
17431                "    case 2:\n"
17432                "      {\n"
17433                "      }\n"
17434                "      break;\n"
17435                "    }\n"
17436                "  }\n",
17437                WhitesmithsBraceStyle);
17438 
17439   verifyFormat("void switchTest2(int a)\n"
17440                "  {\n"
17441                "  switch (a)\n"
17442                "    {\n"
17443                "    case 0:\n"
17444                "      break;\n"
17445                "    case 1:\n"
17446                "      {\n"
17447                "      break;\n"
17448                "      }\n"
17449                "    case 2:\n"
17450                "      {\n"
17451                "      }\n"
17452                "      break;\n"
17453                "    default:\n"
17454                "      break;\n"
17455                "    }\n"
17456                "  }\n",
17457                WhitesmithsBraceStyle);
17458 
17459   verifyFormat("void switchTest3(int a)\n"
17460                "  {\n"
17461                "  switch (a)\n"
17462                "    {\n"
17463                "    case 0:\n"
17464                "      {\n"
17465                "      foo(x);\n"
17466                "      }\n"
17467                "      break;\n"
17468                "    default:\n"
17469                "      {\n"
17470                "      foo(1);\n"
17471                "      }\n"
17472                "      break;\n"
17473                "    }\n"
17474                "  }\n",
17475                WhitesmithsBraceStyle);
17476 
17477   WhitesmithsBraceStyle.IndentCaseLabels = false;
17478 
17479   verifyFormat("void switchTest4(int a)\n"
17480                "  {\n"
17481                "  switch (a)\n"
17482                "    {\n"
17483                "  case 2:\n"
17484                "    {\n"
17485                "    }\n"
17486                "    break;\n"
17487                "    }\n"
17488                "  }\n",
17489                WhitesmithsBraceStyle);
17490 
17491   verifyFormat("void switchTest5(int a)\n"
17492                "  {\n"
17493                "  switch (a)\n"
17494                "    {\n"
17495                "  case 0:\n"
17496                "    break;\n"
17497                "  case 1:\n"
17498                "    {\n"
17499                "    foo();\n"
17500                "    break;\n"
17501                "    }\n"
17502                "  case 2:\n"
17503                "    {\n"
17504                "    }\n"
17505                "    break;\n"
17506                "  default:\n"
17507                "    break;\n"
17508                "    }\n"
17509                "  }\n",
17510                WhitesmithsBraceStyle);
17511 
17512   verifyFormat("void switchTest6(int a)\n"
17513                "  {\n"
17514                "  switch (a)\n"
17515                "    {\n"
17516                "  case 0:\n"
17517                "    {\n"
17518                "    foo(x);\n"
17519                "    }\n"
17520                "    break;\n"
17521                "  default:\n"
17522                "    {\n"
17523                "    foo(1);\n"
17524                "    }\n"
17525                "    break;\n"
17526                "    }\n"
17527                "  }\n",
17528                WhitesmithsBraceStyle);
17529 
17530   verifyFormat("enum X\n"
17531                "  {\n"
17532                "  Y = 0, // testing\n"
17533                "  }\n",
17534                WhitesmithsBraceStyle);
17535 
17536   verifyFormat("enum X\n"
17537                "  {\n"
17538                "  Y = 0\n"
17539                "  }\n",
17540                WhitesmithsBraceStyle);
17541   verifyFormat("enum X\n"
17542                "  {\n"
17543                "  Y = 0,\n"
17544                "  Z = 1\n"
17545                "  };\n",
17546                WhitesmithsBraceStyle);
17547 
17548   verifyFormat("@interface BSApplicationController ()\n"
17549                "  {\n"
17550                "@private\n"
17551                "  id _extraIvar;\n"
17552                "  }\n"
17553                "@end\n",
17554                WhitesmithsBraceStyle);
17555 
17556   verifyFormat("#ifdef _DEBUG\n"
17557                "int foo(int i = 0)\n"
17558                "#else\n"
17559                "int foo(int i = 5)\n"
17560                "#endif\n"
17561                "  {\n"
17562                "  return i;\n"
17563                "  }",
17564                WhitesmithsBraceStyle);
17565 
17566   verifyFormat("void foo() {}\n"
17567                "void bar()\n"
17568                "#ifdef _DEBUG\n"
17569                "  {\n"
17570                "  foo();\n"
17571                "  }\n"
17572                "#else\n"
17573                "  {\n"
17574                "  }\n"
17575                "#endif",
17576                WhitesmithsBraceStyle);
17577 
17578   verifyFormat("void foobar()\n"
17579                "  {\n"
17580                "  int i = 5;\n"
17581                "  }\n"
17582                "#ifdef _DEBUG\n"
17583                "void bar()\n"
17584                "  {\n"
17585                "  }\n"
17586                "#else\n"
17587                "void bar()\n"
17588                "  {\n"
17589                "  foobar();\n"
17590                "  }\n"
17591                "#endif",
17592                WhitesmithsBraceStyle);
17593 
17594   // This shouldn't affect ObjC blocks..
17595   verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
17596                "  // ...\n"
17597                "  int i;\n"
17598                "}];",
17599                WhitesmithsBraceStyle);
17600   verifyFormat("void (^block)(void) = ^{\n"
17601                "  // ...\n"
17602                "  int i;\n"
17603                "};",
17604                WhitesmithsBraceStyle);
17605   // .. or dict literals.
17606   verifyFormat("void f()\n"
17607                "  {\n"
17608                "  [object someMethod:@{@\"a\" : @\"b\"}];\n"
17609                "  }",
17610                WhitesmithsBraceStyle);
17611 
17612   verifyFormat("int f()\n"
17613                "  { // comment\n"
17614                "  return 42;\n"
17615                "  }",
17616                WhitesmithsBraceStyle);
17617 
17618   FormatStyle BreakBeforeBraceShortIfs = WhitesmithsBraceStyle;
17619   BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine =
17620       FormatStyle::SIS_OnlyFirstIf;
17621   BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
17622   verifyFormat("void f(bool b)\n"
17623                "  {\n"
17624                "  if (b)\n"
17625                "    {\n"
17626                "    return;\n"
17627                "    }\n"
17628                "  }\n",
17629                BreakBeforeBraceShortIfs);
17630   verifyFormat("void f(bool b)\n"
17631                "  {\n"
17632                "  if (b) return;\n"
17633                "  }\n",
17634                BreakBeforeBraceShortIfs);
17635   verifyFormat("void f(bool b)\n"
17636                "  {\n"
17637                "  while (b)\n"
17638                "    {\n"
17639                "    return;\n"
17640                "    }\n"
17641                "  }\n",
17642                BreakBeforeBraceShortIfs);
17643 }
17644 
17645 TEST_F(FormatTest, GNUBraceBreaking) {
17646   FormatStyle GNUBraceStyle = getLLVMStyle();
17647   GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
17648   verifyFormat("namespace a\n"
17649                "{\n"
17650                "class A\n"
17651                "{\n"
17652                "  void f()\n"
17653                "  {\n"
17654                "    int a;\n"
17655                "    {\n"
17656                "      int b;\n"
17657                "    }\n"
17658                "    if (true)\n"
17659                "      {\n"
17660                "        a();\n"
17661                "        b();\n"
17662                "      }\n"
17663                "  }\n"
17664                "  void g() { return; }\n"
17665                "}\n"
17666                "} // namespace a",
17667                GNUBraceStyle);
17668 
17669   verifyFormat("void f()\n"
17670                "{\n"
17671                "  if (true)\n"
17672                "    {\n"
17673                "      a();\n"
17674                "    }\n"
17675                "  else if (false)\n"
17676                "    {\n"
17677                "      b();\n"
17678                "    }\n"
17679                "  else\n"
17680                "    {\n"
17681                "      c();\n"
17682                "    }\n"
17683                "}\n",
17684                GNUBraceStyle);
17685 
17686   verifyFormat("void f()\n"
17687                "{\n"
17688                "  for (int i = 0; i < 10; ++i)\n"
17689                "    {\n"
17690                "      a();\n"
17691                "    }\n"
17692                "  while (false)\n"
17693                "    {\n"
17694                "      b();\n"
17695                "    }\n"
17696                "  do\n"
17697                "    {\n"
17698                "      c();\n"
17699                "    }\n"
17700                "  while (false);\n"
17701                "}\n",
17702                GNUBraceStyle);
17703 
17704   verifyFormat("void f(int a)\n"
17705                "{\n"
17706                "  switch (a)\n"
17707                "    {\n"
17708                "    case 0:\n"
17709                "      break;\n"
17710                "    case 1:\n"
17711                "      {\n"
17712                "        break;\n"
17713                "      }\n"
17714                "    case 2:\n"
17715                "      {\n"
17716                "      }\n"
17717                "      break;\n"
17718                "    default:\n"
17719                "      break;\n"
17720                "    }\n"
17721                "}\n",
17722                GNUBraceStyle);
17723 
17724   verifyFormat("enum X\n"
17725                "{\n"
17726                "  Y = 0,\n"
17727                "}\n",
17728                GNUBraceStyle);
17729 
17730   verifyFormat("@interface BSApplicationController ()\n"
17731                "{\n"
17732                "@private\n"
17733                "  id _extraIvar;\n"
17734                "}\n"
17735                "@end\n",
17736                GNUBraceStyle);
17737 
17738   verifyFormat("#ifdef _DEBUG\n"
17739                "int foo(int i = 0)\n"
17740                "#else\n"
17741                "int foo(int i = 5)\n"
17742                "#endif\n"
17743                "{\n"
17744                "  return i;\n"
17745                "}",
17746                GNUBraceStyle);
17747 
17748   verifyFormat("void foo() {}\n"
17749                "void bar()\n"
17750                "#ifdef _DEBUG\n"
17751                "{\n"
17752                "  foo();\n"
17753                "}\n"
17754                "#else\n"
17755                "{\n"
17756                "}\n"
17757                "#endif",
17758                GNUBraceStyle);
17759 
17760   verifyFormat("void foobar() { int i = 5; }\n"
17761                "#ifdef _DEBUG\n"
17762                "void bar() {}\n"
17763                "#else\n"
17764                "void bar() { foobar(); }\n"
17765                "#endif",
17766                GNUBraceStyle);
17767 }
17768 
17769 TEST_F(FormatTest, WebKitBraceBreaking) {
17770   FormatStyle WebKitBraceStyle = getLLVMStyle();
17771   WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit;
17772   WebKitBraceStyle.FixNamespaceComments = false;
17773   verifyFormat("namespace a {\n"
17774                "class A {\n"
17775                "  void f()\n"
17776                "  {\n"
17777                "    if (true) {\n"
17778                "      a();\n"
17779                "      b();\n"
17780                "    }\n"
17781                "  }\n"
17782                "  void g() { return; }\n"
17783                "};\n"
17784                "enum E {\n"
17785                "  A,\n"
17786                "  // foo\n"
17787                "  B,\n"
17788                "  C\n"
17789                "};\n"
17790                "struct B {\n"
17791                "  int x;\n"
17792                "};\n"
17793                "}\n",
17794                WebKitBraceStyle);
17795   verifyFormat("struct S {\n"
17796                "  int Type;\n"
17797                "  union {\n"
17798                "    int x;\n"
17799                "    double y;\n"
17800                "  } Value;\n"
17801                "  class C {\n"
17802                "    MyFavoriteType Value;\n"
17803                "  } Class;\n"
17804                "};\n",
17805                WebKitBraceStyle);
17806 }
17807 
17808 TEST_F(FormatTest, CatchExceptionReferenceBinding) {
17809   verifyFormat("void f() {\n"
17810                "  try {\n"
17811                "  } catch (const Exception &e) {\n"
17812                "  }\n"
17813                "}\n",
17814                getLLVMStyle());
17815 }
17816 
17817 TEST_F(FormatTest, CatchAlignArrayOfStructuresRightAlignment) {
17818   auto Style = getLLVMStyle();
17819   Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
17820   Style.AlignConsecutiveAssignments =
17821       FormatStyle::AlignConsecutiveStyle::ACS_Consecutive;
17822   Style.AlignConsecutiveDeclarations =
17823       FormatStyle::AlignConsecutiveStyle::ACS_Consecutive;
17824   verifyFormat("struct test demo[] = {\n"
17825                "    {56,    23, \"hello\"},\n"
17826                "    {-1, 93463, \"world\"},\n"
17827                "    { 7,     5,    \"!!\"}\n"
17828                "};\n",
17829                Style);
17830 
17831   verifyFormat("struct test demo[] = {\n"
17832                "    {56,    23, \"hello\"}, // first line\n"
17833                "    {-1, 93463, \"world\"}, // second line\n"
17834                "    { 7,     5,    \"!!\"}  // third line\n"
17835                "};\n",
17836                Style);
17837 
17838   verifyFormat("struct test demo[4] = {\n"
17839                "    { 56,    23, 21,       \"oh\"}, // first line\n"
17840                "    { -1, 93463, 22,       \"my\"}, // second line\n"
17841                "    {  7,     5,  1, \"goodness\"}  // third line\n"
17842                "    {234,     5,  1, \"gracious\"}  // fourth line\n"
17843                "};\n",
17844                Style);
17845 
17846   verifyFormat("struct test demo[3] = {\n"
17847                "    {56,    23, \"hello\"},\n"
17848                "    {-1, 93463, \"world\"},\n"
17849                "    { 7,     5,    \"!!\"}\n"
17850                "};\n",
17851                Style);
17852 
17853   verifyFormat("struct test demo[3] = {\n"
17854                "    {int{56},    23, \"hello\"},\n"
17855                "    {int{-1}, 93463, \"world\"},\n"
17856                "    { int{7},     5,    \"!!\"}\n"
17857                "};\n",
17858                Style);
17859 
17860   verifyFormat("struct test demo[] = {\n"
17861                "    {56,    23, \"hello\"},\n"
17862                "    {-1, 93463, \"world\"},\n"
17863                "    { 7,     5,    \"!!\"},\n"
17864                "};\n",
17865                Style);
17866 
17867   verifyFormat("test demo[] = {\n"
17868                "    {56,    23, \"hello\"},\n"
17869                "    {-1, 93463, \"world\"},\n"
17870                "    { 7,     5,    \"!!\"},\n"
17871                "};\n",
17872                Style);
17873 
17874   verifyFormat("demo = std::array<struct test, 3>{\n"
17875                "    test{56,    23, \"hello\"},\n"
17876                "    test{-1, 93463, \"world\"},\n"
17877                "    test{ 7,     5,    \"!!\"},\n"
17878                "};\n",
17879                Style);
17880 
17881   verifyFormat("test demo[] = {\n"
17882                "    {56,    23, \"hello\"},\n"
17883                "#if X\n"
17884                "    {-1, 93463, \"world\"},\n"
17885                "#endif\n"
17886                "    { 7,     5,    \"!!\"}\n"
17887                "};\n",
17888                Style);
17889 
17890   verifyFormat(
17891       "test demo[] = {\n"
17892       "    { 7,    23,\n"
17893       "     \"hello world i am a very long line that really, in any\"\n"
17894       "     \"just world, ought to be split over multiple lines\"},\n"
17895       "    {-1, 93463,                                  \"world\"},\n"
17896       "    {56,     5,                                     \"!!\"}\n"
17897       "};\n",
17898       Style);
17899 
17900   verifyFormat("return GradForUnaryCwise(g, {\n"
17901                "                                {{\"sign\"}, \"Sign\",  "
17902                "  {\"x\", \"dy\"}},\n"
17903                "                                {  {\"dx\"},  \"Mul\", {\"dy\""
17904                ", \"sign\"}},\n"
17905                "});\n",
17906                Style);
17907 
17908   Style.ColumnLimit = 0;
17909   EXPECT_EQ(
17910       "test demo[] = {\n"
17911       "    {56,    23, \"hello world i am a very long line that really, "
17912       "in any just world, ought to be split over multiple lines\"},\n"
17913       "    {-1, 93463,                                                  "
17914       "                                                 \"world\"},\n"
17915       "    { 7,     5,                                                  "
17916       "                                                    \"!!\"},\n"
17917       "};",
17918       format("test demo[] = {{56, 23, \"hello world i am a very long line "
17919              "that really, in any just world, ought to be split over multiple "
17920              "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
17921              Style));
17922 
17923   Style.ColumnLimit = 80;
17924   verifyFormat("test demo[] = {\n"
17925                "    {56,    23, /* a comment */ \"hello\"},\n"
17926                "    {-1, 93463,                 \"world\"},\n"
17927                "    { 7,     5,                    \"!!\"}\n"
17928                "};\n",
17929                Style);
17930 
17931   verifyFormat("test demo[] = {\n"
17932                "    {56,    23,                    \"hello\"},\n"
17933                "    {-1, 93463, \"world\" /* comment here */},\n"
17934                "    { 7,     5,                       \"!!\"}\n"
17935                "};\n",
17936                Style);
17937 
17938   verifyFormat("test demo[] = {\n"
17939                "    {56, /* a comment */ 23, \"hello\"},\n"
17940                "    {-1,              93463, \"world\"},\n"
17941                "    { 7,                  5,    \"!!\"}\n"
17942                "};\n",
17943                Style);
17944 
17945   Style.ColumnLimit = 20;
17946   EXPECT_EQ(
17947       "demo = std::array<\n"
17948       "    struct test, 3>{\n"
17949       "    test{\n"
17950       "         56,    23,\n"
17951       "         \"hello \"\n"
17952       "         \"world i \"\n"
17953       "         \"am a very \"\n"
17954       "         \"long line \"\n"
17955       "         \"that \"\n"
17956       "         \"really, \"\n"
17957       "         \"in any \"\n"
17958       "         \"just \"\n"
17959       "         \"world, \"\n"
17960       "         \"ought to \"\n"
17961       "         \"be split \"\n"
17962       "         \"over \"\n"
17963       "         \"multiple \"\n"
17964       "         \"lines\"},\n"
17965       "    test{-1, 93463,\n"
17966       "         \"world\"},\n"
17967       "    test{ 7,     5,\n"
17968       "         \"!!\"   },\n"
17969       "};",
17970       format("demo = std::array<struct test, 3>{test{56, 23, \"hello world "
17971              "i am a very long line that really, in any just world, ought "
17972              "to be split over multiple lines\"},test{-1, 93463, \"world\"},"
17973              "test{7, 5, \"!!\"},};",
17974              Style));
17975   // This caused a core dump by enabling Alignment in the LLVMStyle globally
17976   Style = getLLVMStyleWithColumns(50);
17977   Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
17978   verifyFormat("static A x = {\n"
17979                "    {{init1, init2, init3, init4},\n"
17980                "     {init1, init2, init3, init4}}\n"
17981                "};",
17982                Style);
17983   Style.ColumnLimit = 100;
17984   EXPECT_EQ(
17985       "test demo[] = {\n"
17986       "    {56,    23,\n"
17987       "     \"hello world i am a very long line that really, in any just world"
17988       ", ought to be split over \"\n"
17989       "     \"multiple lines\"  },\n"
17990       "    {-1, 93463, \"world\"},\n"
17991       "    { 7,     5,    \"!!\"},\n"
17992       "};",
17993       format("test demo[] = {{56, 23, \"hello world i am a very long line "
17994              "that really, in any just world, ought to be split over multiple "
17995              "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
17996              Style));
17997 
17998   Style = getLLVMStyleWithColumns(50);
17999   Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
18000   Style.AlignConsecutiveAssignments =
18001       FormatStyle::AlignConsecutiveStyle::ACS_Consecutive;
18002   Style.AlignConsecutiveDeclarations =
18003       FormatStyle::AlignConsecutiveStyle::ACS_Consecutive;
18004   verifyFormat("struct test demo[] = {\n"
18005                "    {56,    23, \"hello\"},\n"
18006                "    {-1, 93463, \"world\"},\n"
18007                "    { 7,     5,    \"!!\"}\n"
18008                "};\n"
18009                "static A x = {\n"
18010                "    {{init1, init2, init3, init4},\n"
18011                "     {init1, init2, init3, init4}}\n"
18012                "};",
18013                Style);
18014   Style.ColumnLimit = 100;
18015   Style.AlignConsecutiveAssignments =
18016       FormatStyle::AlignConsecutiveStyle::ACS_AcrossComments;
18017   Style.AlignConsecutiveDeclarations =
18018       FormatStyle::AlignConsecutiveStyle::ACS_AcrossComments;
18019   verifyFormat("struct test demo[] = {\n"
18020                "    {56,    23, \"hello\"},\n"
18021                "    {-1, 93463, \"world\"},\n"
18022                "    { 7,     5,    \"!!\"}\n"
18023                "};\n"
18024                "struct test demo[4] = {\n"
18025                "    { 56,    23, 21,       \"oh\"}, // first line\n"
18026                "    { -1, 93463, 22,       \"my\"}, // second line\n"
18027                "    {  7,     5,  1, \"goodness\"}  // third line\n"
18028                "    {234,     5,  1, \"gracious\"}  // fourth line\n"
18029                "};\n",
18030                Style);
18031   EXPECT_EQ(
18032       "test demo[] = {\n"
18033       "    {56,\n"
18034       "     \"hello world i am a very long line that really, in any just world"
18035       ", ought to be split over \"\n"
18036       "     \"multiple lines\",    23},\n"
18037       "    {-1,      \"world\", 93463},\n"
18038       "    { 7,         \"!!\",     5},\n"
18039       "};",
18040       format("test demo[] = {{56, \"hello world i am a very long line "
18041              "that really, in any just world, ought to be split over multiple "
18042              "lines\", 23},{-1, \"world\", 93463},{7, \"!!\", 5},};",
18043              Style));
18044 }
18045 
18046 TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) {
18047   auto Style = getLLVMStyle();
18048   Style.AlignArrayOfStructures = FormatStyle::AIAS_Left;
18049   /* FIXME: This case gets misformatted.
18050   verifyFormat("auto foo = Items{\n"
18051                "    Section{0, bar(), },\n"
18052                "    Section{1, boo()  }\n"
18053                "};\n",
18054                Style);
18055   */
18056   verifyFormat("auto foo = Items{\n"
18057                "    Section{\n"
18058                "            0, bar(),\n"
18059                "            }\n"
18060                "};\n",
18061                Style);
18062   verifyFormat("struct test demo[] = {\n"
18063                "    {56, 23,    \"hello\"},\n"
18064                "    {-1, 93463, \"world\"},\n"
18065                "    {7,  5,     \"!!\"   }\n"
18066                "};\n",
18067                Style);
18068   verifyFormat("struct test demo[] = {\n"
18069                "    {56, 23,    \"hello\"}, // first line\n"
18070                "    {-1, 93463, \"world\"}, // second line\n"
18071                "    {7,  5,     \"!!\"   }  // third line\n"
18072                "};\n",
18073                Style);
18074   verifyFormat("struct test demo[4] = {\n"
18075                "    {56,  23,    21, \"oh\"      }, // first line\n"
18076                "    {-1,  93463, 22, \"my\"      }, // second line\n"
18077                "    {7,   5,     1,  \"goodness\"}  // third line\n"
18078                "    {234, 5,     1,  \"gracious\"}  // fourth line\n"
18079                "};\n",
18080                Style);
18081   verifyFormat("struct test demo[3] = {\n"
18082                "    {56, 23,    \"hello\"},\n"
18083                "    {-1, 93463, \"world\"},\n"
18084                "    {7,  5,     \"!!\"   }\n"
18085                "};\n",
18086                Style);
18087 
18088   verifyFormat("struct test demo[3] = {\n"
18089                "    {int{56}, 23,    \"hello\"},\n"
18090                "    {int{-1}, 93463, \"world\"},\n"
18091                "    {int{7},  5,     \"!!\"   }\n"
18092                "};\n",
18093                Style);
18094   verifyFormat("struct test demo[] = {\n"
18095                "    {56, 23,    \"hello\"},\n"
18096                "    {-1, 93463, \"world\"},\n"
18097                "    {7,  5,     \"!!\"   },\n"
18098                "};\n",
18099                Style);
18100   verifyFormat("test demo[] = {\n"
18101                "    {56, 23,    \"hello\"},\n"
18102                "    {-1, 93463, \"world\"},\n"
18103                "    {7,  5,     \"!!\"   },\n"
18104                "};\n",
18105                Style);
18106   verifyFormat("demo = std::array<struct test, 3>{\n"
18107                "    test{56, 23,    \"hello\"},\n"
18108                "    test{-1, 93463, \"world\"},\n"
18109                "    test{7,  5,     \"!!\"   },\n"
18110                "};\n",
18111                Style);
18112   verifyFormat("test demo[] = {\n"
18113                "    {56, 23,    \"hello\"},\n"
18114                "#if X\n"
18115                "    {-1, 93463, \"world\"},\n"
18116                "#endif\n"
18117                "    {7,  5,     \"!!\"   }\n"
18118                "};\n",
18119                Style);
18120   verifyFormat(
18121       "test demo[] = {\n"
18122       "    {7,  23,\n"
18123       "     \"hello world i am a very long line that really, in any\"\n"
18124       "     \"just world, ought to be split over multiple lines\"},\n"
18125       "    {-1, 93463, \"world\"                                 },\n"
18126       "    {56, 5,     \"!!\"                                    }\n"
18127       "};\n",
18128       Style);
18129 
18130   verifyFormat("return GradForUnaryCwise(g, {\n"
18131                "                                {{\"sign\"}, \"Sign\", {\"x\", "
18132                "\"dy\"}   },\n"
18133                "                                {{\"dx\"},   \"Mul\",  "
18134                "{\"dy\", \"sign\"}},\n"
18135                "});\n",
18136                Style);
18137 
18138   Style.ColumnLimit = 0;
18139   EXPECT_EQ(
18140       "test demo[] = {\n"
18141       "    {56, 23,    \"hello world i am a very long line that really, in any "
18142       "just world, ought to be split over multiple lines\"},\n"
18143       "    {-1, 93463, \"world\"                                               "
18144       "                                                   },\n"
18145       "    {7,  5,     \"!!\"                                                  "
18146       "                                                   },\n"
18147       "};",
18148       format("test demo[] = {{56, 23, \"hello world i am a very long line "
18149              "that really, in any just world, ought to be split over multiple "
18150              "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
18151              Style));
18152 
18153   Style.ColumnLimit = 80;
18154   verifyFormat("test demo[] = {\n"
18155                "    {56, 23,    /* a comment */ \"hello\"},\n"
18156                "    {-1, 93463, \"world\"                },\n"
18157                "    {7,  5,     \"!!\"                   }\n"
18158                "};\n",
18159                Style);
18160 
18161   verifyFormat("test demo[] = {\n"
18162                "    {56, 23,    \"hello\"                   },\n"
18163                "    {-1, 93463, \"world\" /* comment here */},\n"
18164                "    {7,  5,     \"!!\"                      }\n"
18165                "};\n",
18166                Style);
18167 
18168   verifyFormat("test demo[] = {\n"
18169                "    {56, /* a comment */ 23, \"hello\"},\n"
18170                "    {-1, 93463,              \"world\"},\n"
18171                "    {7,  5,                  \"!!\"   }\n"
18172                "};\n",
18173                Style);
18174 
18175   Style.ColumnLimit = 20;
18176   EXPECT_EQ(
18177       "demo = std::array<\n"
18178       "    struct test, 3>{\n"
18179       "    test{\n"
18180       "         56, 23,\n"
18181       "         \"hello \"\n"
18182       "         \"world i \"\n"
18183       "         \"am a very \"\n"
18184       "         \"long line \"\n"
18185       "         \"that \"\n"
18186       "         \"really, \"\n"
18187       "         \"in any \"\n"
18188       "         \"just \"\n"
18189       "         \"world, \"\n"
18190       "         \"ought to \"\n"
18191       "         \"be split \"\n"
18192       "         \"over \"\n"
18193       "         \"multiple \"\n"
18194       "         \"lines\"},\n"
18195       "    test{-1, 93463,\n"
18196       "         \"world\"},\n"
18197       "    test{7,  5,\n"
18198       "         \"!!\"   },\n"
18199       "};",
18200       format("demo = std::array<struct test, 3>{test{56, 23, \"hello world "
18201              "i am a very long line that really, in any just world, ought "
18202              "to be split over multiple lines\"},test{-1, 93463, \"world\"},"
18203              "test{7, 5, \"!!\"},};",
18204              Style));
18205 
18206   // This caused a core dump by enabling Alignment in the LLVMStyle globally
18207   Style = getLLVMStyleWithColumns(50);
18208   Style.AlignArrayOfStructures = FormatStyle::AIAS_Left;
18209   verifyFormat("static A x = {\n"
18210                "    {{init1, init2, init3, init4},\n"
18211                "     {init1, init2, init3, init4}}\n"
18212                "};",
18213                Style);
18214   Style.ColumnLimit = 100;
18215   EXPECT_EQ(
18216       "test demo[] = {\n"
18217       "    {56, 23,\n"
18218       "     \"hello world i am a very long line that really, in any just world"
18219       ", ought to be split over \"\n"
18220       "     \"multiple lines\"  },\n"
18221       "    {-1, 93463, \"world\"},\n"
18222       "    {7,  5,     \"!!\"   },\n"
18223       "};",
18224       format("test demo[] = {{56, 23, \"hello world i am a very long line "
18225              "that really, in any just world, ought to be split over multiple "
18226              "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
18227              Style));
18228 }
18229 
18230 TEST_F(FormatTest, UnderstandsPragmas) {
18231   verifyFormat("#pragma omp reduction(| : var)");
18232   verifyFormat("#pragma omp reduction(+ : var)");
18233 
18234   EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string "
18235             "(including parentheses).",
18236             format("#pragma    mark   Any non-hyphenated or hyphenated string "
18237                    "(including parentheses)."));
18238 }
18239 
18240 TEST_F(FormatTest, UnderstandPragmaOption) {
18241   verifyFormat("#pragma option -C -A");
18242 
18243   EXPECT_EQ("#pragma option -C -A", format("#pragma    option   -C   -A"));
18244 }
18245 
18246 TEST_F(FormatTest, OptimizeBreakPenaltyVsExcess) {
18247   FormatStyle Style = getLLVMStyle();
18248   Style.ColumnLimit = 20;
18249 
18250   // See PR41213
18251   EXPECT_EQ("/*\n"
18252             " *\t9012345\n"
18253             " * /8901\n"
18254             " */",
18255             format("/*\n"
18256                    " *\t9012345 /8901\n"
18257                    " */",
18258                    Style));
18259   EXPECT_EQ("/*\n"
18260             " *345678\n"
18261             " *\t/8901\n"
18262             " */",
18263             format("/*\n"
18264                    " *345678\t/8901\n"
18265                    " */",
18266                    Style));
18267 
18268   verifyFormat("int a; // the\n"
18269                "       // comment",
18270                Style);
18271   EXPECT_EQ("int a; /* first line\n"
18272             "        * second\n"
18273             "        * line third\n"
18274             "        * line\n"
18275             "        */",
18276             format("int a; /* first line\n"
18277                    "        * second\n"
18278                    "        * line third\n"
18279                    "        * line\n"
18280                    "        */",
18281                    Style));
18282   EXPECT_EQ("int a; // first line\n"
18283             "       // second\n"
18284             "       // line third\n"
18285             "       // line",
18286             format("int a; // first line\n"
18287                    "       // second line\n"
18288                    "       // third line",
18289                    Style));
18290 
18291   Style.PenaltyExcessCharacter = 90;
18292   verifyFormat("int a; // the comment", Style);
18293   EXPECT_EQ("int a; // the comment\n"
18294             "       // aaa",
18295             format("int a; // the comment aaa", Style));
18296   EXPECT_EQ("int a; /* first line\n"
18297             "        * second line\n"
18298             "        * third line\n"
18299             "        */",
18300             format("int a; /* first line\n"
18301                    "        * second line\n"
18302                    "        * third line\n"
18303                    "        */",
18304                    Style));
18305   EXPECT_EQ("int a; // first line\n"
18306             "       // second line\n"
18307             "       // third line",
18308             format("int a; // first line\n"
18309                    "       // second line\n"
18310                    "       // third line",
18311                    Style));
18312   // FIXME: Investigate why this is not getting the same layout as the test
18313   // above.
18314   EXPECT_EQ("int a; /* first line\n"
18315             "        * second line\n"
18316             "        * third line\n"
18317             "        */",
18318             format("int a; /* first line second line third line"
18319                    "\n*/",
18320                    Style));
18321 
18322   EXPECT_EQ("// foo bar baz bazfoo\n"
18323             "// foo bar foo bar\n",
18324             format("// foo bar baz bazfoo\n"
18325                    "// foo bar foo           bar\n",
18326                    Style));
18327   EXPECT_EQ("// foo bar baz bazfoo\n"
18328             "// foo bar foo bar\n",
18329             format("// foo bar baz      bazfoo\n"
18330                    "// foo            bar foo bar\n",
18331                    Style));
18332 
18333   // FIXME: Optimally, we'd keep bazfoo on the first line and reflow bar to the
18334   // next one.
18335   EXPECT_EQ("// foo bar baz bazfoo\n"
18336             "// bar foo bar\n",
18337             format("// foo bar baz      bazfoo bar\n"
18338                    "// foo            bar\n",
18339                    Style));
18340 
18341   EXPECT_EQ("// foo bar baz bazfoo\n"
18342             "// foo bar baz bazfoo\n"
18343             "// bar foo bar\n",
18344             format("// foo bar baz      bazfoo\n"
18345                    "// foo bar baz      bazfoo bar\n"
18346                    "// foo bar\n",
18347                    Style));
18348 
18349   EXPECT_EQ("// foo bar baz bazfoo\n"
18350             "// foo bar baz bazfoo\n"
18351             "// bar foo bar\n",
18352             format("// foo bar baz      bazfoo\n"
18353                    "// foo bar baz      bazfoo bar\n"
18354                    "// foo           bar\n",
18355                    Style));
18356 
18357   // Make sure we do not keep protruding characters if strict mode reflow is
18358   // cheaper than keeping protruding characters.
18359   Style.ColumnLimit = 21;
18360   EXPECT_EQ(
18361       "// foo foo foo foo\n"
18362       "// foo foo foo foo\n"
18363       "// foo foo foo foo\n",
18364       format("// foo foo foo foo foo foo foo foo foo foo foo foo\n", Style));
18365 
18366   EXPECT_EQ("int a = /* long block\n"
18367             "           comment */\n"
18368             "    42;",
18369             format("int a = /* long block comment */ 42;", Style));
18370 }
18371 
18372 #define EXPECT_ALL_STYLES_EQUAL(Styles)                                        \
18373   for (size_t i = 1; i < Styles.size(); ++i)                                   \
18374   EXPECT_EQ(Styles[0], Styles[i])                                              \
18375       << "Style #" << i << " of " << Styles.size() << " differs from Style #0"
18376 
18377 TEST_F(FormatTest, GetsPredefinedStyleByName) {
18378   SmallVector<FormatStyle, 3> Styles;
18379   Styles.resize(3);
18380 
18381   Styles[0] = getLLVMStyle();
18382   EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1]));
18383   EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2]));
18384   EXPECT_ALL_STYLES_EQUAL(Styles);
18385 
18386   Styles[0] = getGoogleStyle();
18387   EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1]));
18388   EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2]));
18389   EXPECT_ALL_STYLES_EQUAL(Styles);
18390 
18391   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
18392   EXPECT_TRUE(
18393       getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1]));
18394   EXPECT_TRUE(
18395       getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2]));
18396   EXPECT_ALL_STYLES_EQUAL(Styles);
18397 
18398   Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp);
18399   EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1]));
18400   EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2]));
18401   EXPECT_ALL_STYLES_EQUAL(Styles);
18402 
18403   Styles[0] = getMozillaStyle();
18404   EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1]));
18405   EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2]));
18406   EXPECT_ALL_STYLES_EQUAL(Styles);
18407 
18408   Styles[0] = getWebKitStyle();
18409   EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1]));
18410   EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2]));
18411   EXPECT_ALL_STYLES_EQUAL(Styles);
18412 
18413   Styles[0] = getGNUStyle();
18414   EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1]));
18415   EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2]));
18416   EXPECT_ALL_STYLES_EQUAL(Styles);
18417 
18418   EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0]));
18419 }
18420 
18421 TEST_F(FormatTest, GetsCorrectBasedOnStyle) {
18422   SmallVector<FormatStyle, 8> Styles;
18423   Styles.resize(2);
18424 
18425   Styles[0] = getGoogleStyle();
18426   Styles[1] = getLLVMStyle();
18427   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
18428   EXPECT_ALL_STYLES_EQUAL(Styles);
18429 
18430   Styles.resize(5);
18431   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
18432   Styles[1] = getLLVMStyle();
18433   Styles[1].Language = FormatStyle::LK_JavaScript;
18434   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
18435 
18436   Styles[2] = getLLVMStyle();
18437   Styles[2].Language = FormatStyle::LK_JavaScript;
18438   EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n"
18439                                   "BasedOnStyle: Google",
18440                                   &Styles[2])
18441                    .value());
18442 
18443   Styles[3] = getLLVMStyle();
18444   Styles[3].Language = FormatStyle::LK_JavaScript;
18445   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n"
18446                                   "Language: JavaScript",
18447                                   &Styles[3])
18448                    .value());
18449 
18450   Styles[4] = getLLVMStyle();
18451   Styles[4].Language = FormatStyle::LK_JavaScript;
18452   EXPECT_EQ(0, parseConfiguration("---\n"
18453                                   "BasedOnStyle: LLVM\n"
18454                                   "IndentWidth: 123\n"
18455                                   "---\n"
18456                                   "BasedOnStyle: Google\n"
18457                                   "Language: JavaScript",
18458                                   &Styles[4])
18459                    .value());
18460   EXPECT_ALL_STYLES_EQUAL(Styles);
18461 }
18462 
18463 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME)                             \
18464   Style.FIELD = false;                                                         \
18465   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value());      \
18466   EXPECT_TRUE(Style.FIELD);                                                    \
18467   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value());     \
18468   EXPECT_FALSE(Style.FIELD);
18469 
18470 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD)
18471 
18472 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME)              \
18473   Style.STRUCT.FIELD = false;                                                  \
18474   EXPECT_EQ(0,                                                                 \
18475             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": true", &Style)   \
18476                 .value());                                                     \
18477   EXPECT_TRUE(Style.STRUCT.FIELD);                                             \
18478   EXPECT_EQ(0,                                                                 \
18479             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": false", &Style)  \
18480                 .value());                                                     \
18481   EXPECT_FALSE(Style.STRUCT.FIELD);
18482 
18483 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD)                                 \
18484   CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD)
18485 
18486 #define CHECK_PARSE(TEXT, FIELD, VALUE)                                        \
18487   EXPECT_NE(VALUE, Style.FIELD) << "Initial value already the same!";          \
18488   EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value());                      \
18489   EXPECT_EQ(VALUE, Style.FIELD) << "Unexpected value after parsing!"
18490 
18491 TEST_F(FormatTest, ParsesConfigurationBools) {
18492   FormatStyle Style = {};
18493   Style.Language = FormatStyle::LK_Cpp;
18494   CHECK_PARSE_BOOL(AlignTrailingComments);
18495   CHECK_PARSE_BOOL(AllowAllArgumentsOnNextLine);
18496   CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
18497   CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine);
18498   CHECK_PARSE_BOOL(AllowShortEnumsOnASingleLine);
18499   CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
18500   CHECK_PARSE_BOOL(BinPackArguments);
18501   CHECK_PARSE_BOOL(BinPackParameters);
18502   CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations);
18503   CHECK_PARSE_BOOL(BreakBeforeConceptDeclarations);
18504   CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
18505   CHECK_PARSE_BOOL(BreakStringLiterals);
18506   CHECK_PARSE_BOOL(CompactNamespaces);
18507   CHECK_PARSE_BOOL(DeriveLineEnding);
18508   CHECK_PARSE_BOOL(DerivePointerAlignment);
18509   CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding");
18510   CHECK_PARSE_BOOL(DisableFormat);
18511   CHECK_PARSE_BOOL(IndentAccessModifiers);
18512   CHECK_PARSE_BOOL(IndentCaseLabels);
18513   CHECK_PARSE_BOOL(IndentCaseBlocks);
18514   CHECK_PARSE_BOOL(IndentGotoLabels);
18515   CHECK_PARSE_BOOL(IndentRequires);
18516   CHECK_PARSE_BOOL(IndentWrappedFunctionNames);
18517   CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks);
18518   CHECK_PARSE_BOOL(ObjCSpaceAfterProperty);
18519   CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
18520   CHECK_PARSE_BOOL(Cpp11BracedListStyle);
18521   CHECK_PARSE_BOOL(ReflowComments);
18522   CHECK_PARSE_BOOL(SortUsingDeclarations);
18523   CHECK_PARSE_BOOL(SpacesInParentheses);
18524   CHECK_PARSE_BOOL(SpacesInSquareBrackets);
18525   CHECK_PARSE_BOOL(SpacesInConditionalStatement);
18526   CHECK_PARSE_BOOL(SpaceInEmptyBlock);
18527   CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
18528   CHECK_PARSE_BOOL(SpacesInContainerLiterals);
18529   CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
18530   CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
18531   CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword);
18532   CHECK_PARSE_BOOL(SpaceAfterLogicalNot);
18533   CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
18534   CHECK_PARSE_BOOL(SpaceBeforeCaseColon);
18535   CHECK_PARSE_BOOL(SpaceBeforeCpp11BracedList);
18536   CHECK_PARSE_BOOL(SpaceBeforeCtorInitializerColon);
18537   CHECK_PARSE_BOOL(SpaceBeforeInheritanceColon);
18538   CHECK_PARSE_BOOL(SpaceBeforeRangeBasedForLoopColon);
18539   CHECK_PARSE_BOOL(SpaceBeforeSquareBrackets);
18540   CHECK_PARSE_BOOL(UseCRLF);
18541 
18542   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterCaseLabel);
18543   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass);
18544   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum);
18545   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction);
18546   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace);
18547   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration);
18548   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct);
18549   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion);
18550   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterExternBlock);
18551   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch);
18552   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse);
18553   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeLambdaBody);
18554   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeWhile);
18555   CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces);
18556   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyFunction);
18557   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyRecord);
18558   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyNamespace);
18559 }
18560 
18561 #undef CHECK_PARSE_BOOL
18562 
18563 TEST_F(FormatTest, ParsesConfiguration) {
18564   FormatStyle Style = {};
18565   Style.Language = FormatStyle::LK_Cpp;
18566   CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
18567   CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",
18568               ConstructorInitializerIndentWidth, 1234u);
18569   CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u);
18570   CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
18571   CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
18572   CHECK_PARSE("PenaltyBreakAssignment: 1234", PenaltyBreakAssignment, 1234u);
18573   CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234",
18574               PenaltyBreakBeforeFirstCallParameter, 1234u);
18575   CHECK_PARSE("PenaltyBreakTemplateDeclaration: 1234",
18576               PenaltyBreakTemplateDeclaration, 1234u);
18577   CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
18578   CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
18579               PenaltyReturnTypeOnItsOwnLine, 1234u);
18580   CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
18581               SpacesBeforeTrailingComments, 1234u);
18582   CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
18583   CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u);
18584   CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$");
18585 
18586   Style.QualifierAlignment = FormatStyle::QAS_Right;
18587   CHECK_PARSE("QualifierAlignment: Leave", QualifierAlignment,
18588               FormatStyle::QAS_Leave);
18589   CHECK_PARSE("QualifierAlignment: Right", QualifierAlignment,
18590               FormatStyle::QAS_Right);
18591   CHECK_PARSE("QualifierAlignment: Left", QualifierAlignment,
18592               FormatStyle::QAS_Left);
18593   CHECK_PARSE("QualifierAlignment: Custom", QualifierAlignment,
18594               FormatStyle::QAS_Custom);
18595 
18596   Style.QualifierOrder.clear();
18597   CHECK_PARSE("QualifierOrder: [ const, volatile, type ]", QualifierOrder,
18598               std::vector<std::string>({"const", "volatile", "type"}));
18599   Style.QualifierOrder.clear();
18600   CHECK_PARSE("QualifierOrder: [const, type]", QualifierOrder,
18601               std::vector<std::string>({"const", "type"}));
18602   Style.QualifierOrder.clear();
18603   CHECK_PARSE("QualifierOrder: [volatile, type]", QualifierOrder,
18604               std::vector<std::string>({"volatile", "type"}));
18605 
18606   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
18607   CHECK_PARSE("AlignConsecutiveAssignments: None", AlignConsecutiveAssignments,
18608               FormatStyle::ACS_None);
18609   CHECK_PARSE("AlignConsecutiveAssignments: Consecutive",
18610               AlignConsecutiveAssignments, FormatStyle::ACS_Consecutive);
18611   CHECK_PARSE("AlignConsecutiveAssignments: AcrossEmptyLines",
18612               AlignConsecutiveAssignments, FormatStyle::ACS_AcrossEmptyLines);
18613   CHECK_PARSE("AlignConsecutiveAssignments: AcrossEmptyLinesAndComments",
18614               AlignConsecutiveAssignments,
18615               FormatStyle::ACS_AcrossEmptyLinesAndComments);
18616   // For backwards compability, false / true should still parse
18617   CHECK_PARSE("AlignConsecutiveAssignments: false", AlignConsecutiveAssignments,
18618               FormatStyle::ACS_None);
18619   CHECK_PARSE("AlignConsecutiveAssignments: true", AlignConsecutiveAssignments,
18620               FormatStyle::ACS_Consecutive);
18621 
18622   Style.AlignConsecutiveBitFields = FormatStyle::ACS_Consecutive;
18623   CHECK_PARSE("AlignConsecutiveBitFields: None", AlignConsecutiveBitFields,
18624               FormatStyle::ACS_None);
18625   CHECK_PARSE("AlignConsecutiveBitFields: Consecutive",
18626               AlignConsecutiveBitFields, FormatStyle::ACS_Consecutive);
18627   CHECK_PARSE("AlignConsecutiveBitFields: AcrossEmptyLines",
18628               AlignConsecutiveBitFields, FormatStyle::ACS_AcrossEmptyLines);
18629   CHECK_PARSE("AlignConsecutiveBitFields: AcrossEmptyLinesAndComments",
18630               AlignConsecutiveBitFields,
18631               FormatStyle::ACS_AcrossEmptyLinesAndComments);
18632   // For backwards compability, false / true should still parse
18633   CHECK_PARSE("AlignConsecutiveBitFields: false", AlignConsecutiveBitFields,
18634               FormatStyle::ACS_None);
18635   CHECK_PARSE("AlignConsecutiveBitFields: true", AlignConsecutiveBitFields,
18636               FormatStyle::ACS_Consecutive);
18637 
18638   Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
18639   CHECK_PARSE("AlignConsecutiveMacros: None", AlignConsecutiveMacros,
18640               FormatStyle::ACS_None);
18641   CHECK_PARSE("AlignConsecutiveMacros: Consecutive", AlignConsecutiveMacros,
18642               FormatStyle::ACS_Consecutive);
18643   CHECK_PARSE("AlignConsecutiveMacros: AcrossEmptyLines",
18644               AlignConsecutiveMacros, FormatStyle::ACS_AcrossEmptyLines);
18645   CHECK_PARSE("AlignConsecutiveMacros: AcrossEmptyLinesAndComments",
18646               AlignConsecutiveMacros,
18647               FormatStyle::ACS_AcrossEmptyLinesAndComments);
18648   // For backwards compability, false / true should still parse
18649   CHECK_PARSE("AlignConsecutiveMacros: false", AlignConsecutiveMacros,
18650               FormatStyle::ACS_None);
18651   CHECK_PARSE("AlignConsecutiveMacros: true", AlignConsecutiveMacros,
18652               FormatStyle::ACS_Consecutive);
18653 
18654   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
18655   CHECK_PARSE("AlignConsecutiveDeclarations: None",
18656               AlignConsecutiveDeclarations, FormatStyle::ACS_None);
18657   CHECK_PARSE("AlignConsecutiveDeclarations: Consecutive",
18658               AlignConsecutiveDeclarations, FormatStyle::ACS_Consecutive);
18659   CHECK_PARSE("AlignConsecutiveDeclarations: AcrossEmptyLines",
18660               AlignConsecutiveDeclarations, FormatStyle::ACS_AcrossEmptyLines);
18661   CHECK_PARSE("AlignConsecutiveDeclarations: AcrossEmptyLinesAndComments",
18662               AlignConsecutiveDeclarations,
18663               FormatStyle::ACS_AcrossEmptyLinesAndComments);
18664   // For backwards compability, false / true should still parse
18665   CHECK_PARSE("AlignConsecutiveDeclarations: false",
18666               AlignConsecutiveDeclarations, FormatStyle::ACS_None);
18667   CHECK_PARSE("AlignConsecutiveDeclarations: true",
18668               AlignConsecutiveDeclarations, FormatStyle::ACS_Consecutive);
18669 
18670   Style.PointerAlignment = FormatStyle::PAS_Middle;
18671   CHECK_PARSE("PointerAlignment: Left", PointerAlignment,
18672               FormatStyle::PAS_Left);
18673   CHECK_PARSE("PointerAlignment: Right", PointerAlignment,
18674               FormatStyle::PAS_Right);
18675   CHECK_PARSE("PointerAlignment: Middle", PointerAlignment,
18676               FormatStyle::PAS_Middle);
18677   Style.ReferenceAlignment = FormatStyle::RAS_Middle;
18678   CHECK_PARSE("ReferenceAlignment: Pointer", ReferenceAlignment,
18679               FormatStyle::RAS_Pointer);
18680   CHECK_PARSE("ReferenceAlignment: Left", ReferenceAlignment,
18681               FormatStyle::RAS_Left);
18682   CHECK_PARSE("ReferenceAlignment: Right", ReferenceAlignment,
18683               FormatStyle::RAS_Right);
18684   CHECK_PARSE("ReferenceAlignment: Middle", ReferenceAlignment,
18685               FormatStyle::RAS_Middle);
18686   // For backward compatibility:
18687   CHECK_PARSE("PointerBindsToType: Left", PointerAlignment,
18688               FormatStyle::PAS_Left);
18689   CHECK_PARSE("PointerBindsToType: Right", PointerAlignment,
18690               FormatStyle::PAS_Right);
18691   CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment,
18692               FormatStyle::PAS_Middle);
18693 
18694   Style.Standard = FormatStyle::LS_Auto;
18695   CHECK_PARSE("Standard: c++03", Standard, FormatStyle::LS_Cpp03);
18696   CHECK_PARSE("Standard: c++11", Standard, FormatStyle::LS_Cpp11);
18697   CHECK_PARSE("Standard: c++14", Standard, FormatStyle::LS_Cpp14);
18698   CHECK_PARSE("Standard: c++17", Standard, FormatStyle::LS_Cpp17);
18699   CHECK_PARSE("Standard: c++20", Standard, FormatStyle::LS_Cpp20);
18700   CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
18701   CHECK_PARSE("Standard: Latest", Standard, FormatStyle::LS_Latest);
18702   // Legacy aliases:
18703   CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03);
18704   CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Latest);
18705   CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
18706   CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
18707 
18708   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
18709   CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment",
18710               BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment);
18711   CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators,
18712               FormatStyle::BOS_None);
18713   CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators,
18714               FormatStyle::BOS_All);
18715   // For backward compatibility:
18716   CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators,
18717               FormatStyle::BOS_None);
18718   CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators,
18719               FormatStyle::BOS_All);
18720 
18721   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
18722   CHECK_PARSE("BreakConstructorInitializers: BeforeComma",
18723               BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma);
18724   CHECK_PARSE("BreakConstructorInitializers: AfterColon",
18725               BreakConstructorInitializers, FormatStyle::BCIS_AfterColon);
18726   CHECK_PARSE("BreakConstructorInitializers: BeforeColon",
18727               BreakConstructorInitializers, FormatStyle::BCIS_BeforeColon);
18728   // For backward compatibility:
18729   CHECK_PARSE("BreakConstructorInitializersBeforeComma: true",
18730               BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma);
18731 
18732   Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
18733   CHECK_PARSE("BreakInheritanceList: AfterComma", BreakInheritanceList,
18734               FormatStyle::BILS_AfterComma);
18735   CHECK_PARSE("BreakInheritanceList: BeforeComma", BreakInheritanceList,
18736               FormatStyle::BILS_BeforeComma);
18737   CHECK_PARSE("BreakInheritanceList: AfterColon", BreakInheritanceList,
18738               FormatStyle::BILS_AfterColon);
18739   CHECK_PARSE("BreakInheritanceList: BeforeColon", BreakInheritanceList,
18740               FormatStyle::BILS_BeforeColon);
18741   // For backward compatibility:
18742   CHECK_PARSE("BreakBeforeInheritanceComma: true", BreakInheritanceList,
18743               FormatStyle::BILS_BeforeComma);
18744 
18745   Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack;
18746   CHECK_PARSE("PackConstructorInitializers: Never", PackConstructorInitializers,
18747               FormatStyle::PCIS_Never);
18748   CHECK_PARSE("PackConstructorInitializers: BinPack",
18749               PackConstructorInitializers, FormatStyle::PCIS_BinPack);
18750   CHECK_PARSE("PackConstructorInitializers: CurrentLine",
18751               PackConstructorInitializers, FormatStyle::PCIS_CurrentLine);
18752   CHECK_PARSE("PackConstructorInitializers: NextLine",
18753               PackConstructorInitializers, FormatStyle::PCIS_NextLine);
18754   // For backward compatibility:
18755   CHECK_PARSE("BasedOnStyle: Google\n"
18756               "ConstructorInitializerAllOnOneLineOrOnePerLine: true\n"
18757               "AllowAllConstructorInitializersOnNextLine: false",
18758               PackConstructorInitializers, FormatStyle::PCIS_CurrentLine);
18759   Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
18760   CHECK_PARSE("BasedOnStyle: Google\n"
18761               "ConstructorInitializerAllOnOneLineOrOnePerLine: false",
18762               PackConstructorInitializers, FormatStyle::PCIS_BinPack);
18763   CHECK_PARSE("ConstructorInitializerAllOnOneLineOrOnePerLine: true\n"
18764               "AllowAllConstructorInitializersOnNextLine: true",
18765               PackConstructorInitializers, FormatStyle::PCIS_NextLine);
18766   Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack;
18767   CHECK_PARSE("ConstructorInitializerAllOnOneLineOrOnePerLine: true\n"
18768               "AllowAllConstructorInitializersOnNextLine: false",
18769               PackConstructorInitializers, FormatStyle::PCIS_CurrentLine);
18770 
18771   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
18772   CHECK_PARSE("EmptyLineBeforeAccessModifier: Never",
18773               EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Never);
18774   CHECK_PARSE("EmptyLineBeforeAccessModifier: Leave",
18775               EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Leave);
18776   CHECK_PARSE("EmptyLineBeforeAccessModifier: LogicalBlock",
18777               EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_LogicalBlock);
18778   CHECK_PARSE("EmptyLineBeforeAccessModifier: Always",
18779               EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Always);
18780 
18781   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
18782   CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket,
18783               FormatStyle::BAS_Align);
18784   CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket,
18785               FormatStyle::BAS_DontAlign);
18786   CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket,
18787               FormatStyle::BAS_AlwaysBreak);
18788   // For backward compatibility:
18789   CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket,
18790               FormatStyle::BAS_DontAlign);
18791   CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket,
18792               FormatStyle::BAS_Align);
18793 
18794   Style.AlignEscapedNewlines = FormatStyle::ENAS_Left;
18795   CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines,
18796               FormatStyle::ENAS_DontAlign);
18797   CHECK_PARSE("AlignEscapedNewlines: Left", AlignEscapedNewlines,
18798               FormatStyle::ENAS_Left);
18799   CHECK_PARSE("AlignEscapedNewlines: Right", AlignEscapedNewlines,
18800               FormatStyle::ENAS_Right);
18801   // For backward compatibility:
18802   CHECK_PARSE("AlignEscapedNewlinesLeft: true", AlignEscapedNewlines,
18803               FormatStyle::ENAS_Left);
18804   CHECK_PARSE("AlignEscapedNewlinesLeft: false", AlignEscapedNewlines,
18805               FormatStyle::ENAS_Right);
18806 
18807   Style.AlignOperands = FormatStyle::OAS_Align;
18808   CHECK_PARSE("AlignOperands: DontAlign", AlignOperands,
18809               FormatStyle::OAS_DontAlign);
18810   CHECK_PARSE("AlignOperands: Align", AlignOperands, FormatStyle::OAS_Align);
18811   CHECK_PARSE("AlignOperands: AlignAfterOperator", AlignOperands,
18812               FormatStyle::OAS_AlignAfterOperator);
18813   // For backward compatibility:
18814   CHECK_PARSE("AlignOperands: false", AlignOperands,
18815               FormatStyle::OAS_DontAlign);
18816   CHECK_PARSE("AlignOperands: true", AlignOperands, FormatStyle::OAS_Align);
18817 
18818   Style.UseTab = FormatStyle::UT_ForIndentation;
18819   CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
18820   CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
18821   CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
18822   CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab,
18823               FormatStyle::UT_ForContinuationAndIndentation);
18824   CHECK_PARSE("UseTab: AlignWithSpaces", UseTab,
18825               FormatStyle::UT_AlignWithSpaces);
18826   // For backward compatibility:
18827   CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never);
18828   CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always);
18829 
18830   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty;
18831   CHECK_PARSE("AllowShortBlocksOnASingleLine: Never",
18832               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never);
18833   CHECK_PARSE("AllowShortBlocksOnASingleLine: Empty",
18834               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Empty);
18835   CHECK_PARSE("AllowShortBlocksOnASingleLine: Always",
18836               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always);
18837   // For backward compatibility:
18838   CHECK_PARSE("AllowShortBlocksOnASingleLine: false",
18839               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never);
18840   CHECK_PARSE("AllowShortBlocksOnASingleLine: true",
18841               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always);
18842 
18843   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
18844   CHECK_PARSE("AllowShortFunctionsOnASingleLine: None",
18845               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
18846   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline",
18847               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline);
18848   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty",
18849               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty);
18850   CHECK_PARSE("AllowShortFunctionsOnASingleLine: All",
18851               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
18852   // For backward compatibility:
18853   CHECK_PARSE("AllowShortFunctionsOnASingleLine: false",
18854               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
18855   CHECK_PARSE("AllowShortFunctionsOnASingleLine: true",
18856               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
18857 
18858   Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Both;
18859   CHECK_PARSE("SpaceAroundPointerQualifiers: Default",
18860               SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Default);
18861   CHECK_PARSE("SpaceAroundPointerQualifiers: Before",
18862               SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Before);
18863   CHECK_PARSE("SpaceAroundPointerQualifiers: After",
18864               SpaceAroundPointerQualifiers, FormatStyle::SAPQ_After);
18865   CHECK_PARSE("SpaceAroundPointerQualifiers: Both",
18866               SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Both);
18867 
18868   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
18869   CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens,
18870               FormatStyle::SBPO_Never);
18871   CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens,
18872               FormatStyle::SBPO_Always);
18873   CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens,
18874               FormatStyle::SBPO_ControlStatements);
18875   CHECK_PARSE("SpaceBeforeParens: ControlStatementsExceptControlMacros",
18876               SpaceBeforeParens,
18877               FormatStyle::SBPO_ControlStatementsExceptControlMacros);
18878   CHECK_PARSE("SpaceBeforeParens: NonEmptyParentheses", SpaceBeforeParens,
18879               FormatStyle::SBPO_NonEmptyParentheses);
18880   CHECK_PARSE("SpaceBeforeParens: Custom", SpaceBeforeParens,
18881               FormatStyle::SBPO_Custom);
18882   // For backward compatibility:
18883   CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens,
18884               FormatStyle::SBPO_Never);
18885   CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens,
18886               FormatStyle::SBPO_ControlStatements);
18887   CHECK_PARSE("SpaceBeforeParens: ControlStatementsExceptForEachMacros",
18888               SpaceBeforeParens,
18889               FormatStyle::SBPO_ControlStatementsExceptControlMacros);
18890 
18891   Style.ColumnLimit = 123;
18892   FormatStyle BaseStyle = getLLVMStyle();
18893   CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
18894   CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
18895 
18896   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
18897   CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
18898               FormatStyle::BS_Attach);
18899   CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
18900               FormatStyle::BS_Linux);
18901   CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces,
18902               FormatStyle::BS_Mozilla);
18903   CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
18904               FormatStyle::BS_Stroustrup);
18905   CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces,
18906               FormatStyle::BS_Allman);
18907   CHECK_PARSE("BreakBeforeBraces: Whitesmiths", BreakBeforeBraces,
18908               FormatStyle::BS_Whitesmiths);
18909   CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU);
18910   CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces,
18911               FormatStyle::BS_WebKit);
18912   CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces,
18913               FormatStyle::BS_Custom);
18914 
18915   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never;
18916   CHECK_PARSE("BraceWrapping:\n"
18917               "  AfterControlStatement: MultiLine",
18918               BraceWrapping.AfterControlStatement,
18919               FormatStyle::BWACS_MultiLine);
18920   CHECK_PARSE("BraceWrapping:\n"
18921               "  AfterControlStatement: Always",
18922               BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always);
18923   CHECK_PARSE("BraceWrapping:\n"
18924               "  AfterControlStatement: Never",
18925               BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never);
18926   // For backward compatibility:
18927   CHECK_PARSE("BraceWrapping:\n"
18928               "  AfterControlStatement: true",
18929               BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always);
18930   CHECK_PARSE("BraceWrapping:\n"
18931               "  AfterControlStatement: false",
18932               BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never);
18933 
18934   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
18935   CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType,
18936               FormatStyle::RTBS_None);
18937   CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType,
18938               FormatStyle::RTBS_All);
18939   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel",
18940               AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel);
18941   CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions",
18942               AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions);
18943   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions",
18944               AlwaysBreakAfterReturnType,
18945               FormatStyle::RTBS_TopLevelDefinitions);
18946 
18947   Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
18948   CHECK_PARSE("AlwaysBreakTemplateDeclarations: No",
18949               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_No);
18950   CHECK_PARSE("AlwaysBreakTemplateDeclarations: MultiLine",
18951               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine);
18952   CHECK_PARSE("AlwaysBreakTemplateDeclarations: Yes",
18953               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes);
18954   CHECK_PARSE("AlwaysBreakTemplateDeclarations: false",
18955               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine);
18956   CHECK_PARSE("AlwaysBreakTemplateDeclarations: true",
18957               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes);
18958 
18959   Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
18960   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None",
18961               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None);
18962   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All",
18963               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All);
18964   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel",
18965               AlwaysBreakAfterDefinitionReturnType,
18966               FormatStyle::DRTBS_TopLevel);
18967 
18968   Style.NamespaceIndentation = FormatStyle::NI_All;
18969   CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
18970               FormatStyle::NI_None);
18971   CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation,
18972               FormatStyle::NI_Inner);
18973   CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
18974               FormatStyle::NI_All);
18975 
18976   Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_OnlyFirstIf;
18977   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Never",
18978               AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never);
18979   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: WithoutElse",
18980               AllowShortIfStatementsOnASingleLine,
18981               FormatStyle::SIS_WithoutElse);
18982   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: OnlyFirstIf",
18983               AllowShortIfStatementsOnASingleLine,
18984               FormatStyle::SIS_OnlyFirstIf);
18985   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: AllIfsAndElse",
18986               AllowShortIfStatementsOnASingleLine,
18987               FormatStyle::SIS_AllIfsAndElse);
18988   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Always",
18989               AllowShortIfStatementsOnASingleLine,
18990               FormatStyle::SIS_OnlyFirstIf);
18991   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: false",
18992               AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never);
18993   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: true",
18994               AllowShortIfStatementsOnASingleLine,
18995               FormatStyle::SIS_WithoutElse);
18996 
18997   Style.IndentExternBlock = FormatStyle::IEBS_NoIndent;
18998   CHECK_PARSE("IndentExternBlock: AfterExternBlock", IndentExternBlock,
18999               FormatStyle::IEBS_AfterExternBlock);
19000   CHECK_PARSE("IndentExternBlock: Indent", IndentExternBlock,
19001               FormatStyle::IEBS_Indent);
19002   CHECK_PARSE("IndentExternBlock: NoIndent", IndentExternBlock,
19003               FormatStyle::IEBS_NoIndent);
19004   CHECK_PARSE("IndentExternBlock: true", IndentExternBlock,
19005               FormatStyle::IEBS_Indent);
19006   CHECK_PARSE("IndentExternBlock: false", IndentExternBlock,
19007               FormatStyle::IEBS_NoIndent);
19008 
19009   Style.BitFieldColonSpacing = FormatStyle::BFCS_None;
19010   CHECK_PARSE("BitFieldColonSpacing: Both", BitFieldColonSpacing,
19011               FormatStyle::BFCS_Both);
19012   CHECK_PARSE("BitFieldColonSpacing: None", BitFieldColonSpacing,
19013               FormatStyle::BFCS_None);
19014   CHECK_PARSE("BitFieldColonSpacing: Before", BitFieldColonSpacing,
19015               FormatStyle::BFCS_Before);
19016   CHECK_PARSE("BitFieldColonSpacing: After", BitFieldColonSpacing,
19017               FormatStyle::BFCS_After);
19018 
19019   Style.SortJavaStaticImport = FormatStyle::SJSIO_Before;
19020   CHECK_PARSE("SortJavaStaticImport: After", SortJavaStaticImport,
19021               FormatStyle::SJSIO_After);
19022   CHECK_PARSE("SortJavaStaticImport: Before", SortJavaStaticImport,
19023               FormatStyle::SJSIO_Before);
19024 
19025   // FIXME: This is required because parsing a configuration simply overwrites
19026   // the first N elements of the list instead of resetting it.
19027   Style.ForEachMacros.clear();
19028   std::vector<std::string> BoostForeach;
19029   BoostForeach.push_back("BOOST_FOREACH");
19030   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach);
19031   std::vector<std::string> BoostAndQForeach;
19032   BoostAndQForeach.push_back("BOOST_FOREACH");
19033   BoostAndQForeach.push_back("Q_FOREACH");
19034   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
19035               BoostAndQForeach);
19036 
19037   Style.IfMacros.clear();
19038   std::vector<std::string> CustomIfs;
19039   CustomIfs.push_back("MYIF");
19040   CHECK_PARSE("IfMacros: [MYIF]", IfMacros, CustomIfs);
19041 
19042   Style.AttributeMacros.clear();
19043   CHECK_PARSE("BasedOnStyle: LLVM", AttributeMacros,
19044               std::vector<std::string>{"__capability"});
19045   CHECK_PARSE("AttributeMacros: [attr1, attr2]", AttributeMacros,
19046               std::vector<std::string>({"attr1", "attr2"}));
19047 
19048   Style.StatementAttributeLikeMacros.clear();
19049   CHECK_PARSE("StatementAttributeLikeMacros: [emit,Q_EMIT]",
19050               StatementAttributeLikeMacros,
19051               std::vector<std::string>({"emit", "Q_EMIT"}));
19052 
19053   Style.StatementMacros.clear();
19054   CHECK_PARSE("StatementMacros: [QUNUSED]", StatementMacros,
19055               std::vector<std::string>{"QUNUSED"});
19056   CHECK_PARSE("StatementMacros: [QUNUSED, QT_REQUIRE_VERSION]", StatementMacros,
19057               std::vector<std::string>({"QUNUSED", "QT_REQUIRE_VERSION"}));
19058 
19059   Style.NamespaceMacros.clear();
19060   CHECK_PARSE("NamespaceMacros: [TESTSUITE]", NamespaceMacros,
19061               std::vector<std::string>{"TESTSUITE"});
19062   CHECK_PARSE("NamespaceMacros: [TESTSUITE, SUITE]", NamespaceMacros,
19063               std::vector<std::string>({"TESTSUITE", "SUITE"}));
19064 
19065   Style.WhitespaceSensitiveMacros.clear();
19066   CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE]",
19067               WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"});
19068   CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE, ASSERT]",
19069               WhitespaceSensitiveMacros,
19070               std::vector<std::string>({"STRINGIZE", "ASSERT"}));
19071   Style.WhitespaceSensitiveMacros.clear();
19072   CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE']",
19073               WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"});
19074   CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE', 'ASSERT']",
19075               WhitespaceSensitiveMacros,
19076               std::vector<std::string>({"STRINGIZE", "ASSERT"}));
19077 
19078   Style.IncludeStyle.IncludeCategories.clear();
19079   std::vector<tooling::IncludeStyle::IncludeCategory> ExpectedCategories = {
19080       {"abc/.*", 2, 0, false}, {".*", 1, 0, true}};
19081   CHECK_PARSE("IncludeCategories:\n"
19082               "  - Regex: abc/.*\n"
19083               "    Priority: 2\n"
19084               "  - Regex: .*\n"
19085               "    Priority: 1\n"
19086               "    CaseSensitive: true\n",
19087               IncludeStyle.IncludeCategories, ExpectedCategories);
19088   CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeStyle.IncludeIsMainRegex,
19089               "abc$");
19090   CHECK_PARSE("IncludeIsMainSourceRegex: 'abc$'",
19091               IncludeStyle.IncludeIsMainSourceRegex, "abc$");
19092 
19093   Style.SortIncludes = FormatStyle::SI_Never;
19094   CHECK_PARSE("SortIncludes: true", SortIncludes,
19095               FormatStyle::SI_CaseSensitive);
19096   CHECK_PARSE("SortIncludes: false", SortIncludes, FormatStyle::SI_Never);
19097   CHECK_PARSE("SortIncludes: CaseInsensitive", SortIncludes,
19098               FormatStyle::SI_CaseInsensitive);
19099   CHECK_PARSE("SortIncludes: CaseSensitive", SortIncludes,
19100               FormatStyle::SI_CaseSensitive);
19101   CHECK_PARSE("SortIncludes: Never", SortIncludes, FormatStyle::SI_Never);
19102 
19103   Style.RawStringFormats.clear();
19104   std::vector<FormatStyle::RawStringFormat> ExpectedRawStringFormats = {
19105       {
19106           FormatStyle::LK_TextProto,
19107           {"pb", "proto"},
19108           {"PARSE_TEXT_PROTO"},
19109           /*CanonicalDelimiter=*/"",
19110           "llvm",
19111       },
19112       {
19113           FormatStyle::LK_Cpp,
19114           {"cc", "cpp"},
19115           {"C_CODEBLOCK", "CPPEVAL"},
19116           /*CanonicalDelimiter=*/"cc",
19117           /*BasedOnStyle=*/"",
19118       },
19119   };
19120 
19121   CHECK_PARSE("RawStringFormats:\n"
19122               "  - Language: TextProto\n"
19123               "    Delimiters:\n"
19124               "      - 'pb'\n"
19125               "      - 'proto'\n"
19126               "    EnclosingFunctions:\n"
19127               "      - 'PARSE_TEXT_PROTO'\n"
19128               "    BasedOnStyle: llvm\n"
19129               "  - Language: Cpp\n"
19130               "    Delimiters:\n"
19131               "      - 'cc'\n"
19132               "      - 'cpp'\n"
19133               "    EnclosingFunctions:\n"
19134               "      - 'C_CODEBLOCK'\n"
19135               "      - 'CPPEVAL'\n"
19136               "    CanonicalDelimiter: 'cc'",
19137               RawStringFormats, ExpectedRawStringFormats);
19138 
19139   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
19140               "  Minimum: 0\n"
19141               "  Maximum: 0",
19142               SpacesInLineCommentPrefix.Minimum, 0u);
19143   EXPECT_EQ(Style.SpacesInLineCommentPrefix.Maximum, 0u);
19144   Style.SpacesInLineCommentPrefix.Minimum = 1;
19145   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
19146               "  Minimum: 2",
19147               SpacesInLineCommentPrefix.Minimum, 0u);
19148   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
19149               "  Maximum: -1",
19150               SpacesInLineCommentPrefix.Maximum, -1u);
19151   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
19152               "  Minimum: 2",
19153               SpacesInLineCommentPrefix.Minimum, 2u);
19154   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
19155               "  Maximum: 1",
19156               SpacesInLineCommentPrefix.Maximum, 1u);
19157   EXPECT_EQ(Style.SpacesInLineCommentPrefix.Minimum, 1u);
19158 
19159   Style.SpacesInAngles = FormatStyle::SIAS_Always;
19160   CHECK_PARSE("SpacesInAngles: Never", SpacesInAngles, FormatStyle::SIAS_Never);
19161   CHECK_PARSE("SpacesInAngles: Always", SpacesInAngles,
19162               FormatStyle::SIAS_Always);
19163   CHECK_PARSE("SpacesInAngles: Leave", SpacesInAngles, FormatStyle::SIAS_Leave);
19164   // For backward compatibility:
19165   CHECK_PARSE("SpacesInAngles: false", SpacesInAngles, FormatStyle::SIAS_Never);
19166   CHECK_PARSE("SpacesInAngles: true", SpacesInAngles, FormatStyle::SIAS_Always);
19167 }
19168 
19169 TEST_F(FormatTest, ParsesConfigurationWithLanguages) {
19170   FormatStyle Style = {};
19171   Style.Language = FormatStyle::LK_Cpp;
19172   CHECK_PARSE("Language: Cpp\n"
19173               "IndentWidth: 12",
19174               IndentWidth, 12u);
19175   EXPECT_EQ(parseConfiguration("Language: JavaScript\n"
19176                                "IndentWidth: 34",
19177                                &Style),
19178             ParseError::Unsuitable);
19179   FormatStyle BinPackedTCS = {};
19180   BinPackedTCS.Language = FormatStyle::LK_JavaScript;
19181   EXPECT_EQ(parseConfiguration("BinPackArguments: true\n"
19182                                "InsertTrailingCommas: Wrapped",
19183                                &BinPackedTCS),
19184             ParseError::BinPackTrailingCommaConflict);
19185   EXPECT_EQ(12u, Style.IndentWidth);
19186   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
19187   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
19188 
19189   Style.Language = FormatStyle::LK_JavaScript;
19190   CHECK_PARSE("Language: JavaScript\n"
19191               "IndentWidth: 12",
19192               IndentWidth, 12u);
19193   CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u);
19194   EXPECT_EQ(parseConfiguration("Language: Cpp\n"
19195                                "IndentWidth: 34",
19196                                &Style),
19197             ParseError::Unsuitable);
19198   EXPECT_EQ(23u, Style.IndentWidth);
19199   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
19200   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
19201 
19202   CHECK_PARSE("BasedOnStyle: LLVM\n"
19203               "IndentWidth: 67",
19204               IndentWidth, 67u);
19205 
19206   CHECK_PARSE("---\n"
19207               "Language: JavaScript\n"
19208               "IndentWidth: 12\n"
19209               "---\n"
19210               "Language: Cpp\n"
19211               "IndentWidth: 34\n"
19212               "...\n",
19213               IndentWidth, 12u);
19214 
19215   Style.Language = FormatStyle::LK_Cpp;
19216   CHECK_PARSE("---\n"
19217               "Language: JavaScript\n"
19218               "IndentWidth: 12\n"
19219               "---\n"
19220               "Language: Cpp\n"
19221               "IndentWidth: 34\n"
19222               "...\n",
19223               IndentWidth, 34u);
19224   CHECK_PARSE("---\n"
19225               "IndentWidth: 78\n"
19226               "---\n"
19227               "Language: JavaScript\n"
19228               "IndentWidth: 56\n"
19229               "...\n",
19230               IndentWidth, 78u);
19231 
19232   Style.ColumnLimit = 123;
19233   Style.IndentWidth = 234;
19234   Style.BreakBeforeBraces = FormatStyle::BS_Linux;
19235   Style.TabWidth = 345;
19236   EXPECT_FALSE(parseConfiguration("---\n"
19237                                   "IndentWidth: 456\n"
19238                                   "BreakBeforeBraces: Allman\n"
19239                                   "---\n"
19240                                   "Language: JavaScript\n"
19241                                   "IndentWidth: 111\n"
19242                                   "TabWidth: 111\n"
19243                                   "---\n"
19244                                   "Language: Cpp\n"
19245                                   "BreakBeforeBraces: Stroustrup\n"
19246                                   "TabWidth: 789\n"
19247                                   "...\n",
19248                                   &Style));
19249   EXPECT_EQ(123u, Style.ColumnLimit);
19250   EXPECT_EQ(456u, Style.IndentWidth);
19251   EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces);
19252   EXPECT_EQ(789u, Style.TabWidth);
19253 
19254   EXPECT_EQ(parseConfiguration("---\n"
19255                                "Language: JavaScript\n"
19256                                "IndentWidth: 56\n"
19257                                "---\n"
19258                                "IndentWidth: 78\n"
19259                                "...\n",
19260                                &Style),
19261             ParseError::Error);
19262   EXPECT_EQ(parseConfiguration("---\n"
19263                                "Language: JavaScript\n"
19264                                "IndentWidth: 56\n"
19265                                "---\n"
19266                                "Language: JavaScript\n"
19267                                "IndentWidth: 78\n"
19268                                "...\n",
19269                                &Style),
19270             ParseError::Error);
19271 
19272   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
19273 }
19274 
19275 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) {
19276   FormatStyle Style = {};
19277   Style.Language = FormatStyle::LK_JavaScript;
19278   Style.BreakBeforeTernaryOperators = true;
19279   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value());
19280   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
19281 
19282   Style.BreakBeforeTernaryOperators = true;
19283   EXPECT_EQ(0, parseConfiguration("---\n"
19284                                   "BasedOnStyle: Google\n"
19285                                   "---\n"
19286                                   "Language: JavaScript\n"
19287                                   "IndentWidth: 76\n"
19288                                   "...\n",
19289                                   &Style)
19290                    .value());
19291   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
19292   EXPECT_EQ(76u, Style.IndentWidth);
19293   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
19294 }
19295 
19296 TEST_F(FormatTest, ConfigurationRoundTripTest) {
19297   FormatStyle Style = getLLVMStyle();
19298   std::string YAML = configurationAsText(Style);
19299   FormatStyle ParsedStyle = {};
19300   ParsedStyle.Language = FormatStyle::LK_Cpp;
19301   EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
19302   EXPECT_EQ(Style, ParsedStyle);
19303 }
19304 
19305 TEST_F(FormatTest, WorksFor8bitEncodings) {
19306   EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
19307             "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
19308             "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
19309             "\"\xef\xee\xf0\xf3...\"",
19310             format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
19311                    "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
19312                    "\xef\xee\xf0\xf3...\"",
19313                    getLLVMStyleWithColumns(12)));
19314 }
19315 
19316 TEST_F(FormatTest, HandlesUTF8BOM) {
19317   EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf"));
19318   EXPECT_EQ("\xef\xbb\xbf#include <iostream>",
19319             format("\xef\xbb\xbf#include <iostream>"));
19320   EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>",
19321             format("\xef\xbb\xbf\n#include <iostream>"));
19322 }
19323 
19324 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
19325 #if !defined(_MSC_VER)
19326 
19327 TEST_F(FormatTest, CountsUTF8CharactersProperly) {
19328   verifyFormat("\"Однажды в студёную зимнюю пору...\"",
19329                getLLVMStyleWithColumns(35));
19330   verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
19331                getLLVMStyleWithColumns(31));
19332   verifyFormat("// Однажды в студёную зимнюю пору...",
19333                getLLVMStyleWithColumns(36));
19334   verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32));
19335   verifyFormat("/* Однажды в студёную зимнюю пору... */",
19336                getLLVMStyleWithColumns(39));
19337   verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
19338                getLLVMStyleWithColumns(35));
19339 }
19340 
19341 TEST_F(FormatTest, SplitsUTF8Strings) {
19342   // Non-printable characters' width is currently considered to be the length in
19343   // bytes in UTF8. The characters can be displayed in very different manner
19344   // (zero-width, single width with a substitution glyph, expanded to their code
19345   // (e.g. "<8d>"), so there's no single correct way to handle them.
19346   EXPECT_EQ("\"aaaaÄ\"\n"
19347             "\"\xc2\x8d\";",
19348             format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
19349   EXPECT_EQ("\"aaaaaaaÄ\"\n"
19350             "\"\xc2\x8d\";",
19351             format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
19352   EXPECT_EQ("\"Однажды, в \"\n"
19353             "\"студёную \"\n"
19354             "\"зимнюю \"\n"
19355             "\"пору,\"",
19356             format("\"Однажды, в студёную зимнюю пору,\"",
19357                    getLLVMStyleWithColumns(13)));
19358   EXPECT_EQ(
19359       "\"一 二 三 \"\n"
19360       "\"四 五六 \"\n"
19361       "\"七 八 九 \"\n"
19362       "\"十\"",
19363       format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11)));
19364   EXPECT_EQ("\"一\t\"\n"
19365             "\"二 \t\"\n"
19366             "\"三 四 \"\n"
19367             "\"五\t\"\n"
19368             "\"六 \t\"\n"
19369             "\"七 \"\n"
19370             "\"八九十\tqq\"",
19371             format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
19372                    getLLVMStyleWithColumns(11)));
19373 
19374   // UTF8 character in an escape sequence.
19375   EXPECT_EQ("\"aaaaaa\"\n"
19376             "\"\\\xC2\x8D\"",
19377             format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10)));
19378 }
19379 
19380 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
19381   EXPECT_EQ("const char *sssss =\n"
19382             "    \"一二三四五六七八\\\n"
19383             " 九 十\";",
19384             format("const char *sssss = \"一二三四五六七八\\\n"
19385                    " 九 十\";",
19386                    getLLVMStyleWithColumns(30)));
19387 }
19388 
19389 TEST_F(FormatTest, SplitsUTF8LineComments) {
19390   EXPECT_EQ("// aaaaÄ\xc2\x8d",
19391             format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10)));
19392   EXPECT_EQ("// Я из лесу\n"
19393             "// вышел; был\n"
19394             "// сильный\n"
19395             "// мороз.",
19396             format("// Я из лесу вышел; был сильный мороз.",
19397                    getLLVMStyleWithColumns(13)));
19398   EXPECT_EQ("// 一二三\n"
19399             "// 四五六七\n"
19400             "// 八  九\n"
19401             "// 十",
19402             format("// 一二三 四五六七 八  九 十", getLLVMStyleWithColumns(9)));
19403 }
19404 
19405 TEST_F(FormatTest, SplitsUTF8BlockComments) {
19406   EXPECT_EQ("/* Гляжу,\n"
19407             " * поднимается\n"
19408             " * медленно в\n"
19409             " * гору\n"
19410             " * Лошадка,\n"
19411             " * везущая\n"
19412             " * хворосту\n"
19413             " * воз. */",
19414             format("/* Гляжу, поднимается медленно в гору\n"
19415                    " * Лошадка, везущая хворосту воз. */",
19416                    getLLVMStyleWithColumns(13)));
19417   EXPECT_EQ(
19418       "/* 一二三\n"
19419       " * 四五六七\n"
19420       " * 八  九\n"
19421       " * 十  */",
19422       format("/* 一二三 四五六七 八  九 十  */", getLLVMStyleWithColumns(9)));
19423   EXPECT_EQ("/* �������� ��������\n"
19424             " * ��������\n"
19425             " * ������-�� */",
19426             format("/* �������� �������� �������� ������-�� */", getLLVMStyleWithColumns(12)));
19427 }
19428 
19429 #endif // _MSC_VER
19430 
19431 TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
19432   FormatStyle Style = getLLVMStyle();
19433 
19434   Style.ConstructorInitializerIndentWidth = 4;
19435   verifyFormat(
19436       "SomeClass::Constructor()\n"
19437       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
19438       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
19439       Style);
19440 
19441   Style.ConstructorInitializerIndentWidth = 2;
19442   verifyFormat(
19443       "SomeClass::Constructor()\n"
19444       "  : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
19445       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
19446       Style);
19447 
19448   Style.ConstructorInitializerIndentWidth = 0;
19449   verifyFormat(
19450       "SomeClass::Constructor()\n"
19451       ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
19452       "  aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
19453       Style);
19454   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
19455   verifyFormat(
19456       "SomeLongTemplateVariableName<\n"
19457       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>",
19458       Style);
19459   verifyFormat("bool smaller = 1 < "
19460                "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
19461                "                       "
19462                "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
19463                Style);
19464 
19465   Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
19466   verifyFormat("SomeClass::Constructor() :\n"
19467                "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa),\n"
19468                "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa) {}",
19469                Style);
19470 }
19471 
19472 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {
19473   FormatStyle Style = getLLVMStyle();
19474   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
19475   Style.ConstructorInitializerIndentWidth = 4;
19476   verifyFormat("SomeClass::Constructor()\n"
19477                "    : a(a)\n"
19478                "    , b(b)\n"
19479                "    , c(c) {}",
19480                Style);
19481   verifyFormat("SomeClass::Constructor()\n"
19482                "    : a(a) {}",
19483                Style);
19484 
19485   Style.ColumnLimit = 0;
19486   verifyFormat("SomeClass::Constructor()\n"
19487                "    : a(a) {}",
19488                Style);
19489   verifyFormat("SomeClass::Constructor() noexcept\n"
19490                "    : a(a) {}",
19491                Style);
19492   verifyFormat("SomeClass::Constructor()\n"
19493                "    : a(a)\n"
19494                "    , b(b)\n"
19495                "    , c(c) {}",
19496                Style);
19497   verifyFormat("SomeClass::Constructor()\n"
19498                "    : a(a) {\n"
19499                "  foo();\n"
19500                "  bar();\n"
19501                "}",
19502                Style);
19503 
19504   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
19505   verifyFormat("SomeClass::Constructor()\n"
19506                "    : a(a)\n"
19507                "    , b(b)\n"
19508                "    , c(c) {\n}",
19509                Style);
19510   verifyFormat("SomeClass::Constructor()\n"
19511                "    : a(a) {\n}",
19512                Style);
19513 
19514   Style.ColumnLimit = 80;
19515   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
19516   Style.ConstructorInitializerIndentWidth = 2;
19517   verifyFormat("SomeClass::Constructor()\n"
19518                "  : a(a)\n"
19519                "  , b(b)\n"
19520                "  , c(c) {}",
19521                Style);
19522 
19523   Style.ConstructorInitializerIndentWidth = 0;
19524   verifyFormat("SomeClass::Constructor()\n"
19525                ": a(a)\n"
19526                ", b(b)\n"
19527                ", c(c) {}",
19528                Style);
19529 
19530   Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
19531   Style.ConstructorInitializerIndentWidth = 4;
19532   verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
19533   verifyFormat(
19534       "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n",
19535       Style);
19536   verifyFormat(
19537       "SomeClass::Constructor()\n"
19538       "    : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
19539       Style);
19540   Style.ConstructorInitializerIndentWidth = 4;
19541   Style.ColumnLimit = 60;
19542   verifyFormat("SomeClass::Constructor()\n"
19543                "    : aaaaaaaa(aaaaaaaa)\n"
19544                "    , aaaaaaaa(aaaaaaaa)\n"
19545                "    , aaaaaaaa(aaaaaaaa) {}",
19546                Style);
19547 }
19548 
19549 TEST_F(FormatTest, ConstructorInitializersWithPreprocessorDirective) {
19550   FormatStyle Style = getLLVMStyle();
19551   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
19552   Style.ConstructorInitializerIndentWidth = 4;
19553   verifyFormat("SomeClass::Constructor()\n"
19554                "    : a{a}\n"
19555                "    , b{b} {}",
19556                Style);
19557   verifyFormat("SomeClass::Constructor()\n"
19558                "    : a{a}\n"
19559                "#if CONDITION\n"
19560                "    , b{b}\n"
19561                "#endif\n"
19562                "{\n}",
19563                Style);
19564   Style.ConstructorInitializerIndentWidth = 2;
19565   verifyFormat("SomeClass::Constructor()\n"
19566                "#if CONDITION\n"
19567                "  : a{a}\n"
19568                "#endif\n"
19569                "  , b{b}\n"
19570                "  , c{c} {\n}",
19571                Style);
19572   Style.ConstructorInitializerIndentWidth = 0;
19573   verifyFormat("SomeClass::Constructor()\n"
19574                ": a{a}\n"
19575                "#ifdef CONDITION\n"
19576                ", b{b}\n"
19577                "#else\n"
19578                ", c{c}\n"
19579                "#endif\n"
19580                ", d{d} {\n}",
19581                Style);
19582   Style.ConstructorInitializerIndentWidth = 4;
19583   verifyFormat("SomeClass::Constructor()\n"
19584                "    : a{a}\n"
19585                "#if WINDOWS\n"
19586                "#if DEBUG\n"
19587                "    , b{0}\n"
19588                "#else\n"
19589                "    , b{1}\n"
19590                "#endif\n"
19591                "#else\n"
19592                "#if DEBUG\n"
19593                "    , b{2}\n"
19594                "#else\n"
19595                "    , b{3}\n"
19596                "#endif\n"
19597                "#endif\n"
19598                "{\n}",
19599                Style);
19600   verifyFormat("SomeClass::Constructor()\n"
19601                "    : a{a}\n"
19602                "#if WINDOWS\n"
19603                "    , b{0}\n"
19604                "#if DEBUG\n"
19605                "    , c{0}\n"
19606                "#else\n"
19607                "    , c{1}\n"
19608                "#endif\n"
19609                "#else\n"
19610                "#if DEBUG\n"
19611                "    , c{2}\n"
19612                "#else\n"
19613                "    , c{3}\n"
19614                "#endif\n"
19615                "    , b{1}\n"
19616                "#endif\n"
19617                "{\n}",
19618                Style);
19619 }
19620 
19621 TEST_F(FormatTest, Destructors) {
19622   verifyFormat("void F(int &i) { i.~int(); }");
19623   verifyFormat("void F(int &i) { i->~int(); }");
19624 }
19625 
19626 TEST_F(FormatTest, FormatsWithWebKitStyle) {
19627   FormatStyle Style = getWebKitStyle();
19628 
19629   // Don't indent in outer namespaces.
19630   verifyFormat("namespace outer {\n"
19631                "int i;\n"
19632                "namespace inner {\n"
19633                "    int i;\n"
19634                "} // namespace inner\n"
19635                "} // namespace outer\n"
19636                "namespace other_outer {\n"
19637                "int i;\n"
19638                "}",
19639                Style);
19640 
19641   // Don't indent case labels.
19642   verifyFormat("switch (variable) {\n"
19643                "case 1:\n"
19644                "case 2:\n"
19645                "    doSomething();\n"
19646                "    break;\n"
19647                "default:\n"
19648                "    ++variable;\n"
19649                "}",
19650                Style);
19651 
19652   // Wrap before binary operators.
19653   EXPECT_EQ("void f()\n"
19654             "{\n"
19655             "    if (aaaaaaaaaaaaaaaa\n"
19656             "        && bbbbbbbbbbbbbbbbbbbbbbbb\n"
19657             "        && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
19658             "        return;\n"
19659             "}",
19660             format("void f() {\n"
19661                    "if (aaaaaaaaaaaaaaaa\n"
19662                    "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
19663                    "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
19664                    "return;\n"
19665                    "}",
19666                    Style));
19667 
19668   // Allow functions on a single line.
19669   verifyFormat("void f() { return; }", Style);
19670 
19671   // Allow empty blocks on a single line and insert a space in empty blocks.
19672   EXPECT_EQ("void f() { }", format("void f() {}", Style));
19673   EXPECT_EQ("while (true) { }", format("while (true) {}", Style));
19674   // However, don't merge non-empty short loops.
19675   EXPECT_EQ("while (true) {\n"
19676             "    continue;\n"
19677             "}",
19678             format("while (true) { continue; }", Style));
19679 
19680   // Constructor initializers are formatted one per line with the "," on the
19681   // new line.
19682   verifyFormat("Constructor()\n"
19683                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
19684                "    , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
19685                "          aaaaaaaaaaaaaa)\n"
19686                "    , aaaaaaaaaaaaaaaaaaaaaaa()\n"
19687                "{\n"
19688                "}",
19689                Style);
19690   verifyFormat("SomeClass::Constructor()\n"
19691                "    : a(a)\n"
19692                "{\n"
19693                "}",
19694                Style);
19695   EXPECT_EQ("SomeClass::Constructor()\n"
19696             "    : a(a)\n"
19697             "{\n"
19698             "}",
19699             format("SomeClass::Constructor():a(a){}", Style));
19700   verifyFormat("SomeClass::Constructor()\n"
19701                "    : a(a)\n"
19702                "    , b(b)\n"
19703                "    , c(c)\n"
19704                "{\n"
19705                "}",
19706                Style);
19707   verifyFormat("SomeClass::Constructor()\n"
19708                "    : a(a)\n"
19709                "{\n"
19710                "    foo();\n"
19711                "    bar();\n"
19712                "}",
19713                Style);
19714 
19715   // Access specifiers should be aligned left.
19716   verifyFormat("class C {\n"
19717                "public:\n"
19718                "    int i;\n"
19719                "};",
19720                Style);
19721 
19722   // Do not align comments.
19723   verifyFormat("int a; // Do not\n"
19724                "double b; // align comments.",
19725                Style);
19726 
19727   // Do not align operands.
19728   EXPECT_EQ("ASSERT(aaaa\n"
19729             "    || bbbb);",
19730             format("ASSERT ( aaaa\n||bbbb);", Style));
19731 
19732   // Accept input's line breaks.
19733   EXPECT_EQ("if (aaaaaaaaaaaaaaa\n"
19734             "    || bbbbbbbbbbbbbbb) {\n"
19735             "    i++;\n"
19736             "}",
19737             format("if (aaaaaaaaaaaaaaa\n"
19738                    "|| bbbbbbbbbbbbbbb) { i++; }",
19739                    Style));
19740   EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
19741             "    i++;\n"
19742             "}",
19743             format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
19744 
19745   // Don't automatically break all macro definitions (llvm.org/PR17842).
19746   verifyFormat("#define aNumber 10", Style);
19747   // However, generally keep the line breaks that the user authored.
19748   EXPECT_EQ("#define aNumber \\\n"
19749             "    10",
19750             format("#define aNumber \\\n"
19751                    " 10",
19752                    Style));
19753 
19754   // Keep empty and one-element array literals on a single line.
19755   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
19756             "                                  copyItems:YES];",
19757             format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
19758                    "copyItems:YES];",
19759                    Style));
19760   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
19761             "                                  copyItems:YES];",
19762             format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
19763                    "             copyItems:YES];",
19764                    Style));
19765   // FIXME: This does not seem right, there should be more indentation before
19766   // the array literal's entries. Nested blocks have the same problem.
19767   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
19768             "    @\"a\",\n"
19769             "    @\"a\"\n"
19770             "]\n"
19771             "                                  copyItems:YES];",
19772             format("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
19773                    "     @\"a\",\n"
19774                    "     @\"a\"\n"
19775                    "     ]\n"
19776                    "       copyItems:YES];",
19777                    Style));
19778   EXPECT_EQ(
19779       "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
19780       "                                  copyItems:YES];",
19781       format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
19782              "   copyItems:YES];",
19783              Style));
19784 
19785   verifyFormat("[self.a b:c c:d];", Style);
19786   EXPECT_EQ("[self.a b:c\n"
19787             "        c:d];",
19788             format("[self.a b:c\n"
19789                    "c:d];",
19790                    Style));
19791 }
19792 
19793 TEST_F(FormatTest, FormatsLambdas) {
19794   verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n");
19795   verifyFormat(
19796       "int c = [b]() mutable noexcept { return [&b] { return b++; }(); }();\n");
19797   verifyFormat("int c = [&] { [=] { return b++; }(); }();\n");
19798   verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n");
19799   verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n");
19800   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n");
19801   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n");
19802   verifyFormat("auto c = [a = [b = 42] {}] {};\n");
19803   verifyFormat("auto c = [a = &i + 10, b = [] {}] {};\n");
19804   verifyFormat("int x = f(*+[] {});");
19805   verifyFormat("void f() {\n"
19806                "  other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
19807                "}\n");
19808   verifyFormat("void f() {\n"
19809                "  other(x.begin(), //\n"
19810                "        x.end(),   //\n"
19811                "        [&](int, int) { return 1; });\n"
19812                "}\n");
19813   verifyFormat("void f() {\n"
19814                "  other.other.other.other.other(\n"
19815                "      x.begin(), x.end(),\n"
19816                "      [something, rather](int, int, int, int, int, int, int) { "
19817                "return 1; });\n"
19818                "}\n");
19819   verifyFormat(
19820       "void f() {\n"
19821       "  other.other.other.other.other(\n"
19822       "      x.begin(), x.end(),\n"
19823       "      [something, rather](int, int, int, int, int, int, int) {\n"
19824       "        //\n"
19825       "      });\n"
19826       "}\n");
19827   verifyFormat("SomeFunction([]() { // A cool function...\n"
19828                "  return 43;\n"
19829                "});");
19830   EXPECT_EQ("SomeFunction([]() {\n"
19831             "#define A a\n"
19832             "  return 43;\n"
19833             "});",
19834             format("SomeFunction([](){\n"
19835                    "#define A a\n"
19836                    "return 43;\n"
19837                    "});"));
19838   verifyFormat("void f() {\n"
19839                "  SomeFunction([](decltype(x), A *a) {});\n"
19840                "  SomeFunction([](typeof(x), A *a) {});\n"
19841                "  SomeFunction([](_Atomic(x), A *a) {});\n"
19842                "  SomeFunction([](__underlying_type(x), A *a) {});\n"
19843                "}");
19844   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
19845                "    [](const aaaaaaaaaa &a) { return a; });");
19846   verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
19847                "  SomeOtherFunctioooooooooooooooooooooooooon();\n"
19848                "});");
19849   verifyFormat("Constructor()\n"
19850                "    : Field([] { // comment\n"
19851                "        int i;\n"
19852                "      }) {}");
19853   verifyFormat("auto my_lambda = [](const string &some_parameter) {\n"
19854                "  return some_parameter.size();\n"
19855                "};");
19856   verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n"
19857                "    [](const string &s) { return s; };");
19858   verifyFormat("int i = aaaaaa ? 1 //\n"
19859                "               : [] {\n"
19860                "                   return 2; //\n"
19861                "                 }();");
19862   verifyFormat("llvm::errs() << \"number of twos is \"\n"
19863                "             << std::count_if(v.begin(), v.end(), [](int x) {\n"
19864                "                  return x == 2; // force break\n"
19865                "                });");
19866   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
19867                "    [=](int iiiiiiiiiiii) {\n"
19868                "      return aaaaaaaaaaaaaaaaaaaaaaa !=\n"
19869                "             aaaaaaaaaaaaaaaaaaaaaaa;\n"
19870                "    });",
19871                getLLVMStyleWithColumns(60));
19872 
19873   verifyFormat("SomeFunction({[&] {\n"
19874                "                // comment\n"
19875                "              },\n"
19876                "              [&] {\n"
19877                "                // comment\n"
19878                "              }});");
19879   verifyFormat("SomeFunction({[&] {\n"
19880                "  // comment\n"
19881                "}});");
19882   verifyFormat(
19883       "virtual aaaaaaaaaaaaaaaa(\n"
19884       "    std::function<bool()> bbbbbbbbbbbb = [&]() { return true; },\n"
19885       "    aaaaa aaaaaaaaa);");
19886 
19887   // Lambdas with return types.
19888   verifyFormat("int c = []() -> int { return 2; }();\n");
19889   verifyFormat("int c = []() -> int * { return 2; }();\n");
19890   verifyFormat("int c = []() -> vector<int> { return {2}; }();\n");
19891   verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
19892   verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
19893   verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
19894   verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
19895   verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
19896   verifyFormat("[a, a]() -> a<1> {};");
19897   verifyFormat("[]() -> foo<5 + 2> { return {}; };");
19898   verifyFormat("[]() -> foo<5 - 2> { return {}; };");
19899   verifyFormat("[]() -> foo<5 / 2> { return {}; };");
19900   verifyFormat("[]() -> foo<5 * 2> { return {}; };");
19901   verifyFormat("[]() -> foo<5 % 2> { return {}; };");
19902   verifyFormat("[]() -> foo<5 << 2> { return {}; };");
19903   verifyFormat("[]() -> foo<!5> { return {}; };");
19904   verifyFormat("[]() -> foo<~5> { return {}; };");
19905   verifyFormat("[]() -> foo<5 | 2> { return {}; };");
19906   verifyFormat("[]() -> foo<5 || 2> { return {}; };");
19907   verifyFormat("[]() -> foo<5 & 2> { return {}; };");
19908   verifyFormat("[]() -> foo<5 && 2> { return {}; };");
19909   verifyFormat("[]() -> foo<5 == 2> { return {}; };");
19910   verifyFormat("[]() -> foo<5 != 2> { return {}; };");
19911   verifyFormat("[]() -> foo<5 >= 2> { return {}; };");
19912   verifyFormat("[]() -> foo<5 <= 2> { return {}; };");
19913   verifyFormat("[]() -> foo<5 < 2> { return {}; };");
19914   verifyFormat("[]() -> foo<2 ? 1 : 0> { return {}; };");
19915   verifyFormat("namespace bar {\n"
19916                "// broken:\n"
19917                "auto foo{[]() -> foo<5 + 2> { return {}; }};\n"
19918                "} // namespace bar");
19919   verifyFormat("namespace bar {\n"
19920                "// broken:\n"
19921                "auto foo{[]() -> foo<5 - 2> { return {}; }};\n"
19922                "} // namespace bar");
19923   verifyFormat("namespace bar {\n"
19924                "// broken:\n"
19925                "auto foo{[]() -> foo<5 / 2> { return {}; }};\n"
19926                "} // namespace bar");
19927   verifyFormat("namespace bar {\n"
19928                "// broken:\n"
19929                "auto foo{[]() -> foo<5 * 2> { return {}; }};\n"
19930                "} // namespace bar");
19931   verifyFormat("namespace bar {\n"
19932                "// broken:\n"
19933                "auto foo{[]() -> foo<5 % 2> { return {}; }};\n"
19934                "} // namespace bar");
19935   verifyFormat("namespace bar {\n"
19936                "// broken:\n"
19937                "auto foo{[]() -> foo<5 << 2> { return {}; }};\n"
19938                "} // namespace bar");
19939   verifyFormat("namespace bar {\n"
19940                "// broken:\n"
19941                "auto foo{[]() -> foo<!5> { return {}; }};\n"
19942                "} // namespace bar");
19943   verifyFormat("namespace bar {\n"
19944                "// broken:\n"
19945                "auto foo{[]() -> foo<~5> { return {}; }};\n"
19946                "} // namespace bar");
19947   verifyFormat("namespace bar {\n"
19948                "// broken:\n"
19949                "auto foo{[]() -> foo<5 | 2> { return {}; }};\n"
19950                "} // namespace bar");
19951   verifyFormat("namespace bar {\n"
19952                "// broken:\n"
19953                "auto foo{[]() -> foo<5 || 2> { return {}; }};\n"
19954                "} // namespace bar");
19955   verifyFormat("namespace bar {\n"
19956                "// broken:\n"
19957                "auto foo{[]() -> foo<5 & 2> { return {}; }};\n"
19958                "} // namespace bar");
19959   verifyFormat("namespace bar {\n"
19960                "// broken:\n"
19961                "auto foo{[]() -> foo<5 && 2> { return {}; }};\n"
19962                "} // namespace bar");
19963   verifyFormat("namespace bar {\n"
19964                "// broken:\n"
19965                "auto foo{[]() -> foo<5 == 2> { return {}; }};\n"
19966                "} // namespace bar");
19967   verifyFormat("namespace bar {\n"
19968                "// broken:\n"
19969                "auto foo{[]() -> foo<5 != 2> { return {}; }};\n"
19970                "} // namespace bar");
19971   verifyFormat("namespace bar {\n"
19972                "// broken:\n"
19973                "auto foo{[]() -> foo<5 >= 2> { return {}; }};\n"
19974                "} // namespace bar");
19975   verifyFormat("namespace bar {\n"
19976                "// broken:\n"
19977                "auto foo{[]() -> foo<5 <= 2> { return {}; }};\n"
19978                "} // namespace bar");
19979   verifyFormat("namespace bar {\n"
19980                "// broken:\n"
19981                "auto foo{[]() -> foo<5 < 2> { return {}; }};\n"
19982                "} // namespace bar");
19983   verifyFormat("namespace bar {\n"
19984                "// broken:\n"
19985                "auto foo{[]() -> foo<2 ? 1 : 0> { return {}; }};\n"
19986                "} // namespace bar");
19987   verifyFormat("[]() -> a<1> {};");
19988   verifyFormat("[]() -> a<1> { ; };");
19989   verifyFormat("[]() -> a<1> { ; }();");
19990   verifyFormat("[a, a]() -> a<true> {};");
19991   verifyFormat("[]() -> a<true> {};");
19992   verifyFormat("[]() -> a<true> { ; };");
19993   verifyFormat("[]() -> a<true> { ; }();");
19994   verifyFormat("[a, a]() -> a<false> {};");
19995   verifyFormat("[]() -> a<false> {};");
19996   verifyFormat("[]() -> a<false> { ; };");
19997   verifyFormat("[]() -> a<false> { ; }();");
19998   verifyFormat("auto foo{[]() -> foo<false> { ; }};");
19999   verifyFormat("namespace bar {\n"
20000                "auto foo{[]() -> foo<false> { ; }};\n"
20001                "} // namespace bar");
20002   verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
20003                "                   int j) -> int {\n"
20004                "  return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
20005                "};");
20006   verifyFormat(
20007       "aaaaaaaaaaaaaaaaaaaaaa(\n"
20008       "    [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n"
20009       "      return aaaaaaaaaaaaaaaaa;\n"
20010       "    });",
20011       getLLVMStyleWithColumns(70));
20012   verifyFormat("[]() //\n"
20013                "    -> int {\n"
20014                "  return 1; //\n"
20015                "};");
20016   verifyFormat("[]() -> Void<T...> {};");
20017   verifyFormat("[a, b]() -> Tuple<T...> { return {}; };");
20018 
20019   // Lambdas with explicit template argument lists.
20020   verifyFormat(
20021       "auto L = []<template <typename> class T, class U>(T<U> &&a) {};\n");
20022 
20023   // Multiple lambdas in the same parentheses change indentation rules. These
20024   // lambdas are forced to start on new lines.
20025   verifyFormat("SomeFunction(\n"
20026                "    []() {\n"
20027                "      //\n"
20028                "    },\n"
20029                "    []() {\n"
20030                "      //\n"
20031                "    });");
20032 
20033   // A lambda passed as arg0 is always pushed to the next line.
20034   verifyFormat("SomeFunction(\n"
20035                "    [this] {\n"
20036                "      //\n"
20037                "    },\n"
20038                "    1);\n");
20039 
20040   // A multi-line lambda passed as arg1 forces arg0 to be pushed out, just like
20041   // the arg0 case above.
20042   auto Style = getGoogleStyle();
20043   Style.BinPackArguments = false;
20044   verifyFormat("SomeFunction(\n"
20045                "    a,\n"
20046                "    [this] {\n"
20047                "      //\n"
20048                "    },\n"
20049                "    b);\n",
20050                Style);
20051   verifyFormat("SomeFunction(\n"
20052                "    a,\n"
20053                "    [this] {\n"
20054                "      //\n"
20055                "    },\n"
20056                "    b);\n");
20057 
20058   // A lambda with a very long line forces arg0 to be pushed out irrespective of
20059   // the BinPackArguments value (as long as the code is wide enough).
20060   verifyFormat(
20061       "something->SomeFunction(\n"
20062       "    a,\n"
20063       "    [this] {\n"
20064       "      "
20065       "D0000000000000000000000000000000000000000000000000000000000001();\n"
20066       "    },\n"
20067       "    b);\n");
20068 
20069   // A multi-line lambda is pulled up as long as the introducer fits on the
20070   // previous line and there are no further args.
20071   verifyFormat("function(1, [this, that] {\n"
20072                "  //\n"
20073                "});\n");
20074   verifyFormat("function([this, that] {\n"
20075                "  //\n"
20076                "});\n");
20077   // FIXME: this format is not ideal and we should consider forcing the first
20078   // arg onto its own line.
20079   verifyFormat("function(a, b, c, //\n"
20080                "         d, [this, that] {\n"
20081                "           //\n"
20082                "         });\n");
20083 
20084   // Multiple lambdas are treated correctly even when there is a short arg0.
20085   verifyFormat("SomeFunction(\n"
20086                "    1,\n"
20087                "    [this] {\n"
20088                "      //\n"
20089                "    },\n"
20090                "    [this] {\n"
20091                "      //\n"
20092                "    },\n"
20093                "    1);\n");
20094 
20095   // More complex introducers.
20096   verifyFormat("return [i, args...] {};");
20097 
20098   // Not lambdas.
20099   verifyFormat("constexpr char hello[]{\"hello\"};");
20100   verifyFormat("double &operator[](int i) { return 0; }\n"
20101                "int i;");
20102   verifyFormat("std::unique_ptr<int[]> foo() {}");
20103   verifyFormat("int i = a[a][a]->f();");
20104   verifyFormat("int i = (*b)[a]->f();");
20105 
20106   // Other corner cases.
20107   verifyFormat("void f() {\n"
20108                "  bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
20109                "  );\n"
20110                "}");
20111 
20112   // Lambdas created through weird macros.
20113   verifyFormat("void f() {\n"
20114                "  MACRO((const AA &a) { return 1; });\n"
20115                "  MACRO((AA &a) { return 1; });\n"
20116                "}");
20117 
20118   verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
20119                "      doo_dah();\n"
20120                "      doo_dah();\n"
20121                "    })) {\n"
20122                "}");
20123   verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n"
20124                "                doo_dah();\n"
20125                "                doo_dah();\n"
20126                "              })) {\n"
20127                "}");
20128   verifyFormat("if CONSTEXPR (blah_blah(whatever, whatever, [] {\n"
20129                "                doo_dah();\n"
20130                "                doo_dah();\n"
20131                "              })) {\n"
20132                "}");
20133   verifyFormat("auto lambda = []() {\n"
20134                "  int a = 2\n"
20135                "#if A\n"
20136                "          + 2\n"
20137                "#endif\n"
20138                "      ;\n"
20139                "};");
20140 
20141   // Lambdas with complex multiline introducers.
20142   verifyFormat(
20143       "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
20144       "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n"
20145       "        -> ::std::unordered_set<\n"
20146       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
20147       "      //\n"
20148       "    });");
20149 
20150   FormatStyle DoNotMerge = getLLVMStyle();
20151   DoNotMerge.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
20152   verifyFormat("auto c = []() {\n"
20153                "  return b;\n"
20154                "};",
20155                "auto c = []() { return b; };", DoNotMerge);
20156   verifyFormat("auto c = []() {\n"
20157                "};",
20158                " auto c = []() {};", DoNotMerge);
20159 
20160   FormatStyle MergeEmptyOnly = getLLVMStyle();
20161   MergeEmptyOnly.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Empty;
20162   verifyFormat("auto c = []() {\n"
20163                "  return b;\n"
20164                "};",
20165                "auto c = []() {\n"
20166                "  return b;\n"
20167                " };",
20168                MergeEmptyOnly);
20169   verifyFormat("auto c = []() {};",
20170                "auto c = []() {\n"
20171                "};",
20172                MergeEmptyOnly);
20173 
20174   FormatStyle MergeInline = getLLVMStyle();
20175   MergeInline.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Inline;
20176   verifyFormat("auto c = []() {\n"
20177                "  return b;\n"
20178                "};",
20179                "auto c = []() { return b; };", MergeInline);
20180   verifyFormat("function([]() { return b; })", "function([]() { return b; })",
20181                MergeInline);
20182   verifyFormat("function([]() { return b; }, a)",
20183                "function([]() { return b; }, a)", MergeInline);
20184   verifyFormat("function(a, []() { return b; })",
20185                "function(a, []() { return b; })", MergeInline);
20186 
20187   // Check option "BraceWrapping.BeforeLambdaBody" and different state of
20188   // AllowShortLambdasOnASingleLine
20189   FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle();
20190   LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
20191   LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
20192   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
20193       FormatStyle::ShortLambdaStyle::SLS_None;
20194   verifyFormat("FctWithOneNestedLambdaInline_SLS_None(\n"
20195                "    []()\n"
20196                "    {\n"
20197                "      return 17;\n"
20198                "    });",
20199                LLVMWithBeforeLambdaBody);
20200   verifyFormat("FctWithOneNestedLambdaEmpty_SLS_None(\n"
20201                "    []()\n"
20202                "    {\n"
20203                "    });",
20204                LLVMWithBeforeLambdaBody);
20205   verifyFormat("auto fct_SLS_None = []()\n"
20206                "{\n"
20207                "  return 17;\n"
20208                "};",
20209                LLVMWithBeforeLambdaBody);
20210   verifyFormat("TwoNestedLambdas_SLS_None(\n"
20211                "    []()\n"
20212                "    {\n"
20213                "      return Call(\n"
20214                "          []()\n"
20215                "          {\n"
20216                "            return 17;\n"
20217                "          });\n"
20218                "    });",
20219                LLVMWithBeforeLambdaBody);
20220   verifyFormat("void Fct() {\n"
20221                "  return {[]()\n"
20222                "          {\n"
20223                "            return 17;\n"
20224                "          }};\n"
20225                "}",
20226                LLVMWithBeforeLambdaBody);
20227 
20228   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
20229       FormatStyle::ShortLambdaStyle::SLS_Empty;
20230   verifyFormat("FctWithOneNestedLambdaInline_SLS_Empty(\n"
20231                "    []()\n"
20232                "    {\n"
20233                "      return 17;\n"
20234                "    });",
20235                LLVMWithBeforeLambdaBody);
20236   verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Empty([]() {});",
20237                LLVMWithBeforeLambdaBody);
20238   verifyFormat("FctWithOneNestedLambdaEmptyInsideAVeryVeryVeryVeryVeryVeryVeryL"
20239                "ongFunctionName_SLS_Empty(\n"
20240                "    []() {});",
20241                LLVMWithBeforeLambdaBody);
20242   verifyFormat("FctWithMultipleParams_SLS_Empty(A, B,\n"
20243                "                                []()\n"
20244                "                                {\n"
20245                "                                  return 17;\n"
20246                "                                });",
20247                LLVMWithBeforeLambdaBody);
20248   verifyFormat("auto fct_SLS_Empty = []()\n"
20249                "{\n"
20250                "  return 17;\n"
20251                "};",
20252                LLVMWithBeforeLambdaBody);
20253   verifyFormat("TwoNestedLambdas_SLS_Empty(\n"
20254                "    []()\n"
20255                "    {\n"
20256                "      return Call([]() {});\n"
20257                "    });",
20258                LLVMWithBeforeLambdaBody);
20259   verifyFormat("TwoNestedLambdas_SLS_Empty(A,\n"
20260                "                           []()\n"
20261                "                           {\n"
20262                "                             return Call([]() {});\n"
20263                "                           });",
20264                LLVMWithBeforeLambdaBody);
20265   verifyFormat(
20266       "FctWithLongLineInLambda_SLS_Empty(\n"
20267       "    []()\n"
20268       "    {\n"
20269       "      return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
20270       "                               AndShouldNotBeConsiderAsInline,\n"
20271       "                               LambdaBodyMustBeBreak);\n"
20272       "    });",
20273       LLVMWithBeforeLambdaBody);
20274 
20275   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
20276       FormatStyle::ShortLambdaStyle::SLS_Inline;
20277   verifyFormat("FctWithOneNestedLambdaInline_SLS_Inline([]() { return 17; });",
20278                LLVMWithBeforeLambdaBody);
20279   verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Inline([]() {});",
20280                LLVMWithBeforeLambdaBody);
20281   verifyFormat("auto fct_SLS_Inline = []()\n"
20282                "{\n"
20283                "  return 17;\n"
20284                "};",
20285                LLVMWithBeforeLambdaBody);
20286   verifyFormat("TwoNestedLambdas_SLS_Inline([]() { return Call([]() { return "
20287                "17; }); });",
20288                LLVMWithBeforeLambdaBody);
20289   verifyFormat(
20290       "FctWithLongLineInLambda_SLS_Inline(\n"
20291       "    []()\n"
20292       "    {\n"
20293       "      return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
20294       "                               AndShouldNotBeConsiderAsInline,\n"
20295       "                               LambdaBodyMustBeBreak);\n"
20296       "    });",
20297       LLVMWithBeforeLambdaBody);
20298   verifyFormat("FctWithMultipleParams_SLS_Inline("
20299                "VeryLongParameterThatShouldAskToBeOnMultiLine,\n"
20300                "                                 []() { return 17; });",
20301                LLVMWithBeforeLambdaBody);
20302   verifyFormat(
20303       "FctWithMultipleParams_SLS_Inline(FirstParam, []() { return 17; });",
20304       LLVMWithBeforeLambdaBody);
20305 
20306   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
20307       FormatStyle::ShortLambdaStyle::SLS_All;
20308   verifyFormat("FctWithOneNestedLambdaInline_SLS_All([]() { return 17; });",
20309                LLVMWithBeforeLambdaBody);
20310   verifyFormat("FctWithOneNestedLambdaEmpty_SLS_All([]() {});",
20311                LLVMWithBeforeLambdaBody);
20312   verifyFormat("auto fct_SLS_All = []() { return 17; };",
20313                LLVMWithBeforeLambdaBody);
20314   verifyFormat("FctWithOneParam_SLS_All(\n"
20315                "    []()\n"
20316                "    {\n"
20317                "      // A cool function...\n"
20318                "      return 43;\n"
20319                "    });",
20320                LLVMWithBeforeLambdaBody);
20321   verifyFormat("FctWithMultipleParams_SLS_All("
20322                "VeryLongParameterThatShouldAskToBeOnMultiLine,\n"
20323                "                              []() { return 17; });",
20324                LLVMWithBeforeLambdaBody);
20325   verifyFormat("FctWithMultipleParams_SLS_All(A, []() { return 17; });",
20326                LLVMWithBeforeLambdaBody);
20327   verifyFormat("FctWithMultipleParams_SLS_All(A, B, []() { return 17; });",
20328                LLVMWithBeforeLambdaBody);
20329   verifyFormat(
20330       "FctWithLongLineInLambda_SLS_All(\n"
20331       "    []()\n"
20332       "    {\n"
20333       "      return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
20334       "                               AndShouldNotBeConsiderAsInline,\n"
20335       "                               LambdaBodyMustBeBreak);\n"
20336       "    });",
20337       LLVMWithBeforeLambdaBody);
20338   verifyFormat(
20339       "auto fct_SLS_All = []()\n"
20340       "{\n"
20341       "  return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
20342       "                           AndShouldNotBeConsiderAsInline,\n"
20343       "                           LambdaBodyMustBeBreak);\n"
20344       "};",
20345       LLVMWithBeforeLambdaBody);
20346   LLVMWithBeforeLambdaBody.BinPackParameters = false;
20347   verifyFormat("FctAllOnSameLine_SLS_All([]() { return S; }, Fst, Second);",
20348                LLVMWithBeforeLambdaBody);
20349   verifyFormat(
20350       "FctWithLongLineInLambda_SLS_All([]() { return SomeValueNotSoLong; },\n"
20351       "                                FirstParam,\n"
20352       "                                SecondParam,\n"
20353       "                                ThirdParam,\n"
20354       "                                FourthParam);",
20355       LLVMWithBeforeLambdaBody);
20356   verifyFormat("FctWithLongLineInLambda_SLS_All(\n"
20357                "    []() { return "
20358                "SomeValueVeryVeryVeryVeryVeryVeryVeryVeryVeryLong; },\n"
20359                "    FirstParam,\n"
20360                "    SecondParam,\n"
20361                "    ThirdParam,\n"
20362                "    FourthParam);",
20363                LLVMWithBeforeLambdaBody);
20364   verifyFormat(
20365       "FctWithLongLineInLambda_SLS_All(FirstParam,\n"
20366       "                                SecondParam,\n"
20367       "                                ThirdParam,\n"
20368       "                                FourthParam,\n"
20369       "                                []() { return SomeValueNotSoLong; });",
20370       LLVMWithBeforeLambdaBody);
20371   verifyFormat("FctWithLongLineInLambda_SLS_All(\n"
20372                "    []()\n"
20373                "    {\n"
20374                "      return "
20375                "HereAVeryLongLineThatWillBeFormattedOnMultipleLineAndShouldNotB"
20376                "eConsiderAsInline;\n"
20377                "    });",
20378                LLVMWithBeforeLambdaBody);
20379   verifyFormat(
20380       "FctWithLongLineInLambda_SLS_All(\n"
20381       "    []()\n"
20382       "    {\n"
20383       "      return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
20384       "                               AndShouldNotBeConsiderAsInline,\n"
20385       "                               LambdaBodyMustBeBreak);\n"
20386       "    });",
20387       LLVMWithBeforeLambdaBody);
20388   verifyFormat("FctWithTwoParams_SLS_All(\n"
20389                "    []()\n"
20390                "    {\n"
20391                "      // A cool function...\n"
20392                "      return 43;\n"
20393                "    },\n"
20394                "    87);",
20395                LLVMWithBeforeLambdaBody);
20396   verifyFormat("FctWithTwoParams_SLS_All([]() { return 43; }, 87);",
20397                LLVMWithBeforeLambdaBody);
20398   verifyFormat("FctWithOneNestedLambdas_SLS_All([]() { return 17; });",
20399                LLVMWithBeforeLambdaBody);
20400   verifyFormat(
20401       "TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; }); });",
20402       LLVMWithBeforeLambdaBody);
20403   verifyFormat("TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; "
20404                "}); }, x);",
20405                LLVMWithBeforeLambdaBody);
20406   verifyFormat("TwoNestedLambdas_SLS_All(\n"
20407                "    []()\n"
20408                "    {\n"
20409                "      // A cool function...\n"
20410                "      return Call([]() { return 17; });\n"
20411                "    });",
20412                LLVMWithBeforeLambdaBody);
20413   verifyFormat("TwoNestedLambdas_SLS_All(\n"
20414                "    []()\n"
20415                "    {\n"
20416                "      return Call(\n"
20417                "          []()\n"
20418                "          {\n"
20419                "            // A cool function...\n"
20420                "            return 17;\n"
20421                "          });\n"
20422                "    });",
20423                LLVMWithBeforeLambdaBody);
20424 
20425   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
20426       FormatStyle::ShortLambdaStyle::SLS_None;
20427 
20428   verifyFormat("auto select = [this]() -> const Library::Object *\n"
20429                "{\n"
20430                "  return MyAssignment::SelectFromList(this);\n"
20431                "};\n",
20432                LLVMWithBeforeLambdaBody);
20433 
20434   verifyFormat("auto select = [this]() -> const Library::Object &\n"
20435                "{\n"
20436                "  return MyAssignment::SelectFromList(this);\n"
20437                "};\n",
20438                LLVMWithBeforeLambdaBody);
20439 
20440   verifyFormat("auto select = [this]() -> std::unique_ptr<Object>\n"
20441                "{\n"
20442                "  return MyAssignment::SelectFromList(this);\n"
20443                "};\n",
20444                LLVMWithBeforeLambdaBody);
20445 
20446   verifyFormat("namespace test {\n"
20447                "class Test {\n"
20448                "public:\n"
20449                "  Test() = default;\n"
20450                "};\n"
20451                "} // namespace test",
20452                LLVMWithBeforeLambdaBody);
20453 
20454   // Lambdas with different indentation styles.
20455   Style = getLLVMStyleWithColumns(100);
20456   EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n"
20457             "  return promise.then(\n"
20458             "      [this, &someVariable, someObject = "
20459             "std::mv(s)](std::vector<int> evaluated) mutable {\n"
20460             "        return someObject.startAsyncAction().then(\n"
20461             "            [this, &someVariable](AsyncActionResult result) "
20462             "mutable { result.processMore(); });\n"
20463             "      });\n"
20464             "}\n",
20465             format("SomeResult doSomething(SomeObject promise) {\n"
20466                    "  return promise.then([this, &someVariable, someObject = "
20467                    "std::mv(s)](std::vector<int> evaluated) mutable {\n"
20468                    "    return someObject.startAsyncAction().then([this, "
20469                    "&someVariable](AsyncActionResult result) mutable {\n"
20470                    "      result.processMore();\n"
20471                    "    });\n"
20472                    "  });\n"
20473                    "}\n",
20474                    Style));
20475   Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope;
20476   verifyFormat("test() {\n"
20477                "  ([]() -> {\n"
20478                "    int b = 32;\n"
20479                "    return 3;\n"
20480                "  }).foo();\n"
20481                "}",
20482                Style);
20483   verifyFormat("test() {\n"
20484                "  []() -> {\n"
20485                "    int b = 32;\n"
20486                "    return 3;\n"
20487                "  }\n"
20488                "}",
20489                Style);
20490   verifyFormat("std::sort(v.begin(), v.end(),\n"
20491                "          [](const auto &someLongArgumentName, const auto "
20492                "&someOtherLongArgumentName) {\n"
20493                "  return someLongArgumentName.someMemberVariable < "
20494                "someOtherLongArgumentName.someMemberVariable;\n"
20495                "});",
20496                Style);
20497   verifyFormat("test() {\n"
20498                "  (\n"
20499                "      []() -> {\n"
20500                "        int b = 32;\n"
20501                "        return 3;\n"
20502                "      },\n"
20503                "      foo, bar)\n"
20504                "      .foo();\n"
20505                "}",
20506                Style);
20507   verifyFormat("test() {\n"
20508                "  ([]() -> {\n"
20509                "    int b = 32;\n"
20510                "    return 3;\n"
20511                "  })\n"
20512                "      .foo()\n"
20513                "      .bar();\n"
20514                "}",
20515                Style);
20516   EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n"
20517             "  return promise.then(\n"
20518             "      [this, &someVariable, someObject = "
20519             "std::mv(s)](std::vector<int> evaluated) mutable {\n"
20520             "    return someObject.startAsyncAction().then(\n"
20521             "        [this, &someVariable](AsyncActionResult result) mutable { "
20522             "result.processMore(); });\n"
20523             "  });\n"
20524             "}\n",
20525             format("SomeResult doSomething(SomeObject promise) {\n"
20526                    "  return promise.then([this, &someVariable, someObject = "
20527                    "std::mv(s)](std::vector<int> evaluated) mutable {\n"
20528                    "    return someObject.startAsyncAction().then([this, "
20529                    "&someVariable](AsyncActionResult result) mutable {\n"
20530                    "      result.processMore();\n"
20531                    "    });\n"
20532                    "  });\n"
20533                    "}\n",
20534                    Style));
20535   EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n"
20536             "  return promise.then([this, &someVariable] {\n"
20537             "    return someObject.startAsyncAction().then(\n"
20538             "        [this, &someVariable](AsyncActionResult result) mutable { "
20539             "result.processMore(); });\n"
20540             "  });\n"
20541             "}\n",
20542             format("SomeResult doSomething(SomeObject promise) {\n"
20543                    "  return promise.then([this, &someVariable] {\n"
20544                    "    return someObject.startAsyncAction().then([this, "
20545                    "&someVariable](AsyncActionResult result) mutable {\n"
20546                    "      result.processMore();\n"
20547                    "    });\n"
20548                    "  });\n"
20549                    "}\n",
20550                    Style));
20551   Style = getGoogleStyle();
20552   Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope;
20553   EXPECT_EQ("#define A                                       \\\n"
20554             "  [] {                                          \\\n"
20555             "    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(        \\\n"
20556             "        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
20557             "      }",
20558             format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
20559                    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
20560                    Style));
20561   // TODO: The current formatting has a minor issue that's not worth fixing
20562   // right now whereby the closing brace is indented relative to the signature
20563   // instead of being aligned. This only happens with macros.
20564 }
20565 
20566 TEST_F(FormatTest, LambdaWithLineComments) {
20567   FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle();
20568   LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
20569   LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
20570   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
20571       FormatStyle::ShortLambdaStyle::SLS_All;
20572 
20573   verifyFormat("auto k = []() { return; }", LLVMWithBeforeLambdaBody);
20574   verifyFormat("auto k = []() // comment\n"
20575                "{ return; }",
20576                LLVMWithBeforeLambdaBody);
20577   verifyFormat("auto k = []() /* comment */ { return; }",
20578                LLVMWithBeforeLambdaBody);
20579   verifyFormat("auto k = []() /* comment */ /* comment */ { return; }",
20580                LLVMWithBeforeLambdaBody);
20581   verifyFormat("auto k = []() // X\n"
20582                "{ return; }",
20583                LLVMWithBeforeLambdaBody);
20584   verifyFormat(
20585       "auto k = []() // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"
20586       "{ return; }",
20587       LLVMWithBeforeLambdaBody);
20588 }
20589 
20590 TEST_F(FormatTest, EmptyLinesInLambdas) {
20591   verifyFormat("auto lambda = []() {\n"
20592                "  x(); //\n"
20593                "};",
20594                "auto lambda = []() {\n"
20595                "\n"
20596                "  x(); //\n"
20597                "\n"
20598                "};");
20599 }
20600 
20601 TEST_F(FormatTest, FormatsBlocks) {
20602   FormatStyle ShortBlocks = getLLVMStyle();
20603   ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
20604   verifyFormat("int (^Block)(int, int);", ShortBlocks);
20605   verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
20606   verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
20607   verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks);
20608   verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks);
20609   verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks);
20610 
20611   verifyFormat("foo(^{ bar(); });", ShortBlocks);
20612   verifyFormat("foo(a, ^{ bar(); });", ShortBlocks);
20613   verifyFormat("{ void (^block)(Object *x); }", ShortBlocks);
20614 
20615   verifyFormat("[operation setCompletionBlock:^{\n"
20616                "  [self onOperationDone];\n"
20617                "}];");
20618   verifyFormat("int i = {[operation setCompletionBlock:^{\n"
20619                "  [self onOperationDone];\n"
20620                "}]};");
20621   verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
20622                "  f();\n"
20623                "}];");
20624   verifyFormat("int a = [operation block:^int(int *i) {\n"
20625                "  return 1;\n"
20626                "}];");
20627   verifyFormat("[myObject doSomethingWith:arg1\n"
20628                "                      aaa:^int(int *a) {\n"
20629                "                        return 1;\n"
20630                "                      }\n"
20631                "                      bbb:f(a * bbbbbbbb)];");
20632 
20633   verifyFormat("[operation setCompletionBlock:^{\n"
20634                "  [self.delegate newDataAvailable];\n"
20635                "}];",
20636                getLLVMStyleWithColumns(60));
20637   verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
20638                "  NSString *path = [self sessionFilePath];\n"
20639                "  if (path) {\n"
20640                "    // ...\n"
20641                "  }\n"
20642                "});");
20643   verifyFormat("[[SessionService sharedService]\n"
20644                "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
20645                "      if (window) {\n"
20646                "        [self windowDidLoad:window];\n"
20647                "      } else {\n"
20648                "        [self errorLoadingWindow];\n"
20649                "      }\n"
20650                "    }];");
20651   verifyFormat("void (^largeBlock)(void) = ^{\n"
20652                "  // ...\n"
20653                "};\n",
20654                getLLVMStyleWithColumns(40));
20655   verifyFormat("[[SessionService sharedService]\n"
20656                "    loadWindowWithCompletionBlock: //\n"
20657                "        ^(SessionWindow *window) {\n"
20658                "          if (window) {\n"
20659                "            [self windowDidLoad:window];\n"
20660                "          } else {\n"
20661                "            [self errorLoadingWindow];\n"
20662                "          }\n"
20663                "        }];",
20664                getLLVMStyleWithColumns(60));
20665   verifyFormat("[myObject doSomethingWith:arg1\n"
20666                "    firstBlock:^(Foo *a) {\n"
20667                "      // ...\n"
20668                "      int i;\n"
20669                "    }\n"
20670                "    secondBlock:^(Bar *b) {\n"
20671                "      // ...\n"
20672                "      int i;\n"
20673                "    }\n"
20674                "    thirdBlock:^Foo(Bar *b) {\n"
20675                "      // ...\n"
20676                "      int i;\n"
20677                "    }];");
20678   verifyFormat("[myObject doSomethingWith:arg1\n"
20679                "               firstBlock:-1\n"
20680                "              secondBlock:^(Bar *b) {\n"
20681                "                // ...\n"
20682                "                int i;\n"
20683                "              }];");
20684 
20685   verifyFormat("f(^{\n"
20686                "  @autoreleasepool {\n"
20687                "    if (a) {\n"
20688                "      g();\n"
20689                "    }\n"
20690                "  }\n"
20691                "});");
20692   verifyFormat("Block b = ^int *(A *a, B *b) {}");
20693   verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n"
20694                "};");
20695 
20696   FormatStyle FourIndent = getLLVMStyle();
20697   FourIndent.ObjCBlockIndentWidth = 4;
20698   verifyFormat("[operation setCompletionBlock:^{\n"
20699                "    [self onOperationDone];\n"
20700                "}];",
20701                FourIndent);
20702 }
20703 
20704 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) {
20705   FormatStyle ZeroColumn = getLLVMStyle();
20706   ZeroColumn.ColumnLimit = 0;
20707 
20708   verifyFormat("[[SessionService sharedService] "
20709                "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
20710                "  if (window) {\n"
20711                "    [self windowDidLoad:window];\n"
20712                "  } else {\n"
20713                "    [self errorLoadingWindow];\n"
20714                "  }\n"
20715                "}];",
20716                ZeroColumn);
20717   EXPECT_EQ("[[SessionService sharedService]\n"
20718             "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
20719             "      if (window) {\n"
20720             "        [self windowDidLoad:window];\n"
20721             "      } else {\n"
20722             "        [self errorLoadingWindow];\n"
20723             "      }\n"
20724             "    }];",
20725             format("[[SessionService sharedService]\n"
20726                    "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
20727                    "                if (window) {\n"
20728                    "    [self windowDidLoad:window];\n"
20729                    "  } else {\n"
20730                    "    [self errorLoadingWindow];\n"
20731                    "  }\n"
20732                    "}];",
20733                    ZeroColumn));
20734   verifyFormat("[myObject doSomethingWith:arg1\n"
20735                "    firstBlock:^(Foo *a) {\n"
20736                "      // ...\n"
20737                "      int i;\n"
20738                "    }\n"
20739                "    secondBlock:^(Bar *b) {\n"
20740                "      // ...\n"
20741                "      int i;\n"
20742                "    }\n"
20743                "    thirdBlock:^Foo(Bar *b) {\n"
20744                "      // ...\n"
20745                "      int i;\n"
20746                "    }];",
20747                ZeroColumn);
20748   verifyFormat("f(^{\n"
20749                "  @autoreleasepool {\n"
20750                "    if (a) {\n"
20751                "      g();\n"
20752                "    }\n"
20753                "  }\n"
20754                "});",
20755                ZeroColumn);
20756   verifyFormat("void (^largeBlock)(void) = ^{\n"
20757                "  // ...\n"
20758                "};",
20759                ZeroColumn);
20760 
20761   ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
20762   EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };",
20763             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
20764   ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
20765   EXPECT_EQ("void (^largeBlock)(void) = ^{\n"
20766             "  int i;\n"
20767             "};",
20768             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
20769 }
20770 
20771 TEST_F(FormatTest, SupportsCRLF) {
20772   EXPECT_EQ("int a;\r\n"
20773             "int b;\r\n"
20774             "int c;\r\n",
20775             format("int a;\r\n"
20776                    "  int b;\r\n"
20777                    "    int c;\r\n",
20778                    getLLVMStyle()));
20779   EXPECT_EQ("int a;\r\n"
20780             "int b;\r\n"
20781             "int c;\r\n",
20782             format("int a;\r\n"
20783                    "  int b;\n"
20784                    "    int c;\r\n",
20785                    getLLVMStyle()));
20786   EXPECT_EQ("int a;\n"
20787             "int b;\n"
20788             "int c;\n",
20789             format("int a;\r\n"
20790                    "  int b;\n"
20791                    "    int c;\n",
20792                    getLLVMStyle()));
20793   EXPECT_EQ("\"aaaaaaa \"\r\n"
20794             "\"bbbbbbb\";\r\n",
20795             format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
20796   EXPECT_EQ("#define A \\\r\n"
20797             "  b;      \\\r\n"
20798             "  c;      \\\r\n"
20799             "  d;\r\n",
20800             format("#define A \\\r\n"
20801                    "  b; \\\r\n"
20802                    "  c; d; \r\n",
20803                    getGoogleStyle()));
20804 
20805   EXPECT_EQ("/*\r\n"
20806             "multi line block comments\r\n"
20807             "should not introduce\r\n"
20808             "an extra carriage return\r\n"
20809             "*/\r\n",
20810             format("/*\r\n"
20811                    "multi line block comments\r\n"
20812                    "should not introduce\r\n"
20813                    "an extra carriage return\r\n"
20814                    "*/\r\n"));
20815   EXPECT_EQ("/*\r\n"
20816             "\r\n"
20817             "*/",
20818             format("/*\r\n"
20819                    "    \r\r\r\n"
20820                    "*/"));
20821 
20822   FormatStyle style = getLLVMStyle();
20823 
20824   style.DeriveLineEnding = true;
20825   style.UseCRLF = false;
20826   EXPECT_EQ("union FooBarBazQux {\n"
20827             "  int foo;\n"
20828             "  int bar;\n"
20829             "  int baz;\n"
20830             "};",
20831             format("union FooBarBazQux {\r\n"
20832                    "  int foo;\n"
20833                    "  int bar;\r\n"
20834                    "  int baz;\n"
20835                    "};",
20836                    style));
20837   style.UseCRLF = true;
20838   EXPECT_EQ("union FooBarBazQux {\r\n"
20839             "  int foo;\r\n"
20840             "  int bar;\r\n"
20841             "  int baz;\r\n"
20842             "};",
20843             format("union FooBarBazQux {\r\n"
20844                    "  int foo;\n"
20845                    "  int bar;\r\n"
20846                    "  int baz;\n"
20847                    "};",
20848                    style));
20849 
20850   style.DeriveLineEnding = false;
20851   style.UseCRLF = false;
20852   EXPECT_EQ("union FooBarBazQux {\n"
20853             "  int foo;\n"
20854             "  int bar;\n"
20855             "  int baz;\n"
20856             "  int qux;\n"
20857             "};",
20858             format("union FooBarBazQux {\r\n"
20859                    "  int foo;\n"
20860                    "  int bar;\r\n"
20861                    "  int baz;\n"
20862                    "  int qux;\r\n"
20863                    "};",
20864                    style));
20865   style.UseCRLF = true;
20866   EXPECT_EQ("union FooBarBazQux {\r\n"
20867             "  int foo;\r\n"
20868             "  int bar;\r\n"
20869             "  int baz;\r\n"
20870             "  int qux;\r\n"
20871             "};",
20872             format("union FooBarBazQux {\r\n"
20873                    "  int foo;\n"
20874                    "  int bar;\r\n"
20875                    "  int baz;\n"
20876                    "  int qux;\n"
20877                    "};",
20878                    style));
20879 
20880   style.DeriveLineEnding = true;
20881   style.UseCRLF = false;
20882   EXPECT_EQ("union FooBarBazQux {\r\n"
20883             "  int foo;\r\n"
20884             "  int bar;\r\n"
20885             "  int baz;\r\n"
20886             "  int qux;\r\n"
20887             "};",
20888             format("union FooBarBazQux {\r\n"
20889                    "  int foo;\n"
20890                    "  int bar;\r\n"
20891                    "  int baz;\n"
20892                    "  int qux;\r\n"
20893                    "};",
20894                    style));
20895   style.UseCRLF = true;
20896   EXPECT_EQ("union FooBarBazQux {\n"
20897             "  int foo;\n"
20898             "  int bar;\n"
20899             "  int baz;\n"
20900             "  int qux;\n"
20901             "};",
20902             format("union FooBarBazQux {\r\n"
20903                    "  int foo;\n"
20904                    "  int bar;\r\n"
20905                    "  int baz;\n"
20906                    "  int qux;\n"
20907                    "};",
20908                    style));
20909 }
20910 
20911 TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
20912   verifyFormat("MY_CLASS(C) {\n"
20913                "  int i;\n"
20914                "  int j;\n"
20915                "};");
20916 }
20917 
20918 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
20919   FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
20920   TwoIndent.ContinuationIndentWidth = 2;
20921 
20922   EXPECT_EQ("int i =\n"
20923             "  longFunction(\n"
20924             "    arg);",
20925             format("int i = longFunction(arg);", TwoIndent));
20926 
20927   FormatStyle SixIndent = getLLVMStyleWithColumns(20);
20928   SixIndent.ContinuationIndentWidth = 6;
20929 
20930   EXPECT_EQ("int i =\n"
20931             "      longFunction(\n"
20932             "            arg);",
20933             format("int i = longFunction(arg);", SixIndent));
20934 }
20935 
20936 TEST_F(FormatTest, WrappedClosingParenthesisIndent) {
20937   FormatStyle Style = getLLVMStyle();
20938   verifyFormat("int Foo::getter(\n"
20939                "    //\n"
20940                ") const {\n"
20941                "  return foo;\n"
20942                "}",
20943                Style);
20944   verifyFormat("void Foo::setter(\n"
20945                "    //\n"
20946                ") {\n"
20947                "  foo = 1;\n"
20948                "}",
20949                Style);
20950 }
20951 
20952 TEST_F(FormatTest, SpacesInAngles) {
20953   FormatStyle Spaces = getLLVMStyle();
20954   Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
20955 
20956   verifyFormat("vector< ::std::string > x1;", Spaces);
20957   verifyFormat("Foo< int, Bar > x2;", Spaces);
20958   verifyFormat("Foo< ::int, ::Bar > x3;", Spaces);
20959 
20960   verifyFormat("static_cast< int >(arg);", Spaces);
20961   verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
20962   verifyFormat("f< int, float >();", Spaces);
20963   verifyFormat("template <> g() {}", Spaces);
20964   verifyFormat("template < std::vector< int > > f() {}", Spaces);
20965   verifyFormat("std::function< void(int, int) > fct;", Spaces);
20966   verifyFormat("void inFunction() { std::function< void(int, int) > fct; }",
20967                Spaces);
20968 
20969   Spaces.Standard = FormatStyle::LS_Cpp03;
20970   Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
20971   verifyFormat("A< A< int > >();", Spaces);
20972 
20973   Spaces.SpacesInAngles = FormatStyle::SIAS_Never;
20974   verifyFormat("A<A<int> >();", Spaces);
20975 
20976   Spaces.SpacesInAngles = FormatStyle::SIAS_Leave;
20977   verifyFormat("vector< ::std::string> x4;", "vector<::std::string> x4;",
20978                Spaces);
20979   verifyFormat("vector< ::std::string > x4;", "vector<::std::string > x4;",
20980                Spaces);
20981 
20982   verifyFormat("A<A<int> >();", Spaces);
20983   verifyFormat("A<A<int> >();", "A<A<int>>();", Spaces);
20984   verifyFormat("A< A< int > >();", Spaces);
20985 
20986   Spaces.Standard = FormatStyle::LS_Cpp11;
20987   Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
20988   verifyFormat("A< A< int > >();", Spaces);
20989 
20990   Spaces.SpacesInAngles = FormatStyle::SIAS_Never;
20991   verifyFormat("vector<::std::string> x4;", Spaces);
20992   verifyFormat("vector<int> x5;", Spaces);
20993   verifyFormat("Foo<int, Bar> x6;", Spaces);
20994   verifyFormat("Foo<::int, ::Bar> x7;", Spaces);
20995 
20996   verifyFormat("A<A<int>>();", Spaces);
20997 
20998   Spaces.SpacesInAngles = FormatStyle::SIAS_Leave;
20999   verifyFormat("vector<::std::string> x4;", Spaces);
21000   verifyFormat("vector< ::std::string > x4;", Spaces);
21001   verifyFormat("vector<int> x5;", Spaces);
21002   verifyFormat("vector< int > x5;", Spaces);
21003   verifyFormat("Foo<int, Bar> x6;", Spaces);
21004   verifyFormat("Foo< int, Bar > x6;", Spaces);
21005   verifyFormat("Foo<::int, ::Bar> x7;", Spaces);
21006   verifyFormat("Foo< ::int, ::Bar > x7;", Spaces);
21007 
21008   verifyFormat("A<A<int>>();", Spaces);
21009   verifyFormat("A< A< int > >();", Spaces);
21010   verifyFormat("A<A<int > >();", Spaces);
21011   verifyFormat("A< A< int>>();", Spaces);
21012 }
21013 
21014 TEST_F(FormatTest, SpaceAfterTemplateKeyword) {
21015   FormatStyle Style = getLLVMStyle();
21016   Style.SpaceAfterTemplateKeyword = false;
21017   verifyFormat("template<int> void foo();", Style);
21018 }
21019 
21020 TEST_F(FormatTest, TripleAngleBrackets) {
21021   verifyFormat("f<<<1, 1>>>();");
21022   verifyFormat("f<<<1, 1, 1, s>>>();");
21023   verifyFormat("f<<<a, b, c, d>>>();");
21024   EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();"));
21025   verifyFormat("f<param><<<1, 1>>>();");
21026   verifyFormat("f<1><<<1, 1>>>();");
21027   EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();"));
21028   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
21029                "aaaaaaaaaaa<<<\n    1, 1>>>();");
21030   verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n"
21031                "    <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();");
21032 }
21033 
21034 TEST_F(FormatTest, MergeLessLessAtEnd) {
21035   verifyFormat("<<");
21036   EXPECT_EQ("< < <", format("\\\n<<<"));
21037   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
21038                "aaallvm::outs() <<");
21039   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
21040                "aaaallvm::outs()\n    <<");
21041 }
21042 
21043 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
21044   std::string code = "#if A\n"
21045                      "#if B\n"
21046                      "a.\n"
21047                      "#endif\n"
21048                      "    a = 1;\n"
21049                      "#else\n"
21050                      "#endif\n"
21051                      "#if C\n"
21052                      "#else\n"
21053                      "#endif\n";
21054   EXPECT_EQ(code, format(code));
21055 }
21056 
21057 TEST_F(FormatTest, HandleConflictMarkers) {
21058   // Git/SVN conflict markers.
21059   EXPECT_EQ("int a;\n"
21060             "void f() {\n"
21061             "  callme(some(parameter1,\n"
21062             "<<<<<<< text by the vcs\n"
21063             "              parameter2),\n"
21064             "||||||| text by the vcs\n"
21065             "              parameter2),\n"
21066             "         parameter3,\n"
21067             "======= text by the vcs\n"
21068             "              parameter2, parameter3),\n"
21069             ">>>>>>> text by the vcs\n"
21070             "         otherparameter);\n",
21071             format("int a;\n"
21072                    "void f() {\n"
21073                    "  callme(some(parameter1,\n"
21074                    "<<<<<<< text by the vcs\n"
21075                    "  parameter2),\n"
21076                    "||||||| text by the vcs\n"
21077                    "  parameter2),\n"
21078                    "  parameter3,\n"
21079                    "======= text by the vcs\n"
21080                    "  parameter2,\n"
21081                    "  parameter3),\n"
21082                    ">>>>>>> text by the vcs\n"
21083                    "  otherparameter);\n"));
21084 
21085   // Perforce markers.
21086   EXPECT_EQ("void f() {\n"
21087             "  function(\n"
21088             ">>>> text by the vcs\n"
21089             "      parameter,\n"
21090             "==== text by the vcs\n"
21091             "      parameter,\n"
21092             "==== text by the vcs\n"
21093             "      parameter,\n"
21094             "<<<< text by the vcs\n"
21095             "      parameter);\n",
21096             format("void f() {\n"
21097                    "  function(\n"
21098                    ">>>> text by the vcs\n"
21099                    "  parameter,\n"
21100                    "==== text by the vcs\n"
21101                    "  parameter,\n"
21102                    "==== text by the vcs\n"
21103                    "  parameter,\n"
21104                    "<<<< text by the vcs\n"
21105                    "  parameter);\n"));
21106 
21107   EXPECT_EQ("<<<<<<<\n"
21108             "|||||||\n"
21109             "=======\n"
21110             ">>>>>>>",
21111             format("<<<<<<<\n"
21112                    "|||||||\n"
21113                    "=======\n"
21114                    ">>>>>>>"));
21115 
21116   EXPECT_EQ("<<<<<<<\n"
21117             "|||||||\n"
21118             "int i;\n"
21119             "=======\n"
21120             ">>>>>>>",
21121             format("<<<<<<<\n"
21122                    "|||||||\n"
21123                    "int i;\n"
21124                    "=======\n"
21125                    ">>>>>>>"));
21126 
21127   // FIXME: Handle parsing of macros around conflict markers correctly:
21128   EXPECT_EQ("#define Macro \\\n"
21129             "<<<<<<<\n"
21130             "Something \\\n"
21131             "|||||||\n"
21132             "Else \\\n"
21133             "=======\n"
21134             "Other \\\n"
21135             ">>>>>>>\n"
21136             "    End int i;\n",
21137             format("#define Macro \\\n"
21138                    "<<<<<<<\n"
21139                    "  Something \\\n"
21140                    "|||||||\n"
21141                    "  Else \\\n"
21142                    "=======\n"
21143                    "  Other \\\n"
21144                    ">>>>>>>\n"
21145                    "  End\n"
21146                    "int i;\n"));
21147 
21148   verifyFormat(R"(====
21149 #ifdef A
21150 a
21151 #else
21152 b
21153 #endif
21154 )");
21155 }
21156 
21157 TEST_F(FormatTest, DisableRegions) {
21158   EXPECT_EQ("int i;\n"
21159             "// clang-format off\n"
21160             "  int j;\n"
21161             "// clang-format on\n"
21162             "int k;",
21163             format(" int  i;\n"
21164                    "   // clang-format off\n"
21165                    "  int j;\n"
21166                    " // clang-format on\n"
21167                    "   int   k;"));
21168   EXPECT_EQ("int i;\n"
21169             "/* clang-format off */\n"
21170             "  int j;\n"
21171             "/* clang-format on */\n"
21172             "int k;",
21173             format(" int  i;\n"
21174                    "   /* clang-format off */\n"
21175                    "  int j;\n"
21176                    " /* clang-format on */\n"
21177                    "   int   k;"));
21178 
21179   // Don't reflow comments within disabled regions.
21180   EXPECT_EQ("// clang-format off\n"
21181             "// long long long long long long line\n"
21182             "/* clang-format on */\n"
21183             "/* long long long\n"
21184             " * long long long\n"
21185             " * line */\n"
21186             "int i;\n"
21187             "/* clang-format off */\n"
21188             "/* long long long long long long line */\n",
21189             format("// clang-format off\n"
21190                    "// long long long long long long line\n"
21191                    "/* clang-format on */\n"
21192                    "/* long long long long long long line */\n"
21193                    "int i;\n"
21194                    "/* clang-format off */\n"
21195                    "/* long long long long long long line */\n",
21196                    getLLVMStyleWithColumns(20)));
21197 }
21198 
21199 TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
21200   format("? ) =");
21201   verifyNoCrash("#define a\\\n /**/}");
21202 }
21203 
21204 TEST_F(FormatTest, FormatsTableGenCode) {
21205   FormatStyle Style = getLLVMStyle();
21206   Style.Language = FormatStyle::LK_TableGen;
21207   verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style);
21208 }
21209 
21210 TEST_F(FormatTest, ArrayOfTemplates) {
21211   EXPECT_EQ("auto a = new unique_ptr<int>[10];",
21212             format("auto a = new unique_ptr<int > [ 10];"));
21213 
21214   FormatStyle Spaces = getLLVMStyle();
21215   Spaces.SpacesInSquareBrackets = true;
21216   EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];",
21217             format("auto a = new unique_ptr<int > [10];", Spaces));
21218 }
21219 
21220 TEST_F(FormatTest, ArrayAsTemplateType) {
21221   EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;",
21222             format("auto a = unique_ptr < Foo < Bar>[ 10]> ;"));
21223 
21224   FormatStyle Spaces = getLLVMStyle();
21225   Spaces.SpacesInSquareBrackets = true;
21226   EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;",
21227             format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces));
21228 }
21229 
21230 TEST_F(FormatTest, NoSpaceAfterSuper) { verifyFormat("__super::FooBar();"); }
21231 
21232 TEST(FormatStyle, GetStyleWithEmptyFileName) {
21233   llvm::vfs::InMemoryFileSystem FS;
21234   auto Style1 = getStyle("file", "", "Google", "", &FS);
21235   ASSERT_TRUE((bool)Style1);
21236   ASSERT_EQ(*Style1, getGoogleStyle());
21237 }
21238 
21239 TEST(FormatStyle, GetStyleOfFile) {
21240   llvm::vfs::InMemoryFileSystem FS;
21241   // Test 1: format file in the same directory.
21242   ASSERT_TRUE(
21243       FS.addFile("/a/.clang-format", 0,
21244                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM")));
21245   ASSERT_TRUE(
21246       FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
21247   auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS);
21248   ASSERT_TRUE((bool)Style1);
21249   ASSERT_EQ(*Style1, getLLVMStyle());
21250 
21251   // Test 2.1: fallback to default.
21252   ASSERT_TRUE(
21253       FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
21254   auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS);
21255   ASSERT_TRUE((bool)Style2);
21256   ASSERT_EQ(*Style2, getMozillaStyle());
21257 
21258   // Test 2.2: no format on 'none' fallback style.
21259   Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
21260   ASSERT_TRUE((bool)Style2);
21261   ASSERT_EQ(*Style2, getNoStyle());
21262 
21263   // Test 2.3: format if config is found with no based style while fallback is
21264   // 'none'.
21265   ASSERT_TRUE(FS.addFile("/b/.clang-format", 0,
21266                          llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2")));
21267   Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
21268   ASSERT_TRUE((bool)Style2);
21269   ASSERT_EQ(*Style2, getLLVMStyle());
21270 
21271   // Test 2.4: format if yaml with no based style, while fallback is 'none'.
21272   Style2 = getStyle("{}", "a.h", "none", "", &FS);
21273   ASSERT_TRUE((bool)Style2);
21274   ASSERT_EQ(*Style2, getLLVMStyle());
21275 
21276   // Test 3: format file in parent directory.
21277   ASSERT_TRUE(
21278       FS.addFile("/c/.clang-format", 0,
21279                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google")));
21280   ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0,
21281                          llvm::MemoryBuffer::getMemBuffer("int i;")));
21282   auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS);
21283   ASSERT_TRUE((bool)Style3);
21284   ASSERT_EQ(*Style3, getGoogleStyle());
21285 
21286   // Test 4: error on invalid fallback style
21287   auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS);
21288   ASSERT_FALSE((bool)Style4);
21289   llvm::consumeError(Style4.takeError());
21290 
21291   // Test 5: error on invalid yaml on command line
21292   auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS);
21293   ASSERT_FALSE((bool)Style5);
21294   llvm::consumeError(Style5.takeError());
21295 
21296   // Test 6: error on invalid style
21297   auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS);
21298   ASSERT_FALSE((bool)Style6);
21299   llvm::consumeError(Style6.takeError());
21300 
21301   // Test 7: found config file, error on parsing it
21302   ASSERT_TRUE(
21303       FS.addFile("/d/.clang-format", 0,
21304                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n"
21305                                                   "InvalidKey: InvalidValue")));
21306   ASSERT_TRUE(
21307       FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
21308   auto Style7a = getStyle("file", "/d/.clang-format", "LLVM", "", &FS);
21309   ASSERT_FALSE((bool)Style7a);
21310   llvm::consumeError(Style7a.takeError());
21311 
21312   auto Style7b = getStyle("file", "/d/.clang-format", "LLVM", "", &FS, true);
21313   ASSERT_TRUE((bool)Style7b);
21314 
21315   // Test 8: inferred per-language defaults apply.
21316   auto StyleTd = getStyle("file", "x.td", "llvm", "", &FS);
21317   ASSERT_TRUE((bool)StyleTd);
21318   ASSERT_EQ(*StyleTd, getLLVMStyle(FormatStyle::LK_TableGen));
21319 
21320   // Test 9.1: overwriting a file style, when parent no file exists with no
21321   // fallback style
21322   ASSERT_TRUE(FS.addFile(
21323       "/e/sub/.clang-format", 0,
21324       llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: InheritParentConfig\n"
21325                                        "ColumnLimit: 20")));
21326   ASSERT_TRUE(FS.addFile("/e/sub/code.cpp", 0,
21327                          llvm::MemoryBuffer::getMemBuffer("int i;")));
21328   auto Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS);
21329   ASSERT_TRUE(static_cast<bool>(Style9));
21330   ASSERT_EQ(*Style9, [] {
21331     auto Style = getNoStyle();
21332     Style.ColumnLimit = 20;
21333     return Style;
21334   }());
21335 
21336   // Test 9.2: with LLVM fallback style
21337   Style9 = getStyle("file", "/e/sub/code.cpp", "LLVM", "", &FS);
21338   ASSERT_TRUE(static_cast<bool>(Style9));
21339   ASSERT_EQ(*Style9, [] {
21340     auto Style = getLLVMStyle();
21341     Style.ColumnLimit = 20;
21342     return Style;
21343   }());
21344 
21345   // Test 9.3: with a parent file
21346   ASSERT_TRUE(
21347       FS.addFile("/e/.clang-format", 0,
21348                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google\n"
21349                                                   "UseTab: Always")));
21350   Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS);
21351   ASSERT_TRUE(static_cast<bool>(Style9));
21352   ASSERT_EQ(*Style9, [] {
21353     auto Style = getGoogleStyle();
21354     Style.ColumnLimit = 20;
21355     Style.UseTab = FormatStyle::UT_Always;
21356     return Style;
21357   }());
21358 
21359   // Test 9.4: propagate more than one level
21360   ASSERT_TRUE(FS.addFile("/e/sub/sub/code.cpp", 0,
21361                          llvm::MemoryBuffer::getMemBuffer("int i;")));
21362   ASSERT_TRUE(FS.addFile("/e/sub/sub/.clang-format", 0,
21363                          llvm::MemoryBuffer::getMemBuffer(
21364                              "BasedOnStyle: InheritParentConfig\n"
21365                              "WhitespaceSensitiveMacros: ['FOO', 'BAR']")));
21366   std::vector<std::string> NonDefaultWhiteSpaceMacros{"FOO", "BAR"};
21367 
21368   const auto SubSubStyle = [&NonDefaultWhiteSpaceMacros] {
21369     auto Style = getGoogleStyle();
21370     Style.ColumnLimit = 20;
21371     Style.UseTab = FormatStyle::UT_Always;
21372     Style.WhitespaceSensitiveMacros = NonDefaultWhiteSpaceMacros;
21373     return Style;
21374   }();
21375 
21376   ASSERT_NE(Style9->WhitespaceSensitiveMacros, NonDefaultWhiteSpaceMacros);
21377   Style9 = getStyle("file", "/e/sub/sub/code.cpp", "none", "", &FS);
21378   ASSERT_TRUE(static_cast<bool>(Style9));
21379   ASSERT_EQ(*Style9, SubSubStyle);
21380 
21381   // Test 9.5: use InheritParentConfig as style name
21382   Style9 =
21383       getStyle("inheritparentconfig", "/e/sub/sub/code.cpp", "none", "", &FS);
21384   ASSERT_TRUE(static_cast<bool>(Style9));
21385   ASSERT_EQ(*Style9, SubSubStyle);
21386 
21387   // Test 9.6: use command line style with inheritance
21388   Style9 = getStyle("{BasedOnStyle: InheritParentConfig}", "/e/sub/code.cpp",
21389                     "none", "", &FS);
21390   ASSERT_TRUE(static_cast<bool>(Style9));
21391   ASSERT_EQ(*Style9, SubSubStyle);
21392 
21393   // Test 9.7: use command line style with inheritance and own config
21394   Style9 = getStyle("{BasedOnStyle: InheritParentConfig, "
21395                     "WhitespaceSensitiveMacros: ['FOO', 'BAR']}",
21396                     "/e/sub/code.cpp", "none", "", &FS);
21397   ASSERT_TRUE(static_cast<bool>(Style9));
21398   ASSERT_EQ(*Style9, SubSubStyle);
21399 
21400   // Test 9.8: use inheritance from a file without BasedOnStyle
21401   ASSERT_TRUE(FS.addFile("/e/withoutbase/.clang-format", 0,
21402                          llvm::MemoryBuffer::getMemBuffer("ColumnLimit: 123")));
21403   ASSERT_TRUE(
21404       FS.addFile("/e/withoutbase/sub/.clang-format", 0,
21405                  llvm::MemoryBuffer::getMemBuffer(
21406                      "BasedOnStyle: InheritParentConfig\nIndentWidth: 7")));
21407   // Make sure we do not use the fallback style
21408   Style9 = getStyle("file", "/e/withoutbase/code.cpp", "google", "", &FS);
21409   ASSERT_TRUE(static_cast<bool>(Style9));
21410   ASSERT_EQ(*Style9, [] {
21411     auto Style = getLLVMStyle();
21412     Style.ColumnLimit = 123;
21413     return Style;
21414   }());
21415 
21416   Style9 = getStyle("file", "/e/withoutbase/sub/code.cpp", "google", "", &FS);
21417   ASSERT_TRUE(static_cast<bool>(Style9));
21418   ASSERT_EQ(*Style9, [] {
21419     auto Style = getLLVMStyle();
21420     Style.ColumnLimit = 123;
21421     Style.IndentWidth = 7;
21422     return Style;
21423   }());
21424 }
21425 
21426 TEST_F(ReplacementTest, FormatCodeAfterReplacements) {
21427   // Column limit is 20.
21428   std::string Code = "Type *a =\n"
21429                      "    new Type();\n"
21430                      "g(iiiii, 0, jjjjj,\n"
21431                      "  0, kkkkk, 0, mm);\n"
21432                      "int  bad     = format   ;";
21433   std::string Expected = "auto a = new Type();\n"
21434                          "g(iiiii, nullptr,\n"
21435                          "  jjjjj, nullptr,\n"
21436                          "  kkkkk, nullptr,\n"
21437                          "  mm);\n"
21438                          "int  bad     = format   ;";
21439   FileID ID = Context.createInMemoryFile("format.cpp", Code);
21440   tooling::Replacements Replaces = toReplacements(
21441       {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6,
21442                             "auto "),
21443        tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1,
21444                             "nullptr"),
21445        tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1,
21446                             "nullptr"),
21447        tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1,
21448                             "nullptr")});
21449 
21450   format::FormatStyle Style = format::getLLVMStyle();
21451   Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility.
21452   auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
21453   EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
21454       << llvm::toString(FormattedReplaces.takeError()) << "\n";
21455   auto Result = applyAllReplacements(Code, *FormattedReplaces);
21456   EXPECT_TRUE(static_cast<bool>(Result));
21457   EXPECT_EQ(Expected, *Result);
21458 }
21459 
21460 TEST_F(ReplacementTest, SortIncludesAfterReplacement) {
21461   std::string Code = "#include \"a.h\"\n"
21462                      "#include \"c.h\"\n"
21463                      "\n"
21464                      "int main() {\n"
21465                      "  return 0;\n"
21466                      "}";
21467   std::string Expected = "#include \"a.h\"\n"
21468                          "#include \"b.h\"\n"
21469                          "#include \"c.h\"\n"
21470                          "\n"
21471                          "int main() {\n"
21472                          "  return 0;\n"
21473                          "}";
21474   FileID ID = Context.createInMemoryFile("fix.cpp", Code);
21475   tooling::Replacements Replaces = toReplacements(
21476       {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0,
21477                             "#include \"b.h\"\n")});
21478 
21479   format::FormatStyle Style = format::getLLVMStyle();
21480   Style.SortIncludes = FormatStyle::SI_CaseSensitive;
21481   auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
21482   EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
21483       << llvm::toString(FormattedReplaces.takeError()) << "\n";
21484   auto Result = applyAllReplacements(Code, *FormattedReplaces);
21485   EXPECT_TRUE(static_cast<bool>(Result));
21486   EXPECT_EQ(Expected, *Result);
21487 }
21488 
21489 TEST_F(FormatTest, FormatSortsUsingDeclarations) {
21490   EXPECT_EQ("using std::cin;\n"
21491             "using std::cout;",
21492             format("using std::cout;\n"
21493                    "using std::cin;",
21494                    getGoogleStyle()));
21495 }
21496 
21497 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) {
21498   format::FormatStyle Style = format::getLLVMStyle();
21499   Style.Standard = FormatStyle::LS_Cpp03;
21500   // cpp03 recognize this string as identifier u8 and literal character 'a'
21501   EXPECT_EQ("auto c = u8 'a';", format("auto c = u8'a';", Style));
21502 }
21503 
21504 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) {
21505   // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers
21506   // all modes, including C++11, C++14 and C++17
21507   EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';"));
21508 }
21509 
21510 TEST_F(FormatTest, DoNotFormatLikelyXml) {
21511   EXPECT_EQ("<!-- ;> -->", format("<!-- ;> -->", getGoogleStyle()));
21512   EXPECT_EQ(" <!-- >; -->", format(" <!-- >; -->", getGoogleStyle()));
21513 }
21514 
21515 TEST_F(FormatTest, StructuredBindings) {
21516   // Structured bindings is a C++17 feature.
21517   // all modes, including C++11, C++14 and C++17
21518   verifyFormat("auto [a, b] = f();");
21519   EXPECT_EQ("auto [a, b] = f();", format("auto[a, b] = f();"));
21520   EXPECT_EQ("const auto [a, b] = f();", format("const   auto[a, b] = f();"));
21521   EXPECT_EQ("auto const [a, b] = f();", format("auto  const[a, b] = f();"));
21522   EXPECT_EQ("auto const volatile [a, b] = f();",
21523             format("auto  const   volatile[a, b] = f();"));
21524   EXPECT_EQ("auto [a, b, c] = f();", format("auto   [  a  ,  b,c   ] = f();"));
21525   EXPECT_EQ("auto &[a, b, c] = f();",
21526             format("auto   &[  a  ,  b,c   ] = f();"));
21527   EXPECT_EQ("auto &&[a, b, c] = f();",
21528             format("auto   &&[  a  ,  b,c   ] = f();"));
21529   EXPECT_EQ("auto const &[a, b] = f();", format("auto  const&[a, b] = f();"));
21530   EXPECT_EQ("auto const volatile &&[a, b] = f();",
21531             format("auto  const  volatile  &&[a, b] = f();"));
21532   EXPECT_EQ("auto const &&[a, b] = f();",
21533             format("auto  const   &&  [a, b] = f();"));
21534   EXPECT_EQ("const auto &[a, b] = f();",
21535             format("const  auto  &  [a, b] = f();"));
21536   EXPECT_EQ("const auto volatile &&[a, b] = f();",
21537             format("const  auto   volatile  &&[a, b] = f();"));
21538   EXPECT_EQ("volatile const auto &&[a, b] = f();",
21539             format("volatile  const  auto   &&[a, b] = f();"));
21540   EXPECT_EQ("const auto &&[a, b] = f();",
21541             format("const  auto  &&  [a, b] = f();"));
21542 
21543   // Make sure we don't mistake structured bindings for lambdas.
21544   FormatStyle PointerMiddle = getLLVMStyle();
21545   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
21546   verifyFormat("auto [a1, b]{A * i};", getGoogleStyle());
21547   verifyFormat("auto [a2, b]{A * i};", getLLVMStyle());
21548   verifyFormat("auto [a3, b]{A * i};", PointerMiddle);
21549   verifyFormat("auto const [a1, b]{A * i};", getGoogleStyle());
21550   verifyFormat("auto const [a2, b]{A * i};", getLLVMStyle());
21551   verifyFormat("auto const [a3, b]{A * i};", PointerMiddle);
21552   verifyFormat("auto const& [a1, b]{A * i};", getGoogleStyle());
21553   verifyFormat("auto const &[a2, b]{A * i};", getLLVMStyle());
21554   verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle);
21555   verifyFormat("auto const&& [a1, b]{A * i};", getGoogleStyle());
21556   verifyFormat("auto const &&[a2, b]{A * i};", getLLVMStyle());
21557   verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle);
21558 
21559   EXPECT_EQ("for (const auto &&[a, b] : some_range) {\n}",
21560             format("for (const auto   &&   [a, b] : some_range) {\n}"));
21561   EXPECT_EQ("for (const auto &[a, b] : some_range) {\n}",
21562             format("for (const auto   &   [a, b] : some_range) {\n}"));
21563   EXPECT_EQ("for (const auto [a, b] : some_range) {\n}",
21564             format("for (const auto[a, b] : some_range) {\n}"));
21565   EXPECT_EQ("auto [x, y](expr);", format("auto[x,y]  (expr);"));
21566   EXPECT_EQ("auto &[x, y](expr);", format("auto  &  [x,y]  (expr);"));
21567   EXPECT_EQ("auto &&[x, y](expr);", format("auto  &&  [x,y]  (expr);"));
21568   EXPECT_EQ("auto const &[x, y](expr);",
21569             format("auto  const  &  [x,y]  (expr);"));
21570   EXPECT_EQ("auto const &&[x, y](expr);",
21571             format("auto  const  &&  [x,y]  (expr);"));
21572   EXPECT_EQ("auto [x, y]{expr};", format("auto[x,y]     {expr};"));
21573   EXPECT_EQ("auto const &[x, y]{expr};",
21574             format("auto  const  &  [x,y]  {expr};"));
21575   EXPECT_EQ("auto const &&[x, y]{expr};",
21576             format("auto  const  &&  [x,y]  {expr};"));
21577 
21578   format::FormatStyle Spaces = format::getLLVMStyle();
21579   Spaces.SpacesInSquareBrackets = true;
21580   verifyFormat("auto [ a, b ] = f();", Spaces);
21581   verifyFormat("auto &&[ a, b ] = f();", Spaces);
21582   verifyFormat("auto &[ a, b ] = f();", Spaces);
21583   verifyFormat("auto const &&[ a, b ] = f();", Spaces);
21584   verifyFormat("auto const &[ a, b ] = f();", Spaces);
21585 }
21586 
21587 TEST_F(FormatTest, FileAndCode) {
21588   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.cc", ""));
21589   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.m", ""));
21590   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.mm", ""));
21591   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", ""));
21592   EXPECT_EQ(FormatStyle::LK_ObjC,
21593             guessLanguage("foo.h", "@interface Foo\n@end\n"));
21594   EXPECT_EQ(
21595       FormatStyle::LK_ObjC,
21596       guessLanguage("foo.h", "#define TRY(x, y) @try { x; } @finally { y; }"));
21597   EXPECT_EQ(FormatStyle::LK_ObjC,
21598             guessLanguage("foo.h", "#define AVAIL(x) @available(x, *))"));
21599   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@class Foo;"));
21600   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo", ""));
21601   EXPECT_EQ(FormatStyle::LK_ObjC,
21602             guessLanguage("foo", "@interface Foo\n@end\n"));
21603   EXPECT_EQ(FormatStyle::LK_ObjC,
21604             guessLanguage("foo.h", "int DoStuff(CGRect rect);\n"));
21605   EXPECT_EQ(
21606       FormatStyle::LK_ObjC,
21607       guessLanguage("foo.h",
21608                     "#define MY_POINT_MAKE(x, y) CGPointMake((x), (y));\n"));
21609   EXPECT_EQ(
21610       FormatStyle::LK_Cpp,
21611       guessLanguage("foo.h", "#define FOO(...) auto bar = [] __VA_ARGS__;"));
21612 }
21613 
21614 TEST_F(FormatTest, GuessLanguageWithCpp11AttributeSpecifiers) {
21615   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[noreturn]];"));
21616   EXPECT_EQ(FormatStyle::LK_ObjC,
21617             guessLanguage("foo.h", "array[[calculator getIndex]];"));
21618   EXPECT_EQ(FormatStyle::LK_Cpp,
21619             guessLanguage("foo.h", "[[noreturn, deprecated(\"so sorry\")]];"));
21620   EXPECT_EQ(
21621       FormatStyle::LK_Cpp,
21622       guessLanguage("foo.h", "[[noreturn, deprecated(\"gone, sorry\")]];"));
21623   EXPECT_EQ(FormatStyle::LK_ObjC,
21624             guessLanguage("foo.h", "[[noreturn foo] bar];"));
21625   EXPECT_EQ(FormatStyle::LK_Cpp,
21626             guessLanguage("foo.h", "[[clang::fallthrough]];"));
21627   EXPECT_EQ(FormatStyle::LK_ObjC,
21628             guessLanguage("foo.h", "[[clang:fallthrough] foo];"));
21629   EXPECT_EQ(FormatStyle::LK_Cpp,
21630             guessLanguage("foo.h", "[[gsl::suppress(\"type\")]];"));
21631   EXPECT_EQ(FormatStyle::LK_Cpp,
21632             guessLanguage("foo.h", "[[using clang: fallthrough]];"));
21633   EXPECT_EQ(FormatStyle::LK_ObjC,
21634             guessLanguage("foo.h", "[[abusing clang:fallthrough] bar];"));
21635   EXPECT_EQ(FormatStyle::LK_Cpp,
21636             guessLanguage("foo.h", "[[using gsl: suppress(\"type\")]];"));
21637   EXPECT_EQ(
21638       FormatStyle::LK_Cpp,
21639       guessLanguage("foo.h", "for (auto &&[endpoint, stream] : streams_)"));
21640   EXPECT_EQ(
21641       FormatStyle::LK_Cpp,
21642       guessLanguage("foo.h",
21643                     "[[clang::callable_when(\"unconsumed\", \"unknown\")]]"));
21644   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[foo::bar, ...]]"));
21645 }
21646 
21647 TEST_F(FormatTest, GuessLanguageWithCaret) {
21648   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^);"));
21649   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^, Bar);"));
21650   EXPECT_EQ(FormatStyle::LK_ObjC,
21651             guessLanguage("foo.h", "int(^)(char, float);"));
21652   EXPECT_EQ(FormatStyle::LK_ObjC,
21653             guessLanguage("foo.h", "int(^foo)(char, float);"));
21654   EXPECT_EQ(FormatStyle::LK_ObjC,
21655             guessLanguage("foo.h", "int(^foo[10])(char, float);"));
21656   EXPECT_EQ(FormatStyle::LK_ObjC,
21657             guessLanguage("foo.h", "int(^foo[kNumEntries])(char, float);"));
21658   EXPECT_EQ(
21659       FormatStyle::LK_ObjC,
21660       guessLanguage("foo.h", "int(^foo[(kNumEntries + 10)])(char, float);"));
21661 }
21662 
21663 TEST_F(FormatTest, GuessLanguageWithPragmas) {
21664   EXPECT_EQ(FormatStyle::LK_Cpp,
21665             guessLanguage("foo.h", "__pragma(warning(disable:))"));
21666   EXPECT_EQ(FormatStyle::LK_Cpp,
21667             guessLanguage("foo.h", "#pragma(warning(disable:))"));
21668   EXPECT_EQ(FormatStyle::LK_Cpp,
21669             guessLanguage("foo.h", "_Pragma(warning(disable:))"));
21670 }
21671 
21672 TEST_F(FormatTest, FormatsInlineAsmSymbolicNames) {
21673   // ASM symbolic names are identifiers that must be surrounded by [] without
21674   // space in between:
21675   // https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#InputOperands
21676 
21677   // Example from https://bugs.llvm.org/show_bug.cgi?id=45108.
21678   verifyFormat(R"(//
21679 asm volatile("mrs %x[result], FPCR" : [result] "=r"(result));
21680 )");
21681 
21682   // A list of several ASM symbolic names.
21683   verifyFormat(R"(asm("mov %[e], %[d]" : [d] "=rm"(d), [e] "rm"(*e));)");
21684 
21685   // ASM symbolic names in inline ASM with inputs and outputs.
21686   verifyFormat(R"(//
21687 asm("cmoveq %1, %2, %[result]"
21688     : [result] "=r"(result)
21689     : "r"(test), "r"(new), "[result]"(old));
21690 )");
21691 
21692   // ASM symbolic names in inline ASM with no outputs.
21693   verifyFormat(R"(asm("mov %[e], %[d]" : : [d] "=rm"(d), [e] "rm"(*e));)");
21694 }
21695 
21696 TEST_F(FormatTest, GuessedLanguageWithInlineAsmClobbers) {
21697   EXPECT_EQ(FormatStyle::LK_Cpp,
21698             guessLanguage("foo.h", "void f() {\n"
21699                                    "  asm (\"mov %[e], %[d]\"\n"
21700                                    "     : [d] \"=rm\" (d)\n"
21701                                    "       [e] \"rm\" (*e));\n"
21702                                    "}"));
21703   EXPECT_EQ(FormatStyle::LK_Cpp,
21704             guessLanguage("foo.h", "void f() {\n"
21705                                    "  _asm (\"mov %[e], %[d]\"\n"
21706                                    "     : [d] \"=rm\" (d)\n"
21707                                    "       [e] \"rm\" (*e));\n"
21708                                    "}"));
21709   EXPECT_EQ(FormatStyle::LK_Cpp,
21710             guessLanguage("foo.h", "void f() {\n"
21711                                    "  __asm (\"mov %[e], %[d]\"\n"
21712                                    "     : [d] \"=rm\" (d)\n"
21713                                    "       [e] \"rm\" (*e));\n"
21714                                    "}"));
21715   EXPECT_EQ(FormatStyle::LK_Cpp,
21716             guessLanguage("foo.h", "void f() {\n"
21717                                    "  __asm__ (\"mov %[e], %[d]\"\n"
21718                                    "     : [d] \"=rm\" (d)\n"
21719                                    "       [e] \"rm\" (*e));\n"
21720                                    "}"));
21721   EXPECT_EQ(FormatStyle::LK_Cpp,
21722             guessLanguage("foo.h", "void f() {\n"
21723                                    "  asm (\"mov %[e], %[d]\"\n"
21724                                    "     : [d] \"=rm\" (d),\n"
21725                                    "       [e] \"rm\" (*e));\n"
21726                                    "}"));
21727   EXPECT_EQ(FormatStyle::LK_Cpp,
21728             guessLanguage("foo.h", "void f() {\n"
21729                                    "  asm volatile (\"mov %[e], %[d]\"\n"
21730                                    "     : [d] \"=rm\" (d)\n"
21731                                    "       [e] \"rm\" (*e));\n"
21732                                    "}"));
21733 }
21734 
21735 TEST_F(FormatTest, GuessLanguageWithChildLines) {
21736   EXPECT_EQ(FormatStyle::LK_Cpp,
21737             guessLanguage("foo.h", "#define FOO ({ std::string s; })"));
21738   EXPECT_EQ(FormatStyle::LK_ObjC,
21739             guessLanguage("foo.h", "#define FOO ({ NSString *s; })"));
21740   EXPECT_EQ(
21741       FormatStyle::LK_Cpp,
21742       guessLanguage("foo.h", "#define FOO ({ foo(); ({ std::string s; }) })"));
21743   EXPECT_EQ(
21744       FormatStyle::LK_ObjC,
21745       guessLanguage("foo.h", "#define FOO ({ foo(); ({ NSString *s; }) })"));
21746 }
21747 
21748 TEST_F(FormatTest, TypenameMacros) {
21749   std::vector<std::string> TypenameMacros = {"STACK_OF", "LIST", "TAILQ_ENTRY"};
21750 
21751   // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=30353
21752   FormatStyle Google = getGoogleStyleWithColumns(0);
21753   Google.TypenameMacros = TypenameMacros;
21754   verifyFormat("struct foo {\n"
21755                "  int bar;\n"
21756                "  TAILQ_ENTRY(a) bleh;\n"
21757                "};",
21758                Google);
21759 
21760   FormatStyle Macros = getLLVMStyle();
21761   Macros.TypenameMacros = TypenameMacros;
21762 
21763   verifyFormat("STACK_OF(int) a;", Macros);
21764   verifyFormat("STACK_OF(int) *a;", Macros);
21765   verifyFormat("STACK_OF(int const *) *a;", Macros);
21766   verifyFormat("STACK_OF(int *const) *a;", Macros);
21767   verifyFormat("STACK_OF(int, string) a;", Macros);
21768   verifyFormat("STACK_OF(LIST(int)) a;", Macros);
21769   verifyFormat("STACK_OF(LIST(int)) a, b;", Macros);
21770   verifyFormat("for (LIST(int) *a = NULL; a;) {\n}", Macros);
21771   verifyFormat("STACK_OF(int) f(LIST(int) *arg);", Macros);
21772   verifyFormat("vector<LIST(uint64_t) *attr> x;", Macros);
21773   verifyFormat("vector<LIST(uint64_t) *const> f(LIST(uint64_t) *arg);", Macros);
21774 
21775   Macros.PointerAlignment = FormatStyle::PAS_Left;
21776   verifyFormat("STACK_OF(int)* a;", Macros);
21777   verifyFormat("STACK_OF(int*)* a;", Macros);
21778   verifyFormat("x = (STACK_OF(uint64_t))*a;", Macros);
21779   verifyFormat("x = (STACK_OF(uint64_t))&a;", Macros);
21780   verifyFormat("vector<STACK_OF(uint64_t)* attr> x;", Macros);
21781 }
21782 
21783 TEST_F(FormatTest, AtomicQualifier) {
21784   // Check that we treate _Atomic as a type and not a function call
21785   FormatStyle Google = getGoogleStyleWithColumns(0);
21786   verifyFormat("struct foo {\n"
21787                "  int a1;\n"
21788                "  _Atomic(a) a2;\n"
21789                "  _Atomic(_Atomic(int) *const) a3;\n"
21790                "};",
21791                Google);
21792   verifyFormat("_Atomic(uint64_t) a;");
21793   verifyFormat("_Atomic(uint64_t) *a;");
21794   verifyFormat("_Atomic(uint64_t const *) *a;");
21795   verifyFormat("_Atomic(uint64_t *const) *a;");
21796   verifyFormat("_Atomic(const uint64_t *) *a;");
21797   verifyFormat("_Atomic(uint64_t) a;");
21798   verifyFormat("_Atomic(_Atomic(uint64_t)) a;");
21799   verifyFormat("_Atomic(_Atomic(uint64_t)) a, b;");
21800   verifyFormat("for (_Atomic(uint64_t) *a = NULL; a;) {\n}");
21801   verifyFormat("_Atomic(uint64_t) f(_Atomic(uint64_t) *arg);");
21802 
21803   verifyFormat("_Atomic(uint64_t) *s(InitValue);");
21804   verifyFormat("_Atomic(uint64_t) *s{InitValue};");
21805   FormatStyle Style = getLLVMStyle();
21806   Style.PointerAlignment = FormatStyle::PAS_Left;
21807   verifyFormat("_Atomic(uint64_t)* s(InitValue);", Style);
21808   verifyFormat("_Atomic(uint64_t)* s{InitValue};", Style);
21809   verifyFormat("_Atomic(int)* a;", Style);
21810   verifyFormat("_Atomic(int*)* a;", Style);
21811   verifyFormat("vector<_Atomic(uint64_t)* attr> x;", Style);
21812 
21813   Style.SpacesInCStyleCastParentheses = true;
21814   Style.SpacesInParentheses = false;
21815   verifyFormat("x = ( _Atomic(uint64_t) )*a;", Style);
21816   Style.SpacesInCStyleCastParentheses = false;
21817   Style.SpacesInParentheses = true;
21818   verifyFormat("x = (_Atomic( uint64_t ))*a;", Style);
21819   verifyFormat("x = (_Atomic( uint64_t ))&a;", Style);
21820 }
21821 
21822 TEST_F(FormatTest, AmbersandInLamda) {
21823   // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=41899
21824   FormatStyle AlignStyle = getLLVMStyle();
21825   AlignStyle.PointerAlignment = FormatStyle::PAS_Left;
21826   verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle);
21827   AlignStyle.PointerAlignment = FormatStyle::PAS_Right;
21828   verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle);
21829 }
21830 
21831 TEST_F(FormatTest, SpacesInConditionalStatement) {
21832   FormatStyle Spaces = getLLVMStyle();
21833   Spaces.IfMacros.clear();
21834   Spaces.IfMacros.push_back("MYIF");
21835   Spaces.SpacesInConditionalStatement = true;
21836   verifyFormat("for ( int i = 0; i; i++ )\n  continue;", Spaces);
21837   verifyFormat("if ( !a )\n  return;", Spaces);
21838   verifyFormat("if ( a )\n  return;", Spaces);
21839   verifyFormat("if constexpr ( a )\n  return;", Spaces);
21840   verifyFormat("MYIF ( a )\n  return;", Spaces);
21841   verifyFormat("MYIF ( a )\n  return;\nelse MYIF ( b )\n  return;", Spaces);
21842   verifyFormat("MYIF ( a )\n  return;\nelse\n  return;", Spaces);
21843   verifyFormat("switch ( a )\ncase 1:\n  return;", Spaces);
21844   verifyFormat("while ( a )\n  return;", Spaces);
21845   verifyFormat("while ( (a && b) )\n  return;", Spaces);
21846   verifyFormat("do {\n} while ( 1 != 0 );", Spaces);
21847   verifyFormat("try {\n} catch ( const std::exception & ) {\n}", Spaces);
21848   // Check that space on the left of "::" is inserted as expected at beginning
21849   // of condition.
21850   verifyFormat("while ( ::func() )\n  return;", Spaces);
21851 
21852   // Check impact of ControlStatementsExceptControlMacros is honored.
21853   Spaces.SpaceBeforeParens =
21854       FormatStyle::SBPO_ControlStatementsExceptControlMacros;
21855   verifyFormat("MYIF( a )\n  return;", Spaces);
21856   verifyFormat("MYIF( a )\n  return;\nelse MYIF( b )\n  return;", Spaces);
21857   verifyFormat("MYIF( a )\n  return;\nelse\n  return;", Spaces);
21858 }
21859 
21860 TEST_F(FormatTest, AlternativeOperators) {
21861   // Test case for ensuring alternate operators are not
21862   // combined with their right most neighbour.
21863   verifyFormat("int a and b;");
21864   verifyFormat("int a and_eq b;");
21865   verifyFormat("int a bitand b;");
21866   verifyFormat("int a bitor b;");
21867   verifyFormat("int a compl b;");
21868   verifyFormat("int a not b;");
21869   verifyFormat("int a not_eq b;");
21870   verifyFormat("int a or b;");
21871   verifyFormat("int a xor b;");
21872   verifyFormat("int a xor_eq b;");
21873   verifyFormat("return this not_eq bitand other;");
21874   verifyFormat("bool operator not_eq(const X bitand other)");
21875 
21876   verifyFormat("int a and 5;");
21877   verifyFormat("int a and_eq 5;");
21878   verifyFormat("int a bitand 5;");
21879   verifyFormat("int a bitor 5;");
21880   verifyFormat("int a compl 5;");
21881   verifyFormat("int a not 5;");
21882   verifyFormat("int a not_eq 5;");
21883   verifyFormat("int a or 5;");
21884   verifyFormat("int a xor 5;");
21885   verifyFormat("int a xor_eq 5;");
21886 
21887   verifyFormat("int a compl(5);");
21888   verifyFormat("int a not(5);");
21889 
21890   /* FIXME handle alternate tokens
21891    * https://en.cppreference.com/w/cpp/language/operator_alternative
21892   // alternative tokens
21893   verifyFormat("compl foo();");     //  ~foo();
21894   verifyFormat("foo() <%%>;");      // foo();
21895   verifyFormat("void foo() <%%>;"); // void foo(){}
21896   verifyFormat("int a <:1:>;");     // int a[1];[
21897   verifyFormat("%:define ABC abc"); // #define ABC abc
21898   verifyFormat("%:%:");             // ##
21899   */
21900 }
21901 
21902 TEST_F(FormatTest, STLWhileNotDefineChed) {
21903   verifyFormat("#if defined(while)\n"
21904                "#define while EMIT WARNING C4005\n"
21905                "#endif // while");
21906 }
21907 
21908 TEST_F(FormatTest, OperatorSpacing) {
21909   FormatStyle Style = getLLVMStyle();
21910   Style.PointerAlignment = FormatStyle::PAS_Right;
21911   verifyFormat("Foo::operator*();", Style);
21912   verifyFormat("Foo::operator void *();", Style);
21913   verifyFormat("Foo::operator void **();", Style);
21914   verifyFormat("Foo::operator void *&();", Style);
21915   verifyFormat("Foo::operator void *&&();", Style);
21916   verifyFormat("Foo::operator void const *();", Style);
21917   verifyFormat("Foo::operator void const **();", Style);
21918   verifyFormat("Foo::operator void const *&();", Style);
21919   verifyFormat("Foo::operator void const *&&();", Style);
21920   verifyFormat("Foo::operator()(void *);", Style);
21921   verifyFormat("Foo::operator*(void *);", Style);
21922   verifyFormat("Foo::operator*();", Style);
21923   verifyFormat("Foo::operator**();", Style);
21924   verifyFormat("Foo::operator&();", Style);
21925   verifyFormat("Foo::operator<int> *();", Style);
21926   verifyFormat("Foo::operator<Foo> *();", Style);
21927   verifyFormat("Foo::operator<int> **();", Style);
21928   verifyFormat("Foo::operator<Foo> **();", Style);
21929   verifyFormat("Foo::operator<int> &();", Style);
21930   verifyFormat("Foo::operator<Foo> &();", Style);
21931   verifyFormat("Foo::operator<int> &&();", Style);
21932   verifyFormat("Foo::operator<Foo> &&();", Style);
21933   verifyFormat("Foo::operator<int> *&();", Style);
21934   verifyFormat("Foo::operator<Foo> *&();", Style);
21935   verifyFormat("Foo::operator<int> *&&();", Style);
21936   verifyFormat("Foo::operator<Foo> *&&();", Style);
21937   verifyFormat("operator*(int (*)(), class Foo);", Style);
21938 
21939   verifyFormat("Foo::operator&();", Style);
21940   verifyFormat("Foo::operator void &();", Style);
21941   verifyFormat("Foo::operator void const &();", Style);
21942   verifyFormat("Foo::operator()(void &);", Style);
21943   verifyFormat("Foo::operator&(void &);", Style);
21944   verifyFormat("Foo::operator&();", Style);
21945   verifyFormat("operator&(int (&)(), class Foo);", Style);
21946   verifyFormat("operator&&(int (&)(), class Foo);", Style);
21947 
21948   verifyFormat("Foo::operator&&();", Style);
21949   verifyFormat("Foo::operator**();", Style);
21950   verifyFormat("Foo::operator void &&();", Style);
21951   verifyFormat("Foo::operator void const &&();", Style);
21952   verifyFormat("Foo::operator()(void &&);", Style);
21953   verifyFormat("Foo::operator&&(void &&);", Style);
21954   verifyFormat("Foo::operator&&();", Style);
21955   verifyFormat("operator&&(int (&&)(), class Foo);", Style);
21956   verifyFormat("operator const nsTArrayRight<E> &()", Style);
21957   verifyFormat("[[nodiscard]] operator const nsTArrayRight<E, Allocator> &()",
21958                Style);
21959   verifyFormat("operator void **()", Style);
21960   verifyFormat("operator const FooRight<Object> &()", Style);
21961   verifyFormat("operator const FooRight<Object> *()", Style);
21962   verifyFormat("operator const FooRight<Object> **()", Style);
21963   verifyFormat("operator const FooRight<Object> *&()", Style);
21964   verifyFormat("operator const FooRight<Object> *&&()", Style);
21965 
21966   Style.PointerAlignment = FormatStyle::PAS_Left;
21967   verifyFormat("Foo::operator*();", Style);
21968   verifyFormat("Foo::operator**();", Style);
21969   verifyFormat("Foo::operator void*();", Style);
21970   verifyFormat("Foo::operator void**();", Style);
21971   verifyFormat("Foo::operator void*&();", Style);
21972   verifyFormat("Foo::operator void*&&();", Style);
21973   verifyFormat("Foo::operator void const*();", Style);
21974   verifyFormat("Foo::operator void const**();", Style);
21975   verifyFormat("Foo::operator void const*&();", Style);
21976   verifyFormat("Foo::operator void const*&&();", Style);
21977   verifyFormat("Foo::operator/*comment*/ void*();", Style);
21978   verifyFormat("Foo::operator/*a*/ const /*b*/ void*();", Style);
21979   verifyFormat("Foo::operator/*a*/ volatile /*b*/ void*();", Style);
21980   verifyFormat("Foo::operator()(void*);", Style);
21981   verifyFormat("Foo::operator*(void*);", Style);
21982   verifyFormat("Foo::operator*();", Style);
21983   verifyFormat("Foo::operator<int>*();", Style);
21984   verifyFormat("Foo::operator<Foo>*();", Style);
21985   verifyFormat("Foo::operator<int>**();", Style);
21986   verifyFormat("Foo::operator<Foo>**();", Style);
21987   verifyFormat("Foo::operator<Foo>*&();", Style);
21988   verifyFormat("Foo::operator<int>&();", Style);
21989   verifyFormat("Foo::operator<Foo>&();", Style);
21990   verifyFormat("Foo::operator<int>&&();", Style);
21991   verifyFormat("Foo::operator<Foo>&&();", Style);
21992   verifyFormat("Foo::operator<int>*&();", Style);
21993   verifyFormat("Foo::operator<Foo>*&();", Style);
21994   verifyFormat("operator*(int (*)(), class Foo);", Style);
21995 
21996   verifyFormat("Foo::operator&();", Style);
21997   verifyFormat("Foo::operator void&();", Style);
21998   verifyFormat("Foo::operator void const&();", Style);
21999   verifyFormat("Foo::operator/*comment*/ void&();", Style);
22000   verifyFormat("Foo::operator/*a*/ const /*b*/ void&();", Style);
22001   verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&();", Style);
22002   verifyFormat("Foo::operator()(void&);", Style);
22003   verifyFormat("Foo::operator&(void&);", Style);
22004   verifyFormat("Foo::operator&();", Style);
22005   verifyFormat("operator&(int (&)(), class Foo);", Style);
22006   verifyFormat("operator&(int (&&)(), class Foo);", Style);
22007   verifyFormat("operator&&(int (&&)(), class Foo);", Style);
22008 
22009   verifyFormat("Foo::operator&&();", Style);
22010   verifyFormat("Foo::operator void&&();", Style);
22011   verifyFormat("Foo::operator void const&&();", Style);
22012   verifyFormat("Foo::operator/*comment*/ void&&();", Style);
22013   verifyFormat("Foo::operator/*a*/ const /*b*/ void&&();", Style);
22014   verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&&();", Style);
22015   verifyFormat("Foo::operator()(void&&);", Style);
22016   verifyFormat("Foo::operator&&(void&&);", Style);
22017   verifyFormat("Foo::operator&&();", Style);
22018   verifyFormat("operator&&(int (&&)(), class Foo);", Style);
22019   verifyFormat("operator const nsTArrayLeft<E>&()", Style);
22020   verifyFormat("[[nodiscard]] operator const nsTArrayLeft<E, Allocator>&()",
22021                Style);
22022   verifyFormat("operator void**()", Style);
22023   verifyFormat("operator const FooLeft<Object>&()", Style);
22024   verifyFormat("operator const FooLeft<Object>*()", Style);
22025   verifyFormat("operator const FooLeft<Object>**()", Style);
22026   verifyFormat("operator const FooLeft<Object>*&()", Style);
22027   verifyFormat("operator const FooLeft<Object>*&&()", Style);
22028 
22029   // PR45107
22030   verifyFormat("operator Vector<String>&();", Style);
22031   verifyFormat("operator const Vector<String>&();", Style);
22032   verifyFormat("operator foo::Bar*();", Style);
22033   verifyFormat("operator const Foo<X>::Bar<Y>*();", Style);
22034   verifyFormat("operator/*a*/ const /*b*/ Foo /*c*/<X> /*d*/ ::Bar<Y>*();",
22035                Style);
22036 
22037   Style.PointerAlignment = FormatStyle::PAS_Middle;
22038   verifyFormat("Foo::operator*();", Style);
22039   verifyFormat("Foo::operator void *();", Style);
22040   verifyFormat("Foo::operator()(void *);", Style);
22041   verifyFormat("Foo::operator*(void *);", Style);
22042   verifyFormat("Foo::operator*();", Style);
22043   verifyFormat("operator*(int (*)(), class Foo);", Style);
22044 
22045   verifyFormat("Foo::operator&();", Style);
22046   verifyFormat("Foo::operator void &();", Style);
22047   verifyFormat("Foo::operator void const &();", Style);
22048   verifyFormat("Foo::operator()(void &);", Style);
22049   verifyFormat("Foo::operator&(void &);", Style);
22050   verifyFormat("Foo::operator&();", Style);
22051   verifyFormat("operator&(int (&)(), class Foo);", Style);
22052 
22053   verifyFormat("Foo::operator&&();", Style);
22054   verifyFormat("Foo::operator void &&();", Style);
22055   verifyFormat("Foo::operator void const &&();", Style);
22056   verifyFormat("Foo::operator()(void &&);", Style);
22057   verifyFormat("Foo::operator&&(void &&);", Style);
22058   verifyFormat("Foo::operator&&();", Style);
22059   verifyFormat("operator&&(int (&&)(), class Foo);", Style);
22060 }
22061 
22062 TEST_F(FormatTest, OperatorPassedAsAFunctionPtr) {
22063   FormatStyle Style = getLLVMStyle();
22064   // PR46157
22065   verifyFormat("foo(operator+, -42);", Style);
22066   verifyFormat("foo(operator++, -42);", Style);
22067   verifyFormat("foo(operator--, -42);", Style);
22068   verifyFormat("foo(-42, operator--);", Style);
22069   verifyFormat("foo(-42, operator, );", Style);
22070   verifyFormat("foo(operator, , -42);", Style);
22071 }
22072 
22073 TEST_F(FormatTest, WhitespaceSensitiveMacros) {
22074   FormatStyle Style = getLLVMStyle();
22075   Style.WhitespaceSensitiveMacros.push_back("FOO");
22076 
22077   // Don't use the helpers here, since 'mess up' will change the whitespace
22078   // and these are all whitespace sensitive by definition
22079   EXPECT_EQ("FOO(String-ized&Messy+But(: :Still)=Intentional);",
22080             format("FOO(String-ized&Messy+But(: :Still)=Intentional);", Style));
22081   EXPECT_EQ(
22082       "FOO(String-ized&Messy+But\\(: :Still)=Intentional);",
22083       format("FOO(String-ized&Messy+But\\(: :Still)=Intentional);", Style));
22084   EXPECT_EQ("FOO(String-ized&Messy+But,: :Still=Intentional);",
22085             format("FOO(String-ized&Messy+But,: :Still=Intentional);", Style));
22086   EXPECT_EQ("FOO(String-ized&Messy+But,: :\n"
22087             "       Still=Intentional);",
22088             format("FOO(String-ized&Messy+But,: :\n"
22089                    "       Still=Intentional);",
22090                    Style));
22091   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
22092   EXPECT_EQ("FOO(String-ized=&Messy+But,: :\n"
22093             "       Still=Intentional);",
22094             format("FOO(String-ized=&Messy+But,: :\n"
22095                    "       Still=Intentional);",
22096                    Style));
22097 
22098   Style.ColumnLimit = 21;
22099   EXPECT_EQ("FOO(String-ized&Messy+But: :Still=Intentional);",
22100             format("FOO(String-ized&Messy+But: :Still=Intentional);", Style));
22101 }
22102 
22103 TEST_F(FormatTest, VeryLongNamespaceCommentSplit) {
22104   // These tests are not in NamespaceFixer because that doesn't
22105   // test its interaction with line wrapping
22106   FormatStyle Style = getLLVMStyle();
22107   Style.ColumnLimit = 80;
22108   verifyFormat("namespace {\n"
22109                "int i;\n"
22110                "int j;\n"
22111                "} // namespace",
22112                Style);
22113 
22114   verifyFormat("namespace AAA {\n"
22115                "int i;\n"
22116                "int j;\n"
22117                "} // namespace AAA",
22118                Style);
22119 
22120   EXPECT_EQ("namespace Averyveryveryverylongnamespace {\n"
22121             "int i;\n"
22122             "int j;\n"
22123             "} // namespace Averyveryveryverylongnamespace",
22124             format("namespace Averyveryveryverylongnamespace {\n"
22125                    "int i;\n"
22126                    "int j;\n"
22127                    "}",
22128                    Style));
22129 
22130   EXPECT_EQ(
22131       "namespace "
22132       "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n"
22133       "    went::mad::now {\n"
22134       "int i;\n"
22135       "int j;\n"
22136       "} // namespace\n"
22137       "  // "
22138       "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::"
22139       "went::mad::now",
22140       format("namespace "
22141              "would::it::save::you::a::lot::of::time::if_::i::"
22142              "just::gave::up::and_::went::mad::now {\n"
22143              "int i;\n"
22144              "int j;\n"
22145              "}",
22146              Style));
22147 
22148   // This used to duplicate the comment again and again on subsequent runs
22149   EXPECT_EQ(
22150       "namespace "
22151       "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n"
22152       "    went::mad::now {\n"
22153       "int i;\n"
22154       "int j;\n"
22155       "} // namespace\n"
22156       "  // "
22157       "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::"
22158       "went::mad::now",
22159       format("namespace "
22160              "would::it::save::you::a::lot::of::time::if_::i::"
22161              "just::gave::up::and_::went::mad::now {\n"
22162              "int i;\n"
22163              "int j;\n"
22164              "} // namespace\n"
22165              "  // "
22166              "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::"
22167              "and_::went::mad::now",
22168              Style));
22169 }
22170 
22171 TEST_F(FormatTest, LikelyUnlikely) {
22172   FormatStyle Style = getLLVMStyle();
22173 
22174   verifyFormat("if (argc > 5) [[unlikely]] {\n"
22175                "  return 29;\n"
22176                "}",
22177                Style);
22178 
22179   verifyFormat("if (argc > 5) [[likely]] {\n"
22180                "  return 29;\n"
22181                "}",
22182                Style);
22183 
22184   verifyFormat("if (argc > 5) [[unlikely]] {\n"
22185                "  return 29;\n"
22186                "} else [[likely]] {\n"
22187                "  return 42;\n"
22188                "}\n",
22189                Style);
22190 
22191   verifyFormat("if (argc > 5) [[unlikely]] {\n"
22192                "  return 29;\n"
22193                "} else if (argc > 10) [[likely]] {\n"
22194                "  return 99;\n"
22195                "} else {\n"
22196                "  return 42;\n"
22197                "}\n",
22198                Style);
22199 
22200   verifyFormat("if (argc > 5) [[gnu::unused]] {\n"
22201                "  return 29;\n"
22202                "}",
22203                Style);
22204 }
22205 
22206 TEST_F(FormatTest, PenaltyIndentedWhitespace) {
22207   verifyFormat("Constructor()\n"
22208                "    : aaaaaa(aaaaaa), aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
22209                "                          aaaa(aaaaaaaaaaaaaaaaaa, "
22210                "aaaaaaaaaaaaaaaaaat))");
22211   verifyFormat("Constructor()\n"
22212                "    : aaaaaaaaaaaaa(aaaaaa), "
22213                "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)");
22214 
22215   FormatStyle StyleWithWhitespacePenalty = getLLVMStyle();
22216   StyleWithWhitespacePenalty.PenaltyIndentedWhitespace = 5;
22217   verifyFormat("Constructor()\n"
22218                "    : aaaaaa(aaaaaa),\n"
22219                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
22220                "          aaaa(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaat))",
22221                StyleWithWhitespacePenalty);
22222   verifyFormat("Constructor()\n"
22223                "    : aaaaaaaaaaaaa(aaaaaa), "
22224                "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)",
22225                StyleWithWhitespacePenalty);
22226 }
22227 
22228 TEST_F(FormatTest, LLVMDefaultStyle) {
22229   FormatStyle Style = getLLVMStyle();
22230   verifyFormat("extern \"C\" {\n"
22231                "int foo();\n"
22232                "}",
22233                Style);
22234 }
22235 TEST_F(FormatTest, GNUDefaultStyle) {
22236   FormatStyle Style = getGNUStyle();
22237   verifyFormat("extern \"C\"\n"
22238                "{\n"
22239                "  int foo ();\n"
22240                "}",
22241                Style);
22242 }
22243 TEST_F(FormatTest, MozillaDefaultStyle) {
22244   FormatStyle Style = getMozillaStyle();
22245   verifyFormat("extern \"C\"\n"
22246                "{\n"
22247                "  int foo();\n"
22248                "}",
22249                Style);
22250 }
22251 TEST_F(FormatTest, GoogleDefaultStyle) {
22252   FormatStyle Style = getGoogleStyle();
22253   verifyFormat("extern \"C\" {\n"
22254                "int foo();\n"
22255                "}",
22256                Style);
22257 }
22258 TEST_F(FormatTest, ChromiumDefaultStyle) {
22259   FormatStyle Style = getChromiumStyle(FormatStyle::LanguageKind::LK_Cpp);
22260   verifyFormat("extern \"C\" {\n"
22261                "int foo();\n"
22262                "}",
22263                Style);
22264 }
22265 TEST_F(FormatTest, MicrosoftDefaultStyle) {
22266   FormatStyle Style = getMicrosoftStyle(FormatStyle::LanguageKind::LK_Cpp);
22267   verifyFormat("extern \"C\"\n"
22268                "{\n"
22269                "    int foo();\n"
22270                "}",
22271                Style);
22272 }
22273 TEST_F(FormatTest, WebKitDefaultStyle) {
22274   FormatStyle Style = getWebKitStyle();
22275   verifyFormat("extern \"C\" {\n"
22276                "int foo();\n"
22277                "}",
22278                Style);
22279 }
22280 
22281 TEST_F(FormatTest, ConceptsAndRequires) {
22282   FormatStyle Style = getLLVMStyle();
22283   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
22284 
22285   verifyFormat("template <typename T>\n"
22286                "concept Hashable = requires(T a) {\n"
22287                "  { std::hash<T>{}(a) } -> std::convertible_to<std::size_t>;\n"
22288                "};",
22289                Style);
22290   verifyFormat("template <typename T>\n"
22291                "concept EqualityComparable = requires(T a, T b) {\n"
22292                "  { a == b } -> bool;\n"
22293                "};",
22294                Style);
22295   verifyFormat("template <typename T>\n"
22296                "concept EqualityComparable = requires(T a, T b) {\n"
22297                "  { a == b } -> bool;\n"
22298                "  { a != b } -> bool;\n"
22299                "};",
22300                Style);
22301   verifyFormat("template <typename T>\n"
22302                "concept EqualityComparable = requires(T a, T b) {\n"
22303                "  { a == b } -> bool;\n"
22304                "  { a != b } -> bool;\n"
22305                "};",
22306                Style);
22307 
22308   verifyFormat("template <typename It>\n"
22309                "requires Iterator<It>\n"
22310                "void sort(It begin, It end) {\n"
22311                "  //....\n"
22312                "}",
22313                Style);
22314 
22315   verifyFormat("template <typename T>\n"
22316                "concept Large = sizeof(T) > 10;",
22317                Style);
22318 
22319   verifyFormat("template <typename T, typename U>\n"
22320                "concept FooableWith = requires(T t, U u) {\n"
22321                "  typename T::foo_type;\n"
22322                "  { t.foo(u) } -> typename T::foo_type;\n"
22323                "  t++;\n"
22324                "};\n"
22325                "void doFoo(FooableWith<int> auto t) {\n"
22326                "  t.foo(3);\n"
22327                "}",
22328                Style);
22329   verifyFormat("template <typename T>\n"
22330                "concept Context = sizeof(T) == 1;",
22331                Style);
22332   verifyFormat("template <typename T>\n"
22333                "concept Context = is_specialization_of_v<context, T>;",
22334                Style);
22335   verifyFormat("template <typename T>\n"
22336                "concept Node = std::is_object_v<T>;",
22337                Style);
22338   verifyFormat("template <typename T>\n"
22339                "concept Tree = true;",
22340                Style);
22341 
22342   verifyFormat("template <typename T> int g(T i) requires Concept1<I> {\n"
22343                "  //...\n"
22344                "}",
22345                Style);
22346 
22347   verifyFormat(
22348       "template <typename T> int g(T i) requires Concept1<I> && Concept2<I> {\n"
22349       "  //...\n"
22350       "}",
22351       Style);
22352 
22353   verifyFormat(
22354       "template <typename T> int g(T i) requires Concept1<I> || Concept2<I> {\n"
22355       "  //...\n"
22356       "}",
22357       Style);
22358 
22359   verifyFormat("template <typename T>\n"
22360                "veryveryvery_long_return_type g(T i) requires Concept1<I> || "
22361                "Concept2<I> {\n"
22362                "  //...\n"
22363                "}",
22364                Style);
22365 
22366   verifyFormat("template <typename T>\n"
22367                "veryveryvery_long_return_type g(T i) requires Concept1<I> && "
22368                "Concept2<I> {\n"
22369                "  //...\n"
22370                "}",
22371                Style);
22372 
22373   verifyFormat(
22374       "template <typename T>\n"
22375       "veryveryvery_long_return_type g(T i) requires Concept1 && Concept2 {\n"
22376       "  //...\n"
22377       "}",
22378       Style);
22379 
22380   verifyFormat(
22381       "template <typename T>\n"
22382       "veryveryvery_long_return_type g(T i) requires Concept1 || Concept2 {\n"
22383       "  //...\n"
22384       "}",
22385       Style);
22386 
22387   verifyFormat("template <typename It>\n"
22388                "requires Foo<It>() && Bar<It> {\n"
22389                "  //....\n"
22390                "}",
22391                Style);
22392 
22393   verifyFormat("template <typename It>\n"
22394                "requires Foo<Bar<It>>() && Bar<Foo<It, It>> {\n"
22395                "  //....\n"
22396                "}",
22397                Style);
22398 
22399   verifyFormat("template <typename It>\n"
22400                "requires Foo<Bar<It, It>>() && Bar<Foo<It, It>> {\n"
22401                "  //....\n"
22402                "}",
22403                Style);
22404 
22405   verifyFormat(
22406       "template <typename It>\n"
22407       "requires Foo<Bar<It>, Baz<It>>() && Bar<Foo<It>, Baz<It, It>> {\n"
22408       "  //....\n"
22409       "}",
22410       Style);
22411 
22412   Style.IndentRequires = true;
22413   verifyFormat("template <typename It>\n"
22414                "  requires Iterator<It>\n"
22415                "void sort(It begin, It end) {\n"
22416                "  //....\n"
22417                "}",
22418                Style);
22419   verifyFormat("template <std::size index_>\n"
22420                "  requires(index_ < sizeof...(Children_))\n"
22421                "Tree auto &child() {\n"
22422                "  // ...\n"
22423                "}",
22424                Style);
22425 
22426   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
22427   verifyFormat("template <typename T>\n"
22428                "concept Hashable = requires (T a) {\n"
22429                "  { std::hash<T>{}(a) } -> std::convertible_to<std::size_t>;\n"
22430                "};",
22431                Style);
22432 
22433   verifyFormat("template <class T = void>\n"
22434                "  requires EqualityComparable<T> || Same<T, void>\n"
22435                "struct equal_to;",
22436                Style);
22437 
22438   verifyFormat("template <class T>\n"
22439                "  requires requires {\n"
22440                "    T{};\n"
22441                "    T (int);\n"
22442                "  }\n",
22443                Style);
22444 
22445   Style.ColumnLimit = 78;
22446   verifyFormat("template <typename T>\n"
22447                "concept Context = Traits<typename T::traits_type> and\n"
22448                "    Interface<typename T::interface_type> and\n"
22449                "    Request<typename T::request_type> and\n"
22450                "    Response<typename T::response_type> and\n"
22451                "    ContextExtension<typename T::extension_type> and\n"
22452                "    ::std::is_copy_constructable<T> and "
22453                "::std::is_move_constructable<T> and\n"
22454                "    requires (T c) {\n"
22455                "  { c.response; } -> Response;\n"
22456                "} and requires (T c) {\n"
22457                "  { c.request; } -> Request;\n"
22458                "}\n",
22459                Style);
22460 
22461   verifyFormat("template <typename T>\n"
22462                "concept Context = Traits<typename T::traits_type> or\n"
22463                "    Interface<typename T::interface_type> or\n"
22464                "    Request<typename T::request_type> or\n"
22465                "    Response<typename T::response_type> or\n"
22466                "    ContextExtension<typename T::extension_type> or\n"
22467                "    ::std::is_copy_constructable<T> or "
22468                "::std::is_move_constructable<T> or\n"
22469                "    requires (T c) {\n"
22470                "  { c.response; } -> Response;\n"
22471                "} or requires (T c) {\n"
22472                "  { c.request; } -> Request;\n"
22473                "}\n",
22474                Style);
22475 
22476   verifyFormat("template <typename T>\n"
22477                "concept Context = Traits<typename T::traits_type> &&\n"
22478                "    Interface<typename T::interface_type> &&\n"
22479                "    Request<typename T::request_type> &&\n"
22480                "    Response<typename T::response_type> &&\n"
22481                "    ContextExtension<typename T::extension_type> &&\n"
22482                "    ::std::is_copy_constructable<T> && "
22483                "::std::is_move_constructable<T> &&\n"
22484                "    requires (T c) {\n"
22485                "  { c.response; } -> Response;\n"
22486                "} && requires (T c) {\n"
22487                "  { c.request; } -> Request;\n"
22488                "}\n",
22489                Style);
22490 
22491   verifyFormat("template <typename T>\nconcept someConcept = Constraint1<T> && "
22492                "Constraint2<T>;");
22493 
22494   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
22495   Style.BraceWrapping.AfterFunction = true;
22496   Style.BraceWrapping.AfterClass = true;
22497   Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
22498   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
22499   verifyFormat("void Foo () requires (std::copyable<T>)\n"
22500                "{\n"
22501                "  return\n"
22502                "}\n",
22503                Style);
22504 
22505   verifyFormat("void Foo () requires std::copyable<T>\n"
22506                "{\n"
22507                "  return\n"
22508                "}\n",
22509                Style);
22510 
22511   verifyFormat("template <std::semiregular F, std::semiregular... Args>\n"
22512                "  requires (std::invocable<F, std::invoke_result_t<Args>...>)\n"
22513                "struct constant;",
22514                Style);
22515 
22516   verifyFormat("template <std::semiregular F, std::semiregular... Args>\n"
22517                "  requires std::invocable<F, std::invoke_result_t<Args>...>\n"
22518                "struct constant;",
22519                Style);
22520 
22521   verifyFormat("template <class T>\n"
22522                "class plane_with_very_very_very_long_name\n"
22523                "{\n"
22524                "  constexpr plane_with_very_very_very_long_name () requires "
22525                "std::copyable<T>\n"
22526                "      : plane_with_very_very_very_long_name (1)\n"
22527                "  {\n"
22528                "  }\n"
22529                "}\n",
22530                Style);
22531 
22532   verifyFormat("template <class T>\n"
22533                "class plane_with_long_name\n"
22534                "{\n"
22535                "  constexpr plane_with_long_name () requires std::copyable<T>\n"
22536                "      : plane_with_long_name (1)\n"
22537                "  {\n"
22538                "  }\n"
22539                "}\n",
22540                Style);
22541 
22542   Style.BreakBeforeConceptDeclarations = false;
22543   verifyFormat("template <typename T> concept Tree = true;", Style);
22544 
22545   Style.IndentRequires = false;
22546   verifyFormat("template <std::semiregular F, std::semiregular... Args>\n"
22547                "requires (std::invocable<F, std::invoke_result_t<Args>...>) "
22548                "struct constant;",
22549                Style);
22550 }
22551 
22552 TEST_F(FormatTest, StatementAttributeLikeMacros) {
22553   FormatStyle Style = getLLVMStyle();
22554   StringRef Source = "void Foo::slot() {\n"
22555                      "  unsigned char MyChar = 'x';\n"
22556                      "  emit signal(MyChar);\n"
22557                      "  Q_EMIT signal(MyChar);\n"
22558                      "}";
22559 
22560   EXPECT_EQ(Source, format(Source, Style));
22561 
22562   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
22563   EXPECT_EQ("void Foo::slot() {\n"
22564             "  unsigned char MyChar = 'x';\n"
22565             "  emit          signal(MyChar);\n"
22566             "  Q_EMIT signal(MyChar);\n"
22567             "}",
22568             format(Source, Style));
22569 
22570   Style.StatementAttributeLikeMacros.push_back("emit");
22571   EXPECT_EQ(Source, format(Source, Style));
22572 
22573   Style.StatementAttributeLikeMacros = {};
22574   EXPECT_EQ("void Foo::slot() {\n"
22575             "  unsigned char MyChar = 'x';\n"
22576             "  emit          signal(MyChar);\n"
22577             "  Q_EMIT        signal(MyChar);\n"
22578             "}",
22579             format(Source, Style));
22580 }
22581 
22582 TEST_F(FormatTest, IndentAccessModifiers) {
22583   FormatStyle Style = getLLVMStyle();
22584   Style.IndentAccessModifiers = true;
22585   // Members are *two* levels below the record;
22586   // Style.IndentWidth == 2, thus yielding a 4 spaces wide indentation.
22587   verifyFormat("class C {\n"
22588                "    int i;\n"
22589                "};\n",
22590                Style);
22591   verifyFormat("union C {\n"
22592                "    int i;\n"
22593                "    unsigned u;\n"
22594                "};\n",
22595                Style);
22596   // Access modifiers should be indented one level below the record.
22597   verifyFormat("class C {\n"
22598                "  public:\n"
22599                "    int i;\n"
22600                "};\n",
22601                Style);
22602   verifyFormat("struct S {\n"
22603                "  private:\n"
22604                "    class C {\n"
22605                "        int j;\n"
22606                "\n"
22607                "      public:\n"
22608                "        C();\n"
22609                "    };\n"
22610                "\n"
22611                "  public:\n"
22612                "    int i;\n"
22613                "};\n",
22614                Style);
22615   // Enumerations are not records and should be unaffected.
22616   Style.AllowShortEnumsOnASingleLine = false;
22617   verifyFormat("enum class E {\n"
22618                "  A,\n"
22619                "  B\n"
22620                "};\n",
22621                Style);
22622   // Test with a different indentation width;
22623   // also proves that the result is Style.AccessModifierOffset agnostic.
22624   Style.IndentWidth = 3;
22625   verifyFormat("class C {\n"
22626                "   public:\n"
22627                "      int i;\n"
22628                "};\n",
22629                Style);
22630 }
22631 
22632 TEST_F(FormatTest, LimitlessStringsAndComments) {
22633   auto Style = getLLVMStyleWithColumns(0);
22634   constexpr StringRef Code =
22635       "/**\n"
22636       " * This is a multiline comment with quite some long lines, at least for "
22637       "the LLVM Style.\n"
22638       " * We will redo this with strings and line comments. Just to  check if "
22639       "everything is working.\n"
22640       " */\n"
22641       "bool foo() {\n"
22642       "  /* Single line multi line comment. */\n"
22643       "  const std::string String = \"This is a multiline string with quite "
22644       "some long lines, at least for the LLVM Style.\"\n"
22645       "                             \"We already did it with multi line "
22646       "comments, and we will do it with line comments. Just to check if "
22647       "everything is working.\";\n"
22648       "  // This is a line comment (block) with quite some long lines, at "
22649       "least for the LLVM Style.\n"
22650       "  // We already did this with multi line comments and strings. Just to "
22651       "check if everything is working.\n"
22652       "  const std::string SmallString = \"Hello World\";\n"
22653       "  // Small line comment\n"
22654       "  return String.size() > SmallString.size();\n"
22655       "}";
22656   EXPECT_EQ(Code, format(Code, Style));
22657 }
22658 
22659 TEST_F(FormatTest, FormatDecayCopy) {
22660   // error cases from unit tests
22661   verifyFormat("foo(auto())");
22662   verifyFormat("foo(auto{})");
22663   verifyFormat("foo(auto({}))");
22664   verifyFormat("foo(auto{{}})");
22665 
22666   verifyFormat("foo(auto(1))");
22667   verifyFormat("foo(auto{1})");
22668   verifyFormat("foo(new auto(1))");
22669   verifyFormat("foo(new auto{1})");
22670   verifyFormat("decltype(auto(1)) x;");
22671   verifyFormat("decltype(auto{1}) x;");
22672   verifyFormat("auto(x);");
22673   verifyFormat("auto{x};");
22674   verifyFormat("new auto{x};");
22675   verifyFormat("auto{x} = y;");
22676   verifyFormat("auto(x) = y;"); // actually a declaration, but this is clearly
22677                                 // the user's own fault
22678   verifyFormat("integral auto(x) = y;"); // actually a declaration, but this is
22679                                          // clearly the user's own fault
22680   verifyFormat("auto(*p)() = f;");       // actually a declaration; TODO FIXME
22681 }
22682 
22683 TEST_F(FormatTest, Cpp20ModulesSupport) {
22684   FormatStyle Style = getLLVMStyle();
22685   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
22686   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
22687 
22688   verifyFormat("export import foo;", Style);
22689   verifyFormat("export import foo:bar;", Style);
22690   verifyFormat("export import foo.bar;", Style);
22691   verifyFormat("export import foo.bar:baz;", Style);
22692   verifyFormat("export import :bar;", Style);
22693   verifyFormat("export module foo:bar;", Style);
22694   verifyFormat("export module foo;", Style);
22695   verifyFormat("export module foo.bar;", Style);
22696   verifyFormat("export module foo.bar:baz;", Style);
22697   verifyFormat("export import <string_view>;", Style);
22698 
22699   verifyFormat("export type_name var;", Style);
22700   verifyFormat("template <class T> export using A = B<T>;", Style);
22701   verifyFormat("export using A = B;", Style);
22702   verifyFormat("export int func() {\n"
22703                "  foo();\n"
22704                "}",
22705                Style);
22706   verifyFormat("export struct {\n"
22707                "  int foo;\n"
22708                "};",
22709                Style);
22710   verifyFormat("export {\n"
22711                "  int foo;\n"
22712                "};",
22713                Style);
22714   verifyFormat("export export char const *hello() { return \"hello\"; }");
22715 
22716   verifyFormat("import bar;", Style);
22717   verifyFormat("import foo.bar;", Style);
22718   verifyFormat("import foo:bar;", Style);
22719   verifyFormat("import :bar;", Style);
22720   verifyFormat("import <ctime>;", Style);
22721   verifyFormat("import \"header\";", Style);
22722 
22723   verifyFormat("module foo;", Style);
22724   verifyFormat("module foo:bar;", Style);
22725   verifyFormat("module foo.bar;", Style);
22726   verifyFormat("module;", Style);
22727 
22728   verifyFormat("export namespace hi {\n"
22729                "const char *sayhi();\n"
22730                "}",
22731                Style);
22732 
22733   verifyFormat("module :private;", Style);
22734   verifyFormat("import <foo/bar.h>;", Style);
22735   verifyFormat("import foo...bar;", Style);
22736   verifyFormat("import ..........;", Style);
22737   verifyFormat("module foo:private;", Style);
22738   verifyFormat("import a", Style);
22739   verifyFormat("module a", Style);
22740   verifyFormat("export import a", Style);
22741   verifyFormat("export module a", Style);
22742 
22743   verifyFormat("import", Style);
22744   verifyFormat("module", Style);
22745   verifyFormat("export", Style);
22746 }
22747 
22748 TEST_F(FormatTest, CoroutineForCoawait) {
22749   FormatStyle Style = getLLVMStyle();
22750   verifyFormat("for co_await (auto x : range())\n  ;");
22751   verifyFormat("for (auto i : arr) {\n"
22752                "}",
22753                Style);
22754   verifyFormat("for co_await (auto i : arr) {\n"
22755                "}",
22756                Style);
22757   verifyFormat("for co_await (auto i : foo(T{})) {\n"
22758                "}",
22759                Style);
22760 }
22761 
22762 TEST_F(FormatTest, CoroutineCoAwait) {
22763   verifyFormat("int x = co_await foo();");
22764   verifyFormat("int x = (co_await foo());");
22765   verifyFormat("co_await (42);");
22766   verifyFormat("void operator co_await(int);");
22767   verifyFormat("void operator co_await(a);");
22768   verifyFormat("co_await a;");
22769   verifyFormat("co_await missing_await_resume{};");
22770   verifyFormat("co_await a; // comment");
22771   verifyFormat("void test0() { co_await a; }");
22772   verifyFormat("co_await co_await co_await foo();");
22773   verifyFormat("co_await foo().bar();");
22774   verifyFormat("co_await [this]() -> Task { co_return x; }");
22775   verifyFormat("co_await [this](int a, int b) -> Task { co_return co_await "
22776                "foo(); }(x, y);");
22777 
22778   FormatStyle Style = getLLVMStyle();
22779   Style.ColumnLimit = 40;
22780   verifyFormat("co_await [this](int a, int b) -> Task {\n"
22781                "  co_return co_await foo();\n"
22782                "}(x, y);",
22783                Style);
22784   verifyFormat("co_await;");
22785 }
22786 
22787 TEST_F(FormatTest, CoroutineCoYield) {
22788   verifyFormat("int x = co_yield foo();");
22789   verifyFormat("int x = (co_yield foo());");
22790   verifyFormat("co_yield (42);");
22791   verifyFormat("co_yield {42};");
22792   verifyFormat("co_yield 42;");
22793   verifyFormat("co_yield n++;");
22794   verifyFormat("co_yield ++n;");
22795   verifyFormat("co_yield;");
22796 }
22797 
22798 TEST_F(FormatTest, CoroutineCoReturn) {
22799   verifyFormat("co_return (42);");
22800   verifyFormat("co_return;");
22801   verifyFormat("co_return {};");
22802   verifyFormat("co_return x;");
22803   verifyFormat("co_return co_await foo();");
22804   verifyFormat("co_return co_yield foo();");
22805 }
22806 
22807 } // namespace
22808 } // namespace format
22809 } // namespace clang
22810